diff --git a/.gitignore b/.gitignore index 4fff675801f70..f1ea04e3efb38 100644 --- a/.gitignore +++ b/.gitignore @@ -41,7 +41,9 @@ tests/cases/**/*.js.map scripts/debug.bat scripts/run.bat scripts/word2md.js +scripts/buildProtocol.js scripts/ior.js +scripts/buildProtocol.js scripts/*.js.map scripts/typings/ coverage/ diff --git a/Jakefile.js b/Jakefile.js index e55933ac03dc5..66ecb0a786d31 100644 --- a/Jakefile.js +++ b/Jakefile.js @@ -71,13 +71,14 @@ var compilerSources = [ "visitor.ts", "transformers/destructuring.ts", "transformers/ts.ts", - "transformers/module/es6.ts", + "transformers/module/es2015.ts", "transformers/module/system.ts", "transformers/module/module.ts", "transformers/jsx.ts", - "transformers/es7.ts", + "transformers/es2017.ts", + "transformers/es2016.ts", + "transformers/es2015.ts", "transformers/generators.ts", - "transformers/es6.ts", "transformer.ts", "sourcemap.ts", "comments.ts", @@ -106,13 +107,14 @@ var servicesSources = [ "visitor.ts", "transformers/destructuring.ts", "transformers/ts.ts", - "transformers/module/es6.ts", + "transformers/module/es2015.ts", "transformers/module/system.ts", "transformers/module/module.ts", "transformers/jsx.ts", - "transformers/es7.ts", + "transformers/es2017.ts", + "transformers/es2016.ts", + "transformers/es2015.ts", "transformers/generators.ts", - "transformers/es6.ts", "transformer.ts", "sourcemap.ts", "comments.ts", @@ -176,7 +178,7 @@ var serverCoreSources = [ "lsHost.ts", "project.ts", "editorServices.ts", - "protocol.d.ts", + "protocol.ts", "session.ts", "server.ts" ].map(function (f) { @@ -200,14 +202,13 @@ var typingsInstallerSources = [ var serverSources = serverCoreSources.concat(servicesSources); var languageServiceLibrarySources = [ - "protocol.d.ts", + "protocol.ts", "utilities.ts", "scriptVersionCache.ts", "scriptInfo.ts", "lsHost.ts", "project.ts", "editorServices.ts", - "protocol.d.ts", "session.ts", ].map(function (f) { @@ -261,7 +262,7 @@ var harnessSources = harnessCoreSources.concat([ ].map(function (f) { return path.join(unittestsDirectory, f); })).concat([ - "protocol.d.ts", + "protocol.ts", "utilities.ts", "scriptVersionCache.ts", "scriptInfo.ts", @@ -269,7 +270,6 @@ var harnessSources = harnessCoreSources.concat([ "project.ts", "typingsCache.ts", "editorServices.ts", - "protocol.d.ts", "session.ts", ].map(function (f) { return path.join(serverDirectory, f); @@ -520,6 +520,40 @@ compileFile(processDiagnosticMessagesJs, [], /*useBuiltCompiler*/ false); +var buildProtocolTs = path.join(scriptsDirectory, "buildProtocol.ts"); +var buildProtocolJs = path.join(scriptsDirectory, "buildProtocol.js"); +var buildProtocolDts = path.join(builtLocalDirectory, "protocol.d.ts"); +var typescriptServicesDts = path.join(builtLocalDirectory, "typescriptServices.d.ts"); + +file(buildProtocolTs); + +compileFile(buildProtocolJs, + [buildProtocolTs], + [buildProtocolTs], + [], + /*useBuiltCompiler*/ false, + {noOutFile: true}); + +file(buildProtocolDts, [buildProtocolTs, buildProtocolJs, typescriptServicesDts], function() { + + var protocolTs = path.join(serverDirectory, "protocol.ts"); + + var cmd = host + " " + buildProtocolJs + " "+ protocolTs + " " + typescriptServicesDts + " " + buildProtocolDts; + console.log(cmd); + var ex = jake.createExec([cmd]); + // Add listeners for output and error + ex.addListener("stdout", function (output) { + process.stdout.write(output); + }); + ex.addListener("stderr", function (error) { + process.stderr.write(error); + }); + ex.addListener("cmdEnd", function () { + complete(); + }); + ex.run(); +}, { async: true }) + // The generated diagnostics map; built for the compiler and for the 'generate-diagnostics' task file(diagnosticInfoMapTs, [processDiagnosticMessagesJs, diagnosticMessagesJson], function () { var cmd = host + " " + processDiagnosticMessagesJs + " " + diagnosticMessagesJson; @@ -657,6 +691,8 @@ compileFile( inlineSourceMap: true }); +file(typescriptServicesDts, [servicesFile]); + var cancellationTokenFile = path.join(builtLocalDirectory, "cancellationToken.js"); compileFile(cancellationTokenFile, cancellationTokenSources, [builtLocalDirectory].concat(cancellationTokenSources), /*prefixes*/ [copyright], /*useBuiltCompiler*/ true, { outDir: builtLocalDirectory, noOutFile: true }); @@ -691,7 +727,7 @@ task("build-fold-end", [], function () { // Local target to build the compiler and services desc("Builds the full compiler and services"); -task("local", ["build-fold-start", "generate-diagnostics", "lib", tscFile, servicesFile, nodeDefinitionsFile, serverFile, builtGeneratedDiagnosticMessagesJSON, "lssl", "build-fold-end"]); +task("local", ["build-fold-start", "generate-diagnostics", "lib", tscFile, servicesFile, nodeDefinitionsFile, serverFile, buildProtocolDts, builtGeneratedDiagnosticMessagesJSON, "lssl", "build-fold-end"]); // Local target to build only tsc.js desc("Builds only the compiler"); @@ -747,7 +783,7 @@ task("generate-spec", [specMd]); // Makes a new LKG. This target does not build anything, but errors if not all the outputs are present in the built/local directory desc("Makes a new LKG out of the built js files"); task("LKG", ["clean", "release", "local"].concat(libraryTargets), function () { - var expectedFiles = [tscFile, servicesFile, serverFile, nodePackageFile, nodeDefinitionsFile, standaloneDefinitionsFile, tsserverLibraryFile, tsserverLibraryDefinitionFile, cancellationTokenFile, typingsInstallerFile].concat(libraryTargets); + var expectedFiles = [tscFile, servicesFile, serverFile, nodePackageFile, nodeDefinitionsFile, standaloneDefinitionsFile, tsserverLibraryFile, tsserverLibraryDefinitionFile, cancellationTokenFile, typingsInstallerFile, buildProtocolDts].concat(libraryTargets); var missingFiles = expectedFiles.filter(function (f) { return !fs.existsSync(f); }); diff --git a/issue_template.md b/issue_template.md index 7799960ec7805..fcd995317f5a6 100644 --- a/issue_template.md +++ b/issue_template.md @@ -2,7 +2,7 @@ -**TypeScript Version:** 1.8.0 / nightly (2.0.0-dev.201xxxxx) +**TypeScript Version:** 2.0.3 / nightly (2.1.0-dev.201xxxxx) **Code** diff --git a/lib/lib.d.ts b/lib/lib.d.ts index 586b3675305d3..922820b5d90a5 100644 --- a/lib/lib.d.ts +++ b/lib/lib.d.ts @@ -260,6 +260,9 @@ interface Function { */ bind(this: Function, thisArg: any, ...argArray: any[]): any; + /** Returns a string representation of a function. */ + toString(): string; + prototype: any; readonly length: number; @@ -1216,6 +1219,30 @@ interface Array { * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. */ forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void; + /** + * Calls a defined callback function on each element of an array, and returns an array that contains the results. + * @param callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array. + * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. + */ + map(this: [T, T, T, T, T], callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): [U, U, U, U, U]; + /** + * Calls a defined callback function on each element of an array, and returns an array that contains the results. + * @param callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array. + * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. + */ + map(this: [T, T, T, T], callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): [U, U, U, U]; + /** + * Calls a defined callback function on each element of an array, and returns an array that contains the results. + * @param callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array. + * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. + */ + map(this: [T, T, T], callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): [U, U, U]; + /** + * Calls a defined callback function on each element of an array, and returns an array that contains the results. + * @param callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array. + * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. + */ + map(this: [T, T], callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): [U, U]; /** * Calls a defined callback function on each element of an array, and returns an array that contains the results. * @param callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array. @@ -11660,11 +11687,12 @@ declare var HashChangeEvent: { interface History { readonly length: number; readonly state: any; - back(distance?: any): void; - forward(distance?: any): void; - go(delta?: any): void; - pushState(statedata: any, title?: string, url?: string): void; - replaceState(statedata: any, title?: string, url?: string): void; + scrollRestoration: ScrollRestoration; + back(): void; + forward(): void; + go(delta?: number): void; + pushState(data: any, title: string, url?: string | null): void; + replaceState(data: any, title: string, url?: string | null): void; } declare var History: { @@ -17236,7 +17264,6 @@ interface Window extends EventTarget, WindowTimers, WindowSessionStorage, Window addEventListener(type: "waiting", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; addEventListener(type: "wheel", listener: (this: this, ev: WheelEvent) => any, useCapture?: boolean): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void; - [index: number]: Window; } declare var Window: { @@ -17267,7 +17294,7 @@ declare var XMLDocument: { } interface XMLHttpRequest extends EventTarget, XMLHttpRequestEventTarget { - onreadystatechange: (this: this, ev: ProgressEvent) => any; + onreadystatechange: (this: this, ev: Event) => any; readonly readyState: number; readonly response: any; readonly responseText: string; @@ -17294,13 +17321,13 @@ interface XMLHttpRequest extends EventTarget, XMLHttpRequestEventTarget { readonly LOADING: number; readonly OPENED: number; readonly UNSENT: number; - addEventListener(type: "abort", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; - addEventListener(type: "error", listener: (this: this, ev: ErrorEvent) => any, useCapture?: boolean): void; - addEventListener(type: "load", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; + addEventListener(type: "abort", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; + addEventListener(type: "error", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; + addEventListener(type: "load", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; addEventListener(type: "loadend", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; - addEventListener(type: "loadstart", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; + addEventListener(type: "loadstart", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; addEventListener(type: "progress", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; - addEventListener(type: "readystatechange", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; + addEventListener(type: "readystatechange", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; addEventListener(type: "timeout", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void; } @@ -17641,183 +17668,183 @@ interface NavigatorUserMedia { } interface NodeSelector { - querySelector(selectors: "a"): HTMLAnchorElement; - querySelector(selectors: "abbr"): HTMLElement; - querySelector(selectors: "acronym"): HTMLElement; - querySelector(selectors: "address"): HTMLElement; - querySelector(selectors: "applet"): HTMLAppletElement; - querySelector(selectors: "area"): HTMLAreaElement; - querySelector(selectors: "article"): HTMLElement; - querySelector(selectors: "aside"): HTMLElement; - querySelector(selectors: "audio"): HTMLAudioElement; - querySelector(selectors: "b"): HTMLElement; - querySelector(selectors: "base"): HTMLBaseElement; - querySelector(selectors: "basefont"): HTMLBaseFontElement; - querySelector(selectors: "bdo"): HTMLElement; - querySelector(selectors: "big"): HTMLElement; - querySelector(selectors: "blockquote"): HTMLQuoteElement; - querySelector(selectors: "body"): HTMLBodyElement; - querySelector(selectors: "br"): HTMLBRElement; - querySelector(selectors: "button"): HTMLButtonElement; - querySelector(selectors: "canvas"): HTMLCanvasElement; - querySelector(selectors: "caption"): HTMLTableCaptionElement; - querySelector(selectors: "center"): HTMLElement; - querySelector(selectors: "circle"): SVGCircleElement; - querySelector(selectors: "cite"): HTMLElement; - querySelector(selectors: "clippath"): SVGClipPathElement; - querySelector(selectors: "code"): HTMLElement; - querySelector(selectors: "col"): HTMLTableColElement; - querySelector(selectors: "colgroup"): HTMLTableColElement; - querySelector(selectors: "datalist"): HTMLDataListElement; - querySelector(selectors: "dd"): HTMLElement; - querySelector(selectors: "defs"): SVGDefsElement; - querySelector(selectors: "del"): HTMLModElement; - querySelector(selectors: "desc"): SVGDescElement; - querySelector(selectors: "dfn"): HTMLElement; - querySelector(selectors: "dir"): HTMLDirectoryElement; - querySelector(selectors: "div"): HTMLDivElement; - querySelector(selectors: "dl"): HTMLDListElement; - querySelector(selectors: "dt"): HTMLElement; - querySelector(selectors: "ellipse"): SVGEllipseElement; - querySelector(selectors: "em"): HTMLElement; - querySelector(selectors: "embed"): HTMLEmbedElement; - querySelector(selectors: "feblend"): SVGFEBlendElement; - querySelector(selectors: "fecolormatrix"): SVGFEColorMatrixElement; - querySelector(selectors: "fecomponenttransfer"): SVGFEComponentTransferElement; - querySelector(selectors: "fecomposite"): SVGFECompositeElement; - querySelector(selectors: "feconvolvematrix"): SVGFEConvolveMatrixElement; - querySelector(selectors: "fediffuselighting"): SVGFEDiffuseLightingElement; - querySelector(selectors: "fedisplacementmap"): SVGFEDisplacementMapElement; - querySelector(selectors: "fedistantlight"): SVGFEDistantLightElement; - querySelector(selectors: "feflood"): SVGFEFloodElement; - querySelector(selectors: "fefunca"): SVGFEFuncAElement; - querySelector(selectors: "fefuncb"): SVGFEFuncBElement; - querySelector(selectors: "fefuncg"): SVGFEFuncGElement; - querySelector(selectors: "fefuncr"): SVGFEFuncRElement; - querySelector(selectors: "fegaussianblur"): SVGFEGaussianBlurElement; - querySelector(selectors: "feimage"): SVGFEImageElement; - querySelector(selectors: "femerge"): SVGFEMergeElement; - querySelector(selectors: "femergenode"): SVGFEMergeNodeElement; - querySelector(selectors: "femorphology"): SVGFEMorphologyElement; - querySelector(selectors: "feoffset"): SVGFEOffsetElement; - querySelector(selectors: "fepointlight"): SVGFEPointLightElement; - querySelector(selectors: "fespecularlighting"): SVGFESpecularLightingElement; - querySelector(selectors: "fespotlight"): SVGFESpotLightElement; - querySelector(selectors: "fetile"): SVGFETileElement; - querySelector(selectors: "feturbulence"): SVGFETurbulenceElement; - querySelector(selectors: "fieldset"): HTMLFieldSetElement; - querySelector(selectors: "figcaption"): HTMLElement; - querySelector(selectors: "figure"): HTMLElement; - querySelector(selectors: "filter"): SVGFilterElement; - querySelector(selectors: "font"): HTMLFontElement; - querySelector(selectors: "footer"): HTMLElement; - querySelector(selectors: "foreignobject"): SVGForeignObjectElement; - querySelector(selectors: "form"): HTMLFormElement; - querySelector(selectors: "frame"): HTMLFrameElement; - querySelector(selectors: "frameset"): HTMLFrameSetElement; - querySelector(selectors: "g"): SVGGElement; - querySelector(selectors: "h1"): HTMLHeadingElement; - querySelector(selectors: "h2"): HTMLHeadingElement; - querySelector(selectors: "h3"): HTMLHeadingElement; - querySelector(selectors: "h4"): HTMLHeadingElement; - querySelector(selectors: "h5"): HTMLHeadingElement; - querySelector(selectors: "h6"): HTMLHeadingElement; - querySelector(selectors: "head"): HTMLHeadElement; - querySelector(selectors: "header"): HTMLElement; - querySelector(selectors: "hgroup"): HTMLElement; - querySelector(selectors: "hr"): HTMLHRElement; - querySelector(selectors: "html"): HTMLHtmlElement; - querySelector(selectors: "i"): HTMLElement; - querySelector(selectors: "iframe"): HTMLIFrameElement; - querySelector(selectors: "image"): SVGImageElement; - querySelector(selectors: "img"): HTMLImageElement; - querySelector(selectors: "input"): HTMLInputElement; - querySelector(selectors: "ins"): HTMLModElement; - querySelector(selectors: "isindex"): HTMLUnknownElement; - querySelector(selectors: "kbd"): HTMLElement; - querySelector(selectors: "keygen"): HTMLElement; - querySelector(selectors: "label"): HTMLLabelElement; - querySelector(selectors: "legend"): HTMLLegendElement; - querySelector(selectors: "li"): HTMLLIElement; - querySelector(selectors: "line"): SVGLineElement; - querySelector(selectors: "lineargradient"): SVGLinearGradientElement; - querySelector(selectors: "link"): HTMLLinkElement; - querySelector(selectors: "listing"): HTMLPreElement; - querySelector(selectors: "map"): HTMLMapElement; - querySelector(selectors: "mark"): HTMLElement; - querySelector(selectors: "marker"): SVGMarkerElement; - querySelector(selectors: "marquee"): HTMLMarqueeElement; - querySelector(selectors: "mask"): SVGMaskElement; - querySelector(selectors: "menu"): HTMLMenuElement; - querySelector(selectors: "meta"): HTMLMetaElement; - querySelector(selectors: "metadata"): SVGMetadataElement; - querySelector(selectors: "meter"): HTMLMeterElement; - querySelector(selectors: "nav"): HTMLElement; - querySelector(selectors: "nextid"): HTMLUnknownElement; - querySelector(selectors: "nobr"): HTMLElement; - querySelector(selectors: "noframes"): HTMLElement; - querySelector(selectors: "noscript"): HTMLElement; - querySelector(selectors: "object"): HTMLObjectElement; - querySelector(selectors: "ol"): HTMLOListElement; - querySelector(selectors: "optgroup"): HTMLOptGroupElement; - querySelector(selectors: "option"): HTMLOptionElement; - querySelector(selectors: "p"): HTMLParagraphElement; - querySelector(selectors: "param"): HTMLParamElement; - querySelector(selectors: "path"): SVGPathElement; - querySelector(selectors: "pattern"): SVGPatternElement; - querySelector(selectors: "picture"): HTMLPictureElement; - querySelector(selectors: "plaintext"): HTMLElement; - querySelector(selectors: "polygon"): SVGPolygonElement; - querySelector(selectors: "polyline"): SVGPolylineElement; - querySelector(selectors: "pre"): HTMLPreElement; - querySelector(selectors: "progress"): HTMLProgressElement; - querySelector(selectors: "q"): HTMLQuoteElement; - querySelector(selectors: "radialgradient"): SVGRadialGradientElement; - querySelector(selectors: "rect"): SVGRectElement; - querySelector(selectors: "rt"): HTMLElement; - querySelector(selectors: "ruby"): HTMLElement; - querySelector(selectors: "s"): HTMLElement; - querySelector(selectors: "samp"): HTMLElement; - querySelector(selectors: "script"): HTMLScriptElement; - querySelector(selectors: "section"): HTMLElement; - querySelector(selectors: "select"): HTMLSelectElement; - querySelector(selectors: "small"): HTMLElement; - querySelector(selectors: "source"): HTMLSourceElement; - querySelector(selectors: "span"): HTMLSpanElement; - querySelector(selectors: "stop"): SVGStopElement; - querySelector(selectors: "strike"): HTMLElement; - querySelector(selectors: "strong"): HTMLElement; - querySelector(selectors: "style"): HTMLStyleElement; - querySelector(selectors: "sub"): HTMLElement; - querySelector(selectors: "sup"): HTMLElement; - querySelector(selectors: "svg"): SVGSVGElement; - querySelector(selectors: "switch"): SVGSwitchElement; - querySelector(selectors: "symbol"): SVGSymbolElement; - querySelector(selectors: "table"): HTMLTableElement; - querySelector(selectors: "tbody"): HTMLTableSectionElement; - querySelector(selectors: "td"): HTMLTableDataCellElement; - querySelector(selectors: "template"): HTMLTemplateElement; - querySelector(selectors: "text"): SVGTextElement; - querySelector(selectors: "textpath"): SVGTextPathElement; - querySelector(selectors: "textarea"): HTMLTextAreaElement; - querySelector(selectors: "tfoot"): HTMLTableSectionElement; - querySelector(selectors: "th"): HTMLTableHeaderCellElement; - querySelector(selectors: "thead"): HTMLTableSectionElement; - querySelector(selectors: "title"): HTMLTitleElement; - querySelector(selectors: "tr"): HTMLTableRowElement; - querySelector(selectors: "track"): HTMLTrackElement; - querySelector(selectors: "tspan"): SVGTSpanElement; - querySelector(selectors: "tt"): HTMLElement; - querySelector(selectors: "u"): HTMLElement; - querySelector(selectors: "ul"): HTMLUListElement; - querySelector(selectors: "use"): SVGUseElement; - querySelector(selectors: "var"): HTMLElement; - querySelector(selectors: "video"): HTMLVideoElement; - querySelector(selectors: "view"): SVGViewElement; - querySelector(selectors: "wbr"): HTMLElement; - querySelector(selectors: "x-ms-webview"): MSHTMLWebViewElement; - querySelector(selectors: "xmp"): HTMLPreElement; - querySelector(selectors: string): Element; + querySelector(selectors: "a"): HTMLAnchorElement | null; + querySelector(selectors: "abbr"): HTMLElement | null; + querySelector(selectors: "acronym"): HTMLElement | null; + querySelector(selectors: "address"): HTMLElement | null; + querySelector(selectors: "applet"): HTMLAppletElement | null; + querySelector(selectors: "area"): HTMLAreaElement | null; + querySelector(selectors: "article"): HTMLElement | null; + querySelector(selectors: "aside"): HTMLElement | null; + querySelector(selectors: "audio"): HTMLAudioElement | null; + querySelector(selectors: "b"): HTMLElement | null; + querySelector(selectors: "base"): HTMLBaseElement | null; + querySelector(selectors: "basefont"): HTMLBaseFontElement | null; + querySelector(selectors: "bdo"): HTMLElement | null; + querySelector(selectors: "big"): HTMLElement | null; + querySelector(selectors: "blockquote"): HTMLQuoteElement | null; + querySelector(selectors: "body"): HTMLBodyElement | null; + querySelector(selectors: "br"): HTMLBRElement | null; + querySelector(selectors: "button"): HTMLButtonElement | null; + querySelector(selectors: "canvas"): HTMLCanvasElement | null; + querySelector(selectors: "caption"): HTMLTableCaptionElement | null; + querySelector(selectors: "center"): HTMLElement | null; + querySelector(selectors: "circle"): SVGCircleElement | null; + querySelector(selectors: "cite"): HTMLElement | null; + querySelector(selectors: "clippath"): SVGClipPathElement | null; + querySelector(selectors: "code"): HTMLElement | null; + querySelector(selectors: "col"): HTMLTableColElement | null; + querySelector(selectors: "colgroup"): HTMLTableColElement | null; + querySelector(selectors: "datalist"): HTMLDataListElement | null; + querySelector(selectors: "dd"): HTMLElement | null; + querySelector(selectors: "defs"): SVGDefsElement | null; + querySelector(selectors: "del"): HTMLModElement | null; + querySelector(selectors: "desc"): SVGDescElement | null; + querySelector(selectors: "dfn"): HTMLElement | null; + querySelector(selectors: "dir"): HTMLDirectoryElement | null; + querySelector(selectors: "div"): HTMLDivElement | null; + querySelector(selectors: "dl"): HTMLDListElement | null; + querySelector(selectors: "dt"): HTMLElement | null; + querySelector(selectors: "ellipse"): SVGEllipseElement | null; + querySelector(selectors: "em"): HTMLElement | null; + querySelector(selectors: "embed"): HTMLEmbedElement | null; + querySelector(selectors: "feblend"): SVGFEBlendElement | null; + querySelector(selectors: "fecolormatrix"): SVGFEColorMatrixElement | null; + querySelector(selectors: "fecomponenttransfer"): SVGFEComponentTransferElement | null; + querySelector(selectors: "fecomposite"): SVGFECompositeElement | null; + querySelector(selectors: "feconvolvematrix"): SVGFEConvolveMatrixElement | null; + querySelector(selectors: "fediffuselighting"): SVGFEDiffuseLightingElement | null; + querySelector(selectors: "fedisplacementmap"): SVGFEDisplacementMapElement | null; + querySelector(selectors: "fedistantlight"): SVGFEDistantLightElement | null; + querySelector(selectors: "feflood"): SVGFEFloodElement | null; + querySelector(selectors: "fefunca"): SVGFEFuncAElement | null; + querySelector(selectors: "fefuncb"): SVGFEFuncBElement | null; + querySelector(selectors: "fefuncg"): SVGFEFuncGElement | null; + querySelector(selectors: "fefuncr"): SVGFEFuncRElement | null; + querySelector(selectors: "fegaussianblur"): SVGFEGaussianBlurElement | null; + querySelector(selectors: "feimage"): SVGFEImageElement | null; + querySelector(selectors: "femerge"): SVGFEMergeElement | null; + querySelector(selectors: "femergenode"): SVGFEMergeNodeElement | null; + querySelector(selectors: "femorphology"): SVGFEMorphologyElement | null; + querySelector(selectors: "feoffset"): SVGFEOffsetElement | null; + querySelector(selectors: "fepointlight"): SVGFEPointLightElement | null; + querySelector(selectors: "fespecularlighting"): SVGFESpecularLightingElement | null; + querySelector(selectors: "fespotlight"): SVGFESpotLightElement | null; + querySelector(selectors: "fetile"): SVGFETileElement | null; + querySelector(selectors: "feturbulence"): SVGFETurbulenceElement | null; + querySelector(selectors: "fieldset"): HTMLFieldSetElement | null; + querySelector(selectors: "figcaption"): HTMLElement | null; + querySelector(selectors: "figure"): HTMLElement | null; + querySelector(selectors: "filter"): SVGFilterElement | null; + querySelector(selectors: "font"): HTMLFontElement | null; + querySelector(selectors: "footer"): HTMLElement | null; + querySelector(selectors: "foreignobject"): SVGForeignObjectElement | null; + querySelector(selectors: "form"): HTMLFormElement | null; + querySelector(selectors: "frame"): HTMLFrameElement | null; + querySelector(selectors: "frameset"): HTMLFrameSetElement | null; + querySelector(selectors: "g"): SVGGElement | null; + querySelector(selectors: "h1"): HTMLHeadingElement | null; + querySelector(selectors: "h2"): HTMLHeadingElement | null; + querySelector(selectors: "h3"): HTMLHeadingElement | null; + querySelector(selectors: "h4"): HTMLHeadingElement | null; + querySelector(selectors: "h5"): HTMLHeadingElement | null; + querySelector(selectors: "h6"): HTMLHeadingElement | null; + querySelector(selectors: "head"): HTMLHeadElement | null; + querySelector(selectors: "header"): HTMLElement | null; + querySelector(selectors: "hgroup"): HTMLElement | null; + querySelector(selectors: "hr"): HTMLHRElement | null; + querySelector(selectors: "html"): HTMLHtmlElement | null; + querySelector(selectors: "i"): HTMLElement | null; + querySelector(selectors: "iframe"): HTMLIFrameElement | null; + querySelector(selectors: "image"): SVGImageElement | null; + querySelector(selectors: "img"): HTMLImageElement | null; + querySelector(selectors: "input"): HTMLInputElement | null; + querySelector(selectors: "ins"): HTMLModElement | null; + querySelector(selectors: "isindex"): HTMLUnknownElement | null; + querySelector(selectors: "kbd"): HTMLElement | null; + querySelector(selectors: "keygen"): HTMLElement | null; + querySelector(selectors: "label"): HTMLLabelElement | null; + querySelector(selectors: "legend"): HTMLLegendElement | null; + querySelector(selectors: "li"): HTMLLIElement | null; + querySelector(selectors: "line"): SVGLineElement | null; + querySelector(selectors: "lineargradient"): SVGLinearGradientElement | null; + querySelector(selectors: "link"): HTMLLinkElement | null; + querySelector(selectors: "listing"): HTMLPreElement | null; + querySelector(selectors: "map"): HTMLMapElement | null; + querySelector(selectors: "mark"): HTMLElement | null; + querySelector(selectors: "marker"): SVGMarkerElement | null; + querySelector(selectors: "marquee"): HTMLMarqueeElement | null; + querySelector(selectors: "mask"): SVGMaskElement | null; + querySelector(selectors: "menu"): HTMLMenuElement | null; + querySelector(selectors: "meta"): HTMLMetaElement | null; + querySelector(selectors: "metadata"): SVGMetadataElement | null; + querySelector(selectors: "meter"): HTMLMeterElement | null; + querySelector(selectors: "nav"): HTMLElement | null; + querySelector(selectors: "nextid"): HTMLUnknownElement | null; + querySelector(selectors: "nobr"): HTMLElement | null; + querySelector(selectors: "noframes"): HTMLElement | null; + querySelector(selectors: "noscript"): HTMLElement | null; + querySelector(selectors: "object"): HTMLObjectElement | null; + querySelector(selectors: "ol"): HTMLOListElement | null; + querySelector(selectors: "optgroup"): HTMLOptGroupElement | null; + querySelector(selectors: "option"): HTMLOptionElement | null; + querySelector(selectors: "p"): HTMLParagraphElement | null; + querySelector(selectors: "param"): HTMLParamElement | null; + querySelector(selectors: "path"): SVGPathElement | null; + querySelector(selectors: "pattern"): SVGPatternElement | null; + querySelector(selectors: "picture"): HTMLPictureElement | null; + querySelector(selectors: "plaintext"): HTMLElement | null; + querySelector(selectors: "polygon"): SVGPolygonElement | null; + querySelector(selectors: "polyline"): SVGPolylineElement | null; + querySelector(selectors: "pre"): HTMLPreElement | null; + querySelector(selectors: "progress"): HTMLProgressElement | null; + querySelector(selectors: "q"): HTMLQuoteElement | null; + querySelector(selectors: "radialgradient"): SVGRadialGradientElement | null; + querySelector(selectors: "rect"): SVGRectElement | null; + querySelector(selectors: "rt"): HTMLElement | null; + querySelector(selectors: "ruby"): HTMLElement | null; + querySelector(selectors: "s"): HTMLElement | null; + querySelector(selectors: "samp"): HTMLElement | null; + querySelector(selectors: "script"): HTMLScriptElement | null; + querySelector(selectors: "section"): HTMLElement | null; + querySelector(selectors: "select"): HTMLSelectElement | null; + querySelector(selectors: "small"): HTMLElement | null; + querySelector(selectors: "source"): HTMLSourceElement | null; + querySelector(selectors: "span"): HTMLSpanElement | null; + querySelector(selectors: "stop"): SVGStopElement | null; + querySelector(selectors: "strike"): HTMLElement | null; + querySelector(selectors: "strong"): HTMLElement | null; + querySelector(selectors: "style"): HTMLStyleElement | null; + querySelector(selectors: "sub"): HTMLElement | null; + querySelector(selectors: "sup"): HTMLElement | null; + querySelector(selectors: "svg"): SVGSVGElement | null; + querySelector(selectors: "switch"): SVGSwitchElement | null; + querySelector(selectors: "symbol"): SVGSymbolElement | null; + querySelector(selectors: "table"): HTMLTableElement | null; + querySelector(selectors: "tbody"): HTMLTableSectionElement | null; + querySelector(selectors: "td"): HTMLTableDataCellElement | null; + querySelector(selectors: "template"): HTMLTemplateElement | null; + querySelector(selectors: "text"): SVGTextElement | null; + querySelector(selectors: "textpath"): SVGTextPathElement | null; + querySelector(selectors: "textarea"): HTMLTextAreaElement | null; + querySelector(selectors: "tfoot"): HTMLTableSectionElement | null; + querySelector(selectors: "th"): HTMLTableHeaderCellElement | null; + querySelector(selectors: "thead"): HTMLTableSectionElement | null; + querySelector(selectors: "title"): HTMLTitleElement | null; + querySelector(selectors: "tr"): HTMLTableRowElement | null; + querySelector(selectors: "track"): HTMLTrackElement | null; + querySelector(selectors: "tspan"): SVGTSpanElement | null; + querySelector(selectors: "tt"): HTMLElement | null; + querySelector(selectors: "u"): HTMLElement | null; + querySelector(selectors: "ul"): HTMLUListElement | null; + querySelector(selectors: "use"): SVGUseElement | null; + querySelector(selectors: "var"): HTMLElement | null; + querySelector(selectors: "video"): HTMLVideoElement | null; + querySelector(selectors: "view"): SVGViewElement | null; + querySelector(selectors: "wbr"): HTMLElement | null; + querySelector(selectors: "x-ms-webview"): MSHTMLWebViewElement | null; + querySelector(selectors: "xmp"): HTMLPreElement | null; + querySelector(selectors: string): Element | null; querySelectorAll(selectors: "a"): NodeListOf; querySelectorAll(selectors: "abbr"): NodeListOf; querySelectorAll(selectors: "acronym"): NodeListOf; @@ -18747,6 +18774,7 @@ type ScrollLogicalPosition = "start" | "center" | "end" | "nearest"; type IDBValidKey = number | string | Date | IDBArrayKey; type BufferSource = ArrayBuffer | ArrayBufferView; type MouseWheelEvent = WheelEvent; +type ScrollRestoration = "auto" | "manual"; ///////////////////////////// /// WorkerGlobalScope APIs ///////////////////////////// diff --git a/lib/lib.dom.d.ts b/lib/lib.dom.d.ts index 4e45a38c17eba..4c19c878064a5 100644 --- a/lib/lib.dom.d.ts +++ b/lib/lib.dom.d.ts @@ -7552,11 +7552,12 @@ declare var HashChangeEvent: { interface History { readonly length: number; readonly state: any; - back(distance?: any): void; - forward(distance?: any): void; - go(delta?: any): void; - pushState(statedata: any, title?: string, url?: string): void; - replaceState(statedata: any, title?: string, url?: string): void; + scrollRestoration: ScrollRestoration; + back(): void; + forward(): void; + go(delta?: number): void; + pushState(data: any, title: string, url?: string | null): void; + replaceState(data: any, title: string, url?: string | null): void; } declare var History: { @@ -13128,7 +13129,6 @@ interface Window extends EventTarget, WindowTimers, WindowSessionStorage, Window addEventListener(type: "waiting", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; addEventListener(type: "wheel", listener: (this: this, ev: WheelEvent) => any, useCapture?: boolean): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void; - [index: number]: Window; } declare var Window: { @@ -13159,7 +13159,7 @@ declare var XMLDocument: { } interface XMLHttpRequest extends EventTarget, XMLHttpRequestEventTarget { - onreadystatechange: (this: this, ev: ProgressEvent) => any; + onreadystatechange: (this: this, ev: Event) => any; readonly readyState: number; readonly response: any; readonly responseText: string; @@ -13186,13 +13186,13 @@ interface XMLHttpRequest extends EventTarget, XMLHttpRequestEventTarget { readonly LOADING: number; readonly OPENED: number; readonly UNSENT: number; - addEventListener(type: "abort", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; - addEventListener(type: "error", listener: (this: this, ev: ErrorEvent) => any, useCapture?: boolean): void; - addEventListener(type: "load", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; + addEventListener(type: "abort", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; + addEventListener(type: "error", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; + addEventListener(type: "load", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; addEventListener(type: "loadend", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; - addEventListener(type: "loadstart", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; + addEventListener(type: "loadstart", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; addEventListener(type: "progress", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; - addEventListener(type: "readystatechange", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; + addEventListener(type: "readystatechange", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; addEventListener(type: "timeout", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void; } @@ -13533,183 +13533,183 @@ interface NavigatorUserMedia { } interface NodeSelector { - querySelector(selectors: "a"): HTMLAnchorElement; - querySelector(selectors: "abbr"): HTMLElement; - querySelector(selectors: "acronym"): HTMLElement; - querySelector(selectors: "address"): HTMLElement; - querySelector(selectors: "applet"): HTMLAppletElement; - querySelector(selectors: "area"): HTMLAreaElement; - querySelector(selectors: "article"): HTMLElement; - querySelector(selectors: "aside"): HTMLElement; - querySelector(selectors: "audio"): HTMLAudioElement; - querySelector(selectors: "b"): HTMLElement; - querySelector(selectors: "base"): HTMLBaseElement; - querySelector(selectors: "basefont"): HTMLBaseFontElement; - querySelector(selectors: "bdo"): HTMLElement; - querySelector(selectors: "big"): HTMLElement; - querySelector(selectors: "blockquote"): HTMLQuoteElement; - querySelector(selectors: "body"): HTMLBodyElement; - querySelector(selectors: "br"): HTMLBRElement; - querySelector(selectors: "button"): HTMLButtonElement; - querySelector(selectors: "canvas"): HTMLCanvasElement; - querySelector(selectors: "caption"): HTMLTableCaptionElement; - querySelector(selectors: "center"): HTMLElement; - querySelector(selectors: "circle"): SVGCircleElement; - querySelector(selectors: "cite"): HTMLElement; - querySelector(selectors: "clippath"): SVGClipPathElement; - querySelector(selectors: "code"): HTMLElement; - querySelector(selectors: "col"): HTMLTableColElement; - querySelector(selectors: "colgroup"): HTMLTableColElement; - querySelector(selectors: "datalist"): HTMLDataListElement; - querySelector(selectors: "dd"): HTMLElement; - querySelector(selectors: "defs"): SVGDefsElement; - querySelector(selectors: "del"): HTMLModElement; - querySelector(selectors: "desc"): SVGDescElement; - querySelector(selectors: "dfn"): HTMLElement; - querySelector(selectors: "dir"): HTMLDirectoryElement; - querySelector(selectors: "div"): HTMLDivElement; - querySelector(selectors: "dl"): HTMLDListElement; - querySelector(selectors: "dt"): HTMLElement; - querySelector(selectors: "ellipse"): SVGEllipseElement; - querySelector(selectors: "em"): HTMLElement; - querySelector(selectors: "embed"): HTMLEmbedElement; - querySelector(selectors: "feblend"): SVGFEBlendElement; - querySelector(selectors: "fecolormatrix"): SVGFEColorMatrixElement; - querySelector(selectors: "fecomponenttransfer"): SVGFEComponentTransferElement; - querySelector(selectors: "fecomposite"): SVGFECompositeElement; - querySelector(selectors: "feconvolvematrix"): SVGFEConvolveMatrixElement; - querySelector(selectors: "fediffuselighting"): SVGFEDiffuseLightingElement; - querySelector(selectors: "fedisplacementmap"): SVGFEDisplacementMapElement; - querySelector(selectors: "fedistantlight"): SVGFEDistantLightElement; - querySelector(selectors: "feflood"): SVGFEFloodElement; - querySelector(selectors: "fefunca"): SVGFEFuncAElement; - querySelector(selectors: "fefuncb"): SVGFEFuncBElement; - querySelector(selectors: "fefuncg"): SVGFEFuncGElement; - querySelector(selectors: "fefuncr"): SVGFEFuncRElement; - querySelector(selectors: "fegaussianblur"): SVGFEGaussianBlurElement; - querySelector(selectors: "feimage"): SVGFEImageElement; - querySelector(selectors: "femerge"): SVGFEMergeElement; - querySelector(selectors: "femergenode"): SVGFEMergeNodeElement; - querySelector(selectors: "femorphology"): SVGFEMorphologyElement; - querySelector(selectors: "feoffset"): SVGFEOffsetElement; - querySelector(selectors: "fepointlight"): SVGFEPointLightElement; - querySelector(selectors: "fespecularlighting"): SVGFESpecularLightingElement; - querySelector(selectors: "fespotlight"): SVGFESpotLightElement; - querySelector(selectors: "fetile"): SVGFETileElement; - querySelector(selectors: "feturbulence"): SVGFETurbulenceElement; - querySelector(selectors: "fieldset"): HTMLFieldSetElement; - querySelector(selectors: "figcaption"): HTMLElement; - querySelector(selectors: "figure"): HTMLElement; - querySelector(selectors: "filter"): SVGFilterElement; - querySelector(selectors: "font"): HTMLFontElement; - querySelector(selectors: "footer"): HTMLElement; - querySelector(selectors: "foreignobject"): SVGForeignObjectElement; - querySelector(selectors: "form"): HTMLFormElement; - querySelector(selectors: "frame"): HTMLFrameElement; - querySelector(selectors: "frameset"): HTMLFrameSetElement; - querySelector(selectors: "g"): SVGGElement; - querySelector(selectors: "h1"): HTMLHeadingElement; - querySelector(selectors: "h2"): HTMLHeadingElement; - querySelector(selectors: "h3"): HTMLHeadingElement; - querySelector(selectors: "h4"): HTMLHeadingElement; - querySelector(selectors: "h5"): HTMLHeadingElement; - querySelector(selectors: "h6"): HTMLHeadingElement; - querySelector(selectors: "head"): HTMLHeadElement; - querySelector(selectors: "header"): HTMLElement; - querySelector(selectors: "hgroup"): HTMLElement; - querySelector(selectors: "hr"): HTMLHRElement; - querySelector(selectors: "html"): HTMLHtmlElement; - querySelector(selectors: "i"): HTMLElement; - querySelector(selectors: "iframe"): HTMLIFrameElement; - querySelector(selectors: "image"): SVGImageElement; - querySelector(selectors: "img"): HTMLImageElement; - querySelector(selectors: "input"): HTMLInputElement; - querySelector(selectors: "ins"): HTMLModElement; - querySelector(selectors: "isindex"): HTMLUnknownElement; - querySelector(selectors: "kbd"): HTMLElement; - querySelector(selectors: "keygen"): HTMLElement; - querySelector(selectors: "label"): HTMLLabelElement; - querySelector(selectors: "legend"): HTMLLegendElement; - querySelector(selectors: "li"): HTMLLIElement; - querySelector(selectors: "line"): SVGLineElement; - querySelector(selectors: "lineargradient"): SVGLinearGradientElement; - querySelector(selectors: "link"): HTMLLinkElement; - querySelector(selectors: "listing"): HTMLPreElement; - querySelector(selectors: "map"): HTMLMapElement; - querySelector(selectors: "mark"): HTMLElement; - querySelector(selectors: "marker"): SVGMarkerElement; - querySelector(selectors: "marquee"): HTMLMarqueeElement; - querySelector(selectors: "mask"): SVGMaskElement; - querySelector(selectors: "menu"): HTMLMenuElement; - querySelector(selectors: "meta"): HTMLMetaElement; - querySelector(selectors: "metadata"): SVGMetadataElement; - querySelector(selectors: "meter"): HTMLMeterElement; - querySelector(selectors: "nav"): HTMLElement; - querySelector(selectors: "nextid"): HTMLUnknownElement; - querySelector(selectors: "nobr"): HTMLElement; - querySelector(selectors: "noframes"): HTMLElement; - querySelector(selectors: "noscript"): HTMLElement; - querySelector(selectors: "object"): HTMLObjectElement; - querySelector(selectors: "ol"): HTMLOListElement; - querySelector(selectors: "optgroup"): HTMLOptGroupElement; - querySelector(selectors: "option"): HTMLOptionElement; - querySelector(selectors: "p"): HTMLParagraphElement; - querySelector(selectors: "param"): HTMLParamElement; - querySelector(selectors: "path"): SVGPathElement; - querySelector(selectors: "pattern"): SVGPatternElement; - querySelector(selectors: "picture"): HTMLPictureElement; - querySelector(selectors: "plaintext"): HTMLElement; - querySelector(selectors: "polygon"): SVGPolygonElement; - querySelector(selectors: "polyline"): SVGPolylineElement; - querySelector(selectors: "pre"): HTMLPreElement; - querySelector(selectors: "progress"): HTMLProgressElement; - querySelector(selectors: "q"): HTMLQuoteElement; - querySelector(selectors: "radialgradient"): SVGRadialGradientElement; - querySelector(selectors: "rect"): SVGRectElement; - querySelector(selectors: "rt"): HTMLElement; - querySelector(selectors: "ruby"): HTMLElement; - querySelector(selectors: "s"): HTMLElement; - querySelector(selectors: "samp"): HTMLElement; - querySelector(selectors: "script"): HTMLScriptElement; - querySelector(selectors: "section"): HTMLElement; - querySelector(selectors: "select"): HTMLSelectElement; - querySelector(selectors: "small"): HTMLElement; - querySelector(selectors: "source"): HTMLSourceElement; - querySelector(selectors: "span"): HTMLSpanElement; - querySelector(selectors: "stop"): SVGStopElement; - querySelector(selectors: "strike"): HTMLElement; - querySelector(selectors: "strong"): HTMLElement; - querySelector(selectors: "style"): HTMLStyleElement; - querySelector(selectors: "sub"): HTMLElement; - querySelector(selectors: "sup"): HTMLElement; - querySelector(selectors: "svg"): SVGSVGElement; - querySelector(selectors: "switch"): SVGSwitchElement; - querySelector(selectors: "symbol"): SVGSymbolElement; - querySelector(selectors: "table"): HTMLTableElement; - querySelector(selectors: "tbody"): HTMLTableSectionElement; - querySelector(selectors: "td"): HTMLTableDataCellElement; - querySelector(selectors: "template"): HTMLTemplateElement; - querySelector(selectors: "text"): SVGTextElement; - querySelector(selectors: "textpath"): SVGTextPathElement; - querySelector(selectors: "textarea"): HTMLTextAreaElement; - querySelector(selectors: "tfoot"): HTMLTableSectionElement; - querySelector(selectors: "th"): HTMLTableHeaderCellElement; - querySelector(selectors: "thead"): HTMLTableSectionElement; - querySelector(selectors: "title"): HTMLTitleElement; - querySelector(selectors: "tr"): HTMLTableRowElement; - querySelector(selectors: "track"): HTMLTrackElement; - querySelector(selectors: "tspan"): SVGTSpanElement; - querySelector(selectors: "tt"): HTMLElement; - querySelector(selectors: "u"): HTMLElement; - querySelector(selectors: "ul"): HTMLUListElement; - querySelector(selectors: "use"): SVGUseElement; - querySelector(selectors: "var"): HTMLElement; - querySelector(selectors: "video"): HTMLVideoElement; - querySelector(selectors: "view"): SVGViewElement; - querySelector(selectors: "wbr"): HTMLElement; - querySelector(selectors: "x-ms-webview"): MSHTMLWebViewElement; - querySelector(selectors: "xmp"): HTMLPreElement; - querySelector(selectors: string): Element; + querySelector(selectors: "a"): HTMLAnchorElement | null; + querySelector(selectors: "abbr"): HTMLElement | null; + querySelector(selectors: "acronym"): HTMLElement | null; + querySelector(selectors: "address"): HTMLElement | null; + querySelector(selectors: "applet"): HTMLAppletElement | null; + querySelector(selectors: "area"): HTMLAreaElement | null; + querySelector(selectors: "article"): HTMLElement | null; + querySelector(selectors: "aside"): HTMLElement | null; + querySelector(selectors: "audio"): HTMLAudioElement | null; + querySelector(selectors: "b"): HTMLElement | null; + querySelector(selectors: "base"): HTMLBaseElement | null; + querySelector(selectors: "basefont"): HTMLBaseFontElement | null; + querySelector(selectors: "bdo"): HTMLElement | null; + querySelector(selectors: "big"): HTMLElement | null; + querySelector(selectors: "blockquote"): HTMLQuoteElement | null; + querySelector(selectors: "body"): HTMLBodyElement | null; + querySelector(selectors: "br"): HTMLBRElement | null; + querySelector(selectors: "button"): HTMLButtonElement | null; + querySelector(selectors: "canvas"): HTMLCanvasElement | null; + querySelector(selectors: "caption"): HTMLTableCaptionElement | null; + querySelector(selectors: "center"): HTMLElement | null; + querySelector(selectors: "circle"): SVGCircleElement | null; + querySelector(selectors: "cite"): HTMLElement | null; + querySelector(selectors: "clippath"): SVGClipPathElement | null; + querySelector(selectors: "code"): HTMLElement | null; + querySelector(selectors: "col"): HTMLTableColElement | null; + querySelector(selectors: "colgroup"): HTMLTableColElement | null; + querySelector(selectors: "datalist"): HTMLDataListElement | null; + querySelector(selectors: "dd"): HTMLElement | null; + querySelector(selectors: "defs"): SVGDefsElement | null; + querySelector(selectors: "del"): HTMLModElement | null; + querySelector(selectors: "desc"): SVGDescElement | null; + querySelector(selectors: "dfn"): HTMLElement | null; + querySelector(selectors: "dir"): HTMLDirectoryElement | null; + querySelector(selectors: "div"): HTMLDivElement | null; + querySelector(selectors: "dl"): HTMLDListElement | null; + querySelector(selectors: "dt"): HTMLElement | null; + querySelector(selectors: "ellipse"): SVGEllipseElement | null; + querySelector(selectors: "em"): HTMLElement | null; + querySelector(selectors: "embed"): HTMLEmbedElement | null; + querySelector(selectors: "feblend"): SVGFEBlendElement | null; + querySelector(selectors: "fecolormatrix"): SVGFEColorMatrixElement | null; + querySelector(selectors: "fecomponenttransfer"): SVGFEComponentTransferElement | null; + querySelector(selectors: "fecomposite"): SVGFECompositeElement | null; + querySelector(selectors: "feconvolvematrix"): SVGFEConvolveMatrixElement | null; + querySelector(selectors: "fediffuselighting"): SVGFEDiffuseLightingElement | null; + querySelector(selectors: "fedisplacementmap"): SVGFEDisplacementMapElement | null; + querySelector(selectors: "fedistantlight"): SVGFEDistantLightElement | null; + querySelector(selectors: "feflood"): SVGFEFloodElement | null; + querySelector(selectors: "fefunca"): SVGFEFuncAElement | null; + querySelector(selectors: "fefuncb"): SVGFEFuncBElement | null; + querySelector(selectors: "fefuncg"): SVGFEFuncGElement | null; + querySelector(selectors: "fefuncr"): SVGFEFuncRElement | null; + querySelector(selectors: "fegaussianblur"): SVGFEGaussianBlurElement | null; + querySelector(selectors: "feimage"): SVGFEImageElement | null; + querySelector(selectors: "femerge"): SVGFEMergeElement | null; + querySelector(selectors: "femergenode"): SVGFEMergeNodeElement | null; + querySelector(selectors: "femorphology"): SVGFEMorphologyElement | null; + querySelector(selectors: "feoffset"): SVGFEOffsetElement | null; + querySelector(selectors: "fepointlight"): SVGFEPointLightElement | null; + querySelector(selectors: "fespecularlighting"): SVGFESpecularLightingElement | null; + querySelector(selectors: "fespotlight"): SVGFESpotLightElement | null; + querySelector(selectors: "fetile"): SVGFETileElement | null; + querySelector(selectors: "feturbulence"): SVGFETurbulenceElement | null; + querySelector(selectors: "fieldset"): HTMLFieldSetElement | null; + querySelector(selectors: "figcaption"): HTMLElement | null; + querySelector(selectors: "figure"): HTMLElement | null; + querySelector(selectors: "filter"): SVGFilterElement | null; + querySelector(selectors: "font"): HTMLFontElement | null; + querySelector(selectors: "footer"): HTMLElement | null; + querySelector(selectors: "foreignobject"): SVGForeignObjectElement | null; + querySelector(selectors: "form"): HTMLFormElement | null; + querySelector(selectors: "frame"): HTMLFrameElement | null; + querySelector(selectors: "frameset"): HTMLFrameSetElement | null; + querySelector(selectors: "g"): SVGGElement | null; + querySelector(selectors: "h1"): HTMLHeadingElement | null; + querySelector(selectors: "h2"): HTMLHeadingElement | null; + querySelector(selectors: "h3"): HTMLHeadingElement | null; + querySelector(selectors: "h4"): HTMLHeadingElement | null; + querySelector(selectors: "h5"): HTMLHeadingElement | null; + querySelector(selectors: "h6"): HTMLHeadingElement | null; + querySelector(selectors: "head"): HTMLHeadElement | null; + querySelector(selectors: "header"): HTMLElement | null; + querySelector(selectors: "hgroup"): HTMLElement | null; + querySelector(selectors: "hr"): HTMLHRElement | null; + querySelector(selectors: "html"): HTMLHtmlElement | null; + querySelector(selectors: "i"): HTMLElement | null; + querySelector(selectors: "iframe"): HTMLIFrameElement | null; + querySelector(selectors: "image"): SVGImageElement | null; + querySelector(selectors: "img"): HTMLImageElement | null; + querySelector(selectors: "input"): HTMLInputElement | null; + querySelector(selectors: "ins"): HTMLModElement | null; + querySelector(selectors: "isindex"): HTMLUnknownElement | null; + querySelector(selectors: "kbd"): HTMLElement | null; + querySelector(selectors: "keygen"): HTMLElement | null; + querySelector(selectors: "label"): HTMLLabelElement | null; + querySelector(selectors: "legend"): HTMLLegendElement | null; + querySelector(selectors: "li"): HTMLLIElement | null; + querySelector(selectors: "line"): SVGLineElement | null; + querySelector(selectors: "lineargradient"): SVGLinearGradientElement | null; + querySelector(selectors: "link"): HTMLLinkElement | null; + querySelector(selectors: "listing"): HTMLPreElement | null; + querySelector(selectors: "map"): HTMLMapElement | null; + querySelector(selectors: "mark"): HTMLElement | null; + querySelector(selectors: "marker"): SVGMarkerElement | null; + querySelector(selectors: "marquee"): HTMLMarqueeElement | null; + querySelector(selectors: "mask"): SVGMaskElement | null; + querySelector(selectors: "menu"): HTMLMenuElement | null; + querySelector(selectors: "meta"): HTMLMetaElement | null; + querySelector(selectors: "metadata"): SVGMetadataElement | null; + querySelector(selectors: "meter"): HTMLMeterElement | null; + querySelector(selectors: "nav"): HTMLElement | null; + querySelector(selectors: "nextid"): HTMLUnknownElement | null; + querySelector(selectors: "nobr"): HTMLElement | null; + querySelector(selectors: "noframes"): HTMLElement | null; + querySelector(selectors: "noscript"): HTMLElement | null; + querySelector(selectors: "object"): HTMLObjectElement | null; + querySelector(selectors: "ol"): HTMLOListElement | null; + querySelector(selectors: "optgroup"): HTMLOptGroupElement | null; + querySelector(selectors: "option"): HTMLOptionElement | null; + querySelector(selectors: "p"): HTMLParagraphElement | null; + querySelector(selectors: "param"): HTMLParamElement | null; + querySelector(selectors: "path"): SVGPathElement | null; + querySelector(selectors: "pattern"): SVGPatternElement | null; + querySelector(selectors: "picture"): HTMLPictureElement | null; + querySelector(selectors: "plaintext"): HTMLElement | null; + querySelector(selectors: "polygon"): SVGPolygonElement | null; + querySelector(selectors: "polyline"): SVGPolylineElement | null; + querySelector(selectors: "pre"): HTMLPreElement | null; + querySelector(selectors: "progress"): HTMLProgressElement | null; + querySelector(selectors: "q"): HTMLQuoteElement | null; + querySelector(selectors: "radialgradient"): SVGRadialGradientElement | null; + querySelector(selectors: "rect"): SVGRectElement | null; + querySelector(selectors: "rt"): HTMLElement | null; + querySelector(selectors: "ruby"): HTMLElement | null; + querySelector(selectors: "s"): HTMLElement | null; + querySelector(selectors: "samp"): HTMLElement | null; + querySelector(selectors: "script"): HTMLScriptElement | null; + querySelector(selectors: "section"): HTMLElement | null; + querySelector(selectors: "select"): HTMLSelectElement | null; + querySelector(selectors: "small"): HTMLElement | null; + querySelector(selectors: "source"): HTMLSourceElement | null; + querySelector(selectors: "span"): HTMLSpanElement | null; + querySelector(selectors: "stop"): SVGStopElement | null; + querySelector(selectors: "strike"): HTMLElement | null; + querySelector(selectors: "strong"): HTMLElement | null; + querySelector(selectors: "style"): HTMLStyleElement | null; + querySelector(selectors: "sub"): HTMLElement | null; + querySelector(selectors: "sup"): HTMLElement | null; + querySelector(selectors: "svg"): SVGSVGElement | null; + querySelector(selectors: "switch"): SVGSwitchElement | null; + querySelector(selectors: "symbol"): SVGSymbolElement | null; + querySelector(selectors: "table"): HTMLTableElement | null; + querySelector(selectors: "tbody"): HTMLTableSectionElement | null; + querySelector(selectors: "td"): HTMLTableDataCellElement | null; + querySelector(selectors: "template"): HTMLTemplateElement | null; + querySelector(selectors: "text"): SVGTextElement | null; + querySelector(selectors: "textpath"): SVGTextPathElement | null; + querySelector(selectors: "textarea"): HTMLTextAreaElement | null; + querySelector(selectors: "tfoot"): HTMLTableSectionElement | null; + querySelector(selectors: "th"): HTMLTableHeaderCellElement | null; + querySelector(selectors: "thead"): HTMLTableSectionElement | null; + querySelector(selectors: "title"): HTMLTitleElement | null; + querySelector(selectors: "tr"): HTMLTableRowElement | null; + querySelector(selectors: "track"): HTMLTrackElement | null; + querySelector(selectors: "tspan"): SVGTSpanElement | null; + querySelector(selectors: "tt"): HTMLElement | null; + querySelector(selectors: "u"): HTMLElement | null; + querySelector(selectors: "ul"): HTMLUListElement | null; + querySelector(selectors: "use"): SVGUseElement | null; + querySelector(selectors: "var"): HTMLElement | null; + querySelector(selectors: "video"): HTMLVideoElement | null; + querySelector(selectors: "view"): SVGViewElement | null; + querySelector(selectors: "wbr"): HTMLElement | null; + querySelector(selectors: "x-ms-webview"): MSHTMLWebViewElement | null; + querySelector(selectors: "xmp"): HTMLPreElement | null; + querySelector(selectors: string): Element | null; querySelectorAll(selectors: "a"): NodeListOf; querySelectorAll(selectors: "abbr"): NodeListOf; querySelectorAll(selectors: "acronym"): NodeListOf; @@ -14638,4 +14638,5 @@ type ScrollBehavior = "auto" | "instant" | "smooth"; type ScrollLogicalPosition = "start" | "center" | "end" | "nearest"; type IDBValidKey = number | string | Date | IDBArrayKey; type BufferSource = ArrayBuffer | ArrayBufferView; -type MouseWheelEvent = WheelEvent; \ No newline at end of file +type MouseWheelEvent = WheelEvent; +type ScrollRestoration = "auto" | "manual"; \ No newline at end of file diff --git a/lib/lib.es2015.core.d.ts b/lib/lib.es2015.core.d.ts index 49a81a220ce62..669c80944ecfb 100644 --- a/lib/lib.es2015.core.d.ts +++ b/lib/lib.es2015.core.d.ts @@ -217,7 +217,7 @@ interface NumberConstructor { /** * Returns true if passed value is finite. - * Unlike the global isFininte, Number.isFinite doesn't forcibly convert the parameter to a + * Unlike the global isFinite, Number.isFinite doesn't forcibly convert the parameter to a * number. Only finite values of the type number, result in true. * @param number A numeric value. */ diff --git a/lib/lib.es5.d.ts b/lib/lib.es5.d.ts index 5df0d7d406854..a13ffc92d0407 100644 --- a/lib/lib.es5.d.ts +++ b/lib/lib.es5.d.ts @@ -260,6 +260,9 @@ interface Function { */ bind(this: Function, thisArg: any, ...argArray: any[]): any; + /** Returns a string representation of a function. */ + toString(): string; + prototype: any; readonly length: number; @@ -1216,6 +1219,30 @@ interface Array { * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. */ forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void; + /** + * Calls a defined callback function on each element of an array, and returns an array that contains the results. + * @param callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array. + * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. + */ + map(this: [T, T, T, T, T], callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): [U, U, U, U, U]; + /** + * Calls a defined callback function on each element of an array, and returns an array that contains the results. + * @param callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array. + * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. + */ + map(this: [T, T, T, T], callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): [U, U, U, U]; + /** + * Calls a defined callback function on each element of an array, and returns an array that contains the results. + * @param callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array. + * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. + */ + map(this: [T, T, T], callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): [U, U, U]; + /** + * Calls a defined callback function on each element of an array, and returns an array that contains the results. + * @param callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array. + * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. + */ + map(this: [T, T], callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): [U, U]; /** * Calls a defined callback function on each element of an array, and returns an array that contains the results. * @param callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array. diff --git a/lib/lib.es6.d.ts b/lib/lib.es6.d.ts index f6c7766ea9fd0..4c8806dd7dd48 100644 --- a/lib/lib.es6.d.ts +++ b/lib/lib.es6.d.ts @@ -260,6 +260,9 @@ interface Function { */ bind(this: Function, thisArg: any, ...argArray: any[]): any; + /** Returns a string representation of a function. */ + toString(): string; + prototype: any; readonly length: number; @@ -1216,6 +1219,30 @@ interface Array { * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. */ forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void; + /** + * Calls a defined callback function on each element of an array, and returns an array that contains the results. + * @param callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array. + * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. + */ + map(this: [T, T, T, T, T], callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): [U, U, U, U, U]; + /** + * Calls a defined callback function on each element of an array, and returns an array that contains the results. + * @param callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array. + * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. + */ + map(this: [T, T, T, T], callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): [U, U, U, U]; + /** + * Calls a defined callback function on each element of an array, and returns an array that contains the results. + * @param callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array. + * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. + */ + map(this: [T, T, T], callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): [U, U, U]; + /** + * Calls a defined callback function on each element of an array, and returns an array that contains the results. + * @param callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array. + * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. + */ + map(this: [T, T], callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): [U, U]; /** * Calls a defined callback function on each element of an array, and returns an array that contains the results. * @param callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array. @@ -4325,7 +4352,7 @@ interface NumberConstructor { /** * Returns true if passed value is finite. - * Unlike the global isFininte, Number.isFinite doesn't forcibly convert the parameter to a + * Unlike the global isFinite, Number.isFinite doesn't forcibly convert the parameter to a * number. Only finite values of the type number, result in true. * @param number A numeric value. */ @@ -13361,11 +13388,12 @@ declare var HashChangeEvent: { interface History { readonly length: number; readonly state: any; - back(distance?: any): void; - forward(distance?: any): void; - go(delta?: any): void; - pushState(statedata: any, title?: string, url?: string): void; - replaceState(statedata: any, title?: string, url?: string): void; + scrollRestoration: ScrollRestoration; + back(): void; + forward(): void; + go(delta?: number): void; + pushState(data: any, title: string, url?: string | null): void; + replaceState(data: any, title: string, url?: string | null): void; } declare var History: { @@ -18937,7 +18965,6 @@ interface Window extends EventTarget, WindowTimers, WindowSessionStorage, Window addEventListener(type: "waiting", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; addEventListener(type: "wheel", listener: (this: this, ev: WheelEvent) => any, useCapture?: boolean): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void; - [index: number]: Window; } declare var Window: { @@ -18968,7 +18995,7 @@ declare var XMLDocument: { } interface XMLHttpRequest extends EventTarget, XMLHttpRequestEventTarget { - onreadystatechange: (this: this, ev: ProgressEvent) => any; + onreadystatechange: (this: this, ev: Event) => any; readonly readyState: number; readonly response: any; readonly responseText: string; @@ -18995,13 +19022,13 @@ interface XMLHttpRequest extends EventTarget, XMLHttpRequestEventTarget { readonly LOADING: number; readonly OPENED: number; readonly UNSENT: number; - addEventListener(type: "abort", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; - addEventListener(type: "error", listener: (this: this, ev: ErrorEvent) => any, useCapture?: boolean): void; - addEventListener(type: "load", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; + addEventListener(type: "abort", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; + addEventListener(type: "error", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; + addEventListener(type: "load", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; addEventListener(type: "loadend", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; - addEventListener(type: "loadstart", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; + addEventListener(type: "loadstart", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; addEventListener(type: "progress", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; - addEventListener(type: "readystatechange", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; + addEventListener(type: "readystatechange", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; addEventListener(type: "timeout", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void; } @@ -19342,183 +19369,183 @@ interface NavigatorUserMedia { } interface NodeSelector { - querySelector(selectors: "a"): HTMLAnchorElement; - querySelector(selectors: "abbr"): HTMLElement; - querySelector(selectors: "acronym"): HTMLElement; - querySelector(selectors: "address"): HTMLElement; - querySelector(selectors: "applet"): HTMLAppletElement; - querySelector(selectors: "area"): HTMLAreaElement; - querySelector(selectors: "article"): HTMLElement; - querySelector(selectors: "aside"): HTMLElement; - querySelector(selectors: "audio"): HTMLAudioElement; - querySelector(selectors: "b"): HTMLElement; - querySelector(selectors: "base"): HTMLBaseElement; - querySelector(selectors: "basefont"): HTMLBaseFontElement; - querySelector(selectors: "bdo"): HTMLElement; - querySelector(selectors: "big"): HTMLElement; - querySelector(selectors: "blockquote"): HTMLQuoteElement; - querySelector(selectors: "body"): HTMLBodyElement; - querySelector(selectors: "br"): HTMLBRElement; - querySelector(selectors: "button"): HTMLButtonElement; - querySelector(selectors: "canvas"): HTMLCanvasElement; - querySelector(selectors: "caption"): HTMLTableCaptionElement; - querySelector(selectors: "center"): HTMLElement; - querySelector(selectors: "circle"): SVGCircleElement; - querySelector(selectors: "cite"): HTMLElement; - querySelector(selectors: "clippath"): SVGClipPathElement; - querySelector(selectors: "code"): HTMLElement; - querySelector(selectors: "col"): HTMLTableColElement; - querySelector(selectors: "colgroup"): HTMLTableColElement; - querySelector(selectors: "datalist"): HTMLDataListElement; - querySelector(selectors: "dd"): HTMLElement; - querySelector(selectors: "defs"): SVGDefsElement; - querySelector(selectors: "del"): HTMLModElement; - querySelector(selectors: "desc"): SVGDescElement; - querySelector(selectors: "dfn"): HTMLElement; - querySelector(selectors: "dir"): HTMLDirectoryElement; - querySelector(selectors: "div"): HTMLDivElement; - querySelector(selectors: "dl"): HTMLDListElement; - querySelector(selectors: "dt"): HTMLElement; - querySelector(selectors: "ellipse"): SVGEllipseElement; - querySelector(selectors: "em"): HTMLElement; - querySelector(selectors: "embed"): HTMLEmbedElement; - querySelector(selectors: "feblend"): SVGFEBlendElement; - querySelector(selectors: "fecolormatrix"): SVGFEColorMatrixElement; - querySelector(selectors: "fecomponenttransfer"): SVGFEComponentTransferElement; - querySelector(selectors: "fecomposite"): SVGFECompositeElement; - querySelector(selectors: "feconvolvematrix"): SVGFEConvolveMatrixElement; - querySelector(selectors: "fediffuselighting"): SVGFEDiffuseLightingElement; - querySelector(selectors: "fedisplacementmap"): SVGFEDisplacementMapElement; - querySelector(selectors: "fedistantlight"): SVGFEDistantLightElement; - querySelector(selectors: "feflood"): SVGFEFloodElement; - querySelector(selectors: "fefunca"): SVGFEFuncAElement; - querySelector(selectors: "fefuncb"): SVGFEFuncBElement; - querySelector(selectors: "fefuncg"): SVGFEFuncGElement; - querySelector(selectors: "fefuncr"): SVGFEFuncRElement; - querySelector(selectors: "fegaussianblur"): SVGFEGaussianBlurElement; - querySelector(selectors: "feimage"): SVGFEImageElement; - querySelector(selectors: "femerge"): SVGFEMergeElement; - querySelector(selectors: "femergenode"): SVGFEMergeNodeElement; - querySelector(selectors: "femorphology"): SVGFEMorphologyElement; - querySelector(selectors: "feoffset"): SVGFEOffsetElement; - querySelector(selectors: "fepointlight"): SVGFEPointLightElement; - querySelector(selectors: "fespecularlighting"): SVGFESpecularLightingElement; - querySelector(selectors: "fespotlight"): SVGFESpotLightElement; - querySelector(selectors: "fetile"): SVGFETileElement; - querySelector(selectors: "feturbulence"): SVGFETurbulenceElement; - querySelector(selectors: "fieldset"): HTMLFieldSetElement; - querySelector(selectors: "figcaption"): HTMLElement; - querySelector(selectors: "figure"): HTMLElement; - querySelector(selectors: "filter"): SVGFilterElement; - querySelector(selectors: "font"): HTMLFontElement; - querySelector(selectors: "footer"): HTMLElement; - querySelector(selectors: "foreignobject"): SVGForeignObjectElement; - querySelector(selectors: "form"): HTMLFormElement; - querySelector(selectors: "frame"): HTMLFrameElement; - querySelector(selectors: "frameset"): HTMLFrameSetElement; - querySelector(selectors: "g"): SVGGElement; - querySelector(selectors: "h1"): HTMLHeadingElement; - querySelector(selectors: "h2"): HTMLHeadingElement; - querySelector(selectors: "h3"): HTMLHeadingElement; - querySelector(selectors: "h4"): HTMLHeadingElement; - querySelector(selectors: "h5"): HTMLHeadingElement; - querySelector(selectors: "h6"): HTMLHeadingElement; - querySelector(selectors: "head"): HTMLHeadElement; - querySelector(selectors: "header"): HTMLElement; - querySelector(selectors: "hgroup"): HTMLElement; - querySelector(selectors: "hr"): HTMLHRElement; - querySelector(selectors: "html"): HTMLHtmlElement; - querySelector(selectors: "i"): HTMLElement; - querySelector(selectors: "iframe"): HTMLIFrameElement; - querySelector(selectors: "image"): SVGImageElement; - querySelector(selectors: "img"): HTMLImageElement; - querySelector(selectors: "input"): HTMLInputElement; - querySelector(selectors: "ins"): HTMLModElement; - querySelector(selectors: "isindex"): HTMLUnknownElement; - querySelector(selectors: "kbd"): HTMLElement; - querySelector(selectors: "keygen"): HTMLElement; - querySelector(selectors: "label"): HTMLLabelElement; - querySelector(selectors: "legend"): HTMLLegendElement; - querySelector(selectors: "li"): HTMLLIElement; - querySelector(selectors: "line"): SVGLineElement; - querySelector(selectors: "lineargradient"): SVGLinearGradientElement; - querySelector(selectors: "link"): HTMLLinkElement; - querySelector(selectors: "listing"): HTMLPreElement; - querySelector(selectors: "map"): HTMLMapElement; - querySelector(selectors: "mark"): HTMLElement; - querySelector(selectors: "marker"): SVGMarkerElement; - querySelector(selectors: "marquee"): HTMLMarqueeElement; - querySelector(selectors: "mask"): SVGMaskElement; - querySelector(selectors: "menu"): HTMLMenuElement; - querySelector(selectors: "meta"): HTMLMetaElement; - querySelector(selectors: "metadata"): SVGMetadataElement; - querySelector(selectors: "meter"): HTMLMeterElement; - querySelector(selectors: "nav"): HTMLElement; - querySelector(selectors: "nextid"): HTMLUnknownElement; - querySelector(selectors: "nobr"): HTMLElement; - querySelector(selectors: "noframes"): HTMLElement; - querySelector(selectors: "noscript"): HTMLElement; - querySelector(selectors: "object"): HTMLObjectElement; - querySelector(selectors: "ol"): HTMLOListElement; - querySelector(selectors: "optgroup"): HTMLOptGroupElement; - querySelector(selectors: "option"): HTMLOptionElement; - querySelector(selectors: "p"): HTMLParagraphElement; - querySelector(selectors: "param"): HTMLParamElement; - querySelector(selectors: "path"): SVGPathElement; - querySelector(selectors: "pattern"): SVGPatternElement; - querySelector(selectors: "picture"): HTMLPictureElement; - querySelector(selectors: "plaintext"): HTMLElement; - querySelector(selectors: "polygon"): SVGPolygonElement; - querySelector(selectors: "polyline"): SVGPolylineElement; - querySelector(selectors: "pre"): HTMLPreElement; - querySelector(selectors: "progress"): HTMLProgressElement; - querySelector(selectors: "q"): HTMLQuoteElement; - querySelector(selectors: "radialgradient"): SVGRadialGradientElement; - querySelector(selectors: "rect"): SVGRectElement; - querySelector(selectors: "rt"): HTMLElement; - querySelector(selectors: "ruby"): HTMLElement; - querySelector(selectors: "s"): HTMLElement; - querySelector(selectors: "samp"): HTMLElement; - querySelector(selectors: "script"): HTMLScriptElement; - querySelector(selectors: "section"): HTMLElement; - querySelector(selectors: "select"): HTMLSelectElement; - querySelector(selectors: "small"): HTMLElement; - querySelector(selectors: "source"): HTMLSourceElement; - querySelector(selectors: "span"): HTMLSpanElement; - querySelector(selectors: "stop"): SVGStopElement; - querySelector(selectors: "strike"): HTMLElement; - querySelector(selectors: "strong"): HTMLElement; - querySelector(selectors: "style"): HTMLStyleElement; - querySelector(selectors: "sub"): HTMLElement; - querySelector(selectors: "sup"): HTMLElement; - querySelector(selectors: "svg"): SVGSVGElement; - querySelector(selectors: "switch"): SVGSwitchElement; - querySelector(selectors: "symbol"): SVGSymbolElement; - querySelector(selectors: "table"): HTMLTableElement; - querySelector(selectors: "tbody"): HTMLTableSectionElement; - querySelector(selectors: "td"): HTMLTableDataCellElement; - querySelector(selectors: "template"): HTMLTemplateElement; - querySelector(selectors: "text"): SVGTextElement; - querySelector(selectors: "textpath"): SVGTextPathElement; - querySelector(selectors: "textarea"): HTMLTextAreaElement; - querySelector(selectors: "tfoot"): HTMLTableSectionElement; - querySelector(selectors: "th"): HTMLTableHeaderCellElement; - querySelector(selectors: "thead"): HTMLTableSectionElement; - querySelector(selectors: "title"): HTMLTitleElement; - querySelector(selectors: "tr"): HTMLTableRowElement; - querySelector(selectors: "track"): HTMLTrackElement; - querySelector(selectors: "tspan"): SVGTSpanElement; - querySelector(selectors: "tt"): HTMLElement; - querySelector(selectors: "u"): HTMLElement; - querySelector(selectors: "ul"): HTMLUListElement; - querySelector(selectors: "use"): SVGUseElement; - querySelector(selectors: "var"): HTMLElement; - querySelector(selectors: "video"): HTMLVideoElement; - querySelector(selectors: "view"): SVGViewElement; - querySelector(selectors: "wbr"): HTMLElement; - querySelector(selectors: "x-ms-webview"): MSHTMLWebViewElement; - querySelector(selectors: "xmp"): HTMLPreElement; - querySelector(selectors: string): Element; + querySelector(selectors: "a"): HTMLAnchorElement | null; + querySelector(selectors: "abbr"): HTMLElement | null; + querySelector(selectors: "acronym"): HTMLElement | null; + querySelector(selectors: "address"): HTMLElement | null; + querySelector(selectors: "applet"): HTMLAppletElement | null; + querySelector(selectors: "area"): HTMLAreaElement | null; + querySelector(selectors: "article"): HTMLElement | null; + querySelector(selectors: "aside"): HTMLElement | null; + querySelector(selectors: "audio"): HTMLAudioElement | null; + querySelector(selectors: "b"): HTMLElement | null; + querySelector(selectors: "base"): HTMLBaseElement | null; + querySelector(selectors: "basefont"): HTMLBaseFontElement | null; + querySelector(selectors: "bdo"): HTMLElement | null; + querySelector(selectors: "big"): HTMLElement | null; + querySelector(selectors: "blockquote"): HTMLQuoteElement | null; + querySelector(selectors: "body"): HTMLBodyElement | null; + querySelector(selectors: "br"): HTMLBRElement | null; + querySelector(selectors: "button"): HTMLButtonElement | null; + querySelector(selectors: "canvas"): HTMLCanvasElement | null; + querySelector(selectors: "caption"): HTMLTableCaptionElement | null; + querySelector(selectors: "center"): HTMLElement | null; + querySelector(selectors: "circle"): SVGCircleElement | null; + querySelector(selectors: "cite"): HTMLElement | null; + querySelector(selectors: "clippath"): SVGClipPathElement | null; + querySelector(selectors: "code"): HTMLElement | null; + querySelector(selectors: "col"): HTMLTableColElement | null; + querySelector(selectors: "colgroup"): HTMLTableColElement | null; + querySelector(selectors: "datalist"): HTMLDataListElement | null; + querySelector(selectors: "dd"): HTMLElement | null; + querySelector(selectors: "defs"): SVGDefsElement | null; + querySelector(selectors: "del"): HTMLModElement | null; + querySelector(selectors: "desc"): SVGDescElement | null; + querySelector(selectors: "dfn"): HTMLElement | null; + querySelector(selectors: "dir"): HTMLDirectoryElement | null; + querySelector(selectors: "div"): HTMLDivElement | null; + querySelector(selectors: "dl"): HTMLDListElement | null; + querySelector(selectors: "dt"): HTMLElement | null; + querySelector(selectors: "ellipse"): SVGEllipseElement | null; + querySelector(selectors: "em"): HTMLElement | null; + querySelector(selectors: "embed"): HTMLEmbedElement | null; + querySelector(selectors: "feblend"): SVGFEBlendElement | null; + querySelector(selectors: "fecolormatrix"): SVGFEColorMatrixElement | null; + querySelector(selectors: "fecomponenttransfer"): SVGFEComponentTransferElement | null; + querySelector(selectors: "fecomposite"): SVGFECompositeElement | null; + querySelector(selectors: "feconvolvematrix"): SVGFEConvolveMatrixElement | null; + querySelector(selectors: "fediffuselighting"): SVGFEDiffuseLightingElement | null; + querySelector(selectors: "fedisplacementmap"): SVGFEDisplacementMapElement | null; + querySelector(selectors: "fedistantlight"): SVGFEDistantLightElement | null; + querySelector(selectors: "feflood"): SVGFEFloodElement | null; + querySelector(selectors: "fefunca"): SVGFEFuncAElement | null; + querySelector(selectors: "fefuncb"): SVGFEFuncBElement | null; + querySelector(selectors: "fefuncg"): SVGFEFuncGElement | null; + querySelector(selectors: "fefuncr"): SVGFEFuncRElement | null; + querySelector(selectors: "fegaussianblur"): SVGFEGaussianBlurElement | null; + querySelector(selectors: "feimage"): SVGFEImageElement | null; + querySelector(selectors: "femerge"): SVGFEMergeElement | null; + querySelector(selectors: "femergenode"): SVGFEMergeNodeElement | null; + querySelector(selectors: "femorphology"): SVGFEMorphologyElement | null; + querySelector(selectors: "feoffset"): SVGFEOffsetElement | null; + querySelector(selectors: "fepointlight"): SVGFEPointLightElement | null; + querySelector(selectors: "fespecularlighting"): SVGFESpecularLightingElement | null; + querySelector(selectors: "fespotlight"): SVGFESpotLightElement | null; + querySelector(selectors: "fetile"): SVGFETileElement | null; + querySelector(selectors: "feturbulence"): SVGFETurbulenceElement | null; + querySelector(selectors: "fieldset"): HTMLFieldSetElement | null; + querySelector(selectors: "figcaption"): HTMLElement | null; + querySelector(selectors: "figure"): HTMLElement | null; + querySelector(selectors: "filter"): SVGFilterElement | null; + querySelector(selectors: "font"): HTMLFontElement | null; + querySelector(selectors: "footer"): HTMLElement | null; + querySelector(selectors: "foreignobject"): SVGForeignObjectElement | null; + querySelector(selectors: "form"): HTMLFormElement | null; + querySelector(selectors: "frame"): HTMLFrameElement | null; + querySelector(selectors: "frameset"): HTMLFrameSetElement | null; + querySelector(selectors: "g"): SVGGElement | null; + querySelector(selectors: "h1"): HTMLHeadingElement | null; + querySelector(selectors: "h2"): HTMLHeadingElement | null; + querySelector(selectors: "h3"): HTMLHeadingElement | null; + querySelector(selectors: "h4"): HTMLHeadingElement | null; + querySelector(selectors: "h5"): HTMLHeadingElement | null; + querySelector(selectors: "h6"): HTMLHeadingElement | null; + querySelector(selectors: "head"): HTMLHeadElement | null; + querySelector(selectors: "header"): HTMLElement | null; + querySelector(selectors: "hgroup"): HTMLElement | null; + querySelector(selectors: "hr"): HTMLHRElement | null; + querySelector(selectors: "html"): HTMLHtmlElement | null; + querySelector(selectors: "i"): HTMLElement | null; + querySelector(selectors: "iframe"): HTMLIFrameElement | null; + querySelector(selectors: "image"): SVGImageElement | null; + querySelector(selectors: "img"): HTMLImageElement | null; + querySelector(selectors: "input"): HTMLInputElement | null; + querySelector(selectors: "ins"): HTMLModElement | null; + querySelector(selectors: "isindex"): HTMLUnknownElement | null; + querySelector(selectors: "kbd"): HTMLElement | null; + querySelector(selectors: "keygen"): HTMLElement | null; + querySelector(selectors: "label"): HTMLLabelElement | null; + querySelector(selectors: "legend"): HTMLLegendElement | null; + querySelector(selectors: "li"): HTMLLIElement | null; + querySelector(selectors: "line"): SVGLineElement | null; + querySelector(selectors: "lineargradient"): SVGLinearGradientElement | null; + querySelector(selectors: "link"): HTMLLinkElement | null; + querySelector(selectors: "listing"): HTMLPreElement | null; + querySelector(selectors: "map"): HTMLMapElement | null; + querySelector(selectors: "mark"): HTMLElement | null; + querySelector(selectors: "marker"): SVGMarkerElement | null; + querySelector(selectors: "marquee"): HTMLMarqueeElement | null; + querySelector(selectors: "mask"): SVGMaskElement | null; + querySelector(selectors: "menu"): HTMLMenuElement | null; + querySelector(selectors: "meta"): HTMLMetaElement | null; + querySelector(selectors: "metadata"): SVGMetadataElement | null; + querySelector(selectors: "meter"): HTMLMeterElement | null; + querySelector(selectors: "nav"): HTMLElement | null; + querySelector(selectors: "nextid"): HTMLUnknownElement | null; + querySelector(selectors: "nobr"): HTMLElement | null; + querySelector(selectors: "noframes"): HTMLElement | null; + querySelector(selectors: "noscript"): HTMLElement | null; + querySelector(selectors: "object"): HTMLObjectElement | null; + querySelector(selectors: "ol"): HTMLOListElement | null; + querySelector(selectors: "optgroup"): HTMLOptGroupElement | null; + querySelector(selectors: "option"): HTMLOptionElement | null; + querySelector(selectors: "p"): HTMLParagraphElement | null; + querySelector(selectors: "param"): HTMLParamElement | null; + querySelector(selectors: "path"): SVGPathElement | null; + querySelector(selectors: "pattern"): SVGPatternElement | null; + querySelector(selectors: "picture"): HTMLPictureElement | null; + querySelector(selectors: "plaintext"): HTMLElement | null; + querySelector(selectors: "polygon"): SVGPolygonElement | null; + querySelector(selectors: "polyline"): SVGPolylineElement | null; + querySelector(selectors: "pre"): HTMLPreElement | null; + querySelector(selectors: "progress"): HTMLProgressElement | null; + querySelector(selectors: "q"): HTMLQuoteElement | null; + querySelector(selectors: "radialgradient"): SVGRadialGradientElement | null; + querySelector(selectors: "rect"): SVGRectElement | null; + querySelector(selectors: "rt"): HTMLElement | null; + querySelector(selectors: "ruby"): HTMLElement | null; + querySelector(selectors: "s"): HTMLElement | null; + querySelector(selectors: "samp"): HTMLElement | null; + querySelector(selectors: "script"): HTMLScriptElement | null; + querySelector(selectors: "section"): HTMLElement | null; + querySelector(selectors: "select"): HTMLSelectElement | null; + querySelector(selectors: "small"): HTMLElement | null; + querySelector(selectors: "source"): HTMLSourceElement | null; + querySelector(selectors: "span"): HTMLSpanElement | null; + querySelector(selectors: "stop"): SVGStopElement | null; + querySelector(selectors: "strike"): HTMLElement | null; + querySelector(selectors: "strong"): HTMLElement | null; + querySelector(selectors: "style"): HTMLStyleElement | null; + querySelector(selectors: "sub"): HTMLElement | null; + querySelector(selectors: "sup"): HTMLElement | null; + querySelector(selectors: "svg"): SVGSVGElement | null; + querySelector(selectors: "switch"): SVGSwitchElement | null; + querySelector(selectors: "symbol"): SVGSymbolElement | null; + querySelector(selectors: "table"): HTMLTableElement | null; + querySelector(selectors: "tbody"): HTMLTableSectionElement | null; + querySelector(selectors: "td"): HTMLTableDataCellElement | null; + querySelector(selectors: "template"): HTMLTemplateElement | null; + querySelector(selectors: "text"): SVGTextElement | null; + querySelector(selectors: "textpath"): SVGTextPathElement | null; + querySelector(selectors: "textarea"): HTMLTextAreaElement | null; + querySelector(selectors: "tfoot"): HTMLTableSectionElement | null; + querySelector(selectors: "th"): HTMLTableHeaderCellElement | null; + querySelector(selectors: "thead"): HTMLTableSectionElement | null; + querySelector(selectors: "title"): HTMLTitleElement | null; + querySelector(selectors: "tr"): HTMLTableRowElement | null; + querySelector(selectors: "track"): HTMLTrackElement | null; + querySelector(selectors: "tspan"): SVGTSpanElement | null; + querySelector(selectors: "tt"): HTMLElement | null; + querySelector(selectors: "u"): HTMLElement | null; + querySelector(selectors: "ul"): HTMLUListElement | null; + querySelector(selectors: "use"): SVGUseElement | null; + querySelector(selectors: "var"): HTMLElement | null; + querySelector(selectors: "video"): HTMLVideoElement | null; + querySelector(selectors: "view"): SVGViewElement | null; + querySelector(selectors: "wbr"): HTMLElement | null; + querySelector(selectors: "x-ms-webview"): MSHTMLWebViewElement | null; + querySelector(selectors: "xmp"): HTMLPreElement | null; + querySelector(selectors: string): Element | null; querySelectorAll(selectors: "a"): NodeListOf; querySelectorAll(selectors: "abbr"): NodeListOf; querySelectorAll(selectors: "acronym"): NodeListOf; @@ -20448,6 +20475,7 @@ type ScrollLogicalPosition = "start" | "center" | "end" | "nearest"; type IDBValidKey = number | string | Date | IDBArrayKey; type BufferSource = ArrayBuffer | ArrayBufferView; type MouseWheelEvent = WheelEvent; +type ScrollRestoration = "auto" | "manual"; ///////////////////////////// /// WorkerGlobalScope APIs ///////////////////////////// diff --git a/lib/lib.webworker.d.ts b/lib/lib.webworker.d.ts index 5b35d820dfe3f..6cb14c5b184a9 100644 --- a/lib/lib.webworker.d.ts +++ b/lib/lib.webworker.d.ts @@ -744,7 +744,7 @@ declare var Worker: { } interface XMLHttpRequest extends EventTarget, XMLHttpRequestEventTarget { - onreadystatechange: (this: this, ev: ProgressEvent) => any; + onreadystatechange: (this: this, ev: Event) => any; readonly readyState: number; readonly response: any; readonly responseText: string; @@ -770,13 +770,13 @@ interface XMLHttpRequest extends EventTarget, XMLHttpRequestEventTarget { readonly LOADING: number; readonly OPENED: number; readonly UNSENT: number; - addEventListener(type: "abort", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; - addEventListener(type: "error", listener: (this: this, ev: ErrorEvent) => any, useCapture?: boolean): void; - addEventListener(type: "load", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; + addEventListener(type: "abort", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; + addEventListener(type: "error", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; + addEventListener(type: "load", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; addEventListener(type: "loadend", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; - addEventListener(type: "loadstart", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; + addEventListener(type: "loadstart", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; addEventListener(type: "progress", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; - addEventListener(type: "readystatechange", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; + addEventListener(type: "readystatechange", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; addEventListener(type: "timeout", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void; } diff --git a/lib/protocol.d.ts b/lib/protocol.d.ts new file mode 100644 index 0000000000000..2b57b5de64405 --- /dev/null +++ b/lib/protocol.d.ts @@ -0,0 +1,1848 @@ +/** + * Declaration module describing the TypeScript Server protocol + */ +declare namespace ts.server.protocol { + namespace CommandTypes { + type Brace = "brace"; + type BraceCompletion = "braceCompletion"; + type Change = "change"; + type Close = "close"; + type Completions = "completions"; + type CompletionDetails = "completionEntryDetails"; + type CompileOnSaveAffectedFileList = "compileOnSaveAffectedFileList"; + type CompileOnSaveEmitFile = "compileOnSaveEmitFile"; + type Configure = "configure"; + type Definition = "definition"; + type Implementation = "implementation"; + type Exit = "exit"; + type Format = "format"; + type Formatonkey = "formatonkey"; + type Geterr = "geterr"; + type GeterrForProject = "geterrForProject"; + type SemanticDiagnosticsSync = "semanticDiagnosticsSync"; + type SyntacticDiagnosticsSync = "syntacticDiagnosticsSync"; + type NavBar = "navbar"; + type Navto = "navto"; + type NavTree = "navtree"; + type NavTreeFull = "navtree-full"; + type Occurrences = "occurrences"; + type DocumentHighlights = "documentHighlights"; + type Open = "open"; + type Quickinfo = "quickinfo"; + type References = "references"; + type Reload = "reload"; + type Rename = "rename"; + type Saveto = "saveto"; + type SignatureHelp = "signatureHelp"; + type TypeDefinition = "typeDefinition"; + type ProjectInfo = "projectInfo"; + type ReloadProjects = "reloadProjects"; + type Unknown = "unknown"; + type OpenExternalProject = "openExternalProject"; + type OpenExternalProjects = "openExternalProjects"; + type CloseExternalProject = "closeExternalProject"; + type TodoComments = "todoComments"; + type Indentation = "indentation"; + type DocCommentTemplate = "docCommentTemplate"; + type CompilerOptionsForInferredProjects = "compilerOptionsForInferredProjects"; + type GetCodeFixes = "getCodeFixes"; + type GetSupportedCodeFixes = "getSupportedCodeFixes"; + } + /** + * A TypeScript Server message + */ + interface Message { + /** + * Sequence number of the message + */ + seq: number; + /** + * One of "request", "response", or "event" + */ + type: string; + } + /** + * Client-initiated request message + */ + interface Request extends Message { + /** + * The command to execute + */ + command: string; + /** + * Object containing arguments for the command + */ + arguments?: any; + } + /** + * Request to reload the project structure for all the opened files + */ + interface ReloadProjectsRequest extends Message { + command: CommandTypes.ReloadProjects; + } + /** + * Server-initiated event message + */ + interface Event extends Message { + /** + * Name of event + */ + event: string; + /** + * Event-specific information + */ + body?: any; + } + /** + * Response by server to client request message. + */ + interface Response extends Message { + /** + * Sequence number of the request message. + */ + request_seq: number; + /** + * Outcome of the request. + */ + success: boolean; + /** + * The command requested. + */ + command: string; + /** + * Contains error message if success === false. + */ + message?: string; + /** + * Contains message body if success === true. + */ + body?: any; + } + /** + * Arguments for FileRequest messages. + */ + interface FileRequestArgs { + /** + * The file for the request (absolute pathname required). + */ + file: string; + projectFileName?: string; + } + /** + * Requests a JS Doc comment template for a given position + */ + interface DocCommentTemplateRequest extends FileLocationRequest { + command: CommandTypes.DocCommentTemplate; + } + /** + * Response to DocCommentTemplateRequest + */ + interface DocCommandTemplateResponse extends Response { + body?: TextInsertion; + } + /** + * A request to get TODO comments from the file + */ + interface TodoCommentRequest extends FileRequest { + command: CommandTypes.TodoComments; + arguments: TodoCommentRequestArgs; + } + /** + * Arguments for TodoCommentRequest request. + */ + interface TodoCommentRequestArgs extends FileRequestArgs { + /** + * Array of target TodoCommentDescriptors that describes TODO comments to be found + */ + descriptors: TodoCommentDescriptor[]; + } + /** + * Response for TodoCommentRequest request. + */ + interface TodoCommentsResponse extends Response { + body?: TodoComment[]; + } + /** + * A request to get indentation for a location in file + */ + interface IndentationRequest extends FileLocationRequest { + command: CommandTypes.Indentation; + arguments: IndentationRequestArgs; + } + /** + * Response for IndentationRequest request. + */ + interface IndentationResponse extends Response { + body?: IndentationResult; + } + /** + * Indentation result representing where indentation should be placed + */ + interface IndentationResult { + /** + * The base position in the document that the indent should be relative to + */ + position: number; + /** + * The number of columns the indent should be at relative to the position's column. + */ + indentation: number; + } + /** + * Arguments for IndentationRequest request. + */ + interface IndentationRequestArgs extends FileLocationRequestArgs { + /** + * An optional set of settings to be used when computing indentation. + * If argument is omitted - then it will use settings for file that were previously set via 'configure' request or global settings. + */ + options?: EditorSettings; + } + /** + * Arguments for ProjectInfoRequest request. + */ + interface ProjectInfoRequestArgs extends FileRequestArgs { + /** + * Indicate if the file name list of the project is needed + */ + needFileNameList: boolean; + } + /** + * A request to get the project information of the current file. + */ + interface ProjectInfoRequest extends Request { + command: CommandTypes.ProjectInfo; + arguments: ProjectInfoRequestArgs; + } + /** + * A request to retrieve compiler options diagnostics for a project + */ + interface CompilerOptionsDiagnosticsRequest extends Request { + arguments: CompilerOptionsDiagnosticsRequestArgs; + } + /** + * Arguments for CompilerOptionsDiagnosticsRequest request. + */ + interface CompilerOptionsDiagnosticsRequestArgs { + /** + * Name of the project to retrieve compiler options diagnostics. + */ + projectFileName: string; + } + /** + * Response message body for "projectInfo" request + */ + interface ProjectInfo { + /** + * For configured project, this is the normalized path of the 'tsconfig.json' file + * For inferred project, this is undefined + */ + configFileName: string; + /** + * The list of normalized file name in the project, including 'lib.d.ts' + */ + fileNames?: string[]; + /** + * Indicates if the project has a active language service instance + */ + languageServiceDisabled?: boolean; + } + /** + * Represents diagnostic info that includes location of diagnostic in two forms + * - start position and length of the error span + * - startLocation and endLocation - a pair of Location objects that store start/end line and offset of the error span. + */ + interface DiagnosticWithLinePosition { + message: string; + start: number; + length: number; + startLocation: Location; + endLocation: Location; + category: string; + code: number; + } + /** + * Response message for "projectInfo" request + */ + interface ProjectInfoResponse extends Response { + body?: ProjectInfo; + } + /** + * Request whose sole parameter is a file name. + */ + interface FileRequest extends Request { + arguments: FileRequestArgs; + } + /** + * Instances of this interface specify a location in a source file: + * (file, line, character offset), where line and character offset are 1-based. + */ + interface FileLocationRequestArgs extends FileRequestArgs { + /** + * The line number for the request (1-based). + */ + line: number; + /** + * The character offset (on the line) for the request (1-based). + */ + offset: number; + } + /** + * Request for the available codefixes at a specific position. + */ + interface CodeFixRequest extends Request { + command: CommandTypes.GetCodeFixes; + arguments: CodeFixRequestArgs; + } + /** + * Instances of this interface specify errorcodes on a specific location in a sourcefile. + */ + interface CodeFixRequestArgs extends FileRequestArgs { + /** + * The line number for the request (1-based). + */ + startLine: number; + /** + * The character offset (on the line) for the request (1-based). + */ + startOffset: number; + /** + * The line number for the request (1-based). + */ + endLine: number; + /** + * The character offset (on the line) for the request (1-based). + */ + endOffset: number; + /** + * Errorcodes we want to get the fixes for. + */ + errorCodes?: number[]; + } + /** + * Response for GetCodeFixes request. + */ + interface GetCodeFixesResponse extends Response { + body?: CodeAction[]; + } + /** + * A request whose arguments specify a file location (file, line, col). + */ + interface FileLocationRequest extends FileRequest { + arguments: FileLocationRequestArgs; + } + /** + * A request to get codes of supported code fixes. + */ + interface GetSupportedCodeFixesRequest extends Request { + command: CommandTypes.GetSupportedCodeFixes; + } + /** + * A response for GetSupportedCodeFixesRequest request. + */ + interface GetSupportedCodeFixesResponse extends Response { + /** + * List of error codes supported by the server. + */ + body?: string[]; + } + /** + * Arguments for EncodedSemanticClassificationsRequest request. + */ + interface EncodedSemanticClassificationsRequestArgs extends FileRequestArgs { + /** + * Start position of the span. + */ + start: number; + /** + * Length of the span. + */ + length: number; + } + /** + * Arguments in document highlight request; include: filesToSearch, file, + * line, offset. + */ + interface DocumentHighlightsRequestArgs extends FileLocationRequestArgs { + /** + * List of files to search for document highlights. + */ + filesToSearch: string[]; + } + /** + * Go to definition request; value of command field is + * "definition". Return response giving the file locations that + * define the symbol found in file at location line, col. + */ + interface DefinitionRequest extends FileLocationRequest { + command: CommandTypes.Definition; + } + /** + * Go to type request; value of command field is + * "typeDefinition". Return response giving the file locations that + * define the type for the symbol found in file at location line, col. + */ + interface TypeDefinitionRequest extends FileLocationRequest { + command: CommandTypes.TypeDefinition; + } + /** + * Go to implementation request; value of command field is + * "implementation". Return response giving the file locations that + * implement the symbol found in file at location line, col. + */ + interface ImplementationRequest extends FileLocationRequest { + command: CommandTypes.Implementation; + } + /** + * Location in source code expressed as (one-based) line and character offset. + */ + interface Location { + line: number; + offset: number; + } + /** + * Object found in response messages defining a span of text in source code. + */ + interface TextSpan { + /** + * First character of the definition. + */ + start: Location; + /** + * One character past last character of the definition. + */ + end: Location; + } + /** + * Object found in response messages defining a span of text in a specific source file. + */ + interface FileSpan extends TextSpan { + /** + * File containing text span. + */ + file: string; + } + /** + * Definition response message. Gives text range for definition. + */ + interface DefinitionResponse extends Response { + body?: FileSpan[]; + } + /** + * Definition response message. Gives text range for definition. + */ + interface TypeDefinitionResponse extends Response { + body?: FileSpan[]; + } + /** + * Implementation response message. Gives text range for implementations. + */ + interface ImplementationResponse extends Response { + body?: FileSpan[]; + } + /** + * Request to get brace completion for a location in the file. + */ + interface BraceCompletionRequest extends FileLocationRequest { + command: CommandTypes.BraceCompletion; + arguments: BraceCompletionRequestArgs; + } + /** + * Argument for BraceCompletionRequest request. + */ + interface BraceCompletionRequestArgs extends FileLocationRequestArgs { + /** + * Kind of opening brace + */ + openingBrace: string; + } + /** + * Get occurrences request; value of command field is + * "occurrences". Return response giving spans that are relevant + * in the file at a given line and column. + */ + interface OccurrencesRequest extends FileLocationRequest { + command: CommandTypes.Occurrences; + } + interface OccurrencesResponseItem extends FileSpan { + /** + * True if the occurrence is a write location, false otherwise. + */ + isWriteAccess: boolean; + } + interface OccurrencesResponse extends Response { + body?: OccurrencesResponseItem[]; + } + /** + * Get document highlights request; value of command field is + * "documentHighlights". Return response giving spans that are relevant + * in the file at a given line and column. + */ + interface DocumentHighlightsRequest extends FileLocationRequest { + command: CommandTypes.DocumentHighlights; + arguments: DocumentHighlightsRequestArgs; + } + /** + * Span augmented with extra information that denotes the kind of the highlighting to be used for span. + * Kind is taken from HighlightSpanKind type. + */ + interface HighlightSpan extends TextSpan { + kind: string; + } + /** + * Represents a set of highligh spans for a give name + */ + interface DocumentHighlightsItem { + /** + * File containing highlight spans. + */ + file: string; + /** + * Spans to highlight in file. + */ + highlightSpans: HighlightSpan[]; + } + /** + * Response for a DocumentHighlightsRequest request. + */ + interface DocumentHighlightsResponse extends Response { + body?: DocumentHighlightsItem[]; + } + /** + * Find references request; value of command field is + * "references". Return response giving the file locations that + * reference the symbol found in file at location line, col. + */ + interface ReferencesRequest extends FileLocationRequest { + command: CommandTypes.References; + } + interface ReferencesResponseItem extends FileSpan { + /** Text of line containing the reference. Including this + * with the response avoids latency of editor loading files + * to show text of reference line (the server already has + * loaded the referencing files). + */ + lineText: string; + /** + * True if reference is a write location, false otherwise. + */ + isWriteAccess: boolean; + /** + * True if reference is a definition, false otherwise. + */ + isDefinition: boolean; + } + /** + * The body of a "references" response message. + */ + interface ReferencesResponseBody { + /** + * The file locations referencing the symbol. + */ + refs: ReferencesResponseItem[]; + /** + * The name of the symbol. + */ + symbolName: string; + /** + * The start character offset of the symbol (on the line provided by the references request). + */ + symbolStartOffset: number; + /** + * The full display name of the symbol. + */ + symbolDisplayString: string; + } + /** + * Response to "references" request. + */ + interface ReferencesResponse extends Response { + body?: ReferencesResponseBody; + } + /** + * Argument for RenameRequest request. + */ + interface RenameRequestArgs extends FileLocationRequestArgs { + /** + * Should text at specified location be found/changed in comments? + */ + findInComments?: boolean; + /** + * Should text at specified location be found/changed in strings? + */ + findInStrings?: boolean; + } + /** + * Rename request; value of command field is "rename". Return + * response giving the file locations that reference the symbol + * found in file at location line, col. Also return full display + * name of the symbol so that client can print it unambiguously. + */ + interface RenameRequest extends FileLocationRequest { + command: CommandTypes.Rename; + arguments: RenameRequestArgs; + } + /** + * Information about the item to be renamed. + */ + interface RenameInfo { + /** + * True if item can be renamed. + */ + canRename: boolean; + /** + * Error message if item can not be renamed. + */ + localizedErrorMessage?: string; + /** + * Display name of the item to be renamed. + */ + displayName: string; + /** + * Full display name of item to be renamed. + */ + fullDisplayName: string; + /** + * The items's kind (such as 'className' or 'parameterName' or plain 'text'). + */ + kind: string; + /** + * Optional modifiers for the kind (such as 'public'). + */ + kindModifiers: string; + } + /** + * A group of text spans, all in 'file'. + */ + interface SpanGroup { + /** The file to which the spans apply */ + file: string; + /** The text spans in this group */ + locs: TextSpan[]; + } + interface RenameResponseBody { + /** + * Information about the item to be renamed. + */ + info: RenameInfo; + /** + * An array of span groups (one per file) that refer to the item to be renamed. + */ + locs: SpanGroup[]; + } + /** + * Rename response message. + */ + interface RenameResponse extends Response { + body?: RenameResponseBody; + } + /** + * Represents a file in external project. + * External project is project whose set of files, compilation options and open\close state + * is maintained by the client (i.e. if all this data come from .csproj file in Visual Studio). + * External project will exist even if all files in it are closed and should be closed explicity. + * If external project includes one or more tsconfig.json/jsconfig.json files then tsserver will + * create configured project for every config file but will maintain a link that these projects were created + * as a result of opening external project so they should be removed once external project is closed. + */ + interface ExternalFile { + /** + * Name of file file + */ + fileName: string; + /** + * Script kind of the file + */ + scriptKind?: ScriptKind; + /** + * Whether file has mixed content (i.e. .cshtml file that combines html markup with C#/JavaScript) + */ + hasMixedContent?: boolean; + /** + * Content of the file + */ + content?: string; + } + /** + * Represent an external project + */ + interface ExternalProject { + /** + * Project name + */ + projectFileName: string; + /** + * List of root files in project + */ + rootFiles: ExternalFile[]; + /** + * Compiler options for the project + */ + options: ExternalProjectCompilerOptions; + /** + * Explicitly specified typing options for the project + */ + typingOptions?: TypingOptions; + } + /** + * For external projects, some of the project settings are sent together with + * compiler settings. + */ + interface ExternalProjectCompilerOptions extends CompilerOptions { + /** + * If compile on save is enabled for the project + */ + compileOnSave?: boolean; + } + /** + * Contains information about current project version + */ + interface ProjectVersionInfo { + /** + * Project name + */ + projectName: string; + /** + * true if project is inferred or false if project is external or configured + */ + isInferred: boolean; + /** + * Project version + */ + version: number; + /** + * Current set of compiler options for project + */ + options: CompilerOptions; + } + /** + * Represents a set of changes that happen in project + */ + interface ProjectChanges { + /** + * List of added files + */ + added: string[]; + /** + * List of removed files + */ + removed: string[]; + } + /** + * Describes set of files in the project. + * info might be omitted in case of inferred projects + * if files is set - then this is the entire set of files in the project + * if changes is set - then this is the set of changes that should be applied to existing project + * otherwise - assume that nothing is changed + */ + interface ProjectFiles { + /** + * Information abount project verison + */ + info?: ProjectVersionInfo; + /** + * List of files in project (might be omitted if current state of project can be computed using only information from 'changes') + */ + files?: string[]; + /** + * Set of changes in project (omitted if the entire set of files in project should be replaced) + */ + changes?: ProjectChanges; + } + /** + * Combines project information with project level errors. + */ + interface ProjectFilesWithDiagnostics extends ProjectFiles { + /** + * List of errors in project + */ + projectErrors: DiagnosticWithLinePosition[]; + } + /** + * Information found in a configure request. + */ + interface ConfigureRequestArguments { + /** + * Information about the host, for example 'Emacs 24.4' or + * 'Sublime Text version 3075' + */ + hostInfo?: string; + /** + * If present, tab settings apply only to this file. + */ + file?: string; + /** + * The format options to use during formatting and other code editing features. + */ + formatOptions?: FormatCodeSettings; + } + /** + * Configure request; value of command field is "configure". Specifies + * host information, such as host type, tab size, and indent size. + */ + interface ConfigureRequest extends Request { + command: CommandTypes.Configure; + arguments: ConfigureRequestArguments; + } + /** + * Response to "configure" request. This is just an acknowledgement, so + * no body field is required. + */ + interface ConfigureResponse extends Response { + } + /** + * Information found in an "open" request. + */ + interface OpenRequestArgs extends FileRequestArgs { + /** + * Used when a version of the file content is known to be more up to date than the one on disk. + * Then the known content will be used upon opening instead of the disk copy + */ + fileContent?: string; + /** + * Used to specify the script kind of the file explicitly. It could be one of the following: + * "TS", "JS", "TSX", "JSX" + */ + scriptKindName?: "TS" | "JS" | "TSX" | "JSX"; + } + /** + * Open request; value of command field is "open". Notify the + * server that the client has file open. The server will not + * monitor the filesystem for changes in this file and will assume + * that the client is updating the server (using the change and/or + * reload messages) when the file changes. Server does not currently + * send a response to an open request. + */ + interface OpenRequest extends Request { + command: CommandTypes.Open; + arguments: OpenRequestArgs; + } + /** + * Request to open or update external project + */ + interface OpenExternalProjectRequest extends Request { + command: CommandTypes.OpenExternalProject; + arguments: OpenExternalProjectArgs; + } + /** + * Arguments to OpenExternalProjectRequest request + */ + type OpenExternalProjectArgs = ExternalProject; + /** + * Request to open multiple external projects + */ + interface OpenExternalProjectsRequest extends Request { + command: CommandTypes.OpenExternalProjects; + arguments: OpenExternalProjectsArgs; + } + /** + * Arguments to OpenExternalProjectsRequest + */ + interface OpenExternalProjectsArgs { + /** + * List of external projects to open or update + */ + projects: ExternalProject[]; + } + /** + * Response to OpenExternalProjectRequest request. This is just an acknowledgement, so + * no body field is required. + */ + interface OpenExternalProjectResponse extends Response { + } + /** + * Response to OpenExternalProjectsRequest request. This is just an acknowledgement, so + * no body field is required. + */ + interface OpenExternalProjectsResponse extends Response { + } + /** + * Request to close external project. + */ + interface CloseExternalProjectRequest extends Request { + command: CommandTypes.CloseExternalProject; + arguments: CloseExternalProjectRequestArgs; + } + /** + * Arguments to CloseExternalProjectRequest request + */ + interface CloseExternalProjectRequestArgs { + /** + * Name of the project to close + */ + projectFileName: string; + } + /** + * Response to CloseExternalProjectRequest request. This is just an acknowledgement, so + * no body field is required. + */ + interface CloseExternalProjectResponse extends Response { + } + /** + * Arguments to SynchronizeProjectListRequest + */ + interface SynchronizeProjectListRequestArgs { + /** + * List of last known projects + */ + knownProjects: protocol.ProjectVersionInfo[]; + } + /** + * Request to set compiler options for inferred projects. + * External projects are opened / closed explicitly. + * Configured projects are opened when user opens loose file that has 'tsconfig.json' or 'jsconfig.json' anywhere in one of containing folders. + * This configuration file will be used to obtain a list of files and configuration settings for the project. + * Inferred projects are created when user opens a loose file that is not the part of external project + * or configured project and will contain only open file and transitive closure of referenced files if 'useOneInferredProject' is false, + * or all open loose files and its transitive closure of referenced files if 'useOneInferredProject' is true. + */ + interface SetCompilerOptionsForInferredProjectsRequest extends Request { + command: CommandTypes.CompilerOptionsForInferredProjects; + arguments: SetCompilerOptionsForInferredProjectsArgs; + } + /** + * Argument for SetCompilerOptionsForInferredProjectsRequest request. + */ + interface SetCompilerOptionsForInferredProjectsArgs { + /** + * Compiler options to be used with inferred projects. + */ + options: ExternalProjectCompilerOptions; + } + /** + * Response to SetCompilerOptionsForInferredProjectsResponse request. This is just an acknowledgement, so + * no body field is required. + */ + interface SetCompilerOptionsForInferredProjectsResponse extends Response { + } + /** + * Exit request; value of command field is "exit". Ask the server process + * to exit. + */ + interface ExitRequest extends Request { + command: CommandTypes.Exit; + } + /** + * Close request; value of command field is "close". Notify the + * server that the client has closed a previously open file. If + * file is still referenced by open files, the server will resume + * monitoring the filesystem for changes to file. Server does not + * currently send a response to a close request. + */ + interface CloseRequest extends FileRequest { + command: CommandTypes.Close; + } + /** + * Request to obtain the list of files that should be regenerated if target file is recompiled. + * NOTE: this us query-only operation and does not generate any output on disk. + */ + interface CompileOnSaveAffectedFileListRequest extends FileRequest { + command: CommandTypes.CompileOnSaveAffectedFileList; + } + /** + * Contains a list of files that should be regenerated in a project + */ + interface CompileOnSaveAffectedFileListSingleProject { + /** + * Project name + */ + projectFileName: string; + /** + * List of files names that should be recompiled + */ + fileNames: string[]; + } + /** + * Response for CompileOnSaveAffectedFileListRequest request; + */ + interface CompileOnSaveAffectedFileListResponse extends Response { + body: CompileOnSaveAffectedFileListSingleProject[]; + } + /** + * Request to recompile the file. All generated outputs (.js, .d.ts or .js.map files) is written on disk. + */ + interface CompileOnSaveEmitFileRequest extends FileRequest { + command: CommandTypes.CompileOnSaveEmitFile; + arguments: CompileOnSaveEmitFileRequestArgs; + } + /** + * Arguments for CompileOnSaveEmitFileRequest + */ + interface CompileOnSaveEmitFileRequestArgs extends FileRequestArgs { + /** + * if true - then file should be recompiled even if it does not have any changes. + */ + forced?: boolean; + } + /** + * Quickinfo request; value of command field is + * "quickinfo". Return response giving a quick type and + * documentation string for the symbol found in file at location + * line, col. + */ + interface QuickInfoRequest extends FileLocationRequest { + command: CommandTypes.Quickinfo; + } + /** + * Body of QuickInfoResponse. + */ + interface QuickInfoResponseBody { + /** + * The symbol's kind (such as 'className' or 'parameterName' or plain 'text'). + */ + kind: string; + /** + * Optional modifiers for the kind (such as 'public'). + */ + kindModifiers: string; + /** + * Starting file location of symbol. + */ + start: Location; + /** + * One past last character of symbol. + */ + end: Location; + /** + * Type and kind of symbol. + */ + displayString: string; + /** + * Documentation associated with symbol. + */ + documentation: string; + } + /** + * Quickinfo response message. + */ + interface QuickInfoResponse extends Response { + body?: QuickInfoResponseBody; + } + /** + * Arguments for format messages. + */ + interface FormatRequestArgs extends FileLocationRequestArgs { + /** + * Last line of range for which to format text in file. + */ + endLine: number; + /** + * Character offset on last line of range for which to format text in file. + */ + endOffset: number; + /** + * Format options to be used. + */ + options?: FormatCodeSettings; + } + /** + * Format request; value of command field is "format". Return + * response giving zero or more edit instructions. The edit + * instructions will be sorted in file order. Applying the edit + * instructions in reverse to file will result in correctly + * reformatted text. + */ + interface FormatRequest extends FileLocationRequest { + command: CommandTypes.Format; + arguments: FormatRequestArgs; + } + /** + * Object found in response messages defining an editing + * instruction for a span of text in source code. The effect of + * this instruction is to replace the text starting at start and + * ending one character before end with newText. For an insertion, + * the text span is empty. For a deletion, newText is empty. + */ + interface CodeEdit { + /** + * First character of the text span to edit. + */ + start: Location; + /** + * One character past last character of the text span to edit. + */ + end: Location; + /** + * Replace the span defined above with this string (may be + * the empty string). + */ + newText: string; + } + interface FileCodeEdits { + fileName: string; + textChanges: CodeEdit[]; + } + interface CodeFixResponse extends Response { + /** The code actions that are available */ + body?: CodeAction[]; + } + interface CodeAction { + /** Description of the code action to display in the UI of the editor */ + description: string; + /** Text changes to apply to each file as part of the code action */ + changes: FileCodeEdits[]; + } + /** + * Format and format on key response message. + */ + interface FormatResponse extends Response { + body?: CodeEdit[]; + } + /** + * Arguments for format on key messages. + */ + interface FormatOnKeyRequestArgs extends FileLocationRequestArgs { + /** + * Key pressed (';', '\n', or '}'). + */ + key: string; + options?: FormatCodeSettings; + } + /** + * Format on key request; value of command field is + * "formatonkey". Given file location and key typed (as string), + * return response giving zero or more edit instructions. The + * edit instructions will be sorted in file order. Applying the + * edit instructions in reverse to file will result in correctly + * reformatted text. + */ + interface FormatOnKeyRequest extends FileLocationRequest { + command: CommandTypes.Formatonkey; + arguments: FormatOnKeyRequestArgs; + } + /** + * Arguments for completions messages. + */ + interface CompletionsRequestArgs extends FileLocationRequestArgs { + /** + * Optional prefix to apply to possible completions. + */ + prefix?: string; + } + /** + * Completions request; value of command field is "completions". + * Given a file location (file, line, col) and a prefix (which may + * be the empty string), return the possible completions that + * begin with prefix. + */ + interface CompletionsRequest extends FileLocationRequest { + command: CommandTypes.Completions; + arguments: CompletionsRequestArgs; + } + /** + * Arguments for completion details request. + */ + interface CompletionDetailsRequestArgs extends FileLocationRequestArgs { + /** + * Names of one or more entries for which to obtain details. + */ + entryNames: string[]; + } + /** + * Completion entry details request; value of command field is + * "completionEntryDetails". Given a file location (file, line, + * col) and an array of completion entry names return more + * detailed information for each completion entry. + */ + interface CompletionDetailsRequest extends FileLocationRequest { + command: CommandTypes.CompletionDetails; + arguments: CompletionDetailsRequestArgs; + } + /** + * Part of a symbol description. + */ + interface SymbolDisplayPart { + /** + * Text of an item describing the symbol. + */ + text: string; + /** + * The symbol's kind (such as 'className' or 'parameterName' or plain 'text'). + */ + kind: string; + } + /** + * An item found in a completion response. + */ + interface CompletionEntry { + /** + * The symbol's name. + */ + name: string; + /** + * The symbol's kind (such as 'className' or 'parameterName'). + */ + kind: string; + /** + * Optional modifiers for the kind (such as 'public'). + */ + kindModifiers: string; + /** + * A string that is used for comparing completion items so that they can be ordered. This + * is often the same as the name but may be different in certain circumstances. + */ + sortText: string; + /** + * An optional span that indicates the text to be replaced by this completion item. If present, + * this span should be used instead of the default one. + */ + replacementSpan?: TextSpan; + } + /** + * Additional completion entry details, available on demand + */ + interface CompletionEntryDetails { + /** + * The symbol's name. + */ + name: string; + /** + * The symbol's kind (such as 'className' or 'parameterName'). + */ + kind: string; + /** + * Optional modifiers for the kind (such as 'public'). + */ + kindModifiers: string; + /** + * Display parts of the symbol (similar to quick info). + */ + displayParts: SymbolDisplayPart[]; + /** + * Documentation strings for the symbol. + */ + documentation: SymbolDisplayPart[]; + } + interface CompletionsResponse extends Response { + body?: CompletionEntry[]; + } + interface CompletionDetailsResponse extends Response { + body?: CompletionEntryDetails[]; + } + /** + * Signature help information for a single parameter + */ + interface SignatureHelpParameter { + /** + * The parameter's name + */ + name: string; + /** + * Documentation of the parameter. + */ + documentation: SymbolDisplayPart[]; + /** + * Display parts of the parameter. + */ + displayParts: SymbolDisplayPart[]; + /** + * Whether the parameter is optional or not. + */ + isOptional: boolean; + } + /** + * Represents a single signature to show in signature help. + */ + interface SignatureHelpItem { + /** + * Whether the signature accepts a variable number of arguments. + */ + isVariadic: boolean; + /** + * The prefix display parts. + */ + prefixDisplayParts: SymbolDisplayPart[]; + /** + * The suffix display parts. + */ + suffixDisplayParts: SymbolDisplayPart[]; + /** + * The separator display parts. + */ + separatorDisplayParts: SymbolDisplayPart[]; + /** + * The signature helps items for the parameters. + */ + parameters: SignatureHelpParameter[]; + /** + * The signature's documentation + */ + documentation: SymbolDisplayPart[]; + } + /** + * Signature help items found in the response of a signature help request. + */ + interface SignatureHelpItems { + /** + * The signature help items. + */ + items: SignatureHelpItem[]; + /** + * The span for which signature help should appear on a signature + */ + applicableSpan: TextSpan; + /** + * The item selected in the set of available help items. + */ + selectedItemIndex: number; + /** + * The argument selected in the set of parameters. + */ + argumentIndex: number; + /** + * The argument count + */ + argumentCount: number; + } + /** + * Arguments of a signature help request. + */ + interface SignatureHelpRequestArgs extends FileLocationRequestArgs { + } + /** + * Signature help request; value of command field is "signatureHelp". + * Given a file location (file, line, col), return the signature + * help. + */ + interface SignatureHelpRequest extends FileLocationRequest { + command: CommandTypes.SignatureHelp; + arguments: SignatureHelpRequestArgs; + } + /** + * Response object for a SignatureHelpRequest. + */ + interface SignatureHelpResponse extends Response { + body?: SignatureHelpItems; + } + /** + * Synchronous request for semantic diagnostics of one file. + */ + interface SemanticDiagnosticsSyncRequest extends FileRequest { + command: CommandTypes.SemanticDiagnosticsSync; + arguments: SemanticDiagnosticsSyncRequestArgs; + } + interface SemanticDiagnosticsSyncRequestArgs extends FileRequestArgs { + includeLinePosition?: boolean; + } + /** + * Response object for synchronous sematic diagnostics request. + */ + interface SemanticDiagnosticsSyncResponse extends Response { + body?: Diagnostic[] | DiagnosticWithLinePosition[]; + } + /** + * Synchronous request for syntactic diagnostics of one file. + */ + interface SyntacticDiagnosticsSyncRequest extends FileRequest { + command: CommandTypes.SyntacticDiagnosticsSync; + arguments: SyntacticDiagnosticsSyncRequestArgs; + } + interface SyntacticDiagnosticsSyncRequestArgs extends FileRequestArgs { + includeLinePosition?: boolean; + } + /** + * Response object for synchronous syntactic diagnostics request. + */ + interface SyntacticDiagnosticsSyncResponse extends Response { + body?: Diagnostic[] | DiagnosticWithLinePosition[]; + } + /** + * Arguments for GeterrForProject request. + */ + interface GeterrForProjectRequestArgs { + /** + * the file requesting project error list + */ + file: string; + /** + * Delay in milliseconds to wait before starting to compute + * errors for the files in the file list + */ + delay: number; + } + /** + * GeterrForProjectRequest request; value of command field is + * "geterrForProject". It works similarly with 'Geterr', only + * it request for every file in this project. + */ + interface GeterrForProjectRequest extends Request { + command: CommandTypes.GeterrForProject; + arguments: GeterrForProjectRequestArgs; + } + /** + * Arguments for geterr messages. + */ + interface GeterrRequestArgs { + /** + * List of file names for which to compute compiler errors. + * The files will be checked in list order. + */ + files: string[]; + /** + * Delay in milliseconds to wait before starting to compute + * errors for the files in the file list + */ + delay: number; + } + /** + * Geterr request; value of command field is "geterr". Wait for + * delay milliseconds and then, if during the wait no change or + * reload messages have arrived for the first file in the files + * list, get the syntactic errors for the file, field requests, + * and then get the semantic errors for the file. Repeat with a + * smaller delay for each subsequent file on the files list. Best + * practice for an editor is to send a file list containing each + * file that is currently visible, in most-recently-used order. + */ + interface GeterrRequest extends Request { + command: CommandTypes.Geterr; + arguments: GeterrRequestArgs; + } + /** + * Item of diagnostic information found in a DiagnosticEvent message. + */ + interface Diagnostic { + /** + * Starting file location at which text applies. + */ + start: Location; + /** + * The last file location at which the text applies. + */ + end: Location; + /** + * Text of diagnostic message. + */ + text: string; + /** + * The error code of the diagnostic message. + */ + code?: number; + } + interface DiagnosticEventBody { + /** + * The file for which diagnostic information is reported. + */ + file: string; + /** + * An array of diagnostic information items. + */ + diagnostics: Diagnostic[]; + } + /** + * Event message for "syntaxDiag" and "semanticDiag" event types. + * These events provide syntactic and semantic errors for a file. + */ + interface DiagnosticEvent extends Event { + body?: DiagnosticEventBody; + } + interface ConfigFileDiagnosticEventBody { + /** + * The file which trigged the searching and error-checking of the config file + */ + triggerFile: string; + /** + * The name of the found config file. + */ + configFile: string; + /** + * An arry of diagnostic information items for the found config file. + */ + diagnostics: Diagnostic[]; + } + /** + * Event message for "configFileDiag" event type. + * This event provides errors for a found config file. + */ + interface ConfigFileDiagnosticEvent extends Event { + body?: ConfigFileDiagnosticEventBody; + event: "configFileDiag"; + } + /** + * Arguments for reload request. + */ + interface ReloadRequestArgs extends FileRequestArgs { + /** + * Name of temporary file from which to reload file + * contents. May be same as file. + */ + tmpfile: string; + } + /** + * Reload request message; value of command field is "reload". + * Reload contents of file with name given by the 'file' argument + * from temporary file with name given by the 'tmpfile' argument. + * The two names can be identical. + */ + interface ReloadRequest extends FileRequest { + command: CommandTypes.Reload; + arguments: ReloadRequestArgs; + } + /** + * Response to "reload" request. This is just an acknowledgement, so + * no body field is required. + */ + interface ReloadResponse extends Response { + } + /** + * Arguments for saveto request. + */ + interface SavetoRequestArgs extends FileRequestArgs { + /** + * Name of temporary file into which to save server's view of + * file contents. + */ + tmpfile: string; + } + /** + * Saveto request message; value of command field is "saveto". + * For debugging purposes, save to a temporaryfile (named by + * argument 'tmpfile') the contents of file named by argument + * 'file'. The server does not currently send a response to a + * "saveto" request. + */ + interface SavetoRequest extends FileRequest { + command: CommandTypes.Saveto; + arguments: SavetoRequestArgs; + } + /** + * Arguments for navto request message. + */ + interface NavtoRequestArgs extends FileRequestArgs { + /** + * Search term to navigate to from current location; term can + * be '.*' or an identifier prefix. + */ + searchValue: string; + /** + * Optional limit on the number of items to return. + */ + maxResultCount?: number; + /** + * Optional flag to indicate we want results for just the current file + * or the entire project. + */ + currentFileOnly?: boolean; + projectFileName?: string; + } + /** + * Navto request message; value of command field is "navto". + * Return list of objects giving file locations and symbols that + * match the search term given in argument 'searchTerm'. The + * context for the search is given by the named file. + */ + interface NavtoRequest extends FileRequest { + command: CommandTypes.Navto; + arguments: NavtoRequestArgs; + } + /** + * An item found in a navto response. + */ + interface NavtoItem { + /** + * The symbol's name. + */ + name: string; + /** + * The symbol's kind (such as 'className' or 'parameterName'). + */ + kind: string; + /** + * exact, substring, or prefix. + */ + matchKind?: string; + /** + * If this was a case sensitive or insensitive match. + */ + isCaseSensitive?: boolean; + /** + * Optional modifiers for the kind (such as 'public'). + */ + kindModifiers?: string; + /** + * The file in which the symbol is found. + */ + file: string; + /** + * The location within file at which the symbol is found. + */ + start: Location; + /** + * One past the last character of the symbol. + */ + end: Location; + /** + * Name of symbol's container symbol (if any); for example, + * the class name if symbol is a class member. + */ + containerName?: string; + /** + * Kind of symbol's container symbol (if any). + */ + containerKind?: string; + } + /** + * Navto response message. Body is an array of navto items. Each + * item gives a symbol that matched the search term. + */ + interface NavtoResponse extends Response { + body?: NavtoItem[]; + } + /** + * Arguments for change request message. + */ + interface ChangeRequestArgs extends FormatRequestArgs { + /** + * Optional string to insert at location (file, line, offset). + */ + insertString?: string; + } + /** + * Change request message; value of command field is "change". + * Update the server's view of the file named by argument 'file'. + * Server does not currently send a response to a change request. + */ + interface ChangeRequest extends FileLocationRequest { + command: CommandTypes.Change; + arguments: ChangeRequestArgs; + } + /** + * Response to "brace" request. + */ + interface BraceResponse extends Response { + body?: TextSpan[]; + } + /** + * Brace matching request; value of command field is "brace". + * Return response giving the file locations of matching braces + * found in file at location line, offset. + */ + interface BraceRequest extends FileLocationRequest { + command: CommandTypes.Brace; + } + /** + * NavBar items request; value of command field is "navbar". + * Return response giving the list of navigation bar entries + * extracted from the requested file. + */ + interface NavBarRequest extends FileRequest { + command: CommandTypes.NavBar; + } + /** + * NavTree request; value of command field is "navtree". + * Return response giving the navigation tree of the requested file. + */ + interface NavTreeRequest extends FileRequest { + command: CommandTypes.NavTree; + } + interface NavigationBarItem { + /** + * The item's display text. + */ + text: string; + /** + * The symbol's kind (such as 'className' or 'parameterName'). + */ + kind: string; + /** + * Optional modifiers for the kind (such as 'public'). + */ + kindModifiers?: string; + /** + * The definition locations of the item. + */ + spans: TextSpan[]; + /** + * Optional children. + */ + childItems?: NavigationBarItem[]; + /** + * Number of levels deep this item should appear. + */ + indent: number; + } + /** protocol.NavigationTree is identical to ts.NavigationTree, except using protocol.TextSpan instead of ts.TextSpan */ + interface NavigationTree { + text: string; + kind: string; + kindModifiers: string; + spans: TextSpan[]; + childItems?: NavigationTree[]; + } + interface NavBarResponse extends Response { + body?: NavigationBarItem[]; + } + interface NavTreeResponse extends Response { + body?: NavigationTree; + } +} +declare namespace ts.server.protocol { + + interface TextInsertion { + newText: string; + /** The position in newText the caret should point to after the insertion. */ + caretOffset: number; + } + + interface TodoCommentDescriptor { + text: string; + priority: number; + } + + interface TodoComment { + descriptor: TodoCommentDescriptor; + message: string; + position: number; + } + + interface EditorSettings { + baseIndentSize?: number; + indentSize?: number; + tabSize?: number; + newLineCharacter?: string; + convertTabsToSpaces?: boolean; + indentStyle?: IndentStyle; + } + + enum IndentStyle { + None = 0, + Block = 1, + Smart = 2, + } + + enum ScriptKind { + Unknown = 0, + JS = 1, + JSX = 2, + TS = 3, + TSX = 4, + } + + interface TypingOptions { + enableAutoDiscovery?: boolean; + include?: string[]; + exclude?: string[]; + [option: string]: string[] | boolean | undefined; + } + + interface CompilerOptions { + allowJs?: boolean; + allowSyntheticDefaultImports?: boolean; + allowUnreachableCode?: boolean; + allowUnusedLabels?: boolean; + alwaysStrict?: boolean; + baseUrl?: string; + charset?: string; + declaration?: boolean; + declarationDir?: string; + disableSizeLimit?: boolean; + emitBOM?: boolean; + emitDecoratorMetadata?: boolean; + experimentalDecorators?: boolean; + forceConsistentCasingInFileNames?: boolean; + importHelpers?: boolean; + inlineSourceMap?: boolean; + inlineSources?: boolean; + isolatedModules?: boolean; + jsx?: JsxEmit; + lib?: string[]; + locale?: string; + mapRoot?: string; + maxNodeModuleJsDepth?: number; + module?: ModuleKind; + moduleResolution?: ModuleResolutionKind; + newLine?: NewLineKind; + noEmit?: boolean; + noEmitHelpers?: boolean; + noEmitOnError?: boolean; + noErrorTruncation?: boolean; + noFallthroughCasesInSwitch?: boolean; + noImplicitAny?: boolean; + noImplicitReturns?: boolean; + noImplicitThis?: boolean; + noUnusedLocals?: boolean; + noUnusedParameters?: boolean; + noImplicitUseStrict?: boolean; + noLib?: boolean; + noResolve?: boolean; + out?: string; + outDir?: string; + outFile?: string; + paths?: MapLike; + preserveConstEnums?: boolean; + project?: string; + reactNamespace?: string; + removeComments?: boolean; + rootDir?: string; + rootDirs?: string[]; + skipLibCheck?: boolean; + skipDefaultLibCheck?: boolean; + sourceMap?: boolean; + sourceRoot?: string; + strictNullChecks?: boolean; + suppressExcessPropertyErrors?: boolean; + suppressImplicitAnyIndexErrors?: boolean; + target?: ScriptTarget; + traceResolution?: boolean; + types?: string[]; + /** Paths used to used to compute primary types search locations */ + typeRoots?: string[]; + [option: string]: CompilerOptionsValue | undefined; + } + + enum JsxEmit { + None = 0, + Preserve = 1, + React = 2, + } + + enum ModuleKind { + None = 0, + CommonJS = 1, + AMD = 2, + UMD = 3, + System = 4, + ES6 = 5, + ES2015 = 5, + } + + enum ModuleResolutionKind { + Classic = 1, + NodeJs = 2, + } + + enum NewLineKind { + CarriageReturnLineFeed = 0, + LineFeed = 1, + } + + interface MapLike { + [index: string]: T; + } + + enum ScriptTarget { + ES3 = 0, + ES5 = 1, + ES6 = 2, + ES2015 = 2, + Latest = 2, + } + + type CompilerOptionsValue = string | number | boolean | (string | number)[] | string[] | MapLike; + + interface FormatCodeSettings extends EditorSettings { + insertSpaceAfterCommaDelimiter?: boolean; + insertSpaceAfterSemicolonInForStatements?: boolean; + insertSpaceBeforeAndAfterBinaryOperators?: boolean; + insertSpaceAfterKeywordsInControlFlowStatements?: boolean; + insertSpaceAfterFunctionKeywordForAnonymousFunctions?: boolean; + insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis?: boolean; + insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets?: boolean; + insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces?: boolean; + insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces?: boolean; + insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces?: boolean; + insertSpaceAfterTypeAssertion?: boolean; + placeOpenBraceOnNewLineForFunctions?: boolean; + placeOpenBraceOnNewLineForControlBlocks?: boolean; + } +} \ No newline at end of file diff --git a/lib/tsc.js b/lib/tsc.js index aef54c957e694..b1fac7955d0a1 100644 --- a/lib/tsc.js +++ b/lib/tsc.js @@ -67,7 +67,6 @@ var ts; (function (ts) { ts.timestamp = typeof performance !== "undefined" && performance.now ? function () { return performance.now(); } : Date.now ? Date.now : function () { return +(new Date()); }; })(ts || (ts = {})); -var ts; (function (ts) { var performance; (function (performance) { @@ -145,6 +144,7 @@ var ts; contains: contains, remove: remove, forEachValue: forEachValueInMap, + getKeys: getKeys, clear: clear }; function forEachValueInMap(f) { @@ -152,6 +152,13 @@ var ts; f(key, files[key]); } } + function getKeys() { + var keys = []; + for (var key in files) { + keys.push(key); + } + return keys; + } function get(path) { return files[toKey(path)]; } @@ -528,16 +535,22 @@ var ts; : undefined; } ts.lastOrUndefined = lastOrUndefined; - function binarySearch(array, value) { + function binarySearch(array, value, comparer) { + if (!array || array.length === 0) { + return -1; + } var low = 0; var high = array.length - 1; + comparer = comparer !== undefined + ? comparer + : function (v1, v2) { return (v1 < v2 ? -1 : (v1 > v2 ? 1 : 0)); }; while (low <= high) { var middle = low + ((high - low) >> 1); var midValue = array[middle]; - if (midValue === value) { + if (comparer(midValue, value) === 0) { return middle; } - else if (midValue > value) { + else if (comparer(midValue, value) > 0) { high = middle - 1; } else { @@ -771,6 +784,56 @@ var ts; }; } ts.memoize = memoize; + function chain(a, b, c, d, e) { + if (e) { + var args_2 = []; + for (var i = 0; i < arguments.length; i++) { + args_2[i] = arguments[i]; + } + return function (t) { return compose.apply(void 0, map(args_2, function (f) { return f(t); })); }; + } + else if (d) { + return function (t) { return compose(a(t), b(t), c(t), d(t)); }; + } + else if (c) { + return function (t) { return compose(a(t), b(t), c(t)); }; + } + else if (b) { + return function (t) { return compose(a(t), b(t)); }; + } + else if (a) { + return function (t) { return compose(a(t)); }; + } + else { + return function (t) { return function (u) { return u; }; }; + } + } + ts.chain = chain; + function compose(a, b, c, d, e) { + if (e) { + var args_3 = []; + for (var i = 0; i < arguments.length; i++) { + args_3[i] = arguments[i]; + } + return function (t) { return reduceLeft(args_3, function (u, f) { return f(u); }, t); }; + } + else if (d) { + return function (t) { return d(c(b(a(t)))); }; + } + else if (c) { + return function (t) { return c(b(a(t))); }; + } + else if (b) { + return function (t) { return b(a(t)); }; + } + else if (a) { + return function (t) { return a(t); }; + } + else { + return function (t) { return t; }; + } + } + ts.compose = compose; function formatStringFromArgs(text, args, baseIndex) { baseIndex = baseIndex || 0; return text.replace(/{(\d+)}/g, function (match, index) { return args[+index + baseIndex]; }); @@ -1007,10 +1070,45 @@ var ts; return path && !isRootedDiskPath(path) && path.indexOf("://") !== -1; } ts.isUrl = isUrl; + function isExternalModuleNameRelative(moduleName) { + return /^\.\.?($|[\\/])/.test(moduleName); + } + ts.isExternalModuleNameRelative = isExternalModuleNameRelative; + function getEmitScriptTarget(compilerOptions) { + return compilerOptions.target || 0; + } + ts.getEmitScriptTarget = getEmitScriptTarget; + function getEmitModuleKind(compilerOptions) { + return typeof compilerOptions.module === "number" ? + compilerOptions.module : + getEmitScriptTarget(compilerOptions) === 2 ? ts.ModuleKind.ES6 : ts.ModuleKind.CommonJS; + } + ts.getEmitModuleKind = getEmitModuleKind; + function hasZeroOrOneAsteriskCharacter(str) { + var seenAsterisk = false; + for (var i = 0; i < str.length; i++) { + if (str.charCodeAt(i) === 42) { + if (!seenAsterisk) { + seenAsterisk = true; + } + else { + return false; + } + } + } + return true; + } + ts.hasZeroOrOneAsteriskCharacter = hasZeroOrOneAsteriskCharacter; function isRootedDiskPath(path) { return getRootLength(path) !== 0; } ts.isRootedDiskPath = isRootedDiskPath; + function convertToRelativePath(absoluteOrRelativePath, basePath, getCanonicalFileName) { + return !isRootedDiskPath(absoluteOrRelativePath) + ? absoluteOrRelativePath + : getRelativePathToDirectoryOrUrl(basePath, absoluteOrRelativePath, basePath, getCanonicalFileName, false); + } + ts.convertToRelativePath = convertToRelativePath; function normalizedPathComponents(path, rootLength) { var normalizedParts = getNormalizedParts(path, rootLength); return [path.substr(0, rootLength)].concat(normalizedParts); @@ -1384,6 +1482,14 @@ var ts; return options && options.allowJs ? allSupportedExtensions : ts.supportedTypeScriptExtensions; } ts.getSupportedExtensions = getSupportedExtensions; + function hasJavaScriptFileExtension(fileName) { + return forEach(ts.supportedJavascriptExtensions, function (extension) { return fileExtensionIs(fileName, extension); }); + } + ts.hasJavaScriptFileExtension = hasJavaScriptFileExtension; + function hasTypeScriptFileExtension(fileName) { + return forEach(ts.supportedTypeScriptExtensions, function (extension) { return fileExtensionIs(fileName, extension); }); + } + ts.hasTypeScriptFileExtension = hasTypeScriptFileExtension; function isSupportedSourceFileName(fileName, compilerOptions) { if (!fileName) { return false; @@ -1475,7 +1581,6 @@ var ts; this.transformFlags = 0; this.parent = undefined; this.original = undefined; - this.transformId = 0; } ts.objectAllocator = { getNodeConstructor: function () { return Node; }, @@ -1488,9 +1593,9 @@ var ts; }; var Debug; (function (Debug) { - var currentAssertionLevel; + Debug.currentAssertionLevel = 0; function shouldAssert(level) { - return getCurrentAssertionLevel() >= level; + return Debug.currentAssertionLevel >= level; } Debug.shouldAssert = shouldAssert; function assert(expression, message, verboseDebugInfo) { @@ -1508,30 +1613,7 @@ var ts; Debug.assert(false, message); } Debug.fail = fail; - function getCurrentAssertionLevel() { - if (currentAssertionLevel !== undefined) { - return currentAssertionLevel; - } - if (ts.sys === undefined) { - return 0; - } - var developmentMode = /^development$/i.test(getEnvironmentVariable("NODE_ENV")); - currentAssertionLevel = developmentMode - ? 1 - : 0; - return currentAssertionLevel; - } })(Debug = ts.Debug || (ts.Debug = {})); - function getEnvironmentVariable(name, host) { - if (host && host.getEnvironmentVariable) { - return host.getEnvironmentVariable(name); - } - if (ts.sys && ts.sys.getEnvironmentVariable) { - return ts.sys.getEnvironmentVariable(name); - } - return ""; - } - ts.getEnvironmentVariable = getEnvironmentVariable; function orderedRemoveItemAt(array, index) { for (var i = index; i < array.length - 1; i++) { array[i] = array[i + 1]; @@ -1562,6 +1644,64 @@ var ts; : (function (fileName) { return fileName.toLowerCase(); }); } ts.createGetCanonicalFileName = createGetCanonicalFileName; + function matchPatternOrExact(patternStrings, candidate) { + var patterns = []; + for (var _i = 0, patternStrings_1 = patternStrings; _i < patternStrings_1.length; _i++) { + var patternString = patternStrings_1[_i]; + var pattern = tryParsePattern(patternString); + if (pattern) { + patterns.push(pattern); + } + else if (patternString === candidate) { + return patternString; + } + } + return findBestPatternMatch(patterns, function (_) { return _; }, candidate); + } + ts.matchPatternOrExact = matchPatternOrExact; + function patternText(_a) { + var prefix = _a.prefix, suffix = _a.suffix; + return prefix + "*" + suffix; + } + ts.patternText = patternText; + function matchedText(pattern, candidate) { + Debug.assert(isPatternMatch(pattern, candidate)); + return candidate.substr(pattern.prefix.length, candidate.length - pattern.suffix.length); + } + ts.matchedText = matchedText; + function findBestPatternMatch(values, getPattern, candidate) { + var matchedValue = undefined; + var longestMatchPrefixLength = -1; + for (var _i = 0, values_1 = values; _i < values_1.length; _i++) { + var v = values_1[_i]; + var pattern = getPattern(v); + if (isPatternMatch(pattern, candidate) && pattern.prefix.length > longestMatchPrefixLength) { + longestMatchPrefixLength = pattern.prefix.length; + matchedValue = v; + } + } + return matchedValue; + } + ts.findBestPatternMatch = findBestPatternMatch; + function isPatternMatch(_a, candidate) { + var prefix = _a.prefix, suffix = _a.suffix; + return candidate.length >= prefix.length + suffix.length && + startsWith(candidate, prefix) && + endsWith(candidate, suffix); + } + function tryParsePattern(pattern) { + Debug.assert(hasZeroOrOneAsteriskCharacter(pattern)); + var indexOfStar = pattern.indexOf("*"); + return indexOfStar === -1 ? undefined : { + prefix: pattern.substr(0, indexOfStar), + suffix: pattern.substr(indexOfStar + 1) + }; + } + ts.tryParsePattern = tryParsePattern; + function positionIsSynthesized(pos) { + return !(pos >= 0); + } + ts.positionIsSynthesized = positionIsSynthesized; })(ts || (ts = {})); var ts; (function (ts) { @@ -1755,8 +1895,14 @@ var ts; function isNode4OrLater() { return parseInt(process.version.charAt(1)) >= 4; } + function isFileSystemCaseSensitive() { + if (platform === "win32" || platform === "win64") { + return false; + } + return !fileExists(__filename.toUpperCase()) || !fileExists(__filename.toLowerCase()); + } var platform = _os.platform(); - var useCaseSensitiveFileNames = platform !== "win32" && platform !== "win64" && platform !== "darwin"; + var useCaseSensitiveFileNames = isFileSystemCaseSensitive(); function readFile(fileName, encoding) { if (!fileExists(fileName)) { return undefined; @@ -1881,6 +2027,9 @@ var ts; }, watchDirectory: function (directoryName, callback, recursive) { var options; + if (!directoryExists(directoryName)) { + return; + } if (isNode4OrLater() && (process.platform === "win32" || process.platform === "darwin")) { options = { persistent: true, recursive: !!recursive }; } @@ -1992,19 +2141,43 @@ var ts; realpath: realpath }; } + function recursiveCreateDirectory(directoryPath, sys) { + var basePath = ts.getDirectoryPath(directoryPath); + var shouldCreateParent = directoryPath !== basePath && !sys.directoryExists(basePath); + if (shouldCreateParent) { + recursiveCreateDirectory(basePath, sys); + } + if (shouldCreateParent || !sys.directoryExists(directoryPath)) { + sys.createDirectory(directoryPath); + } + } + var sys; if (typeof ChakraHost !== "undefined") { - return getChakraSystem(); + sys = getChakraSystem(); } else if (typeof WScript !== "undefined" && typeof ActiveXObject === "function") { - return getWScriptSystem(); + sys = getWScriptSystem(); } else if (typeof process !== "undefined" && process.nextTick && !process.browser && typeof require !== "undefined") { - return getNodeSystem(); + sys = getNodeSystem(); } - else { - return undefined; + if (sys) { + var originalWriteFile_1 = sys.writeFile; + sys.writeFile = function (path, data, writeBom) { + var directoryPath = ts.getDirectoryPath(ts.normalizeSlashes(path)); + if (directoryPath && !sys.directoryExists(directoryPath)) { + recursiveCreateDirectory(directoryPath, sys); + } + originalWriteFile_1.call(sys, path, data, writeBom); + }; } + return sys; })(); + if (ts.sys && ts.sys.getEnvironmentVariable) { + ts.Debug.currentAssertionLevel = /^development$/i.test(ts.sys.getEnvironmentVariable("NODE_ENV")) + ? 1 + : 0; + } })(ts || (ts = {})); var ts; (function (ts) { @@ -2329,7 +2502,7 @@ var ts; The_right_hand_side_of_a_for_in_statement_must_be_of_type_any_an_object_type_or_a_type_parameter: { code: 2407, category: ts.DiagnosticCategory.Error, key: "The_right_hand_side_of_a_for_in_statement_must_be_of_type_any_an_object_type_or_a_type_parameter_2407", message: "The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter." }, Setters_cannot_return_a_value: { code: 2408, category: ts.DiagnosticCategory.Error, key: "Setters_cannot_return_a_value_2408", message: "Setters cannot return a value." }, Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class: { code: 2409, category: ts.DiagnosticCategory.Error, key: "Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class_2409", message: "Return type of constructor signature must be assignable to the instance type of the class" }, - All_symbols_within_a_with_block_will_be_resolved_to_any: { code: 2410, category: ts.DiagnosticCategory.Error, key: "All_symbols_within_a_with_block_will_be_resolved_to_any_2410", message: "All symbols within a 'with' block will be resolved to 'any'." }, + The_with_statement_is_not_supported_All_symbols_in_a_with_block_will_have_type_any: { code: 2410, category: ts.DiagnosticCategory.Error, key: "The_with_statement_is_not_supported_All_symbols_in_a_with_block_will_have_type_any_2410", message: "The 'with' statement is not supported. All symbols in a 'with' block will have type 'any'." }, Property_0_of_type_1_is_not_assignable_to_string_index_type_2: { code: 2411, category: ts.DiagnosticCategory.Error, key: "Property_0_of_type_1_is_not_assignable_to_string_index_type_2_2411", message: "Property '{0}' of type '{1}' is not assignable to string index type '{2}'." }, Property_0_of_type_1_is_not_assignable_to_numeric_index_type_2: { code: 2412, category: ts.DiagnosticCategory.Error, key: "Property_0_of_type_1_is_not_assignable_to_numeric_index_type_2_2412", message: "Property '{0}' of type '{1}' is not assignable to numeric index type '{2}'." }, Numeric_index_type_0_is_not_assignable_to_string_index_type_1: { code: 2413, category: ts.DiagnosticCategory.Error, key: "Numeric_index_type_0_is_not_assignable_to_string_index_type_1_2413", message: "Numeric index type '{0}' is not assignable to string index type '{1}'." }, @@ -2490,7 +2663,7 @@ var ts; this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation: { code: 2683, category: ts.DiagnosticCategory.Error, key: "this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation_2683", message: "'this' implicitly has type 'any' because it does not have a type annotation." }, The_this_context_of_type_0_is_not_assignable_to_method_s_this_of_type_1: { code: 2684, category: ts.DiagnosticCategory.Error, key: "The_this_context_of_type_0_is_not_assignable_to_method_s_this_of_type_1_2684", message: "The 'this' context of type '{0}' is not assignable to method's 'this' of type '{1}'." }, The_this_types_of_each_signature_are_incompatible: { code: 2685, category: ts.DiagnosticCategory.Error, key: "The_this_types_of_each_signature_are_incompatible_2685", message: "The 'this' types of each signature are incompatible." }, - Identifier_0_must_be_imported_from_a_module: { code: 2686, category: ts.DiagnosticCategory.Error, key: "Identifier_0_must_be_imported_from_a_module_2686", message: "Identifier '{0}' must be imported from a module" }, + _0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead: { code: 2686, category: ts.DiagnosticCategory.Error, key: "_0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead_2686", message: "'{0}' refers to a UMD global, but the current file is a module. Consider adding an import instead." }, All_declarations_of_0_must_have_identical_modifiers: { code: 2687, category: ts.DiagnosticCategory.Error, key: "All_declarations_of_0_must_have_identical_modifiers_2687", message: "All declarations of '{0}' must have identical modifiers." }, Cannot_find_type_definition_file_for_0: { code: 2688, category: ts.DiagnosticCategory.Error, key: "Cannot_find_type_definition_file_for_0_2688", message: "Cannot find type definition file for '{0}'." }, Cannot_extend_an_interface_0_Did_you_mean_implements: { code: 2689, category: ts.DiagnosticCategory.Error, key: "Cannot_extend_an_interface_0_Did_you_mean_implements_2689", message: "Cannot extend an interface '{0}'. Did you mean 'implements'?" }, @@ -2723,6 +2896,8 @@ var ts; No_types_specified_in_package_json_but_allowJs_is_set_so_returning_main_value_of_0: { code: 6137, category: ts.DiagnosticCategory.Message, key: "No_types_specified_in_package_json_but_allowJs_is_set_so_returning_main_value_of_0_6137", message: "No types specified in 'package.json' but 'allowJs' is set, so returning 'main' value of '{0}'" }, Property_0_is_declared_but_never_used: { code: 6138, category: ts.DiagnosticCategory.Error, key: "Property_0_is_declared_but_never_used_6138", message: "Property '{0}' is declared but never used." }, Import_emit_helpers_from_tslib: { code: 6139, category: ts.DiagnosticCategory.Message, key: "Import_emit_helpers_from_tslib_6139", message: "Import emit helpers from 'tslib'." }, + Auto_discovery_for_typings_is_enabled_in_project_0_Running_extra_resolution_pass_for_module_1_using_cache_location_2: { code: 6140, category: ts.DiagnosticCategory.Error, key: "Auto_discovery_for_typings_is_enabled_in_project_0_Running_extra_resolution_pass_for_module_1_using__6140", message: "Auto discovery for typings is enabled in project '{0}'. Running extra resolution pass for module '{1}' using cache location '{2}'." }, + Parse_in_strict_mode_and_emit_use_strict_for_each_source_file: { code: 6141, category: ts.DiagnosticCategory.Message, key: "Parse_in_strict_mode_and_emit_use_strict_for_each_source_file_6141", message: "Parse in strict mode and emit \"use strict\" for each source file" }, Variable_0_implicitly_has_an_1_type: { code: 7005, category: ts.DiagnosticCategory.Error, key: "Variable_0_implicitly_has_an_1_type_7005", message: "Variable '{0}' implicitly has an '{1}' type." }, Parameter_0_implicitly_has_an_1_type: { code: 7006, category: ts.DiagnosticCategory.Error, key: "Parameter_0_implicitly_has_an_1_type_7006", message: "Parameter '{0}' implicitly has an '{1}' type." }, Member_0_implicitly_has_an_1_type: { code: 7008, category: ts.DiagnosticCategory.Error, key: "Member_0_implicitly_has_an_1_type_7008", message: "Member '{0}' implicitly has an '{1}' type." }, @@ -2747,6 +2922,7 @@ var ts; Binding_element_0_implicitly_has_an_1_type: { code: 7031, category: ts.DiagnosticCategory.Error, key: "Binding_element_0_implicitly_has_an_1_type_7031", message: "Binding element '{0}' implicitly has an '{1}' type." }, Property_0_implicitly_has_type_any_because_its_set_accessor_lacks_a_parameter_type_annotation: { code: 7032, category: ts.DiagnosticCategory.Error, key: "Property_0_implicitly_has_type_any_because_its_set_accessor_lacks_a_parameter_type_annotation_7032", message: "Property '{0}' implicitly has type 'any', because its set accessor lacks a parameter type annotation." }, Property_0_implicitly_has_type_any_because_its_get_accessor_lacks_a_return_type_annotation: { code: 7033, category: ts.DiagnosticCategory.Error, key: "Property_0_implicitly_has_type_any_because_its_get_accessor_lacks_a_return_type_annotation_7033", message: "Property '{0}' implicitly has type 'any', because its get accessor lacks a return type annotation." }, + Variable_0_implicitly_has_type_any_in_some_locations_where_its_type_cannot_be_determined: { code: 7034, category: ts.DiagnosticCategory.Error, key: "Variable_0_implicitly_has_type_any_in_some_locations_where_its_type_cannot_be_determined_7034", message: "Variable '{0}' implicitly has type 'any' in some locations where its type cannot be determined." }, You_cannot_rename_this_element: { code: 8000, category: ts.DiagnosticCategory.Error, key: "You_cannot_rename_this_element_8000", message: "You cannot rename this element." }, You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library: { code: 8001, category: ts.DiagnosticCategory.Error, key: "You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library_8001", message: "You cannot rename elements that are defined in the standard TypeScript library." }, import_can_only_be_used_in_a_ts_file: { code: 8002, category: ts.DiagnosticCategory.Error, key: "import_can_only_be_used_in_a_ts_file_8002", message: "'import ... =' can only be used in a .ts file." }, @@ -2776,7 +2952,14 @@ var ts; super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class: { code: 17009, category: ts.DiagnosticCategory.Error, key: "super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class_17009", message: "'super' must be called before accessing 'this' in the constructor of a derived class." }, Unknown_typing_option_0: { code: 17010, category: ts.DiagnosticCategory.Error, key: "Unknown_typing_option_0_17010", message: "Unknown typing option '{0}'." }, Circularity_detected_while_resolving_configuration_Colon_0: { code: 18000, category: ts.DiagnosticCategory.Error, key: "Circularity_detected_while_resolving_configuration_Colon_0_18000", message: "Circularity detected while resolving configuration: {0}" }, - The_path_in_an_extends_options_must_be_relative_or_rooted: { code: 18001, category: ts.DiagnosticCategory.Error, key: "The_path_in_an_extends_options_must_be_relative_or_rooted_18001", message: "The path in an 'extends' options must be relative or rooted." } + The_path_in_an_extends_options_must_be_relative_or_rooted: { code: 18001, category: ts.DiagnosticCategory.Error, key: "The_path_in_an_extends_options_must_be_relative_or_rooted_18001", message: "The path in an 'extends' options must be relative or rooted." }, + Add_missing_super_call: { code: 90001, category: ts.DiagnosticCategory.Message, key: "Add_missing_super_call_90001", message: "Add missing 'super()' call." }, + Make_super_call_the_first_statement_in_the_constructor: { code: 90002, category: ts.DiagnosticCategory.Message, key: "Make_super_call_the_first_statement_in_the_constructor_90002", message: "Make 'super()' call the first statement in the constructor." }, + Change_extends_to_implements: { code: 90003, category: ts.DiagnosticCategory.Message, key: "Change_extends_to_implements_90003", message: "Change 'extends' to 'implements'" }, + Remove_unused_identifiers: { code: 90004, category: ts.DiagnosticCategory.Message, key: "Remove_unused_identifiers_90004", message: "Remove unused identifiers" }, + Implement_interface_on_reference: { code: 90005, category: ts.DiagnosticCategory.Message, key: "Implement_interface_on_reference_90005", message: "Implement interface on reference" }, + Implement_interface_on_class: { code: 90006, category: ts.DiagnosticCategory.Message, key: "Implement_interface_on_class_90006", message: "Implement interface on class" }, + Implement_inherited_abstract_class: { code: 90007, category: ts.DiagnosticCategory.Message, key: "Implement_inherited_abstract_class_90007", message: "Implement inherited abstract class" } }; })(ts || (ts = {})); var ts; @@ -3686,7 +3869,7 @@ var ts; return token = 69; } function scanBinaryOrOctalDigits(base) { - ts.Debug.assert(base !== 2 || base !== 8, "Expected either base 2 or base 8"); + ts.Debug.assert(base === 2 || base === 8, "Expected either base 2 or base 8"); var value = 0; var numberOfDigits = 0; while (true) { @@ -4810,10 +4993,10 @@ var ts; return !!(ts.getCombinedNodeFlags(node) & 1); } ts.isLet = isLet; - function isSuperCallExpression(n) { + function isSuperCall(n) { return n.kind === 174 && n.expression.kind === 95; } - ts.isSuperCallExpression = isSuperCallExpression; + ts.isSuperCall = isSuperCall; function isPrologueDirective(node) { return node.kind === 202 && node.expression.kind === 9; } @@ -5061,6 +5244,12 @@ var ts; return node && node.kind === 147 && node.parent.kind === 171; } ts.isObjectLiteralMethod = isObjectLiteralMethod; + function isObjectLiteralOrClassExpressionMethod(node) { + return node.kind === 147 && + (node.parent.kind === 171 || + node.parent.kind === 192); + } + ts.isObjectLiteralOrClassExpressionMethod = isObjectLiteralOrClassExpressionMethod; function isIdentifierTypePredicate(predicate) { return predicate && predicate.kind === 1; } @@ -5376,10 +5565,6 @@ var ts; return false; } ts.isPartOfExpression = isPartOfExpression; - function isExternalModuleNameRelative(moduleName) { - return /^\.\.?($|[\\/])/.test(moduleName); - } - ts.isExternalModuleNameRelative = isExternalModuleNameRelative; function isInstantiatedModule(node, preserveConstEnums) { var moduleState = ts.getModuleInstanceState(node); return moduleState === 1 || @@ -5977,14 +6162,10 @@ var ts; } ts.nodeStartsNewLexicalEnvironment = nodeStartsNewLexicalEnvironment; function nodeIsSynthesized(node) { - return positionIsSynthesized(node.pos) - || positionIsSynthesized(node.end); + return ts.positionIsSynthesized(node.pos) + || ts.positionIsSynthesized(node.end); } ts.nodeIsSynthesized = nodeIsSynthesized; - function positionIsSynthesized(pos) { - return !(pos >= 0); - } - ts.positionIsSynthesized = positionIsSynthesized; function getOriginalNode(node) { if (node) { while (node.original !== undefined) { @@ -6420,28 +6601,16 @@ var ts; function getDeclarationEmitOutputFilePath(sourceFile, host) { var options = host.getCompilerOptions(); var outputDir = options.declarationDir || options.outDir; - if (options.declaration) { - var path = outputDir - ? getSourceFilePathInNewDir(sourceFile, host, outputDir) - : sourceFile.fileName; - return ts.removeFileExtension(path) + ".d.ts"; - } + var path = outputDir + ? getSourceFilePathInNewDir(sourceFile, host, outputDir) + : sourceFile.fileName; + return ts.removeFileExtension(path) + ".d.ts"; } ts.getDeclarationEmitOutputFilePath = getDeclarationEmitOutputFilePath; - function getEmitScriptTarget(compilerOptions) { - return compilerOptions.target || 0; - } - ts.getEmitScriptTarget = getEmitScriptTarget; - function getEmitModuleKind(compilerOptions) { - return typeof compilerOptions.module === "number" ? - compilerOptions.module : - getEmitScriptTarget(compilerOptions) === 2 ? ts.ModuleKind.ES6 : ts.ModuleKind.CommonJS; - } - ts.getEmitModuleKind = getEmitModuleKind; function getSourceFilesToEmit(host, targetSourceFile) { var options = host.getCompilerOptions(); if (options.outFile || options.out) { - var moduleKind = getEmitModuleKind(options); + var moduleKind = ts.getEmitModuleKind(options); var moduleEmitEnabled = moduleKind === ts.ModuleKind.AMD || moduleKind === ts.ModuleKind.System; var sourceFiles = host.getSourceFiles(); return ts.filter(sourceFiles, moduleEmitEnabled ? isNonDeclarationFile : isBundleEmitNonExternalModule); @@ -6458,7 +6627,7 @@ var ts; function isBundleEmitNonExternalModule(sourceFile) { return !isDeclarationFile(sourceFile) && !ts.isExternalModule(sourceFile); } - function forEachTransformedEmitFile(host, sourceFiles, action) { + function forEachTransformedEmitFile(host, sourceFiles, action, emitOnlyDtsFiles) { var options = host.getCompilerOptions(); if (options.outFile || options.out) { onBundledEmit(host, sourceFiles); @@ -6485,7 +6654,7 @@ var ts; } var jsFilePath = getOwnEmitOutputFilePath(sourceFile, host, extension); var sourceMapFilePath = getSourceMapFilePath(jsFilePath, options); - var declarationFilePath = !isSourceFileJavaScript(sourceFile) ? getDeclarationEmitOutputFilePath(sourceFile, host) : undefined; + var declarationFilePath = !isSourceFileJavaScript(sourceFile) && (options.declaration || emitOnlyDtsFiles) ? getDeclarationEmitOutputFilePath(sourceFile, host) : undefined; action(jsFilePath, sourceMapFilePath, declarationFilePath, [sourceFile], false); } function onBundledEmit(host, sourceFiles) { @@ -6501,7 +6670,7 @@ var ts; function getSourceMapFilePath(jsFilePath, options) { return options.sourceMap ? jsFilePath + ".map" : undefined; } - function forEachExpectedEmitFile(host, action, targetSourceFile) { + function forEachExpectedEmitFile(host, action, targetSourceFile, emitOnlyDtsFiles) { var options = host.getCompilerOptions(); if (options.outFile || options.out) { onBundledEmit(host); @@ -6528,18 +6697,19 @@ var ts; } } var jsFilePath = getOwnEmitOutputFilePath(sourceFile, host, extension); + var declarationFilePath = !isSourceFileJavaScript(sourceFile) && (emitOnlyDtsFiles || options.declaration) ? getDeclarationEmitOutputFilePath(sourceFile, host) : undefined; var emitFileNames = { jsFilePath: jsFilePath, sourceMapFilePath: getSourceMapFilePath(jsFilePath, options), - declarationFilePath: !isSourceFileJavaScript(sourceFile) ? getDeclarationEmitOutputFilePath(sourceFile, host) : undefined + declarationFilePath: declarationFilePath }; - action(emitFileNames, [sourceFile], false); + action(emitFileNames, [sourceFile], false, emitOnlyDtsFiles); } function onBundledEmit(host) { var bundledSources = ts.filter(host.getSourceFiles(), function (sourceFile) { return !isDeclarationFile(sourceFile) && !host.isSourceFileFromExternalLibrary(sourceFile) && (!ts.isExternalModule(sourceFile) || - !!getEmitModuleKind(options)); }); + !!ts.getEmitModuleKind(options)); }); if (bundledSources.length) { var jsFilePath = options.outFile || options.out; var emitFileNames = { @@ -6547,7 +6717,7 @@ var ts; sourceMapFilePath: getSourceMapFilePath(jsFilePath, options), declarationFilePath: options.declaration ? ts.removeFileExtension(jsFilePath) + ".d.ts" : undefined }; - action(emitFileNames, bundledSources, true); + action(emitFileNames, bundledSources, true, emitOnlyDtsFiles); } } } @@ -6584,13 +6754,32 @@ var ts; ts.getFirstConstructorWithBody = getFirstConstructorWithBody; function getSetAccessorTypeAnnotationNode(accessor) { if (accessor && accessor.parameters.length > 0) { - var hasThis = accessor.parameters.length === 2 && - accessor.parameters[0].name.kind === 69 && - accessor.parameters[0].name.originalKeywordKind === 97; + var hasThis = accessor.parameters.length === 2 && parameterIsThisKeyword(accessor.parameters[0]); return accessor.parameters[hasThis ? 1 : 0].type; } } ts.getSetAccessorTypeAnnotationNode = getSetAccessorTypeAnnotationNode; + function getThisParameter(signature) { + if (signature.parameters.length) { + var thisParameter = signature.parameters[0]; + if (parameterIsThisKeyword(thisParameter)) { + return thisParameter; + } + } + } + ts.getThisParameter = getThisParameter; + function parameterIsThisKeyword(parameter) { + return isThisIdentifier(parameter.name); + } + ts.parameterIsThisKeyword = parameterIsThisKeyword; + function isThisIdentifier(node) { + return node && node.kind === 69 && identifierIsThisKeyword(node); + } + ts.isThisIdentifier = isThisIdentifier; + function identifierIsThisKeyword(id) { + return id.originalKeywordKind === 97; + } + ts.identifierIsThisKeyword = identifierIsThisKeyword; function getAllAccessorDeclarations(declarations, accessor) { var firstAccessor; var secondAccessor; @@ -6904,14 +7093,6 @@ var ts; return symbol && symbol.valueDeclaration && hasModifier(symbol.valueDeclaration, 512) ? symbol.valueDeclaration.localSymbol : undefined; } ts.getLocalSymbolForExportDefault = getLocalSymbolForExportDefault; - function hasJavaScriptFileExtension(fileName) { - return ts.forEach(ts.supportedJavascriptExtensions, function (extension) { return ts.fileExtensionIs(fileName, extension); }); - } - ts.hasJavaScriptFileExtension = hasJavaScriptFileExtension; - function hasTypeScriptFileExtension(fileName) { - return ts.forEach(ts.supportedTypeScriptExtensions, function (extension) { return ts.fileExtensionIs(fileName, extension); }); - } - ts.hasTypeScriptFileExtension = hasTypeScriptFileExtension; function tryExtractTypeScriptExtension(fileName) { return ts.find(ts.supportedTypescriptExtensionsForExtractExtension, function (extension) { return ts.fileExtensionIs(fileName, extension); }); } @@ -7002,12 +7183,6 @@ var ts; return result; } ts.convertToBase64 = convertToBase64; - function convertToRelativePath(absoluteOrRelativePath, basePath, getCanonicalFileName) { - return !ts.isRootedDiskPath(absoluteOrRelativePath) - ? absoluteOrRelativePath - : ts.getRelativePathToDirectoryOrUrl(basePath, absoluteOrRelativePath, basePath, getCanonicalFileName, false); - } - ts.convertToRelativePath = convertToRelativePath; var carriageReturnLineFeed = "\r\n"; var lineFeed = "\n"; function getNewLineCharacter(options) { @@ -7108,7 +7283,7 @@ var ts; } ts.formatSyntaxKind = formatSyntaxKind; function movePos(pos, value) { - return positionIsSynthesized(pos) ? -1 : pos + value; + return ts.positionIsSynthesized(pos) ? -1 : pos + value; } ts.movePos = movePos; function createRange(pos, end) { @@ -7177,7 +7352,7 @@ var ts; } ts.positionsAreOnSameLine = positionsAreOnSameLine; function getStartPositionOfRange(range, sourceFile) { - return positionIsSynthesized(range.pos) ? -1 : ts.skipTrivia(sourceFile.text, range.pos); + return ts.positionIsSynthesized(range.pos) ? -1 : ts.skipTrivia(sourceFile.text, range.pos); } ts.getStartPositionOfRange = getStartPositionOfRange; function collectExternalModuleInfo(sourceFile, resolver) { @@ -7277,15 +7452,16 @@ var ts; return 11 <= kind && kind <= 14; } ts.isTemplateLiteralKind = isTemplateLiteralKind; - function isTemplateLiteralFragmentKind(kind) { - return kind === 12 - || kind === 13 - || kind === 14; + function isTemplateHead(node) { + return node.kind === 12; } - function isTemplateLiteralFragment(node) { - return isTemplateLiteralFragmentKind(node.kind); + ts.isTemplateHead = isTemplateHead; + function isTemplateMiddleOrTemplateTail(node) { + var kind = node.kind; + return kind === 13 + || kind === 14; } - ts.isTemplateLiteralFragment = isTemplateLiteralFragment; + ts.isTemplateMiddleOrTemplateTail = isTemplateMiddleOrTemplateTail; function isIdentifier(node) { return node.kind === 69; } @@ -7424,12 +7600,12 @@ var ts; return node.kind === 174; } ts.isCallExpression = isCallExpression; - function isTemplate(node) { + function isTemplateLiteral(node) { var kind = node.kind; return kind === 189 || kind === 11; } - ts.isTemplate = isTemplate; + ts.isTemplateLiteral = isTemplateLiteral; function isSpreadElementExpression(node) { return node.kind === 191; } @@ -7760,7 +7936,6 @@ var ts; } ts.isWatchSet = isWatchSet; })(ts || (ts = {})); -var ts; (function (ts) { function getDefaultLibFileName(options) { return options.target === 2 ? "lib.es6.d.ts" : "lib.d.ts"; @@ -7995,7 +8170,7 @@ var ts; ts.createSynthesizedNodeArray = createSynthesizedNodeArray; function getSynthesizedClone(node) { var clone = createNode(node.kind, undefined, node.flags); - clone.original = node; + setOriginalNode(clone, node); for (var key in node) { if (clone.hasOwnProperty(key) || !node.hasOwnProperty(key)) { continue; @@ -8027,7 +8202,7 @@ var ts; node.text = value; return node; } - else { + else if (value) { var node = createNode(9, location, undefined); node.textSourceNode = value; node.text = value.text; @@ -8314,7 +8489,7 @@ var ts; function createPropertyAccess(expression, name, location, flags) { var node = createNode(172, location, flags); node.expression = parenthesizeForAccess(expression); - node.emitFlags = 1048576; + (node.emitNode || (node.emitNode = {})).flags |= 1048576; node.name = typeof name === "string" ? createIdentifier(name) : name; return node; } @@ -8322,7 +8497,7 @@ var ts; function updatePropertyAccess(node, expression, name) { if (node.expression !== expression || node.name !== name) { var propertyAccess = createPropertyAccess(expression, name, node, node.flags); - propertyAccess.emitFlags = node.emitFlags; + (propertyAccess.emitNode || (propertyAccess.emitNode = {})).flags = getEmitFlags(node); return updateNode(propertyAccess, node); } return node; @@ -8426,7 +8601,7 @@ var ts; node.typeParameters = typeParameters ? createNodeArray(typeParameters) : undefined; node.parameters = createNodeArray(parameters); node.type = type; - node.equalsGreaterThanToken = equalsGreaterThanToken || createNode(34); + node.equalsGreaterThanToken = equalsGreaterThanToken || createToken(34); node.body = parenthesizeConciseBody(body); return node; } @@ -8519,7 +8694,7 @@ var ts; } ts.updatePostfix = updatePostfix; function createBinary(left, operator, right, location) { - var operatorToken = typeof operator === "number" ? createSynthesizedNode(operator) : operator; + var operatorToken = typeof operator === "number" ? createToken(operator) : operator; var operatorKind = operatorToken.kind; var node = createNode(187, location); node.left = parenthesizeBinaryOperand(operatorKind, left, true, undefined); @@ -9419,13 +9594,13 @@ var ts; } else { var expression = ts.isIdentifier(memberName) ? createPropertyAccess(target, memberName, location) : createElementAccess(target, memberName, location); - expression.emitFlags |= 2048; + (expression.emitNode || (expression.emitNode = {})).flags |= 2048; return expression; } } ts.createMemberAccessForPropertyName = createMemberAccessForPropertyName; function createRestParameter(name) { - return createParameterDeclaration(undefined, undefined, createSynthesizedNode(22), name, undefined, undefined, undefined); + return createParameterDeclaration(undefined, undefined, createToken(22), name, undefined, undefined, undefined); } ts.createRestParameter = createRestParameter; function createFunctionCall(func, thisArg, argumentsList, location) { @@ -9539,8 +9714,8 @@ var ts; } ts.createDecorateHelper = createDecorateHelper; function createAwaiterHelper(externalHelpersModuleName, hasLexicalArguments, promiseConstructor, body) { - var generatorFunc = createFunctionExpression(createNode(37), undefined, undefined, [], undefined, body); - generatorFunc.emitFlags |= 2097152; + var generatorFunc = createFunctionExpression(createToken(37), undefined, undefined, [], undefined, body); + (generatorFunc.emitNode || (generatorFunc.emitNode = {})).flags |= 2097152; return createCall(createHelperName(externalHelpersModuleName, "__awaiter"), undefined, [ createThis(), hasLexicalArguments ? createIdentifier("arguments") : createVoidZero(), @@ -9767,7 +9942,7 @@ var ts; target.push(startOnNewLine(createStatement(createLiteral("use strict")))); foundUseStrict = true; } - if (statement.emitFlags & 8388608) { + if (getEmitFlags(statement) & 8388608) { target.push(visitor ? ts.visitNode(statement, visitor, ts.isStatement) : statement); } else { @@ -9779,6 +9954,28 @@ var ts; return statementOffset; } ts.addPrologueDirectives = addPrologueDirectives; + function ensureUseStrict(node) { + var foundUseStrict = false; + for (var _i = 0, _a = node.statements; _i < _a.length; _i++) { + var statement = _a[_i]; + if (ts.isPrologueDirective(statement)) { + if (isUseStrictPrologue(statement)) { + foundUseStrict = true; + break; + } + } + else { + break; + } + } + if (!foundUseStrict) { + var statements = []; + statements.push(startOnNewLine(createStatement(createLiteral("use strict")))); + return updateSourceFileNode(node, statements.concat(node.statements)); + } + return node; + } + ts.ensureUseStrict = ensureUseStrict; function parenthesizeBinaryOperand(binaryOperator, operand, isLeftSideOfBinary, leftOperand) { var skipped = skipPartiallyEmittedExpressions(operand); if (skipped.kind === 178) { @@ -10018,17 +10215,112 @@ var ts; function setOriginalNode(node, original) { node.original = original; if (original) { - var emitFlags = original.emitFlags, commentRange = original.commentRange, sourceMapRange = original.sourceMapRange; - if (emitFlags) - node.emitFlags = emitFlags; - if (commentRange) - node.commentRange = commentRange; - if (sourceMapRange) - node.sourceMapRange = sourceMapRange; + var emitNode = original.emitNode; + if (emitNode) + node.emitNode = mergeEmitNode(emitNode, node.emitNode); } return node; } ts.setOriginalNode = setOriginalNode; + function mergeEmitNode(sourceEmitNode, destEmitNode) { + var flags = sourceEmitNode.flags, commentRange = sourceEmitNode.commentRange, sourceMapRange = sourceEmitNode.sourceMapRange, tokenSourceMapRanges = sourceEmitNode.tokenSourceMapRanges; + if (!destEmitNode && (flags || commentRange || sourceMapRange || tokenSourceMapRanges)) + destEmitNode = {}; + if (flags) + destEmitNode.flags = flags; + if (commentRange) + destEmitNode.commentRange = commentRange; + if (sourceMapRange) + destEmitNode.sourceMapRange = sourceMapRange; + if (tokenSourceMapRanges) + destEmitNode.tokenSourceMapRanges = mergeTokenSourceMapRanges(tokenSourceMapRanges, destEmitNode.tokenSourceMapRanges); + return destEmitNode; + } + function mergeTokenSourceMapRanges(sourceRanges, destRanges) { + if (!destRanges) + destRanges = ts.createMap(); + ts.copyProperties(sourceRanges, destRanges); + return destRanges; + } + function disposeEmitNodes(sourceFile) { + sourceFile = ts.getSourceFileOfNode(ts.getParseTreeNode(sourceFile)); + var emitNode = sourceFile && sourceFile.emitNode; + var annotatedNodes = emitNode && emitNode.annotatedNodes; + if (annotatedNodes) { + for (var _i = 0, annotatedNodes_1 = annotatedNodes; _i < annotatedNodes_1.length; _i++) { + var node = annotatedNodes_1[_i]; + node.emitNode = undefined; + } + } + } + ts.disposeEmitNodes = disposeEmitNodes; + function getOrCreateEmitNode(node) { + if (!node.emitNode) { + if (ts.isParseTreeNode(node)) { + if (node.kind === 256) { + return node.emitNode = { annotatedNodes: [node] }; + } + var sourceFile = ts.getSourceFileOfNode(node); + getOrCreateEmitNode(sourceFile).annotatedNodes.push(node); + } + node.emitNode = {}; + } + return node.emitNode; + } + function getEmitFlags(node) { + var emitNode = node.emitNode; + return emitNode && emitNode.flags; + } + ts.getEmitFlags = getEmitFlags; + function setEmitFlags(node, emitFlags) { + getOrCreateEmitNode(node).flags = emitFlags; + return node; + } + ts.setEmitFlags = setEmitFlags; + function setSourceMapRange(node, range) { + getOrCreateEmitNode(node).sourceMapRange = range; + return node; + } + ts.setSourceMapRange = setSourceMapRange; + function setTokenSourceMapRange(node, token, range) { + var emitNode = getOrCreateEmitNode(node); + var tokenSourceMapRanges = emitNode.tokenSourceMapRanges || (emitNode.tokenSourceMapRanges = ts.createMap()); + tokenSourceMapRanges[token] = range; + return node; + } + ts.setTokenSourceMapRange = setTokenSourceMapRange; + function setCommentRange(node, range) { + getOrCreateEmitNode(node).commentRange = range; + return node; + } + ts.setCommentRange = setCommentRange; + function getCommentRange(node) { + var emitNode = node.emitNode; + return (emitNode && emitNode.commentRange) || node; + } + ts.getCommentRange = getCommentRange; + function getSourceMapRange(node) { + var emitNode = node.emitNode; + return (emitNode && emitNode.sourceMapRange) || node; + } + ts.getSourceMapRange = getSourceMapRange; + function getTokenSourceMapRange(node, token) { + var emitNode = node.emitNode; + var tokenSourceMapRanges = emitNode && emitNode.tokenSourceMapRanges; + return tokenSourceMapRanges && tokenSourceMapRanges[token]; + } + ts.getTokenSourceMapRange = getTokenSourceMapRange; + function getConstantValue(node) { + var emitNode = node.emitNode; + return emitNode && emitNode.constantValue; + } + ts.getConstantValue = getConstantValue; + function setConstantValue(node, value) { + var emitNode = getOrCreateEmitNode(node); + emitNode.constantValue = value; + return node; + } + ts.setConstantValue = setConstantValue; function setTextRange(node, location) { if (location) { node.pos = location.pos; @@ -11138,7 +11430,7 @@ var ts; case 16: return token() === 18 || token() === 20; case 18: - return token() === 27 || token() === 17; + return token() !== 24; case 20: return token() === 15 || token() === 16; case 13: @@ -11466,7 +11758,7 @@ var ts; } function parseTemplateExpression() { var template = createNode(189); - template.head = parseTemplateLiteralFragment(); + template.head = parseTemplateHead(); ts.Debug.assert(template.head.kind === 12, "Template head has wrong token kind"); var templateSpans = createNodeArray(); do { @@ -11482,7 +11774,7 @@ var ts; var literal; if (token() === 16) { reScanTemplateToken(); - literal = parseTemplateLiteralFragment(); + literal = parseTemplateMiddleOrTemplateTail(); } else { literal = parseExpectedToken(14, false, ts.Diagnostics._0_expected, ts.tokenToString(16)); @@ -11493,8 +11785,15 @@ var ts; function parseLiteralNode(internName) { return parseLiteralLikeNode(token(), internName); } - function parseTemplateLiteralFragment() { - return parseLiteralLikeNode(token(), false); + function parseTemplateHead() { + var fragment = parseLiteralLikeNode(token(), false); + ts.Debug.assert(fragment.kind === 12, "Template head has wrong token kind"); + return fragment; + } + function parseTemplateMiddleOrTemplateTail() { + var fragment = parseLiteralLikeNode(token(), false); + ts.Debug.assert(fragment.kind === 13 || fragment.kind === 14, "Template fragment has wrong token kind"); + return fragment; } function parseLiteralLikeNode(kind, internName) { var node = createNode(kind); @@ -13842,7 +14141,7 @@ var ts; parseExpected(56); node.type = parseType(); parseSemicolon(); - return finishNode(node); + return addJSDocComment(finishNode(node)); } function parseEnumMember() { var node = createNode(255, scanner.getStartPos()); @@ -15277,7 +15576,7 @@ var ts; file = f; options = opts; languageVersion = ts.getEmitScriptTarget(options); - inStrictMode = !!file.externalModuleIndicator; + inStrictMode = bindInStrictMode(file, opts); classifiableNames = ts.createMap(); symbolCount = 0; skipTransformFlagAggregation = ts.isDeclarationFile(file); @@ -15307,6 +15606,14 @@ var ts; subtreeTransformFlags = 0; } return bindSourceFile; + function bindInStrictMode(file, opts) { + if (opts.alwaysStrict && !ts.isDeclarationFile(file)) { + return true; + } + else { + return !!file.externalModuleIndicator; + } + } function createSymbol(flags, name) { symbolCount++; return new Symbol(flags, name); @@ -15425,11 +15732,17 @@ var ts; var message_1 = symbol.flags & 2 ? ts.Diagnostics.Cannot_redeclare_block_scoped_variable_0 : ts.Diagnostics.Duplicate_identifier_0; - ts.forEach(symbol.declarations, function (declaration) { - if (ts.hasModifier(declaration, 512)) { + if (symbol.declarations && symbol.declarations.length) { + if (isDefaultExport) { message_1 = ts.Diagnostics.A_module_cannot_have_multiple_default_exports; } - }); + else { + if (symbol.declarations && symbol.declarations.length && + (isDefaultExport || (node.kind === 235 && !node.isExportEquals))) { + message_1 = ts.Diagnostics.A_module_cannot_have_multiple_default_exports; + } + } + } ts.forEach(symbol.declarations, function (declaration) { file.bindDiagnostics.push(ts.createDiagnosticForNode(declaration.name || declaration, message_1, getDisplayName(declaration))); }); @@ -15494,7 +15807,7 @@ var ts; } else { currentFlow = { flags: 2 }; - if (containerFlags & 16) { + if (containerFlags & (16 | 128)) { currentFlow.container = node; } currentReturnTarget = undefined; @@ -15949,7 +16262,9 @@ var ts; currentFlow = preTryFlow; bind(node.finallyBlock); } - currentFlow = finishFlowLabel(postFinallyLabel); + if (!node.finallyBlock || !(currentFlow.flags & 1)) { + currentFlow = finishFlowLabel(postFinallyLabel); + } } function bindSwitchStatement(node) { var postSwitchLabel = createBranchLabel(); @@ -16082,7 +16397,7 @@ var ts; } else { ts.forEachChild(node, bind); - if (node.operator === 57 || node.operator === 42) { + if (node.operator === 41 || node.operator === 42) { bindAssignmentTargetFlow(node.operand); } } @@ -16181,9 +16496,12 @@ var ts; return 1 | 32; case 256: return 1 | 4 | 32; + case 147: + if (ts.isObjectLiteralOrClassExpressionMethod(node)) { + return 1 | 4 | 32 | 8 | 128; + } case 148: case 220: - case 147: case 146: case 149: case 150: @@ -16715,7 +17033,7 @@ var ts; var flags = node.kind === 235 && ts.exportAssignmentIsAlias(node) ? 8388608 : 4; - declareSymbol(container.symbol.exports, container.symbol, node, flags, 0 | 8388608); + declareSymbol(container.symbol.exports, container.symbol, node, flags, 4 | 8388608 | 32 | 16); } } function bindNamespaceExportDeclaration(node) { @@ -16912,6 +17230,9 @@ var ts; emitFlags |= 2048; } } + if (currentFlow && ts.isObjectLiteralOrClassExpressionMethod(node)) { + node.flowNode = currentFlow; + } return ts.hasDynamicName(node) ? bindAnonymousDeclaration(node, symbolFlags, "__computed") : declareSymbolAndAddToSymbolTable(node, symbolFlags, symbolExcludes); @@ -17050,8 +17371,7 @@ var ts; if (node.questionToken) { transformFlags |= 3; } - if (subtreeFlags & 2048 - || (name && ts.isIdentifier(name) && name.originalKeywordKind === 97)) { + if (subtreeFlags & 2048 || ts.isThisIdentifier(name)) { transformFlags |= 3; } if (modifierFlags & 92) { @@ -17153,7 +17473,7 @@ var ts; || (subtreeFlags & 2048)) { transformFlags |= 3; } - if (asteriskToken && node.emitFlags & 2097152) { + if (asteriskToken && ts.getEmitFlags(node) & 2097152) { transformFlags |= 1536; } node.transformFlags = transformFlags | 536870912; @@ -17198,7 +17518,7 @@ var ts; if (subtreeFlags & 81920) { transformFlags |= 192; } - if (asteriskToken && node.emitFlags & 2097152) { + if (asteriskToken && ts.getEmitFlags(node) & 2097152) { transformFlags |= 1536; } } @@ -17215,7 +17535,7 @@ var ts; if (subtreeFlags & 81920) { transformFlags |= 192; } - if (asteriskToken && node.emitFlags & 2097152) { + if (asteriskToken && ts.getEmitFlags(node) & 2097152) { transformFlags |= 1536; } node.transformFlags = transformFlags | 536870912; @@ -17457,181 +17777,736 @@ var ts; })(ts || (ts = {})); var ts; (function (ts) { - var ambientModuleSymbolRegex = /^".+"$/; - var nextSymbolId = 1; - var nextNodeId = 1; - var nextMergeId = 1; - var nextFlowId = 1; - function getNodeId(node) { - if (!node.id) { - node.id = nextNodeId; - nextNodeId++; + function trace(host, message) { + host.trace(ts.formatMessage.apply(undefined, arguments)); + } + ts.trace = trace; + function isTraceEnabled(compilerOptions, host) { + return compilerOptions.traceResolution && host.trace !== undefined; + } + ts.isTraceEnabled = isTraceEnabled; + function createResolvedModule(resolvedFileName, isExternalLibraryImport, failedLookupLocations) { + return { resolvedModule: resolvedFileName ? { resolvedFileName: resolvedFileName, isExternalLibraryImport: isExternalLibraryImport } : undefined, failedLookupLocations: failedLookupLocations }; + } + ts.createResolvedModule = createResolvedModule; + function moduleHasNonRelativeName(moduleName) { + return !(ts.isRootedDiskPath(moduleName) || ts.isExternalModuleNameRelative(moduleName)); + } + function tryReadTypesSection(packageJsonPath, baseDirectory, state) { + var jsonContent = readJson(packageJsonPath, state.host); + function tryReadFromField(fieldName) { + if (ts.hasProperty(jsonContent, fieldName)) { + var typesFile = jsonContent[fieldName]; + if (typeof typesFile === "string") { + var typesFilePath_1 = ts.normalizePath(ts.combinePaths(baseDirectory, typesFile)); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.package_json_has_0_field_1_that_references_2, fieldName, typesFile, typesFilePath_1); + } + return typesFilePath_1; + } + else { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Expected_type_of_0_field_in_package_json_to_be_string_got_1, fieldName, typeof typesFile); + } + } + } } - return node.id; + var typesFilePath = tryReadFromField("typings") || tryReadFromField("types"); + if (typesFilePath) { + return typesFilePath; + } + if (state.compilerOptions.allowJs && jsonContent.main && typeof jsonContent.main === "string") { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.No_types_specified_in_package_json_but_allowJs_is_set_so_returning_main_value_of_0, jsonContent.main); + } + var mainFilePath = ts.normalizePath(ts.combinePaths(baseDirectory, jsonContent.main)); + return mainFilePath; + } + return undefined; } - ts.getNodeId = getNodeId; - function getSymbolId(symbol) { - if (!symbol.id) { - symbol.id = nextSymbolId; - nextSymbolId++; + function readJson(path, host) { + try { + var jsonText = host.readFile(path); + return jsonText ? JSON.parse(jsonText) : {}; + } + catch (e) { + return {}; } - return symbol.id; } - ts.getSymbolId = getSymbolId; - function createTypeChecker(host, produceDiagnostics) { - var cancellationToken; - var Symbol = ts.objectAllocator.getSymbolConstructor(); - var Type = ts.objectAllocator.getTypeConstructor(); - var Signature = ts.objectAllocator.getSignatureConstructor(); - var typeCount = 0; - var symbolCount = 0; - var emptyArray = []; - var emptySymbols = ts.createMap(); - var compilerOptions = host.getCompilerOptions(); - var languageVersion = compilerOptions.target || 0; - var modulekind = ts.getEmitModuleKind(compilerOptions); - var noUnusedIdentifiers = !!compilerOptions.noUnusedLocals || !!compilerOptions.noUnusedParameters; - var allowSyntheticDefaultImports = typeof compilerOptions.allowSyntheticDefaultImports !== "undefined" ? compilerOptions.allowSyntheticDefaultImports : modulekind === ts.ModuleKind.System; - var strictNullChecks = compilerOptions.strictNullChecks; - var emitResolver = createResolver(); - var undefinedSymbol = createSymbol(4 | 67108864, "undefined"); - undefinedSymbol.declarations = []; - var argumentsSymbol = createSymbol(4 | 67108864, "arguments"); - var checker = { - getNodeCount: function () { return ts.sum(host.getSourceFiles(), "nodeCount"); }, - getIdentifierCount: function () { return ts.sum(host.getSourceFiles(), "identifierCount"); }, - getSymbolCount: function () { return ts.sum(host.getSourceFiles(), "symbolCount") + symbolCount; }, - getTypeCount: function () { return typeCount; }, - isUndefinedSymbol: function (symbol) { return symbol === undefinedSymbol; }, - isArgumentsSymbol: function (symbol) { return symbol === argumentsSymbol; }, - isUnknownSymbol: function (symbol) { return symbol === unknownSymbol; }, - getDiagnostics: getDiagnostics, - getGlobalDiagnostics: getGlobalDiagnostics, - getTypeOfSymbolAtLocation: getTypeOfSymbolAtLocation, - getSymbolsOfParameterPropertyDeclaration: getSymbolsOfParameterPropertyDeclaration, - getDeclaredTypeOfSymbol: getDeclaredTypeOfSymbol, - getPropertiesOfType: getPropertiesOfType, - getPropertyOfType: getPropertyOfType, - getSignaturesOfType: getSignaturesOfType, - getIndexTypeOfType: getIndexTypeOfType, - getBaseTypes: getBaseTypes, - getReturnTypeOfSignature: getReturnTypeOfSignature, - getNonNullableType: getNonNullableType, - getSymbolsInScope: getSymbolsInScope, - getSymbolAtLocation: getSymbolAtLocation, - getShorthandAssignmentValueSymbol: getShorthandAssignmentValueSymbol, - getExportSpecifierLocalTargetSymbol: getExportSpecifierLocalTargetSymbol, - getTypeAtLocation: getTypeOfNode, - getPropertySymbolOfDestructuringAssignment: getPropertySymbolOfDestructuringAssignment, - typeToString: typeToString, - getSymbolDisplayBuilder: getSymbolDisplayBuilder, - symbolToString: symbolToString, - getAugmentedPropertiesOfType: getAugmentedPropertiesOfType, - getRootSymbols: getRootSymbols, - getContextualType: getContextualType, - getFullyQualifiedName: getFullyQualifiedName, - getResolvedSignature: getResolvedSignature, - getConstantValue: getConstantValue, - isValidPropertyAccess: isValidPropertyAccess, - getSignatureFromDeclaration: getSignatureFromDeclaration, - isImplementationOfOverload: isImplementationOfOverload, - getAliasedSymbol: resolveAlias, - getEmitResolver: getEmitResolver, - getExportsOfModule: getExportsOfModuleAsArray, - getAmbientModules: getAmbientModules, - getJsxElementAttributesType: getJsxElementAttributesType, - getJsxIntrinsicTagNames: getJsxIntrinsicTagNames, - isOptionalParameter: isOptionalParameter + var typeReferenceExtensions = [".d.ts"]; + function getEffectiveTypeRoots(options, host) { + if (options.typeRoots) { + return options.typeRoots; + } + var currentDirectory; + if (options.configFilePath) { + currentDirectory = ts.getDirectoryPath(options.configFilePath); + } + else if (host.getCurrentDirectory) { + currentDirectory = host.getCurrentDirectory(); + } + return currentDirectory !== undefined && getDefaultTypeRoots(currentDirectory, host); + } + ts.getEffectiveTypeRoots = getEffectiveTypeRoots; + function getDefaultTypeRoots(currentDirectory, host) { + if (!host.directoryExists) { + return [ts.combinePaths(currentDirectory, nodeModulesAtTypes)]; + } + var typeRoots; + while (true) { + var atTypes = ts.combinePaths(currentDirectory, nodeModulesAtTypes); + if (host.directoryExists(atTypes)) { + (typeRoots || (typeRoots = [])).push(atTypes); + } + var parent_7 = ts.getDirectoryPath(currentDirectory); + if (parent_7 === currentDirectory) { + break; + } + currentDirectory = parent_7; + } + return typeRoots; + } + var nodeModulesAtTypes = ts.combinePaths("node_modules", "@types"); + function resolveTypeReferenceDirective(typeReferenceDirectiveName, containingFile, options, host) { + var traceEnabled = isTraceEnabled(options, host); + var moduleResolutionState = { + compilerOptions: options, + host: host, + skipTsx: true, + traceEnabled: traceEnabled }; - var tupleTypes = []; - var unionTypes = ts.createMap(); - var intersectionTypes = ts.createMap(); - var stringLiteralTypes = ts.createMap(); - var numericLiteralTypes = ts.createMap(); - var unknownSymbol = createSymbol(4 | 67108864, "unknown"); - var resolvingSymbol = createSymbol(67108864, "__resolving__"); - var anyType = createIntrinsicType(1, "any"); - var unknownType = createIntrinsicType(1, "unknown"); - var undefinedType = createIntrinsicType(2048, "undefined"); - var undefinedWideningType = strictNullChecks ? undefinedType : createIntrinsicType(2048 | 33554432, "undefined"); - var nullType = createIntrinsicType(4096, "null"); - var nullWideningType = strictNullChecks ? nullType : createIntrinsicType(4096 | 33554432, "null"); - var stringType = createIntrinsicType(2, "string"); - var numberType = createIntrinsicType(4, "number"); - var trueType = createIntrinsicType(128, "true"); - var falseType = createIntrinsicType(128, "false"); - var booleanType = createBooleanType([trueType, falseType]); - var esSymbolType = createIntrinsicType(512, "symbol"); - var voidType = createIntrinsicType(1024, "void"); - var neverType = createIntrinsicType(8192, "never"); - var silentNeverType = createIntrinsicType(8192, "never"); - var emptyObjectType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); - var emptyGenericType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); - emptyGenericType.instantiations = ts.createMap(); - var anyFunctionType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); - anyFunctionType.flags |= 134217728; - var noConstraintType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); - var anySignature = createSignature(undefined, undefined, undefined, emptyArray, anyType, undefined, 0, false, false); - var unknownSignature = createSignature(undefined, undefined, undefined, emptyArray, unknownType, undefined, 0, false, false); - var resolvingSignature = createSignature(undefined, undefined, undefined, emptyArray, anyType, undefined, 0, false, false); - var silentNeverSignature = createSignature(undefined, undefined, undefined, emptyArray, silentNeverType, undefined, 0, false, false); - var enumNumberIndexInfo = createIndexInfo(stringType, true); - var globals = ts.createMap(); - var patternAmbientModules; - var getGlobalESSymbolConstructorSymbol; - var getGlobalPromiseConstructorSymbol; - var tryGetGlobalPromiseConstructorSymbol; - var globalObjectType; - var globalFunctionType; - var globalArrayType; - var globalReadonlyArrayType; - var globalStringType; - var globalNumberType; - var globalBooleanType; - var globalRegExpType; - var anyArrayType; - var anyReadonlyArrayType; - var getGlobalTemplateStringsArrayType; - var getGlobalESSymbolType; - var getGlobalIterableType; - var getGlobalIteratorType; - var getGlobalIterableIteratorType; - var getGlobalClassDecoratorType; - var getGlobalParameterDecoratorType; - var getGlobalPropertyDecoratorType; - var getGlobalMethodDecoratorType; - var getGlobalTypedPropertyDescriptorType; - var getGlobalPromiseType; - var tryGetGlobalPromiseType; - var getGlobalPromiseLikeType; - var getInstantiatedGlobalPromiseLikeType; - var getGlobalPromiseConstructorLikeType; - var getGlobalThenableType; - var jsxElementClassType; - var deferredNodes; - var deferredUnusedIdentifierNodes; - var flowLoopStart = 0; - var flowLoopCount = 0; - var visitedFlowCount = 0; - var emptyStringType = getLiteralTypeForText(32, ""); - var zeroType = getLiteralTypeForText(64, "0"); - var resolutionTargets = []; - var resolutionResults = []; - var resolutionPropertyNames = []; - var mergedSymbols = []; - var symbolLinks = []; - var nodeLinks = []; - var flowLoopCaches = []; - var flowLoopNodes = []; - var flowLoopKeys = []; - var flowLoopTypes = []; - var visitedFlowNodes = []; - var visitedFlowTypes = []; - var potentialThisCollisions = []; - var awaitedTypeStack = []; - var diagnostics = ts.createDiagnosticCollection(); - var typeofEQFacts = ts.createMap({ - "string": 1, + var typeRoots = getEffectiveTypeRoots(options, host); + if (traceEnabled) { + if (containingFile === undefined) { + if (typeRoots === undefined) { + trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_not_set_root_directory_not_set, typeReferenceDirectiveName); + } + else { + trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_not_set_root_directory_1, typeReferenceDirectiveName, typeRoots); + } + } + else { + if (typeRoots === undefined) { + trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_1_root_directory_not_set, typeReferenceDirectiveName, containingFile); + } + else { + trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_1_root_directory_2, typeReferenceDirectiveName, containingFile, typeRoots); + } + } + } + var failedLookupLocations = []; + if (typeRoots && typeRoots.length) { + if (traceEnabled) { + trace(host, ts.Diagnostics.Resolving_with_primary_search_path_0, typeRoots.join(", ")); + } + var primarySearchPaths = typeRoots; + for (var _i = 0, primarySearchPaths_1 = primarySearchPaths; _i < primarySearchPaths_1.length; _i++) { + var typeRoot = primarySearchPaths_1[_i]; + var candidate = ts.combinePaths(typeRoot, typeReferenceDirectiveName); + var candidateDirectory = ts.getDirectoryPath(candidate); + var resolvedFile_1 = loadNodeModuleFromDirectory(typeReferenceExtensions, candidate, failedLookupLocations, !directoryProbablyExists(candidateDirectory, host), moduleResolutionState); + if (resolvedFile_1) { + if (traceEnabled) { + trace(host, ts.Diagnostics.Type_reference_directive_0_was_successfully_resolved_to_1_primary_Colon_2, typeReferenceDirectiveName, resolvedFile_1, true); + } + return { + resolvedTypeReferenceDirective: { primary: true, resolvedFileName: resolvedFile_1 }, + failedLookupLocations: failedLookupLocations + }; + } + } + } + else { + if (traceEnabled) { + trace(host, ts.Diagnostics.Root_directory_cannot_be_determined_skipping_primary_search_paths); + } + } + var resolvedFile; + var initialLocationForSecondaryLookup; + if (containingFile) { + initialLocationForSecondaryLookup = ts.getDirectoryPath(containingFile); + } + if (initialLocationForSecondaryLookup !== undefined) { + if (traceEnabled) { + trace(host, ts.Diagnostics.Looking_up_in_node_modules_folder_initial_location_0, initialLocationForSecondaryLookup); + } + resolvedFile = loadModuleFromNodeModules(typeReferenceDirectiveName, initialLocationForSecondaryLookup, failedLookupLocations, moduleResolutionState, false); + if (traceEnabled) { + if (resolvedFile) { + trace(host, ts.Diagnostics.Type_reference_directive_0_was_successfully_resolved_to_1_primary_Colon_2, typeReferenceDirectiveName, resolvedFile, false); + } + else { + trace(host, ts.Diagnostics.Type_reference_directive_0_was_not_resolved, typeReferenceDirectiveName); + } + } + } + else { + if (traceEnabled) { + trace(host, ts.Diagnostics.Containing_file_is_not_specified_and_root_directory_cannot_be_determined_skipping_lookup_in_node_modules_folder); + } + } + return { + resolvedTypeReferenceDirective: resolvedFile + ? { primary: false, resolvedFileName: resolvedFile } + : undefined, + failedLookupLocations: failedLookupLocations + }; + } + ts.resolveTypeReferenceDirective = resolveTypeReferenceDirective; + function getAutomaticTypeDirectiveNames(options, host) { + if (options.types) { + return options.types; + } + var result = []; + if (host.directoryExists && host.getDirectories) { + var typeRoots = getEffectiveTypeRoots(options, host); + if (typeRoots) { + for (var _i = 0, typeRoots_1 = typeRoots; _i < typeRoots_1.length; _i++) { + var root = typeRoots_1[_i]; + if (host.directoryExists(root)) { + for (var _a = 0, _b = host.getDirectories(root); _a < _b.length; _a++) { + var typeDirectivePath = _b[_a]; + var normalized = ts.normalizePath(typeDirectivePath); + var packageJsonPath = pathToPackageJson(ts.combinePaths(root, normalized)); + var isNotNeededPackage = host.fileExists(packageJsonPath) && readJson(packageJsonPath, host).typings === null; + if (!isNotNeededPackage) { + result.push(ts.getBaseFileName(normalized)); + } + } + } + } + } + } + return result; + } + ts.getAutomaticTypeDirectiveNames = getAutomaticTypeDirectiveNames; + function resolveModuleName(moduleName, containingFile, compilerOptions, host) { + var traceEnabled = isTraceEnabled(compilerOptions, host); + if (traceEnabled) { + trace(host, ts.Diagnostics.Resolving_module_0_from_1, moduleName, containingFile); + } + var moduleResolution = compilerOptions.moduleResolution; + if (moduleResolution === undefined) { + moduleResolution = ts.getEmitModuleKind(compilerOptions) === ts.ModuleKind.CommonJS ? ts.ModuleResolutionKind.NodeJs : ts.ModuleResolutionKind.Classic; + if (traceEnabled) { + trace(host, ts.Diagnostics.Module_resolution_kind_is_not_specified_using_0, ts.ModuleResolutionKind[moduleResolution]); + } + } + else { + if (traceEnabled) { + trace(host, ts.Diagnostics.Explicitly_specified_module_resolution_kind_Colon_0, ts.ModuleResolutionKind[moduleResolution]); + } + } + var result; + switch (moduleResolution) { + case ts.ModuleResolutionKind.NodeJs: + result = nodeModuleNameResolver(moduleName, containingFile, compilerOptions, host); + break; + case ts.ModuleResolutionKind.Classic: + result = classicNameResolver(moduleName, containingFile, compilerOptions, host); + break; + } + if (traceEnabled) { + if (result.resolvedModule) { + trace(host, ts.Diagnostics.Module_name_0_was_successfully_resolved_to_1, moduleName, result.resolvedModule.resolvedFileName); + } + else { + trace(host, ts.Diagnostics.Module_name_0_was_not_resolved, moduleName); + } + } + return result; + } + ts.resolveModuleName = resolveModuleName; + function tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state) { + if (moduleHasNonRelativeName(moduleName)) { + return tryLoadModuleUsingBaseUrl(moduleName, loader, failedLookupLocations, supportedExtensions, state); + } + else { + return tryLoadModuleUsingRootDirs(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state); + } + } + function tryLoadModuleUsingRootDirs(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state) { + if (!state.compilerOptions.rootDirs) { + return undefined; + } + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.rootDirs_option_is_set_using_it_to_resolve_relative_module_name_0, moduleName); + } + var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); + var matchedRootDir; + var matchedNormalizedPrefix; + for (var _i = 0, _a = state.compilerOptions.rootDirs; _i < _a.length; _i++) { + var rootDir = _a[_i]; + var normalizedRoot = ts.normalizePath(rootDir); + if (!ts.endsWith(normalizedRoot, ts.directorySeparator)) { + normalizedRoot += ts.directorySeparator; + } + var isLongestMatchingPrefix = ts.startsWith(candidate, normalizedRoot) && + (matchedNormalizedPrefix === undefined || matchedNormalizedPrefix.length < normalizedRoot.length); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Checking_if_0_is_the_longest_matching_prefix_for_1_2, normalizedRoot, candidate, isLongestMatchingPrefix); + } + if (isLongestMatchingPrefix) { + matchedNormalizedPrefix = normalizedRoot; + matchedRootDir = rootDir; + } + } + if (matchedNormalizedPrefix) { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Longest_matching_prefix_for_0_is_1, candidate, matchedNormalizedPrefix); + } + var suffix = candidate.substr(matchedNormalizedPrefix.length); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Loading_0_from_the_root_dir_1_candidate_location_2, suffix, matchedNormalizedPrefix, candidate); + } + var resolvedFileName = loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(containingDirectory, state.host), state); + if (resolvedFileName) { + return resolvedFileName; + } + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Trying_other_entries_in_rootDirs); + } + for (var _b = 0, _c = state.compilerOptions.rootDirs; _b < _c.length; _b++) { + var rootDir = _c[_b]; + if (rootDir === matchedRootDir) { + continue; + } + var candidate_1 = ts.combinePaths(ts.normalizePath(rootDir), suffix); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Loading_0_from_the_root_dir_1_candidate_location_2, suffix, rootDir, candidate_1); + } + var baseDirectory = ts.getDirectoryPath(candidate_1); + var resolvedFileName_1 = loader(candidate_1, supportedExtensions, failedLookupLocations, !directoryProbablyExists(baseDirectory, state.host), state); + if (resolvedFileName_1) { + return resolvedFileName_1; + } + } + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Module_resolution_using_rootDirs_has_failed); + } + } + return undefined; + } + function tryLoadModuleUsingBaseUrl(moduleName, loader, failedLookupLocations, supportedExtensions, state) { + if (!state.compilerOptions.baseUrl) { + return undefined; + } + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.baseUrl_option_is_set_to_0_using_this_value_to_resolve_non_relative_module_name_1, state.compilerOptions.baseUrl, moduleName); + } + var matchedPattern = undefined; + if (state.compilerOptions.paths) { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.paths_option_is_specified_looking_for_a_pattern_to_match_module_name_0, moduleName); + } + matchedPattern = ts.matchPatternOrExact(ts.getOwnKeys(state.compilerOptions.paths), moduleName); + } + if (matchedPattern) { + var matchedStar = typeof matchedPattern === "string" ? undefined : ts.matchedText(matchedPattern, moduleName); + var matchedPatternText = typeof matchedPattern === "string" ? matchedPattern : ts.patternText(matchedPattern); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Module_name_0_matched_pattern_1, moduleName, matchedPatternText); + } + for (var _i = 0, _a = state.compilerOptions.paths[matchedPatternText]; _i < _a.length; _i++) { + var subst = _a[_i]; + var path = matchedStar ? subst.replace("*", matchedStar) : subst; + var candidate = ts.normalizePath(ts.combinePaths(state.compilerOptions.baseUrl, path)); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Trying_substitution_0_candidate_module_location_Colon_1, subst, path); + } + var resolvedFileName = loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(ts.getDirectoryPath(candidate), state.host), state); + if (resolvedFileName) { + return resolvedFileName; + } + } + return undefined; + } + else { + var candidate = ts.normalizePath(ts.combinePaths(state.compilerOptions.baseUrl, moduleName)); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Resolving_module_name_0_relative_to_base_url_1_2, moduleName, state.compilerOptions.baseUrl, candidate); + } + return loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(ts.getDirectoryPath(candidate), state.host), state); + } + } + function nodeModuleNameResolver(moduleName, containingFile, compilerOptions, host) { + var containingDirectory = ts.getDirectoryPath(containingFile); + var supportedExtensions = ts.getSupportedExtensions(compilerOptions); + var traceEnabled = isTraceEnabled(compilerOptions, host); + var failedLookupLocations = []; + var state = { compilerOptions: compilerOptions, host: host, traceEnabled: traceEnabled, skipTsx: false }; + var resolvedFileName = tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, nodeLoadModuleByRelativeName, failedLookupLocations, supportedExtensions, state); + var isExternalLibraryImport = false; + if (!resolvedFileName) { + if (moduleHasNonRelativeName(moduleName)) { + if (traceEnabled) { + trace(host, ts.Diagnostics.Loading_module_0_from_node_modules_folder, moduleName); + } + resolvedFileName = loadModuleFromNodeModules(moduleName, containingDirectory, failedLookupLocations, state, false); + isExternalLibraryImport = resolvedFileName !== undefined; + } + else { + var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); + resolvedFileName = nodeLoadModuleByRelativeName(candidate, supportedExtensions, failedLookupLocations, false, state); + } + } + if (resolvedFileName && host.realpath) { + var originalFileName = resolvedFileName; + resolvedFileName = ts.normalizePath(host.realpath(resolvedFileName)); + if (traceEnabled) { + trace(host, ts.Diagnostics.Resolving_real_path_for_0_result_1, originalFileName, resolvedFileName); + } + } + return createResolvedModule(resolvedFileName, isExternalLibraryImport, failedLookupLocations); + } + ts.nodeModuleNameResolver = nodeModuleNameResolver; + function nodeLoadModuleByRelativeName(candidate, supportedExtensions, failedLookupLocations, onlyRecordFailures, state) { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Loading_module_as_file_Slash_folder_candidate_module_location_0, candidate); + } + var resolvedFileName = !ts.pathEndsWithDirectorySeparator(candidate) && loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, onlyRecordFailures, state); + return resolvedFileName || loadNodeModuleFromDirectory(supportedExtensions, candidate, failedLookupLocations, onlyRecordFailures, state); + } + function directoryProbablyExists(directoryName, host) { + return !host.directoryExists || host.directoryExists(directoryName); + } + ts.directoryProbablyExists = directoryProbablyExists; + function loadModuleFromFile(candidate, extensions, failedLookupLocation, onlyRecordFailures, state) { + var resolvedByAddingExtension = tryAddingExtensions(candidate, extensions, failedLookupLocation, onlyRecordFailures, state); + if (resolvedByAddingExtension) { + return resolvedByAddingExtension; + } + if (ts.hasJavaScriptFileExtension(candidate)) { + var extensionless = ts.removeFileExtension(candidate); + if (state.traceEnabled) { + var extension = candidate.substring(extensionless.length); + trace(state.host, ts.Diagnostics.File_name_0_has_a_1_extension_stripping_it, candidate, extension); + } + return tryAddingExtensions(extensionless, extensions, failedLookupLocation, onlyRecordFailures, state); + } + } + function tryAddingExtensions(candidate, extensions, failedLookupLocation, onlyRecordFailures, state) { + if (!onlyRecordFailures) { + var directory = ts.getDirectoryPath(candidate); + if (directory) { + onlyRecordFailures = !directoryProbablyExists(directory, state.host); + } + } + return ts.forEach(extensions, function (ext) { + return !(state.skipTsx && ts.isJsxOrTsxExtension(ext)) && tryFile(candidate + ext, failedLookupLocation, onlyRecordFailures, state); + }); + } + function tryFile(fileName, failedLookupLocation, onlyRecordFailures, state) { + if (!onlyRecordFailures && state.host.fileExists(fileName)) { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.File_0_exist_use_it_as_a_name_resolution_result, fileName); + } + return fileName; + } + else { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.File_0_does_not_exist, fileName); + } + failedLookupLocation.push(fileName); + return undefined; + } + } + function loadNodeModuleFromDirectory(extensions, candidate, failedLookupLocation, onlyRecordFailures, state) { + var packageJsonPath = pathToPackageJson(candidate); + var directoryExists = !onlyRecordFailures && directoryProbablyExists(candidate, state.host); + if (directoryExists && state.host.fileExists(packageJsonPath)) { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Found_package_json_at_0, packageJsonPath); + } + var typesFile = tryReadTypesSection(packageJsonPath, candidate, state); + if (typesFile) { + var onlyRecordFailures_1 = !directoryProbablyExists(ts.getDirectoryPath(typesFile), state.host); + var result = tryFile(typesFile, failedLookupLocation, onlyRecordFailures_1, state) || + tryAddingExtensions(typesFile, extensions, failedLookupLocation, onlyRecordFailures_1, state); + if (result) { + return result; + } + } + else { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.package_json_does_not_have_types_field); + } + } + } + else { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.File_0_does_not_exist, packageJsonPath); + } + failedLookupLocation.push(packageJsonPath); + } + return loadModuleFromFile(ts.combinePaths(candidate, "index"), extensions, failedLookupLocation, !directoryExists, state); + } + function pathToPackageJson(directory) { + return ts.combinePaths(directory, "package.json"); + } + function loadModuleFromNodeModulesFolder(moduleName, directory, failedLookupLocations, state) { + var nodeModulesFolder = ts.combinePaths(directory, "node_modules"); + var nodeModulesFolderExists = directoryProbablyExists(nodeModulesFolder, state.host); + var candidate = ts.normalizePath(ts.combinePaths(nodeModulesFolder, moduleName)); + var supportedExtensions = ts.getSupportedExtensions(state.compilerOptions); + var result = loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, !nodeModulesFolderExists, state); + if (result) { + return result; + } + result = loadNodeModuleFromDirectory(supportedExtensions, candidate, failedLookupLocations, !nodeModulesFolderExists, state); + if (result) { + return result; + } + } + function loadModuleFromNodeModules(moduleName, directory, failedLookupLocations, state, checkOneLevel) { + return loadModuleFromNodeModulesWorker(moduleName, directory, failedLookupLocations, state, checkOneLevel, false); + } + ts.loadModuleFromNodeModules = loadModuleFromNodeModules; + function loadModuleFromNodeModulesAtTypes(moduleName, directory, failedLookupLocations, state) { + return loadModuleFromNodeModulesWorker(moduleName, directory, failedLookupLocations, state, false, true); + } + function loadModuleFromNodeModulesWorker(moduleName, directory, failedLookupLocations, state, checkOneLevel, typesOnly) { + directory = ts.normalizeSlashes(directory); + while (true) { + var baseName = ts.getBaseFileName(directory); + if (baseName !== "node_modules") { + var packageResult = void 0; + if (!typesOnly) { + packageResult = loadModuleFromNodeModulesFolder(moduleName, directory, failedLookupLocations, state); + if (packageResult && ts.hasTypeScriptFileExtension(packageResult)) { + return packageResult; + } + } + var typesResult = loadModuleFromNodeModulesFolder(ts.combinePaths("@types", moduleName), directory, failedLookupLocations, state); + if (typesResult || packageResult) { + return typesResult || packageResult; + } + } + var parentPath = ts.getDirectoryPath(directory); + if (parentPath === directory || checkOneLevel) { + break; + } + directory = parentPath; + } + return undefined; + } + function classicNameResolver(moduleName, containingFile, compilerOptions, host) { + var traceEnabled = isTraceEnabled(compilerOptions, host); + var state = { compilerOptions: compilerOptions, host: host, traceEnabled: traceEnabled, skipTsx: !compilerOptions.jsx }; + var failedLookupLocations = []; + var supportedExtensions = ts.getSupportedExtensions(compilerOptions); + var containingDirectory = ts.getDirectoryPath(containingFile); + var resolvedFileName = tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, loadModuleFromFile, failedLookupLocations, supportedExtensions, state); + if (resolvedFileName) { + return createResolvedModule(resolvedFileName, false, failedLookupLocations); + } + var referencedSourceFile; + if (moduleHasNonRelativeName(moduleName)) { + referencedSourceFile = referencedSourceFile = loadModuleFromAncestorDirectories(moduleName, containingDirectory, supportedExtensions, failedLookupLocations, state) || + loadModuleFromNodeModulesAtTypes(moduleName, containingDirectory, failedLookupLocations, state); + } + else { + var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); + referencedSourceFile = loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, false, state); + } + return referencedSourceFile + ? { resolvedModule: { resolvedFileName: referencedSourceFile }, failedLookupLocations: failedLookupLocations } + : { resolvedModule: undefined, failedLookupLocations: failedLookupLocations }; + } + ts.classicNameResolver = classicNameResolver; + function loadModuleFromAncestorDirectories(moduleName, containingDirectory, supportedExtensions, failedLookupLocations, state) { + while (true) { + var searchName = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); + var referencedSourceFile = loadModuleFromFile(searchName, supportedExtensions, failedLookupLocations, false, state); + if (referencedSourceFile) { + return referencedSourceFile; + } + var parentPath = ts.getDirectoryPath(containingDirectory); + if (parentPath === containingDirectory) { + return undefined; + } + containingDirectory = parentPath; + } + } +})(ts || (ts = {})); +var ts; +(function (ts) { + var ambientModuleSymbolRegex = /^".+"$/; + var nextSymbolId = 1; + var nextNodeId = 1; + var nextMergeId = 1; + var nextFlowId = 1; + function getNodeId(node) { + if (!node.id) { + node.id = nextNodeId; + nextNodeId++; + } + return node.id; + } + ts.getNodeId = getNodeId; + function getSymbolId(symbol) { + if (!symbol.id) { + symbol.id = nextSymbolId; + nextSymbolId++; + } + return symbol.id; + } + ts.getSymbolId = getSymbolId; + function createTypeChecker(host, produceDiagnostics) { + var cancellationToken; + var Symbol = ts.objectAllocator.getSymbolConstructor(); + var Type = ts.objectAllocator.getTypeConstructor(); + var Signature = ts.objectAllocator.getSignatureConstructor(); + var typeCount = 0; + var symbolCount = 0; + var emptyArray = []; + var emptySymbols = ts.createMap(); + var compilerOptions = host.getCompilerOptions(); + var languageVersion = compilerOptions.target || 0; + var modulekind = ts.getEmitModuleKind(compilerOptions); + var noUnusedIdentifiers = !!compilerOptions.noUnusedLocals || !!compilerOptions.noUnusedParameters; + var allowSyntheticDefaultImports = typeof compilerOptions.allowSyntheticDefaultImports !== "undefined" ? compilerOptions.allowSyntheticDefaultImports : modulekind === ts.ModuleKind.System; + var strictNullChecks = compilerOptions.strictNullChecks; + var emitResolver = createResolver(); + var undefinedSymbol = createSymbol(4 | 67108864, "undefined"); + undefinedSymbol.declarations = []; + var argumentsSymbol = createSymbol(4 | 67108864, "arguments"); + var checker = { + getNodeCount: function () { return ts.sum(host.getSourceFiles(), "nodeCount"); }, + getIdentifierCount: function () { return ts.sum(host.getSourceFiles(), "identifierCount"); }, + getSymbolCount: function () { return ts.sum(host.getSourceFiles(), "symbolCount") + symbolCount; }, + getTypeCount: function () { return typeCount; }, + isUndefinedSymbol: function (symbol) { return symbol === undefinedSymbol; }, + isArgumentsSymbol: function (symbol) { return symbol === argumentsSymbol; }, + isUnknownSymbol: function (symbol) { return symbol === unknownSymbol; }, + getDiagnostics: getDiagnostics, + getGlobalDiagnostics: getGlobalDiagnostics, + getTypeOfSymbolAtLocation: getTypeOfSymbolAtLocation, + getSymbolsOfParameterPropertyDeclaration: getSymbolsOfParameterPropertyDeclaration, + getDeclaredTypeOfSymbol: getDeclaredTypeOfSymbol, + getPropertiesOfType: getPropertiesOfType, + getPropertyOfType: getPropertyOfType, + getSignaturesOfType: getSignaturesOfType, + getIndexTypeOfType: getIndexTypeOfType, + getBaseTypes: getBaseTypes, + getReturnTypeOfSignature: getReturnTypeOfSignature, + getNonNullableType: getNonNullableType, + getSymbolsInScope: getSymbolsInScope, + getSymbolAtLocation: getSymbolAtLocation, + getShorthandAssignmentValueSymbol: getShorthandAssignmentValueSymbol, + getExportSpecifierLocalTargetSymbol: getExportSpecifierLocalTargetSymbol, + getTypeAtLocation: getTypeOfNode, + getPropertySymbolOfDestructuringAssignment: getPropertySymbolOfDestructuringAssignment, + typeToString: typeToString, + getSymbolDisplayBuilder: getSymbolDisplayBuilder, + symbolToString: symbolToString, + getAugmentedPropertiesOfType: getAugmentedPropertiesOfType, + getRootSymbols: getRootSymbols, + getContextualType: getContextualType, + getFullyQualifiedName: getFullyQualifiedName, + getResolvedSignature: getResolvedSignature, + getConstantValue: getConstantValue, + isValidPropertyAccess: isValidPropertyAccess, + getSignatureFromDeclaration: getSignatureFromDeclaration, + isImplementationOfOverload: isImplementationOfOverload, + getAliasedSymbol: resolveAlias, + getEmitResolver: getEmitResolver, + getExportsOfModule: getExportsOfModuleAsArray, + getAmbientModules: getAmbientModules, + getJsxElementAttributesType: getJsxElementAttributesType, + getJsxIntrinsicTagNames: getJsxIntrinsicTagNames, + isOptionalParameter: isOptionalParameter + }; + var tupleTypes = []; + var unionTypes = ts.createMap(); + var intersectionTypes = ts.createMap(); + var stringLiteralTypes = ts.createMap(); + var numericLiteralTypes = ts.createMap(); + var unknownSymbol = createSymbol(4 | 67108864, "unknown"); + var resolvingSymbol = createSymbol(67108864, "__resolving__"); + var anyType = createIntrinsicType(1, "any"); + var autoType = createIntrinsicType(1, "any"); + var unknownType = createIntrinsicType(1, "unknown"); + var undefinedType = createIntrinsicType(2048, "undefined"); + var undefinedWideningType = strictNullChecks ? undefinedType : createIntrinsicType(2048 | 33554432, "undefined"); + var nullType = createIntrinsicType(4096, "null"); + var nullWideningType = strictNullChecks ? nullType : createIntrinsicType(4096 | 33554432, "null"); + var stringType = createIntrinsicType(2, "string"); + var numberType = createIntrinsicType(4, "number"); + var trueType = createIntrinsicType(128, "true"); + var falseType = createIntrinsicType(128, "false"); + var booleanType = createBooleanType([trueType, falseType]); + var esSymbolType = createIntrinsicType(512, "symbol"); + var voidType = createIntrinsicType(1024, "void"); + var neverType = createIntrinsicType(8192, "never"); + var silentNeverType = createIntrinsicType(8192, "never"); + var emptyObjectType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); + var emptyGenericType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); + emptyGenericType.instantiations = ts.createMap(); + var anyFunctionType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); + anyFunctionType.flags |= 134217728; + var noConstraintType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); + var anySignature = createSignature(undefined, undefined, undefined, emptyArray, anyType, undefined, 0, false, false); + var unknownSignature = createSignature(undefined, undefined, undefined, emptyArray, unknownType, undefined, 0, false, false); + var resolvingSignature = createSignature(undefined, undefined, undefined, emptyArray, anyType, undefined, 0, false, false); + var silentNeverSignature = createSignature(undefined, undefined, undefined, emptyArray, silentNeverType, undefined, 0, false, false); + var enumNumberIndexInfo = createIndexInfo(stringType, true); + var globals = ts.createMap(); + var patternAmbientModules; + var getGlobalESSymbolConstructorSymbol; + var getGlobalPromiseConstructorSymbol; + var tryGetGlobalPromiseConstructorSymbol; + var globalObjectType; + var globalFunctionType; + var globalArrayType; + var globalReadonlyArrayType; + var globalStringType; + var globalNumberType; + var globalBooleanType; + var globalRegExpType; + var anyArrayType; + var anyReadonlyArrayType; + var getGlobalTemplateStringsArrayType; + var getGlobalESSymbolType; + var getGlobalIterableType; + var getGlobalIteratorType; + var getGlobalIterableIteratorType; + var getGlobalClassDecoratorType; + var getGlobalParameterDecoratorType; + var getGlobalPropertyDecoratorType; + var getGlobalMethodDecoratorType; + var getGlobalTypedPropertyDescriptorType; + var getGlobalPromiseType; + var tryGetGlobalPromiseType; + var getGlobalPromiseLikeType; + var getInstantiatedGlobalPromiseLikeType; + var getGlobalPromiseConstructorLikeType; + var getGlobalThenableType; + var jsxElementClassType; + var deferredNodes; + var deferredUnusedIdentifierNodes; + var flowLoopStart = 0; + var flowLoopCount = 0; + var visitedFlowCount = 0; + var emptyStringType = getLiteralTypeForText(32, ""); + var zeroType = getLiteralTypeForText(64, "0"); + var resolutionTargets = []; + var resolutionResults = []; + var resolutionPropertyNames = []; + var mergedSymbols = []; + var symbolLinks = []; + var nodeLinks = []; + var flowLoopCaches = []; + var flowLoopNodes = []; + var flowLoopKeys = []; + var flowLoopTypes = []; + var visitedFlowNodes = []; + var visitedFlowTypes = []; + var potentialThisCollisions = []; + var awaitedTypeStack = []; + var diagnostics = ts.createDiagnosticCollection(); + var typeofEQFacts = ts.createMap({ + "string": 1, "number": 2, "boolean": 4, "symbol": 8, @@ -18108,7 +18983,7 @@ var ts; if (result && isInExternalModule && (meaning & 107455) === 107455) { var decls = result.declarations; if (decls && decls.length === 1 && decls[0].kind === 228) { - error(errorLocation, ts.Diagnostics.Identifier_0_must_be_imported_from_a_module, name); + error(errorLocation, ts.Diagnostics._0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead, name); } } } @@ -18526,6 +19401,7 @@ var ts; } function getExportsForModule(moduleSymbol) { var visitedSymbols = []; + moduleSymbol = resolveExternalModuleSymbol(moduleSymbol); return visit(moduleSymbol) || moduleSymbol.exports; function visit(symbol) { if (!(symbol && symbol.flags & 1952 && !ts.contains(visitedSymbols, symbol))) { @@ -19010,9 +19886,9 @@ var ts; var accessibleSymbolChain = getAccessibleSymbolChain(symbol, enclosingDeclaration, meaning, !!(flags & 2)); if (!accessibleSymbolChain || needsQualification(accessibleSymbolChain[0], enclosingDeclaration, accessibleSymbolChain.length === 1 ? meaning : getQualifiedLeftMeaning(meaning))) { - var parent_7 = getParentOfSymbol(accessibleSymbolChain ? accessibleSymbolChain[0] : symbol); - if (parent_7) { - walkSymbol(parent_7, getQualifiedLeftMeaning(meaning), false); + var parent_8 = getParentOfSymbol(accessibleSymbolChain ? accessibleSymbolChain[0] : symbol); + if (parent_8) { + walkSymbol(parent_8, getQualifiedLeftMeaning(meaning), false); } } if (accessibleSymbolChain) { @@ -19047,7 +19923,7 @@ var ts; ? "any" : type.intrinsicName); } - else if (type.flags & 268435456) { + else if (type.flags & 16384 && type.isThisType) { if (inObjectTypeLiteral) { writer.reportInaccessibleThisError(); } @@ -19064,7 +19940,7 @@ var ts; else if (type.flags & (32768 | 65536 | 16 | 16384)) { buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, 793064, 0, nextFlags); } - else if (!(flags & 512) && type.flags & (2097152 | 1572864) && type.aliasSymbol && + else if (!(flags & 512) && ((type.flags & 2097152 && !type.target) || type.flags & 1572864) && type.aliasSymbol && isSymbolAccessible(type.aliasSymbol, enclosingDeclaration, 793064, false).accessibility === 0) { var typeArguments = type.aliasTypeArguments; writeSymbolTypeReference(type.aliasSymbol, typeArguments, 0, typeArguments ? typeArguments.length : 0, nextFlags); @@ -19134,12 +20010,12 @@ var ts; var length_1 = outerTypeParameters.length; while (i < length_1) { var start = i; - var parent_8 = getParentSymbolOfTypeParameter(outerTypeParameters[i]); + var parent_9 = getParentSymbolOfTypeParameter(outerTypeParameters[i]); do { i++; - } while (i < length_1 && getParentSymbolOfTypeParameter(outerTypeParameters[i]) === parent_8); + } while (i < length_1 && getParentSymbolOfTypeParameter(outerTypeParameters[i]) === parent_9); if (!ts.rangeEquals(outerTypeParameters, typeArguments, start, i)) { - writeSymbolTypeReference(parent_8, typeArguments, start, i, flags); + writeSymbolTypeReference(parent_9, typeArguments, start, i, flags); writePunctuation(writer, 21); } } @@ -19528,12 +20404,12 @@ var ts; if (ts.isExternalModuleAugmentation(node)) { return true; } - var parent_9 = getDeclarationContainer(node); + var parent_10 = getDeclarationContainer(node); if (!(ts.getCombinedModifierFlags(node) & 1) && - !(node.kind !== 229 && parent_9.kind !== 256 && ts.isInAmbientContext(parent_9))) { - return isGlobalSourceFile(parent_9); + !(node.kind !== 229 && parent_10.kind !== 256 && ts.isInAmbientContext(parent_10))) { + return isGlobalSourceFile(parent_10); } - return isDeclarationVisible(parent_9); + return isDeclarationVisible(parent_10); case 145: case 144: case 149: @@ -19791,6 +20667,10 @@ var ts; } return undefined; } + function isAutoVariableInitializer(initializer) { + var expr = initializer && ts.skipParentheses(initializer); + return !expr || expr.kind === 93 || expr.kind === 69 && getResolvedSymbol(expr) === undefinedSymbol; + } function addOptionality(type, optional) { return strictNullChecks && optional ? includeFalsyTypes(type, 2048) : type; } @@ -19813,6 +20693,11 @@ var ts; if (declaration.type) { return addOptionality(getTypeFromTypeNode(declaration.type), declaration.questionToken && includeOptionality); } + if (declaration.kind === 218 && !ts.isBindingPattern(declaration.name) && + !(ts.getCombinedNodeFlags(declaration) & 2) && !(ts.getCombinedModifierFlags(declaration) & 1) && + !ts.isInAmbientContext(declaration) && isAutoVariableInitializer(declaration.initializer)) { + return autoType; + } if (declaration.kind === 142) { var func = declaration.parent; if (func.kind === 150 && !ts.hasDynamicName(func)) { @@ -19884,7 +20769,7 @@ var ts; result.pattern = pattern; } if (hasComputedProperties) { - result.flags |= 536870912; + result.isObjectLiteralPatternWithComputedProperties = true; } return result; } @@ -20353,7 +21238,8 @@ var ts; type.instantiations[getTypeListId(type.typeParameters)] = type; type.target = type; type.typeArguments = type.typeParameters; - type.thisType = createType(16384 | 268435456); + type.thisType = createType(16384); + type.thisType.isThisType = true; type.thisType.symbol = symbol; type.thisType.constraint = type; } @@ -20886,13 +21772,24 @@ var ts; var current = _a[_i]; for (var _b = 0, _c = getPropertiesOfType(current); _b < _c.length; _b++) { var prop = _c[_b]; - getPropertyOfUnionOrIntersectionType(type, prop.name); + getUnionOrIntersectionProperty(type, prop.name); } if (type.flags & 524288) { break; } } - return type.resolvedProperties ? symbolsToArray(type.resolvedProperties) : emptyArray; + var props = type.resolvedProperties; + if (props) { + var result = []; + for (var key in props) { + var prop = props[key]; + if (!(prop.flags & 268435456 && prop.isPartial)) { + result.push(prop); + } + } + return result; + } + return emptyArray; } function getPropertiesOfType(type) { type = getApparentType(type); @@ -20931,6 +21828,7 @@ var ts; var props; var commonFlags = (containingType.flags & 1048576) ? 536870912 : 0; var isReadonly = false; + var isPartial = false; for (var _i = 0, types_2 = types; _i < types_2.length; _i++) { var current = types_2[_i]; var type = getApparentType(current); @@ -20949,20 +21847,20 @@ var ts; } } else if (containingType.flags & 524288) { - return undefined; + isPartial = true; } } } if (!props) { return undefined; } - if (props.length === 1) { + if (props.length === 1 && !isPartial) { return props[0]; } var propTypes = []; var declarations = []; var commonType = undefined; - var hasCommonType = true; + var hasNonUniformType = false; for (var _a = 0, props_1 = props; _a < props_1.length; _a++) { var prop = props_1[_a]; if (prop.declarations) { @@ -20973,22 +21871,20 @@ var ts; commonType = type; } else if (type !== commonType) { - hasCommonType = false; + hasNonUniformType = true; } - propTypes.push(getTypeOfSymbol(prop)); + propTypes.push(type); } - var result = createSymbol(4 | - 67108864 | - 268435456 | - commonFlags, name); + var result = createSymbol(4 | 67108864 | 268435456 | commonFlags, name); result.containingType = containingType; - result.hasCommonType = hasCommonType; + result.hasNonUniformType = hasNonUniformType; + result.isPartial = isPartial; result.declarations = declarations; result.isReadonly = isReadonly; result.type = containingType.flags & 524288 ? getUnionType(propTypes) : getIntersectionType(propTypes); return result; } - function getPropertyOfUnionOrIntersectionType(type, name) { + function getUnionOrIntersectionProperty(type, name) { var properties = type.resolvedProperties || (type.resolvedProperties = ts.createMap()); var property = properties[name]; if (!property) { @@ -20999,6 +21895,10 @@ var ts; } return property; } + function getPropertyOfUnionOrIntersectionType(type, name) { + var property = getUnionOrIntersectionProperty(type, name); + return property && !(property.flags & 268435456 && property.isPartial) ? property : undefined; + } function getPropertyOfType(type, name) { type = getApparentType(type); if (type.flags & 2588672) { @@ -21371,7 +22271,7 @@ var ts; } function hasConstraintReferenceTo(type, target) { var checked; - while (type && !(type.flags & 268435456) && type.flags & 16384 && !ts.contains(checked, type)) { + while (type && type.flags & 16384 && !(type.isThisType) && !ts.contains(checked, type)) { if (type === target) { return true; } @@ -21655,7 +22555,8 @@ var ts; type.instantiations[getTypeListId(type.typeParameters)] = type; type.target = type; type.typeArguments = type.typeParameters; - type.thisType = createType(16384 | 268435456); + type.thisType = createType(16384); + type.thisType.isThisType = true; type.thisType.constraint = type; type.declaredProperties = properties; type.declaredCallSignatures = emptyArray; @@ -21754,7 +22655,24 @@ var ts; } return false; } + function isSetOfLiteralsFromSameEnum(types) { + var first = types[0]; + if (first.flags & 256) { + var firstEnum = getParentOfSymbol(first.symbol); + for (var i = 1; i < types.length; i++) { + var other = types[i]; + if (!(other.flags & 256) || (firstEnum !== getParentOfSymbol(other.symbol))) { + return false; + } + } + return true; + } + return false; + } function removeSubtypes(types) { + if (types.length === 0 || isSetOfLiteralsFromSameEnum(types)) { + return; + } var i = types.length; while (i > 0) { i--; @@ -22729,7 +23647,8 @@ var ts; !t.numberIndexInfo; } function hasExcessProperties(source, target, reportErrors) { - if (!(target.flags & 536870912) && maybeTypeOfKind(target, 2588672)) { + if (maybeTypeOfKind(target, 2588672) && + (!(target.flags & 2588672) || !target.isObjectLiteralPatternWithComputedProperties)) { for (var _i = 0, _a = getPropertiesOfObjectType(source); _i < _a.length; _i++) { var prop = _a[_i]; if (!isKnownProperty(target, prop.name)) { @@ -23934,17 +24853,10 @@ var ts; } function isDiscriminantProperty(type, name) { if (type && type.flags & 524288) { - var prop = getPropertyOfType(type, name); - if (!prop) { - var filteredType = getTypeWithFacts(type, 4194304); - if (filteredType !== type && filteredType.flags & 524288) { - prop = getPropertyOfType(filteredType, name); - } - } + var prop = getUnionOrIntersectionProperty(type, name); if (prop && prop.flags & 268435456) { if (prop.isDiscriminantProperty === undefined) { - prop.isDiscriminantProperty = !prop.hasCommonType && - isLiteralType(getTypeOfSymbol(prop)); + prop.isDiscriminantProperty = prop.hasNonUniformType && isLiteralType(getTypeOfSymbol(prop)); } return prop.isDiscriminantProperty; } @@ -24221,6 +25133,23 @@ var ts; } return f(type) ? type : neverType; } + function mapType(type, f) { + return type.flags & 524288 ? getUnionType(ts.map(type.types, f)) : f(type); + } + function extractTypesOfKind(type, kind) { + return filterType(type, function (t) { return (t.flags & kind) !== 0; }); + } + function replacePrimitivesWithLiterals(typeWithPrimitives, typeWithLiterals) { + if (isTypeSubsetOf(stringType, typeWithPrimitives) && maybeTypeOfKind(typeWithLiterals, 32) || + isTypeSubsetOf(numberType, typeWithPrimitives) && maybeTypeOfKind(typeWithLiterals, 64)) { + return mapType(typeWithPrimitives, function (t) { + return t.flags & 2 ? extractTypesOfKind(typeWithLiterals, 2 | 32) : + t.flags & 4 ? extractTypesOfKind(typeWithLiterals, 4 | 64) : + t; + }); + } + return typeWithPrimitives; + } function isIncomplete(flowType) { return flowType.flags === 0; } @@ -24235,7 +25164,9 @@ var ts; if (!reference.flowNode || assumeInitialized && !(declaredType.flags & 4178943)) { return declaredType; } - var initialType = assumeInitialized ? declaredType : includeFalsyTypes(declaredType, 2048); + var initialType = assumeInitialized ? declaredType : + declaredType === autoType ? undefinedType : + includeFalsyTypes(declaredType, 2048); var visitedFlowStart = visitedFlowCount; var result = getTypeFromFlowType(getTypeAtFlowNode(reference.flowNode)); visitedFlowCount = visitedFlowStart; @@ -24297,10 +25228,13 @@ var ts; function getTypeAtFlowAssignment(flow) { var node = flow.node; if (isMatchingReference(reference, node)) { - var isIncrementOrDecrement = node.parent.kind === 185 || node.parent.kind === 186; - return declaredType.flags & 524288 && !isIncrementOrDecrement ? - getAssignmentReducedType(declaredType, getInitialOrAssignedType(node)) : - declaredType; + if (node.parent.kind === 185 || node.parent.kind === 186) { + var flowType = getTypeAtFlowNode(flow.antecedent); + return createFlowType(getBaseTypeOfLiteralType(getTypeFromFlowType(flowType)), isIncomplete(flowType)); + } + return declaredType === autoType ? getBaseTypeOfLiteralType(getInitialOrAssignedType(node)) : + declaredType.flags & 524288 ? getAssignmentReducedType(declaredType, getInitialOrAssignedType(node)) : + declaredType; } if (containsMatchingReference(reference, node)) { return declaredType; @@ -24486,12 +25420,12 @@ var ts; assumeTrue ? 16384 : 131072; return getTypeWithFacts(type, facts); } - if (type.flags & 2589191) { + if (type.flags & 2589185) { return type; } if (assumeTrue) { var narrowedType = filterType(type, function (t) { return areTypesComparable(t, valueType); }); - return narrowedType.flags & 8192 ? type : narrowedType; + return narrowedType.flags & 8192 ? type : replacePrimitivesWithLiterals(narrowedType, valueType); } if (isUnitType(valueType)) { var regularType_1 = getRegularTypeOfLiteralType(valueType); @@ -24529,7 +25463,8 @@ var ts; var clauseTypes = switchTypes.slice(clauseStart, clauseEnd); var hasDefaultClause = clauseStart === clauseEnd || ts.contains(clauseTypes, neverType); var discriminantType = getUnionType(clauseTypes); - var caseType = discriminantType.flags & 8192 ? neverType : filterType(type, function (t) { return isTypeComparableTo(discriminantType, t); }); + var caseType = discriminantType.flags & 8192 ? neverType : + replacePrimitivesWithLiterals(filterType(type, function (t) { return isTypeComparableTo(discriminantType, t); }), discriminantType); if (!hasDefaultClause) { return caseType; } @@ -24656,7 +25591,7 @@ var ts; if (ts.isRightSideOfQualifiedNameOrPropertyAccess(location)) { location = location.parent; } - if (ts.isExpression(location) && !ts.isAssignmentTarget(location)) { + if (ts.isPartOfExpression(location) && !ts.isAssignmentTarget(location)) { var type = checkExpression(location); if (getExportSymbolOfValueSymbolIfExported(getNodeLinks(location).resolvedSymbol) === symbol) { return type; @@ -24779,14 +25714,26 @@ var ts; var flowContainer = getControlFlowContainer(node); var isOuterVariable = flowContainer !== declarationContainer; while (flowContainer !== declarationContainer && - (flowContainer.kind === 179 || flowContainer.kind === 180) && + (flowContainer.kind === 179 || + flowContainer.kind === 180 || + ts.isObjectLiteralOrClassExpressionMethod(flowContainer)) && (isReadonlySymbol(localOrExportSymbol) || isParameter && !isParameterAssigned(localOrExportSymbol))) { flowContainer = getControlFlowContainer(flowContainer); } - var assumeInitialized = !strictNullChecks || (type.flags & 1) !== 0 || isParameter || - isOuterVariable || ts.isInAmbientContext(declaration); + var assumeInitialized = isParameter || isOuterVariable || + type !== autoType && (!strictNullChecks || (type.flags & 1) !== 0) || + ts.isInAmbientContext(declaration); var flowType = getFlowTypeOfReference(node, type, assumeInitialized, flowContainer); - if (!assumeInitialized && !(getFalsyFlags(type) & 2048) && getFalsyFlags(flowType) & 2048) { + if (type === autoType) { + if (flowType === autoType) { + if (compilerOptions.noImplicitAny) { + error(declaration.name, ts.Diagnostics.Variable_0_implicitly_has_type_any_in_some_locations_where_its_type_cannot_be_determined, symbolToString(symbol)); + error(node, ts.Diagnostics.Variable_0_implicitly_has_an_1_type, symbolToString(symbol), typeToString(anyType)); + } + return anyType; + } + } + else if (!assumeInitialized && !(getFalsyFlags(type) & 2048) && getFalsyFlags(flowType) & 2048) { error(node, ts.Diagnostics.Variable_0_is_used_before_being_assigned, symbolToString(symbol)); return type; } @@ -24871,7 +25818,7 @@ var ts; } } function findFirstSuperCall(n) { - if (ts.isSuperCallExpression(n)) { + if (ts.isSuperCall(n)) { return n; } else if (ts.isFunctionLike(n)) { @@ -24936,7 +25883,7 @@ var ts; captureLexicalThis(node, container); } if (ts.isFunctionLike(container) && - (!isInParameterInitializerBeforeContainingFunction(node) || getFunctionLikeThisParameter(container))) { + (!isInParameterInitializerBeforeContainingFunction(node) || ts.getThisParameter(container))) { if (container.kind === 179 && ts.isInJavaScriptFile(container.parent) && ts.getSpecialPropertyAssignmentKind(container.parent) === 3) { @@ -25596,7 +26543,8 @@ var ts; patternWithComputedProperties = true; } } - else if (contextualTypeHasPattern && !(contextualType.flags & 536870912)) { + else if (contextualTypeHasPattern && + !(contextualType.flags & 2588672 && contextualType.isObjectLiteralPatternWithComputedProperties)) { var impliedProp = getPropertyOfType(contextualType, member.name); if (impliedProp) { prop.flags |= impliedProp.flags & 536870912; @@ -25647,7 +26595,10 @@ var ts; var numberIndexInfo = hasComputedNumberProperty ? getObjectLiteralIndexInfo(node, propertiesArray, 1) : undefined; var result = createAnonymousType(node.symbol, propertiesTable, emptyArray, emptyArray, stringIndexInfo, numberIndexInfo); var freshObjectLiteralFlag = compilerOptions.suppressExcessPropertyErrors ? 0 : 16777216; - result.flags |= 8388608 | 67108864 | freshObjectLiteralFlag | (typeFlags & 234881024) | (patternWithComputedProperties ? 536870912 : 0); + result.flags |= 8388608 | 67108864 | freshObjectLiteralFlag | (typeFlags & 234881024); + if (patternWithComputedProperties) { + result.isObjectLiteralPatternWithComputedProperties = true; + } if (inDestructuringPattern) { result.pattern = node; } @@ -26042,7 +26993,7 @@ var ts; if (flags & 32) { return true; } - if (type.flags & 268435456) { + if (type.flags & 16384 && type.isThisType) { type = getConstraintOfTypeParameter(type); } if (!(getTargetType(type).flags & (32768 | 65536) && hasBaseType(type, enclosingClass))) { @@ -26083,7 +27034,7 @@ var ts; var prop = getPropertyOfType(apparentType, right.text); if (!prop) { if (right.text && !checkAndReportErrorForExtendingInterface(node)) { - reportNonexistentProperty(right, type.flags & 268435456 ? apparentType : type); + reportNonexistentProperty(right, type.flags & 16384 && type.isThisType ? apparentType : type); } return unknownType; } @@ -26308,19 +27259,19 @@ var ts; for (var _i = 0, signatures_2 = signatures; _i < signatures_2.length; _i++) { var signature = signatures_2[_i]; var symbol = signature.declaration && getSymbolOfNode(signature.declaration); - var parent_10 = signature.declaration && signature.declaration.parent; + var parent_11 = signature.declaration && signature.declaration.parent; if (!lastSymbol || symbol === lastSymbol) { - if (lastParent && parent_10 === lastParent) { + if (lastParent && parent_11 === lastParent) { index++; } else { - lastParent = parent_10; + lastParent = parent_11; index = cutoffIndex; } } else { index = cutoffIndex = result.length; - lastParent = parent_10; + lastParent = parent_11; } lastSymbol = symbol; if (signature.hasLiteralTypes) { @@ -27223,7 +28174,9 @@ var ts; if (!contextualSignature) { reportErrorsFromWidening(func, type); } - if (isUnitType(type) && !(contextualSignature && isLiteralContextualType(getReturnTypeOfSignature(contextualSignature)))) { + if (isUnitType(type) && + !(contextualSignature && + isLiteralContextualType(contextualSignature === getSignatureFromDeclaration(func) ? type : getReturnTypeOfSignature(contextualSignature)))) { type = getWidenedLiteralType(type); } var widenedType = getWidenedType(type); @@ -27373,7 +28326,7 @@ var ts; } } } - if (produceDiagnostics && node.kind !== 147 && node.kind !== 146) { + if (produceDiagnostics && node.kind !== 147) { checkCollisionWithCapturedSuperVariable(node, node.name); checkCollisionWithCapturedThisVariable(node, node.name); } @@ -28316,9 +29269,9 @@ var ts; case 156: case 147: case 146: - var parent_11 = node.parent; - if (node === parent_11.type) { - return parent_11; + var parent_12 = node.parent; + if (node === parent_12.type) { + return parent_12; } } } @@ -28529,7 +29482,7 @@ var ts; return n.name && containsSuperCall(n.name); } function containsSuperCall(n) { - if (ts.isSuperCallExpression(n)) { + if (ts.isSuperCall(n)) { return true; } else if (ts.isFunctionLike(n)) { @@ -28555,6 +29508,7 @@ var ts; } var containingClassDecl = node.parent; if (ts.getClassExtendsHeritageClauseElement(containingClassDecl)) { + captureLexicalThis(node.parent, containingClassDecl); var classExtendsNull = classDeclarationExtendsNull(containingClassDecl); var superCall = getSuperCallInConstructor(node); if (superCall) { @@ -28568,7 +29522,7 @@ var ts; var superCallStatement = void 0; for (var _i = 0, statements_2 = statements; _i < statements_2.length; _i++) { var statement = statements_2[_i]; - if (statement.kind === 202 && ts.isSuperCallExpression(statement.expression)) { + if (statement.kind === 202 && ts.isSuperCall(statement.expression)) { superCallStatement = statement; break; } @@ -29271,14 +30225,14 @@ var ts; } function checkUnusedLocalsAndParameters(node) { if (node.parent.kind !== 222 && noUnusedIdentifiers && !ts.isInAmbientContext(node)) { - var _loop_1 = function(key) { + var _loop_1 = function (key) { var local = node.locals[key]; if (!local.isReferenced) { if (local.valueDeclaration && local.valueDeclaration.kind === 142) { var parameter = local.valueDeclaration; if (compilerOptions.noUnusedParameters && !ts.isParameterPropertyDeclaration(parameter) && - !parameterIsThisKeyword(parameter) && + !ts.parameterIsThisKeyword(parameter) && !parameterNameStartsWithUnderscore(parameter)) { error(local.valueDeclaration.name, ts.Diagnostics._0_is_declared_but_never_used, local.name); } @@ -29293,9 +30247,6 @@ var ts; } } } - function parameterIsThisKeyword(parameter) { - return parameter.name && parameter.name.originalKeywordKind === 97; - } function parameterNameStartsWithUnderscore(parameter) { return parameter.name && parameter.name.kind === 69 && parameter.name.text.charCodeAt(0) === 95; } @@ -29433,6 +30384,9 @@ var ts; } } function checkCollisionWithRequireExportsInGeneratedCode(node, name) { + if (modulekind >= ts.ModuleKind.ES6) { + return; + } if (!needCollisionCheckForIdentifier(node, name, "require") && !needCollisionCheckForIdentifier(node, name, "exports")) { return; } @@ -29536,6 +30490,9 @@ var ts; } } } + function convertAutoToAny(type) { + return type === autoType ? anyType : type; + } function checkVariableLikeDeclaration(node) { checkDecorators(node); checkSourceElement(node.type); @@ -29549,12 +30506,12 @@ var ts; if (node.propertyName && node.propertyName.kind === 140) { checkComputedPropertyName(node.propertyName); } - var parent_12 = node.parent.parent; - var parentType = getTypeForBindingElementParent(parent_12); + var parent_13 = node.parent.parent; + var parentType = getTypeForBindingElementParent(parent_13); var name_21 = node.propertyName || node.name; var property = getPropertyOfType(parentType, getTextOfPropertyName(name_21)); - if (parent_12.initializer && property && getParentOfSymbol(property)) { - checkClassPropertyAccess(parent_12, parent_12.initializer, parentType, property); + if (parent_13.initializer && property && getParentOfSymbol(property)) { + checkClassPropertyAccess(parent_13, parent_13.initializer, parentType, property); } } if (ts.isBindingPattern(node.name)) { @@ -29572,7 +30529,7 @@ var ts; return; } var symbol = getSymbolOfNode(node); - var type = getTypeOfVariableOrParameterOrProperty(symbol); + var type = convertAutoToAny(getTypeOfVariableOrParameterOrProperty(symbol)); if (node === symbol.valueDeclaration) { if (node.initializer && node.parent.parent.kind !== 207) { checkTypeAssignableTo(checkExpressionCached(node.initializer), type, node, undefined); @@ -29580,7 +30537,7 @@ var ts; } } else { - var declarationType = getWidenedTypeForVariableLikeDeclaration(node); + var declarationType = convertAutoToAny(getWidenedTypeForVariableLikeDeclaration(node)); if (type !== unknownType && declarationType !== unknownType && !isTypeIdenticalTo(type, declarationType)) { error(node.name, ts.Diagnostics.Subsequent_variable_declarations_must_have_the_same_type_Variable_0_must_be_of_type_1_but_here_has_type_2, ts.declarationNameToString(node.name), typeToString(type), typeToString(declarationType)); } @@ -29955,7 +30912,12 @@ var ts; } } checkExpression(node.expression); - error(node.expression, ts.Diagnostics.All_symbols_within_a_with_block_will_be_resolved_to_any); + var sourceFile = ts.getSourceFileOfNode(node); + if (!hasParseDiagnostics(sourceFile)) { + var start = ts.getSpanOfTokenAtPosition(sourceFile, node.pos).start; + var end = node.statement.pos; + grammarErrorAtPos(sourceFile, start, end - start, ts.Diagnostics.The_with_statement_is_not_supported_All_symbols_in_a_with_block_will_have_type_any); + } } function checkSwitchStatement(node) { checkGrammarStatementInAmbientContext(node); @@ -30674,9 +31636,11 @@ var ts; grammarErrorOnNode(node.name, ts.Diagnostics.Only_ambient_modules_can_use_quoted_names); } } - checkCollisionWithCapturedThisVariable(node, node.name); - checkCollisionWithRequireExportsInGeneratedCode(node, node.name); - checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); + if (ts.isIdentifier(node.name)) { + checkCollisionWithCapturedThisVariable(node, node.name); + checkCollisionWithRequireExportsInGeneratedCode(node, node.name); + checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); + } checkExportsOnMergedDeclarations(node); var symbol = getSymbolOfNode(node); if (symbol.flags & 512 @@ -30915,9 +31879,11 @@ var ts; } } function checkGrammarModuleElementContext(node, errorMessage) { - if (node.parent.kind !== 256 && node.parent.kind !== 226 && node.parent.kind !== 225) { - return grammarErrorOnFirstToken(node, errorMessage); + var isInAppropriateContext = node.parent.kind === 256 || node.parent.kind === 226 || node.parent.kind === 225; + if (!isInAppropriateContext) { + grammarErrorOnFirstToken(node, errorMessage); } + return !isInAppropriateContext; } function checkExportSpecifier(node) { checkAliasSymbol(node); @@ -31004,7 +31970,8 @@ var ts; links.exportsChecked = true; } function isNotOverload(declaration) { - return declaration.kind !== 220 || !!declaration.body; + return (declaration.kind !== 220 && declaration.kind !== 147) || + !!declaration.body; } } function checkSourceElement(node) { @@ -31489,6 +32456,9 @@ var ts; node.parent.moduleSpecifier === node)) { return resolveExternalModuleName(node, node); } + if (ts.isInJavaScriptFile(node) && ts.isRequireCall(node.parent, false)) { + return resolveExternalModuleName(node, node); + } case 8: if (node.parent.kind === 173 && node.parent.argumentExpression === node) { var objectType = checkExpression(node.parent.expression); @@ -31912,9 +32882,9 @@ var ts; } var location = reference; if (startInDeclarationContainer) { - var parent_13 = reference.parent; - if (ts.isDeclaration(parent_13) && reference === parent_13.name) { - location = getDeclarationContainer(parent_13); + var parent_14 = reference.parent; + if (ts.isDeclaration(parent_14) && reference === parent_14.name) { + location = getDeclarationContainer(parent_14); } } return resolveName(location, reference.text, 107455 | 1048576 | 8388608, undefined, undefined); @@ -32023,9 +32993,9 @@ var ts; } var current = symbol; while (true) { - var parent_14 = getParentOfSymbol(current); - if (parent_14) { - current = parent_14; + var parent_15 = getParentOfSymbol(current); + if (parent_15) { + current = parent_15; } else { break; @@ -32585,8 +33555,8 @@ var ts; function checkGrammarForOmittedArgument(node, args) { if (args) { var sourceFile = ts.getSourceFileOfNode(node); - for (var _i = 0, args_2 = args; _i < args_2.length; _i++) { - var arg = args_2[_i]; + for (var _i = 0, args_4 = args; _i < args_4.length; _i++) { + var arg = args_4[_i]; if (arg.kind === 193) { return grammarErrorAtPos(sourceFile, arg.pos, 0, ts.Diagnostics.Argument_expression_expected); } @@ -32695,8 +33665,7 @@ var ts; for (var _i = 0, _a = node.properties; _i < _a.length; _i++) { var prop = _a[_i]; var name_24 = prop.name; - if (prop.kind === 193 || - name_24.kind === 140) { + if (name_24.kind === 140) { checkGrammarComputedPropertyName(name_24); } if (prop.kind === 254 && !inDestructuring && prop.objectAssignmentInitializer) { @@ -32852,17 +33821,8 @@ var ts; return getAccessorThisParameter(accessor) || accessor.parameters.length === (accessor.kind === 149 ? 0 : 1); } function getAccessorThisParameter(accessor) { - if (accessor.parameters.length === (accessor.kind === 149 ? 1 : 2) && - accessor.parameters[0].name.kind === 69 && - accessor.parameters[0].name.originalKeywordKind === 97) { - return accessor.parameters[0]; - } - } - function getFunctionLikeThisParameter(func) { - if (func.parameters.length && - func.parameters[0].name.kind === 69 && - func.parameters[0].name.originalKeywordKind === 97) { - return func.parameters[0]; + if (accessor.parameters.length === (accessor.kind === 149 ? 1 : 2)) { + return ts.getThisParameter(accessor); } } function checkGrammarForNonSymbolComputedProperty(node, message) { @@ -33222,8 +34182,7 @@ var ts; { name: "name", test: ts.isPropertyName }, { name: "initializer", test: ts.isExpression, optional: true, parenthesize: ts.parenthesizeExpressionForList } ], - _a - )); + _a)); function reduceNode(node, f, initial) { return node ? f(initial, node) : initial; } @@ -33697,7 +34656,7 @@ var ts; case 175: return ts.updateNew(node, visitNode(node.expression, visitor, ts.isExpression), visitNodes(node.typeArguments, visitor, ts.isTypeNode), visitNodes(node.arguments, visitor, ts.isExpression)); case 176: - return ts.updateTaggedTemplate(node, visitNode(node.tag, visitor, ts.isExpression), visitNode(node.template, visitor, ts.isTemplate)); + return ts.updateTaggedTemplate(node, visitNode(node.tag, visitor, ts.isExpression), visitNode(node.template, visitor, ts.isTemplateLiteral)); case 178: return ts.updateParen(node, visitNode(node.expression, visitor, ts.isExpression)); case 179: @@ -33721,7 +34680,7 @@ var ts; case 188: return ts.updateConditional(node, visitNode(node.condition, visitor, ts.isExpression), visitNode(node.whenTrue, visitor, ts.isExpression), visitNode(node.whenFalse, visitor, ts.isExpression)); case 189: - return ts.updateTemplateExpression(node, visitNode(node.head, visitor, ts.isTemplateLiteralFragment), visitNodes(node.templateSpans, visitor, ts.isTemplateSpan)); + return ts.updateTemplateExpression(node, visitNode(node.head, visitor, ts.isTemplateHead), visitNodes(node.templateSpans, visitor, ts.isTemplateSpan)); case 190: return ts.updateYield(node, visitNode(node.expression, visitor, ts.isExpression)); case 191: @@ -33731,7 +34690,7 @@ var ts; case 194: return ts.updateExpressionWithTypeArguments(node, visitNodes(node.typeArguments, visitor, ts.isTypeNode), visitNode(node.expression, visitor, ts.isExpression)); case 197: - return ts.updateTemplateSpan(node, visitNode(node.expression, visitor, ts.isExpression), visitNode(node.literal, visitor, ts.isTemplateLiteralFragment)); + return ts.updateTemplateSpan(node, visitNode(node.expression, visitor, ts.isExpression), visitNode(node.literal, visitor, ts.isTemplateMiddleOrTemplateTail)); case 199: return ts.updateBlock(node, visitNodes(node.statements, visitor, ts.isStatement)); case 200: @@ -33955,10 +34914,10 @@ var ts; ? function (message) { return Debug.assert(false, message || "Node not optional."); } : function (message) { }; Debug.failBadSyntaxKind = Debug.shouldAssert(1) - ? function (node, message) { return Debug.assert(false, message || "Unexpected node.", function () { return ("Node " + ts.formatSyntaxKind(node.kind) + " was unexpected."); }); } + ? function (node, message) { return Debug.assert(false, message || "Unexpected node.", function () { return "Node " + ts.formatSyntaxKind(node.kind) + " was unexpected."; }); } : function (node, message) { }; Debug.assertNode = Debug.shouldAssert(1) - ? function (node, test, message) { return Debug.assert(test === undefined || test(node), message || "Unexpected node.", function () { return ("Node " + ts.formatSyntaxKind(node.kind) + " did not pass test '" + getFunctionName(test) + "'."); }); } + ? function (node, test, message) { return Debug.assert(test === undefined || test(node), message || "Unexpected node.", function () { return "Node " + ts.formatSyntaxKind(node.kind) + " did not pass test '" + getFunctionName(test) + "'."; }); } : function (node, test, message) { }; function getFunctionName(func) { if (typeof func !== "function") { @@ -34006,7 +34965,7 @@ var ts; return expression; function emitAssignment(name, value, location) { var expression = ts.createAssignment(name, value, location); - context.setNodeEmitFlags(expression, 2048); + ts.setEmitFlags(expression, 2048); ts.aggregateTransformFlags(expression); expressions.push(expression); } @@ -34023,7 +34982,7 @@ var ts; return declarations; function emitAssignment(name, value, location) { var declaration = ts.createVariableDeclaration(name, undefined, value, location); - context.setNodeEmitFlags(declaration, 2048); + ts.setEmitFlags(declaration, 2048); ts.aggregateTransformFlags(declaration); declarations.push(declaration); } @@ -34047,7 +35006,7 @@ var ts; } var declaration = ts.createVariableDeclaration(name, undefined, value, location); declaration.original = original; - context.setNodeEmitFlags(declaration, 2048); + ts.setEmitFlags(declaration, 2048); declarations.push(declaration); ts.aggregateTransformFlags(declaration); } @@ -34087,7 +35046,7 @@ var ts; function emitPendingAssignment(name, value, location, original) { var expression = ts.createAssignment(name, value, location); expression.original = original; - context.setNodeEmitFlags(expression, 2048); + ts.setEmitFlags(expression, 2048); pendingAssignments.push(expression); return expression; } @@ -34132,8 +35091,8 @@ var ts; } else { var name_26 = ts.getMutableClone(target); - context.setSourceMapRange(name_26, target); - context.setCommentRange(name_26, target); + ts.setSourceMapRange(name_26, target); + ts.setCommentRange(name_26, target); emitAssignment(name_26, value, location, undefined); } } @@ -34248,7 +35207,7 @@ var ts; (function (ts) { var USE_NEW_TYPE_METADATA_FORMAT = false; function transformTypeScript(context) { - var getNodeEmitFlags = context.getNodeEmitFlags, setNodeEmitFlags = context.setNodeEmitFlags, setCommentRange = context.setCommentRange, setSourceMapRange = context.setSourceMapRange, startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration; + var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration; var resolver = context.getEmitResolver(); var compilerOptions = context.getCompilerOptions(); var languageVersion = ts.getEmitScriptTarget(compilerOptions); @@ -34257,10 +35216,13 @@ var ts; var previousOnSubstituteNode = context.onSubstituteNode; context.onEmitNode = onEmitNode; context.onSubstituteNode = onSubstituteNode; + context.enableSubstitution(172); + context.enableSubstitution(173); var currentSourceFile; var currentNamespace; var currentNamespaceContainerName; var currentScope; + var currentScopeFirstDeclarationsOfName; var currentSourceFileExternalHelpersModuleName; var enabledSubstitutions; var classAliases; @@ -34268,12 +35230,19 @@ var ts; var currentSuperContainer; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } return ts.visitNode(node, visitor, ts.isSourceFile); } function saveStateAndInvoke(node, f) { var savedCurrentScope = currentScope; + var savedCurrentScopeFirstDeclarationsOfName = currentScopeFirstDeclarationsOfName; onBeforeVisitNode(node); var visited = f(node); + if (currentScope !== savedCurrentScope) { + currentScopeFirstDeclarationsOfName = savedCurrentScopeFirstDeclarationsOfName; + } currentScope = savedCurrentScope; return visited; } @@ -34427,11 +35396,22 @@ var ts; case 226: case 199: currentScope = node; + currentScopeFirstDeclarationsOfName = undefined; + break; + case 221: + case 220: + if (ts.hasModifier(node, 2)) { + break; + } + recordEmittedDeclarationInScope(node); break; } } function visitSourceFile(node) { currentSourceFile = node; + if (compilerOptions.alwaysStrict) { + node = ts.ensureUseStrict(node); + } if (node.flags & 31744 && compilerOptions.importHelpers && (ts.isExternalModule(node) || compilerOptions.isolatedModules)) { @@ -34453,7 +35433,7 @@ var ts; else { node = ts.visitEachChild(node, visitor, context); } - setNodeEmitFlags(node, 1 | node.emitFlags); + ts.setEmitFlags(node, 1 | ts.getEmitFlags(node)); return node; } function shouldEmitDecorateCallForClass(node) { @@ -34483,7 +35463,7 @@ var ts; var classDeclaration = ts.createClassDeclaration(undefined, ts.visitNodes(node.modifiers, visitor, ts.isModifier), name, undefined, ts.visitNodes(node.heritageClauses, visitor, ts.isHeritageClause), transformClassMembers(node, hasExtendsClause), node); ts.setOriginalNode(classDeclaration, node); if (staticProperties.length > 0) { - setNodeEmitFlags(classDeclaration, 1024 | getNodeEmitFlags(classDeclaration)); + ts.setEmitFlags(classDeclaration, 1024 | ts.getEmitFlags(classDeclaration)); } statements.push(classDeclaration); } @@ -34525,7 +35505,7 @@ var ts; var transformedClassExpression = ts.createVariableStatement(undefined, ts.createLetDeclarationList([ ts.createVariableDeclaration(classAlias || declaredName, undefined, classExpression) ]), location); - setCommentRange(transformedClassExpression, node); + ts.setCommentRange(transformedClassExpression, node); statements.push(ts.setOriginalNode(transformedClassExpression, node)); if (classAlias) { statements.push(ts.setOriginalNode(ts.createVariableStatement(undefined, ts.createLetDeclarationList([ @@ -34546,7 +35526,7 @@ var ts; enableSubstitutionForClassAliases(); classAliases[ts.getOriginalNodeId(node)] = ts.getSynthesizedClone(temp); } - setNodeEmitFlags(classExpression, 524288 | getNodeEmitFlags(classExpression)); + ts.setEmitFlags(classExpression, 524288 | ts.getEmitFlags(classExpression)); expressions.push(ts.startOnNewLine(ts.createAssignment(temp, classExpression))); ts.addRange(expressions, generateInitializedPropertyExpressions(node, staticProperties, temp)); expressions.push(ts.startOnNewLine(temp)); @@ -34607,7 +35587,7 @@ var ts; return index; } var statement = statements[index]; - if (statement.kind === 202 && ts.isSuperCallExpression(statement.expression)) { + if (statement.kind === 202 && ts.isSuperCall(statement.expression)) { result.push(ts.visitNode(statement, visitor, ts.isStatement)); return index + 1; } @@ -34626,9 +35606,9 @@ var ts; ts.Debug.assert(ts.isIdentifier(node.name)); var name = node.name; var propertyName = ts.getMutableClone(name); - setNodeEmitFlags(propertyName, 49152 | 1536); + ts.setEmitFlags(propertyName, 49152 | 1536); var localName = ts.getMutableClone(name); - setNodeEmitFlags(localName, 49152); + ts.setEmitFlags(localName, 49152); return ts.startOnNewLine(ts.createStatement(ts.createAssignment(ts.createPropertyAccess(ts.createThis(), propertyName, node.name), localName), ts.moveRangePos(node, -1))); } function getInitializedProperties(node, isStatic) { @@ -34649,8 +35629,8 @@ var ts; for (var _i = 0, properties_7 = properties; _i < properties_7.length; _i++) { var property = properties_7[_i]; var statement = ts.createStatement(transformInitializedProperty(node, property, receiver)); - setSourceMapRange(statement, ts.moveRangePastModifiers(property)); - setCommentRange(statement, property); + ts.setSourceMapRange(statement, ts.moveRangePastModifiers(property)); + ts.setCommentRange(statement, property); statements.push(statement); } } @@ -34660,8 +35640,8 @@ var ts; var property = properties_8[_i]; var expression = transformInitializedProperty(node, property, receiver); expression.startsOnNewLine = true; - setSourceMapRange(expression, ts.moveRangePastModifiers(property)); - setCommentRange(expression, property); + ts.setSourceMapRange(expression, ts.moveRangePastModifiers(property)); + ts.setCommentRange(expression, property); expressions.push(expression); } return expressions; @@ -34802,7 +35782,7 @@ var ts; : ts.createNull() : undefined; var helper = ts.createDecorateHelper(currentSourceFileExternalHelpersModuleName, decoratorExpressions, prefix, memberName, descriptor, ts.moveRangePastDecorators(member)); - setNodeEmitFlags(helper, 49152); + ts.setEmitFlags(helper, 49152); return helper; } function addConstructorDecorationStatement(statements, node, decoratedClassAlias) { @@ -34820,12 +35800,12 @@ var ts; if (decoratedClassAlias) { var expression = ts.createAssignment(decoratedClassAlias, ts.createDecorateHelper(currentSourceFileExternalHelpersModuleName, decoratorExpressions, getDeclarationName(node))); var result = ts.createAssignment(getDeclarationName(node), expression, ts.moveRangePastDecorators(node)); - setNodeEmitFlags(result, 49152); + ts.setEmitFlags(result, 49152); return result; } else { var result = ts.createAssignment(getDeclarationName(node), ts.createDecorateHelper(currentSourceFileExternalHelpersModuleName, decoratorExpressions, getDeclarationName(node)), ts.moveRangePastDecorators(node)); - setNodeEmitFlags(result, 49152); + ts.setEmitFlags(result, 49152); return result; } } @@ -34839,7 +35819,7 @@ var ts; for (var _i = 0, decorators_1 = decorators; _i < decorators_1.length; _i++) { var decorator = decorators_1[_i]; var helper = ts.createParamHelper(currentSourceFileExternalHelpersModuleName, transformDecorator(decorator), parameterOffset, decorator.expression); - setNodeEmitFlags(helper, 49152); + ts.setEmitFlags(helper, 49152); expressions.push(helper); } } @@ -35004,10 +35984,33 @@ var ts; : ts.createIdentifier("Symbol"); case 155: return serializeTypeReferenceNode(node); + case 163: + case 162: + { + var unionOrIntersection = node; + var serializedUnion = void 0; + for (var _i = 0, _a = unionOrIntersection.types; _i < _a.length; _i++) { + var typeNode = _a[_i]; + var serializedIndividual = serializeTypeNode(typeNode); + if (serializedIndividual.kind !== 69) { + serializedUnion = undefined; + break; + } + if (serializedIndividual.text === "Object") { + return serializedIndividual; + } + if (serializedUnion && serializedUnion.text !== serializedIndividual.text) { + serializedUnion = undefined; + break; + } + serializedUnion = serializedIndividual; + } + if (serializedUnion) { + return serializedUnion; + } + } case 158: case 159: - case 162: - case 163: case 117: case 165: break; @@ -35128,8 +36131,8 @@ var ts; return undefined; } var method = ts.createMethod(undefined, ts.visitNodes(node.modifiers, visitor, ts.isModifier), node.asteriskToken, visitPropertyNameOfClassElement(node), undefined, ts.visitNodes(node.parameters, visitor, ts.isParameter), undefined, transformFunctionBody(node), node); - setCommentRange(method, node); - setSourceMapRange(method, ts.moveRangePastDecorators(node)); + ts.setCommentRange(method, node); + ts.setSourceMapRange(method, ts.moveRangePastDecorators(node)); ts.setOriginalNode(method, node); return method; } @@ -35141,8 +36144,8 @@ var ts; return undefined; } var accessor = ts.createGetAccessor(undefined, ts.visitNodes(node.modifiers, visitor, ts.isModifier), visitPropertyNameOfClassElement(node), ts.visitNodes(node.parameters, visitor, ts.isParameter), undefined, node.body ? ts.visitEachChild(node.body, visitor, context) : ts.createBlock([]), node); - setCommentRange(accessor, node); - setSourceMapRange(accessor, ts.moveRangePastDecorators(node)); + ts.setCommentRange(accessor, node); + ts.setSourceMapRange(accessor, ts.moveRangePastDecorators(node)); ts.setOriginalNode(accessor, node); return accessor; } @@ -35151,8 +36154,8 @@ var ts; return undefined; } var accessor = ts.createSetAccessor(undefined, ts.visitNodes(node.modifiers, visitor, ts.isModifier), visitPropertyNameOfClassElement(node), ts.visitNodes(node.parameters, visitor, ts.isParameter), node.body ? ts.visitEachChild(node.body, visitor, context) : ts.createBlock([]), node); - setCommentRange(accessor, node); - setSourceMapRange(accessor, ts.moveRangePastDecorators(node)); + ts.setCommentRange(accessor, node); + ts.setSourceMapRange(accessor, ts.moveRangePastDecorators(node)); ts.setOriginalNode(accessor, node); return accessor; } @@ -35247,11 +36250,11 @@ var ts; if (languageVersion >= 2) { if (resolver.getNodeCheckFlags(node) & 4096) { enableSubstitutionForAsyncMethodsWithSuper(); - setNodeEmitFlags(block, 8); + ts.setEmitFlags(block, 8); } else if (resolver.getNodeCheckFlags(node) & 2048) { enableSubstitutionForAsyncMethodsWithSuper(); - setNodeEmitFlags(block, 4); + ts.setEmitFlags(block, 4); } } return block; @@ -35261,14 +36264,14 @@ var ts; } } function visitParameter(node) { - if (node.name && ts.isIdentifier(node.name) && node.name.originalKeywordKind === 97) { + if (ts.parameterIsThisKeyword(node)) { return undefined; } var parameter = ts.createParameterDeclaration(undefined, undefined, node.dotDotDotToken, ts.visitNode(node.name, visitor, ts.isBindingName), undefined, undefined, ts.visitNode(node.initializer, visitor, ts.isExpression), ts.moveRangePastModifiers(node)); ts.setOriginalNode(parameter, node); - setCommentRange(parameter, node); - setSourceMapRange(parameter, ts.moveRangePastModifiers(node)); - setNodeEmitFlags(parameter.name, 1024); + ts.setCommentRange(parameter, node); + ts.setSourceMapRange(parameter, ts.moveRangePastModifiers(node)); + ts.setEmitFlags(parameter.name, 1024); return parameter; } function visitVariableStatement(node) { @@ -35317,12 +36320,13 @@ var ts; || compilerOptions.isolatedModules; } function shouldEmitVarForEnumDeclaration(node) { - return !ts.hasModifier(node, 1) - || (isES6ExportedDeclaration(node) && ts.isFirstDeclarationOfKind(node, node.kind)); + return isFirstEmittedDeclarationInScope(node) + && (!ts.hasModifier(node, 1) + || isES6ExportedDeclaration(node)); } function addVarForEnumExportedFromNamespace(statements, node) { var statement = ts.createVariableStatement(undefined, [ts.createVariableDeclaration(getDeclarationName(node), undefined, getExportName(node))]); - setSourceMapRange(statement, node); + ts.setSourceMapRange(statement, node); statements.push(statement); } function visitEnumDeclaration(node) { @@ -35331,6 +36335,7 @@ var ts; } var statements = []; var emitFlags = 64; + recordEmittedDeclarationInScope(node); if (shouldEmitVarForEnumDeclaration(node)) { addVarForEnumOrModuleDeclaration(statements, node); if (moduleKind !== ts.ModuleKind.System || currentScope !== currentSourceFile) { @@ -35342,7 +36347,7 @@ var ts; var exportName = getExportName(node); var enumStatement = ts.createStatement(ts.createCall(ts.createFunctionExpression(undefined, undefined, undefined, [ts.createParameter(parameterName)], undefined, transformEnumBody(node, containerName)), undefined, [ts.createLogicalOr(exportName, ts.createAssignment(exportName, ts.createObjectLiteral()))]), node); ts.setOriginalNode(enumStatement, node); - setNodeEmitFlags(enumStatement, emitFlags); + ts.setEmitFlags(enumStatement, emitFlags); statements.push(enumStatement); if (isNamespaceExport(node)) { addVarForEnumExportedFromNamespace(statements, node); @@ -35381,18 +36386,32 @@ var ts; function shouldEmitModuleDeclaration(node) { return ts.isInstantiatedModule(node, compilerOptions.preserveConstEnums || compilerOptions.isolatedModules); } - function isModuleMergedWithES6Class(node) { - return languageVersion === 2 - && ts.isMergedWithClass(node); - } function isES6ExportedDeclaration(node) { return isExternalModuleExport(node) && moduleKind === ts.ModuleKind.ES6; } + function recordEmittedDeclarationInScope(node) { + var name = node.symbol && node.symbol.name; + if (name) { + if (!currentScopeFirstDeclarationsOfName) { + currentScopeFirstDeclarationsOfName = ts.createMap(); + } + if (!(name in currentScopeFirstDeclarationsOfName)) { + currentScopeFirstDeclarationsOfName[name] = node; + } + } + } + function isFirstEmittedDeclarationInScope(node) { + if (currentScopeFirstDeclarationsOfName) { + var name_28 = node.symbol && node.symbol.name; + if (name_28) { + return currentScopeFirstDeclarationsOfName[name_28] === node; + } + } + return false; + } function shouldEmitVarForModuleDeclaration(node) { - return !isModuleMergedWithES6Class(node) - && (!isES6ExportedDeclaration(node) - || ts.isFirstDeclarationOfKind(node, node.kind)); + return isFirstEmittedDeclarationInScope(node); } function addVarForEnumOrModuleDeclaration(statements, node) { var statement = ts.createVariableStatement(isES6ExportedDeclaration(node) @@ -35402,13 +36421,13 @@ var ts; ]); ts.setOriginalNode(statement, node); if (node.kind === 224) { - setSourceMapRange(statement.declarationList, node); + ts.setSourceMapRange(statement.declarationList, node); } else { - setSourceMapRange(statement, node); + ts.setSourceMapRange(statement, node); } - setCommentRange(statement, node); - setNodeEmitFlags(statement, 32768); + ts.setCommentRange(statement, node); + ts.setEmitFlags(statement, 32768); statements.push(statement); } function visitModuleDeclaration(node) { @@ -35419,6 +36438,7 @@ var ts; enableSubstitutionForNamespaceExports(); var statements = []; var emitFlags = 64; + recordEmittedDeclarationInScope(node); if (shouldEmitVarForModuleDeclaration(node)) { addVarForEnumOrModuleDeclaration(statements, node); if (moduleKind !== ts.ModuleKind.System || currentScope !== currentSourceFile) { @@ -35435,15 +36455,17 @@ var ts; } var moduleStatement = ts.createStatement(ts.createCall(ts.createFunctionExpression(undefined, undefined, undefined, [ts.createParameter(parameterName)], undefined, transformModuleBody(node, containerName)), undefined, [moduleArg]), node); ts.setOriginalNode(moduleStatement, node); - setNodeEmitFlags(moduleStatement, emitFlags); + ts.setEmitFlags(moduleStatement, emitFlags); statements.push(moduleStatement); return statements; } function transformModuleBody(node, namespaceLocalName) { var savedCurrentNamespaceContainerName = currentNamespaceContainerName; var savedCurrentNamespace = currentNamespace; + var savedCurrentScopeFirstDeclarationsOfName = currentScopeFirstDeclarationsOfName; currentNamespaceContainerName = namespaceLocalName; currentNamespace = node; + currentScopeFirstDeclarationsOfName = undefined; var statements = []; startLexicalEnvironment(); var statementsLocation; @@ -35470,9 +36492,10 @@ var ts; ts.addRange(statements, endLexicalEnvironment()); currentNamespaceContainerName = savedCurrentNamespaceContainerName; currentNamespace = savedCurrentNamespace; + currentScopeFirstDeclarationsOfName = savedCurrentScopeFirstDeclarationsOfName; var block = ts.createBlock(ts.createNodeArray(statements, statementsLocation), blockLocation, true); if (body.kind !== 226) { - setNodeEmitFlags(block, block.emitFlags | 49152); + ts.setEmitFlags(block, ts.getEmitFlags(block) | 49152); } return block; } @@ -35495,7 +36518,7 @@ var ts; return undefined; } var moduleReference = ts.createExpressionFromEntityName(node.moduleReference); - setNodeEmitFlags(moduleReference, 49152 | 65536); + ts.setEmitFlags(moduleReference, 49152 | 65536); if (isNamedExternalModuleExport(node) || !isNamespaceExport(node)) { return ts.setOriginalNode(ts.createVariableStatement(ts.visitNodes(node.modifiers, visitor, ts.isModifier), ts.createVariableDeclarationList([ ts.createVariableDeclaration(node.name, undefined, moduleReference) @@ -35524,9 +36547,9 @@ var ts; } function addExportMemberAssignment(statements, node) { var expression = ts.createAssignment(getExportName(node), getLocalName(node, true)); - setSourceMapRange(expression, ts.createRange(node.name.pos, node.end)); + ts.setSourceMapRange(expression, ts.createRange(node.name.pos, node.end)); var statement = ts.createStatement(expression); - setSourceMapRange(statement, ts.createRange(-1, node.end)); + ts.setSourceMapRange(statement, ts.createRange(-1, node.end)); statements.push(statement); } function createNamespaceExport(exportName, exportValue, location) { @@ -35547,7 +36570,7 @@ var ts; emitFlags |= 1536; } if (emitFlags) { - setNodeEmitFlags(qualifiedName, emitFlags); + ts.setEmitFlags(qualifiedName, emitFlags); } return qualifiedName; } @@ -35556,7 +36579,7 @@ var ts; } function getNamespaceParameterName(node) { var name = ts.getGeneratedNameForNode(node); - setSourceMapRange(name, node.name); + ts.setSourceMapRange(name, node.name); return name; } function getNamespaceContainerName(node) { @@ -35573,8 +36596,8 @@ var ts; } function getDeclarationName(node, allowComments, allowSourceMaps, emitFlags) { if (node.name) { - var name_28 = ts.getMutableClone(node.name); - emitFlags |= getNodeEmitFlags(node.name); + var name_29 = ts.getMutableClone(node.name); + emitFlags |= ts.getEmitFlags(node.name); if (!allowSourceMaps) { emitFlags |= 1536; } @@ -35582,9 +36605,9 @@ var ts; emitFlags |= 49152; } if (emitFlags) { - setNodeEmitFlags(name_28, emitFlags); + ts.setEmitFlags(name_29, emitFlags); } - return name_28; + return name_29; } else { return ts.getGeneratedNameForNode(node); @@ -35646,7 +36669,7 @@ var ts; function isTransformedEnumDeclaration(node) { return ts.getOriginalNode(node).kind === 224; } - function onEmitNode(node, emit) { + function onEmitNode(emitContext, node, emitCallback) { var savedApplicableSubstitutions = applicableSubstitutions; var savedCurrentSuperContainer = currentSuperContainer; if (enabledSubstitutions & 4 && isSuperContainer(node)) { @@ -35658,13 +36681,13 @@ var ts; if (enabledSubstitutions & 8 && isTransformedEnumDeclaration(node)) { applicableSubstitutions |= 8; } - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); applicableSubstitutions = savedApplicableSubstitutions; currentSuperContainer = savedCurrentSuperContainer; } - function onSubstituteNode(node, isExpression) { - node = previousOnSubstituteNode(node, isExpression); - if (isExpression) { + function onSubstituteNode(emitContext, node) { + node = previousOnSubstituteNode(emitContext, node); + if (emitContext === 1) { return substituteExpression(node); } else if (ts.isShorthandPropertyAssignment(node)) { @@ -35674,14 +36697,14 @@ var ts; } function substituteShorthandPropertyAssignment(node) { if (enabledSubstitutions & 2) { - var name_29 = node.name; - var exportedName = trySubstituteNamespaceExportedName(name_29); + var name_30 = node.name; + var exportedName = trySubstituteNamespaceExportedName(name_30); if (exportedName) { if (node.objectAssignmentInitializer) { var initializer = ts.createAssignment(exportedName, node.objectAssignmentInitializer); - return ts.createPropertyAssignment(name_29, initializer, node); + return ts.createPropertyAssignment(name_30, initializer, node); } - return ts.createPropertyAssignment(name_29, exportedName, node); + return ts.createPropertyAssignment(name_30, exportedName, node); } } return node; @@ -35690,16 +36713,15 @@ var ts; switch (node.kind) { case 69: return substituteExpressionIdentifier(node); - } - if (enabledSubstitutions & 4) { - switch (node.kind) { - case 174: + case 172: + return substitutePropertyAccessExpression(node); + case 173: + return substituteElementAccessExpression(node); + case 174: + if (enabledSubstitutions & 4) { return substituteCallExpression(node); - case 172: - return substitutePropertyAccessExpression(node); - case 173: - return substituteElementAccessExpression(node); - } + } + break; } return node; } @@ -35716,8 +36738,8 @@ var ts; var classAlias = classAliases[declaration.id]; if (classAlias) { var clone_4 = ts.getSynthesizedClone(classAlias); - setSourceMapRange(clone_4, node); - setCommentRange(clone_4, node); + ts.setSourceMapRange(clone_4, node); + ts.setCommentRange(clone_4, node); return clone_4; } } @@ -35726,7 +36748,7 @@ var ts; return undefined; } function trySubstituteNamespaceExportedName(node) { - if (enabledSubstitutions & applicableSubstitutions && (getNodeEmitFlags(node) & 262144) === 0) { + if (enabledSubstitutions & applicableSubstitutions && (ts.getEmitFlags(node) & 262144) === 0) { var container = resolver.getReferencedExportContainer(node, false); if (container) { var substitute = (applicableSubstitutions & 2 && container.kind === 225) || @@ -35754,23 +36776,48 @@ var ts; return node; } function substitutePropertyAccessExpression(node) { - if (node.expression.kind === 95) { + if (enabledSubstitutions & 4 && node.expression.kind === 95) { var flags = getSuperContainerAsyncMethodFlags(); if (flags) { return createSuperAccessInAsyncMethod(ts.createLiteral(node.name.text), flags, node); } } - return node; + return substituteConstantValue(node); } function substituteElementAccessExpression(node) { - if (node.expression.kind === 95) { + if (enabledSubstitutions & 4 && node.expression.kind === 95) { var flags = getSuperContainerAsyncMethodFlags(); if (flags) { return createSuperAccessInAsyncMethod(node.argumentExpression, flags, node); } } + return substituteConstantValue(node); + } + function substituteConstantValue(node) { + var constantValue = tryGetConstEnumValue(node); + if (constantValue !== undefined) { + var substitute = ts.createLiteral(constantValue); + ts.setSourceMapRange(substitute, node); + ts.setCommentRange(substitute, node); + if (!compilerOptions.removeComments) { + var propertyName = ts.isPropertyAccessExpression(node) + ? ts.declarationNameToString(node.name) + : ts.getTextOfNode(node.argumentExpression); + substitute.trailingComment = " " + propertyName + " "; + } + ts.setConstantValue(node, constantValue); + return substitute; + } return node; } + function tryGetConstEnumValue(node) { + if (compilerOptions.isolatedModules) { + return undefined; + } + return ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node) + ? resolver.getConstantValue(node) + : undefined; + } function createSuperAccessInAsyncMethod(argumentExpression, flags, location) { if (flags & 4096) { return ts.createPropertyAccess(ts.createCall(ts.createIdentifier("_super"), undefined, [argumentExpression]), "value", location); @@ -35794,6 +36841,9 @@ var ts; var currentSourceFile; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } if (ts.isExternalModule(node) || compilerOptions.isolatedModules) { currentSourceFile = node; return ts.visitEachChild(node, visitor, context); @@ -35905,7 +36955,7 @@ var ts; var ts; (function (ts) { function transformSystemModule(context) { - var getNodeEmitFlags = context.getNodeEmitFlags, setNodeEmitFlags = context.setNodeEmitFlags, startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration, hoistFunctionDeclaration = context.hoistFunctionDeclaration; + var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration, hoistFunctionDeclaration = context.hoistFunctionDeclaration; var compilerOptions = context.getCompilerOptions(); var resolver = context.getEmitResolver(); var host = context.getEmitHost(); @@ -35934,6 +36984,9 @@ var ts; var currentNode; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } if (ts.isExternalModule(node) || compilerOptions.isolatedModules) { currentSourceFile = node; currentNode = node; @@ -35966,12 +37019,12 @@ var ts; var body = ts.createFunctionExpression(undefined, undefined, undefined, [ ts.createParameter(exportFunctionForFile), ts.createParameter(contextObjectForFile) - ], undefined, setNodeEmitFlags(ts.createBlock(statements, undefined, true), 1)); + ], undefined, ts.setEmitFlags(ts.createBlock(statements, undefined, true), 1)); return updateSourceFile(node, [ ts.createStatement(ts.createCall(ts.createPropertyAccess(ts.createIdentifier("System"), "register"), undefined, moduleName ? [moduleName, dependencies, body] : [dependencies, body])) - ], ~1 & getNodeEmitFlags(node)); + ], ~1 & ts.getEmitFlags(node)); var _a; } function addSystemModuleBody(statements, node, dependencyGroups) { @@ -36215,11 +37268,11 @@ var ts; } function visitFunctionDeclaration(node) { if (ts.hasModifier(node, 1)) { - var name_30 = node.name || ts.getGeneratedNameForNode(node); - var newNode = ts.createFunctionDeclaration(undefined, undefined, node.asteriskToken, name_30, undefined, node.parameters, undefined, node.body, node); + var name_31 = node.name || ts.getGeneratedNameForNode(node); + var newNode = ts.createFunctionDeclaration(undefined, undefined, node.asteriskToken, name_31, undefined, node.parameters, undefined, node.body, node); recordExportedFunctionDeclaration(node); if (!ts.hasModifier(node, 512)) { - recordExportName(name_30); + recordExportName(name_31); } ts.setOriginalNode(newNode, node); node = newNode; @@ -36230,13 +37283,13 @@ var ts; function visitExpressionStatement(node) { var originalNode = ts.getOriginalNode(node); if ((originalNode.kind === 225 || originalNode.kind === 224) && ts.hasModifier(originalNode, 1)) { - var name_31 = getDeclarationName(originalNode); + var name_32 = getDeclarationName(originalNode); if (originalNode.kind === 224) { - hoistVariableDeclaration(name_31); + hoistVariableDeclaration(name_32); } return [ node, - createExportStatement(name_31, name_31) + createExportStatement(name_32, name_32) ]; } return node; @@ -36390,19 +37443,19 @@ var ts; function visitBlock(node) { return ts.visitEachChild(node, visitNestedNode, context); } - function onEmitNode(node, emit) { + function onEmitNode(emitContext, node, emitCallback) { if (node.kind === 256) { exportFunctionForFile = exportFunctionForFileMap[ts.getOriginalNodeId(node)]; - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); exportFunctionForFile = undefined; } else { - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); } } - function onSubstituteNode(node, isExpression) { - node = previousOnSubstituteNode(node, isExpression); - if (isExpression) { + function onSubstituteNode(emitContext, node) { + node = previousOnSubstituteNode(emitContext, node); + if (emitContext === 1) { return substituteExpression(node); } return node; @@ -36436,7 +37489,7 @@ var ts; return node; } function substituteAssignmentExpression(node) { - setNodeEmitFlags(node, 128); + ts.setEmitFlags(node, 128); var left = node.left; switch (left.kind) { case 69: @@ -36541,7 +37594,7 @@ var ts; var exportDeclaration = resolver.getReferencedExportContainer(operand); if (exportDeclaration) { var expr = ts.createPrefix(node.operator, operand, node); - setNodeEmitFlags(expr, 128); + ts.setEmitFlags(expr, 128); var call = createExportExpression(operand, expr); if (node.kind === 185) { return call; @@ -36574,7 +37627,7 @@ var ts; ts.createForIn(ts.createVariableDeclarationList([ ts.createVariableDeclaration(n, undefined) ]), m, ts.createBlock([ - setNodeEmitFlags(ts.createIf(condition, ts.createStatement(ts.createAssignment(ts.createElementAccess(exports, n), ts.createElementAccess(m, n)))), 32) + ts.setEmitFlags(ts.createIf(condition, ts.createStatement(ts.createAssignment(ts.createElementAccess(exports, n), ts.createElementAccess(m, n)))), 32) ])), ts.createStatement(ts.createCall(exportFunctionForFile, undefined, [exports])) ], undefined, true))); @@ -36674,7 +37727,7 @@ var ts; function updateSourceFile(node, statements, nodeEmitFlags) { var updated = ts.getMutableClone(node); updated.statements = ts.createNodeArray(statements, node.statements); - setNodeEmitFlags(updated, nodeEmitFlags); + ts.setEmitFlags(updated, nodeEmitFlags); return updated; } } @@ -36688,9 +37741,8 @@ var ts; _a[ts.ModuleKind.CommonJS] = transformCommonJSModule, _a[ts.ModuleKind.AMD] = transformAMDModule, _a[ts.ModuleKind.UMD] = transformUMDModule, - _a - )); - var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration, setNodeEmitFlags = context.setNodeEmitFlags, getNodeEmitFlags = context.getNodeEmitFlags, setSourceMapRange = context.setSourceMapRange; + _a)); + var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration; var compilerOptions = context.getCompilerOptions(); var resolver = context.getEmitResolver(); var host = context.getEmitHost(); @@ -36715,6 +37767,9 @@ var ts; var hasExportStarsToExportValues; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } if (ts.isExternalModule(node) || compilerOptions.isolatedModules) { currentSourceFile = node; (_a = ts.collectExternalModuleInfo(node, resolver), externalImports = _a.externalImports, exportSpecifiers = _a.exportSpecifiers, exportEquals = _a.exportEquals, hasExportStarsToExportValues = _a.hasExportStarsToExportValues, _a); @@ -36740,7 +37795,7 @@ var ts; addExportEqualsIfNeeded(statements, false); var updated = updateSourceFile(node, statements); if (hasExportStarsToExportValues) { - setNodeEmitFlags(updated, 2 | getNodeEmitFlags(node)); + ts.setEmitFlags(updated, 2 | ts.getEmitFlags(node)); } return updated; } @@ -36751,7 +37806,7 @@ var ts; } function transformUMDModule(node) { var define = ts.createIdentifier("define"); - setNodeEmitFlags(define, 16); + ts.setEmitFlags(define, 16); return transformAsynchronousModule(node, define, undefined, false); } function transformAsynchronousModule(node, define, moduleName, includeNonAmdDependencies) { @@ -36778,7 +37833,7 @@ var ts; addExportEqualsIfNeeded(statements, true); var body = ts.createBlock(statements, undefined, true); if (hasExportStarsToExportValues) { - setNodeEmitFlags(body, 2); + ts.setEmitFlags(body, 2); } return body; } @@ -36786,12 +37841,12 @@ var ts; if (exportEquals && resolver.isValueAliasDeclaration(exportEquals)) { if (emitAsReturn) { var statement = ts.createReturn(exportEquals.expression, exportEquals); - setNodeEmitFlags(statement, 12288 | 49152); + ts.setEmitFlags(statement, 12288 | 49152); statements.push(statement); } else { var statement = ts.createStatement(ts.createAssignment(ts.createPropertyAccess(ts.createIdentifier("module"), "exports"), exportEquals.expression), exportEquals); - setNodeEmitFlags(statement, 49152); + ts.setEmitFlags(statement, 49152); statements.push(statement); } } @@ -36854,7 +37909,7 @@ var ts; if (!ts.contains(externalImports, node)) { return undefined; } - setNodeEmitFlags(node.name, 128); + ts.setEmitFlags(node.name, 128); var statements = []; if (moduleKind !== ts.ModuleKind.AMD) { if (ts.hasModifier(node, 1)) { @@ -36942,16 +37997,16 @@ var ts; else { var names = ts.reduceEachChild(node, collectExportMembers, []); for (var _i = 0, names_1 = names; _i < names_1.length; _i++) { - var name_32 = names_1[_i]; - addExportMemberAssignments(statements, name_32); + var name_33 = names_1[_i]; + addExportMemberAssignments(statements, name_33); } } } function collectExportMembers(names, node) { if (ts.isAliasSymbolDeclaration(node) && resolver.isValueAliasDeclaration(node) && ts.isDeclaration(node)) { - var name_33 = node.name; - if (ts.isIdentifier(name_33)) { - names.push(name_33); + var name_34 = node.name; + if (ts.isIdentifier(name_34)) { + names.push(name_34); } } return ts.reduceEachChild(node, collectExportMembers, names); @@ -36969,7 +38024,7 @@ var ts; addExportDefault(statements, getDeclarationName(node), node); } else { - statements.push(createExportStatement(node.name, setNodeEmitFlags(ts.getSynthesizedClone(node.name), 262144), node)); + statements.push(createExportStatement(node.name, ts.setEmitFlags(ts.getSynthesizedClone(node.name), 262144), node)); } } function visitVariableStatement(node) { @@ -37086,25 +38141,25 @@ var ts; } function addVarForExportedEnumOrNamespaceDeclaration(statements, node) { var transformedStatement = ts.createVariableStatement(undefined, [ts.createVariableDeclaration(getDeclarationName(node), undefined, ts.createPropertyAccess(ts.createIdentifier("exports"), getDeclarationName(node)))], node); - setNodeEmitFlags(transformedStatement, 49152); + ts.setEmitFlags(transformedStatement, 49152); statements.push(transformedStatement); } function getDeclarationName(node) { return node.name ? ts.getSynthesizedClone(node.name) : ts.getGeneratedNameForNode(node); } - function onEmitNode(node, emit) { + function onEmitNode(emitContext, node, emitCallback) { if (node.kind === 256) { bindingNameExportSpecifiersMap = bindingNameExportSpecifiersForFileMap[ts.getOriginalNodeId(node)]; - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); bindingNameExportSpecifiersMap = undefined; } else { - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); } } - function onSubstituteNode(node, isExpression) { - node = previousOnSubstituteNode(node, isExpression); - if (isExpression) { + function onSubstituteNode(emitContext, node) { + node = previousOnSubstituteNode(emitContext, node); + if (emitContext === 1) { return substituteExpression(node); } else if (ts.isShorthandPropertyAssignment(node)) { @@ -37145,7 +38200,7 @@ var ts; var left = node.left; if (ts.isIdentifier(left) && ts.isAssignmentOperator(node.operatorToken.kind)) { if (bindingNameExportSpecifiersMap && ts.hasProperty(bindingNameExportSpecifiersMap, left.text)) { - setNodeEmitFlags(node, 128); + ts.setEmitFlags(node, 128); var nestedExportAssignment = void 0; for (var _i = 0, _a = bindingNameExportSpecifiersMap[left.text]; _i < _a.length; _i++) { var specifier = _a[_i]; @@ -37163,11 +38218,11 @@ var ts; var operand = node.operand; if (ts.isIdentifier(operand) && bindingNameExportSpecifiersForFileMap) { if (bindingNameExportSpecifiersMap && ts.hasProperty(bindingNameExportSpecifiersMap, operand.text)) { - setNodeEmitFlags(node, 128); + ts.setEmitFlags(node, 128); var transformedUnaryExpression = void 0; if (node.kind === 186) { - transformedUnaryExpression = ts.createBinary(operand, ts.createNode(operator === 41 ? 57 : 58), ts.createLiteral(1), node); - setNodeEmitFlags(transformedUnaryExpression, 128); + transformedUnaryExpression = ts.createBinary(operand, ts.createToken(operator === 41 ? 57 : 58), ts.createLiteral(1), node); + ts.setEmitFlags(transformedUnaryExpression, 128); } var nestedExportAssignment = void 0; for (var _i = 0, _a = bindingNameExportSpecifiersMap[operand.text]; _i < _a.length; _i++) { @@ -37182,7 +38237,7 @@ var ts; return node; } function trySubstituteExportedName(node) { - var emitFlags = getNodeEmitFlags(node); + var emitFlags = ts.getEmitFlags(node); if ((emitFlags & 262144) === 0) { var container = resolver.getReferencedExportContainer(node, (emitFlags & 131072) !== 0); if (container) { @@ -37194,7 +38249,7 @@ var ts; return undefined; } function trySubstituteImportedName(node) { - if ((getNodeEmitFlags(node) & 262144) === 0) { + if ((ts.getEmitFlags(node) & 262144) === 0) { var declaration = resolver.getReferencedImportDeclaration(node); if (declaration) { if (ts.isImportClause(declaration)) { @@ -37206,12 +38261,12 @@ var ts; } } else if (ts.isImportSpecifier(declaration)) { - var name_34 = declaration.propertyName || declaration.name; - if (name_34.originalKeywordKind === 77 && languageVersion <= 0) { - return ts.createElementAccess(ts.getGeneratedNameForNode(declaration.parent.parent.parent), ts.createLiteral(name_34.text), node); + var name_35 = declaration.propertyName || declaration.name; + if (name_35.originalKeywordKind === 77 && languageVersion <= 0) { + return ts.createElementAccess(ts.getGeneratedNameForNode(declaration.parent.parent.parent), ts.createLiteral(name_35.text), node); } else { - return ts.createPropertyAccess(ts.getGeneratedNameForNode(declaration.parent.parent.parent), ts.getSynthesizedClone(name_34), node); + return ts.createPropertyAccess(ts.getGeneratedNameForNode(declaration.parent.parent.parent), ts.getSynthesizedClone(name_35), node); } } } @@ -37233,7 +38288,7 @@ var ts; var statement = ts.createStatement(createExportAssignment(name, value)); statement.startsOnNewLine = true; if (location) { - setSourceMapRange(statement, location); + ts.setSourceMapRange(statement, location); } return statement; } @@ -37261,7 +38316,7 @@ var ts; var externalModuleName = ts.getExternalModuleNameLiteral(importNode, currentSourceFile, host, resolver, compilerOptions); var importAliasName = ts.getLocalNameForExternalImport(importNode, currentSourceFile); if (includeNonAmdDependencies && importAliasName) { - setNodeEmitFlags(importAliasName, 128); + ts.setEmitFlags(importAliasName, 128); aliasedModuleNames.push(externalModuleName); importAliasNames.push(ts.createParameter(importAliasName)); } @@ -37288,6 +38343,9 @@ var ts; var currentSourceFile; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } currentSourceFile = node; node = ts.visitEachChild(node, visitor, context); currentSourceFile = undefined; @@ -37446,12 +38504,12 @@ var ts; return getTagName(node.openingElement); } else { - var name_35 = node.tagName; - if (ts.isIdentifier(name_35) && ts.isIntrinsicJsxName(name_35.text)) { - return ts.createLiteral(name_35.text); + var name_36 = node.tagName; + if (ts.isIdentifier(name_36) && ts.isIntrinsicJsxName(name_36.text)) { + return ts.createLiteral(name_36.text); } else { - return ts.createExpressionFromEntityName(name_35); + return ts.createExpressionFromEntityName(name_36); } } } @@ -37733,6 +38791,9 @@ var ts; var hoistVariableDeclaration = context.hoistVariableDeclaration; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } return ts.visitEachChild(node, visitor, context); } function visitor(node) { @@ -37797,10 +38858,9 @@ var ts; _a[4] = "yield", _a[5] = "yield*", _a[7] = "endfinally", - _a - )); + _a)); function transformGenerators(context) { - var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistFunctionDeclaration = context.hoistFunctionDeclaration, hoistVariableDeclaration = context.hoistVariableDeclaration, setSourceMapRange = context.setSourceMapRange, setCommentRange = context.setCommentRange, setNodeEmitFlags = context.setNodeEmitFlags; + var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistFunctionDeclaration = context.hoistFunctionDeclaration, hoistVariableDeclaration = context.hoistVariableDeclaration; var compilerOptions = context.getCompilerOptions(); var languageVersion = ts.getEmitScriptTarget(compilerOptions); var resolver = context.getEmitResolver(); @@ -37834,6 +38894,9 @@ var ts; var withBlockStack; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } if (node.transformFlags & 1024) { currentSourceFile = node; node = ts.visitEachChild(node, visitor, context); @@ -37940,7 +39003,7 @@ var ts; } } function visitFunctionDeclaration(node) { - if (node.asteriskToken && node.emitFlags & 2097152) { + if (node.asteriskToken && ts.getEmitFlags(node) & 2097152) { node = ts.setOriginalNode(ts.createFunctionDeclaration(undefined, undefined, undefined, node.name, undefined, node.parameters, undefined, transformGeneratorFunctionBody(node.body), node), node); } else { @@ -37961,7 +39024,7 @@ var ts; } } function visitFunctionExpression(node) { - if (node.asteriskToken && node.emitFlags & 2097152) { + if (node.asteriskToken && ts.getEmitFlags(node) & 2097152) { node = ts.setOriginalNode(ts.createFunctionExpression(undefined, node.name, undefined, node.parameters, undefined, transformGeneratorFunctionBody(node.body), node), node); } else { @@ -37992,6 +39055,7 @@ var ts; var savedBlocks = blocks; var savedBlockOffsets = blockOffsets; var savedBlockActions = blockActions; + var savedBlockStack = blockStack; var savedLabelOffsets = labelOffsets; var savedLabelExpressions = labelExpressions; var savedNextLabelId = nextLabelId; @@ -38004,6 +39068,7 @@ var ts; blocks = undefined; blockOffsets = undefined; blockActions = undefined; + blockStack = undefined; labelOffsets = undefined; labelExpressions = undefined; nextLabelId = 1; @@ -38022,6 +39087,7 @@ var ts; blocks = savedBlocks; blockOffsets = savedBlockOffsets; blockActions = savedBlockActions; + blockStack = savedBlockStack; labelOffsets = savedLabelOffsets; labelExpressions = savedLabelExpressions; nextLabelId = savedNextLabelId; @@ -38037,7 +39103,7 @@ var ts; return undefined; } else { - if (node.emitFlags & 8388608) { + if (ts.getEmitFlags(node) & 8388608) { return node; } for (var _i = 0, _a = node.declarationList.declarations; _i < _a.length; _i++) { @@ -38705,9 +39771,9 @@ var ts; } return -1; } - function onSubstituteNode(node, isExpression) { - node = previousOnSubstituteNode(node, isExpression); - if (isExpression) { + function onSubstituteNode(emitContext, node) { + node = previousOnSubstituteNode(emitContext, node); + if (emitContext === 1) { return substituteExpression(node); } return node; @@ -38724,11 +39790,11 @@ var ts; if (ts.isIdentifier(original) && original.parent) { var declaration = resolver.getReferencedValueDeclaration(original); if (declaration) { - var name_36 = ts.getProperty(renamedCatchVariableDeclarations, String(ts.getOriginalNodeId(declaration))); - if (name_36) { - var clone_7 = ts.getMutableClone(name_36); - setSourceMapRange(clone_7, node); - setCommentRange(clone_7, node); + var name_37 = ts.getProperty(renamedCatchVariableDeclarations, String(ts.getOriginalNodeId(declaration))); + if (name_37) { + var clone_7 = ts.getMutableClone(name_37); + ts.setSourceMapRange(clone_7, node); + ts.setCommentRange(clone_7, node); return clone_7; } } @@ -39122,7 +40188,7 @@ var ts; var buildResult = buildStatements(); return ts.createCall(ts.createHelperName(currentSourceFile.externalHelpersModuleName, "__generator"), undefined, [ ts.createThis(), - setNodeEmitFlags(ts.createFunctionExpression(undefined, undefined, undefined, [ts.createParameter(state)], undefined, ts.createBlock(buildResult, undefined, buildResult.length > 0)), 4194304) + ts.setEmitFlags(ts.createFunctionExpression(undefined, undefined, undefined, [ts.createParameter(state)], undefined, ts.createBlock(buildResult, undefined, buildResult.length > 0)), 4194304) ]); } function buildStatements() { @@ -39391,7 +40457,7 @@ var ts; var ts; (function (ts) { function transformES6(context) { - var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration, getNodeEmitFlags = context.getNodeEmitFlags, setNodeEmitFlags = context.setNodeEmitFlags, getCommentRange = context.getCommentRange, setCommentRange = context.setCommentRange, getSourceMapRange = context.getSourceMapRange, setSourceMapRange = context.setSourceMapRange, setTokenSourceMapRange = context.setTokenSourceMapRange; + var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration; var resolver = context.getEmitResolver(); var previousOnSubstituteNode = context.onSubstituteNode; var previousOnEmitNode = context.onEmitNode; @@ -39411,6 +40477,9 @@ var ts; var enabledSubstitutions; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } currentSourceFile = node; currentText = node.text; return ts.visitNode(node, visitor, ts.isSourceFile); @@ -39580,7 +40649,7 @@ var ts; enclosingFunction = currentNode; if (currentNode.kind !== 180) { enclosingNonArrowFunction = currentNode; - if (!(currentNode.emitFlags & 2097152)) { + if (!(ts.getEmitFlags(currentNode) & 2097152)) { enclosingNonAsyncFunctionBody = currentNode; } } @@ -39717,15 +40786,15 @@ var ts; } var extendsClauseElement = ts.getClassExtendsHeritageClauseElement(node); var classFunction = ts.createFunctionExpression(undefined, undefined, undefined, extendsClauseElement ? [ts.createParameter("_super")] : [], undefined, transformClassBody(node, extendsClauseElement)); - if (getNodeEmitFlags(node) & 524288) { - setNodeEmitFlags(classFunction, 524288); + if (ts.getEmitFlags(node) & 524288) { + ts.setEmitFlags(classFunction, 524288); } var inner = ts.createPartiallyEmittedExpression(classFunction); inner.end = node.end; - setNodeEmitFlags(inner, 49152); + ts.setEmitFlags(inner, 49152); var outer = ts.createPartiallyEmittedExpression(inner); outer.end = ts.skipTrivia(currentText, node.pos); - setNodeEmitFlags(outer, 49152); + ts.setEmitFlags(outer, 49152); return ts.createParen(ts.createCall(outer, undefined, extendsClauseElement ? [ts.visitNode(extendsClauseElement.expression, visitor, ts.isExpression)] : [])); @@ -39740,14 +40809,14 @@ var ts; var localName = getLocalName(node); var outer = ts.createPartiallyEmittedExpression(localName); outer.end = closingBraceLocation.end; - setNodeEmitFlags(outer, 49152); + ts.setEmitFlags(outer, 49152); var statement = ts.createReturn(outer); statement.pos = closingBraceLocation.pos; - setNodeEmitFlags(statement, 49152 | 12288); + ts.setEmitFlags(statement, 49152 | 12288); statements.push(statement); ts.addRange(statements, endLexicalEnvironment()); var block = ts.createBlock(ts.createNodeArray(statements, node.members), undefined, true); - setNodeEmitFlags(block, 49152); + ts.setEmitFlags(block, 49152); return block; } function addExtendsHelperIfNeeded(statements, node, extendsClauseElement) { @@ -39758,7 +40827,11 @@ var ts; function addConstructor(statements, node, extendsClauseElement) { var constructor = ts.getFirstConstructorWithBody(node); var hasSynthesizedSuper = hasSynthesizedDefaultSuperCall(constructor, extendsClauseElement !== undefined); - statements.push(ts.createFunctionDeclaration(undefined, undefined, undefined, getDeclarationName(node), undefined, transformConstructorParameters(constructor, hasSynthesizedSuper), undefined, transformConstructorBody(constructor, node, extendsClauseElement, hasSynthesizedSuper), constructor || node)); + var constructorFunction = ts.createFunctionDeclaration(undefined, undefined, undefined, getDeclarationName(node), undefined, transformConstructorParameters(constructor, hasSynthesizedSuper), undefined, transformConstructorBody(constructor, node, extendsClauseElement, hasSynthesizedSuper), constructor || node); + if (extendsClauseElement) { + ts.setEmitFlags(constructorFunction, 256); + } + statements.push(constructorFunction); } function transformConstructorParameters(constructor, hasSynthesizedSuper) { if (constructor && !hasSynthesizedSuper) { @@ -39769,33 +40842,98 @@ var ts; function transformConstructorBody(constructor, node, extendsClauseElement, hasSynthesizedSuper) { var statements = []; startLexicalEnvironment(); + var statementOffset = -1; + if (hasSynthesizedSuper) { + statementOffset = 1; + } + else if (constructor) { + statementOffset = ts.addPrologueDirectives(statements, constructor.body.statements, false, visitor); + } if (constructor) { - addCaptureThisForNodeIfNeeded(statements, constructor); addDefaultValueAssignmentsIfNeeded(statements, constructor); addRestParameterIfNeeded(statements, constructor, hasSynthesizedSuper); + ts.Debug.assert(statementOffset >= 0, "statementOffset not initialized correctly!"); + } + var superCaptureStatus = declareOrCaptureOrReturnThisForConstructorIfNeeded(statements, constructor, !!extendsClauseElement, hasSynthesizedSuper, statementOffset); + if (superCaptureStatus === 1 || superCaptureStatus === 2) { + statementOffset++; } - addDefaultSuperCallIfNeeded(statements, constructor, extendsClauseElement, hasSynthesizedSuper); if (constructor) { - var body = saveStateAndInvoke(constructor, hasSynthesizedSuper ? transformConstructorBodyWithSynthesizedSuper : transformConstructorBodyWithoutSynthesizedSuper); + var body = saveStateAndInvoke(constructor, function (constructor) { return ts.visitNodes(constructor.body.statements, visitor, ts.isStatement, statementOffset); }); ts.addRange(statements, body); } + if (extendsClauseElement + && superCaptureStatus !== 2 + && !(constructor && isSufficientlyCoveredByReturnStatements(constructor.body))) { + statements.push(ts.createReturn(ts.createIdentifier("_this"))); + } ts.addRange(statements, endLexicalEnvironment()); var block = ts.createBlock(ts.createNodeArray(statements, constructor ? constructor.body.statements : node.members), constructor ? constructor.body : node, true); if (!constructor) { - setNodeEmitFlags(block, 49152); + ts.setEmitFlags(block, 49152); } return block; } - function transformConstructorBodyWithSynthesizedSuper(node) { - return ts.visitNodes(node.body.statements, visitor, ts.isStatement, 1); - } - function transformConstructorBodyWithoutSynthesizedSuper(node) { - return ts.visitNodes(node.body.statements, visitor, ts.isStatement, 0); + function isSufficientlyCoveredByReturnStatements(statement) { + if (statement.kind === 211) { + return true; + } + else if (statement.kind === 203) { + var ifStatement = statement; + if (ifStatement.elseStatement) { + return isSufficientlyCoveredByReturnStatements(ifStatement.thenStatement) && + isSufficientlyCoveredByReturnStatements(ifStatement.elseStatement); + } + } + else if (statement.kind === 199) { + var lastStatement = ts.lastOrUndefined(statement.statements); + if (lastStatement && isSufficientlyCoveredByReturnStatements(lastStatement)) { + return true; + } + } + return false; } - function addDefaultSuperCallIfNeeded(statements, constructor, extendsClauseElement, hasSynthesizedSuper) { - if (constructor ? hasSynthesizedSuper : extendsClauseElement) { - statements.push(ts.createStatement(ts.createFunctionApply(ts.createIdentifier("_super"), ts.createThis(), ts.createIdentifier("arguments")), extendsClauseElement)); + function declareOrCaptureOrReturnThisForConstructorIfNeeded(statements, ctor, hasExtendsClause, hasSynthesizedSuper, statementOffset) { + if (!hasExtendsClause) { + if (ctor) { + addCaptureThisForNodeIfNeeded(statements, ctor); + } + return 0; + } + if (!ctor) { + statements.push(ts.createReturn(createDefaultSuperCallOrThis())); + return 2; } + if (hasSynthesizedSuper) { + captureThisForNode(statements, ctor, createDefaultSuperCallOrThis()); + enableSubstitutionsForCapturedThis(); + return 1; + } + var firstStatement; + var superCallExpression; + var ctorStatements = ctor.body.statements; + if (statementOffset < ctorStatements.length) { + firstStatement = ctorStatements[statementOffset]; + if (firstStatement.kind === 202 && ts.isSuperCall(firstStatement.expression)) { + var superCall = firstStatement.expression; + superCallExpression = ts.setOriginalNode(saveStateAndInvoke(superCall, visitImmediateSuperCallInBody), superCall); + } + } + if (superCallExpression && statementOffset === ctorStatements.length - 1) { + statements.push(ts.createReturn(superCallExpression)); + return 2; + } + captureThisForNode(statements, ctor, superCallExpression, firstStatement); + if (superCallExpression) { + return 1; + } + return 0; + } + function createDefaultSuperCallOrThis() { + var actualThis = ts.createThis(); + ts.setEmitFlags(actualThis, 128); + var superCall = ts.createFunctionApply(ts.createIdentifier("_super"), actualThis, ts.createIdentifier("arguments")); + return ts.createLogicalOr(superCall, actualThis); } function visitParameter(node) { if (node.dotDotDotToken) { @@ -39820,34 +40958,34 @@ var ts; } for (var _i = 0, _a = node.parameters; _i < _a.length; _i++) { var parameter = _a[_i]; - var name_37 = parameter.name, initializer = parameter.initializer, dotDotDotToken = parameter.dotDotDotToken; + var name_38 = parameter.name, initializer = parameter.initializer, dotDotDotToken = parameter.dotDotDotToken; if (dotDotDotToken) { continue; } - if (ts.isBindingPattern(name_37)) { - addDefaultValueAssignmentForBindingPattern(statements, parameter, name_37, initializer); + if (ts.isBindingPattern(name_38)) { + addDefaultValueAssignmentForBindingPattern(statements, parameter, name_38, initializer); } else if (initializer) { - addDefaultValueAssignmentForInitializer(statements, parameter, name_37, initializer); + addDefaultValueAssignmentForInitializer(statements, parameter, name_38, initializer); } } } function addDefaultValueAssignmentForBindingPattern(statements, parameter, name, initializer) { var temp = ts.getGeneratedNameForNode(parameter); if (name.elements.length > 0) { - statements.push(setNodeEmitFlags(ts.createVariableStatement(undefined, ts.createVariableDeclarationList(ts.flattenParameterDestructuring(context, parameter, temp, visitor))), 8388608)); + statements.push(ts.setEmitFlags(ts.createVariableStatement(undefined, ts.createVariableDeclarationList(ts.flattenParameterDestructuring(context, parameter, temp, visitor))), 8388608)); } else if (initializer) { - statements.push(setNodeEmitFlags(ts.createStatement(ts.createAssignment(temp, ts.visitNode(initializer, visitor, ts.isExpression))), 8388608)); + statements.push(ts.setEmitFlags(ts.createStatement(ts.createAssignment(temp, ts.visitNode(initializer, visitor, ts.isExpression))), 8388608)); } } function addDefaultValueAssignmentForInitializer(statements, parameter, name, initializer) { initializer = ts.visitNode(initializer, visitor, ts.isExpression); - var statement = ts.createIf(ts.createStrictEquality(ts.getSynthesizedClone(name), ts.createVoidZero()), setNodeEmitFlags(ts.createBlock([ - ts.createStatement(ts.createAssignment(setNodeEmitFlags(ts.getMutableClone(name), 1536), setNodeEmitFlags(initializer, 1536 | getNodeEmitFlags(initializer)), parameter)) + var statement = ts.createIf(ts.createStrictEquality(ts.getSynthesizedClone(name), ts.createVoidZero()), ts.setEmitFlags(ts.createBlock([ + ts.createStatement(ts.createAssignment(ts.setEmitFlags(ts.getMutableClone(name), 1536), ts.setEmitFlags(initializer, 1536 | ts.getEmitFlags(initializer)), parameter)) ], parameter), 32 | 1024 | 12288), undefined, parameter); statement.startsOnNewLine = true; - setNodeEmitFlags(statement, 12288 | 1024 | 8388608); + ts.setEmitFlags(statement, 12288 | 1024 | 8388608); statements.push(statement); } function shouldAddRestParameter(node, inConstructorWithSynthesizedSuper) { @@ -39859,11 +40997,11 @@ var ts; return; } var declarationName = ts.getMutableClone(parameter.name); - setNodeEmitFlags(declarationName, 1536); + ts.setEmitFlags(declarationName, 1536); var expressionName = ts.getSynthesizedClone(parameter.name); var restIndex = node.parameters.length - 1; var temp = ts.createLoopVariable(); - statements.push(setNodeEmitFlags(ts.createVariableStatement(undefined, ts.createVariableDeclarationList([ + statements.push(ts.setEmitFlags(ts.createVariableStatement(undefined, ts.createVariableDeclarationList([ ts.createVariableDeclaration(declarationName, undefined, ts.createArrayLiteral([])) ]), parameter), 8388608)); var forStatement = ts.createFor(ts.createVariableDeclarationList([ @@ -39871,21 +41009,24 @@ var ts; ], parameter), ts.createLessThan(temp, ts.createPropertyAccess(ts.createIdentifier("arguments"), "length"), parameter), ts.createPostfixIncrement(temp, parameter), ts.createBlock([ ts.startOnNewLine(ts.createStatement(ts.createAssignment(ts.createElementAccess(expressionName, ts.createSubtract(temp, ts.createLiteral(restIndex))), ts.createElementAccess(ts.createIdentifier("arguments"), temp)), parameter)) ])); - setNodeEmitFlags(forStatement, 8388608); + ts.setEmitFlags(forStatement, 8388608); ts.startOnNewLine(forStatement); statements.push(forStatement); } function addCaptureThisForNodeIfNeeded(statements, node) { if (node.transformFlags & 16384 && node.kind !== 180) { - enableSubstitutionsForCapturedThis(); - var captureThisStatement = ts.createVariableStatement(undefined, ts.createVariableDeclarationList([ - ts.createVariableDeclaration("_this", undefined, ts.createThis()) - ])); - setNodeEmitFlags(captureThisStatement, 49152 | 8388608); - setSourceMapRange(captureThisStatement, node); - statements.push(captureThisStatement); + captureThisForNode(statements, node, ts.createThis()); } } + function captureThisForNode(statements, node, initializer, originalStatement) { + enableSubstitutionsForCapturedThis(); + var captureThisStatement = ts.createVariableStatement(undefined, ts.createVariableDeclarationList([ + ts.createVariableDeclaration("_this", undefined, initializer) + ]), originalStatement); + ts.setEmitFlags(captureThisStatement, 49152 | 8388608); + ts.setSourceMapRange(captureThisStatement, node); + statements.push(captureThisStatement); + } function addClassMembers(statements, node) { for (var _i = 0, _a = node.members; _i < _a.length; _i++) { var member = _a[_i]; @@ -39915,43 +41056,43 @@ var ts; return ts.createEmptyStatement(member); } function transformClassMethodDeclarationToStatement(receiver, member) { - var commentRange = getCommentRange(member); - var sourceMapRange = getSourceMapRange(member); + var commentRange = ts.getCommentRange(member); + var sourceMapRange = ts.getSourceMapRange(member); var func = transformFunctionLikeToExpression(member, member, undefined); - setNodeEmitFlags(func, 49152); - setSourceMapRange(func, sourceMapRange); + ts.setEmitFlags(func, 49152); + ts.setSourceMapRange(func, sourceMapRange); var statement = ts.createStatement(ts.createAssignment(ts.createMemberAccessForPropertyName(receiver, ts.visitNode(member.name, visitor, ts.isPropertyName), member.name), func), member); ts.setOriginalNode(statement, member); - setCommentRange(statement, commentRange); - setNodeEmitFlags(statement, 1536); + ts.setCommentRange(statement, commentRange); + ts.setEmitFlags(statement, 1536); return statement; } function transformAccessorsToStatement(receiver, accessors) { - var statement = ts.createStatement(transformAccessorsToExpression(receiver, accessors, false), getSourceMapRange(accessors.firstAccessor)); - setNodeEmitFlags(statement, 49152); + var statement = ts.createStatement(transformAccessorsToExpression(receiver, accessors, false), ts.getSourceMapRange(accessors.firstAccessor)); + ts.setEmitFlags(statement, 49152); return statement; } function transformAccessorsToExpression(receiver, _a, startsOnNewLine) { var firstAccessor = _a.firstAccessor, getAccessor = _a.getAccessor, setAccessor = _a.setAccessor; var target = ts.getMutableClone(receiver); - setNodeEmitFlags(target, 49152 | 1024); - setSourceMapRange(target, firstAccessor.name); + ts.setEmitFlags(target, 49152 | 1024); + ts.setSourceMapRange(target, firstAccessor.name); var propertyName = ts.createExpressionForPropertyName(ts.visitNode(firstAccessor.name, visitor, ts.isPropertyName)); - setNodeEmitFlags(propertyName, 49152 | 512); - setSourceMapRange(propertyName, firstAccessor.name); + ts.setEmitFlags(propertyName, 49152 | 512); + ts.setSourceMapRange(propertyName, firstAccessor.name); var properties = []; if (getAccessor) { var getterFunction = transformFunctionLikeToExpression(getAccessor, undefined, undefined); - setSourceMapRange(getterFunction, getSourceMapRange(getAccessor)); + ts.setSourceMapRange(getterFunction, ts.getSourceMapRange(getAccessor)); var getter = ts.createPropertyAssignment("get", getterFunction); - setCommentRange(getter, getCommentRange(getAccessor)); + ts.setCommentRange(getter, ts.getCommentRange(getAccessor)); properties.push(getter); } if (setAccessor) { var setterFunction = transformFunctionLikeToExpression(setAccessor, undefined, undefined); - setSourceMapRange(setterFunction, getSourceMapRange(setAccessor)); + ts.setSourceMapRange(setterFunction, ts.getSourceMapRange(setAccessor)); var setter = ts.createPropertyAssignment("set", setterFunction); - setCommentRange(setter, getCommentRange(setAccessor)); + ts.setCommentRange(setter, ts.getCommentRange(setAccessor)); properties.push(setter); } properties.push(ts.createPropertyAssignment("enumerable", ts.createLiteral(true)), ts.createPropertyAssignment("configurable", ts.createLiteral(true))); @@ -39970,7 +41111,7 @@ var ts; enableSubstitutionsForCapturedThis(); } var func = transformFunctionLikeToExpression(node, node, undefined); - setNodeEmitFlags(func, 256); + ts.setEmitFlags(func, 256); return func; } function visitFunctionExpression(node) { @@ -40027,7 +41168,7 @@ var ts; } var expression = ts.visitNode(body, visitor, ts.isExpression); var returnStatement = ts.createReturn(expression, body); - setNodeEmitFlags(returnStatement, 12288 | 1024 | 32768); + ts.setEmitFlags(returnStatement, 12288 | 1024 | 32768); statements.push(returnStatement); closeBraceLocation = body; } @@ -40038,10 +41179,10 @@ var ts; } var block = ts.createBlock(ts.createNodeArray(statements, statementsLocation), node.body, multiLine); if (!multiLine && singleLine) { - setNodeEmitFlags(block, 32); + ts.setEmitFlags(block, 32); } if (closeBraceLocation) { - setTokenSourceMapRange(block, 16, closeBraceLocation); + ts.setTokenSourceMapRange(block, 16, closeBraceLocation); } ts.setOriginalNode(block, node.body); return block; @@ -40082,7 +41223,7 @@ var ts; assignment = ts.flattenVariableDestructuringToExpression(context, decl, hoistVariableDeclaration, undefined, visitor); } else { - assignment = ts.createBinary(decl.name, 56, decl.initializer); + assignment = ts.createBinary(decl.name, 56, ts.visitNode(decl.initializer, visitor, ts.isExpression)); } (assignments || (assignments = [])).push(assignment); } @@ -40105,13 +41246,13 @@ var ts; : visitVariableDeclaration)); var declarationList = ts.createVariableDeclarationList(declarations, node); ts.setOriginalNode(declarationList, node); - setCommentRange(declarationList, node); + ts.setCommentRange(declarationList, node); if (node.transformFlags & 2097152 && (ts.isBindingPattern(node.declarations[0].name) || ts.isBindingPattern(ts.lastOrUndefined(node.declarations).name))) { var firstDeclaration = ts.firstOrUndefined(declarations); var lastDeclaration = ts.lastOrUndefined(declarations); - setSourceMapRange(declarationList, ts.createRange(firstDeclaration.pos, lastDeclaration.end)); + ts.setSourceMapRange(declarationList, ts.createRange(firstDeclaration.pos, lastDeclaration.end)); } return declarationList; } @@ -40206,7 +41347,7 @@ var ts; ts.setOriginalNode(declarationList, initializer); var firstDeclaration = declarations[0]; var lastDeclaration = ts.lastOrUndefined(declarations); - setSourceMapRange(declarationList, ts.createRange(firstDeclaration.pos, lastDeclaration.end)); + ts.setSourceMapRange(declarationList, ts.createRange(firstDeclaration.pos, lastDeclaration.end)); statements.push(ts.createVariableStatement(undefined, declarationList)); } else { @@ -40241,14 +41382,14 @@ var ts; statements.push(statement); } } - setNodeEmitFlags(expression, 1536 | getNodeEmitFlags(expression)); + ts.setEmitFlags(expression, 1536 | ts.getEmitFlags(expression)); var body = ts.createBlock(ts.createNodeArray(statements, statementsLocation), bodyLocation); - setNodeEmitFlags(body, 1536 | 12288); + ts.setEmitFlags(body, 1536 | 12288); var forStatement = ts.createFor(ts.createVariableDeclarationList([ ts.createVariableDeclaration(counter, undefined, ts.createLiteral(0), ts.moveRangePos(node.expression, -1)), ts.createVariableDeclaration(rhsReference, undefined, expression, node.expression) ], node.expression), ts.createLessThan(counter, ts.createPropertyAccess(rhsReference, "length"), node.expression), ts.createPostfixIncrement(counter, node.expression), body, node); - setNodeEmitFlags(forStatement, 8192); + ts.setEmitFlags(forStatement, 8192); return forStatement; } function visitObjectLiteralExpression(node) { @@ -40266,7 +41407,7 @@ var ts; ts.Debug.assert(numInitialProperties !== numProperties); var temp = ts.createTempVariable(hoistVariableDeclaration); var expressions = []; - var assignment = ts.createAssignment(temp, setNodeEmitFlags(ts.createObjectLiteral(ts.visitNodes(properties, visitor, ts.isObjectLiteralElementLike, 0, numInitialProperties), undefined, node.multiLine), 524288)); + var assignment = ts.createAssignment(temp, ts.setEmitFlags(ts.createObjectLiteral(ts.visitNodes(properties, visitor, ts.isObjectLiteralElementLike, 0, numInitialProperties), undefined, node.multiLine), 524288)); if (node.multiLine) { assignment.startsOnNewLine = true; } @@ -40355,7 +41496,7 @@ var ts; loopBody = ts.createBlock([loopBody], undefined, true); } var isAsyncBlockContainingAwait = enclosingNonArrowFunction - && (enclosingNonArrowFunction.emitFlags & 2097152) !== 0 + && (ts.getEmitFlags(enclosingNonArrowFunction) & 2097152) !== 0 && (node.statement.transformFlags & 4194304) !== 0; var loopBodyFlags = 0; if (currentState.containsLexicalThis) { @@ -40365,7 +41506,7 @@ var ts; loopBodyFlags |= 2097152; } var convertedLoopVariable = ts.createVariableStatement(undefined, ts.createVariableDeclarationList([ - ts.createVariableDeclaration(functionName, undefined, setNodeEmitFlags(ts.createFunctionExpression(isAsyncBlockContainingAwait ? ts.createToken(37) : undefined, undefined, undefined, loopParameters, undefined, loopBody), loopBodyFlags)) + ts.createVariableDeclaration(functionName, undefined, ts.setEmitFlags(ts.createFunctionExpression(isAsyncBlockContainingAwait ? ts.createToken(37) : undefined, undefined, undefined, loopParameters, undefined, loopBody), loopBodyFlags)) ])); var statements = [convertedLoopVariable]; var extraVariableDeclarations; @@ -40393,8 +41534,8 @@ var ts; if (!extraVariableDeclarations) { extraVariableDeclarations = []; } - for (var name_38 in currentState.hoistedLocalVariables) { - var identifier = currentState.hoistedLocalVariables[name_38]; + for (var _b = 0, _c = currentState.hoistedLocalVariables; _b < _c.length; _b++) { + var identifier = _c[_b]; extraVariableDeclarations.push(ts.createVariableDeclaration(identifier)); } } @@ -40403,8 +41544,8 @@ var ts; if (!extraVariableDeclarations) { extraVariableDeclarations = []; } - for (var _b = 0, loopOutParameters_1 = loopOutParameters; _b < loopOutParameters_1.length; _b++) { - var outParam = loopOutParameters_1[_b]; + for (var _d = 0, loopOutParameters_1 = loopOutParameters; _d < loopOutParameters_1.length; _d++) { + var outParam = loopOutParameters_1[_d]; extraVariableDeclarations.push(ts.createVariableDeclaration(outParam.outParamName)); } } @@ -40582,7 +41723,7 @@ var ts; function visitMethodDeclaration(node) { ts.Debug.assert(!ts.isComputedPropertyName(node.name)); var functionExpression = transformFunctionLikeToExpression(node, ts.moveRangePos(node, -1), undefined); - setNodeEmitFlags(functionExpression, 16384 | getNodeEmitFlags(functionExpression)); + ts.setEmitFlags(functionExpression, 16384 | ts.getEmitFlags(functionExpression)); return ts.createPropertyAssignment(node.name, functionExpression, node); } function visitShorthandPropertyAssignment(node) { @@ -40595,13 +41736,32 @@ var ts; return transformAndSpreadElements(node.elements, true, node.multiLine, node.elements.hasTrailingComma); } function visitCallExpression(node) { + return visitCallExpressionWithPotentialCapturedThisAssignment(node, true); + } + function visitImmediateSuperCallInBody(node) { + return visitCallExpressionWithPotentialCapturedThisAssignment(node, false); + } + function visitCallExpressionWithPotentialCapturedThisAssignment(node, assignToCapturedThis) { var _a = ts.createCallBinding(node.expression, hoistVariableDeclaration), target = _a.target, thisArg = _a.thisArg; + if (node.expression.kind === 95) { + ts.setEmitFlags(thisArg, 128); + } + var resultingCall; if (node.transformFlags & 262144) { - return ts.createFunctionApply(ts.visitNode(target, visitor, ts.isExpression), ts.visitNode(thisArg, visitor, ts.isExpression), transformAndSpreadElements(node.arguments, false, false, false)); + resultingCall = ts.createFunctionApply(ts.visitNode(target, visitor, ts.isExpression), ts.visitNode(thisArg, visitor, ts.isExpression), transformAndSpreadElements(node.arguments, false, false, false)); } else { - return ts.createFunctionCall(ts.visitNode(target, visitor, ts.isExpression), ts.visitNode(thisArg, visitor, ts.isExpression), ts.visitNodes(node.arguments, visitor, ts.isExpression), node); + resultingCall = ts.createFunctionCall(ts.visitNode(target, visitor, ts.isExpression), ts.visitNode(thisArg, visitor, ts.isExpression), ts.visitNodes(node.arguments, visitor, ts.isExpression), node); + } + if (node.expression.kind === 95) { + var actualThis = ts.createThis(); + ts.setEmitFlags(actualThis, 128); + var initializer = ts.createLogicalOr(resultingCall, actualThis); + return assignToCapturedThis + ? ts.createAssignment(ts.createIdentifier("_this"), initializer) + : initializer; } + return resultingCall; } function visitNewExpression(node) { ts.Debug.assert((node.transformFlags & 262144) !== 0); @@ -40721,12 +41881,12 @@ var ts; clone.statements = ts.createNodeArray(statements, node.statements); return clone; } - function onEmitNode(node, emit) { + function onEmitNode(emitContext, node, emitCallback) { var savedEnclosingFunction = enclosingFunction; if (enabledSubstitutions & 1 && ts.isFunctionLike(node)) { enclosingFunction = node; } - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); enclosingFunction = savedEnclosingFunction; } function enableSubstitutionsForBlockScopedBindings() { @@ -40748,9 +41908,9 @@ var ts; context.enableEmitNotification(220); } } - function onSubstituteNode(node, isExpression) { - node = previousOnSubstituteNode(node, isExpression); - if (isExpression) { + function onSubstituteNode(emitContext, node) { + node = previousOnSubstituteNode(emitContext, node); + if (emitContext === 1) { return substituteExpression(node); } if (ts.isIdentifier(node)) { @@ -40800,7 +41960,7 @@ var ts; function substituteThisKeyword(node) { if (enabledSubstitutions & 1 && enclosingFunction - && enclosingFunction.emitFlags & 256) { + && ts.getEmitFlags(enclosingFunction) & 256) { return ts.createIdentifier("_this", node); } return node; @@ -40811,7 +41971,7 @@ var ts; function getDeclarationName(node, allowComments, allowSourceMaps, emitFlags) { if (node.name && !ts.isGeneratedIdentifier(node.name)) { var name_39 = ts.getMutableClone(node.name); - emitFlags |= getNodeEmitFlags(node.name); + emitFlags |= ts.getEmitFlags(node.name); if (!allowSourceMaps) { emitFlags |= 1536; } @@ -40819,7 +41979,7 @@ var ts; emitFlags |= 49152; } if (emitFlags) { - setNodeEmitFlags(name_39, emitFlags); + ts.setEmitFlags(name_39, emitFlags); } return name_39; } @@ -40868,8 +42028,7 @@ var ts; _a[ts.ModuleKind.CommonJS] = ts.transformModule, _a[ts.ModuleKind.UMD] = ts.transformModule, _a[ts.ModuleKind.None] = ts.transformModule, - _a - )); + _a)); function getTransformers(compilerOptions) { var jsx = compilerOptions.jsx; var languageVersion = ts.getEmitScriptTarget(compilerOptions); @@ -40888,18 +42047,10 @@ var ts; return transformers; } ts.getTransformers = getTransformers; - var nextTransformId = 1; function transformFiles(resolver, host, sourceFiles, transformers) { - var transformId = nextTransformId; - nextTransformId++; - var tokenSourceMapRanges = ts.createMap(); var lexicalEnvironmentVariableDeclarationsStack = []; var lexicalEnvironmentFunctionDeclarationsStack = []; var enabledSyntaxKindFeatures = new Array(289); - var parseTreeNodesWithAnnotations = []; - var lastTokenSourceMapRangeNode; - var lastTokenSourceMapRangeToken; - var lastTokenSourceMapRange; var lexicalEnvironmentStackOffset = 0; var hoistedVariableDeclarations; var hoistedFunctionDeclarations; @@ -40908,47 +42059,24 @@ var ts; getCompilerOptions: function () { return host.getCompilerOptions(); }, getEmitResolver: function () { return resolver; }, getEmitHost: function () { return host; }, - getNodeEmitFlags: getNodeEmitFlags, - setNodeEmitFlags: setNodeEmitFlags, - getSourceMapRange: getSourceMapRange, - setSourceMapRange: setSourceMapRange, - getTokenSourceMapRange: getTokenSourceMapRange, - setTokenSourceMapRange: setTokenSourceMapRange, - getCommentRange: getCommentRange, - setCommentRange: setCommentRange, hoistVariableDeclaration: hoistVariableDeclaration, hoistFunctionDeclaration: hoistFunctionDeclaration, startLexicalEnvironment: startLexicalEnvironment, endLexicalEnvironment: endLexicalEnvironment, - onSubstituteNode: onSubstituteNode, + onSubstituteNode: function (emitContext, node) { return node; }, enableSubstitution: enableSubstitution, isSubstitutionEnabled: isSubstitutionEnabled, - onEmitNode: onEmitNode, + onEmitNode: function (node, emitContext, emitCallback) { return emitCallback(node, emitContext); }, enableEmitNotification: enableEmitNotification, isEmitNotificationEnabled: isEmitNotificationEnabled }; - var transformation = chain.apply(void 0, transformers)(context); + var transformation = ts.chain.apply(void 0, transformers)(context); var transformed = ts.map(sourceFiles, transformSourceFile); lexicalEnvironmentDisabled = true; return { - getSourceFiles: function () { return transformed; }, - getTokenSourceMapRange: getTokenSourceMapRange, - isSubstitutionEnabled: isSubstitutionEnabled, - isEmitNotificationEnabled: isEmitNotificationEnabled, - onSubstituteNode: context.onSubstituteNode, - onEmitNode: context.onEmitNode, - dispose: function () { - for (var _i = 0, parseTreeNodesWithAnnotations_1 = parseTreeNodesWithAnnotations; _i < parseTreeNodesWithAnnotations_1.length; _i++) { - var node = parseTreeNodesWithAnnotations_1[_i]; - if (node.transformId === transformId) { - node.transformId = 0; - node.emitFlags = 0; - node.commentRange = undefined; - node.sourceMapRange = undefined; - } - } - parseTreeNodesWithAnnotations.length = 0; - } + transformed: transformed, + emitNodeWithSubstitution: emitNodeWithSubstitution, + emitNodeWithNotification: emitNodeWithNotification }; function transformSourceFile(sourceFile) { if (ts.isDeclarationFile(sourceFile)) { @@ -40960,75 +42088,37 @@ var ts; enabledSyntaxKindFeatures[kind] |= 1; } function isSubstitutionEnabled(node) { - return (enabledSyntaxKindFeatures[node.kind] & 1) !== 0; + return (enabledSyntaxKindFeatures[node.kind] & 1) !== 0 + && (ts.getEmitFlags(node) & 128) === 0; } - function onSubstituteNode(node, isExpression) { - return node; + function emitNodeWithSubstitution(emitContext, node, emitCallback) { + if (node) { + if (isSubstitutionEnabled(node)) { + var substitute = context.onSubstituteNode(emitContext, node); + if (substitute && substitute !== node) { + emitCallback(emitContext, substitute); + return; + } + } + emitCallback(emitContext, node); + } } function enableEmitNotification(kind) { enabledSyntaxKindFeatures[kind] |= 2; } function isEmitNotificationEnabled(node) { return (enabledSyntaxKindFeatures[node.kind] & 2) !== 0 - || (getNodeEmitFlags(node) & 64) !== 0; - } - function onEmitNode(node, emit) { - emit(node); - } - function beforeSetAnnotation(node) { - if ((node.flags & 8) === 0 && node.transformId !== transformId) { - parseTreeNodesWithAnnotations.push(node); - node.transformId = transformId; - } - } - function getNodeEmitFlags(node) { - return node.emitFlags; + || (ts.getEmitFlags(node) & 64) !== 0; } - function setNodeEmitFlags(node, emitFlags) { - beforeSetAnnotation(node); - node.emitFlags = emitFlags; - return node; - } - function getSourceMapRange(node) { - return node.sourceMapRange || node; - } - function setSourceMapRange(node, range) { - beforeSetAnnotation(node); - node.sourceMapRange = range; - return node; - } - function getTokenSourceMapRange(node, token) { - if (lastTokenSourceMapRangeNode === node && lastTokenSourceMapRangeToken === token) { - return lastTokenSourceMapRange; - } - var range; - var current = node; - while (current) { - range = current.id ? tokenSourceMapRanges[current.id + "-" + token] : undefined; - if (range !== undefined) { - break; + function emitNodeWithNotification(emitContext, node, emitCallback) { + if (node) { + if (isEmitNotificationEnabled(node)) { + context.onEmitNode(emitContext, node, emitCallback); + } + else { + emitCallback(emitContext, node); } - current = current.original; } - lastTokenSourceMapRangeNode = node; - lastTokenSourceMapRangeToken = token; - lastTokenSourceMapRange = range; - return range; - } - function setTokenSourceMapRange(node, token, range) { - lastTokenSourceMapRangeNode = node; - lastTokenSourceMapRangeToken = token; - lastTokenSourceMapRange = range; - tokenSourceMapRanges[ts.getNodeId(node) + "-" + token] = range; - return node; - } - function getCommentRange(node) { - return node.commentRange || node; - } - function setCommentRange(node, range) { - beforeSetAnnotation(node); - node.commentRange = range; - return node; } function hoistVariableDeclaration(name) { ts.Debug.assert(!lexicalEnvironmentDisabled, "Cannot modify the lexical environment during the print phase."); @@ -41081,93 +42171,10 @@ var ts; } } ts.transformFiles = transformFiles; - function chain(a, b, c, d, e) { - if (e) { - var args_3 = []; - for (var i = 0; i < arguments.length; i++) { - args_3[i] = arguments[i]; - } - return function (t) { return compose.apply(void 0, ts.map(args_3, function (f) { return f(t); })); }; - } - else if (d) { - return function (t) { return compose(a(t), b(t), c(t), d(t)); }; - } - else if (c) { - return function (t) { return compose(a(t), b(t), c(t)); }; - } - else if (b) { - return function (t) { return compose(a(t), b(t)); }; - } - else if (a) { - return function (t) { return compose(a(t)); }; - } - else { - return function (t) { return function (u) { return u; }; }; - } - } - function compose(a, b, c, d, e) { - if (e) { - var args_4 = []; - for (var i = 0; i < arguments.length; i++) { - args_4[i] = arguments[i]; - } - return function (t) { return ts.reduceLeft(args_4, function (u, f) { return f(u); }, t); }; - } - else if (d) { - return function (t) { return d(c(b(a(t)))); }; - } - else if (c) { - return function (t) { return c(b(a(t))); }; - } - else if (b) { - return function (t) { return b(a(t)); }; - } - else if (a) { - return function (t) { return a(t); }; - } - else { - return function (t) { return t; }; - } - } var _a; })(ts || (ts = {})); var ts; (function (ts) { - function createSourceMapWriter(host, writer) { - var compilerOptions = host.getCompilerOptions(); - if (compilerOptions.sourceMap || compilerOptions.inlineSourceMap) { - if (compilerOptions.extendedDiagnostics) { - return createSourceMapWriterWithExtendedDiagnostics(host, writer); - } - return createSourceMapWriterWorker(host, writer); - } - else { - return getNullSourceMapWriter(); - } - } - ts.createSourceMapWriter = createSourceMapWriter; - var nullSourceMapWriter; - function getNullSourceMapWriter() { - if (nullSourceMapWriter === undefined) { - nullSourceMapWriter = { - initialize: function (filePath, sourceMapFilePath, sourceFiles, isBundledEmit) { }, - reset: function () { }, - getSourceMapData: function () { return undefined; }, - setSourceFile: function (sourceFile) { }, - emitPos: function (pos) { }, - emitStart: function (range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { }, - emitEnd: function (range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { }, - emitTokenStart: function (token, pos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { return -1; }, - emitTokenEnd: function (token, end, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { return -1; }, - changeEmitSourcePos: function () { }, - stopOverridingSpan: function () { }, - getText: function () { return undefined; }, - getSourceMappingURL: function () { return undefined; } - }; - } - return nullSourceMapWriter; - } - ts.getNullSourceMapWriter = getNullSourceMapWriter; var defaultLastEncodedSourceMapSpan = { emittedLine: 1, emittedColumn: 1, @@ -41175,42 +42182,38 @@ var ts; sourceColumn: 1, sourceIndex: 0 }; - function createSourceMapWriterWorker(host, writer) { + function createSourceMapWriter(host, writer) { var compilerOptions = host.getCompilerOptions(); var extendedDiagnostics = compilerOptions.extendedDiagnostics; var currentSourceFile; var currentSourceText; var sourceMapDir; - var stopOverridingSpan = false; - var modifyLastSourcePos = false; var sourceMapSourceIndex; var lastRecordedSourceMapSpan; var lastEncodedSourceMapSpan; var lastEncodedNameIndex; var sourceMapData; - var disableDepth; + var disabled = !(compilerOptions.sourceMap || compilerOptions.inlineSourceMap); return { initialize: initialize, reset: reset, getSourceMapData: function () { return sourceMapData; }, setSourceFile: setSourceFile, emitPos: emitPos, - emitStart: emitStart, - emitEnd: emitEnd, - emitTokenStart: emitTokenStart, - emitTokenEnd: emitTokenEnd, - changeEmitSourcePos: changeEmitSourcePos, - stopOverridingSpan: function () { return stopOverridingSpan = true; }, + emitNodeWithSourceMap: emitNodeWithSourceMap, + emitTokenWithSourceMap: emitTokenWithSourceMap, getText: getText, getSourceMappingURL: getSourceMappingURL }; function initialize(filePath, sourceMapFilePath, sourceFiles, isBundledEmit) { + if (disabled) { + return; + } if (sourceMapData) { reset(); } currentSourceFile = undefined; currentSourceText = undefined; - disableDepth = 0; sourceMapSourceIndex = -1; lastRecordedSourceMapSpan = undefined; lastEncodedSourceMapSpan = defaultLastEncodedSourceMapSpan; @@ -41250,6 +42253,9 @@ var ts; } } function reset() { + if (disabled) { + return; + } currentSourceFile = undefined; sourceMapDir = undefined; sourceMapSourceIndex = undefined; @@ -41257,38 +42263,6 @@ var ts; lastEncodedSourceMapSpan = undefined; lastEncodedNameIndex = undefined; sourceMapData = undefined; - disableDepth = 0; - } - function enable() { - if (disableDepth > 0) { - disableDepth--; - } - } - function disable() { - disableDepth++; - } - function updateLastEncodedAndRecordedSpans() { - if (modifyLastSourcePos) { - modifyLastSourcePos = false; - lastRecordedSourceMapSpan.emittedLine = lastEncodedSourceMapSpan.emittedLine; - lastRecordedSourceMapSpan.emittedColumn = lastEncodedSourceMapSpan.emittedColumn; - sourceMapData.sourceMapDecodedMappings.pop(); - lastEncodedSourceMapSpan = sourceMapData.sourceMapDecodedMappings.length ? - sourceMapData.sourceMapDecodedMappings[sourceMapData.sourceMapDecodedMappings.length - 1] : - defaultLastEncodedSourceMapSpan; - var sourceMapMappings = sourceMapData.sourceMapMappings; - var lenthToSet = sourceMapMappings.length - 1; - for (; lenthToSet >= 0; lenthToSet--) { - var currentChar = sourceMapMappings.charAt(lenthToSet); - if (currentChar === ",") { - break; - } - if (currentChar === ";" && lenthToSet !== 0 && sourceMapMappings.charAt(lenthToSet - 1) !== ";") { - break; - } - } - sourceMapData.sourceMapMappings = sourceMapMappings.substr(0, Math.max(0, lenthToSet)); - } } function encodeLastRecordedSourceMapSpan() { if (!lastRecordedSourceMapSpan || lastRecordedSourceMapSpan === lastEncodedSourceMapSpan) { @@ -41319,7 +42293,7 @@ var ts; sourceMapData.sourceMapDecodedMappings.push(lastEncodedSourceMapSpan); } function emitPos(pos) { - if (ts.positionIsSynthesized(pos) || disableDepth > 0) { + if (disabled || ts.positionIsSynthesized(pos)) { return; } if (extendedDiagnostics) { @@ -41344,84 +42318,68 @@ var ts; sourceColumn: sourceLinePos.character, sourceIndex: sourceMapSourceIndex }; - stopOverridingSpan = false; } - else if (!stopOverridingSpan) { + else { lastRecordedSourceMapSpan.sourceLine = sourceLinePos.line; lastRecordedSourceMapSpan.sourceColumn = sourceLinePos.character; lastRecordedSourceMapSpan.sourceIndex = sourceMapSourceIndex; } - updateLastEncodedAndRecordedSpans(); if (extendedDiagnostics) { ts.performance.mark("afterSourcemap"); ts.performance.measure("Source Map", "beforeSourcemap", "afterSourcemap"); } } - function getStartPosPastDecorators(range) { - var rangeHasDecorators = !!range.decorators; - return ts.skipTrivia(currentSourceText, rangeHasDecorators ? range.decorators.end : range.pos); - } - function emitStart(range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { - if (contextNode) { - if (!ignoreNodeCallback(contextNode)) { - range = getTextRangeCallback(contextNode) || range; - emitPos(getStartPosPastDecorators(range)); - } - if (ignoreChildrenCallback(contextNode)) { - disable(); - } - } - else { - emitPos(getStartPosPastDecorators(range)); + function emitNodeWithSourceMap(emitContext, node, emitCallback) { + if (disabled) { + return emitCallback(emitContext, node); } - } - function emitEnd(range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { - if (contextNode) { - if (ignoreChildrenCallback(contextNode)) { - enable(); - } - if (!ignoreNodeCallback(contextNode)) { - range = getTextRangeCallback(contextNode) || range; - emitPos(range.end); + if (node) { + var emitNode = node.emitNode; + var emitFlags = emitNode && emitNode.flags; + var _a = emitNode && emitNode.sourceMapRange || node, pos = _a.pos, end = _a.end; + if (node.kind !== 287 + && (emitFlags & 512) === 0 + && pos >= 0) { + emitPos(ts.skipTrivia(currentSourceText, pos)); + } + if (emitFlags & 2048) { + disabled = true; + emitCallback(emitContext, node); + disabled = false; } - } - else { - emitPos(range.end); - } - stopOverridingSpan = false; - } - function emitTokenStart(token, tokenStartPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { - if (contextNode) { - if (ignoreTokenCallback(contextNode, token)) { - return ts.skipTrivia(currentSourceText, tokenStartPos); + else { + emitCallback(emitContext, node); } - var range = getTokenTextRangeCallback(contextNode, token); - if (range) { - tokenStartPos = range.pos; + if (node.kind !== 287 + && (emitFlags & 1024) === 0 + && end >= 0) { + emitPos(end); } } - tokenStartPos = ts.skipTrivia(currentSourceText, tokenStartPos); - emitPos(tokenStartPos); - return tokenStartPos; } - function emitTokenEnd(token, tokenEndPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { - if (contextNode) { - if (ignoreTokenCallback(contextNode, token)) { - return tokenEndPos; - } - var range = getTokenTextRangeCallback(contextNode, token); - if (range) { - tokenEndPos = range.end; - } + function emitTokenWithSourceMap(node, token, tokenPos, emitCallback) { + if (disabled) { + return emitCallback(token, tokenPos); } - emitPos(tokenEndPos); - return tokenEndPos; - } - function changeEmitSourcePos() { - ts.Debug.assert(!modifyLastSourcePos); - modifyLastSourcePos = true; + var emitNode = node && node.emitNode; + var emitFlags = emitNode && emitNode.flags; + var range = emitNode && emitNode.tokenSourceMapRanges && emitNode.tokenSourceMapRanges[token]; + tokenPos = ts.skipTrivia(currentSourceText, range ? range.pos : tokenPos); + if ((emitFlags & 4096) === 0 && tokenPos >= 0) { + emitPos(tokenPos); + } + tokenPos = emitCallback(token, tokenPos); + if (range) + tokenPos = range.end; + if ((emitFlags & 8192) === 0 && tokenPos >= 0) { + emitPos(tokenPos); + } + return tokenPos; } function setSourceFile(sourceFile) { + if (disabled) { + return; + } currentSourceFile = sourceFile; currentSourceText = currentSourceFile.text; var sourcesDirectoryPath = compilerOptions.sourceRoot ? host.getCommonSourceDirectory() : sourceMapDir; @@ -41437,6 +42395,9 @@ var ts; } } function getText() { + if (disabled) { + return; + } encodeLastRecordedSourceMapSpan(); return ts.stringify({ version: 3, @@ -41449,6 +42410,9 @@ var ts; }); } function getSourceMappingURL() { + if (disabled) { + return; + } if (compilerOptions.inlineSourceMap) { var base64SourceMapText = ts.convertToBase64(getText()); return sourceMapData.jsSourceMappingURL = "data:application/json;base64," + base64SourceMapText; @@ -41458,46 +42422,7 @@ var ts; } } } - function createSourceMapWriterWithExtendedDiagnostics(host, writer) { - var _a = createSourceMapWriterWorker(host, writer), initialize = _a.initialize, reset = _a.reset, getSourceMapData = _a.getSourceMapData, setSourceFile = _a.setSourceFile, emitPos = _a.emitPos, emitStart = _a.emitStart, emitEnd = _a.emitEnd, emitTokenStart = _a.emitTokenStart, emitTokenEnd = _a.emitTokenEnd, changeEmitSourcePos = _a.changeEmitSourcePos, stopOverridingSpan = _a.stopOverridingSpan, getText = _a.getText, getSourceMappingURL = _a.getSourceMappingURL; - return { - initialize: initialize, - reset: reset, - getSourceMapData: getSourceMapData, - setSourceFile: setSourceFile, - emitPos: function (pos) { - ts.performance.mark("sourcemapStart"); - emitPos(pos); - ts.performance.measure("sourceMapTime", "sourcemapStart"); - }, - emitStart: function (range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { - ts.performance.mark("emitSourcemap:emitStart"); - emitStart(range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback); - ts.performance.measure("sourceMapTime", "emitSourcemap:emitStart"); - }, - emitEnd: function (range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { - ts.performance.mark("emitSourcemap:emitEnd"); - emitEnd(range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback); - ts.performance.measure("sourceMapTime", "emitSourcemap:emitEnd"); - }, - emitTokenStart: function (token, tokenStartPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { - ts.performance.mark("emitSourcemap:emitTokenStart"); - tokenStartPos = emitTokenStart(token, tokenStartPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback); - ts.performance.measure("sourceMapTime", "emitSourcemap:emitTokenStart"); - return tokenStartPos; - }, - emitTokenEnd: function (token, tokenEndPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { - ts.performance.mark("emitSourcemap:emitTokenEnd"); - tokenEndPos = emitTokenEnd(token, tokenEndPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback); - ts.performance.measure("sourceMapTime", "emitSourcemap:emitTokenEnd"); - return tokenEndPos; - }, - changeEmitSourcePos: changeEmitSourcePos, - stopOverridingSpan: stopOverridingSpan, - getText: getText, - getSourceMappingURL: getSourceMappingURL - }; - } + ts.createSourceMapWriter = createSourceMapWriter; var base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; function base64FormatEncode(inValue) { if (inValue < 64) { @@ -41547,20 +42472,22 @@ var ts; emitBodyWithDetachedComments: emitBodyWithDetachedComments, emitTrailingCommentsOfPosition: emitTrailingCommentsOfPosition }; - function emitNodeWithComments(node, emitCallback) { + function emitNodeWithComments(emitContext, node, emitCallback) { if (disabled) { - emitCallback(node); + emitCallback(emitContext, node); return; } if (node) { - var _a = node.commentRange || node, pos = _a.pos, end = _a.end; - var emitFlags = node.emitFlags; + var _a = ts.getCommentRange(node), pos = _a.pos, end = _a.end; + var emitFlags = ts.getEmitFlags(node); if ((pos < 0 && end < 0) || (pos === end)) { if (emitFlags & 65536) { - disableCommentsAndEmit(node, emitCallback); + disabled = true; + emitCallback(emitContext, node); + disabled = false; } else { - emitCallback(node); + emitCallback(emitContext, node); } } else { @@ -41589,10 +42516,12 @@ var ts; ts.performance.measure("commentTime", "preEmitNodeWithComment"); } if (emitFlags & 65536) { - disableCommentsAndEmit(node, emitCallback); + disabled = true; + emitCallback(emitContext, node); + disabled = false; } else { - emitCallback(node); + emitCallback(emitContext, node); } if (extendedDiagnostics) { ts.performance.mark("beginEmitNodeWithComment"); @@ -41614,7 +42543,7 @@ var ts; ts.performance.mark("preEmitBodyWithDetachedComments"); } var pos = detachedRange.pos, end = detachedRange.end; - var emitFlags = node.emitFlags; + var emitFlags = ts.getEmitFlags(node); var skipLeadingComments = pos < 0 || (emitFlags & 16384) !== 0; var skipTrailingComments = disabled || end < 0 || (emitFlags & 32768) !== 0; if (!skipLeadingComments) { @@ -41623,8 +42552,10 @@ var ts; if (extendedDiagnostics) { ts.performance.measure("commentTime", "preEmitBodyWithDetachedComments"); } - if (emitFlags & 65536) { - disableCommentsAndEmit(node, emitCallback); + if (emitFlags & 65536 && !disabled) { + disabled = true; + emitCallback(node); + disabled = false; } else { emitCallback(node); @@ -41732,16 +42663,6 @@ var ts; currentLineMap = ts.getLineStarts(currentSourceFile); detachedCommentsInfo = undefined; } - function disableCommentsAndEmit(node, emitCallback) { - if (disabled) { - emitCallback(node); - } - else { - disabled = true; - emitCallback(node); - disabled = false; - } - } function hasDetachedComments(pos) { return detachedCommentsInfo !== undefined && ts.lastOrUndefined(detachedCommentsInfo).nodePos === pos; } @@ -41793,11 +42714,11 @@ var ts; return declarationDiagnostics.getDiagnostics(targetSourceFile ? targetSourceFile.fileName : undefined); function getDeclarationDiagnosticsFromFile(_a, sources, isBundledEmit) { var declarationFilePath = _a.declarationFilePath; - emitDeclarations(host, resolver, declarationDiagnostics, declarationFilePath, sources, isBundledEmit); + emitDeclarations(host, resolver, declarationDiagnostics, declarationFilePath, sources, isBundledEmit, false); } } ts.getDeclarationDiagnostics = getDeclarationDiagnostics; - function emitDeclarations(host, resolver, emitterDiagnostics, declarationFilePath, sourceFiles, isBundledEmit) { + function emitDeclarations(host, resolver, emitterDiagnostics, declarationFilePath, sourceFiles, isBundledEmit, emitOnlyDtsFiles) { var newLine = host.getNewLine(); var compilerOptions = host.getCompilerOptions(); var write; @@ -41833,7 +42754,7 @@ var ts; ts.forEach(sourceFile.referencedFiles, function (fileReference) { var referencedFile = ts.tryResolveScriptReference(host, sourceFile, fileReference); if (referencedFile && !ts.contains(emittedReferencedFiles, referencedFile)) { - if (writeReferencePath(referencedFile, !isBundledEmit && !addedGlobalFileReference)) { + if (writeReferencePath(referencedFile, !isBundledEmit && !addedGlobalFileReference, emitOnlyDtsFiles)) { addedGlobalFileReference = true; } emittedReferencedFiles.push(referencedFile); @@ -42018,7 +42939,7 @@ var ts; } else { errorNameNode = declaration.name; - resolver.writeTypeOfDeclaration(declaration, enclosingDeclaration, 2, writer); + resolver.writeTypeOfDeclaration(declaration, enclosingDeclaration, 2 | 1024, writer); errorNameNode = undefined; } } @@ -42030,7 +42951,7 @@ var ts; } else { errorNameNode = signature.name; - resolver.writeReturnTypeOfSignatureDeclaration(signature, enclosingDeclaration, 2, writer); + resolver.writeReturnTypeOfSignatureDeclaration(signature, enclosingDeclaration, 2 | 1024, writer); errorNameNode = undefined; } } @@ -42223,7 +43144,7 @@ var ts; write(tempVarName); write(": "); writer.getSymbolAccessibilityDiagnostic = getDefaultExportAccessibilityDiagnostic; - resolver.writeTypeOfExpression(node.expression, enclosingDeclaration, 2, writer); + resolver.writeTypeOfExpression(node.expression, enclosingDeclaration, 2 | 1024, writer); write(";"); writeLine(); write(node.isExportEquals ? "export = " : "export default "); @@ -42629,7 +43550,7 @@ var ts; } else { writer.getSymbolAccessibilityDiagnostic = getHeritageClauseVisibilityError; - resolver.writeBaseConstructorTypeOfClass(enclosingDeclaration, enclosingDeclaration, 2, writer); + resolver.writeBaseConstructorTypeOfClass(enclosingDeclaration, enclosingDeclaration, 2 | 1024, writer); } function getHeritageClauseVisibilityError(symbolAccessibilityResult) { var diagnosticMessage; @@ -43197,14 +44118,14 @@ var ts; return emitSourceFile(node); } } - function writeReferencePath(referencedFile, addBundledFileReference) { + function writeReferencePath(referencedFile, addBundledFileReference, emitOnlyDtsFiles) { var declFileName; var addedBundledEmitReference = false; if (ts.isDeclarationFile(referencedFile)) { declFileName = referencedFile.fileName; } else { - ts.forEachExpectedEmitFile(host, getDeclFileName, referencedFile); + ts.forEachExpectedEmitFile(host, getDeclFileName, referencedFile, emitOnlyDtsFiles); } if (declFileName) { declFileName = ts.getRelativePathToDirectoryOrUrl(ts.getDirectoryPath(ts.normalizeSlashes(declarationFilePath)), declFileName, host.getCurrentDirectory(), host.getCanonicalFileName, false); @@ -43221,8 +44142,8 @@ var ts; } } } - function writeDeclarationFile(declarationFilePath, sourceFiles, isBundledEmit, host, resolver, emitterDiagnostics) { - var emitDeclarationResult = emitDeclarations(host, resolver, emitterDiagnostics, declarationFilePath, sourceFiles, isBundledEmit); + function writeDeclarationFile(declarationFilePath, sourceFiles, isBundledEmit, host, resolver, emitterDiagnostics, emitOnlyDtsFiles) { + var emitDeclarationResult = emitDeclarations(host, resolver, emitterDiagnostics, declarationFilePath, sourceFiles, isBundledEmit, emitOnlyDtsFiles); var emitSkipped = emitDeclarationResult.reportedDeclarationError || host.isEmitBlocked(declarationFilePath) || host.getCompilerOptions().noEmit; if (!emitSkipped) { var declarationOutput = emitDeclarationResult.referencesOutput @@ -43248,7 +44169,9 @@ var ts; })(ts || (ts = {})); var ts; (function (ts) { - function emitFiles(resolver, host, targetSourceFile) { + var id = function (s) { return s; }; + var nullTransformers = [function (ctx) { return id; }]; + function emitFiles(resolver, host, targetSourceFile, emitOnlyDtsFiles) { var delimiters = createDelimiterMap(); var brackets = createBracketsMap(); var extendsHelper = "\nvar __extends = (this && this.__extends) || function (d, b) {\n for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];\n function __() { this.constructor = d; }\n d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\n};"; @@ -43256,7 +44179,7 @@ var ts; var decorateHelper = "\nvar __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {\n var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;\n if (typeof Reflect === \"object\" && typeof Reflect.decorate === \"function\") r = Reflect.decorate(decorators, target, key, desc);\n else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;\n return c > 3 && r && Object.defineProperty(target, key, r), r;\n};"; var metadataHelper = "\nvar __metadata = (this && this.__metadata) || function (k, v) {\n if (typeof Reflect === \"object\" && typeof Reflect.metadata === \"function\") return Reflect.metadata(k, v);\n};"; var paramHelper = "\nvar __param = (this && this.__param) || function (paramIndex, decorator) {\n return function (target, key) { decorator(target, key, paramIndex); }\n};"; - var awaiterHelper = "\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments)).next());\n });\n};"; + var awaiterHelper = "\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments)).next());\n });\n};"; var generatorHelper = "\nvar __generator = (this && this.__generator) || function (thisArg, body) {\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t;\n return { next: verb(0), \"throw\": verb(1), \"return\": verb(2) };\n function verb(n) { return function (v) { return step([n, v]); }; }\n function step(op) {\n if (f) throw new TypeError(\"Generator is already executing.\");\n while (_) try {\n if (f = 1, y && (t = y[op[0] & 2 ? \"return\" : op[0] ? \"throw\" : \"next\"]) && !(t = t.call(y, op[1])).done) return t;\n if (y = 0, t) op = [0, t.value];\n switch (op[0]) {\n case 0: case 1: t = op; break;\n case 4: _.label++; return { value: op[1], done: false };\n case 5: _.label++; y = op[1]; op = [0]; continue;\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\n default:\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\n if (t[2]) _.ops.pop();\n _.trys.pop(); continue;\n }\n op = body.call(thisArg, _);\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\n }\n};"; var exportStarHelper = "\nfunction __export(m) {\n for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];\n}"; var umdHelper = "\n(function (dependencies, factory) {\n if (typeof module === 'object' && typeof module.exports === 'object') {\n var v = factory(require, exports); if (v !== undefined) module.exports = v;\n }\n else if (typeof define === 'function' && define.amd) {\n define(dependencies, factory);\n }\n})"; @@ -43269,11 +44192,11 @@ var ts; var emittedFilesList = compilerOptions.listEmittedFiles ? [] : undefined; var emitterDiagnostics = ts.createDiagnosticCollection(); var newLine = host.getNewLine(); - var transformers = ts.getTransformers(compilerOptions); + var transformers = emitOnlyDtsFiles ? nullTransformers : ts.getTransformers(compilerOptions); var writer = ts.createTextWriter(newLine); var write = writer.write, writeLine = writer.writeLine, increaseIndent = writer.increaseIndent, decreaseIndent = writer.decreaseIndent; var sourceMap = ts.createSourceMapWriter(host, writer); - var emitStart = sourceMap.emitStart, emitEnd = sourceMap.emitEnd, emitTokenStart = sourceMap.emitTokenStart, emitTokenEnd = sourceMap.emitTokenEnd; + var emitNodeWithSourceMap = sourceMap.emitNodeWithSourceMap, emitTokenWithSourceMap = sourceMap.emitTokenWithSourceMap; var comments = ts.createCommentWriter(host, writer, sourceMap); var emitNodeWithComments = comments.emitNodeWithComments, emitBodyWithDetachedComments = comments.emitBodyWithDetachedComments, emitTrailingCommentsOfPosition = comments.emitTrailingCommentsOfPosition; var nodeIdToGeneratedName; @@ -43290,14 +44213,17 @@ var ts; var awaiterEmitted; var isOwnFileEmit; var emitSkipped = false; + var sourceFiles = ts.getSourceFilesToEmit(host, targetSourceFile); ts.performance.mark("beforeTransform"); - var transformed = ts.transformFiles(resolver, host, ts.getSourceFilesToEmit(host, targetSourceFile), transformers); + var _a = ts.transformFiles(resolver, host, sourceFiles, transformers), transformed = _a.transformed, emitNodeWithSubstitution = _a.emitNodeWithSubstitution, emitNodeWithNotification = _a.emitNodeWithNotification; ts.performance.measure("transformTime", "beforeTransform"); - var getTokenSourceMapRange = transformed.getTokenSourceMapRange, isSubstitutionEnabled = transformed.isSubstitutionEnabled, isEmitNotificationEnabled = transformed.isEmitNotificationEnabled, onSubstituteNode = transformed.onSubstituteNode, onEmitNode = transformed.onEmitNode; ts.performance.mark("beforePrint"); - ts.forEachTransformedEmitFile(host, transformed.getSourceFiles(), emitFile); - transformed.dispose(); + ts.forEachTransformedEmitFile(host, transformed, emitFile, emitOnlyDtsFiles); ts.performance.measure("printTime", "beforePrint"); + for (var _b = 0, sourceFiles_4 = sourceFiles; _b < sourceFiles_4.length; _b++) { + var sourceFile = sourceFiles_4[_b]; + ts.disposeEmitNodes(sourceFile); + } return { emitSkipped: emitSkipped, diagnostics: emitterDiagnostics.getDiagnostics(), @@ -43306,16 +44232,20 @@ var ts; }; function emitFile(jsFilePath, sourceMapFilePath, declarationFilePath, sourceFiles, isBundledEmit) { if (!host.isEmitBlocked(jsFilePath) && !compilerOptions.noEmit) { - printFile(jsFilePath, sourceMapFilePath, sourceFiles, isBundledEmit); + if (!emitOnlyDtsFiles) { + printFile(jsFilePath, sourceMapFilePath, sourceFiles, isBundledEmit); + } } else { emitSkipped = true; } if (declarationFilePath) { - emitSkipped = ts.writeDeclarationFile(declarationFilePath, ts.getOriginalSourceFiles(sourceFiles), isBundledEmit, host, resolver, emitterDiagnostics) || emitSkipped; + emitSkipped = ts.writeDeclarationFile(declarationFilePath, ts.getOriginalSourceFiles(sourceFiles), isBundledEmit, host, resolver, emitterDiagnostics, emitOnlyDtsFiles) || emitSkipped; } if (!emitSkipped && emittedFilesList) { - emittedFilesList.push(jsFilePath); + if (!emitOnlyDtsFiles) { + emittedFilesList.push(jsFilePath); + } if (sourceMapFilePath) { emittedFilesList.push(sourceMapFilePath); } @@ -43331,8 +44261,8 @@ var ts; generatedNameSet = ts.createMap(); isOwnFileEmit = !isBundledEmit; if (isBundledEmit && moduleKind) { - for (var _a = 0, sourceFiles_4 = sourceFiles; _a < sourceFiles_4.length; _a++) { - var sourceFile = sourceFiles_4[_a]; + for (var _a = 0, sourceFiles_5 = sourceFiles; _a < sourceFiles_5.length; _a++) { + var sourceFile = sourceFiles_5[_a]; emitEmitHelpers(sourceFile); } } @@ -43368,73 +44298,61 @@ var ts; currentFileIdentifiers = node.identifiers; sourceMap.setSourceFile(node); comments.setSourceFile(node); - emitNodeWithNotification(node, emitWorker); + pipelineEmitWithNotification(0, node); } function emit(node) { - emitNodeWithNotification(node, emitWithComments); - } - function emitWithComments(node) { - emitNodeWithComments(node, emitWithSourceMap); - } - function emitWithSourceMap(node) { - emitNodeWithSourceMap(node, emitWorker); + pipelineEmitWithNotification(3, node); } function emitIdentifierName(node) { - if (node) { - emitNodeWithNotification(node, emitIdentifierNameWithComments); - } - } - function emitIdentifierNameWithComments(node) { - emitNodeWithComments(node, emitWorker); + pipelineEmitWithNotification(2, node); } function emitExpression(node) { - emitNodeWithNotification(node, emitExpressionWithComments); - } - function emitExpressionWithComments(node) { - emitNodeWithComments(node, emitExpressionWithSourceMap); + pipelineEmitWithNotification(1, node); } - function emitExpressionWithSourceMap(node) { - emitNodeWithSourceMap(node, emitExpressionWorker); + function pipelineEmitWithNotification(emitContext, node) { + emitNodeWithNotification(emitContext, node, pipelineEmitWithComments); } - function emitNodeWithNotification(node, emitCallback) { - if (node) { - if (isEmitNotificationEnabled(node)) { - onEmitNode(node, emitCallback); - } - else { - emitCallback(node); - } + function pipelineEmitWithComments(emitContext, node) { + if (emitContext === 0) { + pipelineEmitWithSourceMap(emitContext, node); + return; } + emitNodeWithComments(emitContext, node, pipelineEmitWithSourceMap); } - function emitNodeWithSourceMap(node, emitCallback) { - if (node) { - emitStart(node, node, shouldSkipLeadingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange); - emitCallback(node); - emitEnd(node, node, shouldSkipTrailingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange); + function pipelineEmitWithSourceMap(emitContext, node) { + if (emitContext === 0 + || emitContext === 2) { + pipelineEmitWithSubstitution(emitContext, node); + return; } + emitNodeWithSourceMap(emitContext, node, pipelineEmitWithSubstitution); } - function getSourceMapRange(node) { - return node.sourceMapRange || node; - } - function shouldSkipLeadingCommentsForNode(node) { - return ts.isNotEmittedStatement(node) - || (node.emitFlags & 16384) !== 0; + function pipelineEmitWithSubstitution(emitContext, node) { + emitNodeWithSubstitution(emitContext, node, pipelineEmitForContext); } - function shouldSkipLeadingSourceMapForNode(node) { - return ts.isNotEmittedStatement(node) - || (node.emitFlags & 512) !== 0; - } - function shouldSkipTrailingSourceMapForNode(node) { - return ts.isNotEmittedStatement(node) - || (node.emitFlags & 1024) !== 0; + function pipelineEmitForContext(emitContext, node) { + switch (emitContext) { + case 0: return pipelineEmitInSourceFileContext(node); + case 2: return pipelineEmitInIdentifierNameContext(node); + case 3: return pipelineEmitInUnspecifiedContext(node); + case 1: return pipelineEmitInExpressionContext(node); + } } - function shouldSkipSourceMapForChildren(node) { - return (node.emitFlags & 2048) !== 0; + function pipelineEmitInSourceFileContext(node) { + var kind = node.kind; + switch (kind) { + case 256: + return emitSourceFile(node); + } } - function emitWorker(node) { - if (tryEmitSubstitute(node, emitWorker, false)) { - return; + function pipelineEmitInIdentifierNameContext(node) { + var kind = node.kind; + switch (kind) { + case 69: + return emitIdentifier(node); } + } + function pipelineEmitInUnspecifiedContext(node) { var kind = node.kind; switch (kind) { case 12: @@ -43461,7 +44379,8 @@ var ts; case 132: case 133: case 137: - return writeTokenNode(node); + writeTokenText(kind); + return; case 139: return emitQualifiedName(node); case 140: @@ -43637,17 +44556,12 @@ var ts; return emitShorthandPropertyAssignment(node); case 255: return emitEnumMember(node); - case 256: - return emitSourceFile(node); } if (ts.isExpression(node)) { - return emitExpressionWorker(node); + return pipelineEmitWithSubstitution(1, node); } } - function emitExpressionWorker(node) { - if (tryEmitSubstitute(node, emitExpressionWorker, true)) { - return; - } + function pipelineEmitInExpressionContext(node) { var kind = node.kind; switch (kind) { case 8: @@ -43663,7 +44577,8 @@ var ts; case 95: case 99: case 97: - return writeTokenNode(node); + writeTokenText(kind); + return; case 170: return emitArrayLiteralExpression(node); case 171: @@ -43741,7 +44656,7 @@ var ts; } } function emitIdentifier(node) { - if (node.emitFlags & 16) { + if (ts.getEmitFlags(node) & 16) { writeLines(umdHelper); } else { @@ -43956,7 +44871,7 @@ var ts; write("{}"); } else { - var indentedFlag = node.emitFlags & 524288; + var indentedFlag = ts.getEmitFlags(node) & 524288; if (indentedFlag) { increaseIndent(); } @@ -43969,21 +44884,18 @@ var ts; } } function emitPropertyAccessExpression(node) { - if (tryEmitConstantValue(node)) { - return; - } var indentBeforeDot = false; var indentAfterDot = false; - if (!(node.emitFlags & 1048576)) { + if (!(ts.getEmitFlags(node) & 1048576)) { var dotRangeStart = node.expression.end; var dotRangeEnd = ts.skipTrivia(currentText, node.expression.end) + 1; var dotToken = { kind: 21, pos: dotRangeStart, end: dotRangeEnd }; indentBeforeDot = needsIndentation(node, node.expression, dotToken); indentAfterDot = needsIndentation(node, dotToken, node.name); } - var shouldEmitDotDot = !indentBeforeDot && needsDotDotForPropertyAccess(node.expression); emitExpression(node.expression); increaseIndentIf(indentBeforeDot); + var shouldEmitDotDot = !indentBeforeDot && needsDotDotForPropertyAccess(node.expression); write(shouldEmitDotDot ? ".." : "."); increaseIndentIf(indentAfterDot); emit(node.name); @@ -43994,15 +44906,14 @@ var ts; var text = getLiteralTextOfNode(expression); return text.indexOf(ts.tokenToString(21)) < 0; } - else { - var constantValue = tryGetConstEnumValue(expression); - return isFinite(constantValue) && Math.floor(constantValue) === constantValue; + else if (ts.isPropertyAccessExpression(expression) || ts.isElementAccessExpression(expression)) { + var constantValue = ts.getConstantValue(expression); + return isFinite(constantValue) + && Math.floor(constantValue) === constantValue + && compilerOptions.removeComments; } } function emitElementAccessExpression(node) { - if (tryEmitConstantValue(node)) { - return; - } emitExpression(node.expression); write("["); emitExpression(node.argumentExpression); @@ -44157,7 +45068,7 @@ var ts; } } function emitBlockStatements(node) { - if (node.emitFlags & 32) { + if (ts.getEmitFlags(node) & 32) { emitList(node, node.statements, 384); } else { @@ -44332,11 +45243,11 @@ var ts; var body = node.body; if (body) { if (ts.isBlock(body)) { - var indentedFlag = node.emitFlags & 524288; + var indentedFlag = ts.getEmitFlags(node) & 524288; if (indentedFlag) { increaseIndent(); } - if (node.emitFlags & 4194304) { + if (ts.getEmitFlags(node) & 4194304) { emitSignatureHead(node); emitBlockFunctionBody(node, body); } @@ -44368,7 +45279,7 @@ var ts; emitWithPrefix(": ", node.type); } function shouldEmitBlockFunctionBodyOnSingleLine(parentNode, body) { - if (body.emitFlags & 32) { + if (ts.getEmitFlags(body) & 32) { return true; } if (body.multiLine) { @@ -44423,7 +45334,7 @@ var ts; emitModifiers(node, node.modifiers); write("class"); emitNodeWithPrefix(" ", node.name, emitIdentifierName); - var indentedFlag = node.emitFlags & 524288; + var indentedFlag = ts.getEmitFlags(node) & 524288; if (indentedFlag) { increaseIndent(); } @@ -44485,7 +45396,7 @@ var ts; emit(body); } function emitModuleBlock(node) { - if (isSingleLineEmptyBlock(node)) { + if (isEmptyBlock(node)) { write("{ }"); } else { @@ -44682,8 +45593,8 @@ var ts; emit(node.name); write(": "); var initializer = node.initializer; - if (!shouldSkipLeadingCommentsForNode(initializer)) { - var commentRange = initializer.commentRange || initializer; + if ((ts.getEmitFlags(initializer) & 16384) === 0) { + var commentRange = ts.getCommentRange(initializer); emitTrailingCommentsOfPosition(commentRange.pos); } emitExpression(initializer); @@ -44731,7 +45642,7 @@ var ts; return statements.length; } function emitHelpers(node) { - var emitFlags = node.emitFlags; + var emitFlags = ts.getEmitFlags(node); var helpersEmitted = false; if (emitFlags & 1) { helpersEmitted = emitEmitHelpers(currentSourceFile); @@ -44786,1177 +45697,557 @@ var ts; if (languageVersion < 2) { writeLines(generatorHelper); } - awaiterEmitted = true; - helpersEmitted = true; - } - if (helpersEmitted) { - writeLine(); - } - return helpersEmitted; - } - function writeLines(text) { - var lines = text.split(/\r\n|\r|\n/g); - for (var i = 0; i < lines.length; i++) { - var line = lines[i]; - if (line.length) { - if (i > 0) { - writeLine(); - } - write(line); - } - } - } - function emitShebang() { - var shebang = ts.getShebang(currentText); - if (shebang) { - write(shebang); - writeLine(); - } - } - function emitModifiers(node, modifiers) { - if (modifiers && modifiers.length) { - emitList(node, modifiers, 256); - write(" "); - } - } - function emitWithPrefix(prefix, node) { - emitNodeWithPrefix(prefix, node, emit); - } - function emitExpressionWithPrefix(prefix, node) { - emitNodeWithPrefix(prefix, node, emitExpression); - } - function emitNodeWithPrefix(prefix, node, emit) { - if (node) { - write(prefix); - emit(node); - } - } - function emitWithSuffix(node, suffix) { - if (node) { - emit(node); - write(suffix); - } - } - function tryEmitSubstitute(node, emitNode, isExpression) { - if (isSubstitutionEnabled(node) && (node.emitFlags & 128) === 0) { - var substitute = onSubstituteNode(node, isExpression); - if (substitute !== node) { - substitute.emitFlags |= 128; - emitNode(substitute); - return true; - } - } - return false; - } - function tryEmitConstantValue(node) { - var constantValue = tryGetConstEnumValue(node); - if (constantValue !== undefined) { - write(String(constantValue)); - if (!compilerOptions.removeComments) { - var propertyName = ts.isPropertyAccessExpression(node) - ? ts.declarationNameToString(node.name) - : getTextOfNode(node.argumentExpression); - write(" /* " + propertyName + " */"); - } - return true; - } - return false; - } - function emitEmbeddedStatement(node) { - if (ts.isBlock(node)) { - write(" "); - emit(node); - } - else { - writeLine(); - increaseIndent(); - emit(node); - decreaseIndent(); - } - } - function emitDecorators(parentNode, decorators) { - emitList(parentNode, decorators, 24577); - } - function emitTypeArguments(parentNode, typeArguments) { - emitList(parentNode, typeArguments, 26960); - } - function emitTypeParameters(parentNode, typeParameters) { - emitList(parentNode, typeParameters, 26960); - } - function emitParameters(parentNode, parameters) { - emitList(parentNode, parameters, 1360); - } - function emitParametersForArrow(parentNode, parameters) { - if (parameters && - parameters.length === 1 && - parameters[0].type === undefined && - parameters[0].pos === parentNode.pos) { - emit(parameters[0]); - } - else { - emitParameters(parentNode, parameters); - } - } - function emitParametersForIndexSignature(parentNode, parameters) { - emitList(parentNode, parameters, 4432); - } - function emitList(parentNode, children, format, start, count) { - emitNodeList(emit, parentNode, children, format, start, count); - } - function emitExpressionList(parentNode, children, format, start, count) { - emitNodeList(emitExpression, parentNode, children, format, start, count); - } - function emitNodeList(emit, parentNode, children, format, start, count) { - if (start === void 0) { start = 0; } - if (count === void 0) { count = children ? children.length - start : 0; } - var isUndefined = children === undefined; - if (isUndefined && format & 8192) { - return; - } - var isEmpty = isUndefined || children.length === 0 || start >= children.length || count === 0; - if (isEmpty && format & 16384) { - return; - } - if (format & 7680) { - write(getOpeningBracket(format)); - } - if (isEmpty) { - if (format & 1) { - writeLine(); - } - else if (format & 128) { - write(" "); - } - } - else { - var mayEmitInterveningComments = (format & 131072) === 0; - var shouldEmitInterveningComments = mayEmitInterveningComments; - if (shouldWriteLeadingLineTerminator(parentNode, children, format)) { - writeLine(); - shouldEmitInterveningComments = false; - } - else if (format & 128) { - write(" "); - } - if (format & 64) { - increaseIndent(); - } - var previousSibling = void 0; - var shouldDecreaseIndentAfterEmit = void 0; - var delimiter = getDelimiter(format); - for (var i = 0; i < count; i++) { - var child = children[start + i]; - if (previousSibling) { - write(delimiter); - if (shouldWriteSeparatingLineTerminator(previousSibling, child, format)) { - if ((format & (3 | 64)) === 0) { - increaseIndent(); - shouldDecreaseIndentAfterEmit = true; - } - writeLine(); - shouldEmitInterveningComments = false; - } - else if (previousSibling && format & 256) { - write(" "); - } - } - if (shouldEmitInterveningComments) { - var commentRange = child.commentRange || child; - emitTrailingCommentsOfPosition(commentRange.pos); - } - else { - shouldEmitInterveningComments = mayEmitInterveningComments; - } - emit(child); - if (shouldDecreaseIndentAfterEmit) { - decreaseIndent(); - shouldDecreaseIndentAfterEmit = false; - } - previousSibling = child; - } - var hasTrailingComma = (format & 32) && children.hasTrailingComma; - if (format & 16 && hasTrailingComma) { - write(","); - } - if (format & 64) { - decreaseIndent(); - } - if (shouldWriteClosingLineTerminator(parentNode, children, format)) { - writeLine(); - } - else if (format & 128) { - write(" "); - } - } - if (format & 7680) { - write(getClosingBracket(format)); - } - } - function writeIfAny(nodes, text) { - if (nodes && nodes.length > 0) { - write(text); - } - } - function writeIfPresent(node, text) { - if (node !== undefined) { - write(text); - } - } - function writeToken(token, pos, contextNode) { - var tokenStartPos = emitTokenStart(token, pos, contextNode, shouldSkipLeadingSourceMapForToken, getTokenSourceMapRange); - var tokenEndPos = writeTokenText(token, tokenStartPos); - return emitTokenEnd(token, tokenEndPos, contextNode, shouldSkipTrailingSourceMapForToken, getTokenSourceMapRange); - } - function shouldSkipLeadingSourceMapForToken(contextNode) { - return (contextNode.emitFlags & 4096) !== 0; - } - function shouldSkipTrailingSourceMapForToken(contextNode) { - return (contextNode.emitFlags & 8192) !== 0; - } - function writeTokenText(token, pos) { - var tokenString = ts.tokenToString(token); - write(tokenString); - return ts.positionIsSynthesized(pos) ? -1 : pos + tokenString.length; - } - function writeTokenNode(node) { - if (node) { - emitStart(node, node, shouldSkipLeadingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange); - writeTokenText(node.kind); - emitEnd(node, node, shouldSkipTrailingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange); - } - } - function increaseIndentIf(value, valueToWriteWhenNotIndenting) { - if (value) { - increaseIndent(); - writeLine(); - } - else if (valueToWriteWhenNotIndenting) { - write(valueToWriteWhenNotIndenting); - } - } - function decreaseIndentIf(value1, value2) { - if (value1) { - decreaseIndent(); - } - if (value2) { - decreaseIndent(); - } - } - function shouldWriteLeadingLineTerminator(parentNode, children, format) { - if (format & 1) { - return true; - } - if (format & 2) { - if (format & 32768) { - return true; - } - var firstChild = children[0]; - if (firstChild === undefined) { - return !ts.rangeIsOnSingleLine(parentNode, currentSourceFile); - } - else if (ts.positionIsSynthesized(parentNode.pos) || ts.nodeIsSynthesized(firstChild)) { - return synthesizedNodeStartsOnNewLine(firstChild, format); - } - else { - return !ts.rangeStartPositionsAreOnSameLine(parentNode, firstChild, currentSourceFile); - } - } - else { - return false; - } - } - function shouldWriteSeparatingLineTerminator(previousNode, nextNode, format) { - if (format & 1) { - return true; - } - else if (format & 2) { - if (previousNode === undefined || nextNode === undefined) { - return false; - } - else if (ts.nodeIsSynthesized(previousNode) || ts.nodeIsSynthesized(nextNode)) { - return synthesizedNodeStartsOnNewLine(previousNode, format) || synthesizedNodeStartsOnNewLine(nextNode, format); - } - else { - return !ts.rangeEndIsOnSameLineAsRangeStart(previousNode, nextNode, currentSourceFile); - } - } - else { - return nextNode.startsOnNewLine; - } - } - function shouldWriteClosingLineTerminator(parentNode, children, format) { - if (format & 1) { - return (format & 65536) === 0; - } - else if (format & 2) { - if (format & 32768) { - return true; - } - var lastChild = ts.lastOrUndefined(children); - if (lastChild === undefined) { - return !ts.rangeIsOnSingleLine(parentNode, currentSourceFile); - } - else if (ts.positionIsSynthesized(parentNode.pos) || ts.nodeIsSynthesized(lastChild)) { - return synthesizedNodeStartsOnNewLine(lastChild, format); - } - else { - return !ts.rangeEndPositionsAreOnSameLine(parentNode, lastChild, currentSourceFile); - } - } - else { - return false; - } - } - function synthesizedNodeStartsOnNewLine(node, format) { - if (ts.nodeIsSynthesized(node)) { - var startsOnNewLine = node.startsOnNewLine; - if (startsOnNewLine === undefined) { - return (format & 32768) !== 0; - } - return startsOnNewLine; - } - return (format & 32768) !== 0; - } - function needsIndentation(parent, node1, node2) { - parent = skipSynthesizedParentheses(parent); - node1 = skipSynthesizedParentheses(node1); - node2 = skipSynthesizedParentheses(node2); - if (node2.startsOnNewLine) { - return true; - } - return !ts.nodeIsSynthesized(parent) - && !ts.nodeIsSynthesized(node1) - && !ts.nodeIsSynthesized(node2) - && !ts.rangeEndIsOnSameLineAsRangeStart(node1, node2, currentSourceFile); - } - function skipSynthesizedParentheses(node) { - while (node.kind === 178 && ts.nodeIsSynthesized(node)) { - node = node.expression; - } - return node; - } - function getTextOfNode(node, includeTrivia) { - if (ts.isGeneratedIdentifier(node)) { - return getGeneratedIdentifier(node); - } - else if (ts.isIdentifier(node) && (ts.nodeIsSynthesized(node) || !node.parent)) { - return ts.unescapeIdentifier(node.text); - } - else if (node.kind === 9 && node.textSourceNode) { - return getTextOfNode(node.textSourceNode, includeTrivia); - } - else if (ts.isLiteralExpression(node) && (ts.nodeIsSynthesized(node) || !node.parent)) { - return node.text; - } - return ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, node, includeTrivia); - } - function getLiteralTextOfNode(node) { - if (node.kind === 9 && node.textSourceNode) { - var textSourceNode = node.textSourceNode; - if (ts.isIdentifier(textSourceNode)) { - return "\"" + ts.escapeNonAsciiCharacters(ts.escapeString(getTextOfNode(textSourceNode))) + "\""; - } - else { - return getLiteralTextOfNode(textSourceNode); - } - } - return ts.getLiteralText(node, currentSourceFile, languageVersion); - } - function tryGetConstEnumValue(node) { - if (compilerOptions.isolatedModules) { - return undefined; - } - return ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node) - ? resolver.getConstantValue(node) - : undefined; - } - function isSingleLineEmptyBlock(block) { - return !block.multiLine - && block.statements.length === 0 - && ts.rangeEndIsOnSameLineAsRangeStart(block, block, currentSourceFile); - } - function isUniqueName(name) { - return !resolver.hasGlobalName(name) && - !ts.hasProperty(currentFileIdentifiers, name) && - !ts.hasProperty(generatedNameSet, name); - } - function isUniqueLocalName(name, container) { - for (var node = container; ts.isNodeDescendantOf(node, container); node = node.nextContainer) { - if (node.locals && ts.hasProperty(node.locals, name)) { - if (node.locals[name].flags & (107455 | 1048576 | 8388608)) { - return false; - } - } - } - return true; - } - function makeTempVariableName(flags) { - if (flags && !(tempFlags & flags)) { - var name_41 = flags === 268435456 ? "_i" : "_n"; - if (isUniqueName(name_41)) { - tempFlags |= flags; - return name_41; - } - } - while (true) { - var count = tempFlags & 268435455; - tempFlags++; - if (count !== 8 && count !== 13) { - var name_42 = count < 26 - ? "_" + String.fromCharCode(97 + count) - : "_" + (count - 26); - if (isUniqueName(name_42)) { - return name_42; - } - } - } - } - function makeUniqueName(baseName) { - if (baseName.charCodeAt(baseName.length - 1) !== 95) { - baseName += "_"; - } - var i = 1; - while (true) { - var generatedName = baseName + i; - if (isUniqueName(generatedName)) { - return generatedNameSet[generatedName] = generatedName; - } - i++; - } - } - function generateNameForModuleOrEnum(node) { - var name = getTextOfNode(node.name); - return isUniqueLocalName(name, node) ? name : makeUniqueName(name); - } - function generateNameForImportOrExportDeclaration(node) { - var expr = ts.getExternalModuleName(node); - var baseName = expr.kind === 9 ? - ts.escapeIdentifier(ts.makeIdentifierFromModuleName(expr.text)) : "module"; - return makeUniqueName(baseName); - } - function generateNameForExportDefault() { - return makeUniqueName("default"); - } - function generateNameForClassExpression() { - return makeUniqueName("class"); - } - function generateNameForNode(node) { - switch (node.kind) { - case 69: - return makeUniqueName(getTextOfNode(node)); - case 225: - case 224: - return generateNameForModuleOrEnum(node); - case 230: - case 236: - return generateNameForImportOrExportDeclaration(node); - case 220: - case 221: - case 235: - return generateNameForExportDefault(); - case 192: - return generateNameForClassExpression(); - default: - return makeTempVariableName(0); - } - } - function generateName(name) { - switch (name.autoGenerateKind) { - case 1: - return makeTempVariableName(0); - case 2: - return makeTempVariableName(268435456); - case 3: - return makeUniqueName(name.text); + awaiterEmitted = true; + helpersEmitted = true; } - ts.Debug.fail("Unsupported GeneratedIdentifierKind."); + if (helpersEmitted) { + writeLine(); + } + return helpersEmitted; } - function getNodeForGeneratedName(name) { - var autoGenerateId = name.autoGenerateId; - var node = name; - var original = node.original; - while (original) { - node = original; - if (ts.isIdentifier(node) - && node.autoGenerateKind === 4 - && node.autoGenerateId !== autoGenerateId) { - break; + function writeLines(text) { + var lines = text.split(/\r\n|\r|\n/g); + for (var i = 0; i < lines.length; i++) { + var line = lines[i]; + if (line.length) { + if (i > 0) { + writeLine(); + } + write(line); } - original = node.original; } - return node; } - function getGeneratedIdentifier(name) { - if (name.autoGenerateKind === 4) { - var node = getNodeForGeneratedName(name); - var nodeId = ts.getNodeId(node); - return nodeIdToGeneratedName[nodeId] || (nodeIdToGeneratedName[nodeId] = ts.unescapeIdentifier(generateNameForNode(node))); - } - else { - var autoGenerateId = name.autoGenerateId; - return autoGeneratedIdToGeneratedName[autoGenerateId] || (autoGeneratedIdToGeneratedName[autoGenerateId] = ts.unescapeIdentifier(generateName(name))); + function emitShebang() { + var shebang = ts.getShebang(currentText); + if (shebang) { + write(shebang); + writeLine(); } } - function createDelimiterMap() { - var delimiters = []; - delimiters[0] = ""; - delimiters[16] = ","; - delimiters[4] = " |"; - delimiters[8] = " &"; - return delimiters; - } - function getDelimiter(format) { - return delimiters[format & 28]; - } - function createBracketsMap() { - var brackets = []; - brackets[512] = ["{", "}"]; - brackets[1024] = ["(", ")"]; - brackets[2048] = ["<", ">"]; - brackets[4096] = ["[", "]"]; - return brackets; + function emitModifiers(node, modifiers) { + if (modifiers && modifiers.length) { + emitList(node, modifiers, 256); + write(" "); + } } - function getOpeningBracket(format) { - return brackets[format & 7680][0]; + function emitWithPrefix(prefix, node) { + emitNodeWithPrefix(prefix, node, emit); } - function getClosingBracket(format) { - return brackets[format & 7680][1]; + function emitExpressionWithPrefix(prefix, node) { + emitNodeWithPrefix(prefix, node, emitExpression); } - } - ts.emitFiles = emitFiles; -})(ts || (ts = {})); -var ts; -(function (ts) { - ts.version = "2.1.0"; - var emptyArray = []; - function findConfigFile(searchPath, fileExists, configName) { - if (configName === void 0) { configName = "tsconfig.json"; } - while (true) { - var fileName = ts.combinePaths(searchPath, configName); - if (fileExists(fileName)) { - return fileName; - } - var parentPath = ts.getDirectoryPath(searchPath); - if (parentPath === searchPath) { - break; + function emitNodeWithPrefix(prefix, node, emit) { + if (node) { + write(prefix); + emit(node); } - searchPath = parentPath; } - return undefined; - } - ts.findConfigFile = findConfigFile; - function resolveTripleslashReference(moduleName, containingFile) { - var basePath = ts.getDirectoryPath(containingFile); - var referencedFileName = ts.isRootedDiskPath(moduleName) ? moduleName : ts.combinePaths(basePath, moduleName); - return ts.normalizePath(referencedFileName); - } - ts.resolveTripleslashReference = resolveTripleslashReference; - function computeCommonSourceDirectoryOfFilenames(fileNames, currentDirectory, getCanonicalFileName) { - var commonPathComponents; - var failed = ts.forEach(fileNames, function (sourceFile) { - var sourcePathComponents = ts.getNormalizedPathComponents(sourceFile, currentDirectory); - sourcePathComponents.pop(); - if (!commonPathComponents) { - commonPathComponents = sourcePathComponents; - return; - } - for (var i = 0, n = Math.min(commonPathComponents.length, sourcePathComponents.length); i < n; i++) { - if (getCanonicalFileName(commonPathComponents[i]) !== getCanonicalFileName(sourcePathComponents[i])) { - if (i === 0) { - return true; - } - commonPathComponents.length = i; - break; - } - } - if (sourcePathComponents.length < commonPathComponents.length) { - commonPathComponents.length = sourcePathComponents.length; + function emitWithSuffix(node, suffix) { + if (node) { + emit(node); + write(suffix); } - }); - if (failed) { - return ""; - } - if (!commonPathComponents) { - return currentDirectory; } - return ts.getNormalizedPathFromPathComponents(commonPathComponents); - } - ts.computeCommonSourceDirectoryOfFilenames = computeCommonSourceDirectoryOfFilenames; - function trace(host, message) { - host.trace(ts.formatMessage.apply(undefined, arguments)); - } - function isTraceEnabled(compilerOptions, host) { - return compilerOptions.traceResolution && host.trace !== undefined; - } - function hasZeroOrOneAsteriskCharacter(str) { - var seenAsterisk = false; - for (var i = 0; i < str.length; i++) { - if (str.charCodeAt(i) === 42) { - if (!seenAsterisk) { - seenAsterisk = true; - } - else { - return false; - } + function emitEmbeddedStatement(node) { + if (ts.isBlock(node)) { + write(" "); + emit(node); } - } - return true; - } - ts.hasZeroOrOneAsteriskCharacter = hasZeroOrOneAsteriskCharacter; - function createResolvedModule(resolvedFileName, isExternalLibraryImport, failedLookupLocations) { - return { resolvedModule: resolvedFileName ? { resolvedFileName: resolvedFileName, isExternalLibraryImport: isExternalLibraryImport } : undefined, failedLookupLocations: failedLookupLocations }; - } - function moduleHasNonRelativeName(moduleName) { - return !(ts.isRootedDiskPath(moduleName) || ts.isExternalModuleNameRelative(moduleName)); - } - function tryReadTypesSection(packageJsonPath, baseDirectory, state) { - var jsonContent = readJson(packageJsonPath, state.host); - function tryReadFromField(fieldName) { - if (ts.hasProperty(jsonContent, fieldName)) { - var typesFile = jsonContent[fieldName]; - if (typeof typesFile === "string") { - var typesFilePath_1 = ts.normalizePath(ts.combinePaths(baseDirectory, typesFile)); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.package_json_has_0_field_1_that_references_2, fieldName, typesFile, typesFilePath_1); - } - return typesFilePath_1; - } - else { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Expected_type_of_0_field_in_package_json_to_be_string_got_1, fieldName, typeof typesFile); - } - } + else { + writeLine(); + increaseIndent(); + emit(node); + decreaseIndent(); } } - var typesFilePath = tryReadFromField("typings") || tryReadFromField("types"); - if (typesFilePath) { - return typesFilePath; + function emitDecorators(parentNode, decorators) { + emitList(parentNode, decorators, 24577); } - if (state.compilerOptions.allowJs && jsonContent.main && typeof jsonContent.main === "string") { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.No_types_specified_in_package_json_but_allowJs_is_set_so_returning_main_value_of_0, jsonContent.main); - } - var mainFilePath = ts.normalizePath(ts.combinePaths(baseDirectory, jsonContent.main)); - return mainFilePath; + function emitTypeArguments(parentNode, typeArguments) { + emitList(parentNode, typeArguments, 26960); } - return undefined; - } - function readJson(path, host) { - try { - var jsonText = host.readFile(path); - return jsonText ? JSON.parse(jsonText) : {}; + function emitTypeParameters(parentNode, typeParameters) { + emitList(parentNode, typeParameters, 26960); } - catch (e) { - return {}; + function emitParameters(parentNode, parameters) { + emitList(parentNode, parameters, 1360); } - } - var typeReferenceExtensions = [".d.ts"]; - function getEffectiveTypeRoots(options, host) { - if (options.typeRoots) { - return options.typeRoots; + function emitParametersForArrow(parentNode, parameters) { + if (parameters && + parameters.length === 1 && + parameters[0].type === undefined && + parameters[0].pos === parentNode.pos) { + emit(parameters[0]); + } + else { + emitParameters(parentNode, parameters); + } } - var currentDirectory; - if (options.configFilePath) { - currentDirectory = ts.getDirectoryPath(options.configFilePath); + function emitParametersForIndexSignature(parentNode, parameters) { + emitList(parentNode, parameters, 4432); } - else if (host.getCurrentDirectory) { - currentDirectory = host.getCurrentDirectory(); + function emitList(parentNode, children, format, start, count) { + emitNodeList(emit, parentNode, children, format, start, count); } - return currentDirectory && getDefaultTypeRoots(currentDirectory, host); - } - ts.getEffectiveTypeRoots = getEffectiveTypeRoots; - function getDefaultTypeRoots(currentDirectory, host) { - if (!host.directoryExists) { - return [ts.combinePaths(currentDirectory, nodeModulesAtTypes)]; + function emitExpressionList(parentNode, children, format, start, count) { + emitNodeList(emitExpression, parentNode, children, format, start, count); } - var typeRoots; - while (true) { - var atTypes = ts.combinePaths(currentDirectory, nodeModulesAtTypes); - if (host.directoryExists(atTypes)) { - (typeRoots || (typeRoots = [])).push(atTypes); + function emitNodeList(emit, parentNode, children, format, start, count) { + if (start === void 0) { start = 0; } + if (count === void 0) { count = children ? children.length - start : 0; } + var isUndefined = children === undefined; + if (isUndefined && format & 8192) { + return; } - var parent_15 = ts.getDirectoryPath(currentDirectory); - if (parent_15 === currentDirectory) { - break; + var isEmpty = isUndefined || children.length === 0 || start >= children.length || count === 0; + if (isEmpty && format & 16384) { + return; } - currentDirectory = parent_15; - } - return typeRoots; - } - var nodeModulesAtTypes = ts.combinePaths("node_modules", "@types"); - function resolveTypeReferenceDirective(typeReferenceDirectiveName, containingFile, options, host) { - var traceEnabled = isTraceEnabled(options, host); - var moduleResolutionState = { - compilerOptions: options, - host: host, - skipTsx: true, - traceEnabled: traceEnabled - }; - var typeRoots = getEffectiveTypeRoots(options, host); - if (traceEnabled) { - if (containingFile === undefined) { - if (typeRoots === undefined) { - trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_not_set_root_directory_not_set, typeReferenceDirectiveName); + if (format & 7680) { + write(getOpeningBracket(format)); + } + if (isEmpty) { + if (format & 1) { + writeLine(); } - else { - trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_not_set_root_directory_1, typeReferenceDirectiveName, typeRoots); + else if (format & 128) { + write(" "); } } else { - if (typeRoots === undefined) { - trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_1_root_directory_not_set, typeReferenceDirectiveName, containingFile); + var mayEmitInterveningComments = (format & 131072) === 0; + var shouldEmitInterveningComments = mayEmitInterveningComments; + if (shouldWriteLeadingLineTerminator(parentNode, children, format)) { + writeLine(); + shouldEmitInterveningComments = false; } - else { - trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_1_root_directory_2, typeReferenceDirectiveName, containingFile, typeRoots); + else if (format & 128) { + write(" "); } - } - } - var failedLookupLocations = []; - if (typeRoots && typeRoots.length) { - if (traceEnabled) { - trace(host, ts.Diagnostics.Resolving_with_primary_search_path_0, typeRoots.join(", ")); - } - var primarySearchPaths = typeRoots; - for (var _i = 0, primarySearchPaths_1 = primarySearchPaths; _i < primarySearchPaths_1.length; _i++) { - var typeRoot = primarySearchPaths_1[_i]; - var candidate = ts.combinePaths(typeRoot, typeReferenceDirectiveName); - var candidateDirectory = ts.getDirectoryPath(candidate); - var resolvedFile_1 = loadNodeModuleFromDirectory(typeReferenceExtensions, candidate, failedLookupLocations, !directoryProbablyExists(candidateDirectory, host), moduleResolutionState); - if (resolvedFile_1) { - if (traceEnabled) { - trace(host, ts.Diagnostics.Type_reference_directive_0_was_successfully_resolved_to_1_primary_Colon_2, typeReferenceDirectiveName, resolvedFile_1, true); + if (format & 64) { + increaseIndent(); + } + var previousSibling = void 0; + var shouldDecreaseIndentAfterEmit = void 0; + var delimiter = getDelimiter(format); + for (var i = 0; i < count; i++) { + var child = children[start + i]; + if (previousSibling) { + write(delimiter); + if (shouldWriteSeparatingLineTerminator(previousSibling, child, format)) { + if ((format & (3 | 64)) === 0) { + increaseIndent(); + shouldDecreaseIndentAfterEmit = true; + } + writeLine(); + shouldEmitInterveningComments = false; + } + else if (previousSibling && format & 256) { + write(" "); + } } - return { - resolvedTypeReferenceDirective: { primary: true, resolvedFileName: resolvedFile_1 }, - failedLookupLocations: failedLookupLocations - }; + if (shouldEmitInterveningComments) { + var commentRange = ts.getCommentRange(child); + emitTrailingCommentsOfPosition(commentRange.pos); + } + else { + shouldEmitInterveningComments = mayEmitInterveningComments; + } + emit(child); + if (shouldDecreaseIndentAfterEmit) { + decreaseIndent(); + shouldDecreaseIndentAfterEmit = false; + } + previousSibling = child; } - } - } - else { - if (traceEnabled) { - trace(host, ts.Diagnostics.Root_directory_cannot_be_determined_skipping_primary_search_paths); - } - } - var resolvedFile; - var initialLocationForSecondaryLookup; - if (containingFile) { - initialLocationForSecondaryLookup = ts.getDirectoryPath(containingFile); - } - if (initialLocationForSecondaryLookup !== undefined) { - if (traceEnabled) { - trace(host, ts.Diagnostics.Looking_up_in_node_modules_folder_initial_location_0, initialLocationForSecondaryLookup); - } - resolvedFile = loadModuleFromNodeModules(typeReferenceDirectiveName, initialLocationForSecondaryLookup, failedLookupLocations, moduleResolutionState); - if (traceEnabled) { - if (resolvedFile) { - trace(host, ts.Diagnostics.Type_reference_directive_0_was_successfully_resolved_to_1_primary_Colon_2, typeReferenceDirectiveName, resolvedFile, false); + var hasTrailingComma = (format & 32) && children.hasTrailingComma; + if (format & 16 && hasTrailingComma) { + write(","); } - else { - trace(host, ts.Diagnostics.Type_reference_directive_0_was_not_resolved, typeReferenceDirectiveName); + if (format & 64) { + decreaseIndent(); + } + if (shouldWriteClosingLineTerminator(parentNode, children, format)) { + writeLine(); + } + else if (format & 128) { + write(" "); } } - } - else { - if (traceEnabled) { - trace(host, ts.Diagnostics.Containing_file_is_not_specified_and_root_directory_cannot_be_determined_skipping_lookup_in_node_modules_folder); + if (format & 7680) { + write(getClosingBracket(format)); } } - return { - resolvedTypeReferenceDirective: resolvedFile - ? { primary: false, resolvedFileName: resolvedFile } - : undefined, - failedLookupLocations: failedLookupLocations - }; - } - ts.resolveTypeReferenceDirective = resolveTypeReferenceDirective; - function resolveModuleName(moduleName, containingFile, compilerOptions, host) { - var traceEnabled = isTraceEnabled(compilerOptions, host); - if (traceEnabled) { - trace(host, ts.Diagnostics.Resolving_module_0_from_1, moduleName, containingFile); - } - var moduleResolution = compilerOptions.moduleResolution; - if (moduleResolution === undefined) { - moduleResolution = ts.getEmitModuleKind(compilerOptions) === ts.ModuleKind.CommonJS ? ts.ModuleResolutionKind.NodeJs : ts.ModuleResolutionKind.Classic; - if (traceEnabled) { - trace(host, ts.Diagnostics.Module_resolution_kind_is_not_specified_using_0, ts.ModuleResolutionKind[moduleResolution]); + function writeIfAny(nodes, text) { + if (nodes && nodes.length > 0) { + write(text); } } - else { - if (traceEnabled) { - trace(host, ts.Diagnostics.Explicitly_specified_module_resolution_kind_Colon_0, ts.ModuleResolutionKind[moduleResolution]); + function writeIfPresent(node, text) { + if (node !== undefined) { + write(text); } } - var result; - switch (moduleResolution) { - case ts.ModuleResolutionKind.NodeJs: - result = nodeModuleNameResolver(moduleName, containingFile, compilerOptions, host); - break; - case ts.ModuleResolutionKind.Classic: - result = classicNameResolver(moduleName, containingFile, compilerOptions, host); - break; + function writeToken(token, pos, contextNode) { + return emitTokenWithSourceMap(contextNode, token, pos, writeTokenText); } - if (traceEnabled) { - if (result.resolvedModule) { - trace(host, ts.Diagnostics.Module_name_0_was_successfully_resolved_to_1, moduleName, result.resolvedModule.resolvedFileName); + function writeTokenText(token, pos) { + var tokenString = ts.tokenToString(token); + write(tokenString); + return pos < 0 ? pos : pos + tokenString.length; + } + function increaseIndentIf(value, valueToWriteWhenNotIndenting) { + if (value) { + increaseIndent(); + writeLine(); } - else { - trace(host, ts.Diagnostics.Module_name_0_was_not_resolved, moduleName); + else if (valueToWriteWhenNotIndenting) { + write(valueToWriteWhenNotIndenting); } } - return result; - } - ts.resolveModuleName = resolveModuleName; - function tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state) { - if (moduleHasNonRelativeName(moduleName)) { - return tryLoadModuleUsingBaseUrl(moduleName, loader, failedLookupLocations, supportedExtensions, state); - } - else { - return tryLoadModuleUsingRootDirs(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state); - } - } - function tryLoadModuleUsingRootDirs(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state) { - if (!state.compilerOptions.rootDirs) { - return undefined; - } - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.rootDirs_option_is_set_using_it_to_resolve_relative_module_name_0, moduleName); + function decreaseIndentIf(value1, value2) { + if (value1) { + decreaseIndent(); + } + if (value2) { + decreaseIndent(); + } } - var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); - var matchedRootDir; - var matchedNormalizedPrefix; - for (var _i = 0, _a = state.compilerOptions.rootDirs; _i < _a.length; _i++) { - var rootDir = _a[_i]; - var normalizedRoot = ts.normalizePath(rootDir); - if (!ts.endsWith(normalizedRoot, ts.directorySeparator)) { - normalizedRoot += ts.directorySeparator; + function shouldWriteLeadingLineTerminator(parentNode, children, format) { + if (format & 1) { + return true; } - var isLongestMatchingPrefix = ts.startsWith(candidate, normalizedRoot) && - (matchedNormalizedPrefix === undefined || matchedNormalizedPrefix.length < normalizedRoot.length); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Checking_if_0_is_the_longest_matching_prefix_for_1_2, normalizedRoot, candidate, isLongestMatchingPrefix); + if (format & 2) { + if (format & 32768) { + return true; + } + var firstChild = children[0]; + if (firstChild === undefined) { + return !ts.rangeIsOnSingleLine(parentNode, currentSourceFile); + } + else if (ts.positionIsSynthesized(parentNode.pos) || ts.nodeIsSynthesized(firstChild)) { + return synthesizedNodeStartsOnNewLine(firstChild, format); + } + else { + return !ts.rangeStartPositionsAreOnSameLine(parentNode, firstChild, currentSourceFile); + } } - if (isLongestMatchingPrefix) { - matchedNormalizedPrefix = normalizedRoot; - matchedRootDir = rootDir; + else { + return false; } } - if (matchedNormalizedPrefix) { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Longest_matching_prefix_for_0_is_1, candidate, matchedNormalizedPrefix); + function shouldWriteSeparatingLineTerminator(previousNode, nextNode, format) { + if (format & 1) { + return true; } - var suffix = candidate.substr(matchedNormalizedPrefix.length); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Loading_0_from_the_root_dir_1_candidate_location_2, suffix, matchedNormalizedPrefix, candidate); + else if (format & 2) { + if (previousNode === undefined || nextNode === undefined) { + return false; + } + else if (ts.nodeIsSynthesized(previousNode) || ts.nodeIsSynthesized(nextNode)) { + return synthesizedNodeStartsOnNewLine(previousNode, format) || synthesizedNodeStartsOnNewLine(nextNode, format); + } + else { + return !ts.rangeEndIsOnSameLineAsRangeStart(previousNode, nextNode, currentSourceFile); + } } - var resolvedFileName = loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(containingDirectory, state.host), state); - if (resolvedFileName) { - return resolvedFileName; + else { + return nextNode.startsOnNewLine; } - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Trying_other_entries_in_rootDirs); + } + function shouldWriteClosingLineTerminator(parentNode, children, format) { + if (format & 1) { + return (format & 65536) === 0; } - for (var _b = 0, _c = state.compilerOptions.rootDirs; _b < _c.length; _b++) { - var rootDir = _c[_b]; - if (rootDir === matchedRootDir) { - continue; + else if (format & 2) { + if (format & 32768) { + return true; } - var candidate_1 = ts.combinePaths(ts.normalizePath(rootDir), suffix); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Loading_0_from_the_root_dir_1_candidate_location_2, suffix, rootDir, candidate_1); + var lastChild = ts.lastOrUndefined(children); + if (lastChild === undefined) { + return !ts.rangeIsOnSingleLine(parentNode, currentSourceFile); } - var baseDirectory = ts.getDirectoryPath(candidate_1); - var resolvedFileName_1 = loader(candidate_1, supportedExtensions, failedLookupLocations, !directoryProbablyExists(baseDirectory, state.host), state); - if (resolvedFileName_1) { - return resolvedFileName_1; + else if (ts.positionIsSynthesized(parentNode.pos) || ts.nodeIsSynthesized(lastChild)) { + return synthesizedNodeStartsOnNewLine(lastChild, format); + } + else { + return !ts.rangeEndPositionsAreOnSameLine(parentNode, lastChild, currentSourceFile); } } - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Module_resolution_using_rootDirs_has_failed); + else { + return false; } } - return undefined; - } - function tryLoadModuleUsingBaseUrl(moduleName, loader, failedLookupLocations, supportedExtensions, state) { - if (!state.compilerOptions.baseUrl) { - return undefined; + function synthesizedNodeStartsOnNewLine(node, format) { + if (ts.nodeIsSynthesized(node)) { + var startsOnNewLine = node.startsOnNewLine; + if (startsOnNewLine === undefined) { + return (format & 32768) !== 0; + } + return startsOnNewLine; + } + return (format & 32768) !== 0; } - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.baseUrl_option_is_set_to_0_using_this_value_to_resolve_non_relative_module_name_1, state.compilerOptions.baseUrl, moduleName); + function needsIndentation(parent, node1, node2) { + parent = skipSynthesizedParentheses(parent); + node1 = skipSynthesizedParentheses(node1); + node2 = skipSynthesizedParentheses(node2); + if (node2.startsOnNewLine) { + return true; + } + return !ts.nodeIsSynthesized(parent) + && !ts.nodeIsSynthesized(node1) + && !ts.nodeIsSynthesized(node2) + && !ts.rangeEndIsOnSameLineAsRangeStart(node1, node2, currentSourceFile); } - var matchedPattern = undefined; - if (state.compilerOptions.paths) { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.paths_option_is_specified_looking_for_a_pattern_to_match_module_name_0, moduleName); + function skipSynthesizedParentheses(node) { + while (node.kind === 178 && ts.nodeIsSynthesized(node)) { + node = node.expression; } - matchedPattern = matchPatternOrExact(ts.getOwnKeys(state.compilerOptions.paths), moduleName); + return node; } - if (matchedPattern) { - var matchedStar = typeof matchedPattern === "string" ? undefined : matchedText(matchedPattern, moduleName); - var matchedPatternText = typeof matchedPattern === "string" ? matchedPattern : patternText(matchedPattern); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Module_name_0_matched_pattern_1, moduleName, matchedPatternText); + function getTextOfNode(node, includeTrivia) { + if (ts.isGeneratedIdentifier(node)) { + return getGeneratedIdentifier(node); } - for (var _i = 0, _a = state.compilerOptions.paths[matchedPatternText]; _i < _a.length; _i++) { - var subst = _a[_i]; - var path = matchedStar ? subst.replace("*", matchedStar) : subst; - var candidate = ts.normalizePath(ts.combinePaths(state.compilerOptions.baseUrl, path)); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Trying_substitution_0_candidate_module_location_Colon_1, subst, path); + else if (ts.isIdentifier(node) && (ts.nodeIsSynthesized(node) || !node.parent)) { + return ts.unescapeIdentifier(node.text); + } + else if (node.kind === 9 && node.textSourceNode) { + return getTextOfNode(node.textSourceNode, includeTrivia); + } + else if (ts.isLiteralExpression(node) && (ts.nodeIsSynthesized(node) || !node.parent)) { + return node.text; + } + return ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, node, includeTrivia); + } + function getLiteralTextOfNode(node) { + if (node.kind === 9 && node.textSourceNode) { + var textSourceNode = node.textSourceNode; + if (ts.isIdentifier(textSourceNode)) { + return "\"" + ts.escapeNonAsciiCharacters(ts.escapeString(getTextOfNode(textSourceNode))) + "\""; } - var resolvedFileName = loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(ts.getDirectoryPath(candidate), state.host), state); - if (resolvedFileName) { - return resolvedFileName; + else { + return getLiteralTextOfNode(textSourceNode); } } - return undefined; + return ts.getLiteralText(node, currentSourceFile, languageVersion); } - else { - var candidate = ts.normalizePath(ts.combinePaths(state.compilerOptions.baseUrl, moduleName)); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Resolving_module_name_0_relative_to_base_url_1_2, moduleName, state.compilerOptions.baseUrl, candidate); - } - return loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(ts.getDirectoryPath(candidate), state.host), state); + function isSingleLineEmptyBlock(block) { + return !block.multiLine + && isEmptyBlock(block); } - } - function matchPatternOrExact(patternStrings, candidate) { - var patterns = []; - for (var _i = 0, patternStrings_1 = patternStrings; _i < patternStrings_1.length; _i++) { - var patternString = patternStrings_1[_i]; - var pattern = tryParsePattern(patternString); - if (pattern) { - patterns.push(pattern); - } - else if (patternString === candidate) { - return patternString; - } + function isEmptyBlock(block) { + return block.statements.length === 0 + && ts.rangeEndIsOnSameLineAsRangeStart(block, block, currentSourceFile); } - return findBestPatternMatch(patterns, function (_) { return _; }, candidate); - } - function patternText(_a) { - var prefix = _a.prefix, suffix = _a.suffix; - return prefix + "*" + suffix; - } - function matchedText(pattern, candidate) { - ts.Debug.assert(isPatternMatch(pattern, candidate)); - return candidate.substr(pattern.prefix.length, candidate.length - pattern.suffix.length); - } - function findBestPatternMatch(values, getPattern, candidate) { - var matchedValue = undefined; - var longestMatchPrefixLength = -1; - for (var _i = 0, values_1 = values; _i < values_1.length; _i++) { - var v = values_1[_i]; - var pattern = getPattern(v); - if (isPatternMatch(pattern, candidate) && pattern.prefix.length > longestMatchPrefixLength) { - longestMatchPrefixLength = pattern.prefix.length; - matchedValue = v; + function isUniqueName(name) { + return !resolver.hasGlobalName(name) && + !ts.hasProperty(currentFileIdentifiers, name) && + !ts.hasProperty(generatedNameSet, name); + } + function isUniqueLocalName(name, container) { + for (var node = container; ts.isNodeDescendantOf(node, container); node = node.nextContainer) { + if (node.locals && ts.hasProperty(node.locals, name)) { + if (node.locals[name].flags & (107455 | 1048576 | 8388608)) { + return false; + } + } } + return true; } - return matchedValue; - } - ts.findBestPatternMatch = findBestPatternMatch; - function isPatternMatch(_a, candidate) { - var prefix = _a.prefix, suffix = _a.suffix; - return candidate.length >= prefix.length + suffix.length && - ts.startsWith(candidate, prefix) && - ts.endsWith(candidate, suffix); - } - function tryParsePattern(pattern) { - ts.Debug.assert(hasZeroOrOneAsteriskCharacter(pattern)); - var indexOfStar = pattern.indexOf("*"); - return indexOfStar === -1 ? undefined : { - prefix: pattern.substr(0, indexOfStar), - suffix: pattern.substr(indexOfStar + 1) - }; - } - ts.tryParsePattern = tryParsePattern; - function nodeModuleNameResolver(moduleName, containingFile, compilerOptions, host) { - var containingDirectory = ts.getDirectoryPath(containingFile); - var supportedExtensions = ts.getSupportedExtensions(compilerOptions); - var traceEnabled = isTraceEnabled(compilerOptions, host); - var failedLookupLocations = []; - var state = { compilerOptions: compilerOptions, host: host, traceEnabled: traceEnabled, skipTsx: false }; - var resolvedFileName = tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, nodeLoadModuleByRelativeName, failedLookupLocations, supportedExtensions, state); - var isExternalLibraryImport = false; - if (!resolvedFileName) { - if (moduleHasNonRelativeName(moduleName)) { - if (traceEnabled) { - trace(host, ts.Diagnostics.Loading_module_0_from_node_modules_folder, moduleName); + function makeTempVariableName(flags) { + if (flags && !(tempFlags & flags)) { + var name_41 = flags === 268435456 ? "_i" : "_n"; + if (isUniqueName(name_41)) { + tempFlags |= flags; + return name_41; } - resolvedFileName = loadModuleFromNodeModules(moduleName, containingDirectory, failedLookupLocations, state); - isExternalLibraryImport = resolvedFileName !== undefined; } - else { - var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); - resolvedFileName = nodeLoadModuleByRelativeName(candidate, supportedExtensions, failedLookupLocations, false, state); + while (true) { + var count = tempFlags & 268435455; + tempFlags++; + if (count !== 8 && count !== 13) { + var name_42 = count < 26 + ? "_" + String.fromCharCode(97 + count) + : "_" + (count - 26); + if (isUniqueName(name_42)) { + return name_42; + } + } } } - if (resolvedFileName && host.realpath) { - var originalFileName = resolvedFileName; - resolvedFileName = ts.normalizePath(host.realpath(resolvedFileName)); - if (traceEnabled) { - trace(host, ts.Diagnostics.Resolving_real_path_for_0_result_1, originalFileName, resolvedFileName); + function makeUniqueName(baseName) { + if (baseName.charCodeAt(baseName.length - 1) !== 95) { + baseName += "_"; + } + var i = 1; + while (true) { + var generatedName = baseName + i; + if (isUniqueName(generatedName)) { + return generatedNameSet[generatedName] = generatedName; + } + i++; } } - return createResolvedModule(resolvedFileName, isExternalLibraryImport, failedLookupLocations); - } - ts.nodeModuleNameResolver = nodeModuleNameResolver; - function nodeLoadModuleByRelativeName(candidate, supportedExtensions, failedLookupLocations, onlyRecordFailures, state) { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Loading_module_as_file_Slash_folder_candidate_module_location_0, candidate); + function generateNameForModuleOrEnum(node) { + var name = getTextOfNode(node.name); + return isUniqueLocalName(name, node) ? name : makeUniqueName(name); } - var resolvedFileName = !ts.pathEndsWithDirectorySeparator(candidate) && loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, onlyRecordFailures, state); - return resolvedFileName || loadNodeModuleFromDirectory(supportedExtensions, candidate, failedLookupLocations, onlyRecordFailures, state); - } - function directoryProbablyExists(directoryName, host) { - return !host.directoryExists || host.directoryExists(directoryName); - } - ts.directoryProbablyExists = directoryProbablyExists; - function loadModuleFromFile(candidate, extensions, failedLookupLocation, onlyRecordFailures, state) { - var resolvedByAddingExtension = tryAddingExtensions(candidate, extensions, failedLookupLocation, onlyRecordFailures, state); - if (resolvedByAddingExtension) { - return resolvedByAddingExtension; + function generateNameForImportOrExportDeclaration(node) { + var expr = ts.getExternalModuleName(node); + var baseName = expr.kind === 9 ? + ts.escapeIdentifier(ts.makeIdentifierFromModuleName(expr.text)) : "module"; + return makeUniqueName(baseName); } - if (ts.hasJavaScriptFileExtension(candidate)) { - var extensionless = ts.removeFileExtension(candidate); - if (state.traceEnabled) { - var extension = candidate.substring(extensionless.length); - trace(state.host, ts.Diagnostics.File_name_0_has_a_1_extension_stripping_it, candidate, extension); - } - return tryAddingExtensions(extensionless, extensions, failedLookupLocation, onlyRecordFailures, state); + function generateNameForExportDefault() { + return makeUniqueName("default"); } - } - function tryAddingExtensions(candidate, extensions, failedLookupLocation, onlyRecordFailures, state) { - if (!onlyRecordFailures) { - var directory = ts.getDirectoryPath(candidate); - if (directory) { - onlyRecordFailures = !directoryProbablyExists(directory, state.host); - } + function generateNameForClassExpression() { + return makeUniqueName("class"); } - return ts.forEach(extensions, function (ext) { - return !(state.skipTsx && ts.isJsxOrTsxExtension(ext)) && tryFile(candidate + ext, failedLookupLocation, onlyRecordFailures, state); - }); - } - function tryFile(fileName, failedLookupLocation, onlyRecordFailures, state) { - if (!onlyRecordFailures && state.host.fileExists(fileName)) { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.File_0_exist_use_it_as_a_name_resolution_result, fileName); + function generateNameForNode(node) { + switch (node.kind) { + case 69: + return makeUniqueName(getTextOfNode(node)); + case 225: + case 224: + return generateNameForModuleOrEnum(node); + case 230: + case 236: + return generateNameForImportOrExportDeclaration(node); + case 220: + case 221: + case 235: + return generateNameForExportDefault(); + case 192: + return generateNameForClassExpression(); + default: + return makeTempVariableName(0); } - return fileName; } - else { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.File_0_does_not_exist, fileName); + function generateName(name) { + switch (name.autoGenerateKind) { + case 1: + return makeTempVariableName(0); + case 2: + return makeTempVariableName(268435456); + case 3: + return makeUniqueName(name.text); } - failedLookupLocation.push(fileName); - return undefined; + ts.Debug.fail("Unsupported GeneratedIdentifierKind."); } - } - function loadNodeModuleFromDirectory(extensions, candidate, failedLookupLocation, onlyRecordFailures, state) { - var packageJsonPath = pathToPackageJson(candidate); - var directoryExists = !onlyRecordFailures && directoryProbablyExists(candidate, state.host); - if (directoryExists && state.host.fileExists(packageJsonPath)) { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Found_package_json_at_0, packageJsonPath); - } - var typesFile = tryReadTypesSection(packageJsonPath, candidate, state); - if (typesFile) { - var onlyRecordFailures_1 = !directoryProbablyExists(ts.getDirectoryPath(typesFile), state.host); - var result = tryFile(typesFile, failedLookupLocation, onlyRecordFailures_1, state) || - tryAddingExtensions(typesFile, extensions, failedLookupLocation, onlyRecordFailures_1, state); - if (result) { - return result; + function getNodeForGeneratedName(name) { + var autoGenerateId = name.autoGenerateId; + var node = name; + var original = node.original; + while (original) { + node = original; + if (ts.isIdentifier(node) + && node.autoGenerateKind === 4 + && node.autoGenerateId !== autoGenerateId) { + break; } + original = node.original; + } + return node; + } + function getGeneratedIdentifier(name) { + if (name.autoGenerateKind === 4) { + var node = getNodeForGeneratedName(name); + var nodeId = ts.getNodeId(node); + return nodeIdToGeneratedName[nodeId] || (nodeIdToGeneratedName[nodeId] = ts.unescapeIdentifier(generateNameForNode(node))); } else { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.package_json_does_not_have_types_field); - } + var autoGenerateId = name.autoGenerateId; + return autoGeneratedIdToGeneratedName[autoGenerateId] || (autoGeneratedIdToGeneratedName[autoGenerateId] = ts.unescapeIdentifier(generateName(name))); } } - else { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.File_0_does_not_exist, packageJsonPath); - } - failedLookupLocation.push(packageJsonPath); + function createDelimiterMap() { + var delimiters = []; + delimiters[0] = ""; + delimiters[16] = ","; + delimiters[4] = " |"; + delimiters[8] = " &"; + return delimiters; } - return loadModuleFromFile(ts.combinePaths(candidate, "index"), extensions, failedLookupLocation, !directoryExists, state); - } - function pathToPackageJson(directory) { - return ts.combinePaths(directory, "package.json"); - } - function loadModuleFromNodeModulesFolder(moduleName, directory, failedLookupLocations, state) { - var nodeModulesFolder = ts.combinePaths(directory, "node_modules"); - var nodeModulesFolderExists = directoryProbablyExists(nodeModulesFolder, state.host); - var candidate = ts.normalizePath(ts.combinePaths(nodeModulesFolder, moduleName)); - var supportedExtensions = ts.getSupportedExtensions(state.compilerOptions); - var result = loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, !nodeModulesFolderExists, state); - if (result) { - return result; + function getDelimiter(format) { + return delimiters[format & 28]; } - result = loadNodeModuleFromDirectory(supportedExtensions, candidate, failedLookupLocations, !nodeModulesFolderExists, state); - if (result) { - return result; + function createBracketsMap() { + var brackets = []; + brackets[512] = ["{", "}"]; + brackets[1024] = ["(", ")"]; + brackets[2048] = ["<", ">"]; + brackets[4096] = ["[", "]"]; + return brackets; + } + function getOpeningBracket(format) { + return brackets[format & 7680][0]; + } + function getClosingBracket(format) { + return brackets[format & 7680][1]; } } - function loadModuleFromNodeModules(moduleName, directory, failedLookupLocations, state) { - directory = ts.normalizeSlashes(directory); + ts.emitFiles = emitFiles; +})(ts || (ts = {})); +var ts; +(function (ts) { + ts.version = "2.1.0"; + var emptyArray = []; + function findConfigFile(searchPath, fileExists, configName) { + if (configName === void 0) { configName = "tsconfig.json"; } while (true) { - var baseName = ts.getBaseFileName(directory); - if (baseName !== "node_modules") { - var packageResult = loadModuleFromNodeModulesFolder(moduleName, directory, failedLookupLocations, state); - if (packageResult && ts.hasTypeScriptFileExtension(packageResult)) { - return packageResult; - } - else { - var typesResult = loadModuleFromNodeModulesFolder(ts.combinePaths("@types", moduleName), directory, failedLookupLocations, state); - if (typesResult || packageResult) { - return typesResult || packageResult; - } - } + var fileName = ts.combinePaths(searchPath, configName); + if (fileExists(fileName)) { + return fileName; } - var parentPath = ts.getDirectoryPath(directory); - if (parentPath === directory) { + var parentPath = ts.getDirectoryPath(searchPath); + if (parentPath === searchPath) { break; } - directory = parentPath; + searchPath = parentPath; } return undefined; } - function classicNameResolver(moduleName, containingFile, compilerOptions, host) { - var traceEnabled = isTraceEnabled(compilerOptions, host); - var state = { compilerOptions: compilerOptions, host: host, traceEnabled: traceEnabled, skipTsx: !compilerOptions.jsx }; - var failedLookupLocations = []; - var supportedExtensions = ts.getSupportedExtensions(compilerOptions); - var containingDirectory = ts.getDirectoryPath(containingFile); - var resolvedFileName = tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, loadModuleFromFile, failedLookupLocations, supportedExtensions, state); - if (resolvedFileName) { - return createResolvedModule(resolvedFileName, false, failedLookupLocations); - } - var referencedSourceFile; - if (moduleHasNonRelativeName(moduleName)) { - while (true) { - var searchName = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); - referencedSourceFile = loadModuleFromFile(searchName, supportedExtensions, failedLookupLocations, false, state); - if (referencedSourceFile) { - break; - } - var parentPath = ts.getDirectoryPath(containingDirectory); - if (parentPath === containingDirectory) { + ts.findConfigFile = findConfigFile; + function resolveTripleslashReference(moduleName, containingFile) { + var basePath = ts.getDirectoryPath(containingFile); + var referencedFileName = ts.isRootedDiskPath(moduleName) ? moduleName : ts.combinePaths(basePath, moduleName); + return ts.normalizePath(referencedFileName); + } + ts.resolveTripleslashReference = resolveTripleslashReference; + function computeCommonSourceDirectoryOfFilenames(fileNames, currentDirectory, getCanonicalFileName) { + var commonPathComponents; + var failed = ts.forEach(fileNames, function (sourceFile) { + var sourcePathComponents = ts.getNormalizedPathComponents(sourceFile, currentDirectory); + sourcePathComponents.pop(); + if (!commonPathComponents) { + commonPathComponents = sourcePathComponents; + return; + } + for (var i = 0, n = Math.min(commonPathComponents.length, sourcePathComponents.length); i < n; i++) { + if (getCanonicalFileName(commonPathComponents[i]) !== getCanonicalFileName(sourcePathComponents[i])) { + if (i === 0) { + return true; + } + commonPathComponents.length = i; break; } - containingDirectory = parentPath; } + if (sourcePathComponents.length < commonPathComponents.length) { + commonPathComponents.length = sourcePathComponents.length; + } + }); + if (failed) { + return ""; } - else { - var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); - referencedSourceFile = loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, false, state); + if (!commonPathComponents) { + return currentDirectory; } - return referencedSourceFile - ? { resolvedModule: { resolvedFileName: referencedSourceFile }, failedLookupLocations: failedLookupLocations } - : { resolvedModule: undefined, failedLookupLocations: failedLookupLocations }; + return ts.getNormalizedPathFromPathComponents(commonPathComponents); } - ts.classicNameResolver = classicNameResolver; + ts.computeCommonSourceDirectoryOfFilenames = computeCommonSourceDirectoryOfFilenames; function createCompilerHost(options, setParentNodes) { var existingDirectories = ts.createMap(); function getCanonicalFileName(fileName) { @@ -46058,7 +46349,7 @@ var ts; readFile: function (fileName) { return ts.sys.readFile(fileName); }, trace: function (s) { return ts.sys.write(s + newLine); }, directoryExists: function (directoryName) { return ts.sys.directoryExists(directoryName); }, - getEnvironmentVariable: function (name) { return ts.getEnvironmentVariable(name, undefined); }, + getEnvironmentVariable: function (name) { return ts.sys.getEnvironmentVariable ? ts.sys.getEnvironmentVariable(name) : ""; }, getDirectories: function (path) { return ts.sys.getDirectories(path); }, realpath: realpath }; @@ -46126,33 +46417,6 @@ var ts; } return resolutions; } - function getAutomaticTypeDirectiveNames(options, host) { - if (options.types) { - return options.types; - } - var result = []; - if (host.directoryExists && host.getDirectories) { - var typeRoots = getEffectiveTypeRoots(options, host); - if (typeRoots) { - for (var _i = 0, typeRoots_1 = typeRoots; _i < typeRoots_1.length; _i++) { - var root = typeRoots_1[_i]; - if (host.directoryExists(root)) { - for (var _a = 0, _b = host.getDirectories(root); _a < _b.length; _a++) { - var typeDirectivePath = _b[_a]; - var normalized = ts.normalizePath(typeDirectivePath); - var packageJsonPath = pathToPackageJson(ts.combinePaths(root, normalized)); - var isNotNeededPackage = host.fileExists(packageJsonPath) && readJson(packageJsonPath, host).typings === null; - if (!isNotNeededPackage) { - result.push(ts.getBaseFileName(normalized)); - } - } - } - } - } - } - return result; - } - ts.getAutomaticTypeDirectiveNames = getAutomaticTypeDirectiveNames; function createProgram(rootNames, options, host, oldProgram) { var program; var files = []; @@ -46178,7 +46442,7 @@ var ts; resolveModuleNamesWorker = function (moduleNames, containingFile) { return host.resolveModuleNames(moduleNames, containingFile); }; } else { - var loader_1 = function (moduleName, containingFile) { return resolveModuleName(moduleName, containingFile, options, host).resolvedModule; }; + var loader_1 = function (moduleName, containingFile) { return ts.resolveModuleName(moduleName, containingFile, options, host).resolvedModule; }; resolveModuleNamesWorker = function (moduleNames, containingFile) { return loadWithLocalCache(moduleNames, containingFile, loader_1); }; } var resolveTypeReferenceDirectiveNamesWorker; @@ -46186,15 +46450,15 @@ var ts; resolveTypeReferenceDirectiveNamesWorker = function (typeDirectiveNames, containingFile) { return host.resolveTypeReferenceDirectives(typeDirectiveNames, containingFile); }; } else { - var loader_2 = function (typesRef, containingFile) { return resolveTypeReferenceDirective(typesRef, containingFile, options, host).resolvedTypeReferenceDirective; }; + var loader_2 = function (typesRef, containingFile) { return ts.resolveTypeReferenceDirective(typesRef, containingFile, options, host).resolvedTypeReferenceDirective; }; resolveTypeReferenceDirectiveNamesWorker = function (typeReferenceDirectiveNames, containingFile) { return loadWithLocalCache(typeReferenceDirectiveNames, containingFile, loader_2); }; } var filesByName = ts.createFileMap(); var filesByNameIgnoreCase = host.useCaseSensitiveFileNames() ? ts.createFileMap(function (fileName) { return fileName.toLowerCase(); }) : undefined; if (!tryReuseStructureFromOldProgram()) { ts.forEach(rootNames, function (name) { return processRootFile(name, false); }); - var typeReferences = getAutomaticTypeDirectiveNames(options, host); - if (typeReferences) { + var typeReferences = ts.getAutomaticTypeDirectiveNames(options, host); + if (typeReferences.length) { var containingFilename = ts.combinePaths(host.getCurrentDirectory(), "__inferred type names__.ts"); var resolutions = resolveTypeReferenceDirectiveNamesWorker(typeReferences, containingFilename); for (var i = 0; i < typeReferences.length; i++) { @@ -46236,7 +46500,8 @@ var ts; getSymbolCount: function () { return getDiagnosticsProducingTypeChecker().getSymbolCount(); }, getTypeCount: function () { return getDiagnosticsProducingTypeChecker().getTypeCount(); }, getFileProcessingDiagnostics: function () { return fileProcessingDiagnostics; }, - getResolvedTypeReferenceDirectives: function () { return resolvedTypeReferenceDirectives; } + getResolvedTypeReferenceDirectives: function () { return resolvedTypeReferenceDirectives; }, + dropDiagnosticsProducingTypeChecker: dropDiagnosticsProducingTypeChecker }; verifyCompilerOptions(); ts.performance.mark("afterProgram"); @@ -46283,6 +46548,7 @@ var ts; (oldOptions.configFilePath !== options.configFilePath) || (oldOptions.baseUrl !== options.baseUrl) || (oldOptions.maxNodeModuleJsDepth !== options.maxNodeModuleJsDepth) || + !ts.arrayIsEqualTo(oldOptions.lib, options.lib) || !ts.arrayIsEqualTo(oldOptions.typeRoots, oldOptions.typeRoots) || !ts.arrayIsEqualTo(oldOptions.rootDirs, options.rootDirs) || !ts.equalOwnProperties(oldOptions.paths, options.paths)) { @@ -46383,16 +46649,19 @@ var ts; function getDiagnosticsProducingTypeChecker() { return diagnosticsProducingTypeChecker || (diagnosticsProducingTypeChecker = ts.createTypeChecker(program, true)); } + function dropDiagnosticsProducingTypeChecker() { + diagnosticsProducingTypeChecker = undefined; + } function getTypeChecker() { return noDiagnosticsTypeChecker || (noDiagnosticsTypeChecker = ts.createTypeChecker(program, false)); } - function emit(sourceFile, writeFileCallback, cancellationToken) { - return runWithCancellationToken(function () { return emitWorker(program, sourceFile, writeFileCallback, cancellationToken); }); + function emit(sourceFile, writeFileCallback, cancellationToken, emitOnlyDtsFiles) { + return runWithCancellationToken(function () { return emitWorker(program, sourceFile, writeFileCallback, cancellationToken, emitOnlyDtsFiles); }); } function isEmitBlocked(emitFileName) { return hasEmitBlockingDiagnostics.contains(ts.toPath(emitFileName, currentDirectory, getCanonicalFileName)); } - function emitWorker(program, sourceFile, writeFileCallback, cancellationToken) { + function emitWorker(program, sourceFile, writeFileCallback, cancellationToken, emitOnlyDtsFiles) { var declarationDiagnostics = []; if (options.noEmit) { return { diagnostics: declarationDiagnostics, sourceMaps: undefined, emittedFiles: undefined, emitSkipped: true }; @@ -46413,7 +46682,7 @@ var ts; } var emitResolver = getDiagnosticsProducingTypeChecker().getEmitResolver((options.outFile || options.out) ? undefined : sourceFile); ts.performance.mark("beforeEmit"); - var emitResult = ts.emitFiles(emitResolver, getEmitHost(writeFileCallback), sourceFile); + var emitResult = ts.emitFiles(emitResolver, getEmitHost(writeFileCallback), sourceFile, emitOnlyDtsFiles); ts.performance.mark("afterEmit"); ts.performance.measure("Emit", "beforeEmit", "afterEmit"); return emitResult; @@ -46928,7 +47197,6 @@ var ts; for (var i = 0; i < moduleNames.length; i++) { var resolution = resolutions[i]; ts.setResolvedModule(file, moduleNames[i], resolution); - var resolvedPath = resolution ? ts.toPath(resolution.resolvedFileName, currentDirectory, getCanonicalFileName) : undefined; var isFromNodeModulesSearch = resolution && resolution.isExternalLibraryImport; var isJsFileFromNodeModules = isFromNodeModulesSearch && ts.hasJavaScriptFileExtension(resolution.resolvedFileName); if (isFromNodeModulesSearch) { @@ -46940,7 +47208,7 @@ var ts; modulesWithElidedImports[file.path] = true; } else if (shouldAddFile) { - findSourceFile(resolution.resolvedFileName, resolvedPath, false, false, file, ts.skipTrivia(file.text, file.imports[i].pos), file.imports[i].end); + findSourceFile(resolution.resolvedFileName, ts.toPath(resolution.resolvedFileName, currentDirectory, getCanonicalFileName), false, false, file, ts.skipTrivia(file.text, file.imports[i].pos), file.imports[i].end); } if (isFromNodeModulesSearch) { currentNodeModulesDepth--; @@ -46954,8 +47222,8 @@ var ts; } function computeCommonSourceDirectory(sourceFiles) { var fileNames = []; - for (var _i = 0, sourceFiles_5 = sourceFiles; _i < sourceFiles_5.length; _i++) { - var file = sourceFiles_5[_i]; + for (var _i = 0, sourceFiles_6 = sourceFiles; _i < sourceFiles_6.length; _i++) { + var file = sourceFiles_6[_i]; if (!file.isDeclarationFile) { fileNames.push(file.fileName); } @@ -46966,8 +47234,8 @@ var ts; var allFilesBelongToPath = true; if (sourceFiles) { var absoluteRootDirectoryPath = host.getCanonicalFileName(ts.getNormalizedAbsolutePath(rootDirectory, currentDirectory)); - for (var _i = 0, sourceFiles_6 = sourceFiles; _i < sourceFiles_6.length; _i++) { - var sourceFile = sourceFiles_6[_i]; + for (var _i = 0, sourceFiles_7 = sourceFiles; _i < sourceFiles_7.length; _i++) { + var sourceFile = sourceFiles_7[_i]; if (!ts.isDeclarationFile(sourceFile)) { var absoluteSourceFilePath = host.getCanonicalFileName(ts.getNormalizedAbsolutePath(sourceFile.fileName, currentDirectory)); if (absoluteSourceFilePath.indexOf(absoluteRootDirectoryPath) !== 0) { @@ -47010,7 +47278,7 @@ var ts; if (!ts.hasProperty(options.paths, key)) { continue; } - if (!hasZeroOrOneAsteriskCharacter(key)) { + if (!ts.hasZeroOrOneAsteriskCharacter(key)) { programDiagnostics.add(ts.createCompilerDiagnostic(ts.Diagnostics.Pattern_0_can_have_at_most_one_Asterisk_character, key)); } if (ts.isArray(options.paths[key])) { @@ -47021,7 +47289,7 @@ var ts; var subst = _a[_i]; var typeOfSubst = typeof subst; if (typeOfSubst === "string") { - if (!hasZeroOrOneAsteriskCharacter(subst)) { + if (!ts.hasZeroOrOneAsteriskCharacter(subst)) { programDiagnostics.add(ts.createCompilerDiagnostic(ts.Diagnostics.Substitution_0_in_pattern_1_in_can_have_at_most_one_Asterisk_character, subst, key)); } } @@ -47060,6 +47328,9 @@ var ts; if (options.lib && options.noLib) { programDiagnostics.add(ts.createCompilerDiagnostic(ts.Diagnostics.Option_0_cannot_be_specified_with_option_1, "lib", "noLib")); } + if (options.noImplicitUseStrict && options.alwaysStrict) { + programDiagnostics.add(ts.createCompilerDiagnostic(ts.Diagnostics.Option_0_cannot_be_specified_with_option_1, "noImplicitUseStrict", "alwaysStrict")); + } var languageVersion = options.target || 0; var outFile = options.outFile || options.out; var firstNonAmbientExternalModuleSourceFile = ts.forEach(files, function (f) { return ts.isExternalModule(f) && !ts.isDeclarationFile(f) ? f : undefined; }); @@ -47136,11 +47407,13 @@ var ts; })(ts || (ts = {})); var ts; (function (ts) { + ts.compileOnSaveCommandLineOption = { name: "compileOnSave", type: "boolean" }; ts.optionDeclarations = [ { name: "charset", type: "string" }, + ts.compileOnSaveCommandLineOption, { name: "declaration", shortName: "d", @@ -47563,6 +47836,11 @@ var ts; name: "importHelpers", type: "boolean", description: ts.Diagnostics.Import_emit_helpers_from_tslib + }, + { + name: "alwaysStrict", + type: "boolean", + description: ts.Diagnostics.Parse_in_strict_mode_and_emit_use_strict_for_each_source_file } ]; ts.typingOptionDeclarations = [ @@ -47764,10 +48042,11 @@ var ts; return parseConfigFileTextToJson(fileName, text); } ts.readConfigFile = readConfigFile; - function parseConfigFileTextToJson(fileName, jsonText) { + function parseConfigFileTextToJson(fileName, jsonText, stripComments) { + if (stripComments === void 0) { stripComments = true; } try { - var jsonTextWithoutComments = removeComments(jsonText); - return { config: /\S/.test(jsonTextWithoutComments) ? JSON.parse(jsonTextWithoutComments) : {} }; + var jsonTextToParse = stripComments ? removeComments(jsonText) : jsonText; + return { config: /\S/.test(jsonTextToParse) ? JSON.parse(jsonTextToParse) : {} }; } catch (e) { return { error: ts.createCompilerDiagnostic(ts.Diagnostics.Failed_to_parse_file_0_Colon_1, fileName, e.message) }; @@ -47901,13 +48180,15 @@ var ts; options = ts.extend(existingOptions, options); options.configFilePath = configFileName; var _c = getFileNames(errors), fileNames = _c.fileNames, wildcardDirectories = _c.wildcardDirectories; + var compileOnSave = convertCompileOnSaveOptionFromJson(json, basePath, errors); return { options: options, fileNames: fileNames, typingOptions: typingOptions, raw: json, errors: errors, - wildcardDirectories: wildcardDirectories + wildcardDirectories: wildcardDirectories, + compileOnSave: compileOnSave }; function tryExtendsName(extendedConfig) { if (!(ts.isRootedDiskPath(extendedConfig) || ts.startsWith(ts.normalizeSlashes(extendedConfig), "./") || ts.startsWith(ts.normalizeSlashes(extendedConfig), "../"))) { @@ -47916,7 +48197,7 @@ var ts; } var extendedConfigPath = ts.toPath(extendedConfig, basePath, getCanonicalFileName); if (!host.fileExists(extendedConfigPath) && !ts.endsWith(extendedConfigPath, ".json")) { - extendedConfigPath = (extendedConfigPath + ".json"); + extendedConfigPath = extendedConfigPath + ".json"; if (!host.fileExists(extendedConfigPath)) { errors.push(ts.createCompilerDiagnostic(ts.Diagnostics.File_0_does_not_exist, extendedConfig)); return; @@ -47985,6 +48266,17 @@ var ts; var _b; } ts.parseJsonConfigFileContent = parseJsonConfigFileContent; + function convertCompileOnSaveOptionFromJson(jsonOption, basePath, errors) { + if (!ts.hasProperty(jsonOption, ts.compileOnSaveCommandLineOption.name)) { + return false; + } + var result = convertJsonOption(ts.compileOnSaveCommandLineOption, jsonOption["compileOnSave"], basePath, errors); + if (typeof result === "boolean" && result) { + return result; + } + return false; + } + ts.convertCompileOnSaveOptionFromJson = convertCompileOnSaveOptionFromJson; function convertCompilerOptionsFromJson(jsonOptions, basePath, configFileName) { var errors = []; var options = convertCompilerOptionsFromJsonWorker(jsonOptions, basePath, errors, configFileName); @@ -47998,7 +48290,9 @@ var ts; } ts.convertTypingOptionsFromJson = convertTypingOptionsFromJson; function convertCompilerOptionsFromJsonWorker(jsonOptions, basePath, errors, configFileName) { - var options = ts.getBaseFileName(configFileName) === "jsconfig.json" ? { allowJs: true, maxNodeModuleJsDepth: 2 } : {}; + var options = ts.getBaseFileName(configFileName) === "jsconfig.json" + ? { allowJs: true, maxNodeModuleJsDepth: 2, allowSyntheticDefaultImports: true, skipLibCheck: true } + : {}; convertOptionsFromJson(ts.optionDeclarations, jsonOptions, basePath, options, ts.Diagnostics.Unknown_compiler_option_0, errors); return options; } @@ -48294,8 +48588,7 @@ var ts; _a[ts.DiagnosticCategory.Warning] = yellowForegroundEscapeSequence, _a[ts.DiagnosticCategory.Error] = redForegroundEscapeSequence, _a[ts.DiagnosticCategory.Message] = blueForegroundEscapeSequence, - _a - )); + _a)); function formatAndReset(text, formatStyle) { return formatStyle + text + resetEscapeSequence; } diff --git a/lib/tsserver.js b/lib/tsserver.js index 8bb071cf4a1c7..f5437988c1a20 100644 --- a/lib/tsserver.js +++ b/lib/tsserver.js @@ -72,7 +72,6 @@ var ts; (function (ts) { ts.timestamp = typeof performance !== "undefined" && performance.now ? function () { return performance.now(); } : Date.now ? Date.now : function () { return +(new Date()); }; })(ts || (ts = {})); -var ts; (function (ts) { var performance; (function (performance) { @@ -150,6 +149,7 @@ var ts; contains: contains, remove: remove, forEachValue: forEachValueInMap, + getKeys: getKeys, clear: clear }; function forEachValueInMap(f) { @@ -157,6 +157,13 @@ var ts; f(key, files[key]); } } + function getKeys() { + var keys = []; + for (var key in files) { + keys.push(key); + } + return keys; + } function get(path) { return files[toKey(path)]; } @@ -533,16 +540,22 @@ var ts; : undefined; } ts.lastOrUndefined = lastOrUndefined; - function binarySearch(array, value) { + function binarySearch(array, value, comparer) { + if (!array || array.length === 0) { + return -1; + } var low = 0; var high = array.length - 1; + comparer = comparer !== undefined + ? comparer + : function (v1, v2) { return (v1 < v2 ? -1 : (v1 > v2 ? 1 : 0)); }; while (low <= high) { var middle = low + ((high - low) >> 1); var midValue = array[middle]; - if (midValue === value) { + if (comparer(midValue, value) === 0) { return middle; } - else if (midValue > value) { + else if (comparer(midValue, value) > 0) { high = middle - 1; } else { @@ -776,6 +789,56 @@ var ts; }; } ts.memoize = memoize; + function chain(a, b, c, d, e) { + if (e) { + var args_2 = []; + for (var i = 0; i < arguments.length; i++) { + args_2[i] = arguments[i]; + } + return function (t) { return compose.apply(void 0, map(args_2, function (f) { return f(t); })); }; + } + else if (d) { + return function (t) { return compose(a(t), b(t), c(t), d(t)); }; + } + else if (c) { + return function (t) { return compose(a(t), b(t), c(t)); }; + } + else if (b) { + return function (t) { return compose(a(t), b(t)); }; + } + else if (a) { + return function (t) { return compose(a(t)); }; + } + else { + return function (t) { return function (u) { return u; }; }; + } + } + ts.chain = chain; + function compose(a, b, c, d, e) { + if (e) { + var args_3 = []; + for (var i = 0; i < arguments.length; i++) { + args_3[i] = arguments[i]; + } + return function (t) { return reduceLeft(args_3, function (u, f) { return f(u); }, t); }; + } + else if (d) { + return function (t) { return d(c(b(a(t)))); }; + } + else if (c) { + return function (t) { return c(b(a(t))); }; + } + else if (b) { + return function (t) { return b(a(t)); }; + } + else if (a) { + return function (t) { return a(t); }; + } + else { + return function (t) { return t; }; + } + } + ts.compose = compose; function formatStringFromArgs(text, args, baseIndex) { baseIndex = baseIndex || 0; return text.replace(/{(\d+)}/g, function (match, index) { return args[+index + baseIndex]; }); @@ -1012,10 +1075,45 @@ var ts; return path && !isRootedDiskPath(path) && path.indexOf("://") !== -1; } ts.isUrl = isUrl; + function isExternalModuleNameRelative(moduleName) { + return /^\.\.?($|[\\/])/.test(moduleName); + } + ts.isExternalModuleNameRelative = isExternalModuleNameRelative; + function getEmitScriptTarget(compilerOptions) { + return compilerOptions.target || 0; + } + ts.getEmitScriptTarget = getEmitScriptTarget; + function getEmitModuleKind(compilerOptions) { + return typeof compilerOptions.module === "number" ? + compilerOptions.module : + getEmitScriptTarget(compilerOptions) === 2 ? ts.ModuleKind.ES6 : ts.ModuleKind.CommonJS; + } + ts.getEmitModuleKind = getEmitModuleKind; + function hasZeroOrOneAsteriskCharacter(str) { + var seenAsterisk = false; + for (var i = 0; i < str.length; i++) { + if (str.charCodeAt(i) === 42) { + if (!seenAsterisk) { + seenAsterisk = true; + } + else { + return false; + } + } + } + return true; + } + ts.hasZeroOrOneAsteriskCharacter = hasZeroOrOneAsteriskCharacter; function isRootedDiskPath(path) { return getRootLength(path) !== 0; } ts.isRootedDiskPath = isRootedDiskPath; + function convertToRelativePath(absoluteOrRelativePath, basePath, getCanonicalFileName) { + return !isRootedDiskPath(absoluteOrRelativePath) + ? absoluteOrRelativePath + : getRelativePathToDirectoryOrUrl(basePath, absoluteOrRelativePath, basePath, getCanonicalFileName, false); + } + ts.convertToRelativePath = convertToRelativePath; function normalizedPathComponents(path, rootLength) { var normalizedParts = getNormalizedParts(path, rootLength); return [path.substr(0, rootLength)].concat(normalizedParts); @@ -1389,6 +1487,14 @@ var ts; return options && options.allowJs ? allSupportedExtensions : ts.supportedTypeScriptExtensions; } ts.getSupportedExtensions = getSupportedExtensions; + function hasJavaScriptFileExtension(fileName) { + return forEach(ts.supportedJavascriptExtensions, function (extension) { return fileExtensionIs(fileName, extension); }); + } + ts.hasJavaScriptFileExtension = hasJavaScriptFileExtension; + function hasTypeScriptFileExtension(fileName) { + return forEach(ts.supportedTypeScriptExtensions, function (extension) { return fileExtensionIs(fileName, extension); }); + } + ts.hasTypeScriptFileExtension = hasTypeScriptFileExtension; function isSupportedSourceFileName(fileName, compilerOptions) { if (!fileName) { return false; @@ -1480,7 +1586,6 @@ var ts; this.transformFlags = 0; this.parent = undefined; this.original = undefined; - this.transformId = 0; } ts.objectAllocator = { getNodeConstructor: function () { return Node; }, @@ -1493,9 +1598,9 @@ var ts; }; var Debug; (function (Debug) { - var currentAssertionLevel; + Debug.currentAssertionLevel = 0; function shouldAssert(level) { - return getCurrentAssertionLevel() >= level; + return Debug.currentAssertionLevel >= level; } Debug.shouldAssert = shouldAssert; function assert(expression, message, verboseDebugInfo) { @@ -1513,30 +1618,7 @@ var ts; Debug.assert(false, message); } Debug.fail = fail; - function getCurrentAssertionLevel() { - if (currentAssertionLevel !== undefined) { - return currentAssertionLevel; - } - if (ts.sys === undefined) { - return 0; - } - var developmentMode = /^development$/i.test(getEnvironmentVariable("NODE_ENV")); - currentAssertionLevel = developmentMode - ? 1 - : 0; - return currentAssertionLevel; - } })(Debug = ts.Debug || (ts.Debug = {})); - function getEnvironmentVariable(name, host) { - if (host && host.getEnvironmentVariable) { - return host.getEnvironmentVariable(name); - } - if (ts.sys && ts.sys.getEnvironmentVariable) { - return ts.sys.getEnvironmentVariable(name); - } - return ""; - } - ts.getEnvironmentVariable = getEnvironmentVariable; function orderedRemoveItemAt(array, index) { for (var i = index; i < array.length - 1; i++) { array[i] = array[i + 1]; @@ -1567,6 +1649,64 @@ var ts; : (function (fileName) { return fileName.toLowerCase(); }); } ts.createGetCanonicalFileName = createGetCanonicalFileName; + function matchPatternOrExact(patternStrings, candidate) { + var patterns = []; + for (var _i = 0, patternStrings_1 = patternStrings; _i < patternStrings_1.length; _i++) { + var patternString = patternStrings_1[_i]; + var pattern = tryParsePattern(patternString); + if (pattern) { + patterns.push(pattern); + } + else if (patternString === candidate) { + return patternString; + } + } + return findBestPatternMatch(patterns, function (_) { return _; }, candidate); + } + ts.matchPatternOrExact = matchPatternOrExact; + function patternText(_a) { + var prefix = _a.prefix, suffix = _a.suffix; + return prefix + "*" + suffix; + } + ts.patternText = patternText; + function matchedText(pattern, candidate) { + Debug.assert(isPatternMatch(pattern, candidate)); + return candidate.substr(pattern.prefix.length, candidate.length - pattern.suffix.length); + } + ts.matchedText = matchedText; + function findBestPatternMatch(values, getPattern, candidate) { + var matchedValue = undefined; + var longestMatchPrefixLength = -1; + for (var _i = 0, values_1 = values; _i < values_1.length; _i++) { + var v = values_1[_i]; + var pattern = getPattern(v); + if (isPatternMatch(pattern, candidate) && pattern.prefix.length > longestMatchPrefixLength) { + longestMatchPrefixLength = pattern.prefix.length; + matchedValue = v; + } + } + return matchedValue; + } + ts.findBestPatternMatch = findBestPatternMatch; + function isPatternMatch(_a, candidate) { + var prefix = _a.prefix, suffix = _a.suffix; + return candidate.length >= prefix.length + suffix.length && + startsWith(candidate, prefix) && + endsWith(candidate, suffix); + } + function tryParsePattern(pattern) { + Debug.assert(hasZeroOrOneAsteriskCharacter(pattern)); + var indexOfStar = pattern.indexOf("*"); + return indexOfStar === -1 ? undefined : { + prefix: pattern.substr(0, indexOfStar), + suffix: pattern.substr(indexOfStar + 1) + }; + } + ts.tryParsePattern = tryParsePattern; + function positionIsSynthesized(pos) { + return !(pos >= 0); + } + ts.positionIsSynthesized = positionIsSynthesized; })(ts || (ts = {})); var ts; (function (ts) { @@ -1760,8 +1900,14 @@ var ts; function isNode4OrLater() { return parseInt(process.version.charAt(1)) >= 4; } + function isFileSystemCaseSensitive() { + if (platform === "win32" || platform === "win64") { + return false; + } + return !fileExists(__filename.toUpperCase()) || !fileExists(__filename.toLowerCase()); + } var platform = _os.platform(); - var useCaseSensitiveFileNames = platform !== "win32" && platform !== "win64" && platform !== "darwin"; + var useCaseSensitiveFileNames = isFileSystemCaseSensitive(); function readFile(fileName, encoding) { if (!fileExists(fileName)) { return undefined; @@ -1886,6 +2032,9 @@ var ts; }, watchDirectory: function (directoryName, callback, recursive) { var options; + if (!directoryExists(directoryName)) { + return; + } if (isNode4OrLater() && (process.platform === "win32" || process.platform === "darwin")) { options = { persistent: true, recursive: !!recursive }; } @@ -1997,19 +2146,43 @@ var ts; realpath: realpath }; } + function recursiveCreateDirectory(directoryPath, sys) { + var basePath = ts.getDirectoryPath(directoryPath); + var shouldCreateParent = directoryPath !== basePath && !sys.directoryExists(basePath); + if (shouldCreateParent) { + recursiveCreateDirectory(basePath, sys); + } + if (shouldCreateParent || !sys.directoryExists(directoryPath)) { + sys.createDirectory(directoryPath); + } + } + var sys; if (typeof ChakraHost !== "undefined") { - return getChakraSystem(); + sys = getChakraSystem(); } else if (typeof WScript !== "undefined" && typeof ActiveXObject === "function") { - return getWScriptSystem(); + sys = getWScriptSystem(); } else if (typeof process !== "undefined" && process.nextTick && !process.browser && typeof require !== "undefined") { - return getNodeSystem(); + sys = getNodeSystem(); } - else { - return undefined; + if (sys) { + var originalWriteFile_1 = sys.writeFile; + sys.writeFile = function (path, data, writeBom) { + var directoryPath = ts.getDirectoryPath(ts.normalizeSlashes(path)); + if (directoryPath && !sys.directoryExists(directoryPath)) { + recursiveCreateDirectory(directoryPath, sys); + } + originalWriteFile_1.call(sys, path, data, writeBom); + }; } + return sys; })(); + if (ts.sys && ts.sys.getEnvironmentVariable) { + ts.Debug.currentAssertionLevel = /^development$/i.test(ts.sys.getEnvironmentVariable("NODE_ENV")) + ? 1 + : 0; + } })(ts || (ts = {})); var ts; (function (ts) { @@ -2334,7 +2507,7 @@ var ts; The_right_hand_side_of_a_for_in_statement_must_be_of_type_any_an_object_type_or_a_type_parameter: { code: 2407, category: ts.DiagnosticCategory.Error, key: "The_right_hand_side_of_a_for_in_statement_must_be_of_type_any_an_object_type_or_a_type_parameter_2407", message: "The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter." }, Setters_cannot_return_a_value: { code: 2408, category: ts.DiagnosticCategory.Error, key: "Setters_cannot_return_a_value_2408", message: "Setters cannot return a value." }, Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class: { code: 2409, category: ts.DiagnosticCategory.Error, key: "Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class_2409", message: "Return type of constructor signature must be assignable to the instance type of the class" }, - All_symbols_within_a_with_block_will_be_resolved_to_any: { code: 2410, category: ts.DiagnosticCategory.Error, key: "All_symbols_within_a_with_block_will_be_resolved_to_any_2410", message: "All symbols within a 'with' block will be resolved to 'any'." }, + The_with_statement_is_not_supported_All_symbols_in_a_with_block_will_have_type_any: { code: 2410, category: ts.DiagnosticCategory.Error, key: "The_with_statement_is_not_supported_All_symbols_in_a_with_block_will_have_type_any_2410", message: "The 'with' statement is not supported. All symbols in a 'with' block will have type 'any'." }, Property_0_of_type_1_is_not_assignable_to_string_index_type_2: { code: 2411, category: ts.DiagnosticCategory.Error, key: "Property_0_of_type_1_is_not_assignable_to_string_index_type_2_2411", message: "Property '{0}' of type '{1}' is not assignable to string index type '{2}'." }, Property_0_of_type_1_is_not_assignable_to_numeric_index_type_2: { code: 2412, category: ts.DiagnosticCategory.Error, key: "Property_0_of_type_1_is_not_assignable_to_numeric_index_type_2_2412", message: "Property '{0}' of type '{1}' is not assignable to numeric index type '{2}'." }, Numeric_index_type_0_is_not_assignable_to_string_index_type_1: { code: 2413, category: ts.DiagnosticCategory.Error, key: "Numeric_index_type_0_is_not_assignable_to_string_index_type_1_2413", message: "Numeric index type '{0}' is not assignable to string index type '{1}'." }, @@ -2495,7 +2668,7 @@ var ts; this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation: { code: 2683, category: ts.DiagnosticCategory.Error, key: "this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation_2683", message: "'this' implicitly has type 'any' because it does not have a type annotation." }, The_this_context_of_type_0_is_not_assignable_to_method_s_this_of_type_1: { code: 2684, category: ts.DiagnosticCategory.Error, key: "The_this_context_of_type_0_is_not_assignable_to_method_s_this_of_type_1_2684", message: "The 'this' context of type '{0}' is not assignable to method's 'this' of type '{1}'." }, The_this_types_of_each_signature_are_incompatible: { code: 2685, category: ts.DiagnosticCategory.Error, key: "The_this_types_of_each_signature_are_incompatible_2685", message: "The 'this' types of each signature are incompatible." }, - Identifier_0_must_be_imported_from_a_module: { code: 2686, category: ts.DiagnosticCategory.Error, key: "Identifier_0_must_be_imported_from_a_module_2686", message: "Identifier '{0}' must be imported from a module" }, + _0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead: { code: 2686, category: ts.DiagnosticCategory.Error, key: "_0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead_2686", message: "'{0}' refers to a UMD global, but the current file is a module. Consider adding an import instead." }, All_declarations_of_0_must_have_identical_modifiers: { code: 2687, category: ts.DiagnosticCategory.Error, key: "All_declarations_of_0_must_have_identical_modifiers_2687", message: "All declarations of '{0}' must have identical modifiers." }, Cannot_find_type_definition_file_for_0: { code: 2688, category: ts.DiagnosticCategory.Error, key: "Cannot_find_type_definition_file_for_0_2688", message: "Cannot find type definition file for '{0}'." }, Cannot_extend_an_interface_0_Did_you_mean_implements: { code: 2689, category: ts.DiagnosticCategory.Error, key: "Cannot_extend_an_interface_0_Did_you_mean_implements_2689", message: "Cannot extend an interface '{0}'. Did you mean 'implements'?" }, @@ -2728,6 +2901,8 @@ var ts; No_types_specified_in_package_json_but_allowJs_is_set_so_returning_main_value_of_0: { code: 6137, category: ts.DiagnosticCategory.Message, key: "No_types_specified_in_package_json_but_allowJs_is_set_so_returning_main_value_of_0_6137", message: "No types specified in 'package.json' but 'allowJs' is set, so returning 'main' value of '{0}'" }, Property_0_is_declared_but_never_used: { code: 6138, category: ts.DiagnosticCategory.Error, key: "Property_0_is_declared_but_never_used_6138", message: "Property '{0}' is declared but never used." }, Import_emit_helpers_from_tslib: { code: 6139, category: ts.DiagnosticCategory.Message, key: "Import_emit_helpers_from_tslib_6139", message: "Import emit helpers from 'tslib'." }, + Auto_discovery_for_typings_is_enabled_in_project_0_Running_extra_resolution_pass_for_module_1_using_cache_location_2: { code: 6140, category: ts.DiagnosticCategory.Error, key: "Auto_discovery_for_typings_is_enabled_in_project_0_Running_extra_resolution_pass_for_module_1_using__6140", message: "Auto discovery for typings is enabled in project '{0}'. Running extra resolution pass for module '{1}' using cache location '{2}'." }, + Parse_in_strict_mode_and_emit_use_strict_for_each_source_file: { code: 6141, category: ts.DiagnosticCategory.Message, key: "Parse_in_strict_mode_and_emit_use_strict_for_each_source_file_6141", message: "Parse in strict mode and emit \"use strict\" for each source file" }, Variable_0_implicitly_has_an_1_type: { code: 7005, category: ts.DiagnosticCategory.Error, key: "Variable_0_implicitly_has_an_1_type_7005", message: "Variable '{0}' implicitly has an '{1}' type." }, Parameter_0_implicitly_has_an_1_type: { code: 7006, category: ts.DiagnosticCategory.Error, key: "Parameter_0_implicitly_has_an_1_type_7006", message: "Parameter '{0}' implicitly has an '{1}' type." }, Member_0_implicitly_has_an_1_type: { code: 7008, category: ts.DiagnosticCategory.Error, key: "Member_0_implicitly_has_an_1_type_7008", message: "Member '{0}' implicitly has an '{1}' type." }, @@ -2752,6 +2927,7 @@ var ts; Binding_element_0_implicitly_has_an_1_type: { code: 7031, category: ts.DiagnosticCategory.Error, key: "Binding_element_0_implicitly_has_an_1_type_7031", message: "Binding element '{0}' implicitly has an '{1}' type." }, Property_0_implicitly_has_type_any_because_its_set_accessor_lacks_a_parameter_type_annotation: { code: 7032, category: ts.DiagnosticCategory.Error, key: "Property_0_implicitly_has_type_any_because_its_set_accessor_lacks_a_parameter_type_annotation_7032", message: "Property '{0}' implicitly has type 'any', because its set accessor lacks a parameter type annotation." }, Property_0_implicitly_has_type_any_because_its_get_accessor_lacks_a_return_type_annotation: { code: 7033, category: ts.DiagnosticCategory.Error, key: "Property_0_implicitly_has_type_any_because_its_get_accessor_lacks_a_return_type_annotation_7033", message: "Property '{0}' implicitly has type 'any', because its get accessor lacks a return type annotation." }, + Variable_0_implicitly_has_type_any_in_some_locations_where_its_type_cannot_be_determined: { code: 7034, category: ts.DiagnosticCategory.Error, key: "Variable_0_implicitly_has_type_any_in_some_locations_where_its_type_cannot_be_determined_7034", message: "Variable '{0}' implicitly has type 'any' in some locations where its type cannot be determined." }, You_cannot_rename_this_element: { code: 8000, category: ts.DiagnosticCategory.Error, key: "You_cannot_rename_this_element_8000", message: "You cannot rename this element." }, You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library: { code: 8001, category: ts.DiagnosticCategory.Error, key: "You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library_8001", message: "You cannot rename elements that are defined in the standard TypeScript library." }, import_can_only_be_used_in_a_ts_file: { code: 8002, category: ts.DiagnosticCategory.Error, key: "import_can_only_be_used_in_a_ts_file_8002", message: "'import ... =' can only be used in a .ts file." }, @@ -2781,7 +2957,14 @@ var ts; super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class: { code: 17009, category: ts.DiagnosticCategory.Error, key: "super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class_17009", message: "'super' must be called before accessing 'this' in the constructor of a derived class." }, Unknown_typing_option_0: { code: 17010, category: ts.DiagnosticCategory.Error, key: "Unknown_typing_option_0_17010", message: "Unknown typing option '{0}'." }, Circularity_detected_while_resolving_configuration_Colon_0: { code: 18000, category: ts.DiagnosticCategory.Error, key: "Circularity_detected_while_resolving_configuration_Colon_0_18000", message: "Circularity detected while resolving configuration: {0}" }, - The_path_in_an_extends_options_must_be_relative_or_rooted: { code: 18001, category: ts.DiagnosticCategory.Error, key: "The_path_in_an_extends_options_must_be_relative_or_rooted_18001", message: "The path in an 'extends' options must be relative or rooted." } + The_path_in_an_extends_options_must_be_relative_or_rooted: { code: 18001, category: ts.DiagnosticCategory.Error, key: "The_path_in_an_extends_options_must_be_relative_or_rooted_18001", message: "The path in an 'extends' options must be relative or rooted." }, + Add_missing_super_call: { code: 90001, category: ts.DiagnosticCategory.Message, key: "Add_missing_super_call_90001", message: "Add missing 'super()' call." }, + Make_super_call_the_first_statement_in_the_constructor: { code: 90002, category: ts.DiagnosticCategory.Message, key: "Make_super_call_the_first_statement_in_the_constructor_90002", message: "Make 'super()' call the first statement in the constructor." }, + Change_extends_to_implements: { code: 90003, category: ts.DiagnosticCategory.Message, key: "Change_extends_to_implements_90003", message: "Change 'extends' to 'implements'" }, + Remove_unused_identifiers: { code: 90004, category: ts.DiagnosticCategory.Message, key: "Remove_unused_identifiers_90004", message: "Remove unused identifiers" }, + Implement_interface_on_reference: { code: 90005, category: ts.DiagnosticCategory.Message, key: "Implement_interface_on_reference_90005", message: "Implement interface on reference" }, + Implement_interface_on_class: { code: 90006, category: ts.DiagnosticCategory.Message, key: "Implement_interface_on_class_90006", message: "Implement interface on class" }, + Implement_inherited_abstract_class: { code: 90007, category: ts.DiagnosticCategory.Message, key: "Implement_inherited_abstract_class_90007", message: "Implement inherited abstract class" } }; })(ts || (ts = {})); var ts; @@ -3691,7 +3874,7 @@ var ts; return token = 69; } function scanBinaryOrOctalDigits(base) { - ts.Debug.assert(base !== 2 || base !== 8, "Expected either base 2 or base 8"); + ts.Debug.assert(base === 2 || base === 8, "Expected either base 2 or base 8"); var value = 0; var numberOfDigits = 0; while (true) { @@ -4334,11 +4517,13 @@ var ts; })(ts || (ts = {})); var ts; (function (ts) { + ts.compileOnSaveCommandLineOption = { name: "compileOnSave", type: "boolean" }; ts.optionDeclarations = [ { name: "charset", type: "string" }, + ts.compileOnSaveCommandLineOption, { name: "declaration", shortName: "d", @@ -4761,6 +4946,11 @@ var ts; name: "importHelpers", type: "boolean", description: ts.Diagnostics.Import_emit_helpers_from_tslib + }, + { + name: "alwaysStrict", + type: "boolean", + description: ts.Diagnostics.Parse_in_strict_mode_and_emit_use_strict_for_each_source_file } ]; ts.typingOptionDeclarations = [ @@ -4962,10 +5152,11 @@ var ts; return parseConfigFileTextToJson(fileName, text); } ts.readConfigFile = readConfigFile; - function parseConfigFileTextToJson(fileName, jsonText) { + function parseConfigFileTextToJson(fileName, jsonText, stripComments) { + if (stripComments === void 0) { stripComments = true; } try { - var jsonTextWithoutComments = removeComments(jsonText); - return { config: /\S/.test(jsonTextWithoutComments) ? JSON.parse(jsonTextWithoutComments) : {} }; + var jsonTextToParse = stripComments ? removeComments(jsonText) : jsonText; + return { config: /\S/.test(jsonTextToParse) ? JSON.parse(jsonTextToParse) : {} }; } catch (e) { return { error: ts.createCompilerDiagnostic(ts.Diagnostics.Failed_to_parse_file_0_Colon_1, fileName, e.message) }; @@ -5099,13 +5290,15 @@ var ts; options = ts.extend(existingOptions, options); options.configFilePath = configFileName; var _c = getFileNames(errors), fileNames = _c.fileNames, wildcardDirectories = _c.wildcardDirectories; + var compileOnSave = convertCompileOnSaveOptionFromJson(json, basePath, errors); return { options: options, fileNames: fileNames, typingOptions: typingOptions, raw: json, errors: errors, - wildcardDirectories: wildcardDirectories + wildcardDirectories: wildcardDirectories, + compileOnSave: compileOnSave }; function tryExtendsName(extendedConfig) { if (!(ts.isRootedDiskPath(extendedConfig) || ts.startsWith(ts.normalizeSlashes(extendedConfig), "./") || ts.startsWith(ts.normalizeSlashes(extendedConfig), "../"))) { @@ -5183,6 +5376,17 @@ var ts; var _b; } ts.parseJsonConfigFileContent = parseJsonConfigFileContent; + function convertCompileOnSaveOptionFromJson(jsonOption, basePath, errors) { + if (!ts.hasProperty(jsonOption, ts.compileOnSaveCommandLineOption.name)) { + return false; + } + var result = convertJsonOption(ts.compileOnSaveCommandLineOption, jsonOption["compileOnSave"], basePath, errors); + if (typeof result === "boolean" && result) { + return result; + } + return false; + } + ts.convertCompileOnSaveOptionFromJson = convertCompileOnSaveOptionFromJson; function convertCompilerOptionsFromJson(jsonOptions, basePath, configFileName) { var errors = []; var options = convertCompilerOptionsFromJsonWorker(jsonOptions, basePath, errors, configFileName); @@ -5196,7 +5400,9 @@ var ts; } ts.convertTypingOptionsFromJson = convertTypingOptionsFromJson; function convertCompilerOptionsFromJsonWorker(jsonOptions, basePath, errors, configFileName) { - var options = ts.getBaseFileName(configFileName) === "jsconfig.json" ? { allowJs: true, maxNodeModuleJsDepth: 2 } : {}; + var options = ts.getBaseFileName(configFileName) === "jsconfig.json" + ? { allowJs: true, maxNodeModuleJsDepth: 2, allowSyntheticDefaultImports: true, skipLibCheck: true } + : {}; convertOptionsFromJson(ts.optionDeclarations, jsonOptions, basePath, options, ts.Diagnostics.Unknown_compiler_option_0, errors); return options; } @@ -5396,310 +5602,1241 @@ var ts; })(ts || (ts = {})); var ts; (function (ts) { - ts.externalHelpersModuleNameText = "tslib"; - function getDeclarationOfKind(symbol, kind) { - var declarations = symbol.declarations; - if (declarations) { - for (var _i = 0, declarations_1 = declarations; _i < declarations_1.length; _i++) { - var declaration = declarations_1[_i]; - if (declaration.kind === kind) { - return declaration; + var JsTyping; + (function (JsTyping) { + ; + ; + var safeList; + var EmptySafeList = ts.createMap(); + function discoverTypings(host, fileNames, projectRootPath, safeListPath, packageNameToTypingLocation, typingOptions, compilerOptions) { + var inferredTypings = ts.createMap(); + if (!typingOptions || !typingOptions.enableAutoDiscovery) { + return { cachedTypingPaths: [], newTypingNames: [], filesToWatch: [] }; + } + fileNames = ts.filter(ts.map(fileNames, ts.normalizePath), function (f) { + var kind = ts.ensureScriptKind(f, ts.getScriptKindFromFileName(f)); + return kind === 1 || kind === 2; + }); + if (!safeList) { + var result = ts.readConfigFile(safeListPath, function (path) { return host.readFile(path); }); + safeList = result.config ? ts.createMap(result.config) : EmptySafeList; + } + var filesToWatch = []; + var searchDirs = []; + var exclude = []; + mergeTypings(typingOptions.include); + exclude = typingOptions.exclude || []; + var possibleSearchDirs = ts.map(fileNames, ts.getDirectoryPath); + if (projectRootPath !== undefined) { + possibleSearchDirs.push(projectRootPath); + } + searchDirs = ts.deduplicate(possibleSearchDirs); + for (var _i = 0, searchDirs_1 = searchDirs; _i < searchDirs_1.length; _i++) { + var searchDir = searchDirs_1[_i]; + var packageJsonPath = ts.combinePaths(searchDir, "package.json"); + getTypingNamesFromJson(packageJsonPath, filesToWatch); + var bowerJsonPath = ts.combinePaths(searchDir, "bower.json"); + getTypingNamesFromJson(bowerJsonPath, filesToWatch); + var nodeModulesPath = ts.combinePaths(searchDir, "node_modules"); + getTypingNamesFromNodeModuleFolder(nodeModulesPath); + } + getTypingNamesFromSourceFileNames(fileNames); + for (var name_7 in packageNameToTypingLocation) { + if (name_7 in inferredTypings && !inferredTypings[name_7]) { + inferredTypings[name_7] = packageNameToTypingLocation[name_7]; + } + } + for (var _a = 0, exclude_1 = exclude; _a < exclude_1.length; _a++) { + var excludeTypingName = exclude_1[_a]; + delete inferredTypings[excludeTypingName]; + } + var newTypingNames = []; + var cachedTypingPaths = []; + for (var typing in inferredTypings) { + if (inferredTypings[typing] !== undefined) { + cachedTypingPaths.push(inferredTypings[typing]); + } + else { + newTypingNames.push(typing); + } + } + return { cachedTypingPaths: cachedTypingPaths, newTypingNames: newTypingNames, filesToWatch: filesToWatch }; + function mergeTypings(typingNames) { + if (!typingNames) { + return; + } + for (var _i = 0, typingNames_1 = typingNames; _i < typingNames_1.length; _i++) { + var typing = typingNames_1[_i]; + if (!(typing in inferredTypings)) { + inferredTypings[typing] = undefined; + } + } + } + function getTypingNamesFromJson(jsonPath, filesToWatch) { + var result = ts.readConfigFile(jsonPath, function (path) { return host.readFile(path); }); + if (result.config) { + var jsonConfig = result.config; + filesToWatch.push(jsonPath); + if (jsonConfig.dependencies) { + mergeTypings(ts.getOwnKeys(jsonConfig.dependencies)); + } + if (jsonConfig.devDependencies) { + mergeTypings(ts.getOwnKeys(jsonConfig.devDependencies)); + } + if (jsonConfig.optionalDependencies) { + mergeTypings(ts.getOwnKeys(jsonConfig.optionalDependencies)); + } + if (jsonConfig.peerDependencies) { + mergeTypings(ts.getOwnKeys(jsonConfig.peerDependencies)); + } + } + } + function getTypingNamesFromSourceFileNames(fileNames) { + var jsFileNames = ts.filter(fileNames, ts.hasJavaScriptFileExtension); + var inferredTypingNames = ts.map(jsFileNames, function (f) { return ts.removeFileExtension(ts.getBaseFileName(f.toLowerCase())); }); + var cleanedTypingNames = ts.map(inferredTypingNames, function (f) { return f.replace(/((?:\.|-)min(?=\.|$))|((?:-|\.)\d+)/g, ""); }); + if (safeList !== EmptySafeList) { + mergeTypings(ts.filter(cleanedTypingNames, function (f) { return f in safeList; })); + } + var hasJsxFile = ts.forEach(fileNames, function (f) { return ts.ensureScriptKind(f, ts.getScriptKindFromFileName(f)) === 2; }); + if (hasJsxFile) { + mergeTypings(["react"]); + } + } + function getTypingNamesFromNodeModuleFolder(nodeModulesPath) { + if (!host.directoryExists(nodeModulesPath)) { + return; + } + var typingNames = []; + var fileNames = host.readDirectory(nodeModulesPath, [".json"], undefined, undefined, 2); + for (var _i = 0, fileNames_2 = fileNames; _i < fileNames_2.length; _i++) { + var fileName = fileNames_2[_i]; + var normalizedFileName = ts.normalizePath(fileName); + if (ts.getBaseFileName(normalizedFileName) !== "package.json") { + continue; + } + var result = ts.readConfigFile(normalizedFileName, function (path) { return host.readFile(path); }); + if (!result.config) { + continue; + } + var packageJson = result.config; + if (packageJson._requiredBy && + ts.filter(packageJson._requiredBy, function (r) { return r[0] === "#" || r === "/"; }).length === 0) { + continue; + } + if (!packageJson.name) { + continue; + } + if (packageJson.typings) { + var absolutePath = ts.getNormalizedAbsolutePath(packageJson.typings, ts.getDirectoryPath(normalizedFileName)); + inferredTypings[packageJson.name] = absolutePath; + } + else { + typingNames.push(packageJson.name); + } } + mergeTypings(typingNames); } } - return undefined; - } - ts.getDeclarationOfKind = getDeclarationOfKind; - var stringWriters = []; - function getSingleLineStringWriter() { - if (stringWriters.length === 0) { - var str_1 = ""; - var writeText = function (text) { return str_1 += text; }; + JsTyping.discoverTypings = discoverTypings; + })(JsTyping = ts.JsTyping || (ts.JsTyping = {})); +})(ts || (ts = {})); +var ts; +(function (ts) { + var server; + (function (server) { + (function (LogLevel) { + LogLevel[LogLevel["terse"] = 0] = "terse"; + LogLevel[LogLevel["normal"] = 1] = "normal"; + LogLevel[LogLevel["requestTime"] = 2] = "requestTime"; + LogLevel[LogLevel["verbose"] = 3] = "verbose"; + })(server.LogLevel || (server.LogLevel = {})); + var LogLevel = server.LogLevel; + server.emptyArray = []; + var Msg; + (function (Msg) { + Msg.Err = "Err"; + Msg.Info = "Info"; + Msg.Perf = "Perf"; + })(Msg = server.Msg || (server.Msg = {})); + function getProjectRootPath(project) { + switch (project.projectKind) { + case server.ProjectKind.Configured: + return ts.getDirectoryPath(project.getProjectName()); + case server.ProjectKind.Inferred: + return ""; + case server.ProjectKind.External: + var projectName = ts.normalizeSlashes(project.getProjectName()); + return project.projectService.host.fileExists(projectName) ? ts.getDirectoryPath(projectName) : projectName; + } + } + function createInstallTypingsRequest(project, typingOptions, cachePath) { return { - string: function () { return str_1; }, - writeKeyword: writeText, - writeOperator: writeText, - writePunctuation: writeText, - writeSpace: writeText, - writeStringLiteral: writeText, - writeParameter: writeText, - writeSymbol: writeText, - writeLine: function () { return str_1 += " "; }, - increaseIndent: function () { }, - decreaseIndent: function () { }, - clear: function () { return str_1 = ""; }, - trackSymbol: function () { }, - reportInaccessibleThisError: function () { } + projectName: project.getProjectName(), + fileNames: project.getFileNames(), + compilerOptions: project.getCompilerOptions(), + typingOptions: typingOptions, + projectRootPath: getProjectRootPath(project), + cachePath: cachePath, + kind: "discover" }; } - return stringWriters.pop(); - } - ts.getSingleLineStringWriter = getSingleLineStringWriter; - function releaseStringWriter(writer) { - writer.clear(); - stringWriters.push(writer); - } - ts.releaseStringWriter = releaseStringWriter; - function getFullWidth(node) { - return node.end - node.pos; - } - ts.getFullWidth = getFullWidth; - function arrayIsEqualTo(array1, array2, equaler) { - if (!array1 || !array2) { - return array1 === array2; + server.createInstallTypingsRequest = createInstallTypingsRequest; + var Errors; + (function (Errors) { + function ThrowNoProject() { + throw new Error("No Project."); + } + Errors.ThrowNoProject = ThrowNoProject; + function ThrowProjectLanguageServiceDisabled() { + throw new Error("The project's language service is disabled."); + } + Errors.ThrowProjectLanguageServiceDisabled = ThrowProjectLanguageServiceDisabled; + function ThrowProjectDoesNotContainDocument(fileName, project) { + throw new Error("Project '" + project.getProjectName() + "' does not contain document '" + fileName + "'"); + } + Errors.ThrowProjectDoesNotContainDocument = ThrowProjectDoesNotContainDocument; + })(Errors = server.Errors || (server.Errors = {})); + function getDefaultFormatCodeSettings(host) { + return { + indentSize: 4, + tabSize: 4, + newLineCharacter: host.newLine || "\n", + convertTabsToSpaces: true, + indentStyle: ts.IndentStyle.Smart, + insertSpaceAfterCommaDelimiter: true, + insertSpaceAfterSemicolonInForStatements: true, + insertSpaceBeforeAndAfterBinaryOperators: true, + insertSpaceAfterKeywordsInControlFlowStatements: true, + insertSpaceAfterFunctionKeywordForAnonymousFunctions: false, + insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false, + insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false, + insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false, + insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: false, + placeOpenBraceOnNewLineForFunctions: false, + placeOpenBraceOnNewLineForControlBlocks: false + }; } - if (array1.length !== array2.length) { - return false; + server.getDefaultFormatCodeSettings = getDefaultFormatCodeSettings; + function mergeMaps(target, source) { + for (var key in source) { + if (ts.hasProperty(source, key)) { + target[key] = source[key]; + } + } } - for (var i = 0; i < array1.length; i++) { - var equals = equaler ? equaler(array1[i], array2[i]) : array1[i] === array2[i]; - if (!equals) { - return false; + server.mergeMaps = mergeMaps; + function removeItemFromSet(items, itemToRemove) { + if (items.length === 0) { + return; + } + var index = items.indexOf(itemToRemove); + if (index < 0) { + return; + } + if (index === items.length - 1) { + items.pop(); + } + else { + items[index] = items.pop(); } } - return true; - } - ts.arrayIsEqualTo = arrayIsEqualTo; - function hasResolvedModule(sourceFile, moduleNameText) { - return !!(sourceFile && sourceFile.resolvedModules && sourceFile.resolvedModules[moduleNameText]); - } - ts.hasResolvedModule = hasResolvedModule; - function getResolvedModule(sourceFile, moduleNameText) { - return hasResolvedModule(sourceFile, moduleNameText) ? sourceFile.resolvedModules[moduleNameText] : undefined; - } - ts.getResolvedModule = getResolvedModule; - function setResolvedModule(sourceFile, moduleNameText, resolvedModule) { - if (!sourceFile.resolvedModules) { - sourceFile.resolvedModules = ts.createMap(); + server.removeItemFromSet = removeItemFromSet; + function toNormalizedPath(fileName) { + return ts.normalizePath(fileName); } - sourceFile.resolvedModules[moduleNameText] = resolvedModule; - } - ts.setResolvedModule = setResolvedModule; - function setResolvedTypeReferenceDirective(sourceFile, typeReferenceDirectiveName, resolvedTypeReferenceDirective) { - if (!sourceFile.resolvedTypeReferenceDirectiveNames) { - sourceFile.resolvedTypeReferenceDirectiveNames = ts.createMap(); + server.toNormalizedPath = toNormalizedPath; + function normalizedPathToPath(normalizedPath, currentDirectory, getCanonicalFileName) { + var f = ts.isRootedDiskPath(normalizedPath) ? normalizedPath : ts.getNormalizedAbsolutePath(normalizedPath, currentDirectory); + return getCanonicalFileName(f); } - sourceFile.resolvedTypeReferenceDirectiveNames[typeReferenceDirectiveName] = resolvedTypeReferenceDirective; + server.normalizedPathToPath = normalizedPathToPath; + function asNormalizedPath(fileName) { + return fileName; + } + server.asNormalizedPath = asNormalizedPath; + function createNormalizedPathMap() { + var map = Object.create(null); + return { + get: function (path) { + return map[path]; + }, + set: function (path, value) { + map[path] = value; + }, + contains: function (path) { + return ts.hasProperty(map, path); + }, + remove: function (path) { + delete map[path]; + } + }; + } + server.createNormalizedPathMap = createNormalizedPathMap; + function throwLanguageServiceIsDisabledError() { + throw new Error("LanguageService is disabled"); + } + server.nullLanguageService = { + cleanupSemanticCache: throwLanguageServiceIsDisabledError, + getSyntacticDiagnostics: throwLanguageServiceIsDisabledError, + getSemanticDiagnostics: throwLanguageServiceIsDisabledError, + getCompilerOptionsDiagnostics: throwLanguageServiceIsDisabledError, + getSyntacticClassifications: throwLanguageServiceIsDisabledError, + getEncodedSyntacticClassifications: throwLanguageServiceIsDisabledError, + getSemanticClassifications: throwLanguageServiceIsDisabledError, + getEncodedSemanticClassifications: throwLanguageServiceIsDisabledError, + getCompletionsAtPosition: throwLanguageServiceIsDisabledError, + findReferences: throwLanguageServiceIsDisabledError, + getCompletionEntryDetails: throwLanguageServiceIsDisabledError, + getQuickInfoAtPosition: throwLanguageServiceIsDisabledError, + findRenameLocations: throwLanguageServiceIsDisabledError, + getNameOrDottedNameSpan: throwLanguageServiceIsDisabledError, + getBreakpointStatementAtPosition: throwLanguageServiceIsDisabledError, + getBraceMatchingAtPosition: throwLanguageServiceIsDisabledError, + getSignatureHelpItems: throwLanguageServiceIsDisabledError, + getDefinitionAtPosition: throwLanguageServiceIsDisabledError, + getRenameInfo: throwLanguageServiceIsDisabledError, + getTypeDefinitionAtPosition: throwLanguageServiceIsDisabledError, + getReferencesAtPosition: throwLanguageServiceIsDisabledError, + getDocumentHighlights: throwLanguageServiceIsDisabledError, + getOccurrencesAtPosition: throwLanguageServiceIsDisabledError, + getNavigateToItems: throwLanguageServiceIsDisabledError, + getNavigationBarItems: throwLanguageServiceIsDisabledError, + getNavigationTree: throwLanguageServiceIsDisabledError, + getOutliningSpans: throwLanguageServiceIsDisabledError, + getTodoComments: throwLanguageServiceIsDisabledError, + getIndentationAtPosition: throwLanguageServiceIsDisabledError, + getFormattingEditsForRange: throwLanguageServiceIsDisabledError, + getFormattingEditsForDocument: throwLanguageServiceIsDisabledError, + getFormattingEditsAfterKeystroke: throwLanguageServiceIsDisabledError, + getDocCommentTemplateAtPosition: throwLanguageServiceIsDisabledError, + isValidBraceCompletionAtPosition: throwLanguageServiceIsDisabledError, + getEmitOutput: throwLanguageServiceIsDisabledError, + getProgram: throwLanguageServiceIsDisabledError, + getNonBoundSourceFile: throwLanguageServiceIsDisabledError, + dispose: throwLanguageServiceIsDisabledError, + getCompletionEntrySymbol: throwLanguageServiceIsDisabledError, + getImplementationAtPosition: throwLanguageServiceIsDisabledError, + getSourceFile: throwLanguageServiceIsDisabledError, + getCodeFixesAtPosition: throwLanguageServiceIsDisabledError + }; + server.nullLanguageServiceHost = { + setCompilationSettings: function () { return undefined; }, + notifyFileRemoved: function () { return undefined; } + }; + function isInferredProjectName(name) { + return /dev\/null\/inferredProject\d+\*/.test(name); + } + server.isInferredProjectName = isInferredProjectName; + function makeInferredProjectName(counter) { + return "/dev/null/inferredProject" + counter + "*"; + } + server.makeInferredProjectName = makeInferredProjectName; + var ThrottledOperations = (function () { + function ThrottledOperations(host) { + this.host = host; + this.pendingTimeouts = ts.createMap(); + } + ThrottledOperations.prototype.schedule = function (operationId, delay, cb) { + if (ts.hasProperty(this.pendingTimeouts, operationId)) { + this.host.clearTimeout(this.pendingTimeouts[operationId]); + } + this.pendingTimeouts[operationId] = this.host.setTimeout(ThrottledOperations.run, delay, this, operationId, cb); + }; + ThrottledOperations.run = function (self, operationId, cb) { + delete self.pendingTimeouts[operationId]; + cb(); + }; + return ThrottledOperations; + }()); + server.ThrottledOperations = ThrottledOperations; + var GcTimer = (function () { + function GcTimer(host, delay, logger) { + this.host = host; + this.delay = delay; + this.logger = logger; + } + GcTimer.prototype.scheduleCollect = function () { + if (!this.host.gc || this.timerId != undefined) { + return; + } + this.timerId = this.host.setTimeout(GcTimer.run, this.delay, this); + }; + GcTimer.run = function (self) { + self.timerId = undefined; + var log = self.logger.hasLevel(LogLevel.requestTime); + var before = log && self.host.getMemoryUsage(); + self.host.gc(); + if (log) { + var after = self.host.getMemoryUsage(); + self.logger.perftrc("GC::before " + before + ", after " + after); + } + }; + return GcTimer; + }()); + server.GcTimer = GcTimer; + })(server = ts.server || (ts.server = {})); +})(ts || (ts = {})); +var ts; +(function (ts) { + function trace(host, message) { + host.trace(ts.formatMessage.apply(undefined, arguments)); } - ts.setResolvedTypeReferenceDirective = setResolvedTypeReferenceDirective; - function moduleResolutionIsEqualTo(oldResolution, newResolution) { - return oldResolution.resolvedFileName === newResolution.resolvedFileName && oldResolution.isExternalLibraryImport === newResolution.isExternalLibraryImport; + ts.trace = trace; + function isTraceEnabled(compilerOptions, host) { + return compilerOptions.traceResolution && host.trace !== undefined; } - ts.moduleResolutionIsEqualTo = moduleResolutionIsEqualTo; - function typeDirectiveIsEqualTo(oldResolution, newResolution) { - return oldResolution.resolvedFileName === newResolution.resolvedFileName && oldResolution.primary === newResolution.primary; + ts.isTraceEnabled = isTraceEnabled; + function createResolvedModule(resolvedFileName, isExternalLibraryImport, failedLookupLocations) { + return { resolvedModule: resolvedFileName ? { resolvedFileName: resolvedFileName, isExternalLibraryImport: isExternalLibraryImport } : undefined, failedLookupLocations: failedLookupLocations }; } - ts.typeDirectiveIsEqualTo = typeDirectiveIsEqualTo; - function hasChangesInResolutions(names, newResolutions, oldResolutions, comparer) { - if (names.length !== newResolutions.length) { - return false; - } - for (var i = 0; i < names.length; i++) { - var newResolution = newResolutions[i]; - var oldResolution = oldResolutions && oldResolutions[names[i]]; - var changed = oldResolution - ? !newResolution || !comparer(oldResolution, newResolution) - : newResolution; - if (changed) { - return true; + ts.createResolvedModule = createResolvedModule; + function moduleHasNonRelativeName(moduleName) { + return !(ts.isRootedDiskPath(moduleName) || ts.isExternalModuleNameRelative(moduleName)); + } + function tryReadTypesSection(packageJsonPath, baseDirectory, state) { + var jsonContent = readJson(packageJsonPath, state.host); + function tryReadFromField(fieldName) { + if (ts.hasProperty(jsonContent, fieldName)) { + var typesFile = jsonContent[fieldName]; + if (typeof typesFile === "string") { + var typesFilePath_1 = ts.normalizePath(ts.combinePaths(baseDirectory, typesFile)); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.package_json_has_0_field_1_that_references_2, fieldName, typesFile, typesFilePath_1); + } + return typesFilePath_1; + } + else { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Expected_type_of_0_field_in_package_json_to_be_string_got_1, fieldName, typeof typesFile); + } + } } } - return false; - } - ts.hasChangesInResolutions = hasChangesInResolutions; - function containsParseError(node) { - aggregateChildData(node); - return (node.flags & 2097152) !== 0; - } - ts.containsParseError = containsParseError; - function aggregateChildData(node) { - if (!(node.flags & 4194304)) { - var thisNodeOrAnySubNodesHasError = ((node.flags & 524288) !== 0) || - ts.forEachChild(node, containsParseError); - if (thisNodeOrAnySubNodesHasError) { - node.flags |= 2097152; + var typesFilePath = tryReadFromField("typings") || tryReadFromField("types"); + if (typesFilePath) { + return typesFilePath; + } + if (state.compilerOptions.allowJs && jsonContent.main && typeof jsonContent.main === "string") { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.No_types_specified_in_package_json_but_allowJs_is_set_so_returning_main_value_of_0, jsonContent.main); } - node.flags |= 4194304; + var mainFilePath = ts.normalizePath(ts.combinePaths(baseDirectory, jsonContent.main)); + return mainFilePath; } + return undefined; } - function getSourceFileOfNode(node) { - while (node && node.kind !== 256) { - node = node.parent; + function readJson(path, host) { + try { + var jsonText = host.readFile(path); + return jsonText ? JSON.parse(jsonText) : {}; + } + catch (e) { + return {}; } - return node; } - ts.getSourceFileOfNode = getSourceFileOfNode; - function isStatementWithLocals(node) { - switch (node.kind) { - case 199: - case 227: - case 206: - case 207: - case 208: - return true; + var typeReferenceExtensions = [".d.ts"]; + function getEffectiveTypeRoots(options, host) { + if (options.typeRoots) { + return options.typeRoots; } - return false; - } - ts.isStatementWithLocals = isStatementWithLocals; - function getStartPositionOfLine(line, sourceFile) { - ts.Debug.assert(line >= 0); - return ts.getLineStarts(sourceFile)[line]; - } - ts.getStartPositionOfLine = getStartPositionOfLine; - function nodePosToString(node) { - var file = getSourceFileOfNode(node); - var loc = ts.getLineAndCharacterOfPosition(file, node.pos); - return file.fileName + "(" + (loc.line + 1) + "," + (loc.character + 1) + ")"; - } - ts.nodePosToString = nodePosToString; - function getStartPosOfNode(node) { - return node.pos; + var currentDirectory; + if (options.configFilePath) { + currentDirectory = ts.getDirectoryPath(options.configFilePath); + } + else if (host.getCurrentDirectory) { + currentDirectory = host.getCurrentDirectory(); + } + return currentDirectory !== undefined && getDefaultTypeRoots(currentDirectory, host); } - ts.getStartPosOfNode = getStartPosOfNode; - function isDefined(value) { - return value !== undefined; + ts.getEffectiveTypeRoots = getEffectiveTypeRoots; + function getDefaultTypeRoots(currentDirectory, host) { + if (!host.directoryExists) { + return [ts.combinePaths(currentDirectory, nodeModulesAtTypes)]; + } + var typeRoots; + while (true) { + var atTypes = ts.combinePaths(currentDirectory, nodeModulesAtTypes); + if (host.directoryExists(atTypes)) { + (typeRoots || (typeRoots = [])).push(atTypes); + } + var parent_1 = ts.getDirectoryPath(currentDirectory); + if (parent_1 === currentDirectory) { + break; + } + currentDirectory = parent_1; + } + return typeRoots; } - ts.isDefined = isDefined; - function getEndLinePosition(line, sourceFile) { - ts.Debug.assert(line >= 0); - var lineStarts = ts.getLineStarts(sourceFile); - var lineIndex = line; - var sourceText = sourceFile.text; - if (lineIndex + 1 === lineStarts.length) { - return sourceText.length - 1; + var nodeModulesAtTypes = ts.combinePaths("node_modules", "@types"); + function resolveTypeReferenceDirective(typeReferenceDirectiveName, containingFile, options, host) { + var traceEnabled = isTraceEnabled(options, host); + var moduleResolutionState = { + compilerOptions: options, + host: host, + skipTsx: true, + traceEnabled: traceEnabled + }; + var typeRoots = getEffectiveTypeRoots(options, host); + if (traceEnabled) { + if (containingFile === undefined) { + if (typeRoots === undefined) { + trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_not_set_root_directory_not_set, typeReferenceDirectiveName); + } + else { + trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_not_set_root_directory_1, typeReferenceDirectiveName, typeRoots); + } + } + else { + if (typeRoots === undefined) { + trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_1_root_directory_not_set, typeReferenceDirectiveName, containingFile); + } + else { + trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_1_root_directory_2, typeReferenceDirectiveName, containingFile, typeRoots); + } + } + } + var failedLookupLocations = []; + if (typeRoots && typeRoots.length) { + if (traceEnabled) { + trace(host, ts.Diagnostics.Resolving_with_primary_search_path_0, typeRoots.join(", ")); + } + var primarySearchPaths = typeRoots; + for (var _i = 0, primarySearchPaths_1 = primarySearchPaths; _i < primarySearchPaths_1.length; _i++) { + var typeRoot = primarySearchPaths_1[_i]; + var candidate = ts.combinePaths(typeRoot, typeReferenceDirectiveName); + var candidateDirectory = ts.getDirectoryPath(candidate); + var resolvedFile_1 = loadNodeModuleFromDirectory(typeReferenceExtensions, candidate, failedLookupLocations, !directoryProbablyExists(candidateDirectory, host), moduleResolutionState); + if (resolvedFile_1) { + if (traceEnabled) { + trace(host, ts.Diagnostics.Type_reference_directive_0_was_successfully_resolved_to_1_primary_Colon_2, typeReferenceDirectiveName, resolvedFile_1, true); + } + return { + resolvedTypeReferenceDirective: { primary: true, resolvedFileName: resolvedFile_1 }, + failedLookupLocations: failedLookupLocations + }; + } + } } else { - var start = lineStarts[lineIndex]; - var pos = lineStarts[lineIndex + 1] - 1; - ts.Debug.assert(ts.isLineBreak(sourceText.charCodeAt(pos))); - while (start <= pos && ts.isLineBreak(sourceText.charCodeAt(pos))) { - pos--; + if (traceEnabled) { + trace(host, ts.Diagnostics.Root_directory_cannot_be_determined_skipping_primary_search_paths); } - return pos; } - } - ts.getEndLinePosition = getEndLinePosition; - function nodeIsMissing(node) { - if (node === undefined) { - return true; + var resolvedFile; + var initialLocationForSecondaryLookup; + if (containingFile) { + initialLocationForSecondaryLookup = ts.getDirectoryPath(containingFile); } - return node.pos === node.end && node.pos >= 0 && node.kind !== 1; + if (initialLocationForSecondaryLookup !== undefined) { + if (traceEnabled) { + trace(host, ts.Diagnostics.Looking_up_in_node_modules_folder_initial_location_0, initialLocationForSecondaryLookup); + } + resolvedFile = loadModuleFromNodeModules(typeReferenceDirectiveName, initialLocationForSecondaryLookup, failedLookupLocations, moduleResolutionState, false); + if (traceEnabled) { + if (resolvedFile) { + trace(host, ts.Diagnostics.Type_reference_directive_0_was_successfully_resolved_to_1_primary_Colon_2, typeReferenceDirectiveName, resolvedFile, false); + } + else { + trace(host, ts.Diagnostics.Type_reference_directive_0_was_not_resolved, typeReferenceDirectiveName); + } + } + } + else { + if (traceEnabled) { + trace(host, ts.Diagnostics.Containing_file_is_not_specified_and_root_directory_cannot_be_determined_skipping_lookup_in_node_modules_folder); + } + } + return { + resolvedTypeReferenceDirective: resolvedFile + ? { primary: false, resolvedFileName: resolvedFile } + : undefined, + failedLookupLocations: failedLookupLocations + }; } - ts.nodeIsMissing = nodeIsMissing; - function nodeIsPresent(node) { - return !nodeIsMissing(node); + ts.resolveTypeReferenceDirective = resolveTypeReferenceDirective; + function getAutomaticTypeDirectiveNames(options, host) { + if (options.types) { + return options.types; + } + var result = []; + if (host.directoryExists && host.getDirectories) { + var typeRoots = getEffectiveTypeRoots(options, host); + if (typeRoots) { + for (var _i = 0, typeRoots_1 = typeRoots; _i < typeRoots_1.length; _i++) { + var root = typeRoots_1[_i]; + if (host.directoryExists(root)) { + for (var _a = 0, _b = host.getDirectories(root); _a < _b.length; _a++) { + var typeDirectivePath = _b[_a]; + var normalized = ts.normalizePath(typeDirectivePath); + var packageJsonPath = pathToPackageJson(ts.combinePaths(root, normalized)); + var isNotNeededPackage = host.fileExists(packageJsonPath) && readJson(packageJsonPath, host).typings === null; + if (!isNotNeededPackage) { + result.push(ts.getBaseFileName(normalized)); + } + } + } + } + } + } + return result; } - ts.nodeIsPresent = nodeIsPresent; - function getTokenPosOfNode(node, sourceFile, includeJsDocComment) { - if (nodeIsMissing(node)) { - return node.pos; + ts.getAutomaticTypeDirectiveNames = getAutomaticTypeDirectiveNames; + function resolveModuleName(moduleName, containingFile, compilerOptions, host) { + var traceEnabled = isTraceEnabled(compilerOptions, host); + if (traceEnabled) { + trace(host, ts.Diagnostics.Resolving_module_0_from_1, moduleName, containingFile); } - if (isJSDocNode(node)) { - return ts.skipTrivia((sourceFile || getSourceFileOfNode(node)).text, node.pos, false, true); + var moduleResolution = compilerOptions.moduleResolution; + if (moduleResolution === undefined) { + moduleResolution = ts.getEmitModuleKind(compilerOptions) === ts.ModuleKind.CommonJS ? ts.ModuleResolutionKind.NodeJs : ts.ModuleResolutionKind.Classic; + if (traceEnabled) { + trace(host, ts.Diagnostics.Module_resolution_kind_is_not_specified_using_0, ts.ModuleResolutionKind[moduleResolution]); + } } - if (includeJsDocComment && node.jsDocComments && node.jsDocComments.length > 0) { - return getTokenPosOfNode(node.jsDocComments[0]); + else { + if (traceEnabled) { + trace(host, ts.Diagnostics.Explicitly_specified_module_resolution_kind_Colon_0, ts.ModuleResolutionKind[moduleResolution]); + } } - if (node.kind === 286 && node._children.length > 0) { - return getTokenPosOfNode(node._children[0], sourceFile, includeJsDocComment); + var result; + switch (moduleResolution) { + case ts.ModuleResolutionKind.NodeJs: + result = nodeModuleNameResolver(moduleName, containingFile, compilerOptions, host); + break; + case ts.ModuleResolutionKind.Classic: + result = classicNameResolver(moduleName, containingFile, compilerOptions, host); + break; } - return ts.skipTrivia((sourceFile || getSourceFileOfNode(node)).text, node.pos); + if (traceEnabled) { + if (result.resolvedModule) { + trace(host, ts.Diagnostics.Module_name_0_was_successfully_resolved_to_1, moduleName, result.resolvedModule.resolvedFileName); + } + else { + trace(host, ts.Diagnostics.Module_name_0_was_not_resolved, moduleName); + } + } + return result; } - ts.getTokenPosOfNode = getTokenPosOfNode; - function isJSDocNode(node) { - return node.kind >= 257 && node.kind <= 282; + ts.resolveModuleName = resolveModuleName; + function tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state) { + if (moduleHasNonRelativeName(moduleName)) { + return tryLoadModuleUsingBaseUrl(moduleName, loader, failedLookupLocations, supportedExtensions, state); + } + else { + return tryLoadModuleUsingRootDirs(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state); + } } - ts.isJSDocNode = isJSDocNode; - function isJSDocTag(node) { - return node.kind >= 273 && node.kind <= 285; + function tryLoadModuleUsingRootDirs(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state) { + if (!state.compilerOptions.rootDirs) { + return undefined; + } + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.rootDirs_option_is_set_using_it_to_resolve_relative_module_name_0, moduleName); + } + var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); + var matchedRootDir; + var matchedNormalizedPrefix; + for (var _i = 0, _a = state.compilerOptions.rootDirs; _i < _a.length; _i++) { + var rootDir = _a[_i]; + var normalizedRoot = ts.normalizePath(rootDir); + if (!ts.endsWith(normalizedRoot, ts.directorySeparator)) { + normalizedRoot += ts.directorySeparator; + } + var isLongestMatchingPrefix = ts.startsWith(candidate, normalizedRoot) && + (matchedNormalizedPrefix === undefined || matchedNormalizedPrefix.length < normalizedRoot.length); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Checking_if_0_is_the_longest_matching_prefix_for_1_2, normalizedRoot, candidate, isLongestMatchingPrefix); + } + if (isLongestMatchingPrefix) { + matchedNormalizedPrefix = normalizedRoot; + matchedRootDir = rootDir; + } + } + if (matchedNormalizedPrefix) { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Longest_matching_prefix_for_0_is_1, candidate, matchedNormalizedPrefix); + } + var suffix = candidate.substr(matchedNormalizedPrefix.length); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Loading_0_from_the_root_dir_1_candidate_location_2, suffix, matchedNormalizedPrefix, candidate); + } + var resolvedFileName = loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(containingDirectory, state.host), state); + if (resolvedFileName) { + return resolvedFileName; + } + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Trying_other_entries_in_rootDirs); + } + for (var _b = 0, _c = state.compilerOptions.rootDirs; _b < _c.length; _b++) { + var rootDir = _c[_b]; + if (rootDir === matchedRootDir) { + continue; + } + var candidate_1 = ts.combinePaths(ts.normalizePath(rootDir), suffix); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Loading_0_from_the_root_dir_1_candidate_location_2, suffix, rootDir, candidate_1); + } + var baseDirectory = ts.getDirectoryPath(candidate_1); + var resolvedFileName_1 = loader(candidate_1, supportedExtensions, failedLookupLocations, !directoryProbablyExists(baseDirectory, state.host), state); + if (resolvedFileName_1) { + return resolvedFileName_1; + } + } + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Module_resolution_using_rootDirs_has_failed); + } + } + return undefined; } - ts.isJSDocTag = isJSDocTag; - function getNonDecoratorTokenPosOfNode(node, sourceFile) { - if (nodeIsMissing(node) || !node.decorators) { - return getTokenPosOfNode(node, sourceFile); + function tryLoadModuleUsingBaseUrl(moduleName, loader, failedLookupLocations, supportedExtensions, state) { + if (!state.compilerOptions.baseUrl) { + return undefined; + } + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.baseUrl_option_is_set_to_0_using_this_value_to_resolve_non_relative_module_name_1, state.compilerOptions.baseUrl, moduleName); + } + var matchedPattern = undefined; + if (state.compilerOptions.paths) { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.paths_option_is_specified_looking_for_a_pattern_to_match_module_name_0, moduleName); + } + matchedPattern = ts.matchPatternOrExact(ts.getOwnKeys(state.compilerOptions.paths), moduleName); + } + if (matchedPattern) { + var matchedStar = typeof matchedPattern === "string" ? undefined : ts.matchedText(matchedPattern, moduleName); + var matchedPatternText = typeof matchedPattern === "string" ? matchedPattern : ts.patternText(matchedPattern); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Module_name_0_matched_pattern_1, moduleName, matchedPatternText); + } + for (var _i = 0, _a = state.compilerOptions.paths[matchedPatternText]; _i < _a.length; _i++) { + var subst = _a[_i]; + var path = matchedStar ? subst.replace("*", matchedStar) : subst; + var candidate = ts.normalizePath(ts.combinePaths(state.compilerOptions.baseUrl, path)); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Trying_substitution_0_candidate_module_location_Colon_1, subst, path); + } + var resolvedFileName = loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(ts.getDirectoryPath(candidate), state.host), state); + if (resolvedFileName) { + return resolvedFileName; + } + } + return undefined; + } + else { + var candidate = ts.normalizePath(ts.combinePaths(state.compilerOptions.baseUrl, moduleName)); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Resolving_module_name_0_relative_to_base_url_1_2, moduleName, state.compilerOptions.baseUrl, candidate); + } + return loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(ts.getDirectoryPath(candidate), state.host), state); } - return ts.skipTrivia((sourceFile || getSourceFileOfNode(node)).text, node.decorators.end); } - ts.getNonDecoratorTokenPosOfNode = getNonDecoratorTokenPosOfNode; - function getSourceTextOfNodeFromSourceFile(sourceFile, node, includeTrivia) { - if (includeTrivia === void 0) { includeTrivia = false; } - if (nodeIsMissing(node)) { - return ""; + function nodeModuleNameResolver(moduleName, containingFile, compilerOptions, host) { + var containingDirectory = ts.getDirectoryPath(containingFile); + var supportedExtensions = ts.getSupportedExtensions(compilerOptions); + var traceEnabled = isTraceEnabled(compilerOptions, host); + var failedLookupLocations = []; + var state = { compilerOptions: compilerOptions, host: host, traceEnabled: traceEnabled, skipTsx: false }; + var resolvedFileName = tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, nodeLoadModuleByRelativeName, failedLookupLocations, supportedExtensions, state); + var isExternalLibraryImport = false; + if (!resolvedFileName) { + if (moduleHasNonRelativeName(moduleName)) { + if (traceEnabled) { + trace(host, ts.Diagnostics.Loading_module_0_from_node_modules_folder, moduleName); + } + resolvedFileName = loadModuleFromNodeModules(moduleName, containingDirectory, failedLookupLocations, state, false); + isExternalLibraryImport = resolvedFileName !== undefined; + } + else { + var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); + resolvedFileName = nodeLoadModuleByRelativeName(candidate, supportedExtensions, failedLookupLocations, false, state); + } } - var text = sourceFile.text; - return text.substring(includeTrivia ? node.pos : ts.skipTrivia(text, node.pos), node.end); + if (resolvedFileName && host.realpath) { + var originalFileName = resolvedFileName; + resolvedFileName = ts.normalizePath(host.realpath(resolvedFileName)); + if (traceEnabled) { + trace(host, ts.Diagnostics.Resolving_real_path_for_0_result_1, originalFileName, resolvedFileName); + } + } + return createResolvedModule(resolvedFileName, isExternalLibraryImport, failedLookupLocations); } - ts.getSourceTextOfNodeFromSourceFile = getSourceTextOfNodeFromSourceFile; - function getTextOfNodeFromSourceText(sourceText, node) { - if (nodeIsMissing(node)) { - return ""; + ts.nodeModuleNameResolver = nodeModuleNameResolver; + function nodeLoadModuleByRelativeName(candidate, supportedExtensions, failedLookupLocations, onlyRecordFailures, state) { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Loading_module_as_file_Slash_folder_candidate_module_location_0, candidate); } - return sourceText.substring(ts.skipTrivia(sourceText, node.pos), node.end); + var resolvedFileName = !ts.pathEndsWithDirectorySeparator(candidate) && loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, onlyRecordFailures, state); + return resolvedFileName || loadNodeModuleFromDirectory(supportedExtensions, candidate, failedLookupLocations, onlyRecordFailures, state); } - ts.getTextOfNodeFromSourceText = getTextOfNodeFromSourceText; - function getTextOfNode(node, includeTrivia) { - if (includeTrivia === void 0) { includeTrivia = false; } - return getSourceTextOfNodeFromSourceFile(getSourceFileOfNode(node), node, includeTrivia); + function directoryProbablyExists(directoryName, host) { + return !host.directoryExists || host.directoryExists(directoryName); } - ts.getTextOfNode = getTextOfNode; - function getLiteralText(node, sourceFile, languageVersion) { - if (languageVersion < 2 && (isTemplateLiteralKind(node.kind) || node.hasExtendedUnicodeEscape)) { - return getQuotedEscapedLiteralText('"', node.text, '"'); + ts.directoryProbablyExists = directoryProbablyExists; + function loadModuleFromFile(candidate, extensions, failedLookupLocation, onlyRecordFailures, state) { + var resolvedByAddingExtension = tryAddingExtensions(candidate, extensions, failedLookupLocation, onlyRecordFailures, state); + if (resolvedByAddingExtension) { + return resolvedByAddingExtension; } - if (!nodeIsSynthesized(node) && node.parent) { - var text = getSourceTextOfNodeFromSourceFile(sourceFile, node); - if (languageVersion < 2 && isBinaryOrOctalIntegerLiteral(node, text)) { - return node.text; + if (ts.hasJavaScriptFileExtension(candidate)) { + var extensionless = ts.removeFileExtension(candidate); + if (state.traceEnabled) { + var extension = candidate.substring(extensionless.length); + trace(state.host, ts.Diagnostics.File_name_0_has_a_1_extension_stripping_it, candidate, extension); } - return text; + return tryAddingExtensions(extensionless, extensions, failedLookupLocation, onlyRecordFailures, state); } - switch (node.kind) { - case 9: - return getQuotedEscapedLiteralText('"', node.text, '"'); - case 11: - return getQuotedEscapedLiteralText("`", node.text, "`"); - case 12: - return getQuotedEscapedLiteralText("`", node.text, "${"); - case 13: - return getQuotedEscapedLiteralText("}", node.text, "${"); - case 14: - return getQuotedEscapedLiteralText("}", node.text, "`"); - case 8: - return node.text; + } + function tryAddingExtensions(candidate, extensions, failedLookupLocation, onlyRecordFailures, state) { + if (!onlyRecordFailures) { + var directory = ts.getDirectoryPath(candidate); + if (directory) { + onlyRecordFailures = !directoryProbablyExists(directory, state.host); + } } - ts.Debug.fail("Literal kind '" + node.kind + "' not accounted for."); + return ts.forEach(extensions, function (ext) { + return !(state.skipTsx && ts.isJsxOrTsxExtension(ext)) && tryFile(candidate + ext, failedLookupLocation, onlyRecordFailures, state); + }); } - ts.getLiteralText = getLiteralText; - function isBinaryOrOctalIntegerLiteral(node, text) { - if (node.kind === 8 && text.length > 1) { - switch (text.charCodeAt(1)) { - case 98: - case 66: - case 111: - case 79: - return true; + function tryFile(fileName, failedLookupLocation, onlyRecordFailures, state) { + if (!onlyRecordFailures && state.host.fileExists(fileName)) { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.File_0_exist_use_it_as_a_name_resolution_result, fileName); } + return fileName; + } + else { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.File_0_does_not_exist, fileName); + } + failedLookupLocation.push(fileName); + return undefined; } - return false; } - ts.isBinaryOrOctalIntegerLiteral = isBinaryOrOctalIntegerLiteral; - function getQuotedEscapedLiteralText(leftQuote, text, rightQuote) { - return leftQuote + escapeNonAsciiCharacters(escapeString(text)) + rightQuote; + function loadNodeModuleFromDirectory(extensions, candidate, failedLookupLocation, onlyRecordFailures, state) { + var packageJsonPath = pathToPackageJson(candidate); + var directoryExists = !onlyRecordFailures && directoryProbablyExists(candidate, state.host); + if (directoryExists && state.host.fileExists(packageJsonPath)) { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Found_package_json_at_0, packageJsonPath); + } + var typesFile = tryReadTypesSection(packageJsonPath, candidate, state); + if (typesFile) { + var onlyRecordFailures_1 = !directoryProbablyExists(ts.getDirectoryPath(typesFile), state.host); + var result = tryFile(typesFile, failedLookupLocation, onlyRecordFailures_1, state) || + tryAddingExtensions(typesFile, extensions, failedLookupLocation, onlyRecordFailures_1, state); + if (result) { + return result; + } + } + else { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.package_json_does_not_have_types_field); + } + } + } + else { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.File_0_does_not_exist, packageJsonPath); + } + failedLookupLocation.push(packageJsonPath); + } + return loadModuleFromFile(ts.combinePaths(candidate, "index"), extensions, failedLookupLocation, !directoryExists, state); } - function escapeIdentifier(identifier) { - return identifier.length >= 2 && identifier.charCodeAt(0) === 95 && identifier.charCodeAt(1) === 95 ? "_" + identifier : identifier; + function pathToPackageJson(directory) { + return ts.combinePaths(directory, "package.json"); } - ts.escapeIdentifier = escapeIdentifier; - function unescapeIdentifier(identifier) { - return identifier.length >= 3 && identifier.charCodeAt(0) === 95 && identifier.charCodeAt(1) === 95 && identifier.charCodeAt(2) === 95 ? identifier.substr(1) : identifier; + function loadModuleFromNodeModulesFolder(moduleName, directory, failedLookupLocations, state) { + var nodeModulesFolder = ts.combinePaths(directory, "node_modules"); + var nodeModulesFolderExists = directoryProbablyExists(nodeModulesFolder, state.host); + var candidate = ts.normalizePath(ts.combinePaths(nodeModulesFolder, moduleName)); + var supportedExtensions = ts.getSupportedExtensions(state.compilerOptions); + var result = loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, !nodeModulesFolderExists, state); + if (result) { + return result; + } + result = loadNodeModuleFromDirectory(supportedExtensions, candidate, failedLookupLocations, !nodeModulesFolderExists, state); + if (result) { + return result; + } } - ts.unescapeIdentifier = unescapeIdentifier; - function makeIdentifierFromModuleName(moduleName) { - return ts.getBaseFileName(moduleName).replace(/^(\d)/, "_$1").replace(/\W/g, "_"); + function loadModuleFromNodeModules(moduleName, directory, failedLookupLocations, state, checkOneLevel) { + return loadModuleFromNodeModulesWorker(moduleName, directory, failedLookupLocations, state, checkOneLevel, false); } - ts.makeIdentifierFromModuleName = makeIdentifierFromModuleName; - function isBlockOrCatchScoped(declaration) { - return (ts.getCombinedNodeFlags(declaration) & 3) !== 0 || + ts.loadModuleFromNodeModules = loadModuleFromNodeModules; + function loadModuleFromNodeModulesAtTypes(moduleName, directory, failedLookupLocations, state) { + return loadModuleFromNodeModulesWorker(moduleName, directory, failedLookupLocations, state, false, true); + } + function loadModuleFromNodeModulesWorker(moduleName, directory, failedLookupLocations, state, checkOneLevel, typesOnly) { + directory = ts.normalizeSlashes(directory); + while (true) { + var baseName = ts.getBaseFileName(directory); + if (baseName !== "node_modules") { + var packageResult = void 0; + if (!typesOnly) { + packageResult = loadModuleFromNodeModulesFolder(moduleName, directory, failedLookupLocations, state); + if (packageResult && ts.hasTypeScriptFileExtension(packageResult)) { + return packageResult; + } + } + var typesResult = loadModuleFromNodeModulesFolder(ts.combinePaths("@types", moduleName), directory, failedLookupLocations, state); + if (typesResult || packageResult) { + return typesResult || packageResult; + } + } + var parentPath = ts.getDirectoryPath(directory); + if (parentPath === directory || checkOneLevel) { + break; + } + directory = parentPath; + } + return undefined; + } + function classicNameResolver(moduleName, containingFile, compilerOptions, host) { + var traceEnabled = isTraceEnabled(compilerOptions, host); + var state = { compilerOptions: compilerOptions, host: host, traceEnabled: traceEnabled, skipTsx: !compilerOptions.jsx }; + var failedLookupLocations = []; + var supportedExtensions = ts.getSupportedExtensions(compilerOptions); + var containingDirectory = ts.getDirectoryPath(containingFile); + var resolvedFileName = tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, loadModuleFromFile, failedLookupLocations, supportedExtensions, state); + if (resolvedFileName) { + return createResolvedModule(resolvedFileName, false, failedLookupLocations); + } + var referencedSourceFile; + if (moduleHasNonRelativeName(moduleName)) { + referencedSourceFile = referencedSourceFile = loadModuleFromAncestorDirectories(moduleName, containingDirectory, supportedExtensions, failedLookupLocations, state) || + loadModuleFromNodeModulesAtTypes(moduleName, containingDirectory, failedLookupLocations, state); + } + else { + var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); + referencedSourceFile = loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, false, state); + } + return referencedSourceFile + ? { resolvedModule: { resolvedFileName: referencedSourceFile }, failedLookupLocations: failedLookupLocations } + : { resolvedModule: undefined, failedLookupLocations: failedLookupLocations }; + } + ts.classicNameResolver = classicNameResolver; + function loadModuleFromAncestorDirectories(moduleName, containingDirectory, supportedExtensions, failedLookupLocations, state) { + while (true) { + var searchName = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); + var referencedSourceFile = loadModuleFromFile(searchName, supportedExtensions, failedLookupLocations, false, state); + if (referencedSourceFile) { + return referencedSourceFile; + } + var parentPath = ts.getDirectoryPath(containingDirectory); + if (parentPath === containingDirectory) { + return undefined; + } + containingDirectory = parentPath; + } + } +})(ts || (ts = {})); +var ts; +(function (ts) { + ts.externalHelpersModuleNameText = "tslib"; + function getDeclarationOfKind(symbol, kind) { + var declarations = symbol.declarations; + if (declarations) { + for (var _i = 0, declarations_1 = declarations; _i < declarations_1.length; _i++) { + var declaration = declarations_1[_i]; + if (declaration.kind === kind) { + return declaration; + } + } + } + return undefined; + } + ts.getDeclarationOfKind = getDeclarationOfKind; + var stringWriters = []; + function getSingleLineStringWriter() { + if (stringWriters.length === 0) { + var str_1 = ""; + var writeText = function (text) { return str_1 += text; }; + return { + string: function () { return str_1; }, + writeKeyword: writeText, + writeOperator: writeText, + writePunctuation: writeText, + writeSpace: writeText, + writeStringLiteral: writeText, + writeParameter: writeText, + writeSymbol: writeText, + writeLine: function () { return str_1 += " "; }, + increaseIndent: function () { }, + decreaseIndent: function () { }, + clear: function () { return str_1 = ""; }, + trackSymbol: function () { }, + reportInaccessibleThisError: function () { } + }; + } + return stringWriters.pop(); + } + ts.getSingleLineStringWriter = getSingleLineStringWriter; + function releaseStringWriter(writer) { + writer.clear(); + stringWriters.push(writer); + } + ts.releaseStringWriter = releaseStringWriter; + function getFullWidth(node) { + return node.end - node.pos; + } + ts.getFullWidth = getFullWidth; + function arrayIsEqualTo(array1, array2, equaler) { + if (!array1 || !array2) { + return array1 === array2; + } + if (array1.length !== array2.length) { + return false; + } + for (var i = 0; i < array1.length; i++) { + var equals = equaler ? equaler(array1[i], array2[i]) : array1[i] === array2[i]; + if (!equals) { + return false; + } + } + return true; + } + ts.arrayIsEqualTo = arrayIsEqualTo; + function hasResolvedModule(sourceFile, moduleNameText) { + return !!(sourceFile && sourceFile.resolvedModules && sourceFile.resolvedModules[moduleNameText]); + } + ts.hasResolvedModule = hasResolvedModule; + function getResolvedModule(sourceFile, moduleNameText) { + return hasResolvedModule(sourceFile, moduleNameText) ? sourceFile.resolvedModules[moduleNameText] : undefined; + } + ts.getResolvedModule = getResolvedModule; + function setResolvedModule(sourceFile, moduleNameText, resolvedModule) { + if (!sourceFile.resolvedModules) { + sourceFile.resolvedModules = ts.createMap(); + } + sourceFile.resolvedModules[moduleNameText] = resolvedModule; + } + ts.setResolvedModule = setResolvedModule; + function setResolvedTypeReferenceDirective(sourceFile, typeReferenceDirectiveName, resolvedTypeReferenceDirective) { + if (!sourceFile.resolvedTypeReferenceDirectiveNames) { + sourceFile.resolvedTypeReferenceDirectiveNames = ts.createMap(); + } + sourceFile.resolvedTypeReferenceDirectiveNames[typeReferenceDirectiveName] = resolvedTypeReferenceDirective; + } + ts.setResolvedTypeReferenceDirective = setResolvedTypeReferenceDirective; + function moduleResolutionIsEqualTo(oldResolution, newResolution) { + return oldResolution.resolvedFileName === newResolution.resolvedFileName && oldResolution.isExternalLibraryImport === newResolution.isExternalLibraryImport; + } + ts.moduleResolutionIsEqualTo = moduleResolutionIsEqualTo; + function typeDirectiveIsEqualTo(oldResolution, newResolution) { + return oldResolution.resolvedFileName === newResolution.resolvedFileName && oldResolution.primary === newResolution.primary; + } + ts.typeDirectiveIsEqualTo = typeDirectiveIsEqualTo; + function hasChangesInResolutions(names, newResolutions, oldResolutions, comparer) { + if (names.length !== newResolutions.length) { + return false; + } + for (var i = 0; i < names.length; i++) { + var newResolution = newResolutions[i]; + var oldResolution = oldResolutions && oldResolutions[names[i]]; + var changed = oldResolution + ? !newResolution || !comparer(oldResolution, newResolution) + : newResolution; + if (changed) { + return true; + } + } + return false; + } + ts.hasChangesInResolutions = hasChangesInResolutions; + function containsParseError(node) { + aggregateChildData(node); + return (node.flags & 2097152) !== 0; + } + ts.containsParseError = containsParseError; + function aggregateChildData(node) { + if (!(node.flags & 4194304)) { + var thisNodeOrAnySubNodesHasError = ((node.flags & 524288) !== 0) || + ts.forEachChild(node, containsParseError); + if (thisNodeOrAnySubNodesHasError) { + node.flags |= 2097152; + } + node.flags |= 4194304; + } + } + function getSourceFileOfNode(node) { + while (node && node.kind !== 256) { + node = node.parent; + } + return node; + } + ts.getSourceFileOfNode = getSourceFileOfNode; + function isStatementWithLocals(node) { + switch (node.kind) { + case 199: + case 227: + case 206: + case 207: + case 208: + return true; + } + return false; + } + ts.isStatementWithLocals = isStatementWithLocals; + function getStartPositionOfLine(line, sourceFile) { + ts.Debug.assert(line >= 0); + return ts.getLineStarts(sourceFile)[line]; + } + ts.getStartPositionOfLine = getStartPositionOfLine; + function nodePosToString(node) { + var file = getSourceFileOfNode(node); + var loc = ts.getLineAndCharacterOfPosition(file, node.pos); + return file.fileName + "(" + (loc.line + 1) + "," + (loc.character + 1) + ")"; + } + ts.nodePosToString = nodePosToString; + function getStartPosOfNode(node) { + return node.pos; + } + ts.getStartPosOfNode = getStartPosOfNode; + function isDefined(value) { + return value !== undefined; + } + ts.isDefined = isDefined; + function getEndLinePosition(line, sourceFile) { + ts.Debug.assert(line >= 0); + var lineStarts = ts.getLineStarts(sourceFile); + var lineIndex = line; + var sourceText = sourceFile.text; + if (lineIndex + 1 === lineStarts.length) { + return sourceText.length - 1; + } + else { + var start = lineStarts[lineIndex]; + var pos = lineStarts[lineIndex + 1] - 1; + ts.Debug.assert(ts.isLineBreak(sourceText.charCodeAt(pos))); + while (start <= pos && ts.isLineBreak(sourceText.charCodeAt(pos))) { + pos--; + } + return pos; + } + } + ts.getEndLinePosition = getEndLinePosition; + function nodeIsMissing(node) { + if (node === undefined) { + return true; + } + return node.pos === node.end && node.pos >= 0 && node.kind !== 1; + } + ts.nodeIsMissing = nodeIsMissing; + function nodeIsPresent(node) { + return !nodeIsMissing(node); + } + ts.nodeIsPresent = nodeIsPresent; + function getTokenPosOfNode(node, sourceFile, includeJsDocComment) { + if (nodeIsMissing(node)) { + return node.pos; + } + if (isJSDocNode(node)) { + return ts.skipTrivia((sourceFile || getSourceFileOfNode(node)).text, node.pos, false, true); + } + if (includeJsDocComment && node.jsDocComments && node.jsDocComments.length > 0) { + return getTokenPosOfNode(node.jsDocComments[0]); + } + if (node.kind === 286 && node._children.length > 0) { + return getTokenPosOfNode(node._children[0], sourceFile, includeJsDocComment); + } + return ts.skipTrivia((sourceFile || getSourceFileOfNode(node)).text, node.pos); + } + ts.getTokenPosOfNode = getTokenPosOfNode; + function isJSDocNode(node) { + return node.kind >= 257 && node.kind <= 282; + } + ts.isJSDocNode = isJSDocNode; + function isJSDocTag(node) { + return node.kind >= 273 && node.kind <= 285; + } + ts.isJSDocTag = isJSDocTag; + function getNonDecoratorTokenPosOfNode(node, sourceFile) { + if (nodeIsMissing(node) || !node.decorators) { + return getTokenPosOfNode(node, sourceFile); + } + return ts.skipTrivia((sourceFile || getSourceFileOfNode(node)).text, node.decorators.end); + } + ts.getNonDecoratorTokenPosOfNode = getNonDecoratorTokenPosOfNode; + function getSourceTextOfNodeFromSourceFile(sourceFile, node, includeTrivia) { + if (includeTrivia === void 0) { includeTrivia = false; } + if (nodeIsMissing(node)) { + return ""; + } + var text = sourceFile.text; + return text.substring(includeTrivia ? node.pos : ts.skipTrivia(text, node.pos), node.end); + } + ts.getSourceTextOfNodeFromSourceFile = getSourceTextOfNodeFromSourceFile; + function getTextOfNodeFromSourceText(sourceText, node) { + if (nodeIsMissing(node)) { + return ""; + } + return sourceText.substring(ts.skipTrivia(sourceText, node.pos), node.end); + } + ts.getTextOfNodeFromSourceText = getTextOfNodeFromSourceText; + function getTextOfNode(node, includeTrivia) { + if (includeTrivia === void 0) { includeTrivia = false; } + return getSourceTextOfNodeFromSourceFile(getSourceFileOfNode(node), node, includeTrivia); + } + ts.getTextOfNode = getTextOfNode; + function getLiteralText(node, sourceFile, languageVersion) { + if (languageVersion < 2 && (isTemplateLiteralKind(node.kind) || node.hasExtendedUnicodeEscape)) { + return getQuotedEscapedLiteralText('"', node.text, '"'); + } + if (!nodeIsSynthesized(node) && node.parent) { + var text = getSourceTextOfNodeFromSourceFile(sourceFile, node); + if (languageVersion < 2 && isBinaryOrOctalIntegerLiteral(node, text)) { + return node.text; + } + return text; + } + switch (node.kind) { + case 9: + return getQuotedEscapedLiteralText('"', node.text, '"'); + case 11: + return getQuotedEscapedLiteralText("`", node.text, "`"); + case 12: + return getQuotedEscapedLiteralText("`", node.text, "${"); + case 13: + return getQuotedEscapedLiteralText("}", node.text, "${"); + case 14: + return getQuotedEscapedLiteralText("}", node.text, "`"); + case 8: + return node.text; + } + ts.Debug.fail("Literal kind '" + node.kind + "' not accounted for."); + } + ts.getLiteralText = getLiteralText; + function isBinaryOrOctalIntegerLiteral(node, text) { + if (node.kind === 8 && text.length > 1) { + switch (text.charCodeAt(1)) { + case 98: + case 66: + case 111: + case 79: + return true; + } + } + return false; + } + ts.isBinaryOrOctalIntegerLiteral = isBinaryOrOctalIntegerLiteral; + function getQuotedEscapedLiteralText(leftQuote, text, rightQuote) { + return leftQuote + escapeNonAsciiCharacters(escapeString(text)) + rightQuote; + } + function escapeIdentifier(identifier) { + return identifier.length >= 2 && identifier.charCodeAt(0) === 95 && identifier.charCodeAt(1) === 95 ? "_" + identifier : identifier; + } + ts.escapeIdentifier = escapeIdentifier; + function unescapeIdentifier(identifier) { + return identifier.length >= 3 && identifier.charCodeAt(0) === 95 && identifier.charCodeAt(1) === 95 && identifier.charCodeAt(2) === 95 ? identifier.substr(1) : identifier; + } + ts.unescapeIdentifier = unescapeIdentifier; + function makeIdentifierFromModuleName(moduleName) { + return ts.getBaseFileName(moduleName).replace(/^(\d)/, "_$1").replace(/\W/g, "_"); + } + ts.makeIdentifierFromModuleName = makeIdentifierFromModuleName; + function isBlockOrCatchScoped(declaration) { + return (ts.getCombinedNodeFlags(declaration) & 3) !== 0 || isCatchClauseVariableDeclaration(declaration); } ts.isBlockOrCatchScoped = isBlockOrCatchScoped; @@ -5877,10 +7014,10 @@ var ts; return !!(ts.getCombinedNodeFlags(node) & 1); } ts.isLet = isLet; - function isSuperCallExpression(n) { + function isSuperCall(n) { return n.kind === 174 && n.expression.kind === 95; } - ts.isSuperCallExpression = isSuperCallExpression; + ts.isSuperCall = isSuperCall; function isPrologueDirective(node) { return node.kind === 202 && node.expression.kind === 9; } @@ -5943,23 +7080,23 @@ var ts; case 139: case 172: case 97: - var parent_1 = node.parent; - if (parent_1.kind === 158) { + var parent_2 = node.parent; + if (parent_2.kind === 158) { return false; } - if (154 <= parent_1.kind && parent_1.kind <= 166) { + if (154 <= parent_2.kind && parent_2.kind <= 166) { return true; } - switch (parent_1.kind) { + switch (parent_2.kind) { case 194: - return !isExpressionWithTypeArgumentsInClassExtendsClause(parent_1); + return !isExpressionWithTypeArgumentsInClassExtendsClause(parent_2); case 141: - return node === parent_1.constraint; + return node === parent_2.constraint; case 145: case 144: case 142: case 218: - return node === parent_1.type; + return node === parent_2.type; case 220: case 179: case 180: @@ -5968,16 +7105,16 @@ var ts; case 146: case 149: case 150: - return node === parent_1.type; + return node === parent_2.type; case 151: case 152: case 153: - return node === parent_1.type; + return node === parent_2.type; case 177: - return node === parent_1.type; + return node === parent_2.type; case 174: case 175: - return parent_1.typeArguments && ts.indexOf(parent_1.typeArguments, node) >= 0; + return parent_2.typeArguments && ts.indexOf(parent_2.typeArguments, node) >= 0; case 176: return false; } @@ -6030,9 +7167,9 @@ var ts; return; default: if (isFunctionLike(node)) { - var name_7 = node.name; - if (name_7 && name_7.kind === 140) { - traverse(name_7.expression); + var name_8 = node.name; + if (name_8 && name_8.kind === 140) { + traverse(name_8.expression); return; } } @@ -6128,6 +7265,12 @@ var ts; return node && node.kind === 147 && node.parent.kind === 171; } ts.isObjectLiteralMethod = isObjectLiteralMethod; + function isObjectLiteralOrClassExpressionMethod(node) { + return node.kind === 147 && + (node.parent.kind === 171 || + node.parent.kind === 192); + } + ts.isObjectLiteralOrClassExpressionMethod = isObjectLiteralOrClassExpressionMethod; function isIdentifierTypePredicate(predicate) { return predicate && predicate.kind === 1; } @@ -6238,13 +7381,13 @@ var ts; function getImmediatelyInvokedFunctionExpression(func) { if (func.kind === 179 || func.kind === 180) { var prev = func; - var parent_2 = func.parent; - while (parent_2.kind === 178) { - prev = parent_2; - parent_2 = parent_2.parent; + var parent_3 = func.parent; + while (parent_3.kind === 178) { + prev = parent_3; + parent_3 = parent_3.parent; } - if (parent_2.kind === 174 && parent_2.expression === prev) { - return parent_2; + if (parent_3.kind === 174 && parent_3.expression === prev) { + return parent_3; } } } @@ -6390,8 +7533,8 @@ var ts; case 8: case 9: case 97: - var parent_3 = node.parent; - switch (parent_3.kind) { + var parent_4 = node.parent; + switch (parent_4.kind) { case 218: case 142: case 145: @@ -6399,7 +7542,7 @@ var ts; case 255: case 253: case 169: - return parent_3.initializer === node; + return parent_4.initializer === node; case 202: case 203: case 204: @@ -6410,32 +7553,32 @@ var ts; case 249: case 215: case 213: - return parent_3.expression === node; + return parent_4.expression === node; case 206: - var forStatement = parent_3; + var forStatement = parent_4; return (forStatement.initializer === node && forStatement.initializer.kind !== 219) || forStatement.condition === node || forStatement.incrementor === node; case 207: case 208: - var forInStatement = parent_3; + var forInStatement = parent_4; return (forInStatement.initializer === node && forInStatement.initializer.kind !== 219) || forInStatement.expression === node; case 177: case 195: - return node === parent_3.expression; + return node === parent_4.expression; case 197: - return node === parent_3.expression; + return node === parent_4.expression; case 140: - return node === parent_3.expression; + return node === parent_4.expression; case 143: case 248: case 247: return true; case 194: - return parent_3.expression === node && isExpressionWithTypeArgumentsInClassExtendsClause(parent_3); + return parent_4.expression === node && isExpressionWithTypeArgumentsInClassExtendsClause(parent_4); default: - if (isPartOfExpression(parent_3)) { + if (isPartOfExpression(parent_4)) { return true; } } @@ -6443,10 +7586,6 @@ var ts; return false; } ts.isPartOfExpression = isPartOfExpression; - function isExternalModuleNameRelative(moduleName) { - return /^\.\.?($|[\\/])/.test(moduleName); - } - ts.isExternalModuleNameRelative = isExternalModuleNameRelative; function isInstantiatedModule(node, preserveConstEnums) { var moduleState = ts.getModuleInstanceState(node); return moduleState === 1 || @@ -6650,17 +7789,17 @@ var ts; node.parent && node.parent.kind === 225) { result = append(result, getJSDocs(node.parent, checkParentVariableStatement, getDocs, getTags)); } - var parent_4 = node.parent; - var isSourceOfAssignmentExpressionStatement = parent_4 && parent_4.parent && - parent_4.kind === 187 && - parent_4.operatorToken.kind === 56 && - parent_4.parent.kind === 202; + var parent_5 = node.parent; + var isSourceOfAssignmentExpressionStatement = parent_5 && parent_5.parent && + parent_5.kind === 187 && + parent_5.operatorToken.kind === 56 && + parent_5.parent.kind === 202; if (isSourceOfAssignmentExpressionStatement) { - result = append(result, getJSDocs(parent_4.parent, checkParentVariableStatement, getDocs, getTags)); + result = append(result, getJSDocs(parent_5.parent, checkParentVariableStatement, getDocs, getTags)); } - var isPropertyAssignmentExpression = parent_4 && parent_4.kind === 253; + var isPropertyAssignmentExpression = parent_5 && parent_5.kind === 253; if (isPropertyAssignmentExpression) { - result = append(result, getJSDocs(parent_4, checkParentVariableStatement, getDocs, getTags)); + result = append(result, getJSDocs(parent_5, checkParentVariableStatement, getDocs, getTags)); } if (node.kind === 142) { var paramTags = getJSDocParameterTag(node, checkParentVariableStatement); @@ -6693,8 +7832,8 @@ var ts; } } else if (param.name.kind === 69) { - var name_8 = param.name.text; - var paramTags = ts.filter(tags, function (tag) { return tag.kind === 275 && tag.parameterName.text === name_8; }); + var name_9 = param.name.text; + var paramTags = ts.filter(tags, function (tag) { return tag.kind === 275 && tag.parameterName.text === name_9; }); if (paramTags) { return paramTags; } @@ -6765,20 +7904,20 @@ var ts; node = node.parent; } while (true) { - var parent_5 = node.parent; - if (parent_5.kind === 170 || parent_5.kind === 191) { - node = parent_5; + var parent_6 = node.parent; + if (parent_6.kind === 170 || parent_6.kind === 191) { + node = parent_6; continue; } - if (parent_5.kind === 253 || parent_5.kind === 254) { - node = parent_5.parent; + if (parent_6.kind === 253 || parent_6.kind === 254) { + node = parent_6.parent; continue; } - return parent_5.kind === 187 && - isAssignmentOperator(parent_5.operatorToken.kind) && - parent_5.left === node || - (parent_5.kind === 207 || parent_5.kind === 208) && - parent_5.initializer === node; + return parent_6.kind === 187 && + isAssignmentOperator(parent_6.operatorToken.kind) && + parent_6.left === node || + (parent_6.kind === 207 || parent_6.kind === 208) && + parent_6.initializer === node; } } ts.isAssignmentTarget = isAssignmentTarget; @@ -7044,14 +8183,10 @@ var ts; } ts.nodeStartsNewLexicalEnvironment = nodeStartsNewLexicalEnvironment; function nodeIsSynthesized(node) { - return positionIsSynthesized(node.pos) - || positionIsSynthesized(node.end); + return ts.positionIsSynthesized(node.pos) + || ts.positionIsSynthesized(node.end); } ts.nodeIsSynthesized = nodeIsSynthesized; - function positionIsSynthesized(pos) { - return !(pos >= 0); - } - ts.positionIsSynthesized = positionIsSynthesized; function getOriginalNode(node) { if (node) { while (node.original !== undefined) { @@ -7487,28 +8622,16 @@ var ts; function getDeclarationEmitOutputFilePath(sourceFile, host) { var options = host.getCompilerOptions(); var outputDir = options.declarationDir || options.outDir; - if (options.declaration) { - var path = outputDir - ? getSourceFilePathInNewDir(sourceFile, host, outputDir) - : sourceFile.fileName; - return ts.removeFileExtension(path) + ".d.ts"; - } + var path = outputDir + ? getSourceFilePathInNewDir(sourceFile, host, outputDir) + : sourceFile.fileName; + return ts.removeFileExtension(path) + ".d.ts"; } ts.getDeclarationEmitOutputFilePath = getDeclarationEmitOutputFilePath; - function getEmitScriptTarget(compilerOptions) { - return compilerOptions.target || 0; - } - ts.getEmitScriptTarget = getEmitScriptTarget; - function getEmitModuleKind(compilerOptions) { - return typeof compilerOptions.module === "number" ? - compilerOptions.module : - getEmitScriptTarget(compilerOptions) === 2 ? ts.ModuleKind.ES6 : ts.ModuleKind.CommonJS; - } - ts.getEmitModuleKind = getEmitModuleKind; function getSourceFilesToEmit(host, targetSourceFile) { var options = host.getCompilerOptions(); if (options.outFile || options.out) { - var moduleKind = getEmitModuleKind(options); + var moduleKind = ts.getEmitModuleKind(options); var moduleEmitEnabled = moduleKind === ts.ModuleKind.AMD || moduleKind === ts.ModuleKind.System; var sourceFiles = host.getSourceFiles(); return ts.filter(sourceFiles, moduleEmitEnabled ? isNonDeclarationFile : isBundleEmitNonExternalModule); @@ -7525,7 +8648,7 @@ var ts; function isBundleEmitNonExternalModule(sourceFile) { return !isDeclarationFile(sourceFile) && !ts.isExternalModule(sourceFile); } - function forEachTransformedEmitFile(host, sourceFiles, action) { + function forEachTransformedEmitFile(host, sourceFiles, action, emitOnlyDtsFiles) { var options = host.getCompilerOptions(); if (options.outFile || options.out) { onBundledEmit(host, sourceFiles); @@ -7552,7 +8675,7 @@ var ts; } var jsFilePath = getOwnEmitOutputFilePath(sourceFile, host, extension); var sourceMapFilePath = getSourceMapFilePath(jsFilePath, options); - var declarationFilePath = !isSourceFileJavaScript(sourceFile) ? getDeclarationEmitOutputFilePath(sourceFile, host) : undefined; + var declarationFilePath = !isSourceFileJavaScript(sourceFile) && (options.declaration || emitOnlyDtsFiles) ? getDeclarationEmitOutputFilePath(sourceFile, host) : undefined; action(jsFilePath, sourceMapFilePath, declarationFilePath, [sourceFile], false); } function onBundledEmit(host, sourceFiles) { @@ -7568,7 +8691,7 @@ var ts; function getSourceMapFilePath(jsFilePath, options) { return options.sourceMap ? jsFilePath + ".map" : undefined; } - function forEachExpectedEmitFile(host, action, targetSourceFile) { + function forEachExpectedEmitFile(host, action, targetSourceFile, emitOnlyDtsFiles) { var options = host.getCompilerOptions(); if (options.outFile || options.out) { onBundledEmit(host); @@ -7595,18 +8718,19 @@ var ts; } } var jsFilePath = getOwnEmitOutputFilePath(sourceFile, host, extension); + var declarationFilePath = !isSourceFileJavaScript(sourceFile) && (emitOnlyDtsFiles || options.declaration) ? getDeclarationEmitOutputFilePath(sourceFile, host) : undefined; var emitFileNames = { jsFilePath: jsFilePath, sourceMapFilePath: getSourceMapFilePath(jsFilePath, options), - declarationFilePath: !isSourceFileJavaScript(sourceFile) ? getDeclarationEmitOutputFilePath(sourceFile, host) : undefined + declarationFilePath: declarationFilePath }; - action(emitFileNames, [sourceFile], false); + action(emitFileNames, [sourceFile], false, emitOnlyDtsFiles); } function onBundledEmit(host) { var bundledSources = ts.filter(host.getSourceFiles(), function (sourceFile) { return !isDeclarationFile(sourceFile) && !host.isSourceFileFromExternalLibrary(sourceFile) && (!ts.isExternalModule(sourceFile) || - !!getEmitModuleKind(options)); }); + !!ts.getEmitModuleKind(options)); }); if (bundledSources.length) { var jsFilePath = options.outFile || options.out; var emitFileNames = { @@ -7614,7 +8738,7 @@ var ts; sourceMapFilePath: getSourceMapFilePath(jsFilePath, options), declarationFilePath: options.declaration ? ts.removeFileExtension(jsFilePath) + ".d.ts" : undefined }; - action(emitFileNames, bundledSources, true); + action(emitFileNames, bundledSources, true, emitOnlyDtsFiles); } } } @@ -7651,13 +8775,32 @@ var ts; ts.getFirstConstructorWithBody = getFirstConstructorWithBody; function getSetAccessorTypeAnnotationNode(accessor) { if (accessor && accessor.parameters.length > 0) { - var hasThis = accessor.parameters.length === 2 && - accessor.parameters[0].name.kind === 69 && - accessor.parameters[0].name.originalKeywordKind === 97; + var hasThis = accessor.parameters.length === 2 && parameterIsThisKeyword(accessor.parameters[0]); return accessor.parameters[hasThis ? 1 : 0].type; } } ts.getSetAccessorTypeAnnotationNode = getSetAccessorTypeAnnotationNode; + function getThisParameter(signature) { + if (signature.parameters.length) { + var thisParameter = signature.parameters[0]; + if (parameterIsThisKeyword(thisParameter)) { + return thisParameter; + } + } + } + ts.getThisParameter = getThisParameter; + function parameterIsThisKeyword(parameter) { + return isThisIdentifier(parameter.name); + } + ts.parameterIsThisKeyword = parameterIsThisKeyword; + function isThisIdentifier(node) { + return node && node.kind === 69 && identifierIsThisKeyword(node); + } + ts.isThisIdentifier = isThisIdentifier; + function identifierIsThisKeyword(id) { + return id.originalKeywordKind === 97; + } + ts.identifierIsThisKeyword = identifierIsThisKeyword; function getAllAccessorDeclarations(declarations, accessor) { var firstAccessor; var secondAccessor; @@ -7971,14 +9114,6 @@ var ts; return symbol && symbol.valueDeclaration && hasModifier(symbol.valueDeclaration, 512) ? symbol.valueDeclaration.localSymbol : undefined; } ts.getLocalSymbolForExportDefault = getLocalSymbolForExportDefault; - function hasJavaScriptFileExtension(fileName) { - return ts.forEach(ts.supportedJavascriptExtensions, function (extension) { return ts.fileExtensionIs(fileName, extension); }); - } - ts.hasJavaScriptFileExtension = hasJavaScriptFileExtension; - function hasTypeScriptFileExtension(fileName) { - return ts.forEach(ts.supportedTypeScriptExtensions, function (extension) { return ts.fileExtensionIs(fileName, extension); }); - } - ts.hasTypeScriptFileExtension = hasTypeScriptFileExtension; function tryExtractTypeScriptExtension(fileName) { return ts.find(ts.supportedTypescriptExtensionsForExtractExtension, function (extension) { return ts.fileExtensionIs(fileName, extension); }); } @@ -8069,12 +9204,6 @@ var ts; return result; } ts.convertToBase64 = convertToBase64; - function convertToRelativePath(absoluteOrRelativePath, basePath, getCanonicalFileName) { - return !ts.isRootedDiskPath(absoluteOrRelativePath) - ? absoluteOrRelativePath - : ts.getRelativePathToDirectoryOrUrl(basePath, absoluteOrRelativePath, basePath, getCanonicalFileName, false); - } - ts.convertToRelativePath = convertToRelativePath; var carriageReturnLineFeed = "\r\n"; var lineFeed = "\n"; function getNewLineCharacter(options) { @@ -8163,9 +9292,9 @@ var ts; if (syntaxKindCache[kind]) { return syntaxKindCache[kind]; } - for (var name_9 in syntaxKindEnum) { - if (syntaxKindEnum[name_9] === kind) { - return syntaxKindCache[kind] = kind.toString() + " (" + name_9 + ")"; + for (var name_10 in syntaxKindEnum) { + if (syntaxKindEnum[name_10] === kind) { + return syntaxKindCache[kind] = kind.toString() + " (" + name_10 + ")"; } } } @@ -8175,7 +9304,7 @@ var ts; } ts.formatSyntaxKind = formatSyntaxKind; function movePos(pos, value) { - return positionIsSynthesized(pos) ? -1 : pos + value; + return ts.positionIsSynthesized(pos) ? -1 : pos + value; } ts.movePos = movePos; function createRange(pos, end) { @@ -8244,7 +9373,7 @@ var ts; } ts.positionsAreOnSameLine = positionsAreOnSameLine; function getStartPositionOfRange(range, sourceFile) { - return positionIsSynthesized(range.pos) ? -1 : ts.skipTrivia(sourceFile.text, range.pos); + return ts.positionIsSynthesized(range.pos) ? -1 : ts.skipTrivia(sourceFile.text, range.pos); } ts.getStartPositionOfRange = getStartPositionOfRange; function collectExternalModuleInfo(sourceFile, resolver) { @@ -8281,8 +9410,8 @@ var ts; else { for (var _b = 0, _c = node.exportClause.elements; _b < _c.length; _b++) { var specifier = _c[_b]; - var name_10 = (specifier.propertyName || specifier.name).text; - (exportSpecifiers[name_10] || (exportSpecifiers[name_10] = [])).push(specifier); + var name_11 = (specifier.propertyName || specifier.name).text; + (exportSpecifiers[name_11] || (exportSpecifiers[name_11] = [])).push(specifier); } } break; @@ -8344,15 +9473,16 @@ var ts; return 11 <= kind && kind <= 14; } ts.isTemplateLiteralKind = isTemplateLiteralKind; - function isTemplateLiteralFragmentKind(kind) { - return kind === 12 - || kind === 13 - || kind === 14; + function isTemplateHead(node) { + return node.kind === 12; } - function isTemplateLiteralFragment(node) { - return isTemplateLiteralFragmentKind(node.kind); + ts.isTemplateHead = isTemplateHead; + function isTemplateMiddleOrTemplateTail(node) { + var kind = node.kind; + return kind === 13 + || kind === 14; } - ts.isTemplateLiteralFragment = isTemplateLiteralFragment; + ts.isTemplateMiddleOrTemplateTail = isTemplateMiddleOrTemplateTail; function isIdentifier(node) { return node.kind === 69; } @@ -8491,12 +9621,12 @@ var ts; return node.kind === 174; } ts.isCallExpression = isCallExpression; - function isTemplate(node) { + function isTemplateLiteral(node) { var kind = node.kind; return kind === 189 || kind === 11; } - ts.isTemplate = isTemplate; + ts.isTemplateLiteral = isTemplateLiteral; function isSpreadElementExpression(node) { return node.kind === 191; } @@ -8827,7 +9957,6 @@ var ts; } ts.isWatchSet = isWatchSet; })(ts || (ts = {})); -var ts; (function (ts) { function getDefaultLibFileName(options) { return options.target === 2 ? "lib.es6.d.ts" : "lib.d.ts"; @@ -9062,7 +10191,7 @@ var ts; ts.createSynthesizedNodeArray = createSynthesizedNodeArray; function getSynthesizedClone(node) { var clone = createNode(node.kind, undefined, node.flags); - clone.original = node; + setOriginalNode(clone, node); for (var key in node) { if (clone.hasOwnProperty(key) || !node.hasOwnProperty(key)) { continue; @@ -9094,7 +10223,7 @@ var ts; node.text = value; return node; } - else { + else if (value) { var node = createNode(9, location, undefined); node.textSourceNode = value; node.text = value.text; @@ -9381,7 +10510,7 @@ var ts; function createPropertyAccess(expression, name, location, flags) { var node = createNode(172, location, flags); node.expression = parenthesizeForAccess(expression); - node.emitFlags = 1048576; + (node.emitNode || (node.emitNode = {})).flags |= 1048576; node.name = typeof name === "string" ? createIdentifier(name) : name; return node; } @@ -9389,7 +10518,7 @@ var ts; function updatePropertyAccess(node, expression, name) { if (node.expression !== expression || node.name !== name) { var propertyAccess = createPropertyAccess(expression, name, node, node.flags); - propertyAccess.emitFlags = node.emitFlags; + (propertyAccess.emitNode || (propertyAccess.emitNode = {})).flags = getEmitFlags(node); return updateNode(propertyAccess, node); } return node; @@ -9493,7 +10622,7 @@ var ts; node.typeParameters = typeParameters ? createNodeArray(typeParameters) : undefined; node.parameters = createNodeArray(parameters); node.type = type; - node.equalsGreaterThanToken = equalsGreaterThanToken || createNode(34); + node.equalsGreaterThanToken = equalsGreaterThanToken || createToken(34); node.body = parenthesizeConciseBody(body); return node; } @@ -9586,7 +10715,7 @@ var ts; } ts.updatePostfix = updatePostfix; function createBinary(left, operator, right, location) { - var operatorToken = typeof operator === "number" ? createSynthesizedNode(operator) : operator; + var operatorToken = typeof operator === "number" ? createToken(operator) : operator; var operatorKind = operatorToken.kind; var node = createNode(187, location); node.left = parenthesizeBinaryOperand(operatorKind, left, true, undefined); @@ -10486,13 +11615,13 @@ var ts; } else { var expression = ts.isIdentifier(memberName) ? createPropertyAccess(target, memberName, location) : createElementAccess(target, memberName, location); - expression.emitFlags |= 2048; + (expression.emitNode || (expression.emitNode = {})).flags |= 2048; return expression; } } ts.createMemberAccessForPropertyName = createMemberAccessForPropertyName; function createRestParameter(name) { - return createParameterDeclaration(undefined, undefined, createSynthesizedNode(22), name, undefined, undefined, undefined); + return createParameterDeclaration(undefined, undefined, createToken(22), name, undefined, undefined, undefined); } ts.createRestParameter = createRestParameter; function createFunctionCall(func, thisArg, argumentsList, location) { @@ -10606,8 +11735,8 @@ var ts; } ts.createDecorateHelper = createDecorateHelper; function createAwaiterHelper(externalHelpersModuleName, hasLexicalArguments, promiseConstructor, body) { - var generatorFunc = createFunctionExpression(createNode(37), undefined, undefined, [], undefined, body); - generatorFunc.emitFlags |= 2097152; + var generatorFunc = createFunctionExpression(createToken(37), undefined, undefined, [], undefined, body); + (generatorFunc.emitNode || (generatorFunc.emitNode = {})).flags |= 2097152; return createCall(createHelperName(externalHelpersModuleName, "__awaiter"), undefined, [ createThis(), hasLexicalArguments ? createIdentifier("arguments") : createVoidZero(), @@ -10834,7 +11963,7 @@ var ts; target.push(startOnNewLine(createStatement(createLiteral("use strict")))); foundUseStrict = true; } - if (statement.emitFlags & 8388608) { + if (getEmitFlags(statement) & 8388608) { target.push(visitor ? ts.visitNode(statement, visitor, ts.isStatement) : statement); } else { @@ -10846,6 +11975,28 @@ var ts; return statementOffset; } ts.addPrologueDirectives = addPrologueDirectives; + function ensureUseStrict(node) { + var foundUseStrict = false; + for (var _i = 0, _a = node.statements; _i < _a.length; _i++) { + var statement = _a[_i]; + if (ts.isPrologueDirective(statement)) { + if (isUseStrictPrologue(statement)) { + foundUseStrict = true; + break; + } + } + else { + break; + } + } + if (!foundUseStrict) { + var statements = []; + statements.push(startOnNewLine(createStatement(createLiteral("use strict")))); + return updateSourceFileNode(node, statements.concat(node.statements)); + } + return node; + } + ts.ensureUseStrict = ensureUseStrict; function parenthesizeBinaryOperand(binaryOperator, operand, isLeftSideOfBinary, leftOperand) { var skipped = skipPartiallyEmittedExpressions(operand); if (skipped.kind === 178) { @@ -11085,17 +12236,112 @@ var ts; function setOriginalNode(node, original) { node.original = original; if (original) { - var emitFlags = original.emitFlags, commentRange = original.commentRange, sourceMapRange = original.sourceMapRange; - if (emitFlags) - node.emitFlags = emitFlags; - if (commentRange) - node.commentRange = commentRange; - if (sourceMapRange) - node.sourceMapRange = sourceMapRange; + var emitNode = original.emitNode; + if (emitNode) + node.emitNode = mergeEmitNode(emitNode, node.emitNode); } return node; } ts.setOriginalNode = setOriginalNode; + function mergeEmitNode(sourceEmitNode, destEmitNode) { + var flags = sourceEmitNode.flags, commentRange = sourceEmitNode.commentRange, sourceMapRange = sourceEmitNode.sourceMapRange, tokenSourceMapRanges = sourceEmitNode.tokenSourceMapRanges; + if (!destEmitNode && (flags || commentRange || sourceMapRange || tokenSourceMapRanges)) + destEmitNode = {}; + if (flags) + destEmitNode.flags = flags; + if (commentRange) + destEmitNode.commentRange = commentRange; + if (sourceMapRange) + destEmitNode.sourceMapRange = sourceMapRange; + if (tokenSourceMapRanges) + destEmitNode.tokenSourceMapRanges = mergeTokenSourceMapRanges(tokenSourceMapRanges, destEmitNode.tokenSourceMapRanges); + return destEmitNode; + } + function mergeTokenSourceMapRanges(sourceRanges, destRanges) { + if (!destRanges) + destRanges = ts.createMap(); + ts.copyProperties(sourceRanges, destRanges); + return destRanges; + } + function disposeEmitNodes(sourceFile) { + sourceFile = ts.getSourceFileOfNode(ts.getParseTreeNode(sourceFile)); + var emitNode = sourceFile && sourceFile.emitNode; + var annotatedNodes = emitNode && emitNode.annotatedNodes; + if (annotatedNodes) { + for (var _i = 0, annotatedNodes_1 = annotatedNodes; _i < annotatedNodes_1.length; _i++) { + var node = annotatedNodes_1[_i]; + node.emitNode = undefined; + } + } + } + ts.disposeEmitNodes = disposeEmitNodes; + function getOrCreateEmitNode(node) { + if (!node.emitNode) { + if (ts.isParseTreeNode(node)) { + if (node.kind === 256) { + return node.emitNode = { annotatedNodes: [node] }; + } + var sourceFile = ts.getSourceFileOfNode(node); + getOrCreateEmitNode(sourceFile).annotatedNodes.push(node); + } + node.emitNode = {}; + } + return node.emitNode; + } + function getEmitFlags(node) { + var emitNode = node.emitNode; + return emitNode && emitNode.flags; + } + ts.getEmitFlags = getEmitFlags; + function setEmitFlags(node, emitFlags) { + getOrCreateEmitNode(node).flags = emitFlags; + return node; + } + ts.setEmitFlags = setEmitFlags; + function setSourceMapRange(node, range) { + getOrCreateEmitNode(node).sourceMapRange = range; + return node; + } + ts.setSourceMapRange = setSourceMapRange; + function setTokenSourceMapRange(node, token, range) { + var emitNode = getOrCreateEmitNode(node); + var tokenSourceMapRanges = emitNode.tokenSourceMapRanges || (emitNode.tokenSourceMapRanges = ts.createMap()); + tokenSourceMapRanges[token] = range; + return node; + } + ts.setTokenSourceMapRange = setTokenSourceMapRange; + function setCommentRange(node, range) { + getOrCreateEmitNode(node).commentRange = range; + return node; + } + ts.setCommentRange = setCommentRange; + function getCommentRange(node) { + var emitNode = node.emitNode; + return (emitNode && emitNode.commentRange) || node; + } + ts.getCommentRange = getCommentRange; + function getSourceMapRange(node) { + var emitNode = node.emitNode; + return (emitNode && emitNode.sourceMapRange) || node; + } + ts.getSourceMapRange = getSourceMapRange; + function getTokenSourceMapRange(node, token) { + var emitNode = node.emitNode; + var tokenSourceMapRanges = emitNode && emitNode.tokenSourceMapRanges; + return tokenSourceMapRanges && tokenSourceMapRanges[token]; + } + ts.getTokenSourceMapRange = getTokenSourceMapRange; + function getConstantValue(node) { + var emitNode = node.emitNode; + return emitNode && emitNode.constantValue; + } + ts.getConstantValue = getConstantValue; + function setConstantValue(node, value) { + var emitNode = getOrCreateEmitNode(node); + emitNode.constantValue = value; + return node; + } + ts.setConstantValue = setConstantValue; function setTextRange(node, location) { if (location) { node.pos = location.pos; @@ -11122,8 +12368,8 @@ var ts; function getLocalNameForExternalImport(node, sourceFile) { var namespaceDeclaration = ts.getNamespaceDeclarationNode(node); if (namespaceDeclaration && !ts.isDefaultImport(node)) { - var name_11 = namespaceDeclaration.name; - return ts.isGeneratedIdentifier(name_11) ? name_11 : createIdentifier(ts.getSourceTextOfNodeFromSourceFile(sourceFile, namespaceDeclaration.name)); + var name_12 = namespaceDeclaration.name; + return ts.isGeneratedIdentifier(name_12) ? name_12 : createIdentifier(ts.getSourceTextOfNodeFromSourceFile(sourceFile, namespaceDeclaration.name)); } if (node.kind === 230 && node.importClause) { return getGeneratedNameForNode(node); @@ -12205,7 +13451,7 @@ var ts; case 16: return token() === 18 || token() === 20; case 18: - return token() === 27 || token() === 17; + return token() !== 24; case 20: return token() === 15 || token() === 16; case 13: @@ -12533,7 +13779,7 @@ var ts; } function parseTemplateExpression() { var template = createNode(189); - template.head = parseTemplateLiteralFragment(); + template.head = parseTemplateHead(); ts.Debug.assert(template.head.kind === 12, "Template head has wrong token kind"); var templateSpans = createNodeArray(); do { @@ -12549,7 +13795,7 @@ var ts; var literal; if (token() === 16) { reScanTemplateToken(); - literal = parseTemplateLiteralFragment(); + literal = parseTemplateMiddleOrTemplateTail(); } else { literal = parseExpectedToken(14, false, ts.Diagnostics._0_expected, ts.tokenToString(16)); @@ -12560,8 +13806,15 @@ var ts; function parseLiteralNode(internName) { return parseLiteralLikeNode(token(), internName); } - function parseTemplateLiteralFragment() { - return parseLiteralLikeNode(token(), false); + function parseTemplateHead() { + var fragment = parseLiteralLikeNode(token(), false); + ts.Debug.assert(fragment.kind === 12, "Template head has wrong token kind"); + return fragment; + } + function parseTemplateMiddleOrTemplateTail() { + var fragment = parseLiteralLikeNode(token(), false); + ts.Debug.assert(fragment.kind === 13 || fragment.kind === 14, "Template fragment has wrong token kind"); + return fragment; } function parseLiteralLikeNode(kind, internName) { var node = createNode(kind); @@ -14822,8 +16075,8 @@ var ts; return parsePropertyOrMethodDeclaration(fullStart, decorators, modifiers); } if (decorators || modifiers) { - var name_12 = createMissingNode(69, true, ts.Diagnostics.Declaration_expected); - return parsePropertyDeclaration(fullStart, decorators, modifiers, name_12, undefined); + var name_13 = createMissingNode(69, true, ts.Diagnostics.Declaration_expected); + return parsePropertyDeclaration(fullStart, decorators, modifiers, name_13, undefined); } ts.Debug.fail("Should not have attempted to parse class member declaration."); } @@ -14909,7 +16162,7 @@ var ts; parseExpected(56); node.type = parseType(); parseSemicolon(); - return finishNode(node); + return addJSDocComment(finishNode(node)); } function parseEnumMember() { var node = createNode(255, scanner.getStartPos()); @@ -15847,8 +17100,8 @@ var ts; if (typeExpression.type.kind === 267) { var jsDocTypeReference = typeExpression.type; if (jsDocTypeReference.name.kind === 69) { - var name_13 = jsDocTypeReference.name; - if (name_13.text === "Object") { + var name_14 = jsDocTypeReference.name; + if (name_14.text === "Object") { typedefTag.jsDocTypeLiteral = scanChildTags(); } } @@ -15934,14 +17187,14 @@ var ts; } var typeParameters = createNodeArray(); while (true) { - var name_14 = parseJSDocIdentifierName(); + var name_15 = parseJSDocIdentifierName(); skipWhitespace(); - if (!name_14) { + if (!name_15) { parseErrorAtPosition(scanner.getStartPos(), 0, ts.Diagnostics.Identifier_expected); return undefined; } - var typeParameter = createNode(141, name_14.pos); - typeParameter.name = name_14; + var typeParameter = createNode(141, name_15.pos); + typeParameter.name = name_15; finishNode(typeParameter); typeParameters.push(typeParameter); if (token() === 24) { @@ -16344,7 +17597,7 @@ var ts; file = f; options = opts; languageVersion = ts.getEmitScriptTarget(options); - inStrictMode = !!file.externalModuleIndicator; + inStrictMode = bindInStrictMode(file, opts); classifiableNames = ts.createMap(); symbolCount = 0; skipTransformFlagAggregation = ts.isDeclarationFile(file); @@ -16374,6 +17627,14 @@ var ts; subtreeTransformFlags = 0; } return bindSourceFile; + function bindInStrictMode(file, opts) { + if (opts.alwaysStrict && !ts.isDeclarationFile(file)) { + return true; + } + else { + return !!file.externalModuleIndicator; + } + } function createSymbol(flags, name) { symbolCount++; return new Symbol(flags, name); @@ -16492,11 +17753,17 @@ var ts; var message_1 = symbol.flags & 2 ? ts.Diagnostics.Cannot_redeclare_block_scoped_variable_0 : ts.Diagnostics.Duplicate_identifier_0; - ts.forEach(symbol.declarations, function (declaration) { - if (ts.hasModifier(declaration, 512)) { + if (symbol.declarations && symbol.declarations.length) { + if (isDefaultExport) { message_1 = ts.Diagnostics.A_module_cannot_have_multiple_default_exports; } - }); + else { + if (symbol.declarations && symbol.declarations.length && + (isDefaultExport || (node.kind === 235 && !node.isExportEquals))) { + message_1 = ts.Diagnostics.A_module_cannot_have_multiple_default_exports; + } + } + } ts.forEach(symbol.declarations, function (declaration) { file.bindDiagnostics.push(ts.createDiagnosticForNode(declaration.name || declaration, message_1, getDisplayName(declaration))); }); @@ -16561,7 +17828,7 @@ var ts; } else { currentFlow = { flags: 2 }; - if (containerFlags & 16) { + if (containerFlags & (16 | 128)) { currentFlow.container = node; } currentReturnTarget = undefined; @@ -17016,7 +18283,9 @@ var ts; currentFlow = preTryFlow; bind(node.finallyBlock); } - currentFlow = finishFlowLabel(postFinallyLabel); + if (!node.finallyBlock || !(currentFlow.flags & 1)) { + currentFlow = finishFlowLabel(postFinallyLabel); + } } function bindSwitchStatement(node) { var postSwitchLabel = createBranchLabel(); @@ -17149,7 +18418,7 @@ var ts; } else { ts.forEachChild(node, bind); - if (node.operator === 57 || node.operator === 42) { + if (node.operator === 41 || node.operator === 42) { bindAssignmentTargetFlow(node.operand); } } @@ -17248,9 +18517,12 @@ var ts; return 1 | 32; case 256: return 1 | 4 | 32; + case 147: + if (ts.isObjectLiteralOrClassExpressionMethod(node)) { + return 1 | 4 | 32 | 8 | 128; + } case 148: case 220: - case 147: case 146: case 149: case 150: @@ -17782,7 +19054,7 @@ var ts; var flags = node.kind === 235 && ts.exportAssignmentIsAlias(node) ? 8388608 : 4; - declareSymbol(container.symbol.exports, container.symbol, node, flags, 0 | 8388608); + declareSymbol(container.symbol.exports, container.symbol, node, flags, 4 | 8388608 | 32 | 16); } } function bindNamespaceExportDeclaration(node) { @@ -17794,12 +19066,12 @@ var ts; return; } else { - var parent_6 = node.parent; - if (!ts.isExternalModule(parent_6)) { + var parent_7 = node.parent; + if (!ts.isExternalModule(parent_7)) { file.bindDiagnostics.push(ts.createDiagnosticForNode(node, ts.Diagnostics.Global_module_exports_may_only_appear_in_module_files)); return; } - if (!parent_6.isDeclarationFile) { + if (!parent_7.isDeclarationFile) { file.bindDiagnostics.push(ts.createDiagnosticForNode(node, ts.Diagnostics.Global_module_exports_may_only_appear_in_declaration_files)); return; } @@ -17979,6 +19251,9 @@ var ts; emitFlags |= 2048; } } + if (currentFlow && ts.isObjectLiteralOrClassExpressionMethod(node)) { + node.flowNode = currentFlow; + } return ts.hasDynamicName(node) ? bindAnonymousDeclaration(node, symbolFlags, "__computed") : declareSymbolAndAddToSymbolTable(node, symbolFlags, symbolExcludes); @@ -18117,8 +19392,7 @@ var ts; if (node.questionToken) { transformFlags |= 3; } - if (subtreeFlags & 2048 - || (name && ts.isIdentifier(name) && name.originalKeywordKind === 97)) { + if (subtreeFlags & 2048 || ts.isThisIdentifier(name)) { transformFlags |= 3; } if (modifierFlags & 92) { @@ -18220,7 +19494,7 @@ var ts; || (subtreeFlags & 2048)) { transformFlags |= 3; } - if (asteriskToken && node.emitFlags & 2097152) { + if (asteriskToken && ts.getEmitFlags(node) & 2097152) { transformFlags |= 1536; } node.transformFlags = transformFlags | 536870912; @@ -18265,7 +19539,7 @@ var ts; if (subtreeFlags & 81920) { transformFlags |= 192; } - if (asteriskToken && node.emitFlags & 2097152) { + if (asteriskToken && ts.getEmitFlags(node) & 2097152) { transformFlags |= 1536; } } @@ -18282,7 +19556,7 @@ var ts; if (subtreeFlags & 81920) { transformFlags |= 192; } - if (asteriskToken && node.emitFlags & 2097152) { + if (asteriskToken && ts.getEmitFlags(node) & 2097152) { transformFlags |= 1536; } node.transformFlags = transformFlags | 536870912; @@ -18618,6 +19892,7 @@ var ts; var unknownSymbol = createSymbol(4 | 67108864, "unknown"); var resolvingSymbol = createSymbol(67108864, "__resolving__"); var anyType = createIntrinsicType(1, "any"); + var autoType = createIntrinsicType(1, "any"); var unknownType = createIntrinsicType(1, "unknown"); var undefinedType = createIntrinsicType(2048, "undefined"); var undefinedWideningType = strictNullChecks ? undefinedType : createIntrinsicType(2048 | 33554432, "undefined"); @@ -19175,7 +20450,7 @@ var ts; if (result && isInExternalModule && (meaning & 107455) === 107455) { var decls = result.declarations; if (decls && decls.length === 1 && decls[0].kind === 228) { - error(errorLocation, ts.Diagnostics.Identifier_0_must_be_imported_from_a_module, name); + error(errorLocation, ts.Diagnostics._0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead, name); } } } @@ -19335,28 +20610,28 @@ var ts; var moduleSymbol = resolveExternalModuleName(node, node.moduleSpecifier); var targetSymbol = resolveESModuleSymbol(moduleSymbol, node.moduleSpecifier); if (targetSymbol) { - var name_15 = specifier.propertyName || specifier.name; - if (name_15.text) { + var name_16 = specifier.propertyName || specifier.name; + if (name_16.text) { if (ts.isShorthandAmbientModuleSymbol(moduleSymbol)) { return moduleSymbol; } var symbolFromVariable = void 0; if (moduleSymbol && moduleSymbol.exports && moduleSymbol.exports["export="]) { - symbolFromVariable = getPropertyOfType(getTypeOfSymbol(targetSymbol), name_15.text); + symbolFromVariable = getPropertyOfType(getTypeOfSymbol(targetSymbol), name_16.text); } else { - symbolFromVariable = getPropertyOfVariable(targetSymbol, name_15.text); + symbolFromVariable = getPropertyOfVariable(targetSymbol, name_16.text); } symbolFromVariable = resolveSymbol(symbolFromVariable); - var symbolFromModule = getExportOfModule(targetSymbol, name_15.text); - if (!symbolFromModule && allowSyntheticDefaultImports && name_15.text === "default") { + var symbolFromModule = getExportOfModule(targetSymbol, name_16.text); + if (!symbolFromModule && allowSyntheticDefaultImports && name_16.text === "default") { symbolFromModule = resolveExternalModuleSymbol(moduleSymbol) || resolveSymbol(moduleSymbol); } var symbol = symbolFromModule && symbolFromVariable ? combineValueAndTypeSymbols(symbolFromVariable, symbolFromModule) : symbolFromModule || symbolFromVariable; if (!symbol) { - error(name_15, ts.Diagnostics.Module_0_has_no_exported_member_1, getFullyQualifiedName(moduleSymbol), ts.declarationNameToString(name_15)); + error(name_16, ts.Diagnostics.Module_0_has_no_exported_member_1, getFullyQualifiedName(moduleSymbol), ts.declarationNameToString(name_16)); } return symbol; } @@ -19593,6 +20868,7 @@ var ts; } function getExportsForModule(moduleSymbol) { var visitedSymbols = []; + moduleSymbol = resolveExternalModuleSymbol(moduleSymbol); return visit(moduleSymbol) || moduleSymbol.exports; function visit(symbol) { if (!(symbol && symbol.flags & 1952 && !ts.contains(visitedSymbols, symbol))) { @@ -20077,9 +21353,9 @@ var ts; var accessibleSymbolChain = getAccessibleSymbolChain(symbol, enclosingDeclaration, meaning, !!(flags & 2)); if (!accessibleSymbolChain || needsQualification(accessibleSymbolChain[0], enclosingDeclaration, accessibleSymbolChain.length === 1 ? meaning : getQualifiedLeftMeaning(meaning))) { - var parent_7 = getParentOfSymbol(accessibleSymbolChain ? accessibleSymbolChain[0] : symbol); - if (parent_7) { - walkSymbol(parent_7, getQualifiedLeftMeaning(meaning), false); + var parent_8 = getParentOfSymbol(accessibleSymbolChain ? accessibleSymbolChain[0] : symbol); + if (parent_8) { + walkSymbol(parent_8, getQualifiedLeftMeaning(meaning), false); } } if (accessibleSymbolChain) { @@ -20114,7 +21390,7 @@ var ts; ? "any" : type.intrinsicName); } - else if (type.flags & 268435456) { + else if (type.flags & 16384 && type.isThisType) { if (inObjectTypeLiteral) { writer.reportInaccessibleThisError(); } @@ -20131,7 +21407,7 @@ var ts; else if (type.flags & (32768 | 65536 | 16 | 16384)) { buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, 793064, 0, nextFlags); } - else if (!(flags & 512) && type.flags & (2097152 | 1572864) && type.aliasSymbol && + else if (!(flags & 512) && ((type.flags & 2097152 && !type.target) || type.flags & 1572864) && type.aliasSymbol && isSymbolAccessible(type.aliasSymbol, enclosingDeclaration, 793064, false).accessibility === 0) { var typeArguments = type.aliasTypeArguments; writeSymbolTypeReference(type.aliasSymbol, typeArguments, 0, typeArguments ? typeArguments.length : 0, nextFlags); @@ -20201,12 +21477,12 @@ var ts; var length_1 = outerTypeParameters.length; while (i < length_1) { var start = i; - var parent_8 = getParentSymbolOfTypeParameter(outerTypeParameters[i]); + var parent_9 = getParentSymbolOfTypeParameter(outerTypeParameters[i]); do { i++; - } while (i < length_1 && getParentSymbolOfTypeParameter(outerTypeParameters[i]) === parent_8); + } while (i < length_1 && getParentSymbolOfTypeParameter(outerTypeParameters[i]) === parent_9); if (!ts.rangeEquals(outerTypeParameters, typeArguments, start, i)) { - writeSymbolTypeReference(parent_8, typeArguments, start, i, flags); + writeSymbolTypeReference(parent_9, typeArguments, start, i, flags); writePunctuation(writer, 21); } } @@ -20595,12 +21871,12 @@ var ts; if (ts.isExternalModuleAugmentation(node)) { return true; } - var parent_9 = getDeclarationContainer(node); + var parent_10 = getDeclarationContainer(node); if (!(ts.getCombinedModifierFlags(node) & 1) && - !(node.kind !== 229 && parent_9.kind !== 256 && ts.isInAmbientContext(parent_9))) { - return isGlobalSourceFile(parent_9); + !(node.kind !== 229 && parent_10.kind !== 256 && ts.isInAmbientContext(parent_10))) { + return isGlobalSourceFile(parent_10); } - return isDeclarationVisible(parent_9); + return isDeclarationVisible(parent_10); case 145: case 144: case 149: @@ -20787,19 +22063,19 @@ var ts; } var type; if (pattern.kind === 167) { - var name_16 = declaration.propertyName || declaration.name; - if (isComputedNonLiteralName(name_16)) { + var name_17 = declaration.propertyName || declaration.name; + if (isComputedNonLiteralName(name_17)) { return anyType; } if (declaration.initializer) { getContextualType(declaration.initializer); } - var text = getTextOfPropertyName(name_16); + var text = getTextOfPropertyName(name_17); type = getTypeOfPropertyOfType(parentType, text) || isNumericLiteralName(text) && getIndexTypeOfType(parentType, 1) || getIndexTypeOfType(parentType, 0); if (!type) { - error(name_16, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(parentType), ts.declarationNameToString(name_16)); + error(name_17, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(parentType), ts.declarationNameToString(name_17)); return unknownType; } } @@ -20858,6 +22134,10 @@ var ts; } return undefined; } + function isAutoVariableInitializer(initializer) { + var expr = initializer && ts.skipParentheses(initializer); + return !expr || expr.kind === 93 || expr.kind === 69 && getResolvedSymbol(expr) === undefinedSymbol; + } function addOptionality(type, optional) { return strictNullChecks && optional ? includeFalsyTypes(type, 2048) : type; } @@ -20880,6 +22160,11 @@ var ts; if (declaration.type) { return addOptionality(getTypeFromTypeNode(declaration.type), declaration.questionToken && includeOptionality); } + if (declaration.kind === 218 && !ts.isBindingPattern(declaration.name) && + !(ts.getCombinedNodeFlags(declaration) & 2) && !(ts.getCombinedModifierFlags(declaration) & 1) && + !ts.isInAmbientContext(declaration) && isAutoVariableInitializer(declaration.initializer)) { + return autoType; + } if (declaration.kind === 142) { var func = declaration.parent; if (func.kind === 150 && !ts.hasDynamicName(func)) { @@ -20951,7 +22236,7 @@ var ts; result.pattern = pattern; } if (hasComputedProperties) { - result.flags |= 536870912; + result.isObjectLiteralPatternWithComputedProperties = true; } return result; } @@ -21420,7 +22705,8 @@ var ts; type.instantiations[getTypeListId(type.typeParameters)] = type; type.target = type; type.typeArguments = type.typeParameters; - type.thisType = createType(16384 | 268435456); + type.thisType = createType(16384); + type.thisType.isThisType = true; type.thisType.symbol = symbol; type.thisType.constraint = type; } @@ -21953,13 +23239,24 @@ var ts; var current = _a[_i]; for (var _b = 0, _c = getPropertiesOfType(current); _b < _c.length; _b++) { var prop = _c[_b]; - getPropertyOfUnionOrIntersectionType(type, prop.name); + getUnionOrIntersectionProperty(type, prop.name); } if (type.flags & 524288) { break; } } - return type.resolvedProperties ? symbolsToArray(type.resolvedProperties) : emptyArray; + var props = type.resolvedProperties; + if (props) { + var result = []; + for (var key in props) { + var prop = props[key]; + if (!(prop.flags & 268435456 && prop.isPartial)) { + result.push(prop); + } + } + return result; + } + return emptyArray; } function getPropertiesOfType(type) { type = getApparentType(type); @@ -21998,6 +23295,7 @@ var ts; var props; var commonFlags = (containingType.flags & 1048576) ? 536870912 : 0; var isReadonly = false; + var isPartial = false; for (var _i = 0, types_2 = types; _i < types_2.length; _i++) { var current = types_2[_i]; var type = getApparentType(current); @@ -22016,20 +23314,20 @@ var ts; } } else if (containingType.flags & 524288) { - return undefined; + isPartial = true; } } } if (!props) { return undefined; } - if (props.length === 1) { + if (props.length === 1 && !isPartial) { return props[0]; } var propTypes = []; var declarations = []; var commonType = undefined; - var hasCommonType = true; + var hasNonUniformType = false; for (var _a = 0, props_1 = props; _a < props_1.length; _a++) { var prop = props_1[_a]; if (prop.declarations) { @@ -22040,22 +23338,20 @@ var ts; commonType = type; } else if (type !== commonType) { - hasCommonType = false; + hasNonUniformType = true; } - propTypes.push(getTypeOfSymbol(prop)); + propTypes.push(type); } - var result = createSymbol(4 | - 67108864 | - 268435456 | - commonFlags, name); + var result = createSymbol(4 | 67108864 | 268435456 | commonFlags, name); result.containingType = containingType; - result.hasCommonType = hasCommonType; + result.hasNonUniformType = hasNonUniformType; + result.isPartial = isPartial; result.declarations = declarations; result.isReadonly = isReadonly; result.type = containingType.flags & 524288 ? getUnionType(propTypes) : getIntersectionType(propTypes); return result; } - function getPropertyOfUnionOrIntersectionType(type, name) { + function getUnionOrIntersectionProperty(type, name) { var properties = type.resolvedProperties || (type.resolvedProperties = ts.createMap()); var property = properties[name]; if (!property) { @@ -22066,6 +23362,10 @@ var ts; } return property; } + function getPropertyOfUnionOrIntersectionType(type, name) { + var property = getUnionOrIntersectionProperty(type, name); + return property && !(property.flags & 268435456 && property.isPartial) ? property : undefined; + } function getPropertyOfType(type, name) { type = getApparentType(type); if (type.flags & 2588672) { @@ -22438,7 +23738,7 @@ var ts; } function hasConstraintReferenceTo(type, target) { var checked; - while (type && !(type.flags & 268435456) && type.flags & 16384 && !ts.contains(checked, type)) { + while (type && type.flags & 16384 && !(type.isThisType) && !ts.contains(checked, type)) { if (type === target) { return true; } @@ -22722,7 +24022,8 @@ var ts; type.instantiations[getTypeListId(type.typeParameters)] = type; type.target = type; type.typeArguments = type.typeParameters; - type.thisType = createType(16384 | 268435456); + type.thisType = createType(16384); + type.thisType.isThisType = true; type.thisType.constraint = type; type.declaredProperties = properties; type.declaredCallSignatures = emptyArray; @@ -22821,7 +24122,24 @@ var ts; } return false; } + function isSetOfLiteralsFromSameEnum(types) { + var first = types[0]; + if (first.flags & 256) { + var firstEnum = getParentOfSymbol(first.symbol); + for (var i = 1; i < types.length; i++) { + var other = types[i]; + if (!(other.flags & 256) || (firstEnum !== getParentOfSymbol(other.symbol))) { + return false; + } + } + return true; + } + return false; + } function removeSubtypes(types) { + if (types.length === 0 || isSetOfLiteralsFromSameEnum(types)) { + return; + } var i = types.length; while (i > 0) { i--; @@ -23796,7 +25114,8 @@ var ts; !t.numberIndexInfo; } function hasExcessProperties(source, target, reportErrors) { - if (!(target.flags & 536870912) && maybeTypeOfKind(target, 2588672)) { + if (maybeTypeOfKind(target, 2588672) && + (!(target.flags & 2588672) || !target.isObjectLiteralPatternWithComputedProperties)) { for (var _i = 0, _a = getPropertiesOfObjectType(source); _i < _a.length; _i++) { var prop = _a[_i]; if (!isKnownProperty(target, prop.name)) { @@ -25001,17 +26320,10 @@ var ts; } function isDiscriminantProperty(type, name) { if (type && type.flags & 524288) { - var prop = getPropertyOfType(type, name); - if (!prop) { - var filteredType = getTypeWithFacts(type, 4194304); - if (filteredType !== type && filteredType.flags & 524288) { - prop = getPropertyOfType(filteredType, name); - } - } + var prop = getUnionOrIntersectionProperty(type, name); if (prop && prop.flags & 268435456) { if (prop.isDiscriminantProperty === undefined) { - prop.isDiscriminantProperty = !prop.hasCommonType && - isLiteralType(getTypeOfSymbol(prop)); + prop.isDiscriminantProperty = prop.hasNonUniformType && isLiteralType(getTypeOfSymbol(prop)); } return prop.isDiscriminantProperty; } @@ -25288,6 +26600,23 @@ var ts; } return f(type) ? type : neverType; } + function mapType(type, f) { + return type.flags & 524288 ? getUnionType(ts.map(type.types, f)) : f(type); + } + function extractTypesOfKind(type, kind) { + return filterType(type, function (t) { return (t.flags & kind) !== 0; }); + } + function replacePrimitivesWithLiterals(typeWithPrimitives, typeWithLiterals) { + if (isTypeSubsetOf(stringType, typeWithPrimitives) && maybeTypeOfKind(typeWithLiterals, 32) || + isTypeSubsetOf(numberType, typeWithPrimitives) && maybeTypeOfKind(typeWithLiterals, 64)) { + return mapType(typeWithPrimitives, function (t) { + return t.flags & 2 ? extractTypesOfKind(typeWithLiterals, 2 | 32) : + t.flags & 4 ? extractTypesOfKind(typeWithLiterals, 4 | 64) : + t; + }); + } + return typeWithPrimitives; + } function isIncomplete(flowType) { return flowType.flags === 0; } @@ -25302,7 +26631,9 @@ var ts; if (!reference.flowNode || assumeInitialized && !(declaredType.flags & 4178943)) { return declaredType; } - var initialType = assumeInitialized ? declaredType : includeFalsyTypes(declaredType, 2048); + var initialType = assumeInitialized ? declaredType : + declaredType === autoType ? undefinedType : + includeFalsyTypes(declaredType, 2048); var visitedFlowStart = visitedFlowCount; var result = getTypeFromFlowType(getTypeAtFlowNode(reference.flowNode)); visitedFlowCount = visitedFlowStart; @@ -25364,10 +26695,13 @@ var ts; function getTypeAtFlowAssignment(flow) { var node = flow.node; if (isMatchingReference(reference, node)) { - var isIncrementOrDecrement = node.parent.kind === 185 || node.parent.kind === 186; - return declaredType.flags & 524288 && !isIncrementOrDecrement ? - getAssignmentReducedType(declaredType, getInitialOrAssignedType(node)) : - declaredType; + if (node.parent.kind === 185 || node.parent.kind === 186) { + var flowType = getTypeAtFlowNode(flow.antecedent); + return createFlowType(getBaseTypeOfLiteralType(getTypeFromFlowType(flowType)), isIncomplete(flowType)); + } + return declaredType === autoType ? getBaseTypeOfLiteralType(getInitialOrAssignedType(node)) : + declaredType.flags & 524288 ? getAssignmentReducedType(declaredType, getInitialOrAssignedType(node)) : + declaredType; } if (containsMatchingReference(reference, node)) { return declaredType; @@ -25553,12 +26887,12 @@ var ts; assumeTrue ? 16384 : 131072; return getTypeWithFacts(type, facts); } - if (type.flags & 2589191) { + if (type.flags & 2589185) { return type; } if (assumeTrue) { var narrowedType = filterType(type, function (t) { return areTypesComparable(t, valueType); }); - return narrowedType.flags & 8192 ? type : narrowedType; + return narrowedType.flags & 8192 ? type : replacePrimitivesWithLiterals(narrowedType, valueType); } if (isUnitType(valueType)) { var regularType_1 = getRegularTypeOfLiteralType(valueType); @@ -25596,7 +26930,8 @@ var ts; var clauseTypes = switchTypes.slice(clauseStart, clauseEnd); var hasDefaultClause = clauseStart === clauseEnd || ts.contains(clauseTypes, neverType); var discriminantType = getUnionType(clauseTypes); - var caseType = discriminantType.flags & 8192 ? neverType : filterType(type, function (t) { return isTypeComparableTo(discriminantType, t); }); + var caseType = discriminantType.flags & 8192 ? neverType : + replacePrimitivesWithLiterals(filterType(type, function (t) { return isTypeComparableTo(discriminantType, t); }), discriminantType); if (!hasDefaultClause) { return caseType; } @@ -25723,7 +27058,7 @@ var ts; if (ts.isRightSideOfQualifiedNameOrPropertyAccess(location)) { location = location.parent; } - if (ts.isExpression(location) && !ts.isAssignmentTarget(location)) { + if (ts.isPartOfExpression(location) && !ts.isAssignmentTarget(location)) { var type = checkExpression(location); if (getExportSymbolOfValueSymbolIfExported(getNodeLinks(location).resolvedSymbol) === symbol) { return type; @@ -25846,14 +27181,26 @@ var ts; var flowContainer = getControlFlowContainer(node); var isOuterVariable = flowContainer !== declarationContainer; while (flowContainer !== declarationContainer && - (flowContainer.kind === 179 || flowContainer.kind === 180) && + (flowContainer.kind === 179 || + flowContainer.kind === 180 || + ts.isObjectLiteralOrClassExpressionMethod(flowContainer)) && (isReadonlySymbol(localOrExportSymbol) || isParameter && !isParameterAssigned(localOrExportSymbol))) { flowContainer = getControlFlowContainer(flowContainer); } - var assumeInitialized = !strictNullChecks || (type.flags & 1) !== 0 || isParameter || - isOuterVariable || ts.isInAmbientContext(declaration); + var assumeInitialized = isParameter || isOuterVariable || + type !== autoType && (!strictNullChecks || (type.flags & 1) !== 0) || + ts.isInAmbientContext(declaration); var flowType = getFlowTypeOfReference(node, type, assumeInitialized, flowContainer); - if (!assumeInitialized && !(getFalsyFlags(type) & 2048) && getFalsyFlags(flowType) & 2048) { + if (type === autoType) { + if (flowType === autoType) { + if (compilerOptions.noImplicitAny) { + error(declaration.name, ts.Diagnostics.Variable_0_implicitly_has_type_any_in_some_locations_where_its_type_cannot_be_determined, symbolToString(symbol)); + error(node, ts.Diagnostics.Variable_0_implicitly_has_an_1_type, symbolToString(symbol), typeToString(anyType)); + } + return anyType; + } + } + else if (!assumeInitialized && !(getFalsyFlags(type) & 2048) && getFalsyFlags(flowType) & 2048) { error(node, ts.Diagnostics.Variable_0_is_used_before_being_assigned, symbolToString(symbol)); return type; } @@ -25938,7 +27285,7 @@ var ts; } } function findFirstSuperCall(n) { - if (ts.isSuperCallExpression(n)) { + if (ts.isSuperCall(n)) { return n; } else if (ts.isFunctionLike(n)) { @@ -26003,7 +27350,7 @@ var ts; captureLexicalThis(node, container); } if (ts.isFunctionLike(container) && - (!isInParameterInitializerBeforeContainingFunction(node) || getFunctionLikeThisParameter(container))) { + (!isInParameterInitializerBeforeContainingFunction(node) || ts.getThisParameter(container))) { if (container.kind === 179 && ts.isInJavaScriptFile(container.parent) && ts.getSpecialPropertyAssignmentKind(container.parent) === 3) { @@ -26222,11 +27569,11 @@ var ts; } if (ts.isBindingPattern(declaration.parent)) { var parentDeclaration = declaration.parent.parent; - var name_17 = declaration.propertyName || declaration.name; + var name_18 = declaration.propertyName || declaration.name; if (ts.isVariableLike(parentDeclaration) && parentDeclaration.type && - !ts.isBindingPattern(name_17)) { - var text = getTextOfPropertyName(name_17); + !ts.isBindingPattern(name_18)) { + var text = getTextOfPropertyName(name_18); if (text) { return getTypeOfPropertyOfType(getTypeFromTypeNode(parentDeclaration.type), text); } @@ -26663,7 +28010,8 @@ var ts; patternWithComputedProperties = true; } } - else if (contextualTypeHasPattern && !(contextualType.flags & 536870912)) { + else if (contextualTypeHasPattern && + !(contextualType.flags & 2588672 && contextualType.isObjectLiteralPatternWithComputedProperties)) { var impliedProp = getPropertyOfType(contextualType, member.name); if (impliedProp) { prop.flags |= impliedProp.flags & 536870912; @@ -26714,7 +28062,10 @@ var ts; var numberIndexInfo = hasComputedNumberProperty ? getObjectLiteralIndexInfo(node, propertiesArray, 1) : undefined; var result = createAnonymousType(node.symbol, propertiesTable, emptyArray, emptyArray, stringIndexInfo, numberIndexInfo); var freshObjectLiteralFlag = compilerOptions.suppressExcessPropertyErrors ? 0 : 16777216; - result.flags |= 8388608 | 67108864 | freshObjectLiteralFlag | (typeFlags & 234881024) | (patternWithComputedProperties ? 536870912 : 0); + result.flags |= 8388608 | 67108864 | freshObjectLiteralFlag | (typeFlags & 234881024); + if (patternWithComputedProperties) { + result.isObjectLiteralPatternWithComputedProperties = true; + } if (inDestructuringPattern) { result.pattern = node; } @@ -27109,7 +28460,7 @@ var ts; if (flags & 32) { return true; } - if (type.flags & 268435456) { + if (type.flags & 16384 && type.isThisType) { type = getConstraintOfTypeParameter(type); } if (!(getTargetType(type).flags & (32768 | 65536) && hasBaseType(type, enclosingClass))) { @@ -27150,7 +28501,7 @@ var ts; var prop = getPropertyOfType(apparentType, right.text); if (!prop) { if (right.text && !checkAndReportErrorForExtendingInterface(node)) { - reportNonexistentProperty(right, type.flags & 268435456 ? apparentType : type); + reportNonexistentProperty(right, type.flags & 16384 && type.isThisType ? apparentType : type); } return unknownType; } @@ -27266,15 +28617,15 @@ var ts; return unknownType; } if (node.argumentExpression) { - var name_18 = getPropertyNameForIndexedAccess(node.argumentExpression, indexType); - if (name_18 !== undefined) { - var prop = getPropertyOfType(objectType, name_18); + var name_19 = getPropertyNameForIndexedAccess(node.argumentExpression, indexType); + if (name_19 !== undefined) { + var prop = getPropertyOfType(objectType, name_19); if (prop) { getNodeLinks(node).resolvedSymbol = prop; return getTypeOfSymbol(prop); } else if (isConstEnum) { - error(node.argumentExpression, ts.Diagnostics.Property_0_does_not_exist_on_const_enum_1, name_18, symbolToString(objectType.symbol)); + error(node.argumentExpression, ts.Diagnostics.Property_0_does_not_exist_on_const_enum_1, name_19, symbolToString(objectType.symbol)); return unknownType; } } @@ -27375,19 +28726,19 @@ var ts; for (var _i = 0, signatures_2 = signatures; _i < signatures_2.length; _i++) { var signature = signatures_2[_i]; var symbol = signature.declaration && getSymbolOfNode(signature.declaration); - var parent_10 = signature.declaration && signature.declaration.parent; + var parent_11 = signature.declaration && signature.declaration.parent; if (!lastSymbol || symbol === lastSymbol) { - if (lastParent && parent_10 === lastParent) { + if (lastParent && parent_11 === lastParent) { index++; } else { - lastParent = parent_10; + lastParent = parent_11; index = cutoffIndex; } } else { index = cutoffIndex = result.length; - lastParent = parent_10; + lastParent = parent_11; } lastSymbol = symbol; if (signature.hasLiteralTypes) { @@ -28290,7 +29641,9 @@ var ts; if (!contextualSignature) { reportErrorsFromWidening(func, type); } - if (isUnitType(type) && !(contextualSignature && isLiteralContextualType(getReturnTypeOfSignature(contextualSignature)))) { + if (isUnitType(type) && + !(contextualSignature && + isLiteralContextualType(contextualSignature === getSignatureFromDeclaration(func) ? type : getReturnTypeOfSignature(contextualSignature)))) { type = getWidenedLiteralType(type); } var widenedType = getWidenedType(type); @@ -28440,7 +29793,7 @@ var ts; } } } - if (produceDiagnostics && node.kind !== 147 && node.kind !== 146) { + if (produceDiagnostics && node.kind !== 147) { checkCollisionWithCapturedSuperVariable(node, node.name); checkCollisionWithCapturedThisVariable(node, node.name); } @@ -28690,14 +30043,14 @@ var ts; } function checkObjectLiteralDestructuringPropertyAssignment(objectLiteralType, property, contextualMapper) { if (property.kind === 253 || property.kind === 254) { - var name_19 = property.name; - if (name_19.kind === 140) { - checkComputedPropertyName(name_19); + var name_20 = property.name; + if (name_20.kind === 140) { + checkComputedPropertyName(name_20); } - if (isComputedNonLiteralName(name_19)) { + if (isComputedNonLiteralName(name_20)) { return undefined; } - var text = getTextOfPropertyName(name_19); + var text = getTextOfPropertyName(name_20); var type = isTypeAny(objectLiteralType) ? objectLiteralType : getTypeOfPropertyOfType(objectLiteralType, text) || @@ -28712,7 +30065,7 @@ var ts; } } else { - error(name_19, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(objectLiteralType), ts.declarationNameToString(name_19)); + error(name_20, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(objectLiteralType), ts.declarationNameToString(name_20)); } } else { @@ -29361,9 +30714,9 @@ var ts; else if (parameterName) { var hasReportedError = false; for (var _i = 0, _a = parent.parameters; _i < _a.length; _i++) { - var name_20 = _a[_i].name; - if (ts.isBindingPattern(name_20) && - checkIfTypePredicateVariableIsDeclaredInBindingPattern(name_20, parameterName, typePredicate.parameterName)) { + var name_21 = _a[_i].name; + if (ts.isBindingPattern(name_21) && + checkIfTypePredicateVariableIsDeclaredInBindingPattern(name_21, parameterName, typePredicate.parameterName)) { hasReportedError = true; break; } @@ -29383,9 +30736,9 @@ var ts; case 156: case 147: case 146: - var parent_11 = node.parent; - if (node === parent_11.type) { - return parent_11; + var parent_12 = node.parent; + if (node === parent_12.type) { + return parent_12; } } } @@ -29395,15 +30748,15 @@ var ts; if (ts.isOmittedExpression(element)) { continue; } - var name_21 = element.name; - if (name_21.kind === 69 && - name_21.text === predicateVariableName) { + var name_22 = element.name; + if (name_22.kind === 69 && + name_22.text === predicateVariableName) { error(predicateVariableNode, ts.Diagnostics.A_type_predicate_cannot_reference_element_0_in_a_binding_pattern, predicateVariableName); return true; } - else if (name_21.kind === 168 || - name_21.kind === 167) { - if (checkIfTypePredicateVariableIsDeclaredInBindingPattern(name_21, predicateVariableNode, predicateVariableName)) { + else if (name_22.kind === 168 || + name_22.kind === 167) { + if (checkIfTypePredicateVariableIsDeclaredInBindingPattern(name_22, predicateVariableNode, predicateVariableName)) { return true; } } @@ -29596,7 +30949,7 @@ var ts; return n.name && containsSuperCall(n.name); } function containsSuperCall(n) { - if (ts.isSuperCallExpression(n)) { + if (ts.isSuperCall(n)) { return true; } else if (ts.isFunctionLike(n)) { @@ -29622,6 +30975,7 @@ var ts; } var containingClassDecl = node.parent; if (ts.getClassExtendsHeritageClauseElement(containingClassDecl)) { + captureLexicalThis(node.parent, containingClassDecl); var classExtendsNull = classDeclarationExtendsNull(containingClassDecl); var superCall = getSuperCallInConstructor(node); if (superCall) { @@ -29635,7 +30989,7 @@ var ts; var superCallStatement = void 0; for (var _i = 0, statements_2 = statements; _i < statements_2.length; _i++) { var statement = statements_2[_i]; - if (statement.kind === 202 && ts.isSuperCallExpression(statement.expression)) { + if (statement.kind === 202 && ts.isSuperCall(statement.expression)) { superCallStatement = statement; break; } @@ -30345,7 +31699,7 @@ var ts; var parameter = local.valueDeclaration; if (compilerOptions.noUnusedParameters && !ts.isParameterPropertyDeclaration(parameter) && - !parameterIsThisKeyword(parameter) && + !ts.parameterIsThisKeyword(parameter) && !parameterNameStartsWithUnderscore(parameter)) { error(local.valueDeclaration.name, ts.Diagnostics._0_is_declared_but_never_used, local.name); } @@ -30360,9 +31714,6 @@ var ts; } } } - function parameterIsThisKeyword(parameter) { - return parameter.name && parameter.name.originalKeywordKind === 97; - } function parameterNameStartsWithUnderscore(parameter) { return parameter.name && parameter.name.kind === 69 && parameter.name.text.charCodeAt(0) === 95; } @@ -30500,6 +31851,9 @@ var ts; } } function checkCollisionWithRequireExportsInGeneratedCode(node, name) { + if (modulekind >= ts.ModuleKind.ES6) { + return; + } if (!needCollisionCheckForIdentifier(node, name, "require") && !needCollisionCheckForIdentifier(node, name, "exports")) { return; } @@ -30547,8 +31901,8 @@ var ts; container.kind === 225 || container.kind === 256); if (!namesShareScope) { - var name_22 = symbolToString(localDeclarationSymbol); - error(node, ts.Diagnostics.Cannot_initialize_outer_scoped_variable_0_in_the_same_scope_as_block_scoped_declaration_1, name_22, name_22); + var name_23 = symbolToString(localDeclarationSymbol); + error(node, ts.Diagnostics.Cannot_initialize_outer_scoped_variable_0_in_the_same_scope_as_block_scoped_declaration_1, name_23, name_23); } } } @@ -30603,6 +31957,9 @@ var ts; } } } + function convertAutoToAny(type) { + return type === autoType ? anyType : type; + } function checkVariableLikeDeclaration(node) { checkDecorators(node); checkSourceElement(node.type); @@ -30616,12 +31973,12 @@ var ts; if (node.propertyName && node.propertyName.kind === 140) { checkComputedPropertyName(node.propertyName); } - var parent_12 = node.parent.parent; - var parentType = getTypeForBindingElementParent(parent_12); - var name_23 = node.propertyName || node.name; - var property = getPropertyOfType(parentType, getTextOfPropertyName(name_23)); - if (parent_12.initializer && property && getParentOfSymbol(property)) { - checkClassPropertyAccess(parent_12, parent_12.initializer, parentType, property); + var parent_13 = node.parent.parent; + var parentType = getTypeForBindingElementParent(parent_13); + var name_24 = node.propertyName || node.name; + var property = getPropertyOfType(parentType, getTextOfPropertyName(name_24)); + if (parent_13.initializer && property && getParentOfSymbol(property)) { + checkClassPropertyAccess(parent_13, parent_13.initializer, parentType, property); } } if (ts.isBindingPattern(node.name)) { @@ -30639,7 +31996,7 @@ var ts; return; } var symbol = getSymbolOfNode(node); - var type = getTypeOfVariableOrParameterOrProperty(symbol); + var type = convertAutoToAny(getTypeOfVariableOrParameterOrProperty(symbol)); if (node === symbol.valueDeclaration) { if (node.initializer && node.parent.parent.kind !== 207) { checkTypeAssignableTo(checkExpressionCached(node.initializer), type, node, undefined); @@ -30647,7 +32004,7 @@ var ts; } } else { - var declarationType = getWidenedTypeForVariableLikeDeclaration(node); + var declarationType = convertAutoToAny(getWidenedTypeForVariableLikeDeclaration(node)); if (type !== unknownType && declarationType !== unknownType && !isTypeIdenticalTo(type, declarationType)) { error(node.name, ts.Diagnostics.Subsequent_variable_declarations_must_have_the_same_type_Variable_0_must_be_of_type_1_but_here_has_type_2, ts.declarationNameToString(node.name), typeToString(type), typeToString(declarationType)); } @@ -31022,7 +32379,12 @@ var ts; } } checkExpression(node.expression); - error(node.expression, ts.Diagnostics.All_symbols_within_a_with_block_will_be_resolved_to_any); + var sourceFile = ts.getSourceFileOfNode(node); + if (!hasParseDiagnostics(sourceFile)) { + var start = ts.getSpanOfTokenAtPosition(sourceFile, node.pos).start; + var end = node.statement.pos; + grammarErrorAtPos(sourceFile, start, end - start, ts.Diagnostics.The_with_statement_is_not_supported_All_symbols_in_a_with_block_will_have_type_any); + } } function checkSwitchStatement(node) { checkGrammarStatementInAmbientContext(node); @@ -31741,9 +33103,11 @@ var ts; grammarErrorOnNode(node.name, ts.Diagnostics.Only_ambient_modules_can_use_quoted_names); } } - checkCollisionWithCapturedThisVariable(node, node.name); - checkCollisionWithRequireExportsInGeneratedCode(node, node.name); - checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); + if (ts.isIdentifier(node.name)) { + checkCollisionWithCapturedThisVariable(node, node.name); + checkCollisionWithRequireExportsInGeneratedCode(node, node.name); + checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); + } checkExportsOnMergedDeclarations(node); var symbol = getSymbolOfNode(node); if (symbol.flags & 512 @@ -31818,9 +33182,9 @@ var ts; break; case 169: case 218: - var name_24 = node.name; - if (ts.isBindingPattern(name_24)) { - for (var _b = 0, _c = name_24.elements; _b < _c.length; _b++) { + var name_25 = node.name; + if (ts.isBindingPattern(name_25)) { + for (var _b = 0, _c = name_25.elements; _b < _c.length; _b++) { var el = _c[_b]; checkModuleAugmentationElement(el, isGlobalAugmentation); } @@ -31982,9 +33346,11 @@ var ts; } } function checkGrammarModuleElementContext(node, errorMessage) { - if (node.parent.kind !== 256 && node.parent.kind !== 226 && node.parent.kind !== 225) { - return grammarErrorOnFirstToken(node, errorMessage); + var isInAppropriateContext = node.parent.kind === 256 || node.parent.kind === 226 || node.parent.kind === 225; + if (!isInAppropriateContext) { + grammarErrorOnFirstToken(node, errorMessage); } + return !isInAppropriateContext; } function checkExportSpecifier(node) { checkAliasSymbol(node); @@ -32071,7 +33437,8 @@ var ts; links.exportsChecked = true; } function isNotOverload(declaration) { - return declaration.kind !== 220 || !!declaration.body; + return (declaration.kind !== 220 && declaration.kind !== 147) || + !!declaration.body; } } function checkSourceElement(node) { @@ -32556,6 +33923,9 @@ var ts; node.parent.moduleSpecifier === node)) { return resolveExternalModuleName(node, node); } + if (ts.isInJavaScriptFile(node) && ts.isRequireCall(node.parent, false)) { + return resolveExternalModuleName(node, node); + } case 8: if (node.parent.kind === 173 && node.parent.argumentExpression === node) { var objectType = checkExpression(node.parent.expression); @@ -32670,9 +34040,9 @@ var ts; function getRootSymbols(symbol) { if (symbol.flags & 268435456) { var symbols_3 = []; - var name_25 = symbol.name; + var name_26 = symbol.name; ts.forEach(getSymbolLinks(symbol).containingType.types, function (t) { - var symbol = getPropertyOfType(t, name_25); + var symbol = getPropertyOfType(t, name_26); if (symbol) { symbols_3.push(symbol); } @@ -32979,9 +34349,9 @@ var ts; } var location = reference; if (startInDeclarationContainer) { - var parent_13 = reference.parent; - if (ts.isDeclaration(parent_13) && reference === parent_13.name) { - location = getDeclarationContainer(parent_13); + var parent_14 = reference.parent; + if (ts.isDeclaration(parent_14) && reference === parent_14.name) { + location = getDeclarationContainer(parent_14); } } return resolveName(location, reference.text, 107455 | 1048576 | 8388608, undefined, undefined); @@ -33090,9 +34460,9 @@ var ts; } var current = symbol; while (true) { - var parent_14 = getParentOfSymbol(current); - if (parent_14) { - current = parent_14; + var parent_15 = getParentOfSymbol(current); + if (parent_15) { + current = parent_15; } else { break; @@ -33652,8 +35022,8 @@ var ts; function checkGrammarForOmittedArgument(node, args) { if (args) { var sourceFile = ts.getSourceFileOfNode(node); - for (var _i = 0, args_2 = args; _i < args_2.length; _i++) { - var arg = args_2[_i]; + for (var _i = 0, args_4 = args; _i < args_4.length; _i++) { + var arg = args_4[_i]; if (arg.kind === 193) { return grammarErrorAtPos(sourceFile, arg.pos, 0, ts.Diagnostics.Argument_expression_expected); } @@ -33761,10 +35131,9 @@ var ts; var GetOrSetAccessor = GetAccessor | SetAccessor; for (var _i = 0, _a = node.properties; _i < _a.length; _i++) { var prop = _a[_i]; - var name_26 = prop.name; - if (prop.kind === 193 || - name_26.kind === 140) { - checkGrammarComputedPropertyName(name_26); + var name_27 = prop.name; + if (name_27.kind === 140) { + checkGrammarComputedPropertyName(name_27); } if (prop.kind === 254 && !inDestructuring && prop.objectAssignmentInitializer) { return grammarErrorOnNode(prop.equalsToken, ts.Diagnostics.can_only_be_used_in_an_object_literal_property_inside_a_destructuring_assignment); @@ -33780,8 +35149,8 @@ var ts; var currentKind = void 0; if (prop.kind === 253 || prop.kind === 254) { checkGrammarForInvalidQuestionMark(prop, prop.questionToken, ts.Diagnostics.An_object_member_cannot_be_declared_optional); - if (name_26.kind === 8) { - checkGrammarNumericLiteral(name_26); + if (name_27.kind === 8) { + checkGrammarNumericLiteral(name_27); } currentKind = Property; } @@ -33797,7 +35166,7 @@ var ts; else { ts.Debug.fail("Unexpected syntax kind:" + prop.kind); } - var effectiveName = ts.getPropertyNameForPropertyNameNode(name_26); + var effectiveName = ts.getPropertyNameForPropertyNameNode(name_27); if (effectiveName === undefined) { continue; } @@ -33807,18 +35176,18 @@ var ts; else { var existingKind = seen[effectiveName]; if (currentKind === Property && existingKind === Property) { - grammarErrorOnNode(name_26, ts.Diagnostics.Duplicate_identifier_0, ts.getTextOfNode(name_26)); + grammarErrorOnNode(name_27, ts.Diagnostics.Duplicate_identifier_0, ts.getTextOfNode(name_27)); } else if ((currentKind & GetOrSetAccessor) && (existingKind & GetOrSetAccessor)) { if (existingKind !== GetOrSetAccessor && currentKind !== existingKind) { seen[effectiveName] = currentKind | existingKind; } else { - return grammarErrorOnNode(name_26, ts.Diagnostics.An_object_literal_cannot_have_multiple_get_Slashset_accessors_with_the_same_name); + return grammarErrorOnNode(name_27, ts.Diagnostics.An_object_literal_cannot_have_multiple_get_Slashset_accessors_with_the_same_name); } } else { - return grammarErrorOnNode(name_26, ts.Diagnostics.An_object_literal_cannot_have_property_and_accessor_with_the_same_name); + return grammarErrorOnNode(name_27, ts.Diagnostics.An_object_literal_cannot_have_property_and_accessor_with_the_same_name); } } } @@ -33831,12 +35200,12 @@ var ts; continue; } var jsxAttr = attr; - var name_27 = jsxAttr.name; - if (!seen[name_27.text]) { - seen[name_27.text] = true; + var name_28 = jsxAttr.name; + if (!seen[name_28.text]) { + seen[name_28.text] = true; } else { - return grammarErrorOnNode(name_27, ts.Diagnostics.JSX_elements_cannot_have_multiple_attributes_with_the_same_name); + return grammarErrorOnNode(name_28, ts.Diagnostics.JSX_elements_cannot_have_multiple_attributes_with_the_same_name); } var initializer = jsxAttr.initializer; if (initializer && initializer.kind === 248 && !initializer.expression) { @@ -33919,17 +35288,8 @@ var ts; return getAccessorThisParameter(accessor) || accessor.parameters.length === (accessor.kind === 149 ? 0 : 1); } function getAccessorThisParameter(accessor) { - if (accessor.parameters.length === (accessor.kind === 149 ? 1 : 2) && - accessor.parameters[0].name.kind === 69 && - accessor.parameters[0].name.originalKeywordKind === 97) { - return accessor.parameters[0]; - } - } - function getFunctionLikeThisParameter(func) { - if (func.parameters.length && - func.parameters[0].name.kind === 69 && - func.parameters[0].name.originalKeywordKind === 97) { - return func.parameters[0]; + if (accessor.parameters.length === (accessor.kind === 149 ? 1 : 2)) { + return ts.getThisParameter(accessor); } } function checkGrammarForNonSymbolComputedProperty(node, message) { @@ -34763,7 +36123,7 @@ var ts; case 175: return ts.updateNew(node, visitNode(node.expression, visitor, ts.isExpression), visitNodes(node.typeArguments, visitor, ts.isTypeNode), visitNodes(node.arguments, visitor, ts.isExpression)); case 176: - return ts.updateTaggedTemplate(node, visitNode(node.tag, visitor, ts.isExpression), visitNode(node.template, visitor, ts.isTemplate)); + return ts.updateTaggedTemplate(node, visitNode(node.tag, visitor, ts.isExpression), visitNode(node.template, visitor, ts.isTemplateLiteral)); case 178: return ts.updateParen(node, visitNode(node.expression, visitor, ts.isExpression)); case 179: @@ -34787,7 +36147,7 @@ var ts; case 188: return ts.updateConditional(node, visitNode(node.condition, visitor, ts.isExpression), visitNode(node.whenTrue, visitor, ts.isExpression), visitNode(node.whenFalse, visitor, ts.isExpression)); case 189: - return ts.updateTemplateExpression(node, visitNode(node.head, visitor, ts.isTemplateLiteralFragment), visitNodes(node.templateSpans, visitor, ts.isTemplateSpan)); + return ts.updateTemplateExpression(node, visitNode(node.head, visitor, ts.isTemplateHead), visitNodes(node.templateSpans, visitor, ts.isTemplateSpan)); case 190: return ts.updateYield(node, visitNode(node.expression, visitor, ts.isExpression)); case 191: @@ -34797,7 +36157,7 @@ var ts; case 194: return ts.updateExpressionWithTypeArguments(node, visitNodes(node.typeArguments, visitor, ts.isTypeNode), visitNode(node.expression, visitor, ts.isExpression)); case 197: - return ts.updateTemplateSpan(node, visitNode(node.expression, visitor, ts.isExpression), visitNode(node.literal, visitor, ts.isTemplateLiteralFragment)); + return ts.updateTemplateSpan(node, visitNode(node.expression, visitor, ts.isExpression), visitNode(node.literal, visitor, ts.isTemplateMiddleOrTemplateTail)); case 199: return ts.updateBlock(node, visitNodes(node.statements, visitor, ts.isStatement)); case 200: @@ -35072,7 +36432,7 @@ var ts; return expression; function emitAssignment(name, value, location) { var expression = ts.createAssignment(name, value, location); - context.setNodeEmitFlags(expression, 2048); + ts.setEmitFlags(expression, 2048); ts.aggregateTransformFlags(expression); expressions.push(expression); } @@ -35089,7 +36449,7 @@ var ts; return declarations; function emitAssignment(name, value, location) { var declaration = ts.createVariableDeclaration(name, undefined, value, location); - context.setNodeEmitFlags(declaration, 2048); + ts.setEmitFlags(declaration, 2048); ts.aggregateTransformFlags(declaration); declarations.push(declaration); } @@ -35113,7 +36473,7 @@ var ts; } var declaration = ts.createVariableDeclaration(name, undefined, value, location); declaration.original = original; - context.setNodeEmitFlags(declaration, 2048); + ts.setEmitFlags(declaration, 2048); declarations.push(declaration); ts.aggregateTransformFlags(declaration); } @@ -35153,7 +36513,7 @@ var ts; function emitPendingAssignment(name, value, location, original) { var expression = ts.createAssignment(name, value, location); expression.original = original; - context.setNodeEmitFlags(expression, 2048); + ts.setEmitFlags(expression, 2048); pendingAssignments.push(expression); return expression; } @@ -35197,10 +36557,10 @@ var ts; emitArrayLiteralAssignment(target, value, location); } else { - var name_28 = ts.getMutableClone(target); - context.setSourceMapRange(name_28, target); - context.setCommentRange(name_28, target); - emitAssignment(name_28, value, location, undefined); + var name_29 = ts.getMutableClone(target); + ts.setSourceMapRange(name_29, target); + ts.setCommentRange(name_29, target); + emitAssignment(name_29, value, location, undefined); } } function emitObjectLiteralAssignment(target, value, location) { @@ -35314,7 +36674,7 @@ var ts; (function (ts) { var USE_NEW_TYPE_METADATA_FORMAT = false; function transformTypeScript(context) { - var getNodeEmitFlags = context.getNodeEmitFlags, setNodeEmitFlags = context.setNodeEmitFlags, setCommentRange = context.setCommentRange, setSourceMapRange = context.setSourceMapRange, startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration; + var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration; var resolver = context.getEmitResolver(); var compilerOptions = context.getCompilerOptions(); var languageVersion = ts.getEmitScriptTarget(compilerOptions); @@ -35323,10 +36683,13 @@ var ts; var previousOnSubstituteNode = context.onSubstituteNode; context.onEmitNode = onEmitNode; context.onSubstituteNode = onSubstituteNode; + context.enableSubstitution(172); + context.enableSubstitution(173); var currentSourceFile; var currentNamespace; var currentNamespaceContainerName; var currentScope; + var currentScopeFirstDeclarationsOfName; var currentSourceFileExternalHelpersModuleName; var enabledSubstitutions; var classAliases; @@ -35334,12 +36697,19 @@ var ts; var currentSuperContainer; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } return ts.visitNode(node, visitor, ts.isSourceFile); } function saveStateAndInvoke(node, f) { var savedCurrentScope = currentScope; + var savedCurrentScopeFirstDeclarationsOfName = currentScopeFirstDeclarationsOfName; onBeforeVisitNode(node); var visited = f(node); + if (currentScope !== savedCurrentScope) { + currentScopeFirstDeclarationsOfName = savedCurrentScopeFirstDeclarationsOfName; + } currentScope = savedCurrentScope; return visited; } @@ -35493,11 +36863,22 @@ var ts; case 226: case 199: currentScope = node; + currentScopeFirstDeclarationsOfName = undefined; + break; + case 221: + case 220: + if (ts.hasModifier(node, 2)) { + break; + } + recordEmittedDeclarationInScope(node); break; } } function visitSourceFile(node) { currentSourceFile = node; + if (compilerOptions.alwaysStrict) { + node = ts.ensureUseStrict(node); + } if (node.flags & 31744 && compilerOptions.importHelpers && (ts.isExternalModule(node) || compilerOptions.isolatedModules)) { @@ -35519,7 +36900,7 @@ var ts; else { node = ts.visitEachChild(node, visitor, context); } - setNodeEmitFlags(node, 1 | node.emitFlags); + ts.setEmitFlags(node, 1 | ts.getEmitFlags(node)); return node; } function shouldEmitDecorateCallForClass(node) { @@ -35549,7 +36930,7 @@ var ts; var classDeclaration = ts.createClassDeclaration(undefined, ts.visitNodes(node.modifiers, visitor, ts.isModifier), name, undefined, ts.visitNodes(node.heritageClauses, visitor, ts.isHeritageClause), transformClassMembers(node, hasExtendsClause), node); ts.setOriginalNode(classDeclaration, node); if (staticProperties.length > 0) { - setNodeEmitFlags(classDeclaration, 1024 | getNodeEmitFlags(classDeclaration)); + ts.setEmitFlags(classDeclaration, 1024 | ts.getEmitFlags(classDeclaration)); } statements.push(classDeclaration); } @@ -35591,7 +36972,7 @@ var ts; var transformedClassExpression = ts.createVariableStatement(undefined, ts.createLetDeclarationList([ ts.createVariableDeclaration(classAlias || declaredName, undefined, classExpression) ]), location); - setCommentRange(transformedClassExpression, node); + ts.setCommentRange(transformedClassExpression, node); statements.push(ts.setOriginalNode(transformedClassExpression, node)); if (classAlias) { statements.push(ts.setOriginalNode(ts.createVariableStatement(undefined, ts.createLetDeclarationList([ @@ -35612,7 +36993,7 @@ var ts; enableSubstitutionForClassAliases(); classAliases[ts.getOriginalNodeId(node)] = ts.getSynthesizedClone(temp); } - setNodeEmitFlags(classExpression, 524288 | getNodeEmitFlags(classExpression)); + ts.setEmitFlags(classExpression, 524288 | ts.getEmitFlags(classExpression)); expressions.push(ts.startOnNewLine(ts.createAssignment(temp, classExpression))); ts.addRange(expressions, generateInitializedPropertyExpressions(node, staticProperties, temp)); expressions.push(ts.startOnNewLine(temp)); @@ -35673,7 +37054,7 @@ var ts; return index; } var statement = statements[index]; - if (statement.kind === 202 && ts.isSuperCallExpression(statement.expression)) { + if (statement.kind === 202 && ts.isSuperCall(statement.expression)) { result.push(ts.visitNode(statement, visitor, ts.isStatement)); return index + 1; } @@ -35692,9 +37073,9 @@ var ts; ts.Debug.assert(ts.isIdentifier(node.name)); var name = node.name; var propertyName = ts.getMutableClone(name); - setNodeEmitFlags(propertyName, 49152 | 1536); + ts.setEmitFlags(propertyName, 49152 | 1536); var localName = ts.getMutableClone(name); - setNodeEmitFlags(localName, 49152); + ts.setEmitFlags(localName, 49152); return ts.startOnNewLine(ts.createStatement(ts.createAssignment(ts.createPropertyAccess(ts.createThis(), propertyName, node.name), localName), ts.moveRangePos(node, -1))); } function getInitializedProperties(node, isStatic) { @@ -35715,8 +37096,8 @@ var ts; for (var _i = 0, properties_7 = properties; _i < properties_7.length; _i++) { var property = properties_7[_i]; var statement = ts.createStatement(transformInitializedProperty(node, property, receiver)); - setSourceMapRange(statement, ts.moveRangePastModifiers(property)); - setCommentRange(statement, property); + ts.setSourceMapRange(statement, ts.moveRangePastModifiers(property)); + ts.setCommentRange(statement, property); statements.push(statement); } } @@ -35726,8 +37107,8 @@ var ts; var property = properties_8[_i]; var expression = transformInitializedProperty(node, property, receiver); expression.startsOnNewLine = true; - setSourceMapRange(expression, ts.moveRangePastModifiers(property)); - setCommentRange(expression, property); + ts.setSourceMapRange(expression, ts.moveRangePastModifiers(property)); + ts.setCommentRange(expression, property); expressions.push(expression); } return expressions; @@ -35868,7 +37249,7 @@ var ts; : ts.createNull() : undefined; var helper = ts.createDecorateHelper(currentSourceFileExternalHelpersModuleName, decoratorExpressions, prefix, memberName, descriptor, ts.moveRangePastDecorators(member)); - setNodeEmitFlags(helper, 49152); + ts.setEmitFlags(helper, 49152); return helper; } function addConstructorDecorationStatement(statements, node, decoratedClassAlias) { @@ -35886,12 +37267,12 @@ var ts; if (decoratedClassAlias) { var expression = ts.createAssignment(decoratedClassAlias, ts.createDecorateHelper(currentSourceFileExternalHelpersModuleName, decoratorExpressions, getDeclarationName(node))); var result = ts.createAssignment(getDeclarationName(node), expression, ts.moveRangePastDecorators(node)); - setNodeEmitFlags(result, 49152); + ts.setEmitFlags(result, 49152); return result; } else { var result = ts.createAssignment(getDeclarationName(node), ts.createDecorateHelper(currentSourceFileExternalHelpersModuleName, decoratorExpressions, getDeclarationName(node)), ts.moveRangePastDecorators(node)); - setNodeEmitFlags(result, 49152); + ts.setEmitFlags(result, 49152); return result; } } @@ -35905,7 +37286,7 @@ var ts; for (var _i = 0, decorators_1 = decorators; _i < decorators_1.length; _i++) { var decorator = decorators_1[_i]; var helper = ts.createParamHelper(currentSourceFileExternalHelpersModuleName, transformDecorator(decorator), parameterOffset, decorator.expression); - setNodeEmitFlags(helper, 49152); + ts.setEmitFlags(helper, 49152); expressions.push(helper); } } @@ -36070,10 +37451,33 @@ var ts; : ts.createIdentifier("Symbol"); case 155: return serializeTypeReferenceNode(node); + case 163: + case 162: + { + var unionOrIntersection = node; + var serializedUnion = void 0; + for (var _i = 0, _a = unionOrIntersection.types; _i < _a.length; _i++) { + var typeNode = _a[_i]; + var serializedIndividual = serializeTypeNode(typeNode); + if (serializedIndividual.kind !== 69) { + serializedUnion = undefined; + break; + } + if (serializedIndividual.text === "Object") { + return serializedIndividual; + } + if (serializedUnion && serializedUnion.text !== serializedIndividual.text) { + serializedUnion = undefined; + break; + } + serializedUnion = serializedIndividual; + } + if (serializedUnion) { + return serializedUnion; + } + } case 158: case 159: - case 162: - case 163: case 117: case 165: break; @@ -36117,14 +37521,14 @@ var ts; function serializeEntityNameAsExpression(node, useFallback) { switch (node.kind) { case 69: - var name_29 = ts.getMutableClone(node); - name_29.flags &= ~8; - name_29.original = undefined; - name_29.parent = currentScope; + var name_30 = ts.getMutableClone(node); + name_30.flags &= ~8; + name_30.original = undefined; + name_30.parent = currentScope; if (useFallback) { - return ts.createLogicalAnd(ts.createStrictInequality(ts.createTypeOf(name_29), ts.createLiteral("undefined")), name_29); + return ts.createLogicalAnd(ts.createStrictInequality(ts.createTypeOf(name_30), ts.createLiteral("undefined")), name_30); } - return name_29; + return name_30; case 139: return serializeQualifiedNameAsExpression(node, useFallback); } @@ -36194,8 +37598,8 @@ var ts; return undefined; } var method = ts.createMethod(undefined, ts.visitNodes(node.modifiers, visitor, ts.isModifier), node.asteriskToken, visitPropertyNameOfClassElement(node), undefined, ts.visitNodes(node.parameters, visitor, ts.isParameter), undefined, transformFunctionBody(node), node); - setCommentRange(method, node); - setSourceMapRange(method, ts.moveRangePastDecorators(node)); + ts.setCommentRange(method, node); + ts.setSourceMapRange(method, ts.moveRangePastDecorators(node)); ts.setOriginalNode(method, node); return method; } @@ -36207,8 +37611,8 @@ var ts; return undefined; } var accessor = ts.createGetAccessor(undefined, ts.visitNodes(node.modifiers, visitor, ts.isModifier), visitPropertyNameOfClassElement(node), ts.visitNodes(node.parameters, visitor, ts.isParameter), undefined, node.body ? ts.visitEachChild(node.body, visitor, context) : ts.createBlock([]), node); - setCommentRange(accessor, node); - setSourceMapRange(accessor, ts.moveRangePastDecorators(node)); + ts.setCommentRange(accessor, node); + ts.setSourceMapRange(accessor, ts.moveRangePastDecorators(node)); ts.setOriginalNode(accessor, node); return accessor; } @@ -36217,8 +37621,8 @@ var ts; return undefined; } var accessor = ts.createSetAccessor(undefined, ts.visitNodes(node.modifiers, visitor, ts.isModifier), visitPropertyNameOfClassElement(node), ts.visitNodes(node.parameters, visitor, ts.isParameter), node.body ? ts.visitEachChild(node.body, visitor, context) : ts.createBlock([]), node); - setCommentRange(accessor, node); - setSourceMapRange(accessor, ts.moveRangePastDecorators(node)); + ts.setCommentRange(accessor, node); + ts.setSourceMapRange(accessor, ts.moveRangePastDecorators(node)); ts.setOriginalNode(accessor, node); return accessor; } @@ -36313,11 +37717,11 @@ var ts; if (languageVersion >= 2) { if (resolver.getNodeCheckFlags(node) & 4096) { enableSubstitutionForAsyncMethodsWithSuper(); - setNodeEmitFlags(block, 8); + ts.setEmitFlags(block, 8); } else if (resolver.getNodeCheckFlags(node) & 2048) { enableSubstitutionForAsyncMethodsWithSuper(); - setNodeEmitFlags(block, 4); + ts.setEmitFlags(block, 4); } } return block; @@ -36327,14 +37731,14 @@ var ts; } } function visitParameter(node) { - if (node.name && ts.isIdentifier(node.name) && node.name.originalKeywordKind === 97) { + if (ts.parameterIsThisKeyword(node)) { return undefined; } var parameter = ts.createParameterDeclaration(undefined, undefined, node.dotDotDotToken, ts.visitNode(node.name, visitor, ts.isBindingName), undefined, undefined, ts.visitNode(node.initializer, visitor, ts.isExpression), ts.moveRangePastModifiers(node)); ts.setOriginalNode(parameter, node); - setCommentRange(parameter, node); - setSourceMapRange(parameter, ts.moveRangePastModifiers(node)); - setNodeEmitFlags(parameter.name, 1024); + ts.setCommentRange(parameter, node); + ts.setSourceMapRange(parameter, ts.moveRangePastModifiers(node)); + ts.setEmitFlags(parameter.name, 1024); return parameter; } function visitVariableStatement(node) { @@ -36383,12 +37787,13 @@ var ts; || compilerOptions.isolatedModules; } function shouldEmitVarForEnumDeclaration(node) { - return !ts.hasModifier(node, 1) - || (isES6ExportedDeclaration(node) && ts.isFirstDeclarationOfKind(node, node.kind)); + return isFirstEmittedDeclarationInScope(node) + && (!ts.hasModifier(node, 1) + || isES6ExportedDeclaration(node)); } function addVarForEnumExportedFromNamespace(statements, node) { var statement = ts.createVariableStatement(undefined, [ts.createVariableDeclaration(getDeclarationName(node), undefined, getExportName(node))]); - setSourceMapRange(statement, node); + ts.setSourceMapRange(statement, node); statements.push(statement); } function visitEnumDeclaration(node) { @@ -36397,6 +37802,7 @@ var ts; } var statements = []; var emitFlags = 64; + recordEmittedDeclarationInScope(node); if (shouldEmitVarForEnumDeclaration(node)) { addVarForEnumOrModuleDeclaration(statements, node); if (moduleKind !== ts.ModuleKind.System || currentScope !== currentSourceFile) { @@ -36408,7 +37814,7 @@ var ts; var exportName = getExportName(node); var enumStatement = ts.createStatement(ts.createCall(ts.createFunctionExpression(undefined, undefined, undefined, [ts.createParameter(parameterName)], undefined, transformEnumBody(node, containerName)), undefined, [ts.createLogicalOr(exportName, ts.createAssignment(exportName, ts.createObjectLiteral()))]), node); ts.setOriginalNode(enumStatement, node); - setNodeEmitFlags(enumStatement, emitFlags); + ts.setEmitFlags(enumStatement, emitFlags); statements.push(enumStatement); if (isNamespaceExport(node)) { addVarForEnumExportedFromNamespace(statements, node); @@ -36447,18 +37853,32 @@ var ts; function shouldEmitModuleDeclaration(node) { return ts.isInstantiatedModule(node, compilerOptions.preserveConstEnums || compilerOptions.isolatedModules); } - function isModuleMergedWithES6Class(node) { - return languageVersion === 2 - && ts.isMergedWithClass(node); - } function isES6ExportedDeclaration(node) { return isExternalModuleExport(node) && moduleKind === ts.ModuleKind.ES6; } + function recordEmittedDeclarationInScope(node) { + var name = node.symbol && node.symbol.name; + if (name) { + if (!currentScopeFirstDeclarationsOfName) { + currentScopeFirstDeclarationsOfName = ts.createMap(); + } + if (!(name in currentScopeFirstDeclarationsOfName)) { + currentScopeFirstDeclarationsOfName[name] = node; + } + } + } + function isFirstEmittedDeclarationInScope(node) { + if (currentScopeFirstDeclarationsOfName) { + var name_31 = node.symbol && node.symbol.name; + if (name_31) { + return currentScopeFirstDeclarationsOfName[name_31] === node; + } + } + return false; + } function shouldEmitVarForModuleDeclaration(node) { - return !isModuleMergedWithES6Class(node) - && (!isES6ExportedDeclaration(node) - || ts.isFirstDeclarationOfKind(node, node.kind)); + return isFirstEmittedDeclarationInScope(node); } function addVarForEnumOrModuleDeclaration(statements, node) { var statement = ts.createVariableStatement(isES6ExportedDeclaration(node) @@ -36468,13 +37888,13 @@ var ts; ]); ts.setOriginalNode(statement, node); if (node.kind === 224) { - setSourceMapRange(statement.declarationList, node); + ts.setSourceMapRange(statement.declarationList, node); } else { - setSourceMapRange(statement, node); + ts.setSourceMapRange(statement, node); } - setCommentRange(statement, node); - setNodeEmitFlags(statement, 32768); + ts.setCommentRange(statement, node); + ts.setEmitFlags(statement, 32768); statements.push(statement); } function visitModuleDeclaration(node) { @@ -36485,6 +37905,7 @@ var ts; enableSubstitutionForNamespaceExports(); var statements = []; var emitFlags = 64; + recordEmittedDeclarationInScope(node); if (shouldEmitVarForModuleDeclaration(node)) { addVarForEnumOrModuleDeclaration(statements, node); if (moduleKind !== ts.ModuleKind.System || currentScope !== currentSourceFile) { @@ -36501,15 +37922,17 @@ var ts; } var moduleStatement = ts.createStatement(ts.createCall(ts.createFunctionExpression(undefined, undefined, undefined, [ts.createParameter(parameterName)], undefined, transformModuleBody(node, containerName)), undefined, [moduleArg]), node); ts.setOriginalNode(moduleStatement, node); - setNodeEmitFlags(moduleStatement, emitFlags); + ts.setEmitFlags(moduleStatement, emitFlags); statements.push(moduleStatement); return statements; } function transformModuleBody(node, namespaceLocalName) { var savedCurrentNamespaceContainerName = currentNamespaceContainerName; var savedCurrentNamespace = currentNamespace; + var savedCurrentScopeFirstDeclarationsOfName = currentScopeFirstDeclarationsOfName; currentNamespaceContainerName = namespaceLocalName; currentNamespace = node; + currentScopeFirstDeclarationsOfName = undefined; var statements = []; startLexicalEnvironment(); var statementsLocation; @@ -36536,9 +37959,10 @@ var ts; ts.addRange(statements, endLexicalEnvironment()); currentNamespaceContainerName = savedCurrentNamespaceContainerName; currentNamespace = savedCurrentNamespace; + currentScopeFirstDeclarationsOfName = savedCurrentScopeFirstDeclarationsOfName; var block = ts.createBlock(ts.createNodeArray(statements, statementsLocation), blockLocation, true); if (body.kind !== 226) { - setNodeEmitFlags(block, block.emitFlags | 49152); + ts.setEmitFlags(block, ts.getEmitFlags(block) | 49152); } return block; } @@ -36561,7 +37985,7 @@ var ts; return undefined; } var moduleReference = ts.createExpressionFromEntityName(node.moduleReference); - setNodeEmitFlags(moduleReference, 49152 | 65536); + ts.setEmitFlags(moduleReference, 49152 | 65536); if (isNamedExternalModuleExport(node) || !isNamespaceExport(node)) { return ts.setOriginalNode(ts.createVariableStatement(ts.visitNodes(node.modifiers, visitor, ts.isModifier), ts.createVariableDeclarationList([ ts.createVariableDeclaration(node.name, undefined, moduleReference) @@ -36590,9 +38014,9 @@ var ts; } function addExportMemberAssignment(statements, node) { var expression = ts.createAssignment(getExportName(node), getLocalName(node, true)); - setSourceMapRange(expression, ts.createRange(node.name.pos, node.end)); + ts.setSourceMapRange(expression, ts.createRange(node.name.pos, node.end)); var statement = ts.createStatement(expression); - setSourceMapRange(statement, ts.createRange(-1, node.end)); + ts.setSourceMapRange(statement, ts.createRange(-1, node.end)); statements.push(statement); } function createNamespaceExport(exportName, exportValue, location) { @@ -36613,7 +38037,7 @@ var ts; emitFlags |= 1536; } if (emitFlags) { - setNodeEmitFlags(qualifiedName, emitFlags); + ts.setEmitFlags(qualifiedName, emitFlags); } return qualifiedName; } @@ -36622,7 +38046,7 @@ var ts; } function getNamespaceParameterName(node) { var name = ts.getGeneratedNameForNode(node); - setSourceMapRange(name, node.name); + ts.setSourceMapRange(name, node.name); return name; } function getNamespaceContainerName(node) { @@ -36639,8 +38063,8 @@ var ts; } function getDeclarationName(node, allowComments, allowSourceMaps, emitFlags) { if (node.name) { - var name_30 = ts.getMutableClone(node.name); - emitFlags |= getNodeEmitFlags(node.name); + var name_32 = ts.getMutableClone(node.name); + emitFlags |= ts.getEmitFlags(node.name); if (!allowSourceMaps) { emitFlags |= 1536; } @@ -36648,9 +38072,9 @@ var ts; emitFlags |= 49152; } if (emitFlags) { - setNodeEmitFlags(name_30, emitFlags); + ts.setEmitFlags(name_32, emitFlags); } - return name_30; + return name_32; } else { return ts.getGeneratedNameForNode(node); @@ -36712,7 +38136,7 @@ var ts; function isTransformedEnumDeclaration(node) { return ts.getOriginalNode(node).kind === 224; } - function onEmitNode(node, emit) { + function onEmitNode(emitContext, node, emitCallback) { var savedApplicableSubstitutions = applicableSubstitutions; var savedCurrentSuperContainer = currentSuperContainer; if (enabledSubstitutions & 4 && isSuperContainer(node)) { @@ -36724,13 +38148,13 @@ var ts; if (enabledSubstitutions & 8 && isTransformedEnumDeclaration(node)) { applicableSubstitutions |= 8; } - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); applicableSubstitutions = savedApplicableSubstitutions; currentSuperContainer = savedCurrentSuperContainer; } - function onSubstituteNode(node, isExpression) { - node = previousOnSubstituteNode(node, isExpression); - if (isExpression) { + function onSubstituteNode(emitContext, node) { + node = previousOnSubstituteNode(emitContext, node); + if (emitContext === 1) { return substituteExpression(node); } else if (ts.isShorthandPropertyAssignment(node)) { @@ -36740,14 +38164,14 @@ var ts; } function substituteShorthandPropertyAssignment(node) { if (enabledSubstitutions & 2) { - var name_31 = node.name; - var exportedName = trySubstituteNamespaceExportedName(name_31); + var name_33 = node.name; + var exportedName = trySubstituteNamespaceExportedName(name_33); if (exportedName) { if (node.objectAssignmentInitializer) { var initializer = ts.createAssignment(exportedName, node.objectAssignmentInitializer); - return ts.createPropertyAssignment(name_31, initializer, node); + return ts.createPropertyAssignment(name_33, initializer, node); } - return ts.createPropertyAssignment(name_31, exportedName, node); + return ts.createPropertyAssignment(name_33, exportedName, node); } } return node; @@ -36756,16 +38180,15 @@ var ts; switch (node.kind) { case 69: return substituteExpressionIdentifier(node); - } - if (enabledSubstitutions & 4) { - switch (node.kind) { - case 174: + case 172: + return substitutePropertyAccessExpression(node); + case 173: + return substituteElementAccessExpression(node); + case 174: + if (enabledSubstitutions & 4) { return substituteCallExpression(node); - case 172: - return substitutePropertyAccessExpression(node); - case 173: - return substituteElementAccessExpression(node); - } + } + break; } return node; } @@ -36782,8 +38205,8 @@ var ts; var classAlias = classAliases[declaration.id]; if (classAlias) { var clone_4 = ts.getSynthesizedClone(classAlias); - setSourceMapRange(clone_4, node); - setCommentRange(clone_4, node); + ts.setSourceMapRange(clone_4, node); + ts.setCommentRange(clone_4, node); return clone_4; } } @@ -36792,7 +38215,7 @@ var ts; return undefined; } function trySubstituteNamespaceExportedName(node) { - if (enabledSubstitutions & applicableSubstitutions && (getNodeEmitFlags(node) & 262144) === 0) { + if (enabledSubstitutions & applicableSubstitutions && (ts.getEmitFlags(node) & 262144) === 0) { var container = resolver.getReferencedExportContainer(node, false); if (container) { var substitute = (applicableSubstitutions & 2 && container.kind === 225) || @@ -36820,23 +38243,48 @@ var ts; return node; } function substitutePropertyAccessExpression(node) { - if (node.expression.kind === 95) { + if (enabledSubstitutions & 4 && node.expression.kind === 95) { var flags = getSuperContainerAsyncMethodFlags(); if (flags) { return createSuperAccessInAsyncMethod(ts.createLiteral(node.name.text), flags, node); } } - return node; + return substituteConstantValue(node); } function substituteElementAccessExpression(node) { - if (node.expression.kind === 95) { + if (enabledSubstitutions & 4 && node.expression.kind === 95) { var flags = getSuperContainerAsyncMethodFlags(); if (flags) { return createSuperAccessInAsyncMethod(node.argumentExpression, flags, node); } } + return substituteConstantValue(node); + } + function substituteConstantValue(node) { + var constantValue = tryGetConstEnumValue(node); + if (constantValue !== undefined) { + var substitute = ts.createLiteral(constantValue); + ts.setSourceMapRange(substitute, node); + ts.setCommentRange(substitute, node); + if (!compilerOptions.removeComments) { + var propertyName = ts.isPropertyAccessExpression(node) + ? ts.declarationNameToString(node.name) + : ts.getTextOfNode(node.argumentExpression); + substitute.trailingComment = " " + propertyName + " "; + } + ts.setConstantValue(node, constantValue); + return substitute; + } return node; } + function tryGetConstEnumValue(node) { + if (compilerOptions.isolatedModules) { + return undefined; + } + return ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node) + ? resolver.getConstantValue(node) + : undefined; + } function createSuperAccessInAsyncMethod(argumentExpression, flags, location) { if (flags & 4096) { return ts.createPropertyAccess(ts.createCall(ts.createIdentifier("_super"), undefined, [argumentExpression]), "value", location); @@ -36860,6 +38308,9 @@ var ts; var currentSourceFile; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } currentSourceFile = node; node = ts.visitEachChild(node, visitor, context); currentSourceFile = undefined; @@ -37018,12 +38469,12 @@ var ts; return getTagName(node.openingElement); } else { - var name_32 = node.tagName; - if (ts.isIdentifier(name_32) && ts.isIntrinsicJsxName(name_32.text)) { - return ts.createLiteral(name_32.text); + var name_34 = node.tagName; + if (ts.isIdentifier(name_34) && ts.isIntrinsicJsxName(name_34.text)) { + return ts.createLiteral(name_34.text); } else { - return ts.createExpressionFromEntityName(name_32); + return ts.createExpressionFromEntityName(name_34); } } } @@ -37305,6 +38756,9 @@ var ts; var hoistVariableDeclaration = context.hoistVariableDeclaration; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } return ts.visitEachChild(node, visitor, context); } function visitor(node) { @@ -37364,7 +38818,7 @@ var ts; var ts; (function (ts) { function transformES6(context) { - var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration, getNodeEmitFlags = context.getNodeEmitFlags, setNodeEmitFlags = context.setNodeEmitFlags, getCommentRange = context.getCommentRange, setCommentRange = context.setCommentRange, getSourceMapRange = context.getSourceMapRange, setSourceMapRange = context.setSourceMapRange, setTokenSourceMapRange = context.setTokenSourceMapRange; + var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration; var resolver = context.getEmitResolver(); var previousOnSubstituteNode = context.onSubstituteNode; var previousOnEmitNode = context.onEmitNode; @@ -37384,6 +38838,9 @@ var ts; var enabledSubstitutions; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } currentSourceFile = node; currentText = node.text; return ts.visitNode(node, visitor, ts.isSourceFile); @@ -37553,7 +39010,7 @@ var ts; enclosingFunction = currentNode; if (currentNode.kind !== 180) { enclosingNonArrowFunction = currentNode; - if (!(currentNode.emitFlags & 2097152)) { + if (!(ts.getEmitFlags(currentNode) & 2097152)) { enclosingNonAsyncFunctionBody = currentNode; } } @@ -37690,15 +39147,15 @@ var ts; } var extendsClauseElement = ts.getClassExtendsHeritageClauseElement(node); var classFunction = ts.createFunctionExpression(undefined, undefined, undefined, extendsClauseElement ? [ts.createParameter("_super")] : [], undefined, transformClassBody(node, extendsClauseElement)); - if (getNodeEmitFlags(node) & 524288) { - setNodeEmitFlags(classFunction, 524288); + if (ts.getEmitFlags(node) & 524288) { + ts.setEmitFlags(classFunction, 524288); } var inner = ts.createPartiallyEmittedExpression(classFunction); inner.end = node.end; - setNodeEmitFlags(inner, 49152); + ts.setEmitFlags(inner, 49152); var outer = ts.createPartiallyEmittedExpression(inner); outer.end = ts.skipTrivia(currentText, node.pos); - setNodeEmitFlags(outer, 49152); + ts.setEmitFlags(outer, 49152); return ts.createParen(ts.createCall(outer, undefined, extendsClauseElement ? [ts.visitNode(extendsClauseElement.expression, visitor, ts.isExpression)] : [])); @@ -37713,14 +39170,14 @@ var ts; var localName = getLocalName(node); var outer = ts.createPartiallyEmittedExpression(localName); outer.end = closingBraceLocation.end; - setNodeEmitFlags(outer, 49152); + ts.setEmitFlags(outer, 49152); var statement = ts.createReturn(outer); statement.pos = closingBraceLocation.pos; - setNodeEmitFlags(statement, 49152 | 12288); + ts.setEmitFlags(statement, 49152 | 12288); statements.push(statement); ts.addRange(statements, endLexicalEnvironment()); var block = ts.createBlock(ts.createNodeArray(statements, node.members), undefined, true); - setNodeEmitFlags(block, 49152); + ts.setEmitFlags(block, 49152); return block; } function addExtendsHelperIfNeeded(statements, node, extendsClauseElement) { @@ -37731,7 +39188,11 @@ var ts; function addConstructor(statements, node, extendsClauseElement) { var constructor = ts.getFirstConstructorWithBody(node); var hasSynthesizedSuper = hasSynthesizedDefaultSuperCall(constructor, extendsClauseElement !== undefined); - statements.push(ts.createFunctionDeclaration(undefined, undefined, undefined, getDeclarationName(node), undefined, transformConstructorParameters(constructor, hasSynthesizedSuper), undefined, transformConstructorBody(constructor, node, extendsClauseElement, hasSynthesizedSuper), constructor || node)); + var constructorFunction = ts.createFunctionDeclaration(undefined, undefined, undefined, getDeclarationName(node), undefined, transformConstructorParameters(constructor, hasSynthesizedSuper), undefined, transformConstructorBody(constructor, node, extendsClauseElement, hasSynthesizedSuper), constructor || node); + if (extendsClauseElement) { + ts.setEmitFlags(constructorFunction, 256); + } + statements.push(constructorFunction); } function transformConstructorParameters(constructor, hasSynthesizedSuper) { if (constructor && !hasSynthesizedSuper) { @@ -37742,33 +39203,98 @@ var ts; function transformConstructorBody(constructor, node, extendsClauseElement, hasSynthesizedSuper) { var statements = []; startLexicalEnvironment(); + var statementOffset = -1; + if (hasSynthesizedSuper) { + statementOffset = 1; + } + else if (constructor) { + statementOffset = ts.addPrologueDirectives(statements, constructor.body.statements, false, visitor); + } if (constructor) { - addCaptureThisForNodeIfNeeded(statements, constructor); addDefaultValueAssignmentsIfNeeded(statements, constructor); addRestParameterIfNeeded(statements, constructor, hasSynthesizedSuper); + ts.Debug.assert(statementOffset >= 0, "statementOffset not initialized correctly!"); + } + var superCaptureStatus = declareOrCaptureOrReturnThisForConstructorIfNeeded(statements, constructor, !!extendsClauseElement, hasSynthesizedSuper, statementOffset); + if (superCaptureStatus === 1 || superCaptureStatus === 2) { + statementOffset++; } - addDefaultSuperCallIfNeeded(statements, constructor, extendsClauseElement, hasSynthesizedSuper); if (constructor) { - var body = saveStateAndInvoke(constructor, hasSynthesizedSuper ? transformConstructorBodyWithSynthesizedSuper : transformConstructorBodyWithoutSynthesizedSuper); + var body = saveStateAndInvoke(constructor, function (constructor) { return ts.visitNodes(constructor.body.statements, visitor, ts.isStatement, statementOffset); }); ts.addRange(statements, body); } + if (extendsClauseElement + && superCaptureStatus !== 2 + && !(constructor && isSufficientlyCoveredByReturnStatements(constructor.body))) { + statements.push(ts.createReturn(ts.createIdentifier("_this"))); + } ts.addRange(statements, endLexicalEnvironment()); var block = ts.createBlock(ts.createNodeArray(statements, constructor ? constructor.body.statements : node.members), constructor ? constructor.body : node, true); if (!constructor) { - setNodeEmitFlags(block, 49152); + ts.setEmitFlags(block, 49152); } return block; } - function transformConstructorBodyWithSynthesizedSuper(node) { - return ts.visitNodes(node.body.statements, visitor, ts.isStatement, 1); - } - function transformConstructorBodyWithoutSynthesizedSuper(node) { - return ts.visitNodes(node.body.statements, visitor, ts.isStatement, 0); + function isSufficientlyCoveredByReturnStatements(statement) { + if (statement.kind === 211) { + return true; + } + else if (statement.kind === 203) { + var ifStatement = statement; + if (ifStatement.elseStatement) { + return isSufficientlyCoveredByReturnStatements(ifStatement.thenStatement) && + isSufficientlyCoveredByReturnStatements(ifStatement.elseStatement); + } + } + else if (statement.kind === 199) { + var lastStatement = ts.lastOrUndefined(statement.statements); + if (lastStatement && isSufficientlyCoveredByReturnStatements(lastStatement)) { + return true; + } + } + return false; } - function addDefaultSuperCallIfNeeded(statements, constructor, extendsClauseElement, hasSynthesizedSuper) { - if (constructor ? hasSynthesizedSuper : extendsClauseElement) { - statements.push(ts.createStatement(ts.createFunctionApply(ts.createIdentifier("_super"), ts.createThis(), ts.createIdentifier("arguments")), extendsClauseElement)); + function declareOrCaptureOrReturnThisForConstructorIfNeeded(statements, ctor, hasExtendsClause, hasSynthesizedSuper, statementOffset) { + if (!hasExtendsClause) { + if (ctor) { + addCaptureThisForNodeIfNeeded(statements, ctor); + } + return 0; + } + if (!ctor) { + statements.push(ts.createReturn(createDefaultSuperCallOrThis())); + return 2; + } + if (hasSynthesizedSuper) { + captureThisForNode(statements, ctor, createDefaultSuperCallOrThis()); + enableSubstitutionsForCapturedThis(); + return 1; + } + var firstStatement; + var superCallExpression; + var ctorStatements = ctor.body.statements; + if (statementOffset < ctorStatements.length) { + firstStatement = ctorStatements[statementOffset]; + if (firstStatement.kind === 202 && ts.isSuperCall(firstStatement.expression)) { + var superCall = firstStatement.expression; + superCallExpression = ts.setOriginalNode(saveStateAndInvoke(superCall, visitImmediateSuperCallInBody), superCall); + } + } + if (superCallExpression && statementOffset === ctorStatements.length - 1) { + statements.push(ts.createReturn(superCallExpression)); + return 2; + } + captureThisForNode(statements, ctor, superCallExpression, firstStatement); + if (superCallExpression) { + return 1; } + return 0; + } + function createDefaultSuperCallOrThis() { + var actualThis = ts.createThis(); + ts.setEmitFlags(actualThis, 128); + var superCall = ts.createFunctionApply(ts.createIdentifier("_super"), actualThis, ts.createIdentifier("arguments")); + return ts.createLogicalOr(superCall, actualThis); } function visitParameter(node) { if (node.dotDotDotToken) { @@ -37793,34 +39319,34 @@ var ts; } for (var _i = 0, _a = node.parameters; _i < _a.length; _i++) { var parameter = _a[_i]; - var name_33 = parameter.name, initializer = parameter.initializer, dotDotDotToken = parameter.dotDotDotToken; + var name_35 = parameter.name, initializer = parameter.initializer, dotDotDotToken = parameter.dotDotDotToken; if (dotDotDotToken) { continue; } - if (ts.isBindingPattern(name_33)) { - addDefaultValueAssignmentForBindingPattern(statements, parameter, name_33, initializer); + if (ts.isBindingPattern(name_35)) { + addDefaultValueAssignmentForBindingPattern(statements, parameter, name_35, initializer); } else if (initializer) { - addDefaultValueAssignmentForInitializer(statements, parameter, name_33, initializer); + addDefaultValueAssignmentForInitializer(statements, parameter, name_35, initializer); } } } function addDefaultValueAssignmentForBindingPattern(statements, parameter, name, initializer) { var temp = ts.getGeneratedNameForNode(parameter); if (name.elements.length > 0) { - statements.push(setNodeEmitFlags(ts.createVariableStatement(undefined, ts.createVariableDeclarationList(ts.flattenParameterDestructuring(context, parameter, temp, visitor))), 8388608)); + statements.push(ts.setEmitFlags(ts.createVariableStatement(undefined, ts.createVariableDeclarationList(ts.flattenParameterDestructuring(context, parameter, temp, visitor))), 8388608)); } else if (initializer) { - statements.push(setNodeEmitFlags(ts.createStatement(ts.createAssignment(temp, ts.visitNode(initializer, visitor, ts.isExpression))), 8388608)); + statements.push(ts.setEmitFlags(ts.createStatement(ts.createAssignment(temp, ts.visitNode(initializer, visitor, ts.isExpression))), 8388608)); } } function addDefaultValueAssignmentForInitializer(statements, parameter, name, initializer) { initializer = ts.visitNode(initializer, visitor, ts.isExpression); - var statement = ts.createIf(ts.createStrictEquality(ts.getSynthesizedClone(name), ts.createVoidZero()), setNodeEmitFlags(ts.createBlock([ - ts.createStatement(ts.createAssignment(setNodeEmitFlags(ts.getMutableClone(name), 1536), setNodeEmitFlags(initializer, 1536 | getNodeEmitFlags(initializer)), parameter)) + var statement = ts.createIf(ts.createStrictEquality(ts.getSynthesizedClone(name), ts.createVoidZero()), ts.setEmitFlags(ts.createBlock([ + ts.createStatement(ts.createAssignment(ts.setEmitFlags(ts.getMutableClone(name), 1536), ts.setEmitFlags(initializer, 1536 | ts.getEmitFlags(initializer)), parameter)) ], parameter), 32 | 1024 | 12288), undefined, parameter); statement.startsOnNewLine = true; - setNodeEmitFlags(statement, 12288 | 1024 | 8388608); + ts.setEmitFlags(statement, 12288 | 1024 | 8388608); statements.push(statement); } function shouldAddRestParameter(node, inConstructorWithSynthesizedSuper) { @@ -37832,11 +39358,11 @@ var ts; return; } var declarationName = ts.getMutableClone(parameter.name); - setNodeEmitFlags(declarationName, 1536); + ts.setEmitFlags(declarationName, 1536); var expressionName = ts.getSynthesizedClone(parameter.name); var restIndex = node.parameters.length - 1; var temp = ts.createLoopVariable(); - statements.push(setNodeEmitFlags(ts.createVariableStatement(undefined, ts.createVariableDeclarationList([ + statements.push(ts.setEmitFlags(ts.createVariableStatement(undefined, ts.createVariableDeclarationList([ ts.createVariableDeclaration(declarationName, undefined, ts.createArrayLiteral([])) ]), parameter), 8388608)); var forStatement = ts.createFor(ts.createVariableDeclarationList([ @@ -37844,21 +39370,24 @@ var ts; ], parameter), ts.createLessThan(temp, ts.createPropertyAccess(ts.createIdentifier("arguments"), "length"), parameter), ts.createPostfixIncrement(temp, parameter), ts.createBlock([ ts.startOnNewLine(ts.createStatement(ts.createAssignment(ts.createElementAccess(expressionName, ts.createSubtract(temp, ts.createLiteral(restIndex))), ts.createElementAccess(ts.createIdentifier("arguments"), temp)), parameter)) ])); - setNodeEmitFlags(forStatement, 8388608); + ts.setEmitFlags(forStatement, 8388608); ts.startOnNewLine(forStatement); statements.push(forStatement); } function addCaptureThisForNodeIfNeeded(statements, node) { if (node.transformFlags & 16384 && node.kind !== 180) { - enableSubstitutionsForCapturedThis(); - var captureThisStatement = ts.createVariableStatement(undefined, ts.createVariableDeclarationList([ - ts.createVariableDeclaration("_this", undefined, ts.createThis()) - ])); - setNodeEmitFlags(captureThisStatement, 49152 | 8388608); - setSourceMapRange(captureThisStatement, node); - statements.push(captureThisStatement); + captureThisForNode(statements, node, ts.createThis()); } } + function captureThisForNode(statements, node, initializer, originalStatement) { + enableSubstitutionsForCapturedThis(); + var captureThisStatement = ts.createVariableStatement(undefined, ts.createVariableDeclarationList([ + ts.createVariableDeclaration("_this", undefined, initializer) + ]), originalStatement); + ts.setEmitFlags(captureThisStatement, 49152 | 8388608); + ts.setSourceMapRange(captureThisStatement, node); + statements.push(captureThisStatement); + } function addClassMembers(statements, node) { for (var _i = 0, _a = node.members; _i < _a.length; _i++) { var member = _a[_i]; @@ -37888,43 +39417,43 @@ var ts; return ts.createEmptyStatement(member); } function transformClassMethodDeclarationToStatement(receiver, member) { - var commentRange = getCommentRange(member); - var sourceMapRange = getSourceMapRange(member); + var commentRange = ts.getCommentRange(member); + var sourceMapRange = ts.getSourceMapRange(member); var func = transformFunctionLikeToExpression(member, member, undefined); - setNodeEmitFlags(func, 49152); - setSourceMapRange(func, sourceMapRange); + ts.setEmitFlags(func, 49152); + ts.setSourceMapRange(func, sourceMapRange); var statement = ts.createStatement(ts.createAssignment(ts.createMemberAccessForPropertyName(receiver, ts.visitNode(member.name, visitor, ts.isPropertyName), member.name), func), member); ts.setOriginalNode(statement, member); - setCommentRange(statement, commentRange); - setNodeEmitFlags(statement, 1536); + ts.setCommentRange(statement, commentRange); + ts.setEmitFlags(statement, 1536); return statement; } function transformAccessorsToStatement(receiver, accessors) { - var statement = ts.createStatement(transformAccessorsToExpression(receiver, accessors, false), getSourceMapRange(accessors.firstAccessor)); - setNodeEmitFlags(statement, 49152); + var statement = ts.createStatement(transformAccessorsToExpression(receiver, accessors, false), ts.getSourceMapRange(accessors.firstAccessor)); + ts.setEmitFlags(statement, 49152); return statement; } function transformAccessorsToExpression(receiver, _a, startsOnNewLine) { var firstAccessor = _a.firstAccessor, getAccessor = _a.getAccessor, setAccessor = _a.setAccessor; var target = ts.getMutableClone(receiver); - setNodeEmitFlags(target, 49152 | 1024); - setSourceMapRange(target, firstAccessor.name); + ts.setEmitFlags(target, 49152 | 1024); + ts.setSourceMapRange(target, firstAccessor.name); var propertyName = ts.createExpressionForPropertyName(ts.visitNode(firstAccessor.name, visitor, ts.isPropertyName)); - setNodeEmitFlags(propertyName, 49152 | 512); - setSourceMapRange(propertyName, firstAccessor.name); + ts.setEmitFlags(propertyName, 49152 | 512); + ts.setSourceMapRange(propertyName, firstAccessor.name); var properties = []; if (getAccessor) { var getterFunction = transformFunctionLikeToExpression(getAccessor, undefined, undefined); - setSourceMapRange(getterFunction, getSourceMapRange(getAccessor)); + ts.setSourceMapRange(getterFunction, ts.getSourceMapRange(getAccessor)); var getter = ts.createPropertyAssignment("get", getterFunction); - setCommentRange(getter, getCommentRange(getAccessor)); + ts.setCommentRange(getter, ts.getCommentRange(getAccessor)); properties.push(getter); } if (setAccessor) { var setterFunction = transformFunctionLikeToExpression(setAccessor, undefined, undefined); - setSourceMapRange(setterFunction, getSourceMapRange(setAccessor)); + ts.setSourceMapRange(setterFunction, ts.getSourceMapRange(setAccessor)); var setter = ts.createPropertyAssignment("set", setterFunction); - setCommentRange(setter, getCommentRange(setAccessor)); + ts.setCommentRange(setter, ts.getCommentRange(setAccessor)); properties.push(setter); } properties.push(ts.createPropertyAssignment("enumerable", ts.createLiteral(true)), ts.createPropertyAssignment("configurable", ts.createLiteral(true))); @@ -37943,7 +39472,7 @@ var ts; enableSubstitutionsForCapturedThis(); } var func = transformFunctionLikeToExpression(node, node, undefined); - setNodeEmitFlags(func, 256); + ts.setEmitFlags(func, 256); return func; } function visitFunctionExpression(node) { @@ -38000,7 +39529,7 @@ var ts; } var expression = ts.visitNode(body, visitor, ts.isExpression); var returnStatement = ts.createReturn(expression, body); - setNodeEmitFlags(returnStatement, 12288 | 1024 | 32768); + ts.setEmitFlags(returnStatement, 12288 | 1024 | 32768); statements.push(returnStatement); closeBraceLocation = body; } @@ -38011,10 +39540,10 @@ var ts; } var block = ts.createBlock(ts.createNodeArray(statements, statementsLocation), node.body, multiLine); if (!multiLine && singleLine) { - setNodeEmitFlags(block, 32); + ts.setEmitFlags(block, 32); } if (closeBraceLocation) { - setTokenSourceMapRange(block, 16, closeBraceLocation); + ts.setTokenSourceMapRange(block, 16, closeBraceLocation); } ts.setOriginalNode(block, node.body); return block; @@ -38055,7 +39584,7 @@ var ts; assignment = ts.flattenVariableDestructuringToExpression(context, decl, hoistVariableDeclaration, undefined, visitor); } else { - assignment = ts.createBinary(decl.name, 56, decl.initializer); + assignment = ts.createBinary(decl.name, 56, ts.visitNode(decl.initializer, visitor, ts.isExpression)); } (assignments || (assignments = [])).push(assignment); } @@ -38078,13 +39607,13 @@ var ts; : visitVariableDeclaration)); var declarationList = ts.createVariableDeclarationList(declarations, node); ts.setOriginalNode(declarationList, node); - setCommentRange(declarationList, node); + ts.setCommentRange(declarationList, node); if (node.transformFlags & 2097152 && (ts.isBindingPattern(node.declarations[0].name) || ts.isBindingPattern(ts.lastOrUndefined(node.declarations).name))) { var firstDeclaration = ts.firstOrUndefined(declarations); var lastDeclaration = ts.lastOrUndefined(declarations); - setSourceMapRange(declarationList, ts.createRange(firstDeclaration.pos, lastDeclaration.end)); + ts.setSourceMapRange(declarationList, ts.createRange(firstDeclaration.pos, lastDeclaration.end)); } return declarationList; } @@ -38179,7 +39708,7 @@ var ts; ts.setOriginalNode(declarationList, initializer); var firstDeclaration = declarations[0]; var lastDeclaration = ts.lastOrUndefined(declarations); - setSourceMapRange(declarationList, ts.createRange(firstDeclaration.pos, lastDeclaration.end)); + ts.setSourceMapRange(declarationList, ts.createRange(firstDeclaration.pos, lastDeclaration.end)); statements.push(ts.createVariableStatement(undefined, declarationList)); } else { @@ -38214,14 +39743,14 @@ var ts; statements.push(statement); } } - setNodeEmitFlags(expression, 1536 | getNodeEmitFlags(expression)); + ts.setEmitFlags(expression, 1536 | ts.getEmitFlags(expression)); var body = ts.createBlock(ts.createNodeArray(statements, statementsLocation), bodyLocation); - setNodeEmitFlags(body, 1536 | 12288); + ts.setEmitFlags(body, 1536 | 12288); var forStatement = ts.createFor(ts.createVariableDeclarationList([ ts.createVariableDeclaration(counter, undefined, ts.createLiteral(0), ts.moveRangePos(node.expression, -1)), ts.createVariableDeclaration(rhsReference, undefined, expression, node.expression) ], node.expression), ts.createLessThan(counter, ts.createPropertyAccess(rhsReference, "length"), node.expression), ts.createPostfixIncrement(counter, node.expression), body, node); - setNodeEmitFlags(forStatement, 8192); + ts.setEmitFlags(forStatement, 8192); return forStatement; } function visitObjectLiteralExpression(node) { @@ -38239,7 +39768,7 @@ var ts; ts.Debug.assert(numInitialProperties !== numProperties); var temp = ts.createTempVariable(hoistVariableDeclaration); var expressions = []; - var assignment = ts.createAssignment(temp, setNodeEmitFlags(ts.createObjectLiteral(ts.visitNodes(properties, visitor, ts.isObjectLiteralElementLike, 0, numInitialProperties), undefined, node.multiLine), 524288)); + var assignment = ts.createAssignment(temp, ts.setEmitFlags(ts.createObjectLiteral(ts.visitNodes(properties, visitor, ts.isObjectLiteralElementLike, 0, numInitialProperties), undefined, node.multiLine), 524288)); if (node.multiLine) { assignment.startsOnNewLine = true; } @@ -38328,7 +39857,7 @@ var ts; loopBody = ts.createBlock([loopBody], undefined, true); } var isAsyncBlockContainingAwait = enclosingNonArrowFunction - && (enclosingNonArrowFunction.emitFlags & 2097152) !== 0 + && (ts.getEmitFlags(enclosingNonArrowFunction) & 2097152) !== 0 && (node.statement.transformFlags & 4194304) !== 0; var loopBodyFlags = 0; if (currentState.containsLexicalThis) { @@ -38338,7 +39867,7 @@ var ts; loopBodyFlags |= 2097152; } var convertedLoopVariable = ts.createVariableStatement(undefined, ts.createVariableDeclarationList([ - ts.createVariableDeclaration(functionName, undefined, setNodeEmitFlags(ts.createFunctionExpression(isAsyncBlockContainingAwait ? ts.createToken(37) : undefined, undefined, undefined, loopParameters, undefined, loopBody), loopBodyFlags)) + ts.createVariableDeclaration(functionName, undefined, ts.setEmitFlags(ts.createFunctionExpression(isAsyncBlockContainingAwait ? ts.createToken(37) : undefined, undefined, undefined, loopParameters, undefined, loopBody), loopBodyFlags)) ])); var statements = [convertedLoopVariable]; var extraVariableDeclarations; @@ -38366,8 +39895,8 @@ var ts; if (!extraVariableDeclarations) { extraVariableDeclarations = []; } - for (var name_34 in currentState.hoistedLocalVariables) { - var identifier = currentState.hoistedLocalVariables[name_34]; + for (var _b = 0, _c = currentState.hoistedLocalVariables; _b < _c.length; _b++) { + var identifier = _c[_b]; extraVariableDeclarations.push(ts.createVariableDeclaration(identifier)); } } @@ -38376,8 +39905,8 @@ var ts; if (!extraVariableDeclarations) { extraVariableDeclarations = []; } - for (var _b = 0, loopOutParameters_1 = loopOutParameters; _b < loopOutParameters_1.length; _b++) { - var outParam = loopOutParameters_1[_b]; + for (var _d = 0, loopOutParameters_1 = loopOutParameters; _d < loopOutParameters_1.length; _d++) { + var outParam = loopOutParameters_1[_d]; extraVariableDeclarations.push(ts.createVariableDeclaration(outParam.outParamName)); } } @@ -38555,7 +40084,7 @@ var ts; function visitMethodDeclaration(node) { ts.Debug.assert(!ts.isComputedPropertyName(node.name)); var functionExpression = transformFunctionLikeToExpression(node, ts.moveRangePos(node, -1), undefined); - setNodeEmitFlags(functionExpression, 16384 | getNodeEmitFlags(functionExpression)); + ts.setEmitFlags(functionExpression, 16384 | ts.getEmitFlags(functionExpression)); return ts.createPropertyAssignment(node.name, functionExpression, node); } function visitShorthandPropertyAssignment(node) { @@ -38568,13 +40097,32 @@ var ts; return transformAndSpreadElements(node.elements, true, node.multiLine, node.elements.hasTrailingComma); } function visitCallExpression(node) { + return visitCallExpressionWithPotentialCapturedThisAssignment(node, true); + } + function visitImmediateSuperCallInBody(node) { + return visitCallExpressionWithPotentialCapturedThisAssignment(node, false); + } + function visitCallExpressionWithPotentialCapturedThisAssignment(node, assignToCapturedThis) { var _a = ts.createCallBinding(node.expression, hoistVariableDeclaration), target = _a.target, thisArg = _a.thisArg; + if (node.expression.kind === 95) { + ts.setEmitFlags(thisArg, 128); + } + var resultingCall; if (node.transformFlags & 262144) { - return ts.createFunctionApply(ts.visitNode(target, visitor, ts.isExpression), ts.visitNode(thisArg, visitor, ts.isExpression), transformAndSpreadElements(node.arguments, false, false, false)); + resultingCall = ts.createFunctionApply(ts.visitNode(target, visitor, ts.isExpression), ts.visitNode(thisArg, visitor, ts.isExpression), transformAndSpreadElements(node.arguments, false, false, false)); } else { - return ts.createFunctionCall(ts.visitNode(target, visitor, ts.isExpression), ts.visitNode(thisArg, visitor, ts.isExpression), ts.visitNodes(node.arguments, visitor, ts.isExpression), node); + resultingCall = ts.createFunctionCall(ts.visitNode(target, visitor, ts.isExpression), ts.visitNode(thisArg, visitor, ts.isExpression), ts.visitNodes(node.arguments, visitor, ts.isExpression), node); } + if (node.expression.kind === 95) { + var actualThis = ts.createThis(); + ts.setEmitFlags(actualThis, 128); + var initializer = ts.createLogicalOr(resultingCall, actualThis); + return assignToCapturedThis + ? ts.createAssignment(ts.createIdentifier("_this"), initializer) + : initializer; + } + return resultingCall; } function visitNewExpression(node) { ts.Debug.assert((node.transformFlags & 262144) !== 0); @@ -38694,12 +40242,12 @@ var ts; clone.statements = ts.createNodeArray(statements, node.statements); return clone; } - function onEmitNode(node, emit) { + function onEmitNode(emitContext, node, emitCallback) { var savedEnclosingFunction = enclosingFunction; if (enabledSubstitutions & 1 && ts.isFunctionLike(node)) { enclosingFunction = node; } - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); enclosingFunction = savedEnclosingFunction; } function enableSubstitutionsForBlockScopedBindings() { @@ -38721,9 +40269,9 @@ var ts; context.enableEmitNotification(220); } } - function onSubstituteNode(node, isExpression) { - node = previousOnSubstituteNode(node, isExpression); - if (isExpression) { + function onSubstituteNode(emitContext, node) { + node = previousOnSubstituteNode(emitContext, node); + if (emitContext === 1) { return substituteExpression(node); } if (ts.isIdentifier(node)) { @@ -38773,7 +40321,7 @@ var ts; function substituteThisKeyword(node) { if (enabledSubstitutions & 1 && enclosingFunction - && enclosingFunction.emitFlags & 256) { + && ts.getEmitFlags(enclosingFunction) & 256) { return ts.createIdentifier("_this", node); } return node; @@ -38783,8 +40331,8 @@ var ts; } function getDeclarationName(node, allowComments, allowSourceMaps, emitFlags) { if (node.name && !ts.isGeneratedIdentifier(node.name)) { - var name_35 = ts.getMutableClone(node.name); - emitFlags |= getNodeEmitFlags(node.name); + var name_36 = ts.getMutableClone(node.name); + emitFlags |= ts.getEmitFlags(node.name); if (!allowSourceMaps) { emitFlags |= 1536; } @@ -38792,9 +40340,9 @@ var ts; emitFlags |= 49152; } if (emitFlags) { - setNodeEmitFlags(name_35, emitFlags); + ts.setEmitFlags(name_36, emitFlags); } - return name_35; + return name_36; } return ts.getGeneratedNameForNode(node); } @@ -38842,7 +40390,7 @@ var ts; _a[7] = "endfinally", _a)); function transformGenerators(context) { - var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistFunctionDeclaration = context.hoistFunctionDeclaration, hoistVariableDeclaration = context.hoistVariableDeclaration, setSourceMapRange = context.setSourceMapRange, setCommentRange = context.setCommentRange, setNodeEmitFlags = context.setNodeEmitFlags; + var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistFunctionDeclaration = context.hoistFunctionDeclaration, hoistVariableDeclaration = context.hoistVariableDeclaration; var compilerOptions = context.getCompilerOptions(); var languageVersion = ts.getEmitScriptTarget(compilerOptions); var resolver = context.getEmitResolver(); @@ -38876,6 +40424,9 @@ var ts; var withBlockStack; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } if (node.transformFlags & 1024) { currentSourceFile = node; node = ts.visitEachChild(node, visitor, context); @@ -38982,7 +40533,7 @@ var ts; } } function visitFunctionDeclaration(node) { - if (node.asteriskToken && node.emitFlags & 2097152) { + if (node.asteriskToken && ts.getEmitFlags(node) & 2097152) { node = ts.setOriginalNode(ts.createFunctionDeclaration(undefined, undefined, undefined, node.name, undefined, node.parameters, undefined, transformGeneratorFunctionBody(node.body), node), node); } else { @@ -39003,7 +40554,7 @@ var ts; } } function visitFunctionExpression(node) { - if (node.asteriskToken && node.emitFlags & 2097152) { + if (node.asteriskToken && ts.getEmitFlags(node) & 2097152) { node = ts.setOriginalNode(ts.createFunctionExpression(undefined, node.name, undefined, node.parameters, undefined, transformGeneratorFunctionBody(node.body), node), node); } else { @@ -39034,6 +40585,7 @@ var ts; var savedBlocks = blocks; var savedBlockOffsets = blockOffsets; var savedBlockActions = blockActions; + var savedBlockStack = blockStack; var savedLabelOffsets = labelOffsets; var savedLabelExpressions = labelExpressions; var savedNextLabelId = nextLabelId; @@ -39046,6 +40598,7 @@ var ts; blocks = undefined; blockOffsets = undefined; blockActions = undefined; + blockStack = undefined; labelOffsets = undefined; labelExpressions = undefined; nextLabelId = 1; @@ -39064,6 +40617,7 @@ var ts; blocks = savedBlocks; blockOffsets = savedBlockOffsets; blockActions = savedBlockActions; + blockStack = savedBlockStack; labelOffsets = savedLabelOffsets; labelExpressions = savedLabelExpressions; nextLabelId = savedNextLabelId; @@ -39079,7 +40633,7 @@ var ts; return undefined; } else { - if (node.emitFlags & 8388608) { + if (ts.getEmitFlags(node) & 8388608) { return node; } for (var _i = 0, _a = node.declarationList.declarations; _i < _a.length; _i++) { @@ -39747,9 +41301,9 @@ var ts; } return -1; } - function onSubstituteNode(node, isExpression) { - node = previousOnSubstituteNode(node, isExpression); - if (isExpression) { + function onSubstituteNode(emitContext, node) { + node = previousOnSubstituteNode(emitContext, node); + if (emitContext === 1) { return substituteExpression(node); } return node; @@ -39766,11 +41320,11 @@ var ts; if (ts.isIdentifier(original) && original.parent) { var declaration = resolver.getReferencedValueDeclaration(original); if (declaration) { - var name_36 = ts.getProperty(renamedCatchVariableDeclarations, String(ts.getOriginalNodeId(declaration))); - if (name_36) { - var clone_8 = ts.getMutableClone(name_36); - setSourceMapRange(clone_8, node); - setCommentRange(clone_8, node); + var name_37 = ts.getProperty(renamedCatchVariableDeclarations, String(ts.getOriginalNodeId(declaration))); + if (name_37) { + var clone_8 = ts.getMutableClone(name_37); + ts.setSourceMapRange(clone_8, node); + ts.setCommentRange(clone_8, node); return clone_8; } } @@ -40164,7 +41718,7 @@ var ts; var buildResult = buildStatements(); return ts.createCall(ts.createHelperName(currentSourceFile.externalHelpersModuleName, "__generator"), undefined, [ ts.createThis(), - setNodeEmitFlags(ts.createFunctionExpression(undefined, undefined, undefined, [ts.createParameter(state)], undefined, ts.createBlock(buildResult, undefined, buildResult.length > 0)), 4194304) + ts.setEmitFlags(ts.createFunctionExpression(undefined, undefined, undefined, [ts.createParameter(state)], undefined, ts.createBlock(buildResult, undefined, buildResult.length > 0)), 4194304) ]); } function buildStatements() { @@ -40439,7 +41993,7 @@ var ts; _a[ts.ModuleKind.AMD] = transformAMDModule, _a[ts.ModuleKind.UMD] = transformUMDModule, _a)); - var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration, setNodeEmitFlags = context.setNodeEmitFlags, getNodeEmitFlags = context.getNodeEmitFlags, setSourceMapRange = context.setSourceMapRange; + var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration; var compilerOptions = context.getCompilerOptions(); var resolver = context.getEmitResolver(); var host = context.getEmitHost(); @@ -40464,6 +42018,9 @@ var ts; var hasExportStarsToExportValues; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } if (ts.isExternalModule(node) || compilerOptions.isolatedModules) { currentSourceFile = node; (_a = ts.collectExternalModuleInfo(node, resolver), externalImports = _a.externalImports, exportSpecifiers = _a.exportSpecifiers, exportEquals = _a.exportEquals, hasExportStarsToExportValues = _a.hasExportStarsToExportValues, _a); @@ -40489,7 +42046,7 @@ var ts; addExportEqualsIfNeeded(statements, false); var updated = updateSourceFile(node, statements); if (hasExportStarsToExportValues) { - setNodeEmitFlags(updated, 2 | getNodeEmitFlags(node)); + ts.setEmitFlags(updated, 2 | ts.getEmitFlags(node)); } return updated; } @@ -40500,7 +42057,7 @@ var ts; } function transformUMDModule(node) { var define = ts.createIdentifier("define"); - setNodeEmitFlags(define, 16); + ts.setEmitFlags(define, 16); return transformAsynchronousModule(node, define, undefined, false); } function transformAsynchronousModule(node, define, moduleName, includeNonAmdDependencies) { @@ -40527,7 +42084,7 @@ var ts; addExportEqualsIfNeeded(statements, true); var body = ts.createBlock(statements, undefined, true); if (hasExportStarsToExportValues) { - setNodeEmitFlags(body, 2); + ts.setEmitFlags(body, 2); } return body; } @@ -40535,12 +42092,12 @@ var ts; if (exportEquals && resolver.isValueAliasDeclaration(exportEquals)) { if (emitAsReturn) { var statement = ts.createReturn(exportEquals.expression, exportEquals); - setNodeEmitFlags(statement, 12288 | 49152); + ts.setEmitFlags(statement, 12288 | 49152); statements.push(statement); } else { var statement = ts.createStatement(ts.createAssignment(ts.createPropertyAccess(ts.createIdentifier("module"), "exports"), exportEquals.expression), exportEquals); - setNodeEmitFlags(statement, 49152); + ts.setEmitFlags(statement, 49152); statements.push(statement); } } @@ -40603,7 +42160,7 @@ var ts; if (!ts.contains(externalImports, node)) { return undefined; } - setNodeEmitFlags(node.name, 128); + ts.setEmitFlags(node.name, 128); var statements = []; if (moduleKind !== ts.ModuleKind.AMD) { if (ts.hasModifier(node, 1)) { @@ -40691,16 +42248,16 @@ var ts; else { var names = ts.reduceEachChild(node, collectExportMembers, []); for (var _i = 0, names_1 = names; _i < names_1.length; _i++) { - var name_37 = names_1[_i]; - addExportMemberAssignments(statements, name_37); + var name_38 = names_1[_i]; + addExportMemberAssignments(statements, name_38); } } } function collectExportMembers(names, node) { if (ts.isAliasSymbolDeclaration(node) && resolver.isValueAliasDeclaration(node) && ts.isDeclaration(node)) { - var name_38 = node.name; - if (ts.isIdentifier(name_38)) { - names.push(name_38); + var name_39 = node.name; + if (ts.isIdentifier(name_39)) { + names.push(name_39); } } return ts.reduceEachChild(node, collectExportMembers, names); @@ -40718,7 +42275,7 @@ var ts; addExportDefault(statements, getDeclarationName(node), node); } else { - statements.push(createExportStatement(node.name, setNodeEmitFlags(ts.getSynthesizedClone(node.name), 262144), node)); + statements.push(createExportStatement(node.name, ts.setEmitFlags(ts.getSynthesizedClone(node.name), 262144), node)); } } function visitVariableStatement(node) { @@ -40835,25 +42392,25 @@ var ts; } function addVarForExportedEnumOrNamespaceDeclaration(statements, node) { var transformedStatement = ts.createVariableStatement(undefined, [ts.createVariableDeclaration(getDeclarationName(node), undefined, ts.createPropertyAccess(ts.createIdentifier("exports"), getDeclarationName(node)))], node); - setNodeEmitFlags(transformedStatement, 49152); + ts.setEmitFlags(transformedStatement, 49152); statements.push(transformedStatement); } function getDeclarationName(node) { return node.name ? ts.getSynthesizedClone(node.name) : ts.getGeneratedNameForNode(node); } - function onEmitNode(node, emit) { + function onEmitNode(emitContext, node, emitCallback) { if (node.kind === 256) { bindingNameExportSpecifiersMap = bindingNameExportSpecifiersForFileMap[ts.getOriginalNodeId(node)]; - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); bindingNameExportSpecifiersMap = undefined; } else { - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); } } - function onSubstituteNode(node, isExpression) { - node = previousOnSubstituteNode(node, isExpression); - if (isExpression) { + function onSubstituteNode(emitContext, node) { + node = previousOnSubstituteNode(emitContext, node); + if (emitContext === 1) { return substituteExpression(node); } else if (ts.isShorthandPropertyAssignment(node)) { @@ -40894,7 +42451,7 @@ var ts; var left = node.left; if (ts.isIdentifier(left) && ts.isAssignmentOperator(node.operatorToken.kind)) { if (bindingNameExportSpecifiersMap && ts.hasProperty(bindingNameExportSpecifiersMap, left.text)) { - setNodeEmitFlags(node, 128); + ts.setEmitFlags(node, 128); var nestedExportAssignment = void 0; for (var _i = 0, _a = bindingNameExportSpecifiersMap[left.text]; _i < _a.length; _i++) { var specifier = _a[_i]; @@ -40912,11 +42469,11 @@ var ts; var operand = node.operand; if (ts.isIdentifier(operand) && bindingNameExportSpecifiersForFileMap) { if (bindingNameExportSpecifiersMap && ts.hasProperty(bindingNameExportSpecifiersMap, operand.text)) { - setNodeEmitFlags(node, 128); + ts.setEmitFlags(node, 128); var transformedUnaryExpression = void 0; if (node.kind === 186) { - transformedUnaryExpression = ts.createBinary(operand, ts.createNode(operator === 41 ? 57 : 58), ts.createLiteral(1), node); - setNodeEmitFlags(transformedUnaryExpression, 128); + transformedUnaryExpression = ts.createBinary(operand, ts.createToken(operator === 41 ? 57 : 58), ts.createLiteral(1), node); + ts.setEmitFlags(transformedUnaryExpression, 128); } var nestedExportAssignment = void 0; for (var _i = 0, _a = bindingNameExportSpecifiersMap[operand.text]; _i < _a.length; _i++) { @@ -40931,7 +42488,7 @@ var ts; return node; } function trySubstituteExportedName(node) { - var emitFlags = getNodeEmitFlags(node); + var emitFlags = ts.getEmitFlags(node); if ((emitFlags & 262144) === 0) { var container = resolver.getReferencedExportContainer(node, (emitFlags & 131072) !== 0); if (container) { @@ -40943,7 +42500,7 @@ var ts; return undefined; } function trySubstituteImportedName(node) { - if ((getNodeEmitFlags(node) & 262144) === 0) { + if ((ts.getEmitFlags(node) & 262144) === 0) { var declaration = resolver.getReferencedImportDeclaration(node); if (declaration) { if (ts.isImportClause(declaration)) { @@ -40955,12 +42512,12 @@ var ts; } } else if (ts.isImportSpecifier(declaration)) { - var name_39 = declaration.propertyName || declaration.name; - if (name_39.originalKeywordKind === 77 && languageVersion <= 0) { - return ts.createElementAccess(ts.getGeneratedNameForNode(declaration.parent.parent.parent), ts.createLiteral(name_39.text), node); + var name_40 = declaration.propertyName || declaration.name; + if (name_40.originalKeywordKind === 77 && languageVersion <= 0) { + return ts.createElementAccess(ts.getGeneratedNameForNode(declaration.parent.parent.parent), ts.createLiteral(name_40.text), node); } else { - return ts.createPropertyAccess(ts.getGeneratedNameForNode(declaration.parent.parent.parent), ts.getSynthesizedClone(name_39), node); + return ts.createPropertyAccess(ts.getGeneratedNameForNode(declaration.parent.parent.parent), ts.getSynthesizedClone(name_40), node); } } } @@ -40982,7 +42539,7 @@ var ts; var statement = ts.createStatement(createExportAssignment(name, value)); statement.startsOnNewLine = true; if (location) { - setSourceMapRange(statement, location); + ts.setSourceMapRange(statement, location); } return statement; } @@ -41010,7 +42567,7 @@ var ts; var externalModuleName = ts.getExternalModuleNameLiteral(importNode, currentSourceFile, host, resolver, compilerOptions); var importAliasName = ts.getLocalNameForExternalImport(importNode, currentSourceFile); if (includeNonAmdDependencies && importAliasName) { - setNodeEmitFlags(importAliasName, 128); + ts.setEmitFlags(importAliasName, 128); aliasedModuleNames.push(externalModuleName); importAliasNames.push(ts.createParameter(importAliasName)); } @@ -41032,7 +42589,7 @@ var ts; var ts; (function (ts) { function transformSystemModule(context) { - var getNodeEmitFlags = context.getNodeEmitFlags, setNodeEmitFlags = context.setNodeEmitFlags, startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration, hoistFunctionDeclaration = context.hoistFunctionDeclaration; + var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration, hoistFunctionDeclaration = context.hoistFunctionDeclaration; var compilerOptions = context.getCompilerOptions(); var resolver = context.getEmitResolver(); var host = context.getEmitHost(); @@ -41061,6 +42618,9 @@ var ts; var currentNode; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } if (ts.isExternalModule(node) || compilerOptions.isolatedModules) { currentSourceFile = node; currentNode = node; @@ -41093,12 +42653,12 @@ var ts; var body = ts.createFunctionExpression(undefined, undefined, undefined, [ ts.createParameter(exportFunctionForFile), ts.createParameter(contextObjectForFile) - ], undefined, setNodeEmitFlags(ts.createBlock(statements, undefined, true), 1)); + ], undefined, ts.setEmitFlags(ts.createBlock(statements, undefined, true), 1)); return updateSourceFile(node, [ ts.createStatement(ts.createCall(ts.createPropertyAccess(ts.createIdentifier("System"), "register"), undefined, moduleName ? [moduleName, dependencies, body] : [dependencies, body])) - ], ~1 & getNodeEmitFlags(node)); + ], ~1 & ts.getEmitFlags(node)); var _a; } function addSystemModuleBody(statements, node, dependencyGroups) { @@ -41342,11 +42902,11 @@ var ts; } function visitFunctionDeclaration(node) { if (ts.hasModifier(node, 1)) { - var name_40 = node.name || ts.getGeneratedNameForNode(node); - var newNode = ts.createFunctionDeclaration(undefined, undefined, node.asteriskToken, name_40, undefined, node.parameters, undefined, node.body, node); + var name_41 = node.name || ts.getGeneratedNameForNode(node); + var newNode = ts.createFunctionDeclaration(undefined, undefined, node.asteriskToken, name_41, undefined, node.parameters, undefined, node.body, node); recordExportedFunctionDeclaration(node); if (!ts.hasModifier(node, 512)) { - recordExportName(name_40); + recordExportName(name_41); } ts.setOriginalNode(newNode, node); node = newNode; @@ -41357,13 +42917,13 @@ var ts; function visitExpressionStatement(node) { var originalNode = ts.getOriginalNode(node); if ((originalNode.kind === 225 || originalNode.kind === 224) && ts.hasModifier(originalNode, 1)) { - var name_41 = getDeclarationName(originalNode); + var name_42 = getDeclarationName(originalNode); if (originalNode.kind === 224) { - hoistVariableDeclaration(name_41); + hoistVariableDeclaration(name_42); } return [ node, - createExportStatement(name_41, name_41) + createExportStatement(name_42, name_42) ]; } return node; @@ -41517,19 +43077,19 @@ var ts; function visitBlock(node) { return ts.visitEachChild(node, visitNestedNode, context); } - function onEmitNode(node, emit) { + function onEmitNode(emitContext, node, emitCallback) { if (node.kind === 256) { exportFunctionForFile = exportFunctionForFileMap[ts.getOriginalNodeId(node)]; - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); exportFunctionForFile = undefined; } else { - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); } } - function onSubstituteNode(node, isExpression) { - node = previousOnSubstituteNode(node, isExpression); - if (isExpression) { + function onSubstituteNode(emitContext, node) { + node = previousOnSubstituteNode(emitContext, node); + if (emitContext === 1) { return substituteExpression(node); } return node; @@ -41563,7 +43123,7 @@ var ts; return node; } function substituteAssignmentExpression(node) { - setNodeEmitFlags(node, 128); + ts.setEmitFlags(node, 128); var left = node.left; switch (left.kind) { case 69: @@ -41668,7 +43228,7 @@ var ts; var exportDeclaration = resolver.getReferencedExportContainer(operand); if (exportDeclaration) { var expr = ts.createPrefix(node.operator, operand, node); - setNodeEmitFlags(expr, 128); + ts.setEmitFlags(expr, 128); var call = createExportExpression(operand, expr); if (node.kind === 185) { return call; @@ -41701,7 +43261,7 @@ var ts; ts.createForIn(ts.createVariableDeclarationList([ ts.createVariableDeclaration(n, undefined) ]), m, ts.createBlock([ - setNodeEmitFlags(ts.createIf(condition, ts.createStatement(ts.createAssignment(ts.createElementAccess(exports, n), ts.createElementAccess(m, n)))), 32) + ts.setEmitFlags(ts.createIf(condition, ts.createStatement(ts.createAssignment(ts.createElementAccess(exports, n), ts.createElementAccess(m, n)))), 32) ])), ts.createStatement(ts.createCall(exportFunctionForFile, undefined, [exports])) ], undefined, true))); @@ -41801,7 +43361,7 @@ var ts; function updateSourceFile(node, statements, nodeEmitFlags) { var updated = ts.getMutableClone(node); updated.statements = ts.createNodeArray(statements, node.statements); - setNodeEmitFlags(updated, nodeEmitFlags); + ts.setEmitFlags(updated, nodeEmitFlags); return updated; } } @@ -41815,6 +43375,9 @@ var ts; var currentSourceFile; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } if (ts.isExternalModule(node) || compilerOptions.isolatedModules) { currentSourceFile = node; return ts.visitEachChild(node, visitor, context); @@ -41951,18 +43514,10 @@ var ts; return transformers; } ts.getTransformers = getTransformers; - var nextTransformId = 1; function transformFiles(resolver, host, sourceFiles, transformers) { - var transformId = nextTransformId; - nextTransformId++; - var tokenSourceMapRanges = ts.createMap(); var lexicalEnvironmentVariableDeclarationsStack = []; var lexicalEnvironmentFunctionDeclarationsStack = []; var enabledSyntaxKindFeatures = new Array(289); - var parseTreeNodesWithAnnotations = []; - var lastTokenSourceMapRangeNode; - var lastTokenSourceMapRangeToken; - var lastTokenSourceMapRange; var lexicalEnvironmentStackOffset = 0; var hoistedVariableDeclarations; var hoistedFunctionDeclarations; @@ -41971,47 +43526,24 @@ var ts; getCompilerOptions: function () { return host.getCompilerOptions(); }, getEmitResolver: function () { return resolver; }, getEmitHost: function () { return host; }, - getNodeEmitFlags: getNodeEmitFlags, - setNodeEmitFlags: setNodeEmitFlags, - getSourceMapRange: getSourceMapRange, - setSourceMapRange: setSourceMapRange, - getTokenSourceMapRange: getTokenSourceMapRange, - setTokenSourceMapRange: setTokenSourceMapRange, - getCommentRange: getCommentRange, - setCommentRange: setCommentRange, hoistVariableDeclaration: hoistVariableDeclaration, hoistFunctionDeclaration: hoistFunctionDeclaration, startLexicalEnvironment: startLexicalEnvironment, endLexicalEnvironment: endLexicalEnvironment, - onSubstituteNode: onSubstituteNode, + onSubstituteNode: function (emitContext, node) { return node; }, enableSubstitution: enableSubstitution, isSubstitutionEnabled: isSubstitutionEnabled, - onEmitNode: onEmitNode, + onEmitNode: function (node, emitContext, emitCallback) { return emitCallback(node, emitContext); }, enableEmitNotification: enableEmitNotification, isEmitNotificationEnabled: isEmitNotificationEnabled }; - var transformation = chain.apply(void 0, transformers)(context); + var transformation = ts.chain.apply(void 0, transformers)(context); var transformed = ts.map(sourceFiles, transformSourceFile); lexicalEnvironmentDisabled = true; return { - getSourceFiles: function () { return transformed; }, - getTokenSourceMapRange: getTokenSourceMapRange, - isSubstitutionEnabled: isSubstitutionEnabled, - isEmitNotificationEnabled: isEmitNotificationEnabled, - onSubstituteNode: context.onSubstituteNode, - onEmitNode: context.onEmitNode, - dispose: function () { - for (var _i = 0, parseTreeNodesWithAnnotations_1 = parseTreeNodesWithAnnotations; _i < parseTreeNodesWithAnnotations_1.length; _i++) { - var node = parseTreeNodesWithAnnotations_1[_i]; - if (node.transformId === transformId) { - node.transformId = 0; - node.emitFlags = 0; - node.commentRange = undefined; - node.sourceMapRange = undefined; - } - } - parseTreeNodesWithAnnotations.length = 0; - } + transformed: transformed, + emitNodeWithSubstitution: emitNodeWithSubstitution, + emitNodeWithNotification: emitNodeWithNotification }; function transformSourceFile(sourceFile) { if (ts.isDeclarationFile(sourceFile)) { @@ -42023,75 +43555,37 @@ var ts; enabledSyntaxKindFeatures[kind] |= 1; } function isSubstitutionEnabled(node) { - return (enabledSyntaxKindFeatures[node.kind] & 1) !== 0; + return (enabledSyntaxKindFeatures[node.kind] & 1) !== 0 + && (ts.getEmitFlags(node) & 128) === 0; } - function onSubstituteNode(node, isExpression) { - return node; + function emitNodeWithSubstitution(emitContext, node, emitCallback) { + if (node) { + if (isSubstitutionEnabled(node)) { + var substitute = context.onSubstituteNode(emitContext, node); + if (substitute && substitute !== node) { + emitCallback(emitContext, substitute); + return; + } + } + emitCallback(emitContext, node); + } } function enableEmitNotification(kind) { enabledSyntaxKindFeatures[kind] |= 2; } function isEmitNotificationEnabled(node) { return (enabledSyntaxKindFeatures[node.kind] & 2) !== 0 - || (getNodeEmitFlags(node) & 64) !== 0; + || (ts.getEmitFlags(node) & 64) !== 0; } - function onEmitNode(node, emit) { - emit(node); - } - function beforeSetAnnotation(node) { - if ((node.flags & 8) === 0 && node.transformId !== transformId) { - parseTreeNodesWithAnnotations.push(node); - node.transformId = transformId; - } - } - function getNodeEmitFlags(node) { - return node.emitFlags; - } - function setNodeEmitFlags(node, emitFlags) { - beforeSetAnnotation(node); - node.emitFlags = emitFlags; - return node; - } - function getSourceMapRange(node) { - return node.sourceMapRange || node; - } - function setSourceMapRange(node, range) { - beforeSetAnnotation(node); - node.sourceMapRange = range; - return node; - } - function getTokenSourceMapRange(node, token) { - if (lastTokenSourceMapRangeNode === node && lastTokenSourceMapRangeToken === token) { - return lastTokenSourceMapRange; - } - var range; - var current = node; - while (current) { - range = current.id ? tokenSourceMapRanges[current.id + "-" + token] : undefined; - if (range !== undefined) { - break; + function emitNodeWithNotification(emitContext, node, emitCallback) { + if (node) { + if (isEmitNotificationEnabled(node)) { + context.onEmitNode(emitContext, node, emitCallback); + } + else { + emitCallback(emitContext, node); } - current = current.original; } - lastTokenSourceMapRangeNode = node; - lastTokenSourceMapRangeToken = token; - lastTokenSourceMapRange = range; - return range; - } - function setTokenSourceMapRange(node, token, range) { - lastTokenSourceMapRangeNode = node; - lastTokenSourceMapRangeToken = token; - lastTokenSourceMapRange = range; - tokenSourceMapRanges[ts.getNodeId(node) + "-" + token] = range; - return node; - } - function getCommentRange(node) { - return node.commentRange || node; - } - function setCommentRange(node, range) { - beforeSetAnnotation(node); - node.commentRange = range; - return node; } function hoistVariableDeclaration(name) { ts.Debug.assert(!lexicalEnvironmentDisabled, "Cannot modify the lexical environment during the print phase."); @@ -42144,54 +43638,6 @@ var ts; } } ts.transformFiles = transformFiles; - function chain(a, b, c, d, e) { - if (e) { - var args_3 = []; - for (var i = 0; i < arguments.length; i++) { - args_3[i] = arguments[i]; - } - return function (t) { return compose.apply(void 0, ts.map(args_3, function (f) { return f(t); })); }; - } - else if (d) { - return function (t) { return compose(a(t), b(t), c(t), d(t)); }; - } - else if (c) { - return function (t) { return compose(a(t), b(t), c(t)); }; - } - else if (b) { - return function (t) { return compose(a(t), b(t)); }; - } - else if (a) { - return function (t) { return compose(a(t)); }; - } - else { - return function (t) { return function (u) { return u; }; }; - } - } - function compose(a, b, c, d, e) { - if (e) { - var args_4 = []; - for (var i = 0; i < arguments.length; i++) { - args_4[i] = arguments[i]; - } - return function (t) { return ts.reduceLeft(args_4, function (u, f) { return f(u); }, t); }; - } - else if (d) { - return function (t) { return d(c(b(a(t)))); }; - } - else if (c) { - return function (t) { return c(b(a(t))); }; - } - else if (b) { - return function (t) { return b(a(t)); }; - } - else if (a) { - return function (t) { return a(t); }; - } - else { - return function (t) { return t; }; - } - } var _a; })(ts || (ts = {})); var ts; @@ -42202,11 +43648,11 @@ var ts; return declarationDiagnostics.getDiagnostics(targetSourceFile ? targetSourceFile.fileName : undefined); function getDeclarationDiagnosticsFromFile(_a, sources, isBundledEmit) { var declarationFilePath = _a.declarationFilePath; - emitDeclarations(host, resolver, declarationDiagnostics, declarationFilePath, sources, isBundledEmit); + emitDeclarations(host, resolver, declarationDiagnostics, declarationFilePath, sources, isBundledEmit, false); } } ts.getDeclarationDiagnostics = getDeclarationDiagnostics; - function emitDeclarations(host, resolver, emitterDiagnostics, declarationFilePath, sourceFiles, isBundledEmit) { + function emitDeclarations(host, resolver, emitterDiagnostics, declarationFilePath, sourceFiles, isBundledEmit, emitOnlyDtsFiles) { var newLine = host.getNewLine(); var compilerOptions = host.getCompilerOptions(); var write; @@ -42242,7 +43688,7 @@ var ts; ts.forEach(sourceFile.referencedFiles, function (fileReference) { var referencedFile = ts.tryResolveScriptReference(host, sourceFile, fileReference); if (referencedFile && !ts.contains(emittedReferencedFiles, referencedFile)) { - if (writeReferencePath(referencedFile, !isBundledEmit && !addedGlobalFileReference)) { + if (writeReferencePath(referencedFile, !isBundledEmit && !addedGlobalFileReference, emitOnlyDtsFiles)) { addedGlobalFileReference = true; } emittedReferencedFiles.push(referencedFile); @@ -42427,7 +43873,7 @@ var ts; } else { errorNameNode = declaration.name; - resolver.writeTypeOfDeclaration(declaration, enclosingDeclaration, 2, writer); + resolver.writeTypeOfDeclaration(declaration, enclosingDeclaration, 2 | 1024, writer); errorNameNode = undefined; } } @@ -42439,7 +43885,7 @@ var ts; } else { errorNameNode = signature.name; - resolver.writeReturnTypeOfSignatureDeclaration(signature, enclosingDeclaration, 2, writer); + resolver.writeReturnTypeOfSignatureDeclaration(signature, enclosingDeclaration, 2 | 1024, writer); errorNameNode = undefined; } } @@ -42612,9 +44058,9 @@ var ts; var count = 0; while (true) { count++; - var name_42 = baseName + "_" + count; - if (!(name_42 in currentIdentifiers)) { - return name_42; + var name_43 = baseName + "_" + count; + if (!(name_43 in currentIdentifiers)) { + return name_43; } } } @@ -42632,7 +44078,7 @@ var ts; write(tempVarName); write(": "); writer.getSymbolAccessibilityDiagnostic = getDefaultExportAccessibilityDiagnostic; - resolver.writeTypeOfExpression(node.expression, enclosingDeclaration, 2, writer); + resolver.writeTypeOfExpression(node.expression, enclosingDeclaration, 2 | 1024, writer); write(";"); writeLine(); write(node.isExportEquals ? "export = " : "export default "); @@ -43038,7 +44484,7 @@ var ts; } else { writer.getSymbolAccessibilityDiagnostic = getHeritageClauseVisibilityError; - resolver.writeBaseConstructorTypeOfClass(enclosingDeclaration, enclosingDeclaration, 2, writer); + resolver.writeBaseConstructorTypeOfClass(enclosingDeclaration, enclosingDeclaration, 2 | 1024, writer); } function getHeritageClauseVisibilityError(symbolAccessibilityResult) { var diagnosticMessage; @@ -43606,14 +45052,14 @@ var ts; return emitSourceFile(node); } } - function writeReferencePath(referencedFile, addBundledFileReference) { + function writeReferencePath(referencedFile, addBundledFileReference, emitOnlyDtsFiles) { var declFileName; var addedBundledEmitReference = false; if (ts.isDeclarationFile(referencedFile)) { declFileName = referencedFile.fileName; } else { - ts.forEachExpectedEmitFile(host, getDeclFileName, referencedFile); + ts.forEachExpectedEmitFile(host, getDeclFileName, referencedFile, emitOnlyDtsFiles); } if (declFileName) { declFileName = ts.getRelativePathToDirectoryOrUrl(ts.getDirectoryPath(ts.normalizeSlashes(declarationFilePath)), declFileName, host.getCurrentDirectory(), host.getCanonicalFileName, false); @@ -43630,8 +45076,8 @@ var ts; } } } - function writeDeclarationFile(declarationFilePath, sourceFiles, isBundledEmit, host, resolver, emitterDiagnostics) { - var emitDeclarationResult = emitDeclarations(host, resolver, emitterDiagnostics, declarationFilePath, sourceFiles, isBundledEmit); + function writeDeclarationFile(declarationFilePath, sourceFiles, isBundledEmit, host, resolver, emitterDiagnostics, emitOnlyDtsFiles) { + var emitDeclarationResult = emitDeclarations(host, resolver, emitterDiagnostics, declarationFilePath, sourceFiles, isBundledEmit, emitOnlyDtsFiles); var emitSkipped = emitDeclarationResult.reportedDeclarationError || host.isEmitBlocked(declarationFilePath) || host.getCompilerOptions().noEmit; if (!emitSkipped) { var declarationOutput = emitDeclarationResult.referencesOutput @@ -43657,41 +45103,6 @@ var ts; })(ts || (ts = {})); var ts; (function (ts) { - function createSourceMapWriter(host, writer) { - var compilerOptions = host.getCompilerOptions(); - if (compilerOptions.sourceMap || compilerOptions.inlineSourceMap) { - if (compilerOptions.extendedDiagnostics) { - return createSourceMapWriterWithExtendedDiagnostics(host, writer); - } - return createSourceMapWriterWorker(host, writer); - } - else { - return getNullSourceMapWriter(); - } - } - ts.createSourceMapWriter = createSourceMapWriter; - var nullSourceMapWriter; - function getNullSourceMapWriter() { - if (nullSourceMapWriter === undefined) { - nullSourceMapWriter = { - initialize: function (filePath, sourceMapFilePath, sourceFiles, isBundledEmit) { }, - reset: function () { }, - getSourceMapData: function () { return undefined; }, - setSourceFile: function (sourceFile) { }, - emitPos: function (pos) { }, - emitStart: function (range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { }, - emitEnd: function (range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { }, - emitTokenStart: function (token, pos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { return -1; }, - emitTokenEnd: function (token, end, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { return -1; }, - changeEmitSourcePos: function () { }, - stopOverridingSpan: function () { }, - getText: function () { return undefined; }, - getSourceMappingURL: function () { return undefined; } - }; - } - return nullSourceMapWriter; - } - ts.getNullSourceMapWriter = getNullSourceMapWriter; var defaultLastEncodedSourceMapSpan = { emittedLine: 1, emittedColumn: 1, @@ -43699,42 +45110,38 @@ var ts; sourceColumn: 1, sourceIndex: 0 }; - function createSourceMapWriterWorker(host, writer) { + function createSourceMapWriter(host, writer) { var compilerOptions = host.getCompilerOptions(); var extendedDiagnostics = compilerOptions.extendedDiagnostics; var currentSourceFile; var currentSourceText; var sourceMapDir; - var stopOverridingSpan = false; - var modifyLastSourcePos = false; var sourceMapSourceIndex; var lastRecordedSourceMapSpan; var lastEncodedSourceMapSpan; var lastEncodedNameIndex; var sourceMapData; - var disableDepth; + var disabled = !(compilerOptions.sourceMap || compilerOptions.inlineSourceMap); return { initialize: initialize, reset: reset, getSourceMapData: function () { return sourceMapData; }, setSourceFile: setSourceFile, emitPos: emitPos, - emitStart: emitStart, - emitEnd: emitEnd, - emitTokenStart: emitTokenStart, - emitTokenEnd: emitTokenEnd, - changeEmitSourcePos: changeEmitSourcePos, - stopOverridingSpan: function () { return stopOverridingSpan = true; }, + emitNodeWithSourceMap: emitNodeWithSourceMap, + emitTokenWithSourceMap: emitTokenWithSourceMap, getText: getText, getSourceMappingURL: getSourceMappingURL }; function initialize(filePath, sourceMapFilePath, sourceFiles, isBundledEmit) { + if (disabled) { + return; + } if (sourceMapData) { reset(); } currentSourceFile = undefined; currentSourceText = undefined; - disableDepth = 0; sourceMapSourceIndex = -1; lastRecordedSourceMapSpan = undefined; lastEncodedSourceMapSpan = defaultLastEncodedSourceMapSpan; @@ -43774,6 +45181,9 @@ var ts; } } function reset() { + if (disabled) { + return; + } currentSourceFile = undefined; sourceMapDir = undefined; sourceMapSourceIndex = undefined; @@ -43781,38 +45191,6 @@ var ts; lastEncodedSourceMapSpan = undefined; lastEncodedNameIndex = undefined; sourceMapData = undefined; - disableDepth = 0; - } - function enable() { - if (disableDepth > 0) { - disableDepth--; - } - } - function disable() { - disableDepth++; - } - function updateLastEncodedAndRecordedSpans() { - if (modifyLastSourcePos) { - modifyLastSourcePos = false; - lastRecordedSourceMapSpan.emittedLine = lastEncodedSourceMapSpan.emittedLine; - lastRecordedSourceMapSpan.emittedColumn = lastEncodedSourceMapSpan.emittedColumn; - sourceMapData.sourceMapDecodedMappings.pop(); - lastEncodedSourceMapSpan = sourceMapData.sourceMapDecodedMappings.length ? - sourceMapData.sourceMapDecodedMappings[sourceMapData.sourceMapDecodedMappings.length - 1] : - defaultLastEncodedSourceMapSpan; - var sourceMapMappings = sourceMapData.sourceMapMappings; - var lenthToSet = sourceMapMappings.length - 1; - for (; lenthToSet >= 0; lenthToSet--) { - var currentChar = sourceMapMappings.charAt(lenthToSet); - if (currentChar === ",") { - break; - } - if (currentChar === ";" && lenthToSet !== 0 && sourceMapMappings.charAt(lenthToSet - 1) !== ";") { - break; - } - } - sourceMapData.sourceMapMappings = sourceMapMappings.substr(0, Math.max(0, lenthToSet)); - } } function encodeLastRecordedSourceMapSpan() { if (!lastRecordedSourceMapSpan || lastRecordedSourceMapSpan === lastEncodedSourceMapSpan) { @@ -43843,7 +45221,7 @@ var ts; sourceMapData.sourceMapDecodedMappings.push(lastEncodedSourceMapSpan); } function emitPos(pos) { - if (ts.positionIsSynthesized(pos) || disableDepth > 0) { + if (disabled || ts.positionIsSynthesized(pos)) { return; } if (extendedDiagnostics) { @@ -43868,84 +45246,68 @@ var ts; sourceColumn: sourceLinePos.character, sourceIndex: sourceMapSourceIndex }; - stopOverridingSpan = false; } - else if (!stopOverridingSpan) { + else { lastRecordedSourceMapSpan.sourceLine = sourceLinePos.line; lastRecordedSourceMapSpan.sourceColumn = sourceLinePos.character; lastRecordedSourceMapSpan.sourceIndex = sourceMapSourceIndex; } - updateLastEncodedAndRecordedSpans(); if (extendedDiagnostics) { ts.performance.mark("afterSourcemap"); ts.performance.measure("Source Map", "beforeSourcemap", "afterSourcemap"); } } - function getStartPosPastDecorators(range) { - var rangeHasDecorators = !!range.decorators; - return ts.skipTrivia(currentSourceText, rangeHasDecorators ? range.decorators.end : range.pos); - } - function emitStart(range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { - if (contextNode) { - if (!ignoreNodeCallback(contextNode)) { - range = getTextRangeCallback(contextNode) || range; - emitPos(getStartPosPastDecorators(range)); - } - if (ignoreChildrenCallback(contextNode)) { - disable(); - } - } - else { - emitPos(getStartPosPastDecorators(range)); + function emitNodeWithSourceMap(emitContext, node, emitCallback) { + if (disabled) { + return emitCallback(emitContext, node); } - } - function emitEnd(range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { - if (contextNode) { - if (ignoreChildrenCallback(contextNode)) { - enable(); - } - if (!ignoreNodeCallback(contextNode)) { - range = getTextRangeCallback(contextNode) || range; - emitPos(range.end); + if (node) { + var emitNode = node.emitNode; + var emitFlags = emitNode && emitNode.flags; + var _a = emitNode && emitNode.sourceMapRange || node, pos = _a.pos, end = _a.end; + if (node.kind !== 287 + && (emitFlags & 512) === 0 + && pos >= 0) { + emitPos(ts.skipTrivia(currentSourceText, pos)); + } + if (emitFlags & 2048) { + disabled = true; + emitCallback(emitContext, node); + disabled = false; } - } - else { - emitPos(range.end); - } - stopOverridingSpan = false; - } - function emitTokenStart(token, tokenStartPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { - if (contextNode) { - if (ignoreTokenCallback(contextNode, token)) { - return ts.skipTrivia(currentSourceText, tokenStartPos); + else { + emitCallback(emitContext, node); } - var range = getTokenTextRangeCallback(contextNode, token); - if (range) { - tokenStartPos = range.pos; + if (node.kind !== 287 + && (emitFlags & 1024) === 0 + && end >= 0) { + emitPos(end); } } - tokenStartPos = ts.skipTrivia(currentSourceText, tokenStartPos); - emitPos(tokenStartPos); - return tokenStartPos; } - function emitTokenEnd(token, tokenEndPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { - if (contextNode) { - if (ignoreTokenCallback(contextNode, token)) { - return tokenEndPos; - } - var range = getTokenTextRangeCallback(contextNode, token); - if (range) { - tokenEndPos = range.end; - } + function emitTokenWithSourceMap(node, token, tokenPos, emitCallback) { + if (disabled) { + return emitCallback(token, tokenPos); } - emitPos(tokenEndPos); - return tokenEndPos; - } - function changeEmitSourcePos() { - ts.Debug.assert(!modifyLastSourcePos); - modifyLastSourcePos = true; + var emitNode = node && node.emitNode; + var emitFlags = emitNode && emitNode.flags; + var range = emitNode && emitNode.tokenSourceMapRanges && emitNode.tokenSourceMapRanges[token]; + tokenPos = ts.skipTrivia(currentSourceText, range ? range.pos : tokenPos); + if ((emitFlags & 4096) === 0 && tokenPos >= 0) { + emitPos(tokenPos); + } + tokenPos = emitCallback(token, tokenPos); + if (range) + tokenPos = range.end; + if ((emitFlags & 8192) === 0 && tokenPos >= 0) { + emitPos(tokenPos); + } + return tokenPos; } function setSourceFile(sourceFile) { + if (disabled) { + return; + } currentSourceFile = sourceFile; currentSourceText = currentSourceFile.text; var sourcesDirectoryPath = compilerOptions.sourceRoot ? host.getCommonSourceDirectory() : sourceMapDir; @@ -43961,6 +45323,9 @@ var ts; } } function getText() { + if (disabled) { + return; + } encodeLastRecordedSourceMapSpan(); return ts.stringify({ version: 3, @@ -43973,6 +45338,9 @@ var ts; }); } function getSourceMappingURL() { + if (disabled) { + return; + } if (compilerOptions.inlineSourceMap) { var base64SourceMapText = ts.convertToBase64(getText()); return sourceMapData.jsSourceMappingURL = "data:application/json;base64," + base64SourceMapText; @@ -43982,46 +45350,7 @@ var ts; } } } - function createSourceMapWriterWithExtendedDiagnostics(host, writer) { - var _a = createSourceMapWriterWorker(host, writer), initialize = _a.initialize, reset = _a.reset, getSourceMapData = _a.getSourceMapData, setSourceFile = _a.setSourceFile, emitPos = _a.emitPos, emitStart = _a.emitStart, emitEnd = _a.emitEnd, emitTokenStart = _a.emitTokenStart, emitTokenEnd = _a.emitTokenEnd, changeEmitSourcePos = _a.changeEmitSourcePos, stopOverridingSpan = _a.stopOverridingSpan, getText = _a.getText, getSourceMappingURL = _a.getSourceMappingURL; - return { - initialize: initialize, - reset: reset, - getSourceMapData: getSourceMapData, - setSourceFile: setSourceFile, - emitPos: function (pos) { - ts.performance.mark("sourcemapStart"); - emitPos(pos); - ts.performance.measure("sourceMapTime", "sourcemapStart"); - }, - emitStart: function (range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { - ts.performance.mark("emitSourcemap:emitStart"); - emitStart(range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback); - ts.performance.measure("sourceMapTime", "emitSourcemap:emitStart"); - }, - emitEnd: function (range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { - ts.performance.mark("emitSourcemap:emitEnd"); - emitEnd(range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback); - ts.performance.measure("sourceMapTime", "emitSourcemap:emitEnd"); - }, - emitTokenStart: function (token, tokenStartPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { - ts.performance.mark("emitSourcemap:emitTokenStart"); - tokenStartPos = emitTokenStart(token, tokenStartPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback); - ts.performance.measure("sourceMapTime", "emitSourcemap:emitTokenStart"); - return tokenStartPos; - }, - emitTokenEnd: function (token, tokenEndPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { - ts.performance.mark("emitSourcemap:emitTokenEnd"); - tokenEndPos = emitTokenEnd(token, tokenEndPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback); - ts.performance.measure("sourceMapTime", "emitSourcemap:emitTokenEnd"); - return tokenEndPos; - }, - changeEmitSourcePos: changeEmitSourcePos, - stopOverridingSpan: stopOverridingSpan, - getText: getText, - getSourceMappingURL: getSourceMappingURL - }; - } + ts.createSourceMapWriter = createSourceMapWriter; var base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; function base64FormatEncode(inValue) { if (inValue < 64) { @@ -44071,20 +45400,22 @@ var ts; emitBodyWithDetachedComments: emitBodyWithDetachedComments, emitTrailingCommentsOfPosition: emitTrailingCommentsOfPosition }; - function emitNodeWithComments(node, emitCallback) { + function emitNodeWithComments(emitContext, node, emitCallback) { if (disabled) { - emitCallback(node); + emitCallback(emitContext, node); return; } if (node) { - var _a = node.commentRange || node, pos = _a.pos, end = _a.end; - var emitFlags = node.emitFlags; + var _a = ts.getCommentRange(node), pos = _a.pos, end = _a.end; + var emitFlags = ts.getEmitFlags(node); if ((pos < 0 && end < 0) || (pos === end)) { if (emitFlags & 65536) { - disableCommentsAndEmit(node, emitCallback); + disabled = true; + emitCallback(emitContext, node); + disabled = false; } else { - emitCallback(node); + emitCallback(emitContext, node); } } else { @@ -44113,10 +45444,12 @@ var ts; ts.performance.measure("commentTime", "preEmitNodeWithComment"); } if (emitFlags & 65536) { - disableCommentsAndEmit(node, emitCallback); + disabled = true; + emitCallback(emitContext, node); + disabled = false; } else { - emitCallback(node); + emitCallback(emitContext, node); } if (extendedDiagnostics) { ts.performance.mark("beginEmitNodeWithComment"); @@ -44138,7 +45471,7 @@ var ts; ts.performance.mark("preEmitBodyWithDetachedComments"); } var pos = detachedRange.pos, end = detachedRange.end; - var emitFlags = node.emitFlags; + var emitFlags = ts.getEmitFlags(node); var skipLeadingComments = pos < 0 || (emitFlags & 16384) !== 0; var skipTrailingComments = disabled || end < 0 || (emitFlags & 32768) !== 0; if (!skipLeadingComments) { @@ -44147,8 +45480,10 @@ var ts; if (extendedDiagnostics) { ts.performance.measure("commentTime", "preEmitBodyWithDetachedComments"); } - if (emitFlags & 65536) { - disableCommentsAndEmit(node, emitCallback); + if (emitFlags & 65536 && !disabled) { + disabled = true; + emitCallback(node); + disabled = false; } else { emitCallback(node); @@ -44256,16 +45591,6 @@ var ts; currentLineMap = ts.getLineStarts(currentSourceFile); detachedCommentsInfo = undefined; } - function disableCommentsAndEmit(node, emitCallback) { - if (disabled) { - emitCallback(node); - } - else { - disabled = true; - emitCallback(node); - disabled = false; - } - } function hasDetachedComments(pos) { return detachedCommentsInfo !== undefined && ts.lastOrUndefined(detachedCommentsInfo).nodePos === pos; } @@ -44311,7 +45636,9 @@ var ts; })(ts || (ts = {})); var ts; (function (ts) { - function emitFiles(resolver, host, targetSourceFile) { + var id = function (s) { return s; }; + var nullTransformers = [function (ctx) { return id; }]; + function emitFiles(resolver, host, targetSourceFile, emitOnlyDtsFiles) { var delimiters = createDelimiterMap(); var brackets = createBracketsMap(); var extendsHelper = "\nvar __extends = (this && this.__extends) || function (d, b) {\n for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];\n function __() { this.constructor = d; }\n d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\n};"; @@ -44319,7 +45646,7 @@ var ts; var decorateHelper = "\nvar __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {\n var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;\n if (typeof Reflect === \"object\" && typeof Reflect.decorate === \"function\") r = Reflect.decorate(decorators, target, key, desc);\n else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;\n return c > 3 && r && Object.defineProperty(target, key, r), r;\n};"; var metadataHelper = "\nvar __metadata = (this && this.__metadata) || function (k, v) {\n if (typeof Reflect === \"object\" && typeof Reflect.metadata === \"function\") return Reflect.metadata(k, v);\n};"; var paramHelper = "\nvar __param = (this && this.__param) || function (paramIndex, decorator) {\n return function (target, key) { decorator(target, key, paramIndex); }\n};"; - var awaiterHelper = "\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments)).next());\n });\n};"; + var awaiterHelper = "\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments)).next());\n });\n};"; var generatorHelper = "\nvar __generator = (this && this.__generator) || function (thisArg, body) {\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t;\n return { next: verb(0), \"throw\": verb(1), \"return\": verb(2) };\n function verb(n) { return function (v) { return step([n, v]); }; }\n function step(op) {\n if (f) throw new TypeError(\"Generator is already executing.\");\n while (_) try {\n if (f = 1, y && (t = y[op[0] & 2 ? \"return\" : op[0] ? \"throw\" : \"next\"]) && !(t = t.call(y, op[1])).done) return t;\n if (y = 0, t) op = [0, t.value];\n switch (op[0]) {\n case 0: case 1: t = op; break;\n case 4: _.label++; return { value: op[1], done: false };\n case 5: _.label++; y = op[1]; op = [0]; continue;\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\n default:\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\n if (t[2]) _.ops.pop();\n _.trys.pop(); continue;\n }\n op = body.call(thisArg, _);\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\n }\n};"; var exportStarHelper = "\nfunction __export(m) {\n for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];\n}"; var umdHelper = "\n(function (dependencies, factory) {\n if (typeof module === 'object' && typeof module.exports === 'object') {\n var v = factory(require, exports); if (v !== undefined) module.exports = v;\n }\n else if (typeof define === 'function' && define.amd) {\n define(dependencies, factory);\n }\n})"; @@ -44332,11 +45659,11 @@ var ts; var emittedFilesList = compilerOptions.listEmittedFiles ? [] : undefined; var emitterDiagnostics = ts.createDiagnosticCollection(); var newLine = host.getNewLine(); - var transformers = ts.getTransformers(compilerOptions); + var transformers = emitOnlyDtsFiles ? nullTransformers : ts.getTransformers(compilerOptions); var writer = ts.createTextWriter(newLine); var write = writer.write, writeLine = writer.writeLine, increaseIndent = writer.increaseIndent, decreaseIndent = writer.decreaseIndent; var sourceMap = ts.createSourceMapWriter(host, writer); - var emitStart = sourceMap.emitStart, emitEnd = sourceMap.emitEnd, emitTokenStart = sourceMap.emitTokenStart, emitTokenEnd = sourceMap.emitTokenEnd; + var emitNodeWithSourceMap = sourceMap.emitNodeWithSourceMap, emitTokenWithSourceMap = sourceMap.emitTokenWithSourceMap; var comments = ts.createCommentWriter(host, writer, sourceMap); var emitNodeWithComments = comments.emitNodeWithComments, emitBodyWithDetachedComments = comments.emitBodyWithDetachedComments, emitTrailingCommentsOfPosition = comments.emitTrailingCommentsOfPosition; var nodeIdToGeneratedName; @@ -44353,14 +45680,17 @@ var ts; var awaiterEmitted; var isOwnFileEmit; var emitSkipped = false; + var sourceFiles = ts.getSourceFilesToEmit(host, targetSourceFile); ts.performance.mark("beforeTransform"); - var transformed = ts.transformFiles(resolver, host, ts.getSourceFilesToEmit(host, targetSourceFile), transformers); + var _a = ts.transformFiles(resolver, host, sourceFiles, transformers), transformed = _a.transformed, emitNodeWithSubstitution = _a.emitNodeWithSubstitution, emitNodeWithNotification = _a.emitNodeWithNotification; ts.performance.measure("transformTime", "beforeTransform"); - var getTokenSourceMapRange = transformed.getTokenSourceMapRange, isSubstitutionEnabled = transformed.isSubstitutionEnabled, isEmitNotificationEnabled = transformed.isEmitNotificationEnabled, onSubstituteNode = transformed.onSubstituteNode, onEmitNode = transformed.onEmitNode; ts.performance.mark("beforePrint"); - ts.forEachTransformedEmitFile(host, transformed.getSourceFiles(), emitFile); - transformed.dispose(); + ts.forEachTransformedEmitFile(host, transformed, emitFile, emitOnlyDtsFiles); ts.performance.measure("printTime", "beforePrint"); + for (var _b = 0, sourceFiles_4 = sourceFiles; _b < sourceFiles_4.length; _b++) { + var sourceFile = sourceFiles_4[_b]; + ts.disposeEmitNodes(sourceFile); + } return { emitSkipped: emitSkipped, diagnostics: emitterDiagnostics.getDiagnostics(), @@ -44369,16 +45699,20 @@ var ts; }; function emitFile(jsFilePath, sourceMapFilePath, declarationFilePath, sourceFiles, isBundledEmit) { if (!host.isEmitBlocked(jsFilePath) && !compilerOptions.noEmit) { - printFile(jsFilePath, sourceMapFilePath, sourceFiles, isBundledEmit); + if (!emitOnlyDtsFiles) { + printFile(jsFilePath, sourceMapFilePath, sourceFiles, isBundledEmit); + } } else { emitSkipped = true; } if (declarationFilePath) { - emitSkipped = ts.writeDeclarationFile(declarationFilePath, ts.getOriginalSourceFiles(sourceFiles), isBundledEmit, host, resolver, emitterDiagnostics) || emitSkipped; + emitSkipped = ts.writeDeclarationFile(declarationFilePath, ts.getOriginalSourceFiles(sourceFiles), isBundledEmit, host, resolver, emitterDiagnostics, emitOnlyDtsFiles) || emitSkipped; } if (!emitSkipped && emittedFilesList) { - emittedFilesList.push(jsFilePath); + if (!emitOnlyDtsFiles) { + emittedFilesList.push(jsFilePath); + } if (sourceMapFilePath) { emittedFilesList.push(sourceMapFilePath); } @@ -44394,8 +45728,8 @@ var ts; generatedNameSet = ts.createMap(); isOwnFileEmit = !isBundledEmit; if (isBundledEmit && moduleKind) { - for (var _a = 0, sourceFiles_4 = sourceFiles; _a < sourceFiles_4.length; _a++) { - var sourceFile = sourceFiles_4[_a]; + for (var _a = 0, sourceFiles_5 = sourceFiles; _a < sourceFiles_5.length; _a++) { + var sourceFile = sourceFiles_5[_a]; emitEmitHelpers(sourceFile); } } @@ -44431,73 +45765,61 @@ var ts; currentFileIdentifiers = node.identifiers; sourceMap.setSourceFile(node); comments.setSourceFile(node); - emitNodeWithNotification(node, emitWorker); + pipelineEmitWithNotification(0, node); } function emit(node) { - emitNodeWithNotification(node, emitWithComments); - } - function emitWithComments(node) { - emitNodeWithComments(node, emitWithSourceMap); - } - function emitWithSourceMap(node) { - emitNodeWithSourceMap(node, emitWorker); + pipelineEmitWithNotification(3, node); } function emitIdentifierName(node) { - if (node) { - emitNodeWithNotification(node, emitIdentifierNameWithComments); - } - } - function emitIdentifierNameWithComments(node) { - emitNodeWithComments(node, emitWorker); + pipelineEmitWithNotification(2, node); } function emitExpression(node) { - emitNodeWithNotification(node, emitExpressionWithComments); - } - function emitExpressionWithComments(node) { - emitNodeWithComments(node, emitExpressionWithSourceMap); + pipelineEmitWithNotification(1, node); } - function emitExpressionWithSourceMap(node) { - emitNodeWithSourceMap(node, emitExpressionWorker); + function pipelineEmitWithNotification(emitContext, node) { + emitNodeWithNotification(emitContext, node, pipelineEmitWithComments); } - function emitNodeWithNotification(node, emitCallback) { - if (node) { - if (isEmitNotificationEnabled(node)) { - onEmitNode(node, emitCallback); - } - else { - emitCallback(node); - } + function pipelineEmitWithComments(emitContext, node) { + if (emitContext === 0) { + pipelineEmitWithSourceMap(emitContext, node); + return; } + emitNodeWithComments(emitContext, node, pipelineEmitWithSourceMap); } - function emitNodeWithSourceMap(node, emitCallback) { - if (node) { - emitStart(node, node, shouldSkipLeadingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange); - emitCallback(node); - emitEnd(node, node, shouldSkipTrailingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange); + function pipelineEmitWithSourceMap(emitContext, node) { + if (emitContext === 0 + || emitContext === 2) { + pipelineEmitWithSubstitution(emitContext, node); + return; } + emitNodeWithSourceMap(emitContext, node, pipelineEmitWithSubstitution); } - function getSourceMapRange(node) { - return node.sourceMapRange || node; + function pipelineEmitWithSubstitution(emitContext, node) { + emitNodeWithSubstitution(emitContext, node, pipelineEmitForContext); } - function shouldSkipLeadingCommentsForNode(node) { - return ts.isNotEmittedStatement(node) - || (node.emitFlags & 16384) !== 0; - } - function shouldSkipLeadingSourceMapForNode(node) { - return ts.isNotEmittedStatement(node) - || (node.emitFlags & 512) !== 0; - } - function shouldSkipTrailingSourceMapForNode(node) { - return ts.isNotEmittedStatement(node) - || (node.emitFlags & 1024) !== 0; + function pipelineEmitForContext(emitContext, node) { + switch (emitContext) { + case 0: return pipelineEmitInSourceFileContext(node); + case 2: return pipelineEmitInIdentifierNameContext(node); + case 3: return pipelineEmitInUnspecifiedContext(node); + case 1: return pipelineEmitInExpressionContext(node); + } } - function shouldSkipSourceMapForChildren(node) { - return (node.emitFlags & 2048) !== 0; + function pipelineEmitInSourceFileContext(node) { + var kind = node.kind; + switch (kind) { + case 256: + return emitSourceFile(node); + } } - function emitWorker(node) { - if (tryEmitSubstitute(node, emitWorker, false)) { - return; + function pipelineEmitInIdentifierNameContext(node) { + var kind = node.kind; + switch (kind) { + case 69: + return emitIdentifier(node); } + } + function pipelineEmitInUnspecifiedContext(node) { var kind = node.kind; switch (kind) { case 12: @@ -44524,7 +45846,8 @@ var ts; case 132: case 133: case 137: - return writeTokenNode(node); + writeTokenText(kind); + return; case 139: return emitQualifiedName(node); case 140: @@ -44673,2353 +45996,1725 @@ var ts; case 239: return; case 240: - return emitExternalModuleReference(node); - case 244: - return emitJsxText(node); - case 243: - return emitJsxOpeningElement(node); - case 245: - return emitJsxClosingElement(node); - case 246: - return emitJsxAttribute(node); - case 247: - return emitJsxSpreadAttribute(node); - case 248: - return emitJsxExpression(node); - case 249: - return emitCaseClause(node); - case 250: - return emitDefaultClause(node); - case 251: - return emitHeritageClause(node); - case 252: - return emitCatchClause(node); - case 253: - return emitPropertyAssignment(node); - case 254: - return emitShorthandPropertyAssignment(node); - case 255: - return emitEnumMember(node); - case 256: - return emitSourceFile(node); - } - if (ts.isExpression(node)) { - return emitExpressionWorker(node); - } - } - function emitExpressionWorker(node) { - if (tryEmitSubstitute(node, emitExpressionWorker, true)) { - return; - } - var kind = node.kind; - switch (kind) { - case 8: - return emitNumericLiteral(node); - case 9: - case 10: - case 11: - return emitLiteral(node); - case 69: - return emitIdentifier(node); - case 84: - case 93: - case 95: - case 99: - case 97: - return writeTokenNode(node); - case 170: - return emitArrayLiteralExpression(node); - case 171: - return emitObjectLiteralExpression(node); - case 172: - return emitPropertyAccessExpression(node); - case 173: - return emitElementAccessExpression(node); - case 174: - return emitCallExpression(node); - case 175: - return emitNewExpression(node); - case 176: - return emitTaggedTemplateExpression(node); - case 177: - return emitTypeAssertionExpression(node); - case 178: - return emitParenthesizedExpression(node); - case 179: - return emitFunctionExpression(node); - case 180: - return emitArrowFunction(node); - case 181: - return emitDeleteExpression(node); - case 182: - return emitTypeOfExpression(node); - case 183: - return emitVoidExpression(node); - case 184: - return emitAwaitExpression(node); - case 185: - return emitPrefixUnaryExpression(node); - case 186: - return emitPostfixUnaryExpression(node); - case 187: - return emitBinaryExpression(node); - case 188: - return emitConditionalExpression(node); - case 189: - return emitTemplateExpression(node); - case 190: - return emitYieldExpression(node); - case 191: - return emitSpreadElementExpression(node); - case 192: - return emitClassExpression(node); - case 193: - return; - case 195: - return emitAsExpression(node); - case 196: - return emitNonNullExpression(node); - case 241: - return emitJsxElement(node); - case 242: - return emitJsxSelfClosingElement(node); - case 288: - return emitPartiallyEmittedExpression(node); - } - } - function emitNumericLiteral(node) { - emitLiteral(node); - if (node.trailingComment) { - write(" /*" + node.trailingComment + "*/"); - } - } - function emitLiteral(node) { - var text = getLiteralTextOfNode(node); - if ((compilerOptions.sourceMap || compilerOptions.inlineSourceMap) - && (node.kind === 9 || ts.isTemplateLiteralKind(node.kind))) { - writer.writeLiteral(text); - } - else { - write(text); - } - } - function emitIdentifier(node) { - if (node.emitFlags & 16) { - writeLines(umdHelper); - } - else { - write(getTextOfNode(node, false)); - } - } - function emitQualifiedName(node) { - emitEntityName(node.left); - write("."); - emit(node.right); - } - function emitEntityName(node) { - if (node.kind === 69) { - emitExpression(node); - } - else { - emit(node); - } - } - function emitComputedPropertyName(node) { - write("["); - emitExpression(node.expression); - write("]"); - } - function emitTypeParameter(node) { - emit(node.name); - emitWithPrefix(" extends ", node.constraint); - } - function emitParameter(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - writeIfPresent(node.dotDotDotToken, "..."); - emit(node.name); - writeIfPresent(node.questionToken, "?"); - emitExpressionWithPrefix(" = ", node.initializer); - emitWithPrefix(": ", node.type); - } - function emitDecorator(decorator) { - write("@"); - emitExpression(decorator.expression); - } - function emitPropertySignature(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - emit(node.name); - writeIfPresent(node.questionToken, "?"); - emitWithPrefix(": ", node.type); - write(";"); - } - function emitPropertyDeclaration(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - emit(node.name); - emitWithPrefix(": ", node.type); - emitExpressionWithPrefix(" = ", node.initializer); - write(";"); - } - function emitMethodSignature(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - emit(node.name); - writeIfPresent(node.questionToken, "?"); - emitTypeParameters(node, node.typeParameters); - emitParameters(node, node.parameters); - emitWithPrefix(": ", node.type); - write(";"); - } - function emitMethodDeclaration(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - writeIfPresent(node.asteriskToken, "*"); - emit(node.name); - emitSignatureAndBody(node, emitSignatureHead); - } - function emitConstructor(node) { - emitModifiers(node, node.modifiers); - write("constructor"); - emitSignatureAndBody(node, emitSignatureHead); - } - function emitAccessorDeclaration(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - write(node.kind === 149 ? "get " : "set "); - emit(node.name); - emitSignatureAndBody(node, emitSignatureHead); - } - function emitCallSignature(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - emitTypeParameters(node, node.typeParameters); - emitParameters(node, node.parameters); - emitWithPrefix(": ", node.type); - write(";"); - } - function emitConstructSignature(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - write("new "); - emitTypeParameters(node, node.typeParameters); - emitParameters(node, node.parameters); - emitWithPrefix(": ", node.type); - write(";"); - } - function emitIndexSignature(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - emitParametersForIndexSignature(node, node.parameters); - emitWithPrefix(": ", node.type); - write(";"); - } - function emitSemicolonClassElement(node) { - write(";"); - } - function emitTypePredicate(node) { - emit(node.parameterName); - write(" is "); - emit(node.type); - } - function emitTypeReference(node) { - emit(node.typeName); - emitTypeArguments(node, node.typeArguments); - } - function emitFunctionType(node) { - emitTypeParameters(node, node.typeParameters); - emitParametersForArrow(node, node.parameters); - write(" => "); - emit(node.type); - } - function emitConstructorType(node) { - write("new "); - emitTypeParameters(node, node.typeParameters); - emitParametersForArrow(node, node.parameters); - write(" => "); - emit(node.type); - } - function emitTypeQuery(node) { - write("typeof "); - emit(node.exprName); - } - function emitTypeLiteral(node) { - write("{"); - emitList(node, node.members, 65); - write("}"); - } - function emitArrayType(node) { - emit(node.elementType); - write("[]"); - } - function emitTupleType(node) { - write("["); - emitList(node, node.elementTypes, 336); - write("]"); - } - function emitUnionType(node) { - emitList(node, node.types, 260); - } - function emitIntersectionType(node) { - emitList(node, node.types, 264); - } - function emitParenthesizedType(node) { - write("("); - emit(node.type); - write(")"); - } - function emitThisType(node) { - write("this"); - } - function emitLiteralType(node) { - emitExpression(node.literal); - } - function emitObjectBindingPattern(node) { - var elements = node.elements; - if (elements.length === 0) { - write("{}"); - } - else { - write("{"); - emitList(node, elements, 432); - write("}"); - } - } - function emitArrayBindingPattern(node) { - var elements = node.elements; - if (elements.length === 0) { - write("[]"); - } - else { - write("["); - emitList(node, node.elements, 304); - write("]"); - } - } - function emitBindingElement(node) { - emitWithSuffix(node.propertyName, ": "); - writeIfPresent(node.dotDotDotToken, "..."); - emit(node.name); - emitExpressionWithPrefix(" = ", node.initializer); - } - function emitArrayLiteralExpression(node) { - var elements = node.elements; - if (elements.length === 0) { - write("[]"); - } - else { - var preferNewLine = node.multiLine ? 32768 : 0; - emitExpressionList(node, elements, 4466 | preferNewLine); - } - } - function emitObjectLiteralExpression(node) { - var properties = node.properties; - if (properties.length === 0) { - write("{}"); - } - else { - var indentedFlag = node.emitFlags & 524288; - if (indentedFlag) { - increaseIndent(); - } - var preferNewLine = node.multiLine ? 32768 : 0; - var allowTrailingComma = languageVersion >= 1 ? 32 : 0; - emitList(node, properties, 978 | allowTrailingComma | preferNewLine); - if (indentedFlag) { - decreaseIndent(); - } - } - } - function emitPropertyAccessExpression(node) { - if (tryEmitConstantValue(node)) { - return; - } - var indentBeforeDot = false; - var indentAfterDot = false; - if (!(node.emitFlags & 1048576)) { - var dotRangeStart = node.expression.end; - var dotRangeEnd = ts.skipTrivia(currentText, node.expression.end) + 1; - var dotToken = { kind: 21, pos: dotRangeStart, end: dotRangeEnd }; - indentBeforeDot = needsIndentation(node, node.expression, dotToken); - indentAfterDot = needsIndentation(node, dotToken, node.name); - } - var shouldEmitDotDot = !indentBeforeDot && needsDotDotForPropertyAccess(node.expression); - emitExpression(node.expression); - increaseIndentIf(indentBeforeDot); - write(shouldEmitDotDot ? ".." : "."); - increaseIndentIf(indentAfterDot); - emit(node.name); - decreaseIndentIf(indentBeforeDot, indentAfterDot); - } - function needsDotDotForPropertyAccess(expression) { - if (expression.kind === 8) { - var text = getLiteralTextOfNode(expression); - return text.indexOf(ts.tokenToString(21)) < 0; - } - else { - var constantValue = tryGetConstEnumValue(expression); - return isFinite(constantValue) && Math.floor(constantValue) === constantValue; - } - } - function emitElementAccessExpression(node) { - if (tryEmitConstantValue(node)) { - return; - } - emitExpression(node.expression); - write("["); - emitExpression(node.argumentExpression); - write("]"); - } - function emitCallExpression(node) { - emitExpression(node.expression); - emitExpressionList(node, node.arguments, 1296); - } - function emitNewExpression(node) { - write("new "); - emitExpression(node.expression); - emitExpressionList(node, node.arguments, 9488); - } - function emitTaggedTemplateExpression(node) { - emitExpression(node.tag); - write(" "); - emitExpression(node.template); - } - function emitTypeAssertionExpression(node) { - if (node.type) { - write("<"); - emit(node.type); - write(">"); - } - emitExpression(node.expression); - } - function emitParenthesizedExpression(node) { - write("("); - emitExpression(node.expression); - write(")"); - } - function emitFunctionExpression(node) { - emitFunctionDeclarationOrExpression(node); - } - function emitArrowFunction(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - emitSignatureAndBody(node, emitArrowFunctionHead); - } - function emitArrowFunctionHead(node) { - emitTypeParameters(node, node.typeParameters); - emitParametersForArrow(node, node.parameters); - emitWithPrefix(": ", node.type); - write(" =>"); - } - function emitDeleteExpression(node) { - write("delete "); - emitExpression(node.expression); - } - function emitTypeOfExpression(node) { - write("typeof "); - emitExpression(node.expression); - } - function emitVoidExpression(node) { - write("void "); - emitExpression(node.expression); - } - function emitAwaitExpression(node) { - write("await "); - emitExpression(node.expression); - } - function emitPrefixUnaryExpression(node) { - writeTokenText(node.operator); - if (shouldEmitWhitespaceBeforeOperand(node)) { - write(" "); - } - emitExpression(node.operand); - } - function shouldEmitWhitespaceBeforeOperand(node) { - var operand = node.operand; - return operand.kind === 185 - && ((node.operator === 35 && (operand.operator === 35 || operand.operator === 41)) - || (node.operator === 36 && (operand.operator === 36 || operand.operator === 42))); - } - function emitPostfixUnaryExpression(node) { - emitExpression(node.operand); - writeTokenText(node.operator); - } - function emitBinaryExpression(node) { - var isCommaOperator = node.operatorToken.kind !== 24; - var indentBeforeOperator = needsIndentation(node, node.left, node.operatorToken); - var indentAfterOperator = needsIndentation(node, node.operatorToken, node.right); - emitExpression(node.left); - increaseIndentIf(indentBeforeOperator, isCommaOperator ? " " : undefined); - writeTokenText(node.operatorToken.kind); - increaseIndentIf(indentAfterOperator, " "); - emitExpression(node.right); - decreaseIndentIf(indentBeforeOperator, indentAfterOperator); - } - function emitConditionalExpression(node) { - var indentBeforeQuestion = needsIndentation(node, node.condition, node.questionToken); - var indentAfterQuestion = needsIndentation(node, node.questionToken, node.whenTrue); - var indentBeforeColon = needsIndentation(node, node.whenTrue, node.colonToken); - var indentAfterColon = needsIndentation(node, node.colonToken, node.whenFalse); - emitExpression(node.condition); - increaseIndentIf(indentBeforeQuestion, " "); - write("?"); - increaseIndentIf(indentAfterQuestion, " "); - emitExpression(node.whenTrue); - decreaseIndentIf(indentBeforeQuestion, indentAfterQuestion); - increaseIndentIf(indentBeforeColon, " "); - write(":"); - increaseIndentIf(indentAfterColon, " "); - emitExpression(node.whenFalse); - decreaseIndentIf(indentBeforeColon, indentAfterColon); - } - function emitTemplateExpression(node) { - emit(node.head); - emitList(node, node.templateSpans, 131072); - } - function emitYieldExpression(node) { - write(node.asteriskToken ? "yield*" : "yield"); - emitExpressionWithPrefix(" ", node.expression); - } - function emitSpreadElementExpression(node) { - write("..."); - emitExpression(node.expression); - } - function emitClassExpression(node) { - emitClassDeclarationOrExpression(node); - } - function emitExpressionWithTypeArguments(node) { - emitExpression(node.expression); - emitTypeArguments(node, node.typeArguments); - } - function emitAsExpression(node) { - emitExpression(node.expression); - if (node.type) { - write(" as "); - emit(node.type); + return emitExternalModuleReference(node); + case 244: + return emitJsxText(node); + case 243: + return emitJsxOpeningElement(node); + case 245: + return emitJsxClosingElement(node); + case 246: + return emitJsxAttribute(node); + case 247: + return emitJsxSpreadAttribute(node); + case 248: + return emitJsxExpression(node); + case 249: + return emitCaseClause(node); + case 250: + return emitDefaultClause(node); + case 251: + return emitHeritageClause(node); + case 252: + return emitCatchClause(node); + case 253: + return emitPropertyAssignment(node); + case 254: + return emitShorthandPropertyAssignment(node); + case 255: + return emitEnumMember(node); + } + if (ts.isExpression(node)) { + return pipelineEmitWithSubstitution(1, node); } } - function emitNonNullExpression(node) { - emitExpression(node.expression); - write("!"); + function pipelineEmitInExpressionContext(node) { + var kind = node.kind; + switch (kind) { + case 8: + return emitNumericLiteral(node); + case 9: + case 10: + case 11: + return emitLiteral(node); + case 69: + return emitIdentifier(node); + case 84: + case 93: + case 95: + case 99: + case 97: + writeTokenText(kind); + return; + case 170: + return emitArrayLiteralExpression(node); + case 171: + return emitObjectLiteralExpression(node); + case 172: + return emitPropertyAccessExpression(node); + case 173: + return emitElementAccessExpression(node); + case 174: + return emitCallExpression(node); + case 175: + return emitNewExpression(node); + case 176: + return emitTaggedTemplateExpression(node); + case 177: + return emitTypeAssertionExpression(node); + case 178: + return emitParenthesizedExpression(node); + case 179: + return emitFunctionExpression(node); + case 180: + return emitArrowFunction(node); + case 181: + return emitDeleteExpression(node); + case 182: + return emitTypeOfExpression(node); + case 183: + return emitVoidExpression(node); + case 184: + return emitAwaitExpression(node); + case 185: + return emitPrefixUnaryExpression(node); + case 186: + return emitPostfixUnaryExpression(node); + case 187: + return emitBinaryExpression(node); + case 188: + return emitConditionalExpression(node); + case 189: + return emitTemplateExpression(node); + case 190: + return emitYieldExpression(node); + case 191: + return emitSpreadElementExpression(node); + case 192: + return emitClassExpression(node); + case 193: + return; + case 195: + return emitAsExpression(node); + case 196: + return emitNonNullExpression(node); + case 241: + return emitJsxElement(node); + case 242: + return emitJsxSelfClosingElement(node); + case 288: + return emitPartiallyEmittedExpression(node); + } } - function emitTemplateSpan(node) { - emitExpression(node.expression); - emit(node.literal); + function emitNumericLiteral(node) { + emitLiteral(node); + if (node.trailingComment) { + write(" /*" + node.trailingComment + "*/"); + } } - function emitBlock(node, format) { - if (isSingleLineEmptyBlock(node)) { - writeToken(15, node.pos, node); - write(" "); - writeToken(16, node.statements.end, node); + function emitLiteral(node) { + var text = getLiteralTextOfNode(node); + if ((compilerOptions.sourceMap || compilerOptions.inlineSourceMap) + && (node.kind === 9 || ts.isTemplateLiteralKind(node.kind))) { + writer.writeLiteral(text); } else { - writeToken(15, node.pos, node); - emitBlockStatements(node); - writeToken(16, node.statements.end, node); + write(text); } } - function emitBlockStatements(node) { - if (node.emitFlags & 32) { - emitList(node, node.statements, 384); + function emitIdentifier(node) { + if (ts.getEmitFlags(node) & 16) { + writeLines(umdHelper); } else { - emitList(node, node.statements, 65); + write(getTextOfNode(node, false)); } } - function emitVariableStatement(node) { - emitModifiers(node, node.modifiers); - emit(node.declarationList); - write(";"); - } - function emitEmptyStatement(node) { - write(";"); - } - function emitExpressionStatement(node) { - emitExpression(node.expression); - write(";"); - } - function emitIfStatement(node) { - var openParenPos = writeToken(88, node.pos, node); - write(" "); - writeToken(17, openParenPos, node); - emitExpression(node.expression); - writeToken(18, node.expression.end, node); - emitEmbeddedStatement(node.thenStatement); - if (node.elseStatement) { - writeLine(); - writeToken(80, node.thenStatement.end, node); - if (node.elseStatement.kind === 203) { - write(" "); - emit(node.elseStatement); - } - else { - emitEmbeddedStatement(node.elseStatement); - } - } + function emitQualifiedName(node) { + emitEntityName(node.left); + write("."); + emit(node.right); } - function emitDoStatement(node) { - write("do"); - emitEmbeddedStatement(node.statement); - if (ts.isBlock(node.statement)) { - write(" "); + function emitEntityName(node) { + if (node.kind === 69) { + emitExpression(node); } else { - writeLine(); + emit(node); } - write("while ("); - emitExpression(node.expression); - write(");"); - } - function emitWhileStatement(node) { - write("while ("); - emitExpression(node.expression); - write(")"); - emitEmbeddedStatement(node.statement); - } - function emitForStatement(node) { - var openParenPos = writeToken(86, node.pos); - write(" "); - writeToken(17, openParenPos, node); - emitForBinding(node.initializer); - write(";"); - emitExpressionWithPrefix(" ", node.condition); - write(";"); - emitExpressionWithPrefix(" ", node.incrementor); - write(")"); - emitEmbeddedStatement(node.statement); - } - function emitForInStatement(node) { - var openParenPos = writeToken(86, node.pos); - write(" "); - writeToken(17, openParenPos); - emitForBinding(node.initializer); - write(" in "); - emitExpression(node.expression); - writeToken(18, node.expression.end); - emitEmbeddedStatement(node.statement); } - function emitForOfStatement(node) { - var openParenPos = writeToken(86, node.pos); - write(" "); - writeToken(17, openParenPos); - emitForBinding(node.initializer); - write(" of "); + function emitComputedPropertyName(node) { + write("["); emitExpression(node.expression); - writeToken(18, node.expression.end); - emitEmbeddedStatement(node.statement); + write("]"); } - function emitForBinding(node) { - if (node !== undefined) { - if (node.kind === 219) { - emit(node); - } - else { - emitExpression(node); - } - } + function emitTypeParameter(node) { + emit(node.name); + emitWithPrefix(" extends ", node.constraint); } - function emitContinueStatement(node) { - writeToken(75, node.pos); - emitWithPrefix(" ", node.label); - write(";"); + function emitParameter(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + writeIfPresent(node.dotDotDotToken, "..."); + emit(node.name); + writeIfPresent(node.questionToken, "?"); + emitExpressionWithPrefix(" = ", node.initializer); + emitWithPrefix(": ", node.type); } - function emitBreakStatement(node) { - writeToken(70, node.pos); - emitWithPrefix(" ", node.label); - write(";"); + function emitDecorator(decorator) { + write("@"); + emitExpression(decorator.expression); } - function emitReturnStatement(node) { - writeToken(94, node.pos, node); - emitExpressionWithPrefix(" ", node.expression); + function emitPropertySignature(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + emit(node.name); + writeIfPresent(node.questionToken, "?"); + emitWithPrefix(": ", node.type); write(";"); } - function emitWithStatement(node) { - write("with ("); - emitExpression(node.expression); - write(")"); - emitEmbeddedStatement(node.statement); - } - function emitSwitchStatement(node) { - var openParenPos = writeToken(96, node.pos); - write(" "); - writeToken(17, openParenPos); - emitExpression(node.expression); - writeToken(18, node.expression.end); - write(" "); - emit(node.caseBlock); - } - function emitLabeledStatement(node) { - emit(node.label); - write(": "); - emit(node.statement); - } - function emitThrowStatement(node) { - write("throw"); - emitExpressionWithPrefix(" ", node.expression); + function emitPropertyDeclaration(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + emit(node.name); + emitWithPrefix(": ", node.type); + emitExpressionWithPrefix(" = ", node.initializer); write(";"); } - function emitTryStatement(node) { - write("try "); - emit(node.tryBlock); - emit(node.catchClause); - if (node.finallyBlock) { - writeLine(); - write("finally "); - emit(node.finallyBlock); - } - } - function emitDebuggerStatement(node) { - writeToken(76, node.pos); + function emitMethodSignature(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + emit(node.name); + writeIfPresent(node.questionToken, "?"); + emitTypeParameters(node, node.typeParameters); + emitParameters(node, node.parameters); + emitWithPrefix(": ", node.type); write(";"); } - function emitVariableDeclaration(node) { + function emitMethodDeclaration(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + writeIfPresent(node.asteriskToken, "*"); emit(node.name); - emitExpressionWithPrefix(" = ", node.initializer); - } - function emitVariableDeclarationList(node) { - write(ts.isLet(node) ? "let " : ts.isConst(node) ? "const " : "var "); - emitList(node, node.declarations, 272); + emitSignatureAndBody(node, emitSignatureHead); } - function emitFunctionDeclaration(node) { - emitFunctionDeclarationOrExpression(node); + function emitConstructor(node) { + emitModifiers(node, node.modifiers); + write("constructor"); + emitSignatureAndBody(node, emitSignatureHead); } - function emitFunctionDeclarationOrExpression(node) { + function emitAccessorDeclaration(node) { emitDecorators(node, node.decorators); emitModifiers(node, node.modifiers); - write(node.asteriskToken ? "function* " : "function "); - emitIdentifierName(node.name); + write(node.kind === 149 ? "get " : "set "); + emit(node.name); emitSignatureAndBody(node, emitSignatureHead); } - function emitSignatureAndBody(node, emitSignatureHead) { - var body = node.body; - if (body) { - if (ts.isBlock(body)) { - var indentedFlag = node.emitFlags & 524288; - if (indentedFlag) { - increaseIndent(); - } - if (node.emitFlags & 4194304) { - emitSignatureHead(node); - emitBlockFunctionBody(node, body); - } - else { - var savedTempFlags = tempFlags; - tempFlags = 0; - emitSignatureHead(node); - emitBlockFunctionBody(node, body); - tempFlags = savedTempFlags; - } - if (indentedFlag) { - decreaseIndent(); - } - } - else { - emitSignatureHead(node); - write(" "); - emitExpression(body); - } - } - else { - emitSignatureHead(node); - write(";"); - } - } - function emitSignatureHead(node) { + function emitCallSignature(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); emitTypeParameters(node, node.typeParameters); emitParameters(node, node.parameters); emitWithPrefix(": ", node.type); + write(";"); } - function shouldEmitBlockFunctionBodyOnSingleLine(parentNode, body) { - if (body.emitFlags & 32) { - return true; - } - if (body.multiLine) { - return false; - } - if (!ts.nodeIsSynthesized(body) && !ts.rangeIsOnSingleLine(body, currentSourceFile)) { - return false; - } - if (shouldWriteLeadingLineTerminator(body, body.statements, 2) - || shouldWriteClosingLineTerminator(body, body.statements, 2)) { - return false; - } - var previousStatement; - for (var _a = 0, _b = body.statements; _a < _b.length; _a++) { - var statement = _b[_a]; - if (shouldWriteSeparatingLineTerminator(previousStatement, statement, 2)) { - return false; - } - previousStatement = statement; - } - return true; - } - function emitBlockFunctionBody(parentNode, body) { - write(" {"); - increaseIndent(); - emitBodyWithDetachedComments(body, body.statements, shouldEmitBlockFunctionBodyOnSingleLine(parentNode, body) - ? emitBlockFunctionBodyOnSingleLine - : emitBlockFunctionBodyWorker); - decreaseIndent(); - writeToken(16, body.statements.end, body); - } - function emitBlockFunctionBodyOnSingleLine(body) { - emitBlockFunctionBodyWorker(body, true); - } - function emitBlockFunctionBodyWorker(body, emitBlockFunctionBodyOnSingleLine) { - var statementOffset = emitPrologueDirectives(body.statements, true); - var helpersEmitted = emitHelpers(body); - if (statementOffset === 0 && !helpersEmitted && emitBlockFunctionBodyOnSingleLine) { - decreaseIndent(); - emitList(body, body.statements, 384); - increaseIndent(); - } - else { - emitList(body, body.statements, 1, statementOffset); - } - } - function emitClassDeclaration(node) { - emitClassDeclarationOrExpression(node); - } - function emitClassDeclarationOrExpression(node) { + function emitConstructSignature(node) { emitDecorators(node, node.decorators); emitModifiers(node, node.modifiers); - write("class"); - emitNodeWithPrefix(" ", node.name, emitIdentifierName); - var indentedFlag = node.emitFlags & 524288; - if (indentedFlag) { - increaseIndent(); - } + write("new "); emitTypeParameters(node, node.typeParameters); - emitList(node, node.heritageClauses, 256); - var savedTempFlags = tempFlags; - tempFlags = 0; - write(" {"); - emitList(node, node.members, 65); - write("}"); - if (indentedFlag) { - decreaseIndent(); - } - tempFlags = savedTempFlags; + emitParameters(node, node.parameters); + emitWithPrefix(": ", node.type); + write(";"); } - function emitInterfaceDeclaration(node) { + function emitIndexSignature(node) { emitDecorators(node, node.decorators); emitModifiers(node, node.modifiers); - write("interface "); - emit(node.name); + emitParametersForIndexSignature(node, node.parameters); + emitWithPrefix(": ", node.type); + write(";"); + } + function emitSemicolonClassElement(node) { + write(";"); + } + function emitTypePredicate(node) { + emit(node.parameterName); + write(" is "); + emit(node.type); + } + function emitTypeReference(node) { + emit(node.typeName); + emitTypeArguments(node, node.typeArguments); + } + function emitFunctionType(node) { emitTypeParameters(node, node.typeParameters); - emitList(node, node.heritageClauses, 256); - write(" {"); - emitList(node, node.members, 65); - write("}"); + emitParametersForArrow(node, node.parameters); + write(" => "); + emit(node.type); } - function emitTypeAliasDeclaration(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - write("type "); - emit(node.name); + function emitConstructorType(node) { + write("new "); emitTypeParameters(node, node.typeParameters); - write(" = "); + emitParametersForArrow(node, node.parameters); + write(" => "); emit(node.type); - write(";"); } - function emitEnumDeclaration(node) { - emitModifiers(node, node.modifiers); - write("enum "); - emit(node.name); - var savedTempFlags = tempFlags; - tempFlags = 0; - write(" {"); - emitList(node, node.members, 81); + function emitTypeQuery(node) { + write("typeof "); + emit(node.exprName); + } + function emitTypeLiteral(node) { + write("{"); + emitList(node, node.members, 65); write("}"); - tempFlags = savedTempFlags; } - function emitModuleDeclaration(node) { - emitModifiers(node, node.modifiers); - write(node.flags & 16 ? "namespace " : "module "); - emit(node.name); - var body = node.body; - while (body.kind === 225) { - write("."); - emit(body.name); - body = body.body; - } - write(" "); - emit(body); + function emitArrayType(node) { + emit(node.elementType); + write("[]"); + } + function emitTupleType(node) { + write("["); + emitList(node, node.elementTypes, 336); + write("]"); + } + function emitUnionType(node) { + emitList(node, node.types, 260); + } + function emitIntersectionType(node) { + emitList(node, node.types, 264); + } + function emitParenthesizedType(node) { + write("("); + emit(node.type); + write(")"); + } + function emitThisType(node) { + write("this"); } - function emitModuleBlock(node) { - if (isSingleLineEmptyBlock(node)) { - write("{ }"); + function emitLiteralType(node) { + emitExpression(node.literal); + } + function emitObjectBindingPattern(node) { + var elements = node.elements; + if (elements.length === 0) { + write("{}"); } else { - var savedTempFlags = tempFlags; - tempFlags = 0; write("{"); - increaseIndent(); - emitBlockStatements(node); + emitList(node, elements, 432); write("}"); - tempFlags = savedTempFlags; } } - function emitCaseBlock(node) { - writeToken(15, node.pos); - emitList(node, node.clauses, 65); - writeToken(16, node.clauses.end); + function emitArrayBindingPattern(node) { + var elements = node.elements; + if (elements.length === 0) { + write("[]"); + } + else { + write("["); + emitList(node, node.elements, 304); + write("]"); + } } - function emitImportEqualsDeclaration(node) { - emitModifiers(node, node.modifiers); - write("import "); + function emitBindingElement(node) { + emitWithSuffix(node.propertyName, ": "); + writeIfPresent(node.dotDotDotToken, "..."); emit(node.name); - write(" = "); - emitModuleReference(node.moduleReference); - write(";"); + emitExpressionWithPrefix(" = ", node.initializer); } - function emitModuleReference(node) { - if (node.kind === 69) { - emitExpression(node); + function emitArrayLiteralExpression(node) { + var elements = node.elements; + if (elements.length === 0) { + write("[]"); } else { - emit(node); + var preferNewLine = node.multiLine ? 32768 : 0; + emitExpressionList(node, elements, 4466 | preferNewLine); } } - function emitImportDeclaration(node) { - emitModifiers(node, node.modifiers); - write("import "); - if (node.importClause) { - emit(node.importClause); - write(" from "); + function emitObjectLiteralExpression(node) { + var properties = node.properties; + if (properties.length === 0) { + write("{}"); } - emitExpression(node.moduleSpecifier); - write(";"); - } - function emitImportClause(node) { - emit(node.name); - if (node.name && node.namedBindings) { - write(", "); + else { + var indentedFlag = ts.getEmitFlags(node) & 524288; + if (indentedFlag) { + increaseIndent(); + } + var preferNewLine = node.multiLine ? 32768 : 0; + var allowTrailingComma = languageVersion >= 1 ? 32 : 0; + emitList(node, properties, 978 | allowTrailingComma | preferNewLine); + if (indentedFlag) { + decreaseIndent(); + } } - emit(node.namedBindings); } - function emitNamespaceImport(node) { - write("* as "); - emit(node.name); - } - function emitNamedImports(node) { - emitNamedImportsOrExports(node); - } - function emitImportSpecifier(node) { - emitImportOrExportSpecifier(node); - } - function emitExportAssignment(node) { - write(node.isExportEquals ? "export = " : "export default "); + function emitPropertyAccessExpression(node) { + var indentBeforeDot = false; + var indentAfterDot = false; + if (!(ts.getEmitFlags(node) & 1048576)) { + var dotRangeStart = node.expression.end; + var dotRangeEnd = ts.skipTrivia(currentText, node.expression.end) + 1; + var dotToken = { kind: 21, pos: dotRangeStart, end: dotRangeEnd }; + indentBeforeDot = needsIndentation(node, node.expression, dotToken); + indentAfterDot = needsIndentation(node, dotToken, node.name); + } emitExpression(node.expression); - write(";"); + increaseIndentIf(indentBeforeDot); + var shouldEmitDotDot = !indentBeforeDot && needsDotDotForPropertyAccess(node.expression); + write(shouldEmitDotDot ? ".." : "."); + increaseIndentIf(indentAfterDot); + emit(node.name); + decreaseIndentIf(indentBeforeDot, indentAfterDot); } - function emitExportDeclaration(node) { - write("export "); - if (node.exportClause) { - emit(node.exportClause); - } - else { - write("*"); + function needsDotDotForPropertyAccess(expression) { + if (expression.kind === 8) { + var text = getLiteralTextOfNode(expression); + return text.indexOf(ts.tokenToString(21)) < 0; } - if (node.moduleSpecifier) { - write(" from "); - emitExpression(node.moduleSpecifier); + else if (ts.isPropertyAccessExpression(expression) || ts.isElementAccessExpression(expression)) { + var constantValue = ts.getConstantValue(expression); + return isFinite(constantValue) + && Math.floor(constantValue) === constantValue + && compilerOptions.removeComments; } - write(";"); } - function emitNamedExports(node) { - emitNamedImportsOrExports(node); + function emitElementAccessExpression(node) { + emitExpression(node.expression); + write("["); + emitExpression(node.argumentExpression); + write("]"); } - function emitExportSpecifier(node) { - emitImportOrExportSpecifier(node); + function emitCallExpression(node) { + emitExpression(node.expression); + emitExpressionList(node, node.arguments, 1296); } - function emitNamedImportsOrExports(node) { - write("{"); - emitList(node, node.elements, 432); - write("}"); + function emitNewExpression(node) { + write("new "); + emitExpression(node.expression); + emitExpressionList(node, node.arguments, 9488); } - function emitImportOrExportSpecifier(node) { - if (node.propertyName) { - emit(node.propertyName); - write(" as "); + function emitTaggedTemplateExpression(node) { + emitExpression(node.tag); + write(" "); + emitExpression(node.template); + } + function emitTypeAssertionExpression(node) { + if (node.type) { + write("<"); + emit(node.type); + write(">"); } - emit(node.name); + emitExpression(node.expression); } - function emitExternalModuleReference(node) { - write("require("); + function emitParenthesizedExpression(node) { + write("("); emitExpression(node.expression); write(")"); } - function emitJsxElement(node) { - emit(node.openingElement); - emitList(node, node.children, 131072); - emit(node.closingElement); - } - function emitJsxSelfClosingElement(node) { - write("<"); - emitJsxTagName(node.tagName); - write(" "); - emitList(node, node.attributes, 131328); - write("/>"); - } - function emitJsxOpeningElement(node) { - write("<"); - emitJsxTagName(node.tagName); - writeIfAny(node.attributes, " "); - emitList(node, node.attributes, 131328); - write(">"); - } - function emitJsxText(node) { - writer.writeLiteral(getTextOfNode(node, true)); + function emitFunctionExpression(node) { + emitFunctionDeclarationOrExpression(node); } - function emitJsxClosingElement(node) { - write(""); + function emitArrowFunction(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + emitSignatureAndBody(node, emitArrowFunctionHead); } - function emitJsxAttribute(node) { - emit(node.name); - emitWithPrefix("=", node.initializer); + function emitArrowFunctionHead(node) { + emitTypeParameters(node, node.typeParameters); + emitParametersForArrow(node, node.parameters); + emitWithPrefix(": ", node.type); + write(" =>"); } - function emitJsxSpreadAttribute(node) { - write("{..."); + function emitDeleteExpression(node) { + write("delete "); emitExpression(node.expression); - write("}"); - } - function emitJsxExpression(node) { - if (node.expression) { - write("{"); - emitExpression(node.expression); - write("}"); - } } - function emitJsxTagName(node) { - if (node.kind === 69) { - emitExpression(node); - } - else { - emit(node); - } + function emitTypeOfExpression(node) { + write("typeof "); + emitExpression(node.expression); } - function emitCaseClause(node) { - write("case "); + function emitVoidExpression(node) { + write("void "); emitExpression(node.expression); - write(":"); - emitCaseOrDefaultClauseStatements(node, node.statements); } - function emitDefaultClause(node) { - write("default:"); - emitCaseOrDefaultClauseStatements(node, node.statements); + function emitAwaitExpression(node) { + write("await "); + emitExpression(node.expression); } - function emitCaseOrDefaultClauseStatements(parentNode, statements) { - var emitAsSingleStatement = statements.length === 1 && - (ts.nodeIsSynthesized(parentNode) || - ts.nodeIsSynthesized(statements[0]) || - ts.rangeStartPositionsAreOnSameLine(parentNode, statements[0], currentSourceFile)); - if (emitAsSingleStatement) { + function emitPrefixUnaryExpression(node) { + writeTokenText(node.operator); + if (shouldEmitWhitespaceBeforeOperand(node)) { write(" "); - emit(statements[0]); - } - else { - emitList(parentNode, statements, 81985); } + emitExpression(node.operand); } - function emitHeritageClause(node) { - write(" "); - writeTokenText(node.token); - write(" "); - emitList(node, node.types, 272); - } - function emitCatchClause(node) { - writeLine(); - var openParenPos = writeToken(72, node.pos); - write(" "); - writeToken(17, openParenPos); - emit(node.variableDeclaration); - writeToken(18, node.variableDeclaration ? node.variableDeclaration.end : openParenPos); - write(" "); - emit(node.block); + function shouldEmitWhitespaceBeforeOperand(node) { + var operand = node.operand; + return operand.kind === 185 + && ((node.operator === 35 && (operand.operator === 35 || operand.operator === 41)) + || (node.operator === 36 && (operand.operator === 36 || operand.operator === 42))); } - function emitPropertyAssignment(node) { - emit(node.name); - write(": "); - var initializer = node.initializer; - if (!shouldSkipLeadingCommentsForNode(initializer)) { - var commentRange = initializer.commentRange || initializer; - emitTrailingCommentsOfPosition(commentRange.pos); - } - emitExpression(initializer); + function emitPostfixUnaryExpression(node) { + emitExpression(node.operand); + writeTokenText(node.operator); } - function emitShorthandPropertyAssignment(node) { - emit(node.name); - if (node.objectAssignmentInitializer) { - write(" = "); - emitExpression(node.objectAssignmentInitializer); - } + function emitBinaryExpression(node) { + var isCommaOperator = node.operatorToken.kind !== 24; + var indentBeforeOperator = needsIndentation(node, node.left, node.operatorToken); + var indentAfterOperator = needsIndentation(node, node.operatorToken, node.right); + emitExpression(node.left); + increaseIndentIf(indentBeforeOperator, isCommaOperator ? " " : undefined); + writeTokenText(node.operatorToken.kind); + increaseIndentIf(indentAfterOperator, " "); + emitExpression(node.right); + decreaseIndentIf(indentBeforeOperator, indentAfterOperator); } - function emitEnumMember(node) { - emit(node.name); - emitExpressionWithPrefix(" = ", node.initializer); + function emitConditionalExpression(node) { + var indentBeforeQuestion = needsIndentation(node, node.condition, node.questionToken); + var indentAfterQuestion = needsIndentation(node, node.questionToken, node.whenTrue); + var indentBeforeColon = needsIndentation(node, node.whenTrue, node.colonToken); + var indentAfterColon = needsIndentation(node, node.colonToken, node.whenFalse); + emitExpression(node.condition); + increaseIndentIf(indentBeforeQuestion, " "); + write("?"); + increaseIndentIf(indentAfterQuestion, " "); + emitExpression(node.whenTrue); + decreaseIndentIf(indentBeforeQuestion, indentAfterQuestion); + increaseIndentIf(indentBeforeColon, " "); + write(":"); + increaseIndentIf(indentAfterColon, " "); + emitExpression(node.whenFalse); + decreaseIndentIf(indentBeforeColon, indentAfterColon); } - function emitSourceFile(node) { - writeLine(); - emitShebang(); - emitBodyWithDetachedComments(node, node.statements, emitSourceFileWorker); + function emitTemplateExpression(node) { + emit(node.head); + emitList(node, node.templateSpans, 131072); } - function emitSourceFileWorker(node) { - var statements = node.statements; - var statementOffset = emitPrologueDirectives(statements); - var savedTempFlags = tempFlags; - tempFlags = 0; - emitHelpers(node); - emitList(node, statements, 1, statementOffset); - tempFlags = savedTempFlags; + function emitYieldExpression(node) { + write(node.asteriskToken ? "yield*" : "yield"); + emitExpressionWithPrefix(" ", node.expression); } - function emitPartiallyEmittedExpression(node) { + function emitSpreadElementExpression(node) { + write("..."); emitExpression(node.expression); } - function emitPrologueDirectives(statements, startWithNewLine) { - for (var i = 0; i < statements.length; i++) { - if (ts.isPrologueDirective(statements[i])) { - if (startWithNewLine || i > 0) { - writeLine(); - } - emit(statements[i]); - } - else { - return i; - } - } - return statements.length; + function emitClassExpression(node) { + emitClassDeclarationOrExpression(node); } - function emitHelpers(node) { - var emitFlags = node.emitFlags; - var helpersEmitted = false; - if (emitFlags & 1) { - helpersEmitted = emitEmitHelpers(currentSourceFile); - } - if (emitFlags & 2) { - writeLines(exportStarHelper); - helpersEmitted = true; - } - if (emitFlags & 4) { - writeLines(superHelper); - helpersEmitted = true; - } - if (emitFlags & 8) { - writeLines(advancedSuperHelper); - helpersEmitted = true; - } - return helpersEmitted; + function emitExpressionWithTypeArguments(node) { + emitExpression(node.expression); + emitTypeArguments(node, node.typeArguments); } - function emitEmitHelpers(node) { - if (compilerOptions.noEmitHelpers) { - return false; - } - if (compilerOptions.importHelpers - && (ts.isExternalModule(node) || compilerOptions.isolatedModules)) { - return false; - } - var helpersEmitted = false; - if ((languageVersion < 2) && (!extendsEmitted && node.flags & 1024)) { - writeLines(extendsHelper); - extendsEmitted = true; - helpersEmitted = true; - } - if (compilerOptions.jsx !== 1 && !assignEmitted && (node.flags & 16384)) { - writeLines(assignHelper); - assignEmitted = true; - } - if (!decorateEmitted && node.flags & 2048) { - writeLines(decorateHelper); - if (compilerOptions.emitDecoratorMetadata) { - writeLines(metadataHelper); - } - decorateEmitted = true; - helpersEmitted = true; - } - if (!paramEmitted && node.flags & 4096) { - writeLines(paramHelper); - paramEmitted = true; - helpersEmitted = true; - } - if (!awaiterEmitted && node.flags & 8192) { - writeLines(awaiterHelper); - if (languageVersion < 2) { - writeLines(generatorHelper); - } - awaiterEmitted = true; - helpersEmitted = true; - } - if (helpersEmitted) { - writeLine(); + function emitAsExpression(node) { + emitExpression(node.expression); + if (node.type) { + write(" as "); + emit(node.type); } - return helpersEmitted; } - function writeLines(text) { - var lines = text.split(/\r\n|\r|\n/g); - for (var i = 0; i < lines.length; i++) { - var line = lines[i]; - if (line.length) { - if (i > 0) { - writeLine(); - } - write(line); - } - } + function emitNonNullExpression(node) { + emitExpression(node.expression); + write("!"); } - function emitShebang() { - var shebang = ts.getShebang(currentText); - if (shebang) { - write(shebang); - writeLine(); - } + function emitTemplateSpan(node) { + emitExpression(node.expression); + emit(node.literal); } - function emitModifiers(node, modifiers) { - if (modifiers && modifiers.length) { - emitList(node, modifiers, 256); + function emitBlock(node, format) { + if (isSingleLineEmptyBlock(node)) { + writeToken(15, node.pos, node); write(" "); + writeToken(16, node.statements.end, node); + } + else { + writeToken(15, node.pos, node); + emitBlockStatements(node); + writeToken(16, node.statements.end, node); } } - function emitWithPrefix(prefix, node) { - emitNodeWithPrefix(prefix, node, emit); + function emitBlockStatements(node) { + if (ts.getEmitFlags(node) & 32) { + emitList(node, node.statements, 384); + } + else { + emitList(node, node.statements, 65); + } } - function emitExpressionWithPrefix(prefix, node) { - emitNodeWithPrefix(prefix, node, emitExpression); + function emitVariableStatement(node) { + emitModifiers(node, node.modifiers); + emit(node.declarationList); + write(";"); } - function emitNodeWithPrefix(prefix, node, emit) { - if (node) { - write(prefix); - emit(node); - } + function emitEmptyStatement(node) { + write(";"); } - function emitWithSuffix(node, suffix) { - if (node) { - emit(node); - write(suffix); - } + function emitExpressionStatement(node) { + emitExpression(node.expression); + write(";"); } - function tryEmitSubstitute(node, emitNode, isExpression) { - if (isSubstitutionEnabled(node) && (node.emitFlags & 128) === 0) { - var substitute = onSubstituteNode(node, isExpression); - if (substitute !== node) { - substitute.emitFlags |= 128; - emitNode(substitute); - return true; + function emitIfStatement(node) { + var openParenPos = writeToken(88, node.pos, node); + write(" "); + writeToken(17, openParenPos, node); + emitExpression(node.expression); + writeToken(18, node.expression.end, node); + emitEmbeddedStatement(node.thenStatement); + if (node.elseStatement) { + writeLine(); + writeToken(80, node.thenStatement.end, node); + if (node.elseStatement.kind === 203) { + write(" "); + emit(node.elseStatement); } - } - return false; - } - function tryEmitConstantValue(node) { - var constantValue = tryGetConstEnumValue(node); - if (constantValue !== undefined) { - write(String(constantValue)); - if (!compilerOptions.removeComments) { - var propertyName = ts.isPropertyAccessExpression(node) - ? ts.declarationNameToString(node.name) - : getTextOfNode(node.argumentExpression); - write(" /* " + propertyName + " */"); + else { + emitEmbeddedStatement(node.elseStatement); } - return true; } - return false; } - function emitEmbeddedStatement(node) { - if (ts.isBlock(node)) { + function emitDoStatement(node) { + write("do"); + emitEmbeddedStatement(node.statement); + if (ts.isBlock(node.statement)) { write(" "); - emit(node); } else { writeLine(); - increaseIndent(); - emit(node); - decreaseIndent(); } + write("while ("); + emitExpression(node.expression); + write(");"); } - function emitDecorators(parentNode, decorators) { - emitList(parentNode, decorators, 24577); + function emitWhileStatement(node) { + write("while ("); + emitExpression(node.expression); + write(")"); + emitEmbeddedStatement(node.statement); } - function emitTypeArguments(parentNode, typeArguments) { - emitList(parentNode, typeArguments, 26960); + function emitForStatement(node) { + var openParenPos = writeToken(86, node.pos); + write(" "); + writeToken(17, openParenPos, node); + emitForBinding(node.initializer); + write(";"); + emitExpressionWithPrefix(" ", node.condition); + write(";"); + emitExpressionWithPrefix(" ", node.incrementor); + write(")"); + emitEmbeddedStatement(node.statement); } - function emitTypeParameters(parentNode, typeParameters) { - emitList(parentNode, typeParameters, 26960); + function emitForInStatement(node) { + var openParenPos = writeToken(86, node.pos); + write(" "); + writeToken(17, openParenPos); + emitForBinding(node.initializer); + write(" in "); + emitExpression(node.expression); + writeToken(18, node.expression.end); + emitEmbeddedStatement(node.statement); } - function emitParameters(parentNode, parameters) { - emitList(parentNode, parameters, 1360); + function emitForOfStatement(node) { + var openParenPos = writeToken(86, node.pos); + write(" "); + writeToken(17, openParenPos); + emitForBinding(node.initializer); + write(" of "); + emitExpression(node.expression); + writeToken(18, node.expression.end); + emitEmbeddedStatement(node.statement); } - function emitParametersForArrow(parentNode, parameters) { - if (parameters && - parameters.length === 1 && - parameters[0].type === undefined && - parameters[0].pos === parentNode.pos) { - emit(parameters[0]); - } - else { - emitParameters(parentNode, parameters); + function emitForBinding(node) { + if (node !== undefined) { + if (node.kind === 219) { + emit(node); + } + else { + emitExpression(node); + } } } - function emitParametersForIndexSignature(parentNode, parameters) { - emitList(parentNode, parameters, 4432); + function emitContinueStatement(node) { + writeToken(75, node.pos); + emitWithPrefix(" ", node.label); + write(";"); } - function emitList(parentNode, children, format, start, count) { - emitNodeList(emit, parentNode, children, format, start, count); + function emitBreakStatement(node) { + writeToken(70, node.pos); + emitWithPrefix(" ", node.label); + write(";"); } - function emitExpressionList(parentNode, children, format, start, count) { - emitNodeList(emitExpression, parentNode, children, format, start, count); + function emitReturnStatement(node) { + writeToken(94, node.pos, node); + emitExpressionWithPrefix(" ", node.expression); + write(";"); } - function emitNodeList(emit, parentNode, children, format, start, count) { - if (start === void 0) { start = 0; } - if (count === void 0) { count = children ? children.length - start : 0; } - var isUndefined = children === undefined; - if (isUndefined && format & 8192) { - return; - } - var isEmpty = isUndefined || children.length === 0 || start >= children.length || count === 0; - if (isEmpty && format & 16384) { - return; - } - if (format & 7680) { - write(getOpeningBracket(format)); - } - if (isEmpty) { - if (format & 1) { - writeLine(); - } - else if (format & 128) { - write(" "); - } + function emitWithStatement(node) { + write("with ("); + emitExpression(node.expression); + write(")"); + emitEmbeddedStatement(node.statement); + } + function emitSwitchStatement(node) { + var openParenPos = writeToken(96, node.pos); + write(" "); + writeToken(17, openParenPos); + emitExpression(node.expression); + writeToken(18, node.expression.end); + write(" "); + emit(node.caseBlock); + } + function emitLabeledStatement(node) { + emit(node.label); + write(": "); + emit(node.statement); + } + function emitThrowStatement(node) { + write("throw"); + emitExpressionWithPrefix(" ", node.expression); + write(";"); + } + function emitTryStatement(node) { + write("try "); + emit(node.tryBlock); + emit(node.catchClause); + if (node.finallyBlock) { + writeLine(); + write("finally "); + emit(node.finallyBlock); } - else { - var mayEmitInterveningComments = (format & 131072) === 0; - var shouldEmitInterveningComments = mayEmitInterveningComments; - if (shouldWriteLeadingLineTerminator(parentNode, children, format)) { - writeLine(); - shouldEmitInterveningComments = false; - } - else if (format & 128) { - write(" "); - } - if (format & 64) { - increaseIndent(); - } - var previousSibling = void 0; - var shouldDecreaseIndentAfterEmit = void 0; - var delimiter = getDelimiter(format); - for (var i = 0; i < count; i++) { - var child = children[start + i]; - if (previousSibling) { - write(delimiter); - if (shouldWriteSeparatingLineTerminator(previousSibling, child, format)) { - if ((format & (3 | 64)) === 0) { - increaseIndent(); - shouldDecreaseIndentAfterEmit = true; - } - writeLine(); - shouldEmitInterveningComments = false; - } - else if (previousSibling && format & 256) { - write(" "); - } + } + function emitDebuggerStatement(node) { + writeToken(76, node.pos); + write(";"); + } + function emitVariableDeclaration(node) { + emit(node.name); + emitExpressionWithPrefix(" = ", node.initializer); + } + function emitVariableDeclarationList(node) { + write(ts.isLet(node) ? "let " : ts.isConst(node) ? "const " : "var "); + emitList(node, node.declarations, 272); + } + function emitFunctionDeclaration(node) { + emitFunctionDeclarationOrExpression(node); + } + function emitFunctionDeclarationOrExpression(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + write(node.asteriskToken ? "function* " : "function "); + emitIdentifierName(node.name); + emitSignatureAndBody(node, emitSignatureHead); + } + function emitSignatureAndBody(node, emitSignatureHead) { + var body = node.body; + if (body) { + if (ts.isBlock(body)) { + var indentedFlag = ts.getEmitFlags(node) & 524288; + if (indentedFlag) { + increaseIndent(); } - if (shouldEmitInterveningComments) { - var commentRange = child.commentRange || child; - emitTrailingCommentsOfPosition(commentRange.pos); + if (ts.getEmitFlags(node) & 4194304) { + emitSignatureHead(node); + emitBlockFunctionBody(node, body); } else { - shouldEmitInterveningComments = mayEmitInterveningComments; + var savedTempFlags = tempFlags; + tempFlags = 0; + emitSignatureHead(node); + emitBlockFunctionBody(node, body); + tempFlags = savedTempFlags; } - emit(child); - if (shouldDecreaseIndentAfterEmit) { + if (indentedFlag) { decreaseIndent(); - shouldDecreaseIndentAfterEmit = false; } - previousSibling = child; - } - var hasTrailingComma = (format & 32) && children.hasTrailingComma; - if (format & 16 && hasTrailingComma) { - write(","); - } - if (format & 64) { - decreaseIndent(); - } - if (shouldWriteClosingLineTerminator(parentNode, children, format)) { - writeLine(); } - else if (format & 128) { + else { + emitSignatureHead(node); write(" "); + emitExpression(body); } } - if (format & 7680) { - write(getClosingBracket(format)); - } - } - function writeIfAny(nodes, text) { - if (nodes && nodes.length > 0) { - write(text); - } - } - function writeIfPresent(node, text) { - if (node !== undefined) { - write(text); - } - } - function writeToken(token, pos, contextNode) { - var tokenStartPos = emitTokenStart(token, pos, contextNode, shouldSkipLeadingSourceMapForToken, getTokenSourceMapRange); - var tokenEndPos = writeTokenText(token, tokenStartPos); - return emitTokenEnd(token, tokenEndPos, contextNode, shouldSkipTrailingSourceMapForToken, getTokenSourceMapRange); - } - function shouldSkipLeadingSourceMapForToken(contextNode) { - return (contextNode.emitFlags & 4096) !== 0; - } - function shouldSkipTrailingSourceMapForToken(contextNode) { - return (contextNode.emitFlags & 8192) !== 0; - } - function writeTokenText(token, pos) { - var tokenString = ts.tokenToString(token); - write(tokenString); - return ts.positionIsSynthesized(pos) ? -1 : pos + tokenString.length; - } - function writeTokenNode(node) { - if (node) { - emitStart(node, node, shouldSkipLeadingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange); - writeTokenText(node.kind); - emitEnd(node, node, shouldSkipTrailingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange); - } - } - function increaseIndentIf(value, valueToWriteWhenNotIndenting) { - if (value) { - increaseIndent(); - writeLine(); - } - else if (valueToWriteWhenNotIndenting) { - write(valueToWriteWhenNotIndenting); + else { + emitSignatureHead(node); + write(";"); } } - function decreaseIndentIf(value1, value2) { - if (value1) { - decreaseIndent(); - } - if (value2) { - decreaseIndent(); - } + function emitSignatureHead(node) { + emitTypeParameters(node, node.typeParameters); + emitParameters(node, node.parameters); + emitWithPrefix(": ", node.type); } - function shouldWriteLeadingLineTerminator(parentNode, children, format) { - if (format & 1) { + function shouldEmitBlockFunctionBodyOnSingleLine(parentNode, body) { + if (ts.getEmitFlags(body) & 32) { return true; } - if (format & 2) { - if (format & 32768) { - return true; - } - var firstChild = children[0]; - if (firstChild === undefined) { - return !ts.rangeIsOnSingleLine(parentNode, currentSourceFile); - } - else if (ts.positionIsSynthesized(parentNode.pos) || ts.nodeIsSynthesized(firstChild)) { - return synthesizedNodeStartsOnNewLine(firstChild, format); - } - else { - return !ts.rangeStartPositionsAreOnSameLine(parentNode, firstChild, currentSourceFile); - } + if (body.multiLine) { + return false; } - else { + if (!ts.nodeIsSynthesized(body) && !ts.rangeIsOnSingleLine(body, currentSourceFile)) { return false; } - } - function shouldWriteSeparatingLineTerminator(previousNode, nextNode, format) { - if (format & 1) { - return true; + if (shouldWriteLeadingLineTerminator(body, body.statements, 2) + || shouldWriteClosingLineTerminator(body, body.statements, 2)) { + return false; } - else if (format & 2) { - if (previousNode === undefined || nextNode === undefined) { + var previousStatement; + for (var _a = 0, _b = body.statements; _a < _b.length; _a++) { + var statement = _b[_a]; + if (shouldWriteSeparatingLineTerminator(previousStatement, statement, 2)) { return false; } - else if (ts.nodeIsSynthesized(previousNode) || ts.nodeIsSynthesized(nextNode)) { - return synthesizedNodeStartsOnNewLine(previousNode, format) || synthesizedNodeStartsOnNewLine(nextNode, format); - } - else { - return !ts.rangeEndIsOnSameLineAsRangeStart(previousNode, nextNode, currentSourceFile); - } - } - else { - return nextNode.startsOnNewLine; + previousStatement = statement; } + return true; } - function shouldWriteClosingLineTerminator(parentNode, children, format) { - if (format & 1) { - return (format & 65536) === 0; - } - else if (format & 2) { - if (format & 32768) { - return true; - } - var lastChild = ts.lastOrUndefined(children); - if (lastChild === undefined) { - return !ts.rangeIsOnSingleLine(parentNode, currentSourceFile); - } - else if (ts.positionIsSynthesized(parentNode.pos) || ts.nodeIsSynthesized(lastChild)) { - return synthesizedNodeStartsOnNewLine(lastChild, format); - } - else { - return !ts.rangeEndPositionsAreOnSameLine(parentNode, lastChild, currentSourceFile); - } + function emitBlockFunctionBody(parentNode, body) { + write(" {"); + increaseIndent(); + emitBodyWithDetachedComments(body, body.statements, shouldEmitBlockFunctionBodyOnSingleLine(parentNode, body) + ? emitBlockFunctionBodyOnSingleLine + : emitBlockFunctionBodyWorker); + decreaseIndent(); + writeToken(16, body.statements.end, body); + } + function emitBlockFunctionBodyOnSingleLine(body) { + emitBlockFunctionBodyWorker(body, true); + } + function emitBlockFunctionBodyWorker(body, emitBlockFunctionBodyOnSingleLine) { + var statementOffset = emitPrologueDirectives(body.statements, true); + var helpersEmitted = emitHelpers(body); + if (statementOffset === 0 && !helpersEmitted && emitBlockFunctionBodyOnSingleLine) { + decreaseIndent(); + emitList(body, body.statements, 384); + increaseIndent(); } else { - return false; + emitList(body, body.statements, 1, statementOffset); } } - function synthesizedNodeStartsOnNewLine(node, format) { - if (ts.nodeIsSynthesized(node)) { - var startsOnNewLine = node.startsOnNewLine; - if (startsOnNewLine === undefined) { - return (format & 32768) !== 0; - } - return startsOnNewLine; - } - return (format & 32768) !== 0; + function emitClassDeclaration(node) { + emitClassDeclarationOrExpression(node); } - function needsIndentation(parent, node1, node2) { - parent = skipSynthesizedParentheses(parent); - node1 = skipSynthesizedParentheses(node1); - node2 = skipSynthesizedParentheses(node2); - if (node2.startsOnNewLine) { - return true; + function emitClassDeclarationOrExpression(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + write("class"); + emitNodeWithPrefix(" ", node.name, emitIdentifierName); + var indentedFlag = ts.getEmitFlags(node) & 524288; + if (indentedFlag) { + increaseIndent(); } - return !ts.nodeIsSynthesized(parent) - && !ts.nodeIsSynthesized(node1) - && !ts.nodeIsSynthesized(node2) - && !ts.rangeEndIsOnSameLineAsRangeStart(node1, node2, currentSourceFile); - } - function skipSynthesizedParentheses(node) { - while (node.kind === 178 && ts.nodeIsSynthesized(node)) { - node = node.expression; + emitTypeParameters(node, node.typeParameters); + emitList(node, node.heritageClauses, 256); + var savedTempFlags = tempFlags; + tempFlags = 0; + write(" {"); + emitList(node, node.members, 65); + write("}"); + if (indentedFlag) { + decreaseIndent(); } - return node; + tempFlags = savedTempFlags; } - function getTextOfNode(node, includeTrivia) { - if (ts.isGeneratedIdentifier(node)) { - return getGeneratedIdentifier(node); - } - else if (ts.isIdentifier(node) && (ts.nodeIsSynthesized(node) || !node.parent)) { - return ts.unescapeIdentifier(node.text); + function emitInterfaceDeclaration(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + write("interface "); + emit(node.name); + emitTypeParameters(node, node.typeParameters); + emitList(node, node.heritageClauses, 256); + write(" {"); + emitList(node, node.members, 65); + write("}"); + } + function emitTypeAliasDeclaration(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + write("type "); + emit(node.name); + emitTypeParameters(node, node.typeParameters); + write(" = "); + emit(node.type); + write(";"); + } + function emitEnumDeclaration(node) { + emitModifiers(node, node.modifiers); + write("enum "); + emit(node.name); + var savedTempFlags = tempFlags; + tempFlags = 0; + write(" {"); + emitList(node, node.members, 81); + write("}"); + tempFlags = savedTempFlags; + } + function emitModuleDeclaration(node) { + emitModifiers(node, node.modifiers); + write(node.flags & 16 ? "namespace " : "module "); + emit(node.name); + var body = node.body; + while (body.kind === 225) { + write("."); + emit(body.name); + body = body.body; } - else if (node.kind === 9 && node.textSourceNode) { - return getTextOfNode(node.textSourceNode, includeTrivia); + write(" "); + emit(body); + } + function emitModuleBlock(node) { + if (isEmptyBlock(node)) { + write("{ }"); } - else if (ts.isLiteralExpression(node) && (ts.nodeIsSynthesized(node) || !node.parent)) { - return node.text; + else { + var savedTempFlags = tempFlags; + tempFlags = 0; + write("{"); + increaseIndent(); + emitBlockStatements(node); + write("}"); + tempFlags = savedTempFlags; } - return ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, node, includeTrivia); } - function getLiteralTextOfNode(node) { - if (node.kind === 9 && node.textSourceNode) { - var textSourceNode = node.textSourceNode; - if (ts.isIdentifier(textSourceNode)) { - return "\"" + ts.escapeNonAsciiCharacters(ts.escapeString(getTextOfNode(textSourceNode))) + "\""; - } - else { - return getLiteralTextOfNode(textSourceNode); - } + function emitCaseBlock(node) { + writeToken(15, node.pos); + emitList(node, node.clauses, 65); + writeToken(16, node.clauses.end); + } + function emitImportEqualsDeclaration(node) { + emitModifiers(node, node.modifiers); + write("import "); + emit(node.name); + write(" = "); + emitModuleReference(node.moduleReference); + write(";"); + } + function emitModuleReference(node) { + if (node.kind === 69) { + emitExpression(node); + } + else { + emit(node); } - return ts.getLiteralText(node, currentSourceFile, languageVersion); } - function tryGetConstEnumValue(node) { - if (compilerOptions.isolatedModules) { - return undefined; + function emitImportDeclaration(node) { + emitModifiers(node, node.modifiers); + write("import "); + if (node.importClause) { + emit(node.importClause); + write(" from "); } - return ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node) - ? resolver.getConstantValue(node) - : undefined; + emitExpression(node.moduleSpecifier); + write(";"); } - function isSingleLineEmptyBlock(block) { - return !block.multiLine - && block.statements.length === 0 - && ts.rangeEndIsOnSameLineAsRangeStart(block, block, currentSourceFile); + function emitImportClause(node) { + emit(node.name); + if (node.name && node.namedBindings) { + write(", "); + } + emit(node.namedBindings); } - function isUniqueName(name) { - return !resolver.hasGlobalName(name) && - !ts.hasProperty(currentFileIdentifiers, name) && - !ts.hasProperty(generatedNameSet, name); + function emitNamespaceImport(node) { + write("* as "); + emit(node.name); } - function isUniqueLocalName(name, container) { - for (var node = container; ts.isNodeDescendantOf(node, container); node = node.nextContainer) { - if (node.locals && ts.hasProperty(node.locals, name)) { - if (node.locals[name].flags & (107455 | 1048576 | 8388608)) { - return false; - } - } - } - return true; + function emitNamedImports(node) { + emitNamedImportsOrExports(node); } - function makeTempVariableName(flags) { - if (flags && !(tempFlags & flags)) { - var name_43 = flags === 268435456 ? "_i" : "_n"; - if (isUniqueName(name_43)) { - tempFlags |= flags; - return name_43; - } + function emitImportSpecifier(node) { + emitImportOrExportSpecifier(node); + } + function emitExportAssignment(node) { + write(node.isExportEquals ? "export = " : "export default "); + emitExpression(node.expression); + write(";"); + } + function emitExportDeclaration(node) { + write("export "); + if (node.exportClause) { + emit(node.exportClause); } - while (true) { - var count = tempFlags & 268435455; - tempFlags++; - if (count !== 8 && count !== 13) { - var name_44 = count < 26 - ? "_" + String.fromCharCode(97 + count) - : "_" + (count - 26); - if (isUniqueName(name_44)) { - return name_44; - } - } + else { + write("*"); } - } - function makeUniqueName(baseName) { - if (baseName.charCodeAt(baseName.length - 1) !== 95) { - baseName += "_"; + if (node.moduleSpecifier) { + write(" from "); + emitExpression(node.moduleSpecifier); } - var i = 1; - while (true) { - var generatedName = baseName + i; - if (isUniqueName(generatedName)) { - return generatedNameSet[generatedName] = generatedName; - } - i++; + write(";"); + } + function emitNamedExports(node) { + emitNamedImportsOrExports(node); + } + function emitExportSpecifier(node) { + emitImportOrExportSpecifier(node); + } + function emitNamedImportsOrExports(node) { + write("{"); + emitList(node, node.elements, 432); + write("}"); + } + function emitImportOrExportSpecifier(node) { + if (node.propertyName) { + emit(node.propertyName); + write(" as "); } + emit(node.name); } - function generateNameForModuleOrEnum(node) { - var name = getTextOfNode(node.name); - return isUniqueLocalName(name, node) ? name : makeUniqueName(name); + function emitExternalModuleReference(node) { + write("require("); + emitExpression(node.expression); + write(")"); } - function generateNameForImportOrExportDeclaration(node) { - var expr = ts.getExternalModuleName(node); - var baseName = expr.kind === 9 ? - ts.escapeIdentifier(ts.makeIdentifierFromModuleName(expr.text)) : "module"; - return makeUniqueName(baseName); + function emitJsxElement(node) { + emit(node.openingElement); + emitList(node, node.children, 131072); + emit(node.closingElement); } - function generateNameForExportDefault() { - return makeUniqueName("default"); + function emitJsxSelfClosingElement(node) { + write("<"); + emitJsxTagName(node.tagName); + write(" "); + emitList(node, node.attributes, 131328); + write("/>"); } - function generateNameForClassExpression() { - return makeUniqueName("class"); + function emitJsxOpeningElement(node) { + write("<"); + emitJsxTagName(node.tagName); + writeIfAny(node.attributes, " "); + emitList(node, node.attributes, 131328); + write(">"); } - function generateNameForNode(node) { - switch (node.kind) { - case 69: - return makeUniqueName(getTextOfNode(node)); - case 225: - case 224: - return generateNameForModuleOrEnum(node); - case 230: - case 236: - return generateNameForImportOrExportDeclaration(node); - case 220: - case 221: - case 235: - return generateNameForExportDefault(); - case 192: - return generateNameForClassExpression(); - default: - return makeTempVariableName(0); - } + function emitJsxText(node) { + writer.writeLiteral(getTextOfNode(node, true)); } - function generateName(name) { - switch (name.autoGenerateKind) { - case 1: - return makeTempVariableName(0); - case 2: - return makeTempVariableName(268435456); - case 3: - return makeUniqueName(name.text); - } - ts.Debug.fail("Unsupported GeneratedIdentifierKind."); + function emitJsxClosingElement(node) { + write(""); } - function getNodeForGeneratedName(name) { - var autoGenerateId = name.autoGenerateId; - var node = name; - var original = node.original; - while (original) { - node = original; - if (ts.isIdentifier(node) - && node.autoGenerateKind === 4 - && node.autoGenerateId !== autoGenerateId) { - break; - } - original = node.original; + function emitJsxAttribute(node) { + emit(node.name); + emitWithPrefix("=", node.initializer); + } + function emitJsxSpreadAttribute(node) { + write("{..."); + emitExpression(node.expression); + write("}"); + } + function emitJsxExpression(node) { + if (node.expression) { + write("{"); + emitExpression(node.expression); + write("}"); } - return node; } - function getGeneratedIdentifier(name) { - if (name.autoGenerateKind === 4) { - var node = getNodeForGeneratedName(name); - var nodeId = ts.getNodeId(node); - return nodeIdToGeneratedName[nodeId] || (nodeIdToGeneratedName[nodeId] = ts.unescapeIdentifier(generateNameForNode(node))); + function emitJsxTagName(node) { + if (node.kind === 69) { + emitExpression(node); } else { - var autoGenerateId = name.autoGenerateId; - return autoGeneratedIdToGeneratedName[autoGenerateId] || (autoGeneratedIdToGeneratedName[autoGenerateId] = ts.unescapeIdentifier(generateName(name))); + emit(node); } } - function createDelimiterMap() { - var delimiters = []; - delimiters[0] = ""; - delimiters[16] = ","; - delimiters[4] = " |"; - delimiters[8] = " &"; - return delimiters; + function emitCaseClause(node) { + write("case "); + emitExpression(node.expression); + write(":"); + emitCaseOrDefaultClauseStatements(node, node.statements); } - function getDelimiter(format) { - return delimiters[format & 28]; + function emitDefaultClause(node) { + write("default:"); + emitCaseOrDefaultClauseStatements(node, node.statements); } - function createBracketsMap() { - var brackets = []; - brackets[512] = ["{", "}"]; - brackets[1024] = ["(", ")"]; - brackets[2048] = ["<", ">"]; - brackets[4096] = ["[", "]"]; - return brackets; + function emitCaseOrDefaultClauseStatements(parentNode, statements) { + var emitAsSingleStatement = statements.length === 1 && + (ts.nodeIsSynthesized(parentNode) || + ts.nodeIsSynthesized(statements[0]) || + ts.rangeStartPositionsAreOnSameLine(parentNode, statements[0], currentSourceFile)); + if (emitAsSingleStatement) { + write(" "); + emit(statements[0]); + } + else { + emitList(parentNode, statements, 81985); + } } - function getOpeningBracket(format) { - return brackets[format & 7680][0]; + function emitHeritageClause(node) { + write(" "); + writeTokenText(node.token); + write(" "); + emitList(node, node.types, 272); } - function getClosingBracket(format) { - return brackets[format & 7680][1]; + function emitCatchClause(node) { + writeLine(); + var openParenPos = writeToken(72, node.pos); + write(" "); + writeToken(17, openParenPos); + emit(node.variableDeclaration); + writeToken(18, node.variableDeclaration ? node.variableDeclaration.end : openParenPos); + write(" "); + emit(node.block); } - } - ts.emitFiles = emitFiles; -})(ts || (ts = {})); -var ts; -(function (ts) { - ts.version = "2.1.0"; - var emptyArray = []; - function findConfigFile(searchPath, fileExists, configName) { - if (configName === void 0) { configName = "tsconfig.json"; } - while (true) { - var fileName = ts.combinePaths(searchPath, configName); - if (fileExists(fileName)) { - return fileName; - } - var parentPath = ts.getDirectoryPath(searchPath); - if (parentPath === searchPath) { - break; + function emitPropertyAssignment(node) { + emit(node.name); + write(": "); + var initializer = node.initializer; + if ((ts.getEmitFlags(initializer) & 16384) === 0) { + var commentRange = ts.getCommentRange(initializer); + emitTrailingCommentsOfPosition(commentRange.pos); } - searchPath = parentPath; + emitExpression(initializer); } - return undefined; - } - ts.findConfigFile = findConfigFile; - function resolveTripleslashReference(moduleName, containingFile) { - var basePath = ts.getDirectoryPath(containingFile); - var referencedFileName = ts.isRootedDiskPath(moduleName) ? moduleName : ts.combinePaths(basePath, moduleName); - return ts.normalizePath(referencedFileName); - } - ts.resolveTripleslashReference = resolveTripleslashReference; - function computeCommonSourceDirectoryOfFilenames(fileNames, currentDirectory, getCanonicalFileName) { - var commonPathComponents; - var failed = ts.forEach(fileNames, function (sourceFile) { - var sourcePathComponents = ts.getNormalizedPathComponents(sourceFile, currentDirectory); - sourcePathComponents.pop(); - if (!commonPathComponents) { - commonPathComponents = sourcePathComponents; - return; + function emitShorthandPropertyAssignment(node) { + emit(node.name); + if (node.objectAssignmentInitializer) { + write(" = "); + emitExpression(node.objectAssignmentInitializer); } - for (var i = 0, n = Math.min(commonPathComponents.length, sourcePathComponents.length); i < n; i++) { - if (getCanonicalFileName(commonPathComponents[i]) !== getCanonicalFileName(sourcePathComponents[i])) { - if (i === 0) { - return true; + } + function emitEnumMember(node) { + emit(node.name); + emitExpressionWithPrefix(" = ", node.initializer); + } + function emitSourceFile(node) { + writeLine(); + emitShebang(); + emitBodyWithDetachedComments(node, node.statements, emitSourceFileWorker); + } + function emitSourceFileWorker(node) { + var statements = node.statements; + var statementOffset = emitPrologueDirectives(statements); + var savedTempFlags = tempFlags; + tempFlags = 0; + emitHelpers(node); + emitList(node, statements, 1, statementOffset); + tempFlags = savedTempFlags; + } + function emitPartiallyEmittedExpression(node) { + emitExpression(node.expression); + } + function emitPrologueDirectives(statements, startWithNewLine) { + for (var i = 0; i < statements.length; i++) { + if (ts.isPrologueDirective(statements[i])) { + if (startWithNewLine || i > 0) { + writeLine(); } - commonPathComponents.length = i; - break; + emit(statements[i]); + } + else { + return i; } } - if (sourcePathComponents.length < commonPathComponents.length) { - commonPathComponents.length = sourcePathComponents.length; - } - }); - if (failed) { - return ""; + return statements.length; } - if (!commonPathComponents) { - return currentDirectory; + function emitHelpers(node) { + var emitFlags = ts.getEmitFlags(node); + var helpersEmitted = false; + if (emitFlags & 1) { + helpersEmitted = emitEmitHelpers(currentSourceFile); + } + if (emitFlags & 2) { + writeLines(exportStarHelper); + helpersEmitted = true; + } + if (emitFlags & 4) { + writeLines(superHelper); + helpersEmitted = true; + } + if (emitFlags & 8) { + writeLines(advancedSuperHelper); + helpersEmitted = true; + } + return helpersEmitted; } - return ts.getNormalizedPathFromPathComponents(commonPathComponents); - } - ts.computeCommonSourceDirectoryOfFilenames = computeCommonSourceDirectoryOfFilenames; - function trace(host, message) { - host.trace(ts.formatMessage.apply(undefined, arguments)); - } - function isTraceEnabled(compilerOptions, host) { - return compilerOptions.traceResolution && host.trace !== undefined; - } - function hasZeroOrOneAsteriskCharacter(str) { - var seenAsterisk = false; - for (var i = 0; i < str.length; i++) { - if (str.charCodeAt(i) === 42) { - if (!seenAsterisk) { - seenAsterisk = true; + function emitEmitHelpers(node) { + if (compilerOptions.noEmitHelpers) { + return false; + } + if (compilerOptions.importHelpers + && (ts.isExternalModule(node) || compilerOptions.isolatedModules)) { + return false; + } + var helpersEmitted = false; + if ((languageVersion < 2) && (!extendsEmitted && node.flags & 1024)) { + writeLines(extendsHelper); + extendsEmitted = true; + helpersEmitted = true; + } + if (compilerOptions.jsx !== 1 && !assignEmitted && (node.flags & 16384)) { + writeLines(assignHelper); + assignEmitted = true; + } + if (!decorateEmitted && node.flags & 2048) { + writeLines(decorateHelper); + if (compilerOptions.emitDecoratorMetadata) { + writeLines(metadataHelper); } - else { - return false; + decorateEmitted = true; + helpersEmitted = true; + } + if (!paramEmitted && node.flags & 4096) { + writeLines(paramHelper); + paramEmitted = true; + helpersEmitted = true; + } + if (!awaiterEmitted && node.flags & 8192) { + writeLines(awaiterHelper); + if (languageVersion < 2) { + writeLines(generatorHelper); } + awaiterEmitted = true; + helpersEmitted = true; + } + if (helpersEmitted) { + writeLine(); } + return helpersEmitted; } - return true; - } - ts.hasZeroOrOneAsteriskCharacter = hasZeroOrOneAsteriskCharacter; - function createResolvedModule(resolvedFileName, isExternalLibraryImport, failedLookupLocations) { - return { resolvedModule: resolvedFileName ? { resolvedFileName: resolvedFileName, isExternalLibraryImport: isExternalLibraryImport } : undefined, failedLookupLocations: failedLookupLocations }; - } - function moduleHasNonRelativeName(moduleName) { - return !(ts.isRootedDiskPath(moduleName) || ts.isExternalModuleNameRelative(moduleName)); - } - function tryReadTypesSection(packageJsonPath, baseDirectory, state) { - var jsonContent = readJson(packageJsonPath, state.host); - function tryReadFromField(fieldName) { - if (ts.hasProperty(jsonContent, fieldName)) { - var typesFile = jsonContent[fieldName]; - if (typeof typesFile === "string") { - var typesFilePath_1 = ts.normalizePath(ts.combinePaths(baseDirectory, typesFile)); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.package_json_has_0_field_1_that_references_2, fieldName, typesFile, typesFilePath_1); - } - return typesFilePath_1; - } - else { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Expected_type_of_0_field_in_package_json_to_be_string_got_1, fieldName, typeof typesFile); + function writeLines(text) { + var lines = text.split(/\r\n|\r|\n/g); + for (var i = 0; i < lines.length; i++) { + var line = lines[i]; + if (line.length) { + if (i > 0) { + writeLine(); } + write(line); } } } - var typesFilePath = tryReadFromField("typings") || tryReadFromField("types"); - if (typesFilePath) { - return typesFilePath; + function emitShebang() { + var shebang = ts.getShebang(currentText); + if (shebang) { + write(shebang); + writeLine(); + } } - if (state.compilerOptions.allowJs && jsonContent.main && typeof jsonContent.main === "string") { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.No_types_specified_in_package_json_but_allowJs_is_set_so_returning_main_value_of_0, jsonContent.main); + function emitModifiers(node, modifiers) { + if (modifiers && modifiers.length) { + emitList(node, modifiers, 256); + write(" "); } - var mainFilePath = ts.normalizePath(ts.combinePaths(baseDirectory, jsonContent.main)); - return mainFilePath; } - return undefined; - } - function readJson(path, host) { - try { - var jsonText = host.readFile(path); - return jsonText ? JSON.parse(jsonText) : {}; + function emitWithPrefix(prefix, node) { + emitNodeWithPrefix(prefix, node, emit); } - catch (e) { - return {}; + function emitExpressionWithPrefix(prefix, node) { + emitNodeWithPrefix(prefix, node, emitExpression); + } + function emitNodeWithPrefix(prefix, node, emit) { + if (node) { + write(prefix); + emit(node); + } + } + function emitWithSuffix(node, suffix) { + if (node) { + emit(node); + write(suffix); + } + } + function emitEmbeddedStatement(node) { + if (ts.isBlock(node)) { + write(" "); + emit(node); + } + else { + writeLine(); + increaseIndent(); + emit(node); + decreaseIndent(); + } } - } - var typeReferenceExtensions = [".d.ts"]; - function getEffectiveTypeRoots(options, host) { - if (options.typeRoots) { - return options.typeRoots; + function emitDecorators(parentNode, decorators) { + emitList(parentNode, decorators, 24577); } - var currentDirectory; - if (options.configFilePath) { - currentDirectory = ts.getDirectoryPath(options.configFilePath); + function emitTypeArguments(parentNode, typeArguments) { + emitList(parentNode, typeArguments, 26960); } - else if (host.getCurrentDirectory) { - currentDirectory = host.getCurrentDirectory(); + function emitTypeParameters(parentNode, typeParameters) { + emitList(parentNode, typeParameters, 26960); } - return currentDirectory && getDefaultTypeRoots(currentDirectory, host); - } - ts.getEffectiveTypeRoots = getEffectiveTypeRoots; - function getDefaultTypeRoots(currentDirectory, host) { - if (!host.directoryExists) { - return [ts.combinePaths(currentDirectory, nodeModulesAtTypes)]; + function emitParameters(parentNode, parameters) { + emitList(parentNode, parameters, 1360); } - var typeRoots; - while (true) { - var atTypes = ts.combinePaths(currentDirectory, nodeModulesAtTypes); - if (host.directoryExists(atTypes)) { - (typeRoots || (typeRoots = [])).push(atTypes); + function emitParametersForArrow(parentNode, parameters) { + if (parameters && + parameters.length === 1 && + parameters[0].type === undefined && + parameters[0].pos === parentNode.pos) { + emit(parameters[0]); } - var parent_15 = ts.getDirectoryPath(currentDirectory); - if (parent_15 === currentDirectory) { - break; + else { + emitParameters(parentNode, parameters); } - currentDirectory = parent_15; } - return typeRoots; - } - var nodeModulesAtTypes = ts.combinePaths("node_modules", "@types"); - function resolveTypeReferenceDirective(typeReferenceDirectiveName, containingFile, options, host) { - var traceEnabled = isTraceEnabled(options, host); - var moduleResolutionState = { - compilerOptions: options, - host: host, - skipTsx: true, - traceEnabled: traceEnabled - }; - var typeRoots = getEffectiveTypeRoots(options, host); - if (traceEnabled) { - if (containingFile === undefined) { - if (typeRoots === undefined) { - trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_not_set_root_directory_not_set, typeReferenceDirectiveName); + function emitParametersForIndexSignature(parentNode, parameters) { + emitList(parentNode, parameters, 4432); + } + function emitList(parentNode, children, format, start, count) { + emitNodeList(emit, parentNode, children, format, start, count); + } + function emitExpressionList(parentNode, children, format, start, count) { + emitNodeList(emitExpression, parentNode, children, format, start, count); + } + function emitNodeList(emit, parentNode, children, format, start, count) { + if (start === void 0) { start = 0; } + if (count === void 0) { count = children ? children.length - start : 0; } + var isUndefined = children === undefined; + if (isUndefined && format & 8192) { + return; + } + var isEmpty = isUndefined || children.length === 0 || start >= children.length || count === 0; + if (isEmpty && format & 16384) { + return; + } + if (format & 7680) { + write(getOpeningBracket(format)); + } + if (isEmpty) { + if (format & 1) { + writeLine(); } - else { - trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_not_set_root_directory_1, typeReferenceDirectiveName, typeRoots); + else if (format & 128) { + write(" "); } } else { - if (typeRoots === undefined) { - trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_1_root_directory_not_set, typeReferenceDirectiveName, containingFile); + var mayEmitInterveningComments = (format & 131072) === 0; + var shouldEmitInterveningComments = mayEmitInterveningComments; + if (shouldWriteLeadingLineTerminator(parentNode, children, format)) { + writeLine(); + shouldEmitInterveningComments = false; } - else { - trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_1_root_directory_2, typeReferenceDirectiveName, containingFile, typeRoots); + else if (format & 128) { + write(" "); } - } - } - var failedLookupLocations = []; - if (typeRoots && typeRoots.length) { - if (traceEnabled) { - trace(host, ts.Diagnostics.Resolving_with_primary_search_path_0, typeRoots.join(", ")); - } - var primarySearchPaths = typeRoots; - for (var _i = 0, primarySearchPaths_1 = primarySearchPaths; _i < primarySearchPaths_1.length; _i++) { - var typeRoot = primarySearchPaths_1[_i]; - var candidate = ts.combinePaths(typeRoot, typeReferenceDirectiveName); - var candidateDirectory = ts.getDirectoryPath(candidate); - var resolvedFile_1 = loadNodeModuleFromDirectory(typeReferenceExtensions, candidate, failedLookupLocations, !directoryProbablyExists(candidateDirectory, host), moduleResolutionState); - if (resolvedFile_1) { - if (traceEnabled) { - trace(host, ts.Diagnostics.Type_reference_directive_0_was_successfully_resolved_to_1_primary_Colon_2, typeReferenceDirectiveName, resolvedFile_1, true); + if (format & 64) { + increaseIndent(); + } + var previousSibling = void 0; + var shouldDecreaseIndentAfterEmit = void 0; + var delimiter = getDelimiter(format); + for (var i = 0; i < count; i++) { + var child = children[start + i]; + if (previousSibling) { + write(delimiter); + if (shouldWriteSeparatingLineTerminator(previousSibling, child, format)) { + if ((format & (3 | 64)) === 0) { + increaseIndent(); + shouldDecreaseIndentAfterEmit = true; + } + writeLine(); + shouldEmitInterveningComments = false; + } + else if (previousSibling && format & 256) { + write(" "); + } } - return { - resolvedTypeReferenceDirective: { primary: true, resolvedFileName: resolvedFile_1 }, - failedLookupLocations: failedLookupLocations - }; + if (shouldEmitInterveningComments) { + var commentRange = ts.getCommentRange(child); + emitTrailingCommentsOfPosition(commentRange.pos); + } + else { + shouldEmitInterveningComments = mayEmitInterveningComments; + } + emit(child); + if (shouldDecreaseIndentAfterEmit) { + decreaseIndent(); + shouldDecreaseIndentAfterEmit = false; + } + previousSibling = child; } - } - } - else { - if (traceEnabled) { - trace(host, ts.Diagnostics.Root_directory_cannot_be_determined_skipping_primary_search_paths); - } - } - var resolvedFile; - var initialLocationForSecondaryLookup; - if (containingFile) { - initialLocationForSecondaryLookup = ts.getDirectoryPath(containingFile); - } - if (initialLocationForSecondaryLookup !== undefined) { - if (traceEnabled) { - trace(host, ts.Diagnostics.Looking_up_in_node_modules_folder_initial_location_0, initialLocationForSecondaryLookup); - } - resolvedFile = loadModuleFromNodeModules(typeReferenceDirectiveName, initialLocationForSecondaryLookup, failedLookupLocations, moduleResolutionState); - if (traceEnabled) { - if (resolvedFile) { - trace(host, ts.Diagnostics.Type_reference_directive_0_was_successfully_resolved_to_1_primary_Colon_2, typeReferenceDirectiveName, resolvedFile, false); + var hasTrailingComma = (format & 32) && children.hasTrailingComma; + if (format & 16 && hasTrailingComma) { + write(","); } - else { - trace(host, ts.Diagnostics.Type_reference_directive_0_was_not_resolved, typeReferenceDirectiveName); + if (format & 64) { + decreaseIndent(); + } + if (shouldWriteClosingLineTerminator(parentNode, children, format)) { + writeLine(); + } + else if (format & 128) { + write(" "); } } - } - else { - if (traceEnabled) { - trace(host, ts.Diagnostics.Containing_file_is_not_specified_and_root_directory_cannot_be_determined_skipping_lookup_in_node_modules_folder); + if (format & 7680) { + write(getClosingBracket(format)); } } - return { - resolvedTypeReferenceDirective: resolvedFile - ? { primary: false, resolvedFileName: resolvedFile } - : undefined, - failedLookupLocations: failedLookupLocations - }; - } - ts.resolveTypeReferenceDirective = resolveTypeReferenceDirective; - function resolveModuleName(moduleName, containingFile, compilerOptions, host) { - var traceEnabled = isTraceEnabled(compilerOptions, host); - if (traceEnabled) { - trace(host, ts.Diagnostics.Resolving_module_0_from_1, moduleName, containingFile); - } - var moduleResolution = compilerOptions.moduleResolution; - if (moduleResolution === undefined) { - moduleResolution = ts.getEmitModuleKind(compilerOptions) === ts.ModuleKind.CommonJS ? ts.ModuleResolutionKind.NodeJs : ts.ModuleResolutionKind.Classic; - if (traceEnabled) { - trace(host, ts.Diagnostics.Module_resolution_kind_is_not_specified_using_0, ts.ModuleResolutionKind[moduleResolution]); + function writeIfAny(nodes, text) { + if (nodes && nodes.length > 0) { + write(text); } } - else { - if (traceEnabled) { - trace(host, ts.Diagnostics.Explicitly_specified_module_resolution_kind_Colon_0, ts.ModuleResolutionKind[moduleResolution]); + function writeIfPresent(node, text) { + if (node !== undefined) { + write(text); } } - var result; - switch (moduleResolution) { - case ts.ModuleResolutionKind.NodeJs: - result = nodeModuleNameResolver(moduleName, containingFile, compilerOptions, host); - break; - case ts.ModuleResolutionKind.Classic: - result = classicNameResolver(moduleName, containingFile, compilerOptions, host); - break; + function writeToken(token, pos, contextNode) { + return emitTokenWithSourceMap(contextNode, token, pos, writeTokenText); } - if (traceEnabled) { - if (result.resolvedModule) { - trace(host, ts.Diagnostics.Module_name_0_was_successfully_resolved_to_1, moduleName, result.resolvedModule.resolvedFileName); + function writeTokenText(token, pos) { + var tokenString = ts.tokenToString(token); + write(tokenString); + return pos < 0 ? pos : pos + tokenString.length; + } + function increaseIndentIf(value, valueToWriteWhenNotIndenting) { + if (value) { + increaseIndent(); + writeLine(); } - else { - trace(host, ts.Diagnostics.Module_name_0_was_not_resolved, moduleName); + else if (valueToWriteWhenNotIndenting) { + write(valueToWriteWhenNotIndenting); } } - return result; - } - ts.resolveModuleName = resolveModuleName; - function tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state) { - if (moduleHasNonRelativeName(moduleName)) { - return tryLoadModuleUsingBaseUrl(moduleName, loader, failedLookupLocations, supportedExtensions, state); - } - else { - return tryLoadModuleUsingRootDirs(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state); - } - } - function tryLoadModuleUsingRootDirs(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state) { - if (!state.compilerOptions.rootDirs) { - return undefined; - } - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.rootDirs_option_is_set_using_it_to_resolve_relative_module_name_0, moduleName); + function decreaseIndentIf(value1, value2) { + if (value1) { + decreaseIndent(); + } + if (value2) { + decreaseIndent(); + } } - var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); - var matchedRootDir; - var matchedNormalizedPrefix; - for (var _i = 0, _a = state.compilerOptions.rootDirs; _i < _a.length; _i++) { - var rootDir = _a[_i]; - var normalizedRoot = ts.normalizePath(rootDir); - if (!ts.endsWith(normalizedRoot, ts.directorySeparator)) { - normalizedRoot += ts.directorySeparator; + function shouldWriteLeadingLineTerminator(parentNode, children, format) { + if (format & 1) { + return true; } - var isLongestMatchingPrefix = ts.startsWith(candidate, normalizedRoot) && - (matchedNormalizedPrefix === undefined || matchedNormalizedPrefix.length < normalizedRoot.length); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Checking_if_0_is_the_longest_matching_prefix_for_1_2, normalizedRoot, candidate, isLongestMatchingPrefix); + if (format & 2) { + if (format & 32768) { + return true; + } + var firstChild = children[0]; + if (firstChild === undefined) { + return !ts.rangeIsOnSingleLine(parentNode, currentSourceFile); + } + else if (ts.positionIsSynthesized(parentNode.pos) || ts.nodeIsSynthesized(firstChild)) { + return synthesizedNodeStartsOnNewLine(firstChild, format); + } + else { + return !ts.rangeStartPositionsAreOnSameLine(parentNode, firstChild, currentSourceFile); + } } - if (isLongestMatchingPrefix) { - matchedNormalizedPrefix = normalizedRoot; - matchedRootDir = rootDir; + else { + return false; } } - if (matchedNormalizedPrefix) { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Longest_matching_prefix_for_0_is_1, candidate, matchedNormalizedPrefix); + function shouldWriteSeparatingLineTerminator(previousNode, nextNode, format) { + if (format & 1) { + return true; } - var suffix = candidate.substr(matchedNormalizedPrefix.length); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Loading_0_from_the_root_dir_1_candidate_location_2, suffix, matchedNormalizedPrefix, candidate); + else if (format & 2) { + if (previousNode === undefined || nextNode === undefined) { + return false; + } + else if (ts.nodeIsSynthesized(previousNode) || ts.nodeIsSynthesized(nextNode)) { + return synthesizedNodeStartsOnNewLine(previousNode, format) || synthesizedNodeStartsOnNewLine(nextNode, format); + } + else { + return !ts.rangeEndIsOnSameLineAsRangeStart(previousNode, nextNode, currentSourceFile); + } } - var resolvedFileName = loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(containingDirectory, state.host), state); - if (resolvedFileName) { - return resolvedFileName; + else { + return nextNode.startsOnNewLine; } - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Trying_other_entries_in_rootDirs); + } + function shouldWriteClosingLineTerminator(parentNode, children, format) { + if (format & 1) { + return (format & 65536) === 0; } - for (var _b = 0, _c = state.compilerOptions.rootDirs; _b < _c.length; _b++) { - var rootDir = _c[_b]; - if (rootDir === matchedRootDir) { - continue; + else if (format & 2) { + if (format & 32768) { + return true; } - var candidate_1 = ts.combinePaths(ts.normalizePath(rootDir), suffix); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Loading_0_from_the_root_dir_1_candidate_location_2, suffix, rootDir, candidate_1); + var lastChild = ts.lastOrUndefined(children); + if (lastChild === undefined) { + return !ts.rangeIsOnSingleLine(parentNode, currentSourceFile); } - var baseDirectory = ts.getDirectoryPath(candidate_1); - var resolvedFileName_1 = loader(candidate_1, supportedExtensions, failedLookupLocations, !directoryProbablyExists(baseDirectory, state.host), state); - if (resolvedFileName_1) { - return resolvedFileName_1; + else if (ts.positionIsSynthesized(parentNode.pos) || ts.nodeIsSynthesized(lastChild)) { + return synthesizedNodeStartsOnNewLine(lastChild, format); + } + else { + return !ts.rangeEndPositionsAreOnSameLine(parentNode, lastChild, currentSourceFile); } } - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Module_resolution_using_rootDirs_has_failed); + else { + return false; } } - return undefined; - } - function tryLoadModuleUsingBaseUrl(moduleName, loader, failedLookupLocations, supportedExtensions, state) { - if (!state.compilerOptions.baseUrl) { - return undefined; + function synthesizedNodeStartsOnNewLine(node, format) { + if (ts.nodeIsSynthesized(node)) { + var startsOnNewLine = node.startsOnNewLine; + if (startsOnNewLine === undefined) { + return (format & 32768) !== 0; + } + return startsOnNewLine; + } + return (format & 32768) !== 0; } - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.baseUrl_option_is_set_to_0_using_this_value_to_resolve_non_relative_module_name_1, state.compilerOptions.baseUrl, moduleName); + function needsIndentation(parent, node1, node2) { + parent = skipSynthesizedParentheses(parent); + node1 = skipSynthesizedParentheses(node1); + node2 = skipSynthesizedParentheses(node2); + if (node2.startsOnNewLine) { + return true; + } + return !ts.nodeIsSynthesized(parent) + && !ts.nodeIsSynthesized(node1) + && !ts.nodeIsSynthesized(node2) + && !ts.rangeEndIsOnSameLineAsRangeStart(node1, node2, currentSourceFile); } - var matchedPattern = undefined; - if (state.compilerOptions.paths) { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.paths_option_is_specified_looking_for_a_pattern_to_match_module_name_0, moduleName); + function skipSynthesizedParentheses(node) { + while (node.kind === 178 && ts.nodeIsSynthesized(node)) { + node = node.expression; } - matchedPattern = matchPatternOrExact(ts.getOwnKeys(state.compilerOptions.paths), moduleName); + return node; } - if (matchedPattern) { - var matchedStar = typeof matchedPattern === "string" ? undefined : matchedText(matchedPattern, moduleName); - var matchedPatternText = typeof matchedPattern === "string" ? matchedPattern : patternText(matchedPattern); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Module_name_0_matched_pattern_1, moduleName, matchedPatternText); + function getTextOfNode(node, includeTrivia) { + if (ts.isGeneratedIdentifier(node)) { + return getGeneratedIdentifier(node); } - for (var _i = 0, _a = state.compilerOptions.paths[matchedPatternText]; _i < _a.length; _i++) { - var subst = _a[_i]; - var path = matchedStar ? subst.replace("*", matchedStar) : subst; - var candidate = ts.normalizePath(ts.combinePaths(state.compilerOptions.baseUrl, path)); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Trying_substitution_0_candidate_module_location_Colon_1, subst, path); + else if (ts.isIdentifier(node) && (ts.nodeIsSynthesized(node) || !node.parent)) { + return ts.unescapeIdentifier(node.text); + } + else if (node.kind === 9 && node.textSourceNode) { + return getTextOfNode(node.textSourceNode, includeTrivia); + } + else if (ts.isLiteralExpression(node) && (ts.nodeIsSynthesized(node) || !node.parent)) { + return node.text; + } + return ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, node, includeTrivia); + } + function getLiteralTextOfNode(node) { + if (node.kind === 9 && node.textSourceNode) { + var textSourceNode = node.textSourceNode; + if (ts.isIdentifier(textSourceNode)) { + return "\"" + ts.escapeNonAsciiCharacters(ts.escapeString(getTextOfNode(textSourceNode))) + "\""; } - var resolvedFileName = loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(ts.getDirectoryPath(candidate), state.host), state); - if (resolvedFileName) { - return resolvedFileName; + else { + return getLiteralTextOfNode(textSourceNode); } } - return undefined; + return ts.getLiteralText(node, currentSourceFile, languageVersion); } - else { - var candidate = ts.normalizePath(ts.combinePaths(state.compilerOptions.baseUrl, moduleName)); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Resolving_module_name_0_relative_to_base_url_1_2, moduleName, state.compilerOptions.baseUrl, candidate); - } - return loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(ts.getDirectoryPath(candidate), state.host), state); + function isSingleLineEmptyBlock(block) { + return !block.multiLine + && isEmptyBlock(block); } - } - function matchPatternOrExact(patternStrings, candidate) { - var patterns = []; - for (var _i = 0, patternStrings_1 = patternStrings; _i < patternStrings_1.length; _i++) { - var patternString = patternStrings_1[_i]; - var pattern = tryParsePattern(patternString); - if (pattern) { - patterns.push(pattern); - } - else if (patternString === candidate) { - return patternString; - } + function isEmptyBlock(block) { + return block.statements.length === 0 + && ts.rangeEndIsOnSameLineAsRangeStart(block, block, currentSourceFile); } - return findBestPatternMatch(patterns, function (_) { return _; }, candidate); - } - function patternText(_a) { - var prefix = _a.prefix, suffix = _a.suffix; - return prefix + "*" + suffix; - } - function matchedText(pattern, candidate) { - ts.Debug.assert(isPatternMatch(pattern, candidate)); - return candidate.substr(pattern.prefix.length, candidate.length - pattern.suffix.length); - } - function findBestPatternMatch(values, getPattern, candidate) { - var matchedValue = undefined; - var longestMatchPrefixLength = -1; - for (var _i = 0, values_1 = values; _i < values_1.length; _i++) { - var v = values_1[_i]; - var pattern = getPattern(v); - if (isPatternMatch(pattern, candidate) && pattern.prefix.length > longestMatchPrefixLength) { - longestMatchPrefixLength = pattern.prefix.length; - matchedValue = v; + function isUniqueName(name) { + return !resolver.hasGlobalName(name) && + !ts.hasProperty(currentFileIdentifiers, name) && + !ts.hasProperty(generatedNameSet, name); + } + function isUniqueLocalName(name, container) { + for (var node = container; ts.isNodeDescendantOf(node, container); node = node.nextContainer) { + if (node.locals && ts.hasProperty(node.locals, name)) { + if (node.locals[name].flags & (107455 | 1048576 | 8388608)) { + return false; + } + } } + return true; } - return matchedValue; - } - ts.findBestPatternMatch = findBestPatternMatch; - function isPatternMatch(_a, candidate) { - var prefix = _a.prefix, suffix = _a.suffix; - return candidate.length >= prefix.length + suffix.length && - ts.startsWith(candidate, prefix) && - ts.endsWith(candidate, suffix); - } - function tryParsePattern(pattern) { - ts.Debug.assert(hasZeroOrOneAsteriskCharacter(pattern)); - var indexOfStar = pattern.indexOf("*"); - return indexOfStar === -1 ? undefined : { - prefix: pattern.substr(0, indexOfStar), - suffix: pattern.substr(indexOfStar + 1) - }; - } - ts.tryParsePattern = tryParsePattern; - function nodeModuleNameResolver(moduleName, containingFile, compilerOptions, host) { - var containingDirectory = ts.getDirectoryPath(containingFile); - var supportedExtensions = ts.getSupportedExtensions(compilerOptions); - var traceEnabled = isTraceEnabled(compilerOptions, host); - var failedLookupLocations = []; - var state = { compilerOptions: compilerOptions, host: host, traceEnabled: traceEnabled, skipTsx: false }; - var resolvedFileName = tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, nodeLoadModuleByRelativeName, failedLookupLocations, supportedExtensions, state); - var isExternalLibraryImport = false; - if (!resolvedFileName) { - if (moduleHasNonRelativeName(moduleName)) { - if (traceEnabled) { - trace(host, ts.Diagnostics.Loading_module_0_from_node_modules_folder, moduleName); + function makeTempVariableName(flags) { + if (flags && !(tempFlags & flags)) { + var name_44 = flags === 268435456 ? "_i" : "_n"; + if (isUniqueName(name_44)) { + tempFlags |= flags; + return name_44; + } + } + while (true) { + var count = tempFlags & 268435455; + tempFlags++; + if (count !== 8 && count !== 13) { + var name_45 = count < 26 + ? "_" + String.fromCharCode(97 + count) + : "_" + (count - 26); + if (isUniqueName(name_45)) { + return name_45; + } } - resolvedFileName = loadModuleFromNodeModules(moduleName, containingDirectory, failedLookupLocations, state); - isExternalLibraryImport = resolvedFileName !== undefined; - } - else { - var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); - resolvedFileName = nodeLoadModuleByRelativeName(candidate, supportedExtensions, failedLookupLocations, false, state); } } - if (resolvedFileName && host.realpath) { - var originalFileName = resolvedFileName; - resolvedFileName = ts.normalizePath(host.realpath(resolvedFileName)); - if (traceEnabled) { - trace(host, ts.Diagnostics.Resolving_real_path_for_0_result_1, originalFileName, resolvedFileName); + function makeUniqueName(baseName) { + if (baseName.charCodeAt(baseName.length - 1) !== 95) { + baseName += "_"; + } + var i = 1; + while (true) { + var generatedName = baseName + i; + if (isUniqueName(generatedName)) { + return generatedNameSet[generatedName] = generatedName; + } + i++; } } - return createResolvedModule(resolvedFileName, isExternalLibraryImport, failedLookupLocations); - } - ts.nodeModuleNameResolver = nodeModuleNameResolver; - function nodeLoadModuleByRelativeName(candidate, supportedExtensions, failedLookupLocations, onlyRecordFailures, state) { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Loading_module_as_file_Slash_folder_candidate_module_location_0, candidate); + function generateNameForModuleOrEnum(node) { + var name = getTextOfNode(node.name); + return isUniqueLocalName(name, node) ? name : makeUniqueName(name); } - var resolvedFileName = !ts.pathEndsWithDirectorySeparator(candidate) && loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, onlyRecordFailures, state); - return resolvedFileName || loadNodeModuleFromDirectory(supportedExtensions, candidate, failedLookupLocations, onlyRecordFailures, state); - } - function directoryProbablyExists(directoryName, host) { - return !host.directoryExists || host.directoryExists(directoryName); - } - ts.directoryProbablyExists = directoryProbablyExists; - function loadModuleFromFile(candidate, extensions, failedLookupLocation, onlyRecordFailures, state) { - var resolvedByAddingExtension = tryAddingExtensions(candidate, extensions, failedLookupLocation, onlyRecordFailures, state); - if (resolvedByAddingExtension) { - return resolvedByAddingExtension; + function generateNameForImportOrExportDeclaration(node) { + var expr = ts.getExternalModuleName(node); + var baseName = expr.kind === 9 ? + ts.escapeIdentifier(ts.makeIdentifierFromModuleName(expr.text)) : "module"; + return makeUniqueName(baseName); } - if (ts.hasJavaScriptFileExtension(candidate)) { - var extensionless = ts.removeFileExtension(candidate); - if (state.traceEnabled) { - var extension = candidate.substring(extensionless.length); - trace(state.host, ts.Diagnostics.File_name_0_has_a_1_extension_stripping_it, candidate, extension); - } - return tryAddingExtensions(extensionless, extensions, failedLookupLocation, onlyRecordFailures, state); + function generateNameForExportDefault() { + return makeUniqueName("default"); } - } - function tryAddingExtensions(candidate, extensions, failedLookupLocation, onlyRecordFailures, state) { - if (!onlyRecordFailures) { - var directory = ts.getDirectoryPath(candidate); - if (directory) { - onlyRecordFailures = !directoryProbablyExists(directory, state.host); - } + function generateNameForClassExpression() { + return makeUniqueName("class"); } - return ts.forEach(extensions, function (ext) { - return !(state.skipTsx && ts.isJsxOrTsxExtension(ext)) && tryFile(candidate + ext, failedLookupLocation, onlyRecordFailures, state); - }); - } - function tryFile(fileName, failedLookupLocation, onlyRecordFailures, state) { - if (!onlyRecordFailures && state.host.fileExists(fileName)) { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.File_0_exist_use_it_as_a_name_resolution_result, fileName); + function generateNameForNode(node) { + switch (node.kind) { + case 69: + return makeUniqueName(getTextOfNode(node)); + case 225: + case 224: + return generateNameForModuleOrEnum(node); + case 230: + case 236: + return generateNameForImportOrExportDeclaration(node); + case 220: + case 221: + case 235: + return generateNameForExportDefault(); + case 192: + return generateNameForClassExpression(); + default: + return makeTempVariableName(0); } - return fileName; } - else { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.File_0_does_not_exist, fileName); + function generateName(name) { + switch (name.autoGenerateKind) { + case 1: + return makeTempVariableName(0); + case 2: + return makeTempVariableName(268435456); + case 3: + return makeUniqueName(name.text); } - failedLookupLocation.push(fileName); - return undefined; + ts.Debug.fail("Unsupported GeneratedIdentifierKind."); } - } - function loadNodeModuleFromDirectory(extensions, candidate, failedLookupLocation, onlyRecordFailures, state) { - var packageJsonPath = pathToPackageJson(candidate); - var directoryExists = !onlyRecordFailures && directoryProbablyExists(candidate, state.host); - if (directoryExists && state.host.fileExists(packageJsonPath)) { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Found_package_json_at_0, packageJsonPath); - } - var typesFile = tryReadTypesSection(packageJsonPath, candidate, state); - if (typesFile) { - var onlyRecordFailures_1 = !directoryProbablyExists(ts.getDirectoryPath(typesFile), state.host); - var result = tryFile(typesFile, failedLookupLocation, onlyRecordFailures_1, state) || - tryAddingExtensions(typesFile, extensions, failedLookupLocation, onlyRecordFailures_1, state); - if (result) { - return result; + function getNodeForGeneratedName(name) { + var autoGenerateId = name.autoGenerateId; + var node = name; + var original = node.original; + while (original) { + node = original; + if (ts.isIdentifier(node) + && node.autoGenerateKind === 4 + && node.autoGenerateId !== autoGenerateId) { + break; } + original = node.original; + } + return node; + } + function getGeneratedIdentifier(name) { + if (name.autoGenerateKind === 4) { + var node = getNodeForGeneratedName(name); + var nodeId = ts.getNodeId(node); + return nodeIdToGeneratedName[nodeId] || (nodeIdToGeneratedName[nodeId] = ts.unescapeIdentifier(generateNameForNode(node))); } else { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.package_json_does_not_have_types_field); - } + var autoGenerateId = name.autoGenerateId; + return autoGeneratedIdToGeneratedName[autoGenerateId] || (autoGeneratedIdToGeneratedName[autoGenerateId] = ts.unescapeIdentifier(generateName(name))); } } - else { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.File_0_does_not_exist, packageJsonPath); - } - failedLookupLocation.push(packageJsonPath); + function createDelimiterMap() { + var delimiters = []; + delimiters[0] = ""; + delimiters[16] = ","; + delimiters[4] = " |"; + delimiters[8] = " &"; + return delimiters; } - return loadModuleFromFile(ts.combinePaths(candidate, "index"), extensions, failedLookupLocation, !directoryExists, state); - } - function pathToPackageJson(directory) { - return ts.combinePaths(directory, "package.json"); - } - function loadModuleFromNodeModulesFolder(moduleName, directory, failedLookupLocations, state) { - var nodeModulesFolder = ts.combinePaths(directory, "node_modules"); - var nodeModulesFolderExists = directoryProbablyExists(nodeModulesFolder, state.host); - var candidate = ts.normalizePath(ts.combinePaths(nodeModulesFolder, moduleName)); - var supportedExtensions = ts.getSupportedExtensions(state.compilerOptions); - var result = loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, !nodeModulesFolderExists, state); - if (result) { - return result; + function getDelimiter(format) { + return delimiters[format & 28]; } - result = loadNodeModuleFromDirectory(supportedExtensions, candidate, failedLookupLocations, !nodeModulesFolderExists, state); - if (result) { - return result; + function createBracketsMap() { + var brackets = []; + brackets[512] = ["{", "}"]; + brackets[1024] = ["(", ")"]; + brackets[2048] = ["<", ">"]; + brackets[4096] = ["[", "]"]; + return brackets; + } + function getOpeningBracket(format) { + return brackets[format & 7680][0]; + } + function getClosingBracket(format) { + return brackets[format & 7680][1]; } } - function loadModuleFromNodeModules(moduleName, directory, failedLookupLocations, state) { - directory = ts.normalizeSlashes(directory); + ts.emitFiles = emitFiles; +})(ts || (ts = {})); +var ts; +(function (ts) { + ts.version = "2.1.0"; + var emptyArray = []; + function findConfigFile(searchPath, fileExists, configName) { + if (configName === void 0) { configName = "tsconfig.json"; } while (true) { - var baseName = ts.getBaseFileName(directory); - if (baseName !== "node_modules") { - var packageResult = loadModuleFromNodeModulesFolder(moduleName, directory, failedLookupLocations, state); - if (packageResult && ts.hasTypeScriptFileExtension(packageResult)) { - return packageResult; - } - else { - var typesResult = loadModuleFromNodeModulesFolder(ts.combinePaths("@types", moduleName), directory, failedLookupLocations, state); - if (typesResult || packageResult) { - return typesResult || packageResult; - } - } + var fileName = ts.combinePaths(searchPath, configName); + if (fileExists(fileName)) { + return fileName; } - var parentPath = ts.getDirectoryPath(directory); - if (parentPath === directory) { + var parentPath = ts.getDirectoryPath(searchPath); + if (parentPath === searchPath) { break; } - directory = parentPath; + searchPath = parentPath; } return undefined; } - function classicNameResolver(moduleName, containingFile, compilerOptions, host) { - var traceEnabled = isTraceEnabled(compilerOptions, host); - var state = { compilerOptions: compilerOptions, host: host, traceEnabled: traceEnabled, skipTsx: !compilerOptions.jsx }; - var failedLookupLocations = []; - var supportedExtensions = ts.getSupportedExtensions(compilerOptions); - var containingDirectory = ts.getDirectoryPath(containingFile); - var resolvedFileName = tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, loadModuleFromFile, failedLookupLocations, supportedExtensions, state); - if (resolvedFileName) { - return createResolvedModule(resolvedFileName, false, failedLookupLocations); - } - var referencedSourceFile; - if (moduleHasNonRelativeName(moduleName)) { - while (true) { - var searchName = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); - referencedSourceFile = loadModuleFromFile(searchName, supportedExtensions, failedLookupLocations, false, state); - if (referencedSourceFile) { - break; - } - var parentPath = ts.getDirectoryPath(containingDirectory); - if (parentPath === containingDirectory) { + ts.findConfigFile = findConfigFile; + function resolveTripleslashReference(moduleName, containingFile) { + var basePath = ts.getDirectoryPath(containingFile); + var referencedFileName = ts.isRootedDiskPath(moduleName) ? moduleName : ts.combinePaths(basePath, moduleName); + return ts.normalizePath(referencedFileName); + } + ts.resolveTripleslashReference = resolveTripleslashReference; + function computeCommonSourceDirectoryOfFilenames(fileNames, currentDirectory, getCanonicalFileName) { + var commonPathComponents; + var failed = ts.forEach(fileNames, function (sourceFile) { + var sourcePathComponents = ts.getNormalizedPathComponents(sourceFile, currentDirectory); + sourcePathComponents.pop(); + if (!commonPathComponents) { + commonPathComponents = sourcePathComponents; + return; + } + for (var i = 0, n = Math.min(commonPathComponents.length, sourcePathComponents.length); i < n; i++) { + if (getCanonicalFileName(commonPathComponents[i]) !== getCanonicalFileName(sourcePathComponents[i])) { + if (i === 0) { + return true; + } + commonPathComponents.length = i; break; } - containingDirectory = parentPath; } + if (sourcePathComponents.length < commonPathComponents.length) { + commonPathComponents.length = sourcePathComponents.length; + } + }); + if (failed) { + return ""; } - else { - var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); - referencedSourceFile = loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, false, state); + if (!commonPathComponents) { + return currentDirectory; } - return referencedSourceFile - ? { resolvedModule: { resolvedFileName: referencedSourceFile }, failedLookupLocations: failedLookupLocations } - : { resolvedModule: undefined, failedLookupLocations: failedLookupLocations }; + return ts.getNormalizedPathFromPathComponents(commonPathComponents); } - ts.classicNameResolver = classicNameResolver; + ts.computeCommonSourceDirectoryOfFilenames = computeCommonSourceDirectoryOfFilenames; function createCompilerHost(options, setParentNodes) { var existingDirectories = ts.createMap(); function getCanonicalFileName(fileName) { @@ -47121,7 +47816,7 @@ var ts; readFile: function (fileName) { return ts.sys.readFile(fileName); }, trace: function (s) { return ts.sys.write(s + newLine); }, directoryExists: function (directoryName) { return ts.sys.directoryExists(directoryName); }, - getEnvironmentVariable: function (name) { return ts.getEnvironmentVariable(name, undefined); }, + getEnvironmentVariable: function (name) { return ts.sys.getEnvironmentVariable ? ts.sys.getEnvironmentVariable(name) : ""; }, getDirectories: function (path) { return ts.sys.getDirectories(path); }, realpath: realpath }; @@ -47181,41 +47876,14 @@ var ts; var resolutions = []; var cache = ts.createMap(); for (var _i = 0, names_2 = names; _i < names_2.length; _i++) { - var name_45 = names_2[_i]; - var result = name_45 in cache - ? cache[name_45] - : cache[name_45] = loader(name_45, containingFile); + var name_46 = names_2[_i]; + var result = name_46 in cache + ? cache[name_46] + : cache[name_46] = loader(name_46, containingFile); resolutions.push(result); } return resolutions; } - function getAutomaticTypeDirectiveNames(options, host) { - if (options.types) { - return options.types; - } - var result = []; - if (host.directoryExists && host.getDirectories) { - var typeRoots = getEffectiveTypeRoots(options, host); - if (typeRoots) { - for (var _i = 0, typeRoots_1 = typeRoots; _i < typeRoots_1.length; _i++) { - var root = typeRoots_1[_i]; - if (host.directoryExists(root)) { - for (var _a = 0, _b = host.getDirectories(root); _a < _b.length; _a++) { - var typeDirectivePath = _b[_a]; - var normalized = ts.normalizePath(typeDirectivePath); - var packageJsonPath = pathToPackageJson(ts.combinePaths(root, normalized)); - var isNotNeededPackage = host.fileExists(packageJsonPath) && readJson(packageJsonPath, host).typings === null; - if (!isNotNeededPackage) { - result.push(ts.getBaseFileName(normalized)); - } - } - } - } - } - } - return result; - } - ts.getAutomaticTypeDirectiveNames = getAutomaticTypeDirectiveNames; function createProgram(rootNames, options, host, oldProgram) { var program; var files = []; @@ -47241,7 +47909,7 @@ var ts; resolveModuleNamesWorker = function (moduleNames, containingFile) { return host.resolveModuleNames(moduleNames, containingFile); }; } else { - var loader_1 = function (moduleName, containingFile) { return resolveModuleName(moduleName, containingFile, options, host).resolvedModule; }; + var loader_1 = function (moduleName, containingFile) { return ts.resolveModuleName(moduleName, containingFile, options, host).resolvedModule; }; resolveModuleNamesWorker = function (moduleNames, containingFile) { return loadWithLocalCache(moduleNames, containingFile, loader_1); }; } var resolveTypeReferenceDirectiveNamesWorker; @@ -47249,15 +47917,15 @@ var ts; resolveTypeReferenceDirectiveNamesWorker = function (typeDirectiveNames, containingFile) { return host.resolveTypeReferenceDirectives(typeDirectiveNames, containingFile); }; } else { - var loader_2 = function (typesRef, containingFile) { return resolveTypeReferenceDirective(typesRef, containingFile, options, host).resolvedTypeReferenceDirective; }; + var loader_2 = function (typesRef, containingFile) { return ts.resolveTypeReferenceDirective(typesRef, containingFile, options, host).resolvedTypeReferenceDirective; }; resolveTypeReferenceDirectiveNamesWorker = function (typeReferenceDirectiveNames, containingFile) { return loadWithLocalCache(typeReferenceDirectiveNames, containingFile, loader_2); }; } var filesByName = ts.createFileMap(); var filesByNameIgnoreCase = host.useCaseSensitiveFileNames() ? ts.createFileMap(function (fileName) { return fileName.toLowerCase(); }) : undefined; if (!tryReuseStructureFromOldProgram()) { ts.forEach(rootNames, function (name) { return processRootFile(name, false); }); - var typeReferences = getAutomaticTypeDirectiveNames(options, host); - if (typeReferences) { + var typeReferences = ts.getAutomaticTypeDirectiveNames(options, host); + if (typeReferences.length) { var containingFilename = ts.combinePaths(host.getCurrentDirectory(), "__inferred type names__.ts"); var resolutions = resolveTypeReferenceDirectiveNamesWorker(typeReferences, containingFilename); for (var i = 0; i < typeReferences.length; i++) { @@ -47299,7 +47967,8 @@ var ts; getSymbolCount: function () { return getDiagnosticsProducingTypeChecker().getSymbolCount(); }, getTypeCount: function () { return getDiagnosticsProducingTypeChecker().getTypeCount(); }, getFileProcessingDiagnostics: function () { return fileProcessingDiagnostics; }, - getResolvedTypeReferenceDirectives: function () { return resolvedTypeReferenceDirectives; } + getResolvedTypeReferenceDirectives: function () { return resolvedTypeReferenceDirectives; }, + dropDiagnosticsProducingTypeChecker: dropDiagnosticsProducingTypeChecker }; verifyCompilerOptions(); ts.performance.mark("afterProgram"); @@ -47346,6 +48015,7 @@ var ts; (oldOptions.configFilePath !== options.configFilePath) || (oldOptions.baseUrl !== options.baseUrl) || (oldOptions.maxNodeModuleJsDepth !== options.maxNodeModuleJsDepth) || + !ts.arrayIsEqualTo(oldOptions.lib, options.lib) || !ts.arrayIsEqualTo(oldOptions.typeRoots, oldOptions.typeRoots) || !ts.arrayIsEqualTo(oldOptions.rootDirs, options.rootDirs) || !ts.equalOwnProperties(oldOptions.paths, options.paths)) { @@ -47446,16 +48116,19 @@ var ts; function getDiagnosticsProducingTypeChecker() { return diagnosticsProducingTypeChecker || (diagnosticsProducingTypeChecker = ts.createTypeChecker(program, true)); } + function dropDiagnosticsProducingTypeChecker() { + diagnosticsProducingTypeChecker = undefined; + } function getTypeChecker() { return noDiagnosticsTypeChecker || (noDiagnosticsTypeChecker = ts.createTypeChecker(program, false)); } - function emit(sourceFile, writeFileCallback, cancellationToken) { - return runWithCancellationToken(function () { return emitWorker(program, sourceFile, writeFileCallback, cancellationToken); }); + function emit(sourceFile, writeFileCallback, cancellationToken, emitOnlyDtsFiles) { + return runWithCancellationToken(function () { return emitWorker(program, sourceFile, writeFileCallback, cancellationToken, emitOnlyDtsFiles); }); } function isEmitBlocked(emitFileName) { return hasEmitBlockingDiagnostics.contains(ts.toPath(emitFileName, currentDirectory, getCanonicalFileName)); } - function emitWorker(program, sourceFile, writeFileCallback, cancellationToken) { + function emitWorker(program, sourceFile, writeFileCallback, cancellationToken, emitOnlyDtsFiles) { var declarationDiagnostics = []; if (options.noEmit) { return { diagnostics: declarationDiagnostics, sourceMaps: undefined, emittedFiles: undefined, emitSkipped: true }; @@ -47476,7 +48149,7 @@ var ts; } var emitResolver = getDiagnosticsProducingTypeChecker().getEmitResolver((options.outFile || options.out) ? undefined : sourceFile); ts.performance.mark("beforeEmit"); - var emitResult = ts.emitFiles(emitResolver, getEmitHost(writeFileCallback), sourceFile); + var emitResult = ts.emitFiles(emitResolver, getEmitHost(writeFileCallback), sourceFile, emitOnlyDtsFiles); ts.performance.mark("afterEmit"); ts.performance.measure("Emit", "beforeEmit", "afterEmit"); return emitResult; @@ -47991,7 +48664,6 @@ var ts; for (var i = 0; i < moduleNames.length; i++) { var resolution = resolutions[i]; ts.setResolvedModule(file, moduleNames[i], resolution); - var resolvedPath = resolution ? ts.toPath(resolution.resolvedFileName, currentDirectory, getCanonicalFileName) : undefined; var isFromNodeModulesSearch = resolution && resolution.isExternalLibraryImport; var isJsFileFromNodeModules = isFromNodeModulesSearch && ts.hasJavaScriptFileExtension(resolution.resolvedFileName); if (isFromNodeModulesSearch) { @@ -48003,7 +48675,7 @@ var ts; modulesWithElidedImports[file.path] = true; } else if (shouldAddFile) { - findSourceFile(resolution.resolvedFileName, resolvedPath, false, false, file, ts.skipTrivia(file.text, file.imports[i].pos), file.imports[i].end); + findSourceFile(resolution.resolvedFileName, ts.toPath(resolution.resolvedFileName, currentDirectory, getCanonicalFileName), false, false, file, ts.skipTrivia(file.text, file.imports[i].pos), file.imports[i].end); } if (isFromNodeModulesSearch) { currentNodeModulesDepth--; @@ -48017,8 +48689,8 @@ var ts; } function computeCommonSourceDirectory(sourceFiles) { var fileNames = []; - for (var _i = 0, sourceFiles_5 = sourceFiles; _i < sourceFiles_5.length; _i++) { - var file = sourceFiles_5[_i]; + for (var _i = 0, sourceFiles_6 = sourceFiles; _i < sourceFiles_6.length; _i++) { + var file = sourceFiles_6[_i]; if (!file.isDeclarationFile) { fileNames.push(file.fileName); } @@ -48029,8 +48701,8 @@ var ts; var allFilesBelongToPath = true; if (sourceFiles) { var absoluteRootDirectoryPath = host.getCanonicalFileName(ts.getNormalizedAbsolutePath(rootDirectory, currentDirectory)); - for (var _i = 0, sourceFiles_6 = sourceFiles; _i < sourceFiles_6.length; _i++) { - var sourceFile = sourceFiles_6[_i]; + for (var _i = 0, sourceFiles_7 = sourceFiles; _i < sourceFiles_7.length; _i++) { + var sourceFile = sourceFiles_7[_i]; if (!ts.isDeclarationFile(sourceFile)) { var absoluteSourceFilePath = host.getCanonicalFileName(ts.getNormalizedAbsolutePath(sourceFile.fileName, currentDirectory)); if (absoluteSourceFilePath.indexOf(absoluteRootDirectoryPath) !== 0) { @@ -48073,7 +48745,7 @@ var ts; if (!ts.hasProperty(options.paths, key)) { continue; } - if (!hasZeroOrOneAsteriskCharacter(key)) { + if (!ts.hasZeroOrOneAsteriskCharacter(key)) { programDiagnostics.add(ts.createCompilerDiagnostic(ts.Diagnostics.Pattern_0_can_have_at_most_one_Asterisk_character, key)); } if (ts.isArray(options.paths[key])) { @@ -48084,7 +48756,7 @@ var ts; var subst = _a[_i]; var typeOfSubst = typeof subst; if (typeOfSubst === "string") { - if (!hasZeroOrOneAsteriskCharacter(subst)) { + if (!ts.hasZeroOrOneAsteriskCharacter(subst)) { programDiagnostics.add(ts.createCompilerDiagnostic(ts.Diagnostics.Substitution_0_in_pattern_1_in_can_have_at_most_one_Asterisk_character, subst, key)); } } @@ -48123,6 +48795,9 @@ var ts; if (options.lib && options.noLib) { programDiagnostics.add(ts.createCompilerDiagnostic(ts.Diagnostics.Option_0_cannot_be_specified_with_option_1, "lib", "noLib")); } + if (options.noImplicitUseStrict && options.alwaysStrict) { + programDiagnostics.add(ts.createCompilerDiagnostic(ts.Diagnostics.Option_0_cannot_be_specified_with_option_1, "noImplicitUseStrict", "alwaysStrict")); + } var languageVersion = options.target || 0; var outFile = options.outFile || options.out; var firstNonAmbientExternalModuleSourceFile = ts.forEach(files, function (f) { return ts.isExternalModule(f) && !ts.isDeclarationFile(f) ? f : undefined; }); @@ -48685,7 +49360,7 @@ var ts; case 97: return true; case 69: - return node.originalKeywordKind === 97 && node.parent.kind === 142; + return ts.identifierIsThisKeyword(node) && node.parent.kind === 142; default: return false; } @@ -49258,7 +49933,6 @@ var ts; } ts.isInNonReferenceComment = isInNonReferenceComment; })(ts || (ts = {})); -var ts; (function (ts) { function isFirstDeclarationOfSymbolParameter(symbol) { return symbol.declarations && symbol.declarations.length > 0 && symbol.declarations[0].kind === 142; @@ -49481,7 +50155,7 @@ var ts; return ts.ensureScriptKind(fileName, scriptKind); } ts.getScriptKind = getScriptKind; - function parseAndReEmitConfigJSONFile(content) { + function sanitizeConfigFile(configFileName, content) { var options = { fileName: "config.js", compilerOptions: { @@ -49492,14 +50166,17 @@ var ts; }; var _a = ts.transpileModule("(" + content + ")", options), outputText = _a.outputText, diagnostics = _a.diagnostics; var trimmedOutput = outputText.trim(); - var configJsonObject = JSON.parse(trimmedOutput.substring(1, trimmedOutput.length - 2)); for (var _i = 0, diagnostics_2 = diagnostics; _i < diagnostics_2.length; _i++) { var diagnostic = diagnostics_2[_i]; diagnostic.start = diagnostic.start - 1; } - return { configJsonObject: configJsonObject, diagnostics: diagnostics }; + var _b = ts.parseConfigFileTextToJson(configFileName, trimmedOutput.substring(1, trimmedOutput.length - 2), false), config = _b.config, error = _b.error; + return { + configJsonObject: config || {}, + diagnostics: error ? ts.concatenate(diagnostics, [error]) : diagnostics + }; } - ts.parseAndReEmitConfigJSONFile = parseAndReEmitConfigJSONFile; + ts.sanitizeConfigFile = sanitizeConfigFile; })(ts || (ts = {})); var ts; (function (ts) { @@ -50701,8 +51378,7 @@ var ts; return; case 142: if (token.parent.name === token) { - var isThis_1 = token.kind === 69 && token.originalKeywordKind === 97; - return isThis_1 ? 3 : 17; + return ts.isThisIdentifier(token) ? 3 : 17; } return; } @@ -50743,13 +51419,13 @@ var ts; if (!completionData) { return undefined; } - var symbols = completionData.symbols, isMemberCompletion = completionData.isMemberCompletion, isNewIdentifierLocation = completionData.isNewIdentifierLocation, location = completionData.location, isJsDocTagName = completionData.isJsDocTagName; + var symbols = completionData.symbols, isGlobalCompletion = completionData.isGlobalCompletion, isMemberCompletion = completionData.isMemberCompletion, isNewIdentifierLocation = completionData.isNewIdentifierLocation, location = completionData.location, isJsDocTagName = completionData.isJsDocTagName; if (isJsDocTagName) { - return { isMemberCompletion: false, isNewIdentifierLocation: false, entries: ts.JsDoc.getAllJsDocCompletionEntries() }; + return { isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: false, entries: ts.JsDoc.getAllJsDocCompletionEntries() }; } var entries = []; if (ts.isSourceFileJavaScript(sourceFile)) { - var uniqueNames = getCompletionEntriesFromSymbols(symbols, entries, location, false); + var uniqueNames = getCompletionEntriesFromSymbols(symbols, entries, location, true); ts.addRange(entries, getJavaScriptCompletionEntries(sourceFile, location.pos, uniqueNames)); } else { @@ -50773,17 +51449,17 @@ var ts; if (!isMemberCompletion && !isJsDocTagName) { ts.addRange(entries, keywordCompletions); } - return { isMemberCompletion: isMemberCompletion, isNewIdentifierLocation: isNewIdentifierLocation || ts.isSourceFileJavaScript(sourceFile), entries: entries }; + return { isGlobalCompletion: isGlobalCompletion, isMemberCompletion: isMemberCompletion, isNewIdentifierLocation: isNewIdentifierLocation, entries: entries }; function getJavaScriptCompletionEntries(sourceFile, position, uniqueNames) { var entries = []; var nameTable = ts.getNameTable(sourceFile); - for (var name_46 in nameTable) { - if (nameTable[name_46] === position) { + for (var name_47 in nameTable) { + if (nameTable[name_47] === position) { continue; } - if (!uniqueNames[name_46]) { - uniqueNames[name_46] = name_46; - var displayName = getCompletionEntryDisplayName(ts.unescapeIdentifier(name_46), compilerOptions.target, true); + if (!uniqueNames[name_47]) { + uniqueNames[name_47] = name_47; + var displayName = getCompletionEntryDisplayName(ts.unescapeIdentifier(name_47), compilerOptions.target, true); if (displayName) { var entry = { name: displayName, @@ -50833,7 +51509,9 @@ var ts; if (!node || node.kind !== 9) { return undefined; } - if (node.parent.kind === 253 && node.parent.parent.kind === 171) { + if (node.parent.kind === 253 && + node.parent.parent.kind === 171 && + node.parent.name === node) { return getStringLiteralCompletionEntriesFromPropertyAssignment(node.parent); } else if (ts.isElementAccessExpression(node.parent) && node.parent.argumentExpression === node) { @@ -50856,7 +51534,7 @@ var ts; if (type) { getCompletionEntriesFromSymbols(type.getApparentProperties(), entries, element, false); if (entries.length) { - return { isMemberCompletion: true, isNewIdentifierLocation: true, entries: entries }; + return { isGlobalCompletion: false, isMemberCompletion: true, isNewIdentifierLocation: true, entries: entries }; } } } @@ -50872,7 +51550,7 @@ var ts; } } if (entries.length) { - return { isMemberCompletion: false, isNewIdentifierLocation: true, entries: entries }; + return { isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: true, entries: entries }; } return undefined; } @@ -50882,7 +51560,7 @@ var ts; if (type) { getCompletionEntriesFromSymbols(type.getApparentProperties(), entries, node, false); if (entries.length) { - return { isMemberCompletion: true, isNewIdentifierLocation: true, entries: entries }; + return { isGlobalCompletion: false, isMemberCompletion: true, isNewIdentifierLocation: true, entries: entries }; } } return undefined; @@ -50893,7 +51571,7 @@ var ts; var entries_2 = []; addStringLiteralCompletionsFromType(type, entries_2); if (entries_2.length) { - return { isMemberCompletion: false, isNewIdentifierLocation: false, entries: entries_2 }; + return { isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: false, entries: entries_2 }; } } return undefined; @@ -50934,6 +51612,7 @@ var ts; entries = getCompletionEntriesForNonRelativeModules(literalValue, scriptDirectory, span); } return { + isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: true, entries: entries @@ -50964,13 +51643,15 @@ var ts; } function getCompletionEntriesForDirectoryFragment(fragment, scriptPath, extensions, includeExtensions, span, exclude, result) { if (result === void 0) { result = []; } - fragment = ts.getDirectoryPath(fragment); - if (!fragment) { - fragment = "./"; + if (fragment === undefined) { + fragment = ""; } - else { - fragment = ts.ensureTrailingDirectorySeparator(fragment); + fragment = ts.normalizeSlashes(fragment); + fragment = ts.getDirectoryPath(fragment); + if (fragment === "") { + fragment = "." + ts.directorySeparator; } + fragment = ts.ensureTrailingDirectorySeparator(fragment); var absolutePath = normalizeAndPreserveTrailingSlash(ts.isRootedDiskPath(fragment) ? fragment : ts.combinePaths(scriptPath, fragment)); var baseDirectory = ts.getDirectoryPath(absolutePath); var ignoreCase = !(host.useCaseSensitiveFileNames && host.useCaseSensitiveFileNames()); @@ -51126,6 +51807,12 @@ var ts; if (!range) { return undefined; } + var completionInfo = { + isGlobalCompletion: false, + isMemberCompletion: false, + isNewIdentifierLocation: true, + entries: [] + }; var text = sourceFile.text.substr(range.pos, position - range.pos); var match = tripleSlashDirectiveFragmentRegex.exec(text); if (match) { @@ -51133,22 +51820,16 @@ var ts; var kind = match[2]; var toComplete = match[3]; var scriptPath = ts.getDirectoryPath(sourceFile.path); - var entries_3; if (kind === "path") { var span_10 = getDirectoryFragmentTextSpan(toComplete, range.pos + prefix.length); - entries_3 = getCompletionEntriesForDirectoryFragment(toComplete, scriptPath, ts.getSupportedExtensions(compilerOptions), true, span_10, sourceFile.path); + completionInfo.entries = getCompletionEntriesForDirectoryFragment(toComplete, scriptPath, ts.getSupportedExtensions(compilerOptions), true, span_10, sourceFile.path); } else { var span_11 = { start: range.pos + prefix.length, length: match[0].length - prefix.length }; - entries_3 = getCompletionEntriesFromTypings(host, compilerOptions, scriptPath, span_11); + completionInfo.entries = getCompletionEntriesFromTypings(host, compilerOptions, scriptPath, span_11); } - return { - isMemberCompletion: false, - isNewIdentifierLocation: true, - entries: entries_3 - }; } - return undefined; + return completionInfo; } function getCompletionEntriesFromTypings(host, options, scriptPath, span, result) { if (result === void 0) { result = []; } @@ -51347,7 +52028,7 @@ var ts; } } if (isJsDocTagName) { - return { symbols: undefined, isMemberCompletion: false, isNewIdentifierLocation: false, location: undefined, isRightOfDot: false, isJsDocTagName: isJsDocTagName }; + return { symbols: undefined, isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: false, location: undefined, isRightOfDot: false, isJsDocTagName: isJsDocTagName }; } if (!insideJsDocTagExpression) { log("Returning an empty list because completion was inside a regular comment or plain text part of a JsDoc comment."); @@ -51399,6 +52080,7 @@ var ts; } } var semanticStart = ts.timestamp(); + var isGlobalCompletion = false; var isMemberCompletion; var isNewIdentifierLocation; var symbols = []; @@ -51429,10 +52111,12 @@ var ts; if (!tryGetGlobalSymbols()) { return undefined; } + isGlobalCompletion = true; } log("getCompletionData: Semantic work: " + (ts.timestamp() - semanticStart)); - return { symbols: symbols, isMemberCompletion: isMemberCompletion, isNewIdentifierLocation: isNewIdentifierLocation, location: location, isRightOfDot: (isRightOfDot || isRightOfOpenTag), isJsDocTagName: isJsDocTagName }; + return { symbols: symbols, isGlobalCompletion: isGlobalCompletion, isMemberCompletion: isMemberCompletion, isNewIdentifierLocation: isNewIdentifierLocation, location: location, isRightOfDot: (isRightOfDot || isRightOfOpenTag), isJsDocTagName: isJsDocTagName }; function getTypeScriptMemberSymbols() { + isGlobalCompletion = false; isMemberCompletion = true; isNewIdentifierLocation = false; if (node.kind === 69 || node.kind === 139 || node.kind === 172) { @@ -51483,6 +52167,7 @@ var ts; var attrsType = void 0; if ((jsxContainer.kind === 242) || (jsxContainer.kind === 243)) { attrsType = typeChecker.getJsxElementAttributesType(jsxContainer); + isGlobalCompletion = false; if (attrsType) { symbols = filterJsxAttributes(typeChecker.getPropertiesOfType(attrsType), jsxContainer.attributes); isMemberCompletion = true; @@ -51842,8 +52527,8 @@ var ts; if (element.getStart() <= position && position <= element.getEnd()) { continue; } - var name_47 = element.propertyName || element.name; - existingImportsOrExports[name_47.text] = true; + var name_48 = element.propertyName || element.name; + existingImportsOrExports[name_48.text] = true; } if (!ts.someProperties(existingImportsOrExports)) { return ts.filter(exportsOfModule, function (e) { return e.name !== "default"; }); @@ -51927,7 +52612,7 @@ var ts; sortText: "0" }); } - var tripleSlashDirectiveFragmentRegex = /^(\/\/\/\s* sourceFile.text.length) { return getBaseIndentation(options); } - if (options.IndentStyle === ts.IndentStyle.None) { + if (options.indentStyle === ts.IndentStyle.None) { return 0; } var precedingToken = ts.findPrecedingToken(position, sourceFile); @@ -58576,7 +59140,7 @@ var ts; return 0; } var lineAtPosition = sourceFile.getLineAndCharacterOfPosition(position).line; - if (options.IndentStyle === ts.IndentStyle.Block) { + if (options.indentStyle === ts.IndentStyle.Block) { var current_1 = position; while (current_1 > 0) { var char = sourceFile.text.charCodeAt(current_1); @@ -58605,7 +59169,7 @@ var ts; indentationDelta = 0; } else { - indentationDelta = lineAtPosition !== currentStart.line ? options.IndentSize : 0; + indentationDelta = lineAtPosition !== currentStart.line ? options.indentSize : 0; } break; } @@ -58615,7 +59179,7 @@ var ts; } actualIndentation = getLineIndentationWhenExpressionIsInMultiLine(current, sourceFile, options); if (actualIndentation !== -1) { - return actualIndentation + options.IndentSize; + return actualIndentation + options.indentSize; } previous = current; current = current.parent; @@ -58626,15 +59190,15 @@ var ts; return getIndentationForNodeWorker(current, currentStart, undefined, indentationDelta, sourceFile, options); } SmartIndenter.getIndentation = getIndentation; - function getBaseIndentation(options) { - return options.BaseIndentSize || 0; - } - SmartIndenter.getBaseIndentation = getBaseIndentation; function getIndentationForNode(n, ignoreActualIndentationRange, sourceFile, options) { var start = sourceFile.getLineAndCharacterOfPosition(n.getStart(sourceFile)); return getIndentationForNodeWorker(n, start, ignoreActualIndentationRange, 0, sourceFile, options); } SmartIndenter.getIndentationForNode = getIndentationForNode; + function getBaseIndentation(options) { + return options.baseIndentSize || 0; + } + SmartIndenter.getBaseIndentation = getBaseIndentation; function getIndentationForNodeWorker(current, currentStart, ignoreActualIndentationRange, indentationDelta, sourceFile, options) { var parent = current.parent; var parentStart; @@ -58664,7 +59228,7 @@ var ts; } } if (shouldIndentChildNode(parent, current) && !parentAndChildShareLine) { - indentationDelta += options.IndentSize; + indentationDelta += options.indentSize; } current = parent; currentStart = parentStart; @@ -58842,7 +59406,7 @@ var ts; break; } if (ch === 9) { - column += options.TabSize + (column % options.TabSize); + column += options.tabSize + (column % options.tabSize); } else { column++; @@ -58940,11 +59504,116 @@ var ts; })(formatting = ts.formatting || (ts.formatting = {})); })(ts || (ts = {})); var ts; +(function (ts) { + var codefix; + (function (codefix) { + var codeFixes = ts.createMap(); + function registerCodeFix(action) { + ts.forEach(action.errorCodes, function (error) { + var fixes = codeFixes[error]; + if (!fixes) { + fixes = []; + codeFixes[error] = fixes; + } + fixes.push(action); + }); + } + codefix.registerCodeFix = registerCodeFix; + function getSupportedErrorCodes() { + return Object.keys(codeFixes); + } + codefix.getSupportedErrorCodes = getSupportedErrorCodes; + function getFixes(context) { + var fixes = codeFixes[context.errorCode]; + var allActions = []; + ts.forEach(fixes, function (f) { + var actions = f.getCodeActions(context); + if (actions && actions.length > 0) { + allActions = allActions.concat(actions); + } + }); + return allActions; + } + codefix.getFixes = getFixes; + })(codefix = ts.codefix || (ts.codefix = {})); +})(ts || (ts = {})); +var ts; +(function (ts) { + var codefix; + (function (codefix) { + function getOpenBraceEnd(constructor, sourceFile) { + return constructor.body.getFirstToken(sourceFile).getEnd(); + } + codefix.registerCodeFix({ + errorCodes: [ts.Diagnostics.Constructors_for_derived_classes_must_contain_a_super_call.code], + getCodeActions: function (context) { + var sourceFile = context.sourceFile; + var token = ts.getTokenAtPosition(sourceFile, context.span.start); + if (token.kind !== 121) { + return undefined; + } + var newPosition = getOpenBraceEnd(token.parent, sourceFile); + return [{ + description: ts.getLocaleSpecificMessage(ts.Diagnostics.Add_missing_super_call), + changes: [{ fileName: sourceFile.fileName, textChanges: [{ newText: "super();", span: { start: newPosition, length: 0 } }] }] + }]; + } + }); + codefix.registerCodeFix({ + errorCodes: [ts.Diagnostics.super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class.code], + getCodeActions: function (context) { + var sourceFile = context.sourceFile; + var token = ts.getTokenAtPosition(sourceFile, context.span.start); + if (token.kind !== 97) { + return undefined; + } + var constructor = ts.getContainingFunction(token); + var superCall = findSuperCall(constructor.body); + if (!superCall) { + return undefined; + } + if (superCall.expression && superCall.expression.kind == 174) { + var arguments_1 = superCall.expression.arguments; + for (var i = 0; i < arguments_1.length; i++) { + if (arguments_1[i].expression === token) { + return undefined; + } + } + } + var newPosition = getOpenBraceEnd(constructor, sourceFile); + var changes = [{ + fileName: sourceFile.fileName, textChanges: [{ + newText: superCall.getText(sourceFile), + span: { start: newPosition, length: 0 } + }, + { + newText: "", + span: { start: superCall.getStart(sourceFile), length: superCall.getWidth(sourceFile) } + }] + }]; + return [{ + description: ts.getLocaleSpecificMessage(ts.Diagnostics.Make_super_call_the_first_statement_in_the_constructor), + changes: changes + }]; + function findSuperCall(n) { + if (n.kind === 202 && ts.isSuperCall(n.expression)) { + return n; + } + if (ts.isFunctionLike(n)) { + return undefined; + } + return ts.forEachChild(n, findSuperCall); + } + } + }); + })(codefix = ts.codefix || (ts.codefix = {})); +})(ts || (ts = {})); +var ts; (function (ts) { ts.servicesVersion = "0.5"; function createNode(kind, pos, end, parent) { var node = kind >= 139 ? new NodeObject(kind, pos, end) : - kind === 69 ? new IdentifierObject(kind, pos, end) : + kind === 69 ? new IdentifierObject(69, pos, end) : new TokenObject(kind, pos, end); node.parent = parent; return node; @@ -59167,15 +59836,16 @@ var ts; var TokenObject = (function (_super) { __extends(TokenObject, _super); function TokenObject(kind, pos, end) { - _super.call(this, pos, end); - this.kind = kind; + var _this = _super.call(this, pos, end) || this; + _this.kind = kind; + return _this; } return TokenObject; }(TokenOrIdentifierObject)); var IdentifierObject = (function (_super) { __extends(IdentifierObject, _super); function IdentifierObject(kind, pos, end) { - _super.call(this, pos, end); + return _super.call(this, pos, end) || this; } return IdentifierObject; }(TokenOrIdentifierObject)); @@ -59249,7 +59919,7 @@ var ts; var SourceFileObject = (function (_super) { __extends(SourceFileObject, _super); function SourceFileObject(kind, pos, end) { - _super.call(this, kind, pos, end); + return _super.call(this, kind, pos, end) || this; } SourceFileObject.prototype.update = function (newText, textChangeRange) { return ts.updateSourceFile(this, newText, textChangeRange); @@ -59406,6 +60076,30 @@ var ts; getSignatureConstructor: function () { return SignatureObject; } }; } + function toEditorSettings(optionsAsMap) { + var allPropertiesAreCamelCased = true; + for (var key in optionsAsMap) { + if (ts.hasProperty(optionsAsMap, key) && !isCamelCase(key)) { + allPropertiesAreCamelCased = false; + break; + } + } + if (allPropertiesAreCamelCased) { + return optionsAsMap; + } + var settings = {}; + for (var key in optionsAsMap) { + if (ts.hasProperty(optionsAsMap, key)) { + var newKey = isCamelCase(key) ? key : key.charAt(0).toLowerCase() + key.substr(1); + settings[newKey] = optionsAsMap[key]; + } + } + return settings; + } + ts.toEditorSettings = toEditorSettings; + function isCamelCase(s) { + return !s.length || s.charAt(0) === s.charAt(0).toLowerCase(); + } function displayPartsToString(displayParts) { if (displayParts) { return ts.map(displayParts, function (displayPart) { return displayPart.text; }).join(""); @@ -59420,6 +60114,10 @@ var ts; }; } ts.getDefaultCompilerOptions = getDefaultCompilerOptions; + function getSupportedCodeFixes() { + return ts.codefix.getSupportedErrorCodes(); + } + ts.getSupportedCodeFixes = getSupportedCodeFixes; var HostCache = (function () { function HostCache(host, getCanonicalFileName) { this.host = host; @@ -59583,7 +60281,8 @@ var ts; var ruleProvider; var program; var lastProjectVersion; - var useCaseSensitivefileNames = false; + var lastTypesRootVersion = 0; + var useCaseSensitivefileNames = host.useCaseSensitiveFileNames && host.useCaseSensitiveFileNames(); var cancellationToken = new CancellationTokenObject(host.getCancellationToken && host.getCancellationToken()); var currentDirectory = host.getCurrentDirectory(); if (!ts.localizedDiagnosticMessages && host.getLocalizedDiagnosticMessages) { @@ -59619,6 +60318,12 @@ var ts; lastProjectVersion = hostProjectVersion; } } + var typeRootsVersion = host.getTypeRootsVersion ? host.getTypeRootsVersion() : 0; + if (lastTypesRootVersion !== typeRootsVersion) { + log("TypeRoots version has changed; provide new program"); + program = undefined; + lastTypesRootVersion = typeRootsVersion; + } var hostCache = new HostCache(host, getCanonicalFileName); if (programUpToDate()) { return; @@ -59878,12 +60583,12 @@ var ts; synchronizeHostData(); return ts.FindAllReferences.findReferencedSymbols(program.getTypeChecker(), cancellationToken, program.getSourceFiles(), getValidSourceFile(fileName), position, findInStrings, findInComments); } - function getNavigateToItems(searchValue, maxResultCount, fileName) { + function getNavigateToItems(searchValue, maxResultCount, fileName, excludeDtsFiles) { synchronizeHostData(); var sourceFiles = fileName ? [getValidSourceFile(fileName)] : program.getSourceFiles(); - return ts.NavigateTo.getNavigateToItems(sourceFiles, program.getTypeChecker(), cancellationToken, searchValue, maxResultCount); + return ts.NavigateTo.getNavigateToItems(sourceFiles, program.getTypeChecker(), cancellationToken, searchValue, maxResultCount, excludeDtsFiles); } - function getEmitOutput(fileName) { + function getEmitOutput(fileName, emitOnlyDtsFiles) { synchronizeHostData(); var sourceFile = getValidSourceFile(fileName); var outputFiles = []; @@ -59894,7 +60599,7 @@ var ts; text: data }); } - var emitOutput = program.emit(sourceFile, writeFile, cancellationToken); + var emitOutput = program.emit(sourceFile, writeFile, cancellationToken, emitOnlyDtsFiles); return { outputFiles: outputFiles, emitSkipped: emitOutput.emitSkipped @@ -59957,14 +60662,26 @@ var ts; return ts.BreakpointResolver.spanInSourceFileAtLocation(sourceFile, position); } function getNavigationBarItems(fileName) { - var sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName); - return ts.NavigationBar.getNavigationBarItems(sourceFile); + return ts.NavigationBar.getNavigationBarItems(syntaxTreeCache.getCurrentSourceFile(fileName)); + } + function getNavigationTree(fileName) { + return ts.NavigationBar.getNavigationTree(syntaxTreeCache.getCurrentSourceFile(fileName)); + } + function isTsOrTsxFile(fileName) { + var kind = ts.getScriptKind(fileName, host); + return kind === 3 || kind === 4; } function getSemanticClassifications(fileName, span) { + if (!isTsOrTsxFile(fileName)) { + return []; + } synchronizeHostData(); return ts.getSemanticClassifications(program.getTypeChecker(), cancellationToken, getValidSourceFile(fileName), program.getClassifiableNames(), span); } function getEncodedSemanticClassifications(fileName, span) { + if (!isTsOrTsxFile(fileName)) { + return { spans: [], endOfLineState: 0 }; + } synchronizeHostData(); return ts.getEncodedSemanticClassifications(program.getTypeChecker(), cancellationToken, getValidSourceFile(fileName), program.getClassifiableNames(), span); } @@ -60020,34 +60737,60 @@ var ts; } function getIndentationAtPosition(fileName, position, editorOptions) { var start = ts.timestamp(); + var settings = toEditorSettings(editorOptions); var sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName); log("getIndentationAtPosition: getCurrentSourceFile: " + (ts.timestamp() - start)); start = ts.timestamp(); - var result = ts.formatting.SmartIndenter.getIndentation(position, sourceFile, editorOptions); + var result = ts.formatting.SmartIndenter.getIndentation(position, sourceFile, settings); log("getIndentationAtPosition: computeIndentation : " + (ts.timestamp() - start)); return result; } function getFormattingEditsForRange(fileName, start, end, options) { var sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName); - return ts.formatting.formatSelection(start, end, sourceFile, getRuleProvider(options), options); + var settings = toEditorSettings(options); + return ts.formatting.formatSelection(start, end, sourceFile, getRuleProvider(settings), settings); } function getFormattingEditsForDocument(fileName, options) { var sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName); - return ts.formatting.formatDocument(sourceFile, getRuleProvider(options), options); + var settings = toEditorSettings(options); + return ts.formatting.formatDocument(sourceFile, getRuleProvider(settings), settings); } function getFormattingEditsAfterKeystroke(fileName, position, key, options) { var sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName); + var settings = toEditorSettings(options); if (key === "}") { - return ts.formatting.formatOnClosingCurly(position, sourceFile, getRuleProvider(options), options); + return ts.formatting.formatOnClosingCurly(position, sourceFile, getRuleProvider(settings), settings); } else if (key === ";") { - return ts.formatting.formatOnSemicolon(position, sourceFile, getRuleProvider(options), options); + return ts.formatting.formatOnSemicolon(position, sourceFile, getRuleProvider(settings), settings); } else if (key === "\n") { - return ts.formatting.formatOnEnter(position, sourceFile, getRuleProvider(options), options); + return ts.formatting.formatOnEnter(position, sourceFile, getRuleProvider(settings), settings); } return []; } + function getCodeFixesAtPosition(fileName, start, end, errorCodes) { + synchronizeHostData(); + var sourceFile = getValidSourceFile(fileName); + var span = { start: start, length: end - start }; + var newLineChar = ts.getNewLineOrDefaultFromHost(host); + var allFixes = []; + ts.forEach(errorCodes, function (error) { + cancellationToken.throwIfCancellationRequested(); + var context = { + errorCode: error, + sourceFile: sourceFile, + span: span, + program: program, + newLineCharacter: newLineChar + }; + var fixes = ts.codefix.getFixes(context); + if (fixes) { + allFixes = allFixes.concat(fixes); + } + }); + return allFixes; + } function getDocCommentTemplateAtPosition(fileName, position) { return ts.JsDoc.getDocCommentTemplateAtPosition(ts.getNewLineOrDefaultFromHost(host), syntaxTreeCache.getCurrentSourceFile(fileName), position); } @@ -60159,6 +60902,7 @@ var ts; getRenameInfo: getRenameInfo, findRenameLocations: findRenameLocations, getNavigationBarItems: getNavigationBarItems, + getNavigationTree: getNavigationTree, getOutliningSpans: getOutliningSpans, getTodoComments: getTodoComments, getBraceMatchingAtPosition: getBraceMatchingAtPosition, @@ -60168,6 +60912,7 @@ var ts; getFormattingEditsAfterKeystroke: getFormattingEditsAfterKeystroke, getDocCommentTemplateAtPosition: getDocCommentTemplateAtPosition, isValidBraceCompletionAtPosition: isValidBraceCompletionAtPosition, + getCodeFixesAtPosition: getCodeFixesAtPosition, getEmitOutput: getEmitOutput, getNonBoundSourceFile: getNonBoundSourceFile, getSourceFile: getSourceFile, @@ -60210,2380 +60955,3827 @@ var ts; } } } - } - function isArgumentOfElementAccessExpression(node) { - return node && - node.parent && - node.parent.kind === 173 && - node.parent.argumentExpression === node; - } - function getDefaultLibFilePath(options) { - if (typeof __dirname !== "undefined") { - return __dirname + ts.directorySeparator + ts.getDefaultLibFileName(options); - } - throw new Error("getDefaultLibFilePath is only supported when consumed as a node module. "); - } - ts.getDefaultLibFilePath = getDefaultLibFilePath; - function initializeServices() { - ts.objectAllocator = getServicesObjectAllocator(); - } - initializeServices(); + } + function isArgumentOfElementAccessExpression(node) { + return node && + node.parent && + node.parent.kind === 173 && + node.parent.argumentExpression === node; + } + function getDefaultLibFilePath(options) { + if (typeof __dirname !== "undefined") { + return __dirname + ts.directorySeparator + ts.getDefaultLibFileName(options); + } + throw new Error("getDefaultLibFilePath is only supported when consumed as a node module. "); + } + ts.getDefaultLibFilePath = getDefaultLibFilePath; + function initializeServices() { + ts.objectAllocator = getServicesObjectAllocator(); + } + initializeServices(); +})(ts || (ts = {})); +var ts; +(function (ts) { + var server; + (function (server) { + var ScriptInfo = (function () { + function ScriptInfo(host, fileName, content, scriptKind, isOpen, hasMixedContent) { + if (isOpen === void 0) { isOpen = false; } + if (hasMixedContent === void 0) { hasMixedContent = false; } + this.host = host; + this.fileName = fileName; + this.scriptKind = scriptKind; + this.isOpen = isOpen; + this.hasMixedContent = hasMixedContent; + this.containingProjects = []; + this.path = ts.toPath(fileName, host.getCurrentDirectory(), ts.createGetCanonicalFileName(host.useCaseSensitiveFileNames)); + this.svc = server.ScriptVersionCache.fromString(host, content); + this.scriptKind = scriptKind + ? scriptKind + : ts.getScriptKindFromFileName(fileName); + } + ScriptInfo.prototype.getFormatCodeSettings = function () { + return this.formatCodeSettings; + }; + ScriptInfo.prototype.attachToProject = function (project) { + var isNew = !this.isAttached(project); + if (isNew) { + this.containingProjects.push(project); + } + return isNew; + }; + ScriptInfo.prototype.isAttached = function (project) { + switch (this.containingProjects.length) { + case 0: return false; + case 1: return this.containingProjects[0] === project; + case 2: return this.containingProjects[0] === project || this.containingProjects[1] === project; + default: return ts.contains(this.containingProjects, project); + } + }; + ScriptInfo.prototype.detachFromProject = function (project) { + switch (this.containingProjects.length) { + case 0: + return; + case 1: + if (this.containingProjects[0] === project) { + this.containingProjects.pop(); + } + break; + case 2: + if (this.containingProjects[0] === project) { + this.containingProjects[0] = this.containingProjects.pop(); + } + else if (this.containingProjects[1] === project) { + this.containingProjects.pop(); + } + break; + default: + server.removeItemFromSet(this.containingProjects, project); + break; + } + }; + ScriptInfo.prototype.detachAllProjects = function () { + for (var _i = 0, _a = this.containingProjects; _i < _a.length; _i++) { + var p = _a[_i]; + p.removeFile(this, false); + } + this.containingProjects.length = 0; + }; + ScriptInfo.prototype.getDefaultProject = function () { + if (this.containingProjects.length === 0) { + return server.Errors.ThrowNoProject(); + } + ts.Debug.assert(this.containingProjects.length !== 0); + return this.containingProjects[0]; + }; + ScriptInfo.prototype.setFormatOptions = function (formatSettings) { + if (formatSettings) { + if (!this.formatCodeSettings) { + this.formatCodeSettings = server.getDefaultFormatCodeSettings(this.host); + } + server.mergeMaps(this.formatCodeSettings, formatSettings); + } + }; + ScriptInfo.prototype.setWatcher = function (watcher) { + this.stopWatcher(); + this.fileWatcher = watcher; + }; + ScriptInfo.prototype.stopWatcher = function () { + if (this.fileWatcher) { + this.fileWatcher.close(); + this.fileWatcher = undefined; + } + }; + ScriptInfo.prototype.getLatestVersion = function () { + return this.svc.latestVersion().toString(); + }; + ScriptInfo.prototype.reload = function (script) { + this.svc.reload(script); + this.markContainingProjectsAsDirty(); + }; + ScriptInfo.prototype.saveTo = function (fileName) { + var snap = this.snap(); + this.host.writeFile(fileName, snap.getText(0, snap.getLength())); + }; + ScriptInfo.prototype.reloadFromFile = function () { + if (this.hasMixedContent) { + this.reload(""); + } + else { + this.svc.reloadFromFile(this.fileName); + this.markContainingProjectsAsDirty(); + } + }; + ScriptInfo.prototype.snap = function () { + return this.svc.getSnapshot(); + }; + ScriptInfo.prototype.getLineInfo = function (line) { + var snap = this.snap(); + return snap.index.lineNumberToInfo(line); + }; + ScriptInfo.prototype.editContent = function (start, end, newText) { + this.svc.edit(start, end - start, newText); + this.markContainingProjectsAsDirty(); + }; + ScriptInfo.prototype.markContainingProjectsAsDirty = function () { + for (var _i = 0, _a = this.containingProjects; _i < _a.length; _i++) { + var p = _a[_i]; + p.markAsDirty(); + } + }; + ScriptInfo.prototype.lineToTextSpan = function (line) { + var index = this.snap().index; + var lineInfo = index.lineNumberToInfo(line + 1); + var len; + if (lineInfo.leaf) { + len = lineInfo.leaf.text.length; + } + else { + var nextLineInfo = index.lineNumberToInfo(line + 2); + len = nextLineInfo.offset - lineInfo.offset; + } + return ts.createTextSpan(lineInfo.offset, len); + }; + ScriptInfo.prototype.lineOffsetToPosition = function (line, offset) { + var index = this.snap().index; + var lineInfo = index.lineNumberToInfo(line); + return (lineInfo.offset + offset - 1); + }; + ScriptInfo.prototype.positionToLineOffset = function (position) { + var index = this.snap().index; + var lineOffset = index.charOffsetToLineNumberAndPos(position); + return { line: lineOffset.line, offset: lineOffset.offset + 1 }; + }; + return ScriptInfo; + }()); + server.ScriptInfo = ScriptInfo; + })(server = ts.server || (ts.server = {})); +})(ts || (ts = {})); +var ts; +(function (ts) { + var server; + (function (server) { + var LSHost = (function () { + function LSHost(host, project, cancellationToken) { + var _this = this; + this.host = host; + this.project = project; + this.cancellationToken = cancellationToken; + this.getCanonicalFileName = ts.createGetCanonicalFileName(this.host.useCaseSensitiveFileNames); + this.resolvedModuleNames = ts.createFileMap(); + this.resolvedTypeReferenceDirectives = ts.createFileMap(); + if (host.trace) { + this.trace = function (s) { return host.trace(s); }; + } + this.resolveModuleName = function (moduleName, containingFile, compilerOptions, host) { + var primaryResult = ts.resolveModuleName(moduleName, containingFile, compilerOptions, host); + if (primaryResult.resolvedModule) { + if (ts.fileExtensionIsAny(primaryResult.resolvedModule.resolvedFileName, ts.supportedTypeScriptExtensions)) { + return primaryResult; + } + } + var secondaryLookupFailedLookupLocations = []; + var globalCache = _this.project.projectService.typingsInstaller.globalTypingsCacheLocation; + if (_this.project.getTypingOptions().enableAutoDiscovery && globalCache) { + var traceEnabled = ts.isTraceEnabled(compilerOptions, host); + if (traceEnabled) { + ts.trace(host, ts.Diagnostics.Auto_discovery_for_typings_is_enabled_in_project_0_Running_extra_resolution_pass_for_module_1_using_cache_location_2, _this.project.getProjectName(), moduleName, globalCache); + } + var state = { compilerOptions: compilerOptions, host: host, skipTsx: false, traceEnabled: traceEnabled }; + var resolvedName = ts.loadModuleFromNodeModules(moduleName, globalCache, secondaryLookupFailedLookupLocations, state, true); + if (resolvedName) { + return ts.createResolvedModule(resolvedName, true, primaryResult.failedLookupLocations.concat(secondaryLookupFailedLookupLocations)); + } + } + if (!primaryResult.resolvedModule && secondaryLookupFailedLookupLocations.length) { + primaryResult.failedLookupLocations = primaryResult.failedLookupLocations.concat(secondaryLookupFailedLookupLocations); + } + return primaryResult; + }; + } + LSHost.prototype.resolveNamesWithLocalCache = function (names, containingFile, cache, loader, getResult) { + var path = ts.toPath(containingFile, this.host.getCurrentDirectory(), this.getCanonicalFileName); + var currentResolutionsInFile = cache.get(path); + var newResolutions = ts.createMap(); + var resolvedModules = []; + var compilerOptions = this.getCompilationSettings(); + var lastDeletedFileName = this.project.projectService.lastDeletedFile && this.project.projectService.lastDeletedFile.fileName; + for (var _i = 0, names_3 = names; _i < names_3.length; _i++) { + var name_52 = names_3[_i]; + var resolution = newResolutions[name_52]; + if (!resolution) { + var existingResolution = currentResolutionsInFile && currentResolutionsInFile[name_52]; + if (moduleResolutionIsValid(existingResolution)) { + resolution = existingResolution; + } + else { + newResolutions[name_52] = resolution = loader(name_52, containingFile, compilerOptions, this); + } + } + ts.Debug.assert(resolution !== undefined); + resolvedModules.push(getResult(resolution)); + } + cache.set(path, newResolutions); + return resolvedModules; + function moduleResolutionIsValid(resolution) { + if (!resolution) { + return false; + } + var result = getResult(resolution); + if (result) { + if (result.resolvedFileName && result.resolvedFileName === lastDeletedFileName) { + return false; + } + return true; + } + return resolution.failedLookupLocations.length === 0; + } + }; + LSHost.prototype.getProjectVersion = function () { + return this.project.getProjectVersion(); + }; + LSHost.prototype.getCompilationSettings = function () { + return this.compilationSettings; + }; + LSHost.prototype.useCaseSensitiveFileNames = function () { + return this.host.useCaseSensitiveFileNames; + }; + LSHost.prototype.getCancellationToken = function () { + return this.cancellationToken; + }; + LSHost.prototype.resolveTypeReferenceDirectives = function (typeDirectiveNames, containingFile) { + return this.resolveNamesWithLocalCache(typeDirectiveNames, containingFile, this.resolvedTypeReferenceDirectives, ts.resolveTypeReferenceDirective, function (m) { return m.resolvedTypeReferenceDirective; }); + }; + LSHost.prototype.resolveModuleNames = function (moduleNames, containingFile) { + return this.resolveNamesWithLocalCache(moduleNames, containingFile, this.resolvedModuleNames, this.resolveModuleName, function (m) { return m.resolvedModule; }); + }; + LSHost.prototype.getDefaultLibFileName = function () { + var nodeModuleBinDir = ts.getDirectoryPath(ts.normalizePath(this.host.getExecutingFilePath())); + return ts.combinePaths(nodeModuleBinDir, ts.getDefaultLibFileName(this.compilationSettings)); + }; + LSHost.prototype.getScriptSnapshot = function (filename) { + var scriptInfo = this.project.getScriptInfoLSHost(filename); + if (scriptInfo) { + return scriptInfo.snap(); + } + }; + LSHost.prototype.getScriptFileNames = function () { + return this.project.getRootFilesLSHost(); + }; + LSHost.prototype.getTypeRootsVersion = function () { + return this.project.typesVersion; + }; + LSHost.prototype.getScriptKind = function (fileName) { + var info = this.project.getScriptInfoLSHost(fileName); + return info && info.scriptKind; + }; + LSHost.prototype.getScriptVersion = function (filename) { + var info = this.project.getScriptInfoLSHost(filename); + return info && info.getLatestVersion(); + }; + LSHost.prototype.getCurrentDirectory = function () { + return this.host.getCurrentDirectory(); + }; + LSHost.prototype.resolvePath = function (path) { + return this.host.resolvePath(path); + }; + LSHost.prototype.fileExists = function (path) { + return this.host.fileExists(path); + }; + LSHost.prototype.readFile = function (fileName) { + return this.host.readFile(fileName); + }; + LSHost.prototype.directoryExists = function (path) { + return this.host.directoryExists(path); + }; + LSHost.prototype.readDirectory = function (path, extensions, exclude, include) { + return this.host.readDirectory(path, extensions, exclude, include); + }; + LSHost.prototype.getDirectories = function (path) { + return this.host.getDirectories(path); + }; + LSHost.prototype.notifyFileRemoved = function (info) { + this.resolvedModuleNames.remove(info.path); + this.resolvedTypeReferenceDirectives.remove(info.path); + }; + LSHost.prototype.setCompilationSettings = function (opt) { + this.compilationSettings = opt; + this.resolvedModuleNames.clear(); + this.resolvedTypeReferenceDirectives.clear(); + }; + return LSHost; + }()); + server.LSHost = LSHost; + })(server = ts.server || (ts.server = {})); +})(ts || (ts = {})); +var ts; +(function (ts) { + var server; + (function (server) { + server.nullTypingsInstaller = { + enqueueInstallTypingsRequest: function () { }, + attach: function (projectService) { }, + onProjectClosed: function (p) { }, + globalTypingsCacheLocation: undefined + }; + var TypingsCacheEntry = (function () { + function TypingsCacheEntry() { + } + return TypingsCacheEntry; + }()); + function setIsEqualTo(arr1, arr2) { + if (arr1 === arr2) { + return true; + } + if ((arr1 || server.emptyArray).length === 0 && (arr2 || server.emptyArray).length === 0) { + return true; + } + var set = ts.createMap(); + var unique = 0; + for (var _i = 0, arr1_1 = arr1; _i < arr1_1.length; _i++) { + var v = arr1_1[_i]; + if (set[v] !== true) { + set[v] = true; + unique++; + } + } + for (var _a = 0, arr2_1 = arr2; _a < arr2_1.length; _a++) { + var v = arr2_1[_a]; + if (!ts.hasProperty(set, v)) { + return false; + } + if (set[v] === true) { + set[v] = false; + unique--; + } + } + return unique === 0; + } + function typingOptionsChanged(opt1, opt2) { + return opt1.enableAutoDiscovery !== opt2.enableAutoDiscovery || + !setIsEqualTo(opt1.include, opt2.include) || + !setIsEqualTo(opt1.exclude, opt2.exclude); + } + function compilerOptionsChanged(opt1, opt2) { + return opt1.allowJs != opt2.allowJs; + } + function toTypingsArray(arr) { + arr.sort(); + return arr; + } + var TypingsCache = (function () { + function TypingsCache(installer) { + this.installer = installer; + this.perProjectCache = ts.createMap(); + } + TypingsCache.prototype.getTypingsForProject = function (project, forceRefresh) { + var typingOptions = project.getTypingOptions(); + if (!typingOptions || !typingOptions.enableAutoDiscovery) { + return server.emptyArray; + } + var entry = this.perProjectCache[project.getProjectName()]; + var result = entry ? entry.typings : server.emptyArray; + if (forceRefresh || !entry || typingOptionsChanged(typingOptions, entry.typingOptions) || compilerOptionsChanged(project.getCompilerOptions(), entry.compilerOptions)) { + this.perProjectCache[project.getProjectName()] = { + compilerOptions: project.getCompilerOptions(), + typingOptions: typingOptions, + typings: result, + poisoned: true + }; + this.installer.enqueueInstallTypingsRequest(project, typingOptions); + } + return result; + }; + TypingsCache.prototype.invalidateCachedTypingsForProject = function (project) { + var typingOptions = project.getTypingOptions(); + if (!typingOptions.enableAutoDiscovery) { + return; + } + this.installer.enqueueInstallTypingsRequest(project, typingOptions); + }; + TypingsCache.prototype.updateTypingsForProject = function (projectName, compilerOptions, typingOptions, newTypings) { + this.perProjectCache[projectName] = { + compilerOptions: compilerOptions, + typingOptions: typingOptions, + typings: toTypingsArray(newTypings), + poisoned: false + }; + }; + TypingsCache.prototype.onProjectClosed = function (project) { + delete this.perProjectCache[project.getProjectName()]; + this.installer.onProjectClosed(project); + }; + return TypingsCache; + }()); + server.TypingsCache = TypingsCache; + })(server = ts.server || (ts.server = {})); +})(ts || (ts = {})); +var ts; +(function (ts) { + var server; + (function (server) { + var crypto = require("crypto"); + function shouldEmitFile(scriptInfo) { + return !scriptInfo.hasMixedContent; + } + server.shouldEmitFile = shouldEmitFile; + var BuilderFileInfo = (function () { + function BuilderFileInfo(scriptInfo, project) { + this.scriptInfo = scriptInfo; + this.project = project; + } + BuilderFileInfo.prototype.isExternalModuleOrHasOnlyAmbientExternalModules = function () { + var sourceFile = this.getSourceFile(); + return ts.isExternalModule(sourceFile) || this.containsOnlyAmbientModules(sourceFile); + }; + BuilderFileInfo.prototype.containsOnlyAmbientModules = function (sourceFile) { + for (var _i = 0, _a = sourceFile.statements; _i < _a.length; _i++) { + var statement = _a[_i]; + if (statement.kind !== 225 || statement.name.kind !== 9) { + return false; + } + } + return true; + }; + BuilderFileInfo.prototype.computeHash = function (text) { + return crypto.createHash("md5") + .update(text) + .digest("base64"); + }; + BuilderFileInfo.prototype.getSourceFile = function () { + return this.project.getSourceFile(this.scriptInfo.path); + }; + BuilderFileInfo.prototype.updateShapeSignature = function () { + var sourceFile = this.getSourceFile(); + if (!sourceFile) { + return true; + } + var lastSignature = this.lastCheckedShapeSignature; + if (sourceFile.isDeclarationFile) { + this.lastCheckedShapeSignature = this.computeHash(sourceFile.text); + } + else { + var emitOutput = this.project.getFileEmitOutput(this.scriptInfo, true); + if (emitOutput.outputFiles && emitOutput.outputFiles.length > 0) { + this.lastCheckedShapeSignature = this.computeHash(emitOutput.outputFiles[0].text); + } + } + return !lastSignature || this.lastCheckedShapeSignature !== lastSignature; + }; + return BuilderFileInfo; + }()); + server.BuilderFileInfo = BuilderFileInfo; + var AbstractBuilder = (function () { + function AbstractBuilder(project, ctor) { + this.project = project; + this.ctor = ctor; + this.fileInfos = ts.createFileMap(); + } + AbstractBuilder.prototype.getFileInfo = function (path) { + return this.fileInfos.get(path); + }; + AbstractBuilder.prototype.getOrCreateFileInfo = function (path) { + var fileInfo = this.getFileInfo(path); + if (!fileInfo) { + var scriptInfo = this.project.getScriptInfo(path); + fileInfo = new this.ctor(scriptInfo, this.project); + this.setFileInfo(path, fileInfo); + } + return fileInfo; + }; + AbstractBuilder.prototype.getFileInfoPaths = function () { + return this.fileInfos.getKeys(); + }; + AbstractBuilder.prototype.setFileInfo = function (path, info) { + this.fileInfos.set(path, info); + }; + AbstractBuilder.prototype.removeFileInfo = function (path) { + this.fileInfos.remove(path); + }; + AbstractBuilder.prototype.forEachFileInfo = function (action) { + this.fileInfos.forEachValue(function (path, value) { return action(value); }); + }; + AbstractBuilder.prototype.emitFile = function (scriptInfo, writeFile) { + var fileInfo = this.getFileInfo(scriptInfo.path); + if (!fileInfo) { + return false; + } + var _a = this.project.getFileEmitOutput(fileInfo.scriptInfo, false), emitSkipped = _a.emitSkipped, outputFiles = _a.outputFiles; + if (!emitSkipped) { + var projectRootPath = this.project.getProjectRootPath(); + for (var _i = 0, outputFiles_1 = outputFiles; _i < outputFiles_1.length; _i++) { + var outputFile = outputFiles_1[_i]; + var outputFileAbsoluteFileName = ts.getNormalizedAbsolutePath(outputFile.name, projectRootPath ? projectRootPath : ts.getDirectoryPath(scriptInfo.fileName)); + writeFile(outputFileAbsoluteFileName, outputFile.text, outputFile.writeByteOrderMark); + } + } + return !emitSkipped; + }; + return AbstractBuilder; + }()); + var NonModuleBuilder = (function (_super) { + __extends(NonModuleBuilder, _super); + function NonModuleBuilder(project) { + var _this = _super.call(this, project, BuilderFileInfo) || this; + _this.project = project; + return _this; + } + NonModuleBuilder.prototype.onProjectUpdateGraph = function () { + }; + NonModuleBuilder.prototype.getFilesAffectedBy = function (scriptInfo) { + var info = this.getOrCreateFileInfo(scriptInfo.path); + var singleFileResult = scriptInfo.hasMixedContent ? [] : [scriptInfo.fileName]; + if (info.updateShapeSignature()) { + var options = this.project.getCompilerOptions(); + if (options && (options.out || options.outFile)) { + return singleFileResult; + } + return this.project.getAllEmittableFiles(); + } + return singleFileResult; + }; + return NonModuleBuilder; + }(AbstractBuilder)); + var ModuleBuilderFileInfo = (function (_super) { + __extends(ModuleBuilderFileInfo, _super); + function ModuleBuilderFileInfo() { + var _this = _super.apply(this, arguments) || this; + _this.references = []; + _this.referencedBy = []; + return _this; + } + ModuleBuilderFileInfo.compareFileInfos = function (lf, rf) { + var l = lf.scriptInfo.fileName; + var r = rf.scriptInfo.fileName; + return (l < r ? -1 : (l > r ? 1 : 0)); + }; + ; + ModuleBuilderFileInfo.addToReferenceList = function (array, fileInfo) { + if (array.length === 0) { + array.push(fileInfo); + return; + } + var insertIndex = ts.binarySearch(array, fileInfo, ModuleBuilderFileInfo.compareFileInfos); + if (insertIndex < 0) { + array.splice(~insertIndex, 0, fileInfo); + } + }; + ModuleBuilderFileInfo.removeFromReferenceList = function (array, fileInfo) { + if (!array || array.length === 0) { + return; + } + if (array[0] === fileInfo) { + array.splice(0, 1); + return; + } + var removeIndex = ts.binarySearch(array, fileInfo, ModuleBuilderFileInfo.compareFileInfos); + if (removeIndex >= 0) { + array.splice(removeIndex, 1); + } + }; + ModuleBuilderFileInfo.prototype.addReferencedBy = function (fileInfo) { + ModuleBuilderFileInfo.addToReferenceList(this.referencedBy, fileInfo); + }; + ModuleBuilderFileInfo.prototype.removeReferencedBy = function (fileInfo) { + ModuleBuilderFileInfo.removeFromReferenceList(this.referencedBy, fileInfo); + }; + ModuleBuilderFileInfo.prototype.removeFileReferences = function () { + for (var _i = 0, _a = this.references; _i < _a.length; _i++) { + var reference = _a[_i]; + reference.removeReferencedBy(this); + } + this.references = []; + }; + return ModuleBuilderFileInfo; + }(BuilderFileInfo)); + var ModuleBuilder = (function (_super) { + __extends(ModuleBuilder, _super); + function ModuleBuilder(project) { + var _this = _super.call(this, project, ModuleBuilderFileInfo) || this; + _this.project = project; + return _this; + } + ModuleBuilder.prototype.getReferencedFileInfos = function (fileInfo) { + var _this = this; + if (!fileInfo.isExternalModuleOrHasOnlyAmbientExternalModules()) { + return []; + } + var referencedFilePaths = this.project.getReferencedFiles(fileInfo.scriptInfo.path); + if (referencedFilePaths.length > 0) { + return ts.map(referencedFilePaths, function (f) { return _this.getOrCreateFileInfo(f); }).sort(ModuleBuilderFileInfo.compareFileInfos); + } + return []; + }; + ModuleBuilder.prototype.onProjectUpdateGraph = function () { + this.ensureProjectDependencyGraphUpToDate(); + }; + ModuleBuilder.prototype.ensureProjectDependencyGraphUpToDate = function () { + var _this = this; + if (!this.projectVersionForDependencyGraph || this.project.getProjectVersion() !== this.projectVersionForDependencyGraph) { + var currentScriptInfos = this.project.getScriptInfos(); + for (var _i = 0, currentScriptInfos_1 = currentScriptInfos; _i < currentScriptInfos_1.length; _i++) { + var scriptInfo = currentScriptInfos_1[_i]; + var fileInfo = this.getOrCreateFileInfo(scriptInfo.path); + this.updateFileReferences(fileInfo); + } + this.forEachFileInfo(function (fileInfo) { + if (!_this.project.containsScriptInfo(fileInfo.scriptInfo)) { + fileInfo.removeFileReferences(); + _this.removeFileInfo(fileInfo.scriptInfo.path); + } + }); + this.projectVersionForDependencyGraph = this.project.getProjectVersion(); + } + }; + ModuleBuilder.prototype.updateFileReferences = function (fileInfo) { + if (fileInfo.scriptVersionForReferences === fileInfo.scriptInfo.getLatestVersion()) { + return; + } + var newReferences = this.getReferencedFileInfos(fileInfo); + var oldReferences = fileInfo.references; + var oldIndex = 0; + var newIndex = 0; + while (oldIndex < oldReferences.length && newIndex < newReferences.length) { + var oldReference = oldReferences[oldIndex]; + var newReference = newReferences[newIndex]; + var compare = ModuleBuilderFileInfo.compareFileInfos(oldReference, newReference); + if (compare < 0) { + oldReference.removeReferencedBy(fileInfo); + oldIndex++; + } + else if (compare > 0) { + newReference.addReferencedBy(fileInfo); + newIndex++; + } + else { + oldIndex++; + newIndex++; + } + } + for (var i = oldIndex; i < oldReferences.length; i++) { + oldReferences[i].removeReferencedBy(fileInfo); + } + for (var i = newIndex; i < newReferences.length; i++) { + newReferences[i].addReferencedBy(fileInfo); + } + fileInfo.references = newReferences; + fileInfo.scriptVersionForReferences = fileInfo.scriptInfo.getLatestVersion(); + }; + ModuleBuilder.prototype.getFilesAffectedBy = function (scriptInfo) { + this.ensureProjectDependencyGraphUpToDate(); + var singleFileResult = scriptInfo.hasMixedContent ? [] : [scriptInfo.fileName]; + var fileInfo = this.getFileInfo(scriptInfo.path); + if (!fileInfo || !fileInfo.updateShapeSignature()) { + return singleFileResult; + } + if (!fileInfo.isExternalModuleOrHasOnlyAmbientExternalModules()) { + return this.project.getAllEmittableFiles(); + } + var options = this.project.getCompilerOptions(); + if (options && (options.isolatedModules || options.out || options.outFile)) { + return singleFileResult; + } + var queue = fileInfo.referencedBy.slice(0); + var fileNameSet = ts.createMap(); + fileNameSet[scriptInfo.fileName] = scriptInfo; + while (queue.length > 0) { + var processingFileInfo = queue.pop(); + if (processingFileInfo.updateShapeSignature() && processingFileInfo.referencedBy.length > 0) { + for (var _i = 0, _a = processingFileInfo.referencedBy; _i < _a.length; _i++) { + var potentialFileInfo = _a[_i]; + if (!fileNameSet[potentialFileInfo.scriptInfo.fileName]) { + queue.push(potentialFileInfo); + } + } + } + fileNameSet[processingFileInfo.scriptInfo.fileName] = processingFileInfo.scriptInfo; + } + var result = []; + for (var fileName in fileNameSet) { + if (shouldEmitFile(fileNameSet[fileName])) { + result.push(fileName); + } + } + return result; + }; + return ModuleBuilder; + }(AbstractBuilder)); + function createBuilder(project) { + var moduleKind = project.getCompilerOptions().module; + switch (moduleKind) { + case ts.ModuleKind.None: + return new NonModuleBuilder(project); + default: + return new ModuleBuilder(project); + } + } + server.createBuilder = createBuilder; + })(server = ts.server || (ts.server = {})); })(ts || (ts = {})); var ts; (function (ts) { var server; (function (server) { - var spaceCache = []; - function generateSpaces(n) { - if (!spaceCache[n]) { - var strBuilder = ""; - for (var i = 0; i < n; i++) { - strBuilder += " "; + (function (ProjectKind) { + ProjectKind[ProjectKind["Inferred"] = 0] = "Inferred"; + ProjectKind[ProjectKind["Configured"] = 1] = "Configured"; + ProjectKind[ProjectKind["External"] = 2] = "External"; + })(server.ProjectKind || (server.ProjectKind = {})); + var ProjectKind = server.ProjectKind; + function remove(items, item) { + var index = items.indexOf(item); + if (index >= 0) { + items.splice(index, 1); + } + } + function countEachFileTypes(infos) { + var result = { js: 0, jsx: 0, ts: 0, tsx: 0, dts: 0 }; + for (var _i = 0, infos_1 = infos; _i < infos_1.length; _i++) { + var info = infos_1[_i]; + switch (info.scriptKind) { + case 1: + result.js += 1; + break; + case 2: + result.jsx += 1; + break; + case 3: + ts.fileExtensionIs(info.fileName, ".d.ts") + ? result.dts += 1 + : result.ts += 1; + break; + case 4: + result.tsx += 1; + break; } - spaceCache[n] = strBuilder; } - return spaceCache[n]; + return result; } - server.generateSpaces = generateSpaces; - function generateIndentString(n, editorOptions) { - if (editorOptions.ConvertTabsToSpaces) { - return generateSpaces(n); - } - else { - var result = ""; - for (var i = 0; i < Math.floor(n / editorOptions.TabSize); i++) { - result += "\t"; - } - for (var i = 0; i < n % editorOptions.TabSize; i++) { - result += " "; - } - return result; - } + function hasOneOrMoreJsAndNoTsFiles(project) { + var counts = countEachFileTypes(project.getScriptInfos()); + return counts.js > 0 && counts.ts === 0 && counts.tsx === 0; } - server.generateIndentString = generateIndentString; - function compareNumber(a, b) { - if (a < b) { - return -1; - } - else if (a === b) { - return 0; - } - else - return 1; + function allRootFilesAreJsOrDts(project) { + var counts = countEachFileTypes(project.getRootScriptInfos()); + return counts.ts === 0 && counts.tsx === 0; } - function compareFileStart(a, b) { - if (a.file < b.file) { - return -1; - } - else if (a.file == b.file) { - var n = compareNumber(a.start.line, b.start.line); - if (n === 0) { - return compareNumber(a.start.offset, b.start.offset); + server.allRootFilesAreJsOrDts = allRootFilesAreJsOrDts; + function allFilesAreJsOrDts(project) { + var counts = countEachFileTypes(project.getScriptInfos()); + return counts.ts === 0 && counts.tsx === 0; + } + server.allFilesAreJsOrDts = allFilesAreJsOrDts; + var Project = (function () { + function Project(projectKind, projectService, documentRegistry, hasExplicitListOfFiles, languageServiceEnabled, compilerOptions, compileOnSaveEnabled) { + this.projectKind = projectKind; + this.projectService = projectService; + this.documentRegistry = documentRegistry; + this.languageServiceEnabled = languageServiceEnabled; + this.compilerOptions = compilerOptions; + this.compileOnSaveEnabled = compileOnSaveEnabled; + this.rootFiles = []; + this.rootFilesMap = ts.createFileMap(); + this.lastReportedVersion = 0; + this.projectStructureVersion = 0; + this.projectStateVersion = 0; + this.typesVersion = 0; + if (!this.compilerOptions) { + this.compilerOptions = ts.getDefaultCompilerOptions(); + this.compilerOptions.allowNonTsExtensions = true; + this.compilerOptions.allowJs = true; + } + else if (hasExplicitListOfFiles) { + this.compilerOptions.allowNonTsExtensions = true; + } + if (languageServiceEnabled) { + this.enableLanguageService(); } - else - return n; - } - else { - return 1; + else { + this.disableLanguageService(); + } + this.builder = server.createBuilder(this); + this.markAsDirty(); } - } - function formatDiag(fileName, project, diag) { - return { - start: project.compilerService.host.positionToLineOffset(fileName, diag.start), - end: project.compilerService.host.positionToLineOffset(fileName, diag.start + diag.length), - text: ts.flattenDiagnosticMessageText(diag.messageText, "\n") + Project.prototype.isNonTsProject = function () { + this.updateGraph(); + return allFilesAreJsOrDts(this); }; - } - function formatConfigFileDiag(diag) { - return { - start: undefined, - end: undefined, - text: ts.flattenDiagnosticMessageText(diag.messageText, "\n") + Project.prototype.isJsOnlyProject = function () { + this.updateGraph(); + return hasOneOrMoreJsAndNoTsFiles(this); }; - } - function allEditsBeforePos(edits, pos) { - for (var i = 0, len = edits.length; i < len; i++) { - if (ts.textSpanEnd(edits[i].span) >= pos) { - return false; + Project.prototype.getProjectErrors = function () { + return this.projectErrors; + }; + Project.prototype.getLanguageService = function (ensureSynchronized) { + if (ensureSynchronized === void 0) { ensureSynchronized = true; } + if (ensureSynchronized) { + this.updateGraph(); } - } - return true; - } - var CommandNames; - (function (CommandNames) { - CommandNames.Brace = "brace"; - CommandNames.Change = "change"; - CommandNames.Close = "close"; - CommandNames.Completions = "completions"; - CommandNames.CompletionDetails = "completionEntryDetails"; - CommandNames.Configure = "configure"; - CommandNames.Definition = "definition"; - CommandNames.Exit = "exit"; - CommandNames.Format = "format"; - CommandNames.Formatonkey = "formatonkey"; - CommandNames.Geterr = "geterr"; - CommandNames.GeterrForProject = "geterrForProject"; - CommandNames.Implementation = "implementation"; - CommandNames.SemanticDiagnosticsSync = "semanticDiagnosticsSync"; - CommandNames.SyntacticDiagnosticsSync = "syntacticDiagnosticsSync"; - CommandNames.NavBar = "navbar"; - CommandNames.Navto = "navto"; - CommandNames.Occurrences = "occurrences"; - CommandNames.DocumentHighlights = "documentHighlights"; - CommandNames.Open = "open"; - CommandNames.Quickinfo = "quickinfo"; - CommandNames.References = "references"; - CommandNames.Reload = "reload"; - CommandNames.Rename = "rename"; - CommandNames.Saveto = "saveto"; - CommandNames.SignatureHelp = "signatureHelp"; - CommandNames.TypeDefinition = "typeDefinition"; - CommandNames.ProjectInfo = "projectInfo"; - CommandNames.ReloadProjects = "reloadProjects"; - CommandNames.Unknown = "unknown"; - })(CommandNames = server.CommandNames || (server.CommandNames = {})); - var Errors; - (function (Errors) { - Errors.NoProject = new Error("No Project."); - Errors.ProjectLanguageServiceDisabled = new Error("The project's language service is disabled."); - })(Errors || (Errors = {})); - var Session = (function () { - function Session(host, byteLength, hrtime, logger) { - var _this = this; - this.host = host; - this.byteLength = byteLength; - this.hrtime = hrtime; - this.logger = logger; - this.changeSeq = 0; - this.handlers = ts.createMap((_a = {}, - _a[CommandNames.Exit] = function () { - _this.exit(); - return { responseRequired: false }; - }, - _a[CommandNames.Definition] = function (request) { - var defArgs = request.arguments; - return { response: _this.getDefinition(defArgs.line, defArgs.offset, defArgs.file), responseRequired: true }; - }, - _a[CommandNames.TypeDefinition] = function (request) { - var defArgs = request.arguments; - return { response: _this.getTypeDefinition(defArgs.line, defArgs.offset, defArgs.file), responseRequired: true }; - }, - _a[CommandNames.Implementation] = function (request) { - var implArgs = request.arguments; - return { response: _this.getImplementation(implArgs.line, implArgs.offset, implArgs.file), responseRequired: true }; - }, - _a[CommandNames.References] = function (request) { - var defArgs = request.arguments; - return { response: _this.getReferences(defArgs.line, defArgs.offset, defArgs.file), responseRequired: true }; - }, - _a[CommandNames.Rename] = function (request) { - var renameArgs = request.arguments; - return { response: _this.getRenameLocations(renameArgs.line, renameArgs.offset, renameArgs.file, renameArgs.findInComments, renameArgs.findInStrings), responseRequired: true }; - }, - _a[CommandNames.Open] = function (request) { - var openArgs = request.arguments; - var scriptKind; - switch (openArgs.scriptKindName) { - case "TS": - scriptKind = 3; - break; - case "JS": - scriptKind = 1; - break; - case "TSX": - scriptKind = 4; - break; - case "JSX": - scriptKind = 2; - break; + return this.languageService; + }; + Project.prototype.getCompileOnSaveAffectedFileList = function (scriptInfo) { + if (!this.languageServiceEnabled) { + return []; + } + this.updateGraph(); + return this.builder.getFilesAffectedBy(scriptInfo); + }; + Project.prototype.getProjectVersion = function () { + return this.projectStateVersion.toString(); + }; + Project.prototype.enableLanguageService = function () { + var lsHost = new server.LSHost(this.projectService.host, this, this.projectService.cancellationToken); + lsHost.setCompilationSettings(this.compilerOptions); + this.languageService = ts.createLanguageService(lsHost, this.documentRegistry); + this.lsHost = lsHost; + this.languageServiceEnabled = true; + }; + Project.prototype.disableLanguageService = function () { + this.languageService = server.nullLanguageService; + this.lsHost = server.nullLanguageServiceHost; + this.languageServiceEnabled = false; + }; + Project.prototype.getSourceFile = function (path) { + if (!this.program) { + return undefined; + } + return this.program.getSourceFileByPath(path); + }; + Project.prototype.updateTypes = function () { + this.typesVersion++; + this.markAsDirty(); + this.updateGraph(); + }; + Project.prototype.close = function () { + if (this.program) { + for (var _i = 0, _a = this.program.getSourceFiles(); _i < _a.length; _i++) { + var f = _a[_i]; + var info = this.projectService.getScriptInfo(f.fileName); + info.detachFromProject(this); + } + } + else { + for (var _b = 0, _c = this.rootFiles; _b < _c.length; _b++) { + var root = _c[_b]; + root.detachFromProject(this); + } + } + this.rootFiles = undefined; + this.rootFilesMap = undefined; + this.program = undefined; + this.languageService.dispose(); + }; + Project.prototype.getCompilerOptions = function () { + return this.compilerOptions; + }; + Project.prototype.hasRoots = function () { + return this.rootFiles && this.rootFiles.length > 0; + }; + Project.prototype.getRootFiles = function () { + return this.rootFiles && this.rootFiles.map(function (info) { return info.fileName; }); + }; + Project.prototype.getRootFilesLSHost = function () { + var result = []; + if (this.rootFiles) { + for (var _i = 0, _a = this.rootFiles; _i < _a.length; _i++) { + var f = _a[_i]; + result.push(f.fileName); + } + if (this.typingFiles) { + for (var _b = 0, _c = this.typingFiles; _b < _c.length; _b++) { + var f = _c[_b]; + result.push(f); } - _this.openClientFile(openArgs.file, openArgs.fileContent, scriptKind); - return { responseRequired: false }; - }, - _a[CommandNames.Quickinfo] = function (request) { - var quickinfoArgs = request.arguments; - return { response: _this.getQuickInfo(quickinfoArgs.line, quickinfoArgs.offset, quickinfoArgs.file), responseRequired: true }; - }, - _a[CommandNames.Format] = function (request) { - var formatArgs = request.arguments; - return { response: _this.getFormattingEditsForRange(formatArgs.line, formatArgs.offset, formatArgs.endLine, formatArgs.endOffset, formatArgs.file), responseRequired: true }; - }, - _a[CommandNames.Formatonkey] = function (request) { - var formatOnKeyArgs = request.arguments; - return { response: _this.getFormattingEditsAfterKeystroke(formatOnKeyArgs.line, formatOnKeyArgs.offset, formatOnKeyArgs.key, formatOnKeyArgs.file), responseRequired: true }; - }, - _a[CommandNames.Completions] = function (request) { - var completionsArgs = request.arguments; - return { response: _this.getCompletions(completionsArgs.line, completionsArgs.offset, completionsArgs.prefix, completionsArgs.file), responseRequired: true }; - }, - _a[CommandNames.CompletionDetails] = function (request) { - var completionDetailsArgs = request.arguments; - return { - response: _this.getCompletionEntryDetails(completionDetailsArgs.line, completionDetailsArgs.offset, completionDetailsArgs.entryNames, completionDetailsArgs.file), responseRequired: true - }; - }, - _a[CommandNames.SignatureHelp] = function (request) { - var signatureHelpArgs = request.arguments; - return { response: _this.getSignatureHelpItems(signatureHelpArgs.line, signatureHelpArgs.offset, signatureHelpArgs.file), responseRequired: true }; - }, - _a[CommandNames.SemanticDiagnosticsSync] = function (request) { - return _this.requiredResponse(_this.getSemanticDiagnosticsSync(request.arguments)); - }, - _a[CommandNames.SyntacticDiagnosticsSync] = function (request) { - return _this.requiredResponse(_this.getSyntacticDiagnosticsSync(request.arguments)); - }, - _a[CommandNames.Geterr] = function (request) { - var geterrArgs = request.arguments; - return { response: _this.getDiagnostics(geterrArgs.delay, geterrArgs.files), responseRequired: false }; - }, - _a[CommandNames.GeterrForProject] = function (request) { - var _a = request.arguments, file = _a.file, delay = _a.delay; - return { response: _this.getDiagnosticsForProject(delay, file), responseRequired: false }; - }, - _a[CommandNames.Change] = function (request) { - var changeArgs = request.arguments; - _this.change(changeArgs.line, changeArgs.offset, changeArgs.endLine, changeArgs.endOffset, changeArgs.insertString, changeArgs.file); - return { responseRequired: false }; - }, - _a[CommandNames.Configure] = function (request) { - var configureArgs = request.arguments; - _this.projectService.setHostConfiguration(configureArgs); - _this.output(undefined, CommandNames.Configure, request.seq); - return { responseRequired: false }; - }, - _a[CommandNames.Reload] = function (request) { - var reloadArgs = request.arguments; - _this.reload(reloadArgs.file, reloadArgs.tmpfile, request.seq); - return { response: { reloadFinished: true }, responseRequired: true }; - }, - _a[CommandNames.Saveto] = function (request) { - var savetoArgs = request.arguments; - _this.saveToTmp(savetoArgs.file, savetoArgs.tmpfile); - return { responseRequired: false }; - }, - _a[CommandNames.Close] = function (request) { - var closeArgs = request.arguments; - _this.closeClientFile(closeArgs.file); - return { responseRequired: false }; - }, - _a[CommandNames.Navto] = function (request) { - var navtoArgs = request.arguments; - return { response: _this.getNavigateToItems(navtoArgs.searchValue, navtoArgs.file, navtoArgs.maxResultCount, navtoArgs.currentFileOnly), responseRequired: true }; - }, - _a[CommandNames.Brace] = function (request) { - var braceArguments = request.arguments; - return { response: _this.getBraceMatching(braceArguments.line, braceArguments.offset, braceArguments.file), responseRequired: true }; - }, - _a[CommandNames.NavBar] = function (request) { - var navBarArgs = request.arguments; - return { response: _this.getNavigationBarItems(navBarArgs.file), responseRequired: true }; - }, - _a[CommandNames.Occurrences] = function (request) { - var _a = request.arguments, line = _a.line, offset = _a.offset, fileName = _a.file; - return { response: _this.getOccurrences(line, offset, fileName), responseRequired: true }; - }, - _a[CommandNames.DocumentHighlights] = function (request) { - var _a = request.arguments, line = _a.line, offset = _a.offset, fileName = _a.file, filesToSearch = _a.filesToSearch; - return { response: _this.getDocumentHighlights(line, offset, fileName, filesToSearch), responseRequired: true }; - }, - _a[CommandNames.ProjectInfo] = function (request) { - var _a = request.arguments, file = _a.file, needFileNameList = _a.needFileNameList; - return { response: _this.getProjectInfo(file, needFileNameList), responseRequired: true }; - }, - _a[CommandNames.ReloadProjects] = function (request) { - _this.reloadProjects(); - return { responseRequired: false }; - }, - _a)); - this.projectService = - new server.ProjectService(host, logger, function (event) { - _this.handleEvent(event); - }); - var _a; - } - Session.prototype.handleEvent = function (event) { + } + } + return result; + }; + Project.prototype.getRootScriptInfos = function () { + return this.rootFiles; + }; + Project.prototype.getScriptInfos = function () { var _this = this; - switch (event.eventName) { - case "context": - var _a = event.data, project = _a.project, fileName = _a.fileName; - this.projectService.log("got context event, updating diagnostics for" + fileName, "Info"); - this.updateErrorCheck([{ fileName: fileName, project: project }], this.changeSeq, function (n) { return n === _this.changeSeq; }, 100); - break; - case "configFileDiag": - var _b = event.data, triggerFile = _b.triggerFile, configFileName = _b.configFileName, diagnostics = _b.diagnostics; - this.configFileDiagnosticEvent(triggerFile, configFileName, diagnostics); + return ts.map(this.program.getSourceFiles(), function (sourceFile) { + var scriptInfo = _this.projectService.getScriptInfoForPath(sourceFile.path); + if (!scriptInfo) { + ts.Debug.assert(false, "scriptInfo for a file '" + sourceFile.fileName + "' is missing."); + } + return scriptInfo; + }); + }; + Project.prototype.getFileEmitOutput = function (info, emitOnlyDtsFiles) { + if (!this.languageServiceEnabled) { + return undefined; } + return this.getLanguageService().getEmitOutput(info.fileName, emitOnlyDtsFiles); }; - Session.prototype.logError = function (err, cmd) { - var typedErr = err; - var msg = "Exception on executing command " + cmd; - if (typedErr.message) { - msg += ":\n" + typedErr.message; - if (typedErr.stack) { - msg += "\n" + typedErr.stack; + Project.prototype.getFileNames = function () { + if (!this.program) { + return []; + } + if (!this.languageServiceEnabled) { + var rootFiles = this.getRootFiles(); + if (this.compilerOptions) { + var defaultLibrary = ts.getDefaultLibFilePath(this.compilerOptions); + if (defaultLibrary) { + (rootFiles || (rootFiles = [])).push(server.asNormalizedPath(defaultLibrary)); + } } + return rootFiles; } - this.projectService.log(msg); + var sourceFiles = this.program.getSourceFiles(); + return sourceFiles.map(function (sourceFile) { return server.asNormalizedPath(sourceFile.fileName); }); }; - Session.prototype.sendLineToClient = function (line) { - this.host.write(line + this.host.newLine); + Project.prototype.getAllEmittableFiles = function () { + if (!this.languageServiceEnabled) { + return []; + } + var defaultLibraryFileName = ts.getDefaultLibFileName(this.compilerOptions); + var infos = this.getScriptInfos(); + var result = []; + for (var _i = 0, infos_2 = infos; _i < infos_2.length; _i++) { + var info = infos_2[_i]; + if (ts.getBaseFileName(info.fileName) !== defaultLibraryFileName && server.shouldEmitFile(info)) { + result.push(info.fileName); + } + } + return result; }; - Session.prototype.send = function (msg) { - var json = JSON.stringify(msg); - if (this.logger.isVerbose()) { - this.logger.info(msg.type + ": " + json); + Project.prototype.containsScriptInfo = function (info) { + return this.isRoot(info) || (this.program && this.program.getSourceFileByPath(info.path) !== undefined); + }; + Project.prototype.containsFile = function (filename, requireOpen) { + var info = this.projectService.getScriptInfoForNormalizedPath(filename); + if (info && (info.isOpen || !requireOpen)) { + return this.containsScriptInfo(info); } - this.sendLineToClient("Content-Length: " + (1 + this.byteLength(json, "utf8")) + - "\r\n\r\n" + json); }; - Session.prototype.configFileDiagnosticEvent = function (triggerFile, configFile, diagnostics) { - var bakedDiags = ts.map(diagnostics, formatConfigFileDiag); - var ev = { - seq: 0, - type: "event", - event: "configFileDiag", - body: { - triggerFile: triggerFile, - configFile: configFile, - diagnostics: bakedDiags - } - }; - this.send(ev); + Project.prototype.isRoot = function (info) { + return this.rootFilesMap && this.rootFilesMap.contains(info.path); }; - Session.prototype.event = function (info, eventName) { - var ev = { - seq: 0, - type: "event", - event: eventName, - body: info - }; - this.send(ev); + Project.prototype.addRoot = function (info) { + if (!this.isRoot(info)) { + this.rootFiles.push(info); + this.rootFilesMap.set(info.path, info); + info.attachToProject(this); + this.markAsDirty(); + } }; - Session.prototype.response = function (info, cmdName, reqSeq, errorMsg) { - if (reqSeq === void 0) { reqSeq = 0; } - var res = { - seq: 0, - type: "response", - command: cmdName, - request_seq: reqSeq, - success: !errorMsg - }; - if (!errorMsg) { - res.body = info; + Project.prototype.removeFile = function (info, detachFromProject) { + if (detachFromProject === void 0) { detachFromProject = true; } + this.removeRootFileIfNecessary(info); + this.lsHost.notifyFileRemoved(info); + if (detachFromProject) { + info.detachFromProject(this); + } + this.markAsDirty(); + }; + Project.prototype.markAsDirty = function () { + this.projectStateVersion++; + }; + Project.prototype.updateGraph = function () { + if (!this.languageServiceEnabled) { + return true; } - else { - res.message = errorMsg; + var hasChanges = this.updateGraphWorker(); + var cachedTypings = this.projectService.typingsCache.getTypingsForProject(this, hasChanges); + if (this.setTypings(cachedTypings)) { + hasChanges = this.updateGraphWorker() || hasChanges; } - this.send(res); + if (hasChanges) { + this.projectStructureVersion++; + } + return !hasChanges; }; - Session.prototype.output = function (body, commandName, requestSequence, errorMessage) { - if (requestSequence === void 0) { requestSequence = 0; } - this.response(body, commandName, requestSequence, errorMessage); + Project.prototype.setTypings = function (typings) { + if (ts.arrayIsEqualTo(this.typingFiles, typings)) { + return false; + } + this.typingFiles = typings; + this.markAsDirty(); + return true; }; - Session.prototype.semanticCheck = function (file, project) { - try { - var diags = project.compilerService.languageService.getSemanticDiagnostics(file); - if (diags) { - var bakedDiags = diags.map(function (diag) { return formatDiag(file, project, diag); }); - this.event({ file: file, diagnostics: bakedDiags }, "semanticDiag"); + Project.prototype.updateGraphWorker = function () { + var oldProgram = this.program; + this.program = this.languageService.getProgram(); + var hasChanges = false; + if (!oldProgram || (this.program !== oldProgram && !oldProgram.structureIsReused)) { + hasChanges = true; + if (oldProgram) { + for (var _i = 0, _a = oldProgram.getSourceFiles(); _i < _a.length; _i++) { + var f = _a[_i]; + if (this.program.getSourceFileByPath(f.path)) { + continue; + } + var scriptInfoToDetach = this.projectService.getScriptInfo(f.fileName); + if (scriptInfoToDetach) { + scriptInfoToDetach.detachFromProject(this); + } + } } } - catch (err) { - this.logError(err, "semantic check"); + this.builder.onProjectUpdateGraph(); + return hasChanges; + }; + Project.prototype.getScriptInfoLSHost = function (fileName) { + var scriptInfo = this.projectService.getOrCreateScriptInfo(fileName, false); + if (scriptInfo) { + scriptInfo.attachToProject(this); } + return scriptInfo; }; - Session.prototype.syntacticCheck = function (file, project) { - try { - var diags = project.compilerService.languageService.getSyntacticDiagnostics(file); - if (diags) { - var bakedDiags = diags.map(function (diag) { return formatDiag(file, project, diag); }); - this.event({ file: file, diagnostics: bakedDiags }, "syntaxDiag"); - } + Project.prototype.getScriptInfoForNormalizedPath = function (fileName) { + var scriptInfo = this.projectService.getOrCreateScriptInfoForNormalizedPath(fileName, false); + if (scriptInfo && !scriptInfo.isAttached(this)) { + return server.Errors.ThrowProjectDoesNotContainDocument(fileName, this); } - catch (err) { - this.logError(err, "syntactic check"); + return scriptInfo; + }; + Project.prototype.getScriptInfo = function (uncheckedFileName) { + return this.getScriptInfoForNormalizedPath(server.toNormalizedPath(uncheckedFileName)); + }; + Project.prototype.filesToString = function () { + if (!this.program) { + return ""; } + var strBuilder = ""; + for (var _i = 0, _a = this.program.getSourceFiles(); _i < _a.length; _i++) { + var file = _a[_i]; + strBuilder += file.fileName + "\n"; + } + return strBuilder; }; - Session.prototype.reloadProjects = function () { - this.projectService.reloadProjects(); + Project.prototype.setCompilerOptions = function (compilerOptions) { + if (compilerOptions) { + if (this.projectKind === ProjectKind.Inferred) { + compilerOptions.allowJs = true; + } + compilerOptions.allowNonTsExtensions = true; + this.compilerOptions = compilerOptions; + this.lsHost.setCompilationSettings(compilerOptions); + this.markAsDirty(); + } }; - Session.prototype.updateProjectStructure = function (seq, matchSeq, ms) { - var _this = this; - if (ms === void 0) { ms = 1500; } - setTimeout(function () { - if (matchSeq(seq)) { - _this.projectService.updateProjectStructure(); + Project.prototype.reloadScript = function (filename) { + var script = this.projectService.getScriptInfoForNormalizedPath(filename); + if (script) { + ts.Debug.assert(script.isAttached(this)); + script.reloadFromFile(); + return true; + } + return false; + }; + Project.prototype.getChangesSinceVersion = function (lastKnownVersion) { + this.updateGraph(); + var info = { + projectName: this.getProjectName(), + version: this.projectStructureVersion, + isInferred: this.projectKind === ProjectKind.Inferred, + options: this.getCompilerOptions() + }; + if (this.lastReportedFileNames && lastKnownVersion === this.lastReportedVersion) { + if (this.projectStructureVersion == this.lastReportedVersion) { + return { info: info, projectErrors: this.projectErrors }; } - }, ms); + var lastReportedFileNames = this.lastReportedFileNames; + var currentFiles = ts.arrayToMap(this.getFileNames(), function (x) { return x; }); + var added = []; + var removed = []; + for (var id in currentFiles) { + if (!ts.hasProperty(lastReportedFileNames, id)) { + added.push(id); + } + } + for (var id in lastReportedFileNames) { + if (!ts.hasProperty(currentFiles, id)) { + removed.push(id); + } + } + this.lastReportedFileNames = currentFiles; + this.lastReportedVersion = this.projectStructureVersion; + return { info: info, changes: { added: added, removed: removed }, projectErrors: this.projectErrors }; + } + else { + var projectFileNames = this.getFileNames(); + this.lastReportedFileNames = ts.arrayToMap(projectFileNames, function (x) { return x; }); + this.lastReportedVersion = this.projectStructureVersion; + return { info: info, files: projectFileNames, projectErrors: this.projectErrors }; + } }; - Session.prototype.updateErrorCheck = function (checkList, seq, matchSeq, ms, followMs, requireOpen) { + Project.prototype.getReferencedFiles = function (path) { var _this = this; - if (ms === void 0) { ms = 1500; } - if (followMs === void 0) { followMs = 200; } - if (requireOpen === void 0) { requireOpen = true; } - if (followMs > ms) { - followMs = ms; + if (!this.languageServiceEnabled) { + return []; } - if (this.errorTimer) { - clearTimeout(this.errorTimer); + var sourceFile = this.getSourceFile(path); + if (!sourceFile) { + return []; } - if (this.immediateId) { - clearImmediate(this.immediateId); - this.immediateId = undefined; + var referencedFiles = ts.createMap(); + if (sourceFile.imports && sourceFile.imports.length > 0) { + var checker = this.program.getTypeChecker(); + for (var _i = 0, _a = sourceFile.imports; _i < _a.length; _i++) { + var importName = _a[_i]; + var symbol = checker.getSymbolAtLocation(importName); + if (symbol && symbol.declarations && symbol.declarations[0]) { + var declarationSourceFile = symbol.declarations[0].getSourceFile(); + if (declarationSourceFile) { + referencedFiles[declarationSourceFile.path] = true; + } + } + } } - var index = 0; - var checkOne = function () { - if (matchSeq(seq)) { - var checkSpec_1 = checkList[index]; - index++; - if (checkSpec_1.project.getSourceFileFromName(checkSpec_1.fileName, requireOpen)) { - _this.syntacticCheck(checkSpec_1.fileName, checkSpec_1.project); - _this.immediateId = setImmediate(function () { - _this.semanticCheck(checkSpec_1.fileName, checkSpec_1.project); - _this.immediateId = undefined; - if (checkList.length > index) { - _this.errorTimer = setTimeout(checkOne, followMs); - } - else { - _this.errorTimer = undefined; - } - }); + var currentDirectory = ts.getDirectoryPath(path); + var getCanonicalFileName = ts.createGetCanonicalFileName(this.projectService.host.useCaseSensitiveFileNames); + if (sourceFile.referencedFiles && sourceFile.referencedFiles.length > 0) { + for (var _b = 0, _c = sourceFile.referencedFiles; _b < _c.length; _b++) { + var referencedFile = _c[_b]; + var referencedPath = ts.toPath(referencedFile.fileName, currentDirectory, getCanonicalFileName); + referencedFiles[referencedPath] = true; + } + } + if (sourceFile.resolvedTypeReferenceDirectiveNames) { + for (var typeName in sourceFile.resolvedTypeReferenceDirectiveNames) { + var resolvedTypeReferenceDirective = sourceFile.resolvedTypeReferenceDirectiveNames[typeName]; + if (!resolvedTypeReferenceDirective) { + continue; } + var fileName = resolvedTypeReferenceDirective.resolvedFileName; + var typeFilePath = ts.toPath(fileName, currentDirectory, getCanonicalFileName); + referencedFiles[typeFilePath] = true; } - }; - if ((checkList.length > index) && (matchSeq(seq))) { - this.errorTimer = setTimeout(checkOne, ms); } + var allFileNames = ts.map(Object.keys(referencedFiles), function (key) { return key; }); + return ts.filter(allFileNames, function (file) { return _this.projectService.host.fileExists(file); }); }; - Session.prototype.getDefinition = function (line, offset, fileName) { - var file = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(file); - if (!project || project.languageServiceDiabled) { - throw Errors.NoProject; + Project.prototype.removeRootFileIfNecessary = function (info) { + if (this.isRoot(info)) { + remove(this.rootFiles, info); + this.rootFilesMap.remove(info.path); } - var compilerService = project.compilerService; - var position = compilerService.host.lineOffsetToPosition(file, line, offset); - var definitions = compilerService.languageService.getDefinitionAtPosition(file, position); - if (!definitions) { + }; + return Project; + }()); + server.Project = Project; + var InferredProject = (function (_super) { + __extends(InferredProject, _super); + function InferredProject(projectService, documentRegistry, languageServiceEnabled, compilerOptions, compileOnSaveEnabled) { + var _this = _super.call(this, ProjectKind.Inferred, projectService, documentRegistry, undefined, languageServiceEnabled, compilerOptions, compileOnSaveEnabled) || this; + _this.compileOnSaveEnabled = compileOnSaveEnabled; + _this.directoriesWatchedForTsconfig = []; + _this.inferredProjectName = server.makeInferredProjectName(InferredProject.NextId); + InferredProject.NextId++; + return _this; + } + InferredProject.prototype.getProjectName = function () { + return this.inferredProjectName; + }; + InferredProject.prototype.getProjectRootPath = function () { + if (this.projectService.useSingleInferredProject) { return undefined; } - return definitions.map(function (def) { return ({ - file: def.fileName, - start: compilerService.host.positionToLineOffset(def.fileName, def.textSpan.start), - end: compilerService.host.positionToLineOffset(def.fileName, ts.textSpanEnd(def.textSpan)) - }); }); + var rootFiles = this.getRootFiles(); + return ts.getDirectoryPath(rootFiles[0]); }; - Session.prototype.getTypeDefinition = function (line, offset, fileName) { - var file = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(file); - if (!project || project.languageServiceDiabled) { - throw Errors.NoProject; + InferredProject.prototype.close = function () { + _super.prototype.close.call(this); + for (var _i = 0, _a = this.directoriesWatchedForTsconfig; _i < _a.length; _i++) { + var directory = _a[_i]; + this.projectService.stopWatchingDirectory(directory); } - var compilerService = project.compilerService; - var position = compilerService.host.lineOffsetToPosition(file, line, offset); - var definitions = compilerService.languageService.getTypeDefinitionAtPosition(file, position); - if (!definitions) { - return undefined; + }; + InferredProject.prototype.getTypingOptions = function () { + return { + enableAutoDiscovery: allRootFilesAreJsOrDts(this), + include: [], + exclude: [] + }; + }; + return InferredProject; + }(Project)); + InferredProject.NextId = 1; + server.InferredProject = InferredProject; + var ConfiguredProject = (function (_super) { + __extends(ConfiguredProject, _super); + function ConfiguredProject(configFileName, projectService, documentRegistry, hasExplicitListOfFiles, compilerOptions, wildcardDirectories, languageServiceEnabled, compileOnSaveEnabled) { + var _this = _super.call(this, ProjectKind.Configured, projectService, documentRegistry, hasExplicitListOfFiles, languageServiceEnabled, compilerOptions, compileOnSaveEnabled) || this; + _this.configFileName = configFileName; + _this.wildcardDirectories = wildcardDirectories; + _this.compileOnSaveEnabled = compileOnSaveEnabled; + _this.openRefCount = 0; + return _this; + } + ConfiguredProject.prototype.getProjectRootPath = function () { + return ts.getDirectoryPath(this.configFileName); + }; + ConfiguredProject.prototype.setProjectErrors = function (projectErrors) { + this.projectErrors = projectErrors; + }; + ConfiguredProject.prototype.setTypingOptions = function (newTypingOptions) { + this.typingOptions = newTypingOptions; + }; + ConfiguredProject.prototype.getTypingOptions = function () { + return this.typingOptions; + }; + ConfiguredProject.prototype.getProjectName = function () { + return this.configFileName; + }; + ConfiguredProject.prototype.watchConfigFile = function (callback) { + var _this = this; + this.projectFileWatcher = this.projectService.host.watchFile(this.configFileName, function (_) { return callback(_this); }); + }; + ConfiguredProject.prototype.watchTypeRoots = function (callback) { + var _this = this; + var roots = this.getEffectiveTypeRoots(); + var watchers = []; + for (var _i = 0, roots_1 = roots; _i < roots_1.length; _i++) { + var root = roots_1[_i]; + this.projectService.logger.info("Add type root watcher for: " + root); + watchers.push(this.projectService.host.watchDirectory(root, function (path) { return callback(_this, path); }, false)); + } + this.typeRootsWatchers = watchers; + }; + ConfiguredProject.prototype.watchConfigDirectory = function (callback) { + var _this = this; + if (this.directoryWatcher) { + return; } - return definitions.map(function (def) { return ({ - file: def.fileName, - start: compilerService.host.positionToLineOffset(def.fileName, def.textSpan.start), - end: compilerService.host.positionToLineOffset(def.fileName, ts.textSpanEnd(def.textSpan)) - }); }); + var directoryToWatch = ts.getDirectoryPath(this.configFileName); + this.projectService.logger.info("Add recursive watcher for: " + directoryToWatch); + this.directoryWatcher = this.projectService.host.watchDirectory(directoryToWatch, function (path) { return callback(_this, path); }, true); }; - Session.prototype.getImplementation = function (line, offset, fileName) { - var file = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(file); - if (!project || project.languageServiceDiabled) { - throw Errors.NoProject; + ConfiguredProject.prototype.watchWildcards = function (callback) { + var _this = this; + if (!this.wildcardDirectories) { + return; } - var compilerService = project.compilerService; - var implementations = compilerService.languageService.getImplementationAtPosition(file, compilerService.host.lineOffsetToPosition(file, line, offset)); - if (!implementations) { - return undefined; + var configDirectoryPath = ts.getDirectoryPath(this.configFileName); + this.directoriesWatchedForWildcards = ts.reduceProperties(this.wildcardDirectories, function (watchers, flag, directory) { + if (ts.comparePaths(configDirectoryPath, directory, ".", !_this.projectService.host.useCaseSensitiveFileNames) !== 0) { + var recursive = (flag & 1) !== 0; + _this.projectService.logger.info("Add " + (recursive ? "recursive " : "") + "watcher for: " + directory); + watchers[directory] = _this.projectService.host.watchDirectory(directory, function (path) { return callback(_this, path); }, recursive); + } + return watchers; + }, {}); + }; + ConfiguredProject.prototype.stopWatchingDirectory = function () { + if (this.directoryWatcher) { + this.directoryWatcher.close(); + this.directoryWatcher = undefined; } - return implementations.map(function (impl) { return ({ - file: impl.fileName, - start: compilerService.host.positionToLineOffset(impl.fileName, impl.textSpan.start), - end: compilerService.host.positionToLineOffset(impl.fileName, ts.textSpanEnd(impl.textSpan)) - }); }); }; - Session.prototype.getOccurrences = function (line, offset, fileName) { - fileName = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(fileName); - if (!project || project.languageServiceDiabled) { - throw Errors.NoProject; - } - var compilerService = project.compilerService; - var position = compilerService.host.lineOffsetToPosition(fileName, line, offset); - var occurrences = compilerService.languageService.getOccurrencesAtPosition(fileName, position); - if (!occurrences) { - return undefined; - } - return occurrences.map(function (occurrence) { - var fileName = occurrence.fileName, isWriteAccess = occurrence.isWriteAccess, textSpan = occurrence.textSpan; - var start = compilerService.host.positionToLineOffset(fileName, textSpan.start); - var end = compilerService.host.positionToLineOffset(fileName, ts.textSpanEnd(textSpan)); - return { - start: start, - end: end, - file: fileName, - isWriteAccess: isWriteAccess - }; - }); + ConfiguredProject.prototype.close = function () { + _super.prototype.close.call(this); + if (this.projectFileWatcher) { + this.projectFileWatcher.close(); + } + if (this.typeRootsWatchers) { + for (var _i = 0, _a = this.typeRootsWatchers; _i < _a.length; _i++) { + var watcher = _a[_i]; + watcher.close(); + } + this.typeRootsWatchers = undefined; + } + for (var id in this.directoriesWatchedForWildcards) { + this.directoriesWatchedForWildcards[id].close(); + } + this.directoriesWatchedForWildcards = undefined; + this.stopWatchingDirectory(); + }; + ConfiguredProject.prototype.addOpenRef = function () { + this.openRefCount++; + }; + ConfiguredProject.prototype.deleteOpenRef = function () { + this.openRefCount--; + return this.openRefCount; + }; + ConfiguredProject.prototype.getEffectiveTypeRoots = function () { + return ts.getEffectiveTypeRoots(this.getCompilerOptions(), this.projectService.host) || []; + }; + return ConfiguredProject; + }(Project)); + server.ConfiguredProject = ConfiguredProject; + var ExternalProject = (function (_super) { + __extends(ExternalProject, _super); + function ExternalProject(externalProjectName, projectService, documentRegistry, compilerOptions, languageServiceEnabled, compileOnSaveEnabled, projectFilePath) { + var _this = _super.call(this, ProjectKind.External, projectService, documentRegistry, true, languageServiceEnabled, compilerOptions, compileOnSaveEnabled) || this; + _this.externalProjectName = externalProjectName; + _this.compileOnSaveEnabled = compileOnSaveEnabled; + _this.projectFilePath = projectFilePath; + return _this; + } + ExternalProject.prototype.getProjectRootPath = function () { + if (this.projectFilePath) { + return ts.getDirectoryPath(this.projectFilePath); + } + return ts.getDirectoryPath(ts.normalizeSlashes(this.externalProjectName)); + }; + ExternalProject.prototype.getTypingOptions = function () { + return this.typingOptions; + }; + ExternalProject.prototype.setProjectErrors = function (projectErrors) { + this.projectErrors = projectErrors; + }; + ExternalProject.prototype.setTypingOptions = function (newTypingOptions) { + if (!newTypingOptions) { + newTypingOptions = { + enableAutoDiscovery: allRootFilesAreJsOrDts(this), + include: [], + exclude: [] + }; + } + else { + if (newTypingOptions.enableAutoDiscovery === undefined) { + newTypingOptions.enableAutoDiscovery = allRootFilesAreJsOrDts(this); + } + if (!newTypingOptions.include) { + newTypingOptions.include = []; + } + if (!newTypingOptions.exclude) { + newTypingOptions.exclude = []; + } + } + this.typingOptions = newTypingOptions; + }; + ExternalProject.prototype.getProjectName = function () { + return this.externalProjectName; + }; + return ExternalProject; + }(Project)); + server.ExternalProject = ExternalProject; + })(server = ts.server || (ts.server = {})); +})(ts || (ts = {})); +var ts; +(function (ts) { + var server; + (function (server) { + server.maxProgramSizeForNonTsFiles = 20 * 1024 * 1024; + function combineProjectOutput(projects, action, comparer, areEqual) { + var result = projects.reduce(function (previous, current) { return ts.concatenate(previous, action(current)); }, []).sort(comparer); + return projects.length > 1 ? ts.deduplicate(result, areEqual) : result; + } + server.combineProjectOutput = combineProjectOutput; + var fileNamePropertyReader = { + getFileName: function (x) { return x; }, + getScriptKind: function (_) { return undefined; }, + hasMixedContent: function (_) { return false; } + }; + var externalFilePropertyReader = { + getFileName: function (x) { return x.fileName; }, + getScriptKind: function (x) { return x.scriptKind; }, + hasMixedContent: function (x) { return x.hasMixedContent; } + }; + function findProjectByName(projectName, projects) { + for (var _i = 0, projects_1 = projects; _i < projects_1.length; _i++) { + var proj = projects_1[_i]; + if (proj.getProjectName() === projectName) { + return proj; + } + } + } + function createFileNotFoundDiagnostic(fileName) { + return ts.createCompilerDiagnostic(ts.Diagnostics.File_0_not_found, fileName); + } + function isRootFileInInferredProject(info) { + if (info.containingProjects.length === 0) { + return false; + } + return info.containingProjects[0].projectKind === server.ProjectKind.Inferred && info.containingProjects[0].isRoot(info); + } + var DirectoryWatchers = (function () { + function DirectoryWatchers(projectService) { + this.projectService = projectService; + this.directoryWatchersForTsconfig = ts.createMap(); + this.directoryWatchersRefCount = ts.createMap(); + } + DirectoryWatchers.prototype.stopWatchingDirectory = function (directory) { + this.directoryWatchersRefCount[directory]--; + if (this.directoryWatchersRefCount[directory] === 0) { + this.projectService.logger.info("Close directory watcher for: " + directory); + this.directoryWatchersForTsconfig[directory].close(); + delete this.directoryWatchersForTsconfig[directory]; + } + }; + DirectoryWatchers.prototype.startWatchingContainingDirectoriesForFile = function (fileName, project, callback) { + var currentPath = ts.getDirectoryPath(fileName); + var parentPath = ts.getDirectoryPath(currentPath); + while (currentPath != parentPath) { + if (!this.directoryWatchersForTsconfig[currentPath]) { + this.projectService.logger.info("Add watcher for: " + currentPath); + this.directoryWatchersForTsconfig[currentPath] = this.projectService.host.watchDirectory(currentPath, callback); + this.directoryWatchersRefCount[currentPath] = 1; + } + else { + this.directoryWatchersRefCount[currentPath] += 1; + } + project.directoriesWatchedForTsconfig.push(currentPath); + currentPath = parentPath; + parentPath = ts.getDirectoryPath(parentPath); + } + }; + return DirectoryWatchers; + }()); + var ProjectService = (function () { + function ProjectService(host, logger, cancellationToken, useSingleInferredProject, typingsInstaller, eventHandler) { + if (typingsInstaller === void 0) { typingsInstaller = server.nullTypingsInstaller; } + this.host = host; + this.logger = logger; + this.cancellationToken = cancellationToken; + this.useSingleInferredProject = useSingleInferredProject; + this.typingsInstaller = typingsInstaller; + this.eventHandler = eventHandler; + this.filenameToScriptInfo = ts.createFileMap(); + this.externalProjectToConfiguredProjectMap = ts.createMap(); + this.externalProjects = []; + this.inferredProjects = []; + this.configuredProjects = []; + this.openFiles = []; + this.toCanonicalFileName = ts.createGetCanonicalFileName(host.useCaseSensitiveFileNames); + this.directoryWatchers = new DirectoryWatchers(this); + this.throttledOperations = new server.ThrottledOperations(host); + this.typingsInstaller.attach(this); + this.typingsCache = new server.TypingsCache(this.typingsInstaller); + this.hostConfiguration = { + formatCodeOptions: server.getDefaultFormatCodeSettings(this.host), + hostInfo: "Unknown host" + }; + this.documentRegistry = ts.createDocumentRegistry(host.useCaseSensitiveFileNames, host.getCurrentDirectory()); + } + ProjectService.prototype.getChangedFiles_TestOnly = function () { + return this.changedFiles; + }; + ProjectService.prototype.ensureInferredProjectsUpToDate_TestOnly = function () { + this.ensureInferredProjectsUpToDate(); }; - Session.prototype.getDiagnosticsWorker = function (args, selector) { - var file = ts.normalizePath(args.file); - var project = this.projectService.getProjectForFile(file); + ProjectService.prototype.updateTypingsForProject = function (response) { + var project = this.findProject(response.projectName); if (!project) { - throw Errors.NoProject; + return; } - if (project.languageServiceDiabled) { - throw Errors.ProjectLanguageServiceDisabled; + switch (response.kind) { + case "set": + this.typingsCache.updateTypingsForProject(response.projectName, response.compilerOptions, response.typingOptions, response.typings); + project.updateGraph(); + break; + case "invalidate": + this.typingsCache.invalidateCachedTypingsForProject(project); + break; } - var diagnostics = selector(project, file); - return ts.map(diagnostics, function (originalDiagnostic) { return formatDiag(file, project, originalDiagnostic); }); }; - Session.prototype.getSyntacticDiagnosticsSync = function (args) { - return this.getDiagnosticsWorker(args, function (project, file) { return project.compilerService.languageService.getSyntacticDiagnostics(file); }); + ProjectService.prototype.setCompilerOptionsForInferredProjects = function (projectCompilerOptions) { + this.compilerOptionsForInferredProjects = projectCompilerOptions; + this.compileOnSaveForInferredProjects = projectCompilerOptions.compileOnSave; + for (var _i = 0, _a = this.inferredProjects; _i < _a.length; _i++) { + var proj = _a[_i]; + proj.setCompilerOptions(projectCompilerOptions); + proj.compileOnSaveEnabled = projectCompilerOptions.compileOnSave; + } + this.updateProjectGraphs(this.inferredProjects); }; - Session.prototype.getSemanticDiagnosticsSync = function (args) { - return this.getDiagnosticsWorker(args, function (project, file) { return project.compilerService.languageService.getSemanticDiagnostics(file); }); + ProjectService.prototype.stopWatchingDirectory = function (directory) { + this.directoryWatchers.stopWatchingDirectory(directory); }; - Session.prototype.getDocumentHighlights = function (line, offset, fileName, filesToSearch) { - fileName = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(fileName); - if (!project || project.languageServiceDiabled) { - throw Errors.NoProject; - } - var compilerService = project.compilerService; - var position = compilerService.host.lineOffsetToPosition(fileName, line, offset); - var documentHighlights = compilerService.languageService.getDocumentHighlights(fileName, position, filesToSearch); - if (!documentHighlights) { + ProjectService.prototype.findProject = function (projectName) { + if (projectName === undefined) { return undefined; } - return documentHighlights.map(convertToDocumentHighlightsItem); - function convertToDocumentHighlightsItem(documentHighlights) { - var fileName = documentHighlights.fileName, highlightSpans = documentHighlights.highlightSpans; - return { - file: fileName, - highlightSpans: highlightSpans.map(convertHighlightSpan) - }; - function convertHighlightSpan(highlightSpan) { - var textSpan = highlightSpan.textSpan, kind = highlightSpan.kind; - var start = compilerService.host.positionToLineOffset(fileName, textSpan.start); - var end = compilerService.host.positionToLineOffset(fileName, ts.textSpanEnd(textSpan)); - return { start: start, end: end, kind: kind }; - } + if (server.isInferredProjectName(projectName)) { + this.ensureInferredProjectsUpToDate(); + return findProjectByName(projectName, this.inferredProjects); } + return this.findExternalProjectByProjectName(projectName) || this.findConfiguredProjectByProjectName(server.toNormalizedPath(projectName)); }; - Session.prototype.getProjectInfo = function (fileName, needFileNameList) { - fileName = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(fileName); - if (!project) { - throw Errors.NoProject; + ProjectService.prototype.getDefaultProjectForFile = function (fileName, refreshInferredProjects) { + if (refreshInferredProjects) { + this.ensureInferredProjectsUpToDate(); } - var projectInfo = { - configFileName: project.projectFilename, - languageServiceDisabled: project.languageServiceDiabled - }; - if (needFileNameList) { - projectInfo.fileNames = project.getFileNames(); - } - return projectInfo; + var scriptInfo = this.getScriptInfoForNormalizedPath(fileName); + return scriptInfo && scriptInfo.getDefaultProject(); }; - Session.prototype.getRenameLocations = function (line, offset, fileName, findInComments, findInStrings) { - var file = ts.normalizePath(fileName); - var info = this.projectService.getScriptInfo(file); - var projects = this.projectService.findReferencingProjects(info); - var projectsWithLanguageServiceEnabeld = ts.filter(projects, function (p) { return !p.languageServiceDiabled; }); - if (projectsWithLanguageServiceEnabeld.length === 0) { - throw Errors.NoProject; - } - var defaultProject = projectsWithLanguageServiceEnabeld[0]; - var defaultProjectCompilerService = defaultProject.compilerService; - var position = defaultProjectCompilerService.host.lineOffsetToPosition(file, line, offset); - var renameInfo = defaultProjectCompilerService.languageService.getRenameInfo(file, position); - if (!renameInfo) { - return undefined; - } - if (!renameInfo.canRename) { - return { - info: renameInfo, - locs: [] - }; - } - var fileSpans = server.combineProjectOutput(projectsWithLanguageServiceEnabeld, function (project) { - var compilerService = project.compilerService; - var renameLocations = compilerService.languageService.findRenameLocations(file, position, findInStrings, findInComments); - if (!renameLocations) { - return []; - } - return renameLocations.map(function (location) { return ({ - file: location.fileName, - start: compilerService.host.positionToLineOffset(location.fileName, location.textSpan.start), - end: compilerService.host.positionToLineOffset(location.fileName, ts.textSpanEnd(location.textSpan)) - }); }); - }, compareRenameLocation, function (a, b) { return a.file === b.file && a.start.line === b.start.line && a.start.offset === b.start.offset; }); - var locs = fileSpans.reduce(function (accum, cur) { - var curFileAccum; - if (accum.length > 0) { - curFileAccum = accum[accum.length - 1]; - if (curFileAccum.file !== cur.file) { - curFileAccum = undefined; - } - } - if (!curFileAccum) { - curFileAccum = { file: cur.file, locs: [] }; - accum.push(curFileAccum); - } - curFileAccum.locs.push({ start: cur.start, end: cur.end }); - return accum; - }, []); - return { info: renameInfo, locs: locs }; - function compareRenameLocation(a, b) { - if (a.file < b.file) { - return -1; - } - else if (a.file > b.file) { - return 1; + ProjectService.prototype.ensureInferredProjectsUpToDate = function () { + if (this.changedFiles) { + var projectsToUpdate = void 0; + if (this.changedFiles.length === 1) { + projectsToUpdate = this.changedFiles[0].containingProjects; } else { - if (a.start.line < b.start.line) { - return 1; - } - else if (a.start.line > b.start.line) { - return -1; - } - else { - return b.start.offset - a.start.offset; + projectsToUpdate = []; + for (var _i = 0, _a = this.changedFiles; _i < _a.length; _i++) { + var f = _a[_i]; + projectsToUpdate = projectsToUpdate.concat(f.containingProjects); } } + this.updateProjectGraphs(projectsToUpdate); + this.changedFiles = undefined; } }; - Session.prototype.getReferences = function (line, offset, fileName) { - var file = ts.normalizePath(fileName); - var info = this.projectService.getScriptInfo(file); - var projects = this.projectService.findReferencingProjects(info); - var projectsWithLanguageServiceEnabeld = ts.filter(projects, function (p) { return !p.languageServiceDiabled; }); - if (projectsWithLanguageServiceEnabeld.length === 0) { - throw Errors.NoProject; - } - var defaultProject = projectsWithLanguageServiceEnabeld[0]; - var position = defaultProject.compilerService.host.lineOffsetToPosition(file, line, offset); - var nameInfo = defaultProject.compilerService.languageService.getQuickInfoAtPosition(file, position); - if (!nameInfo) { - return undefined; - } - var displayString = ts.displayPartsToString(nameInfo.displayParts); - var nameSpan = nameInfo.textSpan; - var nameColStart = defaultProject.compilerService.host.positionToLineOffset(file, nameSpan.start).offset; - var nameText = defaultProject.compilerService.host.getScriptSnapshot(file).getText(nameSpan.start, ts.textSpanEnd(nameSpan)); - var refs = server.combineProjectOutput(projectsWithLanguageServiceEnabeld, function (project) { - var compilerService = project.compilerService; - var references = compilerService.languageService.getReferencesAtPosition(file, position); - if (!references) { - return []; - } - return references.map(function (ref) { - var start = compilerService.host.positionToLineOffset(ref.fileName, ref.textSpan.start); - var refLineSpan = compilerService.host.lineToTextSpan(ref.fileName, start.line - 1); - var snap = compilerService.host.getScriptSnapshot(ref.fileName); - var lineText = snap.getText(refLineSpan.start, ts.textSpanEnd(refLineSpan)).replace(/\r|\n/g, ""); - return { - file: ref.fileName, - start: start, - lineText: lineText, - end: compilerService.host.positionToLineOffset(ref.fileName, ts.textSpanEnd(ref.textSpan)), - isWriteAccess: ref.isWriteAccess, - isDefinition: ref.isDefinition - }; - }); - }, compareFileStart, areReferencesResponseItemsForTheSameLocation); - return { - refs: refs, - symbolName: nameText, - symbolStartOffset: nameColStart, - symbolDisplayString: displayString - }; - function areReferencesResponseItemsForTheSameLocation(a, b) { - if (a && b) { - return a.file === b.file && - a.start === b.start && - a.end === b.end; + ProjectService.prototype.findContainingExternalProject = function (fileName) { + for (var _i = 0, _a = this.externalProjects; _i < _a.length; _i++) { + var proj = _a[_i]; + if (proj.containsFile(fileName)) { + return proj; } - return false; } + return undefined; }; - Session.prototype.openClientFile = function (fileName, fileContent, scriptKind) { - var file = ts.normalizePath(fileName); - var _a = this.projectService.openClientFile(file, fileContent, scriptKind), configFileName = _a.configFileName, configFileErrors = _a.configFileErrors; - if (configFileErrors) { - this.configFileDiagnosticEvent(fileName, configFileName, configFileErrors); + ProjectService.prototype.getFormatCodeOptions = function (file) { + var formatCodeSettings; + if (file) { + var info = this.getScriptInfoForNormalizedPath(file); + if (info) { + formatCodeSettings = info.getFormatCodeSettings(); + } } + return formatCodeSettings || this.hostConfiguration.formatCodeOptions; }; - Session.prototype.getQuickInfo = function (line, offset, fileName) { - var file = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(file); - if (!project || project.languageServiceDiabled) { - throw Errors.NoProject; + ProjectService.prototype.updateProjectGraphs = function (projects) { + var shouldRefreshInferredProjects = false; + for (var _i = 0, projects_2 = projects; _i < projects_2.length; _i++) { + var p = projects_2[_i]; + if (!p.updateGraph()) { + shouldRefreshInferredProjects = true; + } } - var compilerService = project.compilerService; - var position = compilerService.host.lineOffsetToPosition(file, line, offset); - var quickInfo = compilerService.languageService.getQuickInfoAtPosition(file, position); - if (!quickInfo) { - return undefined; + if (shouldRefreshInferredProjects) { + this.refreshInferredProjects(); } - var displayString = ts.displayPartsToString(quickInfo.displayParts); - var docString = ts.displayPartsToString(quickInfo.documentation); - return { - kind: quickInfo.kind, - kindModifiers: quickInfo.kindModifiers, - start: compilerService.host.positionToLineOffset(file, quickInfo.textSpan.start), - end: compilerService.host.positionToLineOffset(file, ts.textSpanEnd(quickInfo.textSpan)), - displayString: displayString, - documentation: docString - }; }; - Session.prototype.getFormattingEditsForRange = function (line, offset, endLine, endOffset, fileName) { - var file = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(file); - if (!project || project.languageServiceDiabled) { - throw Errors.NoProject; - } - var compilerService = project.compilerService; - var startPosition = compilerService.host.lineOffsetToPosition(file, line, offset); - var endPosition = compilerService.host.lineOffsetToPosition(file, endLine, endOffset); - var edits = compilerService.languageService.getFormattingEditsForRange(file, startPosition, endPosition, this.projectService.getFormatCodeOptions(file)); - if (!edits) { - return undefined; + ProjectService.prototype.onSourceFileChanged = function (fileName) { + var info = this.getScriptInfoForNormalizedPath(fileName); + if (!info) { + this.logger.info("Error: got watch notification for unknown file: " + fileName); + return; } - return edits.map(function (edit) { - return { - start: compilerService.host.positionToLineOffset(file, edit.span.start), - end: compilerService.host.positionToLineOffset(file, ts.textSpanEnd(edit.span)), - newText: edit.newText ? edit.newText : "" - }; - }); - }; - Session.prototype.getFormattingEditsAfterKeystroke = function (line, offset, key, fileName) { - var file = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(file); - if (!project || project.languageServiceDiabled) { - throw Errors.NoProject; + if (!this.host.fileExists(fileName)) { + this.handleDeletedFile(info); } - var compilerService = project.compilerService; - var position = compilerService.host.lineOffsetToPosition(file, line, offset); - var formatOptions = this.projectService.getFormatCodeOptions(file); - var edits = compilerService.languageService.getFormattingEditsAfterKeystroke(file, position, key, formatOptions); - if ((key == "\n") && ((!edits) || (edits.length === 0) || allEditsBeforePos(edits, position))) { - var scriptInfo = compilerService.host.getScriptInfo(file); - if (scriptInfo) { - var lineInfo = scriptInfo.getLineInfo(line); - if (lineInfo && (lineInfo.leaf) && (lineInfo.leaf.text)) { - var lineText = lineInfo.leaf.text; - if (lineText.search("\\S") < 0) { - var editorOptions = { - BaseIndentSize: formatOptions.BaseIndentSize, - IndentSize: formatOptions.IndentSize, - TabSize: formatOptions.TabSize, - NewLineCharacter: formatOptions.NewLineCharacter, - ConvertTabsToSpaces: formatOptions.ConvertTabsToSpaces, - IndentStyle: ts.IndentStyle.Smart - }; - var preferredIndent = compilerService.languageService.getIndentationAtPosition(file, position, editorOptions); - var hasIndent = 0; - var i = void 0, len = void 0; - for (i = 0, len = lineText.length; i < len; i++) { - if (lineText.charAt(i) == " ") { - hasIndent++; - } - else if (lineText.charAt(i) == "\t") { - hasIndent += editorOptions.TabSize; - } - else { - break; - } - } - if (preferredIndent !== hasIndent) { - var firstNoWhiteSpacePosition = lineInfo.offset + i; - edits.push({ - span: ts.createTextSpanFromBounds(lineInfo.offset, firstNoWhiteSpacePosition), - newText: generateIndentString(preferredIndent, editorOptions) - }); - } - } - } + else { + if (info && (!info.isOpen)) { + info.reloadFromFile(); + this.updateProjectGraphs(info.containingProjects); } } - if (!edits) { - return undefined; - } - return edits.map(function (edit) { - return { - start: compilerService.host.positionToLineOffset(file, edit.span.start), - end: compilerService.host.positionToLineOffset(file, ts.textSpanEnd(edit.span)), - newText: edit.newText ? edit.newText : "" - }; - }); }; - Session.prototype.getCompletions = function (line, offset, prefix, fileName) { - if (!prefix) { - prefix = ""; - } - var file = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(file); - if (!project || project.languageServiceDiabled) { - throw Errors.NoProject; - } - var compilerService = project.compilerService; - var position = compilerService.host.lineOffsetToPosition(file, line, offset); - var completions = compilerService.languageService.getCompletionsAtPosition(file, position); - if (!completions) { - return undefined; - } - return completions.entries.reduce(function (result, entry) { - if (completions.isMemberCompletion || (entry.name.toLowerCase().indexOf(prefix.toLowerCase()) === 0)) { - var name_52 = entry.name, kind = entry.kind, kindModifiers = entry.kindModifiers, sortText = entry.sortText, replacementSpan = entry.replacementSpan; - var convertedSpan = undefined; - if (replacementSpan) { - convertedSpan = { - start: compilerService.host.positionToLineOffset(fileName, replacementSpan.start), - end: compilerService.host.positionToLineOffset(fileName, replacementSpan.start + replacementSpan.length) - }; - } - result.push({ name: name_52, kind: kind, kindModifiers: kindModifiers, sortText: sortText, replacementSpan: convertedSpan }); + ProjectService.prototype.handleDeletedFile = function (info) { + this.logger.info(info.fileName + " deleted"); + info.stopWatcher(); + if (!info.isOpen) { + this.filenameToScriptInfo.remove(info.path); + this.lastDeletedFile = info; + var containingProjects = info.containingProjects.slice(); + info.detachAllProjects(); + this.updateProjectGraphs(containingProjects); + this.lastDeletedFile = undefined; + if (!this.eventHandler) { + return; } - return result; - }, []).sort(function (a, b) { return a.name.localeCompare(b.name); }); - }; - Session.prototype.getCompletionEntryDetails = function (line, offset, entryNames, fileName) { - var file = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(file); - if (!project || project.languageServiceDiabled) { - throw Errors.NoProject; - } - var compilerService = project.compilerService; - var position = compilerService.host.lineOffsetToPosition(file, line, offset); - return entryNames.reduce(function (accum, entryName) { - var details = compilerService.languageService.getCompletionEntryDetails(file, position, entryName); - if (details) { - accum.push(details); + for (var _i = 0, _a = this.openFiles; _i < _a.length; _i++) { + var openFile = _a[_i]; + this.eventHandler({ eventName: "context", data: { project: openFile.getDefaultProject(), fileName: openFile.fileName } }); } - return accum; - }, []); - }; - Session.prototype.getSignatureHelpItems = function (line, offset, fileName) { - var file = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(file); - if (!project || project.languageServiceDiabled) { - throw Errors.NoProject; - } - var compilerService = project.compilerService; - var position = compilerService.host.lineOffsetToPosition(file, line, offset); - var helpItems = compilerService.languageService.getSignatureHelpItems(file, position); - if (!helpItems) { - return undefined; } - var span = helpItems.applicableSpan; - var result = { - items: helpItems.items, - applicableSpan: { - start: compilerService.host.positionToLineOffset(file, span.start), - end: compilerService.host.positionToLineOffset(file, span.start + span.length) - }, - selectedItemIndex: helpItems.selectedItemIndex, - argumentIndex: helpItems.argumentIndex, - argumentCount: helpItems.argumentCount - }; - return result; + this.printProjects(); }; - Session.prototype.getDiagnostics = function (delay, fileNames) { + ProjectService.prototype.onTypeRootFileChanged = function (project, fileName) { var _this = this; - var checkList = fileNames.reduce(function (accum, fileName) { - fileName = ts.normalizePath(fileName); - var project = _this.projectService.getProjectForFile(fileName); - if (project && !project.languageServiceDiabled) { - accum.push({ fileName: fileName, project: project }); - } - return accum; - }, []); - if (checkList.length > 0) { - this.updateErrorCheck(checkList, this.changeSeq, function (n) { return n === _this.changeSeq; }, delay); - } + this.logger.info("Type root file " + fileName + " changed"); + this.throttledOperations.schedule(project.configFileName + " * type root", 250, function () { + project.updateTypes(); + _this.updateConfiguredProject(project); + _this.refreshInferredProjects(); + }); }; - Session.prototype.change = function (line, offset, endLine, endOffset, insertString, fileName) { + ProjectService.prototype.onSourceFileInDirectoryChangedForConfiguredProject = function (project, fileName) { var _this = this; - var file = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(file); - if (project && !project.languageServiceDiabled) { - var compilerService = project.compilerService; - var start = compilerService.host.lineOffsetToPosition(file, line, offset); - var end = compilerService.host.lineOffsetToPosition(file, endLine, endOffset); - if (start >= 0) { - compilerService.host.editScript(file, start, end, insertString); - this.changeSeq++; - } - this.updateProjectStructure(this.changeSeq, function (n) { return n === _this.changeSeq; }); + if (fileName && !ts.isSupportedSourceFileName(fileName, project.getCompilerOptions())) { + return; } + this.logger.info("Detected source file changes: " + fileName); + this.throttledOperations.schedule(project.configFileName, 250, function () { return _this.handleChangeInSourceFileForConfiguredProject(project); }); }; - Session.prototype.reload = function (fileName, tempFileName, reqSeq) { + ProjectService.prototype.handleChangeInSourceFileForConfiguredProject = function (project) { var _this = this; - if (reqSeq === void 0) { reqSeq = 0; } - var file = ts.normalizePath(fileName); - var tmpfile = ts.normalizePath(tempFileName); - var project = this.projectService.getProjectForFile(file); - if (project && !project.languageServiceDiabled) { - this.changeSeq++; - project.compilerService.host.reloadScript(file, tmpfile, function () { - _this.output(undefined, CommandNames.Reload, reqSeq); - }); + var _a = this.convertConfigFileContentToProjectOptions(project.configFileName), projectOptions = _a.projectOptions, configFileErrors = _a.configFileErrors; + this.reportConfigFileDiagnostics(project.getProjectName(), configFileErrors); + var newRootFiles = projectOptions.files.map((function (f) { return _this.getCanonicalFileName(f); })); + var currentRootFiles = project.getRootFiles().map((function (f) { return _this.getCanonicalFileName(f); })); + if (!ts.arrayIsEqualTo(currentRootFiles.sort(), newRootFiles.sort())) { + this.logger.info("Updating configured project"); + this.updateConfiguredProject(project); + this.refreshInferredProjects(); } }; - Session.prototype.saveToTmp = function (fileName, tempFileName) { - var file = ts.normalizePath(fileName); - var tmpfile = ts.normalizePath(tempFileName); - var project = this.projectService.getProjectForFile(file); - if (project && !project.languageServiceDiabled) { - project.compilerService.host.saveTo(file, tmpfile); - } + ProjectService.prototype.onConfigChangedForConfiguredProject = function (project) { + this.logger.info("Config file changed: " + project.configFileName); + this.updateConfiguredProject(project); + this.refreshInferredProjects(); }; - Session.prototype.closeClientFile = function (fileName) { - if (!fileName) { + ProjectService.prototype.onConfigFileAddedForInferredProject = function (fileName) { + if (ts.getBaseFileName(fileName) != "tsconfig.json") { + this.logger.info(fileName + " is not tsconfig.json"); return; } - var file = ts.normalizePath(fileName); - this.projectService.closeClientFile(file); + var configFileErrors = this.convertConfigFileContentToProjectOptions(fileName).configFileErrors; + this.reportConfigFileDiagnostics(fileName, configFileErrors); + this.logger.info("Detected newly added tsconfig file: " + fileName); + this.reloadProjects(); }; - Session.prototype.decorateNavigationBarItem = function (project, fileName, items, lineIndex) { - var _this = this; - if (!items) { - return undefined; - } - var compilerService = project.compilerService; - return items.map(function (item) { return ({ - text: item.text, - kind: item.kind, - kindModifiers: item.kindModifiers, - spans: item.spans.map(function (span) { return ({ - start: compilerService.host.positionToLineOffset(fileName, span.start, lineIndex), - end: compilerService.host.positionToLineOffset(fileName, ts.textSpanEnd(span), lineIndex) - }); }), - childItems: _this.decorateNavigationBarItem(project, fileName, item.childItems, lineIndex), - indent: item.indent - }); }); + ProjectService.prototype.getCanonicalFileName = function (fileName) { + var name = this.host.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase(); + return ts.normalizePath(name); }; - Session.prototype.getNavigationBarItems = function (fileName) { - var file = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(file); - if (!project || project.languageServiceDiabled) { - throw Errors.NoProject; - } - var compilerService = project.compilerService; - var items = compilerService.languageService.getNavigationBarItems(file); - if (!items) { - return undefined; + ProjectService.prototype.removeProject = function (project) { + this.logger.info("remove project: " + project.getRootFiles().toString()); + project.close(); + switch (project.projectKind) { + case server.ProjectKind.External: + server.removeItemFromSet(this.externalProjects, project); + break; + case server.ProjectKind.Configured: + server.removeItemFromSet(this.configuredProjects, project); + break; + case server.ProjectKind.Inferred: + server.removeItemFromSet(this.inferredProjects, project); + break; } - return this.decorateNavigationBarItem(project, fileName, items, compilerService.host.getLineIndex(fileName)); }; - Session.prototype.getNavigateToItems = function (searchValue, fileName, maxResultCount, currentFileOnly) { - var file = ts.normalizePath(fileName); - var info = this.projectService.getScriptInfo(file); - var projects = this.projectService.findReferencingProjects(info); - var projectsWithLanguageServiceEnabeld = ts.filter(projects, function (p) { return !p.languageServiceDiabled; }); - if (projectsWithLanguageServiceEnabeld.length === 0) { - throw Errors.NoProject; - } - var allNavToItems = server.combineProjectOutput(projectsWithLanguageServiceEnabeld, function (project) { - var compilerService = project.compilerService; - var navItems = compilerService.languageService.getNavigateToItems(searchValue, maxResultCount, currentFileOnly ? fileName : undefined); - if (!navItems) { - return []; + ProjectService.prototype.assignScriptInfoToInferredProjectIfNecessary = function (info, addToListOfOpenFiles) { + var externalProject = this.findContainingExternalProject(info.fileName); + if (externalProject) { + if (addToListOfOpenFiles) { + this.openFiles.push(info); } - return navItems.map(function (navItem) { - var start = compilerService.host.positionToLineOffset(navItem.fileName, navItem.textSpan.start); - var end = compilerService.host.positionToLineOffset(navItem.fileName, ts.textSpanEnd(navItem.textSpan)); - var bakedItem = { - name: navItem.name, - kind: navItem.kind, - file: navItem.fileName, - start: start, - end: end - }; - if (navItem.kindModifiers && (navItem.kindModifiers !== "")) { - bakedItem.kindModifiers = navItem.kindModifiers; - } - if (navItem.matchKind !== "none") { - bakedItem.matchKind = navItem.matchKind; - } - if (navItem.containerName && (navItem.containerName.length > 0)) { - bakedItem.containerName = navItem.containerName; - } - if (navItem.containerKind && (navItem.containerKind.length > 0)) { - bakedItem.containerKind = navItem.containerKind; + return; + } + var foundConfiguredProject = false; + for (var _i = 0, _a = info.containingProjects; _i < _a.length; _i++) { + var p = _a[_i]; + if (p.projectKind === server.ProjectKind.Configured) { + foundConfiguredProject = true; + if (addToListOfOpenFiles) { + (p).addOpenRef(); } - return bakedItem; - }); - }, undefined, areNavToItemsForTheSameLocation); - return allNavToItems; - function areNavToItemsForTheSameLocation(a, b) { - if (a && b) { - return a.file === b.file && - a.start === b.start && - a.end === b.end; } - return false; - } - }; - Session.prototype.getBraceMatching = function (line, offset, fileName) { - var file = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(file); - if (!project || project.languageServiceDiabled) { - throw Errors.NoProject; - } - var compilerService = project.compilerService; - var position = compilerService.host.lineOffsetToPosition(file, line, offset); - var spans = compilerService.languageService.getBraceMatchingAtPosition(file, position); - if (!spans) { - return undefined; } - return spans.map(function (span) { return ({ - start: compilerService.host.positionToLineOffset(file, span.start), - end: compilerService.host.positionToLineOffset(file, span.start + span.length) - }); }); - }; - Session.prototype.getDiagnosticsForProject = function (delay, fileName) { - var _this = this; - var _a = this.getProjectInfo(fileName, true), fileNames = _a.fileNames, languageServiceDisabled = _a.languageServiceDisabled; - if (languageServiceDisabled) { + if (foundConfiguredProject) { + if (addToListOfOpenFiles) { + this.openFiles.push(info); + } return; } - var fileNamesInProject = fileNames.filter(function (value, index, array) { return value.indexOf("lib.d.ts") < 0; }); - var highPriorityFiles = []; - var mediumPriorityFiles = []; - var lowPriorityFiles = []; - var veryLowPriorityFiles = []; - var normalizedFileName = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(normalizedFileName); - for (var _i = 0, fileNamesInProject_1 = fileNamesInProject; _i < fileNamesInProject_1.length; _i++) { - var fileNameInProject = fileNamesInProject_1[_i]; - if (this.getCanonicalFileName(fileNameInProject) == this.getCanonicalFileName(fileName)) - highPriorityFiles.push(fileNameInProject); - else { - var info = this.projectService.getScriptInfo(fileNameInProject); - if (!info.isOpen) { - if (fileNameInProject.indexOf(".d.ts") > 0) - veryLowPriorityFiles.push(fileNameInProject); - else - lowPriorityFiles.push(fileNameInProject); + if (info.containingProjects.length === 0) { + var inferredProject = this.createInferredProjectWithRootFileIfNecessary(info); + if (!this.useSingleInferredProject) { + for (var _b = 0, _c = this.openFiles; _b < _c.length; _b++) { + var f = _c[_b]; + if (f.containingProjects.length === 0) { + continue; + } + var defaultProject = f.getDefaultProject(); + if (isRootFileInInferredProject(info) && defaultProject !== inferredProject && inferredProject.containsScriptInfo(f)) { + this.removeProject(defaultProject); + f.attachToProject(inferredProject); + } } - else - mediumPriorityFiles.push(fileNameInProject); } } - fileNamesInProject = highPriorityFiles.concat(mediumPriorityFiles).concat(lowPriorityFiles).concat(veryLowPriorityFiles); - if (fileNamesInProject.length > 0) { - var checkList = fileNamesInProject.map(function (fileName) { - var normalizedFileName = ts.normalizePath(fileName); - return { fileName: normalizedFileName, project: project }; - }); - this.updateErrorCheck(checkList, this.changeSeq, function (n) { return n == _this.changeSeq; }, delay, 200, false); + if (addToListOfOpenFiles) { + this.openFiles.push(info); } }; - Session.prototype.getCanonicalFileName = function (fileName) { - var name = this.host.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase(); - return ts.normalizePath(name); - }; - Session.prototype.exit = function () { - }; - Session.prototype.requiredResponse = function (response) { - return { response: response, responseRequired: true }; - }; - Session.prototype.addProtocolHandler = function (command, handler) { - if (command in this.handlers) { - throw new Error("Protocol handler already exists for command \"" + command + "\""); + ProjectService.prototype.closeOpenFile = function (info) { + info.reloadFromFile(); + server.removeItemFromSet(this.openFiles, info); + info.isOpen = false; + var projectsToRemove; + for (var _i = 0, _a = info.containingProjects; _i < _a.length; _i++) { + var p = _a[_i]; + if (p.projectKind === server.ProjectKind.Configured) { + if (p.deleteOpenRef() === 0) { + (projectsToRemove || (projectsToRemove = [])).push(p); + } + } + else if (p.projectKind === server.ProjectKind.Inferred && p.isRoot(info)) { + (projectsToRemove || (projectsToRemove = [])).push(p); + } + } + if (projectsToRemove) { + for (var _b = 0, projectsToRemove_1 = projectsToRemove; _b < projectsToRemove_1.length; _b++) { + var project = projectsToRemove_1[_b]; + this.removeProject(project); + } + var orphanFiles = void 0; + for (var _c = 0, _d = this.openFiles; _c < _d.length; _c++) { + var f = _d[_c]; + if (f.containingProjects.length === 0) { + (orphanFiles || (orphanFiles = [])).push(f); + } + } + if (orphanFiles) { + for (var _e = 0, orphanFiles_1 = orphanFiles; _e < orphanFiles_1.length; _e++) { + var f = orphanFiles_1[_e]; + this.assignScriptInfoToInferredProjectIfNecessary(f, false); + } + } + } + if (info.containingProjects.length === 0) { + this.filenameToScriptInfo.remove(info.path); } - this.handlers[command] = handler; }; - Session.prototype.executeCommand = function (request) { - var handler = this.handlers[request.command]; - if (handler) { - return handler(request); + ProjectService.prototype.openOrUpdateConfiguredProjectForFile = function (fileName) { + var searchPath = ts.getDirectoryPath(fileName); + this.logger.info("Search path: " + searchPath); + var configFileName = this.findConfigFile(server.asNormalizedPath(searchPath)); + if (!configFileName) { + this.logger.info("No config files found."); + return {}; + } + this.logger.info("Config file name: " + configFileName); + var project = this.findConfiguredProjectByProjectName(configFileName); + if (!project) { + var _a = this.openConfigFile(configFileName, fileName), success = _a.success, errors = _a.errors; + if (!success) { + return { configFileName: configFileName, configFileErrors: errors }; + } + this.logger.info("Opened configuration file " + configFileName); + if (errors && errors.length > 0) { + return { configFileName: configFileName, configFileErrors: errors }; + } } else { - this.projectService.log("Unrecognized JSON command: " + JSON.stringify(request)); - this.output(undefined, CommandNames.Unknown, request.seq, "Unrecognized JSON command: " + request.command); - return { responseRequired: false }; + this.updateConfiguredProject(project); } + return { configFileName: configFileName }; }; - Session.prototype.onMessage = function (message) { - var start; - if (this.logger.isVerbose()) { - this.logger.info("request: " + message); - start = this.hrtime(); - } - var request; - try { - request = JSON.parse(message); - var _a = this.executeCommand(request), response = _a.response, responseRequired = _a.responseRequired; - if (this.logger.isVerbose()) { - var elapsed = this.hrtime(start); - var seconds = elapsed[0]; - var nanoseconds = elapsed[1]; - var elapsedMs = ((1e9 * seconds) + nanoseconds) / 1000000.0; - var leader = "Elapsed time (in milliseconds)"; - if (!responseRequired) { - leader = "Async elapsed time (in milliseconds)"; - } - this.logger.msg(leader + ": " + elapsedMs.toFixed(4).toString(), "Perf"); - } - if (response) { - this.output(response, request.command, request.seq); + ProjectService.prototype.findConfigFile = function (searchPath) { + while (true) { + var tsconfigFileName = server.asNormalizedPath(ts.combinePaths(searchPath, "tsconfig.json")); + if (this.host.fileExists(tsconfigFileName)) { + return tsconfigFileName; } - else if (responseRequired) { - this.output(undefined, request.command, request.seq, "No content available."); + var jsconfigFileName = server.asNormalizedPath(ts.combinePaths(searchPath, "jsconfig.json")); + if (this.host.fileExists(jsconfigFileName)) { + return jsconfigFileName; } - } - catch (err) { - if (err instanceof ts.OperationCanceledException) { + var parentPath = server.asNormalizedPath(ts.getDirectoryPath(searchPath)); + if (parentPath === searchPath) { + break; } - this.logError(err, message); - this.output(undefined, request ? request.command : CommandNames.Unknown, request ? request.seq : 0, "Error processing request. " + err.message + "\n" + err.stack); + searchPath = parentPath; } + return undefined; }; - return Session; - }()); - server.Session = Session; - })(server = ts.server || (ts.server = {})); -})(ts || (ts = {})); -var ts; -(function (ts) { - var server; - (function (server) { - var lineCollectionCapacity = 4; - function mergeFormatOptions(formatCodeOptions, formatOptions) { - var hasOwnProperty = Object.prototype.hasOwnProperty; - Object.keys(formatOptions).forEach(function (key) { - var codeKey = key.charAt(0).toUpperCase() + key.substring(1); - if (hasOwnProperty.call(formatCodeOptions, codeKey)) { - formatCodeOptions[codeKey] = formatOptions[key]; + ProjectService.prototype.printProjects = function () { + if (!this.logger.hasLevel(server.LogLevel.verbose)) { + return; } - }); - } - server.maxProgramSizeForNonTsFiles = 20 * 1024 * 1024; - var ScriptInfo = (function () { - function ScriptInfo(host, fileName, content, isOpen) { - if (isOpen === void 0) { isOpen = false; } - this.host = host; - this.fileName = fileName; - this.isOpen = isOpen; - this.children = []; - this.formatCodeOptions = ts.clone(CompilerService.getDefaultFormatCodeOptions(this.host)); - this.path = ts.toPath(fileName, host.getCurrentDirectory(), ts.createGetCanonicalFileName(host.useCaseSensitiveFileNames)); - this.svc = ScriptVersionCache.fromString(host, content); - } - ScriptInfo.prototype.setFormatOptions = function (formatOptions) { - if (formatOptions) { - mergeFormatOptions(this.formatCodeOptions, formatOptions); + this.logger.startGroup(); + var counter = 0; + counter = printProjects(this.logger, this.externalProjects, counter); + counter = printProjects(this.logger, this.configuredProjects, counter); + counter = printProjects(this.logger, this.inferredProjects, counter); + this.logger.info("Open files: "); + for (var _i = 0, _a = this.openFiles; _i < _a.length; _i++) { + var rootFile = _a[_i]; + this.logger.info(rootFile.fileName); + } + this.logger.endGroup(); + function printProjects(logger, projects, counter) { + for (var _i = 0, projects_3 = projects; _i < projects_3.length; _i++) { + var project = projects_3[_i]; + project.updateGraph(); + logger.info("Project '" + project.getProjectName() + "' (" + server.ProjectKind[project.projectKind] + ") " + counter); + logger.info(project.filesToString()); + logger.info("-----------------------------------------------"); + counter++; + } + return counter; } }; - ScriptInfo.prototype.close = function () { - this.isOpen = false; - }; - ScriptInfo.prototype.addChild = function (childInfo) { - this.children.push(childInfo); + ProjectService.prototype.findConfiguredProjectByProjectName = function (configFileName) { + return findProjectByName(configFileName, this.configuredProjects); }; - ScriptInfo.prototype.snap = function () { - return this.svc.getSnapshot(); + ProjectService.prototype.findExternalProjectByProjectName = function (projectFileName) { + return findProjectByName(projectFileName, this.externalProjects); }; - ScriptInfo.prototype.getText = function () { - var snap = this.snap(); - return snap.getText(0, snap.getLength()); + ProjectService.prototype.convertConfigFileContentToProjectOptions = function (configFilename) { + configFilename = ts.normalizePath(configFilename); + var configFileContent = this.host.readFile(configFilename); + var errors; + var result = ts.parseConfigFileTextToJson(configFilename, configFileContent); + var config = result.config; + if (result.error) { + var _a = ts.sanitizeConfigFile(configFilename, configFileContent), sanitizedConfig = _a.configJsonObject, diagnostics = _a.diagnostics; + config = sanitizedConfig; + errors = diagnostics.length ? diagnostics : [result.error]; + } + var parsedCommandLine = ts.parseJsonConfigFileContent(config, this.host, ts.getDirectoryPath(configFilename), {}, configFilename); + if (parsedCommandLine.errors.length) { + errors = ts.concatenate(errors, parsedCommandLine.errors); + } + ts.Debug.assert(!!parsedCommandLine.fileNames); + if (parsedCommandLine.fileNames.length === 0) { + (errors || (errors = [])).push(ts.createCompilerDiagnostic(ts.Diagnostics.The_config_file_0_found_doesn_t_contain_any_source_files, configFilename)); + return { success: false, configFileErrors: errors }; + } + var projectOptions = { + files: parsedCommandLine.fileNames, + compilerOptions: parsedCommandLine.options, + configHasFilesProperty: config["files"] !== undefined, + wildcardDirectories: ts.createMap(parsedCommandLine.wildcardDirectories), + typingOptions: parsedCommandLine.typingOptions, + compileOnSave: parsedCommandLine.compileOnSave + }; + return { success: true, projectOptions: projectOptions, configFileErrors: errors }; }; - ScriptInfo.prototype.getLineInfo = function (line) { - var snap = this.snap(); - return snap.index.lineNumberToInfo(line); + ProjectService.prototype.exceededTotalSizeLimitForNonTsFiles = function (options, fileNames, propertyReader) { + if (options && options.disableSizeLimit || !this.host.getFileSize) { + return false; + } + var totalNonTsFileSize = 0; + for (var _i = 0, fileNames_3 = fileNames; _i < fileNames_3.length; _i++) { + var f = fileNames_3[_i]; + var fileName = propertyReader.getFileName(f); + if (ts.hasTypeScriptFileExtension(fileName)) { + continue; + } + totalNonTsFileSize += this.host.getFileSize(fileName); + if (totalNonTsFileSize > server.maxProgramSizeForNonTsFiles) { + return true; + } + } + return false; }; - ScriptInfo.prototype.editContent = function (start, end, newText) { - this.svc.edit(start, end - start, newText); + ProjectService.prototype.createAndAddExternalProject = function (projectFileName, files, options, typingOptions) { + var project = new server.ExternalProject(projectFileName, this, this.documentRegistry, options, !this.exceededTotalSizeLimitForNonTsFiles(options, files, externalFilePropertyReader), options.compileOnSave === undefined ? true : options.compileOnSave); + this.addFilesToProjectAndUpdateGraph(project, files, externalFilePropertyReader, undefined, typingOptions, undefined); + this.externalProjects.push(project); + return project; }; - ScriptInfo.prototype.getTextChangeRangeBetweenVersions = function (startVersion, endVersion) { - return this.svc.getTextChangesBetweenVersions(startVersion, endVersion); + ProjectService.prototype.reportConfigFileDiagnostics = function (configFileName, diagnostics, triggerFile) { + if (!this.eventHandler) { + return; + } + this.eventHandler({ + eventName: "configFileDiag", + data: { configFileName: configFileName, diagnostics: diagnostics || [], triggerFile: triggerFile } + }); }; - ScriptInfo.prototype.getChangeRange = function (oldSnapshot) { - return this.snap().getChangeRange(oldSnapshot); + ProjectService.prototype.createAndAddConfiguredProject = function (configFileName, projectOptions, configFileErrors, clientFileName) { + var _this = this; + var sizeLimitExceeded = this.exceededTotalSizeLimitForNonTsFiles(projectOptions.compilerOptions, projectOptions.files, fileNamePropertyReader); + var project = new server.ConfiguredProject(configFileName, this, this.documentRegistry, projectOptions.configHasFilesProperty, projectOptions.compilerOptions, projectOptions.wildcardDirectories, !sizeLimitExceeded, projectOptions.compileOnSave === undefined ? false : projectOptions.compileOnSave); + this.addFilesToProjectAndUpdateGraph(project, projectOptions.files, fileNamePropertyReader, clientFileName, projectOptions.typingOptions, configFileErrors); + project.watchConfigFile(function (project) { return _this.onConfigChangedForConfiguredProject(project); }); + if (!sizeLimitExceeded) { + this.watchConfigDirectoryForProject(project, projectOptions); + } + project.watchWildcards(function (project, path) { return _this.onSourceFileInDirectoryChangedForConfiguredProject(project, path); }); + project.watchTypeRoots(function (project, path) { return _this.onTypeRootFileChanged(project, path); }); + this.configuredProjects.push(project); + return project; }; - return ScriptInfo; - }()); - server.ScriptInfo = ScriptInfo; - var LSHost = (function () { - function LSHost(host, project) { + ProjectService.prototype.watchConfigDirectoryForProject = function (project, options) { var _this = this; - this.host = host; - this.project = project; - this.roots = []; - this.getCanonicalFileName = ts.createGetCanonicalFileName(host.useCaseSensitiveFileNames); - this.resolvedModuleNames = ts.createFileMap(); - this.resolvedTypeReferenceDirectives = ts.createFileMap(); - this.filenameToScript = ts.createFileMap(); - this.moduleResolutionHost = { - fileExists: function (fileName) { return _this.fileExists(fileName); }, - readFile: function (fileName) { return _this.host.readFile(fileName); }, - directoryExists: function (directoryName) { return _this.host.directoryExists(directoryName); } + if (!options.configHasFilesProperty) { + project.watchConfigDirectory(function (project, path) { return _this.onSourceFileInDirectoryChangedForConfiguredProject(project, path); }); + } + }; + ProjectService.prototype.addFilesToProjectAndUpdateGraph = function (project, files, propertyReader, clientFileName, typingOptions, configFileErrors) { + var errors; + for (var _i = 0, files_4 = files; _i < files_4.length; _i++) { + var f = files_4[_i]; + var rootFilename = propertyReader.getFileName(f); + var scriptKind = propertyReader.getScriptKind(f); + var hasMixedContent = propertyReader.hasMixedContent(f); + if (this.host.fileExists(rootFilename)) { + var info = this.getOrCreateScriptInfoForNormalizedPath(server.toNormalizedPath(rootFilename), clientFileName == rootFilename, undefined, scriptKind, hasMixedContent); + project.addRoot(info); + } + else { + (errors || (errors = [])).push(createFileNotFoundDiagnostic(rootFilename)); + } + } + project.setProjectErrors(ts.concatenate(configFileErrors, errors)); + project.setTypingOptions(typingOptions); + project.updateGraph(); + }; + ProjectService.prototype.openConfigFile = function (configFileName, clientFileName) { + var conversionResult = this.convertConfigFileContentToProjectOptions(configFileName); + var projectOptions = conversionResult.success + ? conversionResult.projectOptions + : { files: [], compilerOptions: {} }; + var project = this.createAndAddConfiguredProject(configFileName, projectOptions, conversionResult.configFileErrors, clientFileName); + return { + success: conversionResult.success, + project: project, + errors: project.getProjectErrors() }; - if (this.host.realpath) { - this.moduleResolutionHost.realpath = function (path) { return _this.host.realpath(path); }; + }; + ProjectService.prototype.updateNonInferredProject = function (project, newUncheckedFiles, propertyReader, newOptions, newTypingOptions, compileOnSave, configFileErrors) { + var oldRootScriptInfos = project.getRootScriptInfos(); + var newRootScriptInfos = []; + var newRootScriptInfoMap = server.createNormalizedPathMap(); + var projectErrors; + var rootFilesChanged = false; + for (var _i = 0, newUncheckedFiles_1 = newUncheckedFiles; _i < newUncheckedFiles_1.length; _i++) { + var f = newUncheckedFiles_1[_i]; + var newRootFile = propertyReader.getFileName(f); + if (!this.host.fileExists(newRootFile)) { + (projectErrors || (projectErrors = [])).push(createFileNotFoundDiagnostic(newRootFile)); + continue; + } + var normalizedPath = server.toNormalizedPath(newRootFile); + var scriptInfo = this.getScriptInfoForNormalizedPath(normalizedPath); + if (!scriptInfo || !project.isRoot(scriptInfo)) { + rootFilesChanged = true; + if (!scriptInfo) { + var scriptKind = propertyReader.getScriptKind(f); + var hasMixedContent = propertyReader.hasMixedContent(f); + scriptInfo = this.getOrCreateScriptInfoForNormalizedPath(normalizedPath, false, undefined, scriptKind, hasMixedContent); + } + } + newRootScriptInfos.push(scriptInfo); + newRootScriptInfoMap.set(scriptInfo.fileName, scriptInfo); } - } - LSHost.prototype.resolveNamesWithLocalCache = function (names, containingFile, cache, loader, getResult) { - var path = ts.toPath(containingFile, this.host.getCurrentDirectory(), this.getCanonicalFileName); - var currentResolutionsInFile = cache.get(path); - var newResolutions = ts.createMap(); - var resolvedModules = []; - var compilerOptions = this.getCompilationSettings(); - for (var _i = 0, names_3 = names; _i < names_3.length; _i++) { - var name_53 = names_3[_i]; - var resolution = newResolutions[name_53]; - if (!resolution) { - var existingResolution = currentResolutionsInFile && currentResolutionsInFile[name_53]; - if (moduleResolutionIsValid(existingResolution)) { - resolution = existingResolution; + if (rootFilesChanged || newRootScriptInfos.length !== oldRootScriptInfos.length) { + var toAdd = void 0; + var toRemove = void 0; + for (var _a = 0, oldRootScriptInfos_1 = oldRootScriptInfos; _a < oldRootScriptInfos_1.length; _a++) { + var oldFile = oldRootScriptInfos_1[_a]; + if (!newRootScriptInfoMap.contains(oldFile.fileName)) { + (toRemove || (toRemove = [])).push(oldFile); } - else { - resolution = loader(name_53, containingFile, compilerOptions, this.moduleResolutionHost); - resolution.lastCheckTime = Date.now(); - newResolutions[name_53] = resolution; + } + for (var _b = 0, newRootScriptInfos_1 = newRootScriptInfos; _b < newRootScriptInfos_1.length; _b++) { + var newFile = newRootScriptInfos_1[_b]; + if (!project.isRoot(newFile)) { + (toAdd || (toAdd = [])).push(newFile); + } + } + if (toRemove) { + for (var _c = 0, toRemove_1 = toRemove; _c < toRemove_1.length; _c++) { + var f = toRemove_1[_c]; + project.removeFile(f); + } + } + if (toAdd) { + for (var _d = 0, toAdd_1 = toAdd; _d < toAdd_1.length; _d++) { + var f = toAdd_1[_d]; + if (f.isOpen && isRootFileInInferredProject(f)) { + var inferredProject = f.containingProjects[0]; + inferredProject.removeFile(f); + if (!inferredProject.hasRoots()) { + this.removeProject(inferredProject); + } + } + project.addRoot(f); } } - ts.Debug.assert(resolution !== undefined); - resolvedModules.push(getResult(resolution)); } - cache.set(path, newResolutions); - return resolvedModules; - function moduleResolutionIsValid(resolution) { - if (!resolution) { - return false; + project.setCompilerOptions(newOptions); + project.setTypingOptions(newTypingOptions); + if (compileOnSave !== undefined) { + project.compileOnSaveEnabled = compileOnSave; + } + project.setProjectErrors(ts.concatenate(configFileErrors, projectErrors)); + project.updateGraph(); + }; + ProjectService.prototype.updateConfiguredProject = function (project) { + if (!this.host.fileExists(project.configFileName)) { + this.logger.info("Config file deleted"); + this.removeProject(project); + return; + } + var _a = this.convertConfigFileContentToProjectOptions(project.configFileName), success = _a.success, projectOptions = _a.projectOptions, configFileErrors = _a.configFileErrors; + if (!success) { + this.updateNonInferredProject(project, [], fileNamePropertyReader, {}, {}, false, configFileErrors); + return configFileErrors; + } + if (this.exceededTotalSizeLimitForNonTsFiles(projectOptions.compilerOptions, projectOptions.files, fileNamePropertyReader)) { + project.setCompilerOptions(projectOptions.compilerOptions); + if (!project.languageServiceEnabled) { + return; } - if (getResult(resolution)) { - return true; + project.disableLanguageService(); + project.stopWatchingDirectory(); + } + else { + if (!project.languageServiceEnabled) { + project.enableLanguageService(); } - return resolution.failedLookupLocations.length === 0; + this.watchConfigDirectoryForProject(project, projectOptions); + this.updateNonInferredProject(project, projectOptions.files, fileNamePropertyReader, projectOptions.compilerOptions, projectOptions.typingOptions, projectOptions.compileOnSave, configFileErrors); } }; - LSHost.prototype.resolveTypeReferenceDirectives = function (typeDirectiveNames, containingFile) { - return this.resolveNamesWithLocalCache(typeDirectiveNames, containingFile, this.resolvedTypeReferenceDirectives, ts.resolveTypeReferenceDirective, function (m) { return m.resolvedTypeReferenceDirective; }); + ProjectService.prototype.createInferredProjectWithRootFileIfNecessary = function (root) { + var _this = this; + var useExistingProject = this.useSingleInferredProject && this.inferredProjects.length; + var project = useExistingProject + ? this.inferredProjects[0] + : new server.InferredProject(this, this.documentRegistry, true, this.compilerOptionsForInferredProjects, this.compileOnSaveForInferredProjects); + project.addRoot(root); + this.directoryWatchers.startWatchingContainingDirectoriesForFile(root.fileName, project, function (fileName) { return _this.onConfigFileAddedForInferredProject(fileName); }); + project.updateGraph(); + if (!useExistingProject) { + this.inferredProjects.push(project); + } + return project; }; - LSHost.prototype.resolveModuleNames = function (moduleNames, containingFile) { - return this.resolveNamesWithLocalCache(moduleNames, containingFile, this.resolvedModuleNames, ts.resolveModuleName, function (m) { return m.resolvedModule; }); + ProjectService.prototype.getOrCreateScriptInfo = function (uncheckedFileName, openedByClient, fileContent, scriptKind) { + return this.getOrCreateScriptInfoForNormalizedPath(server.toNormalizedPath(uncheckedFileName), openedByClient, fileContent, scriptKind); }; - LSHost.prototype.getDefaultLibFileName = function () { - var nodeModuleBinDir = ts.getDirectoryPath(ts.normalizePath(this.host.getExecutingFilePath())); - return ts.combinePaths(nodeModuleBinDir, ts.getDefaultLibFileName(this.compilationSettings)); + ProjectService.prototype.getScriptInfo = function (uncheckedFileName) { + return this.getScriptInfoForNormalizedPath(server.toNormalizedPath(uncheckedFileName)); }; - LSHost.prototype.getScriptSnapshot = function (filename) { - var scriptInfo = this.getScriptInfo(filename); - if (scriptInfo) { - return scriptInfo.snap(); + ProjectService.prototype.getOrCreateScriptInfoForNormalizedPath = function (fileName, openedByClient, fileContent, scriptKind, hasMixedContent) { + var _this = this; + var info = this.getScriptInfoForNormalizedPath(fileName); + if (!info) { + var content = void 0; + if (this.host.fileExists(fileName)) { + content = fileContent || (hasMixedContent ? "" : this.host.readFile(fileName)); + } + if (!content) { + if (openedByClient) { + content = ""; + } + } + if (content !== undefined) { + info = new server.ScriptInfo(this.host, fileName, content, scriptKind, openedByClient, hasMixedContent); + this.filenameToScriptInfo.set(info.path, info); + if (!info.isOpen && !hasMixedContent) { + info.setWatcher(this.host.watchFile(fileName, function (_) { return _this.onSourceFileChanged(fileName); })); + } + } + } + if (info) { + if (fileContent !== undefined) { + info.reload(fileContent); + } + if (openedByClient) { + info.isOpen = true; + } } + return info; }; - LSHost.prototype.setCompilationSettings = function (opt) { - this.compilationSettings = opt; - this.resolvedModuleNames.clear(); - this.resolvedTypeReferenceDirectives.clear(); + ProjectService.prototype.getScriptInfoForNormalizedPath = function (fileName) { + return this.getScriptInfoForPath(server.normalizedPathToPath(fileName, this.host.getCurrentDirectory(), this.toCanonicalFileName)); }; - LSHost.prototype.lineAffectsRefs = function (filename, line) { - var info = this.getScriptInfo(filename); - var lineInfo = info.getLineInfo(line); - if (lineInfo && lineInfo.text) { - var regex = /reference|import|\/\*|\*\//; - return regex.test(lineInfo.text); + ProjectService.prototype.getScriptInfoForPath = function (fileName) { + return this.filenameToScriptInfo.get(fileName); + }; + ProjectService.prototype.setHostConfiguration = function (args) { + if (args.file) { + var info = this.getScriptInfoForNormalizedPath(server.toNormalizedPath(args.file)); + if (info) { + info.setFormatOptions(args.formatOptions); + this.logger.info("Host configuration update for file " + args.file); + } + } + else { + if (args.hostInfo !== undefined) { + this.hostConfiguration.hostInfo = args.hostInfo; + this.logger.info("Host information " + args.hostInfo); + } + if (args.formatOptions) { + server.mergeMaps(this.hostConfiguration.formatCodeOptions, args.formatOptions); + this.logger.info("Format host information updated"); + } } }; - LSHost.prototype.getCompilationSettings = function () { - return this.compilationSettings; + ProjectService.prototype.closeLog = function () { + this.logger.close(); }; - LSHost.prototype.getScriptFileNames = function () { - return this.roots.map(function (root) { return root.fileName; }); + ProjectService.prototype.reloadProjects = function () { + this.logger.info("reload projects."); + for (var _i = 0, _a = this.openFiles; _i < _a.length; _i++) { + var info = _a[_i]; + this.openOrUpdateConfiguredProjectForFile(info.fileName); + } + this.refreshInferredProjects(); }; - LSHost.prototype.getScriptKind = function (fileName) { - var info = this.getScriptInfo(fileName); - if (!info) { - return undefined; + ProjectService.prototype.refreshInferredProjects = function () { + this.logger.info("updating project structure from ..."); + this.printProjects(); + var orphantedFiles = []; + for (var _i = 0, _a = this.openFiles; _i < _a.length; _i++) { + var info = _a[_i]; + if (info.containingProjects.length === 0) { + orphantedFiles.push(info); + } + else { + if (isRootFileInInferredProject(info) && info.containingProjects.length > 1) { + var inferredProject = info.containingProjects[0]; + ts.Debug.assert(inferredProject.projectKind === server.ProjectKind.Inferred); + inferredProject.removeFile(info); + if (!inferredProject.hasRoots()) { + this.removeProject(inferredProject); + } + } + } + } + for (var _b = 0, orphantedFiles_1 = orphantedFiles; _b < orphantedFiles_1.length; _b++) { + var f = orphantedFiles_1[_b]; + this.assignScriptInfoToInferredProjectIfNecessary(f, false); } - if (!info.scriptKind) { - info.scriptKind = ts.getScriptKindFromFileName(fileName); + for (var _c = 0, _d = this.inferredProjects; _c < _d.length; _c++) { + var p = _d[_c]; + p.updateGraph(); } - return info.scriptKind; + this.printProjects(); }; - LSHost.prototype.getScriptVersion = function (filename) { - return this.getScriptInfo(filename).svc.latestVersion().toString(); + ProjectService.prototype.openClientFile = function (fileName, fileContent, scriptKind) { + return this.openClientFileWithNormalizedPath(server.toNormalizedPath(fileName), fileContent, scriptKind); + }; + ProjectService.prototype.openClientFileWithNormalizedPath = function (fileName, fileContent, scriptKind, hasMixedContent) { + var _a = this.findContainingExternalProject(fileName) + ? {} + : this.openOrUpdateConfiguredProjectForFile(fileName), _b = _a.configFileName, configFileName = _b === void 0 ? undefined : _b, _c = _a.configFileErrors, configFileErrors = _c === void 0 ? undefined : _c; + var info = this.getOrCreateScriptInfoForNormalizedPath(fileName, true, fileContent, scriptKind, hasMixedContent); + this.assignScriptInfoToInferredProjectIfNecessary(info, true); + this.printProjects(); + return { configFileName: configFileName, configFileErrors: configFileErrors }; + }; + ProjectService.prototype.closeClientFile = function (uncheckedFileName) { + var info = this.getScriptInfoForNormalizedPath(server.toNormalizedPath(uncheckedFileName)); + if (info) { + this.closeOpenFile(info); + info.isOpen = false; + } + this.printProjects(); }; - LSHost.prototype.getCurrentDirectory = function () { - return ""; + ProjectService.prototype.collectChanges = function (lastKnownProjectVersions, currentProjects, result) { + var _loop_3 = function (proj) { + var knownProject = ts.forEach(lastKnownProjectVersions, function (p) { return p.projectName === proj.getProjectName() && p; }); + result.push(proj.getChangesSinceVersion(knownProject && knownProject.version)); + }; + for (var _i = 0, currentProjects_1 = currentProjects; _i < currentProjects_1.length; _i++) { + var proj = currentProjects_1[_i]; + _loop_3(proj); + } }; - LSHost.prototype.getScriptIsOpen = function (filename) { - return this.getScriptInfo(filename).isOpen; + ProjectService.prototype.synchronizeProjectList = function (knownProjects) { + var files = []; + this.collectChanges(knownProjects, this.externalProjects, files); + this.collectChanges(knownProjects, this.configuredProjects, files); + this.collectChanges(knownProjects, this.inferredProjects, files); + return files; }; - LSHost.prototype.removeReferencedFile = function (info) { - if (!info.isOpen) { - this.filenameToScript.remove(info.path); - this.resolvedModuleNames.remove(info.path); - this.resolvedTypeReferenceDirectives.remove(info.path); + ProjectService.prototype.applyChangesInOpenFiles = function (openFiles, changedFiles, closedFiles) { + var recordChangedFiles = changedFiles && !openFiles && !closedFiles; + if (openFiles) { + for (var _i = 0, openFiles_1 = openFiles; _i < openFiles_1.length; _i++) { + var file = openFiles_1[_i]; + var scriptInfo = this.getScriptInfo(file.fileName); + ts.Debug.assert(!scriptInfo || !scriptInfo.isOpen); + var normalizedPath = scriptInfo ? scriptInfo.fileName : server.toNormalizedPath(file.fileName); + this.openClientFileWithNormalizedPath(normalizedPath, file.content, file.scriptKind, file.hasMixedContent); + } + } + if (changedFiles) { + for (var _a = 0, changedFiles_1 = changedFiles; _a < changedFiles_1.length; _a++) { + var file = changedFiles_1[_a]; + var scriptInfo = this.getScriptInfo(file.fileName); + ts.Debug.assert(!!scriptInfo); + for (var i = file.changes.length - 1; i >= 0; i--) { + var change = file.changes[i]; + scriptInfo.editContent(change.span.start, change.span.start + change.span.length, change.newText); + } + if (recordChangedFiles) { + if (!this.changedFiles) { + this.changedFiles = [scriptInfo]; + } + else if (this.changedFiles.indexOf(scriptInfo) < 0) { + this.changedFiles.push(scriptInfo); + } + } + } } - }; - LSHost.prototype.getScriptInfo = function (filename) { - var path = ts.toPath(filename, this.host.getCurrentDirectory(), this.getCanonicalFileName); - var scriptInfo = this.filenameToScript.get(path); - if (!scriptInfo) { - scriptInfo = this.project.openReferencedFile(filename); - if (scriptInfo) { - this.filenameToScript.set(path, scriptInfo); + if (closedFiles) { + for (var _b = 0, closedFiles_1 = closedFiles; _b < closedFiles_1.length; _b++) { + var file = closedFiles_1[_b]; + this.closeClientFile(file); } } - return scriptInfo; - }; - LSHost.prototype.addRoot = function (info) { - if (!this.filenameToScript.contains(info.path)) { - this.filenameToScript.set(info.path, info); - this.roots.push(info); + if (openFiles || closedFiles) { + this.refreshInferredProjects(); } }; - LSHost.prototype.removeRoot = function (info) { - if (this.filenameToScript.contains(info.path)) { - this.filenameToScript.remove(info.path); - ts.unorderedRemoveItem(this.roots, info); - this.resolvedModuleNames.remove(info.path); - this.resolvedTypeReferenceDirectives.remove(info.path); + ProjectService.prototype.closeConfiguredProject = function (configFile) { + var configuredProject = this.findConfiguredProjectByProjectName(configFile); + if (configuredProject && configuredProject.deleteOpenRef() === 0) { + this.removeProject(configuredProject); } }; - LSHost.prototype.saveTo = function (filename, tmpfilename) { - var script = this.getScriptInfo(filename); - if (script) { - var snap = script.snap(); - this.host.writeFile(tmpfilename, snap.getText(0, snap.getLength())); + ProjectService.prototype.closeExternalProject = function (uncheckedFileName, suppressRefresh) { + if (suppressRefresh === void 0) { suppressRefresh = false; } + var fileName = server.toNormalizedPath(uncheckedFileName); + var configFiles = this.externalProjectToConfiguredProjectMap[fileName]; + if (configFiles) { + var shouldRefreshInferredProjects = false; + for (var _i = 0, configFiles_1 = configFiles; _i < configFiles_1.length; _i++) { + var configFile = configFiles_1[_i]; + if (this.closeConfiguredProject(configFile)) { + shouldRefreshInferredProjects = true; + } + } + delete this.externalProjectToConfiguredProjectMap[fileName]; + if (shouldRefreshInferredProjects && !suppressRefresh) { + this.refreshInferredProjects(); + } } - }; - LSHost.prototype.reloadScript = function (filename, tmpfilename, cb) { - var script = this.getScriptInfo(filename); - if (script) { - script.svc.reloadFromFile(tmpfilename, cb); + else { + var externalProject = this.findExternalProjectByProjectName(uncheckedFileName); + if (externalProject) { + this.removeProject(externalProject); + if (!suppressRefresh) { + this.refreshInferredProjects(); + } + } } }; - LSHost.prototype.editScript = function (filename, start, end, newText) { - var script = this.getScriptInfo(filename); - if (script) { - script.editContent(start, end, newText); - return; + ProjectService.prototype.openExternalProject = function (proj) { + var tsConfigFiles; + var rootFiles = []; + for (var _i = 0, _a = proj.rootFiles; _i < _a.length; _i++) { + var file = _a[_i]; + var normalized = server.toNormalizedPath(file.fileName); + if (ts.getBaseFileName(normalized) === "tsconfig.json") { + (tsConfigFiles || (tsConfigFiles = [])).push(normalized); + } + else { + rootFiles.push(file); + } } - throw new Error("No script with name '" + filename + "'"); - }; - LSHost.prototype.fileExists = function (path) { - var result = this.host.fileExists(path); - return result; - }; - LSHost.prototype.directoryExists = function (path) { - return this.host.directoryExists(path); - }; - LSHost.prototype.getDirectories = function (path) { - return this.host.getDirectories(path); - }; - LSHost.prototype.readDirectory = function (path, extensions, exclude, include) { - return this.host.readDirectory(path, extensions, exclude, include); - }; - LSHost.prototype.readFile = function (path, encoding) { - return this.host.readFile(path, encoding); - }; - LSHost.prototype.lineToTextSpan = function (filename, line) { - var path = ts.toPath(filename, this.host.getCurrentDirectory(), this.getCanonicalFileName); - var script = this.filenameToScript.get(path); - var index = script.snap().index; - var lineInfo = index.lineNumberToInfo(line + 1); - var len; - if (lineInfo.leaf) { - len = lineInfo.leaf.text.length; + if (tsConfigFiles) { + tsConfigFiles.sort(); + } + var externalProject = this.findExternalProjectByProjectName(proj.projectFileName); + var exisingConfigFiles; + if (externalProject) { + if (!tsConfigFiles) { + this.updateNonInferredProject(externalProject, proj.rootFiles, externalFilePropertyReader, proj.options, proj.typingOptions, proj.options.compileOnSave, undefined); + return; + } + this.closeExternalProject(proj.projectFileName, true); + } + else if (this.externalProjectToConfiguredProjectMap[proj.projectFileName]) { + if (!tsConfigFiles) { + this.closeExternalProject(proj.projectFileName, true); + } + else { + var oldConfigFiles = this.externalProjectToConfiguredProjectMap[proj.projectFileName]; + var iNew = 0; + var iOld = 0; + while (iNew < tsConfigFiles.length && iOld < oldConfigFiles.length) { + var newConfig = tsConfigFiles[iNew]; + var oldConfig = oldConfigFiles[iOld]; + if (oldConfig < newConfig) { + this.closeConfiguredProject(oldConfig); + iOld++; + } + else if (oldConfig > newConfig) { + iNew++; + } + else { + (exisingConfigFiles || (exisingConfigFiles = [])).push(oldConfig); + iOld++; + iNew++; + } + } + for (var i = iOld; i < oldConfigFiles.length; i++) { + this.closeConfiguredProject(oldConfigFiles[i]); + } + } + } + if (tsConfigFiles) { + this.externalProjectToConfiguredProjectMap[proj.projectFileName] = tsConfigFiles; + for (var _b = 0, tsConfigFiles_1 = tsConfigFiles; _b < tsConfigFiles_1.length; _b++) { + var tsconfigFile = tsConfigFiles_1[_b]; + var project = this.findConfiguredProjectByProjectName(tsconfigFile); + if (!project) { + var result = this.openConfigFile(tsconfigFile); + project = result.success && result.project; + } + if (project && !ts.contains(exisingConfigFiles, tsconfigFile)) { + project.addOpenRef(); + } + } } else { - var nextLineInfo = index.lineNumberToInfo(line + 2); - len = nextLineInfo.offset - lineInfo.offset; + delete this.externalProjectToConfiguredProjectMap[proj.projectFileName]; + this.createAndAddExternalProject(proj.projectFileName, rootFiles, proj.options, proj.typingOptions); } - return ts.createTextSpan(lineInfo.offset, len); - }; - LSHost.prototype.lineOffsetToPosition = function (filename, line, offset) { - var path = ts.toPath(filename, this.host.getCurrentDirectory(), this.getCanonicalFileName); - var script = this.filenameToScript.get(path); - var index = script.snap().index; - var lineInfo = index.lineNumberToInfo(line); - return (lineInfo.offset + offset - 1); - }; - LSHost.prototype.positionToLineOffset = function (filename, position, lineIndex) { - lineIndex = lineIndex || this.getLineIndex(filename); - var lineOffset = lineIndex.charOffsetToLineNumberAndPos(position); - return { line: lineOffset.line, offset: lineOffset.offset + 1 }; - }; - LSHost.prototype.getLineIndex = function (filename) { - var path = ts.toPath(filename, this.host.getCurrentDirectory(), this.getCanonicalFileName); - var script = this.filenameToScript.get(path); - return script.snap().index; + this.refreshInferredProjects(); }; - return LSHost; + return ProjectService; }()); - server.LSHost = LSHost; - var Project = (function () { - function Project(projectService, projectOptions, languageServiceDiabled) { - if (languageServiceDiabled === void 0) { languageServiceDiabled = false; } - this.projectService = projectService; - this.projectOptions = projectOptions; - this.languageServiceDiabled = languageServiceDiabled; - this.directoriesWatchedForTsconfig = []; - this.filenameToSourceFile = ts.createMap(); - this.updateGraphSeq = 0; - this.openRefCount = 0; - if (projectOptions && projectOptions.files) { - projectOptions.compilerOptions.allowNonTsExtensions = true; + server.ProjectService = ProjectService; + })(server = ts.server || (ts.server = {})); +})(ts || (ts = {})); +var ts; +(function (ts) { + var server; + (function (server) { + function hrTimeToMilliseconds(time) { + var seconds = time[0]; + var nanoseconds = time[1]; + return ((1e9 * seconds) + nanoseconds) / 1000000.0; + } + function shouldSkipSematicCheck(project) { + if (project.getCompilerOptions().skipLibCheck !== undefined) { + return false; + } + if ((project.projectKind === server.ProjectKind.Inferred || project.projectKind === server.ProjectKind.External) && project.isJsOnlyProject()) { + return true; + } + return false; + } + function compareNumber(a, b) { + return a - b; + } + function compareFileStart(a, b) { + if (a.file < b.file) { + return -1; + } + else if (a.file == b.file) { + var n = compareNumber(a.start.line, b.start.line); + if (n === 0) { + return compareNumber(a.start.offset, b.start.offset); } - if (!languageServiceDiabled) { - this.compilerService = new CompilerService(this, projectOptions && projectOptions.compilerOptions); + else + return n; + } + else { + return 1; + } + } + function formatDiag(fileName, project, diag) { + var scriptInfo = project.getScriptInfoForNormalizedPath(fileName); + return { + start: scriptInfo.positionToLineOffset(diag.start), + end: scriptInfo.positionToLineOffset(diag.start + diag.length), + text: ts.flattenDiagnosticMessageText(diag.messageText, "\n"), + code: diag.code + }; + } + function formatConfigFileDiag(diag) { + return { + start: undefined, + end: undefined, + text: ts.flattenDiagnosticMessageText(diag.messageText, "\n") + }; + } + function allEditsBeforePos(edits, pos) { + for (var _i = 0, edits_1 = edits; _i < edits_1.length; _i++) { + var edit = edits_1[_i]; + if (ts.textSpanEnd(edit.span) >= pos) { + return false; } } - Project.prototype.enableLanguageService = function () { - if (this.languageServiceDiabled) { - this.compilerService = new CompilerService(this, this.projectOptions && this.projectOptions.compilerOptions); + return true; + } + var CommandNames; + (function (CommandNames) { + CommandNames.Brace = "brace"; + CommandNames.BraceFull = "brace-full"; + CommandNames.BraceCompletion = "braceCompletion"; + CommandNames.Change = "change"; + CommandNames.Close = "close"; + CommandNames.Completions = "completions"; + CommandNames.CompletionsFull = "completions-full"; + CommandNames.CompletionDetails = "completionEntryDetails"; + CommandNames.CompileOnSaveAffectedFileList = "compileOnSaveAffectedFileList"; + CommandNames.CompileOnSaveEmitFile = "compileOnSaveEmitFile"; + CommandNames.Configure = "configure"; + CommandNames.Definition = "definition"; + CommandNames.DefinitionFull = "definition-full"; + CommandNames.Exit = "exit"; + CommandNames.Format = "format"; + CommandNames.Formatonkey = "formatonkey"; + CommandNames.FormatFull = "format-full"; + CommandNames.FormatonkeyFull = "formatonkey-full"; + CommandNames.FormatRangeFull = "formatRange-full"; + CommandNames.Geterr = "geterr"; + CommandNames.GeterrForProject = "geterrForProject"; + CommandNames.Implementation = "implementation"; + CommandNames.ImplementationFull = "implementation-full"; + CommandNames.SemanticDiagnosticsSync = "semanticDiagnosticsSync"; + CommandNames.SyntacticDiagnosticsSync = "syntacticDiagnosticsSync"; + CommandNames.NavBar = "navbar"; + CommandNames.NavBarFull = "navbar-full"; + CommandNames.NavTree = "navtree"; + CommandNames.NavTreeFull = "navtree-full"; + CommandNames.Navto = "navto"; + CommandNames.NavtoFull = "navto-full"; + CommandNames.Occurrences = "occurrences"; + CommandNames.DocumentHighlights = "documentHighlights"; + CommandNames.DocumentHighlightsFull = "documentHighlights-full"; + CommandNames.Open = "open"; + CommandNames.Quickinfo = "quickinfo"; + CommandNames.QuickinfoFull = "quickinfo-full"; + CommandNames.References = "references"; + CommandNames.ReferencesFull = "references-full"; + CommandNames.Reload = "reload"; + CommandNames.Rename = "rename"; + CommandNames.RenameInfoFull = "rename-full"; + CommandNames.RenameLocationsFull = "renameLocations-full"; + CommandNames.Saveto = "saveto"; + CommandNames.SignatureHelp = "signatureHelp"; + CommandNames.SignatureHelpFull = "signatureHelp-full"; + CommandNames.TypeDefinition = "typeDefinition"; + CommandNames.ProjectInfo = "projectInfo"; + CommandNames.ReloadProjects = "reloadProjects"; + CommandNames.Unknown = "unknown"; + CommandNames.OpenExternalProject = "openExternalProject"; + CommandNames.OpenExternalProjects = "openExternalProjects"; + CommandNames.CloseExternalProject = "closeExternalProject"; + CommandNames.SynchronizeProjectList = "synchronizeProjectList"; + CommandNames.ApplyChangedToOpenFiles = "applyChangedToOpenFiles"; + CommandNames.EncodedSemanticClassificationsFull = "encodedSemanticClassifications-full"; + CommandNames.Cleanup = "cleanup"; + CommandNames.OutliningSpans = "outliningSpans"; + CommandNames.TodoComments = "todoComments"; + CommandNames.Indentation = "indentation"; + CommandNames.DocCommentTemplate = "docCommentTemplate"; + CommandNames.CompilerOptionsDiagnosticsFull = "compilerOptionsDiagnostics-full"; + CommandNames.NameOrDottedNameSpan = "nameOrDottedNameSpan"; + CommandNames.BreakpointStatement = "breakpointStatement"; + CommandNames.CompilerOptionsForInferredProjects = "compilerOptionsForInferredProjects"; + CommandNames.GetCodeFixes = "getCodeFixes"; + CommandNames.GetCodeFixesFull = "getCodeFixes-full"; + CommandNames.GetSupportedCodeFixes = "getSupportedCodeFixes"; + })(CommandNames = server.CommandNames || (server.CommandNames = {})); + function formatMessage(msg, logger, byteLength, newLine) { + var verboseLogging = logger.hasLevel(server.LogLevel.verbose); + var json = JSON.stringify(msg); + if (verboseLogging) { + logger.info(msg.type + ": " + json); + } + var len = byteLength(json, "utf8"); + return "Content-Length: " + (1 + len) + "\r\n\r\n" + json + newLine; + } + server.formatMessage = formatMessage; + var Session = (function () { + function Session(host, cancellationToken, useSingleInferredProject, typingsInstaller, byteLength, hrtime, logger, canUseEvents, eventHandler) { + var _this = this; + this.host = host; + this.typingsInstaller = typingsInstaller; + this.byteLength = byteLength; + this.hrtime = hrtime; + this.logger = logger; + this.canUseEvents = canUseEvents; + this.changeSeq = 0; + this.handlers = ts.createMap((_a = {}, + _a[CommandNames.OpenExternalProject] = function (request) { + _this.projectService.openExternalProject(request.arguments); + return _this.requiredResponse(true); + }, + _a[CommandNames.OpenExternalProjects] = function (request) { + for (var _i = 0, _a = request.arguments.projects; _i < _a.length; _i++) { + var proj = _a[_i]; + _this.projectService.openExternalProject(proj); + } + return _this.requiredResponse(true); + }, + _a[CommandNames.CloseExternalProject] = function (request) { + _this.projectService.closeExternalProject(request.arguments.projectFileName); + return _this.requiredResponse(true); + }, + _a[CommandNames.SynchronizeProjectList] = function (request) { + var result = _this.projectService.synchronizeProjectList(request.arguments.knownProjects); + if (!result.some(function (p) { return p.projectErrors && p.projectErrors.length !== 0; })) { + return _this.requiredResponse(result); + } + var converted = ts.map(result, function (p) { + if (!p.projectErrors || p.projectErrors.length === 0) { + return p; + } + return { + info: p.info, + changes: p.changes, + files: p.files, + projectErrors: _this.convertToDiagnosticsWithLinePosition(p.projectErrors, undefined) + }; + }); + return _this.requiredResponse(converted); + }, + _a[CommandNames.ApplyChangedToOpenFiles] = function (request) { + _this.projectService.applyChangesInOpenFiles(request.arguments.openFiles, request.arguments.changedFiles, request.arguments.closedFiles); + _this.changeSeq++; + return _this.requiredResponse(true); + }, + _a[CommandNames.Exit] = function () { + _this.exit(); + return _this.notRequired(); + }, + _a[CommandNames.Definition] = function (request) { + return _this.requiredResponse(_this.getDefinition(request.arguments, true)); + }, + _a[CommandNames.DefinitionFull] = function (request) { + return _this.requiredResponse(_this.getDefinition(request.arguments, false)); + }, + _a[CommandNames.TypeDefinition] = function (request) { + return _this.requiredResponse(_this.getTypeDefinition(request.arguments)); + }, + _a[CommandNames.Implementation] = function (request) { + return _this.requiredResponse(_this.getImplementation(request.arguments, true)); + }, + _a[CommandNames.ImplementationFull] = function (request) { + return _this.requiredResponse(_this.getImplementation(request.arguments, false)); + }, + _a[CommandNames.References] = function (request) { + return _this.requiredResponse(_this.getReferences(request.arguments, true)); + }, + _a[CommandNames.ReferencesFull] = function (request) { + return _this.requiredResponse(_this.getReferences(request.arguments, false)); + }, + _a[CommandNames.Rename] = function (request) { + return _this.requiredResponse(_this.getRenameLocations(request.arguments, true)); + }, + _a[CommandNames.RenameLocationsFull] = function (request) { + return _this.requiredResponse(_this.getRenameLocations(request.arguments, false)); + }, + _a[CommandNames.RenameInfoFull] = function (request) { + return _this.requiredResponse(_this.getRenameInfo(request.arguments)); + }, + _a[CommandNames.Open] = function (request) { + var openArgs = request.arguments; + var scriptKind; + switch (openArgs.scriptKindName) { + case "TS": + scriptKind = 3; + break; + case "JS": + scriptKind = 1; + break; + case "TSX": + scriptKind = 4; + break; + case "JSX": + scriptKind = 2; + break; + } + _this.openClientFile(server.toNormalizedPath(openArgs.file), openArgs.fileContent, scriptKind); + return _this.notRequired(); + }, + _a[CommandNames.Quickinfo] = function (request) { + return _this.requiredResponse(_this.getQuickInfoWorker(request.arguments, true)); + }, + _a[CommandNames.QuickinfoFull] = function (request) { + return _this.requiredResponse(_this.getQuickInfoWorker(request.arguments, false)); + }, + _a[CommandNames.OutliningSpans] = function (request) { + return _this.requiredResponse(_this.getOutliningSpans(request.arguments)); + }, + _a[CommandNames.TodoComments] = function (request) { + return _this.requiredResponse(_this.getTodoComments(request.arguments)); + }, + _a[CommandNames.Indentation] = function (request) { + return _this.requiredResponse(_this.getIndentation(request.arguments)); + }, + _a[CommandNames.NameOrDottedNameSpan] = function (request) { + return _this.requiredResponse(_this.getNameOrDottedNameSpan(request.arguments)); + }, + _a[CommandNames.BreakpointStatement] = function (request) { + return _this.requiredResponse(_this.getBreakpointStatement(request.arguments)); + }, + _a[CommandNames.BraceCompletion] = function (request) { + return _this.requiredResponse(_this.isValidBraceCompletion(request.arguments)); + }, + _a[CommandNames.DocCommentTemplate] = function (request) { + return _this.requiredResponse(_this.getDocCommentTemplate(request.arguments)); + }, + _a[CommandNames.Format] = function (request) { + return _this.requiredResponse(_this.getFormattingEditsForRange(request.arguments)); + }, + _a[CommandNames.Formatonkey] = function (request) { + return _this.requiredResponse(_this.getFormattingEditsAfterKeystroke(request.arguments)); + }, + _a[CommandNames.FormatFull] = function (request) { + return _this.requiredResponse(_this.getFormattingEditsForDocumentFull(request.arguments)); + }, + _a[CommandNames.FormatonkeyFull] = function (request) { + return _this.requiredResponse(_this.getFormattingEditsAfterKeystrokeFull(request.arguments)); + }, + _a[CommandNames.FormatRangeFull] = function (request) { + return _this.requiredResponse(_this.getFormattingEditsForRangeFull(request.arguments)); + }, + _a[CommandNames.Completions] = function (request) { + return _this.requiredResponse(_this.getCompletions(request.arguments, true)); + }, + _a[CommandNames.CompletionsFull] = function (request) { + return _this.requiredResponse(_this.getCompletions(request.arguments, false)); + }, + _a[CommandNames.CompletionDetails] = function (request) { + return _this.requiredResponse(_this.getCompletionEntryDetails(request.arguments)); + }, + _a[CommandNames.CompileOnSaveAffectedFileList] = function (request) { + return _this.requiredResponse(_this.getCompileOnSaveAffectedFileList(request.arguments)); + }, + _a[CommandNames.CompileOnSaveEmitFile] = function (request) { + return _this.requiredResponse(_this.emitFile(request.arguments)); + }, + _a[CommandNames.SignatureHelp] = function (request) { + return _this.requiredResponse(_this.getSignatureHelpItems(request.arguments, true)); + }, + _a[CommandNames.SignatureHelpFull] = function (request) { + return _this.requiredResponse(_this.getSignatureHelpItems(request.arguments, false)); + }, + _a[CommandNames.CompilerOptionsDiagnosticsFull] = function (request) { + return _this.requiredResponse(_this.getCompilerOptionsDiagnostics(request.arguments)); + }, + _a[CommandNames.EncodedSemanticClassificationsFull] = function (request) { + return _this.requiredResponse(_this.getEncodedSemanticClassifications(request.arguments)); + }, + _a[CommandNames.Cleanup] = function (request) { + _this.cleanup(); + return _this.requiredResponse(true); + }, + _a[CommandNames.SemanticDiagnosticsSync] = function (request) { + return _this.requiredResponse(_this.getSemanticDiagnosticsSync(request.arguments)); + }, + _a[CommandNames.SyntacticDiagnosticsSync] = function (request) { + return _this.requiredResponse(_this.getSyntacticDiagnosticsSync(request.arguments)); + }, + _a[CommandNames.Geterr] = function (request) { + var geterrArgs = request.arguments; + return { response: _this.getDiagnostics(geterrArgs.delay, geterrArgs.files), responseRequired: false }; + }, + _a[CommandNames.GeterrForProject] = function (request) { + var _a = request.arguments, file = _a.file, delay = _a.delay; + return { response: _this.getDiagnosticsForProject(delay, file), responseRequired: false }; + }, + _a[CommandNames.Change] = function (request) { + _this.change(request.arguments); + return _this.notRequired(); + }, + _a[CommandNames.Configure] = function (request) { + _this.projectService.setHostConfiguration(request.arguments); + _this.output(undefined, CommandNames.Configure, request.seq); + return _this.notRequired(); + }, + _a[CommandNames.Reload] = function (request) { + _this.reload(request.arguments, request.seq); + return _this.requiredResponse({ reloadFinished: true }); + }, + _a[CommandNames.Saveto] = function (request) { + var savetoArgs = request.arguments; + _this.saveToTmp(savetoArgs.file, savetoArgs.tmpfile); + return _this.notRequired(); + }, + _a[CommandNames.Close] = function (request) { + var closeArgs = request.arguments; + _this.closeClientFile(closeArgs.file); + return _this.notRequired(); + }, + _a[CommandNames.Navto] = function (request) { + return _this.requiredResponse(_this.getNavigateToItems(request.arguments, true)); + }, + _a[CommandNames.NavtoFull] = function (request) { + return _this.requiredResponse(_this.getNavigateToItems(request.arguments, false)); + }, + _a[CommandNames.Brace] = function (request) { + return _this.requiredResponse(_this.getBraceMatching(request.arguments, true)); + }, + _a[CommandNames.BraceFull] = function (request) { + return _this.requiredResponse(_this.getBraceMatching(request.arguments, false)); + }, + _a[CommandNames.NavBar] = function (request) { + return _this.requiredResponse(_this.getNavigationBarItems(request.arguments, true)); + }, + _a[CommandNames.NavBarFull] = function (request) { + return _this.requiredResponse(_this.getNavigationBarItems(request.arguments, false)); + }, + _a[CommandNames.NavTree] = function (request) { + return _this.requiredResponse(_this.getNavigationTree(request.arguments, true)); + }, + _a[CommandNames.NavTreeFull] = function (request) { + return _this.requiredResponse(_this.getNavigationTree(request.arguments, false)); + }, + _a[CommandNames.Occurrences] = function (request) { + return _this.requiredResponse(_this.getOccurrences(request.arguments)); + }, + _a[CommandNames.DocumentHighlights] = function (request) { + return _this.requiredResponse(_this.getDocumentHighlights(request.arguments, true)); + }, + _a[CommandNames.DocumentHighlightsFull] = function (request) { + return _this.requiredResponse(_this.getDocumentHighlights(request.arguments, false)); + }, + _a[CommandNames.CompilerOptionsForInferredProjects] = function (request) { + return _this.requiredResponse(_this.setCompilerOptionsForInferredProjects(request.arguments)); + }, + _a[CommandNames.ProjectInfo] = function (request) { + return _this.requiredResponse(_this.getProjectInfo(request.arguments)); + }, + _a[CommandNames.ReloadProjects] = function (request) { + _this.projectService.reloadProjects(); + return _this.notRequired(); + }, + _a[CommandNames.GetCodeFixes] = function (request) { + return _this.requiredResponse(_this.getCodeFixes(request.arguments, true)); + }, + _a[CommandNames.GetCodeFixesFull] = function (request) { + return _this.requiredResponse(_this.getCodeFixes(request.arguments, false)); + }, + _a[CommandNames.GetSupportedCodeFixes] = function (request) { + return _this.requiredResponse(_this.getSupportedCodeFixes()); + }, + _a)); + this.eventHander = canUseEvents + ? eventHandler || (function (event) { return _this.defaultEventHandler(event); }) + : undefined; + this.projectService = new server.ProjectService(host, logger, cancellationToken, useSingleInferredProject, typingsInstaller, this.eventHander); + this.gcTimer = new server.GcTimer(host, 7000, logger); + var _a; + } + Session.prototype.defaultEventHandler = function (event) { + var _this = this; + switch (event.eventName) { + case "context": + var _a = event.data, project = _a.project, fileName = _a.fileName; + this.projectService.logger.info("got context event, updating diagnostics for " + fileName); + this.updateErrorCheck([{ fileName: fileName, project: project }], this.changeSeq, function (n) { return n === _this.changeSeq; }, 100); + break; + case "configFileDiag": + var _b = event.data, triggerFile = _b.triggerFile, configFileName = _b.configFileName, diagnostics = _b.diagnostics; + this.configFileDiagnosticEvent(triggerFile, configFileName, diagnostics); } - this.languageServiceDiabled = false; }; - Project.prototype.disableLanguageService = function () { - this.languageServiceDiabled = true; + Session.prototype.logError = function (err, cmd) { + var msg = "Exception on executing command " + cmd; + if (err.message) { + msg += ":\n" + err.message; + if (err.stack) { + msg += "\n" + err.stack; + } + } + this.logger.msg(msg, server.Msg.Err); }; - Project.prototype.addOpenRef = function () { - this.openRefCount++; + Session.prototype.send = function (msg) { + if (msg.type === "event" && !this.canUseEvents) { + if (this.logger.hasLevel(server.LogLevel.verbose)) { + this.logger.info("Session does not support events: ignored event: " + JSON.stringify(msg)); + } + return; + } + this.host.write(formatMessage(msg, this.logger, this.byteLength, this.host.newLine)); }; - Project.prototype.deleteOpenRef = function () { - this.openRefCount--; - return this.openRefCount; + Session.prototype.configFileDiagnosticEvent = function (triggerFile, configFile, diagnostics) { + var bakedDiags = ts.map(diagnostics, formatConfigFileDiag); + var ev = { + seq: 0, + type: "event", + event: "configFileDiag", + body: { + triggerFile: triggerFile, + configFile: configFile, + diagnostics: bakedDiags + } + }; + this.send(ev); }; - Project.prototype.openReferencedFile = function (filename) { - return this.projectService.openFile(filename, false); + Session.prototype.event = function (info, eventName) { + var ev = { + seq: 0, + type: "event", + event: eventName, + body: info + }; + this.send(ev); }; - Project.prototype.getRootFiles = function () { - if (this.languageServiceDiabled) { - return this.projectOptions ? this.projectOptions.files : undefined; + Session.prototype.output = function (info, cmdName, reqSeq, errorMsg) { + if (reqSeq === void 0) { reqSeq = 0; } + var res = { + seq: 0, + type: "response", + command: cmdName, + request_seq: reqSeq, + success: !errorMsg + }; + if (!errorMsg) { + res.body = info; + } + else { + res.message = errorMsg; } - return this.compilerService.host.roots.map(function (info) { return info.fileName; }); + this.send(res); }; - Project.prototype.getFileNames = function () { - if (this.languageServiceDiabled) { - if (!this.projectOptions) { - return undefined; - } - var fileNames = []; - if (this.projectOptions && this.projectOptions.compilerOptions) { - fileNames.push(ts.getDefaultLibFilePath(this.projectOptions.compilerOptions)); + Session.prototype.semanticCheck = function (file, project) { + try { + var diags = []; + if (!shouldSkipSematicCheck(project)) { + diags = project.getLanguageService().getSemanticDiagnostics(file); } - ts.addRange(fileNames, this.projectOptions.files); - return fileNames; + var bakedDiags = diags.map(function (diag) { return formatDiag(file, project, diag); }); + this.event({ file: file, diagnostics: bakedDiags }, "semanticDiag"); } - var sourceFiles = this.program.getSourceFiles(); - return sourceFiles.map(function (sourceFile) { return sourceFile.fileName; }); - }; - Project.prototype.getSourceFile = function (info) { - if (this.languageServiceDiabled) { - return undefined; + catch (err) { + this.logError(err, "semantic check"); } - return this.filenameToSourceFile[info.fileName]; }; - Project.prototype.getSourceFileFromName = function (filename, requireOpen) { - if (this.languageServiceDiabled) { - return undefined; - } - var info = this.projectService.getScriptInfo(filename); - if (info) { - if ((!requireOpen) || info.isOpen) { - return this.getSourceFile(info); + Session.prototype.syntacticCheck = function (file, project) { + try { + var diags = project.getLanguageService().getSyntacticDiagnostics(file); + if (diags) { + var bakedDiags = diags.map(function (diag) { return formatDiag(file, project, diag); }); + this.event({ file: file, diagnostics: bakedDiags }, "syntaxDiag"); } } - }; - Project.prototype.isRoot = function (info) { - if (this.languageServiceDiabled) { - return undefined; + catch (err) { + this.logError(err, "syntactic check"); } - return this.compilerService.host.roots.some(function (root) { return root === info; }); }; - Project.prototype.removeReferencedFile = function (info) { - if (this.languageServiceDiabled) { - return; - } - this.compilerService.host.removeReferencedFile(info); - this.updateGraph(); + Session.prototype.updateProjectStructure = function (seq, matchSeq, ms) { + var _this = this; + if (ms === void 0) { ms = 1500; } + this.host.setTimeout(function () { + if (matchSeq(seq)) { + _this.projectService.refreshInferredProjects(); + } + }, ms); }; - Project.prototype.updateFileMap = function () { - if (this.languageServiceDiabled) { - return; + Session.prototype.updateErrorCheck = function (checkList, seq, matchSeq, ms, followMs, requireOpen) { + var _this = this; + if (ms === void 0) { ms = 1500; } + if (followMs === void 0) { followMs = 200; } + if (requireOpen === void 0) { requireOpen = true; } + if (followMs > ms) { + followMs = ms; } - this.filenameToSourceFile = ts.createMap(); - var sourceFiles = this.program.getSourceFiles(); - for (var i = 0, len = sourceFiles.length; i < len; i++) { - var normFilename = ts.normalizePath(sourceFiles[i].fileName); - this.filenameToSourceFile[normFilename] = sourceFiles[i]; + if (this.errorTimer) { + this.host.clearTimeout(this.errorTimer); } - }; - Project.prototype.finishGraph = function () { - if (this.languageServiceDiabled) { - return; + if (this.immediateId) { + this.host.clearImmediate(this.immediateId); + this.immediateId = undefined; } - this.updateGraph(); - this.compilerService.languageService.getNavigateToItems(".*"); - }; - Project.prototype.updateGraph = function () { - if (this.languageServiceDiabled) { - return; + var index = 0; + var checkOne = function () { + if (matchSeq(seq)) { + var checkSpec_1 = checkList[index]; + index++; + if (checkSpec_1.project.containsFile(checkSpec_1.fileName, requireOpen)) { + _this.syntacticCheck(checkSpec_1.fileName, checkSpec_1.project); + _this.immediateId = _this.host.setImmediate(function () { + _this.semanticCheck(checkSpec_1.fileName, checkSpec_1.project); + _this.immediateId = undefined; + if (checkList.length > index) { + _this.errorTimer = _this.host.setTimeout(checkOne, followMs); + } + else { + _this.errorTimer = undefined; + } + }); + } + } + }; + if ((checkList.length > index) && (matchSeq(seq))) { + this.errorTimer = this.host.setTimeout(checkOne, ms); } - this.program = this.compilerService.languageService.getProgram(); - this.updateFileMap(); - }; - Project.prototype.isConfiguredProject = function () { - return this.projectFilename; }; - Project.prototype.addRoot = function (info) { - if (this.languageServiceDiabled) { + Session.prototype.cleanProjects = function (caption, projects) { + if (!projects) { return; } - this.compilerService.host.addRoot(info); - }; - Project.prototype.removeRoot = function (info) { - if (this.languageServiceDiabled) { - return; + this.logger.info("cleaning " + caption); + for (var _i = 0, projects_4 = projects; _i < projects_4.length; _i++) { + var p = projects_4[_i]; + p.getLanguageService(false).cleanupSemanticCache(); } - this.compilerService.host.removeRoot(info); }; - Project.prototype.filesToString = function () { - if (this.languageServiceDiabled) { - if (this.projectOptions) { - var strBuilder_1 = ""; - ts.forEach(this.projectOptions.files, function (file) { strBuilder_1 += file + "\n"; }); - return strBuilder_1; - } + Session.prototype.cleanup = function () { + this.cleanProjects("inferred projects", this.projectService.inferredProjects); + this.cleanProjects("configured projects", this.projectService.configuredProjects); + this.cleanProjects("external projects", this.projectService.externalProjects); + if (this.host.gc) { + this.logger.info("host.gc()"); + this.host.gc(); } - var strBuilder = ""; - ts.forEachProperty(this.filenameToSourceFile, function (sourceFile) { strBuilder += sourceFile.fileName + "\n"; }); - return strBuilder; }; - Project.prototype.setProjectOptions = function (projectOptions) { - this.projectOptions = projectOptions; - if (projectOptions.compilerOptions) { - projectOptions.compilerOptions.allowNonTsExtensions = true; - if (!this.languageServiceDiabled) { - this.compilerService.setCompilerOptions(projectOptions.compilerOptions); - } - } + Session.prototype.getEncodedSemanticClassifications = function (args) { + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + return project.getLanguageService().getEncodedSemanticClassifications(file, args); }; - return Project; - }()); - server.Project = Project; - function combineProjectOutput(projects, action, comparer, areEqual) { - var result = projects.reduce(function (previous, current) { return ts.concatenate(previous, action(current)); }, []).sort(comparer); - return projects.length > 1 ? ts.deduplicate(result, areEqual) : result; - } - server.combineProjectOutput = combineProjectOutput; - var ProjectService = (function () { - function ProjectService(host, psLogger, eventHandler) { - this.host = host; - this.psLogger = psLogger; - this.eventHandler = eventHandler; - this.filenameToScriptInfo = ts.createMap(); - this.openFileRoots = []; - this.inferredProjects = []; - this.configuredProjects = []; - this.openFilesReferenced = []; - this.openFileRootsConfigured = []; - this.directoryWatchersForTsconfig = ts.createMap(); - this.directoryWatchersRefCount = ts.createMap(); - this.timerForDetectingProjectFileListChanges = ts.createMap(); - this.addDefaultHostConfiguration(); - } - ProjectService.prototype.addDefaultHostConfiguration = function () { - this.hostConfiguration = { - formatCodeOptions: ts.clone(CompilerService.getDefaultFormatCodeOptions(this.host)), - hostInfo: "Unknown host" - }; + Session.prototype.getProject = function (projectFileName) { + return projectFileName && this.projectService.findProject(projectFileName); }; - ProjectService.prototype.getFormatCodeOptions = function (file) { - if (file) { - var info = this.filenameToScriptInfo[file]; - if (info) { - return info.formatCodeOptions; - } + Session.prototype.getCompilerOptionsDiagnostics = function (args) { + var project = this.getProject(args.projectFileName); + return this.convertToDiagnosticsWithLinePosition(project.getLanguageService().getCompilerOptionsDiagnostics(), undefined); + }; + Session.prototype.convertToDiagnosticsWithLinePosition = function (diagnostics, scriptInfo) { + var _this = this; + return diagnostics.map(function (d) { return ({ + message: ts.flattenDiagnosticMessageText(d.messageText, _this.host.newLine), + start: d.start, + length: d.length, + category: ts.DiagnosticCategory[d.category].toLowerCase(), + code: d.code, + startLocation: scriptInfo && scriptInfo.positionToLineOffset(d.start), + endLocation: scriptInfo && scriptInfo.positionToLineOffset(d.start + d.length) + }); }); + }; + Session.prototype.getDiagnosticsWorker = function (args, selector, includeLinePosition) { + var _a = this.getFileAndProject(args), project = _a.project, file = _a.file; + if (shouldSkipSematicCheck(project)) { + return []; } - return this.hostConfiguration.formatCodeOptions; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var diagnostics = selector(project, file); + return includeLinePosition + ? this.convertToDiagnosticsWithLinePosition(diagnostics, scriptInfo) + : diagnostics.map(function (d) { return formatDiag(file, project, d); }); }; - ProjectService.prototype.watchedFileChanged = function (fileName) { - var info = this.filenameToScriptInfo[fileName]; - if (!info) { - this.psLogger.info("Error: got watch notification for unknown file: " + fileName); + Session.prototype.getDefinition = function (args, simplifiedResult) { + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var position = this.getPosition(args, scriptInfo); + var definitions = project.getLanguageService().getDefinitionAtPosition(file, position); + if (!definitions) { + return undefined; } - if (!this.host.fileExists(fileName)) { - this.fileDeletedInFilesystem(info); + if (simplifiedResult) { + return definitions.map(function (def) { + var defScriptInfo = project.getScriptInfo(def.fileName); + return { + file: def.fileName, + start: defScriptInfo.positionToLineOffset(def.textSpan.start), + end: defScriptInfo.positionToLineOffset(ts.textSpanEnd(def.textSpan)) + }; + }); } else { - if (info && (!info.isOpen)) { - info.svc.reloadFromFile(info.fileName); - } + return definitions; } }; - ProjectService.prototype.directoryWatchedForSourceFilesChanged = function (project, fileName) { - if (fileName && !ts.isSupportedSourceFileName(fileName, project.projectOptions ? project.projectOptions.compilerOptions : undefined)) { - return; + Session.prototype.getTypeDefinition = function (args) { + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var position = this.getPosition(args, scriptInfo); + var definitions = project.getLanguageService().getTypeDefinitionAtPosition(file, position); + if (!definitions) { + return undefined; } - this.log("Detected source file changes: " + fileName); - this.startTimerForDetectingProjectFileListChanges(project); + return definitions.map(function (def) { + var defScriptInfo = project.getScriptInfo(def.fileName); + return { + file: def.fileName, + start: defScriptInfo.positionToLineOffset(def.textSpan.start), + end: defScriptInfo.positionToLineOffset(ts.textSpanEnd(def.textSpan)) + }; + }); }; - ProjectService.prototype.startTimerForDetectingProjectFileListChanges = function (project) { - var _this = this; - if (this.timerForDetectingProjectFileListChanges[project.projectFilename]) { - this.host.clearTimeout(this.timerForDetectingProjectFileListChanges[project.projectFilename]); + Session.prototype.getImplementation = function (args, simplifiedResult) { + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var position = this.getPosition(args, scriptInfo); + var implementations = project.getLanguageService().getImplementationAtPosition(file, position); + if (!implementations) { + return []; } - this.timerForDetectingProjectFileListChanges[project.projectFilename] = this.host.setTimeout(function () { return _this.handleProjectFileListChanges(project); }, 250); - }; - ProjectService.prototype.handleProjectFileListChanges = function (project) { - var _this = this; - var _a = this.configFileToProjectOptions(project.projectFilename), projectOptions = _a.projectOptions, errors = _a.errors; - this.reportConfigFileDiagnostics(project.projectFilename, errors); - var newRootFiles = projectOptions.files.map((function (f) { return _this.getCanonicalFileName(f); })); - var currentRootFiles = project.getRootFiles().map((function (f) { return _this.getCanonicalFileName(f); })); - if (!ts.arrayIsEqualTo(currentRootFiles && currentRootFiles.sort(), newRootFiles && newRootFiles.sort())) { - this.updateConfiguredProject(project); - this.updateProjectStructure(); + if (simplifiedResult) { + return implementations.map(function (impl) { return ({ + file: impl.fileName, + start: scriptInfo.positionToLineOffset(impl.textSpan.start), + end: scriptInfo.positionToLineOffset(ts.textSpanEnd(impl.textSpan)) + }); }); + } + else { + return implementations; } }; - ProjectService.prototype.reportConfigFileDiagnostics = function (configFileName, diagnostics, triggerFile) { - if (diagnostics && diagnostics.length > 0) { - this.eventHandler({ - eventName: "configFileDiag", - data: { configFileName: configFileName, diagnostics: diagnostics, triggerFile: triggerFile } - }); + Session.prototype.getOccurrences = function (args) { + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var position = this.getPosition(args, scriptInfo); + var occurrences = project.getLanguageService().getOccurrencesAtPosition(file, position); + if (!occurrences) { + return undefined; } + return occurrences.map(function (occurrence) { + var fileName = occurrence.fileName, isWriteAccess = occurrence.isWriteAccess, textSpan = occurrence.textSpan; + var scriptInfo = project.getScriptInfo(fileName); + var start = scriptInfo.positionToLineOffset(textSpan.start); + var end = scriptInfo.positionToLineOffset(ts.textSpanEnd(textSpan)); + return { + start: start, + end: end, + file: fileName, + isWriteAccess: isWriteAccess + }; + }); }; - ProjectService.prototype.directoryWatchedForTsconfigChanged = function (fileName) { - var _this = this; - if (ts.getBaseFileName(fileName) !== "tsconfig.json") { - this.log(fileName + " is not tsconfig.json"); - return; + Session.prototype.getSyntacticDiagnosticsSync = function (args) { + return this.getDiagnosticsWorker(args, function (project, file) { return project.getLanguageService().getSyntacticDiagnostics(file); }, args.includeLinePosition); + }; + Session.prototype.getSemanticDiagnosticsSync = function (args) { + return this.getDiagnosticsWorker(args, function (project, file) { return project.getLanguageService().getSemanticDiagnostics(file); }, args.includeLinePosition); + }; + Session.prototype.getDocumentHighlights = function (args, simplifiedResult) { + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var position = this.getPosition(args, scriptInfo); + var documentHighlights = project.getLanguageService().getDocumentHighlights(file, position, args.filesToSearch); + if (!documentHighlights) { + return undefined; } - this.log("Detected newly added tsconfig file: " + fileName); - var _a = this.configFileToProjectOptions(fileName), projectOptions = _a.projectOptions, errors = _a.errors; - this.reportConfigFileDiagnostics(fileName, errors); - if (!projectOptions) { - return; + if (simplifiedResult) { + return documentHighlights.map(convertToDocumentHighlightsItem); } - var rootFilesInTsconfig = projectOptions.files.map(function (f) { return _this.getCanonicalFileName(f); }); - var openFileRoots = this.openFileRoots.map(function (s) { return _this.getCanonicalFileName(s.fileName); }); - for (var _i = 0, openFileRoots_1 = openFileRoots; _i < openFileRoots_1.length; _i++) { - var openFileRoot = openFileRoots_1[_i]; - if (rootFilesInTsconfig.indexOf(openFileRoot) >= 0) { - this.reloadProjects(); - return; + else { + return documentHighlights; + } + function convertToDocumentHighlightsItem(documentHighlights) { + var fileName = documentHighlights.fileName, highlightSpans = documentHighlights.highlightSpans; + var scriptInfo = project.getScriptInfo(fileName); + return { + file: fileName, + highlightSpans: highlightSpans.map(convertHighlightSpan) + }; + function convertHighlightSpan(highlightSpan) { + var textSpan = highlightSpan.textSpan, kind = highlightSpan.kind; + var start = scriptInfo.positionToLineOffset(textSpan.start); + var end = scriptInfo.positionToLineOffset(ts.textSpanEnd(textSpan)); + return { start: start, end: end, kind: kind }; } } }; - ProjectService.prototype.getCanonicalFileName = function (fileName) { - var name = this.host.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase(); - return ts.normalizePath(name); + Session.prototype.setCompilerOptionsForInferredProjects = function (args) { + this.projectService.setCompilerOptionsForInferredProjects(args.options); }; - ProjectService.prototype.watchedProjectConfigFileChanged = function (project) { - this.log("Config file changed: " + project.projectFilename); - var configFileErrors = this.updateConfiguredProject(project); - this.updateProjectStructure(); - if (configFileErrors && configFileErrors.length > 0) { - this.eventHandler({ eventName: "configFileDiag", data: { triggerFile: project.projectFilename, configFileName: project.projectFilename, diagnostics: configFileErrors } }); - } + Session.prototype.getProjectInfo = function (args) { + return this.getProjectInfoWorker(args.file, args.projectFileName, args.needFileNameList); }; - ProjectService.prototype.log = function (msg, type) { - if (type === void 0) { type = "Err"; } - this.psLogger.msg(msg, type); + Session.prototype.getProjectInfoWorker = function (uncheckedFileName, projectFileName, needFileNameList) { + var project = this.getFileAndProjectWorker(uncheckedFileName, projectFileName, true, true).project; + var projectInfo = { + configFileName: project.getProjectName(), + languageServiceDisabled: !project.languageServiceEnabled, + fileNames: needFileNameList ? project.getFileNames() : undefined + }; + return projectInfo; }; - ProjectService.prototype.setHostConfiguration = function (args) { - if (args.file) { - var info = this.filenameToScriptInfo[args.file]; - if (info) { - info.setFormatOptions(args.formatOptions); - this.log("Host configuration update for file " + args.file, "Info"); + Session.prototype.getRenameInfo = function (args) { + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var position = this.getPosition(args, scriptInfo); + return project.getLanguageService().getRenameInfo(file, position); + }; + Session.prototype.getProjects = function (args) { + var projects; + if (args.projectFileName) { + var project = this.getProject(args.projectFileName); + if (project) { + projects = [project]; } } else { - if (args.hostInfo !== undefined) { - this.hostConfiguration.hostInfo = args.hostInfo; - this.log("Host information " + args.hostInfo, "Info"); - } - if (args.formatOptions) { - mergeFormatOptions(this.hostConfiguration.formatCodeOptions, args.formatOptions); - this.log("Format host information updated", "Info"); - } + var scriptInfo = this.projectService.getScriptInfo(args.file); + projects = scriptInfo.containingProjects; } + projects = ts.filter(projects, function (p) { return p.languageServiceEnabled; }); + if (!projects || !projects.length) { + return server.Errors.ThrowNoProject(); + } + return projects; }; - ProjectService.prototype.closeLog = function () { - this.psLogger.close(); - }; - ProjectService.prototype.createInferredProject = function (root) { - var _this = this; - var project = new Project(this); - project.addRoot(root); - var currentPath = ts.getDirectoryPath(root.fileName); - var parentPath = ts.getDirectoryPath(currentPath); - while (currentPath != parentPath) { - if (!project.projectService.directoryWatchersForTsconfig[currentPath]) { - this.log("Add watcher for: " + currentPath); - project.projectService.directoryWatchersForTsconfig[currentPath] = - this.host.watchDirectory(currentPath, function (fileName) { return _this.directoryWatchedForTsconfigChanged(fileName); }); - project.projectService.directoryWatchersRefCount[currentPath] = 1; + Session.prototype.getRenameLocations = function (args, simplifiedResult) { + var file = server.toNormalizedPath(args.file); + var info = this.projectService.getScriptInfoForNormalizedPath(file); + var position = this.getPosition(args, info); + var projects = this.getProjects(args); + if (simplifiedResult) { + var defaultProject = projects[0]; + var renameInfo = defaultProject.getLanguageService().getRenameInfo(file, position); + if (!renameInfo) { + return undefined; } - else { - project.projectService.directoryWatchersRefCount[currentPath] += 1; + if (!renameInfo.canRename) { + return { + info: renameInfo, + locs: [] + }; } - project.directoriesWatchedForTsconfig.push(currentPath); - currentPath = parentPath; - parentPath = ts.getDirectoryPath(parentPath); + var fileSpans = server.combineProjectOutput(projects, function (project) { + var renameLocations = project.getLanguageService().findRenameLocations(file, position, args.findInStrings, args.findInComments); + if (!renameLocations) { + return []; + } + return renameLocations.map(function (location) { + var locationScriptInfo = project.getScriptInfo(location.fileName); + return { + file: location.fileName, + start: locationScriptInfo.positionToLineOffset(location.textSpan.start), + end: locationScriptInfo.positionToLineOffset(ts.textSpanEnd(location.textSpan)) + }; + }); + }, compareRenameLocation, function (a, b) { return a.file === b.file && a.start.line === b.start.line && a.start.offset === b.start.offset; }); + var locs = fileSpans.reduce(function (accum, cur) { + var curFileAccum; + if (accum.length > 0) { + curFileAccum = accum[accum.length - 1]; + if (curFileAccum.file !== cur.file) { + curFileAccum = undefined; + } + } + if (!curFileAccum) { + curFileAccum = { file: cur.file, locs: [] }; + accum.push(curFileAccum); + } + curFileAccum.locs.push({ start: cur.start, end: cur.end }); + return accum; + }, []); + return { info: renameInfo, locs: locs }; } - project.finishGraph(); - this.inferredProjects.push(project); - return project; - }; - ProjectService.prototype.fileDeletedInFilesystem = function (info) { - this.psLogger.info(info.fileName + " deleted"); - if (info.fileWatcher) { - info.fileWatcher.close(); - info.fileWatcher = undefined; + else { + return server.combineProjectOutput(projects, function (p) { return p.getLanguageService().findRenameLocations(file, position, args.findInStrings, args.findInComments); }, undefined, renameLocationIsEqualTo); } - if (!info.isOpen) { - this.filenameToScriptInfo[info.fileName] = undefined; - var referencingProjects = this.findReferencingProjects(info); - if (info.defaultProject) { - info.defaultProject.removeRoot(info); + function renameLocationIsEqualTo(a, b) { + if (a === b) { + return true; } - for (var i = 0, len = referencingProjects.length; i < len; i++) { - referencingProjects[i].removeReferencedFile(info); + if (!a || !b) { + return false; } - for (var j = 0, flen = this.openFileRoots.length; j < flen; j++) { - var openFile = this.openFileRoots[j]; - if (this.eventHandler) { - this.eventHandler({ eventName: "context", data: { project: openFile.defaultProject, fileName: openFile.fileName } }); - } + return a.fileName === b.fileName && + a.textSpan.start === b.textSpan.start && + a.textSpan.length === b.textSpan.length; + } + function compareRenameLocation(a, b) { + if (a.file < b.file) { + return -1; + } + else if (a.file > b.file) { + return 1; } - for (var j = 0, flen = this.openFilesReferenced.length; j < flen; j++) { - var openFile = this.openFilesReferenced[j]; - if (this.eventHandler) { - this.eventHandler({ eventName: "context", data: { project: openFile.defaultProject, fileName: openFile.fileName } }); + else { + if (a.start.line < b.start.line) { + return 1; + } + else if (a.start.line > b.start.line) { + return -1; + } + else { + return b.start.offset - a.start.offset; } } } - this.printProjects(); }; - ProjectService.prototype.updateConfiguredProjectList = function () { - var configuredProjects = []; - for (var i = 0, len = this.configuredProjects.length; i < len; i++) { - if (this.configuredProjects[i].openRefCount > 0) { - configuredProjects.push(this.configuredProjects[i]); + Session.prototype.getReferences = function (args, simplifiedResult) { + var file = server.toNormalizedPath(args.file); + var projects = this.getProjects(args); + var defaultProject = projects[0]; + var scriptInfo = defaultProject.getScriptInfoForNormalizedPath(file); + var position = this.getPosition(args, scriptInfo); + if (simplifiedResult) { + var nameInfo = defaultProject.getLanguageService().getQuickInfoAtPosition(file, position); + if (!nameInfo) { + return undefined; } - } - this.configuredProjects = configuredProjects; - }; - ProjectService.prototype.removeProject = function (project) { - this.log("remove project: " + project.getRootFiles().toString()); - if (project.isConfiguredProject()) { - project.projectFileWatcher.close(); - project.directoryWatcher.close(); - ts.forEachProperty(project.directoriesWatchedForWildcards, function (watcher) { watcher.close(); }); - delete project.directoriesWatchedForWildcards; - ts.unorderedRemoveItem(this.configuredProjects, project); + var displayString = ts.displayPartsToString(nameInfo.displayParts); + var nameSpan = nameInfo.textSpan; + var nameColStart = scriptInfo.positionToLineOffset(nameSpan.start).offset; + var nameText = scriptInfo.snap().getText(nameSpan.start, ts.textSpanEnd(nameSpan)); + var refs = server.combineProjectOutput(projects, function (project) { + var references = project.getLanguageService().getReferencesAtPosition(file, position); + if (!references) { + return []; + } + return references.map(function (ref) { + var refScriptInfo = project.getScriptInfo(ref.fileName); + var start = refScriptInfo.positionToLineOffset(ref.textSpan.start); + var refLineSpan = refScriptInfo.lineToTextSpan(start.line - 1); + var lineText = refScriptInfo.snap().getText(refLineSpan.start, ts.textSpanEnd(refLineSpan)).replace(/\r|\n/g, ""); + return { + file: ref.fileName, + start: start, + lineText: lineText, + end: refScriptInfo.positionToLineOffset(ts.textSpanEnd(ref.textSpan)), + isWriteAccess: ref.isWriteAccess, + isDefinition: ref.isDefinition + }; + }); + }, compareFileStart, areReferencesResponseItemsForTheSameLocation); + return { + refs: refs, + symbolName: nameText, + symbolStartOffset: nameColStart, + symbolDisplayString: displayString + }; } else { - for (var _i = 0, _a = project.directoriesWatchedForTsconfig; _i < _a.length; _i++) { - var directory = _a[_i]; - project.projectService.directoryWatchersRefCount[directory]--; - if (!project.projectService.directoryWatchersRefCount[directory]) { - this.log("Close directory watcher for: " + directory); - project.projectService.directoryWatchersForTsconfig[directory].close(); - delete project.projectService.directoryWatchersForTsconfig[directory]; - } - } - ts.unorderedRemoveItem(this.inferredProjects, project); + return server.combineProjectOutput(projects, function (project) { return project.getLanguageService().findReferences(file, position); }, undefined, undefined); } - var fileNames = project.getFileNames(); - for (var _b = 0, fileNames_3 = fileNames; _b < fileNames_3.length; _b++) { - var fileName = fileNames_3[_b]; - var info = this.getScriptInfo(fileName); - if (info.defaultProject == project) { - info.defaultProject = undefined; + function areReferencesResponseItemsForTheSameLocation(a, b) { + if (a && b) { + return a.file === b.file && + a.start === b.start && + a.end === b.end; } + return false; } }; - ProjectService.prototype.setConfiguredProjectRoot = function (info) { - for (var i = 0, len = this.configuredProjects.length; i < len; i++) { - var configuredProject = this.configuredProjects[i]; - if (configuredProject.isRoot(info)) { - info.defaultProject = configuredProject; - configuredProject.addOpenRef(); - return true; - } + Session.prototype.openClientFile = function (fileName, fileContent, scriptKind) { + var _a = this.projectService.openClientFileWithNormalizedPath(fileName, fileContent, scriptKind), configFileName = _a.configFileName, configFileErrors = _a.configFileErrors; + if (this.eventHander) { + this.eventHander({ + eventName: "configFileDiag", + data: { fileName: fileName, configFileName: configFileName, diagnostics: configFileErrors || [] } + }); } - return false; }; - ProjectService.prototype.addOpenFile = function (info) { - if (this.setConfiguredProjectRoot(info)) { - this.openFileRootsConfigured.push(info); + Session.prototype.getPosition = function (args, scriptInfo) { + return args.position !== undefined ? args.position : scriptInfo.lineOffsetToPosition(args.line, args.offset); + }; + Session.prototype.getFileAndProject = function (args, errorOnMissingProject) { + if (errorOnMissingProject === void 0) { errorOnMissingProject = true; } + return this.getFileAndProjectWorker(args.file, args.projectFileName, true, errorOnMissingProject); + }; + Session.prototype.getFileAndProjectWithoutRefreshingInferredProjects = function (args, errorOnMissingProject) { + if (errorOnMissingProject === void 0) { errorOnMissingProject = true; } + return this.getFileAndProjectWorker(args.file, args.projectFileName, false, errorOnMissingProject); + }; + Session.prototype.getFileAndProjectWorker = function (uncheckedFileName, projectFileName, refreshInferredProjects, errorOnMissingProject) { + var file = server.toNormalizedPath(uncheckedFileName); + var project = this.getProject(projectFileName) || this.projectService.getDefaultProjectForFile(file, refreshInferredProjects); + if (!project && errorOnMissingProject) { + return server.Errors.ThrowNoProject(); + } + return { file: file, project: project }; + }; + Session.prototype.getOutliningSpans = function (args) { + var _a = this.getFileAndProjectWithoutRefreshingInferredProjects(args), file = _a.file, project = _a.project; + return project.getLanguageService(false).getOutliningSpans(file); + }; + Session.prototype.getTodoComments = function (args) { + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + return project.getLanguageService().getTodoComments(file, args.descriptors); + }; + Session.prototype.getDocCommentTemplate = function (args) { + var _a = this.getFileAndProjectWithoutRefreshingInferredProjects(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var position = this.getPosition(args, scriptInfo); + return project.getLanguageService(false).getDocCommentTemplateAtPosition(file, position); + }; + Session.prototype.getIndentation = function (args) { + var _a = this.getFileAndProjectWithoutRefreshingInferredProjects(args), file = _a.file, project = _a.project; + var position = this.getPosition(args, project.getScriptInfoForNormalizedPath(file)); + var options = args.options || this.projectService.getFormatCodeOptions(file); + var indentation = project.getLanguageService(false).getIndentationAtPosition(file, position, options); + return { position: position, indentation: indentation }; + }; + Session.prototype.getBreakpointStatement = function (args) { + var _a = this.getFileAndProjectWithoutRefreshingInferredProjects(args), file = _a.file, project = _a.project; + var position = this.getPosition(args, project.getScriptInfoForNormalizedPath(file)); + return project.getLanguageService(false).getBreakpointStatementAtPosition(file, position); + }; + Session.prototype.getNameOrDottedNameSpan = function (args) { + var _a = this.getFileAndProjectWithoutRefreshingInferredProjects(args), file = _a.file, project = _a.project; + var position = this.getPosition(args, project.getScriptInfoForNormalizedPath(file)); + return project.getLanguageService(false).getNameOrDottedNameSpan(file, position, position); + }; + Session.prototype.isValidBraceCompletion = function (args) { + var _a = this.getFileAndProjectWithoutRefreshingInferredProjects(args), file = _a.file, project = _a.project; + var position = this.getPosition(args, project.getScriptInfoForNormalizedPath(file)); + return project.getLanguageService(false).isValidBraceCompletionAtPosition(file, position, args.openingBrace.charCodeAt(0)); + }; + Session.prototype.getQuickInfoWorker = function (args, simplifiedResult) { + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var quickInfo = project.getLanguageService().getQuickInfoAtPosition(file, this.getPosition(args, scriptInfo)); + if (!quickInfo) { + return undefined; + } + if (simplifiedResult) { + var displayString = ts.displayPartsToString(quickInfo.displayParts); + var docString = ts.displayPartsToString(quickInfo.documentation); + return { + kind: quickInfo.kind, + kindModifiers: quickInfo.kindModifiers, + start: scriptInfo.positionToLineOffset(quickInfo.textSpan.start), + end: scriptInfo.positionToLineOffset(ts.textSpanEnd(quickInfo.textSpan)), + displayString: displayString, + documentation: docString + }; } else { - this.findReferencingProjects(info); - if (info.defaultProject) { - info.defaultProject.addOpenRef(); - this.openFilesReferenced.push(info); - } - else { - info.defaultProject = this.createInferredProject(info); - var openFileRoots = []; - for (var i = 0, len = this.openFileRoots.length; i < len; i++) { - var r = this.openFileRoots[i]; - if (info.defaultProject.getSourceFile(r)) { - this.removeProject(r.defaultProject); - this.openFilesReferenced.push(r); - r.defaultProject = info.defaultProject; - } - else { - openFileRoots.push(r); - } - } - this.openFileRoots = openFileRoots; - this.openFileRoots.push(info); - } + return quickInfo; } - this.updateConfiguredProjectList(); }; - ProjectService.prototype.closeOpenFile = function (info) { - info.svc.reloadFromFile(info.fileName); - var openFileRoots = []; - var removedProject; - for (var i = 0, len = this.openFileRoots.length; i < len; i++) { - if (info === this.openFileRoots[i]) { - removedProject = info.defaultProject; - } - else { - openFileRoots.push(this.openFileRoots[i]); - } + Session.prototype.getFormattingEditsForRange = function (args) { + var _this = this; + var _a = this.getFileAndProjectWithoutRefreshingInferredProjects(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var startPosition = scriptInfo.lineOffsetToPosition(args.line, args.offset); + var endPosition = scriptInfo.lineOffsetToPosition(args.endLine, args.endOffset); + var edits = project.getLanguageService(false).getFormattingEditsForRange(file, startPosition, endPosition, this.projectService.getFormatCodeOptions(file)); + if (!edits) { + return undefined; } - this.openFileRoots = openFileRoots; - if (!removedProject) { - var openFileRootsConfigured = []; - for (var i = 0, len = this.openFileRootsConfigured.length; i < len; i++) { - if (info === this.openFileRootsConfigured[i]) { - if (info.defaultProject.deleteOpenRef() === 0) { - removedProject = info.defaultProject; + return edits.map(function (edit) { return _this.convertTextChangeToCodeEdit(edit, scriptInfo); }); + }; + Session.prototype.getFormattingEditsForRangeFull = function (args) { + var _a = this.getFileAndProjectWithoutRefreshingInferredProjects(args), file = _a.file, project = _a.project; + var options = args.options || this.projectService.getFormatCodeOptions(file); + return project.getLanguageService(false).getFormattingEditsForRange(file, args.position, args.endPosition, options); + }; + Session.prototype.getFormattingEditsForDocumentFull = function (args) { + var _a = this.getFileAndProjectWithoutRefreshingInferredProjects(args), file = _a.file, project = _a.project; + var options = args.options || this.projectService.getFormatCodeOptions(file); + return project.getLanguageService(false).getFormattingEditsForDocument(file, options); + }; + Session.prototype.getFormattingEditsAfterKeystrokeFull = function (args) { + var _a = this.getFileAndProjectWithoutRefreshingInferredProjects(args), file = _a.file, project = _a.project; + var options = args.options || this.projectService.getFormatCodeOptions(file); + return project.getLanguageService(false).getFormattingEditsAfterKeystroke(file, args.position, args.key, options); + }; + Session.prototype.getFormattingEditsAfterKeystroke = function (args) { + var _a = this.getFileAndProjectWithoutRefreshingInferredProjects(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var position = scriptInfo.lineOffsetToPosition(args.line, args.offset); + var formatOptions = this.projectService.getFormatCodeOptions(file); + var edits = project.getLanguageService(false).getFormattingEditsAfterKeystroke(file, position, args.key, formatOptions); + if ((args.key == "\n") && ((!edits) || (edits.length === 0) || allEditsBeforePos(edits, position))) { + var lineInfo = scriptInfo.getLineInfo(args.line); + if (lineInfo && (lineInfo.leaf) && (lineInfo.leaf.text)) { + var lineText = lineInfo.leaf.text; + if (lineText.search("\\S") < 0) { + var preferredIndent = project.getLanguageService(false).getIndentationAtPosition(file, position, formatOptions); + var hasIndent = 0; + var i = void 0, len = void 0; + for (i = 0, len = lineText.length; i < len; i++) { + if (lineText.charAt(i) == " ") { + hasIndent++; + } + else if (lineText.charAt(i) == "\t") { + hasIndent += formatOptions.tabSize; + } + else { + break; + } + } + if (preferredIndent !== hasIndent) { + var firstNoWhiteSpacePosition = lineInfo.offset + i; + edits.push({ + span: ts.createTextSpanFromBounds(lineInfo.offset, firstNoWhiteSpacePosition), + newText: ts.formatting.getIndentationString(preferredIndent, formatOptions) + }); } - } - else { - openFileRootsConfigured.push(this.openFileRootsConfigured[i]); } } - this.openFileRootsConfigured = openFileRootsConfigured; } - if (removedProject) { - this.removeProject(removedProject); - var openFilesReferenced = []; - var orphanFiles = []; - for (var i = 0, len = this.openFilesReferenced.length; i < len; i++) { - var f = this.openFilesReferenced[i]; - if (f.defaultProject === removedProject || !f.defaultProject) { - f.defaultProject = undefined; - orphanFiles.push(f); - } - else { - openFilesReferenced.push(f); + if (!edits) { + return undefined; + } + return edits.map(function (edit) { + return { + start: scriptInfo.positionToLineOffset(edit.span.start), + end: scriptInfo.positionToLineOffset(ts.textSpanEnd(edit.span)), + newText: edit.newText ? edit.newText : "" + }; + }); + }; + Session.prototype.getCompletions = function (args, simplifiedResult) { + var _this = this; + var prefix = args.prefix || ""; + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var position = this.getPosition(args, scriptInfo); + var completions = project.getLanguageService().getCompletionsAtPosition(file, position); + if (!completions) { + return undefined; + } + if (simplifiedResult) { + return completions.entries.reduce(function (result, entry) { + if (completions.isMemberCompletion || (entry.name.toLowerCase().indexOf(prefix.toLowerCase()) === 0)) { + var name_53 = entry.name, kind = entry.kind, kindModifiers = entry.kindModifiers, sortText = entry.sortText, replacementSpan = entry.replacementSpan; + var convertedSpan = replacementSpan ? _this.decorateSpan(replacementSpan, scriptInfo) : undefined; + result.push({ name: name_53, kind: kind, kindModifiers: kindModifiers, sortText: sortText, replacementSpan: convertedSpan }); } - } - this.openFilesReferenced = openFilesReferenced; - for (var i = 0, len = orphanFiles.length; i < len; i++) { - this.addOpenFile(orphanFiles[i]); - } + return result; + }, []).sort(function (a, b) { return a.name.localeCompare(b.name); }); } else { - ts.unorderedRemoveItem(this.openFilesReferenced, info); + return completions; } - info.close(); }; - ProjectService.prototype.findReferencingProjects = function (info, excludedProject) { - var referencingProjects = []; - info.defaultProject = undefined; - for (var i = 0, len = this.inferredProjects.length; i < len; i++) { - var inferredProject = this.inferredProjects[i]; - inferredProject.updateGraph(); - if (inferredProject !== excludedProject) { - if (inferredProject.getSourceFile(info)) { - info.defaultProject = inferredProject; - referencingProjects.push(inferredProject); - } + Session.prototype.getCompletionEntryDetails = function (args) { + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var position = this.getPosition(args, scriptInfo); + return args.entryNames.reduce(function (accum, entryName) { + var details = project.getLanguageService().getCompletionEntryDetails(file, position, entryName); + if (details) { + accum.push(details); } + return accum; + }, []); + }; + Session.prototype.getCompileOnSaveAffectedFileList = function (args) { + var info = this.projectService.getScriptInfo(args.file); + var result = []; + if (!info) { + return []; } - for (var i = 0, len = this.configuredProjects.length; i < len; i++) { - var configuredProject = this.configuredProjects[i]; - configuredProject.updateGraph(); - if (configuredProject.getSourceFile(info)) { - info.defaultProject = configuredProject; - referencingProjects.push(configuredProject); + var projectsToSearch = args.projectFileName ? [this.projectService.findProject(args.projectFileName)] : info.containingProjects; + for (var _i = 0, projectsToSearch_1 = projectsToSearch; _i < projectsToSearch_1.length; _i++) { + var project = projectsToSearch_1[_i]; + if (project.compileOnSaveEnabled && project.languageServiceEnabled) { + result.push({ + projectFileName: project.getProjectName(), + fileNames: project.getCompileOnSaveAffectedFileList(info) + }); } } - return referencingProjects; + return result; }; - ProjectService.prototype.reloadProjects = function () { - this.log("reload projects."); - for (var _i = 0, _a = this.openFileRoots; _i < _a.length; _i++) { - var info = _a[_i]; - this.openOrUpdateConfiguredProjectForFile(info.fileName); + Session.prototype.emitFile = function (args) { + var _this = this; + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + if (!project) { + server.Errors.ThrowNoProject(); } - this.updateProjectStructure(); + var scriptInfo = project.getScriptInfo(file); + return project.builder.emitFile(scriptInfo, function (path, data, writeByteOrderMark) { return _this.host.writeFile(path, data, writeByteOrderMark); }); }; - ProjectService.prototype.updateProjectStructure = function () { - this.log("updating project structure from ...", "Info"); - this.printProjects(); - var unattachedOpenFiles = []; - var openFileRootsConfigured = []; - for (var _i = 0, _a = this.openFileRootsConfigured; _i < _a.length; _i++) { - var info = _a[_i]; - var project = info.defaultProject; - if (!project || !(project.getSourceFile(info))) { - info.defaultProject = undefined; - unattachedOpenFiles.push(info); - } - else { - openFileRootsConfigured.push(info); - } - } - this.openFileRootsConfigured = openFileRootsConfigured; - var openFilesReferenced = []; - for (var i = 0, len = this.openFilesReferenced.length; i < len; i++) { - var referencedFile = this.openFilesReferenced[i]; - referencedFile.defaultProject.updateGraph(); - var sourceFile = referencedFile.defaultProject.getSourceFile(referencedFile); - if (sourceFile) { - openFilesReferenced.push(referencedFile); - } - else { - unattachedOpenFiles.push(referencedFile); - } + Session.prototype.getSignatureHelpItems = function (args, simplifiedResult) { + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var position = this.getPosition(args, scriptInfo); + var helpItems = project.getLanguageService().getSignatureHelpItems(file, position); + if (!helpItems) { + return undefined; } - this.openFilesReferenced = openFilesReferenced; - var openFileRoots = []; - for (var i = 0, len = this.openFileRoots.length; i < len; i++) { - var rootFile = this.openFileRoots[i]; - var rootedProject = rootFile.defaultProject; - var referencingProjects = this.findReferencingProjects(rootFile, rootedProject); - if (rootFile.defaultProject && rootFile.defaultProject.isConfiguredProject()) { - if (!rootedProject.isConfiguredProject()) { - this.removeProject(rootedProject); - } - this.openFileRootsConfigured.push(rootFile); - } - else { - if (referencingProjects.length === 0) { - rootFile.defaultProject = rootedProject; - openFileRoots.push(rootFile); - } - else { - this.removeProject(rootedProject); - this.openFilesReferenced.push(rootFile); - } - } + if (simplifiedResult) { + var span_16 = helpItems.applicableSpan; + return { + items: helpItems.items, + applicableSpan: { + start: scriptInfo.positionToLineOffset(span_16.start), + end: scriptInfo.positionToLineOffset(span_16.start + span_16.length) + }, + selectedItemIndex: helpItems.selectedItemIndex, + argumentIndex: helpItems.argumentIndex, + argumentCount: helpItems.argumentCount + }; } - this.openFileRoots = openFileRoots; - for (var i = 0, len = unattachedOpenFiles.length; i < len; i++) { - this.addOpenFile(unattachedOpenFiles[i]); + else { + return helpItems; } - this.printProjects(); - }; - ProjectService.prototype.getScriptInfo = function (filename) { - filename = ts.normalizePath(filename); - return this.filenameToScriptInfo[filename]; }; - ProjectService.prototype.openFile = function (fileName, openedByClient, fileContent, scriptKind) { + Session.prototype.getDiagnostics = function (delay, fileNames) { var _this = this; - fileName = ts.normalizePath(fileName); - var info = this.filenameToScriptInfo[fileName]; - if (!info) { - var content = void 0; - if (this.host.fileExists(fileName)) { - content = fileContent || this.host.readFile(fileName); - } - if (!content) { - if (openedByClient) { - content = ""; - } - } - if (content !== undefined) { - info = new ScriptInfo(this.host, fileName, content, openedByClient); - info.scriptKind = scriptKind; - info.setFormatOptions(this.getFormatCodeOptions()); - this.filenameToScriptInfo[fileName] = info; - if (!info.isOpen) { - info.fileWatcher = this.host.watchFile(fileName, function (_) { _this.watchedFileChanged(fileName); }); - } + var checkList = fileNames.reduce(function (accum, uncheckedFileName) { + var fileName = server.toNormalizedPath(uncheckedFileName); + var project = _this.projectService.getDefaultProjectForFile(fileName, true); + if (project) { + accum.push({ fileName: fileName, project: project }); } + return accum; + }, []); + if (checkList.length > 0) { + this.updateErrorCheck(checkList, this.changeSeq, function (n) { return n === _this.changeSeq; }, delay); } - if (info) { - if (fileContent) { - info.svc.reload(fileContent); - } - if (openedByClient) { - info.isOpen = true; + }; + Session.prototype.change = function (args) { + var _this = this; + var _a = this.getFileAndProject(args, false), file = _a.file, project = _a.project; + if (project) { + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var start = scriptInfo.lineOffsetToPosition(args.line, args.offset); + var end = scriptInfo.lineOffsetToPosition(args.endLine, args.endOffset); + if (start >= 0) { + scriptInfo.editContent(start, end, args.insertString); + this.changeSeq++; } + this.updateProjectStructure(this.changeSeq, function (n) { return n === _this.changeSeq; }); } - return info; }; - ProjectService.prototype.findConfigFile = function (searchPath) { - while (true) { - var tsconfigFileName = ts.combinePaths(searchPath, "tsconfig.json"); - if (this.host.fileExists(tsconfigFileName)) { - return tsconfigFileName; - } - var jsconfigFileName = ts.combinePaths(searchPath, "jsconfig.json"); - if (this.host.fileExists(jsconfigFileName)) { - return jsconfigFileName; - } - var parentPath = ts.getDirectoryPath(searchPath); - if (parentPath === searchPath) { - break; + Session.prototype.reload = function (args, reqSeq) { + var file = server.toNormalizedPath(args.file); + var project = this.projectService.getDefaultProjectForFile(file, true); + if (project) { + this.changeSeq++; + if (project.reloadScript(file)) { + this.output(undefined, CommandNames.Reload, reqSeq); } - searchPath = parentPath; } - return undefined; }; - ProjectService.prototype.openClientFile = function (fileName, fileContent, scriptKind) { - var _a = this.openOrUpdateConfiguredProjectForFile(fileName), configFileName = _a.configFileName, configFileErrors = _a.configFileErrors; - var info = this.openFile(fileName, true, fileContent, scriptKind); - this.addOpenFile(info); - this.printProjects(); - return { configFileName: configFileName, configFileErrors: configFileErrors }; + Session.prototype.saveToTmp = function (fileName, tempFileName) { + var scriptInfo = this.projectService.getScriptInfo(fileName); + if (scriptInfo) { + scriptInfo.saveTo(tempFileName); + } }; - ProjectService.prototype.openOrUpdateConfiguredProjectForFile = function (fileName) { - var searchPath = ts.normalizePath(ts.getDirectoryPath(fileName)); - this.log("Search path: " + searchPath, "Info"); - var configFileName = this.findConfigFile(searchPath); - if (configFileName) { - this.log("Config file name: " + configFileName, "Info"); - var project = this.findConfiguredProjectByConfigFile(configFileName); - if (!project) { - var configResult = this.openConfigFile(configFileName, fileName); - if (!configResult.project) { - return { configFileName: configFileName, configFileErrors: configResult.errors }; - } - else { - this.log("Opened configuration file " + configFileName, "Info"); - this.configuredProjects.push(configResult.project); - if (configResult.errors && configResult.errors.length > 0) { - return { configFileName: configFileName, configFileErrors: configResult.errors }; + Session.prototype.closeClientFile = function (fileName) { + if (!fileName) { + return; + } + var file = ts.normalizePath(fileName); + this.projectService.closeClientFile(file); + }; + Session.prototype.decorateNavigationBarItems = function (items, scriptInfo) { + var _this = this; + return ts.map(items, function (item) { return ({ + text: item.text, + kind: item.kind, + kindModifiers: item.kindModifiers, + spans: item.spans.map(function (span) { return _this.decorateSpan(span, scriptInfo); }), + childItems: _this.decorateNavigationBarItems(item.childItems, scriptInfo), + indent: item.indent + }); }); + }; + Session.prototype.getNavigationBarItems = function (args, simplifiedResult) { + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + var items = project.getLanguageService(false).getNavigationBarItems(file); + return !items + ? undefined + : simplifiedResult + ? this.decorateNavigationBarItems(items, project.getScriptInfoForNormalizedPath(file)) + : items; + }; + Session.prototype.decorateNavigationTree = function (tree, scriptInfo) { + var _this = this; + return { + text: tree.text, + kind: tree.kind, + kindModifiers: tree.kindModifiers, + spans: tree.spans.map(function (span) { return _this.decorateSpan(span, scriptInfo); }), + childItems: ts.map(tree.childItems, function (item) { return _this.decorateNavigationTree(item, scriptInfo); }) + }; + }; + Session.prototype.decorateSpan = function (span, scriptInfo) { + return { + start: scriptInfo.positionToLineOffset(span.start), + end: scriptInfo.positionToLineOffset(ts.textSpanEnd(span)) + }; + }; + Session.prototype.getNavigationTree = function (args, simplifiedResult) { + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + var tree = project.getLanguageService(false).getNavigationTree(file); + return !tree + ? undefined + : simplifiedResult + ? this.decorateNavigationTree(tree, project.getScriptInfoForNormalizedPath(file)) + : tree; + }; + Session.prototype.getNavigateToItems = function (args, simplifiedResult) { + var projects = this.getProjects(args); + var fileName = args.currentFileOnly ? args.file && ts.normalizeSlashes(args.file) : undefined; + if (simplifiedResult) { + return server.combineProjectOutput(projects, function (project) { + var navItems = project.getLanguageService().getNavigateToItems(args.searchValue, args.maxResultCount, fileName, project.isNonTsProject()); + if (!navItems) { + return []; + } + return navItems.map(function (navItem) { + var scriptInfo = project.getScriptInfo(navItem.fileName); + var start = scriptInfo.positionToLineOffset(navItem.textSpan.start); + var end = scriptInfo.positionToLineOffset(ts.textSpanEnd(navItem.textSpan)); + var bakedItem = { + name: navItem.name, + kind: navItem.kind, + file: navItem.fileName, + start: start, + end: end + }; + if (navItem.kindModifiers && (navItem.kindModifiers !== "")) { + bakedItem.kindModifiers = navItem.kindModifiers; } - } + if (navItem.matchKind !== "none") { + bakedItem.matchKind = navItem.matchKind; + } + if (navItem.containerName && (navItem.containerName.length > 0)) { + bakedItem.containerName = navItem.containerName; + } + if (navItem.containerKind && (navItem.containerKind.length > 0)) { + bakedItem.containerKind = navItem.containerKind; + } + return bakedItem; + }); + }, undefined, areNavToItemsForTheSameLocation); + } + else { + return server.combineProjectOutput(projects, function (project) { return project.getLanguageService().getNavigateToItems(args.searchValue, args.maxResultCount, fileName, project.isNonTsProject()); }, undefined, navigateToItemIsEqualTo); + } + function navigateToItemIsEqualTo(a, b) { + if (a === b) { + return true; } - else { - this.updateConfiguredProject(project); + if (!a || !b) { + return false; } - return { configFileName: configFileName }; + return a.containerKind === b.containerKind && + a.containerName === b.containerName && + a.fileName === b.fileName && + a.isCaseSensitive === b.isCaseSensitive && + a.kind === b.kind && + a.kindModifiers === b.containerName && + a.matchKind === b.matchKind && + a.name === b.name && + a.textSpan.start === b.textSpan.start && + a.textSpan.length === b.textSpan.length; } - else { - this.log("No config files found."); + function areNavToItemsForTheSameLocation(a, b) { + if (a && b) { + return a.file === b.file && + a.start === b.start && + a.end === b.end; + } + return false; } - return {}; }; - ProjectService.prototype.closeClientFile = function (filename) { - var info = this.filenameToScriptInfo[filename]; - if (info) { - this.closeOpenFile(info); - info.isOpen = false; - } - this.printProjects(); + Session.prototype.getSupportedCodeFixes = function () { + return ts.getSupportedCodeFixes(); }; - ProjectService.prototype.getProjectForFile = function (filename) { - var scriptInfo = this.filenameToScriptInfo[filename]; - if (scriptInfo) { - return scriptInfo.defaultProject; + Session.prototype.getCodeFixes = function (args, simplifiedResult) { + var _this = this; + var _a = this.getFileAndProjectWithoutRefreshingInferredProjects(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var startPosition = getStartPosition(); + var endPosition = getEndPosition(); + var codeActions = project.getLanguageService().getCodeFixesAtPosition(file, startPosition, endPosition, args.errorCodes); + if (!codeActions) { + return undefined; } - }; - ProjectService.prototype.printProjectsForFile = function (filename) { - var scriptInfo = this.filenameToScriptInfo[filename]; - if (scriptInfo) { - this.psLogger.startGroup(); - this.psLogger.info("Projects for " + filename); - var projects = this.findReferencingProjects(scriptInfo); - for (var i = 0, len = projects.length; i < len; i++) { - this.psLogger.info("Project " + i.toString()); - } - this.psLogger.endGroup(); + if (simplifiedResult) { + return codeActions.map(function (codeAction) { return _this.mapCodeAction(codeAction, scriptInfo); }); } else { - this.psLogger.info(filename + " not in any project"); - } - }; - ProjectService.prototype.printProjects = function () { - if (!this.psLogger.isVerbose()) { - return; + return codeActions; } - this.psLogger.startGroup(); - for (var i = 0, len = this.inferredProjects.length; i < len; i++) { - var project = this.inferredProjects[i]; - project.updateGraph(); - this.psLogger.info("Project " + i.toString()); - this.psLogger.info(project.filesToString()); - this.psLogger.info("-----------------------------------------------"); + function getStartPosition() { + return args.startPosition !== undefined ? args.startPosition : scriptInfo.lineOffsetToPosition(args.startLine, args.startOffset); } - for (var i = 0, len = this.configuredProjects.length; i < len; i++) { - var project = this.configuredProjects[i]; - project.updateGraph(); - this.psLogger.info("Project (configured) " + (i + this.inferredProjects.length).toString()); - this.psLogger.info(project.filesToString()); - this.psLogger.info("-----------------------------------------------"); + function getEndPosition() { + return args.endPosition !== undefined ? args.endPosition : scriptInfo.lineOffsetToPosition(args.endLine, args.endOffset); } - this.psLogger.info("Open file roots of inferred projects: "); - for (var i = 0, len = this.openFileRoots.length; i < len; i++) { - this.psLogger.info(this.openFileRoots[i].fileName); + }; + Session.prototype.mapCodeAction = function (codeAction, scriptInfo) { + var _this = this; + return { + description: codeAction.description, + changes: codeAction.changes.map(function (change) { return ({ + fileName: change.fileName, + textChanges: change.textChanges.map(function (textChange) { return _this.convertTextChangeToCodeEdit(textChange, scriptInfo); }) + }); }) + }; + }; + Session.prototype.convertTextChangeToCodeEdit = function (change, scriptInfo) { + return { + start: scriptInfo.positionToLineOffset(change.span.start), + end: scriptInfo.positionToLineOffset(change.span.start + change.span.length), + newText: change.newText ? change.newText : "" + }; + }; + Session.prototype.getBraceMatching = function (args, simplifiedResult) { + var _this = this; + var _a = this.getFileAndProjectWithoutRefreshingInferredProjects(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var position = this.getPosition(args, scriptInfo); + var spans = project.getLanguageService(false).getBraceMatchingAtPosition(file, position); + return !spans + ? undefined + : simplifiedResult + ? spans.map(function (span) { return _this.decorateSpan(span, scriptInfo); }) + : spans; + }; + Session.prototype.getDiagnosticsForProject = function (delay, fileName) { + var _this = this; + var _a = this.getProjectInfoWorker(fileName, undefined, true), fileNames = _a.fileNames, languageServiceDisabled = _a.languageServiceDisabled; + if (languageServiceDisabled) { + return; } - this.psLogger.info("Open files referenced by inferred or configured projects: "); - for (var i = 0, len = this.openFilesReferenced.length; i < len; i++) { - var fileInfo = this.openFilesReferenced[i].fileName; - if (this.openFilesReferenced[i].defaultProject.isConfiguredProject()) { - fileInfo += " (configured)"; + var fileNamesInProject = fileNames.filter(function (value, index, array) { return value.indexOf("lib.d.ts") < 0; }); + var highPriorityFiles = []; + var mediumPriorityFiles = []; + var lowPriorityFiles = []; + var veryLowPriorityFiles = []; + var normalizedFileName = server.toNormalizedPath(fileName); + var project = this.projectService.getDefaultProjectForFile(normalizedFileName, true); + for (var _i = 0, fileNamesInProject_1 = fileNamesInProject; _i < fileNamesInProject_1.length; _i++) { + var fileNameInProject = fileNamesInProject_1[_i]; + if (this.getCanonicalFileName(fileNameInProject) == this.getCanonicalFileName(fileName)) + highPriorityFiles.push(fileNameInProject); + else { + var info = this.projectService.getScriptInfo(fileNameInProject); + if (!info.isOpen) { + if (fileNameInProject.indexOf(".d.ts") > 0) + veryLowPriorityFiles.push(fileNameInProject); + else + lowPriorityFiles.push(fileNameInProject); + } + else + mediumPriorityFiles.push(fileNameInProject); } - this.psLogger.info(fileInfo); } - this.psLogger.info("Open file roots of configured projects: "); - for (var i = 0, len = this.openFileRootsConfigured.length; i < len; i++) { - this.psLogger.info(this.openFileRootsConfigured[i].fileName); + fileNamesInProject = highPriorityFiles.concat(mediumPriorityFiles).concat(lowPriorityFiles).concat(veryLowPriorityFiles); + if (fileNamesInProject.length > 0) { + var checkList = fileNamesInProject.map(function (fileName) { return ({ fileName: fileName, project: project }); }); + this.updateErrorCheck(checkList, this.changeSeq, function (n) { return n == _this.changeSeq; }, delay, 200, false); } - this.psLogger.endGroup(); }; - ProjectService.prototype.configProjectIsActive = function (fileName) { - return this.findConfiguredProjectByConfigFile(fileName) === undefined; + Session.prototype.getCanonicalFileName = function (fileName) { + var name = this.host.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase(); + return ts.normalizePath(name); + }; + Session.prototype.exit = function () { }; - ProjectService.prototype.findConfiguredProjectByConfigFile = function (configFileName) { - for (var i = 0, len = this.configuredProjects.length; i < len; i++) { - if (this.configuredProjects[i].projectFilename == configFileName) { - return this.configuredProjects[i]; - } + Session.prototype.notRequired = function () { + return { responseRequired: false }; + }; + Session.prototype.requiredResponse = function (response) { + return { response: response, responseRequired: true }; + }; + Session.prototype.addProtocolHandler = function (command, handler) { + if (command in this.handlers) { + throw new Error("Protocol handler already exists for command \"" + command + "\""); } - return undefined; + this.handlers[command] = handler; }; - ProjectService.prototype.configFileToProjectOptions = function (configFilename) { - configFilename = ts.normalizePath(configFilename); - var errors = []; - var dirPath = ts.getDirectoryPath(configFilename); - var contents = this.host.readFile(configFilename); - var _a = ts.parseAndReEmitConfigJSONFile(contents), configJsonObject = _a.configJsonObject, diagnostics = _a.diagnostics; - errors = ts.concatenate(errors, diagnostics); - var parsedCommandLine = ts.parseJsonConfigFileContent(configJsonObject, this.host, dirPath, {}, configFilename); - errors = ts.concatenate(errors, parsedCommandLine.errors); - ts.Debug.assert(!!parsedCommandLine.fileNames); - if (parsedCommandLine.fileNames.length === 0) { - errors.push(ts.createCompilerDiagnostic(ts.Diagnostics.The_config_file_0_found_doesn_t_contain_any_source_files, configFilename)); - return { errors: errors }; + Session.prototype.executeCommand = function (request) { + var handler = this.handlers[request.command]; + if (handler) { + return handler(request); } else { - var projectOptions = { - files: parsedCommandLine.fileNames, - wildcardDirectories: parsedCommandLine.wildcardDirectories, - compilerOptions: parsedCommandLine.options - }; - return { projectOptions: projectOptions, errors: errors }; + this.logger.msg("Unrecognized JSON command: " + JSON.stringify(request), server.Msg.Err); + this.output(undefined, CommandNames.Unknown, request.seq, "Unrecognized JSON command: " + request.command); + return { responseRequired: false }; } }; - ProjectService.prototype.exceedTotalNonTsFileSizeLimit = function (fileNames) { - var totalNonTsFileSize = 0; - if (!this.host.getFileSize) { - return false; - } - for (var _i = 0, fileNames_4 = fileNames; _i < fileNames_4.length; _i++) { - var fileName = fileNames_4[_i]; - if (ts.hasTypeScriptFileExtension(fileName)) { - continue; - } - totalNonTsFileSize += this.host.getFileSize(fileName); - if (totalNonTsFileSize > server.maxProgramSizeForNonTsFiles) { - return true; + Session.prototype.onMessage = function (message) { + this.gcTimer.scheduleCollect(); + var start; + if (this.logger.hasLevel(server.LogLevel.requestTime)) { + start = this.hrtime(); + if (this.logger.hasLevel(server.LogLevel.verbose)) { + this.logger.info("request: " + message); } } - return false; - }; - ProjectService.prototype.openConfigFile = function (configFilename, clientFileName) { - var _this = this; - var parseConfigFileResult = this.configFileToProjectOptions(configFilename); - var errors = parseConfigFileResult.errors; - if (!parseConfigFileResult.projectOptions) { - return { errors: errors }; - } - var projectOptions = parseConfigFileResult.projectOptions; - if (!projectOptions.compilerOptions.disableSizeLimit && projectOptions.compilerOptions.allowJs) { - if (this.exceedTotalNonTsFileSizeLimit(projectOptions.files)) { - var project_1 = this.createProject(configFilename, projectOptions, true); - project_1.projectFileWatcher = this.host.watchFile(ts.toPath(configFilename, configFilename, ts.createGetCanonicalFileName(ts.sys.useCaseSensitiveFileNames)), function (_) { return _this.watchedProjectConfigFileChanged(project_1); }); - return { project: project_1, errors: errors }; - } - } - var project = this.createProject(configFilename, projectOptions); - for (var _i = 0, _a = projectOptions.files; _i < _a.length; _i++) { - var rootFilename = _a[_i]; - if (this.host.fileExists(rootFilename)) { - var info = this.openFile(rootFilename, clientFileName == rootFilename); - project.addRoot(info); + var request; + try { + request = JSON.parse(message); + var _a = this.executeCommand(request), response = _a.response, responseRequired = _a.responseRequired; + if (this.logger.hasLevel(server.LogLevel.requestTime)) { + var elapsedTime = hrTimeToMilliseconds(this.hrtime(start)).toFixed(4); + if (responseRequired) { + this.logger.perftrc(request.seq + "::" + request.command + ": elapsed time (in milliseconds) " + elapsedTime); + } + else { + this.logger.perftrc(request.seq + "::" + request.command + ": async elapsed time (in milliseconds) " + elapsedTime); + } } - else { - (errors || (errors = [])).push(ts.createCompilerDiagnostic(ts.Diagnostics.File_0_not_found, rootFilename)); + if (response) { + this.output(response, request.command, request.seq); } - } - project.finishGraph(); - project.projectFileWatcher = this.host.watchFile(configFilename, function (_) { return _this.watchedProjectConfigFileChanged(project); }); - var configDirectoryPath = ts.getDirectoryPath(configFilename); - this.log("Add recursive watcher for: " + configDirectoryPath); - project.directoryWatcher = this.host.watchDirectory(configDirectoryPath, function (path) { return _this.directoryWatchedForSourceFilesChanged(project, path); }, true); - project.directoriesWatchedForWildcards = ts.reduceProperties(ts.createMap(projectOptions.wildcardDirectories), function (watchers, flag, directory) { - if (ts.comparePaths(configDirectoryPath, directory, ".", !_this.host.useCaseSensitiveFileNames) !== 0) { - var recursive = (flag & 1) !== 0; - _this.log("Add " + (recursive ? "recursive " : "") + "watcher for: " + directory); - watchers[directory] = _this.host.watchDirectory(directory, function (path) { return _this.directoryWatchedForSourceFilesChanged(project, path); }, recursive); + else if (responseRequired) { + this.output(undefined, request.command, request.seq, "No content available."); } - return watchers; - }, {}); - return { project: project, errors: errors }; - }; - ProjectService.prototype.updateConfiguredProject = function (project) { - var _this = this; - if (!this.host.fileExists(project.projectFilename)) { - this.log("Config file deleted"); - this.removeProject(project); } - else { - var _a = this.configFileToProjectOptions(project.projectFilename), projectOptions = _a.projectOptions, errors = _a.errors; - if (!projectOptions) { - return errors; - } - else { - if (projectOptions.compilerOptions && !projectOptions.compilerOptions.disableSizeLimit && this.exceedTotalNonTsFileSizeLimit(projectOptions.files)) { - project.setProjectOptions(projectOptions); - if (project.languageServiceDiabled) { - return errors; - } - project.disableLanguageService(); - if (project.directoryWatcher) { - project.directoryWatcher.close(); - project.directoryWatcher = undefined; - } - return errors; - } - if (project.languageServiceDiabled) { - project.setProjectOptions(projectOptions); - project.enableLanguageService(); - project.directoryWatcher = this.host.watchDirectory(ts.getDirectoryPath(project.projectFilename), function (path) { return _this.directoryWatchedForSourceFilesChanged(project, path); }, true); - for (var _i = 0, _b = projectOptions.files; _i < _b.length; _i++) { - var rootFilename = _b[_i]; - if (this.host.fileExists(rootFilename)) { - var info = this.openFile(rootFilename, false); - project.addRoot(info); - } - } - project.finishGraph(); - return errors; - } - var oldFileNames_1 = project.projectOptions ? project.projectOptions.files : project.compilerService.host.roots.map(function (info) { return info.fileName; }); - var newFileNames_1 = ts.filter(projectOptions.files, function (f) { return _this.host.fileExists(f); }); - var fileNamesToRemove = oldFileNames_1.filter(function (f) { return newFileNames_1.indexOf(f) < 0; }); - var fileNamesToAdd = newFileNames_1.filter(function (f) { return oldFileNames_1.indexOf(f) < 0; }); - for (var _c = 0, fileNamesToRemove_1 = fileNamesToRemove; _c < fileNamesToRemove_1.length; _c++) { - var fileName = fileNamesToRemove_1[_c]; - var info = this.getScriptInfo(fileName); - if (info) { - project.removeRoot(info); - } - } - for (var _d = 0, fileNamesToAdd_1 = fileNamesToAdd; _d < fileNamesToAdd_1.length; _d++) { - var fileName = fileNamesToAdd_1[_d]; - var info = this.getScriptInfo(fileName); - if (!info) { - info = this.openFile(fileName, false); - } - else { - if (info.isOpen) { - if (this.openFileRoots.indexOf(info) >= 0) { - ts.unorderedRemoveItem(this.openFileRoots, info); - if (info.defaultProject && !info.defaultProject.isConfiguredProject()) { - this.removeProject(info.defaultProject); - } - } - if (this.openFilesReferenced.indexOf(info) >= 0) { - ts.unorderedRemoveItem(this.openFilesReferenced, info); - } - this.openFileRootsConfigured.push(info); - info.defaultProject = project; - } - } - project.addRoot(info); - } - project.setProjectOptions(projectOptions); - project.finishGraph(); + catch (err) { + if (err instanceof ts.OperationCanceledException) { + this.output({ canceled: true }, request.command, request.seq); + return; } - return errors; + this.logError(err, message); + this.output(undefined, request ? request.command : CommandNames.Unknown, request ? request.seq : 0, "Error processing request. " + err.message + "\n" + err.stack); } }; - ProjectService.prototype.createProject = function (projectFilename, projectOptions, languageServiceDisabled) { - var project = new Project(this, projectOptions, languageServiceDisabled); - project.projectFilename = projectFilename; - return project; - }; - return ProjectService; - }()); - server.ProjectService = ProjectService; - var CompilerService = (function () { - function CompilerService(project, opt) { - this.project = project; - this.documentRegistry = ts.createDocumentRegistry(); - this.host = new LSHost(project.projectService.host, project); - if (opt) { - this.setCompilerOptions(opt); - } - else { - var defaultOpts = ts.getDefaultCompilerOptions(); - defaultOpts.allowNonTsExtensions = true; - defaultOpts.allowJs = true; - this.setCompilerOptions(defaultOpts); - } - this.languageService = ts.createLanguageService(this.host, this.documentRegistry); - this.classifier = ts.createClassifier(); - } - CompilerService.prototype.setCompilerOptions = function (opt) { - this.settings = opt; - this.host.setCompilationSettings(opt); - }; - CompilerService.prototype.isExternalModule = function (filename) { - var sourceFile = this.languageService.getNonBoundSourceFile(filename); - return ts.isExternalModule(sourceFile); - }; - CompilerService.getDefaultFormatCodeOptions = function (host) { - return ts.clone({ - BaseIndentSize: 0, - IndentSize: 4, - TabSize: 4, - NewLineCharacter: host.newLine || "\n", - ConvertTabsToSpaces: true, - IndentStyle: ts.IndentStyle.Smart, - InsertSpaceAfterCommaDelimiter: true, - InsertSpaceAfterSemicolonInForStatements: true, - InsertSpaceBeforeAndAfterBinaryOperators: true, - InsertSpaceAfterKeywordsInControlFlowStatements: true, - InsertSpaceAfterFunctionKeywordForAnonymousFunctions: false, - InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false, - InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false, - InsertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true, - InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false, - InsertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: false, - InsertSpaceAfterTypeAssertion: false, - PlaceOpenBraceOnNewLineForFunctions: false, - PlaceOpenBraceOnNewLineForControlBlocks: false - }); - }; - return CompilerService; + return Session; }()); - server.CompilerService = CompilerService; + server.Session = Session; + })(server = ts.server || (ts.server = {})); +})(ts || (ts = {})); +var ts; +(function (ts) { + var server; + (function (server) { + var lineCollectionCapacity = 4; (function (CharRangeSection) { CharRangeSection[CharRangeSection["PreStart"] = 0] = "PreStart"; CharRangeSection[CharRangeSection["Start"] = 1] = "Start"; @@ -62605,16 +64797,17 @@ var ts; var EditWalker = (function (_super) { __extends(EditWalker, _super); function EditWalker() { - _super.call(this); - this.lineIndex = new LineIndex(); - this.endBranch = []; - this.state = CharRangeSection.Entire; - this.initialText = ""; - this.trailingText = ""; - this.suppressTrailingText = false; - this.lineIndex.root = new LineNode(); - this.startPath = [this.lineIndex.root]; - this.stack = [this.lineIndex.root]; + var _this = _super.call(this) || this; + _this.lineIndex = new LineIndex(); + _this.endBranch = []; + _this.state = CharRangeSection.Entire; + _this.initialText = ""; + _this.trailingText = ""; + _this.suppressTrailingText = false; + _this.lineIndex.root = new LineNode(); + _this.startPath = [_this.lineIndex.root]; + _this.stack = [_this.lineIndex.root]; + return _this; } EditWalker.prototype.insertLines = function (insertedText) { if (this.suppressTrailingText) { @@ -62801,10 +64994,19 @@ var ts; var ScriptVersionCache = (function () { function ScriptVersionCache() { this.changes = []; - this.versions = []; + this.versions = new Array(ScriptVersionCache.maxVersions); this.minVersion = 0; this.currentVersion = 0; } + ScriptVersionCache.prototype.versionToIndex = function (version) { + if (version < this.minVersion || version > this.currentVersion) { + return undefined; + } + return version % ScriptVersionCache.maxVersions; + }; + ScriptVersionCache.prototype.currentVersionToIndex = function () { + return this.currentVersion % ScriptVersionCache.maxVersions; + }; ScriptVersionCache.prototype.edit = function (pos, deleteLen, insertedText) { this.changes[this.changes.length] = new TextChange(pos, deleteLen, insertedText); if ((this.changes.length > ScriptVersionCache.changeNumberThreshold) || @@ -62814,7 +65016,7 @@ var ts; } }; ScriptVersionCache.prototype.latest = function () { - return this.versions[this.currentVersion]; + return this.versions[this.currentVersionToIndex()]; }; ScriptVersionCache.prototype.latestVersion = function () { if (this.changes.length > 0) { @@ -62822,32 +65024,30 @@ var ts; } return this.currentVersion; }; - ScriptVersionCache.prototype.reloadFromFile = function (filename, cb) { + ScriptVersionCache.prototype.reloadFromFile = function (filename) { var content = this.host.readFile(filename); if (!content) { content = ""; } this.reload(content); - if (cb) - cb(); }; ScriptVersionCache.prototype.reload = function (script) { this.currentVersion++; this.changes = []; var snap = new LineIndexSnapshot(this.currentVersion, this); - this.versions[this.currentVersion] = snap; + for (var i = 0; i < this.versions.length; i++) { + this.versions[i] = undefined; + } + this.versions[this.currentVersionToIndex()] = snap; snap.index = new LineIndex(); var lm = LineIndex.linesFromText(script); snap.index.load(lm.lines); - for (var i = this.minVersion; i < this.currentVersion; i++) { - this.versions[i] = undefined; - } this.minVersion = this.currentVersion; }; ScriptVersionCache.prototype.getSnapshot = function () { - var snap = this.versions[this.currentVersion]; + var snap = this.versions[this.currentVersionToIndex()]; if (this.changes.length > 0) { - var snapIndex = this.latest().index; + var snapIndex = snap.index; for (var i = 0, len = this.changes.length; i < len; i++) { var change = this.changes[i]; snapIndex = snapIndex.edit(change.pos, change.deleteLen, change.insertedText); @@ -62856,14 +65056,10 @@ var ts; snap.index = snapIndex; snap.changesSincePreviousVersion = this.changes; this.currentVersion = snap.version; - this.versions[snap.version] = snap; + this.versions[this.currentVersionToIndex()] = snap; this.changes = []; if ((this.currentVersion - this.minVersion) >= ScriptVersionCache.maxVersions) { - var oldMin = this.minVersion; this.minVersion = (this.currentVersion - ScriptVersionCache.maxVersions) + 1; - for (var j = oldMin; j < this.minVersion; j++) { - this.versions[j] = undefined; - } } } return snap; @@ -62873,7 +65069,7 @@ var ts; if (oldVersion >= this.minVersion) { var textChangeRanges = []; for (var i = oldVersion + 1; i <= newVersion; i++) { - var snap = this.versions[i]; + var snap = this.versions[this.versionToIndex(i)]; for (var j = 0, len = snap.changesSincePreviousVersion.length; j < len; j++) { var textChange = snap.changesSincePreviousVersion[j]; textChangeRanges[textChangeRanges.length] = textChange.getTextChangeRange(); @@ -63011,7 +65207,7 @@ var ts; done: false, leaf: function (relativeStart, relativeLength, ll) { if (!f(ll, relativeStart, relativeLength)) { - walkFns.done = true; + this.done = true; } } }; @@ -63024,7 +65220,7 @@ var ts; return source.substring(0, s) + nt + source.substring(s + dl, source.length); } if (this.root.charCount() === 0) { - if (newText) { + if (newText !== undefined) { this.load(LineIndex.linesFromText(newText).lines); return this; } @@ -63404,12 +65600,6 @@ var ts; function LineLeaf(text) { this.text = text; } - LineLeaf.prototype.setUdata = function (data) { - this.udata = data; - }; - LineLeaf.prototype.getUdata = function () { - return this.udata; - }; LineLeaf.prototype.isLeaf = function () { return true; }; @@ -63431,6 +65621,25 @@ var ts; (function (ts) { var server; (function (server) { + var net = require("net"); + var childProcess = require("child_process"); + var os = require("os"); + function getGlobalTypingsCacheLocation() { + var basePath; + switch (process.platform) { + case "win32": + basePath = process.env.LOCALAPPDATA || process.env.APPDATA || os.homedir(); + break; + case "linux": + basePath = os.homedir(); + break; + case "darwin": + basePath = ts.combinePaths(os.homedir(), "Library/Application Support/"); + break; + } + ts.Debug.assert(basePath !== undefined); + return ts.combinePaths(ts.normalizeSlashes(basePath), "Microsoft/TypeScript"); + } var readline = require("readline"); var fs = require("fs"); var rl = readline.createInterface({ @@ -63439,8 +65648,9 @@ var ts; terminal: false }); var Logger = (function () { - function Logger(logFilename, level) { + function Logger(logFilename, traceToConsole, level) { this.logFilename = logFilename; + this.traceToConsole = traceToConsole; this.level = level; this.fd = -1; this.seq = 0; @@ -63455,11 +65665,14 @@ var ts; fs.close(this.fd); } }; + Logger.prototype.getLogFileName = function () { + return this.logFilename; + }; Logger.prototype.perftrc = function (s) { - this.msg(s, "Perf"); + this.msg(s, server.Msg.Perf); }; Logger.prototype.info = function (s) { - this.msg(s, "Info"); + this.msg(s, server.Msg.Info); }; Logger.prototype.startGroup = function () { this.inGroup = true; @@ -63471,19 +65684,19 @@ var ts; this.firstInGroup = true; }; Logger.prototype.loggingEnabled = function () { - return !!this.logFilename; + return !!this.logFilename || this.traceToConsole; }; - Logger.prototype.isVerbose = function () { - return this.loggingEnabled() && (this.level == "verbose"); + Logger.prototype.hasLevel = function (level) { + return this.loggingEnabled() && this.level >= level; }; Logger.prototype.msg = function (s, type) { - if (type === void 0) { type = "Err"; } + if (type === void 0) { type = server.Msg.Err; } if (this.fd < 0) { if (this.logFilename) { this.fd = fs.openSync(this.logFilename, "w"); } } - if (this.fd >= 0) { + if (this.fd >= 0 || this.traceToConsole) { s = s + "\n"; var prefix = Logger.padStringRight(type + " " + this.seq.toString(), " "); if (this.firstInGroup) { @@ -63494,19 +65707,88 @@ var ts; this.seq++; this.firstInGroup = true; } - var buf = new Buffer(s); - fs.writeSync(this.fd, buf, 0, buf.length, null); + if (this.fd >= 0) { + var buf = new Buffer(s); + fs.writeSync(this.fd, buf, 0, buf.length, null); + } + if (this.traceToConsole) { + console.warn(s); + } } }; return Logger; }()); + var NodeTypingsInstaller = (function () { + function NodeTypingsInstaller(logger, eventPort, globalTypingsCacheLocation, newLine) { + var _this = this; + this.logger = logger; + this.eventPort = eventPort; + this.globalTypingsCacheLocation = globalTypingsCacheLocation; + this.newLine = newLine; + if (eventPort) { + var s_1 = net.connect({ port: eventPort }, function () { + _this.socket = s_1; + }); + } + } + NodeTypingsInstaller.prototype.attach = function (projectService) { + var _this = this; + this.projectService = projectService; + if (this.logger.hasLevel(server.LogLevel.requestTime)) { + this.logger.info("Binding..."); + } + var args = ["--globalTypingsCacheLocation", this.globalTypingsCacheLocation]; + if (this.logger.loggingEnabled() && this.logger.getLogFileName()) { + args.push("--logFile", ts.combinePaths(ts.getDirectoryPath(ts.normalizeSlashes(this.logger.getLogFileName())), "ti-" + process.pid + ".log")); + } + var execArgv = []; + { + for (var _i = 0, _a = process.execArgv; _i < _a.length; _i++) { + var arg = _a[_i]; + var match = /^--(debug|inspect)(=(\d+))?$/.exec(arg); + if (match) { + var currentPort = match[3] !== undefined + ? +match[3] + : match[1] === "debug" ? 5858 : 9229; + execArgv.push("--" + match[1] + "=" + (currentPort + 1)); + break; + } + } + } + this.installer = childProcess.fork(ts.combinePaths(__dirname, "typingsInstaller.js"), args, { execArgv: execArgv }); + this.installer.on("message", function (m) { return _this.handleMessage(m); }); + process.on("exit", function () { + _this.installer.kill(); + }); + }; + NodeTypingsInstaller.prototype.onProjectClosed = function (p) { + this.installer.send({ projectName: p.getProjectName(), kind: "closeProject" }); + }; + NodeTypingsInstaller.prototype.enqueueInstallTypingsRequest = function (project, typingOptions) { + var request = server.createInstallTypingsRequest(project, typingOptions); + if (this.logger.hasLevel(server.LogLevel.verbose)) { + this.logger.info("Sending request: " + JSON.stringify(request)); + } + this.installer.send(request); + }; + NodeTypingsInstaller.prototype.handleMessage = function (response) { + if (this.logger.hasLevel(server.LogLevel.verbose)) { + this.logger.info("Received response: " + JSON.stringify(response)); + } + this.projectService.updateTypingsForProject(response); + if (response.kind == "set" && this.socket) { + this.socket.write(server.formatMessage({ seq: 0, type: "event", message: response }, this.logger, Buffer.byteLength, this.newLine), "utf8"); + } + }; + return NodeTypingsInstaller; + }()); var IOSession = (function (_super) { __extends(IOSession, _super); - function IOSession(host, logger) { - _super.call(this, host, Buffer.byteLength, process.hrtime, logger); + function IOSession(host, cancellationToken, installerEventPort, canUseEvents, useSingleInferredProject, globalTypingsCacheLocation, logger) { + return _super.call(this, host, cancellationToken, useSingleInferredProject, new NodeTypingsInstaller(logger, installerEventPort, globalTypingsCacheLocation, host.newLine), Buffer.byteLength, process.hrtime, logger, canUseEvents) || this; } IOSession.prototype.exit = function () { - this.projectService.log("Exiting...", "Info"); + this.logger.info("Exiting..."); this.projectService.closeLog(); process.exit(0); }; @@ -63523,7 +65805,7 @@ var ts; return IOSession; }(server.Session)); function parseLoggingEnvironmentString(logEnvStr) { - var logEnv = {}; + var logEnv = { logToFile: true }; var args = logEnvStr.split(" "); for (var i = 0, len = args.length; i < (len - 1); i += 2) { var option = args[i]; @@ -63531,10 +65813,17 @@ var ts; if (option && value) { switch (option) { case "-file": - logEnv.file = value; + logEnv.file = ts.stripQuotes(value); break; case "-level": - logEnv.detailLevel = value; + var level = server.LogLevel[value]; + logEnv.detailLevel = typeof level === "number" ? level : server.LogLevel.normal; + break; + case "-traceToConsole": + logEnv.traceToConsole = value.toLowerCase() === "true"; + break; + case "-logToFile": + logEnv.logToFile = value.toLowerCase() === "true"; break; } } @@ -63543,21 +65832,25 @@ var ts; } function createLoggerFromEnv() { var fileName = undefined; - var detailLevel = "normal"; + var detailLevel = server.LogLevel.normal; + var traceToConsole = false; var logEnvStr = process.env["TSS_LOG"]; if (logEnvStr) { var logEnv = parseLoggingEnvironmentString(logEnvStr); - if (logEnv.file) { - fileName = logEnv.file; - } - else { - fileName = __dirname + "/.log" + process.pid.toString(); + if (logEnv.logToFile) { + if (logEnv.file) { + fileName = logEnv.file; + } + else { + fileName = __dirname + "/.log" + process.pid.toString(); + } } if (logEnv.detailLevel) { detailLevel = logEnv.detailLevel; } + traceToConsole = logEnv.traceToConsole; } - return new Logger(fileName, detailLevel); + return new Logger(fileName, traceToConsole, detailLevel); } function createPollingWatchedFileSet(interval, chunkSize) { if (interval === void 0) { interval = 2500; } @@ -63629,13 +65922,13 @@ var ts; var logger = createLoggerFromEnv(); var pending = []; var canWrite = true; - function writeMessage(s) { + function writeMessage(buf) { if (!canWrite) { - pending.push(s); + pending.push(buf); } else { canWrite = false; - process.stdout.write(new Buffer(s, "utf8"), setCanWriteFlagAndWriteMessageIfNecessary); + process.stdout.write(buf, setCanWriteFlagAndWriteMessageIfNecessary); } } function setCanWriteFlagAndWriteMessageIfNecessary() { @@ -63645,7 +65938,7 @@ var ts; } } var sys = ts.sys; - sys.write = function (s) { return writeMessage(s); }; + sys.write = function (s) { return writeMessage(new Buffer(s, "utf8")); }; sys.watchFile = function (fileName, callback) { var watchedFile = pollingWatchedFileSet.addFile(fileName, callback); return { @@ -63654,14 +65947,42 @@ var ts; }; sys.setTimeout = setTimeout; sys.clearTimeout = clearTimeout; - var ioSession = new IOSession(sys, logger); + sys.setImmediate = setImmediate; + sys.clearImmediate = clearImmediate; + if (typeof global !== "undefined" && global.gc) { + sys.gc = function () { return global.gc(); }; + } + var cancellationToken; + try { + var factory = require("./cancellationToken"); + cancellationToken = factory(sys.args); + } + catch (e) { + cancellationToken = { + isCancellationRequested: function () { return false; } + }; + } + ; + var eventPort; + { + var index = sys.args.indexOf("--eventPort"); + if (index >= 0 && index < sys.args.length - 1) { + var v = parseInt(sys.args[index + 1]); + if (!isNaN(v)) { + eventPort = v; + } + } + } + var useSingleInferredProject = sys.args.indexOf("--useSingleInferredProject") >= 0; + var ioSession = new IOSession(sys, cancellationToken, eventPort, eventPort === undefined, useSingleInferredProject, getGlobalTypingsCacheLocation(), logger); process.on("uncaughtException", function (err) { ioSession.logError(err, "unknown"); }); + process.noAsar = true; ioSession.listen(); })(server = ts.server || (ts.server = {})); })(ts || (ts = {})); -var debugObjectHost = new Function("return this")(); +var debugObjectHost = (function () { return this; })(); var ts; (function (ts) { function logInternalError(logger, err) { @@ -63739,6 +66060,12 @@ var ts; } return this.shimHost.getProjectVersion(); }; + LanguageServiceShimHostAdapter.prototype.getTypeRootsVersion = function () { + if (!this.shimHost.getTypeRootsVersion) { + return 0; + } + return this.shimHost.getTypeRootsVersion(); + }; LanguageServiceShimHostAdapter.prototype.useCaseSensitiveFileNames = function () { return this.shimHost.useCaseSensitiveFileNames ? this.shimHost.useCaseSensitiveFileNames() : false; }; @@ -63932,11 +66259,12 @@ var ts; var LanguageServiceShimObject = (function (_super) { __extends(LanguageServiceShimObject, _super); function LanguageServiceShimObject(factory, host, languageService) { - _super.call(this, factory); - this.host = host; - this.languageService = languageService; - this.logPerformance = false; - this.logger = this.host; + var _this = _super.call(this, factory) || this; + _this.host = host; + _this.languageService = languageService; + _this.logPerformance = false; + _this.logger = _this.host; + return _this; } LanguageServiceShimObject.prototype.forwardJSONCall = function (actionDescription, action) { return forwardJSONCall(this.logger, actionDescription, action, this.logPerformance); @@ -64115,6 +66443,10 @@ var ts; var _this = this; return this.forwardJSONCall("getNavigationBarItems('" + fileName + "')", function () { return _this.languageService.getNavigationBarItems(fileName); }); }; + LanguageServiceShimObject.prototype.getNavigationTree = function (fileName) { + var _this = this; + return this.forwardJSONCall("getNavigationTree('" + fileName + "')", function () { return _this.languageService.getNavigationTree(fileName); }); + }; LanguageServiceShimObject.prototype.getOutliningSpans = function (fileName) { var _this = this; return this.forwardJSONCall("getOutliningSpans('" + fileName + "')", function () { return _this.languageService.getOutliningSpans(fileName); }); @@ -64139,10 +66471,11 @@ var ts; var ClassifierShimObject = (function (_super) { __extends(ClassifierShimObject, _super); function ClassifierShimObject(factory, logger) { - _super.call(this, factory); - this.logger = logger; - this.logPerformance = false; - this.classifier = ts.createClassifier(); + var _this = _super.call(this, factory) || this; + _this.logger = logger; + _this.logPerformance = false; + _this.classifier = ts.createClassifier(); + return _this; } ClassifierShimObject.prototype.getEncodedLexicalClassifications = function (text, lexState, syntacticClassifierAbsent) { var _this = this; @@ -64164,10 +66497,11 @@ var ts; var CoreServicesShimObject = (function (_super) { __extends(CoreServicesShimObject, _super); function CoreServicesShimObject(factory, logger, host) { - _super.call(this, factory); - this.logger = logger; - this.host = host; - this.logPerformance = false; + var _this = _super.call(this, factory) || this; + _this.logger = logger; + _this.host = host; + _this.logPerformance = false; + return _this; } CoreServicesShimObject.prototype.forwardJSONCall = function (actionDescription, action) { return forwardJSONCall(this.logger, actionDescription, action, this.logPerformance); diff --git a/lib/tsserverlibrary.d.ts b/lib/tsserverlibrary.d.ts index 7ed6595bc802c..c23bccf0a6828 100644 --- a/lib/tsserverlibrary.d.ts +++ b/lib/tsserverlibrary.d.ts @@ -1,4 +1,700 @@ -/// +/// +/// +declare namespace ts.server.protocol { + namespace CommandTypes { + type Brace = "brace"; + type BraceFull = "brace-full"; + type BraceCompletion = "braceCompletion"; + type Change = "change"; + type Close = "close"; + type Completions = "completions"; + type CompletionsFull = "completions-full"; + type CompletionDetails = "completionEntryDetails"; + type CompileOnSaveAffectedFileList = "compileOnSaveAffectedFileList"; + type CompileOnSaveEmitFile = "compileOnSaveEmitFile"; + type Configure = "configure"; + type Definition = "definition"; + type DefinitionFull = "definition-full"; + type Implementation = "implementation"; + type ImplementationFull = "implementation-full"; + type Exit = "exit"; + type Format = "format"; + type Formatonkey = "formatonkey"; + type FormatFull = "format-full"; + type FormatonkeyFull = "formatonkey-full"; + type FormatRangeFull = "formatRange-full"; + type Geterr = "geterr"; + type GeterrForProject = "geterrForProject"; + type SemanticDiagnosticsSync = "semanticDiagnosticsSync"; + type SyntacticDiagnosticsSync = "syntacticDiagnosticsSync"; + type NavBar = "navbar"; + type NavBarFull = "navbar-full"; + type Navto = "navto"; + type NavtoFull = "navto-full"; + type NavTree = "navtree"; + type NavTreeFull = "navtree-full"; + type Occurrences = "occurrences"; + type DocumentHighlights = "documentHighlights"; + type DocumentHighlightsFull = "documentHighlights-full"; + type Open = "open"; + type Quickinfo = "quickinfo"; + type QuickinfoFull = "quickinfo-full"; + type References = "references"; + type ReferencesFull = "references-full"; + type Reload = "reload"; + type Rename = "rename"; + type RenameInfoFull = "rename-full"; + type RenameLocationsFull = "renameLocations-full"; + type Saveto = "saveto"; + type SignatureHelp = "signatureHelp"; + type SignatureHelpFull = "signatureHelp-full"; + type TypeDefinition = "typeDefinition"; + type ProjectInfo = "projectInfo"; + type ReloadProjects = "reloadProjects"; + type Unknown = "unknown"; + type OpenExternalProject = "openExternalProject"; + type OpenExternalProjects = "openExternalProjects"; + type CloseExternalProject = "closeExternalProject"; + type SynchronizeProjectList = "synchronizeProjectList"; + type ApplyChangedToOpenFiles = "applyChangedToOpenFiles"; + type EncodedSemanticClassificationsFull = "encodedSemanticClassifications-full"; + type Cleanup = "cleanup"; + type OutliningSpans = "outliningSpans"; + type TodoComments = "todoComments"; + type Indentation = "indentation"; + type DocCommentTemplate = "docCommentTemplate"; + type CompilerOptionsDiagnosticsFull = "compilerOptionsDiagnostics-full"; + type NameOrDottedNameSpan = "nameOrDottedNameSpan"; + type BreakpointStatement = "breakpointStatement"; + type CompilerOptionsForInferredProjects = "compilerOptionsForInferredProjects"; + type GetCodeFixes = "getCodeFixes"; + type GetCodeFixesFull = "getCodeFixes-full"; + type GetSupportedCodeFixes = "getSupportedCodeFixes"; + } + interface Message { + seq: number; + type: string; + } + interface Request extends Message { + command: string; + arguments?: any; + } + interface ReloadProjectsRequest extends Message { + command: CommandTypes.ReloadProjects; + } + interface Event extends Message { + event: string; + body?: any; + } + interface Response extends Message { + request_seq: number; + success: boolean; + command: string; + message?: string; + body?: any; + } + interface FileRequestArgs { + file: string; + projectFileName?: string; + } + interface DocCommentTemplateRequest extends FileLocationRequest { + command: CommandTypes.DocCommentTemplate; + } + interface DocCommandTemplateResponse extends Response { + body?: TextInsertion; + } + interface TodoCommentRequest extends FileRequest { + command: CommandTypes.TodoComments; + arguments: TodoCommentRequestArgs; + } + interface TodoCommentRequestArgs extends FileRequestArgs { + descriptors: TodoCommentDescriptor[]; + } + interface TodoCommentsResponse extends Response { + body?: TodoComment[]; + } + interface OutliningSpansRequest extends FileRequest { + command: CommandTypes.OutliningSpans; + } + interface OutliningSpansResponse extends Response { + body?: OutliningSpan[]; + } + interface IndentationRequest extends FileLocationRequest { + command: CommandTypes.Indentation; + arguments: IndentationRequestArgs; + } + interface IndentationResponse extends Response { + body?: IndentationResult; + } + interface IndentationResult { + position: number; + indentation: number; + } + interface IndentationRequestArgs extends FileLocationRequestArgs { + options?: EditorSettings; + } + interface ProjectInfoRequestArgs extends FileRequestArgs { + needFileNameList: boolean; + } + interface ProjectInfoRequest extends Request { + command: CommandTypes.ProjectInfo; + arguments: ProjectInfoRequestArgs; + } + interface CompilerOptionsDiagnosticsRequest extends Request { + arguments: CompilerOptionsDiagnosticsRequestArgs; + } + interface CompilerOptionsDiagnosticsRequestArgs { + projectFileName: string; + } + interface ProjectInfo { + configFileName: string; + fileNames?: string[]; + languageServiceDisabled?: boolean; + } + interface DiagnosticWithLinePosition { + message: string; + start: number; + length: number; + startLocation: Location; + endLocation: Location; + category: string; + code: number; + } + interface ProjectInfoResponse extends Response { + body?: ProjectInfo; + } + interface FileRequest extends Request { + arguments: FileRequestArgs; + } + interface FileLocationRequestArgs extends FileRequestArgs { + line: number; + offset: number; + position?: number; + } + interface CodeFixRequest extends Request { + command: CommandTypes.GetCodeFixes; + arguments: CodeFixRequestArgs; + } + interface CodeFixRequestArgs extends FileRequestArgs { + startLine: number; + startOffset: number; + startPosition?: number; + endLine: number; + endOffset: number; + endPosition?: number; + errorCodes?: number[]; + } + interface GetCodeFixesResponse extends Response { + body?: CodeAction[]; + } + interface FileLocationRequest extends FileRequest { + arguments: FileLocationRequestArgs; + } + interface GetSupportedCodeFixesRequest extends Request { + command: CommandTypes.GetSupportedCodeFixes; + } + interface GetSupportedCodeFixesResponse extends Response { + body?: string[]; + } + interface EncodedSemanticClassificationsRequest extends FileRequest { + arguments: EncodedSemanticClassificationsRequestArgs; + } + interface EncodedSemanticClassificationsRequestArgs extends FileRequestArgs { + start: number; + length: number; + } + interface DocumentHighlightsRequestArgs extends FileLocationRequestArgs { + filesToSearch: string[]; + } + interface DefinitionRequest extends FileLocationRequest { + command: CommandTypes.Definition; + } + interface TypeDefinitionRequest extends FileLocationRequest { + command: CommandTypes.TypeDefinition; + } + interface ImplementationRequest extends FileLocationRequest { + command: CommandTypes.Implementation; + } + interface Location { + line: number; + offset: number; + } + interface TextSpan { + start: Location; + end: Location; + } + interface FileSpan extends TextSpan { + file: string; + } + interface DefinitionResponse extends Response { + body?: FileSpan[]; + } + interface TypeDefinitionResponse extends Response { + body?: FileSpan[]; + } + interface ImplementationResponse extends Response { + body?: FileSpan[]; + } + interface BraceCompletionRequest extends FileLocationRequest { + command: CommandTypes.BraceCompletion; + arguments: BraceCompletionRequestArgs; + } + interface BraceCompletionRequestArgs extends FileLocationRequestArgs { + openingBrace: string; + } + interface OccurrencesRequest extends FileLocationRequest { + command: CommandTypes.Occurrences; + } + interface OccurrencesResponseItem extends FileSpan { + isWriteAccess: boolean; + } + interface OccurrencesResponse extends Response { + body?: OccurrencesResponseItem[]; + } + interface DocumentHighlightsRequest extends FileLocationRequest { + command: CommandTypes.DocumentHighlights; + arguments: DocumentHighlightsRequestArgs; + } + interface HighlightSpan extends TextSpan { + kind: string; + } + interface DocumentHighlightsItem { + file: string; + highlightSpans: HighlightSpan[]; + } + interface DocumentHighlightsResponse extends Response { + body?: DocumentHighlightsItem[]; + } + interface ReferencesRequest extends FileLocationRequest { + command: CommandTypes.References; + } + interface ReferencesResponseItem extends FileSpan { + lineText: string; + isWriteAccess: boolean; + isDefinition: boolean; + } + interface ReferencesResponseBody { + refs: ReferencesResponseItem[]; + symbolName: string; + symbolStartOffset: number; + symbolDisplayString: string; + } + interface ReferencesResponse extends Response { + body?: ReferencesResponseBody; + } + interface RenameRequestArgs extends FileLocationRequestArgs { + findInComments?: boolean; + findInStrings?: boolean; + } + interface RenameRequest extends FileLocationRequest { + command: CommandTypes.Rename; + arguments: RenameRequestArgs; + } + interface RenameInfo { + canRename: boolean; + localizedErrorMessage?: string; + displayName: string; + fullDisplayName: string; + kind: string; + kindModifiers: string; + } + interface SpanGroup { + file: string; + locs: TextSpan[]; + } + interface RenameResponseBody { + info: RenameInfo; + locs: SpanGroup[]; + } + interface RenameResponse extends Response { + body?: RenameResponseBody; + } + interface ExternalFile { + fileName: string; + scriptKind?: ScriptKind; + hasMixedContent?: boolean; + content?: string; + } + interface ExternalProject { + projectFileName: string; + rootFiles: ExternalFile[]; + options: ExternalProjectCompilerOptions; + typingOptions?: TypingOptions; + } + interface ExternalProjectCompilerOptions extends CompilerOptions { + compileOnSave?: boolean; + } + interface ProjectVersionInfo { + projectName: string; + isInferred: boolean; + version: number; + options: CompilerOptions; + } + interface ProjectChanges { + added: string[]; + removed: string[]; + } + interface ProjectFiles { + info?: ProjectVersionInfo; + files?: string[]; + changes?: ProjectChanges; + } + interface ProjectFilesWithDiagnostics extends ProjectFiles { + projectErrors: DiagnosticWithLinePosition[]; + } + interface ChangedOpenFile { + fileName: string; + changes: ts.TextChange[]; + } + interface ConfigureRequestArguments { + hostInfo?: string; + file?: string; + formatOptions?: FormatCodeSettings; + } + interface ConfigureRequest extends Request { + command: CommandTypes.Configure; + arguments: ConfigureRequestArguments; + } + interface ConfigureResponse extends Response { + } + interface OpenRequestArgs extends FileRequestArgs { + fileContent?: string; + scriptKindName?: "TS" | "JS" | "TSX" | "JSX"; + } + interface OpenRequest extends Request { + command: CommandTypes.Open; + arguments: OpenRequestArgs; + } + interface OpenExternalProjectRequest extends Request { + command: CommandTypes.OpenExternalProject; + arguments: OpenExternalProjectArgs; + } + type OpenExternalProjectArgs = ExternalProject; + interface OpenExternalProjectsRequest extends Request { + command: CommandTypes.OpenExternalProjects; + arguments: OpenExternalProjectsArgs; + } + interface OpenExternalProjectsArgs { + projects: ExternalProject[]; + } + interface OpenExternalProjectResponse extends Response { + } + interface OpenExternalProjectsResponse extends Response { + } + interface CloseExternalProjectRequest extends Request { + command: CommandTypes.CloseExternalProject; + arguments: CloseExternalProjectRequestArgs; + } + interface CloseExternalProjectRequestArgs { + projectFileName: string; + } + interface CloseExternalProjectResponse extends Response { + } + interface SynchronizeProjectListRequest extends Request { + arguments: SynchronizeProjectListRequestArgs; + } + interface SynchronizeProjectListRequestArgs { + knownProjects: protocol.ProjectVersionInfo[]; + } + interface ApplyChangedToOpenFilesRequest extends Request { + arguments: ApplyChangedToOpenFilesRequestArgs; + } + interface ApplyChangedToOpenFilesRequestArgs { + openFiles?: ExternalFile[]; + changedFiles?: ChangedOpenFile[]; + closedFiles?: string[]; + } + interface SetCompilerOptionsForInferredProjectsRequest extends Request { + command: CommandTypes.CompilerOptionsForInferredProjects; + arguments: SetCompilerOptionsForInferredProjectsArgs; + } + interface SetCompilerOptionsForInferredProjectsArgs { + options: ExternalProjectCompilerOptions; + } + interface SetCompilerOptionsForInferredProjectsResponse extends Response { + } + interface ExitRequest extends Request { + command: CommandTypes.Exit; + } + interface CloseRequest extends FileRequest { + command: CommandTypes.Close; + } + interface CompileOnSaveAffectedFileListRequest extends FileRequest { + command: CommandTypes.CompileOnSaveAffectedFileList; + } + interface CompileOnSaveAffectedFileListSingleProject { + projectFileName: string; + fileNames: string[]; + } + interface CompileOnSaveAffectedFileListResponse extends Response { + body: CompileOnSaveAffectedFileListSingleProject[]; + } + interface CompileOnSaveEmitFileRequest extends FileRequest { + command: CommandTypes.CompileOnSaveEmitFile; + arguments: CompileOnSaveEmitFileRequestArgs; + } + interface CompileOnSaveEmitFileRequestArgs extends FileRequestArgs { + forced?: boolean; + } + interface QuickInfoRequest extends FileLocationRequest { + command: CommandTypes.Quickinfo; + } + interface QuickInfoResponseBody { + kind: string; + kindModifiers: string; + start: Location; + end: Location; + displayString: string; + documentation: string; + } + interface QuickInfoResponse extends Response { + body?: QuickInfoResponseBody; + } + interface FormatRequestArgs extends FileLocationRequestArgs { + endLine: number; + endOffset: number; + endPosition?: number; + options?: FormatCodeSettings; + } + interface FormatRequest extends FileLocationRequest { + command: CommandTypes.Format; + arguments: FormatRequestArgs; + } + interface CodeEdit { + start: Location; + end: Location; + newText: string; + } + interface FileCodeEdits { + fileName: string; + textChanges: CodeEdit[]; + } + interface CodeFixResponse extends Response { + body?: CodeAction[]; + } + interface CodeAction { + description: string; + changes: FileCodeEdits[]; + } + interface FormatResponse extends Response { + body?: CodeEdit[]; + } + interface FormatOnKeyRequestArgs extends FileLocationRequestArgs { + key: string; + options?: FormatCodeSettings; + } + interface FormatOnKeyRequest extends FileLocationRequest { + command: CommandTypes.Formatonkey; + arguments: FormatOnKeyRequestArgs; + } + interface CompletionsRequestArgs extends FileLocationRequestArgs { + prefix?: string; + } + interface CompletionsRequest extends FileLocationRequest { + command: CommandTypes.Completions; + arguments: CompletionsRequestArgs; + } + interface CompletionDetailsRequestArgs extends FileLocationRequestArgs { + entryNames: string[]; + } + interface CompletionDetailsRequest extends FileLocationRequest { + command: CommandTypes.CompletionDetails; + arguments: CompletionDetailsRequestArgs; + } + interface SymbolDisplayPart { + text: string; + kind: string; + } + interface CompletionEntry { + name: string; + kind: string; + kindModifiers: string; + sortText: string; + replacementSpan?: TextSpan; + } + interface CompletionEntryDetails { + name: string; + kind: string; + kindModifiers: string; + displayParts: SymbolDisplayPart[]; + documentation: SymbolDisplayPart[]; + } + interface CompletionsResponse extends Response { + body?: CompletionEntry[]; + } + interface CompletionDetailsResponse extends Response { + body?: CompletionEntryDetails[]; + } + interface SignatureHelpParameter { + name: string; + documentation: SymbolDisplayPart[]; + displayParts: SymbolDisplayPart[]; + isOptional: boolean; + } + interface SignatureHelpItem { + isVariadic: boolean; + prefixDisplayParts: SymbolDisplayPart[]; + suffixDisplayParts: SymbolDisplayPart[]; + separatorDisplayParts: SymbolDisplayPart[]; + parameters: SignatureHelpParameter[]; + documentation: SymbolDisplayPart[]; + } + interface SignatureHelpItems { + items: SignatureHelpItem[]; + applicableSpan: TextSpan; + selectedItemIndex: number; + argumentIndex: number; + argumentCount: number; + } + interface SignatureHelpRequestArgs extends FileLocationRequestArgs { + } + interface SignatureHelpRequest extends FileLocationRequest { + command: CommandTypes.SignatureHelp; + arguments: SignatureHelpRequestArgs; + } + interface SignatureHelpResponse extends Response { + body?: SignatureHelpItems; + } + interface SemanticDiagnosticsSyncRequest extends FileRequest { + command: CommandTypes.SemanticDiagnosticsSync; + arguments: SemanticDiagnosticsSyncRequestArgs; + } + interface SemanticDiagnosticsSyncRequestArgs extends FileRequestArgs { + includeLinePosition?: boolean; + } + interface SemanticDiagnosticsSyncResponse extends Response { + body?: Diagnostic[] | DiagnosticWithLinePosition[]; + } + interface SyntacticDiagnosticsSyncRequest extends FileRequest { + command: CommandTypes.SyntacticDiagnosticsSync; + arguments: SyntacticDiagnosticsSyncRequestArgs; + } + interface SyntacticDiagnosticsSyncRequestArgs extends FileRequestArgs { + includeLinePosition?: boolean; + } + interface SyntacticDiagnosticsSyncResponse extends Response { + body?: Diagnostic[] | DiagnosticWithLinePosition[]; + } + interface GeterrForProjectRequestArgs { + file: string; + delay: number; + } + interface GeterrForProjectRequest extends Request { + command: CommandTypes.GeterrForProject; + arguments: GeterrForProjectRequestArgs; + } + interface GeterrRequestArgs { + files: string[]; + delay: number; + } + interface GeterrRequest extends Request { + command: CommandTypes.Geterr; + arguments: GeterrRequestArgs; + } + interface Diagnostic { + start: Location; + end: Location; + text: string; + code?: number; + } + interface DiagnosticEventBody { + file: string; + diagnostics: Diagnostic[]; + } + interface DiagnosticEvent extends Event { + body?: DiagnosticEventBody; + } + interface ConfigFileDiagnosticEventBody { + triggerFile: string; + configFile: string; + diagnostics: Diagnostic[]; + } + interface ConfigFileDiagnosticEvent extends Event { + body?: ConfigFileDiagnosticEventBody; + event: "configFileDiag"; + } + interface ReloadRequestArgs extends FileRequestArgs { + tmpfile: string; + } + interface ReloadRequest extends FileRequest { + command: CommandTypes.Reload; + arguments: ReloadRequestArgs; + } + interface ReloadResponse extends Response { + } + interface SavetoRequestArgs extends FileRequestArgs { + tmpfile: string; + } + interface SavetoRequest extends FileRequest { + command: CommandTypes.Saveto; + arguments: SavetoRequestArgs; + } + interface NavtoRequestArgs extends FileRequestArgs { + searchValue: string; + maxResultCount?: number; + currentFileOnly?: boolean; + projectFileName?: string; + } + interface NavtoRequest extends FileRequest { + command: CommandTypes.Navto; + arguments: NavtoRequestArgs; + } + interface NavtoItem { + name: string; + kind: string; + matchKind?: string; + isCaseSensitive?: boolean; + kindModifiers?: string; + file: string; + start: Location; + end: Location; + containerName?: string; + containerKind?: string; + } + interface NavtoResponse extends Response { + body?: NavtoItem[]; + } + interface ChangeRequestArgs extends FormatRequestArgs { + insertString?: string; + } + interface ChangeRequest extends FileLocationRequest { + command: CommandTypes.Change; + arguments: ChangeRequestArgs; + } + interface BraceResponse extends Response { + body?: TextSpan[]; + } + interface BraceRequest extends FileLocationRequest { + command: CommandTypes.Brace; + } + interface NavBarRequest extends FileRequest { + command: CommandTypes.NavBar; + } + interface NavTreeRequest extends FileRequest { + command: CommandTypes.NavTree; + } + interface NavigationBarItem { + text: string; + kind: string; + kindModifiers?: string; + spans: TextSpan[]; + childItems?: NavigationBarItem[]; + indent: number; + } + interface NavigationTree { + text: string; + kind: string; + kindModifiers: string; + spans: TextSpan[]; + childItems?: NavigationTree[]; + } + interface NavBarResponse extends Response { + body?: NavigationBarItem[]; + } + interface NavTreeResponse extends Response { + body?: NavigationTree; + } +} declare namespace ts { interface MapLike { [index: string]: T; @@ -15,6 +711,7 @@ declare namespace ts { contains(fileName: Path): boolean; remove(fileName: Path): void; forEachValue(f: (key: Path, v: T) => void): void; + getKeys(): Path[]; clear(): void; } interface TextRange { @@ -374,7 +1071,6 @@ declare namespace ts { ContextFlags = 1540096, TypeExcludesFlags = 327680, } - type ModifiersArray = NodeArray; const enum ModifierFlags { None = 0, Export = 1, @@ -421,19 +1117,24 @@ declare namespace ts { nextContainer?: Node; localSymbol?: Symbol; flowNode?: FlowNode; - transformId?: number; - emitFlags?: NodeEmitFlags; - sourceMapRange?: TextRange; - commentRange?: TextRange; + emitNode?: EmitNode; } interface NodeArray extends Array, TextRange { hasTrailingComma?: boolean; } - interface Token extends Node { - __tokenTag: any; - } - interface Modifier extends Token { - } + interface Token extends Node { + kind: TKind; + } + type DotDotDotToken = Token; + type QuestionToken = Token; + type ColonToken = Token; + type EqualsToken = Token; + type AsteriskToken = Token; + type EqualsGreaterThanToken = Token; + type EndOfFileToken = Token; + type AtToken = Token; + type Modifier = Token | Token | Token | Token | Token | Token | Token | Token | Token | Token | Token; + type ModifiersArray = NodeArray; const enum GeneratedIdentifierKind { None = 0, Auto = 1, @@ -442,6 +1143,7 @@ declare namespace ts { Node = 4, } interface Identifier extends PrimaryExpression { + kind: SyntaxKind.Identifier; text: string; originalKeywordKind?: SyntaxKind; autoGenerateKind?: GeneratedIdentifierKind; @@ -451,6 +1153,7 @@ declare namespace ts { resolvedSymbol: Symbol; } interface QualifiedName extends Node { + kind: SyntaxKind.QualifiedName; left: EntityName; right: Identifier; } @@ -462,15 +1165,18 @@ declare namespace ts { name?: DeclarationName; } interface DeclarationStatement extends Declaration, Statement { - name?: Identifier; + name?: Identifier | LiteralExpression; } interface ComputedPropertyName extends Node { + kind: SyntaxKind.ComputedPropertyName; expression: Expression; } interface Decorator extends Node { + kind: SyntaxKind.Decorator; expression: LeftHandSideExpression; } interface TypeParameterDeclaration extends Declaration { + kind: SyntaxKind.TypeParameter; name: Identifier; constraint?: TypeNode; expression?: Expression; @@ -482,40 +1188,48 @@ declare namespace ts { type?: TypeNode; } interface CallSignatureDeclaration extends SignatureDeclaration, TypeElement { + kind: SyntaxKind.CallSignature; } interface ConstructSignatureDeclaration extends SignatureDeclaration, TypeElement { + kind: SyntaxKind.ConstructSignature; } type BindingName = Identifier | BindingPattern; interface VariableDeclaration extends Declaration { + kind: SyntaxKind.VariableDeclaration; parent?: VariableDeclarationList; name: BindingName; type?: TypeNode; initializer?: Expression; } interface VariableDeclarationList extends Node { + kind: SyntaxKind.VariableDeclarationList; declarations: NodeArray; } interface ParameterDeclaration extends Declaration { - dotDotDotToken?: Node; + kind: SyntaxKind.Parameter; + dotDotDotToken?: DotDotDotToken; name: BindingName; - questionToken?: Node; + questionToken?: QuestionToken; type?: TypeNode; initializer?: Expression; } interface BindingElement extends Declaration { + kind: SyntaxKind.BindingElement; propertyName?: PropertyName; - dotDotDotToken?: Node; + dotDotDotToken?: DotDotDotToken; name: BindingName; initializer?: Expression; } interface PropertySignature extends TypeElement { + kind: SyntaxKind.PropertySignature | SyntaxKind.JSDocRecordMember; name: PropertyName; - questionToken?: Node; + questionToken?: QuestionToken; type?: TypeNode; initializer?: Expression; } interface PropertyDeclaration extends ClassElement { - questionToken?: Node; + kind: SyntaxKind.PropertyDeclaration; + questionToken?: QuestionToken; name: PropertyName; type?: TypeNode; initializer?: Expression; @@ -526,22 +1240,23 @@ declare namespace ts { } type ObjectLiteralElementLike = PropertyAssignment | ShorthandPropertyAssignment | MethodDeclaration | AccessorDeclaration; interface PropertyAssignment extends ObjectLiteralElement { - _propertyAssignmentBrand: any; + kind: SyntaxKind.PropertyAssignment; name: PropertyName; - questionToken?: Node; + questionToken?: QuestionToken; initializer: Expression; } interface ShorthandPropertyAssignment extends ObjectLiteralElement { + kind: SyntaxKind.ShorthandPropertyAssignment; name: Identifier; - questionToken?: Node; - equalsToken?: Node; + questionToken?: QuestionToken; + equalsToken?: Token; objectAssignmentInitializer?: Expression; } interface VariableLikeDeclaration extends Declaration { propertyName?: PropertyName; - dotDotDotToken?: Node; + dotDotDotToken?: DotDotDotToken; name: DeclarationName; - questionToken?: Node; + questionToken?: QuestionToken; type?: TypeNode; initializer?: Expression; } @@ -552,96 +1267,119 @@ declare namespace ts { elements: NodeArray; } interface ObjectBindingPattern extends BindingPattern { + kind: SyntaxKind.ObjectBindingPattern; elements: NodeArray; } type ArrayBindingElement = BindingElement | OmittedExpression; interface ArrayBindingPattern extends BindingPattern { + kind: SyntaxKind.ArrayBindingPattern; elements: NodeArray; } interface FunctionLikeDeclaration extends SignatureDeclaration { _functionLikeDeclarationBrand: any; - asteriskToken?: Node; - questionToken?: Node; + asteriskToken?: AsteriskToken; + questionToken?: QuestionToken; body?: Block | Expression; } interface FunctionDeclaration extends FunctionLikeDeclaration, DeclarationStatement { + kind: SyntaxKind.FunctionDeclaration; name?: Identifier; body?: FunctionBody; } interface MethodSignature extends SignatureDeclaration, TypeElement { + kind: SyntaxKind.MethodSignature; name: PropertyName; } interface MethodDeclaration extends FunctionLikeDeclaration, ClassElement, ObjectLiteralElement { + kind: SyntaxKind.MethodDeclaration; name: PropertyName; body?: FunctionBody; } interface ConstructorDeclaration extends FunctionLikeDeclaration, ClassElement { + kind: SyntaxKind.Constructor; body?: FunctionBody; } interface SemicolonClassElement extends ClassElement { - _semicolonClassElementBrand: any; + kind: SyntaxKind.SemicolonClassElement; } - interface AccessorDeclaration extends FunctionLikeDeclaration, ClassElement, ObjectLiteralElement { - _accessorDeclarationBrand: any; + interface GetAccessorDeclaration extends FunctionLikeDeclaration, ClassElement, ObjectLiteralElement { + kind: SyntaxKind.GetAccessor; name: PropertyName; body: FunctionBody; } - interface GetAccessorDeclaration extends AccessorDeclaration { - } - interface SetAccessorDeclaration extends AccessorDeclaration { + interface SetAccessorDeclaration extends FunctionLikeDeclaration, ClassElement, ObjectLiteralElement { + kind: SyntaxKind.SetAccessor; + name: PropertyName; + body: FunctionBody; } + type AccessorDeclaration = GetAccessorDeclaration | SetAccessorDeclaration; interface IndexSignatureDeclaration extends SignatureDeclaration, ClassElement, TypeElement { - _indexSignatureDeclarationBrand: any; + kind: SyntaxKind.IndexSignature; } interface TypeNode extends Node { _typeNodeBrand: any; } + interface KeywordTypeNode extends TypeNode { + kind: SyntaxKind.AnyKeyword | SyntaxKind.NumberKeyword | SyntaxKind.BooleanKeyword | SyntaxKind.StringKeyword | SyntaxKind.SymbolKeyword | SyntaxKind.VoidKeyword; + } interface ThisTypeNode extends TypeNode { - _thisTypeNodeBrand: any; + kind: SyntaxKind.ThisType; } interface FunctionOrConstructorTypeNode extends TypeNode, SignatureDeclaration { - _functionOrConstructorTypeNodeBrand: any; + kind: SyntaxKind.FunctionType | SyntaxKind.ConstructorType; } interface FunctionTypeNode extends FunctionOrConstructorTypeNode { + kind: SyntaxKind.FunctionType; } interface ConstructorTypeNode extends FunctionOrConstructorTypeNode { + kind: SyntaxKind.ConstructorType; } interface TypeReferenceNode extends TypeNode { + kind: SyntaxKind.TypeReference; typeName: EntityName; typeArguments?: NodeArray; } interface TypePredicateNode extends TypeNode { + kind: SyntaxKind.TypePredicate; parameterName: Identifier | ThisTypeNode; type: TypeNode; } interface TypeQueryNode extends TypeNode { + kind: SyntaxKind.TypeQuery; exprName: EntityName; } interface TypeLiteralNode extends TypeNode, Declaration { + kind: SyntaxKind.TypeLiteral; members: NodeArray; } interface ArrayTypeNode extends TypeNode { + kind: SyntaxKind.ArrayType; elementType: TypeNode; } interface TupleTypeNode extends TypeNode { + kind: SyntaxKind.TupleType; elementTypes: NodeArray; } interface UnionOrIntersectionTypeNode extends TypeNode { + kind: SyntaxKind.UnionType | SyntaxKind.IntersectionType; types: NodeArray; } interface UnionTypeNode extends UnionOrIntersectionTypeNode { + kind: SyntaxKind.UnionType; } interface IntersectionTypeNode extends UnionOrIntersectionTypeNode { + kind: SyntaxKind.IntersectionType; } interface ParenthesizedTypeNode extends TypeNode { + kind: SyntaxKind.ParenthesizedType; type: TypeNode; } interface LiteralTypeNode extends TypeNode { - _stringLiteralTypeBrand: any; + kind: SyntaxKind.LiteralType; literal: Expression; } interface StringLiteral extends LiteralExpression { - _stringLiteralBrand: any; + kind: SyntaxKind.StringLiteral; textSourceNode?: Identifier | StringLiteral; } interface Expression extends Node { @@ -649,9 +1387,10 @@ declare namespace ts { contextualType?: Type; } interface OmittedExpression extends Expression { - _omittedExpressionBrand: any; + kind: SyntaxKind.OmittedExpression; } interface PartiallyEmittedExpression extends LeftHandSideExpression { + kind: SyntaxKind.PartiallyEmittedExpression; expression: Expression; } interface UnaryExpression extends Expression { @@ -660,13 +1399,17 @@ declare namespace ts { interface IncrementExpression extends UnaryExpression { _incrementExpressionBrand: any; } + type PrefixUnaryOperator = SyntaxKind.PlusPlusToken | SyntaxKind.MinusMinusToken | SyntaxKind.PlusToken | SyntaxKind.MinusToken | SyntaxKind.TildeToken | SyntaxKind.ExclamationToken; interface PrefixUnaryExpression extends IncrementExpression { - operator: SyntaxKind; + kind: SyntaxKind.PrefixUnaryExpression; + operator: PrefixUnaryOperator; operand: UnaryExpression; } + type PostfixUnaryOperator = SyntaxKind.PlusPlusToken | SyntaxKind.MinusMinusToken; interface PostfixUnaryExpression extends IncrementExpression { + kind: SyntaxKind.PostfixUnaryExpression; operand: LeftHandSideExpression; - operator: SyntaxKind; + operator: PostfixUnaryOperator; } interface PostfixExpression extends UnaryExpression { _postfixExpressionBrand: any; @@ -680,42 +1423,83 @@ declare namespace ts { interface PrimaryExpression extends MemberExpression { _primaryExpressionBrand: any; } + interface NullLiteral extends PrimaryExpression { + kind: SyntaxKind.NullKeyword; + } + interface BooleanLiteral extends PrimaryExpression { + kind: SyntaxKind.TrueKeyword | SyntaxKind.FalseKeyword; + } + interface ThisExpression extends PrimaryExpression { + kind: SyntaxKind.ThisKeyword; + } + interface SuperExpression extends PrimaryExpression { + kind: SyntaxKind.SuperKeyword; + } interface DeleteExpression extends UnaryExpression { + kind: SyntaxKind.DeleteExpression; expression: UnaryExpression; } interface TypeOfExpression extends UnaryExpression { + kind: SyntaxKind.TypeOfExpression; expression: UnaryExpression; } interface VoidExpression extends UnaryExpression { + kind: SyntaxKind.VoidExpression; expression: UnaryExpression; } interface AwaitExpression extends UnaryExpression { + kind: SyntaxKind.AwaitExpression; expression: UnaryExpression; } interface YieldExpression extends Expression { - asteriskToken?: Node; + kind: SyntaxKind.YieldExpression; + asteriskToken?: AsteriskToken; expression?: Expression; } + type ExponentiationOperator = SyntaxKind.AsteriskAsteriskToken; + type MultiplicativeOperator = SyntaxKind.AsteriskToken | SyntaxKind.SlashToken | SyntaxKind.PercentToken; + type MultiplicativeOperatorOrHigher = ExponentiationOperator | MultiplicativeOperator; + type AdditiveOperator = SyntaxKind.PlusToken | SyntaxKind.MinusToken; + type AdditiveOperatorOrHigher = MultiplicativeOperatorOrHigher | AdditiveOperator; + type ShiftOperator = SyntaxKind.LessThanLessThanToken | SyntaxKind.GreaterThanGreaterThanToken | SyntaxKind.GreaterThanGreaterThanGreaterThanToken; + type ShiftOperatorOrHigher = AdditiveOperatorOrHigher | ShiftOperator; + type RelationalOperator = SyntaxKind.LessThanToken | SyntaxKind.LessThanEqualsToken | SyntaxKind.GreaterThanToken | SyntaxKind.GreaterThanEqualsToken | SyntaxKind.InstanceOfKeyword | SyntaxKind.InKeyword; + type RelationalOperatorOrHigher = ShiftOperatorOrHigher | RelationalOperator; + type EqualityOperator = SyntaxKind.EqualsEqualsToken | SyntaxKind.EqualsEqualsEqualsToken | SyntaxKind.ExclamationEqualsEqualsToken | SyntaxKind.ExclamationEqualsToken; + type EqualityOperatorOrHigher = RelationalOperatorOrHigher | EqualityOperator; + type BitwiseOperator = SyntaxKind.AmpersandToken | SyntaxKind.BarToken | SyntaxKind.CaretToken; + type BitwiseOperatorOrHigher = EqualityOperatorOrHigher | BitwiseOperator; + type LogicalOperator = SyntaxKind.AmpersandAmpersandToken | SyntaxKind.BarBarToken; + type LogicalOperatorOrHigher = BitwiseOperatorOrHigher | LogicalOperator; + type CompoundAssignmentOperator = SyntaxKind.PlusEqualsToken | SyntaxKind.MinusEqualsToken | SyntaxKind.AsteriskAsteriskEqualsToken | SyntaxKind.AsteriskEqualsToken | SyntaxKind.SlashEqualsToken | SyntaxKind.PercentEqualsToken | SyntaxKind.AmpersandEqualsToken | SyntaxKind.BarEqualsToken | SyntaxKind.CaretEqualsToken | SyntaxKind.LessThanLessThanEqualsToken | SyntaxKind.GreaterThanGreaterThanGreaterThanEqualsToken | SyntaxKind.GreaterThanGreaterThanEqualsToken; + type AssignmentOperator = SyntaxKind.EqualsToken | CompoundAssignmentOperator; + type AssignmentOperatorOrHigher = LogicalOperatorOrHigher | AssignmentOperator; + type BinaryOperator = AssignmentOperatorOrHigher | SyntaxKind.CommaToken; + type BinaryOperatorToken = Token; interface BinaryExpression extends Expression, Declaration { + kind: SyntaxKind.BinaryExpression; left: Expression; - operatorToken: Node; + operatorToken: BinaryOperatorToken; right: Expression; } interface ConditionalExpression extends Expression { + kind: SyntaxKind.ConditionalExpression; condition: Expression; - questionToken: Node; + questionToken: QuestionToken; whenTrue: Expression; - colonToken: Node; + colonToken: ColonToken; whenFalse: Expression; } type FunctionBody = Block; type ConciseBody = FunctionBody | Expression; interface FunctionExpression extends PrimaryExpression, FunctionLikeDeclaration { + kind: SyntaxKind.FunctionExpression; name?: Identifier; body: FunctionBody; } interface ArrowFunction extends Expression, FunctionLikeDeclaration { - equalsGreaterThanToken: Node; + kind: SyntaxKind.ArrowFunction; + equalsGreaterThanToken: EqualsGreaterThanToken; body: ConciseBody; } interface LiteralLikeNode extends Node { @@ -727,137 +1511,192 @@ declare namespace ts { interface LiteralExpression extends LiteralLikeNode, PrimaryExpression { _literalExpressionBrand: any; } + interface RegularExpressionLiteral extends LiteralExpression { + kind: SyntaxKind.RegularExpressionLiteral; + } + interface NoSubstitutionTemplateLiteral extends LiteralExpression { + kind: SyntaxKind.NoSubstitutionTemplateLiteral; + } interface NumericLiteral extends LiteralExpression { - _numericLiteralBrand: any; + kind: SyntaxKind.NumericLiteral; trailingComment?: string; } - interface TemplateLiteralFragment extends LiteralLikeNode { - _templateLiteralFragmentBrand: any; + interface TemplateHead extends LiteralLikeNode { + kind: SyntaxKind.TemplateHead; + } + interface TemplateMiddle extends LiteralLikeNode { + kind: SyntaxKind.TemplateMiddle; + } + interface TemplateTail extends LiteralLikeNode { + kind: SyntaxKind.TemplateTail; } - type Template = TemplateExpression | LiteralExpression; + type TemplateLiteral = TemplateExpression | NoSubstitutionTemplateLiteral; interface TemplateExpression extends PrimaryExpression { - head: TemplateLiteralFragment; + kind: SyntaxKind.TemplateExpression; + head: TemplateHead; templateSpans: NodeArray; } interface TemplateSpan extends Node { + kind: SyntaxKind.TemplateSpan; expression: Expression; - literal: TemplateLiteralFragment; + literal: TemplateMiddle | TemplateTail; } interface ParenthesizedExpression extends PrimaryExpression { + kind: SyntaxKind.ParenthesizedExpression; expression: Expression; } interface ArrayLiteralExpression extends PrimaryExpression { + kind: SyntaxKind.ArrayLiteralExpression; elements: NodeArray; multiLine?: boolean; } interface SpreadElementExpression extends Expression { + kind: SyntaxKind.SpreadElementExpression; expression: Expression; } interface ObjectLiteralExpressionBase extends PrimaryExpression, Declaration { properties: NodeArray; } interface ObjectLiteralExpression extends ObjectLiteralExpressionBase { + kind: SyntaxKind.ObjectLiteralExpression; multiLine?: boolean; } type EntityNameExpression = Identifier | PropertyAccessEntityNameExpression; type EntityNameOrEntityNameExpression = EntityName | EntityNameExpression; interface PropertyAccessExpression extends MemberExpression, Declaration { + kind: SyntaxKind.PropertyAccessExpression; expression: LeftHandSideExpression; name: Identifier; } + interface SuperPropertyAccessExpression extends PropertyAccessExpression { + expression: SuperExpression; + } interface PropertyAccessEntityNameExpression extends PropertyAccessExpression { _propertyAccessExpressionLikeQualifiedNameBrand?: any; expression: EntityNameExpression; } interface ElementAccessExpression extends MemberExpression { + kind: SyntaxKind.ElementAccessExpression; expression: LeftHandSideExpression; argumentExpression?: Expression; } + interface SuperElementAccessExpression extends ElementAccessExpression { + expression: SuperExpression; + } + type SuperProperty = SuperPropertyAccessExpression | SuperElementAccessExpression; interface CallExpression extends LeftHandSideExpression, Declaration { + kind: SyntaxKind.CallExpression; expression: LeftHandSideExpression; typeArguments?: NodeArray; arguments: NodeArray; } + interface SuperCall extends CallExpression { + expression: SuperExpression; + } interface ExpressionWithTypeArguments extends TypeNode { + kind: SyntaxKind.ExpressionWithTypeArguments; expression: LeftHandSideExpression; typeArguments?: NodeArray; } - interface NewExpression extends CallExpression, PrimaryExpression { + interface NewExpression extends PrimaryExpression, Declaration { + kind: SyntaxKind.NewExpression; + expression: LeftHandSideExpression; + typeArguments?: NodeArray; + arguments: NodeArray; } interface TaggedTemplateExpression extends MemberExpression { + kind: SyntaxKind.TaggedTemplateExpression; tag: LeftHandSideExpression; - template: Template; + template: TemplateLiteral; } type CallLikeExpression = CallExpression | NewExpression | TaggedTemplateExpression | Decorator; interface AsExpression extends Expression { + kind: SyntaxKind.AsExpression; expression: Expression; type: TypeNode; } interface TypeAssertion extends UnaryExpression { + kind: SyntaxKind.TypeAssertionExpression; type: TypeNode; expression: UnaryExpression; } type AssertionExpression = TypeAssertion | AsExpression; interface NonNullExpression extends LeftHandSideExpression { + kind: SyntaxKind.NonNullExpression; expression: Expression; } interface JsxElement extends PrimaryExpression { + kind: SyntaxKind.JsxElement; openingElement: JsxOpeningElement; children: NodeArray; closingElement: JsxClosingElement; } type JsxTagNameExpression = PrimaryExpression | PropertyAccessExpression; interface JsxOpeningElement extends Expression { - _openingElementBrand?: any; + kind: SyntaxKind.JsxOpeningElement; tagName: JsxTagNameExpression; attributes: NodeArray; } - interface JsxSelfClosingElement extends PrimaryExpression, JsxOpeningElement { - _selfClosingElementBrand?: any; + interface JsxSelfClosingElement extends PrimaryExpression { + kind: SyntaxKind.JsxSelfClosingElement; + tagName: JsxTagNameExpression; + attributes: NodeArray; } type JsxOpeningLikeElement = JsxSelfClosingElement | JsxOpeningElement; type JsxAttributeLike = JsxAttribute | JsxSpreadAttribute; interface JsxAttribute extends Node { + kind: SyntaxKind.JsxAttribute; name: Identifier; initializer?: StringLiteral | JsxExpression; } interface JsxSpreadAttribute extends Node { + kind: SyntaxKind.JsxSpreadAttribute; expression: Expression; } interface JsxClosingElement extends Node { + kind: SyntaxKind.JsxClosingElement; tagName: JsxTagNameExpression; } interface JsxExpression extends Expression { + kind: SyntaxKind.JsxExpression; expression?: Expression; } interface JsxText extends Node { - _jsxTextExpressionBrand: any; + kind: SyntaxKind.JsxText; } type JsxChild = JsxText | JsxExpression | JsxElement | JsxSelfClosingElement; interface Statement extends Node { _statementBrand: any; } interface NotEmittedStatement extends Statement { + kind: SyntaxKind.NotEmittedStatement; } interface EmptyStatement extends Statement { + kind: SyntaxKind.EmptyStatement; } interface DebuggerStatement extends Statement { + kind: SyntaxKind.DebuggerStatement; } interface MissingDeclaration extends DeclarationStatement, ClassElement, ObjectLiteralElement, TypeElement { + kind: SyntaxKind.MissingDeclaration; name?: Identifier; } type BlockLike = SourceFile | Block | ModuleBlock | CaseClause; interface Block extends Statement { + kind: SyntaxKind.Block; statements: NodeArray; multiLine?: boolean; } interface VariableStatement extends Statement { + kind: SyntaxKind.VariableStatement; declarationList: VariableDeclarationList; } interface ExpressionStatement extends Statement { + kind: SyntaxKind.ExpressionStatement; expression: Expression; } interface IfStatement extends Statement { + kind: SyntaxKind.IfStatement; expression: Expression; thenStatement: Statement; elseStatement?: Statement; @@ -866,68 +1705,85 @@ declare namespace ts { statement: Statement; } interface DoStatement extends IterationStatement { + kind: SyntaxKind.DoStatement; expression: Expression; } interface WhileStatement extends IterationStatement { + kind: SyntaxKind.WhileStatement; expression: Expression; } type ForInitializer = VariableDeclarationList | Expression; interface ForStatement extends IterationStatement { + kind: SyntaxKind.ForStatement; initializer?: ForInitializer; condition?: Expression; incrementor?: Expression; } interface ForInStatement extends IterationStatement { + kind: SyntaxKind.ForInStatement; initializer: ForInitializer; expression: Expression; } interface ForOfStatement extends IterationStatement { + kind: SyntaxKind.ForOfStatement; initializer: ForInitializer; expression: Expression; } interface BreakStatement extends Statement { + kind: SyntaxKind.BreakStatement; label?: Identifier; } interface ContinueStatement extends Statement { + kind: SyntaxKind.ContinueStatement; label?: Identifier; } type BreakOrContinueStatement = BreakStatement | ContinueStatement; interface ReturnStatement extends Statement { + kind: SyntaxKind.ReturnStatement; expression?: Expression; } interface WithStatement extends Statement { + kind: SyntaxKind.WithStatement; expression: Expression; statement: Statement; } interface SwitchStatement extends Statement { + kind: SyntaxKind.SwitchStatement; expression: Expression; caseBlock: CaseBlock; possiblyExhaustive?: boolean; } interface CaseBlock extends Node { + kind: SyntaxKind.CaseBlock; clauses: NodeArray; } interface CaseClause extends Node { + kind: SyntaxKind.CaseClause; expression: Expression; statements: NodeArray; } interface DefaultClause extends Node { + kind: SyntaxKind.DefaultClause; statements: NodeArray; } type CaseOrDefaultClause = CaseClause | DefaultClause; interface LabeledStatement extends Statement { + kind: SyntaxKind.LabeledStatement; label: Identifier; statement: Statement; } interface ThrowStatement extends Statement { + kind: SyntaxKind.ThrowStatement; expression: Expression; } interface TryStatement extends Statement { + kind: SyntaxKind.TryStatement; tryBlock: Block; catchClause?: CatchClause; finallyBlock?: Block; } interface CatchClause extends Node { + kind: SyntaxKind.CatchClause; variableDeclaration: VariableDeclaration; block: Block; } @@ -939,9 +1795,11 @@ declare namespace ts { members: NodeArray; } interface ClassDeclaration extends ClassLikeDeclaration, DeclarationStatement { + kind: SyntaxKind.ClassDeclaration; name?: Identifier; } interface ClassExpression extends ClassLikeDeclaration, PrimaryExpression { + kind: SyntaxKind.ClassExpression; } interface ClassElement extends Declaration { _classElementBrand: any; @@ -950,85 +1808,108 @@ declare namespace ts { interface TypeElement extends Declaration { _typeElementBrand: any; name?: PropertyName; - questionToken?: Node; + questionToken?: QuestionToken; } interface InterfaceDeclaration extends DeclarationStatement { + kind: SyntaxKind.InterfaceDeclaration; name: Identifier; typeParameters?: NodeArray; heritageClauses?: NodeArray; members: NodeArray; } interface HeritageClause extends Node { + kind: SyntaxKind.HeritageClause; token: SyntaxKind; types?: NodeArray; } interface TypeAliasDeclaration extends DeclarationStatement { + kind: SyntaxKind.TypeAliasDeclaration; name: Identifier; typeParameters?: NodeArray; type: TypeNode; } interface EnumMember extends Declaration { + kind: SyntaxKind.EnumMember; name: PropertyName; initializer?: Expression; } interface EnumDeclaration extends DeclarationStatement { + kind: SyntaxKind.EnumDeclaration; name: Identifier; members: NodeArray; } type ModuleBody = ModuleBlock | ModuleDeclaration; type ModuleName = Identifier | StringLiteral; interface ModuleDeclaration extends DeclarationStatement { + kind: SyntaxKind.ModuleDeclaration; name: Identifier | LiteralExpression; - body?: ModuleBlock | ModuleDeclaration; + body?: ModuleBlock | NamespaceDeclaration; + } + interface NamespaceDeclaration extends ModuleDeclaration { + name: Identifier; + body: ModuleBlock | NamespaceDeclaration; } interface ModuleBlock extends Node, Statement { + kind: SyntaxKind.ModuleBlock; statements: NodeArray; } type ModuleReference = EntityName | ExternalModuleReference; interface ImportEqualsDeclaration extends DeclarationStatement { + kind: SyntaxKind.ImportEqualsDeclaration; name: Identifier; moduleReference: ModuleReference; } interface ExternalModuleReference extends Node { + kind: SyntaxKind.ExternalModuleReference; expression?: Expression; } interface ImportDeclaration extends Statement { + kind: SyntaxKind.ImportDeclaration; importClause?: ImportClause; moduleSpecifier: Expression; } type NamedImportBindings = NamespaceImport | NamedImports; interface ImportClause extends Declaration { + kind: SyntaxKind.ImportClause; name?: Identifier; namedBindings?: NamedImportBindings; } interface NamespaceImport extends Declaration { + kind: SyntaxKind.NamespaceImport; name: Identifier; } interface NamespaceExportDeclaration extends DeclarationStatement { + kind: SyntaxKind.NamespaceExportDeclaration; name: Identifier; moduleReference: LiteralLikeNode; } interface ExportDeclaration extends DeclarationStatement { + kind: SyntaxKind.ExportDeclaration; exportClause?: NamedExports; moduleSpecifier?: Expression; } interface NamedImports extends Node { + kind: SyntaxKind.NamedImports; elements: NodeArray; } interface NamedExports extends Node { + kind: SyntaxKind.NamedExports; elements: NodeArray; } type NamedImportsOrExports = NamedImports | NamedExports; interface ImportSpecifier extends Declaration { + kind: SyntaxKind.ImportSpecifier; propertyName?: Identifier; name: Identifier; } interface ExportSpecifier extends Declaration { + kind: SyntaxKind.ExportSpecifier; propertyName?: Identifier; name: Identifier; } type ImportOrExportSpecifier = ImportSpecifier | ExportSpecifier; interface ExportAssignment extends DeclarationStatement { + kind: SyntaxKind.ExportAssignment; isExportEquals?: boolean; expression: Expression; } @@ -1040,95 +1921,121 @@ declare namespace ts { kind: SyntaxKind; } interface JSDocTypeExpression extends Node { + kind: SyntaxKind.JSDocTypeExpression; type: JSDocType; } interface JSDocType extends TypeNode { _jsDocTypeBrand: any; } interface JSDocAllType extends JSDocType { - _JSDocAllTypeBrand: any; + kind: SyntaxKind.JSDocAllType; } interface JSDocUnknownType extends JSDocType { - _JSDocUnknownTypeBrand: any; + kind: SyntaxKind.JSDocUnknownType; } interface JSDocArrayType extends JSDocType { + kind: SyntaxKind.JSDocArrayType; elementType: JSDocType; } interface JSDocUnionType extends JSDocType { + kind: SyntaxKind.JSDocUnionType; types: NodeArray; } interface JSDocTupleType extends JSDocType { + kind: SyntaxKind.JSDocTupleType; types: NodeArray; } interface JSDocNonNullableType extends JSDocType { + kind: SyntaxKind.JSDocNonNullableType; type: JSDocType; } interface JSDocNullableType extends JSDocType { + kind: SyntaxKind.JSDocNullableType; type: JSDocType; } - interface JSDocRecordType extends JSDocType, TypeLiteralNode { + interface JSDocRecordType extends JSDocType { + kind: SyntaxKind.JSDocRecordType; literal: TypeLiteralNode; } interface JSDocTypeReference extends JSDocType { + kind: SyntaxKind.JSDocTypeReference; name: EntityName; typeArguments: NodeArray; } interface JSDocOptionalType extends JSDocType { + kind: SyntaxKind.JSDocOptionalType; type: JSDocType; } interface JSDocFunctionType extends JSDocType, SignatureDeclaration { + kind: SyntaxKind.JSDocFunctionType; parameters: NodeArray; type: JSDocType; } interface JSDocVariadicType extends JSDocType { + kind: SyntaxKind.JSDocVariadicType; type: JSDocType; } interface JSDocConstructorType extends JSDocType { + kind: SyntaxKind.JSDocConstructorType; type: JSDocType; } interface JSDocThisType extends JSDocType { + kind: SyntaxKind.JSDocThisType; type: JSDocType; } interface JSDocLiteralType extends JSDocType { + kind: SyntaxKind.JSDocLiteralType; literal: LiteralTypeNode; } type JSDocTypeReferencingNode = JSDocThisType | JSDocConstructorType | JSDocVariadicType | JSDocOptionalType | JSDocNullableType | JSDocNonNullableType; interface JSDocRecordMember extends PropertySignature { + kind: SyntaxKind.JSDocRecordMember; name: Identifier | LiteralExpression; type?: JSDocType; } interface JSDoc extends Node { + kind: SyntaxKind.JSDocComment; tags: NodeArray | undefined; comment: string | undefined; } interface JSDocTag extends Node { - atToken: Node; + atToken: AtToken; tagName: Identifier; comment: string | undefined; } + interface JSDocUnknownTag extends JSDocTag { + kind: SyntaxKind.JSDocTag; + } interface JSDocTemplateTag extends JSDocTag { + kind: SyntaxKind.JSDocTemplateTag; typeParameters: NodeArray; } interface JSDocReturnTag extends JSDocTag { + kind: SyntaxKind.JSDocReturnTag; typeExpression: JSDocTypeExpression; } interface JSDocTypeTag extends JSDocTag { + kind: SyntaxKind.JSDocTypeTag; typeExpression: JSDocTypeExpression; } interface JSDocTypedefTag extends JSDocTag, Declaration { + kind: SyntaxKind.JSDocTypedefTag; name?: Identifier; typeExpression?: JSDocTypeExpression; jsDocTypeLiteral?: JSDocTypeLiteral; } interface JSDocPropertyTag extends JSDocTag, TypeElement { + kind: SyntaxKind.JSDocPropertyTag; name: Identifier; typeExpression: JSDocTypeExpression; } interface JSDocTypeLiteral extends JSDocType { + kind: SyntaxKind.JSDocTypeLiteral; jsDocPropertyTags?: NodeArray; jsDocTypeTag?: JSDocTypeTag; } interface JSDocParameterTag extends JSDocTag { + kind: SyntaxKind.JSDocParameterTag; preParameterName?: Identifier; typeExpression?: JSDocTypeExpression; postParameterName?: Identifier; @@ -1154,7 +2061,7 @@ declare namespace ts { id?: number; } interface FlowStart extends FlowNode { - container?: FunctionExpression | ArrowFunction; + container?: FunctionExpression | ArrowFunction | MethodDeclaration; } interface FlowLabel extends FlowNode { antecedents: FlowNode[]; @@ -1183,8 +2090,9 @@ declare namespace ts { name: string; } interface SourceFile extends Declaration { + kind: SyntaxKind.SourceFile; statements: NodeArray; - endOfFileToken: Node; + endOfFileToken: Token; fileName: string; path: Path; text: string; @@ -1239,7 +2147,7 @@ declare namespace ts { interface Program extends ScriptReferenceHost { getRootFileNames(): string[]; getSourceFiles(): SourceFile[]; - emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback, cancellationToken?: CancellationToken): EmitResult; + emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean): EmitResult; getOptionsDiagnostics(cancellationToken?: CancellationToken): Diagnostic[]; getGlobalDiagnostics(cancellationToken?: CancellationToken): Diagnostic[]; getSyntacticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[]; @@ -1248,6 +2156,7 @@ declare namespace ts { getTypeChecker(): TypeChecker; getCommonSourceDirectory(): string; getDiagnosticsProducingTypeChecker(): TypeChecker; + dropDiagnosticsProducingTypeChecker(): void; getClassifiableNames(): Map; getNodeCount(): number; getIdentifierCount(): number; @@ -1379,6 +2288,7 @@ declare namespace ts { UseFullyQualifiedType = 128, InFirstTypeArgument = 256, InTypeAlias = 512, + UseTypeAliasValue = 1024, } const enum SymbolFormatFlags { None = 0, @@ -1399,9 +2309,10 @@ declare namespace ts { type: Type; } interface ThisTypePredicate extends TypePredicateBase { - _thisTypePredicateBrand: any; + kind: TypePredicateKind.This; } interface IdentifierTypePredicate extends TypePredicateBase { + kind: TypePredicateKind.Identifier; parameterName: string; parameterIndex: number; } @@ -1457,8 +2368,8 @@ declare namespace ts { getExternalModuleFileFromDeclaration(declaration: ImportEqualsDeclaration | ImportDeclaration | ExportDeclaration | ModuleDeclaration): SourceFile; getTypeReferenceDirectivesForEntityName(name: EntityNameOrEntityNameExpression): string[]; getTypeReferenceDirectivesForSymbol(symbol: Symbol, meaning?: SymbolFlags): string[]; - isLiteralConstDeclaration(node: VariableDeclaration): boolean; - writeLiteralConstValue(node: VariableDeclaration, writer: SymbolWriter): void; + isLiteralConstDeclaration(node: VariableDeclaration | PropertyDeclaration | PropertySignature | ParameterDeclaration): boolean; + writeLiteralConstValue(node: VariableDeclaration | PropertyDeclaration | PropertySignature | ParameterDeclaration, writer: SymbolWriter): void; } const enum SymbolFlags { None = 0, @@ -1556,7 +2467,8 @@ declare namespace ts { mapper?: TypeMapper; referenced?: boolean; containingType?: UnionOrIntersectionType; - hasCommonType?: boolean; + hasNonUniformType?: boolean; + isPartial?: boolean; isDiscriminantProperty?: boolean; resolvedExports?: SymbolTable; exportsChecked?: boolean; @@ -1641,8 +2553,6 @@ declare namespace ts { ContainsWideningType = 33554432, ContainsObjectLiteral = 67108864, ContainsAnyFunctionType = 134217728, - ThisType = 268435456, - ObjectLiteralPatternWithComputedProperties = 536870912, Nullable = 6144, Literal = 480, StringOrNumberLiteral = 96, @@ -1659,7 +2569,7 @@ declare namespace ts { StructuredType = 4161536, StructuredOrTypeParameter = 4177920, Narrowable = 4178943, - NotUnionOrUnit = 2589191, + NotUnionOrUnit = 2589185, RequiresWidening = 100663296, PropagatingFlags = 234881024, } @@ -1687,6 +2597,7 @@ declare namespace ts { baseType: EnumType & UnionType; } interface ObjectType extends Type { + isObjectLiteralPatternWithComputedProperties?: boolean; } interface InterfaceType extends ObjectType { typeParameters: TypeParameter[]; @@ -1743,6 +2654,7 @@ declare namespace ts { target?: TypeParameter; mapper?: TypeMapper; resolvedApparentType: Type; + isThisType?: boolean; } const enum SignatureKind { Call = 0, @@ -1830,16 +2742,14 @@ declare namespace ts { Classic = 1, NodeJs = 2, } - type RootPaths = string[]; - type PathSubstitutions = MapLike; - type TsConfigOnlyOptions = RootPaths | PathSubstitutions; - type CompilerOptionsValue = string | number | boolean | (string | number)[] | TsConfigOnlyOptions; + type CompilerOptionsValue = string | number | boolean | (string | number)[] | string[] | MapLike; interface CompilerOptions { allowJs?: boolean; allowNonTsExtensions?: boolean; allowSyntheticDefaultImports?: boolean; allowUnreachableCode?: boolean; allowUnusedLabels?: boolean; + alwaysStrict?: boolean; baseUrl?: string; charset?: string; configFilePath?: string; @@ -1884,14 +2794,14 @@ declare namespace ts { out?: string; outDir?: string; outFile?: string; - paths?: PathSubstitutions; + paths?: MapLike; preserveConstEnums?: boolean; project?: string; pretty?: DiagnosticStyle; reactNamespace?: string; removeComments?: boolean; rootDir?: string; - rootDirs?: RootPaths; + rootDirs?: string[]; skipLibCheck?: boolean; skipDefaultLibCheck?: boolean; sourceMap?: boolean; @@ -1974,6 +2884,7 @@ declare namespace ts { raw?: any; errors: Diagnostic[]; wildcardDirectories?: MapLike; + compileOnSave?: boolean; } const enum WatchDirectoryFlags { None = 0, @@ -2221,8 +3132,16 @@ declare namespace ts { TypeScriptClassSyntaxMask = 137216, ES6FunctionSyntaxMask = 81920, } - const enum NodeEmitFlags { - EmitEmitHelpers = 1, + interface EmitNode { + flags?: EmitFlags; + commentRange?: TextRange; + sourceMapRange?: TextRange; + tokenSourceMapRanges?: Map; + annotatedNodes?: Node[]; + constantValue?: number; + } + const enum EmitFlags { + EmitEmitHelpers = 1, EmitExportStar = 2, EmitSuperHelper = 4, EmitAdvancedSuperHelper = 8, @@ -2250,6 +3169,12 @@ declare namespace ts { ReuseTempVariableScope = 4194304, CustomPrologue = 8388608, } + const enum EmitContext { + SourceFile = 0, + Expression = 1, + IdentifierName = 2, + Unspecified = 3, + } interface LexicalEnvironment { startLexicalEnvironment(): void; endLexicalEnvironment(): Statement[]; @@ -2327,7 +3252,7 @@ declare namespace ts { function singleOrUndefined(array: T[]): T; function singleOrMany(array: T[]): T | T[]; function lastOrUndefined(array: T[]): T; - function binarySearch(array: number[], value: number): number; + function binarySearch(array: T[], value: T, comparer?: (v1: T, v2: T) => number): number; function reduceLeft(array: T[], f: (memo: U, value: T, i: number) => U, initial: U, start?: number, count?: number): U; function reduceLeft(array: T[], f: (memo: T, value: T, i: number) => T): T; function reduceRight(array: T[], f: (memo: U, value: T, i: number) => U, initial: U, start?: number, count?: number): U; @@ -2354,6 +3279,8 @@ declare namespace ts { function multiMapRemove(map: Map, key: string, value: V): void; function isArray(value: any): value is any[]; function memoize(callback: () => T): () => T; + function chain(...args: ((t: T) => (u: U) => U)[]): (t: T) => (u: U) => U; + function compose(...args: ((t: T) => T)[]): (t: T) => T; let localizedDiagnosticMessages: Map; function getLocaleSpecificMessage(message: DiagnosticMessage): string; function createFileDiagnostic(file: SourceFile, start: number, length: number, message: DiagnosticMessage, ...args: any[]): Diagnostic; @@ -2375,7 +3302,12 @@ declare namespace ts { function getDirectoryPath(path: Path): Path; function getDirectoryPath(path: string): string; function isUrl(path: string): boolean; + function isExternalModuleNameRelative(moduleName: string): boolean; + function getEmitScriptTarget(compilerOptions: CompilerOptions): ScriptTarget; + function getEmitModuleKind(compilerOptions: CompilerOptions): ModuleKind; + function hasZeroOrOneAsteriskCharacter(str: string): boolean; function isRootedDiskPath(path: string): boolean; + function convertToRelativePath(absoluteOrRelativePath: string, basePath: string, getCanonicalFileName: (path: string) => string): string; function getNormalizedPathComponents(path: string, currentDirectory: string): string[]; function getNormalizedAbsolutePath(fileName: string, currentDirectory: string): string; function getNormalizedPathFromPathComponents(pathComponents: string[]): string; @@ -2409,6 +3341,8 @@ declare namespace ts { const supportedTypescriptExtensionsForExtractExtension: string[]; const supportedJavascriptExtensions: string[]; function getSupportedExtensions(options?: CompilerOptions): string[]; + function hasJavaScriptFileExtension(fileName: string): boolean; + function hasTypeScriptFileExtension(fileName: string): boolean; function isSupportedSourceFileName(fileName: string, compilerOptions?: CompilerOptions): boolean; const enum ExtensionPriority { TypeScriptFiles = 0, @@ -2427,9 +3361,9 @@ declare namespace ts { function changeExtension(path: T, newExtension: string): T; interface ObjectAllocator { getNodeConstructor(): new (kind: SyntaxKind, pos?: number, end?: number) => Node; - getTokenConstructor(): new (kind: SyntaxKind, pos?: number, end?: number) => Token; - getIdentifierConstructor(): new (kind: SyntaxKind, pos?: number, end?: number) => Token; - getSourceFileConstructor(): new (kind: SyntaxKind, pos?: number, end?: number) => SourceFile; + getTokenConstructor(): new (kind: TKind, pos?: number, end?: number) => Token; + getIdentifierConstructor(): new (kind: SyntaxKind.Identifier, pos?: number, end?: number) => Identifier; + getSourceFileConstructor(): new (kind: SyntaxKind.SourceFile, pos?: number, end?: number) => SourceFile; getSymbolConstructor(): new (flags: SymbolFlags, name: string) => Symbol; getTypeConstructor(): new (checker: TypeChecker, flags: TypeFlags) => Type; getSignatureConstructor(): new (checker: TypeChecker) => Signature; @@ -2442,15 +3376,21 @@ declare namespace ts { VeryAggressive = 3, } namespace Debug { + let currentAssertionLevel: AssertionLevel; function shouldAssert(level: AssertionLevel): boolean; function assert(expression: boolean, message?: string, verboseDebugInfo?: () => string): void; function fail(message?: string): void; } - function getEnvironmentVariable(name: string, host?: CompilerHost): string; function orderedRemoveItemAt(array: T[], index: number): void; function unorderedRemoveItemAt(array: T[], index: number): void; function unorderedRemoveItem(array: T[], item: T): void; function createGetCanonicalFileName(useCaseSensitiveFileNames: boolean): (fileName: string) => string; + function matchPatternOrExact(patternStrings: string[], candidate: string): string | Pattern | undefined; + function patternText({prefix, suffix}: Pattern): string; + function matchedText(pattern: Pattern, candidate: string): string; + function findBestPatternMatch(values: T[], getPattern: (value: T) => Pattern, candidate: string): T | undefined; + function tryParsePattern(pattern: string): Pattern | undefined; + function positionIsSynthesized(pos: number): boolean; } declare namespace ts { type FileWatcherCallback = (fileName: string, removed?: boolean) => void; @@ -2493,10 +3433,10 @@ declare namespace ts { directoryName: string; referenceCount: number; } - var sys: System; + let sys: System; } declare namespace ts { - var Diagnostics: { + const Diagnostics: { Unterminated_string_literal: { code: number; category: DiagnosticCategory; @@ -4417,7 +5357,7 @@ declare namespace ts { key: string; message: string; }; - All_symbols_within_a_with_block_will_be_resolved_to_any: { + The_with_statement_is_not_supported_All_symbols_in_a_with_block_will_have_type_any: { code: number; category: DiagnosticCategory; key: string; @@ -5383,7 +6323,7 @@ declare namespace ts { key: string; message: string; }; - Identifier_0_must_be_imported_from_a_module: { + _0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead: { code: number; category: DiagnosticCategory; key: string; @@ -6781,6 +7721,18 @@ declare namespace ts { key: string; message: string; }; + Auto_discovery_for_typings_is_enabled_in_project_0_Running_extra_resolution_pass_for_module_1_using_cache_location_2: { + code: number; + category: DiagnosticCategory; + key: string; + message: string; + }; + Parse_in_strict_mode_and_emit_use_strict_for_each_source_file: { + code: number; + category: DiagnosticCategory; + key: string; + message: string; + }; Variable_0_implicitly_has_an_1_type: { code: number; category: DiagnosticCategory; @@ -6925,6 +7877,12 @@ declare namespace ts { key: string; message: string; }; + Variable_0_implicitly_has_type_any_in_some_locations_where_its_type_cannot_be_determined: { + code: number; + category: DiagnosticCategory; + key: string; + message: string; + }; You_cannot_rename_this_element: { code: number; category: DiagnosticCategory; @@ -7105,6 +8063,48 @@ declare namespace ts { key: string; message: string; }; + Add_missing_super_call: { + code: number; + category: DiagnosticCategory; + key: string; + message: string; + }; + Make_super_call_the_first_statement_in_the_constructor: { + code: number; + category: DiagnosticCategory; + key: string; + message: string; + }; + Change_extends_to_implements: { + code: number; + category: DiagnosticCategory; + key: string; + message: string; + }; + Remove_unused_identifiers: { + code: number; + category: DiagnosticCategory; + key: string; + message: string; + }; + Implement_interface_on_reference: { + code: number; + category: DiagnosticCategory; + key: string; + message: string; + }; + Implement_interface_on_class: { + code: number; + category: DiagnosticCategory; + key: string; + message: string; + }; + Implement_inherited_abstract_class: { + code: number; + category: DiagnosticCategory; + key: string; + message: string; + }; }; } declare namespace ts { @@ -7174,6 +8174,7 @@ declare namespace ts { function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean, languageVariant?: LanguageVariant, text?: string, onError?: ErrorCallback, start?: number, length?: number): Scanner; } declare namespace ts { + const compileOnSaveCommandLineOption: CommandLineOption; const optionDeclarations: CommandLineOption[]; let typingOptionDeclarations: CommandLineOption[]; interface OptionNameMap { @@ -7190,7 +8191,7 @@ declare namespace ts { config?: any; error?: Diagnostic; }; - function parseConfigFileTextToJson(fileName: string, jsonText: string): { + function parseConfigFileTextToJson(fileName: string, jsonText: string, stripComments?: boolean): { config?: any; error?: Diagnostic; }; @@ -7198,15 +8199,136 @@ declare namespace ts { compilerOptions: Map; }; function parseJsonConfigFileContent(json: any, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[]): ParsedCommandLine; + function convertCompileOnSaveOptionFromJson(jsonOption: any, basePath: string, errors: Diagnostic[]): boolean; function convertCompilerOptionsFromJson(jsonOptions: any, basePath: string, configFileName?: string): { options: CompilerOptions; errors: Diagnostic[]; }; function convertTypingOptionsFromJson(jsonOptions: any, basePath: string, configFileName?: string): { - options: CompilerOptions; + options: TypingOptions; errors: Diagnostic[]; }; } +declare namespace ts.JsTyping { + interface TypingResolutionHost { + directoryExists: (path: string) => boolean; + fileExists: (fileName: string) => boolean; + readFile: (path: string, encoding?: string) => string; + readDirectory: (rootDir: string, extensions: string[], excludes: string[], includes: string[], depth?: number) => string[]; + } + function discoverTypings(host: TypingResolutionHost, fileNames: string[], projectRootPath: Path, safeListPath: Path, packageNameToTypingLocation: Map, typingOptions: TypingOptions, compilerOptions: CompilerOptions): { + cachedTypingPaths: string[]; + newTypingNames: string[]; + filesToWatch: string[]; + }; +} +declare namespace ts.server { + enum LogLevel { + terse = 0, + normal = 1, + requestTime = 2, + verbose = 3, + } + const emptyArray: ReadonlyArray; + interface Logger { + close(): void; + hasLevel(level: LogLevel): boolean; + loggingEnabled(): boolean; + perftrc(s: string): void; + info(s: string): void; + startGroup(): void; + endGroup(): void; + msg(s: string, type?: Msg.Types): void; + getLogFileName(): string; + } + namespace Msg { + type Err = "Err"; + const Err: Err; + type Info = "Info"; + const Info: Info; + type Perf = "Perf"; + const Perf: Perf; + type Types = Err | Info | Perf; + } + function createInstallTypingsRequest(project: Project, typingOptions: TypingOptions, cachePath?: string): DiscoverTypings; + namespace Errors { + function ThrowNoProject(): never; + function ThrowProjectLanguageServiceDisabled(): never; + function ThrowProjectDoesNotContainDocument(fileName: string, project: Project): never; + } + function getDefaultFormatCodeSettings(host: ServerHost): FormatCodeSettings; + function mergeMaps(target: MapLike, source: MapLike): void; + function removeItemFromSet(items: T[], itemToRemove: T): void; + type NormalizedPath = string & { + __normalizedPathTag: any; + }; + function toNormalizedPath(fileName: string): NormalizedPath; + function normalizedPathToPath(normalizedPath: NormalizedPath, currentDirectory: string, getCanonicalFileName: (f: string) => string): Path; + function asNormalizedPath(fileName: string): NormalizedPath; + interface NormalizedPathMap { + get(path: NormalizedPath): T; + set(path: NormalizedPath, value: T): void; + contains(path: NormalizedPath): boolean; + remove(path: NormalizedPath): void; + } + function createNormalizedPathMap(): NormalizedPathMap; + const nullLanguageService: LanguageService; + interface ServerLanguageServiceHost { + setCompilationSettings(options: CompilerOptions): void; + notifyFileRemoved(info: ScriptInfo): void; + } + const nullLanguageServiceHost: ServerLanguageServiceHost; + interface ProjectOptions { + configHasFilesProperty?: boolean; + files?: string[]; + wildcardDirectories?: Map; + compilerOptions?: CompilerOptions; + typingOptions?: TypingOptions; + compileOnSave?: boolean; + } + function isInferredProjectName(name: string): boolean; + function makeInferredProjectName(counter: number): string; + class ThrottledOperations { + private readonly host; + private pendingTimeouts; + constructor(host: ServerHost); + schedule(operationId: string, delay: number, cb: () => void): void; + private static run(self, operationId, cb); + } + class GcTimer { + private readonly host; + private readonly delay; + private readonly logger; + private timerId; + constructor(host: ServerHost, delay: number, logger: Logger); + scheduleCollect(): void; + private static run(self); + } +} +declare namespace ts { + function trace(host: ModuleResolutionHost, message: DiagnosticMessage, ...args: any[]): void; + function isTraceEnabled(compilerOptions: CompilerOptions, host: ModuleResolutionHost): boolean; + function createResolvedModule(resolvedFileName: string, isExternalLibraryImport: boolean, failedLookupLocations: string[]): ResolvedModuleWithFailedLookupLocations; + interface ModuleResolutionState { + host: ModuleResolutionHost; + compilerOptions: CompilerOptions; + traceEnabled: boolean; + skipTsx: boolean; + } + function getEffectiveTypeRoots(options: CompilerOptions, host: { + directoryExists?: (directoryName: string) => boolean; + getCurrentDirectory?: () => string; + }): string[] | undefined; + function resolveTypeReferenceDirective(typeReferenceDirectiveName: string, containingFile: string, options: CompilerOptions, host: ModuleResolutionHost): ResolvedTypeReferenceDirectiveWithFailedLookupLocations; + function getAutomaticTypeDirectiveNames(options: CompilerOptions, host: ModuleResolutionHost): string[]; + function resolveModuleName(moduleName: string, containingFile: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost): ResolvedModuleWithFailedLookupLocations; + function nodeModuleNameResolver(moduleName: string, containingFile: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost): ResolvedModuleWithFailedLookupLocations; + function directoryProbablyExists(directoryName: string, host: { + directoryExists?: (directoryName: string) => boolean; + }): boolean; + function loadModuleFromNodeModules(moduleName: string, directory: string, failedLookupLocations: string[], state: ModuleResolutionState, checkOneLevel: boolean): string; + function classicNameResolver(moduleName: string, containingFile: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost): ResolvedModuleWithFailedLookupLocations; +} declare namespace ts { const externalHelpersModuleNameText = "tslib"; interface ReferencePathMatchResult { @@ -7231,7 +8353,7 @@ declare namespace ts { function getSingleLineStringWriter(): StringSymbolWriter; function releaseStringWriter(writer: StringSymbolWriter): void; function getFullWidth(node: Node): number; - function arrayIsEqualTo(array1: T[], array2: T[], equaler?: (a: T, b: T) => boolean): boolean; + function arrayIsEqualTo(array1: ReadonlyArray, array2: ReadonlyArray, equaler?: (a: T, b: T) => boolean): boolean; function hasResolvedModule(sourceFile: SourceFile, moduleNameText: string): boolean; function getResolvedModule(sourceFile: SourceFile, moduleNameText: string): ResolvedModule; function setResolvedModule(sourceFile: SourceFile, moduleNameText: string, resolvedModule: ResolvedModule): void; @@ -7280,7 +8402,7 @@ declare namespace ts { function isConstEnumDeclaration(node: Node): boolean; function isConst(node: Node): boolean; function isLet(node: Node): boolean; - function isSuperCallExpression(n: Node): boolean; + function isSuperCall(n: Node): n is SuperCall; function isPrologueDirective(node: Node): boolean; function getLeadingCommentRangesOfNode(node: Node, sourceFileOfNode: SourceFile): CommentRange[]; function getLeadingCommentRangesOfNodeFromText(node: Node, text: string): CommentRange[]; @@ -7301,6 +8423,7 @@ declare namespace ts { function isIterationStatement(node: Node, lookInLabeledStatements: boolean): node is IterationStatement; function isFunctionBlock(node: Node): boolean; function isObjectLiteralMethod(node: Node): node is MethodDeclaration; + function isObjectLiteralOrClassExpressionMethod(node: Node): node is MethodDeclaration; function isIdentifierTypePredicate(predicate: TypePredicate): predicate is IdentifierTypePredicate; function isThisTypePredicate(predicate: TypePredicate): predicate is ThisTypePredicate; function getContainingFunction(node: Node): FunctionLikeDeclaration; @@ -7308,7 +8431,7 @@ declare namespace ts { function getThisContainer(node: Node, includeArrowFunctions: boolean): Node; function getSuperContainer(node: Node, stopOnFunctions: boolean): Node; function getImmediatelyInvokedFunctionExpression(func: Node): CallExpression; - function isSuperProperty(node: Node): node is (PropertyAccessExpression | ElementAccessExpression); + function isSuperProperty(node: Node): node is SuperProperty; function getEntityNameFromTypeNode(node: TypeNode): EntityNameOrEntityNameExpression; function isCallLikeExpression(node: Node): node is CallLikeExpression; function getInvokedExpression(node: CallLikeExpression): Expression; @@ -7318,7 +8441,6 @@ declare namespace ts { function childIsDecorated(node: Node): boolean; function isJSXTagName(node: Node): boolean; function isPartOfExpression(node: Node): boolean; - function isExternalModuleNameRelative(moduleName: string): boolean; function isInstantiatedModule(node: ModuleDeclaration, preserveConstEnums: boolean): boolean; function isExternalModuleImportEqualsDeclaration(node: Node): boolean; function getExternalModuleImportEqualsDeclarationExpression(node: Node): Expression; @@ -7330,7 +8452,7 @@ declare namespace ts { function isDeclarationOfFunctionExpression(s: Symbol): boolean; function getSpecialPropertyAssignmentKind(expression: Node): SpecialPropertyAssignmentKind; function getExternalModuleName(node: Node): Expression; - function getNamespaceDeclarationNode(node: ImportDeclaration | ImportEqualsDeclaration | ExportDeclaration): NamespaceImport; + function getNamespaceDeclarationNode(node: ImportDeclaration | ImportEqualsDeclaration | ExportDeclaration): ImportEqualsDeclaration | NamespaceImport; function isDefaultImport(node: ImportDeclaration | ImportEqualsDeclaration | ExportDeclaration): boolean; function hasQuestionToken(node: Node): boolean; function isJSDocConstructSignature(node: Node): boolean; @@ -7373,7 +8495,6 @@ declare namespace ts { function getRootDeclaration(node: Node): Node; function nodeStartsNewLexicalEnvironment(node: Node): boolean; function nodeIsSynthesized(node: TextRange): boolean; - function positionIsSynthesized(pos: number): boolean; function getOriginalNode(node: Node): Node; function isParseTreeNode(node: Node): boolean; function getParseTreeNode(node: Node): Node; @@ -7387,7 +8508,7 @@ declare namespace ts { function getExpressionAssociativity(expression: Expression): Associativity; function getOperatorAssociativity(kind: SyntaxKind, operator: SyntaxKind, hasArguments?: boolean): Associativity; function getExpressionPrecedence(expression: Expression): 0 | 1 | -1 | 2 | 4 | 3 | 16 | 10 | 5 | 6 | 11 | 8 | 19 | 18 | 17 | 15 | 14 | 13 | 12 | 9 | 7; - function getOperator(expression: Expression): SyntaxKind; + function getOperator(expression: Expression): SyntaxKind.Unknown | SyntaxKind.EndOfFileToken | SyntaxKind.SingleLineCommentTrivia | SyntaxKind.MultiLineCommentTrivia | SyntaxKind.NewLineTrivia | SyntaxKind.WhitespaceTrivia | SyntaxKind.ShebangTrivia | SyntaxKind.ConflictMarkerTrivia | SyntaxKind.NumericLiteral | SyntaxKind.StringLiteral | SyntaxKind.RegularExpressionLiteral | SyntaxKind.NoSubstitutionTemplateLiteral | SyntaxKind.TemplateHead | SyntaxKind.TemplateMiddle | SyntaxKind.TemplateTail | SyntaxKind.OpenBraceToken | SyntaxKind.CloseBraceToken | SyntaxKind.OpenParenToken | SyntaxKind.CloseParenToken | SyntaxKind.OpenBracketToken | SyntaxKind.CloseBracketToken | SyntaxKind.DotToken | SyntaxKind.DotDotDotToken | SyntaxKind.SemicolonToken | SyntaxKind.CommaToken | SyntaxKind.LessThanToken | SyntaxKind.LessThanSlashToken | SyntaxKind.GreaterThanToken | SyntaxKind.LessThanEqualsToken | SyntaxKind.GreaterThanEqualsToken | SyntaxKind.EqualsEqualsToken | SyntaxKind.ExclamationEqualsToken | SyntaxKind.EqualsEqualsEqualsToken | SyntaxKind.ExclamationEqualsEqualsToken | SyntaxKind.EqualsGreaterThanToken | SyntaxKind.PlusToken | SyntaxKind.MinusToken | SyntaxKind.AsteriskToken | SyntaxKind.AsteriskAsteriskToken | SyntaxKind.SlashToken | SyntaxKind.PercentToken | SyntaxKind.PlusPlusToken | SyntaxKind.MinusMinusToken | SyntaxKind.LessThanLessThanToken | SyntaxKind.GreaterThanGreaterThanToken | SyntaxKind.GreaterThanGreaterThanGreaterThanToken | SyntaxKind.AmpersandToken | SyntaxKind.BarToken | SyntaxKind.CaretToken | SyntaxKind.ExclamationToken | SyntaxKind.TildeToken | SyntaxKind.AmpersandAmpersandToken | SyntaxKind.BarBarToken | SyntaxKind.QuestionToken | SyntaxKind.ColonToken | SyntaxKind.AtToken | SyntaxKind.EqualsToken | SyntaxKind.PlusEqualsToken | SyntaxKind.MinusEqualsToken | SyntaxKind.AsteriskEqualsToken | SyntaxKind.AsteriskAsteriskEqualsToken | SyntaxKind.SlashEqualsToken | SyntaxKind.PercentEqualsToken | SyntaxKind.LessThanLessThanEqualsToken | SyntaxKind.GreaterThanGreaterThanEqualsToken | SyntaxKind.GreaterThanGreaterThanGreaterThanEqualsToken | SyntaxKind.AmpersandEqualsToken | SyntaxKind.BarEqualsToken | SyntaxKind.CaretEqualsToken | SyntaxKind.Identifier | SyntaxKind.BreakKeyword | SyntaxKind.CaseKeyword | SyntaxKind.CatchKeyword | SyntaxKind.ClassKeyword | SyntaxKind.ConstKeyword | SyntaxKind.ContinueKeyword | SyntaxKind.DebuggerKeyword | SyntaxKind.DefaultKeyword | SyntaxKind.DeleteKeyword | SyntaxKind.DoKeyword | SyntaxKind.ElseKeyword | SyntaxKind.EnumKeyword | SyntaxKind.ExportKeyword | SyntaxKind.ExtendsKeyword | SyntaxKind.FalseKeyword | SyntaxKind.FinallyKeyword | SyntaxKind.ForKeyword | SyntaxKind.FunctionKeyword | SyntaxKind.IfKeyword | SyntaxKind.ImportKeyword | SyntaxKind.InKeyword | SyntaxKind.InstanceOfKeyword | SyntaxKind.NewKeyword | SyntaxKind.NullKeyword | SyntaxKind.ReturnKeyword | SyntaxKind.SuperKeyword | SyntaxKind.SwitchKeyword | SyntaxKind.ThisKeyword | SyntaxKind.ThrowKeyword | SyntaxKind.TrueKeyword | SyntaxKind.TryKeyword | SyntaxKind.TypeOfKeyword | SyntaxKind.VarKeyword | SyntaxKind.VoidKeyword | SyntaxKind.WhileKeyword | SyntaxKind.WithKeyword | SyntaxKind.ImplementsKeyword | SyntaxKind.InterfaceKeyword | SyntaxKind.LetKeyword | SyntaxKind.PackageKeyword | SyntaxKind.PrivateKeyword | SyntaxKind.ProtectedKeyword | SyntaxKind.PublicKeyword | SyntaxKind.StaticKeyword | SyntaxKind.YieldKeyword | SyntaxKind.AbstractKeyword | SyntaxKind.AsKeyword | SyntaxKind.AnyKeyword | SyntaxKind.AsyncKeyword | SyntaxKind.AwaitKeyword | SyntaxKind.BooleanKeyword | SyntaxKind.ConstructorKeyword | SyntaxKind.DeclareKeyword | SyntaxKind.GetKeyword | SyntaxKind.IsKeyword | SyntaxKind.ModuleKeyword | SyntaxKind.NamespaceKeyword | SyntaxKind.NeverKeyword | SyntaxKind.ReadonlyKeyword | SyntaxKind.RequireKeyword | SyntaxKind.NumberKeyword | SyntaxKind.SetKeyword | SyntaxKind.StringKeyword | SyntaxKind.SymbolKeyword | SyntaxKind.TypeKeyword | SyntaxKind.UndefinedKeyword | SyntaxKind.FromKeyword | SyntaxKind.GlobalKeyword | SyntaxKind.OfKeyword | SyntaxKind.QualifiedName | SyntaxKind.ComputedPropertyName | SyntaxKind.TypeParameter | SyntaxKind.Parameter | SyntaxKind.Decorator | SyntaxKind.PropertySignature | SyntaxKind.PropertyDeclaration | SyntaxKind.MethodSignature | SyntaxKind.MethodDeclaration | SyntaxKind.Constructor | SyntaxKind.GetAccessor | SyntaxKind.SetAccessor | SyntaxKind.CallSignature | SyntaxKind.ConstructSignature | SyntaxKind.IndexSignature | SyntaxKind.TypePredicate | SyntaxKind.TypeReference | SyntaxKind.FunctionType | SyntaxKind.ConstructorType | SyntaxKind.TypeQuery | SyntaxKind.TypeLiteral | SyntaxKind.ArrayType | SyntaxKind.TupleType | SyntaxKind.UnionType | SyntaxKind.IntersectionType | SyntaxKind.ParenthesizedType | SyntaxKind.ThisType | SyntaxKind.LiteralType | SyntaxKind.ObjectBindingPattern | SyntaxKind.ArrayBindingPattern | SyntaxKind.BindingElement | SyntaxKind.ArrayLiteralExpression | SyntaxKind.ObjectLiteralExpression | SyntaxKind.PropertyAccessExpression | SyntaxKind.ElementAccessExpression | SyntaxKind.CallExpression | SyntaxKind.NewExpression | SyntaxKind.TaggedTemplateExpression | SyntaxKind.TypeAssertionExpression | SyntaxKind.ParenthesizedExpression | SyntaxKind.FunctionExpression | SyntaxKind.ArrowFunction | SyntaxKind.DeleteExpression | SyntaxKind.TypeOfExpression | SyntaxKind.VoidExpression | SyntaxKind.AwaitExpression | SyntaxKind.ConditionalExpression | SyntaxKind.TemplateExpression | SyntaxKind.YieldExpression | SyntaxKind.SpreadElementExpression | SyntaxKind.ClassExpression | SyntaxKind.OmittedExpression | SyntaxKind.ExpressionWithTypeArguments | SyntaxKind.AsExpression | SyntaxKind.NonNullExpression | SyntaxKind.TemplateSpan | SyntaxKind.SemicolonClassElement | SyntaxKind.Block | SyntaxKind.VariableStatement | SyntaxKind.EmptyStatement | SyntaxKind.ExpressionStatement | SyntaxKind.IfStatement | SyntaxKind.DoStatement | SyntaxKind.WhileStatement | SyntaxKind.ForStatement | SyntaxKind.ForInStatement | SyntaxKind.ForOfStatement | SyntaxKind.ContinueStatement | SyntaxKind.BreakStatement | SyntaxKind.ReturnStatement | SyntaxKind.WithStatement | SyntaxKind.SwitchStatement | SyntaxKind.LabeledStatement | SyntaxKind.ThrowStatement | SyntaxKind.TryStatement | SyntaxKind.DebuggerStatement | SyntaxKind.VariableDeclaration | SyntaxKind.VariableDeclarationList | SyntaxKind.FunctionDeclaration | SyntaxKind.ClassDeclaration | SyntaxKind.InterfaceDeclaration | SyntaxKind.TypeAliasDeclaration | SyntaxKind.EnumDeclaration | SyntaxKind.ModuleDeclaration | SyntaxKind.ModuleBlock | SyntaxKind.CaseBlock | SyntaxKind.NamespaceExportDeclaration | SyntaxKind.ImportEqualsDeclaration | SyntaxKind.ImportDeclaration | SyntaxKind.ImportClause | SyntaxKind.NamespaceImport | SyntaxKind.NamedImports | SyntaxKind.ImportSpecifier | SyntaxKind.ExportAssignment | SyntaxKind.ExportDeclaration | SyntaxKind.NamedExports | SyntaxKind.ExportSpecifier | SyntaxKind.MissingDeclaration | SyntaxKind.ExternalModuleReference | SyntaxKind.JsxElement | SyntaxKind.JsxSelfClosingElement | SyntaxKind.JsxOpeningElement | SyntaxKind.JsxText | SyntaxKind.JsxClosingElement | SyntaxKind.JsxAttribute | SyntaxKind.JsxSpreadAttribute | SyntaxKind.JsxExpression | SyntaxKind.CaseClause | SyntaxKind.DefaultClause | SyntaxKind.HeritageClause | SyntaxKind.CatchClause | SyntaxKind.PropertyAssignment | SyntaxKind.ShorthandPropertyAssignment | SyntaxKind.EnumMember | SyntaxKind.SourceFile | SyntaxKind.JSDocTypeExpression | SyntaxKind.JSDocAllType | SyntaxKind.JSDocUnknownType | SyntaxKind.JSDocArrayType | SyntaxKind.JSDocUnionType | SyntaxKind.JSDocTupleType | SyntaxKind.JSDocNullableType | SyntaxKind.JSDocNonNullableType | SyntaxKind.JSDocRecordType | SyntaxKind.JSDocRecordMember | SyntaxKind.JSDocTypeReference | SyntaxKind.JSDocOptionalType | SyntaxKind.JSDocFunctionType | SyntaxKind.JSDocVariadicType | SyntaxKind.JSDocConstructorType | SyntaxKind.JSDocThisType | SyntaxKind.JSDocComment | SyntaxKind.JSDocTag | SyntaxKind.JSDocParameterTag | SyntaxKind.JSDocReturnTag | SyntaxKind.JSDocTypeTag | SyntaxKind.JSDocTemplateTag | SyntaxKind.JSDocTypedefTag | SyntaxKind.JSDocPropertyTag | SyntaxKind.JSDocTypeLiteral | SyntaxKind.JSDocLiteralType | SyntaxKind.JSDocNullKeyword | SyntaxKind.JSDocUndefinedKeyword | SyntaxKind.JSDocNeverKeyword | SyntaxKind.SyntaxList | SyntaxKind.NotEmittedStatement | SyntaxKind.PartiallyEmittedExpression | SyntaxKind.Count; function getOperatorPrecedence(nodeKind: SyntaxKind, operatorKind: SyntaxKind, hasArguments?: boolean): 0 | 1 | -1 | 2 | 4 | 3 | 16 | 10 | 5 | 6 | 11 | 8 | 19 | 18 | 17 | 15 | 14 | 13 | 12 | 9 | 7; function createDiagnosticCollection(): DiagnosticCollection; function escapeString(s: string): string; @@ -7413,26 +8534,28 @@ declare namespace ts { function getIndentSize(): number; function createTextWriter(newLine: String): EmitTextWriter; function getResolvedExternalModuleName(host: EmitHost, file: SourceFile): string; - function getExternalModuleNameFromDeclaration(host: EmitHost, resolver: EmitResolver, declaration: ImportEqualsDeclaration | ImportDeclaration | ExportDeclaration): string; + function getExternalModuleNameFromDeclaration(host: EmitHost, resolver: EmitResolver, declaration: ImportEqualsDeclaration | ImportDeclaration | ExportDeclaration | ModuleDeclaration): string; function getExternalModuleNameFromPath(host: EmitHost, fileName: string): string; function getOwnEmitOutputFilePath(sourceFile: SourceFile, host: EmitHost, extension: string): string; function getDeclarationEmitOutputFilePath(sourceFile: SourceFile, host: EmitHost): string; - function getEmitScriptTarget(compilerOptions: CompilerOptions): ScriptTarget; - function getEmitModuleKind(compilerOptions: CompilerOptions): ModuleKind; interface EmitFileNames { jsFilePath: string; sourceMapFilePath: string; declarationFilePath: string; } function getSourceFilesToEmit(host: EmitHost, targetSourceFile?: SourceFile): SourceFile[]; - function forEachTransformedEmitFile(host: EmitHost, sourceFiles: SourceFile[], action: (jsFilePath: string, sourceMapFilePath: string, declarationFilePath: string, sourceFiles: SourceFile[], isBundledEmit: boolean) => void): void; - function forEachExpectedEmitFile(host: EmitHost, action: (emitFileNames: EmitFileNames, sourceFiles: SourceFile[], isBundledEmit: boolean) => void, targetSourceFile?: SourceFile): void; + function forEachTransformedEmitFile(host: EmitHost, sourceFiles: SourceFile[], action: (jsFilePath: string, sourceMapFilePath: string, declarationFilePath: string, sourceFiles: SourceFile[], isBundledEmit: boolean) => void, emitOnlyDtsFiles?: boolean): void; + function forEachExpectedEmitFile(host: EmitHost, action: (emitFileNames: EmitFileNames, sourceFiles: SourceFile[], isBundledEmit: boolean, emitOnlyDtsFiles: boolean) => void, targetSourceFile?: SourceFile, emitOnlyDtsFiles?: boolean): void; function getSourceFilePathInNewDir(sourceFile: SourceFile, host: EmitHost, newDirPath: string): string; function writeFile(host: EmitHost, diagnostics: DiagnosticCollection, fileName: string, data: string, writeByteOrderMark: boolean, sourceFiles?: SourceFile[]): void; function getLineOfLocalPosition(currentSourceFile: SourceFile, pos: number): number; function getLineOfLocalPositionFromLineMap(lineMap: number[], pos: number): number; function getFirstConstructorWithBody(node: ClassLikeDeclaration): ConstructorDeclaration; - function getSetAccessorTypeAnnotationNode(accessor: AccessorDeclaration): TypeNode; + function getSetAccessorTypeAnnotationNode(accessor: SetAccessorDeclaration): TypeNode; + function getThisParameter(signature: SignatureDeclaration): ParameterDeclaration | undefined; + function parameterIsThisKeyword(parameter: ParameterDeclaration): boolean; + function isThisIdentifier(node: Node | undefined): boolean; + function identifierIsThisKeyword(id: Identifier): boolean; interface AllAccessorDeclarations { firstAccessor: AccessorDeclaration; secondAccessor: AccessorDeclaration; @@ -7463,12 +8586,9 @@ declare namespace ts { function isRightSideOfQualifiedNameOrPropertyAccess(node: Node): boolean; function isEmptyObjectLiteralOrArrayLiteral(expression: Node): boolean; function getLocalSymbolForExportDefault(symbol: Symbol): Symbol; - function hasJavaScriptFileExtension(fileName: string): boolean; - function hasTypeScriptFileExtension(fileName: string): boolean; function tryExtractTypeScriptExtension(fileName: string): string | undefined; const stringify: (value: any) => string; function convertToBase64(input: string): string; - function convertToRelativePath(absoluteOrRelativePath: string, basePath: string, getCanonicalFileName: (path: string) => string): string; function getNewLineCharacter(options: CompilerOptions): string; function isSimpleExpression(node: Expression): boolean; function formatSyntaxKind(kind: SyntaxKind): string; @@ -7504,7 +8624,8 @@ declare namespace ts { function isTextualLiteralKind(kind: SyntaxKind): boolean; function isLiteralExpression(node: Node): node is LiteralExpression; function isTemplateLiteralKind(kind: SyntaxKind): boolean; - function isTemplateLiteralFragment(node: Node): node is TemplateLiteralFragment; + function isTemplateHead(node: Node): node is TemplateHead; + function isTemplateMiddleOrTemplateTail(node: Node): node is TemplateMiddle | TemplateTail; function isIdentifier(node: Node): node is Identifier; function isGeneratedIdentifier(node: Node): boolean; function isModifier(node: Node): node is Modifier; @@ -7529,7 +8650,7 @@ declare namespace ts { function isBinaryExpression(node: Node): node is BinaryExpression; function isConditionalExpression(node: Node): node is ConditionalExpression; function isCallExpression(node: Node): node is CallExpression; - function isTemplate(node: Node): node is Template; + function isTemplateLiteral(node: Node): node is TemplateLiteral; function isSpreadElementExpression(node: Node): node is SpreadElementExpression; function isExpressionWithTypeArguments(node: Node): node is ExpressionWithTypeArguments; function isLeftHandSideExpression(node: Node): node is LeftHandSideExpression; @@ -7613,24 +8734,25 @@ declare namespace ts { function createLiteral(textSource: StringLiteral | Identifier, location?: TextRange): StringLiteral; function createLiteral(value: string, location?: TextRange): StringLiteral; function createLiteral(value: number, location?: TextRange): NumericLiteral; + function createLiteral(value: boolean, location?: TextRange): BooleanLiteral; function createLiteral(value: string | number | boolean, location?: TextRange): PrimaryExpression; function createIdentifier(text: string, location?: TextRange): Identifier; function createTempVariable(recordTempVariable: ((node: Identifier) => void) | undefined, location?: TextRange): Identifier; function createLoopVariable(location?: TextRange): Identifier; function createUniqueName(text: string, location?: TextRange): Identifier; function getGeneratedNameForNode(node: Node, location?: TextRange): Identifier; - function createToken(token: SyntaxKind): Node; + function createToken(token: TKind): Token; function createSuper(): PrimaryExpression; function createThis(location?: TextRange): PrimaryExpression; function createNull(): PrimaryExpression; function createComputedPropertyName(expression: Expression, location?: TextRange): ComputedPropertyName; function updateComputedPropertyName(node: ComputedPropertyName, expression: Expression): ComputedPropertyName; function createParameter(name: string | Identifier | BindingPattern, initializer?: Expression, location?: TextRange): ParameterDeclaration; - function createParameterDeclaration(decorators: Decorator[], modifiers: Modifier[], dotDotDotToken: Node, name: string | Identifier | BindingPattern, questionToken: Node, type: TypeNode, initializer: Expression, location?: TextRange, flags?: NodeFlags): ParameterDeclaration; + function createParameterDeclaration(decorators: Decorator[], modifiers: Modifier[], dotDotDotToken: DotDotDotToken, name: string | Identifier | BindingPattern, questionToken: QuestionToken, type: TypeNode, initializer: Expression, location?: TextRange, flags?: NodeFlags): ParameterDeclaration; function updateParameterDeclaration(node: ParameterDeclaration, decorators: Decorator[], modifiers: Modifier[], name: BindingName, type: TypeNode, initializer: Expression): ParameterDeclaration; - function createProperty(decorators: Decorator[], modifiers: Modifier[], name: string | PropertyName, questionToken: Node, type: TypeNode, initializer: Expression, location?: TextRange): PropertyDeclaration; + function createProperty(decorators: Decorator[], modifiers: Modifier[], name: string | PropertyName, questionToken: QuestionToken, type: TypeNode, initializer: Expression, location?: TextRange): PropertyDeclaration; function updateProperty(node: PropertyDeclaration, decorators: Decorator[], modifiers: Modifier[], name: PropertyName, type: TypeNode, initializer: Expression): PropertyDeclaration; - function createMethod(decorators: Decorator[], modifiers: Modifier[], asteriskToken: Node, name: string | PropertyName, typeParameters: TypeParameterDeclaration[], parameters: ParameterDeclaration[], type: TypeNode, body: Block, location?: TextRange, flags?: NodeFlags): MethodDeclaration; + function createMethod(decorators: Decorator[], modifiers: Modifier[], asteriskToken: AsteriskToken, name: string | PropertyName, typeParameters: TypeParameterDeclaration[], parameters: ParameterDeclaration[], type: TypeNode, body: Block, location?: TextRange, flags?: NodeFlags): MethodDeclaration; function updateMethod(node: MethodDeclaration, decorators: Decorator[], modifiers: Modifier[], name: PropertyName, typeParameters: TypeParameterDeclaration[], parameters: ParameterDeclaration[], type: TypeNode, body: Block): MethodDeclaration; function createConstructor(decorators: Decorator[], modifiers: Modifier[], parameters: ParameterDeclaration[], body: Block, location?: TextRange, flags?: NodeFlags): ConstructorDeclaration; function updateConstructor(node: ConstructorDeclaration, decorators: Decorator[], modifiers: Modifier[], parameters: ParameterDeclaration[], body: Block): ConstructorDeclaration; @@ -7642,7 +8764,7 @@ declare namespace ts { function updateObjectBindingPattern(node: ObjectBindingPattern, elements: BindingElement[]): ObjectBindingPattern; function createArrayBindingPattern(elements: ArrayBindingElement[], location?: TextRange): ArrayBindingPattern; function updateArrayBindingPattern(node: ArrayBindingPattern, elements: ArrayBindingElement[]): ArrayBindingPattern; - function createBindingElement(propertyName: string | PropertyName, dotDotDotToken: Node, name: string | BindingName, initializer?: Expression, location?: TextRange): BindingElement; + function createBindingElement(propertyName: string | PropertyName, dotDotDotToken: DotDotDotToken, name: string | BindingName, initializer?: Expression, location?: TextRange): BindingElement; function updateBindingElement(node: BindingElement, propertyName: PropertyName, name: BindingName, initializer: Expression): BindingElement; function createArrayLiteral(elements?: Expression[], location?: TextRange, multiLine?: boolean): ArrayLiteralExpression; function updateArrayLiteral(node: ArrayLiteralExpression, elements: Expression[]): ArrayLiteralExpression; @@ -7656,13 +8778,13 @@ declare namespace ts { function updateCall(node: CallExpression, expression: Expression, typeArguments: TypeNode[], argumentsArray: Expression[]): CallExpression; function createNew(expression: Expression, typeArguments: TypeNode[], argumentsArray: Expression[], location?: TextRange, flags?: NodeFlags): NewExpression; function updateNew(node: NewExpression, expression: Expression, typeArguments: TypeNode[], argumentsArray: Expression[]): NewExpression; - function createTaggedTemplate(tag: Expression, template: Template, location?: TextRange): TaggedTemplateExpression; - function updateTaggedTemplate(node: TaggedTemplateExpression, tag: Expression, template: Template): TaggedTemplateExpression; + function createTaggedTemplate(tag: Expression, template: TemplateLiteral, location?: TextRange): TaggedTemplateExpression; + function updateTaggedTemplate(node: TaggedTemplateExpression, tag: Expression, template: TemplateLiteral): TaggedTemplateExpression; function createParen(expression: Expression, location?: TextRange): ParenthesizedExpression; function updateParen(node: ParenthesizedExpression, expression: Expression): ParenthesizedExpression; - function createFunctionExpression(asteriskToken: Node, name: string | Identifier, typeParameters: TypeParameterDeclaration[], parameters: ParameterDeclaration[], type: TypeNode, body: Block, location?: TextRange, flags?: NodeFlags): FunctionExpression; + function createFunctionExpression(asteriskToken: AsteriskToken, name: string | Identifier, typeParameters: TypeParameterDeclaration[], parameters: ParameterDeclaration[], type: TypeNode, body: Block, location?: TextRange, flags?: NodeFlags): FunctionExpression; function updateFunctionExpression(node: FunctionExpression, name: Identifier, typeParameters: TypeParameterDeclaration[], parameters: ParameterDeclaration[], type: TypeNode, body: Block): FunctionExpression; - function createArrowFunction(modifiers: Modifier[], typeParameters: TypeParameterDeclaration[], parameters: ParameterDeclaration[], type: TypeNode, equalsGreaterThanToken: Node, body: ConciseBody, location?: TextRange, flags?: NodeFlags): ArrowFunction; + function createArrowFunction(modifiers: Modifier[], typeParameters: TypeParameterDeclaration[], parameters: ParameterDeclaration[], type: TypeNode, equalsGreaterThanToken: EqualsGreaterThanToken, body: ConciseBody, location?: TextRange, flags?: NodeFlags): ArrowFunction; function updateArrowFunction(node: ArrowFunction, modifiers: Modifier[], typeParameters: TypeParameterDeclaration[], parameters: ParameterDeclaration[], type: TypeNode, body: ConciseBody): ArrowFunction; function createDelete(expression: Expression, location?: TextRange): DeleteExpression; function updateDelete(node: DeleteExpression, expression: Expression): Expression; @@ -7672,17 +8794,17 @@ declare namespace ts { function updateVoid(node: VoidExpression, expression: Expression): VoidExpression; function createAwait(expression: Expression, location?: TextRange): AwaitExpression; function updateAwait(node: AwaitExpression, expression: Expression): AwaitExpression; - function createPrefix(operator: SyntaxKind, operand: Expression, location?: TextRange): PrefixUnaryExpression; + function createPrefix(operator: PrefixUnaryOperator, operand: Expression, location?: TextRange): PrefixUnaryExpression; function updatePrefix(node: PrefixUnaryExpression, operand: Expression): PrefixUnaryExpression; - function createPostfix(operand: Expression, operator: SyntaxKind, location?: TextRange): PostfixUnaryExpression; + function createPostfix(operand: Expression, operator: PostfixUnaryOperator, location?: TextRange): PostfixUnaryExpression; function updatePostfix(node: PostfixUnaryExpression, operand: Expression): PostfixUnaryExpression; - function createBinary(left: Expression, operator: SyntaxKind | Node, right: Expression, location?: TextRange): BinaryExpression; + function createBinary(left: Expression, operator: BinaryOperator | BinaryOperatorToken, right: Expression, location?: TextRange): BinaryExpression; function updateBinary(node: BinaryExpression, left: Expression, right: Expression): BinaryExpression; - function createConditional(condition: Expression, questionToken: Node, whenTrue: Expression, colonToken: Node, whenFalse: Expression, location?: TextRange): ConditionalExpression; + function createConditional(condition: Expression, questionToken: QuestionToken, whenTrue: Expression, colonToken: ColonToken, whenFalse: Expression, location?: TextRange): ConditionalExpression; function updateConditional(node: ConditionalExpression, condition: Expression, whenTrue: Expression, whenFalse: Expression): ConditionalExpression; - function createTemplateExpression(head: TemplateLiteralFragment, templateSpans: TemplateSpan[], location?: TextRange): TemplateExpression; - function updateTemplateExpression(node: TemplateExpression, head: TemplateLiteralFragment, templateSpans: TemplateSpan[]): TemplateExpression; - function createYield(asteriskToken: Node, expression: Expression, location?: TextRange): YieldExpression; + function createTemplateExpression(head: TemplateHead, templateSpans: TemplateSpan[], location?: TextRange): TemplateExpression; + function updateTemplateExpression(node: TemplateExpression, head: TemplateHead, templateSpans: TemplateSpan[]): TemplateExpression; + function createYield(asteriskToken: AsteriskToken, expression: Expression, location?: TextRange): YieldExpression; function updateYield(node: YieldExpression, expression: Expression): YieldExpression; function createSpread(expression: Expression, location?: TextRange): SpreadElementExpression; function updateSpread(node: SpreadElementExpression, expression: Expression): SpreadElementExpression; @@ -7691,8 +8813,8 @@ declare namespace ts { function createOmittedExpression(location?: TextRange): OmittedExpression; function createExpressionWithTypeArguments(typeArguments: TypeNode[], expression: Expression, location?: TextRange): ExpressionWithTypeArguments; function updateExpressionWithTypeArguments(node: ExpressionWithTypeArguments, typeArguments: TypeNode[], expression: Expression): ExpressionWithTypeArguments; - function createTemplateSpan(expression: Expression, literal: TemplateLiteralFragment, location?: TextRange): TemplateSpan; - function updateTemplateSpan(node: TemplateSpan, expression: Expression, literal: TemplateLiteralFragment): TemplateSpan; + function createTemplateSpan(expression: Expression, literal: TemplateMiddle | TemplateTail, location?: TextRange): TemplateSpan; + function updateTemplateSpan(node: TemplateSpan, expression: Expression, literal: TemplateMiddle | TemplateTail): TemplateSpan; function createBlock(statements: Statement[], location?: TextRange, multiLine?: boolean, flags?: NodeFlags): Block; function updateBlock(node: Block, statements: Statement[]): Block; function createVariableStatement(modifiers: Modifier[], declarationList: VariableDeclarationList | VariableDeclaration[], location?: TextRange, flags?: NodeFlags): VariableStatement; @@ -7715,9 +8837,9 @@ declare namespace ts { function createForIn(initializer: ForInitializer, expression: Expression, statement: Statement, location?: TextRange): ForInStatement; function updateForIn(node: ForInStatement, initializer: ForInitializer, expression: Expression, statement: Statement): ForInStatement; function createForOf(initializer: ForInitializer, expression: Expression, statement: Statement, location?: TextRange): ForOfStatement; - function updateForOf(node: ForInStatement, initializer: ForInitializer, expression: Expression, statement: Statement): ForInStatement; - function createContinue(label?: Identifier, location?: TextRange): BreakStatement; - function updateContinue(node: ContinueStatement, label: Identifier): BreakStatement; + function updateForOf(node: ForOfStatement, initializer: ForInitializer, expression: Expression, statement: Statement): ForOfStatement; + function createContinue(label?: Identifier, location?: TextRange): ContinueStatement; + function updateContinue(node: ContinueStatement, label: Identifier): ContinueStatement; function createBreak(label?: Identifier, location?: TextRange): BreakStatement; function updateBreak(node: BreakStatement, label: Identifier): BreakStatement; function createReturn(expression?: Expression, location?: TextRange): ReturnStatement; @@ -7734,7 +8856,7 @@ declare namespace ts { function updateTry(node: TryStatement, tryBlock: Block, catchClause: CatchClause, finallyBlock: Block): TryStatement; function createCaseBlock(clauses: CaseOrDefaultClause[], location?: TextRange): CaseBlock; function updateCaseBlock(node: CaseBlock, clauses: CaseOrDefaultClause[]): CaseBlock; - function createFunctionDeclaration(decorators: Decorator[], modifiers: Modifier[], asteriskToken: Node, name: string | Identifier, typeParameters: TypeParameterDeclaration[], parameters: ParameterDeclaration[], type: TypeNode, body: Block, location?: TextRange, flags?: NodeFlags): FunctionDeclaration; + function createFunctionDeclaration(decorators: Decorator[], modifiers: Modifier[], asteriskToken: AsteriskToken, name: string | Identifier, typeParameters: TypeParameterDeclaration[], parameters: ParameterDeclaration[], type: TypeNode, body: Block, location?: TextRange, flags?: NodeFlags): FunctionDeclaration; function updateFunctionDeclaration(node: FunctionDeclaration, decorators: Decorator[], modifiers: Modifier[], name: Identifier, typeParameters: TypeParameterDeclaration[], parameters: ParameterDeclaration[], type: TypeNode, body: Block): FunctionDeclaration; function createClassDeclaration(decorators: Decorator[], modifiers: Modifier[], name: Identifier, typeParameters: TypeParameterDeclaration[], heritageClauses: HeritageClause[], members: ClassElement[], location?: TextRange): ClassDeclaration; function updateClassDeclaration(node: ClassDeclaration, decorators: Decorator[], modifiers: Modifier[], name: Identifier, typeParameters: TypeParameterDeclaration[], heritageClauses: HeritageClause[], members: ClassElement[]): ClassDeclaration; @@ -7828,6 +8950,7 @@ declare namespace ts { function createExpressionForPropertyName(memberName: PropertyName): Expression; function createExpressionForObjectLiteralElementLike(node: ObjectLiteralExpression, property: ObjectLiteralElementLike, receiver: Expression): Expression; function addPrologueDirectives(target: Statement[], source: Statement[], ensureUseStrict?: boolean, visitor?: (node: Node) => VisitResult): number; + function ensureUseStrict(node: SourceFile): SourceFile; function parenthesizeBinaryOperand(binaryOperator: SyntaxKind, operand: Expression, isLeftSideOfBinary: boolean, leftOperand?: Expression): Expression; function parenthesizeForNew(expression: Expression): LeftHandSideExpression; function parenthesizeForAccess(expression: Expression): LeftHandSideExpression; @@ -7852,6 +8975,17 @@ declare namespace ts { function skipPartiallyEmittedExpressions(node: Node): Node; function startOnNewLine(node: T): T; function setOriginalNode(node: T, original: Node): T; + function disposeEmitNodes(sourceFile: SourceFile): void; + function getEmitFlags(node: Node): EmitFlags; + function setEmitFlags(node: T, emitFlags: EmitFlags): T; + function setSourceMapRange(node: T, range: TextRange): T; + function setTokenSourceMapRange(node: T, token: SyntaxKind, range: TextRange): T; + function setCommentRange(node: T, range: TextRange): T; + function getCommentRange(node: Node): TextRange; + function getSourceMapRange(node: Node): TextRange; + function getTokenSourceMapRange(node: Node, token: SyntaxKind): TextRange; + function getConstantValue(node: PropertyAccessExpression | ElementAccessExpression): number; + function setConstantValue(node: PropertyAccessExpression | ElementAccessExpression, value: number): PropertyAccessExpression | ElementAccessExpression; function setTextRange(node: T, location: TextRange): T; function setNodeFlags(node: T, flags: NodeFlags): T; function setMultiLine(node: T, multiLine: boolean): T; @@ -7940,34 +9074,22 @@ declare namespace ts { } declare namespace ts { interface TransformationResult { - getSourceFiles(): SourceFile[]; - getTokenSourceMapRange(node: Node, token: SyntaxKind): TextRange; - isSubstitutionEnabled(node: Node): boolean; - isEmitNotificationEnabled(node: Node): boolean; - onSubstituteNode(node: Node, isExpression: boolean): Node; - onEmitNode(node: Node, emitCallback: (node: Node) => void): void; - dispose(): void; + transformed: SourceFile[]; + emitNodeWithSubstitution(emitContext: EmitContext, node: Node, emitCallback: (emitContext: EmitContext, node: Node) => void): void; + emitNodeWithNotification(emitContext: EmitContext, node: Node, emitCallback: (emitContext: EmitContext, node: Node) => void): void; } interface TransformationContext extends LexicalEnvironment { getCompilerOptions(): CompilerOptions; getEmitResolver(): EmitResolver; getEmitHost(): EmitHost; - getNodeEmitFlags(node: Node): NodeEmitFlags; - setNodeEmitFlags(node: T, flags: NodeEmitFlags): T; - getSourceMapRange(node: Node): TextRange; - setSourceMapRange(node: T, range: TextRange): T; - getTokenSourceMapRange(node: Node, token: SyntaxKind): TextRange; - setTokenSourceMapRange(node: T, token: SyntaxKind, range: TextRange): T; - getCommentRange(node: Node): TextRange; - setCommentRange(node: T, range: TextRange): T; hoistFunctionDeclaration(node: FunctionDeclaration): void; hoistVariableDeclaration(node: Identifier): void; enableSubstitution(kind: SyntaxKind): void; isSubstitutionEnabled(node: Node): boolean; - onSubstituteNode?: (node: Node, isExpression: boolean) => Node; + onSubstituteNode?: (emitContext: EmitContext, node: Node) => Node; enableEmitNotification(kind: SyntaxKind): void; isEmitNotificationEnabled(node: Node): boolean; - onEmitNode?: (node: Node, emit: (node: Node) => void) => void; + onEmitNode?: (emitContext: EmitContext, node: Node, emitCallback: (emitContext: EmitContext, node: Node) => void) => void; } type Transformer = (context: TransformationContext) => (node: SourceFile) => SourceFile; function getTransformers(compilerOptions: CompilerOptions): Transformer[]; @@ -7975,63 +9097,40 @@ declare namespace ts { } declare namespace ts { function getDeclarationDiagnostics(host: EmitHost, resolver: EmitResolver, targetSourceFile: SourceFile): Diagnostic[]; - function writeDeclarationFile(declarationFilePath: string, sourceFiles: SourceFile[], isBundledEmit: boolean, host: EmitHost, resolver: EmitResolver, emitterDiagnostics: DiagnosticCollection): boolean; + function writeDeclarationFile(declarationFilePath: string, sourceFiles: SourceFile[], isBundledEmit: boolean, host: EmitHost, resolver: EmitResolver, emitterDiagnostics: DiagnosticCollection, emitOnlyDtsFiles: boolean): boolean; } declare namespace ts { interface SourceMapWriter { initialize(filePath: string, sourceMapFilePath: string, sourceFiles: SourceFile[], isBundledEmit: boolean): void; reset(): void; - getSourceMapData(): SourceMapData; setSourceFile(sourceFile: SourceFile): void; emitPos(pos: number): void; - emitStart(range: TextRange): void; - emitStart(range: TextRange, contextNode: Node, ignoreNodeCallback: (node: Node) => boolean, ignoreChildrenCallback: (node: Node) => boolean, getTextRangeCallbackCallback: (node: Node) => TextRange): void; - emitEnd(range: TextRange): void; - emitEnd(range: TextRange, contextNode: Node, ignoreNodeCallback: (node: Node) => boolean, ignoreChildrenCallback: (node: Node) => boolean, getTextRangeCallbackCallback: (node: Node) => TextRange): void; - emitTokenStart(token: SyntaxKind, tokenStartPos: number): number; - emitTokenStart(token: SyntaxKind, tokenStartPos: number, contextNode: Node, ignoreTokenCallback: (node: Node, token: SyntaxKind) => boolean, getTokenTextRangeCallback: (node: Node, token: SyntaxKind) => TextRange): number; - emitTokenEnd(token: SyntaxKind, tokenEndPos: number): number; - emitTokenEnd(token: SyntaxKind, tokenEndPos: number, contextNode: Node, ignoreTokenCallback: (node: Node, token: SyntaxKind) => boolean, getTokenTextRangeCallback: (node: Node, token: SyntaxKind) => TextRange): number; - changeEmitSourcePos(): void; - stopOverridingSpan(): void; + emitNodeWithSourceMap(emitContext: EmitContext, node: Node, emitCallback: (emitContext: EmitContext, node: Node) => void): void; + emitTokenWithSourceMap(node: Node, token: SyntaxKind, tokenStartPos: number, emitCallback: (token: SyntaxKind, tokenStartPos: number) => number): number; getText(): string; getSourceMappingURL(): string; + getSourceMapData(): SourceMapData; } function createSourceMapWriter(host: EmitHost, writer: EmitTextWriter): SourceMapWriter; - function getNullSourceMapWriter(): SourceMapWriter; } declare namespace ts { interface CommentWriter { reset(): void; setSourceFile(sourceFile: SourceFile): void; - emitNodeWithComments(node: Node, emitCallback: (node: Node) => void): void; + emitNodeWithComments(emitContext: EmitContext, node: Node, emitCallback: (emitContext: EmitContext, node: Node) => void): void; emitBodyWithDetachedComments(node: Node, detachedRange: TextRange, emitCallback: (node: Node) => void): void; emitTrailingCommentsOfPosition(pos: number): void; } function createCommentWriter(host: EmitHost, writer: EmitTextWriter, sourceMap: SourceMapWriter): CommentWriter; } declare namespace ts { - function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFile: SourceFile): EmitResult; + function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFile: SourceFile, emitOnlyDtsFiles?: boolean): EmitResult; } declare namespace ts { const version = "2.1.0"; function findConfigFile(searchPath: string, fileExists: (fileName: string) => boolean, configName?: string): string; function resolveTripleslashReference(moduleName: string, containingFile: string): string; function computeCommonSourceDirectoryOfFilenames(fileNames: string[], currentDirectory: string, getCanonicalFileName: (fileName: string) => string): string; - function hasZeroOrOneAsteriskCharacter(str: string): boolean; - function getEffectiveTypeRoots(options: CompilerOptions, host: { - directoryExists?: (directoryName: string) => boolean; - getCurrentDirectory?: () => string; - }): string[] | undefined; - function resolveTypeReferenceDirective(typeReferenceDirectiveName: string, containingFile: string, options: CompilerOptions, host: ModuleResolutionHost): ResolvedTypeReferenceDirectiveWithFailedLookupLocations; - function resolveModuleName(moduleName: string, containingFile: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost): ResolvedModuleWithFailedLookupLocations; - function findBestPatternMatch(values: T[], getPattern: (value: T) => Pattern, candidate: string): T | undefined; - function tryParsePattern(pattern: string): Pattern | undefined; - function nodeModuleNameResolver(moduleName: string, containingFile: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost): ResolvedModuleWithFailedLookupLocations; - function directoryProbablyExists(directoryName: string, host: { - directoryExists?: (directoryName: string) => boolean; - }): boolean; - function classicNameResolver(moduleName: string, containingFile: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost): ResolvedModuleWithFailedLookupLocations; function createCompilerHost(options: CompilerOptions, setParentNodes?: boolean): CompilerHost; function getPreEmitDiagnostics(program: Program, sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[]; interface FormatDiagnosticsHost { @@ -8041,7 +9140,6 @@ declare namespace ts { } function formatDiagnostics(diagnostics: Diagnostic[], host: FormatDiagnosticsHost): string; function flattenDiagnosticMessageText(messageText: string | DiagnosticMessageChain, newLine: string): string; - function getAutomaticTypeDirectiveNames(options: CompilerOptions, host: ModuleResolutionHost): string[]; function createProgram(rootNames: string[], options: CompilerOptions, host?: CompilerHost, oldProgram?: Program): Program; } declare namespace ts { @@ -8135,6 +9233,7 @@ declare namespace ts { readDirectory?(path: string, extensions?: string[], exclude?: string[], include?: string[]): string[]; readFile?(path: string, encoding?: string): string; fileExists?(path: string): boolean; + getTypeRootsVersion?(): number; resolveModuleNames?(moduleNames: string[], containingFile: string): ResolvedModule[]; resolveTypeReferenceDirectives?(typeDirectiveNames: string[], containingFile: string): ResolvedTypeReferenceDirective[]; directoryExists?(directoryName: string): boolean; @@ -8165,18 +9264,20 @@ declare namespace ts { findReferences(fileName: string, position: number): ReferencedSymbol[]; getDocumentHighlights(fileName: string, position: number, filesToSearch: string[]): DocumentHighlights[]; getOccurrencesAtPosition(fileName: string, position: number): ReferenceEntry[]; - getNavigateToItems(searchValue: string, maxResultCount?: number, fileName?: string): NavigateToItem[]; + getNavigateToItems(searchValue: string, maxResultCount?: number, fileName?: string, excludeDtsFiles?: boolean): NavigateToItem[]; getNavigationBarItems(fileName: string): NavigationBarItem[]; + getNavigationTree(fileName: string): NavigationTree; getOutliningSpans(fileName: string): OutliningSpan[]; getTodoComments(fileName: string, descriptors: TodoCommentDescriptor[]): TodoComment[]; getBraceMatchingAtPosition(fileName: string, position: number): TextSpan[]; - getIndentationAtPosition(fileName: string, position: number, options: EditorOptions): number; - getFormattingEditsForRange(fileName: string, start: number, end: number, options: FormatCodeOptions): TextChange[]; - getFormattingEditsForDocument(fileName: string, options: FormatCodeOptions): TextChange[]; - getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: FormatCodeOptions): TextChange[]; + getIndentationAtPosition(fileName: string, position: number, options: EditorOptions | EditorSettings): number; + getFormattingEditsForRange(fileName: string, start: number, end: number, options: FormatCodeOptions | FormatCodeSettings): TextChange[]; + getFormattingEditsForDocument(fileName: string, options: FormatCodeOptions | FormatCodeSettings): TextChange[]; + getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: FormatCodeOptions | FormatCodeSettings): TextChange[]; getDocCommentTemplateAtPosition(fileName: string, position: number): TextInsertion; isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean; - getEmitOutput(fileName: string): EmitOutput; + getCodeFixesAtPosition(fileName: string, start: number, end: number, errorCodes: number[]): CodeAction[]; + getEmitOutput(fileName: string, emitOnlyDtsFiles?: boolean): EmitOutput; getProgram(): Program; getNonBoundSourceFile(fileName: string): SourceFile; getSourceFile(fileName: string): SourceFile; @@ -8200,6 +9301,13 @@ declare namespace ts { bolded: boolean; grayed: boolean; } + interface NavigationTree { + text: string; + kind: string; + kindModifiers: string; + spans: TextSpan[]; + childItems?: NavigationTree[]; + } interface TodoCommentDescriptor { text: string; priority: number; @@ -8213,6 +9321,14 @@ declare namespace ts { span: TextSpan; newText: string; } + interface FileTextChanges { + fileName: string; + textChanges: TextChange[]; + } + interface CodeAction { + description: string; + changes: FileTextChanges[]; + } interface TextInsertion { newText: string; caretOffset: number; @@ -8257,6 +9373,11 @@ declare namespace ts { containerName: string; containerKind: string; } + enum IndentStyle { + None = 0, + Block = 1, + Smart = 2, + } interface EditorOptions { BaseIndentSize?: number; IndentSize: number; @@ -8265,10 +9386,13 @@ declare namespace ts { ConvertTabsToSpaces: boolean; IndentStyle: IndentStyle; } - enum IndentStyle { - None = 0, - Block = 1, - Smart = 2, + interface EditorSettings { + baseIndentSize?: number; + indentSize?: number; + tabSize?: number; + newLineCharacter?: string; + convertTabsToSpaces?: boolean; + indentStyle?: IndentStyle; } interface FormatCodeOptions extends EditorOptions { InsertSpaceAfterCommaDelimiter: boolean; @@ -8284,7 +9408,21 @@ declare namespace ts { InsertSpaceAfterTypeAssertion?: boolean; PlaceOpenBraceOnNewLineForFunctions: boolean; PlaceOpenBraceOnNewLineForControlBlocks: boolean; - [s: string]: boolean | number | string | undefined; + } + interface FormatCodeSettings extends EditorSettings { + insertSpaceAfterCommaDelimiter?: boolean; + insertSpaceAfterSemicolonInForStatements?: boolean; + insertSpaceBeforeAndAfterBinaryOperators?: boolean; + insertSpaceAfterKeywordsInControlFlowStatements?: boolean; + insertSpaceAfterFunctionKeywordForAnonymousFunctions?: boolean; + insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis?: boolean; + insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets?: boolean; + insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces?: boolean; + insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces?: boolean; + insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces?: boolean; + insertSpaceAfterTypeAssertion?: boolean; + placeOpenBraceOnNewLineForFunctions?: boolean; + placeOpenBraceOnNewLineForControlBlocks?: boolean; } interface DefinitionInfo { fileName: string; @@ -8367,6 +9505,7 @@ declare namespace ts { argumentCount: number; } interface CompletionInfo { + isGlobalCompletion: boolean; isMemberCompletion: boolean; isNewIdentifierLocation: boolean; entries: CompletionEntry[]; @@ -8628,7 +9767,7 @@ declare namespace ts { function stripQuotes(name: string): string; function scriptKindIs(fileName: string, host: LanguageServiceHost, ...scriptKinds: ScriptKind[]): boolean; function getScriptKind(fileName: string, host?: LanguageServiceHost): ScriptKind; - function parseAndReEmitConfigJSONFile(content: string): { + function sanitizeConfigFile(configFileName: string, content: string): { configJsonObject: any; diagnostics: Diagnostic[]; }; @@ -8686,24 +9825,12 @@ declare namespace ts.JsDoc { function getAllJsDocCompletionEntries(): CompletionEntry[]; function getDocCommentTemplateAtPosition(newLine: string, sourceFile: SourceFile, position: number): TextInsertion; } -declare namespace ts.JsTyping { - interface TypingResolutionHost { - directoryExists: (path: string) => boolean; - fileExists: (fileName: string) => boolean; - readFile: (path: string, encoding?: string) => string; - readDirectory: (rootDir: string, extensions: string[], excludes: string[], includes: string[], depth?: number) => string[]; - } - function discoverTypings(host: TypingResolutionHost, fileNames: string[], projectRootPath: Path, safeListPath: Path, packageNameToTypingLocation: Map, typingOptions: TypingOptions, compilerOptions: CompilerOptions): { - cachedTypingPaths: string[]; - newTypingNames: string[]; - filesToWatch: string[]; - }; -} declare namespace ts.NavigateTo { - function getNavigateToItems(sourceFiles: SourceFile[], checker: TypeChecker, cancellationToken: CancellationToken, searchValue: string, maxResultCount: number): NavigateToItem[]; + function getNavigateToItems(sourceFiles: SourceFile[], checker: TypeChecker, cancellationToken: CancellationToken, searchValue: string, maxResultCount: number, excludeDtsFiles: boolean): NavigateToItem[]; } declare namespace ts.NavigationBar { function getNavigationBarItems(sourceFile: SourceFile): NavigationBarItem[]; + function getNavigationTree(sourceFile: SourceFile): NavigationTree; } declare namespace ts.OutliningElementsCollector { function collectElements(sourceFile: SourceFile): OutliningSpan[]; @@ -9148,7 +10275,7 @@ declare namespace ts.formatting { getRuleName(rule: Rule): string; getRuleByName(name: string): Rule; getRulesMap(): RulesMap; - ensureUpToDate(options: ts.FormatCodeOptions): void; + ensureUpToDate(options: ts.FormatCodeSettings): void; private createActiveRules(options); } } @@ -9161,35 +10288,58 @@ declare namespace ts.formatting { token: TextRangeWithKind; trailingTrivia: TextRangeWithKind[]; } - function formatOnEnter(position: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeOptions): TextChange[]; - function formatOnSemicolon(position: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeOptions): TextChange[]; - function formatOnClosingCurly(position: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeOptions): TextChange[]; - function formatDocument(sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeOptions): TextChange[]; - function formatSelection(start: number, end: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeOptions): TextChange[]; - function getIndentationString(indentation: number, options: FormatCodeOptions): string; + function formatOnEnter(position: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeSettings): TextChange[]; + function formatOnSemicolon(position: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeSettings): TextChange[]; + function formatOnClosingCurly(position: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeSettings): TextChange[]; + function formatDocument(sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeSettings): TextChange[]; + function formatSelection(start: number, end: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeSettings): TextChange[]; + function getIndentationString(indentation: number, options: EditorSettings): string; } declare namespace ts.formatting { namespace SmartIndenter { - function getIndentation(position: number, sourceFile: SourceFile, options: EditorOptions): number; - function getBaseIndentation(options: EditorOptions): number; - function getIndentationForNode(n: Node, ignoreActualIndentationRange: TextRange, sourceFile: SourceFile, options: FormatCodeOptions): number; + function getIndentation(position: number, sourceFile: SourceFile, options: EditorSettings): number; + function getIndentationForNode(n: Node, ignoreActualIndentationRange: TextRange, sourceFile: SourceFile, options: EditorSettings): number; + function getBaseIndentation(options: EditorSettings): number; function childStartsOnTheSameLineWithElseInIfStatement(parent: Node, child: TextRangeWithKind, childStartLine: number, sourceFile: SourceFile): boolean; - function findFirstNonWhitespaceCharacterAndColumn(startPos: number, endPos: number, sourceFile: SourceFile, options: EditorOptions): { + function findFirstNonWhitespaceCharacterAndColumn(startPos: number, endPos: number, sourceFile: SourceFile, options: EditorSettings): { column: number; character: number; }; - function findFirstNonWhitespaceColumn(startPos: number, endPos: number, sourceFile: SourceFile, options: EditorOptions): number; + function findFirstNonWhitespaceColumn(startPos: number, endPos: number, sourceFile: SourceFile, options: EditorSettings): number; function nodeWillIndentChild(parent: TextRangeWithKind, child: TextRangeWithKind, indentByDefault: boolean): boolean; function shouldIndentChildNode(parent: TextRangeWithKind, child?: TextRangeWithKind): boolean; } } +declare namespace ts { + interface CodeFix { + errorCodes: number[]; + getCodeActions(context: CodeFixContext): CodeAction[] | undefined; + } + interface CodeFixContext { + errorCode: number; + sourceFile: SourceFile; + span: TextSpan; + program: Program; + newLineCharacter: string; + } + namespace codefix { + function registerCodeFix(action: CodeFix): void; + function getSupportedErrorCodes(): string[]; + function getFixes(context: CodeFixContext): CodeAction[]; + } +} +declare namespace ts.codefix { +} declare namespace ts { const servicesVersion = "0.5"; interface DisplayPartsSymbolWriter extends SymbolWriter { displayParts(): SymbolDisplayPart[]; } + function toEditorSettings(options: FormatCodeOptions | FormatCodeSettings): FormatCodeSettings; + function toEditorSettings(options: EditorOptions | EditorSettings): EditorSettings; function displayPartsToString(displayParts: SymbolDisplayPart[]): string; function getDefaultCompilerOptions(): CompilerOptions; + function getSupportedCodeFixes(): string[]; function createLanguageServiceSourceFile(fileName: string, scriptSnapshot: IScriptSnapshot, scriptTarget: ScriptTarget, version: string, setNodeParents: boolean, scriptKind?: ScriptKind): SourceFile; let disableIncrementalParsing: boolean; function updateLanguageServiceSourceFile(sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile; @@ -9198,241 +10348,249 @@ declare namespace ts { function getDefaultLibFilePath(options: CompilerOptions): string; } declare namespace ts.server { - function generateSpaces(n: number): string; - function generateIndentString(n: number, editorOptions: EditorOptions): string; - interface PendingErrorCheck { - fileName: string; - project: Project; - } - namespace CommandNames { - const Brace = "brace"; - const Change = "change"; - const Close = "close"; - const Completions = "completions"; - const CompletionDetails = "completionEntryDetails"; - const Configure = "configure"; - const Definition = "definition"; - const Exit = "exit"; - const Format = "format"; - const Formatonkey = "formatonkey"; - const Geterr = "geterr"; - const GeterrForProject = "geterrForProject"; - const Implementation = "implementation"; - const SemanticDiagnosticsSync = "semanticDiagnosticsSync"; - const SyntacticDiagnosticsSync = "syntacticDiagnosticsSync"; - const NavBar = "navbar"; - const Navto = "navto"; - const Occurrences = "occurrences"; - const DocumentHighlights = "documentHighlights"; - const Open = "open"; - const Quickinfo = "quickinfo"; - const References = "references"; - const Reload = "reload"; - const Rename = "rename"; - const Saveto = "saveto"; - const SignatureHelp = "signatureHelp"; - const TypeDefinition = "typeDefinition"; - const ProjectInfo = "projectInfo"; - const ReloadProjects = "reloadProjects"; - const Unknown = "unknown"; - } - interface ServerHost extends ts.System { - setTimeout(callback: (...args: any[]) => void, ms: number, ...args: any[]): any; - clearTimeout(timeoutId: any): void; - } - class Session { - private host; - private byteLength; - private hrtime; - private logger; - protected projectService: ProjectService; - private errorTimer; - private immediateId; - private changeSeq; - constructor(host: ServerHost, byteLength: (buf: string, encoding?: string) => number, hrtime: (start?: number[]) => number[], logger: Logger); - private handleEvent(event); - logError(err: Error, cmd: string): void; - private sendLineToClient(line); - send(msg: protocol.Message): void; - configFileDiagnosticEvent(triggerFile: string, configFile: string, diagnostics: ts.Diagnostic[]): void; - event(info: any, eventName: string): void; - private response(info, cmdName, reqSeq?, errorMsg?); - output(body: any, commandName: string, requestSequence?: number, errorMessage?: string): void; - private semanticCheck(file, project); - private syntacticCheck(file, project); - private reloadProjects(); - private updateProjectStructure(seq, matchSeq, ms?); - private updateErrorCheck(checkList, seq, matchSeq, ms?, followMs?, requireOpen?); - private getDefinition(line, offset, fileName); - private getTypeDefinition(line, offset, fileName); - private getImplementation(line, offset, fileName); - private getOccurrences(line, offset, fileName); - private getDiagnosticsWorker(args, selector); - private getSyntacticDiagnosticsSync(args); - private getSemanticDiagnosticsSync(args); - private getDocumentHighlights(line, offset, fileName, filesToSearch); - private getProjectInfo(fileName, needFileNameList); - private getRenameLocations(line, offset, fileName, findInComments, findInStrings); - private getReferences(line, offset, fileName); - private openClientFile(fileName, fileContent?, scriptKind?); - private getQuickInfo(line, offset, fileName); - private getFormattingEditsForRange(line, offset, endLine, endOffset, fileName); - private getFormattingEditsAfterKeystroke(line, offset, key, fileName); - private getCompletions(line, offset, prefix, fileName); - private getCompletionEntryDetails(line, offset, entryNames, fileName); - private getSignatureHelpItems(line, offset, fileName); - private getDiagnostics(delay, fileNames); - private change(line, offset, endLine, endOffset, insertString, fileName); - private reload(fileName, tempFileName, reqSeq?); - private saveToTmp(fileName, tempFileName); - private closeClientFile(fileName); - private decorateNavigationBarItem(project, fileName, items, lineIndex); - private getNavigationBarItems(fileName); - private getNavigateToItems(searchValue, fileName, maxResultCount?, currentFileOnly?); - private getBraceMatching(line, offset, fileName); - getDiagnosticsForProject(delay: number, fileName: string): void; - getCanonicalFileName(fileName: string): string; - exit(): void; - private requiredResponse(response); - private handlers; - addProtocolHandler(command: string, handler: (request: protocol.Request) => { - response?: any; - responseRequired: boolean; - }): void; - executeCommand(request: protocol.Request): { - response?: any; - responseRequired?: boolean; - }; - onMessage(message: string): void; - } -} -declare namespace ts.server { - interface Logger { - close(): void; - isVerbose(): boolean; - loggingEnabled(): boolean; - perftrc(s: string): void; - info(s: string): void; - startGroup(): void; - endGroup(): void; - msg(s: string, type?: string): void; - } - const maxProgramSizeForNonTsFiles: number; class ScriptInfo { - private host; - fileName: string; + private readonly host; + readonly fileName: NormalizedPath; + readonly scriptKind: ScriptKind; isOpen: boolean; - svc: ScriptVersionCache; - children: ScriptInfo[]; - defaultProject: Project; - fileWatcher: FileWatcher; - formatCodeOptions: FormatCodeOptions; - path: Path; - scriptKind: ScriptKind; - constructor(host: ServerHost, fileName: string, content: string, isOpen?: boolean); - setFormatOptions(formatOptions: protocol.FormatOptions): void; - close(): void; - addChild(childInfo: ScriptInfo): void; + hasMixedContent: boolean; + readonly containingProjects: Project[]; + private formatCodeSettings; + readonly path: Path; + private fileWatcher; + private svc; + constructor(host: ServerHost, fileName: NormalizedPath, content: string, scriptKind: ScriptKind, isOpen?: boolean, hasMixedContent?: boolean); + getFormatCodeSettings(): FormatCodeSettings; + attachToProject(project: Project): boolean; + isAttached(project: Project): boolean; + detachFromProject(project: Project): void; + detachAllProjects(): void; + getDefaultProject(): Project; + setFormatOptions(formatSettings: FormatCodeSettings): void; + setWatcher(watcher: FileWatcher): void; + stopWatcher(): void; + getLatestVersion(): string; + reload(script: string): void; + saveTo(fileName: string): void; + reloadFromFile(): void; snap(): LineIndexSnapshot; - getText(): string; getLineInfo(line: number): ILineInfo; editContent(start: number, end: number, newText: string): void; - getTextChangeRangeBetweenVersions(startVersion: number, endVersion: number): ts.TextChangeRange; - getChangeRange(oldSnapshot: ts.IScriptSnapshot): ts.TextChangeRange; + markContainingProjectsAsDirty(): void; + lineToTextSpan(line: number): TextSpan; + lineOffsetToPosition(line: number, offset: number): number; + positionToLineOffset(position: number): ILineInfo; } - class LSHost implements ts.LanguageServiceHost { - host: ServerHost; - project: Project; - ls: ts.LanguageService; - compilationSettings: ts.CompilerOptions; - filenameToScript: ts.FileMap; - roots: ScriptInfo[]; - private resolvedModuleNames; - private resolvedTypeReferenceDirectives; - private moduleResolutionHost; - private getCanonicalFileName; - constructor(host: ServerHost, project: Project); +} +declare namespace ts.server { + class LSHost implements ts.LanguageServiceHost, ModuleResolutionHost, ServerLanguageServiceHost { + private readonly host; + private readonly project; + private readonly cancellationToken; + private compilationSettings; + private readonly resolvedModuleNames; + private readonly resolvedTypeReferenceDirectives; + private readonly getCanonicalFileName; + private readonly resolveModuleName; + readonly trace: (s: string) => void; + constructor(host: ServerHost, project: Project, cancellationToken: HostCancellationToken); private resolveNamesWithLocalCache(names, containingFile, cache, loader, getResult); + getProjectVersion(): string; + getCompilationSettings(): CompilerOptions; + useCaseSensitiveFileNames(): boolean; + getCancellationToken(): HostCancellationToken; resolveTypeReferenceDirectives(typeDirectiveNames: string[], containingFile: string): ResolvedTypeReferenceDirective[]; resolveModuleNames(moduleNames: string[], containingFile: string): ResolvedModule[]; getDefaultLibFileName(): string; getScriptSnapshot(filename: string): ts.IScriptSnapshot; - setCompilationSettings(opt: ts.CompilerOptions): void; - lineAffectsRefs(filename: string, line: number): boolean; - getCompilationSettings(): CompilerOptions; getScriptFileNames(): string[]; + getTypeRootsVersion(): number; getScriptKind(fileName: string): ScriptKind; getScriptVersion(filename: string): string; getCurrentDirectory(): string; - getScriptIsOpen(filename: string): boolean; - removeReferencedFile(info: ScriptInfo): void; - getScriptInfo(filename: string): ScriptInfo; - addRoot(info: ScriptInfo): void; - removeRoot(info: ScriptInfo): void; - saveTo(filename: string, tmpfilename: string): void; - reloadScript(filename: string, tmpfilename: string, cb: () => any): void; - editScript(filename: string, start: number, end: number, newText: string): void; + resolvePath(path: string): string; fileExists(path: string): boolean; + readFile(fileName: string): string; directoryExists(path: string): boolean; - getDirectories(path: string): string[]; readDirectory(path: string, extensions?: string[], exclude?: string[], include?: string[]): string[]; - readFile(path: string, encoding?: string): string; - lineToTextSpan(filename: string, line: number): ts.TextSpan; - lineOffsetToPosition(filename: string, line: number, offset: number): number; - positionToLineOffset(filename: string, position: number, lineIndex?: LineIndex): ILineInfo; - getLineIndex(filename: string): LineIndex; + getDirectories(path: string): string[]; + notifyFileRemoved(info: ScriptInfo): void; + setCompilationSettings(opt: ts.CompilerOptions): void; } - interface ProjectOptions { - files?: string[]; - wildcardDirectories?: ts.MapLike; - compilerOptions?: ts.CompilerOptions; - } - class Project { - projectService: ProjectService; - projectOptions: ProjectOptions; - languageServiceDiabled: boolean; - compilerService: CompilerService; - projectFilename: string; - projectFileWatcher: FileWatcher; - directoryWatcher: FileWatcher; - directoriesWatchedForWildcards: Map; - directoriesWatchedForTsconfig: string[]; - program: ts.Program; - filenameToSourceFile: Map; - updateGraphSeq: number; - openRefCount: number; - constructor(projectService: ProjectService, projectOptions?: ProjectOptions, languageServiceDiabled?: boolean); +} +declare namespace ts.server { + interface ITypingsInstaller { + enqueueInstallTypingsRequest(p: Project, typingOptions: TypingOptions): void; + attach(projectService: ProjectService): void; + onProjectClosed(p: Project): void; + readonly globalTypingsCacheLocation: string; + } + const nullTypingsInstaller: ITypingsInstaller; + interface TypingsArray extends ReadonlyArray { + " __typingsArrayBrand": any; + } + class TypingsCache { + private readonly installer; + private readonly perProjectCache; + constructor(installer: ITypingsInstaller); + getTypingsForProject(project: Project, forceRefresh: boolean): TypingsArray; + invalidateCachedTypingsForProject(project: Project): void; + updateTypingsForProject(projectName: string, compilerOptions: CompilerOptions, typingOptions: TypingOptions, newTypings: string[]): void; + onProjectClosed(project: Project): void; + } +} +declare namespace ts.server { + function shouldEmitFile(scriptInfo: ScriptInfo): boolean; + class BuilderFileInfo { + readonly scriptInfo: ScriptInfo; + readonly project: Project; + private lastCheckedShapeSignature; + constructor(scriptInfo: ScriptInfo, project: Project); + isExternalModuleOrHasOnlyAmbientExternalModules(): boolean; + private containsOnlyAmbientModules(sourceFile); + private computeHash(text); + private getSourceFile(); + updateShapeSignature(): boolean; + } + interface Builder { + readonly project: Project; + getFilesAffectedBy(scriptInfo: ScriptInfo): string[]; + onProjectUpdateGraph(): void; + emitFile(scriptInfo: ScriptInfo, writeFile: (path: string, data: string, writeByteOrderMark?: boolean) => void): boolean; + } + function createBuilder(project: Project): Builder; +} +declare namespace ts.server { + enum ProjectKind { + Inferred = 0, + Configured = 1, + External = 2, + } + function allRootFilesAreJsOrDts(project: Project): boolean; + function allFilesAreJsOrDts(project: Project): boolean; + interface ProjectFilesWithTSDiagnostics extends protocol.ProjectFiles { + projectErrors: Diagnostic[]; + } + abstract class Project { + readonly projectKind: ProjectKind; + readonly projectService: ProjectService; + private documentRegistry; + languageServiceEnabled: boolean; + private compilerOptions; + compileOnSaveEnabled: boolean; + private rootFiles; + private rootFilesMap; + private lsHost; + private program; + private languageService; + builder: Builder; + private lastReportedFileNames; + private lastReportedVersion; + private projectStructureVersion; + private projectStateVersion; + private typingFiles; + protected projectErrors: Diagnostic[]; + typesVersion: number; + isNonTsProject(): boolean; + isJsOnlyProject(): boolean; + constructor(projectKind: ProjectKind, projectService: ProjectService, documentRegistry: ts.DocumentRegistry, hasExplicitListOfFiles: boolean, languageServiceEnabled: boolean, compilerOptions: CompilerOptions, compileOnSaveEnabled: boolean); + getProjectErrors(): Diagnostic[]; + getLanguageService(ensureSynchronized?: boolean): LanguageService; + getCompileOnSaveAffectedFileList(scriptInfo: ScriptInfo): string[]; + getProjectVersion(): string; enableLanguageService(): void; disableLanguageService(): void; - addOpenRef(): void; - deleteOpenRef(): number; - openReferencedFile(filename: string): ScriptInfo; - getRootFiles(): string[]; - getFileNames(): string[]; - getSourceFile(info: ScriptInfo): SourceFile; - getSourceFileFromName(filename: string, requireOpen?: boolean): SourceFile; + abstract getProjectName(): string; + abstract getProjectRootPath(): string | undefined; + abstract getTypingOptions(): TypingOptions; + getSourceFile(path: Path): SourceFile; + updateTypes(): void; + close(): void; + getCompilerOptions(): CompilerOptions; + hasRoots(): boolean; + getRootFiles(): NormalizedPath[]; + getRootFilesLSHost(): string[]; + getRootScriptInfos(): ScriptInfo[]; + getScriptInfos(): ScriptInfo[]; + getFileEmitOutput(info: ScriptInfo, emitOnlyDtsFiles: boolean): EmitOutput; + getFileNames(): NormalizedPath[]; + getAllEmittableFiles(): string[]; + containsScriptInfo(info: ScriptInfo): boolean; + containsFile(filename: NormalizedPath, requireOpen?: boolean): boolean; isRoot(info: ScriptInfo): boolean; - removeReferencedFile(info: ScriptInfo): void; - updateFileMap(): void; - finishGraph(): void; - updateGraph(): void; - isConfiguredProject(): string; addRoot(info: ScriptInfo): void; - removeRoot(info: ScriptInfo): void; + removeFile(info: ScriptInfo, detachFromProject?: boolean): void; + markAsDirty(): void; + updateGraph(): boolean; + private setTypings(typings); + private updateGraphWorker(); + getScriptInfoLSHost(fileName: string): ScriptInfo; + getScriptInfoForNormalizedPath(fileName: NormalizedPath): ScriptInfo; + getScriptInfo(uncheckedFileName: string): ScriptInfo; filesToString(): string; - setProjectOptions(projectOptions: ProjectOptions): void; - } - interface ProjectOpenResult { - success?: boolean; - errorMsg?: string; - project?: Project; + setCompilerOptions(compilerOptions: CompilerOptions): void; + reloadScript(filename: NormalizedPath): boolean; + getChangesSinceVersion(lastKnownVersion?: number): ProjectFilesWithTSDiagnostics; + getReferencedFiles(path: Path): Path[]; + private removeRootFileIfNecessary(info); + } + class InferredProject extends Project { + compileOnSaveEnabled: boolean; + private static NextId; + private readonly inferredProjectName; + directoriesWatchedForTsconfig: string[]; + constructor(projectService: ProjectService, documentRegistry: ts.DocumentRegistry, languageServiceEnabled: boolean, compilerOptions: CompilerOptions, compileOnSaveEnabled: boolean); + getProjectName(): string; + getProjectRootPath(): string; + close(): void; + getTypingOptions(): TypingOptions; + } + class ConfiguredProject extends Project { + readonly configFileName: NormalizedPath; + private wildcardDirectories; + compileOnSaveEnabled: boolean; + private typingOptions; + private projectFileWatcher; + private directoryWatcher; + private directoriesWatchedForWildcards; + private typeRootsWatchers; + openRefCount: number; + constructor(configFileName: NormalizedPath, projectService: ProjectService, documentRegistry: ts.DocumentRegistry, hasExplicitListOfFiles: boolean, compilerOptions: CompilerOptions, wildcardDirectories: Map, languageServiceEnabled: boolean, compileOnSaveEnabled: boolean); + getProjectRootPath(): string; + setProjectErrors(projectErrors: Diagnostic[]): void; + setTypingOptions(newTypingOptions: TypingOptions): void; + getTypingOptions(): TypingOptions; + getProjectName(): NormalizedPath; + watchConfigFile(callback: (project: ConfiguredProject) => void): void; + watchTypeRoots(callback: (project: ConfiguredProject, path: string) => void): void; + watchConfigDirectory(callback: (project: ConfiguredProject, path: string) => void): void; + watchWildcards(callback: (project: ConfiguredProject, path: string) => void): void; + stopWatchingDirectory(): void; + close(): void; + addOpenRef(): void; + deleteOpenRef(): number; + getEffectiveTypeRoots(): string[]; + } + class ExternalProject extends Project { + readonly externalProjectName: string; + compileOnSaveEnabled: boolean; + private readonly projectFilePath; + private typingOptions; + constructor(externalProjectName: string, projectService: ProjectService, documentRegistry: ts.DocumentRegistry, compilerOptions: CompilerOptions, languageServiceEnabled: boolean, compileOnSaveEnabled: boolean, projectFilePath?: string); + getProjectRootPath(): string; + getTypingOptions(): TypingOptions; + setProjectErrors(projectErrors: Diagnostic[]): void; + setTypingOptions(newTypingOptions: TypingOptions): void; + getProjectName(): string; } - function combineProjectOutput(projects: Project[], action: (project: Project) => T[], comparer?: (a: T, b: T) => number, areEqual?: (a: T, b: T) => boolean): T[]; +} +declare namespace ts.server { + const maxProgramSizeForNonTsFiles: number; type ProjectServiceEvent = { eventName: "context"; data: { project: Project; - fileName: string; + fileName: NormalizedPath; }; } | { eventName: "configFileDiag"; @@ -9445,89 +10603,275 @@ declare namespace ts.server { interface ProjectServiceEventHandler { (event: ProjectServiceEvent): void; } + function combineProjectOutput(projects: Project[], action: (project: Project) => T[], comparer?: (a: T, b: T) => number, areEqual?: (a: T, b: T) => boolean): T[]; interface HostConfiguration { - formatCodeOptions: ts.FormatCodeOptions; + formatCodeOptions: FormatCodeSettings; hostInfo: string; } + interface OpenConfiguredProjectResult { + configFileName?: string; + configFileErrors?: Diagnostic[]; + } class ProjectService { - host: ServerHost; - psLogger: Logger; - eventHandler: ProjectServiceEventHandler; - filenameToScriptInfo: Map; - openFileRoots: ScriptInfo[]; - inferredProjects: Project[]; - configuredProjects: Project[]; - openFilesReferenced: ScriptInfo[]; - openFileRootsConfigured: ScriptInfo[]; - directoryWatchersForTsconfig: Map; - directoryWatchersRefCount: Map; - hostConfiguration: HostConfiguration; - timerForDetectingProjectFileListChanges: Map; - constructor(host: ServerHost, psLogger: Logger, eventHandler?: ProjectServiceEventHandler); - addDefaultHostConfiguration(): void; - getFormatCodeOptions(file?: string): FormatCodeOptions; - watchedFileChanged(fileName: string): void; - directoryWatchedForSourceFilesChanged(project: Project, fileName: string): void; - startTimerForDetectingProjectFileListChanges(project: Project): void; - handleProjectFileListChanges(project: Project): void; - reportConfigFileDiagnostics(configFileName: string, diagnostics: Diagnostic[], triggerFile?: string): void; - directoryWatchedForTsconfigChanged(fileName: string): void; - getCanonicalFileName(fileName: string): string; - watchedProjectConfigFileChanged(project: Project): void; - log(msg: string, type?: string): void; - setHostConfiguration(args: ts.server.protocol.ConfigureRequestArguments): void; + readonly host: ServerHost; + readonly logger: Logger; + readonly cancellationToken: HostCancellationToken; + readonly useSingleInferredProject: boolean; + readonly typingsInstaller: ITypingsInstaller; + private readonly eventHandler; + readonly typingsCache: TypingsCache; + private readonly documentRegistry; + private readonly filenameToScriptInfo; + private readonly externalProjectToConfiguredProjectMap; + readonly externalProjects: ExternalProject[]; + readonly inferredProjects: InferredProject[]; + readonly configuredProjects: ConfiguredProject[]; + readonly openFiles: ScriptInfo[]; + private compilerOptionsForInferredProjects; + private compileOnSaveForInferredProjects; + private readonly directoryWatchers; + private readonly throttledOperations; + private readonly hostConfiguration; + private changedFiles; + private toCanonicalFileName; + lastDeletedFile: ScriptInfo; + constructor(host: ServerHost, logger: Logger, cancellationToken: HostCancellationToken, useSingleInferredProject: boolean, typingsInstaller?: ITypingsInstaller, eventHandler?: ProjectServiceEventHandler); + getChangedFiles_TestOnly(): ScriptInfo[]; + ensureInferredProjectsUpToDate_TestOnly(): void; + updateTypingsForProject(response: SetTypings | InvalidateCachedTypings): void; + setCompilerOptionsForInferredProjects(projectCompilerOptions: protocol.ExternalProjectCompilerOptions): void; + stopWatchingDirectory(directory: string): void; + findProject(projectName: string): Project; + getDefaultProjectForFile(fileName: NormalizedPath, refreshInferredProjects: boolean): Project; + private ensureInferredProjectsUpToDate(); + private findContainingExternalProject(fileName); + getFormatCodeOptions(file?: NormalizedPath): FormatCodeSettings; + private updateProjectGraphs(projects); + private onSourceFileChanged(fileName); + private handleDeletedFile(info); + private onTypeRootFileChanged(project, fileName); + private onSourceFileInDirectoryChangedForConfiguredProject(project, fileName); + private handleChangeInSourceFileForConfiguredProject(project); + private onConfigChangedForConfiguredProject(project); + private onConfigFileAddedForInferredProject(fileName); + private getCanonicalFileName(fileName); + private removeProject(project); + private assignScriptInfoToInferredProjectIfNecessary(info, addToListOfOpenFiles); + private closeOpenFile(info); + private openOrUpdateConfiguredProjectForFile(fileName); + private findConfigFile(searchPath); + private printProjects(); + private findConfiguredProjectByProjectName(configFileName); + private findExternalProjectByProjectName(projectFileName); + private convertConfigFileContentToProjectOptions(configFilename); + private exceededTotalSizeLimitForNonTsFiles(options, fileNames, propertyReader); + private createAndAddExternalProject(projectFileName, files, options, typingOptions); + private reportConfigFileDiagnostics(configFileName, diagnostics, triggerFile?); + private createAndAddConfiguredProject(configFileName, projectOptions, configFileErrors, clientFileName?); + private watchConfigDirectoryForProject(project, options); + private addFilesToProjectAndUpdateGraph(project, files, propertyReader, clientFileName, typingOptions, configFileErrors); + private openConfigFile(configFileName, clientFileName?); + private updateNonInferredProject(project, newUncheckedFiles, propertyReader, newOptions, newTypingOptions, compileOnSave, configFileErrors); + private updateConfiguredProject(project); + createInferredProjectWithRootFileIfNecessary(root: ScriptInfo): InferredProject; + getOrCreateScriptInfo(uncheckedFileName: string, openedByClient: boolean, fileContent?: string, scriptKind?: ScriptKind): ScriptInfo; + getScriptInfo(uncheckedFileName: string): ScriptInfo; + getOrCreateScriptInfoForNormalizedPath(fileName: NormalizedPath, openedByClient: boolean, fileContent?: string, scriptKind?: ScriptKind, hasMixedContent?: boolean): ScriptInfo; + getScriptInfoForNormalizedPath(fileName: NormalizedPath): ScriptInfo; + getScriptInfoForPath(fileName: Path): ScriptInfo; + setHostConfiguration(args: protocol.ConfigureRequestArguments): void; closeLog(): void; - createInferredProject(root: ScriptInfo): Project; - fileDeletedInFilesystem(info: ScriptInfo): void; - updateConfiguredProjectList(): void; - removeProject(project: Project): void; - setConfiguredProjectRoot(info: ScriptInfo): boolean; - addOpenFile(info: ScriptInfo): void; - closeOpenFile(info: ScriptInfo): void; - findReferencingProjects(info: ScriptInfo, excludedProject?: Project): Project[]; reloadProjects(): void; - updateProjectStructure(): void; - getScriptInfo(filename: string): ScriptInfo; - openFile(fileName: string, openedByClient: boolean, fileContent?: string, scriptKind?: ScriptKind): ScriptInfo; - findConfigFile(searchPath: string): string; - openClientFile(fileName: string, fileContent?: string, scriptKind?: ScriptKind): { - configFileName?: string; - configFileErrors?: Diagnostic[]; - }; - openOrUpdateConfiguredProjectForFile(fileName: string): { - configFileName?: string; - configFileErrors?: Diagnostic[]; - }; - closeClientFile(filename: string): void; - getProjectForFile(filename: string): Project; - printProjectsForFile(filename: string): void; - printProjects(): void; - configProjectIsActive(fileName: string): boolean; - findConfiguredProjectByConfigFile(configFileName: string): Project; - configFileToProjectOptions(configFilename: string): { - projectOptions?: ProjectOptions; - errors: Diagnostic[]; - }; - private exceedTotalNonTsFileSizeLimit(fileNames); - openConfigFile(configFilename: string, clientFileName?: string): { - project?: Project; - errors: Diagnostic[]; - }; - updateConfiguredProject(project: Project): Diagnostic[]; - createProject(projectFilename: string, projectOptions?: ProjectOptions, languageServiceDisabled?: boolean): Project; - } - class CompilerService { + refreshInferredProjects(): void; + openClientFile(fileName: string, fileContent?: string, scriptKind?: ScriptKind): OpenConfiguredProjectResult; + openClientFileWithNormalizedPath(fileName: NormalizedPath, fileContent?: string, scriptKind?: ScriptKind, hasMixedContent?: boolean): OpenConfiguredProjectResult; + closeClientFile(uncheckedFileName: string): void; + private collectChanges(lastKnownProjectVersions, currentProjects, result); + synchronizeProjectList(knownProjects: protocol.ProjectVersionInfo[]): ProjectFilesWithTSDiagnostics[]; + applyChangesInOpenFiles(openFiles: protocol.ExternalFile[], changedFiles: protocol.ChangedOpenFile[], closedFiles: string[]): void; + private closeConfiguredProject(configFile); + closeExternalProject(uncheckedFileName: string, suppressRefresh?: boolean): void; + openExternalProject(proj: protocol.ExternalProject): void; + } +} +declare namespace ts.server { + interface PendingErrorCheck { + fileName: NormalizedPath; project: Project; - host: LSHost; - languageService: ts.LanguageService; - classifier: ts.Classifier; - settings: ts.CompilerOptions; - documentRegistry: DocumentRegistry; - constructor(project: Project, opt?: ts.CompilerOptions); - setCompilerOptions(opt: ts.CompilerOptions): void; - isExternalModule(filename: string): boolean; - static getDefaultFormatCodeOptions(host: ServerHost): ts.FormatCodeOptions; } + namespace CommandNames { + const Brace: protocol.CommandTypes.Brace; + const BraceFull: protocol.CommandTypes.BraceFull; + const BraceCompletion: protocol.CommandTypes.BraceCompletion; + const Change: protocol.CommandTypes.Change; + const Close: protocol.CommandTypes.Close; + const Completions: protocol.CommandTypes.Completions; + const CompletionsFull: protocol.CommandTypes.CompletionsFull; + const CompletionDetails: protocol.CommandTypes.CompletionDetails; + const CompileOnSaveAffectedFileList: protocol.CommandTypes.CompileOnSaveAffectedFileList; + const CompileOnSaveEmitFile: protocol.CommandTypes.CompileOnSaveEmitFile; + const Configure: protocol.CommandTypes.Configure; + const Definition: protocol.CommandTypes.Definition; + const DefinitionFull: protocol.CommandTypes.DefinitionFull; + const Exit: protocol.CommandTypes.Exit; + const Format: protocol.CommandTypes.Format; + const Formatonkey: protocol.CommandTypes.Formatonkey; + const FormatFull: protocol.CommandTypes.FormatFull; + const FormatonkeyFull: protocol.CommandTypes.FormatonkeyFull; + const FormatRangeFull: protocol.CommandTypes.FormatRangeFull; + const Geterr: protocol.CommandTypes.Geterr; + const GeterrForProject: protocol.CommandTypes.GeterrForProject; + const Implementation: protocol.CommandTypes.Implementation; + const ImplementationFull: protocol.CommandTypes.ImplementationFull; + const SemanticDiagnosticsSync: protocol.CommandTypes.SemanticDiagnosticsSync; + const SyntacticDiagnosticsSync: protocol.CommandTypes.SyntacticDiagnosticsSync; + const NavBar: protocol.CommandTypes.NavBar; + const NavBarFull: protocol.CommandTypes.NavBarFull; + const NavTree: protocol.CommandTypes.NavTree; + const NavTreeFull: protocol.CommandTypes.NavTreeFull; + const Navto: protocol.CommandTypes.Navto; + const NavtoFull: protocol.CommandTypes.NavtoFull; + const Occurrences: protocol.CommandTypes.Occurrences; + const DocumentHighlights: protocol.CommandTypes.DocumentHighlights; + const DocumentHighlightsFull: protocol.CommandTypes.DocumentHighlightsFull; + const Open: protocol.CommandTypes.Open; + const Quickinfo: protocol.CommandTypes.Quickinfo; + const QuickinfoFull: protocol.CommandTypes.QuickinfoFull; + const References: protocol.CommandTypes.References; + const ReferencesFull: protocol.CommandTypes.ReferencesFull; + const Reload: protocol.CommandTypes.Reload; + const Rename: protocol.CommandTypes.Rename; + const RenameInfoFull: protocol.CommandTypes.RenameInfoFull; + const RenameLocationsFull: protocol.CommandTypes.RenameLocationsFull; + const Saveto: protocol.CommandTypes.Saveto; + const SignatureHelp: protocol.CommandTypes.SignatureHelp; + const SignatureHelpFull: protocol.CommandTypes.SignatureHelpFull; + const TypeDefinition: protocol.CommandTypes.TypeDefinition; + const ProjectInfo: protocol.CommandTypes.ProjectInfo; + const ReloadProjects: protocol.CommandTypes.ReloadProjects; + const Unknown: protocol.CommandTypes.Unknown; + const OpenExternalProject: protocol.CommandTypes.OpenExternalProject; + const OpenExternalProjects: protocol.CommandTypes.OpenExternalProjects; + const CloseExternalProject: protocol.CommandTypes.CloseExternalProject; + const SynchronizeProjectList: protocol.CommandTypes.SynchronizeProjectList; + const ApplyChangedToOpenFiles: protocol.CommandTypes.ApplyChangedToOpenFiles; + const EncodedSemanticClassificationsFull: protocol.CommandTypes.EncodedSemanticClassificationsFull; + const Cleanup: protocol.CommandTypes.Cleanup; + const OutliningSpans: protocol.CommandTypes.OutliningSpans; + const TodoComments: protocol.CommandTypes.TodoComments; + const Indentation: protocol.CommandTypes.Indentation; + const DocCommentTemplate: protocol.CommandTypes.DocCommentTemplate; + const CompilerOptionsDiagnosticsFull: protocol.CommandTypes.CompilerOptionsDiagnosticsFull; + const NameOrDottedNameSpan: protocol.CommandTypes.NameOrDottedNameSpan; + const BreakpointStatement: protocol.CommandTypes.BreakpointStatement; + const CompilerOptionsForInferredProjects: protocol.CommandTypes.CompilerOptionsForInferredProjects; + const GetCodeFixes: protocol.CommandTypes.GetCodeFixes; + const GetCodeFixesFull: protocol.CommandTypes.GetCodeFixesFull; + const GetSupportedCodeFixes: protocol.CommandTypes.GetSupportedCodeFixes; + } + function formatMessage(msg: T, logger: server.Logger, byteLength: (s: string, encoding: string) => number, newLine: string): string; + class Session { + private host; + protected readonly typingsInstaller: ITypingsInstaller; + private byteLength; + private hrtime; + protected logger: Logger; + protected readonly canUseEvents: boolean; + private readonly gcTimer; + protected projectService: ProjectService; + private errorTimer; + private immediateId; + private changeSeq; + private eventHander; + constructor(host: ServerHost, cancellationToken: HostCancellationToken, useSingleInferredProject: boolean, typingsInstaller: ITypingsInstaller, byteLength: (buf: string, encoding?: string) => number, hrtime: (start?: number[]) => number[], logger: Logger, canUseEvents: boolean, eventHandler?: ProjectServiceEventHandler); + private defaultEventHandler(event); + logError(err: Error, cmd: string): void; + send(msg: protocol.Message): void; + configFileDiagnosticEvent(triggerFile: string, configFile: string, diagnostics: ts.Diagnostic[]): void; + event(info: any, eventName: string): void; + output(info: any, cmdName: string, reqSeq?: number, errorMsg?: string): void; + private semanticCheck(file, project); + private syntacticCheck(file, project); + private updateProjectStructure(seq, matchSeq, ms?); + private updateErrorCheck(checkList, seq, matchSeq, ms?, followMs?, requireOpen?); + private cleanProjects(caption, projects); + private cleanup(); + private getEncodedSemanticClassifications(args); + private getProject(projectFileName); + private getCompilerOptionsDiagnostics(args); + private convertToDiagnosticsWithLinePosition(diagnostics, scriptInfo); + private getDiagnosticsWorker(args, selector, includeLinePosition); + private getDefinition(args, simplifiedResult); + private getTypeDefinition(args); + private getImplementation(args, simplifiedResult); + private getOccurrences(args); + private getSyntacticDiagnosticsSync(args); + private getSemanticDiagnosticsSync(args); + private getDocumentHighlights(args, simplifiedResult); + private setCompilerOptionsForInferredProjects(args); + private getProjectInfo(args); + private getProjectInfoWorker(uncheckedFileName, projectFileName, needFileNameList); + private getRenameInfo(args); + private getProjects(args); + private getRenameLocations(args, simplifiedResult); + private getReferences(args, simplifiedResult); + private openClientFile(fileName, fileContent?, scriptKind?); + private getPosition(args, scriptInfo); + private getFileAndProject(args, errorOnMissingProject?); + private getFileAndProjectWithoutRefreshingInferredProjects(args, errorOnMissingProject?); + private getFileAndProjectWorker(uncheckedFileName, projectFileName, refreshInferredProjects, errorOnMissingProject); + private getOutliningSpans(args); + private getTodoComments(args); + private getDocCommentTemplate(args); + private getIndentation(args); + private getBreakpointStatement(args); + private getNameOrDottedNameSpan(args); + private isValidBraceCompletion(args); + private getQuickInfoWorker(args, simplifiedResult); + private getFormattingEditsForRange(args); + private getFormattingEditsForRangeFull(args); + private getFormattingEditsForDocumentFull(args); + private getFormattingEditsAfterKeystrokeFull(args); + private getFormattingEditsAfterKeystroke(args); + private getCompletions(args, simplifiedResult); + private getCompletionEntryDetails(args); + private getCompileOnSaveAffectedFileList(args); + private emitFile(args); + private getSignatureHelpItems(args, simplifiedResult); + private getDiagnostics(delay, fileNames); + private change(args); + private reload(args, reqSeq); + private saveToTmp(fileName, tempFileName); + private closeClientFile(fileName); + private decorateNavigationBarItems(items, scriptInfo); + private getNavigationBarItems(args, simplifiedResult); + private decorateNavigationTree(tree, scriptInfo); + private decorateSpan(span, scriptInfo); + private getNavigationTree(args, simplifiedResult); + private getNavigateToItems(args, simplifiedResult); + private getSupportedCodeFixes(); + private getCodeFixes(args, simplifiedResult); + private mapCodeAction(codeAction, scriptInfo); + private convertTextChangeToCodeEdit(change, scriptInfo); + private getBraceMatching(args, simplifiedResult); + getDiagnosticsForProject(delay: number, fileName: string): void; + getCanonicalFileName(fileName: string): string; + exit(): void; + private notRequired(); + private requiredResponse(response); + private handlers; + addProtocolHandler(command: string, handler: (request: protocol.Request) => { + response?: any; + responseRequired: boolean; + }): void; + executeCommand(request: protocol.Request): { + response?: any; + responseRequired?: boolean; + }; + onMessage(message: string): void; + } +} +declare namespace ts.server { interface LineCollection { charCount(): number; lineCount(): number; @@ -9566,15 +10910,17 @@ declare namespace ts.server { changes: TextChange[]; versions: LineIndexSnapshot[]; minVersion: number; - private currentVersion; private host; + private currentVersion; static changeNumberThreshold: number; static changeLengthThreshold: number; static maxVersions: number; + private versionToIndex(version); + private currentVersionToIndex(); edit(pos: number, deleteLen: number, insertedText?: string): void; latest(): LineIndexSnapshot; latestVersion(): number; - reloadFromFile(filename: string, cb?: () => any): void; + reloadFromFile(filename: string): void; reload(script: string): void; getSnapshot(): LineIndexSnapshot; getTextChangesBetweenVersions(oldVersion: number, newVersion: number): TextChangeRange; @@ -9643,10 +10989,7 @@ declare namespace ts.server { } class LineLeaf implements LineCollection { text: string; - udata: any; constructor(text: string); - setUdata(data: any): void; - getUdata(): any; isLeaf(): boolean; walk(rangeStart: number, rangeLength: number, walkFns: ILineIndexWalker): void; charCount(): number; @@ -9680,6 +11023,7 @@ declare namespace ts { getNewLine?(): string; getProjectVersion?(): string; useCaseSensitiveFileNames?(): boolean; + getTypeRootsVersion?(): number; readDirectory(rootDir: string, extension: string, basePaths?: string, excludeEx?: string, includeFileEx?: string, includeDirEx?: string, depth?: number): string; readFile(path: string, encoding?: string): string; fileExists(path: string): boolean; @@ -9739,6 +11083,7 @@ declare namespace ts { getDocumentHighlights(fileName: string, position: number, filesToSearch: string): string; getNavigateToItems(searchValue: string, maxResultCount?: number, fileName?: string): string; getNavigationBarItems(fileName: string): string; + getNavigationTree(fileName: string): string; getOutliningSpans(fileName: string): string; getTodoComments(fileName: string, todoCommentDescriptors: string): string; getBraceMatchingAtPosition(fileName: string, position: number): string; @@ -9775,6 +11120,7 @@ declare namespace ts { trace(s: string): void; error(s: string): void; getProjectVersion(): string; + getTypeRootsVersion(): number; useCaseSensitiveFileNames(): boolean; getCompilationSettings(): CompilerOptions; getScriptFileNames(): string[]; diff --git a/lib/tsserverlibrary.js b/lib/tsserverlibrary.js index a8fa909efcccb..a360ac081ec98 100644 --- a/lib/tsserverlibrary.js +++ b/lib/tsserverlibrary.js @@ -72,7 +72,6 @@ var ts; (function (ts) { ts.timestamp = typeof performance !== "undefined" && performance.now ? function () { return performance.now(); } : Date.now ? Date.now : function () { return +(new Date()); }; })(ts || (ts = {})); -var ts; (function (ts) { var performance; (function (performance) { @@ -150,6 +149,7 @@ var ts; contains: contains, remove: remove, forEachValue: forEachValueInMap, + getKeys: getKeys, clear: clear }; function forEachValueInMap(f) { @@ -157,6 +157,13 @@ var ts; f(key, files[key]); } } + function getKeys() { + var keys = []; + for (var key in files) { + keys.push(key); + } + return keys; + } function get(path) { return files[toKey(path)]; } @@ -533,16 +540,22 @@ var ts; : undefined; } ts.lastOrUndefined = lastOrUndefined; - function binarySearch(array, value) { + function binarySearch(array, value, comparer) { + if (!array || array.length === 0) { + return -1; + } var low = 0; var high = array.length - 1; + comparer = comparer !== undefined + ? comparer + : function (v1, v2) { return (v1 < v2 ? -1 : (v1 > v2 ? 1 : 0)); }; while (low <= high) { var middle = low + ((high - low) >> 1); var midValue = array[middle]; - if (midValue === value) { + if (comparer(midValue, value) === 0) { return middle; } - else if (midValue > value) { + else if (comparer(midValue, value) > 0) { high = middle - 1; } else { @@ -776,6 +789,56 @@ var ts; }; } ts.memoize = memoize; + function chain(a, b, c, d, e) { + if (e) { + var args_2 = []; + for (var i = 0; i < arguments.length; i++) { + args_2[i] = arguments[i]; + } + return function (t) { return compose.apply(void 0, map(args_2, function (f) { return f(t); })); }; + } + else if (d) { + return function (t) { return compose(a(t), b(t), c(t), d(t)); }; + } + else if (c) { + return function (t) { return compose(a(t), b(t), c(t)); }; + } + else if (b) { + return function (t) { return compose(a(t), b(t)); }; + } + else if (a) { + return function (t) { return compose(a(t)); }; + } + else { + return function (t) { return function (u) { return u; }; }; + } + } + ts.chain = chain; + function compose(a, b, c, d, e) { + if (e) { + var args_3 = []; + for (var i = 0; i < arguments.length; i++) { + args_3[i] = arguments[i]; + } + return function (t) { return reduceLeft(args_3, function (u, f) { return f(u); }, t); }; + } + else if (d) { + return function (t) { return d(c(b(a(t)))); }; + } + else if (c) { + return function (t) { return c(b(a(t))); }; + } + else if (b) { + return function (t) { return b(a(t)); }; + } + else if (a) { + return function (t) { return a(t); }; + } + else { + return function (t) { return t; }; + } + } + ts.compose = compose; function formatStringFromArgs(text, args, baseIndex) { baseIndex = baseIndex || 0; return text.replace(/{(\d+)}/g, function (match, index) { return args[+index + baseIndex]; }); @@ -1012,10 +1075,45 @@ var ts; return path && !isRootedDiskPath(path) && path.indexOf("://") !== -1; } ts.isUrl = isUrl; + function isExternalModuleNameRelative(moduleName) { + return /^\.\.?($|[\\/])/.test(moduleName); + } + ts.isExternalModuleNameRelative = isExternalModuleNameRelative; + function getEmitScriptTarget(compilerOptions) { + return compilerOptions.target || 0; + } + ts.getEmitScriptTarget = getEmitScriptTarget; + function getEmitModuleKind(compilerOptions) { + return typeof compilerOptions.module === "number" ? + compilerOptions.module : + getEmitScriptTarget(compilerOptions) === 2 ? ts.ModuleKind.ES6 : ts.ModuleKind.CommonJS; + } + ts.getEmitModuleKind = getEmitModuleKind; + function hasZeroOrOneAsteriskCharacter(str) { + var seenAsterisk = false; + for (var i = 0; i < str.length; i++) { + if (str.charCodeAt(i) === 42) { + if (!seenAsterisk) { + seenAsterisk = true; + } + else { + return false; + } + } + } + return true; + } + ts.hasZeroOrOneAsteriskCharacter = hasZeroOrOneAsteriskCharacter; function isRootedDiskPath(path) { return getRootLength(path) !== 0; } ts.isRootedDiskPath = isRootedDiskPath; + function convertToRelativePath(absoluteOrRelativePath, basePath, getCanonicalFileName) { + return !isRootedDiskPath(absoluteOrRelativePath) + ? absoluteOrRelativePath + : getRelativePathToDirectoryOrUrl(basePath, absoluteOrRelativePath, basePath, getCanonicalFileName, false); + } + ts.convertToRelativePath = convertToRelativePath; function normalizedPathComponents(path, rootLength) { var normalizedParts = getNormalizedParts(path, rootLength); return [path.substr(0, rootLength)].concat(normalizedParts); @@ -1389,6 +1487,14 @@ var ts; return options && options.allowJs ? allSupportedExtensions : ts.supportedTypeScriptExtensions; } ts.getSupportedExtensions = getSupportedExtensions; + function hasJavaScriptFileExtension(fileName) { + return forEach(ts.supportedJavascriptExtensions, function (extension) { return fileExtensionIs(fileName, extension); }); + } + ts.hasJavaScriptFileExtension = hasJavaScriptFileExtension; + function hasTypeScriptFileExtension(fileName) { + return forEach(ts.supportedTypeScriptExtensions, function (extension) { return fileExtensionIs(fileName, extension); }); + } + ts.hasTypeScriptFileExtension = hasTypeScriptFileExtension; function isSupportedSourceFileName(fileName, compilerOptions) { if (!fileName) { return false; @@ -1480,7 +1586,6 @@ var ts; this.transformFlags = 0; this.parent = undefined; this.original = undefined; - this.transformId = 0; } ts.objectAllocator = { getNodeConstructor: function () { return Node; }, @@ -1493,9 +1598,9 @@ var ts; }; var Debug; (function (Debug) { - var currentAssertionLevel; + Debug.currentAssertionLevel = 0; function shouldAssert(level) { - return getCurrentAssertionLevel() >= level; + return Debug.currentAssertionLevel >= level; } Debug.shouldAssert = shouldAssert; function assert(expression, message, verboseDebugInfo) { @@ -1513,30 +1618,7 @@ var ts; Debug.assert(false, message); } Debug.fail = fail; - function getCurrentAssertionLevel() { - if (currentAssertionLevel !== undefined) { - return currentAssertionLevel; - } - if (ts.sys === undefined) { - return 0; - } - var developmentMode = /^development$/i.test(getEnvironmentVariable("NODE_ENV")); - currentAssertionLevel = developmentMode - ? 1 - : 0; - return currentAssertionLevel; - } })(Debug = ts.Debug || (ts.Debug = {})); - function getEnvironmentVariable(name, host) { - if (host && host.getEnvironmentVariable) { - return host.getEnvironmentVariable(name); - } - if (ts.sys && ts.sys.getEnvironmentVariable) { - return ts.sys.getEnvironmentVariable(name); - } - return ""; - } - ts.getEnvironmentVariable = getEnvironmentVariable; function orderedRemoveItemAt(array, index) { for (var i = index; i < array.length - 1; i++) { array[i] = array[i + 1]; @@ -1567,6 +1649,64 @@ var ts; : (function (fileName) { return fileName.toLowerCase(); }); } ts.createGetCanonicalFileName = createGetCanonicalFileName; + function matchPatternOrExact(patternStrings, candidate) { + var patterns = []; + for (var _i = 0, patternStrings_1 = patternStrings; _i < patternStrings_1.length; _i++) { + var patternString = patternStrings_1[_i]; + var pattern = tryParsePattern(patternString); + if (pattern) { + patterns.push(pattern); + } + else if (patternString === candidate) { + return patternString; + } + } + return findBestPatternMatch(patterns, function (_) { return _; }, candidate); + } + ts.matchPatternOrExact = matchPatternOrExact; + function patternText(_a) { + var prefix = _a.prefix, suffix = _a.suffix; + return prefix + "*" + suffix; + } + ts.patternText = patternText; + function matchedText(pattern, candidate) { + Debug.assert(isPatternMatch(pattern, candidate)); + return candidate.substr(pattern.prefix.length, candidate.length - pattern.suffix.length); + } + ts.matchedText = matchedText; + function findBestPatternMatch(values, getPattern, candidate) { + var matchedValue = undefined; + var longestMatchPrefixLength = -1; + for (var _i = 0, values_1 = values; _i < values_1.length; _i++) { + var v = values_1[_i]; + var pattern = getPattern(v); + if (isPatternMatch(pattern, candidate) && pattern.prefix.length > longestMatchPrefixLength) { + longestMatchPrefixLength = pattern.prefix.length; + matchedValue = v; + } + } + return matchedValue; + } + ts.findBestPatternMatch = findBestPatternMatch; + function isPatternMatch(_a, candidate) { + var prefix = _a.prefix, suffix = _a.suffix; + return candidate.length >= prefix.length + suffix.length && + startsWith(candidate, prefix) && + endsWith(candidate, suffix); + } + function tryParsePattern(pattern) { + Debug.assert(hasZeroOrOneAsteriskCharacter(pattern)); + var indexOfStar = pattern.indexOf("*"); + return indexOfStar === -1 ? undefined : { + prefix: pattern.substr(0, indexOfStar), + suffix: pattern.substr(indexOfStar + 1) + }; + } + ts.tryParsePattern = tryParsePattern; + function positionIsSynthesized(pos) { + return !(pos >= 0); + } + ts.positionIsSynthesized = positionIsSynthesized; })(ts || (ts = {})); var ts; (function (ts) { @@ -1760,8 +1900,14 @@ var ts; function isNode4OrLater() { return parseInt(process.version.charAt(1)) >= 4; } + function isFileSystemCaseSensitive() { + if (platform === "win32" || platform === "win64") { + return false; + } + return !fileExists(__filename.toUpperCase()) || !fileExists(__filename.toLowerCase()); + } var platform = _os.platform(); - var useCaseSensitiveFileNames = platform !== "win32" && platform !== "win64" && platform !== "darwin"; + var useCaseSensitiveFileNames = isFileSystemCaseSensitive(); function readFile(fileName, encoding) { if (!fileExists(fileName)) { return undefined; @@ -1886,6 +2032,9 @@ var ts; }, watchDirectory: function (directoryName, callback, recursive) { var options; + if (!directoryExists(directoryName)) { + return; + } if (isNode4OrLater() && (process.platform === "win32" || process.platform === "darwin")) { options = { persistent: true, recursive: !!recursive }; } @@ -1997,19 +2146,43 @@ var ts; realpath: realpath }; } + function recursiveCreateDirectory(directoryPath, sys) { + var basePath = ts.getDirectoryPath(directoryPath); + var shouldCreateParent = directoryPath !== basePath && !sys.directoryExists(basePath); + if (shouldCreateParent) { + recursiveCreateDirectory(basePath, sys); + } + if (shouldCreateParent || !sys.directoryExists(directoryPath)) { + sys.createDirectory(directoryPath); + } + } + var sys; if (typeof ChakraHost !== "undefined") { - return getChakraSystem(); + sys = getChakraSystem(); } else if (typeof WScript !== "undefined" && typeof ActiveXObject === "function") { - return getWScriptSystem(); + sys = getWScriptSystem(); } else if (typeof process !== "undefined" && process.nextTick && !process.browser && typeof require !== "undefined") { - return getNodeSystem(); + sys = getNodeSystem(); } - else { - return undefined; + if (sys) { + var originalWriteFile_1 = sys.writeFile; + sys.writeFile = function (path, data, writeBom) { + var directoryPath = ts.getDirectoryPath(ts.normalizeSlashes(path)); + if (directoryPath && !sys.directoryExists(directoryPath)) { + recursiveCreateDirectory(directoryPath, sys); + } + originalWriteFile_1.call(sys, path, data, writeBom); + }; } + return sys; })(); + if (ts.sys && ts.sys.getEnvironmentVariable) { + ts.Debug.currentAssertionLevel = /^development$/i.test(ts.sys.getEnvironmentVariable("NODE_ENV")) + ? 1 + : 0; + } })(ts || (ts = {})); var ts; (function (ts) { @@ -2334,7 +2507,7 @@ var ts; The_right_hand_side_of_a_for_in_statement_must_be_of_type_any_an_object_type_or_a_type_parameter: { code: 2407, category: ts.DiagnosticCategory.Error, key: "The_right_hand_side_of_a_for_in_statement_must_be_of_type_any_an_object_type_or_a_type_parameter_2407", message: "The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter." }, Setters_cannot_return_a_value: { code: 2408, category: ts.DiagnosticCategory.Error, key: "Setters_cannot_return_a_value_2408", message: "Setters cannot return a value." }, Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class: { code: 2409, category: ts.DiagnosticCategory.Error, key: "Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class_2409", message: "Return type of constructor signature must be assignable to the instance type of the class" }, - All_symbols_within_a_with_block_will_be_resolved_to_any: { code: 2410, category: ts.DiagnosticCategory.Error, key: "All_symbols_within_a_with_block_will_be_resolved_to_any_2410", message: "All symbols within a 'with' block will be resolved to 'any'." }, + The_with_statement_is_not_supported_All_symbols_in_a_with_block_will_have_type_any: { code: 2410, category: ts.DiagnosticCategory.Error, key: "The_with_statement_is_not_supported_All_symbols_in_a_with_block_will_have_type_any_2410", message: "The 'with' statement is not supported. All symbols in a 'with' block will have type 'any'." }, Property_0_of_type_1_is_not_assignable_to_string_index_type_2: { code: 2411, category: ts.DiagnosticCategory.Error, key: "Property_0_of_type_1_is_not_assignable_to_string_index_type_2_2411", message: "Property '{0}' of type '{1}' is not assignable to string index type '{2}'." }, Property_0_of_type_1_is_not_assignable_to_numeric_index_type_2: { code: 2412, category: ts.DiagnosticCategory.Error, key: "Property_0_of_type_1_is_not_assignable_to_numeric_index_type_2_2412", message: "Property '{0}' of type '{1}' is not assignable to numeric index type '{2}'." }, Numeric_index_type_0_is_not_assignable_to_string_index_type_1: { code: 2413, category: ts.DiagnosticCategory.Error, key: "Numeric_index_type_0_is_not_assignable_to_string_index_type_1_2413", message: "Numeric index type '{0}' is not assignable to string index type '{1}'." }, @@ -2495,7 +2668,7 @@ var ts; this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation: { code: 2683, category: ts.DiagnosticCategory.Error, key: "this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation_2683", message: "'this' implicitly has type 'any' because it does not have a type annotation." }, The_this_context_of_type_0_is_not_assignable_to_method_s_this_of_type_1: { code: 2684, category: ts.DiagnosticCategory.Error, key: "The_this_context_of_type_0_is_not_assignable_to_method_s_this_of_type_1_2684", message: "The 'this' context of type '{0}' is not assignable to method's 'this' of type '{1}'." }, The_this_types_of_each_signature_are_incompatible: { code: 2685, category: ts.DiagnosticCategory.Error, key: "The_this_types_of_each_signature_are_incompatible_2685", message: "The 'this' types of each signature are incompatible." }, - Identifier_0_must_be_imported_from_a_module: { code: 2686, category: ts.DiagnosticCategory.Error, key: "Identifier_0_must_be_imported_from_a_module_2686", message: "Identifier '{0}' must be imported from a module" }, + _0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead: { code: 2686, category: ts.DiagnosticCategory.Error, key: "_0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead_2686", message: "'{0}' refers to a UMD global, but the current file is a module. Consider adding an import instead." }, All_declarations_of_0_must_have_identical_modifiers: { code: 2687, category: ts.DiagnosticCategory.Error, key: "All_declarations_of_0_must_have_identical_modifiers_2687", message: "All declarations of '{0}' must have identical modifiers." }, Cannot_find_type_definition_file_for_0: { code: 2688, category: ts.DiagnosticCategory.Error, key: "Cannot_find_type_definition_file_for_0_2688", message: "Cannot find type definition file for '{0}'." }, Cannot_extend_an_interface_0_Did_you_mean_implements: { code: 2689, category: ts.DiagnosticCategory.Error, key: "Cannot_extend_an_interface_0_Did_you_mean_implements_2689", message: "Cannot extend an interface '{0}'. Did you mean 'implements'?" }, @@ -2728,6 +2901,8 @@ var ts; No_types_specified_in_package_json_but_allowJs_is_set_so_returning_main_value_of_0: { code: 6137, category: ts.DiagnosticCategory.Message, key: "No_types_specified_in_package_json_but_allowJs_is_set_so_returning_main_value_of_0_6137", message: "No types specified in 'package.json' but 'allowJs' is set, so returning 'main' value of '{0}'" }, Property_0_is_declared_but_never_used: { code: 6138, category: ts.DiagnosticCategory.Error, key: "Property_0_is_declared_but_never_used_6138", message: "Property '{0}' is declared but never used." }, Import_emit_helpers_from_tslib: { code: 6139, category: ts.DiagnosticCategory.Message, key: "Import_emit_helpers_from_tslib_6139", message: "Import emit helpers from 'tslib'." }, + Auto_discovery_for_typings_is_enabled_in_project_0_Running_extra_resolution_pass_for_module_1_using_cache_location_2: { code: 6140, category: ts.DiagnosticCategory.Error, key: "Auto_discovery_for_typings_is_enabled_in_project_0_Running_extra_resolution_pass_for_module_1_using__6140", message: "Auto discovery for typings is enabled in project '{0}'. Running extra resolution pass for module '{1}' using cache location '{2}'." }, + Parse_in_strict_mode_and_emit_use_strict_for_each_source_file: { code: 6141, category: ts.DiagnosticCategory.Message, key: "Parse_in_strict_mode_and_emit_use_strict_for_each_source_file_6141", message: "Parse in strict mode and emit \"use strict\" for each source file" }, Variable_0_implicitly_has_an_1_type: { code: 7005, category: ts.DiagnosticCategory.Error, key: "Variable_0_implicitly_has_an_1_type_7005", message: "Variable '{0}' implicitly has an '{1}' type." }, Parameter_0_implicitly_has_an_1_type: { code: 7006, category: ts.DiagnosticCategory.Error, key: "Parameter_0_implicitly_has_an_1_type_7006", message: "Parameter '{0}' implicitly has an '{1}' type." }, Member_0_implicitly_has_an_1_type: { code: 7008, category: ts.DiagnosticCategory.Error, key: "Member_0_implicitly_has_an_1_type_7008", message: "Member '{0}' implicitly has an '{1}' type." }, @@ -2752,6 +2927,7 @@ var ts; Binding_element_0_implicitly_has_an_1_type: { code: 7031, category: ts.DiagnosticCategory.Error, key: "Binding_element_0_implicitly_has_an_1_type_7031", message: "Binding element '{0}' implicitly has an '{1}' type." }, Property_0_implicitly_has_type_any_because_its_set_accessor_lacks_a_parameter_type_annotation: { code: 7032, category: ts.DiagnosticCategory.Error, key: "Property_0_implicitly_has_type_any_because_its_set_accessor_lacks_a_parameter_type_annotation_7032", message: "Property '{0}' implicitly has type 'any', because its set accessor lacks a parameter type annotation." }, Property_0_implicitly_has_type_any_because_its_get_accessor_lacks_a_return_type_annotation: { code: 7033, category: ts.DiagnosticCategory.Error, key: "Property_0_implicitly_has_type_any_because_its_get_accessor_lacks_a_return_type_annotation_7033", message: "Property '{0}' implicitly has type 'any', because its get accessor lacks a return type annotation." }, + Variable_0_implicitly_has_type_any_in_some_locations_where_its_type_cannot_be_determined: { code: 7034, category: ts.DiagnosticCategory.Error, key: "Variable_0_implicitly_has_type_any_in_some_locations_where_its_type_cannot_be_determined_7034", message: "Variable '{0}' implicitly has type 'any' in some locations where its type cannot be determined." }, You_cannot_rename_this_element: { code: 8000, category: ts.DiagnosticCategory.Error, key: "You_cannot_rename_this_element_8000", message: "You cannot rename this element." }, You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library: { code: 8001, category: ts.DiagnosticCategory.Error, key: "You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library_8001", message: "You cannot rename elements that are defined in the standard TypeScript library." }, import_can_only_be_used_in_a_ts_file: { code: 8002, category: ts.DiagnosticCategory.Error, key: "import_can_only_be_used_in_a_ts_file_8002", message: "'import ... =' can only be used in a .ts file." }, @@ -2781,7 +2957,14 @@ var ts; super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class: { code: 17009, category: ts.DiagnosticCategory.Error, key: "super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class_17009", message: "'super' must be called before accessing 'this' in the constructor of a derived class." }, Unknown_typing_option_0: { code: 17010, category: ts.DiagnosticCategory.Error, key: "Unknown_typing_option_0_17010", message: "Unknown typing option '{0}'." }, Circularity_detected_while_resolving_configuration_Colon_0: { code: 18000, category: ts.DiagnosticCategory.Error, key: "Circularity_detected_while_resolving_configuration_Colon_0_18000", message: "Circularity detected while resolving configuration: {0}" }, - The_path_in_an_extends_options_must_be_relative_or_rooted: { code: 18001, category: ts.DiagnosticCategory.Error, key: "The_path_in_an_extends_options_must_be_relative_or_rooted_18001", message: "The path in an 'extends' options must be relative or rooted." } + The_path_in_an_extends_options_must_be_relative_or_rooted: { code: 18001, category: ts.DiagnosticCategory.Error, key: "The_path_in_an_extends_options_must_be_relative_or_rooted_18001", message: "The path in an 'extends' options must be relative or rooted." }, + Add_missing_super_call: { code: 90001, category: ts.DiagnosticCategory.Message, key: "Add_missing_super_call_90001", message: "Add missing 'super()' call." }, + Make_super_call_the_first_statement_in_the_constructor: { code: 90002, category: ts.DiagnosticCategory.Message, key: "Make_super_call_the_first_statement_in_the_constructor_90002", message: "Make 'super()' call the first statement in the constructor." }, + Change_extends_to_implements: { code: 90003, category: ts.DiagnosticCategory.Message, key: "Change_extends_to_implements_90003", message: "Change 'extends' to 'implements'" }, + Remove_unused_identifiers: { code: 90004, category: ts.DiagnosticCategory.Message, key: "Remove_unused_identifiers_90004", message: "Remove unused identifiers" }, + Implement_interface_on_reference: { code: 90005, category: ts.DiagnosticCategory.Message, key: "Implement_interface_on_reference_90005", message: "Implement interface on reference" }, + Implement_interface_on_class: { code: 90006, category: ts.DiagnosticCategory.Message, key: "Implement_interface_on_class_90006", message: "Implement interface on class" }, + Implement_inherited_abstract_class: { code: 90007, category: ts.DiagnosticCategory.Message, key: "Implement_inherited_abstract_class_90007", message: "Implement inherited abstract class" } }; })(ts || (ts = {})); var ts; @@ -3691,7 +3874,7 @@ var ts; return token = 69; } function scanBinaryOrOctalDigits(base) { - ts.Debug.assert(base !== 2 || base !== 8, "Expected either base 2 or base 8"); + ts.Debug.assert(base === 2 || base === 8, "Expected either base 2 or base 8"); var value = 0; var numberOfDigits = 0; while (true) { @@ -4334,11 +4517,13 @@ var ts; })(ts || (ts = {})); var ts; (function (ts) { + ts.compileOnSaveCommandLineOption = { name: "compileOnSave", type: "boolean" }; ts.optionDeclarations = [ { name: "charset", type: "string" }, + ts.compileOnSaveCommandLineOption, { name: "declaration", shortName: "d", @@ -4761,6 +4946,11 @@ var ts; name: "importHelpers", type: "boolean", description: ts.Diagnostics.Import_emit_helpers_from_tslib + }, + { + name: "alwaysStrict", + type: "boolean", + description: ts.Diagnostics.Parse_in_strict_mode_and_emit_use_strict_for_each_source_file } ]; ts.typingOptionDeclarations = [ @@ -4962,10 +5152,11 @@ var ts; return parseConfigFileTextToJson(fileName, text); } ts.readConfigFile = readConfigFile; - function parseConfigFileTextToJson(fileName, jsonText) { + function parseConfigFileTextToJson(fileName, jsonText, stripComments) { + if (stripComments === void 0) { stripComments = true; } try { - var jsonTextWithoutComments = removeComments(jsonText); - return { config: /\S/.test(jsonTextWithoutComments) ? JSON.parse(jsonTextWithoutComments) : {} }; + var jsonTextToParse = stripComments ? removeComments(jsonText) : jsonText; + return { config: /\S/.test(jsonTextToParse) ? JSON.parse(jsonTextToParse) : {} }; } catch (e) { return { error: ts.createCompilerDiagnostic(ts.Diagnostics.Failed_to_parse_file_0_Colon_1, fileName, e.message) }; @@ -5099,13 +5290,15 @@ var ts; options = ts.extend(existingOptions, options); options.configFilePath = configFileName; var _c = getFileNames(errors), fileNames = _c.fileNames, wildcardDirectories = _c.wildcardDirectories; + var compileOnSave = convertCompileOnSaveOptionFromJson(json, basePath, errors); return { options: options, fileNames: fileNames, typingOptions: typingOptions, raw: json, errors: errors, - wildcardDirectories: wildcardDirectories + wildcardDirectories: wildcardDirectories, + compileOnSave: compileOnSave }; function tryExtendsName(extendedConfig) { if (!(ts.isRootedDiskPath(extendedConfig) || ts.startsWith(ts.normalizeSlashes(extendedConfig), "./") || ts.startsWith(ts.normalizeSlashes(extendedConfig), "../"))) { @@ -5183,6 +5376,17 @@ var ts; var _b; } ts.parseJsonConfigFileContent = parseJsonConfigFileContent; + function convertCompileOnSaveOptionFromJson(jsonOption, basePath, errors) { + if (!ts.hasProperty(jsonOption, ts.compileOnSaveCommandLineOption.name)) { + return false; + } + var result = convertJsonOption(ts.compileOnSaveCommandLineOption, jsonOption["compileOnSave"], basePath, errors); + if (typeof result === "boolean" && result) { + return result; + } + return false; + } + ts.convertCompileOnSaveOptionFromJson = convertCompileOnSaveOptionFromJson; function convertCompilerOptionsFromJson(jsonOptions, basePath, configFileName) { var errors = []; var options = convertCompilerOptionsFromJsonWorker(jsonOptions, basePath, errors, configFileName); @@ -5196,7 +5400,9 @@ var ts; } ts.convertTypingOptionsFromJson = convertTypingOptionsFromJson; function convertCompilerOptionsFromJsonWorker(jsonOptions, basePath, errors, configFileName) { - var options = ts.getBaseFileName(configFileName) === "jsconfig.json" ? { allowJs: true, maxNodeModuleJsDepth: 2 } : {}; + var options = ts.getBaseFileName(configFileName) === "jsconfig.json" + ? { allowJs: true, maxNodeModuleJsDepth: 2, allowSyntheticDefaultImports: true, skipLibCheck: true } + : {}; convertOptionsFromJson(ts.optionDeclarations, jsonOptions, basePath, options, ts.Diagnostics.Unknown_compiler_option_0, errors); return options; } @@ -5396,310 +5602,1241 @@ var ts; })(ts || (ts = {})); var ts; (function (ts) { - ts.externalHelpersModuleNameText = "tslib"; - function getDeclarationOfKind(symbol, kind) { - var declarations = symbol.declarations; - if (declarations) { - for (var _i = 0, declarations_1 = declarations; _i < declarations_1.length; _i++) { - var declaration = declarations_1[_i]; - if (declaration.kind === kind) { - return declaration; + var JsTyping; + (function (JsTyping) { + ; + ; + var safeList; + var EmptySafeList = ts.createMap(); + function discoverTypings(host, fileNames, projectRootPath, safeListPath, packageNameToTypingLocation, typingOptions, compilerOptions) { + var inferredTypings = ts.createMap(); + if (!typingOptions || !typingOptions.enableAutoDiscovery) { + return { cachedTypingPaths: [], newTypingNames: [], filesToWatch: [] }; + } + fileNames = ts.filter(ts.map(fileNames, ts.normalizePath), function (f) { + var kind = ts.ensureScriptKind(f, ts.getScriptKindFromFileName(f)); + return kind === 1 || kind === 2; + }); + if (!safeList) { + var result = ts.readConfigFile(safeListPath, function (path) { return host.readFile(path); }); + safeList = result.config ? ts.createMap(result.config) : EmptySafeList; + } + var filesToWatch = []; + var searchDirs = []; + var exclude = []; + mergeTypings(typingOptions.include); + exclude = typingOptions.exclude || []; + var possibleSearchDirs = ts.map(fileNames, ts.getDirectoryPath); + if (projectRootPath !== undefined) { + possibleSearchDirs.push(projectRootPath); + } + searchDirs = ts.deduplicate(possibleSearchDirs); + for (var _i = 0, searchDirs_1 = searchDirs; _i < searchDirs_1.length; _i++) { + var searchDir = searchDirs_1[_i]; + var packageJsonPath = ts.combinePaths(searchDir, "package.json"); + getTypingNamesFromJson(packageJsonPath, filesToWatch); + var bowerJsonPath = ts.combinePaths(searchDir, "bower.json"); + getTypingNamesFromJson(bowerJsonPath, filesToWatch); + var nodeModulesPath = ts.combinePaths(searchDir, "node_modules"); + getTypingNamesFromNodeModuleFolder(nodeModulesPath); + } + getTypingNamesFromSourceFileNames(fileNames); + for (var name_7 in packageNameToTypingLocation) { + if (name_7 in inferredTypings && !inferredTypings[name_7]) { + inferredTypings[name_7] = packageNameToTypingLocation[name_7]; + } + } + for (var _a = 0, exclude_1 = exclude; _a < exclude_1.length; _a++) { + var excludeTypingName = exclude_1[_a]; + delete inferredTypings[excludeTypingName]; + } + var newTypingNames = []; + var cachedTypingPaths = []; + for (var typing in inferredTypings) { + if (inferredTypings[typing] !== undefined) { + cachedTypingPaths.push(inferredTypings[typing]); + } + else { + newTypingNames.push(typing); + } + } + return { cachedTypingPaths: cachedTypingPaths, newTypingNames: newTypingNames, filesToWatch: filesToWatch }; + function mergeTypings(typingNames) { + if (!typingNames) { + return; + } + for (var _i = 0, typingNames_1 = typingNames; _i < typingNames_1.length; _i++) { + var typing = typingNames_1[_i]; + if (!(typing in inferredTypings)) { + inferredTypings[typing] = undefined; + } + } + } + function getTypingNamesFromJson(jsonPath, filesToWatch) { + var result = ts.readConfigFile(jsonPath, function (path) { return host.readFile(path); }); + if (result.config) { + var jsonConfig = result.config; + filesToWatch.push(jsonPath); + if (jsonConfig.dependencies) { + mergeTypings(ts.getOwnKeys(jsonConfig.dependencies)); + } + if (jsonConfig.devDependencies) { + mergeTypings(ts.getOwnKeys(jsonConfig.devDependencies)); + } + if (jsonConfig.optionalDependencies) { + mergeTypings(ts.getOwnKeys(jsonConfig.optionalDependencies)); + } + if (jsonConfig.peerDependencies) { + mergeTypings(ts.getOwnKeys(jsonConfig.peerDependencies)); + } + } + } + function getTypingNamesFromSourceFileNames(fileNames) { + var jsFileNames = ts.filter(fileNames, ts.hasJavaScriptFileExtension); + var inferredTypingNames = ts.map(jsFileNames, function (f) { return ts.removeFileExtension(ts.getBaseFileName(f.toLowerCase())); }); + var cleanedTypingNames = ts.map(inferredTypingNames, function (f) { return f.replace(/((?:\.|-)min(?=\.|$))|((?:-|\.)\d+)/g, ""); }); + if (safeList !== EmptySafeList) { + mergeTypings(ts.filter(cleanedTypingNames, function (f) { return f in safeList; })); + } + var hasJsxFile = ts.forEach(fileNames, function (f) { return ts.ensureScriptKind(f, ts.getScriptKindFromFileName(f)) === 2; }); + if (hasJsxFile) { + mergeTypings(["react"]); + } + } + function getTypingNamesFromNodeModuleFolder(nodeModulesPath) { + if (!host.directoryExists(nodeModulesPath)) { + return; + } + var typingNames = []; + var fileNames = host.readDirectory(nodeModulesPath, [".json"], undefined, undefined, 2); + for (var _i = 0, fileNames_2 = fileNames; _i < fileNames_2.length; _i++) { + var fileName = fileNames_2[_i]; + var normalizedFileName = ts.normalizePath(fileName); + if (ts.getBaseFileName(normalizedFileName) !== "package.json") { + continue; + } + var result = ts.readConfigFile(normalizedFileName, function (path) { return host.readFile(path); }); + if (!result.config) { + continue; + } + var packageJson = result.config; + if (packageJson._requiredBy && + ts.filter(packageJson._requiredBy, function (r) { return r[0] === "#" || r === "/"; }).length === 0) { + continue; + } + if (!packageJson.name) { + continue; + } + if (packageJson.typings) { + var absolutePath = ts.getNormalizedAbsolutePath(packageJson.typings, ts.getDirectoryPath(normalizedFileName)); + inferredTypings[packageJson.name] = absolutePath; + } + else { + typingNames.push(packageJson.name); + } } + mergeTypings(typingNames); } } - return undefined; - } - ts.getDeclarationOfKind = getDeclarationOfKind; - var stringWriters = []; - function getSingleLineStringWriter() { - if (stringWriters.length === 0) { - var str_1 = ""; - var writeText = function (text) { return str_1 += text; }; + JsTyping.discoverTypings = discoverTypings; + })(JsTyping = ts.JsTyping || (ts.JsTyping = {})); +})(ts || (ts = {})); +var ts; +(function (ts) { + var server; + (function (server) { + (function (LogLevel) { + LogLevel[LogLevel["terse"] = 0] = "terse"; + LogLevel[LogLevel["normal"] = 1] = "normal"; + LogLevel[LogLevel["requestTime"] = 2] = "requestTime"; + LogLevel[LogLevel["verbose"] = 3] = "verbose"; + })(server.LogLevel || (server.LogLevel = {})); + var LogLevel = server.LogLevel; + server.emptyArray = []; + var Msg; + (function (Msg) { + Msg.Err = "Err"; + Msg.Info = "Info"; + Msg.Perf = "Perf"; + })(Msg = server.Msg || (server.Msg = {})); + function getProjectRootPath(project) { + switch (project.projectKind) { + case server.ProjectKind.Configured: + return ts.getDirectoryPath(project.getProjectName()); + case server.ProjectKind.Inferred: + return ""; + case server.ProjectKind.External: + var projectName = ts.normalizeSlashes(project.getProjectName()); + return project.projectService.host.fileExists(projectName) ? ts.getDirectoryPath(projectName) : projectName; + } + } + function createInstallTypingsRequest(project, typingOptions, cachePath) { return { - string: function () { return str_1; }, - writeKeyword: writeText, - writeOperator: writeText, - writePunctuation: writeText, - writeSpace: writeText, - writeStringLiteral: writeText, - writeParameter: writeText, - writeSymbol: writeText, - writeLine: function () { return str_1 += " "; }, - increaseIndent: function () { }, - decreaseIndent: function () { }, - clear: function () { return str_1 = ""; }, - trackSymbol: function () { }, - reportInaccessibleThisError: function () { } + projectName: project.getProjectName(), + fileNames: project.getFileNames(), + compilerOptions: project.getCompilerOptions(), + typingOptions: typingOptions, + projectRootPath: getProjectRootPath(project), + cachePath: cachePath, + kind: "discover" }; } - return stringWriters.pop(); - } - ts.getSingleLineStringWriter = getSingleLineStringWriter; - function releaseStringWriter(writer) { - writer.clear(); - stringWriters.push(writer); - } - ts.releaseStringWriter = releaseStringWriter; - function getFullWidth(node) { - return node.end - node.pos; - } - ts.getFullWidth = getFullWidth; - function arrayIsEqualTo(array1, array2, equaler) { - if (!array1 || !array2) { - return array1 === array2; + server.createInstallTypingsRequest = createInstallTypingsRequest; + var Errors; + (function (Errors) { + function ThrowNoProject() { + throw new Error("No Project."); + } + Errors.ThrowNoProject = ThrowNoProject; + function ThrowProjectLanguageServiceDisabled() { + throw new Error("The project's language service is disabled."); + } + Errors.ThrowProjectLanguageServiceDisabled = ThrowProjectLanguageServiceDisabled; + function ThrowProjectDoesNotContainDocument(fileName, project) { + throw new Error("Project '" + project.getProjectName() + "' does not contain document '" + fileName + "'"); + } + Errors.ThrowProjectDoesNotContainDocument = ThrowProjectDoesNotContainDocument; + })(Errors = server.Errors || (server.Errors = {})); + function getDefaultFormatCodeSettings(host) { + return { + indentSize: 4, + tabSize: 4, + newLineCharacter: host.newLine || "\n", + convertTabsToSpaces: true, + indentStyle: ts.IndentStyle.Smart, + insertSpaceAfterCommaDelimiter: true, + insertSpaceAfterSemicolonInForStatements: true, + insertSpaceBeforeAndAfterBinaryOperators: true, + insertSpaceAfterKeywordsInControlFlowStatements: true, + insertSpaceAfterFunctionKeywordForAnonymousFunctions: false, + insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false, + insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false, + insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false, + insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: false, + placeOpenBraceOnNewLineForFunctions: false, + placeOpenBraceOnNewLineForControlBlocks: false + }; } - if (array1.length !== array2.length) { - return false; + server.getDefaultFormatCodeSettings = getDefaultFormatCodeSettings; + function mergeMaps(target, source) { + for (var key in source) { + if (ts.hasProperty(source, key)) { + target[key] = source[key]; + } + } } - for (var i = 0; i < array1.length; i++) { - var equals = equaler ? equaler(array1[i], array2[i]) : array1[i] === array2[i]; - if (!equals) { - return false; + server.mergeMaps = mergeMaps; + function removeItemFromSet(items, itemToRemove) { + if (items.length === 0) { + return; + } + var index = items.indexOf(itemToRemove); + if (index < 0) { + return; + } + if (index === items.length - 1) { + items.pop(); + } + else { + items[index] = items.pop(); } } - return true; - } - ts.arrayIsEqualTo = arrayIsEqualTo; - function hasResolvedModule(sourceFile, moduleNameText) { - return !!(sourceFile && sourceFile.resolvedModules && sourceFile.resolvedModules[moduleNameText]); - } - ts.hasResolvedModule = hasResolvedModule; - function getResolvedModule(sourceFile, moduleNameText) { - return hasResolvedModule(sourceFile, moduleNameText) ? sourceFile.resolvedModules[moduleNameText] : undefined; - } - ts.getResolvedModule = getResolvedModule; - function setResolvedModule(sourceFile, moduleNameText, resolvedModule) { - if (!sourceFile.resolvedModules) { - sourceFile.resolvedModules = ts.createMap(); + server.removeItemFromSet = removeItemFromSet; + function toNormalizedPath(fileName) { + return ts.normalizePath(fileName); } - sourceFile.resolvedModules[moduleNameText] = resolvedModule; - } - ts.setResolvedModule = setResolvedModule; - function setResolvedTypeReferenceDirective(sourceFile, typeReferenceDirectiveName, resolvedTypeReferenceDirective) { - if (!sourceFile.resolvedTypeReferenceDirectiveNames) { - sourceFile.resolvedTypeReferenceDirectiveNames = ts.createMap(); + server.toNormalizedPath = toNormalizedPath; + function normalizedPathToPath(normalizedPath, currentDirectory, getCanonicalFileName) { + var f = ts.isRootedDiskPath(normalizedPath) ? normalizedPath : ts.getNormalizedAbsolutePath(normalizedPath, currentDirectory); + return getCanonicalFileName(f); } - sourceFile.resolvedTypeReferenceDirectiveNames[typeReferenceDirectiveName] = resolvedTypeReferenceDirective; + server.normalizedPathToPath = normalizedPathToPath; + function asNormalizedPath(fileName) { + return fileName; + } + server.asNormalizedPath = asNormalizedPath; + function createNormalizedPathMap() { + var map = Object.create(null); + return { + get: function (path) { + return map[path]; + }, + set: function (path, value) { + map[path] = value; + }, + contains: function (path) { + return ts.hasProperty(map, path); + }, + remove: function (path) { + delete map[path]; + } + }; + } + server.createNormalizedPathMap = createNormalizedPathMap; + function throwLanguageServiceIsDisabledError() { + throw new Error("LanguageService is disabled"); + } + server.nullLanguageService = { + cleanupSemanticCache: throwLanguageServiceIsDisabledError, + getSyntacticDiagnostics: throwLanguageServiceIsDisabledError, + getSemanticDiagnostics: throwLanguageServiceIsDisabledError, + getCompilerOptionsDiagnostics: throwLanguageServiceIsDisabledError, + getSyntacticClassifications: throwLanguageServiceIsDisabledError, + getEncodedSyntacticClassifications: throwLanguageServiceIsDisabledError, + getSemanticClassifications: throwLanguageServiceIsDisabledError, + getEncodedSemanticClassifications: throwLanguageServiceIsDisabledError, + getCompletionsAtPosition: throwLanguageServiceIsDisabledError, + findReferences: throwLanguageServiceIsDisabledError, + getCompletionEntryDetails: throwLanguageServiceIsDisabledError, + getQuickInfoAtPosition: throwLanguageServiceIsDisabledError, + findRenameLocations: throwLanguageServiceIsDisabledError, + getNameOrDottedNameSpan: throwLanguageServiceIsDisabledError, + getBreakpointStatementAtPosition: throwLanguageServiceIsDisabledError, + getBraceMatchingAtPosition: throwLanguageServiceIsDisabledError, + getSignatureHelpItems: throwLanguageServiceIsDisabledError, + getDefinitionAtPosition: throwLanguageServiceIsDisabledError, + getRenameInfo: throwLanguageServiceIsDisabledError, + getTypeDefinitionAtPosition: throwLanguageServiceIsDisabledError, + getReferencesAtPosition: throwLanguageServiceIsDisabledError, + getDocumentHighlights: throwLanguageServiceIsDisabledError, + getOccurrencesAtPosition: throwLanguageServiceIsDisabledError, + getNavigateToItems: throwLanguageServiceIsDisabledError, + getNavigationBarItems: throwLanguageServiceIsDisabledError, + getNavigationTree: throwLanguageServiceIsDisabledError, + getOutliningSpans: throwLanguageServiceIsDisabledError, + getTodoComments: throwLanguageServiceIsDisabledError, + getIndentationAtPosition: throwLanguageServiceIsDisabledError, + getFormattingEditsForRange: throwLanguageServiceIsDisabledError, + getFormattingEditsForDocument: throwLanguageServiceIsDisabledError, + getFormattingEditsAfterKeystroke: throwLanguageServiceIsDisabledError, + getDocCommentTemplateAtPosition: throwLanguageServiceIsDisabledError, + isValidBraceCompletionAtPosition: throwLanguageServiceIsDisabledError, + getEmitOutput: throwLanguageServiceIsDisabledError, + getProgram: throwLanguageServiceIsDisabledError, + getNonBoundSourceFile: throwLanguageServiceIsDisabledError, + dispose: throwLanguageServiceIsDisabledError, + getCompletionEntrySymbol: throwLanguageServiceIsDisabledError, + getImplementationAtPosition: throwLanguageServiceIsDisabledError, + getSourceFile: throwLanguageServiceIsDisabledError, + getCodeFixesAtPosition: throwLanguageServiceIsDisabledError + }; + server.nullLanguageServiceHost = { + setCompilationSettings: function () { return undefined; }, + notifyFileRemoved: function () { return undefined; } + }; + function isInferredProjectName(name) { + return /dev\/null\/inferredProject\d+\*/.test(name); + } + server.isInferredProjectName = isInferredProjectName; + function makeInferredProjectName(counter) { + return "/dev/null/inferredProject" + counter + "*"; + } + server.makeInferredProjectName = makeInferredProjectName; + var ThrottledOperations = (function () { + function ThrottledOperations(host) { + this.host = host; + this.pendingTimeouts = ts.createMap(); + } + ThrottledOperations.prototype.schedule = function (operationId, delay, cb) { + if (ts.hasProperty(this.pendingTimeouts, operationId)) { + this.host.clearTimeout(this.pendingTimeouts[operationId]); + } + this.pendingTimeouts[operationId] = this.host.setTimeout(ThrottledOperations.run, delay, this, operationId, cb); + }; + ThrottledOperations.run = function (self, operationId, cb) { + delete self.pendingTimeouts[operationId]; + cb(); + }; + return ThrottledOperations; + }()); + server.ThrottledOperations = ThrottledOperations; + var GcTimer = (function () { + function GcTimer(host, delay, logger) { + this.host = host; + this.delay = delay; + this.logger = logger; + } + GcTimer.prototype.scheduleCollect = function () { + if (!this.host.gc || this.timerId != undefined) { + return; + } + this.timerId = this.host.setTimeout(GcTimer.run, this.delay, this); + }; + GcTimer.run = function (self) { + self.timerId = undefined; + var log = self.logger.hasLevel(LogLevel.requestTime); + var before = log && self.host.getMemoryUsage(); + self.host.gc(); + if (log) { + var after = self.host.getMemoryUsage(); + self.logger.perftrc("GC::before " + before + ", after " + after); + } + }; + return GcTimer; + }()); + server.GcTimer = GcTimer; + })(server = ts.server || (ts.server = {})); +})(ts || (ts = {})); +var ts; +(function (ts) { + function trace(host, message) { + host.trace(ts.formatMessage.apply(undefined, arguments)); } - ts.setResolvedTypeReferenceDirective = setResolvedTypeReferenceDirective; - function moduleResolutionIsEqualTo(oldResolution, newResolution) { - return oldResolution.resolvedFileName === newResolution.resolvedFileName && oldResolution.isExternalLibraryImport === newResolution.isExternalLibraryImport; + ts.trace = trace; + function isTraceEnabled(compilerOptions, host) { + return compilerOptions.traceResolution && host.trace !== undefined; } - ts.moduleResolutionIsEqualTo = moduleResolutionIsEqualTo; - function typeDirectiveIsEqualTo(oldResolution, newResolution) { - return oldResolution.resolvedFileName === newResolution.resolvedFileName && oldResolution.primary === newResolution.primary; + ts.isTraceEnabled = isTraceEnabled; + function createResolvedModule(resolvedFileName, isExternalLibraryImport, failedLookupLocations) { + return { resolvedModule: resolvedFileName ? { resolvedFileName: resolvedFileName, isExternalLibraryImport: isExternalLibraryImport } : undefined, failedLookupLocations: failedLookupLocations }; } - ts.typeDirectiveIsEqualTo = typeDirectiveIsEqualTo; - function hasChangesInResolutions(names, newResolutions, oldResolutions, comparer) { - if (names.length !== newResolutions.length) { - return false; - } - for (var i = 0; i < names.length; i++) { - var newResolution = newResolutions[i]; - var oldResolution = oldResolutions && oldResolutions[names[i]]; - var changed = oldResolution - ? !newResolution || !comparer(oldResolution, newResolution) - : newResolution; - if (changed) { - return true; + ts.createResolvedModule = createResolvedModule; + function moduleHasNonRelativeName(moduleName) { + return !(ts.isRootedDiskPath(moduleName) || ts.isExternalModuleNameRelative(moduleName)); + } + function tryReadTypesSection(packageJsonPath, baseDirectory, state) { + var jsonContent = readJson(packageJsonPath, state.host); + function tryReadFromField(fieldName) { + if (ts.hasProperty(jsonContent, fieldName)) { + var typesFile = jsonContent[fieldName]; + if (typeof typesFile === "string") { + var typesFilePath_1 = ts.normalizePath(ts.combinePaths(baseDirectory, typesFile)); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.package_json_has_0_field_1_that_references_2, fieldName, typesFile, typesFilePath_1); + } + return typesFilePath_1; + } + else { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Expected_type_of_0_field_in_package_json_to_be_string_got_1, fieldName, typeof typesFile); + } + } } } - return false; - } - ts.hasChangesInResolutions = hasChangesInResolutions; - function containsParseError(node) { - aggregateChildData(node); - return (node.flags & 2097152) !== 0; - } - ts.containsParseError = containsParseError; - function aggregateChildData(node) { - if (!(node.flags & 4194304)) { - var thisNodeOrAnySubNodesHasError = ((node.flags & 524288) !== 0) || - ts.forEachChild(node, containsParseError); - if (thisNodeOrAnySubNodesHasError) { - node.flags |= 2097152; + var typesFilePath = tryReadFromField("typings") || tryReadFromField("types"); + if (typesFilePath) { + return typesFilePath; + } + if (state.compilerOptions.allowJs && jsonContent.main && typeof jsonContent.main === "string") { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.No_types_specified_in_package_json_but_allowJs_is_set_so_returning_main_value_of_0, jsonContent.main); } - node.flags |= 4194304; + var mainFilePath = ts.normalizePath(ts.combinePaths(baseDirectory, jsonContent.main)); + return mainFilePath; } + return undefined; } - function getSourceFileOfNode(node) { - while (node && node.kind !== 256) { - node = node.parent; + function readJson(path, host) { + try { + var jsonText = host.readFile(path); + return jsonText ? JSON.parse(jsonText) : {}; + } + catch (e) { + return {}; } - return node; } - ts.getSourceFileOfNode = getSourceFileOfNode; - function isStatementWithLocals(node) { - switch (node.kind) { - case 199: - case 227: - case 206: - case 207: - case 208: - return true; + var typeReferenceExtensions = [".d.ts"]; + function getEffectiveTypeRoots(options, host) { + if (options.typeRoots) { + return options.typeRoots; } - return false; - } - ts.isStatementWithLocals = isStatementWithLocals; - function getStartPositionOfLine(line, sourceFile) { - ts.Debug.assert(line >= 0); - return ts.getLineStarts(sourceFile)[line]; - } - ts.getStartPositionOfLine = getStartPositionOfLine; - function nodePosToString(node) { - var file = getSourceFileOfNode(node); - var loc = ts.getLineAndCharacterOfPosition(file, node.pos); - return file.fileName + "(" + (loc.line + 1) + "," + (loc.character + 1) + ")"; - } - ts.nodePosToString = nodePosToString; - function getStartPosOfNode(node) { - return node.pos; + var currentDirectory; + if (options.configFilePath) { + currentDirectory = ts.getDirectoryPath(options.configFilePath); + } + else if (host.getCurrentDirectory) { + currentDirectory = host.getCurrentDirectory(); + } + return currentDirectory !== undefined && getDefaultTypeRoots(currentDirectory, host); } - ts.getStartPosOfNode = getStartPosOfNode; - function isDefined(value) { - return value !== undefined; + ts.getEffectiveTypeRoots = getEffectiveTypeRoots; + function getDefaultTypeRoots(currentDirectory, host) { + if (!host.directoryExists) { + return [ts.combinePaths(currentDirectory, nodeModulesAtTypes)]; + } + var typeRoots; + while (true) { + var atTypes = ts.combinePaths(currentDirectory, nodeModulesAtTypes); + if (host.directoryExists(atTypes)) { + (typeRoots || (typeRoots = [])).push(atTypes); + } + var parent_1 = ts.getDirectoryPath(currentDirectory); + if (parent_1 === currentDirectory) { + break; + } + currentDirectory = parent_1; + } + return typeRoots; } - ts.isDefined = isDefined; - function getEndLinePosition(line, sourceFile) { - ts.Debug.assert(line >= 0); - var lineStarts = ts.getLineStarts(sourceFile); - var lineIndex = line; - var sourceText = sourceFile.text; - if (lineIndex + 1 === lineStarts.length) { - return sourceText.length - 1; + var nodeModulesAtTypes = ts.combinePaths("node_modules", "@types"); + function resolveTypeReferenceDirective(typeReferenceDirectiveName, containingFile, options, host) { + var traceEnabled = isTraceEnabled(options, host); + var moduleResolutionState = { + compilerOptions: options, + host: host, + skipTsx: true, + traceEnabled: traceEnabled + }; + var typeRoots = getEffectiveTypeRoots(options, host); + if (traceEnabled) { + if (containingFile === undefined) { + if (typeRoots === undefined) { + trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_not_set_root_directory_not_set, typeReferenceDirectiveName); + } + else { + trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_not_set_root_directory_1, typeReferenceDirectiveName, typeRoots); + } + } + else { + if (typeRoots === undefined) { + trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_1_root_directory_not_set, typeReferenceDirectiveName, containingFile); + } + else { + trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_1_root_directory_2, typeReferenceDirectiveName, containingFile, typeRoots); + } + } + } + var failedLookupLocations = []; + if (typeRoots && typeRoots.length) { + if (traceEnabled) { + trace(host, ts.Diagnostics.Resolving_with_primary_search_path_0, typeRoots.join(", ")); + } + var primarySearchPaths = typeRoots; + for (var _i = 0, primarySearchPaths_1 = primarySearchPaths; _i < primarySearchPaths_1.length; _i++) { + var typeRoot = primarySearchPaths_1[_i]; + var candidate = ts.combinePaths(typeRoot, typeReferenceDirectiveName); + var candidateDirectory = ts.getDirectoryPath(candidate); + var resolvedFile_1 = loadNodeModuleFromDirectory(typeReferenceExtensions, candidate, failedLookupLocations, !directoryProbablyExists(candidateDirectory, host), moduleResolutionState); + if (resolvedFile_1) { + if (traceEnabled) { + trace(host, ts.Diagnostics.Type_reference_directive_0_was_successfully_resolved_to_1_primary_Colon_2, typeReferenceDirectiveName, resolvedFile_1, true); + } + return { + resolvedTypeReferenceDirective: { primary: true, resolvedFileName: resolvedFile_1 }, + failedLookupLocations: failedLookupLocations + }; + } + } } else { - var start = lineStarts[lineIndex]; - var pos = lineStarts[lineIndex + 1] - 1; - ts.Debug.assert(ts.isLineBreak(sourceText.charCodeAt(pos))); - while (start <= pos && ts.isLineBreak(sourceText.charCodeAt(pos))) { - pos--; + if (traceEnabled) { + trace(host, ts.Diagnostics.Root_directory_cannot_be_determined_skipping_primary_search_paths); } - return pos; } - } - ts.getEndLinePosition = getEndLinePosition; - function nodeIsMissing(node) { - if (node === undefined) { - return true; + var resolvedFile; + var initialLocationForSecondaryLookup; + if (containingFile) { + initialLocationForSecondaryLookup = ts.getDirectoryPath(containingFile); } - return node.pos === node.end && node.pos >= 0 && node.kind !== 1; + if (initialLocationForSecondaryLookup !== undefined) { + if (traceEnabled) { + trace(host, ts.Diagnostics.Looking_up_in_node_modules_folder_initial_location_0, initialLocationForSecondaryLookup); + } + resolvedFile = loadModuleFromNodeModules(typeReferenceDirectiveName, initialLocationForSecondaryLookup, failedLookupLocations, moduleResolutionState, false); + if (traceEnabled) { + if (resolvedFile) { + trace(host, ts.Diagnostics.Type_reference_directive_0_was_successfully_resolved_to_1_primary_Colon_2, typeReferenceDirectiveName, resolvedFile, false); + } + else { + trace(host, ts.Diagnostics.Type_reference_directive_0_was_not_resolved, typeReferenceDirectiveName); + } + } + } + else { + if (traceEnabled) { + trace(host, ts.Diagnostics.Containing_file_is_not_specified_and_root_directory_cannot_be_determined_skipping_lookup_in_node_modules_folder); + } + } + return { + resolvedTypeReferenceDirective: resolvedFile + ? { primary: false, resolvedFileName: resolvedFile } + : undefined, + failedLookupLocations: failedLookupLocations + }; } - ts.nodeIsMissing = nodeIsMissing; - function nodeIsPresent(node) { - return !nodeIsMissing(node); + ts.resolveTypeReferenceDirective = resolveTypeReferenceDirective; + function getAutomaticTypeDirectiveNames(options, host) { + if (options.types) { + return options.types; + } + var result = []; + if (host.directoryExists && host.getDirectories) { + var typeRoots = getEffectiveTypeRoots(options, host); + if (typeRoots) { + for (var _i = 0, typeRoots_1 = typeRoots; _i < typeRoots_1.length; _i++) { + var root = typeRoots_1[_i]; + if (host.directoryExists(root)) { + for (var _a = 0, _b = host.getDirectories(root); _a < _b.length; _a++) { + var typeDirectivePath = _b[_a]; + var normalized = ts.normalizePath(typeDirectivePath); + var packageJsonPath = pathToPackageJson(ts.combinePaths(root, normalized)); + var isNotNeededPackage = host.fileExists(packageJsonPath) && readJson(packageJsonPath, host).typings === null; + if (!isNotNeededPackage) { + result.push(ts.getBaseFileName(normalized)); + } + } + } + } + } + } + return result; } - ts.nodeIsPresent = nodeIsPresent; - function getTokenPosOfNode(node, sourceFile, includeJsDocComment) { - if (nodeIsMissing(node)) { - return node.pos; + ts.getAutomaticTypeDirectiveNames = getAutomaticTypeDirectiveNames; + function resolveModuleName(moduleName, containingFile, compilerOptions, host) { + var traceEnabled = isTraceEnabled(compilerOptions, host); + if (traceEnabled) { + trace(host, ts.Diagnostics.Resolving_module_0_from_1, moduleName, containingFile); } - if (isJSDocNode(node)) { - return ts.skipTrivia((sourceFile || getSourceFileOfNode(node)).text, node.pos, false, true); + var moduleResolution = compilerOptions.moduleResolution; + if (moduleResolution === undefined) { + moduleResolution = ts.getEmitModuleKind(compilerOptions) === ts.ModuleKind.CommonJS ? ts.ModuleResolutionKind.NodeJs : ts.ModuleResolutionKind.Classic; + if (traceEnabled) { + trace(host, ts.Diagnostics.Module_resolution_kind_is_not_specified_using_0, ts.ModuleResolutionKind[moduleResolution]); + } } - if (includeJsDocComment && node.jsDocComments && node.jsDocComments.length > 0) { - return getTokenPosOfNode(node.jsDocComments[0]); + else { + if (traceEnabled) { + trace(host, ts.Diagnostics.Explicitly_specified_module_resolution_kind_Colon_0, ts.ModuleResolutionKind[moduleResolution]); + } } - if (node.kind === 286 && node._children.length > 0) { - return getTokenPosOfNode(node._children[0], sourceFile, includeJsDocComment); + var result; + switch (moduleResolution) { + case ts.ModuleResolutionKind.NodeJs: + result = nodeModuleNameResolver(moduleName, containingFile, compilerOptions, host); + break; + case ts.ModuleResolutionKind.Classic: + result = classicNameResolver(moduleName, containingFile, compilerOptions, host); + break; } - return ts.skipTrivia((sourceFile || getSourceFileOfNode(node)).text, node.pos); + if (traceEnabled) { + if (result.resolvedModule) { + trace(host, ts.Diagnostics.Module_name_0_was_successfully_resolved_to_1, moduleName, result.resolvedModule.resolvedFileName); + } + else { + trace(host, ts.Diagnostics.Module_name_0_was_not_resolved, moduleName); + } + } + return result; } - ts.getTokenPosOfNode = getTokenPosOfNode; - function isJSDocNode(node) { - return node.kind >= 257 && node.kind <= 282; + ts.resolveModuleName = resolveModuleName; + function tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state) { + if (moduleHasNonRelativeName(moduleName)) { + return tryLoadModuleUsingBaseUrl(moduleName, loader, failedLookupLocations, supportedExtensions, state); + } + else { + return tryLoadModuleUsingRootDirs(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state); + } } - ts.isJSDocNode = isJSDocNode; - function isJSDocTag(node) { - return node.kind >= 273 && node.kind <= 285; + function tryLoadModuleUsingRootDirs(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state) { + if (!state.compilerOptions.rootDirs) { + return undefined; + } + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.rootDirs_option_is_set_using_it_to_resolve_relative_module_name_0, moduleName); + } + var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); + var matchedRootDir; + var matchedNormalizedPrefix; + for (var _i = 0, _a = state.compilerOptions.rootDirs; _i < _a.length; _i++) { + var rootDir = _a[_i]; + var normalizedRoot = ts.normalizePath(rootDir); + if (!ts.endsWith(normalizedRoot, ts.directorySeparator)) { + normalizedRoot += ts.directorySeparator; + } + var isLongestMatchingPrefix = ts.startsWith(candidate, normalizedRoot) && + (matchedNormalizedPrefix === undefined || matchedNormalizedPrefix.length < normalizedRoot.length); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Checking_if_0_is_the_longest_matching_prefix_for_1_2, normalizedRoot, candidate, isLongestMatchingPrefix); + } + if (isLongestMatchingPrefix) { + matchedNormalizedPrefix = normalizedRoot; + matchedRootDir = rootDir; + } + } + if (matchedNormalizedPrefix) { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Longest_matching_prefix_for_0_is_1, candidate, matchedNormalizedPrefix); + } + var suffix = candidate.substr(matchedNormalizedPrefix.length); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Loading_0_from_the_root_dir_1_candidate_location_2, suffix, matchedNormalizedPrefix, candidate); + } + var resolvedFileName = loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(containingDirectory, state.host), state); + if (resolvedFileName) { + return resolvedFileName; + } + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Trying_other_entries_in_rootDirs); + } + for (var _b = 0, _c = state.compilerOptions.rootDirs; _b < _c.length; _b++) { + var rootDir = _c[_b]; + if (rootDir === matchedRootDir) { + continue; + } + var candidate_1 = ts.combinePaths(ts.normalizePath(rootDir), suffix); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Loading_0_from_the_root_dir_1_candidate_location_2, suffix, rootDir, candidate_1); + } + var baseDirectory = ts.getDirectoryPath(candidate_1); + var resolvedFileName_1 = loader(candidate_1, supportedExtensions, failedLookupLocations, !directoryProbablyExists(baseDirectory, state.host), state); + if (resolvedFileName_1) { + return resolvedFileName_1; + } + } + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Module_resolution_using_rootDirs_has_failed); + } + } + return undefined; } - ts.isJSDocTag = isJSDocTag; - function getNonDecoratorTokenPosOfNode(node, sourceFile) { - if (nodeIsMissing(node) || !node.decorators) { - return getTokenPosOfNode(node, sourceFile); + function tryLoadModuleUsingBaseUrl(moduleName, loader, failedLookupLocations, supportedExtensions, state) { + if (!state.compilerOptions.baseUrl) { + return undefined; + } + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.baseUrl_option_is_set_to_0_using_this_value_to_resolve_non_relative_module_name_1, state.compilerOptions.baseUrl, moduleName); + } + var matchedPattern = undefined; + if (state.compilerOptions.paths) { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.paths_option_is_specified_looking_for_a_pattern_to_match_module_name_0, moduleName); + } + matchedPattern = ts.matchPatternOrExact(ts.getOwnKeys(state.compilerOptions.paths), moduleName); + } + if (matchedPattern) { + var matchedStar = typeof matchedPattern === "string" ? undefined : ts.matchedText(matchedPattern, moduleName); + var matchedPatternText = typeof matchedPattern === "string" ? matchedPattern : ts.patternText(matchedPattern); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Module_name_0_matched_pattern_1, moduleName, matchedPatternText); + } + for (var _i = 0, _a = state.compilerOptions.paths[matchedPatternText]; _i < _a.length; _i++) { + var subst = _a[_i]; + var path = matchedStar ? subst.replace("*", matchedStar) : subst; + var candidate = ts.normalizePath(ts.combinePaths(state.compilerOptions.baseUrl, path)); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Trying_substitution_0_candidate_module_location_Colon_1, subst, path); + } + var resolvedFileName = loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(ts.getDirectoryPath(candidate), state.host), state); + if (resolvedFileName) { + return resolvedFileName; + } + } + return undefined; + } + else { + var candidate = ts.normalizePath(ts.combinePaths(state.compilerOptions.baseUrl, moduleName)); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Resolving_module_name_0_relative_to_base_url_1_2, moduleName, state.compilerOptions.baseUrl, candidate); + } + return loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(ts.getDirectoryPath(candidate), state.host), state); } - return ts.skipTrivia((sourceFile || getSourceFileOfNode(node)).text, node.decorators.end); } - ts.getNonDecoratorTokenPosOfNode = getNonDecoratorTokenPosOfNode; - function getSourceTextOfNodeFromSourceFile(sourceFile, node, includeTrivia) { - if (includeTrivia === void 0) { includeTrivia = false; } - if (nodeIsMissing(node)) { - return ""; + function nodeModuleNameResolver(moduleName, containingFile, compilerOptions, host) { + var containingDirectory = ts.getDirectoryPath(containingFile); + var supportedExtensions = ts.getSupportedExtensions(compilerOptions); + var traceEnabled = isTraceEnabled(compilerOptions, host); + var failedLookupLocations = []; + var state = { compilerOptions: compilerOptions, host: host, traceEnabled: traceEnabled, skipTsx: false }; + var resolvedFileName = tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, nodeLoadModuleByRelativeName, failedLookupLocations, supportedExtensions, state); + var isExternalLibraryImport = false; + if (!resolvedFileName) { + if (moduleHasNonRelativeName(moduleName)) { + if (traceEnabled) { + trace(host, ts.Diagnostics.Loading_module_0_from_node_modules_folder, moduleName); + } + resolvedFileName = loadModuleFromNodeModules(moduleName, containingDirectory, failedLookupLocations, state, false); + isExternalLibraryImport = resolvedFileName !== undefined; + } + else { + var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); + resolvedFileName = nodeLoadModuleByRelativeName(candidate, supportedExtensions, failedLookupLocations, false, state); + } } - var text = sourceFile.text; - return text.substring(includeTrivia ? node.pos : ts.skipTrivia(text, node.pos), node.end); + if (resolvedFileName && host.realpath) { + var originalFileName = resolvedFileName; + resolvedFileName = ts.normalizePath(host.realpath(resolvedFileName)); + if (traceEnabled) { + trace(host, ts.Diagnostics.Resolving_real_path_for_0_result_1, originalFileName, resolvedFileName); + } + } + return createResolvedModule(resolvedFileName, isExternalLibraryImport, failedLookupLocations); } - ts.getSourceTextOfNodeFromSourceFile = getSourceTextOfNodeFromSourceFile; - function getTextOfNodeFromSourceText(sourceText, node) { - if (nodeIsMissing(node)) { - return ""; + ts.nodeModuleNameResolver = nodeModuleNameResolver; + function nodeLoadModuleByRelativeName(candidate, supportedExtensions, failedLookupLocations, onlyRecordFailures, state) { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Loading_module_as_file_Slash_folder_candidate_module_location_0, candidate); } - return sourceText.substring(ts.skipTrivia(sourceText, node.pos), node.end); + var resolvedFileName = !ts.pathEndsWithDirectorySeparator(candidate) && loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, onlyRecordFailures, state); + return resolvedFileName || loadNodeModuleFromDirectory(supportedExtensions, candidate, failedLookupLocations, onlyRecordFailures, state); } - ts.getTextOfNodeFromSourceText = getTextOfNodeFromSourceText; - function getTextOfNode(node, includeTrivia) { - if (includeTrivia === void 0) { includeTrivia = false; } - return getSourceTextOfNodeFromSourceFile(getSourceFileOfNode(node), node, includeTrivia); + function directoryProbablyExists(directoryName, host) { + return !host.directoryExists || host.directoryExists(directoryName); } - ts.getTextOfNode = getTextOfNode; - function getLiteralText(node, sourceFile, languageVersion) { - if (languageVersion < 2 && (isTemplateLiteralKind(node.kind) || node.hasExtendedUnicodeEscape)) { - return getQuotedEscapedLiteralText('"', node.text, '"'); + ts.directoryProbablyExists = directoryProbablyExists; + function loadModuleFromFile(candidate, extensions, failedLookupLocation, onlyRecordFailures, state) { + var resolvedByAddingExtension = tryAddingExtensions(candidate, extensions, failedLookupLocation, onlyRecordFailures, state); + if (resolvedByAddingExtension) { + return resolvedByAddingExtension; } - if (!nodeIsSynthesized(node) && node.parent) { - var text = getSourceTextOfNodeFromSourceFile(sourceFile, node); - if (languageVersion < 2 && isBinaryOrOctalIntegerLiteral(node, text)) { - return node.text; + if (ts.hasJavaScriptFileExtension(candidate)) { + var extensionless = ts.removeFileExtension(candidate); + if (state.traceEnabled) { + var extension = candidate.substring(extensionless.length); + trace(state.host, ts.Diagnostics.File_name_0_has_a_1_extension_stripping_it, candidate, extension); } - return text; + return tryAddingExtensions(extensionless, extensions, failedLookupLocation, onlyRecordFailures, state); } - switch (node.kind) { - case 9: - return getQuotedEscapedLiteralText('"', node.text, '"'); - case 11: - return getQuotedEscapedLiteralText("`", node.text, "`"); - case 12: - return getQuotedEscapedLiteralText("`", node.text, "${"); - case 13: - return getQuotedEscapedLiteralText("}", node.text, "${"); - case 14: - return getQuotedEscapedLiteralText("}", node.text, "`"); - case 8: - return node.text; + } + function tryAddingExtensions(candidate, extensions, failedLookupLocation, onlyRecordFailures, state) { + if (!onlyRecordFailures) { + var directory = ts.getDirectoryPath(candidate); + if (directory) { + onlyRecordFailures = !directoryProbablyExists(directory, state.host); + } } - ts.Debug.fail("Literal kind '" + node.kind + "' not accounted for."); + return ts.forEach(extensions, function (ext) { + return !(state.skipTsx && ts.isJsxOrTsxExtension(ext)) && tryFile(candidate + ext, failedLookupLocation, onlyRecordFailures, state); + }); } - ts.getLiteralText = getLiteralText; - function isBinaryOrOctalIntegerLiteral(node, text) { - if (node.kind === 8 && text.length > 1) { - switch (text.charCodeAt(1)) { - case 98: - case 66: - case 111: - case 79: - return true; + function tryFile(fileName, failedLookupLocation, onlyRecordFailures, state) { + if (!onlyRecordFailures && state.host.fileExists(fileName)) { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.File_0_exist_use_it_as_a_name_resolution_result, fileName); } + return fileName; + } + else { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.File_0_does_not_exist, fileName); + } + failedLookupLocation.push(fileName); + return undefined; } - return false; } - ts.isBinaryOrOctalIntegerLiteral = isBinaryOrOctalIntegerLiteral; - function getQuotedEscapedLiteralText(leftQuote, text, rightQuote) { - return leftQuote + escapeNonAsciiCharacters(escapeString(text)) + rightQuote; + function loadNodeModuleFromDirectory(extensions, candidate, failedLookupLocation, onlyRecordFailures, state) { + var packageJsonPath = pathToPackageJson(candidate); + var directoryExists = !onlyRecordFailures && directoryProbablyExists(candidate, state.host); + if (directoryExists && state.host.fileExists(packageJsonPath)) { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Found_package_json_at_0, packageJsonPath); + } + var typesFile = tryReadTypesSection(packageJsonPath, candidate, state); + if (typesFile) { + var onlyRecordFailures_1 = !directoryProbablyExists(ts.getDirectoryPath(typesFile), state.host); + var result = tryFile(typesFile, failedLookupLocation, onlyRecordFailures_1, state) || + tryAddingExtensions(typesFile, extensions, failedLookupLocation, onlyRecordFailures_1, state); + if (result) { + return result; + } + } + else { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.package_json_does_not_have_types_field); + } + } + } + else { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.File_0_does_not_exist, packageJsonPath); + } + failedLookupLocation.push(packageJsonPath); + } + return loadModuleFromFile(ts.combinePaths(candidate, "index"), extensions, failedLookupLocation, !directoryExists, state); } - function escapeIdentifier(identifier) { - return identifier.length >= 2 && identifier.charCodeAt(0) === 95 && identifier.charCodeAt(1) === 95 ? "_" + identifier : identifier; + function pathToPackageJson(directory) { + return ts.combinePaths(directory, "package.json"); } - ts.escapeIdentifier = escapeIdentifier; - function unescapeIdentifier(identifier) { - return identifier.length >= 3 && identifier.charCodeAt(0) === 95 && identifier.charCodeAt(1) === 95 && identifier.charCodeAt(2) === 95 ? identifier.substr(1) : identifier; + function loadModuleFromNodeModulesFolder(moduleName, directory, failedLookupLocations, state) { + var nodeModulesFolder = ts.combinePaths(directory, "node_modules"); + var nodeModulesFolderExists = directoryProbablyExists(nodeModulesFolder, state.host); + var candidate = ts.normalizePath(ts.combinePaths(nodeModulesFolder, moduleName)); + var supportedExtensions = ts.getSupportedExtensions(state.compilerOptions); + var result = loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, !nodeModulesFolderExists, state); + if (result) { + return result; + } + result = loadNodeModuleFromDirectory(supportedExtensions, candidate, failedLookupLocations, !nodeModulesFolderExists, state); + if (result) { + return result; + } } - ts.unescapeIdentifier = unescapeIdentifier; - function makeIdentifierFromModuleName(moduleName) { - return ts.getBaseFileName(moduleName).replace(/^(\d)/, "_$1").replace(/\W/g, "_"); + function loadModuleFromNodeModules(moduleName, directory, failedLookupLocations, state, checkOneLevel) { + return loadModuleFromNodeModulesWorker(moduleName, directory, failedLookupLocations, state, checkOneLevel, false); } - ts.makeIdentifierFromModuleName = makeIdentifierFromModuleName; - function isBlockOrCatchScoped(declaration) { - return (ts.getCombinedNodeFlags(declaration) & 3) !== 0 || + ts.loadModuleFromNodeModules = loadModuleFromNodeModules; + function loadModuleFromNodeModulesAtTypes(moduleName, directory, failedLookupLocations, state) { + return loadModuleFromNodeModulesWorker(moduleName, directory, failedLookupLocations, state, false, true); + } + function loadModuleFromNodeModulesWorker(moduleName, directory, failedLookupLocations, state, checkOneLevel, typesOnly) { + directory = ts.normalizeSlashes(directory); + while (true) { + var baseName = ts.getBaseFileName(directory); + if (baseName !== "node_modules") { + var packageResult = void 0; + if (!typesOnly) { + packageResult = loadModuleFromNodeModulesFolder(moduleName, directory, failedLookupLocations, state); + if (packageResult && ts.hasTypeScriptFileExtension(packageResult)) { + return packageResult; + } + } + var typesResult = loadModuleFromNodeModulesFolder(ts.combinePaths("@types", moduleName), directory, failedLookupLocations, state); + if (typesResult || packageResult) { + return typesResult || packageResult; + } + } + var parentPath = ts.getDirectoryPath(directory); + if (parentPath === directory || checkOneLevel) { + break; + } + directory = parentPath; + } + return undefined; + } + function classicNameResolver(moduleName, containingFile, compilerOptions, host) { + var traceEnabled = isTraceEnabled(compilerOptions, host); + var state = { compilerOptions: compilerOptions, host: host, traceEnabled: traceEnabled, skipTsx: !compilerOptions.jsx }; + var failedLookupLocations = []; + var supportedExtensions = ts.getSupportedExtensions(compilerOptions); + var containingDirectory = ts.getDirectoryPath(containingFile); + var resolvedFileName = tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, loadModuleFromFile, failedLookupLocations, supportedExtensions, state); + if (resolvedFileName) { + return createResolvedModule(resolvedFileName, false, failedLookupLocations); + } + var referencedSourceFile; + if (moduleHasNonRelativeName(moduleName)) { + referencedSourceFile = referencedSourceFile = loadModuleFromAncestorDirectories(moduleName, containingDirectory, supportedExtensions, failedLookupLocations, state) || + loadModuleFromNodeModulesAtTypes(moduleName, containingDirectory, failedLookupLocations, state); + } + else { + var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); + referencedSourceFile = loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, false, state); + } + return referencedSourceFile + ? { resolvedModule: { resolvedFileName: referencedSourceFile }, failedLookupLocations: failedLookupLocations } + : { resolvedModule: undefined, failedLookupLocations: failedLookupLocations }; + } + ts.classicNameResolver = classicNameResolver; + function loadModuleFromAncestorDirectories(moduleName, containingDirectory, supportedExtensions, failedLookupLocations, state) { + while (true) { + var searchName = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); + var referencedSourceFile = loadModuleFromFile(searchName, supportedExtensions, failedLookupLocations, false, state); + if (referencedSourceFile) { + return referencedSourceFile; + } + var parentPath = ts.getDirectoryPath(containingDirectory); + if (parentPath === containingDirectory) { + return undefined; + } + containingDirectory = parentPath; + } + } +})(ts || (ts = {})); +var ts; +(function (ts) { + ts.externalHelpersModuleNameText = "tslib"; + function getDeclarationOfKind(symbol, kind) { + var declarations = symbol.declarations; + if (declarations) { + for (var _i = 0, declarations_1 = declarations; _i < declarations_1.length; _i++) { + var declaration = declarations_1[_i]; + if (declaration.kind === kind) { + return declaration; + } + } + } + return undefined; + } + ts.getDeclarationOfKind = getDeclarationOfKind; + var stringWriters = []; + function getSingleLineStringWriter() { + if (stringWriters.length === 0) { + var str_1 = ""; + var writeText = function (text) { return str_1 += text; }; + return { + string: function () { return str_1; }, + writeKeyword: writeText, + writeOperator: writeText, + writePunctuation: writeText, + writeSpace: writeText, + writeStringLiteral: writeText, + writeParameter: writeText, + writeSymbol: writeText, + writeLine: function () { return str_1 += " "; }, + increaseIndent: function () { }, + decreaseIndent: function () { }, + clear: function () { return str_1 = ""; }, + trackSymbol: function () { }, + reportInaccessibleThisError: function () { } + }; + } + return stringWriters.pop(); + } + ts.getSingleLineStringWriter = getSingleLineStringWriter; + function releaseStringWriter(writer) { + writer.clear(); + stringWriters.push(writer); + } + ts.releaseStringWriter = releaseStringWriter; + function getFullWidth(node) { + return node.end - node.pos; + } + ts.getFullWidth = getFullWidth; + function arrayIsEqualTo(array1, array2, equaler) { + if (!array1 || !array2) { + return array1 === array2; + } + if (array1.length !== array2.length) { + return false; + } + for (var i = 0; i < array1.length; i++) { + var equals = equaler ? equaler(array1[i], array2[i]) : array1[i] === array2[i]; + if (!equals) { + return false; + } + } + return true; + } + ts.arrayIsEqualTo = arrayIsEqualTo; + function hasResolvedModule(sourceFile, moduleNameText) { + return !!(sourceFile && sourceFile.resolvedModules && sourceFile.resolvedModules[moduleNameText]); + } + ts.hasResolvedModule = hasResolvedModule; + function getResolvedModule(sourceFile, moduleNameText) { + return hasResolvedModule(sourceFile, moduleNameText) ? sourceFile.resolvedModules[moduleNameText] : undefined; + } + ts.getResolvedModule = getResolvedModule; + function setResolvedModule(sourceFile, moduleNameText, resolvedModule) { + if (!sourceFile.resolvedModules) { + sourceFile.resolvedModules = ts.createMap(); + } + sourceFile.resolvedModules[moduleNameText] = resolvedModule; + } + ts.setResolvedModule = setResolvedModule; + function setResolvedTypeReferenceDirective(sourceFile, typeReferenceDirectiveName, resolvedTypeReferenceDirective) { + if (!sourceFile.resolvedTypeReferenceDirectiveNames) { + sourceFile.resolvedTypeReferenceDirectiveNames = ts.createMap(); + } + sourceFile.resolvedTypeReferenceDirectiveNames[typeReferenceDirectiveName] = resolvedTypeReferenceDirective; + } + ts.setResolvedTypeReferenceDirective = setResolvedTypeReferenceDirective; + function moduleResolutionIsEqualTo(oldResolution, newResolution) { + return oldResolution.resolvedFileName === newResolution.resolvedFileName && oldResolution.isExternalLibraryImport === newResolution.isExternalLibraryImport; + } + ts.moduleResolutionIsEqualTo = moduleResolutionIsEqualTo; + function typeDirectiveIsEqualTo(oldResolution, newResolution) { + return oldResolution.resolvedFileName === newResolution.resolvedFileName && oldResolution.primary === newResolution.primary; + } + ts.typeDirectiveIsEqualTo = typeDirectiveIsEqualTo; + function hasChangesInResolutions(names, newResolutions, oldResolutions, comparer) { + if (names.length !== newResolutions.length) { + return false; + } + for (var i = 0; i < names.length; i++) { + var newResolution = newResolutions[i]; + var oldResolution = oldResolutions && oldResolutions[names[i]]; + var changed = oldResolution + ? !newResolution || !comparer(oldResolution, newResolution) + : newResolution; + if (changed) { + return true; + } + } + return false; + } + ts.hasChangesInResolutions = hasChangesInResolutions; + function containsParseError(node) { + aggregateChildData(node); + return (node.flags & 2097152) !== 0; + } + ts.containsParseError = containsParseError; + function aggregateChildData(node) { + if (!(node.flags & 4194304)) { + var thisNodeOrAnySubNodesHasError = ((node.flags & 524288) !== 0) || + ts.forEachChild(node, containsParseError); + if (thisNodeOrAnySubNodesHasError) { + node.flags |= 2097152; + } + node.flags |= 4194304; + } + } + function getSourceFileOfNode(node) { + while (node && node.kind !== 256) { + node = node.parent; + } + return node; + } + ts.getSourceFileOfNode = getSourceFileOfNode; + function isStatementWithLocals(node) { + switch (node.kind) { + case 199: + case 227: + case 206: + case 207: + case 208: + return true; + } + return false; + } + ts.isStatementWithLocals = isStatementWithLocals; + function getStartPositionOfLine(line, sourceFile) { + ts.Debug.assert(line >= 0); + return ts.getLineStarts(sourceFile)[line]; + } + ts.getStartPositionOfLine = getStartPositionOfLine; + function nodePosToString(node) { + var file = getSourceFileOfNode(node); + var loc = ts.getLineAndCharacterOfPosition(file, node.pos); + return file.fileName + "(" + (loc.line + 1) + "," + (loc.character + 1) + ")"; + } + ts.nodePosToString = nodePosToString; + function getStartPosOfNode(node) { + return node.pos; + } + ts.getStartPosOfNode = getStartPosOfNode; + function isDefined(value) { + return value !== undefined; + } + ts.isDefined = isDefined; + function getEndLinePosition(line, sourceFile) { + ts.Debug.assert(line >= 0); + var lineStarts = ts.getLineStarts(sourceFile); + var lineIndex = line; + var sourceText = sourceFile.text; + if (lineIndex + 1 === lineStarts.length) { + return sourceText.length - 1; + } + else { + var start = lineStarts[lineIndex]; + var pos = lineStarts[lineIndex + 1] - 1; + ts.Debug.assert(ts.isLineBreak(sourceText.charCodeAt(pos))); + while (start <= pos && ts.isLineBreak(sourceText.charCodeAt(pos))) { + pos--; + } + return pos; + } + } + ts.getEndLinePosition = getEndLinePosition; + function nodeIsMissing(node) { + if (node === undefined) { + return true; + } + return node.pos === node.end && node.pos >= 0 && node.kind !== 1; + } + ts.nodeIsMissing = nodeIsMissing; + function nodeIsPresent(node) { + return !nodeIsMissing(node); + } + ts.nodeIsPresent = nodeIsPresent; + function getTokenPosOfNode(node, sourceFile, includeJsDocComment) { + if (nodeIsMissing(node)) { + return node.pos; + } + if (isJSDocNode(node)) { + return ts.skipTrivia((sourceFile || getSourceFileOfNode(node)).text, node.pos, false, true); + } + if (includeJsDocComment && node.jsDocComments && node.jsDocComments.length > 0) { + return getTokenPosOfNode(node.jsDocComments[0]); + } + if (node.kind === 286 && node._children.length > 0) { + return getTokenPosOfNode(node._children[0], sourceFile, includeJsDocComment); + } + return ts.skipTrivia((sourceFile || getSourceFileOfNode(node)).text, node.pos); + } + ts.getTokenPosOfNode = getTokenPosOfNode; + function isJSDocNode(node) { + return node.kind >= 257 && node.kind <= 282; + } + ts.isJSDocNode = isJSDocNode; + function isJSDocTag(node) { + return node.kind >= 273 && node.kind <= 285; + } + ts.isJSDocTag = isJSDocTag; + function getNonDecoratorTokenPosOfNode(node, sourceFile) { + if (nodeIsMissing(node) || !node.decorators) { + return getTokenPosOfNode(node, sourceFile); + } + return ts.skipTrivia((sourceFile || getSourceFileOfNode(node)).text, node.decorators.end); + } + ts.getNonDecoratorTokenPosOfNode = getNonDecoratorTokenPosOfNode; + function getSourceTextOfNodeFromSourceFile(sourceFile, node, includeTrivia) { + if (includeTrivia === void 0) { includeTrivia = false; } + if (nodeIsMissing(node)) { + return ""; + } + var text = sourceFile.text; + return text.substring(includeTrivia ? node.pos : ts.skipTrivia(text, node.pos), node.end); + } + ts.getSourceTextOfNodeFromSourceFile = getSourceTextOfNodeFromSourceFile; + function getTextOfNodeFromSourceText(sourceText, node) { + if (nodeIsMissing(node)) { + return ""; + } + return sourceText.substring(ts.skipTrivia(sourceText, node.pos), node.end); + } + ts.getTextOfNodeFromSourceText = getTextOfNodeFromSourceText; + function getTextOfNode(node, includeTrivia) { + if (includeTrivia === void 0) { includeTrivia = false; } + return getSourceTextOfNodeFromSourceFile(getSourceFileOfNode(node), node, includeTrivia); + } + ts.getTextOfNode = getTextOfNode; + function getLiteralText(node, sourceFile, languageVersion) { + if (languageVersion < 2 && (isTemplateLiteralKind(node.kind) || node.hasExtendedUnicodeEscape)) { + return getQuotedEscapedLiteralText('"', node.text, '"'); + } + if (!nodeIsSynthesized(node) && node.parent) { + var text = getSourceTextOfNodeFromSourceFile(sourceFile, node); + if (languageVersion < 2 && isBinaryOrOctalIntegerLiteral(node, text)) { + return node.text; + } + return text; + } + switch (node.kind) { + case 9: + return getQuotedEscapedLiteralText('"', node.text, '"'); + case 11: + return getQuotedEscapedLiteralText("`", node.text, "`"); + case 12: + return getQuotedEscapedLiteralText("`", node.text, "${"); + case 13: + return getQuotedEscapedLiteralText("}", node.text, "${"); + case 14: + return getQuotedEscapedLiteralText("}", node.text, "`"); + case 8: + return node.text; + } + ts.Debug.fail("Literal kind '" + node.kind + "' not accounted for."); + } + ts.getLiteralText = getLiteralText; + function isBinaryOrOctalIntegerLiteral(node, text) { + if (node.kind === 8 && text.length > 1) { + switch (text.charCodeAt(1)) { + case 98: + case 66: + case 111: + case 79: + return true; + } + } + return false; + } + ts.isBinaryOrOctalIntegerLiteral = isBinaryOrOctalIntegerLiteral; + function getQuotedEscapedLiteralText(leftQuote, text, rightQuote) { + return leftQuote + escapeNonAsciiCharacters(escapeString(text)) + rightQuote; + } + function escapeIdentifier(identifier) { + return identifier.length >= 2 && identifier.charCodeAt(0) === 95 && identifier.charCodeAt(1) === 95 ? "_" + identifier : identifier; + } + ts.escapeIdentifier = escapeIdentifier; + function unescapeIdentifier(identifier) { + return identifier.length >= 3 && identifier.charCodeAt(0) === 95 && identifier.charCodeAt(1) === 95 && identifier.charCodeAt(2) === 95 ? identifier.substr(1) : identifier; + } + ts.unescapeIdentifier = unescapeIdentifier; + function makeIdentifierFromModuleName(moduleName) { + return ts.getBaseFileName(moduleName).replace(/^(\d)/, "_$1").replace(/\W/g, "_"); + } + ts.makeIdentifierFromModuleName = makeIdentifierFromModuleName; + function isBlockOrCatchScoped(declaration) { + return (ts.getCombinedNodeFlags(declaration) & 3) !== 0 || isCatchClauseVariableDeclaration(declaration); } ts.isBlockOrCatchScoped = isBlockOrCatchScoped; @@ -5877,10 +7014,10 @@ var ts; return !!(ts.getCombinedNodeFlags(node) & 1); } ts.isLet = isLet; - function isSuperCallExpression(n) { + function isSuperCall(n) { return n.kind === 174 && n.expression.kind === 95; } - ts.isSuperCallExpression = isSuperCallExpression; + ts.isSuperCall = isSuperCall; function isPrologueDirective(node) { return node.kind === 202 && node.expression.kind === 9; } @@ -5943,23 +7080,23 @@ var ts; case 139: case 172: case 97: - var parent_1 = node.parent; - if (parent_1.kind === 158) { + var parent_2 = node.parent; + if (parent_2.kind === 158) { return false; } - if (154 <= parent_1.kind && parent_1.kind <= 166) { + if (154 <= parent_2.kind && parent_2.kind <= 166) { return true; } - switch (parent_1.kind) { + switch (parent_2.kind) { case 194: - return !isExpressionWithTypeArgumentsInClassExtendsClause(parent_1); + return !isExpressionWithTypeArgumentsInClassExtendsClause(parent_2); case 141: - return node === parent_1.constraint; + return node === parent_2.constraint; case 145: case 144: case 142: case 218: - return node === parent_1.type; + return node === parent_2.type; case 220: case 179: case 180: @@ -5968,16 +7105,16 @@ var ts; case 146: case 149: case 150: - return node === parent_1.type; + return node === parent_2.type; case 151: case 152: case 153: - return node === parent_1.type; + return node === parent_2.type; case 177: - return node === parent_1.type; + return node === parent_2.type; case 174: case 175: - return parent_1.typeArguments && ts.indexOf(parent_1.typeArguments, node) >= 0; + return parent_2.typeArguments && ts.indexOf(parent_2.typeArguments, node) >= 0; case 176: return false; } @@ -6030,9 +7167,9 @@ var ts; return; default: if (isFunctionLike(node)) { - var name_7 = node.name; - if (name_7 && name_7.kind === 140) { - traverse(name_7.expression); + var name_8 = node.name; + if (name_8 && name_8.kind === 140) { + traverse(name_8.expression); return; } } @@ -6128,6 +7265,12 @@ var ts; return node && node.kind === 147 && node.parent.kind === 171; } ts.isObjectLiteralMethod = isObjectLiteralMethod; + function isObjectLiteralOrClassExpressionMethod(node) { + return node.kind === 147 && + (node.parent.kind === 171 || + node.parent.kind === 192); + } + ts.isObjectLiteralOrClassExpressionMethod = isObjectLiteralOrClassExpressionMethod; function isIdentifierTypePredicate(predicate) { return predicate && predicate.kind === 1; } @@ -6238,13 +7381,13 @@ var ts; function getImmediatelyInvokedFunctionExpression(func) { if (func.kind === 179 || func.kind === 180) { var prev = func; - var parent_2 = func.parent; - while (parent_2.kind === 178) { - prev = parent_2; - parent_2 = parent_2.parent; + var parent_3 = func.parent; + while (parent_3.kind === 178) { + prev = parent_3; + parent_3 = parent_3.parent; } - if (parent_2.kind === 174 && parent_2.expression === prev) { - return parent_2; + if (parent_3.kind === 174 && parent_3.expression === prev) { + return parent_3; } } } @@ -6390,8 +7533,8 @@ var ts; case 8: case 9: case 97: - var parent_3 = node.parent; - switch (parent_3.kind) { + var parent_4 = node.parent; + switch (parent_4.kind) { case 218: case 142: case 145: @@ -6399,7 +7542,7 @@ var ts; case 255: case 253: case 169: - return parent_3.initializer === node; + return parent_4.initializer === node; case 202: case 203: case 204: @@ -6410,32 +7553,32 @@ var ts; case 249: case 215: case 213: - return parent_3.expression === node; + return parent_4.expression === node; case 206: - var forStatement = parent_3; + var forStatement = parent_4; return (forStatement.initializer === node && forStatement.initializer.kind !== 219) || forStatement.condition === node || forStatement.incrementor === node; case 207: case 208: - var forInStatement = parent_3; + var forInStatement = parent_4; return (forInStatement.initializer === node && forInStatement.initializer.kind !== 219) || forInStatement.expression === node; case 177: case 195: - return node === parent_3.expression; + return node === parent_4.expression; case 197: - return node === parent_3.expression; + return node === parent_4.expression; case 140: - return node === parent_3.expression; + return node === parent_4.expression; case 143: case 248: case 247: return true; case 194: - return parent_3.expression === node && isExpressionWithTypeArgumentsInClassExtendsClause(parent_3); + return parent_4.expression === node && isExpressionWithTypeArgumentsInClassExtendsClause(parent_4); default: - if (isPartOfExpression(parent_3)) { + if (isPartOfExpression(parent_4)) { return true; } } @@ -6443,10 +7586,6 @@ var ts; return false; } ts.isPartOfExpression = isPartOfExpression; - function isExternalModuleNameRelative(moduleName) { - return /^\.\.?($|[\\/])/.test(moduleName); - } - ts.isExternalModuleNameRelative = isExternalModuleNameRelative; function isInstantiatedModule(node, preserveConstEnums) { var moduleState = ts.getModuleInstanceState(node); return moduleState === 1 || @@ -6650,17 +7789,17 @@ var ts; node.parent && node.parent.kind === 225) { result = append(result, getJSDocs(node.parent, checkParentVariableStatement, getDocs, getTags)); } - var parent_4 = node.parent; - var isSourceOfAssignmentExpressionStatement = parent_4 && parent_4.parent && - parent_4.kind === 187 && - parent_4.operatorToken.kind === 56 && - parent_4.parent.kind === 202; + var parent_5 = node.parent; + var isSourceOfAssignmentExpressionStatement = parent_5 && parent_5.parent && + parent_5.kind === 187 && + parent_5.operatorToken.kind === 56 && + parent_5.parent.kind === 202; if (isSourceOfAssignmentExpressionStatement) { - result = append(result, getJSDocs(parent_4.parent, checkParentVariableStatement, getDocs, getTags)); + result = append(result, getJSDocs(parent_5.parent, checkParentVariableStatement, getDocs, getTags)); } - var isPropertyAssignmentExpression = parent_4 && parent_4.kind === 253; + var isPropertyAssignmentExpression = parent_5 && parent_5.kind === 253; if (isPropertyAssignmentExpression) { - result = append(result, getJSDocs(parent_4, checkParentVariableStatement, getDocs, getTags)); + result = append(result, getJSDocs(parent_5, checkParentVariableStatement, getDocs, getTags)); } if (node.kind === 142) { var paramTags = getJSDocParameterTag(node, checkParentVariableStatement); @@ -6693,8 +7832,8 @@ var ts; } } else if (param.name.kind === 69) { - var name_8 = param.name.text; - var paramTags = ts.filter(tags, function (tag) { return tag.kind === 275 && tag.parameterName.text === name_8; }); + var name_9 = param.name.text; + var paramTags = ts.filter(tags, function (tag) { return tag.kind === 275 && tag.parameterName.text === name_9; }); if (paramTags) { return paramTags; } @@ -6765,20 +7904,20 @@ var ts; node = node.parent; } while (true) { - var parent_5 = node.parent; - if (parent_5.kind === 170 || parent_5.kind === 191) { - node = parent_5; + var parent_6 = node.parent; + if (parent_6.kind === 170 || parent_6.kind === 191) { + node = parent_6; continue; } - if (parent_5.kind === 253 || parent_5.kind === 254) { - node = parent_5.parent; + if (parent_6.kind === 253 || parent_6.kind === 254) { + node = parent_6.parent; continue; } - return parent_5.kind === 187 && - isAssignmentOperator(parent_5.operatorToken.kind) && - parent_5.left === node || - (parent_5.kind === 207 || parent_5.kind === 208) && - parent_5.initializer === node; + return parent_6.kind === 187 && + isAssignmentOperator(parent_6.operatorToken.kind) && + parent_6.left === node || + (parent_6.kind === 207 || parent_6.kind === 208) && + parent_6.initializer === node; } } ts.isAssignmentTarget = isAssignmentTarget; @@ -7044,14 +8183,10 @@ var ts; } ts.nodeStartsNewLexicalEnvironment = nodeStartsNewLexicalEnvironment; function nodeIsSynthesized(node) { - return positionIsSynthesized(node.pos) - || positionIsSynthesized(node.end); + return ts.positionIsSynthesized(node.pos) + || ts.positionIsSynthesized(node.end); } ts.nodeIsSynthesized = nodeIsSynthesized; - function positionIsSynthesized(pos) { - return !(pos >= 0); - } - ts.positionIsSynthesized = positionIsSynthesized; function getOriginalNode(node) { if (node) { while (node.original !== undefined) { @@ -7487,28 +8622,16 @@ var ts; function getDeclarationEmitOutputFilePath(sourceFile, host) { var options = host.getCompilerOptions(); var outputDir = options.declarationDir || options.outDir; - if (options.declaration) { - var path = outputDir - ? getSourceFilePathInNewDir(sourceFile, host, outputDir) - : sourceFile.fileName; - return ts.removeFileExtension(path) + ".d.ts"; - } + var path = outputDir + ? getSourceFilePathInNewDir(sourceFile, host, outputDir) + : sourceFile.fileName; + return ts.removeFileExtension(path) + ".d.ts"; } ts.getDeclarationEmitOutputFilePath = getDeclarationEmitOutputFilePath; - function getEmitScriptTarget(compilerOptions) { - return compilerOptions.target || 0; - } - ts.getEmitScriptTarget = getEmitScriptTarget; - function getEmitModuleKind(compilerOptions) { - return typeof compilerOptions.module === "number" ? - compilerOptions.module : - getEmitScriptTarget(compilerOptions) === 2 ? ts.ModuleKind.ES6 : ts.ModuleKind.CommonJS; - } - ts.getEmitModuleKind = getEmitModuleKind; function getSourceFilesToEmit(host, targetSourceFile) { var options = host.getCompilerOptions(); if (options.outFile || options.out) { - var moduleKind = getEmitModuleKind(options); + var moduleKind = ts.getEmitModuleKind(options); var moduleEmitEnabled = moduleKind === ts.ModuleKind.AMD || moduleKind === ts.ModuleKind.System; var sourceFiles = host.getSourceFiles(); return ts.filter(sourceFiles, moduleEmitEnabled ? isNonDeclarationFile : isBundleEmitNonExternalModule); @@ -7525,7 +8648,7 @@ var ts; function isBundleEmitNonExternalModule(sourceFile) { return !isDeclarationFile(sourceFile) && !ts.isExternalModule(sourceFile); } - function forEachTransformedEmitFile(host, sourceFiles, action) { + function forEachTransformedEmitFile(host, sourceFiles, action, emitOnlyDtsFiles) { var options = host.getCompilerOptions(); if (options.outFile || options.out) { onBundledEmit(host, sourceFiles); @@ -7552,7 +8675,7 @@ var ts; } var jsFilePath = getOwnEmitOutputFilePath(sourceFile, host, extension); var sourceMapFilePath = getSourceMapFilePath(jsFilePath, options); - var declarationFilePath = !isSourceFileJavaScript(sourceFile) ? getDeclarationEmitOutputFilePath(sourceFile, host) : undefined; + var declarationFilePath = !isSourceFileJavaScript(sourceFile) && (options.declaration || emitOnlyDtsFiles) ? getDeclarationEmitOutputFilePath(sourceFile, host) : undefined; action(jsFilePath, sourceMapFilePath, declarationFilePath, [sourceFile], false); } function onBundledEmit(host, sourceFiles) { @@ -7568,7 +8691,7 @@ var ts; function getSourceMapFilePath(jsFilePath, options) { return options.sourceMap ? jsFilePath + ".map" : undefined; } - function forEachExpectedEmitFile(host, action, targetSourceFile) { + function forEachExpectedEmitFile(host, action, targetSourceFile, emitOnlyDtsFiles) { var options = host.getCompilerOptions(); if (options.outFile || options.out) { onBundledEmit(host); @@ -7595,18 +8718,19 @@ var ts; } } var jsFilePath = getOwnEmitOutputFilePath(sourceFile, host, extension); + var declarationFilePath = !isSourceFileJavaScript(sourceFile) && (emitOnlyDtsFiles || options.declaration) ? getDeclarationEmitOutputFilePath(sourceFile, host) : undefined; var emitFileNames = { jsFilePath: jsFilePath, sourceMapFilePath: getSourceMapFilePath(jsFilePath, options), - declarationFilePath: !isSourceFileJavaScript(sourceFile) ? getDeclarationEmitOutputFilePath(sourceFile, host) : undefined + declarationFilePath: declarationFilePath }; - action(emitFileNames, [sourceFile], false); + action(emitFileNames, [sourceFile], false, emitOnlyDtsFiles); } function onBundledEmit(host) { var bundledSources = ts.filter(host.getSourceFiles(), function (sourceFile) { return !isDeclarationFile(sourceFile) && !host.isSourceFileFromExternalLibrary(sourceFile) && (!ts.isExternalModule(sourceFile) || - !!getEmitModuleKind(options)); }); + !!ts.getEmitModuleKind(options)); }); if (bundledSources.length) { var jsFilePath = options.outFile || options.out; var emitFileNames = { @@ -7614,7 +8738,7 @@ var ts; sourceMapFilePath: getSourceMapFilePath(jsFilePath, options), declarationFilePath: options.declaration ? ts.removeFileExtension(jsFilePath) + ".d.ts" : undefined }; - action(emitFileNames, bundledSources, true); + action(emitFileNames, bundledSources, true, emitOnlyDtsFiles); } } } @@ -7651,13 +8775,32 @@ var ts; ts.getFirstConstructorWithBody = getFirstConstructorWithBody; function getSetAccessorTypeAnnotationNode(accessor) { if (accessor && accessor.parameters.length > 0) { - var hasThis = accessor.parameters.length === 2 && - accessor.parameters[0].name.kind === 69 && - accessor.parameters[0].name.originalKeywordKind === 97; + var hasThis = accessor.parameters.length === 2 && parameterIsThisKeyword(accessor.parameters[0]); return accessor.parameters[hasThis ? 1 : 0].type; } } ts.getSetAccessorTypeAnnotationNode = getSetAccessorTypeAnnotationNode; + function getThisParameter(signature) { + if (signature.parameters.length) { + var thisParameter = signature.parameters[0]; + if (parameterIsThisKeyword(thisParameter)) { + return thisParameter; + } + } + } + ts.getThisParameter = getThisParameter; + function parameterIsThisKeyword(parameter) { + return isThisIdentifier(parameter.name); + } + ts.parameterIsThisKeyword = parameterIsThisKeyword; + function isThisIdentifier(node) { + return node && node.kind === 69 && identifierIsThisKeyword(node); + } + ts.isThisIdentifier = isThisIdentifier; + function identifierIsThisKeyword(id) { + return id.originalKeywordKind === 97; + } + ts.identifierIsThisKeyword = identifierIsThisKeyword; function getAllAccessorDeclarations(declarations, accessor) { var firstAccessor; var secondAccessor; @@ -7971,14 +9114,6 @@ var ts; return symbol && symbol.valueDeclaration && hasModifier(symbol.valueDeclaration, 512) ? symbol.valueDeclaration.localSymbol : undefined; } ts.getLocalSymbolForExportDefault = getLocalSymbolForExportDefault; - function hasJavaScriptFileExtension(fileName) { - return ts.forEach(ts.supportedJavascriptExtensions, function (extension) { return ts.fileExtensionIs(fileName, extension); }); - } - ts.hasJavaScriptFileExtension = hasJavaScriptFileExtension; - function hasTypeScriptFileExtension(fileName) { - return ts.forEach(ts.supportedTypeScriptExtensions, function (extension) { return ts.fileExtensionIs(fileName, extension); }); - } - ts.hasTypeScriptFileExtension = hasTypeScriptFileExtension; function tryExtractTypeScriptExtension(fileName) { return ts.find(ts.supportedTypescriptExtensionsForExtractExtension, function (extension) { return ts.fileExtensionIs(fileName, extension); }); } @@ -8069,12 +9204,6 @@ var ts; return result; } ts.convertToBase64 = convertToBase64; - function convertToRelativePath(absoluteOrRelativePath, basePath, getCanonicalFileName) { - return !ts.isRootedDiskPath(absoluteOrRelativePath) - ? absoluteOrRelativePath - : ts.getRelativePathToDirectoryOrUrl(basePath, absoluteOrRelativePath, basePath, getCanonicalFileName, false); - } - ts.convertToRelativePath = convertToRelativePath; var carriageReturnLineFeed = "\r\n"; var lineFeed = "\n"; function getNewLineCharacter(options) { @@ -8163,9 +9292,9 @@ var ts; if (syntaxKindCache[kind]) { return syntaxKindCache[kind]; } - for (var name_9 in syntaxKindEnum) { - if (syntaxKindEnum[name_9] === kind) { - return syntaxKindCache[kind] = kind.toString() + " (" + name_9 + ")"; + for (var name_10 in syntaxKindEnum) { + if (syntaxKindEnum[name_10] === kind) { + return syntaxKindCache[kind] = kind.toString() + " (" + name_10 + ")"; } } } @@ -8175,7 +9304,7 @@ var ts; } ts.formatSyntaxKind = formatSyntaxKind; function movePos(pos, value) { - return positionIsSynthesized(pos) ? -1 : pos + value; + return ts.positionIsSynthesized(pos) ? -1 : pos + value; } ts.movePos = movePos; function createRange(pos, end) { @@ -8244,7 +9373,7 @@ var ts; } ts.positionsAreOnSameLine = positionsAreOnSameLine; function getStartPositionOfRange(range, sourceFile) { - return positionIsSynthesized(range.pos) ? -1 : ts.skipTrivia(sourceFile.text, range.pos); + return ts.positionIsSynthesized(range.pos) ? -1 : ts.skipTrivia(sourceFile.text, range.pos); } ts.getStartPositionOfRange = getStartPositionOfRange; function collectExternalModuleInfo(sourceFile, resolver) { @@ -8281,8 +9410,8 @@ var ts; else { for (var _b = 0, _c = node.exportClause.elements; _b < _c.length; _b++) { var specifier = _c[_b]; - var name_10 = (specifier.propertyName || specifier.name).text; - (exportSpecifiers[name_10] || (exportSpecifiers[name_10] = [])).push(specifier); + var name_11 = (specifier.propertyName || specifier.name).text; + (exportSpecifiers[name_11] || (exportSpecifiers[name_11] = [])).push(specifier); } } break; @@ -8344,15 +9473,16 @@ var ts; return 11 <= kind && kind <= 14; } ts.isTemplateLiteralKind = isTemplateLiteralKind; - function isTemplateLiteralFragmentKind(kind) { - return kind === 12 - || kind === 13 - || kind === 14; + function isTemplateHead(node) { + return node.kind === 12; } - function isTemplateLiteralFragment(node) { - return isTemplateLiteralFragmentKind(node.kind); + ts.isTemplateHead = isTemplateHead; + function isTemplateMiddleOrTemplateTail(node) { + var kind = node.kind; + return kind === 13 + || kind === 14; } - ts.isTemplateLiteralFragment = isTemplateLiteralFragment; + ts.isTemplateMiddleOrTemplateTail = isTemplateMiddleOrTemplateTail; function isIdentifier(node) { return node.kind === 69; } @@ -8491,12 +9621,12 @@ var ts; return node.kind === 174; } ts.isCallExpression = isCallExpression; - function isTemplate(node) { + function isTemplateLiteral(node) { var kind = node.kind; return kind === 189 || kind === 11; } - ts.isTemplate = isTemplate; + ts.isTemplateLiteral = isTemplateLiteral; function isSpreadElementExpression(node) { return node.kind === 191; } @@ -8827,7 +9957,6 @@ var ts; } ts.isWatchSet = isWatchSet; })(ts || (ts = {})); -var ts; (function (ts) { function getDefaultLibFileName(options) { return options.target === 2 ? "lib.es6.d.ts" : "lib.d.ts"; @@ -9062,7 +10191,7 @@ var ts; ts.createSynthesizedNodeArray = createSynthesizedNodeArray; function getSynthesizedClone(node) { var clone = createNode(node.kind, undefined, node.flags); - clone.original = node; + setOriginalNode(clone, node); for (var key in node) { if (clone.hasOwnProperty(key) || !node.hasOwnProperty(key)) { continue; @@ -9094,7 +10223,7 @@ var ts; node.text = value; return node; } - else { + else if (value) { var node = createNode(9, location, undefined); node.textSourceNode = value; node.text = value.text; @@ -9381,7 +10510,7 @@ var ts; function createPropertyAccess(expression, name, location, flags) { var node = createNode(172, location, flags); node.expression = parenthesizeForAccess(expression); - node.emitFlags = 1048576; + (node.emitNode || (node.emitNode = {})).flags |= 1048576; node.name = typeof name === "string" ? createIdentifier(name) : name; return node; } @@ -9389,7 +10518,7 @@ var ts; function updatePropertyAccess(node, expression, name) { if (node.expression !== expression || node.name !== name) { var propertyAccess = createPropertyAccess(expression, name, node, node.flags); - propertyAccess.emitFlags = node.emitFlags; + (propertyAccess.emitNode || (propertyAccess.emitNode = {})).flags = getEmitFlags(node); return updateNode(propertyAccess, node); } return node; @@ -9493,7 +10622,7 @@ var ts; node.typeParameters = typeParameters ? createNodeArray(typeParameters) : undefined; node.parameters = createNodeArray(parameters); node.type = type; - node.equalsGreaterThanToken = equalsGreaterThanToken || createNode(34); + node.equalsGreaterThanToken = equalsGreaterThanToken || createToken(34); node.body = parenthesizeConciseBody(body); return node; } @@ -9586,7 +10715,7 @@ var ts; } ts.updatePostfix = updatePostfix; function createBinary(left, operator, right, location) { - var operatorToken = typeof operator === "number" ? createSynthesizedNode(operator) : operator; + var operatorToken = typeof operator === "number" ? createToken(operator) : operator; var operatorKind = operatorToken.kind; var node = createNode(187, location); node.left = parenthesizeBinaryOperand(operatorKind, left, true, undefined); @@ -10486,13 +11615,13 @@ var ts; } else { var expression = ts.isIdentifier(memberName) ? createPropertyAccess(target, memberName, location) : createElementAccess(target, memberName, location); - expression.emitFlags |= 2048; + (expression.emitNode || (expression.emitNode = {})).flags |= 2048; return expression; } } ts.createMemberAccessForPropertyName = createMemberAccessForPropertyName; function createRestParameter(name) { - return createParameterDeclaration(undefined, undefined, createSynthesizedNode(22), name, undefined, undefined, undefined); + return createParameterDeclaration(undefined, undefined, createToken(22), name, undefined, undefined, undefined); } ts.createRestParameter = createRestParameter; function createFunctionCall(func, thisArg, argumentsList, location) { @@ -10606,8 +11735,8 @@ var ts; } ts.createDecorateHelper = createDecorateHelper; function createAwaiterHelper(externalHelpersModuleName, hasLexicalArguments, promiseConstructor, body) { - var generatorFunc = createFunctionExpression(createNode(37), undefined, undefined, [], undefined, body); - generatorFunc.emitFlags |= 2097152; + var generatorFunc = createFunctionExpression(createToken(37), undefined, undefined, [], undefined, body); + (generatorFunc.emitNode || (generatorFunc.emitNode = {})).flags |= 2097152; return createCall(createHelperName(externalHelpersModuleName, "__awaiter"), undefined, [ createThis(), hasLexicalArguments ? createIdentifier("arguments") : createVoidZero(), @@ -10834,7 +11963,7 @@ var ts; target.push(startOnNewLine(createStatement(createLiteral("use strict")))); foundUseStrict = true; } - if (statement.emitFlags & 8388608) { + if (getEmitFlags(statement) & 8388608) { target.push(visitor ? ts.visitNode(statement, visitor, ts.isStatement) : statement); } else { @@ -10846,6 +11975,28 @@ var ts; return statementOffset; } ts.addPrologueDirectives = addPrologueDirectives; + function ensureUseStrict(node) { + var foundUseStrict = false; + for (var _i = 0, _a = node.statements; _i < _a.length; _i++) { + var statement = _a[_i]; + if (ts.isPrologueDirective(statement)) { + if (isUseStrictPrologue(statement)) { + foundUseStrict = true; + break; + } + } + else { + break; + } + } + if (!foundUseStrict) { + var statements = []; + statements.push(startOnNewLine(createStatement(createLiteral("use strict")))); + return updateSourceFileNode(node, statements.concat(node.statements)); + } + return node; + } + ts.ensureUseStrict = ensureUseStrict; function parenthesizeBinaryOperand(binaryOperator, operand, isLeftSideOfBinary, leftOperand) { var skipped = skipPartiallyEmittedExpressions(operand); if (skipped.kind === 178) { @@ -11085,17 +12236,112 @@ var ts; function setOriginalNode(node, original) { node.original = original; if (original) { - var emitFlags = original.emitFlags, commentRange = original.commentRange, sourceMapRange = original.sourceMapRange; - if (emitFlags) - node.emitFlags = emitFlags; - if (commentRange) - node.commentRange = commentRange; - if (sourceMapRange) - node.sourceMapRange = sourceMapRange; + var emitNode = original.emitNode; + if (emitNode) + node.emitNode = mergeEmitNode(emitNode, node.emitNode); } return node; } ts.setOriginalNode = setOriginalNode; + function mergeEmitNode(sourceEmitNode, destEmitNode) { + var flags = sourceEmitNode.flags, commentRange = sourceEmitNode.commentRange, sourceMapRange = sourceEmitNode.sourceMapRange, tokenSourceMapRanges = sourceEmitNode.tokenSourceMapRanges; + if (!destEmitNode && (flags || commentRange || sourceMapRange || tokenSourceMapRanges)) + destEmitNode = {}; + if (flags) + destEmitNode.flags = flags; + if (commentRange) + destEmitNode.commentRange = commentRange; + if (sourceMapRange) + destEmitNode.sourceMapRange = sourceMapRange; + if (tokenSourceMapRanges) + destEmitNode.tokenSourceMapRanges = mergeTokenSourceMapRanges(tokenSourceMapRanges, destEmitNode.tokenSourceMapRanges); + return destEmitNode; + } + function mergeTokenSourceMapRanges(sourceRanges, destRanges) { + if (!destRanges) + destRanges = ts.createMap(); + ts.copyProperties(sourceRanges, destRanges); + return destRanges; + } + function disposeEmitNodes(sourceFile) { + sourceFile = ts.getSourceFileOfNode(ts.getParseTreeNode(sourceFile)); + var emitNode = sourceFile && sourceFile.emitNode; + var annotatedNodes = emitNode && emitNode.annotatedNodes; + if (annotatedNodes) { + for (var _i = 0, annotatedNodes_1 = annotatedNodes; _i < annotatedNodes_1.length; _i++) { + var node = annotatedNodes_1[_i]; + node.emitNode = undefined; + } + } + } + ts.disposeEmitNodes = disposeEmitNodes; + function getOrCreateEmitNode(node) { + if (!node.emitNode) { + if (ts.isParseTreeNode(node)) { + if (node.kind === 256) { + return node.emitNode = { annotatedNodes: [node] }; + } + var sourceFile = ts.getSourceFileOfNode(node); + getOrCreateEmitNode(sourceFile).annotatedNodes.push(node); + } + node.emitNode = {}; + } + return node.emitNode; + } + function getEmitFlags(node) { + var emitNode = node.emitNode; + return emitNode && emitNode.flags; + } + ts.getEmitFlags = getEmitFlags; + function setEmitFlags(node, emitFlags) { + getOrCreateEmitNode(node).flags = emitFlags; + return node; + } + ts.setEmitFlags = setEmitFlags; + function setSourceMapRange(node, range) { + getOrCreateEmitNode(node).sourceMapRange = range; + return node; + } + ts.setSourceMapRange = setSourceMapRange; + function setTokenSourceMapRange(node, token, range) { + var emitNode = getOrCreateEmitNode(node); + var tokenSourceMapRanges = emitNode.tokenSourceMapRanges || (emitNode.tokenSourceMapRanges = ts.createMap()); + tokenSourceMapRanges[token] = range; + return node; + } + ts.setTokenSourceMapRange = setTokenSourceMapRange; + function setCommentRange(node, range) { + getOrCreateEmitNode(node).commentRange = range; + return node; + } + ts.setCommentRange = setCommentRange; + function getCommentRange(node) { + var emitNode = node.emitNode; + return (emitNode && emitNode.commentRange) || node; + } + ts.getCommentRange = getCommentRange; + function getSourceMapRange(node) { + var emitNode = node.emitNode; + return (emitNode && emitNode.sourceMapRange) || node; + } + ts.getSourceMapRange = getSourceMapRange; + function getTokenSourceMapRange(node, token) { + var emitNode = node.emitNode; + var tokenSourceMapRanges = emitNode && emitNode.tokenSourceMapRanges; + return tokenSourceMapRanges && tokenSourceMapRanges[token]; + } + ts.getTokenSourceMapRange = getTokenSourceMapRange; + function getConstantValue(node) { + var emitNode = node.emitNode; + return emitNode && emitNode.constantValue; + } + ts.getConstantValue = getConstantValue; + function setConstantValue(node, value) { + var emitNode = getOrCreateEmitNode(node); + emitNode.constantValue = value; + return node; + } + ts.setConstantValue = setConstantValue; function setTextRange(node, location) { if (location) { node.pos = location.pos; @@ -11122,8 +12368,8 @@ var ts; function getLocalNameForExternalImport(node, sourceFile) { var namespaceDeclaration = ts.getNamespaceDeclarationNode(node); if (namespaceDeclaration && !ts.isDefaultImport(node)) { - var name_11 = namespaceDeclaration.name; - return ts.isGeneratedIdentifier(name_11) ? name_11 : createIdentifier(ts.getSourceTextOfNodeFromSourceFile(sourceFile, namespaceDeclaration.name)); + var name_12 = namespaceDeclaration.name; + return ts.isGeneratedIdentifier(name_12) ? name_12 : createIdentifier(ts.getSourceTextOfNodeFromSourceFile(sourceFile, namespaceDeclaration.name)); } if (node.kind === 230 && node.importClause) { return getGeneratedNameForNode(node); @@ -12205,7 +13451,7 @@ var ts; case 16: return token() === 18 || token() === 20; case 18: - return token() === 27 || token() === 17; + return token() !== 24; case 20: return token() === 15 || token() === 16; case 13: @@ -12533,7 +13779,7 @@ var ts; } function parseTemplateExpression() { var template = createNode(189); - template.head = parseTemplateLiteralFragment(); + template.head = parseTemplateHead(); ts.Debug.assert(template.head.kind === 12, "Template head has wrong token kind"); var templateSpans = createNodeArray(); do { @@ -12549,7 +13795,7 @@ var ts; var literal; if (token() === 16) { reScanTemplateToken(); - literal = parseTemplateLiteralFragment(); + literal = parseTemplateMiddleOrTemplateTail(); } else { literal = parseExpectedToken(14, false, ts.Diagnostics._0_expected, ts.tokenToString(16)); @@ -12560,8 +13806,15 @@ var ts; function parseLiteralNode(internName) { return parseLiteralLikeNode(token(), internName); } - function parseTemplateLiteralFragment() { - return parseLiteralLikeNode(token(), false); + function parseTemplateHead() { + var fragment = parseLiteralLikeNode(token(), false); + ts.Debug.assert(fragment.kind === 12, "Template head has wrong token kind"); + return fragment; + } + function parseTemplateMiddleOrTemplateTail() { + var fragment = parseLiteralLikeNode(token(), false); + ts.Debug.assert(fragment.kind === 13 || fragment.kind === 14, "Template fragment has wrong token kind"); + return fragment; } function parseLiteralLikeNode(kind, internName) { var node = createNode(kind); @@ -14822,8 +16075,8 @@ var ts; return parsePropertyOrMethodDeclaration(fullStart, decorators, modifiers); } if (decorators || modifiers) { - var name_12 = createMissingNode(69, true, ts.Diagnostics.Declaration_expected); - return parsePropertyDeclaration(fullStart, decorators, modifiers, name_12, undefined); + var name_13 = createMissingNode(69, true, ts.Diagnostics.Declaration_expected); + return parsePropertyDeclaration(fullStart, decorators, modifiers, name_13, undefined); } ts.Debug.fail("Should not have attempted to parse class member declaration."); } @@ -14909,7 +16162,7 @@ var ts; parseExpected(56); node.type = parseType(); parseSemicolon(); - return finishNode(node); + return addJSDocComment(finishNode(node)); } function parseEnumMember() { var node = createNode(255, scanner.getStartPos()); @@ -15847,8 +17100,8 @@ var ts; if (typeExpression.type.kind === 267) { var jsDocTypeReference = typeExpression.type; if (jsDocTypeReference.name.kind === 69) { - var name_13 = jsDocTypeReference.name; - if (name_13.text === "Object") { + var name_14 = jsDocTypeReference.name; + if (name_14.text === "Object") { typedefTag.jsDocTypeLiteral = scanChildTags(); } } @@ -15934,14 +17187,14 @@ var ts; } var typeParameters = createNodeArray(); while (true) { - var name_14 = parseJSDocIdentifierName(); + var name_15 = parseJSDocIdentifierName(); skipWhitespace(); - if (!name_14) { + if (!name_15) { parseErrorAtPosition(scanner.getStartPos(), 0, ts.Diagnostics.Identifier_expected); return undefined; } - var typeParameter = createNode(141, name_14.pos); - typeParameter.name = name_14; + var typeParameter = createNode(141, name_15.pos); + typeParameter.name = name_15; finishNode(typeParameter); typeParameters.push(typeParameter); if (token() === 24) { @@ -16344,7 +17597,7 @@ var ts; file = f; options = opts; languageVersion = ts.getEmitScriptTarget(options); - inStrictMode = !!file.externalModuleIndicator; + inStrictMode = bindInStrictMode(file, opts); classifiableNames = ts.createMap(); symbolCount = 0; skipTransformFlagAggregation = ts.isDeclarationFile(file); @@ -16374,6 +17627,14 @@ var ts; subtreeTransformFlags = 0; } return bindSourceFile; + function bindInStrictMode(file, opts) { + if (opts.alwaysStrict && !ts.isDeclarationFile(file)) { + return true; + } + else { + return !!file.externalModuleIndicator; + } + } function createSymbol(flags, name) { symbolCount++; return new Symbol(flags, name); @@ -16492,11 +17753,17 @@ var ts; var message_1 = symbol.flags & 2 ? ts.Diagnostics.Cannot_redeclare_block_scoped_variable_0 : ts.Diagnostics.Duplicate_identifier_0; - ts.forEach(symbol.declarations, function (declaration) { - if (ts.hasModifier(declaration, 512)) { + if (symbol.declarations && symbol.declarations.length) { + if (isDefaultExport) { message_1 = ts.Diagnostics.A_module_cannot_have_multiple_default_exports; } - }); + else { + if (symbol.declarations && symbol.declarations.length && + (isDefaultExport || (node.kind === 235 && !node.isExportEquals))) { + message_1 = ts.Diagnostics.A_module_cannot_have_multiple_default_exports; + } + } + } ts.forEach(symbol.declarations, function (declaration) { file.bindDiagnostics.push(ts.createDiagnosticForNode(declaration.name || declaration, message_1, getDisplayName(declaration))); }); @@ -16561,7 +17828,7 @@ var ts; } else { currentFlow = { flags: 2 }; - if (containerFlags & 16) { + if (containerFlags & (16 | 128)) { currentFlow.container = node; } currentReturnTarget = undefined; @@ -17016,7 +18283,9 @@ var ts; currentFlow = preTryFlow; bind(node.finallyBlock); } - currentFlow = finishFlowLabel(postFinallyLabel); + if (!node.finallyBlock || !(currentFlow.flags & 1)) { + currentFlow = finishFlowLabel(postFinallyLabel); + } } function bindSwitchStatement(node) { var postSwitchLabel = createBranchLabel(); @@ -17149,7 +18418,7 @@ var ts; } else { ts.forEachChild(node, bind); - if (node.operator === 57 || node.operator === 42) { + if (node.operator === 41 || node.operator === 42) { bindAssignmentTargetFlow(node.operand); } } @@ -17248,9 +18517,12 @@ var ts; return 1 | 32; case 256: return 1 | 4 | 32; + case 147: + if (ts.isObjectLiteralOrClassExpressionMethod(node)) { + return 1 | 4 | 32 | 8 | 128; + } case 148: case 220: - case 147: case 146: case 149: case 150: @@ -17782,7 +19054,7 @@ var ts; var flags = node.kind === 235 && ts.exportAssignmentIsAlias(node) ? 8388608 : 4; - declareSymbol(container.symbol.exports, container.symbol, node, flags, 0 | 8388608); + declareSymbol(container.symbol.exports, container.symbol, node, flags, 4 | 8388608 | 32 | 16); } } function bindNamespaceExportDeclaration(node) { @@ -17794,12 +19066,12 @@ var ts; return; } else { - var parent_6 = node.parent; - if (!ts.isExternalModule(parent_6)) { + var parent_7 = node.parent; + if (!ts.isExternalModule(parent_7)) { file.bindDiagnostics.push(ts.createDiagnosticForNode(node, ts.Diagnostics.Global_module_exports_may_only_appear_in_module_files)); return; } - if (!parent_6.isDeclarationFile) { + if (!parent_7.isDeclarationFile) { file.bindDiagnostics.push(ts.createDiagnosticForNode(node, ts.Diagnostics.Global_module_exports_may_only_appear_in_declaration_files)); return; } @@ -17979,6 +19251,9 @@ var ts; emitFlags |= 2048; } } + if (currentFlow && ts.isObjectLiteralOrClassExpressionMethod(node)) { + node.flowNode = currentFlow; + } return ts.hasDynamicName(node) ? bindAnonymousDeclaration(node, symbolFlags, "__computed") : declareSymbolAndAddToSymbolTable(node, symbolFlags, symbolExcludes); @@ -18117,8 +19392,7 @@ var ts; if (node.questionToken) { transformFlags |= 3; } - if (subtreeFlags & 2048 - || (name && ts.isIdentifier(name) && name.originalKeywordKind === 97)) { + if (subtreeFlags & 2048 || ts.isThisIdentifier(name)) { transformFlags |= 3; } if (modifierFlags & 92) { @@ -18220,7 +19494,7 @@ var ts; || (subtreeFlags & 2048)) { transformFlags |= 3; } - if (asteriskToken && node.emitFlags & 2097152) { + if (asteriskToken && ts.getEmitFlags(node) & 2097152) { transformFlags |= 1536; } node.transformFlags = transformFlags | 536870912; @@ -18265,7 +19539,7 @@ var ts; if (subtreeFlags & 81920) { transformFlags |= 192; } - if (asteriskToken && node.emitFlags & 2097152) { + if (asteriskToken && ts.getEmitFlags(node) & 2097152) { transformFlags |= 1536; } } @@ -18282,7 +19556,7 @@ var ts; if (subtreeFlags & 81920) { transformFlags |= 192; } - if (asteriskToken && node.emitFlags & 2097152) { + if (asteriskToken && ts.getEmitFlags(node) & 2097152) { transformFlags |= 1536; } node.transformFlags = transformFlags | 536870912; @@ -18618,6 +19892,7 @@ var ts; var unknownSymbol = createSymbol(4 | 67108864, "unknown"); var resolvingSymbol = createSymbol(67108864, "__resolving__"); var anyType = createIntrinsicType(1, "any"); + var autoType = createIntrinsicType(1, "any"); var unknownType = createIntrinsicType(1, "unknown"); var undefinedType = createIntrinsicType(2048, "undefined"); var undefinedWideningType = strictNullChecks ? undefinedType : createIntrinsicType(2048 | 33554432, "undefined"); @@ -19175,7 +20450,7 @@ var ts; if (result && isInExternalModule && (meaning & 107455) === 107455) { var decls = result.declarations; if (decls && decls.length === 1 && decls[0].kind === 228) { - error(errorLocation, ts.Diagnostics.Identifier_0_must_be_imported_from_a_module, name); + error(errorLocation, ts.Diagnostics._0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead, name); } } } @@ -19335,28 +20610,28 @@ var ts; var moduleSymbol = resolveExternalModuleName(node, node.moduleSpecifier); var targetSymbol = resolveESModuleSymbol(moduleSymbol, node.moduleSpecifier); if (targetSymbol) { - var name_15 = specifier.propertyName || specifier.name; - if (name_15.text) { + var name_16 = specifier.propertyName || specifier.name; + if (name_16.text) { if (ts.isShorthandAmbientModuleSymbol(moduleSymbol)) { return moduleSymbol; } var symbolFromVariable = void 0; if (moduleSymbol && moduleSymbol.exports && moduleSymbol.exports["export="]) { - symbolFromVariable = getPropertyOfType(getTypeOfSymbol(targetSymbol), name_15.text); + symbolFromVariable = getPropertyOfType(getTypeOfSymbol(targetSymbol), name_16.text); } else { - symbolFromVariable = getPropertyOfVariable(targetSymbol, name_15.text); + symbolFromVariable = getPropertyOfVariable(targetSymbol, name_16.text); } symbolFromVariable = resolveSymbol(symbolFromVariable); - var symbolFromModule = getExportOfModule(targetSymbol, name_15.text); - if (!symbolFromModule && allowSyntheticDefaultImports && name_15.text === "default") { + var symbolFromModule = getExportOfModule(targetSymbol, name_16.text); + if (!symbolFromModule && allowSyntheticDefaultImports && name_16.text === "default") { symbolFromModule = resolveExternalModuleSymbol(moduleSymbol) || resolveSymbol(moduleSymbol); } var symbol = symbolFromModule && symbolFromVariable ? combineValueAndTypeSymbols(symbolFromVariable, symbolFromModule) : symbolFromModule || symbolFromVariable; if (!symbol) { - error(name_15, ts.Diagnostics.Module_0_has_no_exported_member_1, getFullyQualifiedName(moduleSymbol), ts.declarationNameToString(name_15)); + error(name_16, ts.Diagnostics.Module_0_has_no_exported_member_1, getFullyQualifiedName(moduleSymbol), ts.declarationNameToString(name_16)); } return symbol; } @@ -19593,6 +20868,7 @@ var ts; } function getExportsForModule(moduleSymbol) { var visitedSymbols = []; + moduleSymbol = resolveExternalModuleSymbol(moduleSymbol); return visit(moduleSymbol) || moduleSymbol.exports; function visit(symbol) { if (!(symbol && symbol.flags & 1952 && !ts.contains(visitedSymbols, symbol))) { @@ -20077,9 +21353,9 @@ var ts; var accessibleSymbolChain = getAccessibleSymbolChain(symbol, enclosingDeclaration, meaning, !!(flags & 2)); if (!accessibleSymbolChain || needsQualification(accessibleSymbolChain[0], enclosingDeclaration, accessibleSymbolChain.length === 1 ? meaning : getQualifiedLeftMeaning(meaning))) { - var parent_7 = getParentOfSymbol(accessibleSymbolChain ? accessibleSymbolChain[0] : symbol); - if (parent_7) { - walkSymbol(parent_7, getQualifiedLeftMeaning(meaning), false); + var parent_8 = getParentOfSymbol(accessibleSymbolChain ? accessibleSymbolChain[0] : symbol); + if (parent_8) { + walkSymbol(parent_8, getQualifiedLeftMeaning(meaning), false); } } if (accessibleSymbolChain) { @@ -20114,7 +21390,7 @@ var ts; ? "any" : type.intrinsicName); } - else if (type.flags & 268435456) { + else if (type.flags & 16384 && type.isThisType) { if (inObjectTypeLiteral) { writer.reportInaccessibleThisError(); } @@ -20131,7 +21407,7 @@ var ts; else if (type.flags & (32768 | 65536 | 16 | 16384)) { buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, 793064, 0, nextFlags); } - else if (!(flags & 512) && type.flags & (2097152 | 1572864) && type.aliasSymbol && + else if (!(flags & 512) && ((type.flags & 2097152 && !type.target) || type.flags & 1572864) && type.aliasSymbol && isSymbolAccessible(type.aliasSymbol, enclosingDeclaration, 793064, false).accessibility === 0) { var typeArguments = type.aliasTypeArguments; writeSymbolTypeReference(type.aliasSymbol, typeArguments, 0, typeArguments ? typeArguments.length : 0, nextFlags); @@ -20201,12 +21477,12 @@ var ts; var length_1 = outerTypeParameters.length; while (i < length_1) { var start = i; - var parent_8 = getParentSymbolOfTypeParameter(outerTypeParameters[i]); + var parent_9 = getParentSymbolOfTypeParameter(outerTypeParameters[i]); do { i++; - } while (i < length_1 && getParentSymbolOfTypeParameter(outerTypeParameters[i]) === parent_8); + } while (i < length_1 && getParentSymbolOfTypeParameter(outerTypeParameters[i]) === parent_9); if (!ts.rangeEquals(outerTypeParameters, typeArguments, start, i)) { - writeSymbolTypeReference(parent_8, typeArguments, start, i, flags); + writeSymbolTypeReference(parent_9, typeArguments, start, i, flags); writePunctuation(writer, 21); } } @@ -20595,12 +21871,12 @@ var ts; if (ts.isExternalModuleAugmentation(node)) { return true; } - var parent_9 = getDeclarationContainer(node); + var parent_10 = getDeclarationContainer(node); if (!(ts.getCombinedModifierFlags(node) & 1) && - !(node.kind !== 229 && parent_9.kind !== 256 && ts.isInAmbientContext(parent_9))) { - return isGlobalSourceFile(parent_9); + !(node.kind !== 229 && parent_10.kind !== 256 && ts.isInAmbientContext(parent_10))) { + return isGlobalSourceFile(parent_10); } - return isDeclarationVisible(parent_9); + return isDeclarationVisible(parent_10); case 145: case 144: case 149: @@ -20787,19 +22063,19 @@ var ts; } var type; if (pattern.kind === 167) { - var name_16 = declaration.propertyName || declaration.name; - if (isComputedNonLiteralName(name_16)) { + var name_17 = declaration.propertyName || declaration.name; + if (isComputedNonLiteralName(name_17)) { return anyType; } if (declaration.initializer) { getContextualType(declaration.initializer); } - var text = getTextOfPropertyName(name_16); + var text = getTextOfPropertyName(name_17); type = getTypeOfPropertyOfType(parentType, text) || isNumericLiteralName(text) && getIndexTypeOfType(parentType, 1) || getIndexTypeOfType(parentType, 0); if (!type) { - error(name_16, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(parentType), ts.declarationNameToString(name_16)); + error(name_17, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(parentType), ts.declarationNameToString(name_17)); return unknownType; } } @@ -20858,6 +22134,10 @@ var ts; } return undefined; } + function isAutoVariableInitializer(initializer) { + var expr = initializer && ts.skipParentheses(initializer); + return !expr || expr.kind === 93 || expr.kind === 69 && getResolvedSymbol(expr) === undefinedSymbol; + } function addOptionality(type, optional) { return strictNullChecks && optional ? includeFalsyTypes(type, 2048) : type; } @@ -20880,6 +22160,11 @@ var ts; if (declaration.type) { return addOptionality(getTypeFromTypeNode(declaration.type), declaration.questionToken && includeOptionality); } + if (declaration.kind === 218 && !ts.isBindingPattern(declaration.name) && + !(ts.getCombinedNodeFlags(declaration) & 2) && !(ts.getCombinedModifierFlags(declaration) & 1) && + !ts.isInAmbientContext(declaration) && isAutoVariableInitializer(declaration.initializer)) { + return autoType; + } if (declaration.kind === 142) { var func = declaration.parent; if (func.kind === 150 && !ts.hasDynamicName(func)) { @@ -20951,7 +22236,7 @@ var ts; result.pattern = pattern; } if (hasComputedProperties) { - result.flags |= 536870912; + result.isObjectLiteralPatternWithComputedProperties = true; } return result; } @@ -21420,7 +22705,8 @@ var ts; type.instantiations[getTypeListId(type.typeParameters)] = type; type.target = type; type.typeArguments = type.typeParameters; - type.thisType = createType(16384 | 268435456); + type.thisType = createType(16384); + type.thisType.isThisType = true; type.thisType.symbol = symbol; type.thisType.constraint = type; } @@ -21953,13 +23239,24 @@ var ts; var current = _a[_i]; for (var _b = 0, _c = getPropertiesOfType(current); _b < _c.length; _b++) { var prop = _c[_b]; - getPropertyOfUnionOrIntersectionType(type, prop.name); + getUnionOrIntersectionProperty(type, prop.name); } if (type.flags & 524288) { break; } } - return type.resolvedProperties ? symbolsToArray(type.resolvedProperties) : emptyArray; + var props = type.resolvedProperties; + if (props) { + var result = []; + for (var key in props) { + var prop = props[key]; + if (!(prop.flags & 268435456 && prop.isPartial)) { + result.push(prop); + } + } + return result; + } + return emptyArray; } function getPropertiesOfType(type) { type = getApparentType(type); @@ -21998,6 +23295,7 @@ var ts; var props; var commonFlags = (containingType.flags & 1048576) ? 536870912 : 0; var isReadonly = false; + var isPartial = false; for (var _i = 0, types_2 = types; _i < types_2.length; _i++) { var current = types_2[_i]; var type = getApparentType(current); @@ -22016,20 +23314,20 @@ var ts; } } else if (containingType.flags & 524288) { - return undefined; + isPartial = true; } } } if (!props) { return undefined; } - if (props.length === 1) { + if (props.length === 1 && !isPartial) { return props[0]; } var propTypes = []; var declarations = []; var commonType = undefined; - var hasCommonType = true; + var hasNonUniformType = false; for (var _a = 0, props_1 = props; _a < props_1.length; _a++) { var prop = props_1[_a]; if (prop.declarations) { @@ -22040,22 +23338,20 @@ var ts; commonType = type; } else if (type !== commonType) { - hasCommonType = false; + hasNonUniformType = true; } - propTypes.push(getTypeOfSymbol(prop)); + propTypes.push(type); } - var result = createSymbol(4 | - 67108864 | - 268435456 | - commonFlags, name); + var result = createSymbol(4 | 67108864 | 268435456 | commonFlags, name); result.containingType = containingType; - result.hasCommonType = hasCommonType; + result.hasNonUniformType = hasNonUniformType; + result.isPartial = isPartial; result.declarations = declarations; result.isReadonly = isReadonly; result.type = containingType.flags & 524288 ? getUnionType(propTypes) : getIntersectionType(propTypes); return result; } - function getPropertyOfUnionOrIntersectionType(type, name) { + function getUnionOrIntersectionProperty(type, name) { var properties = type.resolvedProperties || (type.resolvedProperties = ts.createMap()); var property = properties[name]; if (!property) { @@ -22066,6 +23362,10 @@ var ts; } return property; } + function getPropertyOfUnionOrIntersectionType(type, name) { + var property = getUnionOrIntersectionProperty(type, name); + return property && !(property.flags & 268435456 && property.isPartial) ? property : undefined; + } function getPropertyOfType(type, name) { type = getApparentType(type); if (type.flags & 2588672) { @@ -22438,7 +23738,7 @@ var ts; } function hasConstraintReferenceTo(type, target) { var checked; - while (type && !(type.flags & 268435456) && type.flags & 16384 && !ts.contains(checked, type)) { + while (type && type.flags & 16384 && !(type.isThisType) && !ts.contains(checked, type)) { if (type === target) { return true; } @@ -22722,7 +24022,8 @@ var ts; type.instantiations[getTypeListId(type.typeParameters)] = type; type.target = type; type.typeArguments = type.typeParameters; - type.thisType = createType(16384 | 268435456); + type.thisType = createType(16384); + type.thisType.isThisType = true; type.thisType.constraint = type; type.declaredProperties = properties; type.declaredCallSignatures = emptyArray; @@ -22821,7 +24122,24 @@ var ts; } return false; } + function isSetOfLiteralsFromSameEnum(types) { + var first = types[0]; + if (first.flags & 256) { + var firstEnum = getParentOfSymbol(first.symbol); + for (var i = 1; i < types.length; i++) { + var other = types[i]; + if (!(other.flags & 256) || (firstEnum !== getParentOfSymbol(other.symbol))) { + return false; + } + } + return true; + } + return false; + } function removeSubtypes(types) { + if (types.length === 0 || isSetOfLiteralsFromSameEnum(types)) { + return; + } var i = types.length; while (i > 0) { i--; @@ -23796,7 +25114,8 @@ var ts; !t.numberIndexInfo; } function hasExcessProperties(source, target, reportErrors) { - if (!(target.flags & 536870912) && maybeTypeOfKind(target, 2588672)) { + if (maybeTypeOfKind(target, 2588672) && + (!(target.flags & 2588672) || !target.isObjectLiteralPatternWithComputedProperties)) { for (var _i = 0, _a = getPropertiesOfObjectType(source); _i < _a.length; _i++) { var prop = _a[_i]; if (!isKnownProperty(target, prop.name)) { @@ -25001,17 +26320,10 @@ var ts; } function isDiscriminantProperty(type, name) { if (type && type.flags & 524288) { - var prop = getPropertyOfType(type, name); - if (!prop) { - var filteredType = getTypeWithFacts(type, 4194304); - if (filteredType !== type && filteredType.flags & 524288) { - prop = getPropertyOfType(filteredType, name); - } - } + var prop = getUnionOrIntersectionProperty(type, name); if (prop && prop.flags & 268435456) { if (prop.isDiscriminantProperty === undefined) { - prop.isDiscriminantProperty = !prop.hasCommonType && - isLiteralType(getTypeOfSymbol(prop)); + prop.isDiscriminantProperty = prop.hasNonUniformType && isLiteralType(getTypeOfSymbol(prop)); } return prop.isDiscriminantProperty; } @@ -25288,6 +26600,23 @@ var ts; } return f(type) ? type : neverType; } + function mapType(type, f) { + return type.flags & 524288 ? getUnionType(ts.map(type.types, f)) : f(type); + } + function extractTypesOfKind(type, kind) { + return filterType(type, function (t) { return (t.flags & kind) !== 0; }); + } + function replacePrimitivesWithLiterals(typeWithPrimitives, typeWithLiterals) { + if (isTypeSubsetOf(stringType, typeWithPrimitives) && maybeTypeOfKind(typeWithLiterals, 32) || + isTypeSubsetOf(numberType, typeWithPrimitives) && maybeTypeOfKind(typeWithLiterals, 64)) { + return mapType(typeWithPrimitives, function (t) { + return t.flags & 2 ? extractTypesOfKind(typeWithLiterals, 2 | 32) : + t.flags & 4 ? extractTypesOfKind(typeWithLiterals, 4 | 64) : + t; + }); + } + return typeWithPrimitives; + } function isIncomplete(flowType) { return flowType.flags === 0; } @@ -25302,7 +26631,9 @@ var ts; if (!reference.flowNode || assumeInitialized && !(declaredType.flags & 4178943)) { return declaredType; } - var initialType = assumeInitialized ? declaredType : includeFalsyTypes(declaredType, 2048); + var initialType = assumeInitialized ? declaredType : + declaredType === autoType ? undefinedType : + includeFalsyTypes(declaredType, 2048); var visitedFlowStart = visitedFlowCount; var result = getTypeFromFlowType(getTypeAtFlowNode(reference.flowNode)); visitedFlowCount = visitedFlowStart; @@ -25364,10 +26695,13 @@ var ts; function getTypeAtFlowAssignment(flow) { var node = flow.node; if (isMatchingReference(reference, node)) { - var isIncrementOrDecrement = node.parent.kind === 185 || node.parent.kind === 186; - return declaredType.flags & 524288 && !isIncrementOrDecrement ? - getAssignmentReducedType(declaredType, getInitialOrAssignedType(node)) : - declaredType; + if (node.parent.kind === 185 || node.parent.kind === 186) { + var flowType = getTypeAtFlowNode(flow.antecedent); + return createFlowType(getBaseTypeOfLiteralType(getTypeFromFlowType(flowType)), isIncomplete(flowType)); + } + return declaredType === autoType ? getBaseTypeOfLiteralType(getInitialOrAssignedType(node)) : + declaredType.flags & 524288 ? getAssignmentReducedType(declaredType, getInitialOrAssignedType(node)) : + declaredType; } if (containsMatchingReference(reference, node)) { return declaredType; @@ -25553,12 +26887,12 @@ var ts; assumeTrue ? 16384 : 131072; return getTypeWithFacts(type, facts); } - if (type.flags & 2589191) { + if (type.flags & 2589185) { return type; } if (assumeTrue) { var narrowedType = filterType(type, function (t) { return areTypesComparable(t, valueType); }); - return narrowedType.flags & 8192 ? type : narrowedType; + return narrowedType.flags & 8192 ? type : replacePrimitivesWithLiterals(narrowedType, valueType); } if (isUnitType(valueType)) { var regularType_1 = getRegularTypeOfLiteralType(valueType); @@ -25596,7 +26930,8 @@ var ts; var clauseTypes = switchTypes.slice(clauseStart, clauseEnd); var hasDefaultClause = clauseStart === clauseEnd || ts.contains(clauseTypes, neverType); var discriminantType = getUnionType(clauseTypes); - var caseType = discriminantType.flags & 8192 ? neverType : filterType(type, function (t) { return isTypeComparableTo(discriminantType, t); }); + var caseType = discriminantType.flags & 8192 ? neverType : + replacePrimitivesWithLiterals(filterType(type, function (t) { return isTypeComparableTo(discriminantType, t); }), discriminantType); if (!hasDefaultClause) { return caseType; } @@ -25723,7 +27058,7 @@ var ts; if (ts.isRightSideOfQualifiedNameOrPropertyAccess(location)) { location = location.parent; } - if (ts.isExpression(location) && !ts.isAssignmentTarget(location)) { + if (ts.isPartOfExpression(location) && !ts.isAssignmentTarget(location)) { var type = checkExpression(location); if (getExportSymbolOfValueSymbolIfExported(getNodeLinks(location).resolvedSymbol) === symbol) { return type; @@ -25846,14 +27181,26 @@ var ts; var flowContainer = getControlFlowContainer(node); var isOuterVariable = flowContainer !== declarationContainer; while (flowContainer !== declarationContainer && - (flowContainer.kind === 179 || flowContainer.kind === 180) && + (flowContainer.kind === 179 || + flowContainer.kind === 180 || + ts.isObjectLiteralOrClassExpressionMethod(flowContainer)) && (isReadonlySymbol(localOrExportSymbol) || isParameter && !isParameterAssigned(localOrExportSymbol))) { flowContainer = getControlFlowContainer(flowContainer); } - var assumeInitialized = !strictNullChecks || (type.flags & 1) !== 0 || isParameter || - isOuterVariable || ts.isInAmbientContext(declaration); + var assumeInitialized = isParameter || isOuterVariable || + type !== autoType && (!strictNullChecks || (type.flags & 1) !== 0) || + ts.isInAmbientContext(declaration); var flowType = getFlowTypeOfReference(node, type, assumeInitialized, flowContainer); - if (!assumeInitialized && !(getFalsyFlags(type) & 2048) && getFalsyFlags(flowType) & 2048) { + if (type === autoType) { + if (flowType === autoType) { + if (compilerOptions.noImplicitAny) { + error(declaration.name, ts.Diagnostics.Variable_0_implicitly_has_type_any_in_some_locations_where_its_type_cannot_be_determined, symbolToString(symbol)); + error(node, ts.Diagnostics.Variable_0_implicitly_has_an_1_type, symbolToString(symbol), typeToString(anyType)); + } + return anyType; + } + } + else if (!assumeInitialized && !(getFalsyFlags(type) & 2048) && getFalsyFlags(flowType) & 2048) { error(node, ts.Diagnostics.Variable_0_is_used_before_being_assigned, symbolToString(symbol)); return type; } @@ -25938,7 +27285,7 @@ var ts; } } function findFirstSuperCall(n) { - if (ts.isSuperCallExpression(n)) { + if (ts.isSuperCall(n)) { return n; } else if (ts.isFunctionLike(n)) { @@ -26003,7 +27350,7 @@ var ts; captureLexicalThis(node, container); } if (ts.isFunctionLike(container) && - (!isInParameterInitializerBeforeContainingFunction(node) || getFunctionLikeThisParameter(container))) { + (!isInParameterInitializerBeforeContainingFunction(node) || ts.getThisParameter(container))) { if (container.kind === 179 && ts.isInJavaScriptFile(container.parent) && ts.getSpecialPropertyAssignmentKind(container.parent) === 3) { @@ -26222,11 +27569,11 @@ var ts; } if (ts.isBindingPattern(declaration.parent)) { var parentDeclaration = declaration.parent.parent; - var name_17 = declaration.propertyName || declaration.name; + var name_18 = declaration.propertyName || declaration.name; if (ts.isVariableLike(parentDeclaration) && parentDeclaration.type && - !ts.isBindingPattern(name_17)) { - var text = getTextOfPropertyName(name_17); + !ts.isBindingPattern(name_18)) { + var text = getTextOfPropertyName(name_18); if (text) { return getTypeOfPropertyOfType(getTypeFromTypeNode(parentDeclaration.type), text); } @@ -26663,7 +28010,8 @@ var ts; patternWithComputedProperties = true; } } - else if (contextualTypeHasPattern && !(contextualType.flags & 536870912)) { + else if (contextualTypeHasPattern && + !(contextualType.flags & 2588672 && contextualType.isObjectLiteralPatternWithComputedProperties)) { var impliedProp = getPropertyOfType(contextualType, member.name); if (impliedProp) { prop.flags |= impliedProp.flags & 536870912; @@ -26714,7 +28062,10 @@ var ts; var numberIndexInfo = hasComputedNumberProperty ? getObjectLiteralIndexInfo(node, propertiesArray, 1) : undefined; var result = createAnonymousType(node.symbol, propertiesTable, emptyArray, emptyArray, stringIndexInfo, numberIndexInfo); var freshObjectLiteralFlag = compilerOptions.suppressExcessPropertyErrors ? 0 : 16777216; - result.flags |= 8388608 | 67108864 | freshObjectLiteralFlag | (typeFlags & 234881024) | (patternWithComputedProperties ? 536870912 : 0); + result.flags |= 8388608 | 67108864 | freshObjectLiteralFlag | (typeFlags & 234881024); + if (patternWithComputedProperties) { + result.isObjectLiteralPatternWithComputedProperties = true; + } if (inDestructuringPattern) { result.pattern = node; } @@ -27109,7 +28460,7 @@ var ts; if (flags & 32) { return true; } - if (type.flags & 268435456) { + if (type.flags & 16384 && type.isThisType) { type = getConstraintOfTypeParameter(type); } if (!(getTargetType(type).flags & (32768 | 65536) && hasBaseType(type, enclosingClass))) { @@ -27150,7 +28501,7 @@ var ts; var prop = getPropertyOfType(apparentType, right.text); if (!prop) { if (right.text && !checkAndReportErrorForExtendingInterface(node)) { - reportNonexistentProperty(right, type.flags & 268435456 ? apparentType : type); + reportNonexistentProperty(right, type.flags & 16384 && type.isThisType ? apparentType : type); } return unknownType; } @@ -27266,15 +28617,15 @@ var ts; return unknownType; } if (node.argumentExpression) { - var name_18 = getPropertyNameForIndexedAccess(node.argumentExpression, indexType); - if (name_18 !== undefined) { - var prop = getPropertyOfType(objectType, name_18); + var name_19 = getPropertyNameForIndexedAccess(node.argumentExpression, indexType); + if (name_19 !== undefined) { + var prop = getPropertyOfType(objectType, name_19); if (prop) { getNodeLinks(node).resolvedSymbol = prop; return getTypeOfSymbol(prop); } else if (isConstEnum) { - error(node.argumentExpression, ts.Diagnostics.Property_0_does_not_exist_on_const_enum_1, name_18, symbolToString(objectType.symbol)); + error(node.argumentExpression, ts.Diagnostics.Property_0_does_not_exist_on_const_enum_1, name_19, symbolToString(objectType.symbol)); return unknownType; } } @@ -27375,19 +28726,19 @@ var ts; for (var _i = 0, signatures_2 = signatures; _i < signatures_2.length; _i++) { var signature = signatures_2[_i]; var symbol = signature.declaration && getSymbolOfNode(signature.declaration); - var parent_10 = signature.declaration && signature.declaration.parent; + var parent_11 = signature.declaration && signature.declaration.parent; if (!lastSymbol || symbol === lastSymbol) { - if (lastParent && parent_10 === lastParent) { + if (lastParent && parent_11 === lastParent) { index++; } else { - lastParent = parent_10; + lastParent = parent_11; index = cutoffIndex; } } else { index = cutoffIndex = result.length; - lastParent = parent_10; + lastParent = parent_11; } lastSymbol = symbol; if (signature.hasLiteralTypes) { @@ -28290,7 +29641,9 @@ var ts; if (!contextualSignature) { reportErrorsFromWidening(func, type); } - if (isUnitType(type) && !(contextualSignature && isLiteralContextualType(getReturnTypeOfSignature(contextualSignature)))) { + if (isUnitType(type) && + !(contextualSignature && + isLiteralContextualType(contextualSignature === getSignatureFromDeclaration(func) ? type : getReturnTypeOfSignature(contextualSignature)))) { type = getWidenedLiteralType(type); } var widenedType = getWidenedType(type); @@ -28440,7 +29793,7 @@ var ts; } } } - if (produceDiagnostics && node.kind !== 147 && node.kind !== 146) { + if (produceDiagnostics && node.kind !== 147) { checkCollisionWithCapturedSuperVariable(node, node.name); checkCollisionWithCapturedThisVariable(node, node.name); } @@ -28690,14 +30043,14 @@ var ts; } function checkObjectLiteralDestructuringPropertyAssignment(objectLiteralType, property, contextualMapper) { if (property.kind === 253 || property.kind === 254) { - var name_19 = property.name; - if (name_19.kind === 140) { - checkComputedPropertyName(name_19); + var name_20 = property.name; + if (name_20.kind === 140) { + checkComputedPropertyName(name_20); } - if (isComputedNonLiteralName(name_19)) { + if (isComputedNonLiteralName(name_20)) { return undefined; } - var text = getTextOfPropertyName(name_19); + var text = getTextOfPropertyName(name_20); var type = isTypeAny(objectLiteralType) ? objectLiteralType : getTypeOfPropertyOfType(objectLiteralType, text) || @@ -28712,7 +30065,7 @@ var ts; } } else { - error(name_19, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(objectLiteralType), ts.declarationNameToString(name_19)); + error(name_20, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(objectLiteralType), ts.declarationNameToString(name_20)); } } else { @@ -29361,9 +30714,9 @@ var ts; else if (parameterName) { var hasReportedError = false; for (var _i = 0, _a = parent.parameters; _i < _a.length; _i++) { - var name_20 = _a[_i].name; - if (ts.isBindingPattern(name_20) && - checkIfTypePredicateVariableIsDeclaredInBindingPattern(name_20, parameterName, typePredicate.parameterName)) { + var name_21 = _a[_i].name; + if (ts.isBindingPattern(name_21) && + checkIfTypePredicateVariableIsDeclaredInBindingPattern(name_21, parameterName, typePredicate.parameterName)) { hasReportedError = true; break; } @@ -29383,9 +30736,9 @@ var ts; case 156: case 147: case 146: - var parent_11 = node.parent; - if (node === parent_11.type) { - return parent_11; + var parent_12 = node.parent; + if (node === parent_12.type) { + return parent_12; } } } @@ -29395,15 +30748,15 @@ var ts; if (ts.isOmittedExpression(element)) { continue; } - var name_21 = element.name; - if (name_21.kind === 69 && - name_21.text === predicateVariableName) { + var name_22 = element.name; + if (name_22.kind === 69 && + name_22.text === predicateVariableName) { error(predicateVariableNode, ts.Diagnostics.A_type_predicate_cannot_reference_element_0_in_a_binding_pattern, predicateVariableName); return true; } - else if (name_21.kind === 168 || - name_21.kind === 167) { - if (checkIfTypePredicateVariableIsDeclaredInBindingPattern(name_21, predicateVariableNode, predicateVariableName)) { + else if (name_22.kind === 168 || + name_22.kind === 167) { + if (checkIfTypePredicateVariableIsDeclaredInBindingPattern(name_22, predicateVariableNode, predicateVariableName)) { return true; } } @@ -29596,7 +30949,7 @@ var ts; return n.name && containsSuperCall(n.name); } function containsSuperCall(n) { - if (ts.isSuperCallExpression(n)) { + if (ts.isSuperCall(n)) { return true; } else if (ts.isFunctionLike(n)) { @@ -29622,6 +30975,7 @@ var ts; } var containingClassDecl = node.parent; if (ts.getClassExtendsHeritageClauseElement(containingClassDecl)) { + captureLexicalThis(node.parent, containingClassDecl); var classExtendsNull = classDeclarationExtendsNull(containingClassDecl); var superCall = getSuperCallInConstructor(node); if (superCall) { @@ -29635,7 +30989,7 @@ var ts; var superCallStatement = void 0; for (var _i = 0, statements_2 = statements; _i < statements_2.length; _i++) { var statement = statements_2[_i]; - if (statement.kind === 202 && ts.isSuperCallExpression(statement.expression)) { + if (statement.kind === 202 && ts.isSuperCall(statement.expression)) { superCallStatement = statement; break; } @@ -30345,7 +31699,7 @@ var ts; var parameter = local.valueDeclaration; if (compilerOptions.noUnusedParameters && !ts.isParameterPropertyDeclaration(parameter) && - !parameterIsThisKeyword(parameter) && + !ts.parameterIsThisKeyword(parameter) && !parameterNameStartsWithUnderscore(parameter)) { error(local.valueDeclaration.name, ts.Diagnostics._0_is_declared_but_never_used, local.name); } @@ -30360,9 +31714,6 @@ var ts; } } } - function parameterIsThisKeyword(parameter) { - return parameter.name && parameter.name.originalKeywordKind === 97; - } function parameterNameStartsWithUnderscore(parameter) { return parameter.name && parameter.name.kind === 69 && parameter.name.text.charCodeAt(0) === 95; } @@ -30500,6 +31851,9 @@ var ts; } } function checkCollisionWithRequireExportsInGeneratedCode(node, name) { + if (modulekind >= ts.ModuleKind.ES6) { + return; + } if (!needCollisionCheckForIdentifier(node, name, "require") && !needCollisionCheckForIdentifier(node, name, "exports")) { return; } @@ -30547,8 +31901,8 @@ var ts; container.kind === 225 || container.kind === 256); if (!namesShareScope) { - var name_22 = symbolToString(localDeclarationSymbol); - error(node, ts.Diagnostics.Cannot_initialize_outer_scoped_variable_0_in_the_same_scope_as_block_scoped_declaration_1, name_22, name_22); + var name_23 = symbolToString(localDeclarationSymbol); + error(node, ts.Diagnostics.Cannot_initialize_outer_scoped_variable_0_in_the_same_scope_as_block_scoped_declaration_1, name_23, name_23); } } } @@ -30603,6 +31957,9 @@ var ts; } } } + function convertAutoToAny(type) { + return type === autoType ? anyType : type; + } function checkVariableLikeDeclaration(node) { checkDecorators(node); checkSourceElement(node.type); @@ -30616,12 +31973,12 @@ var ts; if (node.propertyName && node.propertyName.kind === 140) { checkComputedPropertyName(node.propertyName); } - var parent_12 = node.parent.parent; - var parentType = getTypeForBindingElementParent(parent_12); - var name_23 = node.propertyName || node.name; - var property = getPropertyOfType(parentType, getTextOfPropertyName(name_23)); - if (parent_12.initializer && property && getParentOfSymbol(property)) { - checkClassPropertyAccess(parent_12, parent_12.initializer, parentType, property); + var parent_13 = node.parent.parent; + var parentType = getTypeForBindingElementParent(parent_13); + var name_24 = node.propertyName || node.name; + var property = getPropertyOfType(parentType, getTextOfPropertyName(name_24)); + if (parent_13.initializer && property && getParentOfSymbol(property)) { + checkClassPropertyAccess(parent_13, parent_13.initializer, parentType, property); } } if (ts.isBindingPattern(node.name)) { @@ -30639,7 +31996,7 @@ var ts; return; } var symbol = getSymbolOfNode(node); - var type = getTypeOfVariableOrParameterOrProperty(symbol); + var type = convertAutoToAny(getTypeOfVariableOrParameterOrProperty(symbol)); if (node === symbol.valueDeclaration) { if (node.initializer && node.parent.parent.kind !== 207) { checkTypeAssignableTo(checkExpressionCached(node.initializer), type, node, undefined); @@ -30647,7 +32004,7 @@ var ts; } } else { - var declarationType = getWidenedTypeForVariableLikeDeclaration(node); + var declarationType = convertAutoToAny(getWidenedTypeForVariableLikeDeclaration(node)); if (type !== unknownType && declarationType !== unknownType && !isTypeIdenticalTo(type, declarationType)) { error(node.name, ts.Diagnostics.Subsequent_variable_declarations_must_have_the_same_type_Variable_0_must_be_of_type_1_but_here_has_type_2, ts.declarationNameToString(node.name), typeToString(type), typeToString(declarationType)); } @@ -31022,7 +32379,12 @@ var ts; } } checkExpression(node.expression); - error(node.expression, ts.Diagnostics.All_symbols_within_a_with_block_will_be_resolved_to_any); + var sourceFile = ts.getSourceFileOfNode(node); + if (!hasParseDiagnostics(sourceFile)) { + var start = ts.getSpanOfTokenAtPosition(sourceFile, node.pos).start; + var end = node.statement.pos; + grammarErrorAtPos(sourceFile, start, end - start, ts.Diagnostics.The_with_statement_is_not_supported_All_symbols_in_a_with_block_will_have_type_any); + } } function checkSwitchStatement(node) { checkGrammarStatementInAmbientContext(node); @@ -31741,9 +33103,11 @@ var ts; grammarErrorOnNode(node.name, ts.Diagnostics.Only_ambient_modules_can_use_quoted_names); } } - checkCollisionWithCapturedThisVariable(node, node.name); - checkCollisionWithRequireExportsInGeneratedCode(node, node.name); - checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); + if (ts.isIdentifier(node.name)) { + checkCollisionWithCapturedThisVariable(node, node.name); + checkCollisionWithRequireExportsInGeneratedCode(node, node.name); + checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); + } checkExportsOnMergedDeclarations(node); var symbol = getSymbolOfNode(node); if (symbol.flags & 512 @@ -31818,9 +33182,9 @@ var ts; break; case 169: case 218: - var name_24 = node.name; - if (ts.isBindingPattern(name_24)) { - for (var _b = 0, _c = name_24.elements; _b < _c.length; _b++) { + var name_25 = node.name; + if (ts.isBindingPattern(name_25)) { + for (var _b = 0, _c = name_25.elements; _b < _c.length; _b++) { var el = _c[_b]; checkModuleAugmentationElement(el, isGlobalAugmentation); } @@ -31982,9 +33346,11 @@ var ts; } } function checkGrammarModuleElementContext(node, errorMessage) { - if (node.parent.kind !== 256 && node.parent.kind !== 226 && node.parent.kind !== 225) { - return grammarErrorOnFirstToken(node, errorMessage); + var isInAppropriateContext = node.parent.kind === 256 || node.parent.kind === 226 || node.parent.kind === 225; + if (!isInAppropriateContext) { + grammarErrorOnFirstToken(node, errorMessage); } + return !isInAppropriateContext; } function checkExportSpecifier(node) { checkAliasSymbol(node); @@ -32046,12 +33412,12 @@ var ts; error(declaration, ts.Diagnostics.An_export_assignment_cannot_be_used_in_a_module_with_other_exported_elements); } } - var exports = getExportsOfModule(moduleSymbol); - for (var id in exports) { + var exports_1 = getExportsOfModule(moduleSymbol); + for (var id in exports_1) { if (id === "__export") { continue; } - var _a = exports[id], declarations = _a.declarations, flags = _a.flags; + var _a = exports_1[id], declarations = _a.declarations, flags = _a.flags; if (flags & (1920 | 64 | 384)) { continue; } @@ -32071,7 +33437,8 @@ var ts; links.exportsChecked = true; } function isNotOverload(declaration) { - return declaration.kind !== 220 || !!declaration.body; + return (declaration.kind !== 220 && declaration.kind !== 147) || + !!declaration.body; } } function checkSourceElement(node) { @@ -32556,6 +33923,9 @@ var ts; node.parent.moduleSpecifier === node)) { return resolveExternalModuleName(node, node); } + if (ts.isInJavaScriptFile(node) && ts.isRequireCall(node.parent, false)) { + return resolveExternalModuleName(node, node); + } case 8: if (node.parent.kind === 173 && node.parent.argumentExpression === node) { var objectType = checkExpression(node.parent.expression); @@ -32670,9 +34040,9 @@ var ts; function getRootSymbols(symbol) { if (symbol.flags & 268435456) { var symbols_3 = []; - var name_25 = symbol.name; + var name_26 = symbol.name; ts.forEach(getSymbolLinks(symbol).containingType.types, function (t) { - var symbol = getPropertyOfType(t, name_25); + var symbol = getPropertyOfType(t, name_26); if (symbol) { symbols_3.push(symbol); } @@ -32979,9 +34349,9 @@ var ts; } var location = reference; if (startInDeclarationContainer) { - var parent_13 = reference.parent; - if (ts.isDeclaration(parent_13) && reference === parent_13.name) { - location = getDeclarationContainer(parent_13); + var parent_14 = reference.parent; + if (ts.isDeclaration(parent_14) && reference === parent_14.name) { + location = getDeclarationContainer(parent_14); } } return resolveName(location, reference.text, 107455 | 1048576 | 8388608, undefined, undefined); @@ -33090,9 +34460,9 @@ var ts; } var current = symbol; while (true) { - var parent_14 = getParentOfSymbol(current); - if (parent_14) { - current = parent_14; + var parent_15 = getParentOfSymbol(current); + if (parent_15) { + current = parent_15; } else { break; @@ -33211,26 +34581,26 @@ var ts; if (compilerOptions.importHelpers && firstFileRequestingExternalHelpers) { var helpersModule = resolveExternalModule(firstFileRequestingExternalHelpers, ts.externalHelpersModuleNameText, ts.Diagnostics.Cannot_find_module_0, undefined); if (helpersModule) { - var exports = helpersModule.exports; + var exports_2 = helpersModule.exports; if (requestedExternalEmitHelpers & 1024 && languageVersion < 2) { - verifyHelperSymbol(exports, "__extends", 107455); + verifyHelperSymbol(exports_2, "__extends", 107455); } if (requestedExternalEmitHelpers & 16384 && compilerOptions.jsx !== 1) { - verifyHelperSymbol(exports, "__assign", 107455); + verifyHelperSymbol(exports_2, "__assign", 107455); } if (requestedExternalEmitHelpers & 2048) { - verifyHelperSymbol(exports, "__decorate", 107455); + verifyHelperSymbol(exports_2, "__decorate", 107455); if (compilerOptions.emitDecoratorMetadata) { - verifyHelperSymbol(exports, "__metadata", 107455); + verifyHelperSymbol(exports_2, "__metadata", 107455); } } if (requestedExternalEmitHelpers & 4096) { - verifyHelperSymbol(exports, "__param", 107455); + verifyHelperSymbol(exports_2, "__param", 107455); } if (requestedExternalEmitHelpers & 8192) { - verifyHelperSymbol(exports, "__awaiter", 107455); + verifyHelperSymbol(exports_2, "__awaiter", 107455); if (languageVersion < 2) { - verifyHelperSymbol(exports, "__generator", 107455); + verifyHelperSymbol(exports_2, "__generator", 107455); } } } @@ -33652,8 +35022,8 @@ var ts; function checkGrammarForOmittedArgument(node, args) { if (args) { var sourceFile = ts.getSourceFileOfNode(node); - for (var _i = 0, args_2 = args; _i < args_2.length; _i++) { - var arg = args_2[_i]; + for (var _i = 0, args_4 = args; _i < args_4.length; _i++) { + var arg = args_4[_i]; if (arg.kind === 193) { return grammarErrorAtPos(sourceFile, arg.pos, 0, ts.Diagnostics.Argument_expression_expected); } @@ -33761,10 +35131,9 @@ var ts; var GetOrSetAccessor = GetAccessor | SetAccessor; for (var _i = 0, _a = node.properties; _i < _a.length; _i++) { var prop = _a[_i]; - var name_26 = prop.name; - if (prop.kind === 193 || - name_26.kind === 140) { - checkGrammarComputedPropertyName(name_26); + var name_27 = prop.name; + if (name_27.kind === 140) { + checkGrammarComputedPropertyName(name_27); } if (prop.kind === 254 && !inDestructuring && prop.objectAssignmentInitializer) { return grammarErrorOnNode(prop.equalsToken, ts.Diagnostics.can_only_be_used_in_an_object_literal_property_inside_a_destructuring_assignment); @@ -33780,8 +35149,8 @@ var ts; var currentKind = void 0; if (prop.kind === 253 || prop.kind === 254) { checkGrammarForInvalidQuestionMark(prop, prop.questionToken, ts.Diagnostics.An_object_member_cannot_be_declared_optional); - if (name_26.kind === 8) { - checkGrammarNumericLiteral(name_26); + if (name_27.kind === 8) { + checkGrammarNumericLiteral(name_27); } currentKind = Property; } @@ -33797,7 +35166,7 @@ var ts; else { ts.Debug.fail("Unexpected syntax kind:" + prop.kind); } - var effectiveName = ts.getPropertyNameForPropertyNameNode(name_26); + var effectiveName = ts.getPropertyNameForPropertyNameNode(name_27); if (effectiveName === undefined) { continue; } @@ -33807,18 +35176,18 @@ var ts; else { var existingKind = seen[effectiveName]; if (currentKind === Property && existingKind === Property) { - grammarErrorOnNode(name_26, ts.Diagnostics.Duplicate_identifier_0, ts.getTextOfNode(name_26)); + grammarErrorOnNode(name_27, ts.Diagnostics.Duplicate_identifier_0, ts.getTextOfNode(name_27)); } else if ((currentKind & GetOrSetAccessor) && (existingKind & GetOrSetAccessor)) { if (existingKind !== GetOrSetAccessor && currentKind !== existingKind) { seen[effectiveName] = currentKind | existingKind; } else { - return grammarErrorOnNode(name_26, ts.Diagnostics.An_object_literal_cannot_have_multiple_get_Slashset_accessors_with_the_same_name); + return grammarErrorOnNode(name_27, ts.Diagnostics.An_object_literal_cannot_have_multiple_get_Slashset_accessors_with_the_same_name); } } else { - return grammarErrorOnNode(name_26, ts.Diagnostics.An_object_literal_cannot_have_property_and_accessor_with_the_same_name); + return grammarErrorOnNode(name_27, ts.Diagnostics.An_object_literal_cannot_have_property_and_accessor_with_the_same_name); } } } @@ -33831,12 +35200,12 @@ var ts; continue; } var jsxAttr = attr; - var name_27 = jsxAttr.name; - if (!seen[name_27.text]) { - seen[name_27.text] = true; + var name_28 = jsxAttr.name; + if (!seen[name_28.text]) { + seen[name_28.text] = true; } else { - return grammarErrorOnNode(name_27, ts.Diagnostics.JSX_elements_cannot_have_multiple_attributes_with_the_same_name); + return grammarErrorOnNode(name_28, ts.Diagnostics.JSX_elements_cannot_have_multiple_attributes_with_the_same_name); } var initializer = jsxAttr.initializer; if (initializer && initializer.kind === 248 && !initializer.expression) { @@ -33919,17 +35288,8 @@ var ts; return getAccessorThisParameter(accessor) || accessor.parameters.length === (accessor.kind === 149 ? 0 : 1); } function getAccessorThisParameter(accessor) { - if (accessor.parameters.length === (accessor.kind === 149 ? 1 : 2) && - accessor.parameters[0].name.kind === 69 && - accessor.parameters[0].name.originalKeywordKind === 97) { - return accessor.parameters[0]; - } - } - function getFunctionLikeThisParameter(func) { - if (func.parameters.length && - func.parameters[0].name.kind === 69 && - func.parameters[0].name.originalKeywordKind === 97) { - return func.parameters[0]; + if (accessor.parameters.length === (accessor.kind === 149 ? 1 : 2)) { + return ts.getThisParameter(accessor); } } function checkGrammarForNonSymbolComputedProperty(node, message) { @@ -34763,7 +36123,7 @@ var ts; case 175: return ts.updateNew(node, visitNode(node.expression, visitor, ts.isExpression), visitNodes(node.typeArguments, visitor, ts.isTypeNode), visitNodes(node.arguments, visitor, ts.isExpression)); case 176: - return ts.updateTaggedTemplate(node, visitNode(node.tag, visitor, ts.isExpression), visitNode(node.template, visitor, ts.isTemplate)); + return ts.updateTaggedTemplate(node, visitNode(node.tag, visitor, ts.isExpression), visitNode(node.template, visitor, ts.isTemplateLiteral)); case 178: return ts.updateParen(node, visitNode(node.expression, visitor, ts.isExpression)); case 179: @@ -34787,7 +36147,7 @@ var ts; case 188: return ts.updateConditional(node, visitNode(node.condition, visitor, ts.isExpression), visitNode(node.whenTrue, visitor, ts.isExpression), visitNode(node.whenFalse, visitor, ts.isExpression)); case 189: - return ts.updateTemplateExpression(node, visitNode(node.head, visitor, ts.isTemplateLiteralFragment), visitNodes(node.templateSpans, visitor, ts.isTemplateSpan)); + return ts.updateTemplateExpression(node, visitNode(node.head, visitor, ts.isTemplateHead), visitNodes(node.templateSpans, visitor, ts.isTemplateSpan)); case 190: return ts.updateYield(node, visitNode(node.expression, visitor, ts.isExpression)); case 191: @@ -34797,7 +36157,7 @@ var ts; case 194: return ts.updateExpressionWithTypeArguments(node, visitNodes(node.typeArguments, visitor, ts.isTypeNode), visitNode(node.expression, visitor, ts.isExpression)); case 197: - return ts.updateTemplateSpan(node, visitNode(node.expression, visitor, ts.isExpression), visitNode(node.literal, visitor, ts.isTemplateLiteralFragment)); + return ts.updateTemplateSpan(node, visitNode(node.expression, visitor, ts.isExpression), visitNode(node.literal, visitor, ts.isTemplateMiddleOrTemplateTail)); case 199: return ts.updateBlock(node, visitNodes(node.statements, visitor, ts.isStatement)); case 200: @@ -35072,7 +36432,7 @@ var ts; return expression; function emitAssignment(name, value, location) { var expression = ts.createAssignment(name, value, location); - context.setNodeEmitFlags(expression, 2048); + ts.setEmitFlags(expression, 2048); ts.aggregateTransformFlags(expression); expressions.push(expression); } @@ -35089,7 +36449,7 @@ var ts; return declarations; function emitAssignment(name, value, location) { var declaration = ts.createVariableDeclaration(name, undefined, value, location); - context.setNodeEmitFlags(declaration, 2048); + ts.setEmitFlags(declaration, 2048); ts.aggregateTransformFlags(declaration); declarations.push(declaration); } @@ -35113,7 +36473,7 @@ var ts; } var declaration = ts.createVariableDeclaration(name, undefined, value, location); declaration.original = original; - context.setNodeEmitFlags(declaration, 2048); + ts.setEmitFlags(declaration, 2048); declarations.push(declaration); ts.aggregateTransformFlags(declaration); } @@ -35153,7 +36513,7 @@ var ts; function emitPendingAssignment(name, value, location, original) { var expression = ts.createAssignment(name, value, location); expression.original = original; - context.setNodeEmitFlags(expression, 2048); + ts.setEmitFlags(expression, 2048); pendingAssignments.push(expression); return expression; } @@ -35197,10 +36557,10 @@ var ts; emitArrayLiteralAssignment(target, value, location); } else { - var name_28 = ts.getMutableClone(target); - context.setSourceMapRange(name_28, target); - context.setCommentRange(name_28, target); - emitAssignment(name_28, value, location, undefined); + var name_29 = ts.getMutableClone(target); + ts.setSourceMapRange(name_29, target); + ts.setCommentRange(name_29, target); + emitAssignment(name_29, value, location, undefined); } } function emitObjectLiteralAssignment(target, value, location) { @@ -35314,7 +36674,7 @@ var ts; (function (ts) { var USE_NEW_TYPE_METADATA_FORMAT = false; function transformTypeScript(context) { - var getNodeEmitFlags = context.getNodeEmitFlags, setNodeEmitFlags = context.setNodeEmitFlags, setCommentRange = context.setCommentRange, setSourceMapRange = context.setSourceMapRange, startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration; + var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration; var resolver = context.getEmitResolver(); var compilerOptions = context.getCompilerOptions(); var languageVersion = ts.getEmitScriptTarget(compilerOptions); @@ -35323,10 +36683,13 @@ var ts; var previousOnSubstituteNode = context.onSubstituteNode; context.onEmitNode = onEmitNode; context.onSubstituteNode = onSubstituteNode; + context.enableSubstitution(172); + context.enableSubstitution(173); var currentSourceFile; var currentNamespace; var currentNamespaceContainerName; var currentScope; + var currentScopeFirstDeclarationsOfName; var currentSourceFileExternalHelpersModuleName; var enabledSubstitutions; var classAliases; @@ -35334,12 +36697,19 @@ var ts; var currentSuperContainer; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } return ts.visitNode(node, visitor, ts.isSourceFile); } function saveStateAndInvoke(node, f) { var savedCurrentScope = currentScope; + var savedCurrentScopeFirstDeclarationsOfName = currentScopeFirstDeclarationsOfName; onBeforeVisitNode(node); var visited = f(node); + if (currentScope !== savedCurrentScope) { + currentScopeFirstDeclarationsOfName = savedCurrentScopeFirstDeclarationsOfName; + } currentScope = savedCurrentScope; return visited; } @@ -35493,11 +36863,22 @@ var ts; case 226: case 199: currentScope = node; + currentScopeFirstDeclarationsOfName = undefined; + break; + case 221: + case 220: + if (ts.hasModifier(node, 2)) { + break; + } + recordEmittedDeclarationInScope(node); break; } } function visitSourceFile(node) { currentSourceFile = node; + if (compilerOptions.alwaysStrict) { + node = ts.ensureUseStrict(node); + } if (node.flags & 31744 && compilerOptions.importHelpers && (ts.isExternalModule(node) || compilerOptions.isolatedModules)) { @@ -35519,7 +36900,7 @@ var ts; else { node = ts.visitEachChild(node, visitor, context); } - setNodeEmitFlags(node, 1 | node.emitFlags); + ts.setEmitFlags(node, 1 | ts.getEmitFlags(node)); return node; } function shouldEmitDecorateCallForClass(node) { @@ -35549,7 +36930,7 @@ var ts; var classDeclaration = ts.createClassDeclaration(undefined, ts.visitNodes(node.modifiers, visitor, ts.isModifier), name, undefined, ts.visitNodes(node.heritageClauses, visitor, ts.isHeritageClause), transformClassMembers(node, hasExtendsClause), node); ts.setOriginalNode(classDeclaration, node); if (staticProperties.length > 0) { - setNodeEmitFlags(classDeclaration, 1024 | getNodeEmitFlags(classDeclaration)); + ts.setEmitFlags(classDeclaration, 1024 | ts.getEmitFlags(classDeclaration)); } statements.push(classDeclaration); } @@ -35591,7 +36972,7 @@ var ts; var transformedClassExpression = ts.createVariableStatement(undefined, ts.createLetDeclarationList([ ts.createVariableDeclaration(classAlias || declaredName, undefined, classExpression) ]), location); - setCommentRange(transformedClassExpression, node); + ts.setCommentRange(transformedClassExpression, node); statements.push(ts.setOriginalNode(transformedClassExpression, node)); if (classAlias) { statements.push(ts.setOriginalNode(ts.createVariableStatement(undefined, ts.createLetDeclarationList([ @@ -35612,7 +36993,7 @@ var ts; enableSubstitutionForClassAliases(); classAliases[ts.getOriginalNodeId(node)] = ts.getSynthesizedClone(temp); } - setNodeEmitFlags(classExpression, 524288 | getNodeEmitFlags(classExpression)); + ts.setEmitFlags(classExpression, 524288 | ts.getEmitFlags(classExpression)); expressions.push(ts.startOnNewLine(ts.createAssignment(temp, classExpression))); ts.addRange(expressions, generateInitializedPropertyExpressions(node, staticProperties, temp)); expressions.push(ts.startOnNewLine(temp)); @@ -35673,7 +37054,7 @@ var ts; return index; } var statement = statements[index]; - if (statement.kind === 202 && ts.isSuperCallExpression(statement.expression)) { + if (statement.kind === 202 && ts.isSuperCall(statement.expression)) { result.push(ts.visitNode(statement, visitor, ts.isStatement)); return index + 1; } @@ -35692,9 +37073,9 @@ var ts; ts.Debug.assert(ts.isIdentifier(node.name)); var name = node.name; var propertyName = ts.getMutableClone(name); - setNodeEmitFlags(propertyName, 49152 | 1536); + ts.setEmitFlags(propertyName, 49152 | 1536); var localName = ts.getMutableClone(name); - setNodeEmitFlags(localName, 49152); + ts.setEmitFlags(localName, 49152); return ts.startOnNewLine(ts.createStatement(ts.createAssignment(ts.createPropertyAccess(ts.createThis(), propertyName, node.name), localName), ts.moveRangePos(node, -1))); } function getInitializedProperties(node, isStatic) { @@ -35715,8 +37096,8 @@ var ts; for (var _i = 0, properties_7 = properties; _i < properties_7.length; _i++) { var property = properties_7[_i]; var statement = ts.createStatement(transformInitializedProperty(node, property, receiver)); - setSourceMapRange(statement, ts.moveRangePastModifiers(property)); - setCommentRange(statement, property); + ts.setSourceMapRange(statement, ts.moveRangePastModifiers(property)); + ts.setCommentRange(statement, property); statements.push(statement); } } @@ -35726,8 +37107,8 @@ var ts; var property = properties_8[_i]; var expression = transformInitializedProperty(node, property, receiver); expression.startsOnNewLine = true; - setSourceMapRange(expression, ts.moveRangePastModifiers(property)); - setCommentRange(expression, property); + ts.setSourceMapRange(expression, ts.moveRangePastModifiers(property)); + ts.setCommentRange(expression, property); expressions.push(expression); } return expressions; @@ -35868,7 +37249,7 @@ var ts; : ts.createNull() : undefined; var helper = ts.createDecorateHelper(currentSourceFileExternalHelpersModuleName, decoratorExpressions, prefix, memberName, descriptor, ts.moveRangePastDecorators(member)); - setNodeEmitFlags(helper, 49152); + ts.setEmitFlags(helper, 49152); return helper; } function addConstructorDecorationStatement(statements, node, decoratedClassAlias) { @@ -35886,12 +37267,12 @@ var ts; if (decoratedClassAlias) { var expression = ts.createAssignment(decoratedClassAlias, ts.createDecorateHelper(currentSourceFileExternalHelpersModuleName, decoratorExpressions, getDeclarationName(node))); var result = ts.createAssignment(getDeclarationName(node), expression, ts.moveRangePastDecorators(node)); - setNodeEmitFlags(result, 49152); + ts.setEmitFlags(result, 49152); return result; } else { var result = ts.createAssignment(getDeclarationName(node), ts.createDecorateHelper(currentSourceFileExternalHelpersModuleName, decoratorExpressions, getDeclarationName(node)), ts.moveRangePastDecorators(node)); - setNodeEmitFlags(result, 49152); + ts.setEmitFlags(result, 49152); return result; } } @@ -35905,7 +37286,7 @@ var ts; for (var _i = 0, decorators_1 = decorators; _i < decorators_1.length; _i++) { var decorator = decorators_1[_i]; var helper = ts.createParamHelper(currentSourceFileExternalHelpersModuleName, transformDecorator(decorator), parameterOffset, decorator.expression); - setNodeEmitFlags(helper, 49152); + ts.setEmitFlags(helper, 49152); expressions.push(helper); } } @@ -36070,10 +37451,33 @@ var ts; : ts.createIdentifier("Symbol"); case 155: return serializeTypeReferenceNode(node); + case 163: + case 162: + { + var unionOrIntersection = node; + var serializedUnion = void 0; + for (var _i = 0, _a = unionOrIntersection.types; _i < _a.length; _i++) { + var typeNode = _a[_i]; + var serializedIndividual = serializeTypeNode(typeNode); + if (serializedIndividual.kind !== 69) { + serializedUnion = undefined; + break; + } + if (serializedIndividual.text === "Object") { + return serializedIndividual; + } + if (serializedUnion && serializedUnion.text !== serializedIndividual.text) { + serializedUnion = undefined; + break; + } + serializedUnion = serializedIndividual; + } + if (serializedUnion) { + return serializedUnion; + } + } case 158: case 159: - case 162: - case 163: case 117: case 165: break; @@ -36117,14 +37521,14 @@ var ts; function serializeEntityNameAsExpression(node, useFallback) { switch (node.kind) { case 69: - var name_29 = ts.getMutableClone(node); - name_29.flags &= ~8; - name_29.original = undefined; - name_29.parent = currentScope; + var name_30 = ts.getMutableClone(node); + name_30.flags &= ~8; + name_30.original = undefined; + name_30.parent = currentScope; if (useFallback) { - return ts.createLogicalAnd(ts.createStrictInequality(ts.createTypeOf(name_29), ts.createLiteral("undefined")), name_29); + return ts.createLogicalAnd(ts.createStrictInequality(ts.createTypeOf(name_30), ts.createLiteral("undefined")), name_30); } - return name_29; + return name_30; case 139: return serializeQualifiedNameAsExpression(node, useFallback); } @@ -36194,8 +37598,8 @@ var ts; return undefined; } var method = ts.createMethod(undefined, ts.visitNodes(node.modifiers, visitor, ts.isModifier), node.asteriskToken, visitPropertyNameOfClassElement(node), undefined, ts.visitNodes(node.parameters, visitor, ts.isParameter), undefined, transformFunctionBody(node), node); - setCommentRange(method, node); - setSourceMapRange(method, ts.moveRangePastDecorators(node)); + ts.setCommentRange(method, node); + ts.setSourceMapRange(method, ts.moveRangePastDecorators(node)); ts.setOriginalNode(method, node); return method; } @@ -36207,8 +37611,8 @@ var ts; return undefined; } var accessor = ts.createGetAccessor(undefined, ts.visitNodes(node.modifiers, visitor, ts.isModifier), visitPropertyNameOfClassElement(node), ts.visitNodes(node.parameters, visitor, ts.isParameter), undefined, node.body ? ts.visitEachChild(node.body, visitor, context) : ts.createBlock([]), node); - setCommentRange(accessor, node); - setSourceMapRange(accessor, ts.moveRangePastDecorators(node)); + ts.setCommentRange(accessor, node); + ts.setSourceMapRange(accessor, ts.moveRangePastDecorators(node)); ts.setOriginalNode(accessor, node); return accessor; } @@ -36217,8 +37621,8 @@ var ts; return undefined; } var accessor = ts.createSetAccessor(undefined, ts.visitNodes(node.modifiers, visitor, ts.isModifier), visitPropertyNameOfClassElement(node), ts.visitNodes(node.parameters, visitor, ts.isParameter), node.body ? ts.visitEachChild(node.body, visitor, context) : ts.createBlock([]), node); - setCommentRange(accessor, node); - setSourceMapRange(accessor, ts.moveRangePastDecorators(node)); + ts.setCommentRange(accessor, node); + ts.setSourceMapRange(accessor, ts.moveRangePastDecorators(node)); ts.setOriginalNode(accessor, node); return accessor; } @@ -36313,11 +37717,11 @@ var ts; if (languageVersion >= 2) { if (resolver.getNodeCheckFlags(node) & 4096) { enableSubstitutionForAsyncMethodsWithSuper(); - setNodeEmitFlags(block, 8); + ts.setEmitFlags(block, 8); } else if (resolver.getNodeCheckFlags(node) & 2048) { enableSubstitutionForAsyncMethodsWithSuper(); - setNodeEmitFlags(block, 4); + ts.setEmitFlags(block, 4); } } return block; @@ -36327,14 +37731,14 @@ var ts; } } function visitParameter(node) { - if (node.name && ts.isIdentifier(node.name) && node.name.originalKeywordKind === 97) { + if (ts.parameterIsThisKeyword(node)) { return undefined; } var parameter = ts.createParameterDeclaration(undefined, undefined, node.dotDotDotToken, ts.visitNode(node.name, visitor, ts.isBindingName), undefined, undefined, ts.visitNode(node.initializer, visitor, ts.isExpression), ts.moveRangePastModifiers(node)); ts.setOriginalNode(parameter, node); - setCommentRange(parameter, node); - setSourceMapRange(parameter, ts.moveRangePastModifiers(node)); - setNodeEmitFlags(parameter.name, 1024); + ts.setCommentRange(parameter, node); + ts.setSourceMapRange(parameter, ts.moveRangePastModifiers(node)); + ts.setEmitFlags(parameter.name, 1024); return parameter; } function visitVariableStatement(node) { @@ -36383,12 +37787,13 @@ var ts; || compilerOptions.isolatedModules; } function shouldEmitVarForEnumDeclaration(node) { - return !ts.hasModifier(node, 1) - || (isES6ExportedDeclaration(node) && ts.isFirstDeclarationOfKind(node, node.kind)); + return isFirstEmittedDeclarationInScope(node) + && (!ts.hasModifier(node, 1) + || isES6ExportedDeclaration(node)); } function addVarForEnumExportedFromNamespace(statements, node) { var statement = ts.createVariableStatement(undefined, [ts.createVariableDeclaration(getDeclarationName(node), undefined, getExportName(node))]); - setSourceMapRange(statement, node); + ts.setSourceMapRange(statement, node); statements.push(statement); } function visitEnumDeclaration(node) { @@ -36397,6 +37802,7 @@ var ts; } var statements = []; var emitFlags = 64; + recordEmittedDeclarationInScope(node); if (shouldEmitVarForEnumDeclaration(node)) { addVarForEnumOrModuleDeclaration(statements, node); if (moduleKind !== ts.ModuleKind.System || currentScope !== currentSourceFile) { @@ -36408,7 +37814,7 @@ var ts; var exportName = getExportName(node); var enumStatement = ts.createStatement(ts.createCall(ts.createFunctionExpression(undefined, undefined, undefined, [ts.createParameter(parameterName)], undefined, transformEnumBody(node, containerName)), undefined, [ts.createLogicalOr(exportName, ts.createAssignment(exportName, ts.createObjectLiteral()))]), node); ts.setOriginalNode(enumStatement, node); - setNodeEmitFlags(enumStatement, emitFlags); + ts.setEmitFlags(enumStatement, emitFlags); statements.push(enumStatement); if (isNamespaceExport(node)) { addVarForEnumExportedFromNamespace(statements, node); @@ -36447,18 +37853,32 @@ var ts; function shouldEmitModuleDeclaration(node) { return ts.isInstantiatedModule(node, compilerOptions.preserveConstEnums || compilerOptions.isolatedModules); } - function isModuleMergedWithES6Class(node) { - return languageVersion === 2 - && ts.isMergedWithClass(node); - } function isES6ExportedDeclaration(node) { return isExternalModuleExport(node) && moduleKind === ts.ModuleKind.ES6; } + function recordEmittedDeclarationInScope(node) { + var name = node.symbol && node.symbol.name; + if (name) { + if (!currentScopeFirstDeclarationsOfName) { + currentScopeFirstDeclarationsOfName = ts.createMap(); + } + if (!(name in currentScopeFirstDeclarationsOfName)) { + currentScopeFirstDeclarationsOfName[name] = node; + } + } + } + function isFirstEmittedDeclarationInScope(node) { + if (currentScopeFirstDeclarationsOfName) { + var name_31 = node.symbol && node.symbol.name; + if (name_31) { + return currentScopeFirstDeclarationsOfName[name_31] === node; + } + } + return false; + } function shouldEmitVarForModuleDeclaration(node) { - return !isModuleMergedWithES6Class(node) - && (!isES6ExportedDeclaration(node) - || ts.isFirstDeclarationOfKind(node, node.kind)); + return isFirstEmittedDeclarationInScope(node); } function addVarForEnumOrModuleDeclaration(statements, node) { var statement = ts.createVariableStatement(isES6ExportedDeclaration(node) @@ -36468,13 +37888,13 @@ var ts; ]); ts.setOriginalNode(statement, node); if (node.kind === 224) { - setSourceMapRange(statement.declarationList, node); + ts.setSourceMapRange(statement.declarationList, node); } else { - setSourceMapRange(statement, node); + ts.setSourceMapRange(statement, node); } - setCommentRange(statement, node); - setNodeEmitFlags(statement, 32768); + ts.setCommentRange(statement, node); + ts.setEmitFlags(statement, 32768); statements.push(statement); } function visitModuleDeclaration(node) { @@ -36485,6 +37905,7 @@ var ts; enableSubstitutionForNamespaceExports(); var statements = []; var emitFlags = 64; + recordEmittedDeclarationInScope(node); if (shouldEmitVarForModuleDeclaration(node)) { addVarForEnumOrModuleDeclaration(statements, node); if (moduleKind !== ts.ModuleKind.System || currentScope !== currentSourceFile) { @@ -36501,15 +37922,17 @@ var ts; } var moduleStatement = ts.createStatement(ts.createCall(ts.createFunctionExpression(undefined, undefined, undefined, [ts.createParameter(parameterName)], undefined, transformModuleBody(node, containerName)), undefined, [moduleArg]), node); ts.setOriginalNode(moduleStatement, node); - setNodeEmitFlags(moduleStatement, emitFlags); + ts.setEmitFlags(moduleStatement, emitFlags); statements.push(moduleStatement); return statements; } function transformModuleBody(node, namespaceLocalName) { var savedCurrentNamespaceContainerName = currentNamespaceContainerName; var savedCurrentNamespace = currentNamespace; + var savedCurrentScopeFirstDeclarationsOfName = currentScopeFirstDeclarationsOfName; currentNamespaceContainerName = namespaceLocalName; currentNamespace = node; + currentScopeFirstDeclarationsOfName = undefined; var statements = []; startLexicalEnvironment(); var statementsLocation; @@ -36536,9 +37959,10 @@ var ts; ts.addRange(statements, endLexicalEnvironment()); currentNamespaceContainerName = savedCurrentNamespaceContainerName; currentNamespace = savedCurrentNamespace; + currentScopeFirstDeclarationsOfName = savedCurrentScopeFirstDeclarationsOfName; var block = ts.createBlock(ts.createNodeArray(statements, statementsLocation), blockLocation, true); if (body.kind !== 226) { - setNodeEmitFlags(block, block.emitFlags | 49152); + ts.setEmitFlags(block, ts.getEmitFlags(block) | 49152); } return block; } @@ -36561,7 +37985,7 @@ var ts; return undefined; } var moduleReference = ts.createExpressionFromEntityName(node.moduleReference); - setNodeEmitFlags(moduleReference, 49152 | 65536); + ts.setEmitFlags(moduleReference, 49152 | 65536); if (isNamedExternalModuleExport(node) || !isNamespaceExport(node)) { return ts.setOriginalNode(ts.createVariableStatement(ts.visitNodes(node.modifiers, visitor, ts.isModifier), ts.createVariableDeclarationList([ ts.createVariableDeclaration(node.name, undefined, moduleReference) @@ -36590,9 +38014,9 @@ var ts; } function addExportMemberAssignment(statements, node) { var expression = ts.createAssignment(getExportName(node), getLocalName(node, true)); - setSourceMapRange(expression, ts.createRange(node.name.pos, node.end)); + ts.setSourceMapRange(expression, ts.createRange(node.name.pos, node.end)); var statement = ts.createStatement(expression); - setSourceMapRange(statement, ts.createRange(-1, node.end)); + ts.setSourceMapRange(statement, ts.createRange(-1, node.end)); statements.push(statement); } function createNamespaceExport(exportName, exportValue, location) { @@ -36613,7 +38037,7 @@ var ts; emitFlags |= 1536; } if (emitFlags) { - setNodeEmitFlags(qualifiedName, emitFlags); + ts.setEmitFlags(qualifiedName, emitFlags); } return qualifiedName; } @@ -36622,7 +38046,7 @@ var ts; } function getNamespaceParameterName(node) { var name = ts.getGeneratedNameForNode(node); - setSourceMapRange(name, node.name); + ts.setSourceMapRange(name, node.name); return name; } function getNamespaceContainerName(node) { @@ -36639,8 +38063,8 @@ var ts; } function getDeclarationName(node, allowComments, allowSourceMaps, emitFlags) { if (node.name) { - var name_30 = ts.getMutableClone(node.name); - emitFlags |= getNodeEmitFlags(node.name); + var name_32 = ts.getMutableClone(node.name); + emitFlags |= ts.getEmitFlags(node.name); if (!allowSourceMaps) { emitFlags |= 1536; } @@ -36648,9 +38072,9 @@ var ts; emitFlags |= 49152; } if (emitFlags) { - setNodeEmitFlags(name_30, emitFlags); + ts.setEmitFlags(name_32, emitFlags); } - return name_30; + return name_32; } else { return ts.getGeneratedNameForNode(node); @@ -36712,7 +38136,7 @@ var ts; function isTransformedEnumDeclaration(node) { return ts.getOriginalNode(node).kind === 224; } - function onEmitNode(node, emit) { + function onEmitNode(emitContext, node, emitCallback) { var savedApplicableSubstitutions = applicableSubstitutions; var savedCurrentSuperContainer = currentSuperContainer; if (enabledSubstitutions & 4 && isSuperContainer(node)) { @@ -36724,13 +38148,13 @@ var ts; if (enabledSubstitutions & 8 && isTransformedEnumDeclaration(node)) { applicableSubstitutions |= 8; } - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); applicableSubstitutions = savedApplicableSubstitutions; currentSuperContainer = savedCurrentSuperContainer; } - function onSubstituteNode(node, isExpression) { - node = previousOnSubstituteNode(node, isExpression); - if (isExpression) { + function onSubstituteNode(emitContext, node) { + node = previousOnSubstituteNode(emitContext, node); + if (emitContext === 1) { return substituteExpression(node); } else if (ts.isShorthandPropertyAssignment(node)) { @@ -36740,14 +38164,14 @@ var ts; } function substituteShorthandPropertyAssignment(node) { if (enabledSubstitutions & 2) { - var name_31 = node.name; - var exportedName = trySubstituteNamespaceExportedName(name_31); + var name_33 = node.name; + var exportedName = trySubstituteNamespaceExportedName(name_33); if (exportedName) { if (node.objectAssignmentInitializer) { var initializer = ts.createAssignment(exportedName, node.objectAssignmentInitializer); - return ts.createPropertyAssignment(name_31, initializer, node); + return ts.createPropertyAssignment(name_33, initializer, node); } - return ts.createPropertyAssignment(name_31, exportedName, node); + return ts.createPropertyAssignment(name_33, exportedName, node); } } return node; @@ -36756,16 +38180,15 @@ var ts; switch (node.kind) { case 69: return substituteExpressionIdentifier(node); - } - if (enabledSubstitutions & 4) { - switch (node.kind) { - case 174: + case 172: + return substitutePropertyAccessExpression(node); + case 173: + return substituteElementAccessExpression(node); + case 174: + if (enabledSubstitutions & 4) { return substituteCallExpression(node); - case 172: - return substitutePropertyAccessExpression(node); - case 173: - return substituteElementAccessExpression(node); - } + } + break; } return node; } @@ -36782,8 +38205,8 @@ var ts; var classAlias = classAliases[declaration.id]; if (classAlias) { var clone_4 = ts.getSynthesizedClone(classAlias); - setSourceMapRange(clone_4, node); - setCommentRange(clone_4, node); + ts.setSourceMapRange(clone_4, node); + ts.setCommentRange(clone_4, node); return clone_4; } } @@ -36792,7 +38215,7 @@ var ts; return undefined; } function trySubstituteNamespaceExportedName(node) { - if (enabledSubstitutions & applicableSubstitutions && (getNodeEmitFlags(node) & 262144) === 0) { + if (enabledSubstitutions & applicableSubstitutions && (ts.getEmitFlags(node) & 262144) === 0) { var container = resolver.getReferencedExportContainer(node, false); if (container) { var substitute = (applicableSubstitutions & 2 && container.kind === 225) || @@ -36820,23 +38243,48 @@ var ts; return node; } function substitutePropertyAccessExpression(node) { - if (node.expression.kind === 95) { + if (enabledSubstitutions & 4 && node.expression.kind === 95) { var flags = getSuperContainerAsyncMethodFlags(); if (flags) { return createSuperAccessInAsyncMethod(ts.createLiteral(node.name.text), flags, node); } } - return node; + return substituteConstantValue(node); } function substituteElementAccessExpression(node) { - if (node.expression.kind === 95) { + if (enabledSubstitutions & 4 && node.expression.kind === 95) { var flags = getSuperContainerAsyncMethodFlags(); if (flags) { return createSuperAccessInAsyncMethod(node.argumentExpression, flags, node); } } + return substituteConstantValue(node); + } + function substituteConstantValue(node) { + var constantValue = tryGetConstEnumValue(node); + if (constantValue !== undefined) { + var substitute = ts.createLiteral(constantValue); + ts.setSourceMapRange(substitute, node); + ts.setCommentRange(substitute, node); + if (!compilerOptions.removeComments) { + var propertyName = ts.isPropertyAccessExpression(node) + ? ts.declarationNameToString(node.name) + : ts.getTextOfNode(node.argumentExpression); + substitute.trailingComment = " " + propertyName + " "; + } + ts.setConstantValue(node, constantValue); + return substitute; + } return node; } + function tryGetConstEnumValue(node) { + if (compilerOptions.isolatedModules) { + return undefined; + } + return ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node) + ? resolver.getConstantValue(node) + : undefined; + } function createSuperAccessInAsyncMethod(argumentExpression, flags, location) { if (flags & 4096) { return ts.createPropertyAccess(ts.createCall(ts.createIdentifier("_super"), undefined, [argumentExpression]), "value", location); @@ -36860,6 +38308,9 @@ var ts; var currentSourceFile; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } currentSourceFile = node; node = ts.visitEachChild(node, visitor, context); currentSourceFile = undefined; @@ -37018,12 +38469,12 @@ var ts; return getTagName(node.openingElement); } else { - var name_32 = node.tagName; - if (ts.isIdentifier(name_32) && ts.isIntrinsicJsxName(name_32.text)) { - return ts.createLiteral(name_32.text); + var name_34 = node.tagName; + if (ts.isIdentifier(name_34) && ts.isIntrinsicJsxName(name_34.text)) { + return ts.createLiteral(name_34.text); } else { - return ts.createExpressionFromEntityName(name_32); + return ts.createExpressionFromEntityName(name_34); } } } @@ -37305,6 +38756,9 @@ var ts; var hoistVariableDeclaration = context.hoistVariableDeclaration; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } return ts.visitEachChild(node, visitor, context); } function visitor(node) { @@ -37364,7 +38818,7 @@ var ts; var ts; (function (ts) { function transformES6(context) { - var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration, getNodeEmitFlags = context.getNodeEmitFlags, setNodeEmitFlags = context.setNodeEmitFlags, getCommentRange = context.getCommentRange, setCommentRange = context.setCommentRange, getSourceMapRange = context.getSourceMapRange, setSourceMapRange = context.setSourceMapRange, setTokenSourceMapRange = context.setTokenSourceMapRange; + var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration; var resolver = context.getEmitResolver(); var previousOnSubstituteNode = context.onSubstituteNode; var previousOnEmitNode = context.onEmitNode; @@ -37384,6 +38838,9 @@ var ts; var enabledSubstitutions; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } currentSourceFile = node; currentText = node.text; return ts.visitNode(node, visitor, ts.isSourceFile); @@ -37553,7 +39010,7 @@ var ts; enclosingFunction = currentNode; if (currentNode.kind !== 180) { enclosingNonArrowFunction = currentNode; - if (!(currentNode.emitFlags & 2097152)) { + if (!(ts.getEmitFlags(currentNode) & 2097152)) { enclosingNonAsyncFunctionBody = currentNode; } } @@ -37690,15 +39147,15 @@ var ts; } var extendsClauseElement = ts.getClassExtendsHeritageClauseElement(node); var classFunction = ts.createFunctionExpression(undefined, undefined, undefined, extendsClauseElement ? [ts.createParameter("_super")] : [], undefined, transformClassBody(node, extendsClauseElement)); - if (getNodeEmitFlags(node) & 524288) { - setNodeEmitFlags(classFunction, 524288); + if (ts.getEmitFlags(node) & 524288) { + ts.setEmitFlags(classFunction, 524288); } var inner = ts.createPartiallyEmittedExpression(classFunction); inner.end = node.end; - setNodeEmitFlags(inner, 49152); + ts.setEmitFlags(inner, 49152); var outer = ts.createPartiallyEmittedExpression(inner); outer.end = ts.skipTrivia(currentText, node.pos); - setNodeEmitFlags(outer, 49152); + ts.setEmitFlags(outer, 49152); return ts.createParen(ts.createCall(outer, undefined, extendsClauseElement ? [ts.visitNode(extendsClauseElement.expression, visitor, ts.isExpression)] : [])); @@ -37713,14 +39170,14 @@ var ts; var localName = getLocalName(node); var outer = ts.createPartiallyEmittedExpression(localName); outer.end = closingBraceLocation.end; - setNodeEmitFlags(outer, 49152); + ts.setEmitFlags(outer, 49152); var statement = ts.createReturn(outer); statement.pos = closingBraceLocation.pos; - setNodeEmitFlags(statement, 49152 | 12288); + ts.setEmitFlags(statement, 49152 | 12288); statements.push(statement); ts.addRange(statements, endLexicalEnvironment()); var block = ts.createBlock(ts.createNodeArray(statements, node.members), undefined, true); - setNodeEmitFlags(block, 49152); + ts.setEmitFlags(block, 49152); return block; } function addExtendsHelperIfNeeded(statements, node, extendsClauseElement) { @@ -37731,7 +39188,11 @@ var ts; function addConstructor(statements, node, extendsClauseElement) { var constructor = ts.getFirstConstructorWithBody(node); var hasSynthesizedSuper = hasSynthesizedDefaultSuperCall(constructor, extendsClauseElement !== undefined); - statements.push(ts.createFunctionDeclaration(undefined, undefined, undefined, getDeclarationName(node), undefined, transformConstructorParameters(constructor, hasSynthesizedSuper), undefined, transformConstructorBody(constructor, node, extendsClauseElement, hasSynthesizedSuper), constructor || node)); + var constructorFunction = ts.createFunctionDeclaration(undefined, undefined, undefined, getDeclarationName(node), undefined, transformConstructorParameters(constructor, hasSynthesizedSuper), undefined, transformConstructorBody(constructor, node, extendsClauseElement, hasSynthesizedSuper), constructor || node); + if (extendsClauseElement) { + ts.setEmitFlags(constructorFunction, 256); + } + statements.push(constructorFunction); } function transformConstructorParameters(constructor, hasSynthesizedSuper) { if (constructor && !hasSynthesizedSuper) { @@ -37742,33 +39203,98 @@ var ts; function transformConstructorBody(constructor, node, extendsClauseElement, hasSynthesizedSuper) { var statements = []; startLexicalEnvironment(); + var statementOffset = -1; + if (hasSynthesizedSuper) { + statementOffset = 1; + } + else if (constructor) { + statementOffset = ts.addPrologueDirectives(statements, constructor.body.statements, false, visitor); + } if (constructor) { - addCaptureThisForNodeIfNeeded(statements, constructor); addDefaultValueAssignmentsIfNeeded(statements, constructor); addRestParameterIfNeeded(statements, constructor, hasSynthesizedSuper); + ts.Debug.assert(statementOffset >= 0, "statementOffset not initialized correctly!"); + } + var superCaptureStatus = declareOrCaptureOrReturnThisForConstructorIfNeeded(statements, constructor, !!extendsClauseElement, hasSynthesizedSuper, statementOffset); + if (superCaptureStatus === 1 || superCaptureStatus === 2) { + statementOffset++; } - addDefaultSuperCallIfNeeded(statements, constructor, extendsClauseElement, hasSynthesizedSuper); if (constructor) { - var body = saveStateAndInvoke(constructor, hasSynthesizedSuper ? transformConstructorBodyWithSynthesizedSuper : transformConstructorBodyWithoutSynthesizedSuper); + var body = saveStateAndInvoke(constructor, function (constructor) { return ts.visitNodes(constructor.body.statements, visitor, ts.isStatement, statementOffset); }); ts.addRange(statements, body); } + if (extendsClauseElement + && superCaptureStatus !== 2 + && !(constructor && isSufficientlyCoveredByReturnStatements(constructor.body))) { + statements.push(ts.createReturn(ts.createIdentifier("_this"))); + } ts.addRange(statements, endLexicalEnvironment()); var block = ts.createBlock(ts.createNodeArray(statements, constructor ? constructor.body.statements : node.members), constructor ? constructor.body : node, true); if (!constructor) { - setNodeEmitFlags(block, 49152); + ts.setEmitFlags(block, 49152); } return block; } - function transformConstructorBodyWithSynthesizedSuper(node) { - return ts.visitNodes(node.body.statements, visitor, ts.isStatement, 1); - } - function transformConstructorBodyWithoutSynthesizedSuper(node) { - return ts.visitNodes(node.body.statements, visitor, ts.isStatement, 0); + function isSufficientlyCoveredByReturnStatements(statement) { + if (statement.kind === 211) { + return true; + } + else if (statement.kind === 203) { + var ifStatement = statement; + if (ifStatement.elseStatement) { + return isSufficientlyCoveredByReturnStatements(ifStatement.thenStatement) && + isSufficientlyCoveredByReturnStatements(ifStatement.elseStatement); + } + } + else if (statement.kind === 199) { + var lastStatement = ts.lastOrUndefined(statement.statements); + if (lastStatement && isSufficientlyCoveredByReturnStatements(lastStatement)) { + return true; + } + } + return false; } - function addDefaultSuperCallIfNeeded(statements, constructor, extendsClauseElement, hasSynthesizedSuper) { - if (constructor ? hasSynthesizedSuper : extendsClauseElement) { - statements.push(ts.createStatement(ts.createFunctionApply(ts.createIdentifier("_super"), ts.createThis(), ts.createIdentifier("arguments")), extendsClauseElement)); + function declareOrCaptureOrReturnThisForConstructorIfNeeded(statements, ctor, hasExtendsClause, hasSynthesizedSuper, statementOffset) { + if (!hasExtendsClause) { + if (ctor) { + addCaptureThisForNodeIfNeeded(statements, ctor); + } + return 0; + } + if (!ctor) { + statements.push(ts.createReturn(createDefaultSuperCallOrThis())); + return 2; + } + if (hasSynthesizedSuper) { + captureThisForNode(statements, ctor, createDefaultSuperCallOrThis()); + enableSubstitutionsForCapturedThis(); + return 1; + } + var firstStatement; + var superCallExpression; + var ctorStatements = ctor.body.statements; + if (statementOffset < ctorStatements.length) { + firstStatement = ctorStatements[statementOffset]; + if (firstStatement.kind === 202 && ts.isSuperCall(firstStatement.expression)) { + var superCall = firstStatement.expression; + superCallExpression = ts.setOriginalNode(saveStateAndInvoke(superCall, visitImmediateSuperCallInBody), superCall); + } + } + if (superCallExpression && statementOffset === ctorStatements.length - 1) { + statements.push(ts.createReturn(superCallExpression)); + return 2; + } + captureThisForNode(statements, ctor, superCallExpression, firstStatement); + if (superCallExpression) { + return 1; } + return 0; + } + function createDefaultSuperCallOrThis() { + var actualThis = ts.createThis(); + ts.setEmitFlags(actualThis, 128); + var superCall = ts.createFunctionApply(ts.createIdentifier("_super"), actualThis, ts.createIdentifier("arguments")); + return ts.createLogicalOr(superCall, actualThis); } function visitParameter(node) { if (node.dotDotDotToken) { @@ -37793,34 +39319,34 @@ var ts; } for (var _i = 0, _a = node.parameters; _i < _a.length; _i++) { var parameter = _a[_i]; - var name_33 = parameter.name, initializer = parameter.initializer, dotDotDotToken = parameter.dotDotDotToken; + var name_35 = parameter.name, initializer = parameter.initializer, dotDotDotToken = parameter.dotDotDotToken; if (dotDotDotToken) { continue; } - if (ts.isBindingPattern(name_33)) { - addDefaultValueAssignmentForBindingPattern(statements, parameter, name_33, initializer); + if (ts.isBindingPattern(name_35)) { + addDefaultValueAssignmentForBindingPattern(statements, parameter, name_35, initializer); } else if (initializer) { - addDefaultValueAssignmentForInitializer(statements, parameter, name_33, initializer); + addDefaultValueAssignmentForInitializer(statements, parameter, name_35, initializer); } } } function addDefaultValueAssignmentForBindingPattern(statements, parameter, name, initializer) { var temp = ts.getGeneratedNameForNode(parameter); if (name.elements.length > 0) { - statements.push(setNodeEmitFlags(ts.createVariableStatement(undefined, ts.createVariableDeclarationList(ts.flattenParameterDestructuring(context, parameter, temp, visitor))), 8388608)); + statements.push(ts.setEmitFlags(ts.createVariableStatement(undefined, ts.createVariableDeclarationList(ts.flattenParameterDestructuring(context, parameter, temp, visitor))), 8388608)); } else if (initializer) { - statements.push(setNodeEmitFlags(ts.createStatement(ts.createAssignment(temp, ts.visitNode(initializer, visitor, ts.isExpression))), 8388608)); + statements.push(ts.setEmitFlags(ts.createStatement(ts.createAssignment(temp, ts.visitNode(initializer, visitor, ts.isExpression))), 8388608)); } } function addDefaultValueAssignmentForInitializer(statements, parameter, name, initializer) { initializer = ts.visitNode(initializer, visitor, ts.isExpression); - var statement = ts.createIf(ts.createStrictEquality(ts.getSynthesizedClone(name), ts.createVoidZero()), setNodeEmitFlags(ts.createBlock([ - ts.createStatement(ts.createAssignment(setNodeEmitFlags(ts.getMutableClone(name), 1536), setNodeEmitFlags(initializer, 1536 | getNodeEmitFlags(initializer)), parameter)) + var statement = ts.createIf(ts.createStrictEquality(ts.getSynthesizedClone(name), ts.createVoidZero()), ts.setEmitFlags(ts.createBlock([ + ts.createStatement(ts.createAssignment(ts.setEmitFlags(ts.getMutableClone(name), 1536), ts.setEmitFlags(initializer, 1536 | ts.getEmitFlags(initializer)), parameter)) ], parameter), 32 | 1024 | 12288), undefined, parameter); statement.startsOnNewLine = true; - setNodeEmitFlags(statement, 12288 | 1024 | 8388608); + ts.setEmitFlags(statement, 12288 | 1024 | 8388608); statements.push(statement); } function shouldAddRestParameter(node, inConstructorWithSynthesizedSuper) { @@ -37832,11 +39358,11 @@ var ts; return; } var declarationName = ts.getMutableClone(parameter.name); - setNodeEmitFlags(declarationName, 1536); + ts.setEmitFlags(declarationName, 1536); var expressionName = ts.getSynthesizedClone(parameter.name); var restIndex = node.parameters.length - 1; var temp = ts.createLoopVariable(); - statements.push(setNodeEmitFlags(ts.createVariableStatement(undefined, ts.createVariableDeclarationList([ + statements.push(ts.setEmitFlags(ts.createVariableStatement(undefined, ts.createVariableDeclarationList([ ts.createVariableDeclaration(declarationName, undefined, ts.createArrayLiteral([])) ]), parameter), 8388608)); var forStatement = ts.createFor(ts.createVariableDeclarationList([ @@ -37844,21 +39370,24 @@ var ts; ], parameter), ts.createLessThan(temp, ts.createPropertyAccess(ts.createIdentifier("arguments"), "length"), parameter), ts.createPostfixIncrement(temp, parameter), ts.createBlock([ ts.startOnNewLine(ts.createStatement(ts.createAssignment(ts.createElementAccess(expressionName, ts.createSubtract(temp, ts.createLiteral(restIndex))), ts.createElementAccess(ts.createIdentifier("arguments"), temp)), parameter)) ])); - setNodeEmitFlags(forStatement, 8388608); + ts.setEmitFlags(forStatement, 8388608); ts.startOnNewLine(forStatement); statements.push(forStatement); } function addCaptureThisForNodeIfNeeded(statements, node) { if (node.transformFlags & 16384 && node.kind !== 180) { - enableSubstitutionsForCapturedThis(); - var captureThisStatement = ts.createVariableStatement(undefined, ts.createVariableDeclarationList([ - ts.createVariableDeclaration("_this", undefined, ts.createThis()) - ])); - setNodeEmitFlags(captureThisStatement, 49152 | 8388608); - setSourceMapRange(captureThisStatement, node); - statements.push(captureThisStatement); + captureThisForNode(statements, node, ts.createThis()); } } + function captureThisForNode(statements, node, initializer, originalStatement) { + enableSubstitutionsForCapturedThis(); + var captureThisStatement = ts.createVariableStatement(undefined, ts.createVariableDeclarationList([ + ts.createVariableDeclaration("_this", undefined, initializer) + ]), originalStatement); + ts.setEmitFlags(captureThisStatement, 49152 | 8388608); + ts.setSourceMapRange(captureThisStatement, node); + statements.push(captureThisStatement); + } function addClassMembers(statements, node) { for (var _i = 0, _a = node.members; _i < _a.length; _i++) { var member = _a[_i]; @@ -37888,43 +39417,43 @@ var ts; return ts.createEmptyStatement(member); } function transformClassMethodDeclarationToStatement(receiver, member) { - var commentRange = getCommentRange(member); - var sourceMapRange = getSourceMapRange(member); + var commentRange = ts.getCommentRange(member); + var sourceMapRange = ts.getSourceMapRange(member); var func = transformFunctionLikeToExpression(member, member, undefined); - setNodeEmitFlags(func, 49152); - setSourceMapRange(func, sourceMapRange); + ts.setEmitFlags(func, 49152); + ts.setSourceMapRange(func, sourceMapRange); var statement = ts.createStatement(ts.createAssignment(ts.createMemberAccessForPropertyName(receiver, ts.visitNode(member.name, visitor, ts.isPropertyName), member.name), func), member); ts.setOriginalNode(statement, member); - setCommentRange(statement, commentRange); - setNodeEmitFlags(statement, 1536); + ts.setCommentRange(statement, commentRange); + ts.setEmitFlags(statement, 1536); return statement; } function transformAccessorsToStatement(receiver, accessors) { - var statement = ts.createStatement(transformAccessorsToExpression(receiver, accessors, false), getSourceMapRange(accessors.firstAccessor)); - setNodeEmitFlags(statement, 49152); + var statement = ts.createStatement(transformAccessorsToExpression(receiver, accessors, false), ts.getSourceMapRange(accessors.firstAccessor)); + ts.setEmitFlags(statement, 49152); return statement; } function transformAccessorsToExpression(receiver, _a, startsOnNewLine) { var firstAccessor = _a.firstAccessor, getAccessor = _a.getAccessor, setAccessor = _a.setAccessor; var target = ts.getMutableClone(receiver); - setNodeEmitFlags(target, 49152 | 1024); - setSourceMapRange(target, firstAccessor.name); + ts.setEmitFlags(target, 49152 | 1024); + ts.setSourceMapRange(target, firstAccessor.name); var propertyName = ts.createExpressionForPropertyName(ts.visitNode(firstAccessor.name, visitor, ts.isPropertyName)); - setNodeEmitFlags(propertyName, 49152 | 512); - setSourceMapRange(propertyName, firstAccessor.name); + ts.setEmitFlags(propertyName, 49152 | 512); + ts.setSourceMapRange(propertyName, firstAccessor.name); var properties = []; if (getAccessor) { var getterFunction = transformFunctionLikeToExpression(getAccessor, undefined, undefined); - setSourceMapRange(getterFunction, getSourceMapRange(getAccessor)); + ts.setSourceMapRange(getterFunction, ts.getSourceMapRange(getAccessor)); var getter = ts.createPropertyAssignment("get", getterFunction); - setCommentRange(getter, getCommentRange(getAccessor)); + ts.setCommentRange(getter, ts.getCommentRange(getAccessor)); properties.push(getter); } if (setAccessor) { var setterFunction = transformFunctionLikeToExpression(setAccessor, undefined, undefined); - setSourceMapRange(setterFunction, getSourceMapRange(setAccessor)); + ts.setSourceMapRange(setterFunction, ts.getSourceMapRange(setAccessor)); var setter = ts.createPropertyAssignment("set", setterFunction); - setCommentRange(setter, getCommentRange(setAccessor)); + ts.setCommentRange(setter, ts.getCommentRange(setAccessor)); properties.push(setter); } properties.push(ts.createPropertyAssignment("enumerable", ts.createLiteral(true)), ts.createPropertyAssignment("configurable", ts.createLiteral(true))); @@ -37943,7 +39472,7 @@ var ts; enableSubstitutionsForCapturedThis(); } var func = transformFunctionLikeToExpression(node, node, undefined); - setNodeEmitFlags(func, 256); + ts.setEmitFlags(func, 256); return func; } function visitFunctionExpression(node) { @@ -38000,7 +39529,7 @@ var ts; } var expression = ts.visitNode(body, visitor, ts.isExpression); var returnStatement = ts.createReturn(expression, body); - setNodeEmitFlags(returnStatement, 12288 | 1024 | 32768); + ts.setEmitFlags(returnStatement, 12288 | 1024 | 32768); statements.push(returnStatement); closeBraceLocation = body; } @@ -38011,10 +39540,10 @@ var ts; } var block = ts.createBlock(ts.createNodeArray(statements, statementsLocation), node.body, multiLine); if (!multiLine && singleLine) { - setNodeEmitFlags(block, 32); + ts.setEmitFlags(block, 32); } if (closeBraceLocation) { - setTokenSourceMapRange(block, 16, closeBraceLocation); + ts.setTokenSourceMapRange(block, 16, closeBraceLocation); } ts.setOriginalNode(block, node.body); return block; @@ -38055,7 +39584,7 @@ var ts; assignment = ts.flattenVariableDestructuringToExpression(context, decl, hoistVariableDeclaration, undefined, visitor); } else { - assignment = ts.createBinary(decl.name, 56, decl.initializer); + assignment = ts.createBinary(decl.name, 56, ts.visitNode(decl.initializer, visitor, ts.isExpression)); } (assignments || (assignments = [])).push(assignment); } @@ -38078,13 +39607,13 @@ var ts; : visitVariableDeclaration)); var declarationList = ts.createVariableDeclarationList(declarations, node); ts.setOriginalNode(declarationList, node); - setCommentRange(declarationList, node); + ts.setCommentRange(declarationList, node); if (node.transformFlags & 2097152 && (ts.isBindingPattern(node.declarations[0].name) || ts.isBindingPattern(ts.lastOrUndefined(node.declarations).name))) { var firstDeclaration = ts.firstOrUndefined(declarations); var lastDeclaration = ts.lastOrUndefined(declarations); - setSourceMapRange(declarationList, ts.createRange(firstDeclaration.pos, lastDeclaration.end)); + ts.setSourceMapRange(declarationList, ts.createRange(firstDeclaration.pos, lastDeclaration.end)); } return declarationList; } @@ -38179,7 +39708,7 @@ var ts; ts.setOriginalNode(declarationList, initializer); var firstDeclaration = declarations[0]; var lastDeclaration = ts.lastOrUndefined(declarations); - setSourceMapRange(declarationList, ts.createRange(firstDeclaration.pos, lastDeclaration.end)); + ts.setSourceMapRange(declarationList, ts.createRange(firstDeclaration.pos, lastDeclaration.end)); statements.push(ts.createVariableStatement(undefined, declarationList)); } else { @@ -38214,14 +39743,14 @@ var ts; statements.push(statement); } } - setNodeEmitFlags(expression, 1536 | getNodeEmitFlags(expression)); + ts.setEmitFlags(expression, 1536 | ts.getEmitFlags(expression)); var body = ts.createBlock(ts.createNodeArray(statements, statementsLocation), bodyLocation); - setNodeEmitFlags(body, 1536 | 12288); + ts.setEmitFlags(body, 1536 | 12288); var forStatement = ts.createFor(ts.createVariableDeclarationList([ ts.createVariableDeclaration(counter, undefined, ts.createLiteral(0), ts.moveRangePos(node.expression, -1)), ts.createVariableDeclaration(rhsReference, undefined, expression, node.expression) ], node.expression), ts.createLessThan(counter, ts.createPropertyAccess(rhsReference, "length"), node.expression), ts.createPostfixIncrement(counter, node.expression), body, node); - setNodeEmitFlags(forStatement, 8192); + ts.setEmitFlags(forStatement, 8192); return forStatement; } function visitObjectLiteralExpression(node) { @@ -38239,7 +39768,7 @@ var ts; ts.Debug.assert(numInitialProperties !== numProperties); var temp = ts.createTempVariable(hoistVariableDeclaration); var expressions = []; - var assignment = ts.createAssignment(temp, setNodeEmitFlags(ts.createObjectLiteral(ts.visitNodes(properties, visitor, ts.isObjectLiteralElementLike, 0, numInitialProperties), undefined, node.multiLine), 524288)); + var assignment = ts.createAssignment(temp, ts.setEmitFlags(ts.createObjectLiteral(ts.visitNodes(properties, visitor, ts.isObjectLiteralElementLike, 0, numInitialProperties), undefined, node.multiLine), 524288)); if (node.multiLine) { assignment.startsOnNewLine = true; } @@ -38328,7 +39857,7 @@ var ts; loopBody = ts.createBlock([loopBody], undefined, true); } var isAsyncBlockContainingAwait = enclosingNonArrowFunction - && (enclosingNonArrowFunction.emitFlags & 2097152) !== 0 + && (ts.getEmitFlags(enclosingNonArrowFunction) & 2097152) !== 0 && (node.statement.transformFlags & 4194304) !== 0; var loopBodyFlags = 0; if (currentState.containsLexicalThis) { @@ -38338,7 +39867,7 @@ var ts; loopBodyFlags |= 2097152; } var convertedLoopVariable = ts.createVariableStatement(undefined, ts.createVariableDeclarationList([ - ts.createVariableDeclaration(functionName, undefined, setNodeEmitFlags(ts.createFunctionExpression(isAsyncBlockContainingAwait ? ts.createToken(37) : undefined, undefined, undefined, loopParameters, undefined, loopBody), loopBodyFlags)) + ts.createVariableDeclaration(functionName, undefined, ts.setEmitFlags(ts.createFunctionExpression(isAsyncBlockContainingAwait ? ts.createToken(37) : undefined, undefined, undefined, loopParameters, undefined, loopBody), loopBodyFlags)) ])); var statements = [convertedLoopVariable]; var extraVariableDeclarations; @@ -38366,8 +39895,8 @@ var ts; if (!extraVariableDeclarations) { extraVariableDeclarations = []; } - for (var name_34 in currentState.hoistedLocalVariables) { - var identifier = currentState.hoistedLocalVariables[name_34]; + for (var _b = 0, _c = currentState.hoistedLocalVariables; _b < _c.length; _b++) { + var identifier = _c[_b]; extraVariableDeclarations.push(ts.createVariableDeclaration(identifier)); } } @@ -38376,8 +39905,8 @@ var ts; if (!extraVariableDeclarations) { extraVariableDeclarations = []; } - for (var _b = 0, loopOutParameters_1 = loopOutParameters; _b < loopOutParameters_1.length; _b++) { - var outParam = loopOutParameters_1[_b]; + for (var _d = 0, loopOutParameters_1 = loopOutParameters; _d < loopOutParameters_1.length; _d++) { + var outParam = loopOutParameters_1[_d]; extraVariableDeclarations.push(ts.createVariableDeclaration(outParam.outParamName)); } } @@ -38555,7 +40084,7 @@ var ts; function visitMethodDeclaration(node) { ts.Debug.assert(!ts.isComputedPropertyName(node.name)); var functionExpression = transformFunctionLikeToExpression(node, ts.moveRangePos(node, -1), undefined); - setNodeEmitFlags(functionExpression, 16384 | getNodeEmitFlags(functionExpression)); + ts.setEmitFlags(functionExpression, 16384 | ts.getEmitFlags(functionExpression)); return ts.createPropertyAssignment(node.name, functionExpression, node); } function visitShorthandPropertyAssignment(node) { @@ -38568,13 +40097,32 @@ var ts; return transformAndSpreadElements(node.elements, true, node.multiLine, node.elements.hasTrailingComma); } function visitCallExpression(node) { + return visitCallExpressionWithPotentialCapturedThisAssignment(node, true); + } + function visitImmediateSuperCallInBody(node) { + return visitCallExpressionWithPotentialCapturedThisAssignment(node, false); + } + function visitCallExpressionWithPotentialCapturedThisAssignment(node, assignToCapturedThis) { var _a = ts.createCallBinding(node.expression, hoistVariableDeclaration), target = _a.target, thisArg = _a.thisArg; + if (node.expression.kind === 95) { + ts.setEmitFlags(thisArg, 128); + } + var resultingCall; if (node.transformFlags & 262144) { - return ts.createFunctionApply(ts.visitNode(target, visitor, ts.isExpression), ts.visitNode(thisArg, visitor, ts.isExpression), transformAndSpreadElements(node.arguments, false, false, false)); + resultingCall = ts.createFunctionApply(ts.visitNode(target, visitor, ts.isExpression), ts.visitNode(thisArg, visitor, ts.isExpression), transformAndSpreadElements(node.arguments, false, false, false)); } else { - return ts.createFunctionCall(ts.visitNode(target, visitor, ts.isExpression), ts.visitNode(thisArg, visitor, ts.isExpression), ts.visitNodes(node.arguments, visitor, ts.isExpression), node); + resultingCall = ts.createFunctionCall(ts.visitNode(target, visitor, ts.isExpression), ts.visitNode(thisArg, visitor, ts.isExpression), ts.visitNodes(node.arguments, visitor, ts.isExpression), node); } + if (node.expression.kind === 95) { + var actualThis = ts.createThis(); + ts.setEmitFlags(actualThis, 128); + var initializer = ts.createLogicalOr(resultingCall, actualThis); + return assignToCapturedThis + ? ts.createAssignment(ts.createIdentifier("_this"), initializer) + : initializer; + } + return resultingCall; } function visitNewExpression(node) { ts.Debug.assert((node.transformFlags & 262144) !== 0); @@ -38694,12 +40242,12 @@ var ts; clone.statements = ts.createNodeArray(statements, node.statements); return clone; } - function onEmitNode(node, emit) { + function onEmitNode(emitContext, node, emitCallback) { var savedEnclosingFunction = enclosingFunction; if (enabledSubstitutions & 1 && ts.isFunctionLike(node)) { enclosingFunction = node; } - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); enclosingFunction = savedEnclosingFunction; } function enableSubstitutionsForBlockScopedBindings() { @@ -38721,9 +40269,9 @@ var ts; context.enableEmitNotification(220); } } - function onSubstituteNode(node, isExpression) { - node = previousOnSubstituteNode(node, isExpression); - if (isExpression) { + function onSubstituteNode(emitContext, node) { + node = previousOnSubstituteNode(emitContext, node); + if (emitContext === 1) { return substituteExpression(node); } if (ts.isIdentifier(node)) { @@ -38773,7 +40321,7 @@ var ts; function substituteThisKeyword(node) { if (enabledSubstitutions & 1 && enclosingFunction - && enclosingFunction.emitFlags & 256) { + && ts.getEmitFlags(enclosingFunction) & 256) { return ts.createIdentifier("_this", node); } return node; @@ -38783,8 +40331,8 @@ var ts; } function getDeclarationName(node, allowComments, allowSourceMaps, emitFlags) { if (node.name && !ts.isGeneratedIdentifier(node.name)) { - var name_35 = ts.getMutableClone(node.name); - emitFlags |= getNodeEmitFlags(node.name); + var name_36 = ts.getMutableClone(node.name); + emitFlags |= ts.getEmitFlags(node.name); if (!allowSourceMaps) { emitFlags |= 1536; } @@ -38792,9 +40340,9 @@ var ts; emitFlags |= 49152; } if (emitFlags) { - setNodeEmitFlags(name_35, emitFlags); + ts.setEmitFlags(name_36, emitFlags); } - return name_35; + return name_36; } return ts.getGeneratedNameForNode(node); } @@ -38842,7 +40390,7 @@ var ts; _a[7] = "endfinally", _a)); function transformGenerators(context) { - var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistFunctionDeclaration = context.hoistFunctionDeclaration, hoistVariableDeclaration = context.hoistVariableDeclaration, setSourceMapRange = context.setSourceMapRange, setCommentRange = context.setCommentRange, setNodeEmitFlags = context.setNodeEmitFlags; + var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistFunctionDeclaration = context.hoistFunctionDeclaration, hoistVariableDeclaration = context.hoistVariableDeclaration; var compilerOptions = context.getCompilerOptions(); var languageVersion = ts.getEmitScriptTarget(compilerOptions); var resolver = context.getEmitResolver(); @@ -38876,6 +40424,9 @@ var ts; var withBlockStack; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } if (node.transformFlags & 1024) { currentSourceFile = node; node = ts.visitEachChild(node, visitor, context); @@ -38982,7 +40533,7 @@ var ts; } } function visitFunctionDeclaration(node) { - if (node.asteriskToken && node.emitFlags & 2097152) { + if (node.asteriskToken && ts.getEmitFlags(node) & 2097152) { node = ts.setOriginalNode(ts.createFunctionDeclaration(undefined, undefined, undefined, node.name, undefined, node.parameters, undefined, transformGeneratorFunctionBody(node.body), node), node); } else { @@ -39003,7 +40554,7 @@ var ts; } } function visitFunctionExpression(node) { - if (node.asteriskToken && node.emitFlags & 2097152) { + if (node.asteriskToken && ts.getEmitFlags(node) & 2097152) { node = ts.setOriginalNode(ts.createFunctionExpression(undefined, node.name, undefined, node.parameters, undefined, transformGeneratorFunctionBody(node.body), node), node); } else { @@ -39034,6 +40585,7 @@ var ts; var savedBlocks = blocks; var savedBlockOffsets = blockOffsets; var savedBlockActions = blockActions; + var savedBlockStack = blockStack; var savedLabelOffsets = labelOffsets; var savedLabelExpressions = labelExpressions; var savedNextLabelId = nextLabelId; @@ -39046,6 +40598,7 @@ var ts; blocks = undefined; blockOffsets = undefined; blockActions = undefined; + blockStack = undefined; labelOffsets = undefined; labelExpressions = undefined; nextLabelId = 1; @@ -39064,6 +40617,7 @@ var ts; blocks = savedBlocks; blockOffsets = savedBlockOffsets; blockActions = savedBlockActions; + blockStack = savedBlockStack; labelOffsets = savedLabelOffsets; labelExpressions = savedLabelExpressions; nextLabelId = savedNextLabelId; @@ -39079,7 +40633,7 @@ var ts; return undefined; } else { - if (node.emitFlags & 8388608) { + if (ts.getEmitFlags(node) & 8388608) { return node; } for (var _i = 0, _a = node.declarationList.declarations; _i < _a.length; _i++) { @@ -39747,9 +41301,9 @@ var ts; } return -1; } - function onSubstituteNode(node, isExpression) { - node = previousOnSubstituteNode(node, isExpression); - if (isExpression) { + function onSubstituteNode(emitContext, node) { + node = previousOnSubstituteNode(emitContext, node); + if (emitContext === 1) { return substituteExpression(node); } return node; @@ -39766,11 +41320,11 @@ var ts; if (ts.isIdentifier(original) && original.parent) { var declaration = resolver.getReferencedValueDeclaration(original); if (declaration) { - var name_36 = ts.getProperty(renamedCatchVariableDeclarations, String(ts.getOriginalNodeId(declaration))); - if (name_36) { - var clone_8 = ts.getMutableClone(name_36); - setSourceMapRange(clone_8, node); - setCommentRange(clone_8, node); + var name_37 = ts.getProperty(renamedCatchVariableDeclarations, String(ts.getOriginalNodeId(declaration))); + if (name_37) { + var clone_8 = ts.getMutableClone(name_37); + ts.setSourceMapRange(clone_8, node); + ts.setCommentRange(clone_8, node); return clone_8; } } @@ -40164,7 +41718,7 @@ var ts; var buildResult = buildStatements(); return ts.createCall(ts.createHelperName(currentSourceFile.externalHelpersModuleName, "__generator"), undefined, [ ts.createThis(), - setNodeEmitFlags(ts.createFunctionExpression(undefined, undefined, undefined, [ts.createParameter(state)], undefined, ts.createBlock(buildResult, undefined, buildResult.length > 0)), 4194304) + ts.setEmitFlags(ts.createFunctionExpression(undefined, undefined, undefined, [ts.createParameter(state)], undefined, ts.createBlock(buildResult, undefined, buildResult.length > 0)), 4194304) ]); } function buildStatements() { @@ -40439,7 +41993,7 @@ var ts; _a[ts.ModuleKind.AMD] = transformAMDModule, _a[ts.ModuleKind.UMD] = transformUMDModule, _a)); - var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration, setNodeEmitFlags = context.setNodeEmitFlags, getNodeEmitFlags = context.getNodeEmitFlags, setSourceMapRange = context.setSourceMapRange; + var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration; var compilerOptions = context.getCompilerOptions(); var resolver = context.getEmitResolver(); var host = context.getEmitHost(); @@ -40464,6 +42018,9 @@ var ts; var hasExportStarsToExportValues; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } if (ts.isExternalModule(node) || compilerOptions.isolatedModules) { currentSourceFile = node; (_a = ts.collectExternalModuleInfo(node, resolver), externalImports = _a.externalImports, exportSpecifiers = _a.exportSpecifiers, exportEquals = _a.exportEquals, hasExportStarsToExportValues = _a.hasExportStarsToExportValues, _a); @@ -40489,7 +42046,7 @@ var ts; addExportEqualsIfNeeded(statements, false); var updated = updateSourceFile(node, statements); if (hasExportStarsToExportValues) { - setNodeEmitFlags(updated, 2 | getNodeEmitFlags(node)); + ts.setEmitFlags(updated, 2 | ts.getEmitFlags(node)); } return updated; } @@ -40500,7 +42057,7 @@ var ts; } function transformUMDModule(node) { var define = ts.createIdentifier("define"); - setNodeEmitFlags(define, 16); + ts.setEmitFlags(define, 16); return transformAsynchronousModule(node, define, undefined, false); } function transformAsynchronousModule(node, define, moduleName, includeNonAmdDependencies) { @@ -40527,7 +42084,7 @@ var ts; addExportEqualsIfNeeded(statements, true); var body = ts.createBlock(statements, undefined, true); if (hasExportStarsToExportValues) { - setNodeEmitFlags(body, 2); + ts.setEmitFlags(body, 2); } return body; } @@ -40535,12 +42092,12 @@ var ts; if (exportEquals && resolver.isValueAliasDeclaration(exportEquals)) { if (emitAsReturn) { var statement = ts.createReturn(exportEquals.expression, exportEquals); - setNodeEmitFlags(statement, 12288 | 49152); + ts.setEmitFlags(statement, 12288 | 49152); statements.push(statement); } else { var statement = ts.createStatement(ts.createAssignment(ts.createPropertyAccess(ts.createIdentifier("module"), "exports"), exportEquals.expression), exportEquals); - setNodeEmitFlags(statement, 49152); + ts.setEmitFlags(statement, 49152); statements.push(statement); } } @@ -40603,7 +42160,7 @@ var ts; if (!ts.contains(externalImports, node)) { return undefined; } - setNodeEmitFlags(node.name, 128); + ts.setEmitFlags(node.name, 128); var statements = []; if (moduleKind !== ts.ModuleKind.AMD) { if (ts.hasModifier(node, 1)) { @@ -40691,16 +42248,16 @@ var ts; else { var names = ts.reduceEachChild(node, collectExportMembers, []); for (var _i = 0, names_1 = names; _i < names_1.length; _i++) { - var name_37 = names_1[_i]; - addExportMemberAssignments(statements, name_37); + var name_38 = names_1[_i]; + addExportMemberAssignments(statements, name_38); } } } function collectExportMembers(names, node) { if (ts.isAliasSymbolDeclaration(node) && resolver.isValueAliasDeclaration(node) && ts.isDeclaration(node)) { - var name_38 = node.name; - if (ts.isIdentifier(name_38)) { - names.push(name_38); + var name_39 = node.name; + if (ts.isIdentifier(name_39)) { + names.push(name_39); } } return ts.reduceEachChild(node, collectExportMembers, names); @@ -40718,7 +42275,7 @@ var ts; addExportDefault(statements, getDeclarationName(node), node); } else { - statements.push(createExportStatement(node.name, setNodeEmitFlags(ts.getSynthesizedClone(node.name), 262144), node)); + statements.push(createExportStatement(node.name, ts.setEmitFlags(ts.getSynthesizedClone(node.name), 262144), node)); } } function visitVariableStatement(node) { @@ -40835,25 +42392,25 @@ var ts; } function addVarForExportedEnumOrNamespaceDeclaration(statements, node) { var transformedStatement = ts.createVariableStatement(undefined, [ts.createVariableDeclaration(getDeclarationName(node), undefined, ts.createPropertyAccess(ts.createIdentifier("exports"), getDeclarationName(node)))], node); - setNodeEmitFlags(transformedStatement, 49152); + ts.setEmitFlags(transformedStatement, 49152); statements.push(transformedStatement); } function getDeclarationName(node) { return node.name ? ts.getSynthesizedClone(node.name) : ts.getGeneratedNameForNode(node); } - function onEmitNode(node, emit) { + function onEmitNode(emitContext, node, emitCallback) { if (node.kind === 256) { bindingNameExportSpecifiersMap = bindingNameExportSpecifiersForFileMap[ts.getOriginalNodeId(node)]; - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); bindingNameExportSpecifiersMap = undefined; } else { - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); } } - function onSubstituteNode(node, isExpression) { - node = previousOnSubstituteNode(node, isExpression); - if (isExpression) { + function onSubstituteNode(emitContext, node) { + node = previousOnSubstituteNode(emitContext, node); + if (emitContext === 1) { return substituteExpression(node); } else if (ts.isShorthandPropertyAssignment(node)) { @@ -40894,7 +42451,7 @@ var ts; var left = node.left; if (ts.isIdentifier(left) && ts.isAssignmentOperator(node.operatorToken.kind)) { if (bindingNameExportSpecifiersMap && ts.hasProperty(bindingNameExportSpecifiersMap, left.text)) { - setNodeEmitFlags(node, 128); + ts.setEmitFlags(node, 128); var nestedExportAssignment = void 0; for (var _i = 0, _a = bindingNameExportSpecifiersMap[left.text]; _i < _a.length; _i++) { var specifier = _a[_i]; @@ -40912,11 +42469,11 @@ var ts; var operand = node.operand; if (ts.isIdentifier(operand) && bindingNameExportSpecifiersForFileMap) { if (bindingNameExportSpecifiersMap && ts.hasProperty(bindingNameExportSpecifiersMap, operand.text)) { - setNodeEmitFlags(node, 128); + ts.setEmitFlags(node, 128); var transformedUnaryExpression = void 0; if (node.kind === 186) { - transformedUnaryExpression = ts.createBinary(operand, ts.createNode(operator === 41 ? 57 : 58), ts.createLiteral(1), node); - setNodeEmitFlags(transformedUnaryExpression, 128); + transformedUnaryExpression = ts.createBinary(operand, ts.createToken(operator === 41 ? 57 : 58), ts.createLiteral(1), node); + ts.setEmitFlags(transformedUnaryExpression, 128); } var nestedExportAssignment = void 0; for (var _i = 0, _a = bindingNameExportSpecifiersMap[operand.text]; _i < _a.length; _i++) { @@ -40931,7 +42488,7 @@ var ts; return node; } function trySubstituteExportedName(node) { - var emitFlags = getNodeEmitFlags(node); + var emitFlags = ts.getEmitFlags(node); if ((emitFlags & 262144) === 0) { var container = resolver.getReferencedExportContainer(node, (emitFlags & 131072) !== 0); if (container) { @@ -40943,7 +42500,7 @@ var ts; return undefined; } function trySubstituteImportedName(node) { - if ((getNodeEmitFlags(node) & 262144) === 0) { + if ((ts.getEmitFlags(node) & 262144) === 0) { var declaration = resolver.getReferencedImportDeclaration(node); if (declaration) { if (ts.isImportClause(declaration)) { @@ -40955,12 +42512,12 @@ var ts; } } else if (ts.isImportSpecifier(declaration)) { - var name_39 = declaration.propertyName || declaration.name; - if (name_39.originalKeywordKind === 77 && languageVersion <= 0) { - return ts.createElementAccess(ts.getGeneratedNameForNode(declaration.parent.parent.parent), ts.createLiteral(name_39.text), node); + var name_40 = declaration.propertyName || declaration.name; + if (name_40.originalKeywordKind === 77 && languageVersion <= 0) { + return ts.createElementAccess(ts.getGeneratedNameForNode(declaration.parent.parent.parent), ts.createLiteral(name_40.text), node); } else { - return ts.createPropertyAccess(ts.getGeneratedNameForNode(declaration.parent.parent.parent), ts.getSynthesizedClone(name_39), node); + return ts.createPropertyAccess(ts.getGeneratedNameForNode(declaration.parent.parent.parent), ts.getSynthesizedClone(name_40), node); } } } @@ -40982,7 +42539,7 @@ var ts; var statement = ts.createStatement(createExportAssignment(name, value)); statement.startsOnNewLine = true; if (location) { - setSourceMapRange(statement, location); + ts.setSourceMapRange(statement, location); } return statement; } @@ -41010,7 +42567,7 @@ var ts; var externalModuleName = ts.getExternalModuleNameLiteral(importNode, currentSourceFile, host, resolver, compilerOptions); var importAliasName = ts.getLocalNameForExternalImport(importNode, currentSourceFile); if (includeNonAmdDependencies && importAliasName) { - setNodeEmitFlags(importAliasName, 128); + ts.setEmitFlags(importAliasName, 128); aliasedModuleNames.push(externalModuleName); importAliasNames.push(ts.createParameter(importAliasName)); } @@ -41032,7 +42589,7 @@ var ts; var ts; (function (ts) { function transformSystemModule(context) { - var getNodeEmitFlags = context.getNodeEmitFlags, setNodeEmitFlags = context.setNodeEmitFlags, startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration, hoistFunctionDeclaration = context.hoistFunctionDeclaration; + var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration, hoistFunctionDeclaration = context.hoistFunctionDeclaration; var compilerOptions = context.getCompilerOptions(); var resolver = context.getEmitResolver(); var host = context.getEmitHost(); @@ -41061,6 +42618,9 @@ var ts; var currentNode; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } if (ts.isExternalModule(node) || compilerOptions.isolatedModules) { currentSourceFile = node; currentNode = node; @@ -41093,12 +42653,12 @@ var ts; var body = ts.createFunctionExpression(undefined, undefined, undefined, [ ts.createParameter(exportFunctionForFile), ts.createParameter(contextObjectForFile) - ], undefined, setNodeEmitFlags(ts.createBlock(statements, undefined, true), 1)); + ], undefined, ts.setEmitFlags(ts.createBlock(statements, undefined, true), 1)); return updateSourceFile(node, [ ts.createStatement(ts.createCall(ts.createPropertyAccess(ts.createIdentifier("System"), "register"), undefined, moduleName ? [moduleName, dependencies, body] : [dependencies, body])) - ], ~1 & getNodeEmitFlags(node)); + ], ~1 & ts.getEmitFlags(node)); var _a; } function addSystemModuleBody(statements, node, dependencyGroups) { @@ -41342,11 +42902,11 @@ var ts; } function visitFunctionDeclaration(node) { if (ts.hasModifier(node, 1)) { - var name_40 = node.name || ts.getGeneratedNameForNode(node); - var newNode = ts.createFunctionDeclaration(undefined, undefined, node.asteriskToken, name_40, undefined, node.parameters, undefined, node.body, node); + var name_41 = node.name || ts.getGeneratedNameForNode(node); + var newNode = ts.createFunctionDeclaration(undefined, undefined, node.asteriskToken, name_41, undefined, node.parameters, undefined, node.body, node); recordExportedFunctionDeclaration(node); if (!ts.hasModifier(node, 512)) { - recordExportName(name_40); + recordExportName(name_41); } ts.setOriginalNode(newNode, node); node = newNode; @@ -41357,13 +42917,13 @@ var ts; function visitExpressionStatement(node) { var originalNode = ts.getOriginalNode(node); if ((originalNode.kind === 225 || originalNode.kind === 224) && ts.hasModifier(originalNode, 1)) { - var name_41 = getDeclarationName(originalNode); + var name_42 = getDeclarationName(originalNode); if (originalNode.kind === 224) { - hoistVariableDeclaration(name_41); + hoistVariableDeclaration(name_42); } return [ node, - createExportStatement(name_41, name_41) + createExportStatement(name_42, name_42) ]; } return node; @@ -41517,19 +43077,19 @@ var ts; function visitBlock(node) { return ts.visitEachChild(node, visitNestedNode, context); } - function onEmitNode(node, emit) { + function onEmitNode(emitContext, node, emitCallback) { if (node.kind === 256) { exportFunctionForFile = exportFunctionForFileMap[ts.getOriginalNodeId(node)]; - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); exportFunctionForFile = undefined; } else { - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); } } - function onSubstituteNode(node, isExpression) { - node = previousOnSubstituteNode(node, isExpression); - if (isExpression) { + function onSubstituteNode(emitContext, node) { + node = previousOnSubstituteNode(emitContext, node); + if (emitContext === 1) { return substituteExpression(node); } return node; @@ -41563,7 +43123,7 @@ var ts; return node; } function substituteAssignmentExpression(node) { - setNodeEmitFlags(node, 128); + ts.setEmitFlags(node, 128); var left = node.left; switch (left.kind) { case 69: @@ -41668,7 +43228,7 @@ var ts; var exportDeclaration = resolver.getReferencedExportContainer(operand); if (exportDeclaration) { var expr = ts.createPrefix(node.operator, operand, node); - setNodeEmitFlags(expr, 128); + ts.setEmitFlags(expr, 128); var call = createExportExpression(operand, expr); if (node.kind === 185) { return call; @@ -41701,7 +43261,7 @@ var ts; ts.createForIn(ts.createVariableDeclarationList([ ts.createVariableDeclaration(n, undefined) ]), m, ts.createBlock([ - setNodeEmitFlags(ts.createIf(condition, ts.createStatement(ts.createAssignment(ts.createElementAccess(exports, n), ts.createElementAccess(m, n)))), 32) + ts.setEmitFlags(ts.createIf(condition, ts.createStatement(ts.createAssignment(ts.createElementAccess(exports, n), ts.createElementAccess(m, n)))), 32) ])), ts.createStatement(ts.createCall(exportFunctionForFile, undefined, [exports])) ], undefined, true))); @@ -41801,7 +43361,7 @@ var ts; function updateSourceFile(node, statements, nodeEmitFlags) { var updated = ts.getMutableClone(node); updated.statements = ts.createNodeArray(statements, node.statements); - setNodeEmitFlags(updated, nodeEmitFlags); + ts.setEmitFlags(updated, nodeEmitFlags); return updated; } } @@ -41815,6 +43375,9 @@ var ts; var currentSourceFile; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } if (ts.isExternalModule(node) || compilerOptions.isolatedModules) { currentSourceFile = node; return ts.visitEachChild(node, visitor, context); @@ -41951,18 +43514,10 @@ var ts; return transformers; } ts.getTransformers = getTransformers; - var nextTransformId = 1; function transformFiles(resolver, host, sourceFiles, transformers) { - var transformId = nextTransformId; - nextTransformId++; - var tokenSourceMapRanges = ts.createMap(); var lexicalEnvironmentVariableDeclarationsStack = []; var lexicalEnvironmentFunctionDeclarationsStack = []; var enabledSyntaxKindFeatures = new Array(289); - var parseTreeNodesWithAnnotations = []; - var lastTokenSourceMapRangeNode; - var lastTokenSourceMapRangeToken; - var lastTokenSourceMapRange; var lexicalEnvironmentStackOffset = 0; var hoistedVariableDeclarations; var hoistedFunctionDeclarations; @@ -41971,47 +43526,24 @@ var ts; getCompilerOptions: function () { return host.getCompilerOptions(); }, getEmitResolver: function () { return resolver; }, getEmitHost: function () { return host; }, - getNodeEmitFlags: getNodeEmitFlags, - setNodeEmitFlags: setNodeEmitFlags, - getSourceMapRange: getSourceMapRange, - setSourceMapRange: setSourceMapRange, - getTokenSourceMapRange: getTokenSourceMapRange, - setTokenSourceMapRange: setTokenSourceMapRange, - getCommentRange: getCommentRange, - setCommentRange: setCommentRange, hoistVariableDeclaration: hoistVariableDeclaration, hoistFunctionDeclaration: hoistFunctionDeclaration, startLexicalEnvironment: startLexicalEnvironment, endLexicalEnvironment: endLexicalEnvironment, - onSubstituteNode: onSubstituteNode, + onSubstituteNode: function (emitContext, node) { return node; }, enableSubstitution: enableSubstitution, isSubstitutionEnabled: isSubstitutionEnabled, - onEmitNode: onEmitNode, + onEmitNode: function (node, emitContext, emitCallback) { return emitCallback(node, emitContext); }, enableEmitNotification: enableEmitNotification, isEmitNotificationEnabled: isEmitNotificationEnabled }; - var transformation = chain.apply(void 0, transformers)(context); + var transformation = ts.chain.apply(void 0, transformers)(context); var transformed = ts.map(sourceFiles, transformSourceFile); lexicalEnvironmentDisabled = true; return { - getSourceFiles: function () { return transformed; }, - getTokenSourceMapRange: getTokenSourceMapRange, - isSubstitutionEnabled: isSubstitutionEnabled, - isEmitNotificationEnabled: isEmitNotificationEnabled, - onSubstituteNode: context.onSubstituteNode, - onEmitNode: context.onEmitNode, - dispose: function () { - for (var _i = 0, parseTreeNodesWithAnnotations_1 = parseTreeNodesWithAnnotations; _i < parseTreeNodesWithAnnotations_1.length; _i++) { - var node = parseTreeNodesWithAnnotations_1[_i]; - if (node.transformId === transformId) { - node.transformId = 0; - node.emitFlags = 0; - node.commentRange = undefined; - node.sourceMapRange = undefined; - } - } - parseTreeNodesWithAnnotations.length = 0; - } + transformed: transformed, + emitNodeWithSubstitution: emitNodeWithSubstitution, + emitNodeWithNotification: emitNodeWithNotification }; function transformSourceFile(sourceFile) { if (ts.isDeclarationFile(sourceFile)) { @@ -42023,75 +43555,37 @@ var ts; enabledSyntaxKindFeatures[kind] |= 1; } function isSubstitutionEnabled(node) { - return (enabledSyntaxKindFeatures[node.kind] & 1) !== 0; + return (enabledSyntaxKindFeatures[node.kind] & 1) !== 0 + && (ts.getEmitFlags(node) & 128) === 0; } - function onSubstituteNode(node, isExpression) { - return node; + function emitNodeWithSubstitution(emitContext, node, emitCallback) { + if (node) { + if (isSubstitutionEnabled(node)) { + var substitute = context.onSubstituteNode(emitContext, node); + if (substitute && substitute !== node) { + emitCallback(emitContext, substitute); + return; + } + } + emitCallback(emitContext, node); + } } function enableEmitNotification(kind) { enabledSyntaxKindFeatures[kind] |= 2; } function isEmitNotificationEnabled(node) { return (enabledSyntaxKindFeatures[node.kind] & 2) !== 0 - || (getNodeEmitFlags(node) & 64) !== 0; + || (ts.getEmitFlags(node) & 64) !== 0; } - function onEmitNode(node, emit) { - emit(node); - } - function beforeSetAnnotation(node) { - if ((node.flags & 8) === 0 && node.transformId !== transformId) { - parseTreeNodesWithAnnotations.push(node); - node.transformId = transformId; - } - } - function getNodeEmitFlags(node) { - return node.emitFlags; - } - function setNodeEmitFlags(node, emitFlags) { - beforeSetAnnotation(node); - node.emitFlags = emitFlags; - return node; - } - function getSourceMapRange(node) { - return node.sourceMapRange || node; - } - function setSourceMapRange(node, range) { - beforeSetAnnotation(node); - node.sourceMapRange = range; - return node; - } - function getTokenSourceMapRange(node, token) { - if (lastTokenSourceMapRangeNode === node && lastTokenSourceMapRangeToken === token) { - return lastTokenSourceMapRange; - } - var range; - var current = node; - while (current) { - range = current.id ? tokenSourceMapRanges[current.id + "-" + token] : undefined; - if (range !== undefined) { - break; + function emitNodeWithNotification(emitContext, node, emitCallback) { + if (node) { + if (isEmitNotificationEnabled(node)) { + context.onEmitNode(emitContext, node, emitCallback); + } + else { + emitCallback(emitContext, node); } - current = current.original; } - lastTokenSourceMapRangeNode = node; - lastTokenSourceMapRangeToken = token; - lastTokenSourceMapRange = range; - return range; - } - function setTokenSourceMapRange(node, token, range) { - lastTokenSourceMapRangeNode = node; - lastTokenSourceMapRangeToken = token; - lastTokenSourceMapRange = range; - tokenSourceMapRanges[ts.getNodeId(node) + "-" + token] = range; - return node; - } - function getCommentRange(node) { - return node.commentRange || node; - } - function setCommentRange(node, range) { - beforeSetAnnotation(node); - node.commentRange = range; - return node; } function hoistVariableDeclaration(name) { ts.Debug.assert(!lexicalEnvironmentDisabled, "Cannot modify the lexical environment during the print phase."); @@ -42144,54 +43638,6 @@ var ts; } } ts.transformFiles = transformFiles; - function chain(a, b, c, d, e) { - if (e) { - var args_3 = []; - for (var i = 0; i < arguments.length; i++) { - args_3[i] = arguments[i]; - } - return function (t) { return compose.apply(void 0, ts.map(args_3, function (f) { return f(t); })); }; - } - else if (d) { - return function (t) { return compose(a(t), b(t), c(t), d(t)); }; - } - else if (c) { - return function (t) { return compose(a(t), b(t), c(t)); }; - } - else if (b) { - return function (t) { return compose(a(t), b(t)); }; - } - else if (a) { - return function (t) { return compose(a(t)); }; - } - else { - return function (t) { return function (u) { return u; }; }; - } - } - function compose(a, b, c, d, e) { - if (e) { - var args_4 = []; - for (var i = 0; i < arguments.length; i++) { - args_4[i] = arguments[i]; - } - return function (t) { return ts.reduceLeft(args_4, function (u, f) { return f(u); }, t); }; - } - else if (d) { - return function (t) { return d(c(b(a(t)))); }; - } - else if (c) { - return function (t) { return c(b(a(t))); }; - } - else if (b) { - return function (t) { return b(a(t)); }; - } - else if (a) { - return function (t) { return a(t); }; - } - else { - return function (t) { return t; }; - } - } var _a; })(ts || (ts = {})); var ts; @@ -42202,11 +43648,11 @@ var ts; return declarationDiagnostics.getDiagnostics(targetSourceFile ? targetSourceFile.fileName : undefined); function getDeclarationDiagnosticsFromFile(_a, sources, isBundledEmit) { var declarationFilePath = _a.declarationFilePath; - emitDeclarations(host, resolver, declarationDiagnostics, declarationFilePath, sources, isBundledEmit); + emitDeclarations(host, resolver, declarationDiagnostics, declarationFilePath, sources, isBundledEmit, false); } } ts.getDeclarationDiagnostics = getDeclarationDiagnostics; - function emitDeclarations(host, resolver, emitterDiagnostics, declarationFilePath, sourceFiles, isBundledEmit) { + function emitDeclarations(host, resolver, emitterDiagnostics, declarationFilePath, sourceFiles, isBundledEmit, emitOnlyDtsFiles) { var newLine = host.getNewLine(); var compilerOptions = host.getCompilerOptions(); var write; @@ -42242,7 +43688,7 @@ var ts; ts.forEach(sourceFile.referencedFiles, function (fileReference) { var referencedFile = ts.tryResolveScriptReference(host, sourceFile, fileReference); if (referencedFile && !ts.contains(emittedReferencedFiles, referencedFile)) { - if (writeReferencePath(referencedFile, !isBundledEmit && !addedGlobalFileReference)) { + if (writeReferencePath(referencedFile, !isBundledEmit && !addedGlobalFileReference, emitOnlyDtsFiles)) { addedGlobalFileReference = true; } emittedReferencedFiles.push(referencedFile); @@ -42427,7 +43873,7 @@ var ts; } else { errorNameNode = declaration.name; - resolver.writeTypeOfDeclaration(declaration, enclosingDeclaration, 2, writer); + resolver.writeTypeOfDeclaration(declaration, enclosingDeclaration, 2 | 1024, writer); errorNameNode = undefined; } } @@ -42439,7 +43885,7 @@ var ts; } else { errorNameNode = signature.name; - resolver.writeReturnTypeOfSignatureDeclaration(signature, enclosingDeclaration, 2, writer); + resolver.writeReturnTypeOfSignatureDeclaration(signature, enclosingDeclaration, 2 | 1024, writer); errorNameNode = undefined; } } @@ -42612,9 +44058,9 @@ var ts; var count = 0; while (true) { count++; - var name_42 = baseName + "_" + count; - if (!(name_42 in currentIdentifiers)) { - return name_42; + var name_43 = baseName + "_" + count; + if (!(name_43 in currentIdentifiers)) { + return name_43; } } } @@ -42632,7 +44078,7 @@ var ts; write(tempVarName); write(": "); writer.getSymbolAccessibilityDiagnostic = getDefaultExportAccessibilityDiagnostic; - resolver.writeTypeOfExpression(node.expression, enclosingDeclaration, 2, writer); + resolver.writeTypeOfExpression(node.expression, enclosingDeclaration, 2 | 1024, writer); write(";"); writeLine(); write(node.isExportEquals ? "export = " : "export default "); @@ -43038,7 +44484,7 @@ var ts; } else { writer.getSymbolAccessibilityDiagnostic = getHeritageClauseVisibilityError; - resolver.writeBaseConstructorTypeOfClass(enclosingDeclaration, enclosingDeclaration, 2, writer); + resolver.writeBaseConstructorTypeOfClass(enclosingDeclaration, enclosingDeclaration, 2 | 1024, writer); } function getHeritageClauseVisibilityError(symbolAccessibilityResult) { var diagnosticMessage; @@ -43606,14 +45052,14 @@ var ts; return emitSourceFile(node); } } - function writeReferencePath(referencedFile, addBundledFileReference) { + function writeReferencePath(referencedFile, addBundledFileReference, emitOnlyDtsFiles) { var declFileName; var addedBundledEmitReference = false; if (ts.isDeclarationFile(referencedFile)) { declFileName = referencedFile.fileName; } else { - ts.forEachExpectedEmitFile(host, getDeclFileName, referencedFile); + ts.forEachExpectedEmitFile(host, getDeclFileName, referencedFile, emitOnlyDtsFiles); } if (declFileName) { declFileName = ts.getRelativePathToDirectoryOrUrl(ts.getDirectoryPath(ts.normalizeSlashes(declarationFilePath)), declFileName, host.getCurrentDirectory(), host.getCanonicalFileName, false); @@ -43630,8 +45076,8 @@ var ts; } } } - function writeDeclarationFile(declarationFilePath, sourceFiles, isBundledEmit, host, resolver, emitterDiagnostics) { - var emitDeclarationResult = emitDeclarations(host, resolver, emitterDiagnostics, declarationFilePath, sourceFiles, isBundledEmit); + function writeDeclarationFile(declarationFilePath, sourceFiles, isBundledEmit, host, resolver, emitterDiagnostics, emitOnlyDtsFiles) { + var emitDeclarationResult = emitDeclarations(host, resolver, emitterDiagnostics, declarationFilePath, sourceFiles, isBundledEmit, emitOnlyDtsFiles); var emitSkipped = emitDeclarationResult.reportedDeclarationError || host.isEmitBlocked(declarationFilePath) || host.getCompilerOptions().noEmit; if (!emitSkipped) { var declarationOutput = emitDeclarationResult.referencesOutput @@ -43657,41 +45103,6 @@ var ts; })(ts || (ts = {})); var ts; (function (ts) { - function createSourceMapWriter(host, writer) { - var compilerOptions = host.getCompilerOptions(); - if (compilerOptions.sourceMap || compilerOptions.inlineSourceMap) { - if (compilerOptions.extendedDiagnostics) { - return createSourceMapWriterWithExtendedDiagnostics(host, writer); - } - return createSourceMapWriterWorker(host, writer); - } - else { - return getNullSourceMapWriter(); - } - } - ts.createSourceMapWriter = createSourceMapWriter; - var nullSourceMapWriter; - function getNullSourceMapWriter() { - if (nullSourceMapWriter === undefined) { - nullSourceMapWriter = { - initialize: function (filePath, sourceMapFilePath, sourceFiles, isBundledEmit) { }, - reset: function () { }, - getSourceMapData: function () { return undefined; }, - setSourceFile: function (sourceFile) { }, - emitPos: function (pos) { }, - emitStart: function (range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { }, - emitEnd: function (range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { }, - emitTokenStart: function (token, pos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { return -1; }, - emitTokenEnd: function (token, end, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { return -1; }, - changeEmitSourcePos: function () { }, - stopOverridingSpan: function () { }, - getText: function () { return undefined; }, - getSourceMappingURL: function () { return undefined; } - }; - } - return nullSourceMapWriter; - } - ts.getNullSourceMapWriter = getNullSourceMapWriter; var defaultLastEncodedSourceMapSpan = { emittedLine: 1, emittedColumn: 1, @@ -43699,42 +45110,38 @@ var ts; sourceColumn: 1, sourceIndex: 0 }; - function createSourceMapWriterWorker(host, writer) { + function createSourceMapWriter(host, writer) { var compilerOptions = host.getCompilerOptions(); var extendedDiagnostics = compilerOptions.extendedDiagnostics; var currentSourceFile; var currentSourceText; var sourceMapDir; - var stopOverridingSpan = false; - var modifyLastSourcePos = false; var sourceMapSourceIndex; var lastRecordedSourceMapSpan; var lastEncodedSourceMapSpan; var lastEncodedNameIndex; var sourceMapData; - var disableDepth; + var disabled = !(compilerOptions.sourceMap || compilerOptions.inlineSourceMap); return { initialize: initialize, reset: reset, getSourceMapData: function () { return sourceMapData; }, setSourceFile: setSourceFile, emitPos: emitPos, - emitStart: emitStart, - emitEnd: emitEnd, - emitTokenStart: emitTokenStart, - emitTokenEnd: emitTokenEnd, - changeEmitSourcePos: changeEmitSourcePos, - stopOverridingSpan: function () { return stopOverridingSpan = true; }, + emitNodeWithSourceMap: emitNodeWithSourceMap, + emitTokenWithSourceMap: emitTokenWithSourceMap, getText: getText, getSourceMappingURL: getSourceMappingURL }; function initialize(filePath, sourceMapFilePath, sourceFiles, isBundledEmit) { + if (disabled) { + return; + } if (sourceMapData) { reset(); } currentSourceFile = undefined; currentSourceText = undefined; - disableDepth = 0; sourceMapSourceIndex = -1; lastRecordedSourceMapSpan = undefined; lastEncodedSourceMapSpan = defaultLastEncodedSourceMapSpan; @@ -43774,6 +45181,9 @@ var ts; } } function reset() { + if (disabled) { + return; + } currentSourceFile = undefined; sourceMapDir = undefined; sourceMapSourceIndex = undefined; @@ -43781,38 +45191,6 @@ var ts; lastEncodedSourceMapSpan = undefined; lastEncodedNameIndex = undefined; sourceMapData = undefined; - disableDepth = 0; - } - function enable() { - if (disableDepth > 0) { - disableDepth--; - } - } - function disable() { - disableDepth++; - } - function updateLastEncodedAndRecordedSpans() { - if (modifyLastSourcePos) { - modifyLastSourcePos = false; - lastRecordedSourceMapSpan.emittedLine = lastEncodedSourceMapSpan.emittedLine; - lastRecordedSourceMapSpan.emittedColumn = lastEncodedSourceMapSpan.emittedColumn; - sourceMapData.sourceMapDecodedMappings.pop(); - lastEncodedSourceMapSpan = sourceMapData.sourceMapDecodedMappings.length ? - sourceMapData.sourceMapDecodedMappings[sourceMapData.sourceMapDecodedMappings.length - 1] : - defaultLastEncodedSourceMapSpan; - var sourceMapMappings = sourceMapData.sourceMapMappings; - var lenthToSet = sourceMapMappings.length - 1; - for (; lenthToSet >= 0; lenthToSet--) { - var currentChar = sourceMapMappings.charAt(lenthToSet); - if (currentChar === ",") { - break; - } - if (currentChar === ";" && lenthToSet !== 0 && sourceMapMappings.charAt(lenthToSet - 1) !== ";") { - break; - } - } - sourceMapData.sourceMapMappings = sourceMapMappings.substr(0, Math.max(0, lenthToSet)); - } } function encodeLastRecordedSourceMapSpan() { if (!lastRecordedSourceMapSpan || lastRecordedSourceMapSpan === lastEncodedSourceMapSpan) { @@ -43843,7 +45221,7 @@ var ts; sourceMapData.sourceMapDecodedMappings.push(lastEncodedSourceMapSpan); } function emitPos(pos) { - if (ts.positionIsSynthesized(pos) || disableDepth > 0) { + if (disabled || ts.positionIsSynthesized(pos)) { return; } if (extendedDiagnostics) { @@ -43868,84 +45246,68 @@ var ts; sourceColumn: sourceLinePos.character, sourceIndex: sourceMapSourceIndex }; - stopOverridingSpan = false; } - else if (!stopOverridingSpan) { + else { lastRecordedSourceMapSpan.sourceLine = sourceLinePos.line; lastRecordedSourceMapSpan.sourceColumn = sourceLinePos.character; lastRecordedSourceMapSpan.sourceIndex = sourceMapSourceIndex; } - updateLastEncodedAndRecordedSpans(); if (extendedDiagnostics) { ts.performance.mark("afterSourcemap"); ts.performance.measure("Source Map", "beforeSourcemap", "afterSourcemap"); } } - function getStartPosPastDecorators(range) { - var rangeHasDecorators = !!range.decorators; - return ts.skipTrivia(currentSourceText, rangeHasDecorators ? range.decorators.end : range.pos); - } - function emitStart(range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { - if (contextNode) { - if (!ignoreNodeCallback(contextNode)) { - range = getTextRangeCallback(contextNode) || range; - emitPos(getStartPosPastDecorators(range)); - } - if (ignoreChildrenCallback(contextNode)) { - disable(); - } - } - else { - emitPos(getStartPosPastDecorators(range)); + function emitNodeWithSourceMap(emitContext, node, emitCallback) { + if (disabled) { + return emitCallback(emitContext, node); } - } - function emitEnd(range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { - if (contextNode) { - if (ignoreChildrenCallback(contextNode)) { - enable(); - } - if (!ignoreNodeCallback(contextNode)) { - range = getTextRangeCallback(contextNode) || range; - emitPos(range.end); + if (node) { + var emitNode = node.emitNode; + var emitFlags = emitNode && emitNode.flags; + var _a = emitNode && emitNode.sourceMapRange || node, pos = _a.pos, end = _a.end; + if (node.kind !== 287 + && (emitFlags & 512) === 0 + && pos >= 0) { + emitPos(ts.skipTrivia(currentSourceText, pos)); + } + if (emitFlags & 2048) { + disabled = true; + emitCallback(emitContext, node); + disabled = false; } - } - else { - emitPos(range.end); - } - stopOverridingSpan = false; - } - function emitTokenStart(token, tokenStartPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { - if (contextNode) { - if (ignoreTokenCallback(contextNode, token)) { - return ts.skipTrivia(currentSourceText, tokenStartPos); + else { + emitCallback(emitContext, node); } - var range = getTokenTextRangeCallback(contextNode, token); - if (range) { - tokenStartPos = range.pos; + if (node.kind !== 287 + && (emitFlags & 1024) === 0 + && end >= 0) { + emitPos(end); } } - tokenStartPos = ts.skipTrivia(currentSourceText, tokenStartPos); - emitPos(tokenStartPos); - return tokenStartPos; } - function emitTokenEnd(token, tokenEndPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { - if (contextNode) { - if (ignoreTokenCallback(contextNode, token)) { - return tokenEndPos; - } - var range = getTokenTextRangeCallback(contextNode, token); - if (range) { - tokenEndPos = range.end; - } + function emitTokenWithSourceMap(node, token, tokenPos, emitCallback) { + if (disabled) { + return emitCallback(token, tokenPos); } - emitPos(tokenEndPos); - return tokenEndPos; - } - function changeEmitSourcePos() { - ts.Debug.assert(!modifyLastSourcePos); - modifyLastSourcePos = true; + var emitNode = node && node.emitNode; + var emitFlags = emitNode && emitNode.flags; + var range = emitNode && emitNode.tokenSourceMapRanges && emitNode.tokenSourceMapRanges[token]; + tokenPos = ts.skipTrivia(currentSourceText, range ? range.pos : tokenPos); + if ((emitFlags & 4096) === 0 && tokenPos >= 0) { + emitPos(tokenPos); + } + tokenPos = emitCallback(token, tokenPos); + if (range) + tokenPos = range.end; + if ((emitFlags & 8192) === 0 && tokenPos >= 0) { + emitPos(tokenPos); + } + return tokenPos; } function setSourceFile(sourceFile) { + if (disabled) { + return; + } currentSourceFile = sourceFile; currentSourceText = currentSourceFile.text; var sourcesDirectoryPath = compilerOptions.sourceRoot ? host.getCommonSourceDirectory() : sourceMapDir; @@ -43961,6 +45323,9 @@ var ts; } } function getText() { + if (disabled) { + return; + } encodeLastRecordedSourceMapSpan(); return ts.stringify({ version: 3, @@ -43973,6 +45338,9 @@ var ts; }); } function getSourceMappingURL() { + if (disabled) { + return; + } if (compilerOptions.inlineSourceMap) { var base64SourceMapText = ts.convertToBase64(getText()); return sourceMapData.jsSourceMappingURL = "data:application/json;base64," + base64SourceMapText; @@ -43982,46 +45350,7 @@ var ts; } } } - function createSourceMapWriterWithExtendedDiagnostics(host, writer) { - var _a = createSourceMapWriterWorker(host, writer), initialize = _a.initialize, reset = _a.reset, getSourceMapData = _a.getSourceMapData, setSourceFile = _a.setSourceFile, emitPos = _a.emitPos, emitStart = _a.emitStart, emitEnd = _a.emitEnd, emitTokenStart = _a.emitTokenStart, emitTokenEnd = _a.emitTokenEnd, changeEmitSourcePos = _a.changeEmitSourcePos, stopOverridingSpan = _a.stopOverridingSpan, getText = _a.getText, getSourceMappingURL = _a.getSourceMappingURL; - return { - initialize: initialize, - reset: reset, - getSourceMapData: getSourceMapData, - setSourceFile: setSourceFile, - emitPos: function (pos) { - ts.performance.mark("sourcemapStart"); - emitPos(pos); - ts.performance.measure("sourceMapTime", "sourcemapStart"); - }, - emitStart: function (range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { - ts.performance.mark("emitSourcemap:emitStart"); - emitStart(range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback); - ts.performance.measure("sourceMapTime", "emitSourcemap:emitStart"); - }, - emitEnd: function (range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { - ts.performance.mark("emitSourcemap:emitEnd"); - emitEnd(range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback); - ts.performance.measure("sourceMapTime", "emitSourcemap:emitEnd"); - }, - emitTokenStart: function (token, tokenStartPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { - ts.performance.mark("emitSourcemap:emitTokenStart"); - tokenStartPos = emitTokenStart(token, tokenStartPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback); - ts.performance.measure("sourceMapTime", "emitSourcemap:emitTokenStart"); - return tokenStartPos; - }, - emitTokenEnd: function (token, tokenEndPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { - ts.performance.mark("emitSourcemap:emitTokenEnd"); - tokenEndPos = emitTokenEnd(token, tokenEndPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback); - ts.performance.measure("sourceMapTime", "emitSourcemap:emitTokenEnd"); - return tokenEndPos; - }, - changeEmitSourcePos: changeEmitSourcePos, - stopOverridingSpan: stopOverridingSpan, - getText: getText, - getSourceMappingURL: getSourceMappingURL - }; - } + ts.createSourceMapWriter = createSourceMapWriter; var base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; function base64FormatEncode(inValue) { if (inValue < 64) { @@ -44071,20 +45400,22 @@ var ts; emitBodyWithDetachedComments: emitBodyWithDetachedComments, emitTrailingCommentsOfPosition: emitTrailingCommentsOfPosition }; - function emitNodeWithComments(node, emitCallback) { + function emitNodeWithComments(emitContext, node, emitCallback) { if (disabled) { - emitCallback(node); + emitCallback(emitContext, node); return; } if (node) { - var _a = node.commentRange || node, pos = _a.pos, end = _a.end; - var emitFlags = node.emitFlags; + var _a = ts.getCommentRange(node), pos = _a.pos, end = _a.end; + var emitFlags = ts.getEmitFlags(node); if ((pos < 0 && end < 0) || (pos === end)) { if (emitFlags & 65536) { - disableCommentsAndEmit(node, emitCallback); + disabled = true; + emitCallback(emitContext, node); + disabled = false; } else { - emitCallback(node); + emitCallback(emitContext, node); } } else { @@ -44113,10 +45444,12 @@ var ts; ts.performance.measure("commentTime", "preEmitNodeWithComment"); } if (emitFlags & 65536) { - disableCommentsAndEmit(node, emitCallback); + disabled = true; + emitCallback(emitContext, node); + disabled = false; } else { - emitCallback(node); + emitCallback(emitContext, node); } if (extendedDiagnostics) { ts.performance.mark("beginEmitNodeWithComment"); @@ -44138,7 +45471,7 @@ var ts; ts.performance.mark("preEmitBodyWithDetachedComments"); } var pos = detachedRange.pos, end = detachedRange.end; - var emitFlags = node.emitFlags; + var emitFlags = ts.getEmitFlags(node); var skipLeadingComments = pos < 0 || (emitFlags & 16384) !== 0; var skipTrailingComments = disabled || end < 0 || (emitFlags & 32768) !== 0; if (!skipLeadingComments) { @@ -44147,8 +45480,10 @@ var ts; if (extendedDiagnostics) { ts.performance.measure("commentTime", "preEmitBodyWithDetachedComments"); } - if (emitFlags & 65536) { - disableCommentsAndEmit(node, emitCallback); + if (emitFlags & 65536 && !disabled) { + disabled = true; + emitCallback(node); + disabled = false; } else { emitCallback(node); @@ -44256,16 +45591,6 @@ var ts; currentLineMap = ts.getLineStarts(currentSourceFile); detachedCommentsInfo = undefined; } - function disableCommentsAndEmit(node, emitCallback) { - if (disabled) { - emitCallback(node); - } - else { - disabled = true; - emitCallback(node); - disabled = false; - } - } function hasDetachedComments(pos) { return detachedCommentsInfo !== undefined && ts.lastOrUndefined(detachedCommentsInfo).nodePos === pos; } @@ -44311,7 +45636,9 @@ var ts; })(ts || (ts = {})); var ts; (function (ts) { - function emitFiles(resolver, host, targetSourceFile) { + var id = function (s) { return s; }; + var nullTransformers = [function (ctx) { return id; }]; + function emitFiles(resolver, host, targetSourceFile, emitOnlyDtsFiles) { var delimiters = createDelimiterMap(); var brackets = createBracketsMap(); var extendsHelper = "\nvar __extends = (this && this.__extends) || function (d, b) {\n for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];\n function __() { this.constructor = d; }\n d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\n};"; @@ -44319,7 +45646,7 @@ var ts; var decorateHelper = "\nvar __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {\n var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;\n if (typeof Reflect === \"object\" && typeof Reflect.decorate === \"function\") r = Reflect.decorate(decorators, target, key, desc);\n else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;\n return c > 3 && r && Object.defineProperty(target, key, r), r;\n};"; var metadataHelper = "\nvar __metadata = (this && this.__metadata) || function (k, v) {\n if (typeof Reflect === \"object\" && typeof Reflect.metadata === \"function\") return Reflect.metadata(k, v);\n};"; var paramHelper = "\nvar __param = (this && this.__param) || function (paramIndex, decorator) {\n return function (target, key) { decorator(target, key, paramIndex); }\n};"; - var awaiterHelper = "\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments)).next());\n });\n};"; + var awaiterHelper = "\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments)).next());\n });\n};"; var generatorHelper = "\nvar __generator = (this && this.__generator) || function (thisArg, body) {\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t;\n return { next: verb(0), \"throw\": verb(1), \"return\": verb(2) };\n function verb(n) { return function (v) { return step([n, v]); }; }\n function step(op) {\n if (f) throw new TypeError(\"Generator is already executing.\");\n while (_) try {\n if (f = 1, y && (t = y[op[0] & 2 ? \"return\" : op[0] ? \"throw\" : \"next\"]) && !(t = t.call(y, op[1])).done) return t;\n if (y = 0, t) op = [0, t.value];\n switch (op[0]) {\n case 0: case 1: t = op; break;\n case 4: _.label++; return { value: op[1], done: false };\n case 5: _.label++; y = op[1]; op = [0]; continue;\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\n default:\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\n if (t[2]) _.ops.pop();\n _.trys.pop(); continue;\n }\n op = body.call(thisArg, _);\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\n }\n};"; var exportStarHelper = "\nfunction __export(m) {\n for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];\n}"; var umdHelper = "\n(function (dependencies, factory) {\n if (typeof module === 'object' && typeof module.exports === 'object') {\n var v = factory(require, exports); if (v !== undefined) module.exports = v;\n }\n else if (typeof define === 'function' && define.amd) {\n define(dependencies, factory);\n }\n})"; @@ -44332,11 +45659,11 @@ var ts; var emittedFilesList = compilerOptions.listEmittedFiles ? [] : undefined; var emitterDiagnostics = ts.createDiagnosticCollection(); var newLine = host.getNewLine(); - var transformers = ts.getTransformers(compilerOptions); + var transformers = emitOnlyDtsFiles ? nullTransformers : ts.getTransformers(compilerOptions); var writer = ts.createTextWriter(newLine); var write = writer.write, writeLine = writer.writeLine, increaseIndent = writer.increaseIndent, decreaseIndent = writer.decreaseIndent; var sourceMap = ts.createSourceMapWriter(host, writer); - var emitStart = sourceMap.emitStart, emitEnd = sourceMap.emitEnd, emitTokenStart = sourceMap.emitTokenStart, emitTokenEnd = sourceMap.emitTokenEnd; + var emitNodeWithSourceMap = sourceMap.emitNodeWithSourceMap, emitTokenWithSourceMap = sourceMap.emitTokenWithSourceMap; var comments = ts.createCommentWriter(host, writer, sourceMap); var emitNodeWithComments = comments.emitNodeWithComments, emitBodyWithDetachedComments = comments.emitBodyWithDetachedComments, emitTrailingCommentsOfPosition = comments.emitTrailingCommentsOfPosition; var nodeIdToGeneratedName; @@ -44353,14 +45680,17 @@ var ts; var awaiterEmitted; var isOwnFileEmit; var emitSkipped = false; + var sourceFiles = ts.getSourceFilesToEmit(host, targetSourceFile); ts.performance.mark("beforeTransform"); - var transformed = ts.transformFiles(resolver, host, ts.getSourceFilesToEmit(host, targetSourceFile), transformers); + var _a = ts.transformFiles(resolver, host, sourceFiles, transformers), transformed = _a.transformed, emitNodeWithSubstitution = _a.emitNodeWithSubstitution, emitNodeWithNotification = _a.emitNodeWithNotification; ts.performance.measure("transformTime", "beforeTransform"); - var getTokenSourceMapRange = transformed.getTokenSourceMapRange, isSubstitutionEnabled = transformed.isSubstitutionEnabled, isEmitNotificationEnabled = transformed.isEmitNotificationEnabled, onSubstituteNode = transformed.onSubstituteNode, onEmitNode = transformed.onEmitNode; ts.performance.mark("beforePrint"); - ts.forEachTransformedEmitFile(host, transformed.getSourceFiles(), emitFile); - transformed.dispose(); + ts.forEachTransformedEmitFile(host, transformed, emitFile, emitOnlyDtsFiles); ts.performance.measure("printTime", "beforePrint"); + for (var _b = 0, sourceFiles_4 = sourceFiles; _b < sourceFiles_4.length; _b++) { + var sourceFile = sourceFiles_4[_b]; + ts.disposeEmitNodes(sourceFile); + } return { emitSkipped: emitSkipped, diagnostics: emitterDiagnostics.getDiagnostics(), @@ -44369,16 +45699,20 @@ var ts; }; function emitFile(jsFilePath, sourceMapFilePath, declarationFilePath, sourceFiles, isBundledEmit) { if (!host.isEmitBlocked(jsFilePath) && !compilerOptions.noEmit) { - printFile(jsFilePath, sourceMapFilePath, sourceFiles, isBundledEmit); + if (!emitOnlyDtsFiles) { + printFile(jsFilePath, sourceMapFilePath, sourceFiles, isBundledEmit); + } } else { emitSkipped = true; } if (declarationFilePath) { - emitSkipped = ts.writeDeclarationFile(declarationFilePath, ts.getOriginalSourceFiles(sourceFiles), isBundledEmit, host, resolver, emitterDiagnostics) || emitSkipped; + emitSkipped = ts.writeDeclarationFile(declarationFilePath, ts.getOriginalSourceFiles(sourceFiles), isBundledEmit, host, resolver, emitterDiagnostics, emitOnlyDtsFiles) || emitSkipped; } if (!emitSkipped && emittedFilesList) { - emittedFilesList.push(jsFilePath); + if (!emitOnlyDtsFiles) { + emittedFilesList.push(jsFilePath); + } if (sourceMapFilePath) { emittedFilesList.push(sourceMapFilePath); } @@ -44394,8 +45728,8 @@ var ts; generatedNameSet = ts.createMap(); isOwnFileEmit = !isBundledEmit; if (isBundledEmit && moduleKind) { - for (var _a = 0, sourceFiles_4 = sourceFiles; _a < sourceFiles_4.length; _a++) { - var sourceFile = sourceFiles_4[_a]; + for (var _a = 0, sourceFiles_5 = sourceFiles; _a < sourceFiles_5.length; _a++) { + var sourceFile = sourceFiles_5[_a]; emitEmitHelpers(sourceFile); } } @@ -44431,73 +45765,61 @@ var ts; currentFileIdentifiers = node.identifiers; sourceMap.setSourceFile(node); comments.setSourceFile(node); - emitNodeWithNotification(node, emitWorker); + pipelineEmitWithNotification(0, node); } function emit(node) { - emitNodeWithNotification(node, emitWithComments); - } - function emitWithComments(node) { - emitNodeWithComments(node, emitWithSourceMap); - } - function emitWithSourceMap(node) { - emitNodeWithSourceMap(node, emitWorker); + pipelineEmitWithNotification(3, node); } function emitIdentifierName(node) { - if (node) { - emitNodeWithNotification(node, emitIdentifierNameWithComments); - } - } - function emitIdentifierNameWithComments(node) { - emitNodeWithComments(node, emitWorker); + pipelineEmitWithNotification(2, node); } function emitExpression(node) { - emitNodeWithNotification(node, emitExpressionWithComments); - } - function emitExpressionWithComments(node) { - emitNodeWithComments(node, emitExpressionWithSourceMap); + pipelineEmitWithNotification(1, node); } - function emitExpressionWithSourceMap(node) { - emitNodeWithSourceMap(node, emitExpressionWorker); + function pipelineEmitWithNotification(emitContext, node) { + emitNodeWithNotification(emitContext, node, pipelineEmitWithComments); } - function emitNodeWithNotification(node, emitCallback) { - if (node) { - if (isEmitNotificationEnabled(node)) { - onEmitNode(node, emitCallback); - } - else { - emitCallback(node); - } + function pipelineEmitWithComments(emitContext, node) { + if (emitContext === 0) { + pipelineEmitWithSourceMap(emitContext, node); + return; } + emitNodeWithComments(emitContext, node, pipelineEmitWithSourceMap); } - function emitNodeWithSourceMap(node, emitCallback) { - if (node) { - emitStart(node, node, shouldSkipLeadingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange); - emitCallback(node); - emitEnd(node, node, shouldSkipTrailingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange); + function pipelineEmitWithSourceMap(emitContext, node) { + if (emitContext === 0 + || emitContext === 2) { + pipelineEmitWithSubstitution(emitContext, node); + return; } + emitNodeWithSourceMap(emitContext, node, pipelineEmitWithSubstitution); } - function getSourceMapRange(node) { - return node.sourceMapRange || node; + function pipelineEmitWithSubstitution(emitContext, node) { + emitNodeWithSubstitution(emitContext, node, pipelineEmitForContext); } - function shouldSkipLeadingCommentsForNode(node) { - return ts.isNotEmittedStatement(node) - || (node.emitFlags & 16384) !== 0; - } - function shouldSkipLeadingSourceMapForNode(node) { - return ts.isNotEmittedStatement(node) - || (node.emitFlags & 512) !== 0; - } - function shouldSkipTrailingSourceMapForNode(node) { - return ts.isNotEmittedStatement(node) - || (node.emitFlags & 1024) !== 0; + function pipelineEmitForContext(emitContext, node) { + switch (emitContext) { + case 0: return pipelineEmitInSourceFileContext(node); + case 2: return pipelineEmitInIdentifierNameContext(node); + case 3: return pipelineEmitInUnspecifiedContext(node); + case 1: return pipelineEmitInExpressionContext(node); + } } - function shouldSkipSourceMapForChildren(node) { - return (node.emitFlags & 2048) !== 0; + function pipelineEmitInSourceFileContext(node) { + var kind = node.kind; + switch (kind) { + case 256: + return emitSourceFile(node); + } } - function emitWorker(node) { - if (tryEmitSubstitute(node, emitWorker, false)) { - return; + function pipelineEmitInIdentifierNameContext(node) { + var kind = node.kind; + switch (kind) { + case 69: + return emitIdentifier(node); } + } + function pipelineEmitInUnspecifiedContext(node) { var kind = node.kind; switch (kind) { case 12: @@ -44524,7 +45846,8 @@ var ts; case 132: case 133: case 137: - return writeTokenNode(node); + writeTokenText(kind); + return; case 139: return emitQualifiedName(node); case 140: @@ -44673,2353 +45996,1725 @@ var ts; case 239: return; case 240: - return emitExternalModuleReference(node); - case 244: - return emitJsxText(node); - case 243: - return emitJsxOpeningElement(node); - case 245: - return emitJsxClosingElement(node); - case 246: - return emitJsxAttribute(node); - case 247: - return emitJsxSpreadAttribute(node); - case 248: - return emitJsxExpression(node); - case 249: - return emitCaseClause(node); - case 250: - return emitDefaultClause(node); - case 251: - return emitHeritageClause(node); - case 252: - return emitCatchClause(node); - case 253: - return emitPropertyAssignment(node); - case 254: - return emitShorthandPropertyAssignment(node); - case 255: - return emitEnumMember(node); - case 256: - return emitSourceFile(node); - } - if (ts.isExpression(node)) { - return emitExpressionWorker(node); - } - } - function emitExpressionWorker(node) { - if (tryEmitSubstitute(node, emitExpressionWorker, true)) { - return; - } - var kind = node.kind; - switch (kind) { - case 8: - return emitNumericLiteral(node); - case 9: - case 10: - case 11: - return emitLiteral(node); - case 69: - return emitIdentifier(node); - case 84: - case 93: - case 95: - case 99: - case 97: - return writeTokenNode(node); - case 170: - return emitArrayLiteralExpression(node); - case 171: - return emitObjectLiteralExpression(node); - case 172: - return emitPropertyAccessExpression(node); - case 173: - return emitElementAccessExpression(node); - case 174: - return emitCallExpression(node); - case 175: - return emitNewExpression(node); - case 176: - return emitTaggedTemplateExpression(node); - case 177: - return emitTypeAssertionExpression(node); - case 178: - return emitParenthesizedExpression(node); - case 179: - return emitFunctionExpression(node); - case 180: - return emitArrowFunction(node); - case 181: - return emitDeleteExpression(node); - case 182: - return emitTypeOfExpression(node); - case 183: - return emitVoidExpression(node); - case 184: - return emitAwaitExpression(node); - case 185: - return emitPrefixUnaryExpression(node); - case 186: - return emitPostfixUnaryExpression(node); - case 187: - return emitBinaryExpression(node); - case 188: - return emitConditionalExpression(node); - case 189: - return emitTemplateExpression(node); - case 190: - return emitYieldExpression(node); - case 191: - return emitSpreadElementExpression(node); - case 192: - return emitClassExpression(node); - case 193: - return; - case 195: - return emitAsExpression(node); - case 196: - return emitNonNullExpression(node); - case 241: - return emitJsxElement(node); - case 242: - return emitJsxSelfClosingElement(node); - case 288: - return emitPartiallyEmittedExpression(node); - } - } - function emitNumericLiteral(node) { - emitLiteral(node); - if (node.trailingComment) { - write(" /*" + node.trailingComment + "*/"); - } - } - function emitLiteral(node) { - var text = getLiteralTextOfNode(node); - if ((compilerOptions.sourceMap || compilerOptions.inlineSourceMap) - && (node.kind === 9 || ts.isTemplateLiteralKind(node.kind))) { - writer.writeLiteral(text); - } - else { - write(text); - } - } - function emitIdentifier(node) { - if (node.emitFlags & 16) { - writeLines(umdHelper); - } - else { - write(getTextOfNode(node, false)); - } - } - function emitQualifiedName(node) { - emitEntityName(node.left); - write("."); - emit(node.right); - } - function emitEntityName(node) { - if (node.kind === 69) { - emitExpression(node); - } - else { - emit(node); - } - } - function emitComputedPropertyName(node) { - write("["); - emitExpression(node.expression); - write("]"); - } - function emitTypeParameter(node) { - emit(node.name); - emitWithPrefix(" extends ", node.constraint); - } - function emitParameter(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - writeIfPresent(node.dotDotDotToken, "..."); - emit(node.name); - writeIfPresent(node.questionToken, "?"); - emitExpressionWithPrefix(" = ", node.initializer); - emitWithPrefix(": ", node.type); - } - function emitDecorator(decorator) { - write("@"); - emitExpression(decorator.expression); - } - function emitPropertySignature(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - emit(node.name); - writeIfPresent(node.questionToken, "?"); - emitWithPrefix(": ", node.type); - write(";"); - } - function emitPropertyDeclaration(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - emit(node.name); - emitWithPrefix(": ", node.type); - emitExpressionWithPrefix(" = ", node.initializer); - write(";"); - } - function emitMethodSignature(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - emit(node.name); - writeIfPresent(node.questionToken, "?"); - emitTypeParameters(node, node.typeParameters); - emitParameters(node, node.parameters); - emitWithPrefix(": ", node.type); - write(";"); - } - function emitMethodDeclaration(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - writeIfPresent(node.asteriskToken, "*"); - emit(node.name); - emitSignatureAndBody(node, emitSignatureHead); - } - function emitConstructor(node) { - emitModifiers(node, node.modifiers); - write("constructor"); - emitSignatureAndBody(node, emitSignatureHead); - } - function emitAccessorDeclaration(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - write(node.kind === 149 ? "get " : "set "); - emit(node.name); - emitSignatureAndBody(node, emitSignatureHead); - } - function emitCallSignature(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - emitTypeParameters(node, node.typeParameters); - emitParameters(node, node.parameters); - emitWithPrefix(": ", node.type); - write(";"); - } - function emitConstructSignature(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - write("new "); - emitTypeParameters(node, node.typeParameters); - emitParameters(node, node.parameters); - emitWithPrefix(": ", node.type); - write(";"); - } - function emitIndexSignature(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - emitParametersForIndexSignature(node, node.parameters); - emitWithPrefix(": ", node.type); - write(";"); - } - function emitSemicolonClassElement(node) { - write(";"); - } - function emitTypePredicate(node) { - emit(node.parameterName); - write(" is "); - emit(node.type); - } - function emitTypeReference(node) { - emit(node.typeName); - emitTypeArguments(node, node.typeArguments); - } - function emitFunctionType(node) { - emitTypeParameters(node, node.typeParameters); - emitParametersForArrow(node, node.parameters); - write(" => "); - emit(node.type); - } - function emitConstructorType(node) { - write("new "); - emitTypeParameters(node, node.typeParameters); - emitParametersForArrow(node, node.parameters); - write(" => "); - emit(node.type); - } - function emitTypeQuery(node) { - write("typeof "); - emit(node.exprName); - } - function emitTypeLiteral(node) { - write("{"); - emitList(node, node.members, 65); - write("}"); - } - function emitArrayType(node) { - emit(node.elementType); - write("[]"); - } - function emitTupleType(node) { - write("["); - emitList(node, node.elementTypes, 336); - write("]"); - } - function emitUnionType(node) { - emitList(node, node.types, 260); - } - function emitIntersectionType(node) { - emitList(node, node.types, 264); - } - function emitParenthesizedType(node) { - write("("); - emit(node.type); - write(")"); - } - function emitThisType(node) { - write("this"); - } - function emitLiteralType(node) { - emitExpression(node.literal); - } - function emitObjectBindingPattern(node) { - var elements = node.elements; - if (elements.length === 0) { - write("{}"); - } - else { - write("{"); - emitList(node, elements, 432); - write("}"); - } - } - function emitArrayBindingPattern(node) { - var elements = node.elements; - if (elements.length === 0) { - write("[]"); - } - else { - write("["); - emitList(node, node.elements, 304); - write("]"); - } - } - function emitBindingElement(node) { - emitWithSuffix(node.propertyName, ": "); - writeIfPresent(node.dotDotDotToken, "..."); - emit(node.name); - emitExpressionWithPrefix(" = ", node.initializer); - } - function emitArrayLiteralExpression(node) { - var elements = node.elements; - if (elements.length === 0) { - write("[]"); - } - else { - var preferNewLine = node.multiLine ? 32768 : 0; - emitExpressionList(node, elements, 4466 | preferNewLine); - } - } - function emitObjectLiteralExpression(node) { - var properties = node.properties; - if (properties.length === 0) { - write("{}"); - } - else { - var indentedFlag = node.emitFlags & 524288; - if (indentedFlag) { - increaseIndent(); - } - var preferNewLine = node.multiLine ? 32768 : 0; - var allowTrailingComma = languageVersion >= 1 ? 32 : 0; - emitList(node, properties, 978 | allowTrailingComma | preferNewLine); - if (indentedFlag) { - decreaseIndent(); - } - } - } - function emitPropertyAccessExpression(node) { - if (tryEmitConstantValue(node)) { - return; - } - var indentBeforeDot = false; - var indentAfterDot = false; - if (!(node.emitFlags & 1048576)) { - var dotRangeStart = node.expression.end; - var dotRangeEnd = ts.skipTrivia(currentText, node.expression.end) + 1; - var dotToken = { kind: 21, pos: dotRangeStart, end: dotRangeEnd }; - indentBeforeDot = needsIndentation(node, node.expression, dotToken); - indentAfterDot = needsIndentation(node, dotToken, node.name); - } - var shouldEmitDotDot = !indentBeforeDot && needsDotDotForPropertyAccess(node.expression); - emitExpression(node.expression); - increaseIndentIf(indentBeforeDot); - write(shouldEmitDotDot ? ".." : "."); - increaseIndentIf(indentAfterDot); - emit(node.name); - decreaseIndentIf(indentBeforeDot, indentAfterDot); - } - function needsDotDotForPropertyAccess(expression) { - if (expression.kind === 8) { - var text = getLiteralTextOfNode(expression); - return text.indexOf(ts.tokenToString(21)) < 0; - } - else { - var constantValue = tryGetConstEnumValue(expression); - return isFinite(constantValue) && Math.floor(constantValue) === constantValue; - } - } - function emitElementAccessExpression(node) { - if (tryEmitConstantValue(node)) { - return; - } - emitExpression(node.expression); - write("["); - emitExpression(node.argumentExpression); - write("]"); - } - function emitCallExpression(node) { - emitExpression(node.expression); - emitExpressionList(node, node.arguments, 1296); - } - function emitNewExpression(node) { - write("new "); - emitExpression(node.expression); - emitExpressionList(node, node.arguments, 9488); - } - function emitTaggedTemplateExpression(node) { - emitExpression(node.tag); - write(" "); - emitExpression(node.template); - } - function emitTypeAssertionExpression(node) { - if (node.type) { - write("<"); - emit(node.type); - write(">"); - } - emitExpression(node.expression); - } - function emitParenthesizedExpression(node) { - write("("); - emitExpression(node.expression); - write(")"); - } - function emitFunctionExpression(node) { - emitFunctionDeclarationOrExpression(node); - } - function emitArrowFunction(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - emitSignatureAndBody(node, emitArrowFunctionHead); - } - function emitArrowFunctionHead(node) { - emitTypeParameters(node, node.typeParameters); - emitParametersForArrow(node, node.parameters); - emitWithPrefix(": ", node.type); - write(" =>"); - } - function emitDeleteExpression(node) { - write("delete "); - emitExpression(node.expression); - } - function emitTypeOfExpression(node) { - write("typeof "); - emitExpression(node.expression); - } - function emitVoidExpression(node) { - write("void "); - emitExpression(node.expression); - } - function emitAwaitExpression(node) { - write("await "); - emitExpression(node.expression); - } - function emitPrefixUnaryExpression(node) { - writeTokenText(node.operator); - if (shouldEmitWhitespaceBeforeOperand(node)) { - write(" "); - } - emitExpression(node.operand); - } - function shouldEmitWhitespaceBeforeOperand(node) { - var operand = node.operand; - return operand.kind === 185 - && ((node.operator === 35 && (operand.operator === 35 || operand.operator === 41)) - || (node.operator === 36 && (operand.operator === 36 || operand.operator === 42))); - } - function emitPostfixUnaryExpression(node) { - emitExpression(node.operand); - writeTokenText(node.operator); - } - function emitBinaryExpression(node) { - var isCommaOperator = node.operatorToken.kind !== 24; - var indentBeforeOperator = needsIndentation(node, node.left, node.operatorToken); - var indentAfterOperator = needsIndentation(node, node.operatorToken, node.right); - emitExpression(node.left); - increaseIndentIf(indentBeforeOperator, isCommaOperator ? " " : undefined); - writeTokenText(node.operatorToken.kind); - increaseIndentIf(indentAfterOperator, " "); - emitExpression(node.right); - decreaseIndentIf(indentBeforeOperator, indentAfterOperator); - } - function emitConditionalExpression(node) { - var indentBeforeQuestion = needsIndentation(node, node.condition, node.questionToken); - var indentAfterQuestion = needsIndentation(node, node.questionToken, node.whenTrue); - var indentBeforeColon = needsIndentation(node, node.whenTrue, node.colonToken); - var indentAfterColon = needsIndentation(node, node.colonToken, node.whenFalse); - emitExpression(node.condition); - increaseIndentIf(indentBeforeQuestion, " "); - write("?"); - increaseIndentIf(indentAfterQuestion, " "); - emitExpression(node.whenTrue); - decreaseIndentIf(indentBeforeQuestion, indentAfterQuestion); - increaseIndentIf(indentBeforeColon, " "); - write(":"); - increaseIndentIf(indentAfterColon, " "); - emitExpression(node.whenFalse); - decreaseIndentIf(indentBeforeColon, indentAfterColon); - } - function emitTemplateExpression(node) { - emit(node.head); - emitList(node, node.templateSpans, 131072); - } - function emitYieldExpression(node) { - write(node.asteriskToken ? "yield*" : "yield"); - emitExpressionWithPrefix(" ", node.expression); - } - function emitSpreadElementExpression(node) { - write("..."); - emitExpression(node.expression); - } - function emitClassExpression(node) { - emitClassDeclarationOrExpression(node); - } - function emitExpressionWithTypeArguments(node) { - emitExpression(node.expression); - emitTypeArguments(node, node.typeArguments); - } - function emitAsExpression(node) { - emitExpression(node.expression); - if (node.type) { - write(" as "); - emit(node.type); + return emitExternalModuleReference(node); + case 244: + return emitJsxText(node); + case 243: + return emitJsxOpeningElement(node); + case 245: + return emitJsxClosingElement(node); + case 246: + return emitJsxAttribute(node); + case 247: + return emitJsxSpreadAttribute(node); + case 248: + return emitJsxExpression(node); + case 249: + return emitCaseClause(node); + case 250: + return emitDefaultClause(node); + case 251: + return emitHeritageClause(node); + case 252: + return emitCatchClause(node); + case 253: + return emitPropertyAssignment(node); + case 254: + return emitShorthandPropertyAssignment(node); + case 255: + return emitEnumMember(node); + } + if (ts.isExpression(node)) { + return pipelineEmitWithSubstitution(1, node); } } - function emitNonNullExpression(node) { - emitExpression(node.expression); - write("!"); + function pipelineEmitInExpressionContext(node) { + var kind = node.kind; + switch (kind) { + case 8: + return emitNumericLiteral(node); + case 9: + case 10: + case 11: + return emitLiteral(node); + case 69: + return emitIdentifier(node); + case 84: + case 93: + case 95: + case 99: + case 97: + writeTokenText(kind); + return; + case 170: + return emitArrayLiteralExpression(node); + case 171: + return emitObjectLiteralExpression(node); + case 172: + return emitPropertyAccessExpression(node); + case 173: + return emitElementAccessExpression(node); + case 174: + return emitCallExpression(node); + case 175: + return emitNewExpression(node); + case 176: + return emitTaggedTemplateExpression(node); + case 177: + return emitTypeAssertionExpression(node); + case 178: + return emitParenthesizedExpression(node); + case 179: + return emitFunctionExpression(node); + case 180: + return emitArrowFunction(node); + case 181: + return emitDeleteExpression(node); + case 182: + return emitTypeOfExpression(node); + case 183: + return emitVoidExpression(node); + case 184: + return emitAwaitExpression(node); + case 185: + return emitPrefixUnaryExpression(node); + case 186: + return emitPostfixUnaryExpression(node); + case 187: + return emitBinaryExpression(node); + case 188: + return emitConditionalExpression(node); + case 189: + return emitTemplateExpression(node); + case 190: + return emitYieldExpression(node); + case 191: + return emitSpreadElementExpression(node); + case 192: + return emitClassExpression(node); + case 193: + return; + case 195: + return emitAsExpression(node); + case 196: + return emitNonNullExpression(node); + case 241: + return emitJsxElement(node); + case 242: + return emitJsxSelfClosingElement(node); + case 288: + return emitPartiallyEmittedExpression(node); + } } - function emitTemplateSpan(node) { - emitExpression(node.expression); - emit(node.literal); + function emitNumericLiteral(node) { + emitLiteral(node); + if (node.trailingComment) { + write(" /*" + node.trailingComment + "*/"); + } } - function emitBlock(node, format) { - if (isSingleLineEmptyBlock(node)) { - writeToken(15, node.pos, node); - write(" "); - writeToken(16, node.statements.end, node); + function emitLiteral(node) { + var text = getLiteralTextOfNode(node); + if ((compilerOptions.sourceMap || compilerOptions.inlineSourceMap) + && (node.kind === 9 || ts.isTemplateLiteralKind(node.kind))) { + writer.writeLiteral(text); } else { - writeToken(15, node.pos, node); - emitBlockStatements(node); - writeToken(16, node.statements.end, node); + write(text); } } - function emitBlockStatements(node) { - if (node.emitFlags & 32) { - emitList(node, node.statements, 384); + function emitIdentifier(node) { + if (ts.getEmitFlags(node) & 16) { + writeLines(umdHelper); } else { - emitList(node, node.statements, 65); + write(getTextOfNode(node, false)); } } - function emitVariableStatement(node) { - emitModifiers(node, node.modifiers); - emit(node.declarationList); - write(";"); - } - function emitEmptyStatement(node) { - write(";"); - } - function emitExpressionStatement(node) { - emitExpression(node.expression); - write(";"); - } - function emitIfStatement(node) { - var openParenPos = writeToken(88, node.pos, node); - write(" "); - writeToken(17, openParenPos, node); - emitExpression(node.expression); - writeToken(18, node.expression.end, node); - emitEmbeddedStatement(node.thenStatement); - if (node.elseStatement) { - writeLine(); - writeToken(80, node.thenStatement.end, node); - if (node.elseStatement.kind === 203) { - write(" "); - emit(node.elseStatement); - } - else { - emitEmbeddedStatement(node.elseStatement); - } - } + function emitQualifiedName(node) { + emitEntityName(node.left); + write("."); + emit(node.right); } - function emitDoStatement(node) { - write("do"); - emitEmbeddedStatement(node.statement); - if (ts.isBlock(node.statement)) { - write(" "); + function emitEntityName(node) { + if (node.kind === 69) { + emitExpression(node); } else { - writeLine(); + emit(node); } - write("while ("); - emitExpression(node.expression); - write(");"); - } - function emitWhileStatement(node) { - write("while ("); - emitExpression(node.expression); - write(")"); - emitEmbeddedStatement(node.statement); - } - function emitForStatement(node) { - var openParenPos = writeToken(86, node.pos); - write(" "); - writeToken(17, openParenPos, node); - emitForBinding(node.initializer); - write(";"); - emitExpressionWithPrefix(" ", node.condition); - write(";"); - emitExpressionWithPrefix(" ", node.incrementor); - write(")"); - emitEmbeddedStatement(node.statement); - } - function emitForInStatement(node) { - var openParenPos = writeToken(86, node.pos); - write(" "); - writeToken(17, openParenPos); - emitForBinding(node.initializer); - write(" in "); - emitExpression(node.expression); - writeToken(18, node.expression.end); - emitEmbeddedStatement(node.statement); } - function emitForOfStatement(node) { - var openParenPos = writeToken(86, node.pos); - write(" "); - writeToken(17, openParenPos); - emitForBinding(node.initializer); - write(" of "); + function emitComputedPropertyName(node) { + write("["); emitExpression(node.expression); - writeToken(18, node.expression.end); - emitEmbeddedStatement(node.statement); + write("]"); } - function emitForBinding(node) { - if (node !== undefined) { - if (node.kind === 219) { - emit(node); - } - else { - emitExpression(node); - } - } + function emitTypeParameter(node) { + emit(node.name); + emitWithPrefix(" extends ", node.constraint); } - function emitContinueStatement(node) { - writeToken(75, node.pos); - emitWithPrefix(" ", node.label); - write(";"); + function emitParameter(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + writeIfPresent(node.dotDotDotToken, "..."); + emit(node.name); + writeIfPresent(node.questionToken, "?"); + emitExpressionWithPrefix(" = ", node.initializer); + emitWithPrefix(": ", node.type); } - function emitBreakStatement(node) { - writeToken(70, node.pos); - emitWithPrefix(" ", node.label); - write(";"); + function emitDecorator(decorator) { + write("@"); + emitExpression(decorator.expression); } - function emitReturnStatement(node) { - writeToken(94, node.pos, node); - emitExpressionWithPrefix(" ", node.expression); + function emitPropertySignature(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + emit(node.name); + writeIfPresent(node.questionToken, "?"); + emitWithPrefix(": ", node.type); write(";"); } - function emitWithStatement(node) { - write("with ("); - emitExpression(node.expression); - write(")"); - emitEmbeddedStatement(node.statement); - } - function emitSwitchStatement(node) { - var openParenPos = writeToken(96, node.pos); - write(" "); - writeToken(17, openParenPos); - emitExpression(node.expression); - writeToken(18, node.expression.end); - write(" "); - emit(node.caseBlock); - } - function emitLabeledStatement(node) { - emit(node.label); - write(": "); - emit(node.statement); - } - function emitThrowStatement(node) { - write("throw"); - emitExpressionWithPrefix(" ", node.expression); + function emitPropertyDeclaration(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + emit(node.name); + emitWithPrefix(": ", node.type); + emitExpressionWithPrefix(" = ", node.initializer); write(";"); } - function emitTryStatement(node) { - write("try "); - emit(node.tryBlock); - emit(node.catchClause); - if (node.finallyBlock) { - writeLine(); - write("finally "); - emit(node.finallyBlock); - } - } - function emitDebuggerStatement(node) { - writeToken(76, node.pos); + function emitMethodSignature(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + emit(node.name); + writeIfPresent(node.questionToken, "?"); + emitTypeParameters(node, node.typeParameters); + emitParameters(node, node.parameters); + emitWithPrefix(": ", node.type); write(";"); } - function emitVariableDeclaration(node) { + function emitMethodDeclaration(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + writeIfPresent(node.asteriskToken, "*"); emit(node.name); - emitExpressionWithPrefix(" = ", node.initializer); - } - function emitVariableDeclarationList(node) { - write(ts.isLet(node) ? "let " : ts.isConst(node) ? "const " : "var "); - emitList(node, node.declarations, 272); + emitSignatureAndBody(node, emitSignatureHead); } - function emitFunctionDeclaration(node) { - emitFunctionDeclarationOrExpression(node); + function emitConstructor(node) { + emitModifiers(node, node.modifiers); + write("constructor"); + emitSignatureAndBody(node, emitSignatureHead); } - function emitFunctionDeclarationOrExpression(node) { + function emitAccessorDeclaration(node) { emitDecorators(node, node.decorators); emitModifiers(node, node.modifiers); - write(node.asteriskToken ? "function* " : "function "); - emitIdentifierName(node.name); + write(node.kind === 149 ? "get " : "set "); + emit(node.name); emitSignatureAndBody(node, emitSignatureHead); } - function emitSignatureAndBody(node, emitSignatureHead) { - var body = node.body; - if (body) { - if (ts.isBlock(body)) { - var indentedFlag = node.emitFlags & 524288; - if (indentedFlag) { - increaseIndent(); - } - if (node.emitFlags & 4194304) { - emitSignatureHead(node); - emitBlockFunctionBody(node, body); - } - else { - var savedTempFlags = tempFlags; - tempFlags = 0; - emitSignatureHead(node); - emitBlockFunctionBody(node, body); - tempFlags = savedTempFlags; - } - if (indentedFlag) { - decreaseIndent(); - } - } - else { - emitSignatureHead(node); - write(" "); - emitExpression(body); - } - } - else { - emitSignatureHead(node); - write(";"); - } - } - function emitSignatureHead(node) { + function emitCallSignature(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); emitTypeParameters(node, node.typeParameters); emitParameters(node, node.parameters); emitWithPrefix(": ", node.type); + write(";"); } - function shouldEmitBlockFunctionBodyOnSingleLine(parentNode, body) { - if (body.emitFlags & 32) { - return true; - } - if (body.multiLine) { - return false; - } - if (!ts.nodeIsSynthesized(body) && !ts.rangeIsOnSingleLine(body, currentSourceFile)) { - return false; - } - if (shouldWriteLeadingLineTerminator(body, body.statements, 2) - || shouldWriteClosingLineTerminator(body, body.statements, 2)) { - return false; - } - var previousStatement; - for (var _a = 0, _b = body.statements; _a < _b.length; _a++) { - var statement = _b[_a]; - if (shouldWriteSeparatingLineTerminator(previousStatement, statement, 2)) { - return false; - } - previousStatement = statement; - } - return true; - } - function emitBlockFunctionBody(parentNode, body) { - write(" {"); - increaseIndent(); - emitBodyWithDetachedComments(body, body.statements, shouldEmitBlockFunctionBodyOnSingleLine(parentNode, body) - ? emitBlockFunctionBodyOnSingleLine - : emitBlockFunctionBodyWorker); - decreaseIndent(); - writeToken(16, body.statements.end, body); - } - function emitBlockFunctionBodyOnSingleLine(body) { - emitBlockFunctionBodyWorker(body, true); - } - function emitBlockFunctionBodyWorker(body, emitBlockFunctionBodyOnSingleLine) { - var statementOffset = emitPrologueDirectives(body.statements, true); - var helpersEmitted = emitHelpers(body); - if (statementOffset === 0 && !helpersEmitted && emitBlockFunctionBodyOnSingleLine) { - decreaseIndent(); - emitList(body, body.statements, 384); - increaseIndent(); - } - else { - emitList(body, body.statements, 1, statementOffset); - } - } - function emitClassDeclaration(node) { - emitClassDeclarationOrExpression(node); - } - function emitClassDeclarationOrExpression(node) { + function emitConstructSignature(node) { emitDecorators(node, node.decorators); emitModifiers(node, node.modifiers); - write("class"); - emitNodeWithPrefix(" ", node.name, emitIdentifierName); - var indentedFlag = node.emitFlags & 524288; - if (indentedFlag) { - increaseIndent(); - } + write("new "); emitTypeParameters(node, node.typeParameters); - emitList(node, node.heritageClauses, 256); - var savedTempFlags = tempFlags; - tempFlags = 0; - write(" {"); - emitList(node, node.members, 65); - write("}"); - if (indentedFlag) { - decreaseIndent(); - } - tempFlags = savedTempFlags; + emitParameters(node, node.parameters); + emitWithPrefix(": ", node.type); + write(";"); } - function emitInterfaceDeclaration(node) { + function emitIndexSignature(node) { emitDecorators(node, node.decorators); emitModifiers(node, node.modifiers); - write("interface "); - emit(node.name); + emitParametersForIndexSignature(node, node.parameters); + emitWithPrefix(": ", node.type); + write(";"); + } + function emitSemicolonClassElement(node) { + write(";"); + } + function emitTypePredicate(node) { + emit(node.parameterName); + write(" is "); + emit(node.type); + } + function emitTypeReference(node) { + emit(node.typeName); + emitTypeArguments(node, node.typeArguments); + } + function emitFunctionType(node) { emitTypeParameters(node, node.typeParameters); - emitList(node, node.heritageClauses, 256); - write(" {"); - emitList(node, node.members, 65); - write("}"); + emitParametersForArrow(node, node.parameters); + write(" => "); + emit(node.type); } - function emitTypeAliasDeclaration(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - write("type "); - emit(node.name); + function emitConstructorType(node) { + write("new "); emitTypeParameters(node, node.typeParameters); - write(" = "); + emitParametersForArrow(node, node.parameters); + write(" => "); emit(node.type); - write(";"); } - function emitEnumDeclaration(node) { - emitModifiers(node, node.modifiers); - write("enum "); - emit(node.name); - var savedTempFlags = tempFlags; - tempFlags = 0; - write(" {"); - emitList(node, node.members, 81); + function emitTypeQuery(node) { + write("typeof "); + emit(node.exprName); + } + function emitTypeLiteral(node) { + write("{"); + emitList(node, node.members, 65); write("}"); - tempFlags = savedTempFlags; } - function emitModuleDeclaration(node) { - emitModifiers(node, node.modifiers); - write(node.flags & 16 ? "namespace " : "module "); - emit(node.name); - var body = node.body; - while (body.kind === 225) { - write("."); - emit(body.name); - body = body.body; - } - write(" "); - emit(body); + function emitArrayType(node) { + emit(node.elementType); + write("[]"); + } + function emitTupleType(node) { + write("["); + emitList(node, node.elementTypes, 336); + write("]"); + } + function emitUnionType(node) { + emitList(node, node.types, 260); + } + function emitIntersectionType(node) { + emitList(node, node.types, 264); + } + function emitParenthesizedType(node) { + write("("); + emit(node.type); + write(")"); + } + function emitThisType(node) { + write("this"); } - function emitModuleBlock(node) { - if (isSingleLineEmptyBlock(node)) { - write("{ }"); + function emitLiteralType(node) { + emitExpression(node.literal); + } + function emitObjectBindingPattern(node) { + var elements = node.elements; + if (elements.length === 0) { + write("{}"); } else { - var savedTempFlags = tempFlags; - tempFlags = 0; write("{"); - increaseIndent(); - emitBlockStatements(node); + emitList(node, elements, 432); write("}"); - tempFlags = savedTempFlags; } } - function emitCaseBlock(node) { - writeToken(15, node.pos); - emitList(node, node.clauses, 65); - writeToken(16, node.clauses.end); + function emitArrayBindingPattern(node) { + var elements = node.elements; + if (elements.length === 0) { + write("[]"); + } + else { + write("["); + emitList(node, node.elements, 304); + write("]"); + } } - function emitImportEqualsDeclaration(node) { - emitModifiers(node, node.modifiers); - write("import "); + function emitBindingElement(node) { + emitWithSuffix(node.propertyName, ": "); + writeIfPresent(node.dotDotDotToken, "..."); emit(node.name); - write(" = "); - emitModuleReference(node.moduleReference); - write(";"); + emitExpressionWithPrefix(" = ", node.initializer); } - function emitModuleReference(node) { - if (node.kind === 69) { - emitExpression(node); + function emitArrayLiteralExpression(node) { + var elements = node.elements; + if (elements.length === 0) { + write("[]"); } else { - emit(node); + var preferNewLine = node.multiLine ? 32768 : 0; + emitExpressionList(node, elements, 4466 | preferNewLine); } } - function emitImportDeclaration(node) { - emitModifiers(node, node.modifiers); - write("import "); - if (node.importClause) { - emit(node.importClause); - write(" from "); + function emitObjectLiteralExpression(node) { + var properties = node.properties; + if (properties.length === 0) { + write("{}"); } - emitExpression(node.moduleSpecifier); - write(";"); - } - function emitImportClause(node) { - emit(node.name); - if (node.name && node.namedBindings) { - write(", "); + else { + var indentedFlag = ts.getEmitFlags(node) & 524288; + if (indentedFlag) { + increaseIndent(); + } + var preferNewLine = node.multiLine ? 32768 : 0; + var allowTrailingComma = languageVersion >= 1 ? 32 : 0; + emitList(node, properties, 978 | allowTrailingComma | preferNewLine); + if (indentedFlag) { + decreaseIndent(); + } } - emit(node.namedBindings); } - function emitNamespaceImport(node) { - write("* as "); - emit(node.name); - } - function emitNamedImports(node) { - emitNamedImportsOrExports(node); - } - function emitImportSpecifier(node) { - emitImportOrExportSpecifier(node); - } - function emitExportAssignment(node) { - write(node.isExportEquals ? "export = " : "export default "); + function emitPropertyAccessExpression(node) { + var indentBeforeDot = false; + var indentAfterDot = false; + if (!(ts.getEmitFlags(node) & 1048576)) { + var dotRangeStart = node.expression.end; + var dotRangeEnd = ts.skipTrivia(currentText, node.expression.end) + 1; + var dotToken = { kind: 21, pos: dotRangeStart, end: dotRangeEnd }; + indentBeforeDot = needsIndentation(node, node.expression, dotToken); + indentAfterDot = needsIndentation(node, dotToken, node.name); + } emitExpression(node.expression); - write(";"); + increaseIndentIf(indentBeforeDot); + var shouldEmitDotDot = !indentBeforeDot && needsDotDotForPropertyAccess(node.expression); + write(shouldEmitDotDot ? ".." : "."); + increaseIndentIf(indentAfterDot); + emit(node.name); + decreaseIndentIf(indentBeforeDot, indentAfterDot); } - function emitExportDeclaration(node) { - write("export "); - if (node.exportClause) { - emit(node.exportClause); - } - else { - write("*"); + function needsDotDotForPropertyAccess(expression) { + if (expression.kind === 8) { + var text = getLiteralTextOfNode(expression); + return text.indexOf(ts.tokenToString(21)) < 0; } - if (node.moduleSpecifier) { - write(" from "); - emitExpression(node.moduleSpecifier); + else if (ts.isPropertyAccessExpression(expression) || ts.isElementAccessExpression(expression)) { + var constantValue = ts.getConstantValue(expression); + return isFinite(constantValue) + && Math.floor(constantValue) === constantValue + && compilerOptions.removeComments; } - write(";"); } - function emitNamedExports(node) { - emitNamedImportsOrExports(node); + function emitElementAccessExpression(node) { + emitExpression(node.expression); + write("["); + emitExpression(node.argumentExpression); + write("]"); } - function emitExportSpecifier(node) { - emitImportOrExportSpecifier(node); + function emitCallExpression(node) { + emitExpression(node.expression); + emitExpressionList(node, node.arguments, 1296); } - function emitNamedImportsOrExports(node) { - write("{"); - emitList(node, node.elements, 432); - write("}"); + function emitNewExpression(node) { + write("new "); + emitExpression(node.expression); + emitExpressionList(node, node.arguments, 9488); } - function emitImportOrExportSpecifier(node) { - if (node.propertyName) { - emit(node.propertyName); - write(" as "); + function emitTaggedTemplateExpression(node) { + emitExpression(node.tag); + write(" "); + emitExpression(node.template); + } + function emitTypeAssertionExpression(node) { + if (node.type) { + write("<"); + emit(node.type); + write(">"); } - emit(node.name); + emitExpression(node.expression); } - function emitExternalModuleReference(node) { - write("require("); + function emitParenthesizedExpression(node) { + write("("); emitExpression(node.expression); write(")"); } - function emitJsxElement(node) { - emit(node.openingElement); - emitList(node, node.children, 131072); - emit(node.closingElement); - } - function emitJsxSelfClosingElement(node) { - write("<"); - emitJsxTagName(node.tagName); - write(" "); - emitList(node, node.attributes, 131328); - write("/>"); - } - function emitJsxOpeningElement(node) { - write("<"); - emitJsxTagName(node.tagName); - writeIfAny(node.attributes, " "); - emitList(node, node.attributes, 131328); - write(">"); - } - function emitJsxText(node) { - writer.writeLiteral(getTextOfNode(node, true)); + function emitFunctionExpression(node) { + emitFunctionDeclarationOrExpression(node); } - function emitJsxClosingElement(node) { - write(""); + function emitArrowFunction(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + emitSignatureAndBody(node, emitArrowFunctionHead); } - function emitJsxAttribute(node) { - emit(node.name); - emitWithPrefix("=", node.initializer); + function emitArrowFunctionHead(node) { + emitTypeParameters(node, node.typeParameters); + emitParametersForArrow(node, node.parameters); + emitWithPrefix(": ", node.type); + write(" =>"); } - function emitJsxSpreadAttribute(node) { - write("{..."); + function emitDeleteExpression(node) { + write("delete "); emitExpression(node.expression); - write("}"); - } - function emitJsxExpression(node) { - if (node.expression) { - write("{"); - emitExpression(node.expression); - write("}"); - } } - function emitJsxTagName(node) { - if (node.kind === 69) { - emitExpression(node); - } - else { - emit(node); - } + function emitTypeOfExpression(node) { + write("typeof "); + emitExpression(node.expression); } - function emitCaseClause(node) { - write("case "); + function emitVoidExpression(node) { + write("void "); emitExpression(node.expression); - write(":"); - emitCaseOrDefaultClauseStatements(node, node.statements); } - function emitDefaultClause(node) { - write("default:"); - emitCaseOrDefaultClauseStatements(node, node.statements); + function emitAwaitExpression(node) { + write("await "); + emitExpression(node.expression); } - function emitCaseOrDefaultClauseStatements(parentNode, statements) { - var emitAsSingleStatement = statements.length === 1 && - (ts.nodeIsSynthesized(parentNode) || - ts.nodeIsSynthesized(statements[0]) || - ts.rangeStartPositionsAreOnSameLine(parentNode, statements[0], currentSourceFile)); - if (emitAsSingleStatement) { + function emitPrefixUnaryExpression(node) { + writeTokenText(node.operator); + if (shouldEmitWhitespaceBeforeOperand(node)) { write(" "); - emit(statements[0]); - } - else { - emitList(parentNode, statements, 81985); } + emitExpression(node.operand); } - function emitHeritageClause(node) { - write(" "); - writeTokenText(node.token); - write(" "); - emitList(node, node.types, 272); - } - function emitCatchClause(node) { - writeLine(); - var openParenPos = writeToken(72, node.pos); - write(" "); - writeToken(17, openParenPos); - emit(node.variableDeclaration); - writeToken(18, node.variableDeclaration ? node.variableDeclaration.end : openParenPos); - write(" "); - emit(node.block); + function shouldEmitWhitespaceBeforeOperand(node) { + var operand = node.operand; + return operand.kind === 185 + && ((node.operator === 35 && (operand.operator === 35 || operand.operator === 41)) + || (node.operator === 36 && (operand.operator === 36 || operand.operator === 42))); } - function emitPropertyAssignment(node) { - emit(node.name); - write(": "); - var initializer = node.initializer; - if (!shouldSkipLeadingCommentsForNode(initializer)) { - var commentRange = initializer.commentRange || initializer; - emitTrailingCommentsOfPosition(commentRange.pos); - } - emitExpression(initializer); + function emitPostfixUnaryExpression(node) { + emitExpression(node.operand); + writeTokenText(node.operator); } - function emitShorthandPropertyAssignment(node) { - emit(node.name); - if (node.objectAssignmentInitializer) { - write(" = "); - emitExpression(node.objectAssignmentInitializer); - } + function emitBinaryExpression(node) { + var isCommaOperator = node.operatorToken.kind !== 24; + var indentBeforeOperator = needsIndentation(node, node.left, node.operatorToken); + var indentAfterOperator = needsIndentation(node, node.operatorToken, node.right); + emitExpression(node.left); + increaseIndentIf(indentBeforeOperator, isCommaOperator ? " " : undefined); + writeTokenText(node.operatorToken.kind); + increaseIndentIf(indentAfterOperator, " "); + emitExpression(node.right); + decreaseIndentIf(indentBeforeOperator, indentAfterOperator); } - function emitEnumMember(node) { - emit(node.name); - emitExpressionWithPrefix(" = ", node.initializer); + function emitConditionalExpression(node) { + var indentBeforeQuestion = needsIndentation(node, node.condition, node.questionToken); + var indentAfterQuestion = needsIndentation(node, node.questionToken, node.whenTrue); + var indentBeforeColon = needsIndentation(node, node.whenTrue, node.colonToken); + var indentAfterColon = needsIndentation(node, node.colonToken, node.whenFalse); + emitExpression(node.condition); + increaseIndentIf(indentBeforeQuestion, " "); + write("?"); + increaseIndentIf(indentAfterQuestion, " "); + emitExpression(node.whenTrue); + decreaseIndentIf(indentBeforeQuestion, indentAfterQuestion); + increaseIndentIf(indentBeforeColon, " "); + write(":"); + increaseIndentIf(indentAfterColon, " "); + emitExpression(node.whenFalse); + decreaseIndentIf(indentBeforeColon, indentAfterColon); } - function emitSourceFile(node) { - writeLine(); - emitShebang(); - emitBodyWithDetachedComments(node, node.statements, emitSourceFileWorker); + function emitTemplateExpression(node) { + emit(node.head); + emitList(node, node.templateSpans, 131072); } - function emitSourceFileWorker(node) { - var statements = node.statements; - var statementOffset = emitPrologueDirectives(statements); - var savedTempFlags = tempFlags; - tempFlags = 0; - emitHelpers(node); - emitList(node, statements, 1, statementOffset); - tempFlags = savedTempFlags; + function emitYieldExpression(node) { + write(node.asteriskToken ? "yield*" : "yield"); + emitExpressionWithPrefix(" ", node.expression); } - function emitPartiallyEmittedExpression(node) { + function emitSpreadElementExpression(node) { + write("..."); emitExpression(node.expression); } - function emitPrologueDirectives(statements, startWithNewLine) { - for (var i = 0; i < statements.length; i++) { - if (ts.isPrologueDirective(statements[i])) { - if (startWithNewLine || i > 0) { - writeLine(); - } - emit(statements[i]); - } - else { - return i; - } - } - return statements.length; + function emitClassExpression(node) { + emitClassDeclarationOrExpression(node); } - function emitHelpers(node) { - var emitFlags = node.emitFlags; - var helpersEmitted = false; - if (emitFlags & 1) { - helpersEmitted = emitEmitHelpers(currentSourceFile); - } - if (emitFlags & 2) { - writeLines(exportStarHelper); - helpersEmitted = true; - } - if (emitFlags & 4) { - writeLines(superHelper); - helpersEmitted = true; - } - if (emitFlags & 8) { - writeLines(advancedSuperHelper); - helpersEmitted = true; - } - return helpersEmitted; + function emitExpressionWithTypeArguments(node) { + emitExpression(node.expression); + emitTypeArguments(node, node.typeArguments); } - function emitEmitHelpers(node) { - if (compilerOptions.noEmitHelpers) { - return false; - } - if (compilerOptions.importHelpers - && (ts.isExternalModule(node) || compilerOptions.isolatedModules)) { - return false; - } - var helpersEmitted = false; - if ((languageVersion < 2) && (!extendsEmitted && node.flags & 1024)) { - writeLines(extendsHelper); - extendsEmitted = true; - helpersEmitted = true; - } - if (compilerOptions.jsx !== 1 && !assignEmitted && (node.flags & 16384)) { - writeLines(assignHelper); - assignEmitted = true; - } - if (!decorateEmitted && node.flags & 2048) { - writeLines(decorateHelper); - if (compilerOptions.emitDecoratorMetadata) { - writeLines(metadataHelper); - } - decorateEmitted = true; - helpersEmitted = true; - } - if (!paramEmitted && node.flags & 4096) { - writeLines(paramHelper); - paramEmitted = true; - helpersEmitted = true; - } - if (!awaiterEmitted && node.flags & 8192) { - writeLines(awaiterHelper); - if (languageVersion < 2) { - writeLines(generatorHelper); - } - awaiterEmitted = true; - helpersEmitted = true; - } - if (helpersEmitted) { - writeLine(); + function emitAsExpression(node) { + emitExpression(node.expression); + if (node.type) { + write(" as "); + emit(node.type); } - return helpersEmitted; } - function writeLines(text) { - var lines = text.split(/\r\n|\r|\n/g); - for (var i = 0; i < lines.length; i++) { - var line = lines[i]; - if (line.length) { - if (i > 0) { - writeLine(); - } - write(line); - } - } + function emitNonNullExpression(node) { + emitExpression(node.expression); + write("!"); } - function emitShebang() { - var shebang = ts.getShebang(currentText); - if (shebang) { - write(shebang); - writeLine(); - } + function emitTemplateSpan(node) { + emitExpression(node.expression); + emit(node.literal); } - function emitModifiers(node, modifiers) { - if (modifiers && modifiers.length) { - emitList(node, modifiers, 256); + function emitBlock(node, format) { + if (isSingleLineEmptyBlock(node)) { + writeToken(15, node.pos, node); write(" "); + writeToken(16, node.statements.end, node); + } + else { + writeToken(15, node.pos, node); + emitBlockStatements(node); + writeToken(16, node.statements.end, node); } } - function emitWithPrefix(prefix, node) { - emitNodeWithPrefix(prefix, node, emit); + function emitBlockStatements(node) { + if (ts.getEmitFlags(node) & 32) { + emitList(node, node.statements, 384); + } + else { + emitList(node, node.statements, 65); + } } - function emitExpressionWithPrefix(prefix, node) { - emitNodeWithPrefix(prefix, node, emitExpression); + function emitVariableStatement(node) { + emitModifiers(node, node.modifiers); + emit(node.declarationList); + write(";"); } - function emitNodeWithPrefix(prefix, node, emit) { - if (node) { - write(prefix); - emit(node); - } + function emitEmptyStatement(node) { + write(";"); } - function emitWithSuffix(node, suffix) { - if (node) { - emit(node); - write(suffix); - } + function emitExpressionStatement(node) { + emitExpression(node.expression); + write(";"); } - function tryEmitSubstitute(node, emitNode, isExpression) { - if (isSubstitutionEnabled(node) && (node.emitFlags & 128) === 0) { - var substitute = onSubstituteNode(node, isExpression); - if (substitute !== node) { - substitute.emitFlags |= 128; - emitNode(substitute); - return true; + function emitIfStatement(node) { + var openParenPos = writeToken(88, node.pos, node); + write(" "); + writeToken(17, openParenPos, node); + emitExpression(node.expression); + writeToken(18, node.expression.end, node); + emitEmbeddedStatement(node.thenStatement); + if (node.elseStatement) { + writeLine(); + writeToken(80, node.thenStatement.end, node); + if (node.elseStatement.kind === 203) { + write(" "); + emit(node.elseStatement); } - } - return false; - } - function tryEmitConstantValue(node) { - var constantValue = tryGetConstEnumValue(node); - if (constantValue !== undefined) { - write(String(constantValue)); - if (!compilerOptions.removeComments) { - var propertyName = ts.isPropertyAccessExpression(node) - ? ts.declarationNameToString(node.name) - : getTextOfNode(node.argumentExpression); - write(" /* " + propertyName + " */"); + else { + emitEmbeddedStatement(node.elseStatement); } - return true; } - return false; } - function emitEmbeddedStatement(node) { - if (ts.isBlock(node)) { + function emitDoStatement(node) { + write("do"); + emitEmbeddedStatement(node.statement); + if (ts.isBlock(node.statement)) { write(" "); - emit(node); } else { writeLine(); - increaseIndent(); - emit(node); - decreaseIndent(); } + write("while ("); + emitExpression(node.expression); + write(");"); } - function emitDecorators(parentNode, decorators) { - emitList(parentNode, decorators, 24577); + function emitWhileStatement(node) { + write("while ("); + emitExpression(node.expression); + write(")"); + emitEmbeddedStatement(node.statement); } - function emitTypeArguments(parentNode, typeArguments) { - emitList(parentNode, typeArguments, 26960); + function emitForStatement(node) { + var openParenPos = writeToken(86, node.pos); + write(" "); + writeToken(17, openParenPos, node); + emitForBinding(node.initializer); + write(";"); + emitExpressionWithPrefix(" ", node.condition); + write(";"); + emitExpressionWithPrefix(" ", node.incrementor); + write(")"); + emitEmbeddedStatement(node.statement); } - function emitTypeParameters(parentNode, typeParameters) { - emitList(parentNode, typeParameters, 26960); + function emitForInStatement(node) { + var openParenPos = writeToken(86, node.pos); + write(" "); + writeToken(17, openParenPos); + emitForBinding(node.initializer); + write(" in "); + emitExpression(node.expression); + writeToken(18, node.expression.end); + emitEmbeddedStatement(node.statement); } - function emitParameters(parentNode, parameters) { - emitList(parentNode, parameters, 1360); + function emitForOfStatement(node) { + var openParenPos = writeToken(86, node.pos); + write(" "); + writeToken(17, openParenPos); + emitForBinding(node.initializer); + write(" of "); + emitExpression(node.expression); + writeToken(18, node.expression.end); + emitEmbeddedStatement(node.statement); } - function emitParametersForArrow(parentNode, parameters) { - if (parameters && - parameters.length === 1 && - parameters[0].type === undefined && - parameters[0].pos === parentNode.pos) { - emit(parameters[0]); - } - else { - emitParameters(parentNode, parameters); + function emitForBinding(node) { + if (node !== undefined) { + if (node.kind === 219) { + emit(node); + } + else { + emitExpression(node); + } } } - function emitParametersForIndexSignature(parentNode, parameters) { - emitList(parentNode, parameters, 4432); + function emitContinueStatement(node) { + writeToken(75, node.pos); + emitWithPrefix(" ", node.label); + write(";"); } - function emitList(parentNode, children, format, start, count) { - emitNodeList(emit, parentNode, children, format, start, count); + function emitBreakStatement(node) { + writeToken(70, node.pos); + emitWithPrefix(" ", node.label); + write(";"); } - function emitExpressionList(parentNode, children, format, start, count) { - emitNodeList(emitExpression, parentNode, children, format, start, count); + function emitReturnStatement(node) { + writeToken(94, node.pos, node); + emitExpressionWithPrefix(" ", node.expression); + write(";"); } - function emitNodeList(emit, parentNode, children, format, start, count) { - if (start === void 0) { start = 0; } - if (count === void 0) { count = children ? children.length - start : 0; } - var isUndefined = children === undefined; - if (isUndefined && format & 8192) { - return; - } - var isEmpty = isUndefined || children.length === 0 || start >= children.length || count === 0; - if (isEmpty && format & 16384) { - return; - } - if (format & 7680) { - write(getOpeningBracket(format)); - } - if (isEmpty) { - if (format & 1) { - writeLine(); - } - else if (format & 128) { - write(" "); - } + function emitWithStatement(node) { + write("with ("); + emitExpression(node.expression); + write(")"); + emitEmbeddedStatement(node.statement); + } + function emitSwitchStatement(node) { + var openParenPos = writeToken(96, node.pos); + write(" "); + writeToken(17, openParenPos); + emitExpression(node.expression); + writeToken(18, node.expression.end); + write(" "); + emit(node.caseBlock); + } + function emitLabeledStatement(node) { + emit(node.label); + write(": "); + emit(node.statement); + } + function emitThrowStatement(node) { + write("throw"); + emitExpressionWithPrefix(" ", node.expression); + write(";"); + } + function emitTryStatement(node) { + write("try "); + emit(node.tryBlock); + emit(node.catchClause); + if (node.finallyBlock) { + writeLine(); + write("finally "); + emit(node.finallyBlock); } - else { - var mayEmitInterveningComments = (format & 131072) === 0; - var shouldEmitInterveningComments = mayEmitInterveningComments; - if (shouldWriteLeadingLineTerminator(parentNode, children, format)) { - writeLine(); - shouldEmitInterveningComments = false; - } - else if (format & 128) { - write(" "); - } - if (format & 64) { - increaseIndent(); - } - var previousSibling = void 0; - var shouldDecreaseIndentAfterEmit = void 0; - var delimiter = getDelimiter(format); - for (var i = 0; i < count; i++) { - var child = children[start + i]; - if (previousSibling) { - write(delimiter); - if (shouldWriteSeparatingLineTerminator(previousSibling, child, format)) { - if ((format & (3 | 64)) === 0) { - increaseIndent(); - shouldDecreaseIndentAfterEmit = true; - } - writeLine(); - shouldEmitInterveningComments = false; - } - else if (previousSibling && format & 256) { - write(" "); - } + } + function emitDebuggerStatement(node) { + writeToken(76, node.pos); + write(";"); + } + function emitVariableDeclaration(node) { + emit(node.name); + emitExpressionWithPrefix(" = ", node.initializer); + } + function emitVariableDeclarationList(node) { + write(ts.isLet(node) ? "let " : ts.isConst(node) ? "const " : "var "); + emitList(node, node.declarations, 272); + } + function emitFunctionDeclaration(node) { + emitFunctionDeclarationOrExpression(node); + } + function emitFunctionDeclarationOrExpression(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + write(node.asteriskToken ? "function* " : "function "); + emitIdentifierName(node.name); + emitSignatureAndBody(node, emitSignatureHead); + } + function emitSignatureAndBody(node, emitSignatureHead) { + var body = node.body; + if (body) { + if (ts.isBlock(body)) { + var indentedFlag = ts.getEmitFlags(node) & 524288; + if (indentedFlag) { + increaseIndent(); } - if (shouldEmitInterveningComments) { - var commentRange = child.commentRange || child; - emitTrailingCommentsOfPosition(commentRange.pos); + if (ts.getEmitFlags(node) & 4194304) { + emitSignatureHead(node); + emitBlockFunctionBody(node, body); } else { - shouldEmitInterveningComments = mayEmitInterveningComments; + var savedTempFlags = tempFlags; + tempFlags = 0; + emitSignatureHead(node); + emitBlockFunctionBody(node, body); + tempFlags = savedTempFlags; } - emit(child); - if (shouldDecreaseIndentAfterEmit) { + if (indentedFlag) { decreaseIndent(); - shouldDecreaseIndentAfterEmit = false; } - previousSibling = child; - } - var hasTrailingComma = (format & 32) && children.hasTrailingComma; - if (format & 16 && hasTrailingComma) { - write(","); - } - if (format & 64) { - decreaseIndent(); - } - if (shouldWriteClosingLineTerminator(parentNode, children, format)) { - writeLine(); } - else if (format & 128) { + else { + emitSignatureHead(node); write(" "); + emitExpression(body); } } - if (format & 7680) { - write(getClosingBracket(format)); - } - } - function writeIfAny(nodes, text) { - if (nodes && nodes.length > 0) { - write(text); - } - } - function writeIfPresent(node, text) { - if (node !== undefined) { - write(text); - } - } - function writeToken(token, pos, contextNode) { - var tokenStartPos = emitTokenStart(token, pos, contextNode, shouldSkipLeadingSourceMapForToken, getTokenSourceMapRange); - var tokenEndPos = writeTokenText(token, tokenStartPos); - return emitTokenEnd(token, tokenEndPos, contextNode, shouldSkipTrailingSourceMapForToken, getTokenSourceMapRange); - } - function shouldSkipLeadingSourceMapForToken(contextNode) { - return (contextNode.emitFlags & 4096) !== 0; - } - function shouldSkipTrailingSourceMapForToken(contextNode) { - return (contextNode.emitFlags & 8192) !== 0; - } - function writeTokenText(token, pos) { - var tokenString = ts.tokenToString(token); - write(tokenString); - return ts.positionIsSynthesized(pos) ? -1 : pos + tokenString.length; - } - function writeTokenNode(node) { - if (node) { - emitStart(node, node, shouldSkipLeadingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange); - writeTokenText(node.kind); - emitEnd(node, node, shouldSkipTrailingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange); - } - } - function increaseIndentIf(value, valueToWriteWhenNotIndenting) { - if (value) { - increaseIndent(); - writeLine(); - } - else if (valueToWriteWhenNotIndenting) { - write(valueToWriteWhenNotIndenting); + else { + emitSignatureHead(node); + write(";"); } } - function decreaseIndentIf(value1, value2) { - if (value1) { - decreaseIndent(); - } - if (value2) { - decreaseIndent(); - } + function emitSignatureHead(node) { + emitTypeParameters(node, node.typeParameters); + emitParameters(node, node.parameters); + emitWithPrefix(": ", node.type); } - function shouldWriteLeadingLineTerminator(parentNode, children, format) { - if (format & 1) { + function shouldEmitBlockFunctionBodyOnSingleLine(parentNode, body) { + if (ts.getEmitFlags(body) & 32) { return true; } - if (format & 2) { - if (format & 32768) { - return true; - } - var firstChild = children[0]; - if (firstChild === undefined) { - return !ts.rangeIsOnSingleLine(parentNode, currentSourceFile); - } - else if (ts.positionIsSynthesized(parentNode.pos) || ts.nodeIsSynthesized(firstChild)) { - return synthesizedNodeStartsOnNewLine(firstChild, format); - } - else { - return !ts.rangeStartPositionsAreOnSameLine(parentNode, firstChild, currentSourceFile); - } + if (body.multiLine) { + return false; } - else { + if (!ts.nodeIsSynthesized(body) && !ts.rangeIsOnSingleLine(body, currentSourceFile)) { return false; } - } - function shouldWriteSeparatingLineTerminator(previousNode, nextNode, format) { - if (format & 1) { - return true; + if (shouldWriteLeadingLineTerminator(body, body.statements, 2) + || shouldWriteClosingLineTerminator(body, body.statements, 2)) { + return false; } - else if (format & 2) { - if (previousNode === undefined || nextNode === undefined) { + var previousStatement; + for (var _a = 0, _b = body.statements; _a < _b.length; _a++) { + var statement = _b[_a]; + if (shouldWriteSeparatingLineTerminator(previousStatement, statement, 2)) { return false; } - else if (ts.nodeIsSynthesized(previousNode) || ts.nodeIsSynthesized(nextNode)) { - return synthesizedNodeStartsOnNewLine(previousNode, format) || synthesizedNodeStartsOnNewLine(nextNode, format); - } - else { - return !ts.rangeEndIsOnSameLineAsRangeStart(previousNode, nextNode, currentSourceFile); - } - } - else { - return nextNode.startsOnNewLine; + previousStatement = statement; } + return true; } - function shouldWriteClosingLineTerminator(parentNode, children, format) { - if (format & 1) { - return (format & 65536) === 0; - } - else if (format & 2) { - if (format & 32768) { - return true; - } - var lastChild = ts.lastOrUndefined(children); - if (lastChild === undefined) { - return !ts.rangeIsOnSingleLine(parentNode, currentSourceFile); - } - else if (ts.positionIsSynthesized(parentNode.pos) || ts.nodeIsSynthesized(lastChild)) { - return synthesizedNodeStartsOnNewLine(lastChild, format); - } - else { - return !ts.rangeEndPositionsAreOnSameLine(parentNode, lastChild, currentSourceFile); - } + function emitBlockFunctionBody(parentNode, body) { + write(" {"); + increaseIndent(); + emitBodyWithDetachedComments(body, body.statements, shouldEmitBlockFunctionBodyOnSingleLine(parentNode, body) + ? emitBlockFunctionBodyOnSingleLine + : emitBlockFunctionBodyWorker); + decreaseIndent(); + writeToken(16, body.statements.end, body); + } + function emitBlockFunctionBodyOnSingleLine(body) { + emitBlockFunctionBodyWorker(body, true); + } + function emitBlockFunctionBodyWorker(body, emitBlockFunctionBodyOnSingleLine) { + var statementOffset = emitPrologueDirectives(body.statements, true); + var helpersEmitted = emitHelpers(body); + if (statementOffset === 0 && !helpersEmitted && emitBlockFunctionBodyOnSingleLine) { + decreaseIndent(); + emitList(body, body.statements, 384); + increaseIndent(); } else { - return false; + emitList(body, body.statements, 1, statementOffset); } } - function synthesizedNodeStartsOnNewLine(node, format) { - if (ts.nodeIsSynthesized(node)) { - var startsOnNewLine = node.startsOnNewLine; - if (startsOnNewLine === undefined) { - return (format & 32768) !== 0; - } - return startsOnNewLine; - } - return (format & 32768) !== 0; + function emitClassDeclaration(node) { + emitClassDeclarationOrExpression(node); } - function needsIndentation(parent, node1, node2) { - parent = skipSynthesizedParentheses(parent); - node1 = skipSynthesizedParentheses(node1); - node2 = skipSynthesizedParentheses(node2); - if (node2.startsOnNewLine) { - return true; + function emitClassDeclarationOrExpression(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + write("class"); + emitNodeWithPrefix(" ", node.name, emitIdentifierName); + var indentedFlag = ts.getEmitFlags(node) & 524288; + if (indentedFlag) { + increaseIndent(); } - return !ts.nodeIsSynthesized(parent) - && !ts.nodeIsSynthesized(node1) - && !ts.nodeIsSynthesized(node2) - && !ts.rangeEndIsOnSameLineAsRangeStart(node1, node2, currentSourceFile); - } - function skipSynthesizedParentheses(node) { - while (node.kind === 178 && ts.nodeIsSynthesized(node)) { - node = node.expression; + emitTypeParameters(node, node.typeParameters); + emitList(node, node.heritageClauses, 256); + var savedTempFlags = tempFlags; + tempFlags = 0; + write(" {"); + emitList(node, node.members, 65); + write("}"); + if (indentedFlag) { + decreaseIndent(); } - return node; + tempFlags = savedTempFlags; } - function getTextOfNode(node, includeTrivia) { - if (ts.isGeneratedIdentifier(node)) { - return getGeneratedIdentifier(node); - } - else if (ts.isIdentifier(node) && (ts.nodeIsSynthesized(node) || !node.parent)) { - return ts.unescapeIdentifier(node.text); + function emitInterfaceDeclaration(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + write("interface "); + emit(node.name); + emitTypeParameters(node, node.typeParameters); + emitList(node, node.heritageClauses, 256); + write(" {"); + emitList(node, node.members, 65); + write("}"); + } + function emitTypeAliasDeclaration(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + write("type "); + emit(node.name); + emitTypeParameters(node, node.typeParameters); + write(" = "); + emit(node.type); + write(";"); + } + function emitEnumDeclaration(node) { + emitModifiers(node, node.modifiers); + write("enum "); + emit(node.name); + var savedTempFlags = tempFlags; + tempFlags = 0; + write(" {"); + emitList(node, node.members, 81); + write("}"); + tempFlags = savedTempFlags; + } + function emitModuleDeclaration(node) { + emitModifiers(node, node.modifiers); + write(node.flags & 16 ? "namespace " : "module "); + emit(node.name); + var body = node.body; + while (body.kind === 225) { + write("."); + emit(body.name); + body = body.body; } - else if (node.kind === 9 && node.textSourceNode) { - return getTextOfNode(node.textSourceNode, includeTrivia); + write(" "); + emit(body); + } + function emitModuleBlock(node) { + if (isEmptyBlock(node)) { + write("{ }"); } - else if (ts.isLiteralExpression(node) && (ts.nodeIsSynthesized(node) || !node.parent)) { - return node.text; + else { + var savedTempFlags = tempFlags; + tempFlags = 0; + write("{"); + increaseIndent(); + emitBlockStatements(node); + write("}"); + tempFlags = savedTempFlags; } - return ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, node, includeTrivia); } - function getLiteralTextOfNode(node) { - if (node.kind === 9 && node.textSourceNode) { - var textSourceNode = node.textSourceNode; - if (ts.isIdentifier(textSourceNode)) { - return "\"" + ts.escapeNonAsciiCharacters(ts.escapeString(getTextOfNode(textSourceNode))) + "\""; - } - else { - return getLiteralTextOfNode(textSourceNode); - } + function emitCaseBlock(node) { + writeToken(15, node.pos); + emitList(node, node.clauses, 65); + writeToken(16, node.clauses.end); + } + function emitImportEqualsDeclaration(node) { + emitModifiers(node, node.modifiers); + write("import "); + emit(node.name); + write(" = "); + emitModuleReference(node.moduleReference); + write(";"); + } + function emitModuleReference(node) { + if (node.kind === 69) { + emitExpression(node); + } + else { + emit(node); } - return ts.getLiteralText(node, currentSourceFile, languageVersion); } - function tryGetConstEnumValue(node) { - if (compilerOptions.isolatedModules) { - return undefined; + function emitImportDeclaration(node) { + emitModifiers(node, node.modifiers); + write("import "); + if (node.importClause) { + emit(node.importClause); + write(" from "); } - return ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node) - ? resolver.getConstantValue(node) - : undefined; + emitExpression(node.moduleSpecifier); + write(";"); } - function isSingleLineEmptyBlock(block) { - return !block.multiLine - && block.statements.length === 0 - && ts.rangeEndIsOnSameLineAsRangeStart(block, block, currentSourceFile); + function emitImportClause(node) { + emit(node.name); + if (node.name && node.namedBindings) { + write(", "); + } + emit(node.namedBindings); } - function isUniqueName(name) { - return !resolver.hasGlobalName(name) && - !ts.hasProperty(currentFileIdentifiers, name) && - !ts.hasProperty(generatedNameSet, name); + function emitNamespaceImport(node) { + write("* as "); + emit(node.name); } - function isUniqueLocalName(name, container) { - for (var node = container; ts.isNodeDescendantOf(node, container); node = node.nextContainer) { - if (node.locals && ts.hasProperty(node.locals, name)) { - if (node.locals[name].flags & (107455 | 1048576 | 8388608)) { - return false; - } - } - } - return true; + function emitNamedImports(node) { + emitNamedImportsOrExports(node); } - function makeTempVariableName(flags) { - if (flags && !(tempFlags & flags)) { - var name_43 = flags === 268435456 ? "_i" : "_n"; - if (isUniqueName(name_43)) { - tempFlags |= flags; - return name_43; - } + function emitImportSpecifier(node) { + emitImportOrExportSpecifier(node); + } + function emitExportAssignment(node) { + write(node.isExportEquals ? "export = " : "export default "); + emitExpression(node.expression); + write(";"); + } + function emitExportDeclaration(node) { + write("export "); + if (node.exportClause) { + emit(node.exportClause); } - while (true) { - var count = tempFlags & 268435455; - tempFlags++; - if (count !== 8 && count !== 13) { - var name_44 = count < 26 - ? "_" + String.fromCharCode(97 + count) - : "_" + (count - 26); - if (isUniqueName(name_44)) { - return name_44; - } - } + else { + write("*"); } - } - function makeUniqueName(baseName) { - if (baseName.charCodeAt(baseName.length - 1) !== 95) { - baseName += "_"; + if (node.moduleSpecifier) { + write(" from "); + emitExpression(node.moduleSpecifier); } - var i = 1; - while (true) { - var generatedName = baseName + i; - if (isUniqueName(generatedName)) { - return generatedNameSet[generatedName] = generatedName; - } - i++; + write(";"); + } + function emitNamedExports(node) { + emitNamedImportsOrExports(node); + } + function emitExportSpecifier(node) { + emitImportOrExportSpecifier(node); + } + function emitNamedImportsOrExports(node) { + write("{"); + emitList(node, node.elements, 432); + write("}"); + } + function emitImportOrExportSpecifier(node) { + if (node.propertyName) { + emit(node.propertyName); + write(" as "); } + emit(node.name); } - function generateNameForModuleOrEnum(node) { - var name = getTextOfNode(node.name); - return isUniqueLocalName(name, node) ? name : makeUniqueName(name); + function emitExternalModuleReference(node) { + write("require("); + emitExpression(node.expression); + write(")"); } - function generateNameForImportOrExportDeclaration(node) { - var expr = ts.getExternalModuleName(node); - var baseName = expr.kind === 9 ? - ts.escapeIdentifier(ts.makeIdentifierFromModuleName(expr.text)) : "module"; - return makeUniqueName(baseName); + function emitJsxElement(node) { + emit(node.openingElement); + emitList(node, node.children, 131072); + emit(node.closingElement); } - function generateNameForExportDefault() { - return makeUniqueName("default"); + function emitJsxSelfClosingElement(node) { + write("<"); + emitJsxTagName(node.tagName); + write(" "); + emitList(node, node.attributes, 131328); + write("/>"); } - function generateNameForClassExpression() { - return makeUniqueName("class"); + function emitJsxOpeningElement(node) { + write("<"); + emitJsxTagName(node.tagName); + writeIfAny(node.attributes, " "); + emitList(node, node.attributes, 131328); + write(">"); } - function generateNameForNode(node) { - switch (node.kind) { - case 69: - return makeUniqueName(getTextOfNode(node)); - case 225: - case 224: - return generateNameForModuleOrEnum(node); - case 230: - case 236: - return generateNameForImportOrExportDeclaration(node); - case 220: - case 221: - case 235: - return generateNameForExportDefault(); - case 192: - return generateNameForClassExpression(); - default: - return makeTempVariableName(0); - } + function emitJsxText(node) { + writer.writeLiteral(getTextOfNode(node, true)); } - function generateName(name) { - switch (name.autoGenerateKind) { - case 1: - return makeTempVariableName(0); - case 2: - return makeTempVariableName(268435456); - case 3: - return makeUniqueName(name.text); - } - ts.Debug.fail("Unsupported GeneratedIdentifierKind."); + function emitJsxClosingElement(node) { + write(""); } - function getNodeForGeneratedName(name) { - var autoGenerateId = name.autoGenerateId; - var node = name; - var original = node.original; - while (original) { - node = original; - if (ts.isIdentifier(node) - && node.autoGenerateKind === 4 - && node.autoGenerateId !== autoGenerateId) { - break; - } - original = node.original; + function emitJsxAttribute(node) { + emit(node.name); + emitWithPrefix("=", node.initializer); + } + function emitJsxSpreadAttribute(node) { + write("{..."); + emitExpression(node.expression); + write("}"); + } + function emitJsxExpression(node) { + if (node.expression) { + write("{"); + emitExpression(node.expression); + write("}"); } - return node; } - function getGeneratedIdentifier(name) { - if (name.autoGenerateKind === 4) { - var node = getNodeForGeneratedName(name); - var nodeId = ts.getNodeId(node); - return nodeIdToGeneratedName[nodeId] || (nodeIdToGeneratedName[nodeId] = ts.unescapeIdentifier(generateNameForNode(node))); + function emitJsxTagName(node) { + if (node.kind === 69) { + emitExpression(node); } else { - var autoGenerateId = name.autoGenerateId; - return autoGeneratedIdToGeneratedName[autoGenerateId] || (autoGeneratedIdToGeneratedName[autoGenerateId] = ts.unescapeIdentifier(generateName(name))); + emit(node); } } - function createDelimiterMap() { - var delimiters = []; - delimiters[0] = ""; - delimiters[16] = ","; - delimiters[4] = " |"; - delimiters[8] = " &"; - return delimiters; + function emitCaseClause(node) { + write("case "); + emitExpression(node.expression); + write(":"); + emitCaseOrDefaultClauseStatements(node, node.statements); } - function getDelimiter(format) { - return delimiters[format & 28]; + function emitDefaultClause(node) { + write("default:"); + emitCaseOrDefaultClauseStatements(node, node.statements); } - function createBracketsMap() { - var brackets = []; - brackets[512] = ["{", "}"]; - brackets[1024] = ["(", ")"]; - brackets[2048] = ["<", ">"]; - brackets[4096] = ["[", "]"]; - return brackets; + function emitCaseOrDefaultClauseStatements(parentNode, statements) { + var emitAsSingleStatement = statements.length === 1 && + (ts.nodeIsSynthesized(parentNode) || + ts.nodeIsSynthesized(statements[0]) || + ts.rangeStartPositionsAreOnSameLine(parentNode, statements[0], currentSourceFile)); + if (emitAsSingleStatement) { + write(" "); + emit(statements[0]); + } + else { + emitList(parentNode, statements, 81985); + } } - function getOpeningBracket(format) { - return brackets[format & 7680][0]; + function emitHeritageClause(node) { + write(" "); + writeTokenText(node.token); + write(" "); + emitList(node, node.types, 272); } - function getClosingBracket(format) { - return brackets[format & 7680][1]; + function emitCatchClause(node) { + writeLine(); + var openParenPos = writeToken(72, node.pos); + write(" "); + writeToken(17, openParenPos); + emit(node.variableDeclaration); + writeToken(18, node.variableDeclaration ? node.variableDeclaration.end : openParenPos); + write(" "); + emit(node.block); } - } - ts.emitFiles = emitFiles; -})(ts || (ts = {})); -var ts; -(function (ts) { - ts.version = "2.1.0"; - var emptyArray = []; - function findConfigFile(searchPath, fileExists, configName) { - if (configName === void 0) { configName = "tsconfig.json"; } - while (true) { - var fileName = ts.combinePaths(searchPath, configName); - if (fileExists(fileName)) { - return fileName; - } - var parentPath = ts.getDirectoryPath(searchPath); - if (parentPath === searchPath) { - break; + function emitPropertyAssignment(node) { + emit(node.name); + write(": "); + var initializer = node.initializer; + if ((ts.getEmitFlags(initializer) & 16384) === 0) { + var commentRange = ts.getCommentRange(initializer); + emitTrailingCommentsOfPosition(commentRange.pos); } - searchPath = parentPath; + emitExpression(initializer); } - return undefined; - } - ts.findConfigFile = findConfigFile; - function resolveTripleslashReference(moduleName, containingFile) { - var basePath = ts.getDirectoryPath(containingFile); - var referencedFileName = ts.isRootedDiskPath(moduleName) ? moduleName : ts.combinePaths(basePath, moduleName); - return ts.normalizePath(referencedFileName); - } - ts.resolveTripleslashReference = resolveTripleslashReference; - function computeCommonSourceDirectoryOfFilenames(fileNames, currentDirectory, getCanonicalFileName) { - var commonPathComponents; - var failed = ts.forEach(fileNames, function (sourceFile) { - var sourcePathComponents = ts.getNormalizedPathComponents(sourceFile, currentDirectory); - sourcePathComponents.pop(); - if (!commonPathComponents) { - commonPathComponents = sourcePathComponents; - return; + function emitShorthandPropertyAssignment(node) { + emit(node.name); + if (node.objectAssignmentInitializer) { + write(" = "); + emitExpression(node.objectAssignmentInitializer); } - for (var i = 0, n = Math.min(commonPathComponents.length, sourcePathComponents.length); i < n; i++) { - if (getCanonicalFileName(commonPathComponents[i]) !== getCanonicalFileName(sourcePathComponents[i])) { - if (i === 0) { - return true; + } + function emitEnumMember(node) { + emit(node.name); + emitExpressionWithPrefix(" = ", node.initializer); + } + function emitSourceFile(node) { + writeLine(); + emitShebang(); + emitBodyWithDetachedComments(node, node.statements, emitSourceFileWorker); + } + function emitSourceFileWorker(node) { + var statements = node.statements; + var statementOffset = emitPrologueDirectives(statements); + var savedTempFlags = tempFlags; + tempFlags = 0; + emitHelpers(node); + emitList(node, statements, 1, statementOffset); + tempFlags = savedTempFlags; + } + function emitPartiallyEmittedExpression(node) { + emitExpression(node.expression); + } + function emitPrologueDirectives(statements, startWithNewLine) { + for (var i = 0; i < statements.length; i++) { + if (ts.isPrologueDirective(statements[i])) { + if (startWithNewLine || i > 0) { + writeLine(); } - commonPathComponents.length = i; - break; + emit(statements[i]); + } + else { + return i; } } - if (sourcePathComponents.length < commonPathComponents.length) { - commonPathComponents.length = sourcePathComponents.length; - } - }); - if (failed) { - return ""; + return statements.length; } - if (!commonPathComponents) { - return currentDirectory; + function emitHelpers(node) { + var emitFlags = ts.getEmitFlags(node); + var helpersEmitted = false; + if (emitFlags & 1) { + helpersEmitted = emitEmitHelpers(currentSourceFile); + } + if (emitFlags & 2) { + writeLines(exportStarHelper); + helpersEmitted = true; + } + if (emitFlags & 4) { + writeLines(superHelper); + helpersEmitted = true; + } + if (emitFlags & 8) { + writeLines(advancedSuperHelper); + helpersEmitted = true; + } + return helpersEmitted; } - return ts.getNormalizedPathFromPathComponents(commonPathComponents); - } - ts.computeCommonSourceDirectoryOfFilenames = computeCommonSourceDirectoryOfFilenames; - function trace(host, message) { - host.trace(ts.formatMessage.apply(undefined, arguments)); - } - function isTraceEnabled(compilerOptions, host) { - return compilerOptions.traceResolution && host.trace !== undefined; - } - function hasZeroOrOneAsteriskCharacter(str) { - var seenAsterisk = false; - for (var i = 0; i < str.length; i++) { - if (str.charCodeAt(i) === 42) { - if (!seenAsterisk) { - seenAsterisk = true; + function emitEmitHelpers(node) { + if (compilerOptions.noEmitHelpers) { + return false; + } + if (compilerOptions.importHelpers + && (ts.isExternalModule(node) || compilerOptions.isolatedModules)) { + return false; + } + var helpersEmitted = false; + if ((languageVersion < 2) && (!extendsEmitted && node.flags & 1024)) { + writeLines(extendsHelper); + extendsEmitted = true; + helpersEmitted = true; + } + if (compilerOptions.jsx !== 1 && !assignEmitted && (node.flags & 16384)) { + writeLines(assignHelper); + assignEmitted = true; + } + if (!decorateEmitted && node.flags & 2048) { + writeLines(decorateHelper); + if (compilerOptions.emitDecoratorMetadata) { + writeLines(metadataHelper); } - else { - return false; + decorateEmitted = true; + helpersEmitted = true; + } + if (!paramEmitted && node.flags & 4096) { + writeLines(paramHelper); + paramEmitted = true; + helpersEmitted = true; + } + if (!awaiterEmitted && node.flags & 8192) { + writeLines(awaiterHelper); + if (languageVersion < 2) { + writeLines(generatorHelper); } + awaiterEmitted = true; + helpersEmitted = true; + } + if (helpersEmitted) { + writeLine(); } + return helpersEmitted; } - return true; - } - ts.hasZeroOrOneAsteriskCharacter = hasZeroOrOneAsteriskCharacter; - function createResolvedModule(resolvedFileName, isExternalLibraryImport, failedLookupLocations) { - return { resolvedModule: resolvedFileName ? { resolvedFileName: resolvedFileName, isExternalLibraryImport: isExternalLibraryImport } : undefined, failedLookupLocations: failedLookupLocations }; - } - function moduleHasNonRelativeName(moduleName) { - return !(ts.isRootedDiskPath(moduleName) || ts.isExternalModuleNameRelative(moduleName)); - } - function tryReadTypesSection(packageJsonPath, baseDirectory, state) { - var jsonContent = readJson(packageJsonPath, state.host); - function tryReadFromField(fieldName) { - if (ts.hasProperty(jsonContent, fieldName)) { - var typesFile = jsonContent[fieldName]; - if (typeof typesFile === "string") { - var typesFilePath_1 = ts.normalizePath(ts.combinePaths(baseDirectory, typesFile)); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.package_json_has_0_field_1_that_references_2, fieldName, typesFile, typesFilePath_1); - } - return typesFilePath_1; - } - else { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Expected_type_of_0_field_in_package_json_to_be_string_got_1, fieldName, typeof typesFile); + function writeLines(text) { + var lines = text.split(/\r\n|\r|\n/g); + for (var i = 0; i < lines.length; i++) { + var line = lines[i]; + if (line.length) { + if (i > 0) { + writeLine(); } + write(line); } } } - var typesFilePath = tryReadFromField("typings") || tryReadFromField("types"); - if (typesFilePath) { - return typesFilePath; + function emitShebang() { + var shebang = ts.getShebang(currentText); + if (shebang) { + write(shebang); + writeLine(); + } } - if (state.compilerOptions.allowJs && jsonContent.main && typeof jsonContent.main === "string") { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.No_types_specified_in_package_json_but_allowJs_is_set_so_returning_main_value_of_0, jsonContent.main); + function emitModifiers(node, modifiers) { + if (modifiers && modifiers.length) { + emitList(node, modifiers, 256); + write(" "); } - var mainFilePath = ts.normalizePath(ts.combinePaths(baseDirectory, jsonContent.main)); - return mainFilePath; } - return undefined; - } - function readJson(path, host) { - try { - var jsonText = host.readFile(path); - return jsonText ? JSON.parse(jsonText) : {}; + function emitWithPrefix(prefix, node) { + emitNodeWithPrefix(prefix, node, emit); } - catch (e) { - return {}; + function emitExpressionWithPrefix(prefix, node) { + emitNodeWithPrefix(prefix, node, emitExpression); + } + function emitNodeWithPrefix(prefix, node, emit) { + if (node) { + write(prefix); + emit(node); + } + } + function emitWithSuffix(node, suffix) { + if (node) { + emit(node); + write(suffix); + } + } + function emitEmbeddedStatement(node) { + if (ts.isBlock(node)) { + write(" "); + emit(node); + } + else { + writeLine(); + increaseIndent(); + emit(node); + decreaseIndent(); + } } - } - var typeReferenceExtensions = [".d.ts"]; - function getEffectiveTypeRoots(options, host) { - if (options.typeRoots) { - return options.typeRoots; + function emitDecorators(parentNode, decorators) { + emitList(parentNode, decorators, 24577); } - var currentDirectory; - if (options.configFilePath) { - currentDirectory = ts.getDirectoryPath(options.configFilePath); + function emitTypeArguments(parentNode, typeArguments) { + emitList(parentNode, typeArguments, 26960); } - else if (host.getCurrentDirectory) { - currentDirectory = host.getCurrentDirectory(); + function emitTypeParameters(parentNode, typeParameters) { + emitList(parentNode, typeParameters, 26960); } - return currentDirectory && getDefaultTypeRoots(currentDirectory, host); - } - ts.getEffectiveTypeRoots = getEffectiveTypeRoots; - function getDefaultTypeRoots(currentDirectory, host) { - if (!host.directoryExists) { - return [ts.combinePaths(currentDirectory, nodeModulesAtTypes)]; + function emitParameters(parentNode, parameters) { + emitList(parentNode, parameters, 1360); } - var typeRoots; - while (true) { - var atTypes = ts.combinePaths(currentDirectory, nodeModulesAtTypes); - if (host.directoryExists(atTypes)) { - (typeRoots || (typeRoots = [])).push(atTypes); + function emitParametersForArrow(parentNode, parameters) { + if (parameters && + parameters.length === 1 && + parameters[0].type === undefined && + parameters[0].pos === parentNode.pos) { + emit(parameters[0]); } - var parent_15 = ts.getDirectoryPath(currentDirectory); - if (parent_15 === currentDirectory) { - break; + else { + emitParameters(parentNode, parameters); } - currentDirectory = parent_15; } - return typeRoots; - } - var nodeModulesAtTypes = ts.combinePaths("node_modules", "@types"); - function resolveTypeReferenceDirective(typeReferenceDirectiveName, containingFile, options, host) { - var traceEnabled = isTraceEnabled(options, host); - var moduleResolutionState = { - compilerOptions: options, - host: host, - skipTsx: true, - traceEnabled: traceEnabled - }; - var typeRoots = getEffectiveTypeRoots(options, host); - if (traceEnabled) { - if (containingFile === undefined) { - if (typeRoots === undefined) { - trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_not_set_root_directory_not_set, typeReferenceDirectiveName); + function emitParametersForIndexSignature(parentNode, parameters) { + emitList(parentNode, parameters, 4432); + } + function emitList(parentNode, children, format, start, count) { + emitNodeList(emit, parentNode, children, format, start, count); + } + function emitExpressionList(parentNode, children, format, start, count) { + emitNodeList(emitExpression, parentNode, children, format, start, count); + } + function emitNodeList(emit, parentNode, children, format, start, count) { + if (start === void 0) { start = 0; } + if (count === void 0) { count = children ? children.length - start : 0; } + var isUndefined = children === undefined; + if (isUndefined && format & 8192) { + return; + } + var isEmpty = isUndefined || children.length === 0 || start >= children.length || count === 0; + if (isEmpty && format & 16384) { + return; + } + if (format & 7680) { + write(getOpeningBracket(format)); + } + if (isEmpty) { + if (format & 1) { + writeLine(); } - else { - trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_not_set_root_directory_1, typeReferenceDirectiveName, typeRoots); + else if (format & 128) { + write(" "); } } else { - if (typeRoots === undefined) { - trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_1_root_directory_not_set, typeReferenceDirectiveName, containingFile); + var mayEmitInterveningComments = (format & 131072) === 0; + var shouldEmitInterveningComments = mayEmitInterveningComments; + if (shouldWriteLeadingLineTerminator(parentNode, children, format)) { + writeLine(); + shouldEmitInterveningComments = false; } - else { - trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_1_root_directory_2, typeReferenceDirectiveName, containingFile, typeRoots); + else if (format & 128) { + write(" "); } - } - } - var failedLookupLocations = []; - if (typeRoots && typeRoots.length) { - if (traceEnabled) { - trace(host, ts.Diagnostics.Resolving_with_primary_search_path_0, typeRoots.join(", ")); - } - var primarySearchPaths = typeRoots; - for (var _i = 0, primarySearchPaths_1 = primarySearchPaths; _i < primarySearchPaths_1.length; _i++) { - var typeRoot = primarySearchPaths_1[_i]; - var candidate = ts.combinePaths(typeRoot, typeReferenceDirectiveName); - var candidateDirectory = ts.getDirectoryPath(candidate); - var resolvedFile_1 = loadNodeModuleFromDirectory(typeReferenceExtensions, candidate, failedLookupLocations, !directoryProbablyExists(candidateDirectory, host), moduleResolutionState); - if (resolvedFile_1) { - if (traceEnabled) { - trace(host, ts.Diagnostics.Type_reference_directive_0_was_successfully_resolved_to_1_primary_Colon_2, typeReferenceDirectiveName, resolvedFile_1, true); + if (format & 64) { + increaseIndent(); + } + var previousSibling = void 0; + var shouldDecreaseIndentAfterEmit = void 0; + var delimiter = getDelimiter(format); + for (var i = 0; i < count; i++) { + var child = children[start + i]; + if (previousSibling) { + write(delimiter); + if (shouldWriteSeparatingLineTerminator(previousSibling, child, format)) { + if ((format & (3 | 64)) === 0) { + increaseIndent(); + shouldDecreaseIndentAfterEmit = true; + } + writeLine(); + shouldEmitInterveningComments = false; + } + else if (previousSibling && format & 256) { + write(" "); + } } - return { - resolvedTypeReferenceDirective: { primary: true, resolvedFileName: resolvedFile_1 }, - failedLookupLocations: failedLookupLocations - }; + if (shouldEmitInterveningComments) { + var commentRange = ts.getCommentRange(child); + emitTrailingCommentsOfPosition(commentRange.pos); + } + else { + shouldEmitInterveningComments = mayEmitInterveningComments; + } + emit(child); + if (shouldDecreaseIndentAfterEmit) { + decreaseIndent(); + shouldDecreaseIndentAfterEmit = false; + } + previousSibling = child; } - } - } - else { - if (traceEnabled) { - trace(host, ts.Diagnostics.Root_directory_cannot_be_determined_skipping_primary_search_paths); - } - } - var resolvedFile; - var initialLocationForSecondaryLookup; - if (containingFile) { - initialLocationForSecondaryLookup = ts.getDirectoryPath(containingFile); - } - if (initialLocationForSecondaryLookup !== undefined) { - if (traceEnabled) { - trace(host, ts.Diagnostics.Looking_up_in_node_modules_folder_initial_location_0, initialLocationForSecondaryLookup); - } - resolvedFile = loadModuleFromNodeModules(typeReferenceDirectiveName, initialLocationForSecondaryLookup, failedLookupLocations, moduleResolutionState); - if (traceEnabled) { - if (resolvedFile) { - trace(host, ts.Diagnostics.Type_reference_directive_0_was_successfully_resolved_to_1_primary_Colon_2, typeReferenceDirectiveName, resolvedFile, false); + var hasTrailingComma = (format & 32) && children.hasTrailingComma; + if (format & 16 && hasTrailingComma) { + write(","); } - else { - trace(host, ts.Diagnostics.Type_reference_directive_0_was_not_resolved, typeReferenceDirectiveName); + if (format & 64) { + decreaseIndent(); + } + if (shouldWriteClosingLineTerminator(parentNode, children, format)) { + writeLine(); + } + else if (format & 128) { + write(" "); } } - } - else { - if (traceEnabled) { - trace(host, ts.Diagnostics.Containing_file_is_not_specified_and_root_directory_cannot_be_determined_skipping_lookup_in_node_modules_folder); + if (format & 7680) { + write(getClosingBracket(format)); } } - return { - resolvedTypeReferenceDirective: resolvedFile - ? { primary: false, resolvedFileName: resolvedFile } - : undefined, - failedLookupLocations: failedLookupLocations - }; - } - ts.resolveTypeReferenceDirective = resolveTypeReferenceDirective; - function resolveModuleName(moduleName, containingFile, compilerOptions, host) { - var traceEnabled = isTraceEnabled(compilerOptions, host); - if (traceEnabled) { - trace(host, ts.Diagnostics.Resolving_module_0_from_1, moduleName, containingFile); - } - var moduleResolution = compilerOptions.moduleResolution; - if (moduleResolution === undefined) { - moduleResolution = ts.getEmitModuleKind(compilerOptions) === ts.ModuleKind.CommonJS ? ts.ModuleResolutionKind.NodeJs : ts.ModuleResolutionKind.Classic; - if (traceEnabled) { - trace(host, ts.Diagnostics.Module_resolution_kind_is_not_specified_using_0, ts.ModuleResolutionKind[moduleResolution]); + function writeIfAny(nodes, text) { + if (nodes && nodes.length > 0) { + write(text); } } - else { - if (traceEnabled) { - trace(host, ts.Diagnostics.Explicitly_specified_module_resolution_kind_Colon_0, ts.ModuleResolutionKind[moduleResolution]); + function writeIfPresent(node, text) { + if (node !== undefined) { + write(text); } } - var result; - switch (moduleResolution) { - case ts.ModuleResolutionKind.NodeJs: - result = nodeModuleNameResolver(moduleName, containingFile, compilerOptions, host); - break; - case ts.ModuleResolutionKind.Classic: - result = classicNameResolver(moduleName, containingFile, compilerOptions, host); - break; + function writeToken(token, pos, contextNode) { + return emitTokenWithSourceMap(contextNode, token, pos, writeTokenText); } - if (traceEnabled) { - if (result.resolvedModule) { - trace(host, ts.Diagnostics.Module_name_0_was_successfully_resolved_to_1, moduleName, result.resolvedModule.resolvedFileName); + function writeTokenText(token, pos) { + var tokenString = ts.tokenToString(token); + write(tokenString); + return pos < 0 ? pos : pos + tokenString.length; + } + function increaseIndentIf(value, valueToWriteWhenNotIndenting) { + if (value) { + increaseIndent(); + writeLine(); } - else { - trace(host, ts.Diagnostics.Module_name_0_was_not_resolved, moduleName); + else if (valueToWriteWhenNotIndenting) { + write(valueToWriteWhenNotIndenting); } } - return result; - } - ts.resolveModuleName = resolveModuleName; - function tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state) { - if (moduleHasNonRelativeName(moduleName)) { - return tryLoadModuleUsingBaseUrl(moduleName, loader, failedLookupLocations, supportedExtensions, state); - } - else { - return tryLoadModuleUsingRootDirs(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state); - } - } - function tryLoadModuleUsingRootDirs(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state) { - if (!state.compilerOptions.rootDirs) { - return undefined; - } - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.rootDirs_option_is_set_using_it_to_resolve_relative_module_name_0, moduleName); + function decreaseIndentIf(value1, value2) { + if (value1) { + decreaseIndent(); + } + if (value2) { + decreaseIndent(); + } } - var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); - var matchedRootDir; - var matchedNormalizedPrefix; - for (var _i = 0, _a = state.compilerOptions.rootDirs; _i < _a.length; _i++) { - var rootDir = _a[_i]; - var normalizedRoot = ts.normalizePath(rootDir); - if (!ts.endsWith(normalizedRoot, ts.directorySeparator)) { - normalizedRoot += ts.directorySeparator; + function shouldWriteLeadingLineTerminator(parentNode, children, format) { + if (format & 1) { + return true; } - var isLongestMatchingPrefix = ts.startsWith(candidate, normalizedRoot) && - (matchedNormalizedPrefix === undefined || matchedNormalizedPrefix.length < normalizedRoot.length); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Checking_if_0_is_the_longest_matching_prefix_for_1_2, normalizedRoot, candidate, isLongestMatchingPrefix); + if (format & 2) { + if (format & 32768) { + return true; + } + var firstChild = children[0]; + if (firstChild === undefined) { + return !ts.rangeIsOnSingleLine(parentNode, currentSourceFile); + } + else if (ts.positionIsSynthesized(parentNode.pos) || ts.nodeIsSynthesized(firstChild)) { + return synthesizedNodeStartsOnNewLine(firstChild, format); + } + else { + return !ts.rangeStartPositionsAreOnSameLine(parentNode, firstChild, currentSourceFile); + } } - if (isLongestMatchingPrefix) { - matchedNormalizedPrefix = normalizedRoot; - matchedRootDir = rootDir; + else { + return false; } } - if (matchedNormalizedPrefix) { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Longest_matching_prefix_for_0_is_1, candidate, matchedNormalizedPrefix); + function shouldWriteSeparatingLineTerminator(previousNode, nextNode, format) { + if (format & 1) { + return true; } - var suffix = candidate.substr(matchedNormalizedPrefix.length); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Loading_0_from_the_root_dir_1_candidate_location_2, suffix, matchedNormalizedPrefix, candidate); + else if (format & 2) { + if (previousNode === undefined || nextNode === undefined) { + return false; + } + else if (ts.nodeIsSynthesized(previousNode) || ts.nodeIsSynthesized(nextNode)) { + return synthesizedNodeStartsOnNewLine(previousNode, format) || synthesizedNodeStartsOnNewLine(nextNode, format); + } + else { + return !ts.rangeEndIsOnSameLineAsRangeStart(previousNode, nextNode, currentSourceFile); + } } - var resolvedFileName = loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(containingDirectory, state.host), state); - if (resolvedFileName) { - return resolvedFileName; + else { + return nextNode.startsOnNewLine; } - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Trying_other_entries_in_rootDirs); + } + function shouldWriteClosingLineTerminator(parentNode, children, format) { + if (format & 1) { + return (format & 65536) === 0; } - for (var _b = 0, _c = state.compilerOptions.rootDirs; _b < _c.length; _b++) { - var rootDir = _c[_b]; - if (rootDir === matchedRootDir) { - continue; + else if (format & 2) { + if (format & 32768) { + return true; } - var candidate_1 = ts.combinePaths(ts.normalizePath(rootDir), suffix); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Loading_0_from_the_root_dir_1_candidate_location_2, suffix, rootDir, candidate_1); + var lastChild = ts.lastOrUndefined(children); + if (lastChild === undefined) { + return !ts.rangeIsOnSingleLine(parentNode, currentSourceFile); } - var baseDirectory = ts.getDirectoryPath(candidate_1); - var resolvedFileName_1 = loader(candidate_1, supportedExtensions, failedLookupLocations, !directoryProbablyExists(baseDirectory, state.host), state); - if (resolvedFileName_1) { - return resolvedFileName_1; + else if (ts.positionIsSynthesized(parentNode.pos) || ts.nodeIsSynthesized(lastChild)) { + return synthesizedNodeStartsOnNewLine(lastChild, format); + } + else { + return !ts.rangeEndPositionsAreOnSameLine(parentNode, lastChild, currentSourceFile); } } - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Module_resolution_using_rootDirs_has_failed); + else { + return false; } } - return undefined; - } - function tryLoadModuleUsingBaseUrl(moduleName, loader, failedLookupLocations, supportedExtensions, state) { - if (!state.compilerOptions.baseUrl) { - return undefined; + function synthesizedNodeStartsOnNewLine(node, format) { + if (ts.nodeIsSynthesized(node)) { + var startsOnNewLine = node.startsOnNewLine; + if (startsOnNewLine === undefined) { + return (format & 32768) !== 0; + } + return startsOnNewLine; + } + return (format & 32768) !== 0; } - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.baseUrl_option_is_set_to_0_using_this_value_to_resolve_non_relative_module_name_1, state.compilerOptions.baseUrl, moduleName); + function needsIndentation(parent, node1, node2) { + parent = skipSynthesizedParentheses(parent); + node1 = skipSynthesizedParentheses(node1); + node2 = skipSynthesizedParentheses(node2); + if (node2.startsOnNewLine) { + return true; + } + return !ts.nodeIsSynthesized(parent) + && !ts.nodeIsSynthesized(node1) + && !ts.nodeIsSynthesized(node2) + && !ts.rangeEndIsOnSameLineAsRangeStart(node1, node2, currentSourceFile); } - var matchedPattern = undefined; - if (state.compilerOptions.paths) { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.paths_option_is_specified_looking_for_a_pattern_to_match_module_name_0, moduleName); + function skipSynthesizedParentheses(node) { + while (node.kind === 178 && ts.nodeIsSynthesized(node)) { + node = node.expression; } - matchedPattern = matchPatternOrExact(ts.getOwnKeys(state.compilerOptions.paths), moduleName); + return node; } - if (matchedPattern) { - var matchedStar = typeof matchedPattern === "string" ? undefined : matchedText(matchedPattern, moduleName); - var matchedPatternText = typeof matchedPattern === "string" ? matchedPattern : patternText(matchedPattern); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Module_name_0_matched_pattern_1, moduleName, matchedPatternText); + function getTextOfNode(node, includeTrivia) { + if (ts.isGeneratedIdentifier(node)) { + return getGeneratedIdentifier(node); } - for (var _i = 0, _a = state.compilerOptions.paths[matchedPatternText]; _i < _a.length; _i++) { - var subst = _a[_i]; - var path = matchedStar ? subst.replace("*", matchedStar) : subst; - var candidate = ts.normalizePath(ts.combinePaths(state.compilerOptions.baseUrl, path)); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Trying_substitution_0_candidate_module_location_Colon_1, subst, path); + else if (ts.isIdentifier(node) && (ts.nodeIsSynthesized(node) || !node.parent)) { + return ts.unescapeIdentifier(node.text); + } + else if (node.kind === 9 && node.textSourceNode) { + return getTextOfNode(node.textSourceNode, includeTrivia); + } + else if (ts.isLiteralExpression(node) && (ts.nodeIsSynthesized(node) || !node.parent)) { + return node.text; + } + return ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, node, includeTrivia); + } + function getLiteralTextOfNode(node) { + if (node.kind === 9 && node.textSourceNode) { + var textSourceNode = node.textSourceNode; + if (ts.isIdentifier(textSourceNode)) { + return "\"" + ts.escapeNonAsciiCharacters(ts.escapeString(getTextOfNode(textSourceNode))) + "\""; } - var resolvedFileName = loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(ts.getDirectoryPath(candidate), state.host), state); - if (resolvedFileName) { - return resolvedFileName; + else { + return getLiteralTextOfNode(textSourceNode); } } - return undefined; + return ts.getLiteralText(node, currentSourceFile, languageVersion); } - else { - var candidate = ts.normalizePath(ts.combinePaths(state.compilerOptions.baseUrl, moduleName)); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Resolving_module_name_0_relative_to_base_url_1_2, moduleName, state.compilerOptions.baseUrl, candidate); - } - return loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(ts.getDirectoryPath(candidate), state.host), state); + function isSingleLineEmptyBlock(block) { + return !block.multiLine + && isEmptyBlock(block); } - } - function matchPatternOrExact(patternStrings, candidate) { - var patterns = []; - for (var _i = 0, patternStrings_1 = patternStrings; _i < patternStrings_1.length; _i++) { - var patternString = patternStrings_1[_i]; - var pattern = tryParsePattern(patternString); - if (pattern) { - patterns.push(pattern); - } - else if (patternString === candidate) { - return patternString; - } + function isEmptyBlock(block) { + return block.statements.length === 0 + && ts.rangeEndIsOnSameLineAsRangeStart(block, block, currentSourceFile); } - return findBestPatternMatch(patterns, function (_) { return _; }, candidate); - } - function patternText(_a) { - var prefix = _a.prefix, suffix = _a.suffix; - return prefix + "*" + suffix; - } - function matchedText(pattern, candidate) { - ts.Debug.assert(isPatternMatch(pattern, candidate)); - return candidate.substr(pattern.prefix.length, candidate.length - pattern.suffix.length); - } - function findBestPatternMatch(values, getPattern, candidate) { - var matchedValue = undefined; - var longestMatchPrefixLength = -1; - for (var _i = 0, values_1 = values; _i < values_1.length; _i++) { - var v = values_1[_i]; - var pattern = getPattern(v); - if (isPatternMatch(pattern, candidate) && pattern.prefix.length > longestMatchPrefixLength) { - longestMatchPrefixLength = pattern.prefix.length; - matchedValue = v; + function isUniqueName(name) { + return !resolver.hasGlobalName(name) && + !ts.hasProperty(currentFileIdentifiers, name) && + !ts.hasProperty(generatedNameSet, name); + } + function isUniqueLocalName(name, container) { + for (var node = container; ts.isNodeDescendantOf(node, container); node = node.nextContainer) { + if (node.locals && ts.hasProperty(node.locals, name)) { + if (node.locals[name].flags & (107455 | 1048576 | 8388608)) { + return false; + } + } } + return true; } - return matchedValue; - } - ts.findBestPatternMatch = findBestPatternMatch; - function isPatternMatch(_a, candidate) { - var prefix = _a.prefix, suffix = _a.suffix; - return candidate.length >= prefix.length + suffix.length && - ts.startsWith(candidate, prefix) && - ts.endsWith(candidate, suffix); - } - function tryParsePattern(pattern) { - ts.Debug.assert(hasZeroOrOneAsteriskCharacter(pattern)); - var indexOfStar = pattern.indexOf("*"); - return indexOfStar === -1 ? undefined : { - prefix: pattern.substr(0, indexOfStar), - suffix: pattern.substr(indexOfStar + 1) - }; - } - ts.tryParsePattern = tryParsePattern; - function nodeModuleNameResolver(moduleName, containingFile, compilerOptions, host) { - var containingDirectory = ts.getDirectoryPath(containingFile); - var supportedExtensions = ts.getSupportedExtensions(compilerOptions); - var traceEnabled = isTraceEnabled(compilerOptions, host); - var failedLookupLocations = []; - var state = { compilerOptions: compilerOptions, host: host, traceEnabled: traceEnabled, skipTsx: false }; - var resolvedFileName = tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, nodeLoadModuleByRelativeName, failedLookupLocations, supportedExtensions, state); - var isExternalLibraryImport = false; - if (!resolvedFileName) { - if (moduleHasNonRelativeName(moduleName)) { - if (traceEnabled) { - trace(host, ts.Diagnostics.Loading_module_0_from_node_modules_folder, moduleName); + function makeTempVariableName(flags) { + if (flags && !(tempFlags & flags)) { + var name_44 = flags === 268435456 ? "_i" : "_n"; + if (isUniqueName(name_44)) { + tempFlags |= flags; + return name_44; + } + } + while (true) { + var count = tempFlags & 268435455; + tempFlags++; + if (count !== 8 && count !== 13) { + var name_45 = count < 26 + ? "_" + String.fromCharCode(97 + count) + : "_" + (count - 26); + if (isUniqueName(name_45)) { + return name_45; + } } - resolvedFileName = loadModuleFromNodeModules(moduleName, containingDirectory, failedLookupLocations, state); - isExternalLibraryImport = resolvedFileName !== undefined; - } - else { - var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); - resolvedFileName = nodeLoadModuleByRelativeName(candidate, supportedExtensions, failedLookupLocations, false, state); } } - if (resolvedFileName && host.realpath) { - var originalFileName = resolvedFileName; - resolvedFileName = ts.normalizePath(host.realpath(resolvedFileName)); - if (traceEnabled) { - trace(host, ts.Diagnostics.Resolving_real_path_for_0_result_1, originalFileName, resolvedFileName); + function makeUniqueName(baseName) { + if (baseName.charCodeAt(baseName.length - 1) !== 95) { + baseName += "_"; + } + var i = 1; + while (true) { + var generatedName = baseName + i; + if (isUniqueName(generatedName)) { + return generatedNameSet[generatedName] = generatedName; + } + i++; } } - return createResolvedModule(resolvedFileName, isExternalLibraryImport, failedLookupLocations); - } - ts.nodeModuleNameResolver = nodeModuleNameResolver; - function nodeLoadModuleByRelativeName(candidate, supportedExtensions, failedLookupLocations, onlyRecordFailures, state) { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Loading_module_as_file_Slash_folder_candidate_module_location_0, candidate); + function generateNameForModuleOrEnum(node) { + var name = getTextOfNode(node.name); + return isUniqueLocalName(name, node) ? name : makeUniqueName(name); } - var resolvedFileName = !ts.pathEndsWithDirectorySeparator(candidate) && loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, onlyRecordFailures, state); - return resolvedFileName || loadNodeModuleFromDirectory(supportedExtensions, candidate, failedLookupLocations, onlyRecordFailures, state); - } - function directoryProbablyExists(directoryName, host) { - return !host.directoryExists || host.directoryExists(directoryName); - } - ts.directoryProbablyExists = directoryProbablyExists; - function loadModuleFromFile(candidate, extensions, failedLookupLocation, onlyRecordFailures, state) { - var resolvedByAddingExtension = tryAddingExtensions(candidate, extensions, failedLookupLocation, onlyRecordFailures, state); - if (resolvedByAddingExtension) { - return resolvedByAddingExtension; + function generateNameForImportOrExportDeclaration(node) { + var expr = ts.getExternalModuleName(node); + var baseName = expr.kind === 9 ? + ts.escapeIdentifier(ts.makeIdentifierFromModuleName(expr.text)) : "module"; + return makeUniqueName(baseName); } - if (ts.hasJavaScriptFileExtension(candidate)) { - var extensionless = ts.removeFileExtension(candidate); - if (state.traceEnabled) { - var extension = candidate.substring(extensionless.length); - trace(state.host, ts.Diagnostics.File_name_0_has_a_1_extension_stripping_it, candidate, extension); - } - return tryAddingExtensions(extensionless, extensions, failedLookupLocation, onlyRecordFailures, state); + function generateNameForExportDefault() { + return makeUniqueName("default"); } - } - function tryAddingExtensions(candidate, extensions, failedLookupLocation, onlyRecordFailures, state) { - if (!onlyRecordFailures) { - var directory = ts.getDirectoryPath(candidate); - if (directory) { - onlyRecordFailures = !directoryProbablyExists(directory, state.host); - } + function generateNameForClassExpression() { + return makeUniqueName("class"); } - return ts.forEach(extensions, function (ext) { - return !(state.skipTsx && ts.isJsxOrTsxExtension(ext)) && tryFile(candidate + ext, failedLookupLocation, onlyRecordFailures, state); - }); - } - function tryFile(fileName, failedLookupLocation, onlyRecordFailures, state) { - if (!onlyRecordFailures && state.host.fileExists(fileName)) { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.File_0_exist_use_it_as_a_name_resolution_result, fileName); + function generateNameForNode(node) { + switch (node.kind) { + case 69: + return makeUniqueName(getTextOfNode(node)); + case 225: + case 224: + return generateNameForModuleOrEnum(node); + case 230: + case 236: + return generateNameForImportOrExportDeclaration(node); + case 220: + case 221: + case 235: + return generateNameForExportDefault(); + case 192: + return generateNameForClassExpression(); + default: + return makeTempVariableName(0); } - return fileName; } - else { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.File_0_does_not_exist, fileName); + function generateName(name) { + switch (name.autoGenerateKind) { + case 1: + return makeTempVariableName(0); + case 2: + return makeTempVariableName(268435456); + case 3: + return makeUniqueName(name.text); } - failedLookupLocation.push(fileName); - return undefined; + ts.Debug.fail("Unsupported GeneratedIdentifierKind."); } - } - function loadNodeModuleFromDirectory(extensions, candidate, failedLookupLocation, onlyRecordFailures, state) { - var packageJsonPath = pathToPackageJson(candidate); - var directoryExists = !onlyRecordFailures && directoryProbablyExists(candidate, state.host); - if (directoryExists && state.host.fileExists(packageJsonPath)) { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Found_package_json_at_0, packageJsonPath); - } - var typesFile = tryReadTypesSection(packageJsonPath, candidate, state); - if (typesFile) { - var onlyRecordFailures_1 = !directoryProbablyExists(ts.getDirectoryPath(typesFile), state.host); - var result = tryFile(typesFile, failedLookupLocation, onlyRecordFailures_1, state) || - tryAddingExtensions(typesFile, extensions, failedLookupLocation, onlyRecordFailures_1, state); - if (result) { - return result; + function getNodeForGeneratedName(name) { + var autoGenerateId = name.autoGenerateId; + var node = name; + var original = node.original; + while (original) { + node = original; + if (ts.isIdentifier(node) + && node.autoGenerateKind === 4 + && node.autoGenerateId !== autoGenerateId) { + break; } + original = node.original; + } + return node; + } + function getGeneratedIdentifier(name) { + if (name.autoGenerateKind === 4) { + var node = getNodeForGeneratedName(name); + var nodeId = ts.getNodeId(node); + return nodeIdToGeneratedName[nodeId] || (nodeIdToGeneratedName[nodeId] = ts.unescapeIdentifier(generateNameForNode(node))); } else { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.package_json_does_not_have_types_field); - } + var autoGenerateId = name.autoGenerateId; + return autoGeneratedIdToGeneratedName[autoGenerateId] || (autoGeneratedIdToGeneratedName[autoGenerateId] = ts.unescapeIdentifier(generateName(name))); } } - else { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.File_0_does_not_exist, packageJsonPath); - } - failedLookupLocation.push(packageJsonPath); + function createDelimiterMap() { + var delimiters = []; + delimiters[0] = ""; + delimiters[16] = ","; + delimiters[4] = " |"; + delimiters[8] = " &"; + return delimiters; } - return loadModuleFromFile(ts.combinePaths(candidate, "index"), extensions, failedLookupLocation, !directoryExists, state); - } - function pathToPackageJson(directory) { - return ts.combinePaths(directory, "package.json"); - } - function loadModuleFromNodeModulesFolder(moduleName, directory, failedLookupLocations, state) { - var nodeModulesFolder = ts.combinePaths(directory, "node_modules"); - var nodeModulesFolderExists = directoryProbablyExists(nodeModulesFolder, state.host); - var candidate = ts.normalizePath(ts.combinePaths(nodeModulesFolder, moduleName)); - var supportedExtensions = ts.getSupportedExtensions(state.compilerOptions); - var result = loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, !nodeModulesFolderExists, state); - if (result) { - return result; + function getDelimiter(format) { + return delimiters[format & 28]; } - result = loadNodeModuleFromDirectory(supportedExtensions, candidate, failedLookupLocations, !nodeModulesFolderExists, state); - if (result) { - return result; + function createBracketsMap() { + var brackets = []; + brackets[512] = ["{", "}"]; + brackets[1024] = ["(", ")"]; + brackets[2048] = ["<", ">"]; + brackets[4096] = ["[", "]"]; + return brackets; + } + function getOpeningBracket(format) { + return brackets[format & 7680][0]; + } + function getClosingBracket(format) { + return brackets[format & 7680][1]; } } - function loadModuleFromNodeModules(moduleName, directory, failedLookupLocations, state) { - directory = ts.normalizeSlashes(directory); + ts.emitFiles = emitFiles; +})(ts || (ts = {})); +var ts; +(function (ts) { + ts.version = "2.1.0"; + var emptyArray = []; + function findConfigFile(searchPath, fileExists, configName) { + if (configName === void 0) { configName = "tsconfig.json"; } while (true) { - var baseName = ts.getBaseFileName(directory); - if (baseName !== "node_modules") { - var packageResult = loadModuleFromNodeModulesFolder(moduleName, directory, failedLookupLocations, state); - if (packageResult && ts.hasTypeScriptFileExtension(packageResult)) { - return packageResult; - } - else { - var typesResult = loadModuleFromNodeModulesFolder(ts.combinePaths("@types", moduleName), directory, failedLookupLocations, state); - if (typesResult || packageResult) { - return typesResult || packageResult; - } - } + var fileName = ts.combinePaths(searchPath, configName); + if (fileExists(fileName)) { + return fileName; } - var parentPath = ts.getDirectoryPath(directory); - if (parentPath === directory) { + var parentPath = ts.getDirectoryPath(searchPath); + if (parentPath === searchPath) { break; } - directory = parentPath; + searchPath = parentPath; } return undefined; } - function classicNameResolver(moduleName, containingFile, compilerOptions, host) { - var traceEnabled = isTraceEnabled(compilerOptions, host); - var state = { compilerOptions: compilerOptions, host: host, traceEnabled: traceEnabled, skipTsx: !compilerOptions.jsx }; - var failedLookupLocations = []; - var supportedExtensions = ts.getSupportedExtensions(compilerOptions); - var containingDirectory = ts.getDirectoryPath(containingFile); - var resolvedFileName = tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, loadModuleFromFile, failedLookupLocations, supportedExtensions, state); - if (resolvedFileName) { - return createResolvedModule(resolvedFileName, false, failedLookupLocations); - } - var referencedSourceFile; - if (moduleHasNonRelativeName(moduleName)) { - while (true) { - var searchName = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); - referencedSourceFile = loadModuleFromFile(searchName, supportedExtensions, failedLookupLocations, false, state); - if (referencedSourceFile) { - break; - } - var parentPath = ts.getDirectoryPath(containingDirectory); - if (parentPath === containingDirectory) { + ts.findConfigFile = findConfigFile; + function resolveTripleslashReference(moduleName, containingFile) { + var basePath = ts.getDirectoryPath(containingFile); + var referencedFileName = ts.isRootedDiskPath(moduleName) ? moduleName : ts.combinePaths(basePath, moduleName); + return ts.normalizePath(referencedFileName); + } + ts.resolveTripleslashReference = resolveTripleslashReference; + function computeCommonSourceDirectoryOfFilenames(fileNames, currentDirectory, getCanonicalFileName) { + var commonPathComponents; + var failed = ts.forEach(fileNames, function (sourceFile) { + var sourcePathComponents = ts.getNormalizedPathComponents(sourceFile, currentDirectory); + sourcePathComponents.pop(); + if (!commonPathComponents) { + commonPathComponents = sourcePathComponents; + return; + } + for (var i = 0, n = Math.min(commonPathComponents.length, sourcePathComponents.length); i < n; i++) { + if (getCanonicalFileName(commonPathComponents[i]) !== getCanonicalFileName(sourcePathComponents[i])) { + if (i === 0) { + return true; + } + commonPathComponents.length = i; break; } - containingDirectory = parentPath; } + if (sourcePathComponents.length < commonPathComponents.length) { + commonPathComponents.length = sourcePathComponents.length; + } + }); + if (failed) { + return ""; } - else { - var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); - referencedSourceFile = loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, false, state); + if (!commonPathComponents) { + return currentDirectory; } - return referencedSourceFile - ? { resolvedModule: { resolvedFileName: referencedSourceFile }, failedLookupLocations: failedLookupLocations } - : { resolvedModule: undefined, failedLookupLocations: failedLookupLocations }; + return ts.getNormalizedPathFromPathComponents(commonPathComponents); } - ts.classicNameResolver = classicNameResolver; + ts.computeCommonSourceDirectoryOfFilenames = computeCommonSourceDirectoryOfFilenames; function createCompilerHost(options, setParentNodes) { var existingDirectories = ts.createMap(); function getCanonicalFileName(fileName) { @@ -47121,7 +47816,7 @@ var ts; readFile: function (fileName) { return ts.sys.readFile(fileName); }, trace: function (s) { return ts.sys.write(s + newLine); }, directoryExists: function (directoryName) { return ts.sys.directoryExists(directoryName); }, - getEnvironmentVariable: function (name) { return ts.getEnvironmentVariable(name, undefined); }, + getEnvironmentVariable: function (name) { return ts.sys.getEnvironmentVariable ? ts.sys.getEnvironmentVariable(name) : ""; }, getDirectories: function (path) { return ts.sys.getDirectories(path); }, realpath: realpath }; @@ -47181,41 +47876,14 @@ var ts; var resolutions = []; var cache = ts.createMap(); for (var _i = 0, names_2 = names; _i < names_2.length; _i++) { - var name_45 = names_2[_i]; - var result = name_45 in cache - ? cache[name_45] - : cache[name_45] = loader(name_45, containingFile); + var name_46 = names_2[_i]; + var result = name_46 in cache + ? cache[name_46] + : cache[name_46] = loader(name_46, containingFile); resolutions.push(result); } return resolutions; } - function getAutomaticTypeDirectiveNames(options, host) { - if (options.types) { - return options.types; - } - var result = []; - if (host.directoryExists && host.getDirectories) { - var typeRoots = getEffectiveTypeRoots(options, host); - if (typeRoots) { - for (var _i = 0, typeRoots_1 = typeRoots; _i < typeRoots_1.length; _i++) { - var root = typeRoots_1[_i]; - if (host.directoryExists(root)) { - for (var _a = 0, _b = host.getDirectories(root); _a < _b.length; _a++) { - var typeDirectivePath = _b[_a]; - var normalized = ts.normalizePath(typeDirectivePath); - var packageJsonPath = pathToPackageJson(ts.combinePaths(root, normalized)); - var isNotNeededPackage = host.fileExists(packageJsonPath) && readJson(packageJsonPath, host).typings === null; - if (!isNotNeededPackage) { - result.push(ts.getBaseFileName(normalized)); - } - } - } - } - } - } - return result; - } - ts.getAutomaticTypeDirectiveNames = getAutomaticTypeDirectiveNames; function createProgram(rootNames, options, host, oldProgram) { var program; var files = []; @@ -47241,7 +47909,7 @@ var ts; resolveModuleNamesWorker = function (moduleNames, containingFile) { return host.resolveModuleNames(moduleNames, containingFile); }; } else { - var loader_1 = function (moduleName, containingFile) { return resolveModuleName(moduleName, containingFile, options, host).resolvedModule; }; + var loader_1 = function (moduleName, containingFile) { return ts.resolveModuleName(moduleName, containingFile, options, host).resolvedModule; }; resolveModuleNamesWorker = function (moduleNames, containingFile) { return loadWithLocalCache(moduleNames, containingFile, loader_1); }; } var resolveTypeReferenceDirectiveNamesWorker; @@ -47249,15 +47917,15 @@ var ts; resolveTypeReferenceDirectiveNamesWorker = function (typeDirectiveNames, containingFile) { return host.resolveTypeReferenceDirectives(typeDirectiveNames, containingFile); }; } else { - var loader_2 = function (typesRef, containingFile) { return resolveTypeReferenceDirective(typesRef, containingFile, options, host).resolvedTypeReferenceDirective; }; + var loader_2 = function (typesRef, containingFile) { return ts.resolveTypeReferenceDirective(typesRef, containingFile, options, host).resolvedTypeReferenceDirective; }; resolveTypeReferenceDirectiveNamesWorker = function (typeReferenceDirectiveNames, containingFile) { return loadWithLocalCache(typeReferenceDirectiveNames, containingFile, loader_2); }; } var filesByName = ts.createFileMap(); var filesByNameIgnoreCase = host.useCaseSensitiveFileNames() ? ts.createFileMap(function (fileName) { return fileName.toLowerCase(); }) : undefined; if (!tryReuseStructureFromOldProgram()) { ts.forEach(rootNames, function (name) { return processRootFile(name, false); }); - var typeReferences = getAutomaticTypeDirectiveNames(options, host); - if (typeReferences) { + var typeReferences = ts.getAutomaticTypeDirectiveNames(options, host); + if (typeReferences.length) { var containingFilename = ts.combinePaths(host.getCurrentDirectory(), "__inferred type names__.ts"); var resolutions = resolveTypeReferenceDirectiveNamesWorker(typeReferences, containingFilename); for (var i = 0; i < typeReferences.length; i++) { @@ -47299,7 +47967,8 @@ var ts; getSymbolCount: function () { return getDiagnosticsProducingTypeChecker().getSymbolCount(); }, getTypeCount: function () { return getDiagnosticsProducingTypeChecker().getTypeCount(); }, getFileProcessingDiagnostics: function () { return fileProcessingDiagnostics; }, - getResolvedTypeReferenceDirectives: function () { return resolvedTypeReferenceDirectives; } + getResolvedTypeReferenceDirectives: function () { return resolvedTypeReferenceDirectives; }, + dropDiagnosticsProducingTypeChecker: dropDiagnosticsProducingTypeChecker }; verifyCompilerOptions(); ts.performance.mark("afterProgram"); @@ -47346,6 +48015,7 @@ var ts; (oldOptions.configFilePath !== options.configFilePath) || (oldOptions.baseUrl !== options.baseUrl) || (oldOptions.maxNodeModuleJsDepth !== options.maxNodeModuleJsDepth) || + !ts.arrayIsEqualTo(oldOptions.lib, options.lib) || !ts.arrayIsEqualTo(oldOptions.typeRoots, oldOptions.typeRoots) || !ts.arrayIsEqualTo(oldOptions.rootDirs, options.rootDirs) || !ts.equalOwnProperties(oldOptions.paths, options.paths)) { @@ -47446,16 +48116,19 @@ var ts; function getDiagnosticsProducingTypeChecker() { return diagnosticsProducingTypeChecker || (diagnosticsProducingTypeChecker = ts.createTypeChecker(program, true)); } + function dropDiagnosticsProducingTypeChecker() { + diagnosticsProducingTypeChecker = undefined; + } function getTypeChecker() { return noDiagnosticsTypeChecker || (noDiagnosticsTypeChecker = ts.createTypeChecker(program, false)); } - function emit(sourceFile, writeFileCallback, cancellationToken) { - return runWithCancellationToken(function () { return emitWorker(program, sourceFile, writeFileCallback, cancellationToken); }); + function emit(sourceFile, writeFileCallback, cancellationToken, emitOnlyDtsFiles) { + return runWithCancellationToken(function () { return emitWorker(program, sourceFile, writeFileCallback, cancellationToken, emitOnlyDtsFiles); }); } function isEmitBlocked(emitFileName) { return hasEmitBlockingDiagnostics.contains(ts.toPath(emitFileName, currentDirectory, getCanonicalFileName)); } - function emitWorker(program, sourceFile, writeFileCallback, cancellationToken) { + function emitWorker(program, sourceFile, writeFileCallback, cancellationToken, emitOnlyDtsFiles) { var declarationDiagnostics = []; if (options.noEmit) { return { diagnostics: declarationDiagnostics, sourceMaps: undefined, emittedFiles: undefined, emitSkipped: true }; @@ -47476,7 +48149,7 @@ var ts; } var emitResolver = getDiagnosticsProducingTypeChecker().getEmitResolver((options.outFile || options.out) ? undefined : sourceFile); ts.performance.mark("beforeEmit"); - var emitResult = ts.emitFiles(emitResolver, getEmitHost(writeFileCallback), sourceFile); + var emitResult = ts.emitFiles(emitResolver, getEmitHost(writeFileCallback), sourceFile, emitOnlyDtsFiles); ts.performance.mark("afterEmit"); ts.performance.measure("Emit", "beforeEmit", "afterEmit"); return emitResult; @@ -47991,7 +48664,6 @@ var ts; for (var i = 0; i < moduleNames.length; i++) { var resolution = resolutions[i]; ts.setResolvedModule(file, moduleNames[i], resolution); - var resolvedPath = resolution ? ts.toPath(resolution.resolvedFileName, currentDirectory, getCanonicalFileName) : undefined; var isFromNodeModulesSearch = resolution && resolution.isExternalLibraryImport; var isJsFileFromNodeModules = isFromNodeModulesSearch && ts.hasJavaScriptFileExtension(resolution.resolvedFileName); if (isFromNodeModulesSearch) { @@ -48003,7 +48675,7 @@ var ts; modulesWithElidedImports[file.path] = true; } else if (shouldAddFile) { - findSourceFile(resolution.resolvedFileName, resolvedPath, false, false, file, ts.skipTrivia(file.text, file.imports[i].pos), file.imports[i].end); + findSourceFile(resolution.resolvedFileName, ts.toPath(resolution.resolvedFileName, currentDirectory, getCanonicalFileName), false, false, file, ts.skipTrivia(file.text, file.imports[i].pos), file.imports[i].end); } if (isFromNodeModulesSearch) { currentNodeModulesDepth--; @@ -48017,8 +48689,8 @@ var ts; } function computeCommonSourceDirectory(sourceFiles) { var fileNames = []; - for (var _i = 0, sourceFiles_5 = sourceFiles; _i < sourceFiles_5.length; _i++) { - var file = sourceFiles_5[_i]; + for (var _i = 0, sourceFiles_6 = sourceFiles; _i < sourceFiles_6.length; _i++) { + var file = sourceFiles_6[_i]; if (!file.isDeclarationFile) { fileNames.push(file.fileName); } @@ -48029,8 +48701,8 @@ var ts; var allFilesBelongToPath = true; if (sourceFiles) { var absoluteRootDirectoryPath = host.getCanonicalFileName(ts.getNormalizedAbsolutePath(rootDirectory, currentDirectory)); - for (var _i = 0, sourceFiles_6 = sourceFiles; _i < sourceFiles_6.length; _i++) { - var sourceFile = sourceFiles_6[_i]; + for (var _i = 0, sourceFiles_7 = sourceFiles; _i < sourceFiles_7.length; _i++) { + var sourceFile = sourceFiles_7[_i]; if (!ts.isDeclarationFile(sourceFile)) { var absoluteSourceFilePath = host.getCanonicalFileName(ts.getNormalizedAbsolutePath(sourceFile.fileName, currentDirectory)); if (absoluteSourceFilePath.indexOf(absoluteRootDirectoryPath) !== 0) { @@ -48073,7 +48745,7 @@ var ts; if (!ts.hasProperty(options.paths, key)) { continue; } - if (!hasZeroOrOneAsteriskCharacter(key)) { + if (!ts.hasZeroOrOneAsteriskCharacter(key)) { programDiagnostics.add(ts.createCompilerDiagnostic(ts.Diagnostics.Pattern_0_can_have_at_most_one_Asterisk_character, key)); } if (ts.isArray(options.paths[key])) { @@ -48084,7 +48756,7 @@ var ts; var subst = _a[_i]; var typeOfSubst = typeof subst; if (typeOfSubst === "string") { - if (!hasZeroOrOneAsteriskCharacter(subst)) { + if (!ts.hasZeroOrOneAsteriskCharacter(subst)) { programDiagnostics.add(ts.createCompilerDiagnostic(ts.Diagnostics.Substitution_0_in_pattern_1_in_can_have_at_most_one_Asterisk_character, subst, key)); } } @@ -48123,6 +48795,9 @@ var ts; if (options.lib && options.noLib) { programDiagnostics.add(ts.createCompilerDiagnostic(ts.Diagnostics.Option_0_cannot_be_specified_with_option_1, "lib", "noLib")); } + if (options.noImplicitUseStrict && options.alwaysStrict) { + programDiagnostics.add(ts.createCompilerDiagnostic(ts.Diagnostics.Option_0_cannot_be_specified_with_option_1, "noImplicitUseStrict", "alwaysStrict")); + } var languageVersion = options.target || 0; var outFile = options.outFile || options.out; var firstNonAmbientExternalModuleSourceFile = ts.forEach(files, function (f) { return ts.isExternalModule(f) && !ts.isDeclarationFile(f) ? f : undefined; }); @@ -48685,7 +49360,7 @@ var ts; case 97: return true; case 69: - return node.originalKeywordKind === 97 && node.parent.kind === 142; + return ts.identifierIsThisKeyword(node) && node.parent.kind === 142; default: return false; } @@ -49258,7 +49933,6 @@ var ts; } ts.isInNonReferenceComment = isInNonReferenceComment; })(ts || (ts = {})); -var ts; (function (ts) { function isFirstDeclarationOfSymbolParameter(symbol) { return symbol.declarations && symbol.declarations.length > 0 && symbol.declarations[0].kind === 142; @@ -49481,7 +50155,7 @@ var ts; return ts.ensureScriptKind(fileName, scriptKind); } ts.getScriptKind = getScriptKind; - function parseAndReEmitConfigJSONFile(content) { + function sanitizeConfigFile(configFileName, content) { var options = { fileName: "config.js", compilerOptions: { @@ -49492,14 +50166,17 @@ var ts; }; var _a = ts.transpileModule("(" + content + ")", options), outputText = _a.outputText, diagnostics = _a.diagnostics; var trimmedOutput = outputText.trim(); - var configJsonObject = JSON.parse(trimmedOutput.substring(1, trimmedOutput.length - 2)); for (var _i = 0, diagnostics_2 = diagnostics; _i < diagnostics_2.length; _i++) { var diagnostic = diagnostics_2[_i]; diagnostic.start = diagnostic.start - 1; } - return { configJsonObject: configJsonObject, diagnostics: diagnostics }; + var _b = ts.parseConfigFileTextToJson(configFileName, trimmedOutput.substring(1, trimmedOutput.length - 2), false), config = _b.config, error = _b.error; + return { + configJsonObject: config || {}, + diagnostics: error ? ts.concatenate(diagnostics, [error]) : diagnostics + }; } - ts.parseAndReEmitConfigJSONFile = parseAndReEmitConfigJSONFile; + ts.sanitizeConfigFile = sanitizeConfigFile; })(ts || (ts = {})); var ts; (function (ts) { @@ -50701,8 +51378,7 @@ var ts; return; case 142: if (token.parent.name === token) { - var isThis_1 = token.kind === 69 && token.originalKeywordKind === 97; - return isThis_1 ? 3 : 17; + return ts.isThisIdentifier(token) ? 3 : 17; } return; } @@ -50743,13 +51419,13 @@ var ts; if (!completionData) { return undefined; } - var symbols = completionData.symbols, isMemberCompletion = completionData.isMemberCompletion, isNewIdentifierLocation = completionData.isNewIdentifierLocation, location = completionData.location, isJsDocTagName = completionData.isJsDocTagName; + var symbols = completionData.symbols, isGlobalCompletion = completionData.isGlobalCompletion, isMemberCompletion = completionData.isMemberCompletion, isNewIdentifierLocation = completionData.isNewIdentifierLocation, location = completionData.location, isJsDocTagName = completionData.isJsDocTagName; if (isJsDocTagName) { - return { isMemberCompletion: false, isNewIdentifierLocation: false, entries: ts.JsDoc.getAllJsDocCompletionEntries() }; + return { isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: false, entries: ts.JsDoc.getAllJsDocCompletionEntries() }; } var entries = []; if (ts.isSourceFileJavaScript(sourceFile)) { - var uniqueNames = getCompletionEntriesFromSymbols(symbols, entries, location, false); + var uniqueNames = getCompletionEntriesFromSymbols(symbols, entries, location, true); ts.addRange(entries, getJavaScriptCompletionEntries(sourceFile, location.pos, uniqueNames)); } else { @@ -50773,17 +51449,17 @@ var ts; if (!isMemberCompletion && !isJsDocTagName) { ts.addRange(entries, keywordCompletions); } - return { isMemberCompletion: isMemberCompletion, isNewIdentifierLocation: isNewIdentifierLocation || ts.isSourceFileJavaScript(sourceFile), entries: entries }; + return { isGlobalCompletion: isGlobalCompletion, isMemberCompletion: isMemberCompletion, isNewIdentifierLocation: isNewIdentifierLocation, entries: entries }; function getJavaScriptCompletionEntries(sourceFile, position, uniqueNames) { var entries = []; var nameTable = ts.getNameTable(sourceFile); - for (var name_46 in nameTable) { - if (nameTable[name_46] === position) { + for (var name_47 in nameTable) { + if (nameTable[name_47] === position) { continue; } - if (!uniqueNames[name_46]) { - uniqueNames[name_46] = name_46; - var displayName = getCompletionEntryDisplayName(ts.unescapeIdentifier(name_46), compilerOptions.target, true); + if (!uniqueNames[name_47]) { + uniqueNames[name_47] = name_47; + var displayName = getCompletionEntryDisplayName(ts.unescapeIdentifier(name_47), compilerOptions.target, true); if (displayName) { var entry = { name: displayName, @@ -50833,7 +51509,9 @@ var ts; if (!node || node.kind !== 9) { return undefined; } - if (node.parent.kind === 253 && node.parent.parent.kind === 171) { + if (node.parent.kind === 253 && + node.parent.parent.kind === 171 && + node.parent.name === node) { return getStringLiteralCompletionEntriesFromPropertyAssignment(node.parent); } else if (ts.isElementAccessExpression(node.parent) && node.parent.argumentExpression === node) { @@ -50856,7 +51534,7 @@ var ts; if (type) { getCompletionEntriesFromSymbols(type.getApparentProperties(), entries, element, false); if (entries.length) { - return { isMemberCompletion: true, isNewIdentifierLocation: true, entries: entries }; + return { isGlobalCompletion: false, isMemberCompletion: true, isNewIdentifierLocation: true, entries: entries }; } } } @@ -50872,7 +51550,7 @@ var ts; } } if (entries.length) { - return { isMemberCompletion: false, isNewIdentifierLocation: true, entries: entries }; + return { isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: true, entries: entries }; } return undefined; } @@ -50882,7 +51560,7 @@ var ts; if (type) { getCompletionEntriesFromSymbols(type.getApparentProperties(), entries, node, false); if (entries.length) { - return { isMemberCompletion: true, isNewIdentifierLocation: true, entries: entries }; + return { isGlobalCompletion: false, isMemberCompletion: true, isNewIdentifierLocation: true, entries: entries }; } } return undefined; @@ -50893,7 +51571,7 @@ var ts; var entries_2 = []; addStringLiteralCompletionsFromType(type, entries_2); if (entries_2.length) { - return { isMemberCompletion: false, isNewIdentifierLocation: false, entries: entries_2 }; + return { isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: false, entries: entries_2 }; } } return undefined; @@ -50934,6 +51612,7 @@ var ts; entries = getCompletionEntriesForNonRelativeModules(literalValue, scriptDirectory, span); } return { + isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: true, entries: entries @@ -50964,13 +51643,15 @@ var ts; } function getCompletionEntriesForDirectoryFragment(fragment, scriptPath, extensions, includeExtensions, span, exclude, result) { if (result === void 0) { result = []; } - fragment = ts.getDirectoryPath(fragment); - if (!fragment) { - fragment = "./"; + if (fragment === undefined) { + fragment = ""; } - else { - fragment = ts.ensureTrailingDirectorySeparator(fragment); + fragment = ts.normalizeSlashes(fragment); + fragment = ts.getDirectoryPath(fragment); + if (fragment === "") { + fragment = "." + ts.directorySeparator; } + fragment = ts.ensureTrailingDirectorySeparator(fragment); var absolutePath = normalizeAndPreserveTrailingSlash(ts.isRootedDiskPath(fragment) ? fragment : ts.combinePaths(scriptPath, fragment)); var baseDirectory = ts.getDirectoryPath(absolutePath); var ignoreCase = !(host.useCaseSensitiveFileNames && host.useCaseSensitiveFileNames()); @@ -51126,6 +51807,12 @@ var ts; if (!range) { return undefined; } + var completionInfo = { + isGlobalCompletion: false, + isMemberCompletion: false, + isNewIdentifierLocation: true, + entries: [] + }; var text = sourceFile.text.substr(range.pos, position - range.pos); var match = tripleSlashDirectiveFragmentRegex.exec(text); if (match) { @@ -51133,22 +51820,16 @@ var ts; var kind = match[2]; var toComplete = match[3]; var scriptPath = ts.getDirectoryPath(sourceFile.path); - var entries_3; if (kind === "path") { var span_10 = getDirectoryFragmentTextSpan(toComplete, range.pos + prefix.length); - entries_3 = getCompletionEntriesForDirectoryFragment(toComplete, scriptPath, ts.getSupportedExtensions(compilerOptions), true, span_10, sourceFile.path); + completionInfo.entries = getCompletionEntriesForDirectoryFragment(toComplete, scriptPath, ts.getSupportedExtensions(compilerOptions), true, span_10, sourceFile.path); } else { var span_11 = { start: range.pos + prefix.length, length: match[0].length - prefix.length }; - entries_3 = getCompletionEntriesFromTypings(host, compilerOptions, scriptPath, span_11); + completionInfo.entries = getCompletionEntriesFromTypings(host, compilerOptions, scriptPath, span_11); } - return { - isMemberCompletion: false, - isNewIdentifierLocation: true, - entries: entries_3 - }; } - return undefined; + return completionInfo; } function getCompletionEntriesFromTypings(host, options, scriptPath, span, result) { if (result === void 0) { result = []; } @@ -51347,7 +52028,7 @@ var ts; } } if (isJsDocTagName) { - return { symbols: undefined, isMemberCompletion: false, isNewIdentifierLocation: false, location: undefined, isRightOfDot: false, isJsDocTagName: isJsDocTagName }; + return { symbols: undefined, isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: false, location: undefined, isRightOfDot: false, isJsDocTagName: isJsDocTagName }; } if (!insideJsDocTagExpression) { log("Returning an empty list because completion was inside a regular comment or plain text part of a JsDoc comment."); @@ -51399,6 +52080,7 @@ var ts; } } var semanticStart = ts.timestamp(); + var isGlobalCompletion = false; var isMemberCompletion; var isNewIdentifierLocation; var symbols = []; @@ -51429,10 +52111,12 @@ var ts; if (!tryGetGlobalSymbols()) { return undefined; } + isGlobalCompletion = true; } log("getCompletionData: Semantic work: " + (ts.timestamp() - semanticStart)); - return { symbols: symbols, isMemberCompletion: isMemberCompletion, isNewIdentifierLocation: isNewIdentifierLocation, location: location, isRightOfDot: (isRightOfDot || isRightOfOpenTag), isJsDocTagName: isJsDocTagName }; + return { symbols: symbols, isGlobalCompletion: isGlobalCompletion, isMemberCompletion: isMemberCompletion, isNewIdentifierLocation: isNewIdentifierLocation, location: location, isRightOfDot: (isRightOfDot || isRightOfOpenTag), isJsDocTagName: isJsDocTagName }; function getTypeScriptMemberSymbols() { + isGlobalCompletion = false; isMemberCompletion = true; isNewIdentifierLocation = false; if (node.kind === 69 || node.kind === 139 || node.kind === 172) { @@ -51483,6 +52167,7 @@ var ts; var attrsType = void 0; if ((jsxContainer.kind === 242) || (jsxContainer.kind === 243)) { attrsType = typeChecker.getJsxElementAttributesType(jsxContainer); + isGlobalCompletion = false; if (attrsType) { symbols = filterJsxAttributes(typeChecker.getPropertiesOfType(attrsType), jsxContainer.attributes); isMemberCompletion = true; @@ -51842,8 +52527,8 @@ var ts; if (element.getStart() <= position && position <= element.getEnd()) { continue; } - var name_47 = element.propertyName || element.name; - existingImportsOrExports[name_47.text] = true; + var name_48 = element.propertyName || element.name; + existingImportsOrExports[name_48.text] = true; } if (!ts.someProperties(existingImportsOrExports)) { return ts.filter(exportsOfModule, function (e) { return e.name !== "default"; }); @@ -51927,7 +52612,7 @@ var ts; sortText: "0" }); } - var tripleSlashDirectiveFragmentRegex = /^(\/\/\/\s* sourceFile.text.length) { return getBaseIndentation(options); } - if (options.IndentStyle === ts.IndentStyle.None) { + if (options.indentStyle === ts.IndentStyle.None) { return 0; } var precedingToken = ts.findPrecedingToken(position, sourceFile); @@ -58576,7 +59140,7 @@ var ts; return 0; } var lineAtPosition = sourceFile.getLineAndCharacterOfPosition(position).line; - if (options.IndentStyle === ts.IndentStyle.Block) { + if (options.indentStyle === ts.IndentStyle.Block) { var current_1 = position; while (current_1 > 0) { var char = sourceFile.text.charCodeAt(current_1); @@ -58605,7 +59169,7 @@ var ts; indentationDelta = 0; } else { - indentationDelta = lineAtPosition !== currentStart.line ? options.IndentSize : 0; + indentationDelta = lineAtPosition !== currentStart.line ? options.indentSize : 0; } break; } @@ -58615,7 +59179,7 @@ var ts; } actualIndentation = getLineIndentationWhenExpressionIsInMultiLine(current, sourceFile, options); if (actualIndentation !== -1) { - return actualIndentation + options.IndentSize; + return actualIndentation + options.indentSize; } previous = current; current = current.parent; @@ -58626,15 +59190,15 @@ var ts; return getIndentationForNodeWorker(current, currentStart, undefined, indentationDelta, sourceFile, options); } SmartIndenter.getIndentation = getIndentation; - function getBaseIndentation(options) { - return options.BaseIndentSize || 0; - } - SmartIndenter.getBaseIndentation = getBaseIndentation; function getIndentationForNode(n, ignoreActualIndentationRange, sourceFile, options) { var start = sourceFile.getLineAndCharacterOfPosition(n.getStart(sourceFile)); return getIndentationForNodeWorker(n, start, ignoreActualIndentationRange, 0, sourceFile, options); } SmartIndenter.getIndentationForNode = getIndentationForNode; + function getBaseIndentation(options) { + return options.baseIndentSize || 0; + } + SmartIndenter.getBaseIndentation = getBaseIndentation; function getIndentationForNodeWorker(current, currentStart, ignoreActualIndentationRange, indentationDelta, sourceFile, options) { var parent = current.parent; var parentStart; @@ -58664,7 +59228,7 @@ var ts; } } if (shouldIndentChildNode(parent, current) && !parentAndChildShareLine) { - indentationDelta += options.IndentSize; + indentationDelta += options.indentSize; } current = parent; currentStart = parentStart; @@ -58842,7 +59406,7 @@ var ts; break; } if (ch === 9) { - column += options.TabSize + (column % options.TabSize); + column += options.tabSize + (column % options.tabSize); } else { column++; @@ -58940,11 +59504,116 @@ var ts; })(formatting = ts.formatting || (ts.formatting = {})); })(ts || (ts = {})); var ts; +(function (ts) { + var codefix; + (function (codefix) { + var codeFixes = ts.createMap(); + function registerCodeFix(action) { + ts.forEach(action.errorCodes, function (error) { + var fixes = codeFixes[error]; + if (!fixes) { + fixes = []; + codeFixes[error] = fixes; + } + fixes.push(action); + }); + } + codefix.registerCodeFix = registerCodeFix; + function getSupportedErrorCodes() { + return Object.keys(codeFixes); + } + codefix.getSupportedErrorCodes = getSupportedErrorCodes; + function getFixes(context) { + var fixes = codeFixes[context.errorCode]; + var allActions = []; + ts.forEach(fixes, function (f) { + var actions = f.getCodeActions(context); + if (actions && actions.length > 0) { + allActions = allActions.concat(actions); + } + }); + return allActions; + } + codefix.getFixes = getFixes; + })(codefix = ts.codefix || (ts.codefix = {})); +})(ts || (ts = {})); +var ts; +(function (ts) { + var codefix; + (function (codefix) { + function getOpenBraceEnd(constructor, sourceFile) { + return constructor.body.getFirstToken(sourceFile).getEnd(); + } + codefix.registerCodeFix({ + errorCodes: [ts.Diagnostics.Constructors_for_derived_classes_must_contain_a_super_call.code], + getCodeActions: function (context) { + var sourceFile = context.sourceFile; + var token = ts.getTokenAtPosition(sourceFile, context.span.start); + if (token.kind !== 121) { + return undefined; + } + var newPosition = getOpenBraceEnd(token.parent, sourceFile); + return [{ + description: ts.getLocaleSpecificMessage(ts.Diagnostics.Add_missing_super_call), + changes: [{ fileName: sourceFile.fileName, textChanges: [{ newText: "super();", span: { start: newPosition, length: 0 } }] }] + }]; + } + }); + codefix.registerCodeFix({ + errorCodes: [ts.Diagnostics.super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class.code], + getCodeActions: function (context) { + var sourceFile = context.sourceFile; + var token = ts.getTokenAtPosition(sourceFile, context.span.start); + if (token.kind !== 97) { + return undefined; + } + var constructor = ts.getContainingFunction(token); + var superCall = findSuperCall(constructor.body); + if (!superCall) { + return undefined; + } + if (superCall.expression && superCall.expression.kind == 174) { + var arguments_1 = superCall.expression.arguments; + for (var i = 0; i < arguments_1.length; i++) { + if (arguments_1[i].expression === token) { + return undefined; + } + } + } + var newPosition = getOpenBraceEnd(constructor, sourceFile); + var changes = [{ + fileName: sourceFile.fileName, textChanges: [{ + newText: superCall.getText(sourceFile), + span: { start: newPosition, length: 0 } + }, + { + newText: "", + span: { start: superCall.getStart(sourceFile), length: superCall.getWidth(sourceFile) } + }] + }]; + return [{ + description: ts.getLocaleSpecificMessage(ts.Diagnostics.Make_super_call_the_first_statement_in_the_constructor), + changes: changes + }]; + function findSuperCall(n) { + if (n.kind === 202 && ts.isSuperCall(n.expression)) { + return n; + } + if (ts.isFunctionLike(n)) { + return undefined; + } + return ts.forEachChild(n, findSuperCall); + } + } + }); + })(codefix = ts.codefix || (ts.codefix = {})); +})(ts || (ts = {})); +var ts; (function (ts) { ts.servicesVersion = "0.5"; function createNode(kind, pos, end, parent) { var node = kind >= 139 ? new NodeObject(kind, pos, end) : - kind === 69 ? new IdentifierObject(kind, pos, end) : + kind === 69 ? new IdentifierObject(69, pos, end) : new TokenObject(kind, pos, end); node.parent = parent; return node; @@ -59167,15 +59836,16 @@ var ts; var TokenObject = (function (_super) { __extends(TokenObject, _super); function TokenObject(kind, pos, end) { - _super.call(this, pos, end); - this.kind = kind; + var _this = _super.call(this, pos, end) || this; + _this.kind = kind; + return _this; } return TokenObject; }(TokenOrIdentifierObject)); var IdentifierObject = (function (_super) { __extends(IdentifierObject, _super); function IdentifierObject(kind, pos, end) { - _super.call(this, pos, end); + return _super.call(this, pos, end) || this; } return IdentifierObject; }(TokenOrIdentifierObject)); @@ -59249,7 +59919,7 @@ var ts; var SourceFileObject = (function (_super) { __extends(SourceFileObject, _super); function SourceFileObject(kind, pos, end) { - _super.call(this, kind, pos, end); + return _super.call(this, kind, pos, end) || this; } SourceFileObject.prototype.update = function (newText, textChangeRange) { return ts.updateSourceFile(this, newText, textChangeRange); @@ -59406,6 +60076,30 @@ var ts; getSignatureConstructor: function () { return SignatureObject; } }; } + function toEditorSettings(optionsAsMap) { + var allPropertiesAreCamelCased = true; + for (var key in optionsAsMap) { + if (ts.hasProperty(optionsAsMap, key) && !isCamelCase(key)) { + allPropertiesAreCamelCased = false; + break; + } + } + if (allPropertiesAreCamelCased) { + return optionsAsMap; + } + var settings = {}; + for (var key in optionsAsMap) { + if (ts.hasProperty(optionsAsMap, key)) { + var newKey = isCamelCase(key) ? key : key.charAt(0).toLowerCase() + key.substr(1); + settings[newKey] = optionsAsMap[key]; + } + } + return settings; + } + ts.toEditorSettings = toEditorSettings; + function isCamelCase(s) { + return !s.length || s.charAt(0) === s.charAt(0).toLowerCase(); + } function displayPartsToString(displayParts) { if (displayParts) { return ts.map(displayParts, function (displayPart) { return displayPart.text; }).join(""); @@ -59420,6 +60114,10 @@ var ts; }; } ts.getDefaultCompilerOptions = getDefaultCompilerOptions; + function getSupportedCodeFixes() { + return ts.codefix.getSupportedErrorCodes(); + } + ts.getSupportedCodeFixes = getSupportedCodeFixes; var HostCache = (function () { function HostCache(host, getCanonicalFileName) { this.host = host; @@ -59583,7 +60281,8 @@ var ts; var ruleProvider; var program; var lastProjectVersion; - var useCaseSensitivefileNames = false; + var lastTypesRootVersion = 0; + var useCaseSensitivefileNames = host.useCaseSensitiveFileNames && host.useCaseSensitiveFileNames(); var cancellationToken = new CancellationTokenObject(host.getCancellationToken && host.getCancellationToken()); var currentDirectory = host.getCurrentDirectory(); if (!ts.localizedDiagnosticMessages && host.getLocalizedDiagnosticMessages) { @@ -59619,6 +60318,12 @@ var ts; lastProjectVersion = hostProjectVersion; } } + var typeRootsVersion = host.getTypeRootsVersion ? host.getTypeRootsVersion() : 0; + if (lastTypesRootVersion !== typeRootsVersion) { + log("TypeRoots version has changed; provide new program"); + program = undefined; + lastTypesRootVersion = typeRootsVersion; + } var hostCache = new HostCache(host, getCanonicalFileName); if (programUpToDate()) { return; @@ -59878,12 +60583,12 @@ var ts; synchronizeHostData(); return ts.FindAllReferences.findReferencedSymbols(program.getTypeChecker(), cancellationToken, program.getSourceFiles(), getValidSourceFile(fileName), position, findInStrings, findInComments); } - function getNavigateToItems(searchValue, maxResultCount, fileName) { + function getNavigateToItems(searchValue, maxResultCount, fileName, excludeDtsFiles) { synchronizeHostData(); var sourceFiles = fileName ? [getValidSourceFile(fileName)] : program.getSourceFiles(); - return ts.NavigateTo.getNavigateToItems(sourceFiles, program.getTypeChecker(), cancellationToken, searchValue, maxResultCount); + return ts.NavigateTo.getNavigateToItems(sourceFiles, program.getTypeChecker(), cancellationToken, searchValue, maxResultCount, excludeDtsFiles); } - function getEmitOutput(fileName) { + function getEmitOutput(fileName, emitOnlyDtsFiles) { synchronizeHostData(); var sourceFile = getValidSourceFile(fileName); var outputFiles = []; @@ -59894,7 +60599,7 @@ var ts; text: data }); } - var emitOutput = program.emit(sourceFile, writeFile, cancellationToken); + var emitOutput = program.emit(sourceFile, writeFile, cancellationToken, emitOnlyDtsFiles); return { outputFiles: outputFiles, emitSkipped: emitOutput.emitSkipped @@ -59957,14 +60662,26 @@ var ts; return ts.BreakpointResolver.spanInSourceFileAtLocation(sourceFile, position); } function getNavigationBarItems(fileName) { - var sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName); - return ts.NavigationBar.getNavigationBarItems(sourceFile); + return ts.NavigationBar.getNavigationBarItems(syntaxTreeCache.getCurrentSourceFile(fileName)); + } + function getNavigationTree(fileName) { + return ts.NavigationBar.getNavigationTree(syntaxTreeCache.getCurrentSourceFile(fileName)); + } + function isTsOrTsxFile(fileName) { + var kind = ts.getScriptKind(fileName, host); + return kind === 3 || kind === 4; } function getSemanticClassifications(fileName, span) { + if (!isTsOrTsxFile(fileName)) { + return []; + } synchronizeHostData(); return ts.getSemanticClassifications(program.getTypeChecker(), cancellationToken, getValidSourceFile(fileName), program.getClassifiableNames(), span); } function getEncodedSemanticClassifications(fileName, span) { + if (!isTsOrTsxFile(fileName)) { + return { spans: [], endOfLineState: 0 }; + } synchronizeHostData(); return ts.getEncodedSemanticClassifications(program.getTypeChecker(), cancellationToken, getValidSourceFile(fileName), program.getClassifiableNames(), span); } @@ -60020,34 +60737,60 @@ var ts; } function getIndentationAtPosition(fileName, position, editorOptions) { var start = ts.timestamp(); + var settings = toEditorSettings(editorOptions); var sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName); log("getIndentationAtPosition: getCurrentSourceFile: " + (ts.timestamp() - start)); start = ts.timestamp(); - var result = ts.formatting.SmartIndenter.getIndentation(position, sourceFile, editorOptions); + var result = ts.formatting.SmartIndenter.getIndentation(position, sourceFile, settings); log("getIndentationAtPosition: computeIndentation : " + (ts.timestamp() - start)); return result; } function getFormattingEditsForRange(fileName, start, end, options) { var sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName); - return ts.formatting.formatSelection(start, end, sourceFile, getRuleProvider(options), options); + var settings = toEditorSettings(options); + return ts.formatting.formatSelection(start, end, sourceFile, getRuleProvider(settings), settings); } function getFormattingEditsForDocument(fileName, options) { var sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName); - return ts.formatting.formatDocument(sourceFile, getRuleProvider(options), options); + var settings = toEditorSettings(options); + return ts.formatting.formatDocument(sourceFile, getRuleProvider(settings), settings); } function getFormattingEditsAfterKeystroke(fileName, position, key, options) { var sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName); + var settings = toEditorSettings(options); if (key === "}") { - return ts.formatting.formatOnClosingCurly(position, sourceFile, getRuleProvider(options), options); + return ts.formatting.formatOnClosingCurly(position, sourceFile, getRuleProvider(settings), settings); } else if (key === ";") { - return ts.formatting.formatOnSemicolon(position, sourceFile, getRuleProvider(options), options); + return ts.formatting.formatOnSemicolon(position, sourceFile, getRuleProvider(settings), settings); } else if (key === "\n") { - return ts.formatting.formatOnEnter(position, sourceFile, getRuleProvider(options), options); + return ts.formatting.formatOnEnter(position, sourceFile, getRuleProvider(settings), settings); } return []; } + function getCodeFixesAtPosition(fileName, start, end, errorCodes) { + synchronizeHostData(); + var sourceFile = getValidSourceFile(fileName); + var span = { start: start, length: end - start }; + var newLineChar = ts.getNewLineOrDefaultFromHost(host); + var allFixes = []; + ts.forEach(errorCodes, function (error) { + cancellationToken.throwIfCancellationRequested(); + var context = { + errorCode: error, + sourceFile: sourceFile, + span: span, + program: program, + newLineCharacter: newLineChar + }; + var fixes = ts.codefix.getFixes(context); + if (fixes) { + allFixes = allFixes.concat(fixes); + } + }); + return allFixes; + } function getDocCommentTemplateAtPosition(fileName, position) { return ts.JsDoc.getDocCommentTemplateAtPosition(ts.getNewLineOrDefaultFromHost(host), syntaxTreeCache.getCurrentSourceFile(fileName), position); } @@ -60159,6 +60902,7 @@ var ts; getRenameInfo: getRenameInfo, findRenameLocations: findRenameLocations, getNavigationBarItems: getNavigationBarItems, + getNavigationTree: getNavigationTree, getOutliningSpans: getOutliningSpans, getTodoComments: getTodoComments, getBraceMatchingAtPosition: getBraceMatchingAtPosition, @@ -60168,6 +60912,7 @@ var ts; getFormattingEditsAfterKeystroke: getFormattingEditsAfterKeystroke, getDocCommentTemplateAtPosition: getDocCommentTemplateAtPosition, isValidBraceCompletionAtPosition: isValidBraceCompletionAtPosition, + getCodeFixesAtPosition: getCodeFixesAtPosition, getEmitOutput: getEmitOutput, getNonBoundSourceFile: getNonBoundSourceFile, getSourceFile: getSourceFile, @@ -60199,2391 +60944,3838 @@ var ts; ts.isLiteralComputedPropertyDeclarationName(node)) { nameTable[node.text] = nameTable[node.text] === undefined ? node.pos : -1; } - break; - default: - ts.forEachChild(node, walk); - if (node.jsDocComments) { - for (var _i = 0, _a = node.jsDocComments; _i < _a.length; _i++) { - var jsDocComment = _a[_i]; - ts.forEachChild(jsDocComment, walk); + break; + default: + ts.forEachChild(node, walk); + if (node.jsDocComments) { + for (var _i = 0, _a = node.jsDocComments; _i < _a.length; _i++) { + var jsDocComment = _a[_i]; + ts.forEachChild(jsDocComment, walk); + } + } + } + } + } + function isArgumentOfElementAccessExpression(node) { + return node && + node.parent && + node.parent.kind === 173 && + node.parent.argumentExpression === node; + } + function getDefaultLibFilePath(options) { + if (typeof __dirname !== "undefined") { + return __dirname + ts.directorySeparator + ts.getDefaultLibFileName(options); + } + throw new Error("getDefaultLibFilePath is only supported when consumed as a node module. "); + } + ts.getDefaultLibFilePath = getDefaultLibFilePath; + function initializeServices() { + ts.objectAllocator = getServicesObjectAllocator(); + } + initializeServices(); +})(ts || (ts = {})); +var ts; +(function (ts) { + var server; + (function (server) { + var ScriptInfo = (function () { + function ScriptInfo(host, fileName, content, scriptKind, isOpen, hasMixedContent) { + if (isOpen === void 0) { isOpen = false; } + if (hasMixedContent === void 0) { hasMixedContent = false; } + this.host = host; + this.fileName = fileName; + this.scriptKind = scriptKind; + this.isOpen = isOpen; + this.hasMixedContent = hasMixedContent; + this.containingProjects = []; + this.path = ts.toPath(fileName, host.getCurrentDirectory(), ts.createGetCanonicalFileName(host.useCaseSensitiveFileNames)); + this.svc = server.ScriptVersionCache.fromString(host, content); + this.scriptKind = scriptKind + ? scriptKind + : ts.getScriptKindFromFileName(fileName); + } + ScriptInfo.prototype.getFormatCodeSettings = function () { + return this.formatCodeSettings; + }; + ScriptInfo.prototype.attachToProject = function (project) { + var isNew = !this.isAttached(project); + if (isNew) { + this.containingProjects.push(project); + } + return isNew; + }; + ScriptInfo.prototype.isAttached = function (project) { + switch (this.containingProjects.length) { + case 0: return false; + case 1: return this.containingProjects[0] === project; + case 2: return this.containingProjects[0] === project || this.containingProjects[1] === project; + default: return ts.contains(this.containingProjects, project); + } + }; + ScriptInfo.prototype.detachFromProject = function (project) { + switch (this.containingProjects.length) { + case 0: + return; + case 1: + if (this.containingProjects[0] === project) { + this.containingProjects.pop(); + } + break; + case 2: + if (this.containingProjects[0] === project) { + this.containingProjects[0] = this.containingProjects.pop(); + } + else if (this.containingProjects[1] === project) { + this.containingProjects.pop(); + } + break; + default: + server.removeItemFromSet(this.containingProjects, project); + break; + } + }; + ScriptInfo.prototype.detachAllProjects = function () { + for (var _i = 0, _a = this.containingProjects; _i < _a.length; _i++) { + var p = _a[_i]; + p.removeFile(this, false); + } + this.containingProjects.length = 0; + }; + ScriptInfo.prototype.getDefaultProject = function () { + if (this.containingProjects.length === 0) { + return server.Errors.ThrowNoProject(); + } + ts.Debug.assert(this.containingProjects.length !== 0); + return this.containingProjects[0]; + }; + ScriptInfo.prototype.setFormatOptions = function (formatSettings) { + if (formatSettings) { + if (!this.formatCodeSettings) { + this.formatCodeSettings = server.getDefaultFormatCodeSettings(this.host); + } + server.mergeMaps(this.formatCodeSettings, formatSettings); + } + }; + ScriptInfo.prototype.setWatcher = function (watcher) { + this.stopWatcher(); + this.fileWatcher = watcher; + }; + ScriptInfo.prototype.stopWatcher = function () { + if (this.fileWatcher) { + this.fileWatcher.close(); + this.fileWatcher = undefined; + } + }; + ScriptInfo.prototype.getLatestVersion = function () { + return this.svc.latestVersion().toString(); + }; + ScriptInfo.prototype.reload = function (script) { + this.svc.reload(script); + this.markContainingProjectsAsDirty(); + }; + ScriptInfo.prototype.saveTo = function (fileName) { + var snap = this.snap(); + this.host.writeFile(fileName, snap.getText(0, snap.getLength())); + }; + ScriptInfo.prototype.reloadFromFile = function () { + if (this.hasMixedContent) { + this.reload(""); + } + else { + this.svc.reloadFromFile(this.fileName); + this.markContainingProjectsAsDirty(); + } + }; + ScriptInfo.prototype.snap = function () { + return this.svc.getSnapshot(); + }; + ScriptInfo.prototype.getLineInfo = function (line) { + var snap = this.snap(); + return snap.index.lineNumberToInfo(line); + }; + ScriptInfo.prototype.editContent = function (start, end, newText) { + this.svc.edit(start, end - start, newText); + this.markContainingProjectsAsDirty(); + }; + ScriptInfo.prototype.markContainingProjectsAsDirty = function () { + for (var _i = 0, _a = this.containingProjects; _i < _a.length; _i++) { + var p = _a[_i]; + p.markAsDirty(); + } + }; + ScriptInfo.prototype.lineToTextSpan = function (line) { + var index = this.snap().index; + var lineInfo = index.lineNumberToInfo(line + 1); + var len; + if (lineInfo.leaf) { + len = lineInfo.leaf.text.length; + } + else { + var nextLineInfo = index.lineNumberToInfo(line + 2); + len = nextLineInfo.offset - lineInfo.offset; + } + return ts.createTextSpan(lineInfo.offset, len); + }; + ScriptInfo.prototype.lineOffsetToPosition = function (line, offset) { + var index = this.snap().index; + var lineInfo = index.lineNumberToInfo(line); + return (lineInfo.offset + offset - 1); + }; + ScriptInfo.prototype.positionToLineOffset = function (position) { + var index = this.snap().index; + var lineOffset = index.charOffsetToLineNumberAndPos(position); + return { line: lineOffset.line, offset: lineOffset.offset + 1 }; + }; + return ScriptInfo; + }()); + server.ScriptInfo = ScriptInfo; + })(server = ts.server || (ts.server = {})); +})(ts || (ts = {})); +var ts; +(function (ts) { + var server; + (function (server) { + var LSHost = (function () { + function LSHost(host, project, cancellationToken) { + var _this = this; + this.host = host; + this.project = project; + this.cancellationToken = cancellationToken; + this.getCanonicalFileName = ts.createGetCanonicalFileName(this.host.useCaseSensitiveFileNames); + this.resolvedModuleNames = ts.createFileMap(); + this.resolvedTypeReferenceDirectives = ts.createFileMap(); + if (host.trace) { + this.trace = function (s) { return host.trace(s); }; + } + this.resolveModuleName = function (moduleName, containingFile, compilerOptions, host) { + var primaryResult = ts.resolveModuleName(moduleName, containingFile, compilerOptions, host); + if (primaryResult.resolvedModule) { + if (ts.fileExtensionIsAny(primaryResult.resolvedModule.resolvedFileName, ts.supportedTypeScriptExtensions)) { + return primaryResult; + } + } + var secondaryLookupFailedLookupLocations = []; + var globalCache = _this.project.projectService.typingsInstaller.globalTypingsCacheLocation; + if (_this.project.getTypingOptions().enableAutoDiscovery && globalCache) { + var traceEnabled = ts.isTraceEnabled(compilerOptions, host); + if (traceEnabled) { + ts.trace(host, ts.Diagnostics.Auto_discovery_for_typings_is_enabled_in_project_0_Running_extra_resolution_pass_for_module_1_using_cache_location_2, _this.project.getProjectName(), moduleName, globalCache); + } + var state = { compilerOptions: compilerOptions, host: host, skipTsx: false, traceEnabled: traceEnabled }; + var resolvedName = ts.loadModuleFromNodeModules(moduleName, globalCache, secondaryLookupFailedLookupLocations, state, true); + if (resolvedName) { + return ts.createResolvedModule(resolvedName, true, primaryResult.failedLookupLocations.concat(secondaryLookupFailedLookupLocations)); + } + } + if (!primaryResult.resolvedModule && secondaryLookupFailedLookupLocations.length) { + primaryResult.failedLookupLocations = primaryResult.failedLookupLocations.concat(secondaryLookupFailedLookupLocations); + } + return primaryResult; + }; + } + LSHost.prototype.resolveNamesWithLocalCache = function (names, containingFile, cache, loader, getResult) { + var path = ts.toPath(containingFile, this.host.getCurrentDirectory(), this.getCanonicalFileName); + var currentResolutionsInFile = cache.get(path); + var newResolutions = ts.createMap(); + var resolvedModules = []; + var compilerOptions = this.getCompilationSettings(); + var lastDeletedFileName = this.project.projectService.lastDeletedFile && this.project.projectService.lastDeletedFile.fileName; + for (var _i = 0, names_3 = names; _i < names_3.length; _i++) { + var name_52 = names_3[_i]; + var resolution = newResolutions[name_52]; + if (!resolution) { + var existingResolution = currentResolutionsInFile && currentResolutionsInFile[name_52]; + if (moduleResolutionIsValid(existingResolution)) { + resolution = existingResolution; + } + else { + newResolutions[name_52] = resolution = loader(name_52, containingFile, compilerOptions, this); + } + } + ts.Debug.assert(resolution !== undefined); + resolvedModules.push(getResult(resolution)); + } + cache.set(path, newResolutions); + return resolvedModules; + function moduleResolutionIsValid(resolution) { + if (!resolution) { + return false; + } + var result = getResult(resolution); + if (result) { + if (result.resolvedFileName && result.resolvedFileName === lastDeletedFileName) { + return false; + } + return true; + } + return resolution.failedLookupLocations.length === 0; + } + }; + LSHost.prototype.getProjectVersion = function () { + return this.project.getProjectVersion(); + }; + LSHost.prototype.getCompilationSettings = function () { + return this.compilationSettings; + }; + LSHost.prototype.useCaseSensitiveFileNames = function () { + return this.host.useCaseSensitiveFileNames; + }; + LSHost.prototype.getCancellationToken = function () { + return this.cancellationToken; + }; + LSHost.prototype.resolveTypeReferenceDirectives = function (typeDirectiveNames, containingFile) { + return this.resolveNamesWithLocalCache(typeDirectiveNames, containingFile, this.resolvedTypeReferenceDirectives, ts.resolveTypeReferenceDirective, function (m) { return m.resolvedTypeReferenceDirective; }); + }; + LSHost.prototype.resolveModuleNames = function (moduleNames, containingFile) { + return this.resolveNamesWithLocalCache(moduleNames, containingFile, this.resolvedModuleNames, this.resolveModuleName, function (m) { return m.resolvedModule; }); + }; + LSHost.prototype.getDefaultLibFileName = function () { + var nodeModuleBinDir = ts.getDirectoryPath(ts.normalizePath(this.host.getExecutingFilePath())); + return ts.combinePaths(nodeModuleBinDir, ts.getDefaultLibFileName(this.compilationSettings)); + }; + LSHost.prototype.getScriptSnapshot = function (filename) { + var scriptInfo = this.project.getScriptInfoLSHost(filename); + if (scriptInfo) { + return scriptInfo.snap(); + } + }; + LSHost.prototype.getScriptFileNames = function () { + return this.project.getRootFilesLSHost(); + }; + LSHost.prototype.getTypeRootsVersion = function () { + return this.project.typesVersion; + }; + LSHost.prototype.getScriptKind = function (fileName) { + var info = this.project.getScriptInfoLSHost(fileName); + return info && info.scriptKind; + }; + LSHost.prototype.getScriptVersion = function (filename) { + var info = this.project.getScriptInfoLSHost(filename); + return info && info.getLatestVersion(); + }; + LSHost.prototype.getCurrentDirectory = function () { + return this.host.getCurrentDirectory(); + }; + LSHost.prototype.resolvePath = function (path) { + return this.host.resolvePath(path); + }; + LSHost.prototype.fileExists = function (path) { + return this.host.fileExists(path); + }; + LSHost.prototype.readFile = function (fileName) { + return this.host.readFile(fileName); + }; + LSHost.prototype.directoryExists = function (path) { + return this.host.directoryExists(path); + }; + LSHost.prototype.readDirectory = function (path, extensions, exclude, include) { + return this.host.readDirectory(path, extensions, exclude, include); + }; + LSHost.prototype.getDirectories = function (path) { + return this.host.getDirectories(path); + }; + LSHost.prototype.notifyFileRemoved = function (info) { + this.resolvedModuleNames.remove(info.path); + this.resolvedTypeReferenceDirectives.remove(info.path); + }; + LSHost.prototype.setCompilationSettings = function (opt) { + this.compilationSettings = opt; + this.resolvedModuleNames.clear(); + this.resolvedTypeReferenceDirectives.clear(); + }; + return LSHost; + }()); + server.LSHost = LSHost; + })(server = ts.server || (ts.server = {})); +})(ts || (ts = {})); +var ts; +(function (ts) { + var server; + (function (server) { + server.nullTypingsInstaller = { + enqueueInstallTypingsRequest: function () { }, + attach: function (projectService) { }, + onProjectClosed: function (p) { }, + globalTypingsCacheLocation: undefined + }; + var TypingsCacheEntry = (function () { + function TypingsCacheEntry() { + } + return TypingsCacheEntry; + }()); + function setIsEqualTo(arr1, arr2) { + if (arr1 === arr2) { + return true; + } + if ((arr1 || server.emptyArray).length === 0 && (arr2 || server.emptyArray).length === 0) { + return true; + } + var set = ts.createMap(); + var unique = 0; + for (var _i = 0, arr1_1 = arr1; _i < arr1_1.length; _i++) { + var v = arr1_1[_i]; + if (set[v] !== true) { + set[v] = true; + unique++; + } + } + for (var _a = 0, arr2_1 = arr2; _a < arr2_1.length; _a++) { + var v = arr2_1[_a]; + if (!ts.hasProperty(set, v)) { + return false; + } + if (set[v] === true) { + set[v] = false; + unique--; + } + } + return unique === 0; + } + function typingOptionsChanged(opt1, opt2) { + return opt1.enableAutoDiscovery !== opt2.enableAutoDiscovery || + !setIsEqualTo(opt1.include, opt2.include) || + !setIsEqualTo(opt1.exclude, opt2.exclude); + } + function compilerOptionsChanged(opt1, opt2) { + return opt1.allowJs != opt2.allowJs; + } + function toTypingsArray(arr) { + arr.sort(); + return arr; + } + var TypingsCache = (function () { + function TypingsCache(installer) { + this.installer = installer; + this.perProjectCache = ts.createMap(); + } + TypingsCache.prototype.getTypingsForProject = function (project, forceRefresh) { + var typingOptions = project.getTypingOptions(); + if (!typingOptions || !typingOptions.enableAutoDiscovery) { + return server.emptyArray; + } + var entry = this.perProjectCache[project.getProjectName()]; + var result = entry ? entry.typings : server.emptyArray; + if (forceRefresh || !entry || typingOptionsChanged(typingOptions, entry.typingOptions) || compilerOptionsChanged(project.getCompilerOptions(), entry.compilerOptions)) { + this.perProjectCache[project.getProjectName()] = { + compilerOptions: project.getCompilerOptions(), + typingOptions: typingOptions, + typings: result, + poisoned: true + }; + this.installer.enqueueInstallTypingsRequest(project, typingOptions); + } + return result; + }; + TypingsCache.prototype.invalidateCachedTypingsForProject = function (project) { + var typingOptions = project.getTypingOptions(); + if (!typingOptions.enableAutoDiscovery) { + return; + } + this.installer.enqueueInstallTypingsRequest(project, typingOptions); + }; + TypingsCache.prototype.updateTypingsForProject = function (projectName, compilerOptions, typingOptions, newTypings) { + this.perProjectCache[projectName] = { + compilerOptions: compilerOptions, + typingOptions: typingOptions, + typings: toTypingsArray(newTypings), + poisoned: false + }; + }; + TypingsCache.prototype.onProjectClosed = function (project) { + delete this.perProjectCache[project.getProjectName()]; + this.installer.onProjectClosed(project); + }; + return TypingsCache; + }()); + server.TypingsCache = TypingsCache; + })(server = ts.server || (ts.server = {})); +})(ts || (ts = {})); +var ts; +(function (ts) { + var server; + (function (server) { + var crypto = require("crypto"); + function shouldEmitFile(scriptInfo) { + return !scriptInfo.hasMixedContent; + } + server.shouldEmitFile = shouldEmitFile; + var BuilderFileInfo = (function () { + function BuilderFileInfo(scriptInfo, project) { + this.scriptInfo = scriptInfo; + this.project = project; + } + BuilderFileInfo.prototype.isExternalModuleOrHasOnlyAmbientExternalModules = function () { + var sourceFile = this.getSourceFile(); + return ts.isExternalModule(sourceFile) || this.containsOnlyAmbientModules(sourceFile); + }; + BuilderFileInfo.prototype.containsOnlyAmbientModules = function (sourceFile) { + for (var _i = 0, _a = sourceFile.statements; _i < _a.length; _i++) { + var statement = _a[_i]; + if (statement.kind !== 225 || statement.name.kind !== 9) { + return false; + } + } + return true; + }; + BuilderFileInfo.prototype.computeHash = function (text) { + return crypto.createHash("md5") + .update(text) + .digest("base64"); + }; + BuilderFileInfo.prototype.getSourceFile = function () { + return this.project.getSourceFile(this.scriptInfo.path); + }; + BuilderFileInfo.prototype.updateShapeSignature = function () { + var sourceFile = this.getSourceFile(); + if (!sourceFile) { + return true; + } + var lastSignature = this.lastCheckedShapeSignature; + if (sourceFile.isDeclarationFile) { + this.lastCheckedShapeSignature = this.computeHash(sourceFile.text); + } + else { + var emitOutput = this.project.getFileEmitOutput(this.scriptInfo, true); + if (emitOutput.outputFiles && emitOutput.outputFiles.length > 0) { + this.lastCheckedShapeSignature = this.computeHash(emitOutput.outputFiles[0].text); + } + } + return !lastSignature || this.lastCheckedShapeSignature !== lastSignature; + }; + return BuilderFileInfo; + }()); + server.BuilderFileInfo = BuilderFileInfo; + var AbstractBuilder = (function () { + function AbstractBuilder(project, ctor) { + this.project = project; + this.ctor = ctor; + this.fileInfos = ts.createFileMap(); + } + AbstractBuilder.prototype.getFileInfo = function (path) { + return this.fileInfos.get(path); + }; + AbstractBuilder.prototype.getOrCreateFileInfo = function (path) { + var fileInfo = this.getFileInfo(path); + if (!fileInfo) { + var scriptInfo = this.project.getScriptInfo(path); + fileInfo = new this.ctor(scriptInfo, this.project); + this.setFileInfo(path, fileInfo); + } + return fileInfo; + }; + AbstractBuilder.prototype.getFileInfoPaths = function () { + return this.fileInfos.getKeys(); + }; + AbstractBuilder.prototype.setFileInfo = function (path, info) { + this.fileInfos.set(path, info); + }; + AbstractBuilder.prototype.removeFileInfo = function (path) { + this.fileInfos.remove(path); + }; + AbstractBuilder.prototype.forEachFileInfo = function (action) { + this.fileInfos.forEachValue(function (path, value) { return action(value); }); + }; + AbstractBuilder.prototype.emitFile = function (scriptInfo, writeFile) { + var fileInfo = this.getFileInfo(scriptInfo.path); + if (!fileInfo) { + return false; + } + var _a = this.project.getFileEmitOutput(fileInfo.scriptInfo, false), emitSkipped = _a.emitSkipped, outputFiles = _a.outputFiles; + if (!emitSkipped) { + var projectRootPath = this.project.getProjectRootPath(); + for (var _i = 0, outputFiles_1 = outputFiles; _i < outputFiles_1.length; _i++) { + var outputFile = outputFiles_1[_i]; + var outputFileAbsoluteFileName = ts.getNormalizedAbsolutePath(outputFile.name, projectRootPath ? projectRootPath : ts.getDirectoryPath(scriptInfo.fileName)); + writeFile(outputFileAbsoluteFileName, outputFile.text, outputFile.writeByteOrderMark); + } + } + return !emitSkipped; + }; + return AbstractBuilder; + }()); + var NonModuleBuilder = (function (_super) { + __extends(NonModuleBuilder, _super); + function NonModuleBuilder(project) { + var _this = _super.call(this, project, BuilderFileInfo) || this; + _this.project = project; + return _this; + } + NonModuleBuilder.prototype.onProjectUpdateGraph = function () { + }; + NonModuleBuilder.prototype.getFilesAffectedBy = function (scriptInfo) { + var info = this.getOrCreateFileInfo(scriptInfo.path); + var singleFileResult = scriptInfo.hasMixedContent ? [] : [scriptInfo.fileName]; + if (info.updateShapeSignature()) { + var options = this.project.getCompilerOptions(); + if (options && (options.out || options.outFile)) { + return singleFileResult; + } + return this.project.getAllEmittableFiles(); + } + return singleFileResult; + }; + return NonModuleBuilder; + }(AbstractBuilder)); + var ModuleBuilderFileInfo = (function (_super) { + __extends(ModuleBuilderFileInfo, _super); + function ModuleBuilderFileInfo() { + var _this = _super.apply(this, arguments) || this; + _this.references = []; + _this.referencedBy = []; + return _this; + } + ModuleBuilderFileInfo.compareFileInfos = function (lf, rf) { + var l = lf.scriptInfo.fileName; + var r = rf.scriptInfo.fileName; + return (l < r ? -1 : (l > r ? 1 : 0)); + }; + ; + ModuleBuilderFileInfo.addToReferenceList = function (array, fileInfo) { + if (array.length === 0) { + array.push(fileInfo); + return; + } + var insertIndex = ts.binarySearch(array, fileInfo, ModuleBuilderFileInfo.compareFileInfos); + if (insertIndex < 0) { + array.splice(~insertIndex, 0, fileInfo); + } + }; + ModuleBuilderFileInfo.removeFromReferenceList = function (array, fileInfo) { + if (!array || array.length === 0) { + return; + } + if (array[0] === fileInfo) { + array.splice(0, 1); + return; + } + var removeIndex = ts.binarySearch(array, fileInfo, ModuleBuilderFileInfo.compareFileInfos); + if (removeIndex >= 0) { + array.splice(removeIndex, 1); + } + }; + ModuleBuilderFileInfo.prototype.addReferencedBy = function (fileInfo) { + ModuleBuilderFileInfo.addToReferenceList(this.referencedBy, fileInfo); + }; + ModuleBuilderFileInfo.prototype.removeReferencedBy = function (fileInfo) { + ModuleBuilderFileInfo.removeFromReferenceList(this.referencedBy, fileInfo); + }; + ModuleBuilderFileInfo.prototype.removeFileReferences = function () { + for (var _i = 0, _a = this.references; _i < _a.length; _i++) { + var reference = _a[_i]; + reference.removeReferencedBy(this); + } + this.references = []; + }; + return ModuleBuilderFileInfo; + }(BuilderFileInfo)); + var ModuleBuilder = (function (_super) { + __extends(ModuleBuilder, _super); + function ModuleBuilder(project) { + var _this = _super.call(this, project, ModuleBuilderFileInfo) || this; + _this.project = project; + return _this; + } + ModuleBuilder.prototype.getReferencedFileInfos = function (fileInfo) { + var _this = this; + if (!fileInfo.isExternalModuleOrHasOnlyAmbientExternalModules()) { + return []; + } + var referencedFilePaths = this.project.getReferencedFiles(fileInfo.scriptInfo.path); + if (referencedFilePaths.length > 0) { + return ts.map(referencedFilePaths, function (f) { return _this.getOrCreateFileInfo(f); }).sort(ModuleBuilderFileInfo.compareFileInfos); + } + return []; + }; + ModuleBuilder.prototype.onProjectUpdateGraph = function () { + this.ensureProjectDependencyGraphUpToDate(); + }; + ModuleBuilder.prototype.ensureProjectDependencyGraphUpToDate = function () { + var _this = this; + if (!this.projectVersionForDependencyGraph || this.project.getProjectVersion() !== this.projectVersionForDependencyGraph) { + var currentScriptInfos = this.project.getScriptInfos(); + for (var _i = 0, currentScriptInfos_1 = currentScriptInfos; _i < currentScriptInfos_1.length; _i++) { + var scriptInfo = currentScriptInfos_1[_i]; + var fileInfo = this.getOrCreateFileInfo(scriptInfo.path); + this.updateFileReferences(fileInfo); + } + this.forEachFileInfo(function (fileInfo) { + if (!_this.project.containsScriptInfo(fileInfo.scriptInfo)) { + fileInfo.removeFileReferences(); + _this.removeFileInfo(fileInfo.scriptInfo.path); + } + }); + this.projectVersionForDependencyGraph = this.project.getProjectVersion(); + } + }; + ModuleBuilder.prototype.updateFileReferences = function (fileInfo) { + if (fileInfo.scriptVersionForReferences === fileInfo.scriptInfo.getLatestVersion()) { + return; + } + var newReferences = this.getReferencedFileInfos(fileInfo); + var oldReferences = fileInfo.references; + var oldIndex = 0; + var newIndex = 0; + while (oldIndex < oldReferences.length && newIndex < newReferences.length) { + var oldReference = oldReferences[oldIndex]; + var newReference = newReferences[newIndex]; + var compare = ModuleBuilderFileInfo.compareFileInfos(oldReference, newReference); + if (compare < 0) { + oldReference.removeReferencedBy(fileInfo); + oldIndex++; + } + else if (compare > 0) { + newReference.addReferencedBy(fileInfo); + newIndex++; + } + else { + oldIndex++; + newIndex++; + } + } + for (var i = oldIndex; i < oldReferences.length; i++) { + oldReferences[i].removeReferencedBy(fileInfo); + } + for (var i = newIndex; i < newReferences.length; i++) { + newReferences[i].addReferencedBy(fileInfo); + } + fileInfo.references = newReferences; + fileInfo.scriptVersionForReferences = fileInfo.scriptInfo.getLatestVersion(); + }; + ModuleBuilder.prototype.getFilesAffectedBy = function (scriptInfo) { + this.ensureProjectDependencyGraphUpToDate(); + var singleFileResult = scriptInfo.hasMixedContent ? [] : [scriptInfo.fileName]; + var fileInfo = this.getFileInfo(scriptInfo.path); + if (!fileInfo || !fileInfo.updateShapeSignature()) { + return singleFileResult; + } + if (!fileInfo.isExternalModuleOrHasOnlyAmbientExternalModules()) { + return this.project.getAllEmittableFiles(); + } + var options = this.project.getCompilerOptions(); + if (options && (options.isolatedModules || options.out || options.outFile)) { + return singleFileResult; + } + var queue = fileInfo.referencedBy.slice(0); + var fileNameSet = ts.createMap(); + fileNameSet[scriptInfo.fileName] = scriptInfo; + while (queue.length > 0) { + var processingFileInfo = queue.pop(); + if (processingFileInfo.updateShapeSignature() && processingFileInfo.referencedBy.length > 0) { + for (var _i = 0, _a = processingFileInfo.referencedBy; _i < _a.length; _i++) { + var potentialFileInfo = _a[_i]; + if (!fileNameSet[potentialFileInfo.scriptInfo.fileName]) { + queue.push(potentialFileInfo); + } } } + fileNameSet[processingFileInfo.scriptInfo.fileName] = processingFileInfo.scriptInfo; + } + var result = []; + for (var fileName in fileNameSet) { + if (shouldEmitFile(fileNameSet[fileName])) { + result.push(fileName); + } + } + return result; + }; + return ModuleBuilder; + }(AbstractBuilder)); + function createBuilder(project) { + var moduleKind = project.getCompilerOptions().module; + switch (moduleKind) { + case ts.ModuleKind.None: + return new NonModuleBuilder(project); + default: + return new ModuleBuilder(project); } } - } - function isArgumentOfElementAccessExpression(node) { - return node && - node.parent && - node.parent.kind === 173 && - node.parent.argumentExpression === node; - } - function getDefaultLibFilePath(options) { - if (typeof __dirname !== "undefined") { - return __dirname + ts.directorySeparator + ts.getDefaultLibFileName(options); - } - throw new Error("getDefaultLibFilePath is only supported when consumed as a node module. "); - } - ts.getDefaultLibFilePath = getDefaultLibFilePath; - function initializeServices() { - ts.objectAllocator = getServicesObjectAllocator(); - } - initializeServices(); + server.createBuilder = createBuilder; + })(server = ts.server || (ts.server = {})); })(ts || (ts = {})); var ts; (function (ts) { var server; (function (server) { - var spaceCache = []; - function generateSpaces(n) { - if (!spaceCache[n]) { - var strBuilder = ""; - for (var i = 0; i < n; i++) { - strBuilder += " "; + (function (ProjectKind) { + ProjectKind[ProjectKind["Inferred"] = 0] = "Inferred"; + ProjectKind[ProjectKind["Configured"] = 1] = "Configured"; + ProjectKind[ProjectKind["External"] = 2] = "External"; + })(server.ProjectKind || (server.ProjectKind = {})); + var ProjectKind = server.ProjectKind; + function remove(items, item) { + var index = items.indexOf(item); + if (index >= 0) { + items.splice(index, 1); + } + } + function countEachFileTypes(infos) { + var result = { js: 0, jsx: 0, ts: 0, tsx: 0, dts: 0 }; + for (var _i = 0, infos_1 = infos; _i < infos_1.length; _i++) { + var info = infos_1[_i]; + switch (info.scriptKind) { + case 1: + result.js += 1; + break; + case 2: + result.jsx += 1; + break; + case 3: + ts.fileExtensionIs(info.fileName, ".d.ts") + ? result.dts += 1 + : result.ts += 1; + break; + case 4: + result.tsx += 1; + break; } - spaceCache[n] = strBuilder; } - return spaceCache[n]; + return result; } - server.generateSpaces = generateSpaces; - function generateIndentString(n, editorOptions) { - if (editorOptions.ConvertTabsToSpaces) { - return generateSpaces(n); - } - else { - var result = ""; - for (var i = 0; i < Math.floor(n / editorOptions.TabSize); i++) { - result += "\t"; - } - for (var i = 0; i < n % editorOptions.TabSize; i++) { - result += " "; - } - return result; - } + function hasOneOrMoreJsAndNoTsFiles(project) { + var counts = countEachFileTypes(project.getScriptInfos()); + return counts.js > 0 && counts.ts === 0 && counts.tsx === 0; } - server.generateIndentString = generateIndentString; - function compareNumber(a, b) { - if (a < b) { - return -1; - } - else if (a === b) { - return 0; - } - else - return 1; + function allRootFilesAreJsOrDts(project) { + var counts = countEachFileTypes(project.getRootScriptInfos()); + return counts.ts === 0 && counts.tsx === 0; } - function compareFileStart(a, b) { - if (a.file < b.file) { - return -1; - } - else if (a.file == b.file) { - var n = compareNumber(a.start.line, b.start.line); - if (n === 0) { - return compareNumber(a.start.offset, b.start.offset); + server.allRootFilesAreJsOrDts = allRootFilesAreJsOrDts; + function allFilesAreJsOrDts(project) { + var counts = countEachFileTypes(project.getScriptInfos()); + return counts.ts === 0 && counts.tsx === 0; + } + server.allFilesAreJsOrDts = allFilesAreJsOrDts; + var Project = (function () { + function Project(projectKind, projectService, documentRegistry, hasExplicitListOfFiles, languageServiceEnabled, compilerOptions, compileOnSaveEnabled) { + this.projectKind = projectKind; + this.projectService = projectService; + this.documentRegistry = documentRegistry; + this.languageServiceEnabled = languageServiceEnabled; + this.compilerOptions = compilerOptions; + this.compileOnSaveEnabled = compileOnSaveEnabled; + this.rootFiles = []; + this.rootFilesMap = ts.createFileMap(); + this.lastReportedVersion = 0; + this.projectStructureVersion = 0; + this.projectStateVersion = 0; + this.typesVersion = 0; + if (!this.compilerOptions) { + this.compilerOptions = ts.getDefaultCompilerOptions(); + this.compilerOptions.allowNonTsExtensions = true; + this.compilerOptions.allowJs = true; + } + else if (hasExplicitListOfFiles) { + this.compilerOptions.allowNonTsExtensions = true; + } + if (languageServiceEnabled) { + this.enableLanguageService(); } - else - return n; - } - else { - return 1; + else { + this.disableLanguageService(); + } + this.builder = server.createBuilder(this); + this.markAsDirty(); } - } - function formatDiag(fileName, project, diag) { - return { - start: project.compilerService.host.positionToLineOffset(fileName, diag.start), - end: project.compilerService.host.positionToLineOffset(fileName, diag.start + diag.length), - text: ts.flattenDiagnosticMessageText(diag.messageText, "\n") + Project.prototype.isNonTsProject = function () { + this.updateGraph(); + return allFilesAreJsOrDts(this); }; - } - function formatConfigFileDiag(diag) { - return { - start: undefined, - end: undefined, - text: ts.flattenDiagnosticMessageText(diag.messageText, "\n") + Project.prototype.isJsOnlyProject = function () { + this.updateGraph(); + return hasOneOrMoreJsAndNoTsFiles(this); }; - } - function allEditsBeforePos(edits, pos) { - for (var i = 0, len = edits.length; i < len; i++) { - if (ts.textSpanEnd(edits[i].span) >= pos) { - return false; + Project.prototype.getProjectErrors = function () { + return this.projectErrors; + }; + Project.prototype.getLanguageService = function (ensureSynchronized) { + if (ensureSynchronized === void 0) { ensureSynchronized = true; } + if (ensureSynchronized) { + this.updateGraph(); } - } - return true; - } - var CommandNames; - (function (CommandNames) { - CommandNames.Brace = "brace"; - CommandNames.Change = "change"; - CommandNames.Close = "close"; - CommandNames.Completions = "completions"; - CommandNames.CompletionDetails = "completionEntryDetails"; - CommandNames.Configure = "configure"; - CommandNames.Definition = "definition"; - CommandNames.Exit = "exit"; - CommandNames.Format = "format"; - CommandNames.Formatonkey = "formatonkey"; - CommandNames.Geterr = "geterr"; - CommandNames.GeterrForProject = "geterrForProject"; - CommandNames.Implementation = "implementation"; - CommandNames.SemanticDiagnosticsSync = "semanticDiagnosticsSync"; - CommandNames.SyntacticDiagnosticsSync = "syntacticDiagnosticsSync"; - CommandNames.NavBar = "navbar"; - CommandNames.Navto = "navto"; - CommandNames.Occurrences = "occurrences"; - CommandNames.DocumentHighlights = "documentHighlights"; - CommandNames.Open = "open"; - CommandNames.Quickinfo = "quickinfo"; - CommandNames.References = "references"; - CommandNames.Reload = "reload"; - CommandNames.Rename = "rename"; - CommandNames.Saveto = "saveto"; - CommandNames.SignatureHelp = "signatureHelp"; - CommandNames.TypeDefinition = "typeDefinition"; - CommandNames.ProjectInfo = "projectInfo"; - CommandNames.ReloadProjects = "reloadProjects"; - CommandNames.Unknown = "unknown"; - })(CommandNames = server.CommandNames || (server.CommandNames = {})); - var Errors; - (function (Errors) { - Errors.NoProject = new Error("No Project."); - Errors.ProjectLanguageServiceDisabled = new Error("The project's language service is disabled."); - })(Errors || (Errors = {})); - var Session = (function () { - function Session(host, byteLength, hrtime, logger) { - var _this = this; - this.host = host; - this.byteLength = byteLength; - this.hrtime = hrtime; - this.logger = logger; - this.changeSeq = 0; - this.handlers = ts.createMap((_a = {}, - _a[CommandNames.Exit] = function () { - _this.exit(); - return { responseRequired: false }; - }, - _a[CommandNames.Definition] = function (request) { - var defArgs = request.arguments; - return { response: _this.getDefinition(defArgs.line, defArgs.offset, defArgs.file), responseRequired: true }; - }, - _a[CommandNames.TypeDefinition] = function (request) { - var defArgs = request.arguments; - return { response: _this.getTypeDefinition(defArgs.line, defArgs.offset, defArgs.file), responseRequired: true }; - }, - _a[CommandNames.Implementation] = function (request) { - var implArgs = request.arguments; - return { response: _this.getImplementation(implArgs.line, implArgs.offset, implArgs.file), responseRequired: true }; - }, - _a[CommandNames.References] = function (request) { - var defArgs = request.arguments; - return { response: _this.getReferences(defArgs.line, defArgs.offset, defArgs.file), responseRequired: true }; - }, - _a[CommandNames.Rename] = function (request) { - var renameArgs = request.arguments; - return { response: _this.getRenameLocations(renameArgs.line, renameArgs.offset, renameArgs.file, renameArgs.findInComments, renameArgs.findInStrings), responseRequired: true }; - }, - _a[CommandNames.Open] = function (request) { - var openArgs = request.arguments; - var scriptKind; - switch (openArgs.scriptKindName) { - case "TS": - scriptKind = 3; - break; - case "JS": - scriptKind = 1; - break; - case "TSX": - scriptKind = 4; - break; - case "JSX": - scriptKind = 2; - break; + return this.languageService; + }; + Project.prototype.getCompileOnSaveAffectedFileList = function (scriptInfo) { + if (!this.languageServiceEnabled) { + return []; + } + this.updateGraph(); + return this.builder.getFilesAffectedBy(scriptInfo); + }; + Project.prototype.getProjectVersion = function () { + return this.projectStateVersion.toString(); + }; + Project.prototype.enableLanguageService = function () { + var lsHost = new server.LSHost(this.projectService.host, this, this.projectService.cancellationToken); + lsHost.setCompilationSettings(this.compilerOptions); + this.languageService = ts.createLanguageService(lsHost, this.documentRegistry); + this.lsHost = lsHost; + this.languageServiceEnabled = true; + }; + Project.prototype.disableLanguageService = function () { + this.languageService = server.nullLanguageService; + this.lsHost = server.nullLanguageServiceHost; + this.languageServiceEnabled = false; + }; + Project.prototype.getSourceFile = function (path) { + if (!this.program) { + return undefined; + } + return this.program.getSourceFileByPath(path); + }; + Project.prototype.updateTypes = function () { + this.typesVersion++; + this.markAsDirty(); + this.updateGraph(); + }; + Project.prototype.close = function () { + if (this.program) { + for (var _i = 0, _a = this.program.getSourceFiles(); _i < _a.length; _i++) { + var f = _a[_i]; + var info = this.projectService.getScriptInfo(f.fileName); + info.detachFromProject(this); + } + } + else { + for (var _b = 0, _c = this.rootFiles; _b < _c.length; _b++) { + var root = _c[_b]; + root.detachFromProject(this); + } + } + this.rootFiles = undefined; + this.rootFilesMap = undefined; + this.program = undefined; + this.languageService.dispose(); + }; + Project.prototype.getCompilerOptions = function () { + return this.compilerOptions; + }; + Project.prototype.hasRoots = function () { + return this.rootFiles && this.rootFiles.length > 0; + }; + Project.prototype.getRootFiles = function () { + return this.rootFiles && this.rootFiles.map(function (info) { return info.fileName; }); + }; + Project.prototype.getRootFilesLSHost = function () { + var result = []; + if (this.rootFiles) { + for (var _i = 0, _a = this.rootFiles; _i < _a.length; _i++) { + var f = _a[_i]; + result.push(f.fileName); + } + if (this.typingFiles) { + for (var _b = 0, _c = this.typingFiles; _b < _c.length; _b++) { + var f = _c[_b]; + result.push(f); } - _this.openClientFile(openArgs.file, openArgs.fileContent, scriptKind); - return { responseRequired: false }; - }, - _a[CommandNames.Quickinfo] = function (request) { - var quickinfoArgs = request.arguments; - return { response: _this.getQuickInfo(quickinfoArgs.line, quickinfoArgs.offset, quickinfoArgs.file), responseRequired: true }; - }, - _a[CommandNames.Format] = function (request) { - var formatArgs = request.arguments; - return { response: _this.getFormattingEditsForRange(formatArgs.line, formatArgs.offset, formatArgs.endLine, formatArgs.endOffset, formatArgs.file), responseRequired: true }; - }, - _a[CommandNames.Formatonkey] = function (request) { - var formatOnKeyArgs = request.arguments; - return { response: _this.getFormattingEditsAfterKeystroke(formatOnKeyArgs.line, formatOnKeyArgs.offset, formatOnKeyArgs.key, formatOnKeyArgs.file), responseRequired: true }; - }, - _a[CommandNames.Completions] = function (request) { - var completionsArgs = request.arguments; - return { response: _this.getCompletions(completionsArgs.line, completionsArgs.offset, completionsArgs.prefix, completionsArgs.file), responseRequired: true }; - }, - _a[CommandNames.CompletionDetails] = function (request) { - var completionDetailsArgs = request.arguments; - return { - response: _this.getCompletionEntryDetails(completionDetailsArgs.line, completionDetailsArgs.offset, completionDetailsArgs.entryNames, completionDetailsArgs.file), responseRequired: true - }; - }, - _a[CommandNames.SignatureHelp] = function (request) { - var signatureHelpArgs = request.arguments; - return { response: _this.getSignatureHelpItems(signatureHelpArgs.line, signatureHelpArgs.offset, signatureHelpArgs.file), responseRequired: true }; - }, - _a[CommandNames.SemanticDiagnosticsSync] = function (request) { - return _this.requiredResponse(_this.getSemanticDiagnosticsSync(request.arguments)); - }, - _a[CommandNames.SyntacticDiagnosticsSync] = function (request) { - return _this.requiredResponse(_this.getSyntacticDiagnosticsSync(request.arguments)); - }, - _a[CommandNames.Geterr] = function (request) { - var geterrArgs = request.arguments; - return { response: _this.getDiagnostics(geterrArgs.delay, geterrArgs.files), responseRequired: false }; - }, - _a[CommandNames.GeterrForProject] = function (request) { - var _a = request.arguments, file = _a.file, delay = _a.delay; - return { response: _this.getDiagnosticsForProject(delay, file), responseRequired: false }; - }, - _a[CommandNames.Change] = function (request) { - var changeArgs = request.arguments; - _this.change(changeArgs.line, changeArgs.offset, changeArgs.endLine, changeArgs.endOffset, changeArgs.insertString, changeArgs.file); - return { responseRequired: false }; - }, - _a[CommandNames.Configure] = function (request) { - var configureArgs = request.arguments; - _this.projectService.setHostConfiguration(configureArgs); - _this.output(undefined, CommandNames.Configure, request.seq); - return { responseRequired: false }; - }, - _a[CommandNames.Reload] = function (request) { - var reloadArgs = request.arguments; - _this.reload(reloadArgs.file, reloadArgs.tmpfile, request.seq); - return { response: { reloadFinished: true }, responseRequired: true }; - }, - _a[CommandNames.Saveto] = function (request) { - var savetoArgs = request.arguments; - _this.saveToTmp(savetoArgs.file, savetoArgs.tmpfile); - return { responseRequired: false }; - }, - _a[CommandNames.Close] = function (request) { - var closeArgs = request.arguments; - _this.closeClientFile(closeArgs.file); - return { responseRequired: false }; - }, - _a[CommandNames.Navto] = function (request) { - var navtoArgs = request.arguments; - return { response: _this.getNavigateToItems(navtoArgs.searchValue, navtoArgs.file, navtoArgs.maxResultCount, navtoArgs.currentFileOnly), responseRequired: true }; - }, - _a[CommandNames.Brace] = function (request) { - var braceArguments = request.arguments; - return { response: _this.getBraceMatching(braceArguments.line, braceArguments.offset, braceArguments.file), responseRequired: true }; - }, - _a[CommandNames.NavBar] = function (request) { - var navBarArgs = request.arguments; - return { response: _this.getNavigationBarItems(navBarArgs.file), responseRequired: true }; - }, - _a[CommandNames.Occurrences] = function (request) { - var _a = request.arguments, line = _a.line, offset = _a.offset, fileName = _a.file; - return { response: _this.getOccurrences(line, offset, fileName), responseRequired: true }; - }, - _a[CommandNames.DocumentHighlights] = function (request) { - var _a = request.arguments, line = _a.line, offset = _a.offset, fileName = _a.file, filesToSearch = _a.filesToSearch; - return { response: _this.getDocumentHighlights(line, offset, fileName, filesToSearch), responseRequired: true }; - }, - _a[CommandNames.ProjectInfo] = function (request) { - var _a = request.arguments, file = _a.file, needFileNameList = _a.needFileNameList; - return { response: _this.getProjectInfo(file, needFileNameList), responseRequired: true }; - }, - _a[CommandNames.ReloadProjects] = function (request) { - _this.reloadProjects(); - return { responseRequired: false }; - }, - _a)); - this.projectService = - new server.ProjectService(host, logger, function (event) { - _this.handleEvent(event); - }); - var _a; - } - Session.prototype.handleEvent = function (event) { + } + } + return result; + }; + Project.prototype.getRootScriptInfos = function () { + return this.rootFiles; + }; + Project.prototype.getScriptInfos = function () { var _this = this; - switch (event.eventName) { - case "context": - var _a = event.data, project = _a.project, fileName = _a.fileName; - this.projectService.log("got context event, updating diagnostics for" + fileName, "Info"); - this.updateErrorCheck([{ fileName: fileName, project: project }], this.changeSeq, function (n) { return n === _this.changeSeq; }, 100); - break; - case "configFileDiag": - var _b = event.data, triggerFile = _b.triggerFile, configFileName = _b.configFileName, diagnostics = _b.diagnostics; - this.configFileDiagnosticEvent(triggerFile, configFileName, diagnostics); + return ts.map(this.program.getSourceFiles(), function (sourceFile) { + var scriptInfo = _this.projectService.getScriptInfoForPath(sourceFile.path); + if (!scriptInfo) { + ts.Debug.assert(false, "scriptInfo for a file '" + sourceFile.fileName + "' is missing."); + } + return scriptInfo; + }); + }; + Project.prototype.getFileEmitOutput = function (info, emitOnlyDtsFiles) { + if (!this.languageServiceEnabled) { + return undefined; } + return this.getLanguageService().getEmitOutput(info.fileName, emitOnlyDtsFiles); }; - Session.prototype.logError = function (err, cmd) { - var typedErr = err; - var msg = "Exception on executing command " + cmd; - if (typedErr.message) { - msg += ":\n" + typedErr.message; - if (typedErr.stack) { - msg += "\n" + typedErr.stack; + Project.prototype.getFileNames = function () { + if (!this.program) { + return []; + } + if (!this.languageServiceEnabled) { + var rootFiles = this.getRootFiles(); + if (this.compilerOptions) { + var defaultLibrary = ts.getDefaultLibFilePath(this.compilerOptions); + if (defaultLibrary) { + (rootFiles || (rootFiles = [])).push(server.asNormalizedPath(defaultLibrary)); + } } + return rootFiles; } - this.projectService.log(msg); + var sourceFiles = this.program.getSourceFiles(); + return sourceFiles.map(function (sourceFile) { return server.asNormalizedPath(sourceFile.fileName); }); }; - Session.prototype.sendLineToClient = function (line) { - this.host.write(line + this.host.newLine); + Project.prototype.getAllEmittableFiles = function () { + if (!this.languageServiceEnabled) { + return []; + } + var defaultLibraryFileName = ts.getDefaultLibFileName(this.compilerOptions); + var infos = this.getScriptInfos(); + var result = []; + for (var _i = 0, infos_2 = infos; _i < infos_2.length; _i++) { + var info = infos_2[_i]; + if (ts.getBaseFileName(info.fileName) !== defaultLibraryFileName && server.shouldEmitFile(info)) { + result.push(info.fileName); + } + } + return result; }; - Session.prototype.send = function (msg) { - var json = JSON.stringify(msg); - if (this.logger.isVerbose()) { - this.logger.info(msg.type + ": " + json); + Project.prototype.containsScriptInfo = function (info) { + return this.isRoot(info) || (this.program && this.program.getSourceFileByPath(info.path) !== undefined); + }; + Project.prototype.containsFile = function (filename, requireOpen) { + var info = this.projectService.getScriptInfoForNormalizedPath(filename); + if (info && (info.isOpen || !requireOpen)) { + return this.containsScriptInfo(info); } - this.sendLineToClient("Content-Length: " + (1 + this.byteLength(json, "utf8")) + - "\r\n\r\n" + json); }; - Session.prototype.configFileDiagnosticEvent = function (triggerFile, configFile, diagnostics) { - var bakedDiags = ts.map(diagnostics, formatConfigFileDiag); - var ev = { - seq: 0, - type: "event", - event: "configFileDiag", - body: { - triggerFile: triggerFile, - configFile: configFile, - diagnostics: bakedDiags + Project.prototype.isRoot = function (info) { + return this.rootFilesMap && this.rootFilesMap.contains(info.path); + }; + Project.prototype.addRoot = function (info) { + if (!this.isRoot(info)) { + this.rootFiles.push(info); + this.rootFilesMap.set(info.path, info); + info.attachToProject(this); + this.markAsDirty(); + } + }; + Project.prototype.removeFile = function (info, detachFromProject) { + if (detachFromProject === void 0) { detachFromProject = true; } + this.removeRootFileIfNecessary(info); + this.lsHost.notifyFileRemoved(info); + if (detachFromProject) { + info.detachFromProject(this); + } + this.markAsDirty(); + }; + Project.prototype.markAsDirty = function () { + this.projectStateVersion++; + }; + Project.prototype.updateGraph = function () { + if (!this.languageServiceEnabled) { + return true; + } + var hasChanges = this.updateGraphWorker(); + var cachedTypings = this.projectService.typingsCache.getTypingsForProject(this, hasChanges); + if (this.setTypings(cachedTypings)) { + hasChanges = this.updateGraphWorker() || hasChanges; + } + if (hasChanges) { + this.projectStructureVersion++; + } + return !hasChanges; + }; + Project.prototype.setTypings = function (typings) { + if (ts.arrayIsEqualTo(this.typingFiles, typings)) { + return false; + } + this.typingFiles = typings; + this.markAsDirty(); + return true; + }; + Project.prototype.updateGraphWorker = function () { + var oldProgram = this.program; + this.program = this.languageService.getProgram(); + var hasChanges = false; + if (!oldProgram || (this.program !== oldProgram && !oldProgram.structureIsReused)) { + hasChanges = true; + if (oldProgram) { + for (var _i = 0, _a = oldProgram.getSourceFiles(); _i < _a.length; _i++) { + var f = _a[_i]; + if (this.program.getSourceFileByPath(f.path)) { + continue; + } + var scriptInfoToDetach = this.projectService.getScriptInfo(f.fileName); + if (scriptInfoToDetach) { + scriptInfoToDetach.detachFromProject(this); + } + } } - }; - this.send(ev); + } + this.builder.onProjectUpdateGraph(); + return hasChanges; }; - Session.prototype.event = function (info, eventName) { - var ev = { - seq: 0, - type: "event", - event: eventName, - body: info - }; - this.send(ev); + Project.prototype.getScriptInfoLSHost = function (fileName) { + var scriptInfo = this.projectService.getOrCreateScriptInfo(fileName, false); + if (scriptInfo) { + scriptInfo.attachToProject(this); + } + return scriptInfo; }; - Session.prototype.response = function (info, cmdName, reqSeq, errorMsg) { - if (reqSeq === void 0) { reqSeq = 0; } - var res = { - seq: 0, - type: "response", - command: cmdName, - request_seq: reqSeq, - success: !errorMsg + Project.prototype.getScriptInfoForNormalizedPath = function (fileName) { + var scriptInfo = this.projectService.getOrCreateScriptInfoForNormalizedPath(fileName, false); + if (scriptInfo && !scriptInfo.isAttached(this)) { + return server.Errors.ThrowProjectDoesNotContainDocument(fileName, this); + } + return scriptInfo; + }; + Project.prototype.getScriptInfo = function (uncheckedFileName) { + return this.getScriptInfoForNormalizedPath(server.toNormalizedPath(uncheckedFileName)); + }; + Project.prototype.filesToString = function () { + if (!this.program) { + return ""; + } + var strBuilder = ""; + for (var _i = 0, _a = this.program.getSourceFiles(); _i < _a.length; _i++) { + var file = _a[_i]; + strBuilder += file.fileName + "\n"; + } + return strBuilder; + }; + Project.prototype.setCompilerOptions = function (compilerOptions) { + if (compilerOptions) { + if (this.projectKind === ProjectKind.Inferred) { + compilerOptions.allowJs = true; + } + compilerOptions.allowNonTsExtensions = true; + this.compilerOptions = compilerOptions; + this.lsHost.setCompilationSettings(compilerOptions); + this.markAsDirty(); + } + }; + Project.prototype.reloadScript = function (filename) { + var script = this.projectService.getScriptInfoForNormalizedPath(filename); + if (script) { + ts.Debug.assert(script.isAttached(this)); + script.reloadFromFile(); + return true; + } + return false; + }; + Project.prototype.getChangesSinceVersion = function (lastKnownVersion) { + this.updateGraph(); + var info = { + projectName: this.getProjectName(), + version: this.projectStructureVersion, + isInferred: this.projectKind === ProjectKind.Inferred, + options: this.getCompilerOptions() }; - if (!errorMsg) { - res.body = info; + if (this.lastReportedFileNames && lastKnownVersion === this.lastReportedVersion) { + if (this.projectStructureVersion == this.lastReportedVersion) { + return { info: info, projectErrors: this.projectErrors }; + } + var lastReportedFileNames = this.lastReportedFileNames; + var currentFiles = ts.arrayToMap(this.getFileNames(), function (x) { return x; }); + var added = []; + var removed = []; + for (var id in currentFiles) { + if (!ts.hasProperty(lastReportedFileNames, id)) { + added.push(id); + } + } + for (var id in lastReportedFileNames) { + if (!ts.hasProperty(currentFiles, id)) { + removed.push(id); + } + } + this.lastReportedFileNames = currentFiles; + this.lastReportedVersion = this.projectStructureVersion; + return { info: info, changes: { added: added, removed: removed }, projectErrors: this.projectErrors }; } else { - res.message = errorMsg; + var projectFileNames = this.getFileNames(); + this.lastReportedFileNames = ts.arrayToMap(projectFileNames, function (x) { return x; }); + this.lastReportedVersion = this.projectStructureVersion; + return { info: info, files: projectFileNames, projectErrors: this.projectErrors }; } - this.send(res); }; - Session.prototype.output = function (body, commandName, requestSequence, errorMessage) { - if (requestSequence === void 0) { requestSequence = 0; } - this.response(body, commandName, requestSequence, errorMessage); + Project.prototype.getReferencedFiles = function (path) { + var _this = this; + if (!this.languageServiceEnabled) { + return []; + } + var sourceFile = this.getSourceFile(path); + if (!sourceFile) { + return []; + } + var referencedFiles = ts.createMap(); + if (sourceFile.imports && sourceFile.imports.length > 0) { + var checker = this.program.getTypeChecker(); + for (var _i = 0, _a = sourceFile.imports; _i < _a.length; _i++) { + var importName = _a[_i]; + var symbol = checker.getSymbolAtLocation(importName); + if (symbol && symbol.declarations && symbol.declarations[0]) { + var declarationSourceFile = symbol.declarations[0].getSourceFile(); + if (declarationSourceFile) { + referencedFiles[declarationSourceFile.path] = true; + } + } + } + } + var currentDirectory = ts.getDirectoryPath(path); + var getCanonicalFileName = ts.createGetCanonicalFileName(this.projectService.host.useCaseSensitiveFileNames); + if (sourceFile.referencedFiles && sourceFile.referencedFiles.length > 0) { + for (var _b = 0, _c = sourceFile.referencedFiles; _b < _c.length; _b++) { + var referencedFile = _c[_b]; + var referencedPath = ts.toPath(referencedFile.fileName, currentDirectory, getCanonicalFileName); + referencedFiles[referencedPath] = true; + } + } + if (sourceFile.resolvedTypeReferenceDirectiveNames) { + for (var typeName in sourceFile.resolvedTypeReferenceDirectiveNames) { + var resolvedTypeReferenceDirective = sourceFile.resolvedTypeReferenceDirectiveNames[typeName]; + if (!resolvedTypeReferenceDirective) { + continue; + } + var fileName = resolvedTypeReferenceDirective.resolvedFileName; + var typeFilePath = ts.toPath(fileName, currentDirectory, getCanonicalFileName); + referencedFiles[typeFilePath] = true; + } + } + var allFileNames = ts.map(Object.keys(referencedFiles), function (key) { return key; }); + return ts.filter(allFileNames, function (file) { return _this.projectService.host.fileExists(file); }); + }; + Project.prototype.removeRootFileIfNecessary = function (info) { + if (this.isRoot(info)) { + remove(this.rootFiles, info); + this.rootFilesMap.remove(info.path); + } }; - Session.prototype.semanticCheck = function (file, project) { - try { - var diags = project.compilerService.languageService.getSemanticDiagnostics(file); - if (diags) { - var bakedDiags = diags.map(function (diag) { return formatDiag(file, project, diag); }); - this.event({ file: file, diagnostics: bakedDiags }, "semanticDiag"); + return Project; + }()); + server.Project = Project; + var InferredProject = (function (_super) { + __extends(InferredProject, _super); + function InferredProject(projectService, documentRegistry, languageServiceEnabled, compilerOptions, compileOnSaveEnabled) { + var _this = _super.call(this, ProjectKind.Inferred, projectService, documentRegistry, undefined, languageServiceEnabled, compilerOptions, compileOnSaveEnabled) || this; + _this.compileOnSaveEnabled = compileOnSaveEnabled; + _this.directoriesWatchedForTsconfig = []; + _this.inferredProjectName = server.makeInferredProjectName(InferredProject.NextId); + InferredProject.NextId++; + return _this; + } + InferredProject.prototype.getProjectName = function () { + return this.inferredProjectName; + }; + InferredProject.prototype.getProjectRootPath = function () { + if (this.projectService.useSingleInferredProject) { + return undefined; + } + var rootFiles = this.getRootFiles(); + return ts.getDirectoryPath(rootFiles[0]); + }; + InferredProject.prototype.close = function () { + _super.prototype.close.call(this); + for (var _i = 0, _a = this.directoriesWatchedForTsconfig; _i < _a.length; _i++) { + var directory = _a[_i]; + this.projectService.stopWatchingDirectory(directory); + } + }; + InferredProject.prototype.getTypingOptions = function () { + return { + enableAutoDiscovery: allRootFilesAreJsOrDts(this), + include: [], + exclude: [] + }; + }; + return InferredProject; + }(Project)); + InferredProject.NextId = 1; + server.InferredProject = InferredProject; + var ConfiguredProject = (function (_super) { + __extends(ConfiguredProject, _super); + function ConfiguredProject(configFileName, projectService, documentRegistry, hasExplicitListOfFiles, compilerOptions, wildcardDirectories, languageServiceEnabled, compileOnSaveEnabled) { + var _this = _super.call(this, ProjectKind.Configured, projectService, documentRegistry, hasExplicitListOfFiles, languageServiceEnabled, compilerOptions, compileOnSaveEnabled) || this; + _this.configFileName = configFileName; + _this.wildcardDirectories = wildcardDirectories; + _this.compileOnSaveEnabled = compileOnSaveEnabled; + _this.openRefCount = 0; + return _this; + } + ConfiguredProject.prototype.getProjectRootPath = function () { + return ts.getDirectoryPath(this.configFileName); + }; + ConfiguredProject.prototype.setProjectErrors = function (projectErrors) { + this.projectErrors = projectErrors; + }; + ConfiguredProject.prototype.setTypingOptions = function (newTypingOptions) { + this.typingOptions = newTypingOptions; + }; + ConfiguredProject.prototype.getTypingOptions = function () { + return this.typingOptions; + }; + ConfiguredProject.prototype.getProjectName = function () { + return this.configFileName; + }; + ConfiguredProject.prototype.watchConfigFile = function (callback) { + var _this = this; + this.projectFileWatcher = this.projectService.host.watchFile(this.configFileName, function (_) { return callback(_this); }); + }; + ConfiguredProject.prototype.watchTypeRoots = function (callback) { + var _this = this; + var roots = this.getEffectiveTypeRoots(); + var watchers = []; + for (var _i = 0, roots_1 = roots; _i < roots_1.length; _i++) { + var root = roots_1[_i]; + this.projectService.logger.info("Add type root watcher for: " + root); + watchers.push(this.projectService.host.watchDirectory(root, function (path) { return callback(_this, path); }, false)); + } + this.typeRootsWatchers = watchers; + }; + ConfiguredProject.prototype.watchConfigDirectory = function (callback) { + var _this = this; + if (this.directoryWatcher) { + return; + } + var directoryToWatch = ts.getDirectoryPath(this.configFileName); + this.projectService.logger.info("Add recursive watcher for: " + directoryToWatch); + this.directoryWatcher = this.projectService.host.watchDirectory(directoryToWatch, function (path) { return callback(_this, path); }, true); + }; + ConfiguredProject.prototype.watchWildcards = function (callback) { + var _this = this; + if (!this.wildcardDirectories) { + return; + } + var configDirectoryPath = ts.getDirectoryPath(this.configFileName); + this.directoriesWatchedForWildcards = ts.reduceProperties(this.wildcardDirectories, function (watchers, flag, directory) { + if (ts.comparePaths(configDirectoryPath, directory, ".", !_this.projectService.host.useCaseSensitiveFileNames) !== 0) { + var recursive = (flag & 1) !== 0; + _this.projectService.logger.info("Add " + (recursive ? "recursive " : "") + "watcher for: " + directory); + watchers[directory] = _this.projectService.host.watchDirectory(directory, function (path) { return callback(_this, path); }, recursive); + } + return watchers; + }, {}); + }; + ConfiguredProject.prototype.stopWatchingDirectory = function () { + if (this.directoryWatcher) { + this.directoryWatcher.close(); + this.directoryWatcher = undefined; + } + }; + ConfiguredProject.prototype.close = function () { + _super.prototype.close.call(this); + if (this.projectFileWatcher) { + this.projectFileWatcher.close(); + } + if (this.typeRootsWatchers) { + for (var _i = 0, _a = this.typeRootsWatchers; _i < _a.length; _i++) { + var watcher = _a[_i]; + watcher.close(); } + this.typeRootsWatchers = undefined; } - catch (err) { - this.logError(err, "semantic check"); + for (var id in this.directoriesWatchedForWildcards) { + this.directoriesWatchedForWildcards[id].close(); } + this.directoriesWatchedForWildcards = undefined; + this.stopWatchingDirectory(); }; - Session.prototype.syntacticCheck = function (file, project) { - try { - var diags = project.compilerService.languageService.getSyntacticDiagnostics(file); - if (diags) { - var bakedDiags = diags.map(function (diag) { return formatDiag(file, project, diag); }); - this.event({ file: file, diagnostics: bakedDiags }, "syntaxDiag"); + ConfiguredProject.prototype.addOpenRef = function () { + this.openRefCount++; + }; + ConfiguredProject.prototype.deleteOpenRef = function () { + this.openRefCount--; + return this.openRefCount; + }; + ConfiguredProject.prototype.getEffectiveTypeRoots = function () { + return ts.getEffectiveTypeRoots(this.getCompilerOptions(), this.projectService.host) || []; + }; + return ConfiguredProject; + }(Project)); + server.ConfiguredProject = ConfiguredProject; + var ExternalProject = (function (_super) { + __extends(ExternalProject, _super); + function ExternalProject(externalProjectName, projectService, documentRegistry, compilerOptions, languageServiceEnabled, compileOnSaveEnabled, projectFilePath) { + var _this = _super.call(this, ProjectKind.External, projectService, documentRegistry, true, languageServiceEnabled, compilerOptions, compileOnSaveEnabled) || this; + _this.externalProjectName = externalProjectName; + _this.compileOnSaveEnabled = compileOnSaveEnabled; + _this.projectFilePath = projectFilePath; + return _this; + } + ExternalProject.prototype.getProjectRootPath = function () { + if (this.projectFilePath) { + return ts.getDirectoryPath(this.projectFilePath); + } + return ts.getDirectoryPath(ts.normalizeSlashes(this.externalProjectName)); + }; + ExternalProject.prototype.getTypingOptions = function () { + return this.typingOptions; + }; + ExternalProject.prototype.setProjectErrors = function (projectErrors) { + this.projectErrors = projectErrors; + }; + ExternalProject.prototype.setTypingOptions = function (newTypingOptions) { + if (!newTypingOptions) { + newTypingOptions = { + enableAutoDiscovery: allRootFilesAreJsOrDts(this), + include: [], + exclude: [] + }; + } + else { + if (newTypingOptions.enableAutoDiscovery === undefined) { + newTypingOptions.enableAutoDiscovery = allRootFilesAreJsOrDts(this); + } + if (!newTypingOptions.include) { + newTypingOptions.include = []; + } + if (!newTypingOptions.exclude) { + newTypingOptions.exclude = []; + } + } + this.typingOptions = newTypingOptions; + }; + ExternalProject.prototype.getProjectName = function () { + return this.externalProjectName; + }; + return ExternalProject; + }(Project)); + server.ExternalProject = ExternalProject; + })(server = ts.server || (ts.server = {})); +})(ts || (ts = {})); +var ts; +(function (ts) { + var server; + (function (server) { + server.maxProgramSizeForNonTsFiles = 20 * 1024 * 1024; + function combineProjectOutput(projects, action, comparer, areEqual) { + var result = projects.reduce(function (previous, current) { return ts.concatenate(previous, action(current)); }, []).sort(comparer); + return projects.length > 1 ? ts.deduplicate(result, areEqual) : result; + } + server.combineProjectOutput = combineProjectOutput; + var fileNamePropertyReader = { + getFileName: function (x) { return x; }, + getScriptKind: function (_) { return undefined; }, + hasMixedContent: function (_) { return false; } + }; + var externalFilePropertyReader = { + getFileName: function (x) { return x.fileName; }, + getScriptKind: function (x) { return x.scriptKind; }, + hasMixedContent: function (x) { return x.hasMixedContent; } + }; + function findProjectByName(projectName, projects) { + for (var _i = 0, projects_1 = projects; _i < projects_1.length; _i++) { + var proj = projects_1[_i]; + if (proj.getProjectName() === projectName) { + return proj; + } + } + } + function createFileNotFoundDiagnostic(fileName) { + return ts.createCompilerDiagnostic(ts.Diagnostics.File_0_not_found, fileName); + } + function isRootFileInInferredProject(info) { + if (info.containingProjects.length === 0) { + return false; + } + return info.containingProjects[0].projectKind === server.ProjectKind.Inferred && info.containingProjects[0].isRoot(info); + } + var DirectoryWatchers = (function () { + function DirectoryWatchers(projectService) { + this.projectService = projectService; + this.directoryWatchersForTsconfig = ts.createMap(); + this.directoryWatchersRefCount = ts.createMap(); + } + DirectoryWatchers.prototype.stopWatchingDirectory = function (directory) { + this.directoryWatchersRefCount[directory]--; + if (this.directoryWatchersRefCount[directory] === 0) { + this.projectService.logger.info("Close directory watcher for: " + directory); + this.directoryWatchersForTsconfig[directory].close(); + delete this.directoryWatchersForTsconfig[directory]; + } + }; + DirectoryWatchers.prototype.startWatchingContainingDirectoriesForFile = function (fileName, project, callback) { + var currentPath = ts.getDirectoryPath(fileName); + var parentPath = ts.getDirectoryPath(currentPath); + while (currentPath != parentPath) { + if (!this.directoryWatchersForTsconfig[currentPath]) { + this.projectService.logger.info("Add watcher for: " + currentPath); + this.directoryWatchersForTsconfig[currentPath] = this.projectService.host.watchDirectory(currentPath, callback); + this.directoryWatchersRefCount[currentPath] = 1; + } + else { + this.directoryWatchersRefCount[currentPath] += 1; } + project.directoriesWatchedForTsconfig.push(currentPath); + currentPath = parentPath; + parentPath = ts.getDirectoryPath(parentPath); + } + }; + return DirectoryWatchers; + }()); + var ProjectService = (function () { + function ProjectService(host, logger, cancellationToken, useSingleInferredProject, typingsInstaller, eventHandler) { + if (typingsInstaller === void 0) { typingsInstaller = server.nullTypingsInstaller; } + this.host = host; + this.logger = logger; + this.cancellationToken = cancellationToken; + this.useSingleInferredProject = useSingleInferredProject; + this.typingsInstaller = typingsInstaller; + this.eventHandler = eventHandler; + this.filenameToScriptInfo = ts.createFileMap(); + this.externalProjectToConfiguredProjectMap = ts.createMap(); + this.externalProjects = []; + this.inferredProjects = []; + this.configuredProjects = []; + this.openFiles = []; + this.toCanonicalFileName = ts.createGetCanonicalFileName(host.useCaseSensitiveFileNames); + this.directoryWatchers = new DirectoryWatchers(this); + this.throttledOperations = new server.ThrottledOperations(host); + this.typingsInstaller.attach(this); + this.typingsCache = new server.TypingsCache(this.typingsInstaller); + this.hostConfiguration = { + formatCodeOptions: server.getDefaultFormatCodeSettings(this.host), + hostInfo: "Unknown host" + }; + this.documentRegistry = ts.createDocumentRegistry(host.useCaseSensitiveFileNames, host.getCurrentDirectory()); + } + ProjectService.prototype.getChangedFiles_TestOnly = function () { + return this.changedFiles; + }; + ProjectService.prototype.ensureInferredProjectsUpToDate_TestOnly = function () { + this.ensureInferredProjectsUpToDate(); + }; + ProjectService.prototype.updateTypingsForProject = function (response) { + var project = this.findProject(response.projectName); + if (!project) { + return; } - catch (err) { - this.logError(err, "syntactic check"); + switch (response.kind) { + case "set": + this.typingsCache.updateTypingsForProject(response.projectName, response.compilerOptions, response.typingOptions, response.typings); + project.updateGraph(); + break; + case "invalidate": + this.typingsCache.invalidateCachedTypingsForProject(project); + break; } }; - Session.prototype.reloadProjects = function () { - this.projectService.reloadProjects(); + ProjectService.prototype.setCompilerOptionsForInferredProjects = function (projectCompilerOptions) { + this.compilerOptionsForInferredProjects = projectCompilerOptions; + this.compileOnSaveForInferredProjects = projectCompilerOptions.compileOnSave; + for (var _i = 0, _a = this.inferredProjects; _i < _a.length; _i++) { + var proj = _a[_i]; + proj.setCompilerOptions(projectCompilerOptions); + proj.compileOnSaveEnabled = projectCompilerOptions.compileOnSave; + } + this.updateProjectGraphs(this.inferredProjects); }; - Session.prototype.updateProjectStructure = function (seq, matchSeq, ms) { - var _this = this; - if (ms === void 0) { ms = 1500; } - setTimeout(function () { - if (matchSeq(seq)) { - _this.projectService.updateProjectStructure(); - } - }, ms); + ProjectService.prototype.stopWatchingDirectory = function (directory) { + this.directoryWatchers.stopWatchingDirectory(directory); }; - Session.prototype.updateErrorCheck = function (checkList, seq, matchSeq, ms, followMs, requireOpen) { - var _this = this; - if (ms === void 0) { ms = 1500; } - if (followMs === void 0) { followMs = 200; } - if (requireOpen === void 0) { requireOpen = true; } - if (followMs > ms) { - followMs = ms; + ProjectService.prototype.findProject = function (projectName) { + if (projectName === undefined) { + return undefined; } - if (this.errorTimer) { - clearTimeout(this.errorTimer); + if (server.isInferredProjectName(projectName)) { + this.ensureInferredProjectsUpToDate(); + return findProjectByName(projectName, this.inferredProjects); } - if (this.immediateId) { - clearImmediate(this.immediateId); - this.immediateId = undefined; + return this.findExternalProjectByProjectName(projectName) || this.findConfiguredProjectByProjectName(server.toNormalizedPath(projectName)); + }; + ProjectService.prototype.getDefaultProjectForFile = function (fileName, refreshInferredProjects) { + if (refreshInferredProjects) { + this.ensureInferredProjectsUpToDate(); } - var index = 0; - var checkOne = function () { - if (matchSeq(seq)) { - var checkSpec_1 = checkList[index]; - index++; - if (checkSpec_1.project.getSourceFileFromName(checkSpec_1.fileName, requireOpen)) { - _this.syntacticCheck(checkSpec_1.fileName, checkSpec_1.project); - _this.immediateId = setImmediate(function () { - _this.semanticCheck(checkSpec_1.fileName, checkSpec_1.project); - _this.immediateId = undefined; - if (checkList.length > index) { - _this.errorTimer = setTimeout(checkOne, followMs); - } - else { - _this.errorTimer = undefined; - } - }); + var scriptInfo = this.getScriptInfoForNormalizedPath(fileName); + return scriptInfo && scriptInfo.getDefaultProject(); + }; + ProjectService.prototype.ensureInferredProjectsUpToDate = function () { + if (this.changedFiles) { + var projectsToUpdate = void 0; + if (this.changedFiles.length === 1) { + projectsToUpdate = this.changedFiles[0].containingProjects; + } + else { + projectsToUpdate = []; + for (var _i = 0, _a = this.changedFiles; _i < _a.length; _i++) { + var f = _a[_i]; + projectsToUpdate = projectsToUpdate.concat(f.containingProjects); } } - }; - if ((checkList.length > index) && (matchSeq(seq))) { - this.errorTimer = setTimeout(checkOne, ms); + this.updateProjectGraphs(projectsToUpdate); + this.changedFiles = undefined; } }; - Session.prototype.getDefinition = function (line, offset, fileName) { - var file = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(file); - if (!project || project.languageServiceDiabled) { - throw Errors.NoProject; + ProjectService.prototype.findContainingExternalProject = function (fileName) { + for (var _i = 0, _a = this.externalProjects; _i < _a.length; _i++) { + var proj = _a[_i]; + if (proj.containsFile(fileName)) { + return proj; + } } - var compilerService = project.compilerService; - var position = compilerService.host.lineOffsetToPosition(file, line, offset); - var definitions = compilerService.languageService.getDefinitionAtPosition(file, position); - if (!definitions) { - return undefined; + return undefined; + }; + ProjectService.prototype.getFormatCodeOptions = function (file) { + var formatCodeSettings; + if (file) { + var info = this.getScriptInfoForNormalizedPath(file); + if (info) { + formatCodeSettings = info.getFormatCodeSettings(); + } } - return definitions.map(function (def) { return ({ - file: def.fileName, - start: compilerService.host.positionToLineOffset(def.fileName, def.textSpan.start), - end: compilerService.host.positionToLineOffset(def.fileName, ts.textSpanEnd(def.textSpan)) - }); }); + return formatCodeSettings || this.hostConfiguration.formatCodeOptions; }; - Session.prototype.getTypeDefinition = function (line, offset, fileName) { - var file = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(file); - if (!project || project.languageServiceDiabled) { - throw Errors.NoProject; + ProjectService.prototype.updateProjectGraphs = function (projects) { + var shouldRefreshInferredProjects = false; + for (var _i = 0, projects_2 = projects; _i < projects_2.length; _i++) { + var p = projects_2[_i]; + if (!p.updateGraph()) { + shouldRefreshInferredProjects = true; + } } - var compilerService = project.compilerService; - var position = compilerService.host.lineOffsetToPosition(file, line, offset); - var definitions = compilerService.languageService.getTypeDefinitionAtPosition(file, position); - if (!definitions) { - return undefined; + if (shouldRefreshInferredProjects) { + this.refreshInferredProjects(); } - return definitions.map(function (def) { return ({ - file: def.fileName, - start: compilerService.host.positionToLineOffset(def.fileName, def.textSpan.start), - end: compilerService.host.positionToLineOffset(def.fileName, ts.textSpanEnd(def.textSpan)) - }); }); }; - Session.prototype.getImplementation = function (line, offset, fileName) { - var file = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(file); - if (!project || project.languageServiceDiabled) { - throw Errors.NoProject; + ProjectService.prototype.onSourceFileChanged = function (fileName) { + var info = this.getScriptInfoForNormalizedPath(fileName); + if (!info) { + this.logger.info("Error: got watch notification for unknown file: " + fileName); + return; } - var compilerService = project.compilerService; - var implementations = compilerService.languageService.getImplementationAtPosition(file, compilerService.host.lineOffsetToPosition(file, line, offset)); - if (!implementations) { - return undefined; + if (!this.host.fileExists(fileName)) { + this.handleDeletedFile(info); } - return implementations.map(function (impl) { return ({ - file: impl.fileName, - start: compilerService.host.positionToLineOffset(impl.fileName, impl.textSpan.start), - end: compilerService.host.positionToLineOffset(impl.fileName, ts.textSpanEnd(impl.textSpan)) - }); }); - }; - Session.prototype.getOccurrences = function (line, offset, fileName) { - fileName = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(fileName); - if (!project || project.languageServiceDiabled) { - throw Errors.NoProject; + else { + if (info && (!info.isOpen)) { + info.reloadFromFile(); + this.updateProjectGraphs(info.containingProjects); + } } - var compilerService = project.compilerService; - var position = compilerService.host.lineOffsetToPosition(fileName, line, offset); - var occurrences = compilerService.languageService.getOccurrencesAtPosition(fileName, position); - if (!occurrences) { - return undefined; + }; + ProjectService.prototype.handleDeletedFile = function (info) { + this.logger.info(info.fileName + " deleted"); + info.stopWatcher(); + if (!info.isOpen) { + this.filenameToScriptInfo.remove(info.path); + this.lastDeletedFile = info; + var containingProjects = info.containingProjects.slice(); + info.detachAllProjects(); + this.updateProjectGraphs(containingProjects); + this.lastDeletedFile = undefined; + if (!this.eventHandler) { + return; + } + for (var _i = 0, _a = this.openFiles; _i < _a.length; _i++) { + var openFile = _a[_i]; + this.eventHandler({ eventName: "context", data: { project: openFile.getDefaultProject(), fileName: openFile.fileName } }); + } } - return occurrences.map(function (occurrence) { - var fileName = occurrence.fileName, isWriteAccess = occurrence.isWriteAccess, textSpan = occurrence.textSpan; - var start = compilerService.host.positionToLineOffset(fileName, textSpan.start); - var end = compilerService.host.positionToLineOffset(fileName, ts.textSpanEnd(textSpan)); - return { - start: start, - end: end, - file: fileName, - isWriteAccess: isWriteAccess - }; + this.printProjects(); + }; + ProjectService.prototype.onTypeRootFileChanged = function (project, fileName) { + var _this = this; + this.logger.info("Type root file " + fileName + " changed"); + this.throttledOperations.schedule(project.configFileName + " * type root", 250, function () { + project.updateTypes(); + _this.updateConfiguredProject(project); + _this.refreshInferredProjects(); }); }; - Session.prototype.getDiagnosticsWorker = function (args, selector) { - var file = ts.normalizePath(args.file); - var project = this.projectService.getProjectForFile(file); - if (!project) { - throw Errors.NoProject; - } - if (project.languageServiceDiabled) { - throw Errors.ProjectLanguageServiceDisabled; + ProjectService.prototype.onSourceFileInDirectoryChangedForConfiguredProject = function (project, fileName) { + var _this = this; + if (fileName && !ts.isSupportedSourceFileName(fileName, project.getCompilerOptions())) { + return; } - var diagnostics = selector(project, file); - return ts.map(diagnostics, function (originalDiagnostic) { return formatDiag(file, project, originalDiagnostic); }); + this.logger.info("Detected source file changes: " + fileName); + this.throttledOperations.schedule(project.configFileName, 250, function () { return _this.handleChangeInSourceFileForConfiguredProject(project); }); }; - Session.prototype.getSyntacticDiagnosticsSync = function (args) { - return this.getDiagnosticsWorker(args, function (project, file) { return project.compilerService.languageService.getSyntacticDiagnostics(file); }); + ProjectService.prototype.handleChangeInSourceFileForConfiguredProject = function (project) { + var _this = this; + var _a = this.convertConfigFileContentToProjectOptions(project.configFileName), projectOptions = _a.projectOptions, configFileErrors = _a.configFileErrors; + this.reportConfigFileDiagnostics(project.getProjectName(), configFileErrors); + var newRootFiles = projectOptions.files.map((function (f) { return _this.getCanonicalFileName(f); })); + var currentRootFiles = project.getRootFiles().map((function (f) { return _this.getCanonicalFileName(f); })); + if (!ts.arrayIsEqualTo(currentRootFiles.sort(), newRootFiles.sort())) { + this.logger.info("Updating configured project"); + this.updateConfiguredProject(project); + this.refreshInferredProjects(); + } }; - Session.prototype.getSemanticDiagnosticsSync = function (args) { - return this.getDiagnosticsWorker(args, function (project, file) { return project.compilerService.languageService.getSemanticDiagnostics(file); }); + ProjectService.prototype.onConfigChangedForConfiguredProject = function (project) { + this.logger.info("Config file changed: " + project.configFileName); + this.updateConfiguredProject(project); + this.refreshInferredProjects(); }; - Session.prototype.getDocumentHighlights = function (line, offset, fileName, filesToSearch) { - fileName = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(fileName); - if (!project || project.languageServiceDiabled) { - throw Errors.NoProject; - } - var compilerService = project.compilerService; - var position = compilerService.host.lineOffsetToPosition(fileName, line, offset); - var documentHighlights = compilerService.languageService.getDocumentHighlights(fileName, position, filesToSearch); - if (!documentHighlights) { - return undefined; - } - return documentHighlights.map(convertToDocumentHighlightsItem); - function convertToDocumentHighlightsItem(documentHighlights) { - var fileName = documentHighlights.fileName, highlightSpans = documentHighlights.highlightSpans; - return { - file: fileName, - highlightSpans: highlightSpans.map(convertHighlightSpan) - }; - function convertHighlightSpan(highlightSpan) { - var textSpan = highlightSpan.textSpan, kind = highlightSpan.kind; - var start = compilerService.host.positionToLineOffset(fileName, textSpan.start); - var end = compilerService.host.positionToLineOffset(fileName, ts.textSpanEnd(textSpan)); - return { start: start, end: end, kind: kind }; - } + ProjectService.prototype.onConfigFileAddedForInferredProject = function (fileName) { + if (ts.getBaseFileName(fileName) != "tsconfig.json") { + this.logger.info(fileName + " is not tsconfig.json"); + return; } + var configFileErrors = this.convertConfigFileContentToProjectOptions(fileName).configFileErrors; + this.reportConfigFileDiagnostics(fileName, configFileErrors); + this.logger.info("Detected newly added tsconfig file: " + fileName); + this.reloadProjects(); }; - Session.prototype.getProjectInfo = function (fileName, needFileNameList) { - fileName = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(fileName); - if (!project) { - throw Errors.NoProject; - } - var projectInfo = { - configFileName: project.projectFilename, - languageServiceDisabled: project.languageServiceDiabled - }; - if (needFileNameList) { - projectInfo.fileNames = project.getFileNames(); + ProjectService.prototype.getCanonicalFileName = function (fileName) { + var name = this.host.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase(); + return ts.normalizePath(name); + }; + ProjectService.prototype.removeProject = function (project) { + this.logger.info("remove project: " + project.getRootFiles().toString()); + project.close(); + switch (project.projectKind) { + case server.ProjectKind.External: + server.removeItemFromSet(this.externalProjects, project); + break; + case server.ProjectKind.Configured: + server.removeItemFromSet(this.configuredProjects, project); + break; + case server.ProjectKind.Inferred: + server.removeItemFromSet(this.inferredProjects, project); + break; } - return projectInfo; }; - Session.prototype.getRenameLocations = function (line, offset, fileName, findInComments, findInStrings) { - var file = ts.normalizePath(fileName); - var info = this.projectService.getScriptInfo(file); - var projects = this.projectService.findReferencingProjects(info); - var projectsWithLanguageServiceEnabeld = ts.filter(projects, function (p) { return !p.languageServiceDiabled; }); - if (projectsWithLanguageServiceEnabeld.length === 0) { - throw Errors.NoProject; - } - var defaultProject = projectsWithLanguageServiceEnabeld[0]; - var defaultProjectCompilerService = defaultProject.compilerService; - var position = defaultProjectCompilerService.host.lineOffsetToPosition(file, line, offset); - var renameInfo = defaultProjectCompilerService.languageService.getRenameInfo(file, position); - if (!renameInfo) { - return undefined; + ProjectService.prototype.assignScriptInfoToInferredProjectIfNecessary = function (info, addToListOfOpenFiles) { + var externalProject = this.findContainingExternalProject(info.fileName); + if (externalProject) { + if (addToListOfOpenFiles) { + this.openFiles.push(info); + } + return; } - if (!renameInfo.canRename) { - return { - info: renameInfo, - locs: [] - }; + var foundConfiguredProject = false; + for (var _i = 0, _a = info.containingProjects; _i < _a.length; _i++) { + var p = _a[_i]; + if (p.projectKind === server.ProjectKind.Configured) { + foundConfiguredProject = true; + if (addToListOfOpenFiles) { + (p).addOpenRef(); + } + } } - var fileSpans = server.combineProjectOutput(projectsWithLanguageServiceEnabeld, function (project) { - var compilerService = project.compilerService; - var renameLocations = compilerService.languageService.findRenameLocations(file, position, findInStrings, findInComments); - if (!renameLocations) { - return []; + if (foundConfiguredProject) { + if (addToListOfOpenFiles) { + this.openFiles.push(info); } - return renameLocations.map(function (location) { return ({ - file: location.fileName, - start: compilerService.host.positionToLineOffset(location.fileName, location.textSpan.start), - end: compilerService.host.positionToLineOffset(location.fileName, ts.textSpanEnd(location.textSpan)) - }); }); - }, compareRenameLocation, function (a, b) { return a.file === b.file && a.start.line === b.start.line && a.start.offset === b.start.offset; }); - var locs = fileSpans.reduce(function (accum, cur) { - var curFileAccum; - if (accum.length > 0) { - curFileAccum = accum[accum.length - 1]; - if (curFileAccum.file !== cur.file) { - curFileAccum = undefined; + return; + } + if (info.containingProjects.length === 0) { + var inferredProject = this.createInferredProjectWithRootFileIfNecessary(info); + if (!this.useSingleInferredProject) { + for (var _b = 0, _c = this.openFiles; _b < _c.length; _b++) { + var f = _c[_b]; + if (f.containingProjects.length === 0) { + continue; + } + var defaultProject = f.getDefaultProject(); + if (isRootFileInInferredProject(info) && defaultProject !== inferredProject && inferredProject.containsScriptInfo(f)) { + this.removeProject(defaultProject); + f.attachToProject(inferredProject); + } } } - if (!curFileAccum) { - curFileAccum = { file: cur.file, locs: [] }; - accum.push(curFileAccum); + } + if (addToListOfOpenFiles) { + this.openFiles.push(info); + } + }; + ProjectService.prototype.closeOpenFile = function (info) { + info.reloadFromFile(); + server.removeItemFromSet(this.openFiles, info); + info.isOpen = false; + var projectsToRemove; + for (var _i = 0, _a = info.containingProjects; _i < _a.length; _i++) { + var p = _a[_i]; + if (p.projectKind === server.ProjectKind.Configured) { + if (p.deleteOpenRef() === 0) { + (projectsToRemove || (projectsToRemove = [])).push(p); + } } - curFileAccum.locs.push({ start: cur.start, end: cur.end }); - return accum; - }, []); - return { info: renameInfo, locs: locs }; - function compareRenameLocation(a, b) { - if (a.file < b.file) { - return -1; + else if (p.projectKind === server.ProjectKind.Inferred && p.isRoot(info)) { + (projectsToRemove || (projectsToRemove = [])).push(p); } - else if (a.file > b.file) { - return 1; + } + if (projectsToRemove) { + for (var _b = 0, projectsToRemove_1 = projectsToRemove; _b < projectsToRemove_1.length; _b++) { + var project = projectsToRemove_1[_b]; + this.removeProject(project); } - else { - if (a.start.line < b.start.line) { - return 1; - } - else if (a.start.line > b.start.line) { - return -1; + var orphanFiles = void 0; + for (var _c = 0, _d = this.openFiles; _c < _d.length; _c++) { + var f = _d[_c]; + if (f.containingProjects.length === 0) { + (orphanFiles || (orphanFiles = [])).push(f); } - else { - return b.start.offset - a.start.offset; + } + if (orphanFiles) { + for (var _e = 0, orphanFiles_1 = orphanFiles; _e < orphanFiles_1.length; _e++) { + var f = orphanFiles_1[_e]; + this.assignScriptInfoToInferredProjectIfNecessary(f, false); } } } - }; - Session.prototype.getReferences = function (line, offset, fileName) { - var file = ts.normalizePath(fileName); - var info = this.projectService.getScriptInfo(file); - var projects = this.projectService.findReferencingProjects(info); - var projectsWithLanguageServiceEnabeld = ts.filter(projects, function (p) { return !p.languageServiceDiabled; }); - if (projectsWithLanguageServiceEnabeld.length === 0) { - throw Errors.NoProject; - } - var defaultProject = projectsWithLanguageServiceEnabeld[0]; - var position = defaultProject.compilerService.host.lineOffsetToPosition(file, line, offset); - var nameInfo = defaultProject.compilerService.languageService.getQuickInfoAtPosition(file, position); - if (!nameInfo) { - return undefined; + if (info.containingProjects.length === 0) { + this.filenameToScriptInfo.remove(info.path); } - var displayString = ts.displayPartsToString(nameInfo.displayParts); - var nameSpan = nameInfo.textSpan; - var nameColStart = defaultProject.compilerService.host.positionToLineOffset(file, nameSpan.start).offset; - var nameText = defaultProject.compilerService.host.getScriptSnapshot(file).getText(nameSpan.start, ts.textSpanEnd(nameSpan)); - var refs = server.combineProjectOutput(projectsWithLanguageServiceEnabeld, function (project) { - var compilerService = project.compilerService; - var references = compilerService.languageService.getReferencesAtPosition(file, position); - if (!references) { - return []; + }; + ProjectService.prototype.openOrUpdateConfiguredProjectForFile = function (fileName) { + var searchPath = ts.getDirectoryPath(fileName); + this.logger.info("Search path: " + searchPath); + var configFileName = this.findConfigFile(server.asNormalizedPath(searchPath)); + if (!configFileName) { + this.logger.info("No config files found."); + return {}; + } + this.logger.info("Config file name: " + configFileName); + var project = this.findConfiguredProjectByProjectName(configFileName); + if (!project) { + var _a = this.openConfigFile(configFileName, fileName), success = _a.success, errors = _a.errors; + if (!success) { + return { configFileName: configFileName, configFileErrors: errors }; } - return references.map(function (ref) { - var start = compilerService.host.positionToLineOffset(ref.fileName, ref.textSpan.start); - var refLineSpan = compilerService.host.lineToTextSpan(ref.fileName, start.line - 1); - var snap = compilerService.host.getScriptSnapshot(ref.fileName); - var lineText = snap.getText(refLineSpan.start, ts.textSpanEnd(refLineSpan)).replace(/\r|\n/g, ""); - return { - file: ref.fileName, - start: start, - lineText: lineText, - end: compilerService.host.positionToLineOffset(ref.fileName, ts.textSpanEnd(ref.textSpan)), - isWriteAccess: ref.isWriteAccess, - isDefinition: ref.isDefinition - }; - }); - }, compareFileStart, areReferencesResponseItemsForTheSameLocation); - return { - refs: refs, - symbolName: nameText, - symbolStartOffset: nameColStart, - symbolDisplayString: displayString - }; - function areReferencesResponseItemsForTheSameLocation(a, b) { - if (a && b) { - return a.file === b.file && - a.start === b.start && - a.end === b.end; + this.logger.info("Opened configuration file " + configFileName); + if (errors && errors.length > 0) { + return { configFileName: configFileName, configFileErrors: errors }; } - return false; - } - }; - Session.prototype.openClientFile = function (fileName, fileContent, scriptKind) { - var file = ts.normalizePath(fileName); - var _a = this.projectService.openClientFile(file, fileContent, scriptKind), configFileName = _a.configFileName, configFileErrors = _a.configFileErrors; - if (configFileErrors) { - this.configFileDiagnosticEvent(fileName, configFileName, configFileErrors); - } - }; - Session.prototype.getQuickInfo = function (line, offset, fileName) { - var file = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(file); - if (!project || project.languageServiceDiabled) { - throw Errors.NoProject; } - var compilerService = project.compilerService; - var position = compilerService.host.lineOffsetToPosition(file, line, offset); - var quickInfo = compilerService.languageService.getQuickInfoAtPosition(file, position); - if (!quickInfo) { - return undefined; - } - var displayString = ts.displayPartsToString(quickInfo.displayParts); - var docString = ts.displayPartsToString(quickInfo.documentation); - return { - kind: quickInfo.kind, - kindModifiers: quickInfo.kindModifiers, - start: compilerService.host.positionToLineOffset(file, quickInfo.textSpan.start), - end: compilerService.host.positionToLineOffset(file, ts.textSpanEnd(quickInfo.textSpan)), - displayString: displayString, - documentation: docString - }; - }; - Session.prototype.getFormattingEditsForRange = function (line, offset, endLine, endOffset, fileName) { - var file = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(file); - if (!project || project.languageServiceDiabled) { - throw Errors.NoProject; - } - var compilerService = project.compilerService; - var startPosition = compilerService.host.lineOffsetToPosition(file, line, offset); - var endPosition = compilerService.host.lineOffsetToPosition(file, endLine, endOffset); - var edits = compilerService.languageService.getFormattingEditsForRange(file, startPosition, endPosition, this.projectService.getFormatCodeOptions(file)); - if (!edits) { - return undefined; + else { + this.updateConfiguredProject(project); } - return edits.map(function (edit) { - return { - start: compilerService.host.positionToLineOffset(file, edit.span.start), - end: compilerService.host.positionToLineOffset(file, ts.textSpanEnd(edit.span)), - newText: edit.newText ? edit.newText : "" - }; - }); + return { configFileName: configFileName }; }; - Session.prototype.getFormattingEditsAfterKeystroke = function (line, offset, key, fileName) { - var file = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(file); - if (!project || project.languageServiceDiabled) { - throw Errors.NoProject; - } - var compilerService = project.compilerService; - var position = compilerService.host.lineOffsetToPosition(file, line, offset); - var formatOptions = this.projectService.getFormatCodeOptions(file); - var edits = compilerService.languageService.getFormattingEditsAfterKeystroke(file, position, key, formatOptions); - if ((key == "\n") && ((!edits) || (edits.length === 0) || allEditsBeforePos(edits, position))) { - var scriptInfo = compilerService.host.getScriptInfo(file); - if (scriptInfo) { - var lineInfo = scriptInfo.getLineInfo(line); - if (lineInfo && (lineInfo.leaf) && (lineInfo.leaf.text)) { - var lineText = lineInfo.leaf.text; - if (lineText.search("\\S") < 0) { - var editorOptions = { - BaseIndentSize: formatOptions.BaseIndentSize, - IndentSize: formatOptions.IndentSize, - TabSize: formatOptions.TabSize, - NewLineCharacter: formatOptions.NewLineCharacter, - ConvertTabsToSpaces: formatOptions.ConvertTabsToSpaces, - IndentStyle: ts.IndentStyle.Smart - }; - var preferredIndent = compilerService.languageService.getIndentationAtPosition(file, position, editorOptions); - var hasIndent = 0; - var i = void 0, len = void 0; - for (i = 0, len = lineText.length; i < len; i++) { - if (lineText.charAt(i) == " ") { - hasIndent++; - } - else if (lineText.charAt(i) == "\t") { - hasIndent += editorOptions.TabSize; - } - else { - break; - } - } - if (preferredIndent !== hasIndent) { - var firstNoWhiteSpacePosition = lineInfo.offset + i; - edits.push({ - span: ts.createTextSpanFromBounds(lineInfo.offset, firstNoWhiteSpacePosition), - newText: generateIndentString(preferredIndent, editorOptions) - }); - } - } - } + ProjectService.prototype.findConfigFile = function (searchPath) { + while (true) { + var tsconfigFileName = server.asNormalizedPath(ts.combinePaths(searchPath, "tsconfig.json")); + if (this.host.fileExists(tsconfigFileName)) { + return tsconfigFileName; } + var jsconfigFileName = server.asNormalizedPath(ts.combinePaths(searchPath, "jsconfig.json")); + if (this.host.fileExists(jsconfigFileName)) { + return jsconfigFileName; + } + var parentPath = server.asNormalizedPath(ts.getDirectoryPath(searchPath)); + if (parentPath === searchPath) { + break; + } + searchPath = parentPath; } - if (!edits) { - return undefined; - } - return edits.map(function (edit) { - return { - start: compilerService.host.positionToLineOffset(file, edit.span.start), - end: compilerService.host.positionToLineOffset(file, ts.textSpanEnd(edit.span)), - newText: edit.newText ? edit.newText : "" - }; - }); + return undefined; }; - Session.prototype.getCompletions = function (line, offset, prefix, fileName) { - if (!prefix) { - prefix = ""; - } - var file = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(file); - if (!project || project.languageServiceDiabled) { - throw Errors.NoProject; + ProjectService.prototype.printProjects = function () { + if (!this.logger.hasLevel(server.LogLevel.verbose)) { + return; } - var compilerService = project.compilerService; - var position = compilerService.host.lineOffsetToPosition(file, line, offset); - var completions = compilerService.languageService.getCompletionsAtPosition(file, position); - if (!completions) { - return undefined; + this.logger.startGroup(); + var counter = 0; + counter = printProjects(this.logger, this.externalProjects, counter); + counter = printProjects(this.logger, this.configuredProjects, counter); + counter = printProjects(this.logger, this.inferredProjects, counter); + this.logger.info("Open files: "); + for (var _i = 0, _a = this.openFiles; _i < _a.length; _i++) { + var rootFile = _a[_i]; + this.logger.info(rootFile.fileName); + } + this.logger.endGroup(); + function printProjects(logger, projects, counter) { + for (var _i = 0, projects_3 = projects; _i < projects_3.length; _i++) { + var project = projects_3[_i]; + project.updateGraph(); + logger.info("Project '" + project.getProjectName() + "' (" + server.ProjectKind[project.projectKind] + ") " + counter); + logger.info(project.filesToString()); + logger.info("-----------------------------------------------"); + counter++; + } + return counter; } - return completions.entries.reduce(function (result, entry) { - if (completions.isMemberCompletion || (entry.name.toLowerCase().indexOf(prefix.toLowerCase()) === 0)) { - var name_52 = entry.name, kind = entry.kind, kindModifiers = entry.kindModifiers, sortText = entry.sortText, replacementSpan = entry.replacementSpan; - var convertedSpan = undefined; - if (replacementSpan) { - convertedSpan = { - start: compilerService.host.positionToLineOffset(fileName, replacementSpan.start), - end: compilerService.host.positionToLineOffset(fileName, replacementSpan.start + replacementSpan.length) - }; - } - result.push({ name: name_52, kind: kind, kindModifiers: kindModifiers, sortText: sortText, replacementSpan: convertedSpan }); - } - return result; - }, []).sort(function (a, b) { return a.name.localeCompare(b.name); }); }; - Session.prototype.getCompletionEntryDetails = function (line, offset, entryNames, fileName) { - var file = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(file); - if (!project || project.languageServiceDiabled) { - throw Errors.NoProject; - } - var compilerService = project.compilerService; - var position = compilerService.host.lineOffsetToPosition(file, line, offset); - return entryNames.reduce(function (accum, entryName) { - var details = compilerService.languageService.getCompletionEntryDetails(file, position, entryName); - if (details) { - accum.push(details); - } - return accum; - }, []); + ProjectService.prototype.findConfiguredProjectByProjectName = function (configFileName) { + return findProjectByName(configFileName, this.configuredProjects); }; - Session.prototype.getSignatureHelpItems = function (line, offset, fileName) { - var file = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(file); - if (!project || project.languageServiceDiabled) { - throw Errors.NoProject; + ProjectService.prototype.findExternalProjectByProjectName = function (projectFileName) { + return findProjectByName(projectFileName, this.externalProjects); + }; + ProjectService.prototype.convertConfigFileContentToProjectOptions = function (configFilename) { + configFilename = ts.normalizePath(configFilename); + var configFileContent = this.host.readFile(configFilename); + var errors; + var result = ts.parseConfigFileTextToJson(configFilename, configFileContent); + var config = result.config; + if (result.error) { + var _a = ts.sanitizeConfigFile(configFilename, configFileContent), sanitizedConfig = _a.configJsonObject, diagnostics = _a.diagnostics; + config = sanitizedConfig; + errors = diagnostics.length ? diagnostics : [result.error]; } - var compilerService = project.compilerService; - var position = compilerService.host.lineOffsetToPosition(file, line, offset); - var helpItems = compilerService.languageService.getSignatureHelpItems(file, position); - if (!helpItems) { - return undefined; + var parsedCommandLine = ts.parseJsonConfigFileContent(config, this.host, ts.getDirectoryPath(configFilename), {}, configFilename); + if (parsedCommandLine.errors.length) { + errors = ts.concatenate(errors, parsedCommandLine.errors); } - var span = helpItems.applicableSpan; - var result = { - items: helpItems.items, - applicableSpan: { - start: compilerService.host.positionToLineOffset(file, span.start), - end: compilerService.host.positionToLineOffset(file, span.start + span.length) - }, - selectedItemIndex: helpItems.selectedItemIndex, - argumentIndex: helpItems.argumentIndex, - argumentCount: helpItems.argumentCount + ts.Debug.assert(!!parsedCommandLine.fileNames); + if (parsedCommandLine.fileNames.length === 0) { + (errors || (errors = [])).push(ts.createCompilerDiagnostic(ts.Diagnostics.The_config_file_0_found_doesn_t_contain_any_source_files, configFilename)); + return { success: false, configFileErrors: errors }; + } + var projectOptions = { + files: parsedCommandLine.fileNames, + compilerOptions: parsedCommandLine.options, + configHasFilesProperty: config["files"] !== undefined, + wildcardDirectories: ts.createMap(parsedCommandLine.wildcardDirectories), + typingOptions: parsedCommandLine.typingOptions, + compileOnSave: parsedCommandLine.compileOnSave }; - return result; + return { success: true, projectOptions: projectOptions, configFileErrors: errors }; }; - Session.prototype.getDiagnostics = function (delay, fileNames) { - var _this = this; - var checkList = fileNames.reduce(function (accum, fileName) { - fileName = ts.normalizePath(fileName); - var project = _this.projectService.getProjectForFile(fileName); - if (project && !project.languageServiceDiabled) { - accum.push({ fileName: fileName, project: project }); - } - return accum; - }, []); - if (checkList.length > 0) { - this.updateErrorCheck(checkList, this.changeSeq, function (n) { return n === _this.changeSeq; }, delay); + ProjectService.prototype.exceededTotalSizeLimitForNonTsFiles = function (options, fileNames, propertyReader) { + if (options && options.disableSizeLimit || !this.host.getFileSize) { + return false; } - }; - Session.prototype.change = function (line, offset, endLine, endOffset, insertString, fileName) { - var _this = this; - var file = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(file); - if (project && !project.languageServiceDiabled) { - var compilerService = project.compilerService; - var start = compilerService.host.lineOffsetToPosition(file, line, offset); - var end = compilerService.host.lineOffsetToPosition(file, endLine, endOffset); - if (start >= 0) { - compilerService.host.editScript(file, start, end, insertString); - this.changeSeq++; + var totalNonTsFileSize = 0; + for (var _i = 0, fileNames_3 = fileNames; _i < fileNames_3.length; _i++) { + var f = fileNames_3[_i]; + var fileName = propertyReader.getFileName(f); + if (ts.hasTypeScriptFileExtension(fileName)) { + continue; + } + totalNonTsFileSize += this.host.getFileSize(fileName); + if (totalNonTsFileSize > server.maxProgramSizeForNonTsFiles) { + return true; } - this.updateProjectStructure(this.changeSeq, function (n) { return n === _this.changeSeq; }); - } - }; - Session.prototype.reload = function (fileName, tempFileName, reqSeq) { - var _this = this; - if (reqSeq === void 0) { reqSeq = 0; } - var file = ts.normalizePath(fileName); - var tmpfile = ts.normalizePath(tempFileName); - var project = this.projectService.getProjectForFile(file); - if (project && !project.languageServiceDiabled) { - this.changeSeq++; - project.compilerService.host.reloadScript(file, tmpfile, function () { - _this.output(undefined, CommandNames.Reload, reqSeq); - }); } + return false; }; - Session.prototype.saveToTmp = function (fileName, tempFileName) { - var file = ts.normalizePath(fileName); - var tmpfile = ts.normalizePath(tempFileName); - var project = this.projectService.getProjectForFile(file); - if (project && !project.languageServiceDiabled) { - project.compilerService.host.saveTo(file, tmpfile); - } + ProjectService.prototype.createAndAddExternalProject = function (projectFileName, files, options, typingOptions) { + var project = new server.ExternalProject(projectFileName, this, this.documentRegistry, options, !this.exceededTotalSizeLimitForNonTsFiles(options, files, externalFilePropertyReader), options.compileOnSave === undefined ? true : options.compileOnSave); + this.addFilesToProjectAndUpdateGraph(project, files, externalFilePropertyReader, undefined, typingOptions, undefined); + this.externalProjects.push(project); + return project; }; - Session.prototype.closeClientFile = function (fileName) { - if (!fileName) { + ProjectService.prototype.reportConfigFileDiagnostics = function (configFileName, diagnostics, triggerFile) { + if (!this.eventHandler) { return; } - var file = ts.normalizePath(fileName); - this.projectService.closeClientFile(file); + this.eventHandler({ + eventName: "configFileDiag", + data: { configFileName: configFileName, diagnostics: diagnostics || [], triggerFile: triggerFile } + }); }; - Session.prototype.decorateNavigationBarItem = function (project, fileName, items, lineIndex) { + ProjectService.prototype.createAndAddConfiguredProject = function (configFileName, projectOptions, configFileErrors, clientFileName) { var _this = this; - if (!items) { - return undefined; - } - var compilerService = project.compilerService; - return items.map(function (item) { return ({ - text: item.text, - kind: item.kind, - kindModifiers: item.kindModifiers, - spans: item.spans.map(function (span) { return ({ - start: compilerService.host.positionToLineOffset(fileName, span.start, lineIndex), - end: compilerService.host.positionToLineOffset(fileName, ts.textSpanEnd(span), lineIndex) - }); }), - childItems: _this.decorateNavigationBarItem(project, fileName, item.childItems, lineIndex), - indent: item.indent - }); }); + var sizeLimitExceeded = this.exceededTotalSizeLimitForNonTsFiles(projectOptions.compilerOptions, projectOptions.files, fileNamePropertyReader); + var project = new server.ConfiguredProject(configFileName, this, this.documentRegistry, projectOptions.configHasFilesProperty, projectOptions.compilerOptions, projectOptions.wildcardDirectories, !sizeLimitExceeded, projectOptions.compileOnSave === undefined ? false : projectOptions.compileOnSave); + this.addFilesToProjectAndUpdateGraph(project, projectOptions.files, fileNamePropertyReader, clientFileName, projectOptions.typingOptions, configFileErrors); + project.watchConfigFile(function (project) { return _this.onConfigChangedForConfiguredProject(project); }); + if (!sizeLimitExceeded) { + this.watchConfigDirectoryForProject(project, projectOptions); + } + project.watchWildcards(function (project, path) { return _this.onSourceFileInDirectoryChangedForConfiguredProject(project, path); }); + project.watchTypeRoots(function (project, path) { return _this.onTypeRootFileChanged(project, path); }); + this.configuredProjects.push(project); + return project; }; - Session.prototype.getNavigationBarItems = function (fileName) { - var file = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(file); - if (!project || project.languageServiceDiabled) { - throw Errors.NoProject; + ProjectService.prototype.watchConfigDirectoryForProject = function (project, options) { + var _this = this; + if (!options.configHasFilesProperty) { + project.watchConfigDirectory(function (project, path) { return _this.onSourceFileInDirectoryChangedForConfiguredProject(project, path); }); } - var compilerService = project.compilerService; - var items = compilerService.languageService.getNavigationBarItems(file); - if (!items) { - return undefined; + }; + ProjectService.prototype.addFilesToProjectAndUpdateGraph = function (project, files, propertyReader, clientFileName, typingOptions, configFileErrors) { + var errors; + for (var _i = 0, files_4 = files; _i < files_4.length; _i++) { + var f = files_4[_i]; + var rootFilename = propertyReader.getFileName(f); + var scriptKind = propertyReader.getScriptKind(f); + var hasMixedContent = propertyReader.hasMixedContent(f); + if (this.host.fileExists(rootFilename)) { + var info = this.getOrCreateScriptInfoForNormalizedPath(server.toNormalizedPath(rootFilename), clientFileName == rootFilename, undefined, scriptKind, hasMixedContent); + project.addRoot(info); + } + else { + (errors || (errors = [])).push(createFileNotFoundDiagnostic(rootFilename)); + } } - return this.decorateNavigationBarItem(project, fileName, items, compilerService.host.getLineIndex(fileName)); + project.setProjectErrors(ts.concatenate(configFileErrors, errors)); + project.setTypingOptions(typingOptions); + project.updateGraph(); }; - Session.prototype.getNavigateToItems = function (searchValue, fileName, maxResultCount, currentFileOnly) { - var file = ts.normalizePath(fileName); - var info = this.projectService.getScriptInfo(file); - var projects = this.projectService.findReferencingProjects(info); - var projectsWithLanguageServiceEnabeld = ts.filter(projects, function (p) { return !p.languageServiceDiabled; }); - if (projectsWithLanguageServiceEnabeld.length === 0) { - throw Errors.NoProject; - } - var allNavToItems = server.combineProjectOutput(projectsWithLanguageServiceEnabeld, function (project) { - var compilerService = project.compilerService; - var navItems = compilerService.languageService.getNavigateToItems(searchValue, maxResultCount, currentFileOnly ? fileName : undefined); - if (!navItems) { - return []; + ProjectService.prototype.openConfigFile = function (configFileName, clientFileName) { + var conversionResult = this.convertConfigFileContentToProjectOptions(configFileName); + var projectOptions = conversionResult.success + ? conversionResult.projectOptions + : { files: [], compilerOptions: {} }; + var project = this.createAndAddConfiguredProject(configFileName, projectOptions, conversionResult.configFileErrors, clientFileName); + return { + success: conversionResult.success, + project: project, + errors: project.getProjectErrors() + }; + }; + ProjectService.prototype.updateNonInferredProject = function (project, newUncheckedFiles, propertyReader, newOptions, newTypingOptions, compileOnSave, configFileErrors) { + var oldRootScriptInfos = project.getRootScriptInfos(); + var newRootScriptInfos = []; + var newRootScriptInfoMap = server.createNormalizedPathMap(); + var projectErrors; + var rootFilesChanged = false; + for (var _i = 0, newUncheckedFiles_1 = newUncheckedFiles; _i < newUncheckedFiles_1.length; _i++) { + var f = newUncheckedFiles_1[_i]; + var newRootFile = propertyReader.getFileName(f); + if (!this.host.fileExists(newRootFile)) { + (projectErrors || (projectErrors = [])).push(createFileNotFoundDiagnostic(newRootFile)); + continue; } - return navItems.map(function (navItem) { - var start = compilerService.host.positionToLineOffset(navItem.fileName, navItem.textSpan.start); - var end = compilerService.host.positionToLineOffset(navItem.fileName, ts.textSpanEnd(navItem.textSpan)); - var bakedItem = { - name: navItem.name, - kind: navItem.kind, - file: navItem.fileName, - start: start, - end: end - }; - if (navItem.kindModifiers && (navItem.kindModifiers !== "")) { - bakedItem.kindModifiers = navItem.kindModifiers; + var normalizedPath = server.toNormalizedPath(newRootFile); + var scriptInfo = this.getScriptInfoForNormalizedPath(normalizedPath); + if (!scriptInfo || !project.isRoot(scriptInfo)) { + rootFilesChanged = true; + if (!scriptInfo) { + var scriptKind = propertyReader.getScriptKind(f); + var hasMixedContent = propertyReader.hasMixedContent(f); + scriptInfo = this.getOrCreateScriptInfoForNormalizedPath(normalizedPath, false, undefined, scriptKind, hasMixedContent); } - if (navItem.matchKind !== "none") { - bakedItem.matchKind = navItem.matchKind; + } + newRootScriptInfos.push(scriptInfo); + newRootScriptInfoMap.set(scriptInfo.fileName, scriptInfo); + } + if (rootFilesChanged || newRootScriptInfos.length !== oldRootScriptInfos.length) { + var toAdd = void 0; + var toRemove = void 0; + for (var _a = 0, oldRootScriptInfos_1 = oldRootScriptInfos; _a < oldRootScriptInfos_1.length; _a++) { + var oldFile = oldRootScriptInfos_1[_a]; + if (!newRootScriptInfoMap.contains(oldFile.fileName)) { + (toRemove || (toRemove = [])).push(oldFile); } - if (navItem.containerName && (navItem.containerName.length > 0)) { - bakedItem.containerName = navItem.containerName; + } + for (var _b = 0, newRootScriptInfos_1 = newRootScriptInfos; _b < newRootScriptInfos_1.length; _b++) { + var newFile = newRootScriptInfos_1[_b]; + if (!project.isRoot(newFile)) { + (toAdd || (toAdd = [])).push(newFile); } - if (navItem.containerKind && (navItem.containerKind.length > 0)) { - bakedItem.containerKind = navItem.containerKind; + } + if (toRemove) { + for (var _c = 0, toRemove_1 = toRemove; _c < toRemove_1.length; _c++) { + var f = toRemove_1[_c]; + project.removeFile(f); + } + } + if (toAdd) { + for (var _d = 0, toAdd_1 = toAdd; _d < toAdd_1.length; _d++) { + var f = toAdd_1[_d]; + if (f.isOpen && isRootFileInInferredProject(f)) { + var inferredProject = f.containingProjects[0]; + inferredProject.removeFile(f); + if (!inferredProject.hasRoots()) { + this.removeProject(inferredProject); + } + } + project.addRoot(f); } - return bakedItem; - }); - }, undefined, areNavToItemsForTheSameLocation); - return allNavToItems; - function areNavToItemsForTheSameLocation(a, b) { - if (a && b) { - return a.file === b.file && - a.start === b.start && - a.end === b.end; } - return false; } - }; - Session.prototype.getBraceMatching = function (line, offset, fileName) { - var file = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(file); - if (!project || project.languageServiceDiabled) { - throw Errors.NoProject; - } - var compilerService = project.compilerService; - var position = compilerService.host.lineOffsetToPosition(file, line, offset); - var spans = compilerService.languageService.getBraceMatchingAtPosition(file, position); - if (!spans) { - return undefined; + project.setCompilerOptions(newOptions); + project.setTypingOptions(newTypingOptions); + if (compileOnSave !== undefined) { + project.compileOnSaveEnabled = compileOnSave; } - return spans.map(function (span) { return ({ - start: compilerService.host.positionToLineOffset(file, span.start), - end: compilerService.host.positionToLineOffset(file, span.start + span.length) - }); }); + project.setProjectErrors(ts.concatenate(configFileErrors, projectErrors)); + project.updateGraph(); }; - Session.prototype.getDiagnosticsForProject = function (delay, fileName) { - var _this = this; - var _a = this.getProjectInfo(fileName, true), fileNames = _a.fileNames, languageServiceDisabled = _a.languageServiceDisabled; - if (languageServiceDisabled) { + ProjectService.prototype.updateConfiguredProject = function (project) { + if (!this.host.fileExists(project.configFileName)) { + this.logger.info("Config file deleted"); + this.removeProject(project); return; } - var fileNamesInProject = fileNames.filter(function (value, index, array) { return value.indexOf("lib.d.ts") < 0; }); - var highPriorityFiles = []; - var mediumPriorityFiles = []; - var lowPriorityFiles = []; - var veryLowPriorityFiles = []; - var normalizedFileName = ts.normalizePath(fileName); - var project = this.projectService.getProjectForFile(normalizedFileName); - for (var _i = 0, fileNamesInProject_1 = fileNamesInProject; _i < fileNamesInProject_1.length; _i++) { - var fileNameInProject = fileNamesInProject_1[_i]; - if (this.getCanonicalFileName(fileNameInProject) == this.getCanonicalFileName(fileName)) - highPriorityFiles.push(fileNameInProject); - else { - var info = this.projectService.getScriptInfo(fileNameInProject); - if (!info.isOpen) { - if (fileNameInProject.indexOf(".d.ts") > 0) - veryLowPriorityFiles.push(fileNameInProject); - else - lowPriorityFiles.push(fileNameInProject); - } - else - mediumPriorityFiles.push(fileNameInProject); + var _a = this.convertConfigFileContentToProjectOptions(project.configFileName), success = _a.success, projectOptions = _a.projectOptions, configFileErrors = _a.configFileErrors; + if (!success) { + this.updateNonInferredProject(project, [], fileNamePropertyReader, {}, {}, false, configFileErrors); + return configFileErrors; + } + if (this.exceededTotalSizeLimitForNonTsFiles(projectOptions.compilerOptions, projectOptions.files, fileNamePropertyReader)) { + project.setCompilerOptions(projectOptions.compilerOptions); + if (!project.languageServiceEnabled) { + return; } + project.disableLanguageService(); + project.stopWatchingDirectory(); } - fileNamesInProject = highPriorityFiles.concat(mediumPriorityFiles).concat(lowPriorityFiles).concat(veryLowPriorityFiles); - if (fileNamesInProject.length > 0) { - var checkList = fileNamesInProject.map(function (fileName) { - var normalizedFileName = ts.normalizePath(fileName); - return { fileName: normalizedFileName, project: project }; - }); - this.updateErrorCheck(checkList, this.changeSeq, function (n) { return n == _this.changeSeq; }, delay, 200, false); + else { + if (!project.languageServiceEnabled) { + project.enableLanguageService(); + } + this.watchConfigDirectoryForProject(project, projectOptions); + this.updateNonInferredProject(project, projectOptions.files, fileNamePropertyReader, projectOptions.compilerOptions, projectOptions.typingOptions, projectOptions.compileOnSave, configFileErrors); } }; - Session.prototype.getCanonicalFileName = function (fileName) { - var name = this.host.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase(); - return ts.normalizePath(name); - }; - Session.prototype.exit = function () { - }; - Session.prototype.requiredResponse = function (response) { - return { response: response, responseRequired: true }; - }; - Session.prototype.addProtocolHandler = function (command, handler) { - if (command in this.handlers) { - throw new Error("Protocol handler already exists for command \"" + command + "\""); + ProjectService.prototype.createInferredProjectWithRootFileIfNecessary = function (root) { + var _this = this; + var useExistingProject = this.useSingleInferredProject && this.inferredProjects.length; + var project = useExistingProject + ? this.inferredProjects[0] + : new server.InferredProject(this, this.documentRegistry, true, this.compilerOptionsForInferredProjects, this.compileOnSaveForInferredProjects); + project.addRoot(root); + this.directoryWatchers.startWatchingContainingDirectoriesForFile(root.fileName, project, function (fileName) { return _this.onConfigFileAddedForInferredProject(fileName); }); + project.updateGraph(); + if (!useExistingProject) { + this.inferredProjects.push(project); } - this.handlers[command] = handler; + return project; }; - Session.prototype.executeCommand = function (request) { - var handler = this.handlers[request.command]; - if (handler) { - return handler(request); - } - else { - this.projectService.log("Unrecognized JSON command: " + JSON.stringify(request)); - this.output(undefined, CommandNames.Unknown, request.seq, "Unrecognized JSON command: " + request.command); - return { responseRequired: false }; - } + ProjectService.prototype.getOrCreateScriptInfo = function (uncheckedFileName, openedByClient, fileContent, scriptKind) { + return this.getOrCreateScriptInfoForNormalizedPath(server.toNormalizedPath(uncheckedFileName), openedByClient, fileContent, scriptKind); }; - Session.prototype.onMessage = function (message) { - var start; - if (this.logger.isVerbose()) { - this.logger.info("request: " + message); - start = this.hrtime(); - } - var request; - try { - request = JSON.parse(message); - var _a = this.executeCommand(request), response = _a.response, responseRequired = _a.responseRequired; - if (this.logger.isVerbose()) { - var elapsed = this.hrtime(start); - var seconds = elapsed[0]; - var nanoseconds = elapsed[1]; - var elapsedMs = ((1e9 * seconds) + nanoseconds) / 1000000.0; - var leader = "Elapsed time (in milliseconds)"; - if (!responseRequired) { - leader = "Async elapsed time (in milliseconds)"; - } - this.logger.msg(leader + ": " + elapsedMs.toFixed(4).toString(), "Perf"); + ProjectService.prototype.getScriptInfo = function (uncheckedFileName) { + return this.getScriptInfoForNormalizedPath(server.toNormalizedPath(uncheckedFileName)); + }; + ProjectService.prototype.getOrCreateScriptInfoForNormalizedPath = function (fileName, openedByClient, fileContent, scriptKind, hasMixedContent) { + var _this = this; + var info = this.getScriptInfoForNormalizedPath(fileName); + if (!info) { + var content = void 0; + if (this.host.fileExists(fileName)) { + content = fileContent || (hasMixedContent ? "" : this.host.readFile(fileName)); } - if (response) { - this.output(response, request.command, request.seq); + if (!content) { + if (openedByClient) { + content = ""; + } } - else if (responseRequired) { - this.output(undefined, request.command, request.seq, "No content available."); + if (content !== undefined) { + info = new server.ScriptInfo(this.host, fileName, content, scriptKind, openedByClient, hasMixedContent); + this.filenameToScriptInfo.set(info.path, info); + if (!info.isOpen && !hasMixedContent) { + info.setWatcher(this.host.watchFile(fileName, function (_) { return _this.onSourceFileChanged(fileName); })); + } } } - catch (err) { - if (err instanceof ts.OperationCanceledException) { + if (info) { + if (fileContent !== undefined) { + info.reload(fileContent); + } + if (openedByClient) { + info.isOpen = true; } - this.logError(err, message); - this.output(undefined, request ? request.command : CommandNames.Unknown, request ? request.seq : 0, "Error processing request. " + err.message + "\n" + err.stack); - } - }; - return Session; - }()); - server.Session = Session; - })(server = ts.server || (ts.server = {})); -})(ts || (ts = {})); -var ts; -(function (ts) { - var server; - (function (server) { - var lineCollectionCapacity = 4; - function mergeFormatOptions(formatCodeOptions, formatOptions) { - var hasOwnProperty = Object.prototype.hasOwnProperty; - Object.keys(formatOptions).forEach(function (key) { - var codeKey = key.charAt(0).toUpperCase() + key.substring(1); - if (hasOwnProperty.call(formatCodeOptions, codeKey)) { - formatCodeOptions[codeKey] = formatOptions[key]; - } - }); - } - server.maxProgramSizeForNonTsFiles = 20 * 1024 * 1024; - var ScriptInfo = (function () { - function ScriptInfo(host, fileName, content, isOpen) { - if (isOpen === void 0) { isOpen = false; } - this.host = host; - this.fileName = fileName; - this.isOpen = isOpen; - this.children = []; - this.formatCodeOptions = ts.clone(CompilerService.getDefaultFormatCodeOptions(this.host)); - this.path = ts.toPath(fileName, host.getCurrentDirectory(), ts.createGetCanonicalFileName(host.useCaseSensitiveFileNames)); - this.svc = ScriptVersionCache.fromString(host, content); - } - ScriptInfo.prototype.setFormatOptions = function (formatOptions) { - if (formatOptions) { - mergeFormatOptions(this.formatCodeOptions, formatOptions); } + return info; }; - ScriptInfo.prototype.close = function () { - this.isOpen = false; - }; - ScriptInfo.prototype.addChild = function (childInfo) { - this.children.push(childInfo); - }; - ScriptInfo.prototype.snap = function () { - return this.svc.getSnapshot(); - }; - ScriptInfo.prototype.getText = function () { - var snap = this.snap(); - return snap.getText(0, snap.getLength()); - }; - ScriptInfo.prototype.getLineInfo = function (line) { - var snap = this.snap(); - return snap.index.lineNumberToInfo(line); - }; - ScriptInfo.prototype.editContent = function (start, end, newText) { - this.svc.edit(start, end - start, newText); - }; - ScriptInfo.prototype.getTextChangeRangeBetweenVersions = function (startVersion, endVersion) { - return this.svc.getTextChangesBetweenVersions(startVersion, endVersion); + ProjectService.prototype.getScriptInfoForNormalizedPath = function (fileName) { + return this.getScriptInfoForPath(server.normalizedPathToPath(fileName, this.host.getCurrentDirectory(), this.toCanonicalFileName)); }; - ScriptInfo.prototype.getChangeRange = function (oldSnapshot) { - return this.snap().getChangeRange(oldSnapshot); + ProjectService.prototype.getScriptInfoForPath = function (fileName) { + return this.filenameToScriptInfo.get(fileName); }; - return ScriptInfo; - }()); - server.ScriptInfo = ScriptInfo; - var LSHost = (function () { - function LSHost(host, project) { - var _this = this; - this.host = host; - this.project = project; - this.roots = []; - this.getCanonicalFileName = ts.createGetCanonicalFileName(host.useCaseSensitiveFileNames); - this.resolvedModuleNames = ts.createFileMap(); - this.resolvedTypeReferenceDirectives = ts.createFileMap(); - this.filenameToScript = ts.createFileMap(); - this.moduleResolutionHost = { - fileExists: function (fileName) { return _this.fileExists(fileName); }, - readFile: function (fileName) { return _this.host.readFile(fileName); }, - directoryExists: function (directoryName) { return _this.host.directoryExists(directoryName); } - }; - if (this.host.realpath) { - this.moduleResolutionHost.realpath = function (path) { return _this.host.realpath(path); }; - } - } - LSHost.prototype.resolveNamesWithLocalCache = function (names, containingFile, cache, loader, getResult) { - var path = ts.toPath(containingFile, this.host.getCurrentDirectory(), this.getCanonicalFileName); - var currentResolutionsInFile = cache.get(path); - var newResolutions = ts.createMap(); - var resolvedModules = []; - var compilerOptions = this.getCompilationSettings(); - for (var _i = 0, names_3 = names; _i < names_3.length; _i++) { - var name_53 = names_3[_i]; - var resolution = newResolutions[name_53]; - if (!resolution) { - var existingResolution = currentResolutionsInFile && currentResolutionsInFile[name_53]; - if (moduleResolutionIsValid(existingResolution)) { - resolution = existingResolution; - } - else { - resolution = loader(name_53, containingFile, compilerOptions, this.moduleResolutionHost); - resolution.lastCheckTime = Date.now(); - newResolutions[name_53] = resolution; - } + ProjectService.prototype.setHostConfiguration = function (args) { + if (args.file) { + var info = this.getScriptInfoForNormalizedPath(server.toNormalizedPath(args.file)); + if (info) { + info.setFormatOptions(args.formatOptions); + this.logger.info("Host configuration update for file " + args.file); } - ts.Debug.assert(resolution !== undefined); - resolvedModules.push(getResult(resolution)); } - cache.set(path, newResolutions); - return resolvedModules; - function moduleResolutionIsValid(resolution) { - if (!resolution) { - return false; + else { + if (args.hostInfo !== undefined) { + this.hostConfiguration.hostInfo = args.hostInfo; + this.logger.info("Host information " + args.hostInfo); } - if (getResult(resolution)) { - return true; + if (args.formatOptions) { + server.mergeMaps(this.hostConfiguration.formatCodeOptions, args.formatOptions); + this.logger.info("Format host information updated"); } - return resolution.failedLookupLocations.length === 0; } }; - LSHost.prototype.resolveTypeReferenceDirectives = function (typeDirectiveNames, containingFile) { - return this.resolveNamesWithLocalCache(typeDirectiveNames, containingFile, this.resolvedTypeReferenceDirectives, ts.resolveTypeReferenceDirective, function (m) { return m.resolvedTypeReferenceDirective; }); - }; - LSHost.prototype.resolveModuleNames = function (moduleNames, containingFile) { - return this.resolveNamesWithLocalCache(moduleNames, containingFile, this.resolvedModuleNames, ts.resolveModuleName, function (m) { return m.resolvedModule; }); - }; - LSHost.prototype.getDefaultLibFileName = function () { - var nodeModuleBinDir = ts.getDirectoryPath(ts.normalizePath(this.host.getExecutingFilePath())); - return ts.combinePaths(nodeModuleBinDir, ts.getDefaultLibFileName(this.compilationSettings)); + ProjectService.prototype.closeLog = function () { + this.logger.close(); }; - LSHost.prototype.getScriptSnapshot = function (filename) { - var scriptInfo = this.getScriptInfo(filename); - if (scriptInfo) { - return scriptInfo.snap(); + ProjectService.prototype.reloadProjects = function () { + this.logger.info("reload projects."); + for (var _i = 0, _a = this.openFiles; _i < _a.length; _i++) { + var info = _a[_i]; + this.openOrUpdateConfiguredProjectForFile(info.fileName); } + this.refreshInferredProjects(); }; - LSHost.prototype.setCompilationSettings = function (opt) { - this.compilationSettings = opt; - this.resolvedModuleNames.clear(); - this.resolvedTypeReferenceDirectives.clear(); - }; - LSHost.prototype.lineAffectsRefs = function (filename, line) { - var info = this.getScriptInfo(filename); - var lineInfo = info.getLineInfo(line); - if (lineInfo && lineInfo.text) { - var regex = /reference|import|\/\*|\*\//; - return regex.test(lineInfo.text); + ProjectService.prototype.refreshInferredProjects = function () { + this.logger.info("updating project structure from ..."); + this.printProjects(); + var orphantedFiles = []; + for (var _i = 0, _a = this.openFiles; _i < _a.length; _i++) { + var info = _a[_i]; + if (info.containingProjects.length === 0) { + orphantedFiles.push(info); + } + else { + if (isRootFileInInferredProject(info) && info.containingProjects.length > 1) { + var inferredProject = info.containingProjects[0]; + ts.Debug.assert(inferredProject.projectKind === server.ProjectKind.Inferred); + inferredProject.removeFile(info); + if (!inferredProject.hasRoots()) { + this.removeProject(inferredProject); + } + } + } } - }; - LSHost.prototype.getCompilationSettings = function () { - return this.compilationSettings; - }; - LSHost.prototype.getScriptFileNames = function () { - return this.roots.map(function (root) { return root.fileName; }); - }; - LSHost.prototype.getScriptKind = function (fileName) { - var info = this.getScriptInfo(fileName); - if (!info) { - return undefined; + for (var _b = 0, orphantedFiles_1 = orphantedFiles; _b < orphantedFiles_1.length; _b++) { + var f = orphantedFiles_1[_b]; + this.assignScriptInfoToInferredProjectIfNecessary(f, false); } - if (!info.scriptKind) { - info.scriptKind = ts.getScriptKindFromFileName(fileName); + for (var _c = 0, _d = this.inferredProjects; _c < _d.length; _c++) { + var p = _d[_c]; + p.updateGraph(); } - return info.scriptKind; + this.printProjects(); }; - LSHost.prototype.getScriptVersion = function (filename) { - return this.getScriptInfo(filename).svc.latestVersion().toString(); + ProjectService.prototype.openClientFile = function (fileName, fileContent, scriptKind) { + return this.openClientFileWithNormalizedPath(server.toNormalizedPath(fileName), fileContent, scriptKind); }; - LSHost.prototype.getCurrentDirectory = function () { - return ""; + ProjectService.prototype.openClientFileWithNormalizedPath = function (fileName, fileContent, scriptKind, hasMixedContent) { + var _a = this.findContainingExternalProject(fileName) + ? {} + : this.openOrUpdateConfiguredProjectForFile(fileName), _b = _a.configFileName, configFileName = _b === void 0 ? undefined : _b, _c = _a.configFileErrors, configFileErrors = _c === void 0 ? undefined : _c; + var info = this.getOrCreateScriptInfoForNormalizedPath(fileName, true, fileContent, scriptKind, hasMixedContent); + this.assignScriptInfoToInferredProjectIfNecessary(info, true); + this.printProjects(); + return { configFileName: configFileName, configFileErrors: configFileErrors }; }; - LSHost.prototype.getScriptIsOpen = function (filename) { - return this.getScriptInfo(filename).isOpen; + ProjectService.prototype.closeClientFile = function (uncheckedFileName) { + var info = this.getScriptInfoForNormalizedPath(server.toNormalizedPath(uncheckedFileName)); + if (info) { + this.closeOpenFile(info); + info.isOpen = false; + } + this.printProjects(); }; - LSHost.prototype.removeReferencedFile = function (info) { - if (!info.isOpen) { - this.filenameToScript.remove(info.path); - this.resolvedModuleNames.remove(info.path); - this.resolvedTypeReferenceDirectives.remove(info.path); + ProjectService.prototype.collectChanges = function (lastKnownProjectVersions, currentProjects, result) { + var _loop_3 = function (proj) { + var knownProject = ts.forEach(lastKnownProjectVersions, function (p) { return p.projectName === proj.getProjectName() && p; }); + result.push(proj.getChangesSinceVersion(knownProject && knownProject.version)); + }; + for (var _i = 0, currentProjects_1 = currentProjects; _i < currentProjects_1.length; _i++) { + var proj = currentProjects_1[_i]; + _loop_3(proj); } }; - LSHost.prototype.getScriptInfo = function (filename) { - var path = ts.toPath(filename, this.host.getCurrentDirectory(), this.getCanonicalFileName); - var scriptInfo = this.filenameToScript.get(path); - if (!scriptInfo) { - scriptInfo = this.project.openReferencedFile(filename); - if (scriptInfo) { - this.filenameToScript.set(path, scriptInfo); + ProjectService.prototype.synchronizeProjectList = function (knownProjects) { + var files = []; + this.collectChanges(knownProjects, this.externalProjects, files); + this.collectChanges(knownProjects, this.configuredProjects, files); + this.collectChanges(knownProjects, this.inferredProjects, files); + return files; + }; + ProjectService.prototype.applyChangesInOpenFiles = function (openFiles, changedFiles, closedFiles) { + var recordChangedFiles = changedFiles && !openFiles && !closedFiles; + if (openFiles) { + for (var _i = 0, openFiles_1 = openFiles; _i < openFiles_1.length; _i++) { + var file = openFiles_1[_i]; + var scriptInfo = this.getScriptInfo(file.fileName); + ts.Debug.assert(!scriptInfo || !scriptInfo.isOpen); + var normalizedPath = scriptInfo ? scriptInfo.fileName : server.toNormalizedPath(file.fileName); + this.openClientFileWithNormalizedPath(normalizedPath, file.content, file.scriptKind, file.hasMixedContent); + } + } + if (changedFiles) { + for (var _a = 0, changedFiles_1 = changedFiles; _a < changedFiles_1.length; _a++) { + var file = changedFiles_1[_a]; + var scriptInfo = this.getScriptInfo(file.fileName); + ts.Debug.assert(!!scriptInfo); + for (var i = file.changes.length - 1; i >= 0; i--) { + var change = file.changes[i]; + scriptInfo.editContent(change.span.start, change.span.start + change.span.length, change.newText); + } + if (recordChangedFiles) { + if (!this.changedFiles) { + this.changedFiles = [scriptInfo]; + } + else if (this.changedFiles.indexOf(scriptInfo) < 0) { + this.changedFiles.push(scriptInfo); + } + } } } - return scriptInfo; - }; - LSHost.prototype.addRoot = function (info) { - if (!this.filenameToScript.contains(info.path)) { - this.filenameToScript.set(info.path, info); - this.roots.push(info); + if (closedFiles) { + for (var _b = 0, closedFiles_1 = closedFiles; _b < closedFiles_1.length; _b++) { + var file = closedFiles_1[_b]; + this.closeClientFile(file); + } } - }; - LSHost.prototype.removeRoot = function (info) { - if (this.filenameToScript.contains(info.path)) { - this.filenameToScript.remove(info.path); - ts.unorderedRemoveItem(this.roots, info); - this.resolvedModuleNames.remove(info.path); - this.resolvedTypeReferenceDirectives.remove(info.path); + if (openFiles || closedFiles) { + this.refreshInferredProjects(); } }; - LSHost.prototype.saveTo = function (filename, tmpfilename) { - var script = this.getScriptInfo(filename); - if (script) { - var snap = script.snap(); - this.host.writeFile(tmpfilename, snap.getText(0, snap.getLength())); + ProjectService.prototype.closeConfiguredProject = function (configFile) { + var configuredProject = this.findConfiguredProjectByProjectName(configFile); + if (configuredProject && configuredProject.deleteOpenRef() === 0) { + this.removeProject(configuredProject); } }; - LSHost.prototype.reloadScript = function (filename, tmpfilename, cb) { - var script = this.getScriptInfo(filename); - if (script) { - script.svc.reloadFromFile(tmpfilename, cb); + ProjectService.prototype.closeExternalProject = function (uncheckedFileName, suppressRefresh) { + if (suppressRefresh === void 0) { suppressRefresh = false; } + var fileName = server.toNormalizedPath(uncheckedFileName); + var configFiles = this.externalProjectToConfiguredProjectMap[fileName]; + if (configFiles) { + var shouldRefreshInferredProjects = false; + for (var _i = 0, configFiles_1 = configFiles; _i < configFiles_1.length; _i++) { + var configFile = configFiles_1[_i]; + if (this.closeConfiguredProject(configFile)) { + shouldRefreshInferredProjects = true; + } + } + delete this.externalProjectToConfiguredProjectMap[fileName]; + if (shouldRefreshInferredProjects && !suppressRefresh) { + this.refreshInferredProjects(); + } } - }; - LSHost.prototype.editScript = function (filename, start, end, newText) { - var script = this.getScriptInfo(filename); - if (script) { - script.editContent(start, end, newText); - return; + else { + var externalProject = this.findExternalProjectByProjectName(uncheckedFileName); + if (externalProject) { + this.removeProject(externalProject); + if (!suppressRefresh) { + this.refreshInferredProjects(); + } + } } - throw new Error("No script with name '" + filename + "'"); - }; - LSHost.prototype.fileExists = function (path) { - var result = this.host.fileExists(path); - return result; - }; - LSHost.prototype.directoryExists = function (path) { - return this.host.directoryExists(path); - }; - LSHost.prototype.getDirectories = function (path) { - return this.host.getDirectories(path); - }; - LSHost.prototype.readDirectory = function (path, extensions, exclude, include) { - return this.host.readDirectory(path, extensions, exclude, include); - }; - LSHost.prototype.readFile = function (path, encoding) { - return this.host.readFile(path, encoding); }; - LSHost.prototype.lineToTextSpan = function (filename, line) { - var path = ts.toPath(filename, this.host.getCurrentDirectory(), this.getCanonicalFileName); - var script = this.filenameToScript.get(path); - var index = script.snap().index; - var lineInfo = index.lineNumberToInfo(line + 1); - var len; - if (lineInfo.leaf) { - len = lineInfo.leaf.text.length; + ProjectService.prototype.openExternalProject = function (proj) { + var tsConfigFiles; + var rootFiles = []; + for (var _i = 0, _a = proj.rootFiles; _i < _a.length; _i++) { + var file = _a[_i]; + var normalized = server.toNormalizedPath(file.fileName); + if (ts.getBaseFileName(normalized) === "tsconfig.json") { + (tsConfigFiles || (tsConfigFiles = [])).push(normalized); + } + else { + rootFiles.push(file); + } + } + if (tsConfigFiles) { + tsConfigFiles.sort(); + } + var externalProject = this.findExternalProjectByProjectName(proj.projectFileName); + var exisingConfigFiles; + if (externalProject) { + if (!tsConfigFiles) { + this.updateNonInferredProject(externalProject, proj.rootFiles, externalFilePropertyReader, proj.options, proj.typingOptions, proj.options.compileOnSave, undefined); + return; + } + this.closeExternalProject(proj.projectFileName, true); + } + else if (this.externalProjectToConfiguredProjectMap[proj.projectFileName]) { + if (!tsConfigFiles) { + this.closeExternalProject(proj.projectFileName, true); + } + else { + var oldConfigFiles = this.externalProjectToConfiguredProjectMap[proj.projectFileName]; + var iNew = 0; + var iOld = 0; + while (iNew < tsConfigFiles.length && iOld < oldConfigFiles.length) { + var newConfig = tsConfigFiles[iNew]; + var oldConfig = oldConfigFiles[iOld]; + if (oldConfig < newConfig) { + this.closeConfiguredProject(oldConfig); + iOld++; + } + else if (oldConfig > newConfig) { + iNew++; + } + else { + (exisingConfigFiles || (exisingConfigFiles = [])).push(oldConfig); + iOld++; + iNew++; + } + } + for (var i = iOld; i < oldConfigFiles.length; i++) { + this.closeConfiguredProject(oldConfigFiles[i]); + } + } + } + if (tsConfigFiles) { + this.externalProjectToConfiguredProjectMap[proj.projectFileName] = tsConfigFiles; + for (var _b = 0, tsConfigFiles_1 = tsConfigFiles; _b < tsConfigFiles_1.length; _b++) { + var tsconfigFile = tsConfigFiles_1[_b]; + var project = this.findConfiguredProjectByProjectName(tsconfigFile); + if (!project) { + var result = this.openConfigFile(tsconfigFile); + project = result.success && result.project; + } + if (project && !ts.contains(exisingConfigFiles, tsconfigFile)) { + project.addOpenRef(); + } + } } else { - var nextLineInfo = index.lineNumberToInfo(line + 2); - len = nextLineInfo.offset - lineInfo.offset; + delete this.externalProjectToConfiguredProjectMap[proj.projectFileName]; + this.createAndAddExternalProject(proj.projectFileName, rootFiles, proj.options, proj.typingOptions); } - return ts.createTextSpan(lineInfo.offset, len); - }; - LSHost.prototype.lineOffsetToPosition = function (filename, line, offset) { - var path = ts.toPath(filename, this.host.getCurrentDirectory(), this.getCanonicalFileName); - var script = this.filenameToScript.get(path); - var index = script.snap().index; - var lineInfo = index.lineNumberToInfo(line); - return (lineInfo.offset + offset - 1); - }; - LSHost.prototype.positionToLineOffset = function (filename, position, lineIndex) { - lineIndex = lineIndex || this.getLineIndex(filename); - var lineOffset = lineIndex.charOffsetToLineNumberAndPos(position); - return { line: lineOffset.line, offset: lineOffset.offset + 1 }; - }; - LSHost.prototype.getLineIndex = function (filename) { - var path = ts.toPath(filename, this.host.getCurrentDirectory(), this.getCanonicalFileName); - var script = this.filenameToScript.get(path); - return script.snap().index; + this.refreshInferredProjects(); }; - return LSHost; + return ProjectService; }()); - server.LSHost = LSHost; - var Project = (function () { - function Project(projectService, projectOptions, languageServiceDiabled) { - if (languageServiceDiabled === void 0) { languageServiceDiabled = false; } - this.projectService = projectService; - this.projectOptions = projectOptions; - this.languageServiceDiabled = languageServiceDiabled; - this.directoriesWatchedForTsconfig = []; - this.filenameToSourceFile = ts.createMap(); - this.updateGraphSeq = 0; - this.openRefCount = 0; - if (projectOptions && projectOptions.files) { - projectOptions.compilerOptions.allowNonTsExtensions = true; + server.ProjectService = ProjectService; + })(server = ts.server || (ts.server = {})); +})(ts || (ts = {})); +var ts; +(function (ts) { + var server; + (function (server) { + function hrTimeToMilliseconds(time) { + var seconds = time[0]; + var nanoseconds = time[1]; + return ((1e9 * seconds) + nanoseconds) / 1000000.0; + } + function shouldSkipSematicCheck(project) { + if (project.getCompilerOptions().skipLibCheck !== undefined) { + return false; + } + if ((project.projectKind === server.ProjectKind.Inferred || project.projectKind === server.ProjectKind.External) && project.isJsOnlyProject()) { + return true; + } + return false; + } + function compareNumber(a, b) { + return a - b; + } + function compareFileStart(a, b) { + if (a.file < b.file) { + return -1; + } + else if (a.file == b.file) { + var n = compareNumber(a.start.line, b.start.line); + if (n === 0) { + return compareNumber(a.start.offset, b.start.offset); } - if (!languageServiceDiabled) { - this.compilerService = new CompilerService(this, projectOptions && projectOptions.compilerOptions); + else + return n; + } + else { + return 1; + } + } + function formatDiag(fileName, project, diag) { + var scriptInfo = project.getScriptInfoForNormalizedPath(fileName); + return { + start: scriptInfo.positionToLineOffset(diag.start), + end: scriptInfo.positionToLineOffset(diag.start + diag.length), + text: ts.flattenDiagnosticMessageText(diag.messageText, "\n"), + code: diag.code + }; + } + function formatConfigFileDiag(diag) { + return { + start: undefined, + end: undefined, + text: ts.flattenDiagnosticMessageText(diag.messageText, "\n") + }; + } + function allEditsBeforePos(edits, pos) { + for (var _i = 0, edits_1 = edits; _i < edits_1.length; _i++) { + var edit = edits_1[_i]; + if (ts.textSpanEnd(edit.span) >= pos) { + return false; } } - Project.prototype.enableLanguageService = function () { - if (this.languageServiceDiabled) { - this.compilerService = new CompilerService(this, this.projectOptions && this.projectOptions.compilerOptions); + return true; + } + var CommandNames; + (function (CommandNames) { + CommandNames.Brace = "brace"; + CommandNames.BraceFull = "brace-full"; + CommandNames.BraceCompletion = "braceCompletion"; + CommandNames.Change = "change"; + CommandNames.Close = "close"; + CommandNames.Completions = "completions"; + CommandNames.CompletionsFull = "completions-full"; + CommandNames.CompletionDetails = "completionEntryDetails"; + CommandNames.CompileOnSaveAffectedFileList = "compileOnSaveAffectedFileList"; + CommandNames.CompileOnSaveEmitFile = "compileOnSaveEmitFile"; + CommandNames.Configure = "configure"; + CommandNames.Definition = "definition"; + CommandNames.DefinitionFull = "definition-full"; + CommandNames.Exit = "exit"; + CommandNames.Format = "format"; + CommandNames.Formatonkey = "formatonkey"; + CommandNames.FormatFull = "format-full"; + CommandNames.FormatonkeyFull = "formatonkey-full"; + CommandNames.FormatRangeFull = "formatRange-full"; + CommandNames.Geterr = "geterr"; + CommandNames.GeterrForProject = "geterrForProject"; + CommandNames.Implementation = "implementation"; + CommandNames.ImplementationFull = "implementation-full"; + CommandNames.SemanticDiagnosticsSync = "semanticDiagnosticsSync"; + CommandNames.SyntacticDiagnosticsSync = "syntacticDiagnosticsSync"; + CommandNames.NavBar = "navbar"; + CommandNames.NavBarFull = "navbar-full"; + CommandNames.NavTree = "navtree"; + CommandNames.NavTreeFull = "navtree-full"; + CommandNames.Navto = "navto"; + CommandNames.NavtoFull = "navto-full"; + CommandNames.Occurrences = "occurrences"; + CommandNames.DocumentHighlights = "documentHighlights"; + CommandNames.DocumentHighlightsFull = "documentHighlights-full"; + CommandNames.Open = "open"; + CommandNames.Quickinfo = "quickinfo"; + CommandNames.QuickinfoFull = "quickinfo-full"; + CommandNames.References = "references"; + CommandNames.ReferencesFull = "references-full"; + CommandNames.Reload = "reload"; + CommandNames.Rename = "rename"; + CommandNames.RenameInfoFull = "rename-full"; + CommandNames.RenameLocationsFull = "renameLocations-full"; + CommandNames.Saveto = "saveto"; + CommandNames.SignatureHelp = "signatureHelp"; + CommandNames.SignatureHelpFull = "signatureHelp-full"; + CommandNames.TypeDefinition = "typeDefinition"; + CommandNames.ProjectInfo = "projectInfo"; + CommandNames.ReloadProjects = "reloadProjects"; + CommandNames.Unknown = "unknown"; + CommandNames.OpenExternalProject = "openExternalProject"; + CommandNames.OpenExternalProjects = "openExternalProjects"; + CommandNames.CloseExternalProject = "closeExternalProject"; + CommandNames.SynchronizeProjectList = "synchronizeProjectList"; + CommandNames.ApplyChangedToOpenFiles = "applyChangedToOpenFiles"; + CommandNames.EncodedSemanticClassificationsFull = "encodedSemanticClassifications-full"; + CommandNames.Cleanup = "cleanup"; + CommandNames.OutliningSpans = "outliningSpans"; + CommandNames.TodoComments = "todoComments"; + CommandNames.Indentation = "indentation"; + CommandNames.DocCommentTemplate = "docCommentTemplate"; + CommandNames.CompilerOptionsDiagnosticsFull = "compilerOptionsDiagnostics-full"; + CommandNames.NameOrDottedNameSpan = "nameOrDottedNameSpan"; + CommandNames.BreakpointStatement = "breakpointStatement"; + CommandNames.CompilerOptionsForInferredProjects = "compilerOptionsForInferredProjects"; + CommandNames.GetCodeFixes = "getCodeFixes"; + CommandNames.GetCodeFixesFull = "getCodeFixes-full"; + CommandNames.GetSupportedCodeFixes = "getSupportedCodeFixes"; + })(CommandNames = server.CommandNames || (server.CommandNames = {})); + function formatMessage(msg, logger, byteLength, newLine) { + var verboseLogging = logger.hasLevel(server.LogLevel.verbose); + var json = JSON.stringify(msg); + if (verboseLogging) { + logger.info(msg.type + ": " + json); + } + var len = byteLength(json, "utf8"); + return "Content-Length: " + (1 + len) + "\r\n\r\n" + json + newLine; + } + server.formatMessage = formatMessage; + var Session = (function () { + function Session(host, cancellationToken, useSingleInferredProject, typingsInstaller, byteLength, hrtime, logger, canUseEvents, eventHandler) { + var _this = this; + this.host = host; + this.typingsInstaller = typingsInstaller; + this.byteLength = byteLength; + this.hrtime = hrtime; + this.logger = logger; + this.canUseEvents = canUseEvents; + this.changeSeq = 0; + this.handlers = ts.createMap((_a = {}, + _a[CommandNames.OpenExternalProject] = function (request) { + _this.projectService.openExternalProject(request.arguments); + return _this.requiredResponse(true); + }, + _a[CommandNames.OpenExternalProjects] = function (request) { + for (var _i = 0, _a = request.arguments.projects; _i < _a.length; _i++) { + var proj = _a[_i]; + _this.projectService.openExternalProject(proj); + } + return _this.requiredResponse(true); + }, + _a[CommandNames.CloseExternalProject] = function (request) { + _this.projectService.closeExternalProject(request.arguments.projectFileName); + return _this.requiredResponse(true); + }, + _a[CommandNames.SynchronizeProjectList] = function (request) { + var result = _this.projectService.synchronizeProjectList(request.arguments.knownProjects); + if (!result.some(function (p) { return p.projectErrors && p.projectErrors.length !== 0; })) { + return _this.requiredResponse(result); + } + var converted = ts.map(result, function (p) { + if (!p.projectErrors || p.projectErrors.length === 0) { + return p; + } + return { + info: p.info, + changes: p.changes, + files: p.files, + projectErrors: _this.convertToDiagnosticsWithLinePosition(p.projectErrors, undefined) + }; + }); + return _this.requiredResponse(converted); + }, + _a[CommandNames.ApplyChangedToOpenFiles] = function (request) { + _this.projectService.applyChangesInOpenFiles(request.arguments.openFiles, request.arguments.changedFiles, request.arguments.closedFiles); + _this.changeSeq++; + return _this.requiredResponse(true); + }, + _a[CommandNames.Exit] = function () { + _this.exit(); + return _this.notRequired(); + }, + _a[CommandNames.Definition] = function (request) { + return _this.requiredResponse(_this.getDefinition(request.arguments, true)); + }, + _a[CommandNames.DefinitionFull] = function (request) { + return _this.requiredResponse(_this.getDefinition(request.arguments, false)); + }, + _a[CommandNames.TypeDefinition] = function (request) { + return _this.requiredResponse(_this.getTypeDefinition(request.arguments)); + }, + _a[CommandNames.Implementation] = function (request) { + return _this.requiredResponse(_this.getImplementation(request.arguments, true)); + }, + _a[CommandNames.ImplementationFull] = function (request) { + return _this.requiredResponse(_this.getImplementation(request.arguments, false)); + }, + _a[CommandNames.References] = function (request) { + return _this.requiredResponse(_this.getReferences(request.arguments, true)); + }, + _a[CommandNames.ReferencesFull] = function (request) { + return _this.requiredResponse(_this.getReferences(request.arguments, false)); + }, + _a[CommandNames.Rename] = function (request) { + return _this.requiredResponse(_this.getRenameLocations(request.arguments, true)); + }, + _a[CommandNames.RenameLocationsFull] = function (request) { + return _this.requiredResponse(_this.getRenameLocations(request.arguments, false)); + }, + _a[CommandNames.RenameInfoFull] = function (request) { + return _this.requiredResponse(_this.getRenameInfo(request.arguments)); + }, + _a[CommandNames.Open] = function (request) { + var openArgs = request.arguments; + var scriptKind; + switch (openArgs.scriptKindName) { + case "TS": + scriptKind = 3; + break; + case "JS": + scriptKind = 1; + break; + case "TSX": + scriptKind = 4; + break; + case "JSX": + scriptKind = 2; + break; + } + _this.openClientFile(server.toNormalizedPath(openArgs.file), openArgs.fileContent, scriptKind); + return _this.notRequired(); + }, + _a[CommandNames.Quickinfo] = function (request) { + return _this.requiredResponse(_this.getQuickInfoWorker(request.arguments, true)); + }, + _a[CommandNames.QuickinfoFull] = function (request) { + return _this.requiredResponse(_this.getQuickInfoWorker(request.arguments, false)); + }, + _a[CommandNames.OutliningSpans] = function (request) { + return _this.requiredResponse(_this.getOutliningSpans(request.arguments)); + }, + _a[CommandNames.TodoComments] = function (request) { + return _this.requiredResponse(_this.getTodoComments(request.arguments)); + }, + _a[CommandNames.Indentation] = function (request) { + return _this.requiredResponse(_this.getIndentation(request.arguments)); + }, + _a[CommandNames.NameOrDottedNameSpan] = function (request) { + return _this.requiredResponse(_this.getNameOrDottedNameSpan(request.arguments)); + }, + _a[CommandNames.BreakpointStatement] = function (request) { + return _this.requiredResponse(_this.getBreakpointStatement(request.arguments)); + }, + _a[CommandNames.BraceCompletion] = function (request) { + return _this.requiredResponse(_this.isValidBraceCompletion(request.arguments)); + }, + _a[CommandNames.DocCommentTemplate] = function (request) { + return _this.requiredResponse(_this.getDocCommentTemplate(request.arguments)); + }, + _a[CommandNames.Format] = function (request) { + return _this.requiredResponse(_this.getFormattingEditsForRange(request.arguments)); + }, + _a[CommandNames.Formatonkey] = function (request) { + return _this.requiredResponse(_this.getFormattingEditsAfterKeystroke(request.arguments)); + }, + _a[CommandNames.FormatFull] = function (request) { + return _this.requiredResponse(_this.getFormattingEditsForDocumentFull(request.arguments)); + }, + _a[CommandNames.FormatonkeyFull] = function (request) { + return _this.requiredResponse(_this.getFormattingEditsAfterKeystrokeFull(request.arguments)); + }, + _a[CommandNames.FormatRangeFull] = function (request) { + return _this.requiredResponse(_this.getFormattingEditsForRangeFull(request.arguments)); + }, + _a[CommandNames.Completions] = function (request) { + return _this.requiredResponse(_this.getCompletions(request.arguments, true)); + }, + _a[CommandNames.CompletionsFull] = function (request) { + return _this.requiredResponse(_this.getCompletions(request.arguments, false)); + }, + _a[CommandNames.CompletionDetails] = function (request) { + return _this.requiredResponse(_this.getCompletionEntryDetails(request.arguments)); + }, + _a[CommandNames.CompileOnSaveAffectedFileList] = function (request) { + return _this.requiredResponse(_this.getCompileOnSaveAffectedFileList(request.arguments)); + }, + _a[CommandNames.CompileOnSaveEmitFile] = function (request) { + return _this.requiredResponse(_this.emitFile(request.arguments)); + }, + _a[CommandNames.SignatureHelp] = function (request) { + return _this.requiredResponse(_this.getSignatureHelpItems(request.arguments, true)); + }, + _a[CommandNames.SignatureHelpFull] = function (request) { + return _this.requiredResponse(_this.getSignatureHelpItems(request.arguments, false)); + }, + _a[CommandNames.CompilerOptionsDiagnosticsFull] = function (request) { + return _this.requiredResponse(_this.getCompilerOptionsDiagnostics(request.arguments)); + }, + _a[CommandNames.EncodedSemanticClassificationsFull] = function (request) { + return _this.requiredResponse(_this.getEncodedSemanticClassifications(request.arguments)); + }, + _a[CommandNames.Cleanup] = function (request) { + _this.cleanup(); + return _this.requiredResponse(true); + }, + _a[CommandNames.SemanticDiagnosticsSync] = function (request) { + return _this.requiredResponse(_this.getSemanticDiagnosticsSync(request.arguments)); + }, + _a[CommandNames.SyntacticDiagnosticsSync] = function (request) { + return _this.requiredResponse(_this.getSyntacticDiagnosticsSync(request.arguments)); + }, + _a[CommandNames.Geterr] = function (request) { + var geterrArgs = request.arguments; + return { response: _this.getDiagnostics(geterrArgs.delay, geterrArgs.files), responseRequired: false }; + }, + _a[CommandNames.GeterrForProject] = function (request) { + var _a = request.arguments, file = _a.file, delay = _a.delay; + return { response: _this.getDiagnosticsForProject(delay, file), responseRequired: false }; + }, + _a[CommandNames.Change] = function (request) { + _this.change(request.arguments); + return _this.notRequired(); + }, + _a[CommandNames.Configure] = function (request) { + _this.projectService.setHostConfiguration(request.arguments); + _this.output(undefined, CommandNames.Configure, request.seq); + return _this.notRequired(); + }, + _a[CommandNames.Reload] = function (request) { + _this.reload(request.arguments, request.seq); + return _this.requiredResponse({ reloadFinished: true }); + }, + _a[CommandNames.Saveto] = function (request) { + var savetoArgs = request.arguments; + _this.saveToTmp(savetoArgs.file, savetoArgs.tmpfile); + return _this.notRequired(); + }, + _a[CommandNames.Close] = function (request) { + var closeArgs = request.arguments; + _this.closeClientFile(closeArgs.file); + return _this.notRequired(); + }, + _a[CommandNames.Navto] = function (request) { + return _this.requiredResponse(_this.getNavigateToItems(request.arguments, true)); + }, + _a[CommandNames.NavtoFull] = function (request) { + return _this.requiredResponse(_this.getNavigateToItems(request.arguments, false)); + }, + _a[CommandNames.Brace] = function (request) { + return _this.requiredResponse(_this.getBraceMatching(request.arguments, true)); + }, + _a[CommandNames.BraceFull] = function (request) { + return _this.requiredResponse(_this.getBraceMatching(request.arguments, false)); + }, + _a[CommandNames.NavBar] = function (request) { + return _this.requiredResponse(_this.getNavigationBarItems(request.arguments, true)); + }, + _a[CommandNames.NavBarFull] = function (request) { + return _this.requiredResponse(_this.getNavigationBarItems(request.arguments, false)); + }, + _a[CommandNames.NavTree] = function (request) { + return _this.requiredResponse(_this.getNavigationTree(request.arguments, true)); + }, + _a[CommandNames.NavTreeFull] = function (request) { + return _this.requiredResponse(_this.getNavigationTree(request.arguments, false)); + }, + _a[CommandNames.Occurrences] = function (request) { + return _this.requiredResponse(_this.getOccurrences(request.arguments)); + }, + _a[CommandNames.DocumentHighlights] = function (request) { + return _this.requiredResponse(_this.getDocumentHighlights(request.arguments, true)); + }, + _a[CommandNames.DocumentHighlightsFull] = function (request) { + return _this.requiredResponse(_this.getDocumentHighlights(request.arguments, false)); + }, + _a[CommandNames.CompilerOptionsForInferredProjects] = function (request) { + return _this.requiredResponse(_this.setCompilerOptionsForInferredProjects(request.arguments)); + }, + _a[CommandNames.ProjectInfo] = function (request) { + return _this.requiredResponse(_this.getProjectInfo(request.arguments)); + }, + _a[CommandNames.ReloadProjects] = function (request) { + _this.projectService.reloadProjects(); + return _this.notRequired(); + }, + _a[CommandNames.GetCodeFixes] = function (request) { + return _this.requiredResponse(_this.getCodeFixes(request.arguments, true)); + }, + _a[CommandNames.GetCodeFixesFull] = function (request) { + return _this.requiredResponse(_this.getCodeFixes(request.arguments, false)); + }, + _a[CommandNames.GetSupportedCodeFixes] = function (request) { + return _this.requiredResponse(_this.getSupportedCodeFixes()); + }, + _a)); + this.eventHander = canUseEvents + ? eventHandler || (function (event) { return _this.defaultEventHandler(event); }) + : undefined; + this.projectService = new server.ProjectService(host, logger, cancellationToken, useSingleInferredProject, typingsInstaller, this.eventHander); + this.gcTimer = new server.GcTimer(host, 7000, logger); + var _a; + } + Session.prototype.defaultEventHandler = function (event) { + var _this = this; + switch (event.eventName) { + case "context": + var _a = event.data, project = _a.project, fileName = _a.fileName; + this.projectService.logger.info("got context event, updating diagnostics for " + fileName); + this.updateErrorCheck([{ fileName: fileName, project: project }], this.changeSeq, function (n) { return n === _this.changeSeq; }, 100); + break; + case "configFileDiag": + var _b = event.data, triggerFile = _b.triggerFile, configFileName = _b.configFileName, diagnostics = _b.diagnostics; + this.configFileDiagnosticEvent(triggerFile, configFileName, diagnostics); } - this.languageServiceDiabled = false; }; - Project.prototype.disableLanguageService = function () { - this.languageServiceDiabled = true; + Session.prototype.logError = function (err, cmd) { + var msg = "Exception on executing command " + cmd; + if (err.message) { + msg += ":\n" + err.message; + if (err.stack) { + msg += "\n" + err.stack; + } + } + this.logger.msg(msg, server.Msg.Err); }; - Project.prototype.addOpenRef = function () { - this.openRefCount++; + Session.prototype.send = function (msg) { + if (msg.type === "event" && !this.canUseEvents) { + if (this.logger.hasLevel(server.LogLevel.verbose)) { + this.logger.info("Session does not support events: ignored event: " + JSON.stringify(msg)); + } + return; + } + this.host.write(formatMessage(msg, this.logger, this.byteLength, this.host.newLine)); }; - Project.prototype.deleteOpenRef = function () { - this.openRefCount--; - return this.openRefCount; + Session.prototype.configFileDiagnosticEvent = function (triggerFile, configFile, diagnostics) { + var bakedDiags = ts.map(diagnostics, formatConfigFileDiag); + var ev = { + seq: 0, + type: "event", + event: "configFileDiag", + body: { + triggerFile: triggerFile, + configFile: configFile, + diagnostics: bakedDiags + } + }; + this.send(ev); }; - Project.prototype.openReferencedFile = function (filename) { - return this.projectService.openFile(filename, false); + Session.prototype.event = function (info, eventName) { + var ev = { + seq: 0, + type: "event", + event: eventName, + body: info + }; + this.send(ev); }; - Project.prototype.getRootFiles = function () { - if (this.languageServiceDiabled) { - return this.projectOptions ? this.projectOptions.files : undefined; + Session.prototype.output = function (info, cmdName, reqSeq, errorMsg) { + if (reqSeq === void 0) { reqSeq = 0; } + var res = { + seq: 0, + type: "response", + command: cmdName, + request_seq: reqSeq, + success: !errorMsg + }; + if (!errorMsg) { + res.body = info; + } + else { + res.message = errorMsg; } - return this.compilerService.host.roots.map(function (info) { return info.fileName; }); + this.send(res); }; - Project.prototype.getFileNames = function () { - if (this.languageServiceDiabled) { - if (!this.projectOptions) { - return undefined; - } - var fileNames = []; - if (this.projectOptions && this.projectOptions.compilerOptions) { - fileNames.push(ts.getDefaultLibFilePath(this.projectOptions.compilerOptions)); + Session.prototype.semanticCheck = function (file, project) { + try { + var diags = []; + if (!shouldSkipSematicCheck(project)) { + diags = project.getLanguageService().getSemanticDiagnostics(file); } - ts.addRange(fileNames, this.projectOptions.files); - return fileNames; + var bakedDiags = diags.map(function (diag) { return formatDiag(file, project, diag); }); + this.event({ file: file, diagnostics: bakedDiags }, "semanticDiag"); } - var sourceFiles = this.program.getSourceFiles(); - return sourceFiles.map(function (sourceFile) { return sourceFile.fileName; }); - }; - Project.prototype.getSourceFile = function (info) { - if (this.languageServiceDiabled) { - return undefined; + catch (err) { + this.logError(err, "semantic check"); } - return this.filenameToSourceFile[info.fileName]; }; - Project.prototype.getSourceFileFromName = function (filename, requireOpen) { - if (this.languageServiceDiabled) { - return undefined; - } - var info = this.projectService.getScriptInfo(filename); - if (info) { - if ((!requireOpen) || info.isOpen) { - return this.getSourceFile(info); + Session.prototype.syntacticCheck = function (file, project) { + try { + var diags = project.getLanguageService().getSyntacticDiagnostics(file); + if (diags) { + var bakedDiags = diags.map(function (diag) { return formatDiag(file, project, diag); }); + this.event({ file: file, diagnostics: bakedDiags }, "syntaxDiag"); } } - }; - Project.prototype.isRoot = function (info) { - if (this.languageServiceDiabled) { - return undefined; + catch (err) { + this.logError(err, "syntactic check"); } - return this.compilerService.host.roots.some(function (root) { return root === info; }); }; - Project.prototype.removeReferencedFile = function (info) { - if (this.languageServiceDiabled) { - return; - } - this.compilerService.host.removeReferencedFile(info); - this.updateGraph(); + Session.prototype.updateProjectStructure = function (seq, matchSeq, ms) { + var _this = this; + if (ms === void 0) { ms = 1500; } + this.host.setTimeout(function () { + if (matchSeq(seq)) { + _this.projectService.refreshInferredProjects(); + } + }, ms); }; - Project.prototype.updateFileMap = function () { - if (this.languageServiceDiabled) { - return; + Session.prototype.updateErrorCheck = function (checkList, seq, matchSeq, ms, followMs, requireOpen) { + var _this = this; + if (ms === void 0) { ms = 1500; } + if (followMs === void 0) { followMs = 200; } + if (requireOpen === void 0) { requireOpen = true; } + if (followMs > ms) { + followMs = ms; } - this.filenameToSourceFile = ts.createMap(); - var sourceFiles = this.program.getSourceFiles(); - for (var i = 0, len = sourceFiles.length; i < len; i++) { - var normFilename = ts.normalizePath(sourceFiles[i].fileName); - this.filenameToSourceFile[normFilename] = sourceFiles[i]; + if (this.errorTimer) { + this.host.clearTimeout(this.errorTimer); } - }; - Project.prototype.finishGraph = function () { - if (this.languageServiceDiabled) { - return; + if (this.immediateId) { + this.host.clearImmediate(this.immediateId); + this.immediateId = undefined; } - this.updateGraph(); - this.compilerService.languageService.getNavigateToItems(".*"); - }; - Project.prototype.updateGraph = function () { - if (this.languageServiceDiabled) { - return; + var index = 0; + var checkOne = function () { + if (matchSeq(seq)) { + var checkSpec_1 = checkList[index]; + index++; + if (checkSpec_1.project.containsFile(checkSpec_1.fileName, requireOpen)) { + _this.syntacticCheck(checkSpec_1.fileName, checkSpec_1.project); + _this.immediateId = _this.host.setImmediate(function () { + _this.semanticCheck(checkSpec_1.fileName, checkSpec_1.project); + _this.immediateId = undefined; + if (checkList.length > index) { + _this.errorTimer = _this.host.setTimeout(checkOne, followMs); + } + else { + _this.errorTimer = undefined; + } + }); + } + } + }; + if ((checkList.length > index) && (matchSeq(seq))) { + this.errorTimer = this.host.setTimeout(checkOne, ms); } - this.program = this.compilerService.languageService.getProgram(); - this.updateFileMap(); - }; - Project.prototype.isConfiguredProject = function () { - return this.projectFilename; }; - Project.prototype.addRoot = function (info) { - if (this.languageServiceDiabled) { + Session.prototype.cleanProjects = function (caption, projects) { + if (!projects) { return; } - this.compilerService.host.addRoot(info); - }; - Project.prototype.removeRoot = function (info) { - if (this.languageServiceDiabled) { - return; + this.logger.info("cleaning " + caption); + for (var _i = 0, projects_4 = projects; _i < projects_4.length; _i++) { + var p = projects_4[_i]; + p.getLanguageService(false).cleanupSemanticCache(); } - this.compilerService.host.removeRoot(info); }; - Project.prototype.filesToString = function () { - if (this.languageServiceDiabled) { - if (this.projectOptions) { - var strBuilder_1 = ""; - ts.forEach(this.projectOptions.files, function (file) { strBuilder_1 += file + "\n"; }); - return strBuilder_1; - } + Session.prototype.cleanup = function () { + this.cleanProjects("inferred projects", this.projectService.inferredProjects); + this.cleanProjects("configured projects", this.projectService.configuredProjects); + this.cleanProjects("external projects", this.projectService.externalProjects); + if (this.host.gc) { + this.logger.info("host.gc()"); + this.host.gc(); } - var strBuilder = ""; - ts.forEachProperty(this.filenameToSourceFile, function (sourceFile) { strBuilder += sourceFile.fileName + "\n"; }); - return strBuilder; }; - Project.prototype.setProjectOptions = function (projectOptions) { - this.projectOptions = projectOptions; - if (projectOptions.compilerOptions) { - projectOptions.compilerOptions.allowNonTsExtensions = true; - if (!this.languageServiceDiabled) { - this.compilerService.setCompilerOptions(projectOptions.compilerOptions); - } - } + Session.prototype.getEncodedSemanticClassifications = function (args) { + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + return project.getLanguageService().getEncodedSemanticClassifications(file, args); }; - return Project; - }()); - server.Project = Project; - function combineProjectOutput(projects, action, comparer, areEqual) { - var result = projects.reduce(function (previous, current) { return ts.concatenate(previous, action(current)); }, []).sort(comparer); - return projects.length > 1 ? ts.deduplicate(result, areEqual) : result; - } - server.combineProjectOutput = combineProjectOutput; - var ProjectService = (function () { - function ProjectService(host, psLogger, eventHandler) { - this.host = host; - this.psLogger = psLogger; - this.eventHandler = eventHandler; - this.filenameToScriptInfo = ts.createMap(); - this.openFileRoots = []; - this.inferredProjects = []; - this.configuredProjects = []; - this.openFilesReferenced = []; - this.openFileRootsConfigured = []; - this.directoryWatchersForTsconfig = ts.createMap(); - this.directoryWatchersRefCount = ts.createMap(); - this.timerForDetectingProjectFileListChanges = ts.createMap(); - this.addDefaultHostConfiguration(); - } - ProjectService.prototype.addDefaultHostConfiguration = function () { - this.hostConfiguration = { - formatCodeOptions: ts.clone(CompilerService.getDefaultFormatCodeOptions(this.host)), - hostInfo: "Unknown host" - }; + Session.prototype.getProject = function (projectFileName) { + return projectFileName && this.projectService.findProject(projectFileName); }; - ProjectService.prototype.getFormatCodeOptions = function (file) { - if (file) { - var info = this.filenameToScriptInfo[file]; - if (info) { - return info.formatCodeOptions; - } + Session.prototype.getCompilerOptionsDiagnostics = function (args) { + var project = this.getProject(args.projectFileName); + return this.convertToDiagnosticsWithLinePosition(project.getLanguageService().getCompilerOptionsDiagnostics(), undefined); + }; + Session.prototype.convertToDiagnosticsWithLinePosition = function (diagnostics, scriptInfo) { + var _this = this; + return diagnostics.map(function (d) { return ({ + message: ts.flattenDiagnosticMessageText(d.messageText, _this.host.newLine), + start: d.start, + length: d.length, + category: ts.DiagnosticCategory[d.category].toLowerCase(), + code: d.code, + startLocation: scriptInfo && scriptInfo.positionToLineOffset(d.start), + endLocation: scriptInfo && scriptInfo.positionToLineOffset(d.start + d.length) + }); }); + }; + Session.prototype.getDiagnosticsWorker = function (args, selector, includeLinePosition) { + var _a = this.getFileAndProject(args), project = _a.project, file = _a.file; + if (shouldSkipSematicCheck(project)) { + return []; } - return this.hostConfiguration.formatCodeOptions; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var diagnostics = selector(project, file); + return includeLinePosition + ? this.convertToDiagnosticsWithLinePosition(diagnostics, scriptInfo) + : diagnostics.map(function (d) { return formatDiag(file, project, d); }); }; - ProjectService.prototype.watchedFileChanged = function (fileName) { - var info = this.filenameToScriptInfo[fileName]; - if (!info) { - this.psLogger.info("Error: got watch notification for unknown file: " + fileName); + Session.prototype.getDefinition = function (args, simplifiedResult) { + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var position = this.getPosition(args, scriptInfo); + var definitions = project.getLanguageService().getDefinitionAtPosition(file, position); + if (!definitions) { + return undefined; } - if (!this.host.fileExists(fileName)) { - this.fileDeletedInFilesystem(info); + if (simplifiedResult) { + return definitions.map(function (def) { + var defScriptInfo = project.getScriptInfo(def.fileName); + return { + file: def.fileName, + start: defScriptInfo.positionToLineOffset(def.textSpan.start), + end: defScriptInfo.positionToLineOffset(ts.textSpanEnd(def.textSpan)) + }; + }); } else { - if (info && (!info.isOpen)) { - info.svc.reloadFromFile(info.fileName); - } + return definitions; } }; - ProjectService.prototype.directoryWatchedForSourceFilesChanged = function (project, fileName) { - if (fileName && !ts.isSupportedSourceFileName(fileName, project.projectOptions ? project.projectOptions.compilerOptions : undefined)) { - return; + Session.prototype.getTypeDefinition = function (args) { + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var position = this.getPosition(args, scriptInfo); + var definitions = project.getLanguageService().getTypeDefinitionAtPosition(file, position); + if (!definitions) { + return undefined; } - this.log("Detected source file changes: " + fileName); - this.startTimerForDetectingProjectFileListChanges(project); + return definitions.map(function (def) { + var defScriptInfo = project.getScriptInfo(def.fileName); + return { + file: def.fileName, + start: defScriptInfo.positionToLineOffset(def.textSpan.start), + end: defScriptInfo.positionToLineOffset(ts.textSpanEnd(def.textSpan)) + }; + }); }; - ProjectService.prototype.startTimerForDetectingProjectFileListChanges = function (project) { - var _this = this; - if (this.timerForDetectingProjectFileListChanges[project.projectFilename]) { - this.host.clearTimeout(this.timerForDetectingProjectFileListChanges[project.projectFilename]); + Session.prototype.getImplementation = function (args, simplifiedResult) { + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var position = this.getPosition(args, scriptInfo); + var implementations = project.getLanguageService().getImplementationAtPosition(file, position); + if (!implementations) { + return []; } - this.timerForDetectingProjectFileListChanges[project.projectFilename] = this.host.setTimeout(function () { return _this.handleProjectFileListChanges(project); }, 250); - }; - ProjectService.prototype.handleProjectFileListChanges = function (project) { - var _this = this; - var _a = this.configFileToProjectOptions(project.projectFilename), projectOptions = _a.projectOptions, errors = _a.errors; - this.reportConfigFileDiagnostics(project.projectFilename, errors); - var newRootFiles = projectOptions.files.map((function (f) { return _this.getCanonicalFileName(f); })); - var currentRootFiles = project.getRootFiles().map((function (f) { return _this.getCanonicalFileName(f); })); - if (!ts.arrayIsEqualTo(currentRootFiles && currentRootFiles.sort(), newRootFiles && newRootFiles.sort())) { - this.updateConfiguredProject(project); - this.updateProjectStructure(); + if (simplifiedResult) { + return implementations.map(function (impl) { return ({ + file: impl.fileName, + start: scriptInfo.positionToLineOffset(impl.textSpan.start), + end: scriptInfo.positionToLineOffset(ts.textSpanEnd(impl.textSpan)) + }); }); + } + else { + return implementations; } }; - ProjectService.prototype.reportConfigFileDiagnostics = function (configFileName, diagnostics, triggerFile) { - if (diagnostics && diagnostics.length > 0) { - this.eventHandler({ - eventName: "configFileDiag", - data: { configFileName: configFileName, diagnostics: diagnostics, triggerFile: triggerFile } - }); + Session.prototype.getOccurrences = function (args) { + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var position = this.getPosition(args, scriptInfo); + var occurrences = project.getLanguageService().getOccurrencesAtPosition(file, position); + if (!occurrences) { + return undefined; } + return occurrences.map(function (occurrence) { + var fileName = occurrence.fileName, isWriteAccess = occurrence.isWriteAccess, textSpan = occurrence.textSpan; + var scriptInfo = project.getScriptInfo(fileName); + var start = scriptInfo.positionToLineOffset(textSpan.start); + var end = scriptInfo.positionToLineOffset(ts.textSpanEnd(textSpan)); + return { + start: start, + end: end, + file: fileName, + isWriteAccess: isWriteAccess + }; + }); }; - ProjectService.prototype.directoryWatchedForTsconfigChanged = function (fileName) { - var _this = this; - if (ts.getBaseFileName(fileName) !== "tsconfig.json") { - this.log(fileName + " is not tsconfig.json"); - return; + Session.prototype.getSyntacticDiagnosticsSync = function (args) { + return this.getDiagnosticsWorker(args, function (project, file) { return project.getLanguageService().getSyntacticDiagnostics(file); }, args.includeLinePosition); + }; + Session.prototype.getSemanticDiagnosticsSync = function (args) { + return this.getDiagnosticsWorker(args, function (project, file) { return project.getLanguageService().getSemanticDiagnostics(file); }, args.includeLinePosition); + }; + Session.prototype.getDocumentHighlights = function (args, simplifiedResult) { + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var position = this.getPosition(args, scriptInfo); + var documentHighlights = project.getLanguageService().getDocumentHighlights(file, position, args.filesToSearch); + if (!documentHighlights) { + return undefined; } - this.log("Detected newly added tsconfig file: " + fileName); - var _a = this.configFileToProjectOptions(fileName), projectOptions = _a.projectOptions, errors = _a.errors; - this.reportConfigFileDiagnostics(fileName, errors); - if (!projectOptions) { - return; + if (simplifiedResult) { + return documentHighlights.map(convertToDocumentHighlightsItem); } - var rootFilesInTsconfig = projectOptions.files.map(function (f) { return _this.getCanonicalFileName(f); }); - var openFileRoots = this.openFileRoots.map(function (s) { return _this.getCanonicalFileName(s.fileName); }); - for (var _i = 0, openFileRoots_1 = openFileRoots; _i < openFileRoots_1.length; _i++) { - var openFileRoot = openFileRoots_1[_i]; - if (rootFilesInTsconfig.indexOf(openFileRoot) >= 0) { - this.reloadProjects(); - return; + else { + return documentHighlights; + } + function convertToDocumentHighlightsItem(documentHighlights) { + var fileName = documentHighlights.fileName, highlightSpans = documentHighlights.highlightSpans; + var scriptInfo = project.getScriptInfo(fileName); + return { + file: fileName, + highlightSpans: highlightSpans.map(convertHighlightSpan) + }; + function convertHighlightSpan(highlightSpan) { + var textSpan = highlightSpan.textSpan, kind = highlightSpan.kind; + var start = scriptInfo.positionToLineOffset(textSpan.start); + var end = scriptInfo.positionToLineOffset(ts.textSpanEnd(textSpan)); + return { start: start, end: end, kind: kind }; } } }; - ProjectService.prototype.getCanonicalFileName = function (fileName) { - var name = this.host.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase(); - return ts.normalizePath(name); + Session.prototype.setCompilerOptionsForInferredProjects = function (args) { + this.projectService.setCompilerOptionsForInferredProjects(args.options); }; - ProjectService.prototype.watchedProjectConfigFileChanged = function (project) { - this.log("Config file changed: " + project.projectFilename); - var configFileErrors = this.updateConfiguredProject(project); - this.updateProjectStructure(); - if (configFileErrors && configFileErrors.length > 0) { - this.eventHandler({ eventName: "configFileDiag", data: { triggerFile: project.projectFilename, configFileName: project.projectFilename, diagnostics: configFileErrors } }); - } + Session.prototype.getProjectInfo = function (args) { + return this.getProjectInfoWorker(args.file, args.projectFileName, args.needFileNameList); }; - ProjectService.prototype.log = function (msg, type) { - if (type === void 0) { type = "Err"; } - this.psLogger.msg(msg, type); + Session.prototype.getProjectInfoWorker = function (uncheckedFileName, projectFileName, needFileNameList) { + var project = this.getFileAndProjectWorker(uncheckedFileName, projectFileName, true, true).project; + var projectInfo = { + configFileName: project.getProjectName(), + languageServiceDisabled: !project.languageServiceEnabled, + fileNames: needFileNameList ? project.getFileNames() : undefined + }; + return projectInfo; }; - ProjectService.prototype.setHostConfiguration = function (args) { - if (args.file) { - var info = this.filenameToScriptInfo[args.file]; - if (info) { - info.setFormatOptions(args.formatOptions); - this.log("Host configuration update for file " + args.file, "Info"); + Session.prototype.getRenameInfo = function (args) { + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var position = this.getPosition(args, scriptInfo); + return project.getLanguageService().getRenameInfo(file, position); + }; + Session.prototype.getProjects = function (args) { + var projects; + if (args.projectFileName) { + var project = this.getProject(args.projectFileName); + if (project) { + projects = [project]; } } else { - if (args.hostInfo !== undefined) { - this.hostConfiguration.hostInfo = args.hostInfo; - this.log("Host information " + args.hostInfo, "Info"); - } - if (args.formatOptions) { - mergeFormatOptions(this.hostConfiguration.formatCodeOptions, args.formatOptions); - this.log("Format host information updated", "Info"); - } + var scriptInfo = this.projectService.getScriptInfo(args.file); + projects = scriptInfo.containingProjects; } + projects = ts.filter(projects, function (p) { return p.languageServiceEnabled; }); + if (!projects || !projects.length) { + return server.Errors.ThrowNoProject(); + } + return projects; }; - ProjectService.prototype.closeLog = function () { - this.psLogger.close(); - }; - ProjectService.prototype.createInferredProject = function (root) { - var _this = this; - var project = new Project(this); - project.addRoot(root); - var currentPath = ts.getDirectoryPath(root.fileName); - var parentPath = ts.getDirectoryPath(currentPath); - while (currentPath != parentPath) { - if (!project.projectService.directoryWatchersForTsconfig[currentPath]) { - this.log("Add watcher for: " + currentPath); - project.projectService.directoryWatchersForTsconfig[currentPath] = - this.host.watchDirectory(currentPath, function (fileName) { return _this.directoryWatchedForTsconfigChanged(fileName); }); - project.projectService.directoryWatchersRefCount[currentPath] = 1; + Session.prototype.getRenameLocations = function (args, simplifiedResult) { + var file = server.toNormalizedPath(args.file); + var info = this.projectService.getScriptInfoForNormalizedPath(file); + var position = this.getPosition(args, info); + var projects = this.getProjects(args); + if (simplifiedResult) { + var defaultProject = projects[0]; + var renameInfo = defaultProject.getLanguageService().getRenameInfo(file, position); + if (!renameInfo) { + return undefined; } - else { - project.projectService.directoryWatchersRefCount[currentPath] += 1; + if (!renameInfo.canRename) { + return { + info: renameInfo, + locs: [] + }; } - project.directoriesWatchedForTsconfig.push(currentPath); - currentPath = parentPath; - parentPath = ts.getDirectoryPath(parentPath); + var fileSpans = server.combineProjectOutput(projects, function (project) { + var renameLocations = project.getLanguageService().findRenameLocations(file, position, args.findInStrings, args.findInComments); + if (!renameLocations) { + return []; + } + return renameLocations.map(function (location) { + var locationScriptInfo = project.getScriptInfo(location.fileName); + return { + file: location.fileName, + start: locationScriptInfo.positionToLineOffset(location.textSpan.start), + end: locationScriptInfo.positionToLineOffset(ts.textSpanEnd(location.textSpan)) + }; + }); + }, compareRenameLocation, function (a, b) { return a.file === b.file && a.start.line === b.start.line && a.start.offset === b.start.offset; }); + var locs = fileSpans.reduce(function (accum, cur) { + var curFileAccum; + if (accum.length > 0) { + curFileAccum = accum[accum.length - 1]; + if (curFileAccum.file !== cur.file) { + curFileAccum = undefined; + } + } + if (!curFileAccum) { + curFileAccum = { file: cur.file, locs: [] }; + accum.push(curFileAccum); + } + curFileAccum.locs.push({ start: cur.start, end: cur.end }); + return accum; + }, []); + return { info: renameInfo, locs: locs }; } - project.finishGraph(); - this.inferredProjects.push(project); - return project; - }; - ProjectService.prototype.fileDeletedInFilesystem = function (info) { - this.psLogger.info(info.fileName + " deleted"); - if (info.fileWatcher) { - info.fileWatcher.close(); - info.fileWatcher = undefined; + else { + return server.combineProjectOutput(projects, function (p) { return p.getLanguageService().findRenameLocations(file, position, args.findInStrings, args.findInComments); }, undefined, renameLocationIsEqualTo); } - if (!info.isOpen) { - this.filenameToScriptInfo[info.fileName] = undefined; - var referencingProjects = this.findReferencingProjects(info); - if (info.defaultProject) { - info.defaultProject.removeRoot(info); + function renameLocationIsEqualTo(a, b) { + if (a === b) { + return true; } - for (var i = 0, len = referencingProjects.length; i < len; i++) { - referencingProjects[i].removeReferencedFile(info); + if (!a || !b) { + return false; } - for (var j = 0, flen = this.openFileRoots.length; j < flen; j++) { - var openFile = this.openFileRoots[j]; - if (this.eventHandler) { - this.eventHandler({ eventName: "context", data: { project: openFile.defaultProject, fileName: openFile.fileName } }); - } + return a.fileName === b.fileName && + a.textSpan.start === b.textSpan.start && + a.textSpan.length === b.textSpan.length; + } + function compareRenameLocation(a, b) { + if (a.file < b.file) { + return -1; + } + else if (a.file > b.file) { + return 1; } - for (var j = 0, flen = this.openFilesReferenced.length; j < flen; j++) { - var openFile = this.openFilesReferenced[j]; - if (this.eventHandler) { - this.eventHandler({ eventName: "context", data: { project: openFile.defaultProject, fileName: openFile.fileName } }); + else { + if (a.start.line < b.start.line) { + return 1; + } + else if (a.start.line > b.start.line) { + return -1; + } + else { + return b.start.offset - a.start.offset; } } } - this.printProjects(); }; - ProjectService.prototype.updateConfiguredProjectList = function () { - var configuredProjects = []; - for (var i = 0, len = this.configuredProjects.length; i < len; i++) { - if (this.configuredProjects[i].openRefCount > 0) { - configuredProjects.push(this.configuredProjects[i]); + Session.prototype.getReferences = function (args, simplifiedResult) { + var file = server.toNormalizedPath(args.file); + var projects = this.getProjects(args); + var defaultProject = projects[0]; + var scriptInfo = defaultProject.getScriptInfoForNormalizedPath(file); + var position = this.getPosition(args, scriptInfo); + if (simplifiedResult) { + var nameInfo = defaultProject.getLanguageService().getQuickInfoAtPosition(file, position); + if (!nameInfo) { + return undefined; } - } - this.configuredProjects = configuredProjects; - }; - ProjectService.prototype.removeProject = function (project) { - this.log("remove project: " + project.getRootFiles().toString()); - if (project.isConfiguredProject()) { - project.projectFileWatcher.close(); - project.directoryWatcher.close(); - ts.forEachProperty(project.directoriesWatchedForWildcards, function (watcher) { watcher.close(); }); - delete project.directoriesWatchedForWildcards; - ts.unorderedRemoveItem(this.configuredProjects, project); + var displayString = ts.displayPartsToString(nameInfo.displayParts); + var nameSpan = nameInfo.textSpan; + var nameColStart = scriptInfo.positionToLineOffset(nameSpan.start).offset; + var nameText = scriptInfo.snap().getText(nameSpan.start, ts.textSpanEnd(nameSpan)); + var refs = server.combineProjectOutput(projects, function (project) { + var references = project.getLanguageService().getReferencesAtPosition(file, position); + if (!references) { + return []; + } + return references.map(function (ref) { + var refScriptInfo = project.getScriptInfo(ref.fileName); + var start = refScriptInfo.positionToLineOffset(ref.textSpan.start); + var refLineSpan = refScriptInfo.lineToTextSpan(start.line - 1); + var lineText = refScriptInfo.snap().getText(refLineSpan.start, ts.textSpanEnd(refLineSpan)).replace(/\r|\n/g, ""); + return { + file: ref.fileName, + start: start, + lineText: lineText, + end: refScriptInfo.positionToLineOffset(ts.textSpanEnd(ref.textSpan)), + isWriteAccess: ref.isWriteAccess, + isDefinition: ref.isDefinition + }; + }); + }, compareFileStart, areReferencesResponseItemsForTheSameLocation); + return { + refs: refs, + symbolName: nameText, + symbolStartOffset: nameColStart, + symbolDisplayString: displayString + }; } else { - for (var _i = 0, _a = project.directoriesWatchedForTsconfig; _i < _a.length; _i++) { - var directory = _a[_i]; - project.projectService.directoryWatchersRefCount[directory]--; - if (!project.projectService.directoryWatchersRefCount[directory]) { - this.log("Close directory watcher for: " + directory); - project.projectService.directoryWatchersForTsconfig[directory].close(); - delete project.projectService.directoryWatchersForTsconfig[directory]; - } - } - ts.unorderedRemoveItem(this.inferredProjects, project); + return server.combineProjectOutput(projects, function (project) { return project.getLanguageService().findReferences(file, position); }, undefined, undefined); } - var fileNames = project.getFileNames(); - for (var _b = 0, fileNames_3 = fileNames; _b < fileNames_3.length; _b++) { - var fileName = fileNames_3[_b]; - var info = this.getScriptInfo(fileName); - if (info.defaultProject == project) { - info.defaultProject = undefined; + function areReferencesResponseItemsForTheSameLocation(a, b) { + if (a && b) { + return a.file === b.file && + a.start === b.start && + a.end === b.end; } + return false; } }; - ProjectService.prototype.setConfiguredProjectRoot = function (info) { - for (var i = 0, len = this.configuredProjects.length; i < len; i++) { - var configuredProject = this.configuredProjects[i]; - if (configuredProject.isRoot(info)) { - info.defaultProject = configuredProject; - configuredProject.addOpenRef(); - return true; - } + Session.prototype.openClientFile = function (fileName, fileContent, scriptKind) { + var _a = this.projectService.openClientFileWithNormalizedPath(fileName, fileContent, scriptKind), configFileName = _a.configFileName, configFileErrors = _a.configFileErrors; + if (this.eventHander) { + this.eventHander({ + eventName: "configFileDiag", + data: { fileName: fileName, configFileName: configFileName, diagnostics: configFileErrors || [] } + }); } - return false; }; - ProjectService.prototype.addOpenFile = function (info) { - if (this.setConfiguredProjectRoot(info)) { - this.openFileRootsConfigured.push(info); + Session.prototype.getPosition = function (args, scriptInfo) { + return args.position !== undefined ? args.position : scriptInfo.lineOffsetToPosition(args.line, args.offset); + }; + Session.prototype.getFileAndProject = function (args, errorOnMissingProject) { + if (errorOnMissingProject === void 0) { errorOnMissingProject = true; } + return this.getFileAndProjectWorker(args.file, args.projectFileName, true, errorOnMissingProject); + }; + Session.prototype.getFileAndProjectWithoutRefreshingInferredProjects = function (args, errorOnMissingProject) { + if (errorOnMissingProject === void 0) { errorOnMissingProject = true; } + return this.getFileAndProjectWorker(args.file, args.projectFileName, false, errorOnMissingProject); + }; + Session.prototype.getFileAndProjectWorker = function (uncheckedFileName, projectFileName, refreshInferredProjects, errorOnMissingProject) { + var file = server.toNormalizedPath(uncheckedFileName); + var project = this.getProject(projectFileName) || this.projectService.getDefaultProjectForFile(file, refreshInferredProjects); + if (!project && errorOnMissingProject) { + return server.Errors.ThrowNoProject(); + } + return { file: file, project: project }; + }; + Session.prototype.getOutliningSpans = function (args) { + var _a = this.getFileAndProjectWithoutRefreshingInferredProjects(args), file = _a.file, project = _a.project; + return project.getLanguageService(false).getOutliningSpans(file); + }; + Session.prototype.getTodoComments = function (args) { + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + return project.getLanguageService().getTodoComments(file, args.descriptors); + }; + Session.prototype.getDocCommentTemplate = function (args) { + var _a = this.getFileAndProjectWithoutRefreshingInferredProjects(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var position = this.getPosition(args, scriptInfo); + return project.getLanguageService(false).getDocCommentTemplateAtPosition(file, position); + }; + Session.prototype.getIndentation = function (args) { + var _a = this.getFileAndProjectWithoutRefreshingInferredProjects(args), file = _a.file, project = _a.project; + var position = this.getPosition(args, project.getScriptInfoForNormalizedPath(file)); + var options = args.options || this.projectService.getFormatCodeOptions(file); + var indentation = project.getLanguageService(false).getIndentationAtPosition(file, position, options); + return { position: position, indentation: indentation }; + }; + Session.prototype.getBreakpointStatement = function (args) { + var _a = this.getFileAndProjectWithoutRefreshingInferredProjects(args), file = _a.file, project = _a.project; + var position = this.getPosition(args, project.getScriptInfoForNormalizedPath(file)); + return project.getLanguageService(false).getBreakpointStatementAtPosition(file, position); + }; + Session.prototype.getNameOrDottedNameSpan = function (args) { + var _a = this.getFileAndProjectWithoutRefreshingInferredProjects(args), file = _a.file, project = _a.project; + var position = this.getPosition(args, project.getScriptInfoForNormalizedPath(file)); + return project.getLanguageService(false).getNameOrDottedNameSpan(file, position, position); + }; + Session.prototype.isValidBraceCompletion = function (args) { + var _a = this.getFileAndProjectWithoutRefreshingInferredProjects(args), file = _a.file, project = _a.project; + var position = this.getPosition(args, project.getScriptInfoForNormalizedPath(file)); + return project.getLanguageService(false).isValidBraceCompletionAtPosition(file, position, args.openingBrace.charCodeAt(0)); + }; + Session.prototype.getQuickInfoWorker = function (args, simplifiedResult) { + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var quickInfo = project.getLanguageService().getQuickInfoAtPosition(file, this.getPosition(args, scriptInfo)); + if (!quickInfo) { + return undefined; + } + if (simplifiedResult) { + var displayString = ts.displayPartsToString(quickInfo.displayParts); + var docString = ts.displayPartsToString(quickInfo.documentation); + return { + kind: quickInfo.kind, + kindModifiers: quickInfo.kindModifiers, + start: scriptInfo.positionToLineOffset(quickInfo.textSpan.start), + end: scriptInfo.positionToLineOffset(ts.textSpanEnd(quickInfo.textSpan)), + displayString: displayString, + documentation: docString + }; } else { - this.findReferencingProjects(info); - if (info.defaultProject) { - info.defaultProject.addOpenRef(); - this.openFilesReferenced.push(info); - } - else { - info.defaultProject = this.createInferredProject(info); - var openFileRoots = []; - for (var i = 0, len = this.openFileRoots.length; i < len; i++) { - var r = this.openFileRoots[i]; - if (info.defaultProject.getSourceFile(r)) { - this.removeProject(r.defaultProject); - this.openFilesReferenced.push(r); - r.defaultProject = info.defaultProject; - } - else { - openFileRoots.push(r); - } - } - this.openFileRoots = openFileRoots; - this.openFileRoots.push(info); - } + return quickInfo; } - this.updateConfiguredProjectList(); }; - ProjectService.prototype.closeOpenFile = function (info) { - info.svc.reloadFromFile(info.fileName); - var openFileRoots = []; - var removedProject; - for (var i = 0, len = this.openFileRoots.length; i < len; i++) { - if (info === this.openFileRoots[i]) { - removedProject = info.defaultProject; - } - else { - openFileRoots.push(this.openFileRoots[i]); - } + Session.prototype.getFormattingEditsForRange = function (args) { + var _this = this; + var _a = this.getFileAndProjectWithoutRefreshingInferredProjects(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var startPosition = scriptInfo.lineOffsetToPosition(args.line, args.offset); + var endPosition = scriptInfo.lineOffsetToPosition(args.endLine, args.endOffset); + var edits = project.getLanguageService(false).getFormattingEditsForRange(file, startPosition, endPosition, this.projectService.getFormatCodeOptions(file)); + if (!edits) { + return undefined; } - this.openFileRoots = openFileRoots; - if (!removedProject) { - var openFileRootsConfigured = []; - for (var i = 0, len = this.openFileRootsConfigured.length; i < len; i++) { - if (info === this.openFileRootsConfigured[i]) { - if (info.defaultProject.deleteOpenRef() === 0) { - removedProject = info.defaultProject; + return edits.map(function (edit) { return _this.convertTextChangeToCodeEdit(edit, scriptInfo); }); + }; + Session.prototype.getFormattingEditsForRangeFull = function (args) { + var _a = this.getFileAndProjectWithoutRefreshingInferredProjects(args), file = _a.file, project = _a.project; + var options = args.options || this.projectService.getFormatCodeOptions(file); + return project.getLanguageService(false).getFormattingEditsForRange(file, args.position, args.endPosition, options); + }; + Session.prototype.getFormattingEditsForDocumentFull = function (args) { + var _a = this.getFileAndProjectWithoutRefreshingInferredProjects(args), file = _a.file, project = _a.project; + var options = args.options || this.projectService.getFormatCodeOptions(file); + return project.getLanguageService(false).getFormattingEditsForDocument(file, options); + }; + Session.prototype.getFormattingEditsAfterKeystrokeFull = function (args) { + var _a = this.getFileAndProjectWithoutRefreshingInferredProjects(args), file = _a.file, project = _a.project; + var options = args.options || this.projectService.getFormatCodeOptions(file); + return project.getLanguageService(false).getFormattingEditsAfterKeystroke(file, args.position, args.key, options); + }; + Session.prototype.getFormattingEditsAfterKeystroke = function (args) { + var _a = this.getFileAndProjectWithoutRefreshingInferredProjects(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var position = scriptInfo.lineOffsetToPosition(args.line, args.offset); + var formatOptions = this.projectService.getFormatCodeOptions(file); + var edits = project.getLanguageService(false).getFormattingEditsAfterKeystroke(file, position, args.key, formatOptions); + if ((args.key == "\n") && ((!edits) || (edits.length === 0) || allEditsBeforePos(edits, position))) { + var lineInfo = scriptInfo.getLineInfo(args.line); + if (lineInfo && (lineInfo.leaf) && (lineInfo.leaf.text)) { + var lineText = lineInfo.leaf.text; + if (lineText.search("\\S") < 0) { + var preferredIndent = project.getLanguageService(false).getIndentationAtPosition(file, position, formatOptions); + var hasIndent = 0; + var i = void 0, len = void 0; + for (i = 0, len = lineText.length; i < len; i++) { + if (lineText.charAt(i) == " ") { + hasIndent++; + } + else if (lineText.charAt(i) == "\t") { + hasIndent += formatOptions.tabSize; + } + else { + break; + } + } + if (preferredIndent !== hasIndent) { + var firstNoWhiteSpacePosition = lineInfo.offset + i; + edits.push({ + span: ts.createTextSpanFromBounds(lineInfo.offset, firstNoWhiteSpacePosition), + newText: ts.formatting.getIndentationString(preferredIndent, formatOptions) + }); } - } - else { - openFileRootsConfigured.push(this.openFileRootsConfigured[i]); } } - this.openFileRootsConfigured = openFileRootsConfigured; } - if (removedProject) { - this.removeProject(removedProject); - var openFilesReferenced = []; - var orphanFiles = []; - for (var i = 0, len = this.openFilesReferenced.length; i < len; i++) { - var f = this.openFilesReferenced[i]; - if (f.defaultProject === removedProject || !f.defaultProject) { - f.defaultProject = undefined; - orphanFiles.push(f); - } - else { - openFilesReferenced.push(f); + if (!edits) { + return undefined; + } + return edits.map(function (edit) { + return { + start: scriptInfo.positionToLineOffset(edit.span.start), + end: scriptInfo.positionToLineOffset(ts.textSpanEnd(edit.span)), + newText: edit.newText ? edit.newText : "" + }; + }); + }; + Session.prototype.getCompletions = function (args, simplifiedResult) { + var _this = this; + var prefix = args.prefix || ""; + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var position = this.getPosition(args, scriptInfo); + var completions = project.getLanguageService().getCompletionsAtPosition(file, position); + if (!completions) { + return undefined; + } + if (simplifiedResult) { + return completions.entries.reduce(function (result, entry) { + if (completions.isMemberCompletion || (entry.name.toLowerCase().indexOf(prefix.toLowerCase()) === 0)) { + var name_53 = entry.name, kind = entry.kind, kindModifiers = entry.kindModifiers, sortText = entry.sortText, replacementSpan = entry.replacementSpan; + var convertedSpan = replacementSpan ? _this.decorateSpan(replacementSpan, scriptInfo) : undefined; + result.push({ name: name_53, kind: kind, kindModifiers: kindModifiers, sortText: sortText, replacementSpan: convertedSpan }); } - } - this.openFilesReferenced = openFilesReferenced; - for (var i = 0, len = orphanFiles.length; i < len; i++) { - this.addOpenFile(orphanFiles[i]); - } + return result; + }, []).sort(function (a, b) { return a.name.localeCompare(b.name); }); } else { - ts.unorderedRemoveItem(this.openFilesReferenced, info); + return completions; } - info.close(); }; - ProjectService.prototype.findReferencingProjects = function (info, excludedProject) { - var referencingProjects = []; - info.defaultProject = undefined; - for (var i = 0, len = this.inferredProjects.length; i < len; i++) { - var inferredProject = this.inferredProjects[i]; - inferredProject.updateGraph(); - if (inferredProject !== excludedProject) { - if (inferredProject.getSourceFile(info)) { - info.defaultProject = inferredProject; - referencingProjects.push(inferredProject); - } + Session.prototype.getCompletionEntryDetails = function (args) { + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var position = this.getPosition(args, scriptInfo); + return args.entryNames.reduce(function (accum, entryName) { + var details = project.getLanguageService().getCompletionEntryDetails(file, position, entryName); + if (details) { + accum.push(details); } + return accum; + }, []); + }; + Session.prototype.getCompileOnSaveAffectedFileList = function (args) { + var info = this.projectService.getScriptInfo(args.file); + var result = []; + if (!info) { + return []; } - for (var i = 0, len = this.configuredProjects.length; i < len; i++) { - var configuredProject = this.configuredProjects[i]; - configuredProject.updateGraph(); - if (configuredProject.getSourceFile(info)) { - info.defaultProject = configuredProject; - referencingProjects.push(configuredProject); + var projectsToSearch = args.projectFileName ? [this.projectService.findProject(args.projectFileName)] : info.containingProjects; + for (var _i = 0, projectsToSearch_1 = projectsToSearch; _i < projectsToSearch_1.length; _i++) { + var project = projectsToSearch_1[_i]; + if (project.compileOnSaveEnabled && project.languageServiceEnabled) { + result.push({ + projectFileName: project.getProjectName(), + fileNames: project.getCompileOnSaveAffectedFileList(info) + }); } } - return referencingProjects; + return result; }; - ProjectService.prototype.reloadProjects = function () { - this.log("reload projects."); - for (var _i = 0, _a = this.openFileRoots; _i < _a.length; _i++) { - var info = _a[_i]; - this.openOrUpdateConfiguredProjectForFile(info.fileName); + Session.prototype.emitFile = function (args) { + var _this = this; + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + if (!project) { + server.Errors.ThrowNoProject(); } - this.updateProjectStructure(); + var scriptInfo = project.getScriptInfo(file); + return project.builder.emitFile(scriptInfo, function (path, data, writeByteOrderMark) { return _this.host.writeFile(path, data, writeByteOrderMark); }); }; - ProjectService.prototype.updateProjectStructure = function () { - this.log("updating project structure from ...", "Info"); - this.printProjects(); - var unattachedOpenFiles = []; - var openFileRootsConfigured = []; - for (var _i = 0, _a = this.openFileRootsConfigured; _i < _a.length; _i++) { - var info = _a[_i]; - var project = info.defaultProject; - if (!project || !(project.getSourceFile(info))) { - info.defaultProject = undefined; - unattachedOpenFiles.push(info); - } - else { - openFileRootsConfigured.push(info); - } - } - this.openFileRootsConfigured = openFileRootsConfigured; - var openFilesReferenced = []; - for (var i = 0, len = this.openFilesReferenced.length; i < len; i++) { - var referencedFile = this.openFilesReferenced[i]; - referencedFile.defaultProject.updateGraph(); - var sourceFile = referencedFile.defaultProject.getSourceFile(referencedFile); - if (sourceFile) { - openFilesReferenced.push(referencedFile); - } - else { - unattachedOpenFiles.push(referencedFile); - } + Session.prototype.getSignatureHelpItems = function (args, simplifiedResult) { + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var position = this.getPosition(args, scriptInfo); + var helpItems = project.getLanguageService().getSignatureHelpItems(file, position); + if (!helpItems) { + return undefined; } - this.openFilesReferenced = openFilesReferenced; - var openFileRoots = []; - for (var i = 0, len = this.openFileRoots.length; i < len; i++) { - var rootFile = this.openFileRoots[i]; - var rootedProject = rootFile.defaultProject; - var referencingProjects = this.findReferencingProjects(rootFile, rootedProject); - if (rootFile.defaultProject && rootFile.defaultProject.isConfiguredProject()) { - if (!rootedProject.isConfiguredProject()) { - this.removeProject(rootedProject); - } - this.openFileRootsConfigured.push(rootFile); - } - else { - if (referencingProjects.length === 0) { - rootFile.defaultProject = rootedProject; - openFileRoots.push(rootFile); - } - else { - this.removeProject(rootedProject); - this.openFilesReferenced.push(rootFile); - } - } + if (simplifiedResult) { + var span_16 = helpItems.applicableSpan; + return { + items: helpItems.items, + applicableSpan: { + start: scriptInfo.positionToLineOffset(span_16.start), + end: scriptInfo.positionToLineOffset(span_16.start + span_16.length) + }, + selectedItemIndex: helpItems.selectedItemIndex, + argumentIndex: helpItems.argumentIndex, + argumentCount: helpItems.argumentCount + }; } - this.openFileRoots = openFileRoots; - for (var i = 0, len = unattachedOpenFiles.length; i < len; i++) { - this.addOpenFile(unattachedOpenFiles[i]); + else { + return helpItems; } - this.printProjects(); - }; - ProjectService.prototype.getScriptInfo = function (filename) { - filename = ts.normalizePath(filename); - return this.filenameToScriptInfo[filename]; }; - ProjectService.prototype.openFile = function (fileName, openedByClient, fileContent, scriptKind) { + Session.prototype.getDiagnostics = function (delay, fileNames) { var _this = this; - fileName = ts.normalizePath(fileName); - var info = this.filenameToScriptInfo[fileName]; - if (!info) { - var content = void 0; - if (this.host.fileExists(fileName)) { - content = fileContent || this.host.readFile(fileName); - } - if (!content) { - if (openedByClient) { - content = ""; - } - } - if (content !== undefined) { - info = new ScriptInfo(this.host, fileName, content, openedByClient); - info.scriptKind = scriptKind; - info.setFormatOptions(this.getFormatCodeOptions()); - this.filenameToScriptInfo[fileName] = info; - if (!info.isOpen) { - info.fileWatcher = this.host.watchFile(fileName, function (_) { _this.watchedFileChanged(fileName); }); - } + var checkList = fileNames.reduce(function (accum, uncheckedFileName) { + var fileName = server.toNormalizedPath(uncheckedFileName); + var project = _this.projectService.getDefaultProjectForFile(fileName, true); + if (project) { + accum.push({ fileName: fileName, project: project }); } + return accum; + }, []); + if (checkList.length > 0) { + this.updateErrorCheck(checkList, this.changeSeq, function (n) { return n === _this.changeSeq; }, delay); } - if (info) { - if (fileContent) { - info.svc.reload(fileContent); - } - if (openedByClient) { - info.isOpen = true; + }; + Session.prototype.change = function (args) { + var _this = this; + var _a = this.getFileAndProject(args, false), file = _a.file, project = _a.project; + if (project) { + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var start = scriptInfo.lineOffsetToPosition(args.line, args.offset); + var end = scriptInfo.lineOffsetToPosition(args.endLine, args.endOffset); + if (start >= 0) { + scriptInfo.editContent(start, end, args.insertString); + this.changeSeq++; } + this.updateProjectStructure(this.changeSeq, function (n) { return n === _this.changeSeq; }); } - return info; }; - ProjectService.prototype.findConfigFile = function (searchPath) { - while (true) { - var tsconfigFileName = ts.combinePaths(searchPath, "tsconfig.json"); - if (this.host.fileExists(tsconfigFileName)) { - return tsconfigFileName; - } - var jsconfigFileName = ts.combinePaths(searchPath, "jsconfig.json"); - if (this.host.fileExists(jsconfigFileName)) { - return jsconfigFileName; - } - var parentPath = ts.getDirectoryPath(searchPath); - if (parentPath === searchPath) { - break; + Session.prototype.reload = function (args, reqSeq) { + var file = server.toNormalizedPath(args.file); + var project = this.projectService.getDefaultProjectForFile(file, true); + if (project) { + this.changeSeq++; + if (project.reloadScript(file)) { + this.output(undefined, CommandNames.Reload, reqSeq); } - searchPath = parentPath; } - return undefined; }; - ProjectService.prototype.openClientFile = function (fileName, fileContent, scriptKind) { - var _a = this.openOrUpdateConfiguredProjectForFile(fileName), configFileName = _a.configFileName, configFileErrors = _a.configFileErrors; - var info = this.openFile(fileName, true, fileContent, scriptKind); - this.addOpenFile(info); - this.printProjects(); - return { configFileName: configFileName, configFileErrors: configFileErrors }; + Session.prototype.saveToTmp = function (fileName, tempFileName) { + var scriptInfo = this.projectService.getScriptInfo(fileName); + if (scriptInfo) { + scriptInfo.saveTo(tempFileName); + } }; - ProjectService.prototype.openOrUpdateConfiguredProjectForFile = function (fileName) { - var searchPath = ts.normalizePath(ts.getDirectoryPath(fileName)); - this.log("Search path: " + searchPath, "Info"); - var configFileName = this.findConfigFile(searchPath); - if (configFileName) { - this.log("Config file name: " + configFileName, "Info"); - var project = this.findConfiguredProjectByConfigFile(configFileName); - if (!project) { - var configResult = this.openConfigFile(configFileName, fileName); - if (!configResult.project) { - return { configFileName: configFileName, configFileErrors: configResult.errors }; - } - else { - this.log("Opened configuration file " + configFileName, "Info"); - this.configuredProjects.push(configResult.project); - if (configResult.errors && configResult.errors.length > 0) { - return { configFileName: configFileName, configFileErrors: configResult.errors }; + Session.prototype.closeClientFile = function (fileName) { + if (!fileName) { + return; + } + var file = ts.normalizePath(fileName); + this.projectService.closeClientFile(file); + }; + Session.prototype.decorateNavigationBarItems = function (items, scriptInfo) { + var _this = this; + return ts.map(items, function (item) { return ({ + text: item.text, + kind: item.kind, + kindModifiers: item.kindModifiers, + spans: item.spans.map(function (span) { return _this.decorateSpan(span, scriptInfo); }), + childItems: _this.decorateNavigationBarItems(item.childItems, scriptInfo), + indent: item.indent + }); }); + }; + Session.prototype.getNavigationBarItems = function (args, simplifiedResult) { + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + var items = project.getLanguageService(false).getNavigationBarItems(file); + return !items + ? undefined + : simplifiedResult + ? this.decorateNavigationBarItems(items, project.getScriptInfoForNormalizedPath(file)) + : items; + }; + Session.prototype.decorateNavigationTree = function (tree, scriptInfo) { + var _this = this; + return { + text: tree.text, + kind: tree.kind, + kindModifiers: tree.kindModifiers, + spans: tree.spans.map(function (span) { return _this.decorateSpan(span, scriptInfo); }), + childItems: ts.map(tree.childItems, function (item) { return _this.decorateNavigationTree(item, scriptInfo); }) + }; + }; + Session.prototype.decorateSpan = function (span, scriptInfo) { + return { + start: scriptInfo.positionToLineOffset(span.start), + end: scriptInfo.positionToLineOffset(ts.textSpanEnd(span)) + }; + }; + Session.prototype.getNavigationTree = function (args, simplifiedResult) { + var _a = this.getFileAndProject(args), file = _a.file, project = _a.project; + var tree = project.getLanguageService(false).getNavigationTree(file); + return !tree + ? undefined + : simplifiedResult + ? this.decorateNavigationTree(tree, project.getScriptInfoForNormalizedPath(file)) + : tree; + }; + Session.prototype.getNavigateToItems = function (args, simplifiedResult) { + var projects = this.getProjects(args); + var fileName = args.currentFileOnly ? args.file && ts.normalizeSlashes(args.file) : undefined; + if (simplifiedResult) { + return server.combineProjectOutput(projects, function (project) { + var navItems = project.getLanguageService().getNavigateToItems(args.searchValue, args.maxResultCount, fileName, project.isNonTsProject()); + if (!navItems) { + return []; + } + return navItems.map(function (navItem) { + var scriptInfo = project.getScriptInfo(navItem.fileName); + var start = scriptInfo.positionToLineOffset(navItem.textSpan.start); + var end = scriptInfo.positionToLineOffset(ts.textSpanEnd(navItem.textSpan)); + var bakedItem = { + name: navItem.name, + kind: navItem.kind, + file: navItem.fileName, + start: start, + end: end + }; + if (navItem.kindModifiers && (navItem.kindModifiers !== "")) { + bakedItem.kindModifiers = navItem.kindModifiers; } - } + if (navItem.matchKind !== "none") { + bakedItem.matchKind = navItem.matchKind; + } + if (navItem.containerName && (navItem.containerName.length > 0)) { + bakedItem.containerName = navItem.containerName; + } + if (navItem.containerKind && (navItem.containerKind.length > 0)) { + bakedItem.containerKind = navItem.containerKind; + } + return bakedItem; + }); + }, undefined, areNavToItemsForTheSameLocation); + } + else { + return server.combineProjectOutput(projects, function (project) { return project.getLanguageService().getNavigateToItems(args.searchValue, args.maxResultCount, fileName, project.isNonTsProject()); }, undefined, navigateToItemIsEqualTo); + } + function navigateToItemIsEqualTo(a, b) { + if (a === b) { + return true; } - else { - this.updateConfiguredProject(project); + if (!a || !b) { + return false; } - return { configFileName: configFileName }; + return a.containerKind === b.containerKind && + a.containerName === b.containerName && + a.fileName === b.fileName && + a.isCaseSensitive === b.isCaseSensitive && + a.kind === b.kind && + a.kindModifiers === b.containerName && + a.matchKind === b.matchKind && + a.name === b.name && + a.textSpan.start === b.textSpan.start && + a.textSpan.length === b.textSpan.length; } - else { - this.log("No config files found."); + function areNavToItemsForTheSameLocation(a, b) { + if (a && b) { + return a.file === b.file && + a.start === b.start && + a.end === b.end; + } + return false; } - return {}; }; - ProjectService.prototype.closeClientFile = function (filename) { - var info = this.filenameToScriptInfo[filename]; - if (info) { - this.closeOpenFile(info); - info.isOpen = false; - } - this.printProjects(); + Session.prototype.getSupportedCodeFixes = function () { + return ts.getSupportedCodeFixes(); }; - ProjectService.prototype.getProjectForFile = function (filename) { - var scriptInfo = this.filenameToScriptInfo[filename]; - if (scriptInfo) { - return scriptInfo.defaultProject; + Session.prototype.getCodeFixes = function (args, simplifiedResult) { + var _this = this; + var _a = this.getFileAndProjectWithoutRefreshingInferredProjects(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var startPosition = getStartPosition(); + var endPosition = getEndPosition(); + var codeActions = project.getLanguageService().getCodeFixesAtPosition(file, startPosition, endPosition, args.errorCodes); + if (!codeActions) { + return undefined; } - }; - ProjectService.prototype.printProjectsForFile = function (filename) { - var scriptInfo = this.filenameToScriptInfo[filename]; - if (scriptInfo) { - this.psLogger.startGroup(); - this.psLogger.info("Projects for " + filename); - var projects = this.findReferencingProjects(scriptInfo); - for (var i = 0, len = projects.length; i < len; i++) { - this.psLogger.info("Project " + i.toString()); - } - this.psLogger.endGroup(); + if (simplifiedResult) { + return codeActions.map(function (codeAction) { return _this.mapCodeAction(codeAction, scriptInfo); }); } else { - this.psLogger.info(filename + " not in any project"); - } - }; - ProjectService.prototype.printProjects = function () { - if (!this.psLogger.isVerbose()) { - return; + return codeActions; } - this.psLogger.startGroup(); - for (var i = 0, len = this.inferredProjects.length; i < len; i++) { - var project = this.inferredProjects[i]; - project.updateGraph(); - this.psLogger.info("Project " + i.toString()); - this.psLogger.info(project.filesToString()); - this.psLogger.info("-----------------------------------------------"); + function getStartPosition() { + return args.startPosition !== undefined ? args.startPosition : scriptInfo.lineOffsetToPosition(args.startLine, args.startOffset); } - for (var i = 0, len = this.configuredProjects.length; i < len; i++) { - var project = this.configuredProjects[i]; - project.updateGraph(); - this.psLogger.info("Project (configured) " + (i + this.inferredProjects.length).toString()); - this.psLogger.info(project.filesToString()); - this.psLogger.info("-----------------------------------------------"); + function getEndPosition() { + return args.endPosition !== undefined ? args.endPosition : scriptInfo.lineOffsetToPosition(args.endLine, args.endOffset); } - this.psLogger.info("Open file roots of inferred projects: "); - for (var i = 0, len = this.openFileRoots.length; i < len; i++) { - this.psLogger.info(this.openFileRoots[i].fileName); + }; + Session.prototype.mapCodeAction = function (codeAction, scriptInfo) { + var _this = this; + return { + description: codeAction.description, + changes: codeAction.changes.map(function (change) { return ({ + fileName: change.fileName, + textChanges: change.textChanges.map(function (textChange) { return _this.convertTextChangeToCodeEdit(textChange, scriptInfo); }) + }); }) + }; + }; + Session.prototype.convertTextChangeToCodeEdit = function (change, scriptInfo) { + return { + start: scriptInfo.positionToLineOffset(change.span.start), + end: scriptInfo.positionToLineOffset(change.span.start + change.span.length), + newText: change.newText ? change.newText : "" + }; + }; + Session.prototype.getBraceMatching = function (args, simplifiedResult) { + var _this = this; + var _a = this.getFileAndProjectWithoutRefreshingInferredProjects(args), file = _a.file, project = _a.project; + var scriptInfo = project.getScriptInfoForNormalizedPath(file); + var position = this.getPosition(args, scriptInfo); + var spans = project.getLanguageService(false).getBraceMatchingAtPosition(file, position); + return !spans + ? undefined + : simplifiedResult + ? spans.map(function (span) { return _this.decorateSpan(span, scriptInfo); }) + : spans; + }; + Session.prototype.getDiagnosticsForProject = function (delay, fileName) { + var _this = this; + var _a = this.getProjectInfoWorker(fileName, undefined, true), fileNames = _a.fileNames, languageServiceDisabled = _a.languageServiceDisabled; + if (languageServiceDisabled) { + return; } - this.psLogger.info("Open files referenced by inferred or configured projects: "); - for (var i = 0, len = this.openFilesReferenced.length; i < len; i++) { - var fileInfo = this.openFilesReferenced[i].fileName; - if (this.openFilesReferenced[i].defaultProject.isConfiguredProject()) { - fileInfo += " (configured)"; + var fileNamesInProject = fileNames.filter(function (value, index, array) { return value.indexOf("lib.d.ts") < 0; }); + var highPriorityFiles = []; + var mediumPriorityFiles = []; + var lowPriorityFiles = []; + var veryLowPriorityFiles = []; + var normalizedFileName = server.toNormalizedPath(fileName); + var project = this.projectService.getDefaultProjectForFile(normalizedFileName, true); + for (var _i = 0, fileNamesInProject_1 = fileNamesInProject; _i < fileNamesInProject_1.length; _i++) { + var fileNameInProject = fileNamesInProject_1[_i]; + if (this.getCanonicalFileName(fileNameInProject) == this.getCanonicalFileName(fileName)) + highPriorityFiles.push(fileNameInProject); + else { + var info = this.projectService.getScriptInfo(fileNameInProject); + if (!info.isOpen) { + if (fileNameInProject.indexOf(".d.ts") > 0) + veryLowPriorityFiles.push(fileNameInProject); + else + lowPriorityFiles.push(fileNameInProject); + } + else + mediumPriorityFiles.push(fileNameInProject); } - this.psLogger.info(fileInfo); } - this.psLogger.info("Open file roots of configured projects: "); - for (var i = 0, len = this.openFileRootsConfigured.length; i < len; i++) { - this.psLogger.info(this.openFileRootsConfigured[i].fileName); + fileNamesInProject = highPriorityFiles.concat(mediumPriorityFiles).concat(lowPriorityFiles).concat(veryLowPriorityFiles); + if (fileNamesInProject.length > 0) { + var checkList = fileNamesInProject.map(function (fileName) { return ({ fileName: fileName, project: project }); }); + this.updateErrorCheck(checkList, this.changeSeq, function (n) { return n == _this.changeSeq; }, delay, 200, false); } - this.psLogger.endGroup(); }; - ProjectService.prototype.configProjectIsActive = function (fileName) { - return this.findConfiguredProjectByConfigFile(fileName) === undefined; + Session.prototype.getCanonicalFileName = function (fileName) { + var name = this.host.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase(); + return ts.normalizePath(name); + }; + Session.prototype.exit = function () { }; - ProjectService.prototype.findConfiguredProjectByConfigFile = function (configFileName) { - for (var i = 0, len = this.configuredProjects.length; i < len; i++) { - if (this.configuredProjects[i].projectFilename == configFileName) { - return this.configuredProjects[i]; - } + Session.prototype.notRequired = function () { + return { responseRequired: false }; + }; + Session.prototype.requiredResponse = function (response) { + return { response: response, responseRequired: true }; + }; + Session.prototype.addProtocolHandler = function (command, handler) { + if (command in this.handlers) { + throw new Error("Protocol handler already exists for command \"" + command + "\""); } - return undefined; + this.handlers[command] = handler; }; - ProjectService.prototype.configFileToProjectOptions = function (configFilename) { - configFilename = ts.normalizePath(configFilename); - var errors = []; - var dirPath = ts.getDirectoryPath(configFilename); - var contents = this.host.readFile(configFilename); - var _a = ts.parseAndReEmitConfigJSONFile(contents), configJsonObject = _a.configJsonObject, diagnostics = _a.diagnostics; - errors = ts.concatenate(errors, diagnostics); - var parsedCommandLine = ts.parseJsonConfigFileContent(configJsonObject, this.host, dirPath, {}, configFilename); - errors = ts.concatenate(errors, parsedCommandLine.errors); - ts.Debug.assert(!!parsedCommandLine.fileNames); - if (parsedCommandLine.fileNames.length === 0) { - errors.push(ts.createCompilerDiagnostic(ts.Diagnostics.The_config_file_0_found_doesn_t_contain_any_source_files, configFilename)); - return { errors: errors }; + Session.prototype.executeCommand = function (request) { + var handler = this.handlers[request.command]; + if (handler) { + return handler(request); } else { - var projectOptions = { - files: parsedCommandLine.fileNames, - wildcardDirectories: parsedCommandLine.wildcardDirectories, - compilerOptions: parsedCommandLine.options - }; - return { projectOptions: projectOptions, errors: errors }; + this.logger.msg("Unrecognized JSON command: " + JSON.stringify(request), server.Msg.Err); + this.output(undefined, CommandNames.Unknown, request.seq, "Unrecognized JSON command: " + request.command); + return { responseRequired: false }; } }; - ProjectService.prototype.exceedTotalNonTsFileSizeLimit = function (fileNames) { - var totalNonTsFileSize = 0; - if (!this.host.getFileSize) { - return false; - } - for (var _i = 0, fileNames_4 = fileNames; _i < fileNames_4.length; _i++) { - var fileName = fileNames_4[_i]; - if (ts.hasTypeScriptFileExtension(fileName)) { - continue; - } - totalNonTsFileSize += this.host.getFileSize(fileName); - if (totalNonTsFileSize > server.maxProgramSizeForNonTsFiles) { - return true; + Session.prototype.onMessage = function (message) { + this.gcTimer.scheduleCollect(); + var start; + if (this.logger.hasLevel(server.LogLevel.requestTime)) { + start = this.hrtime(); + if (this.logger.hasLevel(server.LogLevel.verbose)) { + this.logger.info("request: " + message); } } - return false; - }; - ProjectService.prototype.openConfigFile = function (configFilename, clientFileName) { - var _this = this; - var parseConfigFileResult = this.configFileToProjectOptions(configFilename); - var errors = parseConfigFileResult.errors; - if (!parseConfigFileResult.projectOptions) { - return { errors: errors }; - } - var projectOptions = parseConfigFileResult.projectOptions; - if (!projectOptions.compilerOptions.disableSizeLimit && projectOptions.compilerOptions.allowJs) { - if (this.exceedTotalNonTsFileSizeLimit(projectOptions.files)) { - var project_1 = this.createProject(configFilename, projectOptions, true); - project_1.projectFileWatcher = this.host.watchFile(ts.toPath(configFilename, configFilename, ts.createGetCanonicalFileName(ts.sys.useCaseSensitiveFileNames)), function (_) { return _this.watchedProjectConfigFileChanged(project_1); }); - return { project: project_1, errors: errors }; - } - } - var project = this.createProject(configFilename, projectOptions); - for (var _i = 0, _a = projectOptions.files; _i < _a.length; _i++) { - var rootFilename = _a[_i]; - if (this.host.fileExists(rootFilename)) { - var info = this.openFile(rootFilename, clientFileName == rootFilename); - project.addRoot(info); + var request; + try { + request = JSON.parse(message); + var _a = this.executeCommand(request), response = _a.response, responseRequired = _a.responseRequired; + if (this.logger.hasLevel(server.LogLevel.requestTime)) { + var elapsedTime = hrTimeToMilliseconds(this.hrtime(start)).toFixed(4); + if (responseRequired) { + this.logger.perftrc(request.seq + "::" + request.command + ": elapsed time (in milliseconds) " + elapsedTime); + } + else { + this.logger.perftrc(request.seq + "::" + request.command + ": async elapsed time (in milliseconds) " + elapsedTime); + } } - else { - (errors || (errors = [])).push(ts.createCompilerDiagnostic(ts.Diagnostics.File_0_not_found, rootFilename)); + if (response) { + this.output(response, request.command, request.seq); } - } - project.finishGraph(); - project.projectFileWatcher = this.host.watchFile(configFilename, function (_) { return _this.watchedProjectConfigFileChanged(project); }); - var configDirectoryPath = ts.getDirectoryPath(configFilename); - this.log("Add recursive watcher for: " + configDirectoryPath); - project.directoryWatcher = this.host.watchDirectory(configDirectoryPath, function (path) { return _this.directoryWatchedForSourceFilesChanged(project, path); }, true); - project.directoriesWatchedForWildcards = ts.reduceProperties(ts.createMap(projectOptions.wildcardDirectories), function (watchers, flag, directory) { - if (ts.comparePaths(configDirectoryPath, directory, ".", !_this.host.useCaseSensitiveFileNames) !== 0) { - var recursive = (flag & 1) !== 0; - _this.log("Add " + (recursive ? "recursive " : "") + "watcher for: " + directory); - watchers[directory] = _this.host.watchDirectory(directory, function (path) { return _this.directoryWatchedForSourceFilesChanged(project, path); }, recursive); + else if (responseRequired) { + this.output(undefined, request.command, request.seq, "No content available."); } - return watchers; - }, {}); - return { project: project, errors: errors }; - }; - ProjectService.prototype.updateConfiguredProject = function (project) { - var _this = this; - if (!this.host.fileExists(project.projectFilename)) { - this.log("Config file deleted"); - this.removeProject(project); } - else { - var _a = this.configFileToProjectOptions(project.projectFilename), projectOptions = _a.projectOptions, errors = _a.errors; - if (!projectOptions) { - return errors; - } - else { - if (projectOptions.compilerOptions && !projectOptions.compilerOptions.disableSizeLimit && this.exceedTotalNonTsFileSizeLimit(projectOptions.files)) { - project.setProjectOptions(projectOptions); - if (project.languageServiceDiabled) { - return errors; - } - project.disableLanguageService(); - if (project.directoryWatcher) { - project.directoryWatcher.close(); - project.directoryWatcher = undefined; - } - return errors; - } - if (project.languageServiceDiabled) { - project.setProjectOptions(projectOptions); - project.enableLanguageService(); - project.directoryWatcher = this.host.watchDirectory(ts.getDirectoryPath(project.projectFilename), function (path) { return _this.directoryWatchedForSourceFilesChanged(project, path); }, true); - for (var _i = 0, _b = projectOptions.files; _i < _b.length; _i++) { - var rootFilename = _b[_i]; - if (this.host.fileExists(rootFilename)) { - var info = this.openFile(rootFilename, false); - project.addRoot(info); - } - } - project.finishGraph(); - return errors; - } - var oldFileNames_1 = project.projectOptions ? project.projectOptions.files : project.compilerService.host.roots.map(function (info) { return info.fileName; }); - var newFileNames_1 = ts.filter(projectOptions.files, function (f) { return _this.host.fileExists(f); }); - var fileNamesToRemove = oldFileNames_1.filter(function (f) { return newFileNames_1.indexOf(f) < 0; }); - var fileNamesToAdd = newFileNames_1.filter(function (f) { return oldFileNames_1.indexOf(f) < 0; }); - for (var _c = 0, fileNamesToRemove_1 = fileNamesToRemove; _c < fileNamesToRemove_1.length; _c++) { - var fileName = fileNamesToRemove_1[_c]; - var info = this.getScriptInfo(fileName); - if (info) { - project.removeRoot(info); - } - } - for (var _d = 0, fileNamesToAdd_1 = fileNamesToAdd; _d < fileNamesToAdd_1.length; _d++) { - var fileName = fileNamesToAdd_1[_d]; - var info = this.getScriptInfo(fileName); - if (!info) { - info = this.openFile(fileName, false); - } - else { - if (info.isOpen) { - if (this.openFileRoots.indexOf(info) >= 0) { - ts.unorderedRemoveItem(this.openFileRoots, info); - if (info.defaultProject && !info.defaultProject.isConfiguredProject()) { - this.removeProject(info.defaultProject); - } - } - if (this.openFilesReferenced.indexOf(info) >= 0) { - ts.unorderedRemoveItem(this.openFilesReferenced, info); - } - this.openFileRootsConfigured.push(info); - info.defaultProject = project; - } - } - project.addRoot(info); - } - project.setProjectOptions(projectOptions); - project.finishGraph(); + catch (err) { + if (err instanceof ts.OperationCanceledException) { + this.output({ canceled: true }, request.command, request.seq); + return; } - return errors; + this.logError(err, message); + this.output(undefined, request ? request.command : CommandNames.Unknown, request ? request.seq : 0, "Error processing request. " + err.message + "\n" + err.stack); } }; - ProjectService.prototype.createProject = function (projectFilename, projectOptions, languageServiceDisabled) { - var project = new Project(this, projectOptions, languageServiceDisabled); - project.projectFilename = projectFilename; - return project; - }; - return ProjectService; - }()); - server.ProjectService = ProjectService; - var CompilerService = (function () { - function CompilerService(project, opt) { - this.project = project; - this.documentRegistry = ts.createDocumentRegistry(); - this.host = new LSHost(project.projectService.host, project); - if (opt) { - this.setCompilerOptions(opt); - } - else { - var defaultOpts = ts.getDefaultCompilerOptions(); - defaultOpts.allowNonTsExtensions = true; - defaultOpts.allowJs = true; - this.setCompilerOptions(defaultOpts); - } - this.languageService = ts.createLanguageService(this.host, this.documentRegistry); - this.classifier = ts.createClassifier(); - } - CompilerService.prototype.setCompilerOptions = function (opt) { - this.settings = opt; - this.host.setCompilationSettings(opt); - }; - CompilerService.prototype.isExternalModule = function (filename) { - var sourceFile = this.languageService.getNonBoundSourceFile(filename); - return ts.isExternalModule(sourceFile); - }; - CompilerService.getDefaultFormatCodeOptions = function (host) { - return ts.clone({ - BaseIndentSize: 0, - IndentSize: 4, - TabSize: 4, - NewLineCharacter: host.newLine || "\n", - ConvertTabsToSpaces: true, - IndentStyle: ts.IndentStyle.Smart, - InsertSpaceAfterCommaDelimiter: true, - InsertSpaceAfterSemicolonInForStatements: true, - InsertSpaceBeforeAndAfterBinaryOperators: true, - InsertSpaceAfterKeywordsInControlFlowStatements: true, - InsertSpaceAfterFunctionKeywordForAnonymousFunctions: false, - InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false, - InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false, - InsertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true, - InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false, - InsertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: false, - InsertSpaceAfterTypeAssertion: false, - PlaceOpenBraceOnNewLineForFunctions: false, - PlaceOpenBraceOnNewLineForControlBlocks: false - }); - }; - return CompilerService; + return Session; }()); - server.CompilerService = CompilerService; + server.Session = Session; + })(server = ts.server || (ts.server = {})); +})(ts || (ts = {})); +var ts; +(function (ts) { + var server; + (function (server) { + var lineCollectionCapacity = 4; (function (CharRangeSection) { CharRangeSection[CharRangeSection["PreStart"] = 0] = "PreStart"; CharRangeSection[CharRangeSection["Start"] = 1] = "Start"; @@ -62605,16 +64797,17 @@ var ts; var EditWalker = (function (_super) { __extends(EditWalker, _super); function EditWalker() { - _super.call(this); - this.lineIndex = new LineIndex(); - this.endBranch = []; - this.state = CharRangeSection.Entire; - this.initialText = ""; - this.trailingText = ""; - this.suppressTrailingText = false; - this.lineIndex.root = new LineNode(); - this.startPath = [this.lineIndex.root]; - this.stack = [this.lineIndex.root]; + var _this = _super.call(this) || this; + _this.lineIndex = new LineIndex(); + _this.endBranch = []; + _this.state = CharRangeSection.Entire; + _this.initialText = ""; + _this.trailingText = ""; + _this.suppressTrailingText = false; + _this.lineIndex.root = new LineNode(); + _this.startPath = [_this.lineIndex.root]; + _this.stack = [_this.lineIndex.root]; + return _this; } EditWalker.prototype.insertLines = function (insertedText) { if (this.suppressTrailingText) { @@ -62801,10 +64994,19 @@ var ts; var ScriptVersionCache = (function () { function ScriptVersionCache() { this.changes = []; - this.versions = []; + this.versions = new Array(ScriptVersionCache.maxVersions); this.minVersion = 0; this.currentVersion = 0; } + ScriptVersionCache.prototype.versionToIndex = function (version) { + if (version < this.minVersion || version > this.currentVersion) { + return undefined; + } + return version % ScriptVersionCache.maxVersions; + }; + ScriptVersionCache.prototype.currentVersionToIndex = function () { + return this.currentVersion % ScriptVersionCache.maxVersions; + }; ScriptVersionCache.prototype.edit = function (pos, deleteLen, insertedText) { this.changes[this.changes.length] = new TextChange(pos, deleteLen, insertedText); if ((this.changes.length > ScriptVersionCache.changeNumberThreshold) || @@ -62814,7 +65016,7 @@ var ts; } }; ScriptVersionCache.prototype.latest = function () { - return this.versions[this.currentVersion]; + return this.versions[this.currentVersionToIndex()]; }; ScriptVersionCache.prototype.latestVersion = function () { if (this.changes.length > 0) { @@ -62822,32 +65024,30 @@ var ts; } return this.currentVersion; }; - ScriptVersionCache.prototype.reloadFromFile = function (filename, cb) { + ScriptVersionCache.prototype.reloadFromFile = function (filename) { var content = this.host.readFile(filename); if (!content) { content = ""; } this.reload(content); - if (cb) - cb(); }; ScriptVersionCache.prototype.reload = function (script) { this.currentVersion++; this.changes = []; var snap = new LineIndexSnapshot(this.currentVersion, this); - this.versions[this.currentVersion] = snap; + for (var i = 0; i < this.versions.length; i++) { + this.versions[i] = undefined; + } + this.versions[this.currentVersionToIndex()] = snap; snap.index = new LineIndex(); var lm = LineIndex.linesFromText(script); snap.index.load(lm.lines); - for (var i = this.minVersion; i < this.currentVersion; i++) { - this.versions[i] = undefined; - } this.minVersion = this.currentVersion; }; ScriptVersionCache.prototype.getSnapshot = function () { - var snap = this.versions[this.currentVersion]; + var snap = this.versions[this.currentVersionToIndex()]; if (this.changes.length > 0) { - var snapIndex = this.latest().index; + var snapIndex = snap.index; for (var i = 0, len = this.changes.length; i < len; i++) { var change = this.changes[i]; snapIndex = snapIndex.edit(change.pos, change.deleteLen, change.insertedText); @@ -62856,14 +65056,10 @@ var ts; snap.index = snapIndex; snap.changesSincePreviousVersion = this.changes; this.currentVersion = snap.version; - this.versions[snap.version] = snap; + this.versions[this.currentVersionToIndex()] = snap; this.changes = []; if ((this.currentVersion - this.minVersion) >= ScriptVersionCache.maxVersions) { - var oldMin = this.minVersion; this.minVersion = (this.currentVersion - ScriptVersionCache.maxVersions) + 1; - for (var j = oldMin; j < this.minVersion; j++) { - this.versions[j] = undefined; - } } } return snap; @@ -62873,7 +65069,7 @@ var ts; if (oldVersion >= this.minVersion) { var textChangeRanges = []; for (var i = oldVersion + 1; i <= newVersion; i++) { - var snap = this.versions[i]; + var snap = this.versions[this.versionToIndex(i)]; for (var j = 0, len = snap.changesSincePreviousVersion.length; j < len; j++) { var textChange = snap.changesSincePreviousVersion[j]; textChangeRanges[textChangeRanges.length] = textChange.getTextChangeRange(); @@ -63011,7 +65207,7 @@ var ts; done: false, leaf: function (relativeStart, relativeLength, ll) { if (!f(ll, relativeStart, relativeLength)) { - walkFns.done = true; + this.done = true; } } }; @@ -63024,7 +65220,7 @@ var ts; return source.substring(0, s) + nt + source.substring(s + dl, source.length); } if (this.root.charCount() === 0) { - if (newText) { + if (newText !== undefined) { this.load(LineIndex.linesFromText(newText).lines); return this; } @@ -63404,12 +65600,6 @@ var ts; function LineLeaf(text) { this.text = text; } - LineLeaf.prototype.setUdata = function (data) { - this.udata = data; - }; - LineLeaf.prototype.getUdata = function () { - return this.udata; - }; LineLeaf.prototype.isLeaf = function () { return true; }; @@ -63427,7 +65617,7 @@ var ts; server.LineLeaf = LineLeaf; })(server = ts.server || (ts.server = {})); })(ts || (ts = {})); -var debugObjectHost = new Function("return this")(); +var debugObjectHost = (function () { return this; })(); var ts; (function (ts) { function logInternalError(logger, err) { @@ -63505,6 +65695,12 @@ var ts; } return this.shimHost.getProjectVersion(); }; + LanguageServiceShimHostAdapter.prototype.getTypeRootsVersion = function () { + if (!this.shimHost.getTypeRootsVersion) { + return 0; + } + return this.shimHost.getTypeRootsVersion(); + }; LanguageServiceShimHostAdapter.prototype.useCaseSensitiveFileNames = function () { return this.shimHost.useCaseSensitiveFileNames ? this.shimHost.useCaseSensitiveFileNames() : false; }; @@ -63698,11 +65894,12 @@ var ts; var LanguageServiceShimObject = (function (_super) { __extends(LanguageServiceShimObject, _super); function LanguageServiceShimObject(factory, host, languageService) { - _super.call(this, factory); - this.host = host; - this.languageService = languageService; - this.logPerformance = false; - this.logger = this.host; + var _this = _super.call(this, factory) || this; + _this.host = host; + _this.languageService = languageService; + _this.logPerformance = false; + _this.logger = _this.host; + return _this; } LanguageServiceShimObject.prototype.forwardJSONCall = function (actionDescription, action) { return forwardJSONCall(this.logger, actionDescription, action, this.logPerformance); @@ -63881,6 +66078,10 @@ var ts; var _this = this; return this.forwardJSONCall("getNavigationBarItems('" + fileName + "')", function () { return _this.languageService.getNavigationBarItems(fileName); }); }; + LanguageServiceShimObject.prototype.getNavigationTree = function (fileName) { + var _this = this; + return this.forwardJSONCall("getNavigationTree('" + fileName + "')", function () { return _this.languageService.getNavigationTree(fileName); }); + }; LanguageServiceShimObject.prototype.getOutliningSpans = function (fileName) { var _this = this; return this.forwardJSONCall("getOutliningSpans('" + fileName + "')", function () { return _this.languageService.getOutliningSpans(fileName); }); @@ -63905,10 +66106,11 @@ var ts; var ClassifierShimObject = (function (_super) { __extends(ClassifierShimObject, _super); function ClassifierShimObject(factory, logger) { - _super.call(this, factory); - this.logger = logger; - this.logPerformance = false; - this.classifier = ts.createClassifier(); + var _this = _super.call(this, factory) || this; + _this.logger = logger; + _this.logPerformance = false; + _this.classifier = ts.createClassifier(); + return _this; } ClassifierShimObject.prototype.getEncodedLexicalClassifications = function (text, lexState, syntacticClassifierAbsent) { var _this = this; @@ -63930,10 +66132,11 @@ var ts; var CoreServicesShimObject = (function (_super) { __extends(CoreServicesShimObject, _super); function CoreServicesShimObject(factory, logger, host) { - _super.call(this, factory); - this.logger = logger; - this.host = host; - this.logPerformance = false; + var _this = _super.call(this, factory) || this; + _this.logger = logger; + _this.host = host; + _this.logPerformance = false; + return _this; } CoreServicesShimObject.prototype.forwardJSONCall = function (actionDescription, action) { return forwardJSONCall(this.logger, actionDescription, action, this.logPerformance); diff --git a/lib/typescript.d.ts b/lib/typescript.d.ts index 9116a629d6d7c..c915bd9efc877 100644 --- a/lib/typescript.d.ts +++ b/lib/typescript.d.ts @@ -29,6 +29,7 @@ declare namespace ts { contains(fileName: Path): boolean; remove(fileName: Path): void; forEachValue(f: (key: Path, v: T) => void): void; + getKeys(): Path[]; clear(): void; } interface TextRange { @@ -388,7 +389,6 @@ declare namespace ts { ContextFlags = 1540096, TypeExcludesFlags = 327680, } - type ModifiersArray = NodeArray; enum ModifierFlags { None = 0, Export = 1, @@ -425,12 +425,21 @@ declare namespace ts { interface NodeArray extends Array, TextRange { hasTrailingComma?: boolean; } - interface Token extends Node { - __tokenTag: any; - } - interface Modifier extends Token { - } + interface Token extends Node { + kind: TKind; + } + type DotDotDotToken = Token; + type QuestionToken = Token; + type ColonToken = Token; + type EqualsToken = Token; + type AsteriskToken = Token; + type EqualsGreaterThanToken = Token; + type EndOfFileToken = Token; + type AtToken = Token; + type Modifier = Token | Token | Token | Token | Token | Token | Token | Token | Token | Token | Token; + type ModifiersArray = NodeArray; interface Identifier extends PrimaryExpression { + kind: SyntaxKind.Identifier; text: string; originalKeywordKind?: SyntaxKind; } @@ -438,6 +447,7 @@ declare namespace ts { resolvedSymbol: Symbol; } interface QualifiedName extends Node { + kind: SyntaxKind.QualifiedName; left: EntityName; right: Identifier; } @@ -449,15 +459,18 @@ declare namespace ts { name?: DeclarationName; } interface DeclarationStatement extends Declaration, Statement { - name?: Identifier; + name?: Identifier | LiteralExpression; } interface ComputedPropertyName extends Node { + kind: SyntaxKind.ComputedPropertyName; expression: Expression; } interface Decorator extends Node { + kind: SyntaxKind.Decorator; expression: LeftHandSideExpression; } interface TypeParameterDeclaration extends Declaration { + kind: SyntaxKind.TypeParameter; name: Identifier; constraint?: TypeNode; expression?: Expression; @@ -469,40 +482,48 @@ declare namespace ts { type?: TypeNode; } interface CallSignatureDeclaration extends SignatureDeclaration, TypeElement { + kind: SyntaxKind.CallSignature; } interface ConstructSignatureDeclaration extends SignatureDeclaration, TypeElement { + kind: SyntaxKind.ConstructSignature; } type BindingName = Identifier | BindingPattern; interface VariableDeclaration extends Declaration { + kind: SyntaxKind.VariableDeclaration; parent?: VariableDeclarationList; name: BindingName; type?: TypeNode; initializer?: Expression; } interface VariableDeclarationList extends Node { + kind: SyntaxKind.VariableDeclarationList; declarations: NodeArray; } interface ParameterDeclaration extends Declaration { - dotDotDotToken?: Node; + kind: SyntaxKind.Parameter; + dotDotDotToken?: DotDotDotToken; name: BindingName; - questionToken?: Node; + questionToken?: QuestionToken; type?: TypeNode; initializer?: Expression; } interface BindingElement extends Declaration { + kind: SyntaxKind.BindingElement; propertyName?: PropertyName; - dotDotDotToken?: Node; + dotDotDotToken?: DotDotDotToken; name: BindingName; initializer?: Expression; } interface PropertySignature extends TypeElement { + kind: SyntaxKind.PropertySignature | SyntaxKind.JSDocRecordMember; name: PropertyName; - questionToken?: Node; + questionToken?: QuestionToken; type?: TypeNode; initializer?: Expression; } interface PropertyDeclaration extends ClassElement { - questionToken?: Node; + kind: SyntaxKind.PropertyDeclaration; + questionToken?: QuestionToken; name: PropertyName; type?: TypeNode; initializer?: Expression; @@ -513,22 +534,23 @@ declare namespace ts { } type ObjectLiteralElementLike = PropertyAssignment | ShorthandPropertyAssignment | MethodDeclaration | AccessorDeclaration; interface PropertyAssignment extends ObjectLiteralElement { - _propertyAssignmentBrand: any; + kind: SyntaxKind.PropertyAssignment; name: PropertyName; - questionToken?: Node; + questionToken?: QuestionToken; initializer: Expression; } interface ShorthandPropertyAssignment extends ObjectLiteralElement { + kind: SyntaxKind.ShorthandPropertyAssignment; name: Identifier; - questionToken?: Node; - equalsToken?: Node; + questionToken?: QuestionToken; + equalsToken?: Token; objectAssignmentInitializer?: Expression; } interface VariableLikeDeclaration extends Declaration { propertyName?: PropertyName; - dotDotDotToken?: Node; + dotDotDotToken?: DotDotDotToken; name: DeclarationName; - questionToken?: Node; + questionToken?: QuestionToken; type?: TypeNode; initializer?: Expression; } @@ -539,10 +561,12 @@ declare namespace ts { elements: NodeArray; } interface ObjectBindingPattern extends BindingPattern { + kind: SyntaxKind.ObjectBindingPattern; elements: NodeArray; } type ArrayBindingElement = BindingElement | OmittedExpression; interface ArrayBindingPattern extends BindingPattern { + kind: SyntaxKind.ArrayBindingPattern; elements: NodeArray; } /** @@ -555,95 +579,116 @@ declare namespace ts { */ interface FunctionLikeDeclaration extends SignatureDeclaration { _functionLikeDeclarationBrand: any; - asteriskToken?: Node; - questionToken?: Node; + asteriskToken?: AsteriskToken; + questionToken?: QuestionToken; body?: Block | Expression; } interface FunctionDeclaration extends FunctionLikeDeclaration, DeclarationStatement { + kind: SyntaxKind.FunctionDeclaration; name?: Identifier; body?: FunctionBody; } interface MethodSignature extends SignatureDeclaration, TypeElement { + kind: SyntaxKind.MethodSignature; name: PropertyName; } interface MethodDeclaration extends FunctionLikeDeclaration, ClassElement, ObjectLiteralElement { + kind: SyntaxKind.MethodDeclaration; name: PropertyName; body?: FunctionBody; } interface ConstructorDeclaration extends FunctionLikeDeclaration, ClassElement { + kind: SyntaxKind.Constructor; body?: FunctionBody; } interface SemicolonClassElement extends ClassElement { - _semicolonClassElementBrand: any; + kind: SyntaxKind.SemicolonClassElement; } - interface AccessorDeclaration extends FunctionLikeDeclaration, ClassElement, ObjectLiteralElement { - _accessorDeclarationBrand: any; + interface GetAccessorDeclaration extends FunctionLikeDeclaration, ClassElement, ObjectLiteralElement { + kind: SyntaxKind.GetAccessor; name: PropertyName; body: FunctionBody; } - interface GetAccessorDeclaration extends AccessorDeclaration { - } - interface SetAccessorDeclaration extends AccessorDeclaration { + interface SetAccessorDeclaration extends FunctionLikeDeclaration, ClassElement, ObjectLiteralElement { + kind: SyntaxKind.SetAccessor; + name: PropertyName; + body: FunctionBody; } + type AccessorDeclaration = GetAccessorDeclaration | SetAccessorDeclaration; interface IndexSignatureDeclaration extends SignatureDeclaration, ClassElement, TypeElement { - _indexSignatureDeclarationBrand: any; + kind: SyntaxKind.IndexSignature; } interface TypeNode extends Node { _typeNodeBrand: any; } + interface KeywordTypeNode extends TypeNode { + kind: SyntaxKind.AnyKeyword | SyntaxKind.NumberKeyword | SyntaxKind.BooleanKeyword | SyntaxKind.StringKeyword | SyntaxKind.SymbolKeyword | SyntaxKind.VoidKeyword; + } interface ThisTypeNode extends TypeNode { - _thisTypeNodeBrand: any; + kind: SyntaxKind.ThisType; } interface FunctionOrConstructorTypeNode extends TypeNode, SignatureDeclaration { - _functionOrConstructorTypeNodeBrand: any; + kind: SyntaxKind.FunctionType | SyntaxKind.ConstructorType; } interface FunctionTypeNode extends FunctionOrConstructorTypeNode { + kind: SyntaxKind.FunctionType; } interface ConstructorTypeNode extends FunctionOrConstructorTypeNode { + kind: SyntaxKind.ConstructorType; } interface TypeReferenceNode extends TypeNode { + kind: SyntaxKind.TypeReference; typeName: EntityName; typeArguments?: NodeArray; } interface TypePredicateNode extends TypeNode { + kind: SyntaxKind.TypePredicate; parameterName: Identifier | ThisTypeNode; type: TypeNode; } interface TypeQueryNode extends TypeNode { + kind: SyntaxKind.TypeQuery; exprName: EntityName; } interface TypeLiteralNode extends TypeNode, Declaration { + kind: SyntaxKind.TypeLiteral; members: NodeArray; } interface ArrayTypeNode extends TypeNode { + kind: SyntaxKind.ArrayType; elementType: TypeNode; } interface TupleTypeNode extends TypeNode { + kind: SyntaxKind.TupleType; elementTypes: NodeArray; } interface UnionOrIntersectionTypeNode extends TypeNode { + kind: SyntaxKind.UnionType | SyntaxKind.IntersectionType; types: NodeArray; } interface UnionTypeNode extends UnionOrIntersectionTypeNode { + kind: SyntaxKind.UnionType; } interface IntersectionTypeNode extends UnionOrIntersectionTypeNode { + kind: SyntaxKind.IntersectionType; } interface ParenthesizedTypeNode extends TypeNode { + kind: SyntaxKind.ParenthesizedType; type: TypeNode; } interface LiteralTypeNode extends TypeNode { - _stringLiteralTypeBrand: any; + kind: SyntaxKind.LiteralType; literal: Expression; } interface StringLiteral extends LiteralExpression { - _stringLiteralBrand: any; + kind: SyntaxKind.StringLiteral; } interface Expression extends Node { _expressionBrand: any; contextualType?: Type; } interface OmittedExpression extends Expression { - _omittedExpressionBrand: any; + kind: SyntaxKind.OmittedExpression; } interface UnaryExpression extends Expression { _unaryExpressionBrand: any; @@ -651,13 +696,17 @@ declare namespace ts { interface IncrementExpression extends UnaryExpression { _incrementExpressionBrand: any; } + type PrefixUnaryOperator = SyntaxKind.PlusPlusToken | SyntaxKind.MinusMinusToken | SyntaxKind.PlusToken | SyntaxKind.MinusToken | SyntaxKind.TildeToken | SyntaxKind.ExclamationToken; interface PrefixUnaryExpression extends IncrementExpression { - operator: SyntaxKind; + kind: SyntaxKind.PrefixUnaryExpression; + operator: PrefixUnaryOperator; operand: UnaryExpression; } + type PostfixUnaryOperator = SyntaxKind.PlusPlusToken | SyntaxKind.MinusMinusToken; interface PostfixUnaryExpression extends IncrementExpression { + kind: SyntaxKind.PostfixUnaryExpression; operand: LeftHandSideExpression; - operator: SyntaxKind; + operator: PostfixUnaryOperator; } interface PostfixExpression extends UnaryExpression { _postfixExpressionBrand: any; @@ -671,42 +720,83 @@ declare namespace ts { interface PrimaryExpression extends MemberExpression { _primaryExpressionBrand: any; } + interface NullLiteral extends PrimaryExpression { + kind: SyntaxKind.NullKeyword; + } + interface BooleanLiteral extends PrimaryExpression { + kind: SyntaxKind.TrueKeyword | SyntaxKind.FalseKeyword; + } + interface ThisExpression extends PrimaryExpression { + kind: SyntaxKind.ThisKeyword; + } + interface SuperExpression extends PrimaryExpression { + kind: SyntaxKind.SuperKeyword; + } interface DeleteExpression extends UnaryExpression { + kind: SyntaxKind.DeleteExpression; expression: UnaryExpression; } interface TypeOfExpression extends UnaryExpression { + kind: SyntaxKind.TypeOfExpression; expression: UnaryExpression; } interface VoidExpression extends UnaryExpression { + kind: SyntaxKind.VoidExpression; expression: UnaryExpression; } interface AwaitExpression extends UnaryExpression { + kind: SyntaxKind.AwaitExpression; expression: UnaryExpression; } interface YieldExpression extends Expression { - asteriskToken?: Node; + kind: SyntaxKind.YieldExpression; + asteriskToken?: AsteriskToken; expression?: Expression; } + type ExponentiationOperator = SyntaxKind.AsteriskAsteriskToken; + type MultiplicativeOperator = SyntaxKind.AsteriskToken | SyntaxKind.SlashToken | SyntaxKind.PercentToken; + type MultiplicativeOperatorOrHigher = ExponentiationOperator | MultiplicativeOperator; + type AdditiveOperator = SyntaxKind.PlusToken | SyntaxKind.MinusToken; + type AdditiveOperatorOrHigher = MultiplicativeOperatorOrHigher | AdditiveOperator; + type ShiftOperator = SyntaxKind.LessThanLessThanToken | SyntaxKind.GreaterThanGreaterThanToken | SyntaxKind.GreaterThanGreaterThanGreaterThanToken; + type ShiftOperatorOrHigher = AdditiveOperatorOrHigher | ShiftOperator; + type RelationalOperator = SyntaxKind.LessThanToken | SyntaxKind.LessThanEqualsToken | SyntaxKind.GreaterThanToken | SyntaxKind.GreaterThanEqualsToken | SyntaxKind.InstanceOfKeyword | SyntaxKind.InKeyword; + type RelationalOperatorOrHigher = ShiftOperatorOrHigher | RelationalOperator; + type EqualityOperator = SyntaxKind.EqualsEqualsToken | SyntaxKind.EqualsEqualsEqualsToken | SyntaxKind.ExclamationEqualsEqualsToken | SyntaxKind.ExclamationEqualsToken; + type EqualityOperatorOrHigher = RelationalOperatorOrHigher | EqualityOperator; + type BitwiseOperator = SyntaxKind.AmpersandToken | SyntaxKind.BarToken | SyntaxKind.CaretToken; + type BitwiseOperatorOrHigher = EqualityOperatorOrHigher | BitwiseOperator; + type LogicalOperator = SyntaxKind.AmpersandAmpersandToken | SyntaxKind.BarBarToken; + type LogicalOperatorOrHigher = BitwiseOperatorOrHigher | LogicalOperator; + type CompoundAssignmentOperator = SyntaxKind.PlusEqualsToken | SyntaxKind.MinusEqualsToken | SyntaxKind.AsteriskAsteriskEqualsToken | SyntaxKind.AsteriskEqualsToken | SyntaxKind.SlashEqualsToken | SyntaxKind.PercentEqualsToken | SyntaxKind.AmpersandEqualsToken | SyntaxKind.BarEqualsToken | SyntaxKind.CaretEqualsToken | SyntaxKind.LessThanLessThanEqualsToken | SyntaxKind.GreaterThanGreaterThanGreaterThanEqualsToken | SyntaxKind.GreaterThanGreaterThanEqualsToken; + type AssignmentOperator = SyntaxKind.EqualsToken | CompoundAssignmentOperator; + type AssignmentOperatorOrHigher = LogicalOperatorOrHigher | AssignmentOperator; + type BinaryOperator = AssignmentOperatorOrHigher | SyntaxKind.CommaToken; + type BinaryOperatorToken = Token; interface BinaryExpression extends Expression, Declaration { + kind: SyntaxKind.BinaryExpression; left: Expression; - operatorToken: Node; + operatorToken: BinaryOperatorToken; right: Expression; } interface ConditionalExpression extends Expression { + kind: SyntaxKind.ConditionalExpression; condition: Expression; - questionToken: Node; + questionToken: QuestionToken; whenTrue: Expression; - colonToken: Node; + colonToken: ColonToken; whenFalse: Expression; } type FunctionBody = Block; type ConciseBody = FunctionBody | Expression; interface FunctionExpression extends PrimaryExpression, FunctionLikeDeclaration { + kind: SyntaxKind.FunctionExpression; name?: Identifier; body: FunctionBody; } interface ArrowFunction extends Expression, FunctionLikeDeclaration { - equalsGreaterThanToken: Node; + kind: SyntaxKind.ArrowFunction; + equalsGreaterThanToken: EqualsGreaterThanToken; body: ConciseBody; } interface LiteralLikeNode extends Node { @@ -717,29 +807,46 @@ declare namespace ts { interface LiteralExpression extends LiteralLikeNode, PrimaryExpression { _literalExpressionBrand: any; } + interface RegularExpressionLiteral extends LiteralExpression { + kind: SyntaxKind.RegularExpressionLiteral; + } + interface NoSubstitutionTemplateLiteral extends LiteralExpression { + kind: SyntaxKind.NoSubstitutionTemplateLiteral; + } interface NumericLiteral extends LiteralExpression { - _numericLiteralBrand: any; + kind: SyntaxKind.NumericLiteral; trailingComment?: string; } - interface TemplateLiteralFragment extends LiteralLikeNode { - _templateLiteralFragmentBrand: any; + interface TemplateHead extends LiteralLikeNode { + kind: SyntaxKind.TemplateHead; + } + interface TemplateMiddle extends LiteralLikeNode { + kind: SyntaxKind.TemplateMiddle; } - type Template = TemplateExpression | LiteralExpression; + interface TemplateTail extends LiteralLikeNode { + kind: SyntaxKind.TemplateTail; + } + type TemplateLiteral = TemplateExpression | NoSubstitutionTemplateLiteral; interface TemplateExpression extends PrimaryExpression { - head: TemplateLiteralFragment; + kind: SyntaxKind.TemplateExpression; + head: TemplateHead; templateSpans: NodeArray; } interface TemplateSpan extends Node { + kind: SyntaxKind.TemplateSpan; expression: Expression; - literal: TemplateLiteralFragment; + literal: TemplateMiddle | TemplateTail; } interface ParenthesizedExpression extends PrimaryExpression { + kind: SyntaxKind.ParenthesizedExpression; expression: Expression; } interface ArrayLiteralExpression extends PrimaryExpression { + kind: SyntaxKind.ArrayLiteralExpression; elements: NodeArray; } interface SpreadElementExpression extends Expression { + kind: SyntaxKind.SpreadElementExpression; expression: Expression; } /** @@ -752,104 +859,141 @@ declare namespace ts { properties: NodeArray; } interface ObjectLiteralExpression extends ObjectLiteralExpressionBase { + kind: SyntaxKind.ObjectLiteralExpression; } type EntityNameExpression = Identifier | PropertyAccessEntityNameExpression; type EntityNameOrEntityNameExpression = EntityName | EntityNameExpression; interface PropertyAccessExpression extends MemberExpression, Declaration { + kind: SyntaxKind.PropertyAccessExpression; expression: LeftHandSideExpression; name: Identifier; } + interface SuperPropertyAccessExpression extends PropertyAccessExpression { + expression: SuperExpression; + } /** Brand for a PropertyAccessExpression which, like a QualifiedName, consists of a sequence of identifiers separated by dots. */ interface PropertyAccessEntityNameExpression extends PropertyAccessExpression { _propertyAccessExpressionLikeQualifiedNameBrand?: any; expression: EntityNameExpression; } interface ElementAccessExpression extends MemberExpression { + kind: SyntaxKind.ElementAccessExpression; expression: LeftHandSideExpression; argumentExpression?: Expression; } + interface SuperElementAccessExpression extends ElementAccessExpression { + expression: SuperExpression; + } + type SuperProperty = SuperPropertyAccessExpression | SuperElementAccessExpression; interface CallExpression extends LeftHandSideExpression, Declaration { + kind: SyntaxKind.CallExpression; expression: LeftHandSideExpression; typeArguments?: NodeArray; arguments: NodeArray; } + interface SuperCall extends CallExpression { + expression: SuperExpression; + } interface ExpressionWithTypeArguments extends TypeNode { + kind: SyntaxKind.ExpressionWithTypeArguments; expression: LeftHandSideExpression; typeArguments?: NodeArray; } - interface NewExpression extends CallExpression, PrimaryExpression { + interface NewExpression extends PrimaryExpression, Declaration { + kind: SyntaxKind.NewExpression; + expression: LeftHandSideExpression; + typeArguments?: NodeArray; + arguments: NodeArray; } interface TaggedTemplateExpression extends MemberExpression { + kind: SyntaxKind.TaggedTemplateExpression; tag: LeftHandSideExpression; - template: Template; + template: TemplateLiteral; } type CallLikeExpression = CallExpression | NewExpression | TaggedTemplateExpression | Decorator; interface AsExpression extends Expression { + kind: SyntaxKind.AsExpression; expression: Expression; type: TypeNode; } interface TypeAssertion extends UnaryExpression { + kind: SyntaxKind.TypeAssertionExpression; type: TypeNode; expression: UnaryExpression; } type AssertionExpression = TypeAssertion | AsExpression; interface NonNullExpression extends LeftHandSideExpression { + kind: SyntaxKind.NonNullExpression; expression: Expression; } interface JsxElement extends PrimaryExpression { + kind: SyntaxKind.JsxElement; openingElement: JsxOpeningElement; children: NodeArray; closingElement: JsxClosingElement; } type JsxTagNameExpression = PrimaryExpression | PropertyAccessExpression; interface JsxOpeningElement extends Expression { - _openingElementBrand?: any; + kind: SyntaxKind.JsxOpeningElement; tagName: JsxTagNameExpression; attributes: NodeArray; } - interface JsxSelfClosingElement extends PrimaryExpression, JsxOpeningElement { - _selfClosingElementBrand?: any; + interface JsxSelfClosingElement extends PrimaryExpression { + kind: SyntaxKind.JsxSelfClosingElement; + tagName: JsxTagNameExpression; + attributes: NodeArray; } type JsxOpeningLikeElement = JsxSelfClosingElement | JsxOpeningElement; type JsxAttributeLike = JsxAttribute | JsxSpreadAttribute; interface JsxAttribute extends Node { + kind: SyntaxKind.JsxAttribute; name: Identifier; initializer?: StringLiteral | JsxExpression; } interface JsxSpreadAttribute extends Node { + kind: SyntaxKind.JsxSpreadAttribute; expression: Expression; } interface JsxClosingElement extends Node { + kind: SyntaxKind.JsxClosingElement; tagName: JsxTagNameExpression; } interface JsxExpression extends Expression { + kind: SyntaxKind.JsxExpression; expression?: Expression; } interface JsxText extends Node { - _jsxTextExpressionBrand: any; + kind: SyntaxKind.JsxText; } type JsxChild = JsxText | JsxExpression | JsxElement | JsxSelfClosingElement; interface Statement extends Node { _statementBrand: any; } interface EmptyStatement extends Statement { + kind: SyntaxKind.EmptyStatement; } interface DebuggerStatement extends Statement { + kind: SyntaxKind.DebuggerStatement; } interface MissingDeclaration extends DeclarationStatement, ClassElement, ObjectLiteralElement, TypeElement { + kind: SyntaxKind.MissingDeclaration; name?: Identifier; } type BlockLike = SourceFile | Block | ModuleBlock | CaseClause; interface Block extends Statement { + kind: SyntaxKind.Block; statements: NodeArray; } interface VariableStatement extends Statement { + kind: SyntaxKind.VariableStatement; declarationList: VariableDeclarationList; } interface ExpressionStatement extends Statement { + kind: SyntaxKind.ExpressionStatement; expression: Expression; } interface IfStatement extends Statement { + kind: SyntaxKind.IfStatement; expression: Expression; thenStatement: Statement; elseStatement?: Statement; @@ -858,68 +1002,85 @@ declare namespace ts { statement: Statement; } interface DoStatement extends IterationStatement { + kind: SyntaxKind.DoStatement; expression: Expression; } interface WhileStatement extends IterationStatement { + kind: SyntaxKind.WhileStatement; expression: Expression; } type ForInitializer = VariableDeclarationList | Expression; interface ForStatement extends IterationStatement { + kind: SyntaxKind.ForStatement; initializer?: ForInitializer; condition?: Expression; incrementor?: Expression; } interface ForInStatement extends IterationStatement { + kind: SyntaxKind.ForInStatement; initializer: ForInitializer; expression: Expression; } interface ForOfStatement extends IterationStatement { + kind: SyntaxKind.ForOfStatement; initializer: ForInitializer; expression: Expression; } interface BreakStatement extends Statement { + kind: SyntaxKind.BreakStatement; label?: Identifier; } interface ContinueStatement extends Statement { + kind: SyntaxKind.ContinueStatement; label?: Identifier; } type BreakOrContinueStatement = BreakStatement | ContinueStatement; interface ReturnStatement extends Statement { + kind: SyntaxKind.ReturnStatement; expression?: Expression; } interface WithStatement extends Statement { + kind: SyntaxKind.WithStatement; expression: Expression; statement: Statement; } interface SwitchStatement extends Statement { + kind: SyntaxKind.SwitchStatement; expression: Expression; caseBlock: CaseBlock; possiblyExhaustive?: boolean; } interface CaseBlock extends Node { + kind: SyntaxKind.CaseBlock; clauses: NodeArray; } interface CaseClause extends Node { + kind: SyntaxKind.CaseClause; expression: Expression; statements: NodeArray; } interface DefaultClause extends Node { + kind: SyntaxKind.DefaultClause; statements: NodeArray; } type CaseOrDefaultClause = CaseClause | DefaultClause; interface LabeledStatement extends Statement { + kind: SyntaxKind.LabeledStatement; label: Identifier; statement: Statement; } interface ThrowStatement extends Statement { + kind: SyntaxKind.ThrowStatement; expression: Expression; } interface TryStatement extends Statement { + kind: SyntaxKind.TryStatement; tryBlock: Block; catchClause?: CatchClause; finallyBlock?: Block; } interface CatchClause extends Node { + kind: SyntaxKind.CatchClause; variableDeclaration: VariableDeclaration; block: Block; } @@ -931,9 +1092,11 @@ declare namespace ts { members: NodeArray; } interface ClassDeclaration extends ClassLikeDeclaration, DeclarationStatement { + kind: SyntaxKind.ClassDeclaration; name?: Identifier; } interface ClassExpression extends ClassLikeDeclaration, PrimaryExpression { + kind: SyntaxKind.ClassExpression; } interface ClassElement extends Declaration { _classElementBrand: any; @@ -942,85 +1105,108 @@ declare namespace ts { interface TypeElement extends Declaration { _typeElementBrand: any; name?: PropertyName; - questionToken?: Node; + questionToken?: QuestionToken; } interface InterfaceDeclaration extends DeclarationStatement { + kind: SyntaxKind.InterfaceDeclaration; name: Identifier; typeParameters?: NodeArray; heritageClauses?: NodeArray; members: NodeArray; } interface HeritageClause extends Node { + kind: SyntaxKind.HeritageClause; token: SyntaxKind; types?: NodeArray; } interface TypeAliasDeclaration extends DeclarationStatement { + kind: SyntaxKind.TypeAliasDeclaration; name: Identifier; typeParameters?: NodeArray; type: TypeNode; } interface EnumMember extends Declaration { + kind: SyntaxKind.EnumMember; name: PropertyName; initializer?: Expression; } interface EnumDeclaration extends DeclarationStatement { + kind: SyntaxKind.EnumDeclaration; name: Identifier; members: NodeArray; } type ModuleBody = ModuleBlock | ModuleDeclaration; type ModuleName = Identifier | StringLiteral; interface ModuleDeclaration extends DeclarationStatement { + kind: SyntaxKind.ModuleDeclaration; name: Identifier | LiteralExpression; - body?: ModuleBlock | ModuleDeclaration; + body?: ModuleBlock | NamespaceDeclaration; + } + interface NamespaceDeclaration extends ModuleDeclaration { + name: Identifier; + body: ModuleBlock | NamespaceDeclaration; } interface ModuleBlock extends Node, Statement { + kind: SyntaxKind.ModuleBlock; statements: NodeArray; } type ModuleReference = EntityName | ExternalModuleReference; interface ImportEqualsDeclaration extends DeclarationStatement { + kind: SyntaxKind.ImportEqualsDeclaration; name: Identifier; moduleReference: ModuleReference; } interface ExternalModuleReference extends Node { + kind: SyntaxKind.ExternalModuleReference; expression?: Expression; } interface ImportDeclaration extends Statement { + kind: SyntaxKind.ImportDeclaration; importClause?: ImportClause; moduleSpecifier: Expression; } type NamedImportBindings = NamespaceImport | NamedImports; interface ImportClause extends Declaration { + kind: SyntaxKind.ImportClause; name?: Identifier; namedBindings?: NamedImportBindings; } interface NamespaceImport extends Declaration { + kind: SyntaxKind.NamespaceImport; name: Identifier; } interface NamespaceExportDeclaration extends DeclarationStatement { + kind: SyntaxKind.NamespaceExportDeclaration; name: Identifier; moduleReference: LiteralLikeNode; } interface ExportDeclaration extends DeclarationStatement { + kind: SyntaxKind.ExportDeclaration; exportClause?: NamedExports; moduleSpecifier?: Expression; } interface NamedImports extends Node { + kind: SyntaxKind.NamedImports; elements: NodeArray; } interface NamedExports extends Node { + kind: SyntaxKind.NamedExports; elements: NodeArray; } type NamedImportsOrExports = NamedImports | NamedExports; interface ImportSpecifier extends Declaration { + kind: SyntaxKind.ImportSpecifier; propertyName?: Identifier; name: Identifier; } interface ExportSpecifier extends Declaration { + kind: SyntaxKind.ExportSpecifier; propertyName?: Identifier; name: Identifier; } type ImportOrExportSpecifier = ImportSpecifier | ExportSpecifier; interface ExportAssignment extends DeclarationStatement { + kind: SyntaxKind.ExportAssignment; isExportEquals?: boolean; expression: Expression; } @@ -1032,95 +1218,121 @@ declare namespace ts { kind: SyntaxKind; } interface JSDocTypeExpression extends Node { + kind: SyntaxKind.JSDocTypeExpression; type: JSDocType; } interface JSDocType extends TypeNode { _jsDocTypeBrand: any; } interface JSDocAllType extends JSDocType { - _JSDocAllTypeBrand: any; + kind: SyntaxKind.JSDocAllType; } interface JSDocUnknownType extends JSDocType { - _JSDocUnknownTypeBrand: any; + kind: SyntaxKind.JSDocUnknownType; } interface JSDocArrayType extends JSDocType { + kind: SyntaxKind.JSDocArrayType; elementType: JSDocType; } interface JSDocUnionType extends JSDocType { + kind: SyntaxKind.JSDocUnionType; types: NodeArray; } interface JSDocTupleType extends JSDocType { + kind: SyntaxKind.JSDocTupleType; types: NodeArray; } interface JSDocNonNullableType extends JSDocType { + kind: SyntaxKind.JSDocNonNullableType; type: JSDocType; } interface JSDocNullableType extends JSDocType { + kind: SyntaxKind.JSDocNullableType; type: JSDocType; } - interface JSDocRecordType extends JSDocType, TypeLiteralNode { + interface JSDocRecordType extends JSDocType { + kind: SyntaxKind.JSDocRecordType; literal: TypeLiteralNode; } interface JSDocTypeReference extends JSDocType { + kind: SyntaxKind.JSDocTypeReference; name: EntityName; typeArguments: NodeArray; } interface JSDocOptionalType extends JSDocType { + kind: SyntaxKind.JSDocOptionalType; type: JSDocType; } interface JSDocFunctionType extends JSDocType, SignatureDeclaration { + kind: SyntaxKind.JSDocFunctionType; parameters: NodeArray; type: JSDocType; } interface JSDocVariadicType extends JSDocType { + kind: SyntaxKind.JSDocVariadicType; type: JSDocType; } interface JSDocConstructorType extends JSDocType { + kind: SyntaxKind.JSDocConstructorType; type: JSDocType; } interface JSDocThisType extends JSDocType { + kind: SyntaxKind.JSDocThisType; type: JSDocType; } interface JSDocLiteralType extends JSDocType { + kind: SyntaxKind.JSDocLiteralType; literal: LiteralTypeNode; } type JSDocTypeReferencingNode = JSDocThisType | JSDocConstructorType | JSDocVariadicType | JSDocOptionalType | JSDocNullableType | JSDocNonNullableType; interface JSDocRecordMember extends PropertySignature { + kind: SyntaxKind.JSDocRecordMember; name: Identifier | LiteralExpression; type?: JSDocType; } interface JSDoc extends Node { + kind: SyntaxKind.JSDocComment; tags: NodeArray | undefined; comment: string | undefined; } interface JSDocTag extends Node { - atToken: Node; + atToken: AtToken; tagName: Identifier; comment: string | undefined; } + interface JSDocUnknownTag extends JSDocTag { + kind: SyntaxKind.JSDocTag; + } interface JSDocTemplateTag extends JSDocTag { + kind: SyntaxKind.JSDocTemplateTag; typeParameters: NodeArray; } interface JSDocReturnTag extends JSDocTag { + kind: SyntaxKind.JSDocReturnTag; typeExpression: JSDocTypeExpression; } interface JSDocTypeTag extends JSDocTag { + kind: SyntaxKind.JSDocTypeTag; typeExpression: JSDocTypeExpression; } interface JSDocTypedefTag extends JSDocTag, Declaration { + kind: SyntaxKind.JSDocTypedefTag; name?: Identifier; typeExpression?: JSDocTypeExpression; jsDocTypeLiteral?: JSDocTypeLiteral; } interface JSDocPropertyTag extends JSDocTag, TypeElement { + kind: SyntaxKind.JSDocPropertyTag; name: Identifier; typeExpression: JSDocTypeExpression; } interface JSDocTypeLiteral extends JSDocType { + kind: SyntaxKind.JSDocTypeLiteral; jsDocPropertyTags?: NodeArray; jsDocTypeTag?: JSDocTypeTag; } interface JSDocParameterTag extends JSDocTag { + kind: SyntaxKind.JSDocParameterTag; /** the parameter name, if provided *before* the type (TypeScript-style) */ preParameterName?: Identifier; typeExpression?: JSDocTypeExpression; @@ -1149,7 +1361,7 @@ declare namespace ts { id?: number; } interface FlowStart extends FlowNode { - container?: FunctionExpression | ArrowFunction; + container?: FunctionExpression | ArrowFunction | MethodDeclaration; } interface FlowLabel extends FlowNode { antecedents: FlowNode[]; @@ -1178,8 +1390,9 @@ declare namespace ts { name: string; } interface SourceFile extends Declaration { + kind: SyntaxKind.SourceFile; statements: NodeArray; - endOfFileToken: Node; + endOfFileToken: Token; fileName: string; path: Path; text: string; @@ -1245,7 +1458,7 @@ declare namespace ts { * used for writing the JavaScript and declaration files. Otherwise, the writeFile parameter * will be invoked when writing the JavaScript and declaration files. */ - emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback, cancellationToken?: CancellationToken): EmitResult; + emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean): EmitResult; getOptionsDiagnostics(cancellationToken?: CancellationToken): Diagnostic[]; getGlobalDiagnostics(cancellationToken?: CancellationToken): Diagnostic[]; getSyntacticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[]; @@ -1372,6 +1585,7 @@ declare namespace ts { UseFullyQualifiedType = 128, InFirstTypeArgument = 256, InTypeAlias = 512, + UseTypeAliasValue = 1024, } enum SymbolFormatFlags { None = 0, @@ -1387,9 +1601,10 @@ declare namespace ts { type: Type; } interface ThisTypePredicate extends TypePredicateBase { - _thisTypePredicateBrand: any; + kind: TypePredicateKind.This; } interface IdentifierTypePredicate extends TypePredicateBase { + kind: TypePredicateKind.Identifier; parameterName: string; parameterIndex: number; } @@ -1495,8 +1710,6 @@ declare namespace ts { Intersection = 1048576, Anonymous = 2097152, Instantiated = 4194304, - ThisType = 268435456, - ObjectLiteralPatternWithComputedProperties = 536870912, Literal = 480, StringOrNumberLiteral = 96, PossiblyFalsy = 7406, @@ -1509,7 +1722,7 @@ declare namespace ts { StructuredType = 4161536, StructuredOrTypeParameter = 4177920, Narrowable = 4178943, - NotUnionOrUnit = 2589191, + NotUnionOrUnit = 2589185, } type DestructuringPattern = BindingPattern | ObjectLiteralExpression | ArrayLiteralExpression; interface Type { @@ -1531,6 +1744,7 @@ declare namespace ts { baseType: EnumType & UnionType; } interface ObjectType extends Type { + isObjectLiteralPatternWithComputedProperties?: boolean; } interface InterfaceType extends ObjectType { typeParameters: TypeParameter[]; @@ -1614,15 +1828,13 @@ declare namespace ts { Classic = 1, NodeJs = 2, } - type RootPaths = string[]; - type PathSubstitutions = MapLike; - type TsConfigOnlyOptions = RootPaths | PathSubstitutions; - type CompilerOptionsValue = string | number | boolean | (string | number)[] | TsConfigOnlyOptions; + type CompilerOptionsValue = string | number | boolean | (string | number)[] | string[] | MapLike; interface CompilerOptions { allowJs?: boolean; allowSyntheticDefaultImports?: boolean; allowUnreachableCode?: boolean; allowUnusedLabels?: boolean; + alwaysStrict?: boolean; baseUrl?: string; charset?: string; declaration?: boolean; @@ -1660,13 +1872,13 @@ declare namespace ts { out?: string; outDir?: string; outFile?: string; - paths?: PathSubstitutions; + paths?: MapLike; preserveConstEnums?: boolean; project?: string; reactNamespace?: string; removeComments?: boolean; rootDir?: string; - rootDirs?: RootPaths; + rootDirs?: string[]; skipLibCheck?: boolean; skipDefaultLibCheck?: boolean; sourceMap?: boolean; @@ -1742,6 +1954,7 @@ declare namespace ts { raw?: any; errors: Diagnostic[]; wildcardDirectories?: MapLike; + compileOnSave?: boolean; } enum WatchDirectoryFlags { None = 0, @@ -1846,7 +2059,7 @@ declare namespace ts { directoryName: string; referenceCount: number; } - var sys: System; + let sys: System; } declare namespace ts { interface ErrorCallback { @@ -1944,10 +2157,6 @@ declare namespace ts { function updateSourceFile(sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile; } declare namespace ts { - /** The version of the TypeScript compiler release */ - const version = "2.1.0"; - function findConfigFile(searchPath: string, fileExists: (fileName: string) => boolean, configName?: string): string; - function resolveTripleslashReference(moduleName: string, containingFile: string): string; function getEffectiveTypeRoots(options: CompilerOptions, host: { directoryExists?: (directoryName: string) => boolean; getCurrentDirectory?: () => string; @@ -1958,9 +2167,24 @@ declare namespace ts { * is assumed to be the same as root directory of the project. */ function resolveTypeReferenceDirective(typeReferenceDirectiveName: string, containingFile: string, options: CompilerOptions, host: ModuleResolutionHost): ResolvedTypeReferenceDirectiveWithFailedLookupLocations; + /** + * Given a set of options, returns the set of type directive names + * that should be included for this program automatically. + * This list could either come from the config file, + * or from enumerating the types root + initial secondary types lookup location. + * More type directives might appear in the program later as a result of loading actual source files; + * this list is only the set of defaults that are implicitly included. + */ + function getAutomaticTypeDirectiveNames(options: CompilerOptions, host: ModuleResolutionHost): string[]; function resolveModuleName(moduleName: string, containingFile: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost): ResolvedModuleWithFailedLookupLocations; function nodeModuleNameResolver(moduleName: string, containingFile: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost): ResolvedModuleWithFailedLookupLocations; function classicNameResolver(moduleName: string, containingFile: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost): ResolvedModuleWithFailedLookupLocations; +} +declare namespace ts { + /** The version of the TypeScript compiler release */ + const version = "2.1.0"; + function findConfigFile(searchPath: string, fileExists: (fileName: string) => boolean, configName?: string): string; + function resolveTripleslashReference(moduleName: string, containingFile: string): string; function createCompilerHost(options: CompilerOptions, setParentNodes?: boolean): CompilerHost; function getPreEmitDiagnostics(program: Program, sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[]; interface FormatDiagnosticsHost { @@ -1970,15 +2194,6 @@ declare namespace ts { } function formatDiagnostics(diagnostics: Diagnostic[], host: FormatDiagnosticsHost): string; function flattenDiagnosticMessageText(messageText: string | DiagnosticMessageChain, newLine: string): string; - /** - * Given a set of options, returns the set of type directive names - * that should be included for this program automatically. - * This list could either come from the config file, - * or from enumerating the types root + initial secondary types lookup location. - * More type directives might appear in the program later as a result of loading actual source files; - * this list is only the set of defaults that are implicitly included. - */ - function getAutomaticTypeDirectiveNames(options: CompilerOptions, host: ModuleResolutionHost): string[]; function createProgram(rootNames: string[], options: CompilerOptions, host?: CompilerHost, oldProgram?: Program): Program; } declare namespace ts { @@ -1995,7 +2210,7 @@ declare namespace ts { * @param fileName The path to the config file * @param jsonText The text of the config file */ - function parseConfigFileTextToJson(fileName: string, jsonText: string): { + function parseConfigFileTextToJson(fileName: string, jsonText: string, stripComments?: boolean): { config?: any; error?: Diagnostic; }; @@ -2007,12 +2222,13 @@ declare namespace ts { * file to. e.g. outDir */ function parseJsonConfigFileContent(json: any, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[]): ParsedCommandLine; + function convertCompileOnSaveOptionFromJson(jsonOption: any, basePath: string, errors: Diagnostic[]): boolean; function convertCompilerOptionsFromJson(jsonOptions: any, basePath: string, configFileName?: string): { options: CompilerOptions; errors: Diagnostic[]; }; function convertTypingOptionsFromJson(jsonOptions: any, basePath: string, configFileName?: string): { - options: CompilerOptions; + options: TypingOptions; errors: Diagnostic[]; }; } @@ -2118,6 +2334,7 @@ declare namespace ts { readDirectory?(path: string, extensions?: string[], exclude?: string[], include?: string[]): string[]; readFile?(path: string, encoding?: string): string; fileExists?(path: string): boolean; + getTypeRootsVersion?(): number; resolveModuleNames?(moduleNames: string[], containingFile: string): ResolvedModule[]; resolveTypeReferenceDirectives?(typeDirectiveNames: string[], containingFile: string): ResolvedTypeReferenceDirective[]; directoryExists?(directoryName: string): boolean; @@ -2155,18 +2372,20 @@ declare namespace ts { getDocumentHighlights(fileName: string, position: number, filesToSearch: string[]): DocumentHighlights[]; /** @deprecated */ getOccurrencesAtPosition(fileName: string, position: number): ReferenceEntry[]; - getNavigateToItems(searchValue: string, maxResultCount?: number, fileName?: string): NavigateToItem[]; + getNavigateToItems(searchValue: string, maxResultCount?: number, fileName?: string, excludeDtsFiles?: boolean): NavigateToItem[]; getNavigationBarItems(fileName: string): NavigationBarItem[]; + getNavigationTree(fileName: string): NavigationTree; getOutliningSpans(fileName: string): OutliningSpan[]; getTodoComments(fileName: string, descriptors: TodoCommentDescriptor[]): TodoComment[]; getBraceMatchingAtPosition(fileName: string, position: number): TextSpan[]; - getIndentationAtPosition(fileName: string, position: number, options: EditorOptions): number; - getFormattingEditsForRange(fileName: string, start: number, end: number, options: FormatCodeOptions): TextChange[]; - getFormattingEditsForDocument(fileName: string, options: FormatCodeOptions): TextChange[]; - getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: FormatCodeOptions): TextChange[]; + getIndentationAtPosition(fileName: string, position: number, options: EditorOptions | EditorSettings): number; + getFormattingEditsForRange(fileName: string, start: number, end: number, options: FormatCodeOptions | FormatCodeSettings): TextChange[]; + getFormattingEditsForDocument(fileName: string, options: FormatCodeOptions | FormatCodeSettings): TextChange[]; + getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: FormatCodeOptions | FormatCodeSettings): TextChange[]; getDocCommentTemplateAtPosition(fileName: string, position: number): TextInsertion; isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean; - getEmitOutput(fileName: string): EmitOutput; + getCodeFixesAtPosition(fileName: string, start: number, end: number, errorCodes: number[]): CodeAction[]; + getEmitOutput(fileName: string, emitOnlyDtsFiles?: boolean): EmitOutput; getProgram(): Program; dispose(): void; } @@ -2178,6 +2397,12 @@ declare namespace ts { textSpan: TextSpan; classificationType: string; } + /** + * Navigation bar interface designed for visual studio's dual-column layout. + * This does not form a proper tree. + * The navbar is returned as a list of top-level items, each of which has a list of child items. + * Child items always have an empty array for their `childItems`. + */ interface NavigationBarItem { text: string; kind: string; @@ -2188,6 +2413,25 @@ declare namespace ts { bolded: boolean; grayed: boolean; } + /** + * Node in a tree of nested declarations in a file. + * The top node is always a script or module node. + */ + interface NavigationTree { + /** Name of the declaration, or a short description, e.g. "". */ + text: string; + /** A ScriptElementKind */ + kind: string; + /** ScriptElementKindModifier separated by commas, e.g. "public,abstract" */ + kindModifiers: string; + /** + * Spans of the nodes that generated this declaration. + * There will be more than one if this is the result of merging. + */ + spans: TextSpan[]; + /** Present if non-empty */ + childItems?: NavigationTree[]; + } interface TodoCommentDescriptor { text: string; priority: number; @@ -2201,6 +2445,16 @@ declare namespace ts { span: TextSpan; newText: string; } + interface FileTextChanges { + fileName: string; + textChanges: TextChange[]; + } + interface CodeAction { + /** Description of the code action to display in the UI of the editor */ + description: string; + /** Text changes to apply to each file as part of the code action */ + changes: FileTextChanges[]; + } interface TextInsertion { newText: string; /** The position in newText the caret should point to after the insertion. */ @@ -2246,6 +2500,11 @@ declare namespace ts { containerName: string; containerKind: string; } + enum IndentStyle { + None = 0, + Block = 1, + Smart = 2, + } interface EditorOptions { BaseIndentSize?: number; IndentSize: number; @@ -2254,10 +2513,13 @@ declare namespace ts { ConvertTabsToSpaces: boolean; IndentStyle: IndentStyle; } - enum IndentStyle { - None = 0, - Block = 1, - Smart = 2, + interface EditorSettings { + baseIndentSize?: number; + indentSize?: number; + tabSize?: number; + newLineCharacter?: string; + convertTabsToSpaces?: boolean; + indentStyle?: IndentStyle; } interface FormatCodeOptions extends EditorOptions { InsertSpaceAfterCommaDelimiter: boolean; @@ -2273,7 +2535,21 @@ declare namespace ts { InsertSpaceAfterTypeAssertion?: boolean; PlaceOpenBraceOnNewLineForFunctions: boolean; PlaceOpenBraceOnNewLineForControlBlocks: boolean; - [s: string]: boolean | number | string | undefined; + } + interface FormatCodeSettings extends EditorSettings { + insertSpaceAfterCommaDelimiter?: boolean; + insertSpaceAfterSemicolonInForStatements?: boolean; + insertSpaceBeforeAndAfterBinaryOperators?: boolean; + insertSpaceAfterKeywordsInControlFlowStatements?: boolean; + insertSpaceAfterFunctionKeywordForAnonymousFunctions?: boolean; + insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis?: boolean; + insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets?: boolean; + insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces?: boolean; + insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces?: boolean; + insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces?: boolean; + insertSpaceAfterTypeAssertion?: boolean; + placeOpenBraceOnNewLineForFunctions?: boolean; + placeOpenBraceOnNewLineForControlBlocks?: boolean; } interface DefinitionInfo { fileName: string; @@ -2366,7 +2642,11 @@ declare namespace ts { argumentCount: number; } interface CompletionInfo { + isGlobalCompletion: boolean; isMemberCompletion: boolean; + /** + * true when the current location also allows for a new identifier + */ isNewIdentifierLocation: boolean; entries: CompletionEntry[]; } @@ -2687,8 +2967,10 @@ declare namespace ts { interface DisplayPartsSymbolWriter extends SymbolWriter { displayParts(): SymbolDisplayPart[]; } + function toEditorSettings(options: EditorOptions | EditorSettings): EditorSettings; function displayPartsToString(displayParts: SymbolDisplayPart[]): string; function getDefaultCompilerOptions(): CompilerOptions; + function getSupportedCodeFixes(): string[]; function createLanguageServiceSourceFile(fileName: string, scriptSnapshot: IScriptSnapshot, scriptTarget: ScriptTarget, version: string, setNodeParents: boolean, scriptKind?: ScriptKind): SourceFile; let disableIncrementalParsing: boolean; function updateLanguageServiceSourceFile(sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile; diff --git a/lib/typescript.js b/lib/typescript.js index 14fbdce59a832..c534a7259019f 100644 --- a/lib/typescript.js +++ b/lib/typescript.js @@ -502,6 +502,7 @@ var ts; TypeFormatFlags[TypeFormatFlags["UseFullyQualifiedType"] = 128] = "UseFullyQualifiedType"; TypeFormatFlags[TypeFormatFlags["InFirstTypeArgument"] = 256] = "InFirstTypeArgument"; TypeFormatFlags[TypeFormatFlags["InTypeAlias"] = 512] = "InTypeAlias"; + TypeFormatFlags[TypeFormatFlags["UseTypeAliasValue"] = 1024] = "UseTypeAliasValue"; })(ts.TypeFormatFlags || (ts.TypeFormatFlags = {})); var TypeFormatFlags = ts.TypeFormatFlags; (function (SymbolFormatFlags) { @@ -685,8 +686,6 @@ var ts; TypeFlags[TypeFlags["ContainsObjectLiteral"] = 67108864] = "ContainsObjectLiteral"; /* @internal */ TypeFlags[TypeFlags["ContainsAnyFunctionType"] = 134217728] = "ContainsAnyFunctionType"; - TypeFlags[TypeFlags["ThisType"] = 268435456] = "ThisType"; - TypeFlags[TypeFlags["ObjectLiteralPatternWithComputedProperties"] = 536870912] = "ObjectLiteralPatternWithComputedProperties"; /* @internal */ TypeFlags[TypeFlags["Nullable"] = 6144] = "Nullable"; TypeFlags[TypeFlags["Literal"] = 480] = "Literal"; @@ -709,7 +708,7 @@ var ts; // 'Narrowable' types are types where narrowing actually narrows. // This *should* be every type other than null, undefined, void, and never TypeFlags[TypeFlags["Narrowable"] = 4178943] = "Narrowable"; - TypeFlags[TypeFlags["NotUnionOrUnit"] = 2589191] = "NotUnionOrUnit"; + TypeFlags[TypeFlags["NotUnionOrUnit"] = 2589185] = "NotUnionOrUnit"; /* @internal */ TypeFlags[TypeFlags["RequiresWidening"] = 100663296] = "RequiresWidening"; /* @internal */ @@ -993,36 +992,44 @@ var ts; })(ts.TransformFlags || (ts.TransformFlags = {})); var TransformFlags = ts.TransformFlags; /* @internal */ - (function (NodeEmitFlags) { - NodeEmitFlags[NodeEmitFlags["EmitEmitHelpers"] = 1] = "EmitEmitHelpers"; - NodeEmitFlags[NodeEmitFlags["EmitExportStar"] = 2] = "EmitExportStar"; - NodeEmitFlags[NodeEmitFlags["EmitSuperHelper"] = 4] = "EmitSuperHelper"; - NodeEmitFlags[NodeEmitFlags["EmitAdvancedSuperHelper"] = 8] = "EmitAdvancedSuperHelper"; - NodeEmitFlags[NodeEmitFlags["UMDDefine"] = 16] = "UMDDefine"; - NodeEmitFlags[NodeEmitFlags["SingleLine"] = 32] = "SingleLine"; - NodeEmitFlags[NodeEmitFlags["AdviseOnEmitNode"] = 64] = "AdviseOnEmitNode"; - NodeEmitFlags[NodeEmitFlags["NoSubstitution"] = 128] = "NoSubstitution"; - NodeEmitFlags[NodeEmitFlags["CapturesThis"] = 256] = "CapturesThis"; - NodeEmitFlags[NodeEmitFlags["NoLeadingSourceMap"] = 512] = "NoLeadingSourceMap"; - NodeEmitFlags[NodeEmitFlags["NoTrailingSourceMap"] = 1024] = "NoTrailingSourceMap"; - NodeEmitFlags[NodeEmitFlags["NoSourceMap"] = 1536] = "NoSourceMap"; - NodeEmitFlags[NodeEmitFlags["NoNestedSourceMaps"] = 2048] = "NoNestedSourceMaps"; - NodeEmitFlags[NodeEmitFlags["NoTokenLeadingSourceMaps"] = 4096] = "NoTokenLeadingSourceMaps"; - NodeEmitFlags[NodeEmitFlags["NoTokenTrailingSourceMaps"] = 8192] = "NoTokenTrailingSourceMaps"; - NodeEmitFlags[NodeEmitFlags["NoTokenSourceMaps"] = 12288] = "NoTokenSourceMaps"; - NodeEmitFlags[NodeEmitFlags["NoLeadingComments"] = 16384] = "NoLeadingComments"; - NodeEmitFlags[NodeEmitFlags["NoTrailingComments"] = 32768] = "NoTrailingComments"; - NodeEmitFlags[NodeEmitFlags["NoComments"] = 49152] = "NoComments"; - NodeEmitFlags[NodeEmitFlags["NoNestedComments"] = 65536] = "NoNestedComments"; - NodeEmitFlags[NodeEmitFlags["ExportName"] = 131072] = "ExportName"; - NodeEmitFlags[NodeEmitFlags["LocalName"] = 262144] = "LocalName"; - NodeEmitFlags[NodeEmitFlags["Indented"] = 524288] = "Indented"; - NodeEmitFlags[NodeEmitFlags["NoIndentation"] = 1048576] = "NoIndentation"; - NodeEmitFlags[NodeEmitFlags["AsyncFunctionBody"] = 2097152] = "AsyncFunctionBody"; - NodeEmitFlags[NodeEmitFlags["ReuseTempVariableScope"] = 4194304] = "ReuseTempVariableScope"; - NodeEmitFlags[NodeEmitFlags["CustomPrologue"] = 8388608] = "CustomPrologue"; - })(ts.NodeEmitFlags || (ts.NodeEmitFlags = {})); - var NodeEmitFlags = ts.NodeEmitFlags; + (function (EmitFlags) { + EmitFlags[EmitFlags["EmitEmitHelpers"] = 1] = "EmitEmitHelpers"; + EmitFlags[EmitFlags["EmitExportStar"] = 2] = "EmitExportStar"; + EmitFlags[EmitFlags["EmitSuperHelper"] = 4] = "EmitSuperHelper"; + EmitFlags[EmitFlags["EmitAdvancedSuperHelper"] = 8] = "EmitAdvancedSuperHelper"; + EmitFlags[EmitFlags["UMDDefine"] = 16] = "UMDDefine"; + EmitFlags[EmitFlags["SingleLine"] = 32] = "SingleLine"; + EmitFlags[EmitFlags["AdviseOnEmitNode"] = 64] = "AdviseOnEmitNode"; + EmitFlags[EmitFlags["NoSubstitution"] = 128] = "NoSubstitution"; + EmitFlags[EmitFlags["CapturesThis"] = 256] = "CapturesThis"; + EmitFlags[EmitFlags["NoLeadingSourceMap"] = 512] = "NoLeadingSourceMap"; + EmitFlags[EmitFlags["NoTrailingSourceMap"] = 1024] = "NoTrailingSourceMap"; + EmitFlags[EmitFlags["NoSourceMap"] = 1536] = "NoSourceMap"; + EmitFlags[EmitFlags["NoNestedSourceMaps"] = 2048] = "NoNestedSourceMaps"; + EmitFlags[EmitFlags["NoTokenLeadingSourceMaps"] = 4096] = "NoTokenLeadingSourceMaps"; + EmitFlags[EmitFlags["NoTokenTrailingSourceMaps"] = 8192] = "NoTokenTrailingSourceMaps"; + EmitFlags[EmitFlags["NoTokenSourceMaps"] = 12288] = "NoTokenSourceMaps"; + EmitFlags[EmitFlags["NoLeadingComments"] = 16384] = "NoLeadingComments"; + EmitFlags[EmitFlags["NoTrailingComments"] = 32768] = "NoTrailingComments"; + EmitFlags[EmitFlags["NoComments"] = 49152] = "NoComments"; + EmitFlags[EmitFlags["NoNestedComments"] = 65536] = "NoNestedComments"; + EmitFlags[EmitFlags["ExportName"] = 131072] = "ExportName"; + EmitFlags[EmitFlags["LocalName"] = 262144] = "LocalName"; + EmitFlags[EmitFlags["Indented"] = 524288] = "Indented"; + EmitFlags[EmitFlags["NoIndentation"] = 1048576] = "NoIndentation"; + EmitFlags[EmitFlags["AsyncFunctionBody"] = 2097152] = "AsyncFunctionBody"; + EmitFlags[EmitFlags["ReuseTempVariableScope"] = 4194304] = "ReuseTempVariableScope"; + EmitFlags[EmitFlags["CustomPrologue"] = 8388608] = "CustomPrologue"; + })(ts.EmitFlags || (ts.EmitFlags = {})); + var EmitFlags = ts.EmitFlags; + /* @internal */ + (function (EmitContext) { + EmitContext[EmitContext["SourceFile"] = 0] = "SourceFile"; + EmitContext[EmitContext["Expression"] = 1] = "Expression"; + EmitContext[EmitContext["IdentifierName"] = 2] = "IdentifierName"; + EmitContext[EmitContext["Unspecified"] = 3] = "Unspecified"; + })(ts.EmitContext || (ts.EmitContext = {})); + var EmitContext = ts.EmitContext; })(ts || (ts = {})); /*@internal*/ var ts; @@ -1032,7 +1039,6 @@ var ts; })(ts || (ts = {})); /*@internal*/ /** Performance measurements for the compiler. */ -var ts; (function (ts) { var performance; (function (performance) { @@ -1164,6 +1170,7 @@ var ts; contains: contains, remove: remove, forEachValue: forEachValueInMap, + getKeys: getKeys, clear: clear }; function forEachValueInMap(f) { @@ -1171,6 +1178,13 @@ var ts; f(key, files[key]); } } + function getKeys() { + var keys = []; + for (var key in files) { + keys.push(key); + } + return keys; + } // path should already be well-formed so it does not need to be normalized function get(path) { return files[toKey(path)]; @@ -1501,6 +1515,7 @@ var ts; return array1.concat(array2); } ts.concatenate = concatenate; + // TODO: fixme (N^2) - add optional comparer so collection can be sorted before deduplication. function deduplicate(array, areEqual) { var result; if (array) { @@ -1604,16 +1619,22 @@ var ts; * @param array A sorted array whose first element must be no larger than number * @param number The value to be searched for in the array. */ - function binarySearch(array, value) { + function binarySearch(array, value, comparer) { + if (!array || array.length === 0) { + return -1; + } var low = 0; var high = array.length - 1; + comparer = comparer !== undefined + ? comparer + : function (v1, v2) { return (v1 < v2 ? -1 : (v1 > v2 ? 1 : 0)); }; while (low <= high) { var middle = low + ((high - low) >> 1); var midValue = array[middle]; - if (midValue === value) { + if (comparer(midValue, value) === 0) { return middle; } - else if (midValue > value) { + else if (comparer(midValue, value) > 0) { high = middle - 1; } else { @@ -1929,6 +1950,56 @@ var ts; }; } ts.memoize = memoize; + function chain(a, b, c, d, e) { + if (e) { + var args_2 = []; + for (var i = 0; i < arguments.length; i++) { + args_2[i] = arguments[i]; + } + return function (t) { return compose.apply(void 0, map(args_2, function (f) { return f(t); })); }; + } + else if (d) { + return function (t) { return compose(a(t), b(t), c(t), d(t)); }; + } + else if (c) { + return function (t) { return compose(a(t), b(t), c(t)); }; + } + else if (b) { + return function (t) { return compose(a(t), b(t)); }; + } + else if (a) { + return function (t) { return compose(a(t)); }; + } + else { + return function (t) { return function (u) { return u; }; }; + } + } + ts.chain = chain; + function compose(a, b, c, d, e) { + if (e) { + var args_3 = []; + for (var i = 0; i < arguments.length; i++) { + args_3[i] = arguments[i]; + } + return function (t) { return reduceLeft(args_3, function (u, f) { return f(u); }, t); }; + } + else if (d) { + return function (t) { return d(c(b(a(t)))); }; + } + else if (c) { + return function (t) { return c(b(a(t))); }; + } + else if (b) { + return function (t) { return b(a(t)); }; + } + else if (a) { + return function (t) { return a(t); }; + } + else { + return function (t) { return t; }; + } + } + ts.compose = compose; function formatStringFromArgs(text, args, baseIndex) { baseIndex = baseIndex || 0; return text.replace(/{(\d+)}/g, function (match, index) { return args[+index + baseIndex]; }); @@ -2096,7 +2167,9 @@ var ts; return path.replace(/\\/g, "/"); } ts.normalizeSlashes = normalizeSlashes; - // Returns length of path root (i.e. length of "/", "x:/", "//server/share/, file:///user/files") + /** + * Returns length of path root (i.e. length of "/", "x:/", "//server/share/, file:///user/files") + */ function getRootLength(path) { if (path.charCodeAt(0) === 47 /* slash */) { if (path.charCodeAt(1) !== 47 /* slash */) @@ -2129,6 +2202,11 @@ var ts; return 0; } ts.getRootLength = getRootLength; + /** + * Internally, we represent paths as strings with '/' as the directory separator. + * When we make system calls (eg: LanguageServiceHost.getDirectory()), + * we expect the host to correctly handle paths in our specified format. + */ ts.directorySeparator = "/"; var directorySeparatorCharCode = 47 /* slash */; function getNormalizedParts(normalizedSlashedPath, rootLength) { @@ -2178,10 +2256,49 @@ var ts; return path && !isRootedDiskPath(path) && path.indexOf("://") !== -1; } ts.isUrl = isUrl; + function isExternalModuleNameRelative(moduleName) { + // TypeScript 1.0 spec (April 2014): 11.2.1 + // An external module name is "relative" if the first term is "." or "..". + return /^\.\.?($|[\\/])/.test(moduleName); + } + ts.isExternalModuleNameRelative = isExternalModuleNameRelative; + function getEmitScriptTarget(compilerOptions) { + return compilerOptions.target || 0 /* ES3 */; + } + ts.getEmitScriptTarget = getEmitScriptTarget; + function getEmitModuleKind(compilerOptions) { + return typeof compilerOptions.module === "number" ? + compilerOptions.module : + getEmitScriptTarget(compilerOptions) === 2 /* ES6 */ ? ts.ModuleKind.ES6 : ts.ModuleKind.CommonJS; + } + ts.getEmitModuleKind = getEmitModuleKind; + /* @internal */ + function hasZeroOrOneAsteriskCharacter(str) { + var seenAsterisk = false; + for (var i = 0; i < str.length; i++) { + if (str.charCodeAt(i) === 42 /* asterisk */) { + if (!seenAsterisk) { + seenAsterisk = true; + } + else { + // have already seen asterisk + return false; + } + } + } + return true; + } + ts.hasZeroOrOneAsteriskCharacter = hasZeroOrOneAsteriskCharacter; function isRootedDiskPath(path) { return getRootLength(path) !== 0; } ts.isRootedDiskPath = isRootedDiskPath; + function convertToRelativePath(absoluteOrRelativePath, basePath, getCanonicalFileName) { + return !isRootedDiskPath(absoluteOrRelativePath) + ? absoluteOrRelativePath + : getRelativePathToDirectoryOrUrl(basePath, absoluteOrRelativePath, basePath, getCanonicalFileName, /*isAbsolutePathAnUrl*/ false); + } + ts.convertToRelativePath = convertToRelativePath; function normalizedPathComponents(path, rootLength) { var normalizedParts = getNormalizedParts(path, rootLength); return [path.substr(0, rootLength)].concat(normalizedParts); @@ -2625,6 +2742,14 @@ var ts; return options && options.allowJs ? allSupportedExtensions : ts.supportedTypeScriptExtensions; } ts.getSupportedExtensions = getSupportedExtensions; + function hasJavaScriptFileExtension(fileName) { + return forEach(ts.supportedJavascriptExtensions, function (extension) { return fileExtensionIs(fileName, extension); }); + } + ts.hasJavaScriptFileExtension = hasJavaScriptFileExtension; + function hasTypeScriptFileExtension(fileName) { + return forEach(ts.supportedTypeScriptExtensions, function (extension) { return fileExtensionIs(fileName, extension); }); + } + ts.hasTypeScriptFileExtension = hasTypeScriptFileExtension; function isSupportedSourceFileName(fileName, compilerOptions) { if (!fileName) { return false; @@ -2737,7 +2862,6 @@ var ts; this.transformFlags = 0 /* None */; this.parent = undefined; this.original = undefined; - this.transformId = 0; } ts.objectAllocator = { getNodeConstructor: function () { return Node; }, @@ -2757,9 +2881,9 @@ var ts; var AssertionLevel = ts.AssertionLevel; var Debug; (function (Debug) { - var currentAssertionLevel; + Debug.currentAssertionLevel = 0 /* None */; function shouldAssert(level) { - return getCurrentAssertionLevel() >= level; + return Debug.currentAssertionLevel >= level; } Debug.shouldAssert = shouldAssert; function assert(expression, message, verboseDebugInfo) { @@ -2777,30 +2901,7 @@ var ts; Debug.assert(/*expression*/ false, message); } Debug.fail = fail; - function getCurrentAssertionLevel() { - if (currentAssertionLevel !== undefined) { - return currentAssertionLevel; - } - if (ts.sys === undefined) { - return 0 /* None */; - } - var developmentMode = /^development$/i.test(getEnvironmentVariable("NODE_ENV")); - currentAssertionLevel = developmentMode - ? 1 /* Normal */ - : 0 /* None */; - return currentAssertionLevel; - } })(Debug = ts.Debug || (ts.Debug = {})); - function getEnvironmentVariable(name, host) { - if (host && host.getEnvironmentVariable) { - return host.getEnvironmentVariable(name); - } - if (ts.sys && ts.sys.getEnvironmentVariable) { - return ts.sys.getEnvironmentVariable(name); - } - return ""; - } - ts.getEnvironmentVariable = getEnvironmentVariable; /** Remove an item from an array, moving everything to its right one space left. */ function orderedRemoveItemAt(array, index) { // This seems to be faster than either `array.splice(i, 1)` or `array.copyWithin(i, i+ 1)`. @@ -2836,6 +2937,84 @@ var ts; : (function (fileName) { return fileName.toLowerCase(); }); } ts.createGetCanonicalFileName = createGetCanonicalFileName; + /** + * patternStrings contains both pattern strings (containing "*") and regular strings. + * Return an exact match if possible, or a pattern match, or undefined. + * (These are verified by verifyCompilerOptions to have 0 or 1 "*" characters.) + */ + /* @internal */ + function matchPatternOrExact(patternStrings, candidate) { + var patterns = []; + for (var _i = 0, patternStrings_1 = patternStrings; _i < patternStrings_1.length; _i++) { + var patternString = patternStrings_1[_i]; + var pattern = tryParsePattern(patternString); + if (pattern) { + patterns.push(pattern); + } + else if (patternString === candidate) { + // pattern was matched as is - no need to search further + return patternString; + } + } + return findBestPatternMatch(patterns, function (_) { return _; }, candidate); + } + ts.matchPatternOrExact = matchPatternOrExact; + /* @internal */ + function patternText(_a) { + var prefix = _a.prefix, suffix = _a.suffix; + return prefix + "*" + suffix; + } + ts.patternText = patternText; + /** + * Given that candidate matches pattern, returns the text matching the '*'. + * E.g.: matchedText(tryParsePattern("foo*baz"), "foobarbaz") === "bar" + */ + /* @internal */ + function matchedText(pattern, candidate) { + Debug.assert(isPatternMatch(pattern, candidate)); + return candidate.substr(pattern.prefix.length, candidate.length - pattern.suffix.length); + } + ts.matchedText = matchedText; + /** Return the object corresponding to the best pattern to match `candidate`. */ + /* @internal */ + function findBestPatternMatch(values, getPattern, candidate) { + var matchedValue = undefined; + // use length of prefix as betterness criteria + var longestMatchPrefixLength = -1; + for (var _i = 0, values_1 = values; _i < values_1.length; _i++) { + var v = values_1[_i]; + var pattern = getPattern(v); + if (isPatternMatch(pattern, candidate) && pattern.prefix.length > longestMatchPrefixLength) { + longestMatchPrefixLength = pattern.prefix.length; + matchedValue = v; + } + } + return matchedValue; + } + ts.findBestPatternMatch = findBestPatternMatch; + function isPatternMatch(_a, candidate) { + var prefix = _a.prefix, suffix = _a.suffix; + return candidate.length >= prefix.length + suffix.length && + startsWith(candidate, prefix) && + endsWith(candidate, suffix); + } + /* @internal */ + function tryParsePattern(pattern) { + // This should be verified outside of here and a proper error thrown. + Debug.assert(hasZeroOrOneAsteriskCharacter(pattern)); + var indexOfStar = pattern.indexOf("*"); + return indexOfStar === -1 ? undefined : { + prefix: pattern.substr(0, indexOfStar), + suffix: pattern.substr(indexOfStar + 1) + }; + } + ts.tryParsePattern = tryParsePattern; + function positionIsSynthesized(pos) { + // This is a fast way of testing the following conditions: + // pos === undefined || pos === null || isNaN(pos) || pos < 0; + return !(pos >= 0); + } + ts.positionIsSynthesized = positionIsSynthesized; })(ts || (ts = {})); /// var ts; @@ -3040,9 +3219,17 @@ var ts; function isNode4OrLater() { return parseInt(process.version.charAt(1)) >= 4; } + function isFileSystemCaseSensitive() { + // win32\win64 are case insensitive platforms + if (platform === "win32" || platform === "win64") { + return false; + } + // convert current file name to upper case / lower case and check if file exists + // (guards against cases when name is already all uppercase or lowercase) + return !fileExists(__filename.toUpperCase()) || !fileExists(__filename.toLowerCase()); + } var platform = _os.platform(); - // win32\win64 are case insensitive platforms, MacOS (darwin) by default is also case insensitive - var useCaseSensitiveFileNames = platform !== "win32" && platform !== "win64" && platform !== "darwin"; + var useCaseSensitiveFileNames = isFileSystemCaseSensitive(); function readFile(fileName, encoding) { if (!fileExists(fileName)) { return undefined; @@ -3182,6 +3369,9 @@ var ts; // Node 4.0 `fs.watch` function supports the "recursive" option on both OSX and Windows // (ref: https://github.com/nodejs/node/pull/2649 and https://github.com/Microsoft/TypeScript/issues/4643) var options; + if (!directoryExists(directoryName)) { + return; + } if (isNode4OrLater() && (process.platform === "win32" || process.platform === "darwin")) { options = { persistent: true, recursive: !!recursive }; } @@ -3299,21 +3489,46 @@ var ts; realpath: realpath }; } + function recursiveCreateDirectory(directoryPath, sys) { + var basePath = ts.getDirectoryPath(directoryPath); + var shouldCreateParent = directoryPath !== basePath && !sys.directoryExists(basePath); + if (shouldCreateParent) { + recursiveCreateDirectory(basePath, sys); + } + if (shouldCreateParent || !sys.directoryExists(directoryPath)) { + sys.createDirectory(directoryPath); + } + } + var sys; if (typeof ChakraHost !== "undefined") { - return getChakraSystem(); + sys = getChakraSystem(); } else if (typeof WScript !== "undefined" && typeof ActiveXObject === "function") { - return getWScriptSystem(); + sys = getWScriptSystem(); } else if (typeof process !== "undefined" && process.nextTick && !process.browser && typeof require !== "undefined") { // process and process.nextTick checks if current environment is node-like // process.browser check excludes webpack and browserify - return getNodeSystem(); + sys = getNodeSystem(); } - else { - return undefined; // Unsupported host + if (sys) { + // patch writefile to create folder before writing the file + var originalWriteFile_1 = sys.writeFile; + sys.writeFile = function (path, data, writeBom) { + var directoryPath = ts.getDirectoryPath(ts.normalizeSlashes(path)); + if (directoryPath && !sys.directoryExists(directoryPath)) { + recursiveCreateDirectory(directoryPath, sys); + } + originalWriteFile_1.call(sys, path, data, writeBom); + }; } + return sys; })(); + if (ts.sys && ts.sys.getEnvironmentVariable) { + ts.Debug.currentAssertionLevel = /^development$/i.test(ts.sys.getEnvironmentVariable("NODE_ENV")) + ? 1 /* Normal */ + : 0 /* None */; + } })(ts || (ts = {})); // /// @@ -3641,7 +3856,7 @@ var ts; The_right_hand_side_of_a_for_in_statement_must_be_of_type_any_an_object_type_or_a_type_parameter: { code: 2407, category: ts.DiagnosticCategory.Error, key: "The_right_hand_side_of_a_for_in_statement_must_be_of_type_any_an_object_type_or_a_type_parameter_2407", message: "The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter." }, Setters_cannot_return_a_value: { code: 2408, category: ts.DiagnosticCategory.Error, key: "Setters_cannot_return_a_value_2408", message: "Setters cannot return a value." }, Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class: { code: 2409, category: ts.DiagnosticCategory.Error, key: "Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class_2409", message: "Return type of constructor signature must be assignable to the instance type of the class" }, - All_symbols_within_a_with_block_will_be_resolved_to_any: { code: 2410, category: ts.DiagnosticCategory.Error, key: "All_symbols_within_a_with_block_will_be_resolved_to_any_2410", message: "All symbols within a 'with' block will be resolved to 'any'." }, + The_with_statement_is_not_supported_All_symbols_in_a_with_block_will_have_type_any: { code: 2410, category: ts.DiagnosticCategory.Error, key: "The_with_statement_is_not_supported_All_symbols_in_a_with_block_will_have_type_any_2410", message: "The 'with' statement is not supported. All symbols in a 'with' block will have type 'any'." }, Property_0_of_type_1_is_not_assignable_to_string_index_type_2: { code: 2411, category: ts.DiagnosticCategory.Error, key: "Property_0_of_type_1_is_not_assignable_to_string_index_type_2_2411", message: "Property '{0}' of type '{1}' is not assignable to string index type '{2}'." }, Property_0_of_type_1_is_not_assignable_to_numeric_index_type_2: { code: 2412, category: ts.DiagnosticCategory.Error, key: "Property_0_of_type_1_is_not_assignable_to_numeric_index_type_2_2412", message: "Property '{0}' of type '{1}' is not assignable to numeric index type '{2}'." }, Numeric_index_type_0_is_not_assignable_to_string_index_type_1: { code: 2413, category: ts.DiagnosticCategory.Error, key: "Numeric_index_type_0_is_not_assignable_to_string_index_type_1_2413", message: "Numeric index type '{0}' is not assignable to string index type '{1}'." }, @@ -3802,7 +4017,7 @@ var ts; this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation: { code: 2683, category: ts.DiagnosticCategory.Error, key: "this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation_2683", message: "'this' implicitly has type 'any' because it does not have a type annotation." }, The_this_context_of_type_0_is_not_assignable_to_method_s_this_of_type_1: { code: 2684, category: ts.DiagnosticCategory.Error, key: "The_this_context_of_type_0_is_not_assignable_to_method_s_this_of_type_1_2684", message: "The 'this' context of type '{0}' is not assignable to method's 'this' of type '{1}'." }, The_this_types_of_each_signature_are_incompatible: { code: 2685, category: ts.DiagnosticCategory.Error, key: "The_this_types_of_each_signature_are_incompatible_2685", message: "The 'this' types of each signature are incompatible." }, - Identifier_0_must_be_imported_from_a_module: { code: 2686, category: ts.DiagnosticCategory.Error, key: "Identifier_0_must_be_imported_from_a_module_2686", message: "Identifier '{0}' must be imported from a module" }, + _0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead: { code: 2686, category: ts.DiagnosticCategory.Error, key: "_0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead_2686", message: "'{0}' refers to a UMD global, but the current file is a module. Consider adding an import instead." }, All_declarations_of_0_must_have_identical_modifiers: { code: 2687, category: ts.DiagnosticCategory.Error, key: "All_declarations_of_0_must_have_identical_modifiers_2687", message: "All declarations of '{0}' must have identical modifiers." }, Cannot_find_type_definition_file_for_0: { code: 2688, category: ts.DiagnosticCategory.Error, key: "Cannot_find_type_definition_file_for_0_2688", message: "Cannot find type definition file for '{0}'." }, Cannot_extend_an_interface_0_Did_you_mean_implements: { code: 2689, category: ts.DiagnosticCategory.Error, key: "Cannot_extend_an_interface_0_Did_you_mean_implements_2689", message: "Cannot extend an interface '{0}'. Did you mean 'implements'?" }, @@ -4035,6 +4250,8 @@ var ts; No_types_specified_in_package_json_but_allowJs_is_set_so_returning_main_value_of_0: { code: 6137, category: ts.DiagnosticCategory.Message, key: "No_types_specified_in_package_json_but_allowJs_is_set_so_returning_main_value_of_0_6137", message: "No types specified in 'package.json' but 'allowJs' is set, so returning 'main' value of '{0}'" }, Property_0_is_declared_but_never_used: { code: 6138, category: ts.DiagnosticCategory.Error, key: "Property_0_is_declared_but_never_used_6138", message: "Property '{0}' is declared but never used." }, Import_emit_helpers_from_tslib: { code: 6139, category: ts.DiagnosticCategory.Message, key: "Import_emit_helpers_from_tslib_6139", message: "Import emit helpers from 'tslib'." }, + Auto_discovery_for_typings_is_enabled_in_project_0_Running_extra_resolution_pass_for_module_1_using_cache_location_2: { code: 6140, category: ts.DiagnosticCategory.Error, key: "Auto_discovery_for_typings_is_enabled_in_project_0_Running_extra_resolution_pass_for_module_1_using__6140", message: "Auto discovery for typings is enabled in project '{0}'. Running extra resolution pass for module '{1}' using cache location '{2}'." }, + Parse_in_strict_mode_and_emit_use_strict_for_each_source_file: { code: 6141, category: ts.DiagnosticCategory.Message, key: "Parse_in_strict_mode_and_emit_use_strict_for_each_source_file_6141", message: "Parse in strict mode and emit \"use strict\" for each source file" }, Variable_0_implicitly_has_an_1_type: { code: 7005, category: ts.DiagnosticCategory.Error, key: "Variable_0_implicitly_has_an_1_type_7005", message: "Variable '{0}' implicitly has an '{1}' type." }, Parameter_0_implicitly_has_an_1_type: { code: 7006, category: ts.DiagnosticCategory.Error, key: "Parameter_0_implicitly_has_an_1_type_7006", message: "Parameter '{0}' implicitly has an '{1}' type." }, Member_0_implicitly_has_an_1_type: { code: 7008, category: ts.DiagnosticCategory.Error, key: "Member_0_implicitly_has_an_1_type_7008", message: "Member '{0}' implicitly has an '{1}' type." }, @@ -4059,6 +4276,7 @@ var ts; Binding_element_0_implicitly_has_an_1_type: { code: 7031, category: ts.DiagnosticCategory.Error, key: "Binding_element_0_implicitly_has_an_1_type_7031", message: "Binding element '{0}' implicitly has an '{1}' type." }, Property_0_implicitly_has_type_any_because_its_set_accessor_lacks_a_parameter_type_annotation: { code: 7032, category: ts.DiagnosticCategory.Error, key: "Property_0_implicitly_has_type_any_because_its_set_accessor_lacks_a_parameter_type_annotation_7032", message: "Property '{0}' implicitly has type 'any', because its set accessor lacks a parameter type annotation." }, Property_0_implicitly_has_type_any_because_its_get_accessor_lacks_a_return_type_annotation: { code: 7033, category: ts.DiagnosticCategory.Error, key: "Property_0_implicitly_has_type_any_because_its_get_accessor_lacks_a_return_type_annotation_7033", message: "Property '{0}' implicitly has type 'any', because its get accessor lacks a return type annotation." }, + Variable_0_implicitly_has_type_any_in_some_locations_where_its_type_cannot_be_determined: { code: 7034, category: ts.DiagnosticCategory.Error, key: "Variable_0_implicitly_has_type_any_in_some_locations_where_its_type_cannot_be_determined_7034", message: "Variable '{0}' implicitly has type 'any' in some locations where its type cannot be determined." }, You_cannot_rename_this_element: { code: 8000, category: ts.DiagnosticCategory.Error, key: "You_cannot_rename_this_element_8000", message: "You cannot rename this element." }, You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library: { code: 8001, category: ts.DiagnosticCategory.Error, key: "You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library_8001", message: "You cannot rename elements that are defined in the standard TypeScript library." }, import_can_only_be_used_in_a_ts_file: { code: 8002, category: ts.DiagnosticCategory.Error, key: "import_can_only_be_used_in_a_ts_file_8002", message: "'import ... =' can only be used in a .ts file." }, @@ -4088,7 +4306,14 @@ var ts; super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class: { code: 17009, category: ts.DiagnosticCategory.Error, key: "super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class_17009", message: "'super' must be called before accessing 'this' in the constructor of a derived class." }, Unknown_typing_option_0: { code: 17010, category: ts.DiagnosticCategory.Error, key: "Unknown_typing_option_0_17010", message: "Unknown typing option '{0}'." }, Circularity_detected_while_resolving_configuration_Colon_0: { code: 18000, category: ts.DiagnosticCategory.Error, key: "Circularity_detected_while_resolving_configuration_Colon_0_18000", message: "Circularity detected while resolving configuration: {0}" }, - The_path_in_an_extends_options_must_be_relative_or_rooted: { code: 18001, category: ts.DiagnosticCategory.Error, key: "The_path_in_an_extends_options_must_be_relative_or_rooted_18001", message: "The path in an 'extends' options must be relative or rooted." } + The_path_in_an_extends_options_must_be_relative_or_rooted: { code: 18001, category: ts.DiagnosticCategory.Error, key: "The_path_in_an_extends_options_must_be_relative_or_rooted_18001", message: "The path in an 'extends' options must be relative or rooted." }, + Add_missing_super_call: { code: 90001, category: ts.DiagnosticCategory.Message, key: "Add_missing_super_call_90001", message: "Add missing 'super()' call." }, + Make_super_call_the_first_statement_in_the_constructor: { code: 90002, category: ts.DiagnosticCategory.Message, key: "Make_super_call_the_first_statement_in_the_constructor_90002", message: "Make 'super()' call the first statement in the constructor." }, + Change_extends_to_implements: { code: 90003, category: ts.DiagnosticCategory.Message, key: "Change_extends_to_implements_90003", message: "Change 'extends' to 'implements'" }, + Remove_unused_identifiers: { code: 90004, category: ts.DiagnosticCategory.Message, key: "Remove_unused_identifiers_90004", message: "Remove unused identifiers" }, + Implement_interface_on_reference: { code: 90005, category: ts.DiagnosticCategory.Message, key: "Implement_interface_on_reference_90005", message: "Implement interface on reference" }, + Implement_interface_on_class: { code: 90006, category: ts.DiagnosticCategory.Message, key: "Implement_interface_on_class_90006", message: "Implement interface on class" }, + Implement_inherited_abstract_class: { code: 90007, category: ts.DiagnosticCategory.Message, key: "Implement_inherited_abstract_class_90007", message: "Implement inherited abstract class" } }; })(ts || (ts = {})); /// @@ -5145,7 +5370,7 @@ var ts; return token = 69 /* Identifier */; } function scanBinaryOrOctalDigits(base) { - ts.Debug.assert(base !== 2 || base !== 8, "Expected either base 2 or base 8"); + ts.Debug.assert(base === 2 || base === 8, "Expected either base 2 or base 8"); var value = 0; // For counting number of digits; Valid binaryIntegerLiteral must have at least one binary digit following B or b. // Similarly valid octalIntegerLiteral must have at least one octal digit following o or O. @@ -6364,10 +6589,10 @@ var ts; return !!(ts.getCombinedNodeFlags(node) & 1 /* Let */); } ts.isLet = isLet; - function isSuperCallExpression(n) { + function isSuperCall(n) { return n.kind === 174 /* CallExpression */ && n.expression.kind === 95 /* SuperKeyword */; } - ts.isSuperCallExpression = isSuperCallExpression; + ts.isSuperCall = isSuperCall; function isPrologueDirective(node) { return node.kind === 202 /* ExpressionStatement */ && node.expression.kind === 9 /* StringLiteral */; } @@ -6636,6 +6861,12 @@ var ts; return node && node.kind === 147 /* MethodDeclaration */ && node.parent.kind === 171 /* ObjectLiteralExpression */; } ts.isObjectLiteralMethod = isObjectLiteralMethod; + function isObjectLiteralOrClassExpressionMethod(node) { + return node.kind === 147 /* MethodDeclaration */ && + (node.parent.kind === 171 /* ObjectLiteralExpression */ || + node.parent.kind === 192 /* ClassExpression */); + } + ts.isObjectLiteralOrClassExpressionMethod = isObjectLiteralOrClassExpressionMethod; function isIdentifierTypePredicate(predicate) { return predicate && predicate.kind === 1 /* Identifier */; } @@ -6723,11 +6954,11 @@ var ts; } ts.getThisContainer = getThisContainer; /** - * Given an super call\property node returns a closest node where either - * - super call\property is legal in the node and not legal in the parent node the node. + * Given an super call/property node, returns the closest node where + * - a super call/property access is legal in the node and not legal in the parent node the node. * i.e. super call is legal in constructor but not legal in the class body. - * - node is arrow function (so caller might need to call getSuperContainer in case it needs to climb higher) - * - super call\property is definitely illegal in the node (but might be legal in some subnode) + * - the container is an arrow function (so caller might need to call getSuperContainer again in case it needs to climb higher) + * - a super call/property is definitely illegal in the container (but might be legal in some subnode) * i.e. super property access is illegal in function declaration but can be legal in the statement list */ function getSuperContainer(node, stopOnFunctions) { @@ -6988,12 +7219,6 @@ var ts; return false; } ts.isPartOfExpression = isPartOfExpression; - function isExternalModuleNameRelative(moduleName) { - // TypeScript 1.0 spec (April 2014): 11.2.1 - // An external module name is "relative" if the first term is "." or "..". - return /^\.\.?($|[\\/])/.test(moduleName); - } - ts.isExternalModuleNameRelative = isExternalModuleNameRelative; function isInstantiatedModule(node, preserveConstEnums) { var moduleState = ts.getModuleInstanceState(node); return moduleState === 1 /* Instantiated */ || @@ -7655,16 +7880,10 @@ var ts; } ts.nodeStartsNewLexicalEnvironment = nodeStartsNewLexicalEnvironment; function nodeIsSynthesized(node) { - return positionIsSynthesized(node.pos) - || positionIsSynthesized(node.end); + return ts.positionIsSynthesized(node.pos) + || ts.positionIsSynthesized(node.end); } ts.nodeIsSynthesized = nodeIsSynthesized; - function positionIsSynthesized(pos) { - // This is a fast way of testing the following conditions: - // pos === undefined || pos === null || isNaN(pos) || pos < 0; - return !(pos >= 0); - } - ts.positionIsSynthesized = positionIsSynthesized; function getOriginalNode(node) { if (node) { while (node.original !== undefined) { @@ -8125,24 +8344,12 @@ var ts; function getDeclarationEmitOutputFilePath(sourceFile, host) { var options = host.getCompilerOptions(); var outputDir = options.declarationDir || options.outDir; // Prefer declaration folder if specified - if (options.declaration) { - var path = outputDir - ? getSourceFilePathInNewDir(sourceFile, host, outputDir) - : sourceFile.fileName; - return ts.removeFileExtension(path) + ".d.ts"; - } + var path = outputDir + ? getSourceFilePathInNewDir(sourceFile, host, outputDir) + : sourceFile.fileName; + return ts.removeFileExtension(path) + ".d.ts"; } ts.getDeclarationEmitOutputFilePath = getDeclarationEmitOutputFilePath; - function getEmitScriptTarget(compilerOptions) { - return compilerOptions.target || 0 /* ES3 */; - } - ts.getEmitScriptTarget = getEmitScriptTarget; - function getEmitModuleKind(compilerOptions) { - return typeof compilerOptions.module === "number" ? - compilerOptions.module : - getEmitScriptTarget(compilerOptions) === 2 /* ES6 */ ? ts.ModuleKind.ES6 : ts.ModuleKind.CommonJS; - } - ts.getEmitModuleKind = getEmitModuleKind; /** * Gets the source files that are expected to have an emit output. * @@ -8155,7 +8362,7 @@ var ts; function getSourceFilesToEmit(host, targetSourceFile) { var options = host.getCompilerOptions(); if (options.outFile || options.out) { - var moduleKind = getEmitModuleKind(options); + var moduleKind = ts.getEmitModuleKind(options); var moduleEmitEnabled = moduleKind === ts.ModuleKind.AMD || moduleKind === ts.ModuleKind.System; var sourceFiles = host.getSourceFiles(); // Can emit only sources that are not declaration file and are either non module code or module with --module or --target es6 specified @@ -8184,7 +8391,7 @@ var ts; * @param sourceFiles The transformed source files to emit. * @param action The action to execute. */ - function forEachTransformedEmitFile(host, sourceFiles, action) { + function forEachTransformedEmitFile(host, sourceFiles, action, emitOnlyDtsFiles) { var options = host.getCompilerOptions(); // Emit on each source file if (options.outFile || options.out) { @@ -8217,7 +8424,7 @@ var ts; } var jsFilePath = getOwnEmitOutputFilePath(sourceFile, host, extension); var sourceMapFilePath = getSourceMapFilePath(jsFilePath, options); - var declarationFilePath = !isSourceFileJavaScript(sourceFile) ? getDeclarationEmitOutputFilePath(sourceFile, host) : undefined; + var declarationFilePath = !isSourceFileJavaScript(sourceFile) && (options.declaration || emitOnlyDtsFiles) ? getDeclarationEmitOutputFilePath(sourceFile, host) : undefined; action(jsFilePath, sourceMapFilePath, declarationFilePath, [sourceFile], /*isBundledEmit*/ false); } function onBundledEmit(host, sourceFiles) { @@ -8242,7 +8449,7 @@ var ts; * @param action The action to execute. * @param targetSourceFile An optional target source file to emit. */ - function forEachExpectedEmitFile(host, action, targetSourceFile) { + function forEachExpectedEmitFile(host, action, targetSourceFile, emitOnlyDtsFiles) { var options = host.getCompilerOptions(); // Emit on each source file if (options.outFile || options.out) { @@ -8275,12 +8482,13 @@ var ts; } } var jsFilePath = getOwnEmitOutputFilePath(sourceFile, host, extension); + var declarationFilePath = !isSourceFileJavaScript(sourceFile) && (emitOnlyDtsFiles || options.declaration) ? getDeclarationEmitOutputFilePath(sourceFile, host) : undefined; var emitFileNames = { jsFilePath: jsFilePath, sourceMapFilePath: getSourceMapFilePath(jsFilePath, options), - declarationFilePath: !isSourceFileJavaScript(sourceFile) ? getDeclarationEmitOutputFilePath(sourceFile, host) : undefined + declarationFilePath: declarationFilePath }; - action(emitFileNames, [sourceFile], /*isBundledEmit*/ false); + action(emitFileNames, [sourceFile], /*isBundledEmit*/ false, emitOnlyDtsFiles); } function onBundledEmit(host) { // Can emit only sources that are not declaration file and are either non module code or module with @@ -8288,7 +8496,7 @@ var ts; var bundledSources = ts.filter(host.getSourceFiles(), function (sourceFile) { return !isDeclarationFile(sourceFile) && !host.isSourceFileFromExternalLibrary(sourceFile) && (!ts.isExternalModule(sourceFile) || - !!getEmitModuleKind(options)); }); + !!ts.getEmitModuleKind(options)); }); if (bundledSources.length) { var jsFilePath = options.outFile || options.out; var emitFileNames = { @@ -8296,7 +8504,7 @@ var ts; sourceMapFilePath: getSourceMapFilePath(jsFilePath, options), declarationFilePath: options.declaration ? ts.removeFileExtension(jsFilePath) + ".d.ts" : undefined }; - action(emitFileNames, bundledSources, /*isBundledEmit*/ true); + action(emitFileNames, bundledSources, /*isBundledEmit*/ true, emitOnlyDtsFiles); } } } @@ -8331,15 +8539,35 @@ var ts; }); } ts.getFirstConstructorWithBody = getFirstConstructorWithBody; + /** Get the type annotaion for the value parameter. */ function getSetAccessorTypeAnnotationNode(accessor) { if (accessor && accessor.parameters.length > 0) { - var hasThis = accessor.parameters.length === 2 && - accessor.parameters[0].name.kind === 69 /* Identifier */ && - accessor.parameters[0].name.originalKeywordKind === 97 /* ThisKeyword */; + var hasThis = accessor.parameters.length === 2 && parameterIsThisKeyword(accessor.parameters[0]); return accessor.parameters[hasThis ? 1 : 0].type; } } ts.getSetAccessorTypeAnnotationNode = getSetAccessorTypeAnnotationNode; + function getThisParameter(signature) { + if (signature.parameters.length) { + var thisParameter = signature.parameters[0]; + if (parameterIsThisKeyword(thisParameter)) { + return thisParameter; + } + } + } + ts.getThisParameter = getThisParameter; + function parameterIsThisKeyword(parameter) { + return isThisIdentifier(parameter.name); + } + ts.parameterIsThisKeyword = parameterIsThisKeyword; + function isThisIdentifier(node) { + return node && node.kind === 69 /* Identifier */ && identifierIsThisKeyword(node); + } + ts.isThisIdentifier = isThisIdentifier; + function identifierIsThisKeyword(id) { + return id.originalKeywordKind === 97 /* ThisKeyword */; + } + ts.identifierIsThisKeyword = identifierIsThisKeyword; function getAllAccessorDeclarations(declarations, accessor) { var firstAccessor; var secondAccessor; @@ -8700,14 +8928,6 @@ var ts; return symbol && symbol.valueDeclaration && hasModifier(symbol.valueDeclaration, 512 /* Default */) ? symbol.valueDeclaration.localSymbol : undefined; } ts.getLocalSymbolForExportDefault = getLocalSymbolForExportDefault; - function hasJavaScriptFileExtension(fileName) { - return ts.forEach(ts.supportedJavascriptExtensions, function (extension) { return ts.fileExtensionIs(fileName, extension); }); - } - ts.hasJavaScriptFileExtension = hasJavaScriptFileExtension; - function hasTypeScriptFileExtension(fileName) { - return ts.forEach(ts.supportedTypeScriptExtensions, function (extension) { return ts.fileExtensionIs(fileName, extension); }); - } - ts.hasTypeScriptFileExtension = hasTypeScriptFileExtension; /** Return ".ts", ".d.ts", or ".tsx", if that is the extension. */ function tryExtractTypeScriptExtension(fileName) { return ts.find(ts.supportedTypescriptExtensionsForExtractExtension, function (extension) { return ts.fileExtensionIs(fileName, extension); }); @@ -8820,12 +9040,6 @@ var ts; return result; } ts.convertToBase64 = convertToBase64; - function convertToRelativePath(absoluteOrRelativePath, basePath, getCanonicalFileName) { - return !ts.isRootedDiskPath(absoluteOrRelativePath) - ? absoluteOrRelativePath - : ts.getRelativePathToDirectoryOrUrl(basePath, absoluteOrRelativePath, basePath, getCanonicalFileName, /* isAbsolutePathAnUrl */ false); - } - ts.convertToRelativePath = convertToRelativePath; var carriageReturnLineFeed = "\r\n"; var lineFeed = "\n"; function getNewLineCharacter(options) { @@ -8938,7 +9152,7 @@ var ts; * @param value The delta. */ function movePos(pos, value) { - return positionIsSynthesized(pos) ? -1 : pos + value; + return ts.positionIsSynthesized(pos) ? -1 : pos + value; } ts.movePos = movePos; /** @@ -9054,7 +9268,7 @@ var ts; } ts.positionsAreOnSameLine = positionsAreOnSameLine; function getStartPositionOfRange(range, sourceFile) { - return positionIsSynthesized(range.pos) ? -1 : ts.skipTrivia(sourceFile.text, range.pos); + return ts.positionIsSynthesized(range.pos) ? -1 : ts.skipTrivia(sourceFile.text, range.pos); } ts.getStartPositionOfRange = getStartPositionOfRange; function collectExternalModuleInfo(sourceFile, resolver) { @@ -9179,15 +9393,16 @@ var ts; return 11 /* FirstTemplateToken */ <= kind && kind <= 14 /* LastTemplateToken */; } ts.isTemplateLiteralKind = isTemplateLiteralKind; - function isTemplateLiteralFragmentKind(kind) { - return kind === 12 /* TemplateHead */ - || kind === 13 /* TemplateMiddle */ - || kind === 14 /* TemplateTail */; + function isTemplateHead(node) { + return node.kind === 12 /* TemplateHead */; } - function isTemplateLiteralFragment(node) { - return isTemplateLiteralFragmentKind(node.kind); + ts.isTemplateHead = isTemplateHead; + function isTemplateMiddleOrTemplateTail(node) { + var kind = node.kind; + return kind === 13 /* TemplateMiddle */ + || kind === 14 /* TemplateTail */; } - ts.isTemplateLiteralFragment = isTemplateLiteralFragment; + ts.isTemplateMiddleOrTemplateTail = isTemplateMiddleOrTemplateTail; // Identifiers function isIdentifier(node) { return node.kind === 69 /* Identifier */; @@ -9340,12 +9555,12 @@ var ts; return node.kind === 174 /* CallExpression */; } ts.isCallExpression = isCallExpression; - function isTemplate(node) { + function isTemplateLiteral(node) { var kind = node.kind; return kind === 189 /* TemplateExpression */ || kind === 11 /* NoSubstitutionTemplateLiteral */; } - ts.isTemplate = isTemplate; + ts.isTemplateLiteral = isTemplateLiteral; function isSpreadElementExpression(node) { return node.kind === 191 /* SpreadElementExpression */; } @@ -9688,7 +9903,6 @@ var ts; } ts.isWatchSet = isWatchSet; })(ts || (ts = {})); -var ts; (function (ts) { function getDefaultLibFileName(options) { return options.target === 2 /* ES6 */ ? "lib.es6.d.ts" : "lib.d.ts"; @@ -10029,7 +10243,7 @@ var ts; // the original node. We also need to exclude specific properties and only include own- // properties (to skip members already defined on the shared prototype). var clone = createNode(node.kind, /*location*/ undefined, node.flags); - clone.original = node; + setOriginalNode(clone, node); for (var key in node) { if (clone.hasOwnProperty(key) || !node.hasOwnProperty(key)) { continue; @@ -10064,7 +10278,7 @@ var ts; node.text = value; return node; } - else { + else if (value) { var node = createNode(9 /* StringLiteral */, location, /*flags*/ undefined); node.textSourceNode = value; node.text = value.text; @@ -10364,7 +10578,7 @@ var ts; function createPropertyAccess(expression, name, location, flags) { var node = createNode(172 /* PropertyAccessExpression */, location, flags); node.expression = parenthesizeForAccess(expression); - node.emitFlags = 1048576 /* NoIndentation */; + (node.emitNode || (node.emitNode = {})).flags |= 1048576 /* NoIndentation */; node.name = typeof name === "string" ? createIdentifier(name) : name; return node; } @@ -10373,7 +10587,7 @@ var ts; if (node.expression !== expression || node.name !== name) { var propertyAccess = createPropertyAccess(expression, name, /*location*/ node, node.flags); // Because we are updating existed propertyAccess we want to inherit its emitFlags instead of using default from createPropertyAccess - propertyAccess.emitFlags = node.emitFlags; + (propertyAccess.emitNode || (propertyAccess.emitNode = {})).flags = getEmitFlags(node); return updateNode(propertyAccess, node); } return node; @@ -10477,7 +10691,7 @@ var ts; node.typeParameters = typeParameters ? createNodeArray(typeParameters) : undefined; node.parameters = createNodeArray(parameters); node.type = type; - node.equalsGreaterThanToken = equalsGreaterThanToken || createNode(34 /* EqualsGreaterThanToken */); + node.equalsGreaterThanToken = equalsGreaterThanToken || createToken(34 /* EqualsGreaterThanToken */); node.body = parenthesizeConciseBody(body); return node; } @@ -10570,7 +10784,7 @@ var ts; } ts.updatePostfix = updatePostfix; function createBinary(left, operator, right, location) { - var operatorToken = typeof operator === "number" ? createSynthesizedNode(operator) : operator; + var operatorToken = typeof operator === "number" ? createToken(operator) : operator; var operatorKind = operatorToken.kind; var node = createNode(187 /* BinaryExpression */, location); node.left = parenthesizeBinaryOperand(operatorKind, left, /*isLeftSideOfBinary*/ true, /*leftOperand*/ undefined); @@ -11492,7 +11706,7 @@ var ts; } else { var expression = ts.isIdentifier(memberName) ? createPropertyAccess(target, memberName, location) : createElementAccess(target, memberName, location); - expression.emitFlags |= 2048 /* NoNestedSourceMaps */; + (expression.emitNode || (expression.emitNode = {})).flags |= 2048 /* NoNestedSourceMaps */; return expression; } } @@ -11500,7 +11714,7 @@ var ts; function createRestParameter(name) { return createParameterDeclaration( /*decorators*/ undefined, - /*modifiers*/ undefined, createSynthesizedNode(22 /* DotDotDotToken */), name, + /*modifiers*/ undefined, createToken(22 /* DotDotDotToken */), name, /*questionToken*/ undefined, /*type*/ undefined, /*initializer*/ undefined); @@ -11630,13 +11844,13 @@ var ts; } ts.createDecorateHelper = createDecorateHelper; function createAwaiterHelper(externalHelpersModuleName, hasLexicalArguments, promiseConstructor, body) { - var generatorFunc = createFunctionExpression(createNode(37 /* AsteriskToken */), + var generatorFunc = createFunctionExpression(createToken(37 /* AsteriskToken */), /*name*/ undefined, /*typeParameters*/ undefined, /*parameters*/ [], /*type*/ undefined, body); // Mark this node as originally an async function - generatorFunc.emitFlags |= 2097152 /* AsyncFunctionBody */; + (generatorFunc.emitNode || (generatorFunc.emitNode = {})).flags |= 2097152 /* AsyncFunctionBody */; return createCall(createHelperName(externalHelpersModuleName, "__awaiter"), /*typeArguments*/ undefined, [ createThis(), @@ -11953,7 +12167,7 @@ var ts; target.push(startOnNewLine(createStatement(createLiteral("use strict")))); foundUseStrict = true; } - if (statement.emitFlags & 8388608 /* CustomPrologue */) { + if (getEmitFlags(statement) & 8388608 /* CustomPrologue */) { target.push(visitor ? ts.visitNode(statement, visitor, ts.isStatement) : statement); } else { @@ -11965,6 +12179,34 @@ var ts; return statementOffset; } ts.addPrologueDirectives = addPrologueDirectives; + /** + * Ensures "use strict" directive is added + * + * @param node source file + */ + function ensureUseStrict(node) { + var foundUseStrict = false; + for (var _i = 0, _a = node.statements; _i < _a.length; _i++) { + var statement = _a[_i]; + if (ts.isPrologueDirective(statement)) { + if (isUseStrictPrologue(statement)) { + foundUseStrict = true; + break; + } + } + else { + break; + } + } + if (!foundUseStrict) { + var statements = []; + statements.push(startOnNewLine(createStatement(createLiteral("use strict")))); + // add "use strict" as the first statement + return updateSourceFileNode(node, statements.concat(node.statements)); + } + return node; + } + ts.ensureUseStrict = ensureUseStrict; /** * Wraps the operand to a BinaryExpression in parentheses if they are needed to preserve the intended * order of operations. @@ -12323,17 +12565,180 @@ var ts; function setOriginalNode(node, original) { node.original = original; if (original) { - var emitFlags = original.emitFlags, commentRange = original.commentRange, sourceMapRange = original.sourceMapRange; - if (emitFlags) - node.emitFlags = emitFlags; - if (commentRange) - node.commentRange = commentRange; - if (sourceMapRange) - node.sourceMapRange = sourceMapRange; + var emitNode = original.emitNode; + if (emitNode) + node.emitNode = mergeEmitNode(emitNode, node.emitNode); } return node; } ts.setOriginalNode = setOriginalNode; + function mergeEmitNode(sourceEmitNode, destEmitNode) { + var flags = sourceEmitNode.flags, commentRange = sourceEmitNode.commentRange, sourceMapRange = sourceEmitNode.sourceMapRange, tokenSourceMapRanges = sourceEmitNode.tokenSourceMapRanges; + if (!destEmitNode && (flags || commentRange || sourceMapRange || tokenSourceMapRanges)) + destEmitNode = {}; + if (flags) + destEmitNode.flags = flags; + if (commentRange) + destEmitNode.commentRange = commentRange; + if (sourceMapRange) + destEmitNode.sourceMapRange = sourceMapRange; + if (tokenSourceMapRanges) + destEmitNode.tokenSourceMapRanges = mergeTokenSourceMapRanges(tokenSourceMapRanges, destEmitNode.tokenSourceMapRanges); + return destEmitNode; + } + function mergeTokenSourceMapRanges(sourceRanges, destRanges) { + if (!destRanges) + destRanges = ts.createMap(); + ts.copyProperties(sourceRanges, destRanges); + return destRanges; + } + /** + * Clears any EmitNode entries from parse-tree nodes. + * @param sourceFile A source file. + */ + function disposeEmitNodes(sourceFile) { + // During transformation we may need to annotate a parse tree node with transient + // transformation properties. As parse tree nodes live longer than transformation + // nodes, we need to make sure we reclaim any memory allocated for custom ranges + // from these nodes to ensure we do not hold onto entire subtrees just for position + // information. We also need to reset these nodes to a pre-transformation state + // for incremental parsing scenarios so that we do not impact later emit. + sourceFile = ts.getSourceFileOfNode(ts.getParseTreeNode(sourceFile)); + var emitNode = sourceFile && sourceFile.emitNode; + var annotatedNodes = emitNode && emitNode.annotatedNodes; + if (annotatedNodes) { + for (var _i = 0, annotatedNodes_1 = annotatedNodes; _i < annotatedNodes_1.length; _i++) { + var node = annotatedNodes_1[_i]; + node.emitNode = undefined; + } + } + } + ts.disposeEmitNodes = disposeEmitNodes; + /** + * Associates a node with the current transformation, initializing + * various transient transformation properties. + * + * @param node The node. + */ + function getOrCreateEmitNode(node) { + if (!node.emitNode) { + if (ts.isParseTreeNode(node)) { + // To avoid holding onto transformation artifacts, we keep track of any + // parse tree node we are annotating. This allows us to clean them up after + // all transformations have completed. + if (node.kind === 256 /* SourceFile */) { + return node.emitNode = { annotatedNodes: [node] }; + } + var sourceFile = ts.getSourceFileOfNode(node); + getOrCreateEmitNode(sourceFile).annotatedNodes.push(node); + } + node.emitNode = {}; + } + return node.emitNode; + } + /** + * Gets flags that control emit behavior of a node. + * + * @param node The node. + */ + function getEmitFlags(node) { + var emitNode = node.emitNode; + return emitNode && emitNode.flags; + } + ts.getEmitFlags = getEmitFlags; + /** + * Sets flags that control emit behavior of a node. + * + * @param node The node. + * @param emitFlags The NodeEmitFlags for the node. + */ + function setEmitFlags(node, emitFlags) { + getOrCreateEmitNode(node).flags = emitFlags; + return node; + } + ts.setEmitFlags = setEmitFlags; + /** + * Sets a custom text range to use when emitting source maps. + * + * @param node The node. + * @param range The text range. + */ + function setSourceMapRange(node, range) { + getOrCreateEmitNode(node).sourceMapRange = range; + return node; + } + ts.setSourceMapRange = setSourceMapRange; + /** + * Sets the TextRange to use for source maps for a token of a node. + * + * @param node The node. + * @param token The token. + * @param range The text range. + */ + function setTokenSourceMapRange(node, token, range) { + var emitNode = getOrCreateEmitNode(node); + var tokenSourceMapRanges = emitNode.tokenSourceMapRanges || (emitNode.tokenSourceMapRanges = ts.createMap()); + tokenSourceMapRanges[token] = range; + return node; + } + ts.setTokenSourceMapRange = setTokenSourceMapRange; + /** + * Sets a custom text range to use when emitting comments. + */ + function setCommentRange(node, range) { + getOrCreateEmitNode(node).commentRange = range; + return node; + } + ts.setCommentRange = setCommentRange; + /** + * Gets a custom text range to use when emitting comments. + * + * @param node The node. + */ + function getCommentRange(node) { + var emitNode = node.emitNode; + return (emitNode && emitNode.commentRange) || node; + } + ts.getCommentRange = getCommentRange; + /** + * Gets a custom text range to use when emitting source maps. + * + * @param node The node. + */ + function getSourceMapRange(node) { + var emitNode = node.emitNode; + return (emitNode && emitNode.sourceMapRange) || node; + } + ts.getSourceMapRange = getSourceMapRange; + /** + * Gets the TextRange to use for source maps for a token of a node. + * + * @param node The node. + * @param token The token. + */ + function getTokenSourceMapRange(node, token) { + var emitNode = node.emitNode; + var tokenSourceMapRanges = emitNode && emitNode.tokenSourceMapRanges; + return tokenSourceMapRanges && tokenSourceMapRanges[token]; + } + ts.getTokenSourceMapRange = getTokenSourceMapRange; + /** + * Gets the constant value to emit for an expression. + */ + function getConstantValue(node) { + var emitNode = node.emitNode; + return emitNode && emitNode.constantValue; + } + ts.getConstantValue = getConstantValue; + /** + * Sets the constant value to emit for an expression. + */ + function setConstantValue(node, value) { + var emitNode = getOrCreateEmitNode(node); + emitNode.constantValue = value; + return node; + } + ts.setConstantValue = setConstantValue; function setTextRange(node, location) { if (location) { node.pos = location.pos; @@ -12376,13 +12781,13 @@ var ts; } ts.getLocalNameForExternalImport = getLocalNameForExternalImport; /** - * Get the name of a target module from an import/export declaration as should be written in the emitted output. - * The emitted output name can be different from the input if: - * 1. The module has a /// - * 2. --out or --outFile is used, making the name relative to the rootDir - * 3- The containing SourceFile has an entry in renamedDependencies for the import as requested by some module loaders (e.g. System). - * Otherwise, a new StringLiteral node representing the module name will be returned. - */ + * Get the name of a target module from an import/export declaration as should be written in the emitted output. + * The emitted output name can be different from the input if: + * 1. The module has a /// + * 2. --out or --outFile is used, making the name relative to the rootDir + * 3- The containing SourceFile has an entry in renamedDependencies for the import as requested by some module loaders (e.g. System). + * Otherwise, a new StringLiteral node representing the module name will be returned. + */ function getExternalModuleNameLiteral(importNode, sourceFile, host, resolver, compilerOptions) { var moduleName = ts.getExternalModuleName(importNode); if (moduleName.kind === 9 /* StringLiteral */) { @@ -13683,8 +14088,8 @@ var ts; // Tokens other than ')' and ']' (the latter for index signatures) are here for better error recovery return token() === 18 /* CloseParenToken */ || token() === 20 /* CloseBracketToken */ /*|| token === SyntaxKind.OpenBraceToken*/; case 18 /* TypeArguments */: - // Tokens other than '>' are here for better error recovery - return token() === 27 /* GreaterThanToken */ || token() === 17 /* OpenParenToken */; + // All other tokens should cause the type-argument to terminate except comma token + return token() !== 24 /* CommaToken */; case 20 /* HeritageClauses */: return token() === 15 /* OpenBraceToken */ || token() === 16 /* CloseBraceToken */; case 13 /* JsxAttributes */: @@ -14135,7 +14540,7 @@ var ts; } function parseTemplateExpression() { var template = createNode(189 /* TemplateExpression */); - template.head = parseTemplateLiteralFragment(); + template.head = parseTemplateHead(); ts.Debug.assert(template.head.kind === 12 /* TemplateHead */, "Template head has wrong token kind"); var templateSpans = createNodeArray(); do { @@ -14151,7 +14556,7 @@ var ts; var literal; if (token() === 16 /* CloseBraceToken */) { reScanTemplateToken(); - literal = parseTemplateLiteralFragment(); + literal = parseTemplateMiddleOrTemplateTail(); } else { literal = parseExpectedToken(14 /* TemplateTail */, /*reportAtCurrentPosition*/ false, ts.Diagnostics._0_expected, ts.tokenToString(16 /* CloseBraceToken */)); @@ -14162,8 +14567,15 @@ var ts; function parseLiteralNode(internName) { return parseLiteralLikeNode(token(), internName); } - function parseTemplateLiteralFragment() { - return parseLiteralLikeNode(token(), /*internName*/ false); + function parseTemplateHead() { + var fragment = parseLiteralLikeNode(token(), /*internName*/ false); + ts.Debug.assert(fragment.kind === 12 /* TemplateHead */, "Template head has wrong token kind"); + return fragment; + } + function parseTemplateMiddleOrTemplateTail() { + var fragment = parseLiteralLikeNode(token(), /*internName*/ false); + ts.Debug.assert(fragment.kind === 13 /* TemplateMiddle */ || fragment.kind === 14 /* TemplateTail */, "Template fragment has wrong token kind"); + return fragment; } function parseLiteralLikeNode(kind, internName) { var node = createNode(kind); @@ -17144,7 +17556,7 @@ var ts; parseExpected(56 /* EqualsToken */); node.type = parseType(); parseSemicolon(); - return finishNode(node); + return addJSDocComment(finishNode(node)); } // In an ambient declaration, the grammar only allows integer literals as initializers. // In a non-ambient declaration, the grammar allows uninitialized members only in a @@ -17702,6 +18114,7 @@ var ts; var parameter = createNode(142 /* Parameter */); parameter.type = parseJSDocType(); if (parseOptional(56 /* EqualsToken */)) { + // TODO(rbuckton): Can this be changed to SyntaxKind.QuestionToken? parameter.questionToken = createNode(56 /* EqualsToken */); } return finishNode(parameter); @@ -18895,6 +19308,7 @@ var ts; ContainerFlags[ContainerFlags["IsFunctionExpression"] = 16] = "IsFunctionExpression"; ContainerFlags[ContainerFlags["HasLocals"] = 32] = "HasLocals"; ContainerFlags[ContainerFlags["IsInterface"] = 64] = "IsInterface"; + ContainerFlags[ContainerFlags["IsObjectLiteralOrClassExpressionMethod"] = 128] = "IsObjectLiteralOrClassExpressionMethod"; })(ContainerFlags || (ContainerFlags = {})); var binder = createBinder(); function bindSourceFile(file, options) { @@ -18927,7 +19341,8 @@ var ts; var emitFlags; // If this file is an external module, then it is automatically in strict-mode according to // ES6. If it is not an external module, then we'll determine if it is in strict mode or - // not depending on if we see "use strict" in certain places (or if we hit a class/namespace). + // not depending on if we see "use strict" in certain places or if we hit a class/namespace + // or if compiler options contain alwaysStrict. var inStrictMode; var symbolCount = 0; var Symbol; @@ -18941,7 +19356,7 @@ var ts; file = f; options = opts; languageVersion = ts.getEmitScriptTarget(options); - inStrictMode = !!file.externalModuleIndicator; + inStrictMode = bindInStrictMode(file, opts); classifiableNames = ts.createMap(); symbolCount = 0; skipTransformFlagAggregation = ts.isDeclarationFile(file); @@ -18971,6 +19386,15 @@ var ts; subtreeTransformFlags = 0 /* None */; } return bindSourceFile; + function bindInStrictMode(file, opts) { + if (opts.alwaysStrict && !ts.isDeclarationFile(file)) { + // bind in strict mode source files with alwaysStrict option + return true; + } + else { + return !!file.externalModuleIndicator; + } + } function createSymbol(flags, name) { symbolCount++; return new Symbol(flags, name); @@ -19134,11 +19558,24 @@ var ts; var message_1 = symbol.flags & 2 /* BlockScopedVariable */ ? ts.Diagnostics.Cannot_redeclare_block_scoped_variable_0 : ts.Diagnostics.Duplicate_identifier_0; - ts.forEach(symbol.declarations, function (declaration) { - if (ts.hasModifier(declaration, 512 /* Default */)) { + if (symbol.declarations && symbol.declarations.length) { + // If the current node is a default export of some sort, then check if + // there are any other default exports that we need to error on. + // We'll know whether we have other default exports depending on if `symbol` already has a declaration list set. + if (isDefaultExport) { message_1 = ts.Diagnostics.A_module_cannot_have_multiple_default_exports; } - }); + else { + // This is to properly report an error in the case "export default { }" is after export default of class declaration or function declaration. + // Error on multiple export default in the following case: + // 1. multiple export default of class declaration or function declaration by checking NodeFlags.Default + // 2. multiple export default of export assignment. This one doesn't have NodeFlags.Default on (as export default doesn't considered as modifiers) + if (symbol.declarations && symbol.declarations.length && + (isDefaultExport || (node.kind === 235 /* ExportAssignment */ && !node.isExportEquals))) { + message_1 = ts.Diagnostics.A_module_cannot_have_multiple_default_exports; + } + } + } ts.forEach(symbol.declarations, function (declaration) { file.bindDiagnostics.push(ts.createDiagnosticForNode(declaration.name || declaration, message_1, getDisplayName(declaration))); }); @@ -19243,7 +19680,7 @@ var ts; } else { currentFlow = { flags: 2 /* Start */ }; - if (containerFlags & 16 /* IsFunctionExpression */) { + if (containerFlags & (16 /* IsFunctionExpression */ | 128 /* IsObjectLiteralOrClassExpressionMethod */)) { currentFlow.container = node; } currentReturnTarget = undefined; @@ -19705,7 +20142,11 @@ var ts; currentFlow = preTryFlow; bind(node.finallyBlock); } - currentFlow = finishFlowLabel(postFinallyLabel); + // if try statement has finally block and flow after finally block is unreachable - keep it + // otherwise use whatever flow was accumulated at postFinallyLabel + if (!node.finallyBlock || !(currentFlow.flags & 1 /* Unreachable */)) { + currentFlow = finishFlowLabel(postFinallyLabel); + } } function bindSwitchStatement(node) { var postSwitchLabel = createBranchLabel(); @@ -19840,7 +20281,7 @@ var ts; } else { ts.forEachChild(node, bind); - if (node.operator === 57 /* PlusEqualsToken */ || node.operator === 42 /* MinusMinusToken */) { + if (node.operator === 41 /* PlusPlusToken */ || node.operator === 42 /* MinusMinusToken */) { bindAssignmentTargetFlow(node.operand); } } @@ -19942,9 +20383,12 @@ var ts; return 1 /* IsContainer */ | 32 /* HasLocals */; case 256 /* SourceFile */: return 1 /* IsContainer */ | 4 /* IsControlFlowContainer */ | 32 /* HasLocals */; + case 147 /* MethodDeclaration */: + if (ts.isObjectLiteralOrClassExpressionMethod(node)) { + return 1 /* IsContainer */ | 4 /* IsControlFlowContainer */ | 32 /* HasLocals */ | 8 /* IsFunctionLike */ | 128 /* IsObjectLiteralOrClassExpressionMethod */; + } case 148 /* Constructor */: case 220 /* FunctionDeclaration */: - case 147 /* MethodDeclaration */: case 146 /* MethodSignature */: case 149 /* GetAccessor */: case 150 /* SetAccessor */: @@ -20588,10 +21032,13 @@ var ts; bindAnonymousDeclaration(node, 8388608 /* Alias */, getDeclarationName(node)); } else { + // An export default clause with an expression exports a value + // We want to exclude both class and function here, this is necessary to issue an error when there are both + // default export-assignment and default export function and class declaration. var flags = node.kind === 235 /* ExportAssignment */ && ts.exportAssignmentIsAlias(node) ? 8388608 /* Alias */ : 4 /* Property */; - declareSymbol(container.symbol.exports, container.symbol, node, flags, 0 /* PropertyExcludes */ | 8388608 /* AliasExcludes */); + declareSymbol(container.symbol.exports, container.symbol, node, flags, 4 /* Property */ | 8388608 /* AliasExcludes */ | 32 /* Class */ | 16 /* Function */); } } function bindNamespaceExportDeclaration(node) { @@ -20829,6 +21276,9 @@ var ts; emitFlags |= 2048 /* HasDecorators */; } } + if (currentFlow && ts.isObjectLiteralOrClassExpressionMethod(node)) { + node.flowNode = currentFlow; + } return ts.hasDynamicName(node) ? bindAnonymousDeclaration(node, symbolFlags, "__computed") : declareSymbolAndAddToSymbolTable(node, symbolFlags, symbolExcludes); @@ -20994,8 +21444,7 @@ var ts; transformFlags |= 3 /* AssertTypeScript */; } // If the parameter's name is 'this', then it is TypeScript syntax. - if (subtreeFlags & 2048 /* ContainsDecorators */ - || (name && ts.isIdentifier(name) && name.originalKeywordKind === 97 /* ThisKeyword */)) { + if (subtreeFlags & 2048 /* ContainsDecorators */ || ts.isThisIdentifier(name)) { transformFlags |= 3 /* AssertTypeScript */; } // If a parameter has an accessibility modifier, then it is TypeScript syntax. @@ -21128,7 +21577,7 @@ var ts; transformFlags |= 3 /* AssertTypeScript */; } // Currently, we only support generators that were originally async function bodies. - if (asteriskToken && node.emitFlags & 2097152 /* AsyncFunctionBody */) { + if (asteriskToken && ts.getEmitFlags(node) & 2097152 /* AsyncFunctionBody */) { transformFlags |= 1536 /* AssertGenerator */; } node.transformFlags = transformFlags | 536870912 /* HasComputedFlags */; @@ -21190,7 +21639,7 @@ var ts; // down-level generator. // Currently we do not support transforming any other generator fucntions // down level. - if (asteriskToken && node.emitFlags & 2097152 /* AsyncFunctionBody */) { + if (asteriskToken && ts.getEmitFlags(node) & 2097152 /* AsyncFunctionBody */) { transformFlags |= 1536 /* AssertGenerator */; } } @@ -21216,7 +21665,7 @@ var ts; // down-level generator. // Currently we do not support transforming any other generator fucntions // down level. - if (asteriskToken && node.emitFlags & 2097152 /* AsyncFunctionBody */) { + if (asteriskToken && ts.getEmitFlags(node) & 2097152 /* AsyncFunctionBody */) { transformFlags |= 1536 /* AssertGenerator */; } node.transformFlags = transformFlags | 536870912 /* HasComputedFlags */; @@ -21500,5424 +21949,5111 @@ var ts; return transformFlags & ~excludeFlags; } })(ts || (ts = {})); -/// -/* @internal */ +/// +/// var ts; (function (ts) { - var ambientModuleSymbolRegex = /^".+"$/; - var nextSymbolId = 1; - var nextNodeId = 1; - var nextMergeId = 1; - var nextFlowId = 1; - function getNodeId(node) { - if (!node.id) { - node.id = nextNodeId; - nextNodeId++; + function trace(host, message) { + host.trace(ts.formatMessage.apply(undefined, arguments)); + } + ts.trace = trace; + /* @internal */ + function isTraceEnabled(compilerOptions, host) { + return compilerOptions.traceResolution && host.trace !== undefined; + } + ts.isTraceEnabled = isTraceEnabled; + /* @internal */ + function createResolvedModule(resolvedFileName, isExternalLibraryImport, failedLookupLocations) { + return { resolvedModule: resolvedFileName ? { resolvedFileName: resolvedFileName, isExternalLibraryImport: isExternalLibraryImport } : undefined, failedLookupLocations: failedLookupLocations }; + } + ts.createResolvedModule = createResolvedModule; + function moduleHasNonRelativeName(moduleName) { + return !(ts.isRootedDiskPath(moduleName) || ts.isExternalModuleNameRelative(moduleName)); + } + function tryReadTypesSection(packageJsonPath, baseDirectory, state) { + var jsonContent = readJson(packageJsonPath, state.host); + function tryReadFromField(fieldName) { + if (ts.hasProperty(jsonContent, fieldName)) { + var typesFile = jsonContent[fieldName]; + if (typeof typesFile === "string") { + var typesFilePath_1 = ts.normalizePath(ts.combinePaths(baseDirectory, typesFile)); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.package_json_has_0_field_1_that_references_2, fieldName, typesFile, typesFilePath_1); + } + return typesFilePath_1; + } + else { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Expected_type_of_0_field_in_package_json_to_be_string_got_1, fieldName, typeof typesFile); + } + } + } } - return node.id; + var typesFilePath = tryReadFromField("typings") || tryReadFromField("types"); + if (typesFilePath) { + return typesFilePath; + } + // Use the main module for inferring types if no types package specified and the allowJs is set + if (state.compilerOptions.allowJs && jsonContent.main && typeof jsonContent.main === "string") { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.No_types_specified_in_package_json_but_allowJs_is_set_so_returning_main_value_of_0, jsonContent.main); + } + var mainFilePath = ts.normalizePath(ts.combinePaths(baseDirectory, jsonContent.main)); + return mainFilePath; + } + return undefined; } - ts.getNodeId = getNodeId; - function getSymbolId(symbol) { - if (!symbol.id) { - symbol.id = nextSymbolId; - nextSymbolId++; + function readJson(path, host) { + try { + var jsonText = host.readFile(path); + return jsonText ? JSON.parse(jsonText) : {}; + } + catch (e) { + // gracefully handle if readFile fails or returns not JSON + return {}; } - return symbol.id; } - ts.getSymbolId = getSymbolId; - function createTypeChecker(host, produceDiagnostics) { - // Cancellation that controls whether or not we can cancel in the middle of type checking. - // In general cancelling is *not* safe for the type checker. We might be in the middle of - // computing something, and we will leave our internals in an inconsistent state. Callers - // who set the cancellation token should catch if a cancellation exception occurs, and - // should throw away and create a new TypeChecker. - // - // Currently we only support setting the cancellation token when getting diagnostics. This - // is because diagnostics can be quite expensive, and we want to allow hosts to bail out if - // they no longer need the information (for example, if the user started editing again). - var cancellationToken; - var Symbol = ts.objectAllocator.getSymbolConstructor(); - var Type = ts.objectAllocator.getTypeConstructor(); - var Signature = ts.objectAllocator.getSignatureConstructor(); - var typeCount = 0; - var symbolCount = 0; - var emptyArray = []; - var emptySymbols = ts.createMap(); - var compilerOptions = host.getCompilerOptions(); - var languageVersion = compilerOptions.target || 0 /* ES3 */; - var modulekind = ts.getEmitModuleKind(compilerOptions); - var noUnusedIdentifiers = !!compilerOptions.noUnusedLocals || !!compilerOptions.noUnusedParameters; - var allowSyntheticDefaultImports = typeof compilerOptions.allowSyntheticDefaultImports !== "undefined" ? compilerOptions.allowSyntheticDefaultImports : modulekind === ts.ModuleKind.System; - var strictNullChecks = compilerOptions.strictNullChecks; - var emitResolver = createResolver(); - var undefinedSymbol = createSymbol(4 /* Property */ | 67108864 /* Transient */, "undefined"); - undefinedSymbol.declarations = []; - var argumentsSymbol = createSymbol(4 /* Property */ | 67108864 /* Transient */, "arguments"); - var checker = { - getNodeCount: function () { return ts.sum(host.getSourceFiles(), "nodeCount"); }, - getIdentifierCount: function () { return ts.sum(host.getSourceFiles(), "identifierCount"); }, - getSymbolCount: function () { return ts.sum(host.getSourceFiles(), "symbolCount") + symbolCount; }, - getTypeCount: function () { return typeCount; }, - isUndefinedSymbol: function (symbol) { return symbol === undefinedSymbol; }, - isArgumentsSymbol: function (symbol) { return symbol === argumentsSymbol; }, - isUnknownSymbol: function (symbol) { return symbol === unknownSymbol; }, - getDiagnostics: getDiagnostics, - getGlobalDiagnostics: getGlobalDiagnostics, - getTypeOfSymbolAtLocation: getTypeOfSymbolAtLocation, - getSymbolsOfParameterPropertyDeclaration: getSymbolsOfParameterPropertyDeclaration, - getDeclaredTypeOfSymbol: getDeclaredTypeOfSymbol, - getPropertiesOfType: getPropertiesOfType, - getPropertyOfType: getPropertyOfType, - getSignaturesOfType: getSignaturesOfType, - getIndexTypeOfType: getIndexTypeOfType, - getBaseTypes: getBaseTypes, - getReturnTypeOfSignature: getReturnTypeOfSignature, - getNonNullableType: getNonNullableType, - getSymbolsInScope: getSymbolsInScope, - getSymbolAtLocation: getSymbolAtLocation, - getShorthandAssignmentValueSymbol: getShorthandAssignmentValueSymbol, - getExportSpecifierLocalTargetSymbol: getExportSpecifierLocalTargetSymbol, - getTypeAtLocation: getTypeOfNode, - getPropertySymbolOfDestructuringAssignment: getPropertySymbolOfDestructuringAssignment, - typeToString: typeToString, - getSymbolDisplayBuilder: getSymbolDisplayBuilder, - symbolToString: symbolToString, - getAugmentedPropertiesOfType: getAugmentedPropertiesOfType, - getRootSymbols: getRootSymbols, - getContextualType: getContextualType, - getFullyQualifiedName: getFullyQualifiedName, - getResolvedSignature: getResolvedSignature, - getConstantValue: getConstantValue, - isValidPropertyAccess: isValidPropertyAccess, - getSignatureFromDeclaration: getSignatureFromDeclaration, - isImplementationOfOverload: isImplementationOfOverload, - getAliasedSymbol: resolveAlias, - getEmitResolver: getEmitResolver, - getExportsOfModule: getExportsOfModuleAsArray, - getAmbientModules: getAmbientModules, - getJsxElementAttributesType: getJsxElementAttributesType, - getJsxIntrinsicTagNames: getJsxIntrinsicTagNames, - isOptionalParameter: isOptionalParameter - }; - var tupleTypes = []; - var unionTypes = ts.createMap(); - var intersectionTypes = ts.createMap(); - var stringLiteralTypes = ts.createMap(); - var numericLiteralTypes = ts.createMap(); - var unknownSymbol = createSymbol(4 /* Property */ | 67108864 /* Transient */, "unknown"); - var resolvingSymbol = createSymbol(67108864 /* Transient */, "__resolving__"); - var anyType = createIntrinsicType(1 /* Any */, "any"); - var unknownType = createIntrinsicType(1 /* Any */, "unknown"); - var undefinedType = createIntrinsicType(2048 /* Undefined */, "undefined"); - var undefinedWideningType = strictNullChecks ? undefinedType : createIntrinsicType(2048 /* Undefined */ | 33554432 /* ContainsWideningType */, "undefined"); - var nullType = createIntrinsicType(4096 /* Null */, "null"); - var nullWideningType = strictNullChecks ? nullType : createIntrinsicType(4096 /* Null */ | 33554432 /* ContainsWideningType */, "null"); - var stringType = createIntrinsicType(2 /* String */, "string"); - var numberType = createIntrinsicType(4 /* Number */, "number"); - var trueType = createIntrinsicType(128 /* BooleanLiteral */, "true"); - var falseType = createIntrinsicType(128 /* BooleanLiteral */, "false"); - var booleanType = createBooleanType([trueType, falseType]); - var esSymbolType = createIntrinsicType(512 /* ESSymbol */, "symbol"); - var voidType = createIntrinsicType(1024 /* Void */, "void"); - var neverType = createIntrinsicType(8192 /* Never */, "never"); - var silentNeverType = createIntrinsicType(8192 /* Never */, "never"); - var emptyObjectType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); - var emptyGenericType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); - emptyGenericType.instantiations = ts.createMap(); - var anyFunctionType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); - // The anyFunctionType contains the anyFunctionType by definition. The flag is further propagated - // in getPropagatingFlagsOfTypes, and it is checked in inferFromTypes. - anyFunctionType.flags |= 134217728 /* ContainsAnyFunctionType */; - var noConstraintType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); - var anySignature = createSignature(undefined, undefined, undefined, emptyArray, anyType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false); - var unknownSignature = createSignature(undefined, undefined, undefined, emptyArray, unknownType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false); - var resolvingSignature = createSignature(undefined, undefined, undefined, emptyArray, anyType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false); - var silentNeverSignature = createSignature(undefined, undefined, undefined, emptyArray, silentNeverType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false); - var enumNumberIndexInfo = createIndexInfo(stringType, /*isReadonly*/ true); - var globals = ts.createMap(); - /** - * List of every ambient module with a "*" wildcard. - * Unlike other ambient modules, these can't be stored in `globals` because symbol tables only deal with exact matches. - * This is only used if there is no exact match. - */ - var patternAmbientModules; - var getGlobalESSymbolConstructorSymbol; - var getGlobalPromiseConstructorSymbol; - var tryGetGlobalPromiseConstructorSymbol; - var globalObjectType; - var globalFunctionType; - var globalArrayType; - var globalReadonlyArrayType; - var globalStringType; - var globalNumberType; - var globalBooleanType; - var globalRegExpType; - var anyArrayType; - var anyReadonlyArrayType; - // The library files are only loaded when the feature is used. - // This allows users to just specify library files they want to used through --lib - // and they will not get an error from not having unrelated library files - var getGlobalTemplateStringsArrayType; - var getGlobalESSymbolType; - var getGlobalIterableType; - var getGlobalIteratorType; - var getGlobalIterableIteratorType; - var getGlobalClassDecoratorType; - var getGlobalParameterDecoratorType; - var getGlobalPropertyDecoratorType; - var getGlobalMethodDecoratorType; - var getGlobalTypedPropertyDescriptorType; - var getGlobalPromiseType; - var tryGetGlobalPromiseType; - var getGlobalPromiseLikeType; - var getInstantiatedGlobalPromiseLikeType; - var getGlobalPromiseConstructorLikeType; - var getGlobalThenableType; - var jsxElementClassType; - var deferredNodes; - var deferredUnusedIdentifierNodes; - var flowLoopStart = 0; - var flowLoopCount = 0; - var visitedFlowCount = 0; - var emptyStringType = getLiteralTypeForText(32 /* StringLiteral */, ""); - var zeroType = getLiteralTypeForText(64 /* NumberLiteral */, "0"); - var resolutionTargets = []; - var resolutionResults = []; - var resolutionPropertyNames = []; - var mergedSymbols = []; - var symbolLinks = []; - var nodeLinks = []; - var flowLoopCaches = []; - var flowLoopNodes = []; - var flowLoopKeys = []; - var flowLoopTypes = []; - var visitedFlowNodes = []; - var visitedFlowTypes = []; - var potentialThisCollisions = []; - var awaitedTypeStack = []; - var diagnostics = ts.createDiagnosticCollection(); - var TypeFacts; - (function (TypeFacts) { - TypeFacts[TypeFacts["None"] = 0] = "None"; - TypeFacts[TypeFacts["TypeofEQString"] = 1] = "TypeofEQString"; - TypeFacts[TypeFacts["TypeofEQNumber"] = 2] = "TypeofEQNumber"; - TypeFacts[TypeFacts["TypeofEQBoolean"] = 4] = "TypeofEQBoolean"; - TypeFacts[TypeFacts["TypeofEQSymbol"] = 8] = "TypeofEQSymbol"; - TypeFacts[TypeFacts["TypeofEQObject"] = 16] = "TypeofEQObject"; - TypeFacts[TypeFacts["TypeofEQFunction"] = 32] = "TypeofEQFunction"; - TypeFacts[TypeFacts["TypeofEQHostObject"] = 64] = "TypeofEQHostObject"; - TypeFacts[TypeFacts["TypeofNEString"] = 128] = "TypeofNEString"; - TypeFacts[TypeFacts["TypeofNENumber"] = 256] = "TypeofNENumber"; - TypeFacts[TypeFacts["TypeofNEBoolean"] = 512] = "TypeofNEBoolean"; - TypeFacts[TypeFacts["TypeofNESymbol"] = 1024] = "TypeofNESymbol"; - TypeFacts[TypeFacts["TypeofNEObject"] = 2048] = "TypeofNEObject"; - TypeFacts[TypeFacts["TypeofNEFunction"] = 4096] = "TypeofNEFunction"; - TypeFacts[TypeFacts["TypeofNEHostObject"] = 8192] = "TypeofNEHostObject"; - TypeFacts[TypeFacts["EQUndefined"] = 16384] = "EQUndefined"; - TypeFacts[TypeFacts["EQNull"] = 32768] = "EQNull"; - TypeFacts[TypeFacts["EQUndefinedOrNull"] = 65536] = "EQUndefinedOrNull"; - TypeFacts[TypeFacts["NEUndefined"] = 131072] = "NEUndefined"; - TypeFacts[TypeFacts["NENull"] = 262144] = "NENull"; - TypeFacts[TypeFacts["NEUndefinedOrNull"] = 524288] = "NEUndefinedOrNull"; - TypeFacts[TypeFacts["Truthy"] = 1048576] = "Truthy"; - TypeFacts[TypeFacts["Falsy"] = 2097152] = "Falsy"; - TypeFacts[TypeFacts["Discriminatable"] = 4194304] = "Discriminatable"; - TypeFacts[TypeFacts["All"] = 8388607] = "All"; - // The following members encode facts about particular kinds of types for use in the getTypeFacts function. - // The presence of a particular fact means that the given test is true for some (and possibly all) values - // of that kind of type. - TypeFacts[TypeFacts["BaseStringStrictFacts"] = 933633] = "BaseStringStrictFacts"; - TypeFacts[TypeFacts["BaseStringFacts"] = 3145473] = "BaseStringFacts"; - TypeFacts[TypeFacts["StringStrictFacts"] = 4079361] = "StringStrictFacts"; - TypeFacts[TypeFacts["StringFacts"] = 4194049] = "StringFacts"; - TypeFacts[TypeFacts["EmptyStringStrictFacts"] = 3030785] = "EmptyStringStrictFacts"; - TypeFacts[TypeFacts["EmptyStringFacts"] = 3145473] = "EmptyStringFacts"; - TypeFacts[TypeFacts["NonEmptyStringStrictFacts"] = 1982209] = "NonEmptyStringStrictFacts"; - TypeFacts[TypeFacts["NonEmptyStringFacts"] = 4194049] = "NonEmptyStringFacts"; - TypeFacts[TypeFacts["BaseNumberStrictFacts"] = 933506] = "BaseNumberStrictFacts"; - TypeFacts[TypeFacts["BaseNumberFacts"] = 3145346] = "BaseNumberFacts"; - TypeFacts[TypeFacts["NumberStrictFacts"] = 4079234] = "NumberStrictFacts"; - TypeFacts[TypeFacts["NumberFacts"] = 4193922] = "NumberFacts"; - TypeFacts[TypeFacts["ZeroStrictFacts"] = 3030658] = "ZeroStrictFacts"; - TypeFacts[TypeFacts["ZeroFacts"] = 3145346] = "ZeroFacts"; - TypeFacts[TypeFacts["NonZeroStrictFacts"] = 1982082] = "NonZeroStrictFacts"; - TypeFacts[TypeFacts["NonZeroFacts"] = 4193922] = "NonZeroFacts"; - TypeFacts[TypeFacts["BaseBooleanStrictFacts"] = 933252] = "BaseBooleanStrictFacts"; - TypeFacts[TypeFacts["BaseBooleanFacts"] = 3145092] = "BaseBooleanFacts"; - TypeFacts[TypeFacts["BooleanStrictFacts"] = 4078980] = "BooleanStrictFacts"; - TypeFacts[TypeFacts["BooleanFacts"] = 4193668] = "BooleanFacts"; - TypeFacts[TypeFacts["FalseStrictFacts"] = 3030404] = "FalseStrictFacts"; - TypeFacts[TypeFacts["FalseFacts"] = 3145092] = "FalseFacts"; - TypeFacts[TypeFacts["TrueStrictFacts"] = 1981828] = "TrueStrictFacts"; - TypeFacts[TypeFacts["TrueFacts"] = 4193668] = "TrueFacts"; - TypeFacts[TypeFacts["SymbolStrictFacts"] = 1981320] = "SymbolStrictFacts"; - TypeFacts[TypeFacts["SymbolFacts"] = 4193160] = "SymbolFacts"; - TypeFacts[TypeFacts["ObjectStrictFacts"] = 6166480] = "ObjectStrictFacts"; - TypeFacts[TypeFacts["ObjectFacts"] = 8378320] = "ObjectFacts"; - TypeFacts[TypeFacts["FunctionStrictFacts"] = 6164448] = "FunctionStrictFacts"; - TypeFacts[TypeFacts["FunctionFacts"] = 8376288] = "FunctionFacts"; - TypeFacts[TypeFacts["UndefinedFacts"] = 2457472] = "UndefinedFacts"; - TypeFacts[TypeFacts["NullFacts"] = 2340752] = "NullFacts"; - })(TypeFacts || (TypeFacts = {})); - var typeofEQFacts = ts.createMap({ - "string": 1 /* TypeofEQString */, - "number": 2 /* TypeofEQNumber */, - "boolean": 4 /* TypeofEQBoolean */, - "symbol": 8 /* TypeofEQSymbol */, - "undefined": 16384 /* EQUndefined */, - "object": 16 /* TypeofEQObject */, - "function": 32 /* TypeofEQFunction */ - }); - var typeofNEFacts = ts.createMap({ - "string": 128 /* TypeofNEString */, - "number": 256 /* TypeofNENumber */, - "boolean": 512 /* TypeofNEBoolean */, - "symbol": 1024 /* TypeofNESymbol */, - "undefined": 131072 /* NEUndefined */, - "object": 2048 /* TypeofNEObject */, - "function": 4096 /* TypeofNEFunction */ - }); - var typeofTypesByName = ts.createMap({ - "string": stringType, - "number": numberType, - "boolean": booleanType, - "symbol": esSymbolType, - "undefined": undefinedType - }); - var jsxElementType; - /** Things we lazy load from the JSX namespace */ - var jsxTypes = ts.createMap(); - var JsxNames = { - JSX: "JSX", - IntrinsicElements: "IntrinsicElements", - ElementClass: "ElementClass", - ElementAttributesPropertyNameContainer: "ElementAttributesProperty", - Element: "Element", - IntrinsicAttributes: "IntrinsicAttributes", - IntrinsicClassAttributes: "IntrinsicClassAttributes" - }; - var subtypeRelation = ts.createMap(); - var assignableRelation = ts.createMap(); - var comparableRelation = ts.createMap(); - var identityRelation = ts.createMap(); - var enumRelation = ts.createMap(); - // This is for caching the result of getSymbolDisplayBuilder. Do not access directly. - var _displayBuilder; - var TypeSystemPropertyName; - (function (TypeSystemPropertyName) { - TypeSystemPropertyName[TypeSystemPropertyName["Type"] = 0] = "Type"; - TypeSystemPropertyName[TypeSystemPropertyName["ResolvedBaseConstructorType"] = 1] = "ResolvedBaseConstructorType"; - TypeSystemPropertyName[TypeSystemPropertyName["DeclaredType"] = 2] = "DeclaredType"; - TypeSystemPropertyName[TypeSystemPropertyName["ResolvedReturnType"] = 3] = "ResolvedReturnType"; - })(TypeSystemPropertyName || (TypeSystemPropertyName = {})); - var builtinGlobals = ts.createMap(); - builtinGlobals[undefinedSymbol.name] = undefinedSymbol; - initializeTypeChecker(); - return checker; - function getEmitResolver(sourceFile, cancellationToken) { - // Ensure we have all the type information in place for this file so that all the - // emitter questions of this resolver will return the right information. - getDiagnostics(sourceFile, cancellationToken); - return emitResolver; + var typeReferenceExtensions = [".d.ts"]; + function getEffectiveTypeRoots(options, host) { + if (options.typeRoots) { + return options.typeRoots; } - function error(location, message, arg0, arg1, arg2) { - var diagnostic = location - ? ts.createDiagnosticForNode(location, message, arg0, arg1, arg2) - : ts.createCompilerDiagnostic(message, arg0, arg1, arg2); - diagnostics.add(diagnostic); + var currentDirectory; + if (options.configFilePath) { + currentDirectory = ts.getDirectoryPath(options.configFilePath); } - function createSymbol(flags, name) { - symbolCount++; - return new Symbol(flags, name); + else if (host.getCurrentDirectory) { + currentDirectory = host.getCurrentDirectory(); } - function getExcludedSymbolFlags(flags) { - var result = 0; - if (flags & 2 /* BlockScopedVariable */) - result |= 107455 /* BlockScopedVariableExcludes */; - if (flags & 1 /* FunctionScopedVariable */) - result |= 107454 /* FunctionScopedVariableExcludes */; - if (flags & 4 /* Property */) - result |= 0 /* PropertyExcludes */; - if (flags & 8 /* EnumMember */) - result |= 900095 /* EnumMemberExcludes */; - if (flags & 16 /* Function */) - result |= 106927 /* FunctionExcludes */; - if (flags & 32 /* Class */) - result |= 899519 /* ClassExcludes */; - if (flags & 64 /* Interface */) - result |= 792968 /* InterfaceExcludes */; - if (flags & 256 /* RegularEnum */) - result |= 899327 /* RegularEnumExcludes */; - if (flags & 128 /* ConstEnum */) - result |= 899967 /* ConstEnumExcludes */; - if (flags & 512 /* ValueModule */) - result |= 106639 /* ValueModuleExcludes */; - if (flags & 8192 /* Method */) - result |= 99263 /* MethodExcludes */; - if (flags & 32768 /* GetAccessor */) - result |= 41919 /* GetAccessorExcludes */; - if (flags & 65536 /* SetAccessor */) - result |= 74687 /* SetAccessorExcludes */; - if (flags & 262144 /* TypeParameter */) - result |= 530920 /* TypeParameterExcludes */; - if (flags & 524288 /* TypeAlias */) - result |= 793064 /* TypeAliasExcludes */; - if (flags & 8388608 /* Alias */) - result |= 8388608 /* AliasExcludes */; - return result; + return currentDirectory !== undefined && getDefaultTypeRoots(currentDirectory, host); + } + ts.getEffectiveTypeRoots = getEffectiveTypeRoots; + /** + * Returns the path to every node_modules/@types directory from some ancestor directory. + * Returns undefined if there are none. + */ + function getDefaultTypeRoots(currentDirectory, host) { + if (!host.directoryExists) { + return [ts.combinePaths(currentDirectory, nodeModulesAtTypes)]; } - function recordMergedSymbol(target, source) { - if (!source.mergeId) { - source.mergeId = nextMergeId; - nextMergeId++; + var typeRoots; + while (true) { + var atTypes = ts.combinePaths(currentDirectory, nodeModulesAtTypes); + if (host.directoryExists(atTypes)) { + (typeRoots || (typeRoots = [])).push(atTypes); } - mergedSymbols[source.mergeId] = target; - } - function cloneSymbol(symbol) { - var result = createSymbol(symbol.flags | 33554432 /* Merged */, symbol.name); - result.declarations = symbol.declarations.slice(0); - result.parent = symbol.parent; - if (symbol.valueDeclaration) - result.valueDeclaration = symbol.valueDeclaration; - if (symbol.constEnumOnlyModule) - result.constEnumOnlyModule = true; - if (symbol.members) - result.members = ts.cloneMap(symbol.members); - if (symbol.exports) - result.exports = ts.cloneMap(symbol.exports); - recordMergedSymbol(result, symbol); - return result; + var parent_7 = ts.getDirectoryPath(currentDirectory); + if (parent_7 === currentDirectory) { + break; + } + currentDirectory = parent_7; } - function mergeSymbol(target, source) { - if (!(target.flags & getExcludedSymbolFlags(source.flags))) { - if (source.flags & 512 /* ValueModule */ && target.flags & 512 /* ValueModule */ && target.constEnumOnlyModule && !source.constEnumOnlyModule) { - // reset flag when merging instantiated module into value module that has only const enums - target.constEnumOnlyModule = false; + return typeRoots; + } + var nodeModulesAtTypes = ts.combinePaths("node_modules", "@types"); + /** + * @param {string | undefined} containingFile - file that contains type reference directive, can be undefined if containing file is unknown. + * This is possible in case if resolution is performed for directives specified via 'types' parameter. In this case initial path for secondary lookups + * is assumed to be the same as root directory of the project. + */ + function resolveTypeReferenceDirective(typeReferenceDirectiveName, containingFile, options, host) { + var traceEnabled = isTraceEnabled(options, host); + var moduleResolutionState = { + compilerOptions: options, + host: host, + skipTsx: true, + traceEnabled: traceEnabled + }; + var typeRoots = getEffectiveTypeRoots(options, host); + if (traceEnabled) { + if (containingFile === undefined) { + if (typeRoots === undefined) { + trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_not_set_root_directory_not_set, typeReferenceDirectiveName); } - target.flags |= source.flags; - if (source.valueDeclaration && - (!target.valueDeclaration || - (target.valueDeclaration.kind === 225 /* ModuleDeclaration */ && source.valueDeclaration.kind !== 225 /* ModuleDeclaration */))) { - // other kinds of value declarations take precedence over modules - target.valueDeclaration = source.valueDeclaration; - } - ts.forEach(source.declarations, function (node) { - target.declarations.push(node); - }); - if (source.members) { - if (!target.members) - target.members = ts.createMap(); - mergeSymbolTable(target.members, source.members); - } - if (source.exports) { - if (!target.exports) - target.exports = ts.createMap(); - mergeSymbolTable(target.exports, source.exports); + else { + trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_not_set_root_directory_1, typeReferenceDirectiveName, typeRoots); } - recordMergedSymbol(target, source); } else { - var message_2 = target.flags & 2 /* BlockScopedVariable */ || source.flags & 2 /* BlockScopedVariable */ - ? ts.Diagnostics.Cannot_redeclare_block_scoped_variable_0 : ts.Diagnostics.Duplicate_identifier_0; - ts.forEach(source.declarations, function (node) { - error(node.name ? node.name : node, message_2, symbolToString(source)); - }); - ts.forEach(target.declarations, function (node) { - error(node.name ? node.name : node, message_2, symbolToString(source)); - }); - } - } - function mergeSymbolTable(target, source) { - for (var id in source) { - var targetSymbol = target[id]; - if (!targetSymbol) { - target[id] = source[id]; + if (typeRoots === undefined) { + trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_1_root_directory_not_set, typeReferenceDirectiveName, containingFile); } else { - if (!(targetSymbol.flags & 33554432 /* Merged */)) { - target[id] = targetSymbol = cloneSymbol(targetSymbol); - } - mergeSymbol(targetSymbol, source[id]); + trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_1_root_directory_2, typeReferenceDirectiveName, containingFile, typeRoots); } } } - function mergeModuleAugmentation(moduleName) { - var moduleAugmentation = moduleName.parent; - if (moduleAugmentation.symbol.declarations[0] !== moduleAugmentation) { - // this is a combined symbol for multiple augmentations within the same file. - // its symbol already has accumulated information for all declarations - // so we need to add it just once - do the work only for first declaration - ts.Debug.assert(moduleAugmentation.symbol.declarations.length > 1); - return; - } - if (ts.isGlobalScopeAugmentation(moduleAugmentation)) { - mergeSymbolTable(globals, moduleAugmentation.symbol.exports); + var failedLookupLocations = []; + // Check primary library paths + if (typeRoots && typeRoots.length) { + if (traceEnabled) { + trace(host, ts.Diagnostics.Resolving_with_primary_search_path_0, typeRoots.join(", ")); } - else { - // find a module that about to be augmented - // do not validate names of augmentations that are defined in ambient context - var moduleNotFoundError = !ts.isInAmbientContext(moduleName.parent.parent) - ? ts.Diagnostics.Invalid_module_name_in_augmentation_module_0_cannot_be_found - : undefined; - var mainModule = resolveExternalModuleNameWorker(moduleName, moduleName, moduleNotFoundError); - if (!mainModule) { - return; - } - // obtain item referenced by 'export=' - mainModule = resolveExternalModuleSymbol(mainModule); - if (mainModule.flags & 1920 /* Namespace */) { - // if module symbol has already been merged - it is safe to use it. - // otherwise clone it - mainModule = mainModule.flags & 33554432 /* Merged */ ? mainModule : cloneSymbol(mainModule); - mergeSymbol(mainModule, moduleAugmentation.symbol); - } - else { - error(moduleName, ts.Diagnostics.Cannot_augment_module_0_because_it_resolves_to_a_non_module_entity, moduleName.text); + var primarySearchPaths = typeRoots; + for (var _i = 0, primarySearchPaths_1 = primarySearchPaths; _i < primarySearchPaths_1.length; _i++) { + var typeRoot = primarySearchPaths_1[_i]; + var candidate = ts.combinePaths(typeRoot, typeReferenceDirectiveName); + var candidateDirectory = ts.getDirectoryPath(candidate); + var resolvedFile_1 = loadNodeModuleFromDirectory(typeReferenceExtensions, candidate, failedLookupLocations, !directoryProbablyExists(candidateDirectory, host), moduleResolutionState); + if (resolvedFile_1) { + if (traceEnabled) { + trace(host, ts.Diagnostics.Type_reference_directive_0_was_successfully_resolved_to_1_primary_Colon_2, typeReferenceDirectiveName, resolvedFile_1, true); + } + return { + resolvedTypeReferenceDirective: { primary: true, resolvedFileName: resolvedFile_1 }, + failedLookupLocations: failedLookupLocations + }; } } } - function addToSymbolTable(target, source, message) { - for (var id in source) { - if (target[id]) { - // Error on redeclarations - ts.forEach(target[id].declarations, addDeclarationDiagnostic(id, message)); + else { + if (traceEnabled) { + trace(host, ts.Diagnostics.Root_directory_cannot_be_determined_skipping_primary_search_paths); + } + } + var resolvedFile; + var initialLocationForSecondaryLookup; + if (containingFile) { + initialLocationForSecondaryLookup = ts.getDirectoryPath(containingFile); + } + if (initialLocationForSecondaryLookup !== undefined) { + // check secondary locations + if (traceEnabled) { + trace(host, ts.Diagnostics.Looking_up_in_node_modules_folder_initial_location_0, initialLocationForSecondaryLookup); + } + resolvedFile = loadModuleFromNodeModules(typeReferenceDirectiveName, initialLocationForSecondaryLookup, failedLookupLocations, moduleResolutionState, /*checkOneLevel*/ false); + if (traceEnabled) { + if (resolvedFile) { + trace(host, ts.Diagnostics.Type_reference_directive_0_was_successfully_resolved_to_1_primary_Colon_2, typeReferenceDirectiveName, resolvedFile, false); } else { - target[id] = source[id]; + trace(host, ts.Diagnostics.Type_reference_directive_0_was_not_resolved, typeReferenceDirectiveName); } } - function addDeclarationDiagnostic(id, message) { - return function (declaration) { return diagnostics.add(ts.createDiagnosticForNode(declaration, message, id)); }; - } - } - function getSymbolLinks(symbol) { - if (symbol.flags & 67108864 /* Transient */) - return symbol; - var id = getSymbolId(symbol); - return symbolLinks[id] || (symbolLinks[id] = {}); } - function getNodeLinks(node) { - var nodeId = getNodeId(node); - return nodeLinks[nodeId] || (nodeLinks[nodeId] = { flags: 0 }); + else { + if (traceEnabled) { + trace(host, ts.Diagnostics.Containing_file_is_not_specified_and_root_directory_cannot_be_determined_skipping_lookup_in_node_modules_folder); + } } - function isGlobalSourceFile(node) { - return node.kind === 256 /* SourceFile */ && !ts.isExternalOrCommonJsModule(node); + return { + resolvedTypeReferenceDirective: resolvedFile + ? { primary: false, resolvedFileName: resolvedFile } + : undefined, + failedLookupLocations: failedLookupLocations + }; + } + ts.resolveTypeReferenceDirective = resolveTypeReferenceDirective; + /** + * Given a set of options, returns the set of type directive names + * that should be included for this program automatically. + * This list could either come from the config file, + * or from enumerating the types root + initial secondary types lookup location. + * More type directives might appear in the program later as a result of loading actual source files; + * this list is only the set of defaults that are implicitly included. + */ + function getAutomaticTypeDirectiveNames(options, host) { + // Use explicit type list from tsconfig.json + if (options.types) { + return options.types; } - function getSymbol(symbols, name, meaning) { - if (meaning) { - var symbol = symbols[name]; - if (symbol) { - ts.Debug.assert((symbol.flags & 16777216 /* Instantiated */) === 0, "Should never get an instantiated symbol here."); - if (symbol.flags & meaning) { - return symbol; - } - if (symbol.flags & 8388608 /* Alias */) { - var target = resolveAlias(symbol); - // Unknown symbol means an error occurred in alias resolution, treat it as positive answer to avoid cascading errors - if (target === unknownSymbol || target.flags & meaning) { - return symbol; + // Walk the primary type lookup locations + var result = []; + if (host.directoryExists && host.getDirectories) { + var typeRoots = getEffectiveTypeRoots(options, host); + if (typeRoots) { + for (var _i = 0, typeRoots_1 = typeRoots; _i < typeRoots_1.length; _i++) { + var root = typeRoots_1[_i]; + if (host.directoryExists(root)) { + for (var _a = 0, _b = host.getDirectories(root); _a < _b.length; _a++) { + var typeDirectivePath = _b[_a]; + var normalized = ts.normalizePath(typeDirectivePath); + var packageJsonPath = pathToPackageJson(ts.combinePaths(root, normalized)); + // tslint:disable-next-line:no-null-keyword + var isNotNeededPackage = host.fileExists(packageJsonPath) && readJson(packageJsonPath, host).typings === null; + if (!isNotNeededPackage) { + // Return just the type directive names + result.push(ts.getBaseFileName(normalized)); + } } } } } - // return undefined if we can't find a symbol. } - /** - * Get symbols that represent parameter-property-declaration as parameter and as property declaration - * @param parameter a parameterDeclaration node - * @param parameterName a name of the parameter to get the symbols for. - * @return a tuple of two symbols - */ - function getSymbolsOfParameterPropertyDeclaration(parameter, parameterName) { - var constructorDeclaration = parameter.parent; - var classDeclaration = parameter.parent.parent; - var parameterSymbol = getSymbol(constructorDeclaration.locals, parameterName, 107455 /* Value */); - var propertySymbol = getSymbol(classDeclaration.symbol.members, parameterName, 107455 /* Value */); - if (parameterSymbol && propertySymbol) { - return [parameterSymbol, propertySymbol]; + return result; + } + ts.getAutomaticTypeDirectiveNames = getAutomaticTypeDirectiveNames; + function resolveModuleName(moduleName, containingFile, compilerOptions, host) { + var traceEnabled = isTraceEnabled(compilerOptions, host); + if (traceEnabled) { + trace(host, ts.Diagnostics.Resolving_module_0_from_1, moduleName, containingFile); + } + var moduleResolution = compilerOptions.moduleResolution; + if (moduleResolution === undefined) { + moduleResolution = ts.getEmitModuleKind(compilerOptions) === ts.ModuleKind.CommonJS ? ts.ModuleResolutionKind.NodeJs : ts.ModuleResolutionKind.Classic; + if (traceEnabled) { + trace(host, ts.Diagnostics.Module_resolution_kind_is_not_specified_using_0, ts.ModuleResolutionKind[moduleResolution]); } - ts.Debug.fail("There should exist two symbols, one as property declaration and one as parameter declaration"); } - function isBlockScopedNameDeclaredBeforeUse(declaration, usage) { - var declarationFile = ts.getSourceFileOfNode(declaration); - var useFile = ts.getSourceFileOfNode(usage); - if (declarationFile !== useFile) { - if ((modulekind && (declarationFile.externalModuleIndicator || useFile.externalModuleIndicator)) || - (!compilerOptions.outFile && !compilerOptions.out)) { - // nodes are in different files and order cannot be determines - return true; + else { + if (traceEnabled) { + trace(host, ts.Diagnostics.Explicitly_specified_module_resolution_kind_Colon_0, ts.ModuleResolutionKind[moduleResolution]); + } + } + var result; + switch (moduleResolution) { + case ts.ModuleResolutionKind.NodeJs: + result = nodeModuleNameResolver(moduleName, containingFile, compilerOptions, host); + break; + case ts.ModuleResolutionKind.Classic: + result = classicNameResolver(moduleName, containingFile, compilerOptions, host); + break; + } + if (traceEnabled) { + if (result.resolvedModule) { + trace(host, ts.Diagnostics.Module_name_0_was_successfully_resolved_to_1, moduleName, result.resolvedModule.resolvedFileName); + } + else { + trace(host, ts.Diagnostics.Module_name_0_was_not_resolved, moduleName); + } + } + return result; + } + ts.resolveModuleName = resolveModuleName; + /** + * Any module resolution kind can be augmented with optional settings: 'baseUrl', 'paths' and 'rootDirs' - they are used to + * mitigate differences between design time structure of the project and its runtime counterpart so the same import name + * can be resolved successfully by TypeScript compiler and runtime module loader. + * If these settings are set then loading procedure will try to use them to resolve module name and it can of failure it will + * fallback to standard resolution routine. + * + * - baseUrl - this setting controls how non-relative module names are resolved. If this setting is specified then non-relative + * names will be resolved relative to baseUrl: i.e. if baseUrl is '/a/b' then candidate location to resolve module name 'c/d' will + * be '/a/b/c/d' + * - paths - this setting can only be used when baseUrl is specified. allows to tune how non-relative module names + * will be resolved based on the content of the module name. + * Structure of 'paths' compiler options + * 'paths': { + * pattern-1: [...substitutions], + * pattern-2: [...substitutions], + * ... + * pattern-n: [...substitutions] + * } + * Pattern here is a string that can contain zero or one '*' character. During module resolution module name will be matched against + * all patterns in the list. Matching for patterns that don't contain '*' means that module name must be equal to pattern respecting the case. + * If pattern contains '*' then to match pattern "*" module name must start with the and end with . + * denotes part of the module name between and . + * If module name can be matches with multiple patterns then pattern with the longest prefix will be picked. + * After selecting pattern we'll use list of substitutions to get candidate locations of the module and the try to load module + * from the candidate location. + * Substitution is a string that can contain zero or one '*'. To get candidate location from substitution we'll pick every + * substitution in the list and replace '*' with string. If candidate location is not rooted it + * will be converted to absolute using baseUrl. + * For example: + * baseUrl: /a/b/c + * "paths": { + * // match all module names + * "*": [ + * "*", // use matched name as is, + * // will be looked as /a/b/c/ + * + * "folder1/*" // substitution will convert matched name to 'folder1/', + * // since it is not rooted then final candidate location will be /a/b/c/folder1/ + * ], + * // match module names that start with 'components/' + * "components/*": [ "/root/components/*" ] // substitution will convert /components/folder1/ to '/root/components/folder1/', + * // it is rooted so it will be final candidate location + * } + * + * 'rootDirs' allows the project to be spreaded across multiple locations and resolve modules with relative names as if + * they were in the same location. For example lets say there are two files + * '/local/src/content/file1.ts' + * '/shared/components/contracts/src/content/protocols/file2.ts' + * After bundling content of '/shared/components/contracts/src' will be merged with '/local/src' so + * if file1 has the following import 'import {x} from "./protocols/file2"' it will be resolved successfully in runtime. + * 'rootDirs' provides the way to tell compiler that in order to get the whole project it should behave as if content of all + * root dirs were merged together. + * I.e. for the example above 'rootDirs' will have two entries: [ '/local/src', '/shared/components/contracts/src' ]. + * Compiler will first convert './protocols/file2' into absolute path relative to the location of containing file: + * '/local/src/content/protocols/file2' and try to load it - failure. + * Then it will search 'rootDirs' looking for a longest matching prefix of this absolute path and if such prefix is found - absolute path will + * be converted to a path relative to found rootDir entry './content/protocols/file2' (*). As a last step compiler will check all remaining + * entries in 'rootDirs', use them to build absolute path out of (*) and try to resolve module from this location. + */ + function tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state) { + if (moduleHasNonRelativeName(moduleName)) { + return tryLoadModuleUsingBaseUrl(moduleName, loader, failedLookupLocations, supportedExtensions, state); + } + else { + return tryLoadModuleUsingRootDirs(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state); + } + } + function tryLoadModuleUsingRootDirs(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state) { + if (!state.compilerOptions.rootDirs) { + return undefined; + } + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.rootDirs_option_is_set_using_it_to_resolve_relative_module_name_0, moduleName); + } + var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); + var matchedRootDir; + var matchedNormalizedPrefix; + for (var _i = 0, _a = state.compilerOptions.rootDirs; _i < _a.length; _i++) { + var rootDir = _a[_i]; + // rootDirs are expected to be absolute + // in case of tsconfig.json this will happen automatically - compiler will expand relative names + // using location of tsconfig.json as base location + var normalizedRoot = ts.normalizePath(rootDir); + if (!ts.endsWith(normalizedRoot, ts.directorySeparator)) { + normalizedRoot += ts.directorySeparator; + } + var isLongestMatchingPrefix = ts.startsWith(candidate, normalizedRoot) && + (matchedNormalizedPrefix === undefined || matchedNormalizedPrefix.length < normalizedRoot.length); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Checking_if_0_is_the_longest_matching_prefix_for_1_2, normalizedRoot, candidate, isLongestMatchingPrefix); + } + if (isLongestMatchingPrefix) { + matchedNormalizedPrefix = normalizedRoot; + matchedRootDir = rootDir; + } + } + if (matchedNormalizedPrefix) { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Longest_matching_prefix_for_0_is_1, candidate, matchedNormalizedPrefix); + } + var suffix = candidate.substr(matchedNormalizedPrefix.length); + // first - try to load from a initial location + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Loading_0_from_the_root_dir_1_candidate_location_2, suffix, matchedNormalizedPrefix, candidate); + } + var resolvedFileName = loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(containingDirectory, state.host), state); + if (resolvedFileName) { + return resolvedFileName; + } + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Trying_other_entries_in_rootDirs); + } + // then try to resolve using remaining entries in rootDirs + for (var _b = 0, _c = state.compilerOptions.rootDirs; _b < _c.length; _b++) { + var rootDir = _c[_b]; + if (rootDir === matchedRootDir) { + // skip the initially matched entry + continue; + } + var candidate_1 = ts.combinePaths(ts.normalizePath(rootDir), suffix); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Loading_0_from_the_root_dir_1_candidate_location_2, suffix, rootDir, candidate_1); + } + var baseDirectory = ts.getDirectoryPath(candidate_1); + var resolvedFileName_1 = loader(candidate_1, supportedExtensions, failedLookupLocations, !directoryProbablyExists(baseDirectory, state.host), state); + if (resolvedFileName_1) { + return resolvedFileName_1; } - var sourceFiles = host.getSourceFiles(); - return ts.indexOf(sourceFiles, declarationFile) <= ts.indexOf(sourceFiles, useFile); } - if (declaration.pos <= usage.pos) { - // declaration is before usage - // still might be illegal if usage is in the initializer of the variable declaration - return declaration.kind !== 218 /* VariableDeclaration */ || - !isImmediatelyUsedInInitializerOfBlockScopedVariable(declaration, usage); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Module_resolution_using_rootDirs_has_failed); } - // declaration is after usage - // can be legal if usage is deferred (i.e. inside function or in initializer of instance property) - return isUsedInFunctionOrNonStaticProperty(declaration, usage); - function isImmediatelyUsedInInitializerOfBlockScopedVariable(declaration, usage) { - var container = ts.getEnclosingBlockScopeContainer(declaration); - switch (declaration.parent.parent.kind) { - case 200 /* VariableStatement */: - case 206 /* ForStatement */: - case 208 /* ForOfStatement */: - // variable statement/for/for-of statement case, - // use site should not be inside variable declaration (initializer of declaration or binding element) - if (isSameScopeDescendentOf(usage, declaration, container)) { - return true; - } - break; + } + return undefined; + } + function tryLoadModuleUsingBaseUrl(moduleName, loader, failedLookupLocations, supportedExtensions, state) { + if (!state.compilerOptions.baseUrl) { + return undefined; + } + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.baseUrl_option_is_set_to_0_using_this_value_to_resolve_non_relative_module_name_1, state.compilerOptions.baseUrl, moduleName); + } + // string is for exact match + var matchedPattern = undefined; + if (state.compilerOptions.paths) { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.paths_option_is_specified_looking_for_a_pattern_to_match_module_name_0, moduleName); + } + matchedPattern = ts.matchPatternOrExact(ts.getOwnKeys(state.compilerOptions.paths), moduleName); + } + if (matchedPattern) { + var matchedStar = typeof matchedPattern === "string" ? undefined : ts.matchedText(matchedPattern, moduleName); + var matchedPatternText = typeof matchedPattern === "string" ? matchedPattern : ts.patternText(matchedPattern); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Module_name_0_matched_pattern_1, moduleName, matchedPatternText); + } + for (var _i = 0, _a = state.compilerOptions.paths[matchedPatternText]; _i < _a.length; _i++) { + var subst = _a[_i]; + var path = matchedStar ? subst.replace("*", matchedStar) : subst; + var candidate = ts.normalizePath(ts.combinePaths(state.compilerOptions.baseUrl, path)); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Trying_substitution_0_candidate_module_location_Colon_1, subst, path); } - switch (declaration.parent.parent.kind) { - case 207 /* ForInStatement */: - case 208 /* ForOfStatement */: - // ForIn/ForOf case - use site should not be used in expression part - if (isSameScopeDescendentOf(usage, declaration.parent.parent.expression, container)) { - return true; - } + var resolvedFileName = loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(ts.getDirectoryPath(candidate), state.host), state); + if (resolvedFileName) { + return resolvedFileName; } - return false; } - function isUsedInFunctionOrNonStaticProperty(declaration, usage) { - var container = ts.getEnclosingBlockScopeContainer(declaration); - var current = usage; - while (current) { - if (current === container) { - return false; - } - if (ts.isFunctionLike(current)) { - return true; - } - var initializerOfNonStaticProperty = current.parent && - current.parent.kind === 145 /* PropertyDeclaration */ && - (ts.getModifierFlags(current.parent) & 32 /* Static */) === 0 && - current.parent.initializer === current; - if (initializerOfNonStaticProperty) { - return true; - } - current = current.parent; + return undefined; + } + else { + var candidate = ts.normalizePath(ts.combinePaths(state.compilerOptions.baseUrl, moduleName)); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Resolving_module_name_0_relative_to_base_url_1_2, moduleName, state.compilerOptions.baseUrl, candidate); + } + return loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(ts.getDirectoryPath(candidate), state.host), state); + } + } + function nodeModuleNameResolver(moduleName, containingFile, compilerOptions, host) { + var containingDirectory = ts.getDirectoryPath(containingFile); + var supportedExtensions = ts.getSupportedExtensions(compilerOptions); + var traceEnabled = isTraceEnabled(compilerOptions, host); + var failedLookupLocations = []; + var state = { compilerOptions: compilerOptions, host: host, traceEnabled: traceEnabled, skipTsx: false }; + var resolvedFileName = tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, nodeLoadModuleByRelativeName, failedLookupLocations, supportedExtensions, state); + var isExternalLibraryImport = false; + if (!resolvedFileName) { + if (moduleHasNonRelativeName(moduleName)) { + if (traceEnabled) { + trace(host, ts.Diagnostics.Loading_module_0_from_node_modules_folder, moduleName); } - return false; + resolvedFileName = loadModuleFromNodeModules(moduleName, containingDirectory, failedLookupLocations, state, /*checkOneLevel*/ false); + isExternalLibraryImport = resolvedFileName !== undefined; + } + else { + var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); + resolvedFileName = nodeLoadModuleByRelativeName(candidate, supportedExtensions, failedLookupLocations, /*onlyRecordFailures*/ false, state); } } - // Resolve a given name for a given meaning at a given location. An error is reported if the name was not found and - // the nameNotFoundMessage argument is not undefined. Returns the resolved symbol, or undefined if no symbol with - // the given name can be found. - function resolveName(location, name, meaning, nameNotFoundMessage, nameArg) { - var result; - var lastLocation; - var propertyWithInvalidInitializer; - var errorLocation = location; - var grandparent; - var isInExternalModule = false; - loop: while (location) { - // Locals of a source file are not in scope (because they get merged into the global symbol table) - if (location.locals && !isGlobalSourceFile(location)) { - if (result = getSymbol(location.locals, name, meaning)) { - var useResult = true; - if (ts.isFunctionLike(location) && lastLocation && lastLocation !== location.body) { - // symbol lookup restrictions for function-like declarations - // - Type parameters of a function are in scope in the entire function declaration, including the parameter - // list and return type. However, local types are only in scope in the function body. - // - parameters are only in the scope of function body - // This restriction does not apply to JSDoc comment types because they are parented - // at a higher level than type parameters would normally be - if (meaning & result.flags & 793064 /* Type */ && lastLocation.kind !== 273 /* JSDocComment */) { - useResult = result.flags & 262144 /* TypeParameter */ - ? lastLocation === location.type || - lastLocation.kind === 142 /* Parameter */ || - lastLocation.kind === 141 /* TypeParameter */ - : false; - } - if (meaning & 107455 /* Value */ && result.flags & 1 /* FunctionScopedVariable */) { - // parameters are visible only inside function body, parameter list and return type - // technically for parameter list case here we might mix parameters and variables declared in function, - // however it is detected separately when checking initializers of parameters - // to make sure that they reference no variables declared after them. - useResult = - lastLocation.kind === 142 /* Parameter */ || - (lastLocation === location.type && - result.valueDeclaration.kind === 142 /* Parameter */); - } - } - if (useResult) { - break loop; - } - else { - result = undefined; - } - } - } - switch (location.kind) { - case 256 /* SourceFile */: - if (!ts.isExternalOrCommonJsModule(location)) - break; - isInExternalModule = true; - case 225 /* ModuleDeclaration */: - var moduleExports = getSymbolOfNode(location).exports; - if (location.kind === 256 /* SourceFile */ || ts.isAmbientModule(location)) { - // It's an external module. First see if the module has an export default and if the local - // name of that export default matches. - if (result = moduleExports["default"]) { - var localSymbol = ts.getLocalSymbolForExportDefault(result); - if (localSymbol && (result.flags & meaning) && localSymbol.name === name) { - break loop; - } - result = undefined; - } - // Because of module/namespace merging, a module's exports are in scope, - // yet we never want to treat an export specifier as putting a member in scope. - // Therefore, if the name we find is purely an export specifier, it is not actually considered in scope. - // Two things to note about this: - // 1. We have to check this without calling getSymbol. The problem with calling getSymbol - // on an export specifier is that it might find the export specifier itself, and try to - // resolve it as an alias. This will cause the checker to consider the export specifier - // a circular alias reference when it might not be. - // 2. We check === SymbolFlags.Alias in order to check that the symbol is *purely* - // an alias. If we used &, we'd be throwing out symbols that have non alias aspects, - // which is not the desired behavior. - if (moduleExports[name] && - moduleExports[name].flags === 8388608 /* Alias */ && - ts.getDeclarationOfKind(moduleExports[name], 238 /* ExportSpecifier */)) { - break; - } - } - if (result = getSymbol(moduleExports, name, meaning & 8914931 /* ModuleMember */)) { - break loop; - } - break; - case 224 /* EnumDeclaration */: - if (result = getSymbol(getSymbolOfNode(location).exports, name, meaning & 8 /* EnumMember */)) { - break loop; - } - break; - case 145 /* PropertyDeclaration */: - case 144 /* PropertySignature */: - // TypeScript 1.0 spec (April 2014): 8.4.1 - // Initializer expressions for instance member variables are evaluated in the scope - // of the class constructor body but are not permitted to reference parameters or - // local variables of the constructor. This effectively means that entities from outer scopes - // by the same name as a constructor parameter or local variable are inaccessible - // in initializer expressions for instance member variables. - if (ts.isClassLike(location.parent) && !(ts.getModifierFlags(location) & 32 /* Static */)) { - var ctor = findConstructorDeclaration(location.parent); - if (ctor && ctor.locals) { - if (getSymbol(ctor.locals, name, meaning & 107455 /* Value */)) { - // Remember the property node, it will be used later to report appropriate error - propertyWithInvalidInitializer = location; - } - } - } - break; - case 221 /* ClassDeclaration */: - case 192 /* ClassExpression */: - case 222 /* InterfaceDeclaration */: - if (result = getSymbol(getSymbolOfNode(location).members, name, meaning & 793064 /* Type */)) { - if (lastLocation && ts.getModifierFlags(lastLocation) & 32 /* Static */) { - // TypeScript 1.0 spec (April 2014): 3.4.1 - // The scope of a type parameter extends over the entire declaration with which the type - // parameter list is associated, with the exception of static member declarations in classes. - error(errorLocation, ts.Diagnostics.Static_members_cannot_reference_class_type_parameters); - return undefined; - } - break loop; - } - if (location.kind === 192 /* ClassExpression */ && meaning & 32 /* Class */) { - var className = location.name; - if (className && name === className.text) { - result = location.symbol; - break loop; - } - } - break; - // It is not legal to reference a class's own type parameters from a computed property name that - // belongs to the class. For example: - // - // function foo() { return '' } - // class C { // <-- Class's own type parameter T - // [foo()]() { } // <-- Reference to T from class's own computed property - // } - // - case 140 /* ComputedPropertyName */: - grandparent = location.parent.parent; - if (ts.isClassLike(grandparent) || grandparent.kind === 222 /* InterfaceDeclaration */) { - // A reference to this grandparent's type parameters would be an error - if (result = getSymbol(getSymbolOfNode(grandparent).members, name, meaning & 793064 /* Type */)) { - error(errorLocation, ts.Diagnostics.A_computed_property_name_cannot_reference_a_type_parameter_from_its_containing_type); - return undefined; - } - } - break; - case 147 /* MethodDeclaration */: - case 146 /* MethodSignature */: - case 148 /* Constructor */: - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - case 220 /* FunctionDeclaration */: - case 180 /* ArrowFunction */: - if (meaning & 3 /* Variable */ && name === "arguments") { - result = argumentsSymbol; - break loop; - } - break; - case 179 /* FunctionExpression */: - if (meaning & 3 /* Variable */ && name === "arguments") { - result = argumentsSymbol; - break loop; - } - if (meaning & 16 /* Function */) { - var functionName = location.name; - if (functionName && name === functionName.text) { - result = location.symbol; - break loop; - } - } - break; - case 143 /* Decorator */: - // Decorators are resolved at the class declaration. Resolving at the parameter - // or member would result in looking up locals in the method. - // - // function y() {} - // class C { - // method(@y x, y) {} // <-- decorator y should be resolved at the class declaration, not the parameter. - // } - // - if (location.parent && location.parent.kind === 142 /* Parameter */) { - location = location.parent; - } - // - // function y() {} - // class C { - // @y method(x, y) {} // <-- decorator y should be resolved at the class declaration, not the method. - // } - // - if (location.parent && ts.isClassElement(location.parent)) { - location = location.parent; - } - break; - } - lastLocation = location; - location = location.parent; - } - if (result && nameNotFoundMessage && noUnusedIdentifiers) { - result.isReferenced = true; - } - if (!result) { - result = getSymbol(globals, name, meaning); - } - if (!result) { - if (nameNotFoundMessage) { - if (!errorLocation || - !checkAndReportErrorForMissingPrefix(errorLocation, name, nameArg) && - !checkAndReportErrorForExtendingInterface(errorLocation) && - !checkAndReportErrorForUsingTypeAsValue(errorLocation, name, meaning)) { - error(errorLocation, nameNotFoundMessage, typeof nameArg === "string" ? nameArg : ts.declarationNameToString(nameArg)); - } - } - return undefined; - } - // Perform extra checks only if error reporting was requested - if (nameNotFoundMessage) { - if (propertyWithInvalidInitializer) { - // We have a match, but the reference occurred within a property initializer and the identifier also binds - // to a local variable in the constructor where the code will be emitted. - var propertyName = propertyWithInvalidInitializer.name; - error(errorLocation, ts.Diagnostics.Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor, ts.declarationNameToString(propertyName), typeof nameArg === "string" ? nameArg : ts.declarationNameToString(nameArg)); - return undefined; - } - // Only check for block-scoped variable if we are looking for the - // name with variable meaning - // For example, - // declare module foo { - // interface bar {} - // } - // const foo/*1*/: foo/*2*/.bar; - // The foo at /*1*/ and /*2*/ will share same symbol with two meaning - // block - scope variable and namespace module. However, only when we - // try to resolve name in /*1*/ which is used in variable position, - // we want to check for block- scoped - if (meaning & 2 /* BlockScopedVariable */) { - var exportOrLocalSymbol = getExportSymbolOfValueSymbolIfExported(result); - if (exportOrLocalSymbol.flags & 2 /* BlockScopedVariable */) { - checkResolvedBlockScopedVariable(exportOrLocalSymbol, errorLocation); - } - } - // If we're in an external module, we can't reference value symbols created from UMD export declarations - if (result && isInExternalModule && (meaning & 107455 /* Value */) === 107455 /* Value */) { - var decls = result.declarations; - if (decls && decls.length === 1 && decls[0].kind === 228 /* NamespaceExportDeclaration */) { - error(errorLocation, ts.Diagnostics.Identifier_0_must_be_imported_from_a_module, name); - } - } + if (resolvedFileName && host.realpath) { + var originalFileName = resolvedFileName; + resolvedFileName = ts.normalizePath(host.realpath(resolvedFileName)); + if (traceEnabled) { + trace(host, ts.Diagnostics.Resolving_real_path_for_0_result_1, originalFileName, resolvedFileName); } - return result; } - function checkAndReportErrorForMissingPrefix(errorLocation, name, nameArg) { - if ((errorLocation.kind === 69 /* Identifier */ && (isTypeReferenceIdentifier(errorLocation)) || isInTypeQuery(errorLocation))) { - return false; - } - var container = ts.getThisContainer(errorLocation, /* includeArrowFunctions */ true); - var location = container; - while (location) { - if (ts.isClassLike(location.parent)) { - var classSymbol = getSymbolOfNode(location.parent); - if (!classSymbol) { - break; - } - // Check to see if a static member exists. - var constructorType = getTypeOfSymbol(classSymbol); - if (getPropertyOfType(constructorType, name)) { - error(errorLocation, ts.Diagnostics.Cannot_find_name_0_Did_you_mean_the_static_member_1_0, typeof nameArg === "string" ? nameArg : ts.declarationNameToString(nameArg), symbolToString(classSymbol)); - return true; - } - // No static member is present. - // Check if we're in an instance method and look for a relevant instance member. - if (location === container && !(ts.getModifierFlags(location) & 32 /* Static */)) { - var instanceType = getDeclaredTypeOfSymbol(classSymbol).thisType; - if (getPropertyOfType(instanceType, name)) { - error(errorLocation, ts.Diagnostics.Cannot_find_name_0_Did_you_mean_the_instance_member_this_0, typeof nameArg === "string" ? nameArg : ts.declarationNameToString(nameArg)); - return true; - } - } - } - location = location.parent; - } - return false; + return createResolvedModule(resolvedFileName, isExternalLibraryImport, failedLookupLocations); + } + ts.nodeModuleNameResolver = nodeModuleNameResolver; + function nodeLoadModuleByRelativeName(candidate, supportedExtensions, failedLookupLocations, onlyRecordFailures, state) { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Loading_module_as_file_Slash_folder_candidate_module_location_0, candidate); } - function checkAndReportErrorForExtendingInterface(errorLocation) { - var expression = getEntityNameForExtendingInterface(errorLocation); - var isError = !!(expression && resolveEntityName(expression, 64 /* Interface */, /*ignoreErrors*/ true)); - if (isError) { - error(errorLocation, ts.Diagnostics.Cannot_extend_an_interface_0_Did_you_mean_implements, ts.getTextOfNode(expression)); + var resolvedFileName = !ts.pathEndsWithDirectorySeparator(candidate) && loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, onlyRecordFailures, state); + return resolvedFileName || loadNodeModuleFromDirectory(supportedExtensions, candidate, failedLookupLocations, onlyRecordFailures, state); + } + /* @internal */ + function directoryProbablyExists(directoryName, host) { + // if host does not support 'directoryExists' assume that directory will exist + return !host.directoryExists || host.directoryExists(directoryName); + } + ts.directoryProbablyExists = directoryProbablyExists; + /** + * @param {boolean} onlyRecordFailures - if true then function won't try to actually load files but instead record all attempts as failures. This flag is necessary + * in cases when we know upfront that all load attempts will fail (because containing folder does not exists) however we still need to record all failed lookup locations. + */ + function loadModuleFromFile(candidate, extensions, failedLookupLocation, onlyRecordFailures, state) { + // First, try adding an extension. An import of "foo" could be matched by a file "foo.ts", or "foo.js" by "foo.js.ts" + var resolvedByAddingExtension = tryAddingExtensions(candidate, extensions, failedLookupLocation, onlyRecordFailures, state); + if (resolvedByAddingExtension) { + return resolvedByAddingExtension; + } + // If that didn't work, try stripping a ".js" or ".jsx" extension and replacing it with a TypeScript one; + // e.g. "./foo.js" can be matched by "./foo.ts" or "./foo.d.ts" + if (ts.hasJavaScriptFileExtension(candidate)) { + var extensionless = ts.removeFileExtension(candidate); + if (state.traceEnabled) { + var extension = candidate.substring(extensionless.length); + trace(state.host, ts.Diagnostics.File_name_0_has_a_1_extension_stripping_it, candidate, extension); } - return isError; + return tryAddingExtensions(extensionless, extensions, failedLookupLocation, onlyRecordFailures, state); } - /** - * Climbs up parents to an ExpressionWithTypeArguments, and returns its expression, - * but returns undefined if that expression is not an EntityNameExpression. - */ - function getEntityNameForExtendingInterface(node) { - switch (node.kind) { - case 69 /* Identifier */: - case 172 /* PropertyAccessExpression */: - return node.parent ? getEntityNameForExtendingInterface(node.parent) : undefined; - case 194 /* ExpressionWithTypeArguments */: - ts.Debug.assert(ts.isEntityNameExpression(node.expression)); - return node.expression; - default: - return undefined; + } + /** Try to return an existing file that adds one of the `extensions` to `candidate`. */ + function tryAddingExtensions(candidate, extensions, failedLookupLocation, onlyRecordFailures, state) { + if (!onlyRecordFailures) { + // check if containing folder exists - if it doesn't then just record failures for all supported extensions without disk probing + var directory = ts.getDirectoryPath(candidate); + if (directory) { + onlyRecordFailures = !directoryProbablyExists(directory, state.host); } } - function checkAndReportErrorForUsingTypeAsValue(errorLocation, name, meaning) { - if (meaning & (107455 /* Value */ & ~1024 /* NamespaceModule */)) { - var symbol = resolveSymbol(resolveName(errorLocation, name, 793064 /* Type */ & ~107455 /* Value */, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined)); - if (symbol && !(symbol.flags & 1024 /* NamespaceModule */)) { - error(errorLocation, ts.Diagnostics._0_only_refers_to_a_type_but_is_being_used_as_a_value_here, name); - return true; - } + return ts.forEach(extensions, function (ext) { + return !(state.skipTsx && ts.isJsxOrTsxExtension(ext)) && tryFile(candidate + ext, failedLookupLocation, onlyRecordFailures, state); + }); + } + /** Return the file if it exists. */ + function tryFile(fileName, failedLookupLocation, onlyRecordFailures, state) { + if (!onlyRecordFailures && state.host.fileExists(fileName)) { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.File_0_exist_use_it_as_a_name_resolution_result, fileName); } - return false; + return fileName; } - function checkResolvedBlockScopedVariable(result, errorLocation) { - ts.Debug.assert((result.flags & 2 /* BlockScopedVariable */) !== 0); - // Block-scoped variables cannot be used before their definition - var declaration = ts.forEach(result.declarations, function (d) { return ts.isBlockOrCatchScoped(d) ? d : undefined; }); - ts.Debug.assert(declaration !== undefined, "Block-scoped variable declaration is undefined"); - if (!ts.isInAmbientContext(declaration) && !isBlockScopedNameDeclaredBeforeUse(ts.getAncestor(declaration, 218 /* VariableDeclaration */), errorLocation)) { - error(errorLocation, ts.Diagnostics.Block_scoped_variable_0_used_before_its_declaration, ts.declarationNameToString(declaration.name)); + else { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.File_0_does_not_exist, fileName); } + failedLookupLocation.push(fileName); + return undefined; } - /* Starting from 'initial' node walk up the parent chain until 'stopAt' node is reached. - * If at any point current node is equal to 'parent' node - return true. - * Return false if 'stopAt' node is reached or isFunctionLike(current) === true. - */ - function isSameScopeDescendentOf(initial, parent, stopAt) { - if (!parent) { - return false; + } + function loadNodeModuleFromDirectory(extensions, candidate, failedLookupLocation, onlyRecordFailures, state) { + var packageJsonPath = pathToPackageJson(candidate); + var directoryExists = !onlyRecordFailures && directoryProbablyExists(candidate, state.host); + if (directoryExists && state.host.fileExists(packageJsonPath)) { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Found_package_json_at_0, packageJsonPath); } - for (var current = initial; current && current !== stopAt && !ts.isFunctionLike(current); current = current.parent) { - if (current === parent) { - return true; + var typesFile = tryReadTypesSection(packageJsonPath, candidate, state); + if (typesFile) { + var onlyRecordFailures_1 = !directoryProbablyExists(ts.getDirectoryPath(typesFile), state.host); + // A package.json "typings" may specify an exact filename, or may choose to omit an extension. + var result = tryFile(typesFile, failedLookupLocation, onlyRecordFailures_1, state) || + tryAddingExtensions(typesFile, extensions, failedLookupLocation, onlyRecordFailures_1, state); + if (result) { + return result; } } - return false; - } - function getAnyImportSyntax(node) { - if (ts.isAliasSymbolDeclaration(node)) { - if (node.kind === 229 /* ImportEqualsDeclaration */) { - return node; - } - while (node && node.kind !== 230 /* ImportDeclaration */) { - node = node.parent; + else { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.package_json_does_not_have_types_field); } - return node; } } - function getDeclarationOfAliasSymbol(symbol) { - return ts.forEach(symbol.declarations, function (d) { return ts.isAliasSymbolDeclaration(d) ? d : undefined; }); - } - function getTargetOfImportEqualsDeclaration(node) { - if (node.moduleReference.kind === 240 /* ExternalModuleReference */) { - return resolveExternalModuleSymbol(resolveExternalModuleName(node, ts.getExternalModuleImportEqualsDeclarationExpression(node))); + else { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.File_0_does_not_exist, packageJsonPath); } - return getSymbolOfPartOfRightHandSideOfImportEquals(node.moduleReference, node); + // record package json as one of failed lookup locations - in the future if this file will appear it will invalidate resolution results + failedLookupLocation.push(packageJsonPath); } - function getTargetOfImportClause(node) { - var moduleSymbol = resolveExternalModuleName(node, node.parent.moduleSpecifier); - if (moduleSymbol) { - var exportDefaultSymbol = ts.isShorthandAmbientModuleSymbol(moduleSymbol) ? - moduleSymbol : - moduleSymbol.exports["export="] ? - getPropertyOfType(getTypeOfSymbol(moduleSymbol.exports["export="]), "default") : - resolveSymbol(moduleSymbol.exports["default"]); - if (!exportDefaultSymbol && !allowSyntheticDefaultImports) { - error(node.name, ts.Diagnostics.Module_0_has_no_default_export, symbolToString(moduleSymbol)); + return loadModuleFromFile(ts.combinePaths(candidate, "index"), extensions, failedLookupLocation, !directoryExists, state); + } + function pathToPackageJson(directory) { + return ts.combinePaths(directory, "package.json"); + } + function loadModuleFromNodeModulesFolder(moduleName, directory, failedLookupLocations, state) { + var nodeModulesFolder = ts.combinePaths(directory, "node_modules"); + var nodeModulesFolderExists = directoryProbablyExists(nodeModulesFolder, state.host); + var candidate = ts.normalizePath(ts.combinePaths(nodeModulesFolder, moduleName)); + var supportedExtensions = ts.getSupportedExtensions(state.compilerOptions); + var result = loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, !nodeModulesFolderExists, state); + if (result) { + return result; + } + result = loadNodeModuleFromDirectory(supportedExtensions, candidate, failedLookupLocations, !nodeModulesFolderExists, state); + if (result) { + return result; + } + } + /* @internal */ + function loadModuleFromNodeModules(moduleName, directory, failedLookupLocations, state, checkOneLevel) { + return loadModuleFromNodeModulesWorker(moduleName, directory, failedLookupLocations, state, checkOneLevel, /*typesOnly*/ false); + } + ts.loadModuleFromNodeModules = loadModuleFromNodeModules; + function loadModuleFromNodeModulesAtTypes(moduleName, directory, failedLookupLocations, state) { + return loadModuleFromNodeModulesWorker(moduleName, directory, failedLookupLocations, state, /*checkOneLevel*/ false, /*typesOnly*/ true); + } + function loadModuleFromNodeModulesWorker(moduleName, directory, failedLookupLocations, state, checkOneLevel, typesOnly) { + directory = ts.normalizeSlashes(directory); + while (true) { + var baseName = ts.getBaseFileName(directory); + if (baseName !== "node_modules") { + var packageResult = void 0; + if (!typesOnly) { + // Try to load source from the package + packageResult = loadModuleFromNodeModulesFolder(moduleName, directory, failedLookupLocations, state); + if (packageResult && ts.hasTypeScriptFileExtension(packageResult)) { + // Always prefer a TypeScript (.ts, .tsx, .d.ts) file shipped with the package + return packageResult; + } } - else if (!exportDefaultSymbol && allowSyntheticDefaultImports) { - return resolveExternalModuleSymbol(moduleSymbol) || resolveSymbol(moduleSymbol); + // Else prefer a types package over non-TypeScript results (e.g. JavaScript files) + var typesResult = loadModuleFromNodeModulesFolder(ts.combinePaths("@types", moduleName), directory, failedLookupLocations, state); + if (typesResult || packageResult) { + return typesResult || packageResult; } - return exportDefaultSymbol; } + var parentPath = ts.getDirectoryPath(directory); + if (parentPath === directory || checkOneLevel) { + break; + } + directory = parentPath; } - function getTargetOfNamespaceImport(node) { - var moduleSpecifier = node.parent.parent.moduleSpecifier; - return resolveESModuleSymbol(resolveExternalModuleName(node, moduleSpecifier), moduleSpecifier); + return undefined; + } + function classicNameResolver(moduleName, containingFile, compilerOptions, host) { + var traceEnabled = isTraceEnabled(compilerOptions, host); + var state = { compilerOptions: compilerOptions, host: host, traceEnabled: traceEnabled, skipTsx: !compilerOptions.jsx }; + var failedLookupLocations = []; + var supportedExtensions = ts.getSupportedExtensions(compilerOptions); + var containingDirectory = ts.getDirectoryPath(containingFile); + var resolvedFileName = tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, loadModuleFromFile, failedLookupLocations, supportedExtensions, state); + if (resolvedFileName) { + return createResolvedModule(resolvedFileName, /*isExternalLibraryImport*/ false, failedLookupLocations); } - // This function creates a synthetic symbol that combines the value side of one symbol with the - // type/namespace side of another symbol. Consider this example: - // - // declare module graphics { - // interface Point { - // x: number; - // y: number; - // } - // } - // declare var graphics: { - // Point: new (x: number, y: number) => graphics.Point; - // } - // declare module "graphics" { - // export = graphics; - // } - // - // An 'import { Point } from "graphics"' needs to create a symbol that combines the value side 'Point' - // property with the type/namespace side interface 'Point'. - function combineValueAndTypeSymbols(valueSymbol, typeSymbol) { - if (valueSymbol.flags & (793064 /* Type */ | 1920 /* Namespace */)) { - return valueSymbol; - } - var result = createSymbol(valueSymbol.flags | typeSymbol.flags, valueSymbol.name); - result.declarations = ts.concatenate(valueSymbol.declarations, typeSymbol.declarations); - result.parent = valueSymbol.parent || typeSymbol.parent; - if (valueSymbol.valueDeclaration) - result.valueDeclaration = valueSymbol.valueDeclaration; - if (typeSymbol.members) - result.members = typeSymbol.members; - if (valueSymbol.exports) - result.exports = valueSymbol.exports; - return result; + var referencedSourceFile; + if (moduleHasNonRelativeName(moduleName)) { + referencedSourceFile = referencedSourceFile = loadModuleFromAncestorDirectories(moduleName, containingDirectory, supportedExtensions, failedLookupLocations, state) || + // If we didn't find the file normally, look it up in @types. + loadModuleFromNodeModulesAtTypes(moduleName, containingDirectory, failedLookupLocations, state); } - function getExportOfModule(symbol, name) { - if (symbol.flags & 1536 /* Module */) { - var exportedSymbol = getExportsOfSymbol(symbol)[name]; - if (exportedSymbol) { - return resolveSymbol(exportedSymbol); - } - } + else { + var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); + referencedSourceFile = loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, /*onlyRecordFailures*/ false, state); } - function getPropertyOfVariable(symbol, name) { - if (symbol.flags & 3 /* Variable */) { - var typeAnnotation = symbol.valueDeclaration.type; - if (typeAnnotation) { - return resolveSymbol(getPropertyOfType(getTypeFromTypeNode(typeAnnotation), name)); - } + return referencedSourceFile + ? { resolvedModule: { resolvedFileName: referencedSourceFile }, failedLookupLocations: failedLookupLocations } + : { resolvedModule: undefined, failedLookupLocations: failedLookupLocations }; + } + ts.classicNameResolver = classicNameResolver; + /** Climb up parent directories looking for a module. */ + function loadModuleFromAncestorDirectories(moduleName, containingDirectory, supportedExtensions, failedLookupLocations, state) { + while (true) { + var searchName = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); + var referencedSourceFile = loadModuleFromFile(searchName, supportedExtensions, failedLookupLocations, /*onlyRecordFailures*/ false, state); + if (referencedSourceFile) { + return referencedSourceFile; + } + var parentPath = ts.getDirectoryPath(containingDirectory); + if (parentPath === containingDirectory) { + return undefined; } + containingDirectory = parentPath; } - function getExternalModuleMember(node, specifier) { - var moduleSymbol = resolveExternalModuleName(node, node.moduleSpecifier); - var targetSymbol = resolveESModuleSymbol(moduleSymbol, node.moduleSpecifier); - if (targetSymbol) { - var name_13 = specifier.propertyName || specifier.name; - if (name_13.text) { - if (ts.isShorthandAmbientModuleSymbol(moduleSymbol)) { - return moduleSymbol; - } - var symbolFromVariable = void 0; - // First check if module was specified with "export=". If so, get the member from the resolved type - if (moduleSymbol && moduleSymbol.exports && moduleSymbol.exports["export="]) { - symbolFromVariable = getPropertyOfType(getTypeOfSymbol(targetSymbol), name_13.text); - } - else { - symbolFromVariable = getPropertyOfVariable(targetSymbol, name_13.text); - } - // if symbolFromVariable is export - get its final target - symbolFromVariable = resolveSymbol(symbolFromVariable); - var symbolFromModule = getExportOfModule(targetSymbol, name_13.text); - // If the export member we're looking for is default, and there is no real default but allowSyntheticDefaultImports is on, return the entire module as the default - if (!symbolFromModule && allowSyntheticDefaultImports && name_13.text === "default") { - symbolFromModule = resolveExternalModuleSymbol(moduleSymbol) || resolveSymbol(moduleSymbol); - } - var symbol = symbolFromModule && symbolFromVariable ? - combineValueAndTypeSymbols(symbolFromVariable, symbolFromModule) : - symbolFromModule || symbolFromVariable; - if (!symbol) { - error(name_13, ts.Diagnostics.Module_0_has_no_exported_member_1, getFullyQualifiedName(moduleSymbol), ts.declarationNameToString(name_13)); - } - return symbol; - } - } + } +})(ts || (ts = {})); +/// +/// +/* @internal */ +var ts; +(function (ts) { + var ambientModuleSymbolRegex = /^".+"$/; + var nextSymbolId = 1; + var nextNodeId = 1; + var nextMergeId = 1; + var nextFlowId = 1; + function getNodeId(node) { + if (!node.id) { + node.id = nextNodeId; + nextNodeId++; } - function getTargetOfImportSpecifier(node) { - return getExternalModuleMember(node.parent.parent.parent, node); + return node.id; + } + ts.getNodeId = getNodeId; + function getSymbolId(symbol) { + if (!symbol.id) { + symbol.id = nextSymbolId; + nextSymbolId++; } - function getTargetOfNamespaceExportDeclaration(node) { - return resolveExternalModuleSymbol(node.parent.symbol); + return symbol.id; + } + ts.getSymbolId = getSymbolId; + function createTypeChecker(host, produceDiagnostics) { + // Cancellation that controls whether or not we can cancel in the middle of type checking. + // In general cancelling is *not* safe for the type checker. We might be in the middle of + // computing something, and we will leave our internals in an inconsistent state. Callers + // who set the cancellation token should catch if a cancellation exception occurs, and + // should throw away and create a new TypeChecker. + // + // Currently we only support setting the cancellation token when getting diagnostics. This + // is because diagnostics can be quite expensive, and we want to allow hosts to bail out if + // they no longer need the information (for example, if the user started editing again). + var cancellationToken; + var Symbol = ts.objectAllocator.getSymbolConstructor(); + var Type = ts.objectAllocator.getTypeConstructor(); + var Signature = ts.objectAllocator.getSignatureConstructor(); + var typeCount = 0; + var symbolCount = 0; + var emptyArray = []; + var emptySymbols = ts.createMap(); + var compilerOptions = host.getCompilerOptions(); + var languageVersion = compilerOptions.target || 0 /* ES3 */; + var modulekind = ts.getEmitModuleKind(compilerOptions); + var noUnusedIdentifiers = !!compilerOptions.noUnusedLocals || !!compilerOptions.noUnusedParameters; + var allowSyntheticDefaultImports = typeof compilerOptions.allowSyntheticDefaultImports !== "undefined" ? compilerOptions.allowSyntheticDefaultImports : modulekind === ts.ModuleKind.System; + var strictNullChecks = compilerOptions.strictNullChecks; + var emitResolver = createResolver(); + var undefinedSymbol = createSymbol(4 /* Property */ | 67108864 /* Transient */, "undefined"); + undefinedSymbol.declarations = []; + var argumentsSymbol = createSymbol(4 /* Property */ | 67108864 /* Transient */, "arguments"); + var checker = { + getNodeCount: function () { return ts.sum(host.getSourceFiles(), "nodeCount"); }, + getIdentifierCount: function () { return ts.sum(host.getSourceFiles(), "identifierCount"); }, + getSymbolCount: function () { return ts.sum(host.getSourceFiles(), "symbolCount") + symbolCount; }, + getTypeCount: function () { return typeCount; }, + isUndefinedSymbol: function (symbol) { return symbol === undefinedSymbol; }, + isArgumentsSymbol: function (symbol) { return symbol === argumentsSymbol; }, + isUnknownSymbol: function (symbol) { return symbol === unknownSymbol; }, + getDiagnostics: getDiagnostics, + getGlobalDiagnostics: getGlobalDiagnostics, + getTypeOfSymbolAtLocation: getTypeOfSymbolAtLocation, + getSymbolsOfParameterPropertyDeclaration: getSymbolsOfParameterPropertyDeclaration, + getDeclaredTypeOfSymbol: getDeclaredTypeOfSymbol, + getPropertiesOfType: getPropertiesOfType, + getPropertyOfType: getPropertyOfType, + getSignaturesOfType: getSignaturesOfType, + getIndexTypeOfType: getIndexTypeOfType, + getBaseTypes: getBaseTypes, + getReturnTypeOfSignature: getReturnTypeOfSignature, + getNonNullableType: getNonNullableType, + getSymbolsInScope: getSymbolsInScope, + getSymbolAtLocation: getSymbolAtLocation, + getShorthandAssignmentValueSymbol: getShorthandAssignmentValueSymbol, + getExportSpecifierLocalTargetSymbol: getExportSpecifierLocalTargetSymbol, + getTypeAtLocation: getTypeOfNode, + getPropertySymbolOfDestructuringAssignment: getPropertySymbolOfDestructuringAssignment, + typeToString: typeToString, + getSymbolDisplayBuilder: getSymbolDisplayBuilder, + symbolToString: symbolToString, + getAugmentedPropertiesOfType: getAugmentedPropertiesOfType, + getRootSymbols: getRootSymbols, + getContextualType: getContextualType, + getFullyQualifiedName: getFullyQualifiedName, + getResolvedSignature: getResolvedSignature, + getConstantValue: getConstantValue, + isValidPropertyAccess: isValidPropertyAccess, + getSignatureFromDeclaration: getSignatureFromDeclaration, + isImplementationOfOverload: isImplementationOfOverload, + getAliasedSymbol: resolveAlias, + getEmitResolver: getEmitResolver, + getExportsOfModule: getExportsOfModuleAsArray, + getAmbientModules: getAmbientModules, + getJsxElementAttributesType: getJsxElementAttributesType, + getJsxIntrinsicTagNames: getJsxIntrinsicTagNames, + isOptionalParameter: isOptionalParameter + }; + var tupleTypes = []; + var unionTypes = ts.createMap(); + var intersectionTypes = ts.createMap(); + var stringLiteralTypes = ts.createMap(); + var numericLiteralTypes = ts.createMap(); + var unknownSymbol = createSymbol(4 /* Property */ | 67108864 /* Transient */, "unknown"); + var resolvingSymbol = createSymbol(67108864 /* Transient */, "__resolving__"); + var anyType = createIntrinsicType(1 /* Any */, "any"); + var autoType = createIntrinsicType(1 /* Any */, "any"); + var unknownType = createIntrinsicType(1 /* Any */, "unknown"); + var undefinedType = createIntrinsicType(2048 /* Undefined */, "undefined"); + var undefinedWideningType = strictNullChecks ? undefinedType : createIntrinsicType(2048 /* Undefined */ | 33554432 /* ContainsWideningType */, "undefined"); + var nullType = createIntrinsicType(4096 /* Null */, "null"); + var nullWideningType = strictNullChecks ? nullType : createIntrinsicType(4096 /* Null */ | 33554432 /* ContainsWideningType */, "null"); + var stringType = createIntrinsicType(2 /* String */, "string"); + var numberType = createIntrinsicType(4 /* Number */, "number"); + var trueType = createIntrinsicType(128 /* BooleanLiteral */, "true"); + var falseType = createIntrinsicType(128 /* BooleanLiteral */, "false"); + var booleanType = createBooleanType([trueType, falseType]); + var esSymbolType = createIntrinsicType(512 /* ESSymbol */, "symbol"); + var voidType = createIntrinsicType(1024 /* Void */, "void"); + var neverType = createIntrinsicType(8192 /* Never */, "never"); + var silentNeverType = createIntrinsicType(8192 /* Never */, "never"); + var emptyObjectType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); + var emptyGenericType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); + emptyGenericType.instantiations = ts.createMap(); + var anyFunctionType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); + // The anyFunctionType contains the anyFunctionType by definition. The flag is further propagated + // in getPropagatingFlagsOfTypes, and it is checked in inferFromTypes. + anyFunctionType.flags |= 134217728 /* ContainsAnyFunctionType */; + var noConstraintType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); + var anySignature = createSignature(undefined, undefined, undefined, emptyArray, anyType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false); + var unknownSignature = createSignature(undefined, undefined, undefined, emptyArray, unknownType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false); + var resolvingSignature = createSignature(undefined, undefined, undefined, emptyArray, anyType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false); + var silentNeverSignature = createSignature(undefined, undefined, undefined, emptyArray, silentNeverType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false); + var enumNumberIndexInfo = createIndexInfo(stringType, /*isReadonly*/ true); + var globals = ts.createMap(); + /** + * List of every ambient module with a "*" wildcard. + * Unlike other ambient modules, these can't be stored in `globals` because symbol tables only deal with exact matches. + * This is only used if there is no exact match. + */ + var patternAmbientModules; + var getGlobalESSymbolConstructorSymbol; + var getGlobalPromiseConstructorSymbol; + var tryGetGlobalPromiseConstructorSymbol; + var globalObjectType; + var globalFunctionType; + var globalArrayType; + var globalReadonlyArrayType; + var globalStringType; + var globalNumberType; + var globalBooleanType; + var globalRegExpType; + var anyArrayType; + var anyReadonlyArrayType; + // The library files are only loaded when the feature is used. + // This allows users to just specify library files they want to used through --lib + // and they will not get an error from not having unrelated library files + var getGlobalTemplateStringsArrayType; + var getGlobalESSymbolType; + var getGlobalIterableType; + var getGlobalIteratorType; + var getGlobalIterableIteratorType; + var getGlobalClassDecoratorType; + var getGlobalParameterDecoratorType; + var getGlobalPropertyDecoratorType; + var getGlobalMethodDecoratorType; + var getGlobalTypedPropertyDescriptorType; + var getGlobalPromiseType; + var tryGetGlobalPromiseType; + var getGlobalPromiseLikeType; + var getInstantiatedGlobalPromiseLikeType; + var getGlobalPromiseConstructorLikeType; + var getGlobalThenableType; + var jsxElementClassType; + var deferredNodes; + var deferredUnusedIdentifierNodes; + var flowLoopStart = 0; + var flowLoopCount = 0; + var visitedFlowCount = 0; + var emptyStringType = getLiteralTypeForText(32 /* StringLiteral */, ""); + var zeroType = getLiteralTypeForText(64 /* NumberLiteral */, "0"); + var resolutionTargets = []; + var resolutionResults = []; + var resolutionPropertyNames = []; + var mergedSymbols = []; + var symbolLinks = []; + var nodeLinks = []; + var flowLoopCaches = []; + var flowLoopNodes = []; + var flowLoopKeys = []; + var flowLoopTypes = []; + var visitedFlowNodes = []; + var visitedFlowTypes = []; + var potentialThisCollisions = []; + var awaitedTypeStack = []; + var diagnostics = ts.createDiagnosticCollection(); + var TypeFacts; + (function (TypeFacts) { + TypeFacts[TypeFacts["None"] = 0] = "None"; + TypeFacts[TypeFacts["TypeofEQString"] = 1] = "TypeofEQString"; + TypeFacts[TypeFacts["TypeofEQNumber"] = 2] = "TypeofEQNumber"; + TypeFacts[TypeFacts["TypeofEQBoolean"] = 4] = "TypeofEQBoolean"; + TypeFacts[TypeFacts["TypeofEQSymbol"] = 8] = "TypeofEQSymbol"; + TypeFacts[TypeFacts["TypeofEQObject"] = 16] = "TypeofEQObject"; + TypeFacts[TypeFacts["TypeofEQFunction"] = 32] = "TypeofEQFunction"; + TypeFacts[TypeFacts["TypeofEQHostObject"] = 64] = "TypeofEQHostObject"; + TypeFacts[TypeFacts["TypeofNEString"] = 128] = "TypeofNEString"; + TypeFacts[TypeFacts["TypeofNENumber"] = 256] = "TypeofNENumber"; + TypeFacts[TypeFacts["TypeofNEBoolean"] = 512] = "TypeofNEBoolean"; + TypeFacts[TypeFacts["TypeofNESymbol"] = 1024] = "TypeofNESymbol"; + TypeFacts[TypeFacts["TypeofNEObject"] = 2048] = "TypeofNEObject"; + TypeFacts[TypeFacts["TypeofNEFunction"] = 4096] = "TypeofNEFunction"; + TypeFacts[TypeFacts["TypeofNEHostObject"] = 8192] = "TypeofNEHostObject"; + TypeFacts[TypeFacts["EQUndefined"] = 16384] = "EQUndefined"; + TypeFacts[TypeFacts["EQNull"] = 32768] = "EQNull"; + TypeFacts[TypeFacts["EQUndefinedOrNull"] = 65536] = "EQUndefinedOrNull"; + TypeFacts[TypeFacts["NEUndefined"] = 131072] = "NEUndefined"; + TypeFacts[TypeFacts["NENull"] = 262144] = "NENull"; + TypeFacts[TypeFacts["NEUndefinedOrNull"] = 524288] = "NEUndefinedOrNull"; + TypeFacts[TypeFacts["Truthy"] = 1048576] = "Truthy"; + TypeFacts[TypeFacts["Falsy"] = 2097152] = "Falsy"; + TypeFacts[TypeFacts["Discriminatable"] = 4194304] = "Discriminatable"; + TypeFacts[TypeFacts["All"] = 8388607] = "All"; + // The following members encode facts about particular kinds of types for use in the getTypeFacts function. + // The presence of a particular fact means that the given test is true for some (and possibly all) values + // of that kind of type. + TypeFacts[TypeFacts["BaseStringStrictFacts"] = 933633] = "BaseStringStrictFacts"; + TypeFacts[TypeFacts["BaseStringFacts"] = 3145473] = "BaseStringFacts"; + TypeFacts[TypeFacts["StringStrictFacts"] = 4079361] = "StringStrictFacts"; + TypeFacts[TypeFacts["StringFacts"] = 4194049] = "StringFacts"; + TypeFacts[TypeFacts["EmptyStringStrictFacts"] = 3030785] = "EmptyStringStrictFacts"; + TypeFacts[TypeFacts["EmptyStringFacts"] = 3145473] = "EmptyStringFacts"; + TypeFacts[TypeFacts["NonEmptyStringStrictFacts"] = 1982209] = "NonEmptyStringStrictFacts"; + TypeFacts[TypeFacts["NonEmptyStringFacts"] = 4194049] = "NonEmptyStringFacts"; + TypeFacts[TypeFacts["BaseNumberStrictFacts"] = 933506] = "BaseNumberStrictFacts"; + TypeFacts[TypeFacts["BaseNumberFacts"] = 3145346] = "BaseNumberFacts"; + TypeFacts[TypeFacts["NumberStrictFacts"] = 4079234] = "NumberStrictFacts"; + TypeFacts[TypeFacts["NumberFacts"] = 4193922] = "NumberFacts"; + TypeFacts[TypeFacts["ZeroStrictFacts"] = 3030658] = "ZeroStrictFacts"; + TypeFacts[TypeFacts["ZeroFacts"] = 3145346] = "ZeroFacts"; + TypeFacts[TypeFacts["NonZeroStrictFacts"] = 1982082] = "NonZeroStrictFacts"; + TypeFacts[TypeFacts["NonZeroFacts"] = 4193922] = "NonZeroFacts"; + TypeFacts[TypeFacts["BaseBooleanStrictFacts"] = 933252] = "BaseBooleanStrictFacts"; + TypeFacts[TypeFacts["BaseBooleanFacts"] = 3145092] = "BaseBooleanFacts"; + TypeFacts[TypeFacts["BooleanStrictFacts"] = 4078980] = "BooleanStrictFacts"; + TypeFacts[TypeFacts["BooleanFacts"] = 4193668] = "BooleanFacts"; + TypeFacts[TypeFacts["FalseStrictFacts"] = 3030404] = "FalseStrictFacts"; + TypeFacts[TypeFacts["FalseFacts"] = 3145092] = "FalseFacts"; + TypeFacts[TypeFacts["TrueStrictFacts"] = 1981828] = "TrueStrictFacts"; + TypeFacts[TypeFacts["TrueFacts"] = 4193668] = "TrueFacts"; + TypeFacts[TypeFacts["SymbolStrictFacts"] = 1981320] = "SymbolStrictFacts"; + TypeFacts[TypeFacts["SymbolFacts"] = 4193160] = "SymbolFacts"; + TypeFacts[TypeFacts["ObjectStrictFacts"] = 6166480] = "ObjectStrictFacts"; + TypeFacts[TypeFacts["ObjectFacts"] = 8378320] = "ObjectFacts"; + TypeFacts[TypeFacts["FunctionStrictFacts"] = 6164448] = "FunctionStrictFacts"; + TypeFacts[TypeFacts["FunctionFacts"] = 8376288] = "FunctionFacts"; + TypeFacts[TypeFacts["UndefinedFacts"] = 2457472] = "UndefinedFacts"; + TypeFacts[TypeFacts["NullFacts"] = 2340752] = "NullFacts"; + })(TypeFacts || (TypeFacts = {})); + var typeofEQFacts = ts.createMap({ + "string": 1 /* TypeofEQString */, + "number": 2 /* TypeofEQNumber */, + "boolean": 4 /* TypeofEQBoolean */, + "symbol": 8 /* TypeofEQSymbol */, + "undefined": 16384 /* EQUndefined */, + "object": 16 /* TypeofEQObject */, + "function": 32 /* TypeofEQFunction */ + }); + var typeofNEFacts = ts.createMap({ + "string": 128 /* TypeofNEString */, + "number": 256 /* TypeofNENumber */, + "boolean": 512 /* TypeofNEBoolean */, + "symbol": 1024 /* TypeofNESymbol */, + "undefined": 131072 /* NEUndefined */, + "object": 2048 /* TypeofNEObject */, + "function": 4096 /* TypeofNEFunction */ + }); + var typeofTypesByName = ts.createMap({ + "string": stringType, + "number": numberType, + "boolean": booleanType, + "symbol": esSymbolType, + "undefined": undefinedType + }); + var jsxElementType; + /** Things we lazy load from the JSX namespace */ + var jsxTypes = ts.createMap(); + var JsxNames = { + JSX: "JSX", + IntrinsicElements: "IntrinsicElements", + ElementClass: "ElementClass", + ElementAttributesPropertyNameContainer: "ElementAttributesProperty", + Element: "Element", + IntrinsicAttributes: "IntrinsicAttributes", + IntrinsicClassAttributes: "IntrinsicClassAttributes" + }; + var subtypeRelation = ts.createMap(); + var assignableRelation = ts.createMap(); + var comparableRelation = ts.createMap(); + var identityRelation = ts.createMap(); + var enumRelation = ts.createMap(); + // This is for caching the result of getSymbolDisplayBuilder. Do not access directly. + var _displayBuilder; + var TypeSystemPropertyName; + (function (TypeSystemPropertyName) { + TypeSystemPropertyName[TypeSystemPropertyName["Type"] = 0] = "Type"; + TypeSystemPropertyName[TypeSystemPropertyName["ResolvedBaseConstructorType"] = 1] = "ResolvedBaseConstructorType"; + TypeSystemPropertyName[TypeSystemPropertyName["DeclaredType"] = 2] = "DeclaredType"; + TypeSystemPropertyName[TypeSystemPropertyName["ResolvedReturnType"] = 3] = "ResolvedReturnType"; + })(TypeSystemPropertyName || (TypeSystemPropertyName = {})); + var builtinGlobals = ts.createMap(); + builtinGlobals[undefinedSymbol.name] = undefinedSymbol; + initializeTypeChecker(); + return checker; + function getEmitResolver(sourceFile, cancellationToken) { + // Ensure we have all the type information in place for this file so that all the + // emitter questions of this resolver will return the right information. + getDiagnostics(sourceFile, cancellationToken); + return emitResolver; } - function getTargetOfExportSpecifier(node) { - return node.parent.parent.moduleSpecifier ? - getExternalModuleMember(node.parent.parent, node) : - resolveEntityName(node.propertyName || node.name, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */); + function error(location, message, arg0, arg1, arg2) { + var diagnostic = location + ? ts.createDiagnosticForNode(location, message, arg0, arg1, arg2) + : ts.createCompilerDiagnostic(message, arg0, arg1, arg2); + diagnostics.add(diagnostic); } - function getTargetOfExportAssignment(node) { - return resolveEntityName(node.expression, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */); + function createSymbol(flags, name) { + symbolCount++; + return new Symbol(flags, name); } - function getTargetOfAliasDeclaration(node) { - switch (node.kind) { - case 229 /* ImportEqualsDeclaration */: - return getTargetOfImportEqualsDeclaration(node); - case 231 /* ImportClause */: - return getTargetOfImportClause(node); - case 232 /* NamespaceImport */: - return getTargetOfNamespaceImport(node); - case 234 /* ImportSpecifier */: - return getTargetOfImportSpecifier(node); - case 238 /* ExportSpecifier */: - return getTargetOfExportSpecifier(node); - case 235 /* ExportAssignment */: - return getTargetOfExportAssignment(node); - case 228 /* NamespaceExportDeclaration */: - return getTargetOfNamespaceExportDeclaration(node); + function getExcludedSymbolFlags(flags) { + var result = 0; + if (flags & 2 /* BlockScopedVariable */) + result |= 107455 /* BlockScopedVariableExcludes */; + if (flags & 1 /* FunctionScopedVariable */) + result |= 107454 /* FunctionScopedVariableExcludes */; + if (flags & 4 /* Property */) + result |= 0 /* PropertyExcludes */; + if (flags & 8 /* EnumMember */) + result |= 900095 /* EnumMemberExcludes */; + if (flags & 16 /* Function */) + result |= 106927 /* FunctionExcludes */; + if (flags & 32 /* Class */) + result |= 899519 /* ClassExcludes */; + if (flags & 64 /* Interface */) + result |= 792968 /* InterfaceExcludes */; + if (flags & 256 /* RegularEnum */) + result |= 899327 /* RegularEnumExcludes */; + if (flags & 128 /* ConstEnum */) + result |= 899967 /* ConstEnumExcludes */; + if (flags & 512 /* ValueModule */) + result |= 106639 /* ValueModuleExcludes */; + if (flags & 8192 /* Method */) + result |= 99263 /* MethodExcludes */; + if (flags & 32768 /* GetAccessor */) + result |= 41919 /* GetAccessorExcludes */; + if (flags & 65536 /* SetAccessor */) + result |= 74687 /* SetAccessorExcludes */; + if (flags & 262144 /* TypeParameter */) + result |= 530920 /* TypeParameterExcludes */; + if (flags & 524288 /* TypeAlias */) + result |= 793064 /* TypeAliasExcludes */; + if (flags & 8388608 /* Alias */) + result |= 8388608 /* AliasExcludes */; + return result; + } + function recordMergedSymbol(target, source) { + if (!source.mergeId) { + source.mergeId = nextMergeId; + nextMergeId++; } + mergedSymbols[source.mergeId] = target; } - function resolveSymbol(symbol) { - return symbol && symbol.flags & 8388608 /* Alias */ && !(symbol.flags & (107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */)) ? resolveAlias(symbol) : symbol; + function cloneSymbol(symbol) { + var result = createSymbol(symbol.flags | 33554432 /* Merged */, symbol.name); + result.declarations = symbol.declarations.slice(0); + result.parent = symbol.parent; + if (symbol.valueDeclaration) + result.valueDeclaration = symbol.valueDeclaration; + if (symbol.constEnumOnlyModule) + result.constEnumOnlyModule = true; + if (symbol.members) + result.members = ts.cloneMap(symbol.members); + if (symbol.exports) + result.exports = ts.cloneMap(symbol.exports); + recordMergedSymbol(result, symbol); + return result; } - function resolveAlias(symbol) { - ts.Debug.assert((symbol.flags & 8388608 /* Alias */) !== 0, "Should only get Alias here."); - var links = getSymbolLinks(symbol); - if (!links.target) { - links.target = resolvingSymbol; - var node = getDeclarationOfAliasSymbol(symbol); - ts.Debug.assert(!!node); - var target = getTargetOfAliasDeclaration(node); - if (links.target === resolvingSymbol) { - links.target = target || unknownSymbol; + function mergeSymbol(target, source) { + if (!(target.flags & getExcludedSymbolFlags(source.flags))) { + if (source.flags & 512 /* ValueModule */ && target.flags & 512 /* ValueModule */ && target.constEnumOnlyModule && !source.constEnumOnlyModule) { + // reset flag when merging instantiated module into value module that has only const enums + target.constEnumOnlyModule = false; } - else { - error(node, ts.Diagnostics.Circular_definition_of_import_alias_0, symbolToString(symbol)); + target.flags |= source.flags; + if (source.valueDeclaration && + (!target.valueDeclaration || + (target.valueDeclaration.kind === 225 /* ModuleDeclaration */ && source.valueDeclaration.kind !== 225 /* ModuleDeclaration */))) { + // other kinds of value declarations take precedence over modules + target.valueDeclaration = source.valueDeclaration; + } + ts.forEach(source.declarations, function (node) { + target.declarations.push(node); + }); + if (source.members) { + if (!target.members) + target.members = ts.createMap(); + mergeSymbolTable(target.members, source.members); + } + if (source.exports) { + if (!target.exports) + target.exports = ts.createMap(); + mergeSymbolTable(target.exports, source.exports); } + recordMergedSymbol(target, source); } - else if (links.target === resolvingSymbol) { - links.target = unknownSymbol; + else { + var message_2 = target.flags & 2 /* BlockScopedVariable */ || source.flags & 2 /* BlockScopedVariable */ + ? ts.Diagnostics.Cannot_redeclare_block_scoped_variable_0 : ts.Diagnostics.Duplicate_identifier_0; + ts.forEach(source.declarations, function (node) { + error(node.name ? node.name : node, message_2, symbolToString(source)); + }); + ts.forEach(target.declarations, function (node) { + error(node.name ? node.name : node, message_2, symbolToString(source)); + }); } - return links.target; } - function markExportAsReferenced(node) { - var symbol = getSymbolOfNode(node); - var target = resolveAlias(symbol); - if (target) { - var markAlias = target === unknownSymbol || - ((target.flags & 107455 /* Value */) && !isConstEnumOrConstEnumOnlyModule(target)); - if (markAlias) { - markAliasSymbolAsReferenced(symbol); + function mergeSymbolTable(target, source) { + for (var id in source) { + var targetSymbol = target[id]; + if (!targetSymbol) { + target[id] = source[id]; + } + else { + if (!(targetSymbol.flags & 33554432 /* Merged */)) { + target[id] = targetSymbol = cloneSymbol(targetSymbol); + } + mergeSymbol(targetSymbol, source[id]); } } } - // When an alias symbol is referenced, we need to mark the entity it references as referenced and in turn repeat that until - // we reach a non-alias or an exported entity (which is always considered referenced). We do this by checking the target of - // the alias as an expression (which recursively takes us back here if the target references another alias). - function markAliasSymbolAsReferenced(symbol) { - var links = getSymbolLinks(symbol); - if (!links.referenced) { - links.referenced = true; - var node = getDeclarationOfAliasSymbol(symbol); - ts.Debug.assert(!!node); - if (node.kind === 235 /* ExportAssignment */) { - // export default - checkExpressionCached(node.expression); - } - else if (node.kind === 238 /* ExportSpecifier */) { - // export { } or export { as foo } - checkExpressionCached(node.propertyName || node.name); - } - else if (ts.isInternalModuleImportEqualsDeclaration(node)) { - // import foo = - checkExpressionCached(node.moduleReference); - } - } - } - // This function is only for imports with entity names - function getSymbolOfPartOfRightHandSideOfImportEquals(entityName, importDeclaration, dontResolveAlias) { - // There are three things we might try to look for. In the following examples, - // the search term is enclosed in |...|: - // - // import a = |b|; // Namespace - // import a = |b.c|; // Value, type, namespace - // import a = |b.c|.d; // Namespace - if (entityName.kind === 69 /* Identifier */ && ts.isRightSideOfQualifiedNameOrPropertyAccess(entityName)) { - entityName = entityName.parent; + function mergeModuleAugmentation(moduleName) { + var moduleAugmentation = moduleName.parent; + if (moduleAugmentation.symbol.declarations[0] !== moduleAugmentation) { + // this is a combined symbol for multiple augmentations within the same file. + // its symbol already has accumulated information for all declarations + // so we need to add it just once - do the work only for first declaration + ts.Debug.assert(moduleAugmentation.symbol.declarations.length > 1); + return; } - // Check for case 1 and 3 in the above example - if (entityName.kind === 69 /* Identifier */ || entityName.parent.kind === 139 /* QualifiedName */) { - return resolveEntityName(entityName, 1920 /* Namespace */, /*ignoreErrors*/ false, dontResolveAlias); + if (ts.isGlobalScopeAugmentation(moduleAugmentation)) { + mergeSymbolTable(globals, moduleAugmentation.symbol.exports); } else { - // Case 2 in above example - // entityName.kind could be a QualifiedName or a Missing identifier - ts.Debug.assert(entityName.parent.kind === 229 /* ImportEqualsDeclaration */); - return resolveEntityName(entityName, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */, /*ignoreErrors*/ false, dontResolveAlias); - } - } - function getFullyQualifiedName(symbol) { - return symbol.parent ? getFullyQualifiedName(symbol.parent) + "." + symbolToString(symbol) : symbolToString(symbol); - } - // Resolves a qualified name and any involved aliases - function resolveEntityName(name, meaning, ignoreErrors, dontResolveAlias, location) { - if (ts.nodeIsMissing(name)) { - return undefined; - } - var symbol; - if (name.kind === 69 /* Identifier */) { - var message = meaning === 1920 /* Namespace */ ? ts.Diagnostics.Cannot_find_namespace_0 : ts.Diagnostics.Cannot_find_name_0; - symbol = resolveName(location || name, name.text, meaning, ignoreErrors ? undefined : message, name); - if (!symbol) { - return undefined; - } - } - else if (name.kind === 139 /* QualifiedName */ || name.kind === 172 /* PropertyAccessExpression */) { - var left = name.kind === 139 /* QualifiedName */ ? name.left : name.expression; - var right = name.kind === 139 /* QualifiedName */ ? name.right : name.name; - var namespace = resolveEntityName(left, 1920 /* Namespace */, ignoreErrors, /*dontResolveAlias*/ false, location); - if (!namespace || ts.nodeIsMissing(right)) { - return undefined; + // find a module that about to be augmented + // do not validate names of augmentations that are defined in ambient context + var moduleNotFoundError = !ts.isInAmbientContext(moduleName.parent.parent) + ? ts.Diagnostics.Invalid_module_name_in_augmentation_module_0_cannot_be_found + : undefined; + var mainModule = resolveExternalModuleNameWorker(moduleName, moduleName, moduleNotFoundError); + if (!mainModule) { + return; } - else if (namespace === unknownSymbol) { - return namespace; + // obtain item referenced by 'export=' + mainModule = resolveExternalModuleSymbol(mainModule); + if (mainModule.flags & 1920 /* Namespace */) { + // if module symbol has already been merged - it is safe to use it. + // otherwise clone it + mainModule = mainModule.flags & 33554432 /* Merged */ ? mainModule : cloneSymbol(mainModule); + mergeSymbol(mainModule, moduleAugmentation.symbol); } - symbol = getSymbol(getExportsOfSymbol(namespace), right.text, meaning); - if (!symbol) { - if (!ignoreErrors) { - error(right, ts.Diagnostics.Namespace_0_has_no_exported_member_1, getFullyQualifiedName(namespace), ts.declarationNameToString(right)); - } - return undefined; + else { + error(moduleName, ts.Diagnostics.Cannot_augment_module_0_because_it_resolves_to_a_non_module_entity, moduleName.text); } } - else { - ts.Debug.fail("Unknown entity name kind."); - } - ts.Debug.assert((symbol.flags & 16777216 /* Instantiated */) === 0, "Should never get an instantiated symbol here."); - return (symbol.flags & meaning) || dontResolveAlias ? symbol : resolveAlias(symbol); - } - function resolveExternalModuleName(location, moduleReferenceExpression) { - return resolveExternalModuleNameWorker(location, moduleReferenceExpression, ts.Diagnostics.Cannot_find_module_0); - } - function resolveExternalModuleNameWorker(location, moduleReferenceExpression, moduleNotFoundError) { - if (moduleReferenceExpression.kind !== 9 /* StringLiteral */) { - return; - } - var moduleReferenceLiteral = moduleReferenceExpression; - return resolveExternalModule(location, moduleReferenceLiteral.text, moduleNotFoundError, moduleReferenceLiteral); } - function resolveExternalModule(location, moduleReference, moduleNotFoundError, errorNode) { - // Module names are escaped in our symbol table. However, string literal values aren't. - // Escape the name in the "require(...)" clause to ensure we find the right symbol. - var moduleName = ts.escapeIdentifier(moduleReference); - if (moduleName === undefined) { - return; - } - var isRelative = ts.isExternalModuleNameRelative(moduleName); - if (!isRelative) { - var symbol = getSymbol(globals, '"' + moduleName + '"', 512 /* ValueModule */); - if (symbol) { - // merged symbol is module declaration symbol combined with all augmentations - return getMergedSymbol(symbol); - } - } - var resolvedModule = ts.getResolvedModule(ts.getSourceFileOfNode(location), moduleReference); - var sourceFile = resolvedModule && host.getSourceFile(resolvedModule.resolvedFileName); - if (sourceFile) { - if (sourceFile.symbol) { - // merged symbol is module declaration symbol combined with all augmentations - return getMergedSymbol(sourceFile.symbol); - } - if (moduleNotFoundError) { - // report errors only if it was requested - error(errorNode, ts.Diagnostics.File_0_is_not_a_module, sourceFile.fileName); - } - return undefined; - } - if (patternAmbientModules) { - var pattern = ts.findBestPatternMatch(patternAmbientModules, function (_) { return _.pattern; }, moduleName); - if (pattern) { - return getMergedSymbol(pattern.symbol); - } - } - if (moduleNotFoundError) { - // report errors only if it was requested - var tsExtension = ts.tryExtractTypeScriptExtension(moduleName); - if (tsExtension) { - var diag = ts.Diagnostics.An_import_path_cannot_end_with_a_0_extension_Consider_importing_1_instead; - error(errorNode, diag, tsExtension, ts.removeExtension(moduleName, tsExtension)); + function addToSymbolTable(target, source, message) { + for (var id in source) { + if (target[id]) { + // Error on redeclarations + ts.forEach(target[id].declarations, addDeclarationDiagnostic(id, message)); } else { - error(errorNode, moduleNotFoundError, moduleName); + target[id] = source[id]; } } - return undefined; - } - // An external module with an 'export =' declaration resolves to the target of the 'export =' declaration, - // and an external module with no 'export =' declaration resolves to the module itself. - function resolveExternalModuleSymbol(moduleSymbol) { - return moduleSymbol && getMergedSymbol(resolveSymbol(moduleSymbol.exports["export="])) || moduleSymbol; - } - // An external module with an 'export =' declaration may be referenced as an ES6 module provided the 'export =' - // references a symbol that is at least declared as a module or a variable. The target of the 'export =' may - // combine other declarations with the module or variable (e.g. a class/module, function/module, interface/variable). - function resolveESModuleSymbol(moduleSymbol, moduleReferenceExpression) { - var symbol = resolveExternalModuleSymbol(moduleSymbol); - if (symbol && !(symbol.flags & (1536 /* Module */ | 3 /* Variable */))) { - error(moduleReferenceExpression, ts.Diagnostics.Module_0_resolves_to_a_non_module_entity_and_cannot_be_imported_using_this_construct, symbolToString(moduleSymbol)); - symbol = undefined; + function addDeclarationDiagnostic(id, message) { + return function (declaration) { return diagnostics.add(ts.createDiagnosticForNode(declaration, message, id)); }; } - return symbol; - } - function hasExportAssignmentSymbol(moduleSymbol) { - return moduleSymbol.exports["export="] !== undefined; - } - function getExportsOfModuleAsArray(moduleSymbol) { - return symbolsToArray(getExportsOfModule(moduleSymbol)); } - function getExportsOfSymbol(symbol) { - return symbol.flags & 1536 /* Module */ ? getExportsOfModule(symbol) : symbol.exports || emptySymbols; + function getSymbolLinks(symbol) { + if (symbol.flags & 67108864 /* Transient */) + return symbol; + var id = getSymbolId(symbol); + return symbolLinks[id] || (symbolLinks[id] = {}); } - function getExportsOfModule(moduleSymbol) { - var links = getSymbolLinks(moduleSymbol); - return links.resolvedExports || (links.resolvedExports = getExportsForModule(moduleSymbol)); + function getNodeLinks(node) { + var nodeId = getNodeId(node); + return nodeLinks[nodeId] || (nodeLinks[nodeId] = { flags: 0 }); } - /** - * Extends one symbol table with another while collecting information on name collisions for error message generation into the `lookupTable` argument - * Not passing `lookupTable` and `exportNode` disables this collection, and just extends the tables - */ - function extendExportSymbols(target, source, lookupTable, exportNode) { - for (var id in source) { - if (id !== "default" && !target[id]) { - target[id] = source[id]; - if (lookupTable && exportNode) { - lookupTable[id] = { - specifierText: ts.getTextOfNode(exportNode.moduleSpecifier) - }; - } - } - else if (lookupTable && exportNode && id !== "default" && target[id] && resolveSymbol(target[id]) !== resolveSymbol(source[id])) { - if (!lookupTable[id].exportsWithDuplicate) { - lookupTable[id].exportsWithDuplicate = [exportNode]; - } - else { - lookupTable[id].exportsWithDuplicate.push(exportNode); - } - } - } + function isGlobalSourceFile(node) { + return node.kind === 256 /* SourceFile */ && !ts.isExternalOrCommonJsModule(node); } - function getExportsForModule(moduleSymbol) { - var visitedSymbols = []; - return visit(moduleSymbol) || moduleSymbol.exports; - // The ES6 spec permits export * declarations in a module to circularly reference the module itself. For example, - // module 'a' can 'export * from "b"' and 'b' can 'export * from "a"' without error. - function visit(symbol) { - if (!(symbol && symbol.flags & 1952 /* HasExports */ && !ts.contains(visitedSymbols, symbol))) { - return; - } - visitedSymbols.push(symbol); - var symbols = ts.cloneMap(symbol.exports); - // All export * declarations are collected in an __export symbol by the binder - var exportStars = symbol.exports["__export"]; - if (exportStars) { - var nestedSymbols = ts.createMap(); - var lookupTable = ts.createMap(); - for (var _i = 0, _a = exportStars.declarations; _i < _a.length; _i++) { - var node = _a[_i]; - var resolvedModule = resolveExternalModuleName(node, node.moduleSpecifier); - var exportedSymbols = visit(resolvedModule); - extendExportSymbols(nestedSymbols, exportedSymbols, lookupTable, node); + function getSymbol(symbols, name, meaning) { + if (meaning) { + var symbol = symbols[name]; + if (symbol) { + ts.Debug.assert((symbol.flags & 16777216 /* Instantiated */) === 0, "Should never get an instantiated symbol here."); + if (symbol.flags & meaning) { + return symbol; } - for (var id in lookupTable) { - var exportsWithDuplicate = lookupTable[id].exportsWithDuplicate; - // It's not an error if the file with multiple `export *`s with duplicate names exports a member with that name itself - if (id === "export=" || !(exportsWithDuplicate && exportsWithDuplicate.length) || symbols[id]) { - continue; - } - for (var _b = 0, exportsWithDuplicate_1 = exportsWithDuplicate; _b < exportsWithDuplicate_1.length; _b++) { - var node = exportsWithDuplicate_1[_b]; - diagnostics.add(ts.createDiagnosticForNode(node, ts.Diagnostics.Module_0_has_already_exported_a_member_named_1_Consider_explicitly_re_exporting_to_resolve_the_ambiguity, lookupTable[id].specifierText, id)); + if (symbol.flags & 8388608 /* Alias */) { + var target = resolveAlias(symbol); + // Unknown symbol means an error occurred in alias resolution, treat it as positive answer to avoid cascading errors + if (target === unknownSymbol || target.flags & meaning) { + return symbol; } } - extendExportSymbols(symbols, nestedSymbols); } - return symbols; } + // return undefined if we can't find a symbol. } - function getMergedSymbol(symbol) { - var merged; - return symbol && symbol.mergeId && (merged = mergedSymbols[symbol.mergeId]) ? merged : symbol; - } - function getSymbolOfNode(node) { - return getMergedSymbol(node.symbol); - } - function getParentOfSymbol(symbol) { - return getMergedSymbol(symbol.parent); - } - function getExportSymbolOfValueSymbolIfExported(symbol) { - return symbol && (symbol.flags & 1048576 /* ExportValue */) !== 0 - ? getMergedSymbol(symbol.exportSymbol) - : symbol; - } - function symbolIsValue(symbol) { - // If it is an instantiated symbol, then it is a value if the symbol it is an - // instantiation of is a value. - if (symbol.flags & 16777216 /* Instantiated */) { - return symbolIsValue(getSymbolLinks(symbol).target); + /** + * Get symbols that represent parameter-property-declaration as parameter and as property declaration + * @param parameter a parameterDeclaration node + * @param parameterName a name of the parameter to get the symbols for. + * @return a tuple of two symbols + */ + function getSymbolsOfParameterPropertyDeclaration(parameter, parameterName) { + var constructorDeclaration = parameter.parent; + var classDeclaration = parameter.parent.parent; + var parameterSymbol = getSymbol(constructorDeclaration.locals, parameterName, 107455 /* Value */); + var propertySymbol = getSymbol(classDeclaration.symbol.members, parameterName, 107455 /* Value */); + if (parameterSymbol && propertySymbol) { + return [parameterSymbol, propertySymbol]; } - // If the symbol has the value flag, it is trivially a value. - if (symbol.flags & 107455 /* Value */) { - return true; + ts.Debug.fail("There should exist two symbols, one as property declaration and one as parameter declaration"); + } + function isBlockScopedNameDeclaredBeforeUse(declaration, usage) { + var declarationFile = ts.getSourceFileOfNode(declaration); + var useFile = ts.getSourceFileOfNode(usage); + if (declarationFile !== useFile) { + if ((modulekind && (declarationFile.externalModuleIndicator || useFile.externalModuleIndicator)) || + (!compilerOptions.outFile && !compilerOptions.out)) { + // nodes are in different files and order cannot be determines + return true; + } + var sourceFiles = host.getSourceFiles(); + return ts.indexOf(sourceFiles, declarationFile) <= ts.indexOf(sourceFiles, useFile); } - // If it is an alias, then it is a value if the symbol it resolves to is a value. - if (symbol.flags & 8388608 /* Alias */) { - return (resolveAlias(symbol).flags & 107455 /* Value */) !== 0; + if (declaration.pos <= usage.pos) { + // declaration is before usage + // still might be illegal if usage is in the initializer of the variable declaration + return declaration.kind !== 218 /* VariableDeclaration */ || + !isImmediatelyUsedInInitializerOfBlockScopedVariable(declaration, usage); } - return false; - } - function findConstructorDeclaration(node) { - var members = node.members; - for (var _i = 0, members_1 = members; _i < members_1.length; _i++) { - var member = members_1[_i]; - if (member.kind === 148 /* Constructor */ && ts.nodeIsPresent(member.body)) { - return member; + // declaration is after usage + // can be legal if usage is deferred (i.e. inside function or in initializer of instance property) + return isUsedInFunctionOrNonStaticProperty(declaration, usage); + function isImmediatelyUsedInInitializerOfBlockScopedVariable(declaration, usage) { + var container = ts.getEnclosingBlockScopeContainer(declaration); + switch (declaration.parent.parent.kind) { + case 200 /* VariableStatement */: + case 206 /* ForStatement */: + case 208 /* ForOfStatement */: + // variable statement/for/for-of statement case, + // use site should not be inside variable declaration (initializer of declaration or binding element) + if (isSameScopeDescendentOf(usage, declaration, container)) { + return true; + } + break; + } + switch (declaration.parent.parent.kind) { + case 207 /* ForInStatement */: + case 208 /* ForOfStatement */: + // ForIn/ForOf case - use site should not be used in expression part + if (isSameScopeDescendentOf(usage, declaration.parent.parent.expression, container)) { + return true; + } } + return false; } - } - function createType(flags) { - var result = new Type(checker, flags); - typeCount++; - result.id = typeCount; - return result; - } - function createIntrinsicType(kind, intrinsicName) { - var type = createType(kind); - type.intrinsicName = intrinsicName; - return type; - } - function createBooleanType(trueFalseTypes) { - var type = getUnionType(trueFalseTypes); - type.flags |= 8 /* Boolean */; - type.intrinsicName = "boolean"; - return type; - } - function createObjectType(kind, symbol) { - var type = createType(kind); - type.symbol = symbol; - return type; - } - // A reserved member name starts with two underscores, but the third character cannot be an underscore - // or the @ symbol. A third underscore indicates an escaped form of an identifer that started - // with at least two underscores. The @ character indicates that the name is denoted by a well known ES - // Symbol instance. - function isReservedMemberName(name) { - return name.charCodeAt(0) === 95 /* _ */ && - name.charCodeAt(1) === 95 /* _ */ && - name.charCodeAt(2) !== 95 /* _ */ && - name.charCodeAt(2) !== 64 /* at */; - } - function getNamedMembers(members) { - var result; - for (var id in members) { - if (!isReservedMemberName(id)) { - if (!result) - result = []; - var symbol = members[id]; - if (symbolIsValue(symbol)) { - result.push(symbol); + function isUsedInFunctionOrNonStaticProperty(declaration, usage) { + var container = ts.getEnclosingBlockScopeContainer(declaration); + var current = usage; + while (current) { + if (current === container) { + return false; + } + if (ts.isFunctionLike(current)) { + return true; + } + var initializerOfNonStaticProperty = current.parent && + current.parent.kind === 145 /* PropertyDeclaration */ && + (ts.getModifierFlags(current.parent) & 32 /* Static */) === 0 && + current.parent.initializer === current; + if (initializerOfNonStaticProperty) { + return true; } + current = current.parent; } + return false; } - return result || emptyArray; - } - function setObjectTypeMembers(type, members, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo) { - type.members = members; - type.properties = getNamedMembers(members); - type.callSignatures = callSignatures; - type.constructSignatures = constructSignatures; - if (stringIndexInfo) - type.stringIndexInfo = stringIndexInfo; - if (numberIndexInfo) - type.numberIndexInfo = numberIndexInfo; - return type; } - function createAnonymousType(symbol, members, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo) { - return setObjectTypeMembers(createObjectType(2097152 /* Anonymous */, symbol), members, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo); - } - function forEachSymbolTableInScope(enclosingDeclaration, callback) { + // Resolve a given name for a given meaning at a given location. An error is reported if the name was not found and + // the nameNotFoundMessage argument is not undefined. Returns the resolved symbol, or undefined if no symbol with + // the given name can be found. + function resolveName(location, name, meaning, nameNotFoundMessage, nameArg) { var result; - for (var location_1 = enclosingDeclaration; location_1; location_1 = location_1.parent) { + var lastLocation; + var propertyWithInvalidInitializer; + var errorLocation = location; + var grandparent; + var isInExternalModule = false; + loop: while (location) { // Locals of a source file are not in scope (because they get merged into the global symbol table) - if (location_1.locals && !isGlobalSourceFile(location_1)) { - if (result = callback(location_1.locals)) { - return result; + if (location.locals && !isGlobalSourceFile(location)) { + if (result = getSymbol(location.locals, name, meaning)) { + var useResult = true; + if (ts.isFunctionLike(location) && lastLocation && lastLocation !== location.body) { + // symbol lookup restrictions for function-like declarations + // - Type parameters of a function are in scope in the entire function declaration, including the parameter + // list and return type. However, local types are only in scope in the function body. + // - parameters are only in the scope of function body + // This restriction does not apply to JSDoc comment types because they are parented + // at a higher level than type parameters would normally be + if (meaning & result.flags & 793064 /* Type */ && lastLocation.kind !== 273 /* JSDocComment */) { + useResult = result.flags & 262144 /* TypeParameter */ + ? lastLocation === location.type || + lastLocation.kind === 142 /* Parameter */ || + lastLocation.kind === 141 /* TypeParameter */ + : false; + } + if (meaning & 107455 /* Value */ && result.flags & 1 /* FunctionScopedVariable */) { + // parameters are visible only inside function body, parameter list and return type + // technically for parameter list case here we might mix parameters and variables declared in function, + // however it is detected separately when checking initializers of parameters + // to make sure that they reference no variables declared after them. + useResult = + lastLocation.kind === 142 /* Parameter */ || + (lastLocation === location.type && + result.valueDeclaration.kind === 142 /* Parameter */); + } + } + if (useResult) { + break loop; + } + else { + result = undefined; + } } } - switch (location_1.kind) { + switch (location.kind) { case 256 /* SourceFile */: - if (!ts.isExternalOrCommonJsModule(location_1)) { + if (!ts.isExternalOrCommonJsModule(location)) break; - } + isInExternalModule = true; case 225 /* ModuleDeclaration */: - if (result = callback(getSymbolOfNode(location_1).exports)) { - return result; - } - break; - } - } - return callback(globals); - } - function getQualifiedLeftMeaning(rightMeaning) { - // If we are looking in value space, the parent meaning is value, other wise it is namespace - return rightMeaning === 107455 /* Value */ ? 107455 /* Value */ : 1920 /* Namespace */; - } - function getAccessibleSymbolChain(symbol, enclosingDeclaration, meaning, useOnlyExternalAliasing) { - function getAccessibleSymbolChainFromSymbolTable(symbols) { - function canQualifySymbol(symbolFromSymbolTable, meaning) { - // If the symbol is equivalent and doesn't need further qualification, this symbol is accessible - if (!needsQualification(symbolFromSymbolTable, enclosingDeclaration, meaning)) { - return true; - } - // If symbol needs qualification, make sure that parent is accessible, if it is then this symbol is accessible too - var accessibleParent = getAccessibleSymbolChain(symbolFromSymbolTable.parent, enclosingDeclaration, getQualifiedLeftMeaning(meaning), useOnlyExternalAliasing); - return !!accessibleParent; - } - function isAccessible(symbolFromSymbolTable, resolvedAliasSymbol) { - if (symbol === (resolvedAliasSymbol || symbolFromSymbolTable)) { - // if the symbolFromSymbolTable is not external module (it could be if it was determined as ambient external module and would be in globals table) - // and if symbolFromSymbolTable or alias resolution matches the symbol, - // check the symbol can be qualified, it is only then this symbol is accessible - return !ts.forEach(symbolFromSymbolTable.declarations, hasExternalModuleSymbol) && - canQualifySymbol(symbolFromSymbolTable, meaning); - } - } - // If symbol is directly available by its name in the symbol table - if (isAccessible(symbols[symbol.name])) { - return [symbol]; - } - // Check if symbol is any of the alias - return ts.forEachProperty(symbols, function (symbolFromSymbolTable) { - if (symbolFromSymbolTable.flags & 8388608 /* Alias */ - && symbolFromSymbolTable.name !== "export=" - && !ts.getDeclarationOfKind(symbolFromSymbolTable, 238 /* ExportSpecifier */)) { - if (!useOnlyExternalAliasing || - // Is this external alias, then use it to name - ts.forEach(symbolFromSymbolTable.declarations, ts.isExternalModuleImportEqualsDeclaration)) { - var resolvedImportedSymbol = resolveAlias(symbolFromSymbolTable); - if (isAccessible(symbolFromSymbolTable, resolveAlias(symbolFromSymbolTable))) { - return [symbolFromSymbolTable]; + var moduleExports = getSymbolOfNode(location).exports; + if (location.kind === 256 /* SourceFile */ || ts.isAmbientModule(location)) { + // It's an external module. First see if the module has an export default and if the local + // name of that export default matches. + if (result = moduleExports["default"]) { + var localSymbol = ts.getLocalSymbolForExportDefault(result); + if (localSymbol && (result.flags & meaning) && localSymbol.name === name) { + break loop; + } + result = undefined; } - // Look in the exported members, if we can find accessibleSymbolChain, symbol is accessible using this chain - // but only if the symbolFromSymbolTable can be qualified - var accessibleSymbolsFromExports = resolvedImportedSymbol.exports ? getAccessibleSymbolChainFromSymbolTable(resolvedImportedSymbol.exports) : undefined; - if (accessibleSymbolsFromExports && canQualifySymbol(symbolFromSymbolTable, getQualifiedLeftMeaning(meaning))) { - return [symbolFromSymbolTable].concat(accessibleSymbolsFromExports); + // Because of module/namespace merging, a module's exports are in scope, + // yet we never want to treat an export specifier as putting a member in scope. + // Therefore, if the name we find is purely an export specifier, it is not actually considered in scope. + // Two things to note about this: + // 1. We have to check this without calling getSymbol. The problem with calling getSymbol + // on an export specifier is that it might find the export specifier itself, and try to + // resolve it as an alias. This will cause the checker to consider the export specifier + // a circular alias reference when it might not be. + // 2. We check === SymbolFlags.Alias in order to check that the symbol is *purely* + // an alias. If we used &, we'd be throwing out symbols that have non alias aspects, + // which is not the desired behavior. + if (moduleExports[name] && + moduleExports[name].flags === 8388608 /* Alias */ && + ts.getDeclarationOfKind(moduleExports[name], 238 /* ExportSpecifier */)) { + break; } } - } - }); - } - if (symbol) { - if (!(isPropertyOrMethodDeclarationSymbol(symbol))) { - return forEachSymbolTableInScope(enclosingDeclaration, getAccessibleSymbolChainFromSymbolTable); + if (result = getSymbol(moduleExports, name, meaning & 8914931 /* ModuleMember */)) { + break loop; + } + break; + case 224 /* EnumDeclaration */: + if (result = getSymbol(getSymbolOfNode(location).exports, name, meaning & 8 /* EnumMember */)) { + break loop; + } + break; + case 145 /* PropertyDeclaration */: + case 144 /* PropertySignature */: + // TypeScript 1.0 spec (April 2014): 8.4.1 + // Initializer expressions for instance member variables are evaluated in the scope + // of the class constructor body but are not permitted to reference parameters or + // local variables of the constructor. This effectively means that entities from outer scopes + // by the same name as a constructor parameter or local variable are inaccessible + // in initializer expressions for instance member variables. + if (ts.isClassLike(location.parent) && !(ts.getModifierFlags(location) & 32 /* Static */)) { + var ctor = findConstructorDeclaration(location.parent); + if (ctor && ctor.locals) { + if (getSymbol(ctor.locals, name, meaning & 107455 /* Value */)) { + // Remember the property node, it will be used later to report appropriate error + propertyWithInvalidInitializer = location; + } + } + } + break; + case 221 /* ClassDeclaration */: + case 192 /* ClassExpression */: + case 222 /* InterfaceDeclaration */: + if (result = getSymbol(getSymbolOfNode(location).members, name, meaning & 793064 /* Type */)) { + if (lastLocation && ts.getModifierFlags(lastLocation) & 32 /* Static */) { + // TypeScript 1.0 spec (April 2014): 3.4.1 + // The scope of a type parameter extends over the entire declaration with which the type + // parameter list is associated, with the exception of static member declarations in classes. + error(errorLocation, ts.Diagnostics.Static_members_cannot_reference_class_type_parameters); + return undefined; + } + break loop; + } + if (location.kind === 192 /* ClassExpression */ && meaning & 32 /* Class */) { + var className = location.name; + if (className && name === className.text) { + result = location.symbol; + break loop; + } + } + break; + // It is not legal to reference a class's own type parameters from a computed property name that + // belongs to the class. For example: + // + // function foo() { return '' } + // class C { // <-- Class's own type parameter T + // [foo()]() { } // <-- Reference to T from class's own computed property + // } + // + case 140 /* ComputedPropertyName */: + grandparent = location.parent.parent; + if (ts.isClassLike(grandparent) || grandparent.kind === 222 /* InterfaceDeclaration */) { + // A reference to this grandparent's type parameters would be an error + if (result = getSymbol(getSymbolOfNode(grandparent).members, name, meaning & 793064 /* Type */)) { + error(errorLocation, ts.Diagnostics.A_computed_property_name_cannot_reference_a_type_parameter_from_its_containing_type); + return undefined; + } + } + break; + case 147 /* MethodDeclaration */: + case 146 /* MethodSignature */: + case 148 /* Constructor */: + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + case 220 /* FunctionDeclaration */: + case 180 /* ArrowFunction */: + if (meaning & 3 /* Variable */ && name === "arguments") { + result = argumentsSymbol; + break loop; + } + break; + case 179 /* FunctionExpression */: + if (meaning & 3 /* Variable */ && name === "arguments") { + result = argumentsSymbol; + break loop; + } + if (meaning & 16 /* Function */) { + var functionName = location.name; + if (functionName && name === functionName.text) { + result = location.symbol; + break loop; + } + } + break; + case 143 /* Decorator */: + // Decorators are resolved at the class declaration. Resolving at the parameter + // or member would result in looking up locals in the method. + // + // function y() {} + // class C { + // method(@y x, y) {} // <-- decorator y should be resolved at the class declaration, not the parameter. + // } + // + if (location.parent && location.parent.kind === 142 /* Parameter */) { + location = location.parent; + } + // + // function y() {} + // class C { + // @y method(x, y) {} // <-- decorator y should be resolved at the class declaration, not the method. + // } + // + if (location.parent && ts.isClassElement(location.parent)) { + location = location.parent; + } + break; } + lastLocation = location; + location = location.parent; } - } - function needsQualification(symbol, enclosingDeclaration, meaning) { - var qualify = false; - forEachSymbolTableInScope(enclosingDeclaration, function (symbolTable) { - // If symbol of this name is not available in the symbol table we are ok - var symbolFromSymbolTable = symbolTable[symbol.name]; - if (!symbolFromSymbolTable) { - // Continue to the next symbol table - return false; - } - // If the symbol with this name is present it should refer to the symbol - if (symbolFromSymbolTable === symbol) { - // No need to qualify - return true; - } - // Qualify if the symbol from symbol table has same meaning as expected - symbolFromSymbolTable = (symbolFromSymbolTable.flags & 8388608 /* Alias */ && !ts.getDeclarationOfKind(symbolFromSymbolTable, 238 /* ExportSpecifier */)) ? resolveAlias(symbolFromSymbolTable) : symbolFromSymbolTable; - if (symbolFromSymbolTable.flags & meaning) { - qualify = true; - return true; - } - // Continue to the next symbol table - return false; - }); - return qualify; - } - function isPropertyOrMethodDeclarationSymbol(symbol) { - if (symbol.declarations && symbol.declarations.length) { - for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { - var declaration = _a[_i]; - switch (declaration.kind) { - case 145 /* PropertyDeclaration */: - case 147 /* MethodDeclaration */: - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - continue; - default: - return false; + if (result && nameNotFoundMessage && noUnusedIdentifiers) { + result.isReferenced = true; + } + if (!result) { + result = getSymbol(globals, name, meaning); + } + if (!result) { + if (nameNotFoundMessage) { + if (!errorLocation || + !checkAndReportErrorForMissingPrefix(errorLocation, name, nameArg) && + !checkAndReportErrorForExtendingInterface(errorLocation) && + !checkAndReportErrorForUsingTypeAsValue(errorLocation, name, meaning)) { + error(errorLocation, nameNotFoundMessage, typeof nameArg === "string" ? nameArg : ts.declarationNameToString(nameArg)); } } - return true; + return undefined; } - return false; - } - /** - * Check if the given symbol in given enclosing declaration is accessible and mark all associated alias to be visible if requested - * - * @param symbol a Symbol to check if accessible - * @param enclosingDeclaration a Node containing reference to the symbol - * @param meaning a SymbolFlags to check if such meaning of the symbol is accessible - * @param shouldComputeAliasToMakeVisible a boolean value to indicate whether to return aliases to be mark visible in case the symbol is accessible - */ - function isSymbolAccessible(symbol, enclosingDeclaration, meaning, shouldComputeAliasesToMakeVisible) { - if (symbol && enclosingDeclaration && !(symbol.flags & 262144 /* TypeParameter */)) { - var initialSymbol = symbol; - var meaningToLook = meaning; - while (symbol) { - // Symbol is accessible if it by itself is accessible - var accessibleSymbolChain = getAccessibleSymbolChain(symbol, enclosingDeclaration, meaningToLook, /*useOnlyExternalAliasing*/ false); - if (accessibleSymbolChain) { - var hasAccessibleDeclarations = hasVisibleDeclarations(accessibleSymbolChain[0], shouldComputeAliasesToMakeVisible); - if (!hasAccessibleDeclarations) { - return { - accessibility: 1 /* NotAccessible */, - errorSymbolName: symbolToString(initialSymbol, enclosingDeclaration, meaning), - errorModuleName: symbol !== initialSymbol ? symbolToString(symbol, enclosingDeclaration, 1920 /* Namespace */) : undefined - }; - } - return hasAccessibleDeclarations; - } - // If we haven't got the accessible symbol, it doesn't mean the symbol is actually inaccessible. - // It could be a qualified symbol and hence verify the path - // e.g.: - // module m { - // export class c { - // } - // } - // const x: typeof m.c - // In the above example when we start with checking if typeof m.c symbol is accessible, - // we are going to see if c can be accessed in scope directly. - // But it can't, hence the accessible is going to be undefined, but that doesn't mean m.c is inaccessible - // It is accessible if the parent m is accessible because then m.c can be accessed through qualification - meaningToLook = getQualifiedLeftMeaning(meaning); - symbol = getParentOfSymbol(symbol); + // Perform extra checks only if error reporting was requested + if (nameNotFoundMessage) { + if (propertyWithInvalidInitializer) { + // We have a match, but the reference occurred within a property initializer and the identifier also binds + // to a local variable in the constructor where the code will be emitted. + var propertyName = propertyWithInvalidInitializer.name; + error(errorLocation, ts.Diagnostics.Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor, ts.declarationNameToString(propertyName), typeof nameArg === "string" ? nameArg : ts.declarationNameToString(nameArg)); + return undefined; } - // This could be a symbol that is not exported in the external module - // or it could be a symbol from different external module that is not aliased and hence cannot be named - var symbolExternalModule = ts.forEach(initialSymbol.declarations, getExternalModuleContainer); - if (symbolExternalModule) { - var enclosingExternalModule = getExternalModuleContainer(enclosingDeclaration); - if (symbolExternalModule !== enclosingExternalModule) { - // name from different external module that is not visible - return { - accessibility: 2 /* CannotBeNamed */, - errorSymbolName: symbolToString(initialSymbol, enclosingDeclaration, meaning), - errorModuleName: symbolToString(symbolExternalModule) - }; + // Only check for block-scoped variable if we are looking for the + // name with variable meaning + // For example, + // declare module foo { + // interface bar {} + // } + // const foo/*1*/: foo/*2*/.bar; + // The foo at /*1*/ and /*2*/ will share same symbol with two meaning + // block - scope variable and namespace module. However, only when we + // try to resolve name in /*1*/ which is used in variable position, + // we want to check for block- scoped + if (meaning & 2 /* BlockScopedVariable */) { + var exportOrLocalSymbol = getExportSymbolOfValueSymbolIfExported(result); + if (exportOrLocalSymbol.flags & 2 /* BlockScopedVariable */) { + checkResolvedBlockScopedVariable(exportOrLocalSymbol, errorLocation); } } - // Just a local name that is not accessible - return { - accessibility: 1 /* NotAccessible */, - errorSymbolName: symbolToString(initialSymbol, enclosingDeclaration, meaning) - }; - } - return { accessibility: 0 /* Accessible */ }; - function getExternalModuleContainer(declaration) { - for (; declaration; declaration = declaration.parent) { - if (hasExternalModuleSymbol(declaration)) { - return getSymbolOfNode(declaration); + // If we're in an external module, we can't reference value symbols created from UMD export declarations + if (result && isInExternalModule && (meaning & 107455 /* Value */) === 107455 /* Value */) { + var decls = result.declarations; + if (decls && decls.length === 1 && decls[0].kind === 228 /* NamespaceExportDeclaration */) { + error(errorLocation, ts.Diagnostics._0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead, name); } } } + return result; } - function hasExternalModuleSymbol(declaration) { - return ts.isAmbientModule(declaration) || (declaration.kind === 256 /* SourceFile */ && ts.isExternalOrCommonJsModule(declaration)); - } - function hasVisibleDeclarations(symbol, shouldComputeAliasToMakeVisible) { - var aliasesToMakeVisible; - if (ts.forEach(symbol.declarations, function (declaration) { return !getIsDeclarationVisible(declaration); })) { - return undefined; + function checkAndReportErrorForMissingPrefix(errorLocation, name, nameArg) { + if ((errorLocation.kind === 69 /* Identifier */ && (isTypeReferenceIdentifier(errorLocation)) || isInTypeQuery(errorLocation))) { + return false; } - return { accessibility: 0 /* Accessible */, aliasesToMakeVisible: aliasesToMakeVisible }; - function getIsDeclarationVisible(declaration) { - if (!isDeclarationVisible(declaration)) { - // Mark the unexported alias as visible if its parent is visible - // because these kind of aliases can be used to name types in declaration file - var anyImportSyntax = getAnyImportSyntax(declaration); - if (anyImportSyntax && - !(ts.getModifierFlags(anyImportSyntax) & 1 /* Export */) && - isDeclarationVisible(anyImportSyntax.parent)) { - // In function "buildTypeDisplay" where we decide whether to write type-alias or serialize types, - // we want to just check if type- alias is accessible or not but we don't care about emitting those alias at that time - // since we will do the emitting later in trackSymbol. - if (shouldComputeAliasToMakeVisible) { - getNodeLinks(declaration).isVisible = true; - if (aliasesToMakeVisible) { - if (!ts.contains(aliasesToMakeVisible, anyImportSyntax)) { - aliasesToMakeVisible.push(anyImportSyntax); - } - } - else { - aliasesToMakeVisible = [anyImportSyntax]; - } - } + var container = ts.getThisContainer(errorLocation, /* includeArrowFunctions */ true); + var location = container; + while (location) { + if (ts.isClassLike(location.parent)) { + var classSymbol = getSymbolOfNode(location.parent); + if (!classSymbol) { + break; + } + // Check to see if a static member exists. + var constructorType = getTypeOfSymbol(classSymbol); + if (getPropertyOfType(constructorType, name)) { + error(errorLocation, ts.Diagnostics.Cannot_find_name_0_Did_you_mean_the_static_member_1_0, typeof nameArg === "string" ? nameArg : ts.declarationNameToString(nameArg), symbolToString(classSymbol)); return true; } - // Declaration is not visible - return false; + // No static member is present. + // Check if we're in an instance method and look for a relevant instance member. + if (location === container && !(ts.getModifierFlags(location) & 32 /* Static */)) { + var instanceType = getDeclaredTypeOfSymbol(classSymbol).thisType; + if (getPropertyOfType(instanceType, name)) { + error(errorLocation, ts.Diagnostics.Cannot_find_name_0_Did_you_mean_the_instance_member_this_0, typeof nameArg === "string" ? nameArg : ts.declarationNameToString(nameArg)); + return true; + } + } } - return true; + location = location.parent; } + return false; } - function isEntityNameVisible(entityName, enclosingDeclaration) { - // get symbol of the first identifier of the entityName - var meaning; - if (entityName.parent.kind === 158 /* TypeQuery */ || ts.isExpressionWithTypeArgumentsInClassExtendsClause(entityName.parent)) { - // Typeof value - meaning = 107455 /* Value */ | 1048576 /* ExportValue */; + function checkAndReportErrorForExtendingInterface(errorLocation) { + var expression = getEntityNameForExtendingInterface(errorLocation); + var isError = !!(expression && resolveEntityName(expression, 64 /* Interface */, /*ignoreErrors*/ true)); + if (isError) { + error(errorLocation, ts.Diagnostics.Cannot_extend_an_interface_0_Did_you_mean_implements, ts.getTextOfNode(expression)); } - else if (entityName.kind === 139 /* QualifiedName */ || entityName.kind === 172 /* PropertyAccessExpression */ || - entityName.parent.kind === 229 /* ImportEqualsDeclaration */) { - // Left identifier from type reference or TypeAlias - // Entity name of the import declaration - meaning = 1920 /* Namespace */; + return isError; + } + /** + * Climbs up parents to an ExpressionWithTypeArguments, and returns its expression, + * but returns undefined if that expression is not an EntityNameExpression. + */ + function getEntityNameForExtendingInterface(node) { + switch (node.kind) { + case 69 /* Identifier */: + case 172 /* PropertyAccessExpression */: + return node.parent ? getEntityNameForExtendingInterface(node.parent) : undefined; + case 194 /* ExpressionWithTypeArguments */: + ts.Debug.assert(ts.isEntityNameExpression(node.expression)); + return node.expression; + default: + return undefined; } - else { - // Type Reference or TypeAlias entity = Identifier - meaning = 793064 /* Type */; + } + function checkAndReportErrorForUsingTypeAsValue(errorLocation, name, meaning) { + if (meaning & (107455 /* Value */ & ~1024 /* NamespaceModule */)) { + var symbol = resolveSymbol(resolveName(errorLocation, name, 793064 /* Type */ & ~107455 /* Value */, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined)); + if (symbol && !(symbol.flags & 1024 /* NamespaceModule */)) { + error(errorLocation, ts.Diagnostics._0_only_refers_to_a_type_but_is_being_used_as_a_value_here, name); + return true; + } } - var firstIdentifier = getFirstIdentifier(entityName); - var symbol = resolveName(enclosingDeclaration, firstIdentifier.text, meaning, /*nodeNotFoundErrorMessage*/ undefined, /*nameArg*/ undefined); - // Verify if the symbol is accessible - return (symbol && hasVisibleDeclarations(symbol, /*shouldComputeAliasToMakeVisible*/ true)) || { - accessibility: 1 /* NotAccessible */, - errorSymbolName: ts.getTextOfNode(firstIdentifier), - errorNode: firstIdentifier - }; + return false; } - function writeKeyword(writer, kind) { - writer.writeKeyword(ts.tokenToString(kind)); + function checkResolvedBlockScopedVariable(result, errorLocation) { + ts.Debug.assert((result.flags & 2 /* BlockScopedVariable */) !== 0); + // Block-scoped variables cannot be used before their definition + var declaration = ts.forEach(result.declarations, function (d) { return ts.isBlockOrCatchScoped(d) ? d : undefined; }); + ts.Debug.assert(declaration !== undefined, "Block-scoped variable declaration is undefined"); + if (!ts.isInAmbientContext(declaration) && !isBlockScopedNameDeclaredBeforeUse(ts.getAncestor(declaration, 218 /* VariableDeclaration */), errorLocation)) { + error(errorLocation, ts.Diagnostics.Block_scoped_variable_0_used_before_its_declaration, ts.declarationNameToString(declaration.name)); + } } - function writePunctuation(writer, kind) { - writer.writePunctuation(ts.tokenToString(kind)); + /* Starting from 'initial' node walk up the parent chain until 'stopAt' node is reached. + * If at any point current node is equal to 'parent' node - return true. + * Return false if 'stopAt' node is reached or isFunctionLike(current) === true. + */ + function isSameScopeDescendentOf(initial, parent, stopAt) { + if (!parent) { + return false; + } + for (var current = initial; current && current !== stopAt && !ts.isFunctionLike(current); current = current.parent) { + if (current === parent) { + return true; + } + } + return false; } - function writeSpace(writer) { - writer.writeSpace(" "); + function getAnyImportSyntax(node) { + if (ts.isAliasSymbolDeclaration(node)) { + if (node.kind === 229 /* ImportEqualsDeclaration */) { + return node; + } + while (node && node.kind !== 230 /* ImportDeclaration */) { + node = node.parent; + } + return node; + } } - function symbolToString(symbol, enclosingDeclaration, meaning) { - var writer = ts.getSingleLineStringWriter(); - getSymbolDisplayBuilder().buildSymbolDisplay(symbol, writer, enclosingDeclaration, meaning); - var result = writer.string(); - ts.releaseStringWriter(writer); - return result; + function getDeclarationOfAliasSymbol(symbol) { + return ts.forEach(symbol.declarations, function (d) { return ts.isAliasSymbolDeclaration(d) ? d : undefined; }); } - function signatureToString(signature, enclosingDeclaration, flags, kind) { - var writer = ts.getSingleLineStringWriter(); - getSymbolDisplayBuilder().buildSignatureDisplay(signature, writer, enclosingDeclaration, flags, kind); - var result = writer.string(); - ts.releaseStringWriter(writer); - return result; + function getTargetOfImportEqualsDeclaration(node) { + if (node.moduleReference.kind === 240 /* ExternalModuleReference */) { + return resolveExternalModuleSymbol(resolveExternalModuleName(node, ts.getExternalModuleImportEqualsDeclarationExpression(node))); + } + return getSymbolOfPartOfRightHandSideOfImportEquals(node.moduleReference, node); } - function typeToString(type, enclosingDeclaration, flags) { - var writer = ts.getSingleLineStringWriter(); - getSymbolDisplayBuilder().buildTypeDisplay(type, writer, enclosingDeclaration, flags); - var result = writer.string(); - ts.releaseStringWriter(writer); - var maxLength = compilerOptions.noErrorTruncation || flags & 4 /* NoTruncation */ ? undefined : 100; - if (maxLength && result.length >= maxLength) { - result = result.substr(0, maxLength - "...".length) + "..."; + function getTargetOfImportClause(node) { + var moduleSymbol = resolveExternalModuleName(node, node.parent.moduleSpecifier); + if (moduleSymbol) { + var exportDefaultSymbol = ts.isShorthandAmbientModuleSymbol(moduleSymbol) ? + moduleSymbol : + moduleSymbol.exports["export="] ? + getPropertyOfType(getTypeOfSymbol(moduleSymbol.exports["export="]), "default") : + resolveSymbol(moduleSymbol.exports["default"]); + if (!exportDefaultSymbol && !allowSyntheticDefaultImports) { + error(node.name, ts.Diagnostics.Module_0_has_no_default_export, symbolToString(moduleSymbol)); + } + else if (!exportDefaultSymbol && allowSyntheticDefaultImports) { + return resolveExternalModuleSymbol(moduleSymbol) || resolveSymbol(moduleSymbol); + } + return exportDefaultSymbol; } - return result; } - function typePredicateToString(typePredicate, enclosingDeclaration, flags) { - var writer = ts.getSingleLineStringWriter(); - getSymbolDisplayBuilder().buildTypePredicateDisplay(typePredicate, writer, enclosingDeclaration, flags); - var result = writer.string(); - ts.releaseStringWriter(writer); + function getTargetOfNamespaceImport(node) { + var moduleSpecifier = node.parent.parent.moduleSpecifier; + return resolveESModuleSymbol(resolveExternalModuleName(node, moduleSpecifier), moduleSpecifier); + } + // This function creates a synthetic symbol that combines the value side of one symbol with the + // type/namespace side of another symbol. Consider this example: + // + // declare module graphics { + // interface Point { + // x: number; + // y: number; + // } + // } + // declare var graphics: { + // Point: new (x: number, y: number) => graphics.Point; + // } + // declare module "graphics" { + // export = graphics; + // } + // + // An 'import { Point } from "graphics"' needs to create a symbol that combines the value side 'Point' + // property with the type/namespace side interface 'Point'. + function combineValueAndTypeSymbols(valueSymbol, typeSymbol) { + if (valueSymbol.flags & (793064 /* Type */ | 1920 /* Namespace */)) { + return valueSymbol; + } + var result = createSymbol(valueSymbol.flags | typeSymbol.flags, valueSymbol.name); + result.declarations = ts.concatenate(valueSymbol.declarations, typeSymbol.declarations); + result.parent = valueSymbol.parent || typeSymbol.parent; + if (valueSymbol.valueDeclaration) + result.valueDeclaration = valueSymbol.valueDeclaration; + if (typeSymbol.members) + result.members = typeSymbol.members; + if (valueSymbol.exports) + result.exports = valueSymbol.exports; return result; } - function formatUnionTypes(types) { - var result = []; - var flags = 0; - for (var i = 0; i < types.length; i++) { - var t = types[i]; - flags |= t.flags; - if (!(t.flags & 6144 /* Nullable */)) { - if (t.flags & (128 /* BooleanLiteral */ | 256 /* EnumLiteral */)) { - var baseType = t.flags & 128 /* BooleanLiteral */ ? booleanType : t.baseType; - var count = baseType.types.length; - if (i + count <= types.length && types[i + count - 1] === baseType.types[count - 1]) { - result.push(baseType); - i += count - 1; - continue; - } + function getExportOfModule(symbol, name) { + if (symbol.flags & 1536 /* Module */) { + var exportedSymbol = getExportsOfSymbol(symbol)[name]; + if (exportedSymbol) { + return resolveSymbol(exportedSymbol); + } + } + } + function getPropertyOfVariable(symbol, name) { + if (symbol.flags & 3 /* Variable */) { + var typeAnnotation = symbol.valueDeclaration.type; + if (typeAnnotation) { + return resolveSymbol(getPropertyOfType(getTypeFromTypeNode(typeAnnotation), name)); + } + } + } + function getExternalModuleMember(node, specifier) { + var moduleSymbol = resolveExternalModuleName(node, node.moduleSpecifier); + var targetSymbol = resolveESModuleSymbol(moduleSymbol, node.moduleSpecifier); + if (targetSymbol) { + var name_13 = specifier.propertyName || specifier.name; + if (name_13.text) { + if (ts.isShorthandAmbientModuleSymbol(moduleSymbol)) { + return moduleSymbol; } - result.push(t); + var symbolFromVariable = void 0; + // First check if module was specified with "export=". If so, get the member from the resolved type + if (moduleSymbol && moduleSymbol.exports && moduleSymbol.exports["export="]) { + symbolFromVariable = getPropertyOfType(getTypeOfSymbol(targetSymbol), name_13.text); + } + else { + symbolFromVariable = getPropertyOfVariable(targetSymbol, name_13.text); + } + // if symbolFromVariable is export - get its final target + symbolFromVariable = resolveSymbol(symbolFromVariable); + var symbolFromModule = getExportOfModule(targetSymbol, name_13.text); + // If the export member we're looking for is default, and there is no real default but allowSyntheticDefaultImports is on, return the entire module as the default + if (!symbolFromModule && allowSyntheticDefaultImports && name_13.text === "default") { + symbolFromModule = resolveExternalModuleSymbol(moduleSymbol) || resolveSymbol(moduleSymbol); + } + var symbol = symbolFromModule && symbolFromVariable ? + combineValueAndTypeSymbols(symbolFromVariable, symbolFromModule) : + symbolFromModule || symbolFromVariable; + if (!symbol) { + error(name_13, ts.Diagnostics.Module_0_has_no_exported_member_1, getFullyQualifiedName(moduleSymbol), ts.declarationNameToString(name_13)); + } + return symbol; } } - if (flags & 4096 /* Null */) - result.push(nullType); - if (flags & 2048 /* Undefined */) - result.push(undefinedType); - return result || types; } - function visibilityToString(flags) { - if (flags === 8 /* Private */) { - return "private"; + function getTargetOfImportSpecifier(node) { + return getExternalModuleMember(node.parent.parent.parent, node); + } + function getTargetOfNamespaceExportDeclaration(node) { + return resolveExternalModuleSymbol(node.parent.symbol); + } + function getTargetOfExportSpecifier(node) { + return node.parent.parent.moduleSpecifier ? + getExternalModuleMember(node.parent.parent, node) : + resolveEntityName(node.propertyName || node.name, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */); + } + function getTargetOfExportAssignment(node) { + return resolveEntityName(node.expression, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */); + } + function getTargetOfAliasDeclaration(node) { + switch (node.kind) { + case 229 /* ImportEqualsDeclaration */: + return getTargetOfImportEqualsDeclaration(node); + case 231 /* ImportClause */: + return getTargetOfImportClause(node); + case 232 /* NamespaceImport */: + return getTargetOfNamespaceImport(node); + case 234 /* ImportSpecifier */: + return getTargetOfImportSpecifier(node); + case 238 /* ExportSpecifier */: + return getTargetOfExportSpecifier(node); + case 235 /* ExportAssignment */: + return getTargetOfExportAssignment(node); + case 228 /* NamespaceExportDeclaration */: + return getTargetOfNamespaceExportDeclaration(node); } - if (flags === 16 /* Protected */) { - return "protected"; + } + function resolveSymbol(symbol) { + return symbol && symbol.flags & 8388608 /* Alias */ && !(symbol.flags & (107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */)) ? resolveAlias(symbol) : symbol; + } + function resolveAlias(symbol) { + ts.Debug.assert((symbol.flags & 8388608 /* Alias */) !== 0, "Should only get Alias here."); + var links = getSymbolLinks(symbol); + if (!links.target) { + links.target = resolvingSymbol; + var node = getDeclarationOfAliasSymbol(symbol); + ts.Debug.assert(!!node); + var target = getTargetOfAliasDeclaration(node); + if (links.target === resolvingSymbol) { + links.target = target || unknownSymbol; + } + else { + error(node, ts.Diagnostics.Circular_definition_of_import_alias_0, symbolToString(symbol)); + } } - return "public"; + else if (links.target === resolvingSymbol) { + links.target = unknownSymbol; + } + return links.target; } - function getTypeAliasForTypeLiteral(type) { - if (type.symbol && type.symbol.flags & 2048 /* TypeLiteral */) { - var node = type.symbol.declarations[0].parent; - while (node.kind === 164 /* ParenthesizedType */) { - node = node.parent; + function markExportAsReferenced(node) { + var symbol = getSymbolOfNode(node); + var target = resolveAlias(symbol); + if (target) { + var markAlias = target === unknownSymbol || + ((target.flags & 107455 /* Value */) && !isConstEnumOrConstEnumOnlyModule(target)); + if (markAlias) { + markAliasSymbolAsReferenced(symbol); } - if (node.kind === 223 /* TypeAliasDeclaration */) { - return getSymbolOfNode(node); + } + } + // When an alias symbol is referenced, we need to mark the entity it references as referenced and in turn repeat that until + // we reach a non-alias or an exported entity (which is always considered referenced). We do this by checking the target of + // the alias as an expression (which recursively takes us back here if the target references another alias). + function markAliasSymbolAsReferenced(symbol) { + var links = getSymbolLinks(symbol); + if (!links.referenced) { + links.referenced = true; + var node = getDeclarationOfAliasSymbol(symbol); + ts.Debug.assert(!!node); + if (node.kind === 235 /* ExportAssignment */) { + // export default + checkExpressionCached(node.expression); + } + else if (node.kind === 238 /* ExportSpecifier */) { + // export { } or export { as foo } + checkExpressionCached(node.propertyName || node.name); + } + else if (ts.isInternalModuleImportEqualsDeclaration(node)) { + // import foo = + checkExpressionCached(node.moduleReference); } } - return undefined; } - function isTopLevelInExternalModuleAugmentation(node) { - return node && node.parent && - node.parent.kind === 226 /* ModuleBlock */ && - ts.isExternalModuleAugmentation(node.parent.parent); + // This function is only for imports with entity names + function getSymbolOfPartOfRightHandSideOfImportEquals(entityName, importDeclaration, dontResolveAlias) { + // There are three things we might try to look for. In the following examples, + // the search term is enclosed in |...|: + // + // import a = |b|; // Namespace + // import a = |b.c|; // Value, type, namespace + // import a = |b.c|.d; // Namespace + if (entityName.kind === 69 /* Identifier */ && ts.isRightSideOfQualifiedNameOrPropertyAccess(entityName)) { + entityName = entityName.parent; + } + // Check for case 1 and 3 in the above example + if (entityName.kind === 69 /* Identifier */ || entityName.parent.kind === 139 /* QualifiedName */) { + return resolveEntityName(entityName, 1920 /* Namespace */, /*ignoreErrors*/ false, dontResolveAlias); + } + else { + // Case 2 in above example + // entityName.kind could be a QualifiedName or a Missing identifier + ts.Debug.assert(entityName.parent.kind === 229 /* ImportEqualsDeclaration */); + return resolveEntityName(entityName, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */, /*ignoreErrors*/ false, dontResolveAlias); + } } - function literalTypeToString(type) { - return type.flags & 32 /* StringLiteral */ ? "\"" + ts.escapeString(type.text) + "\"" : type.text; + function getFullyQualifiedName(symbol) { + return symbol.parent ? getFullyQualifiedName(symbol.parent) + "." + symbolToString(symbol) : symbolToString(symbol); } - function getSymbolDisplayBuilder() { - function getNameOfSymbol(symbol) { - if (symbol.declarations && symbol.declarations.length) { - var declaration = symbol.declarations[0]; - if (declaration.name) { - return ts.declarationNameToString(declaration.name); - } - switch (declaration.kind) { - case 192 /* ClassExpression */: - return "(Anonymous class)"; - case 179 /* FunctionExpression */: - case 180 /* ArrowFunction */: - return "(Anonymous function)"; - } - } - return symbol.name; + // Resolves a qualified name and any involved aliases + function resolveEntityName(name, meaning, ignoreErrors, dontResolveAlias, location) { + if (ts.nodeIsMissing(name)) { + return undefined; } - /** - * Writes only the name of the symbol out to the writer. Uses the original source text - * for the name of the symbol if it is available to match how the user wrote the name. - */ - function appendSymbolNameOnly(symbol, writer) { - writer.writeSymbol(getNameOfSymbol(symbol), symbol); + var symbol; + if (name.kind === 69 /* Identifier */) { + var message = meaning === 1920 /* Namespace */ ? ts.Diagnostics.Cannot_find_namespace_0 : ts.Diagnostics.Cannot_find_name_0; + symbol = resolveName(location || name, name.text, meaning, ignoreErrors ? undefined : message, name); + if (!symbol) { + return undefined; + } } - /** - * Writes a property access or element access with the name of the symbol out to the writer. - * Uses the original source text for the name of the symbol if it is available to match how the user wrote the name, - * ensuring that any names written with literals use element accesses. - */ - function appendPropertyOrElementAccessForSymbol(symbol, writer) { - var symbolName = getNameOfSymbol(symbol); - var firstChar = symbolName.charCodeAt(0); - var needsElementAccess = !ts.isIdentifierStart(firstChar, languageVersion); - if (needsElementAccess) { - writePunctuation(writer, 19 /* OpenBracketToken */); - if (ts.isSingleOrDoubleQuote(firstChar)) { - writer.writeStringLiteral(symbolName); - } - else { - writer.writeSymbol(symbolName, symbol); + else if (name.kind === 139 /* QualifiedName */ || name.kind === 172 /* PropertyAccessExpression */) { + var left = name.kind === 139 /* QualifiedName */ ? name.left : name.expression; + var right = name.kind === 139 /* QualifiedName */ ? name.right : name.name; + var namespace = resolveEntityName(left, 1920 /* Namespace */, ignoreErrors, /*dontResolveAlias*/ false, location); + if (!namespace || ts.nodeIsMissing(right)) { + return undefined; + } + else if (namespace === unknownSymbol) { + return namespace; + } + symbol = getSymbol(getExportsOfSymbol(namespace), right.text, meaning); + if (!symbol) { + if (!ignoreErrors) { + error(right, ts.Diagnostics.Namespace_0_has_no_exported_member_1, getFullyQualifiedName(namespace), ts.declarationNameToString(right)); } - writePunctuation(writer, 20 /* CloseBracketToken */); + return undefined; } - else { - writePunctuation(writer, 21 /* DotToken */); - writer.writeSymbol(symbolName, symbol); + } + else { + ts.Debug.fail("Unknown entity name kind."); + } + ts.Debug.assert((symbol.flags & 16777216 /* Instantiated */) === 0, "Should never get an instantiated symbol here."); + return (symbol.flags & meaning) || dontResolveAlias ? symbol : resolveAlias(symbol); + } + function resolveExternalModuleName(location, moduleReferenceExpression) { + return resolveExternalModuleNameWorker(location, moduleReferenceExpression, ts.Diagnostics.Cannot_find_module_0); + } + function resolveExternalModuleNameWorker(location, moduleReferenceExpression, moduleNotFoundError) { + if (moduleReferenceExpression.kind !== 9 /* StringLiteral */) { + return; + } + var moduleReferenceLiteral = moduleReferenceExpression; + return resolveExternalModule(location, moduleReferenceLiteral.text, moduleNotFoundError, moduleReferenceLiteral); + } + function resolveExternalModule(location, moduleReference, moduleNotFoundError, errorNode) { + // Module names are escaped in our symbol table. However, string literal values aren't. + // Escape the name in the "require(...)" clause to ensure we find the right symbol. + var moduleName = ts.escapeIdentifier(moduleReference); + if (moduleName === undefined) { + return; + } + var isRelative = ts.isExternalModuleNameRelative(moduleName); + if (!isRelative) { + var symbol = getSymbol(globals, '"' + moduleName + '"', 512 /* ValueModule */); + if (symbol) { + // merged symbol is module declaration symbol combined with all augmentations + return getMergedSymbol(symbol); } } - /** - * Enclosing declaration is optional when we don't want to get qualified name in the enclosing declaration scope - * Meaning needs to be specified if the enclosing declaration is given - */ - function buildSymbolDisplay(symbol, writer, enclosingDeclaration, meaning, flags, typeFlags) { - var parentSymbol; - function appendParentTypeArgumentsAndSymbolName(symbol) { - if (parentSymbol) { - // Write type arguments of instantiated class/interface here - if (flags & 1 /* WriteTypeParametersOrArguments */) { - if (symbol.flags & 16777216 /* Instantiated */) { - buildDisplayForTypeArgumentsAndDelimiters(getTypeParametersOfClassOrInterface(parentSymbol), symbol.mapper, writer, enclosingDeclaration); - } - else { - buildTypeParameterDisplayFromSymbol(parentSymbol, writer, enclosingDeclaration); - } - } - appendPropertyOrElementAccessForSymbol(symbol, writer); - } - else { - appendSymbolNameOnly(symbol, writer); - } - parentSymbol = symbol; + var resolvedModule = ts.getResolvedModule(ts.getSourceFileOfNode(location), moduleReference); + var sourceFile = resolvedModule && host.getSourceFile(resolvedModule.resolvedFileName); + if (sourceFile) { + if (sourceFile.symbol) { + // merged symbol is module declaration symbol combined with all augmentations + return getMergedSymbol(sourceFile.symbol); } - // Let the writer know we just wrote out a symbol. The declaration emitter writer uses - // this to determine if an import it has previously seen (and not written out) needs - // to be written to the file once the walk of the tree is complete. - // - // NOTE(cyrusn): This approach feels somewhat unfortunate. A simple pass over the tree - // up front (for example, during checking) could determine if we need to emit the imports - // and we could then access that data during declaration emit. - writer.trackSymbol(symbol, enclosingDeclaration, meaning); - /** @param endOfChain Set to false for recursive calls; non-recursive calls should always output something. */ - function walkSymbol(symbol, meaning, endOfChain) { - var accessibleSymbolChain = getAccessibleSymbolChain(symbol, enclosingDeclaration, meaning, !!(flags & 2 /* UseOnlyExternalAliasing */)); - if (!accessibleSymbolChain || - needsQualification(accessibleSymbolChain[0], enclosingDeclaration, accessibleSymbolChain.length === 1 ? meaning : getQualifiedLeftMeaning(meaning))) { - // Go up and add our parent. - var parent_7 = getParentOfSymbol(accessibleSymbolChain ? accessibleSymbolChain[0] : symbol); - if (parent_7) { - walkSymbol(parent_7, getQualifiedLeftMeaning(meaning), /*endOfChain*/ false); - } - } - if (accessibleSymbolChain) { - for (var _i = 0, accessibleSymbolChain_1 = accessibleSymbolChain; _i < accessibleSymbolChain_1.length; _i++) { - var accessibleSymbol = accessibleSymbolChain_1[_i]; - appendParentTypeArgumentsAndSymbolName(accessibleSymbol); - } - } - else if ( - // If this is the last part of outputting the symbol, always output. The cases apply only to parent symbols. - endOfChain || - // If a parent symbol is an external module, don't write it. (We prefer just `x` vs `"foo/bar".x`.) - !(!parentSymbol && ts.forEach(symbol.declarations, hasExternalModuleSymbol)) && - // If a parent symbol is an anonymous type, don't write it. - !(symbol.flags & (2048 /* TypeLiteral */ | 4096 /* ObjectLiteral */))) { - appendParentTypeArgumentsAndSymbolName(symbol); - } + if (moduleNotFoundError) { + // report errors only if it was requested + error(errorNode, ts.Diagnostics.File_0_is_not_a_module, sourceFile.fileName); } - // Get qualified name if the symbol is not a type parameter - // and there is an enclosing declaration or we specifically - // asked for it - var isTypeParameter = symbol.flags & 262144 /* TypeParameter */; - var typeFormatFlag = 128 /* UseFullyQualifiedType */ & typeFlags; - if (!isTypeParameter && (enclosingDeclaration || typeFormatFlag)) { - walkSymbol(symbol, meaning, /*endOfChain*/ true); + return undefined; + } + if (patternAmbientModules) { + var pattern = ts.findBestPatternMatch(patternAmbientModules, function (_) { return _.pattern; }, moduleName); + if (pattern) { + return getMergedSymbol(pattern.symbol); + } + } + if (moduleNotFoundError) { + // report errors only if it was requested + var tsExtension = ts.tryExtractTypeScriptExtension(moduleName); + if (tsExtension) { + var diag = ts.Diagnostics.An_import_path_cannot_end_with_a_0_extension_Consider_importing_1_instead; + error(errorNode, diag, tsExtension, ts.removeExtension(moduleName, tsExtension)); } else { - appendParentTypeArgumentsAndSymbolName(symbol); + error(errorNode, moduleNotFoundError, moduleName); } } - function buildTypeDisplay(type, writer, enclosingDeclaration, globalFlags, symbolStack) { - var globalFlagsToPass = globalFlags & 16 /* WriteOwnNameForAnyLike */; - var inObjectTypeLiteral = false; - return writeType(type, globalFlags); - function writeType(type, flags) { - var nextFlags = flags & ~512 /* InTypeAlias */; - // Write undefined/null type as any - if (type.flags & 16015 /* Intrinsic */) { - // Special handling for unknown / resolving types, they should show up as any and not unknown or __resolving - writer.writeKeyword(!(globalFlags & 16 /* WriteOwnNameForAnyLike */) && isTypeAny(type) - ? "any" - : type.intrinsicName); - } - else if (type.flags & 268435456 /* ThisType */) { - if (inObjectTypeLiteral) { - writer.reportInaccessibleThisError(); - } - writer.writeKeyword("this"); - } - else if (type.flags & 131072 /* Reference */) { - writeTypeReference(type, nextFlags); - } - else if (type.flags & 256 /* EnumLiteral */) { - buildSymbolDisplay(getParentOfSymbol(type.symbol), writer, enclosingDeclaration, 793064 /* Type */, 0 /* None */, nextFlags); - writePunctuation(writer, 21 /* DotToken */); - appendSymbolNameOnly(type.symbol, writer); - } - else if (type.flags & (32768 /* Class */ | 65536 /* Interface */ | 16 /* Enum */ | 16384 /* TypeParameter */)) { - // The specified symbol flags need to be reinterpreted as type flags - buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, 793064 /* Type */, 0 /* None */, nextFlags); - } - else if (!(flags & 512 /* InTypeAlias */) && type.flags & (2097152 /* Anonymous */ | 1572864 /* UnionOrIntersection */) && type.aliasSymbol && - isSymbolAccessible(type.aliasSymbol, enclosingDeclaration, 793064 /* Type */, /*shouldComputeAliasesToMakeVisible*/ false).accessibility === 0 /* Accessible */) { - // Only write out inferred type with its corresponding type-alias if type-alias is visible - var typeArguments = type.aliasTypeArguments; - writeSymbolTypeReference(type.aliasSymbol, typeArguments, 0, typeArguments ? typeArguments.length : 0, nextFlags); - } - else if (type.flags & 1572864 /* UnionOrIntersection */) { - writeUnionOrIntersectionType(type, nextFlags); - } - else if (type.flags & 2097152 /* Anonymous */) { - writeAnonymousType(type, nextFlags); + return undefined; + } + // An external module with an 'export =' declaration resolves to the target of the 'export =' declaration, + // and an external module with no 'export =' declaration resolves to the module itself. + function resolveExternalModuleSymbol(moduleSymbol) { + return moduleSymbol && getMergedSymbol(resolveSymbol(moduleSymbol.exports["export="])) || moduleSymbol; + } + // An external module with an 'export =' declaration may be referenced as an ES6 module provided the 'export =' + // references a symbol that is at least declared as a module or a variable. The target of the 'export =' may + // combine other declarations with the module or variable (e.g. a class/module, function/module, interface/variable). + function resolveESModuleSymbol(moduleSymbol, moduleReferenceExpression) { + var symbol = resolveExternalModuleSymbol(moduleSymbol); + if (symbol && !(symbol.flags & (1536 /* Module */ | 3 /* Variable */))) { + error(moduleReferenceExpression, ts.Diagnostics.Module_0_resolves_to_a_non_module_entity_and_cannot_be_imported_using_this_construct, symbolToString(moduleSymbol)); + symbol = undefined; + } + return symbol; + } + function hasExportAssignmentSymbol(moduleSymbol) { + return moduleSymbol.exports["export="] !== undefined; + } + function getExportsOfModuleAsArray(moduleSymbol) { + return symbolsToArray(getExportsOfModule(moduleSymbol)); + } + function getExportsOfSymbol(symbol) { + return symbol.flags & 1536 /* Module */ ? getExportsOfModule(symbol) : symbol.exports || emptySymbols; + } + function getExportsOfModule(moduleSymbol) { + var links = getSymbolLinks(moduleSymbol); + return links.resolvedExports || (links.resolvedExports = getExportsForModule(moduleSymbol)); + } + /** + * Extends one symbol table with another while collecting information on name collisions for error message generation into the `lookupTable` argument + * Not passing `lookupTable` and `exportNode` disables this collection, and just extends the tables + */ + function extendExportSymbols(target, source, lookupTable, exportNode) { + for (var id in source) { + if (id !== "default" && !target[id]) { + target[id] = source[id]; + if (lookupTable && exportNode) { + lookupTable[id] = { + specifierText: ts.getTextOfNode(exportNode.moduleSpecifier) + }; } - else if (type.flags & 96 /* StringOrNumberLiteral */) { - writer.writeStringLiteral(literalTypeToString(type)); + } + else if (lookupTable && exportNode && id !== "default" && target[id] && resolveSymbol(target[id]) !== resolveSymbol(source[id])) { + if (!lookupTable[id].exportsWithDuplicate) { + lookupTable[id].exportsWithDuplicate = [exportNode]; } else { - // Should never get here - // { ... } - writePunctuation(writer, 15 /* OpenBraceToken */); - writeSpace(writer); - writePunctuation(writer, 22 /* DotDotDotToken */); - writeSpace(writer); - writePunctuation(writer, 16 /* CloseBraceToken */); + lookupTable[id].exportsWithDuplicate.push(exportNode); } } - function writeTypeList(types, delimiter) { - for (var i = 0; i < types.length; i++) { - if (i > 0) { - if (delimiter !== 24 /* CommaToken */) { - writeSpace(writer); - } - writePunctuation(writer, delimiter); - writeSpace(writer); - } - writeType(types[i], delimiter === 24 /* CommaToken */ ? 0 /* None */ : 64 /* InElementType */); - } + } + } + function getExportsForModule(moduleSymbol) { + var visitedSymbols = []; + // A module defined by an 'export=' consists on one export that needs to be resolved + moduleSymbol = resolveExternalModuleSymbol(moduleSymbol); + return visit(moduleSymbol) || moduleSymbol.exports; + // The ES6 spec permits export * declarations in a module to circularly reference the module itself. For example, + // module 'a' can 'export * from "b"' and 'b' can 'export * from "a"' without error. + function visit(symbol) { + if (!(symbol && symbol.flags & 1952 /* HasExports */ && !ts.contains(visitedSymbols, symbol))) { + return; } - function writeSymbolTypeReference(symbol, typeArguments, pos, end, flags) { - // Unnamed function expressions and arrow functions have reserved names that we don't want to display - if (symbol.flags & 32 /* Class */ || !isReservedMemberName(symbol.name)) { - buildSymbolDisplay(symbol, writer, enclosingDeclaration, 793064 /* Type */, 0 /* None */, flags); + visitedSymbols.push(symbol); + var symbols = ts.cloneMap(symbol.exports); + // All export * declarations are collected in an __export symbol by the binder + var exportStars = symbol.exports["__export"]; + if (exportStars) { + var nestedSymbols = ts.createMap(); + var lookupTable = ts.createMap(); + for (var _i = 0, _a = exportStars.declarations; _i < _a.length; _i++) { + var node = _a[_i]; + var resolvedModule = resolveExternalModuleName(node, node.moduleSpecifier); + var exportedSymbols = visit(resolvedModule); + extendExportSymbols(nestedSymbols, exportedSymbols, lookupTable, node); } - if (pos < end) { - writePunctuation(writer, 25 /* LessThanToken */); - writeType(typeArguments[pos], 256 /* InFirstTypeArgument */); - pos++; - while (pos < end) { - writePunctuation(writer, 24 /* CommaToken */); - writeSpace(writer); - writeType(typeArguments[pos], 0 /* None */); - pos++; + for (var id in lookupTable) { + var exportsWithDuplicate = lookupTable[id].exportsWithDuplicate; + // It's not an error if the file with multiple `export *`s with duplicate names exports a member with that name itself + if (id === "export=" || !(exportsWithDuplicate && exportsWithDuplicate.length) || symbols[id]) { + continue; } - writePunctuation(writer, 27 /* GreaterThanToken */); - } - } - function writeTypeReference(type, flags) { - var typeArguments = type.typeArguments || emptyArray; - if (type.target === globalArrayType && !(flags & 1 /* WriteArrayAsGenericType */)) { - writeType(typeArguments[0], 64 /* InElementType */); - writePunctuation(writer, 19 /* OpenBracketToken */); - writePunctuation(writer, 20 /* CloseBracketToken */); - } - else if (type.target.flags & 262144 /* Tuple */) { - writePunctuation(writer, 19 /* OpenBracketToken */); - writeTypeList(type.typeArguments.slice(0, getTypeReferenceArity(type)), 24 /* CommaToken */); - writePunctuation(writer, 20 /* CloseBracketToken */); - } - else { - // Write the type reference in the format f.g.C where A and B are type arguments - // for outer type parameters, and f and g are the respective declaring containers of those - // type parameters. - var outerTypeParameters = type.target.outerTypeParameters; - var i = 0; - if (outerTypeParameters) { - var length_1 = outerTypeParameters.length; - while (i < length_1) { - // Find group of type arguments for type parameters with the same declaring container. - var start = i; - var parent_8 = getParentSymbolOfTypeParameter(outerTypeParameters[i]); - do { - i++; - } while (i < length_1 && getParentSymbolOfTypeParameter(outerTypeParameters[i]) === parent_8); - // When type parameters are their own type arguments for the whole group (i.e. we have - // the default outer type arguments), we don't show the group. - if (!ts.rangeEquals(outerTypeParameters, typeArguments, start, i)) { - writeSymbolTypeReference(parent_8, typeArguments, start, i, flags); - writePunctuation(writer, 21 /* DotToken */); - } - } + for (var _b = 0, exportsWithDuplicate_1 = exportsWithDuplicate; _b < exportsWithDuplicate_1.length; _b++) { + var node = exportsWithDuplicate_1[_b]; + diagnostics.add(ts.createDiagnosticForNode(node, ts.Diagnostics.Module_0_has_already_exported_a_member_named_1_Consider_explicitly_re_exporting_to_resolve_the_ambiguity, lookupTable[id].specifierText, id)); } - var typeParameterCount = (type.target.typeParameters || emptyArray).length; - writeSymbolTypeReference(type.symbol, typeArguments, i, typeParameterCount, flags); } + extendExportSymbols(symbols, nestedSymbols); } - function writeUnionOrIntersectionType(type, flags) { - if (flags & 64 /* InElementType */) { - writePunctuation(writer, 17 /* OpenParenToken */); - } - if (type.flags & 524288 /* Union */) { - writeTypeList(formatUnionTypes(type.types), 47 /* BarToken */); - } - else { - writeTypeList(type.types, 46 /* AmpersandToken */); + return symbols; + } + } + function getMergedSymbol(symbol) { + var merged; + return symbol && symbol.mergeId && (merged = mergedSymbols[symbol.mergeId]) ? merged : symbol; + } + function getSymbolOfNode(node) { + return getMergedSymbol(node.symbol); + } + function getParentOfSymbol(symbol) { + return getMergedSymbol(symbol.parent); + } + function getExportSymbolOfValueSymbolIfExported(symbol) { + return symbol && (symbol.flags & 1048576 /* ExportValue */) !== 0 + ? getMergedSymbol(symbol.exportSymbol) + : symbol; + } + function symbolIsValue(symbol) { + // If it is an instantiated symbol, then it is a value if the symbol it is an + // instantiation of is a value. + if (symbol.flags & 16777216 /* Instantiated */) { + return symbolIsValue(getSymbolLinks(symbol).target); + } + // If the symbol has the value flag, it is trivially a value. + if (symbol.flags & 107455 /* Value */) { + return true; + } + // If it is an alias, then it is a value if the symbol it resolves to is a value. + if (symbol.flags & 8388608 /* Alias */) { + return (resolveAlias(symbol).flags & 107455 /* Value */) !== 0; + } + return false; + } + function findConstructorDeclaration(node) { + var members = node.members; + for (var _i = 0, members_1 = members; _i < members_1.length; _i++) { + var member = members_1[_i]; + if (member.kind === 148 /* Constructor */ && ts.nodeIsPresent(member.body)) { + return member; + } + } + } + function createType(flags) { + var result = new Type(checker, flags); + typeCount++; + result.id = typeCount; + return result; + } + function createIntrinsicType(kind, intrinsicName) { + var type = createType(kind); + type.intrinsicName = intrinsicName; + return type; + } + function createBooleanType(trueFalseTypes) { + var type = getUnionType(trueFalseTypes); + type.flags |= 8 /* Boolean */; + type.intrinsicName = "boolean"; + return type; + } + function createObjectType(kind, symbol) { + var type = createType(kind); + type.symbol = symbol; + return type; + } + // A reserved member name starts with two underscores, but the third character cannot be an underscore + // or the @ symbol. A third underscore indicates an escaped form of an identifer that started + // with at least two underscores. The @ character indicates that the name is denoted by a well known ES + // Symbol instance. + function isReservedMemberName(name) { + return name.charCodeAt(0) === 95 /* _ */ && + name.charCodeAt(1) === 95 /* _ */ && + name.charCodeAt(2) !== 95 /* _ */ && + name.charCodeAt(2) !== 64 /* at */; + } + function getNamedMembers(members) { + var result; + for (var id in members) { + if (!isReservedMemberName(id)) { + if (!result) + result = []; + var symbol = members[id]; + if (symbolIsValue(symbol)) { + result.push(symbol); } - if (flags & 64 /* InElementType */) { - writePunctuation(writer, 18 /* CloseParenToken */); + } + } + return result || emptyArray; + } + function setObjectTypeMembers(type, members, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo) { + type.members = members; + type.properties = getNamedMembers(members); + type.callSignatures = callSignatures; + type.constructSignatures = constructSignatures; + if (stringIndexInfo) + type.stringIndexInfo = stringIndexInfo; + if (numberIndexInfo) + type.numberIndexInfo = numberIndexInfo; + return type; + } + function createAnonymousType(symbol, members, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo) { + return setObjectTypeMembers(createObjectType(2097152 /* Anonymous */, symbol), members, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo); + } + function forEachSymbolTableInScope(enclosingDeclaration, callback) { + var result; + for (var location_1 = enclosingDeclaration; location_1; location_1 = location_1.parent) { + // Locals of a source file are not in scope (because they get merged into the global symbol table) + if (location_1.locals && !isGlobalSourceFile(location_1)) { + if (result = callback(location_1.locals)) { + return result; } } - function writeAnonymousType(type, flags) { - var symbol = type.symbol; - if (symbol) { - // Always use 'typeof T' for type of class, enum, and module objects - if (symbol.flags & (32 /* Class */ | 384 /* Enum */ | 512 /* ValueModule */)) { - writeTypeOfSymbol(type, flags); - } - else if (shouldWriteTypeOfFunctionSymbol()) { - writeTypeOfSymbol(type, flags); - } - else if (ts.contains(symbolStack, symbol)) { - // If type is an anonymous type literal in a type alias declaration, use type alias name - var typeAlias = getTypeAliasForTypeLiteral(type); - if (typeAlias) { - // The specified symbol flags need to be reinterpreted as type flags - buildSymbolDisplay(typeAlias, writer, enclosingDeclaration, 793064 /* Type */, 0 /* None */, flags); - } - else { - // Recursive usage, use any - writeKeyword(writer, 117 /* AnyKeyword */); - } - } - else { - // Since instantiations of the same anonymous type have the same symbol, tracking symbols instead - // of types allows us to catch circular references to instantiations of the same anonymous type - if (!symbolStack) { - symbolStack = []; - } - symbolStack.push(symbol); - writeLiteralType(type, flags); - symbolStack.pop(); + switch (location_1.kind) { + case 256 /* SourceFile */: + if (!ts.isExternalOrCommonJsModule(location_1)) { + break; } - } - else { - // Anonymous types with no symbol are never circular - writeLiteralType(type, flags); - } - function shouldWriteTypeOfFunctionSymbol() { - var isStaticMethodSymbol = !!(symbol.flags & 8192 /* Method */ && - ts.forEach(symbol.declarations, function (declaration) { return ts.getModifierFlags(declaration) & 32 /* Static */; })); - var isNonLocalFunctionSymbol = !!(symbol.flags & 16 /* Function */) && - (symbol.parent || - ts.forEach(symbol.declarations, function (declaration) { - return declaration.parent.kind === 256 /* SourceFile */ || declaration.parent.kind === 226 /* ModuleBlock */; - })); - if (isStaticMethodSymbol || isNonLocalFunctionSymbol) { - // typeof is allowed only for static/non local functions - return !!(flags & 2 /* UseTypeOfFunction */) || - (ts.contains(symbolStack, symbol)); // it is type of the symbol uses itself recursively + case 225 /* ModuleDeclaration */: + if (result = callback(getSymbolOfNode(location_1).exports)) { + return result; } - } - } - function writeTypeOfSymbol(type, typeFormatFlags) { - writeKeyword(writer, 101 /* TypeOfKeyword */); - writeSpace(writer); - buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, 107455 /* Value */, 0 /* None */, typeFormatFlags); + break; } - function writeIndexSignature(info, keyword) { - if (info) { - if (info.isReadonly) { - writeKeyword(writer, 128 /* ReadonlyKeyword */); - writeSpace(writer); - } - writePunctuation(writer, 19 /* OpenBracketToken */); - writer.writeParameter(info.declaration ? ts.declarationNameToString(info.declaration.parameters[0].name) : "x"); - writePunctuation(writer, 54 /* ColonToken */); - writeSpace(writer); - writeKeyword(writer, keyword); - writePunctuation(writer, 20 /* CloseBracketToken */); - writePunctuation(writer, 54 /* ColonToken */); - writeSpace(writer); - writeType(info.type, 0 /* None */); - writePunctuation(writer, 23 /* SemicolonToken */); - writer.writeLine(); + } + return callback(globals); + } + function getQualifiedLeftMeaning(rightMeaning) { + // If we are looking in value space, the parent meaning is value, other wise it is namespace + return rightMeaning === 107455 /* Value */ ? 107455 /* Value */ : 1920 /* Namespace */; + } + function getAccessibleSymbolChain(symbol, enclosingDeclaration, meaning, useOnlyExternalAliasing) { + function getAccessibleSymbolChainFromSymbolTable(symbols) { + function canQualifySymbol(symbolFromSymbolTable, meaning) { + // If the symbol is equivalent and doesn't need further qualification, this symbol is accessible + if (!needsQualification(symbolFromSymbolTable, enclosingDeclaration, meaning)) { + return true; } + // If symbol needs qualification, make sure that parent is accessible, if it is then this symbol is accessible too + var accessibleParent = getAccessibleSymbolChain(symbolFromSymbolTable.parent, enclosingDeclaration, getQualifiedLeftMeaning(meaning), useOnlyExternalAliasing); + return !!accessibleParent; } - function writePropertyWithModifiers(prop) { - if (isReadonlySymbol(prop)) { - writeKeyword(writer, 128 /* ReadonlyKeyword */); - writeSpace(writer); - } - buildSymbolDisplay(prop, writer); - if (prop.flags & 536870912 /* Optional */) { - writePunctuation(writer, 53 /* QuestionToken */); + function isAccessible(symbolFromSymbolTable, resolvedAliasSymbol) { + if (symbol === (resolvedAliasSymbol || symbolFromSymbolTable)) { + // if the symbolFromSymbolTable is not external module (it could be if it was determined as ambient external module and would be in globals table) + // and if symbolFromSymbolTable or alias resolution matches the symbol, + // check the symbol can be qualified, it is only then this symbol is accessible + return !ts.forEach(symbolFromSymbolTable.declarations, hasExternalModuleSymbol) && + canQualifySymbol(symbolFromSymbolTable, meaning); } } - function shouldAddParenthesisAroundFunctionType(callSignature, flags) { - if (flags & 64 /* InElementType */) { - return true; - } - else if (flags & 256 /* InFirstTypeArgument */) { - // Add parenthesis around function type for the first type argument to avoid ambiguity - var typeParameters = callSignature.target && (flags & 32 /* WriteTypeArgumentsOfSignature */) ? - callSignature.target.typeParameters : callSignature.typeParameters; - return typeParameters && typeParameters.length !== 0; - } - return false; + // If symbol is directly available by its name in the symbol table + if (isAccessible(symbols[symbol.name])) { + return [symbol]; } - function writeLiteralType(type, flags) { - var resolved = resolveStructuredTypeMembers(type); - if (!resolved.properties.length && !resolved.stringIndexInfo && !resolved.numberIndexInfo) { - if (!resolved.callSignatures.length && !resolved.constructSignatures.length) { - writePunctuation(writer, 15 /* OpenBraceToken */); - writePunctuation(writer, 16 /* CloseBraceToken */); - return; - } - if (resolved.callSignatures.length === 1 && !resolved.constructSignatures.length) { - var parenthesizeSignature = shouldAddParenthesisAroundFunctionType(resolved.callSignatures[0], flags); - if (parenthesizeSignature) { - writePunctuation(writer, 17 /* OpenParenToken */); - } - buildSignatureDisplay(resolved.callSignatures[0], writer, enclosingDeclaration, globalFlagsToPass | 8 /* WriteArrowStyleSignature */, /*kind*/ undefined, symbolStack); - if (parenthesizeSignature) { - writePunctuation(writer, 18 /* CloseParenToken */); - } - return; - } - if (resolved.constructSignatures.length === 1 && !resolved.callSignatures.length) { - if (flags & 64 /* InElementType */) { - writePunctuation(writer, 17 /* OpenParenToken */); - } - writeKeyword(writer, 92 /* NewKeyword */); - writeSpace(writer); - buildSignatureDisplay(resolved.constructSignatures[0], writer, enclosingDeclaration, globalFlagsToPass | 8 /* WriteArrowStyleSignature */, /*kind*/ undefined, symbolStack); - if (flags & 64 /* InElementType */) { - writePunctuation(writer, 18 /* CloseParenToken */); + // Check if symbol is any of the alias + return ts.forEachProperty(symbols, function (symbolFromSymbolTable) { + if (symbolFromSymbolTable.flags & 8388608 /* Alias */ + && symbolFromSymbolTable.name !== "export=" + && !ts.getDeclarationOfKind(symbolFromSymbolTable, 238 /* ExportSpecifier */)) { + if (!useOnlyExternalAliasing || + // Is this external alias, then use it to name + ts.forEach(symbolFromSymbolTable.declarations, ts.isExternalModuleImportEqualsDeclaration)) { + var resolvedImportedSymbol = resolveAlias(symbolFromSymbolTable); + if (isAccessible(symbolFromSymbolTable, resolveAlias(symbolFromSymbolTable))) { + return [symbolFromSymbolTable]; } - return; - } - } - var saveInObjectTypeLiteral = inObjectTypeLiteral; - inObjectTypeLiteral = true; - writePunctuation(writer, 15 /* OpenBraceToken */); - writer.writeLine(); - writer.increaseIndent(); - for (var _i = 0, _a = resolved.callSignatures; _i < _a.length; _i++) { - var signature = _a[_i]; - buildSignatureDisplay(signature, writer, enclosingDeclaration, globalFlagsToPass, /*kind*/ undefined, symbolStack); - writePunctuation(writer, 23 /* SemicolonToken */); - writer.writeLine(); - } - for (var _b = 0, _c = resolved.constructSignatures; _b < _c.length; _b++) { - var signature = _c[_b]; - buildSignatureDisplay(signature, writer, enclosingDeclaration, globalFlagsToPass, 1 /* Construct */, symbolStack); - writePunctuation(writer, 23 /* SemicolonToken */); - writer.writeLine(); - } - writeIndexSignature(resolved.stringIndexInfo, 132 /* StringKeyword */); - writeIndexSignature(resolved.numberIndexInfo, 130 /* NumberKeyword */); - for (var _d = 0, _e = resolved.properties; _d < _e.length; _d++) { - var p = _e[_d]; - var t = getTypeOfSymbol(p); - if (p.flags & (16 /* Function */ | 8192 /* Method */) && !getPropertiesOfObjectType(t).length) { - var signatures = getSignaturesOfType(t, 0 /* Call */); - for (var _f = 0, signatures_1 = signatures; _f < signatures_1.length; _f++) { - var signature = signatures_1[_f]; - writePropertyWithModifiers(p); - buildSignatureDisplay(signature, writer, enclosingDeclaration, globalFlagsToPass, /*kind*/ undefined, symbolStack); - writePunctuation(writer, 23 /* SemicolonToken */); - writer.writeLine(); + // Look in the exported members, if we can find accessibleSymbolChain, symbol is accessible using this chain + // but only if the symbolFromSymbolTable can be qualified + var accessibleSymbolsFromExports = resolvedImportedSymbol.exports ? getAccessibleSymbolChainFromSymbolTable(resolvedImportedSymbol.exports) : undefined; + if (accessibleSymbolsFromExports && canQualifySymbol(symbolFromSymbolTable, getQualifiedLeftMeaning(meaning))) { + return [symbolFromSymbolTable].concat(accessibleSymbolsFromExports); } } - else { - writePropertyWithModifiers(p); - writePunctuation(writer, 54 /* ColonToken */); - writeSpace(writer); - writeType(t, 0 /* None */); - writePunctuation(writer, 23 /* SemicolonToken */); - writer.writeLine(); - } } - writer.decreaseIndent(); - writePunctuation(writer, 16 /* CloseBraceToken */); - inObjectTypeLiteral = saveInObjectTypeLiteral; - } - } - function buildTypeParameterDisplayFromSymbol(symbol, writer, enclosingDeclaration, flags) { - var targetSymbol = getTargetSymbol(symbol); - if (targetSymbol.flags & 32 /* Class */ || targetSymbol.flags & 64 /* Interface */ || targetSymbol.flags & 524288 /* TypeAlias */) { - buildDisplayForTypeParametersAndDelimiters(getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol), writer, enclosingDeclaration, flags); - } + }); } - function buildTypeParameterDisplay(tp, writer, enclosingDeclaration, flags, symbolStack) { - appendSymbolNameOnly(tp.symbol, writer); - var constraint = getConstraintOfTypeParameter(tp); - if (constraint) { - writeSpace(writer); - writeKeyword(writer, 83 /* ExtendsKeyword */); - writeSpace(writer); - buildTypeDisplay(constraint, writer, enclosingDeclaration, flags, symbolStack); + if (symbol) { + if (!(isPropertyOrMethodDeclarationSymbol(symbol))) { + return forEachSymbolTableInScope(enclosingDeclaration, getAccessibleSymbolChainFromSymbolTable); } } - function buildParameterDisplay(p, writer, enclosingDeclaration, flags, symbolStack) { - var parameterNode = p.valueDeclaration; - if (ts.isRestParameter(parameterNode)) { - writePunctuation(writer, 22 /* DotDotDotToken */); - } - if (ts.isBindingPattern(parameterNode.name)) { - buildBindingPatternDisplay(parameterNode.name, writer, enclosingDeclaration, flags, symbolStack); - } - else { - appendSymbolNameOnly(p, writer); + } + function needsQualification(symbol, enclosingDeclaration, meaning) { + var qualify = false; + forEachSymbolTableInScope(enclosingDeclaration, function (symbolTable) { + // If symbol of this name is not available in the symbol table we are ok + var symbolFromSymbolTable = symbolTable[symbol.name]; + if (!symbolFromSymbolTable) { + // Continue to the next symbol table + return false; } - if (isOptionalParameter(parameterNode)) { - writePunctuation(writer, 53 /* QuestionToken */); + // If the symbol with this name is present it should refer to the symbol + if (symbolFromSymbolTable === symbol) { + // No need to qualify + return true; } - writePunctuation(writer, 54 /* ColonToken */); - writeSpace(writer); - buildTypeDisplay(getTypeOfSymbol(p), writer, enclosingDeclaration, flags, symbolStack); - } - function buildBindingPatternDisplay(bindingPattern, writer, enclosingDeclaration, flags, symbolStack) { - // We have to explicitly emit square bracket and bracket because these tokens are not stored inside the node. - if (bindingPattern.kind === 167 /* ObjectBindingPattern */) { - writePunctuation(writer, 15 /* OpenBraceToken */); - buildDisplayForCommaSeparatedList(bindingPattern.elements, writer, function (e) { return buildBindingElementDisplay(e, writer, enclosingDeclaration, flags, symbolStack); }); - writePunctuation(writer, 16 /* CloseBraceToken */); + // Qualify if the symbol from symbol table has same meaning as expected + symbolFromSymbolTable = (symbolFromSymbolTable.flags & 8388608 /* Alias */ && !ts.getDeclarationOfKind(symbolFromSymbolTable, 238 /* ExportSpecifier */)) ? resolveAlias(symbolFromSymbolTable) : symbolFromSymbolTable; + if (symbolFromSymbolTable.flags & meaning) { + qualify = true; + return true; } - else if (bindingPattern.kind === 168 /* ArrayBindingPattern */) { - writePunctuation(writer, 19 /* OpenBracketToken */); - var elements = bindingPattern.elements; - buildDisplayForCommaSeparatedList(elements, writer, function (e) { return buildBindingElementDisplay(e, writer, enclosingDeclaration, flags, symbolStack); }); - if (elements && elements.hasTrailingComma) { - writePunctuation(writer, 24 /* CommaToken */); + // Continue to the next symbol table + return false; + }); + return qualify; + } + function isPropertyOrMethodDeclarationSymbol(symbol) { + if (symbol.declarations && symbol.declarations.length) { + for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { + var declaration = _a[_i]; + switch (declaration.kind) { + case 145 /* PropertyDeclaration */: + case 147 /* MethodDeclaration */: + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + continue; + default: + return false; } - writePunctuation(writer, 20 /* CloseBracketToken */); } + return true; } - function buildBindingElementDisplay(bindingElement, writer, enclosingDeclaration, flags, symbolStack) { - if (ts.isOmittedExpression(bindingElement)) { - return; - } - ts.Debug.assert(bindingElement.kind === 169 /* BindingElement */); - if (bindingElement.propertyName) { - writer.writeSymbol(ts.getTextOfNode(bindingElement.propertyName), bindingElement.symbol); - writePunctuation(writer, 54 /* ColonToken */); - writeSpace(writer); - } - if (ts.isBindingPattern(bindingElement.name)) { - buildBindingPatternDisplay(bindingElement.name, writer, enclosingDeclaration, flags, symbolStack); - } - else { - if (bindingElement.dotDotDotToken) { - writePunctuation(writer, 22 /* DotDotDotToken */); - } - appendSymbolNameOnly(bindingElement.symbol, writer); - } - } - function buildDisplayForTypeParametersAndDelimiters(typeParameters, writer, enclosingDeclaration, flags, symbolStack) { - if (typeParameters && typeParameters.length) { - writePunctuation(writer, 25 /* LessThanToken */); - buildDisplayForCommaSeparatedList(typeParameters, writer, function (p) { return buildTypeParameterDisplay(p, writer, enclosingDeclaration, flags, symbolStack); }); - writePunctuation(writer, 27 /* GreaterThanToken */); - } - } - function buildDisplayForCommaSeparatedList(list, writer, action) { - for (var i = 0; i < list.length; i++) { - if (i > 0) { - writePunctuation(writer, 24 /* CommaToken */); - writeSpace(writer); - } - action(list[i]); - } - } - function buildDisplayForTypeArgumentsAndDelimiters(typeParameters, mapper, writer, enclosingDeclaration, flags, symbolStack) { - if (typeParameters && typeParameters.length) { - writePunctuation(writer, 25 /* LessThanToken */); - var flags_1 = 256 /* InFirstTypeArgument */; - for (var i = 0; i < typeParameters.length; i++) { - if (i > 0) { - writePunctuation(writer, 24 /* CommaToken */); - writeSpace(writer); - flags_1 = 0 /* None */; + return false; + } + /** + * Check if the given symbol in given enclosing declaration is accessible and mark all associated alias to be visible if requested + * + * @param symbol a Symbol to check if accessible + * @param enclosingDeclaration a Node containing reference to the symbol + * @param meaning a SymbolFlags to check if such meaning of the symbol is accessible + * @param shouldComputeAliasToMakeVisible a boolean value to indicate whether to return aliases to be mark visible in case the symbol is accessible + */ + function isSymbolAccessible(symbol, enclosingDeclaration, meaning, shouldComputeAliasesToMakeVisible) { + if (symbol && enclosingDeclaration && !(symbol.flags & 262144 /* TypeParameter */)) { + var initialSymbol = symbol; + var meaningToLook = meaning; + while (symbol) { + // Symbol is accessible if it by itself is accessible + var accessibleSymbolChain = getAccessibleSymbolChain(symbol, enclosingDeclaration, meaningToLook, /*useOnlyExternalAliasing*/ false); + if (accessibleSymbolChain) { + var hasAccessibleDeclarations = hasVisibleDeclarations(accessibleSymbolChain[0], shouldComputeAliasesToMakeVisible); + if (!hasAccessibleDeclarations) { + return { + accessibility: 1 /* NotAccessible */, + errorSymbolName: symbolToString(initialSymbol, enclosingDeclaration, meaning), + errorModuleName: symbol !== initialSymbol ? symbolToString(symbol, enclosingDeclaration, 1920 /* Namespace */) : undefined + }; } - buildTypeDisplay(mapper(typeParameters[i]), writer, enclosingDeclaration, flags_1); + return hasAccessibleDeclarations; } - writePunctuation(writer, 27 /* GreaterThanToken */); - } - } - function buildDisplayForParametersAndDelimiters(thisParameter, parameters, writer, enclosingDeclaration, flags, symbolStack) { - writePunctuation(writer, 17 /* OpenParenToken */); - if (thisParameter) { - buildParameterDisplay(thisParameter, writer, enclosingDeclaration, flags, symbolStack); + // If we haven't got the accessible symbol, it doesn't mean the symbol is actually inaccessible. + // It could be a qualified symbol and hence verify the path + // e.g.: + // module m { + // export class c { + // } + // } + // const x: typeof m.c + // In the above example when we start with checking if typeof m.c symbol is accessible, + // we are going to see if c can be accessed in scope directly. + // But it can't, hence the accessible is going to be undefined, but that doesn't mean m.c is inaccessible + // It is accessible if the parent m is accessible because then m.c can be accessed through qualification + meaningToLook = getQualifiedLeftMeaning(meaning); + symbol = getParentOfSymbol(symbol); } - for (var i = 0; i < parameters.length; i++) { - if (i > 0 || thisParameter) { - writePunctuation(writer, 24 /* CommaToken */); - writeSpace(writer); + // This could be a symbol that is not exported in the external module + // or it could be a symbol from different external module that is not aliased and hence cannot be named + var symbolExternalModule = ts.forEach(initialSymbol.declarations, getExternalModuleContainer); + if (symbolExternalModule) { + var enclosingExternalModule = getExternalModuleContainer(enclosingDeclaration); + if (symbolExternalModule !== enclosingExternalModule) { + // name from different external module that is not visible + return { + accessibility: 2 /* CannotBeNamed */, + errorSymbolName: symbolToString(initialSymbol, enclosingDeclaration, meaning), + errorModuleName: symbolToString(symbolExternalModule) + }; } - buildParameterDisplay(parameters[i], writer, enclosingDeclaration, flags, symbolStack); - } - writePunctuation(writer, 18 /* CloseParenToken */); - } - function buildTypePredicateDisplay(predicate, writer, enclosingDeclaration, flags, symbolStack) { - if (ts.isIdentifierTypePredicate(predicate)) { - writer.writeParameter(predicate.parameterName); - } - else { - writeKeyword(writer, 97 /* ThisKeyword */); - } - writeSpace(writer); - writeKeyword(writer, 124 /* IsKeyword */); - writeSpace(writer); - buildTypeDisplay(predicate.type, writer, enclosingDeclaration, flags, symbolStack); - } - function buildReturnTypeDisplay(signature, writer, enclosingDeclaration, flags, symbolStack) { - if (flags & 8 /* WriteArrowStyleSignature */) { - writeSpace(writer); - writePunctuation(writer, 34 /* EqualsGreaterThanToken */); - } - else { - writePunctuation(writer, 54 /* ColonToken */); - } - writeSpace(writer); - if (signature.typePredicate) { - buildTypePredicateDisplay(signature.typePredicate, writer, enclosingDeclaration, flags, symbolStack); - } - else { - var returnType = getReturnTypeOfSignature(signature); - buildTypeDisplay(returnType, writer, enclosingDeclaration, flags, symbolStack); } + // Just a local name that is not accessible + return { + accessibility: 1 /* NotAccessible */, + errorSymbolName: symbolToString(initialSymbol, enclosingDeclaration, meaning) + }; } - function buildSignatureDisplay(signature, writer, enclosingDeclaration, flags, kind, symbolStack) { - if (kind === 1 /* Construct */) { - writeKeyword(writer, 92 /* NewKeyword */); - writeSpace(writer); - } - if (signature.target && (flags & 32 /* WriteTypeArgumentsOfSignature */)) { - // Instantiated signature, write type arguments instead - // This is achieved by passing in the mapper separately - buildDisplayForTypeArgumentsAndDelimiters(signature.target.typeParameters, signature.mapper, writer, enclosingDeclaration); - } - else { - buildDisplayForTypeParametersAndDelimiters(signature.typeParameters, writer, enclosingDeclaration, flags, symbolStack); + return { accessibility: 0 /* Accessible */ }; + function getExternalModuleContainer(declaration) { + for (; declaration; declaration = declaration.parent) { + if (hasExternalModuleSymbol(declaration)) { + return getSymbolOfNode(declaration); + } } - buildDisplayForParametersAndDelimiters(signature.thisParameter, signature.parameters, writer, enclosingDeclaration, flags, symbolStack); - buildReturnTypeDisplay(signature, writer, enclosingDeclaration, flags, symbolStack); } - return _displayBuilder || (_displayBuilder = { - buildSymbolDisplay: buildSymbolDisplay, - buildTypeDisplay: buildTypeDisplay, - buildTypeParameterDisplay: buildTypeParameterDisplay, - buildTypePredicateDisplay: buildTypePredicateDisplay, - buildParameterDisplay: buildParameterDisplay, - buildDisplayForParametersAndDelimiters: buildDisplayForParametersAndDelimiters, - buildDisplayForTypeParametersAndDelimiters: buildDisplayForTypeParametersAndDelimiters, - buildTypeParameterDisplayFromSymbol: buildTypeParameterDisplayFromSymbol, - buildSignatureDisplay: buildSignatureDisplay, - buildReturnTypeDisplay: buildReturnTypeDisplay - }); } - function isDeclarationVisible(node) { - if (node) { - var links = getNodeLinks(node); - if (links.isVisible === undefined) { - links.isVisible = !!determineIfDeclarationIsVisible(); - } - return links.isVisible; + function hasExternalModuleSymbol(declaration) { + return ts.isAmbientModule(declaration) || (declaration.kind === 256 /* SourceFile */ && ts.isExternalOrCommonJsModule(declaration)); + } + function hasVisibleDeclarations(symbol, shouldComputeAliasToMakeVisible) { + var aliasesToMakeVisible; + if (ts.forEach(symbol.declarations, function (declaration) { return !getIsDeclarationVisible(declaration); })) { + return undefined; } - return false; - function determineIfDeclarationIsVisible() { - switch (node.kind) { - case 169 /* BindingElement */: - return isDeclarationVisible(node.parent.parent); - case 218 /* VariableDeclaration */: - if (ts.isBindingPattern(node.name) && - !node.name.elements.length) { - // If the binding pattern is empty, this variable declaration is not visible - return false; - } - // Otherwise fall through - case 225 /* ModuleDeclaration */: - case 221 /* ClassDeclaration */: - case 222 /* InterfaceDeclaration */: - case 223 /* TypeAliasDeclaration */: - case 220 /* FunctionDeclaration */: - case 224 /* EnumDeclaration */: - case 229 /* ImportEqualsDeclaration */: - // external module augmentation is always visible - if (ts.isExternalModuleAugmentation(node)) { - return true; - } - var parent_9 = getDeclarationContainer(node); - // If the node is not exported or it is not ambient module element (except import declaration) - if (!(ts.getCombinedModifierFlags(node) & 1 /* Export */) && - !(node.kind !== 229 /* ImportEqualsDeclaration */ && parent_9.kind !== 256 /* SourceFile */ && ts.isInAmbientContext(parent_9))) { - return isGlobalSourceFile(parent_9); - } - // Exported members/ambient module elements (exception import declaration) are visible if parent is visible - return isDeclarationVisible(parent_9); - case 145 /* PropertyDeclaration */: - case 144 /* PropertySignature */: - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - case 147 /* MethodDeclaration */: - case 146 /* MethodSignature */: - if (ts.getModifierFlags(node) & (8 /* Private */ | 16 /* Protected */)) { - // Private/protected properties/methods are not visible - return false; + return { accessibility: 0 /* Accessible */, aliasesToMakeVisible: aliasesToMakeVisible }; + function getIsDeclarationVisible(declaration) { + if (!isDeclarationVisible(declaration)) { + // Mark the unexported alias as visible if its parent is visible + // because these kind of aliases can be used to name types in declaration file + var anyImportSyntax = getAnyImportSyntax(declaration); + if (anyImportSyntax && + !(ts.getModifierFlags(anyImportSyntax) & 1 /* Export */) && + isDeclarationVisible(anyImportSyntax.parent)) { + // In function "buildTypeDisplay" where we decide whether to write type-alias or serialize types, + // we want to just check if type- alias is accessible or not but we don't care about emitting those alias at that time + // since we will do the emitting later in trackSymbol. + if (shouldComputeAliasToMakeVisible) { + getNodeLinks(declaration).isVisible = true; + if (aliasesToMakeVisible) { + if (!ts.contains(aliasesToMakeVisible, anyImportSyntax)) { + aliasesToMakeVisible.push(anyImportSyntax); + } + } + else { + aliasesToMakeVisible = [anyImportSyntax]; + } } - // Public properties/methods are visible if its parents are visible, so const it fall into next case statement - case 148 /* Constructor */: - case 152 /* ConstructSignature */: - case 151 /* CallSignature */: - case 153 /* IndexSignature */: - case 142 /* Parameter */: - case 226 /* ModuleBlock */: - case 156 /* FunctionType */: - case 157 /* ConstructorType */: - case 159 /* TypeLiteral */: - case 155 /* TypeReference */: - case 160 /* ArrayType */: - case 161 /* TupleType */: - case 162 /* UnionType */: - case 163 /* IntersectionType */: - case 164 /* ParenthesizedType */: - return isDeclarationVisible(node.parent); - // Default binding, import specifier and namespace import is visible - // only on demand so by default it is not visible - case 231 /* ImportClause */: - case 232 /* NamespaceImport */: - case 234 /* ImportSpecifier */: - return false; - // Type parameters are always visible - case 141 /* TypeParameter */: - // Source file and namespace export are always visible - case 256 /* SourceFile */: - case 228 /* NamespaceExportDeclaration */: return true; - // Export assignments do not create name bindings outside the module - case 235 /* ExportAssignment */: - return false; - default: - return false; + } + // Declaration is not visible + return false; } + return true; } } - function collectLinkedAliases(node) { - var exportSymbol; - if (node.parent && node.parent.kind === 235 /* ExportAssignment */) { - exportSymbol = resolveName(node.parent, node.text, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */ | 8388608 /* Alias */, ts.Diagnostics.Cannot_find_name_0, node); + function isEntityNameVisible(entityName, enclosingDeclaration) { + // get symbol of the first identifier of the entityName + var meaning; + if (entityName.parent.kind === 158 /* TypeQuery */ || ts.isExpressionWithTypeArgumentsInClassExtendsClause(entityName.parent)) { + // Typeof value + meaning = 107455 /* Value */ | 1048576 /* ExportValue */; } - else if (node.parent.kind === 238 /* ExportSpecifier */) { - var exportSpecifier = node.parent; - exportSymbol = exportSpecifier.parent.parent.moduleSpecifier ? - getExternalModuleMember(exportSpecifier.parent.parent, exportSpecifier) : - resolveEntityName(exportSpecifier.propertyName || exportSpecifier.name, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */ | 8388608 /* Alias */); + else if (entityName.kind === 139 /* QualifiedName */ || entityName.kind === 172 /* PropertyAccessExpression */ || + entityName.parent.kind === 229 /* ImportEqualsDeclaration */) { + // Left identifier from type reference or TypeAlias + // Entity name of the import declaration + meaning = 1920 /* Namespace */; } - var result = []; - if (exportSymbol) { - buildVisibleNodeList(exportSymbol.declarations); + else { + // Type Reference or TypeAlias entity = Identifier + meaning = 793064 /* Type */; } + var firstIdentifier = getFirstIdentifier(entityName); + var symbol = resolveName(enclosingDeclaration, firstIdentifier.text, meaning, /*nodeNotFoundErrorMessage*/ undefined, /*nameArg*/ undefined); + // Verify if the symbol is accessible + return (symbol && hasVisibleDeclarations(symbol, /*shouldComputeAliasToMakeVisible*/ true)) || { + accessibility: 1 /* NotAccessible */, + errorSymbolName: ts.getTextOfNode(firstIdentifier), + errorNode: firstIdentifier + }; + } + function writeKeyword(writer, kind) { + writer.writeKeyword(ts.tokenToString(kind)); + } + function writePunctuation(writer, kind) { + writer.writePunctuation(ts.tokenToString(kind)); + } + function writeSpace(writer) { + writer.writeSpace(" "); + } + function symbolToString(symbol, enclosingDeclaration, meaning) { + var writer = ts.getSingleLineStringWriter(); + getSymbolDisplayBuilder().buildSymbolDisplay(symbol, writer, enclosingDeclaration, meaning); + var result = writer.string(); + ts.releaseStringWriter(writer); return result; - function buildVisibleNodeList(declarations) { - ts.forEach(declarations, function (declaration) { - getNodeLinks(declaration).isVisible = true; - var resultNode = getAnyImportSyntax(declaration) || declaration; - if (!ts.contains(result, resultNode)) { - result.push(resultNode); - } - if (ts.isInternalModuleImportEqualsDeclaration(declaration)) { - // Add the referenced top container visible - var internalModuleReference = declaration.moduleReference; - var firstIdentifier = getFirstIdentifier(internalModuleReference); - var importSymbol = resolveName(declaration, firstIdentifier.text, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */, undefined, undefined); - if (importSymbol) { - buildVisibleNodeList(importSymbol.declarations); - } - } - }); - } } - /** - * Push an entry on the type resolution stack. If an entry with the given target and the given property name - * is already on the stack, and no entries in between already have a type, then a circularity has occurred. - * In this case, the result values of the existing entry and all entries pushed after it are changed to false, - * and the value false is returned. Otherwise, the new entry is just pushed onto the stack, and true is returned. - * In order to see if the same query has already been done before, the target object and the propertyName both - * must match the one passed in. - * - * @param target The symbol, type, or signature whose type is being queried - * @param propertyName The property name that should be used to query the target for its type - */ - function pushTypeResolution(target, propertyName) { - var resolutionCycleStartIndex = findResolutionCycleStartIndex(target, propertyName); - if (resolutionCycleStartIndex >= 0) { - // A cycle was found - var length_2 = resolutionTargets.length; - for (var i = resolutionCycleStartIndex; i < length_2; i++) { - resolutionResults[i] = false; - } - return false; + function signatureToString(signature, enclosingDeclaration, flags, kind) { + var writer = ts.getSingleLineStringWriter(); + getSymbolDisplayBuilder().buildSignatureDisplay(signature, writer, enclosingDeclaration, flags, kind); + var result = writer.string(); + ts.releaseStringWriter(writer); + return result; + } + function typeToString(type, enclosingDeclaration, flags) { + var writer = ts.getSingleLineStringWriter(); + getSymbolDisplayBuilder().buildTypeDisplay(type, writer, enclosingDeclaration, flags); + var result = writer.string(); + ts.releaseStringWriter(writer); + var maxLength = compilerOptions.noErrorTruncation || flags & 4 /* NoTruncation */ ? undefined : 100; + if (maxLength && result.length >= maxLength) { + result = result.substr(0, maxLength - "...".length) + "..."; } - resolutionTargets.push(target); - resolutionResults.push(/*items*/ true); - resolutionPropertyNames.push(propertyName); - return true; + return result; } - function findResolutionCycleStartIndex(target, propertyName) { - for (var i = resolutionTargets.length - 1; i >= 0; i--) { - if (hasType(resolutionTargets[i], resolutionPropertyNames[i])) { - return -1; - } - if (resolutionTargets[i] === target && resolutionPropertyNames[i] === propertyName) { - return i; + function typePredicateToString(typePredicate, enclosingDeclaration, flags) { + var writer = ts.getSingleLineStringWriter(); + getSymbolDisplayBuilder().buildTypePredicateDisplay(typePredicate, writer, enclosingDeclaration, flags); + var result = writer.string(); + ts.releaseStringWriter(writer); + return result; + } + function formatUnionTypes(types) { + var result = []; + var flags = 0; + for (var i = 0; i < types.length; i++) { + var t = types[i]; + flags |= t.flags; + if (!(t.flags & 6144 /* Nullable */)) { + if (t.flags & (128 /* BooleanLiteral */ | 256 /* EnumLiteral */)) { + var baseType = t.flags & 128 /* BooleanLiteral */ ? booleanType : t.baseType; + var count = baseType.types.length; + if (i + count <= types.length && types[i + count - 1] === baseType.types[count - 1]) { + result.push(baseType); + i += count - 1; + continue; + } + } + result.push(t); } } - return -1; + if (flags & 4096 /* Null */) + result.push(nullType); + if (flags & 2048 /* Undefined */) + result.push(undefinedType); + return result || types; } - function hasType(target, propertyName) { - if (propertyName === 0 /* Type */) { - return getSymbolLinks(target).type; - } - if (propertyName === 2 /* DeclaredType */) { - return getSymbolLinks(target).declaredType; - } - if (propertyName === 1 /* ResolvedBaseConstructorType */) { - ts.Debug.assert(!!(target.flags & 32768 /* Class */)); - return target.resolvedBaseConstructorType; + function visibilityToString(flags) { + if (flags === 8 /* Private */) { + return "private"; } - if (propertyName === 3 /* ResolvedReturnType */) { - return target.resolvedReturnType; + if (flags === 16 /* Protected */) { + return "protected"; } - ts.Debug.fail("Unhandled TypeSystemPropertyName " + propertyName); - } - // Pop an entry from the type resolution stack and return its associated result value. The result value will - // be true if no circularities were detected, or false if a circularity was found. - function popTypeResolution() { - resolutionTargets.pop(); - resolutionPropertyNames.pop(); - return resolutionResults.pop(); + return "public"; } - function getDeclarationContainer(node) { - node = ts.getRootDeclaration(node); - while (node) { - switch (node.kind) { - case 218 /* VariableDeclaration */: - case 219 /* VariableDeclarationList */: - case 234 /* ImportSpecifier */: - case 233 /* NamedImports */: - case 232 /* NamespaceImport */: - case 231 /* ImportClause */: - node = node.parent; - break; - default: - return node.parent; + function getTypeAliasForTypeLiteral(type) { + if (type.symbol && type.symbol.flags & 2048 /* TypeLiteral */) { + var node = type.symbol.declarations[0].parent; + while (node.kind === 164 /* ParenthesizedType */) { + node = node.parent; + } + if (node.kind === 223 /* TypeAliasDeclaration */) { + return getSymbolOfNode(node); } } + return undefined; } - function getTypeOfPrototypeProperty(prototype) { - // TypeScript 1.0 spec (April 2014): 8.4 - // Every class automatically contains a static property member named 'prototype', - // the type of which is an instantiation of the class type with type Any supplied as a type argument for each type parameter. - // It is an error to explicitly declare a static property member with the name 'prototype'. - var classType = getDeclaredTypeOfSymbol(getParentOfSymbol(prototype)); - return classType.typeParameters ? createTypeReference(classType, ts.map(classType.typeParameters, function (_) { return anyType; })) : classType; - } - // Return the type of the given property in the given type, or undefined if no such property exists - function getTypeOfPropertyOfType(type, name) { - var prop = getPropertyOfType(type, name); - return prop ? getTypeOfSymbol(prop) : undefined; - } - function isTypeAny(type) { - return type && (type.flags & 1 /* Any */) !== 0; - } - function isTypeNever(type) { - return type && (type.flags & 8192 /* Never */) !== 0; + function isTopLevelInExternalModuleAugmentation(node) { + return node && node.parent && + node.parent.kind === 226 /* ModuleBlock */ && + ts.isExternalModuleAugmentation(node.parent.parent); } - // Return the type of a binding element parent. We check SymbolLinks first to see if a type has been - // assigned by contextual typing. - function getTypeForBindingElementParent(node) { - var symbol = getSymbolOfNode(node); - return symbol && getSymbolLinks(symbol).type || getTypeForVariableLikeDeclaration(node, /*includeOptionality*/ false); + function literalTypeToString(type) { + return type.flags & 32 /* StringLiteral */ ? "\"" + ts.escapeString(type.text) + "\"" : type.text; } - function getTextOfPropertyName(name) { - switch (name.kind) { - case 69 /* Identifier */: - return name.text; - case 9 /* StringLiteral */: - case 8 /* NumericLiteral */: - return name.text; - case 140 /* ComputedPropertyName */: - if (ts.isStringOrNumericLiteral(name.expression.kind)) { - return name.expression.text; + function getSymbolDisplayBuilder() { + function getNameOfSymbol(symbol) { + if (symbol.declarations && symbol.declarations.length) { + var declaration = symbol.declarations[0]; + if (declaration.name) { + return ts.declarationNameToString(declaration.name); + } + switch (declaration.kind) { + case 192 /* ClassExpression */: + return "(Anonymous class)"; + case 179 /* FunctionExpression */: + case 180 /* ArrowFunction */: + return "(Anonymous function)"; } - } - return undefined; - } - function isComputedNonLiteralName(name) { - return name.kind === 140 /* ComputedPropertyName */ && !ts.isStringOrNumericLiteral(name.expression.kind); - } - /** Return the inferred type for a binding element */ - function getTypeForBindingElement(declaration) { - var pattern = declaration.parent; - var parentType = getTypeForBindingElementParent(pattern.parent); - // If parent has the unknown (error) type, then so does this binding element - if (parentType === unknownType) { - return unknownType; - } - // If no type was specified or inferred for parent, or if the specified or inferred type is any, - // infer from the initializer of the binding element if one is present. Otherwise, go with the - // undefined or any type of the parent. - if (!parentType || isTypeAny(parentType)) { - if (declaration.initializer) { - return checkDeclarationInitializer(declaration); } - return parentType; + return symbol.name; } - var type; - if (pattern.kind === 167 /* ObjectBindingPattern */) { - // Use explicitly specified property name ({ p: xxx } form), or otherwise the implied name ({ p } form) - var name_14 = declaration.propertyName || declaration.name; - if (isComputedNonLiteralName(name_14)) { - // computed properties with non-literal names are treated as 'any' - return anyType; - } - if (declaration.initializer) { - getContextualType(declaration.initializer); - } - // Use type of the specified property, or otherwise, for a numeric name, the type of the numeric index signature, - // or otherwise the type of the string index signature. - var text = getTextOfPropertyName(name_14); - type = getTypeOfPropertyOfType(parentType, text) || - isNumericLiteralName(text) && getIndexTypeOfType(parentType, 1 /* Number */) || - getIndexTypeOfType(parentType, 0 /* String */); - if (!type) { - error(name_14, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(parentType), ts.declarationNameToString(name_14)); - return unknownType; - } + /** + * Writes only the name of the symbol out to the writer. Uses the original source text + * for the name of the symbol if it is available to match how the user wrote the name. + */ + function appendSymbolNameOnly(symbol, writer) { + writer.writeSymbol(getNameOfSymbol(symbol), symbol); } - else { - // This elementType will be used if the specific property corresponding to this index is not - // present (aka the tuple element property). This call also checks that the parentType is in - // fact an iterable or array (depending on target language). - var elementType = checkIteratedTypeOrElementType(parentType, pattern, /*allowStringInput*/ false); - if (!declaration.dotDotDotToken) { - // Use specific property type when parent is a tuple or numeric index type when parent is an array - var propName = "" + ts.indexOf(pattern.elements, declaration); - type = isTupleLikeType(parentType) - ? getTypeOfPropertyOfType(parentType, propName) - : elementType; - if (!type) { - if (isTupleType(parentType)) { - error(declaration, ts.Diagnostics.Tuple_type_0_with_length_1_cannot_be_assigned_to_tuple_with_length_2, typeToString(parentType), getTypeReferenceArity(parentType), pattern.elements.length); - } - else { - error(declaration, ts.Diagnostics.Type_0_has_no_property_1, typeToString(parentType), propName); - } - return unknownType; + /** + * Writes a property access or element access with the name of the symbol out to the writer. + * Uses the original source text for the name of the symbol if it is available to match how the user wrote the name, + * ensuring that any names written with literals use element accesses. + */ + function appendPropertyOrElementAccessForSymbol(symbol, writer) { + var symbolName = getNameOfSymbol(symbol); + var firstChar = symbolName.charCodeAt(0); + var needsElementAccess = !ts.isIdentifierStart(firstChar, languageVersion); + if (needsElementAccess) { + writePunctuation(writer, 19 /* OpenBracketToken */); + if (ts.isSingleOrDoubleQuote(firstChar)) { + writer.writeStringLiteral(symbolName); + } + else { + writer.writeSymbol(symbolName, symbol); } + writePunctuation(writer, 20 /* CloseBracketToken */); } else { - // Rest element has an array type with the same element type as the parent type - type = createArrayType(elementType); - } - } - // In strict null checking mode, if a default value of a non-undefined type is specified, remove - // undefined from the final type. - if (strictNullChecks && declaration.initializer && !(getFalsyFlags(checkExpressionCached(declaration.initializer)) & 2048 /* Undefined */)) { - type = getTypeWithFacts(type, 131072 /* NEUndefined */); - } - return declaration.initializer ? - getUnionType([type, checkExpressionCached(declaration.initializer)], /*subtypeReduction*/ true) : - type; - } - function getTypeForVariableLikeDeclarationFromJSDocComment(declaration) { - var jsDocType = getJSDocTypeForVariableLikeDeclarationFromJSDocComment(declaration); - if (jsDocType) { - return getTypeFromTypeNode(jsDocType); - } - } - function getJSDocTypeForVariableLikeDeclarationFromJSDocComment(declaration) { - // First, see if this node has an @type annotation on it directly. - var typeTag = ts.getJSDocTypeTag(declaration); - if (typeTag && typeTag.typeExpression) { - return typeTag.typeExpression.type; - } - if (declaration.kind === 218 /* VariableDeclaration */ && - declaration.parent.kind === 219 /* VariableDeclarationList */ && - declaration.parent.parent.kind === 200 /* VariableStatement */) { - // @type annotation might have been on the variable statement, try that instead. - var annotation = ts.getJSDocTypeTag(declaration.parent.parent); - if (annotation && annotation.typeExpression) { - return annotation.typeExpression.type; - } - } - else if (declaration.kind === 142 /* Parameter */) { - // If it's a parameter, see if the parent has a jsdoc comment with an @param - // annotation. - var paramTag = ts.getCorrespondingJSDocParameterTag(declaration); - if (paramTag && paramTag.typeExpression) { - return paramTag.typeExpression.type; + writePunctuation(writer, 21 /* DotToken */); + writer.writeSymbol(symbolName, symbol); } } - return undefined; - } - function addOptionality(type, optional) { - return strictNullChecks && optional ? includeFalsyTypes(type, 2048 /* Undefined */) : type; - } - // Return the inferred type for a variable, parameter, or property declaration - function getTypeForVariableLikeDeclaration(declaration, includeOptionality) { - if (declaration.flags & 1048576 /* JavaScriptFile */) { - // If this is a variable in a JavaScript file, then use the JSDoc type (if it has - // one as its type), otherwise fallback to the below standard TS codepaths to - // try to figure it out. - var type = getTypeForVariableLikeDeclarationFromJSDocComment(declaration); - if (type && type !== unknownType) { - return type; + /** + * Enclosing declaration is optional when we don't want to get qualified name in the enclosing declaration scope + * Meaning needs to be specified if the enclosing declaration is given + */ + function buildSymbolDisplay(symbol, writer, enclosingDeclaration, meaning, flags, typeFlags) { + var parentSymbol; + function appendParentTypeArgumentsAndSymbolName(symbol) { + if (parentSymbol) { + // Write type arguments of instantiated class/interface here + if (flags & 1 /* WriteTypeParametersOrArguments */) { + if (symbol.flags & 16777216 /* Instantiated */) { + buildDisplayForTypeArgumentsAndDelimiters(getTypeParametersOfClassOrInterface(parentSymbol), symbol.mapper, writer, enclosingDeclaration); + } + else { + buildTypeParameterDisplayFromSymbol(parentSymbol, writer, enclosingDeclaration); + } + } + appendPropertyOrElementAccessForSymbol(symbol, writer); + } + else { + appendSymbolNameOnly(symbol, writer); + } + parentSymbol = symbol; } - } - // A variable declared in a for..in statement is always of type string - if (declaration.parent.parent.kind === 207 /* ForInStatement */) { - return stringType; - } - if (declaration.parent.parent.kind === 208 /* ForOfStatement */) { - // checkRightHandSideOfForOf will return undefined if the for-of expression type was - // missing properties/signatures required to get its iteratedType (like - // [Symbol.iterator] or next). This may be because we accessed properties from anyType, - // or it may have led to an error inside getElementTypeOfIterable. - return checkRightHandSideOfForOf(declaration.parent.parent.expression) || anyType; - } - if (ts.isBindingPattern(declaration.parent)) { - return getTypeForBindingElement(declaration); - } - // Use type from type annotation if one is present - if (declaration.type) { - return addOptionality(getTypeFromTypeNode(declaration.type), /*optional*/ declaration.questionToken && includeOptionality); - } - if (declaration.kind === 142 /* Parameter */) { - var func = declaration.parent; - // For a parameter of a set accessor, use the type of the get accessor if one is present - if (func.kind === 150 /* SetAccessor */ && !ts.hasDynamicName(func)) { - var getter = ts.getDeclarationOfKind(declaration.parent.symbol, 149 /* GetAccessor */); - if (getter) { - var getterSignature = getSignatureFromDeclaration(getter); - var thisParameter = getAccessorThisParameter(func); - if (thisParameter && declaration === thisParameter) { - // Use the type from the *getter* - ts.Debug.assert(!thisParameter.type); - return getTypeOfSymbol(getterSignature.thisParameter); + // Let the writer know we just wrote out a symbol. The declaration emitter writer uses + // this to determine if an import it has previously seen (and not written out) needs + // to be written to the file once the walk of the tree is complete. + // + // NOTE(cyrusn): This approach feels somewhat unfortunate. A simple pass over the tree + // up front (for example, during checking) could determine if we need to emit the imports + // and we could then access that data during declaration emit. + writer.trackSymbol(symbol, enclosingDeclaration, meaning); + /** @param endOfChain Set to false for recursive calls; non-recursive calls should always output something. */ + function walkSymbol(symbol, meaning, endOfChain) { + var accessibleSymbolChain = getAccessibleSymbolChain(symbol, enclosingDeclaration, meaning, !!(flags & 2 /* UseOnlyExternalAliasing */)); + if (!accessibleSymbolChain || + needsQualification(accessibleSymbolChain[0], enclosingDeclaration, accessibleSymbolChain.length === 1 ? meaning : getQualifiedLeftMeaning(meaning))) { + // Go up and add our parent. + var parent_8 = getParentOfSymbol(accessibleSymbolChain ? accessibleSymbolChain[0] : symbol); + if (parent_8) { + walkSymbol(parent_8, getQualifiedLeftMeaning(meaning), /*endOfChain*/ false); } - return getReturnTypeOfSignature(getterSignature); + } + if (accessibleSymbolChain) { + for (var _i = 0, accessibleSymbolChain_1 = accessibleSymbolChain; _i < accessibleSymbolChain_1.length; _i++) { + var accessibleSymbol = accessibleSymbolChain_1[_i]; + appendParentTypeArgumentsAndSymbolName(accessibleSymbol); + } + } + else if ( + // If this is the last part of outputting the symbol, always output. The cases apply only to parent symbols. + endOfChain || + // If a parent symbol is an external module, don't write it. (We prefer just `x` vs `"foo/bar".x`.) + !(!parentSymbol && ts.forEach(symbol.declarations, hasExternalModuleSymbol)) && + // If a parent symbol is an anonymous type, don't write it. + !(symbol.flags & (2048 /* TypeLiteral */ | 4096 /* ObjectLiteral */))) { + appendParentTypeArgumentsAndSymbolName(symbol); } } - // Use contextual parameter type if one is available - var type = void 0; - if (declaration.symbol.name === "this") { - var thisParameter = getContextualThisParameter(func); - type = thisParameter ? getTypeOfSymbol(thisParameter) : undefined; + // Get qualified name if the symbol is not a type parameter + // and there is an enclosing declaration or we specifically + // asked for it + var isTypeParameter = symbol.flags & 262144 /* TypeParameter */; + var typeFormatFlag = 128 /* UseFullyQualifiedType */ & typeFlags; + if (!isTypeParameter && (enclosingDeclaration || typeFormatFlag)) { + walkSymbol(symbol, meaning, /*endOfChain*/ true); } else { - type = getContextuallyTypedParameterType(declaration); - } - if (type) { - return addOptionality(type, /*optional*/ declaration.questionToken && includeOptionality); - } - } - // Use the type of the initializer expression if one is present - if (declaration.initializer) { - var type = checkDeclarationInitializer(declaration); - return addOptionality(type, /*optional*/ declaration.questionToken && includeOptionality); - } - // If it is a short-hand property assignment, use the type of the identifier - if (declaration.kind === 254 /* ShorthandPropertyAssignment */) { - return checkIdentifier(declaration.name); - } - // If the declaration specifies a binding pattern, use the type implied by the binding pattern - if (ts.isBindingPattern(declaration.name)) { - return getTypeFromBindingPattern(declaration.name, /*includePatternInType*/ false, /*reportErrors*/ true); - } - // No type specified and nothing can be inferred - return undefined; - } - // Return the type implied by a binding pattern element. This is the type of the initializer of the element if - // one is present. Otherwise, if the element is itself a binding pattern, it is the type implied by the binding - // pattern. Otherwise, it is the type any. - function getTypeFromBindingElement(element, includePatternInType, reportErrors) { - if (element.initializer) { - return checkDeclarationInitializer(element); - } - if (ts.isBindingPattern(element.name)) { - return getTypeFromBindingPattern(element.name, includePatternInType, reportErrors); - } - if (reportErrors && compilerOptions.noImplicitAny && !declarationBelongsToPrivateAmbientMember(element)) { - reportImplicitAnyError(element, anyType); - } - return anyType; - } - // Return the type implied by an object binding pattern - function getTypeFromObjectBindingPattern(pattern, includePatternInType, reportErrors) { - var members = ts.createMap(); - var hasComputedProperties = false; - ts.forEach(pattern.elements, function (e) { - var name = e.propertyName || e.name; - if (isComputedNonLiteralName(name)) { - // do not include computed properties in the implied type - hasComputedProperties = true; - return; - } - var text = getTextOfPropertyName(name); - var flags = 4 /* Property */ | 67108864 /* Transient */ | (e.initializer ? 536870912 /* Optional */ : 0); - var symbol = createSymbol(flags, text); - symbol.type = getTypeFromBindingElement(e, includePatternInType, reportErrors); - symbol.bindingElement = e; - members[symbol.name] = symbol; - }); - var result = createAnonymousType(undefined, members, emptyArray, emptyArray, undefined, undefined); - if (includePatternInType) { - result.pattern = pattern; - } - if (hasComputedProperties) { - result.flags |= 536870912 /* ObjectLiteralPatternWithComputedProperties */; - } - return result; - } - // Return the type implied by an array binding pattern - function getTypeFromArrayBindingPattern(pattern, includePatternInType, reportErrors) { - var elements = pattern.elements; - var lastElement = ts.lastOrUndefined(elements); - if (elements.length === 0 || (!ts.isOmittedExpression(lastElement) && lastElement.dotDotDotToken)) { - return languageVersion >= 2 /* ES6 */ ? createIterableType(anyType) : anyArrayType; - } - // If the pattern has at least one element, and no rest element, then it should imply a tuple type. - var elementTypes = ts.map(elements, function (e) { return ts.isOmittedExpression(e) ? anyType : getTypeFromBindingElement(e, includePatternInType, reportErrors); }); - var result = createTupleType(elementTypes); - if (includePatternInType) { - result = cloneTypeReference(result); - result.pattern = pattern; - } - return result; - } - // Return the type implied by a binding pattern. This is the type implied purely by the binding pattern itself - // and without regard to its context (i.e. without regard any type annotation or initializer associated with the - // declaration in which the binding pattern is contained). For example, the implied type of [x, y] is [any, any] - // and the implied type of { x, y: z = 1 } is { x: any; y: number; }. The type implied by a binding pattern is - // used as the contextual type of an initializer associated with the binding pattern. Also, for a destructuring - // parameter with no type annotation or initializer, the type implied by the binding pattern becomes the type of - // the parameter. - function getTypeFromBindingPattern(pattern, includePatternInType, reportErrors) { - return pattern.kind === 167 /* ObjectBindingPattern */ - ? getTypeFromObjectBindingPattern(pattern, includePatternInType, reportErrors) - : getTypeFromArrayBindingPattern(pattern, includePatternInType, reportErrors); - } - // Return the type associated with a variable, parameter, or property declaration. In the simple case this is the type - // specified in a type annotation or inferred from an initializer. However, in the case of a destructuring declaration it - // is a bit more involved. For example: - // - // var [x, s = ""] = [1, "one"]; - // - // Here, the array literal [1, "one"] is contextually typed by the type [any, string], which is the implied type of the - // binding pattern [x, s = ""]. Because the contextual type is a tuple type, the resulting type of [1, "one"] is the - // tuple type [number, string]. Thus, the type inferred for 'x' is number and the type inferred for 's' is string. - function getWidenedTypeForVariableLikeDeclaration(declaration, reportErrors) { - var type = getTypeForVariableLikeDeclaration(declaration, /*includeOptionality*/ true); - if (type) { - if (reportErrors) { - reportErrorsFromWidening(declaration, type); - } - // During a normal type check we'll never get to here with a property assignment (the check of the containing - // object literal uses a different path). We exclude widening only so that language services and type verification - // tools see the actual type. - if (declaration.kind === 253 /* PropertyAssignment */) { - return type; - } - return getWidenedType(type); - } - // Rest parameters default to type any[], other parameters default to type any - type = declaration.dotDotDotToken ? anyArrayType : anyType; - // Report implicit any errors unless this is a private property within an ambient declaration - if (reportErrors && compilerOptions.noImplicitAny) { - if (!declarationBelongsToPrivateAmbientMember(declaration)) { - reportImplicitAnyError(declaration, type); + appendParentTypeArgumentsAndSymbolName(symbol); } } - return type; - } - function declarationBelongsToPrivateAmbientMember(declaration) { - var root = ts.getRootDeclaration(declaration); - var memberDeclaration = root.kind === 142 /* Parameter */ ? root.parent : root; - return isPrivateWithinAmbient(memberDeclaration); - } - function getTypeOfVariableOrParameterOrProperty(symbol) { - var links = getSymbolLinks(symbol); - if (!links.type) { - // Handle prototype property - if (symbol.flags & 134217728 /* Prototype */) { - return links.type = getTypeOfPrototypeProperty(symbol); - } - // Handle catch clause variables - var declaration = symbol.valueDeclaration; - if (declaration.parent.kind === 252 /* CatchClause */) { - return links.type = anyType; - } - // Handle export default expressions - if (declaration.kind === 235 /* ExportAssignment */) { - return links.type = checkExpression(declaration.expression); - } - if (declaration.flags & 1048576 /* JavaScriptFile */ && declaration.kind === 280 /* JSDocPropertyTag */ && declaration.typeExpression) { - return links.type = getTypeFromTypeNode(declaration.typeExpression.type); - } - // Handle variable, parameter or property - if (!pushTypeResolution(symbol, 0 /* Type */)) { - return unknownType; - } - var type = void 0; - // Handle certain special assignment kinds, which happen to union across multiple declarations: - // * module.exports = expr - // * exports.p = expr - // * this.p = expr - // * className.prototype.method = expr - if (declaration.kind === 187 /* BinaryExpression */ || - declaration.kind === 172 /* PropertyAccessExpression */ && declaration.parent.kind === 187 /* BinaryExpression */) { - // Use JS Doc type if present on parent expression statement - if (declaration.flags & 1048576 /* JavaScriptFile */) { - var typeTag = ts.getJSDocTypeTag(declaration.parent); - if (typeTag && typeTag.typeExpression) { - return links.type = getTypeFromTypeNode(typeTag.typeExpression.type); + function buildTypeDisplay(type, writer, enclosingDeclaration, globalFlags, symbolStack) { + var globalFlagsToPass = globalFlags & 16 /* WriteOwnNameForAnyLike */; + var inObjectTypeLiteral = false; + return writeType(type, globalFlags); + function writeType(type, flags) { + var nextFlags = flags & ~512 /* InTypeAlias */; + // Write undefined/null type as any + if (type.flags & 16015 /* Intrinsic */) { + // Special handling for unknown / resolving types, they should show up as any and not unknown or __resolving + writer.writeKeyword(!(globalFlags & 16 /* WriteOwnNameForAnyLike */) && isTypeAny(type) + ? "any" + : type.intrinsicName); + } + else if (type.flags & 16384 /* TypeParameter */ && type.isThisType) { + if (inObjectTypeLiteral) { + writer.reportInaccessibleThisError(); } + writer.writeKeyword("this"); + } + else if (type.flags & 131072 /* Reference */) { + writeTypeReference(type, nextFlags); + } + else if (type.flags & 256 /* EnumLiteral */) { + buildSymbolDisplay(getParentOfSymbol(type.symbol), writer, enclosingDeclaration, 793064 /* Type */, 0 /* None */, nextFlags); + writePunctuation(writer, 21 /* DotToken */); + appendSymbolNameOnly(type.symbol, writer); + } + else if (type.flags & (32768 /* Class */ | 65536 /* Interface */ | 16 /* Enum */ | 16384 /* TypeParameter */)) { + // The specified symbol flags need to be reinterpreted as type flags + buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, 793064 /* Type */, 0 /* None */, nextFlags); + } + else if (!(flags & 512 /* InTypeAlias */) && ((type.flags & 2097152 /* Anonymous */ && !type.target) || type.flags & 1572864 /* UnionOrIntersection */) && type.aliasSymbol && + isSymbolAccessible(type.aliasSymbol, enclosingDeclaration, 793064 /* Type */, /*shouldComputeAliasesToMakeVisible*/ false).accessibility === 0 /* Accessible */) { + // We emit inferred type as type-alias at the current localtion if all the following is true + // the input type is has alias symbol that is accessible + // the input type is a union, intersection or anonymous type that is fully instantiated (if not we want to keep dive into) + // e.g.: export type Bar = () => [X, Y]; + // export type Foo = Bar; + // export const y = (x: Foo) => 1 // we want to emit as ...x: () => [any, string]) + var typeArguments = type.aliasTypeArguments; + writeSymbolTypeReference(type.aliasSymbol, typeArguments, 0, typeArguments ? typeArguments.length : 0, nextFlags); + } + else if (type.flags & 1572864 /* UnionOrIntersection */) { + writeUnionOrIntersectionType(type, nextFlags); + } + else if (type.flags & 2097152 /* Anonymous */) { + writeAnonymousType(type, nextFlags); + } + else if (type.flags & 96 /* StringOrNumberLiteral */) { + writer.writeStringLiteral(literalTypeToString(type)); + } + else { + // Should never get here + // { ... } + writePunctuation(writer, 15 /* OpenBraceToken */); + writeSpace(writer); + writePunctuation(writer, 22 /* DotDotDotToken */); + writeSpace(writer); + writePunctuation(writer, 16 /* CloseBraceToken */); } - var declaredTypes = ts.map(symbol.declarations, function (decl) { return decl.kind === 187 /* BinaryExpression */ ? - checkExpressionCached(decl.right) : - checkExpressionCached(decl.parent.right); }); - type = getUnionType(declaredTypes, /*subtypeReduction*/ true); } - else { - type = getWidenedTypeForVariableLikeDeclaration(declaration, /*reportErrors*/ true); + function writeTypeList(types, delimiter) { + for (var i = 0; i < types.length; i++) { + if (i > 0) { + if (delimiter !== 24 /* CommaToken */) { + writeSpace(writer); + } + writePunctuation(writer, delimiter); + writeSpace(writer); + } + writeType(types[i], delimiter === 24 /* CommaToken */ ? 0 /* None */ : 64 /* InElementType */); + } } - if (!popTypeResolution()) { - if (symbol.valueDeclaration.type) { - // Variable has type annotation that circularly references the variable itself - type = unknownType; - error(symbol.valueDeclaration, ts.Diagnostics._0_is_referenced_directly_or_indirectly_in_its_own_type_annotation, symbolToString(symbol)); + function writeSymbolTypeReference(symbol, typeArguments, pos, end, flags) { + // Unnamed function expressions and arrow functions have reserved names that we don't want to display + if (symbol.flags & 32 /* Class */ || !isReservedMemberName(symbol.name)) { + buildSymbolDisplay(symbol, writer, enclosingDeclaration, 793064 /* Type */, 0 /* None */, flags); } - else { - // Variable has initializer that circularly references the variable itself - type = anyType; - if (compilerOptions.noImplicitAny) { - error(symbol.valueDeclaration, ts.Diagnostics._0_implicitly_has_type_any_because_it_does_not_have_a_type_annotation_and_is_referenced_directly_or_indirectly_in_its_own_initializer, symbolToString(symbol)); + if (pos < end) { + writePunctuation(writer, 25 /* LessThanToken */); + writeType(typeArguments[pos], 256 /* InFirstTypeArgument */); + pos++; + while (pos < end) { + writePunctuation(writer, 24 /* CommaToken */); + writeSpace(writer); + writeType(typeArguments[pos], 0 /* None */); + pos++; } + writePunctuation(writer, 27 /* GreaterThanToken */); } } - links.type = type; - } - return links.type; - } - function getAnnotatedAccessorType(accessor) { - if (accessor) { - if (accessor.kind === 149 /* GetAccessor */) { - return accessor.type && getTypeFromTypeNode(accessor.type); - } - else { - var setterTypeAnnotation = ts.getSetAccessorTypeAnnotationNode(accessor); - return setterTypeAnnotation && getTypeFromTypeNode(setterTypeAnnotation); - } - } - return undefined; - } - function getAnnotatedAccessorThisParameter(accessor) { - var parameter = getAccessorThisParameter(accessor); - return parameter && parameter.symbol; - } - function getThisTypeOfDeclaration(declaration) { - return getThisTypeOfSignature(getSignatureFromDeclaration(declaration)); - } - function getTypeOfAccessors(symbol) { - var links = getSymbolLinks(symbol); - if (!links.type) { - var getter = ts.getDeclarationOfKind(symbol, 149 /* GetAccessor */); - var setter = ts.getDeclarationOfKind(symbol, 150 /* SetAccessor */); - if (getter && getter.flags & 1048576 /* JavaScriptFile */) { - var jsDocType = getTypeForVariableLikeDeclarationFromJSDocComment(getter); - if (jsDocType) { - return links.type = jsDocType; + function writeTypeReference(type, flags) { + var typeArguments = type.typeArguments || emptyArray; + if (type.target === globalArrayType && !(flags & 1 /* WriteArrayAsGenericType */)) { + writeType(typeArguments[0], 64 /* InElementType */); + writePunctuation(writer, 19 /* OpenBracketToken */); + writePunctuation(writer, 20 /* CloseBracketToken */); } - } - if (!pushTypeResolution(symbol, 0 /* Type */)) { - return unknownType; - } - var type = void 0; - // First try to see if the user specified a return type on the get-accessor. - var getterReturnType = getAnnotatedAccessorType(getter); - if (getterReturnType) { - type = getterReturnType; - } - else { - // If the user didn't specify a return type, try to use the set-accessor's parameter type. - var setterParameterType = getAnnotatedAccessorType(setter); - if (setterParameterType) { - type = setterParameterType; + else if (type.target.flags & 262144 /* Tuple */) { + writePunctuation(writer, 19 /* OpenBracketToken */); + writeTypeList(type.typeArguments.slice(0, getTypeReferenceArity(type)), 24 /* CommaToken */); + writePunctuation(writer, 20 /* CloseBracketToken */); } else { - // If there are no specified types, try to infer it from the body of the get accessor if it exists. - if (getter && getter.body) { - type = getReturnTypeFromBody(getter); - } - else { - if (compilerOptions.noImplicitAny) { - if (setter) { - error(setter, ts.Diagnostics.Property_0_implicitly_has_type_any_because_its_set_accessor_lacks_a_parameter_type_annotation, symbolToString(symbol)); - } - else { - ts.Debug.assert(!!getter, "there must existed getter as we are current checking either setter or getter in this function"); - error(getter, ts.Diagnostics.Property_0_implicitly_has_type_any_because_its_get_accessor_lacks_a_return_type_annotation, symbolToString(symbol)); + // Write the type reference in the format f.g.C where A and B are type arguments + // for outer type parameters, and f and g are the respective declaring containers of those + // type parameters. + var outerTypeParameters = type.target.outerTypeParameters; + var i = 0; + if (outerTypeParameters) { + var length_1 = outerTypeParameters.length; + while (i < length_1) { + // Find group of type arguments for type parameters with the same declaring container. + var start = i; + var parent_9 = getParentSymbolOfTypeParameter(outerTypeParameters[i]); + do { + i++; + } while (i < length_1 && getParentSymbolOfTypeParameter(outerTypeParameters[i]) === parent_9); + // When type parameters are their own type arguments for the whole group (i.e. we have + // the default outer type arguments), we don't show the group. + if (!ts.rangeEquals(outerTypeParameters, typeArguments, start, i)) { + writeSymbolTypeReference(parent_9, typeArguments, start, i, flags); + writePunctuation(writer, 21 /* DotToken */); } } - type = anyType; } + var typeParameterCount = (type.target.typeParameters || emptyArray).length; + writeSymbolTypeReference(type.symbol, typeArguments, i, typeParameterCount, flags); } } - if (!popTypeResolution()) { - type = anyType; - if (compilerOptions.noImplicitAny) { - var getter_1 = ts.getDeclarationOfKind(symbol, 149 /* GetAccessor */); - error(getter_1, ts.Diagnostics._0_implicitly_has_return_type_any_because_it_does_not_have_a_return_type_annotation_and_is_referenced_directly_or_indirectly_in_one_of_its_return_expressions, symbolToString(symbol)); + function writeUnionOrIntersectionType(type, flags) { + if (flags & 64 /* InElementType */) { + writePunctuation(writer, 17 /* OpenParenToken */); + } + if (type.flags & 524288 /* Union */) { + writeTypeList(formatUnionTypes(type.types), 47 /* BarToken */); + } + else { + writeTypeList(type.types, 46 /* AmpersandToken */); + } + if (flags & 64 /* InElementType */) { + writePunctuation(writer, 18 /* CloseParenToken */); } } - links.type = type; - } - return links.type; - } - function getTypeOfFuncClassEnumModule(symbol) { - var links = getSymbolLinks(symbol); - if (!links.type) { - if (symbol.valueDeclaration.kind === 225 /* ModuleDeclaration */ && ts.isShorthandAmbientModuleSymbol(symbol)) { - links.type = anyType; - } - else { - var type = createObjectType(2097152 /* Anonymous */, symbol); - links.type = strictNullChecks && symbol.flags & 536870912 /* Optional */ ? - includeFalsyTypes(type, 2048 /* Undefined */) : type; + function writeAnonymousType(type, flags) { + var symbol = type.symbol; + if (symbol) { + // Always use 'typeof T' for type of class, enum, and module objects + if (symbol.flags & (32 /* Class */ | 384 /* Enum */ | 512 /* ValueModule */)) { + writeTypeOfSymbol(type, flags); + } + else if (shouldWriteTypeOfFunctionSymbol()) { + writeTypeOfSymbol(type, flags); + } + else if (ts.contains(symbolStack, symbol)) { + // If type is an anonymous type literal in a type alias declaration, use type alias name + var typeAlias = getTypeAliasForTypeLiteral(type); + if (typeAlias) { + // The specified symbol flags need to be reinterpreted as type flags + buildSymbolDisplay(typeAlias, writer, enclosingDeclaration, 793064 /* Type */, 0 /* None */, flags); + } + else { + // Recursive usage, use any + writeKeyword(writer, 117 /* AnyKeyword */); + } + } + else { + // Since instantiations of the same anonymous type have the same symbol, tracking symbols instead + // of types allows us to catch circular references to instantiations of the same anonymous type + if (!symbolStack) { + symbolStack = []; + } + symbolStack.push(symbol); + writeLiteralType(type, flags); + symbolStack.pop(); + } + } + else { + // Anonymous types with no symbol are never circular + writeLiteralType(type, flags); + } + function shouldWriteTypeOfFunctionSymbol() { + var isStaticMethodSymbol = !!(symbol.flags & 8192 /* Method */ && + ts.forEach(symbol.declarations, function (declaration) { return ts.getModifierFlags(declaration) & 32 /* Static */; })); + var isNonLocalFunctionSymbol = !!(symbol.flags & 16 /* Function */) && + (symbol.parent || + ts.forEach(symbol.declarations, function (declaration) { + return declaration.parent.kind === 256 /* SourceFile */ || declaration.parent.kind === 226 /* ModuleBlock */; + })); + if (isStaticMethodSymbol || isNonLocalFunctionSymbol) { + // typeof is allowed only for static/non local functions + return !!(flags & 2 /* UseTypeOfFunction */) || + (ts.contains(symbolStack, symbol)); // it is type of the symbol uses itself recursively + } + } } - } - return links.type; - } - function getTypeOfEnumMember(symbol) { - var links = getSymbolLinks(symbol); - if (!links.type) { - links.type = getDeclaredTypeOfEnumMember(symbol); - } - return links.type; - } - function getTypeOfAlias(symbol) { - var links = getSymbolLinks(symbol); - if (!links.type) { - var targetSymbol = resolveAlias(symbol); - // It only makes sense to get the type of a value symbol. If the result of resolving - // the alias is not a value, then it has no type. To get the type associated with a - // type symbol, call getDeclaredTypeOfSymbol. - // This check is important because without it, a call to getTypeOfSymbol could end - // up recursively calling getTypeOfAlias, causing a stack overflow. - links.type = targetSymbol.flags & 107455 /* Value */ - ? getTypeOfSymbol(targetSymbol) - : unknownType; - } - return links.type; - } - function getTypeOfInstantiatedSymbol(symbol) { - var links = getSymbolLinks(symbol); - if (!links.type) { - links.type = instantiateType(getTypeOfSymbol(links.target), links.mapper); - } - return links.type; - } - function getTypeOfSymbol(symbol) { - if (symbol.flags & 16777216 /* Instantiated */) { - return getTypeOfInstantiatedSymbol(symbol); - } - if (symbol.flags & (3 /* Variable */ | 4 /* Property */)) { - return getTypeOfVariableOrParameterOrProperty(symbol); - } - if (symbol.flags & (16 /* Function */ | 8192 /* Method */ | 32 /* Class */ | 384 /* Enum */ | 512 /* ValueModule */)) { - return getTypeOfFuncClassEnumModule(symbol); - } - if (symbol.flags & 8 /* EnumMember */) { - return getTypeOfEnumMember(symbol); - } - if (symbol.flags & 98304 /* Accessor */) { - return getTypeOfAccessors(symbol); - } - if (symbol.flags & 8388608 /* Alias */) { - return getTypeOfAlias(symbol); - } - return unknownType; - } - function getTargetType(type) { - return type.flags & 131072 /* Reference */ ? type.target : type; - } - function hasBaseType(type, checkBase) { - return check(type); - function check(type) { - var target = getTargetType(type); - return target === checkBase || ts.forEach(getBaseTypes(target), check); - } - } - // Appends the type parameters given by a list of declarations to a set of type parameters and returns the resulting set. - // The function allocates a new array if the input type parameter set is undefined, but otherwise it modifies the set - // in-place and returns the same array. - function appendTypeParameters(typeParameters, declarations) { - for (var _i = 0, declarations_2 = declarations; _i < declarations_2.length; _i++) { - var declaration = declarations_2[_i]; - var tp = getDeclaredTypeOfTypeParameter(getSymbolOfNode(declaration)); - if (!typeParameters) { - typeParameters = [tp]; + function writeTypeOfSymbol(type, typeFormatFlags) { + writeKeyword(writer, 101 /* TypeOfKeyword */); + writeSpace(writer); + buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, 107455 /* Value */, 0 /* None */, typeFormatFlags); } - else if (!ts.contains(typeParameters, tp)) { - typeParameters.push(tp); + function writeIndexSignature(info, keyword) { + if (info) { + if (info.isReadonly) { + writeKeyword(writer, 128 /* ReadonlyKeyword */); + writeSpace(writer); + } + writePunctuation(writer, 19 /* OpenBracketToken */); + writer.writeParameter(info.declaration ? ts.declarationNameToString(info.declaration.parameters[0].name) : "x"); + writePunctuation(writer, 54 /* ColonToken */); + writeSpace(writer); + writeKeyword(writer, keyword); + writePunctuation(writer, 20 /* CloseBracketToken */); + writePunctuation(writer, 54 /* ColonToken */); + writeSpace(writer); + writeType(info.type, 0 /* None */); + writePunctuation(writer, 23 /* SemicolonToken */); + writer.writeLine(); + } } - } - return typeParameters; - } - // Appends the outer type parameters of a node to a set of type parameters and returns the resulting set. The function - // allocates a new array if the input type parameter set is undefined, but otherwise it modifies the set in-place and - // returns the same array. - function appendOuterTypeParameters(typeParameters, node) { - while (true) { - node = node.parent; - if (!node) { - return typeParameters; + function writePropertyWithModifiers(prop) { + if (isReadonlySymbol(prop)) { + writeKeyword(writer, 128 /* ReadonlyKeyword */); + writeSpace(writer); + } + buildSymbolDisplay(prop, writer); + if (prop.flags & 536870912 /* Optional */) { + writePunctuation(writer, 53 /* QuestionToken */); + } } - if (node.kind === 221 /* ClassDeclaration */ || node.kind === 192 /* ClassExpression */ || - node.kind === 220 /* FunctionDeclaration */ || node.kind === 179 /* FunctionExpression */ || - node.kind === 147 /* MethodDeclaration */ || node.kind === 180 /* ArrowFunction */) { - var declarations = node.typeParameters; - if (declarations) { - return appendTypeParameters(appendOuterTypeParameters(typeParameters, node), declarations); + function shouldAddParenthesisAroundFunctionType(callSignature, flags) { + if (flags & 64 /* InElementType */) { + return true; } + else if (flags & 256 /* InFirstTypeArgument */) { + // Add parenthesis around function type for the first type argument to avoid ambiguity + var typeParameters = callSignature.target && (flags & 32 /* WriteTypeArgumentsOfSignature */) ? + callSignature.target.typeParameters : callSignature.typeParameters; + return typeParameters && typeParameters.length !== 0; + } + return false; } - } - } - // The outer type parameters are those defined by enclosing generic classes, methods, or functions. - function getOuterTypeParametersOfClassOrInterface(symbol) { - var declaration = symbol.flags & 32 /* Class */ ? symbol.valueDeclaration : ts.getDeclarationOfKind(symbol, 222 /* InterfaceDeclaration */); - return appendOuterTypeParameters(undefined, declaration); - } - // The local type parameters are the combined set of type parameters from all declarations of the class, - // interface, or type alias. - function getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol) { - var result; - for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { - var node = _a[_i]; - if (node.kind === 222 /* InterfaceDeclaration */ || node.kind === 221 /* ClassDeclaration */ || - node.kind === 192 /* ClassExpression */ || node.kind === 223 /* TypeAliasDeclaration */) { - var declaration = node; - if (declaration.typeParameters) { - result = appendTypeParameters(result, declaration.typeParameters); + function writeLiteralType(type, flags) { + var resolved = resolveStructuredTypeMembers(type); + if (!resolved.properties.length && !resolved.stringIndexInfo && !resolved.numberIndexInfo) { + if (!resolved.callSignatures.length && !resolved.constructSignatures.length) { + writePunctuation(writer, 15 /* OpenBraceToken */); + writePunctuation(writer, 16 /* CloseBraceToken */); + return; + } + if (resolved.callSignatures.length === 1 && !resolved.constructSignatures.length) { + var parenthesizeSignature = shouldAddParenthesisAroundFunctionType(resolved.callSignatures[0], flags); + if (parenthesizeSignature) { + writePunctuation(writer, 17 /* OpenParenToken */); + } + buildSignatureDisplay(resolved.callSignatures[0], writer, enclosingDeclaration, globalFlagsToPass | 8 /* WriteArrowStyleSignature */, /*kind*/ undefined, symbolStack); + if (parenthesizeSignature) { + writePunctuation(writer, 18 /* CloseParenToken */); + } + return; + } + if (resolved.constructSignatures.length === 1 && !resolved.callSignatures.length) { + if (flags & 64 /* InElementType */) { + writePunctuation(writer, 17 /* OpenParenToken */); + } + writeKeyword(writer, 92 /* NewKeyword */); + writeSpace(writer); + buildSignatureDisplay(resolved.constructSignatures[0], writer, enclosingDeclaration, globalFlagsToPass | 8 /* WriteArrowStyleSignature */, /*kind*/ undefined, symbolStack); + if (flags & 64 /* InElementType */) { + writePunctuation(writer, 18 /* CloseParenToken */); + } + return; + } + } + var saveInObjectTypeLiteral = inObjectTypeLiteral; + inObjectTypeLiteral = true; + writePunctuation(writer, 15 /* OpenBraceToken */); + writer.writeLine(); + writer.increaseIndent(); + for (var _i = 0, _a = resolved.callSignatures; _i < _a.length; _i++) { + var signature = _a[_i]; + buildSignatureDisplay(signature, writer, enclosingDeclaration, globalFlagsToPass, /*kind*/ undefined, symbolStack); + writePunctuation(writer, 23 /* SemicolonToken */); + writer.writeLine(); + } + for (var _b = 0, _c = resolved.constructSignatures; _b < _c.length; _b++) { + var signature = _c[_b]; + buildSignatureDisplay(signature, writer, enclosingDeclaration, globalFlagsToPass, 1 /* Construct */, symbolStack); + writePunctuation(writer, 23 /* SemicolonToken */); + writer.writeLine(); + } + writeIndexSignature(resolved.stringIndexInfo, 132 /* StringKeyword */); + writeIndexSignature(resolved.numberIndexInfo, 130 /* NumberKeyword */); + for (var _d = 0, _e = resolved.properties; _d < _e.length; _d++) { + var p = _e[_d]; + var t = getTypeOfSymbol(p); + if (p.flags & (16 /* Function */ | 8192 /* Method */) && !getPropertiesOfObjectType(t).length) { + var signatures = getSignaturesOfType(t, 0 /* Call */); + for (var _f = 0, signatures_1 = signatures; _f < signatures_1.length; _f++) { + var signature = signatures_1[_f]; + writePropertyWithModifiers(p); + buildSignatureDisplay(signature, writer, enclosingDeclaration, globalFlagsToPass, /*kind*/ undefined, symbolStack); + writePunctuation(writer, 23 /* SemicolonToken */); + writer.writeLine(); + } + } + else { + writePropertyWithModifiers(p); + writePunctuation(writer, 54 /* ColonToken */); + writeSpace(writer); + writeType(t, 0 /* None */); + writePunctuation(writer, 23 /* SemicolonToken */); + writer.writeLine(); + } } + writer.decreaseIndent(); + writePunctuation(writer, 16 /* CloseBraceToken */); + inObjectTypeLiteral = saveInObjectTypeLiteral; } } - return result; - } - // The full set of type parameters for a generic class or interface type consists of its outer type parameters plus - // its locally declared type parameters. - function getTypeParametersOfClassOrInterface(symbol) { - return ts.concatenate(getOuterTypeParametersOfClassOrInterface(symbol), getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol)); - } - function isConstructorType(type) { - return type.flags & 2588672 /* ObjectType */ && getSignaturesOfType(type, 1 /* Construct */).length > 0; - } - function getBaseTypeNodeOfClass(type) { - return ts.getClassExtendsHeritageClauseElement(type.symbol.valueDeclaration); - } - function getConstructorsForTypeArguments(type, typeArgumentNodes) { - var typeArgCount = typeArgumentNodes ? typeArgumentNodes.length : 0; - return ts.filter(getSignaturesOfType(type, 1 /* Construct */), function (sig) { return (sig.typeParameters ? sig.typeParameters.length : 0) === typeArgCount; }); - } - function getInstantiatedConstructorsForTypeArguments(type, typeArgumentNodes) { - var signatures = getConstructorsForTypeArguments(type, typeArgumentNodes); - if (typeArgumentNodes) { - var typeArguments_1 = ts.map(typeArgumentNodes, getTypeFromTypeNodeNoAlias); - signatures = ts.map(signatures, function (sig) { return getSignatureInstantiation(sig, typeArguments_1); }); + function buildTypeParameterDisplayFromSymbol(symbol, writer, enclosingDeclaration, flags) { + var targetSymbol = getTargetSymbol(symbol); + if (targetSymbol.flags & 32 /* Class */ || targetSymbol.flags & 64 /* Interface */ || targetSymbol.flags & 524288 /* TypeAlias */) { + buildDisplayForTypeParametersAndDelimiters(getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol), writer, enclosingDeclaration, flags); + } } - return signatures; - } - // The base constructor of a class can resolve to - // undefinedType if the class has no extends clause, - // unknownType if an error occurred during resolution of the extends expression, - // nullType if the extends expression is the null value, or - // an object type with at least one construct signature. - function getBaseConstructorTypeOfClass(type) { - if (!type.resolvedBaseConstructorType) { - var baseTypeNode = getBaseTypeNodeOfClass(type); - if (!baseTypeNode) { - return type.resolvedBaseConstructorType = undefinedType; + function buildTypeParameterDisplay(tp, writer, enclosingDeclaration, flags, symbolStack) { + appendSymbolNameOnly(tp.symbol, writer); + var constraint = getConstraintOfTypeParameter(tp); + if (constraint) { + writeSpace(writer); + writeKeyword(writer, 83 /* ExtendsKeyword */); + writeSpace(writer); + buildTypeDisplay(constraint, writer, enclosingDeclaration, flags, symbolStack); } - if (!pushTypeResolution(type, 1 /* ResolvedBaseConstructorType */)) { - return unknownType; + } + function buildParameterDisplay(p, writer, enclosingDeclaration, flags, symbolStack) { + var parameterNode = p.valueDeclaration; + if (ts.isRestParameter(parameterNode)) { + writePunctuation(writer, 22 /* DotDotDotToken */); } - var baseConstructorType = checkExpression(baseTypeNode.expression); - if (baseConstructorType.flags & 2588672 /* ObjectType */) { - // Resolving the members of a class requires us to resolve the base class of that class. - // We force resolution here such that we catch circularities now. - resolveStructuredTypeMembers(baseConstructorType); + if (ts.isBindingPattern(parameterNode.name)) { + buildBindingPatternDisplay(parameterNode.name, writer, enclosingDeclaration, flags, symbolStack); } - if (!popTypeResolution()) { - error(type.symbol.valueDeclaration, ts.Diagnostics._0_is_referenced_directly_or_indirectly_in_its_own_base_expression, symbolToString(type.symbol)); - return type.resolvedBaseConstructorType = unknownType; + else { + appendSymbolNameOnly(p, writer); } - if (baseConstructorType !== unknownType && baseConstructorType !== nullWideningType && !isConstructorType(baseConstructorType)) { - error(baseTypeNode.expression, ts.Diagnostics.Type_0_is_not_a_constructor_function_type, typeToString(baseConstructorType)); - return type.resolvedBaseConstructorType = unknownType; + if (isOptionalParameter(parameterNode)) { + writePunctuation(writer, 53 /* QuestionToken */); } - type.resolvedBaseConstructorType = baseConstructorType; + writePunctuation(writer, 54 /* ColonToken */); + writeSpace(writer); + buildTypeDisplay(getTypeOfSymbol(p), writer, enclosingDeclaration, flags, symbolStack); } - return type.resolvedBaseConstructorType; - } - function getBaseTypes(type) { - if (!type.resolvedBaseTypes) { - if (type.flags & 262144 /* Tuple */) { - type.resolvedBaseTypes = [createArrayType(getUnionType(type.typeParameters))]; + function buildBindingPatternDisplay(bindingPattern, writer, enclosingDeclaration, flags, symbolStack) { + // We have to explicitly emit square bracket and bracket because these tokens are not stored inside the node. + if (bindingPattern.kind === 167 /* ObjectBindingPattern */) { + writePunctuation(writer, 15 /* OpenBraceToken */); + buildDisplayForCommaSeparatedList(bindingPattern.elements, writer, function (e) { return buildBindingElementDisplay(e, writer, enclosingDeclaration, flags, symbolStack); }); + writePunctuation(writer, 16 /* CloseBraceToken */); } - else if (type.symbol.flags & (32 /* Class */ | 64 /* Interface */)) { - if (type.symbol.flags & 32 /* Class */) { - resolveBaseTypesOfClass(type); - } - if (type.symbol.flags & 64 /* Interface */) { - resolveBaseTypesOfInterface(type); + else if (bindingPattern.kind === 168 /* ArrayBindingPattern */) { + writePunctuation(writer, 19 /* OpenBracketToken */); + var elements = bindingPattern.elements; + buildDisplayForCommaSeparatedList(elements, writer, function (e) { return buildBindingElementDisplay(e, writer, enclosingDeclaration, flags, symbolStack); }); + if (elements && elements.hasTrailingComma) { + writePunctuation(writer, 24 /* CommaToken */); } + writePunctuation(writer, 20 /* CloseBracketToken */); + } + } + function buildBindingElementDisplay(bindingElement, writer, enclosingDeclaration, flags, symbolStack) { + if (ts.isOmittedExpression(bindingElement)) { + return; + } + ts.Debug.assert(bindingElement.kind === 169 /* BindingElement */); + if (bindingElement.propertyName) { + writer.writeSymbol(ts.getTextOfNode(bindingElement.propertyName), bindingElement.symbol); + writePunctuation(writer, 54 /* ColonToken */); + writeSpace(writer); + } + if (ts.isBindingPattern(bindingElement.name)) { + buildBindingPatternDisplay(bindingElement.name, writer, enclosingDeclaration, flags, symbolStack); } else { - ts.Debug.fail("type must be class or interface"); + if (bindingElement.dotDotDotToken) { + writePunctuation(writer, 22 /* DotDotDotToken */); + } + appendSymbolNameOnly(bindingElement.symbol, writer); } } - return type.resolvedBaseTypes; - } - function resolveBaseTypesOfClass(type) { - type.resolvedBaseTypes = type.resolvedBaseTypes || emptyArray; - var baseConstructorType = getBaseConstructorTypeOfClass(type); - if (!(baseConstructorType.flags & 2588672 /* ObjectType */)) { - return; - } - var baseTypeNode = getBaseTypeNodeOfClass(type); - var baseType; - var originalBaseType = baseConstructorType && baseConstructorType.symbol ? getDeclaredTypeOfSymbol(baseConstructorType.symbol) : undefined; - if (baseConstructorType.symbol && baseConstructorType.symbol.flags & 32 /* Class */ && - areAllOuterTypeParametersApplied(originalBaseType)) { - // When base constructor type is a class with no captured type arguments we know that the constructors all have the same type parameters as the - // class and all return the instance type of the class. There is no need for further checks and we can apply the - // type arguments in the same manner as a type reference to get the same error reporting experience. - baseType = getTypeFromClassOrInterfaceReference(baseTypeNode, baseConstructorType.symbol); - } - else { - // The class derives from a "class-like" constructor function, check that we have at least one construct signature - // with a matching number of type parameters and use the return type of the first instantiated signature. Elsewhere - // we check that all instantiated signatures return the same type. - var constructors = getInstantiatedConstructorsForTypeArguments(baseConstructorType, baseTypeNode.typeArguments); - if (!constructors.length) { - error(baseTypeNode.expression, ts.Diagnostics.No_base_constructor_has_the_specified_number_of_type_arguments); - return; + function buildDisplayForTypeParametersAndDelimiters(typeParameters, writer, enclosingDeclaration, flags, symbolStack) { + if (typeParameters && typeParameters.length) { + writePunctuation(writer, 25 /* LessThanToken */); + buildDisplayForCommaSeparatedList(typeParameters, writer, function (p) { return buildTypeParameterDisplay(p, writer, enclosingDeclaration, flags, symbolStack); }); + writePunctuation(writer, 27 /* GreaterThanToken */); } - baseType = getReturnTypeOfSignature(constructors[0]); - } - if (baseType === unknownType) { - return; - } - if (!(getTargetType(baseType).flags & (32768 /* Class */ | 65536 /* Interface */))) { - error(baseTypeNode.expression, ts.Diagnostics.Base_constructor_return_type_0_is_not_a_class_or_interface_type, typeToString(baseType)); - return; - } - if (type === baseType || hasBaseType(baseType, type)) { - error(type.symbol.valueDeclaration, ts.Diagnostics.Type_0_recursively_references_itself_as_a_base_type, typeToString(type, /*enclosingDeclaration*/ undefined, 1 /* WriteArrayAsGenericType */)); - return; - } - if (type.resolvedBaseTypes === emptyArray) { - type.resolvedBaseTypes = [baseType]; - } - else { - type.resolvedBaseTypes.push(baseType); } - } - function areAllOuterTypeParametersApplied(type) { - // An unapplied type parameter has its symbol still the same as the matching argument symbol. - // Since parameters are applied outer-to-inner, only the last outer parameter needs to be checked. - var outerTypeParameters = type.outerTypeParameters; - if (outerTypeParameters) { - var last = outerTypeParameters.length - 1; - var typeArguments = type.typeArguments; - return outerTypeParameters[last].symbol !== typeArguments[last].symbol; + function buildDisplayForCommaSeparatedList(list, writer, action) { + for (var i = 0; i < list.length; i++) { + if (i > 0) { + writePunctuation(writer, 24 /* CommaToken */); + writeSpace(writer); + } + action(list[i]); + } } - return true; - } - function resolveBaseTypesOfInterface(type) { - type.resolvedBaseTypes = type.resolvedBaseTypes || emptyArray; - for (var _i = 0, _a = type.symbol.declarations; _i < _a.length; _i++) { - var declaration = _a[_i]; - if (declaration.kind === 222 /* InterfaceDeclaration */ && ts.getInterfaceBaseTypeNodes(declaration)) { - for (var _b = 0, _c = ts.getInterfaceBaseTypeNodes(declaration); _b < _c.length; _b++) { - var node = _c[_b]; - var baseType = getTypeFromTypeNode(node); - if (baseType !== unknownType) { - if (getTargetType(baseType).flags & (32768 /* Class */ | 65536 /* Interface */)) { - if (type !== baseType && !hasBaseType(baseType, type)) { - if (type.resolvedBaseTypes === emptyArray) { - type.resolvedBaseTypes = [baseType]; - } - else { - type.resolvedBaseTypes.push(baseType); - } - } - else { - error(declaration, ts.Diagnostics.Type_0_recursively_references_itself_as_a_base_type, typeToString(type, /*enclosingDeclaration*/ undefined, 1 /* WriteArrayAsGenericType */)); - } - } - else { - error(node, ts.Diagnostics.An_interface_may_only_extend_a_class_or_another_interface); - } + function buildDisplayForTypeArgumentsAndDelimiters(typeParameters, mapper, writer, enclosingDeclaration, flags, symbolStack) { + if (typeParameters && typeParameters.length) { + writePunctuation(writer, 25 /* LessThanToken */); + var flags_1 = 256 /* InFirstTypeArgument */; + for (var i = 0; i < typeParameters.length; i++) { + if (i > 0) { + writePunctuation(writer, 24 /* CommaToken */); + writeSpace(writer); + flags_1 = 0 /* None */; } + buildTypeDisplay(mapper(typeParameters[i]), writer, enclosingDeclaration, flags_1); } + writePunctuation(writer, 27 /* GreaterThanToken */); } } - } - // Returns true if the interface given by the symbol is free of "this" references. Specifically, the result is - // true if the interface itself contains no references to "this" in its body, if all base types are interfaces, - // and if none of the base interfaces have a "this" type. - function isIndependentInterface(symbol) { - for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { - var declaration = _a[_i]; - if (declaration.kind === 222 /* InterfaceDeclaration */) { - if (declaration.flags & 64 /* ContainsThis */) { - return false; - } - var baseTypeNodes = ts.getInterfaceBaseTypeNodes(declaration); - if (baseTypeNodes) { - for (var _b = 0, baseTypeNodes_1 = baseTypeNodes; _b < baseTypeNodes_1.length; _b++) { - var node = baseTypeNodes_1[_b]; - if (ts.isEntityNameExpression(node.expression)) { - var baseSymbol = resolveEntityName(node.expression, 793064 /* Type */, /*ignoreErrors*/ true); - if (!baseSymbol || !(baseSymbol.flags & 64 /* Interface */) || getDeclaredTypeOfClassOrInterface(baseSymbol).thisType) { - return false; - } - } - } + function buildDisplayForParametersAndDelimiters(thisParameter, parameters, writer, enclosingDeclaration, flags, symbolStack) { + writePunctuation(writer, 17 /* OpenParenToken */); + if (thisParameter) { + buildParameterDisplay(thisParameter, writer, enclosingDeclaration, flags, symbolStack); + } + for (var i = 0; i < parameters.length; i++) { + if (i > 0 || thisParameter) { + writePunctuation(writer, 24 /* CommaToken */); + writeSpace(writer); } + buildParameterDisplay(parameters[i], writer, enclosingDeclaration, flags, symbolStack); } + writePunctuation(writer, 18 /* CloseParenToken */); } - return true; - } - function getDeclaredTypeOfClassOrInterface(symbol) { - var links = getSymbolLinks(symbol); - if (!links.declaredType) { - var kind = symbol.flags & 32 /* Class */ ? 32768 /* Class */ : 65536 /* Interface */; - var type = links.declaredType = createObjectType(kind, symbol); - var outerTypeParameters = getOuterTypeParametersOfClassOrInterface(symbol); - var localTypeParameters = getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol); - // A class or interface is generic if it has type parameters or a "this" type. We always give classes a "this" type - // because it is not feasible to analyze all members to determine if the "this" type escapes the class (in particular, - // property types inferred from initializers and method return types inferred from return statements are very hard - // to exhaustively analyze). We give interfaces a "this" type if we can't definitely determine that they are free of - // "this" references. - if (outerTypeParameters || localTypeParameters || kind === 32768 /* Class */ || !isIndependentInterface(symbol)) { - type.flags |= 131072 /* Reference */; - type.typeParameters = ts.concatenate(outerTypeParameters, localTypeParameters); - type.outerTypeParameters = outerTypeParameters; - type.localTypeParameters = localTypeParameters; - type.instantiations = ts.createMap(); - type.instantiations[getTypeListId(type.typeParameters)] = type; - type.target = type; - type.typeArguments = type.typeParameters; - type.thisType = createType(16384 /* TypeParameter */ | 268435456 /* ThisType */); - type.thisType.symbol = symbol; - type.thisType.constraint = type; + function buildTypePredicateDisplay(predicate, writer, enclosingDeclaration, flags, symbolStack) { + if (ts.isIdentifierTypePredicate(predicate)) { + writer.writeParameter(predicate.parameterName); } + else { + writeKeyword(writer, 97 /* ThisKeyword */); + } + writeSpace(writer); + writeKeyword(writer, 124 /* IsKeyword */); + writeSpace(writer); + buildTypeDisplay(predicate.type, writer, enclosingDeclaration, flags, symbolStack); } - return links.declaredType; - } - function getDeclaredTypeOfTypeAlias(symbol) { - var links = getSymbolLinks(symbol); - if (!links.declaredType) { - // Note that we use the links object as the target here because the symbol object is used as the unique - // identity for resolution of the 'type' property in SymbolLinks. - if (!pushTypeResolution(symbol, 2 /* DeclaredType */)) { - return unknownType; + function buildReturnTypeDisplay(signature, writer, enclosingDeclaration, flags, symbolStack) { + if (flags & 8 /* WriteArrowStyleSignature */) { + writeSpace(writer); + writePunctuation(writer, 34 /* EqualsGreaterThanToken */); } - var typeParameters = getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol); - var declaration = ts.getDeclarationOfKind(symbol, 279 /* JSDocTypedefTag */); - var type = void 0; - if (declaration) { - if (declaration.jsDocTypeLiteral) { - type = getTypeFromTypeNode(declaration.jsDocTypeLiteral); - } - else { - type = getTypeFromTypeNode(declaration.typeExpression.type); - } + else { + writePunctuation(writer, 54 /* ColonToken */); + } + writeSpace(writer); + if (signature.typePredicate) { + buildTypePredicateDisplay(signature.typePredicate, writer, enclosingDeclaration, flags, symbolStack); } else { - declaration = ts.getDeclarationOfKind(symbol, 223 /* TypeAliasDeclaration */); - type = getTypeFromTypeNode(declaration.type, symbol, typeParameters); + var returnType = getReturnTypeOfSignature(signature); + buildTypeDisplay(returnType, writer, enclosingDeclaration, flags, symbolStack); } - if (popTypeResolution()) { - links.typeParameters = typeParameters; - if (typeParameters) { - // Initialize the instantiation cache for generic type aliases. The declared type corresponds to - // an instantiation of the type alias with the type parameters supplied as type arguments. - links.instantiations = ts.createMap(); - links.instantiations[getTypeListId(links.typeParameters)] = type; - } + } + function buildSignatureDisplay(signature, writer, enclosingDeclaration, flags, kind, symbolStack) { + if (kind === 1 /* Construct */) { + writeKeyword(writer, 92 /* NewKeyword */); + writeSpace(writer); + } + if (signature.target && (flags & 32 /* WriteTypeArgumentsOfSignature */)) { + // Instantiated signature, write type arguments instead + // This is achieved by passing in the mapper separately + buildDisplayForTypeArgumentsAndDelimiters(signature.target.typeParameters, signature.mapper, writer, enclosingDeclaration); } else { - type = unknownType; - error(declaration.name, ts.Diagnostics.Type_alias_0_circularly_references_itself, symbolToString(symbol)); + buildDisplayForTypeParametersAndDelimiters(signature.typeParameters, writer, enclosingDeclaration, flags, symbolStack); } - links.declaredType = type; + buildDisplayForParametersAndDelimiters(signature.thisParameter, signature.parameters, writer, enclosingDeclaration, flags, symbolStack); + buildReturnTypeDisplay(signature, writer, enclosingDeclaration, flags, symbolStack); } - return links.declaredType; + return _displayBuilder || (_displayBuilder = { + buildSymbolDisplay: buildSymbolDisplay, + buildTypeDisplay: buildTypeDisplay, + buildTypeParameterDisplay: buildTypeParameterDisplay, + buildTypePredicateDisplay: buildTypePredicateDisplay, + buildParameterDisplay: buildParameterDisplay, + buildDisplayForParametersAndDelimiters: buildDisplayForParametersAndDelimiters, + buildDisplayForTypeParametersAndDelimiters: buildDisplayForTypeParametersAndDelimiters, + buildTypeParameterDisplayFromSymbol: buildTypeParameterDisplayFromSymbol, + buildSignatureDisplay: buildSignatureDisplay, + buildReturnTypeDisplay: buildReturnTypeDisplay + }); } - function isLiteralEnumMember(symbol, member) { - var expr = member.initializer; - if (!expr) { - return !ts.isInAmbientContext(member); + function isDeclarationVisible(node) { + if (node) { + var links = getNodeLinks(node); + if (links.isVisible === undefined) { + links.isVisible = !!determineIfDeclarationIsVisible(); + } + return links.isVisible; } - return expr.kind === 8 /* NumericLiteral */ || - expr.kind === 185 /* PrefixUnaryExpression */ && expr.operator === 36 /* MinusToken */ && - expr.operand.kind === 8 /* NumericLiteral */ || - expr.kind === 69 /* Identifier */ && !!symbol.exports[expr.text]; - } - function enumHasLiteralMembers(symbol) { - for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { - var declaration = _a[_i]; - if (declaration.kind === 224 /* EnumDeclaration */) { - for (var _b = 0, _c = declaration.members; _b < _c.length; _b++) { - var member = _c[_b]; - if (!isLiteralEnumMember(symbol, member)) { + return false; + function determineIfDeclarationIsVisible() { + switch (node.kind) { + case 169 /* BindingElement */: + return isDeclarationVisible(node.parent.parent); + case 218 /* VariableDeclaration */: + if (ts.isBindingPattern(node.name) && + !node.name.elements.length) { + // If the binding pattern is empty, this variable declaration is not visible return false; } - } + // Otherwise fall through + case 225 /* ModuleDeclaration */: + case 221 /* ClassDeclaration */: + case 222 /* InterfaceDeclaration */: + case 223 /* TypeAliasDeclaration */: + case 220 /* FunctionDeclaration */: + case 224 /* EnumDeclaration */: + case 229 /* ImportEqualsDeclaration */: + // external module augmentation is always visible + if (ts.isExternalModuleAugmentation(node)) { + return true; + } + var parent_10 = getDeclarationContainer(node); + // If the node is not exported or it is not ambient module element (except import declaration) + if (!(ts.getCombinedModifierFlags(node) & 1 /* Export */) && + !(node.kind !== 229 /* ImportEqualsDeclaration */ && parent_10.kind !== 256 /* SourceFile */ && ts.isInAmbientContext(parent_10))) { + return isGlobalSourceFile(parent_10); + } + // Exported members/ambient module elements (exception import declaration) are visible if parent is visible + return isDeclarationVisible(parent_10); + case 145 /* PropertyDeclaration */: + case 144 /* PropertySignature */: + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + case 147 /* MethodDeclaration */: + case 146 /* MethodSignature */: + if (ts.getModifierFlags(node) & (8 /* Private */ | 16 /* Protected */)) { + // Private/protected properties/methods are not visible + return false; + } + // Public properties/methods are visible if its parents are visible, so const it fall into next case statement + case 148 /* Constructor */: + case 152 /* ConstructSignature */: + case 151 /* CallSignature */: + case 153 /* IndexSignature */: + case 142 /* Parameter */: + case 226 /* ModuleBlock */: + case 156 /* FunctionType */: + case 157 /* ConstructorType */: + case 159 /* TypeLiteral */: + case 155 /* TypeReference */: + case 160 /* ArrayType */: + case 161 /* TupleType */: + case 162 /* UnionType */: + case 163 /* IntersectionType */: + case 164 /* ParenthesizedType */: + return isDeclarationVisible(node.parent); + // Default binding, import specifier and namespace import is visible + // only on demand so by default it is not visible + case 231 /* ImportClause */: + case 232 /* NamespaceImport */: + case 234 /* ImportSpecifier */: + return false; + // Type parameters are always visible + case 141 /* TypeParameter */: + // Source file and namespace export are always visible + case 256 /* SourceFile */: + case 228 /* NamespaceExportDeclaration */: + return true; + // Export assignments do not create name bindings outside the module + case 235 /* ExportAssignment */: + return false; + default: + return false; } } - return true; - } - function createEnumLiteralType(symbol, baseType, text) { - var type = createType(256 /* EnumLiteral */); - type.symbol = symbol; - type.baseType = baseType; - type.text = text; - return type; } - function getDeclaredTypeOfEnum(symbol) { - var links = getSymbolLinks(symbol); - if (!links.declaredType) { - var enumType = links.declaredType = createType(16 /* Enum */); - enumType.symbol = symbol; - if (enumHasLiteralMembers(symbol)) { - var memberTypeList = []; - var memberTypes = ts.createMap(); - for (var _i = 0, _a = enumType.symbol.declarations; _i < _a.length; _i++) { - var declaration = _a[_i]; - if (declaration.kind === 224 /* EnumDeclaration */) { - computeEnumMemberValues(declaration); - for (var _b = 0, _c = declaration.members; _b < _c.length; _b++) { - var member = _c[_b]; - var memberSymbol = getSymbolOfNode(member); - var value = getEnumMemberValue(member); - if (!memberTypes[value]) { - var memberType = memberTypes[value] = createEnumLiteralType(memberSymbol, enumType, "" + value); - memberTypeList.push(memberType); - } - } - } + function collectLinkedAliases(node) { + var exportSymbol; + if (node.parent && node.parent.kind === 235 /* ExportAssignment */) { + exportSymbol = resolveName(node.parent, node.text, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */ | 8388608 /* Alias */, ts.Diagnostics.Cannot_find_name_0, node); + } + else if (node.parent.kind === 238 /* ExportSpecifier */) { + var exportSpecifier = node.parent; + exportSymbol = exportSpecifier.parent.parent.moduleSpecifier ? + getExternalModuleMember(exportSpecifier.parent.parent, exportSpecifier) : + resolveEntityName(exportSpecifier.propertyName || exportSpecifier.name, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */ | 8388608 /* Alias */); + } + var result = []; + if (exportSymbol) { + buildVisibleNodeList(exportSymbol.declarations); + } + return result; + function buildVisibleNodeList(declarations) { + ts.forEach(declarations, function (declaration) { + getNodeLinks(declaration).isVisible = true; + var resultNode = getAnyImportSyntax(declaration) || declaration; + if (!ts.contains(result, resultNode)) { + result.push(resultNode); } - enumType.memberTypes = memberTypes; - if (memberTypeList.length > 1) { - enumType.flags |= 524288 /* Union */; - enumType.types = memberTypeList; - unionTypes[getTypeListId(memberTypeList)] = enumType; + if (ts.isInternalModuleImportEqualsDeclaration(declaration)) { + // Add the referenced top container visible + var internalModuleReference = declaration.moduleReference; + var firstIdentifier = getFirstIdentifier(internalModuleReference); + var importSymbol = resolveName(declaration, firstIdentifier.text, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */, undefined, undefined); + if (importSymbol) { + buildVisibleNodeList(importSymbol.declarations); + } } - } - } - return links.declaredType; - } - function getDeclaredTypeOfEnumMember(symbol) { - var links = getSymbolLinks(symbol); - if (!links.declaredType) { - var enumType = getDeclaredTypeOfEnum(getParentOfSymbol(symbol)); - links.declaredType = enumType.flags & 524288 /* Union */ ? - enumType.memberTypes[getEnumMemberValue(symbol.valueDeclaration)] : - enumType; + }); } - return links.declaredType; } - function getDeclaredTypeOfTypeParameter(symbol) { - var links = getSymbolLinks(symbol); - if (!links.declaredType) { - var type = createType(16384 /* TypeParameter */); - type.symbol = symbol; - if (!ts.getDeclarationOfKind(symbol, 141 /* TypeParameter */).constraint) { - type.constraint = noConstraintType; + /** + * Push an entry on the type resolution stack. If an entry with the given target and the given property name + * is already on the stack, and no entries in between already have a type, then a circularity has occurred. + * In this case, the result values of the existing entry and all entries pushed after it are changed to false, + * and the value false is returned. Otherwise, the new entry is just pushed onto the stack, and true is returned. + * In order to see if the same query has already been done before, the target object and the propertyName both + * must match the one passed in. + * + * @param target The symbol, type, or signature whose type is being queried + * @param propertyName The property name that should be used to query the target for its type + */ + function pushTypeResolution(target, propertyName) { + var resolutionCycleStartIndex = findResolutionCycleStartIndex(target, propertyName); + if (resolutionCycleStartIndex >= 0) { + // A cycle was found + var length_2 = resolutionTargets.length; + for (var i = resolutionCycleStartIndex; i < length_2; i++) { + resolutionResults[i] = false; } - links.declaredType = type; + return false; } - return links.declaredType; + resolutionTargets.push(target); + resolutionResults.push(/*items*/ true); + resolutionPropertyNames.push(propertyName); + return true; } - function getDeclaredTypeOfAlias(symbol) { - var links = getSymbolLinks(symbol); - if (!links.declaredType) { - links.declaredType = getDeclaredTypeOfSymbol(resolveAlias(symbol)); + function findResolutionCycleStartIndex(target, propertyName) { + for (var i = resolutionTargets.length - 1; i >= 0; i--) { + if (hasType(resolutionTargets[i], resolutionPropertyNames[i])) { + return -1; + } + if (resolutionTargets[i] === target && resolutionPropertyNames[i] === propertyName) { + return i; + } } - return links.declaredType; + return -1; } - function getDeclaredTypeOfSymbol(symbol) { - ts.Debug.assert((symbol.flags & 16777216 /* Instantiated */) === 0); - if (symbol.flags & (32 /* Class */ | 64 /* Interface */)) { - return getDeclaredTypeOfClassOrInterface(symbol); - } - if (symbol.flags & 524288 /* TypeAlias */) { - return getDeclaredTypeOfTypeAlias(symbol); - } - if (symbol.flags & 262144 /* TypeParameter */) { - return getDeclaredTypeOfTypeParameter(symbol); + function hasType(target, propertyName) { + if (propertyName === 0 /* Type */) { + return getSymbolLinks(target).type; } - if (symbol.flags & 384 /* Enum */) { - return getDeclaredTypeOfEnum(symbol); + if (propertyName === 2 /* DeclaredType */) { + return getSymbolLinks(target).declaredType; } - if (symbol.flags & 8 /* EnumMember */) { - return getDeclaredTypeOfEnumMember(symbol); + if (propertyName === 1 /* ResolvedBaseConstructorType */) { + ts.Debug.assert(!!(target.flags & 32768 /* Class */)); + return target.resolvedBaseConstructorType; } - if (symbol.flags & 8388608 /* Alias */) { - return getDeclaredTypeOfAlias(symbol); + if (propertyName === 3 /* ResolvedReturnType */) { + return target.resolvedReturnType; } - return unknownType; + ts.Debug.fail("Unhandled TypeSystemPropertyName " + propertyName); } - // A type reference is considered independent if each type argument is considered independent. - function isIndependentTypeReference(node) { - if (node.typeArguments) { - for (var _i = 0, _a = node.typeArguments; _i < _a.length; _i++) { - var typeNode = _a[_i]; - if (!isIndependentType(typeNode)) { - return false; - } + // Pop an entry from the type resolution stack and return its associated result value. The result value will + // be true if no circularities were detected, or false if a circularity was found. + function popTypeResolution() { + resolutionTargets.pop(); + resolutionPropertyNames.pop(); + return resolutionResults.pop(); + } + function getDeclarationContainer(node) { + node = ts.getRootDeclaration(node); + while (node) { + switch (node.kind) { + case 218 /* VariableDeclaration */: + case 219 /* VariableDeclarationList */: + case 234 /* ImportSpecifier */: + case 233 /* NamedImports */: + case 232 /* NamespaceImport */: + case 231 /* ImportClause */: + node = node.parent; + break; + default: + return node.parent; } } - return true; } - // A type is considered independent if it the any, string, number, boolean, symbol, or void keyword, a string - // literal type, an array with an element type that is considered independent, or a type reference that is - // considered independent. - function isIndependentType(node) { - switch (node.kind) { - case 117 /* AnyKeyword */: - case 132 /* StringKeyword */: - case 130 /* NumberKeyword */: - case 120 /* BooleanKeyword */: - case 133 /* SymbolKeyword */: - case 103 /* VoidKeyword */: - case 135 /* UndefinedKeyword */: - case 93 /* NullKeyword */: - case 127 /* NeverKeyword */: - case 166 /* LiteralType */: - return true; - case 160 /* ArrayType */: - return isIndependentType(node.elementType); - case 155 /* TypeReference */: - return isIndependentTypeReference(node); - } - return false; + function getTypeOfPrototypeProperty(prototype) { + // TypeScript 1.0 spec (April 2014): 8.4 + // Every class automatically contains a static property member named 'prototype', + // the type of which is an instantiation of the class type with type Any supplied as a type argument for each type parameter. + // It is an error to explicitly declare a static property member with the name 'prototype'. + var classType = getDeclaredTypeOfSymbol(getParentOfSymbol(prototype)); + return classType.typeParameters ? createTypeReference(classType, ts.map(classType.typeParameters, function (_) { return anyType; })) : classType; } - // A variable-like declaration is considered independent (free of this references) if it has a type annotation - // that specifies an independent type, or if it has no type annotation and no initializer (and thus of type any). - function isIndependentVariableLikeDeclaration(node) { - return node.type && isIndependentType(node.type) || !node.type && !node.initializer; + // Return the type of the given property in the given type, or undefined if no such property exists + function getTypeOfPropertyOfType(type, name) { + var prop = getPropertyOfType(type, name); + return prop ? getTypeOfSymbol(prop) : undefined; } - // A function-like declaration is considered independent (free of this references) if it has a return type - // annotation that is considered independent and if each parameter is considered independent. - function isIndependentFunctionLikeDeclaration(node) { - if (node.kind !== 148 /* Constructor */ && (!node.type || !isIndependentType(node.type))) { - return false; - } - for (var _i = 0, _a = node.parameters; _i < _a.length; _i++) { - var parameter = _a[_i]; - if (!isIndependentVariableLikeDeclaration(parameter)) { - return false; - } - } - return true; + function isTypeAny(type) { + return type && (type.flags & 1 /* Any */) !== 0; } - // Returns true if the class or interface member given by the symbol is free of "this" references. The - // function may return false for symbols that are actually free of "this" references because it is not - // feasible to perform a complete analysis in all cases. In particular, property members with types - // inferred from their initializers and function members with inferred return types are conservatively - // assumed not to be free of "this" references. - function isIndependentMember(symbol) { - if (symbol.declarations && symbol.declarations.length === 1) { - var declaration = symbol.declarations[0]; - if (declaration) { - switch (declaration.kind) { - case 145 /* PropertyDeclaration */: - case 144 /* PropertySignature */: - return isIndependentVariableLikeDeclaration(declaration); - case 147 /* MethodDeclaration */: - case 146 /* MethodSignature */: - case 148 /* Constructor */: - return isIndependentFunctionLikeDeclaration(declaration); - } - } - } - return false; + function isTypeNever(type) { + return type && (type.flags & 8192 /* Never */) !== 0; } - function createSymbolTable(symbols) { - var result = ts.createMap(); - for (var _i = 0, symbols_1 = symbols; _i < symbols_1.length; _i++) { - var symbol = symbols_1[_i]; - result[symbol.name] = symbol; - } - return result; + // Return the type of a binding element parent. We check SymbolLinks first to see if a type has been + // assigned by contextual typing. + function getTypeForBindingElementParent(node) { + var symbol = getSymbolOfNode(node); + return symbol && getSymbolLinks(symbol).type || getTypeForVariableLikeDeclaration(node, /*includeOptionality*/ false); } - // The mappingThisOnly flag indicates that the only type parameter being mapped is "this". When the flag is true, - // we check symbols to see if we can quickly conclude they are free of "this" references, thus needing no instantiation. - function createInstantiatedSymbolTable(symbols, mapper, mappingThisOnly) { - var result = ts.createMap(); - for (var _i = 0, symbols_2 = symbols; _i < symbols_2.length; _i++) { - var symbol = symbols_2[_i]; - result[symbol.name] = mappingThisOnly && isIndependentMember(symbol) ? symbol : instantiateSymbol(symbol, mapper); + function getTextOfPropertyName(name) { + switch (name.kind) { + case 69 /* Identifier */: + return name.text; + case 9 /* StringLiteral */: + case 8 /* NumericLiteral */: + return name.text; + case 140 /* ComputedPropertyName */: + if (ts.isStringOrNumericLiteral(name.expression.kind)) { + return name.expression.text; + } } - return result; + return undefined; } - function addInheritedMembers(symbols, baseSymbols) { - for (var _i = 0, baseSymbols_1 = baseSymbols; _i < baseSymbols_1.length; _i++) { - var s = baseSymbols_1[_i]; - if (!symbols[s.name]) { - symbols[s.name] = s; - } - } + function isComputedNonLiteralName(name) { + return name.kind === 140 /* ComputedPropertyName */ && !ts.isStringOrNumericLiteral(name.expression.kind); } - function resolveDeclaredMembers(type) { - if (!type.declaredProperties) { - var symbol = type.symbol; - type.declaredProperties = getNamedMembers(symbol.members); - type.declaredCallSignatures = getSignaturesOfSymbol(symbol.members["__call"]); - type.declaredConstructSignatures = getSignaturesOfSymbol(symbol.members["__new"]); - type.declaredStringIndexInfo = getIndexInfoOfSymbol(symbol, 0 /* String */); - type.declaredNumberIndexInfo = getIndexInfoOfSymbol(symbol, 1 /* Number */); + /** Return the inferred type for a binding element */ + function getTypeForBindingElement(declaration) { + var pattern = declaration.parent; + var parentType = getTypeForBindingElementParent(pattern.parent); + // If parent has the unknown (error) type, then so does this binding element + if (parentType === unknownType) { + return unknownType; } - return type; - } - function getTypeWithThisArgument(type, thisArgument) { - if (type.flags & 131072 /* Reference */) { - return createTypeReference(type.target, ts.concatenate(type.typeArguments, [thisArgument || type.target.thisType])); + // If no type was specified or inferred for parent, or if the specified or inferred type is any, + // infer from the initializer of the binding element if one is present. Otherwise, go with the + // undefined or any type of the parent. + if (!parentType || isTypeAny(parentType)) { + if (declaration.initializer) { + return checkDeclarationInitializer(declaration); + } + return parentType; } - return type; - } - function resolveObjectTypeMembers(type, source, typeParameters, typeArguments) { - var mapper; - var members; - var callSignatures; - var constructSignatures; - var stringIndexInfo; - var numberIndexInfo; - if (ts.rangeEquals(typeParameters, typeArguments, 0, typeParameters.length)) { - mapper = identityMapper; - members = source.symbol ? source.symbol.members : createSymbolTable(source.declaredProperties); - callSignatures = source.declaredCallSignatures; - constructSignatures = source.declaredConstructSignatures; - stringIndexInfo = source.declaredStringIndexInfo; - numberIndexInfo = source.declaredNumberIndexInfo; + var type; + if (pattern.kind === 167 /* ObjectBindingPattern */) { + // Use explicitly specified property name ({ p: xxx } form), or otherwise the implied name ({ p } form) + var name_14 = declaration.propertyName || declaration.name; + if (isComputedNonLiteralName(name_14)) { + // computed properties with non-literal names are treated as 'any' + return anyType; + } + if (declaration.initializer) { + getContextualType(declaration.initializer); + } + // Use type of the specified property, or otherwise, for a numeric name, the type of the numeric index signature, + // or otherwise the type of the string index signature. + var text = getTextOfPropertyName(name_14); + type = getTypeOfPropertyOfType(parentType, text) || + isNumericLiteralName(text) && getIndexTypeOfType(parentType, 1 /* Number */) || + getIndexTypeOfType(parentType, 0 /* String */); + if (!type) { + error(name_14, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(parentType), ts.declarationNameToString(name_14)); + return unknownType; + } } else { - mapper = createTypeMapper(typeParameters, typeArguments); - members = createInstantiatedSymbolTable(source.declaredProperties, mapper, /*mappingThisOnly*/ typeParameters.length === 1); - callSignatures = instantiateList(source.declaredCallSignatures, mapper, instantiateSignature); - constructSignatures = instantiateList(source.declaredConstructSignatures, mapper, instantiateSignature); - stringIndexInfo = instantiateIndexInfo(source.declaredStringIndexInfo, mapper); - numberIndexInfo = instantiateIndexInfo(source.declaredNumberIndexInfo, mapper); - } - var baseTypes = getBaseTypes(source); - if (baseTypes.length) { - if (source.symbol && members === source.symbol.members) { - members = createSymbolTable(source.declaredProperties); + // This elementType will be used if the specific property corresponding to this index is not + // present (aka the tuple element property). This call also checks that the parentType is in + // fact an iterable or array (depending on target language). + var elementType = checkIteratedTypeOrElementType(parentType, pattern, /*allowStringInput*/ false); + if (!declaration.dotDotDotToken) { + // Use specific property type when parent is a tuple or numeric index type when parent is an array + var propName = "" + ts.indexOf(pattern.elements, declaration); + type = isTupleLikeType(parentType) + ? getTypeOfPropertyOfType(parentType, propName) + : elementType; + if (!type) { + if (isTupleType(parentType)) { + error(declaration, ts.Diagnostics.Tuple_type_0_with_length_1_cannot_be_assigned_to_tuple_with_length_2, typeToString(parentType), getTypeReferenceArity(parentType), pattern.elements.length); + } + else { + error(declaration, ts.Diagnostics.Type_0_has_no_property_1, typeToString(parentType), propName); + } + return unknownType; + } } - var thisArgument = ts.lastOrUndefined(typeArguments); - for (var _i = 0, baseTypes_1 = baseTypes; _i < baseTypes_1.length; _i++) { - var baseType = baseTypes_1[_i]; - var instantiatedBaseType = thisArgument ? getTypeWithThisArgument(instantiateType(baseType, mapper), thisArgument) : baseType; - addInheritedMembers(members, getPropertiesOfObjectType(instantiatedBaseType)); - callSignatures = ts.concatenate(callSignatures, getSignaturesOfType(instantiatedBaseType, 0 /* Call */)); - constructSignatures = ts.concatenate(constructSignatures, getSignaturesOfType(instantiatedBaseType, 1 /* Construct */)); - stringIndexInfo = stringIndexInfo || getIndexInfoOfType(instantiatedBaseType, 0 /* String */); - numberIndexInfo = numberIndexInfo || getIndexInfoOfType(instantiatedBaseType, 1 /* Number */); + else { + // Rest element has an array type with the same element type as the parent type + type = createArrayType(elementType); } } - setObjectTypeMembers(type, members, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo); - } - function resolveClassOrInterfaceMembers(type) { - resolveObjectTypeMembers(type, resolveDeclaredMembers(type), emptyArray, emptyArray); - } - function resolveTypeReferenceMembers(type) { - var source = resolveDeclaredMembers(type.target); - var typeParameters = ts.concatenate(source.typeParameters, [source.thisType]); - var typeArguments = type.typeArguments && type.typeArguments.length === typeParameters.length ? - type.typeArguments : ts.concatenate(type.typeArguments, [type]); - resolveObjectTypeMembers(type, source, typeParameters, typeArguments); - } - function createSignature(declaration, typeParameters, thisParameter, parameters, resolvedReturnType, typePredicate, minArgumentCount, hasRestParameter, hasLiteralTypes) { - var sig = new Signature(checker); - sig.declaration = declaration; - sig.typeParameters = typeParameters; - sig.parameters = parameters; - sig.thisParameter = thisParameter; - sig.resolvedReturnType = resolvedReturnType; - sig.typePredicate = typePredicate; - sig.minArgumentCount = minArgumentCount; - sig.hasRestParameter = hasRestParameter; - sig.hasLiteralTypes = hasLiteralTypes; - return sig; + // In strict null checking mode, if a default value of a non-undefined type is specified, remove + // undefined from the final type. + if (strictNullChecks && declaration.initializer && !(getFalsyFlags(checkExpressionCached(declaration.initializer)) & 2048 /* Undefined */)) { + type = getTypeWithFacts(type, 131072 /* NEUndefined */); + } + return declaration.initializer ? + getUnionType([type, checkExpressionCached(declaration.initializer)], /*subtypeReduction*/ true) : + type; } - function cloneSignature(sig) { - return createSignature(sig.declaration, sig.typeParameters, sig.thisParameter, sig.parameters, sig.resolvedReturnType, sig.typePredicate, sig.minArgumentCount, sig.hasRestParameter, sig.hasLiteralTypes); + function getTypeForVariableLikeDeclarationFromJSDocComment(declaration) { + var jsDocType = getJSDocTypeForVariableLikeDeclarationFromJSDocComment(declaration); + if (jsDocType) { + return getTypeFromTypeNode(jsDocType); + } } - function getDefaultConstructSignatures(classType) { - var baseConstructorType = getBaseConstructorTypeOfClass(classType); - var baseSignatures = getSignaturesOfType(baseConstructorType, 1 /* Construct */); - if (baseSignatures.length === 0) { - return [createSignature(undefined, classType.localTypeParameters, undefined, emptyArray, classType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false)]; + function getJSDocTypeForVariableLikeDeclarationFromJSDocComment(declaration) { + // First, see if this node has an @type annotation on it directly. + var typeTag = ts.getJSDocTypeTag(declaration); + if (typeTag && typeTag.typeExpression) { + return typeTag.typeExpression.type; } - var baseTypeNode = getBaseTypeNodeOfClass(classType); - var typeArguments = ts.map(baseTypeNode.typeArguments, getTypeFromTypeNodeNoAlias); - var typeArgCount = typeArguments ? typeArguments.length : 0; - var result = []; - for (var _i = 0, baseSignatures_1 = baseSignatures; _i < baseSignatures_1.length; _i++) { - var baseSig = baseSignatures_1[_i]; - var typeParamCount = baseSig.typeParameters ? baseSig.typeParameters.length : 0; - if (typeParamCount === typeArgCount) { - var sig = typeParamCount ? getSignatureInstantiation(baseSig, typeArguments) : cloneSignature(baseSig); - sig.typeParameters = classType.localTypeParameters; - sig.resolvedReturnType = classType; - result.push(sig); + if (declaration.kind === 218 /* VariableDeclaration */ && + declaration.parent.kind === 219 /* VariableDeclarationList */ && + declaration.parent.parent.kind === 200 /* VariableStatement */) { + // @type annotation might have been on the variable statement, try that instead. + var annotation = ts.getJSDocTypeTag(declaration.parent.parent); + if (annotation && annotation.typeExpression) { + return annotation.typeExpression.type; } } - return result; - } - function findMatchingSignature(signatureList, signature, partialMatch, ignoreThisTypes, ignoreReturnTypes) { - for (var _i = 0, signatureList_1 = signatureList; _i < signatureList_1.length; _i++) { - var s = signatureList_1[_i]; - if (compareSignaturesIdentical(s, signature, partialMatch, ignoreThisTypes, ignoreReturnTypes, compareTypesIdentical)) { - return s; + else if (declaration.kind === 142 /* Parameter */) { + // If it's a parameter, see if the parent has a jsdoc comment with an @param + // annotation. + var paramTag = ts.getCorrespondingJSDocParameterTag(declaration); + if (paramTag && paramTag.typeExpression) { + return paramTag.typeExpression.type; } } + return undefined; } - function findMatchingSignatures(signatureLists, signature, listIndex) { - if (signature.typeParameters) { - // We require an exact match for generic signatures, so we only return signatures from the first - // signature list and only if they have exact matches in the other signature lists. - if (listIndex > 0) { - return undefined; + function isAutoVariableInitializer(initializer) { + var expr = initializer && ts.skipParentheses(initializer); + return !expr || expr.kind === 93 /* NullKeyword */ || expr.kind === 69 /* Identifier */ && getResolvedSymbol(expr) === undefinedSymbol; + } + function addOptionality(type, optional) { + return strictNullChecks && optional ? includeFalsyTypes(type, 2048 /* Undefined */) : type; + } + // Return the inferred type for a variable, parameter, or property declaration + function getTypeForVariableLikeDeclaration(declaration, includeOptionality) { + if (declaration.flags & 1048576 /* JavaScriptFile */) { + // If this is a variable in a JavaScript file, then use the JSDoc type (if it has + // one as its type), otherwise fallback to the below standard TS codepaths to + // try to figure it out. + var type = getTypeForVariableLikeDeclarationFromJSDocComment(declaration); + if (type && type !== unknownType) { + return type; } - for (var i = 1; i < signatureLists.length; i++) { - if (!findMatchingSignature(signatureLists[i], signature, /*partialMatch*/ false, /*ignoreThisTypes*/ false, /*ignoreReturnTypes*/ false)) { - return undefined; + } + // A variable declared in a for..in statement is always of type string + if (declaration.parent.parent.kind === 207 /* ForInStatement */) { + return stringType; + } + if (declaration.parent.parent.kind === 208 /* ForOfStatement */) { + // checkRightHandSideOfForOf will return undefined if the for-of expression type was + // missing properties/signatures required to get its iteratedType (like + // [Symbol.iterator] or next). This may be because we accessed properties from anyType, + // or it may have led to an error inside getElementTypeOfIterable. + return checkRightHandSideOfForOf(declaration.parent.parent.expression) || anyType; + } + if (ts.isBindingPattern(declaration.parent)) { + return getTypeForBindingElement(declaration); + } + // Use type from type annotation if one is present + if (declaration.type) { + return addOptionality(getTypeFromTypeNode(declaration.type), /*optional*/ declaration.questionToken && includeOptionality); + } + // Use control flow type inference for non-ambient, non-exported var or let variables with no initializer + // or a 'null' or 'undefined' initializer. + if (declaration.kind === 218 /* VariableDeclaration */ && !ts.isBindingPattern(declaration.name) && + !(ts.getCombinedNodeFlags(declaration) & 2 /* Const */) && !(ts.getCombinedModifierFlags(declaration) & 1 /* Export */) && + !ts.isInAmbientContext(declaration) && isAutoVariableInitializer(declaration.initializer)) { + return autoType; + } + if (declaration.kind === 142 /* Parameter */) { + var func = declaration.parent; + // For a parameter of a set accessor, use the type of the get accessor if one is present + if (func.kind === 150 /* SetAccessor */ && !ts.hasDynamicName(func)) { + var getter = ts.getDeclarationOfKind(declaration.parent.symbol, 149 /* GetAccessor */); + if (getter) { + var getterSignature = getSignatureFromDeclaration(getter); + var thisParameter = getAccessorThisParameter(func); + if (thisParameter && declaration === thisParameter) { + // Use the type from the *getter* + ts.Debug.assert(!thisParameter.type); + return getTypeOfSymbol(getterSignature.thisParameter); + } + return getReturnTypeOfSignature(getterSignature); } } - return [signature]; - } - var result = undefined; - for (var i = 0; i < signatureLists.length; i++) { - // Allow matching non-generic signatures to have excess parameters and different return types - var match = i === listIndex ? signature : findMatchingSignature(signatureLists[i], signature, /*partialMatch*/ true, /*ignoreThisTypes*/ true, /*ignoreReturnTypes*/ true); - if (!match) { - return undefined; + // Use contextual parameter type if one is available + var type = void 0; + if (declaration.symbol.name === "this") { + var thisParameter = getContextualThisParameter(func); + type = thisParameter ? getTypeOfSymbol(thisParameter) : undefined; } - if (!ts.contains(result, match)) { - (result || (result = [])).push(match); + else { + type = getContextuallyTypedParameterType(declaration); + } + if (type) { + return addOptionality(type, /*optional*/ declaration.questionToken && includeOptionality); } } - return result; + // Use the type of the initializer expression if one is present + if (declaration.initializer) { + var type = checkDeclarationInitializer(declaration); + return addOptionality(type, /*optional*/ declaration.questionToken && includeOptionality); + } + // If it is a short-hand property assignment, use the type of the identifier + if (declaration.kind === 254 /* ShorthandPropertyAssignment */) { + return checkIdentifier(declaration.name); + } + // If the declaration specifies a binding pattern, use the type implied by the binding pattern + if (ts.isBindingPattern(declaration.name)) { + return getTypeFromBindingPattern(declaration.name, /*includePatternInType*/ false, /*reportErrors*/ true); + } + // No type specified and nothing can be inferred + return undefined; } - // The signatures of a union type are those signatures that are present in each of the constituent types. - // Generic signatures must match exactly, but non-generic signatures are allowed to have extra optional - // parameters and may differ in return types. When signatures differ in return types, the resulting return - // type is the union of the constituent return types. - function getUnionSignatures(types, kind) { - var signatureLists = ts.map(types, function (t) { return getSignaturesOfType(t, kind); }); - var result = undefined; - for (var i = 0; i < signatureLists.length; i++) { - for (var _i = 0, _a = signatureLists[i]; _i < _a.length; _i++) { - var signature = _a[_i]; - // Only process signatures with parameter lists that aren't already in the result list - if (!result || !findMatchingSignature(result, signature, /*partialMatch*/ false, /*ignoreThisTypes*/ true, /*ignoreReturnTypes*/ true)) { - var unionSignatures = findMatchingSignatures(signatureLists, signature, i); - if (unionSignatures) { - var s = signature; - // Union the result types when more than one signature matches - if (unionSignatures.length > 1) { - s = cloneSignature(signature); - if (ts.forEach(unionSignatures, function (sig) { return sig.thisParameter; })) { - var thisType = getUnionType(ts.map(unionSignatures, function (sig) { return getTypeOfSymbol(sig.thisParameter) || anyType; }), /*subtypeReduction*/ true); - s.thisParameter = createTransientSymbol(signature.thisParameter, thisType); - } - // Clear resolved return type we possibly got from cloneSignature - s.resolvedReturnType = undefined; - s.unionSignatures = unionSignatures; - } - (result || (result = [])).push(s); - } - } - } + // Return the type implied by a binding pattern element. This is the type of the initializer of the element if + // one is present. Otherwise, if the element is itself a binding pattern, it is the type implied by the binding + // pattern. Otherwise, it is the type any. + function getTypeFromBindingElement(element, includePatternInType, reportErrors) { + if (element.initializer) { + return checkDeclarationInitializer(element); } - return result || emptyArray; + if (ts.isBindingPattern(element.name)) { + return getTypeFromBindingPattern(element.name, includePatternInType, reportErrors); + } + if (reportErrors && compilerOptions.noImplicitAny && !declarationBelongsToPrivateAmbientMember(element)) { + reportImplicitAnyError(element, anyType); + } + return anyType; } - function getUnionIndexInfo(types, kind) { - var indexTypes = []; - var isAnyReadonly = false; - for (var _i = 0, types_1 = types; _i < types_1.length; _i++) { - var type = types_1[_i]; - var indexInfo = getIndexInfoOfType(type, kind); - if (!indexInfo) { - return undefined; + // Return the type implied by an object binding pattern + function getTypeFromObjectBindingPattern(pattern, includePatternInType, reportErrors) { + var members = ts.createMap(); + var hasComputedProperties = false; + ts.forEach(pattern.elements, function (e) { + var name = e.propertyName || e.name; + if (isComputedNonLiteralName(name)) { + // do not include computed properties in the implied type + hasComputedProperties = true; + return; } - indexTypes.push(indexInfo.type); - isAnyReadonly = isAnyReadonly || indexInfo.isReadonly; + var text = getTextOfPropertyName(name); + var flags = 4 /* Property */ | 67108864 /* Transient */ | (e.initializer ? 536870912 /* Optional */ : 0); + var symbol = createSymbol(flags, text); + symbol.type = getTypeFromBindingElement(e, includePatternInType, reportErrors); + symbol.bindingElement = e; + members[symbol.name] = symbol; + }); + var result = createAnonymousType(undefined, members, emptyArray, emptyArray, undefined, undefined); + if (includePatternInType) { + result.pattern = pattern; } - return createIndexInfo(getUnionType(indexTypes, /*subtypeReduction*/ true), isAnyReadonly); + if (hasComputedProperties) { + result.isObjectLiteralPatternWithComputedProperties = true; + } + return result; } - function resolveUnionTypeMembers(type) { - // The members and properties collections are empty for union types. To get all properties of a union - // type use getPropertiesOfType (only the language service uses this). - var callSignatures = getUnionSignatures(type.types, 0 /* Call */); - var constructSignatures = getUnionSignatures(type.types, 1 /* Construct */); - var stringIndexInfo = getUnionIndexInfo(type.types, 0 /* String */); - var numberIndexInfo = getUnionIndexInfo(type.types, 1 /* Number */); - setObjectTypeMembers(type, emptySymbols, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo); + // Return the type implied by an array binding pattern + function getTypeFromArrayBindingPattern(pattern, includePatternInType, reportErrors) { + var elements = pattern.elements; + var lastElement = ts.lastOrUndefined(elements); + if (elements.length === 0 || (!ts.isOmittedExpression(lastElement) && lastElement.dotDotDotToken)) { + return languageVersion >= 2 /* ES6 */ ? createIterableType(anyType) : anyArrayType; + } + // If the pattern has at least one element, and no rest element, then it should imply a tuple type. + var elementTypes = ts.map(elements, function (e) { return ts.isOmittedExpression(e) ? anyType : getTypeFromBindingElement(e, includePatternInType, reportErrors); }); + var result = createTupleType(elementTypes); + if (includePatternInType) { + result = cloneTypeReference(result); + result.pattern = pattern; + } + return result; } - function intersectTypes(type1, type2) { - return !type1 ? type2 : !type2 ? type1 : getIntersectionType([type1, type2]); + // Return the type implied by a binding pattern. This is the type implied purely by the binding pattern itself + // and without regard to its context (i.e. without regard any type annotation or initializer associated with the + // declaration in which the binding pattern is contained). For example, the implied type of [x, y] is [any, any] + // and the implied type of { x, y: z = 1 } is { x: any; y: number; }. The type implied by a binding pattern is + // used as the contextual type of an initializer associated with the binding pattern. Also, for a destructuring + // parameter with no type annotation or initializer, the type implied by the binding pattern becomes the type of + // the parameter. + function getTypeFromBindingPattern(pattern, includePatternInType, reportErrors) { + return pattern.kind === 167 /* ObjectBindingPattern */ + ? getTypeFromObjectBindingPattern(pattern, includePatternInType, reportErrors) + : getTypeFromArrayBindingPattern(pattern, includePatternInType, reportErrors); } - function intersectIndexInfos(info1, info2) { - return !info1 ? info2 : !info2 ? info1 : createIndexInfo(getIntersectionType([info1.type, info2.type]), info1.isReadonly && info2.isReadonly); - } - function resolveIntersectionTypeMembers(type) { - // The members and properties collections are empty for intersection types. To get all properties of an - // intersection type use getPropertiesOfType (only the language service uses this). - var callSignatures = emptyArray; - var constructSignatures = emptyArray; - var stringIndexInfo = undefined; - var numberIndexInfo = undefined; - for (var _i = 0, _a = type.types; _i < _a.length; _i++) { - var t = _a[_i]; - callSignatures = ts.concatenate(callSignatures, getSignaturesOfType(t, 0 /* Call */)); - constructSignatures = ts.concatenate(constructSignatures, getSignaturesOfType(t, 1 /* Construct */)); - stringIndexInfo = intersectIndexInfos(stringIndexInfo, getIndexInfoOfType(t, 0 /* String */)); - numberIndexInfo = intersectIndexInfos(numberIndexInfo, getIndexInfoOfType(t, 1 /* Number */)); - } - setObjectTypeMembers(type, emptySymbols, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo); - } - function resolveAnonymousTypeMembers(type) { - var symbol = type.symbol; - if (type.target) { - var members = createInstantiatedSymbolTable(getPropertiesOfObjectType(type.target), type.mapper, /*mappingThisOnly*/ false); - var callSignatures = instantiateList(getSignaturesOfType(type.target, 0 /* Call */), type.mapper, instantiateSignature); - var constructSignatures = instantiateList(getSignaturesOfType(type.target, 1 /* Construct */), type.mapper, instantiateSignature); - var stringIndexInfo = instantiateIndexInfo(getIndexInfoOfType(type.target, 0 /* String */), type.mapper); - var numberIndexInfo = instantiateIndexInfo(getIndexInfoOfType(type.target, 1 /* Number */), type.mapper); - setObjectTypeMembers(type, members, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo); - } - else if (symbol.flags & 2048 /* TypeLiteral */) { - var members = symbol.members; - var callSignatures = getSignaturesOfSymbol(members["__call"]); - var constructSignatures = getSignaturesOfSymbol(members["__new"]); - var stringIndexInfo = getIndexInfoOfSymbol(symbol, 0 /* String */); - var numberIndexInfo = getIndexInfoOfSymbol(symbol, 1 /* Number */); - setObjectTypeMembers(type, members, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo); - } - else { - // Combinations of function, class, enum and module - var members = emptySymbols; - var constructSignatures = emptyArray; - if (symbol.flags & 1952 /* HasExports */) { - members = getExportsOfSymbol(symbol); + // Return the type associated with a variable, parameter, or property declaration. In the simple case this is the type + // specified in a type annotation or inferred from an initializer. However, in the case of a destructuring declaration it + // is a bit more involved. For example: + // + // var [x, s = ""] = [1, "one"]; + // + // Here, the array literal [1, "one"] is contextually typed by the type [any, string], which is the implied type of the + // binding pattern [x, s = ""]. Because the contextual type is a tuple type, the resulting type of [1, "one"] is the + // tuple type [number, string]. Thus, the type inferred for 'x' is number and the type inferred for 's' is string. + function getWidenedTypeForVariableLikeDeclaration(declaration, reportErrors) { + var type = getTypeForVariableLikeDeclaration(declaration, /*includeOptionality*/ true); + if (type) { + if (reportErrors) { + reportErrorsFromWidening(declaration, type); } - if (symbol.flags & 32 /* Class */) { - var classType = getDeclaredTypeOfClassOrInterface(symbol); - constructSignatures = getSignaturesOfSymbol(symbol.members["__constructor"]); - if (!constructSignatures.length) { - constructSignatures = getDefaultConstructSignatures(classType); - } - var baseConstructorType = getBaseConstructorTypeOfClass(classType); - if (baseConstructorType.flags & 2588672 /* ObjectType */) { - members = createSymbolTable(getNamedMembers(members)); - addInheritedMembers(members, getPropertiesOfObjectType(baseConstructorType)); - } + // During a normal type check we'll never get to here with a property assignment (the check of the containing + // object literal uses a different path). We exclude widening only so that language services and type verification + // tools see the actual type. + if (declaration.kind === 253 /* PropertyAssignment */) { + return type; } - var numberIndexInfo = symbol.flags & 384 /* Enum */ ? enumNumberIndexInfo : undefined; - setObjectTypeMembers(type, members, emptyArray, constructSignatures, undefined, numberIndexInfo); - // We resolve the members before computing the signatures because a signature may use - // typeof with a qualified name expression that circularly references the type we are - // in the process of resolving (see issue #6072). The temporarily empty signature list - // will never be observed because a qualified name can't reference signatures. - if (symbol.flags & (16 /* Function */ | 8192 /* Method */)) { - type.callSignatures = getSignaturesOfSymbol(symbol); + return getWidenedType(type); + } + // Rest parameters default to type any[], other parameters default to type any + type = declaration.dotDotDotToken ? anyArrayType : anyType; + // Report implicit any errors unless this is a private property within an ambient declaration + if (reportErrors && compilerOptions.noImplicitAny) { + if (!declarationBelongsToPrivateAmbientMember(declaration)) { + reportImplicitAnyError(declaration, type); } } + return type; } - function resolveStructuredTypeMembers(type) { - if (!type.members) { - if (type.flags & 131072 /* Reference */) { - resolveTypeReferenceMembers(type); + function declarationBelongsToPrivateAmbientMember(declaration) { + var root = ts.getRootDeclaration(declaration); + var memberDeclaration = root.kind === 142 /* Parameter */ ? root.parent : root; + return isPrivateWithinAmbient(memberDeclaration); + } + function getTypeOfVariableOrParameterOrProperty(symbol) { + var links = getSymbolLinks(symbol); + if (!links.type) { + // Handle prototype property + if (symbol.flags & 134217728 /* Prototype */) { + return links.type = getTypeOfPrototypeProperty(symbol); } - else if (type.flags & (32768 /* Class */ | 65536 /* Interface */)) { - resolveClassOrInterfaceMembers(type); + // Handle catch clause variables + var declaration = symbol.valueDeclaration; + if (declaration.parent.kind === 252 /* CatchClause */) { + return links.type = anyType; } - else if (type.flags & 2097152 /* Anonymous */) { - resolveAnonymousTypeMembers(type); + // Handle export default expressions + if (declaration.kind === 235 /* ExportAssignment */) { + return links.type = checkExpression(declaration.expression); } - else if (type.flags & 524288 /* Union */) { - resolveUnionTypeMembers(type); + if (declaration.flags & 1048576 /* JavaScriptFile */ && declaration.kind === 280 /* JSDocPropertyTag */ && declaration.typeExpression) { + return links.type = getTypeFromTypeNode(declaration.typeExpression.type); } - else if (type.flags & 1048576 /* Intersection */) { - resolveIntersectionTypeMembers(type); + // Handle variable, parameter or property + if (!pushTypeResolution(symbol, 0 /* Type */)) { + return unknownType; + } + var type = void 0; + // Handle certain special assignment kinds, which happen to union across multiple declarations: + // * module.exports = expr + // * exports.p = expr + // * this.p = expr + // * className.prototype.method = expr + if (declaration.kind === 187 /* BinaryExpression */ || + declaration.kind === 172 /* PropertyAccessExpression */ && declaration.parent.kind === 187 /* BinaryExpression */) { + // Use JS Doc type if present on parent expression statement + if (declaration.flags & 1048576 /* JavaScriptFile */) { + var typeTag = ts.getJSDocTypeTag(declaration.parent); + if (typeTag && typeTag.typeExpression) { + return links.type = getTypeFromTypeNode(typeTag.typeExpression.type); + } + } + var declaredTypes = ts.map(symbol.declarations, function (decl) { return decl.kind === 187 /* BinaryExpression */ ? + checkExpressionCached(decl.right) : + checkExpressionCached(decl.parent.right); }); + type = getUnionType(declaredTypes, /*subtypeReduction*/ true); + } + else { + type = getWidenedTypeForVariableLikeDeclaration(declaration, /*reportErrors*/ true); + } + if (!popTypeResolution()) { + if (symbol.valueDeclaration.type) { + // Variable has type annotation that circularly references the variable itself + type = unknownType; + error(symbol.valueDeclaration, ts.Diagnostics._0_is_referenced_directly_or_indirectly_in_its_own_type_annotation, symbolToString(symbol)); + } + else { + // Variable has initializer that circularly references the variable itself + type = anyType; + if (compilerOptions.noImplicitAny) { + error(symbol.valueDeclaration, ts.Diagnostics._0_implicitly_has_type_any_because_it_does_not_have_a_type_annotation_and_is_referenced_directly_or_indirectly_in_its_own_initializer, symbolToString(symbol)); + } + } } + links.type = type; } - return type; + return links.type; } - /** Return properties of an object type or an empty array for other types */ - function getPropertiesOfObjectType(type) { - if (type.flags & 2588672 /* ObjectType */) { - return resolveStructuredTypeMembers(type).properties; + function getAnnotatedAccessorType(accessor) { + if (accessor) { + if (accessor.kind === 149 /* GetAccessor */) { + return accessor.type && getTypeFromTypeNode(accessor.type); + } + else { + var setterTypeAnnotation = ts.getSetAccessorTypeAnnotationNode(accessor); + return setterTypeAnnotation && getTypeFromTypeNode(setterTypeAnnotation); + } } - return emptyArray; + return undefined; } - /** If the given type is an object type and that type has a property by the given name, - * return the symbol for that property. Otherwise return undefined. */ - function getPropertyOfObjectType(type, name) { - if (type.flags & 2588672 /* ObjectType */) { - var resolved = resolveStructuredTypeMembers(type); - var symbol = resolved.members[name]; - if (symbol && symbolIsValue(symbol)) { - return symbol; + function getAnnotatedAccessorThisParameter(accessor) { + var parameter = getAccessorThisParameter(accessor); + return parameter && parameter.symbol; + } + function getThisTypeOfDeclaration(declaration) { + return getThisTypeOfSignature(getSignatureFromDeclaration(declaration)); + } + function getTypeOfAccessors(symbol) { + var links = getSymbolLinks(symbol); + if (!links.type) { + var getter = ts.getDeclarationOfKind(symbol, 149 /* GetAccessor */); + var setter = ts.getDeclarationOfKind(symbol, 150 /* SetAccessor */); + if (getter && getter.flags & 1048576 /* JavaScriptFile */) { + var jsDocType = getTypeForVariableLikeDeclarationFromJSDocComment(getter); + if (jsDocType) { + return links.type = jsDocType; + } + } + if (!pushTypeResolution(symbol, 0 /* Type */)) { + return unknownType; + } + var type = void 0; + // First try to see if the user specified a return type on the get-accessor. + var getterReturnType = getAnnotatedAccessorType(getter); + if (getterReturnType) { + type = getterReturnType; + } + else { + // If the user didn't specify a return type, try to use the set-accessor's parameter type. + var setterParameterType = getAnnotatedAccessorType(setter); + if (setterParameterType) { + type = setterParameterType; + } + else { + // If there are no specified types, try to infer it from the body of the get accessor if it exists. + if (getter && getter.body) { + type = getReturnTypeFromBody(getter); + } + else { + if (compilerOptions.noImplicitAny) { + if (setter) { + error(setter, ts.Diagnostics.Property_0_implicitly_has_type_any_because_its_set_accessor_lacks_a_parameter_type_annotation, symbolToString(symbol)); + } + else { + ts.Debug.assert(!!getter, "there must existed getter as we are current checking either setter or getter in this function"); + error(getter, ts.Diagnostics.Property_0_implicitly_has_type_any_because_its_get_accessor_lacks_a_return_type_annotation, symbolToString(symbol)); + } + } + type = anyType; + } + } + } + if (!popTypeResolution()) { + type = anyType; + if (compilerOptions.noImplicitAny) { + var getter_1 = ts.getDeclarationOfKind(symbol, 149 /* GetAccessor */); + error(getter_1, ts.Diagnostics._0_implicitly_has_return_type_any_because_it_does_not_have_a_return_type_annotation_and_is_referenced_directly_or_indirectly_in_one_of_its_return_expressions, symbolToString(symbol)); + } } + links.type = type; } + return links.type; } - function getPropertiesOfUnionOrIntersectionType(type) { - for (var _i = 0, _a = type.types; _i < _a.length; _i++) { - var current = _a[_i]; - for (var _b = 0, _c = getPropertiesOfType(current); _b < _c.length; _b++) { - var prop = _c[_b]; - getPropertyOfUnionOrIntersectionType(type, prop.name); + function getTypeOfFuncClassEnumModule(symbol) { + var links = getSymbolLinks(symbol); + if (!links.type) { + if (symbol.valueDeclaration.kind === 225 /* ModuleDeclaration */ && ts.isShorthandAmbientModuleSymbol(symbol)) { + links.type = anyType; } - // The properties of a union type are those that are present in all constituent types, so - // we only need to check the properties of the first type - if (type.flags & 524288 /* Union */) { - break; + else { + var type = createObjectType(2097152 /* Anonymous */, symbol); + links.type = strictNullChecks && symbol.flags & 536870912 /* Optional */ ? + includeFalsyTypes(type, 2048 /* Undefined */) : type; } } - return type.resolvedProperties ? symbolsToArray(type.resolvedProperties) : emptyArray; - } - function getPropertiesOfType(type) { - type = getApparentType(type); - return type.flags & 1572864 /* UnionOrIntersection */ ? getPropertiesOfUnionOrIntersectionType(type) : getPropertiesOfObjectType(type); + return links.type; } - /** - * The apparent type of a type parameter is the base constraint instantiated with the type parameter - * as the type argument for the 'this' type. - */ - function getApparentTypeOfTypeParameter(type) { - if (!type.resolvedApparentType) { - var constraintType = getConstraintOfTypeParameter(type); - while (constraintType && constraintType.flags & 16384 /* TypeParameter */) { - constraintType = getConstraintOfTypeParameter(constraintType); - } - type.resolvedApparentType = getTypeWithThisArgument(constraintType || emptyObjectType, type); + function getTypeOfEnumMember(symbol) { + var links = getSymbolLinks(symbol); + if (!links.type) { + links.type = getDeclaredTypeOfEnumMember(symbol); } - return type.resolvedApparentType; + return links.type; } - /** - * For a type parameter, return the base constraint of the type parameter. For the string, number, - * boolean, and symbol primitive types, return the corresponding object types. Otherwise return the - * type itself. Note that the apparent type of a union type is the union type itself. - */ - function getApparentType(type) { - if (type.flags & 16384 /* TypeParameter */) { - type = getApparentTypeOfTypeParameter(type); + function getTypeOfAlias(symbol) { + var links = getSymbolLinks(symbol); + if (!links.type) { + var targetSymbol = resolveAlias(symbol); + // It only makes sense to get the type of a value symbol. If the result of resolving + // the alias is not a value, then it has no type. To get the type associated with a + // type symbol, call getDeclaredTypeOfSymbol. + // This check is important because without it, a call to getTypeOfSymbol could end + // up recursively calling getTypeOfAlias, causing a stack overflow. + links.type = targetSymbol.flags & 107455 /* Value */ + ? getTypeOfSymbol(targetSymbol) + : unknownType; } - if (type.flags & 34 /* StringLike */) { - type = globalStringType; + return links.type; + } + function getTypeOfInstantiatedSymbol(symbol) { + var links = getSymbolLinks(symbol); + if (!links.type) { + links.type = instantiateType(getTypeOfSymbol(links.target), links.mapper); } - else if (type.flags & 340 /* NumberLike */) { - type = globalNumberType; + return links.type; + } + function getTypeOfSymbol(symbol) { + if (symbol.flags & 16777216 /* Instantiated */) { + return getTypeOfInstantiatedSymbol(symbol); } - else if (type.flags & 136 /* BooleanLike */) { - type = globalBooleanType; + if (symbol.flags & (3 /* Variable */ | 4 /* Property */)) { + return getTypeOfVariableOrParameterOrProperty(symbol); } - else if (type.flags & 512 /* ESSymbol */) { - type = getGlobalESSymbolType(); + if (symbol.flags & (16 /* Function */ | 8192 /* Method */ | 32 /* Class */ | 384 /* Enum */ | 512 /* ValueModule */)) { + return getTypeOfFuncClassEnumModule(symbol); } - return type; - } - function createUnionOrIntersectionProperty(containingType, name) { - var types = containingType.types; - var props; - // Flags we want to propagate to the result if they exist in all source symbols - var commonFlags = (containingType.flags & 1048576 /* Intersection */) ? 536870912 /* Optional */ : 0 /* None */; - var isReadonly = false; - for (var _i = 0, types_2 = types; _i < types_2.length; _i++) { - var current = types_2[_i]; - var type = getApparentType(current); - if (type !== unknownType) { - var prop = getPropertyOfType(type, name); - if (prop && !(getDeclarationModifierFlagsFromSymbol(prop) & (8 /* Private */ | 16 /* Protected */))) { - commonFlags &= prop.flags; - if (!props) { - props = [prop]; - } - else if (!ts.contains(props, prop)) { - props.push(prop); - } - if (isReadonlySymbol(prop)) { - isReadonly = true; - } - } - else if (containingType.flags & 524288 /* Union */) { - // A union type requires the property to be present in all constituent types - return undefined; - } - } + if (symbol.flags & 8 /* EnumMember */) { + return getTypeOfEnumMember(symbol); } - if (!props) { - return undefined; + if (symbol.flags & 98304 /* Accessor */) { + return getTypeOfAccessors(symbol); } - if (props.length === 1) { - return props[0]; + if (symbol.flags & 8388608 /* Alias */) { + return getTypeOfAlias(symbol); } - var propTypes = []; - var declarations = []; - var commonType = undefined; - var hasCommonType = true; - for (var _a = 0, props_1 = props; _a < props_1.length; _a++) { - var prop = props_1[_a]; - if (prop.declarations) { - ts.addRange(declarations, prop.declarations); - } - var type = getTypeOfSymbol(prop); - if (!commonType) { - commonType = type; - } - else if (type !== commonType) { - hasCommonType = false; - } - propTypes.push(getTypeOfSymbol(prop)); + return unknownType; + } + function getTargetType(type) { + return type.flags & 131072 /* Reference */ ? type.target : type; + } + function hasBaseType(type, checkBase) { + return check(type); + function check(type) { + var target = getTargetType(type); + return target === checkBase || ts.forEach(getBaseTypes(target), check); } - var result = createSymbol(4 /* Property */ | - 67108864 /* Transient */ | - 268435456 /* SyntheticProperty */ | - commonFlags, name); - result.containingType = containingType; - result.hasCommonType = hasCommonType; - result.declarations = declarations; - result.isReadonly = isReadonly; - result.type = containingType.flags & 524288 /* Union */ ? getUnionType(propTypes) : getIntersectionType(propTypes); - return result; } - function getPropertyOfUnionOrIntersectionType(type, name) { - var properties = type.resolvedProperties || (type.resolvedProperties = ts.createMap()); - var property = properties[name]; - if (!property) { - property = createUnionOrIntersectionProperty(type, name); - if (property) { - properties[name] = property; + // Appends the type parameters given by a list of declarations to a set of type parameters and returns the resulting set. + // The function allocates a new array if the input type parameter set is undefined, but otherwise it modifies the set + // in-place and returns the same array. + function appendTypeParameters(typeParameters, declarations) { + for (var _i = 0, declarations_2 = declarations; _i < declarations_2.length; _i++) { + var declaration = declarations_2[_i]; + var tp = getDeclaredTypeOfTypeParameter(getSymbolOfNode(declaration)); + if (!typeParameters) { + typeParameters = [tp]; + } + else if (!ts.contains(typeParameters, tp)) { + typeParameters.push(tp); } } - return property; + return typeParameters; } - /** - * Return the symbol for the property with the given name in the given type. Creates synthetic union properties when - * necessary, maps primitive types and type parameters are to their apparent types, and augments with properties from - * Object and Function as appropriate. - * - * @param type a type to look up property from - * @param name a name of property to look up in a given type - */ - function getPropertyOfType(type, name) { - type = getApparentType(type); - if (type.flags & 2588672 /* ObjectType */) { - var resolved = resolveStructuredTypeMembers(type); - var symbol = resolved.members[name]; - if (symbol && symbolIsValue(symbol)) { - return symbol; + // Appends the outer type parameters of a node to a set of type parameters and returns the resulting set. The function + // allocates a new array if the input type parameter set is undefined, but otherwise it modifies the set in-place and + // returns the same array. + function appendOuterTypeParameters(typeParameters, node) { + while (true) { + node = node.parent; + if (!node) { + return typeParameters; } - if (resolved === anyFunctionType || resolved.callSignatures.length || resolved.constructSignatures.length) { - var symbol_1 = getPropertyOfObjectType(globalFunctionType, name); - if (symbol_1) { - return symbol_1; + if (node.kind === 221 /* ClassDeclaration */ || node.kind === 192 /* ClassExpression */ || + node.kind === 220 /* FunctionDeclaration */ || node.kind === 179 /* FunctionExpression */ || + node.kind === 147 /* MethodDeclaration */ || node.kind === 180 /* ArrowFunction */) { + var declarations = node.typeParameters; + if (declarations) { + return appendTypeParameters(appendOuterTypeParameters(typeParameters, node), declarations); } } - return getPropertyOfObjectType(globalObjectType, name); - } - if (type.flags & 1572864 /* UnionOrIntersection */) { - return getPropertyOfUnionOrIntersectionType(type, name); - } - return undefined; - } - function getSignaturesOfStructuredType(type, kind) { - if (type.flags & 4161536 /* StructuredType */) { - var resolved = resolveStructuredTypeMembers(type); - return kind === 0 /* Call */ ? resolved.callSignatures : resolved.constructSignatures; } - return emptyArray; } - /** - * Return the signatures of the given kind in the given type. Creates synthetic union signatures when necessary and - * maps primitive types and type parameters are to their apparent types. - */ - function getSignaturesOfType(type, kind) { - return getSignaturesOfStructuredType(getApparentType(type), kind); + // The outer type parameters are those defined by enclosing generic classes, methods, or functions. + function getOuterTypeParametersOfClassOrInterface(symbol) { + var declaration = symbol.flags & 32 /* Class */ ? symbol.valueDeclaration : ts.getDeclarationOfKind(symbol, 222 /* InterfaceDeclaration */); + return appendOuterTypeParameters(undefined, declaration); } - function getIndexInfoOfStructuredType(type, kind) { - if (type.flags & 4161536 /* StructuredType */) { - var resolved = resolveStructuredTypeMembers(type); - return kind === 0 /* String */ ? resolved.stringIndexInfo : resolved.numberIndexInfo; + // The local type parameters are the combined set of type parameters from all declarations of the class, + // interface, or type alias. + function getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol) { + var result; + for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { + var node = _a[_i]; + if (node.kind === 222 /* InterfaceDeclaration */ || node.kind === 221 /* ClassDeclaration */ || + node.kind === 192 /* ClassExpression */ || node.kind === 223 /* TypeAliasDeclaration */) { + var declaration = node; + if (declaration.typeParameters) { + result = appendTypeParameters(result, declaration.typeParameters); + } + } } + return result; } - function getIndexTypeOfStructuredType(type, kind) { - var info = getIndexInfoOfStructuredType(type, kind); - return info && info.type; + // The full set of type parameters for a generic class or interface type consists of its outer type parameters plus + // its locally declared type parameters. + function getTypeParametersOfClassOrInterface(symbol) { + return ts.concatenate(getOuterTypeParametersOfClassOrInterface(symbol), getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol)); } - // Return the indexing info of the given kind in the given type. Creates synthetic union index types when necessary and - // maps primitive types and type parameters are to their apparent types. - function getIndexInfoOfType(type, kind) { - return getIndexInfoOfStructuredType(getApparentType(type), kind); + function isConstructorType(type) { + return type.flags & 2588672 /* ObjectType */ && getSignaturesOfType(type, 1 /* Construct */).length > 0; } - // Return the index type of the given kind in the given type. Creates synthetic union index types when necessary and - // maps primitive types and type parameters are to their apparent types. - function getIndexTypeOfType(type, kind) { - return getIndexTypeOfStructuredType(getApparentType(type), kind); + function getBaseTypeNodeOfClass(type) { + return ts.getClassExtendsHeritageClauseElement(type.symbol.valueDeclaration); } - function getImplicitIndexTypeOfType(type, kind) { - if (isObjectLiteralType(type)) { - var propTypes = []; - for (var _i = 0, _a = getPropertiesOfType(type); _i < _a.length; _i++) { - var prop = _a[_i]; - if (kind === 0 /* String */ || isNumericLiteralName(prop.name)) { - propTypes.push(getTypeOfSymbol(prop)); - } - } - if (propTypes.length) { - return getUnionType(propTypes, /*subtypeReduction*/ true); - } - } - return undefined; + function getConstructorsForTypeArguments(type, typeArgumentNodes) { + var typeArgCount = typeArgumentNodes ? typeArgumentNodes.length : 0; + return ts.filter(getSignaturesOfType(type, 1 /* Construct */), function (sig) { return (sig.typeParameters ? sig.typeParameters.length : 0) === typeArgCount; }); } - function getTypeParametersFromJSDocTemplate(declaration) { - if (declaration.flags & 1048576 /* JavaScriptFile */) { - var templateTag = ts.getJSDocTemplateTag(declaration); - if (templateTag) { - return getTypeParametersFromDeclaration(templateTag.typeParameters); - } + function getInstantiatedConstructorsForTypeArguments(type, typeArgumentNodes) { + var signatures = getConstructorsForTypeArguments(type, typeArgumentNodes); + if (typeArgumentNodes) { + var typeArguments_1 = ts.map(typeArgumentNodes, getTypeFromTypeNodeNoAlias); + signatures = ts.map(signatures, function (sig) { return getSignatureInstantiation(sig, typeArguments_1); }); } - return undefined; + return signatures; } - // Return list of type parameters with duplicates removed (duplicate identifier errors are generated in the actual - // type checking functions). - function getTypeParametersFromDeclaration(typeParameterDeclarations) { - var result = []; - ts.forEach(typeParameterDeclarations, function (node) { - var tp = getDeclaredTypeOfTypeParameter(node.symbol); - if (!ts.contains(result, tp)) { - result.push(tp); + // The base constructor of a class can resolve to + // undefinedType if the class has no extends clause, + // unknownType if an error occurred during resolution of the extends expression, + // nullType if the extends expression is the null value, or + // an object type with at least one construct signature. + function getBaseConstructorTypeOfClass(type) { + if (!type.resolvedBaseConstructorType) { + var baseTypeNode = getBaseTypeNodeOfClass(type); + if (!baseTypeNode) { + return type.resolvedBaseConstructorType = undefinedType; } - }); - return result; - } - function symbolsToArray(symbols) { - var result = []; - for (var id in symbols) { - if (!isReservedMemberName(id)) { - result.push(symbols[id]); + if (!pushTypeResolution(type, 1 /* ResolvedBaseConstructorType */)) { + return unknownType; + } + var baseConstructorType = checkExpression(baseTypeNode.expression); + if (baseConstructorType.flags & 2588672 /* ObjectType */) { + // Resolving the members of a class requires us to resolve the base class of that class. + // We force resolution here such that we catch circularities now. + resolveStructuredTypeMembers(baseConstructorType); + } + if (!popTypeResolution()) { + error(type.symbol.valueDeclaration, ts.Diagnostics._0_is_referenced_directly_or_indirectly_in_its_own_base_expression, symbolToString(type.symbol)); + return type.resolvedBaseConstructorType = unknownType; + } + if (baseConstructorType !== unknownType && baseConstructorType !== nullWideningType && !isConstructorType(baseConstructorType)) { + error(baseTypeNode.expression, ts.Diagnostics.Type_0_is_not_a_constructor_function_type, typeToString(baseConstructorType)); + return type.resolvedBaseConstructorType = unknownType; } + type.resolvedBaseConstructorType = baseConstructorType; } - return result; + return type.resolvedBaseConstructorType; } - function isJSDocOptionalParameter(node) { - if (node.flags & 1048576 /* JavaScriptFile */) { - if (node.type && node.type.kind === 268 /* JSDocOptionalType */) { - return true; + function getBaseTypes(type) { + if (!type.resolvedBaseTypes) { + if (type.flags & 262144 /* Tuple */) { + type.resolvedBaseTypes = [createArrayType(getUnionType(type.typeParameters))]; } - var paramTag = ts.getCorrespondingJSDocParameterTag(node); - if (paramTag) { - if (paramTag.isBracketed) { - return true; + else if (type.symbol.flags & (32 /* Class */ | 64 /* Interface */)) { + if (type.symbol.flags & 32 /* Class */) { + resolveBaseTypesOfClass(type); } - if (paramTag.typeExpression) { - return paramTag.typeExpression.type.kind === 268 /* JSDocOptionalType */; + if (type.symbol.flags & 64 /* Interface */) { + resolveBaseTypesOfInterface(type); } } + else { + ts.Debug.fail("type must be class or interface"); + } } + return type.resolvedBaseTypes; } - function isOptionalParameter(node) { - if (ts.hasQuestionToken(node) || isJSDocOptionalParameter(node)) { - return true; - } - if (node.initializer) { - var signatureDeclaration = node.parent; - var signature = getSignatureFromDeclaration(signatureDeclaration); - var parameterIndex = ts.indexOf(signatureDeclaration.parameters, node); - ts.Debug.assert(parameterIndex >= 0); - return parameterIndex >= signature.minArgumentCount; + function resolveBaseTypesOfClass(type) { + type.resolvedBaseTypes = type.resolvedBaseTypes || emptyArray; + var baseConstructorType = getBaseConstructorTypeOfClass(type); + if (!(baseConstructorType.flags & 2588672 /* ObjectType */)) { + return; } - return false; - } - function createTypePredicateFromTypePredicateNode(node) { - if (node.parameterName.kind === 69 /* Identifier */) { - var parameterName = node.parameterName; - return { - kind: 1 /* Identifier */, - parameterName: parameterName ? parameterName.text : undefined, - parameterIndex: parameterName ? getTypePredicateParameterIndex(node.parent.parameters, parameterName) : undefined, - type: getTypeFromTypeNode(node.type) - }; + var baseTypeNode = getBaseTypeNodeOfClass(type); + var baseType; + var originalBaseType = baseConstructorType && baseConstructorType.symbol ? getDeclaredTypeOfSymbol(baseConstructorType.symbol) : undefined; + if (baseConstructorType.symbol && baseConstructorType.symbol.flags & 32 /* Class */ && + areAllOuterTypeParametersApplied(originalBaseType)) { + // When base constructor type is a class with no captured type arguments we know that the constructors all have the same type parameters as the + // class and all return the instance type of the class. There is no need for further checks and we can apply the + // type arguments in the same manner as a type reference to get the same error reporting experience. + baseType = getTypeFromClassOrInterfaceReference(baseTypeNode, baseConstructorType.symbol); } else { - return { - kind: 0 /* This */, - type: getTypeFromTypeNode(node.type) - }; - } - } - function getSignatureFromDeclaration(declaration) { - var links = getNodeLinks(declaration); - if (!links.resolvedSignature) { - var parameters = []; - var hasLiteralTypes = false; - var minArgumentCount = -1; - var thisParameter = undefined; - var hasThisParameter = void 0; - var isJSConstructSignature = ts.isJSDocConstructSignature(declaration); - // If this is a JSDoc construct signature, then skip the first parameter in the - // parameter list. The first parameter represents the return type of the construct - // signature. - for (var i = isJSConstructSignature ? 1 : 0, n = declaration.parameters.length; i < n; i++) { - var param = declaration.parameters[i]; - var paramSymbol = param.symbol; - // Include parameter symbol instead of property symbol in the signature - if (paramSymbol && !!(paramSymbol.flags & 4 /* Property */) && !ts.isBindingPattern(param.name)) { - var resolvedSymbol = resolveName(param, paramSymbol.name, 107455 /* Value */, undefined, undefined); - paramSymbol = resolvedSymbol; - } - if (i === 0 && paramSymbol.name === "this") { - hasThisParameter = true; - thisParameter = param.symbol; - } - else { - parameters.push(paramSymbol); - } - if (param.type && param.type.kind === 166 /* LiteralType */) { - hasLiteralTypes = true; - } - if (param.initializer || param.questionToken || param.dotDotDotToken || isJSDocOptionalParameter(param)) { - if (minArgumentCount < 0) { - minArgumentCount = i - (hasThisParameter ? 1 : 0); - } - } - else { - // If we see any required parameters, it means the prior ones were not in fact optional. - minArgumentCount = -1; - } - } - // If only one accessor includes a this-type annotation, the other behaves as if it had the same type annotation - if ((declaration.kind === 149 /* GetAccessor */ || declaration.kind === 150 /* SetAccessor */) && - !ts.hasDynamicName(declaration) && - (!hasThisParameter || !thisParameter)) { - var otherKind = declaration.kind === 149 /* GetAccessor */ ? 150 /* SetAccessor */ : 149 /* GetAccessor */; - var other = ts.getDeclarationOfKind(declaration.symbol, otherKind); - if (other) { - thisParameter = getAnnotatedAccessorThisParameter(other); - } - } - if (minArgumentCount < 0) { - minArgumentCount = declaration.parameters.length - (hasThisParameter ? 1 : 0); - } - if (isJSConstructSignature) { - minArgumentCount--; - } - if (!thisParameter && ts.isObjectLiteralMethod(declaration)) { - thisParameter = getContextualThisParameter(declaration); + // The class derives from a "class-like" constructor function, check that we have at least one construct signature + // with a matching number of type parameters and use the return type of the first instantiated signature. Elsewhere + // we check that all instantiated signatures return the same type. + var constructors = getInstantiatedConstructorsForTypeArguments(baseConstructorType, baseTypeNode.typeArguments); + if (!constructors.length) { + error(baseTypeNode.expression, ts.Diagnostics.No_base_constructor_has_the_specified_number_of_type_arguments); + return; } - var classType = declaration.kind === 148 /* Constructor */ ? - getDeclaredTypeOfClassOrInterface(getMergedSymbol(declaration.parent.symbol)) - : undefined; - var typeParameters = classType ? classType.localTypeParameters : - declaration.typeParameters ? getTypeParametersFromDeclaration(declaration.typeParameters) : - getTypeParametersFromJSDocTemplate(declaration); - var returnType = getSignatureReturnTypeFromDeclaration(declaration, minArgumentCount, isJSConstructSignature, classType); - var typePredicate = declaration.type && declaration.type.kind === 154 /* TypePredicate */ ? - createTypePredicateFromTypePredicateNode(declaration.type) : - undefined; - links.resolvedSignature = createSignature(declaration, typeParameters, thisParameter, parameters, returnType, typePredicate, minArgumentCount, ts.hasRestParameter(declaration), hasLiteralTypes); + baseType = getReturnTypeOfSignature(constructors[0]); } - return links.resolvedSignature; - } - function getSignatureReturnTypeFromDeclaration(declaration, minArgumentCount, isJSConstructSignature, classType) { - if (isJSConstructSignature) { - return getTypeFromTypeNode(declaration.parameters[0].type); + if (baseType === unknownType) { + return; } - else if (classType) { - return classType; + if (!(getTargetType(baseType).flags & (32768 /* Class */ | 65536 /* Interface */))) { + error(baseTypeNode.expression, ts.Diagnostics.Base_constructor_return_type_0_is_not_a_class_or_interface_type, typeToString(baseType)); + return; } - else if (declaration.type) { - return getTypeFromTypeNode(declaration.type); + if (type === baseType || hasBaseType(baseType, type)) { + error(type.symbol.valueDeclaration, ts.Diagnostics.Type_0_recursively_references_itself_as_a_base_type, typeToString(type, /*enclosingDeclaration*/ undefined, 1 /* WriteArrayAsGenericType */)); + return; } - if (declaration.flags & 1048576 /* JavaScriptFile */) { - var type = getReturnTypeFromJSDocComment(declaration); - if (type && type !== unknownType) { - return type; - } + if (type.resolvedBaseTypes === emptyArray) { + type.resolvedBaseTypes = [baseType]; } - // TypeScript 1.0 spec (April 2014): - // If only one accessor includes a type annotation, the other behaves as if it had the same type annotation. - if (declaration.kind === 149 /* GetAccessor */ && !ts.hasDynamicName(declaration)) { - var setter = ts.getDeclarationOfKind(declaration.symbol, 150 /* SetAccessor */); - return getAnnotatedAccessorType(setter); + else { + type.resolvedBaseTypes.push(baseType); } - if (ts.nodeIsMissing(declaration.body)) { - return anyType; + } + function areAllOuterTypeParametersApplied(type) { + // An unapplied type parameter has its symbol still the same as the matching argument symbol. + // Since parameters are applied outer-to-inner, only the last outer parameter needs to be checked. + var outerTypeParameters = type.outerTypeParameters; + if (outerTypeParameters) { + var last = outerTypeParameters.length - 1; + var typeArguments = type.typeArguments; + return outerTypeParameters[last].symbol !== typeArguments[last].symbol; } + return true; } - function getSignaturesOfSymbol(symbol) { - if (!symbol) - return emptyArray; - var result = []; - for (var i = 0, len = symbol.declarations.length; i < len; i++) { - var node = symbol.declarations[i]; - switch (node.kind) { - case 156 /* FunctionType */: - case 157 /* ConstructorType */: - case 220 /* FunctionDeclaration */: - case 147 /* MethodDeclaration */: - case 146 /* MethodSignature */: - case 148 /* Constructor */: - case 151 /* CallSignature */: - case 152 /* ConstructSignature */: - case 153 /* IndexSignature */: - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - case 179 /* FunctionExpression */: - case 180 /* ArrowFunction */: - case 269 /* JSDocFunctionType */: - // Don't include signature if node is the implementation of an overloaded function. A node is considered - // an implementation node if it has a body and the previous node is of the same kind and immediately - // precedes the implementation node (i.e. has the same parent and ends where the implementation starts). - if (i > 0 && node.body) { - var previous = symbol.declarations[i - 1]; - if (node.parent === previous.parent && node.kind === previous.kind && node.pos === previous.end) { - break; + function resolveBaseTypesOfInterface(type) { + type.resolvedBaseTypes = type.resolvedBaseTypes || emptyArray; + for (var _i = 0, _a = type.symbol.declarations; _i < _a.length; _i++) { + var declaration = _a[_i]; + if (declaration.kind === 222 /* InterfaceDeclaration */ && ts.getInterfaceBaseTypeNodes(declaration)) { + for (var _b = 0, _c = ts.getInterfaceBaseTypeNodes(declaration); _b < _c.length; _b++) { + var node = _c[_b]; + var baseType = getTypeFromTypeNode(node); + if (baseType !== unknownType) { + if (getTargetType(baseType).flags & (32768 /* Class */ | 65536 /* Interface */)) { + if (type !== baseType && !hasBaseType(baseType, type)) { + if (type.resolvedBaseTypes === emptyArray) { + type.resolvedBaseTypes = [baseType]; + } + else { + type.resolvedBaseTypes.push(baseType); + } + } + else { + error(declaration, ts.Diagnostics.Type_0_recursively_references_itself_as_a_base_type, typeToString(type, /*enclosingDeclaration*/ undefined, 1 /* WriteArrayAsGenericType */)); + } + } + else { + error(node, ts.Diagnostics.An_interface_may_only_extend_a_class_or_another_interface); } } - result.push(getSignatureFromDeclaration(node)); + } } } - return result; } - function resolveExternalModuleTypeByLiteral(name) { - var moduleSym = resolveExternalModuleName(name, name); - if (moduleSym) { - var resolvedModuleSymbol = resolveExternalModuleSymbol(moduleSym); - if (resolvedModuleSymbol) { - return getTypeOfSymbol(resolvedModuleSymbol); + // Returns true if the interface given by the symbol is free of "this" references. Specifically, the result is + // true if the interface itself contains no references to "this" in its body, if all base types are interfaces, + // and if none of the base interfaces have a "this" type. + function isIndependentInterface(symbol) { + for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { + var declaration = _a[_i]; + if (declaration.kind === 222 /* InterfaceDeclaration */) { + if (declaration.flags & 64 /* ContainsThis */) { + return false; + } + var baseTypeNodes = ts.getInterfaceBaseTypeNodes(declaration); + if (baseTypeNodes) { + for (var _b = 0, baseTypeNodes_1 = baseTypeNodes; _b < baseTypeNodes_1.length; _b++) { + var node = baseTypeNodes_1[_b]; + if (ts.isEntityNameExpression(node.expression)) { + var baseSymbol = resolveEntityName(node.expression, 793064 /* Type */, /*ignoreErrors*/ true); + if (!baseSymbol || !(baseSymbol.flags & 64 /* Interface */) || getDeclaredTypeOfClassOrInterface(baseSymbol).thisType) { + return false; + } + } + } + } } } - return anyType; + return true; } - function getThisTypeOfSignature(signature) { - if (signature.thisParameter) { - return getTypeOfSymbol(signature.thisParameter); + function getDeclaredTypeOfClassOrInterface(symbol) { + var links = getSymbolLinks(symbol); + if (!links.declaredType) { + var kind = symbol.flags & 32 /* Class */ ? 32768 /* Class */ : 65536 /* Interface */; + var type = links.declaredType = createObjectType(kind, symbol); + var outerTypeParameters = getOuterTypeParametersOfClassOrInterface(symbol); + var localTypeParameters = getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol); + // A class or interface is generic if it has type parameters or a "this" type. We always give classes a "this" type + // because it is not feasible to analyze all members to determine if the "this" type escapes the class (in particular, + // property types inferred from initializers and method return types inferred from return statements are very hard + // to exhaustively analyze). We give interfaces a "this" type if we can't definitely determine that they are free of + // "this" references. + if (outerTypeParameters || localTypeParameters || kind === 32768 /* Class */ || !isIndependentInterface(symbol)) { + type.flags |= 131072 /* Reference */; + type.typeParameters = ts.concatenate(outerTypeParameters, localTypeParameters); + type.outerTypeParameters = outerTypeParameters; + type.localTypeParameters = localTypeParameters; + type.instantiations = ts.createMap(); + type.instantiations[getTypeListId(type.typeParameters)] = type; + type.target = type; + type.typeArguments = type.typeParameters; + type.thisType = createType(16384 /* TypeParameter */); + type.thisType.isThisType = true; + type.thisType.symbol = symbol; + type.thisType.constraint = type; + } } + return links.declaredType; } - function getReturnTypeOfSignature(signature) { - if (!signature.resolvedReturnType) { - if (!pushTypeResolution(signature, 3 /* ResolvedReturnType */)) { + function getDeclaredTypeOfTypeAlias(symbol) { + var links = getSymbolLinks(symbol); + if (!links.declaredType) { + // Note that we use the links object as the target here because the symbol object is used as the unique + // identity for resolution of the 'type' property in SymbolLinks. + if (!pushTypeResolution(symbol, 2 /* DeclaredType */)) { return unknownType; } + var typeParameters = getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol); + var declaration = ts.getDeclarationOfKind(symbol, 279 /* JSDocTypedefTag */); var type = void 0; - if (signature.target) { - type = instantiateType(getReturnTypeOfSignature(signature.target), signature.mapper); - } - else if (signature.unionSignatures) { - type = getUnionType(ts.map(signature.unionSignatures, getReturnTypeOfSignature), /*subtypeReduction*/ true); + if (declaration) { + if (declaration.jsDocTypeLiteral) { + type = getTypeFromTypeNode(declaration.jsDocTypeLiteral); + } + else { + type = getTypeFromTypeNode(declaration.typeExpression.type); + } } else { - type = getReturnTypeFromBody(signature.declaration); + declaration = ts.getDeclarationOfKind(symbol, 223 /* TypeAliasDeclaration */); + type = getTypeFromTypeNode(declaration.type, symbol, typeParameters); } - if (!popTypeResolution()) { - type = anyType; - if (compilerOptions.noImplicitAny) { - var declaration = signature.declaration; - if (declaration.name) { - error(declaration.name, ts.Diagnostics._0_implicitly_has_return_type_any_because_it_does_not_have_a_return_type_annotation_and_is_referenced_directly_or_indirectly_in_one_of_its_return_expressions, ts.declarationNameToString(declaration.name)); - } - else { - error(declaration, ts.Diagnostics.Function_implicitly_has_return_type_any_because_it_does_not_have_a_return_type_annotation_and_is_referenced_directly_or_indirectly_in_one_of_its_return_expressions); - } + if (popTypeResolution()) { + links.typeParameters = typeParameters; + if (typeParameters) { + // Initialize the instantiation cache for generic type aliases. The declared type corresponds to + // an instantiation of the type alias with the type parameters supplied as type arguments. + links.instantiations = ts.createMap(); + links.instantiations[getTypeListId(links.typeParameters)] = type; } } - signature.resolvedReturnType = type; - } - return signature.resolvedReturnType; - } - function getRestTypeOfSignature(signature) { - if (signature.hasRestParameter) { - var type = getTypeOfSymbol(ts.lastOrUndefined(signature.parameters)); - if (type.flags & 131072 /* Reference */ && type.target === globalArrayType) { - return type.typeArguments[0]; + else { + type = unknownType; + error(declaration.name, ts.Diagnostics.Type_alias_0_circularly_references_itself, symbolToString(symbol)); } + links.declaredType = type; } - return anyType; - } - function getSignatureInstantiation(signature, typeArguments) { - return instantiateSignature(signature, createTypeMapper(signature.typeParameters, typeArguments), /*eraseTypeParameters*/ true); - } - function getErasedSignature(signature) { - if (!signature.typeParameters) - return signature; - if (!signature.erasedSignatureCache) { - signature.erasedSignatureCache = instantiateSignature(signature, createTypeEraser(signature.typeParameters), /*eraseTypeParameters*/ true); - } - return signature.erasedSignatureCache; + return links.declaredType; } - function getOrCreateTypeFromSignature(signature) { - // There are two ways to declare a construct signature, one is by declaring a class constructor - // using the constructor keyword, and the other is declaring a bare construct signature in an - // object type literal or interface (using the new keyword). Each way of declaring a constructor - // will result in a different declaration kind. - if (!signature.isolatedSignatureType) { - var isConstructor = signature.declaration.kind === 148 /* Constructor */ || signature.declaration.kind === 152 /* ConstructSignature */; - var type = createObjectType(2097152 /* Anonymous */); - type.members = emptySymbols; - type.properties = emptyArray; - type.callSignatures = !isConstructor ? [signature] : emptyArray; - type.constructSignatures = isConstructor ? [signature] : emptyArray; - signature.isolatedSignatureType = type; + function isLiteralEnumMember(symbol, member) { + var expr = member.initializer; + if (!expr) { + return !ts.isInAmbientContext(member); } - return signature.isolatedSignatureType; - } - function getIndexSymbol(symbol) { - return symbol.members["__index"]; + return expr.kind === 8 /* NumericLiteral */ || + expr.kind === 185 /* PrefixUnaryExpression */ && expr.operator === 36 /* MinusToken */ && + expr.operand.kind === 8 /* NumericLiteral */ || + expr.kind === 69 /* Identifier */ && !!symbol.exports[expr.text]; } - function getIndexDeclarationOfSymbol(symbol, kind) { - var syntaxKind = kind === 1 /* Number */ ? 130 /* NumberKeyword */ : 132 /* StringKeyword */; - var indexSymbol = getIndexSymbol(symbol); - if (indexSymbol) { - for (var _i = 0, _a = indexSymbol.declarations; _i < _a.length; _i++) { - var decl = _a[_i]; - var node = decl; - if (node.parameters.length === 1) { - var parameter = node.parameters[0]; - if (parameter && parameter.type && parameter.type.kind === syntaxKind) { - return node; + function enumHasLiteralMembers(symbol) { + for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { + var declaration = _a[_i]; + if (declaration.kind === 224 /* EnumDeclaration */) { + for (var _b = 0, _c = declaration.members; _b < _c.length; _b++) { + var member = _c[_b]; + if (!isLiteralEnumMember(symbol, member)) { + return false; } } } } - return undefined; - } - function createIndexInfo(type, isReadonly, declaration) { - return { type: type, isReadonly: isReadonly, declaration: declaration }; - } - function getIndexInfoOfSymbol(symbol, kind) { - var declaration = getIndexDeclarationOfSymbol(symbol, kind); - if (declaration) { - return createIndexInfo(declaration.type ? getTypeFromTypeNode(declaration.type) : anyType, (ts.getModifierFlags(declaration) & 64 /* Readonly */) !== 0, declaration); - } - return undefined; + return true; } - function getConstraintDeclaration(type) { - return ts.getDeclarationOfKind(type.symbol, 141 /* TypeParameter */).constraint; + function createEnumLiteralType(symbol, baseType, text) { + var type = createType(256 /* EnumLiteral */); + type.symbol = symbol; + type.baseType = baseType; + type.text = text; + return type; } - function hasConstraintReferenceTo(type, target) { - var checked; - while (type && !(type.flags & 268435456 /* ThisType */) && type.flags & 16384 /* TypeParameter */ && !ts.contains(checked, type)) { - if (type === target) { - return true; - } - (checked || (checked = [])).push(type); - var constraintDeclaration = getConstraintDeclaration(type); - type = constraintDeclaration && getTypeFromTypeNode(constraintDeclaration); - } - return false; - } - function getConstraintOfTypeParameter(typeParameter) { - if (!typeParameter.constraint) { - if (typeParameter.target) { - var targetConstraint = getConstraintOfTypeParameter(typeParameter.target); - typeParameter.constraint = targetConstraint ? instantiateType(targetConstraint, typeParameter.mapper) : noConstraintType; - } - else { - var constraintDeclaration = getConstraintDeclaration(typeParameter); - var constraint = getTypeFromTypeNode(constraintDeclaration); - if (hasConstraintReferenceTo(constraint, typeParameter)) { - error(constraintDeclaration, ts.Diagnostics.Type_parameter_0_has_a_circular_constraint, typeToString(typeParameter)); - constraint = unknownType; + function getDeclaredTypeOfEnum(symbol) { + var links = getSymbolLinks(symbol); + if (!links.declaredType) { + var enumType = links.declaredType = createType(16 /* Enum */); + enumType.symbol = symbol; + if (enumHasLiteralMembers(symbol)) { + var memberTypeList = []; + var memberTypes = ts.createMap(); + for (var _i = 0, _a = enumType.symbol.declarations; _i < _a.length; _i++) { + var declaration = _a[_i]; + if (declaration.kind === 224 /* EnumDeclaration */) { + computeEnumMemberValues(declaration); + for (var _b = 0, _c = declaration.members; _b < _c.length; _b++) { + var member = _c[_b]; + var memberSymbol = getSymbolOfNode(member); + var value = getEnumMemberValue(member); + if (!memberTypes[value]) { + var memberType = memberTypes[value] = createEnumLiteralType(memberSymbol, enumType, "" + value); + memberTypeList.push(memberType); + } + } + } + } + enumType.memberTypes = memberTypes; + if (memberTypeList.length > 1) { + enumType.flags |= 524288 /* Union */; + enumType.types = memberTypeList; + unionTypes[getTypeListId(memberTypeList)] = enumType; } - typeParameter.constraint = constraint; } } - return typeParameter.constraint === noConstraintType ? undefined : typeParameter.constraint; - } - function getParentSymbolOfTypeParameter(typeParameter) { - return getSymbolOfNode(ts.getDeclarationOfKind(typeParameter.symbol, 141 /* TypeParameter */).parent); + return links.declaredType; } - function getTypeListId(types) { - var result = ""; - if (types) { - var length_3 = types.length; - var i = 0; - while (i < length_3) { - var startId = types[i].id; - var count = 1; - while (i + count < length_3 && types[i + count].id === startId + count) { - count++; - } - if (result.length) { - result += ","; - } - result += startId; - if (count > 1) { - result += ":" + count; - } - i += count; - } + function getDeclaredTypeOfEnumMember(symbol) { + var links = getSymbolLinks(symbol); + if (!links.declaredType) { + var enumType = getDeclaredTypeOfEnum(getParentOfSymbol(symbol)); + links.declaredType = enumType.flags & 524288 /* Union */ ? + enumType.memberTypes[getEnumMemberValue(symbol.valueDeclaration)] : + enumType; } - return result; + return links.declaredType; } - // This function is used to propagate certain flags when creating new object type references and union types. - // It is only necessary to do so if a constituent type might be the undefined type, the null type, the type - // of an object literal or the anyFunctionType. This is because there are operations in the type checker - // that care about the presence of such types at arbitrary depth in a containing type. - function getPropagatingFlagsOfTypes(types, excludeKinds) { - var result = 0; - for (var _i = 0, types_3 = types; _i < types_3.length; _i++) { - var type = types_3[_i]; - if (!(type.flags & excludeKinds)) { - result |= type.flags; + function getDeclaredTypeOfTypeParameter(symbol) { + var links = getSymbolLinks(symbol); + if (!links.declaredType) { + var type = createType(16384 /* TypeParameter */); + type.symbol = symbol; + if (!ts.getDeclarationOfKind(symbol, 141 /* TypeParameter */).constraint) { + type.constraint = noConstraintType; } + links.declaredType = type; } - return result & 234881024 /* PropagatingFlags */; + return links.declaredType; } - function createTypeReference(target, typeArguments) { - var id = getTypeListId(typeArguments); - var type = target.instantiations[id]; - if (!type) { - var propagatedFlags = typeArguments ? getPropagatingFlagsOfTypes(typeArguments, /*excludeKinds*/ 0) : 0; - var flags = 131072 /* Reference */ | propagatedFlags; - type = target.instantiations[id] = createObjectType(flags, target.symbol); - type.target = target; - type.typeArguments = typeArguments; + function getDeclaredTypeOfAlias(symbol) { + var links = getSymbolLinks(symbol); + if (!links.declaredType) { + links.declaredType = getDeclaredTypeOfSymbol(resolveAlias(symbol)); } - return type; - } - function cloneTypeReference(source) { - var type = createObjectType(source.flags, source.symbol); - type.target = source.target; - type.typeArguments = source.typeArguments; - return type; - } - function getTypeReferenceArity(type) { - return type.target.typeParameters ? type.target.typeParameters.length : 0; + return links.declaredType; } - // Get type from reference to class or interface - function getTypeFromClassOrInterfaceReference(node, symbol) { - var type = getDeclaredTypeOfSymbol(getMergedSymbol(symbol)); - var typeParameters = type.localTypeParameters; - if (typeParameters) { - if (!node.typeArguments || node.typeArguments.length !== typeParameters.length) { - error(node, ts.Diagnostics.Generic_type_0_requires_1_type_argument_s, typeToString(type, /*enclosingDeclaration*/ undefined, 1 /* WriteArrayAsGenericType */), typeParameters.length); - return unknownType; - } - // In a type reference, the outer type parameters of the referenced class or interface are automatically - // supplied as type arguments and the type reference only specifies arguments for the local type parameters - // of the class or interface. - return createTypeReference(type, ts.concatenate(type.outerTypeParameters, ts.map(node.typeArguments, getTypeFromTypeNodeNoAlias))); + function getDeclaredTypeOfSymbol(symbol) { + ts.Debug.assert((symbol.flags & 16777216 /* Instantiated */) === 0); + if (symbol.flags & (32 /* Class */ | 64 /* Interface */)) { + return getDeclaredTypeOfClassOrInterface(symbol); } - if (node.typeArguments) { - error(node, ts.Diagnostics.Type_0_is_not_generic, typeToString(type)); - return unknownType; + if (symbol.flags & 524288 /* TypeAlias */) { + return getDeclaredTypeOfTypeAlias(symbol); } - return type; - } - // Get type from reference to type alias. When a type alias is generic, the declared type of the type alias may include - // references to the type parameters of the alias. We replace those with the actual type arguments by instantiating the - // declared type. Instantiations are cached using the type identities of the type arguments as the key. - function getTypeFromTypeAliasReference(node, symbol) { - var type = getDeclaredTypeOfSymbol(symbol); - var links = getSymbolLinks(symbol); - var typeParameters = links.typeParameters; - if (typeParameters) { - if (!node.typeArguments || node.typeArguments.length !== typeParameters.length) { - error(node, ts.Diagnostics.Generic_type_0_requires_1_type_argument_s, symbolToString(symbol), typeParameters.length); - return unknownType; - } - var typeArguments = ts.map(node.typeArguments, getTypeFromTypeNodeNoAlias); - var id = getTypeListId(typeArguments); - return links.instantiations[id] || (links.instantiations[id] = instantiateType(type, createTypeMapper(typeParameters, typeArguments))); + if (symbol.flags & 262144 /* TypeParameter */) { + return getDeclaredTypeOfTypeParameter(symbol); } - if (node.typeArguments) { - error(node, ts.Diagnostics.Type_0_is_not_generic, symbolToString(symbol)); - return unknownType; + if (symbol.flags & 384 /* Enum */) { + return getDeclaredTypeOfEnum(symbol); } - return type; + if (symbol.flags & 8 /* EnumMember */) { + return getDeclaredTypeOfEnumMember(symbol); + } + if (symbol.flags & 8388608 /* Alias */) { + return getDeclaredTypeOfAlias(symbol); + } + return unknownType; } - // Get type from reference to named type that cannot be generic (enum or type parameter) - function getTypeFromNonGenericTypeReference(node, symbol) { + // A type reference is considered independent if each type argument is considered independent. + function isIndependentTypeReference(node) { if (node.typeArguments) { - error(node, ts.Diagnostics.Type_0_is_not_generic, symbolToString(symbol)); - return unknownType; + for (var _i = 0, _a = node.typeArguments; _i < _a.length; _i++) { + var typeNode = _a[_i]; + if (!isIndependentType(typeNode)) { + return false; + } + } } - return getDeclaredTypeOfSymbol(symbol); + return true; } - function getTypeReferenceName(node) { + // A type is considered independent if it the any, string, number, boolean, symbol, or void keyword, a string + // literal type, an array with an element type that is considered independent, or a type reference that is + // considered independent. + function isIndependentType(node) { switch (node.kind) { + case 117 /* AnyKeyword */: + case 132 /* StringKeyword */: + case 130 /* NumberKeyword */: + case 120 /* BooleanKeyword */: + case 133 /* SymbolKeyword */: + case 103 /* VoidKeyword */: + case 135 /* UndefinedKeyword */: + case 93 /* NullKeyword */: + case 127 /* NeverKeyword */: + case 166 /* LiteralType */: + return true; + case 160 /* ArrayType */: + return isIndependentType(node.elementType); case 155 /* TypeReference */: - return node.typeName; - case 267 /* JSDocTypeReference */: - return node.name; - case 194 /* ExpressionWithTypeArguments */: - // We only support expressions that are simple qualified names. For other - // expressions this produces undefined. - var expr = node.expression; - if (ts.isEntityNameExpression(expr)) { - return expr; - } + return isIndependentTypeReference(node); } - return undefined; + return false; } - function resolveTypeReferenceName(node, typeReferenceName) { - if (!typeReferenceName) { - return unknownSymbol; - } - return resolveEntityName(typeReferenceName, 793064 /* Type */) || unknownSymbol; + // A variable-like declaration is considered independent (free of this references) if it has a type annotation + // that specifies an independent type, or if it has no type annotation and no initializer (and thus of type any). + function isIndependentVariableLikeDeclaration(node) { + return node.type && isIndependentType(node.type) || !node.type && !node.initializer; } - function getTypeReferenceType(node, symbol) { - if (symbol === unknownSymbol) { - return unknownType; - } - if (symbol.flags & (32 /* Class */ | 64 /* Interface */)) { - return getTypeFromClassOrInterfaceReference(node, symbol); - } - if (symbol.flags & 524288 /* TypeAlias */) { - return getTypeFromTypeAliasReference(node, symbol); - } - if (symbol.flags & 107455 /* Value */ && node.kind === 267 /* JSDocTypeReference */) { - // A JSDocTypeReference may have resolved to a value (as opposed to a type). In - // that case, the type of this reference is just the type of the value we resolved - // to. - return getTypeOfSymbol(symbol); + // A function-like declaration is considered independent (free of this references) if it has a return type + // annotation that is considered independent and if each parameter is considered independent. + function isIndependentFunctionLikeDeclaration(node) { + if (node.kind !== 148 /* Constructor */ && (!node.type || !isIndependentType(node.type))) { + return false; } - return getTypeFromNonGenericTypeReference(node, symbol); - } - function getTypeFromTypeReference(node) { - var links = getNodeLinks(node); - if (!links.resolvedType) { - var symbol = void 0; - var type = void 0; - if (node.kind === 267 /* JSDocTypeReference */) { - var typeReferenceName = getTypeReferenceName(node); - symbol = resolveTypeReferenceName(node, typeReferenceName); - type = getTypeReferenceType(node, symbol); - } - else { - // We only support expressions that are simple qualified names. For other expressions this produces undefined. - var typeNameOrExpression = node.kind === 155 /* TypeReference */ - ? node.typeName - : ts.isEntityNameExpression(node.expression) - ? node.expression - : undefined; - symbol = typeNameOrExpression && resolveEntityName(typeNameOrExpression, 793064 /* Type */) || unknownSymbol; - type = symbol === unknownSymbol ? unknownType : - symbol.flags & (32 /* Class */ | 64 /* Interface */) ? getTypeFromClassOrInterfaceReference(node, symbol) : - symbol.flags & 524288 /* TypeAlias */ ? getTypeFromTypeAliasReference(node, symbol) : - getTypeFromNonGenericTypeReference(node, symbol); + for (var _i = 0, _a = node.parameters; _i < _a.length; _i++) { + var parameter = _a[_i]; + if (!isIndependentVariableLikeDeclaration(parameter)) { + return false; } - // Cache both the resolved symbol and the resolved type. The resolved symbol is needed in when we check the - // type reference in checkTypeReferenceOrExpressionWithTypeArguments. - links.resolvedSymbol = symbol; - links.resolvedType = type; - } - return links.resolvedType; - } - function getTypeFromTypeQueryNode(node) { - var links = getNodeLinks(node); - if (!links.resolvedType) { - // TypeScript 1.0 spec (April 2014): 3.6.3 - // The expression is processed as an identifier expression (section 4.3) - // or property access expression(section 4.10), - // the widened type(section 3.9) of which becomes the result. - links.resolvedType = getWidenedType(checkExpression(node.exprName)); } - return links.resolvedType; + return true; } - function getTypeOfGlobalSymbol(symbol, arity) { - function getTypeDeclaration(symbol) { - var declarations = symbol.declarations; - for (var _i = 0, declarations_3 = declarations; _i < declarations_3.length; _i++) { - var declaration = declarations_3[_i]; + // Returns true if the class or interface member given by the symbol is free of "this" references. The + // function may return false for symbols that are actually free of "this" references because it is not + // feasible to perform a complete analysis in all cases. In particular, property members with types + // inferred from their initializers and function members with inferred return types are conservatively + // assumed not to be free of "this" references. + function isIndependentMember(symbol) { + if (symbol.declarations && symbol.declarations.length === 1) { + var declaration = symbol.declarations[0]; + if (declaration) { switch (declaration.kind) { - case 221 /* ClassDeclaration */: - case 222 /* InterfaceDeclaration */: - case 224 /* EnumDeclaration */: - return declaration; + case 145 /* PropertyDeclaration */: + case 144 /* PropertySignature */: + return isIndependentVariableLikeDeclaration(declaration); + case 147 /* MethodDeclaration */: + case 146 /* MethodSignature */: + case 148 /* Constructor */: + return isIndependentFunctionLikeDeclaration(declaration); } } } - if (!symbol) { - return arity ? emptyGenericType : emptyObjectType; - } - var type = getDeclaredTypeOfSymbol(symbol); - if (!(type.flags & 2588672 /* ObjectType */)) { - error(getTypeDeclaration(symbol), ts.Diagnostics.Global_type_0_must_be_a_class_or_interface_type, symbol.name); - return arity ? emptyGenericType : emptyObjectType; - } - if ((type.typeParameters ? type.typeParameters.length : 0) !== arity) { - error(getTypeDeclaration(symbol), ts.Diagnostics.Global_type_0_must_have_1_type_parameter_s, symbol.name, arity); - return arity ? emptyGenericType : emptyObjectType; - } - return type; - } - function getGlobalValueSymbol(name) { - return getGlobalSymbol(name, 107455 /* Value */, ts.Diagnostics.Cannot_find_global_value_0); - } - function getGlobalTypeSymbol(name) { - return getGlobalSymbol(name, 793064 /* Type */, ts.Diagnostics.Cannot_find_global_type_0); - } - function getGlobalSymbol(name, meaning, diagnostic) { - return resolveName(undefined, name, meaning, diagnostic, name); - } - function getGlobalType(name, arity) { - if (arity === void 0) { arity = 0; } - return getTypeOfGlobalSymbol(getGlobalTypeSymbol(name), arity); - } - /** - * Returns a type that is inside a namespace at the global scope, e.g. - * getExportedTypeFromNamespace('JSX', 'Element') returns the JSX.Element type - */ - function getExportedTypeFromNamespace(namespace, name) { - var namespaceSymbol = getGlobalSymbol(namespace, 1920 /* Namespace */, /*diagnosticMessage*/ undefined); - var typeSymbol = namespaceSymbol && getSymbol(namespaceSymbol.exports, name, 793064 /* Type */); - return typeSymbol && getDeclaredTypeOfSymbol(typeSymbol); - } - /** - * Creates a TypeReference for a generic `TypedPropertyDescriptor`. - */ - function createTypedPropertyDescriptorType(propertyType) { - var globalTypedPropertyDescriptorType = getGlobalTypedPropertyDescriptorType(); - return globalTypedPropertyDescriptorType !== emptyGenericType - ? createTypeReference(globalTypedPropertyDescriptorType, [propertyType]) - : emptyObjectType; - } - /** - * Instantiates a global type that is generic with some element type, and returns that instantiation. - */ - function createTypeFromGenericGlobalType(genericGlobalType, typeArguments) { - return genericGlobalType !== emptyGenericType ? createTypeReference(genericGlobalType, typeArguments) : emptyObjectType; - } - function createIterableType(elementType) { - return createTypeFromGenericGlobalType(getGlobalIterableType(), [elementType]); + return false; } - function createIterableIteratorType(elementType) { - return createTypeFromGenericGlobalType(getGlobalIterableIteratorType(), [elementType]); + function createSymbolTable(symbols) { + var result = ts.createMap(); + for (var _i = 0, symbols_1 = symbols; _i < symbols_1.length; _i++) { + var symbol = symbols_1[_i]; + result[symbol.name] = symbol; + } + return result; } - function createArrayType(elementType) { - return createTypeFromGenericGlobalType(globalArrayType, [elementType]); + // The mappingThisOnly flag indicates that the only type parameter being mapped is "this". When the flag is true, + // we check symbols to see if we can quickly conclude they are free of "this" references, thus needing no instantiation. + function createInstantiatedSymbolTable(symbols, mapper, mappingThisOnly) { + var result = ts.createMap(); + for (var _i = 0, symbols_2 = symbols; _i < symbols_2.length; _i++) { + var symbol = symbols_2[_i]; + result[symbol.name] = mappingThisOnly && isIndependentMember(symbol) ? symbol : instantiateSymbol(symbol, mapper); + } + return result; } - function getTypeFromArrayTypeNode(node) { - var links = getNodeLinks(node); - if (!links.resolvedType) { - links.resolvedType = createArrayType(getTypeFromTypeNode(node.elementType)); + function addInheritedMembers(symbols, baseSymbols) { + for (var _i = 0, baseSymbols_1 = baseSymbols; _i < baseSymbols_1.length; _i++) { + var s = baseSymbols_1[_i]; + if (!symbols[s.name]) { + symbols[s.name] = s; + } } - return links.resolvedType; } - // We represent tuple types as type references to synthesized generic interface types created by - // this function. The types are of the form: - // - // interface Tuple extends Array { 0: T0, 1: T1, 2: T2, ... } - // - // Note that the generic type created by this function has no symbol associated with it. The same - // is true for each of the synthesized type parameters. - function createTupleTypeOfArity(arity) { - var typeParameters = []; - var properties = []; - for (var i = 0; i < arity; i++) { - var typeParameter = createType(16384 /* TypeParameter */); - typeParameters.push(typeParameter); - var property = createSymbol(4 /* Property */ | 67108864 /* Transient */, "" + i); - property.type = typeParameter; - properties.push(property); + function resolveDeclaredMembers(type) { + if (!type.declaredProperties) { + var symbol = type.symbol; + type.declaredProperties = getNamedMembers(symbol.members); + type.declaredCallSignatures = getSignaturesOfSymbol(symbol.members["__call"]); + type.declaredConstructSignatures = getSignaturesOfSymbol(symbol.members["__new"]); + type.declaredStringIndexInfo = getIndexInfoOfSymbol(symbol, 0 /* String */); + type.declaredNumberIndexInfo = getIndexInfoOfSymbol(symbol, 1 /* Number */); } - var type = createObjectType(262144 /* Tuple */ | 131072 /* Reference */); - type.typeParameters = typeParameters; - type.outerTypeParameters = undefined; - type.localTypeParameters = typeParameters; - type.instantiations = ts.createMap(); - type.instantiations[getTypeListId(type.typeParameters)] = type; - type.target = type; - type.typeArguments = type.typeParameters; - type.thisType = createType(16384 /* TypeParameter */ | 268435456 /* ThisType */); - type.thisType.constraint = type; - type.declaredProperties = properties; - type.declaredCallSignatures = emptyArray; - type.declaredConstructSignatures = emptyArray; - type.declaredStringIndexInfo = undefined; - type.declaredNumberIndexInfo = undefined; return type; } - function getTupleTypeOfArity(arity) { - return tupleTypes[arity] || (tupleTypes[arity] = createTupleTypeOfArity(arity)); - } - function createTupleType(elementTypes) { - return createTypeReference(getTupleTypeOfArity(elementTypes.length), elementTypes); - } - function getTypeFromTupleTypeNode(node) { - var links = getNodeLinks(node); - if (!links.resolvedType) { - links.resolvedType = createTupleType(ts.map(node.elementTypes, getTypeFromTypeNodeNoAlias)); + function getTypeWithThisArgument(type, thisArgument) { + if (type.flags & 131072 /* Reference */) { + return createTypeReference(type.target, ts.concatenate(type.typeArguments, [thisArgument || type.target.thisType])); } - return links.resolvedType; + return type; } - function binarySearchTypes(types, type) { - var low = 0; - var high = types.length - 1; - var typeId = type.id; - while (low <= high) { - var middle = low + ((high - low) >> 1); - var id = types[middle].id; - if (id === typeId) { - return middle; - } - else if (id > typeId) { - high = middle - 1; + function resolveObjectTypeMembers(type, source, typeParameters, typeArguments) { + var mapper; + var members; + var callSignatures; + var constructSignatures; + var stringIndexInfo; + var numberIndexInfo; + if (ts.rangeEquals(typeParameters, typeArguments, 0, typeParameters.length)) { + mapper = identityMapper; + members = source.symbol ? source.symbol.members : createSymbolTable(source.declaredProperties); + callSignatures = source.declaredCallSignatures; + constructSignatures = source.declaredConstructSignatures; + stringIndexInfo = source.declaredStringIndexInfo; + numberIndexInfo = source.declaredNumberIndexInfo; + } + else { + mapper = createTypeMapper(typeParameters, typeArguments); + members = createInstantiatedSymbolTable(source.declaredProperties, mapper, /*mappingThisOnly*/ typeParameters.length === 1); + callSignatures = instantiateList(source.declaredCallSignatures, mapper, instantiateSignature); + constructSignatures = instantiateList(source.declaredConstructSignatures, mapper, instantiateSignature); + stringIndexInfo = instantiateIndexInfo(source.declaredStringIndexInfo, mapper); + numberIndexInfo = instantiateIndexInfo(source.declaredNumberIndexInfo, mapper); + } + var baseTypes = getBaseTypes(source); + if (baseTypes.length) { + if (source.symbol && members === source.symbol.members) { + members = createSymbolTable(source.declaredProperties); } - else { - low = middle + 1; + var thisArgument = ts.lastOrUndefined(typeArguments); + for (var _i = 0, baseTypes_1 = baseTypes; _i < baseTypes_1.length; _i++) { + var baseType = baseTypes_1[_i]; + var instantiatedBaseType = thisArgument ? getTypeWithThisArgument(instantiateType(baseType, mapper), thisArgument) : baseType; + addInheritedMembers(members, getPropertiesOfObjectType(instantiatedBaseType)); + callSignatures = ts.concatenate(callSignatures, getSignaturesOfType(instantiatedBaseType, 0 /* Call */)); + constructSignatures = ts.concatenate(constructSignatures, getSignaturesOfType(instantiatedBaseType, 1 /* Construct */)); + stringIndexInfo = stringIndexInfo || getIndexInfoOfType(instantiatedBaseType, 0 /* String */); + numberIndexInfo = numberIndexInfo || getIndexInfoOfType(instantiatedBaseType, 1 /* Number */); } } - return ~low; - } - function containsType(types, type) { - return binarySearchTypes(types, type) >= 0; + setObjectTypeMembers(type, members, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo); } - function addTypeToUnion(typeSet, type) { - var flags = type.flags; - if (flags & 524288 /* Union */) { - addTypesToUnion(typeSet, type.types); - } - else if (flags & 1 /* Any */) { - typeSet.containsAny = true; - } - else if (!strictNullChecks && flags & 6144 /* Nullable */) { - if (flags & 2048 /* Undefined */) - typeSet.containsUndefined = true; - if (flags & 4096 /* Null */) - typeSet.containsNull = true; - if (!(flags & 33554432 /* ContainsWideningType */)) - typeSet.containsNonWideningType = true; + function resolveClassOrInterfaceMembers(type) { + resolveObjectTypeMembers(type, resolveDeclaredMembers(type), emptyArray, emptyArray); + } + function resolveTypeReferenceMembers(type) { + var source = resolveDeclaredMembers(type.target); + var typeParameters = ts.concatenate(source.typeParameters, [source.thisType]); + var typeArguments = type.typeArguments && type.typeArguments.length === typeParameters.length ? + type.typeArguments : ts.concatenate(type.typeArguments, [type]); + resolveObjectTypeMembers(type, source, typeParameters, typeArguments); + } + function createSignature(declaration, typeParameters, thisParameter, parameters, resolvedReturnType, typePredicate, minArgumentCount, hasRestParameter, hasLiteralTypes) { + var sig = new Signature(checker); + sig.declaration = declaration; + sig.typeParameters = typeParameters; + sig.parameters = parameters; + sig.thisParameter = thisParameter; + sig.resolvedReturnType = resolvedReturnType; + sig.typePredicate = typePredicate; + sig.minArgumentCount = minArgumentCount; + sig.hasRestParameter = hasRestParameter; + sig.hasLiteralTypes = hasLiteralTypes; + return sig; + } + function cloneSignature(sig) { + return createSignature(sig.declaration, sig.typeParameters, sig.thisParameter, sig.parameters, sig.resolvedReturnType, sig.typePredicate, sig.minArgumentCount, sig.hasRestParameter, sig.hasLiteralTypes); + } + function getDefaultConstructSignatures(classType) { + var baseConstructorType = getBaseConstructorTypeOfClass(classType); + var baseSignatures = getSignaturesOfType(baseConstructorType, 1 /* Construct */); + if (baseSignatures.length === 0) { + return [createSignature(undefined, classType.localTypeParameters, undefined, emptyArray, classType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false)]; } - else if (!(flags & 8192 /* Never */)) { - if (flags & 2 /* String */) - typeSet.containsString = true; - if (flags & 4 /* Number */) - typeSet.containsNumber = true; - if (flags & 96 /* StringOrNumberLiteral */) - typeSet.containsStringOrNumberLiteral = true; - var len = typeSet.length; - var index = len && type.id > typeSet[len - 1].id ? ~len : binarySearchTypes(typeSet, type); - if (index < 0) { - if (!(flags & 2097152 /* Anonymous */ && type.symbol && type.symbol.flags & (16 /* Function */ | 8192 /* Method */) && containsIdenticalType(typeSet, type))) { - typeSet.splice(~index, 0, type); - } + var baseTypeNode = getBaseTypeNodeOfClass(classType); + var typeArguments = ts.map(baseTypeNode.typeArguments, getTypeFromTypeNodeNoAlias); + var typeArgCount = typeArguments ? typeArguments.length : 0; + var result = []; + for (var _i = 0, baseSignatures_1 = baseSignatures; _i < baseSignatures_1.length; _i++) { + var baseSig = baseSignatures_1[_i]; + var typeParamCount = baseSig.typeParameters ? baseSig.typeParameters.length : 0; + if (typeParamCount === typeArgCount) { + var sig = typeParamCount ? getSignatureInstantiation(baseSig, typeArguments) : cloneSignature(baseSig); + sig.typeParameters = classType.localTypeParameters; + sig.resolvedReturnType = classType; + result.push(sig); } } + return result; } - // Add the given types to the given type set. Order is preserved, duplicates are removed, - // and nested types of the given kind are flattened into the set. - function addTypesToUnion(typeSet, types) { - for (var _i = 0, types_4 = types; _i < types_4.length; _i++) { - var type = types_4[_i]; - addTypeToUnion(typeSet, type); + function findMatchingSignature(signatureList, signature, partialMatch, ignoreThisTypes, ignoreReturnTypes) { + for (var _i = 0, signatureList_1 = signatureList; _i < signatureList_1.length; _i++) { + var s = signatureList_1[_i]; + if (compareSignaturesIdentical(s, signature, partialMatch, ignoreThisTypes, ignoreReturnTypes, compareTypesIdentical)) { + return s; + } } } - function containsIdenticalType(types, type) { - for (var _i = 0, types_5 = types; _i < types_5.length; _i++) { - var t = types_5[_i]; - if (isTypeIdenticalTo(t, type)) { - return true; + function findMatchingSignatures(signatureLists, signature, listIndex) { + if (signature.typeParameters) { + // We require an exact match for generic signatures, so we only return signatures from the first + // signature list and only if they have exact matches in the other signature lists. + if (listIndex > 0) { + return undefined; + } + for (var i = 1; i < signatureLists.length; i++) { + if (!findMatchingSignature(signatureLists[i], signature, /*partialMatch*/ false, /*ignoreThisTypes*/ false, /*ignoreReturnTypes*/ false)) { + return undefined; + } } + return [signature]; } - return false; - } - function isSubtypeOfAny(candidate, types) { - for (var i = 0, len = types.length; i < len; i++) { - if (candidate !== types[i] && isTypeSubtypeOf(candidate, types[i])) { - return true; + var result = undefined; + for (var i = 0; i < signatureLists.length; i++) { + // Allow matching non-generic signatures to have excess parameters and different return types + var match = i === listIndex ? signature : findMatchingSignature(signatureLists[i], signature, /*partialMatch*/ true, /*ignoreThisTypes*/ true, /*ignoreReturnTypes*/ true); + if (!match) { + return undefined; + } + if (!ts.contains(result, match)) { + (result || (result = [])).push(match); } } - return false; + return result; } - function removeSubtypes(types) { - var i = types.length; - while (i > 0) { - i--; - if (isSubtypeOfAny(types[i], types)) { - ts.orderedRemoveItemAt(types, i); + // The signatures of a union type are those signatures that are present in each of the constituent types. + // Generic signatures must match exactly, but non-generic signatures are allowed to have extra optional + // parameters and may differ in return types. When signatures differ in return types, the resulting return + // type is the union of the constituent return types. + function getUnionSignatures(types, kind) { + var signatureLists = ts.map(types, function (t) { return getSignaturesOfType(t, kind); }); + var result = undefined; + for (var i = 0; i < signatureLists.length; i++) { + for (var _i = 0, _a = signatureLists[i]; _i < _a.length; _i++) { + var signature = _a[_i]; + // Only process signatures with parameter lists that aren't already in the result list + if (!result || !findMatchingSignature(result, signature, /*partialMatch*/ false, /*ignoreThisTypes*/ true, /*ignoreReturnTypes*/ true)) { + var unionSignatures = findMatchingSignatures(signatureLists, signature, i); + if (unionSignatures) { + var s = signature; + // Union the result types when more than one signature matches + if (unionSignatures.length > 1) { + s = cloneSignature(signature); + if (ts.forEach(unionSignatures, function (sig) { return sig.thisParameter; })) { + var thisType = getUnionType(ts.map(unionSignatures, function (sig) { return getTypeOfSymbol(sig.thisParameter) || anyType; }), /*subtypeReduction*/ true); + s.thisParameter = createTransientSymbol(signature.thisParameter, thisType); + } + // Clear resolved return type we possibly got from cloneSignature + s.resolvedReturnType = undefined; + s.unionSignatures = unionSignatures; + } + (result || (result = [])).push(s); + } + } } } + return result || emptyArray; } - function removeRedundantLiteralTypes(types) { - var i = types.length; - while (i > 0) { - i--; - var t = types[i]; - var remove = t.flags & 32 /* StringLiteral */ && types.containsString || - t.flags & 64 /* NumberLiteral */ && types.containsNumber || - t.flags & 96 /* StringOrNumberLiteral */ && t.flags & 16777216 /* FreshLiteral */ && containsType(types, t.regularType); - if (remove) { - ts.orderedRemoveItemAt(types, i); + function getUnionIndexInfo(types, kind) { + var indexTypes = []; + var isAnyReadonly = false; + for (var _i = 0, types_1 = types; _i < types_1.length; _i++) { + var type = types_1[_i]; + var indexInfo = getIndexInfoOfType(type, kind); + if (!indexInfo) { + return undefined; } + indexTypes.push(indexInfo.type); + isAnyReadonly = isAnyReadonly || indexInfo.isReadonly; } + return createIndexInfo(getUnionType(indexTypes, /*subtypeReduction*/ true), isAnyReadonly); } - // We sort and deduplicate the constituent types based on object identity. If the subtypeReduction - // flag is specified we also reduce the constituent type set to only include types that aren't subtypes - // of other types. Subtype reduction is expensive for large union types and is possible only when union - // types are known not to circularly reference themselves (as is the case with union types created by - // expression constructs such as array literals and the || and ?: operators). Named types can - // circularly reference themselves and therefore cannot be subtype reduced during their declaration. - // For example, "type Item = string | (() => Item" is a named type that circularly references itself. - function getUnionType(types, subtypeReduction, aliasSymbol, aliasTypeArguments) { - if (types.length === 0) { - return neverType; - } - if (types.length === 1) { - return types[0]; - } - var typeSet = []; - addTypesToUnion(typeSet, types); - if (typeSet.containsAny) { - return anyType; + function resolveUnionTypeMembers(type) { + // The members and properties collections are empty for union types. To get all properties of a union + // type use getPropertiesOfType (only the language service uses this). + var callSignatures = getUnionSignatures(type.types, 0 /* Call */); + var constructSignatures = getUnionSignatures(type.types, 1 /* Construct */); + var stringIndexInfo = getUnionIndexInfo(type.types, 0 /* String */); + var numberIndexInfo = getUnionIndexInfo(type.types, 1 /* Number */); + setObjectTypeMembers(type, emptySymbols, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo); + } + function intersectTypes(type1, type2) { + return !type1 ? type2 : !type2 ? type1 : getIntersectionType([type1, type2]); + } + function intersectIndexInfos(info1, info2) { + return !info1 ? info2 : !info2 ? info1 : createIndexInfo(getIntersectionType([info1.type, info2.type]), info1.isReadonly && info2.isReadonly); + } + function resolveIntersectionTypeMembers(type) { + // The members and properties collections are empty for intersection types. To get all properties of an + // intersection type use getPropertiesOfType (only the language service uses this). + var callSignatures = emptyArray; + var constructSignatures = emptyArray; + var stringIndexInfo = undefined; + var numberIndexInfo = undefined; + for (var _i = 0, _a = type.types; _i < _a.length; _i++) { + var t = _a[_i]; + callSignatures = ts.concatenate(callSignatures, getSignaturesOfType(t, 0 /* Call */)); + constructSignatures = ts.concatenate(constructSignatures, getSignaturesOfType(t, 1 /* Construct */)); + stringIndexInfo = intersectIndexInfos(stringIndexInfo, getIndexInfoOfType(t, 0 /* String */)); + numberIndexInfo = intersectIndexInfos(numberIndexInfo, getIndexInfoOfType(t, 1 /* Number */)); } - if (subtypeReduction) { - removeSubtypes(typeSet); + setObjectTypeMembers(type, emptySymbols, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo); + } + function resolveAnonymousTypeMembers(type) { + var symbol = type.symbol; + if (type.target) { + var members = createInstantiatedSymbolTable(getPropertiesOfObjectType(type.target), type.mapper, /*mappingThisOnly*/ false); + var callSignatures = instantiateList(getSignaturesOfType(type.target, 0 /* Call */), type.mapper, instantiateSignature); + var constructSignatures = instantiateList(getSignaturesOfType(type.target, 1 /* Construct */), type.mapper, instantiateSignature); + var stringIndexInfo = instantiateIndexInfo(getIndexInfoOfType(type.target, 0 /* String */), type.mapper); + var numberIndexInfo = instantiateIndexInfo(getIndexInfoOfType(type.target, 1 /* Number */), type.mapper); + setObjectTypeMembers(type, members, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo); } - else if (typeSet.containsStringOrNumberLiteral) { - removeRedundantLiteralTypes(typeSet); + else if (symbol.flags & 2048 /* TypeLiteral */) { + var members = symbol.members; + var callSignatures = getSignaturesOfSymbol(members["__call"]); + var constructSignatures = getSignaturesOfSymbol(members["__new"]); + var stringIndexInfo = getIndexInfoOfSymbol(symbol, 0 /* String */); + var numberIndexInfo = getIndexInfoOfSymbol(symbol, 1 /* Number */); + setObjectTypeMembers(type, members, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo); } - if (typeSet.length === 0) { - return typeSet.containsNull ? typeSet.containsNonWideningType ? nullType : nullWideningType : - typeSet.containsUndefined ? typeSet.containsNonWideningType ? undefinedType : undefinedWideningType : - neverType; + else { + // Combinations of function, class, enum and module + var members = emptySymbols; + var constructSignatures = emptyArray; + if (symbol.flags & 1952 /* HasExports */) { + members = getExportsOfSymbol(symbol); + } + if (symbol.flags & 32 /* Class */) { + var classType = getDeclaredTypeOfClassOrInterface(symbol); + constructSignatures = getSignaturesOfSymbol(symbol.members["__constructor"]); + if (!constructSignatures.length) { + constructSignatures = getDefaultConstructSignatures(classType); + } + var baseConstructorType = getBaseConstructorTypeOfClass(classType); + if (baseConstructorType.flags & 2588672 /* ObjectType */) { + members = createSymbolTable(getNamedMembers(members)); + addInheritedMembers(members, getPropertiesOfObjectType(baseConstructorType)); + } + } + var numberIndexInfo = symbol.flags & 384 /* Enum */ ? enumNumberIndexInfo : undefined; + setObjectTypeMembers(type, members, emptyArray, constructSignatures, undefined, numberIndexInfo); + // We resolve the members before computing the signatures because a signature may use + // typeof with a qualified name expression that circularly references the type we are + // in the process of resolving (see issue #6072). The temporarily empty signature list + // will never be observed because a qualified name can't reference signatures. + if (symbol.flags & (16 /* Function */ | 8192 /* Method */)) { + type.callSignatures = getSignaturesOfSymbol(symbol); + } } - return getUnionTypeFromSortedList(typeSet, aliasSymbol, aliasTypeArguments); } - // This function assumes the constituent type list is sorted and deduplicated. - function getUnionTypeFromSortedList(types, aliasSymbol, aliasTypeArguments) { - if (types.length === 0) { - return neverType; - } - if (types.length === 1) { - return types[0]; - } - var id = getTypeListId(types); - var type = unionTypes[id]; - if (!type) { - var propagatedFlags = getPropagatingFlagsOfTypes(types, /*excludeKinds*/ 6144 /* Nullable */); - type = unionTypes[id] = createObjectType(524288 /* Union */ | propagatedFlags); - type.types = types; - type.aliasSymbol = aliasSymbol; - type.aliasTypeArguments = aliasTypeArguments; + function resolveStructuredTypeMembers(type) { + if (!type.members) { + if (type.flags & 131072 /* Reference */) { + resolveTypeReferenceMembers(type); + } + else if (type.flags & (32768 /* Class */ | 65536 /* Interface */)) { + resolveClassOrInterfaceMembers(type); + } + else if (type.flags & 2097152 /* Anonymous */) { + resolveAnonymousTypeMembers(type); + } + else if (type.flags & 524288 /* Union */) { + resolveUnionTypeMembers(type); + } + else if (type.flags & 1048576 /* Intersection */) { + resolveIntersectionTypeMembers(type); + } } return type; } - function getTypeFromUnionTypeNode(node, aliasSymbol, aliasTypeArguments) { - var links = getNodeLinks(node); - if (!links.resolvedType) { - links.resolvedType = getUnionType(ts.map(node.types, getTypeFromTypeNodeNoAlias), /*subtypeReduction*/ false, aliasSymbol, aliasTypeArguments); + /** Return properties of an object type or an empty array for other types */ + function getPropertiesOfObjectType(type) { + if (type.flags & 2588672 /* ObjectType */) { + return resolveStructuredTypeMembers(type).properties; } - return links.resolvedType; + return emptyArray; } - function addTypeToIntersection(typeSet, type) { - if (type.flags & 1048576 /* Intersection */) { - addTypesToIntersection(typeSet, type.types); + /** If the given type is an object type and that type has a property by the given name, + * return the symbol for that property. Otherwise return undefined. */ + function getPropertyOfObjectType(type, name) { + if (type.flags & 2588672 /* ObjectType */) { + var resolved = resolveStructuredTypeMembers(type); + var symbol = resolved.members[name]; + if (symbol && symbolIsValue(symbol)) { + return symbol; + } } - else if (type.flags & 1 /* Any */) { - typeSet.containsAny = true; + } + function getPropertiesOfUnionOrIntersectionType(type) { + for (var _i = 0, _a = type.types; _i < _a.length; _i++) { + var current = _a[_i]; + for (var _b = 0, _c = getPropertiesOfType(current); _b < _c.length; _b++) { + var prop = _c[_b]; + getUnionOrIntersectionProperty(type, prop.name); + } + // The properties of a union type are those that are present in all constituent types, so + // we only need to check the properties of the first type + if (type.flags & 524288 /* Union */) { + break; + } } - else if (!(type.flags & 8192 /* Never */) && (strictNullChecks || !(type.flags & 6144 /* Nullable */)) && !ts.contains(typeSet, type)) { - typeSet.push(type); + var props = type.resolvedProperties; + if (props) { + var result = []; + for (var key in props) { + var prop = props[key]; + // We need to filter out partial properties in union types + if (!(prop.flags & 268435456 /* SyntheticProperty */ && prop.isPartial)) { + result.push(prop); + } + } + return result; } + return emptyArray; } - // Add the given types to the given type set. Order is preserved, duplicates are removed, - // and nested types of the given kind are flattened into the set. - function addTypesToIntersection(typeSet, types) { - for (var _i = 0, types_6 = types; _i < types_6.length; _i++) { - var type = types_6[_i]; - addTypeToIntersection(typeSet, type); + function getPropertiesOfType(type) { + type = getApparentType(type); + return type.flags & 1572864 /* UnionOrIntersection */ ? getPropertiesOfUnionOrIntersectionType(type) : getPropertiesOfObjectType(type); + } + /** + * The apparent type of a type parameter is the base constraint instantiated with the type parameter + * as the type argument for the 'this' type. + */ + function getApparentTypeOfTypeParameter(type) { + if (!type.resolvedApparentType) { + var constraintType = getConstraintOfTypeParameter(type); + while (constraintType && constraintType.flags & 16384 /* TypeParameter */) { + constraintType = getConstraintOfTypeParameter(constraintType); + } + type.resolvedApparentType = getTypeWithThisArgument(constraintType || emptyObjectType, type); } + return type.resolvedApparentType; } - // We do not perform structural deduplication on intersection types. Intersection types are created only by the & - // type operator and we can't reduce those because we want to support recursive intersection types. For example, - // a type alias of the form "type List = T & { next: List }" cannot be reduced during its declaration. - // Also, unlike union types, the order of the constituent types is preserved in order that overload resolution - // for intersections of types with signatures can be deterministic. - function getIntersectionType(types, aliasSymbol, aliasTypeArguments) { - if (types.length === 0) { - return emptyObjectType; + /** + * For a type parameter, return the base constraint of the type parameter. For the string, number, + * boolean, and symbol primitive types, return the corresponding object types. Otherwise return the + * type itself. Note that the apparent type of a union type is the union type itself. + */ + function getApparentType(type) { + if (type.flags & 16384 /* TypeParameter */) { + type = getApparentTypeOfTypeParameter(type); } - var typeSet = []; - addTypesToIntersection(typeSet, types); - if (typeSet.containsAny) { - return anyType; + if (type.flags & 34 /* StringLike */) { + type = globalStringType; } - if (typeSet.length === 1) { - return typeSet[0]; + else if (type.flags & 340 /* NumberLike */) { + type = globalNumberType; } - var id = getTypeListId(typeSet); - var type = intersectionTypes[id]; - if (!type) { - var propagatedFlags = getPropagatingFlagsOfTypes(typeSet, /*excludeKinds*/ 6144 /* Nullable */); - type = intersectionTypes[id] = createObjectType(1048576 /* Intersection */ | propagatedFlags); - type.types = typeSet; - type.aliasSymbol = aliasSymbol; - type.aliasTypeArguments = aliasTypeArguments; + else if (type.flags & 136 /* BooleanLike */) { + type = globalBooleanType; + } + else if (type.flags & 512 /* ESSymbol */) { + type = getGlobalESSymbolType(); } return type; } - function getTypeFromIntersectionTypeNode(node, aliasSymbol, aliasTypeArguments) { - var links = getNodeLinks(node); - if (!links.resolvedType) { - links.resolvedType = getIntersectionType(ts.map(node.types, getTypeFromTypeNodeNoAlias), aliasSymbol, aliasTypeArguments); + function createUnionOrIntersectionProperty(containingType, name) { + var types = containingType.types; + var props; + // Flags we want to propagate to the result if they exist in all source symbols + var commonFlags = (containingType.flags & 1048576 /* Intersection */) ? 536870912 /* Optional */ : 0 /* None */; + var isReadonly = false; + var isPartial = false; + for (var _i = 0, types_2 = types; _i < types_2.length; _i++) { + var current = types_2[_i]; + var type = getApparentType(current); + if (type !== unknownType) { + var prop = getPropertyOfType(type, name); + if (prop && !(getDeclarationModifierFlagsFromSymbol(prop) & (8 /* Private */ | 16 /* Protected */))) { + commonFlags &= prop.flags; + if (!props) { + props = [prop]; + } + else if (!ts.contains(props, prop)) { + props.push(prop); + } + if (isReadonlySymbol(prop)) { + isReadonly = true; + } + } + else if (containingType.flags & 524288 /* Union */) { + isPartial = true; + } + } } - return links.resolvedType; - } - function getTypeFromTypeLiteralOrFunctionOrConstructorTypeNode(node, aliasSymbol, aliasTypeArguments) { - var links = getNodeLinks(node); - if (!links.resolvedType) { - // Deferred resolution of members is handled by resolveObjectTypeMembers - var type = createObjectType(2097152 /* Anonymous */, node.symbol); - type.aliasSymbol = aliasSymbol; - type.aliasTypeArguments = aliasTypeArguments; - links.resolvedType = type; + if (!props) { + return undefined; } - return links.resolvedType; - } - function createLiteralType(flags, text) { - var type = createType(flags); - type.text = text; - return type; - } - function getFreshTypeOfLiteralType(type) { - if (type.flags & 96 /* StringOrNumberLiteral */ && !(type.flags & 16777216 /* FreshLiteral */)) { - if (!type.freshType) { - var freshType = createLiteralType(type.flags | 16777216 /* FreshLiteral */, type.text); - freshType.regularType = type; - type.freshType = freshType; + if (props.length === 1 && !isPartial) { + return props[0]; + } + var propTypes = []; + var declarations = []; + var commonType = undefined; + var hasNonUniformType = false; + for (var _a = 0, props_1 = props; _a < props_1.length; _a++) { + var prop = props_1[_a]; + if (prop.declarations) { + ts.addRange(declarations, prop.declarations); } - return type.freshType; + var type = getTypeOfSymbol(prop); + if (!commonType) { + commonType = type; + } + else if (type !== commonType) { + hasNonUniformType = true; + } + propTypes.push(type); } - return type; - } - function getRegularTypeOfLiteralType(type) { - return type.flags & 96 /* StringOrNumberLiteral */ && type.flags & 16777216 /* FreshLiteral */ ? type.regularType : type; - } - function getLiteralTypeForText(flags, text) { - var map = flags & 32 /* StringLiteral */ ? stringLiteralTypes : numericLiteralTypes; - return map[text] || (map[text] = createLiteralType(flags, text)); + var result = createSymbol(4 /* Property */ | 67108864 /* Transient */ | 268435456 /* SyntheticProperty */ | commonFlags, name); + result.containingType = containingType; + result.hasNonUniformType = hasNonUniformType; + result.isPartial = isPartial; + result.declarations = declarations; + result.isReadonly = isReadonly; + result.type = containingType.flags & 524288 /* Union */ ? getUnionType(propTypes) : getIntersectionType(propTypes); + return result; } - function getTypeFromLiteralTypeNode(node) { - var links = getNodeLinks(node); - if (!links.resolvedType) { - links.resolvedType = getRegularTypeOfLiteralType(checkExpression(node.literal)); + // Return the symbol for a given property in a union or intersection type, or undefined if the property + // does not exist in any constituent type. Note that the returned property may only be present in some + // constituents, in which case the isPartial flag is set when the containing type is union type. We need + // these partial properties when identifying discriminant properties, but otherwise they are filtered out + // and do not appear to be present in the union type. + function getUnionOrIntersectionProperty(type, name) { + var properties = type.resolvedProperties || (type.resolvedProperties = ts.createMap()); + var property = properties[name]; + if (!property) { + property = createUnionOrIntersectionProperty(type, name); + if (property) { + properties[name] = property; + } } - return links.resolvedType; + return property; } - function getTypeFromJSDocVariadicType(node) { - var links = getNodeLinks(node); - if (!links.resolvedType) { - var type = getTypeFromTypeNode(node.type); - links.resolvedType = type ? createArrayType(type) : unknownType; - } - return links.resolvedType; - } - function getTypeFromJSDocTupleType(node) { - var links = getNodeLinks(node); - if (!links.resolvedType) { - var types = ts.map(node.types, getTypeFromTypeNodeNoAlias); - links.resolvedType = createTupleType(types); - } - return links.resolvedType; + function getPropertyOfUnionOrIntersectionType(type, name) { + var property = getUnionOrIntersectionProperty(type, name); + // We need to filter out partial properties in union types + return property && !(property.flags & 268435456 /* SyntheticProperty */ && property.isPartial) ? property : undefined; } - function getThisType(node) { - var container = ts.getThisContainer(node, /*includeArrowFunctions*/ false); - var parent = container && container.parent; - if (parent && (ts.isClassLike(parent) || parent.kind === 222 /* InterfaceDeclaration */)) { - if (!(ts.getModifierFlags(container) & 32 /* Static */) && - (container.kind !== 148 /* Constructor */ || ts.isNodeDescendantOf(node, container.body))) { - return getDeclaredTypeOfClassOrInterface(getSymbolOfNode(parent)).thisType; + /** + * Return the symbol for the property with the given name in the given type. Creates synthetic union properties when + * necessary, maps primitive types and type parameters are to their apparent types, and augments with properties from + * Object and Function as appropriate. + * + * @param type a type to look up property from + * @param name a name of property to look up in a given type + */ + function getPropertyOfType(type, name) { + type = getApparentType(type); + if (type.flags & 2588672 /* ObjectType */) { + var resolved = resolveStructuredTypeMembers(type); + var symbol = resolved.members[name]; + if (symbol && symbolIsValue(symbol)) { + return symbol; + } + if (resolved === anyFunctionType || resolved.callSignatures.length || resolved.constructSignatures.length) { + var symbol_1 = getPropertyOfObjectType(globalFunctionType, name); + if (symbol_1) { + return symbol_1; + } } + return getPropertyOfObjectType(globalObjectType, name); } - error(node, ts.Diagnostics.A_this_type_is_available_only_in_a_non_static_member_of_a_class_or_interface); - return unknownType; + if (type.flags & 1572864 /* UnionOrIntersection */) { + return getPropertyOfUnionOrIntersectionType(type, name); + } + return undefined; } - function getTypeFromThisTypeNode(node) { - var links = getNodeLinks(node); - if (!links.resolvedType) { - links.resolvedType = getThisType(node); + function getSignaturesOfStructuredType(type, kind) { + if (type.flags & 4161536 /* StructuredType */) { + var resolved = resolveStructuredTypeMembers(type); + return kind === 0 /* Call */ ? resolved.callSignatures : resolved.constructSignatures; } - return links.resolvedType; + return emptyArray; } - function getTypeFromTypeNodeNoAlias(type) { - return getTypeFromTypeNode(type, /*aliasSymbol*/ undefined, /*aliasTypeArguments*/ undefined); + /** + * Return the signatures of the given kind in the given type. Creates synthetic union signatures when necessary and + * maps primitive types and type parameters are to their apparent types. + */ + function getSignaturesOfType(type, kind) { + return getSignaturesOfStructuredType(getApparentType(type), kind); } - function getTypeFromTypeNode(node, aliasSymbol, aliasTypeArguments) { - switch (node.kind) { - case 117 /* AnyKeyword */: - case 258 /* JSDocAllType */: - case 259 /* JSDocUnknownType */: - return anyType; - case 132 /* StringKeyword */: - return stringType; - case 130 /* NumberKeyword */: - return numberType; - case 120 /* BooleanKeyword */: - return booleanType; - case 133 /* SymbolKeyword */: - return esSymbolType; - case 103 /* VoidKeyword */: - return voidType; - case 135 /* UndefinedKeyword */: - return undefinedType; - case 93 /* NullKeyword */: - return nullType; - case 127 /* NeverKeyword */: - return neverType; - case 283 /* JSDocNullKeyword */: - return nullType; - case 284 /* JSDocUndefinedKeyword */: - return undefinedType; - case 285 /* JSDocNeverKeyword */: - return neverType; - case 165 /* ThisType */: - case 97 /* ThisKeyword */: - return getTypeFromThisTypeNode(node); - case 166 /* LiteralType */: - return getTypeFromLiteralTypeNode(node); - case 282 /* JSDocLiteralType */: - return getTypeFromLiteralTypeNode(node.literal); - case 155 /* TypeReference */: - case 267 /* JSDocTypeReference */: - return getTypeFromTypeReference(node); - case 154 /* TypePredicate */: - return booleanType; - case 194 /* ExpressionWithTypeArguments */: - return getTypeFromTypeReference(node); - case 158 /* TypeQuery */: - return getTypeFromTypeQueryNode(node); - case 160 /* ArrayType */: - case 260 /* JSDocArrayType */: - return getTypeFromArrayTypeNode(node); - case 161 /* TupleType */: - return getTypeFromTupleTypeNode(node); - case 162 /* UnionType */: - case 261 /* JSDocUnionType */: - return getTypeFromUnionTypeNode(node, aliasSymbol, aliasTypeArguments); - case 163 /* IntersectionType */: - return getTypeFromIntersectionTypeNode(node, aliasSymbol, aliasTypeArguments); - case 164 /* ParenthesizedType */: - case 263 /* JSDocNullableType */: - case 264 /* JSDocNonNullableType */: - case 271 /* JSDocConstructorType */: - case 272 /* JSDocThisType */: - case 268 /* JSDocOptionalType */: - return getTypeFromTypeNode(node.type); - case 265 /* JSDocRecordType */: - return getTypeFromTypeNode(node.literal); - case 156 /* FunctionType */: - case 157 /* ConstructorType */: - case 159 /* TypeLiteral */: - case 281 /* JSDocTypeLiteral */: - case 269 /* JSDocFunctionType */: - return getTypeFromTypeLiteralOrFunctionOrConstructorTypeNode(node, aliasSymbol, aliasTypeArguments); - // This function assumes that an identifier or qualified name is a type expression - // Callers should first ensure this by calling isTypeNode - case 69 /* Identifier */: - case 139 /* QualifiedName */: - var symbol = getSymbolAtLocation(node); - return symbol && getDeclaredTypeOfSymbol(symbol); - case 262 /* JSDocTupleType */: - return getTypeFromJSDocTupleType(node); - case 270 /* JSDocVariadicType */: - return getTypeFromJSDocVariadicType(node); - default: - return unknownType; + function getIndexInfoOfStructuredType(type, kind) { + if (type.flags & 4161536 /* StructuredType */) { + var resolved = resolveStructuredTypeMembers(type); + return kind === 0 /* String */ ? resolved.stringIndexInfo : resolved.numberIndexInfo; } } - function instantiateList(items, mapper, instantiator) { - if (items && items.length) { - var result = []; - for (var _i = 0, items_1 = items; _i < items_1.length; _i++) { - var v = items_1[_i]; - result.push(instantiator(v, mapper)); - } - return result; - } - return items; + function getIndexTypeOfStructuredType(type, kind) { + var info = getIndexInfoOfStructuredType(type, kind); + return info && info.type; } - function createUnaryTypeMapper(source, target) { - return function (t) { return t === source ? target : t; }; + // Return the indexing info of the given kind in the given type. Creates synthetic union index types when necessary and + // maps primitive types and type parameters are to their apparent types. + function getIndexInfoOfType(type, kind) { + return getIndexInfoOfStructuredType(getApparentType(type), kind); } - function createBinaryTypeMapper(source1, target1, source2, target2) { - return function (t) { return t === source1 ? target1 : t === source2 ? target2 : t; }; + // Return the index type of the given kind in the given type. Creates synthetic union index types when necessary and + // maps primitive types and type parameters are to their apparent types. + function getIndexTypeOfType(type, kind) { + return getIndexTypeOfStructuredType(getApparentType(type), kind); } - function createArrayTypeMapper(sources, targets) { - return function (t) { - for (var i = 0; i < sources.length; i++) { - if (t === sources[i]) { - return targets ? targets[i] : anyType; + function getImplicitIndexTypeOfType(type, kind) { + if (isObjectLiteralType(type)) { + var propTypes = []; + for (var _i = 0, _a = getPropertiesOfType(type); _i < _a.length; _i++) { + var prop = _a[_i]; + if (kind === 0 /* String */ || isNumericLiteralName(prop.name)) { + propTypes.push(getTypeOfSymbol(prop)); } } - return t; - }; + if (propTypes.length) { + return getUnionType(propTypes, /*subtypeReduction*/ true); + } + } + return undefined; } - function createTypeMapper(sources, targets) { - var count = sources.length; - var mapper = count == 1 ? createUnaryTypeMapper(sources[0], targets ? targets[0] : anyType) : - count == 2 ? createBinaryTypeMapper(sources[0], targets ? targets[0] : anyType, sources[1], targets ? targets[1] : anyType) : - createArrayTypeMapper(sources, targets); - mapper.mappedTypes = sources; - mapper.targetTypes = targets; - return mapper; + function getTypeParametersFromJSDocTemplate(declaration) { + if (declaration.flags & 1048576 /* JavaScriptFile */) { + var templateTag = ts.getJSDocTemplateTag(declaration); + if (templateTag) { + return getTypeParametersFromDeclaration(templateTag.typeParameters); + } + } + return undefined; } - function createTypeEraser(sources) { - return createTypeMapper(sources, undefined); + // Return list of type parameters with duplicates removed (duplicate identifier errors are generated in the actual + // type checking functions). + function getTypeParametersFromDeclaration(typeParameterDeclarations) { + var result = []; + ts.forEach(typeParameterDeclarations, function (node) { + var tp = getDeclaredTypeOfTypeParameter(node.symbol); + if (!ts.contains(result, tp)) { + result.push(tp); + } + }); + return result; } - function getInferenceMapper(context) { - if (!context.mapper) { - var mapper = function (t) { - var typeParameters = context.signature.typeParameters; - for (var i = 0; i < typeParameters.length; i++) { - if (t === typeParameters[i]) { - context.inferences[i].isFixed = true; - return getInferredType(context, i); - } - } - return t; - }; - mapper.mappedTypes = context.signature.typeParameters; - mapper.context = context; - context.mapper = mapper; + function symbolsToArray(symbols) { + var result = []; + for (var id in symbols) { + if (!isReservedMemberName(id)) { + result.push(symbols[id]); + } } - return context.mapper; - } - function identityMapper(type) { - return type; + return result; } - function combineTypeMappers(mapper1, mapper2) { - var mapper = function (t) { return instantiateType(mapper1(t), mapper2); }; - mapper.mappedTypes = mapper1.mappedTypes; - return mapper; + function isJSDocOptionalParameter(node) { + if (node.flags & 1048576 /* JavaScriptFile */) { + if (node.type && node.type.kind === 268 /* JSDocOptionalType */) { + return true; + } + var paramTag = ts.getCorrespondingJSDocParameterTag(node); + if (paramTag) { + if (paramTag.isBracketed) { + return true; + } + if (paramTag.typeExpression) { + return paramTag.typeExpression.type.kind === 268 /* JSDocOptionalType */; + } + } + } } - function cloneTypeParameter(typeParameter) { - var result = createType(16384 /* TypeParameter */); - result.symbol = typeParameter.symbol; - result.target = typeParameter; - return result; + function isOptionalParameter(node) { + if (ts.hasQuestionToken(node) || isJSDocOptionalParameter(node)) { + return true; + } + if (node.initializer) { + var signatureDeclaration = node.parent; + var signature = getSignatureFromDeclaration(signatureDeclaration); + var parameterIndex = ts.indexOf(signatureDeclaration.parameters, node); + ts.Debug.assert(parameterIndex >= 0); + return parameterIndex >= signature.minArgumentCount; + } + return false; } - function cloneTypePredicate(predicate, mapper) { - if (ts.isIdentifierTypePredicate(predicate)) { + function createTypePredicateFromTypePredicateNode(node) { + if (node.parameterName.kind === 69 /* Identifier */) { + var parameterName = node.parameterName; return { kind: 1 /* Identifier */, - parameterName: predicate.parameterName, - parameterIndex: predicate.parameterIndex, - type: instantiateType(predicate.type, mapper) + parameterName: parameterName ? parameterName.text : undefined, + parameterIndex: parameterName ? getTypePredicateParameterIndex(node.parent.parameters, parameterName) : undefined, + type: getTypeFromTypeNode(node.type) }; } else { return { kind: 0 /* This */, - type: instantiateType(predicate.type, mapper) + type: getTypeFromTypeNode(node.type) }; } } - function instantiateSignature(signature, mapper, eraseTypeParameters) { - var freshTypeParameters; - var freshTypePredicate; - if (signature.typeParameters && !eraseTypeParameters) { - // First create a fresh set of type parameters, then include a mapping from the old to the - // new type parameters in the mapper function. Finally store this mapper in the new type - // parameters such that we can use it when instantiating constraints. - freshTypeParameters = ts.map(signature.typeParameters, cloneTypeParameter); - mapper = combineTypeMappers(createTypeMapper(signature.typeParameters, freshTypeParameters), mapper); - for (var _i = 0, freshTypeParameters_1 = freshTypeParameters; _i < freshTypeParameters_1.length; _i++) { - var tp = freshTypeParameters_1[_i]; - tp.mapper = mapper; + function getSignatureFromDeclaration(declaration) { + var links = getNodeLinks(declaration); + if (!links.resolvedSignature) { + var parameters = []; + var hasLiteralTypes = false; + var minArgumentCount = -1; + var thisParameter = undefined; + var hasThisParameter = void 0; + var isJSConstructSignature = ts.isJSDocConstructSignature(declaration); + // If this is a JSDoc construct signature, then skip the first parameter in the + // parameter list. The first parameter represents the return type of the construct + // signature. + for (var i = isJSConstructSignature ? 1 : 0, n = declaration.parameters.length; i < n; i++) { + var param = declaration.parameters[i]; + var paramSymbol = param.symbol; + // Include parameter symbol instead of property symbol in the signature + if (paramSymbol && !!(paramSymbol.flags & 4 /* Property */) && !ts.isBindingPattern(param.name)) { + var resolvedSymbol = resolveName(param, paramSymbol.name, 107455 /* Value */, undefined, undefined); + paramSymbol = resolvedSymbol; + } + if (i === 0 && paramSymbol.name === "this") { + hasThisParameter = true; + thisParameter = param.symbol; + } + else { + parameters.push(paramSymbol); + } + if (param.type && param.type.kind === 166 /* LiteralType */) { + hasLiteralTypes = true; + } + if (param.initializer || param.questionToken || param.dotDotDotToken || isJSDocOptionalParameter(param)) { + if (minArgumentCount < 0) { + minArgumentCount = i - (hasThisParameter ? 1 : 0); + } + } + else { + // If we see any required parameters, it means the prior ones were not in fact optional. + minArgumentCount = -1; + } } + // If only one accessor includes a this-type annotation, the other behaves as if it had the same type annotation + if ((declaration.kind === 149 /* GetAccessor */ || declaration.kind === 150 /* SetAccessor */) && + !ts.hasDynamicName(declaration) && + (!hasThisParameter || !thisParameter)) { + var otherKind = declaration.kind === 149 /* GetAccessor */ ? 150 /* SetAccessor */ : 149 /* GetAccessor */; + var other = ts.getDeclarationOfKind(declaration.symbol, otherKind); + if (other) { + thisParameter = getAnnotatedAccessorThisParameter(other); + } + } + if (minArgumentCount < 0) { + minArgumentCount = declaration.parameters.length - (hasThisParameter ? 1 : 0); + } + if (isJSConstructSignature) { + minArgumentCount--; + } + if (!thisParameter && ts.isObjectLiteralMethod(declaration)) { + thisParameter = getContextualThisParameter(declaration); + } + var classType = declaration.kind === 148 /* Constructor */ ? + getDeclaredTypeOfClassOrInterface(getMergedSymbol(declaration.parent.symbol)) + : undefined; + var typeParameters = classType ? classType.localTypeParameters : + declaration.typeParameters ? getTypeParametersFromDeclaration(declaration.typeParameters) : + getTypeParametersFromJSDocTemplate(declaration); + var returnType = getSignatureReturnTypeFromDeclaration(declaration, minArgumentCount, isJSConstructSignature, classType); + var typePredicate = declaration.type && declaration.type.kind === 154 /* TypePredicate */ ? + createTypePredicateFromTypePredicateNode(declaration.type) : + undefined; + links.resolvedSignature = createSignature(declaration, typeParameters, thisParameter, parameters, returnType, typePredicate, minArgumentCount, ts.hasRestParameter(declaration), hasLiteralTypes); } - if (signature.typePredicate) { - freshTypePredicate = cloneTypePredicate(signature.typePredicate, mapper); - } - var result = createSignature(signature.declaration, freshTypeParameters, signature.thisParameter && instantiateSymbol(signature.thisParameter, mapper), instantiateList(signature.parameters, mapper, instantiateSymbol), instantiateType(signature.resolvedReturnType, mapper), freshTypePredicate, signature.minArgumentCount, signature.hasRestParameter, signature.hasLiteralTypes); - result.target = signature; - result.mapper = mapper; - return result; + return links.resolvedSignature; } - function instantiateSymbol(symbol, mapper) { - if (symbol.flags & 16777216 /* Instantiated */) { - var links = getSymbolLinks(symbol); - // If symbol being instantiated is itself a instantiation, fetch the original target and combine the - // type mappers. This ensures that original type identities are properly preserved and that aliases - // always reference a non-aliases. - symbol = links.target; - mapper = combineTypeMappers(links.mapper, mapper); + function getSignatureReturnTypeFromDeclaration(declaration, minArgumentCount, isJSConstructSignature, classType) { + if (isJSConstructSignature) { + return getTypeFromTypeNode(declaration.parameters[0].type); } - // Keep the flags from the symbol we're instantiating. Mark that is instantiated, and - // also transient so that we can just store data on it directly. - var result = createSymbol(16777216 /* Instantiated */ | 67108864 /* Transient */ | symbol.flags, symbol.name); - result.declarations = symbol.declarations; - result.parent = symbol.parent; - result.target = symbol; - result.mapper = mapper; - if (symbol.valueDeclaration) { - result.valueDeclaration = symbol.valueDeclaration; + else if (classType) { + return classType; } - return result; - } - function instantiateAnonymousType(type, mapper) { - if (mapper.instantiations) { - var cachedType = mapper.instantiations[type.id]; - if (cachedType) { - return cachedType; + else if (declaration.type) { + return getTypeFromTypeNode(declaration.type); + } + if (declaration.flags & 1048576 /* JavaScriptFile */) { + var type = getReturnTypeFromJSDocComment(declaration); + if (type && type !== unknownType) { + return type; } } - else { - mapper.instantiations = []; + // TypeScript 1.0 spec (April 2014): + // If only one accessor includes a type annotation, the other behaves as if it had the same type annotation. + if (declaration.kind === 149 /* GetAccessor */ && !ts.hasDynamicName(declaration)) { + var setter = ts.getDeclarationOfKind(declaration.symbol, 150 /* SetAccessor */); + return getAnnotatedAccessorType(setter); + } + if (ts.nodeIsMissing(declaration.body)) { + return anyType; } - // Mark the anonymous type as instantiated such that our infinite instantiation detection logic can recognize it - var result = createObjectType(2097152 /* Anonymous */ | 4194304 /* Instantiated */, type.symbol); - result.target = type; - result.mapper = mapper; - result.aliasSymbol = type.aliasSymbol; - result.aliasTypeArguments = mapper.targetTypes; - mapper.instantiations[type.id] = result; - return result; } - function isSymbolInScopeOfMappedTypeParameter(symbol, mapper) { - var mappedTypes = mapper.mappedTypes; - // Starting with the parent of the symbol's declaration, check if the mapper maps any of - // the type parameters introduced by enclosing declarations. We just pick the first - // declaration since multiple declarations will all have the same parent anyway. - var node = symbol.declarations[0].parent; - while (node) { + function getSignaturesOfSymbol(symbol) { + if (!symbol) + return emptyArray; + var result = []; + for (var i = 0, len = symbol.declarations.length; i < len; i++) { + var node = symbol.declarations[i]; switch (node.kind) { case 156 /* FunctionType */: case 157 /* ConstructorType */: @@ -26932,13734 +27068,14818 @@ var ts; case 150 /* SetAccessor */: case 179 /* FunctionExpression */: case 180 /* ArrowFunction */: - case 221 /* ClassDeclaration */: - case 192 /* ClassExpression */: - case 222 /* InterfaceDeclaration */: - case 223 /* TypeAliasDeclaration */: - var declaration = node; - if (declaration.typeParameters) { - for (var _i = 0, _a = declaration.typeParameters; _i < _a.length; _i++) { - var d = _a[_i]; - if (ts.contains(mappedTypes, getDeclaredTypeOfTypeParameter(getSymbolOfNode(d)))) { - return true; - } - } - } - if (ts.isClassLike(node) || node.kind === 222 /* InterfaceDeclaration */) { - var thisType = getDeclaredTypeOfClassOrInterface(getSymbolOfNode(node)).thisType; - if (thisType && ts.contains(mappedTypes, thisType)) { - return true; + case 269 /* JSDocFunctionType */: + // Don't include signature if node is the implementation of an overloaded function. A node is considered + // an implementation node if it has a body and the previous node is of the same kind and immediately + // precedes the implementation node (i.e. has the same parent and ends where the implementation starts). + if (i > 0 && node.body) { + var previous = symbol.declarations[i - 1]; + if (node.parent === previous.parent && node.kind === previous.kind && node.pos === previous.end) { + break; } } - break; - case 225 /* ModuleDeclaration */: - case 256 /* SourceFile */: - return false; + result.push(getSignatureFromDeclaration(node)); } - node = node.parent; } - return false; + return result; } - function instantiateType(type, mapper) { - if (type && mapper !== identityMapper) { - if (type.flags & 16384 /* TypeParameter */) { - return mapper(type); - } - if (type.flags & 2097152 /* Anonymous */) { - // If the anonymous type originates in a declaration of a function, method, class, or - // interface, in an object type literal, or in an object literal expression, we may need - // to instantiate the type because it might reference a type parameter. We skip instantiation - // if none of the type parameters that are in scope in the type's declaration are mapped by - // the given mapper, however we can only do that analysis if the type isn't itself an - // instantiation. - return type.symbol && - type.symbol.flags & (16 /* Function */ | 8192 /* Method */ | 32 /* Class */ | 2048 /* TypeLiteral */ | 4096 /* ObjectLiteral */) && - (type.flags & 4194304 /* Instantiated */ || isSymbolInScopeOfMappedTypeParameter(type.symbol, mapper)) ? - instantiateAnonymousType(type, mapper) : type; - } - if (type.flags & 131072 /* Reference */) { - return createTypeReference(type.target, instantiateList(type.typeArguments, mapper, instantiateType)); - } - if (type.flags & 524288 /* Union */ && !(type.flags & 8190 /* Primitive */)) { - return getUnionType(instantiateList(type.types, mapper, instantiateType), /*subtypeReduction*/ false, type.aliasSymbol, mapper.targetTypes); - } - if (type.flags & 1048576 /* Intersection */) { - return getIntersectionType(instantiateList(type.types, mapper, instantiateType), type.aliasSymbol, mapper.targetTypes); + function resolveExternalModuleTypeByLiteral(name) { + var moduleSym = resolveExternalModuleName(name, name); + if (moduleSym) { + var resolvedModuleSymbol = resolveExternalModuleSymbol(moduleSym); + if (resolvedModuleSymbol) { + return getTypeOfSymbol(resolvedModuleSymbol); } } - return type; - } - function instantiateIndexInfo(info, mapper) { - return info && createIndexInfo(instantiateType(info.type, mapper), info.isReadonly, info.declaration); + return anyType; } - // Returns true if the given expression contains (at any level of nesting) a function or arrow expression - // that is subject to contextual typing. - function isContextSensitive(node) { - ts.Debug.assert(node.kind !== 147 /* MethodDeclaration */ || ts.isObjectLiteralMethod(node)); - switch (node.kind) { - case 179 /* FunctionExpression */: - case 180 /* ArrowFunction */: - return isContextSensitiveFunctionLikeDeclaration(node); - case 171 /* ObjectLiteralExpression */: - return ts.forEach(node.properties, isContextSensitive); - case 170 /* ArrayLiteralExpression */: - return ts.forEach(node.elements, isContextSensitive); - case 188 /* ConditionalExpression */: - return isContextSensitive(node.whenTrue) || - isContextSensitive(node.whenFalse); - case 187 /* BinaryExpression */: - return node.operatorToken.kind === 52 /* BarBarToken */ && - (isContextSensitive(node.left) || isContextSensitive(node.right)); - case 253 /* PropertyAssignment */: - return isContextSensitive(node.initializer); - case 147 /* MethodDeclaration */: - case 146 /* MethodSignature */: - return isContextSensitiveFunctionLikeDeclaration(node); - case 178 /* ParenthesizedExpression */: - return isContextSensitive(node.expression); + function getThisTypeOfSignature(signature) { + if (signature.thisParameter) { + return getTypeOfSymbol(signature.thisParameter); } - return false; - } - function isContextSensitiveFunctionLikeDeclaration(node) { - var areAllParametersUntyped = !ts.forEach(node.parameters, function (p) { return p.type; }); - var isNullaryArrow = node.kind === 180 /* ArrowFunction */ && !node.parameters.length; - return !node.typeParameters && areAllParametersUntyped && !isNullaryArrow; - } - function isContextSensitiveFunctionOrObjectLiteralMethod(func) { - return (isFunctionExpressionOrArrowFunction(func) || ts.isObjectLiteralMethod(func)) && isContextSensitiveFunctionLikeDeclaration(func); } - function getTypeWithoutSignatures(type) { - if (type.flags & 2588672 /* ObjectType */) { - var resolved = resolveStructuredTypeMembers(type); - if (resolved.constructSignatures.length) { - var result = createObjectType(2097152 /* Anonymous */, type.symbol); - result.members = resolved.members; - result.properties = resolved.properties; - result.callSignatures = emptyArray; - result.constructSignatures = emptyArray; - type = result; + function getReturnTypeOfSignature(signature) { + if (!signature.resolvedReturnType) { + if (!pushTypeResolution(signature, 3 /* ResolvedReturnType */)) { + return unknownType; } + var type = void 0; + if (signature.target) { + type = instantiateType(getReturnTypeOfSignature(signature.target), signature.mapper); + } + else if (signature.unionSignatures) { + type = getUnionType(ts.map(signature.unionSignatures, getReturnTypeOfSignature), /*subtypeReduction*/ true); + } + else { + type = getReturnTypeFromBody(signature.declaration); + } + if (!popTypeResolution()) { + type = anyType; + if (compilerOptions.noImplicitAny) { + var declaration = signature.declaration; + if (declaration.name) { + error(declaration.name, ts.Diagnostics._0_implicitly_has_return_type_any_because_it_does_not_have_a_return_type_annotation_and_is_referenced_directly_or_indirectly_in_one_of_its_return_expressions, ts.declarationNameToString(declaration.name)); + } + else { + error(declaration, ts.Diagnostics.Function_implicitly_has_return_type_any_because_it_does_not_have_a_return_type_annotation_and_is_referenced_directly_or_indirectly_in_one_of_its_return_expressions); + } + } + } + signature.resolvedReturnType = type; } - return type; - } - // TYPE CHECKING - function isTypeIdenticalTo(source, target) { - return isTypeRelatedTo(source, target, identityRelation); - } - function compareTypesIdentical(source, target) { - return isTypeRelatedTo(source, target, identityRelation) ? -1 /* True */ : 0 /* False */; - } - function compareTypesAssignable(source, target) { - return isTypeRelatedTo(source, target, assignableRelation) ? -1 /* True */ : 0 /* False */; + return signature.resolvedReturnType; } - function isTypeSubtypeOf(source, target) { - return isTypeRelatedTo(source, target, subtypeRelation); + function getRestTypeOfSignature(signature) { + if (signature.hasRestParameter) { + var type = getTypeOfSymbol(ts.lastOrUndefined(signature.parameters)); + if (type.flags & 131072 /* Reference */ && type.target === globalArrayType) { + return type.typeArguments[0]; + } + } + return anyType; } - function isTypeAssignableTo(source, target) { - return isTypeRelatedTo(source, target, assignableRelation); + function getSignatureInstantiation(signature, typeArguments) { + return instantiateSignature(signature, createTypeMapper(signature.typeParameters, typeArguments), /*eraseTypeParameters*/ true); } - // A type S is considered to be an instance of a type T if S and T are the same type or if S is a - // subtype of T but not structurally identical to T. This specifically means that two distinct but - // structurally identical types (such as two classes) are not considered instances of each other. - function isTypeInstanceOf(source, target) { - return source === target || isTypeSubtypeOf(source, target) && !isTypeIdenticalTo(source, target); + function getErasedSignature(signature) { + if (!signature.typeParameters) + return signature; + if (!signature.erasedSignatureCache) { + signature.erasedSignatureCache = instantiateSignature(signature, createTypeEraser(signature.typeParameters), /*eraseTypeParameters*/ true); + } + return signature.erasedSignatureCache; } - /** - * This is *not* a bi-directional relationship. - * If one needs to check both directions for comparability, use a second call to this function or 'checkTypeComparableTo'. - */ - function isTypeComparableTo(source, target) { - return isTypeRelatedTo(source, target, comparableRelation); + function getOrCreateTypeFromSignature(signature) { + // There are two ways to declare a construct signature, one is by declaring a class constructor + // using the constructor keyword, and the other is declaring a bare construct signature in an + // object type literal or interface (using the new keyword). Each way of declaring a constructor + // will result in a different declaration kind. + if (!signature.isolatedSignatureType) { + var isConstructor = signature.declaration.kind === 148 /* Constructor */ || signature.declaration.kind === 152 /* ConstructSignature */; + var type = createObjectType(2097152 /* Anonymous */); + type.members = emptySymbols; + type.properties = emptyArray; + type.callSignatures = !isConstructor ? [signature] : emptyArray; + type.constructSignatures = isConstructor ? [signature] : emptyArray; + signature.isolatedSignatureType = type; + } + return signature.isolatedSignatureType; } - function areTypesComparable(type1, type2) { - return isTypeComparableTo(type1, type2) || isTypeComparableTo(type2, type1); + function getIndexSymbol(symbol) { + return symbol.members["__index"]; } - function checkTypeSubtypeOf(source, target, errorNode, headMessage, containingMessageChain) { - return checkTypeRelatedTo(source, target, subtypeRelation, errorNode, headMessage, containingMessageChain); + function getIndexDeclarationOfSymbol(symbol, kind) { + var syntaxKind = kind === 1 /* Number */ ? 130 /* NumberKeyword */ : 132 /* StringKeyword */; + var indexSymbol = getIndexSymbol(symbol); + if (indexSymbol) { + for (var _i = 0, _a = indexSymbol.declarations; _i < _a.length; _i++) { + var decl = _a[_i]; + var node = decl; + if (node.parameters.length === 1) { + var parameter = node.parameters[0]; + if (parameter && parameter.type && parameter.type.kind === syntaxKind) { + return node; + } + } + } + } + return undefined; } - function checkTypeAssignableTo(source, target, errorNode, headMessage, containingMessageChain) { - return checkTypeRelatedTo(source, target, assignableRelation, errorNode, headMessage, containingMessageChain); + function createIndexInfo(type, isReadonly, declaration) { + return { type: type, isReadonly: isReadonly, declaration: declaration }; } - /** - * This is *not* a bi-directional relationship. - * If one needs to check both directions for comparability, use a second call to this function or 'isTypeComparableTo'. - */ - function checkTypeComparableTo(source, target, errorNode, headMessage, containingMessageChain) { - return checkTypeRelatedTo(source, target, comparableRelation, errorNode, headMessage, containingMessageChain); + function getIndexInfoOfSymbol(symbol, kind) { + var declaration = getIndexDeclarationOfSymbol(symbol, kind); + if (declaration) { + return createIndexInfo(declaration.type ? getTypeFromTypeNode(declaration.type) : anyType, (ts.getModifierFlags(declaration) & 64 /* Readonly */) !== 0, declaration); + } + return undefined; } - function isSignatureAssignableTo(source, target, ignoreReturnTypes) { - return compareSignaturesRelated(source, target, ignoreReturnTypes, /*reportErrors*/ false, /*errorReporter*/ undefined, compareTypesAssignable) !== 0 /* False */; + function getConstraintDeclaration(type) { + return ts.getDeclarationOfKind(type.symbol, 141 /* TypeParameter */).constraint; } - /** - * See signatureRelatedTo, compareSignaturesIdentical - */ - function compareSignaturesRelated(source, target, ignoreReturnTypes, reportErrors, errorReporter, compareTypes) { - // TODO (drosen): De-duplicate code between related functions. - if (source === target) { - return -1 /* True */; - } - if (!target.hasRestParameter && source.minArgumentCount > target.parameters.length) { - return 0 /* False */; - } - // Spec 1.0 Section 3.8.3 & 3.8.4: - // M and N (the signatures) are instantiated using type Any as the type argument for all type parameters declared by M and N - source = getErasedSignature(source); - target = getErasedSignature(target); - var result = -1 /* True */; - var sourceThisType = getThisTypeOfSignature(source); - if (sourceThisType && sourceThisType !== voidType) { - var targetThisType = getThisTypeOfSignature(target); - if (targetThisType) { - // void sources are assignable to anything. - var related = compareTypes(sourceThisType, targetThisType, /*reportErrors*/ false) - || compareTypes(targetThisType, sourceThisType, reportErrors); - if (!related) { - if (reportErrors) { - errorReporter(ts.Diagnostics.The_this_types_of_each_signature_are_incompatible); - } - return 0 /* False */; - } - result &= related; + function hasConstraintReferenceTo(type, target) { + var checked; + while (type && type.flags & 16384 /* TypeParameter */ && !(type.isThisType) && !ts.contains(checked, type)) { + if (type === target) { + return true; } + (checked || (checked = [])).push(type); + var constraintDeclaration = getConstraintDeclaration(type); + type = constraintDeclaration && getTypeFromTypeNode(constraintDeclaration); } - var sourceMax = getNumNonRestParameters(source); - var targetMax = getNumNonRestParameters(target); - var checkCount = getNumParametersToCheckForSignatureRelatability(source, sourceMax, target, targetMax); - var sourceParams = source.parameters; - var targetParams = target.parameters; - for (var i = 0; i < checkCount; i++) { - var s = i < sourceMax ? getTypeOfParameter(sourceParams[i]) : getRestTypeOfSignature(source); - var t = i < targetMax ? getTypeOfParameter(targetParams[i]) : getRestTypeOfSignature(target); - var related = compareTypes(s, t, /*reportErrors*/ false) || compareTypes(t, s, reportErrors); - if (!related) { - if (reportErrors) { - errorReporter(ts.Diagnostics.Types_of_parameters_0_and_1_are_incompatible, sourceParams[i < sourceMax ? i : sourceMax].name, targetParams[i < targetMax ? i : targetMax].name); + return false; + } + function getConstraintOfTypeParameter(typeParameter) { + if (!typeParameter.constraint) { + if (typeParameter.target) { + var targetConstraint = getConstraintOfTypeParameter(typeParameter.target); + typeParameter.constraint = targetConstraint ? instantiateType(targetConstraint, typeParameter.mapper) : noConstraintType; + } + else { + var constraintDeclaration = getConstraintDeclaration(typeParameter); + var constraint = getTypeFromTypeNode(constraintDeclaration); + if (hasConstraintReferenceTo(constraint, typeParameter)) { + error(constraintDeclaration, ts.Diagnostics.Type_parameter_0_has_a_circular_constraint, typeToString(typeParameter)); + constraint = unknownType; } - return 0 /* False */; + typeParameter.constraint = constraint; } - result &= related; } - if (!ignoreReturnTypes) { - var targetReturnType = getReturnTypeOfSignature(target); - if (targetReturnType === voidType) { - return result; - } - var sourceReturnType = getReturnTypeOfSignature(source); - // The following block preserves behavior forbidding boolean returning functions from being assignable to type guard returning functions - if (target.typePredicate) { - if (source.typePredicate) { - result &= compareTypePredicateRelatedTo(source.typePredicate, target.typePredicate, reportErrors, errorReporter, compareTypes); + return typeParameter.constraint === noConstraintType ? undefined : typeParameter.constraint; + } + function getParentSymbolOfTypeParameter(typeParameter) { + return getSymbolOfNode(ts.getDeclarationOfKind(typeParameter.symbol, 141 /* TypeParameter */).parent); + } + function getTypeListId(types) { + var result = ""; + if (types) { + var length_3 = types.length; + var i = 0; + while (i < length_3) { + var startId = types[i].id; + var count = 1; + while (i + count < length_3 && types[i + count].id === startId + count) { + count++; } - else if (ts.isIdentifierTypePredicate(target.typePredicate)) { - if (reportErrors) { - errorReporter(ts.Diagnostics.Signature_0_must_have_a_type_predicate, signatureToString(source)); - } - return 0 /* False */; + if (result.length) { + result += ","; } - } - else { - result &= compareTypes(sourceReturnType, targetReturnType, reportErrors); + result += startId; + if (count > 1) { + result += ":" + count; + } + i += count; } } return result; } - function compareTypePredicateRelatedTo(source, target, reportErrors, errorReporter, compareTypes) { - if (source.kind !== target.kind) { - if (reportErrors) { - errorReporter(ts.Diagnostics.A_this_based_type_guard_is_not_compatible_with_a_parameter_based_type_guard); - errorReporter(ts.Diagnostics.Type_predicate_0_is_not_assignable_to_1, typePredicateToString(source), typePredicateToString(target)); - } - return 0 /* False */; - } - if (source.kind === 1 /* Identifier */) { - var sourceIdentifierPredicate = source; - var targetIdentifierPredicate = target; - if (sourceIdentifierPredicate.parameterIndex !== targetIdentifierPredicate.parameterIndex) { - if (reportErrors) { - errorReporter(ts.Diagnostics.Parameter_0_is_not_in_the_same_position_as_parameter_1, sourceIdentifierPredicate.parameterName, targetIdentifierPredicate.parameterName); - errorReporter(ts.Diagnostics.Type_predicate_0_is_not_assignable_to_1, typePredicateToString(source), typePredicateToString(target)); - } - return 0 /* False */; + // This function is used to propagate certain flags when creating new object type references and union types. + // It is only necessary to do so if a constituent type might be the undefined type, the null type, the type + // of an object literal or the anyFunctionType. This is because there are operations in the type checker + // that care about the presence of such types at arbitrary depth in a containing type. + function getPropagatingFlagsOfTypes(types, excludeKinds) { + var result = 0; + for (var _i = 0, types_3 = types; _i < types_3.length; _i++) { + var type = types_3[_i]; + if (!(type.flags & excludeKinds)) { + result |= type.flags; } } - var related = compareTypes(source.type, target.type, reportErrors); - if (related === 0 /* False */ && reportErrors) { - errorReporter(ts.Diagnostics.Type_predicate_0_is_not_assignable_to_1, typePredicateToString(source), typePredicateToString(target)); - } - return related; + return result & 234881024 /* PropagatingFlags */; } - function isImplementationCompatibleWithOverload(implementation, overload) { - var erasedSource = getErasedSignature(implementation); - var erasedTarget = getErasedSignature(overload); - // First see if the return types are compatible in either direction. - var sourceReturnType = getReturnTypeOfSignature(erasedSource); - var targetReturnType = getReturnTypeOfSignature(erasedTarget); - if (targetReturnType === voidType - || isTypeRelatedTo(targetReturnType, sourceReturnType, assignableRelation) - || isTypeRelatedTo(sourceReturnType, targetReturnType, assignableRelation)) { - return isSignatureAssignableTo(erasedSource, erasedTarget, /*ignoreReturnTypes*/ true); + function createTypeReference(target, typeArguments) { + var id = getTypeListId(typeArguments); + var type = target.instantiations[id]; + if (!type) { + var propagatedFlags = typeArguments ? getPropagatingFlagsOfTypes(typeArguments, /*excludeKinds*/ 0) : 0; + var flags = 131072 /* Reference */ | propagatedFlags; + type = target.instantiations[id] = createObjectType(flags, target.symbol); + type.target = target; + type.typeArguments = typeArguments; } - return false; + return type; } - function getNumNonRestParameters(signature) { - var numParams = signature.parameters.length; - return signature.hasRestParameter ? - numParams - 1 : - numParams; + function cloneTypeReference(source) { + var type = createObjectType(source.flags, source.symbol); + type.target = source.target; + type.typeArguments = source.typeArguments; + return type; } - function getNumParametersToCheckForSignatureRelatability(source, sourceNonRestParamCount, target, targetNonRestParamCount) { - if (source.hasRestParameter === target.hasRestParameter) { - if (source.hasRestParameter) { - // If both have rest parameters, get the max and add 1 to - // compensate for the rest parameter. - return Math.max(sourceNonRestParamCount, targetNonRestParamCount) + 1; - } - else { - return Math.min(sourceNonRestParamCount, targetNonRestParamCount); + function getTypeReferenceArity(type) { + return type.target.typeParameters ? type.target.typeParameters.length : 0; + } + // Get type from reference to class or interface + function getTypeFromClassOrInterfaceReference(node, symbol) { + var type = getDeclaredTypeOfSymbol(getMergedSymbol(symbol)); + var typeParameters = type.localTypeParameters; + if (typeParameters) { + if (!node.typeArguments || node.typeArguments.length !== typeParameters.length) { + error(node, ts.Diagnostics.Generic_type_0_requires_1_type_argument_s, typeToString(type, /*enclosingDeclaration*/ undefined, 1 /* WriteArrayAsGenericType */), typeParameters.length); + return unknownType; } + // In a type reference, the outer type parameters of the referenced class or interface are automatically + // supplied as type arguments and the type reference only specifies arguments for the local type parameters + // of the class or interface. + return createTypeReference(type, ts.concatenate(type.outerTypeParameters, ts.map(node.typeArguments, getTypeFromTypeNodeNoAlias))); } - else { - // Return the count for whichever signature doesn't have rest parameters. - return source.hasRestParameter ? - targetNonRestParamCount : - sourceNonRestParamCount; + if (node.typeArguments) { + error(node, ts.Diagnostics.Type_0_is_not_generic, typeToString(type)); + return unknownType; } + return type; } - function isEnumTypeRelatedTo(source, target, errorReporter) { - if (source === target) { - return true; + // Get type from reference to type alias. When a type alias is generic, the declared type of the type alias may include + // references to the type parameters of the alias. We replace those with the actual type arguments by instantiating the + // declared type. Instantiations are cached using the type identities of the type arguments as the key. + function getTypeFromTypeAliasReference(node, symbol) { + var type = getDeclaredTypeOfSymbol(symbol); + var links = getSymbolLinks(symbol); + var typeParameters = links.typeParameters; + if (typeParameters) { + if (!node.typeArguments || node.typeArguments.length !== typeParameters.length) { + error(node, ts.Diagnostics.Generic_type_0_requires_1_type_argument_s, symbolToString(symbol), typeParameters.length); + return unknownType; + } + var typeArguments = ts.map(node.typeArguments, getTypeFromTypeNodeNoAlias); + var id = getTypeListId(typeArguments); + return links.instantiations[id] || (links.instantiations[id] = instantiateType(type, createTypeMapper(typeParameters, typeArguments))); } - var id = source.id + "," + target.id; - if (enumRelation[id] !== undefined) { - return enumRelation[id]; + if (node.typeArguments) { + error(node, ts.Diagnostics.Type_0_is_not_generic, symbolToString(symbol)); + return unknownType; } - if (source.symbol.name !== target.symbol.name || - !(source.symbol.flags & 256 /* RegularEnum */) || !(target.symbol.flags & 256 /* RegularEnum */) || - (source.flags & 524288 /* Union */) !== (target.flags & 524288 /* Union */)) { - return enumRelation[id] = false; + return type; + } + // Get type from reference to named type that cannot be generic (enum or type parameter) + function getTypeFromNonGenericTypeReference(node, symbol) { + if (node.typeArguments) { + error(node, ts.Diagnostics.Type_0_is_not_generic, symbolToString(symbol)); + return unknownType; } - var targetEnumType = getTypeOfSymbol(target.symbol); - for (var _i = 0, _a = getPropertiesOfType(getTypeOfSymbol(source.symbol)); _i < _a.length; _i++) { - var property = _a[_i]; - if (property.flags & 8 /* EnumMember */) { - var targetProperty = getPropertyOfType(targetEnumType, property.name); - if (!targetProperty || !(targetProperty.flags & 8 /* EnumMember */)) { - if (errorReporter) { - errorReporter(ts.Diagnostics.Property_0_is_missing_in_type_1, property.name, typeToString(target, /*enclosingDeclaration*/ undefined, 128 /* UseFullyQualifiedType */)); - } - return enumRelation[id] = false; + return getDeclaredTypeOfSymbol(symbol); + } + function getTypeReferenceName(node) { + switch (node.kind) { + case 155 /* TypeReference */: + return node.typeName; + case 267 /* JSDocTypeReference */: + return node.name; + case 194 /* ExpressionWithTypeArguments */: + // We only support expressions that are simple qualified names. For other + // expressions this produces undefined. + var expr = node.expression; + if (ts.isEntityNameExpression(expr)) { + return expr; } - } } - return enumRelation[id] = true; + return undefined; } - function isSimpleTypeRelatedTo(source, target, relation, errorReporter) { - if (target.flags & 8192 /* Never */) - return false; - if (target.flags & 1 /* Any */ || source.flags & 8192 /* Never */) - return true; - if (source.flags & 34 /* StringLike */ && target.flags & 2 /* String */) - return true; - if (source.flags & 340 /* NumberLike */ && target.flags & 4 /* Number */) - return true; - if (source.flags & 136 /* BooleanLike */ && target.flags & 8 /* Boolean */) - return true; - if (source.flags & 256 /* EnumLiteral */ && target.flags & 16 /* Enum */ && source.baseType === target) - return true; - if (source.flags & 16 /* Enum */ && target.flags & 16 /* Enum */ && isEnumTypeRelatedTo(source, target, errorReporter)) - return true; - if (source.flags & 2048 /* Undefined */ && (!strictNullChecks || target.flags & (2048 /* Undefined */ | 1024 /* Void */))) - return true; - if (source.flags & 4096 /* Null */ && (!strictNullChecks || target.flags & 4096 /* Null */)) - return true; - if (relation === assignableRelation || relation === comparableRelation) { - if (source.flags & 1 /* Any */) - return true; - if ((source.flags & 4 /* Number */ | source.flags & 64 /* NumberLiteral */) && target.flags & 272 /* EnumLike */) - return true; - if (source.flags & 256 /* EnumLiteral */ && - target.flags & 256 /* EnumLiteral */ && - source.text === target.text && - isEnumTypeRelatedTo(source.baseType, target.baseType, errorReporter)) { - return true; - } - if (source.flags & 256 /* EnumLiteral */ && - target.flags & 16 /* Enum */ && - isEnumTypeRelatedTo(target, source.baseType, errorReporter)) { - return true; - } + function resolveTypeReferenceName(node, typeReferenceName) { + if (!typeReferenceName) { + return unknownSymbol; } - return false; + return resolveEntityName(typeReferenceName, 793064 /* Type */) || unknownSymbol; } - function isTypeRelatedTo(source, target, relation) { - if (source.flags & 96 /* StringOrNumberLiteral */ && source.flags & 16777216 /* FreshLiteral */) { - source = source.regularType; + function getTypeReferenceType(node, symbol) { + if (symbol === unknownSymbol) { + return unknownType; } - if (target.flags & 96 /* StringOrNumberLiteral */ && target.flags & 16777216 /* FreshLiteral */) { - target = target.regularType; + if (symbol.flags & (32 /* Class */ | 64 /* Interface */)) { + return getTypeFromClassOrInterfaceReference(node, symbol); } - if (source === target || relation !== identityRelation && isSimpleTypeRelatedTo(source, target, relation)) { - return true; + if (symbol.flags & 524288 /* TypeAlias */) { + return getTypeFromTypeAliasReference(node, symbol); } - if (source.flags & 2588672 /* ObjectType */ && target.flags & 2588672 /* ObjectType */) { - var id = relation !== identityRelation || source.id < target.id ? source.id + "," + target.id : target.id + "," + source.id; - var related = relation[id]; - if (related !== undefined) { - return related === 1 /* Succeeded */; - } + if (symbol.flags & 107455 /* Value */ && node.kind === 267 /* JSDocTypeReference */) { + // A JSDocTypeReference may have resolved to a value (as opposed to a type). In + // that case, the type of this reference is just the type of the value we resolved + // to. + return getTypeOfSymbol(symbol); } - if (source.flags & 4177920 /* StructuredOrTypeParameter */ || target.flags & 4177920 /* StructuredOrTypeParameter */) { - return checkTypeRelatedTo(source, target, relation, undefined, undefined, undefined); + return getTypeFromNonGenericTypeReference(node, symbol); + } + function getTypeFromTypeReference(node) { + var links = getNodeLinks(node); + if (!links.resolvedType) { + var symbol = void 0; + var type = void 0; + if (node.kind === 267 /* JSDocTypeReference */) { + var typeReferenceName = getTypeReferenceName(node); + symbol = resolveTypeReferenceName(node, typeReferenceName); + type = getTypeReferenceType(node, symbol); + } + else { + // We only support expressions that are simple qualified names. For other expressions this produces undefined. + var typeNameOrExpression = node.kind === 155 /* TypeReference */ + ? node.typeName + : ts.isEntityNameExpression(node.expression) + ? node.expression + : undefined; + symbol = typeNameOrExpression && resolveEntityName(typeNameOrExpression, 793064 /* Type */) || unknownSymbol; + type = symbol === unknownSymbol ? unknownType : + symbol.flags & (32 /* Class */ | 64 /* Interface */) ? getTypeFromClassOrInterfaceReference(node, symbol) : + symbol.flags & 524288 /* TypeAlias */ ? getTypeFromTypeAliasReference(node, symbol) : + getTypeFromNonGenericTypeReference(node, symbol); + } + // Cache both the resolved symbol and the resolved type. The resolved symbol is needed in when we check the + // type reference in checkTypeReferenceOrExpressionWithTypeArguments. + links.resolvedSymbol = symbol; + links.resolvedType = type; } - return false; + return links.resolvedType; } - /** - * Checks if 'source' is related to 'target' (e.g.: is a assignable to). - * @param source The left-hand-side of the relation. - * @param target The right-hand-side of the relation. - * @param relation The relation considered. One of 'identityRelation', 'subtypeRelation', 'assignableRelation', or 'comparableRelation'. - * Used as both to determine which checks are performed and as a cache of previously computed results. - * @param errorNode The suggested node upon which all errors will be reported, if defined. This may or may not be the actual node used. - * @param headMessage If the error chain should be prepended by a head message, then headMessage will be used. - * @param containingMessageChain A chain of errors to prepend any new errors found. - */ - function checkTypeRelatedTo(source, target, relation, errorNode, headMessage, containingMessageChain) { - var errorInfo; - var sourceStack; - var targetStack; - var maybeStack; - var expandingFlags; - var depth = 0; - var overflow = false; - ts.Debug.assert(relation !== identityRelation || !errorNode, "no error reporting in identity checking"); - var result = isRelatedTo(source, target, /*reportErrors*/ !!errorNode, headMessage); - if (overflow) { - error(errorNode, ts.Diagnostics.Excessive_stack_depth_comparing_types_0_and_1, typeToString(source), typeToString(target)); + function getTypeFromTypeQueryNode(node) { + var links = getNodeLinks(node); + if (!links.resolvedType) { + // TypeScript 1.0 spec (April 2014): 3.6.3 + // The expression is processed as an identifier expression (section 4.3) + // or property access expression(section 4.10), + // the widened type(section 3.9) of which becomes the result. + links.resolvedType = getWidenedType(checkExpression(node.exprName)); } - else if (errorInfo) { - if (containingMessageChain) { - errorInfo = ts.concatenateDiagnosticMessageChains(containingMessageChain, errorInfo); + return links.resolvedType; + } + function getTypeOfGlobalSymbol(symbol, arity) { + function getTypeDeclaration(symbol) { + var declarations = symbol.declarations; + for (var _i = 0, declarations_3 = declarations; _i < declarations_3.length; _i++) { + var declaration = declarations_3[_i]; + switch (declaration.kind) { + case 221 /* ClassDeclaration */: + case 222 /* InterfaceDeclaration */: + case 224 /* EnumDeclaration */: + return declaration; + } } - diagnostics.add(ts.createDiagnosticForNodeFromMessageChain(errorNode, errorInfo)); } - return result !== 0 /* False */; - function reportError(message, arg0, arg1, arg2) { - ts.Debug.assert(!!errorNode); - errorInfo = ts.chainDiagnosticMessages(errorInfo, message, arg0, arg1, arg2); + if (!symbol) { + return arity ? emptyGenericType : emptyObjectType; } - function reportRelationError(message, source, target) { - var sourceType = typeToString(source); - var targetType = typeToString(target); - if (sourceType === targetType) { - sourceType = typeToString(source, /*enclosingDeclaration*/ undefined, 128 /* UseFullyQualifiedType */); - targetType = typeToString(target, /*enclosingDeclaration*/ undefined, 128 /* UseFullyQualifiedType */); - } - if (!message) { - message = relation === comparableRelation ? - ts.Diagnostics.Type_0_is_not_comparable_to_type_1 : - ts.Diagnostics.Type_0_is_not_assignable_to_type_1; - } - reportError(message, sourceType, targetType); + var type = getDeclaredTypeOfSymbol(symbol); + if (!(type.flags & 2588672 /* ObjectType */)) { + error(getTypeDeclaration(symbol), ts.Diagnostics.Global_type_0_must_be_a_class_or_interface_type, symbol.name); + return arity ? emptyGenericType : emptyObjectType; } - function tryElaborateErrorsForPrimitivesAndObjects(source, target) { - var sourceType = typeToString(source); - var targetType = typeToString(target); - if ((globalStringType === source && stringType === target) || - (globalNumberType === source && numberType === target) || - (globalBooleanType === source && booleanType === target) || - (getGlobalESSymbolType() === source && esSymbolType === target)) { - reportError(ts.Diagnostics._0_is_a_primitive_but_1_is_a_wrapper_object_Prefer_using_0_when_possible, targetType, sourceType); - } + if ((type.typeParameters ? type.typeParameters.length : 0) !== arity) { + error(getTypeDeclaration(symbol), ts.Diagnostics.Global_type_0_must_have_1_type_parameter_s, symbol.name, arity); + return arity ? emptyGenericType : emptyObjectType; } - // Compare two types and return - // Ternary.True if they are related with no assumptions, - // Ternary.Maybe if they are related with assumptions of other relationships, or - // Ternary.False if they are not related. - function isRelatedTo(source, target, reportErrors, headMessage) { - var result; - if (source.flags & 96 /* StringOrNumberLiteral */ && source.flags & 16777216 /* FreshLiteral */) { - source = source.regularType; - } - if (target.flags & 96 /* StringOrNumberLiteral */ && target.flags & 16777216 /* FreshLiteral */) { - target = target.regularType; - } - // both types are the same - covers 'they are the same primitive type or both are Any' or the same type parameter cases - if (source === target) - return -1 /* True */; - if (relation === identityRelation) { - return isIdenticalTo(source, target); - } - if (isSimpleTypeRelatedTo(source, target, relation, reportErrors ? reportError : undefined)) - return -1 /* True */; - if (source.flags & 8388608 /* ObjectLiteral */ && source.flags & 16777216 /* FreshLiteral */) { - if (hasExcessProperties(source, target, reportErrors)) { - if (reportErrors) { - reportRelationError(headMessage, source, target); - } - return 0 /* False */; - } - // Above we check for excess properties with respect to the entire target type. When union - // and intersection types are further deconstructed on the target side, we don't want to - // make the check again (as it might fail for a partial target type). Therefore we obtain - // the regular source type and proceed with that. - if (target.flags & 1572864 /* UnionOrIntersection */) { - source = getRegularTypeOfObjectLiteral(source); - } - } - var saveErrorInfo = errorInfo; - // Note that these checks are specifically ordered to produce correct results. - if (source.flags & 524288 /* Union */) { - if (relation === comparableRelation) { - result = someTypeRelatedToType(source, target, reportErrors && !(source.flags & 8190 /* Primitive */)); - } - else { - result = eachTypeRelatedToType(source, target, reportErrors && !(source.flags & 8190 /* Primitive */)); - } - if (result) { - return result; - } - } - else if (target.flags & 1048576 /* Intersection */) { - result = typeRelatedToEachType(source, target, reportErrors); - if (result) { - return result; - } - } - else { - // It is necessary to try these "some" checks on both sides because there may be nested "each" checks - // on either side that need to be prioritized. For example, A | B = (A | B) & (C | D) or - // A & B = (A & B) | (C & D). - if (source.flags & 1048576 /* Intersection */) { - // Check to see if any constituents of the intersection are immediately related to the target. - // - // Don't report errors though. Checking whether a constituent is related to the source is not actually - // useful and leads to some confusing error messages. Instead it is better to let the below checks - // take care of this, or to not elaborate at all. For instance, - // - // - For an object type (such as 'C = A & B'), users are usually more interested in structural errors. - // - // - For a union type (such as '(A | B) = (C & D)'), it's better to hold onto the whole intersection - // than to report that 'D' is not assignable to 'A' or 'B'. - // - // - For a primitive type or type parameter (such as 'number = A & B') there is no point in - // breaking the intersection apart. - if (result = someTypeRelatedToType(source, target, /*reportErrors*/ false)) { - return result; - } - } - if (target.flags & 524288 /* Union */) { - if (result = typeRelatedToSomeType(source, target, reportErrors && !(source.flags & 8190 /* Primitive */) && !(target.flags & 8190 /* Primitive */))) { - return result; - } - } + return type; + } + function getGlobalValueSymbol(name) { + return getGlobalSymbol(name, 107455 /* Value */, ts.Diagnostics.Cannot_find_global_value_0); + } + function getGlobalTypeSymbol(name) { + return getGlobalSymbol(name, 793064 /* Type */, ts.Diagnostics.Cannot_find_global_type_0); + } + function getGlobalSymbol(name, meaning, diagnostic) { + return resolveName(undefined, name, meaning, diagnostic, name); + } + function getGlobalType(name, arity) { + if (arity === void 0) { arity = 0; } + return getTypeOfGlobalSymbol(getGlobalTypeSymbol(name), arity); + } + /** + * Returns a type that is inside a namespace at the global scope, e.g. + * getExportedTypeFromNamespace('JSX', 'Element') returns the JSX.Element type + */ + function getExportedTypeFromNamespace(namespace, name) { + var namespaceSymbol = getGlobalSymbol(namespace, 1920 /* Namespace */, /*diagnosticMessage*/ undefined); + var typeSymbol = namespaceSymbol && getSymbol(namespaceSymbol.exports, name, 793064 /* Type */); + return typeSymbol && getDeclaredTypeOfSymbol(typeSymbol); + } + /** + * Creates a TypeReference for a generic `TypedPropertyDescriptor`. + */ + function createTypedPropertyDescriptorType(propertyType) { + var globalTypedPropertyDescriptorType = getGlobalTypedPropertyDescriptorType(); + return globalTypedPropertyDescriptorType !== emptyGenericType + ? createTypeReference(globalTypedPropertyDescriptorType, [propertyType]) + : emptyObjectType; + } + /** + * Instantiates a global type that is generic with some element type, and returns that instantiation. + */ + function createTypeFromGenericGlobalType(genericGlobalType, typeArguments) { + return genericGlobalType !== emptyGenericType ? createTypeReference(genericGlobalType, typeArguments) : emptyObjectType; + } + function createIterableType(elementType) { + return createTypeFromGenericGlobalType(getGlobalIterableType(), [elementType]); + } + function createIterableIteratorType(elementType) { + return createTypeFromGenericGlobalType(getGlobalIterableIteratorType(), [elementType]); + } + function createArrayType(elementType) { + return createTypeFromGenericGlobalType(globalArrayType, [elementType]); + } + function getTypeFromArrayTypeNode(node) { + var links = getNodeLinks(node); + if (!links.resolvedType) { + links.resolvedType = createArrayType(getTypeFromTypeNode(node.elementType)); + } + return links.resolvedType; + } + // We represent tuple types as type references to synthesized generic interface types created by + // this function. The types are of the form: + // + // interface Tuple extends Array { 0: T0, 1: T1, 2: T2, ... } + // + // Note that the generic type created by this function has no symbol associated with it. The same + // is true for each of the synthesized type parameters. + function createTupleTypeOfArity(arity) { + var typeParameters = []; + var properties = []; + for (var i = 0; i < arity; i++) { + var typeParameter = createType(16384 /* TypeParameter */); + typeParameters.push(typeParameter); + var property = createSymbol(4 /* Property */ | 67108864 /* Transient */, "" + i); + property.type = typeParameter; + properties.push(property); + } + var type = createObjectType(262144 /* Tuple */ | 131072 /* Reference */); + type.typeParameters = typeParameters; + type.outerTypeParameters = undefined; + type.localTypeParameters = typeParameters; + type.instantiations = ts.createMap(); + type.instantiations[getTypeListId(type.typeParameters)] = type; + type.target = type; + type.typeArguments = type.typeParameters; + type.thisType = createType(16384 /* TypeParameter */); + type.thisType.isThisType = true; + type.thisType.constraint = type; + type.declaredProperties = properties; + type.declaredCallSignatures = emptyArray; + type.declaredConstructSignatures = emptyArray; + type.declaredStringIndexInfo = undefined; + type.declaredNumberIndexInfo = undefined; + return type; + } + function getTupleTypeOfArity(arity) { + return tupleTypes[arity] || (tupleTypes[arity] = createTupleTypeOfArity(arity)); + } + function createTupleType(elementTypes) { + return createTypeReference(getTupleTypeOfArity(elementTypes.length), elementTypes); + } + function getTypeFromTupleTypeNode(node) { + var links = getNodeLinks(node); + if (!links.resolvedType) { + links.resolvedType = createTupleType(ts.map(node.elementTypes, getTypeFromTypeNodeNoAlias)); + } + return links.resolvedType; + } + function binarySearchTypes(types, type) { + var low = 0; + var high = types.length - 1; + var typeId = type.id; + while (low <= high) { + var middle = low + ((high - low) >> 1); + var id = types[middle].id; + if (id === typeId) { + return middle; } - if (source.flags & 16384 /* TypeParameter */) { - var constraint = getConstraintOfTypeParameter(source); - if (!constraint || constraint.flags & 1 /* Any */) { - constraint = emptyObjectType; - } - // The constraint may need to be further instantiated with its 'this' type. - constraint = getTypeWithThisArgument(constraint, source); - // Report constraint errors only if the constraint is not the empty object type - var reportConstraintErrors = reportErrors && constraint !== emptyObjectType; - if (result = isRelatedTo(constraint, target, reportConstraintErrors)) { - errorInfo = saveErrorInfo; - return result; - } + else if (id > typeId) { + high = middle - 1; } else { - if (source.flags & 131072 /* Reference */ && target.flags & 131072 /* Reference */ && source.target === target.target) { - // We have type references to same target type, see if relationship holds for all type arguments - if (result = typeArgumentsRelatedTo(source, target, reportErrors)) { - return result; - } - } - // Even if relationship doesn't hold for unions, intersections, or generic type references, - // it may hold in a structural comparison. - var apparentSource = getApparentType(source); - // In a check of the form X = A & B, we will have previously checked if A relates to X or B relates - // to X. Failing both of those we want to check if the aggregation of A and B's members structurally - // relates to X. Thus, we include intersection types on the source side here. - if (apparentSource.flags & (2588672 /* ObjectType */ | 1048576 /* Intersection */) && target.flags & 2588672 /* ObjectType */) { - // Report structural errors only if we haven't reported any errors yet - var reportStructuralErrors = reportErrors && errorInfo === saveErrorInfo && !(source.flags & 8190 /* Primitive */); - if (result = objectTypeRelatedTo(apparentSource, source, target, reportStructuralErrors)) { - errorInfo = saveErrorInfo; - return result; - } - } - } - if (reportErrors) { - if (source.flags & 2588672 /* ObjectType */ && target.flags & 8190 /* Primitive */) { - tryElaborateErrorsForPrimitivesAndObjects(source, target); - } - else if (source.symbol && source.flags & 2588672 /* ObjectType */ && globalObjectType === source) { - reportError(ts.Diagnostics.The_Object_type_is_assignable_to_very_few_other_types_Did_you_mean_to_use_the_any_type_instead); - } - reportRelationError(headMessage, source, target); + low = middle + 1; } - return 0 /* False */; } - function isIdenticalTo(source, target) { - var result; - if (source.flags & 2588672 /* ObjectType */ && target.flags & 2588672 /* ObjectType */) { - if (source.flags & 131072 /* Reference */ && target.flags & 131072 /* Reference */ && source.target === target.target) { - // We have type references to same target type, see if all type arguments are identical - if (result = typeArgumentsRelatedTo(source, target, /*reportErrors*/ false)) { - return result; - } - } - return objectTypeRelatedTo(source, source, target, /*reportErrors*/ false); - } - if (source.flags & 524288 /* Union */ && target.flags & 524288 /* Union */ || - source.flags & 1048576 /* Intersection */ && target.flags & 1048576 /* Intersection */) { - if (result = eachTypeRelatedToSomeType(source, target, /*reportErrors*/ false)) { - if (result &= eachTypeRelatedToSomeType(target, source, /*reportErrors*/ false)) { - return result; - } - } - } - return 0 /* False */; + return ~low; + } + function containsType(types, type) { + return binarySearchTypes(types, type) >= 0; + } + function addTypeToUnion(typeSet, type) { + var flags = type.flags; + if (flags & 524288 /* Union */) { + addTypesToUnion(typeSet, type.types); } - // Check if a property with the given name is known anywhere in the given type. In an object type, a property - // is considered known if the object type is empty and the check is for assignability, if the object type has - // index signatures, or if the property is actually declared in the object type. In a union or intersection - // type, a property is considered known if it is known in any constituent type. - function isKnownProperty(type, name) { - if (type.flags & 2588672 /* ObjectType */) { - var resolved = resolveStructuredTypeMembers(type); - if ((relation === assignableRelation || relation === comparableRelation) && (type === globalObjectType || isEmptyObjectType(resolved)) || - resolved.stringIndexInfo || - (resolved.numberIndexInfo && isNumericLiteralName(name)) || - getPropertyOfType(type, name)) { - return true; - } - } - else if (type.flags & 1572864 /* UnionOrIntersection */) { - for (var _i = 0, _a = type.types; _i < _a.length; _i++) { - var t = _a[_i]; - if (isKnownProperty(t, name)) { - return true; - } - } - } - return false; + else if (flags & 1 /* Any */) { + typeSet.containsAny = true; } - function isEmptyObjectType(t) { - return t.properties.length === 0 && - t.callSignatures.length === 0 && - t.constructSignatures.length === 0 && - !t.stringIndexInfo && - !t.numberIndexInfo; + else if (!strictNullChecks && flags & 6144 /* Nullable */) { + if (flags & 2048 /* Undefined */) + typeSet.containsUndefined = true; + if (flags & 4096 /* Null */) + typeSet.containsNull = true; + if (!(flags & 33554432 /* ContainsWideningType */)) + typeSet.containsNonWideningType = true; } - function hasExcessProperties(source, target, reportErrors) { - if (!(target.flags & 536870912 /* ObjectLiteralPatternWithComputedProperties */) && maybeTypeOfKind(target, 2588672 /* ObjectType */)) { - for (var _i = 0, _a = getPropertiesOfObjectType(source); _i < _a.length; _i++) { - var prop = _a[_i]; - if (!isKnownProperty(target, prop.name)) { - if (reportErrors) { - // We know *exactly* where things went wrong when comparing the types. - // Use this property as the error node as this will be more helpful in - // reasoning about what went wrong. - ts.Debug.assert(!!errorNode); - errorNode = prop.valueDeclaration; - reportError(ts.Diagnostics.Object_literal_may_only_specify_known_properties_and_0_does_not_exist_in_type_1, symbolToString(prop), typeToString(target)); - } - return true; - } + else if (!(flags & 8192 /* Never */)) { + if (flags & 2 /* String */) + typeSet.containsString = true; + if (flags & 4 /* Number */) + typeSet.containsNumber = true; + if (flags & 96 /* StringOrNumberLiteral */) + typeSet.containsStringOrNumberLiteral = true; + var len = typeSet.length; + var index = len && type.id > typeSet[len - 1].id ? ~len : binarySearchTypes(typeSet, type); + if (index < 0) { + if (!(flags & 2097152 /* Anonymous */ && type.symbol && type.symbol.flags & (16 /* Function */ | 8192 /* Method */) && containsIdenticalType(typeSet, type))) { + typeSet.splice(~index, 0, type); } } - return false; } - function eachTypeRelatedToSomeType(source, target, reportErrors) { - var result = -1 /* True */; - var sourceTypes = source.types; - for (var _i = 0, sourceTypes_1 = sourceTypes; _i < sourceTypes_1.length; _i++) { - var sourceType = sourceTypes_1[_i]; - var related = typeRelatedToSomeType(sourceType, target, /*reportErrors*/ false); - if (!related) { - return 0 /* False */; - } - result &= related; - } - return result; + } + // Add the given types to the given type set. Order is preserved, duplicates are removed, + // and nested types of the given kind are flattened into the set. + function addTypesToUnion(typeSet, types) { + for (var _i = 0, types_4 = types; _i < types_4.length; _i++) { + var type = types_4[_i]; + addTypeToUnion(typeSet, type); } - function typeRelatedToSomeType(source, target, reportErrors) { - var targetTypes = target.types; - if (target.flags & 524288 /* Union */ && containsType(targetTypes, source)) { - return -1 /* True */; + } + function containsIdenticalType(types, type) { + for (var _i = 0, types_5 = types; _i < types_5.length; _i++) { + var t = types_5[_i]; + if (isTypeIdenticalTo(t, type)) { + return true; } - var len = targetTypes.length; - for (var i = 0; i < len; i++) { - var related = isRelatedTo(source, targetTypes[i], reportErrors && i === len - 1); - if (related) { - return related; - } + } + return false; + } + function isSubtypeOfAny(candidate, types) { + for (var i = 0, len = types.length; i < len; i++) { + if (candidate !== types[i] && isTypeSubtypeOf(candidate, types[i])) { + return true; } - return 0 /* False */; } - function typeRelatedToEachType(source, target, reportErrors) { - var result = -1 /* True */; - var targetTypes = target.types; - for (var _i = 0, targetTypes_1 = targetTypes; _i < targetTypes_1.length; _i++) { - var targetType = targetTypes_1[_i]; - var related = isRelatedTo(source, targetType, reportErrors); - if (!related) { - return 0 /* False */; + return false; + } + function isSetOfLiteralsFromSameEnum(types) { + var first = types[0]; + if (first.flags & 256 /* EnumLiteral */) { + var firstEnum = getParentOfSymbol(first.symbol); + for (var i = 1; i < types.length; i++) { + var other = types[i]; + if (!(other.flags & 256 /* EnumLiteral */) || (firstEnum !== getParentOfSymbol(other.symbol))) { + return false; } - result &= related; } - return result; + return true; } - function someTypeRelatedToType(source, target, reportErrors) { - var sourceTypes = source.types; - if (source.flags & 524288 /* Union */ && containsType(sourceTypes, target)) { - return -1 /* True */; - } - var len = sourceTypes.length; - for (var i = 0; i < len; i++) { - var related = isRelatedTo(sourceTypes[i], target, reportErrors && i === len - 1); - if (related) { - return related; - } - } - return 0 /* False */; - } - function eachTypeRelatedToType(source, target, reportErrors) { - var result = -1 /* True */; - var sourceTypes = source.types; - for (var _i = 0, sourceTypes_2 = sourceTypes; _i < sourceTypes_2.length; _i++) { - var sourceType = sourceTypes_2[_i]; - var related = isRelatedTo(sourceType, target, reportErrors); - if (!related) { - return 0 /* False */; - } - result &= related; - } - return result; - } - function typeArgumentsRelatedTo(source, target, reportErrors) { - var sources = source.typeArguments || emptyArray; - var targets = target.typeArguments || emptyArray; - if (sources.length !== targets.length && relation === identityRelation) { - return 0 /* False */; - } - var length = sources.length <= targets.length ? sources.length : targets.length; - var result = -1 /* True */; - for (var i = 0; i < length; i++) { - var related = isRelatedTo(sources[i], targets[i], reportErrors); - if (!related) { - return 0 /* False */; - } - result &= related; - } - return result; - } - // Determine if two object types are related by structure. First, check if the result is already available in the global cache. - // Second, check if we have already started a comparison of the given two types in which case we assume the result to be true. - // Third, check if both types are part of deeply nested chains of generic type instantiations and if so assume the types are - // equal and infinitely expanding. Fourth, if we have reached a depth of 100 nested comparisons, assume we have runaway recursion - // and issue an error. Otherwise, actually compare the structure of the two types. - function objectTypeRelatedTo(source, originalSource, target, reportErrors) { - if (overflow) { - return 0 /* False */; - } - var id = relation !== identityRelation || source.id < target.id ? source.id + "," + target.id : target.id + "," + source.id; - var related = relation[id]; - if (related !== undefined) { - if (reportErrors && related === 2 /* Failed */) { - // We are elaborating errors and the cached result is an unreported failure. Record the result as a reported - // failure and continue computing the relation such that errors get reported. - relation[id] = 3 /* FailedAndReported */; - } - else { - return related === 1 /* Succeeded */ ? -1 /* True */ : 0 /* False */; - } - } - if (depth > 0) { - for (var i = 0; i < depth; i++) { - // If source and target are already being compared, consider them related with assumptions - if (maybeStack[i][id]) { - return 1 /* Maybe */; - } - } - if (depth === 100) { - overflow = true; - return 0 /* False */; - } - } - else { - sourceStack = []; - targetStack = []; - maybeStack = []; - expandingFlags = 0; - } - sourceStack[depth] = source; - targetStack[depth] = target; - maybeStack[depth] = ts.createMap(); - maybeStack[depth][id] = 1 /* Succeeded */; - depth++; - var saveExpandingFlags = expandingFlags; - if (!(expandingFlags & 1) && isDeeplyNestedGeneric(source, sourceStack, depth)) - expandingFlags |= 1; - if (!(expandingFlags & 2) && isDeeplyNestedGeneric(target, targetStack, depth)) - expandingFlags |= 2; - var result; - if (expandingFlags === 3) { - result = 1 /* Maybe */; - } - else { - result = propertiesRelatedTo(source, target, reportErrors); - if (result) { - result &= signaturesRelatedTo(source, target, 0 /* Call */, reportErrors); - if (result) { - result &= signaturesRelatedTo(source, target, 1 /* Construct */, reportErrors); - if (result) { - result &= indexTypesRelatedTo(source, originalSource, target, 0 /* String */, reportErrors); - if (result) { - result &= indexTypesRelatedTo(source, originalSource, target, 1 /* Number */, reportErrors); - } - } - } - } - } - expandingFlags = saveExpandingFlags; - depth--; - if (result) { - var maybeCache = maybeStack[depth]; - // If result is definitely true, copy assumptions to global cache, else copy to next level up - var destinationCache = (result === -1 /* True */ || depth === 0) ? relation : maybeStack[depth - 1]; - ts.copyProperties(maybeCache, destinationCache); - } - else { - // A false result goes straight into global cache (when something is false under assumptions it - // will also be false without assumptions) - relation[id] = reportErrors ? 3 /* FailedAndReported */ : 2 /* Failed */; - } - return result; + return false; + } + function removeSubtypes(types) { + if (types.length === 0 || isSetOfLiteralsFromSameEnum(types)) { + return; } - function propertiesRelatedTo(source, target, reportErrors) { - if (relation === identityRelation) { - return propertiesIdenticalTo(source, target); - } - var result = -1 /* True */; - var properties = getPropertiesOfObjectType(target); - var requireOptionalProperties = relation === subtypeRelation && !(source.flags & 8388608 /* ObjectLiteral */); - for (var _i = 0, properties_2 = properties; _i < properties_2.length; _i++) { - var targetProp = properties_2[_i]; - var sourceProp = getPropertyOfType(source, targetProp.name); - if (sourceProp !== targetProp) { - if (!sourceProp) { - if (!(targetProp.flags & 536870912 /* Optional */) || requireOptionalProperties) { - if (reportErrors) { - reportError(ts.Diagnostics.Property_0_is_missing_in_type_1, symbolToString(targetProp), typeToString(source)); - } - return 0 /* False */; - } - } - else if (!(targetProp.flags & 134217728 /* Prototype */)) { - var sourcePropFlags = getDeclarationModifierFlagsFromSymbol(sourceProp); - var targetPropFlags = getDeclarationModifierFlagsFromSymbol(targetProp); - if (sourcePropFlags & 8 /* Private */ || targetPropFlags & 8 /* Private */) { - if (sourceProp.valueDeclaration !== targetProp.valueDeclaration) { - if (reportErrors) { - if (sourcePropFlags & 8 /* Private */ && targetPropFlags & 8 /* Private */) { - reportError(ts.Diagnostics.Types_have_separate_declarations_of_a_private_property_0, symbolToString(targetProp)); - } - else { - reportError(ts.Diagnostics.Property_0_is_private_in_type_1_but_not_in_type_2, symbolToString(targetProp), typeToString(sourcePropFlags & 8 /* Private */ ? source : target), typeToString(sourcePropFlags & 8 /* Private */ ? target : source)); - } - } - return 0 /* False */; - } - } - else if (targetPropFlags & 16 /* Protected */) { - var sourceDeclaredInClass = sourceProp.parent && sourceProp.parent.flags & 32 /* Class */; - var sourceClass = sourceDeclaredInClass ? getDeclaredTypeOfSymbol(getParentOfSymbol(sourceProp)) : undefined; - var targetClass = getDeclaredTypeOfSymbol(getParentOfSymbol(targetProp)); - if (!sourceClass || !hasBaseType(sourceClass, targetClass)) { - if (reportErrors) { - reportError(ts.Diagnostics.Property_0_is_protected_but_type_1_is_not_a_class_derived_from_2, symbolToString(targetProp), typeToString(sourceClass || source), typeToString(targetClass)); - } - return 0 /* False */; - } - } - else if (sourcePropFlags & 16 /* Protected */) { - if (reportErrors) { - reportError(ts.Diagnostics.Property_0_is_protected_in_type_1_but_public_in_type_2, symbolToString(targetProp), typeToString(source), typeToString(target)); - } - return 0 /* False */; - } - var related = isRelatedTo(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp), reportErrors); - if (!related) { - if (reportErrors) { - reportError(ts.Diagnostics.Types_of_property_0_are_incompatible, symbolToString(targetProp)); - } - return 0 /* False */; - } - result &= related; - if (sourceProp.flags & 536870912 /* Optional */ && !(targetProp.flags & 536870912 /* Optional */)) { - // TypeScript 1.0 spec (April 2014): 3.8.3 - // S is a subtype of a type T, and T is a supertype of S if ... - // S' and T are object types and, for each member M in T.. - // M is a property and S' contains a property N where - // if M is a required property, N is also a required property - // (M - property in T) - // (N - property in S) - if (reportErrors) { - reportError(ts.Diagnostics.Property_0_is_optional_in_type_1_but_required_in_type_2, symbolToString(targetProp), typeToString(source), typeToString(target)); - } - return 0 /* False */; - } - } - } + var i = types.length; + while (i > 0) { + i--; + if (isSubtypeOfAny(types[i], types)) { + ts.orderedRemoveItemAt(types, i); } - return result; } - function propertiesIdenticalTo(source, target) { - if (!(source.flags & 2588672 /* ObjectType */ && target.flags & 2588672 /* ObjectType */)) { - return 0 /* False */; - } - var sourceProperties = getPropertiesOfObjectType(source); - var targetProperties = getPropertiesOfObjectType(target); - if (sourceProperties.length !== targetProperties.length) { - return 0 /* False */; - } - var result = -1 /* True */; - for (var _i = 0, sourceProperties_1 = sourceProperties; _i < sourceProperties_1.length; _i++) { - var sourceProp = sourceProperties_1[_i]; - var targetProp = getPropertyOfObjectType(target, sourceProp.name); - if (!targetProp) { - return 0 /* False */; - } - var related = compareProperties(sourceProp, targetProp, isRelatedTo); - if (!related) { - return 0 /* False */; - } - result &= related; + } + function removeRedundantLiteralTypes(types) { + var i = types.length; + while (i > 0) { + i--; + var t = types[i]; + var remove = t.flags & 32 /* StringLiteral */ && types.containsString || + t.flags & 64 /* NumberLiteral */ && types.containsNumber || + t.flags & 96 /* StringOrNumberLiteral */ && t.flags & 16777216 /* FreshLiteral */ && containsType(types, t.regularType); + if (remove) { + ts.orderedRemoveItemAt(types, i); } - return result; } - function signaturesRelatedTo(source, target, kind, reportErrors) { - if (relation === identityRelation) { - return signaturesIdenticalTo(source, target, kind); - } - if (target === anyFunctionType || source === anyFunctionType) { - return -1 /* True */; - } - var sourceSignatures = getSignaturesOfType(source, kind); - var targetSignatures = getSignaturesOfType(target, kind); - if (kind === 1 /* Construct */ && sourceSignatures.length && targetSignatures.length) { - if (isAbstractConstructorType(source) && !isAbstractConstructorType(target)) { - // An abstract constructor type is not assignable to a non-abstract constructor type - // as it would otherwise be possible to new an abstract class. Note that the assignability - // check we perform for an extends clause excludes construct signatures from the target, - // so this check never proceeds. - if (reportErrors) { - reportError(ts.Diagnostics.Cannot_assign_an_abstract_constructor_type_to_a_non_abstract_constructor_type); - } - return 0 /* False */; - } - if (!constructorVisibilitiesAreCompatible(sourceSignatures[0], targetSignatures[0], reportErrors)) { - return 0 /* False */; - } - } - var result = -1 /* True */; - var saveErrorInfo = errorInfo; - outer: for (var _i = 0, targetSignatures_1 = targetSignatures; _i < targetSignatures_1.length; _i++) { - var t = targetSignatures_1[_i]; - // Only elaborate errors from the first failure - var shouldElaborateErrors = reportErrors; - for (var _a = 0, sourceSignatures_1 = sourceSignatures; _a < sourceSignatures_1.length; _a++) { - var s = sourceSignatures_1[_a]; - var related = signatureRelatedTo(s, t, shouldElaborateErrors); - if (related) { - result &= related; - errorInfo = saveErrorInfo; - continue outer; - } - shouldElaborateErrors = false; - } - if (shouldElaborateErrors) { - reportError(ts.Diagnostics.Type_0_provides_no_match_for_the_signature_1, typeToString(source), signatureToString(t, /*enclosingDeclaration*/ undefined, /*flags*/ undefined, kind)); - } - return 0 /* False */; - } - return result; + } + // We sort and deduplicate the constituent types based on object identity. If the subtypeReduction + // flag is specified we also reduce the constituent type set to only include types that aren't subtypes + // of other types. Subtype reduction is expensive for large union types and is possible only when union + // types are known not to circularly reference themselves (as is the case with union types created by + // expression constructs such as array literals and the || and ?: operators). Named types can + // circularly reference themselves and therefore cannot be subtype reduced during their declaration. + // For example, "type Item = string | (() => Item" is a named type that circularly references itself. + function getUnionType(types, subtypeReduction, aliasSymbol, aliasTypeArguments) { + if (types.length === 0) { + return neverType; } - /** - * See signatureAssignableTo, compareSignaturesIdentical - */ - function signatureRelatedTo(source, target, reportErrors) { - return compareSignaturesRelated(source, target, /*ignoreReturnTypes*/ false, reportErrors, reportError, isRelatedTo); + if (types.length === 1) { + return types[0]; } - function signaturesIdenticalTo(source, target, kind) { - var sourceSignatures = getSignaturesOfType(source, kind); - var targetSignatures = getSignaturesOfType(target, kind); - if (sourceSignatures.length !== targetSignatures.length) { - return 0 /* False */; - } - var result = -1 /* True */; - for (var i = 0, len = sourceSignatures.length; i < len; i++) { - var related = compareSignaturesIdentical(sourceSignatures[i], targetSignatures[i], /*partialMatch*/ false, /*ignoreThisTypes*/ false, /*ignoreReturnTypes*/ false, isRelatedTo); - if (!related) { - return 0 /* False */; - } - result &= related; - } - return result; + var typeSet = []; + addTypesToUnion(typeSet, types); + if (typeSet.containsAny) { + return anyType; } - function eachPropertyRelatedTo(source, target, kind, reportErrors) { - var result = -1 /* True */; - for (var _i = 0, _a = getPropertiesOfObjectType(source); _i < _a.length; _i++) { - var prop = _a[_i]; - if (kind === 0 /* String */ || isNumericLiteralName(prop.name)) { - var related = isRelatedTo(getTypeOfSymbol(prop), target, reportErrors); - if (!related) { - if (reportErrors) { - reportError(ts.Diagnostics.Property_0_is_incompatible_with_index_signature, symbolToString(prop)); - } - return 0 /* False */; - } - result &= related; - } - } - return result; + if (subtypeReduction) { + removeSubtypes(typeSet); } - function indexInfoRelatedTo(sourceInfo, targetInfo, reportErrors) { - var related = isRelatedTo(sourceInfo.type, targetInfo.type, reportErrors); - if (!related && reportErrors) { - reportError(ts.Diagnostics.Index_signatures_are_incompatible); - } - return related; + else if (typeSet.containsStringOrNumberLiteral) { + removeRedundantLiteralTypes(typeSet); } - function indexTypesRelatedTo(source, originalSource, target, kind, reportErrors) { - if (relation === identityRelation) { - return indexTypesIdenticalTo(source, target, kind); - } - var targetInfo = getIndexInfoOfType(target, kind); - if (!targetInfo || ((targetInfo.type.flags & 1 /* Any */) && !(originalSource.flags & 8190 /* Primitive */))) { - // Index signature of type any permits assignment from everything but primitives - return -1 /* True */; - } - var sourceInfo = getIndexInfoOfType(source, kind) || - kind === 1 /* Number */ && getIndexInfoOfType(source, 0 /* String */); - if (sourceInfo) { - return indexInfoRelatedTo(sourceInfo, targetInfo, reportErrors); - } - if (isObjectLiteralType(source)) { - var related = -1 /* True */; - if (kind === 0 /* String */) { - var sourceNumberInfo = getIndexInfoOfType(source, 1 /* Number */); - if (sourceNumberInfo) { - related = indexInfoRelatedTo(sourceNumberInfo, targetInfo, reportErrors); - } - } - if (related) { - related &= eachPropertyRelatedTo(source, targetInfo.type, kind, reportErrors); - } - return related; - } - if (reportErrors) { - reportError(ts.Diagnostics.Index_signature_is_missing_in_type_0, typeToString(source)); - } - return 0 /* False */; + if (typeSet.length === 0) { + return typeSet.containsNull ? typeSet.containsNonWideningType ? nullType : nullWideningType : + typeSet.containsUndefined ? typeSet.containsNonWideningType ? undefinedType : undefinedWideningType : + neverType; } - function indexTypesIdenticalTo(source, target, indexKind) { - var targetInfo = getIndexInfoOfType(target, indexKind); - var sourceInfo = getIndexInfoOfType(source, indexKind); - if (!sourceInfo && !targetInfo) { - return -1 /* True */; - } - if (sourceInfo && targetInfo && sourceInfo.isReadonly === targetInfo.isReadonly) { - return isRelatedTo(sourceInfo.type, targetInfo.type); - } - return 0 /* False */; + return getUnionTypeFromSortedList(typeSet, aliasSymbol, aliasTypeArguments); + } + // This function assumes the constituent type list is sorted and deduplicated. + function getUnionTypeFromSortedList(types, aliasSymbol, aliasTypeArguments) { + if (types.length === 0) { + return neverType; } - function constructorVisibilitiesAreCompatible(sourceSignature, targetSignature, reportErrors) { - if (!sourceSignature.declaration || !targetSignature.declaration) { - return true; - } - var sourceAccessibility = ts.getModifierFlags(sourceSignature.declaration) & 24 /* NonPublicAccessibilityModifier */; - var targetAccessibility = ts.getModifierFlags(targetSignature.declaration) & 24 /* NonPublicAccessibilityModifier */; - // A public, protected and private signature is assignable to a private signature. - if (targetAccessibility === 8 /* Private */) { - return true; - } - // A public and protected signature is assignable to a protected signature. - if (targetAccessibility === 16 /* Protected */ && sourceAccessibility !== 8 /* Private */) { - return true; - } - // Only a public signature is assignable to public signature. - if (targetAccessibility !== 16 /* Protected */ && !sourceAccessibility) { - return true; - } - if (reportErrors) { - reportError(ts.Diagnostics.Cannot_assign_a_0_constructor_type_to_a_1_constructor_type, visibilityToString(sourceAccessibility), visibilityToString(targetAccessibility)); - } - return false; + if (types.length === 1) { + return types[0]; } - } - // Return true if the given type is the constructor type for an abstract class - function isAbstractConstructorType(type) { - if (type.flags & 2097152 /* Anonymous */) { - var symbol = type.symbol; - if (symbol && symbol.flags & 32 /* Class */) { - var declaration = getClassLikeDeclarationOfSymbol(symbol); - if (declaration && ts.getModifierFlags(declaration) & 128 /* Abstract */) { - return true; - } - } + var id = getTypeListId(types); + var type = unionTypes[id]; + if (!type) { + var propagatedFlags = getPropagatingFlagsOfTypes(types, /*excludeKinds*/ 6144 /* Nullable */); + type = unionTypes[id] = createObjectType(524288 /* Union */ | propagatedFlags); + type.types = types; + type.aliasSymbol = aliasSymbol; + type.aliasTypeArguments = aliasTypeArguments; } - return false; + return type; } - // Return true if the given type is part of a deeply nested chain of generic instantiations. We consider this to be the case - // when structural type comparisons have been started for 10 or more instantiations of the same generic type. It is possible, - // though highly unlikely, for this test to be true in a situation where a chain of instantiations is not infinitely expanding. - // Effectively, we will generate a false positive when two types are structurally equal to at least 10 levels, but unequal at - // some level beyond that. - function isDeeplyNestedGeneric(type, stack, depth) { - // We track type references (created by createTypeReference) and instantiated types (created by instantiateType) - if (type.flags & (131072 /* Reference */ | 4194304 /* Instantiated */) && depth >= 5) { - var symbol = type.symbol; - var count = 0; - for (var i = 0; i < depth; i++) { - var t = stack[i]; - if (t.flags & (131072 /* Reference */ | 4194304 /* Instantiated */) && t.symbol === symbol) { - count++; - if (count >= 5) - return true; - } - } + function getTypeFromUnionTypeNode(node, aliasSymbol, aliasTypeArguments) { + var links = getNodeLinks(node); + if (!links.resolvedType) { + links.resolvedType = getUnionType(ts.map(node.types, getTypeFromTypeNodeNoAlias), /*subtypeReduction*/ false, aliasSymbol, aliasTypeArguments); } - return false; - } - function isPropertyIdenticalTo(sourceProp, targetProp) { - return compareProperties(sourceProp, targetProp, compareTypesIdentical) !== 0 /* False */; + return links.resolvedType; } - function compareProperties(sourceProp, targetProp, compareTypes) { - // Two members are considered identical when - // - they are public properties with identical names, optionality, and types, - // - they are private or protected properties originating in the same declaration and having identical types - if (sourceProp === targetProp) { - return -1 /* True */; - } - var sourcePropAccessibility = getDeclarationModifierFlagsFromSymbol(sourceProp) & 24 /* NonPublicAccessibilityModifier */; - var targetPropAccessibility = getDeclarationModifierFlagsFromSymbol(targetProp) & 24 /* NonPublicAccessibilityModifier */; - if (sourcePropAccessibility !== targetPropAccessibility) { - return 0 /* False */; + function addTypeToIntersection(typeSet, type) { + if (type.flags & 1048576 /* Intersection */) { + addTypesToIntersection(typeSet, type.types); } - if (sourcePropAccessibility) { - if (getTargetSymbol(sourceProp) !== getTargetSymbol(targetProp)) { - return 0 /* False */; - } + else if (type.flags & 1 /* Any */) { + typeSet.containsAny = true; } - else { - if ((sourceProp.flags & 536870912 /* Optional */) !== (targetProp.flags & 536870912 /* Optional */)) { - return 0 /* False */; - } + else if (!(type.flags & 8192 /* Never */) && (strictNullChecks || !(type.flags & 6144 /* Nullable */)) && !ts.contains(typeSet, type)) { + typeSet.push(type); } - if (isReadonlySymbol(sourceProp) !== isReadonlySymbol(targetProp)) { - return 0 /* False */; + } + // Add the given types to the given type set. Order is preserved, duplicates are removed, + // and nested types of the given kind are flattened into the set. + function addTypesToIntersection(typeSet, types) { + for (var _i = 0, types_6 = types; _i < types_6.length; _i++) { + var type = types_6[_i]; + addTypeToIntersection(typeSet, type); } - return compareTypes(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp)); } - function isMatchingSignature(source, target, partialMatch) { - // A source signature matches a target signature if the two signatures have the same number of required, - // optional, and rest parameters. - if (source.parameters.length === target.parameters.length && - source.minArgumentCount === target.minArgumentCount && - source.hasRestParameter === target.hasRestParameter) { - return true; + // We do not perform structural deduplication on intersection types. Intersection types are created only by the & + // type operator and we can't reduce those because we want to support recursive intersection types. For example, + // a type alias of the form "type List = T & { next: List }" cannot be reduced during its declaration. + // Also, unlike union types, the order of the constituent types is preserved in order that overload resolution + // for intersections of types with signatures can be deterministic. + function getIntersectionType(types, aliasSymbol, aliasTypeArguments) { + if (types.length === 0) { + return emptyObjectType; } - // A source signature partially matches a target signature if the target signature has no fewer required - // parameters and no more overall parameters than the source signature (where a signature with a rest - // parameter is always considered to have more overall parameters than one without). - var sourceRestCount = source.hasRestParameter ? 1 : 0; - var targetRestCount = target.hasRestParameter ? 1 : 0; - if (partialMatch && source.minArgumentCount <= target.minArgumentCount && (sourceRestCount > targetRestCount || - sourceRestCount === targetRestCount && source.parameters.length >= target.parameters.length)) { - return true; + var typeSet = []; + addTypesToIntersection(typeSet, types); + if (typeSet.containsAny) { + return anyType; } - return false; - } - /** - * See signatureRelatedTo, compareSignaturesIdentical - */ - function compareSignaturesIdentical(source, target, partialMatch, ignoreThisTypes, ignoreReturnTypes, compareTypes) { - // TODO (drosen): De-duplicate code between related functions. - if (source === target) { - return -1 /* True */; + if (typeSet.length === 1) { + return typeSet[0]; } - if (!(isMatchingSignature(source, target, partialMatch))) { - return 0 /* False */; + var id = getTypeListId(typeSet); + var type = intersectionTypes[id]; + if (!type) { + var propagatedFlags = getPropagatingFlagsOfTypes(typeSet, /*excludeKinds*/ 6144 /* Nullable */); + type = intersectionTypes[id] = createObjectType(1048576 /* Intersection */ | propagatedFlags); + type.types = typeSet; + type.aliasSymbol = aliasSymbol; + type.aliasTypeArguments = aliasTypeArguments; } - // Check that the two signatures have the same number of type parameters. We might consider - // also checking that any type parameter constraints match, but that would require instantiating - // the constraints with a common set of type arguments to get relatable entities in places where - // type parameters occur in the constraints. The complexity of doing that doesn't seem worthwhile, - // particularly as we're comparing erased versions of the signatures below. - if ((source.typeParameters ? source.typeParameters.length : 0) !== (target.typeParameters ? target.typeParameters.length : 0)) { - return 0 /* False */; + return type; + } + function getTypeFromIntersectionTypeNode(node, aliasSymbol, aliasTypeArguments) { + var links = getNodeLinks(node); + if (!links.resolvedType) { + links.resolvedType = getIntersectionType(ts.map(node.types, getTypeFromTypeNodeNoAlias), aliasSymbol, aliasTypeArguments); } - // Spec 1.0 Section 3.8.3 & 3.8.4: - // M and N (the signatures) are instantiated using type Any as the type argument for all type parameters declared by M and N - source = getErasedSignature(source); - target = getErasedSignature(target); - var result = -1 /* True */; - if (!ignoreThisTypes) { - var sourceThisType = getThisTypeOfSignature(source); - if (sourceThisType) { - var targetThisType = getThisTypeOfSignature(target); - if (targetThisType) { - var related = compareTypes(sourceThisType, targetThisType); - if (!related) { - return 0 /* False */; - } - result &= related; - } - } + return links.resolvedType; + } + function getTypeFromTypeLiteralOrFunctionOrConstructorTypeNode(node, aliasSymbol, aliasTypeArguments) { + var links = getNodeLinks(node); + if (!links.resolvedType) { + // Deferred resolution of members is handled by resolveObjectTypeMembers + var type = createObjectType(2097152 /* Anonymous */, node.symbol); + type.aliasSymbol = aliasSymbol; + type.aliasTypeArguments = aliasTypeArguments; + links.resolvedType = type; } - var targetLen = target.parameters.length; - for (var i = 0; i < targetLen; i++) { - var s = isRestParameterIndex(source, i) ? getRestTypeOfSignature(source) : getTypeOfParameter(source.parameters[i]); - var t = isRestParameterIndex(target, i) ? getRestTypeOfSignature(target) : getTypeOfParameter(target.parameters[i]); - var related = compareTypes(s, t); - if (!related) { - return 0 /* False */; + return links.resolvedType; + } + function createLiteralType(flags, text) { + var type = createType(flags); + type.text = text; + return type; + } + function getFreshTypeOfLiteralType(type) { + if (type.flags & 96 /* StringOrNumberLiteral */ && !(type.flags & 16777216 /* FreshLiteral */)) { + if (!type.freshType) { + var freshType = createLiteralType(type.flags | 16777216 /* FreshLiteral */, type.text); + freshType.regularType = type; + type.freshType = freshType; } - result &= related; - } - if (!ignoreReturnTypes) { - result &= compareTypes(getReturnTypeOfSignature(source), getReturnTypeOfSignature(target)); + return type.freshType; } - return result; + return type; } - function isRestParameterIndex(signature, parameterIndex) { - return signature.hasRestParameter && parameterIndex >= signature.parameters.length - 1; + function getRegularTypeOfLiteralType(type) { + return type.flags & 96 /* StringOrNumberLiteral */ && type.flags & 16777216 /* FreshLiteral */ ? type.regularType : type; } - function isSupertypeOfEach(candidate, types) { - for (var _i = 0, types_7 = types; _i < types_7.length; _i++) { - var t = types_7[_i]; - if (candidate !== t && !isTypeSubtypeOf(t, candidate)) - return false; - } - return true; + function getLiteralTypeForText(flags, text) { + var map = flags & 32 /* StringLiteral */ ? stringLiteralTypes : numericLiteralTypes; + return map[text] || (map[text] = createLiteralType(flags, text)); } - function literalTypesWithSameBaseType(types) { - var commonBaseType; - for (var _i = 0, types_8 = types; _i < types_8.length; _i++) { - var t = types_8[_i]; - var baseType = getBaseTypeOfLiteralType(t); - if (!commonBaseType) { - commonBaseType = baseType; - } - if (baseType === t || baseType !== commonBaseType) { - return false; - } + function getTypeFromLiteralTypeNode(node) { + var links = getNodeLinks(node); + if (!links.resolvedType) { + links.resolvedType = getRegularTypeOfLiteralType(checkExpression(node.literal)); } - return true; - } - // When the candidate types are all literal types with the same base type, the common - // supertype is a union of those literal types. Otherwise, the common supertype is the - // first type that is a supertype of each of the other types. - function getSupertypeOrUnion(types) { - return literalTypesWithSameBaseType(types) ? getUnionType(types) : ts.forEach(types, function (t) { return isSupertypeOfEach(t, types) ? t : undefined; }); + return links.resolvedType; } - function getCommonSupertype(types) { - if (!strictNullChecks) { - return getSupertypeOrUnion(types); + function getTypeFromJSDocVariadicType(node) { + var links = getNodeLinks(node); + if (!links.resolvedType) { + var type = getTypeFromTypeNode(node.type); + links.resolvedType = type ? createArrayType(type) : unknownType; } - var primaryTypes = ts.filter(types, function (t) { return !(t.flags & 6144 /* Nullable */); }); - if (!primaryTypes.length) { - return getUnionType(types, /*subtypeReduction*/ true); + return links.resolvedType; + } + function getTypeFromJSDocTupleType(node) { + var links = getNodeLinks(node); + if (!links.resolvedType) { + var types = ts.map(node.types, getTypeFromTypeNodeNoAlias); + links.resolvedType = createTupleType(types); } - var supertype = getSupertypeOrUnion(primaryTypes); - return supertype && includeFalsyTypes(supertype, getFalsyFlagsOfTypes(types) & 6144 /* Nullable */); + return links.resolvedType; } - function reportNoCommonSupertypeError(types, errorLocation, errorMessageChainHead) { - // The downfallType/bestSupertypeDownfallType is the first type that caused a particular candidate - // to not be the common supertype. So if it weren't for this one downfallType (and possibly others), - // the type in question could have been the common supertype. - var bestSupertype; - var bestSupertypeDownfallType; - var bestSupertypeScore = 0; - for (var i = 0; i < types.length; i++) { - var score = 0; - var downfallType = undefined; - for (var j = 0; j < types.length; j++) { - if (isTypeSubtypeOf(types[j], types[i])) { - score++; - } - else if (!downfallType) { - downfallType = types[j]; - } - } - ts.Debug.assert(!!downfallType, "If there is no common supertype, each type should have a downfallType"); - if (score > bestSupertypeScore) { - bestSupertype = types[i]; - bestSupertypeDownfallType = downfallType; - bestSupertypeScore = score; - } - // types.length - 1 is the maximum score, given that getCommonSupertype returned false - if (bestSupertypeScore === types.length - 1) { - break; + function getThisType(node) { + var container = ts.getThisContainer(node, /*includeArrowFunctions*/ false); + var parent = container && container.parent; + if (parent && (ts.isClassLike(parent) || parent.kind === 222 /* InterfaceDeclaration */)) { + if (!(ts.getModifierFlags(container) & 32 /* Static */) && + (container.kind !== 148 /* Constructor */ || ts.isNodeDescendantOf(node, container.body))) { + return getDeclaredTypeOfClassOrInterface(getSymbolOfNode(parent)).thisType; } } - // In the following errors, the {1} slot is before the {0} slot because checkTypeSubtypeOf supplies the - // subtype as the first argument to the error - checkTypeSubtypeOf(bestSupertypeDownfallType, bestSupertype, errorLocation, ts.Diagnostics.Type_argument_candidate_1_is_not_a_valid_type_argument_because_it_is_not_a_supertype_of_candidate_0, errorMessageChainHead); - } - function isArrayType(type) { - return type.flags & 131072 /* Reference */ && type.target === globalArrayType; + error(node, ts.Diagnostics.A_this_type_is_available_only_in_a_non_static_member_of_a_class_or_interface); + return unknownType; } - function isArrayLikeType(type) { - // A type is array-like if it is a reference to the global Array or global ReadonlyArray type, - // or if it is not the undefined or null type and if it is assignable to ReadonlyArray - return type.flags & 131072 /* Reference */ && (type.target === globalArrayType || type.target === globalReadonlyArrayType) || - !(type.flags & 6144 /* Nullable */) && isTypeAssignableTo(type, anyReadonlyArrayType); + function getTypeFromThisTypeNode(node) { + var links = getNodeLinks(node); + if (!links.resolvedType) { + links.resolvedType = getThisType(node); + } + return links.resolvedType; } - function isTupleLikeType(type) { - return !!getPropertyOfType(type, "0"); + function getTypeFromTypeNodeNoAlias(type) { + return getTypeFromTypeNode(type, /*aliasSymbol*/ undefined, /*aliasTypeArguments*/ undefined); } - function isUnitType(type) { - return (type.flags & (480 /* Literal */ | 2048 /* Undefined */ | 4096 /* Null */)) !== 0; + function getTypeFromTypeNode(node, aliasSymbol, aliasTypeArguments) { + switch (node.kind) { + case 117 /* AnyKeyword */: + case 258 /* JSDocAllType */: + case 259 /* JSDocUnknownType */: + return anyType; + case 132 /* StringKeyword */: + return stringType; + case 130 /* NumberKeyword */: + return numberType; + case 120 /* BooleanKeyword */: + return booleanType; + case 133 /* SymbolKeyword */: + return esSymbolType; + case 103 /* VoidKeyword */: + return voidType; + case 135 /* UndefinedKeyword */: + return undefinedType; + case 93 /* NullKeyword */: + return nullType; + case 127 /* NeverKeyword */: + return neverType; + case 283 /* JSDocNullKeyword */: + return nullType; + case 284 /* JSDocUndefinedKeyword */: + return undefinedType; + case 285 /* JSDocNeverKeyword */: + return neverType; + case 165 /* ThisType */: + case 97 /* ThisKeyword */: + return getTypeFromThisTypeNode(node); + case 166 /* LiteralType */: + return getTypeFromLiteralTypeNode(node); + case 282 /* JSDocLiteralType */: + return getTypeFromLiteralTypeNode(node.literal); + case 155 /* TypeReference */: + case 267 /* JSDocTypeReference */: + return getTypeFromTypeReference(node); + case 154 /* TypePredicate */: + return booleanType; + case 194 /* ExpressionWithTypeArguments */: + return getTypeFromTypeReference(node); + case 158 /* TypeQuery */: + return getTypeFromTypeQueryNode(node); + case 160 /* ArrayType */: + case 260 /* JSDocArrayType */: + return getTypeFromArrayTypeNode(node); + case 161 /* TupleType */: + return getTypeFromTupleTypeNode(node); + case 162 /* UnionType */: + case 261 /* JSDocUnionType */: + return getTypeFromUnionTypeNode(node, aliasSymbol, aliasTypeArguments); + case 163 /* IntersectionType */: + return getTypeFromIntersectionTypeNode(node, aliasSymbol, aliasTypeArguments); + case 164 /* ParenthesizedType */: + case 263 /* JSDocNullableType */: + case 264 /* JSDocNonNullableType */: + case 271 /* JSDocConstructorType */: + case 272 /* JSDocThisType */: + case 268 /* JSDocOptionalType */: + return getTypeFromTypeNode(node.type); + case 265 /* JSDocRecordType */: + return getTypeFromTypeNode(node.literal); + case 156 /* FunctionType */: + case 157 /* ConstructorType */: + case 159 /* TypeLiteral */: + case 281 /* JSDocTypeLiteral */: + case 269 /* JSDocFunctionType */: + return getTypeFromTypeLiteralOrFunctionOrConstructorTypeNode(node, aliasSymbol, aliasTypeArguments); + // This function assumes that an identifier or qualified name is a type expression + // Callers should first ensure this by calling isTypeNode + case 69 /* Identifier */: + case 139 /* QualifiedName */: + var symbol = getSymbolAtLocation(node); + return symbol && getDeclaredTypeOfSymbol(symbol); + case 262 /* JSDocTupleType */: + return getTypeFromJSDocTupleType(node); + case 270 /* JSDocVariadicType */: + return getTypeFromJSDocVariadicType(node); + default: + return unknownType; + } } - function isLiteralType(type) { - return type.flags & 8 /* Boolean */ ? true : - type.flags & 524288 /* Union */ ? type.flags & 16 /* Enum */ ? true : !ts.forEach(type.types, function (t) { return !isUnitType(t); }) : - isUnitType(type); + function instantiateList(items, mapper, instantiator) { + if (items && items.length) { + var result = []; + for (var _i = 0, items_1 = items; _i < items_1.length; _i++) { + var v = items_1[_i]; + result.push(instantiator(v, mapper)); + } + return result; + } + return items; } - function getBaseTypeOfLiteralType(type) { - return type.flags & 32 /* StringLiteral */ ? stringType : - type.flags & 64 /* NumberLiteral */ ? numberType : - type.flags & 128 /* BooleanLiteral */ ? booleanType : - type.flags & 256 /* EnumLiteral */ ? type.baseType : - type.flags & 524288 /* Union */ && !(type.flags & 16 /* Enum */) ? getUnionType(ts.map(type.types, getBaseTypeOfLiteralType)) : - type; + function createUnaryTypeMapper(source, target) { + return function (t) { return t === source ? target : t; }; } - function getWidenedLiteralType(type) { - return type.flags & 32 /* StringLiteral */ && type.flags & 16777216 /* FreshLiteral */ ? stringType : - type.flags & 64 /* NumberLiteral */ && type.flags & 16777216 /* FreshLiteral */ ? numberType : - type.flags & 128 /* BooleanLiteral */ ? booleanType : - type.flags & 256 /* EnumLiteral */ ? type.baseType : - type.flags & 524288 /* Union */ && !(type.flags & 16 /* Enum */) ? getUnionType(ts.map(type.types, getWidenedLiteralType)) : - type; + function createBinaryTypeMapper(source1, target1, source2, target2) { + return function (t) { return t === source1 ? target1 : t === source2 ? target2 : t; }; } - /** - * Check if a Type was written as a tuple type literal. - * Prefer using isTupleLikeType() unless the use of `elementTypes` is required. - */ - function isTupleType(type) { - return !!(type.flags & 131072 /* Reference */ && type.target.flags & 262144 /* Tuple */); + function createArrayTypeMapper(sources, targets) { + return function (t) { + for (var i = 0; i < sources.length; i++) { + if (t === sources[i]) { + return targets ? targets[i] : anyType; + } + } + return t; + }; } - function getFalsyFlagsOfTypes(types) { - var result = 0; - for (var _i = 0, types_9 = types; _i < types_9.length; _i++) { - var t = types_9[_i]; - result |= getFalsyFlags(t); - } - return result; + function createTypeMapper(sources, targets) { + var count = sources.length; + var mapper = count == 1 ? createUnaryTypeMapper(sources[0], targets ? targets[0] : anyType) : + count == 2 ? createBinaryTypeMapper(sources[0], targets ? targets[0] : anyType, sources[1], targets ? targets[1] : anyType) : + createArrayTypeMapper(sources, targets); + mapper.mappedTypes = sources; + mapper.targetTypes = targets; + return mapper; } - // Returns the String, Number, Boolean, StringLiteral, NumberLiteral, BooleanLiteral, Void, Undefined, or Null - // flags for the string, number, boolean, "", 0, false, void, undefined, or null types respectively. Returns - // no flags for all other types (including non-falsy literal types). - function getFalsyFlags(type) { - return type.flags & 524288 /* Union */ ? getFalsyFlagsOfTypes(type.types) : - type.flags & 32 /* StringLiteral */ ? type.text === "" ? 32 /* StringLiteral */ : 0 : - type.flags & 64 /* NumberLiteral */ ? type.text === "0" ? 64 /* NumberLiteral */ : 0 : - type.flags & 128 /* BooleanLiteral */ ? type === falseType ? 128 /* BooleanLiteral */ : 0 : - type.flags & 7406 /* PossiblyFalsy */; + function createTypeEraser(sources) { + return createTypeMapper(sources, undefined); } - function includeFalsyTypes(type, flags) { - if ((getFalsyFlags(type) & flags) === flags) { - return type; + function getInferenceMapper(context) { + if (!context.mapper) { + var mapper = function (t) { + var typeParameters = context.signature.typeParameters; + for (var i = 0; i < typeParameters.length; i++) { + if (t === typeParameters[i]) { + context.inferences[i].isFixed = true; + return getInferredType(context, i); + } + } + return t; + }; + mapper.mappedTypes = context.signature.typeParameters; + mapper.context = context; + context.mapper = mapper; } - var types = [type]; - if (flags & 34 /* StringLike */) - types.push(emptyStringType); - if (flags & 340 /* NumberLike */) - types.push(zeroType); - if (flags & 136 /* BooleanLike */) - types.push(falseType); - if (flags & 1024 /* Void */) - types.push(voidType); - if (flags & 2048 /* Undefined */) - types.push(undefinedType); - if (flags & 4096 /* Null */) - types.push(nullType); - return getUnionType(types, /*subtypeReduction*/ true); + return context.mapper; } - function removeDefinitelyFalsyTypes(type) { - return getFalsyFlags(type) & 7392 /* DefinitelyFalsy */ ? - filterType(type, function (t) { return !(getFalsyFlags(t) & 7392 /* DefinitelyFalsy */); }) : - type; + function identityMapper(type) { + return type; } - function getNonNullableType(type) { - return strictNullChecks ? getTypeWithFacts(type, 524288 /* NEUndefinedOrNull */) : type; + function combineTypeMappers(mapper1, mapper2) { + var mapper = function (t) { return instantiateType(mapper1(t), mapper2); }; + mapper.mappedTypes = mapper1.mappedTypes; + return mapper; } - /** - * Return true if type was inferred from an object literal or written as an object type literal - * with no call or construct signatures. - */ - function isObjectLiteralType(type) { - return type.symbol && (type.symbol.flags & (4096 /* ObjectLiteral */ | 2048 /* TypeLiteral */)) !== 0 && - getSignaturesOfType(type, 0 /* Call */).length === 0 && - getSignaturesOfType(type, 1 /* Construct */).length === 0; + function cloneTypeParameter(typeParameter) { + var result = createType(16384 /* TypeParameter */); + result.symbol = typeParameter.symbol; + result.target = typeParameter; + return result; } - function createTransientSymbol(source, type) { - var symbol = createSymbol(source.flags | 67108864 /* Transient */, source.name); - symbol.declarations = source.declarations; - symbol.parent = source.parent; - symbol.type = type; - symbol.target = source; - if (source.valueDeclaration) { - symbol.valueDeclaration = source.valueDeclaration; + function cloneTypePredicate(predicate, mapper) { + if (ts.isIdentifierTypePredicate(predicate)) { + return { + kind: 1 /* Identifier */, + parameterName: predicate.parameterName, + parameterIndex: predicate.parameterIndex, + type: instantiateType(predicate.type, mapper) + }; } - return symbol; - } - function transformTypeOfMembers(type, f) { - var members = ts.createMap(); - for (var _i = 0, _a = getPropertiesOfObjectType(type); _i < _a.length; _i++) { - var property = _a[_i]; - var original = getTypeOfSymbol(property); - var updated = f(original); - members[property.name] = updated === original ? property : createTransientSymbol(property, updated); + else { + return { + kind: 0 /* This */, + type: instantiateType(predicate.type, mapper) + }; } - ; - return members; } - /** - * If the the provided object literal is subject to the excess properties check, - * create a new that is exempt. Recursively mark object literal members as exempt. - * Leave signatures alone since they are not subject to the check. - */ - function getRegularTypeOfObjectLiteral(type) { - if (!(type.flags & 8388608 /* ObjectLiteral */ && type.flags & 16777216 /* FreshLiteral */)) { - return type; + function instantiateSignature(signature, mapper, eraseTypeParameters) { + var freshTypeParameters; + var freshTypePredicate; + if (signature.typeParameters && !eraseTypeParameters) { + // First create a fresh set of type parameters, then include a mapping from the old to the + // new type parameters in the mapper function. Finally store this mapper in the new type + // parameters such that we can use it when instantiating constraints. + freshTypeParameters = ts.map(signature.typeParameters, cloneTypeParameter); + mapper = combineTypeMappers(createTypeMapper(signature.typeParameters, freshTypeParameters), mapper); + for (var _i = 0, freshTypeParameters_1 = freshTypeParameters; _i < freshTypeParameters_1.length; _i++) { + var tp = freshTypeParameters_1[_i]; + tp.mapper = mapper; + } } - var regularType = type.regularType; - if (regularType) { - return regularType; + if (signature.typePredicate) { + freshTypePredicate = cloneTypePredicate(signature.typePredicate, mapper); } - var resolved = type; - var members = transformTypeOfMembers(type, getRegularTypeOfObjectLiteral); - var regularNew = createAnonymousType(resolved.symbol, members, resolved.callSignatures, resolved.constructSignatures, resolved.stringIndexInfo, resolved.numberIndexInfo); - regularNew.flags = resolved.flags & ~16777216 /* FreshLiteral */; - type.regularType = regularNew; - return regularNew; + var result = createSignature(signature.declaration, freshTypeParameters, signature.thisParameter && instantiateSymbol(signature.thisParameter, mapper), instantiateList(signature.parameters, mapper, instantiateSymbol), instantiateType(signature.resolvedReturnType, mapper), freshTypePredicate, signature.minArgumentCount, signature.hasRestParameter, signature.hasLiteralTypes); + result.target = signature; + result.mapper = mapper; + return result; } - function getWidenedTypeOfObjectLiteral(type) { - var members = transformTypeOfMembers(type, function (prop) { - var widened = getWidenedType(prop); - return prop === widened ? prop : widened; - }); - var stringIndexInfo = getIndexInfoOfType(type, 0 /* String */); - var numberIndexInfo = getIndexInfoOfType(type, 1 /* Number */); - return createAnonymousType(type.symbol, members, emptyArray, emptyArray, stringIndexInfo && createIndexInfo(getWidenedType(stringIndexInfo.type), stringIndexInfo.isReadonly), numberIndexInfo && createIndexInfo(getWidenedType(numberIndexInfo.type), numberIndexInfo.isReadonly)); + function instantiateSymbol(symbol, mapper) { + if (symbol.flags & 16777216 /* Instantiated */) { + var links = getSymbolLinks(symbol); + // If symbol being instantiated is itself a instantiation, fetch the original target and combine the + // type mappers. This ensures that original type identities are properly preserved and that aliases + // always reference a non-aliases. + symbol = links.target; + mapper = combineTypeMappers(links.mapper, mapper); + } + // Keep the flags from the symbol we're instantiating. Mark that is instantiated, and + // also transient so that we can just store data on it directly. + var result = createSymbol(16777216 /* Instantiated */ | 67108864 /* Transient */ | symbol.flags, symbol.name); + result.declarations = symbol.declarations; + result.parent = symbol.parent; + result.target = symbol; + result.mapper = mapper; + if (symbol.valueDeclaration) { + result.valueDeclaration = symbol.valueDeclaration; + } + return result; } - function getWidenedConstituentType(type) { - return type.flags & 6144 /* Nullable */ ? type : getWidenedType(type); + function instantiateAnonymousType(type, mapper) { + if (mapper.instantiations) { + var cachedType = mapper.instantiations[type.id]; + if (cachedType) { + return cachedType; + } + } + else { + mapper.instantiations = []; + } + // Mark the anonymous type as instantiated such that our infinite instantiation detection logic can recognize it + var result = createObjectType(2097152 /* Anonymous */ | 4194304 /* Instantiated */, type.symbol); + result.target = type; + result.mapper = mapper; + result.aliasSymbol = type.aliasSymbol; + result.aliasTypeArguments = mapper.targetTypes; + mapper.instantiations[type.id] = result; + return result; } - function getWidenedType(type) { - if (type.flags & 100663296 /* RequiresWidening */) { - if (type.flags & 6144 /* Nullable */) { - return anyType; + function isSymbolInScopeOfMappedTypeParameter(symbol, mapper) { + var mappedTypes = mapper.mappedTypes; + // Starting with the parent of the symbol's declaration, check if the mapper maps any of + // the type parameters introduced by enclosing declarations. We just pick the first + // declaration since multiple declarations will all have the same parent anyway. + var node = symbol.declarations[0].parent; + while (node) { + switch (node.kind) { + case 156 /* FunctionType */: + case 157 /* ConstructorType */: + case 220 /* FunctionDeclaration */: + case 147 /* MethodDeclaration */: + case 146 /* MethodSignature */: + case 148 /* Constructor */: + case 151 /* CallSignature */: + case 152 /* ConstructSignature */: + case 153 /* IndexSignature */: + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + case 179 /* FunctionExpression */: + case 180 /* ArrowFunction */: + case 221 /* ClassDeclaration */: + case 192 /* ClassExpression */: + case 222 /* InterfaceDeclaration */: + case 223 /* TypeAliasDeclaration */: + var declaration = node; + if (declaration.typeParameters) { + for (var _i = 0, _a = declaration.typeParameters; _i < _a.length; _i++) { + var d = _a[_i]; + if (ts.contains(mappedTypes, getDeclaredTypeOfTypeParameter(getSymbolOfNode(d)))) { + return true; + } + } + } + if (ts.isClassLike(node) || node.kind === 222 /* InterfaceDeclaration */) { + var thisType = getDeclaredTypeOfClassOrInterface(getSymbolOfNode(node)).thisType; + if (thisType && ts.contains(mappedTypes, thisType)) { + return true; + } + } + break; + case 225 /* ModuleDeclaration */: + case 256 /* SourceFile */: + return false; } - if (type.flags & 8388608 /* ObjectLiteral */) { - return getWidenedTypeOfObjectLiteral(type); + node = node.parent; + } + return false; + } + function instantiateType(type, mapper) { + if (type && mapper !== identityMapper) { + if (type.flags & 16384 /* TypeParameter */) { + return mapper(type); } - if (type.flags & 524288 /* Union */) { - return getUnionType(ts.map(type.types, getWidenedConstituentType)); + if (type.flags & 2097152 /* Anonymous */) { + // If the anonymous type originates in a declaration of a function, method, class, or + // interface, in an object type literal, or in an object literal expression, we may need + // to instantiate the type because it might reference a type parameter. We skip instantiation + // if none of the type parameters that are in scope in the type's declaration are mapped by + // the given mapper, however we can only do that analysis if the type isn't itself an + // instantiation. + return type.symbol && + type.symbol.flags & (16 /* Function */ | 8192 /* Method */ | 32 /* Class */ | 2048 /* TypeLiteral */ | 4096 /* ObjectLiteral */) && + (type.flags & 4194304 /* Instantiated */ || isSymbolInScopeOfMappedTypeParameter(type.symbol, mapper)) ? + instantiateAnonymousType(type, mapper) : type; } - if (isArrayType(type) || isTupleType(type)) { - return createTypeReference(type.target, ts.map(type.typeArguments, getWidenedType)); + if (type.flags & 131072 /* Reference */) { + return createTypeReference(type.target, instantiateList(type.typeArguments, mapper, instantiateType)); + } + if (type.flags & 524288 /* Union */ && !(type.flags & 8190 /* Primitive */)) { + return getUnionType(instantiateList(type.types, mapper, instantiateType), /*subtypeReduction*/ false, type.aliasSymbol, mapper.targetTypes); + } + if (type.flags & 1048576 /* Intersection */) { + return getIntersectionType(instantiateList(type.types, mapper, instantiateType), type.aliasSymbol, mapper.targetTypes); + } + } + return type; + } + function instantiateIndexInfo(info, mapper) { + return info && createIndexInfo(instantiateType(info.type, mapper), info.isReadonly, info.declaration); + } + // Returns true if the given expression contains (at any level of nesting) a function or arrow expression + // that is subject to contextual typing. + function isContextSensitive(node) { + ts.Debug.assert(node.kind !== 147 /* MethodDeclaration */ || ts.isObjectLiteralMethod(node)); + switch (node.kind) { + case 179 /* FunctionExpression */: + case 180 /* ArrowFunction */: + return isContextSensitiveFunctionLikeDeclaration(node); + case 171 /* ObjectLiteralExpression */: + return ts.forEach(node.properties, isContextSensitive); + case 170 /* ArrayLiteralExpression */: + return ts.forEach(node.elements, isContextSensitive); + case 188 /* ConditionalExpression */: + return isContextSensitive(node.whenTrue) || + isContextSensitive(node.whenFalse); + case 187 /* BinaryExpression */: + return node.operatorToken.kind === 52 /* BarBarToken */ && + (isContextSensitive(node.left) || isContextSensitive(node.right)); + case 253 /* PropertyAssignment */: + return isContextSensitive(node.initializer); + case 147 /* MethodDeclaration */: + case 146 /* MethodSignature */: + return isContextSensitiveFunctionLikeDeclaration(node); + case 178 /* ParenthesizedExpression */: + return isContextSensitive(node.expression); + } + return false; + } + function isContextSensitiveFunctionLikeDeclaration(node) { + var areAllParametersUntyped = !ts.forEach(node.parameters, function (p) { return p.type; }); + var isNullaryArrow = node.kind === 180 /* ArrowFunction */ && !node.parameters.length; + return !node.typeParameters && areAllParametersUntyped && !isNullaryArrow; + } + function isContextSensitiveFunctionOrObjectLiteralMethod(func) { + return (isFunctionExpressionOrArrowFunction(func) || ts.isObjectLiteralMethod(func)) && isContextSensitiveFunctionLikeDeclaration(func); + } + function getTypeWithoutSignatures(type) { + if (type.flags & 2588672 /* ObjectType */) { + var resolved = resolveStructuredTypeMembers(type); + if (resolved.constructSignatures.length) { + var result = createObjectType(2097152 /* Anonymous */, type.symbol); + result.members = resolved.members; + result.properties = resolved.properties; + result.callSignatures = emptyArray; + result.constructSignatures = emptyArray; + type = result; } } return type; } + // TYPE CHECKING + function isTypeIdenticalTo(source, target) { + return isTypeRelatedTo(source, target, identityRelation); + } + function compareTypesIdentical(source, target) { + return isTypeRelatedTo(source, target, identityRelation) ? -1 /* True */ : 0 /* False */; + } + function compareTypesAssignable(source, target) { + return isTypeRelatedTo(source, target, assignableRelation) ? -1 /* True */ : 0 /* False */; + } + function isTypeSubtypeOf(source, target) { + return isTypeRelatedTo(source, target, subtypeRelation); + } + function isTypeAssignableTo(source, target) { + return isTypeRelatedTo(source, target, assignableRelation); + } + // A type S is considered to be an instance of a type T if S and T are the same type or if S is a + // subtype of T but not structurally identical to T. This specifically means that two distinct but + // structurally identical types (such as two classes) are not considered instances of each other. + function isTypeInstanceOf(source, target) { + return source === target || isTypeSubtypeOf(source, target) && !isTypeIdenticalTo(source, target); + } /** - * Reports implicit any errors that occur as a result of widening 'null' and 'undefined' - * to 'any'. A call to reportWideningErrorsInType is normally accompanied by a call to - * getWidenedType. But in some cases getWidenedType is called without reporting errors - * (type argument inference is an example). - * - * The return value indicates whether an error was in fact reported. The particular circumstances - * are on a best effort basis. Currently, if the null or undefined that causes widening is inside - * an object literal property (arbitrarily deeply), this function reports an error. If no error is - * reported, reportImplicitAnyError is a suitable fallback to report a general error. + * This is *not* a bi-directional relationship. + * If one needs to check both directions for comparability, use a second call to this function or 'checkTypeComparableTo'. */ - function reportWideningErrorsInType(type) { - var errorReported = false; - if (type.flags & 524288 /* Union */) { - for (var _i = 0, _a = type.types; _i < _a.length; _i++) { - var t = _a[_i]; - if (reportWideningErrorsInType(t)) { - errorReported = true; + function isTypeComparableTo(source, target) { + return isTypeRelatedTo(source, target, comparableRelation); + } + function areTypesComparable(type1, type2) { + return isTypeComparableTo(type1, type2) || isTypeComparableTo(type2, type1); + } + function checkTypeSubtypeOf(source, target, errorNode, headMessage, containingMessageChain) { + return checkTypeRelatedTo(source, target, subtypeRelation, errorNode, headMessage, containingMessageChain); + } + function checkTypeAssignableTo(source, target, errorNode, headMessage, containingMessageChain) { + return checkTypeRelatedTo(source, target, assignableRelation, errorNode, headMessage, containingMessageChain); + } + /** + * This is *not* a bi-directional relationship. + * If one needs to check both directions for comparability, use a second call to this function or 'isTypeComparableTo'. + */ + function checkTypeComparableTo(source, target, errorNode, headMessage, containingMessageChain) { + return checkTypeRelatedTo(source, target, comparableRelation, errorNode, headMessage, containingMessageChain); + } + function isSignatureAssignableTo(source, target, ignoreReturnTypes) { + return compareSignaturesRelated(source, target, ignoreReturnTypes, /*reportErrors*/ false, /*errorReporter*/ undefined, compareTypesAssignable) !== 0 /* False */; + } + /** + * See signatureRelatedTo, compareSignaturesIdentical + */ + function compareSignaturesRelated(source, target, ignoreReturnTypes, reportErrors, errorReporter, compareTypes) { + // TODO (drosen): De-duplicate code between related functions. + if (source === target) { + return -1 /* True */; + } + if (!target.hasRestParameter && source.minArgumentCount > target.parameters.length) { + return 0 /* False */; + } + // Spec 1.0 Section 3.8.3 & 3.8.4: + // M and N (the signatures) are instantiated using type Any as the type argument for all type parameters declared by M and N + source = getErasedSignature(source); + target = getErasedSignature(target); + var result = -1 /* True */; + var sourceThisType = getThisTypeOfSignature(source); + if (sourceThisType && sourceThisType !== voidType) { + var targetThisType = getThisTypeOfSignature(target); + if (targetThisType) { + // void sources are assignable to anything. + var related = compareTypes(sourceThisType, targetThisType, /*reportErrors*/ false) + || compareTypes(targetThisType, sourceThisType, reportErrors); + if (!related) { + if (reportErrors) { + errorReporter(ts.Diagnostics.The_this_types_of_each_signature_are_incompatible); + } + return 0 /* False */; } + result &= related; } } - if (isArrayType(type) || isTupleType(type)) { - for (var _b = 0, _c = type.typeArguments; _b < _c.length; _b++) { - var t = _c[_b]; - if (reportWideningErrorsInType(t)) { - errorReported = true; + var sourceMax = getNumNonRestParameters(source); + var targetMax = getNumNonRestParameters(target); + var checkCount = getNumParametersToCheckForSignatureRelatability(source, sourceMax, target, targetMax); + var sourceParams = source.parameters; + var targetParams = target.parameters; + for (var i = 0; i < checkCount; i++) { + var s = i < sourceMax ? getTypeOfParameter(sourceParams[i]) : getRestTypeOfSignature(source); + var t = i < targetMax ? getTypeOfParameter(targetParams[i]) : getRestTypeOfSignature(target); + var related = compareTypes(s, t, /*reportErrors*/ false) || compareTypes(t, s, reportErrors); + if (!related) { + if (reportErrors) { + errorReporter(ts.Diagnostics.Types_of_parameters_0_and_1_are_incompatible, sourceParams[i < sourceMax ? i : sourceMax].name, targetParams[i < targetMax ? i : targetMax].name); } + return 0 /* False */; } + result &= related; } - if (type.flags & 8388608 /* ObjectLiteral */) { - for (var _d = 0, _e = getPropertiesOfObjectType(type); _d < _e.length; _d++) { - var p = _e[_d]; - var t = getTypeOfSymbol(p); - if (t.flags & 33554432 /* ContainsWideningType */) { - if (!reportWideningErrorsInType(t)) { - error(p.valueDeclaration, ts.Diagnostics.Object_literal_s_property_0_implicitly_has_an_1_type, p.name, typeToString(getWidenedType(t))); + if (!ignoreReturnTypes) { + var targetReturnType = getReturnTypeOfSignature(target); + if (targetReturnType === voidType) { + return result; + } + var sourceReturnType = getReturnTypeOfSignature(source); + // The following block preserves behavior forbidding boolean returning functions from being assignable to type guard returning functions + if (target.typePredicate) { + if (source.typePredicate) { + result &= compareTypePredicateRelatedTo(source.typePredicate, target.typePredicate, reportErrors, errorReporter, compareTypes); + } + else if (ts.isIdentifierTypePredicate(target.typePredicate)) { + if (reportErrors) { + errorReporter(ts.Diagnostics.Signature_0_must_have_a_type_predicate, signatureToString(source)); } - errorReported = true; + return 0 /* False */; } } + else { + result &= compareTypes(sourceReturnType, targetReturnType, reportErrors); + } } - return errorReported; + return result; } - function reportImplicitAnyError(declaration, type) { - var typeAsString = typeToString(getWidenedType(type)); - var diagnostic; - switch (declaration.kind) { - case 145 /* PropertyDeclaration */: - case 144 /* PropertySignature */: - diagnostic = ts.Diagnostics.Member_0_implicitly_has_an_1_type; - break; - case 142 /* Parameter */: - diagnostic = declaration.dotDotDotToken ? - ts.Diagnostics.Rest_parameter_0_implicitly_has_an_any_type : - ts.Diagnostics.Parameter_0_implicitly_has_an_1_type; - break; - case 169 /* BindingElement */: - diagnostic = ts.Diagnostics.Binding_element_0_implicitly_has_an_1_type; - break; - case 220 /* FunctionDeclaration */: - case 147 /* MethodDeclaration */: - case 146 /* MethodSignature */: - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - case 179 /* FunctionExpression */: - case 180 /* ArrowFunction */: - if (!declaration.name) { - error(declaration, ts.Diagnostics.Function_expression_which_lacks_return_type_annotation_implicitly_has_an_0_return_type, typeAsString); - return; - } - diagnostic = ts.Diagnostics._0_which_lacks_return_type_annotation_implicitly_has_an_1_return_type; - break; - default: - diagnostic = ts.Diagnostics.Variable_0_implicitly_has_an_1_type; + function compareTypePredicateRelatedTo(source, target, reportErrors, errorReporter, compareTypes) { + if (source.kind !== target.kind) { + if (reportErrors) { + errorReporter(ts.Diagnostics.A_this_based_type_guard_is_not_compatible_with_a_parameter_based_type_guard); + errorReporter(ts.Diagnostics.Type_predicate_0_is_not_assignable_to_1, typePredicateToString(source), typePredicateToString(target)); + } + return 0 /* False */; } - error(declaration, diagnostic, ts.declarationNameToString(declaration.name), typeAsString); - } - function reportErrorsFromWidening(declaration, type) { - if (produceDiagnostics && compilerOptions.noImplicitAny && type.flags & 33554432 /* ContainsWideningType */) { - // Report implicit any error within type if possible, otherwise report error on declaration - if (!reportWideningErrorsInType(type)) { - reportImplicitAnyError(declaration, type); + if (source.kind === 1 /* Identifier */) { + var sourceIdentifierPredicate = source; + var targetIdentifierPredicate = target; + if (sourceIdentifierPredicate.parameterIndex !== targetIdentifierPredicate.parameterIndex) { + if (reportErrors) { + errorReporter(ts.Diagnostics.Parameter_0_is_not_in_the_same_position_as_parameter_1, sourceIdentifierPredicate.parameterName, targetIdentifierPredicate.parameterName); + errorReporter(ts.Diagnostics.Type_predicate_0_is_not_assignable_to_1, typePredicateToString(source), typePredicateToString(target)); + } + return 0 /* False */; } } - } - function forEachMatchingParameterType(source, target, callback) { - var sourceMax = source.parameters.length; - var targetMax = target.parameters.length; - var count; - if (source.hasRestParameter && target.hasRestParameter) { - count = Math.max(sourceMax, targetMax); + var related = compareTypes(source.type, target.type, reportErrors); + if (related === 0 /* False */ && reportErrors) { + errorReporter(ts.Diagnostics.Type_predicate_0_is_not_assignable_to_1, typePredicateToString(source), typePredicateToString(target)); } - else if (source.hasRestParameter) { - count = targetMax; + return related; + } + function isImplementationCompatibleWithOverload(implementation, overload) { + var erasedSource = getErasedSignature(implementation); + var erasedTarget = getErasedSignature(overload); + // First see if the return types are compatible in either direction. + var sourceReturnType = getReturnTypeOfSignature(erasedSource); + var targetReturnType = getReturnTypeOfSignature(erasedTarget); + if (targetReturnType === voidType + || isTypeRelatedTo(targetReturnType, sourceReturnType, assignableRelation) + || isTypeRelatedTo(sourceReturnType, targetReturnType, assignableRelation)) { + return isSignatureAssignableTo(erasedSource, erasedTarget, /*ignoreReturnTypes*/ true); } - else if (target.hasRestParameter) { - count = sourceMax; + return false; + } + function getNumNonRestParameters(signature) { + var numParams = signature.parameters.length; + return signature.hasRestParameter ? + numParams - 1 : + numParams; + } + function getNumParametersToCheckForSignatureRelatability(source, sourceNonRestParamCount, target, targetNonRestParamCount) { + if (source.hasRestParameter === target.hasRestParameter) { + if (source.hasRestParameter) { + // If both have rest parameters, get the max and add 1 to + // compensate for the rest parameter. + return Math.max(sourceNonRestParamCount, targetNonRestParamCount) + 1; + } + else { + return Math.min(sourceNonRestParamCount, targetNonRestParamCount); + } } else { - count = Math.min(sourceMax, targetMax); - } - for (var i = 0; i < count; i++) { - callback(getTypeAtPosition(source, i), getTypeAtPosition(target, i)); + // Return the count for whichever signature doesn't have rest parameters. + return source.hasRestParameter ? + targetNonRestParamCount : + sourceNonRestParamCount; } } - function createInferenceContext(signature, inferUnionTypes) { - var inferences = ts.map(signature.typeParameters, createTypeInferencesObject); - return { - signature: signature, - inferUnionTypes: inferUnionTypes, - inferences: inferences, - inferredTypes: new Array(signature.typeParameters.length) - }; - } - function createTypeInferencesObject() { - return { - primary: undefined, - secondary: undefined, - topLevel: true, - isFixed: false - }; - } - // Return true if the given type could possibly reference a type parameter for which - // we perform type inference (i.e. a type parameter of a generic function). We cache - // results for union and intersection types for performance reasons. - function couldContainTypeParameters(type) { - return !!(type.flags & 16384 /* TypeParameter */ || - type.flags & 131072 /* Reference */ && ts.forEach(type.typeArguments, couldContainTypeParameters) || - type.flags & 2097152 /* Anonymous */ && type.symbol && type.symbol.flags & (8192 /* Method */ | 2048 /* TypeLiteral */ | 32 /* Class */) || - type.flags & 1572864 /* UnionOrIntersection */ && couldUnionOrIntersectionContainTypeParameters(type)); - } - function couldUnionOrIntersectionContainTypeParameters(type) { - if (type.couldContainTypeParameters === undefined) { - type.couldContainTypeParameters = ts.forEach(type.types, couldContainTypeParameters); + function isEnumTypeRelatedTo(source, target, errorReporter) { + if (source === target) { + return true; } - return type.couldContainTypeParameters; - } - function isTypeParameterAtTopLevel(type, typeParameter) { - return type === typeParameter || type.flags & 1572864 /* UnionOrIntersection */ && ts.forEach(type.types, function (t) { return isTypeParameterAtTopLevel(t, typeParameter); }); - } - function inferTypes(context, originalSource, originalTarget) { - var typeParameters = context.signature.typeParameters; - var sourceStack; - var targetStack; - var depth = 0; - var inferiority = 0; - var visited = ts.createMap(); - inferFromTypes(originalSource, originalTarget); - function isInProcess(source, target) { - for (var i = 0; i < depth; i++) { - if (source === sourceStack[i] && target === targetStack[i]) { - return true; - } - } - return false; + var id = source.id + "," + target.id; + if (enumRelation[id] !== undefined) { + return enumRelation[id]; } - function inferFromTypes(source, target) { - if (!couldContainTypeParameters(target)) { - return; - } - if (source.flags & 524288 /* Union */ && target.flags & 524288 /* Union */ && !(source.flags & 16 /* Enum */ && target.flags & 16 /* Enum */) || - source.flags & 1048576 /* Intersection */ && target.flags & 1048576 /* Intersection */) { - // Source and target are both unions or both intersections. If source and target - // are the same type, just relate each constituent type to itself. - if (source === target) { - for (var _i = 0, _a = source.types; _i < _a.length; _i++) { - var t = _a[_i]; - inferFromTypes(t, t); + if (source.symbol.name !== target.symbol.name || + !(source.symbol.flags & 256 /* RegularEnum */) || !(target.symbol.flags & 256 /* RegularEnum */) || + (source.flags & 524288 /* Union */) !== (target.flags & 524288 /* Union */)) { + return enumRelation[id] = false; + } + var targetEnumType = getTypeOfSymbol(target.symbol); + for (var _i = 0, _a = getPropertiesOfType(getTypeOfSymbol(source.symbol)); _i < _a.length; _i++) { + var property = _a[_i]; + if (property.flags & 8 /* EnumMember */) { + var targetProperty = getPropertyOfType(targetEnumType, property.name); + if (!targetProperty || !(targetProperty.flags & 8 /* EnumMember */)) { + if (errorReporter) { + errorReporter(ts.Diagnostics.Property_0_is_missing_in_type_1, property.name, typeToString(target, /*enclosingDeclaration*/ undefined, 128 /* UseFullyQualifiedType */)); } - return; + return enumRelation[id] = false; } - // Find each source constituent type that has an identically matching target constituent - // type, and for each such type infer from the type to itself. When inferring from a - // type to itself we effectively find all type parameter occurrences within that type - // and infer themselves as their type arguments. We have special handling for numeric - // and string literals because the number and string types are not represented as unions - // of all their possible values. - var matchingTypes = void 0; - for (var _b = 0, _c = source.types; _b < _c.length; _b++) { - var t = _c[_b]; - if (typeIdenticalToSomeType(t, target.types)) { - (matchingTypes || (matchingTypes = [])).push(t); - inferFromTypes(t, t); - } - else if (t.flags & (64 /* NumberLiteral */ | 32 /* StringLiteral */)) { - var b = getBaseTypeOfLiteralType(t); - if (typeIdenticalToSomeType(b, target.types)) { - (matchingTypes || (matchingTypes = [])).push(t, b); - } + } + } + return enumRelation[id] = true; + } + function isSimpleTypeRelatedTo(source, target, relation, errorReporter) { + if (target.flags & 8192 /* Never */) + return false; + if (target.flags & 1 /* Any */ || source.flags & 8192 /* Never */) + return true; + if (source.flags & 34 /* StringLike */ && target.flags & 2 /* String */) + return true; + if (source.flags & 340 /* NumberLike */ && target.flags & 4 /* Number */) + return true; + if (source.flags & 136 /* BooleanLike */ && target.flags & 8 /* Boolean */) + return true; + if (source.flags & 256 /* EnumLiteral */ && target.flags & 16 /* Enum */ && source.baseType === target) + return true; + if (source.flags & 16 /* Enum */ && target.flags & 16 /* Enum */ && isEnumTypeRelatedTo(source, target, errorReporter)) + return true; + if (source.flags & 2048 /* Undefined */ && (!strictNullChecks || target.flags & (2048 /* Undefined */ | 1024 /* Void */))) + return true; + if (source.flags & 4096 /* Null */ && (!strictNullChecks || target.flags & 4096 /* Null */)) + return true; + if (relation === assignableRelation || relation === comparableRelation) { + if (source.flags & 1 /* Any */) + return true; + if ((source.flags & 4 /* Number */ | source.flags & 64 /* NumberLiteral */) && target.flags & 272 /* EnumLike */) + return true; + if (source.flags & 256 /* EnumLiteral */ && + target.flags & 256 /* EnumLiteral */ && + source.text === target.text && + isEnumTypeRelatedTo(source.baseType, target.baseType, errorReporter)) { + return true; + } + if (source.flags & 256 /* EnumLiteral */ && + target.flags & 16 /* Enum */ && + isEnumTypeRelatedTo(target, source.baseType, errorReporter)) { + return true; + } + } + return false; + } + function isTypeRelatedTo(source, target, relation) { + if (source.flags & 96 /* StringOrNumberLiteral */ && source.flags & 16777216 /* FreshLiteral */) { + source = source.regularType; + } + if (target.flags & 96 /* StringOrNumberLiteral */ && target.flags & 16777216 /* FreshLiteral */) { + target = target.regularType; + } + if (source === target || relation !== identityRelation && isSimpleTypeRelatedTo(source, target, relation)) { + return true; + } + if (source.flags & 2588672 /* ObjectType */ && target.flags & 2588672 /* ObjectType */) { + var id = relation !== identityRelation || source.id < target.id ? source.id + "," + target.id : target.id + "," + source.id; + var related = relation[id]; + if (related !== undefined) { + return related === 1 /* Succeeded */; + } + } + if (source.flags & 4177920 /* StructuredOrTypeParameter */ || target.flags & 4177920 /* StructuredOrTypeParameter */) { + return checkTypeRelatedTo(source, target, relation, undefined, undefined, undefined); + } + return false; + } + /** + * Checks if 'source' is related to 'target' (e.g.: is a assignable to). + * @param source The left-hand-side of the relation. + * @param target The right-hand-side of the relation. + * @param relation The relation considered. One of 'identityRelation', 'subtypeRelation', 'assignableRelation', or 'comparableRelation'. + * Used as both to determine which checks are performed and as a cache of previously computed results. + * @param errorNode The suggested node upon which all errors will be reported, if defined. This may or may not be the actual node used. + * @param headMessage If the error chain should be prepended by a head message, then headMessage will be used. + * @param containingMessageChain A chain of errors to prepend any new errors found. + */ + function checkTypeRelatedTo(source, target, relation, errorNode, headMessage, containingMessageChain) { + var errorInfo; + var sourceStack; + var targetStack; + var maybeStack; + var expandingFlags; + var depth = 0; + var overflow = false; + ts.Debug.assert(relation !== identityRelation || !errorNode, "no error reporting in identity checking"); + var result = isRelatedTo(source, target, /*reportErrors*/ !!errorNode, headMessage); + if (overflow) { + error(errorNode, ts.Diagnostics.Excessive_stack_depth_comparing_types_0_and_1, typeToString(source), typeToString(target)); + } + else if (errorInfo) { + if (containingMessageChain) { + errorInfo = ts.concatenateDiagnosticMessageChains(containingMessageChain, errorInfo); + } + diagnostics.add(ts.createDiagnosticForNodeFromMessageChain(errorNode, errorInfo)); + } + return result !== 0 /* False */; + function reportError(message, arg0, arg1, arg2) { + ts.Debug.assert(!!errorNode); + errorInfo = ts.chainDiagnosticMessages(errorInfo, message, arg0, arg1, arg2); + } + function reportRelationError(message, source, target) { + var sourceType = typeToString(source); + var targetType = typeToString(target); + if (sourceType === targetType) { + sourceType = typeToString(source, /*enclosingDeclaration*/ undefined, 128 /* UseFullyQualifiedType */); + targetType = typeToString(target, /*enclosingDeclaration*/ undefined, 128 /* UseFullyQualifiedType */); + } + if (!message) { + message = relation === comparableRelation ? + ts.Diagnostics.Type_0_is_not_comparable_to_type_1 : + ts.Diagnostics.Type_0_is_not_assignable_to_type_1; + } + reportError(message, sourceType, targetType); + } + function tryElaborateErrorsForPrimitivesAndObjects(source, target) { + var sourceType = typeToString(source); + var targetType = typeToString(target); + if ((globalStringType === source && stringType === target) || + (globalNumberType === source && numberType === target) || + (globalBooleanType === source && booleanType === target) || + (getGlobalESSymbolType() === source && esSymbolType === target)) { + reportError(ts.Diagnostics._0_is_a_primitive_but_1_is_a_wrapper_object_Prefer_using_0_when_possible, targetType, sourceType); + } + } + // Compare two types and return + // Ternary.True if they are related with no assumptions, + // Ternary.Maybe if they are related with assumptions of other relationships, or + // Ternary.False if they are not related. + function isRelatedTo(source, target, reportErrors, headMessage) { + var result; + if (source.flags & 96 /* StringOrNumberLiteral */ && source.flags & 16777216 /* FreshLiteral */) { + source = source.regularType; + } + if (target.flags & 96 /* StringOrNumberLiteral */ && target.flags & 16777216 /* FreshLiteral */) { + target = target.regularType; + } + // both types are the same - covers 'they are the same primitive type or both are Any' or the same type parameter cases + if (source === target) + return -1 /* True */; + if (relation === identityRelation) { + return isIdenticalTo(source, target); + } + if (isSimpleTypeRelatedTo(source, target, relation, reportErrors ? reportError : undefined)) + return -1 /* True */; + if (source.flags & 8388608 /* ObjectLiteral */ && source.flags & 16777216 /* FreshLiteral */) { + if (hasExcessProperties(source, target, reportErrors)) { + if (reportErrors) { + reportRelationError(headMessage, source, target); } + return 0 /* False */; } - // Next, to improve the quality of inferences, reduce the source and target types by - // removing the identically matched constituents. For example, when inferring from - // 'string | string[]' to 'string | T' we reduce the types to 'string[]' and 'T'. - if (matchingTypes) { - source = removeTypesFromUnionOrIntersection(source, matchingTypes); - target = removeTypesFromUnionOrIntersection(target, matchingTypes); + // Above we check for excess properties with respect to the entire target type. When union + // and intersection types are further deconstructed on the target side, we don't want to + // make the check again (as it might fail for a partial target type). Therefore we obtain + // the regular source type and proceed with that. + if (target.flags & 1572864 /* UnionOrIntersection */) { + source = getRegularTypeOfObjectLiteral(source); } } - if (target.flags & 16384 /* TypeParameter */) { - // If target is a type parameter, make an inference, unless the source type contains - // the anyFunctionType (the wildcard type that's used to avoid contextually typing functions). - // Because the anyFunctionType is internal, it should not be exposed to the user by adding - // it as an inference candidate. Hopefully, a better candidate will come along that does - // not contain anyFunctionType when we come back to this argument for its second round - // of inference. - if (source.flags & 134217728 /* ContainsAnyFunctionType */) { - return; + var saveErrorInfo = errorInfo; + // Note that these checks are specifically ordered to produce correct results. + if (source.flags & 524288 /* Union */) { + if (relation === comparableRelation) { + result = someTypeRelatedToType(source, target, reportErrors && !(source.flags & 8190 /* Primitive */)); } - for (var i = 0; i < typeParameters.length; i++) { - if (target === typeParameters[i]) { - var inferences = context.inferences[i]; - if (!inferences.isFixed) { - // Any inferences that are made to a type parameter in a union type are inferior - // to inferences made to a flat (non-union) type. This is because if we infer to - // T | string[], we really don't know if we should be inferring to T or not (because - // the correct constituent on the target side could be string[]). Therefore, we put - // such inferior inferences into a secondary bucket, and only use them if the primary - // bucket is empty. - var candidates = inferiority ? - inferences.secondary || (inferences.secondary = []) : - inferences.primary || (inferences.primary = []); - if (!ts.contains(candidates, source)) { - candidates.push(source); - } - if (!isTypeParameterAtTopLevel(originalTarget, target)) { - inferences.topLevel = false; - } - } - return; - } + else { + result = eachTypeRelatedToType(source, target, reportErrors && !(source.flags & 8190 /* Primitive */)); + } + if (result) { + return result; } } - else if (source.flags & 131072 /* Reference */ && target.flags & 131072 /* Reference */ && source.target === target.target) { - // If source and target are references to the same generic type, infer from type arguments - var sourceTypes = source.typeArguments || emptyArray; - var targetTypes = target.typeArguments || emptyArray; - var count = sourceTypes.length < targetTypes.length ? sourceTypes.length : targetTypes.length; - for (var i = 0; i < count; i++) { - inferFromTypes(sourceTypes[i], targetTypes[i]); + else if (target.flags & 1048576 /* Intersection */) { + result = typeRelatedToEachType(source, target, reportErrors); + if (result) { + return result; } } - else if (target.flags & 1572864 /* UnionOrIntersection */) { - var targetTypes = target.types; - var typeParameterCount = 0; - var typeParameter = void 0; - // First infer to each type in union or intersection that isn't a type parameter - for (var _d = 0, targetTypes_2 = targetTypes; _d < targetTypes_2.length; _d++) { - var t = targetTypes_2[_d]; - if (t.flags & 16384 /* TypeParameter */ && ts.contains(typeParameters, t)) { - typeParameter = t; - typeParameterCount++; - } - else { - inferFromTypes(source, t); + else { + // It is necessary to try these "some" checks on both sides because there may be nested "each" checks + // on either side that need to be prioritized. For example, A | B = (A | B) & (C | D) or + // A & B = (A & B) | (C & D). + if (source.flags & 1048576 /* Intersection */) { + // Check to see if any constituents of the intersection are immediately related to the target. + // + // Don't report errors though. Checking whether a constituent is related to the source is not actually + // useful and leads to some confusing error messages. Instead it is better to let the below checks + // take care of this, or to not elaborate at all. For instance, + // + // - For an object type (such as 'C = A & B'), users are usually more interested in structural errors. + // + // - For a union type (such as '(A | B) = (C & D)'), it's better to hold onto the whole intersection + // than to report that 'D' is not assignable to 'A' or 'B'. + // + // - For a primitive type or type parameter (such as 'number = A & B') there is no point in + // breaking the intersection apart. + if (result = someTypeRelatedToType(source, target, /*reportErrors*/ false)) { + return result; } } - // Next, if target containings a single naked type parameter, make a secondary inference to that type - // parameter. This gives meaningful results for union types in co-variant positions and intersection - // types in contra-variant positions (such as callback parameters). - if (typeParameterCount === 1) { - inferiority++; - inferFromTypes(source, typeParameter); - inferiority--; + if (target.flags & 524288 /* Union */) { + if (result = typeRelatedToSomeType(source, target, reportErrors && !(source.flags & 8190 /* Primitive */) && !(target.flags & 8190 /* Primitive */))) { + return result; + } } } - else if (source.flags & 1572864 /* UnionOrIntersection */) { - // Source is a union or intersection type, infer from each constituent type - var sourceTypes = source.types; - for (var _e = 0, sourceTypes_3 = sourceTypes; _e < sourceTypes_3.length; _e++) { - var sourceType = sourceTypes_3[_e]; - inferFromTypes(sourceType, target); + if (source.flags & 16384 /* TypeParameter */) { + var constraint = getConstraintOfTypeParameter(source); + if (!constraint || constraint.flags & 1 /* Any */) { + constraint = emptyObjectType; + } + // The constraint may need to be further instantiated with its 'this' type. + constraint = getTypeWithThisArgument(constraint, source); + // Report constraint errors only if the constraint is not the empty object type + var reportConstraintErrors = reportErrors && constraint !== emptyObjectType; + if (result = isRelatedTo(constraint, target, reportConstraintErrors)) { + errorInfo = saveErrorInfo; + return result; } } else { - source = getApparentType(source); - if (source.flags & 2588672 /* ObjectType */) { - if (isInProcess(source, target)) { - return; + if (source.flags & 131072 /* Reference */ && target.flags & 131072 /* Reference */ && source.target === target.target) { + // We have type references to same target type, see if relationship holds for all type arguments + if (result = typeArgumentsRelatedTo(source, target, reportErrors)) { + return result; } - if (isDeeplyNestedGeneric(source, sourceStack, depth) && isDeeplyNestedGeneric(target, targetStack, depth)) { - return; + } + // Even if relationship doesn't hold for unions, intersections, or generic type references, + // it may hold in a structural comparison. + var apparentSource = getApparentType(source); + // In a check of the form X = A & B, we will have previously checked if A relates to X or B relates + // to X. Failing both of those we want to check if the aggregation of A and B's members structurally + // relates to X. Thus, we include intersection types on the source side here. + if (apparentSource.flags & (2588672 /* ObjectType */ | 1048576 /* Intersection */) && target.flags & 2588672 /* ObjectType */) { + // Report structural errors only if we haven't reported any errors yet + var reportStructuralErrors = reportErrors && errorInfo === saveErrorInfo && !(source.flags & 8190 /* Primitive */); + if (result = objectTypeRelatedTo(apparentSource, source, target, reportStructuralErrors)) { + errorInfo = saveErrorInfo; + return result; } - var key = source.id + "," + target.id; - if (visited[key]) { - return; + } + } + if (reportErrors) { + if (source.flags & 2588672 /* ObjectType */ && target.flags & 8190 /* Primitive */) { + tryElaborateErrorsForPrimitivesAndObjects(source, target); + } + else if (source.symbol && source.flags & 2588672 /* ObjectType */ && globalObjectType === source) { + reportError(ts.Diagnostics.The_Object_type_is_assignable_to_very_few_other_types_Did_you_mean_to_use_the_any_type_instead); + } + reportRelationError(headMessage, source, target); + } + return 0 /* False */; + } + function isIdenticalTo(source, target) { + var result; + if (source.flags & 2588672 /* ObjectType */ && target.flags & 2588672 /* ObjectType */) { + if (source.flags & 131072 /* Reference */ && target.flags & 131072 /* Reference */ && source.target === target.target) { + // We have type references to same target type, see if all type arguments are identical + if (result = typeArgumentsRelatedTo(source, target, /*reportErrors*/ false)) { + return result; } - visited[key] = true; - if (depth === 0) { - sourceStack = []; - targetStack = []; + } + return objectTypeRelatedTo(source, source, target, /*reportErrors*/ false); + } + if (source.flags & 524288 /* Union */ && target.flags & 524288 /* Union */ || + source.flags & 1048576 /* Intersection */ && target.flags & 1048576 /* Intersection */) { + if (result = eachTypeRelatedToSomeType(source, target, /*reportErrors*/ false)) { + if (result &= eachTypeRelatedToSomeType(target, source, /*reportErrors*/ false)) { + return result; } - sourceStack[depth] = source; - targetStack[depth] = target; - depth++; - inferFromProperties(source, target); - inferFromSignatures(source, target, 0 /* Call */); - inferFromSignatures(source, target, 1 /* Construct */); - inferFromIndexTypes(source, target); - depth--; } } + return 0 /* False */; } - function inferFromProperties(source, target) { - var properties = getPropertiesOfObjectType(target); - for (var _i = 0, properties_3 = properties; _i < properties_3.length; _i++) { - var targetProp = properties_3[_i]; - var sourceProp = getPropertyOfObjectType(source, targetProp.name); - if (sourceProp) { - inferFromTypes(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp)); + // Check if a property with the given name is known anywhere in the given type. In an object type, a property + // is considered known if the object type is empty and the check is for assignability, if the object type has + // index signatures, or if the property is actually declared in the object type. In a union or intersection + // type, a property is considered known if it is known in any constituent type. + function isKnownProperty(type, name) { + if (type.flags & 2588672 /* ObjectType */) { + var resolved = resolveStructuredTypeMembers(type); + if ((relation === assignableRelation || relation === comparableRelation) && (type === globalObjectType || isEmptyObjectType(resolved)) || + resolved.stringIndexInfo || + (resolved.numberIndexInfo && isNumericLiteralName(name)) || + getPropertyOfType(type, name)) { + return true; } } - } - function inferFromSignatures(source, target, kind) { - var sourceSignatures = getSignaturesOfType(source, kind); - var targetSignatures = getSignaturesOfType(target, kind); - var sourceLen = sourceSignatures.length; - var targetLen = targetSignatures.length; - var len = sourceLen < targetLen ? sourceLen : targetLen; - for (var i = 0; i < len; i++) { - inferFromSignature(getErasedSignature(sourceSignatures[sourceLen - len + i]), getErasedSignature(targetSignatures[targetLen - len + i])); + else if (type.flags & 1572864 /* UnionOrIntersection */) { + for (var _i = 0, _a = type.types; _i < _a.length; _i++) { + var t = _a[_i]; + if (isKnownProperty(t, name)) { + return true; + } + } } + return false; } - function inferFromParameterTypes(source, target) { - return inferFromTypes(source, target); + function isEmptyObjectType(t) { + return t.properties.length === 0 && + t.callSignatures.length === 0 && + t.constructSignatures.length === 0 && + !t.stringIndexInfo && + !t.numberIndexInfo; } - function inferFromSignature(source, target) { - forEachMatchingParameterType(source, target, inferFromParameterTypes); - if (source.typePredicate && target.typePredicate && source.typePredicate.kind === target.typePredicate.kind) { - inferFromTypes(source.typePredicate.type, target.typePredicate.type); + function hasExcessProperties(source, target, reportErrors) { + if (maybeTypeOfKind(target, 2588672 /* ObjectType */) && + (!(target.flags & 2588672 /* ObjectType */) || !target.isObjectLiteralPatternWithComputedProperties)) { + for (var _i = 0, _a = getPropertiesOfObjectType(source); _i < _a.length; _i++) { + var prop = _a[_i]; + if (!isKnownProperty(target, prop.name)) { + if (reportErrors) { + // We know *exactly* where things went wrong when comparing the types. + // Use this property as the error node as this will be more helpful in + // reasoning about what went wrong. + ts.Debug.assert(!!errorNode); + errorNode = prop.valueDeclaration; + reportError(ts.Diagnostics.Object_literal_may_only_specify_known_properties_and_0_does_not_exist_in_type_1, symbolToString(prop), typeToString(target)); + } + return true; + } + } } - else { - inferFromTypes(getReturnTypeOfSignature(source), getReturnTypeOfSignature(target)); + return false; + } + function eachTypeRelatedToSomeType(source, target, reportErrors) { + var result = -1 /* True */; + var sourceTypes = source.types; + for (var _i = 0, sourceTypes_1 = sourceTypes; _i < sourceTypes_1.length; _i++) { + var sourceType = sourceTypes_1[_i]; + var related = typeRelatedToSomeType(sourceType, target, /*reportErrors*/ false); + if (!related) { + return 0 /* False */; + } + result &= related; } + return result; } - function inferFromIndexTypes(source, target) { - var targetStringIndexType = getIndexTypeOfType(target, 0 /* String */); - if (targetStringIndexType) { - var sourceIndexType = getIndexTypeOfType(source, 0 /* String */) || - getImplicitIndexTypeOfType(source, 0 /* String */); - if (sourceIndexType) { - inferFromTypes(sourceIndexType, targetStringIndexType); + function typeRelatedToSomeType(source, target, reportErrors) { + var targetTypes = target.types; + if (target.flags & 524288 /* Union */ && containsType(targetTypes, source)) { + return -1 /* True */; + } + var len = targetTypes.length; + for (var i = 0; i < len; i++) { + var related = isRelatedTo(source, targetTypes[i], reportErrors && i === len - 1); + if (related) { + return related; } } - var targetNumberIndexType = getIndexTypeOfType(target, 1 /* Number */); - if (targetNumberIndexType) { - var sourceIndexType = getIndexTypeOfType(source, 1 /* Number */) || - getIndexTypeOfType(source, 0 /* String */) || - getImplicitIndexTypeOfType(source, 1 /* Number */); - if (sourceIndexType) { - inferFromTypes(sourceIndexType, targetNumberIndexType); + return 0 /* False */; + } + function typeRelatedToEachType(source, target, reportErrors) { + var result = -1 /* True */; + var targetTypes = target.types; + for (var _i = 0, targetTypes_1 = targetTypes; _i < targetTypes_1.length; _i++) { + var targetType = targetTypes_1[_i]; + var related = isRelatedTo(source, targetType, reportErrors); + if (!related) { + return 0 /* False */; } + result &= related; } + return result; } - } - function typeIdenticalToSomeType(type, types) { - for (var _i = 0, types_10 = types; _i < types_10.length; _i++) { - var t = types_10[_i]; - if (isTypeIdenticalTo(t, type)) { - return true; + function someTypeRelatedToType(source, target, reportErrors) { + var sourceTypes = source.types; + if (source.flags & 524288 /* Union */ && containsType(sourceTypes, target)) { + return -1 /* True */; + } + var len = sourceTypes.length; + for (var i = 0; i < len; i++) { + var related = isRelatedTo(sourceTypes[i], target, reportErrors && i === len - 1); + if (related) { + return related; + } } + return 0 /* False */; } - return false; - } - /** - * Return a new union or intersection type computed by removing a given set of types - * from a given union or intersection type. - */ - function removeTypesFromUnionOrIntersection(type, typesToRemove) { - var reducedTypes = []; - for (var _i = 0, _a = type.types; _i < _a.length; _i++) { - var t = _a[_i]; - if (!typeIdenticalToSomeType(t, typesToRemove)) { - reducedTypes.push(t); + function eachTypeRelatedToType(source, target, reportErrors) { + var result = -1 /* True */; + var sourceTypes = source.types; + for (var _i = 0, sourceTypes_2 = sourceTypes; _i < sourceTypes_2.length; _i++) { + var sourceType = sourceTypes_2[_i]; + var related = isRelatedTo(sourceType, target, reportErrors); + if (!related) { + return 0 /* False */; + } + result &= related; } + return result; } - return type.flags & 524288 /* Union */ ? getUnionType(reducedTypes) : getIntersectionType(reducedTypes); - } - function getInferenceCandidates(context, index) { - var inferences = context.inferences[index]; - return inferences.primary || inferences.secondary || emptyArray; - } - function hasPrimitiveConstraint(type) { - var constraint = getConstraintOfTypeParameter(type); - return constraint && maybeTypeOfKind(constraint, 8190 /* Primitive */); - } - function getInferredType(context, index) { - var inferredType = context.inferredTypes[index]; - var inferenceSucceeded; - if (!inferredType) { - var inferences = getInferenceCandidates(context, index); - if (inferences.length) { - // We widen inferred literal types if - // all inferences were made to top-level ocurrences of the type parameter, and - // the type parameter has no constraint or its constraint includes no primitive or literal types, and - // the type parameter was fixed during inference or does not occur at top-level in the return type. - var signature = context.signature; - var widenLiteralTypes = context.inferences[index].topLevel && - !hasPrimitiveConstraint(signature.typeParameters[index]) && - (context.inferences[index].isFixed || !isTypeParameterAtTopLevel(getReturnTypeOfSignature(signature), signature.typeParameters[index])); - var baseInferences = widenLiteralTypes ? ts.map(inferences, getWidenedLiteralType) : inferences; - // Infer widened union or supertype, or the unknown type for no common supertype - var unionOrSuperType = context.inferUnionTypes ? getUnionType(baseInferences, /*subtypeReduction*/ true) : getCommonSupertype(baseInferences); - inferredType = unionOrSuperType ? getWidenedType(unionOrSuperType) : unknownType; - inferenceSucceeded = !!unionOrSuperType; + function typeArgumentsRelatedTo(source, target, reportErrors) { + var sources = source.typeArguments || emptyArray; + var targets = target.typeArguments || emptyArray; + if (sources.length !== targets.length && relation === identityRelation) { + return 0 /* False */; + } + var length = sources.length <= targets.length ? sources.length : targets.length; + var result = -1 /* True */; + for (var i = 0; i < length; i++) { + var related = isRelatedTo(sources[i], targets[i], reportErrors); + if (!related) { + return 0 /* False */; + } + result &= related; + } + return result; + } + // Determine if two object types are related by structure. First, check if the result is already available in the global cache. + // Second, check if we have already started a comparison of the given two types in which case we assume the result to be true. + // Third, check if both types are part of deeply nested chains of generic type instantiations and if so assume the types are + // equal and infinitely expanding. Fourth, if we have reached a depth of 100 nested comparisons, assume we have runaway recursion + // and issue an error. Otherwise, actually compare the structure of the two types. + function objectTypeRelatedTo(source, originalSource, target, reportErrors) { + if (overflow) { + return 0 /* False */; + } + var id = relation !== identityRelation || source.id < target.id ? source.id + "," + target.id : target.id + "," + source.id; + var related = relation[id]; + if (related !== undefined) { + if (reportErrors && related === 2 /* Failed */) { + // We are elaborating errors and the cached result is an unreported failure. Record the result as a reported + // failure and continue computing the relation such that errors get reported. + relation[id] = 3 /* FailedAndReported */; + } + else { + return related === 1 /* Succeeded */ ? -1 /* True */ : 0 /* False */; + } + } + if (depth > 0) { + for (var i = 0; i < depth; i++) { + // If source and target are already being compared, consider them related with assumptions + if (maybeStack[i][id]) { + return 1 /* Maybe */; + } + } + if (depth === 100) { + overflow = true; + return 0 /* False */; + } } else { - // Infer the empty object type when no inferences were made. It is important to remember that - // in this case, inference still succeeds, meaning there is no error for not having inference - // candidates. An inference error only occurs when there are *conflicting* candidates, i.e. - // candidates with no common supertype. - inferredType = emptyObjectType; - inferenceSucceeded = true; + sourceStack = []; + targetStack = []; + maybeStack = []; + expandingFlags = 0; } - context.inferredTypes[index] = inferredType; - // Only do the constraint check if inference succeeded (to prevent cascading errors) - if (inferenceSucceeded) { - var constraint = getConstraintOfTypeParameter(context.signature.typeParameters[index]); - if (constraint) { - var instantiatedConstraint = instantiateType(constraint, getInferenceMapper(context)); - if (!isTypeAssignableTo(inferredType, getTypeWithThisArgument(instantiatedConstraint, inferredType))) { - context.inferredTypes[index] = inferredType = instantiatedConstraint; + sourceStack[depth] = source; + targetStack[depth] = target; + maybeStack[depth] = ts.createMap(); + maybeStack[depth][id] = 1 /* Succeeded */; + depth++; + var saveExpandingFlags = expandingFlags; + if (!(expandingFlags & 1) && isDeeplyNestedGeneric(source, sourceStack, depth)) + expandingFlags |= 1; + if (!(expandingFlags & 2) && isDeeplyNestedGeneric(target, targetStack, depth)) + expandingFlags |= 2; + var result; + if (expandingFlags === 3) { + result = 1 /* Maybe */; + } + else { + result = propertiesRelatedTo(source, target, reportErrors); + if (result) { + result &= signaturesRelatedTo(source, target, 0 /* Call */, reportErrors); + if (result) { + result &= signaturesRelatedTo(source, target, 1 /* Construct */, reportErrors); + if (result) { + result &= indexTypesRelatedTo(source, originalSource, target, 0 /* String */, reportErrors); + if (result) { + result &= indexTypesRelatedTo(source, originalSource, target, 1 /* Number */, reportErrors); + } + } } } } - else if (context.failedTypeParameterIndex === undefined || context.failedTypeParameterIndex > index) { - // If inference failed, it is necessary to record the index of the failed type parameter (the one we are on). - // It might be that inference has already failed on a later type parameter on a previous call to inferTypeArguments. - // So if this failure is on preceding type parameter, this type parameter is the new failure index. - context.failedTypeParameterIndex = index; + expandingFlags = saveExpandingFlags; + depth--; + if (result) { + var maybeCache = maybeStack[depth]; + // If result is definitely true, copy assumptions to global cache, else copy to next level up + var destinationCache = (result === -1 /* True */ || depth === 0) ? relation : maybeStack[depth - 1]; + ts.copyProperties(maybeCache, destinationCache); } + else { + // A false result goes straight into global cache (when something is false under assumptions it + // will also be false without assumptions) + relation[id] = reportErrors ? 3 /* FailedAndReported */ : 2 /* Failed */; + } + return result; } - return inferredType; - } - function getInferredTypes(context) { - for (var i = 0; i < context.inferredTypes.length; i++) { - getInferredType(context, i); - } - return context.inferredTypes; - } - // EXPRESSION TYPE CHECKING - function getResolvedSymbol(node) { - var links = getNodeLinks(node); - if (!links.resolvedSymbol) { - links.resolvedSymbol = !ts.nodeIsMissing(node) && resolveName(node, node.text, 107455 /* Value */ | 1048576 /* ExportValue */, ts.Diagnostics.Cannot_find_name_0, node) || unknownSymbol; - } - return links.resolvedSymbol; - } - function isInTypeQuery(node) { - // TypeScript 1.0 spec (April 2014): 3.6.3 - // A type query consists of the keyword typeof followed by an expression. - // The expression is restricted to a single identifier or a sequence of identifiers separated by periods - while (node) { - switch (node.kind) { - case 158 /* TypeQuery */: - return true; - case 69 /* Identifier */: - case 139 /* QualifiedName */: - node = node.parent; - continue; - default: - return false; + function propertiesRelatedTo(source, target, reportErrors) { + if (relation === identityRelation) { + return propertiesIdenticalTo(source, target); + } + var result = -1 /* True */; + var properties = getPropertiesOfObjectType(target); + var requireOptionalProperties = relation === subtypeRelation && !(source.flags & 8388608 /* ObjectLiteral */); + for (var _i = 0, properties_2 = properties; _i < properties_2.length; _i++) { + var targetProp = properties_2[_i]; + var sourceProp = getPropertyOfType(source, targetProp.name); + if (sourceProp !== targetProp) { + if (!sourceProp) { + if (!(targetProp.flags & 536870912 /* Optional */) || requireOptionalProperties) { + if (reportErrors) { + reportError(ts.Diagnostics.Property_0_is_missing_in_type_1, symbolToString(targetProp), typeToString(source)); + } + return 0 /* False */; + } + } + else if (!(targetProp.flags & 134217728 /* Prototype */)) { + var sourcePropFlags = getDeclarationModifierFlagsFromSymbol(sourceProp); + var targetPropFlags = getDeclarationModifierFlagsFromSymbol(targetProp); + if (sourcePropFlags & 8 /* Private */ || targetPropFlags & 8 /* Private */) { + if (sourceProp.valueDeclaration !== targetProp.valueDeclaration) { + if (reportErrors) { + if (sourcePropFlags & 8 /* Private */ && targetPropFlags & 8 /* Private */) { + reportError(ts.Diagnostics.Types_have_separate_declarations_of_a_private_property_0, symbolToString(targetProp)); + } + else { + reportError(ts.Diagnostics.Property_0_is_private_in_type_1_but_not_in_type_2, symbolToString(targetProp), typeToString(sourcePropFlags & 8 /* Private */ ? source : target), typeToString(sourcePropFlags & 8 /* Private */ ? target : source)); + } + } + return 0 /* False */; + } + } + else if (targetPropFlags & 16 /* Protected */) { + var sourceDeclaredInClass = sourceProp.parent && sourceProp.parent.flags & 32 /* Class */; + var sourceClass = sourceDeclaredInClass ? getDeclaredTypeOfSymbol(getParentOfSymbol(sourceProp)) : undefined; + var targetClass = getDeclaredTypeOfSymbol(getParentOfSymbol(targetProp)); + if (!sourceClass || !hasBaseType(sourceClass, targetClass)) { + if (reportErrors) { + reportError(ts.Diagnostics.Property_0_is_protected_but_type_1_is_not_a_class_derived_from_2, symbolToString(targetProp), typeToString(sourceClass || source), typeToString(targetClass)); + } + return 0 /* False */; + } + } + else if (sourcePropFlags & 16 /* Protected */) { + if (reportErrors) { + reportError(ts.Diagnostics.Property_0_is_protected_in_type_1_but_public_in_type_2, symbolToString(targetProp), typeToString(source), typeToString(target)); + } + return 0 /* False */; + } + var related = isRelatedTo(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp), reportErrors); + if (!related) { + if (reportErrors) { + reportError(ts.Diagnostics.Types_of_property_0_are_incompatible, symbolToString(targetProp)); + } + return 0 /* False */; + } + result &= related; + if (sourceProp.flags & 536870912 /* Optional */ && !(targetProp.flags & 536870912 /* Optional */)) { + // TypeScript 1.0 spec (April 2014): 3.8.3 + // S is a subtype of a type T, and T is a supertype of S if ... + // S' and T are object types and, for each member M in T.. + // M is a property and S' contains a property N where + // if M is a required property, N is also a required property + // (M - property in T) + // (N - property in S) + if (reportErrors) { + reportError(ts.Diagnostics.Property_0_is_optional_in_type_1_but_required_in_type_2, symbolToString(targetProp), typeToString(source), typeToString(target)); + } + return 0 /* False */; + } + } + } } + return result; } - ts.Debug.fail("should not get here"); - } - // Return the flow cache key for a "dotted name" (i.e. a sequence of identifiers - // separated by dots). The key consists of the id of the symbol referenced by the - // leftmost identifier followed by zero or more property names separated by dots. - // The result is undefined if the reference isn't a dotted name. - function getFlowCacheKey(node) { - if (node.kind === 69 /* Identifier */) { - var symbol = getResolvedSymbol(node); - return symbol !== unknownSymbol ? "" + getSymbolId(symbol) : undefined; + function propertiesIdenticalTo(source, target) { + if (!(source.flags & 2588672 /* ObjectType */ && target.flags & 2588672 /* ObjectType */)) { + return 0 /* False */; + } + var sourceProperties = getPropertiesOfObjectType(source); + var targetProperties = getPropertiesOfObjectType(target); + if (sourceProperties.length !== targetProperties.length) { + return 0 /* False */; + } + var result = -1 /* True */; + for (var _i = 0, sourceProperties_1 = sourceProperties; _i < sourceProperties_1.length; _i++) { + var sourceProp = sourceProperties_1[_i]; + var targetProp = getPropertyOfObjectType(target, sourceProp.name); + if (!targetProp) { + return 0 /* False */; + } + var related = compareProperties(sourceProp, targetProp, isRelatedTo); + if (!related) { + return 0 /* False */; + } + result &= related; + } + return result; } - if (node.kind === 97 /* ThisKeyword */) { - return "0"; + function signaturesRelatedTo(source, target, kind, reportErrors) { + if (relation === identityRelation) { + return signaturesIdenticalTo(source, target, kind); + } + if (target === anyFunctionType || source === anyFunctionType) { + return -1 /* True */; + } + var sourceSignatures = getSignaturesOfType(source, kind); + var targetSignatures = getSignaturesOfType(target, kind); + if (kind === 1 /* Construct */ && sourceSignatures.length && targetSignatures.length) { + if (isAbstractConstructorType(source) && !isAbstractConstructorType(target)) { + // An abstract constructor type is not assignable to a non-abstract constructor type + // as it would otherwise be possible to new an abstract class. Note that the assignability + // check we perform for an extends clause excludes construct signatures from the target, + // so this check never proceeds. + if (reportErrors) { + reportError(ts.Diagnostics.Cannot_assign_an_abstract_constructor_type_to_a_non_abstract_constructor_type); + } + return 0 /* False */; + } + if (!constructorVisibilitiesAreCompatible(sourceSignatures[0], targetSignatures[0], reportErrors)) { + return 0 /* False */; + } + } + var result = -1 /* True */; + var saveErrorInfo = errorInfo; + outer: for (var _i = 0, targetSignatures_1 = targetSignatures; _i < targetSignatures_1.length; _i++) { + var t = targetSignatures_1[_i]; + // Only elaborate errors from the first failure + var shouldElaborateErrors = reportErrors; + for (var _a = 0, sourceSignatures_1 = sourceSignatures; _a < sourceSignatures_1.length; _a++) { + var s = sourceSignatures_1[_a]; + var related = signatureRelatedTo(s, t, shouldElaborateErrors); + if (related) { + result &= related; + errorInfo = saveErrorInfo; + continue outer; + } + shouldElaborateErrors = false; + } + if (shouldElaborateErrors) { + reportError(ts.Diagnostics.Type_0_provides_no_match_for_the_signature_1, typeToString(source), signatureToString(t, /*enclosingDeclaration*/ undefined, /*flags*/ undefined, kind)); + } + return 0 /* False */; + } + return result; } - if (node.kind === 172 /* PropertyAccessExpression */) { - var key = getFlowCacheKey(node.expression); - return key && key + "." + node.name.text; + /** + * See signatureAssignableTo, compareSignaturesIdentical + */ + function signatureRelatedTo(source, target, reportErrors) { + return compareSignaturesRelated(source, target, /*ignoreReturnTypes*/ false, reportErrors, reportError, isRelatedTo); } - return undefined; - } - function getLeftmostIdentifierOrThis(node) { - switch (node.kind) { - case 69 /* Identifier */: - case 97 /* ThisKeyword */: - return node; - case 172 /* PropertyAccessExpression */: - return getLeftmostIdentifierOrThis(node.expression); + function signaturesIdenticalTo(source, target, kind) { + var sourceSignatures = getSignaturesOfType(source, kind); + var targetSignatures = getSignaturesOfType(target, kind); + if (sourceSignatures.length !== targetSignatures.length) { + return 0 /* False */; + } + var result = -1 /* True */; + for (var i = 0, len = sourceSignatures.length; i < len; i++) { + var related = compareSignaturesIdentical(sourceSignatures[i], targetSignatures[i], /*partialMatch*/ false, /*ignoreThisTypes*/ false, /*ignoreReturnTypes*/ false, isRelatedTo); + if (!related) { + return 0 /* False */; + } + result &= related; + } + return result; } - return undefined; - } - function isMatchingReference(source, target) { - switch (source.kind) { - case 69 /* Identifier */: - return target.kind === 69 /* Identifier */ && getResolvedSymbol(source) === getResolvedSymbol(target) || - (target.kind === 218 /* VariableDeclaration */ || target.kind === 169 /* BindingElement */) && - getExportSymbolOfValueSymbolIfExported(getResolvedSymbol(source)) === getSymbolOfNode(target); - case 97 /* ThisKeyword */: - return target.kind === 97 /* ThisKeyword */; - case 172 /* PropertyAccessExpression */: - return target.kind === 172 /* PropertyAccessExpression */ && - source.name.text === target.name.text && - isMatchingReference(source.expression, target.expression); + function eachPropertyRelatedTo(source, target, kind, reportErrors) { + var result = -1 /* True */; + for (var _i = 0, _a = getPropertiesOfObjectType(source); _i < _a.length; _i++) { + var prop = _a[_i]; + if (kind === 0 /* String */ || isNumericLiteralName(prop.name)) { + var related = isRelatedTo(getTypeOfSymbol(prop), target, reportErrors); + if (!related) { + if (reportErrors) { + reportError(ts.Diagnostics.Property_0_is_incompatible_with_index_signature, symbolToString(prop)); + } + return 0 /* False */; + } + result &= related; + } + } + return result; } - return false; - } - function containsMatchingReference(source, target) { - while (source.kind === 172 /* PropertyAccessExpression */) { - source = source.expression; - if (isMatchingReference(source, target)) { - return true; + function indexInfoRelatedTo(sourceInfo, targetInfo, reportErrors) { + var related = isRelatedTo(sourceInfo.type, targetInfo.type, reportErrors); + if (!related && reportErrors) { + reportError(ts.Diagnostics.Index_signatures_are_incompatible); } + return related; } - return false; - } - // Return true if target is a property access xxx.yyy, source is a property access xxx.zzz, the declared - // type of xxx is a union type, and yyy is a property that is possibly a discriminant. We consider a property - // a possible discriminant if its type differs in the constituents of containing union type, and if every - // choice is a unit type or a union of unit types. - function containsMatchingReferenceDiscriminant(source, target) { - return target.kind === 172 /* PropertyAccessExpression */ && - containsMatchingReference(source, target.expression) && - isDiscriminantProperty(getDeclaredTypeOfReference(target.expression), target.name.text); - } - function getDeclaredTypeOfReference(expr) { - if (expr.kind === 69 /* Identifier */) { - return getTypeOfSymbol(getResolvedSymbol(expr)); + function indexTypesRelatedTo(source, originalSource, target, kind, reportErrors) { + if (relation === identityRelation) { + return indexTypesIdenticalTo(source, target, kind); + } + var targetInfo = getIndexInfoOfType(target, kind); + if (!targetInfo || ((targetInfo.type.flags & 1 /* Any */) && !(originalSource.flags & 8190 /* Primitive */))) { + // Index signature of type any permits assignment from everything but primitives + return -1 /* True */; + } + var sourceInfo = getIndexInfoOfType(source, kind) || + kind === 1 /* Number */ && getIndexInfoOfType(source, 0 /* String */); + if (sourceInfo) { + return indexInfoRelatedTo(sourceInfo, targetInfo, reportErrors); + } + if (isObjectLiteralType(source)) { + var related = -1 /* True */; + if (kind === 0 /* String */) { + var sourceNumberInfo = getIndexInfoOfType(source, 1 /* Number */); + if (sourceNumberInfo) { + related = indexInfoRelatedTo(sourceNumberInfo, targetInfo, reportErrors); + } + } + if (related) { + related &= eachPropertyRelatedTo(source, targetInfo.type, kind, reportErrors); + } + return related; + } + if (reportErrors) { + reportError(ts.Diagnostics.Index_signature_is_missing_in_type_0, typeToString(source)); + } + return 0 /* False */; } - if (expr.kind === 172 /* PropertyAccessExpression */) { - var type = getDeclaredTypeOfReference(expr.expression); - return type && getTypeOfPropertyOfType(type, expr.name.text); + function indexTypesIdenticalTo(source, target, indexKind) { + var targetInfo = getIndexInfoOfType(target, indexKind); + var sourceInfo = getIndexInfoOfType(source, indexKind); + if (!sourceInfo && !targetInfo) { + return -1 /* True */; + } + if (sourceInfo && targetInfo && sourceInfo.isReadonly === targetInfo.isReadonly) { + return isRelatedTo(sourceInfo.type, targetInfo.type); + } + return 0 /* False */; } - return undefined; - } - function isDiscriminantProperty(type, name) { - if (type && type.flags & 524288 /* Union */) { - var prop = getPropertyOfType(type, name); - if (!prop) { - // The type may be a union that includes nullable or primitive types. If filtering - // those out produces a different type, get the property from that type instead. - // Effectively, we're checking if this *could* be a discriminant property once nullable - // and primitive types are removed by other type guards. - var filteredType = getTypeWithFacts(type, 4194304 /* Discriminatable */); - if (filteredType !== type && filteredType.flags & 524288 /* Union */) { - prop = getPropertyOfType(filteredType, name); - } + function constructorVisibilitiesAreCompatible(sourceSignature, targetSignature, reportErrors) { + if (!sourceSignature.declaration || !targetSignature.declaration) { + return true; } - if (prop && prop.flags & 268435456 /* SyntheticProperty */) { - if (prop.isDiscriminantProperty === undefined) { - prop.isDiscriminantProperty = !prop.hasCommonType && - isLiteralType(getTypeOfSymbol(prop)); - } - return prop.isDiscriminantProperty; + var sourceAccessibility = ts.getModifierFlags(sourceSignature.declaration) & 24 /* NonPublicAccessibilityModifier */; + var targetAccessibility = ts.getModifierFlags(targetSignature.declaration) & 24 /* NonPublicAccessibilityModifier */; + // A public, protected and private signature is assignable to a private signature. + if (targetAccessibility === 8 /* Private */) { + return true; + } + // A public and protected signature is assignable to a protected signature. + if (targetAccessibility === 16 /* Protected */ && sourceAccessibility !== 8 /* Private */) { + return true; + } + // Only a public signature is assignable to public signature. + if (targetAccessibility !== 16 /* Protected */ && !sourceAccessibility) { + return true; + } + if (reportErrors) { + reportError(ts.Diagnostics.Cannot_assign_a_0_constructor_type_to_a_1_constructor_type, visibilityToString(sourceAccessibility), visibilityToString(targetAccessibility)); } + return false; } - return false; } - function isOrContainsMatchingReference(source, target) { - return isMatchingReference(source, target) || containsMatchingReference(source, target); - } - function hasMatchingArgument(callExpression, reference) { - if (callExpression.arguments) { - for (var _i = 0, _a = callExpression.arguments; _i < _a.length; _i++) { - var argument = _a[_i]; - if (isOrContainsMatchingReference(reference, argument)) { + // Return true if the given type is the constructor type for an abstract class + function isAbstractConstructorType(type) { + if (type.flags & 2097152 /* Anonymous */) { + var symbol = type.symbol; + if (symbol && symbol.flags & 32 /* Class */) { + var declaration = getClassLikeDeclarationOfSymbol(symbol); + if (declaration && ts.getModifierFlags(declaration) & 128 /* Abstract */) { return true; } } } - if (callExpression.expression.kind === 172 /* PropertyAccessExpression */ && - isOrContainsMatchingReference(reference, callExpression.expression.expression)) { - return true; - } return false; } - function getFlowNodeId(flow) { - if (!flow.id) { - flow.id = nextFlowId; - nextFlowId++; - } - return flow.id; - } - function typeMaybeAssignableTo(source, target) { - if (!(source.flags & 524288 /* Union */)) { - return isTypeAssignableTo(source, target); - } - for (var _i = 0, _a = source.types; _i < _a.length; _i++) { - var t = _a[_i]; - if (isTypeAssignableTo(t, target)) { - return true; + // Return true if the given type is part of a deeply nested chain of generic instantiations. We consider this to be the case + // when structural type comparisons have been started for 10 or more instantiations of the same generic type. It is possible, + // though highly unlikely, for this test to be true in a situation where a chain of instantiations is not infinitely expanding. + // Effectively, we will generate a false positive when two types are structurally equal to at least 10 levels, but unequal at + // some level beyond that. + function isDeeplyNestedGeneric(type, stack, depth) { + // We track type references (created by createTypeReference) and instantiated types (created by instantiateType) + if (type.flags & (131072 /* Reference */ | 4194304 /* Instantiated */) && depth >= 5) { + var symbol = type.symbol; + var count = 0; + for (var i = 0; i < depth; i++) { + var t = stack[i]; + if (t.flags & (131072 /* Reference */ | 4194304 /* Instantiated */) && t.symbol === symbol) { + count++; + if (count >= 5) + return true; + } } } return false; } - // Remove those constituent types of declaredType to which no constituent type of assignedType is assignable. - // For example, when a variable of type number | string | boolean is assigned a value of type number | boolean, - // we remove type string. - function getAssignmentReducedType(declaredType, assignedType) { - if (declaredType !== assignedType) { - if (assignedType.flags & 8192 /* Never */) { - return assignedType; + function isPropertyIdenticalTo(sourceProp, targetProp) { + return compareProperties(sourceProp, targetProp, compareTypesIdentical) !== 0 /* False */; + } + function compareProperties(sourceProp, targetProp, compareTypes) { + // Two members are considered identical when + // - they are public properties with identical names, optionality, and types, + // - they are private or protected properties originating in the same declaration and having identical types + if (sourceProp === targetProp) { + return -1 /* True */; + } + var sourcePropAccessibility = getDeclarationModifierFlagsFromSymbol(sourceProp) & 24 /* NonPublicAccessibilityModifier */; + var targetPropAccessibility = getDeclarationModifierFlagsFromSymbol(targetProp) & 24 /* NonPublicAccessibilityModifier */; + if (sourcePropAccessibility !== targetPropAccessibility) { + return 0 /* False */; + } + if (sourcePropAccessibility) { + if (getTargetSymbol(sourceProp) !== getTargetSymbol(targetProp)) { + return 0 /* False */; } - var reducedType = filterType(declaredType, function (t) { return typeMaybeAssignableTo(assignedType, t); }); - if (!(reducedType.flags & 8192 /* Never */)) { - return reducedType; + } + else { + if ((sourceProp.flags & 536870912 /* Optional */) !== (targetProp.flags & 536870912 /* Optional */)) { + return 0 /* False */; } } - return declaredType; - } - function getTypeFactsOfTypes(types) { - var result = 0 /* None */; - for (var _i = 0, types_11 = types; _i < types_11.length; _i++) { - var t = types_11[_i]; - result |= getTypeFacts(t); + if (isReadonlySymbol(sourceProp) !== isReadonlySymbol(targetProp)) { + return 0 /* False */; } - return result; - } - function isFunctionObjectType(type) { - // We do a quick check for a "bind" property before performing the more expensive subtype - // check. This gives us a quicker out in the common case where an object type is not a function. - var resolved = resolveStructuredTypeMembers(type); - return !!(resolved.callSignatures.length || resolved.constructSignatures.length || - resolved.members["bind"] && isTypeSubtypeOf(type, globalFunctionType)); + return compareTypes(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp)); } - function getTypeFacts(type) { - var flags = type.flags; - if (flags & 2 /* String */) { - return strictNullChecks ? 4079361 /* StringStrictFacts */ : 4194049 /* StringFacts */; + function isMatchingSignature(source, target, partialMatch) { + // A source signature matches a target signature if the two signatures have the same number of required, + // optional, and rest parameters. + if (source.parameters.length === target.parameters.length && + source.minArgumentCount === target.minArgumentCount && + source.hasRestParameter === target.hasRestParameter) { + return true; } - if (flags & 32 /* StringLiteral */) { - return strictNullChecks ? - type.text === "" ? 3030785 /* EmptyStringStrictFacts */ : 1982209 /* NonEmptyStringStrictFacts */ : - type.text === "" ? 3145473 /* EmptyStringFacts */ : 4194049 /* NonEmptyStringFacts */; + // A source signature partially matches a target signature if the target signature has no fewer required + // parameters and no more overall parameters than the source signature (where a signature with a rest + // parameter is always considered to have more overall parameters than one without). + var sourceRestCount = source.hasRestParameter ? 1 : 0; + var targetRestCount = target.hasRestParameter ? 1 : 0; + if (partialMatch && source.minArgumentCount <= target.minArgumentCount && (sourceRestCount > targetRestCount || + sourceRestCount === targetRestCount && source.parameters.length >= target.parameters.length)) { + return true; } - if (flags & (4 /* Number */ | 16 /* Enum */)) { - return strictNullChecks ? 4079234 /* NumberStrictFacts */ : 4193922 /* NumberFacts */; + return false; + } + /** + * See signatureRelatedTo, compareSignaturesIdentical + */ + function compareSignaturesIdentical(source, target, partialMatch, ignoreThisTypes, ignoreReturnTypes, compareTypes) { + // TODO (drosen): De-duplicate code between related functions. + if (source === target) { + return -1 /* True */; } - if (flags & (64 /* NumberLiteral */ | 256 /* EnumLiteral */)) { - var isZero = type.text === "0"; - return strictNullChecks ? - isZero ? 3030658 /* ZeroStrictFacts */ : 1982082 /* NonZeroStrictFacts */ : - isZero ? 3145346 /* ZeroFacts */ : 4193922 /* NonZeroFacts */; + if (!(isMatchingSignature(source, target, partialMatch))) { + return 0 /* False */; } - if (flags & 8 /* Boolean */) { - return strictNullChecks ? 4078980 /* BooleanStrictFacts */ : 4193668 /* BooleanFacts */; + // Check that the two signatures have the same number of type parameters. We might consider + // also checking that any type parameter constraints match, but that would require instantiating + // the constraints with a common set of type arguments to get relatable entities in places where + // type parameters occur in the constraints. The complexity of doing that doesn't seem worthwhile, + // particularly as we're comparing erased versions of the signatures below. + if ((source.typeParameters ? source.typeParameters.length : 0) !== (target.typeParameters ? target.typeParameters.length : 0)) { + return 0 /* False */; } - if (flags & 136 /* BooleanLike */) { - return strictNullChecks ? - type === falseType ? 3030404 /* FalseStrictFacts */ : 1981828 /* TrueStrictFacts */ : - type === falseType ? 3145092 /* FalseFacts */ : 4193668 /* TrueFacts */; + // Spec 1.0 Section 3.8.3 & 3.8.4: + // M and N (the signatures) are instantiated using type Any as the type argument for all type parameters declared by M and N + source = getErasedSignature(source); + target = getErasedSignature(target); + var result = -1 /* True */; + if (!ignoreThisTypes) { + var sourceThisType = getThisTypeOfSignature(source); + if (sourceThisType) { + var targetThisType = getThisTypeOfSignature(target); + if (targetThisType) { + var related = compareTypes(sourceThisType, targetThisType); + if (!related) { + return 0 /* False */; + } + result &= related; + } + } } - if (flags & 2588672 /* ObjectType */) { - return isFunctionObjectType(type) ? - strictNullChecks ? 6164448 /* FunctionStrictFacts */ : 8376288 /* FunctionFacts */ : - strictNullChecks ? 6166480 /* ObjectStrictFacts */ : 8378320 /* ObjectFacts */; + var targetLen = target.parameters.length; + for (var i = 0; i < targetLen; i++) { + var s = isRestParameterIndex(source, i) ? getRestTypeOfSignature(source) : getTypeOfParameter(source.parameters[i]); + var t = isRestParameterIndex(target, i) ? getRestTypeOfSignature(target) : getTypeOfParameter(target.parameters[i]); + var related = compareTypes(s, t); + if (!related) { + return 0 /* False */; + } + result &= related; } - if (flags & (1024 /* Void */ | 2048 /* Undefined */)) { - return 2457472 /* UndefinedFacts */; + if (!ignoreReturnTypes) { + result &= compareTypes(getReturnTypeOfSignature(source), getReturnTypeOfSignature(target)); } - if (flags & 4096 /* Null */) { - return 2340752 /* NullFacts */; + return result; + } + function isRestParameterIndex(signature, parameterIndex) { + return signature.hasRestParameter && parameterIndex >= signature.parameters.length - 1; + } + function isSupertypeOfEach(candidate, types) { + for (var _i = 0, types_7 = types; _i < types_7.length; _i++) { + var t = types_7[_i]; + if (candidate !== t && !isTypeSubtypeOf(t, candidate)) + return false; } - if (flags & 512 /* ESSymbol */) { - return strictNullChecks ? 1981320 /* SymbolStrictFacts */ : 4193160 /* SymbolFacts */; + return true; + } + function literalTypesWithSameBaseType(types) { + var commonBaseType; + for (var _i = 0, types_8 = types; _i < types_8.length; _i++) { + var t = types_8[_i]; + var baseType = getBaseTypeOfLiteralType(t); + if (!commonBaseType) { + commonBaseType = baseType; + } + if (baseType === t || baseType !== commonBaseType) { + return false; + } } - if (flags & 16384 /* TypeParameter */) { - var constraint = getConstraintOfTypeParameter(type); - return getTypeFacts(constraint || emptyObjectType); + return true; + } + // When the candidate types are all literal types with the same base type, the common + // supertype is a union of those literal types. Otherwise, the common supertype is the + // first type that is a supertype of each of the other types. + function getSupertypeOrUnion(types) { + return literalTypesWithSameBaseType(types) ? getUnionType(types) : ts.forEach(types, function (t) { return isSupertypeOfEach(t, types) ? t : undefined; }); + } + function getCommonSupertype(types) { + if (!strictNullChecks) { + return getSupertypeOrUnion(types); } - if (flags & 1572864 /* UnionOrIntersection */) { - return getTypeFactsOfTypes(type.types); + var primaryTypes = ts.filter(types, function (t) { return !(t.flags & 6144 /* Nullable */); }); + if (!primaryTypes.length) { + return getUnionType(types, /*subtypeReduction*/ true); } - return 8388607 /* All */; - } - function getTypeWithFacts(type, include) { - return filterType(type, function (t) { return (getTypeFacts(t) & include) !== 0; }); + var supertype = getSupertypeOrUnion(primaryTypes); + return supertype && includeFalsyTypes(supertype, getFalsyFlagsOfTypes(types) & 6144 /* Nullable */); } - function getTypeWithDefault(type, defaultExpression) { - if (defaultExpression) { - var defaultType = checkExpression(defaultExpression); - return getUnionType([getTypeWithFacts(type, 131072 /* NEUndefined */), defaultType]); + function reportNoCommonSupertypeError(types, errorLocation, errorMessageChainHead) { + // The downfallType/bestSupertypeDownfallType is the first type that caused a particular candidate + // to not be the common supertype. So if it weren't for this one downfallType (and possibly others), + // the type in question could have been the common supertype. + var bestSupertype; + var bestSupertypeDownfallType; + var bestSupertypeScore = 0; + for (var i = 0; i < types.length; i++) { + var score = 0; + var downfallType = undefined; + for (var j = 0; j < types.length; j++) { + if (isTypeSubtypeOf(types[j], types[i])) { + score++; + } + else if (!downfallType) { + downfallType = types[j]; + } + } + ts.Debug.assert(!!downfallType, "If there is no common supertype, each type should have a downfallType"); + if (score > bestSupertypeScore) { + bestSupertype = types[i]; + bestSupertypeDownfallType = downfallType; + bestSupertypeScore = score; + } + // types.length - 1 is the maximum score, given that getCommonSupertype returned false + if (bestSupertypeScore === types.length - 1) { + break; + } } - return type; + // In the following errors, the {1} slot is before the {0} slot because checkTypeSubtypeOf supplies the + // subtype as the first argument to the error + checkTypeSubtypeOf(bestSupertypeDownfallType, bestSupertype, errorLocation, ts.Diagnostics.Type_argument_candidate_1_is_not_a_valid_type_argument_because_it_is_not_a_supertype_of_candidate_0, errorMessageChainHead); } - function getTypeOfDestructuredProperty(type, name) { - var text = getTextOfPropertyName(name); - return getTypeOfPropertyOfType(type, text) || - isNumericLiteralName(text) && getIndexTypeOfType(type, 1 /* Number */) || - getIndexTypeOfType(type, 0 /* String */) || - unknownType; + function isArrayType(type) { + return type.flags & 131072 /* Reference */ && type.target === globalArrayType; } - function getTypeOfDestructuredArrayElement(type, index) { - return isTupleLikeType(type) && getTypeOfPropertyOfType(type, "" + index) || - checkIteratedTypeOrElementType(type, /*errorNode*/ undefined, /*allowStringInput*/ false) || - unknownType; + function isArrayLikeType(type) { + // A type is array-like if it is a reference to the global Array or global ReadonlyArray type, + // or if it is not the undefined or null type and if it is assignable to ReadonlyArray + return type.flags & 131072 /* Reference */ && (type.target === globalArrayType || type.target === globalReadonlyArrayType) || + !(type.flags & 6144 /* Nullable */) && isTypeAssignableTo(type, anyReadonlyArrayType); } - function getTypeOfDestructuredSpreadElement(type) { - return createArrayType(checkIteratedTypeOrElementType(type, /*errorNode*/ undefined, /*allowStringInput*/ false) || unknownType); + function isTupleLikeType(type) { + return !!getPropertyOfType(type, "0"); } - function getAssignedTypeOfBinaryExpression(node) { - return node.parent.kind === 170 /* ArrayLiteralExpression */ || node.parent.kind === 253 /* PropertyAssignment */ ? - getTypeWithDefault(getAssignedType(node), node.right) : - checkExpression(node.right); + function isUnitType(type) { + return (type.flags & (480 /* Literal */ | 2048 /* Undefined */ | 4096 /* Null */)) !== 0; } - function getAssignedTypeOfArrayLiteralElement(node, element) { - return getTypeOfDestructuredArrayElement(getAssignedType(node), ts.indexOf(node.elements, element)); + function isLiteralType(type) { + return type.flags & 8 /* Boolean */ ? true : + type.flags & 524288 /* Union */ ? type.flags & 16 /* Enum */ ? true : !ts.forEach(type.types, function (t) { return !isUnitType(t); }) : + isUnitType(type); } - function getAssignedTypeOfSpreadElement(node) { - return getTypeOfDestructuredSpreadElement(getAssignedType(node.parent)); + function getBaseTypeOfLiteralType(type) { + return type.flags & 32 /* StringLiteral */ ? stringType : + type.flags & 64 /* NumberLiteral */ ? numberType : + type.flags & 128 /* BooleanLiteral */ ? booleanType : + type.flags & 256 /* EnumLiteral */ ? type.baseType : + type.flags & 524288 /* Union */ && !(type.flags & 16 /* Enum */) ? getUnionType(ts.map(type.types, getBaseTypeOfLiteralType)) : + type; } - function getAssignedTypeOfPropertyAssignment(node) { - return getTypeOfDestructuredProperty(getAssignedType(node.parent), node.name); + function getWidenedLiteralType(type) { + return type.flags & 32 /* StringLiteral */ && type.flags & 16777216 /* FreshLiteral */ ? stringType : + type.flags & 64 /* NumberLiteral */ && type.flags & 16777216 /* FreshLiteral */ ? numberType : + type.flags & 128 /* BooleanLiteral */ ? booleanType : + type.flags & 256 /* EnumLiteral */ ? type.baseType : + type.flags & 524288 /* Union */ && !(type.flags & 16 /* Enum */) ? getUnionType(ts.map(type.types, getWidenedLiteralType)) : + type; } - function getAssignedTypeOfShorthandPropertyAssignment(node) { - return getTypeWithDefault(getAssignedTypeOfPropertyAssignment(node), node.objectAssignmentInitializer); + /** + * Check if a Type was written as a tuple type literal. + * Prefer using isTupleLikeType() unless the use of `elementTypes` is required. + */ + function isTupleType(type) { + return !!(type.flags & 131072 /* Reference */ && type.target.flags & 262144 /* Tuple */); } - function getAssignedType(node) { - var parent = node.parent; - switch (parent.kind) { - case 207 /* ForInStatement */: - return stringType; - case 208 /* ForOfStatement */: - return checkRightHandSideOfForOf(parent.expression) || unknownType; - case 187 /* BinaryExpression */: - return getAssignedTypeOfBinaryExpression(parent); - case 181 /* DeleteExpression */: - return undefinedType; - case 170 /* ArrayLiteralExpression */: - return getAssignedTypeOfArrayLiteralElement(parent, node); - case 191 /* SpreadElementExpression */: - return getAssignedTypeOfSpreadElement(parent); - case 253 /* PropertyAssignment */: - return getAssignedTypeOfPropertyAssignment(parent); - case 254 /* ShorthandPropertyAssignment */: - return getAssignedTypeOfShorthandPropertyAssignment(parent); + function getFalsyFlagsOfTypes(types) { + var result = 0; + for (var _i = 0, types_9 = types; _i < types_9.length; _i++) { + var t = types_9[_i]; + result |= getFalsyFlags(t); } - return unknownType; - } - function getInitialTypeOfBindingElement(node) { - var pattern = node.parent; - var parentType = getInitialType(pattern.parent); - var type = pattern.kind === 167 /* ObjectBindingPattern */ ? - getTypeOfDestructuredProperty(parentType, node.propertyName || node.name) : - !node.dotDotDotToken ? - getTypeOfDestructuredArrayElement(parentType, ts.indexOf(pattern.elements, node)) : - getTypeOfDestructuredSpreadElement(parentType); - return getTypeWithDefault(type, node.initializer); + return result; } - function getTypeOfInitializer(node) { - // Return the cached type if one is available. If the type of the variable was inferred - // from its initializer, we'll already have cached the type. Otherwise we compute it now - // without caching such that transient types are reflected. - var links = getNodeLinks(node); - return links.resolvedType || checkExpression(node); + // Returns the String, Number, Boolean, StringLiteral, NumberLiteral, BooleanLiteral, Void, Undefined, or Null + // flags for the string, number, boolean, "", 0, false, void, undefined, or null types respectively. Returns + // no flags for all other types (including non-falsy literal types). + function getFalsyFlags(type) { + return type.flags & 524288 /* Union */ ? getFalsyFlagsOfTypes(type.types) : + type.flags & 32 /* StringLiteral */ ? type.text === "" ? 32 /* StringLiteral */ : 0 : + type.flags & 64 /* NumberLiteral */ ? type.text === "0" ? 64 /* NumberLiteral */ : 0 : + type.flags & 128 /* BooleanLiteral */ ? type === falseType ? 128 /* BooleanLiteral */ : 0 : + type.flags & 7406 /* PossiblyFalsy */; } - function getInitialTypeOfVariableDeclaration(node) { - if (node.initializer) { - return getTypeOfInitializer(node.initializer); - } - if (node.parent.parent.kind === 207 /* ForInStatement */) { - return stringType; - } - if (node.parent.parent.kind === 208 /* ForOfStatement */) { - return checkRightHandSideOfForOf(node.parent.parent.expression) || unknownType; + function includeFalsyTypes(type, flags) { + if ((getFalsyFlags(type) & flags) === flags) { + return type; } - return unknownType; + var types = [type]; + if (flags & 34 /* StringLike */) + types.push(emptyStringType); + if (flags & 340 /* NumberLike */) + types.push(zeroType); + if (flags & 136 /* BooleanLike */) + types.push(falseType); + if (flags & 1024 /* Void */) + types.push(voidType); + if (flags & 2048 /* Undefined */) + types.push(undefinedType); + if (flags & 4096 /* Null */) + types.push(nullType); + return getUnionType(types, /*subtypeReduction*/ true); } - function getInitialType(node) { - return node.kind === 218 /* VariableDeclaration */ ? - getInitialTypeOfVariableDeclaration(node) : - getInitialTypeOfBindingElement(node); + function removeDefinitelyFalsyTypes(type) { + return getFalsyFlags(type) & 7392 /* DefinitelyFalsy */ ? + filterType(type, function (t) { return !(getFalsyFlags(t) & 7392 /* DefinitelyFalsy */); }) : + type; } - function getInitialOrAssignedType(node) { - return node.kind === 218 /* VariableDeclaration */ || node.kind === 169 /* BindingElement */ ? - getInitialType(node) : - getAssignedType(node); + function getNonNullableType(type) { + return strictNullChecks ? getTypeWithFacts(type, 524288 /* NEUndefinedOrNull */) : type; } - function getReferenceCandidate(node) { - switch (node.kind) { - case 178 /* ParenthesizedExpression */: - return getReferenceCandidate(node.expression); - case 187 /* BinaryExpression */: - switch (node.operatorToken.kind) { - case 56 /* EqualsToken */: - return getReferenceCandidate(node.left); - case 24 /* CommaToken */: - return getReferenceCandidate(node.right); - } + /** + * Return true if type was inferred from an object literal or written as an object type literal + * with no call or construct signatures. + */ + function isObjectLiteralType(type) { + return type.symbol && (type.symbol.flags & (4096 /* ObjectLiteral */ | 2048 /* TypeLiteral */)) !== 0 && + getSignaturesOfType(type, 0 /* Call */).length === 0 && + getSignaturesOfType(type, 1 /* Construct */).length === 0; + } + function createTransientSymbol(source, type) { + var symbol = createSymbol(source.flags | 67108864 /* Transient */, source.name); + symbol.declarations = source.declarations; + symbol.parent = source.parent; + symbol.type = type; + symbol.target = source; + if (source.valueDeclaration) { + symbol.valueDeclaration = source.valueDeclaration; } - return node; + return symbol; } - function getTypeOfSwitchClause(clause) { - if (clause.kind === 249 /* CaseClause */) { - var caseType = getRegularTypeOfLiteralType(checkExpression(clause.expression)); - return isUnitType(caseType) ? caseType : undefined; + function transformTypeOfMembers(type, f) { + var members = ts.createMap(); + for (var _i = 0, _a = getPropertiesOfObjectType(type); _i < _a.length; _i++) { + var property = _a[_i]; + var original = getTypeOfSymbol(property); + var updated = f(original); + members[property.name] = updated === original ? property : createTransientSymbol(property, updated); } - return neverType; + ; + return members; } - function getSwitchClauseTypes(switchStatement) { - var links = getNodeLinks(switchStatement); - if (!links.switchTypes) { - // If all case clauses specify expressions that have unit types, we return an array - // of those unit types. Otherwise we return an empty array. - var types = ts.map(switchStatement.caseBlock.clauses, getTypeOfSwitchClause); - links.switchTypes = !ts.contains(types, undefined) ? types : emptyArray; + /** + * If the the provided object literal is subject to the excess properties check, + * create a new that is exempt. Recursively mark object literal members as exempt. + * Leave signatures alone since they are not subject to the check. + */ + function getRegularTypeOfObjectLiteral(type) { + if (!(type.flags & 8388608 /* ObjectLiteral */ && type.flags & 16777216 /* FreshLiteral */)) { + return type; } - return links.switchTypes; + var regularType = type.regularType; + if (regularType) { + return regularType; + } + var resolved = type; + var members = transformTypeOfMembers(type, getRegularTypeOfObjectLiteral); + var regularNew = createAnonymousType(resolved.symbol, members, resolved.callSignatures, resolved.constructSignatures, resolved.stringIndexInfo, resolved.numberIndexInfo); + regularNew.flags = resolved.flags & ~16777216 /* FreshLiteral */; + type.regularType = regularNew; + return regularNew; } - function eachTypeContainedIn(source, types) { - return source.flags & 524288 /* Union */ ? !ts.forEach(source.types, function (t) { return !ts.contains(types, t); }) : ts.contains(types, source); + function getWidenedTypeOfObjectLiteral(type) { + var members = transformTypeOfMembers(type, function (prop) { + var widened = getWidenedType(prop); + return prop === widened ? prop : widened; + }); + var stringIndexInfo = getIndexInfoOfType(type, 0 /* String */); + var numberIndexInfo = getIndexInfoOfType(type, 1 /* Number */); + return createAnonymousType(type.symbol, members, emptyArray, emptyArray, stringIndexInfo && createIndexInfo(getWidenedType(stringIndexInfo.type), stringIndexInfo.isReadonly), numberIndexInfo && createIndexInfo(getWidenedType(numberIndexInfo.type), numberIndexInfo.isReadonly)); } - function isTypeSubsetOf(source, target) { - return source === target || target.flags & 524288 /* Union */ && isTypeSubsetOfUnion(source, target); + function getWidenedConstituentType(type) { + return type.flags & 6144 /* Nullable */ ? type : getWidenedType(type); } - function isTypeSubsetOfUnion(source, target) { - if (source.flags & 524288 /* Union */) { - for (var _i = 0, _a = source.types; _i < _a.length; _i++) { - var t = _a[_i]; - if (!containsType(target.types, t)) { - return false; - } + function getWidenedType(type) { + if (type.flags & 100663296 /* RequiresWidening */) { + if (type.flags & 6144 /* Nullable */) { + return anyType; + } + if (type.flags & 8388608 /* ObjectLiteral */) { + return getWidenedTypeOfObjectLiteral(type); + } + if (type.flags & 524288 /* Union */) { + return getUnionType(ts.map(type.types, getWidenedConstituentType)); + } + if (isArrayType(type) || isTupleType(type)) { + return createTypeReference(type.target, ts.map(type.typeArguments, getWidenedType)); } - return true; - } - if (source.flags & 256 /* EnumLiteral */ && target.flags & 16 /* Enum */ && source.baseType === target) { - return true; } - return containsType(target.types, source); + return type; } - function filterType(type, f) { - if (type.flags & 524288 /* Union */) { - var types = type.types; - var filtered = ts.filter(types, f); - return filtered === types ? type : getUnionTypeFromSortedList(filtered); + /** + * Reports implicit any errors that occur as a result of widening 'null' and 'undefined' + * to 'any'. A call to reportWideningErrorsInType is normally accompanied by a call to + * getWidenedType. But in some cases getWidenedType is called without reporting errors + * (type argument inference is an example). + * + * The return value indicates whether an error was in fact reported. The particular circumstances + * are on a best effort basis. Currently, if the null or undefined that causes widening is inside + * an object literal property (arbitrarily deeply), this function reports an error. If no error is + * reported, reportImplicitAnyError is a suitable fallback to report a general error. + */ + function reportWideningErrorsInType(type) { + var errorReported = false; + if (type.flags & 524288 /* Union */) { + for (var _i = 0, _a = type.types; _i < _a.length; _i++) { + var t = _a[_i]; + if (reportWideningErrorsInType(t)) { + errorReported = true; + } + } } - return f(type) ? type : neverType; + if (isArrayType(type) || isTupleType(type)) { + for (var _b = 0, _c = type.typeArguments; _b < _c.length; _b++) { + var t = _c[_b]; + if (reportWideningErrorsInType(t)) { + errorReported = true; + } + } + } + if (type.flags & 8388608 /* ObjectLiteral */) { + for (var _d = 0, _e = getPropertiesOfObjectType(type); _d < _e.length; _d++) { + var p = _e[_d]; + var t = getTypeOfSymbol(p); + if (t.flags & 33554432 /* ContainsWideningType */) { + if (!reportWideningErrorsInType(t)) { + error(p.valueDeclaration, ts.Diagnostics.Object_literal_s_property_0_implicitly_has_an_1_type, p.name, typeToString(getWidenedType(t))); + } + errorReported = true; + } + } + } + return errorReported; } - function isIncomplete(flowType) { - return flowType.flags === 0; + function reportImplicitAnyError(declaration, type) { + var typeAsString = typeToString(getWidenedType(type)); + var diagnostic; + switch (declaration.kind) { + case 145 /* PropertyDeclaration */: + case 144 /* PropertySignature */: + diagnostic = ts.Diagnostics.Member_0_implicitly_has_an_1_type; + break; + case 142 /* Parameter */: + diagnostic = declaration.dotDotDotToken ? + ts.Diagnostics.Rest_parameter_0_implicitly_has_an_any_type : + ts.Diagnostics.Parameter_0_implicitly_has_an_1_type; + break; + case 169 /* BindingElement */: + diagnostic = ts.Diagnostics.Binding_element_0_implicitly_has_an_1_type; + break; + case 220 /* FunctionDeclaration */: + case 147 /* MethodDeclaration */: + case 146 /* MethodSignature */: + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + case 179 /* FunctionExpression */: + case 180 /* ArrowFunction */: + if (!declaration.name) { + error(declaration, ts.Diagnostics.Function_expression_which_lacks_return_type_annotation_implicitly_has_an_0_return_type, typeAsString); + return; + } + diagnostic = ts.Diagnostics._0_which_lacks_return_type_annotation_implicitly_has_an_1_return_type; + break; + default: + diagnostic = ts.Diagnostics.Variable_0_implicitly_has_an_1_type; + } + error(declaration, diagnostic, ts.declarationNameToString(declaration.name), typeAsString); } - function getTypeFromFlowType(flowType) { - return flowType.flags === 0 ? flowType.type : flowType; + function reportErrorsFromWidening(declaration, type) { + if (produceDiagnostics && compilerOptions.noImplicitAny && type.flags & 33554432 /* ContainsWideningType */) { + // Report implicit any error within type if possible, otherwise report error on declaration + if (!reportWideningErrorsInType(type)) { + reportImplicitAnyError(declaration, type); + } + } } - function createFlowType(type, incomplete) { - return incomplete ? { flags: 0, type: type } : type; + function forEachMatchingParameterType(source, target, callback) { + var sourceMax = source.parameters.length; + var targetMax = target.parameters.length; + var count; + if (source.hasRestParameter && target.hasRestParameter) { + count = Math.max(sourceMax, targetMax); + } + else if (source.hasRestParameter) { + count = targetMax; + } + else if (target.hasRestParameter) { + count = sourceMax; + } + else { + count = Math.min(sourceMax, targetMax); + } + for (var i = 0; i < count; i++) { + callback(getTypeAtPosition(source, i), getTypeAtPosition(target, i)); + } } - function getFlowTypeOfReference(reference, declaredType, assumeInitialized, flowContainer) { - var key; - if (!reference.flowNode || assumeInitialized && !(declaredType.flags & 4178943 /* Narrowable */)) { - return declaredType; + function createInferenceContext(signature, inferUnionTypes) { + var inferences = ts.map(signature.typeParameters, createTypeInferencesObject); + return { + signature: signature, + inferUnionTypes: inferUnionTypes, + inferences: inferences, + inferredTypes: new Array(signature.typeParameters.length) + }; + } + function createTypeInferencesObject() { + return { + primary: undefined, + secondary: undefined, + topLevel: true, + isFixed: false + }; + } + // Return true if the given type could possibly reference a type parameter for which + // we perform type inference (i.e. a type parameter of a generic function). We cache + // results for union and intersection types for performance reasons. + function couldContainTypeParameters(type) { + return !!(type.flags & 16384 /* TypeParameter */ || + type.flags & 131072 /* Reference */ && ts.forEach(type.typeArguments, couldContainTypeParameters) || + type.flags & 2097152 /* Anonymous */ && type.symbol && type.symbol.flags & (8192 /* Method */ | 2048 /* TypeLiteral */ | 32 /* Class */) || + type.flags & 1572864 /* UnionOrIntersection */ && couldUnionOrIntersectionContainTypeParameters(type)); + } + function couldUnionOrIntersectionContainTypeParameters(type) { + if (type.couldContainTypeParameters === undefined) { + type.couldContainTypeParameters = ts.forEach(type.types, couldContainTypeParameters); } - var initialType = assumeInitialized ? declaredType : includeFalsyTypes(declaredType, 2048 /* Undefined */); - var visitedFlowStart = visitedFlowCount; - var result = getTypeFromFlowType(getTypeAtFlowNode(reference.flowNode)); - visitedFlowCount = visitedFlowStart; - if (reference.parent.kind === 196 /* NonNullExpression */ && getTypeWithFacts(result, 524288 /* NEUndefinedOrNull */).flags & 8192 /* Never */) { - return declaredType; + return type.couldContainTypeParameters; + } + function isTypeParameterAtTopLevel(type, typeParameter) { + return type === typeParameter || type.flags & 1572864 /* UnionOrIntersection */ && ts.forEach(type.types, function (t) { return isTypeParameterAtTopLevel(t, typeParameter); }); + } + function inferTypes(context, originalSource, originalTarget) { + var typeParameters = context.signature.typeParameters; + var sourceStack; + var targetStack; + var depth = 0; + var inferiority = 0; + var visited = ts.createMap(); + inferFromTypes(originalSource, originalTarget); + function isInProcess(source, target) { + for (var i = 0; i < depth; i++) { + if (source === sourceStack[i] && target === targetStack[i]) { + return true; + } + } + return false; } - return result; - function getTypeAtFlowNode(flow) { - while (true) { - if (flow.flags & 512 /* Shared */) { - // We cache results of flow type resolution for shared nodes that were previously visited in - // the same getFlowTypeOfReference invocation. A node is considered shared when it is the - // antecedent of more than one node. - for (var i = visitedFlowStart; i < visitedFlowCount; i++) { - if (visitedFlowNodes[i] === flow) { - return visitedFlowTypes[i]; - } + function inferFromTypes(source, target) { + if (!couldContainTypeParameters(target)) { + return; + } + if (source.flags & 524288 /* Union */ && target.flags & 524288 /* Union */ && !(source.flags & 16 /* Enum */ && target.flags & 16 /* Enum */) || + source.flags & 1048576 /* Intersection */ && target.flags & 1048576 /* Intersection */) { + // Source and target are both unions or both intersections. If source and target + // are the same type, just relate each constituent type to itself. + if (source === target) { + for (var _i = 0, _a = source.types; _i < _a.length; _i++) { + var t = _a[_i]; + inferFromTypes(t, t); } + return; } - var type = void 0; - if (flow.flags & 16 /* Assignment */) { - type = getTypeAtFlowAssignment(flow); - if (!type) { - flow = flow.antecedent; - continue; + // Find each source constituent type that has an identically matching target constituent + // type, and for each such type infer from the type to itself. When inferring from a + // type to itself we effectively find all type parameter occurrences within that type + // and infer themselves as their type arguments. We have special handling for numeric + // and string literals because the number and string types are not represented as unions + // of all their possible values. + var matchingTypes = void 0; + for (var _b = 0, _c = source.types; _b < _c.length; _b++) { + var t = _c[_b]; + if (typeIdenticalToSomeType(t, target.types)) { + (matchingTypes || (matchingTypes = [])).push(t); + inferFromTypes(t, t); + } + else if (t.flags & (64 /* NumberLiteral */ | 32 /* StringLiteral */)) { + var b = getBaseTypeOfLiteralType(t); + if (typeIdenticalToSomeType(b, target.types)) { + (matchingTypes || (matchingTypes = [])).push(t, b); + } } } - else if (flow.flags & 96 /* Condition */) { - type = getTypeAtFlowCondition(flow); + // Next, to improve the quality of inferences, reduce the source and target types by + // removing the identically matched constituents. For example, when inferring from + // 'string | string[]' to 'string | T' we reduce the types to 'string[]' and 'T'. + if (matchingTypes) { + source = removeTypesFromUnionOrIntersection(source, matchingTypes); + target = removeTypesFromUnionOrIntersection(target, matchingTypes); } - else if (flow.flags & 128 /* SwitchClause */) { - type = getTypeAtSwitchClause(flow); + } + if (target.flags & 16384 /* TypeParameter */) { + // If target is a type parameter, make an inference, unless the source type contains + // the anyFunctionType (the wildcard type that's used to avoid contextually typing functions). + // Because the anyFunctionType is internal, it should not be exposed to the user by adding + // it as an inference candidate. Hopefully, a better candidate will come along that does + // not contain anyFunctionType when we come back to this argument for its second round + // of inference. + if (source.flags & 134217728 /* ContainsAnyFunctionType */) { + return; } - else if (flow.flags & 12 /* Label */) { - if (flow.antecedents.length === 1) { - flow = flow.antecedents[0]; - continue; + for (var i = 0; i < typeParameters.length; i++) { + if (target === typeParameters[i]) { + var inferences = context.inferences[i]; + if (!inferences.isFixed) { + // Any inferences that are made to a type parameter in a union type are inferior + // to inferences made to a flat (non-union) type. This is because if we infer to + // T | string[], we really don't know if we should be inferring to T or not (because + // the correct constituent on the target side could be string[]). Therefore, we put + // such inferior inferences into a secondary bucket, and only use them if the primary + // bucket is empty. + var candidates = inferiority ? + inferences.secondary || (inferences.secondary = []) : + inferences.primary || (inferences.primary = []); + if (!ts.contains(candidates, source)) { + candidates.push(source); + } + if (!isTypeParameterAtTopLevel(originalTarget, target)) { + inferences.topLevel = false; + } + } + return; } - type = flow.flags & 4 /* BranchLabel */ ? - getTypeAtFlowBranchLabel(flow) : - getTypeAtFlowLoopLabel(flow); } - else if (flow.flags & 2 /* Start */) { - // Check if we should continue with the control flow of the containing function. - var container = flow.container; - if (container && container !== flowContainer && reference.kind !== 172 /* PropertyAccessExpression */) { - flow = container.flowNode; - continue; - } - // At the top of the flow we have the initial type. - type = initialType; + } + else if (source.flags & 131072 /* Reference */ && target.flags & 131072 /* Reference */ && source.target === target.target) { + // If source and target are references to the same generic type, infer from type arguments + var sourceTypes = source.typeArguments || emptyArray; + var targetTypes = target.typeArguments || emptyArray; + var count = sourceTypes.length < targetTypes.length ? sourceTypes.length : targetTypes.length; + for (var i = 0; i < count; i++) { + inferFromTypes(sourceTypes[i], targetTypes[i]); } - else { - // Unreachable code errors are reported in the binding phase. Here we - // simply return the declared type to reduce follow-on errors. - type = declaredType; + } + else if (target.flags & 1572864 /* UnionOrIntersection */) { + var targetTypes = target.types; + var typeParameterCount = 0; + var typeParameter = void 0; + // First infer to each type in union or intersection that isn't a type parameter + for (var _d = 0, targetTypes_2 = targetTypes; _d < targetTypes_2.length; _d++) { + var t = targetTypes_2[_d]; + if (t.flags & 16384 /* TypeParameter */ && ts.contains(typeParameters, t)) { + typeParameter = t; + typeParameterCount++; + } + else { + inferFromTypes(source, t); + } } - if (flow.flags & 512 /* Shared */) { - // Record visited node and the associated type in the cache. - visitedFlowNodes[visitedFlowCount] = flow; - visitedFlowTypes[visitedFlowCount] = type; - visitedFlowCount++; + // Next, if target containings a single naked type parameter, make a secondary inference to that type + // parameter. This gives meaningful results for union types in co-variant positions and intersection + // types in contra-variant positions (such as callback parameters). + if (typeParameterCount === 1) { + inferiority++; + inferFromTypes(source, typeParameter); + inferiority--; } - return type; } - } - function getTypeAtFlowAssignment(flow) { - var node = flow.node; - // Assignments only narrow the computed type if the declared type is a union type. Thus, we - // only need to evaluate the assigned type if the declared type is a union type. - if (isMatchingReference(reference, node)) { - var isIncrementOrDecrement = node.parent.kind === 185 /* PrefixUnaryExpression */ || node.parent.kind === 186 /* PostfixUnaryExpression */; - return declaredType.flags & 524288 /* Union */ && !isIncrementOrDecrement ? - getAssignmentReducedType(declaredType, getInitialOrAssignedType(node)) : - declaredType; + else if (source.flags & 1572864 /* UnionOrIntersection */) { + // Source is a union or intersection type, infer from each constituent type + var sourceTypes = source.types; + for (var _e = 0, sourceTypes_3 = sourceTypes; _e < sourceTypes_3.length; _e++) { + var sourceType = sourceTypes_3[_e]; + inferFromTypes(sourceType, target); + } } - // We didn't have a direct match. However, if the reference is a dotted name, this - // may be an assignment to a left hand part of the reference. For example, for a - // reference 'x.y.z', we may be at an assignment to 'x.y' or 'x'. In that case, - // return the declared type. - if (containsMatchingReference(reference, node)) { - return declaredType; + else { + source = getApparentType(source); + if (source.flags & 2588672 /* ObjectType */) { + if (isInProcess(source, target)) { + return; + } + if (isDeeplyNestedGeneric(source, sourceStack, depth) && isDeeplyNestedGeneric(target, targetStack, depth)) { + return; + } + var key = source.id + "," + target.id; + if (visited[key]) { + return; + } + visited[key] = true; + if (depth === 0) { + sourceStack = []; + targetStack = []; + } + sourceStack[depth] = source; + targetStack[depth] = target; + depth++; + inferFromProperties(source, target); + inferFromSignatures(source, target, 0 /* Call */); + inferFromSignatures(source, target, 1 /* Construct */); + inferFromIndexTypes(source, target); + depth--; + } } - // Assignment doesn't affect reference - return undefined; } - function getTypeAtFlowCondition(flow) { - var flowType = getTypeAtFlowNode(flow.antecedent); - var type = getTypeFromFlowType(flowType); - if (!(type.flags & 8192 /* Never */)) { - // If we have an antecedent type (meaning we're reachable in some way), we first - // attempt to narrow the antecedent type. If that produces the never type, and if - // the antecedent type is incomplete (i.e. a transient type in a loop), then we - // take the type guard as an indication that control *could* reach here once we - // have the complete type. We proceed by switching to the silent never type which - // doesn't report errors when operators are applied to it. Note that this is the - // *only* place a silent never type is ever generated. - var assumeTrue = (flow.flags & 32 /* TrueCondition */) !== 0; - type = narrowType(type, flow.expression, assumeTrue); - if (type.flags & 8192 /* Never */ && isIncomplete(flowType)) { - type = silentNeverType; + function inferFromProperties(source, target) { + var properties = getPropertiesOfObjectType(target); + for (var _i = 0, properties_3 = properties; _i < properties_3.length; _i++) { + var targetProp = properties_3[_i]; + var sourceProp = getPropertyOfObjectType(source, targetProp.name); + if (sourceProp) { + inferFromTypes(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp)); } } - return createFlowType(type, isIncomplete(flowType)); } - function getTypeAtSwitchClause(flow) { - var flowType = getTypeAtFlowNode(flow.antecedent); - var type = getTypeFromFlowType(flowType); - var expr = flow.switchStatement.expression; - if (isMatchingReference(reference, expr)) { - type = narrowTypeBySwitchOnDiscriminant(type, flow.switchStatement, flow.clauseStart, flow.clauseEnd); - } - else if (isMatchingReferenceDiscriminant(expr)) { - type = narrowTypeByDiscriminant(type, expr, function (t) { return narrowTypeBySwitchOnDiscriminant(t, flow.switchStatement, flow.clauseStart, flow.clauseEnd); }); + function inferFromSignatures(source, target, kind) { + var sourceSignatures = getSignaturesOfType(source, kind); + var targetSignatures = getSignaturesOfType(target, kind); + var sourceLen = sourceSignatures.length; + var targetLen = targetSignatures.length; + var len = sourceLen < targetLen ? sourceLen : targetLen; + for (var i = 0; i < len; i++) { + inferFromSignature(getErasedSignature(sourceSignatures[sourceLen - len + i]), getErasedSignature(targetSignatures[targetLen - len + i])); } - return createFlowType(type, isIncomplete(flowType)); } - function getTypeAtFlowBranchLabel(flow) { - var antecedentTypes = []; - var subtypeReduction = false; - var seenIncomplete = false; - for (var _i = 0, _a = flow.antecedents; _i < _a.length; _i++) { - var antecedent = _a[_i]; - var flowType = getTypeAtFlowNode(antecedent); - var type = getTypeFromFlowType(flowType); - // If the type at a particular antecedent path is the declared type and the - // reference is known to always be assigned (i.e. when declared and initial types - // are the same), there is no reason to process more antecedents since the only - // possible outcome is subtypes that will be removed in the final union type anyway. - if (type === declaredType && declaredType === initialType) { - return type; - } - if (!ts.contains(antecedentTypes, type)) { - antecedentTypes.push(type); - } - // If an antecedent type is not a subset of the declared type, we need to perform - // subtype reduction. This happens when a "foreign" type is injected into the control - // flow using the instanceof operator or a user defined type predicate. - if (!isTypeSubsetOf(type, declaredType)) { - subtypeReduction = true; - } - if (isIncomplete(flowType)) { - seenIncomplete = true; - } - } - return createFlowType(getUnionType(antecedentTypes, subtypeReduction), seenIncomplete); + function inferFromParameterTypes(source, target) { + return inferFromTypes(source, target); } - function getTypeAtFlowLoopLabel(flow) { - // If we have previously computed the control flow type for the reference at - // this flow loop junction, return the cached type. - var id = getFlowNodeId(flow); - var cache = flowLoopCaches[id] || (flowLoopCaches[id] = ts.createMap()); - if (!key) { - key = getFlowCacheKey(reference); + function inferFromSignature(source, target) { + forEachMatchingParameterType(source, target, inferFromParameterTypes); + if (source.typePredicate && target.typePredicate && source.typePredicate.kind === target.typePredicate.kind) { + inferFromTypes(source.typePredicate.type, target.typePredicate.type); } - if (cache[key]) { - return cache[key]; + else { + inferFromTypes(getReturnTypeOfSignature(source), getReturnTypeOfSignature(target)); } - // If this flow loop junction and reference are already being processed, return - // the union of the types computed for each branch so far, marked as incomplete. - // We should never see an empty array here because the first antecedent of a loop - // junction is always the non-looping control flow path that leads to the top. - for (var i = flowLoopStart; i < flowLoopCount; i++) { - if (flowLoopNodes[i] === flow && flowLoopKeys[i] === key) { - return createFlowType(getUnionType(flowLoopTypes[i]), /*incomplete*/ true); + } + function inferFromIndexTypes(source, target) { + var targetStringIndexType = getIndexTypeOfType(target, 0 /* String */); + if (targetStringIndexType) { + var sourceIndexType = getIndexTypeOfType(source, 0 /* String */) || + getImplicitIndexTypeOfType(source, 0 /* String */); + if (sourceIndexType) { + inferFromTypes(sourceIndexType, targetStringIndexType); } } - // Add the flow loop junction and reference to the in-process stack and analyze - // each antecedent code path. - var antecedentTypes = []; - var subtypeReduction = false; - var firstAntecedentType; - flowLoopNodes[flowLoopCount] = flow; - flowLoopKeys[flowLoopCount] = key; - flowLoopTypes[flowLoopCount] = antecedentTypes; - for (var _i = 0, _a = flow.antecedents; _i < _a.length; _i++) { - var antecedent = _a[_i]; - flowLoopCount++; - var flowType = getTypeAtFlowNode(antecedent); - flowLoopCount--; - if (!firstAntecedentType) { - firstAntecedentType = flowType; - } - var type = getTypeFromFlowType(flowType); - // If we see a value appear in the cache it is a sign that control flow analysis - // was restarted and completed by checkExpressionCached. We can simply pick up - // the resulting type and bail out. - if (cache[key]) { - return cache[key]; - } - if (!ts.contains(antecedentTypes, type)) { - antecedentTypes.push(type); - } - // If an antecedent type is not a subset of the declared type, we need to perform - // subtype reduction. This happens when a "foreign" type is injected into the control - // flow using the instanceof operator or a user defined type predicate. - if (!isTypeSubsetOf(type, declaredType)) { - subtypeReduction = true; - } - // If the type at a particular antecedent path is the declared type there is no - // reason to process more antecedents since the only possible outcome is subtypes - // that will be removed in the final union type anyway. - if (type === declaredType) { - break; + var targetNumberIndexType = getIndexTypeOfType(target, 1 /* Number */); + if (targetNumberIndexType) { + var sourceIndexType = getIndexTypeOfType(source, 1 /* Number */) || + getIndexTypeOfType(source, 0 /* String */) || + getImplicitIndexTypeOfType(source, 1 /* Number */); + if (sourceIndexType) { + inferFromTypes(sourceIndexType, targetNumberIndexType); } } - // The result is incomplete if the first antecedent (the non-looping control flow path) - // is incomplete. - var result = getUnionType(antecedentTypes, subtypeReduction); - if (isIncomplete(firstAntecedentType)) { - return createFlowType(result, /*incomplete*/ true); - } - return cache[key] = result; - } - function isMatchingReferenceDiscriminant(expr) { - return expr.kind === 172 /* PropertyAccessExpression */ && - declaredType.flags & 524288 /* Union */ && - isMatchingReference(reference, expr.expression) && - isDiscriminantProperty(declaredType, expr.name.text); - } - function narrowTypeByDiscriminant(type, propAccess, narrowType) { - var propName = propAccess.name.text; - var propType = getTypeOfPropertyOfType(type, propName); - var narrowedPropType = propType && narrowType(propType); - return propType === narrowedPropType ? type : filterType(type, function (t) { return isTypeComparableTo(getTypeOfPropertyOfType(t, propName), narrowedPropType); }); - } - function narrowTypeByTruthiness(type, expr, assumeTrue) { - if (isMatchingReference(reference, expr)) { - return getTypeWithFacts(type, assumeTrue ? 1048576 /* Truthy */ : 2097152 /* Falsy */); - } - if (isMatchingReferenceDiscriminant(expr)) { - return narrowTypeByDiscriminant(type, expr, function (t) { return getTypeWithFacts(t, assumeTrue ? 1048576 /* Truthy */ : 2097152 /* Falsy */); }); - } - if (containsMatchingReferenceDiscriminant(reference, expr)) { - return declaredType; - } - return type; - } - function narrowTypeByBinaryExpression(type, expr, assumeTrue) { - switch (expr.operatorToken.kind) { - case 56 /* EqualsToken */: - return narrowTypeByTruthiness(type, expr.left, assumeTrue); - case 30 /* EqualsEqualsToken */: - case 31 /* ExclamationEqualsToken */: - case 32 /* EqualsEqualsEqualsToken */: - case 33 /* ExclamationEqualsEqualsToken */: - var operator_1 = expr.operatorToken.kind; - var left_1 = getReferenceCandidate(expr.left); - var right_1 = getReferenceCandidate(expr.right); - if (left_1.kind === 182 /* TypeOfExpression */ && right_1.kind === 9 /* StringLiteral */) { - return narrowTypeByTypeof(type, left_1, operator_1, right_1, assumeTrue); - } - if (right_1.kind === 182 /* TypeOfExpression */ && left_1.kind === 9 /* StringLiteral */) { - return narrowTypeByTypeof(type, right_1, operator_1, left_1, assumeTrue); - } - if (isMatchingReference(reference, left_1)) { - return narrowTypeByEquality(type, operator_1, right_1, assumeTrue); - } - if (isMatchingReference(reference, right_1)) { - return narrowTypeByEquality(type, operator_1, left_1, assumeTrue); - } - if (isMatchingReferenceDiscriminant(left_1)) { - return narrowTypeByDiscriminant(type, left_1, function (t) { return narrowTypeByEquality(t, operator_1, right_1, assumeTrue); }); - } - if (isMatchingReferenceDiscriminant(right_1)) { - return narrowTypeByDiscriminant(type, right_1, function (t) { return narrowTypeByEquality(t, operator_1, left_1, assumeTrue); }); - } - if (containsMatchingReferenceDiscriminant(reference, left_1) || containsMatchingReferenceDiscriminant(reference, right_1)) { - return declaredType; - } - break; - case 91 /* InstanceOfKeyword */: - return narrowTypeByInstanceof(type, expr, assumeTrue); - case 24 /* CommaToken */: - return narrowType(type, expr.right, assumeTrue); - } - return type; - } - function narrowTypeByEquality(type, operator, value, assumeTrue) { - if (type.flags & 1 /* Any */) { - return type; - } - if (operator === 31 /* ExclamationEqualsToken */ || operator === 33 /* ExclamationEqualsEqualsToken */) { - assumeTrue = !assumeTrue; - } - var valueType = checkExpression(value); - if (valueType.flags & 6144 /* Nullable */) { - if (!strictNullChecks) { - return type; - } - var doubleEquals = operator === 30 /* EqualsEqualsToken */ || operator === 31 /* ExclamationEqualsToken */; - var facts = doubleEquals ? - assumeTrue ? 65536 /* EQUndefinedOrNull */ : 524288 /* NEUndefinedOrNull */ : - value.kind === 93 /* NullKeyword */ ? - assumeTrue ? 32768 /* EQNull */ : 262144 /* NENull */ : - assumeTrue ? 16384 /* EQUndefined */ : 131072 /* NEUndefined */; - return getTypeWithFacts(type, facts); - } - if (type.flags & 2589191 /* NotUnionOrUnit */) { - return type; - } - if (assumeTrue) { - var narrowedType = filterType(type, function (t) { return areTypesComparable(t, valueType); }); - return narrowedType.flags & 8192 /* Never */ ? type : narrowedType; - } - if (isUnitType(valueType)) { - var regularType_1 = getRegularTypeOfLiteralType(valueType); - return filterType(type, function (t) { return getRegularTypeOfLiteralType(t) !== regularType_1; }); - } - return type; } - function narrowTypeByTypeof(type, typeOfExpr, operator, literal, assumeTrue) { - // We have '==', '!=', '====', or !==' operator with 'typeof xxx' and string literal operands - var target = getReferenceCandidate(typeOfExpr.expression); - if (!isMatchingReference(reference, target)) { - // For a reference of the form 'x.y', a 'typeof x === ...' type guard resets the - // narrowed type of 'y' to its declared type. - if (containsMatchingReference(reference, target)) { - return declaredType; - } - return type; - } - if (operator === 31 /* ExclamationEqualsToken */ || operator === 33 /* ExclamationEqualsEqualsToken */) { - assumeTrue = !assumeTrue; - } - if (assumeTrue && !(type.flags & 524288 /* Union */)) { - // We narrow a non-union type to an exact primitive type if the non-union type - // is a supertype of that primitive type. For example, type 'any' can be narrowed - // to one of the primitive types. - var targetType = typeofTypesByName[literal.text]; - if (targetType && isTypeSubtypeOf(targetType, type)) { - return targetType; - } + } + function typeIdenticalToSomeType(type, types) { + for (var _i = 0, types_10 = types; _i < types_10.length; _i++) { + var t = types_10[_i]; + if (isTypeIdenticalTo(t, type)) { + return true; } - var facts = assumeTrue ? - typeofEQFacts[literal.text] || 64 /* TypeofEQHostObject */ : - typeofNEFacts[literal.text] || 8192 /* TypeofNEHostObject */; - return getTypeWithFacts(type, facts); } - function narrowTypeBySwitchOnDiscriminant(type, switchStatement, clauseStart, clauseEnd) { - // We only narrow if all case expressions specify values with unit types - var switchTypes = getSwitchClauseTypes(switchStatement); - if (!switchTypes.length) { - return type; - } - var clauseTypes = switchTypes.slice(clauseStart, clauseEnd); - var hasDefaultClause = clauseStart === clauseEnd || ts.contains(clauseTypes, neverType); - var discriminantType = getUnionType(clauseTypes); - var caseType = discriminantType.flags & 8192 /* Never */ ? neverType : filterType(type, function (t) { return isTypeComparableTo(discriminantType, t); }); - if (!hasDefaultClause) { - return caseType; + return false; + } + /** + * Return a new union or intersection type computed by removing a given set of types + * from a given union or intersection type. + */ + function removeTypesFromUnionOrIntersection(type, typesToRemove) { + var reducedTypes = []; + for (var _i = 0, _a = type.types; _i < _a.length; _i++) { + var t = _a[_i]; + if (!typeIdenticalToSomeType(t, typesToRemove)) { + reducedTypes.push(t); } - var defaultType = filterType(type, function (t) { return !(isUnitType(t) && ts.contains(switchTypes, getRegularTypeOfLiteralType(t))); }); - return caseType.flags & 8192 /* Never */ ? defaultType : getUnionType([caseType, defaultType]); } - function narrowTypeByInstanceof(type, expr, assumeTrue) { - var left = getReferenceCandidate(expr.left); - if (!isMatchingReference(reference, left)) { - // For a reference of the form 'x.y', an 'x instanceof T' type guard resets the - // narrowed type of 'y' to its declared type. - if (containsMatchingReference(reference, left)) { - return declaredType; - } - return type; - } - // Check that right operand is a function type with a prototype property - var rightType = checkExpression(expr.right); - if (!isTypeSubtypeOf(rightType, globalFunctionType)) { - return type; - } - var targetType; - var prototypeProperty = getPropertyOfType(rightType, "prototype"); - if (prototypeProperty) { - // Target type is type of the prototype property - var prototypePropertyType = getTypeOfSymbol(prototypeProperty); - if (!isTypeAny(prototypePropertyType)) { - targetType = prototypePropertyType; - } + return type.flags & 524288 /* Union */ ? getUnionType(reducedTypes) : getIntersectionType(reducedTypes); + } + function getInferenceCandidates(context, index) { + var inferences = context.inferences[index]; + return inferences.primary || inferences.secondary || emptyArray; + } + function hasPrimitiveConstraint(type) { + var constraint = getConstraintOfTypeParameter(type); + return constraint && maybeTypeOfKind(constraint, 8190 /* Primitive */); + } + function getInferredType(context, index) { + var inferredType = context.inferredTypes[index]; + var inferenceSucceeded; + if (!inferredType) { + var inferences = getInferenceCandidates(context, index); + if (inferences.length) { + // We widen inferred literal types if + // all inferences were made to top-level ocurrences of the type parameter, and + // the type parameter has no constraint or its constraint includes no primitive or literal types, and + // the type parameter was fixed during inference or does not occur at top-level in the return type. + var signature = context.signature; + var widenLiteralTypes = context.inferences[index].topLevel && + !hasPrimitiveConstraint(signature.typeParameters[index]) && + (context.inferences[index].isFixed || !isTypeParameterAtTopLevel(getReturnTypeOfSignature(signature), signature.typeParameters[index])); + var baseInferences = widenLiteralTypes ? ts.map(inferences, getWidenedLiteralType) : inferences; + // Infer widened union or supertype, or the unknown type for no common supertype + var unionOrSuperType = context.inferUnionTypes ? getUnionType(baseInferences, /*subtypeReduction*/ true) : getCommonSupertype(baseInferences); + inferredType = unionOrSuperType ? getWidenedType(unionOrSuperType) : unknownType; + inferenceSucceeded = !!unionOrSuperType; } - // Don't narrow from 'any' if the target type is exactly 'Object' or 'Function' - if (isTypeAny(type) && (targetType === globalObjectType || targetType === globalFunctionType)) { - return type; + else { + // Infer the empty object type when no inferences were made. It is important to remember that + // in this case, inference still succeeds, meaning there is no error for not having inference + // candidates. An inference error only occurs when there are *conflicting* candidates, i.e. + // candidates with no common supertype. + inferredType = emptyObjectType; + inferenceSucceeded = true; } - if (!targetType) { - // Target type is type of construct signature - var constructSignatures = void 0; - if (rightType.flags & 65536 /* Interface */) { - constructSignatures = resolveDeclaredMembers(rightType).declaredConstructSignatures; - } - else if (rightType.flags & 2097152 /* Anonymous */) { - constructSignatures = getSignaturesOfType(rightType, 1 /* Construct */); - } - if (constructSignatures && constructSignatures.length) { - targetType = getUnionType(ts.map(constructSignatures, function (signature) { return getReturnTypeOfSignature(getErasedSignature(signature)); })); + context.inferredTypes[index] = inferredType; + // Only do the constraint check if inference succeeded (to prevent cascading errors) + if (inferenceSucceeded) { + var constraint = getConstraintOfTypeParameter(context.signature.typeParameters[index]); + if (constraint) { + var instantiatedConstraint = instantiateType(constraint, getInferenceMapper(context)); + if (!isTypeAssignableTo(inferredType, getTypeWithThisArgument(instantiatedConstraint, inferredType))) { + context.inferredTypes[index] = inferredType = instantiatedConstraint; + } } } - if (targetType) { - return getNarrowedType(type, targetType, assumeTrue); + else if (context.failedTypeParameterIndex === undefined || context.failedTypeParameterIndex > index) { + // If inference failed, it is necessary to record the index of the failed type parameter (the one we are on). + // It might be that inference has already failed on a later type parameter on a previous call to inferTypeArguments. + // So if this failure is on preceding type parameter, this type parameter is the new failure index. + context.failedTypeParameterIndex = index; } - return type; } - function getNarrowedType(type, candidate, assumeTrue) { - if (!assumeTrue) { - return filterType(type, function (t) { return !isTypeInstanceOf(t, candidate); }); - } - // If the current type is a union type, remove all constituents that couldn't be instances of - // the candidate type. If one or more constituents remain, return a union of those. - if (type.flags & 524288 /* Union */) { - var assignableType = filterType(type, function (t) { return isTypeInstanceOf(t, candidate); }); - if (!(assignableType.flags & 8192 /* Never */)) { - return assignableType; - } - } - // If the candidate type is a subtype of the target type, narrow to the candidate type. - // Otherwise, if the target type is assignable to the candidate type, keep the target type. - // Otherwise, if the candidate type is assignable to the target type, narrow to the candidate - // type. Otherwise, the types are completely unrelated, so narrow to an intersection of the - // two types. - var targetType = type.flags & 16384 /* TypeParameter */ ? getApparentType(type) : type; - return isTypeSubtypeOf(candidate, type) ? candidate : - isTypeAssignableTo(type, candidate) ? type : - isTypeAssignableTo(candidate, targetType) ? candidate : - getIntersectionType([type, candidate]); + return inferredType; + } + function getInferredTypes(context) { + for (var i = 0; i < context.inferredTypes.length; i++) { + getInferredType(context, i); } - function narrowTypeByTypePredicate(type, callExpression, assumeTrue) { - if (!hasMatchingArgument(callExpression, reference)) { - return type; - } - var signature = getResolvedSignature(callExpression); - var predicate = signature.typePredicate; - if (!predicate) { - return type; - } - // Don't narrow from 'any' if the predicate type is exactly 'Object' or 'Function' - if (isTypeAny(type) && (predicate.type === globalObjectType || predicate.type === globalFunctionType)) { - return type; - } - if (ts.isIdentifierTypePredicate(predicate)) { - var predicateArgument = callExpression.arguments[predicate.parameterIndex]; - if (predicateArgument) { - if (isMatchingReference(reference, predicateArgument)) { - return getNarrowedType(type, predicate.type, assumeTrue); - } - if (containsMatchingReference(reference, predicateArgument)) { - return declaredType; - } - } - } - else { - var invokedExpression = skipParenthesizedNodes(callExpression.expression); - if (invokedExpression.kind === 173 /* ElementAccessExpression */ || invokedExpression.kind === 172 /* PropertyAccessExpression */) { - var accessExpression = invokedExpression; - var possibleReference = skipParenthesizedNodes(accessExpression.expression); - if (isMatchingReference(reference, possibleReference)) { - return getNarrowedType(type, predicate.type, assumeTrue); - } - if (containsMatchingReference(reference, possibleReference)) { - return declaredType; - } - } - } - return type; + return context.inferredTypes; + } + // EXPRESSION TYPE CHECKING + function getResolvedSymbol(node) { + var links = getNodeLinks(node); + if (!links.resolvedSymbol) { + links.resolvedSymbol = !ts.nodeIsMissing(node) && resolveName(node, node.text, 107455 /* Value */ | 1048576 /* ExportValue */, ts.Diagnostics.Cannot_find_name_0, node) || unknownSymbol; } - // Narrow the given type based on the given expression having the assumed boolean value. The returned type - // will be a subtype or the same type as the argument. - function narrowType(type, expr, assumeTrue) { - switch (expr.kind) { + return links.resolvedSymbol; + } + function isInTypeQuery(node) { + // TypeScript 1.0 spec (April 2014): 3.6.3 + // A type query consists of the keyword typeof followed by an expression. + // The expression is restricted to a single identifier or a sequence of identifiers separated by periods + while (node) { + switch (node.kind) { + case 158 /* TypeQuery */: + return true; case 69 /* Identifier */: - case 97 /* ThisKeyword */: - case 172 /* PropertyAccessExpression */: - return narrowTypeByTruthiness(type, expr, assumeTrue); - case 174 /* CallExpression */: - return narrowTypeByTypePredicate(type, expr, assumeTrue); - case 178 /* ParenthesizedExpression */: - return narrowType(type, expr.expression, assumeTrue); - case 187 /* BinaryExpression */: - return narrowTypeByBinaryExpression(type, expr, assumeTrue); - case 185 /* PrefixUnaryExpression */: - if (expr.operator === 49 /* ExclamationToken */) { - return narrowType(type, expr.operand, !assumeTrue); - } - break; + case 139 /* QualifiedName */: + node = node.parent; + continue; + default: + return false; } - return type; } + ts.Debug.fail("should not get here"); } - function getTypeOfSymbolAtLocation(symbol, location) { - // If we have an identifier or a property access at the given location, if the location is - // an dotted name expression, and if the location is not an assignment target, obtain the type - // of the expression (which will reflect control flow analysis). If the expression indeed - // resolved to the given symbol, return the narrowed type. - if (location.kind === 69 /* Identifier */) { - if (ts.isRightSideOfQualifiedNameOrPropertyAccess(location)) { - location = location.parent; - } - if (ts.isExpression(location) && !ts.isAssignmentTarget(location)) { - var type = checkExpression(location); - if (getExportSymbolOfValueSymbolIfExported(getNodeLinks(location).resolvedSymbol) === symbol) { - return type; - } - } + // Return the flow cache key for a "dotted name" (i.e. a sequence of identifiers + // separated by dots). The key consists of the id of the symbol referenced by the + // leftmost identifier followed by zero or more property names separated by dots. + // The result is undefined if the reference isn't a dotted name. + function getFlowCacheKey(node) { + if (node.kind === 69 /* Identifier */) { + var symbol = getResolvedSymbol(node); + return symbol !== unknownSymbol ? "" + getSymbolId(symbol) : undefined; } - // The location isn't a reference to the given symbol, meaning we're being asked - // a hypothetical question of what type the symbol would have if there was a reference - // to it at the given location. Since we have no control flow information for the - // hypothetical reference (control flow information is created and attached by the - // binder), we simply return the declared type of the symbol. - return getTypeOfSymbol(symbol); - } - function skipParenthesizedNodes(expression) { - while (expression.kind === 178 /* ParenthesizedExpression */) { - expression = expression.expression; + if (node.kind === 97 /* ThisKeyword */) { + return "0"; } - return expression; + if (node.kind === 172 /* PropertyAccessExpression */) { + var key = getFlowCacheKey(node.expression); + return key && key + "." + node.name.text; + } + return undefined; } - function getControlFlowContainer(node) { - while (true) { - node = node.parent; - if (ts.isFunctionLike(node) && !ts.getImmediatelyInvokedFunctionExpression(node) || - node.kind === 226 /* ModuleBlock */ || - node.kind === 256 /* SourceFile */ || - node.kind === 145 /* PropertyDeclaration */) { + function getLeftmostIdentifierOrThis(node) { + switch (node.kind) { + case 69 /* Identifier */: + case 97 /* ThisKeyword */: return node; - } + case 172 /* PropertyAccessExpression */: + return getLeftmostIdentifierOrThis(node.expression); } + return undefined; } - // Check if a parameter is assigned anywhere within its declaring function. - function isParameterAssigned(symbol) { - var func = ts.getRootDeclaration(symbol.valueDeclaration).parent; - var links = getNodeLinks(func); - if (!(links.flags & 4194304 /* AssignmentsMarked */)) { - links.flags |= 4194304 /* AssignmentsMarked */; - if (!hasParentWithAssignmentsMarked(func)) { - markParameterAssignments(func); - } + function isMatchingReference(source, target) { + switch (source.kind) { + case 69 /* Identifier */: + return target.kind === 69 /* Identifier */ && getResolvedSymbol(source) === getResolvedSymbol(target) || + (target.kind === 218 /* VariableDeclaration */ || target.kind === 169 /* BindingElement */) && + getExportSymbolOfValueSymbolIfExported(getResolvedSymbol(source)) === getSymbolOfNode(target); + case 97 /* ThisKeyword */: + return target.kind === 97 /* ThisKeyword */; + case 172 /* PropertyAccessExpression */: + return target.kind === 172 /* PropertyAccessExpression */ && + source.name.text === target.name.text && + isMatchingReference(source.expression, target.expression); } - return symbol.isAssigned || false; + return false; } - function hasParentWithAssignmentsMarked(node) { - while (true) { - node = node.parent; - if (!node) { - return false; - } - if (ts.isFunctionLike(node) && getNodeLinks(node).flags & 4194304 /* AssignmentsMarked */) { + function containsMatchingReference(source, target) { + while (source.kind === 172 /* PropertyAccessExpression */) { + source = source.expression; + if (isMatchingReference(source, target)) { return true; } } + return false; } - function markParameterAssignments(node) { - if (node.kind === 69 /* Identifier */) { - if (ts.isAssignmentTarget(node)) { - var symbol = getResolvedSymbol(node); - if (symbol.valueDeclaration && ts.getRootDeclaration(symbol.valueDeclaration).kind === 142 /* Parameter */) { - symbol.isAssigned = true; - } - } + // Return true if target is a property access xxx.yyy, source is a property access xxx.zzz, the declared + // type of xxx is a union type, and yyy is a property that is possibly a discriminant. We consider a property + // a possible discriminant if its type differs in the constituents of containing union type, and if every + // choice is a unit type or a union of unit types. + function containsMatchingReferenceDiscriminant(source, target) { + return target.kind === 172 /* PropertyAccessExpression */ && + containsMatchingReference(source, target.expression) && + isDiscriminantProperty(getDeclaredTypeOfReference(target.expression), target.name.text); + } + function getDeclaredTypeOfReference(expr) { + if (expr.kind === 69 /* Identifier */) { + return getTypeOfSymbol(getResolvedSymbol(expr)); } - else { - ts.forEachChild(node, markParameterAssignments); + if (expr.kind === 172 /* PropertyAccessExpression */) { + var type = getDeclaredTypeOfReference(expr.expression); + return type && getTypeOfPropertyOfType(type, expr.name.text); } + return undefined; } - function checkIdentifier(node) { - var symbol = getResolvedSymbol(node); - // As noted in ECMAScript 6 language spec, arrow functions never have an arguments objects. - // Although in down-level emit of arrow function, we emit it using function expression which means that - // arguments objects will be bound to the inner object; emitting arrow function natively in ES6, arguments objects - // will be bound to non-arrow function that contain this arrow function. This results in inconsistent behavior. - // To avoid that we will give an error to users if they use arguments objects in arrow function so that they - // can explicitly bound arguments objects - if (symbol === argumentsSymbol) { - var container = ts.getContainingFunction(node); - if (languageVersion < 2 /* ES6 */) { - if (container.kind === 180 /* ArrowFunction */) { - error(node, ts.Diagnostics.The_arguments_object_cannot_be_referenced_in_an_arrow_function_in_ES3_and_ES5_Consider_using_a_standard_function_expression); - } - else if (ts.hasModifier(container, 256 /* Async */)) { - error(node, ts.Diagnostics.The_arguments_object_cannot_be_referenced_in_an_async_function_or_method_in_ES3_and_ES5_Consider_using_a_standard_function_or_method); + function isDiscriminantProperty(type, name) { + if (type && type.flags & 524288 /* Union */) { + var prop = getUnionOrIntersectionProperty(type, name); + if (prop && prop.flags & 268435456 /* SyntheticProperty */) { + if (prop.isDiscriminantProperty === undefined) { + prop.isDiscriminantProperty = prop.hasNonUniformType && isLiteralType(getTypeOfSymbol(prop)); } + return prop.isDiscriminantProperty; } - if (node.flags & 262144 /* AwaitContext */) { - getNodeLinks(container).flags |= 8192 /* CaptureArguments */; + } + return false; + } + function isOrContainsMatchingReference(source, target) { + return isMatchingReference(source, target) || containsMatchingReference(source, target); + } + function hasMatchingArgument(callExpression, reference) { + if (callExpression.arguments) { + for (var _i = 0, _a = callExpression.arguments; _i < _a.length; _i++) { + var argument = _a[_i]; + if (isOrContainsMatchingReference(reference, argument)) { + return true; + } } } - if (symbol.flags & 8388608 /* Alias */ && !isInTypeQuery(node) && !isConstEnumOrConstEnumOnlyModule(resolveAlias(symbol))) { - markAliasSymbolAsReferenced(symbol); + if (callExpression.expression.kind === 172 /* PropertyAccessExpression */ && + isOrContainsMatchingReference(reference, callExpression.expression.expression)) { + return true; } - var localOrExportSymbol = getExportSymbolOfValueSymbolIfExported(symbol); - if (localOrExportSymbol.flags & 32 /* Class */) { - var declaration_1 = localOrExportSymbol.valueDeclaration; - // Due to the emit for class decorators, any reference to the class from inside of the class body - // must instead be rewritten to point to a temporary variable to avoid issues with the double-bind - // behavior of class names in ES6. - if (languageVersion === 2 /* ES6 */ - && declaration_1.kind === 221 /* ClassDeclaration */ - && ts.nodeIsDecorated(declaration_1)) { - var container = ts.getContainingClass(node); - while (container !== undefined) { - if (container === declaration_1 && container.name !== node) { - getNodeLinks(declaration_1).flags |= 8388608 /* ClassWithConstructorReference */; - getNodeLinks(node).flags |= 16777216 /* ConstructorReferenceInClass */; - break; - } - container = ts.getContainingClass(container); - } - } - else if (declaration_1.kind === 192 /* ClassExpression */) { - // When we emit a class expression with static members that contain a reference - // to the constructor in the initializer, we will need to substitute that - // binding with an alias as the class name is not in scope. - var container = ts.getThisContainer(node, /*includeArrowFunctions*/ false); - while (container !== undefined) { - if (container.parent === declaration_1) { - if (container.kind === 145 /* PropertyDeclaration */ && ts.hasModifier(container, 32 /* Static */)) { - getNodeLinks(declaration_1).flags |= 8388608 /* ClassWithConstructorReference */; - getNodeLinks(node).flags |= 16777216 /* ConstructorReferenceInClass */; - } - break; - } - container = ts.getThisContainer(container, /*includeArrowFunctions*/ false); - } - } - } - checkCollisionWithCapturedSuperVariable(node, node); - checkCollisionWithCapturedThisVariable(node, node); - checkNestedBlockScopedBinding(node, symbol); - var type = getTypeOfSymbol(localOrExportSymbol); - var declaration = localOrExportSymbol.valueDeclaration; - // We only narrow variables and parameters occurring in a non-assignment position. For all other - // entities we simply return the declared type. - if (!(localOrExportSymbol.flags & 3 /* Variable */) || ts.isAssignmentTarget(node) || !declaration) { - return type; - } - // The declaration container is the innermost function that encloses the declaration of the variable - // or parameter. The flow container is the innermost function starting with which we analyze the control - // flow graph to determine the control flow based type. - var isParameter = ts.getRootDeclaration(declaration).kind === 142 /* Parameter */; - var declarationContainer = getControlFlowContainer(declaration); - var flowContainer = getControlFlowContainer(node); - var isOuterVariable = flowContainer !== declarationContainer; - // When the control flow originates in a function expression or arrow function and we are referencing - // a const variable or parameter from an outer function, we extend the origin of the control flow - // analysis to include the immediately enclosing function. - while (flowContainer !== declarationContainer && - (flowContainer.kind === 179 /* FunctionExpression */ || flowContainer.kind === 180 /* ArrowFunction */) && - (isReadonlySymbol(localOrExportSymbol) || isParameter && !isParameterAssigned(localOrExportSymbol))) { - flowContainer = getControlFlowContainer(flowContainer); - } - // We only look for uninitialized variables in strict null checking mode, and only when we can analyze - // the entire control flow graph from the variable's declaration (i.e. when the flow container and - // declaration container are the same). - var assumeInitialized = !strictNullChecks || (type.flags & 1 /* Any */) !== 0 || isParameter || - isOuterVariable || ts.isInAmbientContext(declaration); - var flowType = getFlowTypeOfReference(node, type, assumeInitialized, flowContainer); - // A variable is considered uninitialized when it is possible to analyze the entire control flow graph - // from declaration to use, and when the variable's declared type doesn't include undefined but the - // control flow based type does include undefined. - if (!assumeInitialized && !(getFalsyFlags(type) & 2048 /* Undefined */) && getFalsyFlags(flowType) & 2048 /* Undefined */) { - error(node, ts.Diagnostics.Variable_0_is_used_before_being_assigned, symbolToString(symbol)); - // Return the declared type to reduce follow-on errors - return type; + return false; + } + function getFlowNodeId(flow) { + if (!flow.id) { + flow.id = nextFlowId; + nextFlowId++; } - return flowType; + return flow.id; } - function isInsideFunction(node, threshold) { - var current = node; - while (current && current !== threshold) { - if (ts.isFunctionLike(current)) { + function typeMaybeAssignableTo(source, target) { + if (!(source.flags & 524288 /* Union */)) { + return isTypeAssignableTo(source, target); + } + for (var _i = 0, _a = source.types; _i < _a.length; _i++) { + var t = _a[_i]; + if (isTypeAssignableTo(t, target)) { return true; } - current = current.parent; } return false; } - function checkNestedBlockScopedBinding(node, symbol) { - if (languageVersion >= 2 /* ES6 */ || - (symbol.flags & (2 /* BlockScopedVariable */ | 32 /* Class */)) === 0 || - symbol.valueDeclaration.parent.kind === 252 /* CatchClause */) { - return; - } - // 1. walk from the use site up to the declaration and check - // if there is anything function like between declaration and use-site (is binding/class is captured in function). - // 2. walk from the declaration up to the boundary of lexical environment and check - // if there is an iteration statement in between declaration and boundary (is binding/class declared inside iteration statement) - var container = ts.getEnclosingBlockScopeContainer(symbol.valueDeclaration); - var usedInFunction = isInsideFunction(node.parent, container); - var current = container; - var containedInIterationStatement = false; - while (current && !ts.nodeStartsNewLexicalEnvironment(current)) { - if (ts.isIterationStatement(current, /*lookInLabeledStatements*/ false)) { - containedInIterationStatement = true; - break; - } - current = current.parent; - } - if (containedInIterationStatement) { - if (usedInFunction) { - // mark iteration statement as containing block-scoped binding captured in some function - getNodeLinks(current).flags |= 65536 /* LoopWithCapturedBlockScopedBinding */; + // Remove those constituent types of declaredType to which no constituent type of assignedType is assignable. + // For example, when a variable of type number | string | boolean is assigned a value of type number | boolean, + // we remove type string. + function getAssignmentReducedType(declaredType, assignedType) { + if (declaredType !== assignedType) { + if (assignedType.flags & 8192 /* Never */) { + return assignedType; } - // mark variables that are declared in loop initializer and reassigned inside the body of ForStatement. - // if body of ForStatement will be converted to function then we'll need a extra machinery to propagate reassigned values back. - if (container.kind === 206 /* ForStatement */ && - ts.getAncestor(symbol.valueDeclaration, 219 /* VariableDeclarationList */).parent === container && - isAssignedInBodyOfForStatement(node, container)) { - getNodeLinks(symbol.valueDeclaration).flags |= 2097152 /* NeedsLoopOutParameter */; + var reducedType = filterType(declaredType, function (t) { return typeMaybeAssignableTo(assignedType, t); }); + if (!(reducedType.flags & 8192 /* Never */)) { + return reducedType; } - // set 'declared inside loop' bit on the block-scoped binding - getNodeLinks(symbol.valueDeclaration).flags |= 262144 /* BlockScopedBindingInLoop */; } - if (usedInFunction) { - getNodeLinks(symbol.valueDeclaration).flags |= 131072 /* CapturedBlockScopedBinding */; + return declaredType; + } + function getTypeFactsOfTypes(types) { + var result = 0 /* None */; + for (var _i = 0, types_11 = types; _i < types_11.length; _i++) { + var t = types_11[_i]; + result |= getTypeFacts(t); } + return result; } - function isAssignedInBodyOfForStatement(node, container) { - var current = node; - // skip parenthesized nodes - while (current.parent.kind === 178 /* ParenthesizedExpression */) { - current = current.parent; + function isFunctionObjectType(type) { + // We do a quick check for a "bind" property before performing the more expensive subtype + // check. This gives us a quicker out in the common case where an object type is not a function. + var resolved = resolveStructuredTypeMembers(type); + return !!(resolved.callSignatures.length || resolved.constructSignatures.length || + resolved.members["bind"] && isTypeSubtypeOf(type, globalFunctionType)); + } + function getTypeFacts(type) { + var flags = type.flags; + if (flags & 2 /* String */) { + return strictNullChecks ? 4079361 /* StringStrictFacts */ : 4194049 /* StringFacts */; } - // check if node is used as LHS in some assignment expression - var isAssigned = false; - if (ts.isAssignmentTarget(current)) { - isAssigned = true; + if (flags & 32 /* StringLiteral */) { + return strictNullChecks ? + type.text === "" ? 3030785 /* EmptyStringStrictFacts */ : 1982209 /* NonEmptyStringStrictFacts */ : + type.text === "" ? 3145473 /* EmptyStringFacts */ : 4194049 /* NonEmptyStringFacts */; } - else if ((current.parent.kind === 185 /* PrefixUnaryExpression */ || current.parent.kind === 186 /* PostfixUnaryExpression */)) { - var expr = current.parent; - isAssigned = expr.operator === 41 /* PlusPlusToken */ || expr.operator === 42 /* MinusMinusToken */; + if (flags & (4 /* Number */ | 16 /* Enum */)) { + return strictNullChecks ? 4079234 /* NumberStrictFacts */ : 4193922 /* NumberFacts */; } - if (!isAssigned) { - return false; + if (flags & (64 /* NumberLiteral */ | 256 /* EnumLiteral */)) { + var isZero = type.text === "0"; + return strictNullChecks ? + isZero ? 3030658 /* ZeroStrictFacts */ : 1982082 /* NonZeroStrictFacts */ : + isZero ? 3145346 /* ZeroFacts */ : 4193922 /* NonZeroFacts */; } - // at this point we know that node is the target of assignment - // now check that modification happens inside the statement part of the ForStatement - while (current !== container) { - if (current === container.statement) { - return true; - } - else { - current = current.parent; - } + if (flags & 8 /* Boolean */) { + return strictNullChecks ? 4078980 /* BooleanStrictFacts */ : 4193668 /* BooleanFacts */; } - return false; - } - function captureLexicalThis(node, container) { - getNodeLinks(node).flags |= 2 /* LexicalThis */; - if (container.kind === 145 /* PropertyDeclaration */ || container.kind === 148 /* Constructor */) { - var classNode = container.parent; - getNodeLinks(classNode).flags |= 4 /* CaptureThis */; + if (flags & 136 /* BooleanLike */) { + return strictNullChecks ? + type === falseType ? 3030404 /* FalseStrictFacts */ : 1981828 /* TrueStrictFacts */ : + type === falseType ? 3145092 /* FalseFacts */ : 4193668 /* TrueFacts */; } - else { - getNodeLinks(container).flags |= 4 /* CaptureThis */; + if (flags & 2588672 /* ObjectType */) { + return isFunctionObjectType(type) ? + strictNullChecks ? 6164448 /* FunctionStrictFacts */ : 8376288 /* FunctionFacts */ : + strictNullChecks ? 6166480 /* ObjectStrictFacts */ : 8378320 /* ObjectFacts */; } - } - function findFirstSuperCall(n) { - if (ts.isSuperCallExpression(n)) { - return n; + if (flags & (1024 /* Void */ | 2048 /* Undefined */)) { + return 2457472 /* UndefinedFacts */; } - else if (ts.isFunctionLike(n)) { - return undefined; + if (flags & 4096 /* Null */) { + return 2340752 /* NullFacts */; } - return ts.forEachChild(n, findFirstSuperCall); + if (flags & 512 /* ESSymbol */) { + return strictNullChecks ? 1981320 /* SymbolStrictFacts */ : 4193160 /* SymbolFacts */; + } + if (flags & 16384 /* TypeParameter */) { + var constraint = getConstraintOfTypeParameter(type); + return getTypeFacts(constraint || emptyObjectType); + } + if (flags & 1572864 /* UnionOrIntersection */) { + return getTypeFactsOfTypes(type.types); + } + return 8388607 /* All */; } - /** - * Return a cached result if super-statement is already found. - * Otherwise, find a super statement in a given constructor function and cache the result in the node-links of the constructor - * - * @param constructor constructor-function to look for super statement - */ - function getSuperCallInConstructor(constructor) { - var links = getNodeLinks(constructor); - // Only trying to find super-call if we haven't yet tried to find one. Once we try, we will record the result - if (links.hasSuperCall === undefined) { - links.superCall = findFirstSuperCall(constructor.body); - links.hasSuperCall = links.superCall ? true : false; + function getTypeWithFacts(type, include) { + return filterType(type, function (t) { return (getTypeFacts(t) & include) !== 0; }); + } + function getTypeWithDefault(type, defaultExpression) { + if (defaultExpression) { + var defaultType = checkExpression(defaultExpression); + return getUnionType([getTypeWithFacts(type, 131072 /* NEUndefined */), defaultType]); } - return links.superCall; + return type; } - /** - * Check if the given class-declaration extends null then return true. - * Otherwise, return false - * @param classDecl a class declaration to check if it extends null - */ - function classDeclarationExtendsNull(classDecl) { - var classSymbol = getSymbolOfNode(classDecl); - var classInstanceType = getDeclaredTypeOfSymbol(classSymbol); - var baseConstructorType = getBaseConstructorTypeOfClass(classInstanceType); - return baseConstructorType === nullWideningType; + function getTypeOfDestructuredProperty(type, name) { + var text = getTextOfPropertyName(name); + return getTypeOfPropertyOfType(type, text) || + isNumericLiteralName(text) && getIndexTypeOfType(type, 1 /* Number */) || + getIndexTypeOfType(type, 0 /* String */) || + unknownType; } - function checkThisExpression(node) { - // Stop at the first arrow function so that we can - // tell whether 'this' needs to be captured. - var container = ts.getThisContainer(node, /* includeArrowFunctions */ true); - var needToCaptureLexicalThis = false; - if (container.kind === 148 /* Constructor */) { - var containingClassDecl = container.parent; - var baseTypeNode = ts.getClassExtendsHeritageClauseElement(containingClassDecl); - // If a containing class does not have extends clause or the class extends null - // skip checking whether super statement is called before "this" accessing. - if (baseTypeNode && !classDeclarationExtendsNull(containingClassDecl)) { - var superCall = getSuperCallInConstructor(container); - // We should give an error in the following cases: - // - No super-call - // - "this" is accessing before super-call. - // i.e super(this) - // this.x; super(); - // We want to make sure that super-call is done before accessing "this" so that - // "this" is not accessed as a parameter of the super-call. - if (!superCall || superCall.end > node.pos) { - // In ES6, super inside constructor of class-declaration has to precede "this" accessing - error(node, ts.Diagnostics.super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class); - } - } + function getTypeOfDestructuredArrayElement(type, index) { + return isTupleLikeType(type) && getTypeOfPropertyOfType(type, "" + index) || + checkIteratedTypeOrElementType(type, /*errorNode*/ undefined, /*allowStringInput*/ false) || + unknownType; + } + function getTypeOfDestructuredSpreadElement(type) { + return createArrayType(checkIteratedTypeOrElementType(type, /*errorNode*/ undefined, /*allowStringInput*/ false) || unknownType); + } + function getAssignedTypeOfBinaryExpression(node) { + return node.parent.kind === 170 /* ArrayLiteralExpression */ || node.parent.kind === 253 /* PropertyAssignment */ ? + getTypeWithDefault(getAssignedType(node), node.right) : + checkExpression(node.right); + } + function getAssignedTypeOfArrayLiteralElement(node, element) { + return getTypeOfDestructuredArrayElement(getAssignedType(node), ts.indexOf(node.elements, element)); + } + function getAssignedTypeOfSpreadElement(node) { + return getTypeOfDestructuredSpreadElement(getAssignedType(node.parent)); + } + function getAssignedTypeOfPropertyAssignment(node) { + return getTypeOfDestructuredProperty(getAssignedType(node.parent), node.name); + } + function getAssignedTypeOfShorthandPropertyAssignment(node) { + return getTypeWithDefault(getAssignedTypeOfPropertyAssignment(node), node.objectAssignmentInitializer); + } + function getAssignedType(node) { + var parent = node.parent; + switch (parent.kind) { + case 207 /* ForInStatement */: + return stringType; + case 208 /* ForOfStatement */: + return checkRightHandSideOfForOf(parent.expression) || unknownType; + case 187 /* BinaryExpression */: + return getAssignedTypeOfBinaryExpression(parent); + case 181 /* DeleteExpression */: + return undefinedType; + case 170 /* ArrayLiteralExpression */: + return getAssignedTypeOfArrayLiteralElement(parent, node); + case 191 /* SpreadElementExpression */: + return getAssignedTypeOfSpreadElement(parent); + case 253 /* PropertyAssignment */: + return getAssignedTypeOfPropertyAssignment(parent); + case 254 /* ShorthandPropertyAssignment */: + return getAssignedTypeOfShorthandPropertyAssignment(parent); } - // Now skip arrow functions to get the "real" owner of 'this'. - if (container.kind === 180 /* ArrowFunction */) { - container = ts.getThisContainer(container, /* includeArrowFunctions */ false); - // When targeting es6, arrow function lexically bind "this" so we do not need to do the work of binding "this" in emitted code - needToCaptureLexicalThis = (languageVersion < 2 /* ES6 */); + return unknownType; + } + function getInitialTypeOfBindingElement(node) { + var pattern = node.parent; + var parentType = getInitialType(pattern.parent); + var type = pattern.kind === 167 /* ObjectBindingPattern */ ? + getTypeOfDestructuredProperty(parentType, node.propertyName || node.name) : + !node.dotDotDotToken ? + getTypeOfDestructuredArrayElement(parentType, ts.indexOf(pattern.elements, node)) : + getTypeOfDestructuredSpreadElement(parentType); + return getTypeWithDefault(type, node.initializer); + } + function getTypeOfInitializer(node) { + // Return the cached type if one is available. If the type of the variable was inferred + // from its initializer, we'll already have cached the type. Otherwise we compute it now + // without caching such that transient types are reflected. + var links = getNodeLinks(node); + return links.resolvedType || checkExpression(node); + } + function getInitialTypeOfVariableDeclaration(node) { + if (node.initializer) { + return getTypeOfInitializer(node.initializer); } - switch (container.kind) { - case 225 /* ModuleDeclaration */: - error(node, ts.Diagnostics.this_cannot_be_referenced_in_a_module_or_namespace_body); - // do not return here so in case if lexical this is captured - it will be reflected in flags on NodeLinks - break; - case 224 /* EnumDeclaration */: - error(node, ts.Diagnostics.this_cannot_be_referenced_in_current_location); - // do not return here so in case if lexical this is captured - it will be reflected in flags on NodeLinks - break; - case 148 /* Constructor */: - if (isInConstructorArgumentInitializer(node, container)) { - error(node, ts.Diagnostics.this_cannot_be_referenced_in_constructor_arguments); - } - break; - case 145 /* PropertyDeclaration */: - case 144 /* PropertySignature */: - if (ts.getModifierFlags(container) & 32 /* Static */) { - error(node, ts.Diagnostics.this_cannot_be_referenced_in_a_static_property_initializer); - } - break; - case 140 /* ComputedPropertyName */: - error(node, ts.Diagnostics.this_cannot_be_referenced_in_a_computed_property_name); - break; + if (node.parent.parent.kind === 207 /* ForInStatement */) { + return stringType; } - if (needToCaptureLexicalThis) { - captureLexicalThis(node, container); + if (node.parent.parent.kind === 208 /* ForOfStatement */) { + return checkRightHandSideOfForOf(node.parent.parent.expression) || unknownType; } - if (ts.isFunctionLike(container) && - (!isInParameterInitializerBeforeContainingFunction(node) || getFunctionLikeThisParameter(container))) { - // Note: a parameter initializer should refer to class-this unless function-this is explicitly annotated. - // If this is a function in a JS file, it might be a class method. Check if it's the RHS - // of a x.prototype.y = function [name]() { .... } - if (container.kind === 179 /* FunctionExpression */ && - ts.isInJavaScriptFile(container.parent) && - ts.getSpecialPropertyAssignmentKind(container.parent) === 3 /* PrototypeProperty */) { - // Get the 'x' of 'x.prototype.y = f' (here, 'f' is 'container') - var className = container.parent // x.prototype.y = f - .left // x.prototype.y - .expression // x.prototype - .expression; // x - var classSymbol = checkExpression(className).symbol; - if (classSymbol && classSymbol.members && (classSymbol.flags & 16 /* Function */)) { - return getInferredClassType(classSymbol); + return unknownType; + } + function getInitialType(node) { + return node.kind === 218 /* VariableDeclaration */ ? + getInitialTypeOfVariableDeclaration(node) : + getInitialTypeOfBindingElement(node); + } + function getInitialOrAssignedType(node) { + return node.kind === 218 /* VariableDeclaration */ || node.kind === 169 /* BindingElement */ ? + getInitialType(node) : + getAssignedType(node); + } + function getReferenceCandidate(node) { + switch (node.kind) { + case 178 /* ParenthesizedExpression */: + return getReferenceCandidate(node.expression); + case 187 /* BinaryExpression */: + switch (node.operatorToken.kind) { + case 56 /* EqualsToken */: + return getReferenceCandidate(node.left); + case 24 /* CommaToken */: + return getReferenceCandidate(node.right); } - } - var thisType = getThisTypeOfDeclaration(container); - if (thisType) { - return thisType; - } - } - if (ts.isClassLike(container.parent)) { - var symbol = getSymbolOfNode(container.parent); - var type = ts.hasModifier(container, 32 /* Static */) ? getTypeOfSymbol(symbol) : getDeclaredTypeOfSymbol(symbol).thisType; - return getFlowTypeOfReference(node, type, /*assumeInitialized*/ true, /*flowContainer*/ undefined); } - if (ts.isInJavaScriptFile(node)) { - var type = getTypeForThisExpressionFromJSDoc(container); - if (type && type !== unknownType) { - return type; - } + return node; + } + function getTypeOfSwitchClause(clause) { + if (clause.kind === 249 /* CaseClause */) { + var caseType = getRegularTypeOfLiteralType(checkExpression(clause.expression)); + return isUnitType(caseType) ? caseType : undefined; } - if (compilerOptions.noImplicitThis) { - // With noImplicitThis, functions may not reference 'this' if it has type 'any' - error(node, ts.Diagnostics.this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation); + return neverType; + } + function getSwitchClauseTypes(switchStatement) { + var links = getNodeLinks(switchStatement); + if (!links.switchTypes) { + // If all case clauses specify expressions that have unit types, we return an array + // of those unit types. Otherwise we return an empty array. + var types = ts.map(switchStatement.caseBlock.clauses, getTypeOfSwitchClause); + links.switchTypes = !ts.contains(types, undefined) ? types : emptyArray; } - return anyType; + return links.switchTypes; } - function getTypeForThisExpressionFromJSDoc(node) { - var typeTag = ts.getJSDocTypeTag(node); - if (typeTag && typeTag.typeExpression && typeTag.typeExpression.type && typeTag.typeExpression.type.kind === 269 /* JSDocFunctionType */) { - var jsDocFunctionType = typeTag.typeExpression.type; - if (jsDocFunctionType.parameters.length > 0 && jsDocFunctionType.parameters[0].type.kind === 272 /* JSDocThisType */) { - return getTypeFromTypeNode(jsDocFunctionType.parameters[0].type); + function eachTypeContainedIn(source, types) { + return source.flags & 524288 /* Union */ ? !ts.forEach(source.types, function (t) { return !ts.contains(types, t); }) : ts.contains(types, source); + } + function isTypeSubsetOf(source, target) { + return source === target || target.flags & 524288 /* Union */ && isTypeSubsetOfUnion(source, target); + } + function isTypeSubsetOfUnion(source, target) { + if (source.flags & 524288 /* Union */) { + for (var _i = 0, _a = source.types; _i < _a.length; _i++) { + var t = _a[_i]; + if (!containsType(target.types, t)) { + return false; + } } + return true; + } + if (source.flags & 256 /* EnumLiteral */ && target.flags & 16 /* Enum */ && source.baseType === target) { + return true; } + return containsType(target.types, source); } - function isInConstructorArgumentInitializer(node, constructorDecl) { - for (var n = node; n && n !== constructorDecl; n = n.parent) { - if (n.kind === 142 /* Parameter */) { - return true; - } + function filterType(type, f) { + if (type.flags & 524288 /* Union */) { + var types = type.types; + var filtered = ts.filter(types, f); + return filtered === types ? type : getUnionTypeFromSortedList(filtered); } - return false; + return f(type) ? type : neverType; } - function checkSuperExpression(node) { - var isCallExpression = node.parent.kind === 174 /* CallExpression */ && node.parent.expression === node; - var container = ts.getSuperContainer(node, /*stopOnFunctions*/ true); - var needToCaptureLexicalThis = false; - if (!isCallExpression) { - // adjust the container reference in case if super is used inside arrow functions with arbitrary deep nesting - while (container && container.kind === 180 /* ArrowFunction */) { - container = ts.getSuperContainer(container, /*stopOnFunctions*/ true); - needToCaptureLexicalThis = languageVersion < 2 /* ES6 */; - } + function mapType(type, f) { + return type.flags & 524288 /* Union */ ? getUnionType(ts.map(type.types, f)) : f(type); + } + function extractTypesOfKind(type, kind) { + return filterType(type, function (t) { return (t.flags & kind) !== 0; }); + } + // Return a new type in which occurrences of the string and number primitive types in + // typeWithPrimitives have been replaced with occurrences of string literals and numeric + // literals in typeWithLiterals, respectively. + function replacePrimitivesWithLiterals(typeWithPrimitives, typeWithLiterals) { + if (isTypeSubsetOf(stringType, typeWithPrimitives) && maybeTypeOfKind(typeWithLiterals, 32 /* StringLiteral */) || + isTypeSubsetOf(numberType, typeWithPrimitives) && maybeTypeOfKind(typeWithLiterals, 64 /* NumberLiteral */)) { + return mapType(typeWithPrimitives, function (t) { + return t.flags & 2 /* String */ ? extractTypesOfKind(typeWithLiterals, 2 /* String */ | 32 /* StringLiteral */) : + t.flags & 4 /* Number */ ? extractTypesOfKind(typeWithLiterals, 4 /* Number */ | 64 /* NumberLiteral */) : + t; + }); } - var canUseSuperExpression = isLegalUsageOfSuperExpression(container); - var nodeCheckFlag = 0; - if (!canUseSuperExpression) { - // issue more specific error if super is used in computed property name - // class A { foo() { return "1" }} - // class B { - // [super.foo()]() {} - // } - var current = node; - while (current && current !== container && current.kind !== 140 /* ComputedPropertyName */) { - current = current.parent; - } - if (current && current.kind === 140 /* ComputedPropertyName */) { - error(node, ts.Diagnostics.super_cannot_be_referenced_in_a_computed_property_name); - } - else if (isCallExpression) { - error(node, ts.Diagnostics.Super_calls_are_not_permitted_outside_constructors_or_in_nested_functions_inside_constructors); - } - else if (!container || !container.parent || !(ts.isClassLike(container.parent) || container.parent.kind === 171 /* ObjectLiteralExpression */)) { - error(node, ts.Diagnostics.super_can_only_be_referenced_in_members_of_derived_classes_or_object_literal_expressions); - } - else { - error(node, ts.Diagnostics.super_property_access_is_permitted_only_in_a_constructor_member_function_or_member_accessor_of_a_derived_class); - } - return unknownType; - } - if ((ts.getModifierFlags(container) & 32 /* Static */) || isCallExpression) { - nodeCheckFlag = 512 /* SuperStatic */; + return typeWithPrimitives; + } + function isIncomplete(flowType) { + return flowType.flags === 0; + } + function getTypeFromFlowType(flowType) { + return flowType.flags === 0 ? flowType.type : flowType; + } + function createFlowType(type, incomplete) { + return incomplete ? { flags: 0, type: type } : type; + } + function getFlowTypeOfReference(reference, declaredType, assumeInitialized, flowContainer) { + var key; + if (!reference.flowNode || assumeInitialized && !(declaredType.flags & 4178943 /* Narrowable */)) { + return declaredType; } - else { - nodeCheckFlag = 256 /* SuperInstance */; + var initialType = assumeInitialized ? declaredType : + declaredType === autoType ? undefinedType : + includeFalsyTypes(declaredType, 2048 /* Undefined */); + var visitedFlowStart = visitedFlowCount; + var result = getTypeFromFlowType(getTypeAtFlowNode(reference.flowNode)); + visitedFlowCount = visitedFlowStart; + if (reference.parent.kind === 196 /* NonNullExpression */ && getTypeWithFacts(result, 524288 /* NEUndefinedOrNull */).flags & 8192 /* Never */) { + return declaredType; } - getNodeLinks(node).flags |= nodeCheckFlag; - // Due to how we emit async functions, we need to specialize the emit for an async method that contains a `super` reference. - // This is due to the fact that we emit the body of an async function inside of a generator function. As generator - // functions cannot reference `super`, we emit a helper inside of the method body, but outside of the generator. This helper - // uses an arrow function, which is permitted to reference `super`. - // - // There are two primary ways we can access `super` from within an async method. The first is getting the value of a property - // or indexed access on super, either as part of a right-hand-side expression or call expression. The second is when setting the value - // of a property or indexed access, either as part of an assignment expression or destructuring assignment. - // - // The simplest case is reading a value, in which case we will emit something like the following: - // - // // ts - // ... - // async asyncMethod() { - // let x = await super.asyncMethod(); - // return x; - // } - // ... - // - // // js - // ... - // asyncMethod() { - // const _super = name => super[name]; - // return __awaiter(this, arguments, Promise, function *() { - // let x = yield _super("asyncMethod").call(this); - // return x; - // }); - // } - // ... - // - // The more complex case is when we wish to assign a value, especially as part of a destructuring assignment. As both cases - // are legal in ES6, but also likely less frequent, we emit the same more complex helper for both scenarios: - // - // // ts - // ... - // async asyncMethod(ar: Promise) { - // [super.a, super.b] = await ar; - // } - // ... - // - // // js - // ... - // asyncMethod(ar) { - // const _super = (function (geti, seti) { - // const cache = Object.create(null); - // return name => cache[name] || (cache[name] = { get value() { return geti(name); }, set value(v) { seti(name, v); } }); - // })(name => super[name], (name, value) => super[name] = value); - // return __awaiter(this, arguments, Promise, function *() { - // [_super("a").value, _super("b").value] = yield ar; - // }); - // } - // ... - // - // This helper creates an object with a "value" property that wraps the `super` property or indexed access for both get and set. - // This is required for destructuring assignments, as a call expression cannot be used as the target of a destructuring assignment - // while a property access can. - if (container.kind === 147 /* MethodDeclaration */ && ts.getModifierFlags(container) & 256 /* Async */) { - if (ts.isSuperProperty(node.parent) && ts.isAssignmentTarget(node.parent)) { - getNodeLinks(container).flags |= 4096 /* AsyncMethodWithSuperBinding */; - } - else { - getNodeLinks(container).flags |= 2048 /* AsyncMethodWithSuper */; + return result; + function getTypeAtFlowNode(flow) { + while (true) { + if (flow.flags & 512 /* Shared */) { + // We cache results of flow type resolution for shared nodes that were previously visited in + // the same getFlowTypeOfReference invocation. A node is considered shared when it is the + // antecedent of more than one node. + for (var i = visitedFlowStart; i < visitedFlowCount; i++) { + if (visitedFlowNodes[i] === flow) { + return visitedFlowTypes[i]; + } + } + } + var type = void 0; + if (flow.flags & 16 /* Assignment */) { + type = getTypeAtFlowAssignment(flow); + if (!type) { + flow = flow.antecedent; + continue; + } + } + else if (flow.flags & 96 /* Condition */) { + type = getTypeAtFlowCondition(flow); + } + else if (flow.flags & 128 /* SwitchClause */) { + type = getTypeAtSwitchClause(flow); + } + else if (flow.flags & 12 /* Label */) { + if (flow.antecedents.length === 1) { + flow = flow.antecedents[0]; + continue; + } + type = flow.flags & 4 /* BranchLabel */ ? + getTypeAtFlowBranchLabel(flow) : + getTypeAtFlowLoopLabel(flow); + } + else if (flow.flags & 2 /* Start */) { + // Check if we should continue with the control flow of the containing function. + var container = flow.container; + if (container && container !== flowContainer && reference.kind !== 172 /* PropertyAccessExpression */) { + flow = container.flowNode; + continue; + } + // At the top of the flow we have the initial type. + type = initialType; + } + else { + // Unreachable code errors are reported in the binding phase. Here we + // simply return the declared type to reduce follow-on errors. + type = declaredType; + } + if (flow.flags & 512 /* Shared */) { + // Record visited node and the associated type in the cache. + visitedFlowNodes[visitedFlowCount] = flow; + visitedFlowTypes[visitedFlowCount] = type; + visitedFlowCount++; + } + return type; } } - if (needToCaptureLexicalThis) { - // call expressions are allowed only in constructors so they should always capture correct 'this' - // super property access expressions can also appear in arrow functions - - // in this case they should also use correct lexical this - captureLexicalThis(node.parent, container); - } - if (container.parent.kind === 171 /* ObjectLiteralExpression */) { - if (languageVersion < 2 /* ES6 */) { - error(node, ts.Diagnostics.super_is_only_allowed_in_members_of_object_literal_expressions_when_option_target_is_ES2015_or_higher); - return unknownType; - } - else { - // for object literal assume that type of 'super' is 'any' - return anyType; + function getTypeAtFlowAssignment(flow) { + var node = flow.node; + // Assignments only narrow the computed type if the declared type is a union type. Thus, we + // only need to evaluate the assigned type if the declared type is a union type. + if (isMatchingReference(reference, node)) { + if (node.parent.kind === 185 /* PrefixUnaryExpression */ || node.parent.kind === 186 /* PostfixUnaryExpression */) { + var flowType = getTypeAtFlowNode(flow.antecedent); + return createFlowType(getBaseTypeOfLiteralType(getTypeFromFlowType(flowType)), isIncomplete(flowType)); + } + return declaredType === autoType ? getBaseTypeOfLiteralType(getInitialOrAssignedType(node)) : + declaredType.flags & 524288 /* Union */ ? getAssignmentReducedType(declaredType, getInitialOrAssignedType(node)) : + declaredType; } - } - // at this point the only legal case for parent is ClassLikeDeclaration - var classLikeDeclaration = container.parent; - var classType = getDeclaredTypeOfSymbol(getSymbolOfNode(classLikeDeclaration)); - var baseClassType = classType && getBaseTypes(classType)[0]; - if (!baseClassType) { - if (!ts.getClassExtendsHeritageClauseElement(classLikeDeclaration)) { - error(node, ts.Diagnostics.super_can_only_be_referenced_in_a_derived_class); + // We didn't have a direct match. However, if the reference is a dotted name, this + // may be an assignment to a left hand part of the reference. For example, for a + // reference 'x.y.z', we may be at an assignment to 'x.y' or 'x'. In that case, + // return the declared type. + if (containsMatchingReference(reference, node)) { + return declaredType; } - return unknownType; - } - if (container.kind === 148 /* Constructor */ && isInConstructorArgumentInitializer(node, container)) { - // issue custom error message for super property access in constructor arguments (to be aligned with old compiler) - error(node, ts.Diagnostics.super_cannot_be_referenced_in_constructor_arguments); - return unknownType; + // Assignment doesn't affect reference + return undefined; } - return nodeCheckFlag === 512 /* SuperStatic */ - ? getBaseConstructorTypeOfClass(classType) - : getTypeWithThisArgument(baseClassType, classType.thisType); - function isLegalUsageOfSuperExpression(container) { - if (!container) { - return false; - } - if (isCallExpression) { - // TS 1.0 SPEC (April 2014): 4.8.1 - // Super calls are only permitted in constructors of derived classes - return container.kind === 148 /* Constructor */; - } - else { - // TS 1.0 SPEC (April 2014) - // 'super' property access is allowed - // - In a constructor, instance member function, instance member accessor, or instance member variable initializer where this references a derived class instance - // - In a static member function or static member accessor - // topmost container must be something that is directly nested in the class declaration\object literal expression - if (ts.isClassLike(container.parent) || container.parent.kind === 171 /* ObjectLiteralExpression */) { - if (ts.getModifierFlags(container) & 32 /* Static */) { - return container.kind === 147 /* MethodDeclaration */ || - container.kind === 146 /* MethodSignature */ || - container.kind === 149 /* GetAccessor */ || - container.kind === 150 /* SetAccessor */; - } - else { - return container.kind === 147 /* MethodDeclaration */ || - container.kind === 146 /* MethodSignature */ || - container.kind === 149 /* GetAccessor */ || - container.kind === 150 /* SetAccessor */ || - container.kind === 145 /* PropertyDeclaration */ || - container.kind === 144 /* PropertySignature */ || - container.kind === 148 /* Constructor */; - } + function getTypeAtFlowCondition(flow) { + var flowType = getTypeAtFlowNode(flow.antecedent); + var type = getTypeFromFlowType(flowType); + if (!(type.flags & 8192 /* Never */)) { + // If we have an antecedent type (meaning we're reachable in some way), we first + // attempt to narrow the antecedent type. If that produces the never type, and if + // the antecedent type is incomplete (i.e. a transient type in a loop), then we + // take the type guard as an indication that control *could* reach here once we + // have the complete type. We proceed by switching to the silent never type which + // doesn't report errors when operators are applied to it. Note that this is the + // *only* place a silent never type is ever generated. + var assumeTrue = (flow.flags & 32 /* TrueCondition */) !== 0; + type = narrowType(type, flow.expression, assumeTrue); + if (type.flags & 8192 /* Never */ && isIncomplete(flowType)) { + type = silentNeverType; } } - return false; + return createFlowType(type, isIncomplete(flowType)); } - } - function getContextualThisParameter(func) { - if (isContextSensitiveFunctionOrObjectLiteralMethod(func) && func.kind !== 180 /* ArrowFunction */) { - var contextualSignature = getContextualSignature(func); - if (contextualSignature) { - return contextualSignature.thisParameter; + function getTypeAtSwitchClause(flow) { + var flowType = getTypeAtFlowNode(flow.antecedent); + var type = getTypeFromFlowType(flowType); + var expr = flow.switchStatement.expression; + if (isMatchingReference(reference, expr)) { + type = narrowTypeBySwitchOnDiscriminant(type, flow.switchStatement, flow.clauseStart, flow.clauseEnd); + } + else if (isMatchingReferenceDiscriminant(expr)) { + type = narrowTypeByDiscriminant(type, expr, function (t) { return narrowTypeBySwitchOnDiscriminant(t, flow.switchStatement, flow.clauseStart, flow.clauseEnd); }); } + return createFlowType(type, isIncomplete(flowType)); } - return undefined; - } - // Return contextual type of parameter or undefined if no contextual type is available - function getContextuallyTypedParameterType(parameter) { - var func = parameter.parent; - if (isContextSensitiveFunctionOrObjectLiteralMethod(func)) { - var iife = ts.getImmediatelyInvokedFunctionExpression(func); - if (iife) { - var indexOfParameter = ts.indexOf(func.parameters, parameter); - if (iife.arguments && indexOfParameter < iife.arguments.length) { - if (parameter.dotDotDotToken) { - var restTypes = []; - for (var i = indexOfParameter; i < iife.arguments.length; i++) { - restTypes.push(getWidenedLiteralType(checkExpression(iife.arguments[i]))); - } - return createArrayType(getUnionType(restTypes)); - } - var links = getNodeLinks(iife); - var cached = links.resolvedSignature; - links.resolvedSignature = anySignature; - var type = getWidenedLiteralType(checkExpression(iife.arguments[indexOfParameter])); - links.resolvedSignature = cached; + function getTypeAtFlowBranchLabel(flow) { + var antecedentTypes = []; + var subtypeReduction = false; + var seenIncomplete = false; + for (var _i = 0, _a = flow.antecedents; _i < _a.length; _i++) { + var antecedent = _a[_i]; + var flowType = getTypeAtFlowNode(antecedent); + var type = getTypeFromFlowType(flowType); + // If the type at a particular antecedent path is the declared type and the + // reference is known to always be assigned (i.e. when declared and initial types + // are the same), there is no reason to process more antecedents since the only + // possible outcome is subtypes that will be removed in the final union type anyway. + if (type === declaredType && declaredType === initialType) { return type; } - } - var contextualSignature = getContextualSignature(func); - if (contextualSignature) { - var funcHasRestParameters = ts.hasRestParameter(func); - var len = func.parameters.length - (funcHasRestParameters ? 1 : 0); - var indexOfParameter = ts.indexOf(func.parameters, parameter); - if (indexOfParameter < len) { - return getTypeAtPosition(contextualSignature, indexOfParameter); + if (!ts.contains(antecedentTypes, type)) { + antecedentTypes.push(type); } - // If last parameter is contextually rest parameter get its type - if (funcHasRestParameters && - indexOfParameter === (func.parameters.length - 1) && - isRestParameterIndex(contextualSignature, func.parameters.length - 1)) { - return getTypeOfSymbol(ts.lastOrUndefined(contextualSignature.parameters)); + // If an antecedent type is not a subset of the declared type, we need to perform + // subtype reduction. This happens when a "foreign" type is injected into the control + // flow using the instanceof operator or a user defined type predicate. + if (!isTypeSubsetOf(type, declaredType)) { + subtypeReduction = true; + } + if (isIncomplete(flowType)) { + seenIncomplete = true; } } + return createFlowType(getUnionType(antecedentTypes, subtypeReduction), seenIncomplete); } - return undefined; - } - // In a variable, parameter or property declaration with a type annotation, - // the contextual type of an initializer expression is the type of the variable, parameter or property. - // Otherwise, in a parameter declaration of a contextually typed function expression, - // the contextual type of an initializer expression is the contextual type of the parameter. - // Otherwise, in a variable or parameter declaration with a binding pattern name, - // the contextual type of an initializer expression is the type implied by the binding pattern. - // Otherwise, in a binding pattern inside a variable or parameter declaration, - // the contextual type of an initializer expression is the type annotation of the containing declaration, if present. - function getContextualTypeForInitializerExpression(node) { - var declaration = node.parent; - if (node === declaration.initializer) { - if (declaration.type) { - return getTypeFromTypeNode(declaration.type); + function getTypeAtFlowLoopLabel(flow) { + // If we have previously computed the control flow type for the reference at + // this flow loop junction, return the cached type. + var id = getFlowNodeId(flow); + var cache = flowLoopCaches[id] || (flowLoopCaches[id] = ts.createMap()); + if (!key) { + key = getFlowCacheKey(reference); } - if (declaration.kind === 142 /* Parameter */) { - var type = getContextuallyTypedParameterType(declaration); - if (type) { - return type; - } + if (cache[key]) { + return cache[key]; } - if (ts.isBindingPattern(declaration.name)) { - return getTypeFromBindingPattern(declaration.name, /*includePatternInType*/ true, /*reportErrors*/ false); + // If this flow loop junction and reference are already being processed, return + // the union of the types computed for each branch so far, marked as incomplete. + // We should never see an empty array here because the first antecedent of a loop + // junction is always the non-looping control flow path that leads to the top. + for (var i = flowLoopStart; i < flowLoopCount; i++) { + if (flowLoopNodes[i] === flow && flowLoopKeys[i] === key) { + return createFlowType(getUnionType(flowLoopTypes[i]), /*incomplete*/ true); + } } - if (ts.isBindingPattern(declaration.parent)) { - var parentDeclaration = declaration.parent.parent; - var name_15 = declaration.propertyName || declaration.name; - if (ts.isVariableLike(parentDeclaration) && - parentDeclaration.type && - !ts.isBindingPattern(name_15)) { - var text = getTextOfPropertyName(name_15); - if (text) { - return getTypeOfPropertyOfType(getTypeFromTypeNode(parentDeclaration.type), text); - } + // Add the flow loop junction and reference to the in-process stack and analyze + // each antecedent code path. + var antecedentTypes = []; + var subtypeReduction = false; + var firstAntecedentType; + flowLoopNodes[flowLoopCount] = flow; + flowLoopKeys[flowLoopCount] = key; + flowLoopTypes[flowLoopCount] = antecedentTypes; + for (var _i = 0, _a = flow.antecedents; _i < _a.length; _i++) { + var antecedent = _a[_i]; + flowLoopCount++; + var flowType = getTypeAtFlowNode(antecedent); + flowLoopCount--; + if (!firstAntecedentType) { + firstAntecedentType = flowType; + } + var type = getTypeFromFlowType(flowType); + // If we see a value appear in the cache it is a sign that control flow analysis + // was restarted and completed by checkExpressionCached. We can simply pick up + // the resulting type and bail out. + if (cache[key]) { + return cache[key]; + } + if (!ts.contains(antecedentTypes, type)) { + antecedentTypes.push(type); + } + // If an antecedent type is not a subset of the declared type, we need to perform + // subtype reduction. This happens when a "foreign" type is injected into the control + // flow using the instanceof operator or a user defined type predicate. + if (!isTypeSubsetOf(type, declaredType)) { + subtypeReduction = true; + } + // If the type at a particular antecedent path is the declared type there is no + // reason to process more antecedents since the only possible outcome is subtypes + // that will be removed in the final union type anyway. + if (type === declaredType) { + break; } } - } - return undefined; - } - function getContextualTypeForReturnExpression(node) { - var func = ts.getContainingFunction(node); - if (ts.isAsyncFunctionLike(func)) { - var contextualReturnType = getContextualReturnType(func); - if (contextualReturnType) { - return getPromisedType(contextualReturnType); + // The result is incomplete if the first antecedent (the non-looping control flow path) + // is incomplete. + var result = getUnionType(antecedentTypes, subtypeReduction); + if (isIncomplete(firstAntecedentType)) { + return createFlowType(result, /*incomplete*/ true); } - return undefined; + return cache[key] = result; } - if (func && !func.asteriskToken) { - return getContextualReturnType(func); + function isMatchingReferenceDiscriminant(expr) { + return expr.kind === 172 /* PropertyAccessExpression */ && + declaredType.flags & 524288 /* Union */ && + isMatchingReference(reference, expr.expression) && + isDiscriminantProperty(declaredType, expr.name.text); } - return undefined; - } - function getContextualTypeForYieldOperand(node) { - var func = ts.getContainingFunction(node); - if (func) { - var contextualReturnType = getContextualReturnType(func); - if (contextualReturnType) { - return node.asteriskToken - ? contextualReturnType - : getElementTypeOfIterableIterator(contextualReturnType); + function narrowTypeByDiscriminant(type, propAccess, narrowType) { + var propName = propAccess.name.text; + var propType = getTypeOfPropertyOfType(type, propName); + var narrowedPropType = propType && narrowType(propType); + return propType === narrowedPropType ? type : filterType(type, function (t) { return isTypeComparableTo(getTypeOfPropertyOfType(t, propName), narrowedPropType); }); + } + function narrowTypeByTruthiness(type, expr, assumeTrue) { + if (isMatchingReference(reference, expr)) { + return getTypeWithFacts(type, assumeTrue ? 1048576 /* Truthy */ : 2097152 /* Falsy */); + } + if (isMatchingReferenceDiscriminant(expr)) { + return narrowTypeByDiscriminant(type, expr, function (t) { return getTypeWithFacts(t, assumeTrue ? 1048576 /* Truthy */ : 2097152 /* Falsy */); }); } + if (containsMatchingReferenceDiscriminant(reference, expr)) { + return declaredType; + } + return type; } - return undefined; - } - function isInParameterInitializerBeforeContainingFunction(node) { - while (node.parent && !ts.isFunctionLike(node.parent)) { - if (node.parent.kind === 142 /* Parameter */ && node.parent.initializer === node) { - return true; + function narrowTypeByBinaryExpression(type, expr, assumeTrue) { + switch (expr.operatorToken.kind) { + case 56 /* EqualsToken */: + return narrowTypeByTruthiness(type, expr.left, assumeTrue); + case 30 /* EqualsEqualsToken */: + case 31 /* ExclamationEqualsToken */: + case 32 /* EqualsEqualsEqualsToken */: + case 33 /* ExclamationEqualsEqualsToken */: + var operator_1 = expr.operatorToken.kind; + var left_1 = getReferenceCandidate(expr.left); + var right_1 = getReferenceCandidate(expr.right); + if (left_1.kind === 182 /* TypeOfExpression */ && right_1.kind === 9 /* StringLiteral */) { + return narrowTypeByTypeof(type, left_1, operator_1, right_1, assumeTrue); + } + if (right_1.kind === 182 /* TypeOfExpression */ && left_1.kind === 9 /* StringLiteral */) { + return narrowTypeByTypeof(type, right_1, operator_1, left_1, assumeTrue); + } + if (isMatchingReference(reference, left_1)) { + return narrowTypeByEquality(type, operator_1, right_1, assumeTrue); + } + if (isMatchingReference(reference, right_1)) { + return narrowTypeByEquality(type, operator_1, left_1, assumeTrue); + } + if (isMatchingReferenceDiscriminant(left_1)) { + return narrowTypeByDiscriminant(type, left_1, function (t) { return narrowTypeByEquality(t, operator_1, right_1, assumeTrue); }); + } + if (isMatchingReferenceDiscriminant(right_1)) { + return narrowTypeByDiscriminant(type, right_1, function (t) { return narrowTypeByEquality(t, operator_1, left_1, assumeTrue); }); + } + if (containsMatchingReferenceDiscriminant(reference, left_1) || containsMatchingReferenceDiscriminant(reference, right_1)) { + return declaredType; + } + break; + case 91 /* InstanceOfKeyword */: + return narrowTypeByInstanceof(type, expr, assumeTrue); + case 24 /* CommaToken */: + return narrowType(type, expr.right, assumeTrue); } - node = node.parent; + return type; } - return false; - } - function getContextualReturnType(functionDecl) { - // If the containing function has a return type annotation, is a constructor, or is a get accessor whose - // corresponding set accessor has a type annotation, return statements in the function are contextually typed - if (functionDecl.type || - functionDecl.kind === 148 /* Constructor */ || - functionDecl.kind === 149 /* GetAccessor */ && ts.getSetAccessorTypeAnnotationNode(ts.getDeclarationOfKind(functionDecl.symbol, 150 /* SetAccessor */))) { - return getReturnTypeOfSignature(getSignatureFromDeclaration(functionDecl)); - } - // Otherwise, if the containing function is contextually typed by a function type with exactly one call signature - // and that call signature is non-generic, return statements are contextually typed by the return type of the signature - var signature = getContextualSignatureForFunctionLikeDeclaration(functionDecl); - if (signature) { - return getReturnTypeOfSignature(signature); - } - return undefined; - } - // In a typed function call, an argument or substitution expression is contextually typed by the type of the corresponding parameter. - function getContextualTypeForArgument(callTarget, arg) { - var args = getEffectiveCallArguments(callTarget); - var argIndex = ts.indexOf(args, arg); - if (argIndex >= 0) { - var signature = getResolvedOrAnySignature(callTarget); - return getTypeAtPosition(signature, argIndex); + function narrowTypeByEquality(type, operator, value, assumeTrue) { + if (type.flags & 1 /* Any */) { + return type; + } + if (operator === 31 /* ExclamationEqualsToken */ || operator === 33 /* ExclamationEqualsEqualsToken */) { + assumeTrue = !assumeTrue; + } + var valueType = checkExpression(value); + if (valueType.flags & 6144 /* Nullable */) { + if (!strictNullChecks) { + return type; + } + var doubleEquals = operator === 30 /* EqualsEqualsToken */ || operator === 31 /* ExclamationEqualsToken */; + var facts = doubleEquals ? + assumeTrue ? 65536 /* EQUndefinedOrNull */ : 524288 /* NEUndefinedOrNull */ : + value.kind === 93 /* NullKeyword */ ? + assumeTrue ? 32768 /* EQNull */ : 262144 /* NENull */ : + assumeTrue ? 16384 /* EQUndefined */ : 131072 /* NEUndefined */; + return getTypeWithFacts(type, facts); + } + if (type.flags & 2589185 /* NotUnionOrUnit */) { + return type; + } + if (assumeTrue) { + var narrowedType = filterType(type, function (t) { return areTypesComparable(t, valueType); }); + return narrowedType.flags & 8192 /* Never */ ? type : replacePrimitivesWithLiterals(narrowedType, valueType); + } + if (isUnitType(valueType)) { + var regularType_1 = getRegularTypeOfLiteralType(valueType); + return filterType(type, function (t) { return getRegularTypeOfLiteralType(t) !== regularType_1; }); + } + return type; } - return undefined; - } - function getContextualTypeForSubstitutionExpression(template, substitutionExpression) { - if (template.parent.kind === 176 /* TaggedTemplateExpression */) { - return getContextualTypeForArgument(template.parent, substitutionExpression); + function narrowTypeByTypeof(type, typeOfExpr, operator, literal, assumeTrue) { + // We have '==', '!=', '====', or !==' operator with 'typeof xxx' and string literal operands + var target = getReferenceCandidate(typeOfExpr.expression); + if (!isMatchingReference(reference, target)) { + // For a reference of the form 'x.y', a 'typeof x === ...' type guard resets the + // narrowed type of 'y' to its declared type. + if (containsMatchingReference(reference, target)) { + return declaredType; + } + return type; + } + if (operator === 31 /* ExclamationEqualsToken */ || operator === 33 /* ExclamationEqualsEqualsToken */) { + assumeTrue = !assumeTrue; + } + if (assumeTrue && !(type.flags & 524288 /* Union */)) { + // We narrow a non-union type to an exact primitive type if the non-union type + // is a supertype of that primitive type. For example, type 'any' can be narrowed + // to one of the primitive types. + var targetType = typeofTypesByName[literal.text]; + if (targetType && isTypeSubtypeOf(targetType, type)) { + return targetType; + } + } + var facts = assumeTrue ? + typeofEQFacts[literal.text] || 64 /* TypeofEQHostObject */ : + typeofNEFacts[literal.text] || 8192 /* TypeofNEHostObject */; + return getTypeWithFacts(type, facts); } - return undefined; - } - function getContextualTypeForBinaryOperand(node) { - var binaryExpression = node.parent; - var operator = binaryExpression.operatorToken.kind; - if (operator >= 56 /* FirstAssignment */ && operator <= 68 /* LastAssignment */) { - // Don't do this for special property assignments to avoid circularity - if (ts.getSpecialPropertyAssignmentKind(binaryExpression) !== 0 /* None */) { - return undefined; + function narrowTypeBySwitchOnDiscriminant(type, switchStatement, clauseStart, clauseEnd) { + // We only narrow if all case expressions specify values with unit types + var switchTypes = getSwitchClauseTypes(switchStatement); + if (!switchTypes.length) { + return type; } - // In an assignment expression, the right operand is contextually typed by the type of the left operand. - if (node === binaryExpression.right) { - return checkExpression(binaryExpression.left); + var clauseTypes = switchTypes.slice(clauseStart, clauseEnd); + var hasDefaultClause = clauseStart === clauseEnd || ts.contains(clauseTypes, neverType); + var discriminantType = getUnionType(clauseTypes); + var caseType = discriminantType.flags & 8192 /* Never */ ? neverType : + replacePrimitivesWithLiterals(filterType(type, function (t) { return isTypeComparableTo(discriminantType, t); }), discriminantType); + if (!hasDefaultClause) { + return caseType; } + var defaultType = filterType(type, function (t) { return !(isUnitType(t) && ts.contains(switchTypes, getRegularTypeOfLiteralType(t))); }); + return caseType.flags & 8192 /* Never */ ? defaultType : getUnionType([caseType, defaultType]); } - else if (operator === 52 /* BarBarToken */) { - // When an || expression has a contextual type, the operands are contextually typed by that type. When an || - // expression has no contextual type, the right operand is contextually typed by the type of the left operand. - var type = getContextualType(binaryExpression); - if (!type && node === binaryExpression.right) { - type = checkExpression(binaryExpression.left); + function narrowTypeByInstanceof(type, expr, assumeTrue) { + var left = getReferenceCandidate(expr.left); + if (!isMatchingReference(reference, left)) { + // For a reference of the form 'x.y', an 'x instanceof T' type guard resets the + // narrowed type of 'y' to its declared type. + if (containsMatchingReference(reference, left)) { + return declaredType; + } + return type; + } + // Check that right operand is a function type with a prototype property + var rightType = checkExpression(expr.right); + if (!isTypeSubtypeOf(rightType, globalFunctionType)) { + return type; + } + var targetType; + var prototypeProperty = getPropertyOfType(rightType, "prototype"); + if (prototypeProperty) { + // Target type is type of the prototype property + var prototypePropertyType = getTypeOfSymbol(prototypeProperty); + if (!isTypeAny(prototypePropertyType)) { + targetType = prototypePropertyType; + } + } + // Don't narrow from 'any' if the target type is exactly 'Object' or 'Function' + if (isTypeAny(type) && (targetType === globalObjectType || targetType === globalFunctionType)) { + return type; + } + if (!targetType) { + // Target type is type of construct signature + var constructSignatures = void 0; + if (rightType.flags & 65536 /* Interface */) { + constructSignatures = resolveDeclaredMembers(rightType).declaredConstructSignatures; + } + else if (rightType.flags & 2097152 /* Anonymous */) { + constructSignatures = getSignaturesOfType(rightType, 1 /* Construct */); + } + if (constructSignatures && constructSignatures.length) { + targetType = getUnionType(ts.map(constructSignatures, function (signature) { return getReturnTypeOfSignature(getErasedSignature(signature)); })); + } + } + if (targetType) { + return getNarrowedType(type, targetType, assumeTrue); } return type; } - else if (operator === 51 /* AmpersandAmpersandToken */ || operator === 24 /* CommaToken */) { - if (node === binaryExpression.right) { - return getContextualType(binaryExpression); + function getNarrowedType(type, candidate, assumeTrue) { + if (!assumeTrue) { + return filterType(type, function (t) { return !isTypeInstanceOf(t, candidate); }); } - } - return undefined; - } - // Apply a mapping function to a contextual type and return the resulting type. If the contextual type - // is a union type, the mapping function is applied to each constituent type and a union of the resulting - // types is returned. - function applyToContextualType(type, mapper) { - if (!(type.flags & 524288 /* Union */)) { - return mapper(type); - } - var types = type.types; - var mappedType; - var mappedTypes; - for (var _i = 0, types_12 = types; _i < types_12.length; _i++) { - var current = types_12[_i]; - var t = mapper(current); - if (t) { - if (!mappedType) { - mappedType = t; + // If the current type is a union type, remove all constituents that couldn't be instances of + // the candidate type. If one or more constituents remain, return a union of those. + if (type.flags & 524288 /* Union */) { + var assignableType = filterType(type, function (t) { return isTypeInstanceOf(t, candidate); }); + if (!(assignableType.flags & 8192 /* Never */)) { + return assignableType; } - else if (!mappedTypes) { - mappedTypes = [mappedType, t]; + } + // If the candidate type is a subtype of the target type, narrow to the candidate type. + // Otherwise, if the target type is assignable to the candidate type, keep the target type. + // Otherwise, if the candidate type is assignable to the target type, narrow to the candidate + // type. Otherwise, the types are completely unrelated, so narrow to an intersection of the + // two types. + var targetType = type.flags & 16384 /* TypeParameter */ ? getApparentType(type) : type; + return isTypeSubtypeOf(candidate, type) ? candidate : + isTypeAssignableTo(type, candidate) ? type : + isTypeAssignableTo(candidate, targetType) ? candidate : + getIntersectionType([type, candidate]); + } + function narrowTypeByTypePredicate(type, callExpression, assumeTrue) { + if (!hasMatchingArgument(callExpression, reference)) { + return type; + } + var signature = getResolvedSignature(callExpression); + var predicate = signature.typePredicate; + if (!predicate) { + return type; + } + // Don't narrow from 'any' if the predicate type is exactly 'Object' or 'Function' + if (isTypeAny(type) && (predicate.type === globalObjectType || predicate.type === globalFunctionType)) { + return type; + } + if (ts.isIdentifierTypePredicate(predicate)) { + var predicateArgument = callExpression.arguments[predicate.parameterIndex]; + if (predicateArgument) { + if (isMatchingReference(reference, predicateArgument)) { + return getNarrowedType(type, predicate.type, assumeTrue); + } + if (containsMatchingReference(reference, predicateArgument)) { + return declaredType; + } } - else { - mappedTypes.push(t); + } + else { + var invokedExpression = skipParenthesizedNodes(callExpression.expression); + if (invokedExpression.kind === 173 /* ElementAccessExpression */ || invokedExpression.kind === 172 /* PropertyAccessExpression */) { + var accessExpression = invokedExpression; + var possibleReference = skipParenthesizedNodes(accessExpression.expression); + if (isMatchingReference(reference, possibleReference)) { + return getNarrowedType(type, predicate.type, assumeTrue); + } + if (containsMatchingReference(reference, possibleReference)) { + return declaredType; + } } } + return type; } - return mappedTypes ? getUnionType(mappedTypes) : mappedType; - } - function getTypeOfPropertyOfContextualType(type, name) { - return applyToContextualType(type, function (t) { - var prop = t.flags & 4161536 /* StructuredType */ ? getPropertyOfType(t, name) : undefined; - return prop ? getTypeOfSymbol(prop) : undefined; - }); - } - function getIndexTypeOfContextualType(type, kind) { - return applyToContextualType(type, function (t) { return getIndexTypeOfStructuredType(t, kind); }); - } - // Return true if the given contextual type is a tuple-like type - function contextualTypeIsTupleLikeType(type) { - return !!(type.flags & 524288 /* Union */ ? ts.forEach(type.types, isTupleLikeType) : isTupleLikeType(type)); - } - // In an object literal contextually typed by a type T, the contextual type of a property assignment is the type of - // the matching property in T, if one exists. Otherwise, it is the type of the numeric index signature in T, if one - // exists. Otherwise, it is the type of the string index signature in T, if one exists. - function getContextualTypeForObjectLiteralMethod(node) { - ts.Debug.assert(ts.isObjectLiteralMethod(node)); - if (isInsideWithStatementBody(node)) { - // We cannot answer semantic questions within a with block, do not proceed any further - return undefined; + // Narrow the given type based on the given expression having the assumed boolean value. The returned type + // will be a subtype or the same type as the argument. + function narrowType(type, expr, assumeTrue) { + switch (expr.kind) { + case 69 /* Identifier */: + case 97 /* ThisKeyword */: + case 172 /* PropertyAccessExpression */: + return narrowTypeByTruthiness(type, expr, assumeTrue); + case 174 /* CallExpression */: + return narrowTypeByTypePredicate(type, expr, assumeTrue); + case 178 /* ParenthesizedExpression */: + return narrowType(type, expr.expression, assumeTrue); + case 187 /* BinaryExpression */: + return narrowTypeByBinaryExpression(type, expr, assumeTrue); + case 185 /* PrefixUnaryExpression */: + if (expr.operator === 49 /* ExclamationToken */) { + return narrowType(type, expr.operand, !assumeTrue); + } + break; + } + return type; } - return getContextualTypeForObjectLiteralElement(node); } - function getContextualTypeForObjectLiteralElement(element) { - var objectLiteral = element.parent; - var type = getApparentTypeOfContextualType(objectLiteral); - if (type) { - if (!ts.hasDynamicName(element)) { - // For a (non-symbol) computed property, there is no reason to look up the name - // in the type. It will just be "__computed", which does not appear in any - // SymbolTable. - var symbolName = getSymbolOfNode(element).name; - var propertyType = getTypeOfPropertyOfContextualType(type, symbolName); - if (propertyType) { - return propertyType; + function getTypeOfSymbolAtLocation(symbol, location) { + // If we have an identifier or a property access at the given location, if the location is + // an dotted name expression, and if the location is not an assignment target, obtain the type + // of the expression (which will reflect control flow analysis). If the expression indeed + // resolved to the given symbol, return the narrowed type. + if (location.kind === 69 /* Identifier */) { + if (ts.isRightSideOfQualifiedNameOrPropertyAccess(location)) { + location = location.parent; + } + if (ts.isPartOfExpression(location) && !ts.isAssignmentTarget(location)) { + var type = checkExpression(location); + if (getExportSymbolOfValueSymbolIfExported(getNodeLinks(location).resolvedSymbol) === symbol) { + return type; } } - return isNumericName(element.name) && getIndexTypeOfContextualType(type, 1 /* Number */) || - getIndexTypeOfContextualType(type, 0 /* String */); } - return undefined; + // The location isn't a reference to the given symbol, meaning we're being asked + // a hypothetical question of what type the symbol would have if there was a reference + // to it at the given location. Since we have no control flow information for the + // hypothetical reference (control flow information is created and attached by the + // binder), we simply return the declared type of the symbol. + return getTypeOfSymbol(symbol); } - // In an array literal contextually typed by a type T, the contextual type of an element expression at index N is - // the type of the property with the numeric name N in T, if one exists. Otherwise, if T has a numeric index signature, - // it is the type of the numeric index signature in T. Otherwise, in ES6 and higher, the contextual type is the iterated - // type of T. - function getContextualTypeForElementExpression(node) { - var arrayLiteral = node.parent; - var type = getApparentTypeOfContextualType(arrayLiteral); - if (type) { - var index = ts.indexOf(arrayLiteral.elements, node); - return getTypeOfPropertyOfContextualType(type, "" + index) - || getIndexTypeOfContextualType(type, 1 /* Number */) - || (languageVersion >= 2 /* ES6 */ ? getElementTypeOfIterable(type, /*errorNode*/ undefined) : undefined); + function skipParenthesizedNodes(expression) { + while (expression.kind === 178 /* ParenthesizedExpression */) { + expression = expression.expression; } - return undefined; - } - // In a contextually typed conditional expression, the true/false expressions are contextually typed by the same type. - function getContextualTypeForConditionalOperand(node) { - var conditional = node.parent; - return node === conditional.whenTrue || node === conditional.whenFalse ? getContextualType(conditional) : undefined; + return expression; } - function getContextualTypeForJsxAttribute(attribute) { - var kind = attribute.kind; - var jsxElement = attribute.parent; - var attrsType = getJsxElementAttributesType(jsxElement); - if (attribute.kind === 246 /* JsxAttribute */) { - if (!attrsType || isTypeAny(attrsType)) { - return undefined; + function getControlFlowContainer(node) { + while (true) { + node = node.parent; + if (ts.isFunctionLike(node) && !ts.getImmediatelyInvokedFunctionExpression(node) || + node.kind === 226 /* ModuleBlock */ || + node.kind === 256 /* SourceFile */ || + node.kind === 145 /* PropertyDeclaration */) { + return node; } - return getTypeOfPropertyOfType(attrsType, attribute.name.text); - } - else if (attribute.kind === 247 /* JsxSpreadAttribute */) { - return attrsType; } - ts.Debug.fail("Expected JsxAttribute or JsxSpreadAttribute, got ts.SyntaxKind[" + kind + "]"); - } - // Return the contextual type for a given expression node. During overload resolution, a contextual type may temporarily - // be "pushed" onto a node using the contextualType property. - function getApparentTypeOfContextualType(node) { - var type = getContextualType(node); - return type && getApparentType(type); } - /** - * Woah! Do you really want to use this function? - * - * Unless you're trying to get the *non-apparent* type for a - * value-literal type or you're authoring relevant portions of this algorithm, - * you probably meant to use 'getApparentTypeOfContextualType'. - * Otherwise this may not be very useful. - * - * In cases where you *are* working on this function, you should understand - * when it is appropriate to use 'getContextualType' and 'getApparentTypeOfContextualType'. - * - * - Use 'getContextualType' when you are simply going to propagate the result to the expression. - * - Use 'getApparentTypeOfContextualType' when you're going to need the members of the type. - * - * @param node the expression whose contextual type will be returned. - * @returns the contextual type of an expression. - */ - function getContextualType(node) { - if (isInsideWithStatementBody(node)) { - // We cannot answer semantic questions within a with block, do not proceed any further - return undefined; - } - if (node.contextualType) { - return node.contextualType; - } - var parent = node.parent; - switch (parent.kind) { - case 218 /* VariableDeclaration */: - case 142 /* Parameter */: - case 145 /* PropertyDeclaration */: - case 144 /* PropertySignature */: - case 169 /* BindingElement */: - return getContextualTypeForInitializerExpression(node); - case 180 /* ArrowFunction */: - case 211 /* ReturnStatement */: - return getContextualTypeForReturnExpression(node); - case 190 /* YieldExpression */: - return getContextualTypeForYieldOperand(parent); - case 174 /* CallExpression */: - case 175 /* NewExpression */: - return getContextualTypeForArgument(parent, node); - case 177 /* TypeAssertionExpression */: - case 195 /* AsExpression */: - return getTypeFromTypeNode(parent.type); - case 187 /* BinaryExpression */: - return getContextualTypeForBinaryOperand(node); - case 253 /* PropertyAssignment */: - case 254 /* ShorthandPropertyAssignment */: - return getContextualTypeForObjectLiteralElement(parent); - case 170 /* ArrayLiteralExpression */: - return getContextualTypeForElementExpression(node); - case 188 /* ConditionalExpression */: - return getContextualTypeForConditionalOperand(node); - case 197 /* TemplateSpan */: - ts.Debug.assert(parent.parent.kind === 189 /* TemplateExpression */); - return getContextualTypeForSubstitutionExpression(parent.parent, node); - case 178 /* ParenthesizedExpression */: - return getContextualType(parent); - case 248 /* JsxExpression */: - return getContextualType(parent); - case 246 /* JsxAttribute */: - case 247 /* JsxSpreadAttribute */: - return getContextualTypeForJsxAttribute(parent); + // Check if a parameter is assigned anywhere within its declaring function. + function isParameterAssigned(symbol) { + var func = ts.getRootDeclaration(symbol.valueDeclaration).parent; + var links = getNodeLinks(func); + if (!(links.flags & 4194304 /* AssignmentsMarked */)) { + links.flags |= 4194304 /* AssignmentsMarked */; + if (!hasParentWithAssignmentsMarked(func)) { + markParameterAssignments(func); + } } - return undefined; + return symbol.isAssigned || false; } - // If the given type is an object or union type, if that type has a single signature, and if - // that signature is non-generic, return the signature. Otherwise return undefined. - function getNonGenericSignature(type) { - var signatures = getSignaturesOfStructuredType(type, 0 /* Call */); - if (signatures.length === 1) { - var signature = signatures[0]; - if (!signature.typeParameters) { - return signature; + function hasParentWithAssignmentsMarked(node) { + while (true) { + node = node.parent; + if (!node) { + return false; + } + if (ts.isFunctionLike(node) && getNodeLinks(node).flags & 4194304 /* AssignmentsMarked */) { + return true; } } } - function isFunctionExpressionOrArrowFunction(node) { - return node.kind === 179 /* FunctionExpression */ || node.kind === 180 /* ArrowFunction */; - } - function getContextualSignatureForFunctionLikeDeclaration(node) { - // Only function expressions, arrow functions, and object literal methods are contextually typed. - return isFunctionExpressionOrArrowFunction(node) || ts.isObjectLiteralMethod(node) - ? getContextualSignature(node) - : undefined; - } - function getContextualTypeForFunctionLikeDeclaration(node) { - return ts.isObjectLiteralMethod(node) ? - getContextualTypeForObjectLiteralMethod(node) : - getApparentTypeOfContextualType(node); - } - // Return the contextual signature for a given expression node. A contextual type provides a - // contextual signature if it has a single call signature and if that call signature is non-generic. - // If the contextual type is a union type, get the signature from each type possible and if they are - // all identical ignoring their return type, the result is same signature but with return type as - // union type of return types from these signatures - function getContextualSignature(node) { - ts.Debug.assert(node.kind !== 147 /* MethodDeclaration */ || ts.isObjectLiteralMethod(node)); - var type = getContextualTypeForFunctionLikeDeclaration(node); - if (!type) { - return undefined; - } - if (!(type.flags & 524288 /* Union */)) { - return getNonGenericSignature(type); - } - var signatureList; - var types = type.types; - for (var _i = 0, types_13 = types; _i < types_13.length; _i++) { - var current = types_13[_i]; - var signature = getNonGenericSignature(current); - if (signature) { - if (!signatureList) { - // This signature will contribute to contextual union signature - signatureList = [signature]; - } - else if (!compareSignaturesIdentical(signatureList[0], signature, /*partialMatch*/ false, /*ignoreThisTypes*/ true, /*ignoreReturnTypes*/ true, compareTypesIdentical)) { - // Signatures aren't identical, do not use - return undefined; - } - else { - // Use this signature for contextual union signature - signatureList.push(signature); + function markParameterAssignments(node) { + if (node.kind === 69 /* Identifier */) { + if (ts.isAssignmentTarget(node)) { + var symbol = getResolvedSymbol(node); + if (symbol.valueDeclaration && ts.getRootDeclaration(symbol.valueDeclaration).kind === 142 /* Parameter */) { + symbol.isAssigned = true; } } } - // Result is union of signatures collected (return type is union of return types of this signature set) - var result; - if (signatureList) { - result = cloneSignature(signatureList[0]); - // Clear resolved return type we possibly got from cloneSignature - result.resolvedReturnType = undefined; - result.unionSignatures = signatureList; + else { + ts.forEachChild(node, markParameterAssignments); } - return result; - } - /** - * Detect if the mapper implies an inference context. Specifically, there are 4 possible values - * for a mapper. Let's go through each one of them: - * - * 1. undefined - this means we are not doing inferential typing, but we may do contextual typing, - * which could cause us to assign a parameter a type - * 2. identityMapper - means we want to avoid assigning a parameter a type, whether or not we are in - * inferential typing (context is undefined for the identityMapper) - * 3. a mapper created by createInferenceMapper - we are doing inferential typing, we want to assign - * types to parameters and fix type parameters (context is defined) - * 4. an instantiation mapper created by createTypeMapper or createTypeEraser - this should never be - * passed as the contextual mapper when checking an expression (context is undefined for these) - * - * isInferentialContext is detecting if we are in case 3 - */ - function isInferentialContext(mapper) { - return mapper && mapper.context; - } - function checkSpreadElementExpression(node, contextualMapper) { - // It is usually not safe to call checkExpressionCached if we can be contextually typing. - // You can tell that we are contextually typing because of the contextualMapper parameter. - // While it is true that a spread element can have a contextual type, it does not do anything - // with this type. It is neither affected by it, nor does it propagate it to its operand. - // So the fact that contextualMapper is passed is not important, because the operand of a spread - // element is not contextually typed. - var arrayOrIterableType = checkExpressionCached(node.expression, contextualMapper); - return checkIteratedTypeOrElementType(arrayOrIterableType, node.expression, /*allowStringInput*/ false); } - function hasDefaultValue(node) { - return (node.kind === 169 /* BindingElement */ && !!node.initializer) || - (node.kind === 187 /* BinaryExpression */ && node.operatorToken.kind === 56 /* EqualsToken */); - } - function checkArrayLiteral(node, contextualMapper) { - var elements = node.elements; - var hasSpreadElement = false; - var elementTypes = []; - var inDestructuringPattern = ts.isAssignmentTarget(node); - for (var _i = 0, elements_1 = elements; _i < elements_1.length; _i++) { - var e = elements_1[_i]; - if (inDestructuringPattern && e.kind === 191 /* SpreadElementExpression */) { - // Given the following situation: - // var c: {}; - // [...c] = ["", 0]; - // - // c is represented in the tree as a spread element in an array literal. - // But c really functions as a rest element, and its purpose is to provide - // a contextual type for the right hand side of the assignment. Therefore, - // instead of calling checkExpression on "...c", which will give an error - // if c is not iterable/array-like, we need to act as if we are trying to - // get the contextual element type from it. So we do something similar to - // getContextualTypeForElementExpression, which will crucially not error - // if there is no index type / iterated type. - var restArrayType = checkExpression(e.expression, contextualMapper); - var restElementType = getIndexTypeOfType(restArrayType, 1 /* Number */) || - (languageVersion >= 2 /* ES6 */ ? getElementTypeOfIterable(restArrayType, /*errorNode*/ undefined) : undefined); - if (restElementType) { - elementTypes.push(restElementType); + function checkIdentifier(node) { + var symbol = getResolvedSymbol(node); + // As noted in ECMAScript 6 language spec, arrow functions never have an arguments objects. + // Although in down-level emit of arrow function, we emit it using function expression which means that + // arguments objects will be bound to the inner object; emitting arrow function natively in ES6, arguments objects + // will be bound to non-arrow function that contain this arrow function. This results in inconsistent behavior. + // To avoid that we will give an error to users if they use arguments objects in arrow function so that they + // can explicitly bound arguments objects + if (symbol === argumentsSymbol) { + var container = ts.getContainingFunction(node); + if (languageVersion < 2 /* ES6 */) { + if (container.kind === 180 /* ArrowFunction */) { + error(node, ts.Diagnostics.The_arguments_object_cannot_be_referenced_in_an_arrow_function_in_ES3_and_ES5_Consider_using_a_standard_function_expression); + } + else if (ts.hasModifier(container, 256 /* Async */)) { + error(node, ts.Diagnostics.The_arguments_object_cannot_be_referenced_in_an_async_function_or_method_in_ES3_and_ES5_Consider_using_a_standard_function_or_method); } } - else { - var type = checkExpressionForMutableLocation(e, contextualMapper); - elementTypes.push(type); + if (node.flags & 262144 /* AwaitContext */) { + getNodeLinks(container).flags |= 8192 /* CaptureArguments */; } - hasSpreadElement = hasSpreadElement || e.kind === 191 /* SpreadElementExpression */; } - if (!hasSpreadElement) { - // If array literal is actually a destructuring pattern, mark it as an implied type. We do this such - // that we get the same behavior for "var [x, y] = []" and "[x, y] = []". - if (inDestructuringPattern && elementTypes.length) { - var type = cloneTypeReference(createTupleType(elementTypes)); - type.pattern = node; - return type; + if (symbol.flags & 8388608 /* Alias */ && !isInTypeQuery(node) && !isConstEnumOrConstEnumOnlyModule(resolveAlias(symbol))) { + markAliasSymbolAsReferenced(symbol); + } + var localOrExportSymbol = getExportSymbolOfValueSymbolIfExported(symbol); + if (localOrExportSymbol.flags & 32 /* Class */) { + var declaration_1 = localOrExportSymbol.valueDeclaration; + // Due to the emit for class decorators, any reference to the class from inside of the class body + // must instead be rewritten to point to a temporary variable to avoid issues with the double-bind + // behavior of class names in ES6. + if (languageVersion === 2 /* ES6 */ + && declaration_1.kind === 221 /* ClassDeclaration */ + && ts.nodeIsDecorated(declaration_1)) { + var container = ts.getContainingClass(node); + while (container !== undefined) { + if (container === declaration_1 && container.name !== node) { + getNodeLinks(declaration_1).flags |= 8388608 /* ClassWithConstructorReference */; + getNodeLinks(node).flags |= 16777216 /* ConstructorReferenceInClass */; + break; + } + container = ts.getContainingClass(container); + } } - var contextualType = getApparentTypeOfContextualType(node); - if (contextualType && contextualTypeIsTupleLikeType(contextualType)) { - var pattern = contextualType.pattern; - // If array literal is contextually typed by a binding pattern or an assignment pattern, pad the resulting - // tuple type with the corresponding binding or assignment element types to make the lengths equal. - if (pattern && (pattern.kind === 168 /* ArrayBindingPattern */ || pattern.kind === 170 /* ArrayLiteralExpression */)) { - var patternElements = pattern.elements; - for (var i = elementTypes.length; i < patternElements.length; i++) { - var patternElement = patternElements[i]; - if (hasDefaultValue(patternElement)) { - elementTypes.push(contextualType.typeArguments[i]); - } - else { - if (patternElement.kind !== 193 /* OmittedExpression */) { - error(patternElement, ts.Diagnostics.Initializer_provides_no_value_for_this_binding_element_and_the_binding_element_has_no_default_value); - } - elementTypes.push(unknownType); + else if (declaration_1.kind === 192 /* ClassExpression */) { + // When we emit a class expression with static members that contain a reference + // to the constructor in the initializer, we will need to substitute that + // binding with an alias as the class name is not in scope. + var container = ts.getThisContainer(node, /*includeArrowFunctions*/ false); + while (container !== undefined) { + if (container.parent === declaration_1) { + if (container.kind === 145 /* PropertyDeclaration */ && ts.hasModifier(container, 32 /* Static */)) { + getNodeLinks(declaration_1).flags |= 8388608 /* ClassWithConstructorReference */; + getNodeLinks(node).flags |= 16777216 /* ConstructorReferenceInClass */; } + break; } + container = ts.getThisContainer(container, /*includeArrowFunctions*/ false); } - if (elementTypes.length) { - return createTupleType(elementTypes); + } + } + checkCollisionWithCapturedSuperVariable(node, node); + checkCollisionWithCapturedThisVariable(node, node); + checkNestedBlockScopedBinding(node, symbol); + var type = getTypeOfSymbol(localOrExportSymbol); + var declaration = localOrExportSymbol.valueDeclaration; + // We only narrow variables and parameters occurring in a non-assignment position. For all other + // entities we simply return the declared type. + if (!(localOrExportSymbol.flags & 3 /* Variable */) || ts.isAssignmentTarget(node) || !declaration) { + return type; + } + // The declaration container is the innermost function that encloses the declaration of the variable + // or parameter. The flow container is the innermost function starting with which we analyze the control + // flow graph to determine the control flow based type. + var isParameter = ts.getRootDeclaration(declaration).kind === 142 /* Parameter */; + var declarationContainer = getControlFlowContainer(declaration); + var flowContainer = getControlFlowContainer(node); + var isOuterVariable = flowContainer !== declarationContainer; + // When the control flow originates in a function expression or arrow function and we are referencing + // a const variable or parameter from an outer function, we extend the origin of the control flow + // analysis to include the immediately enclosing function. + while (flowContainer !== declarationContainer && + (flowContainer.kind === 179 /* FunctionExpression */ || + flowContainer.kind === 180 /* ArrowFunction */ || + ts.isObjectLiteralOrClassExpressionMethod(flowContainer)) && + (isReadonlySymbol(localOrExportSymbol) || isParameter && !isParameterAssigned(localOrExportSymbol))) { + flowContainer = getControlFlowContainer(flowContainer); + } + // We only look for uninitialized variables in strict null checking mode, and only when we can analyze + // the entire control flow graph from the variable's declaration (i.e. when the flow container and + // declaration container are the same). + var assumeInitialized = isParameter || isOuterVariable || + type !== autoType && (!strictNullChecks || (type.flags & 1 /* Any */) !== 0) || + ts.isInAmbientContext(declaration); + var flowType = getFlowTypeOfReference(node, type, assumeInitialized, flowContainer); + // A variable is considered uninitialized when it is possible to analyze the entire control flow graph + // from declaration to use, and when the variable's declared type doesn't include undefined but the + // control flow based type does include undefined. + if (type === autoType) { + if (flowType === autoType) { + if (compilerOptions.noImplicitAny) { + error(declaration.name, ts.Diagnostics.Variable_0_implicitly_has_type_any_in_some_locations_where_its_type_cannot_be_determined, symbolToString(symbol)); + error(node, ts.Diagnostics.Variable_0_implicitly_has_an_1_type, symbolToString(symbol), typeToString(anyType)); } + return anyType; } } - return createArrayType(elementTypes.length ? - getUnionType(elementTypes, /*subtypeReduction*/ true) : - strictNullChecks ? neverType : undefinedWideningType); - } - function isNumericName(name) { - return name.kind === 140 /* ComputedPropertyName */ ? isNumericComputedName(name) : isNumericLiteralName(name.text); - } - function isNumericComputedName(name) { - // It seems odd to consider an expression of type Any to result in a numeric name, - // but this behavior is consistent with checkIndexedAccess - return isTypeAnyOrAllConstituentTypesHaveKind(checkComputedPropertyName(name), 340 /* NumberLike */); - } - function isTypeAnyOrAllConstituentTypesHaveKind(type, kind) { - return isTypeAny(type) || isTypeOfKind(type, kind); - } - function isInfinityOrNaNString(name) { - return name === "Infinity" || name === "-Infinity" || name === "NaN"; - } - function isNumericLiteralName(name) { - // The intent of numeric names is that - // - they are names with text in a numeric form, and that - // - setting properties/indexing with them is always equivalent to doing so with the numeric literal 'numLit', - // acquired by applying the abstract 'ToNumber' operation on the name's text. - // - // The subtlety is in the latter portion, as we cannot reliably say that anything that looks like a numeric literal is a numeric name. - // In fact, it is the case that the text of the name must be equal to 'ToString(numLit)' for this to hold. - // - // Consider the property name '"0xF00D"'. When one indexes with '0xF00D', they are actually indexing with the value of 'ToString(0xF00D)' - // according to the ECMAScript specification, so it is actually as if the user indexed with the string '"61453"'. - // Thus, the text of all numeric literals equivalent to '61543' such as '0xF00D', '0xf00D', '0170015', etc. are not valid numeric names - // because their 'ToString' representation is not equal to their original text. - // This is motivated by ECMA-262 sections 9.3.1, 9.8.1, 11.1.5, and 11.2.1. - // - // Here, we test whether 'ToString(ToNumber(name))' is exactly equal to 'name'. - // The '+' prefix operator is equivalent here to applying the abstract ToNumber operation. - // Applying the 'toString()' method on a number gives us the abstract ToString operation on a number. - // - // Note that this accepts the values 'Infinity', '-Infinity', and 'NaN', and that this is intentional. - // This is desired behavior, because when indexing with them as numeric entities, you are indexing - // with the strings '"Infinity"', '"-Infinity"', and '"NaN"' respectively. - return (+name).toString() === name; + else if (!assumeInitialized && !(getFalsyFlags(type) & 2048 /* Undefined */) && getFalsyFlags(flowType) & 2048 /* Undefined */) { + error(node, ts.Diagnostics.Variable_0_is_used_before_being_assigned, symbolToString(symbol)); + // Return the declared type to reduce follow-on errors + return type; + } + return flowType; } - function checkComputedPropertyName(node) { - var links = getNodeLinks(node.expression); - if (!links.resolvedType) { - links.resolvedType = checkExpression(node.expression); - // This will allow types number, string, symbol or any. It will also allow enums, the unknown - // type, and any union of these types (like string | number). - if (!isTypeAnyOrAllConstituentTypesHaveKind(links.resolvedType, 340 /* NumberLike */ | 34 /* StringLike */ | 512 /* ESSymbol */)) { - error(node, ts.Diagnostics.A_computed_property_name_must_be_of_type_string_number_symbol_or_any); - } - else { - checkThatExpressionIsProperSymbolReference(node.expression, links.resolvedType, /*reportError*/ true); + function isInsideFunction(node, threshold) { + var current = node; + while (current && current !== threshold) { + if (ts.isFunctionLike(current)) { + return true; } + current = current.parent; } - return links.resolvedType; + return false; } - function getObjectLiteralIndexInfo(node, properties, kind) { - var propTypes = []; - for (var i = 0; i < properties.length; i++) { - if (kind === 0 /* String */ || isNumericName(node.properties[i].name)) { - propTypes.push(getTypeOfSymbol(properties[i])); + function checkNestedBlockScopedBinding(node, symbol) { + if (languageVersion >= 2 /* ES6 */ || + (symbol.flags & (2 /* BlockScopedVariable */ | 32 /* Class */)) === 0 || + symbol.valueDeclaration.parent.kind === 252 /* CatchClause */) { + return; + } + // 1. walk from the use site up to the declaration and check + // if there is anything function like between declaration and use-site (is binding/class is captured in function). + // 2. walk from the declaration up to the boundary of lexical environment and check + // if there is an iteration statement in between declaration and boundary (is binding/class declared inside iteration statement) + var container = ts.getEnclosingBlockScopeContainer(symbol.valueDeclaration); + var usedInFunction = isInsideFunction(node.parent, container); + var current = container; + var containedInIterationStatement = false; + while (current && !ts.nodeStartsNewLexicalEnvironment(current)) { + if (ts.isIterationStatement(current, /*lookInLabeledStatements*/ false)) { + containedInIterationStatement = true; + break; } + current = current.parent; } - var unionType = propTypes.length ? getUnionType(propTypes, /*subtypeReduction*/ true) : undefinedType; - return createIndexInfo(unionType, /*isReadonly*/ false); - } - function checkObjectLiteral(node, contextualMapper) { - var inDestructuringPattern = ts.isAssignmentTarget(node); - // Grammar checking - checkGrammarObjectLiteralExpression(node, inDestructuringPattern); - var propertiesTable = ts.createMap(); - var propertiesArray = []; - var contextualType = getApparentTypeOfContextualType(node); - var contextualTypeHasPattern = contextualType && contextualType.pattern && - (contextualType.pattern.kind === 167 /* ObjectBindingPattern */ || contextualType.pattern.kind === 171 /* ObjectLiteralExpression */); - var typeFlags = 0; - var patternWithComputedProperties = false; - var hasComputedStringProperty = false; - var hasComputedNumberProperty = false; - for (var _i = 0, _a = node.properties; _i < _a.length; _i++) { - var memberDecl = _a[_i]; - var member = memberDecl.symbol; - if (memberDecl.kind === 253 /* PropertyAssignment */ || - memberDecl.kind === 254 /* ShorthandPropertyAssignment */ || - ts.isObjectLiteralMethod(memberDecl)) { - var type = void 0; - if (memberDecl.kind === 253 /* PropertyAssignment */) { - type = checkPropertyAssignment(memberDecl, contextualMapper); - } - else if (memberDecl.kind === 147 /* MethodDeclaration */) { - type = checkObjectLiteralMethod(memberDecl, contextualMapper); - } - else { - ts.Debug.assert(memberDecl.kind === 254 /* ShorthandPropertyAssignment */); - type = checkExpressionForMutableLocation(memberDecl.name, contextualMapper); - } - typeFlags |= type.flags; - var prop = createSymbol(4 /* Property */ | 67108864 /* Transient */ | member.flags, member.name); - if (inDestructuringPattern) { - // If object literal is an assignment pattern and if the assignment pattern specifies a default value - // for the property, make the property optional. - var isOptional = (memberDecl.kind === 253 /* PropertyAssignment */ && hasDefaultValue(memberDecl.initializer)) || - (memberDecl.kind === 254 /* ShorthandPropertyAssignment */ && memberDecl.objectAssignmentInitializer); - if (isOptional) { - prop.flags |= 536870912 /* Optional */; - } - if (ts.hasDynamicName(memberDecl)) { - patternWithComputedProperties = true; - } - } - else if (contextualTypeHasPattern && !(contextualType.flags & 536870912 /* ObjectLiteralPatternWithComputedProperties */)) { - // If object literal is contextually typed by the implied type of a binding pattern, and if the - // binding pattern specifies a default value for the property, make the property optional. - var impliedProp = getPropertyOfType(contextualType, member.name); - if (impliedProp) { - prop.flags |= impliedProp.flags & 536870912 /* Optional */; - } - else if (!compilerOptions.suppressExcessPropertyErrors) { - error(memberDecl.name, ts.Diagnostics.Object_literal_may_only_specify_known_properties_and_0_does_not_exist_in_type_1, symbolToString(member), typeToString(contextualType)); - } - } - prop.declarations = member.declarations; - prop.parent = member.parent; - if (member.valueDeclaration) { - prop.valueDeclaration = member.valueDeclaration; - } - prop.type = type; - prop.target = member; - member = prop; + if (containedInIterationStatement) { + if (usedInFunction) { + // mark iteration statement as containing block-scoped binding captured in some function + getNodeLinks(current).flags |= 65536 /* LoopWithCapturedBlockScopedBinding */; } - else { - // TypeScript 1.0 spec (April 2014) - // A get accessor declaration is processed in the same manner as - // an ordinary function declaration(section 6.1) with no parameters. - // A set accessor declaration is processed in the same manner - // as an ordinary function declaration with a single parameter and a Void return type. - ts.Debug.assert(memberDecl.kind === 149 /* GetAccessor */ || memberDecl.kind === 150 /* SetAccessor */); - checkAccessorDeclaration(memberDecl); + // mark variables that are declared in loop initializer and reassigned inside the body of ForStatement. + // if body of ForStatement will be converted to function then we'll need a extra machinery to propagate reassigned values back. + if (container.kind === 206 /* ForStatement */ && + ts.getAncestor(symbol.valueDeclaration, 219 /* VariableDeclarationList */).parent === container && + isAssignedInBodyOfForStatement(node, container)) { + getNodeLinks(symbol.valueDeclaration).flags |= 2097152 /* NeedsLoopOutParameter */; } - if (ts.hasDynamicName(memberDecl)) { - if (isNumericName(memberDecl.name)) { - hasComputedNumberProperty = true; - } - else { - hasComputedStringProperty = true; - } + // set 'declared inside loop' bit on the block-scoped binding + getNodeLinks(symbol.valueDeclaration).flags |= 262144 /* BlockScopedBindingInLoop */; + } + if (usedInFunction) { + getNodeLinks(symbol.valueDeclaration).flags |= 131072 /* CapturedBlockScopedBinding */; + } + } + function isAssignedInBodyOfForStatement(node, container) { + var current = node; + // skip parenthesized nodes + while (current.parent.kind === 178 /* ParenthesizedExpression */) { + current = current.parent; + } + // check if node is used as LHS in some assignment expression + var isAssigned = false; + if (ts.isAssignmentTarget(current)) { + isAssigned = true; + } + else if ((current.parent.kind === 185 /* PrefixUnaryExpression */ || current.parent.kind === 186 /* PostfixUnaryExpression */)) { + var expr = current.parent; + isAssigned = expr.operator === 41 /* PlusPlusToken */ || expr.operator === 42 /* MinusMinusToken */; + } + if (!isAssigned) { + return false; + } + // at this point we know that node is the target of assignment + // now check that modification happens inside the statement part of the ForStatement + while (current !== container) { + if (current === container.statement) { + return true; } else { - propertiesTable[member.name] = member; - } - propertiesArray.push(member); - } - // If object literal is contextually typed by the implied type of a binding pattern, augment the result - // type with those properties for which the binding pattern specifies a default value. - if (contextualTypeHasPattern) { - for (var _b = 0, _c = getPropertiesOfType(contextualType); _b < _c.length; _b++) { - var prop = _c[_b]; - if (!propertiesTable[prop.name]) { - if (!(prop.flags & 536870912 /* Optional */)) { - error(prop.valueDeclaration || prop.bindingElement, ts.Diagnostics.Initializer_provides_no_value_for_this_binding_element_and_the_binding_element_has_no_default_value); - } - propertiesTable[prop.name] = prop; - propertiesArray.push(prop); - } + current = current.parent; } } - var stringIndexInfo = hasComputedStringProperty ? getObjectLiteralIndexInfo(node, propertiesArray, 0 /* String */) : undefined; - var numberIndexInfo = hasComputedNumberProperty ? getObjectLiteralIndexInfo(node, propertiesArray, 1 /* Number */) : undefined; - var result = createAnonymousType(node.symbol, propertiesTable, emptyArray, emptyArray, stringIndexInfo, numberIndexInfo); - var freshObjectLiteralFlag = compilerOptions.suppressExcessPropertyErrors ? 0 : 16777216 /* FreshLiteral */; - result.flags |= 8388608 /* ObjectLiteral */ | 67108864 /* ContainsObjectLiteral */ | freshObjectLiteralFlag | (typeFlags & 234881024 /* PropagatingFlags */) | (patternWithComputedProperties ? 536870912 /* ObjectLiteralPatternWithComputedProperties */ : 0); - if (inDestructuringPattern) { - result.pattern = node; - } - return result; - } - function checkJsxSelfClosingElement(node) { - checkJsxOpeningLikeElement(node); - return jsxElementType || anyType; + return false; } - function checkJsxElement(node) { - // Check attributes - checkJsxOpeningLikeElement(node.openingElement); - // Perform resolution on the closing tag so that rename/go to definition/etc work - if (isJsxIntrinsicIdentifier(node.closingElement.tagName)) { - getIntrinsicTagSymbol(node.closingElement); + function captureLexicalThis(node, container) { + getNodeLinks(node).flags |= 2 /* LexicalThis */; + if (container.kind === 145 /* PropertyDeclaration */ || container.kind === 148 /* Constructor */) { + var classNode = container.parent; + getNodeLinks(classNode).flags |= 4 /* CaptureThis */; } else { - checkExpression(node.closingElement.tagName); + getNodeLinks(container).flags |= 4 /* CaptureThis */; } - // Check children - for (var _i = 0, _a = node.children; _i < _a.length; _i++) { - var child = _a[_i]; - switch (child.kind) { - case 248 /* JsxExpression */: - checkJsxExpression(child); - break; - case 241 /* JsxElement */: - checkJsxElement(child); - break; - case 242 /* JsxSelfClosingElement */: - checkJsxSelfClosingElement(child); - break; - } + } + function findFirstSuperCall(n) { + if (ts.isSuperCall(n)) { + return n; } - return jsxElementType || anyType; + else if (ts.isFunctionLike(n)) { + return undefined; + } + return ts.forEachChild(n, findFirstSuperCall); } /** - * Returns true iff the JSX element name would be a valid JS identifier, ignoring restrictions about keywords not being identifiers + * Return a cached result if super-statement is already found. + * Otherwise, find a super statement in a given constructor function and cache the result in the node-links of the constructor + * + * @param constructor constructor-function to look for super statement */ - function isUnhyphenatedJsxName(name) { - // - is the only character supported in JSX attribute names that isn't valid in JavaScript identifiers - return name.indexOf("-") < 0; + function getSuperCallInConstructor(constructor) { + var links = getNodeLinks(constructor); + // Only trying to find super-call if we haven't yet tried to find one. Once we try, we will record the result + if (links.hasSuperCall === undefined) { + links.superCall = findFirstSuperCall(constructor.body); + links.hasSuperCall = links.superCall ? true : false; + } + return links.superCall; } /** - * Returns true iff React would emit this tag name as a string rather than an identifier or qualified name + * Check if the given class-declaration extends null then return true. + * Otherwise, return false + * @param classDecl a class declaration to check if it extends null */ - function isJsxIntrinsicIdentifier(tagName) { - // TODO (yuisu): comment - if (tagName.kind === 172 /* PropertyAccessExpression */ || tagName.kind === 97 /* ThisKeyword */) { - return false; + function classDeclarationExtendsNull(classDecl) { + var classSymbol = getSymbolOfNode(classDecl); + var classInstanceType = getDeclaredTypeOfSymbol(classSymbol); + var baseConstructorType = getBaseConstructorTypeOfClass(classInstanceType); + return baseConstructorType === nullWideningType; + } + function checkThisExpression(node) { + // Stop at the first arrow function so that we can + // tell whether 'this' needs to be captured. + var container = ts.getThisContainer(node, /* includeArrowFunctions */ true); + var needToCaptureLexicalThis = false; + if (container.kind === 148 /* Constructor */) { + var containingClassDecl = container.parent; + var baseTypeNode = ts.getClassExtendsHeritageClauseElement(containingClassDecl); + // If a containing class does not have extends clause or the class extends null + // skip checking whether super statement is called before "this" accessing. + if (baseTypeNode && !classDeclarationExtendsNull(containingClassDecl)) { + var superCall = getSuperCallInConstructor(container); + // We should give an error in the following cases: + // - No super-call + // - "this" is accessing before super-call. + // i.e super(this) + // this.x; super(); + // We want to make sure that super-call is done before accessing "this" so that + // "this" is not accessed as a parameter of the super-call. + if (!superCall || superCall.end > node.pos) { + // In ES6, super inside constructor of class-declaration has to precede "this" accessing + error(node, ts.Diagnostics.super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class); + } + } } - else { - return ts.isIntrinsicJsxName(tagName.text); + // Now skip arrow functions to get the "real" owner of 'this'. + if (container.kind === 180 /* ArrowFunction */) { + container = ts.getThisContainer(container, /* includeArrowFunctions */ false); + // When targeting es6, arrow function lexically bind "this" so we do not need to do the work of binding "this" in emitted code + needToCaptureLexicalThis = (languageVersion < 2 /* ES6 */); } - } - function checkJsxAttribute(node, elementAttributesType, nameTable) { - var correspondingPropType = undefined; - // Look up the corresponding property for this attribute - if (elementAttributesType === emptyObjectType && isUnhyphenatedJsxName(node.name.text)) { - // If there is no 'props' property, you may not have non-"data-" attributes - error(node.parent, ts.Diagnostics.JSX_element_class_does_not_support_attributes_because_it_does_not_have_a_0_property, getJsxElementPropertiesName()); - } - else if (elementAttributesType && !isTypeAny(elementAttributesType)) { - var correspondingPropSymbol = getPropertyOfType(elementAttributesType, node.name.text); - correspondingPropType = correspondingPropSymbol && getTypeOfSymbol(correspondingPropSymbol); - if (isUnhyphenatedJsxName(node.name.text)) { - var attributeType = getTypeOfPropertyOfType(elementAttributesType, getTextOfPropertyName(node.name)) || getIndexTypeOfType(elementAttributesType, 0 /* String */); - if (attributeType) { - correspondingPropType = attributeType; + switch (container.kind) { + case 225 /* ModuleDeclaration */: + error(node, ts.Diagnostics.this_cannot_be_referenced_in_a_module_or_namespace_body); + // do not return here so in case if lexical this is captured - it will be reflected in flags on NodeLinks + break; + case 224 /* EnumDeclaration */: + error(node, ts.Diagnostics.this_cannot_be_referenced_in_current_location); + // do not return here so in case if lexical this is captured - it will be reflected in flags on NodeLinks + break; + case 148 /* Constructor */: + if (isInConstructorArgumentInitializer(node, container)) { + error(node, ts.Diagnostics.this_cannot_be_referenced_in_constructor_arguments); } - else { - // If there's no corresponding property with this name, error - if (!correspondingPropType) { - error(node.name, ts.Diagnostics.Property_0_does_not_exist_on_type_1, node.name.text, typeToString(elementAttributesType)); - return unknownType; - } + break; + case 145 /* PropertyDeclaration */: + case 144 /* PropertySignature */: + if (ts.getModifierFlags(container) & 32 /* Static */) { + error(node, ts.Diagnostics.this_cannot_be_referenced_in_a_static_property_initializer); } - } + break; + case 140 /* ComputedPropertyName */: + error(node, ts.Diagnostics.this_cannot_be_referenced_in_a_computed_property_name); + break; } - var exprType; - if (node.initializer) { - exprType = checkExpression(node.initializer); + if (needToCaptureLexicalThis) { + captureLexicalThis(node, container); } - else { - // is sugar for - exprType = booleanType; + if (ts.isFunctionLike(container) && + (!isInParameterInitializerBeforeContainingFunction(node) || ts.getThisParameter(container))) { + // Note: a parameter initializer should refer to class-this unless function-this is explicitly annotated. + // If this is a function in a JS file, it might be a class method. Check if it's the RHS + // of a x.prototype.y = function [name]() { .... } + if (container.kind === 179 /* FunctionExpression */ && + ts.isInJavaScriptFile(container.parent) && + ts.getSpecialPropertyAssignmentKind(container.parent) === 3 /* PrototypeProperty */) { + // Get the 'x' of 'x.prototype.y = f' (here, 'f' is 'container') + var className = container.parent // x.prototype.y = f + .left // x.prototype.y + .expression // x.prototype + .expression; // x + var classSymbol = checkExpression(className).symbol; + if (classSymbol && classSymbol.members && (classSymbol.flags & 16 /* Function */)) { + return getInferredClassType(classSymbol); + } + } + var thisType = getThisTypeOfDeclaration(container); + if (thisType) { + return thisType; + } } - if (correspondingPropType) { - checkTypeAssignableTo(exprType, correspondingPropType, node); + if (ts.isClassLike(container.parent)) { + var symbol = getSymbolOfNode(container.parent); + var type = ts.hasModifier(container, 32 /* Static */) ? getTypeOfSymbol(symbol) : getDeclaredTypeOfSymbol(symbol).thisType; + return getFlowTypeOfReference(node, type, /*assumeInitialized*/ true, /*flowContainer*/ undefined); } - nameTable[node.name.text] = true; - return exprType; - } - function checkJsxSpreadAttribute(node, elementAttributesType, nameTable) { - var type = checkExpression(node.expression); - var props = getPropertiesOfType(type); - for (var _i = 0, props_2 = props; _i < props_2.length; _i++) { - var prop = props_2[_i]; - // Is there a corresponding property in the element attributes type? Skip checking of properties - // that have already been assigned to, as these are not actually pushed into the resulting type - if (!nameTable[prop.name]) { - var targetPropSym = getPropertyOfType(elementAttributesType, prop.name); - if (targetPropSym) { - var msg = ts.chainDiagnosticMessages(undefined, ts.Diagnostics.Property_0_of_JSX_spread_attribute_is_not_assignable_to_target_property, prop.name); - checkTypeAssignableTo(getTypeOfSymbol(prop), getTypeOfSymbol(targetPropSym), node, undefined, msg); - } - nameTable[prop.name] = true; + if (ts.isInJavaScriptFile(node)) { + var type = getTypeForThisExpressionFromJSDoc(container); + if (type && type !== unknownType) { + return type; } } - return type; - } - function getJsxType(name) { - if (jsxTypes[name] === undefined) { - return jsxTypes[name] = getExportedTypeFromNamespace(JsxNames.JSX, name) || unknownType; + if (compilerOptions.noImplicitThis) { + // With noImplicitThis, functions may not reference 'this' if it has type 'any' + error(node, ts.Diagnostics.this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation); } - return jsxTypes[name]; + return anyType; } - /** - * Looks up an intrinsic tag name and returns a symbol that either points to an intrinsic - * property (in which case nodeLinks.jsxFlags will be IntrinsicNamedElement) or an intrinsic - * string index signature (in which case nodeLinks.jsxFlags will be IntrinsicIndexedElement). - * May also return unknownSymbol if both of these lookups fail. - */ - function getIntrinsicTagSymbol(node) { - var links = getNodeLinks(node); - if (!links.resolvedSymbol) { - var intrinsicElementsType = getJsxType(JsxNames.IntrinsicElements); - if (intrinsicElementsType !== unknownType) { - // Property case - var intrinsicProp = getPropertyOfType(intrinsicElementsType, node.tagName.text); - if (intrinsicProp) { - links.jsxFlags |= 1 /* IntrinsicNamedElement */; - return links.resolvedSymbol = intrinsicProp; - } - // Intrinsic string indexer case - var indexSignatureType = getIndexTypeOfType(intrinsicElementsType, 0 /* String */); - if (indexSignatureType) { - links.jsxFlags |= 2 /* IntrinsicIndexedElement */; - return links.resolvedSymbol = intrinsicElementsType.symbol; - } - // Wasn't found - error(node, ts.Diagnostics.Property_0_does_not_exist_on_type_1, node.tagName.text, "JSX." + JsxNames.IntrinsicElements); - return links.resolvedSymbol = unknownSymbol; - } - else { - if (compilerOptions.noImplicitAny) { - error(node, ts.Diagnostics.JSX_element_implicitly_has_type_any_because_no_interface_JSX_0_exists, JsxNames.IntrinsicElements); - } - return links.resolvedSymbol = unknownSymbol; + function getTypeForThisExpressionFromJSDoc(node) { + var typeTag = ts.getJSDocTypeTag(node); + if (typeTag && typeTag.typeExpression && typeTag.typeExpression.type && typeTag.typeExpression.type.kind === 269 /* JSDocFunctionType */) { + var jsDocFunctionType = typeTag.typeExpression.type; + if (jsDocFunctionType.parameters.length > 0 && jsDocFunctionType.parameters[0].type.kind === 272 /* JSDocThisType */) { + return getTypeFromTypeNode(jsDocFunctionType.parameters[0].type); } } - return links.resolvedSymbol; } - /** - * Given a JSX element that is a class element, finds the Element Instance Type. If the - * element is not a class element, or the class element type cannot be determined, returns 'undefined'. - * For example, in the element , the element instance type is `MyClass` (not `typeof MyClass`). - */ - function getJsxElementInstanceType(node, valueType) { - ts.Debug.assert(!(valueType.flags & 524288 /* Union */)); - if (isTypeAny(valueType)) { - // Short-circuit if the class tag is using an element type 'any' - return anyType; - } - // Resolve the signatures, preferring constructor - var signatures = getSignaturesOfType(valueType, 1 /* Construct */); - if (signatures.length === 0) { - // No construct signatures, try call signatures - signatures = getSignaturesOfType(valueType, 0 /* Call */); - if (signatures.length === 0) { - // We found no signatures at all, which is an error - error(node.tagName, ts.Diagnostics.JSX_element_type_0_does_not_have_any_construct_or_call_signatures, ts.getTextOfNode(node.tagName)); - return unknownType; + function isInConstructorArgumentInitializer(node, constructorDecl) { + for (var n = node; n && n !== constructorDecl; n = n.parent) { + if (n.kind === 142 /* Parameter */) { + return true; } } - return getUnionType(signatures.map(getReturnTypeOfSignature), /*subtypeReduction*/ true); + return false; } - /// e.g. "props" for React.d.ts, - /// or 'undefined' if ElementAttributesProperty doesn't exist (which means all - /// non-intrinsic elements' attributes type is 'any'), - /// or '' if it has 0 properties (which means every - /// non-intrinsic elements' attributes type is the element instance type) - function getJsxElementPropertiesName() { - // JSX - var jsxNamespace = getGlobalSymbol(JsxNames.JSX, 1920 /* Namespace */, /*diagnosticMessage*/ undefined); - // JSX.ElementAttributesProperty [symbol] - var attribsPropTypeSym = jsxNamespace && getSymbol(jsxNamespace.exports, JsxNames.ElementAttributesPropertyNameContainer, 793064 /* Type */); - // JSX.ElementAttributesProperty [type] - var attribPropType = attribsPropTypeSym && getDeclaredTypeOfSymbol(attribsPropTypeSym); - // The properties of JSX.ElementAttributesProperty - var attribProperties = attribPropType && getPropertiesOfType(attribPropType); - if (attribProperties) { - // Element Attributes has zero properties, so the element attributes type will be the class instance type - if (attribProperties.length === 0) { - return ""; + function checkSuperExpression(node) { + var isCallExpression = node.parent.kind === 174 /* CallExpression */ && node.parent.expression === node; + var container = ts.getSuperContainer(node, /*stopOnFunctions*/ true); + var needToCaptureLexicalThis = false; + // adjust the container reference in case if super is used inside arrow functions with arbitrarily deep nesting + if (!isCallExpression) { + while (container && container.kind === 180 /* ArrowFunction */) { + container = ts.getSuperContainer(container, /*stopOnFunctions*/ true); + needToCaptureLexicalThis = languageVersion < 2 /* ES6 */; } - else if (attribProperties.length === 1) { - return attribProperties[0].name; + } + var canUseSuperExpression = isLegalUsageOfSuperExpression(container); + var nodeCheckFlag = 0; + if (!canUseSuperExpression) { + // issue more specific error if super is used in computed property name + // class A { foo() { return "1" }} + // class B { + // [super.foo()]() {} + // } + var current = node; + while (current && current !== container && current.kind !== 140 /* ComputedPropertyName */) { + current = current.parent; + } + if (current && current.kind === 140 /* ComputedPropertyName */) { + error(node, ts.Diagnostics.super_cannot_be_referenced_in_a_computed_property_name); + } + else if (isCallExpression) { + error(node, ts.Diagnostics.Super_calls_are_not_permitted_outside_constructors_or_in_nested_functions_inside_constructors); + } + else if (!container || !container.parent || !(ts.isClassLike(container.parent) || container.parent.kind === 171 /* ObjectLiteralExpression */)) { + error(node, ts.Diagnostics.super_can_only_be_referenced_in_members_of_derived_classes_or_object_literal_expressions); } else { - error(attribsPropTypeSym.declarations[0], ts.Diagnostics.The_global_type_JSX_0_may_not_have_more_than_one_property, JsxNames.ElementAttributesPropertyNameContainer); - return undefined; + error(node, ts.Diagnostics.super_property_access_is_permitted_only_in_a_constructor_member_function_or_member_accessor_of_a_derived_class); } + return unknownType; } - else { - // No interface exists, so the element attributes type will be an implicit any - return undefined; - } - } - /** - * Given React element instance type and the class type, resolve the Jsx type - * Pass elemType to handle individual type in the union typed element type. - */ - function getResolvedJsxType(node, elemType, elemClassType) { - if (!elemType) { - elemType = checkExpression(node.tagName); - } - if (elemType.flags & 524288 /* Union */) { - var types = elemType.types; - return getUnionType(types.map(function (type) { - return getResolvedJsxType(node, type, elemClassType); - }), /*subtypeReduction*/ true); + if ((ts.getModifierFlags(container) & 32 /* Static */) || isCallExpression) { + nodeCheckFlag = 512 /* SuperStatic */; } - // If the elemType is a string type, we have to return anyType to prevent an error downstream as we will try to find construct or call signature of the type - if (elemType.flags & 2 /* String */) { - return anyType; + else { + nodeCheckFlag = 256 /* SuperInstance */; } - else if (elemType.flags & 32 /* StringLiteral */) { - // If the elemType is a stringLiteral type, we can then provide a check to make sure that the string literal type is one of the Jsx intrinsic element type - var intrinsicElementsType = getJsxType(JsxNames.IntrinsicElements); - if (intrinsicElementsType !== unknownType) { - var stringLiteralTypeName = elemType.text; - var intrinsicProp = getPropertyOfType(intrinsicElementsType, stringLiteralTypeName); - if (intrinsicProp) { - return getTypeOfSymbol(intrinsicProp); - } - var indexSignatureType = getIndexTypeOfType(intrinsicElementsType, 0 /* String */); - if (indexSignatureType) { - return indexSignatureType; - } - error(node, ts.Diagnostics.Property_0_does_not_exist_on_type_1, stringLiteralTypeName, "JSX." + JsxNames.IntrinsicElements); + getNodeLinks(node).flags |= nodeCheckFlag; + // Due to how we emit async functions, we need to specialize the emit for an async method that contains a `super` reference. + // This is due to the fact that we emit the body of an async function inside of a generator function. As generator + // functions cannot reference `super`, we emit a helper inside of the method body, but outside of the generator. This helper + // uses an arrow function, which is permitted to reference `super`. + // + // There are two primary ways we can access `super` from within an async method. The first is getting the value of a property + // or indexed access on super, either as part of a right-hand-side expression or call expression. The second is when setting the value + // of a property or indexed access, either as part of an assignment expression or destructuring assignment. + // + // The simplest case is reading a value, in which case we will emit something like the following: + // + // // ts + // ... + // async asyncMethod() { + // let x = await super.asyncMethod(); + // return x; + // } + // ... + // + // // js + // ... + // asyncMethod() { + // const _super = name => super[name]; + // return __awaiter(this, arguments, Promise, function *() { + // let x = yield _super("asyncMethod").call(this); + // return x; + // }); + // } + // ... + // + // The more complex case is when we wish to assign a value, especially as part of a destructuring assignment. As both cases + // are legal in ES6, but also likely less frequent, we emit the same more complex helper for both scenarios: + // + // // ts + // ... + // async asyncMethod(ar: Promise) { + // [super.a, super.b] = await ar; + // } + // ... + // + // // js + // ... + // asyncMethod(ar) { + // const _super = (function (geti, seti) { + // const cache = Object.create(null); + // return name => cache[name] || (cache[name] = { get value() { return geti(name); }, set value(v) { seti(name, v); } }); + // })(name => super[name], (name, value) => super[name] = value); + // return __awaiter(this, arguments, Promise, function *() { + // [_super("a").value, _super("b").value] = yield ar; + // }); + // } + // ... + // + // This helper creates an object with a "value" property that wraps the `super` property or indexed access for both get and set. + // This is required for destructuring assignments, as a call expression cannot be used as the target of a destructuring assignment + // while a property access can. + if (container.kind === 147 /* MethodDeclaration */ && ts.getModifierFlags(container) & 256 /* Async */) { + if (ts.isSuperProperty(node.parent) && ts.isAssignmentTarget(node.parent)) { + getNodeLinks(container).flags |= 4096 /* AsyncMethodWithSuperBinding */; } - // If we need to report an error, we already done so here. So just return any to prevent any more error downstream - return anyType; - } - // Get the element instance type (the result of newing or invoking this tag) - var elemInstanceType = getJsxElementInstanceType(node, elemType); - if (!elemClassType || !isTypeAssignableTo(elemInstanceType, elemClassType)) { - // Is this is a stateless function component? See if its single signature's return type is - // assignable to the JSX Element Type - if (jsxElementType) { - var callSignatures = elemType && getSignaturesOfType(elemType, 0 /* Call */); - var callSignature = callSignatures && callSignatures.length > 0 && callSignatures[0]; - var callReturnType = callSignature && getReturnTypeOfSignature(callSignature); - var paramType = callReturnType && (callSignature.parameters.length === 0 ? emptyObjectType : getTypeOfSymbol(callSignature.parameters[0])); - if (callReturnType && isTypeAssignableTo(callReturnType, jsxElementType)) { - // Intersect in JSX.IntrinsicAttributes if it exists - var intrinsicAttributes = getJsxType(JsxNames.IntrinsicAttributes); - if (intrinsicAttributes !== unknownType) { - paramType = intersectTypes(intrinsicAttributes, paramType); - } - return paramType; - } + else { + getNodeLinks(container).flags |= 2048 /* AsyncMethodWithSuper */; } } - // Issue an error if this return type isn't assignable to JSX.ElementClass - if (elemClassType) { - checkTypeRelatedTo(elemInstanceType, elemClassType, assignableRelation, node, ts.Diagnostics.JSX_element_type_0_is_not_a_constructor_function_for_JSX_elements); + if (needToCaptureLexicalThis) { + // call expressions are allowed only in constructors so they should always capture correct 'this' + // super property access expressions can also appear in arrow functions - + // in this case they should also use correct lexical this + captureLexicalThis(node.parent, container); } - if (isTypeAny(elemInstanceType)) { - return elemInstanceType; + if (container.parent.kind === 171 /* ObjectLiteralExpression */) { + if (languageVersion < 2 /* ES6 */) { + error(node, ts.Diagnostics.super_is_only_allowed_in_members_of_object_literal_expressions_when_option_target_is_ES2015_or_higher); + return unknownType; + } + else { + // for object literal assume that type of 'super' is 'any' + return anyType; + } } - var propsName = getJsxElementPropertiesName(); - if (propsName === undefined) { - // There is no type ElementAttributesProperty, return 'any' - return anyType; + // at this point the only legal case for parent is ClassLikeDeclaration + var classLikeDeclaration = container.parent; + var classType = getDeclaredTypeOfSymbol(getSymbolOfNode(classLikeDeclaration)); + var baseClassType = classType && getBaseTypes(classType)[0]; + if (!baseClassType) { + if (!ts.getClassExtendsHeritageClauseElement(classLikeDeclaration)) { + error(node, ts.Diagnostics.super_can_only_be_referenced_in_a_derived_class); + } + return unknownType; } - else if (propsName === "") { - // If there is no e.g. 'props' member in ElementAttributesProperty, use the element class type instead - return elemInstanceType; + if (container.kind === 148 /* Constructor */ && isInConstructorArgumentInitializer(node, container)) { + // issue custom error message for super property access in constructor arguments (to be aligned with old compiler) + error(node, ts.Diagnostics.super_cannot_be_referenced_in_constructor_arguments); + return unknownType; } - else { - var attributesType = getTypeOfPropertyOfType(elemInstanceType, propsName); - if (!attributesType) { - // There is no property named 'props' on this instance type - return emptyObjectType; - } - else if (isTypeAny(attributesType) || (attributesType === unknownType)) { - // Props is of type 'any' or unknown - return attributesType; + return nodeCheckFlag === 512 /* SuperStatic */ + ? getBaseConstructorTypeOfClass(classType) + : getTypeWithThisArgument(baseClassType, classType.thisType); + function isLegalUsageOfSuperExpression(container) { + if (!container) { + return false; } - else if (attributesType.flags & 524288 /* Union */) { - // Props cannot be a union type - error(node.tagName, ts.Diagnostics.JSX_element_attributes_type_0_may_not_be_a_union_type, typeToString(attributesType)); - return anyType; + if (isCallExpression) { + // TS 1.0 SPEC (April 2014): 4.8.1 + // Super calls are only permitted in constructors of derived classes + return container.kind === 148 /* Constructor */; } else { - // Normal case -- add in IntrinsicClassElements and IntrinsicElements - var apparentAttributesType = attributesType; - var intrinsicClassAttribs = getJsxType(JsxNames.IntrinsicClassAttributes); - if (intrinsicClassAttribs !== unknownType) { - var typeParams = getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(intrinsicClassAttribs.symbol); - if (typeParams) { - if (typeParams.length === 1) { - apparentAttributesType = intersectTypes(createTypeReference(intrinsicClassAttribs, [elemInstanceType]), apparentAttributesType); - } + // TS 1.0 SPEC (April 2014) + // 'super' property access is allowed + // - In a constructor, instance member function, instance member accessor, or instance member variable initializer where this references a derived class instance + // - In a static member function or static member accessor + // topmost container must be something that is directly nested in the class declaration\object literal expression + if (ts.isClassLike(container.parent) || container.parent.kind === 171 /* ObjectLiteralExpression */) { + if (ts.getModifierFlags(container) & 32 /* Static */) { + return container.kind === 147 /* MethodDeclaration */ || + container.kind === 146 /* MethodSignature */ || + container.kind === 149 /* GetAccessor */ || + container.kind === 150 /* SetAccessor */; } else { - apparentAttributesType = intersectTypes(attributesType, intrinsicClassAttribs); + return container.kind === 147 /* MethodDeclaration */ || + container.kind === 146 /* MethodSignature */ || + container.kind === 149 /* GetAccessor */ || + container.kind === 150 /* SetAccessor */ || + container.kind === 145 /* PropertyDeclaration */ || + container.kind === 144 /* PropertySignature */ || + container.kind === 148 /* Constructor */; } } - var intrinsicAttribs = getJsxType(JsxNames.IntrinsicAttributes); - if (intrinsicAttribs !== unknownType) { - apparentAttributesType = intersectTypes(intrinsicAttribs, apparentAttributesType); - } - return apparentAttributesType; } + return false; } } - /** - * Given an opening/self-closing element, get the 'element attributes type', i.e. the type that tells - * us which attributes are valid on a given element. - */ - function getJsxElementAttributesType(node) { - var links = getNodeLinks(node); - if (!links.resolvedJsxType) { - if (isJsxIntrinsicIdentifier(node.tagName)) { - var symbol = getIntrinsicTagSymbol(node); - if (links.jsxFlags & 1 /* IntrinsicNamedElement */) { - return links.resolvedJsxType = getTypeOfSymbol(symbol); + function getContextualThisParameter(func) { + if (isContextSensitiveFunctionOrObjectLiteralMethod(func) && func.kind !== 180 /* ArrowFunction */) { + var contextualSignature = getContextualSignature(func); + if (contextualSignature) { + return contextualSignature.thisParameter; + } + } + return undefined; + } + // Return contextual type of parameter or undefined if no contextual type is available + function getContextuallyTypedParameterType(parameter) { + var func = parameter.parent; + if (isContextSensitiveFunctionOrObjectLiteralMethod(func)) { + var iife = ts.getImmediatelyInvokedFunctionExpression(func); + if (iife) { + var indexOfParameter = ts.indexOf(func.parameters, parameter); + if (iife.arguments && indexOfParameter < iife.arguments.length) { + if (parameter.dotDotDotToken) { + var restTypes = []; + for (var i = indexOfParameter; i < iife.arguments.length; i++) { + restTypes.push(getWidenedLiteralType(checkExpression(iife.arguments[i]))); + } + return createArrayType(getUnionType(restTypes)); + } + var links = getNodeLinks(iife); + var cached = links.resolvedSignature; + links.resolvedSignature = anySignature; + var type = getWidenedLiteralType(checkExpression(iife.arguments[indexOfParameter])); + links.resolvedSignature = cached; + return type; } - else if (links.jsxFlags & 2 /* IntrinsicIndexedElement */) { - return links.resolvedJsxType = getIndexInfoOfSymbol(symbol, 0 /* String */).type; + } + var contextualSignature = getContextualSignature(func); + if (contextualSignature) { + var funcHasRestParameters = ts.hasRestParameter(func); + var len = func.parameters.length - (funcHasRestParameters ? 1 : 0); + var indexOfParameter = ts.indexOf(func.parameters, parameter); + if (indexOfParameter < len) { + return getTypeAtPosition(contextualSignature, indexOfParameter); } - else { - return links.resolvedJsxType = unknownType; + // If last parameter is contextually rest parameter get its type + if (funcHasRestParameters && + indexOfParameter === (func.parameters.length - 1) && + isRestParameterIndex(contextualSignature, func.parameters.length - 1)) { + return getTypeOfSymbol(ts.lastOrUndefined(contextualSignature.parameters)); } } - else { - var elemClassType = getJsxGlobalElementClassType(); - return links.resolvedJsxType = getResolvedJsxType(node, undefined, elemClassType); - } } - return links.resolvedJsxType; + return undefined; } - /** - * Given a JSX attribute, returns the symbol for the corresponds property - * of the element attributes type. Will return unknownSymbol for attributes - * that have no matching element attributes type property. - */ - function getJsxAttributePropertySymbol(attrib) { - var attributesType = getJsxElementAttributesType(attrib.parent); - var prop = getPropertyOfType(attributesType, attrib.name.text); - return prop || unknownSymbol; - } - function getJsxGlobalElementClassType() { - if (!jsxElementClassType) { - jsxElementClassType = getExportedTypeFromNamespace(JsxNames.JSX, JsxNames.ElementClass); - } - return jsxElementClassType; - } - /// Returns all the properties of the Jsx.IntrinsicElements interface - function getJsxIntrinsicTagNames() { - var intrinsics = getJsxType(JsxNames.IntrinsicElements); - return intrinsics ? getPropertiesOfType(intrinsics) : emptyArray; - } - function checkJsxPreconditions(errorNode) { - // Preconditions for using JSX - if ((compilerOptions.jsx || 0 /* None */) === 0 /* None */) { - error(errorNode, ts.Diagnostics.Cannot_use_JSX_unless_the_jsx_flag_is_provided); - } - if (jsxElementType === undefined) { - if (compilerOptions.noImplicitAny) { - error(errorNode, ts.Diagnostics.JSX_element_implicitly_has_type_any_because_the_global_type_JSX_Element_does_not_exist); - } - } - } - function checkJsxOpeningLikeElement(node) { - checkGrammarJsxElement(node); - checkJsxPreconditions(node); - // The reactNamespace symbol should be marked as 'used' so we don't incorrectly elide its import. And if there - // is no reactNamespace symbol in scope when targeting React emit, we should issue an error. - var reactRefErr = compilerOptions.jsx === 2 /* React */ ? ts.Diagnostics.Cannot_find_name_0 : undefined; - var reactNamespace = compilerOptions.reactNamespace ? compilerOptions.reactNamespace : "React"; - var reactSym = resolveName(node.tagName, reactNamespace, 107455 /* Value */, reactRefErr, reactNamespace); - if (reactSym) { - getSymbolLinks(reactSym).referenced = true; - } - var targetAttributesType = getJsxElementAttributesType(node); - var nameTable = ts.createMap(); - // Process this array in right-to-left order so we know which - // attributes (mostly from spreads) are being overwritten and - // thus should have their types ignored - var sawSpreadedAny = false; - for (var i = node.attributes.length - 1; i >= 0; i--) { - if (node.attributes[i].kind === 246 /* JsxAttribute */) { - checkJsxAttribute((node.attributes[i]), targetAttributesType, nameTable); + // In a variable, parameter or property declaration with a type annotation, + // the contextual type of an initializer expression is the type of the variable, parameter or property. + // Otherwise, in a parameter declaration of a contextually typed function expression, + // the contextual type of an initializer expression is the contextual type of the parameter. + // Otherwise, in a variable or parameter declaration with a binding pattern name, + // the contextual type of an initializer expression is the type implied by the binding pattern. + // Otherwise, in a binding pattern inside a variable or parameter declaration, + // the contextual type of an initializer expression is the type annotation of the containing declaration, if present. + function getContextualTypeForInitializerExpression(node) { + var declaration = node.parent; + if (node === declaration.initializer) { + if (declaration.type) { + return getTypeFromTypeNode(declaration.type); } - else { - ts.Debug.assert(node.attributes[i].kind === 247 /* JsxSpreadAttribute */); - var spreadType = checkJsxSpreadAttribute((node.attributes[i]), targetAttributesType, nameTable); - if (isTypeAny(spreadType)) { - sawSpreadedAny = true; + if (declaration.kind === 142 /* Parameter */) { + var type = getContextuallyTypedParameterType(declaration); + if (type) { + return type; } } - } - // Check that all required properties have been provided. If an 'any' - // was spreaded in, though, assume that it provided all required properties - if (targetAttributesType && !sawSpreadedAny) { - var targetProperties = getPropertiesOfType(targetAttributesType); - for (var i = 0; i < targetProperties.length; i++) { - if (!(targetProperties[i].flags & 536870912 /* Optional */) && - !nameTable[targetProperties[i].name]) { - error(node, ts.Diagnostics.Property_0_is_missing_in_type_1, targetProperties[i].name, typeToString(targetAttributesType)); + if (ts.isBindingPattern(declaration.name)) { + return getTypeFromBindingPattern(declaration.name, /*includePatternInType*/ true, /*reportErrors*/ false); + } + if (ts.isBindingPattern(declaration.parent)) { + var parentDeclaration = declaration.parent.parent; + var name_15 = declaration.propertyName || declaration.name; + if (ts.isVariableLike(parentDeclaration) && + parentDeclaration.type && + !ts.isBindingPattern(name_15)) { + var text = getTextOfPropertyName(name_15); + if (text) { + return getTypeOfPropertyOfType(getTypeFromTypeNode(parentDeclaration.type), text); + } } } } + return undefined; } - function checkJsxExpression(node) { - if (node.expression) { - return checkExpression(node.expression); + function getContextualTypeForReturnExpression(node) { + var func = ts.getContainingFunction(node); + if (ts.isAsyncFunctionLike(func)) { + var contextualReturnType = getContextualReturnType(func); + if (contextualReturnType) { + return getPromisedType(contextualReturnType); + } + return undefined; } - else { - return unknownType; + if (func && !func.asteriskToken) { + return getContextualReturnType(func); } + return undefined; } - // If a symbol is a synthesized symbol with no value declaration, we assume it is a property. Example of this are the synthesized - // '.prototype' property as well as synthesized tuple index properties. - function getDeclarationKindFromSymbol(s) { - return s.valueDeclaration ? s.valueDeclaration.kind : 145 /* PropertyDeclaration */; - } - function getDeclarationModifierFlagsFromSymbol(s) { - return s.valueDeclaration ? ts.getCombinedModifierFlags(s.valueDeclaration) : s.flags & 134217728 /* Prototype */ ? 4 /* Public */ | 32 /* Static */ : 0; - } - function getDeclarationNodeFlagsFromSymbol(s) { - return s.valueDeclaration ? ts.getCombinedNodeFlags(s.valueDeclaration) : 0; - } - /** - * Check whether the requested property access is valid. - * Returns true if node is a valid property access, and false otherwise. - * @param node The node to be checked. - * @param left The left hand side of the property access (e.g.: the super in `super.foo`). - * @param type The type of left. - * @param prop The symbol for the right hand side of the property access. - */ - function checkClassPropertyAccess(node, left, type, prop) { - var flags = getDeclarationModifierFlagsFromSymbol(prop); - var declaringClass = getDeclaredTypeOfSymbol(getParentOfSymbol(prop)); - var errorNode = node.kind === 172 /* PropertyAccessExpression */ || node.kind === 218 /* VariableDeclaration */ ? - node.name : - node.right; - if (left.kind === 95 /* SuperKeyword */) { - // TS 1.0 spec (April 2014): 4.8.2 - // - In a constructor, instance member function, instance member accessor, or - // instance member variable initializer where this references a derived class instance, - // a super property access is permitted and must specify a public instance member function of the base class. - // - In a static member function or static member accessor - // where this references the constructor function object of a derived class, - // a super property access is permitted and must specify a public static member function of the base class. - if (languageVersion < 2 /* ES6 */ && getDeclarationKindFromSymbol(prop) !== 147 /* MethodDeclaration */) { - // `prop` refers to a *property* declared in the super class - // rather than a *method*, so it does not satisfy the above criteria. - error(errorNode, ts.Diagnostics.Only_public_and_protected_methods_of_the_base_class_are_accessible_via_the_super_keyword); - return false; - } - if (flags & 128 /* Abstract */) { - // A method cannot be accessed in a super property access if the method is abstract. - // This error could mask a private property access error. But, a member - // cannot simultaneously be private and abstract, so this will trigger an - // additional error elsewhere. - error(errorNode, ts.Diagnostics.Abstract_method_0_in_class_1_cannot_be_accessed_via_super_expression, symbolToString(prop), typeToString(declaringClass)); - return false; + function getContextualTypeForYieldOperand(node) { + var func = ts.getContainingFunction(node); + if (func) { + var contextualReturnType = getContextualReturnType(func); + if (contextualReturnType) { + return node.asteriskToken + ? contextualReturnType + : getElementTypeOfIterableIterator(contextualReturnType); } } - // Public properties are otherwise accessible. - if (!(flags & 24 /* NonPublicAccessibilityModifier */)) { - return true; - } - // Property is known to be private or protected at this point - // Private property is accessible if the property is within the declaring class - if (flags & 8 /* Private */) { - var declaringClassDeclaration = getClassLikeDeclarationOfSymbol(getParentOfSymbol(prop)); - if (!isNodeWithinClass(node, declaringClassDeclaration)) { - error(errorNode, ts.Diagnostics.Property_0_is_private_and_only_accessible_within_class_1, symbolToString(prop), typeToString(declaringClass)); - return false; + return undefined; + } + function isInParameterInitializerBeforeContainingFunction(node) { + while (node.parent && !ts.isFunctionLike(node.parent)) { + if (node.parent.kind === 142 /* Parameter */ && node.parent.initializer === node) { + return true; } - return true; - } - // Property is known to be protected at this point - // All protected properties of a supertype are accessible in a super access - if (left.kind === 95 /* SuperKeyword */) { - return true; - } - // Get the enclosing class that has the declaring class as its base type - var enclosingClass = forEachEnclosingClass(node, function (enclosingDeclaration) { - var enclosingClass = getDeclaredTypeOfSymbol(getSymbolOfNode(enclosingDeclaration)); - return hasBaseType(enclosingClass, declaringClass) ? enclosingClass : undefined; - }); - // A protected property is accessible if the property is within the declaring class or classes derived from it - if (!enclosingClass) { - error(errorNode, ts.Diagnostics.Property_0_is_protected_and_only_accessible_within_class_1_and_its_subclasses, symbolToString(prop), typeToString(declaringClass)); - return false; - } - // No further restrictions for static properties - if (flags & 32 /* Static */) { - return true; + node = node.parent; } - // An instance property must be accessed through an instance of the enclosing class - if (type.flags & 268435456 /* ThisType */) { - // get the original type -- represented as the type constraint of the 'this' type - type = getConstraintOfTypeParameter(type); + return false; + } + function getContextualReturnType(functionDecl) { + // If the containing function has a return type annotation, is a constructor, or is a get accessor whose + // corresponding set accessor has a type annotation, return statements in the function are contextually typed + if (functionDecl.type || + functionDecl.kind === 148 /* Constructor */ || + functionDecl.kind === 149 /* GetAccessor */ && ts.getSetAccessorTypeAnnotationNode(ts.getDeclarationOfKind(functionDecl.symbol, 150 /* SetAccessor */))) { + return getReturnTypeOfSignature(getSignatureFromDeclaration(functionDecl)); } - // TODO: why is the first part of this check here? - if (!(getTargetType(type).flags & (32768 /* Class */ | 65536 /* Interface */) && hasBaseType(type, enclosingClass))) { - error(errorNode, ts.Diagnostics.Property_0_is_protected_and_only_accessible_through_an_instance_of_class_1, symbolToString(prop), typeToString(enclosingClass)); - return false; + // Otherwise, if the containing function is contextually typed by a function type with exactly one call signature + // and that call signature is non-generic, return statements are contextually typed by the return type of the signature + var signature = getContextualSignatureForFunctionLikeDeclaration(functionDecl); + if (signature) { + return getReturnTypeOfSignature(signature); } - return true; + return undefined; } - function checkNonNullExpression(node) { - var type = checkExpression(node); - if (strictNullChecks) { - var kind = getFalsyFlags(type) & 6144 /* Nullable */; - if (kind) { - error(node, kind & 2048 /* Undefined */ ? kind & 4096 /* Null */ ? - ts.Diagnostics.Object_is_possibly_null_or_undefined : - ts.Diagnostics.Object_is_possibly_undefined : - ts.Diagnostics.Object_is_possibly_null); - } - return getNonNullableType(type); + // In a typed function call, an argument or substitution expression is contextually typed by the type of the corresponding parameter. + function getContextualTypeForArgument(callTarget, arg) { + var args = getEffectiveCallArguments(callTarget); + var argIndex = ts.indexOf(args, arg); + if (argIndex >= 0) { + var signature = getResolvedOrAnySignature(callTarget); + return getTypeAtPosition(signature, argIndex); } - return type; - } - function checkPropertyAccessExpression(node) { - return checkPropertyAccessExpressionOrQualifiedName(node, node.expression, node.name); - } - function checkQualifiedName(node) { - return checkPropertyAccessExpressionOrQualifiedName(node, node.left, node.right); + return undefined; } - function checkPropertyAccessExpressionOrQualifiedName(node, left, right) { - var type = checkNonNullExpression(left); - if (isTypeAny(type) || type === silentNeverType) { - return type; - } - var apparentType = getApparentType(getWidenedType(type)); - if (apparentType === unknownType || (type.flags & 16384 /* TypeParameter */ && isTypeAny(apparentType))) { - // handle cases when type is Type parameter with invalid or any constraint - return apparentType; + function getContextualTypeForSubstitutionExpression(template, substitutionExpression) { + if (template.parent.kind === 176 /* TaggedTemplateExpression */) { + return getContextualTypeForArgument(template.parent, substitutionExpression); } - var prop = getPropertyOfType(apparentType, right.text); - if (!prop) { - if (right.text && !checkAndReportErrorForExtendingInterface(node)) { - reportNonexistentProperty(right, type.flags & 268435456 /* ThisType */ ? apparentType : type); + return undefined; + } + function getContextualTypeForBinaryOperand(node) { + var binaryExpression = node.parent; + var operator = binaryExpression.operatorToken.kind; + if (operator >= 56 /* FirstAssignment */ && operator <= 68 /* LastAssignment */) { + // Don't do this for special property assignments to avoid circularity + if (ts.getSpecialPropertyAssignmentKind(binaryExpression) !== 0 /* None */) { + return undefined; } - return unknownType; - } - if (noUnusedIdentifiers && - (prop.flags & 106500 /* ClassMember */) && - prop.valueDeclaration && (ts.getModifierFlags(prop.valueDeclaration) & 8 /* Private */)) { - if (prop.flags & 16777216 /* Instantiated */) { - getSymbolLinks(prop).target.isReferenced = true; + // In an assignment expression, the right operand is contextually typed by the type of the left operand. + if (node === binaryExpression.right) { + return checkExpression(binaryExpression.left); } - else { - prop.isReferenced = true; + } + else if (operator === 52 /* BarBarToken */) { + // When an || expression has a contextual type, the operands are contextually typed by that type. When an || + // expression has no contextual type, the right operand is contextually typed by the type of the left operand. + var type = getContextualType(binaryExpression); + if (!type && node === binaryExpression.right) { + type = checkExpression(binaryExpression.left); } + return type; } - getNodeLinks(node).resolvedSymbol = prop; - if (prop.parent && prop.parent.flags & 32 /* Class */) { - checkClassPropertyAccess(node, left, apparentType, prop); + else if (operator === 51 /* AmpersandAmpersandToken */ || operator === 24 /* CommaToken */) { + if (node === binaryExpression.right) { + return getContextualType(binaryExpression); + } } - var propType = getTypeOfSymbol(prop); - // Only compute control flow type if this is a property access expression that isn't an - // assignment target, and the referenced property was declared as a variable, property, - // accessor, or optional method. - if (node.kind !== 172 /* PropertyAccessExpression */ || ts.isAssignmentTarget(node) || - !(prop.flags & (3 /* Variable */ | 4 /* Property */ | 98304 /* Accessor */)) && - !(prop.flags & 8192 /* Method */ && propType.flags & 524288 /* Union */)) { - return propType; + return undefined; + } + // Apply a mapping function to a contextual type and return the resulting type. If the contextual type + // is a union type, the mapping function is applied to each constituent type and a union of the resulting + // types is returned. + function applyToContextualType(type, mapper) { + if (!(type.flags & 524288 /* Union */)) { + return mapper(type); } - return getFlowTypeOfReference(node, propType, /*assumeInitialized*/ true, /*flowContainer*/ undefined); - function reportNonexistentProperty(propNode, containingType) { - var errorInfo; - if (containingType.flags & 524288 /* Union */ && !(containingType.flags & 8190 /* Primitive */)) { - for (var _i = 0, _a = containingType.types; _i < _a.length; _i++) { - var subtype = _a[_i]; - if (!getPropertyOfType(subtype, propNode.text)) { - errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Property_0_does_not_exist_on_type_1, ts.declarationNameToString(propNode), typeToString(subtype)); - break; - } + var types = type.types; + var mappedType; + var mappedTypes; + for (var _i = 0, types_12 = types; _i < types_12.length; _i++) { + var current = types_12[_i]; + var t = mapper(current); + if (t) { + if (!mappedType) { + mappedType = t; + } + else if (!mappedTypes) { + mappedTypes = [mappedType, t]; + } + else { + mappedTypes.push(t); } } - errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Property_0_does_not_exist_on_type_1, ts.declarationNameToString(propNode), typeToString(containingType)); - diagnostics.add(ts.createDiagnosticForNodeFromMessageChain(propNode, errorInfo)); } + return mappedTypes ? getUnionType(mappedTypes) : mappedType; } - function isValidPropertyAccess(node, propertyName) { - var left = node.kind === 172 /* PropertyAccessExpression */ - ? node.expression - : node.left; - var type = checkExpression(left); - if (type !== unknownType && !isTypeAny(type)) { - var prop = getPropertyOfType(getWidenedType(type), propertyName); - if (prop && prop.parent && prop.parent.flags & 32 /* Class */) { - return checkClassPropertyAccess(node, left, type, prop); - } + function getTypeOfPropertyOfContextualType(type, name) { + return applyToContextualType(type, function (t) { + var prop = t.flags & 4161536 /* StructuredType */ ? getPropertyOfType(t, name) : undefined; + return prop ? getTypeOfSymbol(prop) : undefined; + }); + } + function getIndexTypeOfContextualType(type, kind) { + return applyToContextualType(type, function (t) { return getIndexTypeOfStructuredType(t, kind); }); + } + // Return true if the given contextual type is a tuple-like type + function contextualTypeIsTupleLikeType(type) { + return !!(type.flags & 524288 /* Union */ ? ts.forEach(type.types, isTupleLikeType) : isTupleLikeType(type)); + } + // In an object literal contextually typed by a type T, the contextual type of a property assignment is the type of + // the matching property in T, if one exists. Otherwise, it is the type of the numeric index signature in T, if one + // exists. Otherwise, it is the type of the string index signature in T, if one exists. + function getContextualTypeForObjectLiteralMethod(node) { + ts.Debug.assert(ts.isObjectLiteralMethod(node)); + if (isInsideWithStatementBody(node)) { + // We cannot answer semantic questions within a with block, do not proceed any further + return undefined; } - return true; + return getContextualTypeForObjectLiteralElement(node); } - /** - * Return the symbol of the for-in variable declared or referenced by the given for-in statement. - */ - function getForInVariableSymbol(node) { - var initializer = node.initializer; - if (initializer.kind === 219 /* VariableDeclarationList */) { - var variable = initializer.declarations[0]; - if (variable && !ts.isBindingPattern(variable.name)) { - return getSymbolOfNode(variable); + function getContextualTypeForObjectLiteralElement(element) { + var objectLiteral = element.parent; + var type = getApparentTypeOfContextualType(objectLiteral); + if (type) { + if (!ts.hasDynamicName(element)) { + // For a (non-symbol) computed property, there is no reason to look up the name + // in the type. It will just be "__computed", which does not appear in any + // SymbolTable. + var symbolName = getSymbolOfNode(element).name; + var propertyType = getTypeOfPropertyOfContextualType(type, symbolName); + if (propertyType) { + return propertyType; + } } + return isNumericName(element.name) && getIndexTypeOfContextualType(type, 1 /* Number */) || + getIndexTypeOfContextualType(type, 0 /* String */); } - else if (initializer.kind === 69 /* Identifier */) { - return getResolvedSymbol(initializer); + return undefined; + } + // In an array literal contextually typed by a type T, the contextual type of an element expression at index N is + // the type of the property with the numeric name N in T, if one exists. Otherwise, if T has a numeric index signature, + // it is the type of the numeric index signature in T. Otherwise, in ES6 and higher, the contextual type is the iterated + // type of T. + function getContextualTypeForElementExpression(node) { + var arrayLiteral = node.parent; + var type = getApparentTypeOfContextualType(arrayLiteral); + if (type) { + var index = ts.indexOf(arrayLiteral.elements, node); + return getTypeOfPropertyOfContextualType(type, "" + index) + || getIndexTypeOfContextualType(type, 1 /* Number */) + || (languageVersion >= 2 /* ES6 */ ? getElementTypeOfIterable(type, /*errorNode*/ undefined) : undefined); } return undefined; } - /** - * Return true if the given type is considered to have numeric property names. - */ - function hasNumericPropertyNames(type) { - return getIndexTypeOfType(type, 1 /* Number */) && !getIndexTypeOfType(type, 0 /* String */); + // In a contextually typed conditional expression, the true/false expressions are contextually typed by the same type. + function getContextualTypeForConditionalOperand(node) { + var conditional = node.parent; + return node === conditional.whenTrue || node === conditional.whenFalse ? getContextualType(conditional) : undefined; } - /** - * Return true if given node is an expression consisting of an identifier (possibly parenthesized) - * that references a for-in variable for an object with numeric property names. - */ - function isForInVariableForNumericPropertyNames(expr) { - var e = skipParenthesizedNodes(expr); - if (e.kind === 69 /* Identifier */) { - var symbol = getResolvedSymbol(e); - if (symbol.flags & 3 /* Variable */) { - var child = expr; - var node = expr.parent; - while (node) { - if (node.kind === 207 /* ForInStatement */ && - child === node.statement && - getForInVariableSymbol(node) === symbol && - hasNumericPropertyNames(checkExpression(node.expression))) { - return true; - } - child = node; - node = node.parent; - } + function getContextualTypeForJsxAttribute(attribute) { + var kind = attribute.kind; + var jsxElement = attribute.parent; + var attrsType = getJsxElementAttributesType(jsxElement); + if (attribute.kind === 246 /* JsxAttribute */) { + if (!attrsType || isTypeAny(attrsType)) { + return undefined; } + return getTypeOfPropertyOfType(attrsType, attribute.name.text); } - return false; + else if (attribute.kind === 247 /* JsxSpreadAttribute */) { + return attrsType; + } + ts.Debug.fail("Expected JsxAttribute or JsxSpreadAttribute, got ts.SyntaxKind[" + kind + "]"); } - function checkIndexedAccess(node) { - // Grammar checking - if (!node.argumentExpression) { - var sourceFile = ts.getSourceFileOfNode(node); - if (node.parent.kind === 175 /* NewExpression */ && node.parent.expression === node) { - var start = ts.skipTrivia(sourceFile.text, node.expression.end); - var end = node.end; - grammarErrorAtPos(sourceFile, start, end - start, ts.Diagnostics.new_T_cannot_be_used_to_create_an_array_Use_new_Array_T_instead); - } - else { - var start = node.end - "]".length; - var end = node.end; - grammarErrorAtPos(sourceFile, start, end - start, ts.Diagnostics.Expression_expected); - } + // Return the contextual type for a given expression node. During overload resolution, a contextual type may temporarily + // be "pushed" onto a node using the contextualType property. + function getApparentTypeOfContextualType(node) { + var type = getContextualType(node); + return type && getApparentType(type); + } + /** + * Woah! Do you really want to use this function? + * + * Unless you're trying to get the *non-apparent* type for a + * value-literal type or you're authoring relevant portions of this algorithm, + * you probably meant to use 'getApparentTypeOfContextualType'. + * Otherwise this may not be very useful. + * + * In cases where you *are* working on this function, you should understand + * when it is appropriate to use 'getContextualType' and 'getApparentTypeOfContextualType'. + * + * - Use 'getContextualType' when you are simply going to propagate the result to the expression. + * - Use 'getApparentTypeOfContextualType' when you're going to need the members of the type. + * + * @param node the expression whose contextual type will be returned. + * @returns the contextual type of an expression. + */ + function getContextualType(node) { + if (isInsideWithStatementBody(node)) { + // We cannot answer semantic questions within a with block, do not proceed any further + return undefined; } - // Obtain base constraint such that we can bail out if the constraint is an unknown type - var objectType = getApparentType(checkNonNullExpression(node.expression)); - var indexType = node.argumentExpression ? checkExpression(node.argumentExpression) : unknownType; - if (objectType === unknownType || objectType === silentNeverType) { - return objectType; + if (node.contextualType) { + return node.contextualType; } - var isConstEnum = isConstEnumObjectType(objectType); - if (isConstEnum && - (!node.argumentExpression || node.argumentExpression.kind !== 9 /* StringLiteral */)) { - error(node.argumentExpression, ts.Diagnostics.A_const_enum_member_can_only_be_accessed_using_a_string_literal); - return unknownType; - } - // TypeScript 1.0 spec (April 2014): 4.10 Property Access - // - If IndexExpr is a string literal or a numeric literal and ObjExpr's apparent type has a property with the name - // given by that literal(converted to its string representation in the case of a numeric literal), the property access is of the type of that property. - // - Otherwise, if ObjExpr's apparent type has a numeric index signature and IndexExpr is of type Any, the Number primitive type, or an enum type, - // the property access is of the type of that index signature. - // - Otherwise, if ObjExpr's apparent type has a string index signature and IndexExpr is of type Any, the String or Number primitive type, or an enum type, - // the property access is of the type of that index signature. - // - Otherwise, if IndexExpr is of type Any, the String or Number primitive type, or an enum type, the property access is of type Any. - // See if we can index as a property. - if (node.argumentExpression) { - var name_16 = getPropertyNameForIndexedAccess(node.argumentExpression, indexType); - if (name_16 !== undefined) { - var prop = getPropertyOfType(objectType, name_16); - if (prop) { - getNodeLinks(node).resolvedSymbol = prop; - return getTypeOfSymbol(prop); - } - else if (isConstEnum) { - error(node.argumentExpression, ts.Diagnostics.Property_0_does_not_exist_on_const_enum_1, name_16, symbolToString(objectType.symbol)); - return unknownType; - } - } + var parent = node.parent; + switch (parent.kind) { + case 218 /* VariableDeclaration */: + case 142 /* Parameter */: + case 145 /* PropertyDeclaration */: + case 144 /* PropertySignature */: + case 169 /* BindingElement */: + return getContextualTypeForInitializerExpression(node); + case 180 /* ArrowFunction */: + case 211 /* ReturnStatement */: + return getContextualTypeForReturnExpression(node); + case 190 /* YieldExpression */: + return getContextualTypeForYieldOperand(parent); + case 174 /* CallExpression */: + case 175 /* NewExpression */: + return getContextualTypeForArgument(parent, node); + case 177 /* TypeAssertionExpression */: + case 195 /* AsExpression */: + return getTypeFromTypeNode(parent.type); + case 187 /* BinaryExpression */: + return getContextualTypeForBinaryOperand(node); + case 253 /* PropertyAssignment */: + case 254 /* ShorthandPropertyAssignment */: + return getContextualTypeForObjectLiteralElement(parent); + case 170 /* ArrayLiteralExpression */: + return getContextualTypeForElementExpression(node); + case 188 /* ConditionalExpression */: + return getContextualTypeForConditionalOperand(node); + case 197 /* TemplateSpan */: + ts.Debug.assert(parent.parent.kind === 189 /* TemplateExpression */); + return getContextualTypeForSubstitutionExpression(parent.parent, node); + case 178 /* ParenthesizedExpression */: + return getContextualType(parent); + case 248 /* JsxExpression */: + return getContextualType(parent); + case 246 /* JsxAttribute */: + case 247 /* JsxSpreadAttribute */: + return getContextualTypeForJsxAttribute(parent); } - // Check for compatible indexer types. - var allowedNullableFlags = strictNullChecks ? 0 : 6144 /* Nullable */; - if (isTypeAnyOrAllConstituentTypesHaveKind(indexType, 34 /* StringLike */ | 340 /* NumberLike */ | 512 /* ESSymbol */ | allowedNullableFlags)) { - // Try to use a number indexer. - if (isTypeAnyOrAllConstituentTypesHaveKind(indexType, 340 /* NumberLike */ | allowedNullableFlags) || isForInVariableForNumericPropertyNames(node.argumentExpression)) { - var numberIndexInfo = getIndexInfoOfType(objectType, 1 /* Number */); - if (numberIndexInfo) { - getNodeLinks(node).resolvedIndexInfo = numberIndexInfo; - return numberIndexInfo.type; - } - } - // Try to use string indexing. - var stringIndexInfo = getIndexInfoOfType(objectType, 0 /* String */); - if (stringIndexInfo) { - getNodeLinks(node).resolvedIndexInfo = stringIndexInfo; - return stringIndexInfo.type; - } - // Fall back to any. - if (compilerOptions.noImplicitAny && !compilerOptions.suppressImplicitAnyIndexErrors && !isTypeAny(objectType)) { - error(node, getIndexTypeOfType(objectType, 1 /* Number */) ? - ts.Diagnostics.Element_implicitly_has_an_any_type_because_index_expression_is_not_of_type_number : - ts.Diagnostics.Index_signature_of_object_type_implicitly_has_an_any_type); + return undefined; + } + // If the given type is an object or union type, if that type has a single signature, and if + // that signature is non-generic, return the signature. Otherwise return undefined. + function getNonGenericSignature(type) { + var signatures = getSignaturesOfStructuredType(type, 0 /* Call */); + if (signatures.length === 1) { + var signature = signatures[0]; + if (!signature.typeParameters) { + return signature; } - return anyType; } - // REVIEW: Users should know the type that was actually used. - error(node, ts.Diagnostics.An_index_expression_argument_must_be_of_type_string_number_symbol_or_any); - return unknownType; } - /** - * If indexArgumentExpression is a string literal or number literal, returns its text. - * If indexArgumentExpression is a constant value, returns its string value. - * If indexArgumentExpression is a well known symbol, returns the property name corresponding - * to this symbol, as long as it is a proper symbol reference. - * Otherwise, returns undefined. - */ - function getPropertyNameForIndexedAccess(indexArgumentExpression, indexArgumentType) { - if (indexArgumentExpression.kind === 9 /* StringLiteral */ || indexArgumentExpression.kind === 8 /* NumericLiteral */) { - return indexArgumentExpression.text; + function isFunctionExpressionOrArrowFunction(node) { + return node.kind === 179 /* FunctionExpression */ || node.kind === 180 /* ArrowFunction */; + } + function getContextualSignatureForFunctionLikeDeclaration(node) { + // Only function expressions, arrow functions, and object literal methods are contextually typed. + return isFunctionExpressionOrArrowFunction(node) || ts.isObjectLiteralMethod(node) + ? getContextualSignature(node) + : undefined; + } + function getContextualTypeForFunctionLikeDeclaration(node) { + return ts.isObjectLiteralMethod(node) ? + getContextualTypeForObjectLiteralMethod(node) : + getApparentTypeOfContextualType(node); + } + // Return the contextual signature for a given expression node. A contextual type provides a + // contextual signature if it has a single call signature and if that call signature is non-generic. + // If the contextual type is a union type, get the signature from each type possible and if they are + // all identical ignoring their return type, the result is same signature but with return type as + // union type of return types from these signatures + function getContextualSignature(node) { + ts.Debug.assert(node.kind !== 147 /* MethodDeclaration */ || ts.isObjectLiteralMethod(node)); + var type = getContextualTypeForFunctionLikeDeclaration(node); + if (!type) { + return undefined; } - if (indexArgumentExpression.kind === 173 /* ElementAccessExpression */ || indexArgumentExpression.kind === 172 /* PropertyAccessExpression */) { - var value = getConstantValue(indexArgumentExpression); - if (value !== undefined) { - return value.toString(); + if (!(type.flags & 524288 /* Union */)) { + return getNonGenericSignature(type); + } + var signatureList; + var types = type.types; + for (var _i = 0, types_13 = types; _i < types_13.length; _i++) { + var current = types_13[_i]; + var signature = getNonGenericSignature(current); + if (signature) { + if (!signatureList) { + // This signature will contribute to contextual union signature + signatureList = [signature]; + } + else if (!compareSignaturesIdentical(signatureList[0], signature, /*partialMatch*/ false, /*ignoreThisTypes*/ true, /*ignoreReturnTypes*/ true, compareTypesIdentical)) { + // Signatures aren't identical, do not use + return undefined; + } + else { + // Use this signature for contextual union signature + signatureList.push(signature); + } } } - if (checkThatExpressionIsProperSymbolReference(indexArgumentExpression, indexArgumentType, /*reportError*/ false)) { - var rightHandSideName = indexArgumentExpression.name.text; - return ts.getPropertyNameForKnownSymbolName(rightHandSideName); + // Result is union of signatures collected (return type is union of return types of this signature set) + var result; + if (signatureList) { + result = cloneSignature(signatureList[0]); + // Clear resolved return type we possibly got from cloneSignature + result.resolvedReturnType = undefined; + result.unionSignatures = signatureList; } - return undefined; + return result; } /** - * A proper symbol reference requires the following: - * 1. The property access denotes a property that exists - * 2. The expression is of the form Symbol. - * 3. The property access is of the primitive type symbol. - * 4. Symbol in this context resolves to the global Symbol object + * Detect if the mapper implies an inference context. Specifically, there are 4 possible values + * for a mapper. Let's go through each one of them: + * + * 1. undefined - this means we are not doing inferential typing, but we may do contextual typing, + * which could cause us to assign a parameter a type + * 2. identityMapper - means we want to avoid assigning a parameter a type, whether or not we are in + * inferential typing (context is undefined for the identityMapper) + * 3. a mapper created by createInferenceMapper - we are doing inferential typing, we want to assign + * types to parameters and fix type parameters (context is defined) + * 4. an instantiation mapper created by createTypeMapper or createTypeEraser - this should never be + * passed as the contextual mapper when checking an expression (context is undefined for these) + * + * isInferentialContext is detecting if we are in case 3 */ - function checkThatExpressionIsProperSymbolReference(expression, expressionType, reportError) { - if (expressionType === unknownType) { - // There is already an error, so no need to report one. - return false; - } - if (!ts.isWellKnownSymbolSyntactically(expression)) { - return false; - } - // Make sure the property type is the primitive symbol type - if ((expressionType.flags & 512 /* ESSymbol */) === 0) { - if (reportError) { - error(expression, ts.Diagnostics.A_computed_property_name_of_the_form_0_must_be_of_type_symbol, ts.getTextOfNode(expression)); - } - return false; - } - // The name is Symbol., so make sure Symbol actually resolves to the - // global Symbol object - var leftHandSide = expression.expression; - var leftHandSideSymbol = getResolvedSymbol(leftHandSide); - if (!leftHandSideSymbol) { - return false; - } - var globalESSymbol = getGlobalESSymbolConstructorSymbol(); - if (!globalESSymbol) { - // Already errored when we tried to look up the symbol - return false; - } - if (leftHandSideSymbol !== globalESSymbol) { - if (reportError) { - error(leftHandSide, ts.Diagnostics.Symbol_reference_does_not_refer_to_the_global_Symbol_constructor_object); - } - return false; - } - return true; + function isInferentialContext(mapper) { + return mapper && mapper.context; } - function resolveUntypedCall(node) { - if (node.kind === 176 /* TaggedTemplateExpression */) { - checkExpression(node.template); - } - else if (node.kind !== 143 /* Decorator */) { - ts.forEach(node.arguments, function (argument) { - checkExpression(argument); - }); - } - return anySignature; + function checkSpreadElementExpression(node, contextualMapper) { + // It is usually not safe to call checkExpressionCached if we can be contextually typing. + // You can tell that we are contextually typing because of the contextualMapper parameter. + // While it is true that a spread element can have a contextual type, it does not do anything + // with this type. It is neither affected by it, nor does it propagate it to its operand. + // So the fact that contextualMapper is passed is not important, because the operand of a spread + // element is not contextually typed. + var arrayOrIterableType = checkExpressionCached(node.expression, contextualMapper); + return checkIteratedTypeOrElementType(arrayOrIterableType, node.expression, /*allowStringInput*/ false); } - function resolveErrorCall(node) { - resolveUntypedCall(node); - return unknownSignature; + function hasDefaultValue(node) { + return (node.kind === 169 /* BindingElement */ && !!node.initializer) || + (node.kind === 187 /* BinaryExpression */ && node.operatorToken.kind === 56 /* EqualsToken */); } - // Re-order candidate signatures into the result array. Assumes the result array to be empty. - // The candidate list orders groups in reverse, but within a group signatures are kept in declaration order - // A nit here is that we reorder only signatures that belong to the same symbol, - // so order how inherited signatures are processed is still preserved. - // interface A { (x: string): void } - // interface B extends A { (x: 'foo'): string } - // const b: B; - // b('foo') // <- here overloads should be processed as [(x:'foo'): string, (x: string): void] - function reorderCandidates(signatures, result) { - var lastParent; - var lastSymbol; - var cutoffIndex = 0; - var index; - var specializedIndex = -1; - var spliceIndex; - ts.Debug.assert(!result.length); - for (var _i = 0, signatures_2 = signatures; _i < signatures_2.length; _i++) { - var signature = signatures_2[_i]; - var symbol = signature.declaration && getSymbolOfNode(signature.declaration); - var parent_10 = signature.declaration && signature.declaration.parent; - if (!lastSymbol || symbol === lastSymbol) { - if (lastParent && parent_10 === lastParent) { - index++; - } - else { - lastParent = parent_10; - index = cutoffIndex; + function checkArrayLiteral(node, contextualMapper) { + var elements = node.elements; + var hasSpreadElement = false; + var elementTypes = []; + var inDestructuringPattern = ts.isAssignmentTarget(node); + for (var _i = 0, elements_1 = elements; _i < elements_1.length; _i++) { + var e = elements_1[_i]; + if (inDestructuringPattern && e.kind === 191 /* SpreadElementExpression */) { + // Given the following situation: + // var c: {}; + // [...c] = ["", 0]; + // + // c is represented in the tree as a spread element in an array literal. + // But c really functions as a rest element, and its purpose is to provide + // a contextual type for the right hand side of the assignment. Therefore, + // instead of calling checkExpression on "...c", which will give an error + // if c is not iterable/array-like, we need to act as if we are trying to + // get the contextual element type from it. So we do something similar to + // getContextualTypeForElementExpression, which will crucially not error + // if there is no index type / iterated type. + var restArrayType = checkExpression(e.expression, contextualMapper); + var restElementType = getIndexTypeOfType(restArrayType, 1 /* Number */) || + (languageVersion >= 2 /* ES6 */ ? getElementTypeOfIterable(restArrayType, /*errorNode*/ undefined) : undefined); + if (restElementType) { + elementTypes.push(restElementType); } } else { - // current declaration belongs to a different symbol - // set cutoffIndex so re-orderings in the future won't change result set from 0 to cutoffIndex - index = cutoffIndex = result.length; - lastParent = parent_10; + var type = checkExpressionForMutableLocation(e, contextualMapper); + elementTypes.push(type); } - lastSymbol = symbol; - // specialized signatures always need to be placed before non-specialized signatures regardless - // of the cutoff position; see GH#1133 - if (signature.hasLiteralTypes) { - specializedIndex++; - spliceIndex = specializedIndex; - // The cutoff index always needs to be greater than or equal to the specialized signature index - // in order to prevent non-specialized signatures from being added before a specialized - // signature. - cutoffIndex++; + hasSpreadElement = hasSpreadElement || e.kind === 191 /* SpreadElementExpression */; + } + if (!hasSpreadElement) { + // If array literal is actually a destructuring pattern, mark it as an implied type. We do this such + // that we get the same behavior for "var [x, y] = []" and "[x, y] = []". + if (inDestructuringPattern && elementTypes.length) { + var type = cloneTypeReference(createTupleType(elementTypes)); + type.pattern = node; + return type; } - else { - spliceIndex = index; + var contextualType = getApparentTypeOfContextualType(node); + if (contextualType && contextualTypeIsTupleLikeType(contextualType)) { + var pattern = contextualType.pattern; + // If array literal is contextually typed by a binding pattern or an assignment pattern, pad the resulting + // tuple type with the corresponding binding or assignment element types to make the lengths equal. + if (pattern && (pattern.kind === 168 /* ArrayBindingPattern */ || pattern.kind === 170 /* ArrayLiteralExpression */)) { + var patternElements = pattern.elements; + for (var i = elementTypes.length; i < patternElements.length; i++) { + var patternElement = patternElements[i]; + if (hasDefaultValue(patternElement)) { + elementTypes.push(contextualType.typeArguments[i]); + } + else { + if (patternElement.kind !== 193 /* OmittedExpression */) { + error(patternElement, ts.Diagnostics.Initializer_provides_no_value_for_this_binding_element_and_the_binding_element_has_no_default_value); + } + elementTypes.push(unknownType); + } + } + } + if (elementTypes.length) { + return createTupleType(elementTypes); + } } - result.splice(spliceIndex, 0, signature); } + return createArrayType(elementTypes.length ? + getUnionType(elementTypes, /*subtypeReduction*/ true) : + strictNullChecks ? neverType : undefinedWideningType); } - function getSpreadArgumentIndex(args) { - for (var i = 0; i < args.length; i++) { - var arg = args[i]; - if (arg && arg.kind === 191 /* SpreadElementExpression */) { - return i; - } - } - return -1; + function isNumericName(name) { + return name.kind === 140 /* ComputedPropertyName */ ? isNumericComputedName(name) : isNumericLiteralName(name.text); } - function hasCorrectArity(node, args, signature, signatureHelpTrailingComma) { - if (signatureHelpTrailingComma === void 0) { signatureHelpTrailingComma = false; } - var argCount; // Apparent number of arguments we will have in this call - var typeArguments; // Type arguments (undefined if none) - var callIsIncomplete; // In incomplete call we want to be lenient when we have too few arguments - var isDecorator; - var spreadArgIndex = -1; - if (node.kind === 176 /* TaggedTemplateExpression */) { - var tagExpression = node; - // Even if the call is incomplete, we'll have a missing expression as our last argument, - // so we can say the count is just the arg list length - argCount = args.length; - typeArguments = undefined; - if (tagExpression.template.kind === 189 /* TemplateExpression */) { - // If a tagged template expression lacks a tail literal, the call is incomplete. - // Specifically, a template only can end in a TemplateTail or a Missing literal. - var templateExpression = tagExpression.template; - var lastSpan = ts.lastOrUndefined(templateExpression.templateSpans); - ts.Debug.assert(lastSpan !== undefined); // we should always have at least one span. - callIsIncomplete = ts.nodeIsMissing(lastSpan.literal) || !!lastSpan.literal.isUnterminated; + function isNumericComputedName(name) { + // It seems odd to consider an expression of type Any to result in a numeric name, + // but this behavior is consistent with checkIndexedAccess + return isTypeAnyOrAllConstituentTypesHaveKind(checkComputedPropertyName(name), 340 /* NumberLike */); + } + function isTypeAnyOrAllConstituentTypesHaveKind(type, kind) { + return isTypeAny(type) || isTypeOfKind(type, kind); + } + function isInfinityOrNaNString(name) { + return name === "Infinity" || name === "-Infinity" || name === "NaN"; + } + function isNumericLiteralName(name) { + // The intent of numeric names is that + // - they are names with text in a numeric form, and that + // - setting properties/indexing with them is always equivalent to doing so with the numeric literal 'numLit', + // acquired by applying the abstract 'ToNumber' operation on the name's text. + // + // The subtlety is in the latter portion, as we cannot reliably say that anything that looks like a numeric literal is a numeric name. + // In fact, it is the case that the text of the name must be equal to 'ToString(numLit)' for this to hold. + // + // Consider the property name '"0xF00D"'. When one indexes with '0xF00D', they are actually indexing with the value of 'ToString(0xF00D)' + // according to the ECMAScript specification, so it is actually as if the user indexed with the string '"61453"'. + // Thus, the text of all numeric literals equivalent to '61543' such as '0xF00D', '0xf00D', '0170015', etc. are not valid numeric names + // because their 'ToString' representation is not equal to their original text. + // This is motivated by ECMA-262 sections 9.3.1, 9.8.1, 11.1.5, and 11.2.1. + // + // Here, we test whether 'ToString(ToNumber(name))' is exactly equal to 'name'. + // The '+' prefix operator is equivalent here to applying the abstract ToNumber operation. + // Applying the 'toString()' method on a number gives us the abstract ToString operation on a number. + // + // Note that this accepts the values 'Infinity', '-Infinity', and 'NaN', and that this is intentional. + // This is desired behavior, because when indexing with them as numeric entities, you are indexing + // with the strings '"Infinity"', '"-Infinity"', and '"NaN"' respectively. + return (+name).toString() === name; + } + function checkComputedPropertyName(node) { + var links = getNodeLinks(node.expression); + if (!links.resolvedType) { + links.resolvedType = checkExpression(node.expression); + // This will allow types number, string, symbol or any. It will also allow enums, the unknown + // type, and any union of these types (like string | number). + if (!isTypeAnyOrAllConstituentTypesHaveKind(links.resolvedType, 340 /* NumberLike */ | 34 /* StringLike */ | 512 /* ESSymbol */)) { + error(node, ts.Diagnostics.A_computed_property_name_must_be_of_type_string_number_symbol_or_any); } else { - // If the template didn't end in a backtick, or its beginning occurred right prior to EOF, - // then this might actually turn out to be a TemplateHead in the future; - // so we consider the call to be incomplete. - var templateLiteral = tagExpression.template; - ts.Debug.assert(templateLiteral.kind === 11 /* NoSubstitutionTemplateLiteral */); - callIsIncomplete = !!templateLiteral.isUnterminated; - } - } - else if (node.kind === 143 /* Decorator */) { - isDecorator = true; - typeArguments = undefined; - argCount = getEffectiveArgumentCount(node, /*args*/ undefined, signature); - } - else { - var callExpression = node; - if (!callExpression.arguments) { - // This only happens when we have something of the form: 'new C' - ts.Debug.assert(callExpression.kind === 175 /* NewExpression */); - return signature.minArgumentCount === 0; + checkThatExpressionIsProperSymbolReference(node.expression, links.resolvedType, /*reportError*/ true); } - argCount = signatureHelpTrailingComma ? args.length + 1 : args.length; - // If we are missing the close paren, the call is incomplete. - callIsIncomplete = callExpression.arguments.end === callExpression.end; - typeArguments = callExpression.typeArguments; - spreadArgIndex = getSpreadArgumentIndex(args); - } - // If the user supplied type arguments, but the number of type arguments does not match - // the declared number of type parameters, the call has an incorrect arity. - var hasRightNumberOfTypeArgs = !typeArguments || - (signature.typeParameters && typeArguments.length === signature.typeParameters.length); - if (!hasRightNumberOfTypeArgs) { - return false; - } - // If spread arguments are present, check that they correspond to a rest parameter. If so, no - // further checking is necessary. - if (spreadArgIndex >= 0) { - return isRestParameterIndex(signature, spreadArgIndex); - } - // Too many arguments implies incorrect arity. - if (!signature.hasRestParameter && argCount > signature.parameters.length) { - return false; } - // If the call is incomplete, we should skip the lower bound check. - var hasEnoughArguments = argCount >= signature.minArgumentCount; - return callIsIncomplete || hasEnoughArguments; + return links.resolvedType; } - // If type has a single call signature and no other members, return that signature. Otherwise, return undefined. - function getSingleCallSignature(type) { - if (type.flags & 2588672 /* ObjectType */) { - var resolved = resolveStructuredTypeMembers(type); - if (resolved.callSignatures.length === 1 && resolved.constructSignatures.length === 0 && - resolved.properties.length === 0 && !resolved.stringIndexInfo && !resolved.numberIndexInfo) { - return resolved.callSignatures[0]; + function getObjectLiteralIndexInfo(node, properties, kind) { + var propTypes = []; + for (var i = 0; i < properties.length; i++) { + if (kind === 0 /* String */ || isNumericName(node.properties[i].name)) { + propTypes.push(getTypeOfSymbol(properties[i])); } } - return undefined; - } - // Instantiate a generic signature in the context of a non-generic signature (section 3.8.5 in TypeScript spec) - function instantiateSignatureInContextOf(signature, contextualSignature, contextualMapper) { - var context = createInferenceContext(signature, /*inferUnionTypes*/ true); - forEachMatchingParameterType(contextualSignature, signature, function (source, target) { - // Type parameters from outer context referenced by source type are fixed by instantiation of the source type - inferTypes(context, instantiateType(source, contextualMapper), target); - }); - return getSignatureInstantiation(signature, getInferredTypes(context)); + var unionType = propTypes.length ? getUnionType(propTypes, /*subtypeReduction*/ true) : undefinedType; + return createIndexInfo(unionType, /*isReadonly*/ false); } - function inferTypeArguments(node, signature, args, excludeArgument, context) { - var typeParameters = signature.typeParameters; - var inferenceMapper = getInferenceMapper(context); - // Clear out all the inference results from the last time inferTypeArguments was called on this context - for (var i = 0; i < typeParameters.length; i++) { - // As an optimization, we don't have to clear (and later recompute) inferred types - // for type parameters that have already been fixed on the previous call to inferTypeArguments. - // It would be just as correct to reset all of them. But then we'd be repeating the same work - // for the type parameters that were fixed, namely the work done by getInferredType. - if (!context.inferences[i].isFixed) { - context.inferredTypes[i] = undefined; - } - } - // On this call to inferTypeArguments, we may get more inferences for certain type parameters that were not - // fixed last time. This means that a type parameter that failed inference last time may succeed this time, - // or vice versa. Therefore, the failedTypeParameterIndex is useless if it points to an unfixed type parameter, - // because it may change. So here we reset it. However, getInferredType will not revisit any type parameters - // that were previously fixed. So if a fixed type parameter failed previously, it will fail again because - // it will contain the exact same set of inferences. So if we reset the index from a fixed type parameter, - // we will lose information that we won't recover this time around. - if (context.failedTypeParameterIndex !== undefined && !context.inferences[context.failedTypeParameterIndex].isFixed) { - context.failedTypeParameterIndex = undefined; - } - var thisType = getThisTypeOfSignature(signature); - if (thisType) { - var thisArgumentNode = getThisArgumentOfCall(node); - var thisArgumentType = thisArgumentNode ? checkExpression(thisArgumentNode) : voidType; - inferTypes(context, thisArgumentType, thisType); - } - // We perform two passes over the arguments. In the first pass we infer from all arguments, but use - // wildcards for all context sensitive function expressions. - var argCount = getEffectiveArgumentCount(node, args, signature); - for (var i = 0; i < argCount; i++) { - var arg = getEffectiveArgument(node, args, i); - // If the effective argument is 'undefined', then it is an argument that is present but is synthetic. - if (arg === undefined || arg.kind !== 193 /* OmittedExpression */) { - var paramType = getTypeAtPosition(signature, i); - var argType = getEffectiveArgumentType(node, i, arg); - // If the effective argument type is 'undefined', there is no synthetic type - // for the argument. In that case, we should check the argument. - if (argType === undefined) { - // For context sensitive arguments we pass the identityMapper, which is a signal to treat all - // context sensitive function expressions as wildcards - var mapper = excludeArgument && excludeArgument[i] !== undefined ? identityMapper : inferenceMapper; - argType = checkExpressionWithContextualType(arg, paramType, mapper); + function checkObjectLiteral(node, contextualMapper) { + var inDestructuringPattern = ts.isAssignmentTarget(node); + // Grammar checking + checkGrammarObjectLiteralExpression(node, inDestructuringPattern); + var propertiesTable = ts.createMap(); + var propertiesArray = []; + var contextualType = getApparentTypeOfContextualType(node); + var contextualTypeHasPattern = contextualType && contextualType.pattern && + (contextualType.pattern.kind === 167 /* ObjectBindingPattern */ || contextualType.pattern.kind === 171 /* ObjectLiteralExpression */); + var typeFlags = 0; + var patternWithComputedProperties = false; + var hasComputedStringProperty = false; + var hasComputedNumberProperty = false; + for (var _i = 0, _a = node.properties; _i < _a.length; _i++) { + var memberDecl = _a[_i]; + var member = memberDecl.symbol; + if (memberDecl.kind === 253 /* PropertyAssignment */ || + memberDecl.kind === 254 /* ShorthandPropertyAssignment */ || + ts.isObjectLiteralMethod(memberDecl)) { + var type = void 0; + if (memberDecl.kind === 253 /* PropertyAssignment */) { + type = checkPropertyAssignment(memberDecl, contextualMapper); } - inferTypes(context, argType, paramType); + else if (memberDecl.kind === 147 /* MethodDeclaration */) { + type = checkObjectLiteralMethod(memberDecl, contextualMapper); + } + else { + ts.Debug.assert(memberDecl.kind === 254 /* ShorthandPropertyAssignment */); + type = checkExpressionForMutableLocation(memberDecl.name, contextualMapper); + } + typeFlags |= type.flags; + var prop = createSymbol(4 /* Property */ | 67108864 /* Transient */ | member.flags, member.name); + if (inDestructuringPattern) { + // If object literal is an assignment pattern and if the assignment pattern specifies a default value + // for the property, make the property optional. + var isOptional = (memberDecl.kind === 253 /* PropertyAssignment */ && hasDefaultValue(memberDecl.initializer)) || + (memberDecl.kind === 254 /* ShorthandPropertyAssignment */ && memberDecl.objectAssignmentInitializer); + if (isOptional) { + prop.flags |= 536870912 /* Optional */; + } + if (ts.hasDynamicName(memberDecl)) { + patternWithComputedProperties = true; + } + } + else if (contextualTypeHasPattern && + !(contextualType.flags & 2588672 /* ObjectType */ && contextualType.isObjectLiteralPatternWithComputedProperties)) { + // If object literal is contextually typed by the implied type of a binding pattern, and if the + // binding pattern specifies a default value for the property, make the property optional. + var impliedProp = getPropertyOfType(contextualType, member.name); + if (impliedProp) { + prop.flags |= impliedProp.flags & 536870912 /* Optional */; + } + else if (!compilerOptions.suppressExcessPropertyErrors) { + error(memberDecl.name, ts.Diagnostics.Object_literal_may_only_specify_known_properties_and_0_does_not_exist_in_type_1, symbolToString(member), typeToString(contextualType)); + } + } + prop.declarations = member.declarations; + prop.parent = member.parent; + if (member.valueDeclaration) { + prop.valueDeclaration = member.valueDeclaration; + } + prop.type = type; + prop.target = member; + member = prop; } - } - // In the second pass we visit only context sensitive arguments, and only those that aren't excluded, this - // time treating function expressions normally (which may cause previously inferred type arguments to be fixed - // as we construct types for contextually typed parameters) - // Decorators will not have `excludeArgument`, as their arguments cannot be contextually typed. - // Tagged template expressions will always have `undefined` for `excludeArgument[0]`. - if (excludeArgument) { - for (var i = 0; i < argCount; i++) { - // No need to check for omitted args and template expressions, their exclusion value is always undefined - if (excludeArgument[i] === false) { - var arg = args[i]; - var paramType = getTypeAtPosition(signature, i); - inferTypes(context, checkExpressionWithContextualType(arg, paramType, inferenceMapper), paramType); + else { + // TypeScript 1.0 spec (April 2014) + // A get accessor declaration is processed in the same manner as + // an ordinary function declaration(section 6.1) with no parameters. + // A set accessor declaration is processed in the same manner + // as an ordinary function declaration with a single parameter and a Void return type. + ts.Debug.assert(memberDecl.kind === 149 /* GetAccessor */ || memberDecl.kind === 150 /* SetAccessor */); + checkAccessorDeclaration(memberDecl); + } + if (ts.hasDynamicName(memberDecl)) { + if (isNumericName(memberDecl.name)) { + hasComputedNumberProperty = true; + } + else { + hasComputedStringProperty = true; } } + else { + propertiesTable[member.name] = member; + } + propertiesArray.push(member); } - getInferredTypes(context); - } - function checkTypeArguments(signature, typeArgumentNodes, typeArgumentTypes, reportErrors, headMessage) { - var typeParameters = signature.typeParameters; - var typeArgumentsAreAssignable = true; - var mapper; - for (var i = 0; i < typeParameters.length; i++) { - if (typeArgumentsAreAssignable /* so far */) { - var constraint = getConstraintOfTypeParameter(typeParameters[i]); - if (constraint) { - var errorInfo = void 0; - var typeArgumentHeadMessage = ts.Diagnostics.Type_0_does_not_satisfy_the_constraint_1; - if (reportErrors && headMessage) { - errorInfo = ts.chainDiagnosticMessages(errorInfo, typeArgumentHeadMessage); - typeArgumentHeadMessage = headMessage; - } - if (!mapper) { - mapper = createTypeMapper(typeParameters, typeArgumentTypes); + // If object literal is contextually typed by the implied type of a binding pattern, augment the result + // type with those properties for which the binding pattern specifies a default value. + if (contextualTypeHasPattern) { + for (var _b = 0, _c = getPropertiesOfType(contextualType); _b < _c.length; _b++) { + var prop = _c[_b]; + if (!propertiesTable[prop.name]) { + if (!(prop.flags & 536870912 /* Optional */)) { + error(prop.valueDeclaration || prop.bindingElement, ts.Diagnostics.Initializer_provides_no_value_for_this_binding_element_and_the_binding_element_has_no_default_value); } - var typeArgument = typeArgumentTypes[i]; - typeArgumentsAreAssignable = checkTypeAssignableTo(typeArgument, getTypeWithThisArgument(instantiateType(constraint, mapper), typeArgument), reportErrors ? typeArgumentNodes[i] : undefined, typeArgumentHeadMessage, errorInfo); + propertiesTable[prop.name] = prop; + propertiesArray.push(prop); } } } - return typeArgumentsAreAssignable; + var stringIndexInfo = hasComputedStringProperty ? getObjectLiteralIndexInfo(node, propertiesArray, 0 /* String */) : undefined; + var numberIndexInfo = hasComputedNumberProperty ? getObjectLiteralIndexInfo(node, propertiesArray, 1 /* Number */) : undefined; + var result = createAnonymousType(node.symbol, propertiesTable, emptyArray, emptyArray, stringIndexInfo, numberIndexInfo); + var freshObjectLiteralFlag = compilerOptions.suppressExcessPropertyErrors ? 0 : 16777216 /* FreshLiteral */; + result.flags |= 8388608 /* ObjectLiteral */ | 67108864 /* ContainsObjectLiteral */ | freshObjectLiteralFlag | (typeFlags & 234881024 /* PropagatingFlags */); + if (patternWithComputedProperties) { + result.isObjectLiteralPatternWithComputedProperties = true; + } + if (inDestructuringPattern) { + result.pattern = node; + } + return result; } - function checkApplicableSignature(node, args, signature, relation, excludeArgument, reportErrors) { - var thisType = getThisTypeOfSignature(signature); - if (thisType && thisType !== voidType && node.kind !== 175 /* NewExpression */) { - // If the called expression is not of the form `x.f` or `x["f"]`, then sourceType = voidType - // If the signature's 'this' type is voidType, then the check is skipped -- anything is compatible. - // If the expression is a new expression, then the check is skipped. - var thisArgumentNode = getThisArgumentOfCall(node); - var thisArgumentType = thisArgumentNode ? checkExpression(thisArgumentNode) : voidType; - var errorNode = reportErrors ? (thisArgumentNode || node) : undefined; - var headMessage_1 = ts.Diagnostics.The_this_context_of_type_0_is_not_assignable_to_method_s_this_of_type_1; - if (!checkTypeRelatedTo(thisArgumentType, getThisTypeOfSignature(signature), relation, errorNode, headMessage_1)) { - return false; - } + function checkJsxSelfClosingElement(node) { + checkJsxOpeningLikeElement(node); + return jsxElementType || anyType; + } + function checkJsxElement(node) { + // Check attributes + checkJsxOpeningLikeElement(node.openingElement); + // Perform resolution on the closing tag so that rename/go to definition/etc work + if (isJsxIntrinsicIdentifier(node.closingElement.tagName)) { + getIntrinsicTagSymbol(node.closingElement); } - var headMessage = ts.Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1; - var argCount = getEffectiveArgumentCount(node, args, signature); - for (var i = 0; i < argCount; i++) { - var arg = getEffectiveArgument(node, args, i); - // If the effective argument is 'undefined', then it is an argument that is present but is synthetic. - if (arg === undefined || arg.kind !== 193 /* OmittedExpression */) { - // Check spread elements against rest type (from arity check we know spread argument corresponds to a rest parameter) - var paramType = getTypeAtPosition(signature, i); - var argType = getEffectiveArgumentType(node, i, arg); - // If the effective argument type is 'undefined', there is no synthetic type - // for the argument. In that case, we should check the argument. - if (argType === undefined) { - argType = checkExpressionWithContextualType(arg, paramType, excludeArgument && excludeArgument[i] ? identityMapper : undefined); - } - // Use argument expression as error location when reporting errors - var errorNode = reportErrors ? getEffectiveArgumentErrorNode(node, i, arg) : undefined; - if (!checkTypeRelatedTo(argType, paramType, relation, errorNode, headMessage)) { - return false; - } + else { + checkExpression(node.closingElement.tagName); + } + // Check children + for (var _i = 0, _a = node.children; _i < _a.length; _i++) { + var child = _a[_i]; + switch (child.kind) { + case 248 /* JsxExpression */: + checkJsxExpression(child); + break; + case 241 /* JsxElement */: + checkJsxElement(child); + break; + case 242 /* JsxSelfClosingElement */: + checkJsxSelfClosingElement(child); + break; } } - return true; + return jsxElementType || anyType; } /** - * Returns the this argument in calls like x.f(...) and x[f](...). Undefined otherwise. + * Returns true iff the JSX element name would be a valid JS identifier, ignoring restrictions about keywords not being identifiers */ - function getThisArgumentOfCall(node) { - if (node.kind === 174 /* CallExpression */) { - var callee = node.expression; - if (callee.kind === 172 /* PropertyAccessExpression */) { - return callee.expression; - } - else if (callee.kind === 173 /* ElementAccessExpression */) { - return callee.expression; - } - } + function isUnhyphenatedJsxName(name) { + // - is the only character supported in JSX attribute names that isn't valid in JavaScript identifiers + return name.indexOf("-") < 0; } /** - * Returns the effective arguments for an expression that works like a function invocation. - * - * If 'node' is a CallExpression or a NewExpression, then its argument list is returned. - * If 'node' is a TaggedTemplateExpression, a new argument list is constructed from the substitution - * expressions, where the first element of the list is `undefined`. - * If 'node' is a Decorator, the argument list will be `undefined`, and its arguments and types - * will be supplied from calls to `getEffectiveArgumentCount` and `getEffectiveArgumentType`. + * Returns true iff React would emit this tag name as a string rather than an identifier or qualified name */ - function getEffectiveCallArguments(node) { - var args; - if (node.kind === 176 /* TaggedTemplateExpression */) { - var template = node.template; - args = [undefined]; - if (template.kind === 189 /* TemplateExpression */) { - ts.forEach(template.templateSpans, function (span) { - args.push(span.expression); - }); - } - } - else if (node.kind === 143 /* Decorator */) { - // For a decorator, we return undefined as we will determine - // the number and types of arguments for a decorator using - // `getEffectiveArgumentCount` and `getEffectiveArgumentType` below. - return undefined; + function isJsxIntrinsicIdentifier(tagName) { + // TODO (yuisu): comment + if (tagName.kind === 172 /* PropertyAccessExpression */ || tagName.kind === 97 /* ThisKeyword */) { + return false; } else { - args = node.arguments || emptyArray; + return ts.isIntrinsicJsxName(tagName.text); } - return args; } - /** - * Returns the effective argument count for a node that works like a function invocation. - * If 'node' is a Decorator, the number of arguments is derived from the decoration - * target and the signature: - * If 'node.target' is a class declaration or class expression, the effective argument - * count is 1. - * If 'node.target' is a parameter declaration, the effective argument count is 3. - * If 'node.target' is a property declaration, the effective argument count is 2. - * If 'node.target' is a method or accessor declaration, the effective argument count - * is 3, although it can be 2 if the signature only accepts two arguments, allowing - * us to match a property decorator. - * Otherwise, the argument count is the length of the 'args' array. - */ - function getEffectiveArgumentCount(node, args, signature) { - if (node.kind === 143 /* Decorator */) { - switch (node.parent.kind) { - case 221 /* ClassDeclaration */: - case 192 /* ClassExpression */: - // A class decorator will have one argument (see `ClassDecorator` in core.d.ts) - return 1; - case 145 /* PropertyDeclaration */: - // A property declaration decorator will have two arguments (see - // `PropertyDecorator` in core.d.ts) - return 2; - case 147 /* MethodDeclaration */: - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - // A method or accessor declaration decorator will have two or three arguments (see - // `PropertyDecorator` and `MethodDecorator` in core.d.ts) - // If we are emitting decorators for ES3, we will only pass two arguments. - if (languageVersion === 0 /* ES3 */) { - return 2; + function checkJsxAttribute(node, elementAttributesType, nameTable) { + var correspondingPropType = undefined; + // Look up the corresponding property for this attribute + if (elementAttributesType === emptyObjectType && isUnhyphenatedJsxName(node.name.text)) { + // If there is no 'props' property, you may not have non-"data-" attributes + error(node.parent, ts.Diagnostics.JSX_element_class_does_not_support_attributes_because_it_does_not_have_a_0_property, getJsxElementPropertiesName()); + } + else if (elementAttributesType && !isTypeAny(elementAttributesType)) { + var correspondingPropSymbol = getPropertyOfType(elementAttributesType, node.name.text); + correspondingPropType = correspondingPropSymbol && getTypeOfSymbol(correspondingPropSymbol); + if (isUnhyphenatedJsxName(node.name.text)) { + var attributeType = getTypeOfPropertyOfType(elementAttributesType, getTextOfPropertyName(node.name)) || getIndexTypeOfType(elementAttributesType, 0 /* String */); + if (attributeType) { + correspondingPropType = attributeType; + } + else { + // If there's no corresponding property with this name, error + if (!correspondingPropType) { + error(node.name, ts.Diagnostics.Property_0_does_not_exist_on_type_1, node.name.text, typeToString(elementAttributesType)); + return unknownType; } - // If the method decorator signature only accepts a target and a key, we will only - // type check those arguments. - return signature.parameters.length >= 3 ? 3 : 2; - case 142 /* Parameter */: - // A parameter declaration decorator will have three arguments (see - // `ParameterDecorator` in core.d.ts) - return 3; + } } } + var exprType; + if (node.initializer) { + exprType = checkExpression(node.initializer); + } else { - return args.length; + // is sugar for + exprType = booleanType; } - } - /** - * Returns the effective type of the first argument to a decorator. - * If 'node' is a class declaration or class expression, the effective argument type - * is the type of the static side of the class. - * If 'node' is a parameter declaration, the effective argument type is either the type - * of the static or instance side of the class for the parameter's parent method, - * depending on whether the method is declared static. - * For a constructor, the type is always the type of the static side of the class. - * If 'node' is a property, method, or accessor declaration, the effective argument - * type is the type of the static or instance side of the parent class for class - * element, depending on whether the element is declared static. - */ - function getEffectiveDecoratorFirstArgumentType(node) { - // The first argument to a decorator is its `target`. - if (node.kind === 221 /* ClassDeclaration */) { - // For a class decorator, the `target` is the type of the class (e.g. the - // "static" or "constructor" side of the class) - var classSymbol = getSymbolOfNode(node); - return getTypeOfSymbol(classSymbol); + if (correspondingPropType) { + checkTypeAssignableTo(exprType, correspondingPropType, node); } - if (node.kind === 142 /* Parameter */) { - // For a parameter decorator, the `target` is the parent type of the - // parameter's containing method. - node = node.parent; - if (node.kind === 148 /* Constructor */) { - var classSymbol = getSymbolOfNode(node); - return getTypeOfSymbol(classSymbol); + nameTable[node.name.text] = true; + return exprType; + } + function checkJsxSpreadAttribute(node, elementAttributesType, nameTable) { + var type = checkExpression(node.expression); + var props = getPropertiesOfType(type); + for (var _i = 0, props_2 = props; _i < props_2.length; _i++) { + var prop = props_2[_i]; + // Is there a corresponding property in the element attributes type? Skip checking of properties + // that have already been assigned to, as these are not actually pushed into the resulting type + if (!nameTable[prop.name]) { + var targetPropSym = getPropertyOfType(elementAttributesType, prop.name); + if (targetPropSym) { + var msg = ts.chainDiagnosticMessages(undefined, ts.Diagnostics.Property_0_of_JSX_spread_attribute_is_not_assignable_to_target_property, prop.name); + checkTypeAssignableTo(getTypeOfSymbol(prop), getTypeOfSymbol(targetPropSym), node, undefined, msg); + } + nameTable[prop.name] = true; } } - if (node.kind === 145 /* PropertyDeclaration */ || - node.kind === 147 /* MethodDeclaration */ || - node.kind === 149 /* GetAccessor */ || - node.kind === 150 /* SetAccessor */) { - // For a property or method decorator, the `target` is the - // "static"-side type of the parent of the member if the member is - // declared "static"; otherwise, it is the "instance"-side type of the - // parent of the member. - return getParentTypeOfClassElement(node); + return type; + } + function getJsxType(name) { + if (jsxTypes[name] === undefined) { + return jsxTypes[name] = getExportedTypeFromNamespace(JsxNames.JSX, name) || unknownType; } - ts.Debug.fail("Unsupported decorator target."); - return unknownType; + return jsxTypes[name]; } /** - * Returns the effective type for the second argument to a decorator. - * If 'node' is a parameter, its effective argument type is one of the following: - * If 'node.parent' is a constructor, the effective argument type is 'any', as we - * will emit `undefined`. - * If 'node.parent' is a member with an identifier, numeric, or string literal name, - * the effective argument type will be a string literal type for the member name. - * If 'node.parent' is a computed property name, the effective argument type will - * either be a symbol type or the string type. - * If 'node' is a member with an identifier, numeric, or string literal name, the - * effective argument type will be a string literal type for the member name. - * If 'node' is a computed property name, the effective argument type will either - * be a symbol type or the string type. - * A class decorator does not have a second argument type. + * Looks up an intrinsic tag name and returns a symbol that either points to an intrinsic + * property (in which case nodeLinks.jsxFlags will be IntrinsicNamedElement) or an intrinsic + * string index signature (in which case nodeLinks.jsxFlags will be IntrinsicIndexedElement). + * May also return unknownSymbol if both of these lookups fail. */ - function getEffectiveDecoratorSecondArgumentType(node) { - // The second argument to a decorator is its `propertyKey` - if (node.kind === 221 /* ClassDeclaration */) { - ts.Debug.fail("Class decorators should not have a second synthetic argument."); - return unknownType; - } - if (node.kind === 142 /* Parameter */) { - node = node.parent; - if (node.kind === 148 /* Constructor */) { - // For a constructor parameter decorator, the `propertyKey` will be `undefined`. - return anyType; + function getIntrinsicTagSymbol(node) { + var links = getNodeLinks(node); + if (!links.resolvedSymbol) { + var intrinsicElementsType = getJsxType(JsxNames.IntrinsicElements); + if (intrinsicElementsType !== unknownType) { + // Property case + var intrinsicProp = getPropertyOfType(intrinsicElementsType, node.tagName.text); + if (intrinsicProp) { + links.jsxFlags |= 1 /* IntrinsicNamedElement */; + return links.resolvedSymbol = intrinsicProp; + } + // Intrinsic string indexer case + var indexSignatureType = getIndexTypeOfType(intrinsicElementsType, 0 /* String */); + if (indexSignatureType) { + links.jsxFlags |= 2 /* IntrinsicIndexedElement */; + return links.resolvedSymbol = intrinsicElementsType.symbol; + } + // Wasn't found + error(node, ts.Diagnostics.Property_0_does_not_exist_on_type_1, node.tagName.text, "JSX." + JsxNames.IntrinsicElements); + return links.resolvedSymbol = unknownSymbol; } - } - if (node.kind === 145 /* PropertyDeclaration */ || - node.kind === 147 /* MethodDeclaration */ || - node.kind === 149 /* GetAccessor */ || - node.kind === 150 /* SetAccessor */) { - // The `propertyKey` for a property or method decorator will be a - // string literal type if the member name is an identifier, number, or string; - // otherwise, if the member name is a computed property name it will - // be either string or symbol. - var element = node; - switch (element.name.kind) { - case 69 /* Identifier */: - case 8 /* NumericLiteral */: - case 9 /* StringLiteral */: - return getLiteralTypeForText(32 /* StringLiteral */, element.name.text); - case 140 /* ComputedPropertyName */: - var nameType = checkComputedPropertyName(element.name); - if (isTypeOfKind(nameType, 512 /* ESSymbol */)) { - return nameType; - } - else { - return stringType; - } - default: - ts.Debug.fail("Unsupported property name."); - return unknownType; + else { + if (compilerOptions.noImplicitAny) { + error(node, ts.Diagnostics.JSX_element_implicitly_has_type_any_because_no_interface_JSX_0_exists, JsxNames.IntrinsicElements); + } + return links.resolvedSymbol = unknownSymbol; } } - ts.Debug.fail("Unsupported decorator target."); - return unknownType; + return links.resolvedSymbol; } /** - * Returns the effective argument type for the third argument to a decorator. - * If 'node' is a parameter, the effective argument type is the number type. - * If 'node' is a method or accessor, the effective argument type is a - * `TypedPropertyDescriptor` instantiated with the type of the member. - * Class and property decorators do not have a third effective argument. - */ - function getEffectiveDecoratorThirdArgumentType(node) { - // The third argument to a decorator is either its `descriptor` for a method decorator - // or its `parameterIndex` for a parameter decorator - if (node.kind === 221 /* ClassDeclaration */) { - ts.Debug.fail("Class decorators should not have a third synthetic argument."); - return unknownType; - } - if (node.kind === 142 /* Parameter */) { - // The `parameterIndex` for a parameter decorator is always a number - return numberType; - } - if (node.kind === 145 /* PropertyDeclaration */) { - ts.Debug.fail("Property decorators should not have a third synthetic argument."); - return unknownType; + * Given a JSX element that is a class element, finds the Element Instance Type. If the + * element is not a class element, or the class element type cannot be determined, returns 'undefined'. + * For example, in the element , the element instance type is `MyClass` (not `typeof MyClass`). + */ + function getJsxElementInstanceType(node, valueType) { + ts.Debug.assert(!(valueType.flags & 524288 /* Union */)); + if (isTypeAny(valueType)) { + // Short-circuit if the class tag is using an element type 'any' + return anyType; } - if (node.kind === 147 /* MethodDeclaration */ || - node.kind === 149 /* GetAccessor */ || - node.kind === 150 /* SetAccessor */) { - // The `descriptor` for a method decorator will be a `TypedPropertyDescriptor` - // for the type of the member. - var propertyType = getTypeOfNode(node); - return createTypedPropertyDescriptorType(propertyType); - } - ts.Debug.fail("Unsupported decorator target."); - return unknownType; - } - /** - * Returns the effective argument type for the provided argument to a decorator. - */ - function getEffectiveDecoratorArgumentType(node, argIndex) { - if (argIndex === 0) { - return getEffectiveDecoratorFirstArgumentType(node.parent); - } - else if (argIndex === 1) { - return getEffectiveDecoratorSecondArgumentType(node.parent); - } - else if (argIndex === 2) { - return getEffectiveDecoratorThirdArgumentType(node.parent); + // Resolve the signatures, preferring constructor + var signatures = getSignaturesOfType(valueType, 1 /* Construct */); + if (signatures.length === 0) { + // No construct signatures, try call signatures + signatures = getSignaturesOfType(valueType, 0 /* Call */); + if (signatures.length === 0) { + // We found no signatures at all, which is an error + error(node.tagName, ts.Diagnostics.JSX_element_type_0_does_not_have_any_construct_or_call_signatures, ts.getTextOfNode(node.tagName)); + return unknownType; + } } - ts.Debug.fail("Decorators should not have a fourth synthetic argument."); - return unknownType; + return getUnionType(signatures.map(getReturnTypeOfSignature), /*subtypeReduction*/ true); } - /** - * Gets the effective argument type for an argument in a call expression. - */ - function getEffectiveArgumentType(node, argIndex, arg) { - // Decorators provide special arguments, a tagged template expression provides - // a special first argument, and string literals get string literal types - // unless we're reporting errors - if (node.kind === 143 /* Decorator */) { - return getEffectiveDecoratorArgumentType(node, argIndex); - } - else if (argIndex === 0 && node.kind === 176 /* TaggedTemplateExpression */) { - return getGlobalTemplateStringsArrayType(); + /// e.g. "props" for React.d.ts, + /// or 'undefined' if ElementAttributesProperty doesn't exist (which means all + /// non-intrinsic elements' attributes type is 'any'), + /// or '' if it has 0 properties (which means every + /// non-intrinsic elements' attributes type is the element instance type) + function getJsxElementPropertiesName() { + // JSX + var jsxNamespace = getGlobalSymbol(JsxNames.JSX, 1920 /* Namespace */, /*diagnosticMessage*/ undefined); + // JSX.ElementAttributesProperty [symbol] + var attribsPropTypeSym = jsxNamespace && getSymbol(jsxNamespace.exports, JsxNames.ElementAttributesPropertyNameContainer, 793064 /* Type */); + // JSX.ElementAttributesProperty [type] + var attribPropType = attribsPropTypeSym && getDeclaredTypeOfSymbol(attribsPropTypeSym); + // The properties of JSX.ElementAttributesProperty + var attribProperties = attribPropType && getPropertiesOfType(attribPropType); + if (attribProperties) { + // Element Attributes has zero properties, so the element attributes type will be the class instance type + if (attribProperties.length === 0) { + return ""; + } + else if (attribProperties.length === 1) { + return attribProperties[0].name; + } + else { + error(attribsPropTypeSym.declarations[0], ts.Diagnostics.The_global_type_JSX_0_may_not_have_more_than_one_property, JsxNames.ElementAttributesPropertyNameContainer); + return undefined; + } } - // This is not a synthetic argument, so we return 'undefined' - // to signal that the caller needs to check the argument. - return undefined; - } - /** - * Gets the effective argument expression for an argument in a call expression. - */ - function getEffectiveArgument(node, args, argIndex) { - // For a decorator or the first argument of a tagged template expression we return undefined. - if (node.kind === 143 /* Decorator */ || - (argIndex === 0 && node.kind === 176 /* TaggedTemplateExpression */)) { + else { + // No interface exists, so the element attributes type will be an implicit any return undefined; } - return args[argIndex]; } /** - * Gets the error node to use when reporting errors for an effective argument. - */ - function getEffectiveArgumentErrorNode(node, argIndex, arg) { - if (node.kind === 143 /* Decorator */) { - // For a decorator, we use the expression of the decorator for error reporting. - return node.expression; + * Given React element instance type and the class type, resolve the Jsx type + * Pass elemType to handle individual type in the union typed element type. + */ + function getResolvedJsxType(node, elemType, elemClassType) { + if (!elemType) { + elemType = checkExpression(node.tagName); } - else if (argIndex === 0 && node.kind === 176 /* TaggedTemplateExpression */) { - // For a the first argument of a tagged template expression, we use the template of the tag for error reporting. - return node.template; + if (elemType.flags & 524288 /* Union */) { + var types = elemType.types; + return getUnionType(types.map(function (type) { + return getResolvedJsxType(node, type, elemClassType); + }), /*subtypeReduction*/ true); } - else { - return arg; + // If the elemType is a string type, we have to return anyType to prevent an error downstream as we will try to find construct or call signature of the type + if (elemType.flags & 2 /* String */) { + return anyType; } - } - function resolveCall(node, signatures, candidatesOutArray, headMessage) { - var isTaggedTemplate = node.kind === 176 /* TaggedTemplateExpression */; - var isDecorator = node.kind === 143 /* Decorator */; - var typeArguments; - if (!isTaggedTemplate && !isDecorator) { - typeArguments = node.typeArguments; - // We already perform checking on the type arguments on the class declaration itself. - if (node.expression.kind !== 95 /* SuperKeyword */) { - ts.forEach(typeArguments, checkSourceElement); + else if (elemType.flags & 32 /* StringLiteral */) { + // If the elemType is a stringLiteral type, we can then provide a check to make sure that the string literal type is one of the Jsx intrinsic element type + var intrinsicElementsType = getJsxType(JsxNames.IntrinsicElements); + if (intrinsicElementsType !== unknownType) { + var stringLiteralTypeName = elemType.text; + var intrinsicProp = getPropertyOfType(intrinsicElementsType, stringLiteralTypeName); + if (intrinsicProp) { + return getTypeOfSymbol(intrinsicProp); + } + var indexSignatureType = getIndexTypeOfType(intrinsicElementsType, 0 /* String */); + if (indexSignatureType) { + return indexSignatureType; + } + error(node, ts.Diagnostics.Property_0_does_not_exist_on_type_1, stringLiteralTypeName, "JSX." + JsxNames.IntrinsicElements); } + // If we need to report an error, we already done so here. So just return any to prevent any more error downstream + return anyType; } - var candidates = candidatesOutArray || []; - // reorderCandidates fills up the candidates array directly - reorderCandidates(signatures, candidates); - if (!candidates.length) { - reportError(ts.Diagnostics.Supplied_parameters_do_not_match_any_signature_of_call_target); - return resolveErrorCall(node); - } - var args = getEffectiveCallArguments(node); - // The following applies to any value of 'excludeArgument[i]': - // - true: the argument at 'i' is susceptible to a one-time permanent contextual typing. - // - undefined: the argument at 'i' is *not* susceptible to permanent contextual typing. - // - false: the argument at 'i' *was* and *has been* permanently contextually typed. - // - // The idea is that we will perform type argument inference & assignability checking once - // without using the susceptible parameters that are functions, and once more for each of those - // parameters, contextually typing each as we go along. - // - // For a tagged template, then the first argument be 'undefined' if necessary - // because it represents a TemplateStringsArray. - // - // For a decorator, no arguments are susceptible to contextual typing due to the fact - // decorators are applied to a declaration by the emitter, and not to an expression. - var excludeArgument; - if (!isDecorator) { - // We do not need to call `getEffectiveArgumentCount` here as it only - // applies when calculating the number of arguments for a decorator. - for (var i = isTaggedTemplate ? 1 : 0; i < args.length; i++) { - if (isContextSensitive(args[i])) { - if (!excludeArgument) { - excludeArgument = new Array(args.length); + // Get the element instance type (the result of newing or invoking this tag) + var elemInstanceType = getJsxElementInstanceType(node, elemType); + if (!elemClassType || !isTypeAssignableTo(elemInstanceType, elemClassType)) { + // Is this is a stateless function component? See if its single signature's return type is + // assignable to the JSX Element Type + if (jsxElementType) { + var callSignatures = elemType && getSignaturesOfType(elemType, 0 /* Call */); + var callSignature = callSignatures && callSignatures.length > 0 && callSignatures[0]; + var callReturnType = callSignature && getReturnTypeOfSignature(callSignature); + var paramType = callReturnType && (callSignature.parameters.length === 0 ? emptyObjectType : getTypeOfSymbol(callSignature.parameters[0])); + if (callReturnType && isTypeAssignableTo(callReturnType, jsxElementType)) { + // Intersect in JSX.IntrinsicAttributes if it exists + var intrinsicAttributes = getJsxType(JsxNames.IntrinsicAttributes); + if (intrinsicAttributes !== unknownType) { + paramType = intersectTypes(intrinsicAttributes, paramType); } - excludeArgument[i] = true; + return paramType; } } } - // The following variables are captured and modified by calls to chooseOverload. - // If overload resolution or type argument inference fails, we want to report the - // best error possible. The best error is one which says that an argument was not - // assignable to a parameter. This implies that everything else about the overload - // was fine. So if there is any overload that is only incorrect because of an - // argument, we will report an error on that one. - // - // function foo(s: string) {} - // function foo(n: number) {} // Report argument error on this overload - // function foo() {} - // foo(true); - // - // If none of the overloads even made it that far, there are two possibilities. - // There was a problem with type arguments for some overload, in which case - // report an error on that. Or none of the overloads even had correct arity, - // in which case give an arity error. - // - // function foo(x: T, y: T) {} // Report type argument inference error - // function foo() {} - // foo(0, true); - // - var candidateForArgumentError; - var candidateForTypeArgumentError; - var resultOfFailedInference; - var result; - // If we are in signature help, a trailing comma indicates that we intend to provide another argument, - // so we will only accept overloads with arity at least 1 higher than the current number of provided arguments. - var signatureHelpTrailingComma = candidatesOutArray && node.kind === 174 /* CallExpression */ && node.arguments.hasTrailingComma; - // Section 4.12.1: - // if the candidate list contains one or more signatures for which the type of each argument - // expression is a subtype of each corresponding parameter type, the return type of the first - // of those signatures becomes the return type of the function call. - // Otherwise, the return type of the first signature in the candidate list becomes the return - // type of the function call. - // - // Whether the call is an error is determined by assignability of the arguments. The subtype pass - // is just important for choosing the best signature. So in the case where there is only one - // signature, the subtype pass is useless. So skipping it is an optimization. - if (candidates.length > 1) { - result = chooseOverload(candidates, subtypeRelation, signatureHelpTrailingComma); - } - if (!result) { - // Reinitialize these pointers for round two - candidateForArgumentError = undefined; - candidateForTypeArgumentError = undefined; - resultOfFailedInference = undefined; - result = chooseOverload(candidates, assignableRelation, signatureHelpTrailingComma); + // Issue an error if this return type isn't assignable to JSX.ElementClass + if (elemClassType) { + checkTypeRelatedTo(elemInstanceType, elemClassType, assignableRelation, node, ts.Diagnostics.JSX_element_type_0_is_not_a_constructor_function_for_JSX_elements); } - if (result) { - return result; + if (isTypeAny(elemInstanceType)) { + return elemInstanceType; } - // No signatures were applicable. Now report errors based on the last applicable signature with - // no arguments excluded from assignability checks. - // If candidate is undefined, it means that no candidates had a suitable arity. In that case, - // skip the checkApplicableSignature check. - if (candidateForArgumentError) { - // excludeArgument is undefined, in this case also equivalent to [undefined, undefined, ...] - // The importance of excludeArgument is to prevent us from typing function expression parameters - // in arguments too early. If possible, we'd like to only type them once we know the correct - // overload. However, this matters for the case where the call is correct. When the call is - // an error, we don't need to exclude any arguments, although it would cause no harm to do so. - checkApplicableSignature(node, args, candidateForArgumentError, assignableRelation, /*excludeArgument*/ undefined, /*reportErrors*/ true); + var propsName = getJsxElementPropertiesName(); + if (propsName === undefined) { + // There is no type ElementAttributesProperty, return 'any' + return anyType; } - else if (candidateForTypeArgumentError) { - if (!isTaggedTemplate && !isDecorator && typeArguments) { - var typeArguments_2 = node.typeArguments; - checkTypeArguments(candidateForTypeArgumentError, typeArguments_2, ts.map(typeArguments_2, getTypeFromTypeNodeNoAlias), /*reportErrors*/ true, headMessage); - } - else { - ts.Debug.assert(resultOfFailedInference.failedTypeParameterIndex >= 0); - var failedTypeParameter = candidateForTypeArgumentError.typeParameters[resultOfFailedInference.failedTypeParameterIndex]; - var inferenceCandidates = getInferenceCandidates(resultOfFailedInference, resultOfFailedInference.failedTypeParameterIndex); - var diagnosticChainHead = ts.chainDiagnosticMessages(/*details*/ undefined, // details will be provided by call to reportNoCommonSupertypeError - ts.Diagnostics.The_type_argument_for_type_parameter_0_cannot_be_inferred_from_the_usage_Consider_specifying_the_type_arguments_explicitly, typeToString(failedTypeParameter)); - if (headMessage) { - diagnosticChainHead = ts.chainDiagnosticMessages(diagnosticChainHead, headMessage); - } - reportNoCommonSupertypeError(inferenceCandidates, node.expression || node.tag, diagnosticChainHead); - } + else if (propsName === "") { + // If there is no e.g. 'props' member in ElementAttributesProperty, use the element class type instead + return elemInstanceType; } else { - reportError(ts.Diagnostics.Supplied_parameters_do_not_match_any_signature_of_call_target); - } - // No signature was applicable. We have already reported the errors for the invalid signature. - // If this is a type resolution session, e.g. Language Service, try to get better information that anySignature. - // Pick the first candidate that matches the arity. This way we can get a contextual type for cases like: - // declare function f(a: { xa: number; xb: number; }); - // f({ | - if (!produceDiagnostics) { - for (var _i = 0, candidates_1 = candidates; _i < candidates_1.length; _i++) { - var candidate = candidates_1[_i]; - if (hasCorrectArity(node, args, candidate)) { - if (candidate.typeParameters && typeArguments) { - candidate = getSignatureInstantiation(candidate, ts.map(typeArguments, getTypeFromTypeNodeNoAlias)); - } - return candidate; - } + var attributesType = getTypeOfPropertyOfType(elemInstanceType, propsName); + if (!attributesType) { + // There is no property named 'props' on this instance type + return emptyObjectType; } - } - return resolveErrorCall(node); - function reportError(message, arg0, arg1, arg2) { - var errorInfo; - errorInfo = ts.chainDiagnosticMessages(errorInfo, message, arg0, arg1, arg2); - if (headMessage) { - errorInfo = ts.chainDiagnosticMessages(errorInfo, headMessage); + else if (isTypeAny(attributesType) || (attributesType === unknownType)) { + // Props is of type 'any' or unknown + return attributesType; } - diagnostics.add(ts.createDiagnosticForNodeFromMessageChain(node, errorInfo)); - } - function chooseOverload(candidates, relation, signatureHelpTrailingComma) { - if (signatureHelpTrailingComma === void 0) { signatureHelpTrailingComma = false; } - for (var _i = 0, candidates_2 = candidates; _i < candidates_2.length; _i++) { - var originalCandidate = candidates_2[_i]; - if (!hasCorrectArity(node, args, originalCandidate, signatureHelpTrailingComma)) { - continue; - } - var candidate = void 0; - var typeArgumentsAreValid = void 0; - var inferenceContext = originalCandidate.typeParameters - ? createInferenceContext(originalCandidate, /*inferUnionTypes*/ false) - : undefined; - while (true) { - candidate = originalCandidate; - if (candidate.typeParameters) { - var typeArgumentTypes = void 0; - if (typeArguments) { - typeArgumentTypes = ts.map(typeArguments, getTypeFromTypeNodeNoAlias); - typeArgumentsAreValid = checkTypeArguments(candidate, typeArguments, typeArgumentTypes, /*reportErrors*/ false); - } - else { - inferTypeArguments(node, candidate, args, excludeArgument, inferenceContext); - typeArgumentsAreValid = inferenceContext.failedTypeParameterIndex === undefined; - typeArgumentTypes = inferenceContext.inferredTypes; - } - if (!typeArgumentsAreValid) { - break; + else if (attributesType.flags & 524288 /* Union */) { + // Props cannot be a union type + error(node.tagName, ts.Diagnostics.JSX_element_attributes_type_0_may_not_be_a_union_type, typeToString(attributesType)); + return anyType; + } + else { + // Normal case -- add in IntrinsicClassElements and IntrinsicElements + var apparentAttributesType = attributesType; + var intrinsicClassAttribs = getJsxType(JsxNames.IntrinsicClassAttributes); + if (intrinsicClassAttribs !== unknownType) { + var typeParams = getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(intrinsicClassAttribs.symbol); + if (typeParams) { + if (typeParams.length === 1) { + apparentAttributesType = intersectTypes(createTypeReference(intrinsicClassAttribs, [elemInstanceType]), apparentAttributesType); } - candidate = getSignatureInstantiation(candidate, typeArgumentTypes); - } - if (!checkApplicableSignature(node, args, candidate, relation, excludeArgument, /*reportErrors*/ false)) { - break; - } - var index = excludeArgument ? ts.indexOf(excludeArgument, true) : -1; - if (index < 0) { - return candidate; - } - excludeArgument[index] = false; - } - // A post-mortem of this iteration of the loop. The signature was not applicable, - // so we want to track it as a candidate for reporting an error. If the candidate - // had no type parameters, or had no issues related to type arguments, we can - // report an error based on the arguments. If there was an issue with type - // arguments, then we can only report an error based on the type arguments. - if (originalCandidate.typeParameters) { - var instantiatedCandidate = candidate; - if (typeArgumentsAreValid) { - candidateForArgumentError = instantiatedCandidate; } else { - candidateForTypeArgumentError = originalCandidate; - if (!typeArguments) { - resultOfFailedInference = inferenceContext; - } + apparentAttributesType = intersectTypes(attributesType, intrinsicClassAttribs); } } - else { - ts.Debug.assert(originalCandidate === candidate); - candidateForArgumentError = originalCandidate; + var intrinsicAttribs = getJsxType(JsxNames.IntrinsicAttributes); + if (intrinsicAttribs !== unknownType) { + apparentAttributesType = intersectTypes(intrinsicAttribs, apparentAttributesType); } + return apparentAttributesType; } - return undefined; } } - function resolveCallExpression(node, candidatesOutArray) { - if (node.expression.kind === 95 /* SuperKeyword */) { - var superType = checkSuperExpression(node.expression); - if (superType !== unknownType) { - // In super call, the candidate signatures are the matching arity signatures of the base constructor function instantiated - // with the type arguments specified in the extends clause. - var baseTypeNode = ts.getClassExtendsHeritageClauseElement(ts.getContainingClass(node)); - if (baseTypeNode) { - var baseConstructors = getInstantiatedConstructorsForTypeArguments(superType, baseTypeNode.typeArguments); - return resolveCall(node, baseConstructors, candidatesOutArray); + /** + * Given an opening/self-closing element, get the 'element attributes type', i.e. the type that tells + * us which attributes are valid on a given element. + */ + function getJsxElementAttributesType(node) { + var links = getNodeLinks(node); + if (!links.resolvedJsxType) { + if (isJsxIntrinsicIdentifier(node.tagName)) { + var symbol = getIntrinsicTagSymbol(node); + if (links.jsxFlags & 1 /* IntrinsicNamedElement */) { + return links.resolvedJsxType = getTypeOfSymbol(symbol); + } + else if (links.jsxFlags & 2 /* IntrinsicIndexedElement */) { + return links.resolvedJsxType = getIndexInfoOfSymbol(symbol, 0 /* String */).type; + } + else { + return links.resolvedJsxType = unknownType; } - } - return resolveUntypedCall(node); - } - var funcType = checkNonNullExpression(node.expression); - if (funcType === silentNeverType) { - return silentNeverSignature; - } - var apparentType = getApparentType(funcType); - if (apparentType === unknownType) { - // Another error has already been reported - return resolveErrorCall(node); - } - // Technically, this signatures list may be incomplete. We are taking the apparent type, - // but we are not including call signatures that may have been added to the Object or - // Function interface, since they have none by default. This is a bit of a leap of faith - // that the user will not add any. - var callSignatures = getSignaturesOfType(apparentType, 0 /* Call */); - var constructSignatures = getSignaturesOfType(apparentType, 1 /* Construct */); - // TS 1.0 Spec: 4.12 - // In an untyped function call no TypeArgs are permitted, Args can be any argument list, no contextual - // types are provided for the argument expressions, and the result is always of type Any. - if (isUntypedFunctionCall(funcType, apparentType, callSignatures.length, constructSignatures.length)) { - // The unknownType indicates that an error already occurred (and was reported). No - // need to report another error in this case. - if (funcType !== unknownType && node.typeArguments) { - error(node, ts.Diagnostics.Untyped_function_calls_may_not_accept_type_arguments); - } - return resolveUntypedCall(node); - } - // If FuncExpr's apparent type(section 3.8.1) is a function type, the call is a typed function call. - // TypeScript employs overload resolution in typed function calls in order to support functions - // with multiple call signatures. - if (!callSignatures.length) { - if (constructSignatures.length) { - error(node, ts.Diagnostics.Value_of_type_0_is_not_callable_Did_you_mean_to_include_new, typeToString(funcType)); } else { - error(node, ts.Diagnostics.Cannot_invoke_an_expression_whose_type_lacks_a_call_signature_Type_0_has_no_compatible_call_signatures, typeToString(apparentType)); + var elemClassType = getJsxGlobalElementClassType(); + return links.resolvedJsxType = getResolvedJsxType(node, undefined, elemClassType); } - return resolveErrorCall(node); } - return resolveCall(node, callSignatures, candidatesOutArray); + return links.resolvedJsxType; } /** - * TS 1.0 spec: 4.12 - * If FuncExpr is of type Any, or of an object type that has no call or construct signatures - * but is a subtype of the Function interface, the call is an untyped function call. + * Given a JSX attribute, returns the symbol for the corresponds property + * of the element attributes type. Will return unknownSymbol for attributes + * that have no matching element attributes type property. */ - function isUntypedFunctionCall(funcType, apparentFuncType, numCallSignatures, numConstructSignatures) { - if (isTypeAny(funcType)) { - return true; + function getJsxAttributePropertySymbol(attrib) { + var attributesType = getJsxElementAttributesType(attrib.parent); + var prop = getPropertyOfType(attributesType, attrib.name.text); + return prop || unknownSymbol; + } + function getJsxGlobalElementClassType() { + if (!jsxElementClassType) { + jsxElementClassType = getExportedTypeFromNamespace(JsxNames.JSX, JsxNames.ElementClass); } - if (isTypeAny(apparentFuncType) && funcType.flags & 16384 /* TypeParameter */) { - return true; + return jsxElementClassType; + } + /// Returns all the properties of the Jsx.IntrinsicElements interface + function getJsxIntrinsicTagNames() { + var intrinsics = getJsxType(JsxNames.IntrinsicElements); + return intrinsics ? getPropertiesOfType(intrinsics) : emptyArray; + } + function checkJsxPreconditions(errorNode) { + // Preconditions for using JSX + if ((compilerOptions.jsx || 0 /* None */) === 0 /* None */) { + error(errorNode, ts.Diagnostics.Cannot_use_JSX_unless_the_jsx_flag_is_provided); } - if (!numCallSignatures && !numConstructSignatures) { - // We exclude union types because we may have a union of function types that happen to have - // no common signatures. - if (funcType.flags & 524288 /* Union */) { - return false; + if (jsxElementType === undefined) { + if (compilerOptions.noImplicitAny) { + error(errorNode, ts.Diagnostics.JSX_element_implicitly_has_type_any_because_the_global_type_JSX_Element_does_not_exist); } - return isTypeAssignableTo(funcType, globalFunctionType); } - return false; } - function resolveNewExpression(node, candidatesOutArray) { - if (node.arguments && languageVersion < 1 /* ES5 */) { - var spreadIndex = getSpreadArgumentIndex(node.arguments); - if (spreadIndex >= 0) { - error(node.arguments[spreadIndex], ts.Diagnostics.Spread_operator_in_new_expressions_is_only_available_when_targeting_ECMAScript_5_and_higher); - } - } - var expressionType = checkNonNullExpression(node.expression); - if (expressionType === silentNeverType) { - return silentNeverSignature; + function checkJsxOpeningLikeElement(node) { + checkGrammarJsxElement(node); + checkJsxPreconditions(node); + // The reactNamespace symbol should be marked as 'used' so we don't incorrectly elide its import. And if there + // is no reactNamespace symbol in scope when targeting React emit, we should issue an error. + var reactRefErr = compilerOptions.jsx === 2 /* React */ ? ts.Diagnostics.Cannot_find_name_0 : undefined; + var reactNamespace = compilerOptions.reactNamespace ? compilerOptions.reactNamespace : "React"; + var reactSym = resolveName(node.tagName, reactNamespace, 107455 /* Value */, reactRefErr, reactNamespace); + if (reactSym) { + getSymbolLinks(reactSym).referenced = true; } - // If expressionType's apparent type(section 3.8.1) is an object type with one or - // more construct signatures, the expression is processed in the same manner as a - // function call, but using the construct signatures as the initial set of candidate - // signatures for overload resolution. The result type of the function call becomes - // the result type of the operation. - expressionType = getApparentType(expressionType); - if (expressionType === unknownType) { - // Another error has already been reported - return resolveErrorCall(node); - } - // If the expression is a class of abstract type, then it cannot be instantiated. - // Note, only class declarations can be declared abstract. - // In the case of a merged class-module or class-interface declaration, - // only the class declaration node will have the Abstract flag set. - var valueDecl = expressionType.symbol && getClassLikeDeclarationOfSymbol(expressionType.symbol); - if (valueDecl && ts.getModifierFlags(valueDecl) & 128 /* Abstract */) { - error(node, ts.Diagnostics.Cannot_create_an_instance_of_the_abstract_class_0, ts.declarationNameToString(valueDecl.name)); - return resolveErrorCall(node); - } - // TS 1.0 spec: 4.11 - // If expressionType is of type Any, Args can be any argument - // list and the result of the operation is of type Any. - if (isTypeAny(expressionType)) { - if (node.typeArguments) { - error(node, ts.Diagnostics.Untyped_function_calls_may_not_accept_type_arguments); + var targetAttributesType = getJsxElementAttributesType(node); + var nameTable = ts.createMap(); + // Process this array in right-to-left order so we know which + // attributes (mostly from spreads) are being overwritten and + // thus should have their types ignored + var sawSpreadedAny = false; + for (var i = node.attributes.length - 1; i >= 0; i--) { + if (node.attributes[i].kind === 246 /* JsxAttribute */) { + checkJsxAttribute((node.attributes[i]), targetAttributesType, nameTable); } - return resolveUntypedCall(node); - } - // Technically, this signatures list may be incomplete. We are taking the apparent type, - // but we are not including construct signatures that may have been added to the Object or - // Function interface, since they have none by default. This is a bit of a leap of faith - // that the user will not add any. - var constructSignatures = getSignaturesOfType(expressionType, 1 /* Construct */); - if (constructSignatures.length) { - if (!isConstructorAccessible(node, constructSignatures[0])) { - return resolveErrorCall(node); + else { + ts.Debug.assert(node.attributes[i].kind === 247 /* JsxSpreadAttribute */); + var spreadType = checkJsxSpreadAttribute((node.attributes[i]), targetAttributesType, nameTable); + if (isTypeAny(spreadType)) { + sawSpreadedAny = true; + } } - return resolveCall(node, constructSignatures, candidatesOutArray); } - // If expressionType's apparent type is an object type with no construct signatures but - // one or more call signatures, the expression is processed as a function call. A compile-time - // error occurs if the result of the function call is not Void. The type of the result of the - // operation is Any. It is an error to have a Void this type. - var callSignatures = getSignaturesOfType(expressionType, 0 /* Call */); - if (callSignatures.length) { - var signature = resolveCall(node, callSignatures, candidatesOutArray); - if (getReturnTypeOfSignature(signature) !== voidType) { - error(node, ts.Diagnostics.Only_a_void_function_can_be_called_with_the_new_keyword); - } - if (getThisTypeOfSignature(signature) === voidType) { - error(node, ts.Diagnostics.A_function_that_is_called_with_the_new_keyword_cannot_have_a_this_type_that_is_void); + // Check that all required properties have been provided. If an 'any' + // was spreaded in, though, assume that it provided all required properties + if (targetAttributesType && !sawSpreadedAny) { + var targetProperties = getPropertiesOfType(targetAttributesType); + for (var i = 0; i < targetProperties.length; i++) { + if (!(targetProperties[i].flags & 536870912 /* Optional */) && + !nameTable[targetProperties[i].name]) { + error(node, ts.Diagnostics.Property_0_is_missing_in_type_1, targetProperties[i].name, typeToString(targetAttributesType)); + } } - return signature; } - error(node, ts.Diagnostics.Cannot_use_new_with_an_expression_whose_type_lacks_a_call_or_construct_signature); - return resolveErrorCall(node); } - function isConstructorAccessible(node, signature) { - if (!signature || !signature.declaration) { - return true; + function checkJsxExpression(node) { + if (node.expression) { + return checkExpression(node.expression); } - var declaration = signature.declaration; - var modifiers = ts.getModifierFlags(declaration); - // Public constructor is accessible. - if (!(modifiers & 24 /* NonPublicAccessibilityModifier */)) { - return true; + else { + return unknownType; } - var declaringClassDeclaration = getClassLikeDeclarationOfSymbol(declaration.parent.symbol); - var declaringClass = getDeclaredTypeOfSymbol(declaration.parent.symbol); - // A private or protected constructor can only be instantiated within its own class (or a subclass, for protected) - if (!isNodeWithinClass(node, declaringClassDeclaration)) { - var containingClass = ts.getContainingClass(node); - if (containingClass) { - var containingType = getTypeOfNode(containingClass); - var baseTypes = getBaseTypes(containingType); - if (baseTypes.length) { - var baseType = baseTypes[0]; - if (modifiers & 16 /* Protected */ && - baseType.symbol === declaration.parent.symbol) { - return true; - } - } - } - if (modifiers & 8 /* Private */) { - error(node, ts.Diagnostics.Constructor_of_class_0_is_private_and_only_accessible_within_the_class_declaration, typeToString(declaringClass)); + } + // If a symbol is a synthesized symbol with no value declaration, we assume it is a property. Example of this are the synthesized + // '.prototype' property as well as synthesized tuple index properties. + function getDeclarationKindFromSymbol(s) { + return s.valueDeclaration ? s.valueDeclaration.kind : 145 /* PropertyDeclaration */; + } + function getDeclarationModifierFlagsFromSymbol(s) { + return s.valueDeclaration ? ts.getCombinedModifierFlags(s.valueDeclaration) : s.flags & 134217728 /* Prototype */ ? 4 /* Public */ | 32 /* Static */ : 0; + } + function getDeclarationNodeFlagsFromSymbol(s) { + return s.valueDeclaration ? ts.getCombinedNodeFlags(s.valueDeclaration) : 0; + } + /** + * Check whether the requested property access is valid. + * Returns true if node is a valid property access, and false otherwise. + * @param node The node to be checked. + * @param left The left hand side of the property access (e.g.: the super in `super.foo`). + * @param type The type of left. + * @param prop The symbol for the right hand side of the property access. + */ + function checkClassPropertyAccess(node, left, type, prop) { + var flags = getDeclarationModifierFlagsFromSymbol(prop); + var declaringClass = getDeclaredTypeOfSymbol(getParentOfSymbol(prop)); + var errorNode = node.kind === 172 /* PropertyAccessExpression */ || node.kind === 218 /* VariableDeclaration */ ? + node.name : + node.right; + if (left.kind === 95 /* SuperKeyword */) { + // TS 1.0 spec (April 2014): 4.8.2 + // - In a constructor, instance member function, instance member accessor, or + // instance member variable initializer where this references a derived class instance, + // a super property access is permitted and must specify a public instance member function of the base class. + // - In a static member function or static member accessor + // where this references the constructor function object of a derived class, + // a super property access is permitted and must specify a public static member function of the base class. + if (languageVersion < 2 /* ES6 */ && getDeclarationKindFromSymbol(prop) !== 147 /* MethodDeclaration */) { + // `prop` refers to a *property* declared in the super class + // rather than a *method*, so it does not satisfy the above criteria. + error(errorNode, ts.Diagnostics.Only_public_and_protected_methods_of_the_base_class_are_accessible_via_the_super_keyword); + return false; } - if (modifiers & 16 /* Protected */) { - error(node, ts.Diagnostics.Constructor_of_class_0_is_protected_and_only_accessible_within_the_class_declaration, typeToString(declaringClass)); + if (flags & 128 /* Abstract */) { + // A method cannot be accessed in a super property access if the method is abstract. + // This error could mask a private property access error. But, a member + // cannot simultaneously be private and abstract, so this will trigger an + // additional error elsewhere. + error(errorNode, ts.Diagnostics.Abstract_method_0_in_class_1_cannot_be_accessed_via_super_expression, symbolToString(prop), typeToString(declaringClass)); + return false; } - return false; } - return true; - } - function resolveTaggedTemplateExpression(node, candidatesOutArray) { - var tagType = checkExpression(node.tag); - var apparentType = getApparentType(tagType); - if (apparentType === unknownType) { - // Another error has already been reported - return resolveErrorCall(node); + // Public properties are otherwise accessible. + if (!(flags & 24 /* NonPublicAccessibilityModifier */)) { + return true; } - var callSignatures = getSignaturesOfType(apparentType, 0 /* Call */); - var constructSignatures = getSignaturesOfType(apparentType, 1 /* Construct */); - if (isUntypedFunctionCall(tagType, apparentType, callSignatures.length, constructSignatures.length)) { - return resolveUntypedCall(node); + // Property is known to be private or protected at this point + // Private property is accessible if the property is within the declaring class + if (flags & 8 /* Private */) { + var declaringClassDeclaration = getClassLikeDeclarationOfSymbol(getParentOfSymbol(prop)); + if (!isNodeWithinClass(node, declaringClassDeclaration)) { + error(errorNode, ts.Diagnostics.Property_0_is_private_and_only_accessible_within_class_1, symbolToString(prop), typeToString(declaringClass)); + return false; + } + return true; } - if (!callSignatures.length) { - error(node, ts.Diagnostics.Cannot_invoke_an_expression_whose_type_lacks_a_call_signature_Type_0_has_no_compatible_call_signatures, typeToString(apparentType)); - return resolveErrorCall(node); + // Property is known to be protected at this point + // All protected properties of a supertype are accessible in a super access + if (left.kind === 95 /* SuperKeyword */) { + return true; } - return resolveCall(node, callSignatures, candidatesOutArray); - } - /** - * Gets the localized diagnostic head message to use for errors when resolving a decorator as a call expression. - */ - function getDiagnosticHeadMessageForDecoratorResolution(node) { - switch (node.parent.kind) { - case 221 /* ClassDeclaration */: - case 192 /* ClassExpression */: - return ts.Diagnostics.Unable_to_resolve_signature_of_class_decorator_when_called_as_an_expression; - case 142 /* Parameter */: - return ts.Diagnostics.Unable_to_resolve_signature_of_parameter_decorator_when_called_as_an_expression; - case 145 /* PropertyDeclaration */: - return ts.Diagnostics.Unable_to_resolve_signature_of_property_decorator_when_called_as_an_expression; - case 147 /* MethodDeclaration */: - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - return ts.Diagnostics.Unable_to_resolve_signature_of_method_decorator_when_called_as_an_expression; + // Get the enclosing class that has the declaring class as its base type + var enclosingClass = forEachEnclosingClass(node, function (enclosingDeclaration) { + var enclosingClass = getDeclaredTypeOfSymbol(getSymbolOfNode(enclosingDeclaration)); + return hasBaseType(enclosingClass, declaringClass) ? enclosingClass : undefined; + }); + // A protected property is accessible if the property is within the declaring class or classes derived from it + if (!enclosingClass) { + error(errorNode, ts.Diagnostics.Property_0_is_protected_and_only_accessible_within_class_1_and_its_subclasses, symbolToString(prop), typeToString(declaringClass)); + return false; } - } - /** - * Resolves a decorator as if it were a call expression. - */ - function resolveDecorator(node, candidatesOutArray) { - var funcType = checkExpression(node.expression); - var apparentType = getApparentType(funcType); - if (apparentType === unknownType) { - return resolveErrorCall(node); + // No further restrictions for static properties + if (flags & 32 /* Static */) { + return true; } - var callSignatures = getSignaturesOfType(apparentType, 0 /* Call */); - var constructSignatures = getSignaturesOfType(apparentType, 1 /* Construct */); - if (isUntypedFunctionCall(funcType, apparentType, callSignatures.length, constructSignatures.length)) { - return resolveUntypedCall(node); + // An instance property must be accessed through an instance of the enclosing class + if (type.flags & 16384 /* TypeParameter */ && type.isThisType) { + // get the original type -- represented as the type constraint of the 'this' type + type = getConstraintOfTypeParameter(type); } - var headMessage = getDiagnosticHeadMessageForDecoratorResolution(node); - if (!callSignatures.length) { - var errorInfo = void 0; - errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Cannot_invoke_an_expression_whose_type_lacks_a_call_signature_Type_0_has_no_compatible_call_signatures, typeToString(apparentType)); - errorInfo = ts.chainDiagnosticMessages(errorInfo, headMessage); - diagnostics.add(ts.createDiagnosticForNodeFromMessageChain(node, errorInfo)); - return resolveErrorCall(node); + // TODO: why is the first part of this check here? + if (!(getTargetType(type).flags & (32768 /* Class */ | 65536 /* Interface */) && hasBaseType(type, enclosingClass))) { + error(errorNode, ts.Diagnostics.Property_0_is_protected_and_only_accessible_through_an_instance_of_class_1, symbolToString(prop), typeToString(enclosingClass)); + return false; } - return resolveCall(node, callSignatures, candidatesOutArray, headMessage); + return true; } - function resolveSignature(node, candidatesOutArray) { - switch (node.kind) { - case 174 /* CallExpression */: - return resolveCallExpression(node, candidatesOutArray); - case 175 /* NewExpression */: - return resolveNewExpression(node, candidatesOutArray); - case 176 /* TaggedTemplateExpression */: - return resolveTaggedTemplateExpression(node, candidatesOutArray); - case 143 /* Decorator */: - return resolveDecorator(node, candidatesOutArray); + function checkNonNullExpression(node) { + var type = checkExpression(node); + if (strictNullChecks) { + var kind = getFalsyFlags(type) & 6144 /* Nullable */; + if (kind) { + error(node, kind & 2048 /* Undefined */ ? kind & 4096 /* Null */ ? + ts.Diagnostics.Object_is_possibly_null_or_undefined : + ts.Diagnostics.Object_is_possibly_undefined : + ts.Diagnostics.Object_is_possibly_null); + } + return getNonNullableType(type); } - ts.Debug.fail("Branch in 'resolveSignature' should be unreachable."); + return type; } - // candidatesOutArray is passed by signature help in the language service, and collectCandidates - // must fill it up with the appropriate candidate signatures - function getResolvedSignature(node, candidatesOutArray) { - var links = getNodeLinks(node); - // If getResolvedSignature has already been called, we will have cached the resolvedSignature. - // However, it is possible that either candidatesOutArray was not passed in the first time, - // or that a different candidatesOutArray was passed in. Therefore, we need to redo the work - // to correctly fill the candidatesOutArray. - var cached = links.resolvedSignature; - if (cached && cached !== resolvingSignature && !candidatesOutArray) { - return cached; - } - links.resolvedSignature = resolvingSignature; - var result = resolveSignature(node, candidatesOutArray); - // If signature resolution originated in control flow type analysis (for example to compute the - // assigned type in a flow assignment) we don't cache the result as it may be based on temporary - // types from the control flow analysis. - links.resolvedSignature = flowLoopStart === flowLoopCount ? result : cached; - return result; + function checkPropertyAccessExpression(node) { + return checkPropertyAccessExpressionOrQualifiedName(node, node.expression, node.name); } - function getResolvedOrAnySignature(node) { - // If we're already in the process of resolving the given signature, don't resolve again as - // that could cause infinite recursion. Instead, return anySignature. - return getNodeLinks(node).resolvedSignature === resolvingSignature ? resolvingSignature : getResolvedSignature(node); + function checkQualifiedName(node) { + return checkPropertyAccessExpressionOrQualifiedName(node, node.left, node.right); } - function getInferredClassType(symbol) { - var links = getSymbolLinks(symbol); - if (!links.inferredClassType) { - links.inferredClassType = createAnonymousType(symbol, symbol.members, emptyArray, emptyArray, /*stringIndexType*/ undefined, /*numberIndexType*/ undefined); + function checkPropertyAccessExpressionOrQualifiedName(node, left, right) { + var type = checkNonNullExpression(left); + if (isTypeAny(type) || type === silentNeverType) { + return type; } - return links.inferredClassType; - } - /** - * Syntactically and semantically checks a call or new expression. - * @param node The call/new expression to be checked. - * @returns On success, the expression's signature's return type. On failure, anyType. - */ - function checkCallExpression(node) { - // Grammar checking; stop grammar-checking if checkGrammarTypeArguments return true - checkGrammarTypeArguments(node, node.typeArguments) || checkGrammarArguments(node, node.arguments); - var signature = getResolvedSignature(node); - if (node.expression.kind === 95 /* SuperKeyword */) { - return voidType; + var apparentType = getApparentType(getWidenedType(type)); + if (apparentType === unknownType || (type.flags & 16384 /* TypeParameter */ && isTypeAny(apparentType))) { + // handle cases when type is Type parameter with invalid or any constraint + return apparentType; } - if (node.kind === 175 /* NewExpression */) { - var declaration = signature.declaration; - if (declaration && - declaration.kind !== 148 /* Constructor */ && - declaration.kind !== 152 /* ConstructSignature */ && - declaration.kind !== 157 /* ConstructorType */ && - !ts.isJSDocConstructSignature(declaration)) { - // When resolved signature is a call signature (and not a construct signature) the result type is any, unless - // the declaring function had members created through 'x.prototype.y = expr' or 'this.y = expr' psuedodeclarations - // in a JS file - // Note:JS inferred classes might come from a variable declaration instead of a function declaration. - // In this case, using getResolvedSymbol directly is required to avoid losing the members from the declaration. - var funcSymbol = node.expression.kind === 69 /* Identifier */ ? - getResolvedSymbol(node.expression) : - checkExpression(node.expression).symbol; - if (funcSymbol && funcSymbol.members && (funcSymbol.flags & 16 /* Function */ || ts.isDeclarationOfFunctionExpression(funcSymbol))) { - return getInferredClassType(funcSymbol); - } - else if (compilerOptions.noImplicitAny) { - error(node, ts.Diagnostics.new_expression_whose_target_lacks_a_construct_signature_implicitly_has_an_any_type); - } - return anyType; + var prop = getPropertyOfType(apparentType, right.text); + if (!prop) { + if (right.text && !checkAndReportErrorForExtendingInterface(node)) { + reportNonexistentProperty(right, type.flags & 16384 /* TypeParameter */ && type.isThisType ? apparentType : type); } + return unknownType; } - // In JavaScript files, calls to any identifier 'require' are treated as external module imports - if (ts.isInJavaScriptFile(node) && ts.isRequireCall(node, /*checkArgumentIsStringLiteral*/ true)) { - return resolveExternalModuleTypeByLiteral(node.arguments[0]); + if (noUnusedIdentifiers && + (prop.flags & 106500 /* ClassMember */) && + prop.valueDeclaration && (ts.getModifierFlags(prop.valueDeclaration) & 8 /* Private */)) { + if (prop.flags & 16777216 /* Instantiated */) { + getSymbolLinks(prop).target.isReferenced = true; + } + else { + prop.isReferenced = true; + } } - return getReturnTypeOfSignature(signature); - } - function checkTaggedTemplateExpression(node) { - return getReturnTypeOfSignature(getResolvedSignature(node)); - } - function checkAssertion(node) { - var exprType = getRegularTypeOfObjectLiteral(getBaseTypeOfLiteralType(checkExpression(node.expression))); - checkSourceElement(node.type); - var targetType = getTypeFromTypeNode(node.type); - if (produceDiagnostics && targetType !== unknownType) { - var widenedType = getWidenedType(exprType); - if (!isTypeComparableTo(targetType, widenedType)) { - checkTypeComparableTo(exprType, targetType, node, ts.Diagnostics.Type_0_cannot_be_converted_to_type_1); + getNodeLinks(node).resolvedSymbol = prop; + if (prop.parent && prop.parent.flags & 32 /* Class */) { + checkClassPropertyAccess(node, left, apparentType, prop); + } + var propType = getTypeOfSymbol(prop); + // Only compute control flow type if this is a property access expression that isn't an + // assignment target, and the referenced property was declared as a variable, property, + // accessor, or optional method. + if (node.kind !== 172 /* PropertyAccessExpression */ || ts.isAssignmentTarget(node) || + !(prop.flags & (3 /* Variable */ | 4 /* Property */ | 98304 /* Accessor */)) && + !(prop.flags & 8192 /* Method */ && propType.flags & 524288 /* Union */)) { + return propType; + } + return getFlowTypeOfReference(node, propType, /*assumeInitialized*/ true, /*flowContainer*/ undefined); + function reportNonexistentProperty(propNode, containingType) { + var errorInfo; + if (containingType.flags & 524288 /* Union */ && !(containingType.flags & 8190 /* Primitive */)) { + for (var _i = 0, _a = containingType.types; _i < _a.length; _i++) { + var subtype = _a[_i]; + if (!getPropertyOfType(subtype, propNode.text)) { + errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Property_0_does_not_exist_on_type_1, ts.declarationNameToString(propNode), typeToString(subtype)); + break; + } + } } + errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Property_0_does_not_exist_on_type_1, ts.declarationNameToString(propNode), typeToString(containingType)); + diagnostics.add(ts.createDiagnosticForNodeFromMessageChain(propNode, errorInfo)); } - return targetType; - } - function checkNonNullAssertion(node) { - return getNonNullableType(checkExpression(node.expression)); } - function getTypeOfParameter(symbol) { - var type = getTypeOfSymbol(symbol); - if (strictNullChecks) { - var declaration = symbol.valueDeclaration; - if (declaration && declaration.initializer) { - return includeFalsyTypes(type, 2048 /* Undefined */); + function isValidPropertyAccess(node, propertyName) { + var left = node.kind === 172 /* PropertyAccessExpression */ + ? node.expression + : node.left; + var type = checkExpression(left); + if (type !== unknownType && !isTypeAny(type)) { + var prop = getPropertyOfType(getWidenedType(type), propertyName); + if (prop && prop.parent && prop.parent.flags & 32 /* Class */) { + return checkClassPropertyAccess(node, left, type, prop); } } - return type; - } - function getTypeAtPosition(signature, pos) { - return signature.hasRestParameter ? - pos < signature.parameters.length - 1 ? getTypeOfParameter(signature.parameters[pos]) : getRestTypeOfSignature(signature) : - pos < signature.parameters.length ? getTypeOfParameter(signature.parameters[pos]) : anyType; + return true; } - function assignContextualParameterTypes(signature, context, mapper) { - var len = signature.parameters.length - (signature.hasRestParameter ? 1 : 0); - if (context.thisParameter) { - if (!signature.thisParameter) { - signature.thisParameter = createTransientSymbol(context.thisParameter, undefined); + /** + * Return the symbol of the for-in variable declared or referenced by the given for-in statement. + */ + function getForInVariableSymbol(node) { + var initializer = node.initializer; + if (initializer.kind === 219 /* VariableDeclarationList */) { + var variable = initializer.declarations[0]; + if (variable && !ts.isBindingPattern(variable.name)) { + return getSymbolOfNode(variable); } - assignTypeToParameterAndFixTypeParameters(signature.thisParameter, getTypeOfSymbol(context.thisParameter), mapper); - } - for (var i = 0; i < len; i++) { - var parameter = signature.parameters[i]; - var contextualParameterType = getTypeAtPosition(context, i); - assignTypeToParameterAndFixTypeParameters(parameter, contextualParameterType, mapper); } - if (signature.hasRestParameter && isRestParameterIndex(context, signature.parameters.length - 1)) { - var parameter = ts.lastOrUndefined(signature.parameters); - var contextualParameterType = getTypeOfSymbol(ts.lastOrUndefined(context.parameters)); - assignTypeToParameterAndFixTypeParameters(parameter, contextualParameterType, mapper); + else if (initializer.kind === 69 /* Identifier */) { + return getResolvedSymbol(initializer); } + return undefined; } - // When contextual typing assigns a type to a parameter that contains a binding pattern, we also need to push - // the destructured type into the contained binding elements. - function assignBindingElementTypes(node) { - if (ts.isBindingPattern(node.name)) { - for (var _i = 0, _a = node.name.elements; _i < _a.length; _i++) { - var element = _a[_i]; - if (!ts.isOmittedExpression(element)) { - if (element.name.kind === 69 /* Identifier */) { - getSymbolLinks(getSymbolOfNode(element)).type = getTypeForBindingElement(element); + /** + * Return true if the given type is considered to have numeric property names. + */ + function hasNumericPropertyNames(type) { + return getIndexTypeOfType(type, 1 /* Number */) && !getIndexTypeOfType(type, 0 /* String */); + } + /** + * Return true if given node is an expression consisting of an identifier (possibly parenthesized) + * that references a for-in variable for an object with numeric property names. + */ + function isForInVariableForNumericPropertyNames(expr) { + var e = skipParenthesizedNodes(expr); + if (e.kind === 69 /* Identifier */) { + var symbol = getResolvedSymbol(e); + if (symbol.flags & 3 /* Variable */) { + var child = expr; + var node = expr.parent; + while (node) { + if (node.kind === 207 /* ForInStatement */ && + child === node.statement && + getForInVariableSymbol(node) === symbol && + hasNumericPropertyNames(checkExpression(node.expression))) { + return true; } - assignBindingElementTypes(element); + child = node; + node = node.parent; } } } + return false; } - function assignTypeToParameterAndFixTypeParameters(parameter, contextualType, mapper) { - var links = getSymbolLinks(parameter); - if (!links.type) { - links.type = instantiateType(contextualType, mapper); - // if inference didn't come up with anything but {}, fall back to the binding pattern if present. - if (links.type === emptyObjectType && - (parameter.valueDeclaration.name.kind === 167 /* ObjectBindingPattern */ || - parameter.valueDeclaration.name.kind === 168 /* ArrayBindingPattern */)) { - links.type = getTypeFromBindingPattern(parameter.valueDeclaration.name); + function checkIndexedAccess(node) { + // Grammar checking + if (!node.argumentExpression) { + var sourceFile = ts.getSourceFileOfNode(node); + if (node.parent.kind === 175 /* NewExpression */ && node.parent.expression === node) { + var start = ts.skipTrivia(sourceFile.text, node.expression.end); + var end = node.end; + grammarErrorAtPos(sourceFile, start, end - start, ts.Diagnostics.new_T_cannot_be_used_to_create_an_array_Use_new_Array_T_instead); + } + else { + var start = node.end - "]".length; + var end = node.end; + grammarErrorAtPos(sourceFile, start, end - start, ts.Diagnostics.Expression_expected); } - assignBindingElementTypes(parameter.valueDeclaration); } - else if (isInferentialContext(mapper)) { - // Even if the parameter already has a type, it might be because it was given a type while - // processing the function as an argument to a prior signature during overload resolution. - // If this was the case, it may have caused some type parameters to be fixed. So here, - // we need to ensure that type parameters at the same positions get fixed again. This is - // done by calling instantiateType to attach the mapper to the contextualType, and then - // calling inferTypes to force a walk of contextualType so that all the correct fixing - // happens. The choice to pass in links.type may seem kind of arbitrary, but it serves - // to make sure that all the correct positions in contextualType are reached by the walk. - // Here is an example: - // - // interface Base { - // baseProp; - // } - // interface Derived extends Base { - // toBase(): Base; - // } - // - // var derived: Derived; - // - // declare function foo(x: T, func: (p: T) => T): T; - // declare function foo(x: T, func: (p: T) => T): T; - // - // var result = foo(derived, d => d.toBase()); - // - // We are typing d while checking the second overload. But we've already given d - // a type (Derived) from the first overload. However, we still want to fix the - // T in the second overload so that we do not infer Base as a candidate for T - // (inferring Base would make type argument inference inconsistent between the two - // overloads). - inferTypes(mapper.context, links.type, instantiateType(contextualType, mapper)); - } - } - function getReturnTypeFromJSDocComment(func) { - var returnTag = ts.getJSDocReturnTag(func); - if (returnTag && returnTag.typeExpression) { - return getTypeFromTypeNode(returnTag.typeExpression.type); - } - return undefined; - } - function createPromiseType(promisedType) { - // creates a `Promise` type where `T` is the promisedType argument - var globalPromiseType = getGlobalPromiseType(); - if (globalPromiseType !== emptyGenericType) { - // if the promised type is itself a promise, get the underlying type; otherwise, fallback to the promised type - promisedType = getAwaitedType(promisedType); - return createTypeReference(globalPromiseType, [promisedType]); - } - return emptyObjectType; - } - function createPromiseReturnType(func, promisedType) { - var promiseType = createPromiseType(promisedType); - if (promiseType === emptyObjectType) { - error(func, ts.Diagnostics.An_async_function_or_method_must_have_a_valid_awaitable_return_type); - return unknownType; + // Obtain base constraint such that we can bail out if the constraint is an unknown type + var objectType = getApparentType(checkNonNullExpression(node.expression)); + var indexType = node.argumentExpression ? checkExpression(node.argumentExpression) : unknownType; + if (objectType === unknownType || objectType === silentNeverType) { + return objectType; } - return promiseType; - } - function getReturnTypeFromBody(func, contextualMapper) { - var contextualSignature = getContextualSignatureForFunctionLikeDeclaration(func); - if (!func.body) { + var isConstEnum = isConstEnumObjectType(objectType); + if (isConstEnum && + (!node.argumentExpression || node.argumentExpression.kind !== 9 /* StringLiteral */)) { + error(node.argumentExpression, ts.Diagnostics.A_const_enum_member_can_only_be_accessed_using_a_string_literal); return unknownType; } - var isAsync = ts.isAsyncFunctionLike(func); - var type; - if (func.body.kind !== 199 /* Block */) { - type = checkExpressionCached(func.body, contextualMapper); - if (isAsync) { - // From within an async function you can return either a non-promise value or a promise. Any - // Promise/A+ compatible implementation will always assimilate any foreign promise, so the - // return type of the body should be unwrapped to its awaited type, which we will wrap in - // the native Promise type later in this function. - type = checkAwaitedType(type, func, ts.Diagnostics.Return_expression_in_async_function_does_not_have_a_valid_callable_then_member); + // TypeScript 1.0 spec (April 2014): 4.10 Property Access + // - If IndexExpr is a string literal or a numeric literal and ObjExpr's apparent type has a property with the name + // given by that literal(converted to its string representation in the case of a numeric literal), the property access is of the type of that property. + // - Otherwise, if ObjExpr's apparent type has a numeric index signature and IndexExpr is of type Any, the Number primitive type, or an enum type, + // the property access is of the type of that index signature. + // - Otherwise, if ObjExpr's apparent type has a string index signature and IndexExpr is of type Any, the String or Number primitive type, or an enum type, + // the property access is of the type of that index signature. + // - Otherwise, if IndexExpr is of type Any, the String or Number primitive type, or an enum type, the property access is of type Any. + // See if we can index as a property. + if (node.argumentExpression) { + var name_16 = getPropertyNameForIndexedAccess(node.argumentExpression, indexType); + if (name_16 !== undefined) { + var prop = getPropertyOfType(objectType, name_16); + if (prop) { + getNodeLinks(node).resolvedSymbol = prop; + return getTypeOfSymbol(prop); + } + else if (isConstEnum) { + error(node.argumentExpression, ts.Diagnostics.Property_0_does_not_exist_on_const_enum_1, name_16, symbolToString(objectType.symbol)); + return unknownType; + } } } - else { - var types = void 0; - var funcIsGenerator = !!func.asteriskToken; - if (funcIsGenerator) { - types = checkAndAggregateYieldOperandTypes(func, contextualMapper); - if (types.length === 0) { - var iterableIteratorAny = createIterableIteratorType(anyType); - if (compilerOptions.noImplicitAny) { - error(func.asteriskToken, ts.Diagnostics.Generator_implicitly_has_type_0_because_it_does_not_yield_any_values_Consider_supplying_a_return_type, typeToString(iterableIteratorAny)); - } - return iterableIteratorAny; + // Check for compatible indexer types. + var allowedNullableFlags = strictNullChecks ? 0 : 6144 /* Nullable */; + if (isTypeAnyOrAllConstituentTypesHaveKind(indexType, 34 /* StringLike */ | 340 /* NumberLike */ | 512 /* ESSymbol */ | allowedNullableFlags)) { + // Try to use a number indexer. + if (isTypeAnyOrAllConstituentTypesHaveKind(indexType, 340 /* NumberLike */ | allowedNullableFlags) || isForInVariableForNumericPropertyNames(node.argumentExpression)) { + var numberIndexInfo = getIndexInfoOfType(objectType, 1 /* Number */); + if (numberIndexInfo) { + getNodeLinks(node).resolvedIndexInfo = numberIndexInfo; + return numberIndexInfo.type; } } - else { - types = checkAndAggregateReturnExpressionTypes(func, contextualMapper); - if (!types) { - // For an async function, the return type will not be never, but rather a Promise for never. - return isAsync ? createPromiseReturnType(func, neverType) : neverType; - } - if (types.length === 0) { - // For an async function, the return type will not be void, but rather a Promise for void. - return isAsync ? createPromiseReturnType(func, voidType) : voidType; - } + // Try to use string indexing. + var stringIndexInfo = getIndexInfoOfType(objectType, 0 /* String */); + if (stringIndexInfo) { + getNodeLinks(node).resolvedIndexInfo = stringIndexInfo; + return stringIndexInfo.type; } - // Return a union of the return expression types. - type = getUnionType(types, /*subtypeReduction*/ true); - if (funcIsGenerator) { - type = createIterableIteratorType(type); + // Fall back to any. + if (compilerOptions.noImplicitAny && !compilerOptions.suppressImplicitAnyIndexErrors && !isTypeAny(objectType)) { + error(node, getIndexTypeOfType(objectType, 1 /* Number */) ? + ts.Diagnostics.Element_implicitly_has_an_any_type_because_index_expression_is_not_of_type_number : + ts.Diagnostics.Index_signature_of_object_type_implicitly_has_an_any_type); } + return anyType; } - if (!contextualSignature) { - reportErrorsFromWidening(func, type); - } - if (isUnitType(type) && !(contextualSignature && isLiteralContextualType(getReturnTypeOfSignature(contextualSignature)))) { - type = getWidenedLiteralType(type); - } - var widenedType = getWidenedType(type); - // From within an async function you can return either a non-promise value or a promise. Any - // Promise/A+ compatible implementation will always assimilate any foreign promise, so the - // return type of the body is awaited type of the body, wrapped in a native Promise type. - return isAsync ? createPromiseReturnType(func, widenedType) : widenedType; + // REVIEW: Users should know the type that was actually used. + error(node, ts.Diagnostics.An_index_expression_argument_must_be_of_type_string_number_symbol_or_any); + return unknownType; } - function checkAndAggregateYieldOperandTypes(func, contextualMapper) { - var aggregatedTypes = []; - ts.forEachYieldExpression(func.body, function (yieldExpression) { - var expr = yieldExpression.expression; - if (expr) { - var type = checkExpressionCached(expr, contextualMapper); - if (yieldExpression.asteriskToken) { - // A yield* expression effectively yields everything that its operand yields - type = checkElementTypeOfIterable(type, yieldExpression.expression); - } - if (!ts.contains(aggregatedTypes, type)) { - aggregatedTypes.push(type); - } + /** + * If indexArgumentExpression is a string literal or number literal, returns its text. + * If indexArgumentExpression is a constant value, returns its string value. + * If indexArgumentExpression is a well known symbol, returns the property name corresponding + * to this symbol, as long as it is a proper symbol reference. + * Otherwise, returns undefined. + */ + function getPropertyNameForIndexedAccess(indexArgumentExpression, indexArgumentType) { + if (indexArgumentExpression.kind === 9 /* StringLiteral */ || indexArgumentExpression.kind === 8 /* NumericLiteral */) { + return indexArgumentExpression.text; + } + if (indexArgumentExpression.kind === 173 /* ElementAccessExpression */ || indexArgumentExpression.kind === 172 /* PropertyAccessExpression */) { + var value = getConstantValue(indexArgumentExpression); + if (value !== undefined) { + return value.toString(); } - }); - return aggregatedTypes; + } + if (checkThatExpressionIsProperSymbolReference(indexArgumentExpression, indexArgumentType, /*reportError*/ false)) { + var rightHandSideName = indexArgumentExpression.name.text; + return ts.getPropertyNameForKnownSymbolName(rightHandSideName); + } + return undefined; } - function isExhaustiveSwitchStatement(node) { - if (!node.possiblyExhaustive) { + /** + * A proper symbol reference requires the following: + * 1. The property access denotes a property that exists + * 2. The expression is of the form Symbol. + * 3. The property access is of the primitive type symbol. + * 4. Symbol in this context resolves to the global Symbol object + */ + function checkThatExpressionIsProperSymbolReference(expression, expressionType, reportError) { + if (expressionType === unknownType) { + // There is already an error, so no need to report one. return false; } - var type = checkExpression(node.expression); - if (!isLiteralType(type)) { + if (!ts.isWellKnownSymbolSyntactically(expression)) { return false; } - var switchTypes = getSwitchClauseTypes(node); - if (!switchTypes.length) { + // Make sure the property type is the primitive symbol type + if ((expressionType.flags & 512 /* ESSymbol */) === 0) { + if (reportError) { + error(expression, ts.Diagnostics.A_computed_property_name_of_the_form_0_must_be_of_type_symbol, ts.getTextOfNode(expression)); + } return false; } - return eachTypeContainedIn(type, switchTypes); - } - function functionHasImplicitReturn(func) { - if (!(func.flags & 128 /* HasImplicitReturn */)) { + // The name is Symbol., so make sure Symbol actually resolves to the + // global Symbol object + var leftHandSide = expression.expression; + var leftHandSideSymbol = getResolvedSymbol(leftHandSide); + if (!leftHandSideSymbol) { return false; } - var lastStatement = ts.lastOrUndefined(func.body.statements); - if (lastStatement && lastStatement.kind === 213 /* SwitchStatement */ && isExhaustiveSwitchStatement(lastStatement)) { + var globalESSymbol = getGlobalESSymbolConstructorSymbol(); + if (!globalESSymbol) { + // Already errored when we tried to look up the symbol + return false; + } + if (leftHandSideSymbol !== globalESSymbol) { + if (reportError) { + error(leftHandSide, ts.Diagnostics.Symbol_reference_does_not_refer_to_the_global_Symbol_constructor_object); + } return false; } return true; } - function checkAndAggregateReturnExpressionTypes(func, contextualMapper) { - var isAsync = ts.isAsyncFunctionLike(func); - var aggregatedTypes = []; - var hasReturnWithNoExpression = functionHasImplicitReturn(func); - var hasReturnOfTypeNever = false; - ts.forEachReturnStatement(func.body, function (returnStatement) { - var expr = returnStatement.expression; - if (expr) { - var type = checkExpressionCached(expr, contextualMapper); - if (isAsync) { - // From within an async function you can return either a non-promise value or a promise. Any - // Promise/A+ compatible implementation will always assimilate any foreign promise, so the - // return type of the body should be unwrapped to its awaited type, which should be wrapped in - // the native Promise type by the caller. - type = checkAwaitedType(type, func, ts.Diagnostics.Return_expression_in_async_function_does_not_have_a_valid_callable_then_member); - } - if (type.flags & 8192 /* Never */) { - hasReturnOfTypeNever = true; + function resolveUntypedCall(node) { + if (node.kind === 176 /* TaggedTemplateExpression */) { + checkExpression(node.template); + } + else if (node.kind !== 143 /* Decorator */) { + ts.forEach(node.arguments, function (argument) { + checkExpression(argument); + }); + } + return anySignature; + } + function resolveErrorCall(node) { + resolveUntypedCall(node); + return unknownSignature; + } + // Re-order candidate signatures into the result array. Assumes the result array to be empty. + // The candidate list orders groups in reverse, but within a group signatures are kept in declaration order + // A nit here is that we reorder only signatures that belong to the same symbol, + // so order how inherited signatures are processed is still preserved. + // interface A { (x: string): void } + // interface B extends A { (x: 'foo'): string } + // const b: B; + // b('foo') // <- here overloads should be processed as [(x:'foo'): string, (x: string): void] + function reorderCandidates(signatures, result) { + var lastParent; + var lastSymbol; + var cutoffIndex = 0; + var index; + var specializedIndex = -1; + var spliceIndex; + ts.Debug.assert(!result.length); + for (var _i = 0, signatures_2 = signatures; _i < signatures_2.length; _i++) { + var signature = signatures_2[_i]; + var symbol = signature.declaration && getSymbolOfNode(signature.declaration); + var parent_11 = signature.declaration && signature.declaration.parent; + if (!lastSymbol || symbol === lastSymbol) { + if (lastParent && parent_11 === lastParent) { + index++; } - else if (!ts.contains(aggregatedTypes, type)) { - aggregatedTypes.push(type); + else { + lastParent = parent_11; + index = cutoffIndex; } } else { - hasReturnWithNoExpression = true; + // current declaration belongs to a different symbol + // set cutoffIndex so re-orderings in the future won't change result set from 0 to cutoffIndex + index = cutoffIndex = result.length; + lastParent = parent_11; } - }); - if (aggregatedTypes.length === 0 && !hasReturnWithNoExpression && (hasReturnOfTypeNever || - func.kind === 179 /* FunctionExpression */ || func.kind === 180 /* ArrowFunction */)) { - return undefined; + lastSymbol = symbol; + // specialized signatures always need to be placed before non-specialized signatures regardless + // of the cutoff position; see GH#1133 + if (signature.hasLiteralTypes) { + specializedIndex++; + spliceIndex = specializedIndex; + // The cutoff index always needs to be greater than or equal to the specialized signature index + // in order to prevent non-specialized signatures from being added before a specialized + // signature. + cutoffIndex++; + } + else { + spliceIndex = index; + } + result.splice(spliceIndex, 0, signature); } - if (strictNullChecks && aggregatedTypes.length && hasReturnWithNoExpression) { - if (!ts.contains(aggregatedTypes, undefinedType)) { - aggregatedTypes.push(undefinedType); + } + function getSpreadArgumentIndex(args) { + for (var i = 0; i < args.length; i++) { + var arg = args[i]; + if (arg && arg.kind === 191 /* SpreadElementExpression */) { + return i; } } - return aggregatedTypes; + return -1; } - /** - * TypeScript Specification 1.0 (6.3) - July 2014 - * An explicitly typed function whose return type isn't the Void type, - * the Any type, or a union type containing the Void or Any type as a constituent - * must have at least one return statement somewhere in its body. - * An exception to this rule is if the function implementation consists of a single 'throw' statement. - * - * @param returnType - return type of the function, can be undefined if return type is not explicitly specified - */ - function checkAllCodePathsInNonVoidFunctionReturnOrThrow(func, returnType) { - if (!produceDiagnostics) { - return; + function hasCorrectArity(node, args, signature, signatureHelpTrailingComma) { + if (signatureHelpTrailingComma === void 0) { signatureHelpTrailingComma = false; } + var argCount; // Apparent number of arguments we will have in this call + var typeArguments; // Type arguments (undefined if none) + var callIsIncomplete; // In incomplete call we want to be lenient when we have too few arguments + var isDecorator; + var spreadArgIndex = -1; + if (node.kind === 176 /* TaggedTemplateExpression */) { + var tagExpression = node; + // Even if the call is incomplete, we'll have a missing expression as our last argument, + // so we can say the count is just the arg list length + argCount = args.length; + typeArguments = undefined; + if (tagExpression.template.kind === 189 /* TemplateExpression */) { + // If a tagged template expression lacks a tail literal, the call is incomplete. + // Specifically, a template only can end in a TemplateTail or a Missing literal. + var templateExpression = tagExpression.template; + var lastSpan = ts.lastOrUndefined(templateExpression.templateSpans); + ts.Debug.assert(lastSpan !== undefined); // we should always have at least one span. + callIsIncomplete = ts.nodeIsMissing(lastSpan.literal) || !!lastSpan.literal.isUnterminated; + } + else { + // If the template didn't end in a backtick, or its beginning occurred right prior to EOF, + // then this might actually turn out to be a TemplateHead in the future; + // so we consider the call to be incomplete. + var templateLiteral = tagExpression.template; + ts.Debug.assert(templateLiteral.kind === 11 /* NoSubstitutionTemplateLiteral */); + callIsIncomplete = !!templateLiteral.isUnterminated; + } } - // Functions with with an explicitly specified 'void' or 'any' return type don't need any return expressions. - if (returnType && maybeTypeOfKind(returnType, 1 /* Any */ | 1024 /* Void */)) { - return; + else if (node.kind === 143 /* Decorator */) { + isDecorator = true; + typeArguments = undefined; + argCount = getEffectiveArgumentCount(node, /*args*/ undefined, signature); } - // If all we have is a function signature, or an arrow function with an expression body, then there is nothing to check. - // also if HasImplicitReturn flag is not set this means that all codepaths in function body end with return or throw - if (ts.nodeIsMissing(func.body) || func.body.kind !== 199 /* Block */ || !functionHasImplicitReturn(func)) { - return; + else { + var callExpression = node; + if (!callExpression.arguments) { + // This only happens when we have something of the form: 'new C' + ts.Debug.assert(callExpression.kind === 175 /* NewExpression */); + return signature.minArgumentCount === 0; + } + argCount = signatureHelpTrailingComma ? args.length + 1 : args.length; + // If we are missing the close paren, the call is incomplete. + callIsIncomplete = callExpression.arguments.end === callExpression.end; + typeArguments = callExpression.typeArguments; + spreadArgIndex = getSpreadArgumentIndex(args); } - var hasExplicitReturn = func.flags & 256 /* HasExplicitReturn */; - if (returnType && returnType.flags & 8192 /* Never */) { - error(func.type, ts.Diagnostics.A_function_returning_never_cannot_have_a_reachable_end_point); + // If the user supplied type arguments, but the number of type arguments does not match + // the declared number of type parameters, the call has an incorrect arity. + var hasRightNumberOfTypeArgs = !typeArguments || + (signature.typeParameters && typeArguments.length === signature.typeParameters.length); + if (!hasRightNumberOfTypeArgs) { + return false; } - else if (returnType && !hasExplicitReturn) { - // minimal check: function has syntactic return type annotation and no explicit return statements in the body - // this function does not conform to the specification. - // NOTE: having returnType !== undefined is a precondition for entering this branch so func.type will always be present - error(func.type, ts.Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value); + // If spread arguments are present, check that they correspond to a rest parameter. If so, no + // further checking is necessary. + if (spreadArgIndex >= 0) { + return isRestParameterIndex(signature, spreadArgIndex); } - else if (returnType && strictNullChecks && !isTypeAssignableTo(undefinedType, returnType)) { - error(func.type, ts.Diagnostics.Function_lacks_ending_return_statement_and_return_type_does_not_include_undefined); + // Too many arguments implies incorrect arity. + if (!signature.hasRestParameter && argCount > signature.parameters.length) { + return false; } - else if (compilerOptions.noImplicitReturns) { - if (!returnType) { - // If return type annotation is omitted check if function has any explicit return statements. - // If it does not have any - its inferred return type is void - don't do any checks. - // Otherwise get inferred return type from function body and report error only if it is not void / anytype - if (!hasExplicitReturn) { - return; - } - var inferredReturnType = getReturnTypeOfSignature(getSignatureFromDeclaration(func)); - if (isUnwrappedReturnTypeVoidOrAny(func, inferredReturnType)) { - return; - } + // If the call is incomplete, we should skip the lower bound check. + var hasEnoughArguments = argCount >= signature.minArgumentCount; + return callIsIncomplete || hasEnoughArguments; + } + // If type has a single call signature and no other members, return that signature. Otherwise, return undefined. + function getSingleCallSignature(type) { + if (type.flags & 2588672 /* ObjectType */) { + var resolved = resolveStructuredTypeMembers(type); + if (resolved.callSignatures.length === 1 && resolved.constructSignatures.length === 0 && + resolved.properties.length === 0 && !resolved.stringIndexInfo && !resolved.numberIndexInfo) { + return resolved.callSignatures[0]; } - error(func.type || func, ts.Diagnostics.Not_all_code_paths_return_a_value); } + return undefined; } - function checkFunctionExpressionOrObjectLiteralMethod(node, contextualMapper) { - ts.Debug.assert(node.kind !== 147 /* MethodDeclaration */ || ts.isObjectLiteralMethod(node)); - // Grammar checking - var hasGrammarError = checkGrammarFunctionLikeDeclaration(node); - if (!hasGrammarError && node.kind === 179 /* FunctionExpression */) { - checkGrammarForGenerator(node); - } - // The identityMapper object is used to indicate that function expressions are wildcards - if (contextualMapper === identityMapper && isContextSensitive(node)) { - checkNodeDeferred(node); - return anyFunctionType; - } - var links = getNodeLinks(node); - var type = getTypeOfSymbol(node.symbol); - var contextSensitive = isContextSensitive(node); - var mightFixTypeParameters = contextSensitive && isInferentialContext(contextualMapper); - // Check if function expression is contextually typed and assign parameter types if so. - // See the comment in assignTypeToParameterAndFixTypeParameters to understand why we need to - // check mightFixTypeParameters. - if (mightFixTypeParameters || !(links.flags & 1024 /* ContextChecked */)) { - var contextualSignature = getContextualSignature(node); - // If a type check is started at a function expression that is an argument of a function call, obtaining the - // contextual type may recursively get back to here during overload resolution of the call. If so, we will have - // already assigned contextual types. - var contextChecked = !!(links.flags & 1024 /* ContextChecked */); - if (mightFixTypeParameters || !contextChecked) { - links.flags |= 1024 /* ContextChecked */; - if (contextualSignature) { - var signature = getSignaturesOfType(type, 0 /* Call */)[0]; - if (contextSensitive) { - assignContextualParameterTypes(signature, contextualSignature, contextualMapper || identityMapper); - } - if (mightFixTypeParameters || !node.type && !signature.resolvedReturnType) { - var returnType = getReturnTypeFromBody(node, contextualMapper); - if (!signature.resolvedReturnType) { - signature.resolvedReturnType = returnType; - } - } - } - if (!contextChecked) { - checkSignatureDeclaration(node); - checkNodeDeferred(node); - } + // Instantiate a generic signature in the context of a non-generic signature (section 3.8.5 in TypeScript spec) + function instantiateSignatureInContextOf(signature, contextualSignature, contextualMapper) { + var context = createInferenceContext(signature, /*inferUnionTypes*/ true); + forEachMatchingParameterType(contextualSignature, signature, function (source, target) { + // Type parameters from outer context referenced by source type are fixed by instantiation of the source type + inferTypes(context, instantiateType(source, contextualMapper), target); + }); + return getSignatureInstantiation(signature, getInferredTypes(context)); + } + function inferTypeArguments(node, signature, args, excludeArgument, context) { + var typeParameters = signature.typeParameters; + var inferenceMapper = getInferenceMapper(context); + // Clear out all the inference results from the last time inferTypeArguments was called on this context + for (var i = 0; i < typeParameters.length; i++) { + // As an optimization, we don't have to clear (and later recompute) inferred types + // for type parameters that have already been fixed on the previous call to inferTypeArguments. + // It would be just as correct to reset all of them. But then we'd be repeating the same work + // for the type parameters that were fixed, namely the work done by getInferredType. + if (!context.inferences[i].isFixed) { + context.inferredTypes[i] = undefined; } } - if (produceDiagnostics && node.kind !== 147 /* MethodDeclaration */ && node.kind !== 146 /* MethodSignature */) { - checkCollisionWithCapturedSuperVariable(node, node.name); - checkCollisionWithCapturedThisVariable(node, node.name); + // On this call to inferTypeArguments, we may get more inferences for certain type parameters that were not + // fixed last time. This means that a type parameter that failed inference last time may succeed this time, + // or vice versa. Therefore, the failedTypeParameterIndex is useless if it points to an unfixed type parameter, + // because it may change. So here we reset it. However, getInferredType will not revisit any type parameters + // that were previously fixed. So if a fixed type parameter failed previously, it will fail again because + // it will contain the exact same set of inferences. So if we reset the index from a fixed type parameter, + // we will lose information that we won't recover this time around. + if (context.failedTypeParameterIndex !== undefined && !context.inferences[context.failedTypeParameterIndex].isFixed) { + context.failedTypeParameterIndex = undefined; } - return type; - } - function checkFunctionExpressionOrObjectLiteralMethodDeferred(node) { - ts.Debug.assert(node.kind !== 147 /* MethodDeclaration */ || ts.isObjectLiteralMethod(node)); - var isAsync = ts.isAsyncFunctionLike(node); - var returnOrPromisedType = node.type && (isAsync ? checkAsyncFunctionReturnType(node) : getTypeFromTypeNode(node.type)); - if (!node.asteriskToken) { - // return is not necessary in the body of generators - checkAllCodePathsInNonVoidFunctionReturnOrThrow(node, returnOrPromisedType); + var thisType = getThisTypeOfSignature(signature); + if (thisType) { + var thisArgumentNode = getThisArgumentOfCall(node); + var thisArgumentType = thisArgumentNode ? checkExpression(thisArgumentNode) : voidType; + inferTypes(context, thisArgumentType, thisType); } - if (node.body) { - if (!node.type) { - // There are some checks that are only performed in getReturnTypeFromBody, that may produce errors - // we need. An example is the noImplicitAny errors resulting from widening the return expression - // of a function. Because checking of function expression bodies is deferred, there was never an - // appropriate time to do this during the main walk of the file (see the comment at the top of - // checkFunctionExpressionBodies). So it must be done now. - getReturnTypeOfSignature(getSignatureFromDeclaration(node)); - } - if (node.body.kind === 199 /* Block */) { - checkSourceElement(node.body); - } - else { - // From within an async function you can return either a non-promise value or a promise. Any - // Promise/A+ compatible implementation will always assimilate any foreign promise, so we - // should not be checking assignability of a promise to the return type. Instead, we need to - // check assignability of the awaited type of the expression body against the promised type of - // its return type annotation. - var exprType = checkExpression(node.body); - if (returnOrPromisedType) { - if (isAsync) { - var awaitedType = checkAwaitedType(exprType, node.body, ts.Diagnostics.Expression_body_for_async_arrow_function_does_not_have_a_valid_callable_then_member); - checkTypeAssignableTo(awaitedType, returnOrPromisedType, node.body); - } - else { - checkTypeAssignableTo(exprType, returnOrPromisedType, node.body); - } + // We perform two passes over the arguments. In the first pass we infer from all arguments, but use + // wildcards for all context sensitive function expressions. + var argCount = getEffectiveArgumentCount(node, args, signature); + for (var i = 0; i < argCount; i++) { + var arg = getEffectiveArgument(node, args, i); + // If the effective argument is 'undefined', then it is an argument that is present but is synthetic. + if (arg === undefined || arg.kind !== 193 /* OmittedExpression */) { + var paramType = getTypeAtPosition(signature, i); + var argType = getEffectiveArgumentType(node, i, arg); + // If the effective argument type is 'undefined', there is no synthetic type + // for the argument. In that case, we should check the argument. + if (argType === undefined) { + // For context sensitive arguments we pass the identityMapper, which is a signal to treat all + // context sensitive function expressions as wildcards + var mapper = excludeArgument && excludeArgument[i] !== undefined ? identityMapper : inferenceMapper; + argType = checkExpressionWithContextualType(arg, paramType, mapper); } + inferTypes(context, argType, paramType); } - registerForUnusedIdentifiersCheck(node); - } - } - function checkArithmeticOperandType(operand, type, diagnostic) { - if (!isTypeAnyOrAllConstituentTypesHaveKind(type, 340 /* NumberLike */)) { - error(operand, diagnostic); - return false; } - return true; - } - function isReadonlySymbol(symbol) { - // The following symbols are considered read-only: - // Properties with a 'readonly' modifier - // Variables declared with 'const' - // Get accessors without matching set accessors - // Enum members - // Unions and intersections of the above (unions and intersections eagerly set isReadonly on creation) - return symbol.isReadonly || - symbol.flags & 4 /* Property */ && (getDeclarationModifierFlagsFromSymbol(symbol) & 64 /* Readonly */) !== 0 || - symbol.flags & 3 /* Variable */ && (getDeclarationNodeFlagsFromSymbol(symbol) & 2 /* Const */) !== 0 || - symbol.flags & 98304 /* Accessor */ && !(symbol.flags & 65536 /* SetAccessor */) || - (symbol.flags & 8 /* EnumMember */) !== 0; - } - function isReferenceToReadonlyEntity(expr, symbol) { - if (isReadonlySymbol(symbol)) { - // Allow assignments to readonly properties within constructors of the same class declaration. - if (symbol.flags & 4 /* Property */ && - (expr.kind === 172 /* PropertyAccessExpression */ || expr.kind === 173 /* ElementAccessExpression */) && - expr.expression.kind === 97 /* ThisKeyword */) { - // Look for if this is the constructor for the class that `symbol` is a property of. - var func = ts.getContainingFunction(expr); - if (!(func && func.kind === 148 /* Constructor */)) - return true; - // If func.parent is a class and symbol is a (readonly) property of that class, or - // if func is a constructor and symbol is a (readonly) parameter property declared in it, - // then symbol is writeable here. - return !(func.parent === symbol.valueDeclaration.parent || func === symbol.valueDeclaration.parent); + // In the second pass we visit only context sensitive arguments, and only those that aren't excluded, this + // time treating function expressions normally (which may cause previously inferred type arguments to be fixed + // as we construct types for contextually typed parameters) + // Decorators will not have `excludeArgument`, as their arguments cannot be contextually typed. + // Tagged template expressions will always have `undefined` for `excludeArgument[0]`. + if (excludeArgument) { + for (var i = 0; i < argCount; i++) { + // No need to check for omitted args and template expressions, their exclusion value is always undefined + if (excludeArgument[i] === false) { + var arg = args[i]; + var paramType = getTypeAtPosition(signature, i); + inferTypes(context, checkExpressionWithContextualType(arg, paramType, inferenceMapper), paramType); + } } - return true; } - return false; + getInferredTypes(context); } - function isReferenceThroughNamespaceImport(expr) { - if (expr.kind === 172 /* PropertyAccessExpression */ || expr.kind === 173 /* ElementAccessExpression */) { - var node = skipParenthesizedNodes(expr.expression); - if (node.kind === 69 /* Identifier */) { - var symbol = getNodeLinks(node).resolvedSymbol; - if (symbol.flags & 8388608 /* Alias */) { - var declaration = getDeclarationOfAliasSymbol(symbol); - return declaration && declaration.kind === 232 /* NamespaceImport */; + function checkTypeArguments(signature, typeArgumentNodes, typeArgumentTypes, reportErrors, headMessage) { + var typeParameters = signature.typeParameters; + var typeArgumentsAreAssignable = true; + var mapper; + for (var i = 0; i < typeParameters.length; i++) { + if (typeArgumentsAreAssignable /* so far */) { + var constraint = getConstraintOfTypeParameter(typeParameters[i]); + if (constraint) { + var errorInfo = void 0; + var typeArgumentHeadMessage = ts.Diagnostics.Type_0_does_not_satisfy_the_constraint_1; + if (reportErrors && headMessage) { + errorInfo = ts.chainDiagnosticMessages(errorInfo, typeArgumentHeadMessage); + typeArgumentHeadMessage = headMessage; + } + if (!mapper) { + mapper = createTypeMapper(typeParameters, typeArgumentTypes); + } + var typeArgument = typeArgumentTypes[i]; + typeArgumentsAreAssignable = checkTypeAssignableTo(typeArgument, getTypeWithThisArgument(instantiateType(constraint, mapper), typeArgument), reportErrors ? typeArgumentNodes[i] : undefined, typeArgumentHeadMessage, errorInfo); } } } - return false; + return typeArgumentsAreAssignable; } - function checkReferenceExpression(expr, invalidReferenceMessage, constantVariableMessage) { - // References are combinations of identifiers, parentheses, and property accesses. - var node = skipParenthesizedNodes(expr); - if (node.kind !== 69 /* Identifier */ && node.kind !== 172 /* PropertyAccessExpression */ && node.kind !== 173 /* ElementAccessExpression */) { - error(expr, invalidReferenceMessage); - return false; + function checkApplicableSignature(node, args, signature, relation, excludeArgument, reportErrors) { + var thisType = getThisTypeOfSignature(signature); + if (thisType && thisType !== voidType && node.kind !== 175 /* NewExpression */) { + // If the called expression is not of the form `x.f` or `x["f"]`, then sourceType = voidType + // If the signature's 'this' type is voidType, then the check is skipped -- anything is compatible. + // If the expression is a new expression, then the check is skipped. + var thisArgumentNode = getThisArgumentOfCall(node); + var thisArgumentType = thisArgumentNode ? checkExpression(thisArgumentNode) : voidType; + var errorNode = reportErrors ? (thisArgumentNode || node) : undefined; + var headMessage_1 = ts.Diagnostics.The_this_context_of_type_0_is_not_assignable_to_method_s_this_of_type_1; + if (!checkTypeRelatedTo(thisArgumentType, getThisTypeOfSignature(signature), relation, errorNode, headMessage_1)) { + return false; + } } - // Because we get the symbol from the resolvedSymbol property, it might be of kind - // SymbolFlags.ExportValue. In this case it is necessary to get the actual export - // symbol, which will have the correct flags set on it. - var links = getNodeLinks(node); - var symbol = getExportSymbolOfValueSymbolIfExported(links.resolvedSymbol); - if (symbol) { - if (symbol !== unknownSymbol && symbol !== argumentsSymbol) { - // Only variables (and not functions, classes, namespaces, enum objects, or enum members) - // are considered references when referenced using a simple identifier. - if (node.kind === 69 /* Identifier */ && !(symbol.flags & 3 /* Variable */)) { - error(expr, invalidReferenceMessage); - return false; + var headMessage = ts.Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1; + var argCount = getEffectiveArgumentCount(node, args, signature); + for (var i = 0; i < argCount; i++) { + var arg = getEffectiveArgument(node, args, i); + // If the effective argument is 'undefined', then it is an argument that is present but is synthetic. + if (arg === undefined || arg.kind !== 193 /* OmittedExpression */) { + // Check spread elements against rest type (from arity check we know spread argument corresponds to a rest parameter) + var paramType = getTypeAtPosition(signature, i); + var argType = getEffectiveArgumentType(node, i, arg); + // If the effective argument type is 'undefined', there is no synthetic type + // for the argument. In that case, we should check the argument. + if (argType === undefined) { + argType = checkExpressionWithContextualType(arg, paramType, excludeArgument && excludeArgument[i] ? identityMapper : undefined); } - if (isReferenceToReadonlyEntity(node, symbol) || isReferenceThroughNamespaceImport(node)) { - error(expr, constantVariableMessage); + // Use argument expression as error location when reporting errors + var errorNode = reportErrors ? getEffectiveArgumentErrorNode(node, i, arg) : undefined; + if (!checkTypeRelatedTo(argType, paramType, relation, errorNode, headMessage)) { return false; } } } - else if (node.kind === 173 /* ElementAccessExpression */) { - if (links.resolvedIndexInfo && links.resolvedIndexInfo.isReadonly) { - error(expr, constantVariableMessage); - return false; - } - } return true; } - function checkDeleteExpression(node) { - checkExpression(node.expression); - return booleanType; - } - function checkTypeOfExpression(node) { - checkExpression(node.expression); - return stringType; - } - function checkVoidExpression(node) { - checkExpression(node.expression); - return undefinedWideningType; - } - function checkAwaitExpression(node) { - // Grammar checking - if (produceDiagnostics) { - if (!(node.flags & 262144 /* AwaitContext */)) { - grammarErrorOnFirstToken(node, ts.Diagnostics.await_expression_is_only_allowed_within_an_async_function); + /** + * Returns the this argument in calls like x.f(...) and x[f](...). Undefined otherwise. + */ + function getThisArgumentOfCall(node) { + if (node.kind === 174 /* CallExpression */) { + var callee = node.expression; + if (callee.kind === 172 /* PropertyAccessExpression */) { + return callee.expression; } - if (isInParameterInitializerBeforeContainingFunction(node)) { - error(node, ts.Diagnostics.await_expressions_cannot_be_used_in_a_parameter_initializer); + else if (callee.kind === 173 /* ElementAccessExpression */) { + return callee.expression; } } - var operandType = checkExpression(node.expression); - return checkAwaitedType(operandType, node); } - function checkPrefixUnaryExpression(node) { - var operandType = checkExpression(node.operand); - if (operandType === silentNeverType) { - return silentNeverType; + /** + * Returns the effective arguments for an expression that works like a function invocation. + * + * If 'node' is a CallExpression or a NewExpression, then its argument list is returned. + * If 'node' is a TaggedTemplateExpression, a new argument list is constructed from the substitution + * expressions, where the first element of the list is `undefined`. + * If 'node' is a Decorator, the argument list will be `undefined`, and its arguments and types + * will be supplied from calls to `getEffectiveArgumentCount` and `getEffectiveArgumentType`. + */ + function getEffectiveCallArguments(node) { + var args; + if (node.kind === 176 /* TaggedTemplateExpression */) { + var template = node.template; + args = [undefined]; + if (template.kind === 189 /* TemplateExpression */) { + ts.forEach(template.templateSpans, function (span) { + args.push(span.expression); + }); + } } - if (node.operator === 36 /* MinusToken */ && node.operand.kind === 8 /* NumericLiteral */) { - return getFreshTypeOfLiteralType(getLiteralTypeForText(64 /* NumberLiteral */, "" + -node.operand.text)); + else if (node.kind === 143 /* Decorator */) { + // For a decorator, we return undefined as we will determine + // the number and types of arguments for a decorator using + // `getEffectiveArgumentCount` and `getEffectiveArgumentType` below. + return undefined; } - switch (node.operator) { - case 35 /* PlusToken */: - case 36 /* MinusToken */: - case 50 /* TildeToken */: - if (maybeTypeOfKind(operandType, 512 /* ESSymbol */)) { - error(node.operand, ts.Diagnostics.The_0_operator_cannot_be_applied_to_type_symbol, ts.tokenToString(node.operator)); - } - return numberType; - case 49 /* ExclamationToken */: - var facts = getTypeFacts(operandType) & (1048576 /* Truthy */ | 2097152 /* Falsy */); - return facts === 1048576 /* Truthy */ ? falseType : - facts === 2097152 /* Falsy */ ? trueType : - booleanType; - case 41 /* PlusPlusToken */: - case 42 /* MinusMinusToken */: - var ok = checkArithmeticOperandType(node.operand, getNonNullableType(operandType), ts.Diagnostics.An_arithmetic_operand_must_be_of_type_any_number_or_an_enum_type); - if (ok) { - // run check only if former checks succeeded to avoid reporting cascading errors - checkReferenceExpression(node.operand, ts.Diagnostics.The_operand_of_an_increment_or_decrement_operator_must_be_a_variable_property_or_indexer, ts.Diagnostics.The_operand_of_an_increment_or_decrement_operator_cannot_be_a_constant_or_a_read_only_property); - } - return numberType; + else { + args = node.arguments || emptyArray; } - return unknownType; + return args; } - function checkPostfixUnaryExpression(node) { - var operandType = checkExpression(node.operand); - if (operandType === silentNeverType) { - return silentNeverType; + /** + * Returns the effective argument count for a node that works like a function invocation. + * If 'node' is a Decorator, the number of arguments is derived from the decoration + * target and the signature: + * If 'node.target' is a class declaration or class expression, the effective argument + * count is 1. + * If 'node.target' is a parameter declaration, the effective argument count is 3. + * If 'node.target' is a property declaration, the effective argument count is 2. + * If 'node.target' is a method or accessor declaration, the effective argument count + * is 3, although it can be 2 if the signature only accepts two arguments, allowing + * us to match a property decorator. + * Otherwise, the argument count is the length of the 'args' array. + */ + function getEffectiveArgumentCount(node, args, signature) { + if (node.kind === 143 /* Decorator */) { + switch (node.parent.kind) { + case 221 /* ClassDeclaration */: + case 192 /* ClassExpression */: + // A class decorator will have one argument (see `ClassDecorator` in core.d.ts) + return 1; + case 145 /* PropertyDeclaration */: + // A property declaration decorator will have two arguments (see + // `PropertyDecorator` in core.d.ts) + return 2; + case 147 /* MethodDeclaration */: + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + // A method or accessor declaration decorator will have two or three arguments (see + // `PropertyDecorator` and `MethodDecorator` in core.d.ts) + // If we are emitting decorators for ES3, we will only pass two arguments. + if (languageVersion === 0 /* ES3 */) { + return 2; + } + // If the method decorator signature only accepts a target and a key, we will only + // type check those arguments. + return signature.parameters.length >= 3 ? 3 : 2; + case 142 /* Parameter */: + // A parameter declaration decorator will have three arguments (see + // `ParameterDecorator` in core.d.ts) + return 3; + } } - var ok = checkArithmeticOperandType(node.operand, getNonNullableType(operandType), ts.Diagnostics.An_arithmetic_operand_must_be_of_type_any_number_or_an_enum_type); - if (ok) { - // run check only if former checks succeeded to avoid reporting cascading errors - checkReferenceExpression(node.operand, ts.Diagnostics.The_operand_of_an_increment_or_decrement_operator_must_be_a_variable_property_or_indexer, ts.Diagnostics.The_operand_of_an_increment_or_decrement_operator_cannot_be_a_constant_or_a_read_only_property); + else { + return args.length; } - return numberType; } - // Return true if type might be of the given kind. A union or intersection type might be of a given - // kind if at least one constituent type is of the given kind. - function maybeTypeOfKind(type, kind) { - if (type.flags & kind) { - return true; + /** + * Returns the effective type of the first argument to a decorator. + * If 'node' is a class declaration or class expression, the effective argument type + * is the type of the static side of the class. + * If 'node' is a parameter declaration, the effective argument type is either the type + * of the static or instance side of the class for the parameter's parent method, + * depending on whether the method is declared static. + * For a constructor, the type is always the type of the static side of the class. + * If 'node' is a property, method, or accessor declaration, the effective argument + * type is the type of the static or instance side of the parent class for class + * element, depending on whether the element is declared static. + */ + function getEffectiveDecoratorFirstArgumentType(node) { + // The first argument to a decorator is its `target`. + if (node.kind === 221 /* ClassDeclaration */) { + // For a class decorator, the `target` is the type of the class (e.g. the + // "static" or "constructor" side of the class) + var classSymbol = getSymbolOfNode(node); + return getTypeOfSymbol(classSymbol); } - if (type.flags & 1572864 /* UnionOrIntersection */) { - var types = type.types; - for (var _i = 0, types_14 = types; _i < types_14.length; _i++) { - var t = types_14[_i]; - if (maybeTypeOfKind(t, kind)) { - return true; - } + if (node.kind === 142 /* Parameter */) { + // For a parameter decorator, the `target` is the parent type of the + // parameter's containing method. + node = node.parent; + if (node.kind === 148 /* Constructor */) { + var classSymbol = getSymbolOfNode(node); + return getTypeOfSymbol(classSymbol); } } - return false; + if (node.kind === 145 /* PropertyDeclaration */ || + node.kind === 147 /* MethodDeclaration */ || + node.kind === 149 /* GetAccessor */ || + node.kind === 150 /* SetAccessor */) { + // For a property or method decorator, the `target` is the + // "static"-side type of the parent of the member if the member is + // declared "static"; otherwise, it is the "instance"-side type of the + // parent of the member. + return getParentTypeOfClassElement(node); + } + ts.Debug.fail("Unsupported decorator target."); + return unknownType; } - // Return true if type is of the given kind. A union type is of a given kind if all constituent types - // are of the given kind. An intersection type is of a given kind if at least one constituent type is - // of the given kind. - function isTypeOfKind(type, kind) { - if (type.flags & kind) { - return true; + /** + * Returns the effective type for the second argument to a decorator. + * If 'node' is a parameter, its effective argument type is one of the following: + * If 'node.parent' is a constructor, the effective argument type is 'any', as we + * will emit `undefined`. + * If 'node.parent' is a member with an identifier, numeric, or string literal name, + * the effective argument type will be a string literal type for the member name. + * If 'node.parent' is a computed property name, the effective argument type will + * either be a symbol type or the string type. + * If 'node' is a member with an identifier, numeric, or string literal name, the + * effective argument type will be a string literal type for the member name. + * If 'node' is a computed property name, the effective argument type will either + * be a symbol type or the string type. + * A class decorator does not have a second argument type. + */ + function getEffectiveDecoratorSecondArgumentType(node) { + // The second argument to a decorator is its `propertyKey` + if (node.kind === 221 /* ClassDeclaration */) { + ts.Debug.fail("Class decorators should not have a second synthetic argument."); + return unknownType; } - if (type.flags & 524288 /* Union */) { - var types = type.types; - for (var _i = 0, types_15 = types; _i < types_15.length; _i++) { - var t = types_15[_i]; - if (!isTypeOfKind(t, kind)) { - return false; - } + if (node.kind === 142 /* Parameter */) { + node = node.parent; + if (node.kind === 148 /* Constructor */) { + // For a constructor parameter decorator, the `propertyKey` will be `undefined`. + return anyType; } - return true; } - if (type.flags & 1048576 /* Intersection */) { - var types = type.types; - for (var _a = 0, types_16 = types; _a < types_16.length; _a++) { - var t = types_16[_a]; - if (isTypeOfKind(t, kind)) { - return true; - } + if (node.kind === 145 /* PropertyDeclaration */ || + node.kind === 147 /* MethodDeclaration */ || + node.kind === 149 /* GetAccessor */ || + node.kind === 150 /* SetAccessor */) { + // The `propertyKey` for a property or method decorator will be a + // string literal type if the member name is an identifier, number, or string; + // otherwise, if the member name is a computed property name it will + // be either string or symbol. + var element = node; + switch (element.name.kind) { + case 69 /* Identifier */: + case 8 /* NumericLiteral */: + case 9 /* StringLiteral */: + return getLiteralTypeForText(32 /* StringLiteral */, element.name.text); + case 140 /* ComputedPropertyName */: + var nameType = checkComputedPropertyName(element.name); + if (isTypeOfKind(nameType, 512 /* ESSymbol */)) { + return nameType; + } + else { + return stringType; + } + default: + ts.Debug.fail("Unsupported property name."); + return unknownType; } } - return false; - } - function isConstEnumObjectType(type) { - return type.flags & (2588672 /* ObjectType */ | 2097152 /* Anonymous */) && type.symbol && isConstEnumSymbol(type.symbol); - } - function isConstEnumSymbol(symbol) { - return (symbol.flags & 128 /* ConstEnum */) !== 0; + ts.Debug.fail("Unsupported decorator target."); + return unknownType; } - function checkInstanceOfExpression(left, right, leftType, rightType) { - if (leftType === silentNeverType || rightType === silentNeverType) { - return silentNeverType; - } - // TypeScript 1.0 spec (April 2014): 4.15.4 - // The instanceof operator requires the left operand to be of type Any, an object type, or a type parameter type, - // and the right operand to be of type Any or a subtype of the 'Function' interface type. - // The result is always of the Boolean primitive type. - // NOTE: do not raise error if leftType is unknown as related error was already reported - if (isTypeOfKind(leftType, 8190 /* Primitive */)) { - error(left, ts.Diagnostics.The_left_hand_side_of_an_instanceof_expression_must_be_of_type_any_an_object_type_or_a_type_parameter); - } - // NOTE: do not raise error if right is unknown as related error was already reported - if (!(isTypeAny(rightType) || isTypeSubtypeOf(rightType, globalFunctionType))) { - error(right, ts.Diagnostics.The_right_hand_side_of_an_instanceof_expression_must_be_of_type_any_or_of_a_type_assignable_to_the_Function_interface_type); + /** + * Returns the effective argument type for the third argument to a decorator. + * If 'node' is a parameter, the effective argument type is the number type. + * If 'node' is a method or accessor, the effective argument type is a + * `TypedPropertyDescriptor` instantiated with the type of the member. + * Class and property decorators do not have a third effective argument. + */ + function getEffectiveDecoratorThirdArgumentType(node) { + // The third argument to a decorator is either its `descriptor` for a method decorator + // or its `parameterIndex` for a parameter decorator + if (node.kind === 221 /* ClassDeclaration */) { + ts.Debug.fail("Class decorators should not have a third synthetic argument."); + return unknownType; } - return booleanType; - } - function checkInExpression(left, right, leftType, rightType) { - if (leftType === silentNeverType || rightType === silentNeverType) { - return silentNeverType; + if (node.kind === 142 /* Parameter */) { + // The `parameterIndex` for a parameter decorator is always a number + return numberType; } - // TypeScript 1.0 spec (April 2014): 4.15.5 - // The in operator requires the left operand to be of type Any, the String primitive type, or the Number primitive type, - // and the right operand to be of type Any, an object type, or a type parameter type. - // The result is always of the Boolean primitive type. - if (!isTypeAnyOrAllConstituentTypesHaveKind(leftType, 34 /* StringLike */ | 340 /* NumberLike */ | 512 /* ESSymbol */)) { - error(left, ts.Diagnostics.The_left_hand_side_of_an_in_expression_must_be_of_type_any_string_number_or_symbol); + if (node.kind === 145 /* PropertyDeclaration */) { + ts.Debug.fail("Property decorators should not have a third synthetic argument."); + return unknownType; } - if (!isTypeAnyOrAllConstituentTypesHaveKind(rightType, 2588672 /* ObjectType */ | 16384 /* TypeParameter */)) { - error(right, ts.Diagnostics.The_right_hand_side_of_an_in_expression_must_be_of_type_any_an_object_type_or_a_type_parameter); + if (node.kind === 147 /* MethodDeclaration */ || + node.kind === 149 /* GetAccessor */ || + node.kind === 150 /* SetAccessor */) { + // The `descriptor` for a method decorator will be a `TypedPropertyDescriptor` + // for the type of the member. + var propertyType = getTypeOfNode(node); + return createTypedPropertyDescriptorType(propertyType); } - return booleanType; + ts.Debug.fail("Unsupported decorator target."); + return unknownType; } - function checkObjectLiteralAssignment(node, sourceType, contextualMapper) { - var properties = node.properties; - for (var _i = 0, properties_4 = properties; _i < properties_4.length; _i++) { - var p = properties_4[_i]; - checkObjectLiteralDestructuringPropertyAssignment(sourceType, p, contextualMapper); + /** + * Returns the effective argument type for the provided argument to a decorator. + */ + function getEffectiveDecoratorArgumentType(node, argIndex) { + if (argIndex === 0) { + return getEffectiveDecoratorFirstArgumentType(node.parent); } - return sourceType; + else if (argIndex === 1) { + return getEffectiveDecoratorSecondArgumentType(node.parent); + } + else if (argIndex === 2) { + return getEffectiveDecoratorThirdArgumentType(node.parent); + } + ts.Debug.fail("Decorators should not have a fourth synthetic argument."); + return unknownType; } - function checkObjectLiteralDestructuringPropertyAssignment(objectLiteralType, property, contextualMapper) { - if (property.kind === 253 /* PropertyAssignment */ || property.kind === 254 /* ShorthandPropertyAssignment */) { - var name_17 = property.name; - if (name_17.kind === 140 /* ComputedPropertyName */) { - checkComputedPropertyName(name_17); - } - if (isComputedNonLiteralName(name_17)) { - return undefined; + /** + * Gets the effective argument type for an argument in a call expression. + */ + function getEffectiveArgumentType(node, argIndex, arg) { + // Decorators provide special arguments, a tagged template expression provides + // a special first argument, and string literals get string literal types + // unless we're reporting errors + if (node.kind === 143 /* Decorator */) { + return getEffectiveDecoratorArgumentType(node, argIndex); + } + else if (argIndex === 0 && node.kind === 176 /* TaggedTemplateExpression */) { + return getGlobalTemplateStringsArrayType(); + } + // This is not a synthetic argument, so we return 'undefined' + // to signal that the caller needs to check the argument. + return undefined; + } + /** + * Gets the effective argument expression for an argument in a call expression. + */ + function getEffectiveArgument(node, args, argIndex) { + // For a decorator or the first argument of a tagged template expression we return undefined. + if (node.kind === 143 /* Decorator */ || + (argIndex === 0 && node.kind === 176 /* TaggedTemplateExpression */)) { + return undefined; + } + return args[argIndex]; + } + /** + * Gets the error node to use when reporting errors for an effective argument. + */ + function getEffectiveArgumentErrorNode(node, argIndex, arg) { + if (node.kind === 143 /* Decorator */) { + // For a decorator, we use the expression of the decorator for error reporting. + return node.expression; + } + else if (argIndex === 0 && node.kind === 176 /* TaggedTemplateExpression */) { + // For a the first argument of a tagged template expression, we use the template of the tag for error reporting. + return node.template; + } + else { + return arg; + } + } + function resolveCall(node, signatures, candidatesOutArray, headMessage) { + var isTaggedTemplate = node.kind === 176 /* TaggedTemplateExpression */; + var isDecorator = node.kind === 143 /* Decorator */; + var typeArguments; + if (!isTaggedTemplate && !isDecorator) { + typeArguments = node.typeArguments; + // We already perform checking on the type arguments on the class declaration itself. + if (node.expression.kind !== 95 /* SuperKeyword */) { + ts.forEach(typeArguments, checkSourceElement); } - var text = getTextOfPropertyName(name_17); - var type = isTypeAny(objectLiteralType) - ? objectLiteralType - : getTypeOfPropertyOfType(objectLiteralType, text) || - isNumericLiteralName(text) && getIndexTypeOfType(objectLiteralType, 1 /* Number */) || - getIndexTypeOfType(objectLiteralType, 0 /* String */); - if (type) { - if (property.kind === 254 /* ShorthandPropertyAssignment */) { - return checkDestructuringAssignment(property, type); - } - else { - // non-shorthand property assignments should always have initializers - return checkDestructuringAssignment(property.initializer, type); + } + var candidates = candidatesOutArray || []; + // reorderCandidates fills up the candidates array directly + reorderCandidates(signatures, candidates); + if (!candidates.length) { + reportError(ts.Diagnostics.Supplied_parameters_do_not_match_any_signature_of_call_target); + return resolveErrorCall(node); + } + var args = getEffectiveCallArguments(node); + // The following applies to any value of 'excludeArgument[i]': + // - true: the argument at 'i' is susceptible to a one-time permanent contextual typing. + // - undefined: the argument at 'i' is *not* susceptible to permanent contextual typing. + // - false: the argument at 'i' *was* and *has been* permanently contextually typed. + // + // The idea is that we will perform type argument inference & assignability checking once + // without using the susceptible parameters that are functions, and once more for each of those + // parameters, contextually typing each as we go along. + // + // For a tagged template, then the first argument be 'undefined' if necessary + // because it represents a TemplateStringsArray. + // + // For a decorator, no arguments are susceptible to contextual typing due to the fact + // decorators are applied to a declaration by the emitter, and not to an expression. + var excludeArgument; + if (!isDecorator) { + // We do not need to call `getEffectiveArgumentCount` here as it only + // applies when calculating the number of arguments for a decorator. + for (var i = isTaggedTemplate ? 1 : 0; i < args.length; i++) { + if (isContextSensitive(args[i])) { + if (!excludeArgument) { + excludeArgument = new Array(args.length); + } + excludeArgument[i] = true; } } + } + // The following variables are captured and modified by calls to chooseOverload. + // If overload resolution or type argument inference fails, we want to report the + // best error possible. The best error is one which says that an argument was not + // assignable to a parameter. This implies that everything else about the overload + // was fine. So if there is any overload that is only incorrect because of an + // argument, we will report an error on that one. + // + // function foo(s: string) {} + // function foo(n: number) {} // Report argument error on this overload + // function foo() {} + // foo(true); + // + // If none of the overloads even made it that far, there are two possibilities. + // There was a problem with type arguments for some overload, in which case + // report an error on that. Or none of the overloads even had correct arity, + // in which case give an arity error. + // + // function foo(x: T, y: T) {} // Report type argument inference error + // function foo() {} + // foo(0, true); + // + var candidateForArgumentError; + var candidateForTypeArgumentError; + var resultOfFailedInference; + var result; + // If we are in signature help, a trailing comma indicates that we intend to provide another argument, + // so we will only accept overloads with arity at least 1 higher than the current number of provided arguments. + var signatureHelpTrailingComma = candidatesOutArray && node.kind === 174 /* CallExpression */ && node.arguments.hasTrailingComma; + // Section 4.12.1: + // if the candidate list contains one or more signatures for which the type of each argument + // expression is a subtype of each corresponding parameter type, the return type of the first + // of those signatures becomes the return type of the function call. + // Otherwise, the return type of the first signature in the candidate list becomes the return + // type of the function call. + // + // Whether the call is an error is determined by assignability of the arguments. The subtype pass + // is just important for choosing the best signature. So in the case where there is only one + // signature, the subtype pass is useless. So skipping it is an optimization. + if (candidates.length > 1) { + result = chooseOverload(candidates, subtypeRelation, signatureHelpTrailingComma); + } + if (!result) { + // Reinitialize these pointers for round two + candidateForArgumentError = undefined; + candidateForTypeArgumentError = undefined; + resultOfFailedInference = undefined; + result = chooseOverload(candidates, assignableRelation, signatureHelpTrailingComma); + } + if (result) { + return result; + } + // No signatures were applicable. Now report errors based on the last applicable signature with + // no arguments excluded from assignability checks. + // If candidate is undefined, it means that no candidates had a suitable arity. In that case, + // skip the checkApplicableSignature check. + if (candidateForArgumentError) { + // excludeArgument is undefined, in this case also equivalent to [undefined, undefined, ...] + // The importance of excludeArgument is to prevent us from typing function expression parameters + // in arguments too early. If possible, we'd like to only type them once we know the correct + // overload. However, this matters for the case where the call is correct. When the call is + // an error, we don't need to exclude any arguments, although it would cause no harm to do so. + checkApplicableSignature(node, args, candidateForArgumentError, assignableRelation, /*excludeArgument*/ undefined, /*reportErrors*/ true); + } + else if (candidateForTypeArgumentError) { + if (!isTaggedTemplate && !isDecorator && typeArguments) { + var typeArguments_2 = node.typeArguments; + checkTypeArguments(candidateForTypeArgumentError, typeArguments_2, ts.map(typeArguments_2, getTypeFromTypeNodeNoAlias), /*reportErrors*/ true, headMessage); + } else { - error(name_17, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(objectLiteralType), ts.declarationNameToString(name_17)); + ts.Debug.assert(resultOfFailedInference.failedTypeParameterIndex >= 0); + var failedTypeParameter = candidateForTypeArgumentError.typeParameters[resultOfFailedInference.failedTypeParameterIndex]; + var inferenceCandidates = getInferenceCandidates(resultOfFailedInference, resultOfFailedInference.failedTypeParameterIndex); + var diagnosticChainHead = ts.chainDiagnosticMessages(/*details*/ undefined, // details will be provided by call to reportNoCommonSupertypeError + ts.Diagnostics.The_type_argument_for_type_parameter_0_cannot_be_inferred_from_the_usage_Consider_specifying_the_type_arguments_explicitly, typeToString(failedTypeParameter)); + if (headMessage) { + diagnosticChainHead = ts.chainDiagnosticMessages(diagnosticChainHead, headMessage); + } + reportNoCommonSupertypeError(inferenceCandidates, node.expression || node.tag, diagnosticChainHead); } } else { - error(property, ts.Diagnostics.Property_assignment_expected); + reportError(ts.Diagnostics.Supplied_parameters_do_not_match_any_signature_of_call_target); } - } - function checkArrayLiteralAssignment(node, sourceType, contextualMapper) { - // This elementType will be used if the specific property corresponding to this index is not - // present (aka the tuple element property). This call also checks that the parentType is in - // fact an iterable or array (depending on target language). - var elementType = checkIteratedTypeOrElementType(sourceType, node, /*allowStringInput*/ false) || unknownType; - var elements = node.elements; - for (var i = 0; i < elements.length; i++) { - checkArrayLiteralDestructuringElementAssignment(node, sourceType, i, elementType, contextualMapper); + // No signature was applicable. We have already reported the errors for the invalid signature. + // If this is a type resolution session, e.g. Language Service, try to get better information that anySignature. + // Pick the first candidate that matches the arity. This way we can get a contextual type for cases like: + // declare function f(a: { xa: number; xb: number; }); + // f({ | + if (!produceDiagnostics) { + for (var _i = 0, candidates_1 = candidates; _i < candidates_1.length; _i++) { + var candidate = candidates_1[_i]; + if (hasCorrectArity(node, args, candidate)) { + if (candidate.typeParameters && typeArguments) { + candidate = getSignatureInstantiation(candidate, ts.map(typeArguments, getTypeFromTypeNodeNoAlias)); + } + return candidate; + } + } } - return sourceType; - } - function checkArrayLiteralDestructuringElementAssignment(node, sourceType, elementIndex, elementType, contextualMapper) { - var elements = node.elements; - var element = elements[elementIndex]; - if (element.kind !== 193 /* OmittedExpression */) { - if (element.kind !== 191 /* SpreadElementExpression */) { - var propName = "" + elementIndex; - var type = isTypeAny(sourceType) - ? sourceType - : isTupleLikeType(sourceType) - ? getTypeOfPropertyOfType(sourceType, propName) - : elementType; - if (type) { - return checkDestructuringAssignment(element, type, contextualMapper); + return resolveErrorCall(node); + function reportError(message, arg0, arg1, arg2) { + var errorInfo; + errorInfo = ts.chainDiagnosticMessages(errorInfo, message, arg0, arg1, arg2); + if (headMessage) { + errorInfo = ts.chainDiagnosticMessages(errorInfo, headMessage); + } + diagnostics.add(ts.createDiagnosticForNodeFromMessageChain(node, errorInfo)); + } + function chooseOverload(candidates, relation, signatureHelpTrailingComma) { + if (signatureHelpTrailingComma === void 0) { signatureHelpTrailingComma = false; } + for (var _i = 0, candidates_2 = candidates; _i < candidates_2.length; _i++) { + var originalCandidate = candidates_2[_i]; + if (!hasCorrectArity(node, args, originalCandidate, signatureHelpTrailingComma)) { + continue; } - else { - // We still need to check element expression here because we may need to set appropriate flag on the expression - // such as NodeCheckFlags.LexicalThis on "this"expression. - checkExpression(element); - if (isTupleType(sourceType)) { - error(element, ts.Diagnostics.Tuple_type_0_with_length_1_cannot_be_assigned_to_tuple_with_length_2, typeToString(sourceType), getTypeReferenceArity(sourceType), elements.length); + var candidate = void 0; + var typeArgumentsAreValid = void 0; + var inferenceContext = originalCandidate.typeParameters + ? createInferenceContext(originalCandidate, /*inferUnionTypes*/ false) + : undefined; + while (true) { + candidate = originalCandidate; + if (candidate.typeParameters) { + var typeArgumentTypes = void 0; + if (typeArguments) { + typeArgumentTypes = ts.map(typeArguments, getTypeFromTypeNodeNoAlias); + typeArgumentsAreValid = checkTypeArguments(candidate, typeArguments, typeArgumentTypes, /*reportErrors*/ false); + } + else { + inferTypeArguments(node, candidate, args, excludeArgument, inferenceContext); + typeArgumentsAreValid = inferenceContext.failedTypeParameterIndex === undefined; + typeArgumentTypes = inferenceContext.inferredTypes; + } + if (!typeArgumentsAreValid) { + break; + } + candidate = getSignatureInstantiation(candidate, typeArgumentTypes); } - else { - error(element, ts.Diagnostics.Type_0_has_no_property_1, typeToString(sourceType), propName); + if (!checkApplicableSignature(node, args, candidate, relation, excludeArgument, /*reportErrors*/ false)) { + break; } + var index = excludeArgument ? ts.indexOf(excludeArgument, true) : -1; + if (index < 0) { + return candidate; + } + excludeArgument[index] = false; } - } - else { - if (elementIndex < elements.length - 1) { - error(element, ts.Diagnostics.A_rest_element_must_be_last_in_an_array_destructuring_pattern); - } - else { - var restExpression = element.expression; - if (restExpression.kind === 187 /* BinaryExpression */ && restExpression.operatorToken.kind === 56 /* EqualsToken */) { - error(restExpression.operatorToken, ts.Diagnostics.A_rest_element_cannot_have_an_initializer); + // A post-mortem of this iteration of the loop. The signature was not applicable, + // so we want to track it as a candidate for reporting an error. If the candidate + // had no type parameters, or had no issues related to type arguments, we can + // report an error based on the arguments. If there was an issue with type + // arguments, then we can only report an error based on the type arguments. + if (originalCandidate.typeParameters) { + var instantiatedCandidate = candidate; + if (typeArgumentsAreValid) { + candidateForArgumentError = instantiatedCandidate; } else { - return checkDestructuringAssignment(restExpression, createArrayType(elementType), contextualMapper); + candidateForTypeArgumentError = originalCandidate; + if (!typeArguments) { + resultOfFailedInference = inferenceContext; + } } } + else { + ts.Debug.assert(originalCandidate === candidate); + candidateForArgumentError = originalCandidate; + } } + return undefined; } - return undefined; } - function checkDestructuringAssignment(exprOrAssignment, sourceType, contextualMapper) { - var target; - if (exprOrAssignment.kind === 254 /* ShorthandPropertyAssignment */) { - var prop = exprOrAssignment; - if (prop.objectAssignmentInitializer) { - // In strict null checking mode, if a default value of a non-undefined type is specified, remove - // undefined from the final type. - if (strictNullChecks && - !(getFalsyFlags(checkExpression(prop.objectAssignmentInitializer)) & 2048 /* Undefined */)) { - sourceType = getTypeWithFacts(sourceType, 131072 /* NEUndefined */); + function resolveCallExpression(node, candidatesOutArray) { + if (node.expression.kind === 95 /* SuperKeyword */) { + var superType = checkSuperExpression(node.expression); + if (superType !== unknownType) { + // In super call, the candidate signatures are the matching arity signatures of the base constructor function instantiated + // with the type arguments specified in the extends clause. + var baseTypeNode = ts.getClassExtendsHeritageClauseElement(ts.getContainingClass(node)); + if (baseTypeNode) { + var baseConstructors = getInstantiatedConstructorsForTypeArguments(superType, baseTypeNode.typeArguments); + return resolveCall(node, baseConstructors, candidatesOutArray); } - checkBinaryLikeExpression(prop.name, prop.equalsToken, prop.objectAssignmentInitializer, contextualMapper); } - target = exprOrAssignment.name; - } - else { - target = exprOrAssignment; + return resolveUntypedCall(node); } - if (target.kind === 187 /* BinaryExpression */ && target.operatorToken.kind === 56 /* EqualsToken */) { - checkBinaryExpression(target, contextualMapper); - target = target.left; + var funcType = checkNonNullExpression(node.expression); + if (funcType === silentNeverType) { + return silentNeverSignature; } - if (target.kind === 171 /* ObjectLiteralExpression */) { - return checkObjectLiteralAssignment(target, sourceType, contextualMapper); + var apparentType = getApparentType(funcType); + if (apparentType === unknownType) { + // Another error has already been reported + return resolveErrorCall(node); } - if (target.kind === 170 /* ArrayLiteralExpression */) { - return checkArrayLiteralAssignment(target, sourceType, contextualMapper); + // Technically, this signatures list may be incomplete. We are taking the apparent type, + // but we are not including call signatures that may have been added to the Object or + // Function interface, since they have none by default. This is a bit of a leap of faith + // that the user will not add any. + var callSignatures = getSignaturesOfType(apparentType, 0 /* Call */); + var constructSignatures = getSignaturesOfType(apparentType, 1 /* Construct */); + // TS 1.0 Spec: 4.12 + // In an untyped function call no TypeArgs are permitted, Args can be any argument list, no contextual + // types are provided for the argument expressions, and the result is always of type Any. + if (isUntypedFunctionCall(funcType, apparentType, callSignatures.length, constructSignatures.length)) { + // The unknownType indicates that an error already occurred (and was reported). No + // need to report another error in this case. + if (funcType !== unknownType && node.typeArguments) { + error(node, ts.Diagnostics.Untyped_function_calls_may_not_accept_type_arguments); + } + return resolveUntypedCall(node); } - return checkReferenceAssignment(target, sourceType, contextualMapper); - } - function checkReferenceAssignment(target, sourceType, contextualMapper) { - var targetType = checkExpression(target, contextualMapper); - if (checkReferenceExpression(target, ts.Diagnostics.Invalid_left_hand_side_of_assignment_expression, ts.Diagnostics.Left_hand_side_of_assignment_expression_cannot_be_a_constant_or_a_read_only_property)) { - checkTypeAssignableTo(sourceType, targetType, target, /*headMessage*/ undefined); + // If FuncExpr's apparent type(section 3.8.1) is a function type, the call is a typed function call. + // TypeScript employs overload resolution in typed function calls in order to support functions + // with multiple call signatures. + if (!callSignatures.length) { + if (constructSignatures.length) { + error(node, ts.Diagnostics.Value_of_type_0_is_not_callable_Did_you_mean_to_include_new, typeToString(funcType)); + } + else { + error(node, ts.Diagnostics.Cannot_invoke_an_expression_whose_type_lacks_a_call_signature_Type_0_has_no_compatible_call_signatures, typeToString(apparentType)); + } + return resolveErrorCall(node); } - return sourceType; + return resolveCall(node, callSignatures, candidatesOutArray); } /** - * This is a *shallow* check: An expression is side-effect-free if the - * evaluation of the expression *itself* cannot produce side effects. - * For example, x++ / 3 is side-effect free because the / operator - * does not have side effects. - * The intent is to "smell test" an expression for correctness in positions where - * its value is discarded (e.g. the left side of the comma operator). + * TS 1.0 spec: 4.12 + * If FuncExpr is of type Any, or of an object type that has no call or construct signatures + * but is a subtype of the Function interface, the call is an untyped function call. */ - function isSideEffectFree(node) { - node = ts.skipParentheses(node); - switch (node.kind) { - case 69 /* Identifier */: - case 9 /* StringLiteral */: - case 10 /* RegularExpressionLiteral */: - case 176 /* TaggedTemplateExpression */: - case 189 /* TemplateExpression */: - case 11 /* NoSubstitutionTemplateLiteral */: - case 8 /* NumericLiteral */: - case 99 /* TrueKeyword */: - case 84 /* FalseKeyword */: - case 93 /* NullKeyword */: - case 135 /* UndefinedKeyword */: - case 179 /* FunctionExpression */: - case 192 /* ClassExpression */: - case 180 /* ArrowFunction */: - case 170 /* ArrayLiteralExpression */: - case 171 /* ObjectLiteralExpression */: - case 182 /* TypeOfExpression */: - case 196 /* NonNullExpression */: - case 242 /* JsxSelfClosingElement */: - case 241 /* JsxElement */: - return true; - case 188 /* ConditionalExpression */: - return isSideEffectFree(node.whenTrue) && - isSideEffectFree(node.whenFalse); - case 187 /* BinaryExpression */: - if (ts.isAssignmentOperator(node.operatorToken.kind)) { - return false; - } - return isSideEffectFree(node.left) && - isSideEffectFree(node.right); - case 185 /* PrefixUnaryExpression */: - case 186 /* PostfixUnaryExpression */: - // Unary operators ~, !, +, and - have no side effects. - // The rest do. - switch (node.operator) { - case 49 /* ExclamationToken */: - case 35 /* PlusToken */: - case 36 /* MinusToken */: - case 50 /* TildeToken */: - return true; - } - return false; - // Some forms listed here for clarity - case 183 /* VoidExpression */: // Explicit opt-out - case 177 /* TypeAssertionExpression */: // Not SEF, but can produce useful type warnings - case 195 /* AsExpression */: // Not SEF, but can produce useful type warnings - default: + function isUntypedFunctionCall(funcType, apparentFuncType, numCallSignatures, numConstructSignatures) { + if (isTypeAny(funcType)) { + return true; + } + if (isTypeAny(apparentFuncType) && funcType.flags & 16384 /* TypeParameter */) { + return true; + } + if (!numCallSignatures && !numConstructSignatures) { + // We exclude union types because we may have a union of function types that happen to have + // no common signatures. + if (funcType.flags & 524288 /* Union */) { return false; + } + return isTypeAssignableTo(funcType, globalFunctionType); } + return false; } - function isTypeEqualityComparableTo(source, target) { - return (target.flags & 6144 /* Nullable */) !== 0 || isTypeComparableTo(source, target); - } - function getBestChoiceType(type1, type2) { - var firstAssignableToSecond = isTypeAssignableTo(type1, type2); - var secondAssignableToFirst = isTypeAssignableTo(type2, type1); - return secondAssignableToFirst && !firstAssignableToSecond ? type1 : - firstAssignableToSecond && !secondAssignableToFirst ? type2 : - getUnionType([type1, type2], /*subtypeReduction*/ true); - } - function checkBinaryExpression(node, contextualMapper) { - return checkBinaryLikeExpression(node.left, node.operatorToken, node.right, contextualMapper, node); - } - function checkBinaryLikeExpression(left, operatorToken, right, contextualMapper, errorNode) { - var operator = operatorToken.kind; - if (operator === 56 /* EqualsToken */ && (left.kind === 171 /* ObjectLiteralExpression */ || left.kind === 170 /* ArrayLiteralExpression */)) { - return checkDestructuringAssignment(left, checkExpression(right, contextualMapper), contextualMapper); + function resolveNewExpression(node, candidatesOutArray) { + if (node.arguments && languageVersion < 1 /* ES5 */) { + var spreadIndex = getSpreadArgumentIndex(node.arguments); + if (spreadIndex >= 0) { + error(node.arguments[spreadIndex], ts.Diagnostics.Spread_operator_in_new_expressions_is_only_available_when_targeting_ECMAScript_5_and_higher); + } } - var leftType = checkExpression(left, contextualMapper); - var rightType = checkExpression(right, contextualMapper); - switch (operator) { - case 37 /* AsteriskToken */: - case 38 /* AsteriskAsteriskToken */: - case 59 /* AsteriskEqualsToken */: - case 60 /* AsteriskAsteriskEqualsToken */: - case 39 /* SlashToken */: - case 61 /* SlashEqualsToken */: - case 40 /* PercentToken */: - case 62 /* PercentEqualsToken */: - case 36 /* MinusToken */: - case 58 /* MinusEqualsToken */: - case 43 /* LessThanLessThanToken */: - case 63 /* LessThanLessThanEqualsToken */: - case 44 /* GreaterThanGreaterThanToken */: - case 64 /* GreaterThanGreaterThanEqualsToken */: - case 45 /* GreaterThanGreaterThanGreaterThanToken */: - case 65 /* GreaterThanGreaterThanGreaterThanEqualsToken */: - case 47 /* BarToken */: - case 67 /* BarEqualsToken */: - case 48 /* CaretToken */: - case 68 /* CaretEqualsToken */: - case 46 /* AmpersandToken */: - case 66 /* AmpersandEqualsToken */: - if (leftType === silentNeverType || rightType === silentNeverType) { - return silentNeverType; - } - // TypeScript 1.0 spec (April 2014): 4.19.1 - // These operators require their operands to be of type Any, the Number primitive type, - // or an enum type. Operands of an enum type are treated - // as having the primitive type Number. If one operand is the null or undefined value, - // it is treated as having the type of the other operand. - // The result is always of the Number primitive type. - if (leftType.flags & 6144 /* Nullable */) - leftType = rightType; - if (rightType.flags & 6144 /* Nullable */) - rightType = leftType; - leftType = getNonNullableType(leftType); - rightType = getNonNullableType(rightType); - var suggestedOperator = void 0; - // if a user tries to apply a bitwise operator to 2 boolean operands - // try and return them a helpful suggestion - if ((leftType.flags & 136 /* BooleanLike */) && - (rightType.flags & 136 /* BooleanLike */) && - (suggestedOperator = getSuggestedBooleanOperator(operatorToken.kind)) !== undefined) { - error(errorNode || operatorToken, ts.Diagnostics.The_0_operator_is_not_allowed_for_boolean_types_Consider_using_1_instead, ts.tokenToString(operatorToken.kind), ts.tokenToString(suggestedOperator)); - } - else { - // otherwise just check each operand separately and report errors as normal - var leftOk = checkArithmeticOperandType(left, leftType, ts.Diagnostics.The_left_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_or_an_enum_type); - var rightOk = checkArithmeticOperandType(right, rightType, ts.Diagnostics.The_right_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_or_an_enum_type); - if (leftOk && rightOk) { - checkAssignmentOperator(numberType); - } - } - return numberType; - case 35 /* PlusToken */: - case 57 /* PlusEqualsToken */: - if (leftType === silentNeverType || rightType === silentNeverType) { - return silentNeverType; - } - // TypeScript 1.0 spec (April 2014): 4.19.2 - // The binary + operator requires both operands to be of the Number primitive type or an enum type, - // or at least one of the operands to be of type Any or the String primitive type. - // If one operand is the null or undefined value, it is treated as having the type of the other operand. - if (leftType.flags & 6144 /* Nullable */) - leftType = rightType; - if (rightType.flags & 6144 /* Nullable */) - rightType = leftType; - leftType = getNonNullableType(leftType); - rightType = getNonNullableType(rightType); - var resultType = void 0; - if (isTypeOfKind(leftType, 340 /* NumberLike */) && isTypeOfKind(rightType, 340 /* NumberLike */)) { - // Operands of an enum type are treated as having the primitive type Number. - // If both operands are of the Number primitive type, the result is of the Number primitive type. - resultType = numberType; - } - else { - if (isTypeOfKind(leftType, 34 /* StringLike */) || isTypeOfKind(rightType, 34 /* StringLike */)) { - // If one or both operands are of the String primitive type, the result is of the String primitive type. - resultType = stringType; - } - else if (isTypeAny(leftType) || isTypeAny(rightType)) { - // Otherwise, the result is of type Any. - // NOTE: unknown type here denotes error type. Old compiler treated this case as any type so do we. - resultType = leftType === unknownType || rightType === unknownType ? unknownType : anyType; - } - // Symbols are not allowed at all in arithmetic expressions - if (resultType && !checkForDisallowedESSymbolOperand(operator)) { - return resultType; - } - } - if (!resultType) { - reportOperatorError(); - return anyType; - } - if (operator === 57 /* PlusEqualsToken */) { - checkAssignmentOperator(resultType); - } - return resultType; - case 25 /* LessThanToken */: - case 27 /* GreaterThanToken */: - case 28 /* LessThanEqualsToken */: - case 29 /* GreaterThanEqualsToken */: - if (checkForDisallowedESSymbolOperand(operator)) { - if (!isTypeComparableTo(leftType, rightType) && !isTypeComparableTo(rightType, leftType)) { - reportOperatorError(); - } - } - return booleanType; - case 30 /* EqualsEqualsToken */: - case 31 /* ExclamationEqualsToken */: - case 32 /* EqualsEqualsEqualsToken */: - case 33 /* ExclamationEqualsEqualsToken */: - var leftIsLiteral = isLiteralType(leftType); - var rightIsLiteral = isLiteralType(rightType); - if (!leftIsLiteral || !rightIsLiteral) { - leftType = leftIsLiteral ? getBaseTypeOfLiteralType(leftType) : leftType; - rightType = rightIsLiteral ? getBaseTypeOfLiteralType(rightType) : rightType; - } - if (!isTypeEqualityComparableTo(leftType, rightType) && !isTypeEqualityComparableTo(rightType, leftType)) { - reportOperatorError(); - } - return booleanType; - case 91 /* InstanceOfKeyword */: - return checkInstanceOfExpression(left, right, leftType, rightType); - case 90 /* InKeyword */: - return checkInExpression(left, right, leftType, rightType); - case 51 /* AmpersandAmpersandToken */: - return getTypeFacts(leftType) & 1048576 /* Truthy */ ? - includeFalsyTypes(rightType, getFalsyFlags(strictNullChecks ? leftType : getBaseTypeOfLiteralType(rightType))) : - leftType; - case 52 /* BarBarToken */: - return getTypeFacts(leftType) & 2097152 /* Falsy */ ? - getBestChoiceType(removeDefinitelyFalsyTypes(leftType), rightType) : - leftType; - case 56 /* EqualsToken */: - checkAssignmentOperator(rightType); - return getRegularTypeOfObjectLiteral(rightType); - case 24 /* CommaToken */: - if (!compilerOptions.allowUnreachableCode && isSideEffectFree(left)) { - error(left, ts.Diagnostics.Left_side_of_comma_operator_is_unused_and_has_no_side_effects); - } - return rightType; + var expressionType = checkNonNullExpression(node.expression); + if (expressionType === silentNeverType) { + return silentNeverSignature; } - // Return true if there was no error, false if there was an error. - function checkForDisallowedESSymbolOperand(operator) { - var offendingSymbolOperand = maybeTypeOfKind(leftType, 512 /* ESSymbol */) ? left : - maybeTypeOfKind(rightType, 512 /* ESSymbol */) ? right : - undefined; - if (offendingSymbolOperand) { - error(offendingSymbolOperand, ts.Diagnostics.The_0_operator_cannot_be_applied_to_type_symbol, ts.tokenToString(operator)); - return false; - } - return true; + // If expressionType's apparent type(section 3.8.1) is an object type with one or + // more construct signatures, the expression is processed in the same manner as a + // function call, but using the construct signatures as the initial set of candidate + // signatures for overload resolution. The result type of the function call becomes + // the result type of the operation. + expressionType = getApparentType(expressionType); + if (expressionType === unknownType) { + // Another error has already been reported + return resolveErrorCall(node); } - function getSuggestedBooleanOperator(operator) { - switch (operator) { - case 47 /* BarToken */: - case 67 /* BarEqualsToken */: - return 52 /* BarBarToken */; - case 48 /* CaretToken */: - case 68 /* CaretEqualsToken */: - return 33 /* ExclamationEqualsEqualsToken */; - case 46 /* AmpersandToken */: - case 66 /* AmpersandEqualsToken */: - return 51 /* AmpersandAmpersandToken */; - default: - return undefined; - } + // If the expression is a class of abstract type, then it cannot be instantiated. + // Note, only class declarations can be declared abstract. + // In the case of a merged class-module or class-interface declaration, + // only the class declaration node will have the Abstract flag set. + var valueDecl = expressionType.symbol && getClassLikeDeclarationOfSymbol(expressionType.symbol); + if (valueDecl && ts.getModifierFlags(valueDecl) & 128 /* Abstract */) { + error(node, ts.Diagnostics.Cannot_create_an_instance_of_the_abstract_class_0, ts.declarationNameToString(valueDecl.name)); + return resolveErrorCall(node); } - function checkAssignmentOperator(valueType) { - if (produceDiagnostics && operator >= 56 /* FirstAssignment */ && operator <= 68 /* LastAssignment */) { - // TypeScript 1.0 spec (April 2014): 4.17 - // An assignment of the form - // VarExpr = ValueExpr - // requires VarExpr to be classified as a reference - // A compound assignment furthermore requires VarExpr to be classified as a reference (section 4.1) - // and the type of the non - compound operation to be assignable to the type of VarExpr. - var ok = checkReferenceExpression(left, ts.Diagnostics.Invalid_left_hand_side_of_assignment_expression, ts.Diagnostics.Left_hand_side_of_assignment_expression_cannot_be_a_constant_or_a_read_only_property); - // Use default messages - if (ok) { - // to avoid cascading errors check assignability only if 'isReference' check succeeded and no errors were reported - checkTypeAssignableTo(valueType, leftType, left, /*headMessage*/ undefined); - } + // TS 1.0 spec: 4.11 + // If expressionType is of type Any, Args can be any argument + // list and the result of the operation is of type Any. + if (isTypeAny(expressionType)) { + if (node.typeArguments) { + error(node, ts.Diagnostics.Untyped_function_calls_may_not_accept_type_arguments); } + return resolveUntypedCall(node); } - function reportOperatorError() { - error(errorNode || operatorToken, ts.Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2, ts.tokenToString(operatorToken.kind), typeToString(leftType), typeToString(rightType)); + // Technically, this signatures list may be incomplete. We are taking the apparent type, + // but we are not including construct signatures that may have been added to the Object or + // Function interface, since they have none by default. This is a bit of a leap of faith + // that the user will not add any. + var constructSignatures = getSignaturesOfType(expressionType, 1 /* Construct */); + if (constructSignatures.length) { + if (!isConstructorAccessible(node, constructSignatures[0])) { + return resolveErrorCall(node); + } + return resolveCall(node, constructSignatures, candidatesOutArray); } - } - function isYieldExpressionInClass(node) { - var current = node; - var parent = node.parent; - while (parent) { - if (ts.isFunctionLike(parent) && current === parent.body) { - return false; + // If expressionType's apparent type is an object type with no construct signatures but + // one or more call signatures, the expression is processed as a function call. A compile-time + // error occurs if the result of the function call is not Void. The type of the result of the + // operation is Any. It is an error to have a Void this type. + var callSignatures = getSignaturesOfType(expressionType, 0 /* Call */); + if (callSignatures.length) { + var signature = resolveCall(node, callSignatures, candidatesOutArray); + if (getReturnTypeOfSignature(signature) !== voidType) { + error(node, ts.Diagnostics.Only_a_void_function_can_be_called_with_the_new_keyword); } - else if (ts.isClassLike(current)) { - return true; + if (getThisTypeOfSignature(signature) === voidType) { + error(node, ts.Diagnostics.A_function_that_is_called_with_the_new_keyword_cannot_have_a_this_type_that_is_void); } - current = parent; - parent = parent.parent; + return signature; } - return false; + error(node, ts.Diagnostics.Cannot_use_new_with_an_expression_whose_type_lacks_a_call_or_construct_signature); + return resolveErrorCall(node); } - function checkYieldExpression(node) { - // Grammar checking - if (produceDiagnostics) { - if (!(node.flags & 65536 /* YieldContext */) || isYieldExpressionInClass(node)) { - grammarErrorOnFirstToken(node, ts.Diagnostics.A_yield_expression_is_only_allowed_in_a_generator_body); - } - if (isInParameterInitializerBeforeContainingFunction(node)) { - error(node, ts.Diagnostics.yield_expressions_cannot_be_used_in_a_parameter_initializer); - } + function isConstructorAccessible(node, signature) { + if (!signature || !signature.declaration) { + return true; } - if (node.expression) { - var func = ts.getContainingFunction(node); - // If the user's code is syntactically correct, the func should always have a star. After all, - // we are in a yield context. - if (func && func.asteriskToken) { - var expressionType = checkExpressionCached(node.expression, /*contextualMapper*/ undefined); - var expressionElementType = void 0; - var nodeIsYieldStar = !!node.asteriskToken; - if (nodeIsYieldStar) { - expressionElementType = checkElementTypeOfIterable(expressionType, node.expression); - } - // There is no point in doing an assignability check if the function - // has no explicit return type because the return type is directly computed - // from the yield expressions. - if (func.type) { - var signatureElementType = getElementTypeOfIterableIterator(getTypeFromTypeNode(func.type)) || anyType; - if (nodeIsYieldStar) { - checkTypeAssignableTo(expressionElementType, signatureElementType, node.expression, /*headMessage*/ undefined); - } - else { - checkTypeAssignableTo(expressionType, signatureElementType, node.expression, /*headMessage*/ undefined); + var declaration = signature.declaration; + var modifiers = ts.getModifierFlags(declaration); + // Public constructor is accessible. + if (!(modifiers & 24 /* NonPublicAccessibilityModifier */)) { + return true; + } + var declaringClassDeclaration = getClassLikeDeclarationOfSymbol(declaration.parent.symbol); + var declaringClass = getDeclaredTypeOfSymbol(declaration.parent.symbol); + // A private or protected constructor can only be instantiated within its own class (or a subclass, for protected) + if (!isNodeWithinClass(node, declaringClassDeclaration)) { + var containingClass = ts.getContainingClass(node); + if (containingClass) { + var containingType = getTypeOfNode(containingClass); + var baseTypes = getBaseTypes(containingType); + if (baseTypes.length) { + var baseType = baseTypes[0]; + if (modifiers & 16 /* Protected */ && + baseType.symbol === declaration.parent.symbol) { + return true; } } } + if (modifiers & 8 /* Private */) { + error(node, ts.Diagnostics.Constructor_of_class_0_is_private_and_only_accessible_within_the_class_declaration, typeToString(declaringClass)); + } + if (modifiers & 16 /* Protected */) { + error(node, ts.Diagnostics.Constructor_of_class_0_is_protected_and_only_accessible_within_the_class_declaration, typeToString(declaringClass)); + } + return false; } - // Both yield and yield* expressions have type 'any' - return anyType; - } - function checkConditionalExpression(node, contextualMapper) { - checkExpression(node.condition); - var type1 = checkExpression(node.whenTrue, contextualMapper); - var type2 = checkExpression(node.whenFalse, contextualMapper); - return getBestChoiceType(type1, type2); + return true; } - function checkLiteralExpression(node) { - if (node.kind === 8 /* NumericLiteral */) { - checkGrammarNumericLiteral(node); + function resolveTaggedTemplateExpression(node, candidatesOutArray) { + var tagType = checkExpression(node.tag); + var apparentType = getApparentType(tagType); + if (apparentType === unknownType) { + // Another error has already been reported + return resolveErrorCall(node); } - switch (node.kind) { - case 9 /* StringLiteral */: - return getFreshTypeOfLiteralType(getLiteralTypeForText(32 /* StringLiteral */, node.text)); - case 8 /* NumericLiteral */: - return getFreshTypeOfLiteralType(getLiteralTypeForText(64 /* NumberLiteral */, node.text)); - case 99 /* TrueKeyword */: - return trueType; - case 84 /* FalseKeyword */: - return falseType; + var callSignatures = getSignaturesOfType(apparentType, 0 /* Call */); + var constructSignatures = getSignaturesOfType(apparentType, 1 /* Construct */); + if (isUntypedFunctionCall(tagType, apparentType, callSignatures.length, constructSignatures.length)) { + return resolveUntypedCall(node); + } + if (!callSignatures.length) { + error(node, ts.Diagnostics.Cannot_invoke_an_expression_whose_type_lacks_a_call_signature_Type_0_has_no_compatible_call_signatures, typeToString(apparentType)); + return resolveErrorCall(node); } + return resolveCall(node, callSignatures, candidatesOutArray); } - function checkTemplateExpression(node) { - // We just want to check each expressions, but we are unconcerned with - // the type of each expression, as any value may be coerced into a string. - // It is worth asking whether this is what we really want though. - // A place where we actually *are* concerned with the expressions' types are - // in tagged templates. - ts.forEach(node.templateSpans, function (templateSpan) { - checkExpression(templateSpan.expression); - }); - return stringType; + /** + * Gets the localized diagnostic head message to use for errors when resolving a decorator as a call expression. + */ + function getDiagnosticHeadMessageForDecoratorResolution(node) { + switch (node.parent.kind) { + case 221 /* ClassDeclaration */: + case 192 /* ClassExpression */: + return ts.Diagnostics.Unable_to_resolve_signature_of_class_decorator_when_called_as_an_expression; + case 142 /* Parameter */: + return ts.Diagnostics.Unable_to_resolve_signature_of_parameter_decorator_when_called_as_an_expression; + case 145 /* PropertyDeclaration */: + return ts.Diagnostics.Unable_to_resolve_signature_of_property_decorator_when_called_as_an_expression; + case 147 /* MethodDeclaration */: + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + return ts.Diagnostics.Unable_to_resolve_signature_of_method_decorator_when_called_as_an_expression; + } } - function checkExpressionWithContextualType(node, contextualType, contextualMapper) { - var saveContextualType = node.contextualType; - node.contextualType = contextualType; - var result = checkExpression(node, contextualMapper); - node.contextualType = saveContextualType; - return result; + /** + * Resolves a decorator as if it were a call expression. + */ + function resolveDecorator(node, candidatesOutArray) { + var funcType = checkExpression(node.expression); + var apparentType = getApparentType(funcType); + if (apparentType === unknownType) { + return resolveErrorCall(node); + } + var callSignatures = getSignaturesOfType(apparentType, 0 /* Call */); + var constructSignatures = getSignaturesOfType(apparentType, 1 /* Construct */); + if (isUntypedFunctionCall(funcType, apparentType, callSignatures.length, constructSignatures.length)) { + return resolveUntypedCall(node); + } + var headMessage = getDiagnosticHeadMessageForDecoratorResolution(node); + if (!callSignatures.length) { + var errorInfo = void 0; + errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Cannot_invoke_an_expression_whose_type_lacks_a_call_signature_Type_0_has_no_compatible_call_signatures, typeToString(apparentType)); + errorInfo = ts.chainDiagnosticMessages(errorInfo, headMessage); + diagnostics.add(ts.createDiagnosticForNodeFromMessageChain(node, errorInfo)); + return resolveErrorCall(node); + } + return resolveCall(node, callSignatures, candidatesOutArray, headMessage); } - function checkExpressionCached(node, contextualMapper) { + function resolveSignature(node, candidatesOutArray) { + switch (node.kind) { + case 174 /* CallExpression */: + return resolveCallExpression(node, candidatesOutArray); + case 175 /* NewExpression */: + return resolveNewExpression(node, candidatesOutArray); + case 176 /* TaggedTemplateExpression */: + return resolveTaggedTemplateExpression(node, candidatesOutArray); + case 143 /* Decorator */: + return resolveDecorator(node, candidatesOutArray); + } + ts.Debug.fail("Branch in 'resolveSignature' should be unreachable."); + } + // candidatesOutArray is passed by signature help in the language service, and collectCandidates + // must fill it up with the appropriate candidate signatures + function getResolvedSignature(node, candidatesOutArray) { var links = getNodeLinks(node); - if (!links.resolvedType) { - // When computing a type that we're going to cache, we need to ignore any ongoing control flow - // analysis because variables may have transient types in indeterminable states. Moving flowLoopStart - // to the top of the stack ensures all transient types are computed from a known point. - var saveFlowLoopStart = flowLoopStart; - flowLoopStart = flowLoopCount; - links.resolvedType = checkExpression(node, contextualMapper); - flowLoopStart = saveFlowLoopStart; + // If getResolvedSignature has already been called, we will have cached the resolvedSignature. + // However, it is possible that either candidatesOutArray was not passed in the first time, + // or that a different candidatesOutArray was passed in. Therefore, we need to redo the work + // to correctly fill the candidatesOutArray. + var cached = links.resolvedSignature; + if (cached && cached !== resolvingSignature && !candidatesOutArray) { + return cached; } - return links.resolvedType; + links.resolvedSignature = resolvingSignature; + var result = resolveSignature(node, candidatesOutArray); + // If signature resolution originated in control flow type analysis (for example to compute the + // assigned type in a flow assignment) we don't cache the result as it may be based on temporary + // types from the control flow analysis. + links.resolvedSignature = flowLoopStart === flowLoopCount ? result : cached; + return result; } - function isTypeAssertion(node) { - node = skipParenthesizedNodes(node); - return node.kind === 177 /* TypeAssertionExpression */ || node.kind === 195 /* AsExpression */; + function getResolvedOrAnySignature(node) { + // If we're already in the process of resolving the given signature, don't resolve again as + // that could cause infinite recursion. Instead, return anySignature. + return getNodeLinks(node).resolvedSignature === resolvingSignature ? resolvingSignature : getResolvedSignature(node); } - function checkDeclarationInitializer(declaration) { - var type = checkExpressionCached(declaration.initializer); - return ts.getCombinedNodeFlags(declaration) & 2 /* Const */ || - ts.getCombinedModifierFlags(declaration) & 64 /* Readonly */ || - isTypeAssertion(declaration.initializer) ? type : getWidenedLiteralType(type); + function getInferredClassType(symbol) { + var links = getSymbolLinks(symbol); + if (!links.inferredClassType) { + links.inferredClassType = createAnonymousType(symbol, symbol.members, emptyArray, emptyArray, /*stringIndexType*/ undefined, /*numberIndexType*/ undefined); + } + return links.inferredClassType; } - function isLiteralContextualType(contextualType) { - if (contextualType) { - if (contextualType.flags & 16384 /* TypeParameter */) { - var apparentType = getApparentTypeOfTypeParameter(contextualType); - // If the type parameter is constrained to the base primitive type we're checking for, - // consider this a literal context. For example, given a type parameter 'T extends string', - // this causes us to infer string literal types for T. - if (apparentType.flags & (2 /* String */ | 4 /* Number */ | 8 /* Boolean */ | 16 /* Enum */)) { - return true; + /** + * Syntactically and semantically checks a call or new expression. + * @param node The call/new expression to be checked. + * @returns On success, the expression's signature's return type. On failure, anyType. + */ + function checkCallExpression(node) { + // Grammar checking; stop grammar-checking if checkGrammarTypeArguments return true + checkGrammarTypeArguments(node, node.typeArguments) || checkGrammarArguments(node, node.arguments); + var signature = getResolvedSignature(node); + if (node.expression.kind === 95 /* SuperKeyword */) { + return voidType; + } + if (node.kind === 175 /* NewExpression */) { + var declaration = signature.declaration; + if (declaration && + declaration.kind !== 148 /* Constructor */ && + declaration.kind !== 152 /* ConstructSignature */ && + declaration.kind !== 157 /* ConstructorType */ && + !ts.isJSDocConstructSignature(declaration)) { + // When resolved signature is a call signature (and not a construct signature) the result type is any, unless + // the declaring function had members created through 'x.prototype.y = expr' or 'this.y = expr' psuedodeclarations + // in a JS file + // Note:JS inferred classes might come from a variable declaration instead of a function declaration. + // In this case, using getResolvedSymbol directly is required to avoid losing the members from the declaration. + var funcSymbol = node.expression.kind === 69 /* Identifier */ ? + getResolvedSymbol(node.expression) : + checkExpression(node.expression).symbol; + if (funcSymbol && funcSymbol.members && (funcSymbol.flags & 16 /* Function */ || ts.isDeclarationOfFunctionExpression(funcSymbol))) { + return getInferredClassType(funcSymbol); } - contextualType = apparentType; + else if (compilerOptions.noImplicitAny) { + error(node, ts.Diagnostics.new_expression_whose_target_lacks_a_construct_signature_implicitly_has_an_any_type); + } + return anyType; } - return maybeTypeOfKind(contextualType, 480 /* Literal */); } - return false; - } - function checkExpressionForMutableLocation(node, contextualMapper) { - var type = checkExpression(node, contextualMapper); - return isTypeAssertion(node) || isLiteralContextualType(getContextualType(node)) ? type : getWidenedLiteralType(type); - } - function checkPropertyAssignment(node, contextualMapper) { - // Do not use hasDynamicName here, because that returns false for well known symbols. - // We want to perform checkComputedPropertyName for all computed properties, including - // well known symbols. - if (node.name.kind === 140 /* ComputedPropertyName */) { - checkComputedPropertyName(node.name); + // In JavaScript files, calls to any identifier 'require' are treated as external module imports + if (ts.isInJavaScriptFile(node) && ts.isRequireCall(node, /*checkArgumentIsStringLiteral*/ true)) { + return resolveExternalModuleTypeByLiteral(node.arguments[0]); } - return checkExpressionForMutableLocation(node.initializer, contextualMapper); + return getReturnTypeOfSignature(signature); } - function checkObjectLiteralMethod(node, contextualMapper) { - // Grammar checking - checkGrammarMethod(node); - // Do not use hasDynamicName here, because that returns false for well known symbols. - // We want to perform checkComputedPropertyName for all computed properties, including - // well known symbols. - if (node.name.kind === 140 /* ComputedPropertyName */) { - checkComputedPropertyName(node.name); - } - var uninstantiatedType = checkFunctionExpressionOrObjectLiteralMethod(node, contextualMapper); - return instantiateTypeWithSingleGenericCallSignature(node, uninstantiatedType, contextualMapper); + function checkTaggedTemplateExpression(node) { + return getReturnTypeOfSignature(getResolvedSignature(node)); } - function instantiateTypeWithSingleGenericCallSignature(node, type, contextualMapper) { - if (isInferentialContext(contextualMapper)) { - var signature = getSingleCallSignature(type); - if (signature && signature.typeParameters) { - var contextualType = getApparentTypeOfContextualType(node); - if (contextualType) { - var contextualSignature = getSingleCallSignature(contextualType); - if (contextualSignature && !contextualSignature.typeParameters) { - return getOrCreateTypeFromSignature(instantiateSignatureInContextOf(signature, contextualSignature, contextualMapper)); - } - } + function checkAssertion(node) { + var exprType = getRegularTypeOfObjectLiteral(getBaseTypeOfLiteralType(checkExpression(node.expression))); + checkSourceElement(node.type); + var targetType = getTypeFromTypeNode(node.type); + if (produceDiagnostics && targetType !== unknownType) { + var widenedType = getWidenedType(exprType); + if (!isTypeComparableTo(targetType, widenedType)) { + checkTypeComparableTo(exprType, targetType, node, ts.Diagnostics.Type_0_cannot_be_converted_to_type_1); } } - return type; + return targetType; } - // Checks an expression and returns its type. The contextualMapper parameter serves two purposes: When - // contextualMapper is not undefined and not equal to the identityMapper function object it indicates that the - // expression is being inferentially typed (section 4.15.2 in spec) and provides the type mapper to use in - // conjunction with the generic contextual type. When contextualMapper is equal to the identityMapper function - // object, it serves as an indicator that all contained function and arrow expressions should be considered to - // have the wildcard function type; this form of type check is used during overload resolution to exclude - // contextually typed function and arrow expressions in the initial phase. - function checkExpression(node, contextualMapper) { - var type; - if (node.kind === 139 /* QualifiedName */) { - type = checkQualifiedName(node); - } - else { - var uninstantiatedType = checkExpressionWorker(node, contextualMapper); - type = instantiateTypeWithSingleGenericCallSignature(node, uninstantiatedType, contextualMapper); - } - if (isConstEnumObjectType(type)) { - // enum object type for const enums are only permitted in: - // - 'left' in property access - // - 'object' in indexed access - // - target in rhs of import statement - var ok = (node.parent.kind === 172 /* PropertyAccessExpression */ && node.parent.expression === node) || - (node.parent.kind === 173 /* ElementAccessExpression */ && node.parent.expression === node) || - ((node.kind === 69 /* Identifier */ || node.kind === 139 /* QualifiedName */) && isInRightSideOfImportOrExportAssignment(node)); - if (!ok) { - error(node, ts.Diagnostics.const_enums_can_only_be_used_in_property_or_index_access_expressions_or_the_right_hand_side_of_an_import_declaration_or_export_assignment); + function checkNonNullAssertion(node) { + return getNonNullableType(checkExpression(node.expression)); + } + function getTypeOfParameter(symbol) { + var type = getTypeOfSymbol(symbol); + if (strictNullChecks) { + var declaration = symbol.valueDeclaration; + if (declaration && declaration.initializer) { + return includeFalsyTypes(type, 2048 /* Undefined */); } } return type; } - function checkExpressionWorker(node, contextualMapper) { - switch (node.kind) { - case 69 /* Identifier */: - return checkIdentifier(node); - case 97 /* ThisKeyword */: - return checkThisExpression(node); - case 95 /* SuperKeyword */: - return checkSuperExpression(node); - case 93 /* NullKeyword */: - return nullWideningType; - case 9 /* StringLiteral */: - case 8 /* NumericLiteral */: - case 99 /* TrueKeyword */: - case 84 /* FalseKeyword */: - return checkLiteralExpression(node); - case 189 /* TemplateExpression */: - return checkTemplateExpression(node); - case 11 /* NoSubstitutionTemplateLiteral */: - return stringType; - case 10 /* RegularExpressionLiteral */: - return globalRegExpType; - case 170 /* ArrayLiteralExpression */: - return checkArrayLiteral(node, contextualMapper); - case 171 /* ObjectLiteralExpression */: - return checkObjectLiteral(node, contextualMapper); - case 172 /* PropertyAccessExpression */: - return checkPropertyAccessExpression(node); - case 173 /* ElementAccessExpression */: - return checkIndexedAccess(node); - case 174 /* CallExpression */: - case 175 /* NewExpression */: - return checkCallExpression(node); - case 176 /* TaggedTemplateExpression */: - return checkTaggedTemplateExpression(node); - case 178 /* ParenthesizedExpression */: - return checkExpression(node.expression, contextualMapper); - case 192 /* ClassExpression */: - return checkClassExpression(node); - case 179 /* FunctionExpression */: - case 180 /* ArrowFunction */: - return checkFunctionExpressionOrObjectLiteralMethod(node, contextualMapper); - case 182 /* TypeOfExpression */: - return checkTypeOfExpression(node); - case 177 /* TypeAssertionExpression */: - case 195 /* AsExpression */: - return checkAssertion(node); - case 196 /* NonNullExpression */: - return checkNonNullAssertion(node); - case 181 /* DeleteExpression */: - return checkDeleteExpression(node); - case 183 /* VoidExpression */: - return checkVoidExpression(node); - case 184 /* AwaitExpression */: - return checkAwaitExpression(node); - case 185 /* PrefixUnaryExpression */: - return checkPrefixUnaryExpression(node); - case 186 /* PostfixUnaryExpression */: - return checkPostfixUnaryExpression(node); - case 187 /* BinaryExpression */: - return checkBinaryExpression(node, contextualMapper); - case 188 /* ConditionalExpression */: - return checkConditionalExpression(node, contextualMapper); - case 191 /* SpreadElementExpression */: - return checkSpreadElementExpression(node, contextualMapper); - case 193 /* OmittedExpression */: - return undefinedWideningType; - case 190 /* YieldExpression */: - return checkYieldExpression(node); - case 248 /* JsxExpression */: - return checkJsxExpression(node); - case 241 /* JsxElement */: - return checkJsxElement(node); - case 242 /* JsxSelfClosingElement */: - return checkJsxSelfClosingElement(node); - case 243 /* JsxOpeningElement */: - ts.Debug.fail("Shouldn't ever directly check a JsxOpeningElement"); - } - return unknownType; + function getTypeAtPosition(signature, pos) { + return signature.hasRestParameter ? + pos < signature.parameters.length - 1 ? getTypeOfParameter(signature.parameters[pos]) : getRestTypeOfSignature(signature) : + pos < signature.parameters.length ? getTypeOfParameter(signature.parameters[pos]) : anyType; } - // DECLARATION AND STATEMENT TYPE CHECKING - function checkTypeParameter(node) { - // Grammar Checking - if (node.expression) { - grammarErrorOnFirstToken(node.expression, ts.Diagnostics.Type_expected); + function assignContextualParameterTypes(signature, context, mapper) { + var len = signature.parameters.length - (signature.hasRestParameter ? 1 : 0); + if (context.thisParameter) { + if (!signature.thisParameter) { + signature.thisParameter = createTransientSymbol(context.thisParameter, undefined); + } + assignTypeToParameterAndFixTypeParameters(signature.thisParameter, getTypeOfSymbol(context.thisParameter), mapper); } - checkSourceElement(node.constraint); - getConstraintOfTypeParameter(getDeclaredTypeOfTypeParameter(getSymbolOfNode(node))); - if (produceDiagnostics) { - checkTypeNameIsReserved(node.name, ts.Diagnostics.Type_parameter_name_cannot_be_0); + for (var i = 0; i < len; i++) { + var parameter = signature.parameters[i]; + var contextualParameterType = getTypeAtPosition(context, i); + assignTypeToParameterAndFixTypeParameters(parameter, contextualParameterType, mapper); + } + if (signature.hasRestParameter && isRestParameterIndex(context, signature.parameters.length - 1)) { + var parameter = ts.lastOrUndefined(signature.parameters); + var contextualParameterType = getTypeOfSymbol(ts.lastOrUndefined(context.parameters)); + assignTypeToParameterAndFixTypeParameters(parameter, contextualParameterType, mapper); } } - function checkParameter(node) { - // Grammar checking - // It is a SyntaxError if the Identifier "eval" or the Identifier "arguments" occurs as the - // Identifier in a PropertySetParameterList of a PropertyAssignment that is contained in strict code - // or if its FunctionBody is strict code(11.1.5). - // Grammar checking - checkGrammarDecorators(node) || checkGrammarModifiers(node); - checkVariableLikeDeclaration(node); - var func = ts.getContainingFunction(node); - if (ts.getModifierFlags(node) & 92 /* ParameterPropertyModifier */) { - func = ts.getContainingFunction(node); - if (!(func.kind === 148 /* Constructor */ && ts.nodeIsPresent(func.body))) { - error(node, ts.Diagnostics.A_parameter_property_is_only_allowed_in_a_constructor_implementation); + // When contextual typing assigns a type to a parameter that contains a binding pattern, we also need to push + // the destructured type into the contained binding elements. + function assignBindingElementTypes(node) { + if (ts.isBindingPattern(node.name)) { + for (var _i = 0, _a = node.name.elements; _i < _a.length; _i++) { + var element = _a[_i]; + if (!ts.isOmittedExpression(element)) { + if (element.name.kind === 69 /* Identifier */) { + getSymbolLinks(getSymbolOfNode(element)).type = getTypeForBindingElement(element); + } + assignBindingElementTypes(element); + } } } - if (node.questionToken && ts.isBindingPattern(node.name) && func.body) { - error(node, ts.Diagnostics.A_binding_pattern_parameter_cannot_be_optional_in_an_implementation_signature); - } - if (node.name.text === "this") { - if (ts.indexOf(func.parameters, node) !== 0) { - error(node, ts.Diagnostics.A_this_parameter_must_be_the_first_parameter); - } - if (func.kind === 148 /* Constructor */ || func.kind === 152 /* ConstructSignature */ || func.kind === 157 /* ConstructorType */) { - error(node, ts.Diagnostics.A_constructor_cannot_have_a_this_parameter); + } + function assignTypeToParameterAndFixTypeParameters(parameter, contextualType, mapper) { + var links = getSymbolLinks(parameter); + if (!links.type) { + links.type = instantiateType(contextualType, mapper); + // if inference didn't come up with anything but {}, fall back to the binding pattern if present. + if (links.type === emptyObjectType && + (parameter.valueDeclaration.name.kind === 167 /* ObjectBindingPattern */ || + parameter.valueDeclaration.name.kind === 168 /* ArrayBindingPattern */)) { + links.type = getTypeFromBindingPattern(parameter.valueDeclaration.name); } + assignBindingElementTypes(parameter.valueDeclaration); } - // Only check rest parameter type if it's not a binding pattern. Since binding patterns are - // not allowed in a rest parameter, we already have an error from checkGrammarParameterList. - if (node.dotDotDotToken && !ts.isBindingPattern(node.name) && !isArrayType(getTypeOfSymbol(node.symbol))) { - error(node, ts.Diagnostics.A_rest_parameter_must_be_of_an_array_type); + else if (isInferentialContext(mapper)) { + // Even if the parameter already has a type, it might be because it was given a type while + // processing the function as an argument to a prior signature during overload resolution. + // If this was the case, it may have caused some type parameters to be fixed. So here, + // we need to ensure that type parameters at the same positions get fixed again. This is + // done by calling instantiateType to attach the mapper to the contextualType, and then + // calling inferTypes to force a walk of contextualType so that all the correct fixing + // happens. The choice to pass in links.type may seem kind of arbitrary, but it serves + // to make sure that all the correct positions in contextualType are reached by the walk. + // Here is an example: + // + // interface Base { + // baseProp; + // } + // interface Derived extends Base { + // toBase(): Base; + // } + // + // var derived: Derived; + // + // declare function foo(x: T, func: (p: T) => T): T; + // declare function foo(x: T, func: (p: T) => T): T; + // + // var result = foo(derived, d => d.toBase()); + // + // We are typing d while checking the second overload. But we've already given d + // a type (Derived) from the first overload. However, we still want to fix the + // T in the second overload so that we do not infer Base as a candidate for T + // (inferring Base would make type argument inference inconsistent between the two + // overloads). + inferTypes(mapper.context, links.type, instantiateType(contextualType, mapper)); } } - function isSyntacticallyValidGenerator(node) { - if (!node.asteriskToken || !node.body) { - return false; + function getReturnTypeFromJSDocComment(func) { + var returnTag = ts.getJSDocReturnTag(func); + if (returnTag && returnTag.typeExpression) { + return getTypeFromTypeNode(returnTag.typeExpression.type); } - return node.kind === 147 /* MethodDeclaration */ || - node.kind === 220 /* FunctionDeclaration */ || - node.kind === 179 /* FunctionExpression */; + return undefined; } - function getTypePredicateParameterIndex(parameterList, parameter) { - if (parameterList) { - for (var i = 0; i < parameterList.length; i++) { - var param = parameterList[i]; - if (param.name.kind === 69 /* Identifier */ && - param.name.text === parameter.text) { - return i; - } - } + function createPromiseType(promisedType) { + // creates a `Promise` type where `T` is the promisedType argument + var globalPromiseType = getGlobalPromiseType(); + if (globalPromiseType !== emptyGenericType) { + // if the promised type is itself a promise, get the underlying type; otherwise, fallback to the promised type + promisedType = getAwaitedType(promisedType); + return createTypeReference(globalPromiseType, [promisedType]); } - return -1; + return emptyObjectType; } - function checkTypePredicate(node) { - var parent = getTypePredicateParent(node); - if (!parent) { - // The parent must not be valid. - error(node, ts.Diagnostics.A_type_predicate_is_only_allowed_in_return_type_position_for_functions_and_methods); - return; + function createPromiseReturnType(func, promisedType) { + var promiseType = createPromiseType(promisedType); + if (promiseType === emptyObjectType) { + error(func, ts.Diagnostics.An_async_function_or_method_must_have_a_valid_awaitable_return_type); + return unknownType; } - var typePredicate = getSignatureFromDeclaration(parent).typePredicate; - if (!typePredicate) { - return; + return promiseType; + } + function getReturnTypeFromBody(func, contextualMapper) { + var contextualSignature = getContextualSignatureForFunctionLikeDeclaration(func); + if (!func.body) { + return unknownType; } - var parameterName = node.parameterName; - if (ts.isThisTypePredicate(typePredicate)) { - getTypeFromThisTypeNode(parameterName); + var isAsync = ts.isAsyncFunctionLike(func); + var type; + if (func.body.kind !== 199 /* Block */) { + type = checkExpressionCached(func.body, contextualMapper); + if (isAsync) { + // From within an async function you can return either a non-promise value or a promise. Any + // Promise/A+ compatible implementation will always assimilate any foreign promise, so the + // return type of the body should be unwrapped to its awaited type, which we will wrap in + // the native Promise type later in this function. + type = checkAwaitedType(type, func, ts.Diagnostics.Return_expression_in_async_function_does_not_have_a_valid_callable_then_member); + } } else { - if (typePredicate.parameterIndex >= 0) { - if (parent.parameters[typePredicate.parameterIndex].dotDotDotToken) { - error(parameterName, ts.Diagnostics.A_type_predicate_cannot_reference_a_rest_parameter); - } - else { - var leadingError = ts.chainDiagnosticMessages(undefined, ts.Diagnostics.A_type_predicate_s_type_must_be_assignable_to_its_parameter_s_type); - checkTypeAssignableTo(typePredicate.type, getTypeOfNode(parent.parameters[typePredicate.parameterIndex]), node.type, - /*headMessage*/ undefined, leadingError); + var types = void 0; + var funcIsGenerator = !!func.asteriskToken; + if (funcIsGenerator) { + types = checkAndAggregateYieldOperandTypes(func, contextualMapper); + if (types.length === 0) { + var iterableIteratorAny = createIterableIteratorType(anyType); + if (compilerOptions.noImplicitAny) { + error(func.asteriskToken, ts.Diagnostics.Generator_implicitly_has_type_0_because_it_does_not_yield_any_values_Consider_supplying_a_return_type, typeToString(iterableIteratorAny)); + } + return iterableIteratorAny; } } - else if (parameterName) { - var hasReportedError = false; - for (var _i = 0, _a = parent.parameters; _i < _a.length; _i++) { - var name_18 = _a[_i].name; - if (ts.isBindingPattern(name_18) && - checkIfTypePredicateVariableIsDeclaredInBindingPattern(name_18, parameterName, typePredicate.parameterName)) { - hasReportedError = true; - break; - } + else { + types = checkAndAggregateReturnExpressionTypes(func, contextualMapper); + if (!types) { + // For an async function, the return type will not be never, but rather a Promise for never. + return isAsync ? createPromiseReturnType(func, neverType) : neverType; } - if (!hasReportedError) { - error(node.parameterName, ts.Diagnostics.Cannot_find_parameter_0, typePredicate.parameterName); + if (types.length === 0) { + // For an async function, the return type will not be void, but rather a Promise for void. + return isAsync ? createPromiseReturnType(func, voidType) : voidType; } } + // Return a union of the return expression types. + type = getUnionType(types, /*subtypeReduction*/ true); + if (funcIsGenerator) { + type = createIterableIteratorType(type); + } } - } - function getTypePredicateParent(node) { - switch (node.parent.kind) { - case 180 /* ArrowFunction */: - case 151 /* CallSignature */: - case 220 /* FunctionDeclaration */: - case 179 /* FunctionExpression */: - case 156 /* FunctionType */: - case 147 /* MethodDeclaration */: - case 146 /* MethodSignature */: - var parent_11 = node.parent; - if (node === parent_11.type) { - return parent_11; - } + if (!contextualSignature) { + reportErrorsFromWidening(func, type); + } + if (isUnitType(type) && + !(contextualSignature && + isLiteralContextualType(contextualSignature === getSignatureFromDeclaration(func) ? type : getReturnTypeOfSignature(contextualSignature)))) { + type = getWidenedLiteralType(type); } + var widenedType = getWidenedType(type); + // From within an async function you can return either a non-promise value or a promise. Any + // Promise/A+ compatible implementation will always assimilate any foreign promise, so the + // return type of the body is awaited type of the body, wrapped in a native Promise type. + return isAsync ? createPromiseReturnType(func, widenedType) : widenedType; } - function checkIfTypePredicateVariableIsDeclaredInBindingPattern(pattern, predicateVariableNode, predicateVariableName) { - for (var _i = 0, _a = pattern.elements; _i < _a.length; _i++) { - var element = _a[_i]; - if (ts.isOmittedExpression(element)) { - continue; - } - var name_19 = element.name; - if (name_19.kind === 69 /* Identifier */ && - name_19.text === predicateVariableName) { - error(predicateVariableNode, ts.Diagnostics.A_type_predicate_cannot_reference_element_0_in_a_binding_pattern, predicateVariableName); - return true; - } - else if (name_19.kind === 168 /* ArrayBindingPattern */ || - name_19.kind === 167 /* ObjectBindingPattern */) { - if (checkIfTypePredicateVariableIsDeclaredInBindingPattern(name_19, predicateVariableNode, predicateVariableName)) { - return true; + function checkAndAggregateYieldOperandTypes(func, contextualMapper) { + var aggregatedTypes = []; + ts.forEachYieldExpression(func.body, function (yieldExpression) { + var expr = yieldExpression.expression; + if (expr) { + var type = checkExpressionCached(expr, contextualMapper); + if (yieldExpression.asteriskToken) { + // A yield* expression effectively yields everything that its operand yields + type = checkElementTypeOfIterable(type, yieldExpression.expression); + } + if (!ts.contains(aggregatedTypes, type)) { + aggregatedTypes.push(type); } } - } + }); + return aggregatedTypes; } - function checkSignatureDeclaration(node) { - // Grammar checking - if (node.kind === 153 /* IndexSignature */) { - checkGrammarIndexSignature(node); + function isExhaustiveSwitchStatement(node) { + if (!node.possiblyExhaustive) { + return false; } - else if (node.kind === 156 /* FunctionType */ || node.kind === 220 /* FunctionDeclaration */ || node.kind === 157 /* ConstructorType */ || - node.kind === 151 /* CallSignature */ || node.kind === 148 /* Constructor */ || - node.kind === 152 /* ConstructSignature */) { - checkGrammarFunctionLikeDeclaration(node); + var type = checkExpression(node.expression); + if (!isLiteralType(type)) { + return false; } - checkTypeParameters(node.typeParameters); - ts.forEach(node.parameters, checkParameter); - if (node.type) { - checkSourceElement(node.type); + var switchTypes = getSwitchClauseTypes(node); + if (!switchTypes.length) { + return false; } - if (produceDiagnostics) { - checkCollisionWithArgumentsInGeneratedCode(node); - if (compilerOptions.noImplicitAny && !node.type) { - switch (node.kind) { - case 152 /* ConstructSignature */: - error(node, ts.Diagnostics.Construct_signature_which_lacks_return_type_annotation_implicitly_has_an_any_return_type); - break; - case 151 /* CallSignature */: - error(node, ts.Diagnostics.Call_signature_which_lacks_return_type_annotation_implicitly_has_an_any_return_type); - break; + return eachTypeContainedIn(type, switchTypes); + } + function functionHasImplicitReturn(func) { + if (!(func.flags & 128 /* HasImplicitReturn */)) { + return false; + } + var lastStatement = ts.lastOrUndefined(func.body.statements); + if (lastStatement && lastStatement.kind === 213 /* SwitchStatement */ && isExhaustiveSwitchStatement(lastStatement)) { + return false; + } + return true; + } + function checkAndAggregateReturnExpressionTypes(func, contextualMapper) { + var isAsync = ts.isAsyncFunctionLike(func); + var aggregatedTypes = []; + var hasReturnWithNoExpression = functionHasImplicitReturn(func); + var hasReturnOfTypeNever = false; + ts.forEachReturnStatement(func.body, function (returnStatement) { + var expr = returnStatement.expression; + if (expr) { + var type = checkExpressionCached(expr, contextualMapper); + if (isAsync) { + // From within an async function you can return either a non-promise value or a promise. Any + // Promise/A+ compatible implementation will always assimilate any foreign promise, so the + // return type of the body should be unwrapped to its awaited type, which should be wrapped in + // the native Promise type by the caller. + type = checkAwaitedType(type, func, ts.Diagnostics.Return_expression_in_async_function_does_not_have_a_valid_callable_then_member); } - } - if (node.type) { - if (languageVersion >= 2 /* ES6 */ && isSyntacticallyValidGenerator(node)) { - var returnType = getTypeFromTypeNode(node.type); - if (returnType === voidType) { - error(node.type, ts.Diagnostics.A_generator_cannot_have_a_void_type_annotation); - } - else { - var generatorElementType = getElementTypeOfIterableIterator(returnType) || anyType; - var iterableIteratorInstantiation = createIterableIteratorType(generatorElementType); - // Naively, one could check that IterableIterator is assignable to the return type annotation. - // However, that would not catch the error in the following case. - // - // interface BadGenerator extends Iterable, Iterator { } - // function* g(): BadGenerator { } // Iterable and Iterator have different types! - // - checkTypeAssignableTo(iterableIteratorInstantiation, returnType, node.type); - } + if (type.flags & 8192 /* Never */) { + hasReturnOfTypeNever = true; } - else if (ts.isAsyncFunctionLike(node)) { - checkAsyncFunctionReturnType(node); + else if (!ts.contains(aggregatedTypes, type)) { + aggregatedTypes.push(type); } } - if (noUnusedIdentifiers && !node.body) { - checkUnusedTypeParameters(node); + else { + hasReturnWithNoExpression = true; + } + }); + if (aggregatedTypes.length === 0 && !hasReturnWithNoExpression && (hasReturnOfTypeNever || + func.kind === 179 /* FunctionExpression */ || func.kind === 180 /* ArrowFunction */)) { + return undefined; + } + if (strictNullChecks && aggregatedTypes.length && hasReturnWithNoExpression) { + if (!ts.contains(aggregatedTypes, undefinedType)) { + aggregatedTypes.push(undefinedType); } } + return aggregatedTypes; } - function checkClassForDuplicateDeclarations(node) { - var Accessor; - (function (Accessor) { - Accessor[Accessor["Getter"] = 1] = "Getter"; - Accessor[Accessor["Setter"] = 2] = "Setter"; - Accessor[Accessor["Property"] = 3] = "Property"; - })(Accessor || (Accessor = {})); - var instanceNames = ts.createMap(); - var staticNames = ts.createMap(); - for (var _i = 0, _a = node.members; _i < _a.length; _i++) { - var member = _a[_i]; - if (member.kind === 148 /* Constructor */) { - for (var _b = 0, _c = member.parameters; _b < _c.length; _b++) { - var param = _c[_b]; - if (ts.isParameterPropertyDeclaration(param)) { - addName(instanceNames, param.name, param.name.text, 3 /* Property */); - } - } - } - else { - var isStatic = ts.forEach(member.modifiers, function (m) { return m.kind === 113 /* StaticKeyword */; }); - var names = isStatic ? staticNames : instanceNames; - var memberName = member.name && ts.getPropertyNameForPropertyNameNode(member.name); - if (memberName) { - switch (member.kind) { - case 149 /* GetAccessor */: - addName(names, member.name, memberName, 1 /* Getter */); - break; - case 150 /* SetAccessor */: - addName(names, member.name, memberName, 2 /* Setter */); - break; - case 145 /* PropertyDeclaration */: - addName(names, member.name, memberName, 3 /* Property */); - break; - } - } - } + /** + * TypeScript Specification 1.0 (6.3) - July 2014 + * An explicitly typed function whose return type isn't the Void type, + * the Any type, or a union type containing the Void or Any type as a constituent + * must have at least one return statement somewhere in its body. + * An exception to this rule is if the function implementation consists of a single 'throw' statement. + * + * @param returnType - return type of the function, can be undefined if return type is not explicitly specified + */ + function checkAllCodePathsInNonVoidFunctionReturnOrThrow(func, returnType) { + if (!produceDiagnostics) { + return; } - function addName(names, location, name, meaning) { - var prev = names[name]; - if (prev) { - if (prev & meaning) { - error(location, ts.Diagnostics.Duplicate_identifier_0, ts.getTextOfNode(location)); - } - else { - names[name] = prev | meaning; - } - } - else { - names[name] = meaning; - } + // Functions with with an explicitly specified 'void' or 'any' return type don't need any return expressions. + if (returnType && maybeTypeOfKind(returnType, 1 /* Any */ | 1024 /* Void */)) { + return; } - } - function checkObjectTypeForDuplicateDeclarations(node) { - var names = ts.createMap(); - for (var _i = 0, _a = node.members; _i < _a.length; _i++) { - var member = _a[_i]; - if (member.kind == 144 /* PropertySignature */) { - var memberName = void 0; - switch (member.name.kind) { - case 9 /* StringLiteral */: - case 8 /* NumericLiteral */: - case 69 /* Identifier */: - memberName = member.name.text; - break; - default: - continue; - } - if (names[memberName]) { - error(member.symbol.valueDeclaration.name, ts.Diagnostics.Duplicate_identifier_0, memberName); - error(member.name, ts.Diagnostics.Duplicate_identifier_0, memberName); - } - else { - names[memberName] = true; - } - } + // If all we have is a function signature, or an arrow function with an expression body, then there is nothing to check. + // also if HasImplicitReturn flag is not set this means that all codepaths in function body end with return or throw + if (ts.nodeIsMissing(func.body) || func.body.kind !== 199 /* Block */ || !functionHasImplicitReturn(func)) { + return; } - } - function checkTypeForDuplicateIndexSignatures(node) { - if (node.kind === 222 /* InterfaceDeclaration */) { - var nodeSymbol = getSymbolOfNode(node); - // in case of merging interface declaration it is possible that we'll enter this check procedure several times for every declaration - // to prevent this run check only for the first declaration of a given kind - if (nodeSymbol.declarations.length > 0 && nodeSymbol.declarations[0] !== node) { - return; - } + var hasExplicitReturn = func.flags & 256 /* HasExplicitReturn */; + if (returnType && returnType.flags & 8192 /* Never */) { + error(func.type, ts.Diagnostics.A_function_returning_never_cannot_have_a_reachable_end_point); } - // TypeScript 1.0 spec (April 2014) - // 3.7.4: An object type can contain at most one string index signature and one numeric index signature. - // 8.5: A class declaration can have at most one string index member declaration and one numeric index member declaration - var indexSymbol = getIndexSymbol(getSymbolOfNode(node)); - if (indexSymbol) { - var seenNumericIndexer = false; - var seenStringIndexer = false; - for (var _i = 0, _a = indexSymbol.declarations; _i < _a.length; _i++) { - var decl = _a[_i]; - var declaration = decl; - if (declaration.parameters.length === 1 && declaration.parameters[0].type) { - switch (declaration.parameters[0].type.kind) { - case 132 /* StringKeyword */: - if (!seenStringIndexer) { - seenStringIndexer = true; - } - else { - error(declaration, ts.Diagnostics.Duplicate_string_index_signature); - } - break; - case 130 /* NumberKeyword */: - if (!seenNumericIndexer) { - seenNumericIndexer = true; - } - else { - error(declaration, ts.Diagnostics.Duplicate_number_index_signature); - } - break; - } + else if (returnType && !hasExplicitReturn) { + // minimal check: function has syntactic return type annotation and no explicit return statements in the body + // this function does not conform to the specification. + // NOTE: having returnType !== undefined is a precondition for entering this branch so func.type will always be present + error(func.type, ts.Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value); + } + else if (returnType && strictNullChecks && !isTypeAssignableTo(undefinedType, returnType)) { + error(func.type, ts.Diagnostics.Function_lacks_ending_return_statement_and_return_type_does_not_include_undefined); + } + else if (compilerOptions.noImplicitReturns) { + if (!returnType) { + // If return type annotation is omitted check if function has any explicit return statements. + // If it does not have any - its inferred return type is void - don't do any checks. + // Otherwise get inferred return type from function body and report error only if it is not void / anytype + if (!hasExplicitReturn) { + return; + } + var inferredReturnType = getReturnTypeOfSignature(getSignatureFromDeclaration(func)); + if (isUnwrappedReturnTypeVoidOrAny(func, inferredReturnType)) { + return; } } + error(func.type || func, ts.Diagnostics.Not_all_code_paths_return_a_value); } } - function checkPropertyDeclaration(node) { - // Grammar checking - checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarProperty(node) || checkGrammarComputedPropertyName(node.name); - checkVariableLikeDeclaration(node); - } - function checkMethodDeclaration(node) { + function checkFunctionExpressionOrObjectLiteralMethod(node, contextualMapper) { + ts.Debug.assert(node.kind !== 147 /* MethodDeclaration */ || ts.isObjectLiteralMethod(node)); // Grammar checking - checkGrammarMethod(node) || checkGrammarComputedPropertyName(node.name); - // Grammar checking for modifiers is done inside the function checkGrammarFunctionLikeDeclaration - checkFunctionOrMethodDeclaration(node); - // Abstract methods cannot have an implementation. - // Extra checks are to avoid reporting multiple errors relating to the "abstractness" of the node. - if (ts.getModifierFlags(node) & 128 /* Abstract */ && node.body) { - error(node, ts.Diagnostics.Method_0_cannot_have_an_implementation_because_it_is_marked_abstract, ts.declarationNameToString(node.name)); - } - } - function checkConstructorDeclaration(node) { - // Grammar check on signature of constructor and modifier of the constructor is done in checkSignatureDeclaration function. - checkSignatureDeclaration(node); - // Grammar check for checking only related to constructorDeclaration - checkGrammarConstructorTypeParameters(node) || checkGrammarConstructorTypeAnnotation(node); - checkSourceElement(node.body); - registerForUnusedIdentifiersCheck(node); - var symbol = getSymbolOfNode(node); - var firstDeclaration = ts.getDeclarationOfKind(symbol, node.kind); - // Only type check the symbol once - if (node === firstDeclaration) { - checkFunctionOrConstructorSymbol(symbol); - } - // exit early in the case of signature - super checks are not relevant to them - if (ts.nodeIsMissing(node.body)) { - return; - } - if (!produceDiagnostics) { - return; - } - function containsSuperCallAsComputedPropertyName(n) { - return n.name && containsSuperCall(n.name); - } - function containsSuperCall(n) { - if (ts.isSuperCallExpression(n)) { - return true; - } - else if (ts.isFunctionLike(n)) { - return false; - } - else if (ts.isClassLike(n)) { - return ts.forEach(n.members, containsSuperCallAsComputedPropertyName); - } - return ts.forEachChild(n, containsSuperCall); - } - function markThisReferencesAsErrors(n) { - if (n.kind === 97 /* ThisKeyword */) { - error(n, ts.Diagnostics.this_cannot_be_referenced_in_current_location); - } - else if (n.kind !== 179 /* FunctionExpression */ && n.kind !== 220 /* FunctionDeclaration */) { - ts.forEachChild(n, markThisReferencesAsErrors); - } + var hasGrammarError = checkGrammarFunctionLikeDeclaration(node); + if (!hasGrammarError && node.kind === 179 /* FunctionExpression */) { + checkGrammarForGenerator(node); } - function isInstancePropertyWithInitializer(n) { - return n.kind === 145 /* PropertyDeclaration */ && - !(ts.getModifierFlags(n) & 32 /* Static */) && - !!n.initializer; + // The identityMapper object is used to indicate that function expressions are wildcards + if (contextualMapper === identityMapper && isContextSensitive(node)) { + checkNodeDeferred(node); + return anyFunctionType; } - // TS 1.0 spec (April 2014): 8.3.2 - // Constructors of classes with no extends clause may not contain super calls, whereas - // constructors of derived classes must contain at least one super call somewhere in their function body. - var containingClassDecl = node.parent; - if (ts.getClassExtendsHeritageClauseElement(containingClassDecl)) { - var classExtendsNull = classDeclarationExtendsNull(containingClassDecl); - var superCall = getSuperCallInConstructor(node); - if (superCall) { - if (classExtendsNull) { - error(superCall, ts.Diagnostics.A_constructor_cannot_contain_a_super_call_when_its_class_extends_null); - } - // The first statement in the body of a constructor (excluding prologue directives) must be a super call - // if both of the following are true: - // - The containing class is a derived class. - // - The constructor declares parameter properties - // or the containing class declares instance member variables with initializers. - var superCallShouldBeFirst = ts.forEach(node.parent.members, isInstancePropertyWithInitializer) || - ts.forEach(node.parameters, function (p) { return ts.getModifierFlags(p) & 92 /* ParameterPropertyModifier */; }); - // Skip past any prologue directives to find the first statement - // to ensure that it was a super call. - if (superCallShouldBeFirst) { - var statements = node.body.statements; - var superCallStatement = void 0; - for (var _i = 0, statements_2 = statements; _i < statements_2.length; _i++) { - var statement = statements_2[_i]; - if (statement.kind === 202 /* ExpressionStatement */ && ts.isSuperCallExpression(statement.expression)) { - superCallStatement = statement; - break; - } - if (!ts.isPrologueDirective(statement)) { - break; - } + var links = getNodeLinks(node); + var type = getTypeOfSymbol(node.symbol); + var contextSensitive = isContextSensitive(node); + var mightFixTypeParameters = contextSensitive && isInferentialContext(contextualMapper); + // Check if function expression is contextually typed and assign parameter types if so. + // See the comment in assignTypeToParameterAndFixTypeParameters to understand why we need to + // check mightFixTypeParameters. + if (mightFixTypeParameters || !(links.flags & 1024 /* ContextChecked */)) { + var contextualSignature = getContextualSignature(node); + // If a type check is started at a function expression that is an argument of a function call, obtaining the + // contextual type may recursively get back to here during overload resolution of the call. If so, we will have + // already assigned contextual types. + var contextChecked = !!(links.flags & 1024 /* ContextChecked */); + if (mightFixTypeParameters || !contextChecked) { + links.flags |= 1024 /* ContextChecked */; + if (contextualSignature) { + var signature = getSignaturesOfType(type, 0 /* Call */)[0]; + if (contextSensitive) { + assignContextualParameterTypes(signature, contextualSignature, contextualMapper || identityMapper); } - if (!superCallStatement) { - error(node, ts.Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_when_a_class_contains_initialized_properties_or_has_parameter_properties); + if (mightFixTypeParameters || !node.type && !signature.resolvedReturnType) { + var returnType = getReturnTypeFromBody(node, contextualMapper); + if (!signature.resolvedReturnType) { + signature.resolvedReturnType = returnType; + } } } - } - else if (!classExtendsNull) { - error(node, ts.Diagnostics.Constructors_for_derived_classes_must_contain_a_super_call); + if (!contextChecked) { + checkSignatureDeclaration(node); + checkNodeDeferred(node); + } } } + if (produceDiagnostics && node.kind !== 147 /* MethodDeclaration */) { + checkCollisionWithCapturedSuperVariable(node, node.name); + checkCollisionWithCapturedThisVariable(node, node.name); + } + return type; } - function checkAccessorDeclaration(node) { - if (produceDiagnostics) { - // Grammar checking accessors - checkGrammarFunctionLikeDeclaration(node) || checkGrammarAccessor(node) || checkGrammarComputedPropertyName(node.name); - checkDecorators(node); - checkSignatureDeclaration(node); - if (node.kind === 149 /* GetAccessor */) { - if (!ts.isInAmbientContext(node) && ts.nodeIsPresent(node.body) && (node.flags & 128 /* HasImplicitReturn */)) { - if (!(node.flags & 256 /* HasExplicitReturn */)) { - error(node.name, ts.Diagnostics.A_get_accessor_must_return_a_value); - } - } + function checkFunctionExpressionOrObjectLiteralMethodDeferred(node) { + ts.Debug.assert(node.kind !== 147 /* MethodDeclaration */ || ts.isObjectLiteralMethod(node)); + var isAsync = ts.isAsyncFunctionLike(node); + var returnOrPromisedType = node.type && (isAsync ? checkAsyncFunctionReturnType(node) : getTypeFromTypeNode(node.type)); + if (!node.asteriskToken) { + // return is not necessary in the body of generators + checkAllCodePathsInNonVoidFunctionReturnOrThrow(node, returnOrPromisedType); + } + if (node.body) { + if (!node.type) { + // There are some checks that are only performed in getReturnTypeFromBody, that may produce errors + // we need. An example is the noImplicitAny errors resulting from widening the return expression + // of a function. Because checking of function expression bodies is deferred, there was never an + // appropriate time to do this during the main walk of the file (see the comment at the top of + // checkFunctionExpressionBodies). So it must be done now. + getReturnTypeOfSignature(getSignatureFromDeclaration(node)); } - // Do not use hasDynamicName here, because that returns false for well known symbols. - // We want to perform checkComputedPropertyName for all computed properties, including - // well known symbols. - if (node.name.kind === 140 /* ComputedPropertyName */) { - checkComputedPropertyName(node.name); + if (node.body.kind === 199 /* Block */) { + checkSourceElement(node.body); } - if (!ts.hasDynamicName(node)) { - // TypeScript 1.0 spec (April 2014): 8.4.3 - // Accessors for the same member name must specify the same accessibility. - var otherKind = node.kind === 149 /* GetAccessor */ ? 150 /* SetAccessor */ : 149 /* GetAccessor */; - var otherAccessor = ts.getDeclarationOfKind(node.symbol, otherKind); - if (otherAccessor) { - if ((ts.getModifierFlags(node) & 28 /* AccessibilityModifier */) !== (ts.getModifierFlags(otherAccessor) & 28 /* AccessibilityModifier */)) { - error(node.name, ts.Diagnostics.Getter_and_setter_accessors_do_not_agree_in_visibility); + else { + // From within an async function you can return either a non-promise value or a promise. Any + // Promise/A+ compatible implementation will always assimilate any foreign promise, so we + // should not be checking assignability of a promise to the return type. Instead, we need to + // check assignability of the awaited type of the expression body against the promised type of + // its return type annotation. + var exprType = checkExpression(node.body); + if (returnOrPromisedType) { + if (isAsync) { + var awaitedType = checkAwaitedType(exprType, node.body, ts.Diagnostics.Expression_body_for_async_arrow_function_does_not_have_a_valid_callable_then_member); + checkTypeAssignableTo(awaitedType, returnOrPromisedType, node.body); } - if (ts.hasModifier(node, 128 /* Abstract */) !== ts.hasModifier(otherAccessor, 128 /* Abstract */)) { - error(node.name, ts.Diagnostics.Accessors_must_both_be_abstract_or_non_abstract); + else { + checkTypeAssignableTo(exprType, returnOrPromisedType, node.body); } - // TypeScript 1.0 spec (April 2014): 4.5 - // If both accessors include type annotations, the specified types must be identical. - checkAccessorDeclarationTypesIdentical(node, otherAccessor, getAnnotatedAccessorType, ts.Diagnostics.get_and_set_accessor_must_have_the_same_type); - checkAccessorDeclarationTypesIdentical(node, otherAccessor, getThisTypeOfDeclaration, ts.Diagnostics.get_and_set_accessor_must_have_the_same_this_type); } } - var returnType = getTypeOfAccessors(getSymbolOfNode(node)); - if (node.kind === 149 /* GetAccessor */) { - checkAllCodePathsInNonVoidFunctionReturnOrThrow(node, returnType); - } - } - if (node.parent.kind !== 171 /* ObjectLiteralExpression */) { - checkSourceElement(node.body); registerForUnusedIdentifiersCheck(node); } - else { - checkNodeDeferred(node); - } } - function checkAccessorDeclarationTypesIdentical(first, second, getAnnotatedType, message) { - var firstType = getAnnotatedType(first); - var secondType = getAnnotatedType(second); - if (firstType && secondType && !isTypeIdenticalTo(firstType, secondType)) { - error(first, message); + function checkArithmeticOperandType(operand, type, diagnostic) { + if (!isTypeAnyOrAllConstituentTypesHaveKind(type, 340 /* NumberLike */)) { + error(operand, diagnostic); + return false; } + return true; } - function checkAccessorDeferred(node) { - checkSourceElement(node.body); - registerForUnusedIdentifiersCheck(node); + function isReadonlySymbol(symbol) { + // The following symbols are considered read-only: + // Properties with a 'readonly' modifier + // Variables declared with 'const' + // Get accessors without matching set accessors + // Enum members + // Unions and intersections of the above (unions and intersections eagerly set isReadonly on creation) + return symbol.isReadonly || + symbol.flags & 4 /* Property */ && (getDeclarationModifierFlagsFromSymbol(symbol) & 64 /* Readonly */) !== 0 || + symbol.flags & 3 /* Variable */ && (getDeclarationNodeFlagsFromSymbol(symbol) & 2 /* Const */) !== 0 || + symbol.flags & 98304 /* Accessor */ && !(symbol.flags & 65536 /* SetAccessor */) || + (symbol.flags & 8 /* EnumMember */) !== 0; } - function checkMissingDeclaration(node) { - checkDecorators(node); + function isReferenceToReadonlyEntity(expr, symbol) { + if (isReadonlySymbol(symbol)) { + // Allow assignments to readonly properties within constructors of the same class declaration. + if (symbol.flags & 4 /* Property */ && + (expr.kind === 172 /* PropertyAccessExpression */ || expr.kind === 173 /* ElementAccessExpression */) && + expr.expression.kind === 97 /* ThisKeyword */) { + // Look for if this is the constructor for the class that `symbol` is a property of. + var func = ts.getContainingFunction(expr); + if (!(func && func.kind === 148 /* Constructor */)) + return true; + // If func.parent is a class and symbol is a (readonly) property of that class, or + // if func is a constructor and symbol is a (readonly) parameter property declared in it, + // then symbol is writeable here. + return !(func.parent === symbol.valueDeclaration.parent || func === symbol.valueDeclaration.parent); + } + return true; + } + return false; } - function checkTypeArgumentConstraints(typeParameters, typeArgumentNodes) { - var typeArguments; - var mapper; - var result = true; - for (var i = 0; i < typeParameters.length; i++) { - var constraint = getConstraintOfTypeParameter(typeParameters[i]); - if (constraint) { - if (!typeArguments) { - typeArguments = ts.map(typeArgumentNodes, getTypeFromTypeNodeNoAlias); - mapper = createTypeMapper(typeParameters, typeArguments); + function isReferenceThroughNamespaceImport(expr) { + if (expr.kind === 172 /* PropertyAccessExpression */ || expr.kind === 173 /* ElementAccessExpression */) { + var node = skipParenthesizedNodes(expr.expression); + if (node.kind === 69 /* Identifier */) { + var symbol = getNodeLinks(node).resolvedSymbol; + if (symbol.flags & 8388608 /* Alias */) { + var declaration = getDeclarationOfAliasSymbol(symbol); + return declaration && declaration.kind === 232 /* NamespaceImport */; } - var typeArgument = typeArguments[i]; - result = result && checkTypeAssignableTo(typeArgument, getTypeWithThisArgument(instantiateType(constraint, mapper), typeArgument), typeArgumentNodes[i], ts.Diagnostics.Type_0_does_not_satisfy_the_constraint_1); } } - return result; + return false; } - function checkTypeReferenceNode(node) { - checkGrammarTypeArguments(node, node.typeArguments); - var type = getTypeFromTypeReference(node); - if (type !== unknownType) { - if (node.typeArguments) { - // Do type argument local checks only if referenced type is successfully resolved - ts.forEach(node.typeArguments, checkSourceElement); - if (produceDiagnostics) { - var symbol = getNodeLinks(node).resolvedSymbol; - var typeParameters = symbol.flags & 524288 /* TypeAlias */ ? getSymbolLinks(symbol).typeParameters : type.target.localTypeParameters; - checkTypeArgumentConstraints(typeParameters, node.typeArguments); + function checkReferenceExpression(expr, invalidReferenceMessage, constantVariableMessage) { + // References are combinations of identifiers, parentheses, and property accesses. + var node = skipParenthesizedNodes(expr); + if (node.kind !== 69 /* Identifier */ && node.kind !== 172 /* PropertyAccessExpression */ && node.kind !== 173 /* ElementAccessExpression */) { + error(expr, invalidReferenceMessage); + return false; + } + // Because we get the symbol from the resolvedSymbol property, it might be of kind + // SymbolFlags.ExportValue. In this case it is necessary to get the actual export + // symbol, which will have the correct flags set on it. + var links = getNodeLinks(node); + var symbol = getExportSymbolOfValueSymbolIfExported(links.resolvedSymbol); + if (symbol) { + if (symbol !== unknownSymbol && symbol !== argumentsSymbol) { + // Only variables (and not functions, classes, namespaces, enum objects, or enum members) + // are considered references when referenced using a simple identifier. + if (node.kind === 69 /* Identifier */ && !(symbol.flags & 3 /* Variable */)) { + error(expr, invalidReferenceMessage); + return false; + } + if (isReferenceToReadonlyEntity(node, symbol) || isReferenceThroughNamespaceImport(node)) { + error(expr, constantVariableMessage); + return false; } } - if (type.flags & 16 /* Enum */ && !type.memberTypes && getNodeLinks(node).resolvedSymbol.flags & 8 /* EnumMember */) { - error(node, ts.Diagnostics.Enum_type_0_has_members_with_initializers_that_are_not_literals, typeToString(type)); + } + else if (node.kind === 173 /* ElementAccessExpression */) { + if (links.resolvedIndexInfo && links.resolvedIndexInfo.isReadonly) { + error(expr, constantVariableMessage); + return false; } } + return true; } - function checkTypeQuery(node) { - getTypeFromTypeQueryNode(node); + function checkDeleteExpression(node) { + checkExpression(node.expression); + return booleanType; } - function checkTypeLiteral(node) { - ts.forEach(node.members, checkSourceElement); - if (produceDiagnostics) { - var type = getTypeFromTypeLiteralOrFunctionOrConstructorTypeNode(node); - checkIndexConstraints(type); - checkTypeForDuplicateIndexSignatures(node); - checkObjectTypeForDuplicateDeclarations(node); - } + function checkTypeOfExpression(node) { + checkExpression(node.expression); + return stringType; } - function checkArrayType(node) { - checkSourceElement(node.elementType); + function checkVoidExpression(node) { + checkExpression(node.expression); + return undefinedWideningType; } - function checkTupleType(node) { + function checkAwaitExpression(node) { // Grammar checking - var hasErrorFromDisallowedTrailingComma = checkGrammarForDisallowedTrailingComma(node.elementTypes); - if (!hasErrorFromDisallowedTrailingComma && node.elementTypes.length === 0) { - grammarErrorOnNode(node, ts.Diagnostics.A_tuple_type_element_list_cannot_be_empty); + if (produceDiagnostics) { + if (!(node.flags & 262144 /* AwaitContext */)) { + grammarErrorOnFirstToken(node, ts.Diagnostics.await_expression_is_only_allowed_within_an_async_function); + } + if (isInParameterInitializerBeforeContainingFunction(node)) { + error(node, ts.Diagnostics.await_expressions_cannot_be_used_in_a_parameter_initializer); + } } - ts.forEach(node.elementTypes, checkSourceElement); - } - function checkUnionOrIntersectionType(node) { - ts.forEach(node.types, checkSourceElement); - } - function isPrivateWithinAmbient(node) { - return (ts.getModifierFlags(node) & 8 /* Private */) && ts.isInAmbientContext(node); + var operandType = checkExpression(node.expression); + return checkAwaitedType(operandType, node); } - function getEffectiveDeclarationFlags(n, flagsToCheck) { - var flags = ts.getCombinedModifierFlags(n); - // children of classes (even ambient classes) should not be marked as ambient or export - // because those flags have no useful semantics there. - if (n.parent.kind !== 222 /* InterfaceDeclaration */ && - n.parent.kind !== 221 /* ClassDeclaration */ && - n.parent.kind !== 192 /* ClassExpression */ && - ts.isInAmbientContext(n)) { - if (!(flags & 2 /* Ambient */)) { - // It is nested in an ambient context, which means it is automatically exported - flags |= 1 /* Export */; - } - flags |= 2 /* Ambient */; + function checkPrefixUnaryExpression(node) { + var operandType = checkExpression(node.operand); + if (operandType === silentNeverType) { + return silentNeverType; } - return flags & flagsToCheck; - } - function checkFunctionOrConstructorSymbol(symbol) { - if (!produceDiagnostics) { - return; + if (node.operator === 36 /* MinusToken */ && node.operand.kind === 8 /* NumericLiteral */) { + return getFreshTypeOfLiteralType(getLiteralTypeForText(64 /* NumberLiteral */, "" + -node.operand.text)); } - function getCanonicalOverload(overloads, implementation) { - // Consider the canonical set of flags to be the flags of the bodyDeclaration or the first declaration - // Error on all deviations from this canonical set of flags - // The caveat is that if some overloads are defined in lib.d.ts, we don't want to - // report the errors on those. To achieve this, we will say that the implementation is - // the canonical signature only if it is in the same container as the first overload - var implementationSharesContainerWithFirstOverload = implementation !== undefined && implementation.parent === overloads[0].parent; - return implementationSharesContainerWithFirstOverload ? implementation : overloads[0]; + switch (node.operator) { + case 35 /* PlusToken */: + case 36 /* MinusToken */: + case 50 /* TildeToken */: + if (maybeTypeOfKind(operandType, 512 /* ESSymbol */)) { + error(node.operand, ts.Diagnostics.The_0_operator_cannot_be_applied_to_type_symbol, ts.tokenToString(node.operator)); + } + return numberType; + case 49 /* ExclamationToken */: + var facts = getTypeFacts(operandType) & (1048576 /* Truthy */ | 2097152 /* Falsy */); + return facts === 1048576 /* Truthy */ ? falseType : + facts === 2097152 /* Falsy */ ? trueType : + booleanType; + case 41 /* PlusPlusToken */: + case 42 /* MinusMinusToken */: + var ok = checkArithmeticOperandType(node.operand, getNonNullableType(operandType), ts.Diagnostics.An_arithmetic_operand_must_be_of_type_any_number_or_an_enum_type); + if (ok) { + // run check only if former checks succeeded to avoid reporting cascading errors + checkReferenceExpression(node.operand, ts.Diagnostics.The_operand_of_an_increment_or_decrement_operator_must_be_a_variable_property_or_indexer, ts.Diagnostics.The_operand_of_an_increment_or_decrement_operator_cannot_be_a_constant_or_a_read_only_property); + } + return numberType; } - function checkFlagAgreementBetweenOverloads(overloads, implementation, flagsToCheck, someOverloadFlags, allOverloadFlags) { - // Error if some overloads have a flag that is not shared by all overloads. To find the - // deviations, we XOR someOverloadFlags with allOverloadFlags - var someButNotAllOverloadFlags = someOverloadFlags ^ allOverloadFlags; - if (someButNotAllOverloadFlags !== 0) { - var canonicalFlags_1 = getEffectiveDeclarationFlags(getCanonicalOverload(overloads, implementation), flagsToCheck); - ts.forEach(overloads, function (o) { - var deviation = getEffectiveDeclarationFlags(o, flagsToCheck) ^ canonicalFlags_1; - if (deviation & 1 /* Export */) { - error(o.name, ts.Diagnostics.Overload_signatures_must_all_be_exported_or_non_exported); - } - else if (deviation & 2 /* Ambient */) { - error(o.name, ts.Diagnostics.Overload_signatures_must_all_be_ambient_or_non_ambient); - } - else if (deviation & (8 /* Private */ | 16 /* Protected */)) { - error(o.name || o, ts.Diagnostics.Overload_signatures_must_all_be_public_private_or_protected); - } - else if (deviation & 128 /* Abstract */) { - error(o.name, ts.Diagnostics.Overload_signatures_must_all_be_abstract_or_non_abstract); - } - }); - } + return unknownType; + } + function checkPostfixUnaryExpression(node) { + var operandType = checkExpression(node.operand); + if (operandType === silentNeverType) { + return silentNeverType; } - function checkQuestionTokenAgreementBetweenOverloads(overloads, implementation, someHaveQuestionToken, allHaveQuestionToken) { - if (someHaveQuestionToken !== allHaveQuestionToken) { - var canonicalHasQuestionToken_1 = ts.hasQuestionToken(getCanonicalOverload(overloads, implementation)); - ts.forEach(overloads, function (o) { - var deviation = ts.hasQuestionToken(o) !== canonicalHasQuestionToken_1; - if (deviation) { - error(o.name, ts.Diagnostics.Overload_signatures_must_all_be_optional_or_required); - } - }); - } + var ok = checkArithmeticOperandType(node.operand, getNonNullableType(operandType), ts.Diagnostics.An_arithmetic_operand_must_be_of_type_any_number_or_an_enum_type); + if (ok) { + // run check only if former checks succeeded to avoid reporting cascading errors + checkReferenceExpression(node.operand, ts.Diagnostics.The_operand_of_an_increment_or_decrement_operator_must_be_a_variable_property_or_indexer, ts.Diagnostics.The_operand_of_an_increment_or_decrement_operator_cannot_be_a_constant_or_a_read_only_property); } - var flagsToCheck = 1 /* Export */ | 2 /* Ambient */ | 8 /* Private */ | 16 /* Protected */ | 128 /* Abstract */; - var someNodeFlags = 0 /* None */; - var allNodeFlags = flagsToCheck; - var someHaveQuestionToken = false; - var allHaveQuestionToken = true; - var hasOverloads = false; - var bodyDeclaration; - var lastSeenNonAmbientDeclaration; - var previousDeclaration; - var declarations = symbol.declarations; - var isConstructor = (symbol.flags & 16384 /* Constructor */) !== 0; - function reportImplementationExpectedError(node) { - if (node.name && ts.nodeIsMissing(node.name)) { - return; - } - var seen = false; - var subsequentNode = ts.forEachChild(node.parent, function (c) { - if (seen) { - return c; - } - else { - seen = c === node; - } - }); - // We may be here because of some extra nodes between overloads that could not be parsed into a valid node. - // In this case the subsequent node is not really consecutive (.pos !== node.end), and we must ignore it here. - if (subsequentNode && subsequentNode.pos === node.end) { - if (subsequentNode.kind === node.kind) { - var errorNode_1 = subsequentNode.name || subsequentNode; - // TODO(jfreeman): These are methods, so handle computed name case - if (node.name && subsequentNode.name && node.name.text === subsequentNode.name.text) { - var reportError = (node.kind === 147 /* MethodDeclaration */ || node.kind === 146 /* MethodSignature */) && - (ts.getModifierFlags(node) & 32 /* Static */) !== (ts.getModifierFlags(subsequentNode) & 32 /* Static */); - // we can get here in two cases - // 1. mixed static and instance class members - // 2. something with the same name was defined before the set of overloads that prevents them from merging - // here we'll report error only for the first case since for second we should already report error in binder - if (reportError) { - var diagnostic = ts.getModifierFlags(node) & 32 /* Static */ ? ts.Diagnostics.Function_overload_must_be_static : ts.Diagnostics.Function_overload_must_not_be_static; - error(errorNode_1, diagnostic); - } - return; - } - else if (ts.nodeIsPresent(subsequentNode.body)) { - error(errorNode_1, ts.Diagnostics.Function_implementation_name_must_be_0, ts.declarationNameToString(node.name)); - return; - } + return numberType; + } + // Return true if type might be of the given kind. A union or intersection type might be of a given + // kind if at least one constituent type is of the given kind. + function maybeTypeOfKind(type, kind) { + if (type.flags & kind) { + return true; + } + if (type.flags & 1572864 /* UnionOrIntersection */) { + var types = type.types; + for (var _i = 0, types_14 = types; _i < types_14.length; _i++) { + var t = types_14[_i]; + if (maybeTypeOfKind(t, kind)) { + return true; } } - var errorNode = node.name || node; - if (isConstructor) { - error(errorNode, ts.Diagnostics.Constructor_implementation_is_missing); - } - else { - // Report different errors regarding non-consecutive blocks of declarations depending on whether - // the node in question is abstract. - if (ts.getModifierFlags(node) & 128 /* Abstract */) { - error(errorNode, ts.Diagnostics.All_declarations_of_an_abstract_method_must_be_consecutive); - } - else { - error(errorNode, ts.Diagnostics.Function_implementation_is_missing_or_not_immediately_following_the_declaration); + } + return false; + } + // Return true if type is of the given kind. A union type is of a given kind if all constituent types + // are of the given kind. An intersection type is of a given kind if at least one constituent type is + // of the given kind. + function isTypeOfKind(type, kind) { + if (type.flags & kind) { + return true; + } + if (type.flags & 524288 /* Union */) { + var types = type.types; + for (var _i = 0, types_15 = types; _i < types_15.length; _i++) { + var t = types_15[_i]; + if (!isTypeOfKind(t, kind)) { + return false; } } + return true; } - var duplicateFunctionDeclaration = false; - var multipleConstructorImplementation = false; - for (var _i = 0, declarations_4 = declarations; _i < declarations_4.length; _i++) { - var current = declarations_4[_i]; - var node = current; - var inAmbientContext = ts.isInAmbientContext(node); - var inAmbientContextOrInterface = node.parent.kind === 222 /* InterfaceDeclaration */ || node.parent.kind === 159 /* TypeLiteral */ || inAmbientContext; - if (inAmbientContextOrInterface) { - // check if declarations are consecutive only if they are non-ambient - // 1. ambient declarations can be interleaved - // i.e. this is legal - // declare function foo(); - // declare function bar(); - // declare function foo(); - // 2. mixing ambient and non-ambient declarations is a separate error that will be reported - do not want to report an extra one - previousDeclaration = undefined; - } - if (node.kind === 220 /* FunctionDeclaration */ || node.kind === 147 /* MethodDeclaration */ || node.kind === 146 /* MethodSignature */ || node.kind === 148 /* Constructor */) { - var currentNodeFlags = getEffectiveDeclarationFlags(node, flagsToCheck); - someNodeFlags |= currentNodeFlags; - allNodeFlags &= currentNodeFlags; - someHaveQuestionToken = someHaveQuestionToken || ts.hasQuestionToken(node); - allHaveQuestionToken = allHaveQuestionToken && ts.hasQuestionToken(node); - if (ts.nodeIsPresent(node.body) && bodyDeclaration) { - if (isConstructor) { - multipleConstructorImplementation = true; - } - else { - duplicateFunctionDeclaration = true; - } - } - else if (previousDeclaration && previousDeclaration.parent === node.parent && previousDeclaration.end !== node.pos) { - reportImplementationExpectedError(previousDeclaration); - } - if (ts.nodeIsPresent(node.body)) { - if (!bodyDeclaration) { - bodyDeclaration = node; - } - } - else { - hasOverloads = true; - } - previousDeclaration = node; - if (!inAmbientContextOrInterface) { - lastSeenNonAmbientDeclaration = node; + if (type.flags & 1048576 /* Intersection */) { + var types = type.types; + for (var _a = 0, types_16 = types; _a < types_16.length; _a++) { + var t = types_16[_a]; + if (isTypeOfKind(t, kind)) { + return true; } } } - if (multipleConstructorImplementation) { - ts.forEach(declarations, function (declaration) { - error(declaration, ts.Diagnostics.Multiple_constructor_implementations_are_not_allowed); - }); - } - if (duplicateFunctionDeclaration) { - ts.forEach(declarations, function (declaration) { - error(declaration.name, ts.Diagnostics.Duplicate_function_implementation); - }); + return false; + } + function isConstEnumObjectType(type) { + return type.flags & (2588672 /* ObjectType */ | 2097152 /* Anonymous */) && type.symbol && isConstEnumSymbol(type.symbol); + } + function isConstEnumSymbol(symbol) { + return (symbol.flags & 128 /* ConstEnum */) !== 0; + } + function checkInstanceOfExpression(left, right, leftType, rightType) { + if (leftType === silentNeverType || rightType === silentNeverType) { + return silentNeverType; } - // Abstract methods can't have an implementation -- in particular, they don't need one. - if (lastSeenNonAmbientDeclaration && !lastSeenNonAmbientDeclaration.body && - !(ts.getModifierFlags(lastSeenNonAmbientDeclaration) & 128 /* Abstract */) && !lastSeenNonAmbientDeclaration.questionToken) { - reportImplementationExpectedError(lastSeenNonAmbientDeclaration); + // TypeScript 1.0 spec (April 2014): 4.15.4 + // The instanceof operator requires the left operand to be of type Any, an object type, or a type parameter type, + // and the right operand to be of type Any or a subtype of the 'Function' interface type. + // The result is always of the Boolean primitive type. + // NOTE: do not raise error if leftType is unknown as related error was already reported + if (isTypeOfKind(leftType, 8190 /* Primitive */)) { + error(left, ts.Diagnostics.The_left_hand_side_of_an_instanceof_expression_must_be_of_type_any_an_object_type_or_a_type_parameter); } - if (hasOverloads) { - checkFlagAgreementBetweenOverloads(declarations, bodyDeclaration, flagsToCheck, someNodeFlags, allNodeFlags); - checkQuestionTokenAgreementBetweenOverloads(declarations, bodyDeclaration, someHaveQuestionToken, allHaveQuestionToken); - if (bodyDeclaration) { - var signatures = getSignaturesOfSymbol(symbol); - var bodySignature = getSignatureFromDeclaration(bodyDeclaration); - for (var _a = 0, signatures_3 = signatures; _a < signatures_3.length; _a++) { - var signature = signatures_3[_a]; - if (!isImplementationCompatibleWithOverload(bodySignature, signature)) { - error(signature.declaration, ts.Diagnostics.Overload_signature_is_not_compatible_with_function_implementation); - break; - } - } - } + // NOTE: do not raise error if right is unknown as related error was already reported + if (!(isTypeAny(rightType) || isTypeSubtypeOf(rightType, globalFunctionType))) { + error(right, ts.Diagnostics.The_right_hand_side_of_an_instanceof_expression_must_be_of_type_any_or_of_a_type_assignable_to_the_Function_interface_type); } + return booleanType; } - function checkExportsOnMergedDeclarations(node) { - if (!produceDiagnostics) { - return; + function checkInExpression(left, right, leftType, rightType) { + if (leftType === silentNeverType || rightType === silentNeverType) { + return silentNeverType; } - // if localSymbol is defined on node then node itself is exported - check is required - var symbol = node.localSymbol; - if (!symbol) { - // local symbol is undefined => this declaration is non-exported. - // however symbol might contain other declarations that are exported - symbol = getSymbolOfNode(node); - if (!(symbol.flags & 7340032 /* Export */)) { - // this is a pure local symbol (all declarations are non-exported) - no need to check anything - return; - } + // TypeScript 1.0 spec (April 2014): 4.15.5 + // The in operator requires the left operand to be of type Any, the String primitive type, or the Number primitive type, + // and the right operand to be of type Any, an object type, or a type parameter type. + // The result is always of the Boolean primitive type. + if (!isTypeAnyOrAllConstituentTypesHaveKind(leftType, 34 /* StringLike */ | 340 /* NumberLike */ | 512 /* ESSymbol */)) { + error(left, ts.Diagnostics.The_left_hand_side_of_an_in_expression_must_be_of_type_any_string_number_or_symbol); } - // run the check only for the first declaration in the list - if (ts.getDeclarationOfKind(symbol, node.kind) !== node) { - return; + if (!isTypeAnyOrAllConstituentTypesHaveKind(rightType, 2588672 /* ObjectType */ | 16384 /* TypeParameter */)) { + error(right, ts.Diagnostics.The_right_hand_side_of_an_in_expression_must_be_of_type_any_an_object_type_or_a_type_parameter); } - // we use SymbolFlags.ExportValue, SymbolFlags.ExportType and SymbolFlags.ExportNamespace - // to denote disjoint declarationSpaces (without making new enum type). - var exportedDeclarationSpaces = 0 /* None */; - var nonExportedDeclarationSpaces = 0 /* None */; - var defaultExportedDeclarationSpaces = 0 /* None */; - for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { - var d = _a[_i]; - var declarationSpaces = getDeclarationSpaces(d); - var effectiveDeclarationFlags = getEffectiveDeclarationFlags(d, 1 /* Export */ | 512 /* Default */); - if (effectiveDeclarationFlags & 1 /* Export */) { - if (effectiveDeclarationFlags & 512 /* Default */) { - defaultExportedDeclarationSpaces |= declarationSpaces; + return booleanType; + } + function checkObjectLiteralAssignment(node, sourceType, contextualMapper) { + var properties = node.properties; + for (var _i = 0, properties_4 = properties; _i < properties_4.length; _i++) { + var p = properties_4[_i]; + checkObjectLiteralDestructuringPropertyAssignment(sourceType, p, contextualMapper); + } + return sourceType; + } + function checkObjectLiteralDestructuringPropertyAssignment(objectLiteralType, property, contextualMapper) { + if (property.kind === 253 /* PropertyAssignment */ || property.kind === 254 /* ShorthandPropertyAssignment */) { + var name_17 = property.name; + if (name_17.kind === 140 /* ComputedPropertyName */) { + checkComputedPropertyName(name_17); + } + if (isComputedNonLiteralName(name_17)) { + return undefined; + } + var text = getTextOfPropertyName(name_17); + var type = isTypeAny(objectLiteralType) + ? objectLiteralType + : getTypeOfPropertyOfType(objectLiteralType, text) || + isNumericLiteralName(text) && getIndexTypeOfType(objectLiteralType, 1 /* Number */) || + getIndexTypeOfType(objectLiteralType, 0 /* String */); + if (type) { + if (property.kind === 254 /* ShorthandPropertyAssignment */) { + return checkDestructuringAssignment(property, type); } else { - exportedDeclarationSpaces |= declarationSpaces; + // non-shorthand property assignments should always have initializers + return checkDestructuringAssignment(property.initializer, type); } } else { - nonExportedDeclarationSpaces |= declarationSpaces; + error(name_17, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(objectLiteralType), ts.declarationNameToString(name_17)); } } - // Spaces for anything not declared a 'default export'. - var nonDefaultExportedDeclarationSpaces = exportedDeclarationSpaces | nonExportedDeclarationSpaces; - var commonDeclarationSpacesForExportsAndLocals = exportedDeclarationSpaces & nonExportedDeclarationSpaces; - var commonDeclarationSpacesForDefaultAndNonDefault = defaultExportedDeclarationSpaces & nonDefaultExportedDeclarationSpaces; - if (commonDeclarationSpacesForExportsAndLocals || commonDeclarationSpacesForDefaultAndNonDefault) { - // declaration spaces for exported and non-exported declarations intersect - for (var _b = 0, _c = symbol.declarations; _b < _c.length; _b++) { - var d = _c[_b]; - var declarationSpaces = getDeclarationSpaces(d); - // Only error on the declarations that contributed to the intersecting spaces. - if (declarationSpaces & commonDeclarationSpacesForDefaultAndNonDefault) { - error(d.name, ts.Diagnostics.Merged_declaration_0_cannot_include_a_default_export_declaration_Consider_adding_a_separate_export_default_0_declaration_instead, ts.declarationNameToString(d.name)); - } - else if (declarationSpaces & commonDeclarationSpacesForExportsAndLocals) { - error(d.name, ts.Diagnostics.Individual_declarations_in_merged_declaration_0_must_be_all_exported_or_all_local, ts.declarationNameToString(d.name)); - } - } + else { + error(property, ts.Diagnostics.Property_assignment_expected); } - function getDeclarationSpaces(d) { - switch (d.kind) { - case 222 /* InterfaceDeclaration */: - return 2097152 /* ExportType */; - case 225 /* ModuleDeclaration */: - return ts.isAmbientModule(d) || ts.getModuleInstanceState(d) !== 0 /* NonInstantiated */ - ? 4194304 /* ExportNamespace */ | 1048576 /* ExportValue */ - : 4194304 /* ExportNamespace */; - case 221 /* ClassDeclaration */: - case 224 /* EnumDeclaration */: - return 2097152 /* ExportType */ | 1048576 /* ExportValue */; - case 229 /* ImportEqualsDeclaration */: - var result_2 = 0; - var target = resolveAlias(getSymbolOfNode(d)); - ts.forEach(target.declarations, function (d) { result_2 |= getDeclarationSpaces(d); }); - return result_2; - default: - return 1048576 /* ExportValue */; - } + } + function checkArrayLiteralAssignment(node, sourceType, contextualMapper) { + // This elementType will be used if the specific property corresponding to this index is not + // present (aka the tuple element property). This call also checks that the parentType is in + // fact an iterable or array (depending on target language). + var elementType = checkIteratedTypeOrElementType(sourceType, node, /*allowStringInput*/ false) || unknownType; + var elements = node.elements; + for (var i = 0; i < elements.length; i++) { + checkArrayLiteralDestructuringElementAssignment(node, sourceType, i, elementType, contextualMapper); } + return sourceType; } - function checkNonThenableType(type, location, message) { - type = getWidenedType(type); - if (!isTypeAny(type) && !isTypeNever(type) && isTypeAssignableTo(type, getGlobalThenableType())) { - if (location) { - if (!message) { - message = ts.Diagnostics.Operand_for_await_does_not_have_a_valid_callable_then_member; + function checkArrayLiteralDestructuringElementAssignment(node, sourceType, elementIndex, elementType, contextualMapper) { + var elements = node.elements; + var element = elements[elementIndex]; + if (element.kind !== 193 /* OmittedExpression */) { + if (element.kind !== 191 /* SpreadElementExpression */) { + var propName = "" + elementIndex; + var type = isTypeAny(sourceType) + ? sourceType + : isTupleLikeType(sourceType) + ? getTypeOfPropertyOfType(sourceType, propName) + : elementType; + if (type) { + return checkDestructuringAssignment(element, type, contextualMapper); + } + else { + // We still need to check element expression here because we may need to set appropriate flag on the expression + // such as NodeCheckFlags.LexicalThis on "this"expression. + checkExpression(element); + if (isTupleType(sourceType)) { + error(element, ts.Diagnostics.Tuple_type_0_with_length_1_cannot_be_assigned_to_tuple_with_length_2, typeToString(sourceType), getTypeReferenceArity(sourceType), elements.length); + } + else { + error(element, ts.Diagnostics.Type_0_has_no_property_1, typeToString(sourceType), propName); + } + } + } + else { + if (elementIndex < elements.length - 1) { + error(element, ts.Diagnostics.A_rest_element_must_be_last_in_an_array_destructuring_pattern); + } + else { + var restExpression = element.expression; + if (restExpression.kind === 187 /* BinaryExpression */ && restExpression.operatorToken.kind === 56 /* EqualsToken */) { + error(restExpression.operatorToken, ts.Diagnostics.A_rest_element_cannot_have_an_initializer); + } + else { + return checkDestructuringAssignment(restExpression, createArrayType(elementType), contextualMapper); + } } - error(location, message); } - return unknownType; } - return type; + return undefined; } - /** - * Gets the "promised type" of a promise. - * @param type The type of the promise. - * @remarks The "promised type" of a type is the type of the "value" parameter of the "onfulfilled" callback. - */ - function getPromisedType(promise) { - // - // { // promise - // then( // thenFunction - // onfulfilled: ( // onfulfilledParameterType - // value: T // valueParameterType - // ) => any - // ): any; - // } - // - if (isTypeAny(promise)) { - return undefined; - } - if (promise.flags & 131072 /* Reference */) { - if (promise.target === tryGetGlobalPromiseType() - || promise.target === getGlobalPromiseLikeType()) { - return promise.typeArguments[0]; + function checkDestructuringAssignment(exprOrAssignment, sourceType, contextualMapper) { + var target; + if (exprOrAssignment.kind === 254 /* ShorthandPropertyAssignment */) { + var prop = exprOrAssignment; + if (prop.objectAssignmentInitializer) { + // In strict null checking mode, if a default value of a non-undefined type is specified, remove + // undefined from the final type. + if (strictNullChecks && + !(getFalsyFlags(checkExpression(prop.objectAssignmentInitializer)) & 2048 /* Undefined */)) { + sourceType = getTypeWithFacts(sourceType, 131072 /* NEUndefined */); + } + checkBinaryLikeExpression(prop.name, prop.equalsToken, prop.objectAssignmentInitializer, contextualMapper); } + target = exprOrAssignment.name; } - var globalPromiseLikeType = getInstantiatedGlobalPromiseLikeType(); - if (globalPromiseLikeType === emptyObjectType || !isTypeAssignableTo(promise, globalPromiseLikeType)) { - return undefined; + else { + target = exprOrAssignment; } - var thenFunction = getTypeOfPropertyOfType(promise, "then"); - if (!thenFunction || isTypeAny(thenFunction)) { - return undefined; + if (target.kind === 187 /* BinaryExpression */ && target.operatorToken.kind === 56 /* EqualsToken */) { + checkBinaryExpression(target, contextualMapper); + target = target.left; } - var thenSignatures = getSignaturesOfType(thenFunction, 0 /* Call */); - if (thenSignatures.length === 0) { - return undefined; + if (target.kind === 171 /* ObjectLiteralExpression */) { + return checkObjectLiteralAssignment(target, sourceType, contextualMapper); } - var onfulfilledParameterType = getTypeWithFacts(getUnionType(ts.map(thenSignatures, getTypeOfFirstParameterOfSignature)), 131072 /* NEUndefined */); - if (isTypeAny(onfulfilledParameterType)) { - return undefined; + if (target.kind === 170 /* ArrayLiteralExpression */) { + return checkArrayLiteralAssignment(target, sourceType, contextualMapper); } - var onfulfilledParameterSignatures = getSignaturesOfType(onfulfilledParameterType, 0 /* Call */); - if (onfulfilledParameterSignatures.length === 0) { - return undefined; + return checkReferenceAssignment(target, sourceType, contextualMapper); + } + function checkReferenceAssignment(target, sourceType, contextualMapper) { + var targetType = checkExpression(target, contextualMapper); + if (checkReferenceExpression(target, ts.Diagnostics.Invalid_left_hand_side_of_assignment_expression, ts.Diagnostics.Left_hand_side_of_assignment_expression_cannot_be_a_constant_or_a_read_only_property)) { + checkTypeAssignableTo(sourceType, targetType, target, /*headMessage*/ undefined); } - return getUnionType(ts.map(onfulfilledParameterSignatures, getTypeOfFirstParameterOfSignature), /*subtypeReduction*/ true); - } - function getTypeOfFirstParameterOfSignature(signature) { - return signature.parameters.length > 0 ? getTypeAtPosition(signature, 0) : neverType; + return sourceType; } /** - * Gets the "awaited type" of a type. - * @param type The type to await. - * @remarks The "awaited type" of an expression is its "promised type" if the expression is a - * Promise-like type; otherwise, it is the type of the expression. This is used to reflect - * The runtime behavior of the `await` keyword. - */ - function getAwaitedType(type) { - return checkAwaitedType(type, /*location*/ undefined, /*message*/ undefined); + * This is a *shallow* check: An expression is side-effect-free if the + * evaluation of the expression *itself* cannot produce side effects. + * For example, x++ / 3 is side-effect free because the / operator + * does not have side effects. + * The intent is to "smell test" an expression for correctness in positions where + * its value is discarded (e.g. the left side of the comma operator). + */ + function isSideEffectFree(node) { + node = ts.skipParentheses(node); + switch (node.kind) { + case 69 /* Identifier */: + case 9 /* StringLiteral */: + case 10 /* RegularExpressionLiteral */: + case 176 /* TaggedTemplateExpression */: + case 189 /* TemplateExpression */: + case 11 /* NoSubstitutionTemplateLiteral */: + case 8 /* NumericLiteral */: + case 99 /* TrueKeyword */: + case 84 /* FalseKeyword */: + case 93 /* NullKeyword */: + case 135 /* UndefinedKeyword */: + case 179 /* FunctionExpression */: + case 192 /* ClassExpression */: + case 180 /* ArrowFunction */: + case 170 /* ArrayLiteralExpression */: + case 171 /* ObjectLiteralExpression */: + case 182 /* TypeOfExpression */: + case 196 /* NonNullExpression */: + case 242 /* JsxSelfClosingElement */: + case 241 /* JsxElement */: + return true; + case 188 /* ConditionalExpression */: + return isSideEffectFree(node.whenTrue) && + isSideEffectFree(node.whenFalse); + case 187 /* BinaryExpression */: + if (ts.isAssignmentOperator(node.operatorToken.kind)) { + return false; + } + return isSideEffectFree(node.left) && + isSideEffectFree(node.right); + case 185 /* PrefixUnaryExpression */: + case 186 /* PostfixUnaryExpression */: + // Unary operators ~, !, +, and - have no side effects. + // The rest do. + switch (node.operator) { + case 49 /* ExclamationToken */: + case 35 /* PlusToken */: + case 36 /* MinusToken */: + case 50 /* TildeToken */: + return true; + } + return false; + // Some forms listed here for clarity + case 183 /* VoidExpression */: // Explicit opt-out + case 177 /* TypeAssertionExpression */: // Not SEF, but can produce useful type warnings + case 195 /* AsExpression */: // Not SEF, but can produce useful type warnings + default: + return false; + } } - function checkAwaitedType(type, location, message) { - return checkAwaitedTypeWorker(type); - function checkAwaitedTypeWorker(type) { - if (type.flags & 524288 /* Union */) { - var types = []; - for (var _i = 0, _a = type.types; _i < _a.length; _i++) { - var constituentType = _a[_i]; - types.push(checkAwaitedTypeWorker(constituentType)); + function isTypeEqualityComparableTo(source, target) { + return (target.flags & 6144 /* Nullable */) !== 0 || isTypeComparableTo(source, target); + } + function getBestChoiceType(type1, type2) { + var firstAssignableToSecond = isTypeAssignableTo(type1, type2); + var secondAssignableToFirst = isTypeAssignableTo(type2, type1); + return secondAssignableToFirst && !firstAssignableToSecond ? type1 : + firstAssignableToSecond && !secondAssignableToFirst ? type2 : + getUnionType([type1, type2], /*subtypeReduction*/ true); + } + function checkBinaryExpression(node, contextualMapper) { + return checkBinaryLikeExpression(node.left, node.operatorToken, node.right, contextualMapper, node); + } + function checkBinaryLikeExpression(left, operatorToken, right, contextualMapper, errorNode) { + var operator = operatorToken.kind; + if (operator === 56 /* EqualsToken */ && (left.kind === 171 /* ObjectLiteralExpression */ || left.kind === 170 /* ArrayLiteralExpression */)) { + return checkDestructuringAssignment(left, checkExpression(right, contextualMapper), contextualMapper); + } + var leftType = checkExpression(left, contextualMapper); + var rightType = checkExpression(right, contextualMapper); + switch (operator) { + case 37 /* AsteriskToken */: + case 38 /* AsteriskAsteriskToken */: + case 59 /* AsteriskEqualsToken */: + case 60 /* AsteriskAsteriskEqualsToken */: + case 39 /* SlashToken */: + case 61 /* SlashEqualsToken */: + case 40 /* PercentToken */: + case 62 /* PercentEqualsToken */: + case 36 /* MinusToken */: + case 58 /* MinusEqualsToken */: + case 43 /* LessThanLessThanToken */: + case 63 /* LessThanLessThanEqualsToken */: + case 44 /* GreaterThanGreaterThanToken */: + case 64 /* GreaterThanGreaterThanEqualsToken */: + case 45 /* GreaterThanGreaterThanGreaterThanToken */: + case 65 /* GreaterThanGreaterThanGreaterThanEqualsToken */: + case 47 /* BarToken */: + case 67 /* BarEqualsToken */: + case 48 /* CaretToken */: + case 68 /* CaretEqualsToken */: + case 46 /* AmpersandToken */: + case 66 /* AmpersandEqualsToken */: + if (leftType === silentNeverType || rightType === silentNeverType) { + return silentNeverType; } - return getUnionType(types, /*subtypeReduction*/ true); - } - else { - var promisedType = getPromisedType(type); - if (promisedType === undefined) { - // The type was not a PromiseLike, so it could not be unwrapped any further. - // As long as the type does not have a callable "then" property, it is - // safe to return the type; otherwise, an error will have been reported in - // the call to checkNonThenableType and we will return unknownType. - // - // An example of a non-promise "thenable" might be: - // - // await { then(): void {} } - // - // The "thenable" does not match the minimal definition for a PromiseLike. When - // a Promise/A+-compatible or ES6 promise tries to adopt this value, the promise - // will never settle. We treat this as an error to help flag an early indicator - // of a runtime problem. If the user wants to return this value from an async - // function, they would need to wrap it in some other value. If they want it to - // be treated as a promise, they can cast to . - return checkNonThenableType(type, location, message); + // TypeScript 1.0 spec (April 2014): 4.19.1 + // These operators require their operands to be of type Any, the Number primitive type, + // or an enum type. Operands of an enum type are treated + // as having the primitive type Number. If one operand is the null or undefined value, + // it is treated as having the type of the other operand. + // The result is always of the Number primitive type. + if (leftType.flags & 6144 /* Nullable */) + leftType = rightType; + if (rightType.flags & 6144 /* Nullable */) + rightType = leftType; + leftType = getNonNullableType(leftType); + rightType = getNonNullableType(rightType); + var suggestedOperator = void 0; + // if a user tries to apply a bitwise operator to 2 boolean operands + // try and return them a helpful suggestion + if ((leftType.flags & 136 /* BooleanLike */) && + (rightType.flags & 136 /* BooleanLike */) && + (suggestedOperator = getSuggestedBooleanOperator(operatorToken.kind)) !== undefined) { + error(errorNode || operatorToken, ts.Diagnostics.The_0_operator_is_not_allowed_for_boolean_types_Consider_using_1_instead, ts.tokenToString(operatorToken.kind), ts.tokenToString(suggestedOperator)); } else { - if (type.id === promisedType.id || ts.indexOf(awaitedTypeStack, promisedType.id) >= 0) { - // We have a bad actor in the form of a promise whose promised type is - // the same promise type, or a mutually recursive promise. Return the - // unknown type as we cannot guess the shape. If this were the actual - // case in the JavaScript, this Promise would never resolve. - // - // An example of a bad actor with a singly-recursive promise type might - // be: - // - // interface BadPromise { - // then( - // onfulfilled: (value: BadPromise) => any, - // onrejected: (error: any) => any): BadPromise; - // } - // - // The above interface will pass the PromiseLike check, and return a - // promised type of `BadPromise`. Since this is a self reference, we - // don't want to keep recursing ad infinitum. - // - // An example of a bad actor in the form of a mutually-recursive - // promise type might be: - // - // interface BadPromiseA { - // then( - // onfulfilled: (value: BadPromiseB) => any, - // onrejected: (error: any) => any): BadPromiseB; - // } - // - // interface BadPromiseB { - // then( - // onfulfilled: (value: BadPromiseA) => any, - // onrejected: (error: any) => any): BadPromiseA; - // } - // - if (location) { - error(location, ts.Diagnostics._0_is_referenced_directly_or_indirectly_in_the_fulfillment_callback_of_its_own_then_method, symbolToString(type.symbol)); - } - return unknownType; + // otherwise just check each operand separately and report errors as normal + var leftOk = checkArithmeticOperandType(left, leftType, ts.Diagnostics.The_left_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_or_an_enum_type); + var rightOk = checkArithmeticOperandType(right, rightType, ts.Diagnostics.The_right_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_or_an_enum_type); + if (leftOk && rightOk) { + checkAssignmentOperator(numberType); } - // Keep track of the type we're about to unwrap to avoid bad recursive promise types. - // See the comments above for more information. - awaitedTypeStack.push(type.id); - var awaitedType = checkAwaitedTypeWorker(promisedType); - awaitedTypeStack.pop(); - return awaitedType; } + return numberType; + case 35 /* PlusToken */: + case 57 /* PlusEqualsToken */: + if (leftType === silentNeverType || rightType === silentNeverType) { + return silentNeverType; + } + // TypeScript 1.0 spec (April 2014): 4.19.2 + // The binary + operator requires both operands to be of the Number primitive type or an enum type, + // or at least one of the operands to be of type Any or the String primitive type. + // If one operand is the null or undefined value, it is treated as having the type of the other operand. + if (leftType.flags & 6144 /* Nullable */) + leftType = rightType; + if (rightType.flags & 6144 /* Nullable */) + rightType = leftType; + leftType = getNonNullableType(leftType); + rightType = getNonNullableType(rightType); + var resultType = void 0; + if (isTypeOfKind(leftType, 340 /* NumberLike */) && isTypeOfKind(rightType, 340 /* NumberLike */)) { + // Operands of an enum type are treated as having the primitive type Number. + // If both operands are of the Number primitive type, the result is of the Number primitive type. + resultType = numberType; + } + else { + if (isTypeOfKind(leftType, 34 /* StringLike */) || isTypeOfKind(rightType, 34 /* StringLike */)) { + // If one or both operands are of the String primitive type, the result is of the String primitive type. + resultType = stringType; + } + else if (isTypeAny(leftType) || isTypeAny(rightType)) { + // Otherwise, the result is of type Any. + // NOTE: unknown type here denotes error type. Old compiler treated this case as any type so do we. + resultType = leftType === unknownType || rightType === unknownType ? unknownType : anyType; + } + // Symbols are not allowed at all in arithmetic expressions + if (resultType && !checkForDisallowedESSymbolOperand(operator)) { + return resultType; + } + } + if (!resultType) { + reportOperatorError(); + return anyType; + } + if (operator === 57 /* PlusEqualsToken */) { + checkAssignmentOperator(resultType); + } + return resultType; + case 25 /* LessThanToken */: + case 27 /* GreaterThanToken */: + case 28 /* LessThanEqualsToken */: + case 29 /* GreaterThanEqualsToken */: + if (checkForDisallowedESSymbolOperand(operator)) { + if (!isTypeComparableTo(leftType, rightType) && !isTypeComparableTo(rightType, leftType)) { + reportOperatorError(); + } + } + return booleanType; + case 30 /* EqualsEqualsToken */: + case 31 /* ExclamationEqualsToken */: + case 32 /* EqualsEqualsEqualsToken */: + case 33 /* ExclamationEqualsEqualsToken */: + var leftIsLiteral = isLiteralType(leftType); + var rightIsLiteral = isLiteralType(rightType); + if (!leftIsLiteral || !rightIsLiteral) { + leftType = leftIsLiteral ? getBaseTypeOfLiteralType(leftType) : leftType; + rightType = rightIsLiteral ? getBaseTypeOfLiteralType(rightType) : rightType; + } + if (!isTypeEqualityComparableTo(leftType, rightType) && !isTypeEqualityComparableTo(rightType, leftType)) { + reportOperatorError(); + } + return booleanType; + case 91 /* InstanceOfKeyword */: + return checkInstanceOfExpression(left, right, leftType, rightType); + case 90 /* InKeyword */: + return checkInExpression(left, right, leftType, rightType); + case 51 /* AmpersandAmpersandToken */: + return getTypeFacts(leftType) & 1048576 /* Truthy */ ? + includeFalsyTypes(rightType, getFalsyFlags(strictNullChecks ? leftType : getBaseTypeOfLiteralType(rightType))) : + leftType; + case 52 /* BarBarToken */: + return getTypeFacts(leftType) & 2097152 /* Falsy */ ? + getBestChoiceType(removeDefinitelyFalsyTypes(leftType), rightType) : + leftType; + case 56 /* EqualsToken */: + checkAssignmentOperator(rightType); + return getRegularTypeOfObjectLiteral(rightType); + case 24 /* CommaToken */: + if (!compilerOptions.allowUnreachableCode && isSideEffectFree(left)) { + error(left, ts.Diagnostics.Left_side_of_comma_operator_is_unused_and_has_no_side_effects); + } + return rightType; + } + // Return true if there was no error, false if there was an error. + function checkForDisallowedESSymbolOperand(operator) { + var offendingSymbolOperand = maybeTypeOfKind(leftType, 512 /* ESSymbol */) ? left : + maybeTypeOfKind(rightType, 512 /* ESSymbol */) ? right : + undefined; + if (offendingSymbolOperand) { + error(offendingSymbolOperand, ts.Diagnostics.The_0_operator_cannot_be_applied_to_type_symbol, ts.tokenToString(operator)); + return false; } + return true; } - } - /** - * Checks that the return type provided is an instantiation of the global Promise type - * and returns the awaited type of the return type. - * - * @param returnType The return type of a FunctionLikeDeclaration - * @param location The node on which to report the error. - */ - function checkCorrectPromiseType(returnType, location, diagnostic, typeName) { - if (returnType === unknownType) { - // The return type already had some other error, so we ignore and return - // the unknown type. - return unknownType; + function getSuggestedBooleanOperator(operator) { + switch (operator) { + case 47 /* BarToken */: + case 67 /* BarEqualsToken */: + return 52 /* BarBarToken */; + case 48 /* CaretToken */: + case 68 /* CaretEqualsToken */: + return 33 /* ExclamationEqualsEqualsToken */; + case 46 /* AmpersandToken */: + case 66 /* AmpersandEqualsToken */: + return 51 /* AmpersandAmpersandToken */; + default: + return undefined; + } } - var globalPromiseType = getGlobalPromiseType(); - if (globalPromiseType === emptyGenericType - || globalPromiseType === getTargetType(returnType)) { - // Either we couldn't resolve the global promise type, which would have already - // reported an error, or we could resolve it and the return type is a valid type - // reference to the global type. In either case, we return the awaited type for - // the return type. - return checkAwaitedType(returnType, location, ts.Diagnostics.An_async_function_or_method_must_have_a_valid_awaitable_return_type); + function checkAssignmentOperator(valueType) { + if (produceDiagnostics && operator >= 56 /* FirstAssignment */ && operator <= 68 /* LastAssignment */) { + // TypeScript 1.0 spec (April 2014): 4.17 + // An assignment of the form + // VarExpr = ValueExpr + // requires VarExpr to be classified as a reference + // A compound assignment furthermore requires VarExpr to be classified as a reference (section 4.1) + // and the type of the non - compound operation to be assignable to the type of VarExpr. + var ok = checkReferenceExpression(left, ts.Diagnostics.Invalid_left_hand_side_of_assignment_expression, ts.Diagnostics.Left_hand_side_of_assignment_expression_cannot_be_a_constant_or_a_read_only_property); + // Use default messages + if (ok) { + // to avoid cascading errors check assignability only if 'isReference' check succeeded and no errors were reported + checkTypeAssignableTo(valueType, leftType, left, /*headMessage*/ undefined); + } + } } - // The promise type was not a valid type reference to the global promise type, so we - // report an error and return the unknown type. - error(location, diagnostic, typeName); - return unknownType; - } - /** - * Checks the return type of an async function to ensure it is a compatible - * Promise implementation. - * @param node The signature to check - * @param returnType The return type for the function - * @remarks - * This checks that an async function has a valid Promise-compatible return type, - * and returns the *awaited type* of the promise. An async function has a valid - * Promise-compatible return type if the resolved value of the return type has a - * construct signature that takes in an `initializer` function that in turn supplies - * a `resolve` function as one of its arguments and results in an object with a - * callable `then` signature. - */ - function checkAsyncFunctionReturnType(node) { - if (languageVersion >= 2 /* ES6 */) { - var returnType = getTypeFromTypeNode(node.type); - return checkCorrectPromiseType(returnType, node.type, ts.Diagnostics.The_return_type_of_an_async_function_or_method_must_be_the_global_Promise_T_type); + function reportOperatorError() { + error(errorNode || operatorToken, ts.Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2, ts.tokenToString(operatorToken.kind), typeToString(leftType), typeToString(rightType)); } - var globalPromiseConstructorLikeType = getGlobalPromiseConstructorLikeType(); - if (globalPromiseConstructorLikeType === emptyObjectType) { - // If we couldn't resolve the global PromiseConstructorLike type we cannot verify - // compatibility with __awaiter. - return unknownType; + } + function isYieldExpressionInClass(node) { + var current = node; + var parent = node.parent; + while (parent) { + if (ts.isFunctionLike(parent) && current === parent.body) { + return false; + } + else if (ts.isClassLike(current)) { + return true; + } + current = parent; + parent = parent.parent; } - // As part of our emit for an async function, we will need to emit the entity name of - // the return type annotation as an expression. To meet the necessary runtime semantics - // for __awaiter, we must also check that the type of the declaration (e.g. the static - // side or "constructor" of the promise type) is compatible `PromiseConstructorLike`. - // - // An example might be (from lib.es6.d.ts): - // - // interface Promise { ... } - // interface PromiseConstructor { - // new (...): Promise; - // } - // declare var Promise: PromiseConstructor; - // - // When an async function declares a return type annotation of `Promise`, we - // need to get the type of the `Promise` variable declaration above, which would - // be `PromiseConstructor`. - // - // The same case applies to a class: - // - // declare class Promise { - // constructor(...); - // then(...): Promise; - // } - // - // When we get the type of the `Promise` symbol here, we get the type of the static - // side of the `Promise` class, which would be `{ new (...): Promise }`. - var promiseType = getTypeFromTypeNode(node.type); - if (promiseType === unknownType && compilerOptions.isolatedModules) { - // If we are compiling with isolatedModules, we may not be able to resolve the - // type as a value. As such, we will just return unknownType; - return unknownType; + return false; + } + function checkYieldExpression(node) { + // Grammar checking + if (produceDiagnostics) { + if (!(node.flags & 65536 /* YieldContext */) || isYieldExpressionInClass(node)) { + grammarErrorOnFirstToken(node, ts.Diagnostics.A_yield_expression_is_only_allowed_in_a_generator_body); + } + if (isInParameterInitializerBeforeContainingFunction(node)) { + error(node, ts.Diagnostics.yield_expressions_cannot_be_used_in_a_parameter_initializer); + } } - var promiseConstructor = getNodeLinks(node.type).resolvedSymbol; - if (!promiseConstructor || !symbolIsValue(promiseConstructor)) { - // try to fall back to global promise type. - var typeName = promiseConstructor - ? symbolToString(promiseConstructor) - : typeToString(promiseType); - return checkCorrectPromiseType(promiseType, node.type, ts.Diagnostics.Type_0_is_not_a_valid_async_function_return_type, typeName); + if (node.expression) { + var func = ts.getContainingFunction(node); + // If the user's code is syntactically correct, the func should always have a star. After all, + // we are in a yield context. + if (func && func.asteriskToken) { + var expressionType = checkExpressionCached(node.expression, /*contextualMapper*/ undefined); + var expressionElementType = void 0; + var nodeIsYieldStar = !!node.asteriskToken; + if (nodeIsYieldStar) { + expressionElementType = checkElementTypeOfIterable(expressionType, node.expression); + } + // There is no point in doing an assignability check if the function + // has no explicit return type because the return type is directly computed + // from the yield expressions. + if (func.type) { + var signatureElementType = getElementTypeOfIterableIterator(getTypeFromTypeNode(func.type)) || anyType; + if (nodeIsYieldStar) { + checkTypeAssignableTo(expressionElementType, signatureElementType, node.expression, /*headMessage*/ undefined); + } + else { + checkTypeAssignableTo(expressionType, signatureElementType, node.expression, /*headMessage*/ undefined); + } + } + } } - // If the Promise constructor, resolved locally, is an alias symbol we should mark it as referenced. - checkReturnTypeAnnotationAsExpression(node); - // Validate the promise constructor type. - var promiseConstructorType = getTypeOfSymbol(promiseConstructor); - if (!checkTypeAssignableTo(promiseConstructorType, globalPromiseConstructorLikeType, node.type, ts.Diagnostics.Type_0_is_not_a_valid_async_function_return_type)) { - return unknownType; + // Both yield and yield* expressions have type 'any' + return anyType; + } + function checkConditionalExpression(node, contextualMapper) { + checkExpression(node.condition); + var type1 = checkExpression(node.whenTrue, contextualMapper); + var type2 = checkExpression(node.whenFalse, contextualMapper); + return getBestChoiceType(type1, type2); + } + function checkLiteralExpression(node) { + if (node.kind === 8 /* NumericLiteral */) { + checkGrammarNumericLiteral(node); } - // Verify there is no local declaration that could collide with the promise constructor. - var promiseName = ts.getEntityNameFromTypeNode(node.type); - var promiseNameOrNamespaceRoot = getFirstIdentifier(promiseName); - var rootSymbol = getSymbol(node.locals, promiseNameOrNamespaceRoot.text, 107455 /* Value */); - if (rootSymbol) { - error(rootSymbol.valueDeclaration, ts.Diagnostics.Duplicate_identifier_0_Compiler_uses_declaration_1_to_support_async_functions, promiseNameOrNamespaceRoot.text, getFullyQualifiedName(promiseConstructor)); - return unknownType; + switch (node.kind) { + case 9 /* StringLiteral */: + return getFreshTypeOfLiteralType(getLiteralTypeForText(32 /* StringLiteral */, node.text)); + case 8 /* NumericLiteral */: + return getFreshTypeOfLiteralType(getLiteralTypeForText(64 /* NumberLiteral */, node.text)); + case 99 /* TrueKeyword */: + return trueType; + case 84 /* FalseKeyword */: + return falseType; } - // Get and return the awaited type of the return type. - return checkAwaitedType(promiseType, node, ts.Diagnostics.An_async_function_or_method_must_have_a_valid_awaitable_return_type); } - /** Check a decorator */ - function checkDecorator(node) { - var signature = getResolvedSignature(node); - var returnType = getReturnTypeOfSignature(signature); - if (returnType.flags & 1 /* Any */) { - return; - } - var expectedReturnType; - var headMessage = getDiagnosticHeadMessageForDecoratorResolution(node); - var errorInfo; - switch (node.parent.kind) { - case 221 /* ClassDeclaration */: - var classSymbol = getSymbolOfNode(node.parent); - var classConstructorType = getTypeOfSymbol(classSymbol); - expectedReturnType = getUnionType([classConstructorType, voidType]); - break; - case 142 /* Parameter */: - expectedReturnType = voidType; - errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.The_return_type_of_a_parameter_decorator_function_must_be_either_void_or_any); - break; - case 145 /* PropertyDeclaration */: - expectedReturnType = voidType; - errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.The_return_type_of_a_property_decorator_function_must_be_either_void_or_any); - break; - case 147 /* MethodDeclaration */: - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - var methodType = getTypeOfNode(node.parent); - var descriptorType = createTypedPropertyDescriptorType(methodType); - expectedReturnType = getUnionType([descriptorType, voidType]); - break; + function checkTemplateExpression(node) { + // We just want to check each expressions, but we are unconcerned with + // the type of each expression, as any value may be coerced into a string. + // It is worth asking whether this is what we really want though. + // A place where we actually *are* concerned with the expressions' types are + // in tagged templates. + ts.forEach(node.templateSpans, function (templateSpan) { + checkExpression(templateSpan.expression); + }); + return stringType; + } + function checkExpressionWithContextualType(node, contextualType, contextualMapper) { + var saveContextualType = node.contextualType; + node.contextualType = contextualType; + var result = checkExpression(node, contextualMapper); + node.contextualType = saveContextualType; + return result; + } + function checkExpressionCached(node, contextualMapper) { + var links = getNodeLinks(node); + if (!links.resolvedType) { + // When computing a type that we're going to cache, we need to ignore any ongoing control flow + // analysis because variables may have transient types in indeterminable states. Moving flowLoopStart + // to the top of the stack ensures all transient types are computed from a known point. + var saveFlowLoopStart = flowLoopStart; + flowLoopStart = flowLoopCount; + links.resolvedType = checkExpression(node, contextualMapper); + flowLoopStart = saveFlowLoopStart; } - checkTypeAssignableTo(returnType, expectedReturnType, node, headMessage, errorInfo); + return links.resolvedType; } - /** Checks a type reference node as an expression. */ - function checkTypeNodeAsExpression(node) { - // When we are emitting type metadata for decorators, we need to try to check the type - // as if it were an expression so that we can emit the type in a value position when we - // serialize the type metadata. - if (node && node.kind === 155 /* TypeReference */) { - var root = getFirstIdentifier(node.typeName); - var meaning = root.parent.kind === 155 /* TypeReference */ ? 793064 /* Type */ : 1920 /* Namespace */; - // Resolve type so we know which symbol is referenced - var rootSymbol = resolveName(root, root.text, meaning | 8388608 /* Alias */, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined); - // Resolved symbol is alias - if (rootSymbol && rootSymbol.flags & 8388608 /* Alias */) { - var aliasTarget = resolveAlias(rootSymbol); - // If alias has value symbol - mark alias as referenced - if (aliasTarget.flags & 107455 /* Value */ && !isConstEnumOrConstEnumOnlyModule(resolveAlias(rootSymbol))) { - markAliasSymbolAsReferenced(rootSymbol); + function isTypeAssertion(node) { + node = skipParenthesizedNodes(node); + return node.kind === 177 /* TypeAssertionExpression */ || node.kind === 195 /* AsExpression */; + } + function checkDeclarationInitializer(declaration) { + var type = checkExpressionCached(declaration.initializer); + return ts.getCombinedNodeFlags(declaration) & 2 /* Const */ || + ts.getCombinedModifierFlags(declaration) & 64 /* Readonly */ || + isTypeAssertion(declaration.initializer) ? type : getWidenedLiteralType(type); + } + function isLiteralContextualType(contextualType) { + if (contextualType) { + if (contextualType.flags & 16384 /* TypeParameter */) { + var apparentType = getApparentTypeOfTypeParameter(contextualType); + // If the type parameter is constrained to the base primitive type we're checking for, + // consider this a literal context. For example, given a type parameter 'T extends string', + // this causes us to infer string literal types for T. + if (apparentType.flags & (2 /* String */ | 4 /* Number */ | 8 /* Boolean */ | 16 /* Enum */)) { + return true; } + contextualType = apparentType; } + return maybeTypeOfKind(contextualType, 480 /* Literal */); } + return false; } - /** - * Checks the type annotation of an accessor declaration or property declaration as - * an expression if it is a type reference to a type with a value declaration. - */ - function checkTypeAnnotationAsExpression(node) { - checkTypeNodeAsExpression(node.type); + function checkExpressionForMutableLocation(node, contextualMapper) { + var type = checkExpression(node, contextualMapper); + return isTypeAssertion(node) || isLiteralContextualType(getContextualType(node)) ? type : getWidenedLiteralType(type); } - function checkReturnTypeAnnotationAsExpression(node) { - checkTypeNodeAsExpression(node.type); + function checkPropertyAssignment(node, contextualMapper) { + // Do not use hasDynamicName here, because that returns false for well known symbols. + // We want to perform checkComputedPropertyName for all computed properties, including + // well known symbols. + if (node.name.kind === 140 /* ComputedPropertyName */) { + checkComputedPropertyName(node.name); + } + return checkExpressionForMutableLocation(node.initializer, contextualMapper); } - /** Checks the type annotation of the parameters of a function/method or the constructor of a class as expressions */ - function checkParameterTypeAnnotationsAsExpressions(node) { - // ensure all type annotations with a value declaration are checked as an expression - for (var _i = 0, _a = node.parameters; _i < _a.length; _i++) { - var parameter = _a[_i]; - checkTypeAnnotationAsExpression(parameter); + function checkObjectLiteralMethod(node, contextualMapper) { + // Grammar checking + checkGrammarMethod(node); + // Do not use hasDynamicName here, because that returns false for well known symbols. + // We want to perform checkComputedPropertyName for all computed properties, including + // well known symbols. + if (node.name.kind === 140 /* ComputedPropertyName */) { + checkComputedPropertyName(node.name); } + var uninstantiatedType = checkFunctionExpressionOrObjectLiteralMethod(node, contextualMapper); + return instantiateTypeWithSingleGenericCallSignature(node, uninstantiatedType, contextualMapper); } - /** Check the decorators of a node */ - function checkDecorators(node) { - if (!node.decorators) { - return; + function instantiateTypeWithSingleGenericCallSignature(node, type, contextualMapper) { + if (isInferentialContext(contextualMapper)) { + var signature = getSingleCallSignature(type); + if (signature && signature.typeParameters) { + var contextualType = getApparentTypeOfContextualType(node); + if (contextualType) { + var contextualSignature = getSingleCallSignature(contextualType); + if (contextualSignature && !contextualSignature.typeParameters) { + return getOrCreateTypeFromSignature(instantiateSignatureInContextOf(signature, contextualSignature, contextualMapper)); + } + } + } } - // skip this check for nodes that cannot have decorators. These should have already had an error reported by - // checkGrammarDecorators. - if (!ts.nodeCanBeDecorated(node)) { - return; + return type; + } + // Checks an expression and returns its type. The contextualMapper parameter serves two purposes: When + // contextualMapper is not undefined and not equal to the identityMapper function object it indicates that the + // expression is being inferentially typed (section 4.15.2 in spec) and provides the type mapper to use in + // conjunction with the generic contextual type. When contextualMapper is equal to the identityMapper function + // object, it serves as an indicator that all contained function and arrow expressions should be considered to + // have the wildcard function type; this form of type check is used during overload resolution to exclude + // contextually typed function and arrow expressions in the initial phase. + function checkExpression(node, contextualMapper) { + var type; + if (node.kind === 139 /* QualifiedName */) { + type = checkQualifiedName(node); } - if (!compilerOptions.experimentalDecorators) { - error(node, ts.Diagnostics.Experimental_support_for_decorators_is_a_feature_that_is_subject_to_change_in_a_future_release_Set_the_experimentalDecorators_option_to_remove_this_warning); + else { + var uninstantiatedType = checkExpressionWorker(node, contextualMapper); + type = instantiateTypeWithSingleGenericCallSignature(node, uninstantiatedType, contextualMapper); } - if (compilerOptions.emitDecoratorMetadata) { - // we only need to perform these checks if we are emitting serialized type metadata for the target of a decorator. - switch (node.kind) { - case 221 /* ClassDeclaration */: - var constructor = ts.getFirstConstructorWithBody(node); - if (constructor) { - checkParameterTypeAnnotationsAsExpressions(constructor); - } - break; - case 147 /* MethodDeclaration */: - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - checkParameterTypeAnnotationsAsExpressions(node); - checkReturnTypeAnnotationAsExpression(node); - break; - case 145 /* PropertyDeclaration */: - case 142 /* Parameter */: - checkTypeAnnotationAsExpression(node); - break; + if (isConstEnumObjectType(type)) { + // enum object type for const enums are only permitted in: + // - 'left' in property access + // - 'object' in indexed access + // - target in rhs of import statement + var ok = (node.parent.kind === 172 /* PropertyAccessExpression */ && node.parent.expression === node) || + (node.parent.kind === 173 /* ElementAccessExpression */ && node.parent.expression === node) || + ((node.kind === 69 /* Identifier */ || node.kind === 139 /* QualifiedName */) && isInRightSideOfImportOrExportAssignment(node)); + if (!ok) { + error(node, ts.Diagnostics.const_enums_can_only_be_used_in_property_or_index_access_expressions_or_the_right_hand_side_of_an_import_declaration_or_export_assignment); } } - ts.forEach(node.decorators, checkDecorator); + return type; } - function checkFunctionDeclaration(node) { - if (produceDiagnostics) { - checkFunctionOrMethodDeclaration(node) || checkGrammarForGenerator(node); - checkCollisionWithCapturedSuperVariable(node, node.name); - checkCollisionWithCapturedThisVariable(node, node.name); - checkCollisionWithRequireExportsInGeneratedCode(node, node.name); - checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); + function checkExpressionWorker(node, contextualMapper) { + switch (node.kind) { + case 69 /* Identifier */: + return checkIdentifier(node); + case 97 /* ThisKeyword */: + return checkThisExpression(node); + case 95 /* SuperKeyword */: + return checkSuperExpression(node); + case 93 /* NullKeyword */: + return nullWideningType; + case 9 /* StringLiteral */: + case 8 /* NumericLiteral */: + case 99 /* TrueKeyword */: + case 84 /* FalseKeyword */: + return checkLiteralExpression(node); + case 189 /* TemplateExpression */: + return checkTemplateExpression(node); + case 11 /* NoSubstitutionTemplateLiteral */: + return stringType; + case 10 /* RegularExpressionLiteral */: + return globalRegExpType; + case 170 /* ArrayLiteralExpression */: + return checkArrayLiteral(node, contextualMapper); + case 171 /* ObjectLiteralExpression */: + return checkObjectLiteral(node, contextualMapper); + case 172 /* PropertyAccessExpression */: + return checkPropertyAccessExpression(node); + case 173 /* ElementAccessExpression */: + return checkIndexedAccess(node); + case 174 /* CallExpression */: + case 175 /* NewExpression */: + return checkCallExpression(node); + case 176 /* TaggedTemplateExpression */: + return checkTaggedTemplateExpression(node); + case 178 /* ParenthesizedExpression */: + return checkExpression(node.expression, contextualMapper); + case 192 /* ClassExpression */: + return checkClassExpression(node); + case 179 /* FunctionExpression */: + case 180 /* ArrowFunction */: + return checkFunctionExpressionOrObjectLiteralMethod(node, contextualMapper); + case 182 /* TypeOfExpression */: + return checkTypeOfExpression(node); + case 177 /* TypeAssertionExpression */: + case 195 /* AsExpression */: + return checkAssertion(node); + case 196 /* NonNullExpression */: + return checkNonNullAssertion(node); + case 181 /* DeleteExpression */: + return checkDeleteExpression(node); + case 183 /* VoidExpression */: + return checkVoidExpression(node); + case 184 /* AwaitExpression */: + return checkAwaitExpression(node); + case 185 /* PrefixUnaryExpression */: + return checkPrefixUnaryExpression(node); + case 186 /* PostfixUnaryExpression */: + return checkPostfixUnaryExpression(node); + case 187 /* BinaryExpression */: + return checkBinaryExpression(node, contextualMapper); + case 188 /* ConditionalExpression */: + return checkConditionalExpression(node, contextualMapper); + case 191 /* SpreadElementExpression */: + return checkSpreadElementExpression(node, contextualMapper); + case 193 /* OmittedExpression */: + return undefinedWideningType; + case 190 /* YieldExpression */: + return checkYieldExpression(node); + case 248 /* JsxExpression */: + return checkJsxExpression(node); + case 241 /* JsxElement */: + return checkJsxElement(node); + case 242 /* JsxSelfClosingElement */: + return checkJsxSelfClosingElement(node); + case 243 /* JsxOpeningElement */: + ts.Debug.fail("Shouldn't ever directly check a JsxOpeningElement"); } + return unknownType; } - function checkFunctionOrMethodDeclaration(node) { - checkDecorators(node); - checkSignatureDeclaration(node); - var isAsync = ts.isAsyncFunctionLike(node); - // Do not use hasDynamicName here, because that returns false for well known symbols. - // We want to perform checkComputedPropertyName for all computed properties, including - // well known symbols. - if (node.name && node.name.kind === 140 /* ComputedPropertyName */) { - // This check will account for methods in class/interface declarations, - // as well as accessors in classes/object literals - checkComputedPropertyName(node.name); + // DECLARATION AND STATEMENT TYPE CHECKING + function checkTypeParameter(node) { + // Grammar Checking + if (node.expression) { + grammarErrorOnFirstToken(node.expression, ts.Diagnostics.Type_expected); } - if (!ts.hasDynamicName(node)) { - // first we want to check the local symbol that contain this declaration - // - if node.localSymbol !== undefined - this is current declaration is exported and localSymbol points to the local symbol - // - if node.localSymbol === undefined - this node is non-exported so we can just pick the result of getSymbolOfNode - var symbol = getSymbolOfNode(node); - var localSymbol = node.localSymbol || symbol; - // Since the javascript won't do semantic analysis like typescript, - // if the javascript file comes before the typescript file and both contain same name functions, - // checkFunctionOrConstructorSymbol wouldn't be called if we didnt ignore javascript function. - var firstDeclaration = ts.forEach(localSymbol.declarations, - // Get first non javascript function declaration - function (declaration) { return declaration.kind === node.kind && !ts.isSourceFileJavaScript(ts.getSourceFileOfNode(declaration)) ? - declaration : undefined; }); - // Only type check the symbol once - if (node === firstDeclaration) { - checkFunctionOrConstructorSymbol(localSymbol); - } - if (symbol.parent) { - // run check once for the first declaration - if (ts.getDeclarationOfKind(symbol, node.kind) === node) { - // run check on export symbol to check that modifiers agree across all exported declarations - checkFunctionOrConstructorSymbol(symbol); - } + checkSourceElement(node.constraint); + getConstraintOfTypeParameter(getDeclaredTypeOfTypeParameter(getSymbolOfNode(node))); + if (produceDiagnostics) { + checkTypeNameIsReserved(node.name, ts.Diagnostics.Type_parameter_name_cannot_be_0); + } + } + function checkParameter(node) { + // Grammar checking + // It is a SyntaxError if the Identifier "eval" or the Identifier "arguments" occurs as the + // Identifier in a PropertySetParameterList of a PropertyAssignment that is contained in strict code + // or if its FunctionBody is strict code(11.1.5). + // Grammar checking + checkGrammarDecorators(node) || checkGrammarModifiers(node); + checkVariableLikeDeclaration(node); + var func = ts.getContainingFunction(node); + if (ts.getModifierFlags(node) & 92 /* ParameterPropertyModifier */) { + func = ts.getContainingFunction(node); + if (!(func.kind === 148 /* Constructor */ && ts.nodeIsPresent(func.body))) { + error(node, ts.Diagnostics.A_parameter_property_is_only_allowed_in_a_constructor_implementation); } } - checkSourceElement(node.body); - if (!node.asteriskToken) { - var returnOrPromisedType = node.type && (isAsync ? checkAsyncFunctionReturnType(node) : getTypeFromTypeNode(node.type)); - checkAllCodePathsInNonVoidFunctionReturnOrThrow(node, returnOrPromisedType); + if (node.questionToken && ts.isBindingPattern(node.name) && func.body) { + error(node, ts.Diagnostics.A_binding_pattern_parameter_cannot_be_optional_in_an_implementation_signature); } - if (produceDiagnostics && !node.type) { - // Report an implicit any error if there is no body, no explicit return type, and node is not a private method - // in an ambient context - if (compilerOptions.noImplicitAny && ts.nodeIsMissing(node.body) && !isPrivateWithinAmbient(node)) { - reportImplicitAnyError(node, anyType); + if (node.name.text === "this") { + if (ts.indexOf(func.parameters, node) !== 0) { + error(node, ts.Diagnostics.A_this_parameter_must_be_the_first_parameter); } - if (node.asteriskToken && ts.nodeIsPresent(node.body)) { - // A generator with a body and no type annotation can still cause errors. It can error if the - // yielded values have no common supertype, or it can give an implicit any error if it has no - // yielded values. The only way to trigger these errors is to try checking its return type. - getReturnTypeOfSignature(getSignatureFromDeclaration(node)); + if (func.kind === 148 /* Constructor */ || func.kind === 152 /* ConstructSignature */ || func.kind === 157 /* ConstructorType */) { + error(node, ts.Diagnostics.A_constructor_cannot_have_a_this_parameter); } } - registerForUnusedIdentifiersCheck(node); + // Only check rest parameter type if it's not a binding pattern. Since binding patterns are + // not allowed in a rest parameter, we already have an error from checkGrammarParameterList. + if (node.dotDotDotToken && !ts.isBindingPattern(node.name) && !isArrayType(getTypeOfSymbol(node.symbol))) { + error(node, ts.Diagnostics.A_rest_parameter_must_be_of_an_array_type); + } } - function registerForUnusedIdentifiersCheck(node) { - if (deferredUnusedIdentifierNodes) { - deferredUnusedIdentifierNodes.push(node); + function isSyntacticallyValidGenerator(node) { + if (!node.asteriskToken || !node.body) { + return false; } + return node.kind === 147 /* MethodDeclaration */ || + node.kind === 220 /* FunctionDeclaration */ || + node.kind === 179 /* FunctionExpression */; } - function checkUnusedIdentifiers() { - if (deferredUnusedIdentifierNodes) { - for (var _i = 0, deferredUnusedIdentifierNodes_1 = deferredUnusedIdentifierNodes; _i < deferredUnusedIdentifierNodes_1.length; _i++) { - var node = deferredUnusedIdentifierNodes_1[_i]; - switch (node.kind) { - case 256 /* SourceFile */: - case 225 /* ModuleDeclaration */: - checkUnusedModuleMembers(node); - break; - case 221 /* ClassDeclaration */: - case 192 /* ClassExpression */: - checkUnusedClassMembers(node); - checkUnusedTypeParameters(node); - break; - case 222 /* InterfaceDeclaration */: - checkUnusedTypeParameters(node); - break; - case 199 /* Block */: - case 227 /* CaseBlock */: - case 206 /* ForStatement */: - case 207 /* ForInStatement */: - case 208 /* ForOfStatement */: - checkUnusedLocalsAndParameters(node); - break; - case 148 /* Constructor */: - case 179 /* FunctionExpression */: - case 220 /* FunctionDeclaration */: - case 180 /* ArrowFunction */: - case 147 /* MethodDeclaration */: - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - if (node.body) { - checkUnusedLocalsAndParameters(node); - } - checkUnusedTypeParameters(node); - break; - case 146 /* MethodSignature */: - case 151 /* CallSignature */: - case 152 /* ConstructSignature */: - case 153 /* IndexSignature */: - case 156 /* FunctionType */: - case 157 /* ConstructorType */: - checkUnusedTypeParameters(node); - break; + function getTypePredicateParameterIndex(parameterList, parameter) { + if (parameterList) { + for (var i = 0; i < parameterList.length; i++) { + var param = parameterList[i]; + if (param.name.kind === 69 /* Identifier */ && + param.name.text === parameter.text) { + return i; } - ; } } + return -1; } - function checkUnusedLocalsAndParameters(node) { - if (node.parent.kind !== 222 /* InterfaceDeclaration */ && noUnusedIdentifiers && !ts.isInAmbientContext(node)) { - var _loop_1 = function (key) { - var local = node.locals[key]; - if (!local.isReferenced) { - if (local.valueDeclaration && local.valueDeclaration.kind === 142 /* Parameter */) { - var parameter = local.valueDeclaration; - if (compilerOptions.noUnusedParameters && - !ts.isParameterPropertyDeclaration(parameter) && - !parameterIsThisKeyword(parameter) && - !parameterNameStartsWithUnderscore(parameter)) { - error(local.valueDeclaration.name, ts.Diagnostics._0_is_declared_but_never_used, local.name); - } - } - else if (compilerOptions.noUnusedLocals) { - ts.forEach(local.declarations, function (d) { return error(d.name || d, ts.Diagnostics._0_is_declared_but_never_used, local.name); }); - } + function checkTypePredicate(node) { + var parent = getTypePredicateParent(node); + if (!parent) { + // The parent must not be valid. + error(node, ts.Diagnostics.A_type_predicate_is_only_allowed_in_return_type_position_for_functions_and_methods); + return; + } + var typePredicate = getSignatureFromDeclaration(parent).typePredicate; + if (!typePredicate) { + return; + } + var parameterName = node.parameterName; + if (ts.isThisTypePredicate(typePredicate)) { + getTypeFromThisTypeNode(parameterName); + } + else { + if (typePredicate.parameterIndex >= 0) { + if (parent.parameters[typePredicate.parameterIndex].dotDotDotToken) { + error(parameterName, ts.Diagnostics.A_type_predicate_cannot_reference_a_rest_parameter); + } + else { + var leadingError = ts.chainDiagnosticMessages(undefined, ts.Diagnostics.A_type_predicate_s_type_must_be_assignable_to_its_parameter_s_type); + checkTypeAssignableTo(typePredicate.type, getTypeOfNode(parent.parameters[typePredicate.parameterIndex]), node.type, + /*headMessage*/ undefined, leadingError); } - }; - for (var key in node.locals) { - _loop_1(key); } - } - } - function parameterIsThisKeyword(parameter) { - return parameter.name && parameter.name.originalKeywordKind === 97 /* ThisKeyword */; - } - function parameterNameStartsWithUnderscore(parameter) { - return parameter.name && parameter.name.kind === 69 /* Identifier */ && parameter.name.text.charCodeAt(0) === 95 /* _ */; - } - function checkUnusedClassMembers(node) { - if (compilerOptions.noUnusedLocals && !ts.isInAmbientContext(node)) { - if (node.members) { - for (var _i = 0, _a = node.members; _i < _a.length; _i++) { - var member = _a[_i]; - if (member.kind === 147 /* MethodDeclaration */ || member.kind === 145 /* PropertyDeclaration */) { - if (!member.symbol.isReferenced && ts.getModifierFlags(member) & 8 /* Private */) { - error(member.name, ts.Diagnostics._0_is_declared_but_never_used, member.symbol.name); - } - } - else if (member.kind === 148 /* Constructor */) { - for (var _b = 0, _c = member.parameters; _b < _c.length; _b++) { - var parameter = _c[_b]; - if (!parameter.symbol.isReferenced && ts.getModifierFlags(parameter) & 8 /* Private */) { - error(parameter.name, ts.Diagnostics.Property_0_is_declared_but_never_used, parameter.symbol.name); - } - } + else if (parameterName) { + var hasReportedError = false; + for (var _i = 0, _a = parent.parameters; _i < _a.length; _i++) { + var name_18 = _a[_i].name; + if (ts.isBindingPattern(name_18) && + checkIfTypePredicateVariableIsDeclaredInBindingPattern(name_18, parameterName, typePredicate.parameterName)) { + hasReportedError = true; + break; } } + if (!hasReportedError) { + error(node.parameterName, ts.Diagnostics.Cannot_find_parameter_0, typePredicate.parameterName); + } } } } - function checkUnusedTypeParameters(node) { - if (compilerOptions.noUnusedLocals && !ts.isInAmbientContext(node)) { - if (node.typeParameters) { - // Only report errors on the last declaration for the type parameter container; - // this ensures that all uses have been accounted for. - var symbol = getSymbolOfNode(node); - var lastDeclaration = symbol && symbol.declarations && ts.lastOrUndefined(symbol.declarations); - if (lastDeclaration !== node) { - return; - } - for (var _i = 0, _a = node.typeParameters; _i < _a.length; _i++) { - var typeParameter = _a[_i]; - if (!getMergedSymbol(typeParameter.symbol).isReferenced) { - error(typeParameter.name, ts.Diagnostics._0_is_declared_but_never_used, typeParameter.symbol.name); - } + function getTypePredicateParent(node) { + switch (node.parent.kind) { + case 180 /* ArrowFunction */: + case 151 /* CallSignature */: + case 220 /* FunctionDeclaration */: + case 179 /* FunctionExpression */: + case 156 /* FunctionType */: + case 147 /* MethodDeclaration */: + case 146 /* MethodSignature */: + var parent_12 = node.parent; + if (node === parent_12.type) { + return parent_12; } - } } } - function checkUnusedModuleMembers(node) { - if (compilerOptions.noUnusedLocals && !ts.isInAmbientContext(node)) { - for (var key in node.locals) { - var local = node.locals[key]; - if (!local.isReferenced && !local.exportSymbol) { - for (var _i = 0, _a = local.declarations; _i < _a.length; _i++) { - var declaration = _a[_i]; - if (!ts.isAmbientModule(declaration)) { - error(declaration.name, ts.Diagnostics._0_is_declared_but_never_used, local.name); - } - } + function checkIfTypePredicateVariableIsDeclaredInBindingPattern(pattern, predicateVariableNode, predicateVariableName) { + for (var _i = 0, _a = pattern.elements; _i < _a.length; _i++) { + var element = _a[_i]; + if (ts.isOmittedExpression(element)) { + continue; + } + var name_19 = element.name; + if (name_19.kind === 69 /* Identifier */ && + name_19.text === predicateVariableName) { + error(predicateVariableNode, ts.Diagnostics.A_type_predicate_cannot_reference_element_0_in_a_binding_pattern, predicateVariableName); + return true; + } + else if (name_19.kind === 168 /* ArrayBindingPattern */ || + name_19.kind === 167 /* ObjectBindingPattern */) { + if (checkIfTypePredicateVariableIsDeclaredInBindingPattern(name_19, predicateVariableNode, predicateVariableName)) { + return true; } } } } - function checkBlock(node) { - // Grammar checking for SyntaxKind.Block - if (node.kind === 199 /* Block */) { - checkGrammarStatementInAmbientContext(node); + function checkSignatureDeclaration(node) { + // Grammar checking + if (node.kind === 153 /* IndexSignature */) { + checkGrammarIndexSignature(node); } - ts.forEach(node.statements, checkSourceElement); - if (node.locals) { - registerForUnusedIdentifiersCheck(node); + else if (node.kind === 156 /* FunctionType */ || node.kind === 220 /* FunctionDeclaration */ || node.kind === 157 /* ConstructorType */ || + node.kind === 151 /* CallSignature */ || node.kind === 148 /* Constructor */ || + node.kind === 152 /* ConstructSignature */) { + checkGrammarFunctionLikeDeclaration(node); } - } - function checkCollisionWithArgumentsInGeneratedCode(node) { - // no rest parameters \ declaration context \ overload - no codegen impact - if (!ts.hasDeclaredRestParameter(node) || ts.isInAmbientContext(node) || ts.nodeIsMissing(node.body)) { - return; + checkTypeParameters(node.typeParameters); + ts.forEach(node.parameters, checkParameter); + if (node.type) { + checkSourceElement(node.type); } - ts.forEach(node.parameters, function (p) { - if (p.name && !ts.isBindingPattern(p.name) && p.name.text === argumentsSymbol.name) { - error(p, ts.Diagnostics.Duplicate_identifier_arguments_Compiler_uses_arguments_to_initialize_rest_parameters); + if (produceDiagnostics) { + checkCollisionWithArgumentsInGeneratedCode(node); + if (compilerOptions.noImplicitAny && !node.type) { + switch (node.kind) { + case 152 /* ConstructSignature */: + error(node, ts.Diagnostics.Construct_signature_which_lacks_return_type_annotation_implicitly_has_an_any_return_type); + break; + case 151 /* CallSignature */: + error(node, ts.Diagnostics.Call_signature_which_lacks_return_type_annotation_implicitly_has_an_any_return_type); + break; + } } - }); - } - function needCollisionCheckForIdentifier(node, identifier, name) { - if (!(identifier && identifier.text === name)) { - return false; - } - if (node.kind === 145 /* PropertyDeclaration */ || - node.kind === 144 /* PropertySignature */ || - node.kind === 147 /* MethodDeclaration */ || - node.kind === 146 /* MethodSignature */ || - node.kind === 149 /* GetAccessor */ || - node.kind === 150 /* SetAccessor */) { - // it is ok to have member named '_super' or '_this' - member access is always qualified - return false; - } - if (ts.isInAmbientContext(node)) { - // ambient context - no codegen impact - return false; - } - var root = ts.getRootDeclaration(node); - if (root.kind === 142 /* Parameter */ && ts.nodeIsMissing(root.parent.body)) { - // just an overload - no codegen impact - return false; - } - return true; - } - function checkCollisionWithCapturedThisVariable(node, name) { - if (needCollisionCheckForIdentifier(node, name, "_this")) { - potentialThisCollisions.push(node); - } - } - // this function will run after checking the source file so 'CaptureThis' is correct for all nodes - function checkIfThisIsCapturedInEnclosingScope(node) { - var current = node; - while (current) { - if (getNodeCheckFlags(current) & 4 /* CaptureThis */) { - var isDeclaration_1 = node.kind !== 69 /* Identifier */; - if (isDeclaration_1) { - error(node.name, ts.Diagnostics.Duplicate_identifier_this_Compiler_uses_variable_declaration_this_to_capture_this_reference); + if (node.type) { + if (languageVersion >= 2 /* ES6 */ && isSyntacticallyValidGenerator(node)) { + var returnType = getTypeFromTypeNode(node.type); + if (returnType === voidType) { + error(node.type, ts.Diagnostics.A_generator_cannot_have_a_void_type_annotation); + } + else { + var generatorElementType = getElementTypeOfIterableIterator(returnType) || anyType; + var iterableIteratorInstantiation = createIterableIteratorType(generatorElementType); + // Naively, one could check that IterableIterator is assignable to the return type annotation. + // However, that would not catch the error in the following case. + // + // interface BadGenerator extends Iterable, Iterator { } + // function* g(): BadGenerator { } // Iterable and Iterator have different types! + // + checkTypeAssignableTo(iterableIteratorInstantiation, returnType, node.type); + } } - else { - error(node, ts.Diagnostics.Expression_resolves_to_variable_declaration_this_that_compiler_uses_to_capture_this_reference); + else if (ts.isAsyncFunctionLike(node)) { + checkAsyncFunctionReturnType(node); } - return; } - current = current.parent; + if (noUnusedIdentifiers && !node.body) { + checkUnusedTypeParameters(node); + } } } - function checkCollisionWithCapturedSuperVariable(node, name) { - if (!needCollisionCheckForIdentifier(node, name, "_super")) { - return; - } - // bubble up and find containing type - var enclosingClass = ts.getContainingClass(node); - // if containing type was not found or it is ambient - exit (no codegen) - if (!enclosingClass || ts.isInAmbientContext(enclosingClass)) { - return; - } - if (ts.getClassExtendsHeritageClauseElement(enclosingClass)) { - var isDeclaration_2 = node.kind !== 69 /* Identifier */; - if (isDeclaration_2) { - error(node, ts.Diagnostics.Duplicate_identifier_super_Compiler_uses_super_to_capture_base_class_reference); + function checkClassForDuplicateDeclarations(node) { + var Accessor; + (function (Accessor) { + Accessor[Accessor["Getter"] = 1] = "Getter"; + Accessor[Accessor["Setter"] = 2] = "Setter"; + Accessor[Accessor["Property"] = 3] = "Property"; + })(Accessor || (Accessor = {})); + var instanceNames = ts.createMap(); + var staticNames = ts.createMap(); + for (var _i = 0, _a = node.members; _i < _a.length; _i++) { + var member = _a[_i]; + if (member.kind === 148 /* Constructor */) { + for (var _b = 0, _c = member.parameters; _b < _c.length; _b++) { + var param = _c[_b]; + if (ts.isParameterPropertyDeclaration(param)) { + addName(instanceNames, param.name, param.name.text, 3 /* Property */); + } + } } else { - error(node, ts.Diagnostics.Expression_resolves_to_super_that_compiler_uses_to_capture_base_class_reference); + var isStatic = ts.forEach(member.modifiers, function (m) { return m.kind === 113 /* StaticKeyword */; }); + var names = isStatic ? staticNames : instanceNames; + var memberName = member.name && ts.getPropertyNameForPropertyNameNode(member.name); + if (memberName) { + switch (member.kind) { + case 149 /* GetAccessor */: + addName(names, member.name, memberName, 1 /* Getter */); + break; + case 150 /* SetAccessor */: + addName(names, member.name, memberName, 2 /* Setter */); + break; + case 145 /* PropertyDeclaration */: + addName(names, member.name, memberName, 3 /* Property */); + break; + } + } } } - } - function checkCollisionWithRequireExportsInGeneratedCode(node, name) { - if (!needCollisionCheckForIdentifier(node, name, "require") && !needCollisionCheckForIdentifier(node, name, "exports")) { - return; - } - // Uninstantiated modules shouldnt do this check - if (node.kind === 225 /* ModuleDeclaration */ && ts.getModuleInstanceState(node) !== 1 /* Instantiated */) { - return; - } - // In case of variable declaration, node.parent is variable statement so look at the variable statement's parent - var parent = getDeclarationContainer(node); - if (parent.kind === 256 /* SourceFile */ && ts.isExternalOrCommonJsModule(parent)) { - // If the declaration happens to be in external module, report error that require and exports are reserved keywords - error(name, ts.Diagnostics.Duplicate_identifier_0_Compiler_reserves_name_1_in_top_level_scope_of_a_module, ts.declarationNameToString(name), ts.declarationNameToString(name)); - } - } - function checkCollisionWithGlobalPromiseInGeneratedCode(node, name) { - if (!needCollisionCheckForIdentifier(node, name, "Promise")) { - return; - } - // Uninstantiated modules shouldnt do this check - if (node.kind === 225 /* ModuleDeclaration */ && ts.getModuleInstanceState(node) !== 1 /* Instantiated */) { - return; - } - // In case of variable declaration, node.parent is variable statement so look at the variable statement's parent - var parent = getDeclarationContainer(node); - if (parent.kind === 256 /* SourceFile */ && ts.isExternalOrCommonJsModule(parent) && parent.flags & 8192 /* HasAsyncFunctions */) { - // If the declaration happens to be in external module, report error that Promise is a reserved identifier. - error(name, ts.Diagnostics.Duplicate_identifier_0_Compiler_reserves_name_1_in_top_level_scope_of_a_module_containing_async_functions, ts.declarationNameToString(name), ts.declarationNameToString(name)); + function addName(names, location, name, meaning) { + var prev = names[name]; + if (prev) { + if (prev & meaning) { + error(location, ts.Diagnostics.Duplicate_identifier_0, ts.getTextOfNode(location)); + } + else { + names[name] = prev | meaning; + } + } + else { + names[name] = meaning; + } } } - function checkVarDeclaredNamesNotShadowed(node) { - // - ScriptBody : StatementList - // It is a Syntax Error if any element of the LexicallyDeclaredNames of StatementList - // also occurs in the VarDeclaredNames of StatementList. - // - Block : { StatementList } - // It is a Syntax Error if any element of the LexicallyDeclaredNames of StatementList - // also occurs in the VarDeclaredNames of StatementList. - // Variable declarations are hoisted to the top of their function scope. They can shadow - // block scoped declarations, which bind tighter. this will not be flagged as duplicate definition - // by the binder as the declaration scope is different. - // A non-initialized declaration is a no-op as the block declaration will resolve before the var - // declaration. the problem is if the declaration has an initializer. this will act as a write to the - // block declared value. this is fine for let, but not const. - // Only consider declarations with initializers, uninitialized const declarations will not - // step on a let/const variable. - // Do not consider const and const declarations, as duplicate block-scoped declarations - // are handled by the binder. - // We are only looking for const declarations that step on let\const declarations from a - // different scope. e.g.: - // { - // const x = 0; // localDeclarationSymbol obtained after name resolution will correspond to this declaration - // const x = 0; // symbol for this declaration will be 'symbol' - // } - // skip block-scoped variables and parameters - if ((ts.getCombinedNodeFlags(node) & 3 /* BlockScoped */) !== 0 || ts.isParameterDeclaration(node)) { - return; - } - // skip variable declarations that don't have initializers - // NOTE: in ES6 spec initializer is required in variable declarations where name is binding pattern - // so we'll always treat binding elements as initialized - if (node.kind === 218 /* VariableDeclaration */ && !node.initializer) { - return; - } - var symbol = getSymbolOfNode(node); - if (symbol.flags & 1 /* FunctionScopedVariable */) { - var localDeclarationSymbol = resolveName(node, node.name.text, 3 /* Variable */, /*nodeNotFoundErrorMessage*/ undefined, /*nameArg*/ undefined); - if (localDeclarationSymbol && - localDeclarationSymbol !== symbol && - localDeclarationSymbol.flags & 2 /* BlockScopedVariable */) { - if (getDeclarationNodeFlagsFromSymbol(localDeclarationSymbol) & 3 /* BlockScoped */) { - var varDeclList = ts.getAncestor(localDeclarationSymbol.valueDeclaration, 219 /* VariableDeclarationList */); - var container = varDeclList.parent.kind === 200 /* VariableStatement */ && varDeclList.parent.parent - ? varDeclList.parent.parent - : undefined; - // names of block-scoped and function scoped variables can collide only - // if block scoped variable is defined in the function\module\source file scope (because of variable hoisting) - var namesShareScope = container && - (container.kind === 199 /* Block */ && ts.isFunctionLike(container.parent) || - container.kind === 226 /* ModuleBlock */ || - container.kind === 225 /* ModuleDeclaration */ || - container.kind === 256 /* SourceFile */); - // here we know that function scoped variable is shadowed by block scoped one - // if they are defined in the same scope - binder has already reported redeclaration error - // otherwise if variable has an initializer - show error that initialization will fail - // since LHS will be block scoped name instead of function scoped - if (!namesShareScope) { - var name_20 = symbolToString(localDeclarationSymbol); - error(node, ts.Diagnostics.Cannot_initialize_outer_scoped_variable_0_in_the_same_scope_as_block_scoped_declaration_1, name_20, name_20); - } + function checkObjectTypeForDuplicateDeclarations(node) { + var names = ts.createMap(); + for (var _i = 0, _a = node.members; _i < _a.length; _i++) { + var member = _a[_i]; + if (member.kind == 144 /* PropertySignature */) { + var memberName = void 0; + switch (member.name.kind) { + case 9 /* StringLiteral */: + case 8 /* NumericLiteral */: + case 69 /* Identifier */: + memberName = member.name.text; + break; + default: + continue; + } + if (names[memberName]) { + error(member.symbol.valueDeclaration.name, ts.Diagnostics.Duplicate_identifier_0, memberName); + error(member.name, ts.Diagnostics.Duplicate_identifier_0, memberName); + } + else { + names[memberName] = true; } } } } - // Check that a parameter initializer contains no references to parameters declared to the right of itself - function checkParameterInitializer(node) { - if (ts.getRootDeclaration(node).kind !== 142 /* Parameter */) { - return; - } - var func = ts.getContainingFunction(node); - visit(node.initializer); - function visit(n) { - if (ts.isTypeNode(n) || ts.isDeclarationName(n)) { - // do not dive in types - // skip declaration names (i.e. in object literal expressions) + function checkTypeForDuplicateIndexSignatures(node) { + if (node.kind === 222 /* InterfaceDeclaration */) { + var nodeSymbol = getSymbolOfNode(node); + // in case of merging interface declaration it is possible that we'll enter this check procedure several times for every declaration + // to prevent this run check only for the first declaration of a given kind + if (nodeSymbol.declarations.length > 0 && nodeSymbol.declarations[0] !== node) { return; } - if (n.kind === 172 /* PropertyAccessExpression */) { - // skip property names in property access expression - return visit(n.expression); - } - else if (n.kind === 69 /* Identifier */) { - // check FunctionLikeDeclaration.locals (stores parameters\function local variable) - // if it contains entry with a specified name - var symbol = resolveName(n, n.text, 107455 /* Value */ | 8388608 /* Alias */, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined); - if (!symbol || symbol === unknownSymbol || !symbol.valueDeclaration) { - return; - } - if (symbol.valueDeclaration === node) { - error(n, ts.Diagnostics.Parameter_0_cannot_be_referenced_in_its_initializer, ts.declarationNameToString(node.name)); - return; - } - // locals map for function contain both parameters and function locals - // so we need to do a bit of extra work to check if reference is legal - var enclosingContainer = ts.getEnclosingBlockScopeContainer(symbol.valueDeclaration); - if (enclosingContainer === func) { - if (symbol.valueDeclaration.kind === 142 /* Parameter */) { - // it is ok to reference parameter in initializer if either - // - parameter is located strictly on the left of current parameter declaration - if (symbol.valueDeclaration.pos < node.pos) { - return; - } - // - parameter is wrapped in function-like entity - var current = n; - while (current !== node.initializer) { - if (ts.isFunctionLike(current.parent)) { - return; + } + // TypeScript 1.0 spec (April 2014) + // 3.7.4: An object type can contain at most one string index signature and one numeric index signature. + // 8.5: A class declaration can have at most one string index member declaration and one numeric index member declaration + var indexSymbol = getIndexSymbol(getSymbolOfNode(node)); + if (indexSymbol) { + var seenNumericIndexer = false; + var seenStringIndexer = false; + for (var _i = 0, _a = indexSymbol.declarations; _i < _a.length; _i++) { + var decl = _a[_i]; + var declaration = decl; + if (declaration.parameters.length === 1 && declaration.parameters[0].type) { + switch (declaration.parameters[0].type.kind) { + case 132 /* StringKeyword */: + if (!seenStringIndexer) { + seenStringIndexer = true; } - // computed property names/initializers in instance property declaration of class like entities - // are executed in constructor and thus deferred - if (current.parent.kind === 145 /* PropertyDeclaration */ && - !(ts.hasModifier(current.parent, 32 /* Static */)) && - ts.isClassLike(current.parent.parent)) { - return; + else { + error(declaration, ts.Diagnostics.Duplicate_string_index_signature); } - current = current.parent; - } + break; + case 130 /* NumberKeyword */: + if (!seenNumericIndexer) { + seenNumericIndexer = true; + } + else { + error(declaration, ts.Diagnostics.Duplicate_number_index_signature); + } + break; } - error(n, ts.Diagnostics.Initializer_of_parameter_0_cannot_reference_identifier_1_declared_after_it, ts.declarationNameToString(node.name), ts.declarationNameToString(n)); } } - else { - return ts.forEachChild(n, visit); - } } } - // Check variable, parameter, or property declaration - function checkVariableLikeDeclaration(node) { - checkDecorators(node); - checkSourceElement(node.type); - // For a computed property, just check the initializer and exit - // Do not use hasDynamicName here, because that returns false for well known symbols. - // We want to perform checkComputedPropertyName for all computed properties, including - // well known symbols. - if (node.name.kind === 140 /* ComputedPropertyName */) { - checkComputedPropertyName(node.name); - if (node.initializer) { - checkExpressionCached(node.initializer); - } - } - if (node.kind === 169 /* BindingElement */) { - // check computed properties inside property names of binding elements - if (node.propertyName && node.propertyName.kind === 140 /* ComputedPropertyName */) { - checkComputedPropertyName(node.propertyName); - } - // check private/protected variable access - var parent_12 = node.parent.parent; - var parentType = getTypeForBindingElementParent(parent_12); - var name_21 = node.propertyName || node.name; - var property = getPropertyOfType(parentType, getTextOfPropertyName(name_21)); - if (parent_12.initializer && property && getParentOfSymbol(property)) { - checkClassPropertyAccess(parent_12, parent_12.initializer, parentType, property); - } + function checkPropertyDeclaration(node) { + // Grammar checking + checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarProperty(node) || checkGrammarComputedPropertyName(node.name); + checkVariableLikeDeclaration(node); + } + function checkMethodDeclaration(node) { + // Grammar checking + checkGrammarMethod(node) || checkGrammarComputedPropertyName(node.name); + // Grammar checking for modifiers is done inside the function checkGrammarFunctionLikeDeclaration + checkFunctionOrMethodDeclaration(node); + // Abstract methods cannot have an implementation. + // Extra checks are to avoid reporting multiple errors relating to the "abstractness" of the node. + if (ts.getModifierFlags(node) & 128 /* Abstract */ && node.body) { + error(node, ts.Diagnostics.Method_0_cannot_have_an_implementation_because_it_is_marked_abstract, ts.declarationNameToString(node.name)); } - // For a binding pattern, check contained binding elements - if (ts.isBindingPattern(node.name)) { - ts.forEach(node.name.elements, checkSourceElement); + } + function checkConstructorDeclaration(node) { + // Grammar check on signature of constructor and modifier of the constructor is done in checkSignatureDeclaration function. + checkSignatureDeclaration(node); + // Grammar check for checking only related to constructorDeclaration + checkGrammarConstructorTypeParameters(node) || checkGrammarConstructorTypeAnnotation(node); + checkSourceElement(node.body); + registerForUnusedIdentifiersCheck(node); + var symbol = getSymbolOfNode(node); + var firstDeclaration = ts.getDeclarationOfKind(symbol, node.kind); + // Only type check the symbol once + if (node === firstDeclaration) { + checkFunctionOrConstructorSymbol(symbol); } - // For a parameter declaration with an initializer, error and exit if the containing function doesn't have a body - if (node.initializer && ts.getRootDeclaration(node).kind === 142 /* Parameter */ && ts.nodeIsMissing(ts.getContainingFunction(node).body)) { - error(node, ts.Diagnostics.A_parameter_initializer_is_only_allowed_in_a_function_or_constructor_implementation); + // exit early in the case of signature - super checks are not relevant to them + if (ts.nodeIsMissing(node.body)) { return; } - // For a binding pattern, validate the initializer and exit - if (ts.isBindingPattern(node.name)) { - // Don't validate for-in initializer as it is already an error - if (node.initializer && node.parent.parent.kind !== 207 /* ForInStatement */) { - checkTypeAssignableTo(checkExpressionCached(node.initializer), getWidenedTypeForVariableLikeDeclaration(node), node, /*headMessage*/ undefined); - checkParameterInitializer(node); - } + if (!produceDiagnostics) { return; } - var symbol = getSymbolOfNode(node); - var type = getTypeOfVariableOrParameterOrProperty(symbol); - if (node === symbol.valueDeclaration) { - // Node is the primary declaration of the symbol, just validate the initializer - // Don't validate for-in initializer as it is already an error - if (node.initializer && node.parent.parent.kind !== 207 /* ForInStatement */) { - checkTypeAssignableTo(checkExpressionCached(node.initializer), type, node, /*headMessage*/ undefined); - checkParameterInitializer(node); - } + function containsSuperCallAsComputedPropertyName(n) { + return n.name && containsSuperCall(n.name); } - else { - // Node is a secondary declaration, check that type is identical to primary declaration and check that - // initializer is consistent with type associated with the node - var declarationType = getWidenedTypeForVariableLikeDeclaration(node); - if (type !== unknownType && declarationType !== unknownType && !isTypeIdenticalTo(type, declarationType)) { - error(node.name, ts.Diagnostics.Subsequent_variable_declarations_must_have_the_same_type_Variable_0_must_be_of_type_1_but_here_has_type_2, ts.declarationNameToString(node.name), typeToString(type), typeToString(declarationType)); + function containsSuperCall(n) { + if (ts.isSuperCall(n)) { + return true; } - if (node.initializer) { - checkTypeAssignableTo(checkExpressionCached(node.initializer), declarationType, node, /*headMessage*/ undefined); + else if (ts.isFunctionLike(n)) { + return false; } - if (!areDeclarationFlagsIdentical(node, symbol.valueDeclaration)) { - error(symbol.valueDeclaration.name, ts.Diagnostics.All_declarations_of_0_must_have_identical_modifiers, ts.declarationNameToString(node.name)); - error(node.name, ts.Diagnostics.All_declarations_of_0_must_have_identical_modifiers, ts.declarationNameToString(node.name)); + else if (ts.isClassLike(n)) { + return ts.forEach(n.members, containsSuperCallAsComputedPropertyName); } + return ts.forEachChild(n, containsSuperCall); } - if (node.kind !== 145 /* PropertyDeclaration */ && node.kind !== 144 /* PropertySignature */) { - // We know we don't have a binding pattern or computed name here - checkExportsOnMergedDeclarations(node); - if (node.kind === 218 /* VariableDeclaration */ || node.kind === 169 /* BindingElement */) { - checkVarDeclaredNamesNotShadowed(node); + function markThisReferencesAsErrors(n) { + if (n.kind === 97 /* ThisKeyword */) { + error(n, ts.Diagnostics.this_cannot_be_referenced_in_current_location); + } + else if (n.kind !== 179 /* FunctionExpression */ && n.kind !== 220 /* FunctionDeclaration */) { + ts.forEachChild(n, markThisReferencesAsErrors); } - checkCollisionWithCapturedSuperVariable(node, node.name); - checkCollisionWithCapturedThisVariable(node, node.name); - checkCollisionWithRequireExportsInGeneratedCode(node, node.name); - checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); - } - } - function areDeclarationFlagsIdentical(left, right) { - if ((left.kind === 142 /* Parameter */ && right.kind === 218 /* VariableDeclaration */) || - (left.kind === 218 /* VariableDeclaration */ && right.kind === 142 /* Parameter */)) { - // Differences in optionality between parameters and variables are allowed. - return true; } - if (ts.hasQuestionToken(left) !== ts.hasQuestionToken(right)) { - return false; + function isInstancePropertyWithInitializer(n) { + return n.kind === 145 /* PropertyDeclaration */ && + !(ts.getModifierFlags(n) & 32 /* Static */) && + !!n.initializer; } - var interestingFlags = 8 /* Private */ | - 16 /* Protected */ | - 256 /* Async */ | - 128 /* Abstract */ | - 64 /* Readonly */ | - 32 /* Static */; - return (ts.getModifierFlags(left) & interestingFlags) === (ts.getModifierFlags(right) & interestingFlags); - } - function checkVariableDeclaration(node) { - checkGrammarVariableDeclaration(node); - return checkVariableLikeDeclaration(node); - } - function checkBindingElement(node) { - checkGrammarBindingElement(node); - return checkVariableLikeDeclaration(node); - } - function checkVariableStatement(node) { - // Grammar checking - checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarVariableDeclarationList(node.declarationList) || checkGrammarForDisallowedLetOrConstStatement(node); - ts.forEach(node.declarationList.declarations, checkSourceElement); - } - function checkGrammarDisallowedModifiersOnObjectLiteralExpressionMethod(node) { - // We only disallow modifier on a method declaration if it is a property of object-literal-expression - if (node.modifiers && node.parent.kind === 171 /* ObjectLiteralExpression */) { - if (ts.isAsyncFunctionLike(node)) { - if (node.modifiers.length > 1) { - return grammarErrorOnFirstToken(node, ts.Diagnostics.Modifiers_cannot_appear_here); + // TS 1.0 spec (April 2014): 8.3.2 + // Constructors of classes with no extends clause may not contain super calls, whereas + // constructors of derived classes must contain at least one super call somewhere in their function body. + var containingClassDecl = node.parent; + if (ts.getClassExtendsHeritageClauseElement(containingClassDecl)) { + captureLexicalThis(node.parent, containingClassDecl); + var classExtendsNull = classDeclarationExtendsNull(containingClassDecl); + var superCall = getSuperCallInConstructor(node); + if (superCall) { + if (classExtendsNull) { + error(superCall, ts.Diagnostics.A_constructor_cannot_contain_a_super_call_when_its_class_extends_null); + } + // The first statement in the body of a constructor (excluding prologue directives) must be a super call + // if both of the following are true: + // - The containing class is a derived class. + // - The constructor declares parameter properties + // or the containing class declares instance member variables with initializers. + var superCallShouldBeFirst = ts.forEach(node.parent.members, isInstancePropertyWithInitializer) || + ts.forEach(node.parameters, function (p) { return ts.getModifierFlags(p) & 92 /* ParameterPropertyModifier */; }); + // Skip past any prologue directives to find the first statement + // to ensure that it was a super call. + if (superCallShouldBeFirst) { + var statements = node.body.statements; + var superCallStatement = void 0; + for (var _i = 0, statements_2 = statements; _i < statements_2.length; _i++) { + var statement = statements_2[_i]; + if (statement.kind === 202 /* ExpressionStatement */ && ts.isSuperCall(statement.expression)) { + superCallStatement = statement; + break; + } + if (!ts.isPrologueDirective(statement)) { + break; + } + } + if (!superCallStatement) { + error(node, ts.Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_when_a_class_contains_initialized_properties_or_has_parameter_properties); + } } } - else { - return grammarErrorOnFirstToken(node, ts.Diagnostics.Modifiers_cannot_appear_here); + else if (!classExtendsNull) { + error(node, ts.Diagnostics.Constructors_for_derived_classes_must_contain_a_super_call); } } } - function checkExpressionStatement(node) { - // Grammar checking - checkGrammarStatementInAmbientContext(node); - checkExpression(node.expression); - } - function checkIfStatement(node) { - // Grammar checking - checkGrammarStatementInAmbientContext(node); - checkExpression(node.expression); - checkSourceElement(node.thenStatement); - if (node.thenStatement.kind === 201 /* EmptyStatement */) { - error(node.thenStatement, ts.Diagnostics.The_body_of_an_if_statement_cannot_be_the_empty_statement); - } - checkSourceElement(node.elseStatement); - } - function checkDoStatement(node) { - // Grammar checking - checkGrammarStatementInAmbientContext(node); - checkSourceElement(node.statement); - checkExpression(node.expression); - } - function checkWhileStatement(node) { - // Grammar checking - checkGrammarStatementInAmbientContext(node); - checkExpression(node.expression); - checkSourceElement(node.statement); - } - function checkForStatement(node) { - // Grammar checking - if (!checkGrammarStatementInAmbientContext(node)) { - if (node.initializer && node.initializer.kind === 219 /* VariableDeclarationList */) { - checkGrammarVariableDeclarationList(node.initializer); + function checkAccessorDeclaration(node) { + if (produceDiagnostics) { + // Grammar checking accessors + checkGrammarFunctionLikeDeclaration(node) || checkGrammarAccessor(node) || checkGrammarComputedPropertyName(node.name); + checkDecorators(node); + checkSignatureDeclaration(node); + if (node.kind === 149 /* GetAccessor */) { + if (!ts.isInAmbientContext(node) && ts.nodeIsPresent(node.body) && (node.flags & 128 /* HasImplicitReturn */)) { + if (!(node.flags & 256 /* HasExplicitReturn */)) { + error(node.name, ts.Diagnostics.A_get_accessor_must_return_a_value); + } + } } - } - if (node.initializer) { - if (node.initializer.kind === 219 /* VariableDeclarationList */) { - ts.forEach(node.initializer.declarations, checkVariableDeclaration); + // Do not use hasDynamicName here, because that returns false for well known symbols. + // We want to perform checkComputedPropertyName for all computed properties, including + // well known symbols. + if (node.name.kind === 140 /* ComputedPropertyName */) { + checkComputedPropertyName(node.name); } - else { - checkExpression(node.initializer); + if (!ts.hasDynamicName(node)) { + // TypeScript 1.0 spec (April 2014): 8.4.3 + // Accessors for the same member name must specify the same accessibility. + var otherKind = node.kind === 149 /* GetAccessor */ ? 150 /* SetAccessor */ : 149 /* GetAccessor */; + var otherAccessor = ts.getDeclarationOfKind(node.symbol, otherKind); + if (otherAccessor) { + if ((ts.getModifierFlags(node) & 28 /* AccessibilityModifier */) !== (ts.getModifierFlags(otherAccessor) & 28 /* AccessibilityModifier */)) { + error(node.name, ts.Diagnostics.Getter_and_setter_accessors_do_not_agree_in_visibility); + } + if (ts.hasModifier(node, 128 /* Abstract */) !== ts.hasModifier(otherAccessor, 128 /* Abstract */)) { + error(node.name, ts.Diagnostics.Accessors_must_both_be_abstract_or_non_abstract); + } + // TypeScript 1.0 spec (April 2014): 4.5 + // If both accessors include type annotations, the specified types must be identical. + checkAccessorDeclarationTypesIdentical(node, otherAccessor, getAnnotatedAccessorType, ts.Diagnostics.get_and_set_accessor_must_have_the_same_type); + checkAccessorDeclarationTypesIdentical(node, otherAccessor, getThisTypeOfDeclaration, ts.Diagnostics.get_and_set_accessor_must_have_the_same_this_type); + } + } + var returnType = getTypeOfAccessors(getSymbolOfNode(node)); + if (node.kind === 149 /* GetAccessor */) { + checkAllCodePathsInNonVoidFunctionReturnOrThrow(node, returnType); } } - if (node.condition) - checkExpression(node.condition); - if (node.incrementor) - checkExpression(node.incrementor); - checkSourceElement(node.statement); - if (node.locals) { + if (node.parent.kind !== 171 /* ObjectLiteralExpression */) { + checkSourceElement(node.body); registerForUnusedIdentifiersCheck(node); } - } - function checkForOfStatement(node) { - checkGrammarForInOrForOfStatement(node); - // Check the LHS and RHS - // If the LHS is a declaration, just check it as a variable declaration, which will in turn check the RHS - // via checkRightHandSideOfForOf. - // If the LHS is an expression, check the LHS, as a destructuring assignment or as a reference. - // Then check that the RHS is assignable to it. - if (node.initializer.kind === 219 /* VariableDeclarationList */) { - checkForInOrForOfVariableDeclaration(node); - } else { - var varExpr = node.initializer; - var iteratedType = checkRightHandSideOfForOf(node.expression); - // There may be a destructuring assignment on the left side - if (varExpr.kind === 170 /* ArrayLiteralExpression */ || varExpr.kind === 171 /* ObjectLiteralExpression */) { - // iteratedType may be undefined. In this case, we still want to check the structure of - // varExpr, in particular making sure it's a valid LeftHandSideExpression. But we'd like - // to short circuit the type relation checking as much as possible, so we pass the unknownType. - checkDestructuringAssignment(varExpr, iteratedType || unknownType); - } - else { - var leftType = checkExpression(varExpr); - checkReferenceExpression(varExpr, /*invalidReferenceMessage*/ ts.Diagnostics.Invalid_left_hand_side_in_for_of_statement, - /*constantVariableMessage*/ ts.Diagnostics.The_left_hand_side_of_a_for_of_statement_cannot_be_a_constant_or_a_read_only_property); - // iteratedType will be undefined if the rightType was missing properties/signatures - // required to get its iteratedType (like [Symbol.iterator] or next). This may be - // because we accessed properties from anyType, or it may have led to an error inside - // getElementTypeOfIterable. - if (iteratedType) { - checkTypeAssignableTo(iteratedType, leftType, varExpr, /*headMessage*/ undefined); - } - } + checkNodeDeferred(node); } - checkSourceElement(node.statement); - if (node.locals) { - registerForUnusedIdentifiersCheck(node); + } + function checkAccessorDeclarationTypesIdentical(first, second, getAnnotatedType, message) { + var firstType = getAnnotatedType(first); + var secondType = getAnnotatedType(second); + if (firstType && secondType && !isTypeIdenticalTo(firstType, secondType)) { + error(first, message); } } - function checkForInStatement(node) { - // Grammar checking - checkGrammarForInOrForOfStatement(node); - // TypeScript 1.0 spec (April 2014): 5.4 - // In a 'for-in' statement of the form - // for (let VarDecl in Expr) Statement - // VarDecl must be a variable declaration without a type annotation that declares a variable of type Any, - // and Expr must be an expression of type Any, an object type, or a type parameter type. - if (node.initializer.kind === 219 /* VariableDeclarationList */) { - var variable = node.initializer.declarations[0]; - if (variable && ts.isBindingPattern(variable.name)) { - error(variable.name, ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_a_destructuring_pattern); + function checkAccessorDeferred(node) { + checkSourceElement(node.body); + registerForUnusedIdentifiersCheck(node); + } + function checkMissingDeclaration(node) { + checkDecorators(node); + } + function checkTypeArgumentConstraints(typeParameters, typeArgumentNodes) { + var typeArguments; + var mapper; + var result = true; + for (var i = 0; i < typeParameters.length; i++) { + var constraint = getConstraintOfTypeParameter(typeParameters[i]); + if (constraint) { + if (!typeArguments) { + typeArguments = ts.map(typeArgumentNodes, getTypeFromTypeNodeNoAlias); + mapper = createTypeMapper(typeParameters, typeArguments); + } + var typeArgument = typeArguments[i]; + result = result && checkTypeAssignableTo(typeArgument, getTypeWithThisArgument(instantiateType(constraint, mapper), typeArgument), typeArgumentNodes[i], ts.Diagnostics.Type_0_does_not_satisfy_the_constraint_1); } - checkForInOrForOfVariableDeclaration(node); } - else { - // In a 'for-in' statement of the form - // for (Var in Expr) Statement - // Var must be an expression classified as a reference of type Any or the String primitive type, - // and Expr must be an expression of type Any, an object type, or a type parameter type. - var varExpr = node.initializer; - var leftType = checkExpression(varExpr); - if (varExpr.kind === 170 /* ArrayLiteralExpression */ || varExpr.kind === 171 /* ObjectLiteralExpression */) { - error(varExpr, ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_a_destructuring_pattern); - } - else if (!isTypeAnyOrAllConstituentTypesHaveKind(leftType, 34 /* StringLike */)) { - error(varExpr, ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_must_be_of_type_string_or_any); + return result; + } + function checkTypeReferenceNode(node) { + checkGrammarTypeArguments(node, node.typeArguments); + var type = getTypeFromTypeReference(node); + if (type !== unknownType) { + if (node.typeArguments) { + // Do type argument local checks only if referenced type is successfully resolved + ts.forEach(node.typeArguments, checkSourceElement); + if (produceDiagnostics) { + var symbol = getNodeLinks(node).resolvedSymbol; + var typeParameters = symbol.flags & 524288 /* TypeAlias */ ? getSymbolLinks(symbol).typeParameters : type.target.localTypeParameters; + checkTypeArgumentConstraints(typeParameters, node.typeArguments); + } } - else { - // run check only former check succeeded to avoid cascading errors - checkReferenceExpression(varExpr, ts.Diagnostics.Invalid_left_hand_side_in_for_in_statement, ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_a_constant_or_a_read_only_property); + if (type.flags & 16 /* Enum */ && !type.memberTypes && getNodeLinks(node).resolvedSymbol.flags & 8 /* EnumMember */) { + error(node, ts.Diagnostics.Enum_type_0_has_members_with_initializers_that_are_not_literals, typeToString(type)); } } - var rightType = checkNonNullExpression(node.expression); - // unknownType is returned i.e. if node.expression is identifier whose name cannot be resolved - // in this case error about missing name is already reported - do not report extra one - if (!isTypeAnyOrAllConstituentTypesHaveKind(rightType, 2588672 /* ObjectType */ | 16384 /* TypeParameter */)) { - error(node.expression, ts.Diagnostics.The_right_hand_side_of_a_for_in_statement_must_be_of_type_any_an_object_type_or_a_type_parameter); - } - checkSourceElement(node.statement); - if (node.locals) { - registerForUnusedIdentifiersCheck(node); - } } - function checkForInOrForOfVariableDeclaration(iterationStatement) { - var variableDeclarationList = iterationStatement.initializer; - // checkGrammarForInOrForOfStatement will check that there is exactly one declaration. - if (variableDeclarationList.declarations.length >= 1) { - var decl = variableDeclarationList.declarations[0]; - checkVariableDeclaration(decl); + function checkTypeQuery(node) { + getTypeFromTypeQueryNode(node); + } + function checkTypeLiteral(node) { + ts.forEach(node.members, checkSourceElement); + if (produceDiagnostics) { + var type = getTypeFromTypeLiteralOrFunctionOrConstructorTypeNode(node); + checkIndexConstraints(type); + checkTypeForDuplicateIndexSignatures(node); + checkObjectTypeForDuplicateDeclarations(node); } } - function checkRightHandSideOfForOf(rhsExpression) { - var expressionType = checkNonNullExpression(rhsExpression); - return checkIteratedTypeOrElementType(expressionType, rhsExpression, /*allowStringInput*/ true); + function checkArrayType(node) { + checkSourceElement(node.elementType); } - function checkIteratedTypeOrElementType(inputType, errorNode, allowStringInput) { - if (isTypeAny(inputType)) { - return inputType; - } - if (languageVersion >= 2 /* ES6 */) { - return checkElementTypeOfIterable(inputType, errorNode); - } - if (allowStringInput) { - return checkElementTypeOfArrayOrString(inputType, errorNode); + function checkTupleType(node) { + // Grammar checking + var hasErrorFromDisallowedTrailingComma = checkGrammarForDisallowedTrailingComma(node.elementTypes); + if (!hasErrorFromDisallowedTrailingComma && node.elementTypes.length === 0) { + grammarErrorOnNode(node, ts.Diagnostics.A_tuple_type_element_list_cannot_be_empty); } - if (isArrayLikeType(inputType)) { - var indexType = getIndexTypeOfType(inputType, 1 /* Number */); - if (indexType) { - return indexType; + ts.forEach(node.elementTypes, checkSourceElement); + } + function checkUnionOrIntersectionType(node) { + ts.forEach(node.types, checkSourceElement); + } + function isPrivateWithinAmbient(node) { + return (ts.getModifierFlags(node) & 8 /* Private */) && ts.isInAmbientContext(node); + } + function getEffectiveDeclarationFlags(n, flagsToCheck) { + var flags = ts.getCombinedModifierFlags(n); + // children of classes (even ambient classes) should not be marked as ambient or export + // because those flags have no useful semantics there. + if (n.parent.kind !== 222 /* InterfaceDeclaration */ && + n.parent.kind !== 221 /* ClassDeclaration */ && + n.parent.kind !== 192 /* ClassExpression */ && + ts.isInAmbientContext(n)) { + if (!(flags & 2 /* Ambient */)) { + // It is nested in an ambient context, which means it is automatically exported + flags |= 1 /* Export */; } + flags |= 2 /* Ambient */; } - if (errorNode) { - error(errorNode, ts.Diagnostics.Type_0_is_not_an_array_type, typeToString(inputType)); - } - return unknownType; + return flags & flagsToCheck; } - /** - * When errorNode is undefined, it means we should not report any errors. - */ - function checkElementTypeOfIterable(iterable, errorNode) { - var elementType = getElementTypeOfIterable(iterable, errorNode); - // Now even though we have extracted the iteratedType, we will have to validate that the type - // passed in is actually an Iterable. - if (errorNode && elementType) { - checkTypeAssignableTo(iterable, createIterableType(elementType), errorNode); + function checkFunctionOrConstructorSymbol(symbol) { + if (!produceDiagnostics) { + return; } - return elementType || anyType; - } - /** - * We want to treat type as an iterable, and get the type it is an iterable of. The iterable - * must have the following structure (annotated with the names of the variables below): - * - * { // iterable - * [Symbol.iterator]: { // iteratorFunction - * (): Iterator - * } - * } - * - * T is the type we are after. At every level that involves analyzing return types - * of signatures, we union the return types of all the signatures. - * - * Another thing to note is that at any step of this process, we could run into a dead end, - * meaning either the property is missing, or we run into the anyType. If either of these things - * happens, we return undefined to signal that we could not find the iterated type. If a property - * is missing, and the previous step did not result in 'any', then we also give an error if the - * caller requested it. Then the caller can decide what to do in the case where there is no iterated - * type. This is different from returning anyType, because that would signify that we have matched the - * whole pattern and that T (above) is 'any'. - */ - function getElementTypeOfIterable(type, errorNode) { - if (isTypeAny(type)) { - return undefined; + function getCanonicalOverload(overloads, implementation) { + // Consider the canonical set of flags to be the flags of the bodyDeclaration or the first declaration + // Error on all deviations from this canonical set of flags + // The caveat is that if some overloads are defined in lib.d.ts, we don't want to + // report the errors on those. To achieve this, we will say that the implementation is + // the canonical signature only if it is in the same container as the first overload + var implementationSharesContainerWithFirstOverload = implementation !== undefined && implementation.parent === overloads[0].parent; + return implementationSharesContainerWithFirstOverload ? implementation : overloads[0]; } - var typeAsIterable = type; - if (!typeAsIterable.iterableElementType) { - // As an optimization, if the type is instantiated directly using the globalIterableType (Iterable), - // then just grab its type argument. - if ((type.flags & 131072 /* Reference */) && type.target === getGlobalIterableType()) { - typeAsIterable.iterableElementType = type.typeArguments[0]; - } - else { - var iteratorFunction = getTypeOfPropertyOfType(type, ts.getPropertyNameForKnownSymbolName("iterator")); - if (isTypeAny(iteratorFunction)) { - return undefined; - } - var iteratorFunctionSignatures = iteratorFunction ? getSignaturesOfType(iteratorFunction, 0 /* Call */) : emptyArray; - if (iteratorFunctionSignatures.length === 0) { - if (errorNode) { - error(errorNode, ts.Diagnostics.Type_must_have_a_Symbol_iterator_method_that_returns_an_iterator); + function checkFlagAgreementBetweenOverloads(overloads, implementation, flagsToCheck, someOverloadFlags, allOverloadFlags) { + // Error if some overloads have a flag that is not shared by all overloads. To find the + // deviations, we XOR someOverloadFlags with allOverloadFlags + var someButNotAllOverloadFlags = someOverloadFlags ^ allOverloadFlags; + if (someButNotAllOverloadFlags !== 0) { + var canonicalFlags_1 = getEffectiveDeclarationFlags(getCanonicalOverload(overloads, implementation), flagsToCheck); + ts.forEach(overloads, function (o) { + var deviation = getEffectiveDeclarationFlags(o, flagsToCheck) ^ canonicalFlags_1; + if (deviation & 1 /* Export */) { + error(o.name, ts.Diagnostics.Overload_signatures_must_all_be_exported_or_non_exported); } - return undefined; - } - typeAsIterable.iterableElementType = getElementTypeOfIterator(getUnionType(ts.map(iteratorFunctionSignatures, getReturnTypeOfSignature), /*subtypeReduction*/ true), errorNode); + else if (deviation & 2 /* Ambient */) { + error(o.name, ts.Diagnostics.Overload_signatures_must_all_be_ambient_or_non_ambient); + } + else if (deviation & (8 /* Private */ | 16 /* Protected */)) { + error(o.name || o, ts.Diagnostics.Overload_signatures_must_all_be_public_private_or_protected); + } + else if (deviation & 128 /* Abstract */) { + error(o.name, ts.Diagnostics.Overload_signatures_must_all_be_abstract_or_non_abstract); + } + }); } } - return typeAsIterable.iterableElementType; - } - /** - * This function has very similar logic as getElementTypeOfIterable, except that it operates on - * Iterators instead of Iterables. Here is the structure: - * - * { // iterator - * next: { // iteratorNextFunction - * (): { // iteratorNextResult - * value: T // iteratorNextValue - * } - * } - * } - * - */ - function getElementTypeOfIterator(type, errorNode) { - if (isTypeAny(type)) { - return undefined; + function checkQuestionTokenAgreementBetweenOverloads(overloads, implementation, someHaveQuestionToken, allHaveQuestionToken) { + if (someHaveQuestionToken !== allHaveQuestionToken) { + var canonicalHasQuestionToken_1 = ts.hasQuestionToken(getCanonicalOverload(overloads, implementation)); + ts.forEach(overloads, function (o) { + var deviation = ts.hasQuestionToken(o) !== canonicalHasQuestionToken_1; + if (deviation) { + error(o.name, ts.Diagnostics.Overload_signatures_must_all_be_optional_or_required); + } + }); + } } - var typeAsIterator = type; - if (!typeAsIterator.iteratorElementType) { - // As an optimization, if the type is instantiated directly using the globalIteratorType (Iterator), - // then just grab its type argument. - if ((type.flags & 131072 /* Reference */) && type.target === getGlobalIteratorType()) { - typeAsIterator.iteratorElementType = type.typeArguments[0]; + var flagsToCheck = 1 /* Export */ | 2 /* Ambient */ | 8 /* Private */ | 16 /* Protected */ | 128 /* Abstract */; + var someNodeFlags = 0 /* None */; + var allNodeFlags = flagsToCheck; + var someHaveQuestionToken = false; + var allHaveQuestionToken = true; + var hasOverloads = false; + var bodyDeclaration; + var lastSeenNonAmbientDeclaration; + var previousDeclaration; + var declarations = symbol.declarations; + var isConstructor = (symbol.flags & 16384 /* Constructor */) !== 0; + function reportImplementationExpectedError(node) { + if (node.name && ts.nodeIsMissing(node.name)) { + return; } - else { - var iteratorNextFunction = getTypeOfPropertyOfType(type, "next"); - if (isTypeAny(iteratorNextFunction)) { - return undefined; - } - var iteratorNextFunctionSignatures = iteratorNextFunction ? getSignaturesOfType(iteratorNextFunction, 0 /* Call */) : emptyArray; - if (iteratorNextFunctionSignatures.length === 0) { - if (errorNode) { - error(errorNode, ts.Diagnostics.An_iterator_must_have_a_next_method); - } - return undefined; - } - var iteratorNextResult = getUnionType(ts.map(iteratorNextFunctionSignatures, getReturnTypeOfSignature), /*subtypeReduction*/ true); - if (isTypeAny(iteratorNextResult)) { - return undefined; + var seen = false; + var subsequentNode = ts.forEachChild(node.parent, function (c) { + if (seen) { + return c; } - var iteratorNextValue = getTypeOfPropertyOfType(iteratorNextResult, "value"); - if (!iteratorNextValue) { - if (errorNode) { - error(errorNode, ts.Diagnostics.The_type_returned_by_the_next_method_of_an_iterator_must_have_a_value_property); - } - return undefined; + else { + seen = c === node; } - typeAsIterator.iteratorElementType = iteratorNextValue; - } - } - return typeAsIterator.iteratorElementType; - } - function getElementTypeOfIterableIterator(type) { - if (isTypeAny(type)) { - return undefined; - } - // As an optimization, if the type is instantiated directly using the globalIterableIteratorType (IterableIterator), - // then just grab its type argument. - if ((type.flags & 131072 /* Reference */) && type.target === getGlobalIterableIteratorType()) { - return type.typeArguments[0]; - } - return getElementTypeOfIterable(type, /*errorNode*/ undefined) || - getElementTypeOfIterator(type, /*errorNode*/ undefined); - } - /** - * This function does the following steps: - * 1. Break up arrayOrStringType (possibly a union) into its string constituents and array constituents. - * 2. Take the element types of the array constituents. - * 3. Return the union of the element types, and string if there was a string constituent. - * - * For example: - * string -> string - * number[] -> number - * string[] | number[] -> string | number - * string | number[] -> string | number - * string | string[] | number[] -> string | number - * - * It also errors if: - * 1. Some constituent is neither a string nor an array. - * 2. Some constituent is a string and target is less than ES5 (because in ES3 string is not indexable). - */ - function checkElementTypeOfArrayOrString(arrayOrStringType, errorNode) { - ts.Debug.assert(languageVersion < 2 /* ES6 */); - // After we remove all types that are StringLike, we will know if there was a string constituent - // based on whether the remaining type is the same as the initial type. - var arrayType = arrayOrStringType; - if (arrayOrStringType.flags & 524288 /* Union */) { - arrayType = getUnionType(ts.filter(arrayOrStringType.types, function (t) { return !(t.flags & 34 /* StringLike */); }), /*subtypeReduction*/ true); - } - else if (arrayOrStringType.flags & 34 /* StringLike */) { - arrayType = neverType; - } - var hasStringConstituent = arrayOrStringType !== arrayType; - var reportedError = false; - if (hasStringConstituent) { - if (languageVersion < 1 /* ES5 */) { - error(errorNode, ts.Diagnostics.Using_a_string_in_a_for_of_statement_is_only_supported_in_ECMAScript_5_and_higher); - reportedError = true; - } - // Now that we've removed all the StringLike types, if no constituents remain, then the entire - // arrayOrStringType was a string. - if (arrayType.flags & 8192 /* Never */) { - return stringType; - } - } - if (!isArrayLikeType(arrayType)) { - if (!reportedError) { - // Which error we report depends on whether there was a string constituent. For example, - // if the input type is number | string, we want to say that number is not an array type. - // But if the input was just number, we want to say that number is not an array type - // or a string type. - var diagnostic = hasStringConstituent - ? ts.Diagnostics.Type_0_is_not_an_array_type - : ts.Diagnostics.Type_0_is_not_an_array_type_or_a_string_type; - error(errorNode, diagnostic, typeToString(arrayType)); - } - return hasStringConstituent ? stringType : unknownType; - } - var arrayElementType = getIndexTypeOfType(arrayType, 1 /* Number */) || unknownType; - if (hasStringConstituent) { - // This is just an optimization for the case where arrayOrStringType is string | string[] - if (arrayElementType.flags & 34 /* StringLike */) { - return stringType; - } - return getUnionType([arrayElementType, stringType], /*subtypeReduction*/ true); - } - return arrayElementType; - } - function checkBreakOrContinueStatement(node) { - // Grammar checking - checkGrammarStatementInAmbientContext(node) || checkGrammarBreakOrContinueStatement(node); - // TODO: Check that target label is valid - } - function isGetAccessorWithAnnotatedSetAccessor(node) { - return !!(node.kind === 149 /* GetAccessor */ && ts.getSetAccessorTypeAnnotationNode(ts.getDeclarationOfKind(node.symbol, 150 /* SetAccessor */))); - } - function isUnwrappedReturnTypeVoidOrAny(func, returnType) { - var unwrappedReturnType = ts.isAsyncFunctionLike(func) ? getPromisedType(returnType) : returnType; - return unwrappedReturnType && maybeTypeOfKind(unwrappedReturnType, 1024 /* Void */ | 1 /* Any */); - } - function checkReturnStatement(node) { - // Grammar checking - if (!checkGrammarStatementInAmbientContext(node)) { - var functionBlock = ts.getContainingFunction(node); - if (!functionBlock) { - grammarErrorOnFirstToken(node, ts.Diagnostics.A_return_statement_can_only_be_used_within_a_function_body); - } - } - var func = ts.getContainingFunction(node); - if (func) { - var signature = getSignatureFromDeclaration(func); - var returnType = getReturnTypeOfSignature(signature); - if (strictNullChecks || node.expression || returnType.flags & 8192 /* Never */) { - var exprType = node.expression ? checkExpressionCached(node.expression) : undefinedType; - if (func.asteriskToken) { - // A generator does not need its return expressions checked against its return type. - // Instead, the yield expressions are checked against the element type. - // TODO: Check return expressions of generators when return type tracking is added - // for generators. - return; - } - if (func.kind === 150 /* SetAccessor */) { - if (node.expression) { - error(node.expression, ts.Diagnostics.Setters_cannot_return_a_value); - } - } - else if (func.kind === 148 /* Constructor */) { - if (node.expression && !checkTypeAssignableTo(exprType, returnType, node.expression)) { - error(node.expression, ts.Diagnostics.Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class); - } - } - else if (func.type || isGetAccessorWithAnnotatedSetAccessor(func)) { - if (ts.isAsyncFunctionLike(func)) { - var promisedType = getPromisedType(returnType); - var awaitedType = checkAwaitedType(exprType, node.expression || node, ts.Diagnostics.Return_expression_in_async_function_does_not_have_a_valid_callable_then_member); - if (promisedType) { - // If the function has a return type, but promisedType is - // undefined, an error will be reported in checkAsyncFunctionReturnType - // so we don't need to report one here. - checkTypeAssignableTo(awaitedType, promisedType, node.expression || node); + }); + // We may be here because of some extra nodes between overloads that could not be parsed into a valid node. + // In this case the subsequent node is not really consecutive (.pos !== node.end), and we must ignore it here. + if (subsequentNode && subsequentNode.pos === node.end) { + if (subsequentNode.kind === node.kind) { + var errorNode_1 = subsequentNode.name || subsequentNode; + // TODO(jfreeman): These are methods, so handle computed name case + if (node.name && subsequentNode.name && node.name.text === subsequentNode.name.text) { + var reportError = (node.kind === 147 /* MethodDeclaration */ || node.kind === 146 /* MethodSignature */) && + (ts.getModifierFlags(node) & 32 /* Static */) !== (ts.getModifierFlags(subsequentNode) & 32 /* Static */); + // we can get here in two cases + // 1. mixed static and instance class members + // 2. something with the same name was defined before the set of overloads that prevents them from merging + // here we'll report error only for the first case since for second we should already report error in binder + if (reportError) { + var diagnostic = ts.getModifierFlags(node) & 32 /* Static */ ? ts.Diagnostics.Function_overload_must_be_static : ts.Diagnostics.Function_overload_must_not_be_static; + error(errorNode_1, diagnostic); } + return; } - else { - checkTypeAssignableTo(exprType, returnType, node.expression || node); + else if (ts.nodeIsPresent(subsequentNode.body)) { + error(errorNode_1, ts.Diagnostics.Function_implementation_name_must_be_0, ts.declarationNameToString(node.name)); + return; } } } - else if (func.kind !== 148 /* Constructor */ && compilerOptions.noImplicitReturns && !isUnwrappedReturnTypeVoidOrAny(func, returnType)) { - // The function has a return type, but the return statement doesn't have an expression. - error(node, ts.Diagnostics.Not_all_code_paths_return_a_value); - } - } - } - function checkWithStatement(node) { - // Grammar checking for withStatement - if (!checkGrammarStatementInAmbientContext(node)) { - if (node.flags & 262144 /* AwaitContext */) { - grammarErrorOnFirstToken(node, ts.Diagnostics.with_statements_are_not_allowed_in_an_async_function_block); + var errorNode = node.name || node; + if (isConstructor) { + error(errorNode, ts.Diagnostics.Constructor_implementation_is_missing); } - } - checkExpression(node.expression); - error(node.expression, ts.Diagnostics.All_symbols_within_a_with_block_will_be_resolved_to_any); - } - function checkSwitchStatement(node) { - // Grammar checking - checkGrammarStatementInAmbientContext(node); - var firstDefaultClause; - var hasDuplicateDefaultClause = false; - var expressionType = checkExpression(node.expression); - ts.forEach(node.caseBlock.clauses, function (clause) { - // Grammar check for duplicate default clauses, skip if we already report duplicate default clause - if (clause.kind === 250 /* DefaultClause */ && !hasDuplicateDefaultClause) { - if (firstDefaultClause === undefined) { - firstDefaultClause = clause; + else { + // Report different errors regarding non-consecutive blocks of declarations depending on whether + // the node in question is abstract. + if (ts.getModifierFlags(node) & 128 /* Abstract */) { + error(errorNode, ts.Diagnostics.All_declarations_of_an_abstract_method_must_be_consecutive); } else { - var sourceFile = ts.getSourceFileOfNode(node); - var start = ts.skipTrivia(sourceFile.text, clause.pos); - var end = clause.statements.length > 0 ? clause.statements[0].pos : clause.end; - grammarErrorAtPos(sourceFile, start, end - start, ts.Diagnostics.A_default_clause_cannot_appear_more_than_once_in_a_switch_statement); - hasDuplicateDefaultClause = true; - } - } - if (produceDiagnostics && clause.kind === 249 /* CaseClause */) { - var caseClause = clause; - // TypeScript 1.0 spec (April 2014): 5.9 - // In a 'switch' statement, each 'case' expression must be of a type that is comparable - // to or from the type of the 'switch' expression. - var caseType = checkExpression(caseClause.expression); - if (!isTypeEqualityComparableTo(expressionType, caseType)) { - // expressionType is not comparable to caseType, try the reversed check and report errors if it fails - checkTypeComparableTo(caseType, expressionType, caseClause.expression, /*headMessage*/ undefined); - } - } - ts.forEach(clause.statements, checkSourceElement); - }); - if (node.caseBlock.locals) { - registerForUnusedIdentifiersCheck(node.caseBlock); - } - } - function checkLabeledStatement(node) { - // Grammar checking - if (!checkGrammarStatementInAmbientContext(node)) { - var current = node.parent; - while (current) { - if (ts.isFunctionLike(current)) { - break; - } - if (current.kind === 214 /* LabeledStatement */ && current.label.text === node.label.text) { - var sourceFile = ts.getSourceFileOfNode(node); - grammarErrorOnNode(node.label, ts.Diagnostics.Duplicate_label_0, ts.getTextOfNodeFromSourceText(sourceFile.text, node.label)); - break; + error(errorNode, ts.Diagnostics.Function_implementation_is_missing_or_not_immediately_following_the_declaration); } - current = current.parent; } } - // ensure that label is unique - checkSourceElement(node.statement); - } - function checkThrowStatement(node) { - // Grammar checking - if (!checkGrammarStatementInAmbientContext(node)) { - if (node.expression === undefined) { - grammarErrorAfterFirstToken(node, ts.Diagnostics.Line_break_not_permitted_here); + var duplicateFunctionDeclaration = false; + var multipleConstructorImplementation = false; + for (var _i = 0, declarations_4 = declarations; _i < declarations_4.length; _i++) { + var current = declarations_4[_i]; + var node = current; + var inAmbientContext = ts.isInAmbientContext(node); + var inAmbientContextOrInterface = node.parent.kind === 222 /* InterfaceDeclaration */ || node.parent.kind === 159 /* TypeLiteral */ || inAmbientContext; + if (inAmbientContextOrInterface) { + // check if declarations are consecutive only if they are non-ambient + // 1. ambient declarations can be interleaved + // i.e. this is legal + // declare function foo(); + // declare function bar(); + // declare function foo(); + // 2. mixing ambient and non-ambient declarations is a separate error that will be reported - do not want to report an extra one + previousDeclaration = undefined; } - } - if (node.expression) { - checkExpression(node.expression); - } - } - function checkTryStatement(node) { - // Grammar checking - checkGrammarStatementInAmbientContext(node); - checkBlock(node.tryBlock); - var catchClause = node.catchClause; - if (catchClause) { - // Grammar checking - if (catchClause.variableDeclaration) { - if (catchClause.variableDeclaration.name.kind !== 69 /* Identifier */) { - grammarErrorOnFirstToken(catchClause.variableDeclaration.name, ts.Diagnostics.Catch_clause_variable_name_must_be_an_identifier); + if (node.kind === 220 /* FunctionDeclaration */ || node.kind === 147 /* MethodDeclaration */ || node.kind === 146 /* MethodSignature */ || node.kind === 148 /* Constructor */) { + var currentNodeFlags = getEffectiveDeclarationFlags(node, flagsToCheck); + someNodeFlags |= currentNodeFlags; + allNodeFlags &= currentNodeFlags; + someHaveQuestionToken = someHaveQuestionToken || ts.hasQuestionToken(node); + allHaveQuestionToken = allHaveQuestionToken && ts.hasQuestionToken(node); + if (ts.nodeIsPresent(node.body) && bodyDeclaration) { + if (isConstructor) { + multipleConstructorImplementation = true; + } + else { + duplicateFunctionDeclaration = true; + } } - else if (catchClause.variableDeclaration.type) { - grammarErrorOnFirstToken(catchClause.variableDeclaration.type, ts.Diagnostics.Catch_clause_variable_cannot_have_a_type_annotation); + else if (previousDeclaration && previousDeclaration.parent === node.parent && previousDeclaration.end !== node.pos) { + reportImplementationExpectedError(previousDeclaration); } - else if (catchClause.variableDeclaration.initializer) { - grammarErrorOnFirstToken(catchClause.variableDeclaration.initializer, ts.Diagnostics.Catch_clause_variable_cannot_have_an_initializer); + if (ts.nodeIsPresent(node.body)) { + if (!bodyDeclaration) { + bodyDeclaration = node; + } } else { - var identifierName = catchClause.variableDeclaration.name.text; - var locals = catchClause.block.locals; - if (locals) { - var localSymbol = locals[identifierName]; - if (localSymbol && (localSymbol.flags & 2 /* BlockScopedVariable */) !== 0) { - grammarErrorOnNode(localSymbol.valueDeclaration, ts.Diagnostics.Cannot_redeclare_identifier_0_in_catch_clause, identifierName); - } - } + hasOverloads = true; + } + previousDeclaration = node; + if (!inAmbientContextOrInterface) { + lastSeenNonAmbientDeclaration = node; } } - checkBlock(catchClause.block); } - if (node.finallyBlock) { - checkBlock(node.finallyBlock); + if (multipleConstructorImplementation) { + ts.forEach(declarations, function (declaration) { + error(declaration, ts.Diagnostics.Multiple_constructor_implementations_are_not_allowed); + }); } - } - function checkIndexConstraints(type) { - var declaredNumberIndexer = getIndexDeclarationOfSymbol(type.symbol, 1 /* Number */); - var declaredStringIndexer = getIndexDeclarationOfSymbol(type.symbol, 0 /* String */); - var stringIndexType = getIndexTypeOfType(type, 0 /* String */); - var numberIndexType = getIndexTypeOfType(type, 1 /* Number */); - if (stringIndexType || numberIndexType) { - ts.forEach(getPropertiesOfObjectType(type), function (prop) { - var propType = getTypeOfSymbol(prop); - checkIndexConstraintForProperty(prop, propType, type, declaredStringIndexer, stringIndexType, 0 /* String */); - checkIndexConstraintForProperty(prop, propType, type, declaredNumberIndexer, numberIndexType, 1 /* Number */); + if (duplicateFunctionDeclaration) { + ts.forEach(declarations, function (declaration) { + error(declaration.name, ts.Diagnostics.Duplicate_function_implementation); }); - if (type.flags & 32768 /* Class */ && ts.isClassLike(type.symbol.valueDeclaration)) { - var classDeclaration = type.symbol.valueDeclaration; - for (var _i = 0, _a = classDeclaration.members; _i < _a.length; _i++) { - var member = _a[_i]; - // Only process instance properties with computed names here. - // Static properties cannot be in conflict with indexers, - // and properties with literal names were already checked. - if (!(ts.getModifierFlags(member) & 32 /* Static */) && ts.hasDynamicName(member)) { - var propType = getTypeOfSymbol(member.symbol); - checkIndexConstraintForProperty(member.symbol, propType, type, declaredStringIndexer, stringIndexType, 0 /* String */); - checkIndexConstraintForProperty(member.symbol, propType, type, declaredNumberIndexer, numberIndexType, 1 /* Number */); + } + // Abstract methods can't have an implementation -- in particular, they don't need one. + if (lastSeenNonAmbientDeclaration && !lastSeenNonAmbientDeclaration.body && + !(ts.getModifierFlags(lastSeenNonAmbientDeclaration) & 128 /* Abstract */) && !lastSeenNonAmbientDeclaration.questionToken) { + reportImplementationExpectedError(lastSeenNonAmbientDeclaration); + } + if (hasOverloads) { + checkFlagAgreementBetweenOverloads(declarations, bodyDeclaration, flagsToCheck, someNodeFlags, allNodeFlags); + checkQuestionTokenAgreementBetweenOverloads(declarations, bodyDeclaration, someHaveQuestionToken, allHaveQuestionToken); + if (bodyDeclaration) { + var signatures = getSignaturesOfSymbol(symbol); + var bodySignature = getSignatureFromDeclaration(bodyDeclaration); + for (var _a = 0, signatures_3 = signatures; _a < signatures_3.length; _a++) { + var signature = signatures_3[_a]; + if (!isImplementationCompatibleWithOverload(bodySignature, signature)) { + error(signature.declaration, ts.Diagnostics.Overload_signature_is_not_compatible_with_function_implementation); + break; } } } } - var errorNode; - if (stringIndexType && numberIndexType) { - errorNode = declaredNumberIndexer || declaredStringIndexer; - // condition 'errorNode === undefined' may appear if types does not declare nor string neither number indexer - if (!errorNode && (type.flags & 65536 /* Interface */)) { - var someBaseTypeHasBothIndexers = ts.forEach(getBaseTypes(type), function (base) { return getIndexTypeOfType(base, 0 /* String */) && getIndexTypeOfType(base, 1 /* Number */); }); - errorNode = someBaseTypeHasBothIndexers ? undefined : type.symbol.declarations[0]; - } - } - if (errorNode && !isTypeAssignableTo(numberIndexType, stringIndexType)) { - error(errorNode, ts.Diagnostics.Numeric_index_type_0_is_not_assignable_to_string_index_type_1, typeToString(numberIndexType), typeToString(stringIndexType)); + } + function checkExportsOnMergedDeclarations(node) { + if (!produceDiagnostics) { + return; } - function checkIndexConstraintForProperty(prop, propertyType, containingType, indexDeclaration, indexType, indexKind) { - if (!indexType) { - return; - } - // index is numeric and property name is not valid numeric literal - if (indexKind === 1 /* Number */ && !isNumericName(prop.valueDeclaration.name)) { + // if localSymbol is defined on node then node itself is exported - check is required + var symbol = node.localSymbol; + if (!symbol) { + // local symbol is undefined => this declaration is non-exported. + // however symbol might contain other declarations that are exported + symbol = getSymbolOfNode(node); + if (!(symbol.flags & 7340032 /* Export */)) { + // this is a pure local symbol (all declarations are non-exported) - no need to check anything return; } - // perform property check if property or indexer is declared in 'type' - // this allows to rule out cases when both property and indexer are inherited from the base class - var errorNode; - if (prop.valueDeclaration.name.kind === 140 /* ComputedPropertyName */ || prop.parent === containingType.symbol) { - errorNode = prop.valueDeclaration; - } - else if (indexDeclaration) { - errorNode = indexDeclaration; - } - else if (containingType.flags & 65536 /* Interface */) { - // for interfaces property and indexer might be inherited from different bases - // check if any base class already has both property and indexer. - // check should be performed only if 'type' is the first type that brings property\indexer together - var someBaseClassHasBothPropertyAndIndexer = ts.forEach(getBaseTypes(containingType), function (base) { return getPropertyOfObjectType(base, prop.name) && getIndexTypeOfType(base, indexKind); }); - errorNode = someBaseClassHasBothPropertyAndIndexer ? undefined : containingType.symbol.declarations[0]; - } - if (errorNode && !isTypeAssignableTo(propertyType, indexType)) { - var errorMessage = indexKind === 0 /* String */ - ? ts.Diagnostics.Property_0_of_type_1_is_not_assignable_to_string_index_type_2 - : ts.Diagnostics.Property_0_of_type_1_is_not_assignable_to_numeric_index_type_2; - error(errorNode, errorMessage, symbolToString(prop), typeToString(propertyType), typeToString(indexType)); - } } - } - function checkTypeNameIsReserved(name, message) { - // TS 1.0 spec (April 2014): 3.6.1 - // The predefined type keywords are reserved and cannot be used as names of user defined types. - switch (name.text) { - case "any": - case "number": - case "boolean": - case "string": - case "symbol": - case "void": - error(name, message, name.text); + // run the check only for the first declaration in the list + if (ts.getDeclarationOfKind(symbol, node.kind) !== node) { + return; } - } - /** Check each type parameter and check that type parameters have no duplicate type parameter declarations */ - function checkTypeParameters(typeParameterDeclarations) { - if (typeParameterDeclarations) { - for (var i = 0, n = typeParameterDeclarations.length; i < n; i++) { - var node = typeParameterDeclarations[i]; - checkTypeParameter(node); - if (produceDiagnostics) { - for (var j = 0; j < i; j++) { - if (typeParameterDeclarations[j].symbol === node.symbol) { - error(node.name, ts.Diagnostics.Duplicate_identifier_0, ts.declarationNameToString(node.name)); - } - } + // we use SymbolFlags.ExportValue, SymbolFlags.ExportType and SymbolFlags.ExportNamespace + // to denote disjoint declarationSpaces (without making new enum type). + var exportedDeclarationSpaces = 0 /* None */; + var nonExportedDeclarationSpaces = 0 /* None */; + var defaultExportedDeclarationSpaces = 0 /* None */; + for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { + var d = _a[_i]; + var declarationSpaces = getDeclarationSpaces(d); + var effectiveDeclarationFlags = getEffectiveDeclarationFlags(d, 1 /* Export */ | 512 /* Default */); + if (effectiveDeclarationFlags & 1 /* Export */) { + if (effectiveDeclarationFlags & 512 /* Default */) { + defaultExportedDeclarationSpaces |= declarationSpaces; + } + else { + exportedDeclarationSpaces |= declarationSpaces; } } + else { + nonExportedDeclarationSpaces |= declarationSpaces; + } } - } - /** Check that type parameter lists are identical across multiple declarations */ - function checkTypeParameterListsIdentical(node, symbol) { - if (symbol.declarations.length === 1) { - return; - } - var firstDecl; - for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { - var declaration = _a[_i]; - if (declaration.kind === 221 /* ClassDeclaration */ || declaration.kind === 222 /* InterfaceDeclaration */) { - if (!firstDecl) { - firstDecl = declaration; + // Spaces for anything not declared a 'default export'. + var nonDefaultExportedDeclarationSpaces = exportedDeclarationSpaces | nonExportedDeclarationSpaces; + var commonDeclarationSpacesForExportsAndLocals = exportedDeclarationSpaces & nonExportedDeclarationSpaces; + var commonDeclarationSpacesForDefaultAndNonDefault = defaultExportedDeclarationSpaces & nonDefaultExportedDeclarationSpaces; + if (commonDeclarationSpacesForExportsAndLocals || commonDeclarationSpacesForDefaultAndNonDefault) { + // declaration spaces for exported and non-exported declarations intersect + for (var _b = 0, _c = symbol.declarations; _b < _c.length; _b++) { + var d = _c[_b]; + var declarationSpaces = getDeclarationSpaces(d); + // Only error on the declarations that contributed to the intersecting spaces. + if (declarationSpaces & commonDeclarationSpacesForDefaultAndNonDefault) { + error(d.name, ts.Diagnostics.Merged_declaration_0_cannot_include_a_default_export_declaration_Consider_adding_a_separate_export_default_0_declaration_instead, ts.declarationNameToString(d.name)); } - else if (!areTypeParametersIdentical(firstDecl.typeParameters, node.typeParameters)) { - error(node.name, ts.Diagnostics.All_declarations_of_0_must_have_identical_type_parameters, node.name.text); + else if (declarationSpaces & commonDeclarationSpacesForExportsAndLocals) { + error(d.name, ts.Diagnostics.Individual_declarations_in_merged_declaration_0_must_be_all_exported_or_all_local, ts.declarationNameToString(d.name)); } } } - } - function checkClassExpression(node) { - checkClassLikeDeclaration(node); - checkNodeDeferred(node); - return getTypeOfSymbol(getSymbolOfNode(node)); - } - function checkClassExpressionDeferred(node) { - ts.forEach(node.members, checkSourceElement); - registerForUnusedIdentifiersCheck(node); - } - function checkClassDeclaration(node) { - if (!node.name && !(ts.getModifierFlags(node) & 512 /* Default */)) { - grammarErrorOnFirstToken(node, ts.Diagnostics.A_class_declaration_without_the_default_modifier_must_have_a_name); + function getDeclarationSpaces(d) { + switch (d.kind) { + case 222 /* InterfaceDeclaration */: + return 2097152 /* ExportType */; + case 225 /* ModuleDeclaration */: + return ts.isAmbientModule(d) || ts.getModuleInstanceState(d) !== 0 /* NonInstantiated */ + ? 4194304 /* ExportNamespace */ | 1048576 /* ExportValue */ + : 4194304 /* ExportNamespace */; + case 221 /* ClassDeclaration */: + case 224 /* EnumDeclaration */: + return 2097152 /* ExportType */ | 1048576 /* ExportValue */; + case 229 /* ImportEqualsDeclaration */: + var result_2 = 0; + var target = resolveAlias(getSymbolOfNode(d)); + ts.forEach(target.declarations, function (d) { result_2 |= getDeclarationSpaces(d); }); + return result_2; + default: + return 1048576 /* ExportValue */; + } } - checkClassLikeDeclaration(node); - ts.forEach(node.members, checkSourceElement); - registerForUnusedIdentifiersCheck(node); } - function checkClassLikeDeclaration(node) { - checkGrammarClassDeclarationHeritageClauses(node); - checkDecorators(node); - if (node.name) { - checkTypeNameIsReserved(node.name, ts.Diagnostics.Class_name_cannot_be_0); - checkCollisionWithCapturedThisVariable(node, node.name); - checkCollisionWithRequireExportsInGeneratedCode(node, node.name); - checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); - } - checkTypeParameters(node.typeParameters); - checkExportsOnMergedDeclarations(node); - var symbol = getSymbolOfNode(node); - var type = getDeclaredTypeOfSymbol(symbol); - var typeWithThis = getTypeWithThisArgument(type); - var staticType = getTypeOfSymbol(symbol); - checkTypeParameterListsIdentical(node, symbol); - checkClassForDuplicateDeclarations(node); - var baseTypeNode = ts.getClassExtendsHeritageClauseElement(node); - if (baseTypeNode) { - var baseTypes = getBaseTypes(type); - if (baseTypes.length && produceDiagnostics) { - var baseType_1 = baseTypes[0]; - var staticBaseType = getBaseConstructorTypeOfClass(type); - checkBaseTypeAccessibility(staticBaseType, baseTypeNode); - checkSourceElement(baseTypeNode.expression); - if (baseTypeNode.typeArguments) { - ts.forEach(baseTypeNode.typeArguments, checkSourceElement); - for (var _i = 0, _a = getConstructorsForTypeArguments(staticBaseType, baseTypeNode.typeArguments); _i < _a.length; _i++) { - var constructor = _a[_i]; - if (!checkTypeArgumentConstraints(constructor.typeParameters, baseTypeNode.typeArguments)) { - break; - } - } - } - checkTypeAssignableTo(typeWithThis, getTypeWithThisArgument(baseType_1, type.thisType), node.name || node, ts.Diagnostics.Class_0_incorrectly_extends_base_class_1); - checkTypeAssignableTo(staticType, getTypeWithoutSignatures(staticBaseType), node.name || node, ts.Diagnostics.Class_static_side_0_incorrectly_extends_base_class_static_side_1); - if (baseType_1.symbol.valueDeclaration && !ts.isInAmbientContext(baseType_1.symbol.valueDeclaration)) { - if (!isBlockScopedNameDeclaredBeforeUse(baseType_1.symbol.valueDeclaration, node)) { - error(baseTypeNode, ts.Diagnostics.A_class_must_be_declared_after_its_base_class); - } - } - if (!(staticBaseType.symbol && staticBaseType.symbol.flags & 32 /* Class */)) { - // When the static base type is a "class-like" constructor function (but not actually a class), we verify - // that all instantiated base constructor signatures return the same type. We can simply compare the type - // references (as opposed to checking the structure of the types) because elsewhere we have already checked - // that the base type is a class or interface type (and not, for example, an anonymous object type). - var constructors = getInstantiatedConstructorsForTypeArguments(staticBaseType, baseTypeNode.typeArguments); - if (ts.forEach(constructors, function (sig) { return getReturnTypeOfSignature(sig) !== baseType_1; })) { - error(baseTypeNode.expression, ts.Diagnostics.Base_constructors_must_all_have_the_same_return_type); - } + function checkNonThenableType(type, location, message) { + type = getWidenedType(type); + if (!isTypeAny(type) && !isTypeNever(type) && isTypeAssignableTo(type, getGlobalThenableType())) { + if (location) { + if (!message) { + message = ts.Diagnostics.Operand_for_await_does_not_have_a_valid_callable_then_member; } - checkKindsOfPropertyMemberOverrides(type, baseType_1); + error(location, message); } + return unknownType; } - var implementedTypeNodes = ts.getClassImplementsHeritageClauseElements(node); - if (implementedTypeNodes) { - for (var _b = 0, implementedTypeNodes_1 = implementedTypeNodes; _b < implementedTypeNodes_1.length; _b++) { - var typeRefNode = implementedTypeNodes_1[_b]; - if (!ts.isEntityNameExpression(typeRefNode.expression)) { - error(typeRefNode.expression, ts.Diagnostics.A_class_can_only_implement_an_identifier_Slashqualified_name_with_optional_type_arguments); - } - checkTypeReferenceNode(typeRefNode); - if (produceDiagnostics) { - var t = getTypeFromTypeNode(typeRefNode); - if (t !== unknownType) { - var declaredType = (t.flags & 131072 /* Reference */) ? t.target : t; - if (declaredType.flags & (32768 /* Class */ | 65536 /* Interface */)) { - checkTypeAssignableTo(typeWithThis, getTypeWithThisArgument(t, type.thisType), node.name || node, ts.Diagnostics.Class_0_incorrectly_implements_interface_1); - } - else { - error(typeRefNode, ts.Diagnostics.A_class_may_only_implement_another_class_or_interface); - } - } - } + return type; + } + /** + * Gets the "promised type" of a promise. + * @param type The type of the promise. + * @remarks The "promised type" of a type is the type of the "value" parameter of the "onfulfilled" callback. + */ + function getPromisedType(promise) { + // + // { // promise + // then( // thenFunction + // onfulfilled: ( // onfulfilledParameterType + // value: T // valueParameterType + // ) => any + // ): any; + // } + // + if (isTypeAny(promise)) { + return undefined; + } + if (promise.flags & 131072 /* Reference */) { + if (promise.target === tryGetGlobalPromiseType() + || promise.target === getGlobalPromiseLikeType()) { + return promise.typeArguments[0]; } } - if (produceDiagnostics) { - checkIndexConstraints(type); - checkTypeForDuplicateIndexSignatures(node); + var globalPromiseLikeType = getInstantiatedGlobalPromiseLikeType(); + if (globalPromiseLikeType === emptyObjectType || !isTypeAssignableTo(promise, globalPromiseLikeType)) { + return undefined; } - } - function checkBaseTypeAccessibility(type, node) { - var signatures = getSignaturesOfType(type, 1 /* Construct */); - if (signatures.length) { - var declaration = signatures[0].declaration; - if (declaration && ts.getModifierFlags(declaration) & 8 /* Private */) { - var typeClassDeclaration = getClassLikeDeclarationOfSymbol(type.symbol); - if (!isNodeWithinClass(node, typeClassDeclaration)) { - error(node, ts.Diagnostics.Cannot_extend_a_class_0_Class_constructor_is_marked_as_private, node.expression.text); - } - } + var thenFunction = getTypeOfPropertyOfType(promise, "then"); + if (!thenFunction || isTypeAny(thenFunction)) { + return undefined; + } + var thenSignatures = getSignaturesOfType(thenFunction, 0 /* Call */); + if (thenSignatures.length === 0) { + return undefined; + } + var onfulfilledParameterType = getTypeWithFacts(getUnionType(ts.map(thenSignatures, getTypeOfFirstParameterOfSignature)), 131072 /* NEUndefined */); + if (isTypeAny(onfulfilledParameterType)) { + return undefined; + } + var onfulfilledParameterSignatures = getSignaturesOfType(onfulfilledParameterType, 0 /* Call */); + if (onfulfilledParameterSignatures.length === 0) { + return undefined; } + return getUnionType(ts.map(onfulfilledParameterSignatures, getTypeOfFirstParameterOfSignature), /*subtypeReduction*/ true); } - function getTargetSymbol(s) { - // if symbol is instantiated its flags are not copied from the 'target' - // so we'll need to get back original 'target' symbol to work with correct set of flags - return s.flags & 16777216 /* Instantiated */ ? getSymbolLinks(s).target : s; + function getTypeOfFirstParameterOfSignature(signature) { + return signature.parameters.length > 0 ? getTypeAtPosition(signature, 0) : neverType; } - function getClassLikeDeclarationOfSymbol(symbol) { - return ts.forEach(symbol.declarations, function (d) { return ts.isClassLike(d) ? d : undefined; }); + /** + * Gets the "awaited type" of a type. + * @param type The type to await. + * @remarks The "awaited type" of an expression is its "promised type" if the expression is a + * Promise-like type; otherwise, it is the type of the expression. This is used to reflect + * The runtime behavior of the `await` keyword. + */ + function getAwaitedType(type) { + return checkAwaitedType(type, /*location*/ undefined, /*message*/ undefined); } - function checkKindsOfPropertyMemberOverrides(type, baseType) { - // TypeScript 1.0 spec (April 2014): 8.2.3 - // A derived class inherits all members from its base class it doesn't override. - // Inheritance means that a derived class implicitly contains all non - overridden members of the base class. - // Both public and private property members are inherited, but only public property members can be overridden. - // A property member in a derived class is said to override a property member in a base class - // when the derived class property member has the same name and kind(instance or static) - // as the base class property member. - // The type of an overriding property member must be assignable(section 3.8.4) - // to the type of the overridden property member, or otherwise a compile - time error occurs. - // Base class instance member functions can be overridden by derived class instance member functions, - // but not by other kinds of members. - // Base class instance member variables and accessors can be overridden by - // derived class instance member variables and accessors, but not by other kinds of members. - // NOTE: assignability is checked in checkClassDeclaration - var baseProperties = getPropertiesOfObjectType(baseType); - for (var _i = 0, baseProperties_1 = baseProperties; _i < baseProperties_1.length; _i++) { - var baseProperty = baseProperties_1[_i]; - var base = getTargetSymbol(baseProperty); - if (base.flags & 134217728 /* Prototype */) { - continue; + function checkAwaitedType(type, location, message) { + return checkAwaitedTypeWorker(type); + function checkAwaitedTypeWorker(type) { + if (type.flags & 524288 /* Union */) { + var types = []; + for (var _i = 0, _a = type.types; _i < _a.length; _i++) { + var constituentType = _a[_i]; + types.push(checkAwaitedTypeWorker(constituentType)); + } + return getUnionType(types, /*subtypeReduction*/ true); } - var derived = getTargetSymbol(getPropertyOfObjectType(type, base.name)); - var baseDeclarationFlags = getDeclarationModifierFlagsFromSymbol(base); - ts.Debug.assert(!!derived, "derived should point to something, even if it is the base class' declaration."); - if (derived) { - // In order to resolve whether the inherited method was overridden in the base class or not, - // we compare the Symbols obtained. Since getTargetSymbol returns the symbol on the *uninstantiated* - // type declaration, derived and base resolve to the same symbol even in the case of generic classes. - if (derived === base) { - // derived class inherits base without override/redeclaration - var derivedClassDecl = getClassLikeDeclarationOfSymbol(type.symbol); - // It is an error to inherit an abstract member without implementing it or being declared abstract. - // If there is no declaration for the derived class (as in the case of class expressions), - // then the class cannot be declared abstract. - if (baseDeclarationFlags & 128 /* Abstract */ && (!derivedClassDecl || !(ts.getModifierFlags(derivedClassDecl) & 128 /* Abstract */))) { - if (derivedClassDecl.kind === 192 /* ClassExpression */) { - error(derivedClassDecl, ts.Diagnostics.Non_abstract_class_expression_does_not_implement_inherited_abstract_member_0_from_class_1, symbolToString(baseProperty), typeToString(baseType)); - } - else { - error(derivedClassDecl, ts.Diagnostics.Non_abstract_class_0_does_not_implement_inherited_abstract_member_1_from_class_2, typeToString(type), symbolToString(baseProperty), typeToString(baseType)); - } - } + else { + var promisedType = getPromisedType(type); + if (promisedType === undefined) { + // The type was not a PromiseLike, so it could not be unwrapped any further. + // As long as the type does not have a callable "then" property, it is + // safe to return the type; otherwise, an error will have been reported in + // the call to checkNonThenableType and we will return unknownType. + // + // An example of a non-promise "thenable" might be: + // + // await { then(): void {} } + // + // The "thenable" does not match the minimal definition for a PromiseLike. When + // a Promise/A+-compatible or ES6 promise tries to adopt this value, the promise + // will never settle. We treat this as an error to help flag an early indicator + // of a runtime problem. If the user wants to return this value from an async + // function, they would need to wrap it in some other value. If they want it to + // be treated as a promise, they can cast to . + return checkNonThenableType(type, location, message); } else { - // derived overrides base. - var derivedDeclarationFlags = getDeclarationModifierFlagsFromSymbol(derived); - if ((baseDeclarationFlags & 8 /* Private */) || (derivedDeclarationFlags & 8 /* Private */)) { - // either base or derived property is private - not override, skip it - continue; - } - if ((baseDeclarationFlags & 32 /* Static */) !== (derivedDeclarationFlags & 32 /* Static */)) { - // value of 'static' is not the same for properties - not override, skip it - continue; - } - if ((base.flags & derived.flags & 8192 /* Method */) || ((base.flags & 98308 /* PropertyOrAccessor */) && (derived.flags & 98308 /* PropertyOrAccessor */))) { - // method is overridden with method or property/accessor is overridden with property/accessor - correct case - continue; - } - var errorMessage = void 0; - if (base.flags & 8192 /* Method */) { - if (derived.flags & 98304 /* Accessor */) { - errorMessage = ts.Diagnostics.Class_0_defines_instance_member_function_1_but_extended_class_2_defines_it_as_instance_member_accessor; - } - else { - ts.Debug.assert((derived.flags & 4 /* Property */) !== 0); - errorMessage = ts.Diagnostics.Class_0_defines_instance_member_function_1_but_extended_class_2_defines_it_as_instance_member_property; + if (type.id === promisedType.id || ts.indexOf(awaitedTypeStack, promisedType.id) >= 0) { + // We have a bad actor in the form of a promise whose promised type is + // the same promise type, or a mutually recursive promise. Return the + // unknown type as we cannot guess the shape. If this were the actual + // case in the JavaScript, this Promise would never resolve. + // + // An example of a bad actor with a singly-recursive promise type might + // be: + // + // interface BadPromise { + // then( + // onfulfilled: (value: BadPromise) => any, + // onrejected: (error: any) => any): BadPromise; + // } + // + // The above interface will pass the PromiseLike check, and return a + // promised type of `BadPromise`. Since this is a self reference, we + // don't want to keep recursing ad infinitum. + // + // An example of a bad actor in the form of a mutually-recursive + // promise type might be: + // + // interface BadPromiseA { + // then( + // onfulfilled: (value: BadPromiseB) => any, + // onrejected: (error: any) => any): BadPromiseB; + // } + // + // interface BadPromiseB { + // then( + // onfulfilled: (value: BadPromiseA) => any, + // onrejected: (error: any) => any): BadPromiseA; + // } + // + if (location) { + error(location, ts.Diagnostics._0_is_referenced_directly_or_indirectly_in_the_fulfillment_callback_of_its_own_then_method, symbolToString(type.symbol)); } + return unknownType; } - else if (base.flags & 4 /* Property */) { - ts.Debug.assert((derived.flags & 8192 /* Method */) !== 0); - errorMessage = ts.Diagnostics.Class_0_defines_instance_member_property_1_but_extended_class_2_defines_it_as_instance_member_function; - } - else { - ts.Debug.assert((base.flags & 98304 /* Accessor */) !== 0); - ts.Debug.assert((derived.flags & 8192 /* Method */) !== 0); - errorMessage = ts.Diagnostics.Class_0_defines_instance_member_accessor_1_but_extended_class_2_defines_it_as_instance_member_function; - } - error(derived.valueDeclaration.name, errorMessage, typeToString(baseType), symbolToString(base), typeToString(type)); + // Keep track of the type we're about to unwrap to avoid bad recursive promise types. + // See the comments above for more information. + awaitedTypeStack.push(type.id); + var awaitedType = checkAwaitedTypeWorker(promisedType); + awaitedTypeStack.pop(); + return awaitedType; } } } } - function isAccessor(kind) { - return kind === 149 /* GetAccessor */ || kind === 150 /* SetAccessor */; + /** + * Checks that the return type provided is an instantiation of the global Promise type + * and returns the awaited type of the return type. + * + * @param returnType The return type of a FunctionLikeDeclaration + * @param location The node on which to report the error. + */ + function checkCorrectPromiseType(returnType, location, diagnostic, typeName) { + if (returnType === unknownType) { + // The return type already had some other error, so we ignore and return + // the unknown type. + return unknownType; + } + var globalPromiseType = getGlobalPromiseType(); + if (globalPromiseType === emptyGenericType + || globalPromiseType === getTargetType(returnType)) { + // Either we couldn't resolve the global promise type, which would have already + // reported an error, or we could resolve it and the return type is a valid type + // reference to the global type. In either case, we return the awaited type for + // the return type. + return checkAwaitedType(returnType, location, ts.Diagnostics.An_async_function_or_method_must_have_a_valid_awaitable_return_type); + } + // The promise type was not a valid type reference to the global promise type, so we + // report an error and return the unknown type. + error(location, diagnostic, typeName); + return unknownType; } - function areTypeParametersIdentical(list1, list2) { - if (!list1 && !list2) { - return true; + /** + * Checks the return type of an async function to ensure it is a compatible + * Promise implementation. + * @param node The signature to check + * @param returnType The return type for the function + * @remarks + * This checks that an async function has a valid Promise-compatible return type, + * and returns the *awaited type* of the promise. An async function has a valid + * Promise-compatible return type if the resolved value of the return type has a + * construct signature that takes in an `initializer` function that in turn supplies + * a `resolve` function as one of its arguments and results in an object with a + * callable `then` signature. + */ + function checkAsyncFunctionReturnType(node) { + if (languageVersion >= 2 /* ES6 */) { + var returnType = getTypeFromTypeNode(node.type); + return checkCorrectPromiseType(returnType, node.type, ts.Diagnostics.The_return_type_of_an_async_function_or_method_must_be_the_global_Promise_T_type); } - if (!list1 || !list2 || list1.length !== list2.length) { - return false; + var globalPromiseConstructorLikeType = getGlobalPromiseConstructorLikeType(); + if (globalPromiseConstructorLikeType === emptyObjectType) { + // If we couldn't resolve the global PromiseConstructorLike type we cannot verify + // compatibility with __awaiter. + return unknownType; } - // TypeScript 1.0 spec (April 2014): - // When a generic interface has multiple declarations, all declarations must have identical type parameter - // lists, i.e. identical type parameter names with identical constraints in identical order. - for (var i = 0, len = list1.length; i < len; i++) { - var tp1 = list1[i]; - var tp2 = list2[i]; - if (tp1.name.text !== tp2.name.text) { - return false; - } - if (!tp1.constraint && !tp2.constraint) { - continue; - } - if (!tp1.constraint || !tp2.constraint) { - return false; - } - if (!isTypeIdenticalTo(getTypeFromTypeNode(tp1.constraint), getTypeFromTypeNode(tp2.constraint))) { - return false; - } + // As part of our emit for an async function, we will need to emit the entity name of + // the return type annotation as an expression. To meet the necessary runtime semantics + // for __awaiter, we must also check that the type of the declaration (e.g. the static + // side or "constructor" of the promise type) is compatible `PromiseConstructorLike`. + // + // An example might be (from lib.es6.d.ts): + // + // interface Promise { ... } + // interface PromiseConstructor { + // new (...): Promise; + // } + // declare var Promise: PromiseConstructor; + // + // When an async function declares a return type annotation of `Promise`, we + // need to get the type of the `Promise` variable declaration above, which would + // be `PromiseConstructor`. + // + // The same case applies to a class: + // + // declare class Promise { + // constructor(...); + // then(...): Promise; + // } + // + // When we get the type of the `Promise` symbol here, we get the type of the static + // side of the `Promise` class, which would be `{ new (...): Promise }`. + var promiseType = getTypeFromTypeNode(node.type); + if (promiseType === unknownType && compilerOptions.isolatedModules) { + // If we are compiling with isolatedModules, we may not be able to resolve the + // type as a value. As such, we will just return unknownType; + return unknownType; } - return true; + var promiseConstructor = getNodeLinks(node.type).resolvedSymbol; + if (!promiseConstructor || !symbolIsValue(promiseConstructor)) { + // try to fall back to global promise type. + var typeName = promiseConstructor + ? symbolToString(promiseConstructor) + : typeToString(promiseType); + return checkCorrectPromiseType(promiseType, node.type, ts.Diagnostics.Type_0_is_not_a_valid_async_function_return_type, typeName); + } + // If the Promise constructor, resolved locally, is an alias symbol we should mark it as referenced. + checkReturnTypeAnnotationAsExpression(node); + // Validate the promise constructor type. + var promiseConstructorType = getTypeOfSymbol(promiseConstructor); + if (!checkTypeAssignableTo(promiseConstructorType, globalPromiseConstructorLikeType, node.type, ts.Diagnostics.Type_0_is_not_a_valid_async_function_return_type)) { + return unknownType; + } + // Verify there is no local declaration that could collide with the promise constructor. + var promiseName = ts.getEntityNameFromTypeNode(node.type); + var promiseNameOrNamespaceRoot = getFirstIdentifier(promiseName); + var rootSymbol = getSymbol(node.locals, promiseNameOrNamespaceRoot.text, 107455 /* Value */); + if (rootSymbol) { + error(rootSymbol.valueDeclaration, ts.Diagnostics.Duplicate_identifier_0_Compiler_uses_declaration_1_to_support_async_functions, promiseNameOrNamespaceRoot.text, getFullyQualifiedName(promiseConstructor)); + return unknownType; + } + // Get and return the awaited type of the return type. + return checkAwaitedType(promiseType, node, ts.Diagnostics.An_async_function_or_method_must_have_a_valid_awaitable_return_type); } - function checkInheritedPropertiesAreIdentical(type, typeNode) { - var baseTypes = getBaseTypes(type); - if (baseTypes.length < 2) { - return true; + /** Check a decorator */ + function checkDecorator(node) { + var signature = getResolvedSignature(node); + var returnType = getReturnTypeOfSignature(signature); + if (returnType.flags & 1 /* Any */) { + return; } - var seen = ts.createMap(); - ts.forEach(resolveDeclaredMembers(type).declaredProperties, function (p) { seen[p.name] = { prop: p, containingType: type }; }); - var ok = true; - for (var _i = 0, baseTypes_2 = baseTypes; _i < baseTypes_2.length; _i++) { - var base = baseTypes_2[_i]; - var properties = getPropertiesOfObjectType(getTypeWithThisArgument(base, type.thisType)); - for (var _a = 0, properties_5 = properties; _a < properties_5.length; _a++) { - var prop = properties_5[_a]; - var existing = seen[prop.name]; - if (!existing) { - seen[prop.name] = { prop: prop, containingType: base }; - } - else { - var isInheritedProperty = existing.containingType !== type; - if (isInheritedProperty && !isPropertyIdenticalTo(existing.prop, prop)) { - ok = false; - var typeName1 = typeToString(existing.containingType); - var typeName2 = typeToString(base); - var errorInfo = ts.chainDiagnosticMessages(undefined, ts.Diagnostics.Named_property_0_of_types_1_and_2_are_not_identical, symbolToString(prop), typeName1, typeName2); - errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Interface_0_cannot_simultaneously_extend_types_1_and_2, typeToString(type), typeName1, typeName2); - diagnostics.add(ts.createDiagnosticForNodeFromMessageChain(typeNode, errorInfo)); - } + var expectedReturnType; + var headMessage = getDiagnosticHeadMessageForDecoratorResolution(node); + var errorInfo; + switch (node.parent.kind) { + case 221 /* ClassDeclaration */: + var classSymbol = getSymbolOfNode(node.parent); + var classConstructorType = getTypeOfSymbol(classSymbol); + expectedReturnType = getUnionType([classConstructorType, voidType]); + break; + case 142 /* Parameter */: + expectedReturnType = voidType; + errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.The_return_type_of_a_parameter_decorator_function_must_be_either_void_or_any); + break; + case 145 /* PropertyDeclaration */: + expectedReturnType = voidType; + errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.The_return_type_of_a_property_decorator_function_must_be_either_void_or_any); + break; + case 147 /* MethodDeclaration */: + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + var methodType = getTypeOfNode(node.parent); + var descriptorType = createTypedPropertyDescriptorType(methodType); + expectedReturnType = getUnionType([descriptorType, voidType]); + break; + } + checkTypeAssignableTo(returnType, expectedReturnType, node, headMessage, errorInfo); + } + /** Checks a type reference node as an expression. */ + function checkTypeNodeAsExpression(node) { + // When we are emitting type metadata for decorators, we need to try to check the type + // as if it were an expression so that we can emit the type in a value position when we + // serialize the type metadata. + if (node && node.kind === 155 /* TypeReference */) { + var root = getFirstIdentifier(node.typeName); + var meaning = root.parent.kind === 155 /* TypeReference */ ? 793064 /* Type */ : 1920 /* Namespace */; + // Resolve type so we know which symbol is referenced + var rootSymbol = resolveName(root, root.text, meaning | 8388608 /* Alias */, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined); + // Resolved symbol is alias + if (rootSymbol && rootSymbol.flags & 8388608 /* Alias */) { + var aliasTarget = resolveAlias(rootSymbol); + // If alias has value symbol - mark alias as referenced + if (aliasTarget.flags & 107455 /* Value */ && !isConstEnumOrConstEnumOnlyModule(resolveAlias(rootSymbol))) { + markAliasSymbolAsReferenced(rootSymbol); } } } - return ok; } - function checkInterfaceDeclaration(node) { - // Grammar checking - checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarInterfaceDeclaration(node); - checkTypeParameters(node.typeParameters); - if (produceDiagnostics) { - checkTypeNameIsReserved(node.name, ts.Diagnostics.Interface_name_cannot_be_0); - checkExportsOnMergedDeclarations(node); - var symbol = getSymbolOfNode(node); - checkTypeParameterListsIdentical(node, symbol); - // Only check this symbol once - var firstInterfaceDecl = ts.getDeclarationOfKind(symbol, 222 /* InterfaceDeclaration */); - if (node === firstInterfaceDecl) { - var type = getDeclaredTypeOfSymbol(symbol); - var typeWithThis = getTypeWithThisArgument(type); - // run subsequent checks only if first set succeeded - if (checkInheritedPropertiesAreIdentical(type, node.name)) { - for (var _i = 0, _a = getBaseTypes(type); _i < _a.length; _i++) { - var baseType = _a[_i]; - checkTypeAssignableTo(typeWithThis, getTypeWithThisArgument(baseType, type.thisType), node.name, ts.Diagnostics.Interface_0_incorrectly_extends_interface_1); + /** + * Checks the type annotation of an accessor declaration or property declaration as + * an expression if it is a type reference to a type with a value declaration. + */ + function checkTypeAnnotationAsExpression(node) { + checkTypeNodeAsExpression(node.type); + } + function checkReturnTypeAnnotationAsExpression(node) { + checkTypeNodeAsExpression(node.type); + } + /** Checks the type annotation of the parameters of a function/method or the constructor of a class as expressions */ + function checkParameterTypeAnnotationsAsExpressions(node) { + // ensure all type annotations with a value declaration are checked as an expression + for (var _i = 0, _a = node.parameters; _i < _a.length; _i++) { + var parameter = _a[_i]; + checkTypeAnnotationAsExpression(parameter); + } + } + /** Check the decorators of a node */ + function checkDecorators(node) { + if (!node.decorators) { + return; + } + // skip this check for nodes that cannot have decorators. These should have already had an error reported by + // checkGrammarDecorators. + if (!ts.nodeCanBeDecorated(node)) { + return; + } + if (!compilerOptions.experimentalDecorators) { + error(node, ts.Diagnostics.Experimental_support_for_decorators_is_a_feature_that_is_subject_to_change_in_a_future_release_Set_the_experimentalDecorators_option_to_remove_this_warning); + } + if (compilerOptions.emitDecoratorMetadata) { + // we only need to perform these checks if we are emitting serialized type metadata for the target of a decorator. + switch (node.kind) { + case 221 /* ClassDeclaration */: + var constructor = ts.getFirstConstructorWithBody(node); + if (constructor) { + checkParameterTypeAnnotationsAsExpressions(constructor); } - checkIndexConstraints(type); + break; + case 147 /* MethodDeclaration */: + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + checkParameterTypeAnnotationsAsExpressions(node); + checkReturnTypeAnnotationAsExpression(node); + break; + case 145 /* PropertyDeclaration */: + case 142 /* Parameter */: + checkTypeAnnotationAsExpression(node); + break; + } + } + ts.forEach(node.decorators, checkDecorator); + } + function checkFunctionDeclaration(node) { + if (produceDiagnostics) { + checkFunctionOrMethodDeclaration(node) || checkGrammarForGenerator(node); + checkCollisionWithCapturedSuperVariable(node, node.name); + checkCollisionWithCapturedThisVariable(node, node.name); + checkCollisionWithRequireExportsInGeneratedCode(node, node.name); + checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); + } + } + function checkFunctionOrMethodDeclaration(node) { + checkDecorators(node); + checkSignatureDeclaration(node); + var isAsync = ts.isAsyncFunctionLike(node); + // Do not use hasDynamicName here, because that returns false for well known symbols. + // We want to perform checkComputedPropertyName for all computed properties, including + // well known symbols. + if (node.name && node.name.kind === 140 /* ComputedPropertyName */) { + // This check will account for methods in class/interface declarations, + // as well as accessors in classes/object literals + checkComputedPropertyName(node.name); + } + if (!ts.hasDynamicName(node)) { + // first we want to check the local symbol that contain this declaration + // - if node.localSymbol !== undefined - this is current declaration is exported and localSymbol points to the local symbol + // - if node.localSymbol === undefined - this node is non-exported so we can just pick the result of getSymbolOfNode + var symbol = getSymbolOfNode(node); + var localSymbol = node.localSymbol || symbol; + // Since the javascript won't do semantic analysis like typescript, + // if the javascript file comes before the typescript file and both contain same name functions, + // checkFunctionOrConstructorSymbol wouldn't be called if we didnt ignore javascript function. + var firstDeclaration = ts.forEach(localSymbol.declarations, + // Get first non javascript function declaration + function (declaration) { return declaration.kind === node.kind && !ts.isSourceFileJavaScript(ts.getSourceFileOfNode(declaration)) ? + declaration : undefined; }); + // Only type check the symbol once + if (node === firstDeclaration) { + checkFunctionOrConstructorSymbol(localSymbol); + } + if (symbol.parent) { + // run check once for the first declaration + if (ts.getDeclarationOfKind(symbol, node.kind) === node) { + // run check on export symbol to check that modifiers agree across all exported declarations + checkFunctionOrConstructorSymbol(symbol); } } - checkObjectTypeForDuplicateDeclarations(node); } - ts.forEach(ts.getInterfaceBaseTypeNodes(node), function (heritageElement) { - if (!ts.isEntityNameExpression(heritageElement.expression)) { - error(heritageElement.expression, ts.Diagnostics.An_interface_can_only_extend_an_identifier_Slashqualified_name_with_optional_type_arguments); + checkSourceElement(node.body); + if (!node.asteriskToken) { + var returnOrPromisedType = node.type && (isAsync ? checkAsyncFunctionReturnType(node) : getTypeFromTypeNode(node.type)); + checkAllCodePathsInNonVoidFunctionReturnOrThrow(node, returnOrPromisedType); + } + if (produceDiagnostics && !node.type) { + // Report an implicit any error if there is no body, no explicit return type, and node is not a private method + // in an ambient context + if (compilerOptions.noImplicitAny && ts.nodeIsMissing(node.body) && !isPrivateWithinAmbient(node)) { + reportImplicitAnyError(node, anyType); + } + if (node.asteriskToken && ts.nodeIsPresent(node.body)) { + // A generator with a body and no type annotation can still cause errors. It can error if the + // yielded values have no common supertype, or it can give an implicit any error if it has no + // yielded values. The only way to trigger these errors is to try checking its return type. + getReturnTypeOfSignature(getSignatureFromDeclaration(node)); } - checkTypeReferenceNode(heritageElement); - }); - ts.forEach(node.members, checkSourceElement); - if (produceDiagnostics) { - checkTypeForDuplicateIndexSignatures(node); - registerForUnusedIdentifiersCheck(node); } + registerForUnusedIdentifiersCheck(node); } - function checkTypeAliasDeclaration(node) { - // Grammar checking - checkGrammarDecorators(node) || checkGrammarModifiers(node); - checkTypeNameIsReserved(node.name, ts.Diagnostics.Type_alias_name_cannot_be_0); - checkSourceElement(node.type); + function registerForUnusedIdentifiersCheck(node) { + if (deferredUnusedIdentifierNodes) { + deferredUnusedIdentifierNodes.push(node); + } } - function computeEnumMemberValues(node) { - var nodeLinks = getNodeLinks(node); - if (!(nodeLinks.flags & 16384 /* EnumValuesComputed */)) { - var enumSymbol = getSymbolOfNode(node); - var enumType = getDeclaredTypeOfSymbol(enumSymbol); - var autoValue = 0; // set to undefined when enum member is non-constant - var ambient = ts.isInAmbientContext(node); - var enumIsConst = ts.isConst(node); - for (var _i = 0, _a = node.members; _i < _a.length; _i++) { - var member = _a[_i]; - if (isComputedNonLiteralName(member.name)) { - error(member.name, ts.Diagnostics.Computed_property_names_are_not_allowed_in_enums); - } - else { - var text = getTextOfPropertyName(member.name); - if (isNumericLiteralName(text) && !isInfinityOrNaNString(text)) { - error(member.name, ts.Diagnostics.An_enum_member_cannot_have_a_numeric_name); - } - } - var previousEnumMemberIsNonConstant = autoValue === undefined; - var initializer = member.initializer; - if (initializer) { - autoValue = computeConstantValueForEnumMemberInitializer(initializer, enumType, enumIsConst, ambient); - } - else if (ambient && !enumIsConst) { - // In ambient enum declarations that specify no const modifier, enum member declarations - // that omit a value are considered computed members (as opposed to having auto-incremented values assigned). - autoValue = undefined; - } - else if (previousEnumMemberIsNonConstant) { - // If the member declaration specifies no value, the member is considered a constant enum member. - // If the member is the first member in the enum declaration, it is assigned the value zero. - // Otherwise, it is assigned the value of the immediately preceding member plus one, - // and an error occurs if the immediately preceding member is not a constant enum member - error(member.name, ts.Diagnostics.Enum_member_must_have_initializer); - } - if (autoValue !== undefined) { - getNodeLinks(member).enumMemberValue = autoValue; - autoValue++; + function checkUnusedIdentifiers() { + if (deferredUnusedIdentifierNodes) { + for (var _i = 0, deferredUnusedIdentifierNodes_1 = deferredUnusedIdentifierNodes; _i < deferredUnusedIdentifierNodes_1.length; _i++) { + var node = deferredUnusedIdentifierNodes_1[_i]; + switch (node.kind) { + case 256 /* SourceFile */: + case 225 /* ModuleDeclaration */: + checkUnusedModuleMembers(node); + break; + case 221 /* ClassDeclaration */: + case 192 /* ClassExpression */: + checkUnusedClassMembers(node); + checkUnusedTypeParameters(node); + break; + case 222 /* InterfaceDeclaration */: + checkUnusedTypeParameters(node); + break; + case 199 /* Block */: + case 227 /* CaseBlock */: + case 206 /* ForStatement */: + case 207 /* ForInStatement */: + case 208 /* ForOfStatement */: + checkUnusedLocalsAndParameters(node); + break; + case 148 /* Constructor */: + case 179 /* FunctionExpression */: + case 220 /* FunctionDeclaration */: + case 180 /* ArrowFunction */: + case 147 /* MethodDeclaration */: + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + if (node.body) { + checkUnusedLocalsAndParameters(node); + } + checkUnusedTypeParameters(node); + break; + case 146 /* MethodSignature */: + case 151 /* CallSignature */: + case 152 /* ConstructSignature */: + case 153 /* IndexSignature */: + case 156 /* FunctionType */: + case 157 /* ConstructorType */: + checkUnusedTypeParameters(node); + break; } + ; } - nodeLinks.flags |= 16384 /* EnumValuesComputed */; } - function computeConstantValueForEnumMemberInitializer(initializer, enumType, enumIsConst, ambient) { - // Controls if error should be reported after evaluation of constant value is completed - // Can be false if another more precise error was already reported during evaluation. - var reportError = true; - var value = evalConstant(initializer); - if (reportError) { - if (value === undefined) { - if (enumIsConst) { - error(initializer, ts.Diagnostics.In_const_enum_declarations_member_initializer_must_be_constant_expression); - } - else if (ambient) { - error(initializer, ts.Diagnostics.In_ambient_enum_declarations_member_initializer_must_be_constant_expression); - } - else { - // Only here do we need to check that the initializer is assignable to the enum type. - checkTypeAssignableTo(checkExpression(initializer), enumType, initializer, /*headMessage*/ undefined); - } - } - else if (enumIsConst) { - if (isNaN(value)) { - error(initializer, ts.Diagnostics.const_enum_member_initializer_was_evaluated_to_disallowed_value_NaN); + } + function checkUnusedLocalsAndParameters(node) { + if (node.parent.kind !== 222 /* InterfaceDeclaration */ && noUnusedIdentifiers && !ts.isInAmbientContext(node)) { + var _loop_1 = function (key) { + var local = node.locals[key]; + if (!local.isReferenced) { + if (local.valueDeclaration && local.valueDeclaration.kind === 142 /* Parameter */) { + var parameter = local.valueDeclaration; + if (compilerOptions.noUnusedParameters && + !ts.isParameterPropertyDeclaration(parameter) && + !ts.parameterIsThisKeyword(parameter) && + !parameterNameStartsWithUnderscore(parameter)) { + error(local.valueDeclaration.name, ts.Diagnostics._0_is_declared_but_never_used, local.name); + } } - else if (!isFinite(value)) { - error(initializer, ts.Diagnostics.const_enum_member_initializer_was_evaluated_to_a_non_finite_value); + else if (compilerOptions.noUnusedLocals) { + ts.forEach(local.declarations, function (d) { return error(d.name || d, ts.Diagnostics._0_is_declared_but_never_used, local.name); }); } } + }; + for (var key in node.locals) { + _loop_1(key); } - return value; - function evalConstant(e) { - switch (e.kind) { - case 185 /* PrefixUnaryExpression */: - var value_1 = evalConstant(e.operand); - if (value_1 === undefined) { - return undefined; - } - switch (e.operator) { - case 35 /* PlusToken */: return value_1; - case 36 /* MinusToken */: return -value_1; - case 50 /* TildeToken */: return ~value_1; - } - return undefined; - case 187 /* BinaryExpression */: - var left = evalConstant(e.left); - if (left === undefined) { - return undefined; - } - var right = evalConstant(e.right); - if (right === undefined) { - return undefined; - } - switch (e.operatorToken.kind) { - case 47 /* BarToken */: return left | right; - case 46 /* AmpersandToken */: return left & right; - case 44 /* GreaterThanGreaterThanToken */: return left >> right; - case 45 /* GreaterThanGreaterThanGreaterThanToken */: return left >>> right; - case 43 /* LessThanLessThanToken */: return left << right; - case 48 /* CaretToken */: return left ^ right; - case 37 /* AsteriskToken */: return left * right; - case 39 /* SlashToken */: return left / right; - case 35 /* PlusToken */: return left + right; - case 36 /* MinusToken */: return left - right; - case 40 /* PercentToken */: return left % right; - } - return undefined; - case 8 /* NumericLiteral */: - return +e.text; - case 178 /* ParenthesizedExpression */: - return evalConstant(e.expression); - case 69 /* Identifier */: - case 173 /* ElementAccessExpression */: - case 172 /* PropertyAccessExpression */: - var member = initializer.parent; - var currentType = getTypeOfSymbol(getSymbolOfNode(member.parent)); - var enumType_1; - var propertyName = void 0; - if (e.kind === 69 /* Identifier */) { - // unqualified names can refer to member that reside in different declaration of the enum so just doing name resolution won't work. - // instead pick current enum type and later try to fetch member from the type - enumType_1 = currentType; - propertyName = e.text; + } + } + function parameterNameStartsWithUnderscore(parameter) { + return parameter.name && parameter.name.kind === 69 /* Identifier */ && parameter.name.text.charCodeAt(0) === 95 /* _ */; + } + function checkUnusedClassMembers(node) { + if (compilerOptions.noUnusedLocals && !ts.isInAmbientContext(node)) { + if (node.members) { + for (var _i = 0, _a = node.members; _i < _a.length; _i++) { + var member = _a[_i]; + if (member.kind === 147 /* MethodDeclaration */ || member.kind === 145 /* PropertyDeclaration */) { + if (!member.symbol.isReferenced && ts.getModifierFlags(member) & 8 /* Private */) { + error(member.name, ts.Diagnostics._0_is_declared_but_never_used, member.symbol.name); } - else { - var expression = void 0; - if (e.kind === 173 /* ElementAccessExpression */) { - if (e.argumentExpression === undefined || - e.argumentExpression.kind !== 9 /* StringLiteral */) { - return undefined; - } - expression = e.expression; - propertyName = e.argumentExpression.text; - } - else { - expression = e.expression; - propertyName = e.name.text; - } - // expression part in ElementAccess\PropertyAccess should be either identifier or dottedName - var current = expression; - while (current) { - if (current.kind === 69 /* Identifier */) { - break; - } - else if (current.kind === 172 /* PropertyAccessExpression */) { - current = current.expression; - } - else { - return undefined; - } - } - enumType_1 = checkExpression(expression); - // allow references to constant members of other enums - if (!(enumType_1.symbol && (enumType_1.symbol.flags & 384 /* Enum */))) { - return undefined; + } + else if (member.kind === 148 /* Constructor */) { + for (var _b = 0, _c = member.parameters; _b < _c.length; _b++) { + var parameter = _c[_b]; + if (!parameter.symbol.isReferenced && ts.getModifierFlags(parameter) & 8 /* Private */) { + error(parameter.name, ts.Diagnostics.Property_0_is_declared_but_never_used, parameter.symbol.name); } } - if (propertyName === undefined) { - return undefined; - } - var property = getPropertyOfObjectType(enumType_1, propertyName); - if (!property || !(property.flags & 8 /* EnumMember */)) { - return undefined; - } - var propertyDecl = property.valueDeclaration; - // self references are illegal - if (member === propertyDecl) { - return undefined; - } - // illegal case: forward reference - if (!isBlockScopedNameDeclaredBeforeUse(propertyDecl, member)) { - reportError = false; - error(e, ts.Diagnostics.A_member_initializer_in_a_enum_declaration_cannot_reference_members_declared_after_it_including_members_defined_in_other_enums); - return undefined; - } - return getNodeLinks(propertyDecl).enumMemberValue; + } } } } } - function checkEnumDeclaration(node) { - if (!produceDiagnostics) { - return; - } - // Grammar checking - checkGrammarDecorators(node) || checkGrammarModifiers(node); - checkTypeNameIsReserved(node.name, ts.Diagnostics.Enum_name_cannot_be_0); - checkCollisionWithCapturedThisVariable(node, node.name); - checkCollisionWithRequireExportsInGeneratedCode(node, node.name); - checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); - checkExportsOnMergedDeclarations(node); - computeEnumMemberValues(node); - var enumIsConst = ts.isConst(node); - if (compilerOptions.isolatedModules && enumIsConst && ts.isInAmbientContext(node)) { - error(node.name, ts.Diagnostics.Ambient_const_enums_are_not_allowed_when_the_isolatedModules_flag_is_provided); - } - // Spec 2014 - Section 9.3: - // It isn't possible for one enum declaration to continue the automatic numbering sequence of another, - // and when an enum type has multiple declarations, only one declaration is permitted to omit a value - // for the first member. - // - // Only perform this check once per symbol - var enumSymbol = getSymbolOfNode(node); - var firstDeclaration = ts.getDeclarationOfKind(enumSymbol, node.kind); - if (node === firstDeclaration) { - if (enumSymbol.declarations.length > 1) { - // check that const is placed\omitted on all enum declarations - ts.forEach(enumSymbol.declarations, function (decl) { - if (ts.isConstEnumDeclaration(decl) !== enumIsConst) { - error(decl.name, ts.Diagnostics.Enum_declarations_must_all_be_const_or_non_const); - } - }); - } - var seenEnumMissingInitialInitializer_1 = false; - ts.forEach(enumSymbol.declarations, function (declaration) { - // return true if we hit a violation of the rule, false otherwise - if (declaration.kind !== 224 /* EnumDeclaration */) { - return false; - } - var enumDeclaration = declaration; - if (!enumDeclaration.members.length) { - return false; + function checkUnusedTypeParameters(node) { + if (compilerOptions.noUnusedLocals && !ts.isInAmbientContext(node)) { + if (node.typeParameters) { + // Only report errors on the last declaration for the type parameter container; + // this ensures that all uses have been accounted for. + var symbol = getSymbolOfNode(node); + var lastDeclaration = symbol && symbol.declarations && ts.lastOrUndefined(symbol.declarations); + if (lastDeclaration !== node) { + return; } - var firstEnumMember = enumDeclaration.members[0]; - if (!firstEnumMember.initializer) { - if (seenEnumMissingInitialInitializer_1) { - error(firstEnumMember.name, ts.Diagnostics.In_an_enum_with_multiple_declarations_only_one_declaration_can_omit_an_initializer_for_its_first_enum_element); - } - else { - seenEnumMissingInitialInitializer_1 = true; + for (var _i = 0, _a = node.typeParameters; _i < _a.length; _i++) { + var typeParameter = _a[_i]; + if (!getMergedSymbol(typeParameter.symbol).isReferenced) { + error(typeParameter.name, ts.Diagnostics._0_is_declared_but_never_used, typeParameter.symbol.name); } } - }); + } } } - function getFirstNonAmbientClassOrFunctionDeclaration(symbol) { - var declarations = symbol.declarations; - for (var _i = 0, declarations_5 = declarations; _i < declarations_5.length; _i++) { - var declaration = declarations_5[_i]; - if ((declaration.kind === 221 /* ClassDeclaration */ || - (declaration.kind === 220 /* FunctionDeclaration */ && ts.nodeIsPresent(declaration.body))) && - !ts.isInAmbientContext(declaration)) { - return declaration; + function checkUnusedModuleMembers(node) { + if (compilerOptions.noUnusedLocals && !ts.isInAmbientContext(node)) { + for (var key in node.locals) { + var local = node.locals[key]; + if (!local.isReferenced && !local.exportSymbol) { + for (var _i = 0, _a = local.declarations; _i < _a.length; _i++) { + var declaration = _a[_i]; + if (!ts.isAmbientModule(declaration)) { + error(declaration.name, ts.Diagnostics._0_is_declared_but_never_used, local.name); + } + } + } } } - return undefined; } - function inSameLexicalScope(node1, node2) { - var container1 = ts.getEnclosingBlockScopeContainer(node1); - var container2 = ts.getEnclosingBlockScopeContainer(node2); - if (isGlobalSourceFile(container1)) { - return isGlobalSourceFile(container2); - } - else if (isGlobalSourceFile(container2)) { - return false; + function checkBlock(node) { + // Grammar checking for SyntaxKind.Block + if (node.kind === 199 /* Block */) { + checkGrammarStatementInAmbientContext(node); } - else { - return container1 === container2; + ts.forEach(node.statements, checkSourceElement); + if (node.locals) { + registerForUnusedIdentifiersCheck(node); } } - function checkModuleDeclaration(node) { - if (produceDiagnostics) { - // Grammar checking - var isGlobalAugmentation = ts.isGlobalScopeAugmentation(node); - var inAmbientContext = ts.isInAmbientContext(node); - if (isGlobalAugmentation && !inAmbientContext) { - error(node.name, ts.Diagnostics.Augmentations_for_the_global_scope_should_have_declare_modifier_unless_they_appear_in_already_ambient_context); + function checkCollisionWithArgumentsInGeneratedCode(node) { + // no rest parameters \ declaration context \ overload - no codegen impact + if (!ts.hasDeclaredRestParameter(node) || ts.isInAmbientContext(node) || ts.nodeIsMissing(node.body)) { + return; + } + ts.forEach(node.parameters, function (p) { + if (p.name && !ts.isBindingPattern(p.name) && p.name.text === argumentsSymbol.name) { + error(p, ts.Diagnostics.Duplicate_identifier_arguments_Compiler_uses_arguments_to_initialize_rest_parameters); } - var isAmbientExternalModule = ts.isAmbientModule(node); - var contextErrorMessage = isAmbientExternalModule - ? ts.Diagnostics.An_ambient_module_declaration_is_only_allowed_at_the_top_level_in_a_file - : ts.Diagnostics.A_namespace_declaration_is_only_allowed_in_a_namespace_or_module; - if (checkGrammarModuleElementContext(node, contextErrorMessage)) { - // If we hit a module declaration in an illegal context, just bail out to avoid cascading errors. - return; - } - if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node)) { - if (!inAmbientContext && node.name.kind === 9 /* StringLiteral */) { - grammarErrorOnNode(node.name, ts.Diagnostics.Only_ambient_modules_can_use_quoted_names); - } - } - checkCollisionWithCapturedThisVariable(node, node.name); - checkCollisionWithRequireExportsInGeneratedCode(node, node.name); - checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); - checkExportsOnMergedDeclarations(node); - var symbol = getSymbolOfNode(node); - // The following checks only apply on a non-ambient instantiated module declaration. - if (symbol.flags & 512 /* ValueModule */ - && symbol.declarations.length > 1 - && !inAmbientContext - && ts.isInstantiatedModule(node, compilerOptions.preserveConstEnums || compilerOptions.isolatedModules)) { - var firstNonAmbientClassOrFunc = getFirstNonAmbientClassOrFunctionDeclaration(symbol); - if (firstNonAmbientClassOrFunc) { - if (ts.getSourceFileOfNode(node) !== ts.getSourceFileOfNode(firstNonAmbientClassOrFunc)) { - error(node.name, ts.Diagnostics.A_namespace_declaration_cannot_be_in_a_different_file_from_a_class_or_function_with_which_it_is_merged); - } - else if (node.pos < firstNonAmbientClassOrFunc.pos) { - error(node.name, ts.Diagnostics.A_namespace_declaration_cannot_be_located_prior_to_a_class_or_function_with_which_it_is_merged); - } - } - // if the module merges with a class declaration in the same lexical scope, - // we need to track this to ensure the correct emit. - var mergedClass = ts.getDeclarationOfKind(symbol, 221 /* ClassDeclaration */); - if (mergedClass && - inSameLexicalScope(node, mergedClass)) { - getNodeLinks(node).flags |= 32768 /* LexicalModuleMergesWithClass */; - } - } - if (isAmbientExternalModule) { - if (ts.isExternalModuleAugmentation(node)) { - // body of the augmentation should be checked for consistency only if augmentation was applied to its target (either global scope or module) - // otherwise we'll be swamped in cascading errors. - // We can detect if augmentation was applied using following rules: - // - augmentation for a global scope is always applied - // - augmentation for some external module is applied if symbol for augmentation is merged (it was combined with target module). - var checkBody = isGlobalAugmentation || (getSymbolOfNode(node).flags & 33554432 /* Merged */); - if (checkBody && node.body) { - // body of ambient external module is always a module block - for (var _i = 0, _a = node.body.statements; _i < _a.length; _i++) { - var statement = _a[_i]; - checkModuleAugmentationElement(statement, isGlobalAugmentation); - } - } - } - else if (isGlobalSourceFile(node.parent)) { - if (isGlobalAugmentation) { - error(node.name, ts.Diagnostics.Augmentations_for_the_global_scope_can_only_be_directly_nested_in_external_modules_or_ambient_module_declarations); - } - else if (ts.isExternalModuleNameRelative(node.name.text)) { - error(node.name, ts.Diagnostics.Ambient_module_declaration_cannot_specify_relative_module_name); - } - } - else { - if (isGlobalAugmentation) { - error(node.name, ts.Diagnostics.Augmentations_for_the_global_scope_can_only_be_directly_nested_in_external_modules_or_ambient_module_declarations); - } - else { - // Node is not an augmentation and is not located on the script level. - // This means that this is declaration of ambient module that is located in other module or namespace which is prohibited. - error(node.name, ts.Diagnostics.Ambient_modules_cannot_be_nested_in_other_modules_or_namespaces); - } - } - } - } - if (node.body) { - checkSourceElement(node.body); - if (!ts.isGlobalScopeAugmentation(node)) { - registerForUnusedIdentifiersCheck(node); - } - } - } - function checkModuleAugmentationElement(node, isGlobalAugmentation) { - switch (node.kind) { - case 200 /* VariableStatement */: - // error each individual name in variable statement instead of marking the entire variable statement - for (var _i = 0, _a = node.declarationList.declarations; _i < _a.length; _i++) { - var decl = _a[_i]; - checkModuleAugmentationElement(decl, isGlobalAugmentation); - } - break; - case 235 /* ExportAssignment */: - case 236 /* ExportDeclaration */: - grammarErrorOnFirstToken(node, ts.Diagnostics.Exports_and_export_assignments_are_not_permitted_in_module_augmentations); - break; - case 229 /* ImportEqualsDeclaration */: - case 230 /* ImportDeclaration */: - grammarErrorOnFirstToken(node, ts.Diagnostics.Imports_are_not_permitted_in_module_augmentations_Consider_moving_them_to_the_enclosing_external_module); - break; - case 169 /* BindingElement */: - case 218 /* VariableDeclaration */: - var name_22 = node.name; - if (ts.isBindingPattern(name_22)) { - for (var _b = 0, _c = name_22.elements; _b < _c.length; _b++) { - var el = _c[_b]; - // mark individual names in binding pattern - checkModuleAugmentationElement(el, isGlobalAugmentation); - } - break; - } - // fallthrough - case 221 /* ClassDeclaration */: - case 224 /* EnumDeclaration */: - case 220 /* FunctionDeclaration */: - case 222 /* InterfaceDeclaration */: - case 225 /* ModuleDeclaration */: - case 223 /* TypeAliasDeclaration */: - if (isGlobalAugmentation) { - return; - } - var symbol = getSymbolOfNode(node); - if (symbol) { - // module augmentations cannot introduce new names on the top level scope of the module - // this is done it two steps - // 1. quick check - if symbol for node is not merged - this is local symbol to this augmentation - report error - // 2. main check - report error if value declaration of the parent symbol is module augmentation) - var reportError = !(symbol.flags & 33554432 /* Merged */); - if (!reportError) { - // symbol should not originate in augmentation - reportError = ts.isExternalModuleAugmentation(symbol.parent.declarations[0]); - } - } - break; - } + }); } - function getFirstIdentifier(node) { - switch (node.kind) { - case 69 /* Identifier */: - return node; - case 139 /* QualifiedName */: - do { - node = node.left; - } while (node.kind !== 69 /* Identifier */); - return node; - case 172 /* PropertyAccessExpression */: - do { - node = node.expression; - } while (node.kind !== 69 /* Identifier */); - return node; + function needCollisionCheckForIdentifier(node, identifier, name) { + if (!(identifier && identifier.text === name)) { + return false; } - } - function checkExternalImportOrExportDeclaration(node) { - var moduleName = ts.getExternalModuleName(node); - if (!ts.nodeIsMissing(moduleName) && moduleName.kind !== 9 /* StringLiteral */) { - error(moduleName, ts.Diagnostics.String_literal_expected); + if (node.kind === 145 /* PropertyDeclaration */ || + node.kind === 144 /* PropertySignature */ || + node.kind === 147 /* MethodDeclaration */ || + node.kind === 146 /* MethodSignature */ || + node.kind === 149 /* GetAccessor */ || + node.kind === 150 /* SetAccessor */) { + // it is ok to have member named '_super' or '_this' - member access is always qualified return false; } - var inAmbientExternalModule = node.parent.kind === 226 /* ModuleBlock */ && ts.isAmbientModule(node.parent.parent); - if (node.parent.kind !== 256 /* SourceFile */ && !inAmbientExternalModule) { - error(moduleName, node.kind === 236 /* ExportDeclaration */ ? - ts.Diagnostics.Export_declarations_are_not_permitted_in_a_namespace : - ts.Diagnostics.Import_declarations_in_a_namespace_cannot_reference_a_module); + if (ts.isInAmbientContext(node)) { + // ambient context - no codegen impact return false; } - if (inAmbientExternalModule && ts.isExternalModuleNameRelative(moduleName.text)) { - // we have already reported errors on top level imports\exports in external module augmentations in checkModuleDeclaration - // no need to do this again. - if (!isTopLevelInExternalModuleAugmentation(node)) { - // TypeScript 1.0 spec (April 2013): 12.1.6 - // An ExternalImportDeclaration in an AmbientExternalModuleDeclaration may reference - // other external modules only through top - level external module names. - // Relative external module names are not permitted. - error(node, ts.Diagnostics.Import_or_export_declaration_in_an_ambient_module_declaration_cannot_reference_module_through_relative_module_name); - return false; - } + var root = ts.getRootDeclaration(node); + if (root.kind === 142 /* Parameter */ && ts.nodeIsMissing(root.parent.body)) { + // just an overload - no codegen impact + return false; } return true; } - function checkAliasSymbol(node) { - var symbol = getSymbolOfNode(node); - var target = resolveAlias(symbol); - if (target !== unknownSymbol) { - // For external modules symbol represent local symbol for an alias. - // This local symbol will merge any other local declarations (excluding other aliases) - // and symbol.flags will contains combined representation for all merged declaration. - // Based on symbol.flags we can compute a set of excluded meanings (meaning that resolved alias should not have, - // otherwise it will conflict with some local declaration). Note that in addition to normal flags we include matching SymbolFlags.Export* - // in order to prevent collisions with declarations that were exported from the current module (they still contribute to local names). - var excludedMeanings = (symbol.flags & (107455 /* Value */ | 1048576 /* ExportValue */) ? 107455 /* Value */ : 0) | - (symbol.flags & 793064 /* Type */ ? 793064 /* Type */ : 0) | - (symbol.flags & 1920 /* Namespace */ ? 1920 /* Namespace */ : 0); - if (target.flags & excludedMeanings) { - var message = node.kind === 238 /* ExportSpecifier */ ? - ts.Diagnostics.Export_declaration_conflicts_with_exported_declaration_of_0 : - ts.Diagnostics.Import_declaration_conflicts_with_local_declaration_of_0; - error(node, message, symbolToString(symbol)); - } + function checkCollisionWithCapturedThisVariable(node, name) { + if (needCollisionCheckForIdentifier(node, name, "_this")) { + potentialThisCollisions.push(node); } } - function checkImportBinding(node) { - checkCollisionWithCapturedThisVariable(node, node.name); - checkCollisionWithRequireExportsInGeneratedCode(node, node.name); - checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); - checkAliasSymbol(node); - } - function checkImportDeclaration(node) { - if (checkGrammarModuleElementContext(node, ts.Diagnostics.An_import_declaration_can_only_be_used_in_a_namespace_or_module)) { - // If we hit an import declaration in an illegal context, just bail out to avoid cascading errors. - return; - } - if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node) && ts.getModifierFlags(node) !== 0) { - grammarErrorOnFirstToken(node, ts.Diagnostics.An_import_declaration_cannot_have_modifiers); - } - if (checkExternalImportOrExportDeclaration(node)) { - var importClause = node.importClause; - if (importClause) { - if (importClause.name) { - checkImportBinding(importClause); + // this function will run after checking the source file so 'CaptureThis' is correct for all nodes + function checkIfThisIsCapturedInEnclosingScope(node) { + var current = node; + while (current) { + if (getNodeCheckFlags(current) & 4 /* CaptureThis */) { + var isDeclaration_1 = node.kind !== 69 /* Identifier */; + if (isDeclaration_1) { + error(node.name, ts.Diagnostics.Duplicate_identifier_this_Compiler_uses_variable_declaration_this_to_capture_this_reference); } - if (importClause.namedBindings) { - if (importClause.namedBindings.kind === 232 /* NamespaceImport */) { - checkImportBinding(importClause.namedBindings); - } - else { - ts.forEach(importClause.namedBindings.elements, checkImportBinding); - } + else { + error(node, ts.Diagnostics.Expression_resolves_to_variable_declaration_this_that_compiler_uses_to_capture_this_reference); } + return; } + current = current.parent; } } - function checkImportEqualsDeclaration(node) { - if (checkGrammarModuleElementContext(node, ts.Diagnostics.An_import_declaration_can_only_be_used_in_a_namespace_or_module)) { - // If we hit an import declaration in an illegal context, just bail out to avoid cascading errors. + function checkCollisionWithCapturedSuperVariable(node, name) { + if (!needCollisionCheckForIdentifier(node, name, "_super")) { return; } - checkGrammarDecorators(node) || checkGrammarModifiers(node); - if (ts.isInternalModuleImportEqualsDeclaration(node) || checkExternalImportOrExportDeclaration(node)) { - checkImportBinding(node); - if (ts.getModifierFlags(node) & 1 /* Export */) { - markExportAsReferenced(node); - } - if (ts.isInternalModuleImportEqualsDeclaration(node)) { - var target = resolveAlias(getSymbolOfNode(node)); - if (target !== unknownSymbol) { - if (target.flags & 107455 /* Value */) { - // Target is a value symbol, check that it is not hidden by a local declaration with the same name - var moduleName = getFirstIdentifier(node.moduleReference); - if (!(resolveEntityName(moduleName, 107455 /* Value */ | 1920 /* Namespace */).flags & 1920 /* Namespace */)) { - error(moduleName, ts.Diagnostics.Module_0_is_hidden_by_a_local_declaration_with_the_same_name, ts.declarationNameToString(moduleName)); - } - } - if (target.flags & 793064 /* Type */) { - checkTypeNameIsReserved(node.name, ts.Diagnostics.Import_name_cannot_be_0); - } - } + // bubble up and find containing type + var enclosingClass = ts.getContainingClass(node); + // if containing type was not found or it is ambient - exit (no codegen) + if (!enclosingClass || ts.isInAmbientContext(enclosingClass)) { + return; + } + if (ts.getClassExtendsHeritageClauseElement(enclosingClass)) { + var isDeclaration_2 = node.kind !== 69 /* Identifier */; + if (isDeclaration_2) { + error(node, ts.Diagnostics.Duplicate_identifier_super_Compiler_uses_super_to_capture_base_class_reference); } else { - if (modulekind === ts.ModuleKind.ES6 && !ts.isInAmbientContext(node)) { - // Import equals declaration is deprecated in es6 or above - grammarErrorOnNode(node, ts.Diagnostics.Import_assignment_cannot_be_used_when_targeting_ECMAScript_2015_modules_Consider_using_import_Asterisk_as_ns_from_mod_import_a_from_mod_import_d_from_mod_or_another_module_format_instead); - } + error(node, ts.Diagnostics.Expression_resolves_to_super_that_compiler_uses_to_capture_base_class_reference); } } } - function checkExportDeclaration(node) { - if (checkGrammarModuleElementContext(node, ts.Diagnostics.An_export_declaration_can_only_be_used_in_a_module)) { - // If we hit an export in an illegal context, just bail out to avoid cascading errors. + function checkCollisionWithRequireExportsInGeneratedCode(node, name) { + // No need to check for require or exports for ES6 modules and later + if (modulekind >= ts.ModuleKind.ES6) { return; } - if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node) && ts.getModifierFlags(node) !== 0) { - grammarErrorOnFirstToken(node, ts.Diagnostics.An_export_declaration_cannot_have_modifiers); - } - if (!node.moduleSpecifier || checkExternalImportOrExportDeclaration(node)) { - if (node.exportClause) { - // export { x, y } - // export { x, y } from "foo" - ts.forEach(node.exportClause.elements, checkExportSpecifier); - var inAmbientExternalModule = node.parent.kind === 226 /* ModuleBlock */ && ts.isAmbientModule(node.parent.parent); - if (node.parent.kind !== 256 /* SourceFile */ && !inAmbientExternalModule) { - error(node, ts.Diagnostics.Export_declarations_are_not_permitted_in_a_namespace); - } - } - else { - // export * from "foo" - var moduleSymbol = resolveExternalModuleName(node, node.moduleSpecifier); - if (moduleSymbol && hasExportAssignmentSymbol(moduleSymbol)) { - error(node.moduleSpecifier, ts.Diagnostics.Module_0_uses_export_and_cannot_be_used_with_export_Asterisk, symbolToString(moduleSymbol)); - } - } + if (!needCollisionCheckForIdentifier(node, name, "require") && !needCollisionCheckForIdentifier(node, name, "exports")) { + return; } - } - function checkGrammarModuleElementContext(node, errorMessage) { - if (node.parent.kind !== 256 /* SourceFile */ && node.parent.kind !== 226 /* ModuleBlock */ && node.parent.kind !== 225 /* ModuleDeclaration */) { - return grammarErrorOnFirstToken(node, errorMessage); + // Uninstantiated modules shouldnt do this check + if (node.kind === 225 /* ModuleDeclaration */ && ts.getModuleInstanceState(node) !== 1 /* Instantiated */) { + return; } - } - function checkExportSpecifier(node) { - checkAliasSymbol(node); - if (!node.parent.parent.moduleSpecifier) { - var exportedName = node.propertyName || node.name; - // find immediate value referenced by exported name (SymbolFlags.Alias is set so we don't chase down aliases) - var symbol = resolveName(exportedName, exportedName.text, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */ | 8388608 /* Alias */, - /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined); - if (symbol && (symbol === undefinedSymbol || isGlobalSourceFile(getDeclarationContainer(symbol.declarations[0])))) { - error(exportedName, ts.Diagnostics.Cannot_export_0_Only_local_declarations_can_be_exported_from_a_module, exportedName.text); - } - else { - markExportAsReferenced(node); - } + // In case of variable declaration, node.parent is variable statement so look at the variable statement's parent + var parent = getDeclarationContainer(node); + if (parent.kind === 256 /* SourceFile */ && ts.isExternalOrCommonJsModule(parent)) { + // If the declaration happens to be in external module, report error that require and exports are reserved keywords + error(name, ts.Diagnostics.Duplicate_identifier_0_Compiler_reserves_name_1_in_top_level_scope_of_a_module, ts.declarationNameToString(name), ts.declarationNameToString(name)); } } - function checkExportAssignment(node) { - if (checkGrammarModuleElementContext(node, ts.Diagnostics.An_export_assignment_can_only_be_used_in_a_module)) { - // If we hit an export assignment in an illegal context, just bail out to avoid cascading errors. + function checkCollisionWithGlobalPromiseInGeneratedCode(node, name) { + if (!needCollisionCheckForIdentifier(node, name, "Promise")) { return; } - var container = node.parent.kind === 256 /* SourceFile */ ? node.parent : node.parent.parent; - if (container.kind === 225 /* ModuleDeclaration */ && !ts.isAmbientModule(container)) { - error(node, ts.Diagnostics.An_export_assignment_cannot_be_used_in_a_namespace); + // Uninstantiated modules shouldnt do this check + if (node.kind === 225 /* ModuleDeclaration */ && ts.getModuleInstanceState(node) !== 1 /* Instantiated */) { return; } - // Grammar checking - if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node) && ts.getModifierFlags(node) !== 0) { - grammarErrorOnFirstToken(node, ts.Diagnostics.An_export_assignment_cannot_have_modifiers); + // In case of variable declaration, node.parent is variable statement so look at the variable statement's parent + var parent = getDeclarationContainer(node); + if (parent.kind === 256 /* SourceFile */ && ts.isExternalOrCommonJsModule(parent) && parent.flags & 8192 /* HasAsyncFunctions */) { + // If the declaration happens to be in external module, report error that Promise is a reserved identifier. + error(name, ts.Diagnostics.Duplicate_identifier_0_Compiler_reserves_name_1_in_top_level_scope_of_a_module_containing_async_functions, ts.declarationNameToString(name), ts.declarationNameToString(name)); } - if (node.expression.kind === 69 /* Identifier */) { - markExportAsReferenced(node); + } + function checkVarDeclaredNamesNotShadowed(node) { + // - ScriptBody : StatementList + // It is a Syntax Error if any element of the LexicallyDeclaredNames of StatementList + // also occurs in the VarDeclaredNames of StatementList. + // - Block : { StatementList } + // It is a Syntax Error if any element of the LexicallyDeclaredNames of StatementList + // also occurs in the VarDeclaredNames of StatementList. + // Variable declarations are hoisted to the top of their function scope. They can shadow + // block scoped declarations, which bind tighter. this will not be flagged as duplicate definition + // by the binder as the declaration scope is different. + // A non-initialized declaration is a no-op as the block declaration will resolve before the var + // declaration. the problem is if the declaration has an initializer. this will act as a write to the + // block declared value. this is fine for let, but not const. + // Only consider declarations with initializers, uninitialized const declarations will not + // step on a let/const variable. + // Do not consider const and const declarations, as duplicate block-scoped declarations + // are handled by the binder. + // We are only looking for const declarations that step on let\const declarations from a + // different scope. e.g.: + // { + // const x = 0; // localDeclarationSymbol obtained after name resolution will correspond to this declaration + // const x = 0; // symbol for this declaration will be 'symbol' + // } + // skip block-scoped variables and parameters + if ((ts.getCombinedNodeFlags(node) & 3 /* BlockScoped */) !== 0 || ts.isParameterDeclaration(node)) { + return; } - else { - checkExpressionCached(node.expression); + // skip variable declarations that don't have initializers + // NOTE: in ES6 spec initializer is required in variable declarations where name is binding pattern + // so we'll always treat binding elements as initialized + if (node.kind === 218 /* VariableDeclaration */ && !node.initializer) { + return; } - checkExternalModuleExports(container); - if (node.isExportEquals && !ts.isInAmbientContext(node)) { - if (modulekind === ts.ModuleKind.ES6) { - // export assignment is not supported in es6 modules - grammarErrorOnNode(node, ts.Diagnostics.Export_assignment_cannot_be_used_when_targeting_ECMAScript_2015_modules_Consider_using_export_default_or_another_module_format_instead); - } - else if (modulekind === ts.ModuleKind.System) { - // system modules does not support export assignment - grammarErrorOnNode(node, ts.Diagnostics.Export_assignment_is_not_supported_when_module_flag_is_system); + var symbol = getSymbolOfNode(node); + if (symbol.flags & 1 /* FunctionScopedVariable */) { + var localDeclarationSymbol = resolveName(node, node.name.text, 3 /* Variable */, /*nodeNotFoundErrorMessage*/ undefined, /*nameArg*/ undefined); + if (localDeclarationSymbol && + localDeclarationSymbol !== symbol && + localDeclarationSymbol.flags & 2 /* BlockScopedVariable */) { + if (getDeclarationNodeFlagsFromSymbol(localDeclarationSymbol) & 3 /* BlockScoped */) { + var varDeclList = ts.getAncestor(localDeclarationSymbol.valueDeclaration, 219 /* VariableDeclarationList */); + var container = varDeclList.parent.kind === 200 /* VariableStatement */ && varDeclList.parent.parent + ? varDeclList.parent.parent + : undefined; + // names of block-scoped and function scoped variables can collide only + // if block scoped variable is defined in the function\module\source file scope (because of variable hoisting) + var namesShareScope = container && + (container.kind === 199 /* Block */ && ts.isFunctionLike(container.parent) || + container.kind === 226 /* ModuleBlock */ || + container.kind === 225 /* ModuleDeclaration */ || + container.kind === 256 /* SourceFile */); + // here we know that function scoped variable is shadowed by block scoped one + // if they are defined in the same scope - binder has already reported redeclaration error + // otherwise if variable has an initializer - show error that initialization will fail + // since LHS will be block scoped name instead of function scoped + if (!namesShareScope) { + var name_20 = symbolToString(localDeclarationSymbol); + error(node, ts.Diagnostics.Cannot_initialize_outer_scoped_variable_0_in_the_same_scope_as_block_scoped_declaration_1, name_20, name_20); + } + } } } } - function hasExportedMembers(moduleSymbol) { - for (var id in moduleSymbol.exports) { - if (id !== "export=") { - return true; - } + // Check that a parameter initializer contains no references to parameters declared to the right of itself + function checkParameterInitializer(node) { + if (ts.getRootDeclaration(node).kind !== 142 /* Parameter */) { + return; } - return false; - } - function checkExternalModuleExports(node) { - var moduleSymbol = getSymbolOfNode(node); - var links = getSymbolLinks(moduleSymbol); - if (!links.exportsChecked) { - var exportEqualsSymbol = moduleSymbol.exports["export="]; - if (exportEqualsSymbol && hasExportedMembers(moduleSymbol)) { - var declaration = getDeclarationOfAliasSymbol(exportEqualsSymbol) || exportEqualsSymbol.valueDeclaration; - if (!isTopLevelInExternalModuleAugmentation(declaration)) { - error(declaration, ts.Diagnostics.An_export_assignment_cannot_be_used_in_a_module_with_other_exported_elements); - } + var func = ts.getContainingFunction(node); + visit(node.initializer); + function visit(n) { + if (ts.isTypeNode(n) || ts.isDeclarationName(n)) { + // do not dive in types + // skip declaration names (i.e. in object literal expressions) + return; } - // Checks for export * conflicts - var exports = getExportsOfModule(moduleSymbol); - for (var id in exports) { - if (id === "__export") { - continue; - } - var _a = exports[id], declarations = _a.declarations, flags = _a.flags; - // ECMA262: 15.2.1.1 It is a Syntax Error if the ExportedNames of ModuleItemList contains any duplicate entries. - // (TS Exceptions: namespaces, function overloads, enums, and interfaces) - if (flags & (1920 /* Namespace */ | 64 /* Interface */ | 384 /* Enum */)) { - continue; + if (n.kind === 172 /* PropertyAccessExpression */) { + // skip property names in property access expression + return visit(n.expression); + } + else if (n.kind === 69 /* Identifier */) { + // check FunctionLikeDeclaration.locals (stores parameters\function local variable) + // if it contains entry with a specified name + var symbol = resolveName(n, n.text, 107455 /* Value */ | 8388608 /* Alias */, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined); + if (!symbol || symbol === unknownSymbol || !symbol.valueDeclaration) { + return; } - var exportedDeclarationsCount = ts.countWhere(declarations, isNotOverload); - if (flags & 524288 /* TypeAlias */ && exportedDeclarationsCount <= 2) { - // it is legal to merge type alias with other values - // so count should be either 1 (just type alias) or 2 (type alias + merged value) - continue; + if (symbol.valueDeclaration === node) { + error(n, ts.Diagnostics.Parameter_0_cannot_be_referenced_in_its_initializer, ts.declarationNameToString(node.name)); + return; } - if (exportedDeclarationsCount > 1) { - for (var _i = 0, declarations_6 = declarations; _i < declarations_6.length; _i++) { - var declaration = declarations_6[_i]; - if (isNotOverload(declaration)) { - diagnostics.add(ts.createDiagnosticForNode(declaration, ts.Diagnostics.Cannot_redeclare_exported_variable_0, id)); + // locals map for function contain both parameters and function locals + // so we need to do a bit of extra work to check if reference is legal + var enclosingContainer = ts.getEnclosingBlockScopeContainer(symbol.valueDeclaration); + if (enclosingContainer === func) { + if (symbol.valueDeclaration.kind === 142 /* Parameter */) { + // it is ok to reference parameter in initializer if either + // - parameter is located strictly on the left of current parameter declaration + if (symbol.valueDeclaration.pos < node.pos) { + return; + } + // - parameter is wrapped in function-like entity + var current = n; + while (current !== node.initializer) { + if (ts.isFunctionLike(current.parent)) { + return; + } + // computed property names/initializers in instance property declaration of class like entities + // are executed in constructor and thus deferred + if (current.parent.kind === 145 /* PropertyDeclaration */ && + !(ts.hasModifier(current.parent, 32 /* Static */)) && + ts.isClassLike(current.parent.parent)) { + return; + } + current = current.parent; } } + error(n, ts.Diagnostics.Initializer_of_parameter_0_cannot_reference_identifier_1_declared_after_it, ts.declarationNameToString(node.name), ts.declarationNameToString(n)); } } - links.exportsChecked = true; - } - function isNotOverload(declaration) { - return declaration.kind !== 220 /* FunctionDeclaration */ || !!declaration.body; + else { + return ts.forEachChild(n, visit); + } } } - function checkSourceElement(node) { - if (!node) { - return; + function convertAutoToAny(type) { + return type === autoType ? anyType : type; + } + // Check variable, parameter, or property declaration + function checkVariableLikeDeclaration(node) { + checkDecorators(node); + checkSourceElement(node.type); + // For a computed property, just check the initializer and exit + // Do not use hasDynamicName here, because that returns false for well known symbols. + // We want to perform checkComputedPropertyName for all computed properties, including + // well known symbols. + if (node.name.kind === 140 /* ComputedPropertyName */) { + checkComputedPropertyName(node.name); + if (node.initializer) { + checkExpressionCached(node.initializer); + } } - var kind = node.kind; - if (cancellationToken) { - // Only bother checking on a few construct kinds. We don't want to be excessively - // hitting the cancellation token on every node we check. - switch (kind) { - case 225 /* ModuleDeclaration */: - case 221 /* ClassDeclaration */: - case 222 /* InterfaceDeclaration */: - case 220 /* FunctionDeclaration */: - cancellationToken.throwIfCancellationRequested(); + if (node.kind === 169 /* BindingElement */) { + // check computed properties inside property names of binding elements + if (node.propertyName && node.propertyName.kind === 140 /* ComputedPropertyName */) { + checkComputedPropertyName(node.propertyName); + } + // check private/protected variable access + var parent_13 = node.parent.parent; + var parentType = getTypeForBindingElementParent(parent_13); + var name_21 = node.propertyName || node.name; + var property = getPropertyOfType(parentType, getTextOfPropertyName(name_21)); + if (parent_13.initializer && property && getParentOfSymbol(property)) { + checkClassPropertyAccess(parent_13, parent_13.initializer, parentType, property); } } - switch (kind) { - case 141 /* TypeParameter */: - return checkTypeParameter(node); - case 142 /* Parameter */: - return checkParameter(node); - case 145 /* PropertyDeclaration */: - case 144 /* PropertySignature */: - return checkPropertyDeclaration(node); - case 156 /* FunctionType */: - case 157 /* ConstructorType */: - case 151 /* CallSignature */: - case 152 /* ConstructSignature */: - return checkSignatureDeclaration(node); - case 153 /* IndexSignature */: - return checkSignatureDeclaration(node); - case 147 /* MethodDeclaration */: - case 146 /* MethodSignature */: - return checkMethodDeclaration(node); - case 148 /* Constructor */: - return checkConstructorDeclaration(node); - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - return checkAccessorDeclaration(node); - case 155 /* TypeReference */: - return checkTypeReferenceNode(node); - case 154 /* TypePredicate */: - return checkTypePredicate(node); - case 158 /* TypeQuery */: - return checkTypeQuery(node); - case 159 /* TypeLiteral */: - return checkTypeLiteral(node); - case 160 /* ArrayType */: - return checkArrayType(node); - case 161 /* TupleType */: - return checkTupleType(node); - case 162 /* UnionType */: - case 163 /* IntersectionType */: - return checkUnionOrIntersectionType(node); - case 164 /* ParenthesizedType */: - return checkSourceElement(node.type); - case 220 /* FunctionDeclaration */: - return checkFunctionDeclaration(node); - case 199 /* Block */: - case 226 /* ModuleBlock */: - return checkBlock(node); - case 200 /* VariableStatement */: - return checkVariableStatement(node); - case 202 /* ExpressionStatement */: - return checkExpressionStatement(node); - case 203 /* IfStatement */: - return checkIfStatement(node); - case 204 /* DoStatement */: - return checkDoStatement(node); - case 205 /* WhileStatement */: - return checkWhileStatement(node); - case 206 /* ForStatement */: - return checkForStatement(node); - case 207 /* ForInStatement */: - return checkForInStatement(node); - case 208 /* ForOfStatement */: - return checkForOfStatement(node); - case 209 /* ContinueStatement */: - case 210 /* BreakStatement */: - return checkBreakOrContinueStatement(node); - case 211 /* ReturnStatement */: - return checkReturnStatement(node); - case 212 /* WithStatement */: - return checkWithStatement(node); - case 213 /* SwitchStatement */: - return checkSwitchStatement(node); - case 214 /* LabeledStatement */: - return checkLabeledStatement(node); - case 215 /* ThrowStatement */: - return checkThrowStatement(node); - case 216 /* TryStatement */: - return checkTryStatement(node); - case 218 /* VariableDeclaration */: - return checkVariableDeclaration(node); - case 169 /* BindingElement */: - return checkBindingElement(node); - case 221 /* ClassDeclaration */: - return checkClassDeclaration(node); - case 222 /* InterfaceDeclaration */: - return checkInterfaceDeclaration(node); - case 223 /* TypeAliasDeclaration */: - return checkTypeAliasDeclaration(node); - case 224 /* EnumDeclaration */: - return checkEnumDeclaration(node); - case 225 /* ModuleDeclaration */: - return checkModuleDeclaration(node); - case 230 /* ImportDeclaration */: - return checkImportDeclaration(node); - case 229 /* ImportEqualsDeclaration */: - return checkImportEqualsDeclaration(node); - case 236 /* ExportDeclaration */: - return checkExportDeclaration(node); - case 235 /* ExportAssignment */: - return checkExportAssignment(node); - case 201 /* EmptyStatement */: - checkGrammarStatementInAmbientContext(node); - return; - case 217 /* DebuggerStatement */: - checkGrammarStatementInAmbientContext(node); - return; - case 239 /* MissingDeclaration */: - return checkMissingDeclaration(node); + // For a binding pattern, check contained binding elements + if (ts.isBindingPattern(node.name)) { + ts.forEach(node.name.elements, checkSourceElement); } - } - // Function and class expression bodies are checked after all statements in the enclosing body. This is - // to ensure constructs like the following are permitted: - // const foo = function () { - // const s = foo(); - // return "hello"; - // } - // Here, performing a full type check of the body of the function expression whilst in the process of - // determining the type of foo would cause foo to be given type any because of the recursive reference. - // Delaying the type check of the body ensures foo has been assigned a type. - function checkNodeDeferred(node) { - if (deferredNodes) { - deferredNodes.push(node); + // For a parameter declaration with an initializer, error and exit if the containing function doesn't have a body + if (node.initializer && ts.getRootDeclaration(node).kind === 142 /* Parameter */ && ts.nodeIsMissing(ts.getContainingFunction(node).body)) { + error(node, ts.Diagnostics.A_parameter_initializer_is_only_allowed_in_a_function_or_constructor_implementation); + return; } - } - function checkDeferredNodes() { - for (var _i = 0, deferredNodes_1 = deferredNodes; _i < deferredNodes_1.length; _i++) { - var node = deferredNodes_1[_i]; - switch (node.kind) { - case 179 /* FunctionExpression */: - case 180 /* ArrowFunction */: - case 147 /* MethodDeclaration */: - case 146 /* MethodSignature */: - checkFunctionExpressionOrObjectLiteralMethodDeferred(node); - break; - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - checkAccessorDeferred(node); - break; - case 192 /* ClassExpression */: - checkClassExpressionDeferred(node); - break; + // For a binding pattern, validate the initializer and exit + if (ts.isBindingPattern(node.name)) { + // Don't validate for-in initializer as it is already an error + if (node.initializer && node.parent.parent.kind !== 207 /* ForInStatement */) { + checkTypeAssignableTo(checkExpressionCached(node.initializer), getWidenedTypeForVariableLikeDeclaration(node), node, /*headMessage*/ undefined); + checkParameterInitializer(node); } + return; } - } - function checkSourceFile(node) { - ts.performance.mark("beforeCheck"); - checkSourceFileWorker(node); - ts.performance.mark("afterCheck"); - ts.performance.measure("Check", "beforeCheck", "afterCheck"); - } - // Fully type check a source file and collect the relevant diagnostics. - function checkSourceFileWorker(node) { - var links = getNodeLinks(node); - if (!(links.flags & 1 /* TypeChecked */)) { - // If skipLibCheck is enabled, skip type checking if file is a declaration file. - // If skipDefaultLibCheck is enabled, skip type checking if file contains a - // '/// ' directive. - if (compilerOptions.skipLibCheck && node.isDeclarationFile || compilerOptions.skipDefaultLibCheck && node.hasNoDefaultLib) { - return; + var symbol = getSymbolOfNode(node); + var type = convertAutoToAny(getTypeOfVariableOrParameterOrProperty(symbol)); + if (node === symbol.valueDeclaration) { + // Node is the primary declaration of the symbol, just validate the initializer + // Don't validate for-in initializer as it is already an error + if (node.initializer && node.parent.parent.kind !== 207 /* ForInStatement */) { + checkTypeAssignableTo(checkExpressionCached(node.initializer), type, node, /*headMessage*/ undefined); + checkParameterInitializer(node); } - // Grammar checking - checkGrammarSourceFile(node); - potentialThisCollisions.length = 0; - deferredNodes = []; - deferredUnusedIdentifierNodes = produceDiagnostics && noUnusedIdentifiers ? [] : undefined; - ts.forEach(node.statements, checkSourceElement); - checkDeferredNodes(); - if (ts.isExternalModule(node)) { - registerForUnusedIdentifiersCheck(node); + } + else { + // Node is a secondary declaration, check that type is identical to primary declaration and check that + // initializer is consistent with type associated with the node + var declarationType = convertAutoToAny(getWidenedTypeForVariableLikeDeclaration(node)); + if (type !== unknownType && declarationType !== unknownType && !isTypeIdenticalTo(type, declarationType)) { + error(node.name, ts.Diagnostics.Subsequent_variable_declarations_must_have_the_same_type_Variable_0_must_be_of_type_1_but_here_has_type_2, ts.declarationNameToString(node.name), typeToString(type), typeToString(declarationType)); } - if (!node.isDeclarationFile) { - checkUnusedIdentifiers(); + if (node.initializer) { + checkTypeAssignableTo(checkExpressionCached(node.initializer), declarationType, node, /*headMessage*/ undefined); } - deferredNodes = undefined; - deferredUnusedIdentifierNodes = undefined; - if (ts.isExternalOrCommonJsModule(node)) { - checkExternalModuleExports(node); + if (!areDeclarationFlagsIdentical(node, symbol.valueDeclaration)) { + error(symbol.valueDeclaration.name, ts.Diagnostics.All_declarations_of_0_must_have_identical_modifiers, ts.declarationNameToString(node.name)); + error(node.name, ts.Diagnostics.All_declarations_of_0_must_have_identical_modifiers, ts.declarationNameToString(node.name)); } - if (potentialThisCollisions.length) { - ts.forEach(potentialThisCollisions, checkIfThisIsCapturedInEnclosingScope); - potentialThisCollisions.length = 0; + } + if (node.kind !== 145 /* PropertyDeclaration */ && node.kind !== 144 /* PropertySignature */) { + // We know we don't have a binding pattern or computed name here + checkExportsOnMergedDeclarations(node); + if (node.kind === 218 /* VariableDeclaration */ || node.kind === 169 /* BindingElement */) { + checkVarDeclaredNamesNotShadowed(node); } - links.flags |= 1 /* TypeChecked */; + checkCollisionWithCapturedSuperVariable(node, node.name); + checkCollisionWithCapturedThisVariable(node, node.name); + checkCollisionWithRequireExportsInGeneratedCode(node, node.name); + checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); } } - function getDiagnostics(sourceFile, ct) { - try { - // Record the cancellation token so it can be checked later on during checkSourceElement. - // Do this in a finally block so we can ensure that it gets reset back to nothing after - // this call is done. - cancellationToken = ct; - return getDiagnosticsWorker(sourceFile); + function areDeclarationFlagsIdentical(left, right) { + if ((left.kind === 142 /* Parameter */ && right.kind === 218 /* VariableDeclaration */) || + (left.kind === 218 /* VariableDeclaration */ && right.kind === 142 /* Parameter */)) { + // Differences in optionality between parameters and variables are allowed. + return true; } - finally { - cancellationToken = undefined; + if (ts.hasQuestionToken(left) !== ts.hasQuestionToken(right)) { + return false; } + var interestingFlags = 8 /* Private */ | + 16 /* Protected */ | + 256 /* Async */ | + 128 /* Abstract */ | + 64 /* Readonly */ | + 32 /* Static */; + return (ts.getModifierFlags(left) & interestingFlags) === (ts.getModifierFlags(right) & interestingFlags); } - function getDiagnosticsWorker(sourceFile) { - throwIfNonDiagnosticsProducing(); - if (sourceFile) { - checkSourceFile(sourceFile); - return diagnostics.getDiagnostics(sourceFile.fileName); - } - ts.forEach(host.getSourceFiles(), checkSourceFile); - return diagnostics.getDiagnostics(); + function checkVariableDeclaration(node) { + checkGrammarVariableDeclaration(node); + return checkVariableLikeDeclaration(node); } - function getGlobalDiagnostics() { - throwIfNonDiagnosticsProducing(); - return diagnostics.getGlobalDiagnostics(); + function checkBindingElement(node) { + checkGrammarBindingElement(node); + return checkVariableLikeDeclaration(node); } - function throwIfNonDiagnosticsProducing() { - if (!produceDiagnostics) { - throw new Error("Trying to get diagnostics from a type checker that does not produce them."); - } + function checkVariableStatement(node) { + // Grammar checking + checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarVariableDeclarationList(node.declarationList) || checkGrammarForDisallowedLetOrConstStatement(node); + ts.forEach(node.declarationList.declarations, checkSourceElement); } - // Language service support - function isInsideWithStatementBody(node) { - if (node) { - while (node.parent) { - if (node.parent.kind === 212 /* WithStatement */ && node.parent.statement === node) { - return true; + function checkGrammarDisallowedModifiersOnObjectLiteralExpressionMethod(node) { + // We only disallow modifier on a method declaration if it is a property of object-literal-expression + if (node.modifiers && node.parent.kind === 171 /* ObjectLiteralExpression */) { + if (ts.isAsyncFunctionLike(node)) { + if (node.modifiers.length > 1) { + return grammarErrorOnFirstToken(node, ts.Diagnostics.Modifiers_cannot_appear_here); } - node = node.parent; + } + else { + return grammarErrorOnFirstToken(node, ts.Diagnostics.Modifiers_cannot_appear_here); } } - return false; } - function getSymbolsInScope(location, meaning) { - var symbols = ts.createMap(); - var memberFlags = 0 /* None */; - if (isInsideWithStatementBody(location)) { - // We cannot answer semantic questions within a with block, do not proceed any further - return []; + function checkExpressionStatement(node) { + // Grammar checking + checkGrammarStatementInAmbientContext(node); + checkExpression(node.expression); + } + function checkIfStatement(node) { + // Grammar checking + checkGrammarStatementInAmbientContext(node); + checkExpression(node.expression); + checkSourceElement(node.thenStatement); + if (node.thenStatement.kind === 201 /* EmptyStatement */) { + error(node.thenStatement, ts.Diagnostics.The_body_of_an_if_statement_cannot_be_the_empty_statement); } - populateSymbols(); - return symbolsToArray(symbols); - function populateSymbols() { - while (location) { - if (location.locals && !isGlobalSourceFile(location)) { - copySymbols(location.locals, meaning); - } - switch (location.kind) { - case 256 /* SourceFile */: - if (!ts.isExternalOrCommonJsModule(location)) { - break; - } - case 225 /* ModuleDeclaration */: - copySymbols(getSymbolOfNode(location).exports, meaning & 8914931 /* ModuleMember */); - break; - case 224 /* EnumDeclaration */: - copySymbols(getSymbolOfNode(location).exports, meaning & 8 /* EnumMember */); - break; - case 192 /* ClassExpression */: - var className = location.name; - if (className) { - copySymbol(location.symbol, meaning); - } - // fall through; this fall-through is necessary because we would like to handle - // type parameter inside class expression similar to how we handle it in classDeclaration and interface Declaration - case 221 /* ClassDeclaration */: - case 222 /* InterfaceDeclaration */: - // If we didn't come from static member of class or interface, - // add the type parameters into the symbol table - // (type parameters of classDeclaration/classExpression and interface are in member property of the symbol. - // Note: that the memberFlags come from previous iteration. - if (!(memberFlags & 32 /* Static */)) { - copySymbols(getSymbolOfNode(location).members, meaning & 793064 /* Type */); - } - break; - case 179 /* FunctionExpression */: - var funcName = location.name; - if (funcName) { - copySymbol(location.symbol, meaning); - } - break; - } - if (ts.introducesArgumentsExoticObject(location)) { - copySymbol(argumentsSymbol, meaning); - } - memberFlags = ts.getModifierFlags(location); - location = location.parent; + checkSourceElement(node.elseStatement); + } + function checkDoStatement(node) { + // Grammar checking + checkGrammarStatementInAmbientContext(node); + checkSourceElement(node.statement); + checkExpression(node.expression); + } + function checkWhileStatement(node) { + // Grammar checking + checkGrammarStatementInAmbientContext(node); + checkExpression(node.expression); + checkSourceElement(node.statement); + } + function checkForStatement(node) { + // Grammar checking + if (!checkGrammarStatementInAmbientContext(node)) { + if (node.initializer && node.initializer.kind === 219 /* VariableDeclarationList */) { + checkGrammarVariableDeclarationList(node.initializer); } - copySymbols(globals, meaning); } - /** - * Copy the given symbol into symbol tables if the symbol has the given meaning - * and it doesn't already existed in the symbol table - * @param key a key for storing in symbol table; if undefined, use symbol.name - * @param symbol the symbol to be added into symbol table - * @param meaning meaning of symbol to filter by before adding to symbol table - */ - function copySymbol(symbol, meaning) { - if (symbol.flags & meaning) { - var id = symbol.name; - // We will copy all symbol regardless of its reserved name because - // symbolsToArray will check whether the key is a reserved name and - // it will not copy symbol with reserved name to the array - if (!symbols[id]) { - symbols[id] = symbol; - } + if (node.initializer) { + if (node.initializer.kind === 219 /* VariableDeclarationList */) { + ts.forEach(node.initializer.declarations, checkVariableDeclaration); } - } - function copySymbols(source, meaning) { - if (meaning) { - for (var id in source) { - var symbol = source[id]; - copySymbol(symbol, meaning); - } + else { + checkExpression(node.initializer); } } - } - function isTypeDeclarationName(name) { - return name.kind === 69 /* Identifier */ && - isTypeDeclaration(name.parent) && - name.parent.name === name; - } - function isTypeDeclaration(node) { - switch (node.kind) { - case 141 /* TypeParameter */: - case 221 /* ClassDeclaration */: - case 222 /* InterfaceDeclaration */: - case 223 /* TypeAliasDeclaration */: - case 224 /* EnumDeclaration */: - return true; + if (node.condition) + checkExpression(node.condition); + if (node.incrementor) + checkExpression(node.incrementor); + checkSourceElement(node.statement); + if (node.locals) { + registerForUnusedIdentifiersCheck(node); } } - // True if the given identifier is part of a type reference - function isTypeReferenceIdentifier(entityName) { - var node = entityName; - while (node.parent && node.parent.kind === 139 /* QualifiedName */) { - node = node.parent; + function checkForOfStatement(node) { + checkGrammarForInOrForOfStatement(node); + // Check the LHS and RHS + // If the LHS is a declaration, just check it as a variable declaration, which will in turn check the RHS + // via checkRightHandSideOfForOf. + // If the LHS is an expression, check the LHS, as a destructuring assignment or as a reference. + // Then check that the RHS is assignable to it. + if (node.initializer.kind === 219 /* VariableDeclarationList */) { + checkForInOrForOfVariableDeclaration(node); } - return node.parent && (node.parent.kind === 155 /* TypeReference */ || node.parent.kind === 267 /* JSDocTypeReference */); - } - function isHeritageClauseElementIdentifier(entityName) { - var node = entityName; - while (node.parent && node.parent.kind === 172 /* PropertyAccessExpression */) { - node = node.parent; + else { + var varExpr = node.initializer; + var iteratedType = checkRightHandSideOfForOf(node.expression); + // There may be a destructuring assignment on the left side + if (varExpr.kind === 170 /* ArrayLiteralExpression */ || varExpr.kind === 171 /* ObjectLiteralExpression */) { + // iteratedType may be undefined. In this case, we still want to check the structure of + // varExpr, in particular making sure it's a valid LeftHandSideExpression. But we'd like + // to short circuit the type relation checking as much as possible, so we pass the unknownType. + checkDestructuringAssignment(varExpr, iteratedType || unknownType); + } + else { + var leftType = checkExpression(varExpr); + checkReferenceExpression(varExpr, /*invalidReferenceMessage*/ ts.Diagnostics.Invalid_left_hand_side_in_for_of_statement, + /*constantVariableMessage*/ ts.Diagnostics.The_left_hand_side_of_a_for_of_statement_cannot_be_a_constant_or_a_read_only_property); + // iteratedType will be undefined if the rightType was missing properties/signatures + // required to get its iteratedType (like [Symbol.iterator] or next). This may be + // because we accessed properties from anyType, or it may have led to an error inside + // getElementTypeOfIterable. + if (iteratedType) { + checkTypeAssignableTo(iteratedType, leftType, varExpr, /*headMessage*/ undefined); + } + } } - return node.parent && node.parent.kind === 194 /* ExpressionWithTypeArguments */; - } - function forEachEnclosingClass(node, callback) { - var result; - while (true) { - node = ts.getContainingClass(node); - if (!node) - break; - if (result = callback(node)) - break; + checkSourceElement(node.statement); + if (node.locals) { + registerForUnusedIdentifiersCheck(node); } - return result; - } - function isNodeWithinClass(node, classDeclaration) { - return !!forEachEnclosingClass(node, function (n) { return n === classDeclaration; }); } - function getLeftSideOfImportEqualsOrExportAssignment(nodeOnRightSide) { - while (nodeOnRightSide.parent.kind === 139 /* QualifiedName */) { - nodeOnRightSide = nodeOnRightSide.parent; + function checkForInStatement(node) { + // Grammar checking + checkGrammarForInOrForOfStatement(node); + // TypeScript 1.0 spec (April 2014): 5.4 + // In a 'for-in' statement of the form + // for (let VarDecl in Expr) Statement + // VarDecl must be a variable declaration without a type annotation that declares a variable of type Any, + // and Expr must be an expression of type Any, an object type, or a type parameter type. + if (node.initializer.kind === 219 /* VariableDeclarationList */) { + var variable = node.initializer.declarations[0]; + if (variable && ts.isBindingPattern(variable.name)) { + error(variable.name, ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_a_destructuring_pattern); + } + checkForInOrForOfVariableDeclaration(node); } - if (nodeOnRightSide.parent.kind === 229 /* ImportEqualsDeclaration */) { - return nodeOnRightSide.parent.moduleReference === nodeOnRightSide && nodeOnRightSide.parent; + else { + // In a 'for-in' statement of the form + // for (Var in Expr) Statement + // Var must be an expression classified as a reference of type Any or the String primitive type, + // and Expr must be an expression of type Any, an object type, or a type parameter type. + var varExpr = node.initializer; + var leftType = checkExpression(varExpr); + if (varExpr.kind === 170 /* ArrayLiteralExpression */ || varExpr.kind === 171 /* ObjectLiteralExpression */) { + error(varExpr, ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_a_destructuring_pattern); + } + else if (!isTypeAnyOrAllConstituentTypesHaveKind(leftType, 34 /* StringLike */)) { + error(varExpr, ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_must_be_of_type_string_or_any); + } + else { + // run check only former check succeeded to avoid cascading errors + checkReferenceExpression(varExpr, ts.Diagnostics.Invalid_left_hand_side_in_for_in_statement, ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_a_constant_or_a_read_only_property); + } } - if (nodeOnRightSide.parent.kind === 235 /* ExportAssignment */) { - return nodeOnRightSide.parent.expression === nodeOnRightSide && nodeOnRightSide.parent; + var rightType = checkNonNullExpression(node.expression); + // unknownType is returned i.e. if node.expression is identifier whose name cannot be resolved + // in this case error about missing name is already reported - do not report extra one + if (!isTypeAnyOrAllConstituentTypesHaveKind(rightType, 2588672 /* ObjectType */ | 16384 /* TypeParameter */)) { + error(node.expression, ts.Diagnostics.The_right_hand_side_of_a_for_in_statement_must_be_of_type_any_an_object_type_or_a_type_parameter); + } + checkSourceElement(node.statement); + if (node.locals) { + registerForUnusedIdentifiersCheck(node); } - return undefined; } - function isInRightSideOfImportOrExportAssignment(node) { - return getLeftSideOfImportEqualsOrExportAssignment(node) !== undefined; + function checkForInOrForOfVariableDeclaration(iterationStatement) { + var variableDeclarationList = iterationStatement.initializer; + // checkGrammarForInOrForOfStatement will check that there is exactly one declaration. + if (variableDeclarationList.declarations.length >= 1) { + var decl = variableDeclarationList.declarations[0]; + checkVariableDeclaration(decl); + } } - function getSymbolOfEntityNameOrPropertyAccessExpression(entityName) { - if (ts.isDeclarationName(entityName)) { - return getSymbolOfNode(entityName.parent); + function checkRightHandSideOfForOf(rhsExpression) { + var expressionType = checkNonNullExpression(rhsExpression); + return checkIteratedTypeOrElementType(expressionType, rhsExpression, /*allowStringInput*/ true); + } + function checkIteratedTypeOrElementType(inputType, errorNode, allowStringInput) { + if (isTypeAny(inputType)) { + return inputType; } - if (ts.isInJavaScriptFile(entityName) && entityName.parent.kind === 172 /* PropertyAccessExpression */) { - var specialPropertyAssignmentKind = ts.getSpecialPropertyAssignmentKind(entityName.parent.parent); - switch (specialPropertyAssignmentKind) { - case 1 /* ExportsProperty */: - case 3 /* PrototypeProperty */: - return getSymbolOfNode(entityName.parent); - case 4 /* ThisProperty */: - case 2 /* ModuleExports */: - return getSymbolOfNode(entityName.parent.parent); - default: + if (languageVersion >= 2 /* ES6 */) { + return checkElementTypeOfIterable(inputType, errorNode); + } + if (allowStringInput) { + return checkElementTypeOfArrayOrString(inputType, errorNode); + } + if (isArrayLikeType(inputType)) { + var indexType = getIndexTypeOfType(inputType, 1 /* Number */); + if (indexType) { + return indexType; } } - if (entityName.parent.kind === 235 /* ExportAssignment */ && ts.isEntityNameExpression(entityName)) { - return resolveEntityName(entityName, - /*all meanings*/ 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */ | 8388608 /* Alias */); + if (errorNode) { + error(errorNode, ts.Diagnostics.Type_0_is_not_an_array_type, typeToString(inputType)); } - if (entityName.kind !== 172 /* PropertyAccessExpression */ && isInRightSideOfImportOrExportAssignment(entityName)) { - // Since we already checked for ExportAssignment, this really could only be an Import - var importEqualsDeclaration = ts.getAncestor(entityName, 229 /* ImportEqualsDeclaration */); - ts.Debug.assert(importEqualsDeclaration !== undefined); - return getSymbolOfPartOfRightHandSideOfImportEquals(entityName, importEqualsDeclaration, /*dontResolveAlias*/ true); + return unknownType; + } + /** + * When errorNode is undefined, it means we should not report any errors. + */ + function checkElementTypeOfIterable(iterable, errorNode) { + var elementType = getElementTypeOfIterable(iterable, errorNode); + // Now even though we have extracted the iteratedType, we will have to validate that the type + // passed in is actually an Iterable. + if (errorNode && elementType) { + checkTypeAssignableTo(iterable, createIterableType(elementType), errorNode); } - if (ts.isRightSideOfQualifiedNameOrPropertyAccess(entityName)) { - entityName = entityName.parent; + return elementType || anyType; + } + /** + * We want to treat type as an iterable, and get the type it is an iterable of. The iterable + * must have the following structure (annotated with the names of the variables below): + * + * { // iterable + * [Symbol.iterator]: { // iteratorFunction + * (): Iterator + * } + * } + * + * T is the type we are after. At every level that involves analyzing return types + * of signatures, we union the return types of all the signatures. + * + * Another thing to note is that at any step of this process, we could run into a dead end, + * meaning either the property is missing, or we run into the anyType. If either of these things + * happens, we return undefined to signal that we could not find the iterated type. If a property + * is missing, and the previous step did not result in 'any', then we also give an error if the + * caller requested it. Then the caller can decide what to do in the case where there is no iterated + * type. This is different from returning anyType, because that would signify that we have matched the + * whole pattern and that T (above) is 'any'. + */ + function getElementTypeOfIterable(type, errorNode) { + if (isTypeAny(type)) { + return undefined; } - if (isHeritageClauseElementIdentifier(entityName)) { - var meaning = 0 /* None */; - // In an interface or class, we're definitely interested in a type. - if (entityName.parent.kind === 194 /* ExpressionWithTypeArguments */) { - meaning = 793064 /* Type */; - // In a class 'extends' clause we are also looking for a value. - if (ts.isExpressionWithTypeArgumentsInClassExtendsClause(entityName.parent)) { - meaning |= 107455 /* Value */; - } + var typeAsIterable = type; + if (!typeAsIterable.iterableElementType) { + // As an optimization, if the type is instantiated directly using the globalIterableType (Iterable), + // then just grab its type argument. + if ((type.flags & 131072 /* Reference */) && type.target === getGlobalIterableType()) { + typeAsIterable.iterableElementType = type.typeArguments[0]; } else { - meaning = 1920 /* Namespace */; + var iteratorFunction = getTypeOfPropertyOfType(type, ts.getPropertyNameForKnownSymbolName("iterator")); + if (isTypeAny(iteratorFunction)) { + return undefined; + } + var iteratorFunctionSignatures = iteratorFunction ? getSignaturesOfType(iteratorFunction, 0 /* Call */) : emptyArray; + if (iteratorFunctionSignatures.length === 0) { + if (errorNode) { + error(errorNode, ts.Diagnostics.Type_must_have_a_Symbol_iterator_method_that_returns_an_iterator); + } + return undefined; + } + typeAsIterable.iterableElementType = getElementTypeOfIterator(getUnionType(ts.map(iteratorFunctionSignatures, getReturnTypeOfSignature), /*subtypeReduction*/ true), errorNode); } - meaning |= 8388608 /* Alias */; - return resolveEntityName(entityName, meaning); } - else if (ts.isPartOfExpression(entityName)) { - if (ts.nodeIsMissing(entityName)) { - // Missing entity name. - return undefined; + return typeAsIterable.iterableElementType; + } + /** + * This function has very similar logic as getElementTypeOfIterable, except that it operates on + * Iterators instead of Iterables. Here is the structure: + * + * { // iterator + * next: { // iteratorNextFunction + * (): { // iteratorNextResult + * value: T // iteratorNextValue + * } + * } + * } + * + */ + function getElementTypeOfIterator(type, errorNode) { + if (isTypeAny(type)) { + return undefined; + } + var typeAsIterator = type; + if (!typeAsIterator.iteratorElementType) { + // As an optimization, if the type is instantiated directly using the globalIteratorType (Iterator), + // then just grab its type argument. + if ((type.flags & 131072 /* Reference */) && type.target === getGlobalIteratorType()) { + typeAsIterator.iteratorElementType = type.typeArguments[0]; } - if (entityName.kind === 69 /* Identifier */) { - if (ts.isJSXTagName(entityName) && isJsxIntrinsicIdentifier(entityName)) { - return getIntrinsicTagSymbol(entityName.parent); + else { + var iteratorNextFunction = getTypeOfPropertyOfType(type, "next"); + if (isTypeAny(iteratorNextFunction)) { + return undefined; } - return resolveEntityName(entityName, 107455 /* Value */, /*ignoreErrors*/ false, /*dontResolveAlias*/ true); - } - else if (entityName.kind === 172 /* PropertyAccessExpression */) { - var symbol = getNodeLinks(entityName).resolvedSymbol; - if (!symbol) { - checkPropertyAccessExpression(entityName); + var iteratorNextFunctionSignatures = iteratorNextFunction ? getSignaturesOfType(iteratorNextFunction, 0 /* Call */) : emptyArray; + if (iteratorNextFunctionSignatures.length === 0) { + if (errorNode) { + error(errorNode, ts.Diagnostics.An_iterator_must_have_a_next_method); + } + return undefined; } - return getNodeLinks(entityName).resolvedSymbol; - } - else if (entityName.kind === 139 /* QualifiedName */) { - var symbol = getNodeLinks(entityName).resolvedSymbol; - if (!symbol) { - checkQualifiedName(entityName); + var iteratorNextResult = getUnionType(ts.map(iteratorNextFunctionSignatures, getReturnTypeOfSignature), /*subtypeReduction*/ true); + if (isTypeAny(iteratorNextResult)) { + return undefined; } - return getNodeLinks(entityName).resolvedSymbol; + var iteratorNextValue = getTypeOfPropertyOfType(iteratorNextResult, "value"); + if (!iteratorNextValue) { + if (errorNode) { + error(errorNode, ts.Diagnostics.The_type_returned_by_the_next_method_of_an_iterator_must_have_a_value_property); + } + return undefined; + } + typeAsIterator.iteratorElementType = iteratorNextValue; } } - else if (isTypeReferenceIdentifier(entityName)) { - var meaning = (entityName.parent.kind === 155 /* TypeReference */ || entityName.parent.kind === 267 /* JSDocTypeReference */) ? 793064 /* Type */ : 1920 /* Namespace */; - return resolveEntityName(entityName, meaning, /*ignoreErrors*/ false, /*dontResolveAlias*/ true); - } - else if (entityName.parent.kind === 246 /* JsxAttribute */) { - return getJsxAttributePropertySymbol(entityName.parent); + return typeAsIterator.iteratorElementType; + } + function getElementTypeOfIterableIterator(type) { + if (isTypeAny(type)) { + return undefined; } - if (entityName.parent.kind === 154 /* TypePredicate */) { - return resolveEntityName(entityName, /*meaning*/ 1 /* FunctionScopedVariable */); + // As an optimization, if the type is instantiated directly using the globalIterableIteratorType (IterableIterator), + // then just grab its type argument. + if ((type.flags & 131072 /* Reference */) && type.target === getGlobalIterableIteratorType()) { + return type.typeArguments[0]; } - // Do we want to return undefined here? - return undefined; + return getElementTypeOfIterable(type, /*errorNode*/ undefined) || + getElementTypeOfIterator(type, /*errorNode*/ undefined); } - function getSymbolAtLocation(node) { - if (node.kind === 256 /* SourceFile */) { - return ts.isExternalModule(node) ? getMergedSymbol(node.symbol) : undefined; + /** + * This function does the following steps: + * 1. Break up arrayOrStringType (possibly a union) into its string constituents and array constituents. + * 2. Take the element types of the array constituents. + * 3. Return the union of the element types, and string if there was a string constituent. + * + * For example: + * string -> string + * number[] -> number + * string[] | number[] -> string | number + * string | number[] -> string | number + * string | string[] | number[] -> string | number + * + * It also errors if: + * 1. Some constituent is neither a string nor an array. + * 2. Some constituent is a string and target is less than ES5 (because in ES3 string is not indexable). + */ + function checkElementTypeOfArrayOrString(arrayOrStringType, errorNode) { + ts.Debug.assert(languageVersion < 2 /* ES6 */); + // After we remove all types that are StringLike, we will know if there was a string constituent + // based on whether the remaining type is the same as the initial type. + var arrayType = arrayOrStringType; + if (arrayOrStringType.flags & 524288 /* Union */) { + arrayType = getUnionType(ts.filter(arrayOrStringType.types, function (t) { return !(t.flags & 34 /* StringLike */); }), /*subtypeReduction*/ true); } - if (isInsideWithStatementBody(node)) { - // We cannot answer semantic questions within a with block, do not proceed any further - return undefined; + else if (arrayOrStringType.flags & 34 /* StringLike */) { + arrayType = neverType; } - if (ts.isDeclarationName(node)) { - // This is a declaration, call getSymbolOfNode - return getSymbolOfNode(node.parent); + var hasStringConstituent = arrayOrStringType !== arrayType; + var reportedError = false; + if (hasStringConstituent) { + if (languageVersion < 1 /* ES5 */) { + error(errorNode, ts.Diagnostics.Using_a_string_in_a_for_of_statement_is_only_supported_in_ECMAScript_5_and_higher); + reportedError = true; + } + // Now that we've removed all the StringLike types, if no constituents remain, then the entire + // arrayOrStringType was a string. + if (arrayType.flags & 8192 /* Never */) { + return stringType; + } } - else if (ts.isLiteralComputedPropertyDeclarationName(node)) { - return getSymbolOfNode(node.parent.parent); + if (!isArrayLikeType(arrayType)) { + if (!reportedError) { + // Which error we report depends on whether there was a string constituent. For example, + // if the input type is number | string, we want to say that number is not an array type. + // But if the input was just number, we want to say that number is not an array type + // or a string type. + var diagnostic = hasStringConstituent + ? ts.Diagnostics.Type_0_is_not_an_array_type + : ts.Diagnostics.Type_0_is_not_an_array_type_or_a_string_type; + error(errorNode, diagnostic, typeToString(arrayType)); + } + return hasStringConstituent ? stringType : unknownType; } - if (node.kind === 69 /* Identifier */) { - if (isInRightSideOfImportOrExportAssignment(node)) { - return getSymbolOfEntityNameOrPropertyAccessExpression(node); + var arrayElementType = getIndexTypeOfType(arrayType, 1 /* Number */) || unknownType; + if (hasStringConstituent) { + // This is just an optimization for the case where arrayOrStringType is string | string[] + if (arrayElementType.flags & 34 /* StringLike */) { + return stringType; } - else if (node.parent.kind === 169 /* BindingElement */ && - node.parent.parent.kind === 167 /* ObjectBindingPattern */ && - node === node.parent.propertyName) { - var typeOfPattern = getTypeOfNode(node.parent.parent); - var propertyDeclaration = typeOfPattern && getPropertyOfType(typeOfPattern, node.text); - if (propertyDeclaration) { - return propertyDeclaration; - } + return getUnionType([arrayElementType, stringType], /*subtypeReduction*/ true); + } + return arrayElementType; + } + function checkBreakOrContinueStatement(node) { + // Grammar checking + checkGrammarStatementInAmbientContext(node) || checkGrammarBreakOrContinueStatement(node); + // TODO: Check that target label is valid + } + function isGetAccessorWithAnnotatedSetAccessor(node) { + return !!(node.kind === 149 /* GetAccessor */ && ts.getSetAccessorTypeAnnotationNode(ts.getDeclarationOfKind(node.symbol, 150 /* SetAccessor */))); + } + function isUnwrappedReturnTypeVoidOrAny(func, returnType) { + var unwrappedReturnType = ts.isAsyncFunctionLike(func) ? getPromisedType(returnType) : returnType; + return unwrappedReturnType && maybeTypeOfKind(unwrappedReturnType, 1024 /* Void */ | 1 /* Any */); + } + function checkReturnStatement(node) { + // Grammar checking + if (!checkGrammarStatementInAmbientContext(node)) { + var functionBlock = ts.getContainingFunction(node); + if (!functionBlock) { + grammarErrorOnFirstToken(node, ts.Diagnostics.A_return_statement_can_only_be_used_within_a_function_body); } } - switch (node.kind) { - case 69 /* Identifier */: - case 172 /* PropertyAccessExpression */: - case 139 /* QualifiedName */: - return getSymbolOfEntityNameOrPropertyAccessExpression(node); - case 97 /* ThisKeyword */: - var container = ts.getThisContainer(node, /*includeArrowFunctions*/ false); - if (ts.isFunctionLike(container)) { - var sig = getSignatureFromDeclaration(container); - if (sig.thisParameter) { - return sig.thisParameter; - } + var func = ts.getContainingFunction(node); + if (func) { + var signature = getSignatureFromDeclaration(func); + var returnType = getReturnTypeOfSignature(signature); + if (strictNullChecks || node.expression || returnType.flags & 8192 /* Never */) { + var exprType = node.expression ? checkExpressionCached(node.expression) : undefinedType; + if (func.asteriskToken) { + // A generator does not need its return expressions checked against its return type. + // Instead, the yield expressions are checked against the element type. + // TODO: Check return expressions of generators when return type tracking is added + // for generators. + return; } - // fallthrough - case 95 /* SuperKeyword */: - var type = ts.isPartOfExpression(node) ? checkExpression(node) : getTypeFromTypeNode(node); - return type.symbol; - case 165 /* ThisType */: - return getTypeFromTypeNode(node).symbol; - case 121 /* ConstructorKeyword */: - // constructor keyword for an overload, should take us to the definition if it exist - var constructorDeclaration = node.parent; - if (constructorDeclaration && constructorDeclaration.kind === 148 /* Constructor */) { - return constructorDeclaration.parent.symbol; + if (func.kind === 150 /* SetAccessor */) { + if (node.expression) { + error(node.expression, ts.Diagnostics.Setters_cannot_return_a_value); + } } - return undefined; - case 9 /* StringLiteral */: - // External module name in an import declaration - if ((ts.isExternalModuleImportEqualsDeclaration(node.parent.parent) && - ts.getExternalModuleImportEqualsDeclarationExpression(node.parent.parent) === node) || - ((node.parent.kind === 230 /* ImportDeclaration */ || node.parent.kind === 236 /* ExportDeclaration */) && - node.parent.moduleSpecifier === node)) { - return resolveExternalModuleName(node, node); + else if (func.kind === 148 /* Constructor */) { + if (node.expression && !checkTypeAssignableTo(exprType, returnType, node.expression)) { + error(node.expression, ts.Diagnostics.Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class); + } } - // Fall through - case 8 /* NumericLiteral */: - // index access - if (node.parent.kind === 173 /* ElementAccessExpression */ && node.parent.argumentExpression === node) { - var objectType = checkExpression(node.parent.expression); - if (objectType === unknownType) - return undefined; - var apparentType = getApparentType(objectType); - if (apparentType === unknownType) - return undefined; - return getPropertyOfType(apparentType, node.text); + else if (func.type || isGetAccessorWithAnnotatedSetAccessor(func)) { + if (ts.isAsyncFunctionLike(func)) { + var promisedType = getPromisedType(returnType); + var awaitedType = checkAwaitedType(exprType, node.expression || node, ts.Diagnostics.Return_expression_in_async_function_does_not_have_a_valid_callable_then_member); + if (promisedType) { + // If the function has a return type, but promisedType is + // undefined, an error will be reported in checkAsyncFunctionReturnType + // so we don't need to report one here. + checkTypeAssignableTo(awaitedType, promisedType, node.expression || node); + } + } + else { + checkTypeAssignableTo(exprType, returnType, node.expression || node); + } } - break; + } + else if (func.kind !== 148 /* Constructor */ && compilerOptions.noImplicitReturns && !isUnwrappedReturnTypeVoidOrAny(func, returnType)) { + // The function has a return type, but the return statement doesn't have an expression. + error(node, ts.Diagnostics.Not_all_code_paths_return_a_value); + } } - return undefined; } - function getShorthandAssignmentValueSymbol(location) { - // The function returns a value symbol of an identifier in the short-hand property assignment. - // This is necessary as an identifier in short-hand property assignment can contains two meaning: - // property name and property value. - if (location && location.kind === 254 /* ShorthandPropertyAssignment */) { - return resolveEntityName(location.name, 107455 /* Value */ | 8388608 /* Alias */); + function checkWithStatement(node) { + // Grammar checking for withStatement + if (!checkGrammarStatementInAmbientContext(node)) { + if (node.flags & 262144 /* AwaitContext */) { + grammarErrorOnFirstToken(node, ts.Diagnostics.with_statements_are_not_allowed_in_an_async_function_block); + } + } + checkExpression(node.expression); + var sourceFile = ts.getSourceFileOfNode(node); + if (!hasParseDiagnostics(sourceFile)) { + var start = ts.getSpanOfTokenAtPosition(sourceFile, node.pos).start; + var end = node.statement.pos; + grammarErrorAtPos(sourceFile, start, end - start, ts.Diagnostics.The_with_statement_is_not_supported_All_symbols_in_a_with_block_will_have_type_any); } - return undefined; } - /** Returns the target of an export specifier without following aliases */ - function getExportSpecifierLocalTargetSymbol(node) { - return node.parent.parent.moduleSpecifier ? - getExternalModuleMember(node.parent.parent, node) : - resolveEntityName(node.propertyName || node.name, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */ | 8388608 /* Alias */); + function checkSwitchStatement(node) { + // Grammar checking + checkGrammarStatementInAmbientContext(node); + var firstDefaultClause; + var hasDuplicateDefaultClause = false; + var expressionType = checkExpression(node.expression); + ts.forEach(node.caseBlock.clauses, function (clause) { + // Grammar check for duplicate default clauses, skip if we already report duplicate default clause + if (clause.kind === 250 /* DefaultClause */ && !hasDuplicateDefaultClause) { + if (firstDefaultClause === undefined) { + firstDefaultClause = clause; + } + else { + var sourceFile = ts.getSourceFileOfNode(node); + var start = ts.skipTrivia(sourceFile.text, clause.pos); + var end = clause.statements.length > 0 ? clause.statements[0].pos : clause.end; + grammarErrorAtPos(sourceFile, start, end - start, ts.Diagnostics.A_default_clause_cannot_appear_more_than_once_in_a_switch_statement); + hasDuplicateDefaultClause = true; + } + } + if (produceDiagnostics && clause.kind === 249 /* CaseClause */) { + var caseClause = clause; + // TypeScript 1.0 spec (April 2014): 5.9 + // In a 'switch' statement, each 'case' expression must be of a type that is comparable + // to or from the type of the 'switch' expression. + var caseType = checkExpression(caseClause.expression); + if (!isTypeEqualityComparableTo(expressionType, caseType)) { + // expressionType is not comparable to caseType, try the reversed check and report errors if it fails + checkTypeComparableTo(caseType, expressionType, caseClause.expression, /*headMessage*/ undefined); + } + } + ts.forEach(clause.statements, checkSourceElement); + }); + if (node.caseBlock.locals) { + registerForUnusedIdentifiersCheck(node.caseBlock); + } } - function getTypeOfNode(node) { - if (isInsideWithStatementBody(node)) { - // We cannot answer semantic questions within a with block, do not proceed any further - return unknownType; + function checkLabeledStatement(node) { + // Grammar checking + if (!checkGrammarStatementInAmbientContext(node)) { + var current = node.parent; + while (current) { + if (ts.isFunctionLike(current)) { + break; + } + if (current.kind === 214 /* LabeledStatement */ && current.label.text === node.label.text) { + var sourceFile = ts.getSourceFileOfNode(node); + grammarErrorOnNode(node.label, ts.Diagnostics.Duplicate_label_0, ts.getTextOfNodeFromSourceText(sourceFile.text, node.label)); + break; + } + current = current.parent; + } } - if (ts.isPartOfTypeNode(node)) { - return getTypeFromTypeNode(node); - } - if (ts.isPartOfExpression(node)) { - return getTypeOfExpression(node); - } - if (ts.isExpressionWithTypeArgumentsInClassExtendsClause(node)) { - // A SyntaxKind.ExpressionWithTypeArguments is considered a type node, except when it occurs in the - // extends clause of a class. We handle that case here. - return getBaseTypes(getDeclaredTypeOfSymbol(getSymbolOfNode(node.parent.parent)))[0]; - } - if (isTypeDeclaration(node)) { - // In this case, we call getSymbolOfNode instead of getSymbolAtLocation because it is a declaration - var symbol = getSymbolOfNode(node); - return getDeclaredTypeOfSymbol(symbol); - } - if (isTypeDeclarationName(node)) { - var symbol = getSymbolAtLocation(node); - return symbol && getDeclaredTypeOfSymbol(symbol); - } - if (ts.isDeclaration(node)) { - // In this case, we call getSymbolOfNode instead of getSymbolAtLocation because it is a declaration - var symbol = getSymbolOfNode(node); - return getTypeOfSymbol(symbol); - } - if (ts.isDeclarationName(node)) { - var symbol = getSymbolAtLocation(node); - return symbol && getTypeOfSymbol(symbol); - } - if (ts.isBindingPattern(node)) { - return getTypeForVariableLikeDeclaration(node.parent, /*includeOptionality*/ true); - } - if (isInRightSideOfImportOrExportAssignment(node)) { - var symbol = getSymbolAtLocation(node); - var declaredType = symbol && getDeclaredTypeOfSymbol(symbol); - return declaredType !== unknownType ? declaredType : getTypeOfSymbol(symbol); - } - return unknownType; + // ensure that label is unique + checkSourceElement(node.statement); } - // Gets the type of object literal or array literal of destructuring assignment. - // { a } from - // for ( { a } of elems) { - // } - // [ a ] from - // [a] = [ some array ...] - function getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(expr) { - ts.Debug.assert(expr.kind === 171 /* ObjectLiteralExpression */ || expr.kind === 170 /* ArrayLiteralExpression */); - // If this is from "for of" - // for ( { a } of elems) { - // } - if (expr.parent.kind === 208 /* ForOfStatement */) { - var iteratedType = checkRightHandSideOfForOf(expr.parent.expression); - return checkDestructuringAssignment(expr, iteratedType || unknownType); - } - // If this is from "for" initializer - // for ({a } = elems[0];.....) { } - if (expr.parent.kind === 187 /* BinaryExpression */) { - var iteratedType = checkExpression(expr.parent.right); - return checkDestructuringAssignment(expr, iteratedType || unknownType); - } - // If this is from nested object binding pattern - // for ({ skills: { primary, secondary } } = multiRobot, i = 0; i < 1; i++) { - if (expr.parent.kind === 253 /* PropertyAssignment */) { - var typeOfParentObjectLiteral = getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(expr.parent.parent); - return checkObjectLiteralDestructuringPropertyAssignment(typeOfParentObjectLiteral || unknownType, expr.parent); + function checkThrowStatement(node) { + // Grammar checking + if (!checkGrammarStatementInAmbientContext(node)) { + if (node.expression === undefined) { + grammarErrorAfterFirstToken(node, ts.Diagnostics.Line_break_not_permitted_here); + } } - // Array literal assignment - array destructuring pattern - ts.Debug.assert(expr.parent.kind === 170 /* ArrayLiteralExpression */); - // [{ property1: p1, property2 }] = elems; - var typeOfArrayLiteral = getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(expr.parent); - var elementType = checkIteratedTypeOrElementType(typeOfArrayLiteral || unknownType, expr.parent, /*allowStringInput*/ false) || unknownType; - return checkArrayLiteralDestructuringElementAssignment(expr.parent, typeOfArrayLiteral, ts.indexOf(expr.parent.elements, expr), elementType || unknownType); - } - // Gets the property symbol corresponding to the property in destructuring assignment - // 'property1' from - // for ( { property1: a } of elems) { - // } - // 'property1' at location 'a' from: - // [a] = [ property1, property2 ] - function getPropertySymbolOfDestructuringAssignment(location) { - // Get the type of the object or array literal and then look for property of given name in the type - var typeOfObjectLiteral = getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(location.parent.parent); - return typeOfObjectLiteral && getPropertyOfType(typeOfObjectLiteral, location.text); - } - function getTypeOfExpression(expr) { - if (ts.isRightSideOfQualifiedNameOrPropertyAccess(expr)) { - expr = expr.parent; + if (node.expression) { + checkExpression(node.expression); } - return getRegularTypeOfLiteralType(checkExpression(expr)); - } - /** - * Gets either the static or instance type of a class element, based on - * whether the element is declared as "static". - */ - function getParentTypeOfClassElement(node) { - var classSymbol = getSymbolOfNode(node.parent); - return ts.getModifierFlags(node) & 32 /* Static */ - ? getTypeOfSymbol(classSymbol) - : getDeclaredTypeOfSymbol(classSymbol); } - // Return the list of properties of the given type, augmented with properties from Function - // if the type has call or construct signatures - function getAugmentedPropertiesOfType(type) { - type = getApparentType(type); - var propsByName = createSymbolTable(getPropertiesOfType(type)); - if (getSignaturesOfType(type, 0 /* Call */).length || getSignaturesOfType(type, 1 /* Construct */).length) { - ts.forEach(getPropertiesOfType(globalFunctionType), function (p) { - if (!propsByName[p.name]) { - propsByName[p.name] = p; + function checkTryStatement(node) { + // Grammar checking + checkGrammarStatementInAmbientContext(node); + checkBlock(node.tryBlock); + var catchClause = node.catchClause; + if (catchClause) { + // Grammar checking + if (catchClause.variableDeclaration) { + if (catchClause.variableDeclaration.name.kind !== 69 /* Identifier */) { + grammarErrorOnFirstToken(catchClause.variableDeclaration.name, ts.Diagnostics.Catch_clause_variable_name_must_be_an_identifier); } - }); - } - return getNamedMembers(propsByName); - } - function getRootSymbols(symbol) { - if (symbol.flags & 268435456 /* SyntheticProperty */) { - var symbols_3 = []; - var name_23 = symbol.name; - ts.forEach(getSymbolLinks(symbol).containingType.types, function (t) { - var symbol = getPropertyOfType(t, name_23); - if (symbol) { - symbols_3.push(symbol); + else if (catchClause.variableDeclaration.type) { + grammarErrorOnFirstToken(catchClause.variableDeclaration.type, ts.Diagnostics.Catch_clause_variable_cannot_have_a_type_annotation); + } + else if (catchClause.variableDeclaration.initializer) { + grammarErrorOnFirstToken(catchClause.variableDeclaration.initializer, ts.Diagnostics.Catch_clause_variable_cannot_have_an_initializer); + } + else { + var identifierName = catchClause.variableDeclaration.name.text; + var locals = catchClause.block.locals; + if (locals) { + var localSymbol = locals[identifierName]; + if (localSymbol && (localSymbol.flags & 2 /* BlockScopedVariable */) !== 0) { + grammarErrorOnNode(localSymbol.valueDeclaration, ts.Diagnostics.Cannot_redeclare_identifier_0_in_catch_clause, identifierName); + } + } } - }); - return symbols_3; - } - else if (symbol.flags & 67108864 /* Transient */) { - var target = void 0; - var next = symbol; - while (next = getSymbolLinks(next).target) { - target = next; - } - if (target) { - return [target]; } + checkBlock(catchClause.block); + } + if (node.finallyBlock) { + checkBlock(node.finallyBlock); } - return [symbol]; } - // Emitter support - function isArgumentsLocalBinding(node) { - if (!ts.isGeneratedIdentifier(node)) { - node = ts.getParseTreeNode(node, ts.isIdentifier); - if (node) { - return getReferencedValueSymbol(node) === argumentsSymbol; + function checkIndexConstraints(type) { + var declaredNumberIndexer = getIndexDeclarationOfSymbol(type.symbol, 1 /* Number */); + var declaredStringIndexer = getIndexDeclarationOfSymbol(type.symbol, 0 /* String */); + var stringIndexType = getIndexTypeOfType(type, 0 /* String */); + var numberIndexType = getIndexTypeOfType(type, 1 /* Number */); + if (stringIndexType || numberIndexType) { + ts.forEach(getPropertiesOfObjectType(type), function (prop) { + var propType = getTypeOfSymbol(prop); + checkIndexConstraintForProperty(prop, propType, type, declaredStringIndexer, stringIndexType, 0 /* String */); + checkIndexConstraintForProperty(prop, propType, type, declaredNumberIndexer, numberIndexType, 1 /* Number */); + }); + if (type.flags & 32768 /* Class */ && ts.isClassLike(type.symbol.valueDeclaration)) { + var classDeclaration = type.symbol.valueDeclaration; + for (var _i = 0, _a = classDeclaration.members; _i < _a.length; _i++) { + var member = _a[_i]; + // Only process instance properties with computed names here. + // Static properties cannot be in conflict with indexers, + // and properties with literal names were already checked. + if (!(ts.getModifierFlags(member) & 32 /* Static */) && ts.hasDynamicName(member)) { + var propType = getTypeOfSymbol(member.symbol); + checkIndexConstraintForProperty(member.symbol, propType, type, declaredStringIndexer, stringIndexType, 0 /* String */); + checkIndexConstraintForProperty(member.symbol, propType, type, declaredNumberIndexer, numberIndexType, 1 /* Number */); + } + } } } - return false; - } - function moduleExportsSomeValue(moduleReferenceExpression) { - var moduleSymbol = resolveExternalModuleName(moduleReferenceExpression.parent, moduleReferenceExpression); - if (!moduleSymbol || ts.isShorthandAmbientModuleSymbol(moduleSymbol)) { - // If the module is not found or is shorthand, assume that it may export a value. - return true; + var errorNode; + if (stringIndexType && numberIndexType) { + errorNode = declaredNumberIndexer || declaredStringIndexer; + // condition 'errorNode === undefined' may appear if types does not declare nor string neither number indexer + if (!errorNode && (type.flags & 65536 /* Interface */)) { + var someBaseTypeHasBothIndexers = ts.forEach(getBaseTypes(type), function (base) { return getIndexTypeOfType(base, 0 /* String */) && getIndexTypeOfType(base, 1 /* Number */); }); + errorNode = someBaseTypeHasBothIndexers ? undefined : type.symbol.declarations[0]; + } } - var hasExportAssignment = hasExportAssignmentSymbol(moduleSymbol); - // if module has export assignment then 'resolveExternalModuleSymbol' will return resolved symbol for export assignment - // otherwise it will return moduleSymbol itself - moduleSymbol = resolveExternalModuleSymbol(moduleSymbol); - var symbolLinks = getSymbolLinks(moduleSymbol); - if (symbolLinks.exportsSomeValue === undefined) { - // for export assignments - check if resolved symbol for RHS is itself a value - // otherwise - check if at least one export is value - symbolLinks.exportsSomeValue = hasExportAssignment - ? !!(moduleSymbol.flags & 107455 /* Value */) - : ts.forEachProperty(getExportsOfModule(moduleSymbol), isValue); + if (errorNode && !isTypeAssignableTo(numberIndexType, stringIndexType)) { + error(errorNode, ts.Diagnostics.Numeric_index_type_0_is_not_assignable_to_string_index_type_1, typeToString(numberIndexType), typeToString(stringIndexType)); } - return symbolLinks.exportsSomeValue; - function isValue(s) { - s = resolveSymbol(s); - return s && !!(s.flags & 107455 /* Value */); + function checkIndexConstraintForProperty(prop, propertyType, containingType, indexDeclaration, indexType, indexKind) { + if (!indexType) { + return; + } + // index is numeric and property name is not valid numeric literal + if (indexKind === 1 /* Number */ && !isNumericName(prop.valueDeclaration.name)) { + return; + } + // perform property check if property or indexer is declared in 'type' + // this allows to rule out cases when both property and indexer are inherited from the base class + var errorNode; + if (prop.valueDeclaration.name.kind === 140 /* ComputedPropertyName */ || prop.parent === containingType.symbol) { + errorNode = prop.valueDeclaration; + } + else if (indexDeclaration) { + errorNode = indexDeclaration; + } + else if (containingType.flags & 65536 /* Interface */) { + // for interfaces property and indexer might be inherited from different bases + // check if any base class already has both property and indexer. + // check should be performed only if 'type' is the first type that brings property\indexer together + var someBaseClassHasBothPropertyAndIndexer = ts.forEach(getBaseTypes(containingType), function (base) { return getPropertyOfObjectType(base, prop.name) && getIndexTypeOfType(base, indexKind); }); + errorNode = someBaseClassHasBothPropertyAndIndexer ? undefined : containingType.symbol.declarations[0]; + } + if (errorNode && !isTypeAssignableTo(propertyType, indexType)) { + var errorMessage = indexKind === 0 /* String */ + ? ts.Diagnostics.Property_0_of_type_1_is_not_assignable_to_string_index_type_2 + : ts.Diagnostics.Property_0_of_type_1_is_not_assignable_to_numeric_index_type_2; + error(errorNode, errorMessage, symbolToString(prop), typeToString(propertyType), typeToString(indexType)); + } } } - function isNameOfModuleOrEnumDeclaration(node) { - var parent = node.parent; - return ts.isModuleOrEnumDeclaration(parent) && node === parent.name; + function checkTypeNameIsReserved(name, message) { + // TS 1.0 spec (April 2014): 3.6.1 + // The predefined type keywords are reserved and cannot be used as names of user defined types. + switch (name.text) { + case "any": + case "number": + case "boolean": + case "string": + case "symbol": + case "void": + error(name, message, name.text); + } } - // When resolved as an expression identifier, if the given node references an exported entity, return the declaration - // node of the exported entity's container. Otherwise, return undefined. - function getReferencedExportContainer(node, prefixLocals) { - node = ts.getParseTreeNode(node, ts.isIdentifier); - if (node) { - // When resolving the export container for the name of a module or enum - // declaration, we need to start resolution at the declaration's container. - // Otherwise, we could incorrectly resolve the export container as the - // declaration if it contains an exported member with the same name. - var symbol = getReferencedValueSymbol(node, /*startInDeclarationContainer*/ isNameOfModuleOrEnumDeclaration(node)); - if (symbol) { - if (symbol.flags & 1048576 /* ExportValue */) { - // If we reference an exported entity within the same module declaration, then whether - // we prefix depends on the kind of entity. SymbolFlags.ExportHasLocal encompasses all the - // kinds that we do NOT prefix. - var exportSymbol = getMergedSymbol(symbol.exportSymbol); - if (!prefixLocals && exportSymbol.flags & 944 /* ExportHasLocal */) { - return undefined; - } - symbol = exportSymbol; - } - var parentSymbol = getParentOfSymbol(symbol); - if (parentSymbol) { - if (parentSymbol.flags & 512 /* ValueModule */ && parentSymbol.valueDeclaration.kind === 256 /* SourceFile */) { - var symbolFile = parentSymbol.valueDeclaration; - var referenceFile = ts.getSourceFileOfNode(node); - // If `node` accesses an export and that export isn't in the same file, then symbol is a namespace export, so return undefined. - var symbolIsUmdExport = symbolFile !== referenceFile; - return symbolIsUmdExport ? undefined : symbolFile; - } - for (var n = node.parent; n; n = n.parent) { - if (ts.isModuleOrEnumDeclaration(n) && getSymbolOfNode(n) === parentSymbol) { - return n; + /** Check each type parameter and check that type parameters have no duplicate type parameter declarations */ + function checkTypeParameters(typeParameterDeclarations) { + if (typeParameterDeclarations) { + for (var i = 0, n = typeParameterDeclarations.length; i < n; i++) { + var node = typeParameterDeclarations[i]; + checkTypeParameter(node); + if (produceDiagnostics) { + for (var j = 0; j < i; j++) { + if (typeParameterDeclarations[j].symbol === node.symbol) { + error(node.name, ts.Diagnostics.Duplicate_identifier_0, ts.declarationNameToString(node.name)); } } } } } } - // When resolved as an expression identifier, if the given node references an import, return the declaration of - // that import. Otherwise, return undefined. - function getReferencedImportDeclaration(node) { - node = ts.getParseTreeNode(node, ts.isIdentifier); - if (node) { - var symbol = getReferencedValueSymbol(node); - if (symbol && symbol.flags & 8388608 /* Alias */) { - return getDeclarationOfAliasSymbol(symbol); + /** Check that type parameter lists are identical across multiple declarations */ + function checkTypeParameterListsIdentical(node, symbol) { + if (symbol.declarations.length === 1) { + return; + } + var firstDecl; + for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { + var declaration = _a[_i]; + if (declaration.kind === 221 /* ClassDeclaration */ || declaration.kind === 222 /* InterfaceDeclaration */) { + if (!firstDecl) { + firstDecl = declaration; + } + else if (!areTypeParametersIdentical(firstDecl.typeParameters, node.typeParameters)) { + error(node.name, ts.Diagnostics.All_declarations_of_0_must_have_identical_type_parameters, node.name.text); + } } } - return undefined; } - function isSymbolOfDeclarationWithCollidingName(symbol) { - if (symbol.flags & 418 /* BlockScoped */) { - var links = getSymbolLinks(symbol); - if (links.isDeclarationWithCollidingName === undefined) { - var container = ts.getEnclosingBlockScopeContainer(symbol.valueDeclaration); - if (ts.isStatementWithLocals(container)) { - var nodeLinks_1 = getNodeLinks(symbol.valueDeclaration); - if (!!resolveName(container.parent, symbol.name, 107455 /* Value */, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined)) { - // redeclaration - always should be renamed - links.isDeclarationWithCollidingName = true; + function checkClassExpression(node) { + checkClassLikeDeclaration(node); + checkNodeDeferred(node); + return getTypeOfSymbol(getSymbolOfNode(node)); + } + function checkClassExpressionDeferred(node) { + ts.forEach(node.members, checkSourceElement); + registerForUnusedIdentifiersCheck(node); + } + function checkClassDeclaration(node) { + if (!node.name && !(ts.getModifierFlags(node) & 512 /* Default */)) { + grammarErrorOnFirstToken(node, ts.Diagnostics.A_class_declaration_without_the_default_modifier_must_have_a_name); + } + checkClassLikeDeclaration(node); + ts.forEach(node.members, checkSourceElement); + registerForUnusedIdentifiersCheck(node); + } + function checkClassLikeDeclaration(node) { + checkGrammarClassDeclarationHeritageClauses(node); + checkDecorators(node); + if (node.name) { + checkTypeNameIsReserved(node.name, ts.Diagnostics.Class_name_cannot_be_0); + checkCollisionWithCapturedThisVariable(node, node.name); + checkCollisionWithRequireExportsInGeneratedCode(node, node.name); + checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); + } + checkTypeParameters(node.typeParameters); + checkExportsOnMergedDeclarations(node); + var symbol = getSymbolOfNode(node); + var type = getDeclaredTypeOfSymbol(symbol); + var typeWithThis = getTypeWithThisArgument(type); + var staticType = getTypeOfSymbol(symbol); + checkTypeParameterListsIdentical(node, symbol); + checkClassForDuplicateDeclarations(node); + var baseTypeNode = ts.getClassExtendsHeritageClauseElement(node); + if (baseTypeNode) { + var baseTypes = getBaseTypes(type); + if (baseTypes.length && produceDiagnostics) { + var baseType_1 = baseTypes[0]; + var staticBaseType = getBaseConstructorTypeOfClass(type); + checkBaseTypeAccessibility(staticBaseType, baseTypeNode); + checkSourceElement(baseTypeNode.expression); + if (baseTypeNode.typeArguments) { + ts.forEach(baseTypeNode.typeArguments, checkSourceElement); + for (var _i = 0, _a = getConstructorsForTypeArguments(staticBaseType, baseTypeNode.typeArguments); _i < _a.length; _i++) { + var constructor = _a[_i]; + if (!checkTypeArgumentConstraints(constructor.typeParameters, baseTypeNode.typeArguments)) { + break; + } } - else if (nodeLinks_1.flags & 131072 /* CapturedBlockScopedBinding */) { - // binding is captured in the function - // should be renamed if: - // - binding is not top level - top level bindings never collide with anything - // AND - // - binding is not declared in loop, should be renamed to avoid name reuse across siblings - // let a, b - // { let x = 1; a = () => x; } - // { let x = 100; b = () => x; } - // console.log(a()); // should print '1' - // console.log(b()); // should print '100' - // OR - // - binding is declared inside loop but not in inside initializer of iteration statement or directly inside loop body - // * variables from initializer are passed to rewritten loop body as parameters so they are not captured directly - // * variables that are declared immediately in loop body will become top level variable after loop is rewritten and thus - // they will not collide with anything - var isDeclaredInLoop = nodeLinks_1.flags & 262144 /* BlockScopedBindingInLoop */; - var inLoopInitializer = ts.isIterationStatement(container, /*lookInLabeledStatements*/ false); - var inLoopBodyBlock = container.kind === 199 /* Block */ && ts.isIterationStatement(container.parent, /*lookInLabeledStatements*/ false); - links.isDeclarationWithCollidingName = !ts.isBlockScopedContainerTopLevel(container) && (!isDeclaredInLoop || (!inLoopInitializer && !inLoopBodyBlock)); + } + checkTypeAssignableTo(typeWithThis, getTypeWithThisArgument(baseType_1, type.thisType), node.name || node, ts.Diagnostics.Class_0_incorrectly_extends_base_class_1); + checkTypeAssignableTo(staticType, getTypeWithoutSignatures(staticBaseType), node.name || node, ts.Diagnostics.Class_static_side_0_incorrectly_extends_base_class_static_side_1); + if (baseType_1.symbol.valueDeclaration && !ts.isInAmbientContext(baseType_1.symbol.valueDeclaration)) { + if (!isBlockScopedNameDeclaredBeforeUse(baseType_1.symbol.valueDeclaration, node)) { + error(baseTypeNode, ts.Diagnostics.A_class_must_be_declared_after_its_base_class); } - else { - links.isDeclarationWithCollidingName = false; + } + if (!(staticBaseType.symbol && staticBaseType.symbol.flags & 32 /* Class */)) { + // When the static base type is a "class-like" constructor function (but not actually a class), we verify + // that all instantiated base constructor signatures return the same type. We can simply compare the type + // references (as opposed to checking the structure of the types) because elsewhere we have already checked + // that the base type is a class or interface type (and not, for example, an anonymous object type). + var constructors = getInstantiatedConstructorsForTypeArguments(staticBaseType, baseTypeNode.typeArguments); + if (ts.forEach(constructors, function (sig) { return getReturnTypeOfSignature(sig) !== baseType_1; })) { + error(baseTypeNode.expression, ts.Diagnostics.Base_constructors_must_all_have_the_same_return_type); } } + checkKindsOfPropertyMemberOverrides(type, baseType_1); } - return links.isDeclarationWithCollidingName; } - return false; - } - // When resolved as an expression identifier, if the given node references a nested block scoped entity with - // a name that either hides an existing name or might hide it when compiled downlevel, - // return the declaration of that entity. Otherwise, return undefined. - function getReferencedDeclarationWithCollidingName(node) { - if (!ts.isGeneratedIdentifier(node)) { - node = ts.getParseTreeNode(node, ts.isIdentifier); - if (node) { - var symbol = getReferencedValueSymbol(node); - if (symbol && isSymbolOfDeclarationWithCollidingName(symbol)) { - return symbol.valueDeclaration; + var implementedTypeNodes = ts.getClassImplementsHeritageClauseElements(node); + if (implementedTypeNodes) { + for (var _b = 0, implementedTypeNodes_1 = implementedTypeNodes; _b < implementedTypeNodes_1.length; _b++) { + var typeRefNode = implementedTypeNodes_1[_b]; + if (!ts.isEntityNameExpression(typeRefNode.expression)) { + error(typeRefNode.expression, ts.Diagnostics.A_class_can_only_implement_an_identifier_Slashqualified_name_with_optional_type_arguments); + } + checkTypeReferenceNode(typeRefNode); + if (produceDiagnostics) { + var t = getTypeFromTypeNode(typeRefNode); + if (t !== unknownType) { + var declaredType = (t.flags & 131072 /* Reference */) ? t.target : t; + if (declaredType.flags & (32768 /* Class */ | 65536 /* Interface */)) { + checkTypeAssignableTo(typeWithThis, getTypeWithThisArgument(t, type.thisType), node.name || node, ts.Diagnostics.Class_0_incorrectly_implements_interface_1); + } + else { + error(typeRefNode, ts.Diagnostics.A_class_may_only_implement_another_class_or_interface); + } + } } } } - return undefined; + if (produceDiagnostics) { + checkIndexConstraints(type); + checkTypeForDuplicateIndexSignatures(node); + } } - // Return true if the given node is a declaration of a nested block scoped entity with a name that either hides an - // existing name or might hide a name when compiled downlevel - function isDeclarationWithCollidingName(node) { - node = ts.getParseTreeNode(node, ts.isDeclaration); - if (node) { - var symbol = getSymbolOfNode(node); - if (symbol) { - return isSymbolOfDeclarationWithCollidingName(symbol); + function checkBaseTypeAccessibility(type, node) { + var signatures = getSignaturesOfType(type, 1 /* Construct */); + if (signatures.length) { + var declaration = signatures[0].declaration; + if (declaration && ts.getModifierFlags(declaration) & 8 /* Private */) { + var typeClassDeclaration = getClassLikeDeclarationOfSymbol(type.symbol); + if (!isNodeWithinClass(node, typeClassDeclaration)) { + error(node, ts.Diagnostics.Cannot_extend_a_class_0_Class_constructor_is_marked_as_private, node.expression.text); + } } } - return false; } - function isValueAliasDeclaration(node) { - node = ts.getParseTreeNode(node); - if (node === undefined) { - // A synthesized node comes from an emit transformation and is always a value. - return true; - } - switch (node.kind) { - case 229 /* ImportEqualsDeclaration */: - case 231 /* ImportClause */: - case 232 /* NamespaceImport */: - case 234 /* ImportSpecifier */: - case 238 /* ExportSpecifier */: - return isAliasResolvedToValue(getSymbolOfNode(node) || unknownSymbol); - case 236 /* ExportDeclaration */: - var exportClause = node.exportClause; - return exportClause && ts.forEach(exportClause.elements, isValueAliasDeclaration); - case 235 /* ExportAssignment */: - return node.expression - && node.expression.kind === 69 /* Identifier */ - ? isAliasResolvedToValue(getSymbolOfNode(node) || unknownSymbol) - : true; - } - return false; - } - function isTopLevelValueImportEqualsWithEntityName(node) { - node = ts.getParseTreeNode(node, ts.isImportEqualsDeclaration); - if (node === undefined || node.parent.kind !== 256 /* SourceFile */ || !ts.isInternalModuleImportEqualsDeclaration(node)) { - // parent is not source file or it is not reference to internal module - return false; - } - var isValue = isAliasResolvedToValue(getSymbolOfNode(node)); - return isValue && node.moduleReference && !ts.nodeIsMissing(node.moduleReference); - } - function isAliasResolvedToValue(symbol) { - var target = resolveAlias(symbol); - if (target === unknownSymbol) { - return true; - } - // const enums and modules that contain only const enums are not considered values from the emit perspective - // unless 'preserveConstEnums' option is set to true - return target.flags & 107455 /* Value */ && - (compilerOptions.preserveConstEnums || !isConstEnumOrConstEnumOnlyModule(target)); + function getTargetSymbol(s) { + // if symbol is instantiated its flags are not copied from the 'target' + // so we'll need to get back original 'target' symbol to work with correct set of flags + return s.flags & 16777216 /* Instantiated */ ? getSymbolLinks(s).target : s; } - function isConstEnumOrConstEnumOnlyModule(s) { - return isConstEnumSymbol(s) || s.constEnumOnlyModule; + function getClassLikeDeclarationOfSymbol(symbol) { + return ts.forEach(symbol.declarations, function (d) { return ts.isClassLike(d) ? d : undefined; }); } - function isReferencedAliasDeclaration(node, checkChildren) { - node = ts.getParseTreeNode(node); - // Purely synthesized nodes are always emitted. - if (node === undefined) { - return true; - } - if (ts.isAliasSymbolDeclaration(node)) { - var symbol = getSymbolOfNode(node); - if (symbol && getSymbolLinks(symbol).referenced) { - return true; + function checkKindsOfPropertyMemberOverrides(type, baseType) { + // TypeScript 1.0 spec (April 2014): 8.2.3 + // A derived class inherits all members from its base class it doesn't override. + // Inheritance means that a derived class implicitly contains all non - overridden members of the base class. + // Both public and private property members are inherited, but only public property members can be overridden. + // A property member in a derived class is said to override a property member in a base class + // when the derived class property member has the same name and kind(instance or static) + // as the base class property member. + // The type of an overriding property member must be assignable(section 3.8.4) + // to the type of the overridden property member, or otherwise a compile - time error occurs. + // Base class instance member functions can be overridden by derived class instance member functions, + // but not by other kinds of members. + // Base class instance member variables and accessors can be overridden by + // derived class instance member variables and accessors, but not by other kinds of members. + // NOTE: assignability is checked in checkClassDeclaration + var baseProperties = getPropertiesOfObjectType(baseType); + for (var _i = 0, baseProperties_1 = baseProperties; _i < baseProperties_1.length; _i++) { + var baseProperty = baseProperties_1[_i]; + var base = getTargetSymbol(baseProperty); + if (base.flags & 134217728 /* Prototype */) { + continue; } - } - if (checkChildren) { - return ts.forEachChild(node, function (node) { return isReferencedAliasDeclaration(node, checkChildren); }); - } - return false; - } - function isImplementationOfOverload(node) { - if (ts.nodeIsPresent(node.body)) { - var symbol = getSymbolOfNode(node); - var signaturesOfSymbol = getSignaturesOfSymbol(symbol); - // If this function body corresponds to function with multiple signature, it is implementation of overload - // e.g.: function foo(a: string): string; - // function foo(a: number): number; - // function foo(a: any) { // This is implementation of the overloads - // return a; - // } - return signaturesOfSymbol.length > 1 || - // If there is single signature for the symbol, it is overload if that signature isn't coming from the node - // e.g.: function foo(a: string): string; - // function foo(a: any) { // This is implementation of the overloads - // return a; - // } - (signaturesOfSymbol.length === 1 && signaturesOfSymbol[0].declaration !== node); - } - return false; - } - function getNodeCheckFlags(node) { - node = ts.getParseTreeNode(node); - return node ? getNodeLinks(node).flags : undefined; - } - function getEnumMemberValue(node) { - computeEnumMemberValues(node.parent); - return getNodeLinks(node).enumMemberValue; - } - function getConstantValue(node) { - if (node.kind === 255 /* EnumMember */) { - return getEnumMemberValue(node); - } - var symbol = getNodeLinks(node).resolvedSymbol; - if (symbol && (symbol.flags & 8 /* EnumMember */)) { - // inline property\index accesses only for const enums - if (ts.isConstEnumDeclaration(symbol.valueDeclaration.parent)) { - return getEnumMemberValue(symbol.valueDeclaration); + var derived = getTargetSymbol(getPropertyOfObjectType(type, base.name)); + var baseDeclarationFlags = getDeclarationModifierFlagsFromSymbol(base); + ts.Debug.assert(!!derived, "derived should point to something, even if it is the base class' declaration."); + if (derived) { + // In order to resolve whether the inherited method was overridden in the base class or not, + // we compare the Symbols obtained. Since getTargetSymbol returns the symbol on the *uninstantiated* + // type declaration, derived and base resolve to the same symbol even in the case of generic classes. + if (derived === base) { + // derived class inherits base without override/redeclaration + var derivedClassDecl = getClassLikeDeclarationOfSymbol(type.symbol); + // It is an error to inherit an abstract member without implementing it or being declared abstract. + // If there is no declaration for the derived class (as in the case of class expressions), + // then the class cannot be declared abstract. + if (baseDeclarationFlags & 128 /* Abstract */ && (!derivedClassDecl || !(ts.getModifierFlags(derivedClassDecl) & 128 /* Abstract */))) { + if (derivedClassDecl.kind === 192 /* ClassExpression */) { + error(derivedClassDecl, ts.Diagnostics.Non_abstract_class_expression_does_not_implement_inherited_abstract_member_0_from_class_1, symbolToString(baseProperty), typeToString(baseType)); + } + else { + error(derivedClassDecl, ts.Diagnostics.Non_abstract_class_0_does_not_implement_inherited_abstract_member_1_from_class_2, typeToString(type), symbolToString(baseProperty), typeToString(baseType)); + } + } + } + else { + // derived overrides base. + var derivedDeclarationFlags = getDeclarationModifierFlagsFromSymbol(derived); + if ((baseDeclarationFlags & 8 /* Private */) || (derivedDeclarationFlags & 8 /* Private */)) { + // either base or derived property is private - not override, skip it + continue; + } + if ((baseDeclarationFlags & 32 /* Static */) !== (derivedDeclarationFlags & 32 /* Static */)) { + // value of 'static' is not the same for properties - not override, skip it + continue; + } + if ((base.flags & derived.flags & 8192 /* Method */) || ((base.flags & 98308 /* PropertyOrAccessor */) && (derived.flags & 98308 /* PropertyOrAccessor */))) { + // method is overridden with method or property/accessor is overridden with property/accessor - correct case + continue; + } + var errorMessage = void 0; + if (base.flags & 8192 /* Method */) { + if (derived.flags & 98304 /* Accessor */) { + errorMessage = ts.Diagnostics.Class_0_defines_instance_member_function_1_but_extended_class_2_defines_it_as_instance_member_accessor; + } + else { + ts.Debug.assert((derived.flags & 4 /* Property */) !== 0); + errorMessage = ts.Diagnostics.Class_0_defines_instance_member_function_1_but_extended_class_2_defines_it_as_instance_member_property; + } + } + else if (base.flags & 4 /* Property */) { + ts.Debug.assert((derived.flags & 8192 /* Method */) !== 0); + errorMessage = ts.Diagnostics.Class_0_defines_instance_member_property_1_but_extended_class_2_defines_it_as_instance_member_function; + } + else { + ts.Debug.assert((base.flags & 98304 /* Accessor */) !== 0); + ts.Debug.assert((derived.flags & 8192 /* Method */) !== 0); + errorMessage = ts.Diagnostics.Class_0_defines_instance_member_accessor_1_but_extended_class_2_defines_it_as_instance_member_function; + } + error(derived.valueDeclaration.name, errorMessage, typeToString(baseType), symbolToString(base), typeToString(type)); + } } } - return undefined; } - function isFunctionType(type) { - return type.flags & 2588672 /* ObjectType */ && getSignaturesOfType(type, 0 /* Call */).length > 0; + function isAccessor(kind) { + return kind === 149 /* GetAccessor */ || kind === 150 /* SetAccessor */; } - function getTypeReferenceSerializationKind(typeName, location) { - // Resolve the symbol as a value to ensure the type can be reached at runtime during emit. - var valueSymbol = resolveEntityName(typeName, 107455 /* Value */, /*ignoreErrors*/ true, /*dontResolveAlias*/ false, location); - var globalPromiseSymbol = tryGetGlobalPromiseConstructorSymbol(); - if (globalPromiseSymbol && valueSymbol === globalPromiseSymbol) { - return ts.TypeReferenceSerializationKind.Promise; - } - var constructorType = valueSymbol ? getTypeOfSymbol(valueSymbol) : undefined; - if (constructorType && isConstructorType(constructorType)) { - return ts.TypeReferenceSerializationKind.TypeWithConstructSignatureAndValue; - } - // Resolve the symbol as a type so that we can provide a more useful hint for the type serializer. - var typeSymbol = resolveEntityName(typeName, 793064 /* Type */, /*ignoreErrors*/ true, /*dontResolveAlias*/ false, location); - // We might not be able to resolve type symbol so use unknown type in that case (eg error case) - if (!typeSymbol) { - return ts.TypeReferenceSerializationKind.ObjectType; - } - var type = getDeclaredTypeOfSymbol(typeSymbol); - if (type === unknownType) { - return ts.TypeReferenceSerializationKind.Unknown; - } - else if (type.flags & 1 /* Any */) { - return ts.TypeReferenceSerializationKind.ObjectType; - } - else if (isTypeOfKind(type, 1024 /* Void */ | 6144 /* Nullable */ | 8192 /* Never */)) { - return ts.TypeReferenceSerializationKind.VoidNullableOrNeverType; - } - else if (isTypeOfKind(type, 136 /* BooleanLike */)) { - return ts.TypeReferenceSerializationKind.BooleanType; - } - else if (isTypeOfKind(type, 340 /* NumberLike */)) { - return ts.TypeReferenceSerializationKind.NumberLikeType; - } - else if (isTypeOfKind(type, 34 /* StringLike */)) { - return ts.TypeReferenceSerializationKind.StringLikeType; - } - else if (isTupleType(type)) { - return ts.TypeReferenceSerializationKind.ArrayLikeType; - } - else if (isTypeOfKind(type, 512 /* ESSymbol */)) { - return ts.TypeReferenceSerializationKind.ESSymbolType; - } - else if (isFunctionType(type)) { - return ts.TypeReferenceSerializationKind.TypeWithCallSignature; + function areTypeParametersIdentical(list1, list2) { + if (!list1 && !list2) { + return true; } - else if (isArrayType(type)) { - return ts.TypeReferenceSerializationKind.ArrayLikeType; + if (!list1 || !list2 || list1.length !== list2.length) { + return false; } - else { - return ts.TypeReferenceSerializationKind.ObjectType; + // TypeScript 1.0 spec (April 2014): + // When a generic interface has multiple declarations, all declarations must have identical type parameter + // lists, i.e. identical type parameter names with identical constraints in identical order. + for (var i = 0, len = list1.length; i < len; i++) { + var tp1 = list1[i]; + var tp2 = list2[i]; + if (tp1.name.text !== tp2.name.text) { + return false; + } + if (!tp1.constraint && !tp2.constraint) { + continue; + } + if (!tp1.constraint || !tp2.constraint) { + return false; + } + if (!isTypeIdenticalTo(getTypeFromTypeNode(tp1.constraint), getTypeFromTypeNode(tp2.constraint))) { + return false; + } } + return true; } - function writeTypeOfDeclaration(declaration, enclosingDeclaration, flags, writer) { - // Get type of the symbol if this is the valid symbol otherwise get type at location - var symbol = getSymbolOfNode(declaration); - var type = symbol && !(symbol.flags & (2048 /* TypeLiteral */ | 131072 /* Signature */)) - ? getWidenedLiteralType(getTypeOfSymbol(symbol)) - : unknownType; - getSymbolDisplayBuilder().buildTypeDisplay(type, writer, enclosingDeclaration, flags); - } - function writeReturnTypeOfSignatureDeclaration(signatureDeclaration, enclosingDeclaration, flags, writer) { - var signature = getSignatureFromDeclaration(signatureDeclaration); - getSymbolDisplayBuilder().buildTypeDisplay(getReturnTypeOfSignature(signature), writer, enclosingDeclaration, flags); - } - function writeTypeOfExpression(expr, enclosingDeclaration, flags, writer) { - var type = getWidenedType(getTypeOfExpression(expr)); - getSymbolDisplayBuilder().buildTypeDisplay(type, writer, enclosingDeclaration, flags); - } - function writeBaseConstructorTypeOfClass(node, enclosingDeclaration, flags, writer) { - var classType = getDeclaredTypeOfSymbol(getSymbolOfNode(node)); - resolveBaseTypesOfClass(classType); - var baseType = classType.resolvedBaseTypes.length ? classType.resolvedBaseTypes[0] : unknownType; - getSymbolDisplayBuilder().buildTypeDisplay(baseType, writer, enclosingDeclaration, flags); - } - function hasGlobalName(name) { - return !!globals[name]; - } - function getReferencedValueSymbol(reference, startInDeclarationContainer) { - var resolvedSymbol = getNodeLinks(reference).resolvedSymbol; - if (resolvedSymbol) { - return resolvedSymbol; + function checkInheritedPropertiesAreIdentical(type, typeNode) { + var baseTypes = getBaseTypes(type); + if (baseTypes.length < 2) { + return true; } - var location = reference; - if (startInDeclarationContainer) { - // When resolving the name of a declaration as a value, we need to start resolution - // at a point outside of the declaration. - var parent_13 = reference.parent; - if (ts.isDeclaration(parent_13) && reference === parent_13.name) { - location = getDeclarationContainer(parent_13); + var seen = ts.createMap(); + ts.forEach(resolveDeclaredMembers(type).declaredProperties, function (p) { seen[p.name] = { prop: p, containingType: type }; }); + var ok = true; + for (var _i = 0, baseTypes_2 = baseTypes; _i < baseTypes_2.length; _i++) { + var base = baseTypes_2[_i]; + var properties = getPropertiesOfObjectType(getTypeWithThisArgument(base, type.thisType)); + for (var _a = 0, properties_5 = properties; _a < properties_5.length; _a++) { + var prop = properties_5[_a]; + var existing = seen[prop.name]; + if (!existing) { + seen[prop.name] = { prop: prop, containingType: base }; + } + else { + var isInheritedProperty = existing.containingType !== type; + if (isInheritedProperty && !isPropertyIdenticalTo(existing.prop, prop)) { + ok = false; + var typeName1 = typeToString(existing.containingType); + var typeName2 = typeToString(base); + var errorInfo = ts.chainDiagnosticMessages(undefined, ts.Diagnostics.Named_property_0_of_types_1_and_2_are_not_identical, symbolToString(prop), typeName1, typeName2); + errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Interface_0_cannot_simultaneously_extend_types_1_and_2, typeToString(type), typeName1, typeName2); + diagnostics.add(ts.createDiagnosticForNodeFromMessageChain(typeNode, errorInfo)); + } + } } } - return resolveName(location, reference.text, 107455 /* Value */ | 1048576 /* ExportValue */ | 8388608 /* Alias */, /*nodeNotFoundMessage*/ undefined, /*nameArg*/ undefined); + return ok; } - function getReferencedValueDeclaration(reference) { - if (!ts.isGeneratedIdentifier(reference)) { - reference = ts.getParseTreeNode(reference, ts.isIdentifier); - if (reference) { - var symbol = getReferencedValueSymbol(reference); - if (symbol) { - return getExportSymbolOfValueSymbolIfExported(symbol).valueDeclaration; + function checkInterfaceDeclaration(node) { + // Grammar checking + checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarInterfaceDeclaration(node); + checkTypeParameters(node.typeParameters); + if (produceDiagnostics) { + checkTypeNameIsReserved(node.name, ts.Diagnostics.Interface_name_cannot_be_0); + checkExportsOnMergedDeclarations(node); + var symbol = getSymbolOfNode(node); + checkTypeParameterListsIdentical(node, symbol); + // Only check this symbol once + var firstInterfaceDecl = ts.getDeclarationOfKind(symbol, 222 /* InterfaceDeclaration */); + if (node === firstInterfaceDecl) { + var type = getDeclaredTypeOfSymbol(symbol); + var typeWithThis = getTypeWithThisArgument(type); + // run subsequent checks only if first set succeeded + if (checkInheritedPropertiesAreIdentical(type, node.name)) { + for (var _i = 0, _a = getBaseTypes(type); _i < _a.length; _i++) { + var baseType = _a[_i]; + checkTypeAssignableTo(typeWithThis, getTypeWithThisArgument(baseType, type.thisType), node.name, ts.Diagnostics.Interface_0_incorrectly_extends_interface_1); + } + checkIndexConstraints(type); } } + checkObjectTypeForDuplicateDeclarations(node); } - return undefined; - } - function isLiteralConstDeclaration(node) { - if (ts.isConst(node)) { - var type = getTypeOfSymbol(getSymbolOfNode(node)); - return !!(type.flags & 96 /* StringOrNumberLiteral */ && type.flags & 16777216 /* FreshLiteral */); + ts.forEach(ts.getInterfaceBaseTypeNodes(node), function (heritageElement) { + if (!ts.isEntityNameExpression(heritageElement.expression)) { + error(heritageElement.expression, ts.Diagnostics.An_interface_can_only_extend_an_identifier_Slashqualified_name_with_optional_type_arguments); + } + checkTypeReferenceNode(heritageElement); + }); + ts.forEach(node.members, checkSourceElement); + if (produceDiagnostics) { + checkTypeForDuplicateIndexSignatures(node); + registerForUnusedIdentifiersCheck(node); } - return false; } - function writeLiteralConstValue(node, writer) { - var type = getTypeOfSymbol(getSymbolOfNode(node)); - writer.writeStringLiteral(literalTypeToString(type)); + function checkTypeAliasDeclaration(node) { + // Grammar checking + checkGrammarDecorators(node) || checkGrammarModifiers(node); + checkTypeNameIsReserved(node.name, ts.Diagnostics.Type_alias_name_cannot_be_0); + checkSourceElement(node.type); } - function createResolver() { - // this variable and functions that use it are deliberately moved here from the outer scope - // to avoid scope pollution - var resolvedTypeReferenceDirectives = host.getResolvedTypeReferenceDirectives(); - var fileToDirective; - if (resolvedTypeReferenceDirectives) { - // populate reverse mapping: file path -> type reference directive that was resolved to this file - fileToDirective = ts.createFileMap(); - for (var key in resolvedTypeReferenceDirectives) { - var resolvedDirective = resolvedTypeReferenceDirectives[key]; - if (!resolvedDirective) { - continue; + function computeEnumMemberValues(node) { + var nodeLinks = getNodeLinks(node); + if (!(nodeLinks.flags & 16384 /* EnumValuesComputed */)) { + var enumSymbol = getSymbolOfNode(node); + var enumType = getDeclaredTypeOfSymbol(enumSymbol); + var autoValue = 0; // set to undefined when enum member is non-constant + var ambient = ts.isInAmbientContext(node); + var enumIsConst = ts.isConst(node); + for (var _i = 0, _a = node.members; _i < _a.length; _i++) { + var member = _a[_i]; + if (isComputedNonLiteralName(member.name)) { + error(member.name, ts.Diagnostics.Computed_property_names_are_not_allowed_in_enums); } - var file = host.getSourceFile(resolvedDirective.resolvedFileName); - fileToDirective.set(file.path, key); - } - } - return { - getReferencedExportContainer: getReferencedExportContainer, - getReferencedImportDeclaration: getReferencedImportDeclaration, - getReferencedDeclarationWithCollidingName: getReferencedDeclarationWithCollidingName, - isDeclarationWithCollidingName: isDeclarationWithCollidingName, - isValueAliasDeclaration: isValueAliasDeclaration, - hasGlobalName: hasGlobalName, - isReferencedAliasDeclaration: isReferencedAliasDeclaration, - getNodeCheckFlags: getNodeCheckFlags, - isTopLevelValueImportEqualsWithEntityName: isTopLevelValueImportEqualsWithEntityName, - isDeclarationVisible: isDeclarationVisible, - isImplementationOfOverload: isImplementationOfOverload, - writeTypeOfDeclaration: writeTypeOfDeclaration, - writeReturnTypeOfSignatureDeclaration: writeReturnTypeOfSignatureDeclaration, - writeTypeOfExpression: writeTypeOfExpression, - writeBaseConstructorTypeOfClass: writeBaseConstructorTypeOfClass, - isSymbolAccessible: isSymbolAccessible, - isEntityNameVisible: isEntityNameVisible, - getConstantValue: getConstantValue, - collectLinkedAliases: collectLinkedAliases, - getReferencedValueDeclaration: getReferencedValueDeclaration, - getTypeReferenceSerializationKind: getTypeReferenceSerializationKind, - isOptionalParameter: isOptionalParameter, - moduleExportsSomeValue: moduleExportsSomeValue, - isArgumentsLocalBinding: isArgumentsLocalBinding, - getExternalModuleFileFromDeclaration: getExternalModuleFileFromDeclaration, - getTypeReferenceDirectivesForEntityName: getTypeReferenceDirectivesForEntityName, - getTypeReferenceDirectivesForSymbol: getTypeReferenceDirectivesForSymbol, - isLiteralConstDeclaration: isLiteralConstDeclaration, - writeLiteralConstValue: writeLiteralConstValue - }; - // defined here to avoid outer scope pollution - function getTypeReferenceDirectivesForEntityName(node) { - // program does not have any files with type reference directives - bail out - if (!fileToDirective) { - return undefined; - } - // property access can only be used as values - // qualified names can only be used as types\namespaces - // identifiers are treated as values only if they appear in type queries - var meaning = (node.kind === 172 /* PropertyAccessExpression */) || (node.kind === 69 /* Identifier */ && isInTypeQuery(node)) - ? 107455 /* Value */ | 1048576 /* ExportValue */ - : 793064 /* Type */ | 1920 /* Namespace */; - var symbol = resolveEntityName(node, meaning, /*ignoreErrors*/ true); - return symbol && symbol !== unknownSymbol ? getTypeReferenceDirectivesForSymbol(symbol, meaning) : undefined; - } - // defined here to avoid outer scope pollution - function getTypeReferenceDirectivesForSymbol(symbol, meaning) { - // program does not have any files with type reference directives - bail out - if (!fileToDirective) { - return undefined; - } - if (!isSymbolFromTypeDeclarationFile(symbol)) { - return undefined; - } - // check what declarations in the symbol can contribute to the target meaning - var typeReferenceDirectives; - for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { - var decl = _a[_i]; - // check meaning of the local symbol to see if declaration needs to be analyzed further - if (decl.symbol && decl.symbol.flags & meaning) { - var file = ts.getSourceFileOfNode(decl); - var typeReferenceDirective = fileToDirective.get(file.path); - if (typeReferenceDirective) { - (typeReferenceDirectives || (typeReferenceDirectives = [])).push(typeReferenceDirective); + else { + var text = getTextOfPropertyName(member.name); + if (isNumericLiteralName(text) && !isInfinityOrNaNString(text)) { + error(member.name, ts.Diagnostics.An_enum_member_cannot_have_a_numeric_name); } } - } - return typeReferenceDirectives; - } - function isSymbolFromTypeDeclarationFile(symbol) { - // bail out if symbol does not have associated declarations (i.e. this is transient symbol created for property in binding pattern) - if (!symbol.declarations) { - return false; - } - // walk the parent chain for symbols to make sure that top level parent symbol is in the global scope - // external modules cannot define or contribute to type declaration files - var current = symbol; - while (true) { - var parent_14 = getParentOfSymbol(current); - if (parent_14) { - current = parent_14; + var previousEnumMemberIsNonConstant = autoValue === undefined; + var initializer = member.initializer; + if (initializer) { + autoValue = computeConstantValueForEnumMemberInitializer(initializer, enumType, enumIsConst, ambient); } - else { - break; + else if (ambient && !enumIsConst) { + // In ambient enum declarations that specify no const modifier, enum member declarations + // that omit a value are considered computed members (as opposed to having auto-incremented values assigned). + autoValue = undefined; } - } - if (current.valueDeclaration && current.valueDeclaration.kind === 256 /* SourceFile */ && current.flags & 512 /* ValueModule */) { - return false; - } - // check that at least one declaration of top level symbol originates from type declaration file - for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { - var decl = _a[_i]; - var file = ts.getSourceFileOfNode(decl); - if (fileToDirective.contains(file.path)) { - return true; + else if (previousEnumMemberIsNonConstant) { + // If the member declaration specifies no value, the member is considered a constant enum member. + // If the member is the first member in the enum declaration, it is assigned the value zero. + // Otherwise, it is assigned the value of the immediately preceding member plus one, + // and an error occurs if the immediately preceding member is not a constant enum member + error(member.name, ts.Diagnostics.Enum_member_must_have_initializer); + } + if (autoValue !== undefined) { + getNodeLinks(member).enumMemberValue = autoValue; + autoValue++; } } - return false; - } - } - function getExternalModuleFileFromDeclaration(declaration) { - var specifier = ts.getExternalModuleName(declaration); - var moduleSymbol = resolveExternalModuleNameWorker(specifier, specifier, /*moduleNotFoundError*/ undefined); - if (!moduleSymbol) { - return undefined; - } - return ts.getDeclarationOfKind(moduleSymbol, 256 /* SourceFile */); - } - function initializeTypeChecker() { - // Bind all source files and propagate errors - for (var _i = 0, _a = host.getSourceFiles(); _i < _a.length; _i++) { - var file = _a[_i]; - ts.bindSourceFile(file, compilerOptions); + nodeLinks.flags |= 16384 /* EnumValuesComputed */; } - // Initialize global symbol table - var augmentations; - var requestedExternalEmitHelpers = 0; - var firstFileRequestingExternalHelpers; - for (var _b = 0, _c = host.getSourceFiles(); _b < _c.length; _b++) { - var file = _c[_b]; - if (!ts.isExternalOrCommonJsModule(file)) { - mergeSymbolTable(globals, file.locals); - } - if (file.patternAmbientModules && file.patternAmbientModules.length) { - patternAmbientModules = ts.concatenate(patternAmbientModules, file.patternAmbientModules); - } - if (file.moduleAugmentations.length) { - (augmentations || (augmentations = [])).push(file.moduleAugmentations); - } - if (file.symbol && file.symbol.globalExports) { - // Merge in UMD exports with first-in-wins semantics (see #9771) - var source = file.symbol.globalExports; - for (var id in source) { - if (!(id in globals)) { - globals[id] = source[id]; + function computeConstantValueForEnumMemberInitializer(initializer, enumType, enumIsConst, ambient) { + // Controls if error should be reported after evaluation of constant value is completed + // Can be false if another more precise error was already reported during evaluation. + var reportError = true; + var value = evalConstant(initializer); + if (reportError) { + if (value === undefined) { + if (enumIsConst) { + error(initializer, ts.Diagnostics.In_const_enum_declarations_member_initializer_must_be_constant_expression); + } + else if (ambient) { + error(initializer, ts.Diagnostics.In_ambient_enum_declarations_member_initializer_must_be_constant_expression); + } + else { + // Only here do we need to check that the initializer is assignable to the enum type. + checkTypeAssignableTo(checkExpression(initializer), enumType, initializer, /*headMessage*/ undefined); } } - } - if ((compilerOptions.isolatedModules || ts.isExternalModule(file)) && !file.isDeclarationFile) { - var fileRequestedExternalEmitHelpers = file.flags & 31744 /* EmitHelperFlags */; - if (fileRequestedExternalEmitHelpers) { - requestedExternalEmitHelpers |= fileRequestedExternalEmitHelpers; - if (firstFileRequestingExternalHelpers === undefined) { - firstFileRequestingExternalHelpers = file; + else if (enumIsConst) { + if (isNaN(value)) { + error(initializer, ts.Diagnostics.const_enum_member_initializer_was_evaluated_to_disallowed_value_NaN); + } + else if (!isFinite(value)) { + error(initializer, ts.Diagnostics.const_enum_member_initializer_was_evaluated_to_a_non_finite_value); } } } - } - if (augmentations) { - // merge module augmentations. - // this needs to be done after global symbol table is initialized to make sure that all ambient modules are indexed - for (var _d = 0, augmentations_1 = augmentations; _d < augmentations_1.length; _d++) { - var list = augmentations_1[_d]; - for (var _e = 0, list_1 = list; _e < list_1.length; _e++) { - var augmentation = list_1[_e]; - mergeModuleAugmentation(augmentation); + return value; + function evalConstant(e) { + switch (e.kind) { + case 185 /* PrefixUnaryExpression */: + var value_1 = evalConstant(e.operand); + if (value_1 === undefined) { + return undefined; + } + switch (e.operator) { + case 35 /* PlusToken */: return value_1; + case 36 /* MinusToken */: return -value_1; + case 50 /* TildeToken */: return ~value_1; + } + return undefined; + case 187 /* BinaryExpression */: + var left = evalConstant(e.left); + if (left === undefined) { + return undefined; + } + var right = evalConstant(e.right); + if (right === undefined) { + return undefined; + } + switch (e.operatorToken.kind) { + case 47 /* BarToken */: return left | right; + case 46 /* AmpersandToken */: return left & right; + case 44 /* GreaterThanGreaterThanToken */: return left >> right; + case 45 /* GreaterThanGreaterThanGreaterThanToken */: return left >>> right; + case 43 /* LessThanLessThanToken */: return left << right; + case 48 /* CaretToken */: return left ^ right; + case 37 /* AsteriskToken */: return left * right; + case 39 /* SlashToken */: return left / right; + case 35 /* PlusToken */: return left + right; + case 36 /* MinusToken */: return left - right; + case 40 /* PercentToken */: return left % right; + } + return undefined; + case 8 /* NumericLiteral */: + return +e.text; + case 178 /* ParenthesizedExpression */: + return evalConstant(e.expression); + case 69 /* Identifier */: + case 173 /* ElementAccessExpression */: + case 172 /* PropertyAccessExpression */: + var member = initializer.parent; + var currentType = getTypeOfSymbol(getSymbolOfNode(member.parent)); + var enumType_1; + var propertyName = void 0; + if (e.kind === 69 /* Identifier */) { + // unqualified names can refer to member that reside in different declaration of the enum so just doing name resolution won't work. + // instead pick current enum type and later try to fetch member from the type + enumType_1 = currentType; + propertyName = e.text; + } + else { + var expression = void 0; + if (e.kind === 173 /* ElementAccessExpression */) { + if (e.argumentExpression === undefined || + e.argumentExpression.kind !== 9 /* StringLiteral */) { + return undefined; + } + expression = e.expression; + propertyName = e.argumentExpression.text; + } + else { + expression = e.expression; + propertyName = e.name.text; + } + // expression part in ElementAccess\PropertyAccess should be either identifier or dottedName + var current = expression; + while (current) { + if (current.kind === 69 /* Identifier */) { + break; + } + else if (current.kind === 172 /* PropertyAccessExpression */) { + current = current.expression; + } + else { + return undefined; + } + } + enumType_1 = checkExpression(expression); + // allow references to constant members of other enums + if (!(enumType_1.symbol && (enumType_1.symbol.flags & 384 /* Enum */))) { + return undefined; + } + } + if (propertyName === undefined) { + return undefined; + } + var property = getPropertyOfObjectType(enumType_1, propertyName); + if (!property || !(property.flags & 8 /* EnumMember */)) { + return undefined; + } + var propertyDecl = property.valueDeclaration; + // self references are illegal + if (member === propertyDecl) { + return undefined; + } + // illegal case: forward reference + if (!isBlockScopedNameDeclaredBeforeUse(propertyDecl, member)) { + reportError = false; + error(e, ts.Diagnostics.A_member_initializer_in_a_enum_declaration_cannot_reference_members_declared_after_it_including_members_defined_in_other_enums); + return undefined; + } + return getNodeLinks(propertyDecl).enumMemberValue; } } } - // Setup global builtins - addToSymbolTable(globals, builtinGlobals, ts.Diagnostics.Declaration_name_conflicts_with_built_in_global_identifier_0); - getSymbolLinks(undefinedSymbol).type = undefinedWideningType; - getSymbolLinks(argumentsSymbol).type = getGlobalType("IArguments"); - getSymbolLinks(unknownSymbol).type = unknownType; - // Initialize special types - globalArrayType = getGlobalType("Array", /*arity*/ 1); - globalObjectType = getGlobalType("Object"); - globalFunctionType = getGlobalType("Function"); - globalStringType = getGlobalType("String"); - globalNumberType = getGlobalType("Number"); - globalBooleanType = getGlobalType("Boolean"); - globalRegExpType = getGlobalType("RegExp"); - jsxElementType = getExportedTypeFromNamespace("JSX", JsxNames.Element); - getGlobalClassDecoratorType = ts.memoize(function () { return getGlobalType("ClassDecorator"); }); - getGlobalPropertyDecoratorType = ts.memoize(function () { return getGlobalType("PropertyDecorator"); }); - getGlobalMethodDecoratorType = ts.memoize(function () { return getGlobalType("MethodDecorator"); }); - getGlobalParameterDecoratorType = ts.memoize(function () { return getGlobalType("ParameterDecorator"); }); - getGlobalTypedPropertyDescriptorType = ts.memoize(function () { return getGlobalType("TypedPropertyDescriptor", /*arity*/ 1); }); - getGlobalESSymbolConstructorSymbol = ts.memoize(function () { return getGlobalValueSymbol("Symbol"); }); - getGlobalPromiseType = ts.memoize(function () { return getGlobalType("Promise", /*arity*/ 1); }); - tryGetGlobalPromiseType = ts.memoize(function () { return getGlobalSymbol("Promise", 793064 /* Type */, /*diagnostic*/ undefined) && getGlobalPromiseType(); }); - getGlobalPromiseLikeType = ts.memoize(function () { return getGlobalType("PromiseLike", /*arity*/ 1); }); - getInstantiatedGlobalPromiseLikeType = ts.memoize(createInstantiatedPromiseLikeType); - getGlobalPromiseConstructorSymbol = ts.memoize(function () { return getGlobalValueSymbol("Promise"); }); - tryGetGlobalPromiseConstructorSymbol = ts.memoize(function () { return getGlobalSymbol("Promise", 107455 /* Value */, /*diagnostic*/ undefined) && getGlobalPromiseConstructorSymbol(); }); - getGlobalPromiseConstructorLikeType = ts.memoize(function () { return getGlobalType("PromiseConstructorLike"); }); - getGlobalThenableType = ts.memoize(createThenableType); - getGlobalTemplateStringsArrayType = ts.memoize(function () { return getGlobalType("TemplateStringsArray"); }); - if (languageVersion >= 2 /* ES6 */) { - getGlobalESSymbolType = ts.memoize(function () { return getGlobalType("Symbol"); }); - getGlobalIterableType = ts.memoize(function () { return getGlobalType("Iterable", /*arity*/ 1); }); - getGlobalIteratorType = ts.memoize(function () { return getGlobalType("Iterator", /*arity*/ 1); }); - getGlobalIterableIteratorType = ts.memoize(function () { return getGlobalType("IterableIterator", /*arity*/ 1); }); + } + function checkEnumDeclaration(node) { + if (!produceDiagnostics) { + return; } - else { - getGlobalESSymbolType = ts.memoize(function () { return emptyObjectType; }); - getGlobalIterableType = ts.memoize(function () { return emptyGenericType; }); - getGlobalIteratorType = ts.memoize(function () { return emptyGenericType; }); - getGlobalIterableIteratorType = ts.memoize(function () { return emptyGenericType; }); + // Grammar checking + checkGrammarDecorators(node) || checkGrammarModifiers(node); + checkTypeNameIsReserved(node.name, ts.Diagnostics.Enum_name_cannot_be_0); + checkCollisionWithCapturedThisVariable(node, node.name); + checkCollisionWithRequireExportsInGeneratedCode(node, node.name); + checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); + checkExportsOnMergedDeclarations(node); + computeEnumMemberValues(node); + var enumIsConst = ts.isConst(node); + if (compilerOptions.isolatedModules && enumIsConst && ts.isInAmbientContext(node)) { + error(node.name, ts.Diagnostics.Ambient_const_enums_are_not_allowed_when_the_isolatedModules_flag_is_provided); } - anyArrayType = createArrayType(anyType); - var symbol = getGlobalSymbol("ReadonlyArray", 793064 /* Type */, /*diagnostic*/ undefined); - globalReadonlyArrayType = symbol && getTypeOfGlobalSymbol(symbol, /*arity*/ 1); - anyReadonlyArrayType = globalReadonlyArrayType ? createTypeFromGenericGlobalType(globalReadonlyArrayType, [anyType]) : anyArrayType; - // If we have specified that we are importing helpers, we should report global - // errors if we cannot resolve the helpers external module, or if it does not have - // the necessary helpers exported. - if (compilerOptions.importHelpers && firstFileRequestingExternalHelpers) { - // Find the first reference to the helpers module. - var helpersModule = resolveExternalModule(firstFileRequestingExternalHelpers, ts.externalHelpersModuleNameText, ts.Diagnostics.Cannot_find_module_0, - /*errorNode*/ undefined); - // If we found the module, report errors if it does not have the necessary exports. - if (helpersModule) { - var exports = helpersModule.exports; - if (requestedExternalEmitHelpers & 1024 /* HasClassExtends */ && languageVersion < 2 /* ES6 */) { - verifyHelperSymbol(exports, "__extends", 107455 /* Value */); - } - if (requestedExternalEmitHelpers & 16384 /* HasJsxSpreadAttributes */ && compilerOptions.jsx !== 1 /* Preserve */) { - verifyHelperSymbol(exports, "__assign", 107455 /* Value */); - } - if (requestedExternalEmitHelpers & 2048 /* HasDecorators */) { - verifyHelperSymbol(exports, "__decorate", 107455 /* Value */); - if (compilerOptions.emitDecoratorMetadata) { - verifyHelperSymbol(exports, "__metadata", 107455 /* Value */); + // Spec 2014 - Section 9.3: + // It isn't possible for one enum declaration to continue the automatic numbering sequence of another, + // and when an enum type has multiple declarations, only one declaration is permitted to omit a value + // for the first member. + // + // Only perform this check once per symbol + var enumSymbol = getSymbolOfNode(node); + var firstDeclaration = ts.getDeclarationOfKind(enumSymbol, node.kind); + if (node === firstDeclaration) { + if (enumSymbol.declarations.length > 1) { + // check that const is placed\omitted on all enum declarations + ts.forEach(enumSymbol.declarations, function (decl) { + if (ts.isConstEnumDeclaration(decl) !== enumIsConst) { + error(decl.name, ts.Diagnostics.Enum_declarations_must_all_be_const_or_non_const); } + }); + } + var seenEnumMissingInitialInitializer_1 = false; + ts.forEach(enumSymbol.declarations, function (declaration) { + // return true if we hit a violation of the rule, false otherwise + if (declaration.kind !== 224 /* EnumDeclaration */) { + return false; } - if (requestedExternalEmitHelpers & 4096 /* HasParamDecorators */) { - verifyHelperSymbol(exports, "__param", 107455 /* Value */); + var enumDeclaration = declaration; + if (!enumDeclaration.members.length) { + return false; } - if (requestedExternalEmitHelpers & 8192 /* HasAsyncFunctions */) { - verifyHelperSymbol(exports, "__awaiter", 107455 /* Value */); - if (languageVersion < 2 /* ES6 */) { - verifyHelperSymbol(exports, "__generator", 107455 /* Value */); + var firstEnumMember = enumDeclaration.members[0]; + if (!firstEnumMember.initializer) { + if (seenEnumMissingInitialInitializer_1) { + error(firstEnumMember.name, ts.Diagnostics.In_an_enum_with_multiple_declarations_only_one_declaration_can_omit_an_initializer_for_its_first_enum_element); + } + else { + seenEnumMissingInitialInitializer_1 = true; } } - } + }); } } - function verifyHelperSymbol(symbols, name, meaning) { - var symbol = getSymbol(symbols, ts.escapeIdentifier(name), meaning); - if (!symbol) { - error(/*location*/ undefined, ts.Diagnostics.Module_0_has_no_exported_member_1, ts.externalHelpersModuleNameText, name); + function getFirstNonAmbientClassOrFunctionDeclaration(symbol) { + var declarations = symbol.declarations; + for (var _i = 0, declarations_5 = declarations; _i < declarations_5.length; _i++) { + var declaration = declarations_5[_i]; + if ((declaration.kind === 221 /* ClassDeclaration */ || + (declaration.kind === 220 /* FunctionDeclaration */ && ts.nodeIsPresent(declaration.body))) && + !ts.isInAmbientContext(declaration)) { + return declaration; + } } + return undefined; } - function createInstantiatedPromiseLikeType() { - var promiseLikeType = getGlobalPromiseLikeType(); - if (promiseLikeType !== emptyGenericType) { - return createTypeReference(promiseLikeType, [anyType]); + function inSameLexicalScope(node1, node2) { + var container1 = ts.getEnclosingBlockScopeContainer(node1); + var container2 = ts.getEnclosingBlockScopeContainer(node2); + if (isGlobalSourceFile(container1)) { + return isGlobalSourceFile(container2); } - return emptyObjectType; - } - function createThenableType() { - // build the thenable type that is used to verify against a non-promise "thenable" operand to `await`. - var thenPropertySymbol = createSymbol(67108864 /* Transient */ | 4 /* Property */, "then"); - getSymbolLinks(thenPropertySymbol).type = globalFunctionType; - var thenableType = createObjectType(2097152 /* Anonymous */); - thenableType.properties = [thenPropertySymbol]; - thenableType.members = createSymbolTable(thenableType.properties); - thenableType.callSignatures = []; - thenableType.constructSignatures = []; - return thenableType; - } - // GRAMMAR CHECKING - function checkGrammarDecorators(node) { - if (!node.decorators) { + else if (isGlobalSourceFile(container2)) { return false; } - if (!ts.nodeCanBeDecorated(node)) { - if (node.kind === 147 /* MethodDeclaration */ && !ts.nodeIsPresent(node.body)) { - return grammarErrorOnFirstToken(node, ts.Diagnostics.A_decorator_can_only_decorate_a_method_implementation_not_an_overload); - } - else { - return grammarErrorOnFirstToken(node, ts.Diagnostics.Decorators_are_not_valid_here); - } - } - else if (node.kind === 149 /* GetAccessor */ || node.kind === 150 /* SetAccessor */) { - var accessors = ts.getAllAccessorDeclarations(node.parent.members, node); - if (accessors.firstAccessor.decorators && node === accessors.secondAccessor) { - return grammarErrorOnFirstToken(node, ts.Diagnostics.Decorators_cannot_be_applied_to_multiple_get_Slashset_accessors_of_the_same_name); - } + else { + return container1 === container2; } - return false; } - function checkGrammarModifiers(node) { - var quickResult = reportObviousModifierErrors(node); - if (quickResult !== undefined) { - return quickResult; - } - var lastStatic, lastPrivate, lastProtected, lastDeclare, lastAsync, lastReadonly; - var flags = 0 /* None */; - for (var _i = 0, _a = node.modifiers; _i < _a.length; _i++) { - var modifier = _a[_i]; - if (modifier.kind !== 128 /* ReadonlyKeyword */) { - if (node.kind === 144 /* PropertySignature */ || node.kind === 146 /* MethodSignature */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_type_member, ts.tokenToString(modifier.kind)); - } - if (node.kind === 153 /* IndexSignature */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_an_index_signature, ts.tokenToString(modifier.kind)); + function checkModuleDeclaration(node) { + if (produceDiagnostics) { + // Grammar checking + var isGlobalAugmentation = ts.isGlobalScopeAugmentation(node); + var inAmbientContext = ts.isInAmbientContext(node); + if (isGlobalAugmentation && !inAmbientContext) { + error(node.name, ts.Diagnostics.Augmentations_for_the_global_scope_should_have_declare_modifier_unless_they_appear_in_already_ambient_context); + } + var isAmbientExternalModule = ts.isAmbientModule(node); + var contextErrorMessage = isAmbientExternalModule + ? ts.Diagnostics.An_ambient_module_declaration_is_only_allowed_at_the_top_level_in_a_file + : ts.Diagnostics.A_namespace_declaration_is_only_allowed_in_a_namespace_or_module; + if (checkGrammarModuleElementContext(node, contextErrorMessage)) { + // If we hit a module declaration in an illegal context, just bail out to avoid cascading errors. + return; + } + if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node)) { + if (!inAmbientContext && node.name.kind === 9 /* StringLiteral */) { + grammarErrorOnNode(node.name, ts.Diagnostics.Only_ambient_modules_can_use_quoted_names); } } - switch (modifier.kind) { - case 74 /* ConstKeyword */: - if (node.kind !== 224 /* EnumDeclaration */ && node.parent.kind === 221 /* ClassDeclaration */) { - return grammarErrorOnNode(node, ts.Diagnostics.A_class_member_cannot_have_the_0_keyword, ts.tokenToString(74 /* ConstKeyword */)); - } - break; - case 112 /* PublicKeyword */: - case 111 /* ProtectedKeyword */: - case 110 /* PrivateKeyword */: - var text = visibilityToString(ts.modifierToFlag(modifier.kind)); - if (modifier.kind === 111 /* ProtectedKeyword */) { - lastProtected = modifier; - } - else if (modifier.kind === 110 /* PrivateKeyword */) { - lastPrivate = modifier; - } - if (flags & 28 /* AccessibilityModifier */) { - return grammarErrorOnNode(modifier, ts.Diagnostics.Accessibility_modifier_already_seen); - } - else if (flags & 32 /* Static */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, text, "static"); - } - else if (flags & 64 /* Readonly */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, text, "readonly"); - } - else if (flags & 256 /* Async */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, text, "async"); + if (ts.isIdentifier(node.name)) { + checkCollisionWithCapturedThisVariable(node, node.name); + checkCollisionWithRequireExportsInGeneratedCode(node, node.name); + checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); + } + checkExportsOnMergedDeclarations(node); + var symbol = getSymbolOfNode(node); + // The following checks only apply on a non-ambient instantiated module declaration. + if (symbol.flags & 512 /* ValueModule */ + && symbol.declarations.length > 1 + && !inAmbientContext + && ts.isInstantiatedModule(node, compilerOptions.preserveConstEnums || compilerOptions.isolatedModules)) { + var firstNonAmbientClassOrFunc = getFirstNonAmbientClassOrFunctionDeclaration(symbol); + if (firstNonAmbientClassOrFunc) { + if (ts.getSourceFileOfNode(node) !== ts.getSourceFileOfNode(firstNonAmbientClassOrFunc)) { + error(node.name, ts.Diagnostics.A_namespace_declaration_cannot_be_in_a_different_file_from_a_class_or_function_with_which_it_is_merged); } - else if (node.parent.kind === 226 /* ModuleBlock */ || node.parent.kind === 256 /* SourceFile */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_module_or_namespace_element, text); + else if (node.pos < firstNonAmbientClassOrFunc.pos) { + error(node.name, ts.Diagnostics.A_namespace_declaration_cannot_be_located_prior_to_a_class_or_function_with_which_it_is_merged); } - else if (flags & 128 /* Abstract */) { - if (modifier.kind === 110 /* PrivateKeyword */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_be_used_with_1_modifier, text, "abstract"); - } - else { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, text, "abstract"); + } + // if the module merges with a class declaration in the same lexical scope, + // we need to track this to ensure the correct emit. + var mergedClass = ts.getDeclarationOfKind(symbol, 221 /* ClassDeclaration */); + if (mergedClass && + inSameLexicalScope(node, mergedClass)) { + getNodeLinks(node).flags |= 32768 /* LexicalModuleMergesWithClass */; + } + } + if (isAmbientExternalModule) { + if (ts.isExternalModuleAugmentation(node)) { + // body of the augmentation should be checked for consistency only if augmentation was applied to its target (either global scope or module) + // otherwise we'll be swamped in cascading errors. + // We can detect if augmentation was applied using following rules: + // - augmentation for a global scope is always applied + // - augmentation for some external module is applied if symbol for augmentation is merged (it was combined with target module). + var checkBody = isGlobalAugmentation || (getSymbolOfNode(node).flags & 33554432 /* Merged */); + if (checkBody && node.body) { + // body of ambient external module is always a module block + for (var _i = 0, _a = node.body.statements; _i < _a.length; _i++) { + var statement = _a[_i]; + checkModuleAugmentationElement(statement, isGlobalAugmentation); } } - flags |= ts.modifierToFlag(modifier.kind); - break; - case 113 /* StaticKeyword */: - if (flags & 32 /* Static */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_already_seen, "static"); - } - else if (flags & 64 /* Readonly */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, "static", "readonly"); + } + else if (isGlobalSourceFile(node.parent)) { + if (isGlobalAugmentation) { + error(node.name, ts.Diagnostics.Augmentations_for_the_global_scope_can_only_be_directly_nested_in_external_modules_or_ambient_module_declarations); } - else if (flags & 256 /* Async */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, "static", "async"); + else if (ts.isExternalModuleNameRelative(node.name.text)) { + error(node.name, ts.Diagnostics.Ambient_module_declaration_cannot_specify_relative_module_name); } - else if (node.parent.kind === 226 /* ModuleBlock */ || node.parent.kind === 256 /* SourceFile */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_module_or_namespace_element, "static"); + } + else { + if (isGlobalAugmentation) { + error(node.name, ts.Diagnostics.Augmentations_for_the_global_scope_can_only_be_directly_nested_in_external_modules_or_ambient_module_declarations); } - else if (node.kind === 142 /* Parameter */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_parameter, "static"); + else { + // Node is not an augmentation and is not located on the script level. + // This means that this is declaration of ambient module that is located in other module or namespace which is prohibited. + error(node.name, ts.Diagnostics.Ambient_modules_cannot_be_nested_in_other_modules_or_namespaces); } - else if (flags & 128 /* Abstract */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "static", "abstract"); + } + } + } + if (node.body) { + checkSourceElement(node.body); + if (!ts.isGlobalScopeAugmentation(node)) { + registerForUnusedIdentifiersCheck(node); + } + } + } + function checkModuleAugmentationElement(node, isGlobalAugmentation) { + switch (node.kind) { + case 200 /* VariableStatement */: + // error each individual name in variable statement instead of marking the entire variable statement + for (var _i = 0, _a = node.declarationList.declarations; _i < _a.length; _i++) { + var decl = _a[_i]; + checkModuleAugmentationElement(decl, isGlobalAugmentation); + } + break; + case 235 /* ExportAssignment */: + case 236 /* ExportDeclaration */: + grammarErrorOnFirstToken(node, ts.Diagnostics.Exports_and_export_assignments_are_not_permitted_in_module_augmentations); + break; + case 229 /* ImportEqualsDeclaration */: + case 230 /* ImportDeclaration */: + grammarErrorOnFirstToken(node, ts.Diagnostics.Imports_are_not_permitted_in_module_augmentations_Consider_moving_them_to_the_enclosing_external_module); + break; + case 169 /* BindingElement */: + case 218 /* VariableDeclaration */: + var name_22 = node.name; + if (ts.isBindingPattern(name_22)) { + for (var _b = 0, _c = name_22.elements; _b < _c.length; _b++) { + var el = _c[_b]; + // mark individual names in binding pattern + checkModuleAugmentationElement(el, isGlobalAugmentation); } - flags |= 32 /* Static */; - lastStatic = modifier; break; - case 128 /* ReadonlyKeyword */: - if (flags & 64 /* Readonly */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_already_seen, "readonly"); - } - else if (node.kind !== 145 /* PropertyDeclaration */ && node.kind !== 144 /* PropertySignature */ && node.kind !== 153 /* IndexSignature */ && node.kind !== 142 /* Parameter */) { - // If node.kind === SyntaxKind.Parameter, checkParameter report an error if it's not a parameter property. - return grammarErrorOnNode(modifier, ts.Diagnostics.readonly_modifier_can_only_appear_on_a_property_declaration_or_index_signature); - } - flags |= 64 /* Readonly */; - lastReadonly = modifier; - break; - case 82 /* ExportKeyword */: - if (flags & 1 /* Export */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_already_seen, "export"); - } - else if (flags & 2 /* Ambient */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, "export", "declare"); - } - else if (flags & 128 /* Abstract */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, "export", "abstract"); - } - else if (flags & 256 /* Async */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, "export", "async"); - } - else if (node.parent.kind === 221 /* ClassDeclaration */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_class_element, "export"); - } - else if (node.kind === 142 /* Parameter */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_parameter, "export"); - } - flags |= 1 /* Export */; - break; - case 122 /* DeclareKeyword */: - if (flags & 2 /* Ambient */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_already_seen, "declare"); - } - else if (flags & 256 /* Async */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_be_used_in_an_ambient_context, "async"); - } - else if (node.parent.kind === 221 /* ClassDeclaration */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_class_element, "declare"); - } - else if (node.kind === 142 /* Parameter */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_parameter, "declare"); - } - else if (ts.isInAmbientContext(node.parent) && node.parent.kind === 226 /* ModuleBlock */) { - return grammarErrorOnNode(modifier, ts.Diagnostics.A_declare_modifier_cannot_be_used_in_an_already_ambient_context); - } - flags |= 2 /* Ambient */; - lastDeclare = modifier; - break; - case 115 /* AbstractKeyword */: - if (flags & 128 /* Abstract */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_already_seen, "abstract"); - } - if (node.kind !== 221 /* ClassDeclaration */) { - if (node.kind !== 147 /* MethodDeclaration */ && - node.kind !== 145 /* PropertyDeclaration */ && - node.kind !== 149 /* GetAccessor */ && - node.kind !== 150 /* SetAccessor */) { - return grammarErrorOnNode(modifier, ts.Diagnostics.abstract_modifier_can_only_appear_on_a_class_method_or_property_declaration); - } - if (!(node.parent.kind === 221 /* ClassDeclaration */ && ts.getModifierFlags(node.parent) & 128 /* Abstract */)) { - return grammarErrorOnNode(modifier, ts.Diagnostics.Abstract_methods_can_only_appear_within_an_abstract_class); - } - if (flags & 32 /* Static */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "static", "abstract"); - } - if (flags & 8 /* Private */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "private", "abstract"); - } - } - flags |= 128 /* Abstract */; - break; - case 118 /* AsyncKeyword */: - if (flags & 256 /* Async */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_already_seen, "async"); - } - else if (flags & 2 /* Ambient */ || ts.isInAmbientContext(node.parent)) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_be_used_in_an_ambient_context, "async"); - } - else if (node.kind === 142 /* Parameter */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_parameter, "async"); + } + // fallthrough + case 221 /* ClassDeclaration */: + case 224 /* EnumDeclaration */: + case 220 /* FunctionDeclaration */: + case 222 /* InterfaceDeclaration */: + case 225 /* ModuleDeclaration */: + case 223 /* TypeAliasDeclaration */: + if (isGlobalAugmentation) { + return; + } + var symbol = getSymbolOfNode(node); + if (symbol) { + // module augmentations cannot introduce new names on the top level scope of the module + // this is done it two steps + // 1. quick check - if symbol for node is not merged - this is local symbol to this augmentation - report error + // 2. main check - report error if value declaration of the parent symbol is module augmentation) + var reportError = !(symbol.flags & 33554432 /* Merged */); + if (!reportError) { + // symbol should not originate in augmentation + reportError = ts.isExternalModuleAugmentation(symbol.parent.declarations[0]); } - flags |= 256 /* Async */; - lastAsync = modifier; - break; - } + } + break; } - if (node.kind === 148 /* Constructor */) { - if (flags & 32 /* Static */) { - return grammarErrorOnNode(lastStatic, ts.Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "static"); - } - if (flags & 128 /* Abstract */) { - return grammarErrorOnNode(lastStatic, ts.Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "abstract"); - } - else if (flags & 256 /* Async */) { - return grammarErrorOnNode(lastAsync, ts.Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "async"); - } - else if (flags & 64 /* Readonly */) { - return grammarErrorOnNode(lastReadonly, ts.Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "readonly"); - } - return; + } + function getFirstIdentifier(node) { + switch (node.kind) { + case 69 /* Identifier */: + return node; + case 139 /* QualifiedName */: + do { + node = node.left; + } while (node.kind !== 69 /* Identifier */); + return node; + case 172 /* PropertyAccessExpression */: + do { + node = node.expression; + } while (node.kind !== 69 /* Identifier */); + return node; } - else if ((node.kind === 230 /* ImportDeclaration */ || node.kind === 229 /* ImportEqualsDeclaration */) && flags & 2 /* Ambient */) { - return grammarErrorOnNode(lastDeclare, ts.Diagnostics.A_0_modifier_cannot_be_used_with_an_import_declaration, "declare"); + } + function checkExternalImportOrExportDeclaration(node) { + var moduleName = ts.getExternalModuleName(node); + if (!ts.nodeIsMissing(moduleName) && moduleName.kind !== 9 /* StringLiteral */) { + error(moduleName, ts.Diagnostics.String_literal_expected); + return false; } - else if (node.kind === 142 /* Parameter */ && (flags & 92 /* ParameterPropertyModifier */) && ts.isBindingPattern(node.name)) { - return grammarErrorOnNode(node, ts.Diagnostics.A_parameter_property_may_not_be_declared_using_a_binding_pattern); + var inAmbientExternalModule = node.parent.kind === 226 /* ModuleBlock */ && ts.isAmbientModule(node.parent.parent); + if (node.parent.kind !== 256 /* SourceFile */ && !inAmbientExternalModule) { + error(moduleName, node.kind === 236 /* ExportDeclaration */ ? + ts.Diagnostics.Export_declarations_are_not_permitted_in_a_namespace : + ts.Diagnostics.Import_declarations_in_a_namespace_cannot_reference_a_module); + return false; } - else if (node.kind === 142 /* Parameter */ && (flags & 92 /* ParameterPropertyModifier */) && node.dotDotDotToken) { - return grammarErrorOnNode(node, ts.Diagnostics.A_parameter_property_cannot_be_declared_using_a_rest_parameter); + if (inAmbientExternalModule && ts.isExternalModuleNameRelative(moduleName.text)) { + // we have already reported errors on top level imports\exports in external module augmentations in checkModuleDeclaration + // no need to do this again. + if (!isTopLevelInExternalModuleAugmentation(node)) { + // TypeScript 1.0 spec (April 2013): 12.1.6 + // An ExternalImportDeclaration in an AmbientExternalModuleDeclaration may reference + // other external modules only through top - level external module names. + // Relative external module names are not permitted. + error(node, ts.Diagnostics.Import_or_export_declaration_in_an_ambient_module_declaration_cannot_reference_module_through_relative_module_name); + return false; + } } - if (flags & 256 /* Async */) { - return checkGrammarAsyncModifier(node, lastAsync); + return true; + } + function checkAliasSymbol(node) { + var symbol = getSymbolOfNode(node); + var target = resolveAlias(symbol); + if (target !== unknownSymbol) { + // For external modules symbol represent local symbol for an alias. + // This local symbol will merge any other local declarations (excluding other aliases) + // and symbol.flags will contains combined representation for all merged declaration. + // Based on symbol.flags we can compute a set of excluded meanings (meaning that resolved alias should not have, + // otherwise it will conflict with some local declaration). Note that in addition to normal flags we include matching SymbolFlags.Export* + // in order to prevent collisions with declarations that were exported from the current module (they still contribute to local names). + var excludedMeanings = (symbol.flags & (107455 /* Value */ | 1048576 /* ExportValue */) ? 107455 /* Value */ : 0) | + (symbol.flags & 793064 /* Type */ ? 793064 /* Type */ : 0) | + (symbol.flags & 1920 /* Namespace */ ? 1920 /* Namespace */ : 0); + if (target.flags & excludedMeanings) { + var message = node.kind === 238 /* ExportSpecifier */ ? + ts.Diagnostics.Export_declaration_conflicts_with_exported_declaration_of_0 : + ts.Diagnostics.Import_declaration_conflicts_with_local_declaration_of_0; + error(node, message, symbolToString(symbol)); + } } } - /** - * true | false: Early return this value from checkGrammarModifiers. - * undefined: Need to do full checking on the modifiers. - */ - function reportObviousModifierErrors(node) { - return !node.modifiers - ? false - : shouldReportBadModifier(node) - ? grammarErrorOnFirstToken(node, ts.Diagnostics.Modifiers_cannot_appear_here) - : undefined; + function checkImportBinding(node) { + checkCollisionWithCapturedThisVariable(node, node.name); + checkCollisionWithRequireExportsInGeneratedCode(node, node.name); + checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); + checkAliasSymbol(node); } - function shouldReportBadModifier(node) { - switch (node.kind) { - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - case 148 /* Constructor */: - case 145 /* PropertyDeclaration */: - case 144 /* PropertySignature */: - case 147 /* MethodDeclaration */: - case 146 /* MethodSignature */: - case 153 /* IndexSignature */: - case 225 /* ModuleDeclaration */: - case 230 /* ImportDeclaration */: - case 229 /* ImportEqualsDeclaration */: - case 236 /* ExportDeclaration */: - case 235 /* ExportAssignment */: - case 179 /* FunctionExpression */: - case 180 /* ArrowFunction */: - case 142 /* Parameter */: - return false; - default: - if (node.parent.kind === 226 /* ModuleBlock */ || node.parent.kind === 256 /* SourceFile */) { - return false; + function checkImportDeclaration(node) { + if (checkGrammarModuleElementContext(node, ts.Diagnostics.An_import_declaration_can_only_be_used_in_a_namespace_or_module)) { + // If we hit an import declaration in an illegal context, just bail out to avoid cascading errors. + return; + } + if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node) && ts.getModifierFlags(node) !== 0) { + grammarErrorOnFirstToken(node, ts.Diagnostics.An_import_declaration_cannot_have_modifiers); + } + if (checkExternalImportOrExportDeclaration(node)) { + var importClause = node.importClause; + if (importClause) { + if (importClause.name) { + checkImportBinding(importClause); } - switch (node.kind) { - case 220 /* FunctionDeclaration */: - return nodeHasAnyModifiersExcept(node, 118 /* AsyncKeyword */); - case 221 /* ClassDeclaration */: - return nodeHasAnyModifiersExcept(node, 115 /* AbstractKeyword */); - case 222 /* InterfaceDeclaration */: - case 200 /* VariableStatement */: - case 223 /* TypeAliasDeclaration */: - return true; - case 224 /* EnumDeclaration */: - return nodeHasAnyModifiersExcept(node, 74 /* ConstKeyword */); - default: - ts.Debug.fail(); - return false; + if (importClause.namedBindings) { + if (importClause.namedBindings.kind === 232 /* NamespaceImport */) { + checkImportBinding(importClause.namedBindings); + } + else { + ts.forEach(importClause.namedBindings.elements, checkImportBinding); + } } + } } } - function nodeHasAnyModifiersExcept(node, allowedModifier) { - return node.modifiers.length > 1 || node.modifiers[0].kind !== allowedModifier; - } - function checkGrammarAsyncModifier(node, asyncModifier) { - switch (node.kind) { - case 147 /* MethodDeclaration */: - case 220 /* FunctionDeclaration */: - case 179 /* FunctionExpression */: - case 180 /* ArrowFunction */: - if (!node.asteriskToken) { - return false; - } - break; + function checkImportEqualsDeclaration(node) { + if (checkGrammarModuleElementContext(node, ts.Diagnostics.An_import_declaration_can_only_be_used_in_a_namespace_or_module)) { + // If we hit an import declaration in an illegal context, just bail out to avoid cascading errors. + return; } - return grammarErrorOnNode(asyncModifier, ts.Diagnostics._0_modifier_cannot_be_used_here, "async"); - } - function checkGrammarForDisallowedTrailingComma(list) { - if (list && list.hasTrailingComma) { - var start = list.end - ",".length; - var end = list.end; - var sourceFile = ts.getSourceFileOfNode(list[0]); - return grammarErrorAtPos(sourceFile, start, end - start, ts.Diagnostics.Trailing_comma_not_allowed); + checkGrammarDecorators(node) || checkGrammarModifiers(node); + if (ts.isInternalModuleImportEqualsDeclaration(node) || checkExternalImportOrExportDeclaration(node)) { + checkImportBinding(node); + if (ts.getModifierFlags(node) & 1 /* Export */) { + markExportAsReferenced(node); + } + if (ts.isInternalModuleImportEqualsDeclaration(node)) { + var target = resolveAlias(getSymbolOfNode(node)); + if (target !== unknownSymbol) { + if (target.flags & 107455 /* Value */) { + // Target is a value symbol, check that it is not hidden by a local declaration with the same name + var moduleName = getFirstIdentifier(node.moduleReference); + if (!(resolveEntityName(moduleName, 107455 /* Value */ | 1920 /* Namespace */).flags & 1920 /* Namespace */)) { + error(moduleName, ts.Diagnostics.Module_0_is_hidden_by_a_local_declaration_with_the_same_name, ts.declarationNameToString(moduleName)); + } + } + if (target.flags & 793064 /* Type */) { + checkTypeNameIsReserved(node.name, ts.Diagnostics.Import_name_cannot_be_0); + } + } + } + else { + if (modulekind === ts.ModuleKind.ES6 && !ts.isInAmbientContext(node)) { + // Import equals declaration is deprecated in es6 or above + grammarErrorOnNode(node, ts.Diagnostics.Import_assignment_cannot_be_used_when_targeting_ECMAScript_2015_modules_Consider_using_import_Asterisk_as_ns_from_mod_import_a_from_mod_import_d_from_mod_or_another_module_format_instead); + } + } } } - function checkGrammarTypeParameterList(node, typeParameters, file) { - if (checkGrammarForDisallowedTrailingComma(typeParameters)) { - return true; + function checkExportDeclaration(node) { + if (checkGrammarModuleElementContext(node, ts.Diagnostics.An_export_declaration_can_only_be_used_in_a_module)) { + // If we hit an export in an illegal context, just bail out to avoid cascading errors. + return; } - if (typeParameters && typeParameters.length === 0) { - var start = typeParameters.pos - "<".length; - var end = ts.skipTrivia(file.text, typeParameters.end) + ">".length; - return grammarErrorAtPos(file, start, end - start, ts.Diagnostics.Type_parameter_list_cannot_be_empty); + if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node) && ts.getModifierFlags(node) !== 0) { + grammarErrorOnFirstToken(node, ts.Diagnostics.An_export_declaration_cannot_have_modifiers); } - } - function checkGrammarParameterList(parameters) { - var seenOptionalParameter = false; - var parameterCount = parameters.length; - for (var i = 0; i < parameterCount; i++) { - var parameter = parameters[i]; - if (parameter.dotDotDotToken) { - if (i !== (parameterCount - 1)) { - return grammarErrorOnNode(parameter.dotDotDotToken, ts.Diagnostics.A_rest_parameter_must_be_last_in_a_parameter_list); - } - if (ts.isBindingPattern(parameter.name)) { - return grammarErrorOnNode(parameter.name, ts.Diagnostics.A_rest_element_cannot_contain_a_binding_pattern); - } - if (parameter.questionToken) { - return grammarErrorOnNode(parameter.questionToken, ts.Diagnostics.A_rest_parameter_cannot_be_optional); - } - if (parameter.initializer) { - return grammarErrorOnNode(parameter.name, ts.Diagnostics.A_rest_parameter_cannot_have_an_initializer); + if (!node.moduleSpecifier || checkExternalImportOrExportDeclaration(node)) { + if (node.exportClause) { + // export { x, y } + // export { x, y } from "foo" + ts.forEach(node.exportClause.elements, checkExportSpecifier); + var inAmbientExternalModule = node.parent.kind === 226 /* ModuleBlock */ && ts.isAmbientModule(node.parent.parent); + if (node.parent.kind !== 256 /* SourceFile */ && !inAmbientExternalModule) { + error(node, ts.Diagnostics.Export_declarations_are_not_permitted_in_a_namespace); } } - else if (parameter.questionToken) { - seenOptionalParameter = true; - if (parameter.initializer) { - return grammarErrorOnNode(parameter.name, ts.Diagnostics.Parameter_cannot_have_question_mark_and_initializer); + else { + // export * from "foo" + var moduleSymbol = resolveExternalModuleName(node, node.moduleSpecifier); + if (moduleSymbol && hasExportAssignmentSymbol(moduleSymbol)) { + error(node.moduleSpecifier, ts.Diagnostics.Module_0_uses_export_and_cannot_be_used_with_export_Asterisk, symbolToString(moduleSymbol)); } } - else if (seenOptionalParameter && !parameter.initializer) { - return grammarErrorOnNode(parameter.name, ts.Diagnostics.A_required_parameter_cannot_follow_an_optional_parameter); - } } } - function checkGrammarFunctionLikeDeclaration(node) { - // Prevent cascading error by short-circuit - var file = ts.getSourceFileOfNode(node); - return checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarTypeParameterList(node, node.typeParameters, file) || - checkGrammarParameterList(node.parameters) || checkGrammarArrowFunction(node, file); - } - function checkGrammarArrowFunction(node, file) { - if (node.kind === 180 /* ArrowFunction */) { - var arrowFunction = node; - var startLine = ts.getLineAndCharacterOfPosition(file, arrowFunction.equalsGreaterThanToken.pos).line; - var endLine = ts.getLineAndCharacterOfPosition(file, arrowFunction.equalsGreaterThanToken.end).line; - if (startLine !== endLine) { - return grammarErrorOnNode(arrowFunction.equalsGreaterThanToken, ts.Diagnostics.Line_terminator_not_permitted_before_arrow); - } + function checkGrammarModuleElementContext(node, errorMessage) { + var isInAppropriateContext = node.parent.kind === 256 /* SourceFile */ || node.parent.kind === 226 /* ModuleBlock */ || node.parent.kind === 225 /* ModuleDeclaration */; + if (!isInAppropriateContext) { + grammarErrorOnFirstToken(node, errorMessage); } - return false; + return !isInAppropriateContext; } - function checkGrammarIndexSignatureParameters(node) { - var parameter = node.parameters[0]; - if (node.parameters.length !== 1) { - if (parameter) { - return grammarErrorOnNode(parameter.name, ts.Diagnostics.An_index_signature_must_have_exactly_one_parameter); + function checkExportSpecifier(node) { + checkAliasSymbol(node); + if (!node.parent.parent.moduleSpecifier) { + var exportedName = node.propertyName || node.name; + // find immediate value referenced by exported name (SymbolFlags.Alias is set so we don't chase down aliases) + var symbol = resolveName(exportedName, exportedName.text, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */ | 8388608 /* Alias */, + /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined); + if (symbol && (symbol === undefinedSymbol || isGlobalSourceFile(getDeclarationContainer(symbol.declarations[0])))) { + error(exportedName, ts.Diagnostics.Cannot_export_0_Only_local_declarations_can_be_exported_from_a_module, exportedName.text); } else { - return grammarErrorOnNode(node, ts.Diagnostics.An_index_signature_must_have_exactly_one_parameter); + markExportAsReferenced(node); } } - if (parameter.dotDotDotToken) { - return grammarErrorOnNode(parameter.dotDotDotToken, ts.Diagnostics.An_index_signature_cannot_have_a_rest_parameter); - } - if (ts.getModifierFlags(parameter) !== 0) { - return grammarErrorOnNode(parameter.name, ts.Diagnostics.An_index_signature_parameter_cannot_have_an_accessibility_modifier); + } + function checkExportAssignment(node) { + if (checkGrammarModuleElementContext(node, ts.Diagnostics.An_export_assignment_can_only_be_used_in_a_module)) { + // If we hit an export assignment in an illegal context, just bail out to avoid cascading errors. + return; } - if (parameter.questionToken) { - return grammarErrorOnNode(parameter.questionToken, ts.Diagnostics.An_index_signature_parameter_cannot_have_a_question_mark); + var container = node.parent.kind === 256 /* SourceFile */ ? node.parent : node.parent.parent; + if (container.kind === 225 /* ModuleDeclaration */ && !ts.isAmbientModule(container)) { + error(node, ts.Diagnostics.An_export_assignment_cannot_be_used_in_a_namespace); + return; } - if (parameter.initializer) { - return grammarErrorOnNode(parameter.name, ts.Diagnostics.An_index_signature_parameter_cannot_have_an_initializer); + // Grammar checking + if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node) && ts.getModifierFlags(node) !== 0) { + grammarErrorOnFirstToken(node, ts.Diagnostics.An_export_assignment_cannot_have_modifiers); } - if (!parameter.type) { - return grammarErrorOnNode(parameter.name, ts.Diagnostics.An_index_signature_parameter_must_have_a_type_annotation); + if (node.expression.kind === 69 /* Identifier */) { + markExportAsReferenced(node); } - if (parameter.type.kind !== 132 /* StringKeyword */ && parameter.type.kind !== 130 /* NumberKeyword */) { - return grammarErrorOnNode(parameter.name, ts.Diagnostics.An_index_signature_parameter_type_must_be_string_or_number); + else { + checkExpressionCached(node.expression); } - if (!node.type) { - return grammarErrorOnNode(node, ts.Diagnostics.An_index_signature_must_have_a_type_annotation); + checkExternalModuleExports(container); + if (node.isExportEquals && !ts.isInAmbientContext(node)) { + if (modulekind === ts.ModuleKind.ES6) { + // export assignment is not supported in es6 modules + grammarErrorOnNode(node, ts.Diagnostics.Export_assignment_cannot_be_used_when_targeting_ECMAScript_2015_modules_Consider_using_export_default_or_another_module_format_instead); + } + else if (modulekind === ts.ModuleKind.System) { + // system modules does not support export assignment + grammarErrorOnNode(node, ts.Diagnostics.Export_assignment_is_not_supported_when_module_flag_is_system); + } } } - function checkGrammarIndexSignature(node) { - // Prevent cascading error by short-circuit - return checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarIndexSignatureParameters(node); - } - function checkGrammarForAtLeastOneTypeArgument(node, typeArguments) { - if (typeArguments && typeArguments.length === 0) { - var sourceFile = ts.getSourceFileOfNode(node); - var start = typeArguments.pos - "<".length; - var end = ts.skipTrivia(sourceFile.text, typeArguments.end) + ">".length; - return grammarErrorAtPos(sourceFile, start, end - start, ts.Diagnostics.Type_argument_list_cannot_be_empty); + function hasExportedMembers(moduleSymbol) { + for (var id in moduleSymbol.exports) { + if (id !== "export=") { + return true; + } } + return false; } - function checkGrammarTypeArguments(node, typeArguments) { - return checkGrammarForDisallowedTrailingComma(typeArguments) || - checkGrammarForAtLeastOneTypeArgument(node, typeArguments); - } - function checkGrammarForOmittedArgument(node, args) { - if (args) { - var sourceFile = ts.getSourceFileOfNode(node); - for (var _i = 0, args_2 = args; _i < args_2.length; _i++) { - var arg = args_2[_i]; - if (arg.kind === 193 /* OmittedExpression */) { - return grammarErrorAtPos(sourceFile, arg.pos, 0, ts.Diagnostics.Argument_expression_expected); + function checkExternalModuleExports(node) { + var moduleSymbol = getSymbolOfNode(node); + var links = getSymbolLinks(moduleSymbol); + if (!links.exportsChecked) { + var exportEqualsSymbol = moduleSymbol.exports["export="]; + if (exportEqualsSymbol && hasExportedMembers(moduleSymbol)) { + var declaration = getDeclarationOfAliasSymbol(exportEqualsSymbol) || exportEqualsSymbol.valueDeclaration; + if (!isTopLevelInExternalModuleAugmentation(declaration)) { + error(declaration, ts.Diagnostics.An_export_assignment_cannot_be_used_in_a_module_with_other_exported_elements); } } + // Checks for export * conflicts + var exports = getExportsOfModule(moduleSymbol); + for (var id in exports) { + if (id === "__export") { + continue; + } + var _a = exports[id], declarations = _a.declarations, flags = _a.flags; + // ECMA262: 15.2.1.1 It is a Syntax Error if the ExportedNames of ModuleItemList contains any duplicate entries. + // (TS Exceptions: namespaces, function overloads, enums, and interfaces) + if (flags & (1920 /* Namespace */ | 64 /* Interface */ | 384 /* Enum */)) { + continue; + } + var exportedDeclarationsCount = ts.countWhere(declarations, isNotOverload); + if (flags & 524288 /* TypeAlias */ && exportedDeclarationsCount <= 2) { + // it is legal to merge type alias with other values + // so count should be either 1 (just type alias) or 2 (type alias + merged value) + continue; + } + if (exportedDeclarationsCount > 1) { + for (var _i = 0, declarations_6 = declarations; _i < declarations_6.length; _i++) { + var declaration = declarations_6[_i]; + if (isNotOverload(declaration)) { + diagnostics.add(ts.createDiagnosticForNode(declaration, ts.Diagnostics.Cannot_redeclare_exported_variable_0, id)); + } + } + } + } + links.exportsChecked = true; } - } - function checkGrammarArguments(node, args) { - return checkGrammarForOmittedArgument(node, args); - } - function checkGrammarHeritageClause(node) { - var types = node.types; - if (checkGrammarForDisallowedTrailingComma(types)) { - return true; - } - if (types && types.length === 0) { - var listType = ts.tokenToString(node.token); - var sourceFile = ts.getSourceFileOfNode(node); - return grammarErrorAtPos(sourceFile, types.pos, 0, ts.Diagnostics._0_list_cannot_be_empty, listType); + function isNotOverload(declaration) { + return (declaration.kind !== 220 /* FunctionDeclaration */ && declaration.kind !== 147 /* MethodDeclaration */) || + !!declaration.body; } } - function checkGrammarClassDeclarationHeritageClauses(node) { - var seenExtendsClause = false; - var seenImplementsClause = false; - if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node) && node.heritageClauses) { - for (var _i = 0, _a = node.heritageClauses; _i < _a.length; _i++) { - var heritageClause = _a[_i]; - if (heritageClause.token === 83 /* ExtendsKeyword */) { - if (seenExtendsClause) { - return grammarErrorOnFirstToken(heritageClause, ts.Diagnostics.extends_clause_already_seen); - } - if (seenImplementsClause) { - return grammarErrorOnFirstToken(heritageClause, ts.Diagnostics.extends_clause_must_precede_implements_clause); - } - if (heritageClause.types.length > 1) { - return grammarErrorOnFirstToken(heritageClause.types[1], ts.Diagnostics.Classes_can_only_extend_a_single_class); - } - seenExtendsClause = true; - } - else { - ts.Debug.assert(heritageClause.token === 106 /* ImplementsKeyword */); - if (seenImplementsClause) { - return grammarErrorOnFirstToken(heritageClause, ts.Diagnostics.implements_clause_already_seen); - } - seenImplementsClause = true; - } - // Grammar checking heritageClause inside class declaration - checkGrammarHeritageClause(heritageClause); - } + function checkSourceElement(node) { + if (!node) { + return; } - } - function checkGrammarInterfaceDeclaration(node) { - var seenExtendsClause = false; - if (node.heritageClauses) { - for (var _i = 0, _a = node.heritageClauses; _i < _a.length; _i++) { - var heritageClause = _a[_i]; - if (heritageClause.token === 83 /* ExtendsKeyword */) { - if (seenExtendsClause) { - return grammarErrorOnFirstToken(heritageClause, ts.Diagnostics.extends_clause_already_seen); - } - seenExtendsClause = true; - } - else { - ts.Debug.assert(heritageClause.token === 106 /* ImplementsKeyword */); - return grammarErrorOnFirstToken(heritageClause, ts.Diagnostics.Interface_declaration_cannot_have_implements_clause); - } - // Grammar checking heritageClause inside class declaration - checkGrammarHeritageClause(heritageClause); + var kind = node.kind; + if (cancellationToken) { + // Only bother checking on a few construct kinds. We don't want to be excessively + // hitting the cancellation token on every node we check. + switch (kind) { + case 225 /* ModuleDeclaration */: + case 221 /* ClassDeclaration */: + case 222 /* InterfaceDeclaration */: + case 220 /* FunctionDeclaration */: + cancellationToken.throwIfCancellationRequested(); } } - return false; - } - function checkGrammarComputedPropertyName(node) { - // If node is not a computedPropertyName, just skip the grammar checking - if (node.kind !== 140 /* ComputedPropertyName */) { - return false; + switch (kind) { + case 141 /* TypeParameter */: + return checkTypeParameter(node); + case 142 /* Parameter */: + return checkParameter(node); + case 145 /* PropertyDeclaration */: + case 144 /* PropertySignature */: + return checkPropertyDeclaration(node); + case 156 /* FunctionType */: + case 157 /* ConstructorType */: + case 151 /* CallSignature */: + case 152 /* ConstructSignature */: + return checkSignatureDeclaration(node); + case 153 /* IndexSignature */: + return checkSignatureDeclaration(node); + case 147 /* MethodDeclaration */: + case 146 /* MethodSignature */: + return checkMethodDeclaration(node); + case 148 /* Constructor */: + return checkConstructorDeclaration(node); + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + return checkAccessorDeclaration(node); + case 155 /* TypeReference */: + return checkTypeReferenceNode(node); + case 154 /* TypePredicate */: + return checkTypePredicate(node); + case 158 /* TypeQuery */: + return checkTypeQuery(node); + case 159 /* TypeLiteral */: + return checkTypeLiteral(node); + case 160 /* ArrayType */: + return checkArrayType(node); + case 161 /* TupleType */: + return checkTupleType(node); + case 162 /* UnionType */: + case 163 /* IntersectionType */: + return checkUnionOrIntersectionType(node); + case 164 /* ParenthesizedType */: + return checkSourceElement(node.type); + case 220 /* FunctionDeclaration */: + return checkFunctionDeclaration(node); + case 199 /* Block */: + case 226 /* ModuleBlock */: + return checkBlock(node); + case 200 /* VariableStatement */: + return checkVariableStatement(node); + case 202 /* ExpressionStatement */: + return checkExpressionStatement(node); + case 203 /* IfStatement */: + return checkIfStatement(node); + case 204 /* DoStatement */: + return checkDoStatement(node); + case 205 /* WhileStatement */: + return checkWhileStatement(node); + case 206 /* ForStatement */: + return checkForStatement(node); + case 207 /* ForInStatement */: + return checkForInStatement(node); + case 208 /* ForOfStatement */: + return checkForOfStatement(node); + case 209 /* ContinueStatement */: + case 210 /* BreakStatement */: + return checkBreakOrContinueStatement(node); + case 211 /* ReturnStatement */: + return checkReturnStatement(node); + case 212 /* WithStatement */: + return checkWithStatement(node); + case 213 /* SwitchStatement */: + return checkSwitchStatement(node); + case 214 /* LabeledStatement */: + return checkLabeledStatement(node); + case 215 /* ThrowStatement */: + return checkThrowStatement(node); + case 216 /* TryStatement */: + return checkTryStatement(node); + case 218 /* VariableDeclaration */: + return checkVariableDeclaration(node); + case 169 /* BindingElement */: + return checkBindingElement(node); + case 221 /* ClassDeclaration */: + return checkClassDeclaration(node); + case 222 /* InterfaceDeclaration */: + return checkInterfaceDeclaration(node); + case 223 /* TypeAliasDeclaration */: + return checkTypeAliasDeclaration(node); + case 224 /* EnumDeclaration */: + return checkEnumDeclaration(node); + case 225 /* ModuleDeclaration */: + return checkModuleDeclaration(node); + case 230 /* ImportDeclaration */: + return checkImportDeclaration(node); + case 229 /* ImportEqualsDeclaration */: + return checkImportEqualsDeclaration(node); + case 236 /* ExportDeclaration */: + return checkExportDeclaration(node); + case 235 /* ExportAssignment */: + return checkExportAssignment(node); + case 201 /* EmptyStatement */: + checkGrammarStatementInAmbientContext(node); + return; + case 217 /* DebuggerStatement */: + checkGrammarStatementInAmbientContext(node); + return; + case 239 /* MissingDeclaration */: + return checkMissingDeclaration(node); } - var computedPropertyName = node; - if (computedPropertyName.expression.kind === 187 /* BinaryExpression */ && computedPropertyName.expression.operatorToken.kind === 24 /* CommaToken */) { - return grammarErrorOnNode(computedPropertyName.expression, ts.Diagnostics.A_comma_expression_is_not_allowed_in_a_computed_property_name); + } + // Function and class expression bodies are checked after all statements in the enclosing body. This is + // to ensure constructs like the following are permitted: + // const foo = function () { + // const s = foo(); + // return "hello"; + // } + // Here, performing a full type check of the body of the function expression whilst in the process of + // determining the type of foo would cause foo to be given type any because of the recursive reference. + // Delaying the type check of the body ensures foo has been assigned a type. + function checkNodeDeferred(node) { + if (deferredNodes) { + deferredNodes.push(node); } } - function checkGrammarForGenerator(node) { - if (node.asteriskToken) { - ts.Debug.assert(node.kind === 220 /* FunctionDeclaration */ || - node.kind === 179 /* FunctionExpression */ || - node.kind === 147 /* MethodDeclaration */); - if (ts.isInAmbientContext(node)) { - return grammarErrorOnNode(node.asteriskToken, ts.Diagnostics.Generators_are_not_allowed_in_an_ambient_context); - } - if (!node.body) { - return grammarErrorOnNode(node.asteriskToken, ts.Diagnostics.An_overload_signature_cannot_be_declared_as_a_generator); - } - if (languageVersion < 2 /* ES6 */) { - return grammarErrorOnNode(node.asteriskToken, ts.Diagnostics.Generators_are_only_available_when_targeting_ECMAScript_2015_or_higher); + function checkDeferredNodes() { + for (var _i = 0, deferredNodes_1 = deferredNodes; _i < deferredNodes_1.length; _i++) { + var node = deferredNodes_1[_i]; + switch (node.kind) { + case 179 /* FunctionExpression */: + case 180 /* ArrowFunction */: + case 147 /* MethodDeclaration */: + case 146 /* MethodSignature */: + checkFunctionExpressionOrObjectLiteralMethodDeferred(node); + break; + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + checkAccessorDeferred(node); + break; + case 192 /* ClassExpression */: + checkClassExpressionDeferred(node); + break; } } } - function checkGrammarForInvalidQuestionMark(node, questionToken, message) { - if (questionToken) { - return grammarErrorOnNode(questionToken, message); - } + function checkSourceFile(node) { + ts.performance.mark("beforeCheck"); + checkSourceFileWorker(node); + ts.performance.mark("afterCheck"); + ts.performance.measure("Check", "beforeCheck", "afterCheck"); } - function checkGrammarObjectLiteralExpression(node, inDestructuring) { - var seen = ts.createMap(); - var Property = 1; - var GetAccessor = 2; - var SetAccessor = 4; - var GetOrSetAccessor = GetAccessor | SetAccessor; - for (var _i = 0, _a = node.properties; _i < _a.length; _i++) { - var prop = _a[_i]; - var name_24 = prop.name; - if (prop.kind === 193 /* OmittedExpression */ || - name_24.kind === 140 /* ComputedPropertyName */) { - // If the name is not a ComputedPropertyName, the grammar checking will skip it - checkGrammarComputedPropertyName(name_24); - } - if (prop.kind === 254 /* ShorthandPropertyAssignment */ && !inDestructuring && prop.objectAssignmentInitializer) { - // having objectAssignmentInitializer is only valid in ObjectAssignmentPattern - // outside of destructuring it is a syntax error - return grammarErrorOnNode(prop.equalsToken, ts.Diagnostics.can_only_be_used_in_an_object_literal_property_inside_a_destructuring_assignment); - } - // Modifiers are never allowed on properties except for 'async' on a method declaration - if (prop.modifiers) { - for (var _b = 0, _c = prop.modifiers; _b < _c.length; _b++) { - var mod = _c[_b]; - if (mod.kind !== 118 /* AsyncKeyword */ || prop.kind !== 147 /* MethodDeclaration */) { - grammarErrorOnNode(mod, ts.Diagnostics._0_modifier_cannot_be_used_here, ts.getTextOfNode(mod)); - } - } - } - // ECMA-262 11.1.5 Object Initializer - // If previous is not undefined then throw a SyntaxError exception if any of the following conditions are true - // a.This production is contained in strict code and IsDataDescriptor(previous) is true and - // IsDataDescriptor(propId.descriptor) is true. - // b.IsDataDescriptor(previous) is true and IsAccessorDescriptor(propId.descriptor) is true. - // c.IsAccessorDescriptor(previous) is true and IsDataDescriptor(propId.descriptor) is true. - // d.IsAccessorDescriptor(previous) is true and IsAccessorDescriptor(propId.descriptor) is true - // and either both previous and propId.descriptor have[[Get]] fields or both previous and propId.descriptor have[[Set]] fields - var currentKind = void 0; - if (prop.kind === 253 /* PropertyAssignment */ || prop.kind === 254 /* ShorthandPropertyAssignment */) { - // Grammar checking for computedPropertyName and shorthandPropertyAssignment - checkGrammarForInvalidQuestionMark(prop, prop.questionToken, ts.Diagnostics.An_object_member_cannot_be_declared_optional); - if (name_24.kind === 8 /* NumericLiteral */) { - checkGrammarNumericLiteral(name_24); - } - currentKind = Property; - } - else if (prop.kind === 147 /* MethodDeclaration */) { - currentKind = Property; - } - else if (prop.kind === 149 /* GetAccessor */) { - currentKind = GetAccessor; - } - else if (prop.kind === 150 /* SetAccessor */) { - currentKind = SetAccessor; + // Fully type check a source file and collect the relevant diagnostics. + function checkSourceFileWorker(node) { + var links = getNodeLinks(node); + if (!(links.flags & 1 /* TypeChecked */)) { + // If skipLibCheck is enabled, skip type checking if file is a declaration file. + // If skipDefaultLibCheck is enabled, skip type checking if file contains a + // '/// ' directive. + if (compilerOptions.skipLibCheck && node.isDeclarationFile || compilerOptions.skipDefaultLibCheck && node.hasNoDefaultLib) { + return; } - else { - ts.Debug.fail("Unexpected syntax kind:" + prop.kind); + // Grammar checking + checkGrammarSourceFile(node); + potentialThisCollisions.length = 0; + deferredNodes = []; + deferredUnusedIdentifierNodes = produceDiagnostics && noUnusedIdentifiers ? [] : undefined; + ts.forEach(node.statements, checkSourceElement); + checkDeferredNodes(); + if (ts.isExternalModule(node)) { + registerForUnusedIdentifiersCheck(node); } - var effectiveName = ts.getPropertyNameForPropertyNameNode(name_24); - if (effectiveName === undefined) { - continue; + if (!node.isDeclarationFile) { + checkUnusedIdentifiers(); } - if (!seen[effectiveName]) { - seen[effectiveName] = currentKind; + deferredNodes = undefined; + deferredUnusedIdentifierNodes = undefined; + if (ts.isExternalOrCommonJsModule(node)) { + checkExternalModuleExports(node); } - else { - var existingKind = seen[effectiveName]; - if (currentKind === Property && existingKind === Property) { - grammarErrorOnNode(name_24, ts.Diagnostics.Duplicate_identifier_0, ts.getTextOfNode(name_24)); - } - else if ((currentKind & GetOrSetAccessor) && (existingKind & GetOrSetAccessor)) { - if (existingKind !== GetOrSetAccessor && currentKind !== existingKind) { - seen[effectiveName] = currentKind | existingKind; - } - else { - return grammarErrorOnNode(name_24, ts.Diagnostics.An_object_literal_cannot_have_multiple_get_Slashset_accessors_with_the_same_name); - } - } - else { - return grammarErrorOnNode(name_24, ts.Diagnostics.An_object_literal_cannot_have_property_and_accessor_with_the_same_name); - } + if (potentialThisCollisions.length) { + ts.forEach(potentialThisCollisions, checkIfThisIsCapturedInEnclosingScope); + potentialThisCollisions.length = 0; } + links.flags |= 1 /* TypeChecked */; } } - function checkGrammarJsxElement(node) { - var seen = ts.createMap(); - for (var _i = 0, _a = node.attributes; _i < _a.length; _i++) { - var attr = _a[_i]; - if (attr.kind === 247 /* JsxSpreadAttribute */) { - continue; - } - var jsxAttr = attr; - var name_25 = jsxAttr.name; - if (!seen[name_25.text]) { - seen[name_25.text] = true; - } - else { - return grammarErrorOnNode(name_25, ts.Diagnostics.JSX_elements_cannot_have_multiple_attributes_with_the_same_name); - } - var initializer = jsxAttr.initializer; - if (initializer && initializer.kind === 248 /* JsxExpression */ && !initializer.expression) { - return grammarErrorOnNode(jsxAttr.initializer, ts.Diagnostics.JSX_attributes_must_only_be_assigned_a_non_empty_expression); - } + function getDiagnostics(sourceFile, ct) { + try { + // Record the cancellation token so it can be checked later on during checkSourceElement. + // Do this in a finally block so we can ensure that it gets reset back to nothing after + // this call is done. + cancellationToken = ct; + return getDiagnosticsWorker(sourceFile); + } + finally { + cancellationToken = undefined; } } - function checkGrammarForInOrForOfStatement(forInOrOfStatement) { - if (checkGrammarStatementInAmbientContext(forInOrOfStatement)) { - return true; + function getDiagnosticsWorker(sourceFile) { + throwIfNonDiagnosticsProducing(); + if (sourceFile) { + checkSourceFile(sourceFile); + return diagnostics.getDiagnostics(sourceFile.fileName); } - if (forInOrOfStatement.initializer.kind === 219 /* VariableDeclarationList */) { - var variableList = forInOrOfStatement.initializer; - if (!checkGrammarVariableDeclarationList(variableList)) { - var declarations = variableList.declarations; - // declarations.length can be zero if there is an error in variable declaration in for-of or for-in - // See http://www.ecma-international.org/ecma-262/6.0/#sec-for-in-and-for-of-statements for details - // For example: - // var let = 10; - // for (let of [1,2,3]) {} // this is invalid ES6 syntax - // for (let in [1,2,3]) {} // this is invalid ES6 syntax - // We will then want to skip on grammar checking on variableList declaration - if (!declarations.length) { - return false; - } - if (declarations.length > 1) { - var diagnostic = forInOrOfStatement.kind === 207 /* ForInStatement */ - ? ts.Diagnostics.Only_a_single_variable_declaration_is_allowed_in_a_for_in_statement - : ts.Diagnostics.Only_a_single_variable_declaration_is_allowed_in_a_for_of_statement; - return grammarErrorOnFirstToken(variableList.declarations[1], diagnostic); - } - var firstDeclaration = declarations[0]; - if (firstDeclaration.initializer) { - var diagnostic = forInOrOfStatement.kind === 207 /* ForInStatement */ - ? ts.Diagnostics.The_variable_declaration_of_a_for_in_statement_cannot_have_an_initializer - : ts.Diagnostics.The_variable_declaration_of_a_for_of_statement_cannot_have_an_initializer; - return grammarErrorOnNode(firstDeclaration.name, diagnostic); - } - if (firstDeclaration.type) { - var diagnostic = forInOrOfStatement.kind === 207 /* ForInStatement */ - ? ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_use_a_type_annotation - : ts.Diagnostics.The_left_hand_side_of_a_for_of_statement_cannot_use_a_type_annotation; - return grammarErrorOnNode(firstDeclaration, diagnostic); + ts.forEach(host.getSourceFiles(), checkSourceFile); + return diagnostics.getDiagnostics(); + } + function getGlobalDiagnostics() { + throwIfNonDiagnosticsProducing(); + return diagnostics.getGlobalDiagnostics(); + } + function throwIfNonDiagnosticsProducing() { + if (!produceDiagnostics) { + throw new Error("Trying to get diagnostics from a type checker that does not produce them."); + } + } + // Language service support + function isInsideWithStatementBody(node) { + if (node) { + while (node.parent) { + if (node.parent.kind === 212 /* WithStatement */ && node.parent.statement === node) { + return true; } + node = node.parent; } } return false; } - function checkGrammarAccessor(accessor) { - var kind = accessor.kind; - if (languageVersion < 1 /* ES5 */) { - return grammarErrorOnNode(accessor.name, ts.Diagnostics.Accessors_are_only_available_when_targeting_ECMAScript_5_and_higher); - } - else if (ts.isInAmbientContext(accessor)) { - return grammarErrorOnNode(accessor.name, ts.Diagnostics.An_accessor_cannot_be_declared_in_an_ambient_context); - } - else if (accessor.body === undefined && !(ts.getModifierFlags(accessor) & 128 /* Abstract */)) { - return grammarErrorAtPos(ts.getSourceFileOfNode(accessor), accessor.end - 1, ";".length, ts.Diagnostics._0_expected, "{"); - } - else if (accessor.typeParameters) { - return grammarErrorOnNode(accessor.name, ts.Diagnostics.An_accessor_cannot_have_type_parameters); - } - else if (!doesAccessorHaveCorrectParameterCount(accessor)) { - return grammarErrorOnNode(accessor.name, kind === 149 /* GetAccessor */ ? - ts.Diagnostics.A_get_accessor_cannot_have_parameters : - ts.Diagnostics.A_set_accessor_must_have_exactly_one_parameter); + function getSymbolsInScope(location, meaning) { + var symbols = ts.createMap(); + var memberFlags = 0 /* None */; + if (isInsideWithStatementBody(location)) { + // We cannot answer semantic questions within a with block, do not proceed any further + return []; } - else if (kind === 150 /* SetAccessor */) { - if (accessor.type) { - return grammarErrorOnNode(accessor.name, ts.Diagnostics.A_set_accessor_cannot_have_a_return_type_annotation); - } - else { - var parameter = accessor.parameters[0]; - if (parameter.dotDotDotToken) { - return grammarErrorOnNode(parameter.dotDotDotToken, ts.Diagnostics.A_set_accessor_cannot_have_rest_parameter); + populateSymbols(); + return symbolsToArray(symbols); + function populateSymbols() { + while (location) { + if (location.locals && !isGlobalSourceFile(location)) { + copySymbols(location.locals, meaning); } - else if (parameter.questionToken) { - return grammarErrorOnNode(parameter.questionToken, ts.Diagnostics.A_set_accessor_cannot_have_an_optional_parameter); + switch (location.kind) { + case 256 /* SourceFile */: + if (!ts.isExternalOrCommonJsModule(location)) { + break; + } + case 225 /* ModuleDeclaration */: + copySymbols(getSymbolOfNode(location).exports, meaning & 8914931 /* ModuleMember */); + break; + case 224 /* EnumDeclaration */: + copySymbols(getSymbolOfNode(location).exports, meaning & 8 /* EnumMember */); + break; + case 192 /* ClassExpression */: + var className = location.name; + if (className) { + copySymbol(location.symbol, meaning); + } + // fall through; this fall-through is necessary because we would like to handle + // type parameter inside class expression similar to how we handle it in classDeclaration and interface Declaration + case 221 /* ClassDeclaration */: + case 222 /* InterfaceDeclaration */: + // If we didn't come from static member of class or interface, + // add the type parameters into the symbol table + // (type parameters of classDeclaration/classExpression and interface are in member property of the symbol. + // Note: that the memberFlags come from previous iteration. + if (!(memberFlags & 32 /* Static */)) { + copySymbols(getSymbolOfNode(location).members, meaning & 793064 /* Type */); + } + break; + case 179 /* FunctionExpression */: + var funcName = location.name; + if (funcName) { + copySymbol(location.symbol, meaning); + } + break; } - else if (parameter.initializer) { - return grammarErrorOnNode(accessor.name, ts.Diagnostics.A_set_accessor_parameter_cannot_have_an_initializer); + if (ts.introducesArgumentsExoticObject(location)) { + copySymbol(argumentsSymbol, meaning); } + memberFlags = ts.getModifierFlags(location); + location = location.parent; } + copySymbols(globals, meaning); } - } - /** Does the accessor have the right number of parameters? - - A get accessor has no parameters or a single `this` parameter. - A set accessor has one parameter or a `this` parameter and one more parameter */ - function doesAccessorHaveCorrectParameterCount(accessor) { - return getAccessorThisParameter(accessor) || accessor.parameters.length === (accessor.kind === 149 /* GetAccessor */ ? 0 : 1); - } - function getAccessorThisParameter(accessor) { - if (accessor.parameters.length === (accessor.kind === 149 /* GetAccessor */ ? 1 : 2) && - accessor.parameters[0].name.kind === 69 /* Identifier */ && - accessor.parameters[0].name.originalKeywordKind === 97 /* ThisKeyword */) { - return accessor.parameters[0]; - } - } - function getFunctionLikeThisParameter(func) { - if (func.parameters.length && - func.parameters[0].name.kind === 69 /* Identifier */ && - func.parameters[0].name.originalKeywordKind === 97 /* ThisKeyword */) { - return func.parameters[0]; - } - } - function checkGrammarForNonSymbolComputedProperty(node, message) { - if (ts.isDynamicName(node)) { - return grammarErrorOnNode(node, message); + /** + * Copy the given symbol into symbol tables if the symbol has the given meaning + * and it doesn't already existed in the symbol table + * @param key a key for storing in symbol table; if undefined, use symbol.name + * @param symbol the symbol to be added into symbol table + * @param meaning meaning of symbol to filter by before adding to symbol table + */ + function copySymbol(symbol, meaning) { + if (symbol.flags & meaning) { + var id = symbol.name; + // We will copy all symbol regardless of its reserved name because + // symbolsToArray will check whether the key is a reserved name and + // it will not copy symbol with reserved name to the array + if (!symbols[id]) { + symbols[id] = symbol; + } + } } - } - function checkGrammarMethod(node) { - if (checkGrammarDisallowedModifiersOnObjectLiteralExpressionMethod(node) || - checkGrammarFunctionLikeDeclaration(node) || - checkGrammarForGenerator(node)) { - return true; + function copySymbols(source, meaning) { + if (meaning) { + for (var id in source) { + var symbol = source[id]; + copySymbol(symbol, meaning); + } + } } - if (node.parent.kind === 171 /* ObjectLiteralExpression */) { - if (checkGrammarForInvalidQuestionMark(node, node.questionToken, ts.Diagnostics.An_object_member_cannot_be_declared_optional)) { + } + function isTypeDeclarationName(name) { + return name.kind === 69 /* Identifier */ && + isTypeDeclaration(name.parent) && + name.parent.name === name; + } + function isTypeDeclaration(node) { + switch (node.kind) { + case 141 /* TypeParameter */: + case 221 /* ClassDeclaration */: + case 222 /* InterfaceDeclaration */: + case 223 /* TypeAliasDeclaration */: + case 224 /* EnumDeclaration */: return true; - } - else if (node.body === undefined) { - return grammarErrorAtPos(ts.getSourceFileOfNode(node), node.end - 1, ";".length, ts.Diagnostics._0_expected, "{"); - } } - if (ts.isClassLike(node.parent)) { - // Technically, computed properties in ambient contexts is disallowed - // for property declarations and accessors too, not just methods. - // However, property declarations disallow computed names in general, - // and accessors are not allowed in ambient contexts in general, - // so this error only really matters for methods. - if (ts.isInAmbientContext(node)) { - return checkGrammarForNonSymbolComputedProperty(node.name, ts.Diagnostics.A_computed_property_name_in_an_ambient_context_must_directly_refer_to_a_built_in_symbol); - } - else if (!node.body) { - return checkGrammarForNonSymbolComputedProperty(node.name, ts.Diagnostics.A_computed_property_name_in_a_method_overload_must_directly_refer_to_a_built_in_symbol); - } + } + // True if the given identifier is part of a type reference + function isTypeReferenceIdentifier(entityName) { + var node = entityName; + while (node.parent && node.parent.kind === 139 /* QualifiedName */) { + node = node.parent; } - else if (node.parent.kind === 222 /* InterfaceDeclaration */) { - return checkGrammarForNonSymbolComputedProperty(node.name, ts.Diagnostics.A_computed_property_name_in_an_interface_must_directly_refer_to_a_built_in_symbol); + return node.parent && (node.parent.kind === 155 /* TypeReference */ || node.parent.kind === 267 /* JSDocTypeReference */); + } + function isHeritageClauseElementIdentifier(entityName) { + var node = entityName; + while (node.parent && node.parent.kind === 172 /* PropertyAccessExpression */) { + node = node.parent; } - else if (node.parent.kind === 159 /* TypeLiteral */) { - return checkGrammarForNonSymbolComputedProperty(node.name, ts.Diagnostics.A_computed_property_name_in_a_type_literal_must_directly_refer_to_a_built_in_symbol); + return node.parent && node.parent.kind === 194 /* ExpressionWithTypeArguments */; + } + function forEachEnclosingClass(node, callback) { + var result; + while (true) { + node = ts.getContainingClass(node); + if (!node) + break; + if (result = callback(node)) + break; } + return result; } - function checkGrammarBreakOrContinueStatement(node) { - var current = node; - while (current) { - if (ts.isFunctionLike(current)) { - return grammarErrorOnNode(node, ts.Diagnostics.Jump_target_cannot_cross_function_boundary); - } - switch (current.kind) { - case 214 /* LabeledStatement */: - if (node.label && current.label.text === node.label.text) { - // found matching label - verify that label usage is correct - // continue can only target labels that are on iteration statements - var isMisplacedContinueLabel = node.kind === 209 /* ContinueStatement */ - && !ts.isIterationStatement(current.statement, /*lookInLabeledStatement*/ true); - if (isMisplacedContinueLabel) { - return grammarErrorOnNode(node, ts.Diagnostics.A_continue_statement_can_only_jump_to_a_label_of_an_enclosing_iteration_statement); - } - return false; - } - break; - case 213 /* SwitchStatement */: - if (node.kind === 210 /* BreakStatement */ && !node.label) { - // unlabeled break within switch statement - ok - return false; - } - break; - default: - if (ts.isIterationStatement(current, /*lookInLabeledStatement*/ false) && !node.label) { - // unlabeled break or continue within iteration statement - ok - return false; - } - break; - } - current = current.parent; + function isNodeWithinClass(node, classDeclaration) { + return !!forEachEnclosingClass(node, function (n) { return n === classDeclaration; }); + } + function getLeftSideOfImportEqualsOrExportAssignment(nodeOnRightSide) { + while (nodeOnRightSide.parent.kind === 139 /* QualifiedName */) { + nodeOnRightSide = nodeOnRightSide.parent; } - if (node.label) { - var message = node.kind === 210 /* BreakStatement */ - ? ts.Diagnostics.A_break_statement_can_only_jump_to_a_label_of_an_enclosing_statement - : ts.Diagnostics.A_continue_statement_can_only_jump_to_a_label_of_an_enclosing_iteration_statement; - return grammarErrorOnNode(node, message); + if (nodeOnRightSide.parent.kind === 229 /* ImportEqualsDeclaration */) { + return nodeOnRightSide.parent.moduleReference === nodeOnRightSide && nodeOnRightSide.parent; } - else { - var message = node.kind === 210 /* BreakStatement */ - ? ts.Diagnostics.A_break_statement_can_only_be_used_within_an_enclosing_iteration_or_switch_statement - : ts.Diagnostics.A_continue_statement_can_only_be_used_within_an_enclosing_iteration_statement; - return grammarErrorOnNode(node, message); + if (nodeOnRightSide.parent.kind === 235 /* ExportAssignment */) { + return nodeOnRightSide.parent.expression === nodeOnRightSide && nodeOnRightSide.parent; } + return undefined; } - function checkGrammarBindingElement(node) { - if (node.dotDotDotToken) { - var elements = node.parent.elements; - if (node !== ts.lastOrUndefined(elements)) { - return grammarErrorOnNode(node, ts.Diagnostics.A_rest_element_must_be_last_in_an_array_destructuring_pattern); + function isInRightSideOfImportOrExportAssignment(node) { + return getLeftSideOfImportEqualsOrExportAssignment(node) !== undefined; + } + function getSymbolOfEntityNameOrPropertyAccessExpression(entityName) { + if (ts.isDeclarationName(entityName)) { + return getSymbolOfNode(entityName.parent); + } + if (ts.isInJavaScriptFile(entityName) && entityName.parent.kind === 172 /* PropertyAccessExpression */) { + var specialPropertyAssignmentKind = ts.getSpecialPropertyAssignmentKind(entityName.parent.parent); + switch (specialPropertyAssignmentKind) { + case 1 /* ExportsProperty */: + case 3 /* PrototypeProperty */: + return getSymbolOfNode(entityName.parent); + case 4 /* ThisProperty */: + case 2 /* ModuleExports */: + return getSymbolOfNode(entityName.parent.parent); + default: } - if (node.name.kind === 168 /* ArrayBindingPattern */ || node.name.kind === 167 /* ObjectBindingPattern */) { - return grammarErrorOnNode(node.name, ts.Diagnostics.A_rest_element_cannot_contain_a_binding_pattern); + } + if (entityName.parent.kind === 235 /* ExportAssignment */ && ts.isEntityNameExpression(entityName)) { + return resolveEntityName(entityName, + /*all meanings*/ 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */ | 8388608 /* Alias */); + } + if (entityName.kind !== 172 /* PropertyAccessExpression */ && isInRightSideOfImportOrExportAssignment(entityName)) { + // Since we already checked for ExportAssignment, this really could only be an Import + var importEqualsDeclaration = ts.getAncestor(entityName, 229 /* ImportEqualsDeclaration */); + ts.Debug.assert(importEqualsDeclaration !== undefined); + return getSymbolOfPartOfRightHandSideOfImportEquals(entityName, importEqualsDeclaration, /*dontResolveAlias*/ true); + } + if (ts.isRightSideOfQualifiedNameOrPropertyAccess(entityName)) { + entityName = entityName.parent; + } + if (isHeritageClauseElementIdentifier(entityName)) { + var meaning = 0 /* None */; + // In an interface or class, we're definitely interested in a type. + if (entityName.parent.kind === 194 /* ExpressionWithTypeArguments */) { + meaning = 793064 /* Type */; + // In a class 'extends' clause we are also looking for a value. + if (ts.isExpressionWithTypeArgumentsInClassExtendsClause(entityName.parent)) { + meaning |= 107455 /* Value */; + } } - if (node.initializer) { - // Error on equals token which immediate precedes the initializer - return grammarErrorAtPos(ts.getSourceFileOfNode(node), node.initializer.pos - 1, 1, ts.Diagnostics.A_rest_element_cannot_have_an_initializer); + else { + meaning = 1920 /* Namespace */; } + meaning |= 8388608 /* Alias */; + return resolveEntityName(entityName, meaning); } - } - function isStringOrNumberLiteralExpression(expr) { - return expr.kind === 9 /* StringLiteral */ || expr.kind === 8 /* NumericLiteral */ || - expr.kind === 185 /* PrefixUnaryExpression */ && expr.operator === 36 /* MinusToken */ && - expr.operand.kind === 8 /* NumericLiteral */; - } - function checkGrammarVariableDeclaration(node) { - if (node.parent.parent.kind !== 207 /* ForInStatement */ && node.parent.parent.kind !== 208 /* ForOfStatement */) { - if (ts.isInAmbientContext(node)) { - if (node.initializer) { - if (ts.isConst(node) && !node.type) { - if (!isStringOrNumberLiteralExpression(node.initializer)) { - return grammarErrorOnNode(node.initializer, ts.Diagnostics.A_const_initializer_in_an_ambient_context_must_be_a_string_or_numeric_literal); - } - } - else { - // Error on equals token which immediate precedes the initializer - var equalsTokenLength = "=".length; - return grammarErrorAtPos(ts.getSourceFileOfNode(node), node.initializer.pos - equalsTokenLength, equalsTokenLength, ts.Diagnostics.Initializers_are_not_allowed_in_ambient_contexts); - } - } - if (node.initializer && !(ts.isConst(node) && isStringOrNumberLiteralExpression(node.initializer))) { - // Error on equals token which immediate precedes the initializer - var equalsTokenLength = "=".length; - return grammarErrorAtPos(ts.getSourceFileOfNode(node), node.initializer.pos - equalsTokenLength, equalsTokenLength, ts.Diagnostics.Initializers_are_not_allowed_in_ambient_contexts); + else if (ts.isPartOfExpression(entityName)) { + if (ts.nodeIsMissing(entityName)) { + // Missing entity name. + return undefined; + } + if (entityName.kind === 69 /* Identifier */) { + if (ts.isJSXTagName(entityName) && isJsxIntrinsicIdentifier(entityName)) { + return getIntrinsicTagSymbol(entityName.parent); } + return resolveEntityName(entityName, 107455 /* Value */, /*ignoreErrors*/ false, /*dontResolveAlias*/ true); } - else if (!node.initializer) { - if (ts.isBindingPattern(node.name) && !ts.isBindingPattern(node.parent)) { - return grammarErrorOnNode(node, ts.Diagnostics.A_destructuring_declaration_must_have_an_initializer); + else if (entityName.kind === 172 /* PropertyAccessExpression */) { + var symbol = getNodeLinks(entityName).resolvedSymbol; + if (!symbol) { + checkPropertyAccessExpression(entityName); } - if (ts.isConst(node)) { - return grammarErrorOnNode(node, ts.Diagnostics.const_declarations_must_be_initialized); + return getNodeLinks(entityName).resolvedSymbol; + } + else if (entityName.kind === 139 /* QualifiedName */) { + var symbol = getNodeLinks(entityName).resolvedSymbol; + if (!symbol) { + checkQualifiedName(entityName); } + return getNodeLinks(entityName).resolvedSymbol; } } - var checkLetConstNames = (ts.isLet(node) || ts.isConst(node)); - // 1. LexicalDeclaration : LetOrConst BindingList ; - // It is a Syntax Error if the BoundNames of BindingList contains "let". - // 2. ForDeclaration: ForDeclaration : LetOrConst ForBinding - // It is a Syntax Error if the BoundNames of ForDeclaration contains "let". - // It is a SyntaxError if a VariableDeclaration or VariableDeclarationNoIn occurs within strict code - // and its Identifier is eval or arguments - return checkLetConstNames && checkGrammarNameInLetOrConstDeclarations(node.name); - } - function checkGrammarNameInLetOrConstDeclarations(name) { - if (name.kind === 69 /* Identifier */) { - if (name.originalKeywordKind === 108 /* LetKeyword */) { - return grammarErrorOnNode(name, ts.Diagnostics.let_is_not_allowed_to_be_used_as_a_name_in_let_or_const_declarations); - } + else if (isTypeReferenceIdentifier(entityName)) { + var meaning = (entityName.parent.kind === 155 /* TypeReference */ || entityName.parent.kind === 267 /* JSDocTypeReference */) ? 793064 /* Type */ : 1920 /* Namespace */; + return resolveEntityName(entityName, meaning, /*ignoreErrors*/ false, /*dontResolveAlias*/ true); } - else { - var elements = name.elements; - for (var _i = 0, elements_2 = elements; _i < elements_2.length; _i++) { - var element = elements_2[_i]; - if (!ts.isOmittedExpression(element)) { - checkGrammarNameInLetOrConstDeclarations(element.name); - } - } + else if (entityName.parent.kind === 246 /* JsxAttribute */) { + return getJsxAttributePropertySymbol(entityName.parent); + } + if (entityName.parent.kind === 154 /* TypePredicate */) { + return resolveEntityName(entityName, /*meaning*/ 1 /* FunctionScopedVariable */); } + // Do we want to return undefined here? + return undefined; } - function checkGrammarVariableDeclarationList(declarationList) { - var declarations = declarationList.declarations; - if (checkGrammarForDisallowedTrailingComma(declarationList.declarations)) { - return true; + function getSymbolAtLocation(node) { + if (node.kind === 256 /* SourceFile */) { + return ts.isExternalModule(node) ? getMergedSymbol(node.symbol) : undefined; } - if (!declarationList.declarations.length) { - return grammarErrorAtPos(ts.getSourceFileOfNode(declarationList), declarations.pos, declarations.end - declarations.pos, ts.Diagnostics.Variable_declaration_list_cannot_be_empty); + if (isInsideWithStatementBody(node)) { + // We cannot answer semantic questions within a with block, do not proceed any further + return undefined; } - } - function allowLetAndConstDeclarations(parent) { - switch (parent.kind) { - case 203 /* IfStatement */: - case 204 /* DoStatement */: - case 205 /* WhileStatement */: - case 212 /* WithStatement */: - case 206 /* ForStatement */: - case 207 /* ForInStatement */: - case 208 /* ForOfStatement */: - return false; - case 214 /* LabeledStatement */: - return allowLetAndConstDeclarations(parent.parent); + if (ts.isDeclarationName(node)) { + // This is a declaration, call getSymbolOfNode + return getSymbolOfNode(node.parent); } - return true; - } - function checkGrammarForDisallowedLetOrConstStatement(node) { - if (!allowLetAndConstDeclarations(node.parent)) { - if (ts.isLet(node.declarationList)) { - return grammarErrorOnNode(node, ts.Diagnostics.let_declarations_can_only_be_declared_inside_a_block); + else if (ts.isLiteralComputedPropertyDeclarationName(node)) { + return getSymbolOfNode(node.parent.parent); + } + if (node.kind === 69 /* Identifier */) { + if (isInRightSideOfImportOrExportAssignment(node)) { + return getSymbolOfEntityNameOrPropertyAccessExpression(node); } - else if (ts.isConst(node.declarationList)) { - return grammarErrorOnNode(node, ts.Diagnostics.const_declarations_can_only_be_declared_inside_a_block); + else if (node.parent.kind === 169 /* BindingElement */ && + node.parent.parent.kind === 167 /* ObjectBindingPattern */ && + node === node.parent.propertyName) { + var typeOfPattern = getTypeOfNode(node.parent.parent); + var propertyDeclaration = typeOfPattern && getPropertyOfType(typeOfPattern, node.text); + if (propertyDeclaration) { + return propertyDeclaration; + } } } - } - function hasParseDiagnostics(sourceFile) { - return sourceFile.parseDiagnostics.length > 0; - } - function grammarErrorOnFirstToken(node, message, arg0, arg1, arg2) { - var sourceFile = ts.getSourceFileOfNode(node); - if (!hasParseDiagnostics(sourceFile)) { - var span_4 = ts.getSpanOfTokenAtPosition(sourceFile, node.pos); - diagnostics.add(ts.createFileDiagnostic(sourceFile, span_4.start, span_4.length, message, arg0, arg1, arg2)); - return true; + switch (node.kind) { + case 69 /* Identifier */: + case 172 /* PropertyAccessExpression */: + case 139 /* QualifiedName */: + return getSymbolOfEntityNameOrPropertyAccessExpression(node); + case 97 /* ThisKeyword */: + var container = ts.getThisContainer(node, /*includeArrowFunctions*/ false); + if (ts.isFunctionLike(container)) { + var sig = getSignatureFromDeclaration(container); + if (sig.thisParameter) { + return sig.thisParameter; + } + } + // fallthrough + case 95 /* SuperKeyword */: + var type = ts.isPartOfExpression(node) ? checkExpression(node) : getTypeFromTypeNode(node); + return type.symbol; + case 165 /* ThisType */: + return getTypeFromTypeNode(node).symbol; + case 121 /* ConstructorKeyword */: + // constructor keyword for an overload, should take us to the definition if it exist + var constructorDeclaration = node.parent; + if (constructorDeclaration && constructorDeclaration.kind === 148 /* Constructor */) { + return constructorDeclaration.parent.symbol; + } + return undefined; + case 9 /* StringLiteral */: + // External module name in an import declaration + if ((ts.isExternalModuleImportEqualsDeclaration(node.parent.parent) && + ts.getExternalModuleImportEqualsDeclarationExpression(node.parent.parent) === node) || + ((node.parent.kind === 230 /* ImportDeclaration */ || node.parent.kind === 236 /* ExportDeclaration */) && + node.parent.moduleSpecifier === node)) { + return resolveExternalModuleName(node, node); + } + if (ts.isInJavaScriptFile(node) && ts.isRequireCall(node.parent, /*checkArgumentIsStringLiteral*/ false)) { + return resolveExternalModuleName(node, node); + } + // Fall through + case 8 /* NumericLiteral */: + // index access + if (node.parent.kind === 173 /* ElementAccessExpression */ && node.parent.argumentExpression === node) { + var objectType = checkExpression(node.parent.expression); + if (objectType === unknownType) + return undefined; + var apparentType = getApparentType(objectType); + if (apparentType === unknownType) + return undefined; + return getPropertyOfType(apparentType, node.text); + } + break; } + return undefined; } - function grammarErrorAtPos(sourceFile, start, length, message, arg0, arg1, arg2) { - if (!hasParseDiagnostics(sourceFile)) { - diagnostics.add(ts.createFileDiagnostic(sourceFile, start, length, message, arg0, arg1, arg2)); - return true; + function getShorthandAssignmentValueSymbol(location) { + // The function returns a value symbol of an identifier in the short-hand property assignment. + // This is necessary as an identifier in short-hand property assignment can contains two meaning: + // property name and property value. + if (location && location.kind === 254 /* ShorthandPropertyAssignment */) { + return resolveEntityName(location.name, 107455 /* Value */ | 8388608 /* Alias */); } + return undefined; } - function grammarErrorOnNode(node, message, arg0, arg1, arg2) { - var sourceFile = ts.getSourceFileOfNode(node); - if (!hasParseDiagnostics(sourceFile)) { - diagnostics.add(ts.createDiagnosticForNode(node, message, arg0, arg1, arg2)); - return true; - } + /** Returns the target of an export specifier without following aliases */ + function getExportSpecifierLocalTargetSymbol(node) { + return node.parent.parent.moduleSpecifier ? + getExternalModuleMember(node.parent.parent, node) : + resolveEntityName(node.propertyName || node.name, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */ | 8388608 /* Alias */); } - function checkGrammarConstructorTypeParameters(node) { - if (node.typeParameters) { - return grammarErrorAtPos(ts.getSourceFileOfNode(node), node.typeParameters.pos, node.typeParameters.end - node.typeParameters.pos, ts.Diagnostics.Type_parameters_cannot_appear_on_a_constructor_declaration); + function getTypeOfNode(node) { + if (isInsideWithStatementBody(node)) { + // We cannot answer semantic questions within a with block, do not proceed any further + return unknownType; } - } - function checkGrammarConstructorTypeAnnotation(node) { - if (node.type) { - return grammarErrorOnNode(node.type, ts.Diagnostics.Type_annotation_cannot_appear_on_a_constructor_declaration); + if (ts.isPartOfTypeNode(node)) { + return getTypeFromTypeNode(node); } - } - function checkGrammarProperty(node) { - if (ts.isClassLike(node.parent)) { - if (checkGrammarForNonSymbolComputedProperty(node.name, ts.Diagnostics.A_computed_property_name_in_a_class_property_declaration_must_directly_refer_to_a_built_in_symbol)) { - return true; - } + if (ts.isPartOfExpression(node)) { + return getTypeOfExpression(node); } - else if (node.parent.kind === 222 /* InterfaceDeclaration */) { - if (checkGrammarForNonSymbolComputedProperty(node.name, ts.Diagnostics.A_computed_property_name_in_an_interface_must_directly_refer_to_a_built_in_symbol)) { - return true; - } - if (node.initializer) { - return grammarErrorOnNode(node.initializer, ts.Diagnostics.An_interface_property_cannot_have_an_initializer); - } + if (ts.isExpressionWithTypeArgumentsInClassExtendsClause(node)) { + // A SyntaxKind.ExpressionWithTypeArguments is considered a type node, except when it occurs in the + // extends clause of a class. We handle that case here. + return getBaseTypes(getDeclaredTypeOfSymbol(getSymbolOfNode(node.parent.parent)))[0]; } - else if (node.parent.kind === 159 /* TypeLiteral */) { - if (checkGrammarForNonSymbolComputedProperty(node.name, ts.Diagnostics.A_computed_property_name_in_a_type_literal_must_directly_refer_to_a_built_in_symbol)) { - return true; - } - if (node.initializer) { - return grammarErrorOnNode(node.initializer, ts.Diagnostics.A_type_literal_property_cannot_have_an_initializer); - } + if (isTypeDeclaration(node)) { + // In this case, we call getSymbolOfNode instead of getSymbolAtLocation because it is a declaration + var symbol = getSymbolOfNode(node); + return getDeclaredTypeOfSymbol(symbol); } - if (ts.isInAmbientContext(node) && node.initializer) { - return grammarErrorOnFirstToken(node.initializer, ts.Diagnostics.Initializers_are_not_allowed_in_ambient_contexts); + if (isTypeDeclarationName(node)) { + var symbol = getSymbolAtLocation(node); + return symbol && getDeclaredTypeOfSymbol(symbol); } - } - function checkGrammarTopLevelElementForRequiredDeclareModifier(node) { - // A declare modifier is required for any top level .d.ts declaration except export=, export default, export as namespace - // interfaces and imports categories: - // - // DeclarationElement: - // ExportAssignment - // export_opt InterfaceDeclaration - // export_opt TypeAliasDeclaration - // export_opt ImportDeclaration - // export_opt ExternalImportDeclaration - // export_opt AmbientDeclaration - // - // TODO: The spec needs to be amended to reflect this grammar. - if (node.kind === 222 /* InterfaceDeclaration */ || - node.kind === 223 /* TypeAliasDeclaration */ || - node.kind === 230 /* ImportDeclaration */ || - node.kind === 229 /* ImportEqualsDeclaration */ || - node.kind === 236 /* ExportDeclaration */ || - node.kind === 235 /* ExportAssignment */ || - node.kind === 228 /* NamespaceExportDeclaration */ || - ts.getModifierFlags(node) & (2 /* Ambient */ | 1 /* Export */ | 512 /* Default */)) { - return false; + if (ts.isDeclaration(node)) { + // In this case, we call getSymbolOfNode instead of getSymbolAtLocation because it is a declaration + var symbol = getSymbolOfNode(node); + return getTypeOfSymbol(symbol); } - return grammarErrorOnFirstToken(node, ts.Diagnostics.A_declare_modifier_is_required_for_a_top_level_declaration_in_a_d_ts_file); - } - function checkGrammarTopLevelElementsForRequiredDeclareModifier(file) { - for (var _i = 0, _a = file.statements; _i < _a.length; _i++) { - var decl = _a[_i]; - if (ts.isDeclaration(decl) || decl.kind === 200 /* VariableStatement */) { - if (checkGrammarTopLevelElementForRequiredDeclareModifier(decl)) { - return true; - } - } + if (ts.isDeclarationName(node)) { + var symbol = getSymbolAtLocation(node); + return symbol && getTypeOfSymbol(symbol); } + if (ts.isBindingPattern(node)) { + return getTypeForVariableLikeDeclaration(node.parent, /*includeOptionality*/ true); + } + if (isInRightSideOfImportOrExportAssignment(node)) { + var symbol = getSymbolAtLocation(node); + var declaredType = symbol && getDeclaredTypeOfSymbol(symbol); + return declaredType !== unknownType ? declaredType : getTypeOfSymbol(symbol); + } + return unknownType; } - function checkGrammarSourceFile(node) { - return ts.isInAmbientContext(node) && checkGrammarTopLevelElementsForRequiredDeclareModifier(node); - } - function checkGrammarStatementInAmbientContext(node) { - if (ts.isInAmbientContext(node)) { - // An accessors is already reported about the ambient context - if (isAccessor(node.parent.kind)) { - return getNodeLinks(node).hasReportedStatementInAmbientContext = true; + // Gets the type of object literal or array literal of destructuring assignment. + // { a } from + // for ( { a } of elems) { + // } + // [ a ] from + // [a] = [ some array ...] + function getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(expr) { + ts.Debug.assert(expr.kind === 171 /* ObjectLiteralExpression */ || expr.kind === 170 /* ArrayLiteralExpression */); + // If this is from "for of" + // for ( { a } of elems) { + // } + if (expr.parent.kind === 208 /* ForOfStatement */) { + var iteratedType = checkRightHandSideOfForOf(expr.parent.expression); + return checkDestructuringAssignment(expr, iteratedType || unknownType); + } + // If this is from "for" initializer + // for ({a } = elems[0];.....) { } + if (expr.parent.kind === 187 /* BinaryExpression */) { + var iteratedType = checkExpression(expr.parent.right); + return checkDestructuringAssignment(expr, iteratedType || unknownType); + } + // If this is from nested object binding pattern + // for ({ skills: { primary, secondary } } = multiRobot, i = 0; i < 1; i++) { + if (expr.parent.kind === 253 /* PropertyAssignment */) { + var typeOfParentObjectLiteral = getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(expr.parent.parent); + return checkObjectLiteralDestructuringPropertyAssignment(typeOfParentObjectLiteral || unknownType, expr.parent); + } + // Array literal assignment - array destructuring pattern + ts.Debug.assert(expr.parent.kind === 170 /* ArrayLiteralExpression */); + // [{ property1: p1, property2 }] = elems; + var typeOfArrayLiteral = getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(expr.parent); + var elementType = checkIteratedTypeOrElementType(typeOfArrayLiteral || unknownType, expr.parent, /*allowStringInput*/ false) || unknownType; + return checkArrayLiteralDestructuringElementAssignment(expr.parent, typeOfArrayLiteral, ts.indexOf(expr.parent.elements, expr), elementType || unknownType); + } + // Gets the property symbol corresponding to the property in destructuring assignment + // 'property1' from + // for ( { property1: a } of elems) { + // } + // 'property1' at location 'a' from: + // [a] = [ property1, property2 ] + function getPropertySymbolOfDestructuringAssignment(location) { + // Get the type of the object or array literal and then look for property of given name in the type + var typeOfObjectLiteral = getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(location.parent.parent); + return typeOfObjectLiteral && getPropertyOfType(typeOfObjectLiteral, location.text); + } + function getTypeOfExpression(expr) { + if (ts.isRightSideOfQualifiedNameOrPropertyAccess(expr)) { + expr = expr.parent; + } + return getRegularTypeOfLiteralType(checkExpression(expr)); + } + /** + * Gets either the static or instance type of a class element, based on + * whether the element is declared as "static". + */ + function getParentTypeOfClassElement(node) { + var classSymbol = getSymbolOfNode(node.parent); + return ts.getModifierFlags(node) & 32 /* Static */ + ? getTypeOfSymbol(classSymbol) + : getDeclaredTypeOfSymbol(classSymbol); + } + // Return the list of properties of the given type, augmented with properties from Function + // if the type has call or construct signatures + function getAugmentedPropertiesOfType(type) { + type = getApparentType(type); + var propsByName = createSymbolTable(getPropertiesOfType(type)); + if (getSignaturesOfType(type, 0 /* Call */).length || getSignaturesOfType(type, 1 /* Construct */).length) { + ts.forEach(getPropertiesOfType(globalFunctionType), function (p) { + if (!propsByName[p.name]) { + propsByName[p.name] = p; + } + }); + } + return getNamedMembers(propsByName); + } + function getRootSymbols(symbol) { + if (symbol.flags & 268435456 /* SyntheticProperty */) { + var symbols_3 = []; + var name_23 = symbol.name; + ts.forEach(getSymbolLinks(symbol).containingType.types, function (t) { + var symbol = getPropertyOfType(t, name_23); + if (symbol) { + symbols_3.push(symbol); + } + }); + return symbols_3; + } + else if (symbol.flags & 67108864 /* Transient */) { + var target = void 0; + var next = symbol; + while (next = getSymbolLinks(next).target) { + target = next; } - // Find containing block which is either Block, ModuleBlock, SourceFile - var links = getNodeLinks(node); - if (!links.hasReportedStatementInAmbientContext && ts.isFunctionLike(node.parent)) { - return getNodeLinks(node).hasReportedStatementInAmbientContext = grammarErrorOnFirstToken(node, ts.Diagnostics.An_implementation_cannot_be_declared_in_ambient_contexts); + if (target) { + return [target]; } - // We are either parented by another statement, or some sort of block. - // If we're in a block, we only want to really report an error once - // to prevent noisiness. So use a bit on the block to indicate if - // this has already been reported, and don't report if it has. - // - if (node.parent.kind === 199 /* Block */ || node.parent.kind === 226 /* ModuleBlock */ || node.parent.kind === 256 /* SourceFile */) { - var links_1 = getNodeLinks(node.parent); - // Check if the containing block ever report this error - if (!links_1.hasReportedStatementInAmbientContext) { - return links_1.hasReportedStatementInAmbientContext = grammarErrorOnFirstToken(node, ts.Diagnostics.Statements_are_not_allowed_in_ambient_contexts); + } + return [symbol]; + } + // Emitter support + function isArgumentsLocalBinding(node) { + if (!ts.isGeneratedIdentifier(node)) { + node = ts.getParseTreeNode(node, ts.isIdentifier); + if (node) { + return getReferencedValueSymbol(node) === argumentsSymbol; + } + } + return false; + } + function moduleExportsSomeValue(moduleReferenceExpression) { + var moduleSymbol = resolveExternalModuleName(moduleReferenceExpression.parent, moduleReferenceExpression); + if (!moduleSymbol || ts.isShorthandAmbientModuleSymbol(moduleSymbol)) { + // If the module is not found or is shorthand, assume that it may export a value. + return true; + } + var hasExportAssignment = hasExportAssignmentSymbol(moduleSymbol); + // if module has export assignment then 'resolveExternalModuleSymbol' will return resolved symbol for export assignment + // otherwise it will return moduleSymbol itself + moduleSymbol = resolveExternalModuleSymbol(moduleSymbol); + var symbolLinks = getSymbolLinks(moduleSymbol); + if (symbolLinks.exportsSomeValue === undefined) { + // for export assignments - check if resolved symbol for RHS is itself a value + // otherwise - check if at least one export is value + symbolLinks.exportsSomeValue = hasExportAssignment + ? !!(moduleSymbol.flags & 107455 /* Value */) + : ts.forEachProperty(getExportsOfModule(moduleSymbol), isValue); + } + return symbolLinks.exportsSomeValue; + function isValue(s) { + s = resolveSymbol(s); + return s && !!(s.flags & 107455 /* Value */); + } + } + function isNameOfModuleOrEnumDeclaration(node) { + var parent = node.parent; + return ts.isModuleOrEnumDeclaration(parent) && node === parent.name; + } + // When resolved as an expression identifier, if the given node references an exported entity, return the declaration + // node of the exported entity's container. Otherwise, return undefined. + function getReferencedExportContainer(node, prefixLocals) { + node = ts.getParseTreeNode(node, ts.isIdentifier); + if (node) { + // When resolving the export container for the name of a module or enum + // declaration, we need to start resolution at the declaration's container. + // Otherwise, we could incorrectly resolve the export container as the + // declaration if it contains an exported member with the same name. + var symbol = getReferencedValueSymbol(node, /*startInDeclarationContainer*/ isNameOfModuleOrEnumDeclaration(node)); + if (symbol) { + if (symbol.flags & 1048576 /* ExportValue */) { + // If we reference an exported entity within the same module declaration, then whether + // we prefix depends on the kind of entity. SymbolFlags.ExportHasLocal encompasses all the + // kinds that we do NOT prefix. + var exportSymbol = getMergedSymbol(symbol.exportSymbol); + if (!prefixLocals && exportSymbol.flags & 944 /* ExportHasLocal */) { + return undefined; + } + symbol = exportSymbol; + } + var parentSymbol = getParentOfSymbol(symbol); + if (parentSymbol) { + if (parentSymbol.flags & 512 /* ValueModule */ && parentSymbol.valueDeclaration.kind === 256 /* SourceFile */) { + var symbolFile = parentSymbol.valueDeclaration; + var referenceFile = ts.getSourceFileOfNode(node); + // If `node` accesses an export and that export isn't in the same file, then symbol is a namespace export, so return undefined. + var symbolIsUmdExport = symbolFile !== referenceFile; + return symbolIsUmdExport ? undefined : symbolFile; + } + for (var n = node.parent; n; n = n.parent) { + if (ts.isModuleOrEnumDeclaration(n) && getSymbolOfNode(n) === parentSymbol) { + return n; + } + } } } - else { + } + } + // When resolved as an expression identifier, if the given node references an import, return the declaration of + // that import. Otherwise, return undefined. + function getReferencedImportDeclaration(node) { + node = ts.getParseTreeNode(node, ts.isIdentifier); + if (node) { + var symbol = getReferencedValueSymbol(node); + if (symbol && symbol.flags & 8388608 /* Alias */) { + return getDeclarationOfAliasSymbol(symbol); } } + return undefined; } - function checkGrammarNumericLiteral(node) { - // Grammar checking - if (node.isOctalLiteral && languageVersion >= 1 /* ES5 */) { - return grammarErrorOnNode(node, ts.Diagnostics.Octal_literals_are_not_available_when_targeting_ECMAScript_5_and_higher); + function isSymbolOfDeclarationWithCollidingName(symbol) { + if (symbol.flags & 418 /* BlockScoped */) { + var links = getSymbolLinks(symbol); + if (links.isDeclarationWithCollidingName === undefined) { + var container = ts.getEnclosingBlockScopeContainer(symbol.valueDeclaration); + if (ts.isStatementWithLocals(container)) { + var nodeLinks_1 = getNodeLinks(symbol.valueDeclaration); + if (!!resolveName(container.parent, symbol.name, 107455 /* Value */, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined)) { + // redeclaration - always should be renamed + links.isDeclarationWithCollidingName = true; + } + else if (nodeLinks_1.flags & 131072 /* CapturedBlockScopedBinding */) { + // binding is captured in the function + // should be renamed if: + // - binding is not top level - top level bindings never collide with anything + // AND + // - binding is not declared in loop, should be renamed to avoid name reuse across siblings + // let a, b + // { let x = 1; a = () => x; } + // { let x = 100; b = () => x; } + // console.log(a()); // should print '1' + // console.log(b()); // should print '100' + // OR + // - binding is declared inside loop but not in inside initializer of iteration statement or directly inside loop body + // * variables from initializer are passed to rewritten loop body as parameters so they are not captured directly + // * variables that are declared immediately in loop body will become top level variable after loop is rewritten and thus + // they will not collide with anything + var isDeclaredInLoop = nodeLinks_1.flags & 262144 /* BlockScopedBindingInLoop */; + var inLoopInitializer = ts.isIterationStatement(container, /*lookInLabeledStatements*/ false); + var inLoopBodyBlock = container.kind === 199 /* Block */ && ts.isIterationStatement(container.parent, /*lookInLabeledStatements*/ false); + links.isDeclarationWithCollidingName = !ts.isBlockScopedContainerTopLevel(container) && (!isDeclaredInLoop || (!inLoopInitializer && !inLoopBodyBlock)); + } + else { + links.isDeclarationWithCollidingName = false; + } + } + } + return links.isDeclarationWithCollidingName; } + return false; } - function grammarErrorAfterFirstToken(node, message, arg0, arg1, arg2) { - var sourceFile = ts.getSourceFileOfNode(node); - if (!hasParseDiagnostics(sourceFile)) { - var span_5 = ts.getSpanOfTokenAtPosition(sourceFile, node.pos); - diagnostics.add(ts.createFileDiagnostic(sourceFile, ts.textSpanEnd(span_5), /*length*/ 0, message, arg0, arg1, arg2)); + // When resolved as an expression identifier, if the given node references a nested block scoped entity with + // a name that either hides an existing name or might hide it when compiled downlevel, + // return the declaration of that entity. Otherwise, return undefined. + function getReferencedDeclarationWithCollidingName(node) { + if (!ts.isGeneratedIdentifier(node)) { + node = ts.getParseTreeNode(node, ts.isIdentifier); + if (node) { + var symbol = getReferencedValueSymbol(node); + if (symbol && isSymbolOfDeclarationWithCollidingName(symbol)) { + return symbol.valueDeclaration; + } + } + } + return undefined; + } + // Return true if the given node is a declaration of a nested block scoped entity with a name that either hides an + // existing name or might hide a name when compiled downlevel + function isDeclarationWithCollidingName(node) { + node = ts.getParseTreeNode(node, ts.isDeclaration); + if (node) { + var symbol = getSymbolOfNode(node); + if (symbol) { + return isSymbolOfDeclarationWithCollidingName(symbol); + } + } + return false; + } + function isValueAliasDeclaration(node) { + node = ts.getParseTreeNode(node); + if (node === undefined) { + // A synthesized node comes from an emit transformation and is always a value. + return true; + } + switch (node.kind) { + case 229 /* ImportEqualsDeclaration */: + case 231 /* ImportClause */: + case 232 /* NamespaceImport */: + case 234 /* ImportSpecifier */: + case 238 /* ExportSpecifier */: + return isAliasResolvedToValue(getSymbolOfNode(node) || unknownSymbol); + case 236 /* ExportDeclaration */: + var exportClause = node.exportClause; + return exportClause && ts.forEach(exportClause.elements, isValueAliasDeclaration); + case 235 /* ExportAssignment */: + return node.expression + && node.expression.kind === 69 /* Identifier */ + ? isAliasResolvedToValue(getSymbolOfNode(node) || unknownSymbol) + : true; + } + return false; + } + function isTopLevelValueImportEqualsWithEntityName(node) { + node = ts.getParseTreeNode(node, ts.isImportEqualsDeclaration); + if (node === undefined || node.parent.kind !== 256 /* SourceFile */ || !ts.isInternalModuleImportEqualsDeclaration(node)) { + // parent is not source file or it is not reference to internal module + return false; + } + var isValue = isAliasResolvedToValue(getSymbolOfNode(node)); + return isValue && node.moduleReference && !ts.nodeIsMissing(node.moduleReference); + } + function isAliasResolvedToValue(symbol) { + var target = resolveAlias(symbol); + if (target === unknownSymbol) { return true; } + // const enums and modules that contain only const enums are not considered values from the emit perspective + // unless 'preserveConstEnums' option is set to true + return target.flags & 107455 /* Value */ && + (compilerOptions.preserveConstEnums || !isConstEnumOrConstEnumOnlyModule(target)); } - function getAmbientModules() { - var result = []; - for (var sym in globals) { - if (ambientModuleSymbolRegex.test(sym)) { - result.push(globals[sym]); + function isConstEnumOrConstEnumOnlyModule(s) { + return isConstEnumSymbol(s) || s.constEnumOnlyModule; + } + function isReferencedAliasDeclaration(node, checkChildren) { + node = ts.getParseTreeNode(node); + // Purely synthesized nodes are always emitted. + if (node === undefined) { + return true; + } + if (ts.isAliasSymbolDeclaration(node)) { + var symbol = getSymbolOfNode(node); + if (symbol && getSymbolLinks(symbol).referenced) { + return true; } } - return result; + if (checkChildren) { + return ts.forEachChild(node, function (node) { return isReferencedAliasDeclaration(node, checkChildren); }); + } + return false; } - } - ts.createTypeChecker = createTypeChecker; -})(ts || (ts = {})); -/// -/// -/// -/* @internal */ -var ts; -(function (ts) { - ; - /** - * This map contains information about the shape of each Node in "types.ts" pertaining to how - * each node should be traversed during a transformation. - * - * Each edge corresponds to a property in a Node subtype that should be traversed when visiting - * each child. The properties are assigned in the order in which traversal should occur. - * - * We only add entries for nodes that do not have a create/update pair defined in factory.ts - * - * NOTE: This needs to be kept up to date with changes to nodes in "types.ts". Currently, this - * map is not comprehensive. Only node edges relevant to tree transformation are - * currently defined. We may extend this to be more comprehensive, and eventually - * supplant the existing `forEachChild` implementation if performance is not - * significantly impacted. - */ - var nodeEdgeTraversalMap = ts.createMap((_a = {}, - _a[139 /* QualifiedName */] = [ - { name: "left", test: ts.isEntityName }, - { name: "right", test: ts.isIdentifier } - ], - _a[143 /* Decorator */] = [ - { name: "expression", test: ts.isLeftHandSideExpression } - ], - _a[177 /* TypeAssertionExpression */] = [ - { name: "type", test: ts.isTypeNode }, - { name: "expression", test: ts.isUnaryExpression } - ], - _a[195 /* AsExpression */] = [ - { name: "expression", test: ts.isExpression }, - { name: "type", test: ts.isTypeNode } - ], - _a[196 /* NonNullExpression */] = [ - { name: "expression", test: ts.isLeftHandSideExpression } - ], - _a[224 /* EnumDeclaration */] = [ - { name: "decorators", test: ts.isDecorator }, - { name: "modifiers", test: ts.isModifier }, - { name: "name", test: ts.isIdentifier }, - { name: "members", test: ts.isEnumMember } - ], - _a[225 /* ModuleDeclaration */] = [ - { name: "decorators", test: ts.isDecorator }, - { name: "modifiers", test: ts.isModifier }, - { name: "name", test: ts.isModuleName }, - { name: "body", test: ts.isModuleBody } - ], - _a[226 /* ModuleBlock */] = [ - { name: "statements", test: ts.isStatement } - ], - _a[229 /* ImportEqualsDeclaration */] = [ - { name: "decorators", test: ts.isDecorator }, - { name: "modifiers", test: ts.isModifier }, - { name: "name", test: ts.isIdentifier }, - { name: "moduleReference", test: ts.isModuleReference } - ], - _a[240 /* ExternalModuleReference */] = [ - { name: "expression", test: ts.isExpression, optional: true } - ], - _a[255 /* EnumMember */] = [ - { name: "name", test: ts.isPropertyName }, - { name: "initializer", test: ts.isExpression, optional: true, parenthesize: ts.parenthesizeExpressionForList } - ], - _a)); - function reduceNode(node, f, initial) { - return node ? f(initial, node) : initial; - } - /** - * Similar to `reduceLeft`, performs a reduction against each child of a node. - * NOTE: Unlike `forEachChild`, this does *not* visit every node. Only nodes added to the - * `nodeEdgeTraversalMap` above will be visited. - * - * @param node The node containing the children to reduce. - * @param f The callback function - * @param initial The initial value to supply to the reduction. - */ - function reduceEachChild(node, f, initial) { - if (node === undefined) { - return initial; + function isImplementationOfOverload(node) { + if (ts.nodeIsPresent(node.body)) { + var symbol = getSymbolOfNode(node); + var signaturesOfSymbol = getSignaturesOfSymbol(symbol); + // If this function body corresponds to function with multiple signature, it is implementation of overload + // e.g.: function foo(a: string): string; + // function foo(a: number): number; + // function foo(a: any) { // This is implementation of the overloads + // return a; + // } + return signaturesOfSymbol.length > 1 || + // If there is single signature for the symbol, it is overload if that signature isn't coming from the node + // e.g.: function foo(a: string): string; + // function foo(a: any) { // This is implementation of the overloads + // return a; + // } + (signaturesOfSymbol.length === 1 && signaturesOfSymbol[0].declaration !== node); + } + return false; } - var kind = node.kind; - // No need to visit nodes with no children. - if ((kind > 0 /* FirstToken */ && kind <= 138 /* LastToken */)) { - return initial; + function getNodeCheckFlags(node) { + node = ts.getParseTreeNode(node); + return node ? getNodeLinks(node).flags : undefined; } - // We do not yet support types. - if ((kind >= 154 /* TypePredicate */ && kind <= 166 /* LiteralType */)) { - return initial; + function getEnumMemberValue(node) { + computeEnumMemberValues(node.parent); + return getNodeLinks(node).enumMemberValue; } - var result = initial; - switch (node.kind) { - // Leaf nodes - case 198 /* SemicolonClassElement */: - case 201 /* EmptyStatement */: - case 193 /* OmittedExpression */: - case 217 /* DebuggerStatement */: - case 287 /* NotEmittedStatement */: - // No need to visit nodes with no children. - break; - // Names - case 140 /* ComputedPropertyName */: - result = reduceNode(node.expression, f, result); - break; - // Signature elements - case 142 /* Parameter */: - result = ts.reduceLeft(node.decorators, f, result); - result = ts.reduceLeft(node.modifiers, f, result); - result = reduceNode(node.name, f, result); - result = reduceNode(node.type, f, result); - result = reduceNode(node.initializer, f, result); - break; - case 143 /* Decorator */: - result = reduceNode(node.expression, f, result); - break; - // Type member - case 145 /* PropertyDeclaration */: - result = ts.reduceLeft(node.decorators, f, result); - result = ts.reduceLeft(node.modifiers, f, result); - result = reduceNode(node.name, f, result); - result = reduceNode(node.type, f, result); - result = reduceNode(node.initializer, f, result); - break; - case 147 /* MethodDeclaration */: - result = ts.reduceLeft(node.decorators, f, result); - result = ts.reduceLeft(node.modifiers, f, result); - result = reduceNode(node.name, f, result); - result = ts.reduceLeft(node.typeParameters, f, result); - result = ts.reduceLeft(node.parameters, f, result); - result = reduceNode(node.type, f, result); - result = reduceNode(node.body, f, result); - break; - case 148 /* Constructor */: - result = ts.reduceLeft(node.modifiers, f, result); - result = ts.reduceLeft(node.parameters, f, result); - result = reduceNode(node.body, f, result); - break; - case 149 /* GetAccessor */: - result = ts.reduceLeft(node.decorators, f, result); - result = ts.reduceLeft(node.modifiers, f, result); - result = reduceNode(node.name, f, result); - result = ts.reduceLeft(node.parameters, f, result); - result = reduceNode(node.type, f, result); - result = reduceNode(node.body, f, result); - break; - case 150 /* SetAccessor */: - result = ts.reduceLeft(node.decorators, f, result); - result = ts.reduceLeft(node.modifiers, f, result); - result = reduceNode(node.name, f, result); - result = ts.reduceLeft(node.parameters, f, result); - result = reduceNode(node.body, f, result); - break; - // Binding patterns - case 167 /* ObjectBindingPattern */: - case 168 /* ArrayBindingPattern */: - result = ts.reduceLeft(node.elements, f, result); - break; - case 169 /* BindingElement */: - result = reduceNode(node.propertyName, f, result); - result = reduceNode(node.name, f, result); - result = reduceNode(node.initializer, f, result); - break; - // Expression - case 170 /* ArrayLiteralExpression */: - result = ts.reduceLeft(node.elements, f, result); - break; - case 171 /* ObjectLiteralExpression */: - result = ts.reduceLeft(node.properties, f, result); - break; - case 172 /* PropertyAccessExpression */: - result = reduceNode(node.expression, f, result); - result = reduceNode(node.name, f, result); - break; - case 173 /* ElementAccessExpression */: - result = reduceNode(node.expression, f, result); - result = reduceNode(node.argumentExpression, f, result); - break; - case 174 /* CallExpression */: - result = reduceNode(node.expression, f, result); - result = ts.reduceLeft(node.typeArguments, f, result); - result = ts.reduceLeft(node.arguments, f, result); - break; - case 175 /* NewExpression */: - result = reduceNode(node.expression, f, result); - result = ts.reduceLeft(node.typeArguments, f, result); - result = ts.reduceLeft(node.arguments, f, result); - break; - case 176 /* TaggedTemplateExpression */: - result = reduceNode(node.tag, f, result); - result = reduceNode(node.template, f, result); - break; - case 179 /* FunctionExpression */: - result = ts.reduceLeft(node.modifiers, f, result); - result = reduceNode(node.name, f, result); - result = ts.reduceLeft(node.typeParameters, f, result); - result = ts.reduceLeft(node.parameters, f, result); - result = reduceNode(node.type, f, result); - result = reduceNode(node.body, f, result); - break; - case 180 /* ArrowFunction */: - result = ts.reduceLeft(node.modifiers, f, result); - result = ts.reduceLeft(node.typeParameters, f, result); - result = ts.reduceLeft(node.parameters, f, result); - result = reduceNode(node.type, f, result); - result = reduceNode(node.body, f, result); - break; - case 178 /* ParenthesizedExpression */: - case 181 /* DeleteExpression */: - case 182 /* TypeOfExpression */: - case 183 /* VoidExpression */: - case 184 /* AwaitExpression */: - case 190 /* YieldExpression */: - case 191 /* SpreadElementExpression */: - case 196 /* NonNullExpression */: - result = reduceNode(node.expression, f, result); - break; - case 185 /* PrefixUnaryExpression */: - case 186 /* PostfixUnaryExpression */: - result = reduceNode(node.operand, f, result); - break; - case 187 /* BinaryExpression */: - result = reduceNode(node.left, f, result); - result = reduceNode(node.right, f, result); - break; - case 188 /* ConditionalExpression */: - result = reduceNode(node.condition, f, result); - result = reduceNode(node.whenTrue, f, result); - result = reduceNode(node.whenFalse, f, result); - break; - case 189 /* TemplateExpression */: - result = reduceNode(node.head, f, result); - result = ts.reduceLeft(node.templateSpans, f, result); - break; - case 192 /* ClassExpression */: - result = ts.reduceLeft(node.modifiers, f, result); - result = reduceNode(node.name, f, result); - result = ts.reduceLeft(node.typeParameters, f, result); - result = ts.reduceLeft(node.heritageClauses, f, result); - result = ts.reduceLeft(node.members, f, result); - break; - case 194 /* ExpressionWithTypeArguments */: - result = reduceNode(node.expression, f, result); - result = ts.reduceLeft(node.typeArguments, f, result); - break; - // Misc - case 197 /* TemplateSpan */: - result = reduceNode(node.expression, f, result); - result = reduceNode(node.literal, f, result); - break; - // Element - case 199 /* Block */: - result = ts.reduceLeft(node.statements, f, result); - break; - case 200 /* VariableStatement */: - result = ts.reduceLeft(node.modifiers, f, result); - result = reduceNode(node.declarationList, f, result); - break; - case 202 /* ExpressionStatement */: - result = reduceNode(node.expression, f, result); - break; - case 203 /* IfStatement */: - result = reduceNode(node.expression, f, result); - result = reduceNode(node.thenStatement, f, result); - result = reduceNode(node.elseStatement, f, result); - break; - case 204 /* DoStatement */: - result = reduceNode(node.statement, f, result); - result = reduceNode(node.expression, f, result); - break; - case 205 /* WhileStatement */: - case 212 /* WithStatement */: - result = reduceNode(node.expression, f, result); - result = reduceNode(node.statement, f, result); - break; - case 206 /* ForStatement */: - result = reduceNode(node.initializer, f, result); - result = reduceNode(node.condition, f, result); - result = reduceNode(node.incrementor, f, result); - result = reduceNode(node.statement, f, result); - break; - case 207 /* ForInStatement */: - case 208 /* ForOfStatement */: - result = reduceNode(node.initializer, f, result); - result = reduceNode(node.expression, f, result); - result = reduceNode(node.statement, f, result); - break; - case 211 /* ReturnStatement */: - case 215 /* ThrowStatement */: - result = reduceNode(node.expression, f, result); - break; - case 213 /* SwitchStatement */: - result = reduceNode(node.expression, f, result); - result = reduceNode(node.caseBlock, f, result); - break; - case 214 /* LabeledStatement */: - result = reduceNode(node.label, f, result); - result = reduceNode(node.statement, f, result); - break; - case 216 /* TryStatement */: - result = reduceNode(node.tryBlock, f, result); - result = reduceNode(node.catchClause, f, result); - result = reduceNode(node.finallyBlock, f, result); - break; - case 218 /* VariableDeclaration */: - result = reduceNode(node.name, f, result); - result = reduceNode(node.type, f, result); - result = reduceNode(node.initializer, f, result); - break; - case 219 /* VariableDeclarationList */: - result = ts.reduceLeft(node.declarations, f, result); - break; - case 220 /* FunctionDeclaration */: - result = ts.reduceLeft(node.decorators, f, result); - result = ts.reduceLeft(node.modifiers, f, result); - result = reduceNode(node.name, f, result); - result = ts.reduceLeft(node.typeParameters, f, result); - result = ts.reduceLeft(node.parameters, f, result); - result = reduceNode(node.type, f, result); - result = reduceNode(node.body, f, result); - break; - case 221 /* ClassDeclaration */: - result = ts.reduceLeft(node.decorators, f, result); - result = ts.reduceLeft(node.modifiers, f, result); - result = reduceNode(node.name, f, result); - result = ts.reduceLeft(node.typeParameters, f, result); - result = ts.reduceLeft(node.heritageClauses, f, result); - result = ts.reduceLeft(node.members, f, result); - break; - case 227 /* CaseBlock */: - result = ts.reduceLeft(node.clauses, f, result); - break; - case 230 /* ImportDeclaration */: - result = ts.reduceLeft(node.decorators, f, result); - result = ts.reduceLeft(node.modifiers, f, result); - result = reduceNode(node.importClause, f, result); - result = reduceNode(node.moduleSpecifier, f, result); - break; - case 231 /* ImportClause */: - result = reduceNode(node.name, f, result); - result = reduceNode(node.namedBindings, f, result); - break; - case 232 /* NamespaceImport */: - result = reduceNode(node.name, f, result); - break; - case 233 /* NamedImports */: - case 237 /* NamedExports */: - result = ts.reduceLeft(node.elements, f, result); - break; - case 234 /* ImportSpecifier */: - case 238 /* ExportSpecifier */: - result = reduceNode(node.propertyName, f, result); - result = reduceNode(node.name, f, result); - break; - case 235 /* ExportAssignment */: - result = ts.reduceLeft(node.decorators, f, result); - result = ts.reduceLeft(node.modifiers, f, result); - result = reduceNode(node.expression, f, result); - break; - case 236 /* ExportDeclaration */: - result = ts.reduceLeft(node.decorators, f, result); - result = ts.reduceLeft(node.modifiers, f, result); - result = reduceNode(node.exportClause, f, result); - result = reduceNode(node.moduleSpecifier, f, result); - break; - // JSX - case 241 /* JsxElement */: - result = reduceNode(node.openingElement, f, result); - result = ts.reduceLeft(node.children, f, result); - result = reduceNode(node.closingElement, f, result); - break; - case 242 /* JsxSelfClosingElement */: - case 243 /* JsxOpeningElement */: - result = reduceNode(node.tagName, f, result); - result = ts.reduceLeft(node.attributes, f, result); - break; - case 245 /* JsxClosingElement */: - result = reduceNode(node.tagName, f, result); - break; - case 246 /* JsxAttribute */: - result = reduceNode(node.name, f, result); - result = reduceNode(node.initializer, f, result); - break; - case 247 /* JsxSpreadAttribute */: - result = reduceNode(node.expression, f, result); - break; - case 248 /* JsxExpression */: - result = reduceNode(node.expression, f, result); - break; - // Clauses - case 249 /* CaseClause */: - result = reduceNode(node.expression, f, result); - // fall-through - case 250 /* DefaultClause */: - result = ts.reduceLeft(node.statements, f, result); - break; - case 251 /* HeritageClause */: - result = ts.reduceLeft(node.types, f, result); - break; - case 252 /* CatchClause */: - result = reduceNode(node.variableDeclaration, f, result); - result = reduceNode(node.block, f, result); - break; - // Property assignments - case 253 /* PropertyAssignment */: - result = reduceNode(node.name, f, result); - result = reduceNode(node.initializer, f, result); - break; - case 254 /* ShorthandPropertyAssignment */: - result = reduceNode(node.name, f, result); - result = reduceNode(node.objectAssignmentInitializer, f, result); - break; - // Top-level nodes - case 256 /* SourceFile */: - result = ts.reduceLeft(node.statements, f, result); - break; - case 288 /* PartiallyEmittedExpression */: - result = reduceNode(node.expression, f, result); - break; - default: - var edgeTraversalPath = nodeEdgeTraversalMap[kind]; - if (edgeTraversalPath) { - for (var _i = 0, edgeTraversalPath_1 = edgeTraversalPath; _i < edgeTraversalPath_1.length; _i++) { - var edge = edgeTraversalPath_1[_i]; - var value = node[edge.name]; - if (value !== undefined) { - result = ts.isArray(value) - ? ts.reduceLeft(value, f, result) - : f(result, value); - } - } + function getConstantValue(node) { + if (node.kind === 255 /* EnumMember */) { + return getEnumMemberValue(node); + } + var symbol = getNodeLinks(node).resolvedSymbol; + if (symbol && (symbol.flags & 8 /* EnumMember */)) { + // inline property\index accesses only for const enums + if (ts.isConstEnumDeclaration(symbol.valueDeclaration.parent)) { + return getEnumMemberValue(symbol.valueDeclaration); } - break; - } - return result; - } - ts.reduceEachChild = reduceEachChild; - function visitNode(node, visitor, test, optional, lift, parenthesize, parentNode) { - if (node === undefined) { - return undefined; - } - var visited = visitor(node); - if (visited === node) { - return node; - } - var visitedNode; - if (visited === undefined) { - if (!optional) { - Debug.failNotOptional(); } return undefined; } - else if (ts.isArray(visited)) { - visitedNode = (lift || extractSingleNode)(visited); + function isFunctionType(type) { + return type.flags & 2588672 /* ObjectType */ && getSignaturesOfType(type, 0 /* Call */).length > 0; } - else { - visitedNode = visited; + function getTypeReferenceSerializationKind(typeName, location) { + // Resolve the symbol as a value to ensure the type can be reached at runtime during emit. + var valueSymbol = resolveEntityName(typeName, 107455 /* Value */, /*ignoreErrors*/ true, /*dontResolveAlias*/ false, location); + var globalPromiseSymbol = tryGetGlobalPromiseConstructorSymbol(); + if (globalPromiseSymbol && valueSymbol === globalPromiseSymbol) { + return ts.TypeReferenceSerializationKind.Promise; + } + var constructorType = valueSymbol ? getTypeOfSymbol(valueSymbol) : undefined; + if (constructorType && isConstructorType(constructorType)) { + return ts.TypeReferenceSerializationKind.TypeWithConstructSignatureAndValue; + } + // Resolve the symbol as a type so that we can provide a more useful hint for the type serializer. + var typeSymbol = resolveEntityName(typeName, 793064 /* Type */, /*ignoreErrors*/ true, /*dontResolveAlias*/ false, location); + // We might not be able to resolve type symbol so use unknown type in that case (eg error case) + if (!typeSymbol) { + return ts.TypeReferenceSerializationKind.ObjectType; + } + var type = getDeclaredTypeOfSymbol(typeSymbol); + if (type === unknownType) { + return ts.TypeReferenceSerializationKind.Unknown; + } + else if (type.flags & 1 /* Any */) { + return ts.TypeReferenceSerializationKind.ObjectType; + } + else if (isTypeOfKind(type, 1024 /* Void */ | 6144 /* Nullable */ | 8192 /* Never */)) { + return ts.TypeReferenceSerializationKind.VoidNullableOrNeverType; + } + else if (isTypeOfKind(type, 136 /* BooleanLike */)) { + return ts.TypeReferenceSerializationKind.BooleanType; + } + else if (isTypeOfKind(type, 340 /* NumberLike */)) { + return ts.TypeReferenceSerializationKind.NumberLikeType; + } + else if (isTypeOfKind(type, 34 /* StringLike */)) { + return ts.TypeReferenceSerializationKind.StringLikeType; + } + else if (isTupleType(type)) { + return ts.TypeReferenceSerializationKind.ArrayLikeType; + } + else if (isTypeOfKind(type, 512 /* ESSymbol */)) { + return ts.TypeReferenceSerializationKind.ESSymbolType; + } + else if (isFunctionType(type)) { + return ts.TypeReferenceSerializationKind.TypeWithCallSignature; + } + else if (isArrayType(type)) { + return ts.TypeReferenceSerializationKind.ArrayLikeType; + } + else { + return ts.TypeReferenceSerializationKind.ObjectType; + } } - if (parenthesize !== undefined) { - visitedNode = parenthesize(visitedNode, parentNode); + function writeTypeOfDeclaration(declaration, enclosingDeclaration, flags, writer) { + // Get type of the symbol if this is the valid symbol otherwise get type at location + var symbol = getSymbolOfNode(declaration); + var type = symbol && !(symbol.flags & (2048 /* TypeLiteral */ | 131072 /* Signature */)) + ? getWidenedLiteralType(getTypeOfSymbol(symbol)) + : unknownType; + getSymbolDisplayBuilder().buildTypeDisplay(type, writer, enclosingDeclaration, flags); } - Debug.assertNode(visitedNode, test); - aggregateTransformFlags(visitedNode); - return visitedNode; - } - ts.visitNode = visitNode; - function visitNodes(nodes, visitor, test, start, count, parenthesize, parentNode) { - if (nodes === undefined) { - return undefined; + function writeReturnTypeOfSignatureDeclaration(signatureDeclaration, enclosingDeclaration, flags, writer) { + var signature = getSignatureFromDeclaration(signatureDeclaration); + getSymbolDisplayBuilder().buildTypeDisplay(getReturnTypeOfSignature(signature), writer, enclosingDeclaration, flags); } - var updated; - // Ensure start and count have valid values - var length = nodes.length; - if (start === undefined || start < 0) { - start = 0; + function writeTypeOfExpression(expr, enclosingDeclaration, flags, writer) { + var type = getWidenedType(getTypeOfExpression(expr)); + getSymbolDisplayBuilder().buildTypeDisplay(type, writer, enclosingDeclaration, flags); } - if (count === undefined || count > length - start) { - count = length - start; + function writeBaseConstructorTypeOfClass(node, enclosingDeclaration, flags, writer) { + var classType = getDeclaredTypeOfSymbol(getSymbolOfNode(node)); + resolveBaseTypesOfClass(classType); + var baseType = classType.resolvedBaseTypes.length ? classType.resolvedBaseTypes[0] : unknownType; + getSymbolDisplayBuilder().buildTypeDisplay(baseType, writer, enclosingDeclaration, flags); } - if (start > 0 || count < length) { - // If we are not visiting all of the original nodes, we must always create a new array. - // Since this is a fragment of a node array, we do not copy over the previous location - // and will only copy over `hasTrailingComma` if we are including the last element. - updated = ts.createNodeArray([], /*location*/ undefined, - /*hasTrailingComma*/ nodes.hasTrailingComma && start + count === length); + function hasGlobalName(name) { + return !!globals[name]; } - // Visit each original node. - for (var i = 0; i < count; i++) { - var node = nodes[i + start]; - var visited = node !== undefined ? visitor(node) : undefined; - if (updated !== undefined || visited === undefined || visited !== node) { - if (updated === undefined) { - // Ensure we have a copy of `nodes`, up to the current index. - updated = ts.createNodeArray(nodes.slice(0, i), /*location*/ nodes, nodes.hasTrailingComma); + function getReferencedValueSymbol(reference, startInDeclarationContainer) { + var resolvedSymbol = getNodeLinks(reference).resolvedSymbol; + if (resolvedSymbol) { + return resolvedSymbol; + } + var location = reference; + if (startInDeclarationContainer) { + // When resolving the name of a declaration as a value, we need to start resolution + // at a point outside of the declaration. + var parent_14 = reference.parent; + if (ts.isDeclaration(parent_14) && reference === parent_14.name) { + location = getDeclarationContainer(parent_14); } - if (visited) { - if (ts.isArray(visited)) { - for (var _i = 0, visited_1 = visited; _i < visited_1.length; _i++) { - var visitedNode = visited_1[_i]; - visitedNode = parenthesize - ? parenthesize(visitedNode, parentNode) - : visitedNode; - Debug.assertNode(visitedNode, test); - aggregateTransformFlags(visitedNode); - updated.push(visitedNode); - } - } - else { - var visitedNode = parenthesize - ? parenthesize(visited, parentNode) - : visited; - Debug.assertNode(visitedNode, test); - aggregateTransformFlags(visitedNode); - updated.push(visitedNode); + } + return resolveName(location, reference.text, 107455 /* Value */ | 1048576 /* ExportValue */ | 8388608 /* Alias */, /*nodeNotFoundMessage*/ undefined, /*nameArg*/ undefined); + } + function getReferencedValueDeclaration(reference) { + if (!ts.isGeneratedIdentifier(reference)) { + reference = ts.getParseTreeNode(reference, ts.isIdentifier); + if (reference) { + var symbol = getReferencedValueSymbol(reference); + if (symbol) { + return getExportSymbolOfValueSymbolIfExported(symbol).valueDeclaration; } } } - } - return updated || nodes; - } - ts.visitNodes = visitNodes; - function visitEachChild(node, visitor, context) { - if (node === undefined) { return undefined; } - var kind = node.kind; - // No need to visit nodes with no children. - if ((kind > 0 /* FirstToken */ && kind <= 138 /* LastToken */)) { - return node; + function isLiteralConstDeclaration(node) { + if (ts.isConst(node)) { + var type = getTypeOfSymbol(getSymbolOfNode(node)); + return !!(type.flags & 96 /* StringOrNumberLiteral */ && type.flags & 16777216 /* FreshLiteral */); + } + return false; } - // We do not yet support types. - if ((kind >= 154 /* TypePredicate */ && kind <= 166 /* LiteralType */)) { - return node; + function writeLiteralConstValue(node, writer) { + var type = getTypeOfSymbol(getSymbolOfNode(node)); + writer.writeStringLiteral(literalTypeToString(type)); } - switch (node.kind) { - case 198 /* SemicolonClassElement */: - case 201 /* EmptyStatement */: - case 193 /* OmittedExpression */: - case 217 /* DebuggerStatement */: - // No need to visit nodes with no children. - return node; - // Names - case 140 /* ComputedPropertyName */: - return ts.updateComputedPropertyName(node, visitNode(node.expression, visitor, ts.isExpression)); - // Signature elements - case 142 /* Parameter */: - return ts.updateParameterDeclaration(node, visitNodes(node.decorators, visitor, ts.isDecorator), visitNodes(node.modifiers, visitor, ts.isModifier), visitNode(node.name, visitor, ts.isBindingName), visitNode(node.type, visitor, ts.isTypeNode, /*optional*/ true), visitNode(node.initializer, visitor, ts.isExpression, /*optional*/ true)); - // Type member - case 145 /* PropertyDeclaration */: - return ts.updateProperty(node, visitNodes(node.decorators, visitor, ts.isDecorator), visitNodes(node.modifiers, visitor, ts.isModifier), visitNode(node.name, visitor, ts.isPropertyName), visitNode(node.type, visitor, ts.isTypeNode, /*optional*/ true), visitNode(node.initializer, visitor, ts.isExpression, /*optional*/ true)); - case 147 /* MethodDeclaration */: - return ts.updateMethod(node, visitNodes(node.decorators, visitor, ts.isDecorator), visitNodes(node.modifiers, visitor, ts.isModifier), visitNode(node.name, visitor, ts.isPropertyName), visitNodes(node.typeParameters, visitor, ts.isTypeParameter), (context.startLexicalEnvironment(), visitNodes(node.parameters, visitor, ts.isParameter)), visitNode(node.type, visitor, ts.isTypeNode, /*optional*/ true), mergeFunctionBodyLexicalEnvironment(visitNode(node.body, visitor, ts.isFunctionBody, /*optional*/ true), context.endLexicalEnvironment())); - case 148 /* Constructor */: - return ts.updateConstructor(node, visitNodes(node.decorators, visitor, ts.isDecorator), visitNodes(node.modifiers, visitor, ts.isModifier), (context.startLexicalEnvironment(), visitNodes(node.parameters, visitor, ts.isParameter)), mergeFunctionBodyLexicalEnvironment(visitNode(node.body, visitor, ts.isFunctionBody, /*optional*/ true), context.endLexicalEnvironment())); - case 149 /* GetAccessor */: - return ts.updateGetAccessor(node, visitNodes(node.decorators, visitor, ts.isDecorator), visitNodes(node.modifiers, visitor, ts.isModifier), visitNode(node.name, visitor, ts.isPropertyName), (context.startLexicalEnvironment(), visitNodes(node.parameters, visitor, ts.isParameter)), visitNode(node.type, visitor, ts.isTypeNode, /*optional*/ true), mergeFunctionBodyLexicalEnvironment(visitNode(node.body, visitor, ts.isFunctionBody, /*optional*/ true), context.endLexicalEnvironment())); - case 150 /* SetAccessor */: - return ts.updateSetAccessor(node, visitNodes(node.decorators, visitor, ts.isDecorator), visitNodes(node.modifiers, visitor, ts.isModifier), visitNode(node.name, visitor, ts.isPropertyName), (context.startLexicalEnvironment(), visitNodes(node.parameters, visitor, ts.isParameter)), mergeFunctionBodyLexicalEnvironment(visitNode(node.body, visitor, ts.isFunctionBody, /*optional*/ true), context.endLexicalEnvironment())); - // Binding patterns - case 167 /* ObjectBindingPattern */: - return ts.updateObjectBindingPattern(node, visitNodes(node.elements, visitor, ts.isBindingElement)); - case 168 /* ArrayBindingPattern */: - return ts.updateArrayBindingPattern(node, visitNodes(node.elements, visitor, ts.isArrayBindingElement)); - case 169 /* BindingElement */: - return ts.updateBindingElement(node, visitNode(node.propertyName, visitor, ts.isPropertyName, /*optional*/ true), visitNode(node.name, visitor, ts.isBindingName), visitNode(node.initializer, visitor, ts.isExpression, /*optional*/ true)); - // Expression + function createResolver() { + // this variable and functions that use it are deliberately moved here from the outer scope + // to avoid scope pollution + var resolvedTypeReferenceDirectives = host.getResolvedTypeReferenceDirectives(); + var fileToDirective; + if (resolvedTypeReferenceDirectives) { + // populate reverse mapping: file path -> type reference directive that was resolved to this file + fileToDirective = ts.createFileMap(); + for (var key in resolvedTypeReferenceDirectives) { + var resolvedDirective = resolvedTypeReferenceDirectives[key]; + if (!resolvedDirective) { + continue; + } + var file = host.getSourceFile(resolvedDirective.resolvedFileName); + fileToDirective.set(file.path, key); + } + } + return { + getReferencedExportContainer: getReferencedExportContainer, + getReferencedImportDeclaration: getReferencedImportDeclaration, + getReferencedDeclarationWithCollidingName: getReferencedDeclarationWithCollidingName, + isDeclarationWithCollidingName: isDeclarationWithCollidingName, + isValueAliasDeclaration: isValueAliasDeclaration, + hasGlobalName: hasGlobalName, + isReferencedAliasDeclaration: isReferencedAliasDeclaration, + getNodeCheckFlags: getNodeCheckFlags, + isTopLevelValueImportEqualsWithEntityName: isTopLevelValueImportEqualsWithEntityName, + isDeclarationVisible: isDeclarationVisible, + isImplementationOfOverload: isImplementationOfOverload, + writeTypeOfDeclaration: writeTypeOfDeclaration, + writeReturnTypeOfSignatureDeclaration: writeReturnTypeOfSignatureDeclaration, + writeTypeOfExpression: writeTypeOfExpression, + writeBaseConstructorTypeOfClass: writeBaseConstructorTypeOfClass, + isSymbolAccessible: isSymbolAccessible, + isEntityNameVisible: isEntityNameVisible, + getConstantValue: getConstantValue, + collectLinkedAliases: collectLinkedAliases, + getReferencedValueDeclaration: getReferencedValueDeclaration, + getTypeReferenceSerializationKind: getTypeReferenceSerializationKind, + isOptionalParameter: isOptionalParameter, + moduleExportsSomeValue: moduleExportsSomeValue, + isArgumentsLocalBinding: isArgumentsLocalBinding, + getExternalModuleFileFromDeclaration: getExternalModuleFileFromDeclaration, + getTypeReferenceDirectivesForEntityName: getTypeReferenceDirectivesForEntityName, + getTypeReferenceDirectivesForSymbol: getTypeReferenceDirectivesForSymbol, + isLiteralConstDeclaration: isLiteralConstDeclaration, + writeLiteralConstValue: writeLiteralConstValue + }; + // defined here to avoid outer scope pollution + function getTypeReferenceDirectivesForEntityName(node) { + // program does not have any files with type reference directives - bail out + if (!fileToDirective) { + return undefined; + } + // property access can only be used as values + // qualified names can only be used as types\namespaces + // identifiers are treated as values only if they appear in type queries + var meaning = (node.kind === 172 /* PropertyAccessExpression */) || (node.kind === 69 /* Identifier */ && isInTypeQuery(node)) + ? 107455 /* Value */ | 1048576 /* ExportValue */ + : 793064 /* Type */ | 1920 /* Namespace */; + var symbol = resolveEntityName(node, meaning, /*ignoreErrors*/ true); + return symbol && symbol !== unknownSymbol ? getTypeReferenceDirectivesForSymbol(symbol, meaning) : undefined; + } + // defined here to avoid outer scope pollution + function getTypeReferenceDirectivesForSymbol(symbol, meaning) { + // program does not have any files with type reference directives - bail out + if (!fileToDirective) { + return undefined; + } + if (!isSymbolFromTypeDeclarationFile(symbol)) { + return undefined; + } + // check what declarations in the symbol can contribute to the target meaning + var typeReferenceDirectives; + for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { + var decl = _a[_i]; + // check meaning of the local symbol to see if declaration needs to be analyzed further + if (decl.symbol && decl.symbol.flags & meaning) { + var file = ts.getSourceFileOfNode(decl); + var typeReferenceDirective = fileToDirective.get(file.path); + if (typeReferenceDirective) { + (typeReferenceDirectives || (typeReferenceDirectives = [])).push(typeReferenceDirective); + } + } + } + return typeReferenceDirectives; + } + function isSymbolFromTypeDeclarationFile(symbol) { + // bail out if symbol does not have associated declarations (i.e. this is transient symbol created for property in binding pattern) + if (!symbol.declarations) { + return false; + } + // walk the parent chain for symbols to make sure that top level parent symbol is in the global scope + // external modules cannot define or contribute to type declaration files + var current = symbol; + while (true) { + var parent_15 = getParentOfSymbol(current); + if (parent_15) { + current = parent_15; + } + else { + break; + } + } + if (current.valueDeclaration && current.valueDeclaration.kind === 256 /* SourceFile */ && current.flags & 512 /* ValueModule */) { + return false; + } + // check that at least one declaration of top level symbol originates from type declaration file + for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { + var decl = _a[_i]; + var file = ts.getSourceFileOfNode(decl); + if (fileToDirective.contains(file.path)) { + return true; + } + } + return false; + } + } + function getExternalModuleFileFromDeclaration(declaration) { + var specifier = ts.getExternalModuleName(declaration); + var moduleSymbol = resolveExternalModuleNameWorker(specifier, specifier, /*moduleNotFoundError*/ undefined); + if (!moduleSymbol) { + return undefined; + } + return ts.getDeclarationOfKind(moduleSymbol, 256 /* SourceFile */); + } + function initializeTypeChecker() { + // Bind all source files and propagate errors + for (var _i = 0, _a = host.getSourceFiles(); _i < _a.length; _i++) { + var file = _a[_i]; + ts.bindSourceFile(file, compilerOptions); + } + // Initialize global symbol table + var augmentations; + var requestedExternalEmitHelpers = 0; + var firstFileRequestingExternalHelpers; + for (var _b = 0, _c = host.getSourceFiles(); _b < _c.length; _b++) { + var file = _c[_b]; + if (!ts.isExternalOrCommonJsModule(file)) { + mergeSymbolTable(globals, file.locals); + } + if (file.patternAmbientModules && file.patternAmbientModules.length) { + patternAmbientModules = ts.concatenate(patternAmbientModules, file.patternAmbientModules); + } + if (file.moduleAugmentations.length) { + (augmentations || (augmentations = [])).push(file.moduleAugmentations); + } + if (file.symbol && file.symbol.globalExports) { + // Merge in UMD exports with first-in-wins semantics (see #9771) + var source = file.symbol.globalExports; + for (var id in source) { + if (!(id in globals)) { + globals[id] = source[id]; + } + } + } + if ((compilerOptions.isolatedModules || ts.isExternalModule(file)) && !file.isDeclarationFile) { + var fileRequestedExternalEmitHelpers = file.flags & 31744 /* EmitHelperFlags */; + if (fileRequestedExternalEmitHelpers) { + requestedExternalEmitHelpers |= fileRequestedExternalEmitHelpers; + if (firstFileRequestingExternalHelpers === undefined) { + firstFileRequestingExternalHelpers = file; + } + } + } + } + if (augmentations) { + // merge module augmentations. + // this needs to be done after global symbol table is initialized to make sure that all ambient modules are indexed + for (var _d = 0, augmentations_1 = augmentations; _d < augmentations_1.length; _d++) { + var list = augmentations_1[_d]; + for (var _e = 0, list_1 = list; _e < list_1.length; _e++) { + var augmentation = list_1[_e]; + mergeModuleAugmentation(augmentation); + } + } + } + // Setup global builtins + addToSymbolTable(globals, builtinGlobals, ts.Diagnostics.Declaration_name_conflicts_with_built_in_global_identifier_0); + getSymbolLinks(undefinedSymbol).type = undefinedWideningType; + getSymbolLinks(argumentsSymbol).type = getGlobalType("IArguments"); + getSymbolLinks(unknownSymbol).type = unknownType; + // Initialize special types + globalArrayType = getGlobalType("Array", /*arity*/ 1); + globalObjectType = getGlobalType("Object"); + globalFunctionType = getGlobalType("Function"); + globalStringType = getGlobalType("String"); + globalNumberType = getGlobalType("Number"); + globalBooleanType = getGlobalType("Boolean"); + globalRegExpType = getGlobalType("RegExp"); + jsxElementType = getExportedTypeFromNamespace("JSX", JsxNames.Element); + getGlobalClassDecoratorType = ts.memoize(function () { return getGlobalType("ClassDecorator"); }); + getGlobalPropertyDecoratorType = ts.memoize(function () { return getGlobalType("PropertyDecorator"); }); + getGlobalMethodDecoratorType = ts.memoize(function () { return getGlobalType("MethodDecorator"); }); + getGlobalParameterDecoratorType = ts.memoize(function () { return getGlobalType("ParameterDecorator"); }); + getGlobalTypedPropertyDescriptorType = ts.memoize(function () { return getGlobalType("TypedPropertyDescriptor", /*arity*/ 1); }); + getGlobalESSymbolConstructorSymbol = ts.memoize(function () { return getGlobalValueSymbol("Symbol"); }); + getGlobalPromiseType = ts.memoize(function () { return getGlobalType("Promise", /*arity*/ 1); }); + tryGetGlobalPromiseType = ts.memoize(function () { return getGlobalSymbol("Promise", 793064 /* Type */, /*diagnostic*/ undefined) && getGlobalPromiseType(); }); + getGlobalPromiseLikeType = ts.memoize(function () { return getGlobalType("PromiseLike", /*arity*/ 1); }); + getInstantiatedGlobalPromiseLikeType = ts.memoize(createInstantiatedPromiseLikeType); + getGlobalPromiseConstructorSymbol = ts.memoize(function () { return getGlobalValueSymbol("Promise"); }); + tryGetGlobalPromiseConstructorSymbol = ts.memoize(function () { return getGlobalSymbol("Promise", 107455 /* Value */, /*diagnostic*/ undefined) && getGlobalPromiseConstructorSymbol(); }); + getGlobalPromiseConstructorLikeType = ts.memoize(function () { return getGlobalType("PromiseConstructorLike"); }); + getGlobalThenableType = ts.memoize(createThenableType); + getGlobalTemplateStringsArrayType = ts.memoize(function () { return getGlobalType("TemplateStringsArray"); }); + if (languageVersion >= 2 /* ES6 */) { + getGlobalESSymbolType = ts.memoize(function () { return getGlobalType("Symbol"); }); + getGlobalIterableType = ts.memoize(function () { return getGlobalType("Iterable", /*arity*/ 1); }); + getGlobalIteratorType = ts.memoize(function () { return getGlobalType("Iterator", /*arity*/ 1); }); + getGlobalIterableIteratorType = ts.memoize(function () { return getGlobalType("IterableIterator", /*arity*/ 1); }); + } + else { + getGlobalESSymbolType = ts.memoize(function () { return emptyObjectType; }); + getGlobalIterableType = ts.memoize(function () { return emptyGenericType; }); + getGlobalIteratorType = ts.memoize(function () { return emptyGenericType; }); + getGlobalIterableIteratorType = ts.memoize(function () { return emptyGenericType; }); + } + anyArrayType = createArrayType(anyType); + var symbol = getGlobalSymbol("ReadonlyArray", 793064 /* Type */, /*diagnostic*/ undefined); + globalReadonlyArrayType = symbol && getTypeOfGlobalSymbol(symbol, /*arity*/ 1); + anyReadonlyArrayType = globalReadonlyArrayType ? createTypeFromGenericGlobalType(globalReadonlyArrayType, [anyType]) : anyArrayType; + // If we have specified that we are importing helpers, we should report global + // errors if we cannot resolve the helpers external module, or if it does not have + // the necessary helpers exported. + if (compilerOptions.importHelpers && firstFileRequestingExternalHelpers) { + // Find the first reference to the helpers module. + var helpersModule = resolveExternalModule(firstFileRequestingExternalHelpers, ts.externalHelpersModuleNameText, ts.Diagnostics.Cannot_find_module_0, + /*errorNode*/ undefined); + // If we found the module, report errors if it does not have the necessary exports. + if (helpersModule) { + var exports = helpersModule.exports; + if (requestedExternalEmitHelpers & 1024 /* HasClassExtends */ && languageVersion < 2 /* ES6 */) { + verifyHelperSymbol(exports, "__extends", 107455 /* Value */); + } + if (requestedExternalEmitHelpers & 16384 /* HasJsxSpreadAttributes */ && compilerOptions.jsx !== 1 /* Preserve */) { + verifyHelperSymbol(exports, "__assign", 107455 /* Value */); + } + if (requestedExternalEmitHelpers & 2048 /* HasDecorators */) { + verifyHelperSymbol(exports, "__decorate", 107455 /* Value */); + if (compilerOptions.emitDecoratorMetadata) { + verifyHelperSymbol(exports, "__metadata", 107455 /* Value */); + } + } + if (requestedExternalEmitHelpers & 4096 /* HasParamDecorators */) { + verifyHelperSymbol(exports, "__param", 107455 /* Value */); + } + if (requestedExternalEmitHelpers & 8192 /* HasAsyncFunctions */) { + verifyHelperSymbol(exports, "__awaiter", 107455 /* Value */); + if (languageVersion < 2 /* ES6 */) { + verifyHelperSymbol(exports, "__generator", 107455 /* Value */); + } + } + } + } + } + function verifyHelperSymbol(symbols, name, meaning) { + var symbol = getSymbol(symbols, ts.escapeIdentifier(name), meaning); + if (!symbol) { + error(/*location*/ undefined, ts.Diagnostics.Module_0_has_no_exported_member_1, ts.externalHelpersModuleNameText, name); + } + } + function createInstantiatedPromiseLikeType() { + var promiseLikeType = getGlobalPromiseLikeType(); + if (promiseLikeType !== emptyGenericType) { + return createTypeReference(promiseLikeType, [anyType]); + } + return emptyObjectType; + } + function createThenableType() { + // build the thenable type that is used to verify against a non-promise "thenable" operand to `await`. + var thenPropertySymbol = createSymbol(67108864 /* Transient */ | 4 /* Property */, "then"); + getSymbolLinks(thenPropertySymbol).type = globalFunctionType; + var thenableType = createObjectType(2097152 /* Anonymous */); + thenableType.properties = [thenPropertySymbol]; + thenableType.members = createSymbolTable(thenableType.properties); + thenableType.callSignatures = []; + thenableType.constructSignatures = []; + return thenableType; + } + // GRAMMAR CHECKING + function checkGrammarDecorators(node) { + if (!node.decorators) { + return false; + } + if (!ts.nodeCanBeDecorated(node)) { + if (node.kind === 147 /* MethodDeclaration */ && !ts.nodeIsPresent(node.body)) { + return grammarErrorOnFirstToken(node, ts.Diagnostics.A_decorator_can_only_decorate_a_method_implementation_not_an_overload); + } + else { + return grammarErrorOnFirstToken(node, ts.Diagnostics.Decorators_are_not_valid_here); + } + } + else if (node.kind === 149 /* GetAccessor */ || node.kind === 150 /* SetAccessor */) { + var accessors = ts.getAllAccessorDeclarations(node.parent.members, node); + if (accessors.firstAccessor.decorators && node === accessors.secondAccessor) { + return grammarErrorOnFirstToken(node, ts.Diagnostics.Decorators_cannot_be_applied_to_multiple_get_Slashset_accessors_of_the_same_name); + } + } + return false; + } + function checkGrammarModifiers(node) { + var quickResult = reportObviousModifierErrors(node); + if (quickResult !== undefined) { + return quickResult; + } + var lastStatic, lastPrivate, lastProtected, lastDeclare, lastAsync, lastReadonly; + var flags = 0 /* None */; + for (var _i = 0, _a = node.modifiers; _i < _a.length; _i++) { + var modifier = _a[_i]; + if (modifier.kind !== 128 /* ReadonlyKeyword */) { + if (node.kind === 144 /* PropertySignature */ || node.kind === 146 /* MethodSignature */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_type_member, ts.tokenToString(modifier.kind)); + } + if (node.kind === 153 /* IndexSignature */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_an_index_signature, ts.tokenToString(modifier.kind)); + } + } + switch (modifier.kind) { + case 74 /* ConstKeyword */: + if (node.kind !== 224 /* EnumDeclaration */ && node.parent.kind === 221 /* ClassDeclaration */) { + return grammarErrorOnNode(node, ts.Diagnostics.A_class_member_cannot_have_the_0_keyword, ts.tokenToString(74 /* ConstKeyword */)); + } + break; + case 112 /* PublicKeyword */: + case 111 /* ProtectedKeyword */: + case 110 /* PrivateKeyword */: + var text = visibilityToString(ts.modifierToFlag(modifier.kind)); + if (modifier.kind === 111 /* ProtectedKeyword */) { + lastProtected = modifier; + } + else if (modifier.kind === 110 /* PrivateKeyword */) { + lastPrivate = modifier; + } + if (flags & 28 /* AccessibilityModifier */) { + return grammarErrorOnNode(modifier, ts.Diagnostics.Accessibility_modifier_already_seen); + } + else if (flags & 32 /* Static */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, text, "static"); + } + else if (flags & 64 /* Readonly */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, text, "readonly"); + } + else if (flags & 256 /* Async */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, text, "async"); + } + else if (node.parent.kind === 226 /* ModuleBlock */ || node.parent.kind === 256 /* SourceFile */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_module_or_namespace_element, text); + } + else if (flags & 128 /* Abstract */) { + if (modifier.kind === 110 /* PrivateKeyword */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_be_used_with_1_modifier, text, "abstract"); + } + else { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, text, "abstract"); + } + } + flags |= ts.modifierToFlag(modifier.kind); + break; + case 113 /* StaticKeyword */: + if (flags & 32 /* Static */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_already_seen, "static"); + } + else if (flags & 64 /* Readonly */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, "static", "readonly"); + } + else if (flags & 256 /* Async */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, "static", "async"); + } + else if (node.parent.kind === 226 /* ModuleBlock */ || node.parent.kind === 256 /* SourceFile */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_module_or_namespace_element, "static"); + } + else if (node.kind === 142 /* Parameter */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_parameter, "static"); + } + else if (flags & 128 /* Abstract */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "static", "abstract"); + } + flags |= 32 /* Static */; + lastStatic = modifier; + break; + case 128 /* ReadonlyKeyword */: + if (flags & 64 /* Readonly */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_already_seen, "readonly"); + } + else if (node.kind !== 145 /* PropertyDeclaration */ && node.kind !== 144 /* PropertySignature */ && node.kind !== 153 /* IndexSignature */ && node.kind !== 142 /* Parameter */) { + // If node.kind === SyntaxKind.Parameter, checkParameter report an error if it's not a parameter property. + return grammarErrorOnNode(modifier, ts.Diagnostics.readonly_modifier_can_only_appear_on_a_property_declaration_or_index_signature); + } + flags |= 64 /* Readonly */; + lastReadonly = modifier; + break; + case 82 /* ExportKeyword */: + if (flags & 1 /* Export */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_already_seen, "export"); + } + else if (flags & 2 /* Ambient */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, "export", "declare"); + } + else if (flags & 128 /* Abstract */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, "export", "abstract"); + } + else if (flags & 256 /* Async */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, "export", "async"); + } + else if (node.parent.kind === 221 /* ClassDeclaration */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_class_element, "export"); + } + else if (node.kind === 142 /* Parameter */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_parameter, "export"); + } + flags |= 1 /* Export */; + break; + case 122 /* DeclareKeyword */: + if (flags & 2 /* Ambient */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_already_seen, "declare"); + } + else if (flags & 256 /* Async */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_be_used_in_an_ambient_context, "async"); + } + else if (node.parent.kind === 221 /* ClassDeclaration */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_class_element, "declare"); + } + else if (node.kind === 142 /* Parameter */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_parameter, "declare"); + } + else if (ts.isInAmbientContext(node.parent) && node.parent.kind === 226 /* ModuleBlock */) { + return grammarErrorOnNode(modifier, ts.Diagnostics.A_declare_modifier_cannot_be_used_in_an_already_ambient_context); + } + flags |= 2 /* Ambient */; + lastDeclare = modifier; + break; + case 115 /* AbstractKeyword */: + if (flags & 128 /* Abstract */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_already_seen, "abstract"); + } + if (node.kind !== 221 /* ClassDeclaration */) { + if (node.kind !== 147 /* MethodDeclaration */ && + node.kind !== 145 /* PropertyDeclaration */ && + node.kind !== 149 /* GetAccessor */ && + node.kind !== 150 /* SetAccessor */) { + return grammarErrorOnNode(modifier, ts.Diagnostics.abstract_modifier_can_only_appear_on_a_class_method_or_property_declaration); + } + if (!(node.parent.kind === 221 /* ClassDeclaration */ && ts.getModifierFlags(node.parent) & 128 /* Abstract */)) { + return grammarErrorOnNode(modifier, ts.Diagnostics.Abstract_methods_can_only_appear_within_an_abstract_class); + } + if (flags & 32 /* Static */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "static", "abstract"); + } + if (flags & 8 /* Private */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "private", "abstract"); + } + } + flags |= 128 /* Abstract */; + break; + case 118 /* AsyncKeyword */: + if (flags & 256 /* Async */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_already_seen, "async"); + } + else if (flags & 2 /* Ambient */ || ts.isInAmbientContext(node.parent)) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_be_used_in_an_ambient_context, "async"); + } + else if (node.kind === 142 /* Parameter */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_parameter, "async"); + } + flags |= 256 /* Async */; + lastAsync = modifier; + break; + } + } + if (node.kind === 148 /* Constructor */) { + if (flags & 32 /* Static */) { + return grammarErrorOnNode(lastStatic, ts.Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "static"); + } + if (flags & 128 /* Abstract */) { + return grammarErrorOnNode(lastStatic, ts.Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "abstract"); + } + else if (flags & 256 /* Async */) { + return grammarErrorOnNode(lastAsync, ts.Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "async"); + } + else if (flags & 64 /* Readonly */) { + return grammarErrorOnNode(lastReadonly, ts.Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "readonly"); + } + return; + } + else if ((node.kind === 230 /* ImportDeclaration */ || node.kind === 229 /* ImportEqualsDeclaration */) && flags & 2 /* Ambient */) { + return grammarErrorOnNode(lastDeclare, ts.Diagnostics.A_0_modifier_cannot_be_used_with_an_import_declaration, "declare"); + } + else if (node.kind === 142 /* Parameter */ && (flags & 92 /* ParameterPropertyModifier */) && ts.isBindingPattern(node.name)) { + return grammarErrorOnNode(node, ts.Diagnostics.A_parameter_property_may_not_be_declared_using_a_binding_pattern); + } + else if (node.kind === 142 /* Parameter */ && (flags & 92 /* ParameterPropertyModifier */) && node.dotDotDotToken) { + return grammarErrorOnNode(node, ts.Diagnostics.A_parameter_property_cannot_be_declared_using_a_rest_parameter); + } + if (flags & 256 /* Async */) { + return checkGrammarAsyncModifier(node, lastAsync); + } + } + /** + * true | false: Early return this value from checkGrammarModifiers. + * undefined: Need to do full checking on the modifiers. + */ + function reportObviousModifierErrors(node) { + return !node.modifiers + ? false + : shouldReportBadModifier(node) + ? grammarErrorOnFirstToken(node, ts.Diagnostics.Modifiers_cannot_appear_here) + : undefined; + } + function shouldReportBadModifier(node) { + switch (node.kind) { + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + case 148 /* Constructor */: + case 145 /* PropertyDeclaration */: + case 144 /* PropertySignature */: + case 147 /* MethodDeclaration */: + case 146 /* MethodSignature */: + case 153 /* IndexSignature */: + case 225 /* ModuleDeclaration */: + case 230 /* ImportDeclaration */: + case 229 /* ImportEqualsDeclaration */: + case 236 /* ExportDeclaration */: + case 235 /* ExportAssignment */: + case 179 /* FunctionExpression */: + case 180 /* ArrowFunction */: + case 142 /* Parameter */: + return false; + default: + if (node.parent.kind === 226 /* ModuleBlock */ || node.parent.kind === 256 /* SourceFile */) { + return false; + } + switch (node.kind) { + case 220 /* FunctionDeclaration */: + return nodeHasAnyModifiersExcept(node, 118 /* AsyncKeyword */); + case 221 /* ClassDeclaration */: + return nodeHasAnyModifiersExcept(node, 115 /* AbstractKeyword */); + case 222 /* InterfaceDeclaration */: + case 200 /* VariableStatement */: + case 223 /* TypeAliasDeclaration */: + return true; + case 224 /* EnumDeclaration */: + return nodeHasAnyModifiersExcept(node, 74 /* ConstKeyword */); + default: + ts.Debug.fail(); + return false; + } + } + } + function nodeHasAnyModifiersExcept(node, allowedModifier) { + return node.modifiers.length > 1 || node.modifiers[0].kind !== allowedModifier; + } + function checkGrammarAsyncModifier(node, asyncModifier) { + switch (node.kind) { + case 147 /* MethodDeclaration */: + case 220 /* FunctionDeclaration */: + case 179 /* FunctionExpression */: + case 180 /* ArrowFunction */: + if (!node.asteriskToken) { + return false; + } + break; + } + return grammarErrorOnNode(asyncModifier, ts.Diagnostics._0_modifier_cannot_be_used_here, "async"); + } + function checkGrammarForDisallowedTrailingComma(list) { + if (list && list.hasTrailingComma) { + var start = list.end - ",".length; + var end = list.end; + var sourceFile = ts.getSourceFileOfNode(list[0]); + return grammarErrorAtPos(sourceFile, start, end - start, ts.Diagnostics.Trailing_comma_not_allowed); + } + } + function checkGrammarTypeParameterList(node, typeParameters, file) { + if (checkGrammarForDisallowedTrailingComma(typeParameters)) { + return true; + } + if (typeParameters && typeParameters.length === 0) { + var start = typeParameters.pos - "<".length; + var end = ts.skipTrivia(file.text, typeParameters.end) + ">".length; + return grammarErrorAtPos(file, start, end - start, ts.Diagnostics.Type_parameter_list_cannot_be_empty); + } + } + function checkGrammarParameterList(parameters) { + var seenOptionalParameter = false; + var parameterCount = parameters.length; + for (var i = 0; i < parameterCount; i++) { + var parameter = parameters[i]; + if (parameter.dotDotDotToken) { + if (i !== (parameterCount - 1)) { + return grammarErrorOnNode(parameter.dotDotDotToken, ts.Diagnostics.A_rest_parameter_must_be_last_in_a_parameter_list); + } + if (ts.isBindingPattern(parameter.name)) { + return grammarErrorOnNode(parameter.name, ts.Diagnostics.A_rest_element_cannot_contain_a_binding_pattern); + } + if (parameter.questionToken) { + return grammarErrorOnNode(parameter.questionToken, ts.Diagnostics.A_rest_parameter_cannot_be_optional); + } + if (parameter.initializer) { + return grammarErrorOnNode(parameter.name, ts.Diagnostics.A_rest_parameter_cannot_have_an_initializer); + } + } + else if (parameter.questionToken) { + seenOptionalParameter = true; + if (parameter.initializer) { + return grammarErrorOnNode(parameter.name, ts.Diagnostics.Parameter_cannot_have_question_mark_and_initializer); + } + } + else if (seenOptionalParameter && !parameter.initializer) { + return grammarErrorOnNode(parameter.name, ts.Diagnostics.A_required_parameter_cannot_follow_an_optional_parameter); + } + } + } + function checkGrammarFunctionLikeDeclaration(node) { + // Prevent cascading error by short-circuit + var file = ts.getSourceFileOfNode(node); + return checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarTypeParameterList(node, node.typeParameters, file) || + checkGrammarParameterList(node.parameters) || checkGrammarArrowFunction(node, file); + } + function checkGrammarArrowFunction(node, file) { + if (node.kind === 180 /* ArrowFunction */) { + var arrowFunction = node; + var startLine = ts.getLineAndCharacterOfPosition(file, arrowFunction.equalsGreaterThanToken.pos).line; + var endLine = ts.getLineAndCharacterOfPosition(file, arrowFunction.equalsGreaterThanToken.end).line; + if (startLine !== endLine) { + return grammarErrorOnNode(arrowFunction.equalsGreaterThanToken, ts.Diagnostics.Line_terminator_not_permitted_before_arrow); + } + } + return false; + } + function checkGrammarIndexSignatureParameters(node) { + var parameter = node.parameters[0]; + if (node.parameters.length !== 1) { + if (parameter) { + return grammarErrorOnNode(parameter.name, ts.Diagnostics.An_index_signature_must_have_exactly_one_parameter); + } + else { + return grammarErrorOnNode(node, ts.Diagnostics.An_index_signature_must_have_exactly_one_parameter); + } + } + if (parameter.dotDotDotToken) { + return grammarErrorOnNode(parameter.dotDotDotToken, ts.Diagnostics.An_index_signature_cannot_have_a_rest_parameter); + } + if (ts.getModifierFlags(parameter) !== 0) { + return grammarErrorOnNode(parameter.name, ts.Diagnostics.An_index_signature_parameter_cannot_have_an_accessibility_modifier); + } + if (parameter.questionToken) { + return grammarErrorOnNode(parameter.questionToken, ts.Diagnostics.An_index_signature_parameter_cannot_have_a_question_mark); + } + if (parameter.initializer) { + return grammarErrorOnNode(parameter.name, ts.Diagnostics.An_index_signature_parameter_cannot_have_an_initializer); + } + if (!parameter.type) { + return grammarErrorOnNode(parameter.name, ts.Diagnostics.An_index_signature_parameter_must_have_a_type_annotation); + } + if (parameter.type.kind !== 132 /* StringKeyword */ && parameter.type.kind !== 130 /* NumberKeyword */) { + return grammarErrorOnNode(parameter.name, ts.Diagnostics.An_index_signature_parameter_type_must_be_string_or_number); + } + if (!node.type) { + return grammarErrorOnNode(node, ts.Diagnostics.An_index_signature_must_have_a_type_annotation); + } + } + function checkGrammarIndexSignature(node) { + // Prevent cascading error by short-circuit + return checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarIndexSignatureParameters(node); + } + function checkGrammarForAtLeastOneTypeArgument(node, typeArguments) { + if (typeArguments && typeArguments.length === 0) { + var sourceFile = ts.getSourceFileOfNode(node); + var start = typeArguments.pos - "<".length; + var end = ts.skipTrivia(sourceFile.text, typeArguments.end) + ">".length; + return grammarErrorAtPos(sourceFile, start, end - start, ts.Diagnostics.Type_argument_list_cannot_be_empty); + } + } + function checkGrammarTypeArguments(node, typeArguments) { + return checkGrammarForDisallowedTrailingComma(typeArguments) || + checkGrammarForAtLeastOneTypeArgument(node, typeArguments); + } + function checkGrammarForOmittedArgument(node, args) { + if (args) { + var sourceFile = ts.getSourceFileOfNode(node); + for (var _i = 0, args_4 = args; _i < args_4.length; _i++) { + var arg = args_4[_i]; + if (arg.kind === 193 /* OmittedExpression */) { + return grammarErrorAtPos(sourceFile, arg.pos, 0, ts.Diagnostics.Argument_expression_expected); + } + } + } + } + function checkGrammarArguments(node, args) { + return checkGrammarForOmittedArgument(node, args); + } + function checkGrammarHeritageClause(node) { + var types = node.types; + if (checkGrammarForDisallowedTrailingComma(types)) { + return true; + } + if (types && types.length === 0) { + var listType = ts.tokenToString(node.token); + var sourceFile = ts.getSourceFileOfNode(node); + return grammarErrorAtPos(sourceFile, types.pos, 0, ts.Diagnostics._0_list_cannot_be_empty, listType); + } + } + function checkGrammarClassDeclarationHeritageClauses(node) { + var seenExtendsClause = false; + var seenImplementsClause = false; + if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node) && node.heritageClauses) { + for (var _i = 0, _a = node.heritageClauses; _i < _a.length; _i++) { + var heritageClause = _a[_i]; + if (heritageClause.token === 83 /* ExtendsKeyword */) { + if (seenExtendsClause) { + return grammarErrorOnFirstToken(heritageClause, ts.Diagnostics.extends_clause_already_seen); + } + if (seenImplementsClause) { + return grammarErrorOnFirstToken(heritageClause, ts.Diagnostics.extends_clause_must_precede_implements_clause); + } + if (heritageClause.types.length > 1) { + return grammarErrorOnFirstToken(heritageClause.types[1], ts.Diagnostics.Classes_can_only_extend_a_single_class); + } + seenExtendsClause = true; + } + else { + ts.Debug.assert(heritageClause.token === 106 /* ImplementsKeyword */); + if (seenImplementsClause) { + return grammarErrorOnFirstToken(heritageClause, ts.Diagnostics.implements_clause_already_seen); + } + seenImplementsClause = true; + } + // Grammar checking heritageClause inside class declaration + checkGrammarHeritageClause(heritageClause); + } + } + } + function checkGrammarInterfaceDeclaration(node) { + var seenExtendsClause = false; + if (node.heritageClauses) { + for (var _i = 0, _a = node.heritageClauses; _i < _a.length; _i++) { + var heritageClause = _a[_i]; + if (heritageClause.token === 83 /* ExtendsKeyword */) { + if (seenExtendsClause) { + return grammarErrorOnFirstToken(heritageClause, ts.Diagnostics.extends_clause_already_seen); + } + seenExtendsClause = true; + } + else { + ts.Debug.assert(heritageClause.token === 106 /* ImplementsKeyword */); + return grammarErrorOnFirstToken(heritageClause, ts.Diagnostics.Interface_declaration_cannot_have_implements_clause); + } + // Grammar checking heritageClause inside class declaration + checkGrammarHeritageClause(heritageClause); + } + } + return false; + } + function checkGrammarComputedPropertyName(node) { + // If node is not a computedPropertyName, just skip the grammar checking + if (node.kind !== 140 /* ComputedPropertyName */) { + return false; + } + var computedPropertyName = node; + if (computedPropertyName.expression.kind === 187 /* BinaryExpression */ && computedPropertyName.expression.operatorToken.kind === 24 /* CommaToken */) { + return grammarErrorOnNode(computedPropertyName.expression, ts.Diagnostics.A_comma_expression_is_not_allowed_in_a_computed_property_name); + } + } + function checkGrammarForGenerator(node) { + if (node.asteriskToken) { + ts.Debug.assert(node.kind === 220 /* FunctionDeclaration */ || + node.kind === 179 /* FunctionExpression */ || + node.kind === 147 /* MethodDeclaration */); + if (ts.isInAmbientContext(node)) { + return grammarErrorOnNode(node.asteriskToken, ts.Diagnostics.Generators_are_not_allowed_in_an_ambient_context); + } + if (!node.body) { + return grammarErrorOnNode(node.asteriskToken, ts.Diagnostics.An_overload_signature_cannot_be_declared_as_a_generator); + } + if (languageVersion < 2 /* ES6 */) { + return grammarErrorOnNode(node.asteriskToken, ts.Diagnostics.Generators_are_only_available_when_targeting_ECMAScript_2015_or_higher); + } + } + } + function checkGrammarForInvalidQuestionMark(node, questionToken, message) { + if (questionToken) { + return grammarErrorOnNode(questionToken, message); + } + } + function checkGrammarObjectLiteralExpression(node, inDestructuring) { + var seen = ts.createMap(); + var Property = 1; + var GetAccessor = 2; + var SetAccessor = 4; + var GetOrSetAccessor = GetAccessor | SetAccessor; + for (var _i = 0, _a = node.properties; _i < _a.length; _i++) { + var prop = _a[_i]; + var name_24 = prop.name; + if (name_24.kind === 140 /* ComputedPropertyName */) { + // If the name is not a ComputedPropertyName, the grammar checking will skip it + checkGrammarComputedPropertyName(name_24); + } + if (prop.kind === 254 /* ShorthandPropertyAssignment */ && !inDestructuring && prop.objectAssignmentInitializer) { + // having objectAssignmentInitializer is only valid in ObjectAssignmentPattern + // outside of destructuring it is a syntax error + return grammarErrorOnNode(prop.equalsToken, ts.Diagnostics.can_only_be_used_in_an_object_literal_property_inside_a_destructuring_assignment); + } + // Modifiers are never allowed on properties except for 'async' on a method declaration + if (prop.modifiers) { + for (var _b = 0, _c = prop.modifiers; _b < _c.length; _b++) { + var mod = _c[_b]; + if (mod.kind !== 118 /* AsyncKeyword */ || prop.kind !== 147 /* MethodDeclaration */) { + grammarErrorOnNode(mod, ts.Diagnostics._0_modifier_cannot_be_used_here, ts.getTextOfNode(mod)); + } + } + } + // ECMA-262 11.1.5 Object Initializer + // If previous is not undefined then throw a SyntaxError exception if any of the following conditions are true + // a.This production is contained in strict code and IsDataDescriptor(previous) is true and + // IsDataDescriptor(propId.descriptor) is true. + // b.IsDataDescriptor(previous) is true and IsAccessorDescriptor(propId.descriptor) is true. + // c.IsAccessorDescriptor(previous) is true and IsDataDescriptor(propId.descriptor) is true. + // d.IsAccessorDescriptor(previous) is true and IsAccessorDescriptor(propId.descriptor) is true + // and either both previous and propId.descriptor have[[Get]] fields or both previous and propId.descriptor have[[Set]] fields + var currentKind = void 0; + if (prop.kind === 253 /* PropertyAssignment */ || prop.kind === 254 /* ShorthandPropertyAssignment */) { + // Grammar checking for computedPropertyName and shorthandPropertyAssignment + checkGrammarForInvalidQuestionMark(prop, prop.questionToken, ts.Diagnostics.An_object_member_cannot_be_declared_optional); + if (name_24.kind === 8 /* NumericLiteral */) { + checkGrammarNumericLiteral(name_24); + } + currentKind = Property; + } + else if (prop.kind === 147 /* MethodDeclaration */) { + currentKind = Property; + } + else if (prop.kind === 149 /* GetAccessor */) { + currentKind = GetAccessor; + } + else if (prop.kind === 150 /* SetAccessor */) { + currentKind = SetAccessor; + } + else { + ts.Debug.fail("Unexpected syntax kind:" + prop.kind); + } + var effectiveName = ts.getPropertyNameForPropertyNameNode(name_24); + if (effectiveName === undefined) { + continue; + } + if (!seen[effectiveName]) { + seen[effectiveName] = currentKind; + } + else { + var existingKind = seen[effectiveName]; + if (currentKind === Property && existingKind === Property) { + grammarErrorOnNode(name_24, ts.Diagnostics.Duplicate_identifier_0, ts.getTextOfNode(name_24)); + } + else if ((currentKind & GetOrSetAccessor) && (existingKind & GetOrSetAccessor)) { + if (existingKind !== GetOrSetAccessor && currentKind !== existingKind) { + seen[effectiveName] = currentKind | existingKind; + } + else { + return grammarErrorOnNode(name_24, ts.Diagnostics.An_object_literal_cannot_have_multiple_get_Slashset_accessors_with_the_same_name); + } + } + else { + return grammarErrorOnNode(name_24, ts.Diagnostics.An_object_literal_cannot_have_property_and_accessor_with_the_same_name); + } + } + } + } + function checkGrammarJsxElement(node) { + var seen = ts.createMap(); + for (var _i = 0, _a = node.attributes; _i < _a.length; _i++) { + var attr = _a[_i]; + if (attr.kind === 247 /* JsxSpreadAttribute */) { + continue; + } + var jsxAttr = attr; + var name_25 = jsxAttr.name; + if (!seen[name_25.text]) { + seen[name_25.text] = true; + } + else { + return grammarErrorOnNode(name_25, ts.Diagnostics.JSX_elements_cannot_have_multiple_attributes_with_the_same_name); + } + var initializer = jsxAttr.initializer; + if (initializer && initializer.kind === 248 /* JsxExpression */ && !initializer.expression) { + return grammarErrorOnNode(jsxAttr.initializer, ts.Diagnostics.JSX_attributes_must_only_be_assigned_a_non_empty_expression); + } + } + } + function checkGrammarForInOrForOfStatement(forInOrOfStatement) { + if (checkGrammarStatementInAmbientContext(forInOrOfStatement)) { + return true; + } + if (forInOrOfStatement.initializer.kind === 219 /* VariableDeclarationList */) { + var variableList = forInOrOfStatement.initializer; + if (!checkGrammarVariableDeclarationList(variableList)) { + var declarations = variableList.declarations; + // declarations.length can be zero if there is an error in variable declaration in for-of or for-in + // See http://www.ecma-international.org/ecma-262/6.0/#sec-for-in-and-for-of-statements for details + // For example: + // var let = 10; + // for (let of [1,2,3]) {} // this is invalid ES6 syntax + // for (let in [1,2,3]) {} // this is invalid ES6 syntax + // We will then want to skip on grammar checking on variableList declaration + if (!declarations.length) { + return false; + } + if (declarations.length > 1) { + var diagnostic = forInOrOfStatement.kind === 207 /* ForInStatement */ + ? ts.Diagnostics.Only_a_single_variable_declaration_is_allowed_in_a_for_in_statement + : ts.Diagnostics.Only_a_single_variable_declaration_is_allowed_in_a_for_of_statement; + return grammarErrorOnFirstToken(variableList.declarations[1], diagnostic); + } + var firstDeclaration = declarations[0]; + if (firstDeclaration.initializer) { + var diagnostic = forInOrOfStatement.kind === 207 /* ForInStatement */ + ? ts.Diagnostics.The_variable_declaration_of_a_for_in_statement_cannot_have_an_initializer + : ts.Diagnostics.The_variable_declaration_of_a_for_of_statement_cannot_have_an_initializer; + return grammarErrorOnNode(firstDeclaration.name, diagnostic); + } + if (firstDeclaration.type) { + var diagnostic = forInOrOfStatement.kind === 207 /* ForInStatement */ + ? ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_use_a_type_annotation + : ts.Diagnostics.The_left_hand_side_of_a_for_of_statement_cannot_use_a_type_annotation; + return grammarErrorOnNode(firstDeclaration, diagnostic); + } + } + } + return false; + } + function checkGrammarAccessor(accessor) { + var kind = accessor.kind; + if (languageVersion < 1 /* ES5 */) { + return grammarErrorOnNode(accessor.name, ts.Diagnostics.Accessors_are_only_available_when_targeting_ECMAScript_5_and_higher); + } + else if (ts.isInAmbientContext(accessor)) { + return grammarErrorOnNode(accessor.name, ts.Diagnostics.An_accessor_cannot_be_declared_in_an_ambient_context); + } + else if (accessor.body === undefined && !(ts.getModifierFlags(accessor) & 128 /* Abstract */)) { + return grammarErrorAtPos(ts.getSourceFileOfNode(accessor), accessor.end - 1, ";".length, ts.Diagnostics._0_expected, "{"); + } + else if (accessor.typeParameters) { + return grammarErrorOnNode(accessor.name, ts.Diagnostics.An_accessor_cannot_have_type_parameters); + } + else if (!doesAccessorHaveCorrectParameterCount(accessor)) { + return grammarErrorOnNode(accessor.name, kind === 149 /* GetAccessor */ ? + ts.Diagnostics.A_get_accessor_cannot_have_parameters : + ts.Diagnostics.A_set_accessor_must_have_exactly_one_parameter); + } + else if (kind === 150 /* SetAccessor */) { + if (accessor.type) { + return grammarErrorOnNode(accessor.name, ts.Diagnostics.A_set_accessor_cannot_have_a_return_type_annotation); + } + else { + var parameter = accessor.parameters[0]; + if (parameter.dotDotDotToken) { + return grammarErrorOnNode(parameter.dotDotDotToken, ts.Diagnostics.A_set_accessor_cannot_have_rest_parameter); + } + else if (parameter.questionToken) { + return grammarErrorOnNode(parameter.questionToken, ts.Diagnostics.A_set_accessor_cannot_have_an_optional_parameter); + } + else if (parameter.initializer) { + return grammarErrorOnNode(accessor.name, ts.Diagnostics.A_set_accessor_parameter_cannot_have_an_initializer); + } + } + } + } + /** Does the accessor have the right number of parameters? + + A get accessor has no parameters or a single `this` parameter. + A set accessor has one parameter or a `this` parameter and one more parameter */ + function doesAccessorHaveCorrectParameterCount(accessor) { + return getAccessorThisParameter(accessor) || accessor.parameters.length === (accessor.kind === 149 /* GetAccessor */ ? 0 : 1); + } + function getAccessorThisParameter(accessor) { + if (accessor.parameters.length === (accessor.kind === 149 /* GetAccessor */ ? 1 : 2)) { + return ts.getThisParameter(accessor); + } + } + function checkGrammarForNonSymbolComputedProperty(node, message) { + if (ts.isDynamicName(node)) { + return grammarErrorOnNode(node, message); + } + } + function checkGrammarMethod(node) { + if (checkGrammarDisallowedModifiersOnObjectLiteralExpressionMethod(node) || + checkGrammarFunctionLikeDeclaration(node) || + checkGrammarForGenerator(node)) { + return true; + } + if (node.parent.kind === 171 /* ObjectLiteralExpression */) { + if (checkGrammarForInvalidQuestionMark(node, node.questionToken, ts.Diagnostics.An_object_member_cannot_be_declared_optional)) { + return true; + } + else if (node.body === undefined) { + return grammarErrorAtPos(ts.getSourceFileOfNode(node), node.end - 1, ";".length, ts.Diagnostics._0_expected, "{"); + } + } + if (ts.isClassLike(node.parent)) { + // Technically, computed properties in ambient contexts is disallowed + // for property declarations and accessors too, not just methods. + // However, property declarations disallow computed names in general, + // and accessors are not allowed in ambient contexts in general, + // so this error only really matters for methods. + if (ts.isInAmbientContext(node)) { + return checkGrammarForNonSymbolComputedProperty(node.name, ts.Diagnostics.A_computed_property_name_in_an_ambient_context_must_directly_refer_to_a_built_in_symbol); + } + else if (!node.body) { + return checkGrammarForNonSymbolComputedProperty(node.name, ts.Diagnostics.A_computed_property_name_in_a_method_overload_must_directly_refer_to_a_built_in_symbol); + } + } + else if (node.parent.kind === 222 /* InterfaceDeclaration */) { + return checkGrammarForNonSymbolComputedProperty(node.name, ts.Diagnostics.A_computed_property_name_in_an_interface_must_directly_refer_to_a_built_in_symbol); + } + else if (node.parent.kind === 159 /* TypeLiteral */) { + return checkGrammarForNonSymbolComputedProperty(node.name, ts.Diagnostics.A_computed_property_name_in_a_type_literal_must_directly_refer_to_a_built_in_symbol); + } + } + function checkGrammarBreakOrContinueStatement(node) { + var current = node; + while (current) { + if (ts.isFunctionLike(current)) { + return grammarErrorOnNode(node, ts.Diagnostics.Jump_target_cannot_cross_function_boundary); + } + switch (current.kind) { + case 214 /* LabeledStatement */: + if (node.label && current.label.text === node.label.text) { + // found matching label - verify that label usage is correct + // continue can only target labels that are on iteration statements + var isMisplacedContinueLabel = node.kind === 209 /* ContinueStatement */ + && !ts.isIterationStatement(current.statement, /*lookInLabeledStatement*/ true); + if (isMisplacedContinueLabel) { + return grammarErrorOnNode(node, ts.Diagnostics.A_continue_statement_can_only_jump_to_a_label_of_an_enclosing_iteration_statement); + } + return false; + } + break; + case 213 /* SwitchStatement */: + if (node.kind === 210 /* BreakStatement */ && !node.label) { + // unlabeled break within switch statement - ok + return false; + } + break; + default: + if (ts.isIterationStatement(current, /*lookInLabeledStatement*/ false) && !node.label) { + // unlabeled break or continue within iteration statement - ok + return false; + } + break; + } + current = current.parent; + } + if (node.label) { + var message = node.kind === 210 /* BreakStatement */ + ? ts.Diagnostics.A_break_statement_can_only_jump_to_a_label_of_an_enclosing_statement + : ts.Diagnostics.A_continue_statement_can_only_jump_to_a_label_of_an_enclosing_iteration_statement; + return grammarErrorOnNode(node, message); + } + else { + var message = node.kind === 210 /* BreakStatement */ + ? ts.Diagnostics.A_break_statement_can_only_be_used_within_an_enclosing_iteration_or_switch_statement + : ts.Diagnostics.A_continue_statement_can_only_be_used_within_an_enclosing_iteration_statement; + return grammarErrorOnNode(node, message); + } + } + function checkGrammarBindingElement(node) { + if (node.dotDotDotToken) { + var elements = node.parent.elements; + if (node !== ts.lastOrUndefined(elements)) { + return grammarErrorOnNode(node, ts.Diagnostics.A_rest_element_must_be_last_in_an_array_destructuring_pattern); + } + if (node.name.kind === 168 /* ArrayBindingPattern */ || node.name.kind === 167 /* ObjectBindingPattern */) { + return grammarErrorOnNode(node.name, ts.Diagnostics.A_rest_element_cannot_contain_a_binding_pattern); + } + if (node.initializer) { + // Error on equals token which immediate precedes the initializer + return grammarErrorAtPos(ts.getSourceFileOfNode(node), node.initializer.pos - 1, 1, ts.Diagnostics.A_rest_element_cannot_have_an_initializer); + } + } + } + function isStringOrNumberLiteralExpression(expr) { + return expr.kind === 9 /* StringLiteral */ || expr.kind === 8 /* NumericLiteral */ || + expr.kind === 185 /* PrefixUnaryExpression */ && expr.operator === 36 /* MinusToken */ && + expr.operand.kind === 8 /* NumericLiteral */; + } + function checkGrammarVariableDeclaration(node) { + if (node.parent.parent.kind !== 207 /* ForInStatement */ && node.parent.parent.kind !== 208 /* ForOfStatement */) { + if (ts.isInAmbientContext(node)) { + if (node.initializer) { + if (ts.isConst(node) && !node.type) { + if (!isStringOrNumberLiteralExpression(node.initializer)) { + return grammarErrorOnNode(node.initializer, ts.Diagnostics.A_const_initializer_in_an_ambient_context_must_be_a_string_or_numeric_literal); + } + } + else { + // Error on equals token which immediate precedes the initializer + var equalsTokenLength = "=".length; + return grammarErrorAtPos(ts.getSourceFileOfNode(node), node.initializer.pos - equalsTokenLength, equalsTokenLength, ts.Diagnostics.Initializers_are_not_allowed_in_ambient_contexts); + } + } + if (node.initializer && !(ts.isConst(node) && isStringOrNumberLiteralExpression(node.initializer))) { + // Error on equals token which immediate precedes the initializer + var equalsTokenLength = "=".length; + return grammarErrorAtPos(ts.getSourceFileOfNode(node), node.initializer.pos - equalsTokenLength, equalsTokenLength, ts.Diagnostics.Initializers_are_not_allowed_in_ambient_contexts); + } + } + else if (!node.initializer) { + if (ts.isBindingPattern(node.name) && !ts.isBindingPattern(node.parent)) { + return grammarErrorOnNode(node, ts.Diagnostics.A_destructuring_declaration_must_have_an_initializer); + } + if (ts.isConst(node)) { + return grammarErrorOnNode(node, ts.Diagnostics.const_declarations_must_be_initialized); + } + } + } + var checkLetConstNames = (ts.isLet(node) || ts.isConst(node)); + // 1. LexicalDeclaration : LetOrConst BindingList ; + // It is a Syntax Error if the BoundNames of BindingList contains "let". + // 2. ForDeclaration: ForDeclaration : LetOrConst ForBinding + // It is a Syntax Error if the BoundNames of ForDeclaration contains "let". + // It is a SyntaxError if a VariableDeclaration or VariableDeclarationNoIn occurs within strict code + // and its Identifier is eval or arguments + return checkLetConstNames && checkGrammarNameInLetOrConstDeclarations(node.name); + } + function checkGrammarNameInLetOrConstDeclarations(name) { + if (name.kind === 69 /* Identifier */) { + if (name.originalKeywordKind === 108 /* LetKeyword */) { + return grammarErrorOnNode(name, ts.Diagnostics.let_is_not_allowed_to_be_used_as_a_name_in_let_or_const_declarations); + } + } + else { + var elements = name.elements; + for (var _i = 0, elements_2 = elements; _i < elements_2.length; _i++) { + var element = elements_2[_i]; + if (!ts.isOmittedExpression(element)) { + checkGrammarNameInLetOrConstDeclarations(element.name); + } + } + } + } + function checkGrammarVariableDeclarationList(declarationList) { + var declarations = declarationList.declarations; + if (checkGrammarForDisallowedTrailingComma(declarationList.declarations)) { + return true; + } + if (!declarationList.declarations.length) { + return grammarErrorAtPos(ts.getSourceFileOfNode(declarationList), declarations.pos, declarations.end - declarations.pos, ts.Diagnostics.Variable_declaration_list_cannot_be_empty); + } + } + function allowLetAndConstDeclarations(parent) { + switch (parent.kind) { + case 203 /* IfStatement */: + case 204 /* DoStatement */: + case 205 /* WhileStatement */: + case 212 /* WithStatement */: + case 206 /* ForStatement */: + case 207 /* ForInStatement */: + case 208 /* ForOfStatement */: + return false; + case 214 /* LabeledStatement */: + return allowLetAndConstDeclarations(parent.parent); + } + return true; + } + function checkGrammarForDisallowedLetOrConstStatement(node) { + if (!allowLetAndConstDeclarations(node.parent)) { + if (ts.isLet(node.declarationList)) { + return grammarErrorOnNode(node, ts.Diagnostics.let_declarations_can_only_be_declared_inside_a_block); + } + else if (ts.isConst(node.declarationList)) { + return grammarErrorOnNode(node, ts.Diagnostics.const_declarations_can_only_be_declared_inside_a_block); + } + } + } + function hasParseDiagnostics(sourceFile) { + return sourceFile.parseDiagnostics.length > 0; + } + function grammarErrorOnFirstToken(node, message, arg0, arg1, arg2) { + var sourceFile = ts.getSourceFileOfNode(node); + if (!hasParseDiagnostics(sourceFile)) { + var span_4 = ts.getSpanOfTokenAtPosition(sourceFile, node.pos); + diagnostics.add(ts.createFileDiagnostic(sourceFile, span_4.start, span_4.length, message, arg0, arg1, arg2)); + return true; + } + } + function grammarErrorAtPos(sourceFile, start, length, message, arg0, arg1, arg2) { + if (!hasParseDiagnostics(sourceFile)) { + diagnostics.add(ts.createFileDiagnostic(sourceFile, start, length, message, arg0, arg1, arg2)); + return true; + } + } + function grammarErrorOnNode(node, message, arg0, arg1, arg2) { + var sourceFile = ts.getSourceFileOfNode(node); + if (!hasParseDiagnostics(sourceFile)) { + diagnostics.add(ts.createDiagnosticForNode(node, message, arg0, arg1, arg2)); + return true; + } + } + function checkGrammarConstructorTypeParameters(node) { + if (node.typeParameters) { + return grammarErrorAtPos(ts.getSourceFileOfNode(node), node.typeParameters.pos, node.typeParameters.end - node.typeParameters.pos, ts.Diagnostics.Type_parameters_cannot_appear_on_a_constructor_declaration); + } + } + function checkGrammarConstructorTypeAnnotation(node) { + if (node.type) { + return grammarErrorOnNode(node.type, ts.Diagnostics.Type_annotation_cannot_appear_on_a_constructor_declaration); + } + } + function checkGrammarProperty(node) { + if (ts.isClassLike(node.parent)) { + if (checkGrammarForNonSymbolComputedProperty(node.name, ts.Diagnostics.A_computed_property_name_in_a_class_property_declaration_must_directly_refer_to_a_built_in_symbol)) { + return true; + } + } + else if (node.parent.kind === 222 /* InterfaceDeclaration */) { + if (checkGrammarForNonSymbolComputedProperty(node.name, ts.Diagnostics.A_computed_property_name_in_an_interface_must_directly_refer_to_a_built_in_symbol)) { + return true; + } + if (node.initializer) { + return grammarErrorOnNode(node.initializer, ts.Diagnostics.An_interface_property_cannot_have_an_initializer); + } + } + else if (node.parent.kind === 159 /* TypeLiteral */) { + if (checkGrammarForNonSymbolComputedProperty(node.name, ts.Diagnostics.A_computed_property_name_in_a_type_literal_must_directly_refer_to_a_built_in_symbol)) { + return true; + } + if (node.initializer) { + return grammarErrorOnNode(node.initializer, ts.Diagnostics.A_type_literal_property_cannot_have_an_initializer); + } + } + if (ts.isInAmbientContext(node) && node.initializer) { + return grammarErrorOnFirstToken(node.initializer, ts.Diagnostics.Initializers_are_not_allowed_in_ambient_contexts); + } + } + function checkGrammarTopLevelElementForRequiredDeclareModifier(node) { + // A declare modifier is required for any top level .d.ts declaration except export=, export default, export as namespace + // interfaces and imports categories: + // + // DeclarationElement: + // ExportAssignment + // export_opt InterfaceDeclaration + // export_opt TypeAliasDeclaration + // export_opt ImportDeclaration + // export_opt ExternalImportDeclaration + // export_opt AmbientDeclaration + // + // TODO: The spec needs to be amended to reflect this grammar. + if (node.kind === 222 /* InterfaceDeclaration */ || + node.kind === 223 /* TypeAliasDeclaration */ || + node.kind === 230 /* ImportDeclaration */ || + node.kind === 229 /* ImportEqualsDeclaration */ || + node.kind === 236 /* ExportDeclaration */ || + node.kind === 235 /* ExportAssignment */ || + node.kind === 228 /* NamespaceExportDeclaration */ || + ts.getModifierFlags(node) & (2 /* Ambient */ | 1 /* Export */ | 512 /* Default */)) { + return false; + } + return grammarErrorOnFirstToken(node, ts.Diagnostics.A_declare_modifier_is_required_for_a_top_level_declaration_in_a_d_ts_file); + } + function checkGrammarTopLevelElementsForRequiredDeclareModifier(file) { + for (var _i = 0, _a = file.statements; _i < _a.length; _i++) { + var decl = _a[_i]; + if (ts.isDeclaration(decl) || decl.kind === 200 /* VariableStatement */) { + if (checkGrammarTopLevelElementForRequiredDeclareModifier(decl)) { + return true; + } + } + } + } + function checkGrammarSourceFile(node) { + return ts.isInAmbientContext(node) && checkGrammarTopLevelElementsForRequiredDeclareModifier(node); + } + function checkGrammarStatementInAmbientContext(node) { + if (ts.isInAmbientContext(node)) { + // An accessors is already reported about the ambient context + if (isAccessor(node.parent.kind)) { + return getNodeLinks(node).hasReportedStatementInAmbientContext = true; + } + // Find containing block which is either Block, ModuleBlock, SourceFile + var links = getNodeLinks(node); + if (!links.hasReportedStatementInAmbientContext && ts.isFunctionLike(node.parent)) { + return getNodeLinks(node).hasReportedStatementInAmbientContext = grammarErrorOnFirstToken(node, ts.Diagnostics.An_implementation_cannot_be_declared_in_ambient_contexts); + } + // We are either parented by another statement, or some sort of block. + // If we're in a block, we only want to really report an error once + // to prevent noisiness. So use a bit on the block to indicate if + // this has already been reported, and don't report if it has. + // + if (node.parent.kind === 199 /* Block */ || node.parent.kind === 226 /* ModuleBlock */ || node.parent.kind === 256 /* SourceFile */) { + var links_1 = getNodeLinks(node.parent); + // Check if the containing block ever report this error + if (!links_1.hasReportedStatementInAmbientContext) { + return links_1.hasReportedStatementInAmbientContext = grammarErrorOnFirstToken(node, ts.Diagnostics.Statements_are_not_allowed_in_ambient_contexts); + } + } + else { + } + } + } + function checkGrammarNumericLiteral(node) { + // Grammar checking + if (node.isOctalLiteral && languageVersion >= 1 /* ES5 */) { + return grammarErrorOnNode(node, ts.Diagnostics.Octal_literals_are_not_available_when_targeting_ECMAScript_5_and_higher); + } + } + function grammarErrorAfterFirstToken(node, message, arg0, arg1, arg2) { + var sourceFile = ts.getSourceFileOfNode(node); + if (!hasParseDiagnostics(sourceFile)) { + var span_5 = ts.getSpanOfTokenAtPosition(sourceFile, node.pos); + diagnostics.add(ts.createFileDiagnostic(sourceFile, ts.textSpanEnd(span_5), /*length*/ 0, message, arg0, arg1, arg2)); + return true; + } + } + function getAmbientModules() { + var result = []; + for (var sym in globals) { + if (ambientModuleSymbolRegex.test(sym)) { + result.push(globals[sym]); + } + } + return result; + } + } + ts.createTypeChecker = createTypeChecker; +})(ts || (ts = {})); +/// +/// +/// +/* @internal */ +var ts; +(function (ts) { + ; + /** + * This map contains information about the shape of each Node in "types.ts" pertaining to how + * each node should be traversed during a transformation. + * + * Each edge corresponds to a property in a Node subtype that should be traversed when visiting + * each child. The properties are assigned in the order in which traversal should occur. + * + * We only add entries for nodes that do not have a create/update pair defined in factory.ts + * + * NOTE: This needs to be kept up to date with changes to nodes in "types.ts". Currently, this + * map is not comprehensive. Only node edges relevant to tree transformation are + * currently defined. We may extend this to be more comprehensive, and eventually + * supplant the existing `forEachChild` implementation if performance is not + * significantly impacted. + */ + var nodeEdgeTraversalMap = ts.createMap((_a = {}, + _a[139 /* QualifiedName */] = [ + { name: "left", test: ts.isEntityName }, + { name: "right", test: ts.isIdentifier } + ], + _a[143 /* Decorator */] = [ + { name: "expression", test: ts.isLeftHandSideExpression } + ], + _a[177 /* TypeAssertionExpression */] = [ + { name: "type", test: ts.isTypeNode }, + { name: "expression", test: ts.isUnaryExpression } + ], + _a[195 /* AsExpression */] = [ + { name: "expression", test: ts.isExpression }, + { name: "type", test: ts.isTypeNode } + ], + _a[196 /* NonNullExpression */] = [ + { name: "expression", test: ts.isLeftHandSideExpression } + ], + _a[224 /* EnumDeclaration */] = [ + { name: "decorators", test: ts.isDecorator }, + { name: "modifiers", test: ts.isModifier }, + { name: "name", test: ts.isIdentifier }, + { name: "members", test: ts.isEnumMember } + ], + _a[225 /* ModuleDeclaration */] = [ + { name: "decorators", test: ts.isDecorator }, + { name: "modifiers", test: ts.isModifier }, + { name: "name", test: ts.isModuleName }, + { name: "body", test: ts.isModuleBody } + ], + _a[226 /* ModuleBlock */] = [ + { name: "statements", test: ts.isStatement } + ], + _a[229 /* ImportEqualsDeclaration */] = [ + { name: "decorators", test: ts.isDecorator }, + { name: "modifiers", test: ts.isModifier }, + { name: "name", test: ts.isIdentifier }, + { name: "moduleReference", test: ts.isModuleReference } + ], + _a[240 /* ExternalModuleReference */] = [ + { name: "expression", test: ts.isExpression, optional: true } + ], + _a[255 /* EnumMember */] = [ + { name: "name", test: ts.isPropertyName }, + { name: "initializer", test: ts.isExpression, optional: true, parenthesize: ts.parenthesizeExpressionForList } + ], + _a)); + function reduceNode(node, f, initial) { + return node ? f(initial, node) : initial; + } + /** + * Similar to `reduceLeft`, performs a reduction against each child of a node. + * NOTE: Unlike `forEachChild`, this does *not* visit every node. Only nodes added to the + * `nodeEdgeTraversalMap` above will be visited. + * + * @param node The node containing the children to reduce. + * @param f The callback function + * @param initial The initial value to supply to the reduction. + */ + function reduceEachChild(node, f, initial) { + if (node === undefined) { + return initial; + } + var kind = node.kind; + // No need to visit nodes with no children. + if ((kind > 0 /* FirstToken */ && kind <= 138 /* LastToken */)) { + return initial; + } + // We do not yet support types. + if ((kind >= 154 /* TypePredicate */ && kind <= 166 /* LiteralType */)) { + return initial; + } + var result = initial; + switch (node.kind) { + // Leaf nodes + case 198 /* SemicolonClassElement */: + case 201 /* EmptyStatement */: + case 193 /* OmittedExpression */: + case 217 /* DebuggerStatement */: + case 287 /* NotEmittedStatement */: + // No need to visit nodes with no children. + break; + // Names + case 140 /* ComputedPropertyName */: + result = reduceNode(node.expression, f, result); + break; + // Signature elements + case 142 /* Parameter */: + result = ts.reduceLeft(node.decorators, f, result); + result = ts.reduceLeft(node.modifiers, f, result); + result = reduceNode(node.name, f, result); + result = reduceNode(node.type, f, result); + result = reduceNode(node.initializer, f, result); + break; + case 143 /* Decorator */: + result = reduceNode(node.expression, f, result); + break; + // Type member + case 145 /* PropertyDeclaration */: + result = ts.reduceLeft(node.decorators, f, result); + result = ts.reduceLeft(node.modifiers, f, result); + result = reduceNode(node.name, f, result); + result = reduceNode(node.type, f, result); + result = reduceNode(node.initializer, f, result); + break; + case 147 /* MethodDeclaration */: + result = ts.reduceLeft(node.decorators, f, result); + result = ts.reduceLeft(node.modifiers, f, result); + result = reduceNode(node.name, f, result); + result = ts.reduceLeft(node.typeParameters, f, result); + result = ts.reduceLeft(node.parameters, f, result); + result = reduceNode(node.type, f, result); + result = reduceNode(node.body, f, result); + break; + case 148 /* Constructor */: + result = ts.reduceLeft(node.modifiers, f, result); + result = ts.reduceLeft(node.parameters, f, result); + result = reduceNode(node.body, f, result); + break; + case 149 /* GetAccessor */: + result = ts.reduceLeft(node.decorators, f, result); + result = ts.reduceLeft(node.modifiers, f, result); + result = reduceNode(node.name, f, result); + result = ts.reduceLeft(node.parameters, f, result); + result = reduceNode(node.type, f, result); + result = reduceNode(node.body, f, result); + break; + case 150 /* SetAccessor */: + result = ts.reduceLeft(node.decorators, f, result); + result = ts.reduceLeft(node.modifiers, f, result); + result = reduceNode(node.name, f, result); + result = ts.reduceLeft(node.parameters, f, result); + result = reduceNode(node.body, f, result); + break; + // Binding patterns + case 167 /* ObjectBindingPattern */: + case 168 /* ArrayBindingPattern */: + result = ts.reduceLeft(node.elements, f, result); + break; + case 169 /* BindingElement */: + result = reduceNode(node.propertyName, f, result); + result = reduceNode(node.name, f, result); + result = reduceNode(node.initializer, f, result); + break; + // Expression + case 170 /* ArrayLiteralExpression */: + result = ts.reduceLeft(node.elements, f, result); + break; + case 171 /* ObjectLiteralExpression */: + result = ts.reduceLeft(node.properties, f, result); + break; + case 172 /* PropertyAccessExpression */: + result = reduceNode(node.expression, f, result); + result = reduceNode(node.name, f, result); + break; + case 173 /* ElementAccessExpression */: + result = reduceNode(node.expression, f, result); + result = reduceNode(node.argumentExpression, f, result); + break; + case 174 /* CallExpression */: + result = reduceNode(node.expression, f, result); + result = ts.reduceLeft(node.typeArguments, f, result); + result = ts.reduceLeft(node.arguments, f, result); + break; + case 175 /* NewExpression */: + result = reduceNode(node.expression, f, result); + result = ts.reduceLeft(node.typeArguments, f, result); + result = ts.reduceLeft(node.arguments, f, result); + break; + case 176 /* TaggedTemplateExpression */: + result = reduceNode(node.tag, f, result); + result = reduceNode(node.template, f, result); + break; + case 179 /* FunctionExpression */: + result = ts.reduceLeft(node.modifiers, f, result); + result = reduceNode(node.name, f, result); + result = ts.reduceLeft(node.typeParameters, f, result); + result = ts.reduceLeft(node.parameters, f, result); + result = reduceNode(node.type, f, result); + result = reduceNode(node.body, f, result); + break; + case 180 /* ArrowFunction */: + result = ts.reduceLeft(node.modifiers, f, result); + result = ts.reduceLeft(node.typeParameters, f, result); + result = ts.reduceLeft(node.parameters, f, result); + result = reduceNode(node.type, f, result); + result = reduceNode(node.body, f, result); + break; + case 178 /* ParenthesizedExpression */: + case 181 /* DeleteExpression */: + case 182 /* TypeOfExpression */: + case 183 /* VoidExpression */: + case 184 /* AwaitExpression */: + case 190 /* YieldExpression */: + case 191 /* SpreadElementExpression */: + case 196 /* NonNullExpression */: + result = reduceNode(node.expression, f, result); + break; + case 185 /* PrefixUnaryExpression */: + case 186 /* PostfixUnaryExpression */: + result = reduceNode(node.operand, f, result); + break; + case 187 /* BinaryExpression */: + result = reduceNode(node.left, f, result); + result = reduceNode(node.right, f, result); + break; + case 188 /* ConditionalExpression */: + result = reduceNode(node.condition, f, result); + result = reduceNode(node.whenTrue, f, result); + result = reduceNode(node.whenFalse, f, result); + break; + case 189 /* TemplateExpression */: + result = reduceNode(node.head, f, result); + result = ts.reduceLeft(node.templateSpans, f, result); + break; + case 192 /* ClassExpression */: + result = ts.reduceLeft(node.modifiers, f, result); + result = reduceNode(node.name, f, result); + result = ts.reduceLeft(node.typeParameters, f, result); + result = ts.reduceLeft(node.heritageClauses, f, result); + result = ts.reduceLeft(node.members, f, result); + break; + case 194 /* ExpressionWithTypeArguments */: + result = reduceNode(node.expression, f, result); + result = ts.reduceLeft(node.typeArguments, f, result); + break; + // Misc + case 197 /* TemplateSpan */: + result = reduceNode(node.expression, f, result); + result = reduceNode(node.literal, f, result); + break; + // Element + case 199 /* Block */: + result = ts.reduceLeft(node.statements, f, result); + break; + case 200 /* VariableStatement */: + result = ts.reduceLeft(node.modifiers, f, result); + result = reduceNode(node.declarationList, f, result); + break; + case 202 /* ExpressionStatement */: + result = reduceNode(node.expression, f, result); + break; + case 203 /* IfStatement */: + result = reduceNode(node.expression, f, result); + result = reduceNode(node.thenStatement, f, result); + result = reduceNode(node.elseStatement, f, result); + break; + case 204 /* DoStatement */: + result = reduceNode(node.statement, f, result); + result = reduceNode(node.expression, f, result); + break; + case 205 /* WhileStatement */: + case 212 /* WithStatement */: + result = reduceNode(node.expression, f, result); + result = reduceNode(node.statement, f, result); + break; + case 206 /* ForStatement */: + result = reduceNode(node.initializer, f, result); + result = reduceNode(node.condition, f, result); + result = reduceNode(node.incrementor, f, result); + result = reduceNode(node.statement, f, result); + break; + case 207 /* ForInStatement */: + case 208 /* ForOfStatement */: + result = reduceNode(node.initializer, f, result); + result = reduceNode(node.expression, f, result); + result = reduceNode(node.statement, f, result); + break; + case 211 /* ReturnStatement */: + case 215 /* ThrowStatement */: + result = reduceNode(node.expression, f, result); + break; + case 213 /* SwitchStatement */: + result = reduceNode(node.expression, f, result); + result = reduceNode(node.caseBlock, f, result); + break; + case 214 /* LabeledStatement */: + result = reduceNode(node.label, f, result); + result = reduceNode(node.statement, f, result); + break; + case 216 /* TryStatement */: + result = reduceNode(node.tryBlock, f, result); + result = reduceNode(node.catchClause, f, result); + result = reduceNode(node.finallyBlock, f, result); + break; + case 218 /* VariableDeclaration */: + result = reduceNode(node.name, f, result); + result = reduceNode(node.type, f, result); + result = reduceNode(node.initializer, f, result); + break; + case 219 /* VariableDeclarationList */: + result = ts.reduceLeft(node.declarations, f, result); + break; + case 220 /* FunctionDeclaration */: + result = ts.reduceLeft(node.decorators, f, result); + result = ts.reduceLeft(node.modifiers, f, result); + result = reduceNode(node.name, f, result); + result = ts.reduceLeft(node.typeParameters, f, result); + result = ts.reduceLeft(node.parameters, f, result); + result = reduceNode(node.type, f, result); + result = reduceNode(node.body, f, result); + break; + case 221 /* ClassDeclaration */: + result = ts.reduceLeft(node.decorators, f, result); + result = ts.reduceLeft(node.modifiers, f, result); + result = reduceNode(node.name, f, result); + result = ts.reduceLeft(node.typeParameters, f, result); + result = ts.reduceLeft(node.heritageClauses, f, result); + result = ts.reduceLeft(node.members, f, result); + break; + case 227 /* CaseBlock */: + result = ts.reduceLeft(node.clauses, f, result); + break; + case 230 /* ImportDeclaration */: + result = ts.reduceLeft(node.decorators, f, result); + result = ts.reduceLeft(node.modifiers, f, result); + result = reduceNode(node.importClause, f, result); + result = reduceNode(node.moduleSpecifier, f, result); + break; + case 231 /* ImportClause */: + result = reduceNode(node.name, f, result); + result = reduceNode(node.namedBindings, f, result); + break; + case 232 /* NamespaceImport */: + result = reduceNode(node.name, f, result); + break; + case 233 /* NamedImports */: + case 237 /* NamedExports */: + result = ts.reduceLeft(node.elements, f, result); + break; + case 234 /* ImportSpecifier */: + case 238 /* ExportSpecifier */: + result = reduceNode(node.propertyName, f, result); + result = reduceNode(node.name, f, result); + break; + case 235 /* ExportAssignment */: + result = ts.reduceLeft(node.decorators, f, result); + result = ts.reduceLeft(node.modifiers, f, result); + result = reduceNode(node.expression, f, result); + break; + case 236 /* ExportDeclaration */: + result = ts.reduceLeft(node.decorators, f, result); + result = ts.reduceLeft(node.modifiers, f, result); + result = reduceNode(node.exportClause, f, result); + result = reduceNode(node.moduleSpecifier, f, result); + break; + // JSX + case 241 /* JsxElement */: + result = reduceNode(node.openingElement, f, result); + result = ts.reduceLeft(node.children, f, result); + result = reduceNode(node.closingElement, f, result); + break; + case 242 /* JsxSelfClosingElement */: + case 243 /* JsxOpeningElement */: + result = reduceNode(node.tagName, f, result); + result = ts.reduceLeft(node.attributes, f, result); + break; + case 245 /* JsxClosingElement */: + result = reduceNode(node.tagName, f, result); + break; + case 246 /* JsxAttribute */: + result = reduceNode(node.name, f, result); + result = reduceNode(node.initializer, f, result); + break; + case 247 /* JsxSpreadAttribute */: + result = reduceNode(node.expression, f, result); + break; + case 248 /* JsxExpression */: + result = reduceNode(node.expression, f, result); + break; + // Clauses + case 249 /* CaseClause */: + result = reduceNode(node.expression, f, result); + // fall-through + case 250 /* DefaultClause */: + result = ts.reduceLeft(node.statements, f, result); + break; + case 251 /* HeritageClause */: + result = ts.reduceLeft(node.types, f, result); + break; + case 252 /* CatchClause */: + result = reduceNode(node.variableDeclaration, f, result); + result = reduceNode(node.block, f, result); + break; + // Property assignments + case 253 /* PropertyAssignment */: + result = reduceNode(node.name, f, result); + result = reduceNode(node.initializer, f, result); + break; + case 254 /* ShorthandPropertyAssignment */: + result = reduceNode(node.name, f, result); + result = reduceNode(node.objectAssignmentInitializer, f, result); + break; + // Top-level nodes + case 256 /* SourceFile */: + result = ts.reduceLeft(node.statements, f, result); + break; + case 288 /* PartiallyEmittedExpression */: + result = reduceNode(node.expression, f, result); + break; + default: + var edgeTraversalPath = nodeEdgeTraversalMap[kind]; + if (edgeTraversalPath) { + for (var _i = 0, edgeTraversalPath_1 = edgeTraversalPath; _i < edgeTraversalPath_1.length; _i++) { + var edge = edgeTraversalPath_1[_i]; + var value = node[edge.name]; + if (value !== undefined) { + result = ts.isArray(value) + ? ts.reduceLeft(value, f, result) + : f(result, value); + } + } + } + break; + } + return result; + } + ts.reduceEachChild = reduceEachChild; + function visitNode(node, visitor, test, optional, lift, parenthesize, parentNode) { + if (node === undefined) { + return undefined; + } + var visited = visitor(node); + if (visited === node) { + return node; + } + var visitedNode; + if (visited === undefined) { + if (!optional) { + Debug.failNotOptional(); + } + return undefined; + } + else if (ts.isArray(visited)) { + visitedNode = (lift || extractSingleNode)(visited); + } + else { + visitedNode = visited; + } + if (parenthesize !== undefined) { + visitedNode = parenthesize(visitedNode, parentNode); + } + Debug.assertNode(visitedNode, test); + aggregateTransformFlags(visitedNode); + return visitedNode; + } + ts.visitNode = visitNode; + function visitNodes(nodes, visitor, test, start, count, parenthesize, parentNode) { + if (nodes === undefined) { + return undefined; + } + var updated; + // Ensure start and count have valid values + var length = nodes.length; + if (start === undefined || start < 0) { + start = 0; + } + if (count === undefined || count > length - start) { + count = length - start; + } + if (start > 0 || count < length) { + // If we are not visiting all of the original nodes, we must always create a new array. + // Since this is a fragment of a node array, we do not copy over the previous location + // and will only copy over `hasTrailingComma` if we are including the last element. + updated = ts.createNodeArray([], /*location*/ undefined, + /*hasTrailingComma*/ nodes.hasTrailingComma && start + count === length); + } + // Visit each original node. + for (var i = 0; i < count; i++) { + var node = nodes[i + start]; + var visited = node !== undefined ? visitor(node) : undefined; + if (updated !== undefined || visited === undefined || visited !== node) { + if (updated === undefined) { + // Ensure we have a copy of `nodes`, up to the current index. + updated = ts.createNodeArray(nodes.slice(0, i), /*location*/ nodes, nodes.hasTrailingComma); + } + if (visited) { + if (ts.isArray(visited)) { + for (var _i = 0, visited_1 = visited; _i < visited_1.length; _i++) { + var visitedNode = visited_1[_i]; + visitedNode = parenthesize + ? parenthesize(visitedNode, parentNode) + : visitedNode; + Debug.assertNode(visitedNode, test); + aggregateTransformFlags(visitedNode); + updated.push(visitedNode); + } + } + else { + var visitedNode = parenthesize + ? parenthesize(visited, parentNode) + : visited; + Debug.assertNode(visitedNode, test); + aggregateTransformFlags(visitedNode); + updated.push(visitedNode); + } + } + } + } + return updated || nodes; + } + ts.visitNodes = visitNodes; + function visitEachChild(node, visitor, context) { + if (node === undefined) { + return undefined; + } + var kind = node.kind; + // No need to visit nodes with no children. + if ((kind > 0 /* FirstToken */ && kind <= 138 /* LastToken */)) { + return node; + } + // We do not yet support types. + if ((kind >= 154 /* TypePredicate */ && kind <= 166 /* LiteralType */)) { + return node; + } + switch (node.kind) { + case 198 /* SemicolonClassElement */: + case 201 /* EmptyStatement */: + case 193 /* OmittedExpression */: + case 217 /* DebuggerStatement */: + // No need to visit nodes with no children. + return node; + // Names + case 140 /* ComputedPropertyName */: + return ts.updateComputedPropertyName(node, visitNode(node.expression, visitor, ts.isExpression)); + // Signature elements + case 142 /* Parameter */: + return ts.updateParameterDeclaration(node, visitNodes(node.decorators, visitor, ts.isDecorator), visitNodes(node.modifiers, visitor, ts.isModifier), visitNode(node.name, visitor, ts.isBindingName), visitNode(node.type, visitor, ts.isTypeNode, /*optional*/ true), visitNode(node.initializer, visitor, ts.isExpression, /*optional*/ true)); + // Type member + case 145 /* PropertyDeclaration */: + return ts.updateProperty(node, visitNodes(node.decorators, visitor, ts.isDecorator), visitNodes(node.modifiers, visitor, ts.isModifier), visitNode(node.name, visitor, ts.isPropertyName), visitNode(node.type, visitor, ts.isTypeNode, /*optional*/ true), visitNode(node.initializer, visitor, ts.isExpression, /*optional*/ true)); + case 147 /* MethodDeclaration */: + return ts.updateMethod(node, visitNodes(node.decorators, visitor, ts.isDecorator), visitNodes(node.modifiers, visitor, ts.isModifier), visitNode(node.name, visitor, ts.isPropertyName), visitNodes(node.typeParameters, visitor, ts.isTypeParameter), (context.startLexicalEnvironment(), visitNodes(node.parameters, visitor, ts.isParameter)), visitNode(node.type, visitor, ts.isTypeNode, /*optional*/ true), mergeFunctionBodyLexicalEnvironment(visitNode(node.body, visitor, ts.isFunctionBody, /*optional*/ true), context.endLexicalEnvironment())); + case 148 /* Constructor */: + return ts.updateConstructor(node, visitNodes(node.decorators, visitor, ts.isDecorator), visitNodes(node.modifiers, visitor, ts.isModifier), (context.startLexicalEnvironment(), visitNodes(node.parameters, visitor, ts.isParameter)), mergeFunctionBodyLexicalEnvironment(visitNode(node.body, visitor, ts.isFunctionBody, /*optional*/ true), context.endLexicalEnvironment())); + case 149 /* GetAccessor */: + return ts.updateGetAccessor(node, visitNodes(node.decorators, visitor, ts.isDecorator), visitNodes(node.modifiers, visitor, ts.isModifier), visitNode(node.name, visitor, ts.isPropertyName), (context.startLexicalEnvironment(), visitNodes(node.parameters, visitor, ts.isParameter)), visitNode(node.type, visitor, ts.isTypeNode, /*optional*/ true), mergeFunctionBodyLexicalEnvironment(visitNode(node.body, visitor, ts.isFunctionBody, /*optional*/ true), context.endLexicalEnvironment())); + case 150 /* SetAccessor */: + return ts.updateSetAccessor(node, visitNodes(node.decorators, visitor, ts.isDecorator), visitNodes(node.modifiers, visitor, ts.isModifier), visitNode(node.name, visitor, ts.isPropertyName), (context.startLexicalEnvironment(), visitNodes(node.parameters, visitor, ts.isParameter)), mergeFunctionBodyLexicalEnvironment(visitNode(node.body, visitor, ts.isFunctionBody, /*optional*/ true), context.endLexicalEnvironment())); + // Binding patterns + case 167 /* ObjectBindingPattern */: + return ts.updateObjectBindingPattern(node, visitNodes(node.elements, visitor, ts.isBindingElement)); + case 168 /* ArrayBindingPattern */: + return ts.updateArrayBindingPattern(node, visitNodes(node.elements, visitor, ts.isArrayBindingElement)); + case 169 /* BindingElement */: + return ts.updateBindingElement(node, visitNode(node.propertyName, visitor, ts.isPropertyName, /*optional*/ true), visitNode(node.name, visitor, ts.isBindingName), visitNode(node.initializer, visitor, ts.isExpression, /*optional*/ true)); + // Expression case 170 /* ArrayLiteralExpression */: return ts.updateArrayLiteral(node, visitNodes(node.elements, visitor, ts.isExpression)); case 171 /* ObjectLiteralExpression */: @@ -40673,7 +41893,7 @@ var ts; case 175 /* NewExpression */: return ts.updateNew(node, visitNode(node.expression, visitor, ts.isExpression), visitNodes(node.typeArguments, visitor, ts.isTypeNode), visitNodes(node.arguments, visitor, ts.isExpression)); case 176 /* TaggedTemplateExpression */: - return ts.updateTaggedTemplate(node, visitNode(node.tag, visitor, ts.isExpression), visitNode(node.template, visitor, ts.isTemplate)); + return ts.updateTaggedTemplate(node, visitNode(node.tag, visitor, ts.isExpression), visitNode(node.template, visitor, ts.isTemplateLiteral)); case 178 /* ParenthesizedExpression */: return ts.updateParen(node, visitNode(node.expression, visitor, ts.isExpression)); case 179 /* FunctionExpression */: @@ -40697,7 +41917,7 @@ var ts; case 188 /* ConditionalExpression */: return ts.updateConditional(node, visitNode(node.condition, visitor, ts.isExpression), visitNode(node.whenTrue, visitor, ts.isExpression), visitNode(node.whenFalse, visitor, ts.isExpression)); case 189 /* TemplateExpression */: - return ts.updateTemplateExpression(node, visitNode(node.head, visitor, ts.isTemplateLiteralFragment), visitNodes(node.templateSpans, visitor, ts.isTemplateSpan)); + return ts.updateTemplateExpression(node, visitNode(node.head, visitor, ts.isTemplateHead), visitNodes(node.templateSpans, visitor, ts.isTemplateSpan)); case 190 /* YieldExpression */: return ts.updateYield(node, visitNode(node.expression, visitor, ts.isExpression)); case 191 /* SpreadElementExpression */: @@ -40708,7 +41928,7 @@ var ts; return ts.updateExpressionWithTypeArguments(node, visitNodes(node.typeArguments, visitor, ts.isTypeNode), visitNode(node.expression, visitor, ts.isExpression)); // Misc case 197 /* TemplateSpan */: - return ts.updateTemplateSpan(node, visitNode(node.expression, visitor, ts.isExpression), visitNode(node.literal, visitor, ts.isTemplateLiteralFragment)); + return ts.updateTemplateSpan(node, visitNode(node.expression, visitor, ts.isExpression), visitNode(node.literal, visitor, ts.isTemplateMiddleOrTemplateTail)); // Element case 199 /* Block */: return ts.updateBlock(node, visitNodes(node.statements, visitor, ts.isStatement)); @@ -41055,7 +42275,7 @@ var ts; var expression = ts.createAssignment(name, value, location); // NOTE: this completely disables source maps, but aligns with the behavior of // `emitAssignment` in the old emitter. - context.setNodeEmitFlags(expression, 2048 /* NoNestedSourceMaps */); + ts.setEmitFlags(expression, 2048 /* NoNestedSourceMaps */); ts.aggregateTransformFlags(expression); expressions.push(expression); } @@ -41081,7 +42301,7 @@ var ts; var declaration = ts.createVariableDeclaration(name, /*type*/ undefined, value, location); // NOTE: this completely disables source maps, but aligns with the behavior of // `emitAssignment` in the old emitter. - context.setNodeEmitFlags(declaration, 2048 /* NoNestedSourceMaps */); + ts.setEmitFlags(declaration, 2048 /* NoNestedSourceMaps */); ts.aggregateTransformFlags(declaration); declarations.push(declaration); } @@ -41114,7 +42334,7 @@ var ts; declaration.original = original; // NOTE: this completely disables source maps, but aligns with the behavior of // `emitAssignment` in the old emitter. - context.setNodeEmitFlags(declaration, 2048 /* NoNestedSourceMaps */); + ts.setEmitFlags(declaration, 2048 /* NoNestedSourceMaps */); declarations.push(declaration); ts.aggregateTransformFlags(declaration); } @@ -41164,7 +42384,7 @@ var ts; expression.original = original; // NOTE: this completely disables source maps, but aligns with the behavior of // `emitAssignment` in the old emitter. - context.setNodeEmitFlags(expression, 2048 /* NoNestedSourceMaps */); + ts.setEmitFlags(expression, 2048 /* NoNestedSourceMaps */); pendingAssignments.push(expression); return expression; } @@ -41210,8 +42430,8 @@ var ts; } else { var name_26 = ts.getMutableClone(target); - context.setSourceMapRange(name_26, target); - context.setCommentRange(name_26, target); + ts.setSourceMapRange(name_26, target); + ts.setCommentRange(name_26, target); emitAssignment(name_26, value, location, /*original*/ undefined); } } @@ -41380,7 +42600,7 @@ var ts; TypeScriptSubstitutionFlags[TypeScriptSubstitutionFlags["NonQualifiedEnumMembers"] = 8] = "NonQualifiedEnumMembers"; })(TypeScriptSubstitutionFlags || (TypeScriptSubstitutionFlags = {})); function transformTypeScript(context) { - var getNodeEmitFlags = context.getNodeEmitFlags, setNodeEmitFlags = context.setNodeEmitFlags, setCommentRange = context.setCommentRange, setSourceMapRange = context.setSourceMapRange, startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration; + var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration; var resolver = context.getEmitResolver(); var compilerOptions = context.getCompilerOptions(); var languageVersion = ts.getEmitScriptTarget(compilerOptions); @@ -41391,11 +42611,15 @@ var ts; // Set new transformation hooks. context.onEmitNode = onEmitNode; context.onSubstituteNode = onSubstituteNode; + // Enable substitution for property/element access to emit const enum values. + context.enableSubstitution(172 /* PropertyAccessExpression */); + context.enableSubstitution(173 /* ElementAccessExpression */); // These variables contain state that changes as we descend into the tree. var currentSourceFile; var currentNamespace; var currentNamespaceContainerName; var currentScope; + var currentScopeFirstDeclarationsOfName; var currentSourceFileExternalHelpersModuleName; /** * Keeps track of whether expression substitution has been enabled for specific edge cases. @@ -41424,6 +42648,9 @@ var ts; * @param node A SourceFile node. */ function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } return ts.visitNode(node, visitor, ts.isSourceFile); } /** @@ -41434,10 +42661,14 @@ var ts; function saveStateAndInvoke(node, f) { // Save state var savedCurrentScope = currentScope; + var savedCurrentScopeFirstDeclarationsOfName = currentScopeFirstDeclarationsOfName; // Handle state changes before visiting a node. onBeforeVisitNode(node); var visited = f(node); // Restore state + if (currentScope !== savedCurrentScope) { + currentScopeFirstDeclarationsOfName = savedCurrentScopeFirstDeclarationsOfName; + } currentScope = savedCurrentScope; return visited; } @@ -41702,11 +42933,23 @@ var ts; case 226 /* ModuleBlock */: case 199 /* Block */: currentScope = node; + currentScopeFirstDeclarationsOfName = undefined; + break; + case 221 /* ClassDeclaration */: + case 220 /* FunctionDeclaration */: + if (ts.hasModifier(node, 2 /* Ambient */)) { + break; + } + recordEmittedDeclarationInScope(node); break; } } function visitSourceFile(node) { currentSourceFile = node; + // ensure "use strict" is emitted in all scenarios in alwaysStrict mode + if (compilerOptions.alwaysStrict) { + node = ts.ensureUseStrict(node); + } // If the source file requires any helpers and is an external module, and // the importHelpers compiler option is enabled, emit a synthesized import // statement for the helpers library. @@ -41733,7 +42976,7 @@ var ts; else { node = ts.visitEachChild(node, visitor, context); } - setNodeEmitFlags(node, 1 /* EmitEmitHelpers */ | node.emitFlags); + ts.setEmitFlags(node, 1 /* EmitEmitHelpers */ | ts.getEmitFlags(node)); return node; } /** @@ -41792,7 +43035,7 @@ var ts; // To better align with the old emitter, we should not emit a trailing source map // entry if the class has static properties. if (staticProperties.length > 0) { - setNodeEmitFlags(classDeclaration, 1024 /* NoTrailingSourceMap */ | getNodeEmitFlags(classDeclaration)); + ts.setEmitFlags(classDeclaration, 1024 /* NoTrailingSourceMap */ | ts.getEmitFlags(classDeclaration)); } statements.push(classDeclaration); } @@ -41951,7 +43194,7 @@ var ts; /*type*/ undefined, classExpression) ]), /*location*/ location); - setCommentRange(transformedClassExpression, node); + ts.setCommentRange(transformedClassExpression, node); statements.push(ts.setOriginalNode( /*node*/ transformedClassExpression, /*original*/ node)); @@ -41996,7 +43239,7 @@ var ts; } // To preserve the behavior of the old emitter, we explicitly indent // the body of a class with static initializers. - setNodeEmitFlags(classExpression, 524288 /* Indented */ | getNodeEmitFlags(classExpression)); + ts.setEmitFlags(classExpression, 524288 /* Indented */ | ts.getEmitFlags(classExpression)); expressions.push(ts.startOnNewLine(ts.createAssignment(temp, classExpression))); ts.addRange(expressions, generateInitializedPropertyExpressions(node, staticProperties, temp)); expressions.push(ts.startOnNewLine(temp)); @@ -42151,7 +43394,7 @@ var ts; return index; } var statement = statements[index]; - if (statement.kind === 202 /* ExpressionStatement */ && ts.isSuperCallExpression(statement.expression)) { + if (statement.kind === 202 /* ExpressionStatement */ && ts.isSuperCall(statement.expression)) { result.push(ts.visitNode(statement, visitor, ts.isStatement)); return index + 1; } @@ -42185,9 +43428,9 @@ var ts; ts.Debug.assert(ts.isIdentifier(node.name)); var name = node.name; var propertyName = ts.getMutableClone(name); - setNodeEmitFlags(propertyName, 49152 /* NoComments */ | 1536 /* NoSourceMap */); + ts.setEmitFlags(propertyName, 49152 /* NoComments */ | 1536 /* NoSourceMap */); var localName = ts.getMutableClone(name); - setNodeEmitFlags(localName, 49152 /* NoComments */); + ts.setEmitFlags(localName, 49152 /* NoComments */); return ts.startOnNewLine(ts.createStatement(ts.createAssignment(ts.createPropertyAccess(ts.createThis(), propertyName, /*location*/ node.name), localName), /*location*/ ts.moveRangePos(node, -1))); @@ -42239,8 +43482,8 @@ var ts; for (var _i = 0, properties_7 = properties; _i < properties_7.length; _i++) { var property = properties_7[_i]; var statement = ts.createStatement(transformInitializedProperty(node, property, receiver)); - setSourceMapRange(statement, ts.moveRangePastModifiers(property)); - setCommentRange(statement, property); + ts.setSourceMapRange(statement, ts.moveRangePastModifiers(property)); + ts.setCommentRange(statement, property); statements.push(statement); } } @@ -42257,8 +43500,8 @@ var ts; var property = properties_8[_i]; var expression = transformInitializedProperty(node, property, receiver); expression.startsOnNewLine = true; - setSourceMapRange(expression, ts.moveRangePastModifiers(property)); - setCommentRange(expression, property); + ts.setSourceMapRange(expression, ts.moveRangePastModifiers(property)); + ts.setCommentRange(expression, property); expressions.push(expression); } return expressions; @@ -42524,7 +43767,7 @@ var ts; : ts.createNull() : undefined; var helper = ts.createDecorateHelper(currentSourceFileExternalHelpersModuleName, decoratorExpressions, prefix, memberName, descriptor, ts.moveRangePastDecorators(member)); - setNodeEmitFlags(helper, 49152 /* NoComments */); + ts.setEmitFlags(helper, 49152 /* NoComments */); return helper; } /** @@ -42562,12 +43805,12 @@ var ts; if (decoratedClassAlias) { var expression = ts.createAssignment(decoratedClassAlias, ts.createDecorateHelper(currentSourceFileExternalHelpersModuleName, decoratorExpressions, getDeclarationName(node))); var result = ts.createAssignment(getDeclarationName(node), expression, ts.moveRangePastDecorators(node)); - setNodeEmitFlags(result, 49152 /* NoComments */); + ts.setEmitFlags(result, 49152 /* NoComments */); return result; } else { var result = ts.createAssignment(getDeclarationName(node), ts.createDecorateHelper(currentSourceFileExternalHelpersModuleName, decoratorExpressions, getDeclarationName(node)), ts.moveRangePastDecorators(node)); - setNodeEmitFlags(result, 49152 /* NoComments */); + ts.setEmitFlags(result, 49152 /* NoComments */); return result; } } @@ -42593,7 +43836,7 @@ var ts; var decorator = decorators_1[_i]; var helper = ts.createParamHelper(currentSourceFileExternalHelpersModuleName, transformDecorator(decorator), parameterOffset, /*location*/ decorator.expression); - setNodeEmitFlags(helper, 49152 /* NoComments */); + ts.setEmitFlags(helper, 49152 /* NoComments */); expressions.push(helper); } } @@ -42824,10 +44067,38 @@ var ts; : ts.createIdentifier("Symbol"); case 155 /* TypeReference */: return serializeTypeReferenceNode(node); + case 163 /* IntersectionType */: + case 162 /* UnionType */: + { + var unionOrIntersection = node; + var serializedUnion = void 0; + for (var _i = 0, _a = unionOrIntersection.types; _i < _a.length; _i++) { + var typeNode = _a[_i]; + var serializedIndividual = serializeTypeNode(typeNode); + // Non identifier + if (serializedIndividual.kind !== 69 /* Identifier */) { + serializedUnion = undefined; + break; + } + // One of the individual is global object, return immediately + if (serializedIndividual.text === "Object") { + return serializedIndividual; + } + // Different types + if (serializedUnion && serializedUnion.text !== serializedIndividual.text) { + serializedUnion = undefined; + break; + } + serializedUnion = serializedIndividual; + } + // If we were able to find common type + if (serializedUnion) { + return serializedUnion; + } + } + // Fallthrough case 158 /* TypeQuery */: case 159 /* TypeLiteral */: - case 162 /* UnionType */: - case 163 /* IntersectionType */: case 117 /* AnyKeyword */: case 165 /* ThisType */: break; @@ -43027,8 +44298,8 @@ var ts; /*location*/ node); // While we emit the source map for the node after skipping decorators and modifiers, // we need to emit the comments for the original range. - setCommentRange(method, node); - setSourceMapRange(method, ts.moveRangePastDecorators(node)); + ts.setCommentRange(method, node); + ts.setSourceMapRange(method, ts.moveRangePastDecorators(node)); ts.setOriginalNode(method, node); return method; } @@ -43060,8 +44331,8 @@ var ts; /*location*/ node); // While we emit the source map for the node after skipping decorators and modifiers, // we need to emit the comments for the original range. - setCommentRange(accessor, node); - setSourceMapRange(accessor, ts.moveRangePastDecorators(node)); + ts.setCommentRange(accessor, node); + ts.setSourceMapRange(accessor, ts.moveRangePastDecorators(node)); ts.setOriginalNode(accessor, node); return accessor; } @@ -43083,8 +44354,8 @@ var ts; /*location*/ node); // While we emit the source map for the node after skipping decorators and modifiers, // we need to emit the comments for the original range. - setCommentRange(accessor, node); - setSourceMapRange(accessor, ts.moveRangePastDecorators(node)); + ts.setCommentRange(accessor, node); + ts.setSourceMapRange(accessor, ts.moveRangePastDecorators(node)); ts.setOriginalNode(accessor, node); return accessor; } @@ -43220,11 +44491,11 @@ var ts; if (languageVersion >= 2 /* ES6 */) { if (resolver.getNodeCheckFlags(node) & 4096 /* AsyncMethodWithSuperBinding */) { enableSubstitutionForAsyncMethodsWithSuper(); - setNodeEmitFlags(block, 8 /* EmitAdvancedSuperHelper */); + ts.setEmitFlags(block, 8 /* EmitAdvancedSuperHelper */); } else if (resolver.getNodeCheckFlags(node) & 2048 /* AsyncMethodWithSuper */) { enableSubstitutionForAsyncMethodsWithSuper(); - setNodeEmitFlags(block, 4 /* EmitSuperHelper */); + ts.setEmitFlags(block, 4 /* EmitSuperHelper */); } } return block; @@ -43244,7 +44515,7 @@ var ts; * @param node The parameter declaration node. */ function visitParameter(node) { - if (node.name && ts.isIdentifier(node.name) && node.name.originalKeywordKind === 97 /* ThisKeyword */) { + if (ts.parameterIsThisKeyword(node)) { return undefined; } var parameter = ts.createParameterDeclaration( @@ -43256,9 +44527,9 @@ var ts; // While we emit the source map for the node after skipping decorators and modifiers, // we need to emit the comments for the original range. ts.setOriginalNode(parameter, node); - setCommentRange(parameter, node); - setSourceMapRange(parameter, ts.moveRangePastModifiers(node)); - setNodeEmitFlags(parameter.name, 1024 /* NoTrailingSourceMap */); + ts.setCommentRange(parameter, node); + ts.setSourceMapRange(parameter, ts.moveRangePastModifiers(node)); + ts.setEmitFlags(parameter.name, 1024 /* NoTrailingSourceMap */); return parameter; } /** @@ -43351,8 +44622,9 @@ var ts; || compilerOptions.isolatedModules; } function shouldEmitVarForEnumDeclaration(node) { - return !ts.hasModifier(node, 1 /* Export */) - || (isES6ExportedDeclaration(node) && ts.isFirstDeclarationOfKind(node, node.kind)); + return isFirstEmittedDeclarationInScope(node) + && (!ts.hasModifier(node, 1 /* Export */) + || isES6ExportedDeclaration(node)); } /* * Adds a trailing VariableStatement for an enum or module declaration. @@ -43361,7 +44633,7 @@ var ts; var statement = ts.createVariableStatement( /*modifiers*/ undefined, [ts.createVariableDeclaration(getDeclarationName(node), /*type*/ undefined, getExportName(node))]); - setSourceMapRange(statement, node); + ts.setSourceMapRange(statement, node); statements.push(statement); } /** @@ -43382,6 +44654,7 @@ var ts; // If needed, we should emit a variable declaration for the enum. If we emit // a leading variable declaration, we should not emit leading comments for the // enum body. + recordEmittedDeclarationInScope(node); if (shouldEmitVarForEnumDeclaration(node)) { addVarForEnumOrModuleDeclaration(statements, node); // We should still emit the comments if we are emitting a system module. @@ -43407,7 +44680,7 @@ var ts; /*typeArguments*/ undefined, [ts.createLogicalOr(exportName, ts.createAssignment(exportName, ts.createObjectLiteral()))]), /*location*/ node); ts.setOriginalNode(enumStatement, node); - setNodeEmitFlags(enumStatement, emitFlags); + ts.setEmitFlags(enumStatement, emitFlags); statements.push(enumStatement); if (isNamespaceExport(node)) { addVarForEnumExportedFromNamespace(statements, node); @@ -43473,18 +44746,43 @@ var ts; function shouldEmitModuleDeclaration(node) { return ts.isInstantiatedModule(node, compilerOptions.preserveConstEnums || compilerOptions.isolatedModules); } - function isModuleMergedWithES6Class(node) { - return languageVersion === 2 /* ES6 */ - && ts.isMergedWithClass(node); - } function isES6ExportedDeclaration(node) { return isExternalModuleExport(node) && moduleKind === ts.ModuleKind.ES6; } + /** + * Records that a declaration was emitted in the current scope, if it was the first + * declaration for the provided symbol. + * + * NOTE: if there is ever a transformation above this one, we may not be able to rely + * on symbol names. + */ + function recordEmittedDeclarationInScope(node) { + var name = node.symbol && node.symbol.name; + if (name) { + if (!currentScopeFirstDeclarationsOfName) { + currentScopeFirstDeclarationsOfName = ts.createMap(); + } + if (!(name in currentScopeFirstDeclarationsOfName)) { + currentScopeFirstDeclarationsOfName[name] = node; + } + } + } + /** + * Determines whether a declaration is the first declaration with the same name emitted + * in the current scope. + */ + function isFirstEmittedDeclarationInScope(node) { + if (currentScopeFirstDeclarationsOfName) { + var name_28 = node.symbol && node.symbol.name; + if (name_28) { + return currentScopeFirstDeclarationsOfName[name_28] === node; + } + } + return false; + } function shouldEmitVarForModuleDeclaration(node) { - return !isModuleMergedWithES6Class(node) - && (!isES6ExportedDeclaration(node) - || ts.isFirstDeclarationOfKind(node, node.kind)); + return isFirstEmittedDeclarationInScope(node); } /** * Adds a leading VariableStatement for a enum or module declaration. @@ -43499,10 +44797,10 @@ var ts; ts.setOriginalNode(statement, /*original*/ node); // Adjust the source map emit to match the old emitter. if (node.kind === 224 /* EnumDeclaration */) { - setSourceMapRange(statement.declarationList, node); + ts.setSourceMapRange(statement.declarationList, node); } else { - setSourceMapRange(statement, node); + ts.setSourceMapRange(statement, node); } // Trailing comments for module declaration should be emitted after the function closure // instead of the variable statement: @@ -43522,8 +44820,8 @@ var ts; // } // })(m1 || (m1 = {})); // trailing comment module // - setCommentRange(statement, node); - setNodeEmitFlags(statement, 32768 /* NoTrailingComments */); + ts.setCommentRange(statement, node); + ts.setEmitFlags(statement, 32768 /* NoTrailingComments */); statements.push(statement); } /** @@ -43546,6 +44844,7 @@ var ts; // If needed, we should emit a variable declaration for the module. If we emit // a leading variable declaration, we should not emit leading comments for the // module body. + recordEmittedDeclarationInScope(node); if (shouldEmitVarForModuleDeclaration(node)) { addVarForEnumOrModuleDeclaration(statements, node); // We should still emit the comments if we are emitting a system module. @@ -43579,7 +44878,7 @@ var ts; /*typeArguments*/ undefined, [moduleArg]), /*location*/ node); ts.setOriginalNode(moduleStatement, node); - setNodeEmitFlags(moduleStatement, emitFlags); + ts.setEmitFlags(moduleStatement, emitFlags); statements.push(moduleStatement); return statements; } @@ -43591,8 +44890,10 @@ var ts; function transformModuleBody(node, namespaceLocalName) { var savedCurrentNamespaceContainerName = currentNamespaceContainerName; var savedCurrentNamespace = currentNamespace; + var savedCurrentScopeFirstDeclarationsOfName = currentScopeFirstDeclarationsOfName; currentNamespaceContainerName = namespaceLocalName; currentNamespace = node; + currentScopeFirstDeclarationsOfName = undefined; var statements = []; startLexicalEnvironment(); var statementsLocation; @@ -43619,6 +44920,7 @@ var ts; ts.addRange(statements, endLexicalEnvironment()); currentNamespaceContainerName = savedCurrentNamespaceContainerName; currentNamespace = savedCurrentNamespace; + currentScopeFirstDeclarationsOfName = savedCurrentScopeFirstDeclarationsOfName; var block = ts.createBlock(ts.createNodeArray(statements, /*location*/ statementsLocation), /*location*/ blockLocation, @@ -43644,7 +44946,7 @@ var ts; // })(hello || (hello = {})); // We only want to emit comment on the namespace which contains block body itself, not the containing namespaces. if (body.kind !== 226 /* ModuleBlock */) { - setNodeEmitFlags(block, block.emitFlags | 49152 /* NoComments */); + ts.setEmitFlags(block, ts.getEmitFlags(block) | 49152 /* NoComments */); } return block; } @@ -43680,7 +44982,7 @@ var ts; return undefined; } var moduleReference = ts.createExpressionFromEntityName(node.moduleReference); - setNodeEmitFlags(moduleReference, 49152 /* NoComments */ | 65536 /* NoNestedComments */); + ts.setEmitFlags(moduleReference, 49152 /* NoComments */ | 65536 /* NoNestedComments */); if (isNamedExternalModuleExport(node) || !isNamespaceExport(node)) { // export var ${name} = ${moduleReference}; // var ${name} = ${moduleReference}; @@ -43736,9 +45038,9 @@ var ts; } function addExportMemberAssignment(statements, node) { var expression = ts.createAssignment(getExportName(node), getLocalName(node, /*noSourceMaps*/ true)); - setSourceMapRange(expression, ts.createRange(node.name.pos, node.end)); + ts.setSourceMapRange(expression, ts.createRange(node.name.pos, node.end)); var statement = ts.createStatement(expression); - setSourceMapRange(statement, ts.createRange(-1, node.end)); + ts.setSourceMapRange(statement, ts.createRange(-1, node.end)); statements.push(statement); } function createNamespaceExport(exportName, exportValue, location) { @@ -43761,7 +45063,7 @@ var ts; emitFlags |= 1536 /* NoSourceMap */; } if (emitFlags) { - setNodeEmitFlags(qualifiedName, emitFlags); + ts.setEmitFlags(qualifiedName, emitFlags); } return qualifiedName; } @@ -43773,7 +45075,7 @@ var ts; */ function getNamespaceParameterName(node) { var name = ts.getGeneratedNameForNode(node); - setSourceMapRange(name, node.name); + ts.setSourceMapRange(name, node.name); return name; } /** @@ -43822,8 +45124,8 @@ var ts; */ function getDeclarationName(node, allowComments, allowSourceMaps, emitFlags) { if (node.name) { - var name_28 = ts.getMutableClone(node.name); - emitFlags |= getNodeEmitFlags(node.name); + var name_29 = ts.getMutableClone(node.name); + emitFlags |= ts.getEmitFlags(node.name); if (!allowSourceMaps) { emitFlags |= 1536 /* NoSourceMap */; } @@ -43831,9 +45133,9 @@ var ts; emitFlags |= 49152 /* NoComments */; } if (emitFlags) { - setNodeEmitFlags(name_28, emitFlags); + ts.setEmitFlags(name_29, emitFlags); } - return name_28; + return name_29; } else { return ts.getGeneratedNameForNode(node); @@ -43910,7 +45212,7 @@ var ts; * @param node The node to emit. * @param emit A callback used to emit the node in the printer. */ - function onEmitNode(node, emit) { + function onEmitNode(emitContext, node, emitCallback) { var savedApplicableSubstitutions = applicableSubstitutions; var savedCurrentSuperContainer = currentSuperContainer; // If we need to support substitutions for `super` in an async method, @@ -43924,7 +45226,7 @@ var ts; if (enabledSubstitutions & 8 /* NonQualifiedEnumMembers */ && isTransformedEnumDeclaration(node)) { applicableSubstitutions |= 8 /* NonQualifiedEnumMembers */; } - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); applicableSubstitutions = savedApplicableSubstitutions; currentSuperContainer = savedCurrentSuperContainer; } @@ -43935,9 +45237,9 @@ var ts; * @param isExpression A value indicating whether the node is to be used in an expression * position. */ - function onSubstituteNode(node, isExpression) { - node = previousOnSubstituteNode(node, isExpression); - if (isExpression) { + function onSubstituteNode(emitContext, node) { + node = previousOnSubstituteNode(emitContext, node); + if (emitContext === 1 /* Expression */) { return substituteExpression(node); } else if (ts.isShorthandPropertyAssignment(node)) { @@ -43947,16 +45249,16 @@ var ts; } function substituteShorthandPropertyAssignment(node) { if (enabledSubstitutions & 2 /* NamespaceExports */) { - var name_29 = node.name; - var exportedName = trySubstituteNamespaceExportedName(name_29); + var name_30 = node.name; + var exportedName = trySubstituteNamespaceExportedName(name_30); if (exportedName) { // A shorthand property with an assignment initializer is probably part of a // destructuring assignment if (node.objectAssignmentInitializer) { var initializer = ts.createAssignment(exportedName, node.objectAssignmentInitializer); - return ts.createPropertyAssignment(name_29, initializer, /*location*/ node); + return ts.createPropertyAssignment(name_30, initializer, /*location*/ node); } - return ts.createPropertyAssignment(name_29, exportedName, /*location*/ node); + return ts.createPropertyAssignment(name_30, exportedName, /*location*/ node); } } return node; @@ -43965,16 +45267,15 @@ var ts; switch (node.kind) { case 69 /* Identifier */: return substituteExpressionIdentifier(node); - } - if (enabledSubstitutions & 4 /* AsyncMethodsWithSuper */) { - switch (node.kind) { - case 174 /* CallExpression */: + case 172 /* PropertyAccessExpression */: + return substitutePropertyAccessExpression(node); + case 173 /* ElementAccessExpression */: + return substituteElementAccessExpression(node); + case 174 /* CallExpression */: + if (enabledSubstitutions & 4 /* AsyncMethodsWithSuper */) { return substituteCallExpression(node); - case 172 /* PropertyAccessExpression */: - return substitutePropertyAccessExpression(node); - case 173 /* ElementAccessExpression */: - return substituteElementAccessExpression(node); - } + } + break; } return node; } @@ -43996,8 +45297,8 @@ var ts; var classAlias = classAliases[declaration.id]; if (classAlias) { var clone_4 = ts.getSynthesizedClone(classAlias); - setSourceMapRange(clone_4, node); - setCommentRange(clone_4, node); + ts.setSourceMapRange(clone_4, node); + ts.setCommentRange(clone_4, node); return clone_4; } } @@ -44007,7 +45308,7 @@ var ts; } function trySubstituteNamespaceExportedName(node) { // If this is explicitly a local name, do not substitute. - if (enabledSubstitutions & applicableSubstitutions && (getNodeEmitFlags(node) & 262144 /* LocalName */) === 0) { + if (enabledSubstitutions & applicableSubstitutions && (ts.getEmitFlags(node) & 262144 /* LocalName */) === 0) { // If we are nested within a namespace declaration, we may need to qualifiy // an identifier that is exported from a merged namespace. var container = resolver.getReferencedExportContainer(node, /*prefixLocals*/ false); @@ -44038,23 +45339,48 @@ var ts; return node; } function substitutePropertyAccessExpression(node) { - if (node.expression.kind === 95 /* SuperKeyword */) { + if (enabledSubstitutions & 4 /* AsyncMethodsWithSuper */ && node.expression.kind === 95 /* SuperKeyword */) { var flags = getSuperContainerAsyncMethodFlags(); if (flags) { return createSuperAccessInAsyncMethod(ts.createLiteral(node.name.text), flags, node); } } - return node; + return substituteConstantValue(node); } function substituteElementAccessExpression(node) { - if (node.expression.kind === 95 /* SuperKeyword */) { + if (enabledSubstitutions & 4 /* AsyncMethodsWithSuper */ && node.expression.kind === 95 /* SuperKeyword */) { var flags = getSuperContainerAsyncMethodFlags(); if (flags) { return createSuperAccessInAsyncMethod(node.argumentExpression, flags, node); } } + return substituteConstantValue(node); + } + function substituteConstantValue(node) { + var constantValue = tryGetConstEnumValue(node); + if (constantValue !== undefined) { + var substitute = ts.createLiteral(constantValue); + ts.setSourceMapRange(substitute, node); + ts.setCommentRange(substitute, node); + if (!compilerOptions.removeComments) { + var propertyName = ts.isPropertyAccessExpression(node) + ? ts.declarationNameToString(node.name) + : ts.getTextOfNode(node.argumentExpression); + substitute.trailingComment = " " + propertyName + " "; + } + ts.setConstantValue(node, constantValue); + return substitute; + } return node; } + function tryGetConstEnumValue(node) { + if (compilerOptions.isolatedModules) { + return undefined; + } + return ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node) + ? resolver.getConstantValue(node) + : undefined; + } function createSuperAccessInAsyncMethod(argumentExpression, flags, location) { if (flags & 4096 /* AsyncMethodWithSuperBinding */) { return ts.createPropertyAccess(ts.createCall(ts.createIdentifier("_super"), @@ -44083,6 +45409,9 @@ var ts; var currentSourceFile; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } if (ts.isExternalModule(node) || compilerOptions.isolatedModules) { currentSourceFile = node; return ts.visitEachChild(node, visitor, context); @@ -44201,7 +45530,7 @@ var ts; var ts; (function (ts) { function transformSystemModule(context) { - var getNodeEmitFlags = context.getNodeEmitFlags, setNodeEmitFlags = context.setNodeEmitFlags, startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration, hoistFunctionDeclaration = context.hoistFunctionDeclaration; + var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration, hoistFunctionDeclaration = context.hoistFunctionDeclaration; var compilerOptions = context.getCompilerOptions(); var resolver = context.getEmitResolver(); var host = context.getEmitHost(); @@ -44230,6 +45559,9 @@ var ts; var currentNode; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } if (ts.isExternalModule(node) || compilerOptions.isolatedModules) { currentSourceFile = node; currentNode = node; @@ -44283,7 +45615,7 @@ var ts; ts.createParameter(exportFunctionForFile), ts.createParameter(contextObjectForFile) ], - /*type*/ undefined, setNodeEmitFlags(ts.createBlock(statements, /*location*/ undefined, /*multiLine*/ true), 1 /* EmitEmitHelpers */)); + /*type*/ undefined, ts.setEmitFlags(ts.createBlock(statements, /*location*/ undefined, /*multiLine*/ true), 1 /* EmitEmitHelpers */)); // Write the call to `System.register` // Clear the emit-helpers flag for later passes since we'll have already used it in the module body // So the helper will be emit at the correct position instead of at the top of the source-file @@ -44292,7 +45624,7 @@ var ts; /*typeArguments*/ undefined, moduleName ? [moduleName, dependencies, body] : [dependencies, body])) - ], /*nodeEmitFlags*/ ~1 /* EmitEmitHelpers */ & getNodeEmitFlags(node)); + ], /*nodeEmitFlags*/ ~1 /* EmitEmitHelpers */ & ts.getEmitFlags(node)); var _a; } /** @@ -44676,17 +46008,17 @@ var ts; function visitFunctionDeclaration(node) { if (ts.hasModifier(node, 1 /* Export */)) { // If the function is exported, ensure it has a name and rewrite the function without any export flags. - var name_30 = node.name || ts.getGeneratedNameForNode(node); + var name_31 = node.name || ts.getGeneratedNameForNode(node); var newNode = ts.createFunctionDeclaration( /*decorators*/ undefined, - /*modifiers*/ undefined, node.asteriskToken, name_30, + /*modifiers*/ undefined, node.asteriskToken, name_31, /*typeParameters*/ undefined, node.parameters, /*type*/ undefined, node.body, /*location*/ node); // Record a declaration export in the outer module body function. recordExportedFunctionDeclaration(node); if (!ts.hasModifier(node, 512 /* Default */)) { - recordExportName(name_30); + recordExportName(name_31); } ts.setOriginalNode(newNode, node); node = newNode; @@ -44698,16 +46030,16 @@ var ts; function visitExpressionStatement(node) { var originalNode = ts.getOriginalNode(node); if ((originalNode.kind === 225 /* ModuleDeclaration */ || originalNode.kind === 224 /* EnumDeclaration */) && ts.hasModifier(originalNode, 1 /* Export */)) { - var name_31 = getDeclarationName(originalNode); + var name_32 = getDeclarationName(originalNode); // We only need to hoistVariableDeclaration for EnumDeclaration // as ModuleDeclaration is already hoisted when the transformer call visitVariableStatement // which then call transformsVariable for each declaration in declarationList if (originalNode.kind === 224 /* EnumDeclaration */) { - hoistVariableDeclaration(name_31); + hoistVariableDeclaration(name_32); } return [ node, - createExportStatement(name_31, name_31) + createExportStatement(name_32, name_32) ]; } return node; @@ -44952,14 +46284,14 @@ var ts; // // Substitutions // - function onEmitNode(node, emit) { + function onEmitNode(emitContext, node, emitCallback) { if (node.kind === 256 /* SourceFile */) { exportFunctionForFile = exportFunctionForFileMap[ts.getOriginalNodeId(node)]; - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); exportFunctionForFile = undefined; } else { - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); } } /** @@ -44969,9 +46301,9 @@ var ts; * @param isExpression A value indicating whether the node is to be used in an expression * position. */ - function onSubstituteNode(node, isExpression) { - node = previousOnSubstituteNode(node, isExpression); - if (isExpression) { + function onSubstituteNode(emitContext, node) { + node = previousOnSubstituteNode(emitContext, node); + if (emitContext === 1 /* Expression */) { return substituteExpression(node); } return node; @@ -45013,7 +46345,7 @@ var ts; return node; } function substituteAssignmentExpression(node) { - setNodeEmitFlags(node, 128 /* NoSubstitution */); + ts.setEmitFlags(node, 128 /* NoSubstitution */); var left = node.left; switch (left.kind) { case 69 /* Identifier */: @@ -45118,7 +46450,7 @@ var ts; var exportDeclaration = resolver.getReferencedExportContainer(operand); if (exportDeclaration) { var expr = ts.createPrefix(node.operator, operand, node); - setNodeEmitFlags(expr, 128 /* NoSubstitution */); + ts.setEmitFlags(expr, 128 /* NoSubstitution */); var call = createExportExpression(operand, expr); if (node.kind === 185 /* PrefixUnaryExpression */) { return call; @@ -45165,7 +46497,7 @@ var ts; ts.createForIn(ts.createVariableDeclarationList([ ts.createVariableDeclaration(n, /*type*/ undefined) ]), m, ts.createBlock([ - setNodeEmitFlags(ts.createIf(condition, ts.createStatement(ts.createAssignment(ts.createElementAccess(exports, n), ts.createElementAccess(m, n)))), 32 /* SingleLine */) + ts.setEmitFlags(ts.createIf(condition, ts.createStatement(ts.createAssignment(ts.createElementAccess(exports, n), ts.createElementAccess(m, n)))), 32 /* SingleLine */) ])), ts.createStatement(ts.createCall(exportFunctionForFile, /*typeArguments*/ undefined, [exports])) @@ -45283,7 +46615,7 @@ var ts; function updateSourceFile(node, statements, nodeEmitFlags) { var updated = ts.getMutableClone(node); updated.statements = ts.createNodeArray(statements, node.statements); - setNodeEmitFlags(updated, nodeEmitFlags); + ts.setEmitFlags(updated, nodeEmitFlags); return updated; } } @@ -45301,7 +46633,7 @@ var ts; _a[ts.ModuleKind.AMD] = transformAMDModule, _a[ts.ModuleKind.UMD] = transformUMDModule, _a)); - var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration, setNodeEmitFlags = context.setNodeEmitFlags, getNodeEmitFlags = context.getNodeEmitFlags, setSourceMapRange = context.setSourceMapRange; + var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration; var compilerOptions = context.getCompilerOptions(); var resolver = context.getEmitResolver(); var host = context.getEmitHost(); @@ -45333,6 +46665,9 @@ var ts; * @param node The SourceFile node. */ function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } if (ts.isExternalModule(node) || compilerOptions.isolatedModules) { currentSourceFile = node; // Collect information about the external module. @@ -45365,7 +46700,7 @@ var ts; addExportEqualsIfNeeded(statements, /*emitAsReturn*/ false); var updated = updateSourceFile(node, statements); if (hasExportStarsToExportValues) { - setNodeEmitFlags(updated, 2 /* EmitExportStar */ | getNodeEmitFlags(node)); + ts.setEmitFlags(updated, 2 /* EmitExportStar */ | ts.getEmitFlags(node)); } return updated; } @@ -45386,7 +46721,7 @@ var ts; */ function transformUMDModule(node) { var define = ts.createIdentifier("define"); - setNodeEmitFlags(define, 16 /* UMDDefine */); + ts.setEmitFlags(define, 16 /* UMDDefine */); return transformAsynchronousModule(node, define, /*moduleName*/ undefined, /*includeNonAmdDependencies*/ false); } /** @@ -45466,7 +46801,7 @@ var ts; if (hasExportStarsToExportValues) { // If we have any `export * from ...` declarations // we need to inform the emitter to add the __export helper. - setNodeEmitFlags(body, 2 /* EmitExportStar */); + ts.setEmitFlags(body, 2 /* EmitExportStar */); } return body; } @@ -45475,13 +46810,13 @@ var ts; if (emitAsReturn) { var statement = ts.createReturn(exportEquals.expression, /*location*/ exportEquals); - setNodeEmitFlags(statement, 12288 /* NoTokenSourceMaps */ | 49152 /* NoComments */); + ts.setEmitFlags(statement, 12288 /* NoTokenSourceMaps */ | 49152 /* NoComments */); statements.push(statement); } else { var statement = ts.createStatement(ts.createAssignment(ts.createPropertyAccess(ts.createIdentifier("module"), "exports"), exportEquals.expression), /*location*/ exportEquals); - setNodeEmitFlags(statement, 49152 /* NoComments */); + ts.setEmitFlags(statement, 49152 /* NoComments */); statements.push(statement); } } @@ -45574,7 +46909,7 @@ var ts; } // Set emitFlags on the name of the importEqualsDeclaration // This is so the printer will not substitute the identifier - setNodeEmitFlags(node.name, 128 /* NoSubstitution */); + ts.setEmitFlags(node.name, 128 /* NoSubstitution */); var statements = []; if (moduleKind !== ts.ModuleKind.AMD) { if (ts.hasModifier(node, 1 /* Export */)) { @@ -45678,16 +47013,16 @@ var ts; else { var names = ts.reduceEachChild(node, collectExportMembers, []); for (var _i = 0, names_1 = names; _i < names_1.length; _i++) { - var name_32 = names_1[_i]; - addExportMemberAssignments(statements, name_32); + var name_33 = names_1[_i]; + addExportMemberAssignments(statements, name_33); } } } function collectExportMembers(names, node) { if (ts.isAliasSymbolDeclaration(node) && resolver.isValueAliasDeclaration(node) && ts.isDeclaration(node)) { - var name_33 = node.name; - if (ts.isIdentifier(name_33)) { - names.push(name_33); + var name_34 = node.name; + if (ts.isIdentifier(name_34)) { + names.push(name_34); } } return ts.reduceEachChild(node, collectExportMembers, names); @@ -45706,7 +47041,7 @@ var ts; addExportDefault(statements, getDeclarationName(node), /*location*/ node); } else { - statements.push(createExportStatement(node.name, setNodeEmitFlags(ts.getSynthesizedClone(node.name), 262144 /* LocalName */), /*location*/ node)); + statements.push(createExportStatement(node.name, ts.setEmitFlags(ts.getSynthesizedClone(node.name), 262144 /* LocalName */), /*location*/ node)); } } function visitVariableStatement(node) { @@ -45856,20 +47191,20 @@ var ts; /*modifiers*/ undefined, [ts.createVariableDeclaration(getDeclarationName(node), /*type*/ undefined, ts.createPropertyAccess(ts.createIdentifier("exports"), getDeclarationName(node)))], /*location*/ node); - setNodeEmitFlags(transformedStatement, 49152 /* NoComments */); + ts.setEmitFlags(transformedStatement, 49152 /* NoComments */); statements.push(transformedStatement); } function getDeclarationName(node) { return node.name ? ts.getSynthesizedClone(node.name) : ts.getGeneratedNameForNode(node); } - function onEmitNode(node, emit) { + function onEmitNode(emitContext, node, emitCallback) { if (node.kind === 256 /* SourceFile */) { bindingNameExportSpecifiersMap = bindingNameExportSpecifiersForFileMap[ts.getOriginalNodeId(node)]; - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); bindingNameExportSpecifiersMap = undefined; } else { - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); } } /** @@ -45879,9 +47214,9 @@ var ts; * @param isExpression A value indicating whether the node is to be used in an expression * position. */ - function onSubstituteNode(node, isExpression) { - node = previousOnSubstituteNode(node, isExpression); - if (isExpression) { + function onSubstituteNode(emitContext, node) { + node = previousOnSubstituteNode(emitContext, node); + if (emitContext === 1 /* Expression */) { return substituteExpression(node); } else if (ts.isShorthandPropertyAssignment(node)) { @@ -45925,7 +47260,7 @@ var ts; // If the left-hand-side of the binaryExpression is an identifier and its is export through export Specifier if (ts.isIdentifier(left) && ts.isAssignmentOperator(node.operatorToken.kind)) { if (bindingNameExportSpecifiersMap && ts.hasProperty(bindingNameExportSpecifiersMap, left.text)) { - setNodeEmitFlags(node, 128 /* NoSubstitution */); + ts.setEmitFlags(node, 128 /* NoSubstitution */); var nestedExportAssignment = void 0; for (var _i = 0, _a = bindingNameExportSpecifiersMap[left.text]; _i < _a.length; _i++) { var specifier = _a[_i]; @@ -45945,13 +47280,13 @@ var ts; var operand = node.operand; if (ts.isIdentifier(operand) && bindingNameExportSpecifiersForFileMap) { if (bindingNameExportSpecifiersMap && ts.hasProperty(bindingNameExportSpecifiersMap, operand.text)) { - setNodeEmitFlags(node, 128 /* NoSubstitution */); + ts.setEmitFlags(node, 128 /* NoSubstitution */); var transformedUnaryExpression = void 0; if (node.kind === 186 /* PostfixUnaryExpression */) { - transformedUnaryExpression = ts.createBinary(operand, ts.createNode(operator === 41 /* PlusPlusToken */ ? 57 /* PlusEqualsToken */ : 58 /* MinusEqualsToken */), ts.createLiteral(1), + transformedUnaryExpression = ts.createBinary(operand, ts.createToken(operator === 41 /* PlusPlusToken */ ? 57 /* PlusEqualsToken */ : 58 /* MinusEqualsToken */), ts.createLiteral(1), /*location*/ node); // We have to set no substitution flag here to prevent visit the binary expression and substitute it again as we will preform all necessary substitution in here - setNodeEmitFlags(transformedUnaryExpression, 128 /* NoSubstitution */); + ts.setEmitFlags(transformedUnaryExpression, 128 /* NoSubstitution */); } var nestedExportAssignment = void 0; for (var _i = 0, _a = bindingNameExportSpecifiersMap[operand.text]; _i < _a.length; _i++) { @@ -45966,7 +47301,7 @@ var ts; return node; } function trySubstituteExportedName(node) { - var emitFlags = getNodeEmitFlags(node); + var emitFlags = ts.getEmitFlags(node); if ((emitFlags & 262144 /* LocalName */) === 0) { var container = resolver.getReferencedExportContainer(node, (emitFlags & 131072 /* ExportName */) !== 0); if (container) { @@ -45979,7 +47314,7 @@ var ts; return undefined; } function trySubstituteImportedName(node) { - if ((getNodeEmitFlags(node) & 262144 /* LocalName */) === 0) { + if ((ts.getEmitFlags(node) & 262144 /* LocalName */) === 0) { var declaration = resolver.getReferencedImportDeclaration(node); if (declaration) { if (ts.isImportClause(declaration)) { @@ -45994,14 +47329,14 @@ var ts; } } else if (ts.isImportSpecifier(declaration)) { - var name_34 = declaration.propertyName || declaration.name; - if (name_34.originalKeywordKind === 77 /* DefaultKeyword */ && languageVersion <= 0 /* ES3 */) { + var name_35 = declaration.propertyName || declaration.name; + if (name_35.originalKeywordKind === 77 /* DefaultKeyword */ && languageVersion <= 0 /* ES3 */) { // TODO: ES3 transform to handle x.default -> x["default"] - return ts.createElementAccess(ts.getGeneratedNameForNode(declaration.parent.parent.parent), ts.createLiteral(name_34.text), + return ts.createElementAccess(ts.getGeneratedNameForNode(declaration.parent.parent.parent), ts.createLiteral(name_35.text), /*location*/ node); } else { - return ts.createPropertyAccess(ts.getGeneratedNameForNode(declaration.parent.parent.parent), ts.getSynthesizedClone(name_34), + return ts.createPropertyAccess(ts.getGeneratedNameForNode(declaration.parent.parent.parent), ts.getSynthesizedClone(name_35), /*location*/ node); } } @@ -46025,7 +47360,7 @@ var ts; var statement = ts.createStatement(createExportAssignment(name, value)); statement.startsOnNewLine = true; if (location) { - setSourceMapRange(statement, location); + ts.setSourceMapRange(statement, location); } return statement; } @@ -46063,7 +47398,7 @@ var ts; if (includeNonAmdDependencies && importAliasName) { // Set emitFlags on the name of the classDeclaration // This is so that when printer will not substitute the identifier - setNodeEmitFlags(importAliasName, 128 /* NoSubstitution */); + ts.setEmitFlags(importAliasName, 128 /* NoSubstitution */); aliasedModuleNames.push(externalModuleName); importAliasNames.push(ts.createParameter(importAliasName)); } @@ -46098,6 +47433,9 @@ var ts; * @param node A SourceFile node. */ function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } currentSourceFile = node; node = ts.visitEachChild(node, visitor, context); currentSourceFile = undefined; @@ -46280,12 +47618,12 @@ var ts; return getTagName(node.openingElement); } else { - var name_35 = node.tagName; - if (ts.isIdentifier(name_35) && ts.isIntrinsicJsxName(name_35.text)) { - return ts.createLiteral(name_35.text); + var name_36 = node.tagName; + if (ts.isIdentifier(name_36) && ts.isIntrinsicJsxName(name_36.text)) { + return ts.createLiteral(name_36.text); } else { - return ts.createExpressionFromEntityName(name_35); + return ts.createExpressionFromEntityName(name_36); } } } @@ -46575,6 +47913,9 @@ var ts; var hoistVariableDeclaration = context.hoistVariableDeclaration; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } return ts.visitEachChild(node, visitor, context); } function visitor(node) { @@ -46820,7 +48161,7 @@ var ts; _a[7 /* Endfinally */] = "endfinally", _a)); function transformGenerators(context) { - var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistFunctionDeclaration = context.hoistFunctionDeclaration, hoistVariableDeclaration = context.hoistVariableDeclaration, setSourceMapRange = context.setSourceMapRange, setCommentRange = context.setCommentRange, setNodeEmitFlags = context.setNodeEmitFlags; + var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistFunctionDeclaration = context.hoistFunctionDeclaration, hoistVariableDeclaration = context.hoistVariableDeclaration; var compilerOptions = context.getCompilerOptions(); var languageVersion = ts.getEmitScriptTarget(compilerOptions); var resolver = context.getEmitResolver(); @@ -46870,6 +48211,9 @@ var ts; var withBlockStack; // A stack containing `with` blocks. return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } if (node.transformFlags & 1024 /* ContainsGenerator */) { currentSourceFile = node; node = ts.visitEachChild(node, visitor, context); @@ -47011,7 +48355,7 @@ var ts; */ function visitFunctionDeclaration(node) { // Currently, we only support generators that were originally async functions. - if (node.asteriskToken && node.emitFlags & 2097152 /* AsyncFunctionBody */) { + if (node.asteriskToken && ts.getEmitFlags(node) & 2097152 /* AsyncFunctionBody */) { node = ts.setOriginalNode(ts.createFunctionDeclaration( /*decorators*/ undefined, /*modifiers*/ undefined, @@ -47050,7 +48394,7 @@ var ts; */ function visitFunctionExpression(node) { // Currently, we only support generators that were originally async functions. - if (node.asteriskToken && node.emitFlags & 2097152 /* AsyncFunctionBody */) { + if (node.asteriskToken && ts.getEmitFlags(node) & 2097152 /* AsyncFunctionBody */) { node = ts.setOriginalNode(ts.createFunctionExpression( /*asteriskToken*/ undefined, node.name, /*typeParameters*/ undefined, node.parameters, @@ -47099,6 +48443,7 @@ var ts; var savedBlocks = blocks; var savedBlockOffsets = blockOffsets; var savedBlockActions = blockActions; + var savedBlockStack = blockStack; var savedLabelOffsets = labelOffsets; var savedLabelExpressions = labelExpressions; var savedNextLabelId = nextLabelId; @@ -47112,6 +48457,7 @@ var ts; blocks = undefined; blockOffsets = undefined; blockActions = undefined; + blockStack = undefined; labelOffsets = undefined; labelExpressions = undefined; nextLabelId = 1; @@ -47132,6 +48478,7 @@ var ts; blocks = savedBlocks; blockOffsets = savedBlockOffsets; blockActions = savedBlockActions; + blockStack = savedBlockStack; labelOffsets = savedLabelOffsets; labelExpressions = savedLabelExpressions; nextLabelId = savedNextLabelId; @@ -47156,7 +48503,7 @@ var ts; } else { // Do not hoist custom prologues. - if (node.emitFlags & 8388608 /* CustomPrologue */) { + if (ts.getEmitFlags(node) & 8388608 /* CustomPrologue */) { return node; } for (var _i = 0, _a = node.declarationList.declarations; _i < _a.length; _i++) { @@ -48202,9 +49549,9 @@ var ts; } return -1; } - function onSubstituteNode(node, isExpression) { - node = previousOnSubstituteNode(node, isExpression); - if (isExpression) { + function onSubstituteNode(emitContext, node) { + node = previousOnSubstituteNode(emitContext, node); + if (emitContext === 1 /* Expression */) { return substituteExpression(node); } return node; @@ -48221,11 +49568,11 @@ var ts; if (ts.isIdentifier(original) && original.parent) { var declaration = resolver.getReferencedValueDeclaration(original); if (declaration) { - var name_36 = ts.getProperty(renamedCatchVariableDeclarations, String(ts.getOriginalNodeId(declaration))); - if (name_36) { - var clone_7 = ts.getMutableClone(name_36); - setSourceMapRange(clone_7, node); - setCommentRange(clone_7, node); + var name_37 = ts.getProperty(renamedCatchVariableDeclarations, String(ts.getOriginalNodeId(declaration))); + if (name_37) { + var clone_7 = ts.getMutableClone(name_37); + ts.setSourceMapRange(clone_7, node); + ts.setCommentRange(clone_7, node); return clone_7; } } @@ -48815,7 +50162,7 @@ var ts; return ts.createCall(ts.createHelperName(currentSourceFile.externalHelpersModuleName, "__generator"), /*typeArguments*/ undefined, [ ts.createThis(), - setNodeEmitFlags(ts.createFunctionExpression( + ts.setEmitFlags(ts.createFunctionExpression( /*asteriskToken*/ undefined, /*name*/ undefined, /*typeParameters*/ undefined, [ts.createParameter(state)], @@ -49218,8 +50565,32 @@ var ts; Jump[Jump["Continue"] = 4] = "Continue"; Jump[Jump["Return"] = 8] = "Return"; })(Jump || (Jump = {})); + var SuperCaptureResult; + (function (SuperCaptureResult) { + /** + * A capture may have been added for calls to 'super', but + * the caller should emit subsequent statements normally. + */ + SuperCaptureResult[SuperCaptureResult["NoReplacement"] = 0] = "NoReplacement"; + /** + * A call to 'super()' got replaced with a capturing statement like: + * + * var _this = _super.call(...) || this; + * + * Callers should skip the current statement. + */ + SuperCaptureResult[SuperCaptureResult["ReplaceSuperCapture"] = 1] = "ReplaceSuperCapture"; + /** + * A call to 'super()' got replaced with a capturing statement like: + * + * return _super.call(...) || this; + * + * Callers should skip the current statement and avoid any returns of '_this'. + */ + SuperCaptureResult[SuperCaptureResult["ReplaceWithReturn"] = 2] = "ReplaceWithReturn"; + })(SuperCaptureResult || (SuperCaptureResult = {})); function transformES6(context) { - var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration, getNodeEmitFlags = context.getNodeEmitFlags, setNodeEmitFlags = context.setNodeEmitFlags, getCommentRange = context.getCommentRange, setCommentRange = context.setCommentRange, getSourceMapRange = context.getSourceMapRange, setSourceMapRange = context.setSourceMapRange, setTokenSourceMapRange = context.setTokenSourceMapRange; + var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration; var resolver = context.getEmitResolver(); var previousOnSubstituteNode = context.onSubstituteNode; var previousOnEmitNode = context.onEmitNode; @@ -49247,6 +50618,9 @@ var ts; var enabledSubstitutions; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } currentSourceFile = node; currentText = node.text; return ts.visitNode(node, visitor, ts.isSourceFile); @@ -49418,7 +50792,7 @@ var ts; enclosingFunction = currentNode; if (currentNode.kind !== 180 /* ArrowFunction */) { enclosingNonArrowFunction = currentNode; - if (!(currentNode.emitFlags & 2097152 /* AsyncFunctionBody */)) { + if (!(ts.getEmitFlags(currentNode) & 2097152 /* AsyncFunctionBody */)) { enclosingNonAsyncFunctionBody = currentNode; } } @@ -49634,17 +51008,17 @@ var ts; // To preserve the behavior of the old emitter, we explicitly indent // the body of the function here if it was requested in an earlier // transformation. - if (getNodeEmitFlags(node) & 524288 /* Indented */) { - setNodeEmitFlags(classFunction, 524288 /* Indented */); + if (ts.getEmitFlags(node) & 524288 /* Indented */) { + ts.setEmitFlags(classFunction, 524288 /* Indented */); } // "inner" and "outer" below are added purely to preserve source map locations from // the old emitter var inner = ts.createPartiallyEmittedExpression(classFunction); inner.end = node.end; - setNodeEmitFlags(inner, 49152 /* NoComments */); + ts.setEmitFlags(inner, 49152 /* NoComments */); var outer = ts.createPartiallyEmittedExpression(inner); outer.end = ts.skipTrivia(currentText, node.pos); - setNodeEmitFlags(outer, 49152 /* NoComments */); + ts.setEmitFlags(outer, 49152 /* NoComments */); return ts.createParen(ts.createCall(outer, /*typeArguments*/ undefined, extendsClauseElement ? [ts.visitNode(extendsClauseElement.expression, visitor, ts.isExpression)] @@ -49669,14 +51043,14 @@ var ts; // emit with the original emitter. var outer = ts.createPartiallyEmittedExpression(localName); outer.end = closingBraceLocation.end; - setNodeEmitFlags(outer, 49152 /* NoComments */); + ts.setEmitFlags(outer, 49152 /* NoComments */); var statement = ts.createReturn(outer); statement.pos = closingBraceLocation.pos; - setNodeEmitFlags(statement, 49152 /* NoComments */ | 12288 /* NoTokenSourceMaps */); + ts.setEmitFlags(statement, 49152 /* NoComments */ | 12288 /* NoTokenSourceMaps */); statements.push(statement); ts.addRange(statements, endLexicalEnvironment()); var block = ts.createBlock(ts.createNodeArray(statements, /*location*/ node.members), /*location*/ undefined, /*multiLine*/ true); - setNodeEmitFlags(block, 49152 /* NoComments */); + ts.setEmitFlags(block, 49152 /* NoComments */); return block; } /** @@ -49702,13 +51076,17 @@ var ts; function addConstructor(statements, node, extendsClauseElement) { var constructor = ts.getFirstConstructorWithBody(node); var hasSynthesizedSuper = hasSynthesizedDefaultSuperCall(constructor, extendsClauseElement !== undefined); - statements.push(ts.createFunctionDeclaration( + var constructorFunction = ts.createFunctionDeclaration( /*decorators*/ undefined, /*modifiers*/ undefined, /*asteriskToken*/ undefined, getDeclarationName(node), /*typeParameters*/ undefined, transformConstructorParameters(constructor, hasSynthesizedSuper), /*type*/ undefined, transformConstructorBody(constructor, node, extendsClauseElement, hasSynthesizedSuper), - /*location*/ constructor || node)); + /*location*/ constructor || node); + if (extendsClauseElement) { + ts.setEmitFlags(constructorFunction, 256 /* CapturesThis */); + } + statements.push(constructorFunction); } /** * Transforms the parameters of the constructor declaration of a class. @@ -49740,51 +51118,146 @@ var ts; function transformConstructorBody(constructor, node, extendsClauseElement, hasSynthesizedSuper) { var statements = []; startLexicalEnvironment(); + var statementOffset = -1; + if (hasSynthesizedSuper) { + // If a super call has already been synthesized, + // we're going to assume that we should just transform everything after that. + // The assumption is that no prior step in the pipeline has added any prologue directives. + statementOffset = 1; + } + else if (constructor) { + // Otherwise, try to emit all potential prologue directives first. + statementOffset = ts.addPrologueDirectives(statements, constructor.body.statements, /*ensureUseStrict*/ false, visitor); + } if (constructor) { - addCaptureThisForNodeIfNeeded(statements, constructor); addDefaultValueAssignmentsIfNeeded(statements, constructor); addRestParameterIfNeeded(statements, constructor, hasSynthesizedSuper); + ts.Debug.assert(statementOffset >= 0, "statementOffset not initialized correctly!"); + } + var superCaptureStatus = declareOrCaptureOrReturnThisForConstructorIfNeeded(statements, constructor, !!extendsClauseElement, hasSynthesizedSuper, statementOffset); + // The last statement expression was replaced. Skip it. + if (superCaptureStatus === 1 /* ReplaceSuperCapture */ || superCaptureStatus === 2 /* ReplaceWithReturn */) { + statementOffset++; } - addDefaultSuperCallIfNeeded(statements, constructor, extendsClauseElement, hasSynthesizedSuper); if (constructor) { - var body = saveStateAndInvoke(constructor, hasSynthesizedSuper ? transformConstructorBodyWithSynthesizedSuper : transformConstructorBodyWithoutSynthesizedSuper); + var body = saveStateAndInvoke(constructor, function (constructor) { return ts.visitNodes(constructor.body.statements, visitor, ts.isStatement, /*start*/ statementOffset); }); ts.addRange(statements, body); } + // Return `_this` unless we're sure enough that it would be pointless to add a return statement. + // If there's a constructor that we can tell returns in enough places, then we *do not* want to add a return. + if (extendsClauseElement + && superCaptureStatus !== 2 /* ReplaceWithReturn */ + && !(constructor && isSufficientlyCoveredByReturnStatements(constructor.body))) { + statements.push(ts.createReturn(ts.createIdentifier("_this"))); + } ts.addRange(statements, endLexicalEnvironment()); var block = ts.createBlock(ts.createNodeArray(statements, /*location*/ constructor ? constructor.body.statements : node.members), /*location*/ constructor ? constructor.body : node, /*multiLine*/ true); if (!constructor) { - setNodeEmitFlags(block, 49152 /* NoComments */); + ts.setEmitFlags(block, 49152 /* NoComments */); } return block; } - function transformConstructorBodyWithSynthesizedSuper(node) { - return ts.visitNodes(node.body.statements, visitor, ts.isStatement, 1); - } - function transformConstructorBodyWithoutSynthesizedSuper(node) { - return ts.visitNodes(node.body.statements, visitor, ts.isStatement, 0); + /** + * We want to try to avoid emitting a return statement in certain cases if a user already returned something. + * It would generate obviously dead code, so we'll try to make things a little bit prettier + * by doing a minimal check on whether some common patterns always explicitly return. + */ + function isSufficientlyCoveredByReturnStatements(statement) { + // A return statement is considered covered. + if (statement.kind === 211 /* ReturnStatement */) { + return true; + } + else if (statement.kind === 203 /* IfStatement */) { + var ifStatement = statement; + if (ifStatement.elseStatement) { + return isSufficientlyCoveredByReturnStatements(ifStatement.thenStatement) && + isSufficientlyCoveredByReturnStatements(ifStatement.elseStatement); + } + } + else if (statement.kind === 199 /* Block */) { + var lastStatement = ts.lastOrUndefined(statement.statements); + if (lastStatement && isSufficientlyCoveredByReturnStatements(lastStatement)) { + return true; + } + } + return false; } /** - * Adds a synthesized call to `_super` if it is needed. + * Declares a `_this` variable for derived classes and for when arrow functions capture `this`. * - * @param statements The statements for the new constructor body. - * @param constructor The constructor for the class. - * @param extendsClauseElement The expression for the class `extends` clause. - * @param hasSynthesizedSuper A value indicating whether the constructor starts with a - * synthesized `super` call. + * @returns The new statement offset into the `statements` array. */ - function addDefaultSuperCallIfNeeded(statements, constructor, extendsClauseElement, hasSynthesizedSuper) { - // If the TypeScript transformer needed to synthesize a constructor for property - // initializers, it would have also added a synthetic `...args` parameter and - // `super` call. - // If this is the case, or if the class has an `extends` clause but no - // constructor, we emit a synthesized call to `_super`. - if (constructor ? hasSynthesizedSuper : extendsClauseElement) { - statements.push(ts.createStatement(ts.createFunctionApply(ts.createIdentifier("_super"), ts.createThis(), ts.createIdentifier("arguments")), - /*location*/ extendsClauseElement)); + function declareOrCaptureOrReturnThisForConstructorIfNeeded(statements, ctor, hasExtendsClause, hasSynthesizedSuper, statementOffset) { + // If this isn't a derived class, just capture 'this' for arrow functions if necessary. + if (!hasExtendsClause) { + if (ctor) { + addCaptureThisForNodeIfNeeded(statements, ctor); + } + return 0 /* NoReplacement */; + } + // We must be here because the user didn't write a constructor + // but we needed to call 'super(...args)' anyway as per 14.5.14 of the ES2016 spec. + // If that's the case we can just immediately return the result of a 'super()' call. + if (!ctor) { + statements.push(ts.createReturn(createDefaultSuperCallOrThis())); + return 2 /* ReplaceWithReturn */; + } + // The constructor exists, but it and the 'super()' call it contains were generated + // for something like property initializers. + // Create a captured '_this' variable and assume it will subsequently be used. + if (hasSynthesizedSuper) { + captureThisForNode(statements, ctor, createDefaultSuperCallOrThis()); + enableSubstitutionsForCapturedThis(); + return 1 /* ReplaceSuperCapture */; } + // Most of the time, a 'super' call will be the first real statement in a constructor body. + // In these cases, we'd like to transform these into a *single* statement instead of a declaration + // followed by an assignment statement for '_this'. For instance, if we emitted without an initializer, + // we'd get: + // + // var _this; + // _this = _super.call(...) || this; + // + // instead of + // + // var _this = _super.call(...) || this; + // + // Additionally, if the 'super()' call is the last statement, we should just avoid capturing + // entirely and immediately return the result like so: + // + // return _super.call(...) || this; + // + var firstStatement; + var superCallExpression; + var ctorStatements = ctor.body.statements; + if (statementOffset < ctorStatements.length) { + firstStatement = ctorStatements[statementOffset]; + if (firstStatement.kind === 202 /* ExpressionStatement */ && ts.isSuperCall(firstStatement.expression)) { + var superCall = firstStatement.expression; + superCallExpression = ts.setOriginalNode(saveStateAndInvoke(superCall, visitImmediateSuperCallInBody), superCall); + } + } + // Return the result if we have an immediate super() call on the last statement. + if (superCallExpression && statementOffset === ctorStatements.length - 1) { + statements.push(ts.createReturn(superCallExpression)); + return 2 /* ReplaceWithReturn */; + } + // Perform the capture. + captureThisForNode(statements, ctor, superCallExpression, firstStatement); + // If we're actually replacing the original statement, we need to signal this to the caller. + if (superCallExpression) { + return 1 /* ReplaceSuperCapture */; + } + return 0 /* NoReplacement */; + } + function createDefaultSuperCallOrThis() { + var actualThis = ts.createThis(); + ts.setEmitFlags(actualThis, 128 /* NoSubstitution */); + var superCall = ts.createFunctionApply(ts.createIdentifier("_super"), actualThis, ts.createIdentifier("arguments")); + return ts.createLogicalOr(superCall, actualThis); } /** * Visits a parameter declaration. @@ -49837,17 +51310,17 @@ var ts; } for (var _i = 0, _a = node.parameters; _i < _a.length; _i++) { var parameter = _a[_i]; - var name_37 = parameter.name, initializer = parameter.initializer, dotDotDotToken = parameter.dotDotDotToken; + var name_38 = parameter.name, initializer = parameter.initializer, dotDotDotToken = parameter.dotDotDotToken; // A rest parameter cannot have a binding pattern or an initializer, // so let's just ignore it. if (dotDotDotToken) { continue; } - if (ts.isBindingPattern(name_37)) { - addDefaultValueAssignmentForBindingPattern(statements, parameter, name_37, initializer); + if (ts.isBindingPattern(name_38)) { + addDefaultValueAssignmentForBindingPattern(statements, parameter, name_38, initializer); } else if (initializer) { - addDefaultValueAssignmentForInitializer(statements, parameter, name_37, initializer); + addDefaultValueAssignmentForInitializer(statements, parameter, name_38, initializer); } } } @@ -49865,11 +51338,11 @@ var ts; // we usually don't want to emit a var declaration; however, in the presence // of an initializer, we must emit that expression to preserve side effects. if (name.elements.length > 0) { - statements.push(setNodeEmitFlags(ts.createVariableStatement( + statements.push(ts.setEmitFlags(ts.createVariableStatement( /*modifiers*/ undefined, ts.createVariableDeclarationList(ts.flattenParameterDestructuring(context, parameter, temp, visitor))), 8388608 /* CustomPrologue */)); } else if (initializer) { - statements.push(setNodeEmitFlags(ts.createStatement(ts.createAssignment(temp, ts.visitNode(initializer, visitor, ts.isExpression))), 8388608 /* CustomPrologue */)); + statements.push(ts.setEmitFlags(ts.createStatement(ts.createAssignment(temp, ts.visitNode(initializer, visitor, ts.isExpression))), 8388608 /* CustomPrologue */)); } } /** @@ -49882,14 +51355,14 @@ var ts; */ function addDefaultValueAssignmentForInitializer(statements, parameter, name, initializer) { initializer = ts.visitNode(initializer, visitor, ts.isExpression); - var statement = ts.createIf(ts.createStrictEquality(ts.getSynthesizedClone(name), ts.createVoidZero()), setNodeEmitFlags(ts.createBlock([ - ts.createStatement(ts.createAssignment(setNodeEmitFlags(ts.getMutableClone(name), 1536 /* NoSourceMap */), setNodeEmitFlags(initializer, 1536 /* NoSourceMap */ | getNodeEmitFlags(initializer)), + var statement = ts.createIf(ts.createStrictEquality(ts.getSynthesizedClone(name), ts.createVoidZero()), ts.setEmitFlags(ts.createBlock([ + ts.createStatement(ts.createAssignment(ts.setEmitFlags(ts.getMutableClone(name), 1536 /* NoSourceMap */), ts.setEmitFlags(initializer, 1536 /* NoSourceMap */ | ts.getEmitFlags(initializer)), /*location*/ parameter)) ], /*location*/ parameter), 32 /* SingleLine */ | 1024 /* NoTrailingSourceMap */ | 12288 /* NoTokenSourceMaps */), /*elseStatement*/ undefined, /*location*/ parameter); statement.startsOnNewLine = true; - setNodeEmitFlags(statement, 12288 /* NoTokenSourceMaps */ | 1024 /* NoTrailingSourceMap */ | 8388608 /* CustomPrologue */); + ts.setEmitFlags(statement, 12288 /* NoTokenSourceMaps */ | 1024 /* NoTrailingSourceMap */ | 8388608 /* CustomPrologue */); statements.push(statement); } /** @@ -49919,13 +51392,13 @@ var ts; } // `declarationName` is the name of the local declaration for the parameter. var declarationName = ts.getMutableClone(parameter.name); - setNodeEmitFlags(declarationName, 1536 /* NoSourceMap */); + ts.setEmitFlags(declarationName, 1536 /* NoSourceMap */); // `expressionName` is the name of the parameter used in expressions. var expressionName = ts.getSynthesizedClone(parameter.name); var restIndex = node.parameters.length - 1; var temp = ts.createLoopVariable(); // var param = []; - statements.push(setNodeEmitFlags(ts.createVariableStatement( + statements.push(ts.setEmitFlags(ts.createVariableStatement( /*modifiers*/ undefined, ts.createVariableDeclarationList([ ts.createVariableDeclaration(declarationName, /*type*/ undefined, ts.createArrayLiteral([])) @@ -49941,7 +51414,7 @@ var ts; ts.startOnNewLine(ts.createStatement(ts.createAssignment(ts.createElementAccess(expressionName, ts.createSubtract(temp, ts.createLiteral(restIndex))), ts.createElementAccess(ts.createIdentifier("arguments"), temp)), /*location*/ parameter)) ])); - setNodeEmitFlags(forStatement, 8388608 /* CustomPrologue */); + ts.setEmitFlags(forStatement, 8388608 /* CustomPrologue */); ts.startOnNewLine(forStatement); statements.push(forStatement); } @@ -49953,17 +51426,20 @@ var ts; */ function addCaptureThisForNodeIfNeeded(statements, node) { if (node.transformFlags & 16384 /* ContainsCapturedLexicalThis */ && node.kind !== 180 /* ArrowFunction */) { - enableSubstitutionsForCapturedThis(); - var captureThisStatement = ts.createVariableStatement( - /*modifiers*/ undefined, ts.createVariableDeclarationList([ - ts.createVariableDeclaration("_this", - /*type*/ undefined, ts.createThis()) - ])); - setNodeEmitFlags(captureThisStatement, 49152 /* NoComments */ | 8388608 /* CustomPrologue */); - setSourceMapRange(captureThisStatement, node); - statements.push(captureThisStatement); + captureThisForNode(statements, node, ts.createThis()); } } + function captureThisForNode(statements, node, initializer, originalStatement) { + enableSubstitutionsForCapturedThis(); + var captureThisStatement = ts.createVariableStatement( + /*modifiers*/ undefined, ts.createVariableDeclarationList([ + ts.createVariableDeclaration("_this", + /*type*/ undefined, initializer) + ]), originalStatement); + ts.setEmitFlags(captureThisStatement, 49152 /* NoComments */ | 8388608 /* CustomPrologue */); + ts.setSourceMapRange(captureThisStatement, node); + statements.push(captureThisStatement); + } /** * Adds statements to the class body function for a class to define the members of the * class. @@ -50012,20 +51488,20 @@ var ts; * @param member The MethodDeclaration node. */ function transformClassMethodDeclarationToStatement(receiver, member) { - var commentRange = getCommentRange(member); - var sourceMapRange = getSourceMapRange(member); + var commentRange = ts.getCommentRange(member); + var sourceMapRange = ts.getSourceMapRange(member); var func = transformFunctionLikeToExpression(member, /*location*/ member, /*name*/ undefined); - setNodeEmitFlags(func, 49152 /* NoComments */); - setSourceMapRange(func, sourceMapRange); + ts.setEmitFlags(func, 49152 /* NoComments */); + ts.setSourceMapRange(func, sourceMapRange); var statement = ts.createStatement(ts.createAssignment(ts.createMemberAccessForPropertyName(receiver, ts.visitNode(member.name, visitor, ts.isPropertyName), /*location*/ member.name), func), /*location*/ member); ts.setOriginalNode(statement, member); - setCommentRange(statement, commentRange); + ts.setCommentRange(statement, commentRange); // The location for the statement is used to emit comments only. // No source map should be emitted for this statement to align with the // old emitter. - setNodeEmitFlags(statement, 1536 /* NoSourceMap */); + ts.setEmitFlags(statement, 1536 /* NoSourceMap */); return statement; } /** @@ -50036,11 +51512,11 @@ var ts; */ function transformAccessorsToStatement(receiver, accessors) { var statement = ts.createStatement(transformAccessorsToExpression(receiver, accessors, /*startsOnNewLine*/ false), - /*location*/ getSourceMapRange(accessors.firstAccessor)); + /*location*/ ts.getSourceMapRange(accessors.firstAccessor)); // The location for the statement is used to emit source maps only. // No comments should be emitted for this statement to align with the // old emitter. - setNodeEmitFlags(statement, 49152 /* NoComments */); + ts.setEmitFlags(statement, 49152 /* NoComments */); return statement; } /** @@ -50054,24 +51530,24 @@ var ts; // To align with source maps in the old emitter, the receiver and property name // arguments are both mapped contiguously to the accessor name. var target = ts.getMutableClone(receiver); - setNodeEmitFlags(target, 49152 /* NoComments */ | 1024 /* NoTrailingSourceMap */); - setSourceMapRange(target, firstAccessor.name); + ts.setEmitFlags(target, 49152 /* NoComments */ | 1024 /* NoTrailingSourceMap */); + ts.setSourceMapRange(target, firstAccessor.name); var propertyName = ts.createExpressionForPropertyName(ts.visitNode(firstAccessor.name, visitor, ts.isPropertyName)); - setNodeEmitFlags(propertyName, 49152 /* NoComments */ | 512 /* NoLeadingSourceMap */); - setSourceMapRange(propertyName, firstAccessor.name); + ts.setEmitFlags(propertyName, 49152 /* NoComments */ | 512 /* NoLeadingSourceMap */); + ts.setSourceMapRange(propertyName, firstAccessor.name); var properties = []; if (getAccessor) { var getterFunction = transformFunctionLikeToExpression(getAccessor, /*location*/ undefined, /*name*/ undefined); - setSourceMapRange(getterFunction, getSourceMapRange(getAccessor)); + ts.setSourceMapRange(getterFunction, ts.getSourceMapRange(getAccessor)); var getter = ts.createPropertyAssignment("get", getterFunction); - setCommentRange(getter, getCommentRange(getAccessor)); + ts.setCommentRange(getter, ts.getCommentRange(getAccessor)); properties.push(getter); } if (setAccessor) { var setterFunction = transformFunctionLikeToExpression(setAccessor, /*location*/ undefined, /*name*/ undefined); - setSourceMapRange(setterFunction, getSourceMapRange(setAccessor)); + ts.setSourceMapRange(setterFunction, ts.getSourceMapRange(setAccessor)); var setter = ts.createPropertyAssignment("set", setterFunction); - setCommentRange(setter, getCommentRange(setAccessor)); + ts.setCommentRange(setter, ts.getCommentRange(setAccessor)); properties.push(setter); } properties.push(ts.createPropertyAssignment("enumerable", ts.createLiteral(true)), ts.createPropertyAssignment("configurable", ts.createLiteral(true))); @@ -50096,7 +51572,7 @@ var ts; enableSubstitutionsForCapturedThis(); } var func = transformFunctionLikeToExpression(node, /*location*/ node, /*name*/ undefined); - setNodeEmitFlags(func, 256 /* CapturesThis */); + ts.setEmitFlags(func, 256 /* CapturesThis */); return func; } /** @@ -50191,7 +51667,7 @@ var ts; } var expression = ts.visitNode(body, visitor, ts.isExpression); var returnStatement = ts.createReturn(expression, /*location*/ body); - setNodeEmitFlags(returnStatement, 12288 /* NoTokenSourceMaps */ | 1024 /* NoTrailingSourceMap */ | 32768 /* NoTrailingComments */); + ts.setEmitFlags(returnStatement, 12288 /* NoTokenSourceMaps */ | 1024 /* NoTrailingSourceMap */ | 32768 /* NoTrailingComments */); statements.push(returnStatement); // To align with the source map emit for the old emitter, we set a custom // source map location for the close brace. @@ -50205,10 +51681,10 @@ var ts; } var block = ts.createBlock(ts.createNodeArray(statements, statementsLocation), node.body, multiLine); if (!multiLine && singleLine) { - setNodeEmitFlags(block, 32 /* SingleLine */); + ts.setEmitFlags(block, 32 /* SingleLine */); } if (closeBraceLocation) { - setTokenSourceMapRange(block, 16 /* CloseBraceToken */, closeBraceLocation); + ts.setTokenSourceMapRange(block, 16 /* CloseBraceToken */, closeBraceLocation); } ts.setOriginalNode(block, node.body); return block; @@ -50274,7 +51750,7 @@ var ts; assignment = ts.flattenVariableDestructuringToExpression(context, decl, hoistVariableDeclaration, /*nameSubstitution*/ undefined, visitor); } else { - assignment = ts.createBinary(decl.name, 56 /* EqualsToken */, decl.initializer); + assignment = ts.createBinary(decl.name, 56 /* EqualsToken */, ts.visitNode(decl.initializer, visitor, ts.isExpression)); } (assignments || (assignments = [])).push(assignment); } @@ -50303,7 +51779,7 @@ var ts; : visitVariableDeclaration)); var declarationList = ts.createVariableDeclarationList(declarations, /*location*/ node); ts.setOriginalNode(declarationList, node); - setCommentRange(declarationList, node); + ts.setCommentRange(declarationList, node); if (node.transformFlags & 2097152 /* ContainsBindingPattern */ && (ts.isBindingPattern(node.declarations[0].name) || ts.isBindingPattern(ts.lastOrUndefined(node.declarations).name))) { @@ -50311,7 +51787,7 @@ var ts; // the source map range for the declaration list. var firstDeclaration = ts.firstOrUndefined(declarations); var lastDeclaration = ts.lastOrUndefined(declarations); - setSourceMapRange(declarationList, ts.createRange(firstDeclaration.pos, lastDeclaration.end)); + ts.setSourceMapRange(declarationList, ts.createRange(firstDeclaration.pos, lastDeclaration.end)); } return declarationList; } @@ -50501,7 +51977,7 @@ var ts; // emitter. var firstDeclaration = declarations[0]; var lastDeclaration = ts.lastOrUndefined(declarations); - setSourceMapRange(declarationList, ts.createRange(firstDeclaration.pos, lastDeclaration.end)); + ts.setSourceMapRange(declarationList, ts.createRange(firstDeclaration.pos, lastDeclaration.end)); statements.push(ts.createVariableStatement( /*modifiers*/ undefined, declarationList)); } @@ -50549,12 +52025,12 @@ var ts; } } // The old emitter does not emit source maps for the expression - setNodeEmitFlags(expression, 1536 /* NoSourceMap */ | getNodeEmitFlags(expression)); + ts.setEmitFlags(expression, 1536 /* NoSourceMap */ | ts.getEmitFlags(expression)); // The old emitter does not emit source maps for the block. // We add the location to preserve comments. var body = ts.createBlock(ts.createNodeArray(statements, /*location*/ statementsLocation), /*location*/ bodyLocation); - setNodeEmitFlags(body, 1536 /* NoSourceMap */ | 12288 /* NoTokenSourceMaps */); + ts.setEmitFlags(body, 1536 /* NoSourceMap */ | 12288 /* NoTokenSourceMaps */); var forStatement = ts.createFor(ts.createVariableDeclarationList([ ts.createVariableDeclaration(counter, /*type*/ undefined, ts.createLiteral(0), /*location*/ ts.moveRangePos(node.expression, -1)), ts.createVariableDeclaration(rhsReference, /*type*/ undefined, expression, /*location*/ node.expression) @@ -50562,7 +52038,7 @@ var ts; /*location*/ node.expression), ts.createPostfixIncrement(counter, /*location*/ node.expression), body, /*location*/ node); // Disable trailing source maps for the OpenParenToken to align source map emit with the old emitter. - setNodeEmitFlags(forStatement, 8192 /* NoTokenTrailingSourceMaps */); + ts.setEmitFlags(forStatement, 8192 /* NoTokenTrailingSourceMaps */); return forStatement; } /** @@ -50591,7 +52067,7 @@ var ts; var temp = ts.createTempVariable(hoistVariableDeclaration); // Write out the first non-computed properties, then emit the rest through indexing on the temp variable. var expressions = []; - var assignment = ts.createAssignment(temp, setNodeEmitFlags(ts.createObjectLiteral(ts.visitNodes(properties, visitor, ts.isObjectLiteralElementLike, 0, numInitialProperties), + var assignment = ts.createAssignment(temp, ts.setEmitFlags(ts.createObjectLiteral(ts.visitNodes(properties, visitor, ts.isObjectLiteralElementLike, 0, numInitialProperties), /*location*/ undefined, node.multiLine), 524288 /* Indented */)); if (node.multiLine) { assignment.startsOnNewLine = true; @@ -50698,7 +52174,7 @@ var ts; loopBody = ts.createBlock([loopBody], /*location*/ undefined, /*multiline*/ true); } var isAsyncBlockContainingAwait = enclosingNonArrowFunction - && (enclosingNonArrowFunction.emitFlags & 2097152 /* AsyncFunctionBody */) !== 0 + && (ts.getEmitFlags(enclosingNonArrowFunction) & 2097152 /* AsyncFunctionBody */) !== 0 && (node.statement.transformFlags & 4194304 /* ContainsYield */) !== 0; var loopBodyFlags = 0; if (currentState.containsLexicalThis) { @@ -50710,7 +52186,7 @@ var ts; var convertedLoopVariable = ts.createVariableStatement( /*modifiers*/ undefined, ts.createVariableDeclarationList([ ts.createVariableDeclaration(functionName, - /*type*/ undefined, setNodeEmitFlags(ts.createFunctionExpression(isAsyncBlockContainingAwait ? ts.createToken(37 /* AsteriskToken */) : undefined, + /*type*/ undefined, ts.setEmitFlags(ts.createFunctionExpression(isAsyncBlockContainingAwait ? ts.createToken(37 /* AsteriskToken */) : undefined, /*name*/ undefined, /*typeParameters*/ undefined, loopParameters, /*type*/ undefined, loopBody), loopBodyFlags)) @@ -50756,8 +52232,8 @@ var ts; extraVariableDeclarations = []; } // hoist collected variable declarations - for (var name_38 in currentState.hoistedLocalVariables) { - var identifier = currentState.hoistedLocalVariables[name_38]; + for (var _b = 0, _c = currentState.hoistedLocalVariables; _b < _c.length; _b++) { + var identifier = _c[_b]; extraVariableDeclarations.push(ts.createVariableDeclaration(identifier)); } } @@ -50767,8 +52243,8 @@ var ts; if (!extraVariableDeclarations) { extraVariableDeclarations = []; } - for (var _b = 0, loopOutParameters_1 = loopOutParameters; _b < loopOutParameters_1.length; _b++) { - var outParam = loopOutParameters_1[_b]; + for (var _d = 0, loopOutParameters_1 = loopOutParameters; _d < loopOutParameters_1.length; _d++) { + var outParam = loopOutParameters_1[_d]; extraVariableDeclarations.push(ts.createVariableDeclaration(outParam.outParamName)); } } @@ -51003,7 +52479,7 @@ var ts; // Methods with computed property names are handled in visitObjectLiteralExpression. ts.Debug.assert(!ts.isComputedPropertyName(node.name)); var functionExpression = transformFunctionLikeToExpression(node, /*location*/ ts.moveRangePos(node, -1), /*name*/ undefined); - setNodeEmitFlags(functionExpression, 16384 /* NoLeadingComments */ | getNodeEmitFlags(functionExpression)); + ts.setEmitFlags(functionExpression, 16384 /* NoLeadingComments */ | ts.getEmitFlags(functionExpression)); return ts.createPropertyAssignment(node.name, functionExpression, /*location*/ node); } @@ -51040,9 +52516,19 @@ var ts; * @param node a CallExpression. */ function visitCallExpression(node) { + return visitCallExpressionWithPotentialCapturedThisAssignment(node, /*assignToCapturedThis*/ true); + } + function visitImmediateSuperCallInBody(node) { + return visitCallExpressionWithPotentialCapturedThisAssignment(node, /*assignToCapturedThis*/ false); + } + function visitCallExpressionWithPotentialCapturedThisAssignment(node, assignToCapturedThis) { // We are here either because SuperKeyword was used somewhere in the expression, or // because we contain a SpreadElementExpression. var _a = ts.createCallBinding(node.expression, hoistVariableDeclaration), target = _a.target, thisArg = _a.thisArg; + if (node.expression.kind === 95 /* SuperKeyword */) { + ts.setEmitFlags(thisArg, 128 /* NoSubstitution */); + } + var resultingCall; if (node.transformFlags & 262144 /* ContainsSpreadElementExpression */) { // [source] // f(...a, b) @@ -51057,7 +52543,7 @@ var ts; // _super.apply(this, a.concat([b])) // _super.m.apply(this, a.concat([b])) // _super.prototype.m.apply(this, a.concat([b])) - return ts.createFunctionApply(ts.visitNode(target, visitor, ts.isExpression), ts.visitNode(thisArg, visitor, ts.isExpression), transformAndSpreadElements(node.arguments, /*needsUniqueCopy*/ false, /*multiLine*/ false, /*hasTrailingComma*/ false)); + resultingCall = ts.createFunctionApply(ts.visitNode(target, visitor, ts.isExpression), ts.visitNode(thisArg, visitor, ts.isExpression), transformAndSpreadElements(node.arguments, /*needsUniqueCopy*/ false, /*multiLine*/ false, /*hasTrailingComma*/ false)); } else { // [source] @@ -51069,9 +52555,18 @@ var ts; // _super.call(this, a) // _super.m.call(this, a) // _super.prototype.m.call(this, a) - return ts.createFunctionCall(ts.visitNode(target, visitor, ts.isExpression), ts.visitNode(thisArg, visitor, ts.isExpression), ts.visitNodes(node.arguments, visitor, ts.isExpression), + resultingCall = ts.createFunctionCall(ts.visitNode(target, visitor, ts.isExpression), ts.visitNode(thisArg, visitor, ts.isExpression), ts.visitNodes(node.arguments, visitor, ts.isExpression), /*location*/ node); } + if (node.expression.kind === 95 /* SuperKeyword */) { + var actualThis = ts.createThis(); + ts.setEmitFlags(actualThis, 128 /* NoSubstitution */); + var initializer = ts.createLogicalOr(resultingCall, actualThis); + return assignToCapturedThis + ? ts.createAssignment(ts.createIdentifier("_this"), initializer) + : initializer; + } + return resultingCall; } /** * Visits a NewExpression that contains a spread element. @@ -51313,13 +52808,13 @@ var ts; * * @param node The node to be printed. */ - function onEmitNode(node, emit) { + function onEmitNode(emitContext, node, emitCallback) { var savedEnclosingFunction = enclosingFunction; if (enabledSubstitutions & 1 /* CapturedThis */ && ts.isFunctionLike(node)) { // If we are tracking a captured `this`, keep track of the enclosing function. enclosingFunction = node; } - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); enclosingFunction = savedEnclosingFunction; } /** @@ -51356,9 +52851,9 @@ var ts; * @param isExpression A value indicating whether the node is to be used in an expression * position. */ - function onSubstituteNode(node, isExpression) { - node = previousOnSubstituteNode(node, isExpression); - if (isExpression) { + function onSubstituteNode(emitContext, node) { + node = previousOnSubstituteNode(emitContext, node); + if (emitContext === 1 /* Expression */) { return substituteExpression(node); } if (ts.isIdentifier(node)) { @@ -51434,7 +52929,7 @@ var ts; function substituteThisKeyword(node) { if (enabledSubstitutions & 1 /* CapturedThis */ && enclosingFunction - && enclosingFunction.emitFlags & 256 /* CapturesThis */) { + && ts.getEmitFlags(enclosingFunction) & 256 /* CapturesThis */) { return ts.createIdentifier("_this", /*location*/ node); } return node; @@ -51461,7 +52956,7 @@ var ts; function getDeclarationName(node, allowComments, allowSourceMaps, emitFlags) { if (node.name && !ts.isGeneratedIdentifier(node.name)) { var name_39 = ts.getMutableClone(node.name); - emitFlags |= getNodeEmitFlags(node.name); + emitFlags |= ts.getEmitFlags(node.name); if (!allowSourceMaps) { emitFlags |= 1536 /* NoSourceMap */; } @@ -51469,7 +52964,7 @@ var ts; emitFlags |= 49152 /* NoComments */; } if (emitFlags) { - setNodeEmitFlags(name_39, emitFlags); + ts.setEmitFlags(name_39, emitFlags); } return name_39; } @@ -51552,13 +53047,6 @@ var ts; return transformers; } ts.getTransformers = getTransformers; - /** - * Tracks a monotonically increasing transformation id used to associate a node with a specific - * transformation. This ensures transient properties related to transformations can be safely - * stored on source tree nodes that may be reused across multiple transformations (such as - * with compile-on-save). - */ - var nextTransformId = 1; /** * Transforms an array of SourceFiles by passing them through each transformer. * @@ -51568,16 +53056,9 @@ var ts; * @param transforms An array of Transformers. */ function transformFiles(resolver, host, sourceFiles, transformers) { - var transformId = nextTransformId; - nextTransformId++; - var tokenSourceMapRanges = ts.createMap(); var lexicalEnvironmentVariableDeclarationsStack = []; var lexicalEnvironmentFunctionDeclarationsStack = []; var enabledSyntaxKindFeatures = new Array(289 /* Count */); - var parseTreeNodesWithAnnotations = []; - var lastTokenSourceMapRangeNode; - var lastTokenSourceMapRangeToken; - var lastTokenSourceMapRange; var lexicalEnvironmentStackOffset = 0; var hoistedVariableDeclarations; var hoistedFunctionDeclarations; @@ -51588,56 +53069,27 @@ var ts; getCompilerOptions: function () { return host.getCompilerOptions(); }, getEmitResolver: function () { return resolver; }, getEmitHost: function () { return host; }, - getNodeEmitFlags: getNodeEmitFlags, - setNodeEmitFlags: setNodeEmitFlags, - getSourceMapRange: getSourceMapRange, - setSourceMapRange: setSourceMapRange, - getTokenSourceMapRange: getTokenSourceMapRange, - setTokenSourceMapRange: setTokenSourceMapRange, - getCommentRange: getCommentRange, - setCommentRange: setCommentRange, hoistVariableDeclaration: hoistVariableDeclaration, hoistFunctionDeclaration: hoistFunctionDeclaration, startLexicalEnvironment: startLexicalEnvironment, endLexicalEnvironment: endLexicalEnvironment, - onSubstituteNode: onSubstituteNode, + onSubstituteNode: function (emitContext, node) { return node; }, enableSubstitution: enableSubstitution, isSubstitutionEnabled: isSubstitutionEnabled, - onEmitNode: onEmitNode, + onEmitNode: function (node, emitContext, emitCallback) { return emitCallback(node, emitContext); }, enableEmitNotification: enableEmitNotification, isEmitNotificationEnabled: isEmitNotificationEnabled }; // Chain together and initialize each transformer. - var transformation = chain.apply(void 0, transformers)(context); + var transformation = ts.chain.apply(void 0, transformers)(context); // Transform each source file. var transformed = ts.map(sourceFiles, transformSourceFile); // Disable modification of the lexical environment. lexicalEnvironmentDisabled = true; return { - getSourceFiles: function () { return transformed; }, - getTokenSourceMapRange: getTokenSourceMapRange, - isSubstitutionEnabled: isSubstitutionEnabled, - isEmitNotificationEnabled: isEmitNotificationEnabled, - onSubstituteNode: context.onSubstituteNode, - onEmitNode: context.onEmitNode, - dispose: function () { - // During transformation we may need to annotate a parse tree node with transient - // transformation properties. As parse tree nodes live longer than transformation - // nodes, we need to make sure we reclaim any memory allocated for custom ranges - // from these nodes to ensure we do not hold onto entire subtrees just for position - // information. We also need to reset these nodes to a pre-transformation state - // for incremental parsing scenarios so that we do not impact later emit. - for (var _i = 0, parseTreeNodesWithAnnotations_1 = parseTreeNodesWithAnnotations; _i < parseTreeNodesWithAnnotations_1.length; _i++) { - var node = parseTreeNodesWithAnnotations_1[_i]; - if (node.transformId === transformId) { - node.transformId = 0; - node.emitFlags = 0; - node.commentRange = undefined; - node.sourceMapRange = undefined; - } - } - parseTreeNodesWithAnnotations.length = 0; - } + transformed: transformed, + emitNodeWithSubstitution: emitNodeWithSubstitution, + emitNodeWithNotification: emitNodeWithNotification }; /** * Transforms a source file. @@ -51660,17 +53112,27 @@ var ts; * Determines whether expression substitutions are enabled for the provided node. */ function isSubstitutionEnabled(node) { - return (enabledSyntaxKindFeatures[node.kind] & 1 /* Substitution */) !== 0; + return (enabledSyntaxKindFeatures[node.kind] & 1 /* Substitution */) !== 0 + && (ts.getEmitFlags(node) & 128 /* NoSubstitution */) === 0; } /** - * Default hook for node substitutions. + * Emits a node with possible substitution. * - * @param node The node to substitute. - * @param isExpression A value indicating whether the node is to be used in an expression - * position. + * @param emitContext The current emit context. + * @param node The node to emit. + * @param emitCallback The callback used to emit the node or its substitute. */ - function onSubstituteNode(node, isExpression) { - return node; + function emitNodeWithSubstitution(emitContext, node, emitCallback) { + if (node) { + if (isSubstitutionEnabled(node)) { + var substitute = context.onSubstituteNode(emitContext, node); + if (substitute && substitute !== node) { + emitCallback(emitContext, substitute); + return; + } + } + emitCallback(emitContext, node); + } } /** * Enables before/after emit notifications in the pretty printer for the provided SyntaxKind. @@ -51684,141 +53146,24 @@ var ts; */ function isEmitNotificationEnabled(node) { return (enabledSyntaxKindFeatures[node.kind] & 2 /* EmitNotifications */) !== 0 - || (getNodeEmitFlags(node) & 64 /* AdviseOnEmitNode */) !== 0; + || (ts.getEmitFlags(node) & 64 /* AdviseOnEmitNode */) !== 0; } /** - * Default hook for node emit. + * Emits a node with possible emit notification. * + * @param emitContext The current emit context. * @param node The node to emit. - * @param emit A callback used to emit the node in the printer. - */ - function onEmitNode(node, emit) { - emit(node); - } - /** - * Associates a node with the current transformation, initializing - * various transient transformation properties. - * - * @param node The node. + * @param emitCallback The callback used to emit the node. */ - function beforeSetAnnotation(node) { - if ((node.flags & 8 /* Synthesized */) === 0 && node.transformId !== transformId) { - // To avoid holding onto transformation artifacts, we keep track of any - // parse tree node we are annotating. This allows us to clean them up after - // all transformations have completed. - parseTreeNodesWithAnnotations.push(node); - node.transformId = transformId; - } - } - /** - * Gets flags that control emit behavior of a node. - * - * If the node does not have its own NodeEmitFlags set, the node emit flags of its - * original pointer are used. - * - * @param node The node. - */ - function getNodeEmitFlags(node) { - return node.emitFlags; - } - /** - * Sets flags that control emit behavior of a node. - * - * @param node The node. - * @param emitFlags The NodeEmitFlags for the node. - */ - function setNodeEmitFlags(node, emitFlags) { - beforeSetAnnotation(node); - node.emitFlags = emitFlags; - return node; - } - /** - * Gets a custom text range to use when emitting source maps. - * - * If a node does not have its own custom source map text range, the custom source map - * text range of its original pointer is used. - * - * @param node The node. - */ - function getSourceMapRange(node) { - return node.sourceMapRange || node; - } - /** - * Sets a custom text range to use when emitting source maps. - * - * @param node The node. - * @param range The text range. - */ - function setSourceMapRange(node, range) { - beforeSetAnnotation(node); - node.sourceMapRange = range; - return node; - } - /** - * Gets the TextRange to use for source maps for a token of a node. - * - * If a node does not have its own custom source map text range for a token, the custom - * source map text range for the token of its original pointer is used. - * - * @param node The node. - * @param token The token. - */ - function getTokenSourceMapRange(node, token) { - // As a performance optimization, use the cached value of the most recent node. - // This helps for cases where this function is called repeatedly for the same node. - if (lastTokenSourceMapRangeNode === node && lastTokenSourceMapRangeToken === token) { - return lastTokenSourceMapRange; - } - // Get the custom token source map range for a node or from one of its original nodes. - // Custom token ranges are not stored on the node to avoid the GC burden. - var range; - var current = node; - while (current) { - range = current.id ? tokenSourceMapRanges[current.id + "-" + token] : undefined; - if (range !== undefined) { - break; + function emitNodeWithNotification(emitContext, node, emitCallback) { + if (node) { + if (isEmitNotificationEnabled(node)) { + context.onEmitNode(emitContext, node, emitCallback); + } + else { + emitCallback(emitContext, node); } - current = current.original; } - // Cache the most recently requested value. - lastTokenSourceMapRangeNode = node; - lastTokenSourceMapRangeToken = token; - lastTokenSourceMapRange = range; - return range; - } - /** - * Sets the TextRange to use for source maps for a token of a node. - * - * @param node The node. - * @param token The token. - * @param range The text range. - */ - function setTokenSourceMapRange(node, token, range) { - // Cache the most recently requested value. - lastTokenSourceMapRangeNode = node; - lastTokenSourceMapRangeToken = token; - lastTokenSourceMapRange = range; - tokenSourceMapRanges[ts.getNodeId(node) + "-" + token] = range; - return node; - } - /** - * Gets a custom text range to use when emitting comments. - * - * If a node does not have its own custom source map text range, the custom source map - * text range of its original pointer is used. - * - * @param node The node. - */ - function getCommentRange(node) { - return node.commentRange || node; - } - /** - * Sets a custom text range to use when emitting comments. - */ - function setCommentRange(node, range) { - beforeSetAnnotation(node); - node.commentRange = range; - return node; } /** * Records a hoisted variable declaration for the provided name within a lexical environment. @@ -51891,95 +53236,12 @@ var ts; } } ts.transformFiles = transformFiles; - function chain(a, b, c, d, e) { - if (e) { - var args_3 = []; - for (var i = 0; i < arguments.length; i++) { - args_3[i] = arguments[i]; - } - return function (t) { return compose.apply(void 0, ts.map(args_3, function (f) { return f(t); })); }; - } - else if (d) { - return function (t) { return compose(a(t), b(t), c(t), d(t)); }; - } - else if (c) { - return function (t) { return compose(a(t), b(t), c(t)); }; - } - else if (b) { - return function (t) { return compose(a(t), b(t)); }; - } - else if (a) { - return function (t) { return compose(a(t)); }; - } - else { - return function (t) { return function (u) { return u; }; }; - } - } - function compose(a, b, c, d, e) { - if (e) { - var args_4 = []; - for (var i = 0; i < arguments.length; i++) { - args_4[i] = arguments[i]; - } - return function (t) { return ts.reduceLeft(args_4, function (u, f) { return f(u); }, t); }; - } - else if (d) { - return function (t) { return d(c(b(a(t)))); }; - } - else if (c) { - return function (t) { return c(b(a(t))); }; - } - else if (b) { - return function (t) { return b(a(t)); }; - } - else if (a) { - return function (t) { return a(t); }; - } - else { - return function (t) { return t; }; - } - } var _a; })(ts || (ts = {})); /// /* @internal */ var ts; (function (ts) { - function createSourceMapWriter(host, writer) { - var compilerOptions = host.getCompilerOptions(); - if (compilerOptions.sourceMap || compilerOptions.inlineSourceMap) { - if (compilerOptions.extendedDiagnostics) { - return createSourceMapWriterWithExtendedDiagnostics(host, writer); - } - return createSourceMapWriterWorker(host, writer); - } - else { - return getNullSourceMapWriter(); - } - } - ts.createSourceMapWriter = createSourceMapWriter; - var nullSourceMapWriter; - function getNullSourceMapWriter() { - if (nullSourceMapWriter === undefined) { - nullSourceMapWriter = { - initialize: function (filePath, sourceMapFilePath, sourceFiles, isBundledEmit) { }, - reset: function () { }, - getSourceMapData: function () { return undefined; }, - setSourceFile: function (sourceFile) { }, - emitPos: function (pos) { }, - emitStart: function (range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { }, - emitEnd: function (range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { }, - emitTokenStart: function (token, pos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { return -1; }, - emitTokenEnd: function (token, end, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { return -1; }, - changeEmitSourcePos: function () { }, - stopOverridingSpan: function () { }, - getText: function () { return undefined; }, - getSourceMappingURL: function () { return undefined; } - }; - } - return nullSourceMapWriter; - } - ts.getNullSourceMapWriter = getNullSourceMapWriter; // Used for initialize lastEncodedSourceMapSpan and reset lastEncodedSourceMapSpan when updateLastEncodedAndRecordedSpans var defaultLastEncodedSourceMapSpan = { emittedLine: 1, @@ -51988,14 +53250,12 @@ var ts; sourceColumn: 1, sourceIndex: 0 }; - function createSourceMapWriterWorker(host, writer) { + function createSourceMapWriter(host, writer) { var compilerOptions = host.getCompilerOptions(); var extendedDiagnostics = compilerOptions.extendedDiagnostics; var currentSourceFile; var currentSourceText; var sourceMapDir; // The directory in which sourcemap will be - var stopOverridingSpan = false; - var modifyLastSourcePos = false; // Current source map file and its index in the sources list var sourceMapSourceIndex; // Last recorded and encoded spans @@ -52004,24 +53264,15 @@ var ts; var lastEncodedNameIndex; // Source map data var sourceMapData; - // This keeps track of the number of times `disable` has been called without a - // corresponding call to `enable`. As long as this value is non-zero, mappings will not - // be recorded. - // This is primarily used to provide a better experience when debugging binding - // patterns and destructuring assignments for simple expressions. - var disableDepth; + var disabled = !(compilerOptions.sourceMap || compilerOptions.inlineSourceMap); return { initialize: initialize, reset: reset, getSourceMapData: function () { return sourceMapData; }, setSourceFile: setSourceFile, emitPos: emitPos, - emitStart: emitStart, - emitEnd: emitEnd, - emitTokenStart: emitTokenStart, - emitTokenEnd: emitTokenEnd, - changeEmitSourcePos: changeEmitSourcePos, - stopOverridingSpan: function () { return stopOverridingSpan = true; }, + emitNodeWithSourceMap: emitNodeWithSourceMap, + emitTokenWithSourceMap: emitTokenWithSourceMap, getText: getText, getSourceMappingURL: getSourceMappingURL }; @@ -52034,12 +53285,14 @@ var ts; * @param isBundledEmit A value indicating whether the generated output file is a bundle. */ function initialize(filePath, sourceMapFilePath, sourceFiles, isBundledEmit) { + if (disabled) { + return; + } if (sourceMapData) { reset(); } currentSourceFile = undefined; currentSourceText = undefined; - disableDepth = 0; // Current source map file and its index in the sources list sourceMapSourceIndex = -1; // Last recorded and encoded spans @@ -52093,6 +53346,9 @@ var ts; * Reset the SourceMapWriter to an empty state. */ function reset() { + if (disabled) { + return; + } currentSourceFile = undefined; sourceMapDir = undefined; sourceMapSourceIndex = undefined; @@ -52100,56 +53356,6 @@ var ts; lastEncodedSourceMapSpan = undefined; lastEncodedNameIndex = undefined; sourceMapData = undefined; - disableDepth = 0; - } - /** - * Re-enables the recording of mappings. - */ - function enable() { - if (disableDepth > 0) { - disableDepth--; - } - } - /** - * Disables the recording of mappings. - */ - function disable() { - disableDepth++; - } - function updateLastEncodedAndRecordedSpans() { - if (modifyLastSourcePos) { - // Reset the source pos - modifyLastSourcePos = false; - // Change Last recorded Map with last encoded emit line and character - lastRecordedSourceMapSpan.emittedLine = lastEncodedSourceMapSpan.emittedLine; - lastRecordedSourceMapSpan.emittedColumn = lastEncodedSourceMapSpan.emittedColumn; - // Pop sourceMapDecodedMappings to remove last entry - sourceMapData.sourceMapDecodedMappings.pop(); - // Point the lastEncodedSourceMapSpace to the previous encoded sourceMapSpan - // If the list is empty which indicates that we are at the beginning of the file, - // we have to reset it to default value (same value when we first initialize sourceMapWriter) - lastEncodedSourceMapSpan = sourceMapData.sourceMapDecodedMappings.length ? - sourceMapData.sourceMapDecodedMappings[sourceMapData.sourceMapDecodedMappings.length - 1] : - defaultLastEncodedSourceMapSpan; - // TODO: Update lastEncodedNameIndex - // Since we dont support this any more, lets not worry about it right now. - // When we start supporting nameIndex, we will get back to this - // Change the encoded source map - var sourceMapMappings = sourceMapData.sourceMapMappings; - var lenthToSet = sourceMapMappings.length - 1; - for (; lenthToSet >= 0; lenthToSet--) { - var currentChar = sourceMapMappings.charAt(lenthToSet); - if (currentChar === ",") { - // Separator for the entry found - break; - } - if (currentChar === ";" && lenthToSet !== 0 && sourceMapMappings.charAt(lenthToSet - 1) !== ";") { - // Last line separator found - break; - } - } - sourceMapData.sourceMapMappings = sourceMapMappings.substr(0, Math.max(0, lenthToSet)); - } } // Encoding for sourcemap span function encodeLastRecordedSourceMapSpan() { @@ -52197,7 +53403,7 @@ var ts; * @param pos The position. */ function emitPos(pos) { - if (ts.positionIsSynthesized(pos) || disableDepth > 0) { + if (disabled || ts.positionIsSynthesized(pos)) { return; } if (extendedDiagnostics) { @@ -52226,84 +53432,78 @@ var ts; sourceColumn: sourceLinePos.character, sourceIndex: sourceMapSourceIndex }; - stopOverridingSpan = false; } - else if (!stopOverridingSpan) { + else { // Take the new pos instead since there is no change in emittedLine and column since last location lastRecordedSourceMapSpan.sourceLine = sourceLinePos.line; lastRecordedSourceMapSpan.sourceColumn = sourceLinePos.character; lastRecordedSourceMapSpan.sourceIndex = sourceMapSourceIndex; } - updateLastEncodedAndRecordedSpans(); if (extendedDiagnostics) { ts.performance.mark("afterSourcemap"); ts.performance.measure("Source Map", "beforeSourcemap", "afterSourcemap"); } } - function getStartPosPastDecorators(range) { - var rangeHasDecorators = !!range.decorators; - return ts.skipTrivia(currentSourceText, rangeHasDecorators ? range.decorators.end : range.pos); - } - function emitStart(range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { - if (contextNode) { - if (!ignoreNodeCallback(contextNode)) { - range = getTextRangeCallback(contextNode) || range; - emitPos(getStartPosPastDecorators(range)); - } - if (ignoreChildrenCallback(contextNode)) { - disable(); - } - } - else { - emitPos(getStartPosPastDecorators(range)); + /** + * Emits a node with possible leading and trailing source maps. + * + * @param node The node to emit. + * @param emitCallback The callback used to emit the node. + */ + function emitNodeWithSourceMap(emitContext, node, emitCallback) { + if (disabled) { + return emitCallback(emitContext, node); } - } - function emitEnd(range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { - if (contextNode) { - if (ignoreChildrenCallback(contextNode)) { - enable(); - } - if (!ignoreNodeCallback(contextNode)) { - range = getTextRangeCallback(contextNode) || range; - emitPos(range.end); + if (node) { + var emitNode = node.emitNode; + var emitFlags = emitNode && emitNode.flags; + var _a = emitNode && emitNode.sourceMapRange || node, pos = _a.pos, end = _a.end; + if (node.kind !== 287 /* NotEmittedStatement */ + && (emitFlags & 512 /* NoLeadingSourceMap */) === 0 + && pos >= 0) { + emitPos(ts.skipTrivia(currentSourceText, pos)); + } + if (emitFlags & 2048 /* NoNestedSourceMaps */) { + disabled = true; + emitCallback(emitContext, node); + disabled = false; } - } - else { - emitPos(range.end); - } - stopOverridingSpan = false; - } - function emitTokenStart(token, tokenStartPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { - if (contextNode) { - if (ignoreTokenCallback(contextNode, token)) { - return ts.skipTrivia(currentSourceText, tokenStartPos); + else { + emitCallback(emitContext, node); } - var range = getTokenTextRangeCallback(contextNode, token); - if (range) { - tokenStartPos = range.pos; + if (node.kind !== 287 /* NotEmittedStatement */ + && (emitFlags & 1024 /* NoTrailingSourceMap */) === 0 + && end >= 0) { + emitPos(end); } } - tokenStartPos = ts.skipTrivia(currentSourceText, tokenStartPos); - emitPos(tokenStartPos); - return tokenStartPos; } - function emitTokenEnd(token, tokenEndPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { - if (contextNode) { - if (ignoreTokenCallback(contextNode, token)) { - return tokenEndPos; - } - var range = getTokenTextRangeCallback(contextNode, token); - if (range) { - tokenEndPos = range.end; - } + /** + * Emits a token of a node with possible leading and trailing source maps. + * + * @param node The node containing the token. + * @param token The token to emit. + * @param tokenStartPos The start pos of the token. + * @param emitCallback The callback used to emit the token. + */ + function emitTokenWithSourceMap(node, token, tokenPos, emitCallback) { + if (disabled) { + return emitCallback(token, tokenPos); } - emitPos(tokenEndPos); - return tokenEndPos; - } - // @deprecated - function changeEmitSourcePos() { - ts.Debug.assert(!modifyLastSourcePos); - modifyLastSourcePos = true; + var emitNode = node && node.emitNode; + var emitFlags = emitNode && emitNode.flags; + var range = emitNode && emitNode.tokenSourceMapRanges && emitNode.tokenSourceMapRanges[token]; + tokenPos = ts.skipTrivia(currentSourceText, range ? range.pos : tokenPos); + if ((emitFlags & 4096 /* NoTokenLeadingSourceMaps */) === 0 && tokenPos >= 0) { + emitPos(tokenPos); + } + tokenPos = emitCallback(token, tokenPos); + if (range) + tokenPos = range.end; + if ((emitFlags & 8192 /* NoTokenTrailingSourceMaps */) === 0 && tokenPos >= 0) { + emitPos(tokenPos); + } + return tokenPos; } /** * Set the current source file. @@ -52311,6 +53511,9 @@ var ts; * @param sourceFile The source file. */ function setSourceFile(sourceFile) { + if (disabled) { + return; + } currentSourceFile = sourceFile; currentSourceText = currentSourceFile.text; // Add the file to tsFilePaths @@ -52334,6 +53537,9 @@ var ts; * Gets the text for the source map. */ function getText() { + if (disabled) { + return; + } encodeLastRecordedSourceMapSpan(); return ts.stringify({ version: 3, @@ -52349,6 +53555,9 @@ var ts; * Gets the SourceMappingURL for the source map. */ function getSourceMappingURL() { + if (disabled) { + return; + } if (compilerOptions.inlineSourceMap) { // Encode the sourceMap into the sourceMap url var base64SourceMapText = ts.convertToBase64(getText()); @@ -52359,46 +53568,7 @@ var ts; } } } - function createSourceMapWriterWithExtendedDiagnostics(host, writer) { - var _a = createSourceMapWriterWorker(host, writer), initialize = _a.initialize, reset = _a.reset, getSourceMapData = _a.getSourceMapData, setSourceFile = _a.setSourceFile, emitPos = _a.emitPos, emitStart = _a.emitStart, emitEnd = _a.emitEnd, emitTokenStart = _a.emitTokenStart, emitTokenEnd = _a.emitTokenEnd, changeEmitSourcePos = _a.changeEmitSourcePos, stopOverridingSpan = _a.stopOverridingSpan, getText = _a.getText, getSourceMappingURL = _a.getSourceMappingURL; - return { - initialize: initialize, - reset: reset, - getSourceMapData: getSourceMapData, - setSourceFile: setSourceFile, - emitPos: function (pos) { - ts.performance.mark("sourcemapStart"); - emitPos(pos); - ts.performance.measure("sourceMapTime", "sourcemapStart"); - }, - emitStart: function (range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { - ts.performance.mark("emitSourcemap:emitStart"); - emitStart(range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback); - ts.performance.measure("sourceMapTime", "emitSourcemap:emitStart"); - }, - emitEnd: function (range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { - ts.performance.mark("emitSourcemap:emitEnd"); - emitEnd(range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback); - ts.performance.measure("sourceMapTime", "emitSourcemap:emitEnd"); - }, - emitTokenStart: function (token, tokenStartPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { - ts.performance.mark("emitSourcemap:emitTokenStart"); - tokenStartPos = emitTokenStart(token, tokenStartPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback); - ts.performance.measure("sourceMapTime", "emitSourcemap:emitTokenStart"); - return tokenStartPos; - }, - emitTokenEnd: function (token, tokenEndPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { - ts.performance.mark("emitSourcemap:emitTokenEnd"); - tokenEndPos = emitTokenEnd(token, tokenEndPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback); - ts.performance.measure("sourceMapTime", "emitSourcemap:emitTokenEnd"); - return tokenEndPos; - }, - changeEmitSourcePos: changeEmitSourcePos, - stopOverridingSpan: stopOverridingSpan, - getText: getText, - getSourceMappingURL: getSourceMappingURL - }; - } + ts.createSourceMapWriter = createSourceMapWriter; var base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; function base64FormatEncode(inValue) { if (inValue < 64) { @@ -52457,21 +53627,23 @@ var ts; emitBodyWithDetachedComments: emitBodyWithDetachedComments, emitTrailingCommentsOfPosition: emitTrailingCommentsOfPosition }; - function emitNodeWithComments(node, emitCallback) { + function emitNodeWithComments(emitContext, node, emitCallback) { if (disabled) { - emitCallback(node); + emitCallback(emitContext, node); return; } if (node) { - var _a = node.commentRange || node, pos = _a.pos, end = _a.end; - var emitFlags = node.emitFlags; + var _a = ts.getCommentRange(node), pos = _a.pos, end = _a.end; + var emitFlags = ts.getEmitFlags(node); if ((pos < 0 && end < 0) || (pos === end)) { // Both pos and end are synthesized, so just emit the node without comments. if (emitFlags & 65536 /* NoNestedComments */) { - disableCommentsAndEmit(node, emitCallback); + disabled = true; + emitCallback(emitContext, node); + disabled = false; } else { - emitCallback(node); + emitCallback(emitContext, node); } } else { @@ -52505,10 +53677,12 @@ var ts; ts.performance.measure("commentTime", "preEmitNodeWithComment"); } if (emitFlags & 65536 /* NoNestedComments */) { - disableCommentsAndEmit(node, emitCallback); + disabled = true; + emitCallback(emitContext, node); + disabled = false; } else { - emitCallback(node); + emitCallback(emitContext, node); } if (extendedDiagnostics) { ts.performance.mark("beginEmitNodeWithComment"); @@ -52533,7 +53707,7 @@ var ts; ts.performance.mark("preEmitBodyWithDetachedComments"); } var pos = detachedRange.pos, end = detachedRange.end; - var emitFlags = node.emitFlags; + var emitFlags = ts.getEmitFlags(node); var skipLeadingComments = pos < 0 || (emitFlags & 16384 /* NoLeadingComments */) !== 0; var skipTrailingComments = disabled || end < 0 || (emitFlags & 32768 /* NoTrailingComments */) !== 0; if (!skipLeadingComments) { @@ -52542,8 +53716,10 @@ var ts; if (extendedDiagnostics) { ts.performance.measure("commentTime", "preEmitBodyWithDetachedComments"); } - if (emitFlags & 65536 /* NoNestedComments */) { - disableCommentsAndEmit(node, emitCallback); + if (emitFlags & 65536 /* NoNestedComments */ && !disabled) { + disabled = true; + emitCallback(node); + disabled = false; } else { emitCallback(node); @@ -52664,16 +53840,6 @@ var ts; currentLineMap = ts.getLineStarts(currentSourceFile); detachedCommentsInfo = undefined; } - function disableCommentsAndEmit(node, emitCallback) { - if (disabled) { - emitCallback(node); - } - else { - disabled = true; - emitCallback(node); - disabled = false; - } - } function hasDetachedComments(pos) { return detachedCommentsInfo !== undefined && ts.lastOrUndefined(detachedCommentsInfo).nodePos === pos; } @@ -52735,11 +53901,11 @@ var ts; return declarationDiagnostics.getDiagnostics(targetSourceFile ? targetSourceFile.fileName : undefined); function getDeclarationDiagnosticsFromFile(_a, sources, isBundledEmit) { var declarationFilePath = _a.declarationFilePath; - emitDeclarations(host, resolver, declarationDiagnostics, declarationFilePath, sources, isBundledEmit); + emitDeclarations(host, resolver, declarationDiagnostics, declarationFilePath, sources, isBundledEmit, /*emitOnlyDtsFiles*/ false); } } ts.getDeclarationDiagnostics = getDeclarationDiagnostics; - function emitDeclarations(host, resolver, emitterDiagnostics, declarationFilePath, sourceFiles, isBundledEmit) { + function emitDeclarations(host, resolver, emitterDiagnostics, declarationFilePath, sourceFiles, isBundledEmit, emitOnlyDtsFiles) { var newLine = host.getNewLine(); var compilerOptions = host.getCompilerOptions(); var write; @@ -52786,7 +53952,7 @@ var ts; // global file reference is added only // - if it is not bundled emit (because otherwise it would be self reference) // - and it is not already added - if (writeReferencePath(referencedFile, !isBundledEmit && !addedGlobalFileReference)) { + if (writeReferencePath(referencedFile, !isBundledEmit && !addedGlobalFileReference, emitOnlyDtsFiles)) { addedGlobalFileReference = true; } emittedReferencedFiles.push(referencedFile); @@ -52987,7 +54153,7 @@ var ts; } else { errorNameNode = declaration.name; - resolver.writeTypeOfDeclaration(declaration, enclosingDeclaration, 2 /* UseTypeOfFunction */, writer); + resolver.writeTypeOfDeclaration(declaration, enclosingDeclaration, 2 /* UseTypeOfFunction */ | 1024 /* UseTypeAliasValue */, writer); errorNameNode = undefined; } } @@ -53000,7 +54166,7 @@ var ts; } else { errorNameNode = signature.name; - resolver.writeReturnTypeOfSignatureDeclaration(signature, enclosingDeclaration, 2 /* UseTypeOfFunction */, writer); + resolver.writeReturnTypeOfSignatureDeclaration(signature, enclosingDeclaration, 2 /* UseTypeOfFunction */ | 1024 /* UseTypeAliasValue */, writer); errorNameNode = undefined; } } @@ -53202,7 +54368,7 @@ var ts; write(tempVarName); write(": "); writer.getSymbolAccessibilityDiagnostic = getDefaultExportAccessibilityDiagnostic; - resolver.writeTypeOfExpression(node.expression, enclosingDeclaration, 2 /* UseTypeOfFunction */, writer); + resolver.writeTypeOfExpression(node.expression, enclosingDeclaration, 2 /* UseTypeOfFunction */ | 1024 /* UseTypeAliasValue */, writer); write(";"); writeLine(); write(node.isExportEquals ? "export = " : "export default "); @@ -53624,7 +54790,7 @@ var ts; } else { writer.getSymbolAccessibilityDiagnostic = getHeritageClauseVisibilityError; - resolver.writeBaseConstructorTypeOfClass(enclosingDeclaration, enclosingDeclaration, 2 /* UseTypeOfFunction */, writer); + resolver.writeBaseConstructorTypeOfClass(enclosingDeclaration, enclosingDeclaration, 2 /* UseTypeOfFunction */ | 1024 /* UseTypeAliasValue */, writer); } function getHeritageClauseVisibilityError(symbolAccessibilityResult) { var diagnosticMessage; @@ -53772,3801 +54938,3064 @@ var ts; } } } - function getVariableDeclarationTypeVisibilityError(symbolAccessibilityResult) { - var diagnosticMessage = getVariableDeclarationTypeVisibilityDiagnosticMessage(symbolAccessibilityResult); - return diagnosticMessage !== undefined ? { - diagnosticMessage: diagnosticMessage, - errorNode: node, - typeName: node.name - } : undefined; - } - function emitBindingPattern(bindingPattern) { - // Only select non-omitted expression from the bindingPattern's elements. - // We have to do this to avoid emitting trailing commas. - // For example: - // original: var [, c,,] = [ 2,3,4] - // emitted: declare var c: number; // instead of declare var c:number, ; - var elements = []; - for (var _i = 0, _a = bindingPattern.elements; _i < _a.length; _i++) { - var element = _a[_i]; - if (element.kind !== 193 /* OmittedExpression */) { - elements.push(element); - } - } - emitCommaList(elements, emitBindingElement); - } - function emitBindingElement(bindingElement) { - function getBindingElementTypeVisibilityError(symbolAccessibilityResult) { - var diagnosticMessage = getVariableDeclarationTypeVisibilityDiagnosticMessage(symbolAccessibilityResult); - return diagnosticMessage !== undefined ? { - diagnosticMessage: diagnosticMessage, - errorNode: bindingElement, - typeName: bindingElement.name - } : undefined; - } - if (bindingElement.name) { - if (ts.isBindingPattern(bindingElement.name)) { - emitBindingPattern(bindingElement.name); - } - else { - writeTextOfNode(currentText, bindingElement.name); - writeTypeOfDeclaration(bindingElement, /*type*/ undefined, getBindingElementTypeVisibilityError); - } - } - } - } - function emitTypeOfVariableDeclarationFromTypeLiteral(node) { - // if this is property of type literal, - // or is parameter of method/call/construct/index signature of type literal - // emit only if type is specified - if (node.type) { - write(": "); - emitType(node.type); - } - } - function isVariableStatementVisible(node) { - return ts.forEach(node.declarationList.declarations, function (varDeclaration) { return resolver.isDeclarationVisible(varDeclaration); }); - } - function writeVariableStatement(node) { - emitJsDocComments(node); - emitModuleElementDeclarationFlags(node); - if (ts.isLet(node.declarationList)) { - write("let "); - } - else if (ts.isConst(node.declarationList)) { - write("const "); - } - else { - write("var "); - } - emitCommaList(node.declarationList.declarations, emitVariableDeclaration, resolver.isDeclarationVisible); - write(";"); - writeLine(); - } - function emitAccessorDeclaration(node) { - if (ts.hasDynamicName(node)) { - return; - } - var accessors = ts.getAllAccessorDeclarations(node.parent.members, node); - var accessorWithTypeAnnotation; - if (node === accessors.firstAccessor) { - emitJsDocComments(accessors.getAccessor); - emitJsDocComments(accessors.setAccessor); - emitClassMemberDeclarationFlags(ts.getModifierFlags(node) | (accessors.setAccessor ? 0 : 64 /* Readonly */)); - writeTextOfNode(currentText, node.name); - if (!ts.hasModifier(node, 8 /* Private */)) { - accessorWithTypeAnnotation = node; - var type = getTypeAnnotationFromAccessor(node); - if (!type) { - // couldn't get type for the first accessor, try the another one - var anotherAccessor = node.kind === 149 /* GetAccessor */ ? accessors.setAccessor : accessors.getAccessor; - type = getTypeAnnotationFromAccessor(anotherAccessor); - if (type) { - accessorWithTypeAnnotation = anotherAccessor; - } - } - writeTypeOfDeclaration(node, type, getAccessorDeclarationTypeVisibilityError); - } - write(";"); - writeLine(); - } - function getTypeAnnotationFromAccessor(accessor) { - if (accessor) { - return accessor.kind === 149 /* GetAccessor */ - ? accessor.type // Getter - return type - : accessor.parameters.length > 0 - ? accessor.parameters[0].type // Setter parameter type - : undefined; - } - } - function getAccessorDeclarationTypeVisibilityError(symbolAccessibilityResult) { - var diagnosticMessage; - if (accessorWithTypeAnnotation.kind === 150 /* SetAccessor */) { - // Setters have to have type named and cannot infer it so, the type should always be named - if (ts.hasModifier(accessorWithTypeAnnotation.parent, 32 /* Static */)) { - diagnosticMessage = symbolAccessibilityResult.errorModuleName ? - ts.Diagnostics.Parameter_0_of_public_static_property_setter_from_exported_class_has_or_is_using_name_1_from_private_module_2 : - ts.Diagnostics.Parameter_0_of_public_static_property_setter_from_exported_class_has_or_is_using_private_name_1; - } - else { - diagnosticMessage = symbolAccessibilityResult.errorModuleName ? - ts.Diagnostics.Parameter_0_of_public_property_setter_from_exported_class_has_or_is_using_name_1_from_private_module_2 : - ts.Diagnostics.Parameter_0_of_public_property_setter_from_exported_class_has_or_is_using_private_name_1; - } - return { - diagnosticMessage: diagnosticMessage, - errorNode: accessorWithTypeAnnotation.parameters[0], - // TODO(jfreeman): Investigate why we are passing node.name instead of node.parameters[0].name - typeName: accessorWithTypeAnnotation.name - }; - } - else { - if (ts.hasModifier(accessorWithTypeAnnotation, 32 /* Static */)) { - diagnosticMessage = symbolAccessibilityResult.errorModuleName ? - symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? - ts.Diagnostics.Return_type_of_public_static_property_getter_from_exported_class_has_or_is_using_name_0_from_external_module_1_but_cannot_be_named : - ts.Diagnostics.Return_type_of_public_static_property_getter_from_exported_class_has_or_is_using_name_0_from_private_module_1 : - ts.Diagnostics.Return_type_of_public_static_property_getter_from_exported_class_has_or_is_using_private_name_0; - } - else { - diagnosticMessage = symbolAccessibilityResult.errorModuleName ? - symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? - ts.Diagnostics.Return_type_of_public_property_getter_from_exported_class_has_or_is_using_name_0_from_external_module_1_but_cannot_be_named : - ts.Diagnostics.Return_type_of_public_property_getter_from_exported_class_has_or_is_using_name_0_from_private_module_1 : - ts.Diagnostics.Return_type_of_public_property_getter_from_exported_class_has_or_is_using_private_name_0; - } - return { - diagnosticMessage: diagnosticMessage, - errorNode: accessorWithTypeAnnotation.name, - typeName: undefined - }; - } - } - } - function writeFunctionDeclaration(node) { - if (ts.hasDynamicName(node)) { - return; - } - // If we are emitting Method/Constructor it isn't moduleElement and hence already determined to be emitting - // so no need to verify if the declaration is visible - if (!resolver.isImplementationOfOverload(node)) { - emitJsDocComments(node); - if (node.kind === 220 /* FunctionDeclaration */) { - emitModuleElementDeclarationFlags(node); - } - else if (node.kind === 147 /* MethodDeclaration */ || node.kind === 148 /* Constructor */) { - emitClassMemberDeclarationFlags(ts.getModifierFlags(node)); - } - if (node.kind === 220 /* FunctionDeclaration */) { - write("function "); - writeTextOfNode(currentText, node.name); - } - else if (node.kind === 148 /* Constructor */) { - write("constructor"); - } - else { - writeTextOfNode(currentText, node.name); - if (ts.hasQuestionToken(node)) { - write("?"); - } - } - emitSignatureDeclaration(node); - } - } - function emitSignatureDeclarationWithJsDocComments(node) { - emitJsDocComments(node); - emitSignatureDeclaration(node); - } - function emitSignatureDeclaration(node) { - var prevEnclosingDeclaration = enclosingDeclaration; - enclosingDeclaration = node; - var closeParenthesizedFunctionType = false; - if (node.kind === 153 /* IndexSignature */) { - // Index signature can have readonly modifier - emitClassMemberDeclarationFlags(ts.getModifierFlags(node)); - write("["); - } - else { - // Construct signature or constructor type write new Signature - if (node.kind === 152 /* ConstructSignature */ || node.kind === 157 /* ConstructorType */) { - write("new "); - } - else if (node.kind === 156 /* FunctionType */) { - var currentOutput = writer.getText(); - // Do not generate incorrect type when function type with type parameters is type argument - // This could happen if user used space between two '<' making it error free - // e.g var x: A< (a: Tany)=>Tany>; - if (node.typeParameters && currentOutput.charAt(currentOutput.length - 1) === "<") { - closeParenthesizedFunctionType = true; - write("("); - } - } - emitTypeParameters(node.typeParameters); - write("("); - } - // Parameters - emitCommaList(node.parameters, emitParameterDeclaration); - if (node.kind === 153 /* IndexSignature */) { - write("]"); - } - else { - write(")"); - } - // If this is not a constructor and is not private, emit the return type - var isFunctionTypeOrConstructorType = node.kind === 156 /* FunctionType */ || node.kind === 157 /* ConstructorType */; - if (isFunctionTypeOrConstructorType || node.parent.kind === 159 /* TypeLiteral */) { - // Emit type literal signature return type only if specified - if (node.type) { - write(isFunctionTypeOrConstructorType ? " => " : ": "); - emitType(node.type); - } - } - else if (node.kind !== 148 /* Constructor */ && !ts.hasModifier(node, 8 /* Private */)) { - writeReturnTypeAtSignature(node, getReturnTypeVisibilityError); - } - enclosingDeclaration = prevEnclosingDeclaration; - if (!isFunctionTypeOrConstructorType) { - write(";"); - writeLine(); - } - else if (closeParenthesizedFunctionType) { - write(")"); - } - function getReturnTypeVisibilityError(symbolAccessibilityResult) { - var diagnosticMessage; - switch (node.kind) { - case 152 /* ConstructSignature */: - // Interfaces cannot have return types that cannot be named - diagnosticMessage = symbolAccessibilityResult.errorModuleName ? - ts.Diagnostics.Return_type_of_constructor_signature_from_exported_interface_has_or_is_using_name_0_from_private_module_1 : - ts.Diagnostics.Return_type_of_constructor_signature_from_exported_interface_has_or_is_using_private_name_0; - break; - case 151 /* CallSignature */: - // Interfaces cannot have return types that cannot be named - diagnosticMessage = symbolAccessibilityResult.errorModuleName ? - ts.Diagnostics.Return_type_of_call_signature_from_exported_interface_has_or_is_using_name_0_from_private_module_1 : - ts.Diagnostics.Return_type_of_call_signature_from_exported_interface_has_or_is_using_private_name_0; - break; - case 153 /* IndexSignature */: - // Interfaces cannot have return types that cannot be named - diagnosticMessage = symbolAccessibilityResult.errorModuleName ? - ts.Diagnostics.Return_type_of_index_signature_from_exported_interface_has_or_is_using_name_0_from_private_module_1 : - ts.Diagnostics.Return_type_of_index_signature_from_exported_interface_has_or_is_using_private_name_0; - break; - case 147 /* MethodDeclaration */: - case 146 /* MethodSignature */: - if (ts.hasModifier(node, 32 /* Static */)) { - diagnosticMessage = symbolAccessibilityResult.errorModuleName ? - symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? - ts.Diagnostics.Return_type_of_public_static_method_from_exported_class_has_or_is_using_name_0_from_external_module_1_but_cannot_be_named : - ts.Diagnostics.Return_type_of_public_static_method_from_exported_class_has_or_is_using_name_0_from_private_module_1 : - ts.Diagnostics.Return_type_of_public_static_method_from_exported_class_has_or_is_using_private_name_0; - } - else if (node.parent.kind === 221 /* ClassDeclaration */) { - diagnosticMessage = symbolAccessibilityResult.errorModuleName ? - symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? - ts.Diagnostics.Return_type_of_public_method_from_exported_class_has_or_is_using_name_0_from_external_module_1_but_cannot_be_named : - ts.Diagnostics.Return_type_of_public_method_from_exported_class_has_or_is_using_name_0_from_private_module_1 : - ts.Diagnostics.Return_type_of_public_method_from_exported_class_has_or_is_using_private_name_0; - } - else { - // Interfaces cannot have return types that cannot be named - diagnosticMessage = symbolAccessibilityResult.errorModuleName ? - ts.Diagnostics.Return_type_of_method_from_exported_interface_has_or_is_using_name_0_from_private_module_1 : - ts.Diagnostics.Return_type_of_method_from_exported_interface_has_or_is_using_private_name_0; - } - break; - case 220 /* FunctionDeclaration */: - diagnosticMessage = symbolAccessibilityResult.errorModuleName ? - symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? - ts.Diagnostics.Return_type_of_exported_function_has_or_is_using_name_0_from_external_module_1_but_cannot_be_named : - ts.Diagnostics.Return_type_of_exported_function_has_or_is_using_name_0_from_private_module_1 : - ts.Diagnostics.Return_type_of_exported_function_has_or_is_using_private_name_0; - break; - default: - ts.Debug.fail("This is unknown kind for signature: " + node.kind); - } - return { - diagnosticMessage: diagnosticMessage, - errorNode: node.name || node - }; - } - } - function emitParameterDeclaration(node) { - increaseIndent(); - emitJsDocComments(node); - if (node.dotDotDotToken) { - write("..."); - } - if (ts.isBindingPattern(node.name)) { - // For bindingPattern, we can't simply writeTextOfNode from the source file - // because we want to omit the initializer and using writeTextOfNode will result in initializer get emitted. - // Therefore, we will have to recursively emit each element in the bindingPattern. - emitBindingPattern(node.name); - } - else { - writeTextOfNode(currentText, node.name); - } - if (resolver.isOptionalParameter(node)) { - write("?"); - } - decreaseIndent(); - if (node.parent.kind === 156 /* FunctionType */ || - node.parent.kind === 157 /* ConstructorType */ || - node.parent.parent.kind === 159 /* TypeLiteral */) { - emitTypeOfVariableDeclarationFromTypeLiteral(node); - } - else if (!ts.hasModifier(node.parent, 8 /* Private */)) { - writeTypeOfDeclaration(node, node.type, getParameterDeclarationTypeVisibilityError); - } - function getParameterDeclarationTypeVisibilityError(symbolAccessibilityResult) { - var diagnosticMessage = getParameterDeclarationTypeVisibilityDiagnosticMessage(symbolAccessibilityResult); - return diagnosticMessage !== undefined ? { - diagnosticMessage: diagnosticMessage, - errorNode: node, - typeName: node.name - } : undefined; - } - function getParameterDeclarationTypeVisibilityDiagnosticMessage(symbolAccessibilityResult) { - switch (node.parent.kind) { - case 148 /* Constructor */: - return symbolAccessibilityResult.errorModuleName ? - symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? - ts.Diagnostics.Parameter_0_of_constructor_from_exported_class_has_or_is_using_name_1_from_external_module_2_but_cannot_be_named : - ts.Diagnostics.Parameter_0_of_constructor_from_exported_class_has_or_is_using_name_1_from_private_module_2 : - ts.Diagnostics.Parameter_0_of_constructor_from_exported_class_has_or_is_using_private_name_1; - case 152 /* ConstructSignature */: - // Interfaces cannot have parameter types that cannot be named - return symbolAccessibilityResult.errorModuleName ? - ts.Diagnostics.Parameter_0_of_constructor_signature_from_exported_interface_has_or_is_using_name_1_from_private_module_2 : - ts.Diagnostics.Parameter_0_of_constructor_signature_from_exported_interface_has_or_is_using_private_name_1; - case 151 /* CallSignature */: - // Interfaces cannot have parameter types that cannot be named - return symbolAccessibilityResult.errorModuleName ? - ts.Diagnostics.Parameter_0_of_call_signature_from_exported_interface_has_or_is_using_name_1_from_private_module_2 : - ts.Diagnostics.Parameter_0_of_call_signature_from_exported_interface_has_or_is_using_private_name_1; - case 147 /* MethodDeclaration */: - case 146 /* MethodSignature */: - if (ts.hasModifier(node.parent, 32 /* Static */)) { - return symbolAccessibilityResult.errorModuleName ? - symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? - ts.Diagnostics.Parameter_0_of_public_static_method_from_exported_class_has_or_is_using_name_1_from_external_module_2_but_cannot_be_named : - ts.Diagnostics.Parameter_0_of_public_static_method_from_exported_class_has_or_is_using_name_1_from_private_module_2 : - ts.Diagnostics.Parameter_0_of_public_static_method_from_exported_class_has_or_is_using_private_name_1; - } - else if (node.parent.parent.kind === 221 /* ClassDeclaration */) { - return symbolAccessibilityResult.errorModuleName ? - symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? - ts.Diagnostics.Parameter_0_of_public_method_from_exported_class_has_or_is_using_name_1_from_external_module_2_but_cannot_be_named : - ts.Diagnostics.Parameter_0_of_public_method_from_exported_class_has_or_is_using_name_1_from_private_module_2 : - ts.Diagnostics.Parameter_0_of_public_method_from_exported_class_has_or_is_using_private_name_1; - } - else { - // Interfaces cannot have parameter types that cannot be named - return symbolAccessibilityResult.errorModuleName ? - ts.Diagnostics.Parameter_0_of_method_from_exported_interface_has_or_is_using_name_1_from_private_module_2 : - ts.Diagnostics.Parameter_0_of_method_from_exported_interface_has_or_is_using_private_name_1; - } - case 220 /* FunctionDeclaration */: - return symbolAccessibilityResult.errorModuleName ? - symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? - ts.Diagnostics.Parameter_0_of_exported_function_has_or_is_using_name_1_from_external_module_2_but_cannot_be_named : - ts.Diagnostics.Parameter_0_of_exported_function_has_or_is_using_name_1_from_private_module_2 : - ts.Diagnostics.Parameter_0_of_exported_function_has_or_is_using_private_name_1; - default: - ts.Debug.fail("This is unknown parent for parameter: " + node.parent.kind); - } + function getVariableDeclarationTypeVisibilityError(symbolAccessibilityResult) { + var diagnosticMessage = getVariableDeclarationTypeVisibilityDiagnosticMessage(symbolAccessibilityResult); + return diagnosticMessage !== undefined ? { + diagnosticMessage: diagnosticMessage, + errorNode: node, + typeName: node.name + } : undefined; } function emitBindingPattern(bindingPattern) { - // We have to explicitly emit square bracket and bracket because these tokens are not store inside the node. - if (bindingPattern.kind === 167 /* ObjectBindingPattern */) { - write("{"); - emitCommaList(bindingPattern.elements, emitBindingElement); - write("}"); - } - else if (bindingPattern.kind === 168 /* ArrayBindingPattern */) { - write("["); - var elements = bindingPattern.elements; - emitCommaList(elements, emitBindingElement); - if (elements && elements.hasTrailingComma) { - write(", "); + // Only select non-omitted expression from the bindingPattern's elements. + // We have to do this to avoid emitting trailing commas. + // For example: + // original: var [, c,,] = [ 2,3,4] + // emitted: declare var c: number; // instead of declare var c:number, ; + var elements = []; + for (var _i = 0, _a = bindingPattern.elements; _i < _a.length; _i++) { + var element = _a[_i]; + if (element.kind !== 193 /* OmittedExpression */) { + elements.push(element); } - write("]"); } + emitCommaList(elements, emitBindingElement); } function emitBindingElement(bindingElement) { - if (bindingElement.kind === 193 /* OmittedExpression */) { - // If bindingElement is an omittedExpression (i.e. containing elision), - // we will emit blank space (although this may differ from users' original code, - // it allows emitSeparatedList to write separator appropriately) - // Example: - // original: function foo([, x, ,]) {} - // emit : function foo([ , x, , ]) {} - write(" "); + function getBindingElementTypeVisibilityError(symbolAccessibilityResult) { + var diagnosticMessage = getVariableDeclarationTypeVisibilityDiagnosticMessage(symbolAccessibilityResult); + return diagnosticMessage !== undefined ? { + diagnosticMessage: diagnosticMessage, + errorNode: bindingElement, + typeName: bindingElement.name + } : undefined; } - else if (bindingElement.kind === 169 /* BindingElement */) { - if (bindingElement.propertyName) { - // bindingElement has propertyName property in the following case: - // { y: [a,b,c] ...} -> bindingPattern will have a property called propertyName for "y" - // We have to explicitly emit the propertyName before descending into its binding elements. - // Example: - // original: function foo({y: [a,b,c]}) {} - // emit : declare function foo({y: [a, b, c]}: { y: [any, any, any] }) void; - writeTextOfNode(currentText, bindingElement.propertyName); - write(": "); + if (bindingElement.name) { + if (ts.isBindingPattern(bindingElement.name)) { + emitBindingPattern(bindingElement.name); } - if (bindingElement.name) { - if (ts.isBindingPattern(bindingElement.name)) { - // If it is a nested binding pattern, we will recursively descend into each element and emit each one separately. - // In the case of rest element, we will omit rest element. - // Example: - // original: function foo([a, [[b]], c] = [1,[["string"]], 3]) {} - // emit : declare function foo([a, [[b]], c]: [number, [[string]], number]): void; - // original with rest: function foo([a, ...c]) {} - // emit : declare function foo([a, ...c]): void; - emitBindingPattern(bindingElement.name); - } - else { - ts.Debug.assert(bindingElement.name.kind === 69 /* Identifier */); - // If the node is just an identifier, we will simply emit the text associated with the node's name - // Example: - // original: function foo({y = 10, x}) {} - // emit : declare function foo({y, x}: {number, any}): void; - if (bindingElement.dotDotDotToken) { - write("..."); - } - writeTextOfNode(currentText, bindingElement.name); - } + else { + writeTextOfNode(currentText, bindingElement.name); + writeTypeOfDeclaration(bindingElement, /*type*/ undefined, getBindingElementTypeVisibilityError); } } } } - function emitNode(node) { - switch (node.kind) { - case 220 /* FunctionDeclaration */: - case 225 /* ModuleDeclaration */: - case 229 /* ImportEqualsDeclaration */: - case 222 /* InterfaceDeclaration */: - case 221 /* ClassDeclaration */: - case 223 /* TypeAliasDeclaration */: - case 224 /* EnumDeclaration */: - return emitModuleElement(node, isModuleElementVisible(node)); - case 200 /* VariableStatement */: - return emitModuleElement(node, isVariableStatementVisible(node)); - case 230 /* ImportDeclaration */: - // Import declaration without import clause is visible, otherwise it is not visible - return emitModuleElement(node, /*isModuleElementVisible*/ !node.importClause); - case 236 /* ExportDeclaration */: - return emitExportDeclaration(node); - case 148 /* Constructor */: - case 147 /* MethodDeclaration */: - case 146 /* MethodSignature */: - return writeFunctionDeclaration(node); - case 152 /* ConstructSignature */: - case 151 /* CallSignature */: - case 153 /* IndexSignature */: - return emitSignatureDeclarationWithJsDocComments(node); - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - return emitAccessorDeclaration(node); - case 145 /* PropertyDeclaration */: - case 144 /* PropertySignature */: - return emitPropertyDeclaration(node); - case 255 /* EnumMember */: - return emitEnumMemberDeclaration(node); - case 235 /* ExportAssignment */: - return emitExportAssignment(node); - case 256 /* SourceFile */: - return emitSourceFile(node); + function emitTypeOfVariableDeclarationFromTypeLiteral(node) { + // if this is property of type literal, + // or is parameter of method/call/construct/index signature of type literal + // emit only if type is specified + if (node.type) { + write(": "); + emitType(node.type); } } - /** - * Adds the reference to referenced file, returns true if global file reference was emitted - * @param referencedFile - * @param addBundledFileReference Determines if global file reference corresponding to bundled file should be emitted or not - */ - function writeReferencePath(referencedFile, addBundledFileReference) { - var declFileName; - var addedBundledEmitReference = false; - if (ts.isDeclarationFile(referencedFile)) { - // Declaration file, use declaration file name - declFileName = referencedFile.fileName; + function isVariableStatementVisible(node) { + return ts.forEach(node.declarationList.declarations, function (varDeclaration) { return resolver.isDeclarationVisible(varDeclaration); }); + } + function writeVariableStatement(node) { + emitJsDocComments(node); + emitModuleElementDeclarationFlags(node); + if (ts.isLet(node.declarationList)) { + write("let "); + } + else if (ts.isConst(node.declarationList)) { + write("const "); } else { - // Get the declaration file path - ts.forEachExpectedEmitFile(host, getDeclFileName, referencedFile); + write("var "); } - if (declFileName) { - declFileName = ts.getRelativePathToDirectoryOrUrl(ts.getDirectoryPath(ts.normalizeSlashes(declarationFilePath)), declFileName, host.getCurrentDirectory(), host.getCanonicalFileName, - /*isAbsolutePathAnUrl*/ false); - referencesOutput += "/// " + newLine; + emitCommaList(node.declarationList.declarations, emitVariableDeclaration, resolver.isDeclarationVisible); + write(";"); + writeLine(); + } + function emitAccessorDeclaration(node) { + if (ts.hasDynamicName(node)) { + return; } - return addedBundledEmitReference; - function getDeclFileName(emitFileNames, sourceFiles, isBundledEmit) { - // Dont add reference path to this file if it is a bundled emit and caller asked not emit bundled file path - if (isBundledEmit && !addBundledFileReference) { - return; + var accessors = ts.getAllAccessorDeclarations(node.parent.members, node); + var accessorWithTypeAnnotation; + if (node === accessors.firstAccessor) { + emitJsDocComments(accessors.getAccessor); + emitJsDocComments(accessors.setAccessor); + emitClassMemberDeclarationFlags(ts.getModifierFlags(node) | (accessors.setAccessor ? 0 : 64 /* Readonly */)); + writeTextOfNode(currentText, node.name); + if (!ts.hasModifier(node, 8 /* Private */)) { + accessorWithTypeAnnotation = node; + var type = getTypeAnnotationFromAccessor(node); + if (!type) { + // couldn't get type for the first accessor, try the another one + var anotherAccessor = node.kind === 149 /* GetAccessor */ ? accessors.setAccessor : accessors.getAccessor; + type = getTypeAnnotationFromAccessor(anotherAccessor); + if (type) { + accessorWithTypeAnnotation = anotherAccessor; + } + } + writeTypeOfDeclaration(node, type, getAccessorDeclarationTypeVisibilityError); } - ts.Debug.assert(!!emitFileNames.declarationFilePath || ts.isSourceFileJavaScript(referencedFile), "Declaration file is not present only for javascript files"); - declFileName = emitFileNames.declarationFilePath || emitFileNames.jsFilePath; - addedBundledEmitReference = isBundledEmit; + write(";"); + writeLine(); } - } - } - /* @internal */ - function writeDeclarationFile(declarationFilePath, sourceFiles, isBundledEmit, host, resolver, emitterDiagnostics) { - var emitDeclarationResult = emitDeclarations(host, resolver, emitterDiagnostics, declarationFilePath, sourceFiles, isBundledEmit); - var emitSkipped = emitDeclarationResult.reportedDeclarationError || host.isEmitBlocked(declarationFilePath) || host.getCompilerOptions().noEmit; - if (!emitSkipped) { - var declarationOutput = emitDeclarationResult.referencesOutput - + getDeclarationOutput(emitDeclarationResult.synchronousDeclarationOutput, emitDeclarationResult.moduleElementDeclarationEmitInfo); - ts.writeFile(host, emitterDiagnostics, declarationFilePath, declarationOutput, host.getCompilerOptions().emitBOM, sourceFiles); - } - return emitSkipped; - function getDeclarationOutput(synchronousDeclarationOutput, moduleElementDeclarationEmitInfo) { - var appliedSyncOutputPos = 0; - var declarationOutput = ""; - // apply asynchronous additions to the synchronous output - ts.forEach(moduleElementDeclarationEmitInfo, function (aliasEmitInfo) { - if (aliasEmitInfo.asynchronousOutput) { - declarationOutput += synchronousDeclarationOutput.substring(appliedSyncOutputPos, aliasEmitInfo.outputPos); - declarationOutput += getDeclarationOutput(aliasEmitInfo.asynchronousOutput, aliasEmitInfo.subModuleElementDeclarationEmitInfo); - appliedSyncOutputPos = aliasEmitInfo.outputPos; + function getTypeAnnotationFromAccessor(accessor) { + if (accessor) { + return accessor.kind === 149 /* GetAccessor */ + ? accessor.type // Getter - return type + : accessor.parameters.length > 0 + ? accessor.parameters[0].type // Setter parameter type + : undefined; } - }); - declarationOutput += synchronousDeclarationOutput.substring(appliedSyncOutputPos); - return declarationOutput; - } - } - ts.writeDeclarationFile = writeDeclarationFile; -})(ts || (ts = {})); -/// -/// -/// -/// -/// -/* @internal */ -var ts; -(function (ts) { - // Flags enum to track count of temp variables and a few dedicated names - var TempFlags; - (function (TempFlags) { - TempFlags[TempFlags["Auto"] = 0] = "Auto"; - TempFlags[TempFlags["CountMask"] = 268435455] = "CountMask"; - TempFlags[TempFlags["_i"] = 268435456] = "_i"; - })(TempFlags || (TempFlags = {})); - // targetSourceFile is when users only want one file in entire project to be emitted. This is used in compileOnSave feature - function emitFiles(resolver, host, targetSourceFile) { - var delimiters = createDelimiterMap(); - var brackets = createBracketsMap(); - // emit output for the __extends helper function - var extendsHelper = "\nvar __extends = (this && this.__extends) || function (d, b) {\n for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];\n function __() { this.constructor = d; }\n d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\n};"; - // Emit output for the __assign helper function. - // This is typically used for JSX spread attributes, - // and can be used for object literal spread properties. - var assignHelper = "\nvar __assign = (this && this.__assign) || Object.assign || function(t) {\n for (var s, i = 1, n = arguments.length; i < n; i++) {\n s = arguments[i];\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))\n t[p] = s[p];\n }\n return t;\n};"; - // emit output for the __decorate helper function - var decorateHelper = "\nvar __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {\n var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;\n if (typeof Reflect === \"object\" && typeof Reflect.decorate === \"function\") r = Reflect.decorate(decorators, target, key, desc);\n else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;\n return c > 3 && r && Object.defineProperty(target, key, r), r;\n};"; - // emit output for the __metadata helper function - var metadataHelper = "\nvar __metadata = (this && this.__metadata) || function (k, v) {\n if (typeof Reflect === \"object\" && typeof Reflect.metadata === \"function\") return Reflect.metadata(k, v);\n};"; - // emit output for the __param helper function - var paramHelper = "\nvar __param = (this && this.__param) || function (paramIndex, decorator) {\n return function (target, key) { decorator(target, key, paramIndex); }\n};"; - // emit output for the __awaiter helper function - var awaiterHelper = "\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments)).next());\n });\n};"; - // The __generator helper is used by down-level transformations to emulate the runtime - // semantics of an ES2015 generator function. When called, this helper returns an - // object that implements the Iterator protocol, in that it has `next`, `return`, and - // `throw` methods that step through the generator when invoked. - // - // parameters: - // thisArg The value to use as the `this` binding for the transformed generator body. - // body A function that acts as the transformed generator body. - // - // variables: - // _ Persistent state for the generator that is shared between the helper and the - // generator body. The state object has the following members: - // sent() - A method that returns or throws the current completion value. - // label - The next point at which to resume evaluation of the generator body. - // trys - A stack of protected regions (try/catch/finally blocks). - // ops - A stack of pending instructions when inside of a finally block. - // f A value indicating whether the generator is executing. - // y An iterator to delegate for a yield*. - // t A temporary variable that holds one of the following values (note that these - // cases do not overlap): - // - The completion value when resuming from a `yield` or `yield*`. - // - The error value for a catch block. - // - The current protected region (array of try/catch/finally/end labels). - // - The verb (`next`, `throw`, or `return` method) to delegate to the expression - // of a `yield*`. - // - The result of evaluating the verb delegated to the expression of a `yield*`. - // - // functions: - // verb(n) Creates a bound callback to the `step` function for opcode `n`. - // step(op) Evaluates opcodes in a generator body until execution is suspended or - // completed. - // - // The __generator helper understands a limited set of instructions: - // 0: next(value?) - Start or resume the generator with the specified value. - // 1: throw(error) - Resume the generator with an exception. If the generator is - // suspended inside of one or more protected regions, evaluates - // any intervening finally blocks between the current label and - // the nearest catch block or function boundary. If uncaught, the - // exception is thrown to the caller. - // 2: return(value?) - Resume the generator as if with a return. If the generator is - // suspended inside of one or more protected regions, evaluates any - // intervening finally blocks. - // 3: break(label) - Jump to the specified label. If the label is outside of the - // current protected region, evaluates any intervening finally - // blocks. - // 4: yield(value?) - Yield execution to the caller with an optional value. When - // resumed, the generator will continue at the next label. - // 5: yield*(value) - Delegates evaluation to the supplied iterator. When - // delegation completes, the generator will continue at the next - // label. - // 6: catch(error) - Handles an exception thrown from within the generator body. If - // the current label is inside of one or more protected regions, - // evaluates any intervening finally blocks between the current - // label and the nearest catch block or function boundary. If - // uncaught, the exception is thrown to the caller. - // 7: endfinally - Ends a finally block, resuming the last instruction prior to - // entering a finally block. - // - // For examples of how these are used, see the comments in ./transformers/generators.ts - var generatorHelper = "\nvar __generator = (this && this.__generator) || function (thisArg, body) {\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t;\n return { next: verb(0), \"throw\": verb(1), \"return\": verb(2) };\n function verb(n) { return function (v) { return step([n, v]); }; }\n function step(op) {\n if (f) throw new TypeError(\"Generator is already executing.\");\n while (_) try {\n if (f = 1, y && (t = y[op[0] & 2 ? \"return\" : op[0] ? \"throw\" : \"next\"]) && !(t = t.call(y, op[1])).done) return t;\n if (y = 0, t) op = [0, t.value];\n switch (op[0]) {\n case 0: case 1: t = op; break;\n case 4: _.label++; return { value: op[1], done: false };\n case 5: _.label++; y = op[1]; op = [0]; continue;\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\n default:\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\n if (t[2]) _.ops.pop();\n _.trys.pop(); continue;\n }\n op = body.call(thisArg, _);\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\n }\n};"; - // emit output for the __export helper function - var exportStarHelper = "\nfunction __export(m) {\n for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];\n}"; - // emit output for the UMD helper function. - var umdHelper = "\n(function (dependencies, factory) {\n if (typeof module === 'object' && typeof module.exports === 'object') {\n var v = factory(require, exports); if (v !== undefined) module.exports = v;\n }\n else if (typeof define === 'function' && define.amd) {\n define(dependencies, factory);\n }\n})"; - var superHelper = "\nconst _super = name => super[name];"; - var advancedSuperHelper = "\nconst _super = (function (geti, seti) {\n const cache = Object.create(null);\n return name => cache[name] || (cache[name] = { get value() { return geti(name); }, set value(v) { seti(name, v); } });\n})(name => super[name], (name, value) => super[name] = value);"; - var compilerOptions = host.getCompilerOptions(); - var languageVersion = ts.getEmitScriptTarget(compilerOptions); - var moduleKind = ts.getEmitModuleKind(compilerOptions); - var sourceMapDataList = compilerOptions.sourceMap || compilerOptions.inlineSourceMap ? [] : undefined; - var emittedFilesList = compilerOptions.listEmittedFiles ? [] : undefined; - var emitterDiagnostics = ts.createDiagnosticCollection(); - var newLine = host.getNewLine(); - var transformers = ts.getTransformers(compilerOptions); - var writer = ts.createTextWriter(newLine); - var write = writer.write, writeLine = writer.writeLine, increaseIndent = writer.increaseIndent, decreaseIndent = writer.decreaseIndent; - var sourceMap = ts.createSourceMapWriter(host, writer); - var emitStart = sourceMap.emitStart, emitEnd = sourceMap.emitEnd, emitTokenStart = sourceMap.emitTokenStart, emitTokenEnd = sourceMap.emitTokenEnd; - var comments = ts.createCommentWriter(host, writer, sourceMap); - var emitNodeWithComments = comments.emitNodeWithComments, emitBodyWithDetachedComments = comments.emitBodyWithDetachedComments, emitTrailingCommentsOfPosition = comments.emitTrailingCommentsOfPosition; - var nodeIdToGeneratedName; - var autoGeneratedIdToGeneratedName; - var generatedNameSet; - var tempFlags; - var currentSourceFile; - var currentText; - var currentFileIdentifiers; - var extendsEmitted; - var assignEmitted; - var decorateEmitted; - var paramEmitted; - var awaiterEmitted; - var isOwnFileEmit; - var emitSkipped = false; - ts.performance.mark("beforeTransform"); - // Transform the source files - var transformed = ts.transformFiles(resolver, host, ts.getSourceFilesToEmit(host, targetSourceFile), transformers); - ts.performance.measure("transformTime", "beforeTransform"); - // Extract helpers from the result - var getTokenSourceMapRange = transformed.getTokenSourceMapRange, isSubstitutionEnabled = transformed.isSubstitutionEnabled, isEmitNotificationEnabled = transformed.isEmitNotificationEnabled, onSubstituteNode = transformed.onSubstituteNode, onEmitNode = transformed.onEmitNode; - ts.performance.mark("beforePrint"); - // Emit each output file - ts.forEachTransformedEmitFile(host, transformed.getSourceFiles(), emitFile); - // Clean up after transformation - transformed.dispose(); - ts.performance.measure("printTime", "beforePrint"); - return { - emitSkipped: emitSkipped, - diagnostics: emitterDiagnostics.getDiagnostics(), - emittedFiles: emittedFilesList, - sourceMaps: sourceMapDataList - }; - function emitFile(jsFilePath, sourceMapFilePath, declarationFilePath, sourceFiles, isBundledEmit) { - // Make sure not to write js file and source map file if any of them cannot be written - if (!host.isEmitBlocked(jsFilePath) && !compilerOptions.noEmit) { - printFile(jsFilePath, sourceMapFilePath, sourceFiles, isBundledEmit); - } - else { - emitSkipped = true; - } - if (declarationFilePath) { - emitSkipped = ts.writeDeclarationFile(declarationFilePath, ts.getOriginalSourceFiles(sourceFiles), isBundledEmit, host, resolver, emitterDiagnostics) || emitSkipped; } - if (!emitSkipped && emittedFilesList) { - emittedFilesList.push(jsFilePath); - if (sourceMapFilePath) { - emittedFilesList.push(sourceMapFilePath); - } - if (declarationFilePath) { - emittedFilesList.push(declarationFilePath); + function getAccessorDeclarationTypeVisibilityError(symbolAccessibilityResult) { + var diagnosticMessage; + if (accessorWithTypeAnnotation.kind === 150 /* SetAccessor */) { + // Setters have to have type named and cannot infer it so, the type should always be named + if (ts.hasModifier(accessorWithTypeAnnotation.parent, 32 /* Static */)) { + diagnosticMessage = symbolAccessibilityResult.errorModuleName ? + ts.Diagnostics.Parameter_0_of_public_static_property_setter_from_exported_class_has_or_is_using_name_1_from_private_module_2 : + ts.Diagnostics.Parameter_0_of_public_static_property_setter_from_exported_class_has_or_is_using_private_name_1; + } + else { + diagnosticMessage = symbolAccessibilityResult.errorModuleName ? + ts.Diagnostics.Parameter_0_of_public_property_setter_from_exported_class_has_or_is_using_name_1_from_private_module_2 : + ts.Diagnostics.Parameter_0_of_public_property_setter_from_exported_class_has_or_is_using_private_name_1; + } + return { + diagnosticMessage: diagnosticMessage, + errorNode: accessorWithTypeAnnotation.parameters[0], + // TODO(jfreeman): Investigate why we are passing node.name instead of node.parameters[0].name + typeName: accessorWithTypeAnnotation.name + }; } - } - } - function printFile(jsFilePath, sourceMapFilePath, sourceFiles, isBundledEmit) { - sourceMap.initialize(jsFilePath, sourceMapFilePath, sourceFiles, isBundledEmit); - nodeIdToGeneratedName = []; - autoGeneratedIdToGeneratedName = []; - generatedNameSet = ts.createMap(); - isOwnFileEmit = !isBundledEmit; - // Emit helpers from all the files - if (isBundledEmit && moduleKind) { - for (var _a = 0, sourceFiles_4 = sourceFiles; _a < sourceFiles_4.length; _a++) { - var sourceFile = sourceFiles_4[_a]; - emitEmitHelpers(sourceFile); + else { + if (ts.hasModifier(accessorWithTypeAnnotation, 32 /* Static */)) { + diagnosticMessage = symbolAccessibilityResult.errorModuleName ? + symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? + ts.Diagnostics.Return_type_of_public_static_property_getter_from_exported_class_has_or_is_using_name_0_from_external_module_1_but_cannot_be_named : + ts.Diagnostics.Return_type_of_public_static_property_getter_from_exported_class_has_or_is_using_name_0_from_private_module_1 : + ts.Diagnostics.Return_type_of_public_static_property_getter_from_exported_class_has_or_is_using_private_name_0; + } + else { + diagnosticMessage = symbolAccessibilityResult.errorModuleName ? + symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? + ts.Diagnostics.Return_type_of_public_property_getter_from_exported_class_has_or_is_using_name_0_from_external_module_1_but_cannot_be_named : + ts.Diagnostics.Return_type_of_public_property_getter_from_exported_class_has_or_is_using_name_0_from_private_module_1 : + ts.Diagnostics.Return_type_of_public_property_getter_from_exported_class_has_or_is_using_private_name_0; + } + return { + diagnosticMessage: diagnosticMessage, + errorNode: accessorWithTypeAnnotation.name, + typeName: undefined + }; } } - // Print each transformed source file. - ts.forEach(sourceFiles, printSourceFile); - writeLine(); - var sourceMappingURL = sourceMap.getSourceMappingURL(); - if (sourceMappingURL) { - write("//# " + "sourceMappingURL" + "=" + sourceMappingURL); // Sometimes tools can sometimes see this line as a source mapping url comment - } - // Write the source map - if (compilerOptions.sourceMap && !compilerOptions.inlineSourceMap) { - ts.writeFile(host, emitterDiagnostics, sourceMapFilePath, sourceMap.getText(), /*writeByteOrderMark*/ false); - } - // Record source map data for the test harness. - if (sourceMapDataList) { - sourceMapDataList.push(sourceMap.getSourceMapData()); - } - // Write the output file - ts.writeFile(host, emitterDiagnostics, jsFilePath, writer.getText(), compilerOptions.emitBOM); - // Reset state - sourceMap.reset(); - comments.reset(); - writer.reset(); - tempFlags = 0 /* Auto */; - currentSourceFile = undefined; - currentText = undefined; - extendsEmitted = false; - assignEmitted = false; - decorateEmitted = false; - paramEmitted = false; - awaiterEmitted = false; - isOwnFileEmit = false; - } - function printSourceFile(node) { - currentSourceFile = node; - currentText = node.text; - currentFileIdentifiers = node.identifiers; - sourceMap.setSourceFile(node); - comments.setSourceFile(node); - emitNodeWithNotification(node, emitWorker); - } - /** - * Emits a node. - */ - function emit(node) { - emitNodeWithNotification(node, emitWithComments); - } - /** - * Emits a node with comments. - * - * NOTE: Do not call this method directly. It is part of the emit pipeline - * and should only be called indirectly from emit. - */ - function emitWithComments(node) { - emitNodeWithComments(node, emitWithSourceMap); } - /** - * Emits a node with source maps. - * - * NOTE: Do not call this method directly. It is part of the emit pipeline - * and should only be called indirectly from emitWithComments. - */ - function emitWithSourceMap(node) { - emitNodeWithSourceMap(node, emitWorker); - } - function emitIdentifierName(node) { - if (node) { - emitNodeWithNotification(node, emitIdentifierNameWithComments); + function writeFunctionDeclaration(node) { + if (ts.hasDynamicName(node)) { + return; } - } - function emitIdentifierNameWithComments(node) { - emitNodeWithComments(node, emitWorker); - } - /** - * Emits an expression node. - */ - function emitExpression(node) { - emitNodeWithNotification(node, emitExpressionWithComments); - } - /** - * Emits an expression with comments. - * - * NOTE: Do not call this method directly. It is part of the emitExpression pipeline - * and should only be called indirectly from emitExpression. - */ - function emitExpressionWithComments(node) { - emitNodeWithComments(node, emitExpressionWithSourceMap); - } - /** - * Emits an expression with source maps. - * - * NOTE: Do not call this method directly. It is part of the emitExpression pipeline - * and should only be called indirectly from emitExpressionWithComments. - */ - function emitExpressionWithSourceMap(node) { - emitNodeWithSourceMap(node, emitExpressionWorker); - } - /** - * Emits a node with emit notification if available. - */ - function emitNodeWithNotification(node, emitCallback) { - if (node) { - if (isEmitNotificationEnabled(node)) { - onEmitNode(node, emitCallback); + // If we are emitting Method/Constructor it isn't moduleElement and hence already determined to be emitting + // so no need to verify if the declaration is visible + if (!resolver.isImplementationOfOverload(node)) { + emitJsDocComments(node); + if (node.kind === 220 /* FunctionDeclaration */) { + emitModuleElementDeclarationFlags(node); + } + else if (node.kind === 147 /* MethodDeclaration */ || node.kind === 148 /* Constructor */) { + emitClassMemberDeclarationFlags(ts.getModifierFlags(node)); + } + if (node.kind === 220 /* FunctionDeclaration */) { + write("function "); + writeTextOfNode(currentText, node.name); + } + else if (node.kind === 148 /* Constructor */) { + write("constructor"); } else { - emitCallback(node); + writeTextOfNode(currentText, node.name); + if (ts.hasQuestionToken(node)) { + write("?"); + } } + emitSignatureDeclaration(node); } } - function emitNodeWithSourceMap(node, emitCallback) { - if (node) { - emitStart(/*range*/ node, /*contextNode*/ node, shouldSkipLeadingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange); - emitCallback(node); - emitEnd(/*range*/ node, /*contextNode*/ node, shouldSkipTrailingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange); - } - } - function getSourceMapRange(node) { - return node.sourceMapRange || node; - } - /** - * Determines whether to skip leading comment emit for a node. - * - * We do not emit comments for NotEmittedStatement nodes or any node that has - * NodeEmitFlags.NoLeadingComments. - * - * @param node A Node. - */ - function shouldSkipLeadingCommentsForNode(node) { - return ts.isNotEmittedStatement(node) - || (node.emitFlags & 16384 /* NoLeadingComments */) !== 0; - } - /** - * Determines whether to skip source map emit for the start position of a node. - * - * We do not emit source maps for NotEmittedStatement nodes or any node that - * has NodeEmitFlags.NoLeadingSourceMap. - * - * @param node A Node. - */ - function shouldSkipLeadingSourceMapForNode(node) { - return ts.isNotEmittedStatement(node) - || (node.emitFlags & 512 /* NoLeadingSourceMap */) !== 0; - } - /** - * Determines whether to skip source map emit for the end position of a node. - * - * We do not emit source maps for NotEmittedStatement nodes or any node that - * has NodeEmitFlags.NoTrailingSourceMap. - * - * @param node A Node. - */ - function shouldSkipTrailingSourceMapForNode(node) { - return ts.isNotEmittedStatement(node) - || (node.emitFlags & 1024 /* NoTrailingSourceMap */) !== 0; - } - /** - * Determines whether to skip source map emit for a node and its children. - * - * We do not emit source maps for a node that has NodeEmitFlags.NoNestedSourceMaps. - */ - function shouldSkipSourceMapForChildren(node) { - return (node.emitFlags & 2048 /* NoNestedSourceMaps */) !== 0; + function emitSignatureDeclarationWithJsDocComments(node) { + emitJsDocComments(node); + emitSignatureDeclaration(node); } - function emitWorker(node) { - if (tryEmitSubstitute(node, emitWorker, /*isExpression*/ false)) { - return; + function emitSignatureDeclaration(node) { + var prevEnclosingDeclaration = enclosingDeclaration; + enclosingDeclaration = node; + var closeParenthesizedFunctionType = false; + if (node.kind === 153 /* IndexSignature */) { + // Index signature can have readonly modifier + emitClassMemberDeclarationFlags(ts.getModifierFlags(node)); + write("["); } - var kind = node.kind; - switch (kind) { - // Pseudo-literals - case 12 /* TemplateHead */: - case 13 /* TemplateMiddle */: - case 14 /* TemplateTail */: - return emitLiteral(node); - // Identifiers - case 69 /* Identifier */: - return emitIdentifier(node); - // Reserved words - case 74 /* ConstKeyword */: - case 77 /* DefaultKeyword */: - case 82 /* ExportKeyword */: - case 103 /* VoidKeyword */: - // Strict mode reserved words - case 110 /* PrivateKeyword */: - case 111 /* ProtectedKeyword */: - case 112 /* PublicKeyword */: - case 113 /* StaticKeyword */: - // Contextual keywords - case 115 /* AbstractKeyword */: - case 117 /* AnyKeyword */: - case 118 /* AsyncKeyword */: - case 120 /* BooleanKeyword */: - case 122 /* DeclareKeyword */: - case 130 /* NumberKeyword */: - case 128 /* ReadonlyKeyword */: - case 132 /* StringKeyword */: - case 133 /* SymbolKeyword */: - case 137 /* GlobalKeyword */: - return writeTokenNode(node); - // Parse tree nodes - // Names - case 139 /* QualifiedName */: - return emitQualifiedName(node); - case 140 /* ComputedPropertyName */: - return emitComputedPropertyName(node); - // Signature elements - case 141 /* TypeParameter */: - return emitTypeParameter(node); - case 142 /* Parameter */: - return emitParameter(node); - case 143 /* Decorator */: - return emitDecorator(node); - // Type members - case 144 /* PropertySignature */: - return emitPropertySignature(node); - case 145 /* PropertyDeclaration */: - return emitPropertyDeclaration(node); - case 146 /* MethodSignature */: - return emitMethodSignature(node); - case 147 /* MethodDeclaration */: - return emitMethodDeclaration(node); - case 148 /* Constructor */: - return emitConstructor(node); - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - return emitAccessorDeclaration(node); - case 151 /* CallSignature */: - return emitCallSignature(node); - case 152 /* ConstructSignature */: - return emitConstructSignature(node); - case 153 /* IndexSignature */: - return emitIndexSignature(node); - // Types - case 154 /* TypePredicate */: - return emitTypePredicate(node); - case 155 /* TypeReference */: - return emitTypeReference(node); - case 156 /* FunctionType */: - return emitFunctionType(node); - case 157 /* ConstructorType */: - return emitConstructorType(node); - case 158 /* TypeQuery */: - return emitTypeQuery(node); - case 159 /* TypeLiteral */: - return emitTypeLiteral(node); - case 160 /* ArrayType */: - return emitArrayType(node); - case 161 /* TupleType */: - return emitTupleType(node); - case 162 /* UnionType */: - return emitUnionType(node); - case 163 /* IntersectionType */: - return emitIntersectionType(node); - case 164 /* ParenthesizedType */: - return emitParenthesizedType(node); - case 194 /* ExpressionWithTypeArguments */: - return emitExpressionWithTypeArguments(node); - case 165 /* ThisType */: - return emitThisType(node); - case 166 /* LiteralType */: - return emitLiteralType(node); - // Binding patterns - case 167 /* ObjectBindingPattern */: - return emitObjectBindingPattern(node); - case 168 /* ArrayBindingPattern */: - return emitArrayBindingPattern(node); - case 169 /* BindingElement */: - return emitBindingElement(node); - // Misc - case 197 /* TemplateSpan */: - return emitTemplateSpan(node); - case 198 /* SemicolonClassElement */: - return emitSemicolonClassElement(node); - // Statements - case 199 /* Block */: - return emitBlock(node); - case 200 /* VariableStatement */: - return emitVariableStatement(node); - case 201 /* EmptyStatement */: - return emitEmptyStatement(node); - case 202 /* ExpressionStatement */: - return emitExpressionStatement(node); - case 203 /* IfStatement */: - return emitIfStatement(node); - case 204 /* DoStatement */: - return emitDoStatement(node); - case 205 /* WhileStatement */: - return emitWhileStatement(node); - case 206 /* ForStatement */: - return emitForStatement(node); - case 207 /* ForInStatement */: - return emitForInStatement(node); - case 208 /* ForOfStatement */: - return emitForOfStatement(node); - case 209 /* ContinueStatement */: - return emitContinueStatement(node); - case 210 /* BreakStatement */: - return emitBreakStatement(node); - case 211 /* ReturnStatement */: - return emitReturnStatement(node); - case 212 /* WithStatement */: - return emitWithStatement(node); - case 213 /* SwitchStatement */: - return emitSwitchStatement(node); - case 214 /* LabeledStatement */: - return emitLabeledStatement(node); - case 215 /* ThrowStatement */: - return emitThrowStatement(node); - case 216 /* TryStatement */: - return emitTryStatement(node); - case 217 /* DebuggerStatement */: - return emitDebuggerStatement(node); - // Declarations - case 218 /* VariableDeclaration */: - return emitVariableDeclaration(node); - case 219 /* VariableDeclarationList */: - return emitVariableDeclarationList(node); - case 220 /* FunctionDeclaration */: - return emitFunctionDeclaration(node); - case 221 /* ClassDeclaration */: - return emitClassDeclaration(node); - case 222 /* InterfaceDeclaration */: - return emitInterfaceDeclaration(node); - case 223 /* TypeAliasDeclaration */: - return emitTypeAliasDeclaration(node); - case 224 /* EnumDeclaration */: - return emitEnumDeclaration(node); - case 225 /* ModuleDeclaration */: - return emitModuleDeclaration(node); - case 226 /* ModuleBlock */: - return emitModuleBlock(node); - case 227 /* CaseBlock */: - return emitCaseBlock(node); - case 229 /* ImportEqualsDeclaration */: - return emitImportEqualsDeclaration(node); - case 230 /* ImportDeclaration */: - return emitImportDeclaration(node); - case 231 /* ImportClause */: - return emitImportClause(node); - case 232 /* NamespaceImport */: - return emitNamespaceImport(node); - case 233 /* NamedImports */: - return emitNamedImports(node); - case 234 /* ImportSpecifier */: - return emitImportSpecifier(node); - case 235 /* ExportAssignment */: - return emitExportAssignment(node); - case 236 /* ExportDeclaration */: - return emitExportDeclaration(node); - case 237 /* NamedExports */: - return emitNamedExports(node); - case 238 /* ExportSpecifier */: - return emitExportSpecifier(node); - case 239 /* MissingDeclaration */: - return; - // Module references - case 240 /* ExternalModuleReference */: - return emitExternalModuleReference(node); - // JSX (non-expression) - case 244 /* JsxText */: - return emitJsxText(node); - case 243 /* JsxOpeningElement */: - return emitJsxOpeningElement(node); - case 245 /* JsxClosingElement */: - return emitJsxClosingElement(node); - case 246 /* JsxAttribute */: - return emitJsxAttribute(node); - case 247 /* JsxSpreadAttribute */: - return emitJsxSpreadAttribute(node); - case 248 /* JsxExpression */: - return emitJsxExpression(node); - // Clauses - case 249 /* CaseClause */: - return emitCaseClause(node); - case 250 /* DefaultClause */: - return emitDefaultClause(node); - case 251 /* HeritageClause */: - return emitHeritageClause(node); - case 252 /* CatchClause */: - return emitCatchClause(node); - // Property assignments - case 253 /* PropertyAssignment */: - return emitPropertyAssignment(node); - case 254 /* ShorthandPropertyAssignment */: - return emitShorthandPropertyAssignment(node); - // Enum - case 255 /* EnumMember */: - return emitEnumMember(node); - // Top-level nodes - case 256 /* SourceFile */: - return emitSourceFile(node); + else { + // Construct signature or constructor type write new Signature + if (node.kind === 152 /* ConstructSignature */ || node.kind === 157 /* ConstructorType */) { + write("new "); + } + else if (node.kind === 156 /* FunctionType */) { + var currentOutput = writer.getText(); + // Do not generate incorrect type when function type with type parameters is type argument + // This could happen if user used space between two '<' making it error free + // e.g var x: A< (a: Tany)=>Tany>; + if (node.typeParameters && currentOutput.charAt(currentOutput.length - 1) === "<") { + closeParenthesizedFunctionType = true; + write("("); + } + } + emitTypeParameters(node.typeParameters); + write("("); } - if (ts.isExpression(node)) { - return emitExpressionWorker(node); + // Parameters + emitCommaList(node.parameters, emitParameterDeclaration); + if (node.kind === 153 /* IndexSignature */) { + write("]"); + } + else { + write(")"); + } + // If this is not a constructor and is not private, emit the return type + var isFunctionTypeOrConstructorType = node.kind === 156 /* FunctionType */ || node.kind === 157 /* ConstructorType */; + if (isFunctionTypeOrConstructorType || node.parent.kind === 159 /* TypeLiteral */) { + // Emit type literal signature return type only if specified + if (node.type) { + write(isFunctionTypeOrConstructorType ? " => " : ": "); + emitType(node.type); + } + } + else if (node.kind !== 148 /* Constructor */ && !ts.hasModifier(node, 8 /* Private */)) { + writeReturnTypeAtSignature(node, getReturnTypeVisibilityError); + } + enclosingDeclaration = prevEnclosingDeclaration; + if (!isFunctionTypeOrConstructorType) { + write(";"); + writeLine(); + } + else if (closeParenthesizedFunctionType) { + write(")"); + } + function getReturnTypeVisibilityError(symbolAccessibilityResult) { + var diagnosticMessage; + switch (node.kind) { + case 152 /* ConstructSignature */: + // Interfaces cannot have return types that cannot be named + diagnosticMessage = symbolAccessibilityResult.errorModuleName ? + ts.Diagnostics.Return_type_of_constructor_signature_from_exported_interface_has_or_is_using_name_0_from_private_module_1 : + ts.Diagnostics.Return_type_of_constructor_signature_from_exported_interface_has_or_is_using_private_name_0; + break; + case 151 /* CallSignature */: + // Interfaces cannot have return types that cannot be named + diagnosticMessage = symbolAccessibilityResult.errorModuleName ? + ts.Diagnostics.Return_type_of_call_signature_from_exported_interface_has_or_is_using_name_0_from_private_module_1 : + ts.Diagnostics.Return_type_of_call_signature_from_exported_interface_has_or_is_using_private_name_0; + break; + case 153 /* IndexSignature */: + // Interfaces cannot have return types that cannot be named + diagnosticMessage = symbolAccessibilityResult.errorModuleName ? + ts.Diagnostics.Return_type_of_index_signature_from_exported_interface_has_or_is_using_name_0_from_private_module_1 : + ts.Diagnostics.Return_type_of_index_signature_from_exported_interface_has_or_is_using_private_name_0; + break; + case 147 /* MethodDeclaration */: + case 146 /* MethodSignature */: + if (ts.hasModifier(node, 32 /* Static */)) { + diagnosticMessage = symbolAccessibilityResult.errorModuleName ? + symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? + ts.Diagnostics.Return_type_of_public_static_method_from_exported_class_has_or_is_using_name_0_from_external_module_1_but_cannot_be_named : + ts.Diagnostics.Return_type_of_public_static_method_from_exported_class_has_or_is_using_name_0_from_private_module_1 : + ts.Diagnostics.Return_type_of_public_static_method_from_exported_class_has_or_is_using_private_name_0; + } + else if (node.parent.kind === 221 /* ClassDeclaration */) { + diagnosticMessage = symbolAccessibilityResult.errorModuleName ? + symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? + ts.Diagnostics.Return_type_of_public_method_from_exported_class_has_or_is_using_name_0_from_external_module_1_but_cannot_be_named : + ts.Diagnostics.Return_type_of_public_method_from_exported_class_has_or_is_using_name_0_from_private_module_1 : + ts.Diagnostics.Return_type_of_public_method_from_exported_class_has_or_is_using_private_name_0; + } + else { + // Interfaces cannot have return types that cannot be named + diagnosticMessage = symbolAccessibilityResult.errorModuleName ? + ts.Diagnostics.Return_type_of_method_from_exported_interface_has_or_is_using_name_0_from_private_module_1 : + ts.Diagnostics.Return_type_of_method_from_exported_interface_has_or_is_using_private_name_0; + } + break; + case 220 /* FunctionDeclaration */: + diagnosticMessage = symbolAccessibilityResult.errorModuleName ? + symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? + ts.Diagnostics.Return_type_of_exported_function_has_or_is_using_name_0_from_external_module_1_but_cannot_be_named : + ts.Diagnostics.Return_type_of_exported_function_has_or_is_using_name_0_from_private_module_1 : + ts.Diagnostics.Return_type_of_exported_function_has_or_is_using_private_name_0; + break; + default: + ts.Debug.fail("This is unknown kind for signature: " + node.kind); + } + return { + diagnosticMessage: diagnosticMessage, + errorNode: node.name || node + }; } } - function emitExpressionWorker(node) { - if (tryEmitSubstitute(node, emitExpressionWorker, /*isExpression*/ true)) { - return; + function emitParameterDeclaration(node) { + increaseIndent(); + emitJsDocComments(node); + if (node.dotDotDotToken) { + write("..."); } - var kind = node.kind; - switch (kind) { - // Literals - case 8 /* NumericLiteral */: - return emitNumericLiteral(node); - case 9 /* StringLiteral */: - case 10 /* RegularExpressionLiteral */: - case 11 /* NoSubstitutionTemplateLiteral */: - return emitLiteral(node); - // Identifiers - case 69 /* Identifier */: - return emitIdentifier(node); - // Reserved words - case 84 /* FalseKeyword */: - case 93 /* NullKeyword */: - case 95 /* SuperKeyword */: - case 99 /* TrueKeyword */: - case 97 /* ThisKeyword */: - return writeTokenNode(node); - // Expressions - case 170 /* ArrayLiteralExpression */: - return emitArrayLiteralExpression(node); - case 171 /* ObjectLiteralExpression */: - return emitObjectLiteralExpression(node); - case 172 /* PropertyAccessExpression */: - return emitPropertyAccessExpression(node); - case 173 /* ElementAccessExpression */: - return emitElementAccessExpression(node); - case 174 /* CallExpression */: - return emitCallExpression(node); - case 175 /* NewExpression */: - return emitNewExpression(node); - case 176 /* TaggedTemplateExpression */: - return emitTaggedTemplateExpression(node); - case 177 /* TypeAssertionExpression */: - return emitTypeAssertionExpression(node); - case 178 /* ParenthesizedExpression */: - return emitParenthesizedExpression(node); - case 179 /* FunctionExpression */: - return emitFunctionExpression(node); - case 180 /* ArrowFunction */: - return emitArrowFunction(node); - case 181 /* DeleteExpression */: - return emitDeleteExpression(node); - case 182 /* TypeOfExpression */: - return emitTypeOfExpression(node); - case 183 /* VoidExpression */: - return emitVoidExpression(node); - case 184 /* AwaitExpression */: - return emitAwaitExpression(node); - case 185 /* PrefixUnaryExpression */: - return emitPrefixUnaryExpression(node); - case 186 /* PostfixUnaryExpression */: - return emitPostfixUnaryExpression(node); - case 187 /* BinaryExpression */: - return emitBinaryExpression(node); - case 188 /* ConditionalExpression */: - return emitConditionalExpression(node); - case 189 /* TemplateExpression */: - return emitTemplateExpression(node); - case 190 /* YieldExpression */: - return emitYieldExpression(node); - case 191 /* SpreadElementExpression */: - return emitSpreadElementExpression(node); - case 192 /* ClassExpression */: - return emitClassExpression(node); - case 193 /* OmittedExpression */: - return; - case 195 /* AsExpression */: - return emitAsExpression(node); - case 196 /* NonNullExpression */: - return emitNonNullExpression(node); - // JSX - case 241 /* JsxElement */: - return emitJsxElement(node); - case 242 /* JsxSelfClosingElement */: - return emitJsxSelfClosingElement(node); - // Transformation nodes - case 288 /* PartiallyEmittedExpression */: - return emitPartiallyEmittedExpression(node); + if (ts.isBindingPattern(node.name)) { + // For bindingPattern, we can't simply writeTextOfNode from the source file + // because we want to omit the initializer and using writeTextOfNode will result in initializer get emitted. + // Therefore, we will have to recursively emit each element in the bindingPattern. + emitBindingPattern(node.name); + } + else { + writeTextOfNode(currentText, node.name); + } + if (resolver.isOptionalParameter(node)) { + write("?"); + } + decreaseIndent(); + if (node.parent.kind === 156 /* FunctionType */ || + node.parent.kind === 157 /* ConstructorType */ || + node.parent.parent.kind === 159 /* TypeLiteral */) { + emitTypeOfVariableDeclarationFromTypeLiteral(node); + } + else if (!ts.hasModifier(node.parent, 8 /* Private */)) { + writeTypeOfDeclaration(node, node.type, getParameterDeclarationTypeVisibilityError); + } + function getParameterDeclarationTypeVisibilityError(symbolAccessibilityResult) { + var diagnosticMessage = getParameterDeclarationTypeVisibilityDiagnosticMessage(symbolAccessibilityResult); + return diagnosticMessage !== undefined ? { + diagnosticMessage: diagnosticMessage, + errorNode: node, + typeName: node.name + } : undefined; + } + function getParameterDeclarationTypeVisibilityDiagnosticMessage(symbolAccessibilityResult) { + switch (node.parent.kind) { + case 148 /* Constructor */: + return symbolAccessibilityResult.errorModuleName ? + symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? + ts.Diagnostics.Parameter_0_of_constructor_from_exported_class_has_or_is_using_name_1_from_external_module_2_but_cannot_be_named : + ts.Diagnostics.Parameter_0_of_constructor_from_exported_class_has_or_is_using_name_1_from_private_module_2 : + ts.Diagnostics.Parameter_0_of_constructor_from_exported_class_has_or_is_using_private_name_1; + case 152 /* ConstructSignature */: + // Interfaces cannot have parameter types that cannot be named + return symbolAccessibilityResult.errorModuleName ? + ts.Diagnostics.Parameter_0_of_constructor_signature_from_exported_interface_has_or_is_using_name_1_from_private_module_2 : + ts.Diagnostics.Parameter_0_of_constructor_signature_from_exported_interface_has_or_is_using_private_name_1; + case 151 /* CallSignature */: + // Interfaces cannot have parameter types that cannot be named + return symbolAccessibilityResult.errorModuleName ? + ts.Diagnostics.Parameter_0_of_call_signature_from_exported_interface_has_or_is_using_name_1_from_private_module_2 : + ts.Diagnostics.Parameter_0_of_call_signature_from_exported_interface_has_or_is_using_private_name_1; + case 147 /* MethodDeclaration */: + case 146 /* MethodSignature */: + if (ts.hasModifier(node.parent, 32 /* Static */)) { + return symbolAccessibilityResult.errorModuleName ? + symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? + ts.Diagnostics.Parameter_0_of_public_static_method_from_exported_class_has_or_is_using_name_1_from_external_module_2_but_cannot_be_named : + ts.Diagnostics.Parameter_0_of_public_static_method_from_exported_class_has_or_is_using_name_1_from_private_module_2 : + ts.Diagnostics.Parameter_0_of_public_static_method_from_exported_class_has_or_is_using_private_name_1; + } + else if (node.parent.parent.kind === 221 /* ClassDeclaration */) { + return symbolAccessibilityResult.errorModuleName ? + symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? + ts.Diagnostics.Parameter_0_of_public_method_from_exported_class_has_or_is_using_name_1_from_external_module_2_but_cannot_be_named : + ts.Diagnostics.Parameter_0_of_public_method_from_exported_class_has_or_is_using_name_1_from_private_module_2 : + ts.Diagnostics.Parameter_0_of_public_method_from_exported_class_has_or_is_using_private_name_1; + } + else { + // Interfaces cannot have parameter types that cannot be named + return symbolAccessibilityResult.errorModuleName ? + ts.Diagnostics.Parameter_0_of_method_from_exported_interface_has_or_is_using_name_1_from_private_module_2 : + ts.Diagnostics.Parameter_0_of_method_from_exported_interface_has_or_is_using_private_name_1; + } + case 220 /* FunctionDeclaration */: + return symbolAccessibilityResult.errorModuleName ? + symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? + ts.Diagnostics.Parameter_0_of_exported_function_has_or_is_using_name_1_from_external_module_2_but_cannot_be_named : + ts.Diagnostics.Parameter_0_of_exported_function_has_or_is_using_name_1_from_private_module_2 : + ts.Diagnostics.Parameter_0_of_exported_function_has_or_is_using_private_name_1; + default: + ts.Debug.fail("This is unknown parent for parameter: " + node.parent.kind); + } } - } - // - // Literals/Pseudo-literals - // - // SyntaxKind.NumericLiteral - function emitNumericLiteral(node) { - emitLiteral(node); - if (node.trailingComment) { - write(" /*" + node.trailingComment + "*/"); + function emitBindingPattern(bindingPattern) { + // We have to explicitly emit square bracket and bracket because these tokens are not store inside the node. + if (bindingPattern.kind === 167 /* ObjectBindingPattern */) { + write("{"); + emitCommaList(bindingPattern.elements, emitBindingElement); + write("}"); + } + else if (bindingPattern.kind === 168 /* ArrayBindingPattern */) { + write("["); + var elements = bindingPattern.elements; + emitCommaList(elements, emitBindingElement); + if (elements && elements.hasTrailingComma) { + write(", "); + } + write("]"); + } } - } - // SyntaxKind.StringLiteral - // SyntaxKind.RegularExpressionLiteral - // SyntaxKind.NoSubstitutionTemplateLiteral - // SyntaxKind.TemplateHead - // SyntaxKind.TemplateMiddle - // SyntaxKind.TemplateTail - function emitLiteral(node) { - var text = getLiteralTextOfNode(node); - if ((compilerOptions.sourceMap || compilerOptions.inlineSourceMap) - && (node.kind === 9 /* StringLiteral */ || ts.isTemplateLiteralKind(node.kind))) { - writer.writeLiteral(text); + function emitBindingElement(bindingElement) { + if (bindingElement.kind === 193 /* OmittedExpression */) { + // If bindingElement is an omittedExpression (i.e. containing elision), + // we will emit blank space (although this may differ from users' original code, + // it allows emitSeparatedList to write separator appropriately) + // Example: + // original: function foo([, x, ,]) {} + // emit : function foo([ , x, , ]) {} + write(" "); + } + else if (bindingElement.kind === 169 /* BindingElement */) { + if (bindingElement.propertyName) { + // bindingElement has propertyName property in the following case: + // { y: [a,b,c] ...} -> bindingPattern will have a property called propertyName for "y" + // We have to explicitly emit the propertyName before descending into its binding elements. + // Example: + // original: function foo({y: [a,b,c]}) {} + // emit : declare function foo({y: [a, b, c]}: { y: [any, any, any] }) void; + writeTextOfNode(currentText, bindingElement.propertyName); + write(": "); + } + if (bindingElement.name) { + if (ts.isBindingPattern(bindingElement.name)) { + // If it is a nested binding pattern, we will recursively descend into each element and emit each one separately. + // In the case of rest element, we will omit rest element. + // Example: + // original: function foo([a, [[b]], c] = [1,[["string"]], 3]) {} + // emit : declare function foo([a, [[b]], c]: [number, [[string]], number]): void; + // original with rest: function foo([a, ...c]) {} + // emit : declare function foo([a, ...c]): void; + emitBindingPattern(bindingElement.name); + } + else { + ts.Debug.assert(bindingElement.name.kind === 69 /* Identifier */); + // If the node is just an identifier, we will simply emit the text associated with the node's name + // Example: + // original: function foo({y = 10, x}) {} + // emit : declare function foo({y, x}: {number, any}): void; + if (bindingElement.dotDotDotToken) { + write("..."); + } + writeTextOfNode(currentText, bindingElement.name); + } + } + } } - else { - write(text); + } + function emitNode(node) { + switch (node.kind) { + case 220 /* FunctionDeclaration */: + case 225 /* ModuleDeclaration */: + case 229 /* ImportEqualsDeclaration */: + case 222 /* InterfaceDeclaration */: + case 221 /* ClassDeclaration */: + case 223 /* TypeAliasDeclaration */: + case 224 /* EnumDeclaration */: + return emitModuleElement(node, isModuleElementVisible(node)); + case 200 /* VariableStatement */: + return emitModuleElement(node, isVariableStatementVisible(node)); + case 230 /* ImportDeclaration */: + // Import declaration without import clause is visible, otherwise it is not visible + return emitModuleElement(node, /*isModuleElementVisible*/ !node.importClause); + case 236 /* ExportDeclaration */: + return emitExportDeclaration(node); + case 148 /* Constructor */: + case 147 /* MethodDeclaration */: + case 146 /* MethodSignature */: + return writeFunctionDeclaration(node); + case 152 /* ConstructSignature */: + case 151 /* CallSignature */: + case 153 /* IndexSignature */: + return emitSignatureDeclarationWithJsDocComments(node); + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + return emitAccessorDeclaration(node); + case 145 /* PropertyDeclaration */: + case 144 /* PropertySignature */: + return emitPropertyDeclaration(node); + case 255 /* EnumMember */: + return emitEnumMemberDeclaration(node); + case 235 /* ExportAssignment */: + return emitExportAssignment(node); + case 256 /* SourceFile */: + return emitSourceFile(node); } } - // - // Identifiers - // - function emitIdentifier(node) { - if (node.emitFlags & 16 /* UMDDefine */) { - writeLines(umdHelper); + /** + * Adds the reference to referenced file, returns true if global file reference was emitted + * @param referencedFile + * @param addBundledFileReference Determines if global file reference corresponding to bundled file should be emitted or not + */ + function writeReferencePath(referencedFile, addBundledFileReference, emitOnlyDtsFiles) { + var declFileName; + var addedBundledEmitReference = false; + if (ts.isDeclarationFile(referencedFile)) { + // Declaration file, use declaration file name + declFileName = referencedFile.fileName; } else { - write(getTextOfNode(node, /*includeTrivia*/ false)); + // Get the declaration file path + ts.forEachExpectedEmitFile(host, getDeclFileName, referencedFile, emitOnlyDtsFiles); } - } - // - // Names - // - function emitQualifiedName(node) { - emitEntityName(node.left); - write("."); - emit(node.right); - } - function emitEntityName(node) { - if (node.kind === 69 /* Identifier */) { - emitExpression(node); + if (declFileName) { + declFileName = ts.getRelativePathToDirectoryOrUrl(ts.getDirectoryPath(ts.normalizeSlashes(declarationFilePath)), declFileName, host.getCurrentDirectory(), host.getCanonicalFileName, + /*isAbsolutePathAnUrl*/ false); + referencesOutput += "/// " + newLine; } - else { - emit(node); + return addedBundledEmitReference; + function getDeclFileName(emitFileNames, sourceFiles, isBundledEmit) { + // Dont add reference path to this file if it is a bundled emit and caller asked not emit bundled file path + if (isBundledEmit && !addBundledFileReference) { + return; + } + ts.Debug.assert(!!emitFileNames.declarationFilePath || ts.isSourceFileJavaScript(referencedFile), "Declaration file is not present only for javascript files"); + declFileName = emitFileNames.declarationFilePath || emitFileNames.jsFilePath; + addedBundledEmitReference = isBundledEmit; } } - function emitComputedPropertyName(node) { - write("["); - emitExpression(node.expression); - write("]"); - } - // - // Signature elements - // - function emitTypeParameter(node) { - emit(node.name); - emitWithPrefix(" extends ", node.constraint); - } - function emitParameter(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - writeIfPresent(node.dotDotDotToken, "..."); - emit(node.name); - writeIfPresent(node.questionToken, "?"); - emitExpressionWithPrefix(" = ", node.initializer); - emitWithPrefix(": ", node.type); + } + /* @internal */ + function writeDeclarationFile(declarationFilePath, sourceFiles, isBundledEmit, host, resolver, emitterDiagnostics, emitOnlyDtsFiles) { + var emitDeclarationResult = emitDeclarations(host, resolver, emitterDiagnostics, declarationFilePath, sourceFiles, isBundledEmit, emitOnlyDtsFiles); + var emitSkipped = emitDeclarationResult.reportedDeclarationError || host.isEmitBlocked(declarationFilePath) || host.getCompilerOptions().noEmit; + if (!emitSkipped) { + var declarationOutput = emitDeclarationResult.referencesOutput + + getDeclarationOutput(emitDeclarationResult.synchronousDeclarationOutput, emitDeclarationResult.moduleElementDeclarationEmitInfo); + ts.writeFile(host, emitterDiagnostics, declarationFilePath, declarationOutput, host.getCompilerOptions().emitBOM, sourceFiles); } - function emitDecorator(decorator) { - write("@"); - emitExpression(decorator.expression); + return emitSkipped; + function getDeclarationOutput(synchronousDeclarationOutput, moduleElementDeclarationEmitInfo) { + var appliedSyncOutputPos = 0; + var declarationOutput = ""; + // apply asynchronous additions to the synchronous output + ts.forEach(moduleElementDeclarationEmitInfo, function (aliasEmitInfo) { + if (aliasEmitInfo.asynchronousOutput) { + declarationOutput += synchronousDeclarationOutput.substring(appliedSyncOutputPos, aliasEmitInfo.outputPos); + declarationOutput += getDeclarationOutput(aliasEmitInfo.asynchronousOutput, aliasEmitInfo.subModuleElementDeclarationEmitInfo); + appliedSyncOutputPos = aliasEmitInfo.outputPos; + } + }); + declarationOutput += synchronousDeclarationOutput.substring(appliedSyncOutputPos); + return declarationOutput; } + } + ts.writeDeclarationFile = writeDeclarationFile; +})(ts || (ts = {})); +/// +/// +/// +/// +/// +/* @internal */ +var ts; +(function (ts) { + // Flags enum to track count of temp variables and a few dedicated names + var TempFlags; + (function (TempFlags) { + TempFlags[TempFlags["Auto"] = 0] = "Auto"; + TempFlags[TempFlags["CountMask"] = 268435455] = "CountMask"; + TempFlags[TempFlags["_i"] = 268435456] = "_i"; + })(TempFlags || (TempFlags = {})); + var id = function (s) { return s; }; + var nullTransformers = [function (ctx) { return id; }]; + // targetSourceFile is when users only want one file in entire project to be emitted. This is used in compileOnSave feature + function emitFiles(resolver, host, targetSourceFile, emitOnlyDtsFiles) { + var delimiters = createDelimiterMap(); + var brackets = createBracketsMap(); + // emit output for the __extends helper function + var extendsHelper = "\nvar __extends = (this && this.__extends) || function (d, b) {\n for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];\n function __() { this.constructor = d; }\n d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\n};"; + // Emit output for the __assign helper function. + // This is typically used for JSX spread attributes, + // and can be used for object literal spread properties. + var assignHelper = "\nvar __assign = (this && this.__assign) || Object.assign || function(t) {\n for (var s, i = 1, n = arguments.length; i < n; i++) {\n s = arguments[i];\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))\n t[p] = s[p];\n }\n return t;\n};"; + // emit output for the __decorate helper function + var decorateHelper = "\nvar __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {\n var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;\n if (typeof Reflect === \"object\" && typeof Reflect.decorate === \"function\") r = Reflect.decorate(decorators, target, key, desc);\n else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;\n return c > 3 && r && Object.defineProperty(target, key, r), r;\n};"; + // emit output for the __metadata helper function + var metadataHelper = "\nvar __metadata = (this && this.__metadata) || function (k, v) {\n if (typeof Reflect === \"object\" && typeof Reflect.metadata === \"function\") return Reflect.metadata(k, v);\n};"; + // emit output for the __param helper function + var paramHelper = "\nvar __param = (this && this.__param) || function (paramIndex, decorator) {\n return function (target, key) { decorator(target, key, paramIndex); }\n};"; + // emit output for the __awaiter helper function + var awaiterHelper = "\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments)).next());\n });\n};"; + // The __generator helper is used by down-level transformations to emulate the runtime + // semantics of an ES2015 generator function. When called, this helper returns an + // object that implements the Iterator protocol, in that it has `next`, `return`, and + // `throw` methods that step through the generator when invoked. // - // Type members - // - function emitPropertySignature(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - emit(node.name); - writeIfPresent(node.questionToken, "?"); - emitWithPrefix(": ", node.type); - write(";"); - } - function emitPropertyDeclaration(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - emit(node.name); - emitWithPrefix(": ", node.type); - emitExpressionWithPrefix(" = ", node.initializer); - write(";"); - } - function emitMethodSignature(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - emit(node.name); - writeIfPresent(node.questionToken, "?"); - emitTypeParameters(node, node.typeParameters); - emitParameters(node, node.parameters); - emitWithPrefix(": ", node.type); - write(";"); - } - function emitMethodDeclaration(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - writeIfPresent(node.asteriskToken, "*"); - emit(node.name); - emitSignatureAndBody(node, emitSignatureHead); - } - function emitConstructor(node) { - emitModifiers(node, node.modifiers); - write("constructor"); - emitSignatureAndBody(node, emitSignatureHead); - } - function emitAccessorDeclaration(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - write(node.kind === 149 /* GetAccessor */ ? "get " : "set "); - emit(node.name); - emitSignatureAndBody(node, emitSignatureHead); - } - function emitCallSignature(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - emitTypeParameters(node, node.typeParameters); - emitParameters(node, node.parameters); - emitWithPrefix(": ", node.type); - write(";"); - } - function emitConstructSignature(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - write("new "); - emitTypeParameters(node, node.typeParameters); - emitParameters(node, node.parameters); - emitWithPrefix(": ", node.type); - write(";"); - } - function emitIndexSignature(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - emitParametersForIndexSignature(node, node.parameters); - emitWithPrefix(": ", node.type); - write(";"); - } - function emitSemicolonClassElement(node) { - write(";"); - } + // parameters: + // thisArg The value to use as the `this` binding for the transformed generator body. + // body A function that acts as the transformed generator body. // - // Types + // variables: + // _ Persistent state for the generator that is shared between the helper and the + // generator body. The state object has the following members: + // sent() - A method that returns or throws the current completion value. + // label - The next point at which to resume evaluation of the generator body. + // trys - A stack of protected regions (try/catch/finally blocks). + // ops - A stack of pending instructions when inside of a finally block. + // f A value indicating whether the generator is executing. + // y An iterator to delegate for a yield*. + // t A temporary variable that holds one of the following values (note that these + // cases do not overlap): + // - The completion value when resuming from a `yield` or `yield*`. + // - The error value for a catch block. + // - The current protected region (array of try/catch/finally/end labels). + // - The verb (`next`, `throw`, or `return` method) to delegate to the expression + // of a `yield*`. + // - The result of evaluating the verb delegated to the expression of a `yield*`. // - function emitTypePredicate(node) { - emit(node.parameterName); - write(" is "); - emit(node.type); - } - function emitTypeReference(node) { - emit(node.typeName); - emitTypeArguments(node, node.typeArguments); - } - function emitFunctionType(node) { - emitTypeParameters(node, node.typeParameters); - emitParametersForArrow(node, node.parameters); - write(" => "); - emit(node.type); - } - function emitConstructorType(node) { - write("new "); - emitTypeParameters(node, node.typeParameters); - emitParametersForArrow(node, node.parameters); - write(" => "); - emit(node.type); - } - function emitTypeQuery(node) { - write("typeof "); - emit(node.exprName); - } - function emitTypeLiteral(node) { - write("{"); - emitList(node, node.members, 65 /* TypeLiteralMembers */); - write("}"); - } - function emitArrayType(node) { - emit(node.elementType); - write("[]"); - } - function emitTupleType(node) { - write("["); - emitList(node, node.elementTypes, 336 /* TupleTypeElements */); - write("]"); - } - function emitUnionType(node) { - emitList(node, node.types, 260 /* UnionTypeConstituents */); - } - function emitIntersectionType(node) { - emitList(node, node.types, 264 /* IntersectionTypeConstituents */); - } - function emitParenthesizedType(node) { - write("("); - emit(node.type); - write(")"); - } - function emitThisType(node) { - write("this"); - } - function emitLiteralType(node) { - emitExpression(node.literal); - } + // functions: + // verb(n) Creates a bound callback to the `step` function for opcode `n`. + // step(op) Evaluates opcodes in a generator body until execution is suspended or + // completed. // - // Binding patterns + // The __generator helper understands a limited set of instructions: + // 0: next(value?) - Start or resume the generator with the specified value. + // 1: throw(error) - Resume the generator with an exception. If the generator is + // suspended inside of one or more protected regions, evaluates + // any intervening finally blocks between the current label and + // the nearest catch block or function boundary. If uncaught, the + // exception is thrown to the caller. + // 2: return(value?) - Resume the generator as if with a return. If the generator is + // suspended inside of one or more protected regions, evaluates any + // intervening finally blocks. + // 3: break(label) - Jump to the specified label. If the label is outside of the + // current protected region, evaluates any intervening finally + // blocks. + // 4: yield(value?) - Yield execution to the caller with an optional value. When + // resumed, the generator will continue at the next label. + // 5: yield*(value) - Delegates evaluation to the supplied iterator. When + // delegation completes, the generator will continue at the next + // label. + // 6: catch(error) - Handles an exception thrown from within the generator body. If + // the current label is inside of one or more protected regions, + // evaluates any intervening finally blocks between the current + // label and the nearest catch block or function boundary. If + // uncaught, the exception is thrown to the caller. + // 7: endfinally - Ends a finally block, resuming the last instruction prior to + // entering a finally block. // - function emitObjectBindingPattern(node) { - var elements = node.elements; - if (elements.length === 0) { - write("{}"); - } - else { - write("{"); - emitList(node, elements, 432 /* ObjectBindingPatternElements */); - write("}"); - } - } - function emitArrayBindingPattern(node) { - var elements = node.elements; - if (elements.length === 0) { - write("[]"); - } - else { - write("["); - emitList(node, node.elements, 304 /* ArrayBindingPatternElements */); - write("]"); - } - } - function emitBindingElement(node) { - emitWithSuffix(node.propertyName, ": "); - writeIfPresent(node.dotDotDotToken, "..."); - emit(node.name); - emitExpressionWithPrefix(" = ", node.initializer); + // For examples of how these are used, see the comments in ./transformers/generators.ts + var generatorHelper = "\nvar __generator = (this && this.__generator) || function (thisArg, body) {\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t;\n return { next: verb(0), \"throw\": verb(1), \"return\": verb(2) };\n function verb(n) { return function (v) { return step([n, v]); }; }\n function step(op) {\n if (f) throw new TypeError(\"Generator is already executing.\");\n while (_) try {\n if (f = 1, y && (t = y[op[0] & 2 ? \"return\" : op[0] ? \"throw\" : \"next\"]) && !(t = t.call(y, op[1])).done) return t;\n if (y = 0, t) op = [0, t.value];\n switch (op[0]) {\n case 0: case 1: t = op; break;\n case 4: _.label++; return { value: op[1], done: false };\n case 5: _.label++; y = op[1]; op = [0]; continue;\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\n default:\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\n if (t[2]) _.ops.pop();\n _.trys.pop(); continue;\n }\n op = body.call(thisArg, _);\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\n }\n};"; + // emit output for the __export helper function + var exportStarHelper = "\nfunction __export(m) {\n for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];\n}"; + // emit output for the UMD helper function. + var umdHelper = "\n(function (dependencies, factory) {\n if (typeof module === 'object' && typeof module.exports === 'object') {\n var v = factory(require, exports); if (v !== undefined) module.exports = v;\n }\n else if (typeof define === 'function' && define.amd) {\n define(dependencies, factory);\n }\n})"; + var superHelper = "\nconst _super = name => super[name];"; + var advancedSuperHelper = "\nconst _super = (function (geti, seti) {\n const cache = Object.create(null);\n return name => cache[name] || (cache[name] = { get value() { return geti(name); }, set value(v) { seti(name, v); } });\n})(name => super[name], (name, value) => super[name] = value);"; + var compilerOptions = host.getCompilerOptions(); + var languageVersion = ts.getEmitScriptTarget(compilerOptions); + var moduleKind = ts.getEmitModuleKind(compilerOptions); + var sourceMapDataList = compilerOptions.sourceMap || compilerOptions.inlineSourceMap ? [] : undefined; + var emittedFilesList = compilerOptions.listEmittedFiles ? [] : undefined; + var emitterDiagnostics = ts.createDiagnosticCollection(); + var newLine = host.getNewLine(); + var transformers = emitOnlyDtsFiles ? nullTransformers : ts.getTransformers(compilerOptions); + var writer = ts.createTextWriter(newLine); + var write = writer.write, writeLine = writer.writeLine, increaseIndent = writer.increaseIndent, decreaseIndent = writer.decreaseIndent; + var sourceMap = ts.createSourceMapWriter(host, writer); + var emitNodeWithSourceMap = sourceMap.emitNodeWithSourceMap, emitTokenWithSourceMap = sourceMap.emitTokenWithSourceMap; + var comments = ts.createCommentWriter(host, writer, sourceMap); + var emitNodeWithComments = comments.emitNodeWithComments, emitBodyWithDetachedComments = comments.emitBodyWithDetachedComments, emitTrailingCommentsOfPosition = comments.emitTrailingCommentsOfPosition; + var nodeIdToGeneratedName; + var autoGeneratedIdToGeneratedName; + var generatedNameSet; + var tempFlags; + var currentSourceFile; + var currentText; + var currentFileIdentifiers; + var extendsEmitted; + var assignEmitted; + var decorateEmitted; + var paramEmitted; + var awaiterEmitted; + var isOwnFileEmit; + var emitSkipped = false; + var sourceFiles = ts.getSourceFilesToEmit(host, targetSourceFile); + // Transform the source files + ts.performance.mark("beforeTransform"); + var _a = ts.transformFiles(resolver, host, sourceFiles, transformers), transformed = _a.transformed, emitNodeWithSubstitution = _a.emitNodeWithSubstitution, emitNodeWithNotification = _a.emitNodeWithNotification; + ts.performance.measure("transformTime", "beforeTransform"); + // Emit each output file + ts.performance.mark("beforePrint"); + ts.forEachTransformedEmitFile(host, transformed, emitFile, emitOnlyDtsFiles); + ts.performance.measure("printTime", "beforePrint"); + // Clean up emit nodes on parse tree + for (var _b = 0, sourceFiles_4 = sourceFiles; _b < sourceFiles_4.length; _b++) { + var sourceFile = sourceFiles_4[_b]; + ts.disposeEmitNodes(sourceFile); } - // - // Expressions - // - function emitArrayLiteralExpression(node) { - var elements = node.elements; - if (elements.length === 0) { - write("[]"); + return { + emitSkipped: emitSkipped, + diagnostics: emitterDiagnostics.getDiagnostics(), + emittedFiles: emittedFilesList, + sourceMaps: sourceMapDataList + }; + function emitFile(jsFilePath, sourceMapFilePath, declarationFilePath, sourceFiles, isBundledEmit) { + // Make sure not to write js file and source map file if any of them cannot be written + if (!host.isEmitBlocked(jsFilePath) && !compilerOptions.noEmit) { + if (!emitOnlyDtsFiles) { + printFile(jsFilePath, sourceMapFilePath, sourceFiles, isBundledEmit); + } } else { - var preferNewLine = node.multiLine ? 32768 /* PreferNewLine */ : 0 /* None */; - emitExpressionList(node, elements, 4466 /* ArrayLiteralExpressionElements */ | preferNewLine); + emitSkipped = true; } - } - function emitObjectLiteralExpression(node) { - var properties = node.properties; - if (properties.length === 0) { - write("{}"); + if (declarationFilePath) { + emitSkipped = ts.writeDeclarationFile(declarationFilePath, ts.getOriginalSourceFiles(sourceFiles), isBundledEmit, host, resolver, emitterDiagnostics, emitOnlyDtsFiles) || emitSkipped; } - else { - var indentedFlag = node.emitFlags & 524288 /* Indented */; - if (indentedFlag) { - increaseIndent(); + if (!emitSkipped && emittedFilesList) { + if (!emitOnlyDtsFiles) { + emittedFilesList.push(jsFilePath); } - var preferNewLine = node.multiLine ? 32768 /* PreferNewLine */ : 0 /* None */; - var allowTrailingComma = languageVersion >= 1 /* ES5 */ ? 32 /* AllowTrailingComma */ : 0 /* None */; - emitList(node, properties, 978 /* ObjectLiteralExpressionProperties */ | allowTrailingComma | preferNewLine); - if (indentedFlag) { - decreaseIndent(); + if (sourceMapFilePath) { + emittedFilesList.push(sourceMapFilePath); + } + if (declarationFilePath) { + emittedFilesList.push(declarationFilePath); } } } - function emitPropertyAccessExpression(node) { - if (tryEmitConstantValue(node)) { - return; - } - var indentBeforeDot = false; - var indentAfterDot = false; - if (!(node.emitFlags & 1048576 /* NoIndentation */)) { - var dotRangeStart = node.expression.end; - var dotRangeEnd = ts.skipTrivia(currentText, node.expression.end) + 1; - var dotToken = { kind: 21 /* DotToken */, pos: dotRangeStart, end: dotRangeEnd }; - indentBeforeDot = needsIndentation(node, node.expression, dotToken); - indentAfterDot = needsIndentation(node, dotToken, node.name); - } - var shouldEmitDotDot = !indentBeforeDot && needsDotDotForPropertyAccess(node.expression); - emitExpression(node.expression); - increaseIndentIf(indentBeforeDot); - write(shouldEmitDotDot ? ".." : "."); - increaseIndentIf(indentAfterDot); - emit(node.name); - decreaseIndentIf(indentBeforeDot, indentAfterDot); - } - // 1..toString is a valid property access, emit a dot after the literal - // Also emit a dot if expression is a integer const enum value - it will appear in generated code as numeric literal - function needsDotDotForPropertyAccess(expression) { - if (expression.kind === 8 /* NumericLiteral */) { - // check if numeric literal was originally written with a dot - var text = getLiteralTextOfNode(expression); - return text.indexOf(ts.tokenToString(21 /* DotToken */)) < 0; + function printFile(jsFilePath, sourceMapFilePath, sourceFiles, isBundledEmit) { + sourceMap.initialize(jsFilePath, sourceMapFilePath, sourceFiles, isBundledEmit); + nodeIdToGeneratedName = []; + autoGeneratedIdToGeneratedName = []; + generatedNameSet = ts.createMap(); + isOwnFileEmit = !isBundledEmit; + // Emit helpers from all the files + if (isBundledEmit && moduleKind) { + for (var _a = 0, sourceFiles_5 = sourceFiles; _a < sourceFiles_5.length; _a++) { + var sourceFile = sourceFiles_5[_a]; + emitEmitHelpers(sourceFile); + } } - else { - // check if constant enum value is integer - var constantValue = tryGetConstEnumValue(expression); - // isFinite handles cases when constantValue is undefined - return isFinite(constantValue) && Math.floor(constantValue) === constantValue; + // Print each transformed source file. + ts.forEach(sourceFiles, printSourceFile); + writeLine(); + var sourceMappingURL = sourceMap.getSourceMappingURL(); + if (sourceMappingURL) { + write("//# " + "sourceMappingURL" + "=" + sourceMappingURL); // Sometimes tools can sometimes see this line as a source mapping url comment } - } - function emitElementAccessExpression(node) { - if (tryEmitConstantValue(node)) { - return; + // Write the source map + if (compilerOptions.sourceMap && !compilerOptions.inlineSourceMap) { + ts.writeFile(host, emitterDiagnostics, sourceMapFilePath, sourceMap.getText(), /*writeByteOrderMark*/ false); } - emitExpression(node.expression); - write("["); - emitExpression(node.argumentExpression); - write("]"); - } - function emitCallExpression(node) { - emitExpression(node.expression); - emitExpressionList(node, node.arguments, 1296 /* CallExpressionArguments */); - } - function emitNewExpression(node) { - write("new "); - emitExpression(node.expression); - emitExpressionList(node, node.arguments, 9488 /* NewExpressionArguments */); - } - function emitTaggedTemplateExpression(node) { - emitExpression(node.tag); - write(" "); - emitExpression(node.template); - } - function emitTypeAssertionExpression(node) { - if (node.type) { - write("<"); - emit(node.type); - write(">"); + // Record source map data for the test harness. + if (sourceMapDataList) { + sourceMapDataList.push(sourceMap.getSourceMapData()); } - emitExpression(node.expression); - } - function emitParenthesizedExpression(node) { - write("("); - emitExpression(node.expression); - write(")"); - } - function emitFunctionExpression(node) { - emitFunctionDeclarationOrExpression(node); - } - function emitArrowFunction(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - emitSignatureAndBody(node, emitArrowFunctionHead); + // Write the output file + ts.writeFile(host, emitterDiagnostics, jsFilePath, writer.getText(), compilerOptions.emitBOM); + // Reset state + sourceMap.reset(); + comments.reset(); + writer.reset(); + tempFlags = 0 /* Auto */; + currentSourceFile = undefined; + currentText = undefined; + extendsEmitted = false; + assignEmitted = false; + decorateEmitted = false; + paramEmitted = false; + awaiterEmitted = false; + isOwnFileEmit = false; } - function emitArrowFunctionHead(node) { - emitTypeParameters(node, node.typeParameters); - emitParametersForArrow(node, node.parameters); - emitWithPrefix(": ", node.type); - write(" =>"); + function printSourceFile(node) { + currentSourceFile = node; + currentText = node.text; + currentFileIdentifiers = node.identifiers; + sourceMap.setSourceFile(node); + comments.setSourceFile(node); + pipelineEmitWithNotification(0 /* SourceFile */, node); } - function emitDeleteExpression(node) { - write("delete "); - emitExpression(node.expression); + /** + * Emits a node. + */ + function emit(node) { + pipelineEmitWithNotification(3 /* Unspecified */, node); } - function emitTypeOfExpression(node) { - write("typeof "); - emitExpression(node.expression); + /** + * Emits an IdentifierName. + */ + function emitIdentifierName(node) { + pipelineEmitWithNotification(2 /* IdentifierName */, node); } - function emitVoidExpression(node) { - write("void "); - emitExpression(node.expression); + /** + * Emits an expression node. + */ + function emitExpression(node) { + pipelineEmitWithNotification(1 /* Expression */, node); } - function emitAwaitExpression(node) { - write("await "); - emitExpression(node.expression); + /** + * Emits a node with possible notification. + * + * NOTE: Do not call this method directly. It is part of the emit pipeline + * and should only be called from printSourceFile, emit, emitExpression, or + * emitIdentifierName. + */ + function pipelineEmitWithNotification(emitContext, node) { + emitNodeWithNotification(emitContext, node, pipelineEmitWithComments); } - function emitPrefixUnaryExpression(node) { - writeTokenText(node.operator); - if (shouldEmitWhitespaceBeforeOperand(node)) { - write(" "); + /** + * Emits a node with comments. + * + * NOTE: Do not call this method directly. It is part of the emit pipeline + * and should only be called indirectly from pipelineEmitWithNotification. + */ + function pipelineEmitWithComments(emitContext, node) { + // Do not emit comments for SourceFile + if (emitContext === 0 /* SourceFile */) { + pipelineEmitWithSourceMap(emitContext, node); + return; } - emitExpression(node.operand); - } - function shouldEmitWhitespaceBeforeOperand(node) { - // In some cases, we need to emit a space between the operator and the operand. One obvious case - // is when the operator is an identifier, like delete or typeof. We also need to do this for plus - // and minus expressions in certain cases. Specifically, consider the following two cases (parens - // are just for clarity of exposition, and not part of the source code): - // - // (+(+1)) - // (+(++1)) - // - // We need to emit a space in both cases. In the first case, the absence of a space will make - // the resulting expression a prefix increment operation. And in the second, it will make the resulting - // expression a prefix increment whose operand is a plus expression - (++(+x)) - // The same is true of minus of course. - var operand = node.operand; - return operand.kind === 185 /* PrefixUnaryExpression */ - && ((node.operator === 35 /* PlusToken */ && (operand.operator === 35 /* PlusToken */ || operand.operator === 41 /* PlusPlusToken */)) - || (node.operator === 36 /* MinusToken */ && (operand.operator === 36 /* MinusToken */ || operand.operator === 42 /* MinusMinusToken */))); - } - function emitPostfixUnaryExpression(node) { - emitExpression(node.operand); - writeTokenText(node.operator); - } - function emitBinaryExpression(node) { - var isCommaOperator = node.operatorToken.kind !== 24 /* CommaToken */; - var indentBeforeOperator = needsIndentation(node, node.left, node.operatorToken); - var indentAfterOperator = needsIndentation(node, node.operatorToken, node.right); - emitExpression(node.left); - increaseIndentIf(indentBeforeOperator, isCommaOperator ? " " : undefined); - writeTokenText(node.operatorToken.kind); - increaseIndentIf(indentAfterOperator, " "); - emitExpression(node.right); - decreaseIndentIf(indentBeforeOperator, indentAfterOperator); - } - function emitConditionalExpression(node) { - var indentBeforeQuestion = needsIndentation(node, node.condition, node.questionToken); - var indentAfterQuestion = needsIndentation(node, node.questionToken, node.whenTrue); - var indentBeforeColon = needsIndentation(node, node.whenTrue, node.colonToken); - var indentAfterColon = needsIndentation(node, node.colonToken, node.whenFalse); - emitExpression(node.condition); - increaseIndentIf(indentBeforeQuestion, " "); - write("?"); - increaseIndentIf(indentAfterQuestion, " "); - emitExpression(node.whenTrue); - decreaseIndentIf(indentBeforeQuestion, indentAfterQuestion); - increaseIndentIf(indentBeforeColon, " "); - write(":"); - increaseIndentIf(indentAfterColon, " "); - emitExpression(node.whenFalse); - decreaseIndentIf(indentBeforeColon, indentAfterColon); + emitNodeWithComments(emitContext, node, pipelineEmitWithSourceMap); } - function emitTemplateExpression(node) { - emit(node.head); - emitList(node, node.templateSpans, 131072 /* TemplateExpressionSpans */); + /** + * Emits a node with source maps. + * + * NOTE: Do not call this method directly. It is part of the emit pipeline + * and should only be called indirectly from pipelineEmitWithComments. + */ + function pipelineEmitWithSourceMap(emitContext, node) { + // Do not emit source mappings for SourceFile or IdentifierName + if (emitContext === 0 /* SourceFile */ + || emitContext === 2 /* IdentifierName */) { + pipelineEmitWithSubstitution(emitContext, node); + return; + } + emitNodeWithSourceMap(emitContext, node, pipelineEmitWithSubstitution); } - function emitYieldExpression(node) { - write(node.asteriskToken ? "yield*" : "yield"); - emitExpressionWithPrefix(" ", node.expression); + /** + * Emits a node with possible substitution. + * + * NOTE: Do not call this method directly. It is part of the emit pipeline + * and should only be called indirectly from pipelineEmitWithSourceMap or + * pipelineEmitInUnspecifiedContext (when picking a more specific context). + */ + function pipelineEmitWithSubstitution(emitContext, node) { + emitNodeWithSubstitution(emitContext, node, pipelineEmitForContext); } - function emitSpreadElementExpression(node) { - write("..."); - emitExpression(node.expression); + /** + * Emits a node. + * + * NOTE: Do not call this method directly. It is part of the emit pipeline + * and should only be called indirectly from pipelineEmitWithSubstitution. + */ + function pipelineEmitForContext(emitContext, node) { + switch (emitContext) { + case 0 /* SourceFile */: return pipelineEmitInSourceFileContext(node); + case 2 /* IdentifierName */: return pipelineEmitInIdentifierNameContext(node); + case 3 /* Unspecified */: return pipelineEmitInUnspecifiedContext(node); + case 1 /* Expression */: return pipelineEmitInExpressionContext(node); + } } - function emitClassExpression(node) { - emitClassDeclarationOrExpression(node); + /** + * Emits a node in the SourceFile EmitContext. + * + * NOTE: Do not call this method directly. It is part of the emit pipeline + * and should only be called indirectly from pipelineEmitForContext. + */ + function pipelineEmitInSourceFileContext(node) { + var kind = node.kind; + switch (kind) { + // Top-level nodes + case 256 /* SourceFile */: + return emitSourceFile(node); + } } - function emitExpressionWithTypeArguments(node) { - emitExpression(node.expression); - emitTypeArguments(node, node.typeArguments); + /** + * Emits a node in the IdentifierName EmitContext. + * + * NOTE: Do not call this method directly. It is part of the emit pipeline + * and should only be called indirectly from pipelineEmitForContext. + */ + function pipelineEmitInIdentifierNameContext(node) { + var kind = node.kind; + switch (kind) { + // Identifiers + case 69 /* Identifier */: + return emitIdentifier(node); + } } - function emitAsExpression(node) { - emitExpression(node.expression); - if (node.type) { - write(" as "); - emit(node.type); + /** + * Emits a node in the Unspecified EmitContext. + * + * NOTE: Do not call this method directly. It is part of the emit pipeline + * and should only be called indirectly from pipelineEmitForContext. + */ + function pipelineEmitInUnspecifiedContext(node) { + var kind = node.kind; + switch (kind) { + // Pseudo-literals + case 12 /* TemplateHead */: + case 13 /* TemplateMiddle */: + case 14 /* TemplateTail */: + return emitLiteral(node); + // Identifiers + case 69 /* Identifier */: + return emitIdentifier(node); + // Reserved words + case 74 /* ConstKeyword */: + case 77 /* DefaultKeyword */: + case 82 /* ExportKeyword */: + case 103 /* VoidKeyword */: + // Strict mode reserved words + case 110 /* PrivateKeyword */: + case 111 /* ProtectedKeyword */: + case 112 /* PublicKeyword */: + case 113 /* StaticKeyword */: + // Contextual keywords + case 115 /* AbstractKeyword */: + case 117 /* AnyKeyword */: + case 118 /* AsyncKeyword */: + case 120 /* BooleanKeyword */: + case 122 /* DeclareKeyword */: + case 130 /* NumberKeyword */: + case 128 /* ReadonlyKeyword */: + case 132 /* StringKeyword */: + case 133 /* SymbolKeyword */: + case 137 /* GlobalKeyword */: + writeTokenText(kind); + return; + // Parse tree nodes + // Names + case 139 /* QualifiedName */: + return emitQualifiedName(node); + case 140 /* ComputedPropertyName */: + return emitComputedPropertyName(node); + // Signature elements + case 141 /* TypeParameter */: + return emitTypeParameter(node); + case 142 /* Parameter */: + return emitParameter(node); + case 143 /* Decorator */: + return emitDecorator(node); + // Type members + case 144 /* PropertySignature */: + return emitPropertySignature(node); + case 145 /* PropertyDeclaration */: + return emitPropertyDeclaration(node); + case 146 /* MethodSignature */: + return emitMethodSignature(node); + case 147 /* MethodDeclaration */: + return emitMethodDeclaration(node); + case 148 /* Constructor */: + return emitConstructor(node); + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + return emitAccessorDeclaration(node); + case 151 /* CallSignature */: + return emitCallSignature(node); + case 152 /* ConstructSignature */: + return emitConstructSignature(node); + case 153 /* IndexSignature */: + return emitIndexSignature(node); + // Types + case 154 /* TypePredicate */: + return emitTypePredicate(node); + case 155 /* TypeReference */: + return emitTypeReference(node); + case 156 /* FunctionType */: + return emitFunctionType(node); + case 157 /* ConstructorType */: + return emitConstructorType(node); + case 158 /* TypeQuery */: + return emitTypeQuery(node); + case 159 /* TypeLiteral */: + return emitTypeLiteral(node); + case 160 /* ArrayType */: + return emitArrayType(node); + case 161 /* TupleType */: + return emitTupleType(node); + case 162 /* UnionType */: + return emitUnionType(node); + case 163 /* IntersectionType */: + return emitIntersectionType(node); + case 164 /* ParenthesizedType */: + return emitParenthesizedType(node); + case 194 /* ExpressionWithTypeArguments */: + return emitExpressionWithTypeArguments(node); + case 165 /* ThisType */: + return emitThisType(node); + case 166 /* LiteralType */: + return emitLiteralType(node); + // Binding patterns + case 167 /* ObjectBindingPattern */: + return emitObjectBindingPattern(node); + case 168 /* ArrayBindingPattern */: + return emitArrayBindingPattern(node); + case 169 /* BindingElement */: + return emitBindingElement(node); + // Misc + case 197 /* TemplateSpan */: + return emitTemplateSpan(node); + case 198 /* SemicolonClassElement */: + return emitSemicolonClassElement(node); + // Statements + case 199 /* Block */: + return emitBlock(node); + case 200 /* VariableStatement */: + return emitVariableStatement(node); + case 201 /* EmptyStatement */: + return emitEmptyStatement(node); + case 202 /* ExpressionStatement */: + return emitExpressionStatement(node); + case 203 /* IfStatement */: + return emitIfStatement(node); + case 204 /* DoStatement */: + return emitDoStatement(node); + case 205 /* WhileStatement */: + return emitWhileStatement(node); + case 206 /* ForStatement */: + return emitForStatement(node); + case 207 /* ForInStatement */: + return emitForInStatement(node); + case 208 /* ForOfStatement */: + return emitForOfStatement(node); + case 209 /* ContinueStatement */: + return emitContinueStatement(node); + case 210 /* BreakStatement */: + return emitBreakStatement(node); + case 211 /* ReturnStatement */: + return emitReturnStatement(node); + case 212 /* WithStatement */: + return emitWithStatement(node); + case 213 /* SwitchStatement */: + return emitSwitchStatement(node); + case 214 /* LabeledStatement */: + return emitLabeledStatement(node); + case 215 /* ThrowStatement */: + return emitThrowStatement(node); + case 216 /* TryStatement */: + return emitTryStatement(node); + case 217 /* DebuggerStatement */: + return emitDebuggerStatement(node); + // Declarations + case 218 /* VariableDeclaration */: + return emitVariableDeclaration(node); + case 219 /* VariableDeclarationList */: + return emitVariableDeclarationList(node); + case 220 /* FunctionDeclaration */: + return emitFunctionDeclaration(node); + case 221 /* ClassDeclaration */: + return emitClassDeclaration(node); + case 222 /* InterfaceDeclaration */: + return emitInterfaceDeclaration(node); + case 223 /* TypeAliasDeclaration */: + return emitTypeAliasDeclaration(node); + case 224 /* EnumDeclaration */: + return emitEnumDeclaration(node); + case 225 /* ModuleDeclaration */: + return emitModuleDeclaration(node); + case 226 /* ModuleBlock */: + return emitModuleBlock(node); + case 227 /* CaseBlock */: + return emitCaseBlock(node); + case 229 /* ImportEqualsDeclaration */: + return emitImportEqualsDeclaration(node); + case 230 /* ImportDeclaration */: + return emitImportDeclaration(node); + case 231 /* ImportClause */: + return emitImportClause(node); + case 232 /* NamespaceImport */: + return emitNamespaceImport(node); + case 233 /* NamedImports */: + return emitNamedImports(node); + case 234 /* ImportSpecifier */: + return emitImportSpecifier(node); + case 235 /* ExportAssignment */: + return emitExportAssignment(node); + case 236 /* ExportDeclaration */: + return emitExportDeclaration(node); + case 237 /* NamedExports */: + return emitNamedExports(node); + case 238 /* ExportSpecifier */: + return emitExportSpecifier(node); + case 239 /* MissingDeclaration */: + return; + // Module references + case 240 /* ExternalModuleReference */: + return emitExternalModuleReference(node); + // JSX (non-expression) + case 244 /* JsxText */: + return emitJsxText(node); + case 243 /* JsxOpeningElement */: + return emitJsxOpeningElement(node); + case 245 /* JsxClosingElement */: + return emitJsxClosingElement(node); + case 246 /* JsxAttribute */: + return emitJsxAttribute(node); + case 247 /* JsxSpreadAttribute */: + return emitJsxSpreadAttribute(node); + case 248 /* JsxExpression */: + return emitJsxExpression(node); + // Clauses + case 249 /* CaseClause */: + return emitCaseClause(node); + case 250 /* DefaultClause */: + return emitDefaultClause(node); + case 251 /* HeritageClause */: + return emitHeritageClause(node); + case 252 /* CatchClause */: + return emitCatchClause(node); + // Property assignments + case 253 /* PropertyAssignment */: + return emitPropertyAssignment(node); + case 254 /* ShorthandPropertyAssignment */: + return emitShorthandPropertyAssignment(node); + // Enum + case 255 /* EnumMember */: + return emitEnumMember(node); + } + // If the node is an expression, try to emit it as an expression with + // substitution. + if (ts.isExpression(node)) { + return pipelineEmitWithSubstitution(1 /* Expression */, node); } } - function emitNonNullExpression(node) { - emitExpression(node.expression); - write("!"); + /** + * Emits a node in the Expression EmitContext. + * + * NOTE: Do not call this method directly. It is part of the emit pipeline + * and should only be called indirectly from pipelineEmitForContext. + */ + function pipelineEmitInExpressionContext(node) { + var kind = node.kind; + switch (kind) { + // Literals + case 8 /* NumericLiteral */: + return emitNumericLiteral(node); + case 9 /* StringLiteral */: + case 10 /* RegularExpressionLiteral */: + case 11 /* NoSubstitutionTemplateLiteral */: + return emitLiteral(node); + // Identifiers + case 69 /* Identifier */: + return emitIdentifier(node); + // Reserved words + case 84 /* FalseKeyword */: + case 93 /* NullKeyword */: + case 95 /* SuperKeyword */: + case 99 /* TrueKeyword */: + case 97 /* ThisKeyword */: + writeTokenText(kind); + return; + // Expressions + case 170 /* ArrayLiteralExpression */: + return emitArrayLiteralExpression(node); + case 171 /* ObjectLiteralExpression */: + return emitObjectLiteralExpression(node); + case 172 /* PropertyAccessExpression */: + return emitPropertyAccessExpression(node); + case 173 /* ElementAccessExpression */: + return emitElementAccessExpression(node); + case 174 /* CallExpression */: + return emitCallExpression(node); + case 175 /* NewExpression */: + return emitNewExpression(node); + case 176 /* TaggedTemplateExpression */: + return emitTaggedTemplateExpression(node); + case 177 /* TypeAssertionExpression */: + return emitTypeAssertionExpression(node); + case 178 /* ParenthesizedExpression */: + return emitParenthesizedExpression(node); + case 179 /* FunctionExpression */: + return emitFunctionExpression(node); + case 180 /* ArrowFunction */: + return emitArrowFunction(node); + case 181 /* DeleteExpression */: + return emitDeleteExpression(node); + case 182 /* TypeOfExpression */: + return emitTypeOfExpression(node); + case 183 /* VoidExpression */: + return emitVoidExpression(node); + case 184 /* AwaitExpression */: + return emitAwaitExpression(node); + case 185 /* PrefixUnaryExpression */: + return emitPrefixUnaryExpression(node); + case 186 /* PostfixUnaryExpression */: + return emitPostfixUnaryExpression(node); + case 187 /* BinaryExpression */: + return emitBinaryExpression(node); + case 188 /* ConditionalExpression */: + return emitConditionalExpression(node); + case 189 /* TemplateExpression */: + return emitTemplateExpression(node); + case 190 /* YieldExpression */: + return emitYieldExpression(node); + case 191 /* SpreadElementExpression */: + return emitSpreadElementExpression(node); + case 192 /* ClassExpression */: + return emitClassExpression(node); + case 193 /* OmittedExpression */: + return; + case 195 /* AsExpression */: + return emitAsExpression(node); + case 196 /* NonNullExpression */: + return emitNonNullExpression(node); + // JSX + case 241 /* JsxElement */: + return emitJsxElement(node); + case 242 /* JsxSelfClosingElement */: + return emitJsxSelfClosingElement(node); + // Transformation nodes + case 288 /* PartiallyEmittedExpression */: + return emitPartiallyEmittedExpression(node); + } } // - // Misc + // Literals/Pseudo-literals // - function emitTemplateSpan(node) { - emitExpression(node.expression); - emit(node.literal); + // SyntaxKind.NumericLiteral + function emitNumericLiteral(node) { + emitLiteral(node); + if (node.trailingComment) { + write(" /*" + node.trailingComment + "*/"); + } } - // - // Statements - // - function emitBlock(node, format) { - if (isSingleLineEmptyBlock(node)) { - writeToken(15 /* OpenBraceToken */, node.pos, /*contextNode*/ node); - write(" "); - writeToken(16 /* CloseBraceToken */, node.statements.end, /*contextNode*/ node); + // SyntaxKind.StringLiteral + // SyntaxKind.RegularExpressionLiteral + // SyntaxKind.NoSubstitutionTemplateLiteral + // SyntaxKind.TemplateHead + // SyntaxKind.TemplateMiddle + // SyntaxKind.TemplateTail + function emitLiteral(node) { + var text = getLiteralTextOfNode(node); + if ((compilerOptions.sourceMap || compilerOptions.inlineSourceMap) + && (node.kind === 9 /* StringLiteral */ || ts.isTemplateLiteralKind(node.kind))) { + writer.writeLiteral(text); } else { - writeToken(15 /* OpenBraceToken */, node.pos, /*contextNode*/ node); - emitBlockStatements(node); - writeToken(16 /* CloseBraceToken */, node.statements.end, /*contextNode*/ node); + write(text); } } - function emitBlockStatements(node) { - if (node.emitFlags & 32 /* SingleLine */) { - emitList(node, node.statements, 384 /* SingleLineBlockStatements */); + // + // Identifiers + // + function emitIdentifier(node) { + if (ts.getEmitFlags(node) & 16 /* UMDDefine */) { + writeLines(umdHelper); } else { - emitList(node, node.statements, 65 /* MultiLineBlockStatements */); + write(getTextOfNode(node, /*includeTrivia*/ false)); } } - function emitVariableStatement(node) { - emitModifiers(node, node.modifiers); - emit(node.declarationList); - write(";"); - } - function emitEmptyStatement(node) { - write(";"); - } - function emitExpressionStatement(node) { - emitExpression(node.expression); - write(";"); - } - function emitIfStatement(node) { - var openParenPos = writeToken(88 /* IfKeyword */, node.pos, node); - write(" "); - writeToken(17 /* OpenParenToken */, openParenPos, node); - emitExpression(node.expression); - writeToken(18 /* CloseParenToken */, node.expression.end, node); - emitEmbeddedStatement(node.thenStatement); - if (node.elseStatement) { - writeLine(); - writeToken(80 /* ElseKeyword */, node.thenStatement.end, node); - if (node.elseStatement.kind === 203 /* IfStatement */) { - write(" "); - emit(node.elseStatement); - } - else { - emitEmbeddedStatement(node.elseStatement); - } - } + // + // Names + // + function emitQualifiedName(node) { + emitEntityName(node.left); + write("."); + emit(node.right); } - function emitDoStatement(node) { - write("do"); - emitEmbeddedStatement(node.statement); - if (ts.isBlock(node.statement)) { - write(" "); + function emitEntityName(node) { + if (node.kind === 69 /* Identifier */) { + emitExpression(node); } else { - writeLine(); - } - write("while ("); - emitExpression(node.expression); - write(");"); - } - function emitWhileStatement(node) { - write("while ("); - emitExpression(node.expression); - write(")"); - emitEmbeddedStatement(node.statement); - } - function emitForStatement(node) { - var openParenPos = writeToken(86 /* ForKeyword */, node.pos); - write(" "); - writeToken(17 /* OpenParenToken */, openParenPos, /*contextNode*/ node); - emitForBinding(node.initializer); - write(";"); - emitExpressionWithPrefix(" ", node.condition); - write(";"); - emitExpressionWithPrefix(" ", node.incrementor); - write(")"); - emitEmbeddedStatement(node.statement); - } - function emitForInStatement(node) { - var openParenPos = writeToken(86 /* ForKeyword */, node.pos); - write(" "); - writeToken(17 /* OpenParenToken */, openParenPos); - emitForBinding(node.initializer); - write(" in "); - emitExpression(node.expression); - writeToken(18 /* CloseParenToken */, node.expression.end); - emitEmbeddedStatement(node.statement); - } - function emitForOfStatement(node) { - var openParenPos = writeToken(86 /* ForKeyword */, node.pos); - write(" "); - writeToken(17 /* OpenParenToken */, openParenPos); - emitForBinding(node.initializer); - write(" of "); - emitExpression(node.expression); - writeToken(18 /* CloseParenToken */, node.expression.end); - emitEmbeddedStatement(node.statement); - } - function emitForBinding(node) { - if (node !== undefined) { - if (node.kind === 219 /* VariableDeclarationList */) { - emit(node); - } - else { - emitExpression(node); - } + emit(node); } } - function emitContinueStatement(node) { - writeToken(75 /* ContinueKeyword */, node.pos); - emitWithPrefix(" ", node.label); - write(";"); - } - function emitBreakStatement(node) { - writeToken(70 /* BreakKeyword */, node.pos); - emitWithPrefix(" ", node.label); - write(";"); - } - function emitReturnStatement(node) { - writeToken(94 /* ReturnKeyword */, node.pos, /*contextNode*/ node); - emitExpressionWithPrefix(" ", node.expression); - write(";"); - } - function emitWithStatement(node) { - write("with ("); - emitExpression(node.expression); - write(")"); - emitEmbeddedStatement(node.statement); - } - function emitSwitchStatement(node) { - var openParenPos = writeToken(96 /* SwitchKeyword */, node.pos); - write(" "); - writeToken(17 /* OpenParenToken */, openParenPos); + function emitComputedPropertyName(node) { + write("["); emitExpression(node.expression); - writeToken(18 /* CloseParenToken */, node.expression.end); - write(" "); - emit(node.caseBlock); - } - function emitLabeledStatement(node) { - emit(node.label); - write(": "); - emit(node.statement); - } - function emitThrowStatement(node) { - write("throw"); - emitExpressionWithPrefix(" ", node.expression); - write(";"); - } - function emitTryStatement(node) { - write("try "); - emit(node.tryBlock); - emit(node.catchClause); - if (node.finallyBlock) { - writeLine(); - write("finally "); - emit(node.finallyBlock); - } - } - function emitDebuggerStatement(node) { - writeToken(76 /* DebuggerKeyword */, node.pos); - write(";"); + write("]"); } // - // Declarations + // Signature elements // - function emitVariableDeclaration(node) { + function emitTypeParameter(node) { emit(node.name); - emitExpressionWithPrefix(" = ", node.initializer); - } - function emitVariableDeclarationList(node) { - write(ts.isLet(node) ? "let " : ts.isConst(node) ? "const " : "var "); - emitList(node, node.declarations, 272 /* VariableDeclarationList */); - } - function emitFunctionDeclaration(node) { - emitFunctionDeclarationOrExpression(node); + emitWithPrefix(" extends ", node.constraint); } - function emitFunctionDeclarationOrExpression(node) { + function emitParameter(node) { emitDecorators(node, node.decorators); emitModifiers(node, node.modifiers); - write(node.asteriskToken ? "function* " : "function "); - emitIdentifierName(node.name); - emitSignatureAndBody(node, emitSignatureHead); - } - function emitSignatureAndBody(node, emitSignatureHead) { - var body = node.body; - if (body) { - if (ts.isBlock(body)) { - var indentedFlag = node.emitFlags & 524288 /* Indented */; - if (indentedFlag) { - increaseIndent(); - } - if (node.emitFlags & 4194304 /* ReuseTempVariableScope */) { - emitSignatureHead(node); - emitBlockFunctionBody(node, body); - } - else { - var savedTempFlags = tempFlags; - tempFlags = 0; - emitSignatureHead(node); - emitBlockFunctionBody(node, body); - tempFlags = savedTempFlags; - } - if (indentedFlag) { - decreaseIndent(); - } - } - else { - emitSignatureHead(node); - write(" "); - emitExpression(body); - } - } - else { - emitSignatureHead(node); - write(";"); - } - } - function emitSignatureHead(node) { - emitTypeParameters(node, node.typeParameters); - emitParameters(node, node.parameters); + writeIfPresent(node.dotDotDotToken, "..."); + emit(node.name); + writeIfPresent(node.questionToken, "?"); + emitExpressionWithPrefix(" = ", node.initializer); emitWithPrefix(": ", node.type); } - function shouldEmitBlockFunctionBodyOnSingleLine(parentNode, body) { - // We must emit a function body as a single-line body in the following case: - // * The body has NodeEmitFlags.SingleLine specified. - // We must emit a function body as a multi-line body in the following cases: - // * The body is explicitly marked as multi-line. - // * A non-synthesized body's start and end position are on different lines. - // * Any statement in the body starts on a new line. - if (body.emitFlags & 32 /* SingleLine */) { - return true; - } - if (body.multiLine) { - return false; - } - if (!ts.nodeIsSynthesized(body) && !ts.rangeIsOnSingleLine(body, currentSourceFile)) { - return false; - } - if (shouldWriteLeadingLineTerminator(body, body.statements, 2 /* PreserveLines */) - || shouldWriteClosingLineTerminator(body, body.statements, 2 /* PreserveLines */)) { - return false; - } - var previousStatement; - for (var _a = 0, _b = body.statements; _a < _b.length; _a++) { - var statement = _b[_a]; - if (shouldWriteSeparatingLineTerminator(previousStatement, statement, 2 /* PreserveLines */)) { - return false; - } - previousStatement = statement; - } - return true; - } - function emitBlockFunctionBody(parentNode, body) { - write(" {"); - increaseIndent(); - emitBodyWithDetachedComments(body, body.statements, shouldEmitBlockFunctionBodyOnSingleLine(parentNode, body) - ? emitBlockFunctionBodyOnSingleLine - : emitBlockFunctionBodyWorker); - decreaseIndent(); - writeToken(16 /* CloseBraceToken */, body.statements.end, body); - } - function emitBlockFunctionBodyOnSingleLine(body) { - emitBlockFunctionBodyWorker(body, /*emitBlockFunctionBodyOnSingleLine*/ true); - } - function emitBlockFunctionBodyWorker(body, emitBlockFunctionBodyOnSingleLine) { - // Emit all the prologue directives (like "use strict"). - var statementOffset = emitPrologueDirectives(body.statements, /*startWithNewLine*/ true); - var helpersEmitted = emitHelpers(body); - if (statementOffset === 0 && !helpersEmitted && emitBlockFunctionBodyOnSingleLine) { - decreaseIndent(); - emitList(body, body.statements, 384 /* SingleLineFunctionBodyStatements */); - increaseIndent(); - } - else { - emitList(body, body.statements, 1 /* MultiLineFunctionBodyStatements */, statementOffset); - } - } - function emitClassDeclaration(node) { - emitClassDeclarationOrExpression(node); - } - function emitClassDeclarationOrExpression(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - write("class"); - emitNodeWithPrefix(" ", node.name, emitIdentifierName); - var indentedFlag = node.emitFlags & 524288 /* Indented */; - if (indentedFlag) { - increaseIndent(); - } - emitTypeParameters(node, node.typeParameters); - emitList(node, node.heritageClauses, 256 /* ClassHeritageClauses */); - var savedTempFlags = tempFlags; - tempFlags = 0; - write(" {"); - emitList(node, node.members, 65 /* ClassMembers */); - write("}"); - if (indentedFlag) { - decreaseIndent(); - } - tempFlags = savedTempFlags; - } - function emitInterfaceDeclaration(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - write("interface "); - emit(node.name); - emitTypeParameters(node, node.typeParameters); - emitList(node, node.heritageClauses, 256 /* HeritageClauses */); - write(" {"); - emitList(node, node.members, 65 /* InterfaceMembers */); - write("}"); + function emitDecorator(decorator) { + write("@"); + emitExpression(decorator.expression); } - function emitTypeAliasDeclaration(node) { + // + // Type members + // + function emitPropertySignature(node) { emitDecorators(node, node.decorators); emitModifiers(node, node.modifiers); - write("type "); - emit(node.name); - emitTypeParameters(node, node.typeParameters); - write(" = "); - emit(node.type); - write(";"); - } - function emitEnumDeclaration(node) { - emitModifiers(node, node.modifiers); - write("enum "); - emit(node.name); - var savedTempFlags = tempFlags; - tempFlags = 0; - write(" {"); - emitList(node, node.members, 81 /* EnumMembers */); - write("}"); - tempFlags = savedTempFlags; - } - function emitModuleDeclaration(node) { - emitModifiers(node, node.modifiers); - write(node.flags & 16 /* Namespace */ ? "namespace " : "module "); emit(node.name); - var body = node.body; - while (body.kind === 225 /* ModuleDeclaration */) { - write("."); - emit(body.name); - body = body.body; - } - write(" "); - emit(body); - } - function emitModuleBlock(node) { - if (isSingleLineEmptyBlock(node)) { - write("{ }"); - } - else { - var savedTempFlags = tempFlags; - tempFlags = 0; - write("{"); - increaseIndent(); - emitBlockStatements(node); - write("}"); - tempFlags = savedTempFlags; - } - } - function emitCaseBlock(node) { - writeToken(15 /* OpenBraceToken */, node.pos); - emitList(node, node.clauses, 65 /* CaseBlockClauses */); - writeToken(16 /* CloseBraceToken */, node.clauses.end); + writeIfPresent(node.questionToken, "?"); + emitWithPrefix(": ", node.type); + write(";"); } - function emitImportEqualsDeclaration(node) { + function emitPropertyDeclaration(node) { + emitDecorators(node, node.decorators); emitModifiers(node, node.modifiers); - write("import "); emit(node.name); - write(" = "); - emitModuleReference(node.moduleReference); + emitWithPrefix(": ", node.type); + emitExpressionWithPrefix(" = ", node.initializer); write(";"); } - function emitModuleReference(node) { - if (node.kind === 69 /* Identifier */) { - emitExpression(node); - } - else { - emit(node); - } - } - function emitImportDeclaration(node) { + function emitMethodSignature(node) { + emitDecorators(node, node.decorators); emitModifiers(node, node.modifiers); - write("import "); - if (node.importClause) { - emit(node.importClause); - write(" from "); - } - emitExpression(node.moduleSpecifier); + emit(node.name); + writeIfPresent(node.questionToken, "?"); + emitTypeParameters(node, node.typeParameters); + emitParameters(node, node.parameters); + emitWithPrefix(": ", node.type); write(";"); } - function emitImportClause(node) { + function emitMethodDeclaration(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + writeIfPresent(node.asteriskToken, "*"); emit(node.name); - if (node.name && node.namedBindings) { - write(", "); - } - emit(node.namedBindings); + emitSignatureAndBody(node, emitSignatureHead); } - function emitNamespaceImport(node) { - write("* as "); + function emitConstructor(node) { + emitModifiers(node, node.modifiers); + write("constructor"); + emitSignatureAndBody(node, emitSignatureHead); + } + function emitAccessorDeclaration(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + write(node.kind === 149 /* GetAccessor */ ? "get " : "set "); emit(node.name); + emitSignatureAndBody(node, emitSignatureHead); } - function emitNamedImports(node) { - emitNamedImportsOrExports(node); + function emitCallSignature(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + emitTypeParameters(node, node.typeParameters); + emitParameters(node, node.parameters); + emitWithPrefix(": ", node.type); + write(";"); } - function emitImportSpecifier(node) { - emitImportOrExportSpecifier(node); + function emitConstructSignature(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + write("new "); + emitTypeParameters(node, node.typeParameters); + emitParameters(node, node.parameters); + emitWithPrefix(": ", node.type); + write(";"); } - function emitExportAssignment(node) { - write(node.isExportEquals ? "export = " : "export default "); - emitExpression(node.expression); + function emitIndexSignature(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + emitParametersForIndexSignature(node, node.parameters); + emitWithPrefix(": ", node.type); write(";"); } - function emitExportDeclaration(node) { - write("export "); - if (node.exportClause) { - emit(node.exportClause); - } - else { - write("*"); - } - if (node.moduleSpecifier) { - write(" from "); - emitExpression(node.moduleSpecifier); - } + function emitSemicolonClassElement(node) { write(";"); } - function emitNamedExports(node) { - emitNamedImportsOrExports(node); + // + // Types + // + function emitTypePredicate(node) { + emit(node.parameterName); + write(" is "); + emit(node.type); } - function emitExportSpecifier(node) { - emitImportOrExportSpecifier(node); + function emitTypeReference(node) { + emit(node.typeName); + emitTypeArguments(node, node.typeArguments); } - function emitNamedImportsOrExports(node) { - write("{"); - emitList(node, node.elements, 432 /* NamedImportsOrExportsElements */); - write("}"); + function emitFunctionType(node) { + emitTypeParameters(node, node.typeParameters); + emitParametersForArrow(node, node.parameters); + write(" => "); + emit(node.type); } - function emitImportOrExportSpecifier(node) { - if (node.propertyName) { - emit(node.propertyName); - write(" as "); - } - emit(node.name); + function emitConstructorType(node) { + write("new "); + emitTypeParameters(node, node.typeParameters); + emitParametersForArrow(node, node.parameters); + write(" => "); + emit(node.type); } - // - // Module references - // - function emitExternalModuleReference(node) { - write("require("); - emitExpression(node.expression); - write(")"); + function emitTypeQuery(node) { + write("typeof "); + emit(node.exprName); } - // - // JSX - // - function emitJsxElement(node) { - emit(node.openingElement); - emitList(node, node.children, 131072 /* JsxElementChildren */); - emit(node.closingElement); + function emitTypeLiteral(node) { + write("{"); + emitList(node, node.members, 65 /* TypeLiteralMembers */); + write("}"); } - function emitJsxSelfClosingElement(node) { - write("<"); - emitJsxTagName(node.tagName); - write(" "); - emitList(node, node.attributes, 131328 /* JsxElementAttributes */); - write("/>"); + function emitArrayType(node) { + emit(node.elementType); + write("[]"); } - function emitJsxOpeningElement(node) { - write("<"); - emitJsxTagName(node.tagName); - writeIfAny(node.attributes, " "); - emitList(node, node.attributes, 131328 /* JsxElementAttributes */); - write(">"); + function emitTupleType(node) { + write("["); + emitList(node, node.elementTypes, 336 /* TupleTypeElements */); + write("]"); } - function emitJsxText(node) { - writer.writeLiteral(getTextOfNode(node, /*includeTrivia*/ true)); + function emitUnionType(node) { + emitList(node, node.types, 260 /* UnionTypeConstituents */); } - function emitJsxClosingElement(node) { - write(""); + function emitIntersectionType(node) { + emitList(node, node.types, 264 /* IntersectionTypeConstituents */); } - function emitJsxAttribute(node) { - emit(node.name); - emitWithPrefix("=", node.initializer); + function emitParenthesizedType(node) { + write("("); + emit(node.type); + write(")"); } - function emitJsxSpreadAttribute(node) { - write("{..."); - emitExpression(node.expression); - write("}"); + function emitThisType(node) { + write("this"); } - function emitJsxExpression(node) { - if (node.expression) { + function emitLiteralType(node) { + emitExpression(node.literal); + } + // + // Binding patterns + // + function emitObjectBindingPattern(node) { + var elements = node.elements; + if (elements.length === 0) { + write("{}"); + } + else { write("{"); - emitExpression(node.expression); + emitList(node, elements, 432 /* ObjectBindingPatternElements */); write("}"); } } - function emitJsxTagName(node) { - if (node.kind === 69 /* Identifier */) { - emitExpression(node); + function emitArrayBindingPattern(node) { + var elements = node.elements; + if (elements.length === 0) { + write("[]"); } else { - emit(node); + write("["); + emitList(node, node.elements, 304 /* ArrayBindingPatternElements */); + write("]"); } } + function emitBindingElement(node) { + emitWithSuffix(node.propertyName, ": "); + writeIfPresent(node.dotDotDotToken, "..."); + emit(node.name); + emitExpressionWithPrefix(" = ", node.initializer); + } // - // Clauses + // Expressions // - function emitCaseClause(node) { - write("case "); - emitExpression(node.expression); - write(":"); - emitCaseOrDefaultClauseStatements(node, node.statements); - } - function emitDefaultClause(node) { - write("default:"); - emitCaseOrDefaultClauseStatements(node, node.statements); + function emitArrayLiteralExpression(node) { + var elements = node.elements; + if (elements.length === 0) { + write("[]"); + } + else { + var preferNewLine = node.multiLine ? 32768 /* PreferNewLine */ : 0 /* None */; + emitExpressionList(node, elements, 4466 /* ArrayLiteralExpressionElements */ | preferNewLine); + } } - function emitCaseOrDefaultClauseStatements(parentNode, statements) { - var emitAsSingleStatement = statements.length === 1 && - ( - // treat synthesized nodes as located on the same line for emit purposes - ts.nodeIsSynthesized(parentNode) || - ts.nodeIsSynthesized(statements[0]) || - ts.rangeStartPositionsAreOnSameLine(parentNode, statements[0], currentSourceFile)); - if (emitAsSingleStatement) { - write(" "); - emit(statements[0]); + function emitObjectLiteralExpression(node) { + var properties = node.properties; + if (properties.length === 0) { + write("{}"); } else { - emitList(parentNode, statements, 81985 /* CaseOrDefaultClauseStatements */); + var indentedFlag = ts.getEmitFlags(node) & 524288 /* Indented */; + if (indentedFlag) { + increaseIndent(); + } + var preferNewLine = node.multiLine ? 32768 /* PreferNewLine */ : 0 /* None */; + var allowTrailingComma = languageVersion >= 1 /* ES5 */ ? 32 /* AllowTrailingComma */ : 0 /* None */; + emitList(node, properties, 978 /* ObjectLiteralExpressionProperties */ | allowTrailingComma | preferNewLine); + if (indentedFlag) { + decreaseIndent(); + } } } - function emitHeritageClause(node) { - write(" "); - writeTokenText(node.token); - write(" "); - emitList(node, node.types, 272 /* HeritageClauseTypes */); + function emitPropertyAccessExpression(node) { + var indentBeforeDot = false; + var indentAfterDot = false; + if (!(ts.getEmitFlags(node) & 1048576 /* NoIndentation */)) { + var dotRangeStart = node.expression.end; + var dotRangeEnd = ts.skipTrivia(currentText, node.expression.end) + 1; + var dotToken = { kind: 21 /* DotToken */, pos: dotRangeStart, end: dotRangeEnd }; + indentBeforeDot = needsIndentation(node, node.expression, dotToken); + indentAfterDot = needsIndentation(node, dotToken, node.name); + } + emitExpression(node.expression); + increaseIndentIf(indentBeforeDot); + var shouldEmitDotDot = !indentBeforeDot && needsDotDotForPropertyAccess(node.expression); + write(shouldEmitDotDot ? ".." : "."); + increaseIndentIf(indentAfterDot); + emit(node.name); + decreaseIndentIf(indentBeforeDot, indentAfterDot); } - function emitCatchClause(node) { - writeLine(); - var openParenPos = writeToken(72 /* CatchKeyword */, node.pos); - write(" "); - writeToken(17 /* OpenParenToken */, openParenPos); - emit(node.variableDeclaration); - writeToken(18 /* CloseParenToken */, node.variableDeclaration ? node.variableDeclaration.end : openParenPos); + // 1..toString is a valid property access, emit a dot after the literal + // Also emit a dot if expression is a integer const enum value - it will appear in generated code as numeric literal + function needsDotDotForPropertyAccess(expression) { + if (expression.kind === 8 /* NumericLiteral */) { + // check if numeric literal was originally written with a dot + var text = getLiteralTextOfNode(expression); + return text.indexOf(ts.tokenToString(21 /* DotToken */)) < 0; + } + else if (ts.isPropertyAccessExpression(expression) || ts.isElementAccessExpression(expression)) { + // check if constant enum value is integer + var constantValue = ts.getConstantValue(expression); + // isFinite handles cases when constantValue is undefined + return isFinite(constantValue) + && Math.floor(constantValue) === constantValue + && compilerOptions.removeComments; + } + } + function emitElementAccessExpression(node) { + emitExpression(node.expression); + write("["); + emitExpression(node.argumentExpression); + write("]"); + } + function emitCallExpression(node) { + emitExpression(node.expression); + emitExpressionList(node, node.arguments, 1296 /* CallExpressionArguments */); + } + function emitNewExpression(node) { + write("new "); + emitExpression(node.expression); + emitExpressionList(node, node.arguments, 9488 /* NewExpressionArguments */); + } + function emitTaggedTemplateExpression(node) { + emitExpression(node.tag); write(" "); - emit(node.block); + emitExpression(node.template); } - // - // Property assignments - // - function emitPropertyAssignment(node) { - emit(node.name); - write(": "); - // This is to ensure that we emit comment in the following case: - // For example: - // obj = { - // id: /*comment1*/ ()=>void - // } - // "comment1" is not considered to be leading comment for node.initializer - // but rather a trailing comment on the previous node. - var initializer = node.initializer; - if (!shouldSkipLeadingCommentsForNode(initializer)) { - var commentRange = initializer.commentRange || initializer; - emitTrailingCommentsOfPosition(commentRange.pos); + function emitTypeAssertionExpression(node) { + if (node.type) { + write("<"); + emit(node.type); + write(">"); } - emitExpression(initializer); + emitExpression(node.expression); } - function emitShorthandPropertyAssignment(node) { - emit(node.name); - if (node.objectAssignmentInitializer) { - write(" = "); - emitExpression(node.objectAssignmentInitializer); - } + function emitParenthesizedExpression(node) { + write("("); + emitExpression(node.expression); + write(")"); } - // - // Enum - // - function emitEnumMember(node) { - emit(node.name); - emitExpressionWithPrefix(" = ", node.initializer); + function emitFunctionExpression(node) { + emitFunctionDeclarationOrExpression(node); } - // - // Top-level nodes - // - function emitSourceFile(node) { - writeLine(); - emitShebang(); - emitBodyWithDetachedComments(node, node.statements, emitSourceFileWorker); + function emitArrowFunction(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + emitSignatureAndBody(node, emitArrowFunctionHead); } - function emitSourceFileWorker(node) { - var statements = node.statements; - var statementOffset = emitPrologueDirectives(statements); - var savedTempFlags = tempFlags; - tempFlags = 0; - emitHelpers(node); - emitList(node, statements, 1 /* MultiLine */, statementOffset); - tempFlags = savedTempFlags; + function emitArrowFunctionHead(node) { + emitTypeParameters(node, node.typeParameters); + emitParametersForArrow(node, node.parameters); + emitWithPrefix(": ", node.type); + write(" =>"); } - // Transformation nodes - function emitPartiallyEmittedExpression(node) { + function emitDeleteExpression(node) { + write("delete "); emitExpression(node.expression); } - /** - * Emits any prologue directives at the start of a Statement list, returning the - * number of prologue directives written to the output. - */ - function emitPrologueDirectives(statements, startWithNewLine) { - for (var i = 0; i < statements.length; i++) { - if (ts.isPrologueDirective(statements[i])) { - if (startWithNewLine || i > 0) { - writeLine(); - } - emit(statements[i]); - } - else { - // return index of the first non prologue directive - return i; - } - } - return statements.length; + function emitTypeOfExpression(node) { + write("typeof "); + emitExpression(node.expression); } - function emitHelpers(node) { - var emitFlags = node.emitFlags; - var helpersEmitted = false; - if (emitFlags & 1 /* EmitEmitHelpers */) { - helpersEmitted = emitEmitHelpers(currentSourceFile); - } - if (emitFlags & 2 /* EmitExportStar */) { - writeLines(exportStarHelper); - helpersEmitted = true; - } - if (emitFlags & 4 /* EmitSuperHelper */) { - writeLines(superHelper); - helpersEmitted = true; - } - if (emitFlags & 8 /* EmitAdvancedSuperHelper */) { - writeLines(advancedSuperHelper); - helpersEmitted = true; + function emitVoidExpression(node) { + write("void "); + emitExpression(node.expression); + } + function emitAwaitExpression(node) { + write("await "); + emitExpression(node.expression); + } + function emitPrefixUnaryExpression(node) { + writeTokenText(node.operator); + if (shouldEmitWhitespaceBeforeOperand(node)) { + write(" "); } - return helpersEmitted; + emitExpression(node.operand); + } + function shouldEmitWhitespaceBeforeOperand(node) { + // In some cases, we need to emit a space between the operator and the operand. One obvious case + // is when the operator is an identifier, like delete or typeof. We also need to do this for plus + // and minus expressions in certain cases. Specifically, consider the following two cases (parens + // are just for clarity of exposition, and not part of the source code): + // + // (+(+1)) + // (+(++1)) + // + // We need to emit a space in both cases. In the first case, the absence of a space will make + // the resulting expression a prefix increment operation. And in the second, it will make the resulting + // expression a prefix increment whose operand is a plus expression - (++(+x)) + // The same is true of minus of course. + var operand = node.operand; + return operand.kind === 185 /* PrefixUnaryExpression */ + && ((node.operator === 35 /* PlusToken */ && (operand.operator === 35 /* PlusToken */ || operand.operator === 41 /* PlusPlusToken */)) + || (node.operator === 36 /* MinusToken */ && (operand.operator === 36 /* MinusToken */ || operand.operator === 42 /* MinusMinusToken */))); + } + function emitPostfixUnaryExpression(node) { + emitExpression(node.operand); + writeTokenText(node.operator); + } + function emitBinaryExpression(node) { + var isCommaOperator = node.operatorToken.kind !== 24 /* CommaToken */; + var indentBeforeOperator = needsIndentation(node, node.left, node.operatorToken); + var indentAfterOperator = needsIndentation(node, node.operatorToken, node.right); + emitExpression(node.left); + increaseIndentIf(indentBeforeOperator, isCommaOperator ? " " : undefined); + writeTokenText(node.operatorToken.kind); + increaseIndentIf(indentAfterOperator, " "); + emitExpression(node.right); + decreaseIndentIf(indentBeforeOperator, indentAfterOperator); + } + function emitConditionalExpression(node) { + var indentBeforeQuestion = needsIndentation(node, node.condition, node.questionToken); + var indentAfterQuestion = needsIndentation(node, node.questionToken, node.whenTrue); + var indentBeforeColon = needsIndentation(node, node.whenTrue, node.colonToken); + var indentAfterColon = needsIndentation(node, node.colonToken, node.whenFalse); + emitExpression(node.condition); + increaseIndentIf(indentBeforeQuestion, " "); + write("?"); + increaseIndentIf(indentAfterQuestion, " "); + emitExpression(node.whenTrue); + decreaseIndentIf(indentBeforeQuestion, indentAfterQuestion); + increaseIndentIf(indentBeforeColon, " "); + write(":"); + increaseIndentIf(indentAfterColon, " "); + emitExpression(node.whenFalse); + decreaseIndentIf(indentBeforeColon, indentAfterColon); + } + function emitTemplateExpression(node) { + emit(node.head); + emitList(node, node.templateSpans, 131072 /* TemplateExpressionSpans */); + } + function emitYieldExpression(node) { + write(node.asteriskToken ? "yield*" : "yield"); + emitExpressionWithPrefix(" ", node.expression); + } + function emitSpreadElementExpression(node) { + write("..."); + emitExpression(node.expression); + } + function emitClassExpression(node) { + emitClassDeclarationOrExpression(node); } - function emitEmitHelpers(node) { - // Only emit helpers if the user did not say otherwise. - if (compilerOptions.noEmitHelpers) { - return false; - } - // Don't emit helpers if we can import them. - if (compilerOptions.importHelpers - && (ts.isExternalModule(node) || compilerOptions.isolatedModules)) { - return false; - } - var helpersEmitted = false; - // Only Emit __extends function when target ES5. - // For target ES6 and above, we can emit classDeclaration as is. - if ((languageVersion < 2 /* ES6 */) && (!extendsEmitted && node.flags & 1024 /* HasClassExtends */)) { - writeLines(extendsHelper); - extendsEmitted = true; - helpersEmitted = true; - } - if (compilerOptions.jsx !== 1 /* Preserve */ && !assignEmitted && (node.flags & 16384 /* HasJsxSpreadAttributes */)) { - writeLines(assignHelper); - assignEmitted = true; - } - if (!decorateEmitted && node.flags & 2048 /* HasDecorators */) { - writeLines(decorateHelper); - if (compilerOptions.emitDecoratorMetadata) { - writeLines(metadataHelper); - } - decorateEmitted = true; - helpersEmitted = true; - } - if (!paramEmitted && node.flags & 4096 /* HasParamDecorators */) { - writeLines(paramHelper); - paramEmitted = true; - helpersEmitted = true; - } - if (!awaiterEmitted && node.flags & 8192 /* HasAsyncFunctions */) { - writeLines(awaiterHelper); - if (languageVersion < 2 /* ES6 */) { - writeLines(generatorHelper); - } - awaiterEmitted = true; - helpersEmitted = true; - } - if (helpersEmitted) { - writeLine(); - } - return helpersEmitted; + function emitExpressionWithTypeArguments(node) { + emitExpression(node.expression); + emitTypeArguments(node, node.typeArguments); } - function writeLines(text) { - var lines = text.split(/\r\n|\r|\n/g); - for (var i = 0; i < lines.length; i++) { - var line = lines[i]; - if (line.length) { - if (i > 0) { - writeLine(); - } - write(line); - } + function emitAsExpression(node) { + emitExpression(node.expression); + if (node.type) { + write(" as "); + emit(node.type); } } + function emitNonNullExpression(node) { + emitExpression(node.expression); + write("!"); + } // - // Helpers + // Misc // - function emitShebang() { - var shebang = ts.getShebang(currentText); - if (shebang) { - write(shebang); - writeLine(); - } + function emitTemplateSpan(node) { + emitExpression(node.expression); + emit(node.literal); } - function emitModifiers(node, modifiers) { - if (modifiers && modifiers.length) { - emitList(node, modifiers, 256 /* Modifiers */); + // + // Statements + // + function emitBlock(node, format) { + if (isSingleLineEmptyBlock(node)) { + writeToken(15 /* OpenBraceToken */, node.pos, /*contextNode*/ node); write(" "); + writeToken(16 /* CloseBraceToken */, node.statements.end, /*contextNode*/ node); + } + else { + writeToken(15 /* OpenBraceToken */, node.pos, /*contextNode*/ node); + emitBlockStatements(node); + writeToken(16 /* CloseBraceToken */, node.statements.end, /*contextNode*/ node); } } - function emitWithPrefix(prefix, node) { - emitNodeWithPrefix(prefix, node, emit); + function emitBlockStatements(node) { + if (ts.getEmitFlags(node) & 32 /* SingleLine */) { + emitList(node, node.statements, 384 /* SingleLineBlockStatements */); + } + else { + emitList(node, node.statements, 65 /* MultiLineBlockStatements */); + } } - function emitExpressionWithPrefix(prefix, node) { - emitNodeWithPrefix(prefix, node, emitExpression); + function emitVariableStatement(node) { + emitModifiers(node, node.modifiers); + emit(node.declarationList); + write(";"); } - function emitNodeWithPrefix(prefix, node, emit) { - if (node) { - write(prefix); - emit(node); - } + function emitEmptyStatement(node) { + write(";"); } - function emitWithSuffix(node, suffix) { - if (node) { - emit(node); - write(suffix); - } + function emitExpressionStatement(node) { + emitExpression(node.expression); + write(";"); } - function tryEmitSubstitute(node, emitNode, isExpression) { - if (isSubstitutionEnabled(node) && (node.emitFlags & 128 /* NoSubstitution */) === 0) { - var substitute = onSubstituteNode(node, isExpression); - if (substitute !== node) { - substitute.emitFlags |= 128 /* NoSubstitution */; - emitNode(substitute); - return true; + function emitIfStatement(node) { + var openParenPos = writeToken(88 /* IfKeyword */, node.pos, node); + write(" "); + writeToken(17 /* OpenParenToken */, openParenPos, node); + emitExpression(node.expression); + writeToken(18 /* CloseParenToken */, node.expression.end, node); + emitEmbeddedStatement(node.thenStatement); + if (node.elseStatement) { + writeLine(); + writeToken(80 /* ElseKeyword */, node.thenStatement.end, node); + if (node.elseStatement.kind === 203 /* IfStatement */) { + write(" "); + emit(node.elseStatement); } - } - return false; - } - function tryEmitConstantValue(node) { - var constantValue = tryGetConstEnumValue(node); - if (constantValue !== undefined) { - write(String(constantValue)); - if (!compilerOptions.removeComments) { - var propertyName = ts.isPropertyAccessExpression(node) - ? ts.declarationNameToString(node.name) - : getTextOfNode(node.argumentExpression); - write(" /* " + propertyName + " */"); + else { + emitEmbeddedStatement(node.elseStatement); } - return true; } - return false; } - function emitEmbeddedStatement(node) { - if (ts.isBlock(node)) { + function emitDoStatement(node) { + write("do"); + emitEmbeddedStatement(node.statement); + if (ts.isBlock(node.statement)) { write(" "); - emit(node); } else { writeLine(); - increaseIndent(); - emit(node); - decreaseIndent(); } + write("while ("); + emitExpression(node.expression); + write(");"); } - function emitDecorators(parentNode, decorators) { - emitList(parentNode, decorators, 24577 /* Decorators */); + function emitWhileStatement(node) { + write("while ("); + emitExpression(node.expression); + write(")"); + emitEmbeddedStatement(node.statement); } - function emitTypeArguments(parentNode, typeArguments) { - emitList(parentNode, typeArguments, 26960 /* TypeArguments */); + function emitForStatement(node) { + var openParenPos = writeToken(86 /* ForKeyword */, node.pos); + write(" "); + writeToken(17 /* OpenParenToken */, openParenPos, /*contextNode*/ node); + emitForBinding(node.initializer); + write(";"); + emitExpressionWithPrefix(" ", node.condition); + write(";"); + emitExpressionWithPrefix(" ", node.incrementor); + write(")"); + emitEmbeddedStatement(node.statement); } - function emitTypeParameters(parentNode, typeParameters) { - emitList(parentNode, typeParameters, 26960 /* TypeParameters */); + function emitForInStatement(node) { + var openParenPos = writeToken(86 /* ForKeyword */, node.pos); + write(" "); + writeToken(17 /* OpenParenToken */, openParenPos); + emitForBinding(node.initializer); + write(" in "); + emitExpression(node.expression); + writeToken(18 /* CloseParenToken */, node.expression.end); + emitEmbeddedStatement(node.statement); } - function emitParameters(parentNode, parameters) { - emitList(parentNode, parameters, 1360 /* Parameters */); + function emitForOfStatement(node) { + var openParenPos = writeToken(86 /* ForKeyword */, node.pos); + write(" "); + writeToken(17 /* OpenParenToken */, openParenPos); + emitForBinding(node.initializer); + write(" of "); + emitExpression(node.expression); + writeToken(18 /* CloseParenToken */, node.expression.end); + emitEmbeddedStatement(node.statement); } - function emitParametersForArrow(parentNode, parameters) { - if (parameters && - parameters.length === 1 && - parameters[0].type === undefined && - parameters[0].pos === parentNode.pos) { - emit(parameters[0]); - } - else { - emitParameters(parentNode, parameters); + function emitForBinding(node) { + if (node !== undefined) { + if (node.kind === 219 /* VariableDeclarationList */) { + emit(node); + } + else { + emitExpression(node); + } } } - function emitParametersForIndexSignature(parentNode, parameters) { - emitList(parentNode, parameters, 4432 /* IndexSignatureParameters */); + function emitContinueStatement(node) { + writeToken(75 /* ContinueKeyword */, node.pos); + emitWithPrefix(" ", node.label); + write(";"); } - function emitList(parentNode, children, format, start, count) { - emitNodeList(emit, parentNode, children, format, start, count); + function emitBreakStatement(node) { + writeToken(70 /* BreakKeyword */, node.pos); + emitWithPrefix(" ", node.label); + write(";"); } - function emitExpressionList(parentNode, children, format, start, count) { - emitNodeList(emitExpression, parentNode, children, format, start, count); + function emitReturnStatement(node) { + writeToken(94 /* ReturnKeyword */, node.pos, /*contextNode*/ node); + emitExpressionWithPrefix(" ", node.expression); + write(";"); } - function emitNodeList(emit, parentNode, children, format, start, count) { - if (start === void 0) { start = 0; } - if (count === void 0) { count = children ? children.length - start : 0; } - var isUndefined = children === undefined; - if (isUndefined && format & 8192 /* OptionalIfUndefined */) { - return; - } - var isEmpty = isUndefined || children.length === 0 || start >= children.length || count === 0; - if (isEmpty && format & 16384 /* OptionalIfEmpty */) { - return; - } - if (format & 7680 /* BracketsMask */) { - write(getOpeningBracket(format)); - } - if (isEmpty) { - // Write a line terminator if the parent node was multi-line - if (format & 1 /* MultiLine */) { - writeLine(); - } - else if (format & 128 /* SpaceBetweenBraces */) { - write(" "); - } + function emitWithStatement(node) { + write("with ("); + emitExpression(node.expression); + write(")"); + emitEmbeddedStatement(node.statement); + } + function emitSwitchStatement(node) { + var openParenPos = writeToken(96 /* SwitchKeyword */, node.pos); + write(" "); + writeToken(17 /* OpenParenToken */, openParenPos); + emitExpression(node.expression); + writeToken(18 /* CloseParenToken */, node.expression.end); + write(" "); + emit(node.caseBlock); + } + function emitLabeledStatement(node) { + emit(node.label); + write(": "); + emit(node.statement); + } + function emitThrowStatement(node) { + write("throw"); + emitExpressionWithPrefix(" ", node.expression); + write(";"); + } + function emitTryStatement(node) { + write("try "); + emit(node.tryBlock); + emit(node.catchClause); + if (node.finallyBlock) { + writeLine(); + write("finally "); + emit(node.finallyBlock); } - else { - // Write the opening line terminator or leading whitespace. - var mayEmitInterveningComments = (format & 131072 /* NoInterveningComments */) === 0; - var shouldEmitInterveningComments = mayEmitInterveningComments; - if (shouldWriteLeadingLineTerminator(parentNode, children, format)) { - writeLine(); - shouldEmitInterveningComments = false; - } - else if (format & 128 /* SpaceBetweenBraces */) { - write(" "); - } - // Increase the indent, if requested. - if (format & 64 /* Indented */) { - increaseIndent(); - } - // Emit each child. - var previousSibling = void 0; - var shouldDecreaseIndentAfterEmit = void 0; - var delimiter = getDelimiter(format); - for (var i = 0; i < count; i++) { - var child = children[start + i]; - // Write the delimiter if this is not the first node. - if (previousSibling) { - write(delimiter); - // Write either a line terminator or whitespace to separate the elements. - if (shouldWriteSeparatingLineTerminator(previousSibling, child, format)) { - // If a synthesized node in a single-line list starts on a new - // line, we should increase the indent. - if ((format & (3 /* LinesMask */ | 64 /* Indented */)) === 0 /* SingleLine */) { - increaseIndent(); - shouldDecreaseIndentAfterEmit = true; - } - writeLine(); - shouldEmitInterveningComments = false; - } - else if (previousSibling && format & 256 /* SpaceBetweenSiblings */) { - write(" "); - } + } + function emitDebuggerStatement(node) { + writeToken(76 /* DebuggerKeyword */, node.pos); + write(";"); + } + // + // Declarations + // + function emitVariableDeclaration(node) { + emit(node.name); + emitExpressionWithPrefix(" = ", node.initializer); + } + function emitVariableDeclarationList(node) { + write(ts.isLet(node) ? "let " : ts.isConst(node) ? "const " : "var "); + emitList(node, node.declarations, 272 /* VariableDeclarationList */); + } + function emitFunctionDeclaration(node) { + emitFunctionDeclarationOrExpression(node); + } + function emitFunctionDeclarationOrExpression(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + write(node.asteriskToken ? "function* " : "function "); + emitIdentifierName(node.name); + emitSignatureAndBody(node, emitSignatureHead); + } + function emitSignatureAndBody(node, emitSignatureHead) { + var body = node.body; + if (body) { + if (ts.isBlock(body)) { + var indentedFlag = ts.getEmitFlags(node) & 524288 /* Indented */; + if (indentedFlag) { + increaseIndent(); } - if (shouldEmitInterveningComments) { - var commentRange = child.commentRange || child; - emitTrailingCommentsOfPosition(commentRange.pos); + if (ts.getEmitFlags(node) & 4194304 /* ReuseTempVariableScope */) { + emitSignatureHead(node); + emitBlockFunctionBody(node, body); } else { - shouldEmitInterveningComments = mayEmitInterveningComments; + var savedTempFlags = tempFlags; + tempFlags = 0; + emitSignatureHead(node); + emitBlockFunctionBody(node, body); + tempFlags = savedTempFlags; } - // Emit this child. - emit(child); - if (shouldDecreaseIndentAfterEmit) { + if (indentedFlag) { decreaseIndent(); - shouldDecreaseIndentAfterEmit = false; } - previousSibling = child; - } - // Write a trailing comma, if requested. - var hasTrailingComma = (format & 32 /* AllowTrailingComma */) && children.hasTrailingComma; - if (format & 16 /* CommaDelimited */ && hasTrailingComma) { - write(","); - } - // Decrease the indent, if requested. - if (format & 64 /* Indented */) { - decreaseIndent(); - } - // Write the closing line terminator or closing whitespace. - if (shouldWriteClosingLineTerminator(parentNode, children, format)) { - writeLine(); } - else if (format & 128 /* SpaceBetweenBraces */) { + else { + emitSignatureHead(node); write(" "); + emitExpression(body); } } - if (format & 7680 /* BracketsMask */) { - write(getClosingBracket(format)); - } - } - function writeIfAny(nodes, text) { - if (nodes && nodes.length > 0) { - write(text); - } - } - function writeIfPresent(node, text) { - if (node !== undefined) { - write(text); - } - } - function writeToken(token, pos, contextNode) { - var tokenStartPos = emitTokenStart(token, pos, contextNode, shouldSkipLeadingSourceMapForToken, getTokenSourceMapRange); - var tokenEndPos = writeTokenText(token, tokenStartPos); - return emitTokenEnd(token, tokenEndPos, contextNode, shouldSkipTrailingSourceMapForToken, getTokenSourceMapRange); - } - function shouldSkipLeadingSourceMapForToken(contextNode) { - return (contextNode.emitFlags & 4096 /* NoTokenLeadingSourceMaps */) !== 0; - } - function shouldSkipTrailingSourceMapForToken(contextNode) { - return (contextNode.emitFlags & 8192 /* NoTokenTrailingSourceMaps */) !== 0; - } - function writeTokenText(token, pos) { - var tokenString = ts.tokenToString(token); - write(tokenString); - return ts.positionIsSynthesized(pos) ? -1 : pos + tokenString.length; - } - function writeTokenNode(node) { - if (node) { - emitStart(/*range*/ node, /*contextNode*/ node, shouldSkipLeadingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange); - writeTokenText(node.kind); - emitEnd(/*range*/ node, /*contextNode*/ node, shouldSkipTrailingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange); - } - } - function increaseIndentIf(value, valueToWriteWhenNotIndenting) { - if (value) { - increaseIndent(); - writeLine(); - } - else if (valueToWriteWhenNotIndenting) { - write(valueToWriteWhenNotIndenting); + else { + emitSignatureHead(node); + write(";"); } } - // Helper function to decrease the indent if we previously indented. Allows multiple - // previous indent values to be considered at a time. This also allows caller to just - // call this once, passing in all their appropriate indent values, instead of needing - // to call this helper function multiple times. - function decreaseIndentIf(value1, value2) { - if (value1) { - decreaseIndent(); - } - if (value2) { - decreaseIndent(); - } + function emitSignatureHead(node) { + emitTypeParameters(node, node.typeParameters); + emitParameters(node, node.parameters); + emitWithPrefix(": ", node.type); } - function shouldWriteLeadingLineTerminator(parentNode, children, format) { - if (format & 1 /* MultiLine */) { + function shouldEmitBlockFunctionBodyOnSingleLine(parentNode, body) { + // We must emit a function body as a single-line body in the following case: + // * The body has NodeEmitFlags.SingleLine specified. + // We must emit a function body as a multi-line body in the following cases: + // * The body is explicitly marked as multi-line. + // * A non-synthesized body's start and end position are on different lines. + // * Any statement in the body starts on a new line. + if (ts.getEmitFlags(body) & 32 /* SingleLine */) { return true; } - if (format & 2 /* PreserveLines */) { - if (format & 32768 /* PreferNewLine */) { - return true; - } - var firstChild = children[0]; - if (firstChild === undefined) { - return !ts.rangeIsOnSingleLine(parentNode, currentSourceFile); - } - else if (ts.positionIsSynthesized(parentNode.pos) || ts.nodeIsSynthesized(firstChild)) { - return synthesizedNodeStartsOnNewLine(firstChild, format); - } - else { - return !ts.rangeStartPositionsAreOnSameLine(parentNode, firstChild, currentSourceFile); - } + if (body.multiLine) { + return false; } - else { + if (!ts.nodeIsSynthesized(body) && !ts.rangeIsOnSingleLine(body, currentSourceFile)) { return false; } - } - function shouldWriteSeparatingLineTerminator(previousNode, nextNode, format) { - if (format & 1 /* MultiLine */) { - return true; + if (shouldWriteLeadingLineTerminator(body, body.statements, 2 /* PreserveLines */) + || shouldWriteClosingLineTerminator(body, body.statements, 2 /* PreserveLines */)) { + return false; } - else if (format & 2 /* PreserveLines */) { - if (previousNode === undefined || nextNode === undefined) { + var previousStatement; + for (var _a = 0, _b = body.statements; _a < _b.length; _a++) { + var statement = _b[_a]; + if (shouldWriteSeparatingLineTerminator(previousStatement, statement, 2 /* PreserveLines */)) { return false; } - else if (ts.nodeIsSynthesized(previousNode) || ts.nodeIsSynthesized(nextNode)) { - return synthesizedNodeStartsOnNewLine(previousNode, format) || synthesizedNodeStartsOnNewLine(nextNode, format); - } - else { - return !ts.rangeEndIsOnSameLineAsRangeStart(previousNode, nextNode, currentSourceFile); - } + previousStatement = statement; + } + return true; + } + function emitBlockFunctionBody(parentNode, body) { + write(" {"); + increaseIndent(); + emitBodyWithDetachedComments(body, body.statements, shouldEmitBlockFunctionBodyOnSingleLine(parentNode, body) + ? emitBlockFunctionBodyOnSingleLine + : emitBlockFunctionBodyWorker); + decreaseIndent(); + writeToken(16 /* CloseBraceToken */, body.statements.end, body); + } + function emitBlockFunctionBodyOnSingleLine(body) { + emitBlockFunctionBodyWorker(body, /*emitBlockFunctionBodyOnSingleLine*/ true); + } + function emitBlockFunctionBodyWorker(body, emitBlockFunctionBodyOnSingleLine) { + // Emit all the prologue directives (like "use strict"). + var statementOffset = emitPrologueDirectives(body.statements, /*startWithNewLine*/ true); + var helpersEmitted = emitHelpers(body); + if (statementOffset === 0 && !helpersEmitted && emitBlockFunctionBodyOnSingleLine) { + decreaseIndent(); + emitList(body, body.statements, 384 /* SingleLineFunctionBodyStatements */); + increaseIndent(); } else { - return nextNode.startsOnNewLine; + emitList(body, body.statements, 1 /* MultiLineFunctionBodyStatements */, statementOffset); } } - function shouldWriteClosingLineTerminator(parentNode, children, format) { - if (format & 1 /* MultiLine */) { - return (format & 65536 /* NoTrailingNewLine */) === 0; - } - else if (format & 2 /* PreserveLines */) { - if (format & 32768 /* PreferNewLine */) { - return true; - } - var lastChild = ts.lastOrUndefined(children); - if (lastChild === undefined) { - return !ts.rangeIsOnSingleLine(parentNode, currentSourceFile); - } - else if (ts.positionIsSynthesized(parentNode.pos) || ts.nodeIsSynthesized(lastChild)) { - return synthesizedNodeStartsOnNewLine(lastChild, format); - } - else { - return !ts.rangeEndPositionsAreOnSameLine(parentNode, lastChild, currentSourceFile); - } + function emitClassDeclaration(node) { + emitClassDeclarationOrExpression(node); + } + function emitClassDeclarationOrExpression(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + write("class"); + emitNodeWithPrefix(" ", node.name, emitIdentifierName); + var indentedFlag = ts.getEmitFlags(node) & 524288 /* Indented */; + if (indentedFlag) { + increaseIndent(); } - else { - return false; + emitTypeParameters(node, node.typeParameters); + emitList(node, node.heritageClauses, 256 /* ClassHeritageClauses */); + var savedTempFlags = tempFlags; + tempFlags = 0; + write(" {"); + emitList(node, node.members, 65 /* ClassMembers */); + write("}"); + if (indentedFlag) { + decreaseIndent(); } + tempFlags = savedTempFlags; } - function synthesizedNodeStartsOnNewLine(node, format) { - if (ts.nodeIsSynthesized(node)) { - var startsOnNewLine = node.startsOnNewLine; - if (startsOnNewLine === undefined) { - return (format & 32768 /* PreferNewLine */) !== 0; - } - return startsOnNewLine; - } - return (format & 32768 /* PreferNewLine */) !== 0; + function emitInterfaceDeclaration(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + write("interface "); + emit(node.name); + emitTypeParameters(node, node.typeParameters); + emitList(node, node.heritageClauses, 256 /* HeritageClauses */); + write(" {"); + emitList(node, node.members, 65 /* InterfaceMembers */); + write("}"); } - function needsIndentation(parent, node1, node2) { - parent = skipSynthesizedParentheses(parent); - node1 = skipSynthesizedParentheses(node1); - node2 = skipSynthesizedParentheses(node2); - // Always use a newline for synthesized code if the synthesizer desires it. - if (node2.startsOnNewLine) { - return true; - } - return !ts.nodeIsSynthesized(parent) - && !ts.nodeIsSynthesized(node1) - && !ts.nodeIsSynthesized(node2) - && !ts.rangeEndIsOnSameLineAsRangeStart(node1, node2, currentSourceFile); + function emitTypeAliasDeclaration(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + write("type "); + emit(node.name); + emitTypeParameters(node, node.typeParameters); + write(" = "); + emit(node.type); + write(";"); } - function skipSynthesizedParentheses(node) { - while (node.kind === 178 /* ParenthesizedExpression */ && ts.nodeIsSynthesized(node)) { - node = node.expression; + function emitEnumDeclaration(node) { + emitModifiers(node, node.modifiers); + write("enum "); + emit(node.name); + var savedTempFlags = tempFlags; + tempFlags = 0; + write(" {"); + emitList(node, node.members, 81 /* EnumMembers */); + write("}"); + tempFlags = savedTempFlags; + } + function emitModuleDeclaration(node) { + emitModifiers(node, node.modifiers); + write(node.flags & 16 /* Namespace */ ? "namespace " : "module "); + emit(node.name); + var body = node.body; + while (body.kind === 225 /* ModuleDeclaration */) { + write("."); + emit(body.name); + body = body.body; } - return node; + write(" "); + emit(body); } - function getTextOfNode(node, includeTrivia) { - if (ts.isGeneratedIdentifier(node)) { - return getGeneratedIdentifier(node); + function emitModuleBlock(node) { + if (isEmptyBlock(node)) { + write("{ }"); } - else if (ts.isIdentifier(node) && (ts.nodeIsSynthesized(node) || !node.parent)) { - return ts.unescapeIdentifier(node.text); + else { + var savedTempFlags = tempFlags; + tempFlags = 0; + write("{"); + increaseIndent(); + emitBlockStatements(node); + write("}"); + tempFlags = savedTempFlags; } - else if (node.kind === 9 /* StringLiteral */ && node.textSourceNode) { - return getTextOfNode(node.textSourceNode, includeTrivia); + } + function emitCaseBlock(node) { + writeToken(15 /* OpenBraceToken */, node.pos); + emitList(node, node.clauses, 65 /* CaseBlockClauses */); + writeToken(16 /* CloseBraceToken */, node.clauses.end); + } + function emitImportEqualsDeclaration(node) { + emitModifiers(node, node.modifiers); + write("import "); + emit(node.name); + write(" = "); + emitModuleReference(node.moduleReference); + write(";"); + } + function emitModuleReference(node) { + if (node.kind === 69 /* Identifier */) { + emitExpression(node); } - else if (ts.isLiteralExpression(node) && (ts.nodeIsSynthesized(node) || !node.parent)) { - return node.text; + else { + emit(node); } - return ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, node, includeTrivia); } - function getLiteralTextOfNode(node) { - if (node.kind === 9 /* StringLiteral */ && node.textSourceNode) { - var textSourceNode = node.textSourceNode; - if (ts.isIdentifier(textSourceNode)) { - return "\"" + ts.escapeNonAsciiCharacters(ts.escapeString(getTextOfNode(textSourceNode))) + "\""; - } - else { - return getLiteralTextOfNode(textSourceNode); - } + function emitImportDeclaration(node) { + emitModifiers(node, node.modifiers); + write("import "); + if (node.importClause) { + emit(node.importClause); + write(" from "); } - return ts.getLiteralText(node, currentSourceFile, languageVersion); + emitExpression(node.moduleSpecifier); + write(";"); } - function tryGetConstEnumValue(node) { - if (compilerOptions.isolatedModules) { - return undefined; + function emitImportClause(node) { + emit(node.name); + if (node.name && node.namedBindings) { + write(", "); } - return ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node) - ? resolver.getConstantValue(node) - : undefined; + emit(node.namedBindings); } - function isSingleLineEmptyBlock(block) { - return !block.multiLine - && block.statements.length === 0 - && ts.rangeEndIsOnSameLineAsRangeStart(block, block, currentSourceFile); + function emitNamespaceImport(node) { + write("* as "); + emit(node.name); } - function isUniqueName(name) { - return !resolver.hasGlobalName(name) && - !ts.hasProperty(currentFileIdentifiers, name) && - !ts.hasProperty(generatedNameSet, name); + function emitNamedImports(node) { + emitNamedImportsOrExports(node); } - function isUniqueLocalName(name, container) { - for (var node = container; ts.isNodeDescendantOf(node, container); node = node.nextContainer) { - if (node.locals && ts.hasProperty(node.locals, name)) { - // We conservatively include alias symbols to cover cases where they're emitted as locals - if (node.locals[name].flags & (107455 /* Value */ | 1048576 /* ExportValue */ | 8388608 /* Alias */)) { - return false; - } - } - } - return true; + function emitImportSpecifier(node) { + emitImportOrExportSpecifier(node); } - /** - * Return the next available name in the pattern _a ... _z, _0, _1, ... - * TempFlags._i or TempFlags._n may be used to express a preference for that dedicated name. - * Note that names generated by makeTempVariableName and makeUniqueName will never conflict. - */ - function makeTempVariableName(flags) { - if (flags && !(tempFlags & flags)) { - var name_41 = flags === 268435456 /* _i */ ? "_i" : "_n"; - if (isUniqueName(name_41)) { - tempFlags |= flags; - return name_41; - } - } - while (true) { - var count = tempFlags & 268435455 /* CountMask */; - tempFlags++; - // Skip over 'i' and 'n' - if (count !== 8 && count !== 13) { - var name_42 = count < 26 - ? "_" + String.fromCharCode(97 /* a */ + count) - : "_" + (count - 26); - if (isUniqueName(name_42)) { - return name_42; - } - } - } + function emitExportAssignment(node) { + write(node.isExportEquals ? "export = " : "export default "); + emitExpression(node.expression); + write(";"); } - // Generate a name that is unique within the current file and doesn't conflict with any names - // in global scope. The name is formed by adding an '_n' suffix to the specified base name, - // where n is a positive integer. Note that names generated by makeTempVariableName and - // makeUniqueName are guaranteed to never conflict. - function makeUniqueName(baseName) { - // Find the first unique 'name_n', where n is a positive number - if (baseName.charCodeAt(baseName.length - 1) !== 95 /* _ */) { - baseName += "_"; + function emitExportDeclaration(node) { + write("export "); + if (node.exportClause) { + emit(node.exportClause); } - var i = 1; - while (true) { - var generatedName = baseName + i; - if (isUniqueName(generatedName)) { - return generatedNameSet[generatedName] = generatedName; - } - i++; + else { + write("*"); } + if (node.moduleSpecifier) { + write(" from "); + emitExpression(node.moduleSpecifier); + } + write(";"); } - function generateNameForModuleOrEnum(node) { - var name = getTextOfNode(node.name); - // Use module/enum name itself if it is unique, otherwise make a unique variation - return isUniqueLocalName(name, node) ? name : makeUniqueName(name); - } - function generateNameForImportOrExportDeclaration(node) { - var expr = ts.getExternalModuleName(node); - var baseName = expr.kind === 9 /* StringLiteral */ ? - ts.escapeIdentifier(ts.makeIdentifierFromModuleName(expr.text)) : "module"; - return makeUniqueName(baseName); + function emitNamedExports(node) { + emitNamedImportsOrExports(node); } - function generateNameForExportDefault() { - return makeUniqueName("default"); + function emitExportSpecifier(node) { + emitImportOrExportSpecifier(node); } - function generateNameForClassExpression() { - return makeUniqueName("class"); + function emitNamedImportsOrExports(node) { + write("{"); + emitList(node, node.elements, 432 /* NamedImportsOrExportsElements */); + write("}"); } - /** - * Generates a unique name from a node. - * - * @param node A node. - */ - function generateNameForNode(node) { - switch (node.kind) { - case 69 /* Identifier */: - return makeUniqueName(getTextOfNode(node)); - case 225 /* ModuleDeclaration */: - case 224 /* EnumDeclaration */: - return generateNameForModuleOrEnum(node); - case 230 /* ImportDeclaration */: - case 236 /* ExportDeclaration */: - return generateNameForImportOrExportDeclaration(node); - case 220 /* FunctionDeclaration */: - case 221 /* ClassDeclaration */: - case 235 /* ExportAssignment */: - return generateNameForExportDefault(); - case 192 /* ClassExpression */: - return generateNameForClassExpression(); - default: - return makeTempVariableName(0 /* Auto */); + function emitImportOrExportSpecifier(node) { + if (node.propertyName) { + emit(node.propertyName); + write(" as "); } + emit(node.name); } - /** - * Generates a unique identifier for a node. - * - * @param name A generated name. - */ - function generateName(name) { - switch (name.autoGenerateKind) { - case 1 /* Auto */: - return makeTempVariableName(0 /* Auto */); - case 2 /* Loop */: - return makeTempVariableName(268435456 /* _i */); - case 3 /* Unique */: - return makeUniqueName(name.text); - } - ts.Debug.fail("Unsupported GeneratedIdentifierKind."); + // + // Module references + // + function emitExternalModuleReference(node) { + write("require("); + emitExpression(node.expression); + write(")"); } - /** - * Gets the node from which a name should be generated. - * - * @param name A generated name wrapper. - */ - function getNodeForGeneratedName(name) { - var autoGenerateId = name.autoGenerateId; - var node = name; - var original = node.original; - while (original) { - node = original; - // if "node" is a different generated name (having a different - // "autoGenerateId"), use it and stop traversing. - if (ts.isIdentifier(node) - && node.autoGenerateKind === 4 /* Node */ - && node.autoGenerateId !== autoGenerateId) { - break; - } - original = node.original; - } - // otherwise, return the original node for the source; - return node; + // + // JSX + // + function emitJsxElement(node) { + emit(node.openingElement); + emitList(node, node.children, 131072 /* JsxElementChildren */); + emit(node.closingElement); } - /** - * Gets the generated identifier text from a generated identifier. - * - * @param name The generated identifier. - */ - function getGeneratedIdentifier(name) { - if (name.autoGenerateKind === 4 /* Node */) { - // Generated names generate unique names based on their original node - // and are cached based on that node's id - var node = getNodeForGeneratedName(name); - var nodeId = ts.getNodeId(node); - return nodeIdToGeneratedName[nodeId] || (nodeIdToGeneratedName[nodeId] = ts.unescapeIdentifier(generateNameForNode(node))); - } - else { - // Auto, Loop, and Unique names are cached based on their unique - // autoGenerateId. - var autoGenerateId = name.autoGenerateId; - return autoGeneratedIdToGeneratedName[autoGenerateId] || (autoGeneratedIdToGeneratedName[autoGenerateId] = ts.unescapeIdentifier(generateName(name))); - } + function emitJsxSelfClosingElement(node) { + write("<"); + emitJsxTagName(node.tagName); + write(" "); + emitList(node, node.attributes, 131328 /* JsxElementAttributes */); + write("/>"); } - function createDelimiterMap() { - var delimiters = []; - delimiters[0 /* None */] = ""; - delimiters[16 /* CommaDelimited */] = ","; - delimiters[4 /* BarDelimited */] = " |"; - delimiters[8 /* AmpersandDelimited */] = " &"; - return delimiters; + function emitJsxOpeningElement(node) { + write("<"); + emitJsxTagName(node.tagName); + writeIfAny(node.attributes, " "); + emitList(node, node.attributes, 131328 /* JsxElementAttributes */); + write(">"); } - function getDelimiter(format) { - return delimiters[format & 28 /* DelimitersMask */]; + function emitJsxText(node) { + writer.writeLiteral(getTextOfNode(node, /*includeTrivia*/ true)); } - function createBracketsMap() { - var brackets = []; - brackets[512 /* Braces */] = ["{", "}"]; - brackets[1024 /* Parenthesis */] = ["(", ")"]; - brackets[2048 /* AngleBrackets */] = ["<", ">"]; - brackets[4096 /* SquareBrackets */] = ["[", "]"]; - return brackets; + function emitJsxClosingElement(node) { + write(""); } - function getOpeningBracket(format) { - return brackets[format & 7680 /* BracketsMask */][0]; + function emitJsxAttribute(node) { + emit(node.name); + emitWithPrefix("=", node.initializer); } - function getClosingBracket(format) { - return brackets[format & 7680 /* BracketsMask */][1]; + function emitJsxSpreadAttribute(node) { + write("{..."); + emitExpression(node.expression); + write("}"); } - } - ts.emitFiles = emitFiles; - var ListFormat; - (function (ListFormat) { - ListFormat[ListFormat["None"] = 0] = "None"; - // Line separators - ListFormat[ListFormat["SingleLine"] = 0] = "SingleLine"; - ListFormat[ListFormat["MultiLine"] = 1] = "MultiLine"; - ListFormat[ListFormat["PreserveLines"] = 2] = "PreserveLines"; - ListFormat[ListFormat["LinesMask"] = 3] = "LinesMask"; - // Delimiters - ListFormat[ListFormat["NotDelimited"] = 0] = "NotDelimited"; - ListFormat[ListFormat["BarDelimited"] = 4] = "BarDelimited"; - ListFormat[ListFormat["AmpersandDelimited"] = 8] = "AmpersandDelimited"; - ListFormat[ListFormat["CommaDelimited"] = 16] = "CommaDelimited"; - ListFormat[ListFormat["DelimitersMask"] = 28] = "DelimitersMask"; - ListFormat[ListFormat["AllowTrailingComma"] = 32] = "AllowTrailingComma"; - // Whitespace - ListFormat[ListFormat["Indented"] = 64] = "Indented"; - ListFormat[ListFormat["SpaceBetweenBraces"] = 128] = "SpaceBetweenBraces"; - ListFormat[ListFormat["SpaceBetweenSiblings"] = 256] = "SpaceBetweenSiblings"; - // Brackets/Braces - ListFormat[ListFormat["Braces"] = 512] = "Braces"; - ListFormat[ListFormat["Parenthesis"] = 1024] = "Parenthesis"; - ListFormat[ListFormat["AngleBrackets"] = 2048] = "AngleBrackets"; - ListFormat[ListFormat["SquareBrackets"] = 4096] = "SquareBrackets"; - ListFormat[ListFormat["BracketsMask"] = 7680] = "BracketsMask"; - ListFormat[ListFormat["OptionalIfUndefined"] = 8192] = "OptionalIfUndefined"; - ListFormat[ListFormat["OptionalIfEmpty"] = 16384] = "OptionalIfEmpty"; - ListFormat[ListFormat["Optional"] = 24576] = "Optional"; - // Other - ListFormat[ListFormat["PreferNewLine"] = 32768] = "PreferNewLine"; - ListFormat[ListFormat["NoTrailingNewLine"] = 65536] = "NoTrailingNewLine"; - ListFormat[ListFormat["NoInterveningComments"] = 131072] = "NoInterveningComments"; - // Precomputed Formats - ListFormat[ListFormat["Modifiers"] = 256] = "Modifiers"; - ListFormat[ListFormat["HeritageClauses"] = 256] = "HeritageClauses"; - ListFormat[ListFormat["TypeLiteralMembers"] = 65] = "TypeLiteralMembers"; - ListFormat[ListFormat["TupleTypeElements"] = 336] = "TupleTypeElements"; - ListFormat[ListFormat["UnionTypeConstituents"] = 260] = "UnionTypeConstituents"; - ListFormat[ListFormat["IntersectionTypeConstituents"] = 264] = "IntersectionTypeConstituents"; - ListFormat[ListFormat["ObjectBindingPatternElements"] = 432] = "ObjectBindingPatternElements"; - ListFormat[ListFormat["ArrayBindingPatternElements"] = 304] = "ArrayBindingPatternElements"; - ListFormat[ListFormat["ObjectLiteralExpressionProperties"] = 978] = "ObjectLiteralExpressionProperties"; - ListFormat[ListFormat["ArrayLiteralExpressionElements"] = 4466] = "ArrayLiteralExpressionElements"; - ListFormat[ListFormat["CallExpressionArguments"] = 1296] = "CallExpressionArguments"; - ListFormat[ListFormat["NewExpressionArguments"] = 9488] = "NewExpressionArguments"; - ListFormat[ListFormat["TemplateExpressionSpans"] = 131072] = "TemplateExpressionSpans"; - ListFormat[ListFormat["SingleLineBlockStatements"] = 384] = "SingleLineBlockStatements"; - ListFormat[ListFormat["MultiLineBlockStatements"] = 65] = "MultiLineBlockStatements"; - ListFormat[ListFormat["VariableDeclarationList"] = 272] = "VariableDeclarationList"; - ListFormat[ListFormat["SingleLineFunctionBodyStatements"] = 384] = "SingleLineFunctionBodyStatements"; - ListFormat[ListFormat["MultiLineFunctionBodyStatements"] = 1] = "MultiLineFunctionBodyStatements"; - ListFormat[ListFormat["ClassHeritageClauses"] = 256] = "ClassHeritageClauses"; - ListFormat[ListFormat["ClassMembers"] = 65] = "ClassMembers"; - ListFormat[ListFormat["InterfaceMembers"] = 65] = "InterfaceMembers"; - ListFormat[ListFormat["EnumMembers"] = 81] = "EnumMembers"; - ListFormat[ListFormat["CaseBlockClauses"] = 65] = "CaseBlockClauses"; - ListFormat[ListFormat["NamedImportsOrExportsElements"] = 432] = "NamedImportsOrExportsElements"; - ListFormat[ListFormat["JsxElementChildren"] = 131072] = "JsxElementChildren"; - ListFormat[ListFormat["JsxElementAttributes"] = 131328] = "JsxElementAttributes"; - ListFormat[ListFormat["CaseOrDefaultClauseStatements"] = 81985] = "CaseOrDefaultClauseStatements"; - ListFormat[ListFormat["HeritageClauseTypes"] = 272] = "HeritageClauseTypes"; - ListFormat[ListFormat["SourceFileStatements"] = 65537] = "SourceFileStatements"; - ListFormat[ListFormat["Decorators"] = 24577] = "Decorators"; - ListFormat[ListFormat["TypeArguments"] = 26960] = "TypeArguments"; - ListFormat[ListFormat["TypeParameters"] = 26960] = "TypeParameters"; - ListFormat[ListFormat["Parameters"] = 1360] = "Parameters"; - ListFormat[ListFormat["IndexSignatureParameters"] = 4432] = "IndexSignatureParameters"; - })(ListFormat || (ListFormat = {})); -})(ts || (ts = {})); -/// -/// -/// -var ts; -(function (ts) { - /** The version of the TypeScript compiler release */ - ts.version = "2.1.0"; - var emptyArray = []; - function findConfigFile(searchPath, fileExists, configName) { - if (configName === void 0) { configName = "tsconfig.json"; } - while (true) { - var fileName = ts.combinePaths(searchPath, configName); - if (fileExists(fileName)) { - return fileName; + function emitJsxExpression(node) { + if (node.expression) { + write("{"); + emitExpression(node.expression); + write("}"); } - var parentPath = ts.getDirectoryPath(searchPath); - if (parentPath === searchPath) { - break; + } + function emitJsxTagName(node) { + if (node.kind === 69 /* Identifier */) { + emitExpression(node); + } + else { + emit(node); } - searchPath = parentPath; } - return undefined; - } - ts.findConfigFile = findConfigFile; - function resolveTripleslashReference(moduleName, containingFile) { - var basePath = ts.getDirectoryPath(containingFile); - var referencedFileName = ts.isRootedDiskPath(moduleName) ? moduleName : ts.combinePaths(basePath, moduleName); - return ts.normalizePath(referencedFileName); - } - ts.resolveTripleslashReference = resolveTripleslashReference; - /* @internal */ - function computeCommonSourceDirectoryOfFilenames(fileNames, currentDirectory, getCanonicalFileName) { - var commonPathComponents; - var failed = ts.forEach(fileNames, function (sourceFile) { - // Each file contributes into common source file path - var sourcePathComponents = ts.getNormalizedPathComponents(sourceFile, currentDirectory); - sourcePathComponents.pop(); // The base file name is not part of the common directory path - if (!commonPathComponents) { - // first file - commonPathComponents = sourcePathComponents; - return; + // + // Clauses + // + function emitCaseClause(node) { + write("case "); + emitExpression(node.expression); + write(":"); + emitCaseOrDefaultClauseStatements(node, node.statements); + } + function emitDefaultClause(node) { + write("default:"); + emitCaseOrDefaultClauseStatements(node, node.statements); + } + function emitCaseOrDefaultClauseStatements(parentNode, statements) { + var emitAsSingleStatement = statements.length === 1 && + ( + // treat synthesized nodes as located on the same line for emit purposes + ts.nodeIsSynthesized(parentNode) || + ts.nodeIsSynthesized(statements[0]) || + ts.rangeStartPositionsAreOnSameLine(parentNode, statements[0], currentSourceFile)); + if (emitAsSingleStatement) { + write(" "); + emit(statements[0]); } - for (var i = 0, n = Math.min(commonPathComponents.length, sourcePathComponents.length); i < n; i++) { - if (getCanonicalFileName(commonPathComponents[i]) !== getCanonicalFileName(sourcePathComponents[i])) { - if (i === 0) { - // Failed to find any common path component - return true; - } - // New common path found that is 0 -> i-1 - commonPathComponents.length = i; - break; - } + else { + emitList(parentNode, statements, 81985 /* CaseOrDefaultClauseStatements */); } - // If the sourcePathComponents was shorter than the commonPathComponents, truncate to the sourcePathComponents - if (sourcePathComponents.length < commonPathComponents.length) { - commonPathComponents.length = sourcePathComponents.length; + } + function emitHeritageClause(node) { + write(" "); + writeTokenText(node.token); + write(" "); + emitList(node, node.types, 272 /* HeritageClauseTypes */); + } + function emitCatchClause(node) { + writeLine(); + var openParenPos = writeToken(72 /* CatchKeyword */, node.pos); + write(" "); + writeToken(17 /* OpenParenToken */, openParenPos); + emit(node.variableDeclaration); + writeToken(18 /* CloseParenToken */, node.variableDeclaration ? node.variableDeclaration.end : openParenPos); + write(" "); + emit(node.block); + } + // + // Property assignments + // + function emitPropertyAssignment(node) { + emit(node.name); + write(": "); + // This is to ensure that we emit comment in the following case: + // For example: + // obj = { + // id: /*comment1*/ ()=>void + // } + // "comment1" is not considered to be leading comment for node.initializer + // but rather a trailing comment on the previous node. + var initializer = node.initializer; + if ((ts.getEmitFlags(initializer) & 16384 /* NoLeadingComments */) === 0) { + var commentRange = ts.getCommentRange(initializer); + emitTrailingCommentsOfPosition(commentRange.pos); } - }); - // A common path can not be found when paths span multiple drives on windows, for example - if (failed) { - return ""; + emitExpression(initializer); } - if (!commonPathComponents) { - return currentDirectory; + function emitShorthandPropertyAssignment(node) { + emit(node.name); + if (node.objectAssignmentInitializer) { + write(" = "); + emitExpression(node.objectAssignmentInitializer); + } } - return ts.getNormalizedPathFromPathComponents(commonPathComponents); - } - ts.computeCommonSourceDirectoryOfFilenames = computeCommonSourceDirectoryOfFilenames; - function trace(host, message) { - host.trace(ts.formatMessage.apply(undefined, arguments)); - } - function isTraceEnabled(compilerOptions, host) { - return compilerOptions.traceResolution && host.trace !== undefined; - } - /* @internal */ - function hasZeroOrOneAsteriskCharacter(str) { - var seenAsterisk = false; - for (var i = 0; i < str.length; i++) { - if (str.charCodeAt(i) === 42 /* asterisk */) { - if (!seenAsterisk) { - seenAsterisk = true; + // + // Enum + // + function emitEnumMember(node) { + emit(node.name); + emitExpressionWithPrefix(" = ", node.initializer); + } + // + // Top-level nodes + // + function emitSourceFile(node) { + writeLine(); + emitShebang(); + emitBodyWithDetachedComments(node, node.statements, emitSourceFileWorker); + } + function emitSourceFileWorker(node) { + var statements = node.statements; + var statementOffset = emitPrologueDirectives(statements); + var savedTempFlags = tempFlags; + tempFlags = 0; + emitHelpers(node); + emitList(node, statements, 1 /* MultiLine */, statementOffset); + tempFlags = savedTempFlags; + } + // Transformation nodes + function emitPartiallyEmittedExpression(node) { + emitExpression(node.expression); + } + /** + * Emits any prologue directives at the start of a Statement list, returning the + * number of prologue directives written to the output. + */ + function emitPrologueDirectives(statements, startWithNewLine) { + for (var i = 0; i < statements.length; i++) { + if (ts.isPrologueDirective(statements[i])) { + if (startWithNewLine || i > 0) { + writeLine(); + } + emit(statements[i]); } else { - // have already seen asterisk - return false; + // return index of the first non prologue directive + return i; } } + return statements.length; } - return true; - } - ts.hasZeroOrOneAsteriskCharacter = hasZeroOrOneAsteriskCharacter; - function createResolvedModule(resolvedFileName, isExternalLibraryImport, failedLookupLocations) { - return { resolvedModule: resolvedFileName ? { resolvedFileName: resolvedFileName, isExternalLibraryImport: isExternalLibraryImport } : undefined, failedLookupLocations: failedLookupLocations }; - } - function moduleHasNonRelativeName(moduleName) { - return !(ts.isRootedDiskPath(moduleName) || ts.isExternalModuleNameRelative(moduleName)); - } - function tryReadTypesSection(packageJsonPath, baseDirectory, state) { - var jsonContent = readJson(packageJsonPath, state.host); - function tryReadFromField(fieldName) { - if (ts.hasProperty(jsonContent, fieldName)) { - var typesFile = jsonContent[fieldName]; - if (typeof typesFile === "string") { - var typesFilePath_1 = ts.normalizePath(ts.combinePaths(baseDirectory, typesFile)); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.package_json_has_0_field_1_that_references_2, fieldName, typesFile, typesFilePath_1); - } - return typesFilePath_1; + function emitHelpers(node) { + var emitFlags = ts.getEmitFlags(node); + var helpersEmitted = false; + if (emitFlags & 1 /* EmitEmitHelpers */) { + helpersEmitted = emitEmitHelpers(currentSourceFile); + } + if (emitFlags & 2 /* EmitExportStar */) { + writeLines(exportStarHelper); + helpersEmitted = true; + } + if (emitFlags & 4 /* EmitSuperHelper */) { + writeLines(superHelper); + helpersEmitted = true; + } + if (emitFlags & 8 /* EmitAdvancedSuperHelper */) { + writeLines(advancedSuperHelper); + helpersEmitted = true; + } + return helpersEmitted; + } + function emitEmitHelpers(node) { + // Only emit helpers if the user did not say otherwise. + if (compilerOptions.noEmitHelpers) { + return false; + } + // Don't emit helpers if we can import them. + if (compilerOptions.importHelpers + && (ts.isExternalModule(node) || compilerOptions.isolatedModules)) { + return false; + } + var helpersEmitted = false; + // Only Emit __extends function when target ES5. + // For target ES6 and above, we can emit classDeclaration as is. + if ((languageVersion < 2 /* ES6 */) && (!extendsEmitted && node.flags & 1024 /* HasClassExtends */)) { + writeLines(extendsHelper); + extendsEmitted = true; + helpersEmitted = true; + } + if (compilerOptions.jsx !== 1 /* Preserve */ && !assignEmitted && (node.flags & 16384 /* HasJsxSpreadAttributes */)) { + writeLines(assignHelper); + assignEmitted = true; + } + if (!decorateEmitted && node.flags & 2048 /* HasDecorators */) { + writeLines(decorateHelper); + if (compilerOptions.emitDecoratorMetadata) { + writeLines(metadataHelper); } - else { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Expected_type_of_0_field_in_package_json_to_be_string_got_1, fieldName, typeof typesFile); + decorateEmitted = true; + helpersEmitted = true; + } + if (!paramEmitted && node.flags & 4096 /* HasParamDecorators */) { + writeLines(paramHelper); + paramEmitted = true; + helpersEmitted = true; + } + if (!awaiterEmitted && node.flags & 8192 /* HasAsyncFunctions */) { + writeLines(awaiterHelper); + if (languageVersion < 2 /* ES6 */) { + writeLines(generatorHelper); + } + awaiterEmitted = true; + helpersEmitted = true; + } + if (helpersEmitted) { + writeLine(); + } + return helpersEmitted; + } + function writeLines(text) { + var lines = text.split(/\r\n|\r|\n/g); + for (var i = 0; i < lines.length; i++) { + var line = lines[i]; + if (line.length) { + if (i > 0) { + writeLine(); } + write(line); } } } - var typesFilePath = tryReadFromField("typings") || tryReadFromField("types"); - if (typesFilePath) { - return typesFilePath; + // + // Helpers + // + function emitShebang() { + var shebang = ts.getShebang(currentText); + if (shebang) { + write(shebang); + writeLine(); + } } - // Use the main module for inferring types if no types package specified and the allowJs is set - if (state.compilerOptions.allowJs && jsonContent.main && typeof jsonContent.main === "string") { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.No_types_specified_in_package_json_but_allowJs_is_set_so_returning_main_value_of_0, jsonContent.main); + function emitModifiers(node, modifiers) { + if (modifiers && modifiers.length) { + emitList(node, modifiers, 256 /* Modifiers */); + write(" "); } - var mainFilePath = ts.normalizePath(ts.combinePaths(baseDirectory, jsonContent.main)); - return mainFilePath; } - return undefined; - } - function readJson(path, host) { - try { - var jsonText = host.readFile(path); - return jsonText ? JSON.parse(jsonText) : {}; + function emitWithPrefix(prefix, node) { + emitNodeWithPrefix(prefix, node, emit); } - catch (e) { - // gracefully handle if readFile fails or returns not JSON - return {}; + function emitExpressionWithPrefix(prefix, node) { + emitNodeWithPrefix(prefix, node, emitExpression); } - } - var typeReferenceExtensions = [".d.ts"]; - function getEffectiveTypeRoots(options, host) { - if (options.typeRoots) { - return options.typeRoots; + function emitNodeWithPrefix(prefix, node, emit) { + if (node) { + write(prefix); + emit(node); + } } - var currentDirectory; - if (options.configFilePath) { - currentDirectory = ts.getDirectoryPath(options.configFilePath); + function emitWithSuffix(node, suffix) { + if (node) { + emit(node); + write(suffix); + } } - else if (host.getCurrentDirectory) { - currentDirectory = host.getCurrentDirectory(); + function emitEmbeddedStatement(node) { + if (ts.isBlock(node)) { + write(" "); + emit(node); + } + else { + writeLine(); + increaseIndent(); + emit(node); + decreaseIndent(); + } } - return currentDirectory && getDefaultTypeRoots(currentDirectory, host); - } - ts.getEffectiveTypeRoots = getEffectiveTypeRoots; - /** - * Returns the path to every node_modules/@types directory from some ancestor directory. - * Returns undefined if there are none. - */ - function getDefaultTypeRoots(currentDirectory, host) { - if (!host.directoryExists) { - return [ts.combinePaths(currentDirectory, nodeModulesAtTypes)]; + function emitDecorators(parentNode, decorators) { + emitList(parentNode, decorators, 24577 /* Decorators */); } - var typeRoots; - while (true) { - var atTypes = ts.combinePaths(currentDirectory, nodeModulesAtTypes); - if (host.directoryExists(atTypes)) { - (typeRoots || (typeRoots = [])).push(atTypes); + function emitTypeArguments(parentNode, typeArguments) { + emitList(parentNode, typeArguments, 26960 /* TypeArguments */); + } + function emitTypeParameters(parentNode, typeParameters) { + emitList(parentNode, typeParameters, 26960 /* TypeParameters */); + } + function emitParameters(parentNode, parameters) { + emitList(parentNode, parameters, 1360 /* Parameters */); + } + function emitParametersForArrow(parentNode, parameters) { + if (parameters && + parameters.length === 1 && + parameters[0].type === undefined && + parameters[0].pos === parentNode.pos) { + emit(parameters[0]); } - var parent_15 = ts.getDirectoryPath(currentDirectory); - if (parent_15 === currentDirectory) { - break; + else { + emitParameters(parentNode, parameters); } - currentDirectory = parent_15; } - return typeRoots; - } - var nodeModulesAtTypes = ts.combinePaths("node_modules", "@types"); - /** - * @param {string | undefined} containingFile - file that contains type reference directive, can be undefined if containing file is unknown. - * This is possible in case if resolution is performed for directives specified via 'types' parameter. In this case initial path for secondary lookups - * is assumed to be the same as root directory of the project. - */ - function resolveTypeReferenceDirective(typeReferenceDirectiveName, containingFile, options, host) { - var traceEnabled = isTraceEnabled(options, host); - var moduleResolutionState = { - compilerOptions: options, - host: host, - skipTsx: true, - traceEnabled: traceEnabled - }; - var typeRoots = getEffectiveTypeRoots(options, host); - if (traceEnabled) { - if (containingFile === undefined) { - if (typeRoots === undefined) { - trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_not_set_root_directory_not_set, typeReferenceDirectiveName); + function emitParametersForIndexSignature(parentNode, parameters) { + emitList(parentNode, parameters, 4432 /* IndexSignatureParameters */); + } + function emitList(parentNode, children, format, start, count) { + emitNodeList(emit, parentNode, children, format, start, count); + } + function emitExpressionList(parentNode, children, format, start, count) { + emitNodeList(emitExpression, parentNode, children, format, start, count); + } + function emitNodeList(emit, parentNode, children, format, start, count) { + if (start === void 0) { start = 0; } + if (count === void 0) { count = children ? children.length - start : 0; } + var isUndefined = children === undefined; + if (isUndefined && format & 8192 /* OptionalIfUndefined */) { + return; + } + var isEmpty = isUndefined || children.length === 0 || start >= children.length || count === 0; + if (isEmpty && format & 16384 /* OptionalIfEmpty */) { + return; + } + if (format & 7680 /* BracketsMask */) { + write(getOpeningBracket(format)); + } + if (isEmpty) { + // Write a line terminator if the parent node was multi-line + if (format & 1 /* MultiLine */) { + writeLine(); } - else { - trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_not_set_root_directory_1, typeReferenceDirectiveName, typeRoots); + else if (format & 128 /* SpaceBetweenBraces */) { + write(" "); } } else { - if (typeRoots === undefined) { - trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_1_root_directory_not_set, typeReferenceDirectiveName, containingFile); + // Write the opening line terminator or leading whitespace. + var mayEmitInterveningComments = (format & 131072 /* NoInterveningComments */) === 0; + var shouldEmitInterveningComments = mayEmitInterveningComments; + if (shouldWriteLeadingLineTerminator(parentNode, children, format)) { + writeLine(); + shouldEmitInterveningComments = false; } - else { - trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_1_root_directory_2, typeReferenceDirectiveName, containingFile, typeRoots); + else if (format & 128 /* SpaceBetweenBraces */) { + write(" "); } - } - } - var failedLookupLocations = []; - // Check primary library paths - if (typeRoots && typeRoots.length) { - if (traceEnabled) { - trace(host, ts.Diagnostics.Resolving_with_primary_search_path_0, typeRoots.join(", ")); - } - var primarySearchPaths = typeRoots; - for (var _i = 0, primarySearchPaths_1 = primarySearchPaths; _i < primarySearchPaths_1.length; _i++) { - var typeRoot = primarySearchPaths_1[_i]; - var candidate = ts.combinePaths(typeRoot, typeReferenceDirectiveName); - var candidateDirectory = ts.getDirectoryPath(candidate); - var resolvedFile_1 = loadNodeModuleFromDirectory(typeReferenceExtensions, candidate, failedLookupLocations, !directoryProbablyExists(candidateDirectory, host), moduleResolutionState); - if (resolvedFile_1) { - if (traceEnabled) { - trace(host, ts.Diagnostics.Type_reference_directive_0_was_successfully_resolved_to_1_primary_Colon_2, typeReferenceDirectiveName, resolvedFile_1, true); + // Increase the indent, if requested. + if (format & 64 /* Indented */) { + increaseIndent(); + } + // Emit each child. + var previousSibling = void 0; + var shouldDecreaseIndentAfterEmit = void 0; + var delimiter = getDelimiter(format); + for (var i = 0; i < count; i++) { + var child = children[start + i]; + // Write the delimiter if this is not the first node. + if (previousSibling) { + write(delimiter); + // Write either a line terminator or whitespace to separate the elements. + if (shouldWriteSeparatingLineTerminator(previousSibling, child, format)) { + // If a synthesized node in a single-line list starts on a new + // line, we should increase the indent. + if ((format & (3 /* LinesMask */ | 64 /* Indented */)) === 0 /* SingleLine */) { + increaseIndent(); + shouldDecreaseIndentAfterEmit = true; + } + writeLine(); + shouldEmitInterveningComments = false; + } + else if (previousSibling && format & 256 /* SpaceBetweenSiblings */) { + write(" "); + } } - return { - resolvedTypeReferenceDirective: { primary: true, resolvedFileName: resolvedFile_1 }, - failedLookupLocations: failedLookupLocations - }; + if (shouldEmitInterveningComments) { + var commentRange = ts.getCommentRange(child); + emitTrailingCommentsOfPosition(commentRange.pos); + } + else { + shouldEmitInterveningComments = mayEmitInterveningComments; + } + // Emit this child. + emit(child); + if (shouldDecreaseIndentAfterEmit) { + decreaseIndent(); + shouldDecreaseIndentAfterEmit = false; + } + previousSibling = child; + } + // Write a trailing comma, if requested. + var hasTrailingComma = (format & 32 /* AllowTrailingComma */) && children.hasTrailingComma; + if (format & 16 /* CommaDelimited */ && hasTrailingComma) { + write(","); + } + // Decrease the indent, if requested. + if (format & 64 /* Indented */) { + decreaseIndent(); + } + // Write the closing line terminator or closing whitespace. + if (shouldWriteClosingLineTerminator(parentNode, children, format)) { + writeLine(); + } + else if (format & 128 /* SpaceBetweenBraces */) { + write(" "); } } - } - else { - if (traceEnabled) { - trace(host, ts.Diagnostics.Root_directory_cannot_be_determined_skipping_primary_search_paths); + if (format & 7680 /* BracketsMask */) { + write(getClosingBracket(format)); } } - var resolvedFile; - var initialLocationForSecondaryLookup; - if (containingFile) { - initialLocationForSecondaryLookup = ts.getDirectoryPath(containingFile); - } - if (initialLocationForSecondaryLookup !== undefined) { - // check secondary locations - if (traceEnabled) { - trace(host, ts.Diagnostics.Looking_up_in_node_modules_folder_initial_location_0, initialLocationForSecondaryLookup); - } - resolvedFile = loadModuleFromNodeModules(typeReferenceDirectiveName, initialLocationForSecondaryLookup, failedLookupLocations, moduleResolutionState); - if (traceEnabled) { - if (resolvedFile) { - trace(host, ts.Diagnostics.Type_reference_directive_0_was_successfully_resolved_to_1_primary_Colon_2, typeReferenceDirectiveName, resolvedFile, false); - } - else { - trace(host, ts.Diagnostics.Type_reference_directive_0_was_not_resolved, typeReferenceDirectiveName); - } + function writeIfAny(nodes, text) { + if (nodes && nodes.length > 0) { + write(text); } } - else { - if (traceEnabled) { - trace(host, ts.Diagnostics.Containing_file_is_not_specified_and_root_directory_cannot_be_determined_skipping_lookup_in_node_modules_folder); + function writeIfPresent(node, text) { + if (node !== undefined) { + write(text); } } - return { - resolvedTypeReferenceDirective: resolvedFile - ? { primary: false, resolvedFileName: resolvedFile } - : undefined, - failedLookupLocations: failedLookupLocations - }; - } - ts.resolveTypeReferenceDirective = resolveTypeReferenceDirective; - function resolveModuleName(moduleName, containingFile, compilerOptions, host) { - var traceEnabled = isTraceEnabled(compilerOptions, host); - if (traceEnabled) { - trace(host, ts.Diagnostics.Resolving_module_0_from_1, moduleName, containingFile); + function writeToken(token, pos, contextNode) { + return emitTokenWithSourceMap(contextNode, token, pos, writeTokenText); } - var moduleResolution = compilerOptions.moduleResolution; - if (moduleResolution === undefined) { - moduleResolution = ts.getEmitModuleKind(compilerOptions) === ts.ModuleKind.CommonJS ? ts.ModuleResolutionKind.NodeJs : ts.ModuleResolutionKind.Classic; - if (traceEnabled) { - trace(host, ts.Diagnostics.Module_resolution_kind_is_not_specified_using_0, ts.ModuleResolutionKind[moduleResolution]); - } + function writeTokenText(token, pos) { + var tokenString = ts.tokenToString(token); + write(tokenString); + return pos < 0 ? pos : pos + tokenString.length; } - else { - if (traceEnabled) { - trace(host, ts.Diagnostics.Explicitly_specified_module_resolution_kind_Colon_0, ts.ModuleResolutionKind[moduleResolution]); + function increaseIndentIf(value, valueToWriteWhenNotIndenting) { + if (value) { + increaseIndent(); + writeLine(); + } + else if (valueToWriteWhenNotIndenting) { + write(valueToWriteWhenNotIndenting); } } - var result; - switch (moduleResolution) { - case ts.ModuleResolutionKind.NodeJs: - result = nodeModuleNameResolver(moduleName, containingFile, compilerOptions, host); - break; - case ts.ModuleResolutionKind.Classic: - result = classicNameResolver(moduleName, containingFile, compilerOptions, host); - break; - } - if (traceEnabled) { - if (result.resolvedModule) { - trace(host, ts.Diagnostics.Module_name_0_was_successfully_resolved_to_1, moduleName, result.resolvedModule.resolvedFileName); + // Helper function to decrease the indent if we previously indented. Allows multiple + // previous indent values to be considered at a time. This also allows caller to just + // call this once, passing in all their appropriate indent values, instead of needing + // to call this helper function multiple times. + function decreaseIndentIf(value1, value2) { + if (value1) { + decreaseIndent(); } - else { - trace(host, ts.Diagnostics.Module_name_0_was_not_resolved, moduleName); + if (value2) { + decreaseIndent(); } } - return result; - } - ts.resolveModuleName = resolveModuleName; - /** - * Any module resolution kind can be augmented with optional settings: 'baseUrl', 'paths' and 'rootDirs' - they are used to - * mitigate differences between design time structure of the project and its runtime counterpart so the same import name - * can be resolved successfully by TypeScript compiler and runtime module loader. - * If these settings are set then loading procedure will try to use them to resolve module name and it can of failure it will - * fallback to standard resolution routine. - * - * - baseUrl - this setting controls how non-relative module names are resolved. If this setting is specified then non-relative - * names will be resolved relative to baseUrl: i.e. if baseUrl is '/a/b' then candidate location to resolve module name 'c/d' will - * be '/a/b/c/d' - * - paths - this setting can only be used when baseUrl is specified. allows to tune how non-relative module names - * will be resolved based on the content of the module name. - * Structure of 'paths' compiler options - * 'paths': { - * pattern-1: [...substitutions], - * pattern-2: [...substitutions], - * ... - * pattern-n: [...substitutions] - * } - * Pattern here is a string that can contain zero or one '*' character. During module resolution module name will be matched against - * all patterns in the list. Matching for patterns that don't contain '*' means that module name must be equal to pattern respecting the case. - * If pattern contains '*' then to match pattern "*" module name must start with the and end with . - * denotes part of the module name between and . - * If module name can be matches with multiple patterns then pattern with the longest prefix will be picked. - * After selecting pattern we'll use list of substitutions to get candidate locations of the module and the try to load module - * from the candidate location. - * Substitution is a string that can contain zero or one '*'. To get candidate location from substitution we'll pick every - * substitution in the list and replace '*' with string. If candidate location is not rooted it - * will be converted to absolute using baseUrl. - * For example: - * baseUrl: /a/b/c - * "paths": { - * // match all module names - * "*": [ - * "*", // use matched name as is, - * // will be looked as /a/b/c/ - * - * "folder1/*" // substitution will convert matched name to 'folder1/', - * // since it is not rooted then final candidate location will be /a/b/c/folder1/ - * ], - * // match module names that start with 'components/' - * "components/*": [ "/root/components/*" ] // substitution will convert /components/folder1/ to '/root/components/folder1/', - * // it is rooted so it will be final candidate location - * } - * - * 'rootDirs' allows the project to be spreaded across multiple locations and resolve modules with relative names as if - * they were in the same location. For example lets say there are two files - * '/local/src/content/file1.ts' - * '/shared/components/contracts/src/content/protocols/file2.ts' - * After bundling content of '/shared/components/contracts/src' will be merged with '/local/src' so - * if file1 has the following import 'import {x} from "./protocols/file2"' it will be resolved successfully in runtime. - * 'rootDirs' provides the way to tell compiler that in order to get the whole project it should behave as if content of all - * root dirs were merged together. - * I.e. for the example above 'rootDirs' will have two entries: [ '/local/src', '/shared/components/contracts/src' ]. - * Compiler will first convert './protocols/file2' into absolute path relative to the location of containing file: - * '/local/src/content/protocols/file2' and try to load it - failure. - * Then it will search 'rootDirs' looking for a longest matching prefix of this absolute path and if such prefix is found - absolute path will - * be converted to a path relative to found rootDir entry './content/protocols/file2' (*). As a last step compiler will check all remaining - * entries in 'rootDirs', use them to build absolute path out of (*) and try to resolve module from this location. - */ - function tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state) { - if (moduleHasNonRelativeName(moduleName)) { - return tryLoadModuleUsingBaseUrl(moduleName, loader, failedLookupLocations, supportedExtensions, state); - } - else { - return tryLoadModuleUsingRootDirs(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state); - } - } - function tryLoadModuleUsingRootDirs(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state) { - if (!state.compilerOptions.rootDirs) { - return undefined; - } - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.rootDirs_option_is_set_using_it_to_resolve_relative_module_name_0, moduleName); - } - var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); - var matchedRootDir; - var matchedNormalizedPrefix; - for (var _i = 0, _a = state.compilerOptions.rootDirs; _i < _a.length; _i++) { - var rootDir = _a[_i]; - // rootDirs are expected to be absolute - // in case of tsconfig.json this will happen automatically - compiler will expand relative names - // using location of tsconfig.json as base location - var normalizedRoot = ts.normalizePath(rootDir); - if (!ts.endsWith(normalizedRoot, ts.directorySeparator)) { - normalizedRoot += ts.directorySeparator; + function shouldWriteLeadingLineTerminator(parentNode, children, format) { + if (format & 1 /* MultiLine */) { + return true; } - var isLongestMatchingPrefix = ts.startsWith(candidate, normalizedRoot) && - (matchedNormalizedPrefix === undefined || matchedNormalizedPrefix.length < normalizedRoot.length); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Checking_if_0_is_the_longest_matching_prefix_for_1_2, normalizedRoot, candidate, isLongestMatchingPrefix); + if (format & 2 /* PreserveLines */) { + if (format & 32768 /* PreferNewLine */) { + return true; + } + var firstChild = children[0]; + if (firstChild === undefined) { + return !ts.rangeIsOnSingleLine(parentNode, currentSourceFile); + } + else if (ts.positionIsSynthesized(parentNode.pos) || ts.nodeIsSynthesized(firstChild)) { + return synthesizedNodeStartsOnNewLine(firstChild, format); + } + else { + return !ts.rangeStartPositionsAreOnSameLine(parentNode, firstChild, currentSourceFile); + } } - if (isLongestMatchingPrefix) { - matchedNormalizedPrefix = normalizedRoot; - matchedRootDir = rootDir; + else { + return false; } } - if (matchedNormalizedPrefix) { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Longest_matching_prefix_for_0_is_1, candidate, matchedNormalizedPrefix); + function shouldWriteSeparatingLineTerminator(previousNode, nextNode, format) { + if (format & 1 /* MultiLine */) { + return true; } - var suffix = candidate.substr(matchedNormalizedPrefix.length); - // first - try to load from a initial location - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Loading_0_from_the_root_dir_1_candidate_location_2, suffix, matchedNormalizedPrefix, candidate); + else if (format & 2 /* PreserveLines */) { + if (previousNode === undefined || nextNode === undefined) { + return false; + } + else if (ts.nodeIsSynthesized(previousNode) || ts.nodeIsSynthesized(nextNode)) { + return synthesizedNodeStartsOnNewLine(previousNode, format) || synthesizedNodeStartsOnNewLine(nextNode, format); + } + else { + return !ts.rangeEndIsOnSameLineAsRangeStart(previousNode, nextNode, currentSourceFile); + } } - var resolvedFileName = loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(containingDirectory, state.host), state); - if (resolvedFileName) { - return resolvedFileName; + else { + return nextNode.startsOnNewLine; } - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Trying_other_entries_in_rootDirs); + } + function shouldWriteClosingLineTerminator(parentNode, children, format) { + if (format & 1 /* MultiLine */) { + return (format & 65536 /* NoTrailingNewLine */) === 0; } - // then try to resolve using remaining entries in rootDirs - for (var _b = 0, _c = state.compilerOptions.rootDirs; _b < _c.length; _b++) { - var rootDir = _c[_b]; - if (rootDir === matchedRootDir) { - // skip the initially matched entry - continue; + else if (format & 2 /* PreserveLines */) { + if (format & 32768 /* PreferNewLine */) { + return true; } - var candidate_1 = ts.combinePaths(ts.normalizePath(rootDir), suffix); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Loading_0_from_the_root_dir_1_candidate_location_2, suffix, rootDir, candidate_1); + var lastChild = ts.lastOrUndefined(children); + if (lastChild === undefined) { + return !ts.rangeIsOnSingleLine(parentNode, currentSourceFile); } - var baseDirectory = ts.getDirectoryPath(candidate_1); - var resolvedFileName_1 = loader(candidate_1, supportedExtensions, failedLookupLocations, !directoryProbablyExists(baseDirectory, state.host), state); - if (resolvedFileName_1) { - return resolvedFileName_1; + else if (ts.positionIsSynthesized(parentNode.pos) || ts.nodeIsSynthesized(lastChild)) { + return synthesizedNodeStartsOnNewLine(lastChild, format); + } + else { + return !ts.rangeEndPositionsAreOnSameLine(parentNode, lastChild, currentSourceFile); } } - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Module_resolution_using_rootDirs_has_failed); + else { + return false; } } - return undefined; - } - function tryLoadModuleUsingBaseUrl(moduleName, loader, failedLookupLocations, supportedExtensions, state) { - if (!state.compilerOptions.baseUrl) { - return undefined; + function synthesizedNodeStartsOnNewLine(node, format) { + if (ts.nodeIsSynthesized(node)) { + var startsOnNewLine = node.startsOnNewLine; + if (startsOnNewLine === undefined) { + return (format & 32768 /* PreferNewLine */) !== 0; + } + return startsOnNewLine; + } + return (format & 32768 /* PreferNewLine */) !== 0; } - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.baseUrl_option_is_set_to_0_using_this_value_to_resolve_non_relative_module_name_1, state.compilerOptions.baseUrl, moduleName); + function needsIndentation(parent, node1, node2) { + parent = skipSynthesizedParentheses(parent); + node1 = skipSynthesizedParentheses(node1); + node2 = skipSynthesizedParentheses(node2); + // Always use a newline for synthesized code if the synthesizer desires it. + if (node2.startsOnNewLine) { + return true; + } + return !ts.nodeIsSynthesized(parent) + && !ts.nodeIsSynthesized(node1) + && !ts.nodeIsSynthesized(node2) + && !ts.rangeEndIsOnSameLineAsRangeStart(node1, node2, currentSourceFile); } - // string is for exact match - var matchedPattern = undefined; - if (state.compilerOptions.paths) { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.paths_option_is_specified_looking_for_a_pattern_to_match_module_name_0, moduleName); + function skipSynthesizedParentheses(node) { + while (node.kind === 178 /* ParenthesizedExpression */ && ts.nodeIsSynthesized(node)) { + node = node.expression; } - matchedPattern = matchPatternOrExact(ts.getOwnKeys(state.compilerOptions.paths), moduleName); + return node; } - if (matchedPattern) { - var matchedStar = typeof matchedPattern === "string" ? undefined : matchedText(matchedPattern, moduleName); - var matchedPatternText = typeof matchedPattern === "string" ? matchedPattern : patternText(matchedPattern); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Module_name_0_matched_pattern_1, moduleName, matchedPatternText); + function getTextOfNode(node, includeTrivia) { + if (ts.isGeneratedIdentifier(node)) { + return getGeneratedIdentifier(node); } - for (var _i = 0, _a = state.compilerOptions.paths[matchedPatternText]; _i < _a.length; _i++) { - var subst = _a[_i]; - var path = matchedStar ? subst.replace("*", matchedStar) : subst; - var candidate = ts.normalizePath(ts.combinePaths(state.compilerOptions.baseUrl, path)); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Trying_substitution_0_candidate_module_location_Colon_1, subst, path); + else if (ts.isIdentifier(node) && (ts.nodeIsSynthesized(node) || !node.parent)) { + return ts.unescapeIdentifier(node.text); + } + else if (node.kind === 9 /* StringLiteral */ && node.textSourceNode) { + return getTextOfNode(node.textSourceNode, includeTrivia); + } + else if (ts.isLiteralExpression(node) && (ts.nodeIsSynthesized(node) || !node.parent)) { + return node.text; + } + return ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, node, includeTrivia); + } + function getLiteralTextOfNode(node) { + if (node.kind === 9 /* StringLiteral */ && node.textSourceNode) { + var textSourceNode = node.textSourceNode; + if (ts.isIdentifier(textSourceNode)) { + return "\"" + ts.escapeNonAsciiCharacters(ts.escapeString(getTextOfNode(textSourceNode))) + "\""; } - var resolvedFileName = loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(ts.getDirectoryPath(candidate), state.host), state); - if (resolvedFileName) { - return resolvedFileName; + else { + return getLiteralTextOfNode(textSourceNode); } } - return undefined; + return ts.getLiteralText(node, currentSourceFile, languageVersion); } - else { - var candidate = ts.normalizePath(ts.combinePaths(state.compilerOptions.baseUrl, moduleName)); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Resolving_module_name_0_relative_to_base_url_1_2, moduleName, state.compilerOptions.baseUrl, candidate); - } - return loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(ts.getDirectoryPath(candidate), state.host), state); + function isSingleLineEmptyBlock(block) { + return !block.multiLine + && isEmptyBlock(block); } - } - /** - * patternStrings contains both pattern strings (containing "*") and regular strings. - * Return an exact match if possible, or a pattern match, or undefined. - * (These are verified by verifyCompilerOptions to have 0 or 1 "*" characters.) - */ - function matchPatternOrExact(patternStrings, candidate) { - var patterns = []; - for (var _i = 0, patternStrings_1 = patternStrings; _i < patternStrings_1.length; _i++) { - var patternString = patternStrings_1[_i]; - var pattern = tryParsePattern(patternString); - if (pattern) { - patterns.push(pattern); - } - else if (patternString === candidate) { - // pattern was matched as is - no need to search further - return patternString; - } + function isEmptyBlock(block) { + return block.statements.length === 0 + && ts.rangeEndIsOnSameLineAsRangeStart(block, block, currentSourceFile); } - return findBestPatternMatch(patterns, function (_) { return _; }, candidate); - } - function patternText(_a) { - var prefix = _a.prefix, suffix = _a.suffix; - return prefix + "*" + suffix; - } - /** - * Given that candidate matches pattern, returns the text matching the '*'. - * E.g.: matchedText(tryParsePattern("foo*baz"), "foobarbaz") === "bar" - */ - function matchedText(pattern, candidate) { - ts.Debug.assert(isPatternMatch(pattern, candidate)); - return candidate.substr(pattern.prefix.length, candidate.length - pattern.suffix.length); - } - /** Return the object corresponding to the best pattern to match `candidate`. */ - /* @internal */ - function findBestPatternMatch(values, getPattern, candidate) { - var matchedValue = undefined; - // use length of prefix as betterness criteria - var longestMatchPrefixLength = -1; - for (var _i = 0, values_1 = values; _i < values_1.length; _i++) { - var v = values_1[_i]; - var pattern = getPattern(v); - if (isPatternMatch(pattern, candidate) && pattern.prefix.length > longestMatchPrefixLength) { - longestMatchPrefixLength = pattern.prefix.length; - matchedValue = v; + function isUniqueName(name) { + return !resolver.hasGlobalName(name) && + !ts.hasProperty(currentFileIdentifiers, name) && + !ts.hasProperty(generatedNameSet, name); + } + function isUniqueLocalName(name, container) { + for (var node = container; ts.isNodeDescendantOf(node, container); node = node.nextContainer) { + if (node.locals && ts.hasProperty(node.locals, name)) { + // We conservatively include alias symbols to cover cases where they're emitted as locals + if (node.locals[name].flags & (107455 /* Value */ | 1048576 /* ExportValue */ | 8388608 /* Alias */)) { + return false; + } + } } + return true; } - return matchedValue; - } - ts.findBestPatternMatch = findBestPatternMatch; - function isPatternMatch(_a, candidate) { - var prefix = _a.prefix, suffix = _a.suffix; - return candidate.length >= prefix.length + suffix.length && - ts.startsWith(candidate, prefix) && - ts.endsWith(candidate, suffix); - } - /* @internal */ - function tryParsePattern(pattern) { - // This should be verified outside of here and a proper error thrown. - ts.Debug.assert(hasZeroOrOneAsteriskCharacter(pattern)); - var indexOfStar = pattern.indexOf("*"); - return indexOfStar === -1 ? undefined : { - prefix: pattern.substr(0, indexOfStar), - suffix: pattern.substr(indexOfStar + 1) - }; - } - ts.tryParsePattern = tryParsePattern; - function nodeModuleNameResolver(moduleName, containingFile, compilerOptions, host) { - var containingDirectory = ts.getDirectoryPath(containingFile); - var supportedExtensions = ts.getSupportedExtensions(compilerOptions); - var traceEnabled = isTraceEnabled(compilerOptions, host); - var failedLookupLocations = []; - var state = { compilerOptions: compilerOptions, host: host, traceEnabled: traceEnabled, skipTsx: false }; - var resolvedFileName = tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, nodeLoadModuleByRelativeName, failedLookupLocations, supportedExtensions, state); - var isExternalLibraryImport = false; - if (!resolvedFileName) { - if (moduleHasNonRelativeName(moduleName)) { - if (traceEnabled) { - trace(host, ts.Diagnostics.Loading_module_0_from_node_modules_folder, moduleName); + /** + * Return the next available name in the pattern _a ... _z, _0, _1, ... + * TempFlags._i or TempFlags._n may be used to express a preference for that dedicated name. + * Note that names generated by makeTempVariableName and makeUniqueName will never conflict. + */ + function makeTempVariableName(flags) { + if (flags && !(tempFlags & flags)) { + var name_41 = flags === 268435456 /* _i */ ? "_i" : "_n"; + if (isUniqueName(name_41)) { + tempFlags |= flags; + return name_41; } - resolvedFileName = loadModuleFromNodeModules(moduleName, containingDirectory, failedLookupLocations, state); - isExternalLibraryImport = resolvedFileName !== undefined; } - else { - var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); - resolvedFileName = nodeLoadModuleByRelativeName(candidate, supportedExtensions, failedLookupLocations, /*onlyRecordFailures*/ false, state); + while (true) { + var count = tempFlags & 268435455 /* CountMask */; + tempFlags++; + // Skip over 'i' and 'n' + if (count !== 8 && count !== 13) { + var name_42 = count < 26 + ? "_" + String.fromCharCode(97 /* a */ + count) + : "_" + (count - 26); + if (isUniqueName(name_42)) { + return name_42; + } + } } } - if (resolvedFileName && host.realpath) { - var originalFileName = resolvedFileName; - resolvedFileName = ts.normalizePath(host.realpath(resolvedFileName)); - if (traceEnabled) { - trace(host, ts.Diagnostics.Resolving_real_path_for_0_result_1, originalFileName, resolvedFileName); + // Generate a name that is unique within the current file and doesn't conflict with any names + // in global scope. The name is formed by adding an '_n' suffix to the specified base name, + // where n is a positive integer. Note that names generated by makeTempVariableName and + // makeUniqueName are guaranteed to never conflict. + function makeUniqueName(baseName) { + // Find the first unique 'name_n', where n is a positive number + if (baseName.charCodeAt(baseName.length - 1) !== 95 /* _ */) { + baseName += "_"; } - } - return createResolvedModule(resolvedFileName, isExternalLibraryImport, failedLookupLocations); - } - ts.nodeModuleNameResolver = nodeModuleNameResolver; - function nodeLoadModuleByRelativeName(candidate, supportedExtensions, failedLookupLocations, onlyRecordFailures, state) { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Loading_module_as_file_Slash_folder_candidate_module_location_0, candidate); - } - var resolvedFileName = !ts.pathEndsWithDirectorySeparator(candidate) && loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, onlyRecordFailures, state); - return resolvedFileName || loadNodeModuleFromDirectory(supportedExtensions, candidate, failedLookupLocations, onlyRecordFailures, state); - } - /* @internal */ - function directoryProbablyExists(directoryName, host) { - // if host does not support 'directoryExists' assume that directory will exist - return !host.directoryExists || host.directoryExists(directoryName); - } - ts.directoryProbablyExists = directoryProbablyExists; - /** - * @param {boolean} onlyRecordFailures - if true then function won't try to actually load files but instead record all attempts as failures. This flag is necessary - * in cases when we know upfront that all load attempts will fail (because containing folder does not exists) however we still need to record all failed lookup locations. - */ - function loadModuleFromFile(candidate, extensions, failedLookupLocation, onlyRecordFailures, state) { - // First, try adding an extension. An import of "foo" could be matched by a file "foo.ts", or "foo.js" by "foo.js.ts" - var resolvedByAddingExtension = tryAddingExtensions(candidate, extensions, failedLookupLocation, onlyRecordFailures, state); - if (resolvedByAddingExtension) { - return resolvedByAddingExtension; - } - // If that didn't work, try stripping a ".js" or ".jsx" extension and replacing it with a TypeScript one; - // e.g. "./foo.js" can be matched by "./foo.ts" or "./foo.d.ts" - if (ts.hasJavaScriptFileExtension(candidate)) { - var extensionless = ts.removeFileExtension(candidate); - if (state.traceEnabled) { - var extension = candidate.substring(extensionless.length); - trace(state.host, ts.Diagnostics.File_name_0_has_a_1_extension_stripping_it, candidate, extension); + var i = 1; + while (true) { + var generatedName = baseName + i; + if (isUniqueName(generatedName)) { + return generatedNameSet[generatedName] = generatedName; + } + i++; } - return tryAddingExtensions(extensionless, extensions, failedLookupLocation, onlyRecordFailures, state); } - } - /** Try to return an existing file that adds one of the `extensions` to `candidate`. */ - function tryAddingExtensions(candidate, extensions, failedLookupLocation, onlyRecordFailures, state) { - if (!onlyRecordFailures) { - // check if containing folder exists - if it doesn't then just record failures for all supported extensions without disk probing - var directory = ts.getDirectoryPath(candidate); - if (directory) { - onlyRecordFailures = !directoryProbablyExists(directory, state.host); - } + function generateNameForModuleOrEnum(node) { + var name = getTextOfNode(node.name); + // Use module/enum name itself if it is unique, otherwise make a unique variation + return isUniqueLocalName(name, node) ? name : makeUniqueName(name); } - return ts.forEach(extensions, function (ext) { - return !(state.skipTsx && ts.isJsxOrTsxExtension(ext)) && tryFile(candidate + ext, failedLookupLocation, onlyRecordFailures, state); - }); - } - /** Return the file if it exists. */ - function tryFile(fileName, failedLookupLocation, onlyRecordFailures, state) { - if (!onlyRecordFailures && state.host.fileExists(fileName)) { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.File_0_exist_use_it_as_a_name_resolution_result, fileName); - } - return fileName; + function generateNameForImportOrExportDeclaration(node) { + var expr = ts.getExternalModuleName(node); + var baseName = expr.kind === 9 /* StringLiteral */ ? + ts.escapeIdentifier(ts.makeIdentifierFromModuleName(expr.text)) : "module"; + return makeUniqueName(baseName); } - else { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.File_0_does_not_exist, fileName); + function generateNameForExportDefault() { + return makeUniqueName("default"); + } + function generateNameForClassExpression() { + return makeUniqueName("class"); + } + /** + * Generates a unique name from a node. + * + * @param node A node. + */ + function generateNameForNode(node) { + switch (node.kind) { + case 69 /* Identifier */: + return makeUniqueName(getTextOfNode(node)); + case 225 /* ModuleDeclaration */: + case 224 /* EnumDeclaration */: + return generateNameForModuleOrEnum(node); + case 230 /* ImportDeclaration */: + case 236 /* ExportDeclaration */: + return generateNameForImportOrExportDeclaration(node); + case 220 /* FunctionDeclaration */: + case 221 /* ClassDeclaration */: + case 235 /* ExportAssignment */: + return generateNameForExportDefault(); + case 192 /* ClassExpression */: + return generateNameForClassExpression(); + default: + return makeTempVariableName(0 /* Auto */); } - failedLookupLocation.push(fileName); - return undefined; } - } - function loadNodeModuleFromDirectory(extensions, candidate, failedLookupLocation, onlyRecordFailures, state) { - var packageJsonPath = pathToPackageJson(candidate); - var directoryExists = !onlyRecordFailures && directoryProbablyExists(candidate, state.host); - if (directoryExists && state.host.fileExists(packageJsonPath)) { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Found_package_json_at_0, packageJsonPath); + /** + * Generates a unique identifier for a node. + * + * @param name A generated name. + */ + function generateName(name) { + switch (name.autoGenerateKind) { + case 1 /* Auto */: + return makeTempVariableName(0 /* Auto */); + case 2 /* Loop */: + return makeTempVariableName(268435456 /* _i */); + case 3 /* Unique */: + return makeUniqueName(name.text); } - var typesFile = tryReadTypesSection(packageJsonPath, candidate, state); - if (typesFile) { - var onlyRecordFailures_1 = !directoryProbablyExists(ts.getDirectoryPath(typesFile), state.host); - // A package.json "typings" may specify an exact filename, or may choose to omit an extension. - var result = tryFile(typesFile, failedLookupLocation, onlyRecordFailures_1, state) || - tryAddingExtensions(typesFile, extensions, failedLookupLocation, onlyRecordFailures_1, state); - if (result) { - return result; + ts.Debug.fail("Unsupported GeneratedIdentifierKind."); + } + /** + * Gets the node from which a name should be generated. + * + * @param name A generated name wrapper. + */ + function getNodeForGeneratedName(name) { + var autoGenerateId = name.autoGenerateId; + var node = name; + var original = node.original; + while (original) { + node = original; + // if "node" is a different generated name (having a different + // "autoGenerateId"), use it and stop traversing. + if (ts.isIdentifier(node) + && node.autoGenerateKind === 4 /* Node */ + && node.autoGenerateId !== autoGenerateId) { + break; } + original = node.original; + } + // otherwise, return the original node for the source; + return node; + } + /** + * Gets the generated identifier text from a generated identifier. + * + * @param name The generated identifier. + */ + function getGeneratedIdentifier(name) { + if (name.autoGenerateKind === 4 /* Node */) { + // Generated names generate unique names based on their original node + // and are cached based on that node's id + var node = getNodeForGeneratedName(name); + var nodeId = ts.getNodeId(node); + return nodeIdToGeneratedName[nodeId] || (nodeIdToGeneratedName[nodeId] = ts.unescapeIdentifier(generateNameForNode(node))); } else { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.package_json_does_not_have_types_field); - } + // Auto, Loop, and Unique names are cached based on their unique + // autoGenerateId. + var autoGenerateId = name.autoGenerateId; + return autoGeneratedIdToGeneratedName[autoGenerateId] || (autoGeneratedIdToGeneratedName[autoGenerateId] = ts.unescapeIdentifier(generateName(name))); } } - else { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.File_0_does_not_exist, packageJsonPath); - } - // record package json as one of failed lookup locations - in the future if this file will appear it will invalidate resolution results - failedLookupLocation.push(packageJsonPath); + function createDelimiterMap() { + var delimiters = []; + delimiters[0 /* None */] = ""; + delimiters[16 /* CommaDelimited */] = ","; + delimiters[4 /* BarDelimited */] = " |"; + delimiters[8 /* AmpersandDelimited */] = " &"; + return delimiters; } - return loadModuleFromFile(ts.combinePaths(candidate, "index"), extensions, failedLookupLocation, !directoryExists, state); - } - function pathToPackageJson(directory) { - return ts.combinePaths(directory, "package.json"); - } - function loadModuleFromNodeModulesFolder(moduleName, directory, failedLookupLocations, state) { - var nodeModulesFolder = ts.combinePaths(directory, "node_modules"); - var nodeModulesFolderExists = directoryProbablyExists(nodeModulesFolder, state.host); - var candidate = ts.normalizePath(ts.combinePaths(nodeModulesFolder, moduleName)); - var supportedExtensions = ts.getSupportedExtensions(state.compilerOptions); - var result = loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, !nodeModulesFolderExists, state); - if (result) { - return result; + function getDelimiter(format) { + return delimiters[format & 28 /* DelimitersMask */]; } - result = loadNodeModuleFromDirectory(supportedExtensions, candidate, failedLookupLocations, !nodeModulesFolderExists, state); - if (result) { - return result; + function createBracketsMap() { + var brackets = []; + brackets[512 /* Braces */] = ["{", "}"]; + brackets[1024 /* Parenthesis */] = ["(", ")"]; + brackets[2048 /* AngleBrackets */] = ["<", ">"]; + brackets[4096 /* SquareBrackets */] = ["[", "]"]; + return brackets; + } + function getOpeningBracket(format) { + return brackets[format & 7680 /* BracketsMask */][0]; + } + function getClosingBracket(format) { + return brackets[format & 7680 /* BracketsMask */][1]; } } - function loadModuleFromNodeModules(moduleName, directory, failedLookupLocations, state) { - directory = ts.normalizeSlashes(directory); + ts.emitFiles = emitFiles; + var ListFormat; + (function (ListFormat) { + ListFormat[ListFormat["None"] = 0] = "None"; + // Line separators + ListFormat[ListFormat["SingleLine"] = 0] = "SingleLine"; + ListFormat[ListFormat["MultiLine"] = 1] = "MultiLine"; + ListFormat[ListFormat["PreserveLines"] = 2] = "PreserveLines"; + ListFormat[ListFormat["LinesMask"] = 3] = "LinesMask"; + // Delimiters + ListFormat[ListFormat["NotDelimited"] = 0] = "NotDelimited"; + ListFormat[ListFormat["BarDelimited"] = 4] = "BarDelimited"; + ListFormat[ListFormat["AmpersandDelimited"] = 8] = "AmpersandDelimited"; + ListFormat[ListFormat["CommaDelimited"] = 16] = "CommaDelimited"; + ListFormat[ListFormat["DelimitersMask"] = 28] = "DelimitersMask"; + ListFormat[ListFormat["AllowTrailingComma"] = 32] = "AllowTrailingComma"; + // Whitespace + ListFormat[ListFormat["Indented"] = 64] = "Indented"; + ListFormat[ListFormat["SpaceBetweenBraces"] = 128] = "SpaceBetweenBraces"; + ListFormat[ListFormat["SpaceBetweenSiblings"] = 256] = "SpaceBetweenSiblings"; + // Brackets/Braces + ListFormat[ListFormat["Braces"] = 512] = "Braces"; + ListFormat[ListFormat["Parenthesis"] = 1024] = "Parenthesis"; + ListFormat[ListFormat["AngleBrackets"] = 2048] = "AngleBrackets"; + ListFormat[ListFormat["SquareBrackets"] = 4096] = "SquareBrackets"; + ListFormat[ListFormat["BracketsMask"] = 7680] = "BracketsMask"; + ListFormat[ListFormat["OptionalIfUndefined"] = 8192] = "OptionalIfUndefined"; + ListFormat[ListFormat["OptionalIfEmpty"] = 16384] = "OptionalIfEmpty"; + ListFormat[ListFormat["Optional"] = 24576] = "Optional"; + // Other + ListFormat[ListFormat["PreferNewLine"] = 32768] = "PreferNewLine"; + ListFormat[ListFormat["NoTrailingNewLine"] = 65536] = "NoTrailingNewLine"; + ListFormat[ListFormat["NoInterveningComments"] = 131072] = "NoInterveningComments"; + // Precomputed Formats + ListFormat[ListFormat["Modifiers"] = 256] = "Modifiers"; + ListFormat[ListFormat["HeritageClauses"] = 256] = "HeritageClauses"; + ListFormat[ListFormat["TypeLiteralMembers"] = 65] = "TypeLiteralMembers"; + ListFormat[ListFormat["TupleTypeElements"] = 336] = "TupleTypeElements"; + ListFormat[ListFormat["UnionTypeConstituents"] = 260] = "UnionTypeConstituents"; + ListFormat[ListFormat["IntersectionTypeConstituents"] = 264] = "IntersectionTypeConstituents"; + ListFormat[ListFormat["ObjectBindingPatternElements"] = 432] = "ObjectBindingPatternElements"; + ListFormat[ListFormat["ArrayBindingPatternElements"] = 304] = "ArrayBindingPatternElements"; + ListFormat[ListFormat["ObjectLiteralExpressionProperties"] = 978] = "ObjectLiteralExpressionProperties"; + ListFormat[ListFormat["ArrayLiteralExpressionElements"] = 4466] = "ArrayLiteralExpressionElements"; + ListFormat[ListFormat["CallExpressionArguments"] = 1296] = "CallExpressionArguments"; + ListFormat[ListFormat["NewExpressionArguments"] = 9488] = "NewExpressionArguments"; + ListFormat[ListFormat["TemplateExpressionSpans"] = 131072] = "TemplateExpressionSpans"; + ListFormat[ListFormat["SingleLineBlockStatements"] = 384] = "SingleLineBlockStatements"; + ListFormat[ListFormat["MultiLineBlockStatements"] = 65] = "MultiLineBlockStatements"; + ListFormat[ListFormat["VariableDeclarationList"] = 272] = "VariableDeclarationList"; + ListFormat[ListFormat["SingleLineFunctionBodyStatements"] = 384] = "SingleLineFunctionBodyStatements"; + ListFormat[ListFormat["MultiLineFunctionBodyStatements"] = 1] = "MultiLineFunctionBodyStatements"; + ListFormat[ListFormat["ClassHeritageClauses"] = 256] = "ClassHeritageClauses"; + ListFormat[ListFormat["ClassMembers"] = 65] = "ClassMembers"; + ListFormat[ListFormat["InterfaceMembers"] = 65] = "InterfaceMembers"; + ListFormat[ListFormat["EnumMembers"] = 81] = "EnumMembers"; + ListFormat[ListFormat["CaseBlockClauses"] = 65] = "CaseBlockClauses"; + ListFormat[ListFormat["NamedImportsOrExportsElements"] = 432] = "NamedImportsOrExportsElements"; + ListFormat[ListFormat["JsxElementChildren"] = 131072] = "JsxElementChildren"; + ListFormat[ListFormat["JsxElementAttributes"] = 131328] = "JsxElementAttributes"; + ListFormat[ListFormat["CaseOrDefaultClauseStatements"] = 81985] = "CaseOrDefaultClauseStatements"; + ListFormat[ListFormat["HeritageClauseTypes"] = 272] = "HeritageClauseTypes"; + ListFormat[ListFormat["SourceFileStatements"] = 65537] = "SourceFileStatements"; + ListFormat[ListFormat["Decorators"] = 24577] = "Decorators"; + ListFormat[ListFormat["TypeArguments"] = 26960] = "TypeArguments"; + ListFormat[ListFormat["TypeParameters"] = 26960] = "TypeParameters"; + ListFormat[ListFormat["Parameters"] = 1360] = "Parameters"; + ListFormat[ListFormat["IndexSignatureParameters"] = 4432] = "IndexSignatureParameters"; + })(ListFormat || (ListFormat = {})); +})(ts || (ts = {})); +/// +/// +/// +var ts; +(function (ts) { + /** The version of the TypeScript compiler release */ + ts.version = "2.1.0"; + var emptyArray = []; + function findConfigFile(searchPath, fileExists, configName) { + if (configName === void 0) { configName = "tsconfig.json"; } while (true) { - var baseName = ts.getBaseFileName(directory); - if (baseName !== "node_modules") { - // Try to load source from the package - var packageResult = loadModuleFromNodeModulesFolder(moduleName, directory, failedLookupLocations, state); - if (packageResult && ts.hasTypeScriptFileExtension(packageResult)) { - // Always prefer a TypeScript (.ts, .tsx, .d.ts) file shipped with the package - return packageResult; - } - else { - // Else prefer a types package over non-TypeScript results (e.g. JavaScript files) - var typesResult = loadModuleFromNodeModulesFolder(ts.combinePaths("@types", moduleName), directory, failedLookupLocations, state); - if (typesResult || packageResult) { - return typesResult || packageResult; - } - } + var fileName = ts.combinePaths(searchPath, configName); + if (fileExists(fileName)) { + return fileName; } - var parentPath = ts.getDirectoryPath(directory); - if (parentPath === directory) { + var parentPath = ts.getDirectoryPath(searchPath); + if (parentPath === searchPath) { break; } - directory = parentPath; + searchPath = parentPath; } return undefined; } - function classicNameResolver(moduleName, containingFile, compilerOptions, host) { - var traceEnabled = isTraceEnabled(compilerOptions, host); - var state = { compilerOptions: compilerOptions, host: host, traceEnabled: traceEnabled, skipTsx: !compilerOptions.jsx }; - var failedLookupLocations = []; - var supportedExtensions = ts.getSupportedExtensions(compilerOptions); - var containingDirectory = ts.getDirectoryPath(containingFile); - var resolvedFileName = tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, loadModuleFromFile, failedLookupLocations, supportedExtensions, state); - if (resolvedFileName) { - return createResolvedModule(resolvedFileName, /*isExternalLibraryImport*/ false, failedLookupLocations); - } - var referencedSourceFile; - if (moduleHasNonRelativeName(moduleName)) { - while (true) { - var searchName = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); - referencedSourceFile = loadModuleFromFile(searchName, supportedExtensions, failedLookupLocations, /*onlyRecordFailures*/ false, state); - if (referencedSourceFile) { - break; - } - var parentPath = ts.getDirectoryPath(containingDirectory); - if (parentPath === containingDirectory) { + ts.findConfigFile = findConfigFile; + function resolveTripleslashReference(moduleName, containingFile) { + var basePath = ts.getDirectoryPath(containingFile); + var referencedFileName = ts.isRootedDiskPath(moduleName) ? moduleName : ts.combinePaths(basePath, moduleName); + return ts.normalizePath(referencedFileName); + } + ts.resolveTripleslashReference = resolveTripleslashReference; + /* @internal */ + function computeCommonSourceDirectoryOfFilenames(fileNames, currentDirectory, getCanonicalFileName) { + var commonPathComponents; + var failed = ts.forEach(fileNames, function (sourceFile) { + // Each file contributes into common source file path + var sourcePathComponents = ts.getNormalizedPathComponents(sourceFile, currentDirectory); + sourcePathComponents.pop(); // The base file name is not part of the common directory path + if (!commonPathComponents) { + // first file + commonPathComponents = sourcePathComponents; + return; + } + for (var i = 0, n = Math.min(commonPathComponents.length, sourcePathComponents.length); i < n; i++) { + if (getCanonicalFileName(commonPathComponents[i]) !== getCanonicalFileName(sourcePathComponents[i])) { + if (i === 0) { + // Failed to find any common path component + return true; + } + // New common path found that is 0 -> i-1 + commonPathComponents.length = i; break; } - containingDirectory = parentPath; } + // If the sourcePathComponents was shorter than the commonPathComponents, truncate to the sourcePathComponents + if (sourcePathComponents.length < commonPathComponents.length) { + commonPathComponents.length = sourcePathComponents.length; + } + }); + // A common path can not be found when paths span multiple drives on windows, for example + if (failed) { + return ""; } - else { - var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); - referencedSourceFile = loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, /*onlyRecordFailures*/ false, state); + if (!commonPathComponents) { + return currentDirectory; } - return referencedSourceFile - ? { resolvedModule: { resolvedFileName: referencedSourceFile }, failedLookupLocations: failedLookupLocations } - : { resolvedModule: undefined, failedLookupLocations: failedLookupLocations }; + return ts.getNormalizedPathFromPathComponents(commonPathComponents); } - ts.classicNameResolver = classicNameResolver; + ts.computeCommonSourceDirectoryOfFilenames = computeCommonSourceDirectoryOfFilenames; function createCompilerHost(options, setParentNodes) { var existingDirectories = ts.createMap(); function getCanonicalFileName(fileName) { @@ -57672,7 +58101,7 @@ var ts; readFile: function (fileName) { return ts.sys.readFile(fileName); }, trace: function (s) { return ts.sys.write(s + newLine); }, directoryExists: function (directoryName) { return ts.sys.directoryExists(directoryName); }, - getEnvironmentVariable: function (name) { return ts.getEnvironmentVariable(name, /*host*/ undefined); }, + getEnvironmentVariable: function (name) { return ts.sys.getEnvironmentVariable ? ts.sys.getEnvironmentVariable(name) : ""; }, getDirectories: function (path) { return ts.sys.getDirectories(path); }, realpath: realpath }; @@ -57740,45 +58169,6 @@ var ts; } return resolutions; } - /** - * Given a set of options, returns the set of type directive names - * that should be included for this program automatically. - * This list could either come from the config file, - * or from enumerating the types root + initial secondary types lookup location. - * More type directives might appear in the program later as a result of loading actual source files; - * this list is only the set of defaults that are implicitly included. - */ - function getAutomaticTypeDirectiveNames(options, host) { - // Use explicit type list from tsconfig.json - if (options.types) { - return options.types; - } - // Walk the primary type lookup locations - var result = []; - if (host.directoryExists && host.getDirectories) { - var typeRoots = getEffectiveTypeRoots(options, host); - if (typeRoots) { - for (var _i = 0, typeRoots_1 = typeRoots; _i < typeRoots_1.length; _i++) { - var root = typeRoots_1[_i]; - if (host.directoryExists(root)) { - for (var _a = 0, _b = host.getDirectories(root); _a < _b.length; _a++) { - var typeDirectivePath = _b[_a]; - var normalized = ts.normalizePath(typeDirectivePath); - var packageJsonPath = pathToPackageJson(ts.combinePaths(root, normalized)); - // tslint:disable-next-line:no-null-keyword - var isNotNeededPackage = host.fileExists(packageJsonPath) && readJson(packageJsonPath, host).typings === null; - if (!isNotNeededPackage) { - // Return just the type directive names - result.push(ts.getBaseFileName(normalized)); - } - } - } - } - } - } - return result; - } - ts.getAutomaticTypeDirectiveNames = getAutomaticTypeDirectiveNames; function createProgram(rootNames, options, host, oldProgram) { var program; var files = []; @@ -57815,7 +58205,7 @@ var ts; resolveModuleNamesWorker = function (moduleNames, containingFile) { return host.resolveModuleNames(moduleNames, containingFile); }; } else { - var loader_1 = function (moduleName, containingFile) { return resolveModuleName(moduleName, containingFile, options, host).resolvedModule; }; + var loader_1 = function (moduleName, containingFile) { return ts.resolveModuleName(moduleName, containingFile, options, host).resolvedModule; }; resolveModuleNamesWorker = function (moduleNames, containingFile) { return loadWithLocalCache(moduleNames, containingFile, loader_1); }; } var resolveTypeReferenceDirectiveNamesWorker; @@ -57823,7 +58213,7 @@ var ts; resolveTypeReferenceDirectiveNamesWorker = function (typeDirectiveNames, containingFile) { return host.resolveTypeReferenceDirectives(typeDirectiveNames, containingFile); }; } else { - var loader_2 = function (typesRef, containingFile) { return resolveTypeReferenceDirective(typesRef, containingFile, options, host).resolvedTypeReferenceDirective; }; + var loader_2 = function (typesRef, containingFile) { return ts.resolveTypeReferenceDirective(typesRef, containingFile, options, host).resolvedTypeReferenceDirective; }; resolveTypeReferenceDirectiveNamesWorker = function (typeReferenceDirectiveNames, containingFile) { return loadWithLocalCache(typeReferenceDirectiveNames, containingFile, loader_2); }; } var filesByName = ts.createFileMap(); @@ -57833,8 +58223,8 @@ var ts; if (!tryReuseStructureFromOldProgram()) { ts.forEach(rootNames, function (name) { return processRootFile(name, /*isDefaultLib*/ false); }); // load type declarations specified via 'types' argument or implicitly from types/ and node_modules/@types folders - var typeReferences = getAutomaticTypeDirectiveNames(options, host); - if (typeReferences) { + var typeReferences = ts.getAutomaticTypeDirectiveNames(options, host); + if (typeReferences.length) { // This containingFilename needs to match with the one used in managed-side var containingFilename = ts.combinePaths(host.getCurrentDirectory(), "__inferred type names__.ts"); var resolutions = resolveTypeReferenceDirectiveNamesWorker(typeReferences, containingFilename); @@ -57884,7 +58274,8 @@ var ts; getSymbolCount: function () { return getDiagnosticsProducingTypeChecker().getSymbolCount(); }, getTypeCount: function () { return getDiagnosticsProducingTypeChecker().getTypeCount(); }, getFileProcessingDiagnostics: function () { return fileProcessingDiagnostics; }, - getResolvedTypeReferenceDirectives: function () { return resolvedTypeReferenceDirectives; } + getResolvedTypeReferenceDirectives: function () { return resolvedTypeReferenceDirectives; }, + dropDiagnosticsProducingTypeChecker: dropDiagnosticsProducingTypeChecker }; verifyCompilerOptions(); ts.performance.mark("afterProgram"); @@ -57938,6 +58329,7 @@ var ts; (oldOptions.configFilePath !== options.configFilePath) || (oldOptions.baseUrl !== options.baseUrl) || (oldOptions.maxNodeModuleJsDepth !== options.maxNodeModuleJsDepth) || + !ts.arrayIsEqualTo(oldOptions.lib, options.lib) || !ts.arrayIsEqualTo(oldOptions.typeRoots, oldOptions.typeRoots) || !ts.arrayIsEqualTo(oldOptions.rootDirs, options.rootDirs) || !ts.equalOwnProperties(oldOptions.paths, options.paths)) { @@ -58054,16 +58446,19 @@ var ts; function getDiagnosticsProducingTypeChecker() { return diagnosticsProducingTypeChecker || (diagnosticsProducingTypeChecker = ts.createTypeChecker(program, /*produceDiagnostics:*/ true)); } + function dropDiagnosticsProducingTypeChecker() { + diagnosticsProducingTypeChecker = undefined; + } function getTypeChecker() { return noDiagnosticsTypeChecker || (noDiagnosticsTypeChecker = ts.createTypeChecker(program, /*produceDiagnostics:*/ false)); } - function emit(sourceFile, writeFileCallback, cancellationToken) { - return runWithCancellationToken(function () { return emitWorker(program, sourceFile, writeFileCallback, cancellationToken); }); + function emit(sourceFile, writeFileCallback, cancellationToken, emitOnlyDtsFiles) { + return runWithCancellationToken(function () { return emitWorker(program, sourceFile, writeFileCallback, cancellationToken, emitOnlyDtsFiles); }); } function isEmitBlocked(emitFileName) { return hasEmitBlockingDiagnostics.contains(ts.toPath(emitFileName, currentDirectory, getCanonicalFileName)); } - function emitWorker(program, sourceFile, writeFileCallback, cancellationToken) { + function emitWorker(program, sourceFile, writeFileCallback, cancellationToken, emitOnlyDtsFiles) { var declarationDiagnostics = []; if (options.noEmit) { return { diagnostics: declarationDiagnostics, sourceMaps: undefined, emittedFiles: undefined, emitSkipped: true }; @@ -58095,7 +58490,7 @@ var ts; // checked is to not pass the file to getEmitResolver. var emitResolver = getDiagnosticsProducingTypeChecker().getEmitResolver((options.outFile || options.out) ? undefined : sourceFile); ts.performance.mark("beforeEmit"); - var emitResult = ts.emitFiles(emitResolver, getEmitHost(writeFileCallback), sourceFile); + var emitResult = ts.emitFiles(emitResolver, getEmitHost(writeFileCallback), sourceFile, emitOnlyDtsFiles); ts.performance.mark("afterEmit"); ts.performance.measure("Emit", "beforeEmit", "afterEmit"); return emitResult; @@ -58659,7 +59054,6 @@ var ts; for (var i = 0; i < moduleNames.length; i++) { var resolution = resolutions[i]; ts.setResolvedModule(file, moduleNames[i], resolution); - var resolvedPath = resolution ? ts.toPath(resolution.resolvedFileName, currentDirectory, getCanonicalFileName) : undefined; // add file to program only if: // - resolution was successful // - noResolve is falsy @@ -58676,7 +59070,7 @@ var ts; modulesWithElidedImports[file.path] = true; } else if (shouldAddFile) { - findSourceFile(resolution.resolvedFileName, resolvedPath, + findSourceFile(resolution.resolvedFileName, ts.toPath(resolution.resolvedFileName, currentDirectory, getCanonicalFileName), /*isDefaultLib*/ false, /*isReference*/ false, file, ts.skipTrivia(file.text, file.imports[i].pos), file.imports[i].end); } if (isFromNodeModulesSearch) { @@ -58692,8 +59086,8 @@ var ts; } function computeCommonSourceDirectory(sourceFiles) { var fileNames = []; - for (var _i = 0, sourceFiles_5 = sourceFiles; _i < sourceFiles_5.length; _i++) { - var file = sourceFiles_5[_i]; + for (var _i = 0, sourceFiles_6 = sourceFiles; _i < sourceFiles_6.length; _i++) { + var file = sourceFiles_6[_i]; if (!file.isDeclarationFile) { fileNames.push(file.fileName); } @@ -58704,8 +59098,8 @@ var ts; var allFilesBelongToPath = true; if (sourceFiles) { var absoluteRootDirectoryPath = host.getCanonicalFileName(ts.getNormalizedAbsolutePath(rootDirectory, currentDirectory)); - for (var _i = 0, sourceFiles_6 = sourceFiles; _i < sourceFiles_6.length; _i++) { - var sourceFile = sourceFiles_6[_i]; + for (var _i = 0, sourceFiles_7 = sourceFiles; _i < sourceFiles_7.length; _i++) { + var sourceFile = sourceFiles_7[_i]; if (!ts.isDeclarationFile(sourceFile)) { var absoluteSourceFilePath = host.getCanonicalFileName(ts.getNormalizedAbsolutePath(sourceFile.fileName, currentDirectory)); if (absoluteSourceFilePath.indexOf(absoluteRootDirectoryPath) !== 0) { @@ -58748,7 +59142,7 @@ var ts; if (!ts.hasProperty(options.paths, key)) { continue; } - if (!hasZeroOrOneAsteriskCharacter(key)) { + if (!ts.hasZeroOrOneAsteriskCharacter(key)) { programDiagnostics.add(ts.createCompilerDiagnostic(ts.Diagnostics.Pattern_0_can_have_at_most_one_Asterisk_character, key)); } if (ts.isArray(options.paths[key])) { @@ -58759,7 +59153,7 @@ var ts; var subst = _a[_i]; var typeOfSubst = typeof subst; if (typeOfSubst === "string") { - if (!hasZeroOrOneAsteriskCharacter(subst)) { + if (!ts.hasZeroOrOneAsteriskCharacter(subst)) { programDiagnostics.add(ts.createCompilerDiagnostic(ts.Diagnostics.Substitution_0_in_pattern_1_in_can_have_at_most_one_Asterisk_character, subst, key)); } } @@ -58799,6 +59193,9 @@ var ts; if (options.lib && options.noLib) { programDiagnostics.add(ts.createCompilerDiagnostic(ts.Diagnostics.Option_0_cannot_be_specified_with_option_1, "lib", "noLib")); } + if (options.noImplicitUseStrict && options.alwaysStrict) { + programDiagnostics.add(ts.createCompilerDiagnostic(ts.Diagnostics.Option_0_cannot_be_specified_with_option_1, "noImplicitUseStrict", "alwaysStrict")); + } var languageVersion = options.target || 0 /* ES3 */; var outFile = options.outFile || options.out; var firstNonAmbientExternalModuleSourceFile = ts.forEach(files, function (f) { return ts.isExternalModule(f) && !ts.isDeclarationFile(f) ? f : undefined; }); @@ -58891,12 +59288,15 @@ var ts; /// var ts; (function (ts) { + /* @internal */ + ts.compileOnSaveCommandLineOption = { name: "compileOnSave", type: "boolean" }; /* @internal */ ts.optionDeclarations = [ { name: "charset", type: "string" }, + ts.compileOnSaveCommandLineOption, { name: "declaration", shortName: "d", @@ -59327,6 +59727,11 @@ var ts; name: "importHelpers", type: "boolean", description: ts.Diagnostics.Import_emit_helpers_from_tslib + }, + { + name: "alwaysStrict", + type: "boolean", + description: ts.Diagnostics.Parse_in_strict_mode_and_emit_use_strict_for_each_source_file } ]; /* @internal */ @@ -59547,10 +59952,11 @@ var ts; * @param fileName The path to the config file * @param jsonText The text of the config file */ - function parseConfigFileTextToJson(fileName, jsonText) { + function parseConfigFileTextToJson(fileName, jsonText, stripComments) { + if (stripComments === void 0) { stripComments = true; } try { - var jsonTextWithoutComments = removeComments(jsonText); - return { config: /\S/.test(jsonTextWithoutComments) ? JSON.parse(jsonTextWithoutComments) : {} }; + var jsonTextToParse = stripComments ? removeComments(jsonText) : jsonText; + return { config: /\S/.test(jsonTextToParse) ? JSON.parse(jsonTextToParse) : {} }; } catch (e) { return { error: ts.createCompilerDiagnostic(ts.Diagnostics.Failed_to_parse_file_0_Colon_1, fileName, e.message) }; @@ -59712,13 +60118,15 @@ var ts; options = ts.extend(existingOptions, options); options.configFilePath = configFileName; var _c = getFileNames(errors), fileNames = _c.fileNames, wildcardDirectories = _c.wildcardDirectories; + var compileOnSave = convertCompileOnSaveOptionFromJson(json, basePath, errors); return { options: options, fileNames: fileNames, typingOptions: typingOptions, raw: json, errors: errors, - wildcardDirectories: wildcardDirectories + wildcardDirectories: wildcardDirectories, + compileOnSave: compileOnSave }; function tryExtendsName(extendedConfig) { // If the path isn't a rooted or relative path, don't try to resolve it (we reserve the right to special case module-id like paths in the future) @@ -59799,6 +60207,17 @@ var ts; var _b; } ts.parseJsonConfigFileContent = parseJsonConfigFileContent; + function convertCompileOnSaveOptionFromJson(jsonOption, basePath, errors) { + if (!ts.hasProperty(jsonOption, ts.compileOnSaveCommandLineOption.name)) { + return false; + } + var result = convertJsonOption(ts.compileOnSaveCommandLineOption, jsonOption["compileOnSave"], basePath, errors); + if (typeof result === "boolean" && result) { + return result; + } + return false; + } + ts.convertCompileOnSaveOptionFromJson = convertCompileOnSaveOptionFromJson; function convertCompilerOptionsFromJson(jsonOptions, basePath, configFileName) { var errors = []; var options = convertCompilerOptionsFromJsonWorker(jsonOptions, basePath, errors, configFileName); @@ -59812,7 +60231,9 @@ var ts; } ts.convertTypingOptionsFromJson = convertTypingOptionsFromJson; function convertCompilerOptionsFromJsonWorker(jsonOptions, basePath, errors, configFileName) { - var options = ts.getBaseFileName(configFileName) === "jsconfig.json" ? { allowJs: true, maxNodeModuleJsDepth: 2 } : {}; + var options = ts.getBaseFileName(configFileName) === "jsconfig.json" + ? { allowJs: true, maxNodeModuleJsDepth: 2, allowSyntheticDefaultImports: true, skipLibCheck: true } + : {}; convertOptionsFromJson(ts.optionDeclarations, jsonOptions, basePath, options, ts.Diagnostics.Unknown_compiler_option_0, errors); return options; } @@ -60742,7 +61163,7 @@ var ts; return true; case 69 /* Identifier */: // 'this' as a parameter - return node.originalKeywordKind === 97 /* ThisKeyword */ && node.parent.kind === 142 /* Parameter */; + return ts.identifierIsThisKeyword(node) && node.parent.kind === 142 /* Parameter */; default: return false; } @@ -61420,7 +61841,6 @@ var ts; })(ts || (ts = {})); // Display-part writer helpers /* @internal */ -var ts; (function (ts) { function isFirstDeclarationOfSymbolParameter(symbol) { return symbol.declarations && symbol.declarations.length > 0 && symbol.declarations[0].kind === 142 /* Parameter */; @@ -61657,7 +62077,7 @@ var ts; return ts.ensureScriptKind(fileName, scriptKind); } ts.getScriptKind = getScriptKind; - function parseAndReEmitConfigJSONFile(content) { + function sanitizeConfigFile(configFileName, content) { var options = { fileName: "config.js", compilerOptions: { @@ -61671,14 +62091,17 @@ var ts; // also, the emitted result will have "(" in the beginning and ");" in the end. We need to strip these // as well var trimmedOutput = outputText.trim(); - var configJsonObject = JSON.parse(trimmedOutput.substring(1, trimmedOutput.length - 2)); for (var _i = 0, diagnostics_2 = diagnostics; _i < diagnostics_2.length; _i++) { var diagnostic = diagnostics_2[_i]; diagnostic.start = diagnostic.start - 1; } - return { configJsonObject: configJsonObject, diagnostics: diagnostics }; + var _b = ts.parseConfigFileTextToJson(configFileName, trimmedOutput.substring(1, trimmedOutput.length - 2), /*stripComments*/ false), config = _b.config, error = _b.error; + return { + configJsonObject: config || {}, + diagnostics: error ? ts.concatenate(diagnostics, [error]) : diagnostics + }; } - ts.parseAndReEmitConfigJSONFile = parseAndReEmitConfigJSONFile; + ts.sanitizeConfigFile = sanitizeConfigFile; })(ts || (ts = {})); var ts; (function (ts) { @@ -62538,8 +62961,7 @@ var ts; return; case 142 /* Parameter */: if (token.parent.name === token) { - var isThis_1 = token.kind === 69 /* Identifier */ && token.originalKeywordKind === 97 /* ThisKeyword */; - return isThis_1 ? 3 /* keyword */ : 17 /* parameterName */; + return ts.isThisIdentifier(token) ? 3 /* keyword */ : 17 /* parameterName */; } return; } @@ -62583,14 +63005,14 @@ var ts; if (!completionData) { return undefined; } - var symbols = completionData.symbols, isMemberCompletion = completionData.isMemberCompletion, isNewIdentifierLocation = completionData.isNewIdentifierLocation, location = completionData.location, isJsDocTagName = completionData.isJsDocTagName; + var symbols = completionData.symbols, isGlobalCompletion = completionData.isGlobalCompletion, isMemberCompletion = completionData.isMemberCompletion, isNewIdentifierLocation = completionData.isNewIdentifierLocation, location = completionData.location, isJsDocTagName = completionData.isJsDocTagName; if (isJsDocTagName) { // If the current position is a jsDoc tag name, only tag names should be provided for completion - return { isMemberCompletion: false, isNewIdentifierLocation: false, entries: ts.JsDoc.getAllJsDocCompletionEntries() }; + return { isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: false, entries: ts.JsDoc.getAllJsDocCompletionEntries() }; } var entries = []; if (ts.isSourceFileJavaScript(sourceFile)) { - var uniqueNames = getCompletionEntriesFromSymbols(symbols, entries, location, /*performCharacterChecks*/ false); + var uniqueNames = getCompletionEntriesFromSymbols(symbols, entries, location, /*performCharacterChecks*/ true); ts.addRange(entries, getJavaScriptCompletionEntries(sourceFile, location.pos, uniqueNames)); } else { @@ -62619,7 +63041,7 @@ var ts; if (!isMemberCompletion && !isJsDocTagName) { ts.addRange(entries, keywordCompletions); } - return { isMemberCompletion: isMemberCompletion, isNewIdentifierLocation: isNewIdentifierLocation || ts.isSourceFileJavaScript(sourceFile), entries: entries }; + return { isGlobalCompletion: isGlobalCompletion, isMemberCompletion: isMemberCompletion, isNewIdentifierLocation: isNewIdentifierLocation, entries: entries }; function getJavaScriptCompletionEntries(sourceFile, position, uniqueNames) { var entries = []; var nameTable = ts.getNameTable(sourceFile); @@ -62690,7 +63112,9 @@ var ts; if (!node || node.kind !== 9 /* StringLiteral */) { return undefined; } - if (node.parent.kind === 253 /* PropertyAssignment */ && node.parent.parent.kind === 171 /* ObjectLiteralExpression */) { + if (node.parent.kind === 253 /* PropertyAssignment */ && + node.parent.parent.kind === 171 /* ObjectLiteralExpression */ && + node.parent.name === node) { // Get quoted name of properties of the object literal expression // i.e. interface ConfigFiles { // 'jspm:dev': string @@ -62740,7 +63164,7 @@ var ts; if (type) { getCompletionEntriesFromSymbols(type.getApparentProperties(), entries, element, /*performCharacterChecks*/ false); if (entries.length) { - return { isMemberCompletion: true, isNewIdentifierLocation: true, entries: entries }; + return { isGlobalCompletion: false, isMemberCompletion: true, isNewIdentifierLocation: true, entries: entries }; } } } @@ -62756,7 +63180,7 @@ var ts; } } if (entries.length) { - return { isMemberCompletion: false, isNewIdentifierLocation: true, entries: entries }; + return { isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: true, entries: entries }; } return undefined; } @@ -62766,7 +63190,7 @@ var ts; if (type) { getCompletionEntriesFromSymbols(type.getApparentProperties(), entries, node, /*performCharacterChecks*/ false); if (entries.length) { - return { isMemberCompletion: true, isNewIdentifierLocation: true, entries: entries }; + return { isGlobalCompletion: false, isMemberCompletion: true, isNewIdentifierLocation: true, entries: entries }; } } return undefined; @@ -62777,7 +63201,7 @@ var ts; var entries_2 = []; addStringLiteralCompletionsFromType(type, entries_2); if (entries_2.length) { - return { isMemberCompletion: false, isNewIdentifierLocation: false, entries: entries_2 }; + return { isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: false, entries: entries_2 }; } } return undefined; @@ -62819,6 +63243,7 @@ var ts; entries = getCompletionEntriesForNonRelativeModules(literalValue, scriptDirectory, span); } return { + isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: true, entries: entries @@ -62854,15 +63279,24 @@ var ts; } return result; } + /** + * Given a path ending at a directory, gets the completions for the path, and filters for those entries containing the basename. + */ function getCompletionEntriesForDirectoryFragment(fragment, scriptPath, extensions, includeExtensions, span, exclude, result) { if (result === void 0) { result = []; } - fragment = ts.getDirectoryPath(fragment); - if (!fragment) { - fragment = "./"; + if (fragment === undefined) { + fragment = ""; } - else { - fragment = ts.ensureTrailingDirectorySeparator(fragment); + fragment = ts.normalizeSlashes(fragment); + /** + * Remove the basename from the path. Note that we don't use the basename to filter completions; + * the client is responsible for refining completions. + */ + fragment = ts.getDirectoryPath(fragment); + if (fragment === "") { + fragment = "." + ts.directorySeparator; } + fragment = ts.ensureTrailingDirectorySeparator(fragment); var absolutePath = normalizeAndPreserveTrailingSlash(ts.isRootedDiskPath(fragment) ? fragment : ts.combinePaths(scriptPath, fragment)); var baseDirectory = ts.getDirectoryPath(absolutePath); var ignoreCase = !(host.useCaseSensitiveFileNames && host.useCaseSensitiveFileNames()); @@ -62870,6 +63304,12 @@ var ts; // Enumerate the available files if possible var files = tryReadDirectory(host, baseDirectory, extensions, /*exclude*/ undefined, /*include*/ ["./*"]); if (files) { + /** + * Multiple file entries might map to the same truncated name once we remove extensions + * (happens iff includeExtensions === false)so we use a set-like data structure. Eg: + * + * both foo.ts and foo.tsx become foo + */ var foundFiles = ts.createMap(); for (var _i = 0, files_3 = files; _i < files_3.length; _i++) { var filePath = files_3[_i]; @@ -63039,6 +63479,19 @@ var ts; if (!range) { return undefined; } + var completionInfo = { + /** + * We don't want the editor to offer any other completions, such as snippets, inside a comment. + */ + isGlobalCompletion: false, + isMemberCompletion: false, + /** + * The user may type in a path that doesn't yet exist, creating a "new identifier" + * with respect to the collection of identifiers the server is aware of. + */ + isNewIdentifierLocation: true, + entries: [] + }; var text = sourceFile.text.substr(range.pos, position - range.pos); var match = tripleSlashDirectiveFragmentRegex.exec(text); if (match) { @@ -63046,24 +63499,18 @@ var ts; var kind = match[2]; var toComplete = match[3]; var scriptPath = ts.getDirectoryPath(sourceFile.path); - var entries_3; if (kind === "path") { // Give completions for a relative path var span_10 = getDirectoryFragmentTextSpan(toComplete, range.pos + prefix.length); - entries_3 = getCompletionEntriesForDirectoryFragment(toComplete, scriptPath, ts.getSupportedExtensions(compilerOptions), /*includeExtensions*/ true, span_10, sourceFile.path); + completionInfo.entries = getCompletionEntriesForDirectoryFragment(toComplete, scriptPath, ts.getSupportedExtensions(compilerOptions), /*includeExtensions*/ true, span_10, sourceFile.path); } else { // Give completions based on the typings available var span_11 = { start: range.pos + prefix.length, length: match[0].length - prefix.length }; - entries_3 = getCompletionEntriesFromTypings(host, compilerOptions, scriptPath, span_11); + completionInfo.entries = getCompletionEntriesFromTypings(host, compilerOptions, scriptPath, span_11); } - return { - isMemberCompletion: false, - isNewIdentifierLocation: true, - entries: entries_3 - }; } - return undefined; + return completionInfo; } function getCompletionEntriesFromTypings(host, options, scriptPath, span, result) { if (result === void 0) { result = []; } @@ -63285,7 +63732,7 @@ var ts; } } if (isJsDocTagName) { - return { symbols: undefined, isMemberCompletion: false, isNewIdentifierLocation: false, location: undefined, isRightOfDot: false, isJsDocTagName: isJsDocTagName }; + return { symbols: undefined, isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: false, location: undefined, isRightOfDot: false, isJsDocTagName: isJsDocTagName }; } if (!insideJsDocTagExpression) { // Proceed if the current position is in jsDoc tag expression; otherwise it is a normal @@ -63349,6 +63796,7 @@ var ts; } } var semanticStart = ts.timestamp(); + var isGlobalCompletion = false; var isMemberCompletion; var isNewIdentifierLocation; var symbols = []; @@ -63382,11 +63830,13 @@ var ts; if (!tryGetGlobalSymbols()) { return undefined; } + isGlobalCompletion = true; } log("getCompletionData: Semantic work: " + (ts.timestamp() - semanticStart)); - return { symbols: symbols, isMemberCompletion: isMemberCompletion, isNewIdentifierLocation: isNewIdentifierLocation, location: location, isRightOfDot: (isRightOfDot || isRightOfOpenTag), isJsDocTagName: isJsDocTagName }; + return { symbols: symbols, isGlobalCompletion: isGlobalCompletion, isMemberCompletion: isMemberCompletion, isNewIdentifierLocation: isNewIdentifierLocation, location: location, isRightOfDot: (isRightOfDot || isRightOfOpenTag), isJsDocTagName: isJsDocTagName }; function getTypeScriptMemberSymbols() { // Right of dot member completion list + isGlobalCompletion = false; isMemberCompletion = true; isNewIdentifierLocation = false; if (node.kind === 69 /* Identifier */ || node.kind === 139 /* QualifiedName */ || node.kind === 172 /* PropertyAccessExpression */) { @@ -63448,6 +63898,7 @@ var ts; if ((jsxContainer.kind === 242 /* JsxSelfClosingElement */) || (jsxContainer.kind === 243 /* JsxOpeningElement */)) { // Cursor is inside a JSX self-closing element or opening element attrsType = typeChecker.getJsxElementAttributesType(jsxContainer); + isGlobalCompletion = false; if (attrsType) { symbols = filterJsxAttributes(typeChecker.getPropertiesOfType(attrsType), jsxContainer.attributes); isMemberCompletion = true; @@ -64026,9 +64477,15 @@ var ts; * Matches a triple slash reference directive with an incomplete string literal for its path. Used * to determine if the caret is currently within the string literal and capture the literal fragment * for completions. - * For example, this matches /// +/// +/// +/// /* @internal */ var ts; (function (ts) { @@ -66442,6 +66901,7 @@ var ts; // A map of loose file names to library names // that we are confident require typings var safeList; + var EmptySafeList = ts.createMap(); /** * @param host is the object providing I/O related operations. * @param fileNames are the file names that belong to the same project @@ -66458,10 +66918,13 @@ var ts; return { cachedTypingPaths: [], newTypingNames: [], filesToWatch: [] }; } // Only infer typings for .js and .jsx files - fileNames = ts.filter(ts.map(fileNames, ts.normalizePath), function (f) { return ts.scriptKindIs(f, /*LanguageServiceHost*/ undefined, 1 /* JS */, 2 /* JSX */); }); + fileNames = ts.filter(ts.map(fileNames, ts.normalizePath), function (f) { + var kind = ts.ensureScriptKind(f, ts.getScriptKindFromFileName(f)); + return kind === 1 /* JS */ || kind === 2 /* JSX */; + }); if (!safeList) { var result = ts.readConfigFile(safeListPath, function (path) { return host.readFile(path); }); - safeList = ts.createMap(result.config); + safeList = result.config ? ts.createMap(result.config) : EmptySafeList; } var filesToWatch = []; // Directories to search for package.json, bower.json and other typing information @@ -66552,13 +67015,10 @@ var ts; var jsFileNames = ts.filter(fileNames, ts.hasJavaScriptFileExtension); var inferredTypingNames = ts.map(jsFileNames, function (f) { return ts.removeFileExtension(ts.getBaseFileName(f.toLowerCase())); }); var cleanedTypingNames = ts.map(inferredTypingNames, function (f) { return f.replace(/((?:\.|-)min(?=\.|$))|((?:-|\.)\d+)/g, ""); }); - if (safeList === undefined) { - mergeTypings(cleanedTypingNames); - } - else { + if (safeList !== EmptySafeList) { mergeTypings(ts.filter(cleanedTypingNames, function (f) { return f in safeList; })); } - var hasJsxFile = ts.forEach(fileNames, function (f) { return ts.scriptKindIs(f, /*LanguageServiceHost*/ undefined, 2 /* JSX */); }); + var hasJsxFile = ts.forEach(fileNames, function (f) { return ts.ensureScriptKind(f, ts.getScriptKindFromFileName(f)) === 2 /* JSX */; }); if (hasJsxFile) { mergeTypings(["react"]); } @@ -66573,7 +67033,7 @@ var ts; return; } var typingNames = []; - var fileNames = host.readDirectory(nodeModulesPath, ["*.json"], /*excludes*/ undefined, /*includes*/ undefined, /*depth*/ 2); + var fileNames = host.readDirectory(nodeModulesPath, [".json"], /*excludes*/ undefined, /*includes*/ undefined, /*depth*/ 2); for (var _i = 0, fileNames_2 = fileNames; _i < fileNames_2.length; _i++) { var fileName = fileNames_2[_i]; var normalizedFileName = ts.normalizePath(fileName); @@ -66616,7 +67076,7 @@ var ts; (function (ts) { var NavigateTo; (function (NavigateTo) { - function getNavigateToItems(sourceFiles, checker, cancellationToken, searchValue, maxResultCount) { + function getNavigateToItems(sourceFiles, checker, cancellationToken, searchValue, maxResultCount, excludeDtsFiles) { var patternMatcher = ts.createPatternMatcher(searchValue); var rawItems = []; // This means "compare in a case insensitive manner." @@ -66624,6 +67084,9 @@ var ts; // Search the declarations in all files and output matched NavigateToItem into array of NavigateToItem[] ts.forEach(sourceFiles, function (sourceFile) { cancellationToken.throwIfCancellationRequested(); + if (excludeDtsFiles && ts.fileExtensionIs(sourceFile.fileName, ".d.ts")) { + return; + } var nameToDeclarations = sourceFile.getNamedDeclarations(); for (var name_49 in nameToDeclarations) { var declarations = nameToDeclarations[name_49]; @@ -66803,6 +67266,13 @@ var ts; return result; } NavigationBar.getNavigationBarItems = getNavigationBarItems; + function getNavigationTree(sourceFile) { + curSourceFile = sourceFile; + var result = convertToTree(rootNavigationBarNode(sourceFile)); + curSourceFile = undefined; + return result; + } + NavigationBar.getNavigationTree = getNavigationTree; // Keep sourceFile handy so we don't have to search for it every time we need to call `getText`. var curSourceFile; function nodeText(node) { @@ -67077,7 +67547,7 @@ var ts; } } // More efficient to create a collator once and use its `compare` than to call `a.localeCompare(b)` many times. - var collator = typeof Intl === "undefined" ? undefined : new Intl.Collator(); + var collator = typeof Intl === "object" && typeof Intl.Collator === "function" ? new Intl.Collator() : undefined; // Intl is missing in Safari, and node 0.10 treats "a" as greater than "B". var localeCompareIsCorrect = collator && collator.compare("a", "B") < 0; var localeCompareFix = localeCompareIsCorrect ? collator.compare : function (a, b) { @@ -67242,6 +67712,15 @@ var ts; } // NavigationBarItem requires an array, but will not mutate it, so just give it this for performance. var emptyChildItemArray = []; + function convertToTree(n) { + return { + text: getItemName(n.node), + kind: ts.getNodeKind(n.node), + kindModifiers: ts.getNodeModifiers(n.node), + spans: getSpans(n), + childItems: ts.map(n.children, convertToTree) + }; + } function convertToTopLevelItem(n) { return { text: getItemName(n.node), @@ -67265,16 +67744,16 @@ var ts; grayed: false }; } - function getSpans(n) { - var spans = [getNodeSpan(n.node)]; - if (n.additionalNodes) { - for (var _i = 0, _a = n.additionalNodes; _i < _a.length; _i++) { - var node = _a[_i]; - spans.push(getNodeSpan(node)); - } + } + function getSpans(n) { + var spans = [getNodeSpan(n.node)]; + if (n.additionalNodes) { + for (var _i = 0, _a = n.additionalNodes; _i < _a.length; _i++) { + var node = _a[_i]; + spans.push(getNodeSpan(node)); } - return spans; } + return spans; } function getModuleName(moduleDeclaration) { // We want to maintain quotation marks. @@ -71005,25 +71484,25 @@ var ts; }; RulesProvider.prototype.createActiveRules = function (options) { var rules = this.globalRules.HighPriorityCommonRules.slice(0); - if (options.InsertSpaceAfterCommaDelimiter) { + if (options.insertSpaceAfterCommaDelimiter) { rules.push(this.globalRules.SpaceAfterComma); } else { rules.push(this.globalRules.NoSpaceAfterComma); } - if (options.InsertSpaceAfterFunctionKeywordForAnonymousFunctions) { + if (options.insertSpaceAfterFunctionKeywordForAnonymousFunctions) { rules.push(this.globalRules.SpaceAfterAnonymousFunctionKeyword); } else { rules.push(this.globalRules.NoSpaceAfterAnonymousFunctionKeyword); } - if (options.InsertSpaceAfterKeywordsInControlFlowStatements) { + if (options.insertSpaceAfterKeywordsInControlFlowStatements) { rules.push(this.globalRules.SpaceAfterKeywordInControl); } else { rules.push(this.globalRules.NoSpaceAfterKeywordInControl); } - if (options.InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis) { + if (options.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis) { rules.push(this.globalRules.SpaceAfterOpenParen); rules.push(this.globalRules.SpaceBeforeCloseParen); rules.push(this.globalRules.NoSpaceBetweenParens); @@ -71033,7 +71512,7 @@ var ts; rules.push(this.globalRules.NoSpaceBeforeCloseParen); rules.push(this.globalRules.NoSpaceBetweenParens); } - if (options.InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets) { + if (options.insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets) { rules.push(this.globalRules.SpaceAfterOpenBracket); rules.push(this.globalRules.SpaceBeforeCloseBracket); rules.push(this.globalRules.NoSpaceBetweenBrackets); @@ -71045,7 +71524,7 @@ var ts; } // The default value of InsertSpaceAfterOpeningAndBeforeClosingNonemptyBraces is true // so if the option is undefined, we should treat it as true as well - if (options.InsertSpaceAfterOpeningAndBeforeClosingNonemptyBraces !== false) { + if (options.insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces !== false) { rules.push(this.globalRules.SpaceAfterOpenBrace); rules.push(this.globalRules.SpaceBeforeCloseBrace); rules.push(this.globalRules.NoSpaceBetweenEmptyBraceBrackets); @@ -71055,7 +71534,7 @@ var ts; rules.push(this.globalRules.NoSpaceBeforeCloseBrace); rules.push(this.globalRules.NoSpaceBetweenEmptyBraceBrackets); } - if (options.InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces) { + if (options.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces) { rules.push(this.globalRules.SpaceAfterTemplateHeadAndMiddle); rules.push(this.globalRules.SpaceBeforeTemplateMiddleAndTail); } @@ -71063,7 +71542,7 @@ var ts; rules.push(this.globalRules.NoSpaceAfterTemplateHeadAndMiddle); rules.push(this.globalRules.NoSpaceBeforeTemplateMiddleAndTail); } - if (options.InsertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces) { + if (options.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces) { rules.push(this.globalRules.SpaceAfterOpenBraceInJsxExpression); rules.push(this.globalRules.SpaceBeforeCloseBraceInJsxExpression); } @@ -71071,13 +71550,13 @@ var ts; rules.push(this.globalRules.NoSpaceAfterOpenBraceInJsxExpression); rules.push(this.globalRules.NoSpaceBeforeCloseBraceInJsxExpression); } - if (options.InsertSpaceAfterSemicolonInForStatements) { + if (options.insertSpaceAfterSemicolonInForStatements) { rules.push(this.globalRules.SpaceAfterSemicolonInFor); } else { rules.push(this.globalRules.NoSpaceAfterSemicolonInFor); } - if (options.InsertSpaceBeforeAndAfterBinaryOperators) { + if (options.insertSpaceBeforeAndAfterBinaryOperators) { rules.push(this.globalRules.SpaceBeforeBinaryOperator); rules.push(this.globalRules.SpaceAfterBinaryOperator); } @@ -71085,14 +71564,14 @@ var ts; rules.push(this.globalRules.NoSpaceBeforeBinaryOperator); rules.push(this.globalRules.NoSpaceAfterBinaryOperator); } - if (options.PlaceOpenBraceOnNewLineForControlBlocks) { + if (options.placeOpenBraceOnNewLineForControlBlocks) { rules.push(this.globalRules.NewLineBeforeOpenBraceInControl); } - if (options.PlaceOpenBraceOnNewLineForFunctions) { + if (options.placeOpenBraceOnNewLineForFunctions) { rules.push(this.globalRules.NewLineBeforeOpenBraceInFunction); rules.push(this.globalRules.NewLineBeforeOpenBraceInTypeScriptDeclWithBlock); } - if (options.InsertSpaceAfterTypeAssertion) { + if (options.insertSpaceAfterTypeAssertion) { rules.push(this.globalRules.SpaceAfterTypeAssertion); } else { @@ -71222,7 +71701,7 @@ var ts; return ts.rangeContainsRange(parent.members, node); case 225 /* ModuleDeclaration */: var body = parent.body; - return body && body.kind === 199 /* Block */ && ts.rangeContainsRange(body.statements, node); + return body && body.kind === 226 /* ModuleBlock */ && ts.rangeContainsRange(body.statements, node); case 256 /* SourceFile */: case 199 /* Block */: case 226 /* ModuleBlock */: @@ -71332,7 +71811,7 @@ var ts; break; } if (formatting.SmartIndenter.shouldIndentChildNode(n, child)) { - return options.IndentSize; + return options.indentSize; } previousLine = line; child = n; @@ -71404,7 +71883,7 @@ var ts; } function computeIndentation(node, startLine, inheritedIndentation, parent, parentDynamicIndentation, effectiveParentStartLine) { var indentation = inheritedIndentation; - var delta = formatting.SmartIndenter.shouldIndentChildNode(node) ? options.IndentSize : 0; + var delta = formatting.SmartIndenter.shouldIndentChildNode(node) ? options.indentSize : 0; if (effectiveParentStartLine === startLine) { // if node is located on the same line with the parent // - inherit indentation from the parent @@ -71412,7 +71891,7 @@ var ts; indentation = startLine === lastIndentedLine ? indentationOnLastIndentedLine : parentDynamicIndentation.getIndentation(); - delta = Math.min(options.IndentSize, parentDynamicIndentation.getDelta(node) + delta); + delta = Math.min(options.indentSize, parentDynamicIndentation.getDelta(node) + delta); } else if (indentation === -1 /* Unknown */) { if (formatting.SmartIndenter.childStartsOnTheSameLineWithElseInIfStatement(parent, node, startLine, sourceFile)) { @@ -71493,13 +71972,13 @@ var ts; recomputeIndentation: function (lineAdded) { if (node.parent && formatting.SmartIndenter.shouldIndentChildNode(node.parent, node)) { if (lineAdded) { - indentation += options.IndentSize; + indentation += options.indentSize; } else { - indentation -= options.IndentSize; + indentation -= options.indentSize; } if (formatting.SmartIndenter.shouldIndentChildNode(node)) { - delta = options.IndentSize; + delta = options.indentSize; } else { delta = 0; @@ -71919,7 +72398,7 @@ var ts; // edit should not be applied only if we have one line feed between elements var lineDelta = currentStartLine - previousStartLine; if (lineDelta !== 1) { - recordReplace(previousRange.end, currentRange.pos - previousRange.end, options.NewLineCharacter); + recordReplace(previousRange.end, currentRange.pos - previousRange.end, options.newLineCharacter); } break; case 2 /* Space */: @@ -71980,14 +72459,14 @@ var ts; var internedSpacesIndentation; function getIndentationString(indentation, options) { // reset interned strings if FormatCodeOptions were changed - var resetInternedStrings = !internedSizes || (internedSizes.tabSize !== options.TabSize || internedSizes.indentSize !== options.IndentSize); + var resetInternedStrings = !internedSizes || (internedSizes.tabSize !== options.tabSize || internedSizes.indentSize !== options.indentSize); if (resetInternedStrings) { - internedSizes = { tabSize: options.TabSize, indentSize: options.IndentSize }; + internedSizes = { tabSize: options.tabSize, indentSize: options.indentSize }; internedTabsIndentation = internedSpacesIndentation = undefined; } - if (!options.ConvertTabsToSpaces) { - var tabs = Math.floor(indentation / options.TabSize); - var spaces = indentation - tabs * options.TabSize; + if (!options.convertTabsToSpaces) { + var tabs = Math.floor(indentation / options.tabSize); + var spaces = indentation - tabs * options.tabSize; var tabString = void 0; if (!internedTabsIndentation) { internedTabsIndentation = []; @@ -72002,13 +72481,13 @@ var ts; } else { var spacesString = void 0; - var quotient = Math.floor(indentation / options.IndentSize); - var remainder = indentation % options.IndentSize; + var quotient = Math.floor(indentation / options.indentSize); + var remainder = indentation % options.indentSize; if (!internedSpacesIndentation) { internedSpacesIndentation = []; } if (internedSpacesIndentation[quotient] === undefined) { - spacesString = repeat(" ", options.IndentSize * quotient); + spacesString = repeat(" ", options.indentSize * quotient); internedSpacesIndentation[quotient] = spacesString; } else { @@ -72045,7 +72524,7 @@ var ts; } // no indentation when the indent style is set to none, // so we can return fast - if (options.IndentStyle === ts.IndentStyle.None) { + if (options.indentStyle === ts.IndentStyle.None) { return 0; } var precedingToken = ts.findPrecedingToken(position, sourceFile); @@ -72061,7 +72540,7 @@ var ts; // indentation is first non-whitespace character in a previous line // for block indentation, we should look for a line which contains something that's not // whitespace. - if (options.IndentStyle === ts.IndentStyle.Block) { + if (options.indentStyle === ts.IndentStyle.Block) { // move backwards until we find a line with a non-whitespace character, // then find the first non-whitespace character for that line. var current_1 = position; @@ -72095,7 +72574,7 @@ var ts; indentationDelta = 0; } else { - indentationDelta = lineAtPosition !== currentStart.line ? options.IndentSize : 0; + indentationDelta = lineAtPosition !== currentStart.line ? options.indentSize : 0; } break; } @@ -72106,7 +72585,7 @@ var ts; } actualIndentation = getLineIndentationWhenExpressionIsInMultiLine(current, sourceFile, options); if (actualIndentation !== -1 /* Unknown */) { - return actualIndentation + options.IndentSize; + return actualIndentation + options.indentSize; } previous = current; current = current.parent; @@ -72118,15 +72597,15 @@ var ts; return getIndentationForNodeWorker(current, currentStart, /*ignoreActualIndentationRange*/ undefined, indentationDelta, sourceFile, options); } SmartIndenter.getIndentation = getIndentation; - function getBaseIndentation(options) { - return options.BaseIndentSize || 0; - } - SmartIndenter.getBaseIndentation = getBaseIndentation; function getIndentationForNode(n, ignoreActualIndentationRange, sourceFile, options) { var start = sourceFile.getLineAndCharacterOfPosition(n.getStart(sourceFile)); return getIndentationForNodeWorker(n, start, ignoreActualIndentationRange, /*indentationDelta*/ 0, sourceFile, options); } SmartIndenter.getIndentationForNode = getIndentationForNode; + function getBaseIndentation(options) { + return options.baseIndentSize || 0; + } + SmartIndenter.getBaseIndentation = getBaseIndentation; function getIndentationForNodeWorker(current, currentStart, ignoreActualIndentationRange, indentationDelta, sourceFile, options) { var parent = current.parent; var parentStart; @@ -72161,7 +72640,7 @@ var ts; } // increase indentation if parent node wants its content to be indented and parent and child nodes don't start on the same line if (shouldIndentChildNode(parent, current) && !parentAndChildShareLine) { - indentationDelta += options.IndentSize; + indentationDelta += options.indentSize; } current = parent; currentStart = parentStart; @@ -72371,7 +72850,7 @@ var ts; break; } if (ch === 9 /* tab */) { - column += options.TabSize + (column % options.TabSize); + column += options.tabSize + (column % options.tabSize); } else { column++; @@ -72473,6 +72952,117 @@ var ts; })(SmartIndenter = formatting.SmartIndenter || (formatting.SmartIndenter = {})); })(formatting = ts.formatting || (ts.formatting = {})); })(ts || (ts = {})); +/* @internal */ +var ts; +(function (ts) { + var codefix; + (function (codefix) { + var codeFixes = ts.createMap(); + function registerCodeFix(action) { + ts.forEach(action.errorCodes, function (error) { + var fixes = codeFixes[error]; + if (!fixes) { + fixes = []; + codeFixes[error] = fixes; + } + fixes.push(action); + }); + } + codefix.registerCodeFix = registerCodeFix; + function getSupportedErrorCodes() { + return Object.keys(codeFixes); + } + codefix.getSupportedErrorCodes = getSupportedErrorCodes; + function getFixes(context) { + var fixes = codeFixes[context.errorCode]; + var allActions = []; + ts.forEach(fixes, function (f) { + var actions = f.getCodeActions(context); + if (actions && actions.length > 0) { + allActions = allActions.concat(actions); + } + }); + return allActions; + } + codefix.getFixes = getFixes; + })(codefix = ts.codefix || (ts.codefix = {})); +})(ts || (ts = {})); +/* @internal */ +var ts; +(function (ts) { + var codefix; + (function (codefix) { + function getOpenBraceEnd(constructor, sourceFile) { + // First token is the open curly, this is where we want to put the 'super' call. + return constructor.body.getFirstToken(sourceFile).getEnd(); + } + codefix.registerCodeFix({ + errorCodes: [ts.Diagnostics.Constructors_for_derived_classes_must_contain_a_super_call.code], + getCodeActions: function (context) { + var sourceFile = context.sourceFile; + var token = ts.getTokenAtPosition(sourceFile, context.span.start); + if (token.kind !== 121 /* ConstructorKeyword */) { + return undefined; + } + var newPosition = getOpenBraceEnd(token.parent, sourceFile); + return [{ + description: ts.getLocaleSpecificMessage(ts.Diagnostics.Add_missing_super_call), + changes: [{ fileName: sourceFile.fileName, textChanges: [{ newText: "super();", span: { start: newPosition, length: 0 } }] }] + }]; + } + }); + codefix.registerCodeFix({ + errorCodes: [ts.Diagnostics.super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class.code], + getCodeActions: function (context) { + var sourceFile = context.sourceFile; + var token = ts.getTokenAtPosition(sourceFile, context.span.start); + if (token.kind !== 97 /* ThisKeyword */) { + return undefined; + } + var constructor = ts.getContainingFunction(token); + var superCall = findSuperCall(constructor.body); + if (!superCall) { + return undefined; + } + // figure out if the this access is actuall inside the supercall + // i.e. super(this.a), since in that case we won't suggest a fix + if (superCall.expression && superCall.expression.kind == 174 /* CallExpression */) { + var arguments_1 = superCall.expression.arguments; + for (var i = 0; i < arguments_1.length; i++) { + if (arguments_1[i].expression === token) { + return undefined; + } + } + } + var newPosition = getOpenBraceEnd(constructor, sourceFile); + var changes = [{ + fileName: sourceFile.fileName, textChanges: [{ + newText: superCall.getText(sourceFile), + span: { start: newPosition, length: 0 } + }, + { + newText: "", + span: { start: superCall.getStart(sourceFile), length: superCall.getWidth(sourceFile) } + }] + }]; + return [{ + description: ts.getLocaleSpecificMessage(ts.Diagnostics.Make_super_call_the_first_statement_in_the_constructor), + changes: changes + }]; + function findSuperCall(n) { + if (n.kind === 202 /* ExpressionStatement */ && ts.isSuperCall(n.expression)) { + return n; + } + if (ts.isFunctionLike(n)) { + return undefined; + } + return ts.forEachChild(n, findSuperCall); + } + } + }); + })(codefix = ts.codefix || (ts.codefix = {})); +})(ts || (ts = {})); +/// /// /// /// @@ -72498,13 +73088,15 @@ var ts; /// /// /// +/// +/// var ts; (function (ts) { /** The version of the language service API */ ts.servicesVersion = "0.5"; function createNode(kind, pos, end, parent) { var node = kind >= 139 /* FirstNode */ ? new NodeObject(kind, pos, end) : - kind === 69 /* Identifier */ ? new IdentifierObject(kind, pos, end) : + kind === 69 /* Identifier */ ? new IdentifierObject(69 /* Identifier */, pos, end) : new TokenObject(kind, pos, end); node.parent = parent; return node; @@ -72732,15 +73324,16 @@ var ts; var TokenObject = (function (_super) { __extends(TokenObject, _super); function TokenObject(kind, pos, end) { - _super.call(this, pos, end); - this.kind = kind; + var _this = _super.call(this, pos, end) || this; + _this.kind = kind; + return _this; } return TokenObject; }(TokenOrIdentifierObject)); var IdentifierObject = (function (_super) { __extends(IdentifierObject, _super); function IdentifierObject(kind, pos, end) { - _super.call(this, pos, end); + return _super.call(this, pos, end) || this; } return IdentifierObject; }(TokenOrIdentifierObject)); @@ -72816,7 +73409,7 @@ var ts; var SourceFileObject = (function (_super) { __extends(SourceFileObject, _super); function SourceFileObject(kind, pos, end) { - _super.call(this, kind, pos, end); + return _super.call(this, kind, pos, end) || this; } SourceFileObject.prototype.update = function (newText, textChangeRange) { return ts.updateSourceFile(this, newText, textChangeRange); @@ -72985,6 +73578,30 @@ var ts; getSignatureConstructor: function () { return SignatureObject; } }; } + function toEditorSettings(optionsAsMap) { + var allPropertiesAreCamelCased = true; + for (var key in optionsAsMap) { + if (ts.hasProperty(optionsAsMap, key) && !isCamelCase(key)) { + allPropertiesAreCamelCased = false; + break; + } + } + if (allPropertiesAreCamelCased) { + return optionsAsMap; + } + var settings = {}; + for (var key in optionsAsMap) { + if (ts.hasProperty(optionsAsMap, key)) { + var newKey = isCamelCase(key) ? key : key.charAt(0).toLowerCase() + key.substr(1); + settings[newKey] = optionsAsMap[key]; + } + } + return settings; + } + ts.toEditorSettings = toEditorSettings; + function isCamelCase(s) { + return !s.length || s.charAt(0) === s.charAt(0).toLowerCase(); + } function displayPartsToString(displayParts) { if (displayParts) { return ts.map(displayParts, function (displayPart) { return displayPart.text; }).join(""); @@ -73000,9 +73617,13 @@ var ts; }; } ts.getDefaultCompilerOptions = getDefaultCompilerOptions; - // Cache host information about script should be refreshed + function getSupportedCodeFixes() { + return ts.codefix.getSupportedErrorCodes(); + } + ts.getSupportedCodeFixes = getSupportedCodeFixes; + // Cache host information about script Should be refreshed // at each language service public entry point, since we don't know when - // set of scripts handled by the host changes. + // the set of scripts handled by the host changes. var HostCache = (function () { function HostCache(host, getCanonicalFileName) { this.host = host; @@ -73185,7 +73806,8 @@ var ts; var ruleProvider; var program; var lastProjectVersion; - var useCaseSensitivefileNames = false; + var lastTypesRootVersion = 0; + var useCaseSensitivefileNames = host.useCaseSensitiveFileNames && host.useCaseSensitiveFileNames(); var cancellationToken = new CancellationTokenObject(host.getCancellationToken && host.getCancellationToken()); var currentDirectory = host.getCurrentDirectory(); // Check if the localized messages json is set, otherwise query the host for it @@ -73224,6 +73846,12 @@ var ts; lastProjectVersion = hostProjectVersion; } } + var typeRootsVersion = host.getTypeRootsVersion ? host.getTypeRootsVersion() : 0; + if (lastTypesRootVersion !== typeRootsVersion) { + log("TypeRoots version has changed; provide new program"); + program = undefined; + lastTypesRootVersion = typeRootsVersion; + } // Get a fresh cache of the host information var hostCache = new HostCache(host, getCanonicalFileName); // If the program is already up-to-date, we can reuse it @@ -73553,12 +74181,12 @@ var ts; return ts.FindAllReferences.findReferencedSymbols(program.getTypeChecker(), cancellationToken, program.getSourceFiles(), getValidSourceFile(fileName), position, findInStrings, findInComments); } /// NavigateTo - function getNavigateToItems(searchValue, maxResultCount, fileName) { + function getNavigateToItems(searchValue, maxResultCount, fileName, excludeDtsFiles) { synchronizeHostData(); var sourceFiles = fileName ? [getValidSourceFile(fileName)] : program.getSourceFiles(); - return ts.NavigateTo.getNavigateToItems(sourceFiles, program.getTypeChecker(), cancellationToken, searchValue, maxResultCount); + return ts.NavigateTo.getNavigateToItems(sourceFiles, program.getTypeChecker(), cancellationToken, searchValue, maxResultCount, excludeDtsFiles); } - function getEmitOutput(fileName) { + function getEmitOutput(fileName, emitOnlyDtsFiles) { synchronizeHostData(); var sourceFile = getValidSourceFile(fileName); var outputFiles = []; @@ -73569,7 +74197,7 @@ var ts; text: data }); } - var emitOutput = program.emit(sourceFile, writeFile, cancellationToken); + var emitOutput = program.emit(sourceFile, writeFile, cancellationToken, emitOnlyDtsFiles); return { outputFiles: outputFiles, emitSkipped: emitOutput.emitSkipped @@ -73647,14 +74275,28 @@ var ts; return ts.BreakpointResolver.spanInSourceFileAtLocation(sourceFile, position); } function getNavigationBarItems(fileName) { - var sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName); - return ts.NavigationBar.getNavigationBarItems(sourceFile); + return ts.NavigationBar.getNavigationBarItems(syntaxTreeCache.getCurrentSourceFile(fileName)); + } + function getNavigationTree(fileName) { + return ts.NavigationBar.getNavigationTree(syntaxTreeCache.getCurrentSourceFile(fileName)); + } + function isTsOrTsxFile(fileName) { + var kind = ts.getScriptKind(fileName, host); + return kind === 3 /* TS */ || kind === 4 /* TSX */; } function getSemanticClassifications(fileName, span) { + if (!isTsOrTsxFile(fileName)) { + // do not run semantic classification on non-ts-or-tsx files + return []; + } synchronizeHostData(); return ts.getSemanticClassifications(program.getTypeChecker(), cancellationToken, getValidSourceFile(fileName), program.getClassifiableNames(), span); } function getEncodedSemanticClassifications(fileName, span) { + if (!isTsOrTsxFile(fileName)) { + // do not run semantic classification on non-ts-or-tsx files + return { spans: [], endOfLineState: 0 /* None */ }; + } synchronizeHostData(); return ts.getEncodedSemanticClassifications(program.getTypeChecker(), cancellationToken, getValidSourceFile(fileName), program.getClassifiableNames(), span); } @@ -73715,34 +74357,60 @@ var ts; } function getIndentationAtPosition(fileName, position, editorOptions) { var start = ts.timestamp(); + var settings = toEditorSettings(editorOptions); var sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName); log("getIndentationAtPosition: getCurrentSourceFile: " + (ts.timestamp() - start)); start = ts.timestamp(); - var result = ts.formatting.SmartIndenter.getIndentation(position, sourceFile, editorOptions); + var result = ts.formatting.SmartIndenter.getIndentation(position, sourceFile, settings); log("getIndentationAtPosition: computeIndentation : " + (ts.timestamp() - start)); return result; } function getFormattingEditsForRange(fileName, start, end, options) { var sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName); - return ts.formatting.formatSelection(start, end, sourceFile, getRuleProvider(options), options); + var settings = toEditorSettings(options); + return ts.formatting.formatSelection(start, end, sourceFile, getRuleProvider(settings), settings); } function getFormattingEditsForDocument(fileName, options) { var sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName); - return ts.formatting.formatDocument(sourceFile, getRuleProvider(options), options); + var settings = toEditorSettings(options); + return ts.formatting.formatDocument(sourceFile, getRuleProvider(settings), settings); } function getFormattingEditsAfterKeystroke(fileName, position, key, options) { var sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName); + var settings = toEditorSettings(options); if (key === "}") { - return ts.formatting.formatOnClosingCurly(position, sourceFile, getRuleProvider(options), options); + return ts.formatting.formatOnClosingCurly(position, sourceFile, getRuleProvider(settings), settings); } else if (key === ";") { - return ts.formatting.formatOnSemicolon(position, sourceFile, getRuleProvider(options), options); + return ts.formatting.formatOnSemicolon(position, sourceFile, getRuleProvider(settings), settings); } else if (key === "\n") { - return ts.formatting.formatOnEnter(position, sourceFile, getRuleProvider(options), options); + return ts.formatting.formatOnEnter(position, sourceFile, getRuleProvider(settings), settings); } return []; } + function getCodeFixesAtPosition(fileName, start, end, errorCodes) { + synchronizeHostData(); + var sourceFile = getValidSourceFile(fileName); + var span = { start: start, length: end - start }; + var newLineChar = ts.getNewLineOrDefaultFromHost(host); + var allFixes = []; + ts.forEach(errorCodes, function (error) { + cancellationToken.throwIfCancellationRequested(); + var context = { + errorCode: error, + sourceFile: sourceFile, + span: span, + program: program, + newLineCharacter: newLineChar + }; + var fixes = ts.codefix.getFixes(context); + if (fixes) { + allFixes = allFixes.concat(fixes); + } + }); + return allFixes; + } function getDocCommentTemplateAtPosition(fileName, position) { return ts.JsDoc.getDocCommentTemplateAtPosition(ts.getNewLineOrDefaultFromHost(host), syntaxTreeCache.getCurrentSourceFile(fileName), position); } @@ -73926,6 +74594,7 @@ var ts; getRenameInfo: getRenameInfo, findRenameLocations: findRenameLocations, getNavigationBarItems: getNavigationBarItems, + getNavigationTree: getNavigationTree, getOutliningSpans: getOutliningSpans, getTodoComments: getTodoComments, getBraceMatchingAtPosition: getBraceMatchingAtPosition, @@ -73935,6 +74604,7 @@ var ts; getFormattingEditsAfterKeystroke: getFormattingEditsAfterKeystroke, getDocCommentTemplateAtPosition: getDocCommentTemplateAtPosition, isValidBraceCompletionAtPosition: isValidBraceCompletionAtPosition, + getCodeFixesAtPosition: getCodeFixesAtPosition, getEmitOutput: getEmitOutput, getNonBoundSourceFile: getNonBoundSourceFile, getSourceFile: getSourceFile, @@ -74624,7 +75294,7 @@ var ts; // /// /* @internal */ -var debugObjectHost = new Function("return this")(); +var debugObjectHost = (function () { return this; })(); // We need to use 'null' to interface with the managed side. /* tslint:disable:no-null-keyword */ /* tslint:disable:no-in-operator */ @@ -74712,6 +75382,12 @@ var ts; } return this.shimHost.getProjectVersion(); }; + LanguageServiceShimHostAdapter.prototype.getTypeRootsVersion = function () { + if (!this.shimHost.getTypeRootsVersion) { + return 0; + } + return this.shimHost.getTypeRootsVersion(); + }; LanguageServiceShimHostAdapter.prototype.useCaseSensitiveFileNames = function () { return this.shimHost.useCaseSensitiveFileNames ? this.shimHost.useCaseSensitiveFileNames() : false; }; @@ -74914,11 +75590,12 @@ var ts; var LanguageServiceShimObject = (function (_super) { __extends(LanguageServiceShimObject, _super); function LanguageServiceShimObject(factory, host, languageService) { - _super.call(this, factory); - this.host = host; - this.languageService = languageService; - this.logPerformance = false; - this.logger = this.host; + var _this = _super.call(this, factory) || this; + _this.host = host; + _this.languageService = languageService; + _this.logPerformance = false; + _this.logger = _this.host; + return _this; } LanguageServiceShimObject.prototype.forwardJSONCall = function (actionDescription, action) { return forwardJSONCall(this.logger, actionDescription, action, this.logPerformance); @@ -75156,6 +75833,10 @@ var ts; var _this = this; return this.forwardJSONCall("getNavigationBarItems('" + fileName + "')", function () { return _this.languageService.getNavigationBarItems(fileName); }); }; + LanguageServiceShimObject.prototype.getNavigationTree = function (fileName) { + var _this = this; + return this.forwardJSONCall("getNavigationTree('" + fileName + "')", function () { return _this.languageService.getNavigationTree(fileName); }); + }; LanguageServiceShimObject.prototype.getOutliningSpans = function (fileName) { var _this = this; return this.forwardJSONCall("getOutliningSpans('" + fileName + "')", function () { return _this.languageService.getOutliningSpans(fileName); }); @@ -75182,10 +75863,11 @@ var ts; var ClassifierShimObject = (function (_super) { __extends(ClassifierShimObject, _super); function ClassifierShimObject(factory, logger) { - _super.call(this, factory); - this.logger = logger; - this.logPerformance = false; - this.classifier = ts.createClassifier(); + var _this = _super.call(this, factory) || this; + _this.logger = logger; + _this.logPerformance = false; + _this.classifier = ts.createClassifier(); + return _this; } ClassifierShimObject.prototype.getEncodedLexicalClassifications = function (text, lexState, syntacticClassifierAbsent) { var _this = this; @@ -75208,10 +75890,11 @@ var ts; var CoreServicesShimObject = (function (_super) { __extends(CoreServicesShimObject, _super); function CoreServicesShimObject(factory, logger, host) { - _super.call(this, factory); - this.logger = logger; - this.host = host; - this.logPerformance = false; + var _this = _super.call(this, factory) || this; + _this.logger = logger; + _this.host = host; + _this.logPerformance = false; + return _this; } CoreServicesShimObject.prototype.forwardJSONCall = function (actionDescription, action) { return forwardJSONCall(this.logger, actionDescription, action, this.logPerformance); diff --git a/lib/typescriptServices.d.ts b/lib/typescriptServices.d.ts index e9c209674b5ef..5f319ffb74c0d 100644 --- a/lib/typescriptServices.d.ts +++ b/lib/typescriptServices.d.ts @@ -29,6 +29,7 @@ declare namespace ts { contains(fileName: Path): boolean; remove(fileName: Path): void; forEachValue(f: (key: Path, v: T) => void): void; + getKeys(): Path[]; clear(): void; } interface TextRange { @@ -388,7 +389,6 @@ declare namespace ts { ContextFlags = 1540096, TypeExcludesFlags = 327680, } - type ModifiersArray = NodeArray; enum ModifierFlags { None = 0, Export = 1, @@ -425,12 +425,21 @@ declare namespace ts { interface NodeArray extends Array, TextRange { hasTrailingComma?: boolean; } - interface Token extends Node { - __tokenTag: any; - } - interface Modifier extends Token { - } + interface Token extends Node { + kind: TKind; + } + type DotDotDotToken = Token; + type QuestionToken = Token; + type ColonToken = Token; + type EqualsToken = Token; + type AsteriskToken = Token; + type EqualsGreaterThanToken = Token; + type EndOfFileToken = Token; + type AtToken = Token; + type Modifier = Token | Token | Token | Token | Token | Token | Token | Token | Token | Token | Token; + type ModifiersArray = NodeArray; interface Identifier extends PrimaryExpression { + kind: SyntaxKind.Identifier; text: string; originalKeywordKind?: SyntaxKind; } @@ -438,6 +447,7 @@ declare namespace ts { resolvedSymbol: Symbol; } interface QualifiedName extends Node { + kind: SyntaxKind.QualifiedName; left: EntityName; right: Identifier; } @@ -449,15 +459,18 @@ declare namespace ts { name?: DeclarationName; } interface DeclarationStatement extends Declaration, Statement { - name?: Identifier; + name?: Identifier | LiteralExpression; } interface ComputedPropertyName extends Node { + kind: SyntaxKind.ComputedPropertyName; expression: Expression; } interface Decorator extends Node { + kind: SyntaxKind.Decorator; expression: LeftHandSideExpression; } interface TypeParameterDeclaration extends Declaration { + kind: SyntaxKind.TypeParameter; name: Identifier; constraint?: TypeNode; expression?: Expression; @@ -469,40 +482,48 @@ declare namespace ts { type?: TypeNode; } interface CallSignatureDeclaration extends SignatureDeclaration, TypeElement { + kind: SyntaxKind.CallSignature; } interface ConstructSignatureDeclaration extends SignatureDeclaration, TypeElement { + kind: SyntaxKind.ConstructSignature; } type BindingName = Identifier | BindingPattern; interface VariableDeclaration extends Declaration { + kind: SyntaxKind.VariableDeclaration; parent?: VariableDeclarationList; name: BindingName; type?: TypeNode; initializer?: Expression; } interface VariableDeclarationList extends Node { + kind: SyntaxKind.VariableDeclarationList; declarations: NodeArray; } interface ParameterDeclaration extends Declaration { - dotDotDotToken?: Node; + kind: SyntaxKind.Parameter; + dotDotDotToken?: DotDotDotToken; name: BindingName; - questionToken?: Node; + questionToken?: QuestionToken; type?: TypeNode; initializer?: Expression; } interface BindingElement extends Declaration { + kind: SyntaxKind.BindingElement; propertyName?: PropertyName; - dotDotDotToken?: Node; + dotDotDotToken?: DotDotDotToken; name: BindingName; initializer?: Expression; } interface PropertySignature extends TypeElement { + kind: SyntaxKind.PropertySignature | SyntaxKind.JSDocRecordMember; name: PropertyName; - questionToken?: Node; + questionToken?: QuestionToken; type?: TypeNode; initializer?: Expression; } interface PropertyDeclaration extends ClassElement { - questionToken?: Node; + kind: SyntaxKind.PropertyDeclaration; + questionToken?: QuestionToken; name: PropertyName; type?: TypeNode; initializer?: Expression; @@ -513,22 +534,23 @@ declare namespace ts { } type ObjectLiteralElementLike = PropertyAssignment | ShorthandPropertyAssignment | MethodDeclaration | AccessorDeclaration; interface PropertyAssignment extends ObjectLiteralElement { - _propertyAssignmentBrand: any; + kind: SyntaxKind.PropertyAssignment; name: PropertyName; - questionToken?: Node; + questionToken?: QuestionToken; initializer: Expression; } interface ShorthandPropertyAssignment extends ObjectLiteralElement { + kind: SyntaxKind.ShorthandPropertyAssignment; name: Identifier; - questionToken?: Node; - equalsToken?: Node; + questionToken?: QuestionToken; + equalsToken?: Token; objectAssignmentInitializer?: Expression; } interface VariableLikeDeclaration extends Declaration { propertyName?: PropertyName; - dotDotDotToken?: Node; + dotDotDotToken?: DotDotDotToken; name: DeclarationName; - questionToken?: Node; + questionToken?: QuestionToken; type?: TypeNode; initializer?: Expression; } @@ -539,10 +561,12 @@ declare namespace ts { elements: NodeArray; } interface ObjectBindingPattern extends BindingPattern { + kind: SyntaxKind.ObjectBindingPattern; elements: NodeArray; } type ArrayBindingElement = BindingElement | OmittedExpression; interface ArrayBindingPattern extends BindingPattern { + kind: SyntaxKind.ArrayBindingPattern; elements: NodeArray; } /** @@ -555,95 +579,116 @@ declare namespace ts { */ interface FunctionLikeDeclaration extends SignatureDeclaration { _functionLikeDeclarationBrand: any; - asteriskToken?: Node; - questionToken?: Node; + asteriskToken?: AsteriskToken; + questionToken?: QuestionToken; body?: Block | Expression; } interface FunctionDeclaration extends FunctionLikeDeclaration, DeclarationStatement { + kind: SyntaxKind.FunctionDeclaration; name?: Identifier; body?: FunctionBody; } interface MethodSignature extends SignatureDeclaration, TypeElement { + kind: SyntaxKind.MethodSignature; name: PropertyName; } interface MethodDeclaration extends FunctionLikeDeclaration, ClassElement, ObjectLiteralElement { + kind: SyntaxKind.MethodDeclaration; name: PropertyName; body?: FunctionBody; } interface ConstructorDeclaration extends FunctionLikeDeclaration, ClassElement { + kind: SyntaxKind.Constructor; body?: FunctionBody; } interface SemicolonClassElement extends ClassElement { - _semicolonClassElementBrand: any; + kind: SyntaxKind.SemicolonClassElement; } - interface AccessorDeclaration extends FunctionLikeDeclaration, ClassElement, ObjectLiteralElement { - _accessorDeclarationBrand: any; + interface GetAccessorDeclaration extends FunctionLikeDeclaration, ClassElement, ObjectLiteralElement { + kind: SyntaxKind.GetAccessor; name: PropertyName; body: FunctionBody; } - interface GetAccessorDeclaration extends AccessorDeclaration { - } - interface SetAccessorDeclaration extends AccessorDeclaration { + interface SetAccessorDeclaration extends FunctionLikeDeclaration, ClassElement, ObjectLiteralElement { + kind: SyntaxKind.SetAccessor; + name: PropertyName; + body: FunctionBody; } + type AccessorDeclaration = GetAccessorDeclaration | SetAccessorDeclaration; interface IndexSignatureDeclaration extends SignatureDeclaration, ClassElement, TypeElement { - _indexSignatureDeclarationBrand: any; + kind: SyntaxKind.IndexSignature; } interface TypeNode extends Node { _typeNodeBrand: any; } + interface KeywordTypeNode extends TypeNode { + kind: SyntaxKind.AnyKeyword | SyntaxKind.NumberKeyword | SyntaxKind.BooleanKeyword | SyntaxKind.StringKeyword | SyntaxKind.SymbolKeyword | SyntaxKind.VoidKeyword; + } interface ThisTypeNode extends TypeNode { - _thisTypeNodeBrand: any; + kind: SyntaxKind.ThisType; } interface FunctionOrConstructorTypeNode extends TypeNode, SignatureDeclaration { - _functionOrConstructorTypeNodeBrand: any; + kind: SyntaxKind.FunctionType | SyntaxKind.ConstructorType; } interface FunctionTypeNode extends FunctionOrConstructorTypeNode { + kind: SyntaxKind.FunctionType; } interface ConstructorTypeNode extends FunctionOrConstructorTypeNode { + kind: SyntaxKind.ConstructorType; } interface TypeReferenceNode extends TypeNode { + kind: SyntaxKind.TypeReference; typeName: EntityName; typeArguments?: NodeArray; } interface TypePredicateNode extends TypeNode { + kind: SyntaxKind.TypePredicate; parameterName: Identifier | ThisTypeNode; type: TypeNode; } interface TypeQueryNode extends TypeNode { + kind: SyntaxKind.TypeQuery; exprName: EntityName; } interface TypeLiteralNode extends TypeNode, Declaration { + kind: SyntaxKind.TypeLiteral; members: NodeArray; } interface ArrayTypeNode extends TypeNode { + kind: SyntaxKind.ArrayType; elementType: TypeNode; } interface TupleTypeNode extends TypeNode { + kind: SyntaxKind.TupleType; elementTypes: NodeArray; } interface UnionOrIntersectionTypeNode extends TypeNode { + kind: SyntaxKind.UnionType | SyntaxKind.IntersectionType; types: NodeArray; } interface UnionTypeNode extends UnionOrIntersectionTypeNode { + kind: SyntaxKind.UnionType; } interface IntersectionTypeNode extends UnionOrIntersectionTypeNode { + kind: SyntaxKind.IntersectionType; } interface ParenthesizedTypeNode extends TypeNode { + kind: SyntaxKind.ParenthesizedType; type: TypeNode; } interface LiteralTypeNode extends TypeNode { - _stringLiteralTypeBrand: any; + kind: SyntaxKind.LiteralType; literal: Expression; } interface StringLiteral extends LiteralExpression { - _stringLiteralBrand: any; + kind: SyntaxKind.StringLiteral; } interface Expression extends Node { _expressionBrand: any; contextualType?: Type; } interface OmittedExpression extends Expression { - _omittedExpressionBrand: any; + kind: SyntaxKind.OmittedExpression; } interface UnaryExpression extends Expression { _unaryExpressionBrand: any; @@ -651,13 +696,17 @@ declare namespace ts { interface IncrementExpression extends UnaryExpression { _incrementExpressionBrand: any; } + type PrefixUnaryOperator = SyntaxKind.PlusPlusToken | SyntaxKind.MinusMinusToken | SyntaxKind.PlusToken | SyntaxKind.MinusToken | SyntaxKind.TildeToken | SyntaxKind.ExclamationToken; interface PrefixUnaryExpression extends IncrementExpression { - operator: SyntaxKind; + kind: SyntaxKind.PrefixUnaryExpression; + operator: PrefixUnaryOperator; operand: UnaryExpression; } + type PostfixUnaryOperator = SyntaxKind.PlusPlusToken | SyntaxKind.MinusMinusToken; interface PostfixUnaryExpression extends IncrementExpression { + kind: SyntaxKind.PostfixUnaryExpression; operand: LeftHandSideExpression; - operator: SyntaxKind; + operator: PostfixUnaryOperator; } interface PostfixExpression extends UnaryExpression { _postfixExpressionBrand: any; @@ -671,42 +720,83 @@ declare namespace ts { interface PrimaryExpression extends MemberExpression { _primaryExpressionBrand: any; } + interface NullLiteral extends PrimaryExpression { + kind: SyntaxKind.NullKeyword; + } + interface BooleanLiteral extends PrimaryExpression { + kind: SyntaxKind.TrueKeyword | SyntaxKind.FalseKeyword; + } + interface ThisExpression extends PrimaryExpression { + kind: SyntaxKind.ThisKeyword; + } + interface SuperExpression extends PrimaryExpression { + kind: SyntaxKind.SuperKeyword; + } interface DeleteExpression extends UnaryExpression { + kind: SyntaxKind.DeleteExpression; expression: UnaryExpression; } interface TypeOfExpression extends UnaryExpression { + kind: SyntaxKind.TypeOfExpression; expression: UnaryExpression; } interface VoidExpression extends UnaryExpression { + kind: SyntaxKind.VoidExpression; expression: UnaryExpression; } interface AwaitExpression extends UnaryExpression { + kind: SyntaxKind.AwaitExpression; expression: UnaryExpression; } interface YieldExpression extends Expression { - asteriskToken?: Node; + kind: SyntaxKind.YieldExpression; + asteriskToken?: AsteriskToken; expression?: Expression; } + type ExponentiationOperator = SyntaxKind.AsteriskAsteriskToken; + type MultiplicativeOperator = SyntaxKind.AsteriskToken | SyntaxKind.SlashToken | SyntaxKind.PercentToken; + type MultiplicativeOperatorOrHigher = ExponentiationOperator | MultiplicativeOperator; + type AdditiveOperator = SyntaxKind.PlusToken | SyntaxKind.MinusToken; + type AdditiveOperatorOrHigher = MultiplicativeOperatorOrHigher | AdditiveOperator; + type ShiftOperator = SyntaxKind.LessThanLessThanToken | SyntaxKind.GreaterThanGreaterThanToken | SyntaxKind.GreaterThanGreaterThanGreaterThanToken; + type ShiftOperatorOrHigher = AdditiveOperatorOrHigher | ShiftOperator; + type RelationalOperator = SyntaxKind.LessThanToken | SyntaxKind.LessThanEqualsToken | SyntaxKind.GreaterThanToken | SyntaxKind.GreaterThanEqualsToken | SyntaxKind.InstanceOfKeyword | SyntaxKind.InKeyword; + type RelationalOperatorOrHigher = ShiftOperatorOrHigher | RelationalOperator; + type EqualityOperator = SyntaxKind.EqualsEqualsToken | SyntaxKind.EqualsEqualsEqualsToken | SyntaxKind.ExclamationEqualsEqualsToken | SyntaxKind.ExclamationEqualsToken; + type EqualityOperatorOrHigher = RelationalOperatorOrHigher | EqualityOperator; + type BitwiseOperator = SyntaxKind.AmpersandToken | SyntaxKind.BarToken | SyntaxKind.CaretToken; + type BitwiseOperatorOrHigher = EqualityOperatorOrHigher | BitwiseOperator; + type LogicalOperator = SyntaxKind.AmpersandAmpersandToken | SyntaxKind.BarBarToken; + type LogicalOperatorOrHigher = BitwiseOperatorOrHigher | LogicalOperator; + type CompoundAssignmentOperator = SyntaxKind.PlusEqualsToken | SyntaxKind.MinusEqualsToken | SyntaxKind.AsteriskAsteriskEqualsToken | SyntaxKind.AsteriskEqualsToken | SyntaxKind.SlashEqualsToken | SyntaxKind.PercentEqualsToken | SyntaxKind.AmpersandEqualsToken | SyntaxKind.BarEqualsToken | SyntaxKind.CaretEqualsToken | SyntaxKind.LessThanLessThanEqualsToken | SyntaxKind.GreaterThanGreaterThanGreaterThanEqualsToken | SyntaxKind.GreaterThanGreaterThanEqualsToken; + type AssignmentOperator = SyntaxKind.EqualsToken | CompoundAssignmentOperator; + type AssignmentOperatorOrHigher = LogicalOperatorOrHigher | AssignmentOperator; + type BinaryOperator = AssignmentOperatorOrHigher | SyntaxKind.CommaToken; + type BinaryOperatorToken = Token; interface BinaryExpression extends Expression, Declaration { + kind: SyntaxKind.BinaryExpression; left: Expression; - operatorToken: Node; + operatorToken: BinaryOperatorToken; right: Expression; } interface ConditionalExpression extends Expression { + kind: SyntaxKind.ConditionalExpression; condition: Expression; - questionToken: Node; + questionToken: QuestionToken; whenTrue: Expression; - colonToken: Node; + colonToken: ColonToken; whenFalse: Expression; } type FunctionBody = Block; type ConciseBody = FunctionBody | Expression; interface FunctionExpression extends PrimaryExpression, FunctionLikeDeclaration { + kind: SyntaxKind.FunctionExpression; name?: Identifier; body: FunctionBody; } interface ArrowFunction extends Expression, FunctionLikeDeclaration { - equalsGreaterThanToken: Node; + kind: SyntaxKind.ArrowFunction; + equalsGreaterThanToken: EqualsGreaterThanToken; body: ConciseBody; } interface LiteralLikeNode extends Node { @@ -717,29 +807,46 @@ declare namespace ts { interface LiteralExpression extends LiteralLikeNode, PrimaryExpression { _literalExpressionBrand: any; } + interface RegularExpressionLiteral extends LiteralExpression { + kind: SyntaxKind.RegularExpressionLiteral; + } + interface NoSubstitutionTemplateLiteral extends LiteralExpression { + kind: SyntaxKind.NoSubstitutionTemplateLiteral; + } interface NumericLiteral extends LiteralExpression { - _numericLiteralBrand: any; + kind: SyntaxKind.NumericLiteral; trailingComment?: string; } - interface TemplateLiteralFragment extends LiteralLikeNode { - _templateLiteralFragmentBrand: any; + interface TemplateHead extends LiteralLikeNode { + kind: SyntaxKind.TemplateHead; + } + interface TemplateMiddle extends LiteralLikeNode { + kind: SyntaxKind.TemplateMiddle; } - type Template = TemplateExpression | LiteralExpression; + interface TemplateTail extends LiteralLikeNode { + kind: SyntaxKind.TemplateTail; + } + type TemplateLiteral = TemplateExpression | NoSubstitutionTemplateLiteral; interface TemplateExpression extends PrimaryExpression { - head: TemplateLiteralFragment; + kind: SyntaxKind.TemplateExpression; + head: TemplateHead; templateSpans: NodeArray; } interface TemplateSpan extends Node { + kind: SyntaxKind.TemplateSpan; expression: Expression; - literal: TemplateLiteralFragment; + literal: TemplateMiddle | TemplateTail; } interface ParenthesizedExpression extends PrimaryExpression { + kind: SyntaxKind.ParenthesizedExpression; expression: Expression; } interface ArrayLiteralExpression extends PrimaryExpression { + kind: SyntaxKind.ArrayLiteralExpression; elements: NodeArray; } interface SpreadElementExpression extends Expression { + kind: SyntaxKind.SpreadElementExpression; expression: Expression; } /** @@ -752,104 +859,141 @@ declare namespace ts { properties: NodeArray; } interface ObjectLiteralExpression extends ObjectLiteralExpressionBase { + kind: SyntaxKind.ObjectLiteralExpression; } type EntityNameExpression = Identifier | PropertyAccessEntityNameExpression; type EntityNameOrEntityNameExpression = EntityName | EntityNameExpression; interface PropertyAccessExpression extends MemberExpression, Declaration { + kind: SyntaxKind.PropertyAccessExpression; expression: LeftHandSideExpression; name: Identifier; } + interface SuperPropertyAccessExpression extends PropertyAccessExpression { + expression: SuperExpression; + } /** Brand for a PropertyAccessExpression which, like a QualifiedName, consists of a sequence of identifiers separated by dots. */ interface PropertyAccessEntityNameExpression extends PropertyAccessExpression { _propertyAccessExpressionLikeQualifiedNameBrand?: any; expression: EntityNameExpression; } interface ElementAccessExpression extends MemberExpression { + kind: SyntaxKind.ElementAccessExpression; expression: LeftHandSideExpression; argumentExpression?: Expression; } + interface SuperElementAccessExpression extends ElementAccessExpression { + expression: SuperExpression; + } + type SuperProperty = SuperPropertyAccessExpression | SuperElementAccessExpression; interface CallExpression extends LeftHandSideExpression, Declaration { + kind: SyntaxKind.CallExpression; expression: LeftHandSideExpression; typeArguments?: NodeArray; arguments: NodeArray; } + interface SuperCall extends CallExpression { + expression: SuperExpression; + } interface ExpressionWithTypeArguments extends TypeNode { + kind: SyntaxKind.ExpressionWithTypeArguments; expression: LeftHandSideExpression; typeArguments?: NodeArray; } - interface NewExpression extends CallExpression, PrimaryExpression { + interface NewExpression extends PrimaryExpression, Declaration { + kind: SyntaxKind.NewExpression; + expression: LeftHandSideExpression; + typeArguments?: NodeArray; + arguments: NodeArray; } interface TaggedTemplateExpression extends MemberExpression { + kind: SyntaxKind.TaggedTemplateExpression; tag: LeftHandSideExpression; - template: Template; + template: TemplateLiteral; } type CallLikeExpression = CallExpression | NewExpression | TaggedTemplateExpression | Decorator; interface AsExpression extends Expression { + kind: SyntaxKind.AsExpression; expression: Expression; type: TypeNode; } interface TypeAssertion extends UnaryExpression { + kind: SyntaxKind.TypeAssertionExpression; type: TypeNode; expression: UnaryExpression; } type AssertionExpression = TypeAssertion | AsExpression; interface NonNullExpression extends LeftHandSideExpression { + kind: SyntaxKind.NonNullExpression; expression: Expression; } interface JsxElement extends PrimaryExpression { + kind: SyntaxKind.JsxElement; openingElement: JsxOpeningElement; children: NodeArray; closingElement: JsxClosingElement; } type JsxTagNameExpression = PrimaryExpression | PropertyAccessExpression; interface JsxOpeningElement extends Expression { - _openingElementBrand?: any; + kind: SyntaxKind.JsxOpeningElement; tagName: JsxTagNameExpression; attributes: NodeArray; } - interface JsxSelfClosingElement extends PrimaryExpression, JsxOpeningElement { - _selfClosingElementBrand?: any; + interface JsxSelfClosingElement extends PrimaryExpression { + kind: SyntaxKind.JsxSelfClosingElement; + tagName: JsxTagNameExpression; + attributes: NodeArray; } type JsxOpeningLikeElement = JsxSelfClosingElement | JsxOpeningElement; type JsxAttributeLike = JsxAttribute | JsxSpreadAttribute; interface JsxAttribute extends Node { + kind: SyntaxKind.JsxAttribute; name: Identifier; initializer?: StringLiteral | JsxExpression; } interface JsxSpreadAttribute extends Node { + kind: SyntaxKind.JsxSpreadAttribute; expression: Expression; } interface JsxClosingElement extends Node { + kind: SyntaxKind.JsxClosingElement; tagName: JsxTagNameExpression; } interface JsxExpression extends Expression { + kind: SyntaxKind.JsxExpression; expression?: Expression; } interface JsxText extends Node { - _jsxTextExpressionBrand: any; + kind: SyntaxKind.JsxText; } type JsxChild = JsxText | JsxExpression | JsxElement | JsxSelfClosingElement; interface Statement extends Node { _statementBrand: any; } interface EmptyStatement extends Statement { + kind: SyntaxKind.EmptyStatement; } interface DebuggerStatement extends Statement { + kind: SyntaxKind.DebuggerStatement; } interface MissingDeclaration extends DeclarationStatement, ClassElement, ObjectLiteralElement, TypeElement { + kind: SyntaxKind.MissingDeclaration; name?: Identifier; } type BlockLike = SourceFile | Block | ModuleBlock | CaseClause; interface Block extends Statement { + kind: SyntaxKind.Block; statements: NodeArray; } interface VariableStatement extends Statement { + kind: SyntaxKind.VariableStatement; declarationList: VariableDeclarationList; } interface ExpressionStatement extends Statement { + kind: SyntaxKind.ExpressionStatement; expression: Expression; } interface IfStatement extends Statement { + kind: SyntaxKind.IfStatement; expression: Expression; thenStatement: Statement; elseStatement?: Statement; @@ -858,68 +1002,85 @@ declare namespace ts { statement: Statement; } interface DoStatement extends IterationStatement { + kind: SyntaxKind.DoStatement; expression: Expression; } interface WhileStatement extends IterationStatement { + kind: SyntaxKind.WhileStatement; expression: Expression; } type ForInitializer = VariableDeclarationList | Expression; interface ForStatement extends IterationStatement { + kind: SyntaxKind.ForStatement; initializer?: ForInitializer; condition?: Expression; incrementor?: Expression; } interface ForInStatement extends IterationStatement { + kind: SyntaxKind.ForInStatement; initializer: ForInitializer; expression: Expression; } interface ForOfStatement extends IterationStatement { + kind: SyntaxKind.ForOfStatement; initializer: ForInitializer; expression: Expression; } interface BreakStatement extends Statement { + kind: SyntaxKind.BreakStatement; label?: Identifier; } interface ContinueStatement extends Statement { + kind: SyntaxKind.ContinueStatement; label?: Identifier; } type BreakOrContinueStatement = BreakStatement | ContinueStatement; interface ReturnStatement extends Statement { + kind: SyntaxKind.ReturnStatement; expression?: Expression; } interface WithStatement extends Statement { + kind: SyntaxKind.WithStatement; expression: Expression; statement: Statement; } interface SwitchStatement extends Statement { + kind: SyntaxKind.SwitchStatement; expression: Expression; caseBlock: CaseBlock; possiblyExhaustive?: boolean; } interface CaseBlock extends Node { + kind: SyntaxKind.CaseBlock; clauses: NodeArray; } interface CaseClause extends Node { + kind: SyntaxKind.CaseClause; expression: Expression; statements: NodeArray; } interface DefaultClause extends Node { + kind: SyntaxKind.DefaultClause; statements: NodeArray; } type CaseOrDefaultClause = CaseClause | DefaultClause; interface LabeledStatement extends Statement { + kind: SyntaxKind.LabeledStatement; label: Identifier; statement: Statement; } interface ThrowStatement extends Statement { + kind: SyntaxKind.ThrowStatement; expression: Expression; } interface TryStatement extends Statement { + kind: SyntaxKind.TryStatement; tryBlock: Block; catchClause?: CatchClause; finallyBlock?: Block; } interface CatchClause extends Node { + kind: SyntaxKind.CatchClause; variableDeclaration: VariableDeclaration; block: Block; } @@ -931,9 +1092,11 @@ declare namespace ts { members: NodeArray; } interface ClassDeclaration extends ClassLikeDeclaration, DeclarationStatement { + kind: SyntaxKind.ClassDeclaration; name?: Identifier; } interface ClassExpression extends ClassLikeDeclaration, PrimaryExpression { + kind: SyntaxKind.ClassExpression; } interface ClassElement extends Declaration { _classElementBrand: any; @@ -942,85 +1105,108 @@ declare namespace ts { interface TypeElement extends Declaration { _typeElementBrand: any; name?: PropertyName; - questionToken?: Node; + questionToken?: QuestionToken; } interface InterfaceDeclaration extends DeclarationStatement { + kind: SyntaxKind.InterfaceDeclaration; name: Identifier; typeParameters?: NodeArray; heritageClauses?: NodeArray; members: NodeArray; } interface HeritageClause extends Node { + kind: SyntaxKind.HeritageClause; token: SyntaxKind; types?: NodeArray; } interface TypeAliasDeclaration extends DeclarationStatement { + kind: SyntaxKind.TypeAliasDeclaration; name: Identifier; typeParameters?: NodeArray; type: TypeNode; } interface EnumMember extends Declaration { + kind: SyntaxKind.EnumMember; name: PropertyName; initializer?: Expression; } interface EnumDeclaration extends DeclarationStatement { + kind: SyntaxKind.EnumDeclaration; name: Identifier; members: NodeArray; } type ModuleBody = ModuleBlock | ModuleDeclaration; type ModuleName = Identifier | StringLiteral; interface ModuleDeclaration extends DeclarationStatement { + kind: SyntaxKind.ModuleDeclaration; name: Identifier | LiteralExpression; - body?: ModuleBlock | ModuleDeclaration; + body?: ModuleBlock | NamespaceDeclaration; + } + interface NamespaceDeclaration extends ModuleDeclaration { + name: Identifier; + body: ModuleBlock | NamespaceDeclaration; } interface ModuleBlock extends Node, Statement { + kind: SyntaxKind.ModuleBlock; statements: NodeArray; } type ModuleReference = EntityName | ExternalModuleReference; interface ImportEqualsDeclaration extends DeclarationStatement { + kind: SyntaxKind.ImportEqualsDeclaration; name: Identifier; moduleReference: ModuleReference; } interface ExternalModuleReference extends Node { + kind: SyntaxKind.ExternalModuleReference; expression?: Expression; } interface ImportDeclaration extends Statement { + kind: SyntaxKind.ImportDeclaration; importClause?: ImportClause; moduleSpecifier: Expression; } type NamedImportBindings = NamespaceImport | NamedImports; interface ImportClause extends Declaration { + kind: SyntaxKind.ImportClause; name?: Identifier; namedBindings?: NamedImportBindings; } interface NamespaceImport extends Declaration { + kind: SyntaxKind.NamespaceImport; name: Identifier; } interface NamespaceExportDeclaration extends DeclarationStatement { + kind: SyntaxKind.NamespaceExportDeclaration; name: Identifier; moduleReference: LiteralLikeNode; } interface ExportDeclaration extends DeclarationStatement { + kind: SyntaxKind.ExportDeclaration; exportClause?: NamedExports; moduleSpecifier?: Expression; } interface NamedImports extends Node { + kind: SyntaxKind.NamedImports; elements: NodeArray; } interface NamedExports extends Node { + kind: SyntaxKind.NamedExports; elements: NodeArray; } type NamedImportsOrExports = NamedImports | NamedExports; interface ImportSpecifier extends Declaration { + kind: SyntaxKind.ImportSpecifier; propertyName?: Identifier; name: Identifier; } interface ExportSpecifier extends Declaration { + kind: SyntaxKind.ExportSpecifier; propertyName?: Identifier; name: Identifier; } type ImportOrExportSpecifier = ImportSpecifier | ExportSpecifier; interface ExportAssignment extends DeclarationStatement { + kind: SyntaxKind.ExportAssignment; isExportEquals?: boolean; expression: Expression; } @@ -1032,95 +1218,121 @@ declare namespace ts { kind: SyntaxKind; } interface JSDocTypeExpression extends Node { + kind: SyntaxKind.JSDocTypeExpression; type: JSDocType; } interface JSDocType extends TypeNode { _jsDocTypeBrand: any; } interface JSDocAllType extends JSDocType { - _JSDocAllTypeBrand: any; + kind: SyntaxKind.JSDocAllType; } interface JSDocUnknownType extends JSDocType { - _JSDocUnknownTypeBrand: any; + kind: SyntaxKind.JSDocUnknownType; } interface JSDocArrayType extends JSDocType { + kind: SyntaxKind.JSDocArrayType; elementType: JSDocType; } interface JSDocUnionType extends JSDocType { + kind: SyntaxKind.JSDocUnionType; types: NodeArray; } interface JSDocTupleType extends JSDocType { + kind: SyntaxKind.JSDocTupleType; types: NodeArray; } interface JSDocNonNullableType extends JSDocType { + kind: SyntaxKind.JSDocNonNullableType; type: JSDocType; } interface JSDocNullableType extends JSDocType { + kind: SyntaxKind.JSDocNullableType; type: JSDocType; } - interface JSDocRecordType extends JSDocType, TypeLiteralNode { + interface JSDocRecordType extends JSDocType { + kind: SyntaxKind.JSDocRecordType; literal: TypeLiteralNode; } interface JSDocTypeReference extends JSDocType { + kind: SyntaxKind.JSDocTypeReference; name: EntityName; typeArguments: NodeArray; } interface JSDocOptionalType extends JSDocType { + kind: SyntaxKind.JSDocOptionalType; type: JSDocType; } interface JSDocFunctionType extends JSDocType, SignatureDeclaration { + kind: SyntaxKind.JSDocFunctionType; parameters: NodeArray; type: JSDocType; } interface JSDocVariadicType extends JSDocType { + kind: SyntaxKind.JSDocVariadicType; type: JSDocType; } interface JSDocConstructorType extends JSDocType { + kind: SyntaxKind.JSDocConstructorType; type: JSDocType; } interface JSDocThisType extends JSDocType { + kind: SyntaxKind.JSDocThisType; type: JSDocType; } interface JSDocLiteralType extends JSDocType { + kind: SyntaxKind.JSDocLiteralType; literal: LiteralTypeNode; } type JSDocTypeReferencingNode = JSDocThisType | JSDocConstructorType | JSDocVariadicType | JSDocOptionalType | JSDocNullableType | JSDocNonNullableType; interface JSDocRecordMember extends PropertySignature { + kind: SyntaxKind.JSDocRecordMember; name: Identifier | LiteralExpression; type?: JSDocType; } interface JSDoc extends Node { + kind: SyntaxKind.JSDocComment; tags: NodeArray | undefined; comment: string | undefined; } interface JSDocTag extends Node { - atToken: Node; + atToken: AtToken; tagName: Identifier; comment: string | undefined; } + interface JSDocUnknownTag extends JSDocTag { + kind: SyntaxKind.JSDocTag; + } interface JSDocTemplateTag extends JSDocTag { + kind: SyntaxKind.JSDocTemplateTag; typeParameters: NodeArray; } interface JSDocReturnTag extends JSDocTag { + kind: SyntaxKind.JSDocReturnTag; typeExpression: JSDocTypeExpression; } interface JSDocTypeTag extends JSDocTag { + kind: SyntaxKind.JSDocTypeTag; typeExpression: JSDocTypeExpression; } interface JSDocTypedefTag extends JSDocTag, Declaration { + kind: SyntaxKind.JSDocTypedefTag; name?: Identifier; typeExpression?: JSDocTypeExpression; jsDocTypeLiteral?: JSDocTypeLiteral; } interface JSDocPropertyTag extends JSDocTag, TypeElement { + kind: SyntaxKind.JSDocPropertyTag; name: Identifier; typeExpression: JSDocTypeExpression; } interface JSDocTypeLiteral extends JSDocType { + kind: SyntaxKind.JSDocTypeLiteral; jsDocPropertyTags?: NodeArray; jsDocTypeTag?: JSDocTypeTag; } interface JSDocParameterTag extends JSDocTag { + kind: SyntaxKind.JSDocParameterTag; /** the parameter name, if provided *before* the type (TypeScript-style) */ preParameterName?: Identifier; typeExpression?: JSDocTypeExpression; @@ -1149,7 +1361,7 @@ declare namespace ts { id?: number; } interface FlowStart extends FlowNode { - container?: FunctionExpression | ArrowFunction; + container?: FunctionExpression | ArrowFunction | MethodDeclaration; } interface FlowLabel extends FlowNode { antecedents: FlowNode[]; @@ -1178,8 +1390,9 @@ declare namespace ts { name: string; } interface SourceFile extends Declaration { + kind: SyntaxKind.SourceFile; statements: NodeArray; - endOfFileToken: Node; + endOfFileToken: Token; fileName: string; path: Path; text: string; @@ -1245,7 +1458,7 @@ declare namespace ts { * used for writing the JavaScript and declaration files. Otherwise, the writeFile parameter * will be invoked when writing the JavaScript and declaration files. */ - emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback, cancellationToken?: CancellationToken): EmitResult; + emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean): EmitResult; getOptionsDiagnostics(cancellationToken?: CancellationToken): Diagnostic[]; getGlobalDiagnostics(cancellationToken?: CancellationToken): Diagnostic[]; getSyntacticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[]; @@ -1372,6 +1585,7 @@ declare namespace ts { UseFullyQualifiedType = 128, InFirstTypeArgument = 256, InTypeAlias = 512, + UseTypeAliasValue = 1024, } enum SymbolFormatFlags { None = 0, @@ -1387,9 +1601,10 @@ declare namespace ts { type: Type; } interface ThisTypePredicate extends TypePredicateBase { - _thisTypePredicateBrand: any; + kind: TypePredicateKind.This; } interface IdentifierTypePredicate extends TypePredicateBase { + kind: TypePredicateKind.Identifier; parameterName: string; parameterIndex: number; } @@ -1495,8 +1710,6 @@ declare namespace ts { Intersection = 1048576, Anonymous = 2097152, Instantiated = 4194304, - ThisType = 268435456, - ObjectLiteralPatternWithComputedProperties = 536870912, Literal = 480, StringOrNumberLiteral = 96, PossiblyFalsy = 7406, @@ -1509,7 +1722,7 @@ declare namespace ts { StructuredType = 4161536, StructuredOrTypeParameter = 4177920, Narrowable = 4178943, - NotUnionOrUnit = 2589191, + NotUnionOrUnit = 2589185, } type DestructuringPattern = BindingPattern | ObjectLiteralExpression | ArrayLiteralExpression; interface Type { @@ -1531,6 +1744,7 @@ declare namespace ts { baseType: EnumType & UnionType; } interface ObjectType extends Type { + isObjectLiteralPatternWithComputedProperties?: boolean; } interface InterfaceType extends ObjectType { typeParameters: TypeParameter[]; @@ -1614,15 +1828,13 @@ declare namespace ts { Classic = 1, NodeJs = 2, } - type RootPaths = string[]; - type PathSubstitutions = MapLike; - type TsConfigOnlyOptions = RootPaths | PathSubstitutions; - type CompilerOptionsValue = string | number | boolean | (string | number)[] | TsConfigOnlyOptions; + type CompilerOptionsValue = string | number | boolean | (string | number)[] | string[] | MapLike; interface CompilerOptions { allowJs?: boolean; allowSyntheticDefaultImports?: boolean; allowUnreachableCode?: boolean; allowUnusedLabels?: boolean; + alwaysStrict?: boolean; baseUrl?: string; charset?: string; declaration?: boolean; @@ -1660,13 +1872,13 @@ declare namespace ts { out?: string; outDir?: string; outFile?: string; - paths?: PathSubstitutions; + paths?: MapLike; preserveConstEnums?: boolean; project?: string; reactNamespace?: string; removeComments?: boolean; rootDir?: string; - rootDirs?: RootPaths; + rootDirs?: string[]; skipLibCheck?: boolean; skipDefaultLibCheck?: boolean; sourceMap?: boolean; @@ -1742,6 +1954,7 @@ declare namespace ts { raw?: any; errors: Diagnostic[]; wildcardDirectories?: MapLike; + compileOnSave?: boolean; } enum WatchDirectoryFlags { None = 0, @@ -1846,7 +2059,7 @@ declare namespace ts { directoryName: string; referenceCount: number; } - var sys: System; + let sys: System; } declare namespace ts { interface ErrorCallback { @@ -1944,10 +2157,6 @@ declare namespace ts { function updateSourceFile(sourceFile: SourceFile, newText: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile; } declare namespace ts { - /** The version of the TypeScript compiler release */ - const version = "2.1.0"; - function findConfigFile(searchPath: string, fileExists: (fileName: string) => boolean, configName?: string): string; - function resolveTripleslashReference(moduleName: string, containingFile: string): string; function getEffectiveTypeRoots(options: CompilerOptions, host: { directoryExists?: (directoryName: string) => boolean; getCurrentDirectory?: () => string; @@ -1958,9 +2167,24 @@ declare namespace ts { * is assumed to be the same as root directory of the project. */ function resolveTypeReferenceDirective(typeReferenceDirectiveName: string, containingFile: string, options: CompilerOptions, host: ModuleResolutionHost): ResolvedTypeReferenceDirectiveWithFailedLookupLocations; + /** + * Given a set of options, returns the set of type directive names + * that should be included for this program automatically. + * This list could either come from the config file, + * or from enumerating the types root + initial secondary types lookup location. + * More type directives might appear in the program later as a result of loading actual source files; + * this list is only the set of defaults that are implicitly included. + */ + function getAutomaticTypeDirectiveNames(options: CompilerOptions, host: ModuleResolutionHost): string[]; function resolveModuleName(moduleName: string, containingFile: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost): ResolvedModuleWithFailedLookupLocations; function nodeModuleNameResolver(moduleName: string, containingFile: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost): ResolvedModuleWithFailedLookupLocations; function classicNameResolver(moduleName: string, containingFile: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost): ResolvedModuleWithFailedLookupLocations; +} +declare namespace ts { + /** The version of the TypeScript compiler release */ + const version = "2.1.0"; + function findConfigFile(searchPath: string, fileExists: (fileName: string) => boolean, configName?: string): string; + function resolveTripleslashReference(moduleName: string, containingFile: string): string; function createCompilerHost(options: CompilerOptions, setParentNodes?: boolean): CompilerHost; function getPreEmitDiagnostics(program: Program, sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[]; interface FormatDiagnosticsHost { @@ -1970,15 +2194,6 @@ declare namespace ts { } function formatDiagnostics(diagnostics: Diagnostic[], host: FormatDiagnosticsHost): string; function flattenDiagnosticMessageText(messageText: string | DiagnosticMessageChain, newLine: string): string; - /** - * Given a set of options, returns the set of type directive names - * that should be included for this program automatically. - * This list could either come from the config file, - * or from enumerating the types root + initial secondary types lookup location. - * More type directives might appear in the program later as a result of loading actual source files; - * this list is only the set of defaults that are implicitly included. - */ - function getAutomaticTypeDirectiveNames(options: CompilerOptions, host: ModuleResolutionHost): string[]; function createProgram(rootNames: string[], options: CompilerOptions, host?: CompilerHost, oldProgram?: Program): Program; } declare namespace ts { @@ -1995,7 +2210,7 @@ declare namespace ts { * @param fileName The path to the config file * @param jsonText The text of the config file */ - function parseConfigFileTextToJson(fileName: string, jsonText: string): { + function parseConfigFileTextToJson(fileName: string, jsonText: string, stripComments?: boolean): { config?: any; error?: Diagnostic; }; @@ -2007,12 +2222,13 @@ declare namespace ts { * file to. e.g. outDir */ function parseJsonConfigFileContent(json: any, host: ParseConfigHost, basePath: string, existingOptions?: CompilerOptions, configFileName?: string, resolutionStack?: Path[]): ParsedCommandLine; + function convertCompileOnSaveOptionFromJson(jsonOption: any, basePath: string, errors: Diagnostic[]): boolean; function convertCompilerOptionsFromJson(jsonOptions: any, basePath: string, configFileName?: string): { options: CompilerOptions; errors: Diagnostic[]; }; function convertTypingOptionsFromJson(jsonOptions: any, basePath: string, configFileName?: string): { - options: CompilerOptions; + options: TypingOptions; errors: Diagnostic[]; }; } @@ -2118,6 +2334,7 @@ declare namespace ts { readDirectory?(path: string, extensions?: string[], exclude?: string[], include?: string[]): string[]; readFile?(path: string, encoding?: string): string; fileExists?(path: string): boolean; + getTypeRootsVersion?(): number; resolveModuleNames?(moduleNames: string[], containingFile: string): ResolvedModule[]; resolveTypeReferenceDirectives?(typeDirectiveNames: string[], containingFile: string): ResolvedTypeReferenceDirective[]; directoryExists?(directoryName: string): boolean; @@ -2155,18 +2372,20 @@ declare namespace ts { getDocumentHighlights(fileName: string, position: number, filesToSearch: string[]): DocumentHighlights[]; /** @deprecated */ getOccurrencesAtPosition(fileName: string, position: number): ReferenceEntry[]; - getNavigateToItems(searchValue: string, maxResultCount?: number, fileName?: string): NavigateToItem[]; + getNavigateToItems(searchValue: string, maxResultCount?: number, fileName?: string, excludeDtsFiles?: boolean): NavigateToItem[]; getNavigationBarItems(fileName: string): NavigationBarItem[]; + getNavigationTree(fileName: string): NavigationTree; getOutliningSpans(fileName: string): OutliningSpan[]; getTodoComments(fileName: string, descriptors: TodoCommentDescriptor[]): TodoComment[]; getBraceMatchingAtPosition(fileName: string, position: number): TextSpan[]; - getIndentationAtPosition(fileName: string, position: number, options: EditorOptions): number; - getFormattingEditsForRange(fileName: string, start: number, end: number, options: FormatCodeOptions): TextChange[]; - getFormattingEditsForDocument(fileName: string, options: FormatCodeOptions): TextChange[]; - getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: FormatCodeOptions): TextChange[]; + getIndentationAtPosition(fileName: string, position: number, options: EditorOptions | EditorSettings): number; + getFormattingEditsForRange(fileName: string, start: number, end: number, options: FormatCodeOptions | FormatCodeSettings): TextChange[]; + getFormattingEditsForDocument(fileName: string, options: FormatCodeOptions | FormatCodeSettings): TextChange[]; + getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: FormatCodeOptions | FormatCodeSettings): TextChange[]; getDocCommentTemplateAtPosition(fileName: string, position: number): TextInsertion; isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean; - getEmitOutput(fileName: string): EmitOutput; + getCodeFixesAtPosition(fileName: string, start: number, end: number, errorCodes: number[]): CodeAction[]; + getEmitOutput(fileName: string, emitOnlyDtsFiles?: boolean): EmitOutput; getProgram(): Program; dispose(): void; } @@ -2178,6 +2397,12 @@ declare namespace ts { textSpan: TextSpan; classificationType: string; } + /** + * Navigation bar interface designed for visual studio's dual-column layout. + * This does not form a proper tree. + * The navbar is returned as a list of top-level items, each of which has a list of child items. + * Child items always have an empty array for their `childItems`. + */ interface NavigationBarItem { text: string; kind: string; @@ -2188,6 +2413,25 @@ declare namespace ts { bolded: boolean; grayed: boolean; } + /** + * Node in a tree of nested declarations in a file. + * The top node is always a script or module node. + */ + interface NavigationTree { + /** Name of the declaration, or a short description, e.g. "". */ + text: string; + /** A ScriptElementKind */ + kind: string; + /** ScriptElementKindModifier separated by commas, e.g. "public,abstract" */ + kindModifiers: string; + /** + * Spans of the nodes that generated this declaration. + * There will be more than one if this is the result of merging. + */ + spans: TextSpan[]; + /** Present if non-empty */ + childItems?: NavigationTree[]; + } interface TodoCommentDescriptor { text: string; priority: number; @@ -2201,6 +2445,16 @@ declare namespace ts { span: TextSpan; newText: string; } + interface FileTextChanges { + fileName: string; + textChanges: TextChange[]; + } + interface CodeAction { + /** Description of the code action to display in the UI of the editor */ + description: string; + /** Text changes to apply to each file as part of the code action */ + changes: FileTextChanges[]; + } interface TextInsertion { newText: string; /** The position in newText the caret should point to after the insertion. */ @@ -2246,6 +2500,11 @@ declare namespace ts { containerName: string; containerKind: string; } + enum IndentStyle { + None = 0, + Block = 1, + Smart = 2, + } interface EditorOptions { BaseIndentSize?: number; IndentSize: number; @@ -2254,10 +2513,13 @@ declare namespace ts { ConvertTabsToSpaces: boolean; IndentStyle: IndentStyle; } - enum IndentStyle { - None = 0, - Block = 1, - Smart = 2, + interface EditorSettings { + baseIndentSize?: number; + indentSize?: number; + tabSize?: number; + newLineCharacter?: string; + convertTabsToSpaces?: boolean; + indentStyle?: IndentStyle; } interface FormatCodeOptions extends EditorOptions { InsertSpaceAfterCommaDelimiter: boolean; @@ -2273,7 +2535,21 @@ declare namespace ts { InsertSpaceAfterTypeAssertion?: boolean; PlaceOpenBraceOnNewLineForFunctions: boolean; PlaceOpenBraceOnNewLineForControlBlocks: boolean; - [s: string]: boolean | number | string | undefined; + } + interface FormatCodeSettings extends EditorSettings { + insertSpaceAfterCommaDelimiter?: boolean; + insertSpaceAfterSemicolonInForStatements?: boolean; + insertSpaceBeforeAndAfterBinaryOperators?: boolean; + insertSpaceAfterKeywordsInControlFlowStatements?: boolean; + insertSpaceAfterFunctionKeywordForAnonymousFunctions?: boolean; + insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis?: boolean; + insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets?: boolean; + insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces?: boolean; + insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces?: boolean; + insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces?: boolean; + insertSpaceAfterTypeAssertion?: boolean; + placeOpenBraceOnNewLineForFunctions?: boolean; + placeOpenBraceOnNewLineForControlBlocks?: boolean; } interface DefinitionInfo { fileName: string; @@ -2366,7 +2642,11 @@ declare namespace ts { argumentCount: number; } interface CompletionInfo { + isGlobalCompletion: boolean; isMemberCompletion: boolean; + /** + * true when the current location also allows for a new identifier + */ isNewIdentifierLocation: boolean; entries: CompletionEntry[]; } @@ -2687,8 +2967,10 @@ declare namespace ts { interface DisplayPartsSymbolWriter extends SymbolWriter { displayParts(): SymbolDisplayPart[]; } + function toEditorSettings(options: EditorOptions | EditorSettings): EditorSettings; function displayPartsToString(displayParts: SymbolDisplayPart[]): string; function getDefaultCompilerOptions(): CompilerOptions; + function getSupportedCodeFixes(): string[]; function createLanguageServiceSourceFile(fileName: string, scriptSnapshot: IScriptSnapshot, scriptTarget: ScriptTarget, version: string, setNodeParents: boolean, scriptKind?: ScriptKind): SourceFile; let disableIncrementalParsing: boolean; function updateLanguageServiceSourceFile(sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string, textChangeRange: TextChangeRange, aggressiveChecks?: boolean): SourceFile; diff --git a/lib/typescriptServices.js b/lib/typescriptServices.js index 14fbdce59a832..c534a7259019f 100644 --- a/lib/typescriptServices.js +++ b/lib/typescriptServices.js @@ -502,6 +502,7 @@ var ts; TypeFormatFlags[TypeFormatFlags["UseFullyQualifiedType"] = 128] = "UseFullyQualifiedType"; TypeFormatFlags[TypeFormatFlags["InFirstTypeArgument"] = 256] = "InFirstTypeArgument"; TypeFormatFlags[TypeFormatFlags["InTypeAlias"] = 512] = "InTypeAlias"; + TypeFormatFlags[TypeFormatFlags["UseTypeAliasValue"] = 1024] = "UseTypeAliasValue"; })(ts.TypeFormatFlags || (ts.TypeFormatFlags = {})); var TypeFormatFlags = ts.TypeFormatFlags; (function (SymbolFormatFlags) { @@ -685,8 +686,6 @@ var ts; TypeFlags[TypeFlags["ContainsObjectLiteral"] = 67108864] = "ContainsObjectLiteral"; /* @internal */ TypeFlags[TypeFlags["ContainsAnyFunctionType"] = 134217728] = "ContainsAnyFunctionType"; - TypeFlags[TypeFlags["ThisType"] = 268435456] = "ThisType"; - TypeFlags[TypeFlags["ObjectLiteralPatternWithComputedProperties"] = 536870912] = "ObjectLiteralPatternWithComputedProperties"; /* @internal */ TypeFlags[TypeFlags["Nullable"] = 6144] = "Nullable"; TypeFlags[TypeFlags["Literal"] = 480] = "Literal"; @@ -709,7 +708,7 @@ var ts; // 'Narrowable' types are types where narrowing actually narrows. // This *should* be every type other than null, undefined, void, and never TypeFlags[TypeFlags["Narrowable"] = 4178943] = "Narrowable"; - TypeFlags[TypeFlags["NotUnionOrUnit"] = 2589191] = "NotUnionOrUnit"; + TypeFlags[TypeFlags["NotUnionOrUnit"] = 2589185] = "NotUnionOrUnit"; /* @internal */ TypeFlags[TypeFlags["RequiresWidening"] = 100663296] = "RequiresWidening"; /* @internal */ @@ -993,36 +992,44 @@ var ts; })(ts.TransformFlags || (ts.TransformFlags = {})); var TransformFlags = ts.TransformFlags; /* @internal */ - (function (NodeEmitFlags) { - NodeEmitFlags[NodeEmitFlags["EmitEmitHelpers"] = 1] = "EmitEmitHelpers"; - NodeEmitFlags[NodeEmitFlags["EmitExportStar"] = 2] = "EmitExportStar"; - NodeEmitFlags[NodeEmitFlags["EmitSuperHelper"] = 4] = "EmitSuperHelper"; - NodeEmitFlags[NodeEmitFlags["EmitAdvancedSuperHelper"] = 8] = "EmitAdvancedSuperHelper"; - NodeEmitFlags[NodeEmitFlags["UMDDefine"] = 16] = "UMDDefine"; - NodeEmitFlags[NodeEmitFlags["SingleLine"] = 32] = "SingleLine"; - NodeEmitFlags[NodeEmitFlags["AdviseOnEmitNode"] = 64] = "AdviseOnEmitNode"; - NodeEmitFlags[NodeEmitFlags["NoSubstitution"] = 128] = "NoSubstitution"; - NodeEmitFlags[NodeEmitFlags["CapturesThis"] = 256] = "CapturesThis"; - NodeEmitFlags[NodeEmitFlags["NoLeadingSourceMap"] = 512] = "NoLeadingSourceMap"; - NodeEmitFlags[NodeEmitFlags["NoTrailingSourceMap"] = 1024] = "NoTrailingSourceMap"; - NodeEmitFlags[NodeEmitFlags["NoSourceMap"] = 1536] = "NoSourceMap"; - NodeEmitFlags[NodeEmitFlags["NoNestedSourceMaps"] = 2048] = "NoNestedSourceMaps"; - NodeEmitFlags[NodeEmitFlags["NoTokenLeadingSourceMaps"] = 4096] = "NoTokenLeadingSourceMaps"; - NodeEmitFlags[NodeEmitFlags["NoTokenTrailingSourceMaps"] = 8192] = "NoTokenTrailingSourceMaps"; - NodeEmitFlags[NodeEmitFlags["NoTokenSourceMaps"] = 12288] = "NoTokenSourceMaps"; - NodeEmitFlags[NodeEmitFlags["NoLeadingComments"] = 16384] = "NoLeadingComments"; - NodeEmitFlags[NodeEmitFlags["NoTrailingComments"] = 32768] = "NoTrailingComments"; - NodeEmitFlags[NodeEmitFlags["NoComments"] = 49152] = "NoComments"; - NodeEmitFlags[NodeEmitFlags["NoNestedComments"] = 65536] = "NoNestedComments"; - NodeEmitFlags[NodeEmitFlags["ExportName"] = 131072] = "ExportName"; - NodeEmitFlags[NodeEmitFlags["LocalName"] = 262144] = "LocalName"; - NodeEmitFlags[NodeEmitFlags["Indented"] = 524288] = "Indented"; - NodeEmitFlags[NodeEmitFlags["NoIndentation"] = 1048576] = "NoIndentation"; - NodeEmitFlags[NodeEmitFlags["AsyncFunctionBody"] = 2097152] = "AsyncFunctionBody"; - NodeEmitFlags[NodeEmitFlags["ReuseTempVariableScope"] = 4194304] = "ReuseTempVariableScope"; - NodeEmitFlags[NodeEmitFlags["CustomPrologue"] = 8388608] = "CustomPrologue"; - })(ts.NodeEmitFlags || (ts.NodeEmitFlags = {})); - var NodeEmitFlags = ts.NodeEmitFlags; + (function (EmitFlags) { + EmitFlags[EmitFlags["EmitEmitHelpers"] = 1] = "EmitEmitHelpers"; + EmitFlags[EmitFlags["EmitExportStar"] = 2] = "EmitExportStar"; + EmitFlags[EmitFlags["EmitSuperHelper"] = 4] = "EmitSuperHelper"; + EmitFlags[EmitFlags["EmitAdvancedSuperHelper"] = 8] = "EmitAdvancedSuperHelper"; + EmitFlags[EmitFlags["UMDDefine"] = 16] = "UMDDefine"; + EmitFlags[EmitFlags["SingleLine"] = 32] = "SingleLine"; + EmitFlags[EmitFlags["AdviseOnEmitNode"] = 64] = "AdviseOnEmitNode"; + EmitFlags[EmitFlags["NoSubstitution"] = 128] = "NoSubstitution"; + EmitFlags[EmitFlags["CapturesThis"] = 256] = "CapturesThis"; + EmitFlags[EmitFlags["NoLeadingSourceMap"] = 512] = "NoLeadingSourceMap"; + EmitFlags[EmitFlags["NoTrailingSourceMap"] = 1024] = "NoTrailingSourceMap"; + EmitFlags[EmitFlags["NoSourceMap"] = 1536] = "NoSourceMap"; + EmitFlags[EmitFlags["NoNestedSourceMaps"] = 2048] = "NoNestedSourceMaps"; + EmitFlags[EmitFlags["NoTokenLeadingSourceMaps"] = 4096] = "NoTokenLeadingSourceMaps"; + EmitFlags[EmitFlags["NoTokenTrailingSourceMaps"] = 8192] = "NoTokenTrailingSourceMaps"; + EmitFlags[EmitFlags["NoTokenSourceMaps"] = 12288] = "NoTokenSourceMaps"; + EmitFlags[EmitFlags["NoLeadingComments"] = 16384] = "NoLeadingComments"; + EmitFlags[EmitFlags["NoTrailingComments"] = 32768] = "NoTrailingComments"; + EmitFlags[EmitFlags["NoComments"] = 49152] = "NoComments"; + EmitFlags[EmitFlags["NoNestedComments"] = 65536] = "NoNestedComments"; + EmitFlags[EmitFlags["ExportName"] = 131072] = "ExportName"; + EmitFlags[EmitFlags["LocalName"] = 262144] = "LocalName"; + EmitFlags[EmitFlags["Indented"] = 524288] = "Indented"; + EmitFlags[EmitFlags["NoIndentation"] = 1048576] = "NoIndentation"; + EmitFlags[EmitFlags["AsyncFunctionBody"] = 2097152] = "AsyncFunctionBody"; + EmitFlags[EmitFlags["ReuseTempVariableScope"] = 4194304] = "ReuseTempVariableScope"; + EmitFlags[EmitFlags["CustomPrologue"] = 8388608] = "CustomPrologue"; + })(ts.EmitFlags || (ts.EmitFlags = {})); + var EmitFlags = ts.EmitFlags; + /* @internal */ + (function (EmitContext) { + EmitContext[EmitContext["SourceFile"] = 0] = "SourceFile"; + EmitContext[EmitContext["Expression"] = 1] = "Expression"; + EmitContext[EmitContext["IdentifierName"] = 2] = "IdentifierName"; + EmitContext[EmitContext["Unspecified"] = 3] = "Unspecified"; + })(ts.EmitContext || (ts.EmitContext = {})); + var EmitContext = ts.EmitContext; })(ts || (ts = {})); /*@internal*/ var ts; @@ -1032,7 +1039,6 @@ var ts; })(ts || (ts = {})); /*@internal*/ /** Performance measurements for the compiler. */ -var ts; (function (ts) { var performance; (function (performance) { @@ -1164,6 +1170,7 @@ var ts; contains: contains, remove: remove, forEachValue: forEachValueInMap, + getKeys: getKeys, clear: clear }; function forEachValueInMap(f) { @@ -1171,6 +1178,13 @@ var ts; f(key, files[key]); } } + function getKeys() { + var keys = []; + for (var key in files) { + keys.push(key); + } + return keys; + } // path should already be well-formed so it does not need to be normalized function get(path) { return files[toKey(path)]; @@ -1501,6 +1515,7 @@ var ts; return array1.concat(array2); } ts.concatenate = concatenate; + // TODO: fixme (N^2) - add optional comparer so collection can be sorted before deduplication. function deduplicate(array, areEqual) { var result; if (array) { @@ -1604,16 +1619,22 @@ var ts; * @param array A sorted array whose first element must be no larger than number * @param number The value to be searched for in the array. */ - function binarySearch(array, value) { + function binarySearch(array, value, comparer) { + if (!array || array.length === 0) { + return -1; + } var low = 0; var high = array.length - 1; + comparer = comparer !== undefined + ? comparer + : function (v1, v2) { return (v1 < v2 ? -1 : (v1 > v2 ? 1 : 0)); }; while (low <= high) { var middle = low + ((high - low) >> 1); var midValue = array[middle]; - if (midValue === value) { + if (comparer(midValue, value) === 0) { return middle; } - else if (midValue > value) { + else if (comparer(midValue, value) > 0) { high = middle - 1; } else { @@ -1929,6 +1950,56 @@ var ts; }; } ts.memoize = memoize; + function chain(a, b, c, d, e) { + if (e) { + var args_2 = []; + for (var i = 0; i < arguments.length; i++) { + args_2[i] = arguments[i]; + } + return function (t) { return compose.apply(void 0, map(args_2, function (f) { return f(t); })); }; + } + else if (d) { + return function (t) { return compose(a(t), b(t), c(t), d(t)); }; + } + else if (c) { + return function (t) { return compose(a(t), b(t), c(t)); }; + } + else if (b) { + return function (t) { return compose(a(t), b(t)); }; + } + else if (a) { + return function (t) { return compose(a(t)); }; + } + else { + return function (t) { return function (u) { return u; }; }; + } + } + ts.chain = chain; + function compose(a, b, c, d, e) { + if (e) { + var args_3 = []; + for (var i = 0; i < arguments.length; i++) { + args_3[i] = arguments[i]; + } + return function (t) { return reduceLeft(args_3, function (u, f) { return f(u); }, t); }; + } + else if (d) { + return function (t) { return d(c(b(a(t)))); }; + } + else if (c) { + return function (t) { return c(b(a(t))); }; + } + else if (b) { + return function (t) { return b(a(t)); }; + } + else if (a) { + return function (t) { return a(t); }; + } + else { + return function (t) { return t; }; + } + } + ts.compose = compose; function formatStringFromArgs(text, args, baseIndex) { baseIndex = baseIndex || 0; return text.replace(/{(\d+)}/g, function (match, index) { return args[+index + baseIndex]; }); @@ -2096,7 +2167,9 @@ var ts; return path.replace(/\\/g, "/"); } ts.normalizeSlashes = normalizeSlashes; - // Returns length of path root (i.e. length of "/", "x:/", "//server/share/, file:///user/files") + /** + * Returns length of path root (i.e. length of "/", "x:/", "//server/share/, file:///user/files") + */ function getRootLength(path) { if (path.charCodeAt(0) === 47 /* slash */) { if (path.charCodeAt(1) !== 47 /* slash */) @@ -2129,6 +2202,11 @@ var ts; return 0; } ts.getRootLength = getRootLength; + /** + * Internally, we represent paths as strings with '/' as the directory separator. + * When we make system calls (eg: LanguageServiceHost.getDirectory()), + * we expect the host to correctly handle paths in our specified format. + */ ts.directorySeparator = "/"; var directorySeparatorCharCode = 47 /* slash */; function getNormalizedParts(normalizedSlashedPath, rootLength) { @@ -2178,10 +2256,49 @@ var ts; return path && !isRootedDiskPath(path) && path.indexOf("://") !== -1; } ts.isUrl = isUrl; + function isExternalModuleNameRelative(moduleName) { + // TypeScript 1.0 spec (April 2014): 11.2.1 + // An external module name is "relative" if the first term is "." or "..". + return /^\.\.?($|[\\/])/.test(moduleName); + } + ts.isExternalModuleNameRelative = isExternalModuleNameRelative; + function getEmitScriptTarget(compilerOptions) { + return compilerOptions.target || 0 /* ES3 */; + } + ts.getEmitScriptTarget = getEmitScriptTarget; + function getEmitModuleKind(compilerOptions) { + return typeof compilerOptions.module === "number" ? + compilerOptions.module : + getEmitScriptTarget(compilerOptions) === 2 /* ES6 */ ? ts.ModuleKind.ES6 : ts.ModuleKind.CommonJS; + } + ts.getEmitModuleKind = getEmitModuleKind; + /* @internal */ + function hasZeroOrOneAsteriskCharacter(str) { + var seenAsterisk = false; + for (var i = 0; i < str.length; i++) { + if (str.charCodeAt(i) === 42 /* asterisk */) { + if (!seenAsterisk) { + seenAsterisk = true; + } + else { + // have already seen asterisk + return false; + } + } + } + return true; + } + ts.hasZeroOrOneAsteriskCharacter = hasZeroOrOneAsteriskCharacter; function isRootedDiskPath(path) { return getRootLength(path) !== 0; } ts.isRootedDiskPath = isRootedDiskPath; + function convertToRelativePath(absoluteOrRelativePath, basePath, getCanonicalFileName) { + return !isRootedDiskPath(absoluteOrRelativePath) + ? absoluteOrRelativePath + : getRelativePathToDirectoryOrUrl(basePath, absoluteOrRelativePath, basePath, getCanonicalFileName, /*isAbsolutePathAnUrl*/ false); + } + ts.convertToRelativePath = convertToRelativePath; function normalizedPathComponents(path, rootLength) { var normalizedParts = getNormalizedParts(path, rootLength); return [path.substr(0, rootLength)].concat(normalizedParts); @@ -2625,6 +2742,14 @@ var ts; return options && options.allowJs ? allSupportedExtensions : ts.supportedTypeScriptExtensions; } ts.getSupportedExtensions = getSupportedExtensions; + function hasJavaScriptFileExtension(fileName) { + return forEach(ts.supportedJavascriptExtensions, function (extension) { return fileExtensionIs(fileName, extension); }); + } + ts.hasJavaScriptFileExtension = hasJavaScriptFileExtension; + function hasTypeScriptFileExtension(fileName) { + return forEach(ts.supportedTypeScriptExtensions, function (extension) { return fileExtensionIs(fileName, extension); }); + } + ts.hasTypeScriptFileExtension = hasTypeScriptFileExtension; function isSupportedSourceFileName(fileName, compilerOptions) { if (!fileName) { return false; @@ -2737,7 +2862,6 @@ var ts; this.transformFlags = 0 /* None */; this.parent = undefined; this.original = undefined; - this.transformId = 0; } ts.objectAllocator = { getNodeConstructor: function () { return Node; }, @@ -2757,9 +2881,9 @@ var ts; var AssertionLevel = ts.AssertionLevel; var Debug; (function (Debug) { - var currentAssertionLevel; + Debug.currentAssertionLevel = 0 /* None */; function shouldAssert(level) { - return getCurrentAssertionLevel() >= level; + return Debug.currentAssertionLevel >= level; } Debug.shouldAssert = shouldAssert; function assert(expression, message, verboseDebugInfo) { @@ -2777,30 +2901,7 @@ var ts; Debug.assert(/*expression*/ false, message); } Debug.fail = fail; - function getCurrentAssertionLevel() { - if (currentAssertionLevel !== undefined) { - return currentAssertionLevel; - } - if (ts.sys === undefined) { - return 0 /* None */; - } - var developmentMode = /^development$/i.test(getEnvironmentVariable("NODE_ENV")); - currentAssertionLevel = developmentMode - ? 1 /* Normal */ - : 0 /* None */; - return currentAssertionLevel; - } })(Debug = ts.Debug || (ts.Debug = {})); - function getEnvironmentVariable(name, host) { - if (host && host.getEnvironmentVariable) { - return host.getEnvironmentVariable(name); - } - if (ts.sys && ts.sys.getEnvironmentVariable) { - return ts.sys.getEnvironmentVariable(name); - } - return ""; - } - ts.getEnvironmentVariable = getEnvironmentVariable; /** Remove an item from an array, moving everything to its right one space left. */ function orderedRemoveItemAt(array, index) { // This seems to be faster than either `array.splice(i, 1)` or `array.copyWithin(i, i+ 1)`. @@ -2836,6 +2937,84 @@ var ts; : (function (fileName) { return fileName.toLowerCase(); }); } ts.createGetCanonicalFileName = createGetCanonicalFileName; + /** + * patternStrings contains both pattern strings (containing "*") and regular strings. + * Return an exact match if possible, or a pattern match, or undefined. + * (These are verified by verifyCompilerOptions to have 0 or 1 "*" characters.) + */ + /* @internal */ + function matchPatternOrExact(patternStrings, candidate) { + var patterns = []; + for (var _i = 0, patternStrings_1 = patternStrings; _i < patternStrings_1.length; _i++) { + var patternString = patternStrings_1[_i]; + var pattern = tryParsePattern(patternString); + if (pattern) { + patterns.push(pattern); + } + else if (patternString === candidate) { + // pattern was matched as is - no need to search further + return patternString; + } + } + return findBestPatternMatch(patterns, function (_) { return _; }, candidate); + } + ts.matchPatternOrExact = matchPatternOrExact; + /* @internal */ + function patternText(_a) { + var prefix = _a.prefix, suffix = _a.suffix; + return prefix + "*" + suffix; + } + ts.patternText = patternText; + /** + * Given that candidate matches pattern, returns the text matching the '*'. + * E.g.: matchedText(tryParsePattern("foo*baz"), "foobarbaz") === "bar" + */ + /* @internal */ + function matchedText(pattern, candidate) { + Debug.assert(isPatternMatch(pattern, candidate)); + return candidate.substr(pattern.prefix.length, candidate.length - pattern.suffix.length); + } + ts.matchedText = matchedText; + /** Return the object corresponding to the best pattern to match `candidate`. */ + /* @internal */ + function findBestPatternMatch(values, getPattern, candidate) { + var matchedValue = undefined; + // use length of prefix as betterness criteria + var longestMatchPrefixLength = -1; + for (var _i = 0, values_1 = values; _i < values_1.length; _i++) { + var v = values_1[_i]; + var pattern = getPattern(v); + if (isPatternMatch(pattern, candidate) && pattern.prefix.length > longestMatchPrefixLength) { + longestMatchPrefixLength = pattern.prefix.length; + matchedValue = v; + } + } + return matchedValue; + } + ts.findBestPatternMatch = findBestPatternMatch; + function isPatternMatch(_a, candidate) { + var prefix = _a.prefix, suffix = _a.suffix; + return candidate.length >= prefix.length + suffix.length && + startsWith(candidate, prefix) && + endsWith(candidate, suffix); + } + /* @internal */ + function tryParsePattern(pattern) { + // This should be verified outside of here and a proper error thrown. + Debug.assert(hasZeroOrOneAsteriskCharacter(pattern)); + var indexOfStar = pattern.indexOf("*"); + return indexOfStar === -1 ? undefined : { + prefix: pattern.substr(0, indexOfStar), + suffix: pattern.substr(indexOfStar + 1) + }; + } + ts.tryParsePattern = tryParsePattern; + function positionIsSynthesized(pos) { + // This is a fast way of testing the following conditions: + // pos === undefined || pos === null || isNaN(pos) || pos < 0; + return !(pos >= 0); + } + ts.positionIsSynthesized = positionIsSynthesized; })(ts || (ts = {})); /// var ts; @@ -3040,9 +3219,17 @@ var ts; function isNode4OrLater() { return parseInt(process.version.charAt(1)) >= 4; } + function isFileSystemCaseSensitive() { + // win32\win64 are case insensitive platforms + if (platform === "win32" || platform === "win64") { + return false; + } + // convert current file name to upper case / lower case and check if file exists + // (guards against cases when name is already all uppercase or lowercase) + return !fileExists(__filename.toUpperCase()) || !fileExists(__filename.toLowerCase()); + } var platform = _os.platform(); - // win32\win64 are case insensitive platforms, MacOS (darwin) by default is also case insensitive - var useCaseSensitiveFileNames = platform !== "win32" && platform !== "win64" && platform !== "darwin"; + var useCaseSensitiveFileNames = isFileSystemCaseSensitive(); function readFile(fileName, encoding) { if (!fileExists(fileName)) { return undefined; @@ -3182,6 +3369,9 @@ var ts; // Node 4.0 `fs.watch` function supports the "recursive" option on both OSX and Windows // (ref: https://github.com/nodejs/node/pull/2649 and https://github.com/Microsoft/TypeScript/issues/4643) var options; + if (!directoryExists(directoryName)) { + return; + } if (isNode4OrLater() && (process.platform === "win32" || process.platform === "darwin")) { options = { persistent: true, recursive: !!recursive }; } @@ -3299,21 +3489,46 @@ var ts; realpath: realpath }; } + function recursiveCreateDirectory(directoryPath, sys) { + var basePath = ts.getDirectoryPath(directoryPath); + var shouldCreateParent = directoryPath !== basePath && !sys.directoryExists(basePath); + if (shouldCreateParent) { + recursiveCreateDirectory(basePath, sys); + } + if (shouldCreateParent || !sys.directoryExists(directoryPath)) { + sys.createDirectory(directoryPath); + } + } + var sys; if (typeof ChakraHost !== "undefined") { - return getChakraSystem(); + sys = getChakraSystem(); } else if (typeof WScript !== "undefined" && typeof ActiveXObject === "function") { - return getWScriptSystem(); + sys = getWScriptSystem(); } else if (typeof process !== "undefined" && process.nextTick && !process.browser && typeof require !== "undefined") { // process and process.nextTick checks if current environment is node-like // process.browser check excludes webpack and browserify - return getNodeSystem(); + sys = getNodeSystem(); } - else { - return undefined; // Unsupported host + if (sys) { + // patch writefile to create folder before writing the file + var originalWriteFile_1 = sys.writeFile; + sys.writeFile = function (path, data, writeBom) { + var directoryPath = ts.getDirectoryPath(ts.normalizeSlashes(path)); + if (directoryPath && !sys.directoryExists(directoryPath)) { + recursiveCreateDirectory(directoryPath, sys); + } + originalWriteFile_1.call(sys, path, data, writeBom); + }; } + return sys; })(); + if (ts.sys && ts.sys.getEnvironmentVariable) { + ts.Debug.currentAssertionLevel = /^development$/i.test(ts.sys.getEnvironmentVariable("NODE_ENV")) + ? 1 /* Normal */ + : 0 /* None */; + } })(ts || (ts = {})); // /// @@ -3641,7 +3856,7 @@ var ts; The_right_hand_side_of_a_for_in_statement_must_be_of_type_any_an_object_type_or_a_type_parameter: { code: 2407, category: ts.DiagnosticCategory.Error, key: "The_right_hand_side_of_a_for_in_statement_must_be_of_type_any_an_object_type_or_a_type_parameter_2407", message: "The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter." }, Setters_cannot_return_a_value: { code: 2408, category: ts.DiagnosticCategory.Error, key: "Setters_cannot_return_a_value_2408", message: "Setters cannot return a value." }, Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class: { code: 2409, category: ts.DiagnosticCategory.Error, key: "Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class_2409", message: "Return type of constructor signature must be assignable to the instance type of the class" }, - All_symbols_within_a_with_block_will_be_resolved_to_any: { code: 2410, category: ts.DiagnosticCategory.Error, key: "All_symbols_within_a_with_block_will_be_resolved_to_any_2410", message: "All symbols within a 'with' block will be resolved to 'any'." }, + The_with_statement_is_not_supported_All_symbols_in_a_with_block_will_have_type_any: { code: 2410, category: ts.DiagnosticCategory.Error, key: "The_with_statement_is_not_supported_All_symbols_in_a_with_block_will_have_type_any_2410", message: "The 'with' statement is not supported. All symbols in a 'with' block will have type 'any'." }, Property_0_of_type_1_is_not_assignable_to_string_index_type_2: { code: 2411, category: ts.DiagnosticCategory.Error, key: "Property_0_of_type_1_is_not_assignable_to_string_index_type_2_2411", message: "Property '{0}' of type '{1}' is not assignable to string index type '{2}'." }, Property_0_of_type_1_is_not_assignable_to_numeric_index_type_2: { code: 2412, category: ts.DiagnosticCategory.Error, key: "Property_0_of_type_1_is_not_assignable_to_numeric_index_type_2_2412", message: "Property '{0}' of type '{1}' is not assignable to numeric index type '{2}'." }, Numeric_index_type_0_is_not_assignable_to_string_index_type_1: { code: 2413, category: ts.DiagnosticCategory.Error, key: "Numeric_index_type_0_is_not_assignable_to_string_index_type_1_2413", message: "Numeric index type '{0}' is not assignable to string index type '{1}'." }, @@ -3802,7 +4017,7 @@ var ts; this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation: { code: 2683, category: ts.DiagnosticCategory.Error, key: "this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation_2683", message: "'this' implicitly has type 'any' because it does not have a type annotation." }, The_this_context_of_type_0_is_not_assignable_to_method_s_this_of_type_1: { code: 2684, category: ts.DiagnosticCategory.Error, key: "The_this_context_of_type_0_is_not_assignable_to_method_s_this_of_type_1_2684", message: "The 'this' context of type '{0}' is not assignable to method's 'this' of type '{1}'." }, The_this_types_of_each_signature_are_incompatible: { code: 2685, category: ts.DiagnosticCategory.Error, key: "The_this_types_of_each_signature_are_incompatible_2685", message: "The 'this' types of each signature are incompatible." }, - Identifier_0_must_be_imported_from_a_module: { code: 2686, category: ts.DiagnosticCategory.Error, key: "Identifier_0_must_be_imported_from_a_module_2686", message: "Identifier '{0}' must be imported from a module" }, + _0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead: { code: 2686, category: ts.DiagnosticCategory.Error, key: "_0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead_2686", message: "'{0}' refers to a UMD global, but the current file is a module. Consider adding an import instead." }, All_declarations_of_0_must_have_identical_modifiers: { code: 2687, category: ts.DiagnosticCategory.Error, key: "All_declarations_of_0_must_have_identical_modifiers_2687", message: "All declarations of '{0}' must have identical modifiers." }, Cannot_find_type_definition_file_for_0: { code: 2688, category: ts.DiagnosticCategory.Error, key: "Cannot_find_type_definition_file_for_0_2688", message: "Cannot find type definition file for '{0}'." }, Cannot_extend_an_interface_0_Did_you_mean_implements: { code: 2689, category: ts.DiagnosticCategory.Error, key: "Cannot_extend_an_interface_0_Did_you_mean_implements_2689", message: "Cannot extend an interface '{0}'. Did you mean 'implements'?" }, @@ -4035,6 +4250,8 @@ var ts; No_types_specified_in_package_json_but_allowJs_is_set_so_returning_main_value_of_0: { code: 6137, category: ts.DiagnosticCategory.Message, key: "No_types_specified_in_package_json_but_allowJs_is_set_so_returning_main_value_of_0_6137", message: "No types specified in 'package.json' but 'allowJs' is set, so returning 'main' value of '{0}'" }, Property_0_is_declared_but_never_used: { code: 6138, category: ts.DiagnosticCategory.Error, key: "Property_0_is_declared_but_never_used_6138", message: "Property '{0}' is declared but never used." }, Import_emit_helpers_from_tslib: { code: 6139, category: ts.DiagnosticCategory.Message, key: "Import_emit_helpers_from_tslib_6139", message: "Import emit helpers from 'tslib'." }, + Auto_discovery_for_typings_is_enabled_in_project_0_Running_extra_resolution_pass_for_module_1_using_cache_location_2: { code: 6140, category: ts.DiagnosticCategory.Error, key: "Auto_discovery_for_typings_is_enabled_in_project_0_Running_extra_resolution_pass_for_module_1_using__6140", message: "Auto discovery for typings is enabled in project '{0}'. Running extra resolution pass for module '{1}' using cache location '{2}'." }, + Parse_in_strict_mode_and_emit_use_strict_for_each_source_file: { code: 6141, category: ts.DiagnosticCategory.Message, key: "Parse_in_strict_mode_and_emit_use_strict_for_each_source_file_6141", message: "Parse in strict mode and emit \"use strict\" for each source file" }, Variable_0_implicitly_has_an_1_type: { code: 7005, category: ts.DiagnosticCategory.Error, key: "Variable_0_implicitly_has_an_1_type_7005", message: "Variable '{0}' implicitly has an '{1}' type." }, Parameter_0_implicitly_has_an_1_type: { code: 7006, category: ts.DiagnosticCategory.Error, key: "Parameter_0_implicitly_has_an_1_type_7006", message: "Parameter '{0}' implicitly has an '{1}' type." }, Member_0_implicitly_has_an_1_type: { code: 7008, category: ts.DiagnosticCategory.Error, key: "Member_0_implicitly_has_an_1_type_7008", message: "Member '{0}' implicitly has an '{1}' type." }, @@ -4059,6 +4276,7 @@ var ts; Binding_element_0_implicitly_has_an_1_type: { code: 7031, category: ts.DiagnosticCategory.Error, key: "Binding_element_0_implicitly_has_an_1_type_7031", message: "Binding element '{0}' implicitly has an '{1}' type." }, Property_0_implicitly_has_type_any_because_its_set_accessor_lacks_a_parameter_type_annotation: { code: 7032, category: ts.DiagnosticCategory.Error, key: "Property_0_implicitly_has_type_any_because_its_set_accessor_lacks_a_parameter_type_annotation_7032", message: "Property '{0}' implicitly has type 'any', because its set accessor lacks a parameter type annotation." }, Property_0_implicitly_has_type_any_because_its_get_accessor_lacks_a_return_type_annotation: { code: 7033, category: ts.DiagnosticCategory.Error, key: "Property_0_implicitly_has_type_any_because_its_get_accessor_lacks_a_return_type_annotation_7033", message: "Property '{0}' implicitly has type 'any', because its get accessor lacks a return type annotation." }, + Variable_0_implicitly_has_type_any_in_some_locations_where_its_type_cannot_be_determined: { code: 7034, category: ts.DiagnosticCategory.Error, key: "Variable_0_implicitly_has_type_any_in_some_locations_where_its_type_cannot_be_determined_7034", message: "Variable '{0}' implicitly has type 'any' in some locations where its type cannot be determined." }, You_cannot_rename_this_element: { code: 8000, category: ts.DiagnosticCategory.Error, key: "You_cannot_rename_this_element_8000", message: "You cannot rename this element." }, You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library: { code: 8001, category: ts.DiagnosticCategory.Error, key: "You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library_8001", message: "You cannot rename elements that are defined in the standard TypeScript library." }, import_can_only_be_used_in_a_ts_file: { code: 8002, category: ts.DiagnosticCategory.Error, key: "import_can_only_be_used_in_a_ts_file_8002", message: "'import ... =' can only be used in a .ts file." }, @@ -4088,7 +4306,14 @@ var ts; super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class: { code: 17009, category: ts.DiagnosticCategory.Error, key: "super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class_17009", message: "'super' must be called before accessing 'this' in the constructor of a derived class." }, Unknown_typing_option_0: { code: 17010, category: ts.DiagnosticCategory.Error, key: "Unknown_typing_option_0_17010", message: "Unknown typing option '{0}'." }, Circularity_detected_while_resolving_configuration_Colon_0: { code: 18000, category: ts.DiagnosticCategory.Error, key: "Circularity_detected_while_resolving_configuration_Colon_0_18000", message: "Circularity detected while resolving configuration: {0}" }, - The_path_in_an_extends_options_must_be_relative_or_rooted: { code: 18001, category: ts.DiagnosticCategory.Error, key: "The_path_in_an_extends_options_must_be_relative_or_rooted_18001", message: "The path in an 'extends' options must be relative or rooted." } + The_path_in_an_extends_options_must_be_relative_or_rooted: { code: 18001, category: ts.DiagnosticCategory.Error, key: "The_path_in_an_extends_options_must_be_relative_or_rooted_18001", message: "The path in an 'extends' options must be relative or rooted." }, + Add_missing_super_call: { code: 90001, category: ts.DiagnosticCategory.Message, key: "Add_missing_super_call_90001", message: "Add missing 'super()' call." }, + Make_super_call_the_first_statement_in_the_constructor: { code: 90002, category: ts.DiagnosticCategory.Message, key: "Make_super_call_the_first_statement_in_the_constructor_90002", message: "Make 'super()' call the first statement in the constructor." }, + Change_extends_to_implements: { code: 90003, category: ts.DiagnosticCategory.Message, key: "Change_extends_to_implements_90003", message: "Change 'extends' to 'implements'" }, + Remove_unused_identifiers: { code: 90004, category: ts.DiagnosticCategory.Message, key: "Remove_unused_identifiers_90004", message: "Remove unused identifiers" }, + Implement_interface_on_reference: { code: 90005, category: ts.DiagnosticCategory.Message, key: "Implement_interface_on_reference_90005", message: "Implement interface on reference" }, + Implement_interface_on_class: { code: 90006, category: ts.DiagnosticCategory.Message, key: "Implement_interface_on_class_90006", message: "Implement interface on class" }, + Implement_inherited_abstract_class: { code: 90007, category: ts.DiagnosticCategory.Message, key: "Implement_inherited_abstract_class_90007", message: "Implement inherited abstract class" } }; })(ts || (ts = {})); /// @@ -5145,7 +5370,7 @@ var ts; return token = 69 /* Identifier */; } function scanBinaryOrOctalDigits(base) { - ts.Debug.assert(base !== 2 || base !== 8, "Expected either base 2 or base 8"); + ts.Debug.assert(base === 2 || base === 8, "Expected either base 2 or base 8"); var value = 0; // For counting number of digits; Valid binaryIntegerLiteral must have at least one binary digit following B or b. // Similarly valid octalIntegerLiteral must have at least one octal digit following o or O. @@ -6364,10 +6589,10 @@ var ts; return !!(ts.getCombinedNodeFlags(node) & 1 /* Let */); } ts.isLet = isLet; - function isSuperCallExpression(n) { + function isSuperCall(n) { return n.kind === 174 /* CallExpression */ && n.expression.kind === 95 /* SuperKeyword */; } - ts.isSuperCallExpression = isSuperCallExpression; + ts.isSuperCall = isSuperCall; function isPrologueDirective(node) { return node.kind === 202 /* ExpressionStatement */ && node.expression.kind === 9 /* StringLiteral */; } @@ -6636,6 +6861,12 @@ var ts; return node && node.kind === 147 /* MethodDeclaration */ && node.parent.kind === 171 /* ObjectLiteralExpression */; } ts.isObjectLiteralMethod = isObjectLiteralMethod; + function isObjectLiteralOrClassExpressionMethod(node) { + return node.kind === 147 /* MethodDeclaration */ && + (node.parent.kind === 171 /* ObjectLiteralExpression */ || + node.parent.kind === 192 /* ClassExpression */); + } + ts.isObjectLiteralOrClassExpressionMethod = isObjectLiteralOrClassExpressionMethod; function isIdentifierTypePredicate(predicate) { return predicate && predicate.kind === 1 /* Identifier */; } @@ -6723,11 +6954,11 @@ var ts; } ts.getThisContainer = getThisContainer; /** - * Given an super call\property node returns a closest node where either - * - super call\property is legal in the node and not legal in the parent node the node. + * Given an super call/property node, returns the closest node where + * - a super call/property access is legal in the node and not legal in the parent node the node. * i.e. super call is legal in constructor but not legal in the class body. - * - node is arrow function (so caller might need to call getSuperContainer in case it needs to climb higher) - * - super call\property is definitely illegal in the node (but might be legal in some subnode) + * - the container is an arrow function (so caller might need to call getSuperContainer again in case it needs to climb higher) + * - a super call/property is definitely illegal in the container (but might be legal in some subnode) * i.e. super property access is illegal in function declaration but can be legal in the statement list */ function getSuperContainer(node, stopOnFunctions) { @@ -6988,12 +7219,6 @@ var ts; return false; } ts.isPartOfExpression = isPartOfExpression; - function isExternalModuleNameRelative(moduleName) { - // TypeScript 1.0 spec (April 2014): 11.2.1 - // An external module name is "relative" if the first term is "." or "..". - return /^\.\.?($|[\\/])/.test(moduleName); - } - ts.isExternalModuleNameRelative = isExternalModuleNameRelative; function isInstantiatedModule(node, preserveConstEnums) { var moduleState = ts.getModuleInstanceState(node); return moduleState === 1 /* Instantiated */ || @@ -7655,16 +7880,10 @@ var ts; } ts.nodeStartsNewLexicalEnvironment = nodeStartsNewLexicalEnvironment; function nodeIsSynthesized(node) { - return positionIsSynthesized(node.pos) - || positionIsSynthesized(node.end); + return ts.positionIsSynthesized(node.pos) + || ts.positionIsSynthesized(node.end); } ts.nodeIsSynthesized = nodeIsSynthesized; - function positionIsSynthesized(pos) { - // This is a fast way of testing the following conditions: - // pos === undefined || pos === null || isNaN(pos) || pos < 0; - return !(pos >= 0); - } - ts.positionIsSynthesized = positionIsSynthesized; function getOriginalNode(node) { if (node) { while (node.original !== undefined) { @@ -8125,24 +8344,12 @@ var ts; function getDeclarationEmitOutputFilePath(sourceFile, host) { var options = host.getCompilerOptions(); var outputDir = options.declarationDir || options.outDir; // Prefer declaration folder if specified - if (options.declaration) { - var path = outputDir - ? getSourceFilePathInNewDir(sourceFile, host, outputDir) - : sourceFile.fileName; - return ts.removeFileExtension(path) + ".d.ts"; - } + var path = outputDir + ? getSourceFilePathInNewDir(sourceFile, host, outputDir) + : sourceFile.fileName; + return ts.removeFileExtension(path) + ".d.ts"; } ts.getDeclarationEmitOutputFilePath = getDeclarationEmitOutputFilePath; - function getEmitScriptTarget(compilerOptions) { - return compilerOptions.target || 0 /* ES3 */; - } - ts.getEmitScriptTarget = getEmitScriptTarget; - function getEmitModuleKind(compilerOptions) { - return typeof compilerOptions.module === "number" ? - compilerOptions.module : - getEmitScriptTarget(compilerOptions) === 2 /* ES6 */ ? ts.ModuleKind.ES6 : ts.ModuleKind.CommonJS; - } - ts.getEmitModuleKind = getEmitModuleKind; /** * Gets the source files that are expected to have an emit output. * @@ -8155,7 +8362,7 @@ var ts; function getSourceFilesToEmit(host, targetSourceFile) { var options = host.getCompilerOptions(); if (options.outFile || options.out) { - var moduleKind = getEmitModuleKind(options); + var moduleKind = ts.getEmitModuleKind(options); var moduleEmitEnabled = moduleKind === ts.ModuleKind.AMD || moduleKind === ts.ModuleKind.System; var sourceFiles = host.getSourceFiles(); // Can emit only sources that are not declaration file and are either non module code or module with --module or --target es6 specified @@ -8184,7 +8391,7 @@ var ts; * @param sourceFiles The transformed source files to emit. * @param action The action to execute. */ - function forEachTransformedEmitFile(host, sourceFiles, action) { + function forEachTransformedEmitFile(host, sourceFiles, action, emitOnlyDtsFiles) { var options = host.getCompilerOptions(); // Emit on each source file if (options.outFile || options.out) { @@ -8217,7 +8424,7 @@ var ts; } var jsFilePath = getOwnEmitOutputFilePath(sourceFile, host, extension); var sourceMapFilePath = getSourceMapFilePath(jsFilePath, options); - var declarationFilePath = !isSourceFileJavaScript(sourceFile) ? getDeclarationEmitOutputFilePath(sourceFile, host) : undefined; + var declarationFilePath = !isSourceFileJavaScript(sourceFile) && (options.declaration || emitOnlyDtsFiles) ? getDeclarationEmitOutputFilePath(sourceFile, host) : undefined; action(jsFilePath, sourceMapFilePath, declarationFilePath, [sourceFile], /*isBundledEmit*/ false); } function onBundledEmit(host, sourceFiles) { @@ -8242,7 +8449,7 @@ var ts; * @param action The action to execute. * @param targetSourceFile An optional target source file to emit. */ - function forEachExpectedEmitFile(host, action, targetSourceFile) { + function forEachExpectedEmitFile(host, action, targetSourceFile, emitOnlyDtsFiles) { var options = host.getCompilerOptions(); // Emit on each source file if (options.outFile || options.out) { @@ -8275,12 +8482,13 @@ var ts; } } var jsFilePath = getOwnEmitOutputFilePath(sourceFile, host, extension); + var declarationFilePath = !isSourceFileJavaScript(sourceFile) && (emitOnlyDtsFiles || options.declaration) ? getDeclarationEmitOutputFilePath(sourceFile, host) : undefined; var emitFileNames = { jsFilePath: jsFilePath, sourceMapFilePath: getSourceMapFilePath(jsFilePath, options), - declarationFilePath: !isSourceFileJavaScript(sourceFile) ? getDeclarationEmitOutputFilePath(sourceFile, host) : undefined + declarationFilePath: declarationFilePath }; - action(emitFileNames, [sourceFile], /*isBundledEmit*/ false); + action(emitFileNames, [sourceFile], /*isBundledEmit*/ false, emitOnlyDtsFiles); } function onBundledEmit(host) { // Can emit only sources that are not declaration file and are either non module code or module with @@ -8288,7 +8496,7 @@ var ts; var bundledSources = ts.filter(host.getSourceFiles(), function (sourceFile) { return !isDeclarationFile(sourceFile) && !host.isSourceFileFromExternalLibrary(sourceFile) && (!ts.isExternalModule(sourceFile) || - !!getEmitModuleKind(options)); }); + !!ts.getEmitModuleKind(options)); }); if (bundledSources.length) { var jsFilePath = options.outFile || options.out; var emitFileNames = { @@ -8296,7 +8504,7 @@ var ts; sourceMapFilePath: getSourceMapFilePath(jsFilePath, options), declarationFilePath: options.declaration ? ts.removeFileExtension(jsFilePath) + ".d.ts" : undefined }; - action(emitFileNames, bundledSources, /*isBundledEmit*/ true); + action(emitFileNames, bundledSources, /*isBundledEmit*/ true, emitOnlyDtsFiles); } } } @@ -8331,15 +8539,35 @@ var ts; }); } ts.getFirstConstructorWithBody = getFirstConstructorWithBody; + /** Get the type annotaion for the value parameter. */ function getSetAccessorTypeAnnotationNode(accessor) { if (accessor && accessor.parameters.length > 0) { - var hasThis = accessor.parameters.length === 2 && - accessor.parameters[0].name.kind === 69 /* Identifier */ && - accessor.parameters[0].name.originalKeywordKind === 97 /* ThisKeyword */; + var hasThis = accessor.parameters.length === 2 && parameterIsThisKeyword(accessor.parameters[0]); return accessor.parameters[hasThis ? 1 : 0].type; } } ts.getSetAccessorTypeAnnotationNode = getSetAccessorTypeAnnotationNode; + function getThisParameter(signature) { + if (signature.parameters.length) { + var thisParameter = signature.parameters[0]; + if (parameterIsThisKeyword(thisParameter)) { + return thisParameter; + } + } + } + ts.getThisParameter = getThisParameter; + function parameterIsThisKeyword(parameter) { + return isThisIdentifier(parameter.name); + } + ts.parameterIsThisKeyword = parameterIsThisKeyword; + function isThisIdentifier(node) { + return node && node.kind === 69 /* Identifier */ && identifierIsThisKeyword(node); + } + ts.isThisIdentifier = isThisIdentifier; + function identifierIsThisKeyword(id) { + return id.originalKeywordKind === 97 /* ThisKeyword */; + } + ts.identifierIsThisKeyword = identifierIsThisKeyword; function getAllAccessorDeclarations(declarations, accessor) { var firstAccessor; var secondAccessor; @@ -8700,14 +8928,6 @@ var ts; return symbol && symbol.valueDeclaration && hasModifier(symbol.valueDeclaration, 512 /* Default */) ? symbol.valueDeclaration.localSymbol : undefined; } ts.getLocalSymbolForExportDefault = getLocalSymbolForExportDefault; - function hasJavaScriptFileExtension(fileName) { - return ts.forEach(ts.supportedJavascriptExtensions, function (extension) { return ts.fileExtensionIs(fileName, extension); }); - } - ts.hasJavaScriptFileExtension = hasJavaScriptFileExtension; - function hasTypeScriptFileExtension(fileName) { - return ts.forEach(ts.supportedTypeScriptExtensions, function (extension) { return ts.fileExtensionIs(fileName, extension); }); - } - ts.hasTypeScriptFileExtension = hasTypeScriptFileExtension; /** Return ".ts", ".d.ts", or ".tsx", if that is the extension. */ function tryExtractTypeScriptExtension(fileName) { return ts.find(ts.supportedTypescriptExtensionsForExtractExtension, function (extension) { return ts.fileExtensionIs(fileName, extension); }); @@ -8820,12 +9040,6 @@ var ts; return result; } ts.convertToBase64 = convertToBase64; - function convertToRelativePath(absoluteOrRelativePath, basePath, getCanonicalFileName) { - return !ts.isRootedDiskPath(absoluteOrRelativePath) - ? absoluteOrRelativePath - : ts.getRelativePathToDirectoryOrUrl(basePath, absoluteOrRelativePath, basePath, getCanonicalFileName, /* isAbsolutePathAnUrl */ false); - } - ts.convertToRelativePath = convertToRelativePath; var carriageReturnLineFeed = "\r\n"; var lineFeed = "\n"; function getNewLineCharacter(options) { @@ -8938,7 +9152,7 @@ var ts; * @param value The delta. */ function movePos(pos, value) { - return positionIsSynthesized(pos) ? -1 : pos + value; + return ts.positionIsSynthesized(pos) ? -1 : pos + value; } ts.movePos = movePos; /** @@ -9054,7 +9268,7 @@ var ts; } ts.positionsAreOnSameLine = positionsAreOnSameLine; function getStartPositionOfRange(range, sourceFile) { - return positionIsSynthesized(range.pos) ? -1 : ts.skipTrivia(sourceFile.text, range.pos); + return ts.positionIsSynthesized(range.pos) ? -1 : ts.skipTrivia(sourceFile.text, range.pos); } ts.getStartPositionOfRange = getStartPositionOfRange; function collectExternalModuleInfo(sourceFile, resolver) { @@ -9179,15 +9393,16 @@ var ts; return 11 /* FirstTemplateToken */ <= kind && kind <= 14 /* LastTemplateToken */; } ts.isTemplateLiteralKind = isTemplateLiteralKind; - function isTemplateLiteralFragmentKind(kind) { - return kind === 12 /* TemplateHead */ - || kind === 13 /* TemplateMiddle */ - || kind === 14 /* TemplateTail */; + function isTemplateHead(node) { + return node.kind === 12 /* TemplateHead */; } - function isTemplateLiteralFragment(node) { - return isTemplateLiteralFragmentKind(node.kind); + ts.isTemplateHead = isTemplateHead; + function isTemplateMiddleOrTemplateTail(node) { + var kind = node.kind; + return kind === 13 /* TemplateMiddle */ + || kind === 14 /* TemplateTail */; } - ts.isTemplateLiteralFragment = isTemplateLiteralFragment; + ts.isTemplateMiddleOrTemplateTail = isTemplateMiddleOrTemplateTail; // Identifiers function isIdentifier(node) { return node.kind === 69 /* Identifier */; @@ -9340,12 +9555,12 @@ var ts; return node.kind === 174 /* CallExpression */; } ts.isCallExpression = isCallExpression; - function isTemplate(node) { + function isTemplateLiteral(node) { var kind = node.kind; return kind === 189 /* TemplateExpression */ || kind === 11 /* NoSubstitutionTemplateLiteral */; } - ts.isTemplate = isTemplate; + ts.isTemplateLiteral = isTemplateLiteral; function isSpreadElementExpression(node) { return node.kind === 191 /* SpreadElementExpression */; } @@ -9688,7 +9903,6 @@ var ts; } ts.isWatchSet = isWatchSet; })(ts || (ts = {})); -var ts; (function (ts) { function getDefaultLibFileName(options) { return options.target === 2 /* ES6 */ ? "lib.es6.d.ts" : "lib.d.ts"; @@ -10029,7 +10243,7 @@ var ts; // the original node. We also need to exclude specific properties and only include own- // properties (to skip members already defined on the shared prototype). var clone = createNode(node.kind, /*location*/ undefined, node.flags); - clone.original = node; + setOriginalNode(clone, node); for (var key in node) { if (clone.hasOwnProperty(key) || !node.hasOwnProperty(key)) { continue; @@ -10064,7 +10278,7 @@ var ts; node.text = value; return node; } - else { + else if (value) { var node = createNode(9 /* StringLiteral */, location, /*flags*/ undefined); node.textSourceNode = value; node.text = value.text; @@ -10364,7 +10578,7 @@ var ts; function createPropertyAccess(expression, name, location, flags) { var node = createNode(172 /* PropertyAccessExpression */, location, flags); node.expression = parenthesizeForAccess(expression); - node.emitFlags = 1048576 /* NoIndentation */; + (node.emitNode || (node.emitNode = {})).flags |= 1048576 /* NoIndentation */; node.name = typeof name === "string" ? createIdentifier(name) : name; return node; } @@ -10373,7 +10587,7 @@ var ts; if (node.expression !== expression || node.name !== name) { var propertyAccess = createPropertyAccess(expression, name, /*location*/ node, node.flags); // Because we are updating existed propertyAccess we want to inherit its emitFlags instead of using default from createPropertyAccess - propertyAccess.emitFlags = node.emitFlags; + (propertyAccess.emitNode || (propertyAccess.emitNode = {})).flags = getEmitFlags(node); return updateNode(propertyAccess, node); } return node; @@ -10477,7 +10691,7 @@ var ts; node.typeParameters = typeParameters ? createNodeArray(typeParameters) : undefined; node.parameters = createNodeArray(parameters); node.type = type; - node.equalsGreaterThanToken = equalsGreaterThanToken || createNode(34 /* EqualsGreaterThanToken */); + node.equalsGreaterThanToken = equalsGreaterThanToken || createToken(34 /* EqualsGreaterThanToken */); node.body = parenthesizeConciseBody(body); return node; } @@ -10570,7 +10784,7 @@ var ts; } ts.updatePostfix = updatePostfix; function createBinary(left, operator, right, location) { - var operatorToken = typeof operator === "number" ? createSynthesizedNode(operator) : operator; + var operatorToken = typeof operator === "number" ? createToken(operator) : operator; var operatorKind = operatorToken.kind; var node = createNode(187 /* BinaryExpression */, location); node.left = parenthesizeBinaryOperand(operatorKind, left, /*isLeftSideOfBinary*/ true, /*leftOperand*/ undefined); @@ -11492,7 +11706,7 @@ var ts; } else { var expression = ts.isIdentifier(memberName) ? createPropertyAccess(target, memberName, location) : createElementAccess(target, memberName, location); - expression.emitFlags |= 2048 /* NoNestedSourceMaps */; + (expression.emitNode || (expression.emitNode = {})).flags |= 2048 /* NoNestedSourceMaps */; return expression; } } @@ -11500,7 +11714,7 @@ var ts; function createRestParameter(name) { return createParameterDeclaration( /*decorators*/ undefined, - /*modifiers*/ undefined, createSynthesizedNode(22 /* DotDotDotToken */), name, + /*modifiers*/ undefined, createToken(22 /* DotDotDotToken */), name, /*questionToken*/ undefined, /*type*/ undefined, /*initializer*/ undefined); @@ -11630,13 +11844,13 @@ var ts; } ts.createDecorateHelper = createDecorateHelper; function createAwaiterHelper(externalHelpersModuleName, hasLexicalArguments, promiseConstructor, body) { - var generatorFunc = createFunctionExpression(createNode(37 /* AsteriskToken */), + var generatorFunc = createFunctionExpression(createToken(37 /* AsteriskToken */), /*name*/ undefined, /*typeParameters*/ undefined, /*parameters*/ [], /*type*/ undefined, body); // Mark this node as originally an async function - generatorFunc.emitFlags |= 2097152 /* AsyncFunctionBody */; + (generatorFunc.emitNode || (generatorFunc.emitNode = {})).flags |= 2097152 /* AsyncFunctionBody */; return createCall(createHelperName(externalHelpersModuleName, "__awaiter"), /*typeArguments*/ undefined, [ createThis(), @@ -11953,7 +12167,7 @@ var ts; target.push(startOnNewLine(createStatement(createLiteral("use strict")))); foundUseStrict = true; } - if (statement.emitFlags & 8388608 /* CustomPrologue */) { + if (getEmitFlags(statement) & 8388608 /* CustomPrologue */) { target.push(visitor ? ts.visitNode(statement, visitor, ts.isStatement) : statement); } else { @@ -11965,6 +12179,34 @@ var ts; return statementOffset; } ts.addPrologueDirectives = addPrologueDirectives; + /** + * Ensures "use strict" directive is added + * + * @param node source file + */ + function ensureUseStrict(node) { + var foundUseStrict = false; + for (var _i = 0, _a = node.statements; _i < _a.length; _i++) { + var statement = _a[_i]; + if (ts.isPrologueDirective(statement)) { + if (isUseStrictPrologue(statement)) { + foundUseStrict = true; + break; + } + } + else { + break; + } + } + if (!foundUseStrict) { + var statements = []; + statements.push(startOnNewLine(createStatement(createLiteral("use strict")))); + // add "use strict" as the first statement + return updateSourceFileNode(node, statements.concat(node.statements)); + } + return node; + } + ts.ensureUseStrict = ensureUseStrict; /** * Wraps the operand to a BinaryExpression in parentheses if they are needed to preserve the intended * order of operations. @@ -12323,17 +12565,180 @@ var ts; function setOriginalNode(node, original) { node.original = original; if (original) { - var emitFlags = original.emitFlags, commentRange = original.commentRange, sourceMapRange = original.sourceMapRange; - if (emitFlags) - node.emitFlags = emitFlags; - if (commentRange) - node.commentRange = commentRange; - if (sourceMapRange) - node.sourceMapRange = sourceMapRange; + var emitNode = original.emitNode; + if (emitNode) + node.emitNode = mergeEmitNode(emitNode, node.emitNode); } return node; } ts.setOriginalNode = setOriginalNode; + function mergeEmitNode(sourceEmitNode, destEmitNode) { + var flags = sourceEmitNode.flags, commentRange = sourceEmitNode.commentRange, sourceMapRange = sourceEmitNode.sourceMapRange, tokenSourceMapRanges = sourceEmitNode.tokenSourceMapRanges; + if (!destEmitNode && (flags || commentRange || sourceMapRange || tokenSourceMapRanges)) + destEmitNode = {}; + if (flags) + destEmitNode.flags = flags; + if (commentRange) + destEmitNode.commentRange = commentRange; + if (sourceMapRange) + destEmitNode.sourceMapRange = sourceMapRange; + if (tokenSourceMapRanges) + destEmitNode.tokenSourceMapRanges = mergeTokenSourceMapRanges(tokenSourceMapRanges, destEmitNode.tokenSourceMapRanges); + return destEmitNode; + } + function mergeTokenSourceMapRanges(sourceRanges, destRanges) { + if (!destRanges) + destRanges = ts.createMap(); + ts.copyProperties(sourceRanges, destRanges); + return destRanges; + } + /** + * Clears any EmitNode entries from parse-tree nodes. + * @param sourceFile A source file. + */ + function disposeEmitNodes(sourceFile) { + // During transformation we may need to annotate a parse tree node with transient + // transformation properties. As parse tree nodes live longer than transformation + // nodes, we need to make sure we reclaim any memory allocated for custom ranges + // from these nodes to ensure we do not hold onto entire subtrees just for position + // information. We also need to reset these nodes to a pre-transformation state + // for incremental parsing scenarios so that we do not impact later emit. + sourceFile = ts.getSourceFileOfNode(ts.getParseTreeNode(sourceFile)); + var emitNode = sourceFile && sourceFile.emitNode; + var annotatedNodes = emitNode && emitNode.annotatedNodes; + if (annotatedNodes) { + for (var _i = 0, annotatedNodes_1 = annotatedNodes; _i < annotatedNodes_1.length; _i++) { + var node = annotatedNodes_1[_i]; + node.emitNode = undefined; + } + } + } + ts.disposeEmitNodes = disposeEmitNodes; + /** + * Associates a node with the current transformation, initializing + * various transient transformation properties. + * + * @param node The node. + */ + function getOrCreateEmitNode(node) { + if (!node.emitNode) { + if (ts.isParseTreeNode(node)) { + // To avoid holding onto transformation artifacts, we keep track of any + // parse tree node we are annotating. This allows us to clean them up after + // all transformations have completed. + if (node.kind === 256 /* SourceFile */) { + return node.emitNode = { annotatedNodes: [node] }; + } + var sourceFile = ts.getSourceFileOfNode(node); + getOrCreateEmitNode(sourceFile).annotatedNodes.push(node); + } + node.emitNode = {}; + } + return node.emitNode; + } + /** + * Gets flags that control emit behavior of a node. + * + * @param node The node. + */ + function getEmitFlags(node) { + var emitNode = node.emitNode; + return emitNode && emitNode.flags; + } + ts.getEmitFlags = getEmitFlags; + /** + * Sets flags that control emit behavior of a node. + * + * @param node The node. + * @param emitFlags The NodeEmitFlags for the node. + */ + function setEmitFlags(node, emitFlags) { + getOrCreateEmitNode(node).flags = emitFlags; + return node; + } + ts.setEmitFlags = setEmitFlags; + /** + * Sets a custom text range to use when emitting source maps. + * + * @param node The node. + * @param range The text range. + */ + function setSourceMapRange(node, range) { + getOrCreateEmitNode(node).sourceMapRange = range; + return node; + } + ts.setSourceMapRange = setSourceMapRange; + /** + * Sets the TextRange to use for source maps for a token of a node. + * + * @param node The node. + * @param token The token. + * @param range The text range. + */ + function setTokenSourceMapRange(node, token, range) { + var emitNode = getOrCreateEmitNode(node); + var tokenSourceMapRanges = emitNode.tokenSourceMapRanges || (emitNode.tokenSourceMapRanges = ts.createMap()); + tokenSourceMapRanges[token] = range; + return node; + } + ts.setTokenSourceMapRange = setTokenSourceMapRange; + /** + * Sets a custom text range to use when emitting comments. + */ + function setCommentRange(node, range) { + getOrCreateEmitNode(node).commentRange = range; + return node; + } + ts.setCommentRange = setCommentRange; + /** + * Gets a custom text range to use when emitting comments. + * + * @param node The node. + */ + function getCommentRange(node) { + var emitNode = node.emitNode; + return (emitNode && emitNode.commentRange) || node; + } + ts.getCommentRange = getCommentRange; + /** + * Gets a custom text range to use when emitting source maps. + * + * @param node The node. + */ + function getSourceMapRange(node) { + var emitNode = node.emitNode; + return (emitNode && emitNode.sourceMapRange) || node; + } + ts.getSourceMapRange = getSourceMapRange; + /** + * Gets the TextRange to use for source maps for a token of a node. + * + * @param node The node. + * @param token The token. + */ + function getTokenSourceMapRange(node, token) { + var emitNode = node.emitNode; + var tokenSourceMapRanges = emitNode && emitNode.tokenSourceMapRanges; + return tokenSourceMapRanges && tokenSourceMapRanges[token]; + } + ts.getTokenSourceMapRange = getTokenSourceMapRange; + /** + * Gets the constant value to emit for an expression. + */ + function getConstantValue(node) { + var emitNode = node.emitNode; + return emitNode && emitNode.constantValue; + } + ts.getConstantValue = getConstantValue; + /** + * Sets the constant value to emit for an expression. + */ + function setConstantValue(node, value) { + var emitNode = getOrCreateEmitNode(node); + emitNode.constantValue = value; + return node; + } + ts.setConstantValue = setConstantValue; function setTextRange(node, location) { if (location) { node.pos = location.pos; @@ -12376,13 +12781,13 @@ var ts; } ts.getLocalNameForExternalImport = getLocalNameForExternalImport; /** - * Get the name of a target module from an import/export declaration as should be written in the emitted output. - * The emitted output name can be different from the input if: - * 1. The module has a /// - * 2. --out or --outFile is used, making the name relative to the rootDir - * 3- The containing SourceFile has an entry in renamedDependencies for the import as requested by some module loaders (e.g. System). - * Otherwise, a new StringLiteral node representing the module name will be returned. - */ + * Get the name of a target module from an import/export declaration as should be written in the emitted output. + * The emitted output name can be different from the input if: + * 1. The module has a /// + * 2. --out or --outFile is used, making the name relative to the rootDir + * 3- The containing SourceFile has an entry in renamedDependencies for the import as requested by some module loaders (e.g. System). + * Otherwise, a new StringLiteral node representing the module name will be returned. + */ function getExternalModuleNameLiteral(importNode, sourceFile, host, resolver, compilerOptions) { var moduleName = ts.getExternalModuleName(importNode); if (moduleName.kind === 9 /* StringLiteral */) { @@ -13683,8 +14088,8 @@ var ts; // Tokens other than ')' and ']' (the latter for index signatures) are here for better error recovery return token() === 18 /* CloseParenToken */ || token() === 20 /* CloseBracketToken */ /*|| token === SyntaxKind.OpenBraceToken*/; case 18 /* TypeArguments */: - // Tokens other than '>' are here for better error recovery - return token() === 27 /* GreaterThanToken */ || token() === 17 /* OpenParenToken */; + // All other tokens should cause the type-argument to terminate except comma token + return token() !== 24 /* CommaToken */; case 20 /* HeritageClauses */: return token() === 15 /* OpenBraceToken */ || token() === 16 /* CloseBraceToken */; case 13 /* JsxAttributes */: @@ -14135,7 +14540,7 @@ var ts; } function parseTemplateExpression() { var template = createNode(189 /* TemplateExpression */); - template.head = parseTemplateLiteralFragment(); + template.head = parseTemplateHead(); ts.Debug.assert(template.head.kind === 12 /* TemplateHead */, "Template head has wrong token kind"); var templateSpans = createNodeArray(); do { @@ -14151,7 +14556,7 @@ var ts; var literal; if (token() === 16 /* CloseBraceToken */) { reScanTemplateToken(); - literal = parseTemplateLiteralFragment(); + literal = parseTemplateMiddleOrTemplateTail(); } else { literal = parseExpectedToken(14 /* TemplateTail */, /*reportAtCurrentPosition*/ false, ts.Diagnostics._0_expected, ts.tokenToString(16 /* CloseBraceToken */)); @@ -14162,8 +14567,15 @@ var ts; function parseLiteralNode(internName) { return parseLiteralLikeNode(token(), internName); } - function parseTemplateLiteralFragment() { - return parseLiteralLikeNode(token(), /*internName*/ false); + function parseTemplateHead() { + var fragment = parseLiteralLikeNode(token(), /*internName*/ false); + ts.Debug.assert(fragment.kind === 12 /* TemplateHead */, "Template head has wrong token kind"); + return fragment; + } + function parseTemplateMiddleOrTemplateTail() { + var fragment = parseLiteralLikeNode(token(), /*internName*/ false); + ts.Debug.assert(fragment.kind === 13 /* TemplateMiddle */ || fragment.kind === 14 /* TemplateTail */, "Template fragment has wrong token kind"); + return fragment; } function parseLiteralLikeNode(kind, internName) { var node = createNode(kind); @@ -17144,7 +17556,7 @@ var ts; parseExpected(56 /* EqualsToken */); node.type = parseType(); parseSemicolon(); - return finishNode(node); + return addJSDocComment(finishNode(node)); } // In an ambient declaration, the grammar only allows integer literals as initializers. // In a non-ambient declaration, the grammar allows uninitialized members only in a @@ -17702,6 +18114,7 @@ var ts; var parameter = createNode(142 /* Parameter */); parameter.type = parseJSDocType(); if (parseOptional(56 /* EqualsToken */)) { + // TODO(rbuckton): Can this be changed to SyntaxKind.QuestionToken? parameter.questionToken = createNode(56 /* EqualsToken */); } return finishNode(parameter); @@ -18895,6 +19308,7 @@ var ts; ContainerFlags[ContainerFlags["IsFunctionExpression"] = 16] = "IsFunctionExpression"; ContainerFlags[ContainerFlags["HasLocals"] = 32] = "HasLocals"; ContainerFlags[ContainerFlags["IsInterface"] = 64] = "IsInterface"; + ContainerFlags[ContainerFlags["IsObjectLiteralOrClassExpressionMethod"] = 128] = "IsObjectLiteralOrClassExpressionMethod"; })(ContainerFlags || (ContainerFlags = {})); var binder = createBinder(); function bindSourceFile(file, options) { @@ -18927,7 +19341,8 @@ var ts; var emitFlags; // If this file is an external module, then it is automatically in strict-mode according to // ES6. If it is not an external module, then we'll determine if it is in strict mode or - // not depending on if we see "use strict" in certain places (or if we hit a class/namespace). + // not depending on if we see "use strict" in certain places or if we hit a class/namespace + // or if compiler options contain alwaysStrict. var inStrictMode; var symbolCount = 0; var Symbol; @@ -18941,7 +19356,7 @@ var ts; file = f; options = opts; languageVersion = ts.getEmitScriptTarget(options); - inStrictMode = !!file.externalModuleIndicator; + inStrictMode = bindInStrictMode(file, opts); classifiableNames = ts.createMap(); symbolCount = 0; skipTransformFlagAggregation = ts.isDeclarationFile(file); @@ -18971,6 +19386,15 @@ var ts; subtreeTransformFlags = 0 /* None */; } return bindSourceFile; + function bindInStrictMode(file, opts) { + if (opts.alwaysStrict && !ts.isDeclarationFile(file)) { + // bind in strict mode source files with alwaysStrict option + return true; + } + else { + return !!file.externalModuleIndicator; + } + } function createSymbol(flags, name) { symbolCount++; return new Symbol(flags, name); @@ -19134,11 +19558,24 @@ var ts; var message_1 = symbol.flags & 2 /* BlockScopedVariable */ ? ts.Diagnostics.Cannot_redeclare_block_scoped_variable_0 : ts.Diagnostics.Duplicate_identifier_0; - ts.forEach(symbol.declarations, function (declaration) { - if (ts.hasModifier(declaration, 512 /* Default */)) { + if (symbol.declarations && symbol.declarations.length) { + // If the current node is a default export of some sort, then check if + // there are any other default exports that we need to error on. + // We'll know whether we have other default exports depending on if `symbol` already has a declaration list set. + if (isDefaultExport) { message_1 = ts.Diagnostics.A_module_cannot_have_multiple_default_exports; } - }); + else { + // This is to properly report an error in the case "export default { }" is after export default of class declaration or function declaration. + // Error on multiple export default in the following case: + // 1. multiple export default of class declaration or function declaration by checking NodeFlags.Default + // 2. multiple export default of export assignment. This one doesn't have NodeFlags.Default on (as export default doesn't considered as modifiers) + if (symbol.declarations && symbol.declarations.length && + (isDefaultExport || (node.kind === 235 /* ExportAssignment */ && !node.isExportEquals))) { + message_1 = ts.Diagnostics.A_module_cannot_have_multiple_default_exports; + } + } + } ts.forEach(symbol.declarations, function (declaration) { file.bindDiagnostics.push(ts.createDiagnosticForNode(declaration.name || declaration, message_1, getDisplayName(declaration))); }); @@ -19243,7 +19680,7 @@ var ts; } else { currentFlow = { flags: 2 /* Start */ }; - if (containerFlags & 16 /* IsFunctionExpression */) { + if (containerFlags & (16 /* IsFunctionExpression */ | 128 /* IsObjectLiteralOrClassExpressionMethod */)) { currentFlow.container = node; } currentReturnTarget = undefined; @@ -19705,7 +20142,11 @@ var ts; currentFlow = preTryFlow; bind(node.finallyBlock); } - currentFlow = finishFlowLabel(postFinallyLabel); + // if try statement has finally block and flow after finally block is unreachable - keep it + // otherwise use whatever flow was accumulated at postFinallyLabel + if (!node.finallyBlock || !(currentFlow.flags & 1 /* Unreachable */)) { + currentFlow = finishFlowLabel(postFinallyLabel); + } } function bindSwitchStatement(node) { var postSwitchLabel = createBranchLabel(); @@ -19840,7 +20281,7 @@ var ts; } else { ts.forEachChild(node, bind); - if (node.operator === 57 /* PlusEqualsToken */ || node.operator === 42 /* MinusMinusToken */) { + if (node.operator === 41 /* PlusPlusToken */ || node.operator === 42 /* MinusMinusToken */) { bindAssignmentTargetFlow(node.operand); } } @@ -19942,9 +20383,12 @@ var ts; return 1 /* IsContainer */ | 32 /* HasLocals */; case 256 /* SourceFile */: return 1 /* IsContainer */ | 4 /* IsControlFlowContainer */ | 32 /* HasLocals */; + case 147 /* MethodDeclaration */: + if (ts.isObjectLiteralOrClassExpressionMethod(node)) { + return 1 /* IsContainer */ | 4 /* IsControlFlowContainer */ | 32 /* HasLocals */ | 8 /* IsFunctionLike */ | 128 /* IsObjectLiteralOrClassExpressionMethod */; + } case 148 /* Constructor */: case 220 /* FunctionDeclaration */: - case 147 /* MethodDeclaration */: case 146 /* MethodSignature */: case 149 /* GetAccessor */: case 150 /* SetAccessor */: @@ -20588,10 +21032,13 @@ var ts; bindAnonymousDeclaration(node, 8388608 /* Alias */, getDeclarationName(node)); } else { + // An export default clause with an expression exports a value + // We want to exclude both class and function here, this is necessary to issue an error when there are both + // default export-assignment and default export function and class declaration. var flags = node.kind === 235 /* ExportAssignment */ && ts.exportAssignmentIsAlias(node) ? 8388608 /* Alias */ : 4 /* Property */; - declareSymbol(container.symbol.exports, container.symbol, node, flags, 0 /* PropertyExcludes */ | 8388608 /* AliasExcludes */); + declareSymbol(container.symbol.exports, container.symbol, node, flags, 4 /* Property */ | 8388608 /* AliasExcludes */ | 32 /* Class */ | 16 /* Function */); } } function bindNamespaceExportDeclaration(node) { @@ -20829,6 +21276,9 @@ var ts; emitFlags |= 2048 /* HasDecorators */; } } + if (currentFlow && ts.isObjectLiteralOrClassExpressionMethod(node)) { + node.flowNode = currentFlow; + } return ts.hasDynamicName(node) ? bindAnonymousDeclaration(node, symbolFlags, "__computed") : declareSymbolAndAddToSymbolTable(node, symbolFlags, symbolExcludes); @@ -20994,8 +21444,7 @@ var ts; transformFlags |= 3 /* AssertTypeScript */; } // If the parameter's name is 'this', then it is TypeScript syntax. - if (subtreeFlags & 2048 /* ContainsDecorators */ - || (name && ts.isIdentifier(name) && name.originalKeywordKind === 97 /* ThisKeyword */)) { + if (subtreeFlags & 2048 /* ContainsDecorators */ || ts.isThisIdentifier(name)) { transformFlags |= 3 /* AssertTypeScript */; } // If a parameter has an accessibility modifier, then it is TypeScript syntax. @@ -21128,7 +21577,7 @@ var ts; transformFlags |= 3 /* AssertTypeScript */; } // Currently, we only support generators that were originally async function bodies. - if (asteriskToken && node.emitFlags & 2097152 /* AsyncFunctionBody */) { + if (asteriskToken && ts.getEmitFlags(node) & 2097152 /* AsyncFunctionBody */) { transformFlags |= 1536 /* AssertGenerator */; } node.transformFlags = transformFlags | 536870912 /* HasComputedFlags */; @@ -21190,7 +21639,7 @@ var ts; // down-level generator. // Currently we do not support transforming any other generator fucntions // down level. - if (asteriskToken && node.emitFlags & 2097152 /* AsyncFunctionBody */) { + if (asteriskToken && ts.getEmitFlags(node) & 2097152 /* AsyncFunctionBody */) { transformFlags |= 1536 /* AssertGenerator */; } } @@ -21216,7 +21665,7 @@ var ts; // down-level generator. // Currently we do not support transforming any other generator fucntions // down level. - if (asteriskToken && node.emitFlags & 2097152 /* AsyncFunctionBody */) { + if (asteriskToken && ts.getEmitFlags(node) & 2097152 /* AsyncFunctionBody */) { transformFlags |= 1536 /* AssertGenerator */; } node.transformFlags = transformFlags | 536870912 /* HasComputedFlags */; @@ -21500,5424 +21949,5111 @@ var ts; return transformFlags & ~excludeFlags; } })(ts || (ts = {})); -/// -/* @internal */ +/// +/// var ts; (function (ts) { - var ambientModuleSymbolRegex = /^".+"$/; - var nextSymbolId = 1; - var nextNodeId = 1; - var nextMergeId = 1; - var nextFlowId = 1; - function getNodeId(node) { - if (!node.id) { - node.id = nextNodeId; - nextNodeId++; + function trace(host, message) { + host.trace(ts.formatMessage.apply(undefined, arguments)); + } + ts.trace = trace; + /* @internal */ + function isTraceEnabled(compilerOptions, host) { + return compilerOptions.traceResolution && host.trace !== undefined; + } + ts.isTraceEnabled = isTraceEnabled; + /* @internal */ + function createResolvedModule(resolvedFileName, isExternalLibraryImport, failedLookupLocations) { + return { resolvedModule: resolvedFileName ? { resolvedFileName: resolvedFileName, isExternalLibraryImport: isExternalLibraryImport } : undefined, failedLookupLocations: failedLookupLocations }; + } + ts.createResolvedModule = createResolvedModule; + function moduleHasNonRelativeName(moduleName) { + return !(ts.isRootedDiskPath(moduleName) || ts.isExternalModuleNameRelative(moduleName)); + } + function tryReadTypesSection(packageJsonPath, baseDirectory, state) { + var jsonContent = readJson(packageJsonPath, state.host); + function tryReadFromField(fieldName) { + if (ts.hasProperty(jsonContent, fieldName)) { + var typesFile = jsonContent[fieldName]; + if (typeof typesFile === "string") { + var typesFilePath_1 = ts.normalizePath(ts.combinePaths(baseDirectory, typesFile)); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.package_json_has_0_field_1_that_references_2, fieldName, typesFile, typesFilePath_1); + } + return typesFilePath_1; + } + else { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Expected_type_of_0_field_in_package_json_to_be_string_got_1, fieldName, typeof typesFile); + } + } + } } - return node.id; + var typesFilePath = tryReadFromField("typings") || tryReadFromField("types"); + if (typesFilePath) { + return typesFilePath; + } + // Use the main module for inferring types if no types package specified and the allowJs is set + if (state.compilerOptions.allowJs && jsonContent.main && typeof jsonContent.main === "string") { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.No_types_specified_in_package_json_but_allowJs_is_set_so_returning_main_value_of_0, jsonContent.main); + } + var mainFilePath = ts.normalizePath(ts.combinePaths(baseDirectory, jsonContent.main)); + return mainFilePath; + } + return undefined; } - ts.getNodeId = getNodeId; - function getSymbolId(symbol) { - if (!symbol.id) { - symbol.id = nextSymbolId; - nextSymbolId++; + function readJson(path, host) { + try { + var jsonText = host.readFile(path); + return jsonText ? JSON.parse(jsonText) : {}; + } + catch (e) { + // gracefully handle if readFile fails or returns not JSON + return {}; } - return symbol.id; } - ts.getSymbolId = getSymbolId; - function createTypeChecker(host, produceDiagnostics) { - // Cancellation that controls whether or not we can cancel in the middle of type checking. - // In general cancelling is *not* safe for the type checker. We might be in the middle of - // computing something, and we will leave our internals in an inconsistent state. Callers - // who set the cancellation token should catch if a cancellation exception occurs, and - // should throw away and create a new TypeChecker. - // - // Currently we only support setting the cancellation token when getting diagnostics. This - // is because diagnostics can be quite expensive, and we want to allow hosts to bail out if - // they no longer need the information (for example, if the user started editing again). - var cancellationToken; - var Symbol = ts.objectAllocator.getSymbolConstructor(); - var Type = ts.objectAllocator.getTypeConstructor(); - var Signature = ts.objectAllocator.getSignatureConstructor(); - var typeCount = 0; - var symbolCount = 0; - var emptyArray = []; - var emptySymbols = ts.createMap(); - var compilerOptions = host.getCompilerOptions(); - var languageVersion = compilerOptions.target || 0 /* ES3 */; - var modulekind = ts.getEmitModuleKind(compilerOptions); - var noUnusedIdentifiers = !!compilerOptions.noUnusedLocals || !!compilerOptions.noUnusedParameters; - var allowSyntheticDefaultImports = typeof compilerOptions.allowSyntheticDefaultImports !== "undefined" ? compilerOptions.allowSyntheticDefaultImports : modulekind === ts.ModuleKind.System; - var strictNullChecks = compilerOptions.strictNullChecks; - var emitResolver = createResolver(); - var undefinedSymbol = createSymbol(4 /* Property */ | 67108864 /* Transient */, "undefined"); - undefinedSymbol.declarations = []; - var argumentsSymbol = createSymbol(4 /* Property */ | 67108864 /* Transient */, "arguments"); - var checker = { - getNodeCount: function () { return ts.sum(host.getSourceFiles(), "nodeCount"); }, - getIdentifierCount: function () { return ts.sum(host.getSourceFiles(), "identifierCount"); }, - getSymbolCount: function () { return ts.sum(host.getSourceFiles(), "symbolCount") + symbolCount; }, - getTypeCount: function () { return typeCount; }, - isUndefinedSymbol: function (symbol) { return symbol === undefinedSymbol; }, - isArgumentsSymbol: function (symbol) { return symbol === argumentsSymbol; }, - isUnknownSymbol: function (symbol) { return symbol === unknownSymbol; }, - getDiagnostics: getDiagnostics, - getGlobalDiagnostics: getGlobalDiagnostics, - getTypeOfSymbolAtLocation: getTypeOfSymbolAtLocation, - getSymbolsOfParameterPropertyDeclaration: getSymbolsOfParameterPropertyDeclaration, - getDeclaredTypeOfSymbol: getDeclaredTypeOfSymbol, - getPropertiesOfType: getPropertiesOfType, - getPropertyOfType: getPropertyOfType, - getSignaturesOfType: getSignaturesOfType, - getIndexTypeOfType: getIndexTypeOfType, - getBaseTypes: getBaseTypes, - getReturnTypeOfSignature: getReturnTypeOfSignature, - getNonNullableType: getNonNullableType, - getSymbolsInScope: getSymbolsInScope, - getSymbolAtLocation: getSymbolAtLocation, - getShorthandAssignmentValueSymbol: getShorthandAssignmentValueSymbol, - getExportSpecifierLocalTargetSymbol: getExportSpecifierLocalTargetSymbol, - getTypeAtLocation: getTypeOfNode, - getPropertySymbolOfDestructuringAssignment: getPropertySymbolOfDestructuringAssignment, - typeToString: typeToString, - getSymbolDisplayBuilder: getSymbolDisplayBuilder, - symbolToString: symbolToString, - getAugmentedPropertiesOfType: getAugmentedPropertiesOfType, - getRootSymbols: getRootSymbols, - getContextualType: getContextualType, - getFullyQualifiedName: getFullyQualifiedName, - getResolvedSignature: getResolvedSignature, - getConstantValue: getConstantValue, - isValidPropertyAccess: isValidPropertyAccess, - getSignatureFromDeclaration: getSignatureFromDeclaration, - isImplementationOfOverload: isImplementationOfOverload, - getAliasedSymbol: resolveAlias, - getEmitResolver: getEmitResolver, - getExportsOfModule: getExportsOfModuleAsArray, - getAmbientModules: getAmbientModules, - getJsxElementAttributesType: getJsxElementAttributesType, - getJsxIntrinsicTagNames: getJsxIntrinsicTagNames, - isOptionalParameter: isOptionalParameter - }; - var tupleTypes = []; - var unionTypes = ts.createMap(); - var intersectionTypes = ts.createMap(); - var stringLiteralTypes = ts.createMap(); - var numericLiteralTypes = ts.createMap(); - var unknownSymbol = createSymbol(4 /* Property */ | 67108864 /* Transient */, "unknown"); - var resolvingSymbol = createSymbol(67108864 /* Transient */, "__resolving__"); - var anyType = createIntrinsicType(1 /* Any */, "any"); - var unknownType = createIntrinsicType(1 /* Any */, "unknown"); - var undefinedType = createIntrinsicType(2048 /* Undefined */, "undefined"); - var undefinedWideningType = strictNullChecks ? undefinedType : createIntrinsicType(2048 /* Undefined */ | 33554432 /* ContainsWideningType */, "undefined"); - var nullType = createIntrinsicType(4096 /* Null */, "null"); - var nullWideningType = strictNullChecks ? nullType : createIntrinsicType(4096 /* Null */ | 33554432 /* ContainsWideningType */, "null"); - var stringType = createIntrinsicType(2 /* String */, "string"); - var numberType = createIntrinsicType(4 /* Number */, "number"); - var trueType = createIntrinsicType(128 /* BooleanLiteral */, "true"); - var falseType = createIntrinsicType(128 /* BooleanLiteral */, "false"); - var booleanType = createBooleanType([trueType, falseType]); - var esSymbolType = createIntrinsicType(512 /* ESSymbol */, "symbol"); - var voidType = createIntrinsicType(1024 /* Void */, "void"); - var neverType = createIntrinsicType(8192 /* Never */, "never"); - var silentNeverType = createIntrinsicType(8192 /* Never */, "never"); - var emptyObjectType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); - var emptyGenericType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); - emptyGenericType.instantiations = ts.createMap(); - var anyFunctionType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); - // The anyFunctionType contains the anyFunctionType by definition. The flag is further propagated - // in getPropagatingFlagsOfTypes, and it is checked in inferFromTypes. - anyFunctionType.flags |= 134217728 /* ContainsAnyFunctionType */; - var noConstraintType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); - var anySignature = createSignature(undefined, undefined, undefined, emptyArray, anyType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false); - var unknownSignature = createSignature(undefined, undefined, undefined, emptyArray, unknownType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false); - var resolvingSignature = createSignature(undefined, undefined, undefined, emptyArray, anyType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false); - var silentNeverSignature = createSignature(undefined, undefined, undefined, emptyArray, silentNeverType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false); - var enumNumberIndexInfo = createIndexInfo(stringType, /*isReadonly*/ true); - var globals = ts.createMap(); - /** - * List of every ambient module with a "*" wildcard. - * Unlike other ambient modules, these can't be stored in `globals` because symbol tables only deal with exact matches. - * This is only used if there is no exact match. - */ - var patternAmbientModules; - var getGlobalESSymbolConstructorSymbol; - var getGlobalPromiseConstructorSymbol; - var tryGetGlobalPromiseConstructorSymbol; - var globalObjectType; - var globalFunctionType; - var globalArrayType; - var globalReadonlyArrayType; - var globalStringType; - var globalNumberType; - var globalBooleanType; - var globalRegExpType; - var anyArrayType; - var anyReadonlyArrayType; - // The library files are only loaded when the feature is used. - // This allows users to just specify library files they want to used through --lib - // and they will not get an error from not having unrelated library files - var getGlobalTemplateStringsArrayType; - var getGlobalESSymbolType; - var getGlobalIterableType; - var getGlobalIteratorType; - var getGlobalIterableIteratorType; - var getGlobalClassDecoratorType; - var getGlobalParameterDecoratorType; - var getGlobalPropertyDecoratorType; - var getGlobalMethodDecoratorType; - var getGlobalTypedPropertyDescriptorType; - var getGlobalPromiseType; - var tryGetGlobalPromiseType; - var getGlobalPromiseLikeType; - var getInstantiatedGlobalPromiseLikeType; - var getGlobalPromiseConstructorLikeType; - var getGlobalThenableType; - var jsxElementClassType; - var deferredNodes; - var deferredUnusedIdentifierNodes; - var flowLoopStart = 0; - var flowLoopCount = 0; - var visitedFlowCount = 0; - var emptyStringType = getLiteralTypeForText(32 /* StringLiteral */, ""); - var zeroType = getLiteralTypeForText(64 /* NumberLiteral */, "0"); - var resolutionTargets = []; - var resolutionResults = []; - var resolutionPropertyNames = []; - var mergedSymbols = []; - var symbolLinks = []; - var nodeLinks = []; - var flowLoopCaches = []; - var flowLoopNodes = []; - var flowLoopKeys = []; - var flowLoopTypes = []; - var visitedFlowNodes = []; - var visitedFlowTypes = []; - var potentialThisCollisions = []; - var awaitedTypeStack = []; - var diagnostics = ts.createDiagnosticCollection(); - var TypeFacts; - (function (TypeFacts) { - TypeFacts[TypeFacts["None"] = 0] = "None"; - TypeFacts[TypeFacts["TypeofEQString"] = 1] = "TypeofEQString"; - TypeFacts[TypeFacts["TypeofEQNumber"] = 2] = "TypeofEQNumber"; - TypeFacts[TypeFacts["TypeofEQBoolean"] = 4] = "TypeofEQBoolean"; - TypeFacts[TypeFacts["TypeofEQSymbol"] = 8] = "TypeofEQSymbol"; - TypeFacts[TypeFacts["TypeofEQObject"] = 16] = "TypeofEQObject"; - TypeFacts[TypeFacts["TypeofEQFunction"] = 32] = "TypeofEQFunction"; - TypeFacts[TypeFacts["TypeofEQHostObject"] = 64] = "TypeofEQHostObject"; - TypeFacts[TypeFacts["TypeofNEString"] = 128] = "TypeofNEString"; - TypeFacts[TypeFacts["TypeofNENumber"] = 256] = "TypeofNENumber"; - TypeFacts[TypeFacts["TypeofNEBoolean"] = 512] = "TypeofNEBoolean"; - TypeFacts[TypeFacts["TypeofNESymbol"] = 1024] = "TypeofNESymbol"; - TypeFacts[TypeFacts["TypeofNEObject"] = 2048] = "TypeofNEObject"; - TypeFacts[TypeFacts["TypeofNEFunction"] = 4096] = "TypeofNEFunction"; - TypeFacts[TypeFacts["TypeofNEHostObject"] = 8192] = "TypeofNEHostObject"; - TypeFacts[TypeFacts["EQUndefined"] = 16384] = "EQUndefined"; - TypeFacts[TypeFacts["EQNull"] = 32768] = "EQNull"; - TypeFacts[TypeFacts["EQUndefinedOrNull"] = 65536] = "EQUndefinedOrNull"; - TypeFacts[TypeFacts["NEUndefined"] = 131072] = "NEUndefined"; - TypeFacts[TypeFacts["NENull"] = 262144] = "NENull"; - TypeFacts[TypeFacts["NEUndefinedOrNull"] = 524288] = "NEUndefinedOrNull"; - TypeFacts[TypeFacts["Truthy"] = 1048576] = "Truthy"; - TypeFacts[TypeFacts["Falsy"] = 2097152] = "Falsy"; - TypeFacts[TypeFacts["Discriminatable"] = 4194304] = "Discriminatable"; - TypeFacts[TypeFacts["All"] = 8388607] = "All"; - // The following members encode facts about particular kinds of types for use in the getTypeFacts function. - // The presence of a particular fact means that the given test is true for some (and possibly all) values - // of that kind of type. - TypeFacts[TypeFacts["BaseStringStrictFacts"] = 933633] = "BaseStringStrictFacts"; - TypeFacts[TypeFacts["BaseStringFacts"] = 3145473] = "BaseStringFacts"; - TypeFacts[TypeFacts["StringStrictFacts"] = 4079361] = "StringStrictFacts"; - TypeFacts[TypeFacts["StringFacts"] = 4194049] = "StringFacts"; - TypeFacts[TypeFacts["EmptyStringStrictFacts"] = 3030785] = "EmptyStringStrictFacts"; - TypeFacts[TypeFacts["EmptyStringFacts"] = 3145473] = "EmptyStringFacts"; - TypeFacts[TypeFacts["NonEmptyStringStrictFacts"] = 1982209] = "NonEmptyStringStrictFacts"; - TypeFacts[TypeFacts["NonEmptyStringFacts"] = 4194049] = "NonEmptyStringFacts"; - TypeFacts[TypeFacts["BaseNumberStrictFacts"] = 933506] = "BaseNumberStrictFacts"; - TypeFacts[TypeFacts["BaseNumberFacts"] = 3145346] = "BaseNumberFacts"; - TypeFacts[TypeFacts["NumberStrictFacts"] = 4079234] = "NumberStrictFacts"; - TypeFacts[TypeFacts["NumberFacts"] = 4193922] = "NumberFacts"; - TypeFacts[TypeFacts["ZeroStrictFacts"] = 3030658] = "ZeroStrictFacts"; - TypeFacts[TypeFacts["ZeroFacts"] = 3145346] = "ZeroFacts"; - TypeFacts[TypeFacts["NonZeroStrictFacts"] = 1982082] = "NonZeroStrictFacts"; - TypeFacts[TypeFacts["NonZeroFacts"] = 4193922] = "NonZeroFacts"; - TypeFacts[TypeFacts["BaseBooleanStrictFacts"] = 933252] = "BaseBooleanStrictFacts"; - TypeFacts[TypeFacts["BaseBooleanFacts"] = 3145092] = "BaseBooleanFacts"; - TypeFacts[TypeFacts["BooleanStrictFacts"] = 4078980] = "BooleanStrictFacts"; - TypeFacts[TypeFacts["BooleanFacts"] = 4193668] = "BooleanFacts"; - TypeFacts[TypeFacts["FalseStrictFacts"] = 3030404] = "FalseStrictFacts"; - TypeFacts[TypeFacts["FalseFacts"] = 3145092] = "FalseFacts"; - TypeFacts[TypeFacts["TrueStrictFacts"] = 1981828] = "TrueStrictFacts"; - TypeFacts[TypeFacts["TrueFacts"] = 4193668] = "TrueFacts"; - TypeFacts[TypeFacts["SymbolStrictFacts"] = 1981320] = "SymbolStrictFacts"; - TypeFacts[TypeFacts["SymbolFacts"] = 4193160] = "SymbolFacts"; - TypeFacts[TypeFacts["ObjectStrictFacts"] = 6166480] = "ObjectStrictFacts"; - TypeFacts[TypeFacts["ObjectFacts"] = 8378320] = "ObjectFacts"; - TypeFacts[TypeFacts["FunctionStrictFacts"] = 6164448] = "FunctionStrictFacts"; - TypeFacts[TypeFacts["FunctionFacts"] = 8376288] = "FunctionFacts"; - TypeFacts[TypeFacts["UndefinedFacts"] = 2457472] = "UndefinedFacts"; - TypeFacts[TypeFacts["NullFacts"] = 2340752] = "NullFacts"; - })(TypeFacts || (TypeFacts = {})); - var typeofEQFacts = ts.createMap({ - "string": 1 /* TypeofEQString */, - "number": 2 /* TypeofEQNumber */, - "boolean": 4 /* TypeofEQBoolean */, - "symbol": 8 /* TypeofEQSymbol */, - "undefined": 16384 /* EQUndefined */, - "object": 16 /* TypeofEQObject */, - "function": 32 /* TypeofEQFunction */ - }); - var typeofNEFacts = ts.createMap({ - "string": 128 /* TypeofNEString */, - "number": 256 /* TypeofNENumber */, - "boolean": 512 /* TypeofNEBoolean */, - "symbol": 1024 /* TypeofNESymbol */, - "undefined": 131072 /* NEUndefined */, - "object": 2048 /* TypeofNEObject */, - "function": 4096 /* TypeofNEFunction */ - }); - var typeofTypesByName = ts.createMap({ - "string": stringType, - "number": numberType, - "boolean": booleanType, - "symbol": esSymbolType, - "undefined": undefinedType - }); - var jsxElementType; - /** Things we lazy load from the JSX namespace */ - var jsxTypes = ts.createMap(); - var JsxNames = { - JSX: "JSX", - IntrinsicElements: "IntrinsicElements", - ElementClass: "ElementClass", - ElementAttributesPropertyNameContainer: "ElementAttributesProperty", - Element: "Element", - IntrinsicAttributes: "IntrinsicAttributes", - IntrinsicClassAttributes: "IntrinsicClassAttributes" - }; - var subtypeRelation = ts.createMap(); - var assignableRelation = ts.createMap(); - var comparableRelation = ts.createMap(); - var identityRelation = ts.createMap(); - var enumRelation = ts.createMap(); - // This is for caching the result of getSymbolDisplayBuilder. Do not access directly. - var _displayBuilder; - var TypeSystemPropertyName; - (function (TypeSystemPropertyName) { - TypeSystemPropertyName[TypeSystemPropertyName["Type"] = 0] = "Type"; - TypeSystemPropertyName[TypeSystemPropertyName["ResolvedBaseConstructorType"] = 1] = "ResolvedBaseConstructorType"; - TypeSystemPropertyName[TypeSystemPropertyName["DeclaredType"] = 2] = "DeclaredType"; - TypeSystemPropertyName[TypeSystemPropertyName["ResolvedReturnType"] = 3] = "ResolvedReturnType"; - })(TypeSystemPropertyName || (TypeSystemPropertyName = {})); - var builtinGlobals = ts.createMap(); - builtinGlobals[undefinedSymbol.name] = undefinedSymbol; - initializeTypeChecker(); - return checker; - function getEmitResolver(sourceFile, cancellationToken) { - // Ensure we have all the type information in place for this file so that all the - // emitter questions of this resolver will return the right information. - getDiagnostics(sourceFile, cancellationToken); - return emitResolver; + var typeReferenceExtensions = [".d.ts"]; + function getEffectiveTypeRoots(options, host) { + if (options.typeRoots) { + return options.typeRoots; } - function error(location, message, arg0, arg1, arg2) { - var diagnostic = location - ? ts.createDiagnosticForNode(location, message, arg0, arg1, arg2) - : ts.createCompilerDiagnostic(message, arg0, arg1, arg2); - diagnostics.add(diagnostic); + var currentDirectory; + if (options.configFilePath) { + currentDirectory = ts.getDirectoryPath(options.configFilePath); } - function createSymbol(flags, name) { - symbolCount++; - return new Symbol(flags, name); + else if (host.getCurrentDirectory) { + currentDirectory = host.getCurrentDirectory(); } - function getExcludedSymbolFlags(flags) { - var result = 0; - if (flags & 2 /* BlockScopedVariable */) - result |= 107455 /* BlockScopedVariableExcludes */; - if (flags & 1 /* FunctionScopedVariable */) - result |= 107454 /* FunctionScopedVariableExcludes */; - if (flags & 4 /* Property */) - result |= 0 /* PropertyExcludes */; - if (flags & 8 /* EnumMember */) - result |= 900095 /* EnumMemberExcludes */; - if (flags & 16 /* Function */) - result |= 106927 /* FunctionExcludes */; - if (flags & 32 /* Class */) - result |= 899519 /* ClassExcludes */; - if (flags & 64 /* Interface */) - result |= 792968 /* InterfaceExcludes */; - if (flags & 256 /* RegularEnum */) - result |= 899327 /* RegularEnumExcludes */; - if (flags & 128 /* ConstEnum */) - result |= 899967 /* ConstEnumExcludes */; - if (flags & 512 /* ValueModule */) - result |= 106639 /* ValueModuleExcludes */; - if (flags & 8192 /* Method */) - result |= 99263 /* MethodExcludes */; - if (flags & 32768 /* GetAccessor */) - result |= 41919 /* GetAccessorExcludes */; - if (flags & 65536 /* SetAccessor */) - result |= 74687 /* SetAccessorExcludes */; - if (flags & 262144 /* TypeParameter */) - result |= 530920 /* TypeParameterExcludes */; - if (flags & 524288 /* TypeAlias */) - result |= 793064 /* TypeAliasExcludes */; - if (flags & 8388608 /* Alias */) - result |= 8388608 /* AliasExcludes */; - return result; + return currentDirectory !== undefined && getDefaultTypeRoots(currentDirectory, host); + } + ts.getEffectiveTypeRoots = getEffectiveTypeRoots; + /** + * Returns the path to every node_modules/@types directory from some ancestor directory. + * Returns undefined if there are none. + */ + function getDefaultTypeRoots(currentDirectory, host) { + if (!host.directoryExists) { + return [ts.combinePaths(currentDirectory, nodeModulesAtTypes)]; } - function recordMergedSymbol(target, source) { - if (!source.mergeId) { - source.mergeId = nextMergeId; - nextMergeId++; + var typeRoots; + while (true) { + var atTypes = ts.combinePaths(currentDirectory, nodeModulesAtTypes); + if (host.directoryExists(atTypes)) { + (typeRoots || (typeRoots = [])).push(atTypes); } - mergedSymbols[source.mergeId] = target; - } - function cloneSymbol(symbol) { - var result = createSymbol(symbol.flags | 33554432 /* Merged */, symbol.name); - result.declarations = symbol.declarations.slice(0); - result.parent = symbol.parent; - if (symbol.valueDeclaration) - result.valueDeclaration = symbol.valueDeclaration; - if (symbol.constEnumOnlyModule) - result.constEnumOnlyModule = true; - if (symbol.members) - result.members = ts.cloneMap(symbol.members); - if (symbol.exports) - result.exports = ts.cloneMap(symbol.exports); - recordMergedSymbol(result, symbol); - return result; + var parent_7 = ts.getDirectoryPath(currentDirectory); + if (parent_7 === currentDirectory) { + break; + } + currentDirectory = parent_7; } - function mergeSymbol(target, source) { - if (!(target.flags & getExcludedSymbolFlags(source.flags))) { - if (source.flags & 512 /* ValueModule */ && target.flags & 512 /* ValueModule */ && target.constEnumOnlyModule && !source.constEnumOnlyModule) { - // reset flag when merging instantiated module into value module that has only const enums - target.constEnumOnlyModule = false; + return typeRoots; + } + var nodeModulesAtTypes = ts.combinePaths("node_modules", "@types"); + /** + * @param {string | undefined} containingFile - file that contains type reference directive, can be undefined if containing file is unknown. + * This is possible in case if resolution is performed for directives specified via 'types' parameter. In this case initial path for secondary lookups + * is assumed to be the same as root directory of the project. + */ + function resolveTypeReferenceDirective(typeReferenceDirectiveName, containingFile, options, host) { + var traceEnabled = isTraceEnabled(options, host); + var moduleResolutionState = { + compilerOptions: options, + host: host, + skipTsx: true, + traceEnabled: traceEnabled + }; + var typeRoots = getEffectiveTypeRoots(options, host); + if (traceEnabled) { + if (containingFile === undefined) { + if (typeRoots === undefined) { + trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_not_set_root_directory_not_set, typeReferenceDirectiveName); } - target.flags |= source.flags; - if (source.valueDeclaration && - (!target.valueDeclaration || - (target.valueDeclaration.kind === 225 /* ModuleDeclaration */ && source.valueDeclaration.kind !== 225 /* ModuleDeclaration */))) { - // other kinds of value declarations take precedence over modules - target.valueDeclaration = source.valueDeclaration; - } - ts.forEach(source.declarations, function (node) { - target.declarations.push(node); - }); - if (source.members) { - if (!target.members) - target.members = ts.createMap(); - mergeSymbolTable(target.members, source.members); - } - if (source.exports) { - if (!target.exports) - target.exports = ts.createMap(); - mergeSymbolTable(target.exports, source.exports); + else { + trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_not_set_root_directory_1, typeReferenceDirectiveName, typeRoots); } - recordMergedSymbol(target, source); } else { - var message_2 = target.flags & 2 /* BlockScopedVariable */ || source.flags & 2 /* BlockScopedVariable */ - ? ts.Diagnostics.Cannot_redeclare_block_scoped_variable_0 : ts.Diagnostics.Duplicate_identifier_0; - ts.forEach(source.declarations, function (node) { - error(node.name ? node.name : node, message_2, symbolToString(source)); - }); - ts.forEach(target.declarations, function (node) { - error(node.name ? node.name : node, message_2, symbolToString(source)); - }); - } - } - function mergeSymbolTable(target, source) { - for (var id in source) { - var targetSymbol = target[id]; - if (!targetSymbol) { - target[id] = source[id]; + if (typeRoots === undefined) { + trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_1_root_directory_not_set, typeReferenceDirectiveName, containingFile); } else { - if (!(targetSymbol.flags & 33554432 /* Merged */)) { - target[id] = targetSymbol = cloneSymbol(targetSymbol); - } - mergeSymbol(targetSymbol, source[id]); + trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_1_root_directory_2, typeReferenceDirectiveName, containingFile, typeRoots); } } } - function mergeModuleAugmentation(moduleName) { - var moduleAugmentation = moduleName.parent; - if (moduleAugmentation.symbol.declarations[0] !== moduleAugmentation) { - // this is a combined symbol for multiple augmentations within the same file. - // its symbol already has accumulated information for all declarations - // so we need to add it just once - do the work only for first declaration - ts.Debug.assert(moduleAugmentation.symbol.declarations.length > 1); - return; - } - if (ts.isGlobalScopeAugmentation(moduleAugmentation)) { - mergeSymbolTable(globals, moduleAugmentation.symbol.exports); + var failedLookupLocations = []; + // Check primary library paths + if (typeRoots && typeRoots.length) { + if (traceEnabled) { + trace(host, ts.Diagnostics.Resolving_with_primary_search_path_0, typeRoots.join(", ")); } - else { - // find a module that about to be augmented - // do not validate names of augmentations that are defined in ambient context - var moduleNotFoundError = !ts.isInAmbientContext(moduleName.parent.parent) - ? ts.Diagnostics.Invalid_module_name_in_augmentation_module_0_cannot_be_found - : undefined; - var mainModule = resolveExternalModuleNameWorker(moduleName, moduleName, moduleNotFoundError); - if (!mainModule) { - return; - } - // obtain item referenced by 'export=' - mainModule = resolveExternalModuleSymbol(mainModule); - if (mainModule.flags & 1920 /* Namespace */) { - // if module symbol has already been merged - it is safe to use it. - // otherwise clone it - mainModule = mainModule.flags & 33554432 /* Merged */ ? mainModule : cloneSymbol(mainModule); - mergeSymbol(mainModule, moduleAugmentation.symbol); - } - else { - error(moduleName, ts.Diagnostics.Cannot_augment_module_0_because_it_resolves_to_a_non_module_entity, moduleName.text); + var primarySearchPaths = typeRoots; + for (var _i = 0, primarySearchPaths_1 = primarySearchPaths; _i < primarySearchPaths_1.length; _i++) { + var typeRoot = primarySearchPaths_1[_i]; + var candidate = ts.combinePaths(typeRoot, typeReferenceDirectiveName); + var candidateDirectory = ts.getDirectoryPath(candidate); + var resolvedFile_1 = loadNodeModuleFromDirectory(typeReferenceExtensions, candidate, failedLookupLocations, !directoryProbablyExists(candidateDirectory, host), moduleResolutionState); + if (resolvedFile_1) { + if (traceEnabled) { + trace(host, ts.Diagnostics.Type_reference_directive_0_was_successfully_resolved_to_1_primary_Colon_2, typeReferenceDirectiveName, resolvedFile_1, true); + } + return { + resolvedTypeReferenceDirective: { primary: true, resolvedFileName: resolvedFile_1 }, + failedLookupLocations: failedLookupLocations + }; } } } - function addToSymbolTable(target, source, message) { - for (var id in source) { - if (target[id]) { - // Error on redeclarations - ts.forEach(target[id].declarations, addDeclarationDiagnostic(id, message)); + else { + if (traceEnabled) { + trace(host, ts.Diagnostics.Root_directory_cannot_be_determined_skipping_primary_search_paths); + } + } + var resolvedFile; + var initialLocationForSecondaryLookup; + if (containingFile) { + initialLocationForSecondaryLookup = ts.getDirectoryPath(containingFile); + } + if (initialLocationForSecondaryLookup !== undefined) { + // check secondary locations + if (traceEnabled) { + trace(host, ts.Diagnostics.Looking_up_in_node_modules_folder_initial_location_0, initialLocationForSecondaryLookup); + } + resolvedFile = loadModuleFromNodeModules(typeReferenceDirectiveName, initialLocationForSecondaryLookup, failedLookupLocations, moduleResolutionState, /*checkOneLevel*/ false); + if (traceEnabled) { + if (resolvedFile) { + trace(host, ts.Diagnostics.Type_reference_directive_0_was_successfully_resolved_to_1_primary_Colon_2, typeReferenceDirectiveName, resolvedFile, false); } else { - target[id] = source[id]; + trace(host, ts.Diagnostics.Type_reference_directive_0_was_not_resolved, typeReferenceDirectiveName); } } - function addDeclarationDiagnostic(id, message) { - return function (declaration) { return diagnostics.add(ts.createDiagnosticForNode(declaration, message, id)); }; - } - } - function getSymbolLinks(symbol) { - if (symbol.flags & 67108864 /* Transient */) - return symbol; - var id = getSymbolId(symbol); - return symbolLinks[id] || (symbolLinks[id] = {}); } - function getNodeLinks(node) { - var nodeId = getNodeId(node); - return nodeLinks[nodeId] || (nodeLinks[nodeId] = { flags: 0 }); + else { + if (traceEnabled) { + trace(host, ts.Diagnostics.Containing_file_is_not_specified_and_root_directory_cannot_be_determined_skipping_lookup_in_node_modules_folder); + } } - function isGlobalSourceFile(node) { - return node.kind === 256 /* SourceFile */ && !ts.isExternalOrCommonJsModule(node); + return { + resolvedTypeReferenceDirective: resolvedFile + ? { primary: false, resolvedFileName: resolvedFile } + : undefined, + failedLookupLocations: failedLookupLocations + }; + } + ts.resolveTypeReferenceDirective = resolveTypeReferenceDirective; + /** + * Given a set of options, returns the set of type directive names + * that should be included for this program automatically. + * This list could either come from the config file, + * or from enumerating the types root + initial secondary types lookup location. + * More type directives might appear in the program later as a result of loading actual source files; + * this list is only the set of defaults that are implicitly included. + */ + function getAutomaticTypeDirectiveNames(options, host) { + // Use explicit type list from tsconfig.json + if (options.types) { + return options.types; } - function getSymbol(symbols, name, meaning) { - if (meaning) { - var symbol = symbols[name]; - if (symbol) { - ts.Debug.assert((symbol.flags & 16777216 /* Instantiated */) === 0, "Should never get an instantiated symbol here."); - if (symbol.flags & meaning) { - return symbol; - } - if (symbol.flags & 8388608 /* Alias */) { - var target = resolveAlias(symbol); - // Unknown symbol means an error occurred in alias resolution, treat it as positive answer to avoid cascading errors - if (target === unknownSymbol || target.flags & meaning) { - return symbol; + // Walk the primary type lookup locations + var result = []; + if (host.directoryExists && host.getDirectories) { + var typeRoots = getEffectiveTypeRoots(options, host); + if (typeRoots) { + for (var _i = 0, typeRoots_1 = typeRoots; _i < typeRoots_1.length; _i++) { + var root = typeRoots_1[_i]; + if (host.directoryExists(root)) { + for (var _a = 0, _b = host.getDirectories(root); _a < _b.length; _a++) { + var typeDirectivePath = _b[_a]; + var normalized = ts.normalizePath(typeDirectivePath); + var packageJsonPath = pathToPackageJson(ts.combinePaths(root, normalized)); + // tslint:disable-next-line:no-null-keyword + var isNotNeededPackage = host.fileExists(packageJsonPath) && readJson(packageJsonPath, host).typings === null; + if (!isNotNeededPackage) { + // Return just the type directive names + result.push(ts.getBaseFileName(normalized)); + } } } } } - // return undefined if we can't find a symbol. } - /** - * Get symbols that represent parameter-property-declaration as parameter and as property declaration - * @param parameter a parameterDeclaration node - * @param parameterName a name of the parameter to get the symbols for. - * @return a tuple of two symbols - */ - function getSymbolsOfParameterPropertyDeclaration(parameter, parameterName) { - var constructorDeclaration = parameter.parent; - var classDeclaration = parameter.parent.parent; - var parameterSymbol = getSymbol(constructorDeclaration.locals, parameterName, 107455 /* Value */); - var propertySymbol = getSymbol(classDeclaration.symbol.members, parameterName, 107455 /* Value */); - if (parameterSymbol && propertySymbol) { - return [parameterSymbol, propertySymbol]; + return result; + } + ts.getAutomaticTypeDirectiveNames = getAutomaticTypeDirectiveNames; + function resolveModuleName(moduleName, containingFile, compilerOptions, host) { + var traceEnabled = isTraceEnabled(compilerOptions, host); + if (traceEnabled) { + trace(host, ts.Diagnostics.Resolving_module_0_from_1, moduleName, containingFile); + } + var moduleResolution = compilerOptions.moduleResolution; + if (moduleResolution === undefined) { + moduleResolution = ts.getEmitModuleKind(compilerOptions) === ts.ModuleKind.CommonJS ? ts.ModuleResolutionKind.NodeJs : ts.ModuleResolutionKind.Classic; + if (traceEnabled) { + trace(host, ts.Diagnostics.Module_resolution_kind_is_not_specified_using_0, ts.ModuleResolutionKind[moduleResolution]); } - ts.Debug.fail("There should exist two symbols, one as property declaration and one as parameter declaration"); } - function isBlockScopedNameDeclaredBeforeUse(declaration, usage) { - var declarationFile = ts.getSourceFileOfNode(declaration); - var useFile = ts.getSourceFileOfNode(usage); - if (declarationFile !== useFile) { - if ((modulekind && (declarationFile.externalModuleIndicator || useFile.externalModuleIndicator)) || - (!compilerOptions.outFile && !compilerOptions.out)) { - // nodes are in different files and order cannot be determines - return true; + else { + if (traceEnabled) { + trace(host, ts.Diagnostics.Explicitly_specified_module_resolution_kind_Colon_0, ts.ModuleResolutionKind[moduleResolution]); + } + } + var result; + switch (moduleResolution) { + case ts.ModuleResolutionKind.NodeJs: + result = nodeModuleNameResolver(moduleName, containingFile, compilerOptions, host); + break; + case ts.ModuleResolutionKind.Classic: + result = classicNameResolver(moduleName, containingFile, compilerOptions, host); + break; + } + if (traceEnabled) { + if (result.resolvedModule) { + trace(host, ts.Diagnostics.Module_name_0_was_successfully_resolved_to_1, moduleName, result.resolvedModule.resolvedFileName); + } + else { + trace(host, ts.Diagnostics.Module_name_0_was_not_resolved, moduleName); + } + } + return result; + } + ts.resolveModuleName = resolveModuleName; + /** + * Any module resolution kind can be augmented with optional settings: 'baseUrl', 'paths' and 'rootDirs' - they are used to + * mitigate differences between design time structure of the project and its runtime counterpart so the same import name + * can be resolved successfully by TypeScript compiler and runtime module loader. + * If these settings are set then loading procedure will try to use them to resolve module name and it can of failure it will + * fallback to standard resolution routine. + * + * - baseUrl - this setting controls how non-relative module names are resolved. If this setting is specified then non-relative + * names will be resolved relative to baseUrl: i.e. if baseUrl is '/a/b' then candidate location to resolve module name 'c/d' will + * be '/a/b/c/d' + * - paths - this setting can only be used when baseUrl is specified. allows to tune how non-relative module names + * will be resolved based on the content of the module name. + * Structure of 'paths' compiler options + * 'paths': { + * pattern-1: [...substitutions], + * pattern-2: [...substitutions], + * ... + * pattern-n: [...substitutions] + * } + * Pattern here is a string that can contain zero or one '*' character. During module resolution module name will be matched against + * all patterns in the list. Matching for patterns that don't contain '*' means that module name must be equal to pattern respecting the case. + * If pattern contains '*' then to match pattern "*" module name must start with the and end with . + * denotes part of the module name between and . + * If module name can be matches with multiple patterns then pattern with the longest prefix will be picked. + * After selecting pattern we'll use list of substitutions to get candidate locations of the module and the try to load module + * from the candidate location. + * Substitution is a string that can contain zero or one '*'. To get candidate location from substitution we'll pick every + * substitution in the list and replace '*' with string. If candidate location is not rooted it + * will be converted to absolute using baseUrl. + * For example: + * baseUrl: /a/b/c + * "paths": { + * // match all module names + * "*": [ + * "*", // use matched name as is, + * // will be looked as /a/b/c/ + * + * "folder1/*" // substitution will convert matched name to 'folder1/', + * // since it is not rooted then final candidate location will be /a/b/c/folder1/ + * ], + * // match module names that start with 'components/' + * "components/*": [ "/root/components/*" ] // substitution will convert /components/folder1/ to '/root/components/folder1/', + * // it is rooted so it will be final candidate location + * } + * + * 'rootDirs' allows the project to be spreaded across multiple locations and resolve modules with relative names as if + * they were in the same location. For example lets say there are two files + * '/local/src/content/file1.ts' + * '/shared/components/contracts/src/content/protocols/file2.ts' + * After bundling content of '/shared/components/contracts/src' will be merged with '/local/src' so + * if file1 has the following import 'import {x} from "./protocols/file2"' it will be resolved successfully in runtime. + * 'rootDirs' provides the way to tell compiler that in order to get the whole project it should behave as if content of all + * root dirs were merged together. + * I.e. for the example above 'rootDirs' will have two entries: [ '/local/src', '/shared/components/contracts/src' ]. + * Compiler will first convert './protocols/file2' into absolute path relative to the location of containing file: + * '/local/src/content/protocols/file2' and try to load it - failure. + * Then it will search 'rootDirs' looking for a longest matching prefix of this absolute path and if such prefix is found - absolute path will + * be converted to a path relative to found rootDir entry './content/protocols/file2' (*). As a last step compiler will check all remaining + * entries in 'rootDirs', use them to build absolute path out of (*) and try to resolve module from this location. + */ + function tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state) { + if (moduleHasNonRelativeName(moduleName)) { + return tryLoadModuleUsingBaseUrl(moduleName, loader, failedLookupLocations, supportedExtensions, state); + } + else { + return tryLoadModuleUsingRootDirs(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state); + } + } + function tryLoadModuleUsingRootDirs(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state) { + if (!state.compilerOptions.rootDirs) { + return undefined; + } + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.rootDirs_option_is_set_using_it_to_resolve_relative_module_name_0, moduleName); + } + var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); + var matchedRootDir; + var matchedNormalizedPrefix; + for (var _i = 0, _a = state.compilerOptions.rootDirs; _i < _a.length; _i++) { + var rootDir = _a[_i]; + // rootDirs are expected to be absolute + // in case of tsconfig.json this will happen automatically - compiler will expand relative names + // using location of tsconfig.json as base location + var normalizedRoot = ts.normalizePath(rootDir); + if (!ts.endsWith(normalizedRoot, ts.directorySeparator)) { + normalizedRoot += ts.directorySeparator; + } + var isLongestMatchingPrefix = ts.startsWith(candidate, normalizedRoot) && + (matchedNormalizedPrefix === undefined || matchedNormalizedPrefix.length < normalizedRoot.length); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Checking_if_0_is_the_longest_matching_prefix_for_1_2, normalizedRoot, candidate, isLongestMatchingPrefix); + } + if (isLongestMatchingPrefix) { + matchedNormalizedPrefix = normalizedRoot; + matchedRootDir = rootDir; + } + } + if (matchedNormalizedPrefix) { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Longest_matching_prefix_for_0_is_1, candidate, matchedNormalizedPrefix); + } + var suffix = candidate.substr(matchedNormalizedPrefix.length); + // first - try to load from a initial location + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Loading_0_from_the_root_dir_1_candidate_location_2, suffix, matchedNormalizedPrefix, candidate); + } + var resolvedFileName = loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(containingDirectory, state.host), state); + if (resolvedFileName) { + return resolvedFileName; + } + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Trying_other_entries_in_rootDirs); + } + // then try to resolve using remaining entries in rootDirs + for (var _b = 0, _c = state.compilerOptions.rootDirs; _b < _c.length; _b++) { + var rootDir = _c[_b]; + if (rootDir === matchedRootDir) { + // skip the initially matched entry + continue; + } + var candidate_1 = ts.combinePaths(ts.normalizePath(rootDir), suffix); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Loading_0_from_the_root_dir_1_candidate_location_2, suffix, rootDir, candidate_1); + } + var baseDirectory = ts.getDirectoryPath(candidate_1); + var resolvedFileName_1 = loader(candidate_1, supportedExtensions, failedLookupLocations, !directoryProbablyExists(baseDirectory, state.host), state); + if (resolvedFileName_1) { + return resolvedFileName_1; } - var sourceFiles = host.getSourceFiles(); - return ts.indexOf(sourceFiles, declarationFile) <= ts.indexOf(sourceFiles, useFile); } - if (declaration.pos <= usage.pos) { - // declaration is before usage - // still might be illegal if usage is in the initializer of the variable declaration - return declaration.kind !== 218 /* VariableDeclaration */ || - !isImmediatelyUsedInInitializerOfBlockScopedVariable(declaration, usage); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Module_resolution_using_rootDirs_has_failed); } - // declaration is after usage - // can be legal if usage is deferred (i.e. inside function or in initializer of instance property) - return isUsedInFunctionOrNonStaticProperty(declaration, usage); - function isImmediatelyUsedInInitializerOfBlockScopedVariable(declaration, usage) { - var container = ts.getEnclosingBlockScopeContainer(declaration); - switch (declaration.parent.parent.kind) { - case 200 /* VariableStatement */: - case 206 /* ForStatement */: - case 208 /* ForOfStatement */: - // variable statement/for/for-of statement case, - // use site should not be inside variable declaration (initializer of declaration or binding element) - if (isSameScopeDescendentOf(usage, declaration, container)) { - return true; - } - break; + } + return undefined; + } + function tryLoadModuleUsingBaseUrl(moduleName, loader, failedLookupLocations, supportedExtensions, state) { + if (!state.compilerOptions.baseUrl) { + return undefined; + } + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.baseUrl_option_is_set_to_0_using_this_value_to_resolve_non_relative_module_name_1, state.compilerOptions.baseUrl, moduleName); + } + // string is for exact match + var matchedPattern = undefined; + if (state.compilerOptions.paths) { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.paths_option_is_specified_looking_for_a_pattern_to_match_module_name_0, moduleName); + } + matchedPattern = ts.matchPatternOrExact(ts.getOwnKeys(state.compilerOptions.paths), moduleName); + } + if (matchedPattern) { + var matchedStar = typeof matchedPattern === "string" ? undefined : ts.matchedText(matchedPattern, moduleName); + var matchedPatternText = typeof matchedPattern === "string" ? matchedPattern : ts.patternText(matchedPattern); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Module_name_0_matched_pattern_1, moduleName, matchedPatternText); + } + for (var _i = 0, _a = state.compilerOptions.paths[matchedPatternText]; _i < _a.length; _i++) { + var subst = _a[_i]; + var path = matchedStar ? subst.replace("*", matchedStar) : subst; + var candidate = ts.normalizePath(ts.combinePaths(state.compilerOptions.baseUrl, path)); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Trying_substitution_0_candidate_module_location_Colon_1, subst, path); } - switch (declaration.parent.parent.kind) { - case 207 /* ForInStatement */: - case 208 /* ForOfStatement */: - // ForIn/ForOf case - use site should not be used in expression part - if (isSameScopeDescendentOf(usage, declaration.parent.parent.expression, container)) { - return true; - } + var resolvedFileName = loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(ts.getDirectoryPath(candidate), state.host), state); + if (resolvedFileName) { + return resolvedFileName; } - return false; } - function isUsedInFunctionOrNonStaticProperty(declaration, usage) { - var container = ts.getEnclosingBlockScopeContainer(declaration); - var current = usage; - while (current) { - if (current === container) { - return false; - } - if (ts.isFunctionLike(current)) { - return true; - } - var initializerOfNonStaticProperty = current.parent && - current.parent.kind === 145 /* PropertyDeclaration */ && - (ts.getModifierFlags(current.parent) & 32 /* Static */) === 0 && - current.parent.initializer === current; - if (initializerOfNonStaticProperty) { - return true; - } - current = current.parent; + return undefined; + } + else { + var candidate = ts.normalizePath(ts.combinePaths(state.compilerOptions.baseUrl, moduleName)); + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Resolving_module_name_0_relative_to_base_url_1_2, moduleName, state.compilerOptions.baseUrl, candidate); + } + return loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(ts.getDirectoryPath(candidate), state.host), state); + } + } + function nodeModuleNameResolver(moduleName, containingFile, compilerOptions, host) { + var containingDirectory = ts.getDirectoryPath(containingFile); + var supportedExtensions = ts.getSupportedExtensions(compilerOptions); + var traceEnabled = isTraceEnabled(compilerOptions, host); + var failedLookupLocations = []; + var state = { compilerOptions: compilerOptions, host: host, traceEnabled: traceEnabled, skipTsx: false }; + var resolvedFileName = tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, nodeLoadModuleByRelativeName, failedLookupLocations, supportedExtensions, state); + var isExternalLibraryImport = false; + if (!resolvedFileName) { + if (moduleHasNonRelativeName(moduleName)) { + if (traceEnabled) { + trace(host, ts.Diagnostics.Loading_module_0_from_node_modules_folder, moduleName); } - return false; + resolvedFileName = loadModuleFromNodeModules(moduleName, containingDirectory, failedLookupLocations, state, /*checkOneLevel*/ false); + isExternalLibraryImport = resolvedFileName !== undefined; + } + else { + var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); + resolvedFileName = nodeLoadModuleByRelativeName(candidate, supportedExtensions, failedLookupLocations, /*onlyRecordFailures*/ false, state); } } - // Resolve a given name for a given meaning at a given location. An error is reported if the name was not found and - // the nameNotFoundMessage argument is not undefined. Returns the resolved symbol, or undefined if no symbol with - // the given name can be found. - function resolveName(location, name, meaning, nameNotFoundMessage, nameArg) { - var result; - var lastLocation; - var propertyWithInvalidInitializer; - var errorLocation = location; - var grandparent; - var isInExternalModule = false; - loop: while (location) { - // Locals of a source file are not in scope (because they get merged into the global symbol table) - if (location.locals && !isGlobalSourceFile(location)) { - if (result = getSymbol(location.locals, name, meaning)) { - var useResult = true; - if (ts.isFunctionLike(location) && lastLocation && lastLocation !== location.body) { - // symbol lookup restrictions for function-like declarations - // - Type parameters of a function are in scope in the entire function declaration, including the parameter - // list and return type. However, local types are only in scope in the function body. - // - parameters are only in the scope of function body - // This restriction does not apply to JSDoc comment types because they are parented - // at a higher level than type parameters would normally be - if (meaning & result.flags & 793064 /* Type */ && lastLocation.kind !== 273 /* JSDocComment */) { - useResult = result.flags & 262144 /* TypeParameter */ - ? lastLocation === location.type || - lastLocation.kind === 142 /* Parameter */ || - lastLocation.kind === 141 /* TypeParameter */ - : false; - } - if (meaning & 107455 /* Value */ && result.flags & 1 /* FunctionScopedVariable */) { - // parameters are visible only inside function body, parameter list and return type - // technically for parameter list case here we might mix parameters and variables declared in function, - // however it is detected separately when checking initializers of parameters - // to make sure that they reference no variables declared after them. - useResult = - lastLocation.kind === 142 /* Parameter */ || - (lastLocation === location.type && - result.valueDeclaration.kind === 142 /* Parameter */); - } - } - if (useResult) { - break loop; - } - else { - result = undefined; - } - } - } - switch (location.kind) { - case 256 /* SourceFile */: - if (!ts.isExternalOrCommonJsModule(location)) - break; - isInExternalModule = true; - case 225 /* ModuleDeclaration */: - var moduleExports = getSymbolOfNode(location).exports; - if (location.kind === 256 /* SourceFile */ || ts.isAmbientModule(location)) { - // It's an external module. First see if the module has an export default and if the local - // name of that export default matches. - if (result = moduleExports["default"]) { - var localSymbol = ts.getLocalSymbolForExportDefault(result); - if (localSymbol && (result.flags & meaning) && localSymbol.name === name) { - break loop; - } - result = undefined; - } - // Because of module/namespace merging, a module's exports are in scope, - // yet we never want to treat an export specifier as putting a member in scope. - // Therefore, if the name we find is purely an export specifier, it is not actually considered in scope. - // Two things to note about this: - // 1. We have to check this without calling getSymbol. The problem with calling getSymbol - // on an export specifier is that it might find the export specifier itself, and try to - // resolve it as an alias. This will cause the checker to consider the export specifier - // a circular alias reference when it might not be. - // 2. We check === SymbolFlags.Alias in order to check that the symbol is *purely* - // an alias. If we used &, we'd be throwing out symbols that have non alias aspects, - // which is not the desired behavior. - if (moduleExports[name] && - moduleExports[name].flags === 8388608 /* Alias */ && - ts.getDeclarationOfKind(moduleExports[name], 238 /* ExportSpecifier */)) { - break; - } - } - if (result = getSymbol(moduleExports, name, meaning & 8914931 /* ModuleMember */)) { - break loop; - } - break; - case 224 /* EnumDeclaration */: - if (result = getSymbol(getSymbolOfNode(location).exports, name, meaning & 8 /* EnumMember */)) { - break loop; - } - break; - case 145 /* PropertyDeclaration */: - case 144 /* PropertySignature */: - // TypeScript 1.0 spec (April 2014): 8.4.1 - // Initializer expressions for instance member variables are evaluated in the scope - // of the class constructor body but are not permitted to reference parameters or - // local variables of the constructor. This effectively means that entities from outer scopes - // by the same name as a constructor parameter or local variable are inaccessible - // in initializer expressions for instance member variables. - if (ts.isClassLike(location.parent) && !(ts.getModifierFlags(location) & 32 /* Static */)) { - var ctor = findConstructorDeclaration(location.parent); - if (ctor && ctor.locals) { - if (getSymbol(ctor.locals, name, meaning & 107455 /* Value */)) { - // Remember the property node, it will be used later to report appropriate error - propertyWithInvalidInitializer = location; - } - } - } - break; - case 221 /* ClassDeclaration */: - case 192 /* ClassExpression */: - case 222 /* InterfaceDeclaration */: - if (result = getSymbol(getSymbolOfNode(location).members, name, meaning & 793064 /* Type */)) { - if (lastLocation && ts.getModifierFlags(lastLocation) & 32 /* Static */) { - // TypeScript 1.0 spec (April 2014): 3.4.1 - // The scope of a type parameter extends over the entire declaration with which the type - // parameter list is associated, with the exception of static member declarations in classes. - error(errorLocation, ts.Diagnostics.Static_members_cannot_reference_class_type_parameters); - return undefined; - } - break loop; - } - if (location.kind === 192 /* ClassExpression */ && meaning & 32 /* Class */) { - var className = location.name; - if (className && name === className.text) { - result = location.symbol; - break loop; - } - } - break; - // It is not legal to reference a class's own type parameters from a computed property name that - // belongs to the class. For example: - // - // function foo() { return '' } - // class C { // <-- Class's own type parameter T - // [foo()]() { } // <-- Reference to T from class's own computed property - // } - // - case 140 /* ComputedPropertyName */: - grandparent = location.parent.parent; - if (ts.isClassLike(grandparent) || grandparent.kind === 222 /* InterfaceDeclaration */) { - // A reference to this grandparent's type parameters would be an error - if (result = getSymbol(getSymbolOfNode(grandparent).members, name, meaning & 793064 /* Type */)) { - error(errorLocation, ts.Diagnostics.A_computed_property_name_cannot_reference_a_type_parameter_from_its_containing_type); - return undefined; - } - } - break; - case 147 /* MethodDeclaration */: - case 146 /* MethodSignature */: - case 148 /* Constructor */: - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - case 220 /* FunctionDeclaration */: - case 180 /* ArrowFunction */: - if (meaning & 3 /* Variable */ && name === "arguments") { - result = argumentsSymbol; - break loop; - } - break; - case 179 /* FunctionExpression */: - if (meaning & 3 /* Variable */ && name === "arguments") { - result = argumentsSymbol; - break loop; - } - if (meaning & 16 /* Function */) { - var functionName = location.name; - if (functionName && name === functionName.text) { - result = location.symbol; - break loop; - } - } - break; - case 143 /* Decorator */: - // Decorators are resolved at the class declaration. Resolving at the parameter - // or member would result in looking up locals in the method. - // - // function y() {} - // class C { - // method(@y x, y) {} // <-- decorator y should be resolved at the class declaration, not the parameter. - // } - // - if (location.parent && location.parent.kind === 142 /* Parameter */) { - location = location.parent; - } - // - // function y() {} - // class C { - // @y method(x, y) {} // <-- decorator y should be resolved at the class declaration, not the method. - // } - // - if (location.parent && ts.isClassElement(location.parent)) { - location = location.parent; - } - break; - } - lastLocation = location; - location = location.parent; - } - if (result && nameNotFoundMessage && noUnusedIdentifiers) { - result.isReferenced = true; - } - if (!result) { - result = getSymbol(globals, name, meaning); - } - if (!result) { - if (nameNotFoundMessage) { - if (!errorLocation || - !checkAndReportErrorForMissingPrefix(errorLocation, name, nameArg) && - !checkAndReportErrorForExtendingInterface(errorLocation) && - !checkAndReportErrorForUsingTypeAsValue(errorLocation, name, meaning)) { - error(errorLocation, nameNotFoundMessage, typeof nameArg === "string" ? nameArg : ts.declarationNameToString(nameArg)); - } - } - return undefined; - } - // Perform extra checks only if error reporting was requested - if (nameNotFoundMessage) { - if (propertyWithInvalidInitializer) { - // We have a match, but the reference occurred within a property initializer and the identifier also binds - // to a local variable in the constructor where the code will be emitted. - var propertyName = propertyWithInvalidInitializer.name; - error(errorLocation, ts.Diagnostics.Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor, ts.declarationNameToString(propertyName), typeof nameArg === "string" ? nameArg : ts.declarationNameToString(nameArg)); - return undefined; - } - // Only check for block-scoped variable if we are looking for the - // name with variable meaning - // For example, - // declare module foo { - // interface bar {} - // } - // const foo/*1*/: foo/*2*/.bar; - // The foo at /*1*/ and /*2*/ will share same symbol with two meaning - // block - scope variable and namespace module. However, only when we - // try to resolve name in /*1*/ which is used in variable position, - // we want to check for block- scoped - if (meaning & 2 /* BlockScopedVariable */) { - var exportOrLocalSymbol = getExportSymbolOfValueSymbolIfExported(result); - if (exportOrLocalSymbol.flags & 2 /* BlockScopedVariable */) { - checkResolvedBlockScopedVariable(exportOrLocalSymbol, errorLocation); - } - } - // If we're in an external module, we can't reference value symbols created from UMD export declarations - if (result && isInExternalModule && (meaning & 107455 /* Value */) === 107455 /* Value */) { - var decls = result.declarations; - if (decls && decls.length === 1 && decls[0].kind === 228 /* NamespaceExportDeclaration */) { - error(errorLocation, ts.Diagnostics.Identifier_0_must_be_imported_from_a_module, name); - } - } + if (resolvedFileName && host.realpath) { + var originalFileName = resolvedFileName; + resolvedFileName = ts.normalizePath(host.realpath(resolvedFileName)); + if (traceEnabled) { + trace(host, ts.Diagnostics.Resolving_real_path_for_0_result_1, originalFileName, resolvedFileName); } - return result; } - function checkAndReportErrorForMissingPrefix(errorLocation, name, nameArg) { - if ((errorLocation.kind === 69 /* Identifier */ && (isTypeReferenceIdentifier(errorLocation)) || isInTypeQuery(errorLocation))) { - return false; - } - var container = ts.getThisContainer(errorLocation, /* includeArrowFunctions */ true); - var location = container; - while (location) { - if (ts.isClassLike(location.parent)) { - var classSymbol = getSymbolOfNode(location.parent); - if (!classSymbol) { - break; - } - // Check to see if a static member exists. - var constructorType = getTypeOfSymbol(classSymbol); - if (getPropertyOfType(constructorType, name)) { - error(errorLocation, ts.Diagnostics.Cannot_find_name_0_Did_you_mean_the_static_member_1_0, typeof nameArg === "string" ? nameArg : ts.declarationNameToString(nameArg), symbolToString(classSymbol)); - return true; - } - // No static member is present. - // Check if we're in an instance method and look for a relevant instance member. - if (location === container && !(ts.getModifierFlags(location) & 32 /* Static */)) { - var instanceType = getDeclaredTypeOfSymbol(classSymbol).thisType; - if (getPropertyOfType(instanceType, name)) { - error(errorLocation, ts.Diagnostics.Cannot_find_name_0_Did_you_mean_the_instance_member_this_0, typeof nameArg === "string" ? nameArg : ts.declarationNameToString(nameArg)); - return true; - } - } - } - location = location.parent; - } - return false; + return createResolvedModule(resolvedFileName, isExternalLibraryImport, failedLookupLocations); + } + ts.nodeModuleNameResolver = nodeModuleNameResolver; + function nodeLoadModuleByRelativeName(candidate, supportedExtensions, failedLookupLocations, onlyRecordFailures, state) { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Loading_module_as_file_Slash_folder_candidate_module_location_0, candidate); } - function checkAndReportErrorForExtendingInterface(errorLocation) { - var expression = getEntityNameForExtendingInterface(errorLocation); - var isError = !!(expression && resolveEntityName(expression, 64 /* Interface */, /*ignoreErrors*/ true)); - if (isError) { - error(errorLocation, ts.Diagnostics.Cannot_extend_an_interface_0_Did_you_mean_implements, ts.getTextOfNode(expression)); + var resolvedFileName = !ts.pathEndsWithDirectorySeparator(candidate) && loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, onlyRecordFailures, state); + return resolvedFileName || loadNodeModuleFromDirectory(supportedExtensions, candidate, failedLookupLocations, onlyRecordFailures, state); + } + /* @internal */ + function directoryProbablyExists(directoryName, host) { + // if host does not support 'directoryExists' assume that directory will exist + return !host.directoryExists || host.directoryExists(directoryName); + } + ts.directoryProbablyExists = directoryProbablyExists; + /** + * @param {boolean} onlyRecordFailures - if true then function won't try to actually load files but instead record all attempts as failures. This flag is necessary + * in cases when we know upfront that all load attempts will fail (because containing folder does not exists) however we still need to record all failed lookup locations. + */ + function loadModuleFromFile(candidate, extensions, failedLookupLocation, onlyRecordFailures, state) { + // First, try adding an extension. An import of "foo" could be matched by a file "foo.ts", or "foo.js" by "foo.js.ts" + var resolvedByAddingExtension = tryAddingExtensions(candidate, extensions, failedLookupLocation, onlyRecordFailures, state); + if (resolvedByAddingExtension) { + return resolvedByAddingExtension; + } + // If that didn't work, try stripping a ".js" or ".jsx" extension and replacing it with a TypeScript one; + // e.g. "./foo.js" can be matched by "./foo.ts" or "./foo.d.ts" + if (ts.hasJavaScriptFileExtension(candidate)) { + var extensionless = ts.removeFileExtension(candidate); + if (state.traceEnabled) { + var extension = candidate.substring(extensionless.length); + trace(state.host, ts.Diagnostics.File_name_0_has_a_1_extension_stripping_it, candidate, extension); } - return isError; + return tryAddingExtensions(extensionless, extensions, failedLookupLocation, onlyRecordFailures, state); } - /** - * Climbs up parents to an ExpressionWithTypeArguments, and returns its expression, - * but returns undefined if that expression is not an EntityNameExpression. - */ - function getEntityNameForExtendingInterface(node) { - switch (node.kind) { - case 69 /* Identifier */: - case 172 /* PropertyAccessExpression */: - return node.parent ? getEntityNameForExtendingInterface(node.parent) : undefined; - case 194 /* ExpressionWithTypeArguments */: - ts.Debug.assert(ts.isEntityNameExpression(node.expression)); - return node.expression; - default: - return undefined; + } + /** Try to return an existing file that adds one of the `extensions` to `candidate`. */ + function tryAddingExtensions(candidate, extensions, failedLookupLocation, onlyRecordFailures, state) { + if (!onlyRecordFailures) { + // check if containing folder exists - if it doesn't then just record failures for all supported extensions without disk probing + var directory = ts.getDirectoryPath(candidate); + if (directory) { + onlyRecordFailures = !directoryProbablyExists(directory, state.host); } } - function checkAndReportErrorForUsingTypeAsValue(errorLocation, name, meaning) { - if (meaning & (107455 /* Value */ & ~1024 /* NamespaceModule */)) { - var symbol = resolveSymbol(resolveName(errorLocation, name, 793064 /* Type */ & ~107455 /* Value */, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined)); - if (symbol && !(symbol.flags & 1024 /* NamespaceModule */)) { - error(errorLocation, ts.Diagnostics._0_only_refers_to_a_type_but_is_being_used_as_a_value_here, name); - return true; - } + return ts.forEach(extensions, function (ext) { + return !(state.skipTsx && ts.isJsxOrTsxExtension(ext)) && tryFile(candidate + ext, failedLookupLocation, onlyRecordFailures, state); + }); + } + /** Return the file if it exists. */ + function tryFile(fileName, failedLookupLocation, onlyRecordFailures, state) { + if (!onlyRecordFailures && state.host.fileExists(fileName)) { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.File_0_exist_use_it_as_a_name_resolution_result, fileName); } - return false; + return fileName; } - function checkResolvedBlockScopedVariable(result, errorLocation) { - ts.Debug.assert((result.flags & 2 /* BlockScopedVariable */) !== 0); - // Block-scoped variables cannot be used before their definition - var declaration = ts.forEach(result.declarations, function (d) { return ts.isBlockOrCatchScoped(d) ? d : undefined; }); - ts.Debug.assert(declaration !== undefined, "Block-scoped variable declaration is undefined"); - if (!ts.isInAmbientContext(declaration) && !isBlockScopedNameDeclaredBeforeUse(ts.getAncestor(declaration, 218 /* VariableDeclaration */), errorLocation)) { - error(errorLocation, ts.Diagnostics.Block_scoped_variable_0_used_before_its_declaration, ts.declarationNameToString(declaration.name)); + else { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.File_0_does_not_exist, fileName); } + failedLookupLocation.push(fileName); + return undefined; } - /* Starting from 'initial' node walk up the parent chain until 'stopAt' node is reached. - * If at any point current node is equal to 'parent' node - return true. - * Return false if 'stopAt' node is reached or isFunctionLike(current) === true. - */ - function isSameScopeDescendentOf(initial, parent, stopAt) { - if (!parent) { - return false; + } + function loadNodeModuleFromDirectory(extensions, candidate, failedLookupLocation, onlyRecordFailures, state) { + var packageJsonPath = pathToPackageJson(candidate); + var directoryExists = !onlyRecordFailures && directoryProbablyExists(candidate, state.host); + if (directoryExists && state.host.fileExists(packageJsonPath)) { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.Found_package_json_at_0, packageJsonPath); } - for (var current = initial; current && current !== stopAt && !ts.isFunctionLike(current); current = current.parent) { - if (current === parent) { - return true; + var typesFile = tryReadTypesSection(packageJsonPath, candidate, state); + if (typesFile) { + var onlyRecordFailures_1 = !directoryProbablyExists(ts.getDirectoryPath(typesFile), state.host); + // A package.json "typings" may specify an exact filename, or may choose to omit an extension. + var result = tryFile(typesFile, failedLookupLocation, onlyRecordFailures_1, state) || + tryAddingExtensions(typesFile, extensions, failedLookupLocation, onlyRecordFailures_1, state); + if (result) { + return result; } } - return false; - } - function getAnyImportSyntax(node) { - if (ts.isAliasSymbolDeclaration(node)) { - if (node.kind === 229 /* ImportEqualsDeclaration */) { - return node; - } - while (node && node.kind !== 230 /* ImportDeclaration */) { - node = node.parent; + else { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.package_json_does_not_have_types_field); } - return node; } } - function getDeclarationOfAliasSymbol(symbol) { - return ts.forEach(symbol.declarations, function (d) { return ts.isAliasSymbolDeclaration(d) ? d : undefined; }); - } - function getTargetOfImportEqualsDeclaration(node) { - if (node.moduleReference.kind === 240 /* ExternalModuleReference */) { - return resolveExternalModuleSymbol(resolveExternalModuleName(node, ts.getExternalModuleImportEqualsDeclarationExpression(node))); + else { + if (state.traceEnabled) { + trace(state.host, ts.Diagnostics.File_0_does_not_exist, packageJsonPath); } - return getSymbolOfPartOfRightHandSideOfImportEquals(node.moduleReference, node); + // record package json as one of failed lookup locations - in the future if this file will appear it will invalidate resolution results + failedLookupLocation.push(packageJsonPath); } - function getTargetOfImportClause(node) { - var moduleSymbol = resolveExternalModuleName(node, node.parent.moduleSpecifier); - if (moduleSymbol) { - var exportDefaultSymbol = ts.isShorthandAmbientModuleSymbol(moduleSymbol) ? - moduleSymbol : - moduleSymbol.exports["export="] ? - getPropertyOfType(getTypeOfSymbol(moduleSymbol.exports["export="]), "default") : - resolveSymbol(moduleSymbol.exports["default"]); - if (!exportDefaultSymbol && !allowSyntheticDefaultImports) { - error(node.name, ts.Diagnostics.Module_0_has_no_default_export, symbolToString(moduleSymbol)); + return loadModuleFromFile(ts.combinePaths(candidate, "index"), extensions, failedLookupLocation, !directoryExists, state); + } + function pathToPackageJson(directory) { + return ts.combinePaths(directory, "package.json"); + } + function loadModuleFromNodeModulesFolder(moduleName, directory, failedLookupLocations, state) { + var nodeModulesFolder = ts.combinePaths(directory, "node_modules"); + var nodeModulesFolderExists = directoryProbablyExists(nodeModulesFolder, state.host); + var candidate = ts.normalizePath(ts.combinePaths(nodeModulesFolder, moduleName)); + var supportedExtensions = ts.getSupportedExtensions(state.compilerOptions); + var result = loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, !nodeModulesFolderExists, state); + if (result) { + return result; + } + result = loadNodeModuleFromDirectory(supportedExtensions, candidate, failedLookupLocations, !nodeModulesFolderExists, state); + if (result) { + return result; + } + } + /* @internal */ + function loadModuleFromNodeModules(moduleName, directory, failedLookupLocations, state, checkOneLevel) { + return loadModuleFromNodeModulesWorker(moduleName, directory, failedLookupLocations, state, checkOneLevel, /*typesOnly*/ false); + } + ts.loadModuleFromNodeModules = loadModuleFromNodeModules; + function loadModuleFromNodeModulesAtTypes(moduleName, directory, failedLookupLocations, state) { + return loadModuleFromNodeModulesWorker(moduleName, directory, failedLookupLocations, state, /*checkOneLevel*/ false, /*typesOnly*/ true); + } + function loadModuleFromNodeModulesWorker(moduleName, directory, failedLookupLocations, state, checkOneLevel, typesOnly) { + directory = ts.normalizeSlashes(directory); + while (true) { + var baseName = ts.getBaseFileName(directory); + if (baseName !== "node_modules") { + var packageResult = void 0; + if (!typesOnly) { + // Try to load source from the package + packageResult = loadModuleFromNodeModulesFolder(moduleName, directory, failedLookupLocations, state); + if (packageResult && ts.hasTypeScriptFileExtension(packageResult)) { + // Always prefer a TypeScript (.ts, .tsx, .d.ts) file shipped with the package + return packageResult; + } } - else if (!exportDefaultSymbol && allowSyntheticDefaultImports) { - return resolveExternalModuleSymbol(moduleSymbol) || resolveSymbol(moduleSymbol); + // Else prefer a types package over non-TypeScript results (e.g. JavaScript files) + var typesResult = loadModuleFromNodeModulesFolder(ts.combinePaths("@types", moduleName), directory, failedLookupLocations, state); + if (typesResult || packageResult) { + return typesResult || packageResult; } - return exportDefaultSymbol; } + var parentPath = ts.getDirectoryPath(directory); + if (parentPath === directory || checkOneLevel) { + break; + } + directory = parentPath; } - function getTargetOfNamespaceImport(node) { - var moduleSpecifier = node.parent.parent.moduleSpecifier; - return resolveESModuleSymbol(resolveExternalModuleName(node, moduleSpecifier), moduleSpecifier); + return undefined; + } + function classicNameResolver(moduleName, containingFile, compilerOptions, host) { + var traceEnabled = isTraceEnabled(compilerOptions, host); + var state = { compilerOptions: compilerOptions, host: host, traceEnabled: traceEnabled, skipTsx: !compilerOptions.jsx }; + var failedLookupLocations = []; + var supportedExtensions = ts.getSupportedExtensions(compilerOptions); + var containingDirectory = ts.getDirectoryPath(containingFile); + var resolvedFileName = tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, loadModuleFromFile, failedLookupLocations, supportedExtensions, state); + if (resolvedFileName) { + return createResolvedModule(resolvedFileName, /*isExternalLibraryImport*/ false, failedLookupLocations); } - // This function creates a synthetic symbol that combines the value side of one symbol with the - // type/namespace side of another symbol. Consider this example: - // - // declare module graphics { - // interface Point { - // x: number; - // y: number; - // } - // } - // declare var graphics: { - // Point: new (x: number, y: number) => graphics.Point; - // } - // declare module "graphics" { - // export = graphics; - // } - // - // An 'import { Point } from "graphics"' needs to create a symbol that combines the value side 'Point' - // property with the type/namespace side interface 'Point'. - function combineValueAndTypeSymbols(valueSymbol, typeSymbol) { - if (valueSymbol.flags & (793064 /* Type */ | 1920 /* Namespace */)) { - return valueSymbol; - } - var result = createSymbol(valueSymbol.flags | typeSymbol.flags, valueSymbol.name); - result.declarations = ts.concatenate(valueSymbol.declarations, typeSymbol.declarations); - result.parent = valueSymbol.parent || typeSymbol.parent; - if (valueSymbol.valueDeclaration) - result.valueDeclaration = valueSymbol.valueDeclaration; - if (typeSymbol.members) - result.members = typeSymbol.members; - if (valueSymbol.exports) - result.exports = valueSymbol.exports; - return result; + var referencedSourceFile; + if (moduleHasNonRelativeName(moduleName)) { + referencedSourceFile = referencedSourceFile = loadModuleFromAncestorDirectories(moduleName, containingDirectory, supportedExtensions, failedLookupLocations, state) || + // If we didn't find the file normally, look it up in @types. + loadModuleFromNodeModulesAtTypes(moduleName, containingDirectory, failedLookupLocations, state); } - function getExportOfModule(symbol, name) { - if (symbol.flags & 1536 /* Module */) { - var exportedSymbol = getExportsOfSymbol(symbol)[name]; - if (exportedSymbol) { - return resolveSymbol(exportedSymbol); - } - } + else { + var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); + referencedSourceFile = loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, /*onlyRecordFailures*/ false, state); } - function getPropertyOfVariable(symbol, name) { - if (symbol.flags & 3 /* Variable */) { - var typeAnnotation = symbol.valueDeclaration.type; - if (typeAnnotation) { - return resolveSymbol(getPropertyOfType(getTypeFromTypeNode(typeAnnotation), name)); - } + return referencedSourceFile + ? { resolvedModule: { resolvedFileName: referencedSourceFile }, failedLookupLocations: failedLookupLocations } + : { resolvedModule: undefined, failedLookupLocations: failedLookupLocations }; + } + ts.classicNameResolver = classicNameResolver; + /** Climb up parent directories looking for a module. */ + function loadModuleFromAncestorDirectories(moduleName, containingDirectory, supportedExtensions, failedLookupLocations, state) { + while (true) { + var searchName = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); + var referencedSourceFile = loadModuleFromFile(searchName, supportedExtensions, failedLookupLocations, /*onlyRecordFailures*/ false, state); + if (referencedSourceFile) { + return referencedSourceFile; + } + var parentPath = ts.getDirectoryPath(containingDirectory); + if (parentPath === containingDirectory) { + return undefined; } + containingDirectory = parentPath; } - function getExternalModuleMember(node, specifier) { - var moduleSymbol = resolveExternalModuleName(node, node.moduleSpecifier); - var targetSymbol = resolveESModuleSymbol(moduleSymbol, node.moduleSpecifier); - if (targetSymbol) { - var name_13 = specifier.propertyName || specifier.name; - if (name_13.text) { - if (ts.isShorthandAmbientModuleSymbol(moduleSymbol)) { - return moduleSymbol; - } - var symbolFromVariable = void 0; - // First check if module was specified with "export=". If so, get the member from the resolved type - if (moduleSymbol && moduleSymbol.exports && moduleSymbol.exports["export="]) { - symbolFromVariable = getPropertyOfType(getTypeOfSymbol(targetSymbol), name_13.text); - } - else { - symbolFromVariable = getPropertyOfVariable(targetSymbol, name_13.text); - } - // if symbolFromVariable is export - get its final target - symbolFromVariable = resolveSymbol(symbolFromVariable); - var symbolFromModule = getExportOfModule(targetSymbol, name_13.text); - // If the export member we're looking for is default, and there is no real default but allowSyntheticDefaultImports is on, return the entire module as the default - if (!symbolFromModule && allowSyntheticDefaultImports && name_13.text === "default") { - symbolFromModule = resolveExternalModuleSymbol(moduleSymbol) || resolveSymbol(moduleSymbol); - } - var symbol = symbolFromModule && symbolFromVariable ? - combineValueAndTypeSymbols(symbolFromVariable, symbolFromModule) : - symbolFromModule || symbolFromVariable; - if (!symbol) { - error(name_13, ts.Diagnostics.Module_0_has_no_exported_member_1, getFullyQualifiedName(moduleSymbol), ts.declarationNameToString(name_13)); - } - return symbol; - } - } + } +})(ts || (ts = {})); +/// +/// +/* @internal */ +var ts; +(function (ts) { + var ambientModuleSymbolRegex = /^".+"$/; + var nextSymbolId = 1; + var nextNodeId = 1; + var nextMergeId = 1; + var nextFlowId = 1; + function getNodeId(node) { + if (!node.id) { + node.id = nextNodeId; + nextNodeId++; } - function getTargetOfImportSpecifier(node) { - return getExternalModuleMember(node.parent.parent.parent, node); + return node.id; + } + ts.getNodeId = getNodeId; + function getSymbolId(symbol) { + if (!symbol.id) { + symbol.id = nextSymbolId; + nextSymbolId++; } - function getTargetOfNamespaceExportDeclaration(node) { - return resolveExternalModuleSymbol(node.parent.symbol); + return symbol.id; + } + ts.getSymbolId = getSymbolId; + function createTypeChecker(host, produceDiagnostics) { + // Cancellation that controls whether or not we can cancel in the middle of type checking. + // In general cancelling is *not* safe for the type checker. We might be in the middle of + // computing something, and we will leave our internals in an inconsistent state. Callers + // who set the cancellation token should catch if a cancellation exception occurs, and + // should throw away and create a new TypeChecker. + // + // Currently we only support setting the cancellation token when getting diagnostics. This + // is because diagnostics can be quite expensive, and we want to allow hosts to bail out if + // they no longer need the information (for example, if the user started editing again). + var cancellationToken; + var Symbol = ts.objectAllocator.getSymbolConstructor(); + var Type = ts.objectAllocator.getTypeConstructor(); + var Signature = ts.objectAllocator.getSignatureConstructor(); + var typeCount = 0; + var symbolCount = 0; + var emptyArray = []; + var emptySymbols = ts.createMap(); + var compilerOptions = host.getCompilerOptions(); + var languageVersion = compilerOptions.target || 0 /* ES3 */; + var modulekind = ts.getEmitModuleKind(compilerOptions); + var noUnusedIdentifiers = !!compilerOptions.noUnusedLocals || !!compilerOptions.noUnusedParameters; + var allowSyntheticDefaultImports = typeof compilerOptions.allowSyntheticDefaultImports !== "undefined" ? compilerOptions.allowSyntheticDefaultImports : modulekind === ts.ModuleKind.System; + var strictNullChecks = compilerOptions.strictNullChecks; + var emitResolver = createResolver(); + var undefinedSymbol = createSymbol(4 /* Property */ | 67108864 /* Transient */, "undefined"); + undefinedSymbol.declarations = []; + var argumentsSymbol = createSymbol(4 /* Property */ | 67108864 /* Transient */, "arguments"); + var checker = { + getNodeCount: function () { return ts.sum(host.getSourceFiles(), "nodeCount"); }, + getIdentifierCount: function () { return ts.sum(host.getSourceFiles(), "identifierCount"); }, + getSymbolCount: function () { return ts.sum(host.getSourceFiles(), "symbolCount") + symbolCount; }, + getTypeCount: function () { return typeCount; }, + isUndefinedSymbol: function (symbol) { return symbol === undefinedSymbol; }, + isArgumentsSymbol: function (symbol) { return symbol === argumentsSymbol; }, + isUnknownSymbol: function (symbol) { return symbol === unknownSymbol; }, + getDiagnostics: getDiagnostics, + getGlobalDiagnostics: getGlobalDiagnostics, + getTypeOfSymbolAtLocation: getTypeOfSymbolAtLocation, + getSymbolsOfParameterPropertyDeclaration: getSymbolsOfParameterPropertyDeclaration, + getDeclaredTypeOfSymbol: getDeclaredTypeOfSymbol, + getPropertiesOfType: getPropertiesOfType, + getPropertyOfType: getPropertyOfType, + getSignaturesOfType: getSignaturesOfType, + getIndexTypeOfType: getIndexTypeOfType, + getBaseTypes: getBaseTypes, + getReturnTypeOfSignature: getReturnTypeOfSignature, + getNonNullableType: getNonNullableType, + getSymbolsInScope: getSymbolsInScope, + getSymbolAtLocation: getSymbolAtLocation, + getShorthandAssignmentValueSymbol: getShorthandAssignmentValueSymbol, + getExportSpecifierLocalTargetSymbol: getExportSpecifierLocalTargetSymbol, + getTypeAtLocation: getTypeOfNode, + getPropertySymbolOfDestructuringAssignment: getPropertySymbolOfDestructuringAssignment, + typeToString: typeToString, + getSymbolDisplayBuilder: getSymbolDisplayBuilder, + symbolToString: symbolToString, + getAugmentedPropertiesOfType: getAugmentedPropertiesOfType, + getRootSymbols: getRootSymbols, + getContextualType: getContextualType, + getFullyQualifiedName: getFullyQualifiedName, + getResolvedSignature: getResolvedSignature, + getConstantValue: getConstantValue, + isValidPropertyAccess: isValidPropertyAccess, + getSignatureFromDeclaration: getSignatureFromDeclaration, + isImplementationOfOverload: isImplementationOfOverload, + getAliasedSymbol: resolveAlias, + getEmitResolver: getEmitResolver, + getExportsOfModule: getExportsOfModuleAsArray, + getAmbientModules: getAmbientModules, + getJsxElementAttributesType: getJsxElementAttributesType, + getJsxIntrinsicTagNames: getJsxIntrinsicTagNames, + isOptionalParameter: isOptionalParameter + }; + var tupleTypes = []; + var unionTypes = ts.createMap(); + var intersectionTypes = ts.createMap(); + var stringLiteralTypes = ts.createMap(); + var numericLiteralTypes = ts.createMap(); + var unknownSymbol = createSymbol(4 /* Property */ | 67108864 /* Transient */, "unknown"); + var resolvingSymbol = createSymbol(67108864 /* Transient */, "__resolving__"); + var anyType = createIntrinsicType(1 /* Any */, "any"); + var autoType = createIntrinsicType(1 /* Any */, "any"); + var unknownType = createIntrinsicType(1 /* Any */, "unknown"); + var undefinedType = createIntrinsicType(2048 /* Undefined */, "undefined"); + var undefinedWideningType = strictNullChecks ? undefinedType : createIntrinsicType(2048 /* Undefined */ | 33554432 /* ContainsWideningType */, "undefined"); + var nullType = createIntrinsicType(4096 /* Null */, "null"); + var nullWideningType = strictNullChecks ? nullType : createIntrinsicType(4096 /* Null */ | 33554432 /* ContainsWideningType */, "null"); + var stringType = createIntrinsicType(2 /* String */, "string"); + var numberType = createIntrinsicType(4 /* Number */, "number"); + var trueType = createIntrinsicType(128 /* BooleanLiteral */, "true"); + var falseType = createIntrinsicType(128 /* BooleanLiteral */, "false"); + var booleanType = createBooleanType([trueType, falseType]); + var esSymbolType = createIntrinsicType(512 /* ESSymbol */, "symbol"); + var voidType = createIntrinsicType(1024 /* Void */, "void"); + var neverType = createIntrinsicType(8192 /* Never */, "never"); + var silentNeverType = createIntrinsicType(8192 /* Never */, "never"); + var emptyObjectType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); + var emptyGenericType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); + emptyGenericType.instantiations = ts.createMap(); + var anyFunctionType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); + // The anyFunctionType contains the anyFunctionType by definition. The flag is further propagated + // in getPropagatingFlagsOfTypes, and it is checked in inferFromTypes. + anyFunctionType.flags |= 134217728 /* ContainsAnyFunctionType */; + var noConstraintType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); + var anySignature = createSignature(undefined, undefined, undefined, emptyArray, anyType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false); + var unknownSignature = createSignature(undefined, undefined, undefined, emptyArray, unknownType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false); + var resolvingSignature = createSignature(undefined, undefined, undefined, emptyArray, anyType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false); + var silentNeverSignature = createSignature(undefined, undefined, undefined, emptyArray, silentNeverType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false); + var enumNumberIndexInfo = createIndexInfo(stringType, /*isReadonly*/ true); + var globals = ts.createMap(); + /** + * List of every ambient module with a "*" wildcard. + * Unlike other ambient modules, these can't be stored in `globals` because symbol tables only deal with exact matches. + * This is only used if there is no exact match. + */ + var patternAmbientModules; + var getGlobalESSymbolConstructorSymbol; + var getGlobalPromiseConstructorSymbol; + var tryGetGlobalPromiseConstructorSymbol; + var globalObjectType; + var globalFunctionType; + var globalArrayType; + var globalReadonlyArrayType; + var globalStringType; + var globalNumberType; + var globalBooleanType; + var globalRegExpType; + var anyArrayType; + var anyReadonlyArrayType; + // The library files are only loaded when the feature is used. + // This allows users to just specify library files they want to used through --lib + // and they will not get an error from not having unrelated library files + var getGlobalTemplateStringsArrayType; + var getGlobalESSymbolType; + var getGlobalIterableType; + var getGlobalIteratorType; + var getGlobalIterableIteratorType; + var getGlobalClassDecoratorType; + var getGlobalParameterDecoratorType; + var getGlobalPropertyDecoratorType; + var getGlobalMethodDecoratorType; + var getGlobalTypedPropertyDescriptorType; + var getGlobalPromiseType; + var tryGetGlobalPromiseType; + var getGlobalPromiseLikeType; + var getInstantiatedGlobalPromiseLikeType; + var getGlobalPromiseConstructorLikeType; + var getGlobalThenableType; + var jsxElementClassType; + var deferredNodes; + var deferredUnusedIdentifierNodes; + var flowLoopStart = 0; + var flowLoopCount = 0; + var visitedFlowCount = 0; + var emptyStringType = getLiteralTypeForText(32 /* StringLiteral */, ""); + var zeroType = getLiteralTypeForText(64 /* NumberLiteral */, "0"); + var resolutionTargets = []; + var resolutionResults = []; + var resolutionPropertyNames = []; + var mergedSymbols = []; + var symbolLinks = []; + var nodeLinks = []; + var flowLoopCaches = []; + var flowLoopNodes = []; + var flowLoopKeys = []; + var flowLoopTypes = []; + var visitedFlowNodes = []; + var visitedFlowTypes = []; + var potentialThisCollisions = []; + var awaitedTypeStack = []; + var diagnostics = ts.createDiagnosticCollection(); + var TypeFacts; + (function (TypeFacts) { + TypeFacts[TypeFacts["None"] = 0] = "None"; + TypeFacts[TypeFacts["TypeofEQString"] = 1] = "TypeofEQString"; + TypeFacts[TypeFacts["TypeofEQNumber"] = 2] = "TypeofEQNumber"; + TypeFacts[TypeFacts["TypeofEQBoolean"] = 4] = "TypeofEQBoolean"; + TypeFacts[TypeFacts["TypeofEQSymbol"] = 8] = "TypeofEQSymbol"; + TypeFacts[TypeFacts["TypeofEQObject"] = 16] = "TypeofEQObject"; + TypeFacts[TypeFacts["TypeofEQFunction"] = 32] = "TypeofEQFunction"; + TypeFacts[TypeFacts["TypeofEQHostObject"] = 64] = "TypeofEQHostObject"; + TypeFacts[TypeFacts["TypeofNEString"] = 128] = "TypeofNEString"; + TypeFacts[TypeFacts["TypeofNENumber"] = 256] = "TypeofNENumber"; + TypeFacts[TypeFacts["TypeofNEBoolean"] = 512] = "TypeofNEBoolean"; + TypeFacts[TypeFacts["TypeofNESymbol"] = 1024] = "TypeofNESymbol"; + TypeFacts[TypeFacts["TypeofNEObject"] = 2048] = "TypeofNEObject"; + TypeFacts[TypeFacts["TypeofNEFunction"] = 4096] = "TypeofNEFunction"; + TypeFacts[TypeFacts["TypeofNEHostObject"] = 8192] = "TypeofNEHostObject"; + TypeFacts[TypeFacts["EQUndefined"] = 16384] = "EQUndefined"; + TypeFacts[TypeFacts["EQNull"] = 32768] = "EQNull"; + TypeFacts[TypeFacts["EQUndefinedOrNull"] = 65536] = "EQUndefinedOrNull"; + TypeFacts[TypeFacts["NEUndefined"] = 131072] = "NEUndefined"; + TypeFacts[TypeFacts["NENull"] = 262144] = "NENull"; + TypeFacts[TypeFacts["NEUndefinedOrNull"] = 524288] = "NEUndefinedOrNull"; + TypeFacts[TypeFacts["Truthy"] = 1048576] = "Truthy"; + TypeFacts[TypeFacts["Falsy"] = 2097152] = "Falsy"; + TypeFacts[TypeFacts["Discriminatable"] = 4194304] = "Discriminatable"; + TypeFacts[TypeFacts["All"] = 8388607] = "All"; + // The following members encode facts about particular kinds of types for use in the getTypeFacts function. + // The presence of a particular fact means that the given test is true for some (and possibly all) values + // of that kind of type. + TypeFacts[TypeFacts["BaseStringStrictFacts"] = 933633] = "BaseStringStrictFacts"; + TypeFacts[TypeFacts["BaseStringFacts"] = 3145473] = "BaseStringFacts"; + TypeFacts[TypeFacts["StringStrictFacts"] = 4079361] = "StringStrictFacts"; + TypeFacts[TypeFacts["StringFacts"] = 4194049] = "StringFacts"; + TypeFacts[TypeFacts["EmptyStringStrictFacts"] = 3030785] = "EmptyStringStrictFacts"; + TypeFacts[TypeFacts["EmptyStringFacts"] = 3145473] = "EmptyStringFacts"; + TypeFacts[TypeFacts["NonEmptyStringStrictFacts"] = 1982209] = "NonEmptyStringStrictFacts"; + TypeFacts[TypeFacts["NonEmptyStringFacts"] = 4194049] = "NonEmptyStringFacts"; + TypeFacts[TypeFacts["BaseNumberStrictFacts"] = 933506] = "BaseNumberStrictFacts"; + TypeFacts[TypeFacts["BaseNumberFacts"] = 3145346] = "BaseNumberFacts"; + TypeFacts[TypeFacts["NumberStrictFacts"] = 4079234] = "NumberStrictFacts"; + TypeFacts[TypeFacts["NumberFacts"] = 4193922] = "NumberFacts"; + TypeFacts[TypeFacts["ZeroStrictFacts"] = 3030658] = "ZeroStrictFacts"; + TypeFacts[TypeFacts["ZeroFacts"] = 3145346] = "ZeroFacts"; + TypeFacts[TypeFacts["NonZeroStrictFacts"] = 1982082] = "NonZeroStrictFacts"; + TypeFacts[TypeFacts["NonZeroFacts"] = 4193922] = "NonZeroFacts"; + TypeFacts[TypeFacts["BaseBooleanStrictFacts"] = 933252] = "BaseBooleanStrictFacts"; + TypeFacts[TypeFacts["BaseBooleanFacts"] = 3145092] = "BaseBooleanFacts"; + TypeFacts[TypeFacts["BooleanStrictFacts"] = 4078980] = "BooleanStrictFacts"; + TypeFacts[TypeFacts["BooleanFacts"] = 4193668] = "BooleanFacts"; + TypeFacts[TypeFacts["FalseStrictFacts"] = 3030404] = "FalseStrictFacts"; + TypeFacts[TypeFacts["FalseFacts"] = 3145092] = "FalseFacts"; + TypeFacts[TypeFacts["TrueStrictFacts"] = 1981828] = "TrueStrictFacts"; + TypeFacts[TypeFacts["TrueFacts"] = 4193668] = "TrueFacts"; + TypeFacts[TypeFacts["SymbolStrictFacts"] = 1981320] = "SymbolStrictFacts"; + TypeFacts[TypeFacts["SymbolFacts"] = 4193160] = "SymbolFacts"; + TypeFacts[TypeFacts["ObjectStrictFacts"] = 6166480] = "ObjectStrictFacts"; + TypeFacts[TypeFacts["ObjectFacts"] = 8378320] = "ObjectFacts"; + TypeFacts[TypeFacts["FunctionStrictFacts"] = 6164448] = "FunctionStrictFacts"; + TypeFacts[TypeFacts["FunctionFacts"] = 8376288] = "FunctionFacts"; + TypeFacts[TypeFacts["UndefinedFacts"] = 2457472] = "UndefinedFacts"; + TypeFacts[TypeFacts["NullFacts"] = 2340752] = "NullFacts"; + })(TypeFacts || (TypeFacts = {})); + var typeofEQFacts = ts.createMap({ + "string": 1 /* TypeofEQString */, + "number": 2 /* TypeofEQNumber */, + "boolean": 4 /* TypeofEQBoolean */, + "symbol": 8 /* TypeofEQSymbol */, + "undefined": 16384 /* EQUndefined */, + "object": 16 /* TypeofEQObject */, + "function": 32 /* TypeofEQFunction */ + }); + var typeofNEFacts = ts.createMap({ + "string": 128 /* TypeofNEString */, + "number": 256 /* TypeofNENumber */, + "boolean": 512 /* TypeofNEBoolean */, + "symbol": 1024 /* TypeofNESymbol */, + "undefined": 131072 /* NEUndefined */, + "object": 2048 /* TypeofNEObject */, + "function": 4096 /* TypeofNEFunction */ + }); + var typeofTypesByName = ts.createMap({ + "string": stringType, + "number": numberType, + "boolean": booleanType, + "symbol": esSymbolType, + "undefined": undefinedType + }); + var jsxElementType; + /** Things we lazy load from the JSX namespace */ + var jsxTypes = ts.createMap(); + var JsxNames = { + JSX: "JSX", + IntrinsicElements: "IntrinsicElements", + ElementClass: "ElementClass", + ElementAttributesPropertyNameContainer: "ElementAttributesProperty", + Element: "Element", + IntrinsicAttributes: "IntrinsicAttributes", + IntrinsicClassAttributes: "IntrinsicClassAttributes" + }; + var subtypeRelation = ts.createMap(); + var assignableRelation = ts.createMap(); + var comparableRelation = ts.createMap(); + var identityRelation = ts.createMap(); + var enumRelation = ts.createMap(); + // This is for caching the result of getSymbolDisplayBuilder. Do not access directly. + var _displayBuilder; + var TypeSystemPropertyName; + (function (TypeSystemPropertyName) { + TypeSystemPropertyName[TypeSystemPropertyName["Type"] = 0] = "Type"; + TypeSystemPropertyName[TypeSystemPropertyName["ResolvedBaseConstructorType"] = 1] = "ResolvedBaseConstructorType"; + TypeSystemPropertyName[TypeSystemPropertyName["DeclaredType"] = 2] = "DeclaredType"; + TypeSystemPropertyName[TypeSystemPropertyName["ResolvedReturnType"] = 3] = "ResolvedReturnType"; + })(TypeSystemPropertyName || (TypeSystemPropertyName = {})); + var builtinGlobals = ts.createMap(); + builtinGlobals[undefinedSymbol.name] = undefinedSymbol; + initializeTypeChecker(); + return checker; + function getEmitResolver(sourceFile, cancellationToken) { + // Ensure we have all the type information in place for this file so that all the + // emitter questions of this resolver will return the right information. + getDiagnostics(sourceFile, cancellationToken); + return emitResolver; } - function getTargetOfExportSpecifier(node) { - return node.parent.parent.moduleSpecifier ? - getExternalModuleMember(node.parent.parent, node) : - resolveEntityName(node.propertyName || node.name, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */); + function error(location, message, arg0, arg1, arg2) { + var diagnostic = location + ? ts.createDiagnosticForNode(location, message, arg0, arg1, arg2) + : ts.createCompilerDiagnostic(message, arg0, arg1, arg2); + diagnostics.add(diagnostic); } - function getTargetOfExportAssignment(node) { - return resolveEntityName(node.expression, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */); + function createSymbol(flags, name) { + symbolCount++; + return new Symbol(flags, name); } - function getTargetOfAliasDeclaration(node) { - switch (node.kind) { - case 229 /* ImportEqualsDeclaration */: - return getTargetOfImportEqualsDeclaration(node); - case 231 /* ImportClause */: - return getTargetOfImportClause(node); - case 232 /* NamespaceImport */: - return getTargetOfNamespaceImport(node); - case 234 /* ImportSpecifier */: - return getTargetOfImportSpecifier(node); - case 238 /* ExportSpecifier */: - return getTargetOfExportSpecifier(node); - case 235 /* ExportAssignment */: - return getTargetOfExportAssignment(node); - case 228 /* NamespaceExportDeclaration */: - return getTargetOfNamespaceExportDeclaration(node); + function getExcludedSymbolFlags(flags) { + var result = 0; + if (flags & 2 /* BlockScopedVariable */) + result |= 107455 /* BlockScopedVariableExcludes */; + if (flags & 1 /* FunctionScopedVariable */) + result |= 107454 /* FunctionScopedVariableExcludes */; + if (flags & 4 /* Property */) + result |= 0 /* PropertyExcludes */; + if (flags & 8 /* EnumMember */) + result |= 900095 /* EnumMemberExcludes */; + if (flags & 16 /* Function */) + result |= 106927 /* FunctionExcludes */; + if (flags & 32 /* Class */) + result |= 899519 /* ClassExcludes */; + if (flags & 64 /* Interface */) + result |= 792968 /* InterfaceExcludes */; + if (flags & 256 /* RegularEnum */) + result |= 899327 /* RegularEnumExcludes */; + if (flags & 128 /* ConstEnum */) + result |= 899967 /* ConstEnumExcludes */; + if (flags & 512 /* ValueModule */) + result |= 106639 /* ValueModuleExcludes */; + if (flags & 8192 /* Method */) + result |= 99263 /* MethodExcludes */; + if (flags & 32768 /* GetAccessor */) + result |= 41919 /* GetAccessorExcludes */; + if (flags & 65536 /* SetAccessor */) + result |= 74687 /* SetAccessorExcludes */; + if (flags & 262144 /* TypeParameter */) + result |= 530920 /* TypeParameterExcludes */; + if (flags & 524288 /* TypeAlias */) + result |= 793064 /* TypeAliasExcludes */; + if (flags & 8388608 /* Alias */) + result |= 8388608 /* AliasExcludes */; + return result; + } + function recordMergedSymbol(target, source) { + if (!source.mergeId) { + source.mergeId = nextMergeId; + nextMergeId++; } + mergedSymbols[source.mergeId] = target; } - function resolveSymbol(symbol) { - return symbol && symbol.flags & 8388608 /* Alias */ && !(symbol.flags & (107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */)) ? resolveAlias(symbol) : symbol; + function cloneSymbol(symbol) { + var result = createSymbol(symbol.flags | 33554432 /* Merged */, symbol.name); + result.declarations = symbol.declarations.slice(0); + result.parent = symbol.parent; + if (symbol.valueDeclaration) + result.valueDeclaration = symbol.valueDeclaration; + if (symbol.constEnumOnlyModule) + result.constEnumOnlyModule = true; + if (symbol.members) + result.members = ts.cloneMap(symbol.members); + if (symbol.exports) + result.exports = ts.cloneMap(symbol.exports); + recordMergedSymbol(result, symbol); + return result; } - function resolveAlias(symbol) { - ts.Debug.assert((symbol.flags & 8388608 /* Alias */) !== 0, "Should only get Alias here."); - var links = getSymbolLinks(symbol); - if (!links.target) { - links.target = resolvingSymbol; - var node = getDeclarationOfAliasSymbol(symbol); - ts.Debug.assert(!!node); - var target = getTargetOfAliasDeclaration(node); - if (links.target === resolvingSymbol) { - links.target = target || unknownSymbol; + function mergeSymbol(target, source) { + if (!(target.flags & getExcludedSymbolFlags(source.flags))) { + if (source.flags & 512 /* ValueModule */ && target.flags & 512 /* ValueModule */ && target.constEnumOnlyModule && !source.constEnumOnlyModule) { + // reset flag when merging instantiated module into value module that has only const enums + target.constEnumOnlyModule = false; } - else { - error(node, ts.Diagnostics.Circular_definition_of_import_alias_0, symbolToString(symbol)); + target.flags |= source.flags; + if (source.valueDeclaration && + (!target.valueDeclaration || + (target.valueDeclaration.kind === 225 /* ModuleDeclaration */ && source.valueDeclaration.kind !== 225 /* ModuleDeclaration */))) { + // other kinds of value declarations take precedence over modules + target.valueDeclaration = source.valueDeclaration; + } + ts.forEach(source.declarations, function (node) { + target.declarations.push(node); + }); + if (source.members) { + if (!target.members) + target.members = ts.createMap(); + mergeSymbolTable(target.members, source.members); + } + if (source.exports) { + if (!target.exports) + target.exports = ts.createMap(); + mergeSymbolTable(target.exports, source.exports); } + recordMergedSymbol(target, source); } - else if (links.target === resolvingSymbol) { - links.target = unknownSymbol; + else { + var message_2 = target.flags & 2 /* BlockScopedVariable */ || source.flags & 2 /* BlockScopedVariable */ + ? ts.Diagnostics.Cannot_redeclare_block_scoped_variable_0 : ts.Diagnostics.Duplicate_identifier_0; + ts.forEach(source.declarations, function (node) { + error(node.name ? node.name : node, message_2, symbolToString(source)); + }); + ts.forEach(target.declarations, function (node) { + error(node.name ? node.name : node, message_2, symbolToString(source)); + }); } - return links.target; } - function markExportAsReferenced(node) { - var symbol = getSymbolOfNode(node); - var target = resolveAlias(symbol); - if (target) { - var markAlias = target === unknownSymbol || - ((target.flags & 107455 /* Value */) && !isConstEnumOrConstEnumOnlyModule(target)); - if (markAlias) { - markAliasSymbolAsReferenced(symbol); + function mergeSymbolTable(target, source) { + for (var id in source) { + var targetSymbol = target[id]; + if (!targetSymbol) { + target[id] = source[id]; + } + else { + if (!(targetSymbol.flags & 33554432 /* Merged */)) { + target[id] = targetSymbol = cloneSymbol(targetSymbol); + } + mergeSymbol(targetSymbol, source[id]); } } } - // When an alias symbol is referenced, we need to mark the entity it references as referenced and in turn repeat that until - // we reach a non-alias or an exported entity (which is always considered referenced). We do this by checking the target of - // the alias as an expression (which recursively takes us back here if the target references another alias). - function markAliasSymbolAsReferenced(symbol) { - var links = getSymbolLinks(symbol); - if (!links.referenced) { - links.referenced = true; - var node = getDeclarationOfAliasSymbol(symbol); - ts.Debug.assert(!!node); - if (node.kind === 235 /* ExportAssignment */) { - // export default - checkExpressionCached(node.expression); - } - else if (node.kind === 238 /* ExportSpecifier */) { - // export { } or export { as foo } - checkExpressionCached(node.propertyName || node.name); - } - else if (ts.isInternalModuleImportEqualsDeclaration(node)) { - // import foo = - checkExpressionCached(node.moduleReference); - } - } - } - // This function is only for imports with entity names - function getSymbolOfPartOfRightHandSideOfImportEquals(entityName, importDeclaration, dontResolveAlias) { - // There are three things we might try to look for. In the following examples, - // the search term is enclosed in |...|: - // - // import a = |b|; // Namespace - // import a = |b.c|; // Value, type, namespace - // import a = |b.c|.d; // Namespace - if (entityName.kind === 69 /* Identifier */ && ts.isRightSideOfQualifiedNameOrPropertyAccess(entityName)) { - entityName = entityName.parent; + function mergeModuleAugmentation(moduleName) { + var moduleAugmentation = moduleName.parent; + if (moduleAugmentation.symbol.declarations[0] !== moduleAugmentation) { + // this is a combined symbol for multiple augmentations within the same file. + // its symbol already has accumulated information for all declarations + // so we need to add it just once - do the work only for first declaration + ts.Debug.assert(moduleAugmentation.symbol.declarations.length > 1); + return; } - // Check for case 1 and 3 in the above example - if (entityName.kind === 69 /* Identifier */ || entityName.parent.kind === 139 /* QualifiedName */) { - return resolveEntityName(entityName, 1920 /* Namespace */, /*ignoreErrors*/ false, dontResolveAlias); + if (ts.isGlobalScopeAugmentation(moduleAugmentation)) { + mergeSymbolTable(globals, moduleAugmentation.symbol.exports); } else { - // Case 2 in above example - // entityName.kind could be a QualifiedName or a Missing identifier - ts.Debug.assert(entityName.parent.kind === 229 /* ImportEqualsDeclaration */); - return resolveEntityName(entityName, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */, /*ignoreErrors*/ false, dontResolveAlias); - } - } - function getFullyQualifiedName(symbol) { - return symbol.parent ? getFullyQualifiedName(symbol.parent) + "." + symbolToString(symbol) : symbolToString(symbol); - } - // Resolves a qualified name and any involved aliases - function resolveEntityName(name, meaning, ignoreErrors, dontResolveAlias, location) { - if (ts.nodeIsMissing(name)) { - return undefined; - } - var symbol; - if (name.kind === 69 /* Identifier */) { - var message = meaning === 1920 /* Namespace */ ? ts.Diagnostics.Cannot_find_namespace_0 : ts.Diagnostics.Cannot_find_name_0; - symbol = resolveName(location || name, name.text, meaning, ignoreErrors ? undefined : message, name); - if (!symbol) { - return undefined; - } - } - else if (name.kind === 139 /* QualifiedName */ || name.kind === 172 /* PropertyAccessExpression */) { - var left = name.kind === 139 /* QualifiedName */ ? name.left : name.expression; - var right = name.kind === 139 /* QualifiedName */ ? name.right : name.name; - var namespace = resolveEntityName(left, 1920 /* Namespace */, ignoreErrors, /*dontResolveAlias*/ false, location); - if (!namespace || ts.nodeIsMissing(right)) { - return undefined; + // find a module that about to be augmented + // do not validate names of augmentations that are defined in ambient context + var moduleNotFoundError = !ts.isInAmbientContext(moduleName.parent.parent) + ? ts.Diagnostics.Invalid_module_name_in_augmentation_module_0_cannot_be_found + : undefined; + var mainModule = resolveExternalModuleNameWorker(moduleName, moduleName, moduleNotFoundError); + if (!mainModule) { + return; } - else if (namespace === unknownSymbol) { - return namespace; + // obtain item referenced by 'export=' + mainModule = resolveExternalModuleSymbol(mainModule); + if (mainModule.flags & 1920 /* Namespace */) { + // if module symbol has already been merged - it is safe to use it. + // otherwise clone it + mainModule = mainModule.flags & 33554432 /* Merged */ ? mainModule : cloneSymbol(mainModule); + mergeSymbol(mainModule, moduleAugmentation.symbol); } - symbol = getSymbol(getExportsOfSymbol(namespace), right.text, meaning); - if (!symbol) { - if (!ignoreErrors) { - error(right, ts.Diagnostics.Namespace_0_has_no_exported_member_1, getFullyQualifiedName(namespace), ts.declarationNameToString(right)); - } - return undefined; + else { + error(moduleName, ts.Diagnostics.Cannot_augment_module_0_because_it_resolves_to_a_non_module_entity, moduleName.text); } } - else { - ts.Debug.fail("Unknown entity name kind."); - } - ts.Debug.assert((symbol.flags & 16777216 /* Instantiated */) === 0, "Should never get an instantiated symbol here."); - return (symbol.flags & meaning) || dontResolveAlias ? symbol : resolveAlias(symbol); - } - function resolveExternalModuleName(location, moduleReferenceExpression) { - return resolveExternalModuleNameWorker(location, moduleReferenceExpression, ts.Diagnostics.Cannot_find_module_0); - } - function resolveExternalModuleNameWorker(location, moduleReferenceExpression, moduleNotFoundError) { - if (moduleReferenceExpression.kind !== 9 /* StringLiteral */) { - return; - } - var moduleReferenceLiteral = moduleReferenceExpression; - return resolveExternalModule(location, moduleReferenceLiteral.text, moduleNotFoundError, moduleReferenceLiteral); } - function resolveExternalModule(location, moduleReference, moduleNotFoundError, errorNode) { - // Module names are escaped in our symbol table. However, string literal values aren't. - // Escape the name in the "require(...)" clause to ensure we find the right symbol. - var moduleName = ts.escapeIdentifier(moduleReference); - if (moduleName === undefined) { - return; - } - var isRelative = ts.isExternalModuleNameRelative(moduleName); - if (!isRelative) { - var symbol = getSymbol(globals, '"' + moduleName + '"', 512 /* ValueModule */); - if (symbol) { - // merged symbol is module declaration symbol combined with all augmentations - return getMergedSymbol(symbol); - } - } - var resolvedModule = ts.getResolvedModule(ts.getSourceFileOfNode(location), moduleReference); - var sourceFile = resolvedModule && host.getSourceFile(resolvedModule.resolvedFileName); - if (sourceFile) { - if (sourceFile.symbol) { - // merged symbol is module declaration symbol combined with all augmentations - return getMergedSymbol(sourceFile.symbol); - } - if (moduleNotFoundError) { - // report errors only if it was requested - error(errorNode, ts.Diagnostics.File_0_is_not_a_module, sourceFile.fileName); - } - return undefined; - } - if (patternAmbientModules) { - var pattern = ts.findBestPatternMatch(patternAmbientModules, function (_) { return _.pattern; }, moduleName); - if (pattern) { - return getMergedSymbol(pattern.symbol); - } - } - if (moduleNotFoundError) { - // report errors only if it was requested - var tsExtension = ts.tryExtractTypeScriptExtension(moduleName); - if (tsExtension) { - var diag = ts.Diagnostics.An_import_path_cannot_end_with_a_0_extension_Consider_importing_1_instead; - error(errorNode, diag, tsExtension, ts.removeExtension(moduleName, tsExtension)); + function addToSymbolTable(target, source, message) { + for (var id in source) { + if (target[id]) { + // Error on redeclarations + ts.forEach(target[id].declarations, addDeclarationDiagnostic(id, message)); } else { - error(errorNode, moduleNotFoundError, moduleName); + target[id] = source[id]; } } - return undefined; - } - // An external module with an 'export =' declaration resolves to the target of the 'export =' declaration, - // and an external module with no 'export =' declaration resolves to the module itself. - function resolveExternalModuleSymbol(moduleSymbol) { - return moduleSymbol && getMergedSymbol(resolveSymbol(moduleSymbol.exports["export="])) || moduleSymbol; - } - // An external module with an 'export =' declaration may be referenced as an ES6 module provided the 'export =' - // references a symbol that is at least declared as a module or a variable. The target of the 'export =' may - // combine other declarations with the module or variable (e.g. a class/module, function/module, interface/variable). - function resolveESModuleSymbol(moduleSymbol, moduleReferenceExpression) { - var symbol = resolveExternalModuleSymbol(moduleSymbol); - if (symbol && !(symbol.flags & (1536 /* Module */ | 3 /* Variable */))) { - error(moduleReferenceExpression, ts.Diagnostics.Module_0_resolves_to_a_non_module_entity_and_cannot_be_imported_using_this_construct, symbolToString(moduleSymbol)); - symbol = undefined; + function addDeclarationDiagnostic(id, message) { + return function (declaration) { return diagnostics.add(ts.createDiagnosticForNode(declaration, message, id)); }; } - return symbol; - } - function hasExportAssignmentSymbol(moduleSymbol) { - return moduleSymbol.exports["export="] !== undefined; - } - function getExportsOfModuleAsArray(moduleSymbol) { - return symbolsToArray(getExportsOfModule(moduleSymbol)); } - function getExportsOfSymbol(symbol) { - return symbol.flags & 1536 /* Module */ ? getExportsOfModule(symbol) : symbol.exports || emptySymbols; + function getSymbolLinks(symbol) { + if (symbol.flags & 67108864 /* Transient */) + return symbol; + var id = getSymbolId(symbol); + return symbolLinks[id] || (symbolLinks[id] = {}); } - function getExportsOfModule(moduleSymbol) { - var links = getSymbolLinks(moduleSymbol); - return links.resolvedExports || (links.resolvedExports = getExportsForModule(moduleSymbol)); + function getNodeLinks(node) { + var nodeId = getNodeId(node); + return nodeLinks[nodeId] || (nodeLinks[nodeId] = { flags: 0 }); } - /** - * Extends one symbol table with another while collecting information on name collisions for error message generation into the `lookupTable` argument - * Not passing `lookupTable` and `exportNode` disables this collection, and just extends the tables - */ - function extendExportSymbols(target, source, lookupTable, exportNode) { - for (var id in source) { - if (id !== "default" && !target[id]) { - target[id] = source[id]; - if (lookupTable && exportNode) { - lookupTable[id] = { - specifierText: ts.getTextOfNode(exportNode.moduleSpecifier) - }; - } - } - else if (lookupTable && exportNode && id !== "default" && target[id] && resolveSymbol(target[id]) !== resolveSymbol(source[id])) { - if (!lookupTable[id].exportsWithDuplicate) { - lookupTable[id].exportsWithDuplicate = [exportNode]; - } - else { - lookupTable[id].exportsWithDuplicate.push(exportNode); - } - } - } + function isGlobalSourceFile(node) { + return node.kind === 256 /* SourceFile */ && !ts.isExternalOrCommonJsModule(node); } - function getExportsForModule(moduleSymbol) { - var visitedSymbols = []; - return visit(moduleSymbol) || moduleSymbol.exports; - // The ES6 spec permits export * declarations in a module to circularly reference the module itself. For example, - // module 'a' can 'export * from "b"' and 'b' can 'export * from "a"' without error. - function visit(symbol) { - if (!(symbol && symbol.flags & 1952 /* HasExports */ && !ts.contains(visitedSymbols, symbol))) { - return; - } - visitedSymbols.push(symbol); - var symbols = ts.cloneMap(symbol.exports); - // All export * declarations are collected in an __export symbol by the binder - var exportStars = symbol.exports["__export"]; - if (exportStars) { - var nestedSymbols = ts.createMap(); - var lookupTable = ts.createMap(); - for (var _i = 0, _a = exportStars.declarations; _i < _a.length; _i++) { - var node = _a[_i]; - var resolvedModule = resolveExternalModuleName(node, node.moduleSpecifier); - var exportedSymbols = visit(resolvedModule); - extendExportSymbols(nestedSymbols, exportedSymbols, lookupTable, node); + function getSymbol(symbols, name, meaning) { + if (meaning) { + var symbol = symbols[name]; + if (symbol) { + ts.Debug.assert((symbol.flags & 16777216 /* Instantiated */) === 0, "Should never get an instantiated symbol here."); + if (symbol.flags & meaning) { + return symbol; } - for (var id in lookupTable) { - var exportsWithDuplicate = lookupTable[id].exportsWithDuplicate; - // It's not an error if the file with multiple `export *`s with duplicate names exports a member with that name itself - if (id === "export=" || !(exportsWithDuplicate && exportsWithDuplicate.length) || symbols[id]) { - continue; - } - for (var _b = 0, exportsWithDuplicate_1 = exportsWithDuplicate; _b < exportsWithDuplicate_1.length; _b++) { - var node = exportsWithDuplicate_1[_b]; - diagnostics.add(ts.createDiagnosticForNode(node, ts.Diagnostics.Module_0_has_already_exported_a_member_named_1_Consider_explicitly_re_exporting_to_resolve_the_ambiguity, lookupTable[id].specifierText, id)); + if (symbol.flags & 8388608 /* Alias */) { + var target = resolveAlias(symbol); + // Unknown symbol means an error occurred in alias resolution, treat it as positive answer to avoid cascading errors + if (target === unknownSymbol || target.flags & meaning) { + return symbol; } } - extendExportSymbols(symbols, nestedSymbols); } - return symbols; } + // return undefined if we can't find a symbol. } - function getMergedSymbol(symbol) { - var merged; - return symbol && symbol.mergeId && (merged = mergedSymbols[symbol.mergeId]) ? merged : symbol; - } - function getSymbolOfNode(node) { - return getMergedSymbol(node.symbol); - } - function getParentOfSymbol(symbol) { - return getMergedSymbol(symbol.parent); - } - function getExportSymbolOfValueSymbolIfExported(symbol) { - return symbol && (symbol.flags & 1048576 /* ExportValue */) !== 0 - ? getMergedSymbol(symbol.exportSymbol) - : symbol; - } - function symbolIsValue(symbol) { - // If it is an instantiated symbol, then it is a value if the symbol it is an - // instantiation of is a value. - if (symbol.flags & 16777216 /* Instantiated */) { - return symbolIsValue(getSymbolLinks(symbol).target); + /** + * Get symbols that represent parameter-property-declaration as parameter and as property declaration + * @param parameter a parameterDeclaration node + * @param parameterName a name of the parameter to get the symbols for. + * @return a tuple of two symbols + */ + function getSymbolsOfParameterPropertyDeclaration(parameter, parameterName) { + var constructorDeclaration = parameter.parent; + var classDeclaration = parameter.parent.parent; + var parameterSymbol = getSymbol(constructorDeclaration.locals, parameterName, 107455 /* Value */); + var propertySymbol = getSymbol(classDeclaration.symbol.members, parameterName, 107455 /* Value */); + if (parameterSymbol && propertySymbol) { + return [parameterSymbol, propertySymbol]; } - // If the symbol has the value flag, it is trivially a value. - if (symbol.flags & 107455 /* Value */) { - return true; + ts.Debug.fail("There should exist two symbols, one as property declaration and one as parameter declaration"); + } + function isBlockScopedNameDeclaredBeforeUse(declaration, usage) { + var declarationFile = ts.getSourceFileOfNode(declaration); + var useFile = ts.getSourceFileOfNode(usage); + if (declarationFile !== useFile) { + if ((modulekind && (declarationFile.externalModuleIndicator || useFile.externalModuleIndicator)) || + (!compilerOptions.outFile && !compilerOptions.out)) { + // nodes are in different files and order cannot be determines + return true; + } + var sourceFiles = host.getSourceFiles(); + return ts.indexOf(sourceFiles, declarationFile) <= ts.indexOf(sourceFiles, useFile); } - // If it is an alias, then it is a value if the symbol it resolves to is a value. - if (symbol.flags & 8388608 /* Alias */) { - return (resolveAlias(symbol).flags & 107455 /* Value */) !== 0; + if (declaration.pos <= usage.pos) { + // declaration is before usage + // still might be illegal if usage is in the initializer of the variable declaration + return declaration.kind !== 218 /* VariableDeclaration */ || + !isImmediatelyUsedInInitializerOfBlockScopedVariable(declaration, usage); } - return false; - } - function findConstructorDeclaration(node) { - var members = node.members; - for (var _i = 0, members_1 = members; _i < members_1.length; _i++) { - var member = members_1[_i]; - if (member.kind === 148 /* Constructor */ && ts.nodeIsPresent(member.body)) { - return member; + // declaration is after usage + // can be legal if usage is deferred (i.e. inside function or in initializer of instance property) + return isUsedInFunctionOrNonStaticProperty(declaration, usage); + function isImmediatelyUsedInInitializerOfBlockScopedVariable(declaration, usage) { + var container = ts.getEnclosingBlockScopeContainer(declaration); + switch (declaration.parent.parent.kind) { + case 200 /* VariableStatement */: + case 206 /* ForStatement */: + case 208 /* ForOfStatement */: + // variable statement/for/for-of statement case, + // use site should not be inside variable declaration (initializer of declaration or binding element) + if (isSameScopeDescendentOf(usage, declaration, container)) { + return true; + } + break; + } + switch (declaration.parent.parent.kind) { + case 207 /* ForInStatement */: + case 208 /* ForOfStatement */: + // ForIn/ForOf case - use site should not be used in expression part + if (isSameScopeDescendentOf(usage, declaration.parent.parent.expression, container)) { + return true; + } } + return false; } - } - function createType(flags) { - var result = new Type(checker, flags); - typeCount++; - result.id = typeCount; - return result; - } - function createIntrinsicType(kind, intrinsicName) { - var type = createType(kind); - type.intrinsicName = intrinsicName; - return type; - } - function createBooleanType(trueFalseTypes) { - var type = getUnionType(trueFalseTypes); - type.flags |= 8 /* Boolean */; - type.intrinsicName = "boolean"; - return type; - } - function createObjectType(kind, symbol) { - var type = createType(kind); - type.symbol = symbol; - return type; - } - // A reserved member name starts with two underscores, but the third character cannot be an underscore - // or the @ symbol. A third underscore indicates an escaped form of an identifer that started - // with at least two underscores. The @ character indicates that the name is denoted by a well known ES - // Symbol instance. - function isReservedMemberName(name) { - return name.charCodeAt(0) === 95 /* _ */ && - name.charCodeAt(1) === 95 /* _ */ && - name.charCodeAt(2) !== 95 /* _ */ && - name.charCodeAt(2) !== 64 /* at */; - } - function getNamedMembers(members) { - var result; - for (var id in members) { - if (!isReservedMemberName(id)) { - if (!result) - result = []; - var symbol = members[id]; - if (symbolIsValue(symbol)) { - result.push(symbol); + function isUsedInFunctionOrNonStaticProperty(declaration, usage) { + var container = ts.getEnclosingBlockScopeContainer(declaration); + var current = usage; + while (current) { + if (current === container) { + return false; + } + if (ts.isFunctionLike(current)) { + return true; + } + var initializerOfNonStaticProperty = current.parent && + current.parent.kind === 145 /* PropertyDeclaration */ && + (ts.getModifierFlags(current.parent) & 32 /* Static */) === 0 && + current.parent.initializer === current; + if (initializerOfNonStaticProperty) { + return true; } + current = current.parent; } + return false; } - return result || emptyArray; - } - function setObjectTypeMembers(type, members, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo) { - type.members = members; - type.properties = getNamedMembers(members); - type.callSignatures = callSignatures; - type.constructSignatures = constructSignatures; - if (stringIndexInfo) - type.stringIndexInfo = stringIndexInfo; - if (numberIndexInfo) - type.numberIndexInfo = numberIndexInfo; - return type; } - function createAnonymousType(symbol, members, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo) { - return setObjectTypeMembers(createObjectType(2097152 /* Anonymous */, symbol), members, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo); - } - function forEachSymbolTableInScope(enclosingDeclaration, callback) { + // Resolve a given name for a given meaning at a given location. An error is reported if the name was not found and + // the nameNotFoundMessage argument is not undefined. Returns the resolved symbol, or undefined if no symbol with + // the given name can be found. + function resolveName(location, name, meaning, nameNotFoundMessage, nameArg) { var result; - for (var location_1 = enclosingDeclaration; location_1; location_1 = location_1.parent) { + var lastLocation; + var propertyWithInvalidInitializer; + var errorLocation = location; + var grandparent; + var isInExternalModule = false; + loop: while (location) { // Locals of a source file are not in scope (because they get merged into the global symbol table) - if (location_1.locals && !isGlobalSourceFile(location_1)) { - if (result = callback(location_1.locals)) { - return result; + if (location.locals && !isGlobalSourceFile(location)) { + if (result = getSymbol(location.locals, name, meaning)) { + var useResult = true; + if (ts.isFunctionLike(location) && lastLocation && lastLocation !== location.body) { + // symbol lookup restrictions for function-like declarations + // - Type parameters of a function are in scope in the entire function declaration, including the parameter + // list and return type. However, local types are only in scope in the function body. + // - parameters are only in the scope of function body + // This restriction does not apply to JSDoc comment types because they are parented + // at a higher level than type parameters would normally be + if (meaning & result.flags & 793064 /* Type */ && lastLocation.kind !== 273 /* JSDocComment */) { + useResult = result.flags & 262144 /* TypeParameter */ + ? lastLocation === location.type || + lastLocation.kind === 142 /* Parameter */ || + lastLocation.kind === 141 /* TypeParameter */ + : false; + } + if (meaning & 107455 /* Value */ && result.flags & 1 /* FunctionScopedVariable */) { + // parameters are visible only inside function body, parameter list and return type + // technically for parameter list case here we might mix parameters and variables declared in function, + // however it is detected separately when checking initializers of parameters + // to make sure that they reference no variables declared after them. + useResult = + lastLocation.kind === 142 /* Parameter */ || + (lastLocation === location.type && + result.valueDeclaration.kind === 142 /* Parameter */); + } + } + if (useResult) { + break loop; + } + else { + result = undefined; + } } } - switch (location_1.kind) { + switch (location.kind) { case 256 /* SourceFile */: - if (!ts.isExternalOrCommonJsModule(location_1)) { + if (!ts.isExternalOrCommonJsModule(location)) break; - } + isInExternalModule = true; case 225 /* ModuleDeclaration */: - if (result = callback(getSymbolOfNode(location_1).exports)) { - return result; - } - break; - } - } - return callback(globals); - } - function getQualifiedLeftMeaning(rightMeaning) { - // If we are looking in value space, the parent meaning is value, other wise it is namespace - return rightMeaning === 107455 /* Value */ ? 107455 /* Value */ : 1920 /* Namespace */; - } - function getAccessibleSymbolChain(symbol, enclosingDeclaration, meaning, useOnlyExternalAliasing) { - function getAccessibleSymbolChainFromSymbolTable(symbols) { - function canQualifySymbol(symbolFromSymbolTable, meaning) { - // If the symbol is equivalent and doesn't need further qualification, this symbol is accessible - if (!needsQualification(symbolFromSymbolTable, enclosingDeclaration, meaning)) { - return true; - } - // If symbol needs qualification, make sure that parent is accessible, if it is then this symbol is accessible too - var accessibleParent = getAccessibleSymbolChain(symbolFromSymbolTable.parent, enclosingDeclaration, getQualifiedLeftMeaning(meaning), useOnlyExternalAliasing); - return !!accessibleParent; - } - function isAccessible(symbolFromSymbolTable, resolvedAliasSymbol) { - if (symbol === (resolvedAliasSymbol || symbolFromSymbolTable)) { - // if the symbolFromSymbolTable is not external module (it could be if it was determined as ambient external module and would be in globals table) - // and if symbolFromSymbolTable or alias resolution matches the symbol, - // check the symbol can be qualified, it is only then this symbol is accessible - return !ts.forEach(symbolFromSymbolTable.declarations, hasExternalModuleSymbol) && - canQualifySymbol(symbolFromSymbolTable, meaning); - } - } - // If symbol is directly available by its name in the symbol table - if (isAccessible(symbols[symbol.name])) { - return [symbol]; - } - // Check if symbol is any of the alias - return ts.forEachProperty(symbols, function (symbolFromSymbolTable) { - if (symbolFromSymbolTable.flags & 8388608 /* Alias */ - && symbolFromSymbolTable.name !== "export=" - && !ts.getDeclarationOfKind(symbolFromSymbolTable, 238 /* ExportSpecifier */)) { - if (!useOnlyExternalAliasing || - // Is this external alias, then use it to name - ts.forEach(symbolFromSymbolTable.declarations, ts.isExternalModuleImportEqualsDeclaration)) { - var resolvedImportedSymbol = resolveAlias(symbolFromSymbolTable); - if (isAccessible(symbolFromSymbolTable, resolveAlias(symbolFromSymbolTable))) { - return [symbolFromSymbolTable]; + var moduleExports = getSymbolOfNode(location).exports; + if (location.kind === 256 /* SourceFile */ || ts.isAmbientModule(location)) { + // It's an external module. First see if the module has an export default and if the local + // name of that export default matches. + if (result = moduleExports["default"]) { + var localSymbol = ts.getLocalSymbolForExportDefault(result); + if (localSymbol && (result.flags & meaning) && localSymbol.name === name) { + break loop; + } + result = undefined; } - // Look in the exported members, if we can find accessibleSymbolChain, symbol is accessible using this chain - // but only if the symbolFromSymbolTable can be qualified - var accessibleSymbolsFromExports = resolvedImportedSymbol.exports ? getAccessibleSymbolChainFromSymbolTable(resolvedImportedSymbol.exports) : undefined; - if (accessibleSymbolsFromExports && canQualifySymbol(symbolFromSymbolTable, getQualifiedLeftMeaning(meaning))) { - return [symbolFromSymbolTable].concat(accessibleSymbolsFromExports); + // Because of module/namespace merging, a module's exports are in scope, + // yet we never want to treat an export specifier as putting a member in scope. + // Therefore, if the name we find is purely an export specifier, it is not actually considered in scope. + // Two things to note about this: + // 1. We have to check this without calling getSymbol. The problem with calling getSymbol + // on an export specifier is that it might find the export specifier itself, and try to + // resolve it as an alias. This will cause the checker to consider the export specifier + // a circular alias reference when it might not be. + // 2. We check === SymbolFlags.Alias in order to check that the symbol is *purely* + // an alias. If we used &, we'd be throwing out symbols that have non alias aspects, + // which is not the desired behavior. + if (moduleExports[name] && + moduleExports[name].flags === 8388608 /* Alias */ && + ts.getDeclarationOfKind(moduleExports[name], 238 /* ExportSpecifier */)) { + break; } } - } - }); - } - if (symbol) { - if (!(isPropertyOrMethodDeclarationSymbol(symbol))) { - return forEachSymbolTableInScope(enclosingDeclaration, getAccessibleSymbolChainFromSymbolTable); + if (result = getSymbol(moduleExports, name, meaning & 8914931 /* ModuleMember */)) { + break loop; + } + break; + case 224 /* EnumDeclaration */: + if (result = getSymbol(getSymbolOfNode(location).exports, name, meaning & 8 /* EnumMember */)) { + break loop; + } + break; + case 145 /* PropertyDeclaration */: + case 144 /* PropertySignature */: + // TypeScript 1.0 spec (April 2014): 8.4.1 + // Initializer expressions for instance member variables are evaluated in the scope + // of the class constructor body but are not permitted to reference parameters or + // local variables of the constructor. This effectively means that entities from outer scopes + // by the same name as a constructor parameter or local variable are inaccessible + // in initializer expressions for instance member variables. + if (ts.isClassLike(location.parent) && !(ts.getModifierFlags(location) & 32 /* Static */)) { + var ctor = findConstructorDeclaration(location.parent); + if (ctor && ctor.locals) { + if (getSymbol(ctor.locals, name, meaning & 107455 /* Value */)) { + // Remember the property node, it will be used later to report appropriate error + propertyWithInvalidInitializer = location; + } + } + } + break; + case 221 /* ClassDeclaration */: + case 192 /* ClassExpression */: + case 222 /* InterfaceDeclaration */: + if (result = getSymbol(getSymbolOfNode(location).members, name, meaning & 793064 /* Type */)) { + if (lastLocation && ts.getModifierFlags(lastLocation) & 32 /* Static */) { + // TypeScript 1.0 spec (April 2014): 3.4.1 + // The scope of a type parameter extends over the entire declaration with which the type + // parameter list is associated, with the exception of static member declarations in classes. + error(errorLocation, ts.Diagnostics.Static_members_cannot_reference_class_type_parameters); + return undefined; + } + break loop; + } + if (location.kind === 192 /* ClassExpression */ && meaning & 32 /* Class */) { + var className = location.name; + if (className && name === className.text) { + result = location.symbol; + break loop; + } + } + break; + // It is not legal to reference a class's own type parameters from a computed property name that + // belongs to the class. For example: + // + // function foo() { return '' } + // class C { // <-- Class's own type parameter T + // [foo()]() { } // <-- Reference to T from class's own computed property + // } + // + case 140 /* ComputedPropertyName */: + grandparent = location.parent.parent; + if (ts.isClassLike(grandparent) || grandparent.kind === 222 /* InterfaceDeclaration */) { + // A reference to this grandparent's type parameters would be an error + if (result = getSymbol(getSymbolOfNode(grandparent).members, name, meaning & 793064 /* Type */)) { + error(errorLocation, ts.Diagnostics.A_computed_property_name_cannot_reference_a_type_parameter_from_its_containing_type); + return undefined; + } + } + break; + case 147 /* MethodDeclaration */: + case 146 /* MethodSignature */: + case 148 /* Constructor */: + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + case 220 /* FunctionDeclaration */: + case 180 /* ArrowFunction */: + if (meaning & 3 /* Variable */ && name === "arguments") { + result = argumentsSymbol; + break loop; + } + break; + case 179 /* FunctionExpression */: + if (meaning & 3 /* Variable */ && name === "arguments") { + result = argumentsSymbol; + break loop; + } + if (meaning & 16 /* Function */) { + var functionName = location.name; + if (functionName && name === functionName.text) { + result = location.symbol; + break loop; + } + } + break; + case 143 /* Decorator */: + // Decorators are resolved at the class declaration. Resolving at the parameter + // or member would result in looking up locals in the method. + // + // function y() {} + // class C { + // method(@y x, y) {} // <-- decorator y should be resolved at the class declaration, not the parameter. + // } + // + if (location.parent && location.parent.kind === 142 /* Parameter */) { + location = location.parent; + } + // + // function y() {} + // class C { + // @y method(x, y) {} // <-- decorator y should be resolved at the class declaration, not the method. + // } + // + if (location.parent && ts.isClassElement(location.parent)) { + location = location.parent; + } + break; } + lastLocation = location; + location = location.parent; } - } - function needsQualification(symbol, enclosingDeclaration, meaning) { - var qualify = false; - forEachSymbolTableInScope(enclosingDeclaration, function (symbolTable) { - // If symbol of this name is not available in the symbol table we are ok - var symbolFromSymbolTable = symbolTable[symbol.name]; - if (!symbolFromSymbolTable) { - // Continue to the next symbol table - return false; - } - // If the symbol with this name is present it should refer to the symbol - if (symbolFromSymbolTable === symbol) { - // No need to qualify - return true; - } - // Qualify if the symbol from symbol table has same meaning as expected - symbolFromSymbolTable = (symbolFromSymbolTable.flags & 8388608 /* Alias */ && !ts.getDeclarationOfKind(symbolFromSymbolTable, 238 /* ExportSpecifier */)) ? resolveAlias(symbolFromSymbolTable) : symbolFromSymbolTable; - if (symbolFromSymbolTable.flags & meaning) { - qualify = true; - return true; - } - // Continue to the next symbol table - return false; - }); - return qualify; - } - function isPropertyOrMethodDeclarationSymbol(symbol) { - if (symbol.declarations && symbol.declarations.length) { - for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { - var declaration = _a[_i]; - switch (declaration.kind) { - case 145 /* PropertyDeclaration */: - case 147 /* MethodDeclaration */: - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - continue; - default: - return false; + if (result && nameNotFoundMessage && noUnusedIdentifiers) { + result.isReferenced = true; + } + if (!result) { + result = getSymbol(globals, name, meaning); + } + if (!result) { + if (nameNotFoundMessage) { + if (!errorLocation || + !checkAndReportErrorForMissingPrefix(errorLocation, name, nameArg) && + !checkAndReportErrorForExtendingInterface(errorLocation) && + !checkAndReportErrorForUsingTypeAsValue(errorLocation, name, meaning)) { + error(errorLocation, nameNotFoundMessage, typeof nameArg === "string" ? nameArg : ts.declarationNameToString(nameArg)); } } - return true; + return undefined; } - return false; - } - /** - * Check if the given symbol in given enclosing declaration is accessible and mark all associated alias to be visible if requested - * - * @param symbol a Symbol to check if accessible - * @param enclosingDeclaration a Node containing reference to the symbol - * @param meaning a SymbolFlags to check if such meaning of the symbol is accessible - * @param shouldComputeAliasToMakeVisible a boolean value to indicate whether to return aliases to be mark visible in case the symbol is accessible - */ - function isSymbolAccessible(symbol, enclosingDeclaration, meaning, shouldComputeAliasesToMakeVisible) { - if (symbol && enclosingDeclaration && !(symbol.flags & 262144 /* TypeParameter */)) { - var initialSymbol = symbol; - var meaningToLook = meaning; - while (symbol) { - // Symbol is accessible if it by itself is accessible - var accessibleSymbolChain = getAccessibleSymbolChain(symbol, enclosingDeclaration, meaningToLook, /*useOnlyExternalAliasing*/ false); - if (accessibleSymbolChain) { - var hasAccessibleDeclarations = hasVisibleDeclarations(accessibleSymbolChain[0], shouldComputeAliasesToMakeVisible); - if (!hasAccessibleDeclarations) { - return { - accessibility: 1 /* NotAccessible */, - errorSymbolName: symbolToString(initialSymbol, enclosingDeclaration, meaning), - errorModuleName: symbol !== initialSymbol ? symbolToString(symbol, enclosingDeclaration, 1920 /* Namespace */) : undefined - }; - } - return hasAccessibleDeclarations; - } - // If we haven't got the accessible symbol, it doesn't mean the symbol is actually inaccessible. - // It could be a qualified symbol and hence verify the path - // e.g.: - // module m { - // export class c { - // } - // } - // const x: typeof m.c - // In the above example when we start with checking if typeof m.c symbol is accessible, - // we are going to see if c can be accessed in scope directly. - // But it can't, hence the accessible is going to be undefined, but that doesn't mean m.c is inaccessible - // It is accessible if the parent m is accessible because then m.c can be accessed through qualification - meaningToLook = getQualifiedLeftMeaning(meaning); - symbol = getParentOfSymbol(symbol); + // Perform extra checks only if error reporting was requested + if (nameNotFoundMessage) { + if (propertyWithInvalidInitializer) { + // We have a match, but the reference occurred within a property initializer and the identifier also binds + // to a local variable in the constructor where the code will be emitted. + var propertyName = propertyWithInvalidInitializer.name; + error(errorLocation, ts.Diagnostics.Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor, ts.declarationNameToString(propertyName), typeof nameArg === "string" ? nameArg : ts.declarationNameToString(nameArg)); + return undefined; } - // This could be a symbol that is not exported in the external module - // or it could be a symbol from different external module that is not aliased and hence cannot be named - var symbolExternalModule = ts.forEach(initialSymbol.declarations, getExternalModuleContainer); - if (symbolExternalModule) { - var enclosingExternalModule = getExternalModuleContainer(enclosingDeclaration); - if (symbolExternalModule !== enclosingExternalModule) { - // name from different external module that is not visible - return { - accessibility: 2 /* CannotBeNamed */, - errorSymbolName: symbolToString(initialSymbol, enclosingDeclaration, meaning), - errorModuleName: symbolToString(symbolExternalModule) - }; + // Only check for block-scoped variable if we are looking for the + // name with variable meaning + // For example, + // declare module foo { + // interface bar {} + // } + // const foo/*1*/: foo/*2*/.bar; + // The foo at /*1*/ and /*2*/ will share same symbol with two meaning + // block - scope variable and namespace module. However, only when we + // try to resolve name in /*1*/ which is used in variable position, + // we want to check for block- scoped + if (meaning & 2 /* BlockScopedVariable */) { + var exportOrLocalSymbol = getExportSymbolOfValueSymbolIfExported(result); + if (exportOrLocalSymbol.flags & 2 /* BlockScopedVariable */) { + checkResolvedBlockScopedVariable(exportOrLocalSymbol, errorLocation); } } - // Just a local name that is not accessible - return { - accessibility: 1 /* NotAccessible */, - errorSymbolName: symbolToString(initialSymbol, enclosingDeclaration, meaning) - }; - } - return { accessibility: 0 /* Accessible */ }; - function getExternalModuleContainer(declaration) { - for (; declaration; declaration = declaration.parent) { - if (hasExternalModuleSymbol(declaration)) { - return getSymbolOfNode(declaration); + // If we're in an external module, we can't reference value symbols created from UMD export declarations + if (result && isInExternalModule && (meaning & 107455 /* Value */) === 107455 /* Value */) { + var decls = result.declarations; + if (decls && decls.length === 1 && decls[0].kind === 228 /* NamespaceExportDeclaration */) { + error(errorLocation, ts.Diagnostics._0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead, name); } } } + return result; } - function hasExternalModuleSymbol(declaration) { - return ts.isAmbientModule(declaration) || (declaration.kind === 256 /* SourceFile */ && ts.isExternalOrCommonJsModule(declaration)); - } - function hasVisibleDeclarations(symbol, shouldComputeAliasToMakeVisible) { - var aliasesToMakeVisible; - if (ts.forEach(symbol.declarations, function (declaration) { return !getIsDeclarationVisible(declaration); })) { - return undefined; + function checkAndReportErrorForMissingPrefix(errorLocation, name, nameArg) { + if ((errorLocation.kind === 69 /* Identifier */ && (isTypeReferenceIdentifier(errorLocation)) || isInTypeQuery(errorLocation))) { + return false; } - return { accessibility: 0 /* Accessible */, aliasesToMakeVisible: aliasesToMakeVisible }; - function getIsDeclarationVisible(declaration) { - if (!isDeclarationVisible(declaration)) { - // Mark the unexported alias as visible if its parent is visible - // because these kind of aliases can be used to name types in declaration file - var anyImportSyntax = getAnyImportSyntax(declaration); - if (anyImportSyntax && - !(ts.getModifierFlags(anyImportSyntax) & 1 /* Export */) && - isDeclarationVisible(anyImportSyntax.parent)) { - // In function "buildTypeDisplay" where we decide whether to write type-alias or serialize types, - // we want to just check if type- alias is accessible or not but we don't care about emitting those alias at that time - // since we will do the emitting later in trackSymbol. - if (shouldComputeAliasToMakeVisible) { - getNodeLinks(declaration).isVisible = true; - if (aliasesToMakeVisible) { - if (!ts.contains(aliasesToMakeVisible, anyImportSyntax)) { - aliasesToMakeVisible.push(anyImportSyntax); - } - } - else { - aliasesToMakeVisible = [anyImportSyntax]; - } - } + var container = ts.getThisContainer(errorLocation, /* includeArrowFunctions */ true); + var location = container; + while (location) { + if (ts.isClassLike(location.parent)) { + var classSymbol = getSymbolOfNode(location.parent); + if (!classSymbol) { + break; + } + // Check to see if a static member exists. + var constructorType = getTypeOfSymbol(classSymbol); + if (getPropertyOfType(constructorType, name)) { + error(errorLocation, ts.Diagnostics.Cannot_find_name_0_Did_you_mean_the_static_member_1_0, typeof nameArg === "string" ? nameArg : ts.declarationNameToString(nameArg), symbolToString(classSymbol)); return true; } - // Declaration is not visible - return false; + // No static member is present. + // Check if we're in an instance method and look for a relevant instance member. + if (location === container && !(ts.getModifierFlags(location) & 32 /* Static */)) { + var instanceType = getDeclaredTypeOfSymbol(classSymbol).thisType; + if (getPropertyOfType(instanceType, name)) { + error(errorLocation, ts.Diagnostics.Cannot_find_name_0_Did_you_mean_the_instance_member_this_0, typeof nameArg === "string" ? nameArg : ts.declarationNameToString(nameArg)); + return true; + } + } } - return true; + location = location.parent; } + return false; } - function isEntityNameVisible(entityName, enclosingDeclaration) { - // get symbol of the first identifier of the entityName - var meaning; - if (entityName.parent.kind === 158 /* TypeQuery */ || ts.isExpressionWithTypeArgumentsInClassExtendsClause(entityName.parent)) { - // Typeof value - meaning = 107455 /* Value */ | 1048576 /* ExportValue */; + function checkAndReportErrorForExtendingInterface(errorLocation) { + var expression = getEntityNameForExtendingInterface(errorLocation); + var isError = !!(expression && resolveEntityName(expression, 64 /* Interface */, /*ignoreErrors*/ true)); + if (isError) { + error(errorLocation, ts.Diagnostics.Cannot_extend_an_interface_0_Did_you_mean_implements, ts.getTextOfNode(expression)); } - else if (entityName.kind === 139 /* QualifiedName */ || entityName.kind === 172 /* PropertyAccessExpression */ || - entityName.parent.kind === 229 /* ImportEqualsDeclaration */) { - // Left identifier from type reference or TypeAlias - // Entity name of the import declaration - meaning = 1920 /* Namespace */; + return isError; + } + /** + * Climbs up parents to an ExpressionWithTypeArguments, and returns its expression, + * but returns undefined if that expression is not an EntityNameExpression. + */ + function getEntityNameForExtendingInterface(node) { + switch (node.kind) { + case 69 /* Identifier */: + case 172 /* PropertyAccessExpression */: + return node.parent ? getEntityNameForExtendingInterface(node.parent) : undefined; + case 194 /* ExpressionWithTypeArguments */: + ts.Debug.assert(ts.isEntityNameExpression(node.expression)); + return node.expression; + default: + return undefined; } - else { - // Type Reference or TypeAlias entity = Identifier - meaning = 793064 /* Type */; + } + function checkAndReportErrorForUsingTypeAsValue(errorLocation, name, meaning) { + if (meaning & (107455 /* Value */ & ~1024 /* NamespaceModule */)) { + var symbol = resolveSymbol(resolveName(errorLocation, name, 793064 /* Type */ & ~107455 /* Value */, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined)); + if (symbol && !(symbol.flags & 1024 /* NamespaceModule */)) { + error(errorLocation, ts.Diagnostics._0_only_refers_to_a_type_but_is_being_used_as_a_value_here, name); + return true; + } } - var firstIdentifier = getFirstIdentifier(entityName); - var symbol = resolveName(enclosingDeclaration, firstIdentifier.text, meaning, /*nodeNotFoundErrorMessage*/ undefined, /*nameArg*/ undefined); - // Verify if the symbol is accessible - return (symbol && hasVisibleDeclarations(symbol, /*shouldComputeAliasToMakeVisible*/ true)) || { - accessibility: 1 /* NotAccessible */, - errorSymbolName: ts.getTextOfNode(firstIdentifier), - errorNode: firstIdentifier - }; + return false; } - function writeKeyword(writer, kind) { - writer.writeKeyword(ts.tokenToString(kind)); + function checkResolvedBlockScopedVariable(result, errorLocation) { + ts.Debug.assert((result.flags & 2 /* BlockScopedVariable */) !== 0); + // Block-scoped variables cannot be used before their definition + var declaration = ts.forEach(result.declarations, function (d) { return ts.isBlockOrCatchScoped(d) ? d : undefined; }); + ts.Debug.assert(declaration !== undefined, "Block-scoped variable declaration is undefined"); + if (!ts.isInAmbientContext(declaration) && !isBlockScopedNameDeclaredBeforeUse(ts.getAncestor(declaration, 218 /* VariableDeclaration */), errorLocation)) { + error(errorLocation, ts.Diagnostics.Block_scoped_variable_0_used_before_its_declaration, ts.declarationNameToString(declaration.name)); + } } - function writePunctuation(writer, kind) { - writer.writePunctuation(ts.tokenToString(kind)); + /* Starting from 'initial' node walk up the parent chain until 'stopAt' node is reached. + * If at any point current node is equal to 'parent' node - return true. + * Return false if 'stopAt' node is reached or isFunctionLike(current) === true. + */ + function isSameScopeDescendentOf(initial, parent, stopAt) { + if (!parent) { + return false; + } + for (var current = initial; current && current !== stopAt && !ts.isFunctionLike(current); current = current.parent) { + if (current === parent) { + return true; + } + } + return false; } - function writeSpace(writer) { - writer.writeSpace(" "); + function getAnyImportSyntax(node) { + if (ts.isAliasSymbolDeclaration(node)) { + if (node.kind === 229 /* ImportEqualsDeclaration */) { + return node; + } + while (node && node.kind !== 230 /* ImportDeclaration */) { + node = node.parent; + } + return node; + } } - function symbolToString(symbol, enclosingDeclaration, meaning) { - var writer = ts.getSingleLineStringWriter(); - getSymbolDisplayBuilder().buildSymbolDisplay(symbol, writer, enclosingDeclaration, meaning); - var result = writer.string(); - ts.releaseStringWriter(writer); - return result; + function getDeclarationOfAliasSymbol(symbol) { + return ts.forEach(symbol.declarations, function (d) { return ts.isAliasSymbolDeclaration(d) ? d : undefined; }); } - function signatureToString(signature, enclosingDeclaration, flags, kind) { - var writer = ts.getSingleLineStringWriter(); - getSymbolDisplayBuilder().buildSignatureDisplay(signature, writer, enclosingDeclaration, flags, kind); - var result = writer.string(); - ts.releaseStringWriter(writer); - return result; + function getTargetOfImportEqualsDeclaration(node) { + if (node.moduleReference.kind === 240 /* ExternalModuleReference */) { + return resolveExternalModuleSymbol(resolveExternalModuleName(node, ts.getExternalModuleImportEqualsDeclarationExpression(node))); + } + return getSymbolOfPartOfRightHandSideOfImportEquals(node.moduleReference, node); } - function typeToString(type, enclosingDeclaration, flags) { - var writer = ts.getSingleLineStringWriter(); - getSymbolDisplayBuilder().buildTypeDisplay(type, writer, enclosingDeclaration, flags); - var result = writer.string(); - ts.releaseStringWriter(writer); - var maxLength = compilerOptions.noErrorTruncation || flags & 4 /* NoTruncation */ ? undefined : 100; - if (maxLength && result.length >= maxLength) { - result = result.substr(0, maxLength - "...".length) + "..."; + function getTargetOfImportClause(node) { + var moduleSymbol = resolveExternalModuleName(node, node.parent.moduleSpecifier); + if (moduleSymbol) { + var exportDefaultSymbol = ts.isShorthandAmbientModuleSymbol(moduleSymbol) ? + moduleSymbol : + moduleSymbol.exports["export="] ? + getPropertyOfType(getTypeOfSymbol(moduleSymbol.exports["export="]), "default") : + resolveSymbol(moduleSymbol.exports["default"]); + if (!exportDefaultSymbol && !allowSyntheticDefaultImports) { + error(node.name, ts.Diagnostics.Module_0_has_no_default_export, symbolToString(moduleSymbol)); + } + else if (!exportDefaultSymbol && allowSyntheticDefaultImports) { + return resolveExternalModuleSymbol(moduleSymbol) || resolveSymbol(moduleSymbol); + } + return exportDefaultSymbol; } - return result; } - function typePredicateToString(typePredicate, enclosingDeclaration, flags) { - var writer = ts.getSingleLineStringWriter(); - getSymbolDisplayBuilder().buildTypePredicateDisplay(typePredicate, writer, enclosingDeclaration, flags); - var result = writer.string(); - ts.releaseStringWriter(writer); + function getTargetOfNamespaceImport(node) { + var moduleSpecifier = node.parent.parent.moduleSpecifier; + return resolveESModuleSymbol(resolveExternalModuleName(node, moduleSpecifier), moduleSpecifier); + } + // This function creates a synthetic symbol that combines the value side of one symbol with the + // type/namespace side of another symbol. Consider this example: + // + // declare module graphics { + // interface Point { + // x: number; + // y: number; + // } + // } + // declare var graphics: { + // Point: new (x: number, y: number) => graphics.Point; + // } + // declare module "graphics" { + // export = graphics; + // } + // + // An 'import { Point } from "graphics"' needs to create a symbol that combines the value side 'Point' + // property with the type/namespace side interface 'Point'. + function combineValueAndTypeSymbols(valueSymbol, typeSymbol) { + if (valueSymbol.flags & (793064 /* Type */ | 1920 /* Namespace */)) { + return valueSymbol; + } + var result = createSymbol(valueSymbol.flags | typeSymbol.flags, valueSymbol.name); + result.declarations = ts.concatenate(valueSymbol.declarations, typeSymbol.declarations); + result.parent = valueSymbol.parent || typeSymbol.parent; + if (valueSymbol.valueDeclaration) + result.valueDeclaration = valueSymbol.valueDeclaration; + if (typeSymbol.members) + result.members = typeSymbol.members; + if (valueSymbol.exports) + result.exports = valueSymbol.exports; return result; } - function formatUnionTypes(types) { - var result = []; - var flags = 0; - for (var i = 0; i < types.length; i++) { - var t = types[i]; - flags |= t.flags; - if (!(t.flags & 6144 /* Nullable */)) { - if (t.flags & (128 /* BooleanLiteral */ | 256 /* EnumLiteral */)) { - var baseType = t.flags & 128 /* BooleanLiteral */ ? booleanType : t.baseType; - var count = baseType.types.length; - if (i + count <= types.length && types[i + count - 1] === baseType.types[count - 1]) { - result.push(baseType); - i += count - 1; - continue; - } + function getExportOfModule(symbol, name) { + if (symbol.flags & 1536 /* Module */) { + var exportedSymbol = getExportsOfSymbol(symbol)[name]; + if (exportedSymbol) { + return resolveSymbol(exportedSymbol); + } + } + } + function getPropertyOfVariable(symbol, name) { + if (symbol.flags & 3 /* Variable */) { + var typeAnnotation = symbol.valueDeclaration.type; + if (typeAnnotation) { + return resolveSymbol(getPropertyOfType(getTypeFromTypeNode(typeAnnotation), name)); + } + } + } + function getExternalModuleMember(node, specifier) { + var moduleSymbol = resolveExternalModuleName(node, node.moduleSpecifier); + var targetSymbol = resolveESModuleSymbol(moduleSymbol, node.moduleSpecifier); + if (targetSymbol) { + var name_13 = specifier.propertyName || specifier.name; + if (name_13.text) { + if (ts.isShorthandAmbientModuleSymbol(moduleSymbol)) { + return moduleSymbol; } - result.push(t); + var symbolFromVariable = void 0; + // First check if module was specified with "export=". If so, get the member from the resolved type + if (moduleSymbol && moduleSymbol.exports && moduleSymbol.exports["export="]) { + symbolFromVariable = getPropertyOfType(getTypeOfSymbol(targetSymbol), name_13.text); + } + else { + symbolFromVariable = getPropertyOfVariable(targetSymbol, name_13.text); + } + // if symbolFromVariable is export - get its final target + symbolFromVariable = resolveSymbol(symbolFromVariable); + var symbolFromModule = getExportOfModule(targetSymbol, name_13.text); + // If the export member we're looking for is default, and there is no real default but allowSyntheticDefaultImports is on, return the entire module as the default + if (!symbolFromModule && allowSyntheticDefaultImports && name_13.text === "default") { + symbolFromModule = resolveExternalModuleSymbol(moduleSymbol) || resolveSymbol(moduleSymbol); + } + var symbol = symbolFromModule && symbolFromVariable ? + combineValueAndTypeSymbols(symbolFromVariable, symbolFromModule) : + symbolFromModule || symbolFromVariable; + if (!symbol) { + error(name_13, ts.Diagnostics.Module_0_has_no_exported_member_1, getFullyQualifiedName(moduleSymbol), ts.declarationNameToString(name_13)); + } + return symbol; } } - if (flags & 4096 /* Null */) - result.push(nullType); - if (flags & 2048 /* Undefined */) - result.push(undefinedType); - return result || types; } - function visibilityToString(flags) { - if (flags === 8 /* Private */) { - return "private"; + function getTargetOfImportSpecifier(node) { + return getExternalModuleMember(node.parent.parent.parent, node); + } + function getTargetOfNamespaceExportDeclaration(node) { + return resolveExternalModuleSymbol(node.parent.symbol); + } + function getTargetOfExportSpecifier(node) { + return node.parent.parent.moduleSpecifier ? + getExternalModuleMember(node.parent.parent, node) : + resolveEntityName(node.propertyName || node.name, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */); + } + function getTargetOfExportAssignment(node) { + return resolveEntityName(node.expression, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */); + } + function getTargetOfAliasDeclaration(node) { + switch (node.kind) { + case 229 /* ImportEqualsDeclaration */: + return getTargetOfImportEqualsDeclaration(node); + case 231 /* ImportClause */: + return getTargetOfImportClause(node); + case 232 /* NamespaceImport */: + return getTargetOfNamespaceImport(node); + case 234 /* ImportSpecifier */: + return getTargetOfImportSpecifier(node); + case 238 /* ExportSpecifier */: + return getTargetOfExportSpecifier(node); + case 235 /* ExportAssignment */: + return getTargetOfExportAssignment(node); + case 228 /* NamespaceExportDeclaration */: + return getTargetOfNamespaceExportDeclaration(node); } - if (flags === 16 /* Protected */) { - return "protected"; + } + function resolveSymbol(symbol) { + return symbol && symbol.flags & 8388608 /* Alias */ && !(symbol.flags & (107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */)) ? resolveAlias(symbol) : symbol; + } + function resolveAlias(symbol) { + ts.Debug.assert((symbol.flags & 8388608 /* Alias */) !== 0, "Should only get Alias here."); + var links = getSymbolLinks(symbol); + if (!links.target) { + links.target = resolvingSymbol; + var node = getDeclarationOfAliasSymbol(symbol); + ts.Debug.assert(!!node); + var target = getTargetOfAliasDeclaration(node); + if (links.target === resolvingSymbol) { + links.target = target || unknownSymbol; + } + else { + error(node, ts.Diagnostics.Circular_definition_of_import_alias_0, symbolToString(symbol)); + } } - return "public"; + else if (links.target === resolvingSymbol) { + links.target = unknownSymbol; + } + return links.target; } - function getTypeAliasForTypeLiteral(type) { - if (type.symbol && type.symbol.flags & 2048 /* TypeLiteral */) { - var node = type.symbol.declarations[0].parent; - while (node.kind === 164 /* ParenthesizedType */) { - node = node.parent; + function markExportAsReferenced(node) { + var symbol = getSymbolOfNode(node); + var target = resolveAlias(symbol); + if (target) { + var markAlias = target === unknownSymbol || + ((target.flags & 107455 /* Value */) && !isConstEnumOrConstEnumOnlyModule(target)); + if (markAlias) { + markAliasSymbolAsReferenced(symbol); } - if (node.kind === 223 /* TypeAliasDeclaration */) { - return getSymbolOfNode(node); + } + } + // When an alias symbol is referenced, we need to mark the entity it references as referenced and in turn repeat that until + // we reach a non-alias or an exported entity (which is always considered referenced). We do this by checking the target of + // the alias as an expression (which recursively takes us back here if the target references another alias). + function markAliasSymbolAsReferenced(symbol) { + var links = getSymbolLinks(symbol); + if (!links.referenced) { + links.referenced = true; + var node = getDeclarationOfAliasSymbol(symbol); + ts.Debug.assert(!!node); + if (node.kind === 235 /* ExportAssignment */) { + // export default + checkExpressionCached(node.expression); + } + else if (node.kind === 238 /* ExportSpecifier */) { + // export { } or export { as foo } + checkExpressionCached(node.propertyName || node.name); + } + else if (ts.isInternalModuleImportEqualsDeclaration(node)) { + // import foo = + checkExpressionCached(node.moduleReference); } } - return undefined; } - function isTopLevelInExternalModuleAugmentation(node) { - return node && node.parent && - node.parent.kind === 226 /* ModuleBlock */ && - ts.isExternalModuleAugmentation(node.parent.parent); + // This function is only for imports with entity names + function getSymbolOfPartOfRightHandSideOfImportEquals(entityName, importDeclaration, dontResolveAlias) { + // There are three things we might try to look for. In the following examples, + // the search term is enclosed in |...|: + // + // import a = |b|; // Namespace + // import a = |b.c|; // Value, type, namespace + // import a = |b.c|.d; // Namespace + if (entityName.kind === 69 /* Identifier */ && ts.isRightSideOfQualifiedNameOrPropertyAccess(entityName)) { + entityName = entityName.parent; + } + // Check for case 1 and 3 in the above example + if (entityName.kind === 69 /* Identifier */ || entityName.parent.kind === 139 /* QualifiedName */) { + return resolveEntityName(entityName, 1920 /* Namespace */, /*ignoreErrors*/ false, dontResolveAlias); + } + else { + // Case 2 in above example + // entityName.kind could be a QualifiedName or a Missing identifier + ts.Debug.assert(entityName.parent.kind === 229 /* ImportEqualsDeclaration */); + return resolveEntityName(entityName, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */, /*ignoreErrors*/ false, dontResolveAlias); + } } - function literalTypeToString(type) { - return type.flags & 32 /* StringLiteral */ ? "\"" + ts.escapeString(type.text) + "\"" : type.text; + function getFullyQualifiedName(symbol) { + return symbol.parent ? getFullyQualifiedName(symbol.parent) + "." + symbolToString(symbol) : symbolToString(symbol); } - function getSymbolDisplayBuilder() { - function getNameOfSymbol(symbol) { - if (symbol.declarations && symbol.declarations.length) { - var declaration = symbol.declarations[0]; - if (declaration.name) { - return ts.declarationNameToString(declaration.name); - } - switch (declaration.kind) { - case 192 /* ClassExpression */: - return "(Anonymous class)"; - case 179 /* FunctionExpression */: - case 180 /* ArrowFunction */: - return "(Anonymous function)"; - } - } - return symbol.name; + // Resolves a qualified name and any involved aliases + function resolveEntityName(name, meaning, ignoreErrors, dontResolveAlias, location) { + if (ts.nodeIsMissing(name)) { + return undefined; } - /** - * Writes only the name of the symbol out to the writer. Uses the original source text - * for the name of the symbol if it is available to match how the user wrote the name. - */ - function appendSymbolNameOnly(symbol, writer) { - writer.writeSymbol(getNameOfSymbol(symbol), symbol); + var symbol; + if (name.kind === 69 /* Identifier */) { + var message = meaning === 1920 /* Namespace */ ? ts.Diagnostics.Cannot_find_namespace_0 : ts.Diagnostics.Cannot_find_name_0; + symbol = resolveName(location || name, name.text, meaning, ignoreErrors ? undefined : message, name); + if (!symbol) { + return undefined; + } } - /** - * Writes a property access or element access with the name of the symbol out to the writer. - * Uses the original source text for the name of the symbol if it is available to match how the user wrote the name, - * ensuring that any names written with literals use element accesses. - */ - function appendPropertyOrElementAccessForSymbol(symbol, writer) { - var symbolName = getNameOfSymbol(symbol); - var firstChar = symbolName.charCodeAt(0); - var needsElementAccess = !ts.isIdentifierStart(firstChar, languageVersion); - if (needsElementAccess) { - writePunctuation(writer, 19 /* OpenBracketToken */); - if (ts.isSingleOrDoubleQuote(firstChar)) { - writer.writeStringLiteral(symbolName); - } - else { - writer.writeSymbol(symbolName, symbol); + else if (name.kind === 139 /* QualifiedName */ || name.kind === 172 /* PropertyAccessExpression */) { + var left = name.kind === 139 /* QualifiedName */ ? name.left : name.expression; + var right = name.kind === 139 /* QualifiedName */ ? name.right : name.name; + var namespace = resolveEntityName(left, 1920 /* Namespace */, ignoreErrors, /*dontResolveAlias*/ false, location); + if (!namespace || ts.nodeIsMissing(right)) { + return undefined; + } + else if (namespace === unknownSymbol) { + return namespace; + } + symbol = getSymbol(getExportsOfSymbol(namespace), right.text, meaning); + if (!symbol) { + if (!ignoreErrors) { + error(right, ts.Diagnostics.Namespace_0_has_no_exported_member_1, getFullyQualifiedName(namespace), ts.declarationNameToString(right)); } - writePunctuation(writer, 20 /* CloseBracketToken */); + return undefined; } - else { - writePunctuation(writer, 21 /* DotToken */); - writer.writeSymbol(symbolName, symbol); + } + else { + ts.Debug.fail("Unknown entity name kind."); + } + ts.Debug.assert((symbol.flags & 16777216 /* Instantiated */) === 0, "Should never get an instantiated symbol here."); + return (symbol.flags & meaning) || dontResolveAlias ? symbol : resolveAlias(symbol); + } + function resolveExternalModuleName(location, moduleReferenceExpression) { + return resolveExternalModuleNameWorker(location, moduleReferenceExpression, ts.Diagnostics.Cannot_find_module_0); + } + function resolveExternalModuleNameWorker(location, moduleReferenceExpression, moduleNotFoundError) { + if (moduleReferenceExpression.kind !== 9 /* StringLiteral */) { + return; + } + var moduleReferenceLiteral = moduleReferenceExpression; + return resolveExternalModule(location, moduleReferenceLiteral.text, moduleNotFoundError, moduleReferenceLiteral); + } + function resolveExternalModule(location, moduleReference, moduleNotFoundError, errorNode) { + // Module names are escaped in our symbol table. However, string literal values aren't. + // Escape the name in the "require(...)" clause to ensure we find the right symbol. + var moduleName = ts.escapeIdentifier(moduleReference); + if (moduleName === undefined) { + return; + } + var isRelative = ts.isExternalModuleNameRelative(moduleName); + if (!isRelative) { + var symbol = getSymbol(globals, '"' + moduleName + '"', 512 /* ValueModule */); + if (symbol) { + // merged symbol is module declaration symbol combined with all augmentations + return getMergedSymbol(symbol); } } - /** - * Enclosing declaration is optional when we don't want to get qualified name in the enclosing declaration scope - * Meaning needs to be specified if the enclosing declaration is given - */ - function buildSymbolDisplay(symbol, writer, enclosingDeclaration, meaning, flags, typeFlags) { - var parentSymbol; - function appendParentTypeArgumentsAndSymbolName(symbol) { - if (parentSymbol) { - // Write type arguments of instantiated class/interface here - if (flags & 1 /* WriteTypeParametersOrArguments */) { - if (symbol.flags & 16777216 /* Instantiated */) { - buildDisplayForTypeArgumentsAndDelimiters(getTypeParametersOfClassOrInterface(parentSymbol), symbol.mapper, writer, enclosingDeclaration); - } - else { - buildTypeParameterDisplayFromSymbol(parentSymbol, writer, enclosingDeclaration); - } - } - appendPropertyOrElementAccessForSymbol(symbol, writer); - } - else { - appendSymbolNameOnly(symbol, writer); - } - parentSymbol = symbol; + var resolvedModule = ts.getResolvedModule(ts.getSourceFileOfNode(location), moduleReference); + var sourceFile = resolvedModule && host.getSourceFile(resolvedModule.resolvedFileName); + if (sourceFile) { + if (sourceFile.symbol) { + // merged symbol is module declaration symbol combined with all augmentations + return getMergedSymbol(sourceFile.symbol); } - // Let the writer know we just wrote out a symbol. The declaration emitter writer uses - // this to determine if an import it has previously seen (and not written out) needs - // to be written to the file once the walk of the tree is complete. - // - // NOTE(cyrusn): This approach feels somewhat unfortunate. A simple pass over the tree - // up front (for example, during checking) could determine if we need to emit the imports - // and we could then access that data during declaration emit. - writer.trackSymbol(symbol, enclosingDeclaration, meaning); - /** @param endOfChain Set to false for recursive calls; non-recursive calls should always output something. */ - function walkSymbol(symbol, meaning, endOfChain) { - var accessibleSymbolChain = getAccessibleSymbolChain(symbol, enclosingDeclaration, meaning, !!(flags & 2 /* UseOnlyExternalAliasing */)); - if (!accessibleSymbolChain || - needsQualification(accessibleSymbolChain[0], enclosingDeclaration, accessibleSymbolChain.length === 1 ? meaning : getQualifiedLeftMeaning(meaning))) { - // Go up and add our parent. - var parent_7 = getParentOfSymbol(accessibleSymbolChain ? accessibleSymbolChain[0] : symbol); - if (parent_7) { - walkSymbol(parent_7, getQualifiedLeftMeaning(meaning), /*endOfChain*/ false); - } - } - if (accessibleSymbolChain) { - for (var _i = 0, accessibleSymbolChain_1 = accessibleSymbolChain; _i < accessibleSymbolChain_1.length; _i++) { - var accessibleSymbol = accessibleSymbolChain_1[_i]; - appendParentTypeArgumentsAndSymbolName(accessibleSymbol); - } - } - else if ( - // If this is the last part of outputting the symbol, always output. The cases apply only to parent symbols. - endOfChain || - // If a parent symbol is an external module, don't write it. (We prefer just `x` vs `"foo/bar".x`.) - !(!parentSymbol && ts.forEach(symbol.declarations, hasExternalModuleSymbol)) && - // If a parent symbol is an anonymous type, don't write it. - !(symbol.flags & (2048 /* TypeLiteral */ | 4096 /* ObjectLiteral */))) { - appendParentTypeArgumentsAndSymbolName(symbol); - } + if (moduleNotFoundError) { + // report errors only if it was requested + error(errorNode, ts.Diagnostics.File_0_is_not_a_module, sourceFile.fileName); } - // Get qualified name if the symbol is not a type parameter - // and there is an enclosing declaration or we specifically - // asked for it - var isTypeParameter = symbol.flags & 262144 /* TypeParameter */; - var typeFormatFlag = 128 /* UseFullyQualifiedType */ & typeFlags; - if (!isTypeParameter && (enclosingDeclaration || typeFormatFlag)) { - walkSymbol(symbol, meaning, /*endOfChain*/ true); + return undefined; + } + if (patternAmbientModules) { + var pattern = ts.findBestPatternMatch(patternAmbientModules, function (_) { return _.pattern; }, moduleName); + if (pattern) { + return getMergedSymbol(pattern.symbol); + } + } + if (moduleNotFoundError) { + // report errors only if it was requested + var tsExtension = ts.tryExtractTypeScriptExtension(moduleName); + if (tsExtension) { + var diag = ts.Diagnostics.An_import_path_cannot_end_with_a_0_extension_Consider_importing_1_instead; + error(errorNode, diag, tsExtension, ts.removeExtension(moduleName, tsExtension)); } else { - appendParentTypeArgumentsAndSymbolName(symbol); + error(errorNode, moduleNotFoundError, moduleName); } } - function buildTypeDisplay(type, writer, enclosingDeclaration, globalFlags, symbolStack) { - var globalFlagsToPass = globalFlags & 16 /* WriteOwnNameForAnyLike */; - var inObjectTypeLiteral = false; - return writeType(type, globalFlags); - function writeType(type, flags) { - var nextFlags = flags & ~512 /* InTypeAlias */; - // Write undefined/null type as any - if (type.flags & 16015 /* Intrinsic */) { - // Special handling for unknown / resolving types, they should show up as any and not unknown or __resolving - writer.writeKeyword(!(globalFlags & 16 /* WriteOwnNameForAnyLike */) && isTypeAny(type) - ? "any" - : type.intrinsicName); - } - else if (type.flags & 268435456 /* ThisType */) { - if (inObjectTypeLiteral) { - writer.reportInaccessibleThisError(); - } - writer.writeKeyword("this"); - } - else if (type.flags & 131072 /* Reference */) { - writeTypeReference(type, nextFlags); - } - else if (type.flags & 256 /* EnumLiteral */) { - buildSymbolDisplay(getParentOfSymbol(type.symbol), writer, enclosingDeclaration, 793064 /* Type */, 0 /* None */, nextFlags); - writePunctuation(writer, 21 /* DotToken */); - appendSymbolNameOnly(type.symbol, writer); - } - else if (type.flags & (32768 /* Class */ | 65536 /* Interface */ | 16 /* Enum */ | 16384 /* TypeParameter */)) { - // The specified symbol flags need to be reinterpreted as type flags - buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, 793064 /* Type */, 0 /* None */, nextFlags); - } - else if (!(flags & 512 /* InTypeAlias */) && type.flags & (2097152 /* Anonymous */ | 1572864 /* UnionOrIntersection */) && type.aliasSymbol && - isSymbolAccessible(type.aliasSymbol, enclosingDeclaration, 793064 /* Type */, /*shouldComputeAliasesToMakeVisible*/ false).accessibility === 0 /* Accessible */) { - // Only write out inferred type with its corresponding type-alias if type-alias is visible - var typeArguments = type.aliasTypeArguments; - writeSymbolTypeReference(type.aliasSymbol, typeArguments, 0, typeArguments ? typeArguments.length : 0, nextFlags); - } - else if (type.flags & 1572864 /* UnionOrIntersection */) { - writeUnionOrIntersectionType(type, nextFlags); - } - else if (type.flags & 2097152 /* Anonymous */) { - writeAnonymousType(type, nextFlags); + return undefined; + } + // An external module with an 'export =' declaration resolves to the target of the 'export =' declaration, + // and an external module with no 'export =' declaration resolves to the module itself. + function resolveExternalModuleSymbol(moduleSymbol) { + return moduleSymbol && getMergedSymbol(resolveSymbol(moduleSymbol.exports["export="])) || moduleSymbol; + } + // An external module with an 'export =' declaration may be referenced as an ES6 module provided the 'export =' + // references a symbol that is at least declared as a module or a variable. The target of the 'export =' may + // combine other declarations with the module or variable (e.g. a class/module, function/module, interface/variable). + function resolveESModuleSymbol(moduleSymbol, moduleReferenceExpression) { + var symbol = resolveExternalModuleSymbol(moduleSymbol); + if (symbol && !(symbol.flags & (1536 /* Module */ | 3 /* Variable */))) { + error(moduleReferenceExpression, ts.Diagnostics.Module_0_resolves_to_a_non_module_entity_and_cannot_be_imported_using_this_construct, symbolToString(moduleSymbol)); + symbol = undefined; + } + return symbol; + } + function hasExportAssignmentSymbol(moduleSymbol) { + return moduleSymbol.exports["export="] !== undefined; + } + function getExportsOfModuleAsArray(moduleSymbol) { + return symbolsToArray(getExportsOfModule(moduleSymbol)); + } + function getExportsOfSymbol(symbol) { + return symbol.flags & 1536 /* Module */ ? getExportsOfModule(symbol) : symbol.exports || emptySymbols; + } + function getExportsOfModule(moduleSymbol) { + var links = getSymbolLinks(moduleSymbol); + return links.resolvedExports || (links.resolvedExports = getExportsForModule(moduleSymbol)); + } + /** + * Extends one symbol table with another while collecting information on name collisions for error message generation into the `lookupTable` argument + * Not passing `lookupTable` and `exportNode` disables this collection, and just extends the tables + */ + function extendExportSymbols(target, source, lookupTable, exportNode) { + for (var id in source) { + if (id !== "default" && !target[id]) { + target[id] = source[id]; + if (lookupTable && exportNode) { + lookupTable[id] = { + specifierText: ts.getTextOfNode(exportNode.moduleSpecifier) + }; } - else if (type.flags & 96 /* StringOrNumberLiteral */) { - writer.writeStringLiteral(literalTypeToString(type)); + } + else if (lookupTable && exportNode && id !== "default" && target[id] && resolveSymbol(target[id]) !== resolveSymbol(source[id])) { + if (!lookupTable[id].exportsWithDuplicate) { + lookupTable[id].exportsWithDuplicate = [exportNode]; } else { - // Should never get here - // { ... } - writePunctuation(writer, 15 /* OpenBraceToken */); - writeSpace(writer); - writePunctuation(writer, 22 /* DotDotDotToken */); - writeSpace(writer); - writePunctuation(writer, 16 /* CloseBraceToken */); + lookupTable[id].exportsWithDuplicate.push(exportNode); } } - function writeTypeList(types, delimiter) { - for (var i = 0; i < types.length; i++) { - if (i > 0) { - if (delimiter !== 24 /* CommaToken */) { - writeSpace(writer); - } - writePunctuation(writer, delimiter); - writeSpace(writer); - } - writeType(types[i], delimiter === 24 /* CommaToken */ ? 0 /* None */ : 64 /* InElementType */); - } + } + } + function getExportsForModule(moduleSymbol) { + var visitedSymbols = []; + // A module defined by an 'export=' consists on one export that needs to be resolved + moduleSymbol = resolveExternalModuleSymbol(moduleSymbol); + return visit(moduleSymbol) || moduleSymbol.exports; + // The ES6 spec permits export * declarations in a module to circularly reference the module itself. For example, + // module 'a' can 'export * from "b"' and 'b' can 'export * from "a"' without error. + function visit(symbol) { + if (!(symbol && symbol.flags & 1952 /* HasExports */ && !ts.contains(visitedSymbols, symbol))) { + return; } - function writeSymbolTypeReference(symbol, typeArguments, pos, end, flags) { - // Unnamed function expressions and arrow functions have reserved names that we don't want to display - if (symbol.flags & 32 /* Class */ || !isReservedMemberName(symbol.name)) { - buildSymbolDisplay(symbol, writer, enclosingDeclaration, 793064 /* Type */, 0 /* None */, flags); + visitedSymbols.push(symbol); + var symbols = ts.cloneMap(symbol.exports); + // All export * declarations are collected in an __export symbol by the binder + var exportStars = symbol.exports["__export"]; + if (exportStars) { + var nestedSymbols = ts.createMap(); + var lookupTable = ts.createMap(); + for (var _i = 0, _a = exportStars.declarations; _i < _a.length; _i++) { + var node = _a[_i]; + var resolvedModule = resolveExternalModuleName(node, node.moduleSpecifier); + var exportedSymbols = visit(resolvedModule); + extendExportSymbols(nestedSymbols, exportedSymbols, lookupTable, node); } - if (pos < end) { - writePunctuation(writer, 25 /* LessThanToken */); - writeType(typeArguments[pos], 256 /* InFirstTypeArgument */); - pos++; - while (pos < end) { - writePunctuation(writer, 24 /* CommaToken */); - writeSpace(writer); - writeType(typeArguments[pos], 0 /* None */); - pos++; + for (var id in lookupTable) { + var exportsWithDuplicate = lookupTable[id].exportsWithDuplicate; + // It's not an error if the file with multiple `export *`s with duplicate names exports a member with that name itself + if (id === "export=" || !(exportsWithDuplicate && exportsWithDuplicate.length) || symbols[id]) { + continue; } - writePunctuation(writer, 27 /* GreaterThanToken */); - } - } - function writeTypeReference(type, flags) { - var typeArguments = type.typeArguments || emptyArray; - if (type.target === globalArrayType && !(flags & 1 /* WriteArrayAsGenericType */)) { - writeType(typeArguments[0], 64 /* InElementType */); - writePunctuation(writer, 19 /* OpenBracketToken */); - writePunctuation(writer, 20 /* CloseBracketToken */); - } - else if (type.target.flags & 262144 /* Tuple */) { - writePunctuation(writer, 19 /* OpenBracketToken */); - writeTypeList(type.typeArguments.slice(0, getTypeReferenceArity(type)), 24 /* CommaToken */); - writePunctuation(writer, 20 /* CloseBracketToken */); - } - else { - // Write the type reference in the format f.g.C where A and B are type arguments - // for outer type parameters, and f and g are the respective declaring containers of those - // type parameters. - var outerTypeParameters = type.target.outerTypeParameters; - var i = 0; - if (outerTypeParameters) { - var length_1 = outerTypeParameters.length; - while (i < length_1) { - // Find group of type arguments for type parameters with the same declaring container. - var start = i; - var parent_8 = getParentSymbolOfTypeParameter(outerTypeParameters[i]); - do { - i++; - } while (i < length_1 && getParentSymbolOfTypeParameter(outerTypeParameters[i]) === parent_8); - // When type parameters are their own type arguments for the whole group (i.e. we have - // the default outer type arguments), we don't show the group. - if (!ts.rangeEquals(outerTypeParameters, typeArguments, start, i)) { - writeSymbolTypeReference(parent_8, typeArguments, start, i, flags); - writePunctuation(writer, 21 /* DotToken */); - } - } + for (var _b = 0, exportsWithDuplicate_1 = exportsWithDuplicate; _b < exportsWithDuplicate_1.length; _b++) { + var node = exportsWithDuplicate_1[_b]; + diagnostics.add(ts.createDiagnosticForNode(node, ts.Diagnostics.Module_0_has_already_exported_a_member_named_1_Consider_explicitly_re_exporting_to_resolve_the_ambiguity, lookupTable[id].specifierText, id)); } - var typeParameterCount = (type.target.typeParameters || emptyArray).length; - writeSymbolTypeReference(type.symbol, typeArguments, i, typeParameterCount, flags); } + extendExportSymbols(symbols, nestedSymbols); } - function writeUnionOrIntersectionType(type, flags) { - if (flags & 64 /* InElementType */) { - writePunctuation(writer, 17 /* OpenParenToken */); - } - if (type.flags & 524288 /* Union */) { - writeTypeList(formatUnionTypes(type.types), 47 /* BarToken */); - } - else { - writeTypeList(type.types, 46 /* AmpersandToken */); + return symbols; + } + } + function getMergedSymbol(symbol) { + var merged; + return symbol && symbol.mergeId && (merged = mergedSymbols[symbol.mergeId]) ? merged : symbol; + } + function getSymbolOfNode(node) { + return getMergedSymbol(node.symbol); + } + function getParentOfSymbol(symbol) { + return getMergedSymbol(symbol.parent); + } + function getExportSymbolOfValueSymbolIfExported(symbol) { + return symbol && (symbol.flags & 1048576 /* ExportValue */) !== 0 + ? getMergedSymbol(symbol.exportSymbol) + : symbol; + } + function symbolIsValue(symbol) { + // If it is an instantiated symbol, then it is a value if the symbol it is an + // instantiation of is a value. + if (symbol.flags & 16777216 /* Instantiated */) { + return symbolIsValue(getSymbolLinks(symbol).target); + } + // If the symbol has the value flag, it is trivially a value. + if (symbol.flags & 107455 /* Value */) { + return true; + } + // If it is an alias, then it is a value if the symbol it resolves to is a value. + if (symbol.flags & 8388608 /* Alias */) { + return (resolveAlias(symbol).flags & 107455 /* Value */) !== 0; + } + return false; + } + function findConstructorDeclaration(node) { + var members = node.members; + for (var _i = 0, members_1 = members; _i < members_1.length; _i++) { + var member = members_1[_i]; + if (member.kind === 148 /* Constructor */ && ts.nodeIsPresent(member.body)) { + return member; + } + } + } + function createType(flags) { + var result = new Type(checker, flags); + typeCount++; + result.id = typeCount; + return result; + } + function createIntrinsicType(kind, intrinsicName) { + var type = createType(kind); + type.intrinsicName = intrinsicName; + return type; + } + function createBooleanType(trueFalseTypes) { + var type = getUnionType(trueFalseTypes); + type.flags |= 8 /* Boolean */; + type.intrinsicName = "boolean"; + return type; + } + function createObjectType(kind, symbol) { + var type = createType(kind); + type.symbol = symbol; + return type; + } + // A reserved member name starts with two underscores, but the third character cannot be an underscore + // or the @ symbol. A third underscore indicates an escaped form of an identifer that started + // with at least two underscores. The @ character indicates that the name is denoted by a well known ES + // Symbol instance. + function isReservedMemberName(name) { + return name.charCodeAt(0) === 95 /* _ */ && + name.charCodeAt(1) === 95 /* _ */ && + name.charCodeAt(2) !== 95 /* _ */ && + name.charCodeAt(2) !== 64 /* at */; + } + function getNamedMembers(members) { + var result; + for (var id in members) { + if (!isReservedMemberName(id)) { + if (!result) + result = []; + var symbol = members[id]; + if (symbolIsValue(symbol)) { + result.push(symbol); } - if (flags & 64 /* InElementType */) { - writePunctuation(writer, 18 /* CloseParenToken */); + } + } + return result || emptyArray; + } + function setObjectTypeMembers(type, members, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo) { + type.members = members; + type.properties = getNamedMembers(members); + type.callSignatures = callSignatures; + type.constructSignatures = constructSignatures; + if (stringIndexInfo) + type.stringIndexInfo = stringIndexInfo; + if (numberIndexInfo) + type.numberIndexInfo = numberIndexInfo; + return type; + } + function createAnonymousType(symbol, members, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo) { + return setObjectTypeMembers(createObjectType(2097152 /* Anonymous */, symbol), members, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo); + } + function forEachSymbolTableInScope(enclosingDeclaration, callback) { + var result; + for (var location_1 = enclosingDeclaration; location_1; location_1 = location_1.parent) { + // Locals of a source file are not in scope (because they get merged into the global symbol table) + if (location_1.locals && !isGlobalSourceFile(location_1)) { + if (result = callback(location_1.locals)) { + return result; } } - function writeAnonymousType(type, flags) { - var symbol = type.symbol; - if (symbol) { - // Always use 'typeof T' for type of class, enum, and module objects - if (symbol.flags & (32 /* Class */ | 384 /* Enum */ | 512 /* ValueModule */)) { - writeTypeOfSymbol(type, flags); - } - else if (shouldWriteTypeOfFunctionSymbol()) { - writeTypeOfSymbol(type, flags); - } - else if (ts.contains(symbolStack, symbol)) { - // If type is an anonymous type literal in a type alias declaration, use type alias name - var typeAlias = getTypeAliasForTypeLiteral(type); - if (typeAlias) { - // The specified symbol flags need to be reinterpreted as type flags - buildSymbolDisplay(typeAlias, writer, enclosingDeclaration, 793064 /* Type */, 0 /* None */, flags); - } - else { - // Recursive usage, use any - writeKeyword(writer, 117 /* AnyKeyword */); - } - } - else { - // Since instantiations of the same anonymous type have the same symbol, tracking symbols instead - // of types allows us to catch circular references to instantiations of the same anonymous type - if (!symbolStack) { - symbolStack = []; - } - symbolStack.push(symbol); - writeLiteralType(type, flags); - symbolStack.pop(); + switch (location_1.kind) { + case 256 /* SourceFile */: + if (!ts.isExternalOrCommonJsModule(location_1)) { + break; } - } - else { - // Anonymous types with no symbol are never circular - writeLiteralType(type, flags); - } - function shouldWriteTypeOfFunctionSymbol() { - var isStaticMethodSymbol = !!(symbol.flags & 8192 /* Method */ && - ts.forEach(symbol.declarations, function (declaration) { return ts.getModifierFlags(declaration) & 32 /* Static */; })); - var isNonLocalFunctionSymbol = !!(symbol.flags & 16 /* Function */) && - (symbol.parent || - ts.forEach(symbol.declarations, function (declaration) { - return declaration.parent.kind === 256 /* SourceFile */ || declaration.parent.kind === 226 /* ModuleBlock */; - })); - if (isStaticMethodSymbol || isNonLocalFunctionSymbol) { - // typeof is allowed only for static/non local functions - return !!(flags & 2 /* UseTypeOfFunction */) || - (ts.contains(symbolStack, symbol)); // it is type of the symbol uses itself recursively + case 225 /* ModuleDeclaration */: + if (result = callback(getSymbolOfNode(location_1).exports)) { + return result; } - } - } - function writeTypeOfSymbol(type, typeFormatFlags) { - writeKeyword(writer, 101 /* TypeOfKeyword */); - writeSpace(writer); - buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, 107455 /* Value */, 0 /* None */, typeFormatFlags); + break; } - function writeIndexSignature(info, keyword) { - if (info) { - if (info.isReadonly) { - writeKeyword(writer, 128 /* ReadonlyKeyword */); - writeSpace(writer); - } - writePunctuation(writer, 19 /* OpenBracketToken */); - writer.writeParameter(info.declaration ? ts.declarationNameToString(info.declaration.parameters[0].name) : "x"); - writePunctuation(writer, 54 /* ColonToken */); - writeSpace(writer); - writeKeyword(writer, keyword); - writePunctuation(writer, 20 /* CloseBracketToken */); - writePunctuation(writer, 54 /* ColonToken */); - writeSpace(writer); - writeType(info.type, 0 /* None */); - writePunctuation(writer, 23 /* SemicolonToken */); - writer.writeLine(); + } + return callback(globals); + } + function getQualifiedLeftMeaning(rightMeaning) { + // If we are looking in value space, the parent meaning is value, other wise it is namespace + return rightMeaning === 107455 /* Value */ ? 107455 /* Value */ : 1920 /* Namespace */; + } + function getAccessibleSymbolChain(symbol, enclosingDeclaration, meaning, useOnlyExternalAliasing) { + function getAccessibleSymbolChainFromSymbolTable(symbols) { + function canQualifySymbol(symbolFromSymbolTable, meaning) { + // If the symbol is equivalent and doesn't need further qualification, this symbol is accessible + if (!needsQualification(symbolFromSymbolTable, enclosingDeclaration, meaning)) { + return true; } + // If symbol needs qualification, make sure that parent is accessible, if it is then this symbol is accessible too + var accessibleParent = getAccessibleSymbolChain(symbolFromSymbolTable.parent, enclosingDeclaration, getQualifiedLeftMeaning(meaning), useOnlyExternalAliasing); + return !!accessibleParent; } - function writePropertyWithModifiers(prop) { - if (isReadonlySymbol(prop)) { - writeKeyword(writer, 128 /* ReadonlyKeyword */); - writeSpace(writer); - } - buildSymbolDisplay(prop, writer); - if (prop.flags & 536870912 /* Optional */) { - writePunctuation(writer, 53 /* QuestionToken */); + function isAccessible(symbolFromSymbolTable, resolvedAliasSymbol) { + if (symbol === (resolvedAliasSymbol || symbolFromSymbolTable)) { + // if the symbolFromSymbolTable is not external module (it could be if it was determined as ambient external module and would be in globals table) + // and if symbolFromSymbolTable or alias resolution matches the symbol, + // check the symbol can be qualified, it is only then this symbol is accessible + return !ts.forEach(symbolFromSymbolTable.declarations, hasExternalModuleSymbol) && + canQualifySymbol(symbolFromSymbolTable, meaning); } } - function shouldAddParenthesisAroundFunctionType(callSignature, flags) { - if (flags & 64 /* InElementType */) { - return true; - } - else if (flags & 256 /* InFirstTypeArgument */) { - // Add parenthesis around function type for the first type argument to avoid ambiguity - var typeParameters = callSignature.target && (flags & 32 /* WriteTypeArgumentsOfSignature */) ? - callSignature.target.typeParameters : callSignature.typeParameters; - return typeParameters && typeParameters.length !== 0; - } - return false; + // If symbol is directly available by its name in the symbol table + if (isAccessible(symbols[symbol.name])) { + return [symbol]; } - function writeLiteralType(type, flags) { - var resolved = resolveStructuredTypeMembers(type); - if (!resolved.properties.length && !resolved.stringIndexInfo && !resolved.numberIndexInfo) { - if (!resolved.callSignatures.length && !resolved.constructSignatures.length) { - writePunctuation(writer, 15 /* OpenBraceToken */); - writePunctuation(writer, 16 /* CloseBraceToken */); - return; - } - if (resolved.callSignatures.length === 1 && !resolved.constructSignatures.length) { - var parenthesizeSignature = shouldAddParenthesisAroundFunctionType(resolved.callSignatures[0], flags); - if (parenthesizeSignature) { - writePunctuation(writer, 17 /* OpenParenToken */); - } - buildSignatureDisplay(resolved.callSignatures[0], writer, enclosingDeclaration, globalFlagsToPass | 8 /* WriteArrowStyleSignature */, /*kind*/ undefined, symbolStack); - if (parenthesizeSignature) { - writePunctuation(writer, 18 /* CloseParenToken */); - } - return; - } - if (resolved.constructSignatures.length === 1 && !resolved.callSignatures.length) { - if (flags & 64 /* InElementType */) { - writePunctuation(writer, 17 /* OpenParenToken */); - } - writeKeyword(writer, 92 /* NewKeyword */); - writeSpace(writer); - buildSignatureDisplay(resolved.constructSignatures[0], writer, enclosingDeclaration, globalFlagsToPass | 8 /* WriteArrowStyleSignature */, /*kind*/ undefined, symbolStack); - if (flags & 64 /* InElementType */) { - writePunctuation(writer, 18 /* CloseParenToken */); + // Check if symbol is any of the alias + return ts.forEachProperty(symbols, function (symbolFromSymbolTable) { + if (symbolFromSymbolTable.flags & 8388608 /* Alias */ + && symbolFromSymbolTable.name !== "export=" + && !ts.getDeclarationOfKind(symbolFromSymbolTable, 238 /* ExportSpecifier */)) { + if (!useOnlyExternalAliasing || + // Is this external alias, then use it to name + ts.forEach(symbolFromSymbolTable.declarations, ts.isExternalModuleImportEqualsDeclaration)) { + var resolvedImportedSymbol = resolveAlias(symbolFromSymbolTable); + if (isAccessible(symbolFromSymbolTable, resolveAlias(symbolFromSymbolTable))) { + return [symbolFromSymbolTable]; } - return; - } - } - var saveInObjectTypeLiteral = inObjectTypeLiteral; - inObjectTypeLiteral = true; - writePunctuation(writer, 15 /* OpenBraceToken */); - writer.writeLine(); - writer.increaseIndent(); - for (var _i = 0, _a = resolved.callSignatures; _i < _a.length; _i++) { - var signature = _a[_i]; - buildSignatureDisplay(signature, writer, enclosingDeclaration, globalFlagsToPass, /*kind*/ undefined, symbolStack); - writePunctuation(writer, 23 /* SemicolonToken */); - writer.writeLine(); - } - for (var _b = 0, _c = resolved.constructSignatures; _b < _c.length; _b++) { - var signature = _c[_b]; - buildSignatureDisplay(signature, writer, enclosingDeclaration, globalFlagsToPass, 1 /* Construct */, symbolStack); - writePunctuation(writer, 23 /* SemicolonToken */); - writer.writeLine(); - } - writeIndexSignature(resolved.stringIndexInfo, 132 /* StringKeyword */); - writeIndexSignature(resolved.numberIndexInfo, 130 /* NumberKeyword */); - for (var _d = 0, _e = resolved.properties; _d < _e.length; _d++) { - var p = _e[_d]; - var t = getTypeOfSymbol(p); - if (p.flags & (16 /* Function */ | 8192 /* Method */) && !getPropertiesOfObjectType(t).length) { - var signatures = getSignaturesOfType(t, 0 /* Call */); - for (var _f = 0, signatures_1 = signatures; _f < signatures_1.length; _f++) { - var signature = signatures_1[_f]; - writePropertyWithModifiers(p); - buildSignatureDisplay(signature, writer, enclosingDeclaration, globalFlagsToPass, /*kind*/ undefined, symbolStack); - writePunctuation(writer, 23 /* SemicolonToken */); - writer.writeLine(); + // Look in the exported members, if we can find accessibleSymbolChain, symbol is accessible using this chain + // but only if the symbolFromSymbolTable can be qualified + var accessibleSymbolsFromExports = resolvedImportedSymbol.exports ? getAccessibleSymbolChainFromSymbolTable(resolvedImportedSymbol.exports) : undefined; + if (accessibleSymbolsFromExports && canQualifySymbol(symbolFromSymbolTable, getQualifiedLeftMeaning(meaning))) { + return [symbolFromSymbolTable].concat(accessibleSymbolsFromExports); } } - else { - writePropertyWithModifiers(p); - writePunctuation(writer, 54 /* ColonToken */); - writeSpace(writer); - writeType(t, 0 /* None */); - writePunctuation(writer, 23 /* SemicolonToken */); - writer.writeLine(); - } } - writer.decreaseIndent(); - writePunctuation(writer, 16 /* CloseBraceToken */); - inObjectTypeLiteral = saveInObjectTypeLiteral; - } - } - function buildTypeParameterDisplayFromSymbol(symbol, writer, enclosingDeclaration, flags) { - var targetSymbol = getTargetSymbol(symbol); - if (targetSymbol.flags & 32 /* Class */ || targetSymbol.flags & 64 /* Interface */ || targetSymbol.flags & 524288 /* TypeAlias */) { - buildDisplayForTypeParametersAndDelimiters(getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol), writer, enclosingDeclaration, flags); - } + }); } - function buildTypeParameterDisplay(tp, writer, enclosingDeclaration, flags, symbolStack) { - appendSymbolNameOnly(tp.symbol, writer); - var constraint = getConstraintOfTypeParameter(tp); - if (constraint) { - writeSpace(writer); - writeKeyword(writer, 83 /* ExtendsKeyword */); - writeSpace(writer); - buildTypeDisplay(constraint, writer, enclosingDeclaration, flags, symbolStack); + if (symbol) { + if (!(isPropertyOrMethodDeclarationSymbol(symbol))) { + return forEachSymbolTableInScope(enclosingDeclaration, getAccessibleSymbolChainFromSymbolTable); } } - function buildParameterDisplay(p, writer, enclosingDeclaration, flags, symbolStack) { - var parameterNode = p.valueDeclaration; - if (ts.isRestParameter(parameterNode)) { - writePunctuation(writer, 22 /* DotDotDotToken */); - } - if (ts.isBindingPattern(parameterNode.name)) { - buildBindingPatternDisplay(parameterNode.name, writer, enclosingDeclaration, flags, symbolStack); - } - else { - appendSymbolNameOnly(p, writer); + } + function needsQualification(symbol, enclosingDeclaration, meaning) { + var qualify = false; + forEachSymbolTableInScope(enclosingDeclaration, function (symbolTable) { + // If symbol of this name is not available in the symbol table we are ok + var symbolFromSymbolTable = symbolTable[symbol.name]; + if (!symbolFromSymbolTable) { + // Continue to the next symbol table + return false; } - if (isOptionalParameter(parameterNode)) { - writePunctuation(writer, 53 /* QuestionToken */); + // If the symbol with this name is present it should refer to the symbol + if (symbolFromSymbolTable === symbol) { + // No need to qualify + return true; } - writePunctuation(writer, 54 /* ColonToken */); - writeSpace(writer); - buildTypeDisplay(getTypeOfSymbol(p), writer, enclosingDeclaration, flags, symbolStack); - } - function buildBindingPatternDisplay(bindingPattern, writer, enclosingDeclaration, flags, symbolStack) { - // We have to explicitly emit square bracket and bracket because these tokens are not stored inside the node. - if (bindingPattern.kind === 167 /* ObjectBindingPattern */) { - writePunctuation(writer, 15 /* OpenBraceToken */); - buildDisplayForCommaSeparatedList(bindingPattern.elements, writer, function (e) { return buildBindingElementDisplay(e, writer, enclosingDeclaration, flags, symbolStack); }); - writePunctuation(writer, 16 /* CloseBraceToken */); + // Qualify if the symbol from symbol table has same meaning as expected + symbolFromSymbolTable = (symbolFromSymbolTable.flags & 8388608 /* Alias */ && !ts.getDeclarationOfKind(symbolFromSymbolTable, 238 /* ExportSpecifier */)) ? resolveAlias(symbolFromSymbolTable) : symbolFromSymbolTable; + if (symbolFromSymbolTable.flags & meaning) { + qualify = true; + return true; } - else if (bindingPattern.kind === 168 /* ArrayBindingPattern */) { - writePunctuation(writer, 19 /* OpenBracketToken */); - var elements = bindingPattern.elements; - buildDisplayForCommaSeparatedList(elements, writer, function (e) { return buildBindingElementDisplay(e, writer, enclosingDeclaration, flags, symbolStack); }); - if (elements && elements.hasTrailingComma) { - writePunctuation(writer, 24 /* CommaToken */); + // Continue to the next symbol table + return false; + }); + return qualify; + } + function isPropertyOrMethodDeclarationSymbol(symbol) { + if (symbol.declarations && symbol.declarations.length) { + for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { + var declaration = _a[_i]; + switch (declaration.kind) { + case 145 /* PropertyDeclaration */: + case 147 /* MethodDeclaration */: + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + continue; + default: + return false; } - writePunctuation(writer, 20 /* CloseBracketToken */); } + return true; } - function buildBindingElementDisplay(bindingElement, writer, enclosingDeclaration, flags, symbolStack) { - if (ts.isOmittedExpression(bindingElement)) { - return; - } - ts.Debug.assert(bindingElement.kind === 169 /* BindingElement */); - if (bindingElement.propertyName) { - writer.writeSymbol(ts.getTextOfNode(bindingElement.propertyName), bindingElement.symbol); - writePunctuation(writer, 54 /* ColonToken */); - writeSpace(writer); - } - if (ts.isBindingPattern(bindingElement.name)) { - buildBindingPatternDisplay(bindingElement.name, writer, enclosingDeclaration, flags, symbolStack); - } - else { - if (bindingElement.dotDotDotToken) { - writePunctuation(writer, 22 /* DotDotDotToken */); - } - appendSymbolNameOnly(bindingElement.symbol, writer); - } - } - function buildDisplayForTypeParametersAndDelimiters(typeParameters, writer, enclosingDeclaration, flags, symbolStack) { - if (typeParameters && typeParameters.length) { - writePunctuation(writer, 25 /* LessThanToken */); - buildDisplayForCommaSeparatedList(typeParameters, writer, function (p) { return buildTypeParameterDisplay(p, writer, enclosingDeclaration, flags, symbolStack); }); - writePunctuation(writer, 27 /* GreaterThanToken */); - } - } - function buildDisplayForCommaSeparatedList(list, writer, action) { - for (var i = 0; i < list.length; i++) { - if (i > 0) { - writePunctuation(writer, 24 /* CommaToken */); - writeSpace(writer); - } - action(list[i]); - } - } - function buildDisplayForTypeArgumentsAndDelimiters(typeParameters, mapper, writer, enclosingDeclaration, flags, symbolStack) { - if (typeParameters && typeParameters.length) { - writePunctuation(writer, 25 /* LessThanToken */); - var flags_1 = 256 /* InFirstTypeArgument */; - for (var i = 0; i < typeParameters.length; i++) { - if (i > 0) { - writePunctuation(writer, 24 /* CommaToken */); - writeSpace(writer); - flags_1 = 0 /* None */; + return false; + } + /** + * Check if the given symbol in given enclosing declaration is accessible and mark all associated alias to be visible if requested + * + * @param symbol a Symbol to check if accessible + * @param enclosingDeclaration a Node containing reference to the symbol + * @param meaning a SymbolFlags to check if such meaning of the symbol is accessible + * @param shouldComputeAliasToMakeVisible a boolean value to indicate whether to return aliases to be mark visible in case the symbol is accessible + */ + function isSymbolAccessible(symbol, enclosingDeclaration, meaning, shouldComputeAliasesToMakeVisible) { + if (symbol && enclosingDeclaration && !(symbol.flags & 262144 /* TypeParameter */)) { + var initialSymbol = symbol; + var meaningToLook = meaning; + while (symbol) { + // Symbol is accessible if it by itself is accessible + var accessibleSymbolChain = getAccessibleSymbolChain(symbol, enclosingDeclaration, meaningToLook, /*useOnlyExternalAliasing*/ false); + if (accessibleSymbolChain) { + var hasAccessibleDeclarations = hasVisibleDeclarations(accessibleSymbolChain[0], shouldComputeAliasesToMakeVisible); + if (!hasAccessibleDeclarations) { + return { + accessibility: 1 /* NotAccessible */, + errorSymbolName: symbolToString(initialSymbol, enclosingDeclaration, meaning), + errorModuleName: symbol !== initialSymbol ? symbolToString(symbol, enclosingDeclaration, 1920 /* Namespace */) : undefined + }; } - buildTypeDisplay(mapper(typeParameters[i]), writer, enclosingDeclaration, flags_1); + return hasAccessibleDeclarations; } - writePunctuation(writer, 27 /* GreaterThanToken */); - } - } - function buildDisplayForParametersAndDelimiters(thisParameter, parameters, writer, enclosingDeclaration, flags, symbolStack) { - writePunctuation(writer, 17 /* OpenParenToken */); - if (thisParameter) { - buildParameterDisplay(thisParameter, writer, enclosingDeclaration, flags, symbolStack); + // If we haven't got the accessible symbol, it doesn't mean the symbol is actually inaccessible. + // It could be a qualified symbol and hence verify the path + // e.g.: + // module m { + // export class c { + // } + // } + // const x: typeof m.c + // In the above example when we start with checking if typeof m.c symbol is accessible, + // we are going to see if c can be accessed in scope directly. + // But it can't, hence the accessible is going to be undefined, but that doesn't mean m.c is inaccessible + // It is accessible if the parent m is accessible because then m.c can be accessed through qualification + meaningToLook = getQualifiedLeftMeaning(meaning); + symbol = getParentOfSymbol(symbol); } - for (var i = 0; i < parameters.length; i++) { - if (i > 0 || thisParameter) { - writePunctuation(writer, 24 /* CommaToken */); - writeSpace(writer); + // This could be a symbol that is not exported in the external module + // or it could be a symbol from different external module that is not aliased and hence cannot be named + var symbolExternalModule = ts.forEach(initialSymbol.declarations, getExternalModuleContainer); + if (symbolExternalModule) { + var enclosingExternalModule = getExternalModuleContainer(enclosingDeclaration); + if (symbolExternalModule !== enclosingExternalModule) { + // name from different external module that is not visible + return { + accessibility: 2 /* CannotBeNamed */, + errorSymbolName: symbolToString(initialSymbol, enclosingDeclaration, meaning), + errorModuleName: symbolToString(symbolExternalModule) + }; } - buildParameterDisplay(parameters[i], writer, enclosingDeclaration, flags, symbolStack); - } - writePunctuation(writer, 18 /* CloseParenToken */); - } - function buildTypePredicateDisplay(predicate, writer, enclosingDeclaration, flags, symbolStack) { - if (ts.isIdentifierTypePredicate(predicate)) { - writer.writeParameter(predicate.parameterName); - } - else { - writeKeyword(writer, 97 /* ThisKeyword */); - } - writeSpace(writer); - writeKeyword(writer, 124 /* IsKeyword */); - writeSpace(writer); - buildTypeDisplay(predicate.type, writer, enclosingDeclaration, flags, symbolStack); - } - function buildReturnTypeDisplay(signature, writer, enclosingDeclaration, flags, symbolStack) { - if (flags & 8 /* WriteArrowStyleSignature */) { - writeSpace(writer); - writePunctuation(writer, 34 /* EqualsGreaterThanToken */); - } - else { - writePunctuation(writer, 54 /* ColonToken */); - } - writeSpace(writer); - if (signature.typePredicate) { - buildTypePredicateDisplay(signature.typePredicate, writer, enclosingDeclaration, flags, symbolStack); - } - else { - var returnType = getReturnTypeOfSignature(signature); - buildTypeDisplay(returnType, writer, enclosingDeclaration, flags, symbolStack); } + // Just a local name that is not accessible + return { + accessibility: 1 /* NotAccessible */, + errorSymbolName: symbolToString(initialSymbol, enclosingDeclaration, meaning) + }; } - function buildSignatureDisplay(signature, writer, enclosingDeclaration, flags, kind, symbolStack) { - if (kind === 1 /* Construct */) { - writeKeyword(writer, 92 /* NewKeyword */); - writeSpace(writer); - } - if (signature.target && (flags & 32 /* WriteTypeArgumentsOfSignature */)) { - // Instantiated signature, write type arguments instead - // This is achieved by passing in the mapper separately - buildDisplayForTypeArgumentsAndDelimiters(signature.target.typeParameters, signature.mapper, writer, enclosingDeclaration); - } - else { - buildDisplayForTypeParametersAndDelimiters(signature.typeParameters, writer, enclosingDeclaration, flags, symbolStack); + return { accessibility: 0 /* Accessible */ }; + function getExternalModuleContainer(declaration) { + for (; declaration; declaration = declaration.parent) { + if (hasExternalModuleSymbol(declaration)) { + return getSymbolOfNode(declaration); + } } - buildDisplayForParametersAndDelimiters(signature.thisParameter, signature.parameters, writer, enclosingDeclaration, flags, symbolStack); - buildReturnTypeDisplay(signature, writer, enclosingDeclaration, flags, symbolStack); } - return _displayBuilder || (_displayBuilder = { - buildSymbolDisplay: buildSymbolDisplay, - buildTypeDisplay: buildTypeDisplay, - buildTypeParameterDisplay: buildTypeParameterDisplay, - buildTypePredicateDisplay: buildTypePredicateDisplay, - buildParameterDisplay: buildParameterDisplay, - buildDisplayForParametersAndDelimiters: buildDisplayForParametersAndDelimiters, - buildDisplayForTypeParametersAndDelimiters: buildDisplayForTypeParametersAndDelimiters, - buildTypeParameterDisplayFromSymbol: buildTypeParameterDisplayFromSymbol, - buildSignatureDisplay: buildSignatureDisplay, - buildReturnTypeDisplay: buildReturnTypeDisplay - }); } - function isDeclarationVisible(node) { - if (node) { - var links = getNodeLinks(node); - if (links.isVisible === undefined) { - links.isVisible = !!determineIfDeclarationIsVisible(); - } - return links.isVisible; + function hasExternalModuleSymbol(declaration) { + return ts.isAmbientModule(declaration) || (declaration.kind === 256 /* SourceFile */ && ts.isExternalOrCommonJsModule(declaration)); + } + function hasVisibleDeclarations(symbol, shouldComputeAliasToMakeVisible) { + var aliasesToMakeVisible; + if (ts.forEach(symbol.declarations, function (declaration) { return !getIsDeclarationVisible(declaration); })) { + return undefined; } - return false; - function determineIfDeclarationIsVisible() { - switch (node.kind) { - case 169 /* BindingElement */: - return isDeclarationVisible(node.parent.parent); - case 218 /* VariableDeclaration */: - if (ts.isBindingPattern(node.name) && - !node.name.elements.length) { - // If the binding pattern is empty, this variable declaration is not visible - return false; - } - // Otherwise fall through - case 225 /* ModuleDeclaration */: - case 221 /* ClassDeclaration */: - case 222 /* InterfaceDeclaration */: - case 223 /* TypeAliasDeclaration */: - case 220 /* FunctionDeclaration */: - case 224 /* EnumDeclaration */: - case 229 /* ImportEqualsDeclaration */: - // external module augmentation is always visible - if (ts.isExternalModuleAugmentation(node)) { - return true; - } - var parent_9 = getDeclarationContainer(node); - // If the node is not exported or it is not ambient module element (except import declaration) - if (!(ts.getCombinedModifierFlags(node) & 1 /* Export */) && - !(node.kind !== 229 /* ImportEqualsDeclaration */ && parent_9.kind !== 256 /* SourceFile */ && ts.isInAmbientContext(parent_9))) { - return isGlobalSourceFile(parent_9); - } - // Exported members/ambient module elements (exception import declaration) are visible if parent is visible - return isDeclarationVisible(parent_9); - case 145 /* PropertyDeclaration */: - case 144 /* PropertySignature */: - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - case 147 /* MethodDeclaration */: - case 146 /* MethodSignature */: - if (ts.getModifierFlags(node) & (8 /* Private */ | 16 /* Protected */)) { - // Private/protected properties/methods are not visible - return false; + return { accessibility: 0 /* Accessible */, aliasesToMakeVisible: aliasesToMakeVisible }; + function getIsDeclarationVisible(declaration) { + if (!isDeclarationVisible(declaration)) { + // Mark the unexported alias as visible if its parent is visible + // because these kind of aliases can be used to name types in declaration file + var anyImportSyntax = getAnyImportSyntax(declaration); + if (anyImportSyntax && + !(ts.getModifierFlags(anyImportSyntax) & 1 /* Export */) && + isDeclarationVisible(anyImportSyntax.parent)) { + // In function "buildTypeDisplay" where we decide whether to write type-alias or serialize types, + // we want to just check if type- alias is accessible or not but we don't care about emitting those alias at that time + // since we will do the emitting later in trackSymbol. + if (shouldComputeAliasToMakeVisible) { + getNodeLinks(declaration).isVisible = true; + if (aliasesToMakeVisible) { + if (!ts.contains(aliasesToMakeVisible, anyImportSyntax)) { + aliasesToMakeVisible.push(anyImportSyntax); + } + } + else { + aliasesToMakeVisible = [anyImportSyntax]; + } } - // Public properties/methods are visible if its parents are visible, so const it fall into next case statement - case 148 /* Constructor */: - case 152 /* ConstructSignature */: - case 151 /* CallSignature */: - case 153 /* IndexSignature */: - case 142 /* Parameter */: - case 226 /* ModuleBlock */: - case 156 /* FunctionType */: - case 157 /* ConstructorType */: - case 159 /* TypeLiteral */: - case 155 /* TypeReference */: - case 160 /* ArrayType */: - case 161 /* TupleType */: - case 162 /* UnionType */: - case 163 /* IntersectionType */: - case 164 /* ParenthesizedType */: - return isDeclarationVisible(node.parent); - // Default binding, import specifier and namespace import is visible - // only on demand so by default it is not visible - case 231 /* ImportClause */: - case 232 /* NamespaceImport */: - case 234 /* ImportSpecifier */: - return false; - // Type parameters are always visible - case 141 /* TypeParameter */: - // Source file and namespace export are always visible - case 256 /* SourceFile */: - case 228 /* NamespaceExportDeclaration */: return true; - // Export assignments do not create name bindings outside the module - case 235 /* ExportAssignment */: - return false; - default: - return false; + } + // Declaration is not visible + return false; } + return true; } } - function collectLinkedAliases(node) { - var exportSymbol; - if (node.parent && node.parent.kind === 235 /* ExportAssignment */) { - exportSymbol = resolveName(node.parent, node.text, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */ | 8388608 /* Alias */, ts.Diagnostics.Cannot_find_name_0, node); + function isEntityNameVisible(entityName, enclosingDeclaration) { + // get symbol of the first identifier of the entityName + var meaning; + if (entityName.parent.kind === 158 /* TypeQuery */ || ts.isExpressionWithTypeArgumentsInClassExtendsClause(entityName.parent)) { + // Typeof value + meaning = 107455 /* Value */ | 1048576 /* ExportValue */; } - else if (node.parent.kind === 238 /* ExportSpecifier */) { - var exportSpecifier = node.parent; - exportSymbol = exportSpecifier.parent.parent.moduleSpecifier ? - getExternalModuleMember(exportSpecifier.parent.parent, exportSpecifier) : - resolveEntityName(exportSpecifier.propertyName || exportSpecifier.name, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */ | 8388608 /* Alias */); + else if (entityName.kind === 139 /* QualifiedName */ || entityName.kind === 172 /* PropertyAccessExpression */ || + entityName.parent.kind === 229 /* ImportEqualsDeclaration */) { + // Left identifier from type reference or TypeAlias + // Entity name of the import declaration + meaning = 1920 /* Namespace */; } - var result = []; - if (exportSymbol) { - buildVisibleNodeList(exportSymbol.declarations); + else { + // Type Reference or TypeAlias entity = Identifier + meaning = 793064 /* Type */; } + var firstIdentifier = getFirstIdentifier(entityName); + var symbol = resolveName(enclosingDeclaration, firstIdentifier.text, meaning, /*nodeNotFoundErrorMessage*/ undefined, /*nameArg*/ undefined); + // Verify if the symbol is accessible + return (symbol && hasVisibleDeclarations(symbol, /*shouldComputeAliasToMakeVisible*/ true)) || { + accessibility: 1 /* NotAccessible */, + errorSymbolName: ts.getTextOfNode(firstIdentifier), + errorNode: firstIdentifier + }; + } + function writeKeyword(writer, kind) { + writer.writeKeyword(ts.tokenToString(kind)); + } + function writePunctuation(writer, kind) { + writer.writePunctuation(ts.tokenToString(kind)); + } + function writeSpace(writer) { + writer.writeSpace(" "); + } + function symbolToString(symbol, enclosingDeclaration, meaning) { + var writer = ts.getSingleLineStringWriter(); + getSymbolDisplayBuilder().buildSymbolDisplay(symbol, writer, enclosingDeclaration, meaning); + var result = writer.string(); + ts.releaseStringWriter(writer); return result; - function buildVisibleNodeList(declarations) { - ts.forEach(declarations, function (declaration) { - getNodeLinks(declaration).isVisible = true; - var resultNode = getAnyImportSyntax(declaration) || declaration; - if (!ts.contains(result, resultNode)) { - result.push(resultNode); - } - if (ts.isInternalModuleImportEqualsDeclaration(declaration)) { - // Add the referenced top container visible - var internalModuleReference = declaration.moduleReference; - var firstIdentifier = getFirstIdentifier(internalModuleReference); - var importSymbol = resolveName(declaration, firstIdentifier.text, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */, undefined, undefined); - if (importSymbol) { - buildVisibleNodeList(importSymbol.declarations); - } - } - }); - } } - /** - * Push an entry on the type resolution stack. If an entry with the given target and the given property name - * is already on the stack, and no entries in between already have a type, then a circularity has occurred. - * In this case, the result values of the existing entry and all entries pushed after it are changed to false, - * and the value false is returned. Otherwise, the new entry is just pushed onto the stack, and true is returned. - * In order to see if the same query has already been done before, the target object and the propertyName both - * must match the one passed in. - * - * @param target The symbol, type, or signature whose type is being queried - * @param propertyName The property name that should be used to query the target for its type - */ - function pushTypeResolution(target, propertyName) { - var resolutionCycleStartIndex = findResolutionCycleStartIndex(target, propertyName); - if (resolutionCycleStartIndex >= 0) { - // A cycle was found - var length_2 = resolutionTargets.length; - for (var i = resolutionCycleStartIndex; i < length_2; i++) { - resolutionResults[i] = false; - } - return false; + function signatureToString(signature, enclosingDeclaration, flags, kind) { + var writer = ts.getSingleLineStringWriter(); + getSymbolDisplayBuilder().buildSignatureDisplay(signature, writer, enclosingDeclaration, flags, kind); + var result = writer.string(); + ts.releaseStringWriter(writer); + return result; + } + function typeToString(type, enclosingDeclaration, flags) { + var writer = ts.getSingleLineStringWriter(); + getSymbolDisplayBuilder().buildTypeDisplay(type, writer, enclosingDeclaration, flags); + var result = writer.string(); + ts.releaseStringWriter(writer); + var maxLength = compilerOptions.noErrorTruncation || flags & 4 /* NoTruncation */ ? undefined : 100; + if (maxLength && result.length >= maxLength) { + result = result.substr(0, maxLength - "...".length) + "..."; } - resolutionTargets.push(target); - resolutionResults.push(/*items*/ true); - resolutionPropertyNames.push(propertyName); - return true; + return result; } - function findResolutionCycleStartIndex(target, propertyName) { - for (var i = resolutionTargets.length - 1; i >= 0; i--) { - if (hasType(resolutionTargets[i], resolutionPropertyNames[i])) { - return -1; - } - if (resolutionTargets[i] === target && resolutionPropertyNames[i] === propertyName) { - return i; + function typePredicateToString(typePredicate, enclosingDeclaration, flags) { + var writer = ts.getSingleLineStringWriter(); + getSymbolDisplayBuilder().buildTypePredicateDisplay(typePredicate, writer, enclosingDeclaration, flags); + var result = writer.string(); + ts.releaseStringWriter(writer); + return result; + } + function formatUnionTypes(types) { + var result = []; + var flags = 0; + for (var i = 0; i < types.length; i++) { + var t = types[i]; + flags |= t.flags; + if (!(t.flags & 6144 /* Nullable */)) { + if (t.flags & (128 /* BooleanLiteral */ | 256 /* EnumLiteral */)) { + var baseType = t.flags & 128 /* BooleanLiteral */ ? booleanType : t.baseType; + var count = baseType.types.length; + if (i + count <= types.length && types[i + count - 1] === baseType.types[count - 1]) { + result.push(baseType); + i += count - 1; + continue; + } + } + result.push(t); } } - return -1; + if (flags & 4096 /* Null */) + result.push(nullType); + if (flags & 2048 /* Undefined */) + result.push(undefinedType); + return result || types; } - function hasType(target, propertyName) { - if (propertyName === 0 /* Type */) { - return getSymbolLinks(target).type; - } - if (propertyName === 2 /* DeclaredType */) { - return getSymbolLinks(target).declaredType; - } - if (propertyName === 1 /* ResolvedBaseConstructorType */) { - ts.Debug.assert(!!(target.flags & 32768 /* Class */)); - return target.resolvedBaseConstructorType; + function visibilityToString(flags) { + if (flags === 8 /* Private */) { + return "private"; } - if (propertyName === 3 /* ResolvedReturnType */) { - return target.resolvedReturnType; + if (flags === 16 /* Protected */) { + return "protected"; } - ts.Debug.fail("Unhandled TypeSystemPropertyName " + propertyName); - } - // Pop an entry from the type resolution stack and return its associated result value. The result value will - // be true if no circularities were detected, or false if a circularity was found. - function popTypeResolution() { - resolutionTargets.pop(); - resolutionPropertyNames.pop(); - return resolutionResults.pop(); + return "public"; } - function getDeclarationContainer(node) { - node = ts.getRootDeclaration(node); - while (node) { - switch (node.kind) { - case 218 /* VariableDeclaration */: - case 219 /* VariableDeclarationList */: - case 234 /* ImportSpecifier */: - case 233 /* NamedImports */: - case 232 /* NamespaceImport */: - case 231 /* ImportClause */: - node = node.parent; - break; - default: - return node.parent; + function getTypeAliasForTypeLiteral(type) { + if (type.symbol && type.symbol.flags & 2048 /* TypeLiteral */) { + var node = type.symbol.declarations[0].parent; + while (node.kind === 164 /* ParenthesizedType */) { + node = node.parent; + } + if (node.kind === 223 /* TypeAliasDeclaration */) { + return getSymbolOfNode(node); } } + return undefined; } - function getTypeOfPrototypeProperty(prototype) { - // TypeScript 1.0 spec (April 2014): 8.4 - // Every class automatically contains a static property member named 'prototype', - // the type of which is an instantiation of the class type with type Any supplied as a type argument for each type parameter. - // It is an error to explicitly declare a static property member with the name 'prototype'. - var classType = getDeclaredTypeOfSymbol(getParentOfSymbol(prototype)); - return classType.typeParameters ? createTypeReference(classType, ts.map(classType.typeParameters, function (_) { return anyType; })) : classType; - } - // Return the type of the given property in the given type, or undefined if no such property exists - function getTypeOfPropertyOfType(type, name) { - var prop = getPropertyOfType(type, name); - return prop ? getTypeOfSymbol(prop) : undefined; - } - function isTypeAny(type) { - return type && (type.flags & 1 /* Any */) !== 0; - } - function isTypeNever(type) { - return type && (type.flags & 8192 /* Never */) !== 0; + function isTopLevelInExternalModuleAugmentation(node) { + return node && node.parent && + node.parent.kind === 226 /* ModuleBlock */ && + ts.isExternalModuleAugmentation(node.parent.parent); } - // Return the type of a binding element parent. We check SymbolLinks first to see if a type has been - // assigned by contextual typing. - function getTypeForBindingElementParent(node) { - var symbol = getSymbolOfNode(node); - return symbol && getSymbolLinks(symbol).type || getTypeForVariableLikeDeclaration(node, /*includeOptionality*/ false); + function literalTypeToString(type) { + return type.flags & 32 /* StringLiteral */ ? "\"" + ts.escapeString(type.text) + "\"" : type.text; } - function getTextOfPropertyName(name) { - switch (name.kind) { - case 69 /* Identifier */: - return name.text; - case 9 /* StringLiteral */: - case 8 /* NumericLiteral */: - return name.text; - case 140 /* ComputedPropertyName */: - if (ts.isStringOrNumericLiteral(name.expression.kind)) { - return name.expression.text; + function getSymbolDisplayBuilder() { + function getNameOfSymbol(symbol) { + if (symbol.declarations && symbol.declarations.length) { + var declaration = symbol.declarations[0]; + if (declaration.name) { + return ts.declarationNameToString(declaration.name); + } + switch (declaration.kind) { + case 192 /* ClassExpression */: + return "(Anonymous class)"; + case 179 /* FunctionExpression */: + case 180 /* ArrowFunction */: + return "(Anonymous function)"; } - } - return undefined; - } - function isComputedNonLiteralName(name) { - return name.kind === 140 /* ComputedPropertyName */ && !ts.isStringOrNumericLiteral(name.expression.kind); - } - /** Return the inferred type for a binding element */ - function getTypeForBindingElement(declaration) { - var pattern = declaration.parent; - var parentType = getTypeForBindingElementParent(pattern.parent); - // If parent has the unknown (error) type, then so does this binding element - if (parentType === unknownType) { - return unknownType; - } - // If no type was specified or inferred for parent, or if the specified or inferred type is any, - // infer from the initializer of the binding element if one is present. Otherwise, go with the - // undefined or any type of the parent. - if (!parentType || isTypeAny(parentType)) { - if (declaration.initializer) { - return checkDeclarationInitializer(declaration); } - return parentType; + return symbol.name; } - var type; - if (pattern.kind === 167 /* ObjectBindingPattern */) { - // Use explicitly specified property name ({ p: xxx } form), or otherwise the implied name ({ p } form) - var name_14 = declaration.propertyName || declaration.name; - if (isComputedNonLiteralName(name_14)) { - // computed properties with non-literal names are treated as 'any' - return anyType; - } - if (declaration.initializer) { - getContextualType(declaration.initializer); - } - // Use type of the specified property, or otherwise, for a numeric name, the type of the numeric index signature, - // or otherwise the type of the string index signature. - var text = getTextOfPropertyName(name_14); - type = getTypeOfPropertyOfType(parentType, text) || - isNumericLiteralName(text) && getIndexTypeOfType(parentType, 1 /* Number */) || - getIndexTypeOfType(parentType, 0 /* String */); - if (!type) { - error(name_14, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(parentType), ts.declarationNameToString(name_14)); - return unknownType; - } + /** + * Writes only the name of the symbol out to the writer. Uses the original source text + * for the name of the symbol if it is available to match how the user wrote the name. + */ + function appendSymbolNameOnly(symbol, writer) { + writer.writeSymbol(getNameOfSymbol(symbol), symbol); } - else { - // This elementType will be used if the specific property corresponding to this index is not - // present (aka the tuple element property). This call also checks that the parentType is in - // fact an iterable or array (depending on target language). - var elementType = checkIteratedTypeOrElementType(parentType, pattern, /*allowStringInput*/ false); - if (!declaration.dotDotDotToken) { - // Use specific property type when parent is a tuple or numeric index type when parent is an array - var propName = "" + ts.indexOf(pattern.elements, declaration); - type = isTupleLikeType(parentType) - ? getTypeOfPropertyOfType(parentType, propName) - : elementType; - if (!type) { - if (isTupleType(parentType)) { - error(declaration, ts.Diagnostics.Tuple_type_0_with_length_1_cannot_be_assigned_to_tuple_with_length_2, typeToString(parentType), getTypeReferenceArity(parentType), pattern.elements.length); - } - else { - error(declaration, ts.Diagnostics.Type_0_has_no_property_1, typeToString(parentType), propName); - } - return unknownType; + /** + * Writes a property access or element access with the name of the symbol out to the writer. + * Uses the original source text for the name of the symbol if it is available to match how the user wrote the name, + * ensuring that any names written with literals use element accesses. + */ + function appendPropertyOrElementAccessForSymbol(symbol, writer) { + var symbolName = getNameOfSymbol(symbol); + var firstChar = symbolName.charCodeAt(0); + var needsElementAccess = !ts.isIdentifierStart(firstChar, languageVersion); + if (needsElementAccess) { + writePunctuation(writer, 19 /* OpenBracketToken */); + if (ts.isSingleOrDoubleQuote(firstChar)) { + writer.writeStringLiteral(symbolName); + } + else { + writer.writeSymbol(symbolName, symbol); } + writePunctuation(writer, 20 /* CloseBracketToken */); } else { - // Rest element has an array type with the same element type as the parent type - type = createArrayType(elementType); - } - } - // In strict null checking mode, if a default value of a non-undefined type is specified, remove - // undefined from the final type. - if (strictNullChecks && declaration.initializer && !(getFalsyFlags(checkExpressionCached(declaration.initializer)) & 2048 /* Undefined */)) { - type = getTypeWithFacts(type, 131072 /* NEUndefined */); - } - return declaration.initializer ? - getUnionType([type, checkExpressionCached(declaration.initializer)], /*subtypeReduction*/ true) : - type; - } - function getTypeForVariableLikeDeclarationFromJSDocComment(declaration) { - var jsDocType = getJSDocTypeForVariableLikeDeclarationFromJSDocComment(declaration); - if (jsDocType) { - return getTypeFromTypeNode(jsDocType); - } - } - function getJSDocTypeForVariableLikeDeclarationFromJSDocComment(declaration) { - // First, see if this node has an @type annotation on it directly. - var typeTag = ts.getJSDocTypeTag(declaration); - if (typeTag && typeTag.typeExpression) { - return typeTag.typeExpression.type; - } - if (declaration.kind === 218 /* VariableDeclaration */ && - declaration.parent.kind === 219 /* VariableDeclarationList */ && - declaration.parent.parent.kind === 200 /* VariableStatement */) { - // @type annotation might have been on the variable statement, try that instead. - var annotation = ts.getJSDocTypeTag(declaration.parent.parent); - if (annotation && annotation.typeExpression) { - return annotation.typeExpression.type; - } - } - else if (declaration.kind === 142 /* Parameter */) { - // If it's a parameter, see if the parent has a jsdoc comment with an @param - // annotation. - var paramTag = ts.getCorrespondingJSDocParameterTag(declaration); - if (paramTag && paramTag.typeExpression) { - return paramTag.typeExpression.type; + writePunctuation(writer, 21 /* DotToken */); + writer.writeSymbol(symbolName, symbol); } } - return undefined; - } - function addOptionality(type, optional) { - return strictNullChecks && optional ? includeFalsyTypes(type, 2048 /* Undefined */) : type; - } - // Return the inferred type for a variable, parameter, or property declaration - function getTypeForVariableLikeDeclaration(declaration, includeOptionality) { - if (declaration.flags & 1048576 /* JavaScriptFile */) { - // If this is a variable in a JavaScript file, then use the JSDoc type (if it has - // one as its type), otherwise fallback to the below standard TS codepaths to - // try to figure it out. - var type = getTypeForVariableLikeDeclarationFromJSDocComment(declaration); - if (type && type !== unknownType) { - return type; + /** + * Enclosing declaration is optional when we don't want to get qualified name in the enclosing declaration scope + * Meaning needs to be specified if the enclosing declaration is given + */ + function buildSymbolDisplay(symbol, writer, enclosingDeclaration, meaning, flags, typeFlags) { + var parentSymbol; + function appendParentTypeArgumentsAndSymbolName(symbol) { + if (parentSymbol) { + // Write type arguments of instantiated class/interface here + if (flags & 1 /* WriteTypeParametersOrArguments */) { + if (symbol.flags & 16777216 /* Instantiated */) { + buildDisplayForTypeArgumentsAndDelimiters(getTypeParametersOfClassOrInterface(parentSymbol), symbol.mapper, writer, enclosingDeclaration); + } + else { + buildTypeParameterDisplayFromSymbol(parentSymbol, writer, enclosingDeclaration); + } + } + appendPropertyOrElementAccessForSymbol(symbol, writer); + } + else { + appendSymbolNameOnly(symbol, writer); + } + parentSymbol = symbol; } - } - // A variable declared in a for..in statement is always of type string - if (declaration.parent.parent.kind === 207 /* ForInStatement */) { - return stringType; - } - if (declaration.parent.parent.kind === 208 /* ForOfStatement */) { - // checkRightHandSideOfForOf will return undefined if the for-of expression type was - // missing properties/signatures required to get its iteratedType (like - // [Symbol.iterator] or next). This may be because we accessed properties from anyType, - // or it may have led to an error inside getElementTypeOfIterable. - return checkRightHandSideOfForOf(declaration.parent.parent.expression) || anyType; - } - if (ts.isBindingPattern(declaration.parent)) { - return getTypeForBindingElement(declaration); - } - // Use type from type annotation if one is present - if (declaration.type) { - return addOptionality(getTypeFromTypeNode(declaration.type), /*optional*/ declaration.questionToken && includeOptionality); - } - if (declaration.kind === 142 /* Parameter */) { - var func = declaration.parent; - // For a parameter of a set accessor, use the type of the get accessor if one is present - if (func.kind === 150 /* SetAccessor */ && !ts.hasDynamicName(func)) { - var getter = ts.getDeclarationOfKind(declaration.parent.symbol, 149 /* GetAccessor */); - if (getter) { - var getterSignature = getSignatureFromDeclaration(getter); - var thisParameter = getAccessorThisParameter(func); - if (thisParameter && declaration === thisParameter) { - // Use the type from the *getter* - ts.Debug.assert(!thisParameter.type); - return getTypeOfSymbol(getterSignature.thisParameter); + // Let the writer know we just wrote out a symbol. The declaration emitter writer uses + // this to determine if an import it has previously seen (and not written out) needs + // to be written to the file once the walk of the tree is complete. + // + // NOTE(cyrusn): This approach feels somewhat unfortunate. A simple pass over the tree + // up front (for example, during checking) could determine if we need to emit the imports + // and we could then access that data during declaration emit. + writer.trackSymbol(symbol, enclosingDeclaration, meaning); + /** @param endOfChain Set to false for recursive calls; non-recursive calls should always output something. */ + function walkSymbol(symbol, meaning, endOfChain) { + var accessibleSymbolChain = getAccessibleSymbolChain(symbol, enclosingDeclaration, meaning, !!(flags & 2 /* UseOnlyExternalAliasing */)); + if (!accessibleSymbolChain || + needsQualification(accessibleSymbolChain[0], enclosingDeclaration, accessibleSymbolChain.length === 1 ? meaning : getQualifiedLeftMeaning(meaning))) { + // Go up and add our parent. + var parent_8 = getParentOfSymbol(accessibleSymbolChain ? accessibleSymbolChain[0] : symbol); + if (parent_8) { + walkSymbol(parent_8, getQualifiedLeftMeaning(meaning), /*endOfChain*/ false); } - return getReturnTypeOfSignature(getterSignature); + } + if (accessibleSymbolChain) { + for (var _i = 0, accessibleSymbolChain_1 = accessibleSymbolChain; _i < accessibleSymbolChain_1.length; _i++) { + var accessibleSymbol = accessibleSymbolChain_1[_i]; + appendParentTypeArgumentsAndSymbolName(accessibleSymbol); + } + } + else if ( + // If this is the last part of outputting the symbol, always output. The cases apply only to parent symbols. + endOfChain || + // If a parent symbol is an external module, don't write it. (We prefer just `x` vs `"foo/bar".x`.) + !(!parentSymbol && ts.forEach(symbol.declarations, hasExternalModuleSymbol)) && + // If a parent symbol is an anonymous type, don't write it. + !(symbol.flags & (2048 /* TypeLiteral */ | 4096 /* ObjectLiteral */))) { + appendParentTypeArgumentsAndSymbolName(symbol); } } - // Use contextual parameter type if one is available - var type = void 0; - if (declaration.symbol.name === "this") { - var thisParameter = getContextualThisParameter(func); - type = thisParameter ? getTypeOfSymbol(thisParameter) : undefined; + // Get qualified name if the symbol is not a type parameter + // and there is an enclosing declaration or we specifically + // asked for it + var isTypeParameter = symbol.flags & 262144 /* TypeParameter */; + var typeFormatFlag = 128 /* UseFullyQualifiedType */ & typeFlags; + if (!isTypeParameter && (enclosingDeclaration || typeFormatFlag)) { + walkSymbol(symbol, meaning, /*endOfChain*/ true); } else { - type = getContextuallyTypedParameterType(declaration); - } - if (type) { - return addOptionality(type, /*optional*/ declaration.questionToken && includeOptionality); - } - } - // Use the type of the initializer expression if one is present - if (declaration.initializer) { - var type = checkDeclarationInitializer(declaration); - return addOptionality(type, /*optional*/ declaration.questionToken && includeOptionality); - } - // If it is a short-hand property assignment, use the type of the identifier - if (declaration.kind === 254 /* ShorthandPropertyAssignment */) { - return checkIdentifier(declaration.name); - } - // If the declaration specifies a binding pattern, use the type implied by the binding pattern - if (ts.isBindingPattern(declaration.name)) { - return getTypeFromBindingPattern(declaration.name, /*includePatternInType*/ false, /*reportErrors*/ true); - } - // No type specified and nothing can be inferred - return undefined; - } - // Return the type implied by a binding pattern element. This is the type of the initializer of the element if - // one is present. Otherwise, if the element is itself a binding pattern, it is the type implied by the binding - // pattern. Otherwise, it is the type any. - function getTypeFromBindingElement(element, includePatternInType, reportErrors) { - if (element.initializer) { - return checkDeclarationInitializer(element); - } - if (ts.isBindingPattern(element.name)) { - return getTypeFromBindingPattern(element.name, includePatternInType, reportErrors); - } - if (reportErrors && compilerOptions.noImplicitAny && !declarationBelongsToPrivateAmbientMember(element)) { - reportImplicitAnyError(element, anyType); - } - return anyType; - } - // Return the type implied by an object binding pattern - function getTypeFromObjectBindingPattern(pattern, includePatternInType, reportErrors) { - var members = ts.createMap(); - var hasComputedProperties = false; - ts.forEach(pattern.elements, function (e) { - var name = e.propertyName || e.name; - if (isComputedNonLiteralName(name)) { - // do not include computed properties in the implied type - hasComputedProperties = true; - return; - } - var text = getTextOfPropertyName(name); - var flags = 4 /* Property */ | 67108864 /* Transient */ | (e.initializer ? 536870912 /* Optional */ : 0); - var symbol = createSymbol(flags, text); - symbol.type = getTypeFromBindingElement(e, includePatternInType, reportErrors); - symbol.bindingElement = e; - members[symbol.name] = symbol; - }); - var result = createAnonymousType(undefined, members, emptyArray, emptyArray, undefined, undefined); - if (includePatternInType) { - result.pattern = pattern; - } - if (hasComputedProperties) { - result.flags |= 536870912 /* ObjectLiteralPatternWithComputedProperties */; - } - return result; - } - // Return the type implied by an array binding pattern - function getTypeFromArrayBindingPattern(pattern, includePatternInType, reportErrors) { - var elements = pattern.elements; - var lastElement = ts.lastOrUndefined(elements); - if (elements.length === 0 || (!ts.isOmittedExpression(lastElement) && lastElement.dotDotDotToken)) { - return languageVersion >= 2 /* ES6 */ ? createIterableType(anyType) : anyArrayType; - } - // If the pattern has at least one element, and no rest element, then it should imply a tuple type. - var elementTypes = ts.map(elements, function (e) { return ts.isOmittedExpression(e) ? anyType : getTypeFromBindingElement(e, includePatternInType, reportErrors); }); - var result = createTupleType(elementTypes); - if (includePatternInType) { - result = cloneTypeReference(result); - result.pattern = pattern; - } - return result; - } - // Return the type implied by a binding pattern. This is the type implied purely by the binding pattern itself - // and without regard to its context (i.e. without regard any type annotation or initializer associated with the - // declaration in which the binding pattern is contained). For example, the implied type of [x, y] is [any, any] - // and the implied type of { x, y: z = 1 } is { x: any; y: number; }. The type implied by a binding pattern is - // used as the contextual type of an initializer associated with the binding pattern. Also, for a destructuring - // parameter with no type annotation or initializer, the type implied by the binding pattern becomes the type of - // the parameter. - function getTypeFromBindingPattern(pattern, includePatternInType, reportErrors) { - return pattern.kind === 167 /* ObjectBindingPattern */ - ? getTypeFromObjectBindingPattern(pattern, includePatternInType, reportErrors) - : getTypeFromArrayBindingPattern(pattern, includePatternInType, reportErrors); - } - // Return the type associated with a variable, parameter, or property declaration. In the simple case this is the type - // specified in a type annotation or inferred from an initializer. However, in the case of a destructuring declaration it - // is a bit more involved. For example: - // - // var [x, s = ""] = [1, "one"]; - // - // Here, the array literal [1, "one"] is contextually typed by the type [any, string], which is the implied type of the - // binding pattern [x, s = ""]. Because the contextual type is a tuple type, the resulting type of [1, "one"] is the - // tuple type [number, string]. Thus, the type inferred for 'x' is number and the type inferred for 's' is string. - function getWidenedTypeForVariableLikeDeclaration(declaration, reportErrors) { - var type = getTypeForVariableLikeDeclaration(declaration, /*includeOptionality*/ true); - if (type) { - if (reportErrors) { - reportErrorsFromWidening(declaration, type); - } - // During a normal type check we'll never get to here with a property assignment (the check of the containing - // object literal uses a different path). We exclude widening only so that language services and type verification - // tools see the actual type. - if (declaration.kind === 253 /* PropertyAssignment */) { - return type; - } - return getWidenedType(type); - } - // Rest parameters default to type any[], other parameters default to type any - type = declaration.dotDotDotToken ? anyArrayType : anyType; - // Report implicit any errors unless this is a private property within an ambient declaration - if (reportErrors && compilerOptions.noImplicitAny) { - if (!declarationBelongsToPrivateAmbientMember(declaration)) { - reportImplicitAnyError(declaration, type); + appendParentTypeArgumentsAndSymbolName(symbol); } } - return type; - } - function declarationBelongsToPrivateAmbientMember(declaration) { - var root = ts.getRootDeclaration(declaration); - var memberDeclaration = root.kind === 142 /* Parameter */ ? root.parent : root; - return isPrivateWithinAmbient(memberDeclaration); - } - function getTypeOfVariableOrParameterOrProperty(symbol) { - var links = getSymbolLinks(symbol); - if (!links.type) { - // Handle prototype property - if (symbol.flags & 134217728 /* Prototype */) { - return links.type = getTypeOfPrototypeProperty(symbol); - } - // Handle catch clause variables - var declaration = symbol.valueDeclaration; - if (declaration.parent.kind === 252 /* CatchClause */) { - return links.type = anyType; - } - // Handle export default expressions - if (declaration.kind === 235 /* ExportAssignment */) { - return links.type = checkExpression(declaration.expression); - } - if (declaration.flags & 1048576 /* JavaScriptFile */ && declaration.kind === 280 /* JSDocPropertyTag */ && declaration.typeExpression) { - return links.type = getTypeFromTypeNode(declaration.typeExpression.type); - } - // Handle variable, parameter or property - if (!pushTypeResolution(symbol, 0 /* Type */)) { - return unknownType; - } - var type = void 0; - // Handle certain special assignment kinds, which happen to union across multiple declarations: - // * module.exports = expr - // * exports.p = expr - // * this.p = expr - // * className.prototype.method = expr - if (declaration.kind === 187 /* BinaryExpression */ || - declaration.kind === 172 /* PropertyAccessExpression */ && declaration.parent.kind === 187 /* BinaryExpression */) { - // Use JS Doc type if present on parent expression statement - if (declaration.flags & 1048576 /* JavaScriptFile */) { - var typeTag = ts.getJSDocTypeTag(declaration.parent); - if (typeTag && typeTag.typeExpression) { - return links.type = getTypeFromTypeNode(typeTag.typeExpression.type); + function buildTypeDisplay(type, writer, enclosingDeclaration, globalFlags, symbolStack) { + var globalFlagsToPass = globalFlags & 16 /* WriteOwnNameForAnyLike */; + var inObjectTypeLiteral = false; + return writeType(type, globalFlags); + function writeType(type, flags) { + var nextFlags = flags & ~512 /* InTypeAlias */; + // Write undefined/null type as any + if (type.flags & 16015 /* Intrinsic */) { + // Special handling for unknown / resolving types, they should show up as any and not unknown or __resolving + writer.writeKeyword(!(globalFlags & 16 /* WriteOwnNameForAnyLike */) && isTypeAny(type) + ? "any" + : type.intrinsicName); + } + else if (type.flags & 16384 /* TypeParameter */ && type.isThisType) { + if (inObjectTypeLiteral) { + writer.reportInaccessibleThisError(); } + writer.writeKeyword("this"); + } + else if (type.flags & 131072 /* Reference */) { + writeTypeReference(type, nextFlags); + } + else if (type.flags & 256 /* EnumLiteral */) { + buildSymbolDisplay(getParentOfSymbol(type.symbol), writer, enclosingDeclaration, 793064 /* Type */, 0 /* None */, nextFlags); + writePunctuation(writer, 21 /* DotToken */); + appendSymbolNameOnly(type.symbol, writer); + } + else if (type.flags & (32768 /* Class */ | 65536 /* Interface */ | 16 /* Enum */ | 16384 /* TypeParameter */)) { + // The specified symbol flags need to be reinterpreted as type flags + buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, 793064 /* Type */, 0 /* None */, nextFlags); + } + else if (!(flags & 512 /* InTypeAlias */) && ((type.flags & 2097152 /* Anonymous */ && !type.target) || type.flags & 1572864 /* UnionOrIntersection */) && type.aliasSymbol && + isSymbolAccessible(type.aliasSymbol, enclosingDeclaration, 793064 /* Type */, /*shouldComputeAliasesToMakeVisible*/ false).accessibility === 0 /* Accessible */) { + // We emit inferred type as type-alias at the current localtion if all the following is true + // the input type is has alias symbol that is accessible + // the input type is a union, intersection or anonymous type that is fully instantiated (if not we want to keep dive into) + // e.g.: export type Bar = () => [X, Y]; + // export type Foo = Bar; + // export const y = (x: Foo) => 1 // we want to emit as ...x: () => [any, string]) + var typeArguments = type.aliasTypeArguments; + writeSymbolTypeReference(type.aliasSymbol, typeArguments, 0, typeArguments ? typeArguments.length : 0, nextFlags); + } + else if (type.flags & 1572864 /* UnionOrIntersection */) { + writeUnionOrIntersectionType(type, nextFlags); + } + else if (type.flags & 2097152 /* Anonymous */) { + writeAnonymousType(type, nextFlags); + } + else if (type.flags & 96 /* StringOrNumberLiteral */) { + writer.writeStringLiteral(literalTypeToString(type)); + } + else { + // Should never get here + // { ... } + writePunctuation(writer, 15 /* OpenBraceToken */); + writeSpace(writer); + writePunctuation(writer, 22 /* DotDotDotToken */); + writeSpace(writer); + writePunctuation(writer, 16 /* CloseBraceToken */); } - var declaredTypes = ts.map(symbol.declarations, function (decl) { return decl.kind === 187 /* BinaryExpression */ ? - checkExpressionCached(decl.right) : - checkExpressionCached(decl.parent.right); }); - type = getUnionType(declaredTypes, /*subtypeReduction*/ true); } - else { - type = getWidenedTypeForVariableLikeDeclaration(declaration, /*reportErrors*/ true); + function writeTypeList(types, delimiter) { + for (var i = 0; i < types.length; i++) { + if (i > 0) { + if (delimiter !== 24 /* CommaToken */) { + writeSpace(writer); + } + writePunctuation(writer, delimiter); + writeSpace(writer); + } + writeType(types[i], delimiter === 24 /* CommaToken */ ? 0 /* None */ : 64 /* InElementType */); + } } - if (!popTypeResolution()) { - if (symbol.valueDeclaration.type) { - // Variable has type annotation that circularly references the variable itself - type = unknownType; - error(symbol.valueDeclaration, ts.Diagnostics._0_is_referenced_directly_or_indirectly_in_its_own_type_annotation, symbolToString(symbol)); + function writeSymbolTypeReference(symbol, typeArguments, pos, end, flags) { + // Unnamed function expressions and arrow functions have reserved names that we don't want to display + if (symbol.flags & 32 /* Class */ || !isReservedMemberName(symbol.name)) { + buildSymbolDisplay(symbol, writer, enclosingDeclaration, 793064 /* Type */, 0 /* None */, flags); } - else { - // Variable has initializer that circularly references the variable itself - type = anyType; - if (compilerOptions.noImplicitAny) { - error(symbol.valueDeclaration, ts.Diagnostics._0_implicitly_has_type_any_because_it_does_not_have_a_type_annotation_and_is_referenced_directly_or_indirectly_in_its_own_initializer, symbolToString(symbol)); + if (pos < end) { + writePunctuation(writer, 25 /* LessThanToken */); + writeType(typeArguments[pos], 256 /* InFirstTypeArgument */); + pos++; + while (pos < end) { + writePunctuation(writer, 24 /* CommaToken */); + writeSpace(writer); + writeType(typeArguments[pos], 0 /* None */); + pos++; } + writePunctuation(writer, 27 /* GreaterThanToken */); } } - links.type = type; - } - return links.type; - } - function getAnnotatedAccessorType(accessor) { - if (accessor) { - if (accessor.kind === 149 /* GetAccessor */) { - return accessor.type && getTypeFromTypeNode(accessor.type); - } - else { - var setterTypeAnnotation = ts.getSetAccessorTypeAnnotationNode(accessor); - return setterTypeAnnotation && getTypeFromTypeNode(setterTypeAnnotation); - } - } - return undefined; - } - function getAnnotatedAccessorThisParameter(accessor) { - var parameter = getAccessorThisParameter(accessor); - return parameter && parameter.symbol; - } - function getThisTypeOfDeclaration(declaration) { - return getThisTypeOfSignature(getSignatureFromDeclaration(declaration)); - } - function getTypeOfAccessors(symbol) { - var links = getSymbolLinks(symbol); - if (!links.type) { - var getter = ts.getDeclarationOfKind(symbol, 149 /* GetAccessor */); - var setter = ts.getDeclarationOfKind(symbol, 150 /* SetAccessor */); - if (getter && getter.flags & 1048576 /* JavaScriptFile */) { - var jsDocType = getTypeForVariableLikeDeclarationFromJSDocComment(getter); - if (jsDocType) { - return links.type = jsDocType; + function writeTypeReference(type, flags) { + var typeArguments = type.typeArguments || emptyArray; + if (type.target === globalArrayType && !(flags & 1 /* WriteArrayAsGenericType */)) { + writeType(typeArguments[0], 64 /* InElementType */); + writePunctuation(writer, 19 /* OpenBracketToken */); + writePunctuation(writer, 20 /* CloseBracketToken */); } - } - if (!pushTypeResolution(symbol, 0 /* Type */)) { - return unknownType; - } - var type = void 0; - // First try to see if the user specified a return type on the get-accessor. - var getterReturnType = getAnnotatedAccessorType(getter); - if (getterReturnType) { - type = getterReturnType; - } - else { - // If the user didn't specify a return type, try to use the set-accessor's parameter type. - var setterParameterType = getAnnotatedAccessorType(setter); - if (setterParameterType) { - type = setterParameterType; + else if (type.target.flags & 262144 /* Tuple */) { + writePunctuation(writer, 19 /* OpenBracketToken */); + writeTypeList(type.typeArguments.slice(0, getTypeReferenceArity(type)), 24 /* CommaToken */); + writePunctuation(writer, 20 /* CloseBracketToken */); } else { - // If there are no specified types, try to infer it from the body of the get accessor if it exists. - if (getter && getter.body) { - type = getReturnTypeFromBody(getter); - } - else { - if (compilerOptions.noImplicitAny) { - if (setter) { - error(setter, ts.Diagnostics.Property_0_implicitly_has_type_any_because_its_set_accessor_lacks_a_parameter_type_annotation, symbolToString(symbol)); - } - else { - ts.Debug.assert(!!getter, "there must existed getter as we are current checking either setter or getter in this function"); - error(getter, ts.Diagnostics.Property_0_implicitly_has_type_any_because_its_get_accessor_lacks_a_return_type_annotation, symbolToString(symbol)); + // Write the type reference in the format f.g.C where A and B are type arguments + // for outer type parameters, and f and g are the respective declaring containers of those + // type parameters. + var outerTypeParameters = type.target.outerTypeParameters; + var i = 0; + if (outerTypeParameters) { + var length_1 = outerTypeParameters.length; + while (i < length_1) { + // Find group of type arguments for type parameters with the same declaring container. + var start = i; + var parent_9 = getParentSymbolOfTypeParameter(outerTypeParameters[i]); + do { + i++; + } while (i < length_1 && getParentSymbolOfTypeParameter(outerTypeParameters[i]) === parent_9); + // When type parameters are their own type arguments for the whole group (i.e. we have + // the default outer type arguments), we don't show the group. + if (!ts.rangeEquals(outerTypeParameters, typeArguments, start, i)) { + writeSymbolTypeReference(parent_9, typeArguments, start, i, flags); + writePunctuation(writer, 21 /* DotToken */); } } - type = anyType; } + var typeParameterCount = (type.target.typeParameters || emptyArray).length; + writeSymbolTypeReference(type.symbol, typeArguments, i, typeParameterCount, flags); } } - if (!popTypeResolution()) { - type = anyType; - if (compilerOptions.noImplicitAny) { - var getter_1 = ts.getDeclarationOfKind(symbol, 149 /* GetAccessor */); - error(getter_1, ts.Diagnostics._0_implicitly_has_return_type_any_because_it_does_not_have_a_return_type_annotation_and_is_referenced_directly_or_indirectly_in_one_of_its_return_expressions, symbolToString(symbol)); + function writeUnionOrIntersectionType(type, flags) { + if (flags & 64 /* InElementType */) { + writePunctuation(writer, 17 /* OpenParenToken */); + } + if (type.flags & 524288 /* Union */) { + writeTypeList(formatUnionTypes(type.types), 47 /* BarToken */); + } + else { + writeTypeList(type.types, 46 /* AmpersandToken */); + } + if (flags & 64 /* InElementType */) { + writePunctuation(writer, 18 /* CloseParenToken */); } } - links.type = type; - } - return links.type; - } - function getTypeOfFuncClassEnumModule(symbol) { - var links = getSymbolLinks(symbol); - if (!links.type) { - if (symbol.valueDeclaration.kind === 225 /* ModuleDeclaration */ && ts.isShorthandAmbientModuleSymbol(symbol)) { - links.type = anyType; - } - else { - var type = createObjectType(2097152 /* Anonymous */, symbol); - links.type = strictNullChecks && symbol.flags & 536870912 /* Optional */ ? - includeFalsyTypes(type, 2048 /* Undefined */) : type; + function writeAnonymousType(type, flags) { + var symbol = type.symbol; + if (symbol) { + // Always use 'typeof T' for type of class, enum, and module objects + if (symbol.flags & (32 /* Class */ | 384 /* Enum */ | 512 /* ValueModule */)) { + writeTypeOfSymbol(type, flags); + } + else if (shouldWriteTypeOfFunctionSymbol()) { + writeTypeOfSymbol(type, flags); + } + else if (ts.contains(symbolStack, symbol)) { + // If type is an anonymous type literal in a type alias declaration, use type alias name + var typeAlias = getTypeAliasForTypeLiteral(type); + if (typeAlias) { + // The specified symbol flags need to be reinterpreted as type flags + buildSymbolDisplay(typeAlias, writer, enclosingDeclaration, 793064 /* Type */, 0 /* None */, flags); + } + else { + // Recursive usage, use any + writeKeyword(writer, 117 /* AnyKeyword */); + } + } + else { + // Since instantiations of the same anonymous type have the same symbol, tracking symbols instead + // of types allows us to catch circular references to instantiations of the same anonymous type + if (!symbolStack) { + symbolStack = []; + } + symbolStack.push(symbol); + writeLiteralType(type, flags); + symbolStack.pop(); + } + } + else { + // Anonymous types with no symbol are never circular + writeLiteralType(type, flags); + } + function shouldWriteTypeOfFunctionSymbol() { + var isStaticMethodSymbol = !!(symbol.flags & 8192 /* Method */ && + ts.forEach(symbol.declarations, function (declaration) { return ts.getModifierFlags(declaration) & 32 /* Static */; })); + var isNonLocalFunctionSymbol = !!(symbol.flags & 16 /* Function */) && + (symbol.parent || + ts.forEach(symbol.declarations, function (declaration) { + return declaration.parent.kind === 256 /* SourceFile */ || declaration.parent.kind === 226 /* ModuleBlock */; + })); + if (isStaticMethodSymbol || isNonLocalFunctionSymbol) { + // typeof is allowed only for static/non local functions + return !!(flags & 2 /* UseTypeOfFunction */) || + (ts.contains(symbolStack, symbol)); // it is type of the symbol uses itself recursively + } + } } - } - return links.type; - } - function getTypeOfEnumMember(symbol) { - var links = getSymbolLinks(symbol); - if (!links.type) { - links.type = getDeclaredTypeOfEnumMember(symbol); - } - return links.type; - } - function getTypeOfAlias(symbol) { - var links = getSymbolLinks(symbol); - if (!links.type) { - var targetSymbol = resolveAlias(symbol); - // It only makes sense to get the type of a value symbol. If the result of resolving - // the alias is not a value, then it has no type. To get the type associated with a - // type symbol, call getDeclaredTypeOfSymbol. - // This check is important because without it, a call to getTypeOfSymbol could end - // up recursively calling getTypeOfAlias, causing a stack overflow. - links.type = targetSymbol.flags & 107455 /* Value */ - ? getTypeOfSymbol(targetSymbol) - : unknownType; - } - return links.type; - } - function getTypeOfInstantiatedSymbol(symbol) { - var links = getSymbolLinks(symbol); - if (!links.type) { - links.type = instantiateType(getTypeOfSymbol(links.target), links.mapper); - } - return links.type; - } - function getTypeOfSymbol(symbol) { - if (symbol.flags & 16777216 /* Instantiated */) { - return getTypeOfInstantiatedSymbol(symbol); - } - if (symbol.flags & (3 /* Variable */ | 4 /* Property */)) { - return getTypeOfVariableOrParameterOrProperty(symbol); - } - if (symbol.flags & (16 /* Function */ | 8192 /* Method */ | 32 /* Class */ | 384 /* Enum */ | 512 /* ValueModule */)) { - return getTypeOfFuncClassEnumModule(symbol); - } - if (symbol.flags & 8 /* EnumMember */) { - return getTypeOfEnumMember(symbol); - } - if (symbol.flags & 98304 /* Accessor */) { - return getTypeOfAccessors(symbol); - } - if (symbol.flags & 8388608 /* Alias */) { - return getTypeOfAlias(symbol); - } - return unknownType; - } - function getTargetType(type) { - return type.flags & 131072 /* Reference */ ? type.target : type; - } - function hasBaseType(type, checkBase) { - return check(type); - function check(type) { - var target = getTargetType(type); - return target === checkBase || ts.forEach(getBaseTypes(target), check); - } - } - // Appends the type parameters given by a list of declarations to a set of type parameters and returns the resulting set. - // The function allocates a new array if the input type parameter set is undefined, but otherwise it modifies the set - // in-place and returns the same array. - function appendTypeParameters(typeParameters, declarations) { - for (var _i = 0, declarations_2 = declarations; _i < declarations_2.length; _i++) { - var declaration = declarations_2[_i]; - var tp = getDeclaredTypeOfTypeParameter(getSymbolOfNode(declaration)); - if (!typeParameters) { - typeParameters = [tp]; + function writeTypeOfSymbol(type, typeFormatFlags) { + writeKeyword(writer, 101 /* TypeOfKeyword */); + writeSpace(writer); + buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, 107455 /* Value */, 0 /* None */, typeFormatFlags); } - else if (!ts.contains(typeParameters, tp)) { - typeParameters.push(tp); + function writeIndexSignature(info, keyword) { + if (info) { + if (info.isReadonly) { + writeKeyword(writer, 128 /* ReadonlyKeyword */); + writeSpace(writer); + } + writePunctuation(writer, 19 /* OpenBracketToken */); + writer.writeParameter(info.declaration ? ts.declarationNameToString(info.declaration.parameters[0].name) : "x"); + writePunctuation(writer, 54 /* ColonToken */); + writeSpace(writer); + writeKeyword(writer, keyword); + writePunctuation(writer, 20 /* CloseBracketToken */); + writePunctuation(writer, 54 /* ColonToken */); + writeSpace(writer); + writeType(info.type, 0 /* None */); + writePunctuation(writer, 23 /* SemicolonToken */); + writer.writeLine(); + } } - } - return typeParameters; - } - // Appends the outer type parameters of a node to a set of type parameters and returns the resulting set. The function - // allocates a new array if the input type parameter set is undefined, but otherwise it modifies the set in-place and - // returns the same array. - function appendOuterTypeParameters(typeParameters, node) { - while (true) { - node = node.parent; - if (!node) { - return typeParameters; + function writePropertyWithModifiers(prop) { + if (isReadonlySymbol(prop)) { + writeKeyword(writer, 128 /* ReadonlyKeyword */); + writeSpace(writer); + } + buildSymbolDisplay(prop, writer); + if (prop.flags & 536870912 /* Optional */) { + writePunctuation(writer, 53 /* QuestionToken */); + } } - if (node.kind === 221 /* ClassDeclaration */ || node.kind === 192 /* ClassExpression */ || - node.kind === 220 /* FunctionDeclaration */ || node.kind === 179 /* FunctionExpression */ || - node.kind === 147 /* MethodDeclaration */ || node.kind === 180 /* ArrowFunction */) { - var declarations = node.typeParameters; - if (declarations) { - return appendTypeParameters(appendOuterTypeParameters(typeParameters, node), declarations); + function shouldAddParenthesisAroundFunctionType(callSignature, flags) { + if (flags & 64 /* InElementType */) { + return true; } + else if (flags & 256 /* InFirstTypeArgument */) { + // Add parenthesis around function type for the first type argument to avoid ambiguity + var typeParameters = callSignature.target && (flags & 32 /* WriteTypeArgumentsOfSignature */) ? + callSignature.target.typeParameters : callSignature.typeParameters; + return typeParameters && typeParameters.length !== 0; + } + return false; } - } - } - // The outer type parameters are those defined by enclosing generic classes, methods, or functions. - function getOuterTypeParametersOfClassOrInterface(symbol) { - var declaration = symbol.flags & 32 /* Class */ ? symbol.valueDeclaration : ts.getDeclarationOfKind(symbol, 222 /* InterfaceDeclaration */); - return appendOuterTypeParameters(undefined, declaration); - } - // The local type parameters are the combined set of type parameters from all declarations of the class, - // interface, or type alias. - function getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol) { - var result; - for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { - var node = _a[_i]; - if (node.kind === 222 /* InterfaceDeclaration */ || node.kind === 221 /* ClassDeclaration */ || - node.kind === 192 /* ClassExpression */ || node.kind === 223 /* TypeAliasDeclaration */) { - var declaration = node; - if (declaration.typeParameters) { - result = appendTypeParameters(result, declaration.typeParameters); + function writeLiteralType(type, flags) { + var resolved = resolveStructuredTypeMembers(type); + if (!resolved.properties.length && !resolved.stringIndexInfo && !resolved.numberIndexInfo) { + if (!resolved.callSignatures.length && !resolved.constructSignatures.length) { + writePunctuation(writer, 15 /* OpenBraceToken */); + writePunctuation(writer, 16 /* CloseBraceToken */); + return; + } + if (resolved.callSignatures.length === 1 && !resolved.constructSignatures.length) { + var parenthesizeSignature = shouldAddParenthesisAroundFunctionType(resolved.callSignatures[0], flags); + if (parenthesizeSignature) { + writePunctuation(writer, 17 /* OpenParenToken */); + } + buildSignatureDisplay(resolved.callSignatures[0], writer, enclosingDeclaration, globalFlagsToPass | 8 /* WriteArrowStyleSignature */, /*kind*/ undefined, symbolStack); + if (parenthesizeSignature) { + writePunctuation(writer, 18 /* CloseParenToken */); + } + return; + } + if (resolved.constructSignatures.length === 1 && !resolved.callSignatures.length) { + if (flags & 64 /* InElementType */) { + writePunctuation(writer, 17 /* OpenParenToken */); + } + writeKeyword(writer, 92 /* NewKeyword */); + writeSpace(writer); + buildSignatureDisplay(resolved.constructSignatures[0], writer, enclosingDeclaration, globalFlagsToPass | 8 /* WriteArrowStyleSignature */, /*kind*/ undefined, symbolStack); + if (flags & 64 /* InElementType */) { + writePunctuation(writer, 18 /* CloseParenToken */); + } + return; + } + } + var saveInObjectTypeLiteral = inObjectTypeLiteral; + inObjectTypeLiteral = true; + writePunctuation(writer, 15 /* OpenBraceToken */); + writer.writeLine(); + writer.increaseIndent(); + for (var _i = 0, _a = resolved.callSignatures; _i < _a.length; _i++) { + var signature = _a[_i]; + buildSignatureDisplay(signature, writer, enclosingDeclaration, globalFlagsToPass, /*kind*/ undefined, symbolStack); + writePunctuation(writer, 23 /* SemicolonToken */); + writer.writeLine(); + } + for (var _b = 0, _c = resolved.constructSignatures; _b < _c.length; _b++) { + var signature = _c[_b]; + buildSignatureDisplay(signature, writer, enclosingDeclaration, globalFlagsToPass, 1 /* Construct */, symbolStack); + writePunctuation(writer, 23 /* SemicolonToken */); + writer.writeLine(); + } + writeIndexSignature(resolved.stringIndexInfo, 132 /* StringKeyword */); + writeIndexSignature(resolved.numberIndexInfo, 130 /* NumberKeyword */); + for (var _d = 0, _e = resolved.properties; _d < _e.length; _d++) { + var p = _e[_d]; + var t = getTypeOfSymbol(p); + if (p.flags & (16 /* Function */ | 8192 /* Method */) && !getPropertiesOfObjectType(t).length) { + var signatures = getSignaturesOfType(t, 0 /* Call */); + for (var _f = 0, signatures_1 = signatures; _f < signatures_1.length; _f++) { + var signature = signatures_1[_f]; + writePropertyWithModifiers(p); + buildSignatureDisplay(signature, writer, enclosingDeclaration, globalFlagsToPass, /*kind*/ undefined, symbolStack); + writePunctuation(writer, 23 /* SemicolonToken */); + writer.writeLine(); + } + } + else { + writePropertyWithModifiers(p); + writePunctuation(writer, 54 /* ColonToken */); + writeSpace(writer); + writeType(t, 0 /* None */); + writePunctuation(writer, 23 /* SemicolonToken */); + writer.writeLine(); + } } + writer.decreaseIndent(); + writePunctuation(writer, 16 /* CloseBraceToken */); + inObjectTypeLiteral = saveInObjectTypeLiteral; } } - return result; - } - // The full set of type parameters for a generic class or interface type consists of its outer type parameters plus - // its locally declared type parameters. - function getTypeParametersOfClassOrInterface(symbol) { - return ts.concatenate(getOuterTypeParametersOfClassOrInterface(symbol), getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol)); - } - function isConstructorType(type) { - return type.flags & 2588672 /* ObjectType */ && getSignaturesOfType(type, 1 /* Construct */).length > 0; - } - function getBaseTypeNodeOfClass(type) { - return ts.getClassExtendsHeritageClauseElement(type.symbol.valueDeclaration); - } - function getConstructorsForTypeArguments(type, typeArgumentNodes) { - var typeArgCount = typeArgumentNodes ? typeArgumentNodes.length : 0; - return ts.filter(getSignaturesOfType(type, 1 /* Construct */), function (sig) { return (sig.typeParameters ? sig.typeParameters.length : 0) === typeArgCount; }); - } - function getInstantiatedConstructorsForTypeArguments(type, typeArgumentNodes) { - var signatures = getConstructorsForTypeArguments(type, typeArgumentNodes); - if (typeArgumentNodes) { - var typeArguments_1 = ts.map(typeArgumentNodes, getTypeFromTypeNodeNoAlias); - signatures = ts.map(signatures, function (sig) { return getSignatureInstantiation(sig, typeArguments_1); }); + function buildTypeParameterDisplayFromSymbol(symbol, writer, enclosingDeclaration, flags) { + var targetSymbol = getTargetSymbol(symbol); + if (targetSymbol.flags & 32 /* Class */ || targetSymbol.flags & 64 /* Interface */ || targetSymbol.flags & 524288 /* TypeAlias */) { + buildDisplayForTypeParametersAndDelimiters(getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol), writer, enclosingDeclaration, flags); + } } - return signatures; - } - // The base constructor of a class can resolve to - // undefinedType if the class has no extends clause, - // unknownType if an error occurred during resolution of the extends expression, - // nullType if the extends expression is the null value, or - // an object type with at least one construct signature. - function getBaseConstructorTypeOfClass(type) { - if (!type.resolvedBaseConstructorType) { - var baseTypeNode = getBaseTypeNodeOfClass(type); - if (!baseTypeNode) { - return type.resolvedBaseConstructorType = undefinedType; + function buildTypeParameterDisplay(tp, writer, enclosingDeclaration, flags, symbolStack) { + appendSymbolNameOnly(tp.symbol, writer); + var constraint = getConstraintOfTypeParameter(tp); + if (constraint) { + writeSpace(writer); + writeKeyword(writer, 83 /* ExtendsKeyword */); + writeSpace(writer); + buildTypeDisplay(constraint, writer, enclosingDeclaration, flags, symbolStack); } - if (!pushTypeResolution(type, 1 /* ResolvedBaseConstructorType */)) { - return unknownType; + } + function buildParameterDisplay(p, writer, enclosingDeclaration, flags, symbolStack) { + var parameterNode = p.valueDeclaration; + if (ts.isRestParameter(parameterNode)) { + writePunctuation(writer, 22 /* DotDotDotToken */); } - var baseConstructorType = checkExpression(baseTypeNode.expression); - if (baseConstructorType.flags & 2588672 /* ObjectType */) { - // Resolving the members of a class requires us to resolve the base class of that class. - // We force resolution here such that we catch circularities now. - resolveStructuredTypeMembers(baseConstructorType); + if (ts.isBindingPattern(parameterNode.name)) { + buildBindingPatternDisplay(parameterNode.name, writer, enclosingDeclaration, flags, symbolStack); } - if (!popTypeResolution()) { - error(type.symbol.valueDeclaration, ts.Diagnostics._0_is_referenced_directly_or_indirectly_in_its_own_base_expression, symbolToString(type.symbol)); - return type.resolvedBaseConstructorType = unknownType; + else { + appendSymbolNameOnly(p, writer); } - if (baseConstructorType !== unknownType && baseConstructorType !== nullWideningType && !isConstructorType(baseConstructorType)) { - error(baseTypeNode.expression, ts.Diagnostics.Type_0_is_not_a_constructor_function_type, typeToString(baseConstructorType)); - return type.resolvedBaseConstructorType = unknownType; + if (isOptionalParameter(parameterNode)) { + writePunctuation(writer, 53 /* QuestionToken */); } - type.resolvedBaseConstructorType = baseConstructorType; + writePunctuation(writer, 54 /* ColonToken */); + writeSpace(writer); + buildTypeDisplay(getTypeOfSymbol(p), writer, enclosingDeclaration, flags, symbolStack); } - return type.resolvedBaseConstructorType; - } - function getBaseTypes(type) { - if (!type.resolvedBaseTypes) { - if (type.flags & 262144 /* Tuple */) { - type.resolvedBaseTypes = [createArrayType(getUnionType(type.typeParameters))]; + function buildBindingPatternDisplay(bindingPattern, writer, enclosingDeclaration, flags, symbolStack) { + // We have to explicitly emit square bracket and bracket because these tokens are not stored inside the node. + if (bindingPattern.kind === 167 /* ObjectBindingPattern */) { + writePunctuation(writer, 15 /* OpenBraceToken */); + buildDisplayForCommaSeparatedList(bindingPattern.elements, writer, function (e) { return buildBindingElementDisplay(e, writer, enclosingDeclaration, flags, symbolStack); }); + writePunctuation(writer, 16 /* CloseBraceToken */); } - else if (type.symbol.flags & (32 /* Class */ | 64 /* Interface */)) { - if (type.symbol.flags & 32 /* Class */) { - resolveBaseTypesOfClass(type); - } - if (type.symbol.flags & 64 /* Interface */) { - resolveBaseTypesOfInterface(type); + else if (bindingPattern.kind === 168 /* ArrayBindingPattern */) { + writePunctuation(writer, 19 /* OpenBracketToken */); + var elements = bindingPattern.elements; + buildDisplayForCommaSeparatedList(elements, writer, function (e) { return buildBindingElementDisplay(e, writer, enclosingDeclaration, flags, symbolStack); }); + if (elements && elements.hasTrailingComma) { + writePunctuation(writer, 24 /* CommaToken */); } + writePunctuation(writer, 20 /* CloseBracketToken */); + } + } + function buildBindingElementDisplay(bindingElement, writer, enclosingDeclaration, flags, symbolStack) { + if (ts.isOmittedExpression(bindingElement)) { + return; + } + ts.Debug.assert(bindingElement.kind === 169 /* BindingElement */); + if (bindingElement.propertyName) { + writer.writeSymbol(ts.getTextOfNode(bindingElement.propertyName), bindingElement.symbol); + writePunctuation(writer, 54 /* ColonToken */); + writeSpace(writer); + } + if (ts.isBindingPattern(bindingElement.name)) { + buildBindingPatternDisplay(bindingElement.name, writer, enclosingDeclaration, flags, symbolStack); } else { - ts.Debug.fail("type must be class or interface"); + if (bindingElement.dotDotDotToken) { + writePunctuation(writer, 22 /* DotDotDotToken */); + } + appendSymbolNameOnly(bindingElement.symbol, writer); } } - return type.resolvedBaseTypes; - } - function resolveBaseTypesOfClass(type) { - type.resolvedBaseTypes = type.resolvedBaseTypes || emptyArray; - var baseConstructorType = getBaseConstructorTypeOfClass(type); - if (!(baseConstructorType.flags & 2588672 /* ObjectType */)) { - return; - } - var baseTypeNode = getBaseTypeNodeOfClass(type); - var baseType; - var originalBaseType = baseConstructorType && baseConstructorType.symbol ? getDeclaredTypeOfSymbol(baseConstructorType.symbol) : undefined; - if (baseConstructorType.symbol && baseConstructorType.symbol.flags & 32 /* Class */ && - areAllOuterTypeParametersApplied(originalBaseType)) { - // When base constructor type is a class with no captured type arguments we know that the constructors all have the same type parameters as the - // class and all return the instance type of the class. There is no need for further checks and we can apply the - // type arguments in the same manner as a type reference to get the same error reporting experience. - baseType = getTypeFromClassOrInterfaceReference(baseTypeNode, baseConstructorType.symbol); - } - else { - // The class derives from a "class-like" constructor function, check that we have at least one construct signature - // with a matching number of type parameters and use the return type of the first instantiated signature. Elsewhere - // we check that all instantiated signatures return the same type. - var constructors = getInstantiatedConstructorsForTypeArguments(baseConstructorType, baseTypeNode.typeArguments); - if (!constructors.length) { - error(baseTypeNode.expression, ts.Diagnostics.No_base_constructor_has_the_specified_number_of_type_arguments); - return; + function buildDisplayForTypeParametersAndDelimiters(typeParameters, writer, enclosingDeclaration, flags, symbolStack) { + if (typeParameters && typeParameters.length) { + writePunctuation(writer, 25 /* LessThanToken */); + buildDisplayForCommaSeparatedList(typeParameters, writer, function (p) { return buildTypeParameterDisplay(p, writer, enclosingDeclaration, flags, symbolStack); }); + writePunctuation(writer, 27 /* GreaterThanToken */); } - baseType = getReturnTypeOfSignature(constructors[0]); - } - if (baseType === unknownType) { - return; - } - if (!(getTargetType(baseType).flags & (32768 /* Class */ | 65536 /* Interface */))) { - error(baseTypeNode.expression, ts.Diagnostics.Base_constructor_return_type_0_is_not_a_class_or_interface_type, typeToString(baseType)); - return; - } - if (type === baseType || hasBaseType(baseType, type)) { - error(type.symbol.valueDeclaration, ts.Diagnostics.Type_0_recursively_references_itself_as_a_base_type, typeToString(type, /*enclosingDeclaration*/ undefined, 1 /* WriteArrayAsGenericType */)); - return; - } - if (type.resolvedBaseTypes === emptyArray) { - type.resolvedBaseTypes = [baseType]; - } - else { - type.resolvedBaseTypes.push(baseType); } - } - function areAllOuterTypeParametersApplied(type) { - // An unapplied type parameter has its symbol still the same as the matching argument symbol. - // Since parameters are applied outer-to-inner, only the last outer parameter needs to be checked. - var outerTypeParameters = type.outerTypeParameters; - if (outerTypeParameters) { - var last = outerTypeParameters.length - 1; - var typeArguments = type.typeArguments; - return outerTypeParameters[last].symbol !== typeArguments[last].symbol; + function buildDisplayForCommaSeparatedList(list, writer, action) { + for (var i = 0; i < list.length; i++) { + if (i > 0) { + writePunctuation(writer, 24 /* CommaToken */); + writeSpace(writer); + } + action(list[i]); + } } - return true; - } - function resolveBaseTypesOfInterface(type) { - type.resolvedBaseTypes = type.resolvedBaseTypes || emptyArray; - for (var _i = 0, _a = type.symbol.declarations; _i < _a.length; _i++) { - var declaration = _a[_i]; - if (declaration.kind === 222 /* InterfaceDeclaration */ && ts.getInterfaceBaseTypeNodes(declaration)) { - for (var _b = 0, _c = ts.getInterfaceBaseTypeNodes(declaration); _b < _c.length; _b++) { - var node = _c[_b]; - var baseType = getTypeFromTypeNode(node); - if (baseType !== unknownType) { - if (getTargetType(baseType).flags & (32768 /* Class */ | 65536 /* Interface */)) { - if (type !== baseType && !hasBaseType(baseType, type)) { - if (type.resolvedBaseTypes === emptyArray) { - type.resolvedBaseTypes = [baseType]; - } - else { - type.resolvedBaseTypes.push(baseType); - } - } - else { - error(declaration, ts.Diagnostics.Type_0_recursively_references_itself_as_a_base_type, typeToString(type, /*enclosingDeclaration*/ undefined, 1 /* WriteArrayAsGenericType */)); - } - } - else { - error(node, ts.Diagnostics.An_interface_may_only_extend_a_class_or_another_interface); - } + function buildDisplayForTypeArgumentsAndDelimiters(typeParameters, mapper, writer, enclosingDeclaration, flags, symbolStack) { + if (typeParameters && typeParameters.length) { + writePunctuation(writer, 25 /* LessThanToken */); + var flags_1 = 256 /* InFirstTypeArgument */; + for (var i = 0; i < typeParameters.length; i++) { + if (i > 0) { + writePunctuation(writer, 24 /* CommaToken */); + writeSpace(writer); + flags_1 = 0 /* None */; } + buildTypeDisplay(mapper(typeParameters[i]), writer, enclosingDeclaration, flags_1); } + writePunctuation(writer, 27 /* GreaterThanToken */); } } - } - // Returns true if the interface given by the symbol is free of "this" references. Specifically, the result is - // true if the interface itself contains no references to "this" in its body, if all base types are interfaces, - // and if none of the base interfaces have a "this" type. - function isIndependentInterface(symbol) { - for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { - var declaration = _a[_i]; - if (declaration.kind === 222 /* InterfaceDeclaration */) { - if (declaration.flags & 64 /* ContainsThis */) { - return false; - } - var baseTypeNodes = ts.getInterfaceBaseTypeNodes(declaration); - if (baseTypeNodes) { - for (var _b = 0, baseTypeNodes_1 = baseTypeNodes; _b < baseTypeNodes_1.length; _b++) { - var node = baseTypeNodes_1[_b]; - if (ts.isEntityNameExpression(node.expression)) { - var baseSymbol = resolveEntityName(node.expression, 793064 /* Type */, /*ignoreErrors*/ true); - if (!baseSymbol || !(baseSymbol.flags & 64 /* Interface */) || getDeclaredTypeOfClassOrInterface(baseSymbol).thisType) { - return false; - } - } - } + function buildDisplayForParametersAndDelimiters(thisParameter, parameters, writer, enclosingDeclaration, flags, symbolStack) { + writePunctuation(writer, 17 /* OpenParenToken */); + if (thisParameter) { + buildParameterDisplay(thisParameter, writer, enclosingDeclaration, flags, symbolStack); + } + for (var i = 0; i < parameters.length; i++) { + if (i > 0 || thisParameter) { + writePunctuation(writer, 24 /* CommaToken */); + writeSpace(writer); } + buildParameterDisplay(parameters[i], writer, enclosingDeclaration, flags, symbolStack); } + writePunctuation(writer, 18 /* CloseParenToken */); } - return true; - } - function getDeclaredTypeOfClassOrInterface(symbol) { - var links = getSymbolLinks(symbol); - if (!links.declaredType) { - var kind = symbol.flags & 32 /* Class */ ? 32768 /* Class */ : 65536 /* Interface */; - var type = links.declaredType = createObjectType(kind, symbol); - var outerTypeParameters = getOuterTypeParametersOfClassOrInterface(symbol); - var localTypeParameters = getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol); - // A class or interface is generic if it has type parameters or a "this" type. We always give classes a "this" type - // because it is not feasible to analyze all members to determine if the "this" type escapes the class (in particular, - // property types inferred from initializers and method return types inferred from return statements are very hard - // to exhaustively analyze). We give interfaces a "this" type if we can't definitely determine that they are free of - // "this" references. - if (outerTypeParameters || localTypeParameters || kind === 32768 /* Class */ || !isIndependentInterface(symbol)) { - type.flags |= 131072 /* Reference */; - type.typeParameters = ts.concatenate(outerTypeParameters, localTypeParameters); - type.outerTypeParameters = outerTypeParameters; - type.localTypeParameters = localTypeParameters; - type.instantiations = ts.createMap(); - type.instantiations[getTypeListId(type.typeParameters)] = type; - type.target = type; - type.typeArguments = type.typeParameters; - type.thisType = createType(16384 /* TypeParameter */ | 268435456 /* ThisType */); - type.thisType.symbol = symbol; - type.thisType.constraint = type; + function buildTypePredicateDisplay(predicate, writer, enclosingDeclaration, flags, symbolStack) { + if (ts.isIdentifierTypePredicate(predicate)) { + writer.writeParameter(predicate.parameterName); } + else { + writeKeyword(writer, 97 /* ThisKeyword */); + } + writeSpace(writer); + writeKeyword(writer, 124 /* IsKeyword */); + writeSpace(writer); + buildTypeDisplay(predicate.type, writer, enclosingDeclaration, flags, symbolStack); } - return links.declaredType; - } - function getDeclaredTypeOfTypeAlias(symbol) { - var links = getSymbolLinks(symbol); - if (!links.declaredType) { - // Note that we use the links object as the target here because the symbol object is used as the unique - // identity for resolution of the 'type' property in SymbolLinks. - if (!pushTypeResolution(symbol, 2 /* DeclaredType */)) { - return unknownType; + function buildReturnTypeDisplay(signature, writer, enclosingDeclaration, flags, symbolStack) { + if (flags & 8 /* WriteArrowStyleSignature */) { + writeSpace(writer); + writePunctuation(writer, 34 /* EqualsGreaterThanToken */); } - var typeParameters = getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol); - var declaration = ts.getDeclarationOfKind(symbol, 279 /* JSDocTypedefTag */); - var type = void 0; - if (declaration) { - if (declaration.jsDocTypeLiteral) { - type = getTypeFromTypeNode(declaration.jsDocTypeLiteral); - } - else { - type = getTypeFromTypeNode(declaration.typeExpression.type); - } + else { + writePunctuation(writer, 54 /* ColonToken */); + } + writeSpace(writer); + if (signature.typePredicate) { + buildTypePredicateDisplay(signature.typePredicate, writer, enclosingDeclaration, flags, symbolStack); } else { - declaration = ts.getDeclarationOfKind(symbol, 223 /* TypeAliasDeclaration */); - type = getTypeFromTypeNode(declaration.type, symbol, typeParameters); + var returnType = getReturnTypeOfSignature(signature); + buildTypeDisplay(returnType, writer, enclosingDeclaration, flags, symbolStack); } - if (popTypeResolution()) { - links.typeParameters = typeParameters; - if (typeParameters) { - // Initialize the instantiation cache for generic type aliases. The declared type corresponds to - // an instantiation of the type alias with the type parameters supplied as type arguments. - links.instantiations = ts.createMap(); - links.instantiations[getTypeListId(links.typeParameters)] = type; - } + } + function buildSignatureDisplay(signature, writer, enclosingDeclaration, flags, kind, symbolStack) { + if (kind === 1 /* Construct */) { + writeKeyword(writer, 92 /* NewKeyword */); + writeSpace(writer); + } + if (signature.target && (flags & 32 /* WriteTypeArgumentsOfSignature */)) { + // Instantiated signature, write type arguments instead + // This is achieved by passing in the mapper separately + buildDisplayForTypeArgumentsAndDelimiters(signature.target.typeParameters, signature.mapper, writer, enclosingDeclaration); } else { - type = unknownType; - error(declaration.name, ts.Diagnostics.Type_alias_0_circularly_references_itself, symbolToString(symbol)); + buildDisplayForTypeParametersAndDelimiters(signature.typeParameters, writer, enclosingDeclaration, flags, symbolStack); } - links.declaredType = type; + buildDisplayForParametersAndDelimiters(signature.thisParameter, signature.parameters, writer, enclosingDeclaration, flags, symbolStack); + buildReturnTypeDisplay(signature, writer, enclosingDeclaration, flags, symbolStack); } - return links.declaredType; + return _displayBuilder || (_displayBuilder = { + buildSymbolDisplay: buildSymbolDisplay, + buildTypeDisplay: buildTypeDisplay, + buildTypeParameterDisplay: buildTypeParameterDisplay, + buildTypePredicateDisplay: buildTypePredicateDisplay, + buildParameterDisplay: buildParameterDisplay, + buildDisplayForParametersAndDelimiters: buildDisplayForParametersAndDelimiters, + buildDisplayForTypeParametersAndDelimiters: buildDisplayForTypeParametersAndDelimiters, + buildTypeParameterDisplayFromSymbol: buildTypeParameterDisplayFromSymbol, + buildSignatureDisplay: buildSignatureDisplay, + buildReturnTypeDisplay: buildReturnTypeDisplay + }); } - function isLiteralEnumMember(symbol, member) { - var expr = member.initializer; - if (!expr) { - return !ts.isInAmbientContext(member); + function isDeclarationVisible(node) { + if (node) { + var links = getNodeLinks(node); + if (links.isVisible === undefined) { + links.isVisible = !!determineIfDeclarationIsVisible(); + } + return links.isVisible; } - return expr.kind === 8 /* NumericLiteral */ || - expr.kind === 185 /* PrefixUnaryExpression */ && expr.operator === 36 /* MinusToken */ && - expr.operand.kind === 8 /* NumericLiteral */ || - expr.kind === 69 /* Identifier */ && !!symbol.exports[expr.text]; - } - function enumHasLiteralMembers(symbol) { - for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { - var declaration = _a[_i]; - if (declaration.kind === 224 /* EnumDeclaration */) { - for (var _b = 0, _c = declaration.members; _b < _c.length; _b++) { - var member = _c[_b]; - if (!isLiteralEnumMember(symbol, member)) { + return false; + function determineIfDeclarationIsVisible() { + switch (node.kind) { + case 169 /* BindingElement */: + return isDeclarationVisible(node.parent.parent); + case 218 /* VariableDeclaration */: + if (ts.isBindingPattern(node.name) && + !node.name.elements.length) { + // If the binding pattern is empty, this variable declaration is not visible return false; } - } + // Otherwise fall through + case 225 /* ModuleDeclaration */: + case 221 /* ClassDeclaration */: + case 222 /* InterfaceDeclaration */: + case 223 /* TypeAliasDeclaration */: + case 220 /* FunctionDeclaration */: + case 224 /* EnumDeclaration */: + case 229 /* ImportEqualsDeclaration */: + // external module augmentation is always visible + if (ts.isExternalModuleAugmentation(node)) { + return true; + } + var parent_10 = getDeclarationContainer(node); + // If the node is not exported or it is not ambient module element (except import declaration) + if (!(ts.getCombinedModifierFlags(node) & 1 /* Export */) && + !(node.kind !== 229 /* ImportEqualsDeclaration */ && parent_10.kind !== 256 /* SourceFile */ && ts.isInAmbientContext(parent_10))) { + return isGlobalSourceFile(parent_10); + } + // Exported members/ambient module elements (exception import declaration) are visible if parent is visible + return isDeclarationVisible(parent_10); + case 145 /* PropertyDeclaration */: + case 144 /* PropertySignature */: + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + case 147 /* MethodDeclaration */: + case 146 /* MethodSignature */: + if (ts.getModifierFlags(node) & (8 /* Private */ | 16 /* Protected */)) { + // Private/protected properties/methods are not visible + return false; + } + // Public properties/methods are visible if its parents are visible, so const it fall into next case statement + case 148 /* Constructor */: + case 152 /* ConstructSignature */: + case 151 /* CallSignature */: + case 153 /* IndexSignature */: + case 142 /* Parameter */: + case 226 /* ModuleBlock */: + case 156 /* FunctionType */: + case 157 /* ConstructorType */: + case 159 /* TypeLiteral */: + case 155 /* TypeReference */: + case 160 /* ArrayType */: + case 161 /* TupleType */: + case 162 /* UnionType */: + case 163 /* IntersectionType */: + case 164 /* ParenthesizedType */: + return isDeclarationVisible(node.parent); + // Default binding, import specifier and namespace import is visible + // only on demand so by default it is not visible + case 231 /* ImportClause */: + case 232 /* NamespaceImport */: + case 234 /* ImportSpecifier */: + return false; + // Type parameters are always visible + case 141 /* TypeParameter */: + // Source file and namespace export are always visible + case 256 /* SourceFile */: + case 228 /* NamespaceExportDeclaration */: + return true; + // Export assignments do not create name bindings outside the module + case 235 /* ExportAssignment */: + return false; + default: + return false; } } - return true; - } - function createEnumLiteralType(symbol, baseType, text) { - var type = createType(256 /* EnumLiteral */); - type.symbol = symbol; - type.baseType = baseType; - type.text = text; - return type; } - function getDeclaredTypeOfEnum(symbol) { - var links = getSymbolLinks(symbol); - if (!links.declaredType) { - var enumType = links.declaredType = createType(16 /* Enum */); - enumType.symbol = symbol; - if (enumHasLiteralMembers(symbol)) { - var memberTypeList = []; - var memberTypes = ts.createMap(); - for (var _i = 0, _a = enumType.symbol.declarations; _i < _a.length; _i++) { - var declaration = _a[_i]; - if (declaration.kind === 224 /* EnumDeclaration */) { - computeEnumMemberValues(declaration); - for (var _b = 0, _c = declaration.members; _b < _c.length; _b++) { - var member = _c[_b]; - var memberSymbol = getSymbolOfNode(member); - var value = getEnumMemberValue(member); - if (!memberTypes[value]) { - var memberType = memberTypes[value] = createEnumLiteralType(memberSymbol, enumType, "" + value); - memberTypeList.push(memberType); - } - } - } + function collectLinkedAliases(node) { + var exportSymbol; + if (node.parent && node.parent.kind === 235 /* ExportAssignment */) { + exportSymbol = resolveName(node.parent, node.text, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */ | 8388608 /* Alias */, ts.Diagnostics.Cannot_find_name_0, node); + } + else if (node.parent.kind === 238 /* ExportSpecifier */) { + var exportSpecifier = node.parent; + exportSymbol = exportSpecifier.parent.parent.moduleSpecifier ? + getExternalModuleMember(exportSpecifier.parent.parent, exportSpecifier) : + resolveEntityName(exportSpecifier.propertyName || exportSpecifier.name, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */ | 8388608 /* Alias */); + } + var result = []; + if (exportSymbol) { + buildVisibleNodeList(exportSymbol.declarations); + } + return result; + function buildVisibleNodeList(declarations) { + ts.forEach(declarations, function (declaration) { + getNodeLinks(declaration).isVisible = true; + var resultNode = getAnyImportSyntax(declaration) || declaration; + if (!ts.contains(result, resultNode)) { + result.push(resultNode); } - enumType.memberTypes = memberTypes; - if (memberTypeList.length > 1) { - enumType.flags |= 524288 /* Union */; - enumType.types = memberTypeList; - unionTypes[getTypeListId(memberTypeList)] = enumType; + if (ts.isInternalModuleImportEqualsDeclaration(declaration)) { + // Add the referenced top container visible + var internalModuleReference = declaration.moduleReference; + var firstIdentifier = getFirstIdentifier(internalModuleReference); + var importSymbol = resolveName(declaration, firstIdentifier.text, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */, undefined, undefined); + if (importSymbol) { + buildVisibleNodeList(importSymbol.declarations); + } } - } - } - return links.declaredType; - } - function getDeclaredTypeOfEnumMember(symbol) { - var links = getSymbolLinks(symbol); - if (!links.declaredType) { - var enumType = getDeclaredTypeOfEnum(getParentOfSymbol(symbol)); - links.declaredType = enumType.flags & 524288 /* Union */ ? - enumType.memberTypes[getEnumMemberValue(symbol.valueDeclaration)] : - enumType; + }); } - return links.declaredType; } - function getDeclaredTypeOfTypeParameter(symbol) { - var links = getSymbolLinks(symbol); - if (!links.declaredType) { - var type = createType(16384 /* TypeParameter */); - type.symbol = symbol; - if (!ts.getDeclarationOfKind(symbol, 141 /* TypeParameter */).constraint) { - type.constraint = noConstraintType; + /** + * Push an entry on the type resolution stack. If an entry with the given target and the given property name + * is already on the stack, and no entries in between already have a type, then a circularity has occurred. + * In this case, the result values of the existing entry and all entries pushed after it are changed to false, + * and the value false is returned. Otherwise, the new entry is just pushed onto the stack, and true is returned. + * In order to see if the same query has already been done before, the target object and the propertyName both + * must match the one passed in. + * + * @param target The symbol, type, or signature whose type is being queried + * @param propertyName The property name that should be used to query the target for its type + */ + function pushTypeResolution(target, propertyName) { + var resolutionCycleStartIndex = findResolutionCycleStartIndex(target, propertyName); + if (resolutionCycleStartIndex >= 0) { + // A cycle was found + var length_2 = resolutionTargets.length; + for (var i = resolutionCycleStartIndex; i < length_2; i++) { + resolutionResults[i] = false; } - links.declaredType = type; + return false; } - return links.declaredType; + resolutionTargets.push(target); + resolutionResults.push(/*items*/ true); + resolutionPropertyNames.push(propertyName); + return true; } - function getDeclaredTypeOfAlias(symbol) { - var links = getSymbolLinks(symbol); - if (!links.declaredType) { - links.declaredType = getDeclaredTypeOfSymbol(resolveAlias(symbol)); + function findResolutionCycleStartIndex(target, propertyName) { + for (var i = resolutionTargets.length - 1; i >= 0; i--) { + if (hasType(resolutionTargets[i], resolutionPropertyNames[i])) { + return -1; + } + if (resolutionTargets[i] === target && resolutionPropertyNames[i] === propertyName) { + return i; + } } - return links.declaredType; + return -1; } - function getDeclaredTypeOfSymbol(symbol) { - ts.Debug.assert((symbol.flags & 16777216 /* Instantiated */) === 0); - if (symbol.flags & (32 /* Class */ | 64 /* Interface */)) { - return getDeclaredTypeOfClassOrInterface(symbol); - } - if (symbol.flags & 524288 /* TypeAlias */) { - return getDeclaredTypeOfTypeAlias(symbol); - } - if (symbol.flags & 262144 /* TypeParameter */) { - return getDeclaredTypeOfTypeParameter(symbol); + function hasType(target, propertyName) { + if (propertyName === 0 /* Type */) { + return getSymbolLinks(target).type; } - if (symbol.flags & 384 /* Enum */) { - return getDeclaredTypeOfEnum(symbol); + if (propertyName === 2 /* DeclaredType */) { + return getSymbolLinks(target).declaredType; } - if (symbol.flags & 8 /* EnumMember */) { - return getDeclaredTypeOfEnumMember(symbol); + if (propertyName === 1 /* ResolvedBaseConstructorType */) { + ts.Debug.assert(!!(target.flags & 32768 /* Class */)); + return target.resolvedBaseConstructorType; } - if (symbol.flags & 8388608 /* Alias */) { - return getDeclaredTypeOfAlias(symbol); + if (propertyName === 3 /* ResolvedReturnType */) { + return target.resolvedReturnType; } - return unknownType; + ts.Debug.fail("Unhandled TypeSystemPropertyName " + propertyName); } - // A type reference is considered independent if each type argument is considered independent. - function isIndependentTypeReference(node) { - if (node.typeArguments) { - for (var _i = 0, _a = node.typeArguments; _i < _a.length; _i++) { - var typeNode = _a[_i]; - if (!isIndependentType(typeNode)) { - return false; - } + // Pop an entry from the type resolution stack and return its associated result value. The result value will + // be true if no circularities were detected, or false if a circularity was found. + function popTypeResolution() { + resolutionTargets.pop(); + resolutionPropertyNames.pop(); + return resolutionResults.pop(); + } + function getDeclarationContainer(node) { + node = ts.getRootDeclaration(node); + while (node) { + switch (node.kind) { + case 218 /* VariableDeclaration */: + case 219 /* VariableDeclarationList */: + case 234 /* ImportSpecifier */: + case 233 /* NamedImports */: + case 232 /* NamespaceImport */: + case 231 /* ImportClause */: + node = node.parent; + break; + default: + return node.parent; } } - return true; } - // A type is considered independent if it the any, string, number, boolean, symbol, or void keyword, a string - // literal type, an array with an element type that is considered independent, or a type reference that is - // considered independent. - function isIndependentType(node) { - switch (node.kind) { - case 117 /* AnyKeyword */: - case 132 /* StringKeyword */: - case 130 /* NumberKeyword */: - case 120 /* BooleanKeyword */: - case 133 /* SymbolKeyword */: - case 103 /* VoidKeyword */: - case 135 /* UndefinedKeyword */: - case 93 /* NullKeyword */: - case 127 /* NeverKeyword */: - case 166 /* LiteralType */: - return true; - case 160 /* ArrayType */: - return isIndependentType(node.elementType); - case 155 /* TypeReference */: - return isIndependentTypeReference(node); - } - return false; + function getTypeOfPrototypeProperty(prototype) { + // TypeScript 1.0 spec (April 2014): 8.4 + // Every class automatically contains a static property member named 'prototype', + // the type of which is an instantiation of the class type with type Any supplied as a type argument for each type parameter. + // It is an error to explicitly declare a static property member with the name 'prototype'. + var classType = getDeclaredTypeOfSymbol(getParentOfSymbol(prototype)); + return classType.typeParameters ? createTypeReference(classType, ts.map(classType.typeParameters, function (_) { return anyType; })) : classType; } - // A variable-like declaration is considered independent (free of this references) if it has a type annotation - // that specifies an independent type, or if it has no type annotation and no initializer (and thus of type any). - function isIndependentVariableLikeDeclaration(node) { - return node.type && isIndependentType(node.type) || !node.type && !node.initializer; + // Return the type of the given property in the given type, or undefined if no such property exists + function getTypeOfPropertyOfType(type, name) { + var prop = getPropertyOfType(type, name); + return prop ? getTypeOfSymbol(prop) : undefined; } - // A function-like declaration is considered independent (free of this references) if it has a return type - // annotation that is considered independent and if each parameter is considered independent. - function isIndependentFunctionLikeDeclaration(node) { - if (node.kind !== 148 /* Constructor */ && (!node.type || !isIndependentType(node.type))) { - return false; - } - for (var _i = 0, _a = node.parameters; _i < _a.length; _i++) { - var parameter = _a[_i]; - if (!isIndependentVariableLikeDeclaration(parameter)) { - return false; - } - } - return true; + function isTypeAny(type) { + return type && (type.flags & 1 /* Any */) !== 0; } - // Returns true if the class or interface member given by the symbol is free of "this" references. The - // function may return false for symbols that are actually free of "this" references because it is not - // feasible to perform a complete analysis in all cases. In particular, property members with types - // inferred from their initializers and function members with inferred return types are conservatively - // assumed not to be free of "this" references. - function isIndependentMember(symbol) { - if (symbol.declarations && symbol.declarations.length === 1) { - var declaration = symbol.declarations[0]; - if (declaration) { - switch (declaration.kind) { - case 145 /* PropertyDeclaration */: - case 144 /* PropertySignature */: - return isIndependentVariableLikeDeclaration(declaration); - case 147 /* MethodDeclaration */: - case 146 /* MethodSignature */: - case 148 /* Constructor */: - return isIndependentFunctionLikeDeclaration(declaration); - } - } - } - return false; + function isTypeNever(type) { + return type && (type.flags & 8192 /* Never */) !== 0; } - function createSymbolTable(symbols) { - var result = ts.createMap(); - for (var _i = 0, symbols_1 = symbols; _i < symbols_1.length; _i++) { - var symbol = symbols_1[_i]; - result[symbol.name] = symbol; - } - return result; + // Return the type of a binding element parent. We check SymbolLinks first to see if a type has been + // assigned by contextual typing. + function getTypeForBindingElementParent(node) { + var symbol = getSymbolOfNode(node); + return symbol && getSymbolLinks(symbol).type || getTypeForVariableLikeDeclaration(node, /*includeOptionality*/ false); } - // The mappingThisOnly flag indicates that the only type parameter being mapped is "this". When the flag is true, - // we check symbols to see if we can quickly conclude they are free of "this" references, thus needing no instantiation. - function createInstantiatedSymbolTable(symbols, mapper, mappingThisOnly) { - var result = ts.createMap(); - for (var _i = 0, symbols_2 = symbols; _i < symbols_2.length; _i++) { - var symbol = symbols_2[_i]; - result[symbol.name] = mappingThisOnly && isIndependentMember(symbol) ? symbol : instantiateSymbol(symbol, mapper); + function getTextOfPropertyName(name) { + switch (name.kind) { + case 69 /* Identifier */: + return name.text; + case 9 /* StringLiteral */: + case 8 /* NumericLiteral */: + return name.text; + case 140 /* ComputedPropertyName */: + if (ts.isStringOrNumericLiteral(name.expression.kind)) { + return name.expression.text; + } } - return result; + return undefined; } - function addInheritedMembers(symbols, baseSymbols) { - for (var _i = 0, baseSymbols_1 = baseSymbols; _i < baseSymbols_1.length; _i++) { - var s = baseSymbols_1[_i]; - if (!symbols[s.name]) { - symbols[s.name] = s; - } - } + function isComputedNonLiteralName(name) { + return name.kind === 140 /* ComputedPropertyName */ && !ts.isStringOrNumericLiteral(name.expression.kind); } - function resolveDeclaredMembers(type) { - if (!type.declaredProperties) { - var symbol = type.symbol; - type.declaredProperties = getNamedMembers(symbol.members); - type.declaredCallSignatures = getSignaturesOfSymbol(symbol.members["__call"]); - type.declaredConstructSignatures = getSignaturesOfSymbol(symbol.members["__new"]); - type.declaredStringIndexInfo = getIndexInfoOfSymbol(symbol, 0 /* String */); - type.declaredNumberIndexInfo = getIndexInfoOfSymbol(symbol, 1 /* Number */); + /** Return the inferred type for a binding element */ + function getTypeForBindingElement(declaration) { + var pattern = declaration.parent; + var parentType = getTypeForBindingElementParent(pattern.parent); + // If parent has the unknown (error) type, then so does this binding element + if (parentType === unknownType) { + return unknownType; } - return type; - } - function getTypeWithThisArgument(type, thisArgument) { - if (type.flags & 131072 /* Reference */) { - return createTypeReference(type.target, ts.concatenate(type.typeArguments, [thisArgument || type.target.thisType])); + // If no type was specified or inferred for parent, or if the specified or inferred type is any, + // infer from the initializer of the binding element if one is present. Otherwise, go with the + // undefined or any type of the parent. + if (!parentType || isTypeAny(parentType)) { + if (declaration.initializer) { + return checkDeclarationInitializer(declaration); + } + return parentType; } - return type; - } - function resolveObjectTypeMembers(type, source, typeParameters, typeArguments) { - var mapper; - var members; - var callSignatures; - var constructSignatures; - var stringIndexInfo; - var numberIndexInfo; - if (ts.rangeEquals(typeParameters, typeArguments, 0, typeParameters.length)) { - mapper = identityMapper; - members = source.symbol ? source.symbol.members : createSymbolTable(source.declaredProperties); - callSignatures = source.declaredCallSignatures; - constructSignatures = source.declaredConstructSignatures; - stringIndexInfo = source.declaredStringIndexInfo; - numberIndexInfo = source.declaredNumberIndexInfo; + var type; + if (pattern.kind === 167 /* ObjectBindingPattern */) { + // Use explicitly specified property name ({ p: xxx } form), or otherwise the implied name ({ p } form) + var name_14 = declaration.propertyName || declaration.name; + if (isComputedNonLiteralName(name_14)) { + // computed properties with non-literal names are treated as 'any' + return anyType; + } + if (declaration.initializer) { + getContextualType(declaration.initializer); + } + // Use type of the specified property, or otherwise, for a numeric name, the type of the numeric index signature, + // or otherwise the type of the string index signature. + var text = getTextOfPropertyName(name_14); + type = getTypeOfPropertyOfType(parentType, text) || + isNumericLiteralName(text) && getIndexTypeOfType(parentType, 1 /* Number */) || + getIndexTypeOfType(parentType, 0 /* String */); + if (!type) { + error(name_14, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(parentType), ts.declarationNameToString(name_14)); + return unknownType; + } } else { - mapper = createTypeMapper(typeParameters, typeArguments); - members = createInstantiatedSymbolTable(source.declaredProperties, mapper, /*mappingThisOnly*/ typeParameters.length === 1); - callSignatures = instantiateList(source.declaredCallSignatures, mapper, instantiateSignature); - constructSignatures = instantiateList(source.declaredConstructSignatures, mapper, instantiateSignature); - stringIndexInfo = instantiateIndexInfo(source.declaredStringIndexInfo, mapper); - numberIndexInfo = instantiateIndexInfo(source.declaredNumberIndexInfo, mapper); - } - var baseTypes = getBaseTypes(source); - if (baseTypes.length) { - if (source.symbol && members === source.symbol.members) { - members = createSymbolTable(source.declaredProperties); + // This elementType will be used if the specific property corresponding to this index is not + // present (aka the tuple element property). This call also checks that the parentType is in + // fact an iterable or array (depending on target language). + var elementType = checkIteratedTypeOrElementType(parentType, pattern, /*allowStringInput*/ false); + if (!declaration.dotDotDotToken) { + // Use specific property type when parent is a tuple or numeric index type when parent is an array + var propName = "" + ts.indexOf(pattern.elements, declaration); + type = isTupleLikeType(parentType) + ? getTypeOfPropertyOfType(parentType, propName) + : elementType; + if (!type) { + if (isTupleType(parentType)) { + error(declaration, ts.Diagnostics.Tuple_type_0_with_length_1_cannot_be_assigned_to_tuple_with_length_2, typeToString(parentType), getTypeReferenceArity(parentType), pattern.elements.length); + } + else { + error(declaration, ts.Diagnostics.Type_0_has_no_property_1, typeToString(parentType), propName); + } + return unknownType; + } } - var thisArgument = ts.lastOrUndefined(typeArguments); - for (var _i = 0, baseTypes_1 = baseTypes; _i < baseTypes_1.length; _i++) { - var baseType = baseTypes_1[_i]; - var instantiatedBaseType = thisArgument ? getTypeWithThisArgument(instantiateType(baseType, mapper), thisArgument) : baseType; - addInheritedMembers(members, getPropertiesOfObjectType(instantiatedBaseType)); - callSignatures = ts.concatenate(callSignatures, getSignaturesOfType(instantiatedBaseType, 0 /* Call */)); - constructSignatures = ts.concatenate(constructSignatures, getSignaturesOfType(instantiatedBaseType, 1 /* Construct */)); - stringIndexInfo = stringIndexInfo || getIndexInfoOfType(instantiatedBaseType, 0 /* String */); - numberIndexInfo = numberIndexInfo || getIndexInfoOfType(instantiatedBaseType, 1 /* Number */); + else { + // Rest element has an array type with the same element type as the parent type + type = createArrayType(elementType); } } - setObjectTypeMembers(type, members, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo); - } - function resolveClassOrInterfaceMembers(type) { - resolveObjectTypeMembers(type, resolveDeclaredMembers(type), emptyArray, emptyArray); - } - function resolveTypeReferenceMembers(type) { - var source = resolveDeclaredMembers(type.target); - var typeParameters = ts.concatenate(source.typeParameters, [source.thisType]); - var typeArguments = type.typeArguments && type.typeArguments.length === typeParameters.length ? - type.typeArguments : ts.concatenate(type.typeArguments, [type]); - resolveObjectTypeMembers(type, source, typeParameters, typeArguments); - } - function createSignature(declaration, typeParameters, thisParameter, parameters, resolvedReturnType, typePredicate, minArgumentCount, hasRestParameter, hasLiteralTypes) { - var sig = new Signature(checker); - sig.declaration = declaration; - sig.typeParameters = typeParameters; - sig.parameters = parameters; - sig.thisParameter = thisParameter; - sig.resolvedReturnType = resolvedReturnType; - sig.typePredicate = typePredicate; - sig.minArgumentCount = minArgumentCount; - sig.hasRestParameter = hasRestParameter; - sig.hasLiteralTypes = hasLiteralTypes; - return sig; + // In strict null checking mode, if a default value of a non-undefined type is specified, remove + // undefined from the final type. + if (strictNullChecks && declaration.initializer && !(getFalsyFlags(checkExpressionCached(declaration.initializer)) & 2048 /* Undefined */)) { + type = getTypeWithFacts(type, 131072 /* NEUndefined */); + } + return declaration.initializer ? + getUnionType([type, checkExpressionCached(declaration.initializer)], /*subtypeReduction*/ true) : + type; } - function cloneSignature(sig) { - return createSignature(sig.declaration, sig.typeParameters, sig.thisParameter, sig.parameters, sig.resolvedReturnType, sig.typePredicate, sig.minArgumentCount, sig.hasRestParameter, sig.hasLiteralTypes); + function getTypeForVariableLikeDeclarationFromJSDocComment(declaration) { + var jsDocType = getJSDocTypeForVariableLikeDeclarationFromJSDocComment(declaration); + if (jsDocType) { + return getTypeFromTypeNode(jsDocType); + } } - function getDefaultConstructSignatures(classType) { - var baseConstructorType = getBaseConstructorTypeOfClass(classType); - var baseSignatures = getSignaturesOfType(baseConstructorType, 1 /* Construct */); - if (baseSignatures.length === 0) { - return [createSignature(undefined, classType.localTypeParameters, undefined, emptyArray, classType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false)]; + function getJSDocTypeForVariableLikeDeclarationFromJSDocComment(declaration) { + // First, see if this node has an @type annotation on it directly. + var typeTag = ts.getJSDocTypeTag(declaration); + if (typeTag && typeTag.typeExpression) { + return typeTag.typeExpression.type; } - var baseTypeNode = getBaseTypeNodeOfClass(classType); - var typeArguments = ts.map(baseTypeNode.typeArguments, getTypeFromTypeNodeNoAlias); - var typeArgCount = typeArguments ? typeArguments.length : 0; - var result = []; - for (var _i = 0, baseSignatures_1 = baseSignatures; _i < baseSignatures_1.length; _i++) { - var baseSig = baseSignatures_1[_i]; - var typeParamCount = baseSig.typeParameters ? baseSig.typeParameters.length : 0; - if (typeParamCount === typeArgCount) { - var sig = typeParamCount ? getSignatureInstantiation(baseSig, typeArguments) : cloneSignature(baseSig); - sig.typeParameters = classType.localTypeParameters; - sig.resolvedReturnType = classType; - result.push(sig); + if (declaration.kind === 218 /* VariableDeclaration */ && + declaration.parent.kind === 219 /* VariableDeclarationList */ && + declaration.parent.parent.kind === 200 /* VariableStatement */) { + // @type annotation might have been on the variable statement, try that instead. + var annotation = ts.getJSDocTypeTag(declaration.parent.parent); + if (annotation && annotation.typeExpression) { + return annotation.typeExpression.type; } } - return result; - } - function findMatchingSignature(signatureList, signature, partialMatch, ignoreThisTypes, ignoreReturnTypes) { - for (var _i = 0, signatureList_1 = signatureList; _i < signatureList_1.length; _i++) { - var s = signatureList_1[_i]; - if (compareSignaturesIdentical(s, signature, partialMatch, ignoreThisTypes, ignoreReturnTypes, compareTypesIdentical)) { - return s; + else if (declaration.kind === 142 /* Parameter */) { + // If it's a parameter, see if the parent has a jsdoc comment with an @param + // annotation. + var paramTag = ts.getCorrespondingJSDocParameterTag(declaration); + if (paramTag && paramTag.typeExpression) { + return paramTag.typeExpression.type; } } + return undefined; } - function findMatchingSignatures(signatureLists, signature, listIndex) { - if (signature.typeParameters) { - // We require an exact match for generic signatures, so we only return signatures from the first - // signature list and only if they have exact matches in the other signature lists. - if (listIndex > 0) { - return undefined; + function isAutoVariableInitializer(initializer) { + var expr = initializer && ts.skipParentheses(initializer); + return !expr || expr.kind === 93 /* NullKeyword */ || expr.kind === 69 /* Identifier */ && getResolvedSymbol(expr) === undefinedSymbol; + } + function addOptionality(type, optional) { + return strictNullChecks && optional ? includeFalsyTypes(type, 2048 /* Undefined */) : type; + } + // Return the inferred type for a variable, parameter, or property declaration + function getTypeForVariableLikeDeclaration(declaration, includeOptionality) { + if (declaration.flags & 1048576 /* JavaScriptFile */) { + // If this is a variable in a JavaScript file, then use the JSDoc type (if it has + // one as its type), otherwise fallback to the below standard TS codepaths to + // try to figure it out. + var type = getTypeForVariableLikeDeclarationFromJSDocComment(declaration); + if (type && type !== unknownType) { + return type; } - for (var i = 1; i < signatureLists.length; i++) { - if (!findMatchingSignature(signatureLists[i], signature, /*partialMatch*/ false, /*ignoreThisTypes*/ false, /*ignoreReturnTypes*/ false)) { - return undefined; + } + // A variable declared in a for..in statement is always of type string + if (declaration.parent.parent.kind === 207 /* ForInStatement */) { + return stringType; + } + if (declaration.parent.parent.kind === 208 /* ForOfStatement */) { + // checkRightHandSideOfForOf will return undefined if the for-of expression type was + // missing properties/signatures required to get its iteratedType (like + // [Symbol.iterator] or next). This may be because we accessed properties from anyType, + // or it may have led to an error inside getElementTypeOfIterable. + return checkRightHandSideOfForOf(declaration.parent.parent.expression) || anyType; + } + if (ts.isBindingPattern(declaration.parent)) { + return getTypeForBindingElement(declaration); + } + // Use type from type annotation if one is present + if (declaration.type) { + return addOptionality(getTypeFromTypeNode(declaration.type), /*optional*/ declaration.questionToken && includeOptionality); + } + // Use control flow type inference for non-ambient, non-exported var or let variables with no initializer + // or a 'null' or 'undefined' initializer. + if (declaration.kind === 218 /* VariableDeclaration */ && !ts.isBindingPattern(declaration.name) && + !(ts.getCombinedNodeFlags(declaration) & 2 /* Const */) && !(ts.getCombinedModifierFlags(declaration) & 1 /* Export */) && + !ts.isInAmbientContext(declaration) && isAutoVariableInitializer(declaration.initializer)) { + return autoType; + } + if (declaration.kind === 142 /* Parameter */) { + var func = declaration.parent; + // For a parameter of a set accessor, use the type of the get accessor if one is present + if (func.kind === 150 /* SetAccessor */ && !ts.hasDynamicName(func)) { + var getter = ts.getDeclarationOfKind(declaration.parent.symbol, 149 /* GetAccessor */); + if (getter) { + var getterSignature = getSignatureFromDeclaration(getter); + var thisParameter = getAccessorThisParameter(func); + if (thisParameter && declaration === thisParameter) { + // Use the type from the *getter* + ts.Debug.assert(!thisParameter.type); + return getTypeOfSymbol(getterSignature.thisParameter); + } + return getReturnTypeOfSignature(getterSignature); } } - return [signature]; - } - var result = undefined; - for (var i = 0; i < signatureLists.length; i++) { - // Allow matching non-generic signatures to have excess parameters and different return types - var match = i === listIndex ? signature : findMatchingSignature(signatureLists[i], signature, /*partialMatch*/ true, /*ignoreThisTypes*/ true, /*ignoreReturnTypes*/ true); - if (!match) { - return undefined; + // Use contextual parameter type if one is available + var type = void 0; + if (declaration.symbol.name === "this") { + var thisParameter = getContextualThisParameter(func); + type = thisParameter ? getTypeOfSymbol(thisParameter) : undefined; } - if (!ts.contains(result, match)) { - (result || (result = [])).push(match); + else { + type = getContextuallyTypedParameterType(declaration); + } + if (type) { + return addOptionality(type, /*optional*/ declaration.questionToken && includeOptionality); } } - return result; + // Use the type of the initializer expression if one is present + if (declaration.initializer) { + var type = checkDeclarationInitializer(declaration); + return addOptionality(type, /*optional*/ declaration.questionToken && includeOptionality); + } + // If it is a short-hand property assignment, use the type of the identifier + if (declaration.kind === 254 /* ShorthandPropertyAssignment */) { + return checkIdentifier(declaration.name); + } + // If the declaration specifies a binding pattern, use the type implied by the binding pattern + if (ts.isBindingPattern(declaration.name)) { + return getTypeFromBindingPattern(declaration.name, /*includePatternInType*/ false, /*reportErrors*/ true); + } + // No type specified and nothing can be inferred + return undefined; } - // The signatures of a union type are those signatures that are present in each of the constituent types. - // Generic signatures must match exactly, but non-generic signatures are allowed to have extra optional - // parameters and may differ in return types. When signatures differ in return types, the resulting return - // type is the union of the constituent return types. - function getUnionSignatures(types, kind) { - var signatureLists = ts.map(types, function (t) { return getSignaturesOfType(t, kind); }); - var result = undefined; - for (var i = 0; i < signatureLists.length; i++) { - for (var _i = 0, _a = signatureLists[i]; _i < _a.length; _i++) { - var signature = _a[_i]; - // Only process signatures with parameter lists that aren't already in the result list - if (!result || !findMatchingSignature(result, signature, /*partialMatch*/ false, /*ignoreThisTypes*/ true, /*ignoreReturnTypes*/ true)) { - var unionSignatures = findMatchingSignatures(signatureLists, signature, i); - if (unionSignatures) { - var s = signature; - // Union the result types when more than one signature matches - if (unionSignatures.length > 1) { - s = cloneSignature(signature); - if (ts.forEach(unionSignatures, function (sig) { return sig.thisParameter; })) { - var thisType = getUnionType(ts.map(unionSignatures, function (sig) { return getTypeOfSymbol(sig.thisParameter) || anyType; }), /*subtypeReduction*/ true); - s.thisParameter = createTransientSymbol(signature.thisParameter, thisType); - } - // Clear resolved return type we possibly got from cloneSignature - s.resolvedReturnType = undefined; - s.unionSignatures = unionSignatures; - } - (result || (result = [])).push(s); - } - } - } + // Return the type implied by a binding pattern element. This is the type of the initializer of the element if + // one is present. Otherwise, if the element is itself a binding pattern, it is the type implied by the binding + // pattern. Otherwise, it is the type any. + function getTypeFromBindingElement(element, includePatternInType, reportErrors) { + if (element.initializer) { + return checkDeclarationInitializer(element); } - return result || emptyArray; + if (ts.isBindingPattern(element.name)) { + return getTypeFromBindingPattern(element.name, includePatternInType, reportErrors); + } + if (reportErrors && compilerOptions.noImplicitAny && !declarationBelongsToPrivateAmbientMember(element)) { + reportImplicitAnyError(element, anyType); + } + return anyType; } - function getUnionIndexInfo(types, kind) { - var indexTypes = []; - var isAnyReadonly = false; - for (var _i = 0, types_1 = types; _i < types_1.length; _i++) { - var type = types_1[_i]; - var indexInfo = getIndexInfoOfType(type, kind); - if (!indexInfo) { - return undefined; + // Return the type implied by an object binding pattern + function getTypeFromObjectBindingPattern(pattern, includePatternInType, reportErrors) { + var members = ts.createMap(); + var hasComputedProperties = false; + ts.forEach(pattern.elements, function (e) { + var name = e.propertyName || e.name; + if (isComputedNonLiteralName(name)) { + // do not include computed properties in the implied type + hasComputedProperties = true; + return; } - indexTypes.push(indexInfo.type); - isAnyReadonly = isAnyReadonly || indexInfo.isReadonly; + var text = getTextOfPropertyName(name); + var flags = 4 /* Property */ | 67108864 /* Transient */ | (e.initializer ? 536870912 /* Optional */ : 0); + var symbol = createSymbol(flags, text); + symbol.type = getTypeFromBindingElement(e, includePatternInType, reportErrors); + symbol.bindingElement = e; + members[symbol.name] = symbol; + }); + var result = createAnonymousType(undefined, members, emptyArray, emptyArray, undefined, undefined); + if (includePatternInType) { + result.pattern = pattern; } - return createIndexInfo(getUnionType(indexTypes, /*subtypeReduction*/ true), isAnyReadonly); + if (hasComputedProperties) { + result.isObjectLiteralPatternWithComputedProperties = true; + } + return result; } - function resolveUnionTypeMembers(type) { - // The members and properties collections are empty for union types. To get all properties of a union - // type use getPropertiesOfType (only the language service uses this). - var callSignatures = getUnionSignatures(type.types, 0 /* Call */); - var constructSignatures = getUnionSignatures(type.types, 1 /* Construct */); - var stringIndexInfo = getUnionIndexInfo(type.types, 0 /* String */); - var numberIndexInfo = getUnionIndexInfo(type.types, 1 /* Number */); - setObjectTypeMembers(type, emptySymbols, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo); + // Return the type implied by an array binding pattern + function getTypeFromArrayBindingPattern(pattern, includePatternInType, reportErrors) { + var elements = pattern.elements; + var lastElement = ts.lastOrUndefined(elements); + if (elements.length === 0 || (!ts.isOmittedExpression(lastElement) && lastElement.dotDotDotToken)) { + return languageVersion >= 2 /* ES6 */ ? createIterableType(anyType) : anyArrayType; + } + // If the pattern has at least one element, and no rest element, then it should imply a tuple type. + var elementTypes = ts.map(elements, function (e) { return ts.isOmittedExpression(e) ? anyType : getTypeFromBindingElement(e, includePatternInType, reportErrors); }); + var result = createTupleType(elementTypes); + if (includePatternInType) { + result = cloneTypeReference(result); + result.pattern = pattern; + } + return result; } - function intersectTypes(type1, type2) { - return !type1 ? type2 : !type2 ? type1 : getIntersectionType([type1, type2]); + // Return the type implied by a binding pattern. This is the type implied purely by the binding pattern itself + // and without regard to its context (i.e. without regard any type annotation or initializer associated with the + // declaration in which the binding pattern is contained). For example, the implied type of [x, y] is [any, any] + // and the implied type of { x, y: z = 1 } is { x: any; y: number; }. The type implied by a binding pattern is + // used as the contextual type of an initializer associated with the binding pattern. Also, for a destructuring + // parameter with no type annotation or initializer, the type implied by the binding pattern becomes the type of + // the parameter. + function getTypeFromBindingPattern(pattern, includePatternInType, reportErrors) { + return pattern.kind === 167 /* ObjectBindingPattern */ + ? getTypeFromObjectBindingPattern(pattern, includePatternInType, reportErrors) + : getTypeFromArrayBindingPattern(pattern, includePatternInType, reportErrors); } - function intersectIndexInfos(info1, info2) { - return !info1 ? info2 : !info2 ? info1 : createIndexInfo(getIntersectionType([info1.type, info2.type]), info1.isReadonly && info2.isReadonly); - } - function resolveIntersectionTypeMembers(type) { - // The members and properties collections are empty for intersection types. To get all properties of an - // intersection type use getPropertiesOfType (only the language service uses this). - var callSignatures = emptyArray; - var constructSignatures = emptyArray; - var stringIndexInfo = undefined; - var numberIndexInfo = undefined; - for (var _i = 0, _a = type.types; _i < _a.length; _i++) { - var t = _a[_i]; - callSignatures = ts.concatenate(callSignatures, getSignaturesOfType(t, 0 /* Call */)); - constructSignatures = ts.concatenate(constructSignatures, getSignaturesOfType(t, 1 /* Construct */)); - stringIndexInfo = intersectIndexInfos(stringIndexInfo, getIndexInfoOfType(t, 0 /* String */)); - numberIndexInfo = intersectIndexInfos(numberIndexInfo, getIndexInfoOfType(t, 1 /* Number */)); - } - setObjectTypeMembers(type, emptySymbols, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo); - } - function resolveAnonymousTypeMembers(type) { - var symbol = type.symbol; - if (type.target) { - var members = createInstantiatedSymbolTable(getPropertiesOfObjectType(type.target), type.mapper, /*mappingThisOnly*/ false); - var callSignatures = instantiateList(getSignaturesOfType(type.target, 0 /* Call */), type.mapper, instantiateSignature); - var constructSignatures = instantiateList(getSignaturesOfType(type.target, 1 /* Construct */), type.mapper, instantiateSignature); - var stringIndexInfo = instantiateIndexInfo(getIndexInfoOfType(type.target, 0 /* String */), type.mapper); - var numberIndexInfo = instantiateIndexInfo(getIndexInfoOfType(type.target, 1 /* Number */), type.mapper); - setObjectTypeMembers(type, members, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo); - } - else if (symbol.flags & 2048 /* TypeLiteral */) { - var members = symbol.members; - var callSignatures = getSignaturesOfSymbol(members["__call"]); - var constructSignatures = getSignaturesOfSymbol(members["__new"]); - var stringIndexInfo = getIndexInfoOfSymbol(symbol, 0 /* String */); - var numberIndexInfo = getIndexInfoOfSymbol(symbol, 1 /* Number */); - setObjectTypeMembers(type, members, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo); - } - else { - // Combinations of function, class, enum and module - var members = emptySymbols; - var constructSignatures = emptyArray; - if (symbol.flags & 1952 /* HasExports */) { - members = getExportsOfSymbol(symbol); + // Return the type associated with a variable, parameter, or property declaration. In the simple case this is the type + // specified in a type annotation or inferred from an initializer. However, in the case of a destructuring declaration it + // is a bit more involved. For example: + // + // var [x, s = ""] = [1, "one"]; + // + // Here, the array literal [1, "one"] is contextually typed by the type [any, string], which is the implied type of the + // binding pattern [x, s = ""]. Because the contextual type is a tuple type, the resulting type of [1, "one"] is the + // tuple type [number, string]. Thus, the type inferred for 'x' is number and the type inferred for 's' is string. + function getWidenedTypeForVariableLikeDeclaration(declaration, reportErrors) { + var type = getTypeForVariableLikeDeclaration(declaration, /*includeOptionality*/ true); + if (type) { + if (reportErrors) { + reportErrorsFromWidening(declaration, type); } - if (symbol.flags & 32 /* Class */) { - var classType = getDeclaredTypeOfClassOrInterface(symbol); - constructSignatures = getSignaturesOfSymbol(symbol.members["__constructor"]); - if (!constructSignatures.length) { - constructSignatures = getDefaultConstructSignatures(classType); - } - var baseConstructorType = getBaseConstructorTypeOfClass(classType); - if (baseConstructorType.flags & 2588672 /* ObjectType */) { - members = createSymbolTable(getNamedMembers(members)); - addInheritedMembers(members, getPropertiesOfObjectType(baseConstructorType)); - } + // During a normal type check we'll never get to here with a property assignment (the check of the containing + // object literal uses a different path). We exclude widening only so that language services and type verification + // tools see the actual type. + if (declaration.kind === 253 /* PropertyAssignment */) { + return type; } - var numberIndexInfo = symbol.flags & 384 /* Enum */ ? enumNumberIndexInfo : undefined; - setObjectTypeMembers(type, members, emptyArray, constructSignatures, undefined, numberIndexInfo); - // We resolve the members before computing the signatures because a signature may use - // typeof with a qualified name expression that circularly references the type we are - // in the process of resolving (see issue #6072). The temporarily empty signature list - // will never be observed because a qualified name can't reference signatures. - if (symbol.flags & (16 /* Function */ | 8192 /* Method */)) { - type.callSignatures = getSignaturesOfSymbol(symbol); + return getWidenedType(type); + } + // Rest parameters default to type any[], other parameters default to type any + type = declaration.dotDotDotToken ? anyArrayType : anyType; + // Report implicit any errors unless this is a private property within an ambient declaration + if (reportErrors && compilerOptions.noImplicitAny) { + if (!declarationBelongsToPrivateAmbientMember(declaration)) { + reportImplicitAnyError(declaration, type); } } + return type; } - function resolveStructuredTypeMembers(type) { - if (!type.members) { - if (type.flags & 131072 /* Reference */) { - resolveTypeReferenceMembers(type); + function declarationBelongsToPrivateAmbientMember(declaration) { + var root = ts.getRootDeclaration(declaration); + var memberDeclaration = root.kind === 142 /* Parameter */ ? root.parent : root; + return isPrivateWithinAmbient(memberDeclaration); + } + function getTypeOfVariableOrParameterOrProperty(symbol) { + var links = getSymbolLinks(symbol); + if (!links.type) { + // Handle prototype property + if (symbol.flags & 134217728 /* Prototype */) { + return links.type = getTypeOfPrototypeProperty(symbol); } - else if (type.flags & (32768 /* Class */ | 65536 /* Interface */)) { - resolveClassOrInterfaceMembers(type); + // Handle catch clause variables + var declaration = symbol.valueDeclaration; + if (declaration.parent.kind === 252 /* CatchClause */) { + return links.type = anyType; } - else if (type.flags & 2097152 /* Anonymous */) { - resolveAnonymousTypeMembers(type); + // Handle export default expressions + if (declaration.kind === 235 /* ExportAssignment */) { + return links.type = checkExpression(declaration.expression); } - else if (type.flags & 524288 /* Union */) { - resolveUnionTypeMembers(type); + if (declaration.flags & 1048576 /* JavaScriptFile */ && declaration.kind === 280 /* JSDocPropertyTag */ && declaration.typeExpression) { + return links.type = getTypeFromTypeNode(declaration.typeExpression.type); } - else if (type.flags & 1048576 /* Intersection */) { - resolveIntersectionTypeMembers(type); + // Handle variable, parameter or property + if (!pushTypeResolution(symbol, 0 /* Type */)) { + return unknownType; + } + var type = void 0; + // Handle certain special assignment kinds, which happen to union across multiple declarations: + // * module.exports = expr + // * exports.p = expr + // * this.p = expr + // * className.prototype.method = expr + if (declaration.kind === 187 /* BinaryExpression */ || + declaration.kind === 172 /* PropertyAccessExpression */ && declaration.parent.kind === 187 /* BinaryExpression */) { + // Use JS Doc type if present on parent expression statement + if (declaration.flags & 1048576 /* JavaScriptFile */) { + var typeTag = ts.getJSDocTypeTag(declaration.parent); + if (typeTag && typeTag.typeExpression) { + return links.type = getTypeFromTypeNode(typeTag.typeExpression.type); + } + } + var declaredTypes = ts.map(symbol.declarations, function (decl) { return decl.kind === 187 /* BinaryExpression */ ? + checkExpressionCached(decl.right) : + checkExpressionCached(decl.parent.right); }); + type = getUnionType(declaredTypes, /*subtypeReduction*/ true); + } + else { + type = getWidenedTypeForVariableLikeDeclaration(declaration, /*reportErrors*/ true); + } + if (!popTypeResolution()) { + if (symbol.valueDeclaration.type) { + // Variable has type annotation that circularly references the variable itself + type = unknownType; + error(symbol.valueDeclaration, ts.Diagnostics._0_is_referenced_directly_or_indirectly_in_its_own_type_annotation, symbolToString(symbol)); + } + else { + // Variable has initializer that circularly references the variable itself + type = anyType; + if (compilerOptions.noImplicitAny) { + error(symbol.valueDeclaration, ts.Diagnostics._0_implicitly_has_type_any_because_it_does_not_have_a_type_annotation_and_is_referenced_directly_or_indirectly_in_its_own_initializer, symbolToString(symbol)); + } + } } + links.type = type; } - return type; + return links.type; } - /** Return properties of an object type or an empty array for other types */ - function getPropertiesOfObjectType(type) { - if (type.flags & 2588672 /* ObjectType */) { - return resolveStructuredTypeMembers(type).properties; + function getAnnotatedAccessorType(accessor) { + if (accessor) { + if (accessor.kind === 149 /* GetAccessor */) { + return accessor.type && getTypeFromTypeNode(accessor.type); + } + else { + var setterTypeAnnotation = ts.getSetAccessorTypeAnnotationNode(accessor); + return setterTypeAnnotation && getTypeFromTypeNode(setterTypeAnnotation); + } } - return emptyArray; + return undefined; } - /** If the given type is an object type and that type has a property by the given name, - * return the symbol for that property. Otherwise return undefined. */ - function getPropertyOfObjectType(type, name) { - if (type.flags & 2588672 /* ObjectType */) { - var resolved = resolveStructuredTypeMembers(type); - var symbol = resolved.members[name]; - if (symbol && symbolIsValue(symbol)) { - return symbol; + function getAnnotatedAccessorThisParameter(accessor) { + var parameter = getAccessorThisParameter(accessor); + return parameter && parameter.symbol; + } + function getThisTypeOfDeclaration(declaration) { + return getThisTypeOfSignature(getSignatureFromDeclaration(declaration)); + } + function getTypeOfAccessors(symbol) { + var links = getSymbolLinks(symbol); + if (!links.type) { + var getter = ts.getDeclarationOfKind(symbol, 149 /* GetAccessor */); + var setter = ts.getDeclarationOfKind(symbol, 150 /* SetAccessor */); + if (getter && getter.flags & 1048576 /* JavaScriptFile */) { + var jsDocType = getTypeForVariableLikeDeclarationFromJSDocComment(getter); + if (jsDocType) { + return links.type = jsDocType; + } + } + if (!pushTypeResolution(symbol, 0 /* Type */)) { + return unknownType; + } + var type = void 0; + // First try to see if the user specified a return type on the get-accessor. + var getterReturnType = getAnnotatedAccessorType(getter); + if (getterReturnType) { + type = getterReturnType; + } + else { + // If the user didn't specify a return type, try to use the set-accessor's parameter type. + var setterParameterType = getAnnotatedAccessorType(setter); + if (setterParameterType) { + type = setterParameterType; + } + else { + // If there are no specified types, try to infer it from the body of the get accessor if it exists. + if (getter && getter.body) { + type = getReturnTypeFromBody(getter); + } + else { + if (compilerOptions.noImplicitAny) { + if (setter) { + error(setter, ts.Diagnostics.Property_0_implicitly_has_type_any_because_its_set_accessor_lacks_a_parameter_type_annotation, symbolToString(symbol)); + } + else { + ts.Debug.assert(!!getter, "there must existed getter as we are current checking either setter or getter in this function"); + error(getter, ts.Diagnostics.Property_0_implicitly_has_type_any_because_its_get_accessor_lacks_a_return_type_annotation, symbolToString(symbol)); + } + } + type = anyType; + } + } + } + if (!popTypeResolution()) { + type = anyType; + if (compilerOptions.noImplicitAny) { + var getter_1 = ts.getDeclarationOfKind(symbol, 149 /* GetAccessor */); + error(getter_1, ts.Diagnostics._0_implicitly_has_return_type_any_because_it_does_not_have_a_return_type_annotation_and_is_referenced_directly_or_indirectly_in_one_of_its_return_expressions, symbolToString(symbol)); + } } + links.type = type; } + return links.type; } - function getPropertiesOfUnionOrIntersectionType(type) { - for (var _i = 0, _a = type.types; _i < _a.length; _i++) { - var current = _a[_i]; - for (var _b = 0, _c = getPropertiesOfType(current); _b < _c.length; _b++) { - var prop = _c[_b]; - getPropertyOfUnionOrIntersectionType(type, prop.name); + function getTypeOfFuncClassEnumModule(symbol) { + var links = getSymbolLinks(symbol); + if (!links.type) { + if (symbol.valueDeclaration.kind === 225 /* ModuleDeclaration */ && ts.isShorthandAmbientModuleSymbol(symbol)) { + links.type = anyType; } - // The properties of a union type are those that are present in all constituent types, so - // we only need to check the properties of the first type - if (type.flags & 524288 /* Union */) { - break; + else { + var type = createObjectType(2097152 /* Anonymous */, symbol); + links.type = strictNullChecks && symbol.flags & 536870912 /* Optional */ ? + includeFalsyTypes(type, 2048 /* Undefined */) : type; } } - return type.resolvedProperties ? symbolsToArray(type.resolvedProperties) : emptyArray; - } - function getPropertiesOfType(type) { - type = getApparentType(type); - return type.flags & 1572864 /* UnionOrIntersection */ ? getPropertiesOfUnionOrIntersectionType(type) : getPropertiesOfObjectType(type); + return links.type; } - /** - * The apparent type of a type parameter is the base constraint instantiated with the type parameter - * as the type argument for the 'this' type. - */ - function getApparentTypeOfTypeParameter(type) { - if (!type.resolvedApparentType) { - var constraintType = getConstraintOfTypeParameter(type); - while (constraintType && constraintType.flags & 16384 /* TypeParameter */) { - constraintType = getConstraintOfTypeParameter(constraintType); - } - type.resolvedApparentType = getTypeWithThisArgument(constraintType || emptyObjectType, type); + function getTypeOfEnumMember(symbol) { + var links = getSymbolLinks(symbol); + if (!links.type) { + links.type = getDeclaredTypeOfEnumMember(symbol); } - return type.resolvedApparentType; + return links.type; } - /** - * For a type parameter, return the base constraint of the type parameter. For the string, number, - * boolean, and symbol primitive types, return the corresponding object types. Otherwise return the - * type itself. Note that the apparent type of a union type is the union type itself. - */ - function getApparentType(type) { - if (type.flags & 16384 /* TypeParameter */) { - type = getApparentTypeOfTypeParameter(type); + function getTypeOfAlias(symbol) { + var links = getSymbolLinks(symbol); + if (!links.type) { + var targetSymbol = resolveAlias(symbol); + // It only makes sense to get the type of a value symbol. If the result of resolving + // the alias is not a value, then it has no type. To get the type associated with a + // type symbol, call getDeclaredTypeOfSymbol. + // This check is important because without it, a call to getTypeOfSymbol could end + // up recursively calling getTypeOfAlias, causing a stack overflow. + links.type = targetSymbol.flags & 107455 /* Value */ + ? getTypeOfSymbol(targetSymbol) + : unknownType; } - if (type.flags & 34 /* StringLike */) { - type = globalStringType; + return links.type; + } + function getTypeOfInstantiatedSymbol(symbol) { + var links = getSymbolLinks(symbol); + if (!links.type) { + links.type = instantiateType(getTypeOfSymbol(links.target), links.mapper); } - else if (type.flags & 340 /* NumberLike */) { - type = globalNumberType; + return links.type; + } + function getTypeOfSymbol(symbol) { + if (symbol.flags & 16777216 /* Instantiated */) { + return getTypeOfInstantiatedSymbol(symbol); } - else if (type.flags & 136 /* BooleanLike */) { - type = globalBooleanType; + if (symbol.flags & (3 /* Variable */ | 4 /* Property */)) { + return getTypeOfVariableOrParameterOrProperty(symbol); } - else if (type.flags & 512 /* ESSymbol */) { - type = getGlobalESSymbolType(); + if (symbol.flags & (16 /* Function */ | 8192 /* Method */ | 32 /* Class */ | 384 /* Enum */ | 512 /* ValueModule */)) { + return getTypeOfFuncClassEnumModule(symbol); } - return type; - } - function createUnionOrIntersectionProperty(containingType, name) { - var types = containingType.types; - var props; - // Flags we want to propagate to the result if they exist in all source symbols - var commonFlags = (containingType.flags & 1048576 /* Intersection */) ? 536870912 /* Optional */ : 0 /* None */; - var isReadonly = false; - for (var _i = 0, types_2 = types; _i < types_2.length; _i++) { - var current = types_2[_i]; - var type = getApparentType(current); - if (type !== unknownType) { - var prop = getPropertyOfType(type, name); - if (prop && !(getDeclarationModifierFlagsFromSymbol(prop) & (8 /* Private */ | 16 /* Protected */))) { - commonFlags &= prop.flags; - if (!props) { - props = [prop]; - } - else if (!ts.contains(props, prop)) { - props.push(prop); - } - if (isReadonlySymbol(prop)) { - isReadonly = true; - } - } - else if (containingType.flags & 524288 /* Union */) { - // A union type requires the property to be present in all constituent types - return undefined; - } - } + if (symbol.flags & 8 /* EnumMember */) { + return getTypeOfEnumMember(symbol); } - if (!props) { - return undefined; + if (symbol.flags & 98304 /* Accessor */) { + return getTypeOfAccessors(symbol); } - if (props.length === 1) { - return props[0]; + if (symbol.flags & 8388608 /* Alias */) { + return getTypeOfAlias(symbol); } - var propTypes = []; - var declarations = []; - var commonType = undefined; - var hasCommonType = true; - for (var _a = 0, props_1 = props; _a < props_1.length; _a++) { - var prop = props_1[_a]; - if (prop.declarations) { - ts.addRange(declarations, prop.declarations); - } - var type = getTypeOfSymbol(prop); - if (!commonType) { - commonType = type; - } - else if (type !== commonType) { - hasCommonType = false; - } - propTypes.push(getTypeOfSymbol(prop)); + return unknownType; + } + function getTargetType(type) { + return type.flags & 131072 /* Reference */ ? type.target : type; + } + function hasBaseType(type, checkBase) { + return check(type); + function check(type) { + var target = getTargetType(type); + return target === checkBase || ts.forEach(getBaseTypes(target), check); } - var result = createSymbol(4 /* Property */ | - 67108864 /* Transient */ | - 268435456 /* SyntheticProperty */ | - commonFlags, name); - result.containingType = containingType; - result.hasCommonType = hasCommonType; - result.declarations = declarations; - result.isReadonly = isReadonly; - result.type = containingType.flags & 524288 /* Union */ ? getUnionType(propTypes) : getIntersectionType(propTypes); - return result; } - function getPropertyOfUnionOrIntersectionType(type, name) { - var properties = type.resolvedProperties || (type.resolvedProperties = ts.createMap()); - var property = properties[name]; - if (!property) { - property = createUnionOrIntersectionProperty(type, name); - if (property) { - properties[name] = property; + // Appends the type parameters given by a list of declarations to a set of type parameters and returns the resulting set. + // The function allocates a new array if the input type parameter set is undefined, but otherwise it modifies the set + // in-place and returns the same array. + function appendTypeParameters(typeParameters, declarations) { + for (var _i = 0, declarations_2 = declarations; _i < declarations_2.length; _i++) { + var declaration = declarations_2[_i]; + var tp = getDeclaredTypeOfTypeParameter(getSymbolOfNode(declaration)); + if (!typeParameters) { + typeParameters = [tp]; + } + else if (!ts.contains(typeParameters, tp)) { + typeParameters.push(tp); } } - return property; + return typeParameters; } - /** - * Return the symbol for the property with the given name in the given type. Creates synthetic union properties when - * necessary, maps primitive types and type parameters are to their apparent types, and augments with properties from - * Object and Function as appropriate. - * - * @param type a type to look up property from - * @param name a name of property to look up in a given type - */ - function getPropertyOfType(type, name) { - type = getApparentType(type); - if (type.flags & 2588672 /* ObjectType */) { - var resolved = resolveStructuredTypeMembers(type); - var symbol = resolved.members[name]; - if (symbol && symbolIsValue(symbol)) { - return symbol; + // Appends the outer type parameters of a node to a set of type parameters and returns the resulting set. The function + // allocates a new array if the input type parameter set is undefined, but otherwise it modifies the set in-place and + // returns the same array. + function appendOuterTypeParameters(typeParameters, node) { + while (true) { + node = node.parent; + if (!node) { + return typeParameters; } - if (resolved === anyFunctionType || resolved.callSignatures.length || resolved.constructSignatures.length) { - var symbol_1 = getPropertyOfObjectType(globalFunctionType, name); - if (symbol_1) { - return symbol_1; + if (node.kind === 221 /* ClassDeclaration */ || node.kind === 192 /* ClassExpression */ || + node.kind === 220 /* FunctionDeclaration */ || node.kind === 179 /* FunctionExpression */ || + node.kind === 147 /* MethodDeclaration */ || node.kind === 180 /* ArrowFunction */) { + var declarations = node.typeParameters; + if (declarations) { + return appendTypeParameters(appendOuterTypeParameters(typeParameters, node), declarations); } } - return getPropertyOfObjectType(globalObjectType, name); - } - if (type.flags & 1572864 /* UnionOrIntersection */) { - return getPropertyOfUnionOrIntersectionType(type, name); - } - return undefined; - } - function getSignaturesOfStructuredType(type, kind) { - if (type.flags & 4161536 /* StructuredType */) { - var resolved = resolveStructuredTypeMembers(type); - return kind === 0 /* Call */ ? resolved.callSignatures : resolved.constructSignatures; } - return emptyArray; } - /** - * Return the signatures of the given kind in the given type. Creates synthetic union signatures when necessary and - * maps primitive types and type parameters are to their apparent types. - */ - function getSignaturesOfType(type, kind) { - return getSignaturesOfStructuredType(getApparentType(type), kind); + // The outer type parameters are those defined by enclosing generic classes, methods, or functions. + function getOuterTypeParametersOfClassOrInterface(symbol) { + var declaration = symbol.flags & 32 /* Class */ ? symbol.valueDeclaration : ts.getDeclarationOfKind(symbol, 222 /* InterfaceDeclaration */); + return appendOuterTypeParameters(undefined, declaration); } - function getIndexInfoOfStructuredType(type, kind) { - if (type.flags & 4161536 /* StructuredType */) { - var resolved = resolveStructuredTypeMembers(type); - return kind === 0 /* String */ ? resolved.stringIndexInfo : resolved.numberIndexInfo; + // The local type parameters are the combined set of type parameters from all declarations of the class, + // interface, or type alias. + function getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol) { + var result; + for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { + var node = _a[_i]; + if (node.kind === 222 /* InterfaceDeclaration */ || node.kind === 221 /* ClassDeclaration */ || + node.kind === 192 /* ClassExpression */ || node.kind === 223 /* TypeAliasDeclaration */) { + var declaration = node; + if (declaration.typeParameters) { + result = appendTypeParameters(result, declaration.typeParameters); + } + } } + return result; } - function getIndexTypeOfStructuredType(type, kind) { - var info = getIndexInfoOfStructuredType(type, kind); - return info && info.type; + // The full set of type parameters for a generic class or interface type consists of its outer type parameters plus + // its locally declared type parameters. + function getTypeParametersOfClassOrInterface(symbol) { + return ts.concatenate(getOuterTypeParametersOfClassOrInterface(symbol), getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol)); } - // Return the indexing info of the given kind in the given type. Creates synthetic union index types when necessary and - // maps primitive types and type parameters are to their apparent types. - function getIndexInfoOfType(type, kind) { - return getIndexInfoOfStructuredType(getApparentType(type), kind); + function isConstructorType(type) { + return type.flags & 2588672 /* ObjectType */ && getSignaturesOfType(type, 1 /* Construct */).length > 0; } - // Return the index type of the given kind in the given type. Creates synthetic union index types when necessary and - // maps primitive types and type parameters are to their apparent types. - function getIndexTypeOfType(type, kind) { - return getIndexTypeOfStructuredType(getApparentType(type), kind); + function getBaseTypeNodeOfClass(type) { + return ts.getClassExtendsHeritageClauseElement(type.symbol.valueDeclaration); } - function getImplicitIndexTypeOfType(type, kind) { - if (isObjectLiteralType(type)) { - var propTypes = []; - for (var _i = 0, _a = getPropertiesOfType(type); _i < _a.length; _i++) { - var prop = _a[_i]; - if (kind === 0 /* String */ || isNumericLiteralName(prop.name)) { - propTypes.push(getTypeOfSymbol(prop)); - } - } - if (propTypes.length) { - return getUnionType(propTypes, /*subtypeReduction*/ true); - } - } - return undefined; + function getConstructorsForTypeArguments(type, typeArgumentNodes) { + var typeArgCount = typeArgumentNodes ? typeArgumentNodes.length : 0; + return ts.filter(getSignaturesOfType(type, 1 /* Construct */), function (sig) { return (sig.typeParameters ? sig.typeParameters.length : 0) === typeArgCount; }); } - function getTypeParametersFromJSDocTemplate(declaration) { - if (declaration.flags & 1048576 /* JavaScriptFile */) { - var templateTag = ts.getJSDocTemplateTag(declaration); - if (templateTag) { - return getTypeParametersFromDeclaration(templateTag.typeParameters); - } + function getInstantiatedConstructorsForTypeArguments(type, typeArgumentNodes) { + var signatures = getConstructorsForTypeArguments(type, typeArgumentNodes); + if (typeArgumentNodes) { + var typeArguments_1 = ts.map(typeArgumentNodes, getTypeFromTypeNodeNoAlias); + signatures = ts.map(signatures, function (sig) { return getSignatureInstantiation(sig, typeArguments_1); }); } - return undefined; + return signatures; } - // Return list of type parameters with duplicates removed (duplicate identifier errors are generated in the actual - // type checking functions). - function getTypeParametersFromDeclaration(typeParameterDeclarations) { - var result = []; - ts.forEach(typeParameterDeclarations, function (node) { - var tp = getDeclaredTypeOfTypeParameter(node.symbol); - if (!ts.contains(result, tp)) { - result.push(tp); + // The base constructor of a class can resolve to + // undefinedType if the class has no extends clause, + // unknownType if an error occurred during resolution of the extends expression, + // nullType if the extends expression is the null value, or + // an object type with at least one construct signature. + function getBaseConstructorTypeOfClass(type) { + if (!type.resolvedBaseConstructorType) { + var baseTypeNode = getBaseTypeNodeOfClass(type); + if (!baseTypeNode) { + return type.resolvedBaseConstructorType = undefinedType; } - }); - return result; - } - function symbolsToArray(symbols) { - var result = []; - for (var id in symbols) { - if (!isReservedMemberName(id)) { - result.push(symbols[id]); + if (!pushTypeResolution(type, 1 /* ResolvedBaseConstructorType */)) { + return unknownType; + } + var baseConstructorType = checkExpression(baseTypeNode.expression); + if (baseConstructorType.flags & 2588672 /* ObjectType */) { + // Resolving the members of a class requires us to resolve the base class of that class. + // We force resolution here such that we catch circularities now. + resolveStructuredTypeMembers(baseConstructorType); + } + if (!popTypeResolution()) { + error(type.symbol.valueDeclaration, ts.Diagnostics._0_is_referenced_directly_or_indirectly_in_its_own_base_expression, symbolToString(type.symbol)); + return type.resolvedBaseConstructorType = unknownType; + } + if (baseConstructorType !== unknownType && baseConstructorType !== nullWideningType && !isConstructorType(baseConstructorType)) { + error(baseTypeNode.expression, ts.Diagnostics.Type_0_is_not_a_constructor_function_type, typeToString(baseConstructorType)); + return type.resolvedBaseConstructorType = unknownType; } + type.resolvedBaseConstructorType = baseConstructorType; } - return result; + return type.resolvedBaseConstructorType; } - function isJSDocOptionalParameter(node) { - if (node.flags & 1048576 /* JavaScriptFile */) { - if (node.type && node.type.kind === 268 /* JSDocOptionalType */) { - return true; + function getBaseTypes(type) { + if (!type.resolvedBaseTypes) { + if (type.flags & 262144 /* Tuple */) { + type.resolvedBaseTypes = [createArrayType(getUnionType(type.typeParameters))]; } - var paramTag = ts.getCorrespondingJSDocParameterTag(node); - if (paramTag) { - if (paramTag.isBracketed) { - return true; + else if (type.symbol.flags & (32 /* Class */ | 64 /* Interface */)) { + if (type.symbol.flags & 32 /* Class */) { + resolveBaseTypesOfClass(type); } - if (paramTag.typeExpression) { - return paramTag.typeExpression.type.kind === 268 /* JSDocOptionalType */; + if (type.symbol.flags & 64 /* Interface */) { + resolveBaseTypesOfInterface(type); } } + else { + ts.Debug.fail("type must be class or interface"); + } } + return type.resolvedBaseTypes; } - function isOptionalParameter(node) { - if (ts.hasQuestionToken(node) || isJSDocOptionalParameter(node)) { - return true; - } - if (node.initializer) { - var signatureDeclaration = node.parent; - var signature = getSignatureFromDeclaration(signatureDeclaration); - var parameterIndex = ts.indexOf(signatureDeclaration.parameters, node); - ts.Debug.assert(parameterIndex >= 0); - return parameterIndex >= signature.minArgumentCount; + function resolveBaseTypesOfClass(type) { + type.resolvedBaseTypes = type.resolvedBaseTypes || emptyArray; + var baseConstructorType = getBaseConstructorTypeOfClass(type); + if (!(baseConstructorType.flags & 2588672 /* ObjectType */)) { + return; } - return false; - } - function createTypePredicateFromTypePredicateNode(node) { - if (node.parameterName.kind === 69 /* Identifier */) { - var parameterName = node.parameterName; - return { - kind: 1 /* Identifier */, - parameterName: parameterName ? parameterName.text : undefined, - parameterIndex: parameterName ? getTypePredicateParameterIndex(node.parent.parameters, parameterName) : undefined, - type: getTypeFromTypeNode(node.type) - }; + var baseTypeNode = getBaseTypeNodeOfClass(type); + var baseType; + var originalBaseType = baseConstructorType && baseConstructorType.symbol ? getDeclaredTypeOfSymbol(baseConstructorType.symbol) : undefined; + if (baseConstructorType.symbol && baseConstructorType.symbol.flags & 32 /* Class */ && + areAllOuterTypeParametersApplied(originalBaseType)) { + // When base constructor type is a class with no captured type arguments we know that the constructors all have the same type parameters as the + // class and all return the instance type of the class. There is no need for further checks and we can apply the + // type arguments in the same manner as a type reference to get the same error reporting experience. + baseType = getTypeFromClassOrInterfaceReference(baseTypeNode, baseConstructorType.symbol); } else { - return { - kind: 0 /* This */, - type: getTypeFromTypeNode(node.type) - }; - } - } - function getSignatureFromDeclaration(declaration) { - var links = getNodeLinks(declaration); - if (!links.resolvedSignature) { - var parameters = []; - var hasLiteralTypes = false; - var minArgumentCount = -1; - var thisParameter = undefined; - var hasThisParameter = void 0; - var isJSConstructSignature = ts.isJSDocConstructSignature(declaration); - // If this is a JSDoc construct signature, then skip the first parameter in the - // parameter list. The first parameter represents the return type of the construct - // signature. - for (var i = isJSConstructSignature ? 1 : 0, n = declaration.parameters.length; i < n; i++) { - var param = declaration.parameters[i]; - var paramSymbol = param.symbol; - // Include parameter symbol instead of property symbol in the signature - if (paramSymbol && !!(paramSymbol.flags & 4 /* Property */) && !ts.isBindingPattern(param.name)) { - var resolvedSymbol = resolveName(param, paramSymbol.name, 107455 /* Value */, undefined, undefined); - paramSymbol = resolvedSymbol; - } - if (i === 0 && paramSymbol.name === "this") { - hasThisParameter = true; - thisParameter = param.symbol; - } - else { - parameters.push(paramSymbol); - } - if (param.type && param.type.kind === 166 /* LiteralType */) { - hasLiteralTypes = true; - } - if (param.initializer || param.questionToken || param.dotDotDotToken || isJSDocOptionalParameter(param)) { - if (minArgumentCount < 0) { - minArgumentCount = i - (hasThisParameter ? 1 : 0); - } - } - else { - // If we see any required parameters, it means the prior ones were not in fact optional. - minArgumentCount = -1; - } - } - // If only one accessor includes a this-type annotation, the other behaves as if it had the same type annotation - if ((declaration.kind === 149 /* GetAccessor */ || declaration.kind === 150 /* SetAccessor */) && - !ts.hasDynamicName(declaration) && - (!hasThisParameter || !thisParameter)) { - var otherKind = declaration.kind === 149 /* GetAccessor */ ? 150 /* SetAccessor */ : 149 /* GetAccessor */; - var other = ts.getDeclarationOfKind(declaration.symbol, otherKind); - if (other) { - thisParameter = getAnnotatedAccessorThisParameter(other); - } - } - if (minArgumentCount < 0) { - minArgumentCount = declaration.parameters.length - (hasThisParameter ? 1 : 0); - } - if (isJSConstructSignature) { - minArgumentCount--; - } - if (!thisParameter && ts.isObjectLiteralMethod(declaration)) { - thisParameter = getContextualThisParameter(declaration); + // The class derives from a "class-like" constructor function, check that we have at least one construct signature + // with a matching number of type parameters and use the return type of the first instantiated signature. Elsewhere + // we check that all instantiated signatures return the same type. + var constructors = getInstantiatedConstructorsForTypeArguments(baseConstructorType, baseTypeNode.typeArguments); + if (!constructors.length) { + error(baseTypeNode.expression, ts.Diagnostics.No_base_constructor_has_the_specified_number_of_type_arguments); + return; } - var classType = declaration.kind === 148 /* Constructor */ ? - getDeclaredTypeOfClassOrInterface(getMergedSymbol(declaration.parent.symbol)) - : undefined; - var typeParameters = classType ? classType.localTypeParameters : - declaration.typeParameters ? getTypeParametersFromDeclaration(declaration.typeParameters) : - getTypeParametersFromJSDocTemplate(declaration); - var returnType = getSignatureReturnTypeFromDeclaration(declaration, minArgumentCount, isJSConstructSignature, classType); - var typePredicate = declaration.type && declaration.type.kind === 154 /* TypePredicate */ ? - createTypePredicateFromTypePredicateNode(declaration.type) : - undefined; - links.resolvedSignature = createSignature(declaration, typeParameters, thisParameter, parameters, returnType, typePredicate, minArgumentCount, ts.hasRestParameter(declaration), hasLiteralTypes); + baseType = getReturnTypeOfSignature(constructors[0]); } - return links.resolvedSignature; - } - function getSignatureReturnTypeFromDeclaration(declaration, minArgumentCount, isJSConstructSignature, classType) { - if (isJSConstructSignature) { - return getTypeFromTypeNode(declaration.parameters[0].type); + if (baseType === unknownType) { + return; } - else if (classType) { - return classType; + if (!(getTargetType(baseType).flags & (32768 /* Class */ | 65536 /* Interface */))) { + error(baseTypeNode.expression, ts.Diagnostics.Base_constructor_return_type_0_is_not_a_class_or_interface_type, typeToString(baseType)); + return; } - else if (declaration.type) { - return getTypeFromTypeNode(declaration.type); + if (type === baseType || hasBaseType(baseType, type)) { + error(type.symbol.valueDeclaration, ts.Diagnostics.Type_0_recursively_references_itself_as_a_base_type, typeToString(type, /*enclosingDeclaration*/ undefined, 1 /* WriteArrayAsGenericType */)); + return; } - if (declaration.flags & 1048576 /* JavaScriptFile */) { - var type = getReturnTypeFromJSDocComment(declaration); - if (type && type !== unknownType) { - return type; - } + if (type.resolvedBaseTypes === emptyArray) { + type.resolvedBaseTypes = [baseType]; } - // TypeScript 1.0 spec (April 2014): - // If only one accessor includes a type annotation, the other behaves as if it had the same type annotation. - if (declaration.kind === 149 /* GetAccessor */ && !ts.hasDynamicName(declaration)) { - var setter = ts.getDeclarationOfKind(declaration.symbol, 150 /* SetAccessor */); - return getAnnotatedAccessorType(setter); + else { + type.resolvedBaseTypes.push(baseType); } - if (ts.nodeIsMissing(declaration.body)) { - return anyType; + } + function areAllOuterTypeParametersApplied(type) { + // An unapplied type parameter has its symbol still the same as the matching argument symbol. + // Since parameters are applied outer-to-inner, only the last outer parameter needs to be checked. + var outerTypeParameters = type.outerTypeParameters; + if (outerTypeParameters) { + var last = outerTypeParameters.length - 1; + var typeArguments = type.typeArguments; + return outerTypeParameters[last].symbol !== typeArguments[last].symbol; } + return true; } - function getSignaturesOfSymbol(symbol) { - if (!symbol) - return emptyArray; - var result = []; - for (var i = 0, len = symbol.declarations.length; i < len; i++) { - var node = symbol.declarations[i]; - switch (node.kind) { - case 156 /* FunctionType */: - case 157 /* ConstructorType */: - case 220 /* FunctionDeclaration */: - case 147 /* MethodDeclaration */: - case 146 /* MethodSignature */: - case 148 /* Constructor */: - case 151 /* CallSignature */: - case 152 /* ConstructSignature */: - case 153 /* IndexSignature */: - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - case 179 /* FunctionExpression */: - case 180 /* ArrowFunction */: - case 269 /* JSDocFunctionType */: - // Don't include signature if node is the implementation of an overloaded function. A node is considered - // an implementation node if it has a body and the previous node is of the same kind and immediately - // precedes the implementation node (i.e. has the same parent and ends where the implementation starts). - if (i > 0 && node.body) { - var previous = symbol.declarations[i - 1]; - if (node.parent === previous.parent && node.kind === previous.kind && node.pos === previous.end) { - break; + function resolveBaseTypesOfInterface(type) { + type.resolvedBaseTypes = type.resolvedBaseTypes || emptyArray; + for (var _i = 0, _a = type.symbol.declarations; _i < _a.length; _i++) { + var declaration = _a[_i]; + if (declaration.kind === 222 /* InterfaceDeclaration */ && ts.getInterfaceBaseTypeNodes(declaration)) { + for (var _b = 0, _c = ts.getInterfaceBaseTypeNodes(declaration); _b < _c.length; _b++) { + var node = _c[_b]; + var baseType = getTypeFromTypeNode(node); + if (baseType !== unknownType) { + if (getTargetType(baseType).flags & (32768 /* Class */ | 65536 /* Interface */)) { + if (type !== baseType && !hasBaseType(baseType, type)) { + if (type.resolvedBaseTypes === emptyArray) { + type.resolvedBaseTypes = [baseType]; + } + else { + type.resolvedBaseTypes.push(baseType); + } + } + else { + error(declaration, ts.Diagnostics.Type_0_recursively_references_itself_as_a_base_type, typeToString(type, /*enclosingDeclaration*/ undefined, 1 /* WriteArrayAsGenericType */)); + } + } + else { + error(node, ts.Diagnostics.An_interface_may_only_extend_a_class_or_another_interface); } } - result.push(getSignatureFromDeclaration(node)); + } } } - return result; } - function resolveExternalModuleTypeByLiteral(name) { - var moduleSym = resolveExternalModuleName(name, name); - if (moduleSym) { - var resolvedModuleSymbol = resolveExternalModuleSymbol(moduleSym); - if (resolvedModuleSymbol) { - return getTypeOfSymbol(resolvedModuleSymbol); + // Returns true if the interface given by the symbol is free of "this" references. Specifically, the result is + // true if the interface itself contains no references to "this" in its body, if all base types are interfaces, + // and if none of the base interfaces have a "this" type. + function isIndependentInterface(symbol) { + for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { + var declaration = _a[_i]; + if (declaration.kind === 222 /* InterfaceDeclaration */) { + if (declaration.flags & 64 /* ContainsThis */) { + return false; + } + var baseTypeNodes = ts.getInterfaceBaseTypeNodes(declaration); + if (baseTypeNodes) { + for (var _b = 0, baseTypeNodes_1 = baseTypeNodes; _b < baseTypeNodes_1.length; _b++) { + var node = baseTypeNodes_1[_b]; + if (ts.isEntityNameExpression(node.expression)) { + var baseSymbol = resolveEntityName(node.expression, 793064 /* Type */, /*ignoreErrors*/ true); + if (!baseSymbol || !(baseSymbol.flags & 64 /* Interface */) || getDeclaredTypeOfClassOrInterface(baseSymbol).thisType) { + return false; + } + } + } + } } } - return anyType; + return true; } - function getThisTypeOfSignature(signature) { - if (signature.thisParameter) { - return getTypeOfSymbol(signature.thisParameter); + function getDeclaredTypeOfClassOrInterface(symbol) { + var links = getSymbolLinks(symbol); + if (!links.declaredType) { + var kind = symbol.flags & 32 /* Class */ ? 32768 /* Class */ : 65536 /* Interface */; + var type = links.declaredType = createObjectType(kind, symbol); + var outerTypeParameters = getOuterTypeParametersOfClassOrInterface(symbol); + var localTypeParameters = getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol); + // A class or interface is generic if it has type parameters or a "this" type. We always give classes a "this" type + // because it is not feasible to analyze all members to determine if the "this" type escapes the class (in particular, + // property types inferred from initializers and method return types inferred from return statements are very hard + // to exhaustively analyze). We give interfaces a "this" type if we can't definitely determine that they are free of + // "this" references. + if (outerTypeParameters || localTypeParameters || kind === 32768 /* Class */ || !isIndependentInterface(symbol)) { + type.flags |= 131072 /* Reference */; + type.typeParameters = ts.concatenate(outerTypeParameters, localTypeParameters); + type.outerTypeParameters = outerTypeParameters; + type.localTypeParameters = localTypeParameters; + type.instantiations = ts.createMap(); + type.instantiations[getTypeListId(type.typeParameters)] = type; + type.target = type; + type.typeArguments = type.typeParameters; + type.thisType = createType(16384 /* TypeParameter */); + type.thisType.isThisType = true; + type.thisType.symbol = symbol; + type.thisType.constraint = type; + } } + return links.declaredType; } - function getReturnTypeOfSignature(signature) { - if (!signature.resolvedReturnType) { - if (!pushTypeResolution(signature, 3 /* ResolvedReturnType */)) { + function getDeclaredTypeOfTypeAlias(symbol) { + var links = getSymbolLinks(symbol); + if (!links.declaredType) { + // Note that we use the links object as the target here because the symbol object is used as the unique + // identity for resolution of the 'type' property in SymbolLinks. + if (!pushTypeResolution(symbol, 2 /* DeclaredType */)) { return unknownType; } + var typeParameters = getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(symbol); + var declaration = ts.getDeclarationOfKind(symbol, 279 /* JSDocTypedefTag */); var type = void 0; - if (signature.target) { - type = instantiateType(getReturnTypeOfSignature(signature.target), signature.mapper); - } - else if (signature.unionSignatures) { - type = getUnionType(ts.map(signature.unionSignatures, getReturnTypeOfSignature), /*subtypeReduction*/ true); + if (declaration) { + if (declaration.jsDocTypeLiteral) { + type = getTypeFromTypeNode(declaration.jsDocTypeLiteral); + } + else { + type = getTypeFromTypeNode(declaration.typeExpression.type); + } } else { - type = getReturnTypeFromBody(signature.declaration); + declaration = ts.getDeclarationOfKind(symbol, 223 /* TypeAliasDeclaration */); + type = getTypeFromTypeNode(declaration.type, symbol, typeParameters); } - if (!popTypeResolution()) { - type = anyType; - if (compilerOptions.noImplicitAny) { - var declaration = signature.declaration; - if (declaration.name) { - error(declaration.name, ts.Diagnostics._0_implicitly_has_return_type_any_because_it_does_not_have_a_return_type_annotation_and_is_referenced_directly_or_indirectly_in_one_of_its_return_expressions, ts.declarationNameToString(declaration.name)); - } - else { - error(declaration, ts.Diagnostics.Function_implicitly_has_return_type_any_because_it_does_not_have_a_return_type_annotation_and_is_referenced_directly_or_indirectly_in_one_of_its_return_expressions); - } + if (popTypeResolution()) { + links.typeParameters = typeParameters; + if (typeParameters) { + // Initialize the instantiation cache for generic type aliases. The declared type corresponds to + // an instantiation of the type alias with the type parameters supplied as type arguments. + links.instantiations = ts.createMap(); + links.instantiations[getTypeListId(links.typeParameters)] = type; } } - signature.resolvedReturnType = type; - } - return signature.resolvedReturnType; - } - function getRestTypeOfSignature(signature) { - if (signature.hasRestParameter) { - var type = getTypeOfSymbol(ts.lastOrUndefined(signature.parameters)); - if (type.flags & 131072 /* Reference */ && type.target === globalArrayType) { - return type.typeArguments[0]; + else { + type = unknownType; + error(declaration.name, ts.Diagnostics.Type_alias_0_circularly_references_itself, symbolToString(symbol)); } + links.declaredType = type; } - return anyType; - } - function getSignatureInstantiation(signature, typeArguments) { - return instantiateSignature(signature, createTypeMapper(signature.typeParameters, typeArguments), /*eraseTypeParameters*/ true); - } - function getErasedSignature(signature) { - if (!signature.typeParameters) - return signature; - if (!signature.erasedSignatureCache) { - signature.erasedSignatureCache = instantiateSignature(signature, createTypeEraser(signature.typeParameters), /*eraseTypeParameters*/ true); - } - return signature.erasedSignatureCache; + return links.declaredType; } - function getOrCreateTypeFromSignature(signature) { - // There are two ways to declare a construct signature, one is by declaring a class constructor - // using the constructor keyword, and the other is declaring a bare construct signature in an - // object type literal or interface (using the new keyword). Each way of declaring a constructor - // will result in a different declaration kind. - if (!signature.isolatedSignatureType) { - var isConstructor = signature.declaration.kind === 148 /* Constructor */ || signature.declaration.kind === 152 /* ConstructSignature */; - var type = createObjectType(2097152 /* Anonymous */); - type.members = emptySymbols; - type.properties = emptyArray; - type.callSignatures = !isConstructor ? [signature] : emptyArray; - type.constructSignatures = isConstructor ? [signature] : emptyArray; - signature.isolatedSignatureType = type; + function isLiteralEnumMember(symbol, member) { + var expr = member.initializer; + if (!expr) { + return !ts.isInAmbientContext(member); } - return signature.isolatedSignatureType; - } - function getIndexSymbol(symbol) { - return symbol.members["__index"]; + return expr.kind === 8 /* NumericLiteral */ || + expr.kind === 185 /* PrefixUnaryExpression */ && expr.operator === 36 /* MinusToken */ && + expr.operand.kind === 8 /* NumericLiteral */ || + expr.kind === 69 /* Identifier */ && !!symbol.exports[expr.text]; } - function getIndexDeclarationOfSymbol(symbol, kind) { - var syntaxKind = kind === 1 /* Number */ ? 130 /* NumberKeyword */ : 132 /* StringKeyword */; - var indexSymbol = getIndexSymbol(symbol); - if (indexSymbol) { - for (var _i = 0, _a = indexSymbol.declarations; _i < _a.length; _i++) { - var decl = _a[_i]; - var node = decl; - if (node.parameters.length === 1) { - var parameter = node.parameters[0]; - if (parameter && parameter.type && parameter.type.kind === syntaxKind) { - return node; + function enumHasLiteralMembers(symbol) { + for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { + var declaration = _a[_i]; + if (declaration.kind === 224 /* EnumDeclaration */) { + for (var _b = 0, _c = declaration.members; _b < _c.length; _b++) { + var member = _c[_b]; + if (!isLiteralEnumMember(symbol, member)) { + return false; } } } } - return undefined; - } - function createIndexInfo(type, isReadonly, declaration) { - return { type: type, isReadonly: isReadonly, declaration: declaration }; - } - function getIndexInfoOfSymbol(symbol, kind) { - var declaration = getIndexDeclarationOfSymbol(symbol, kind); - if (declaration) { - return createIndexInfo(declaration.type ? getTypeFromTypeNode(declaration.type) : anyType, (ts.getModifierFlags(declaration) & 64 /* Readonly */) !== 0, declaration); - } - return undefined; + return true; } - function getConstraintDeclaration(type) { - return ts.getDeclarationOfKind(type.symbol, 141 /* TypeParameter */).constraint; + function createEnumLiteralType(symbol, baseType, text) { + var type = createType(256 /* EnumLiteral */); + type.symbol = symbol; + type.baseType = baseType; + type.text = text; + return type; } - function hasConstraintReferenceTo(type, target) { - var checked; - while (type && !(type.flags & 268435456 /* ThisType */) && type.flags & 16384 /* TypeParameter */ && !ts.contains(checked, type)) { - if (type === target) { - return true; - } - (checked || (checked = [])).push(type); - var constraintDeclaration = getConstraintDeclaration(type); - type = constraintDeclaration && getTypeFromTypeNode(constraintDeclaration); - } - return false; - } - function getConstraintOfTypeParameter(typeParameter) { - if (!typeParameter.constraint) { - if (typeParameter.target) { - var targetConstraint = getConstraintOfTypeParameter(typeParameter.target); - typeParameter.constraint = targetConstraint ? instantiateType(targetConstraint, typeParameter.mapper) : noConstraintType; - } - else { - var constraintDeclaration = getConstraintDeclaration(typeParameter); - var constraint = getTypeFromTypeNode(constraintDeclaration); - if (hasConstraintReferenceTo(constraint, typeParameter)) { - error(constraintDeclaration, ts.Diagnostics.Type_parameter_0_has_a_circular_constraint, typeToString(typeParameter)); - constraint = unknownType; + function getDeclaredTypeOfEnum(symbol) { + var links = getSymbolLinks(symbol); + if (!links.declaredType) { + var enumType = links.declaredType = createType(16 /* Enum */); + enumType.symbol = symbol; + if (enumHasLiteralMembers(symbol)) { + var memberTypeList = []; + var memberTypes = ts.createMap(); + for (var _i = 0, _a = enumType.symbol.declarations; _i < _a.length; _i++) { + var declaration = _a[_i]; + if (declaration.kind === 224 /* EnumDeclaration */) { + computeEnumMemberValues(declaration); + for (var _b = 0, _c = declaration.members; _b < _c.length; _b++) { + var member = _c[_b]; + var memberSymbol = getSymbolOfNode(member); + var value = getEnumMemberValue(member); + if (!memberTypes[value]) { + var memberType = memberTypes[value] = createEnumLiteralType(memberSymbol, enumType, "" + value); + memberTypeList.push(memberType); + } + } + } + } + enumType.memberTypes = memberTypes; + if (memberTypeList.length > 1) { + enumType.flags |= 524288 /* Union */; + enumType.types = memberTypeList; + unionTypes[getTypeListId(memberTypeList)] = enumType; } - typeParameter.constraint = constraint; } } - return typeParameter.constraint === noConstraintType ? undefined : typeParameter.constraint; - } - function getParentSymbolOfTypeParameter(typeParameter) { - return getSymbolOfNode(ts.getDeclarationOfKind(typeParameter.symbol, 141 /* TypeParameter */).parent); + return links.declaredType; } - function getTypeListId(types) { - var result = ""; - if (types) { - var length_3 = types.length; - var i = 0; - while (i < length_3) { - var startId = types[i].id; - var count = 1; - while (i + count < length_3 && types[i + count].id === startId + count) { - count++; - } - if (result.length) { - result += ","; - } - result += startId; - if (count > 1) { - result += ":" + count; - } - i += count; - } + function getDeclaredTypeOfEnumMember(symbol) { + var links = getSymbolLinks(symbol); + if (!links.declaredType) { + var enumType = getDeclaredTypeOfEnum(getParentOfSymbol(symbol)); + links.declaredType = enumType.flags & 524288 /* Union */ ? + enumType.memberTypes[getEnumMemberValue(symbol.valueDeclaration)] : + enumType; } - return result; + return links.declaredType; } - // This function is used to propagate certain flags when creating new object type references and union types. - // It is only necessary to do so if a constituent type might be the undefined type, the null type, the type - // of an object literal or the anyFunctionType. This is because there are operations in the type checker - // that care about the presence of such types at arbitrary depth in a containing type. - function getPropagatingFlagsOfTypes(types, excludeKinds) { - var result = 0; - for (var _i = 0, types_3 = types; _i < types_3.length; _i++) { - var type = types_3[_i]; - if (!(type.flags & excludeKinds)) { - result |= type.flags; + function getDeclaredTypeOfTypeParameter(symbol) { + var links = getSymbolLinks(symbol); + if (!links.declaredType) { + var type = createType(16384 /* TypeParameter */); + type.symbol = symbol; + if (!ts.getDeclarationOfKind(symbol, 141 /* TypeParameter */).constraint) { + type.constraint = noConstraintType; } + links.declaredType = type; } - return result & 234881024 /* PropagatingFlags */; + return links.declaredType; } - function createTypeReference(target, typeArguments) { - var id = getTypeListId(typeArguments); - var type = target.instantiations[id]; - if (!type) { - var propagatedFlags = typeArguments ? getPropagatingFlagsOfTypes(typeArguments, /*excludeKinds*/ 0) : 0; - var flags = 131072 /* Reference */ | propagatedFlags; - type = target.instantiations[id] = createObjectType(flags, target.symbol); - type.target = target; - type.typeArguments = typeArguments; + function getDeclaredTypeOfAlias(symbol) { + var links = getSymbolLinks(symbol); + if (!links.declaredType) { + links.declaredType = getDeclaredTypeOfSymbol(resolveAlias(symbol)); } - return type; - } - function cloneTypeReference(source) { - var type = createObjectType(source.flags, source.symbol); - type.target = source.target; - type.typeArguments = source.typeArguments; - return type; - } - function getTypeReferenceArity(type) { - return type.target.typeParameters ? type.target.typeParameters.length : 0; + return links.declaredType; } - // Get type from reference to class or interface - function getTypeFromClassOrInterfaceReference(node, symbol) { - var type = getDeclaredTypeOfSymbol(getMergedSymbol(symbol)); - var typeParameters = type.localTypeParameters; - if (typeParameters) { - if (!node.typeArguments || node.typeArguments.length !== typeParameters.length) { - error(node, ts.Diagnostics.Generic_type_0_requires_1_type_argument_s, typeToString(type, /*enclosingDeclaration*/ undefined, 1 /* WriteArrayAsGenericType */), typeParameters.length); - return unknownType; - } - // In a type reference, the outer type parameters of the referenced class or interface are automatically - // supplied as type arguments and the type reference only specifies arguments for the local type parameters - // of the class or interface. - return createTypeReference(type, ts.concatenate(type.outerTypeParameters, ts.map(node.typeArguments, getTypeFromTypeNodeNoAlias))); + function getDeclaredTypeOfSymbol(symbol) { + ts.Debug.assert((symbol.flags & 16777216 /* Instantiated */) === 0); + if (symbol.flags & (32 /* Class */ | 64 /* Interface */)) { + return getDeclaredTypeOfClassOrInterface(symbol); } - if (node.typeArguments) { - error(node, ts.Diagnostics.Type_0_is_not_generic, typeToString(type)); - return unknownType; + if (symbol.flags & 524288 /* TypeAlias */) { + return getDeclaredTypeOfTypeAlias(symbol); } - return type; - } - // Get type from reference to type alias. When a type alias is generic, the declared type of the type alias may include - // references to the type parameters of the alias. We replace those with the actual type arguments by instantiating the - // declared type. Instantiations are cached using the type identities of the type arguments as the key. - function getTypeFromTypeAliasReference(node, symbol) { - var type = getDeclaredTypeOfSymbol(symbol); - var links = getSymbolLinks(symbol); - var typeParameters = links.typeParameters; - if (typeParameters) { - if (!node.typeArguments || node.typeArguments.length !== typeParameters.length) { - error(node, ts.Diagnostics.Generic_type_0_requires_1_type_argument_s, symbolToString(symbol), typeParameters.length); - return unknownType; - } - var typeArguments = ts.map(node.typeArguments, getTypeFromTypeNodeNoAlias); - var id = getTypeListId(typeArguments); - return links.instantiations[id] || (links.instantiations[id] = instantiateType(type, createTypeMapper(typeParameters, typeArguments))); + if (symbol.flags & 262144 /* TypeParameter */) { + return getDeclaredTypeOfTypeParameter(symbol); } - if (node.typeArguments) { - error(node, ts.Diagnostics.Type_0_is_not_generic, symbolToString(symbol)); - return unknownType; + if (symbol.flags & 384 /* Enum */) { + return getDeclaredTypeOfEnum(symbol); } - return type; + if (symbol.flags & 8 /* EnumMember */) { + return getDeclaredTypeOfEnumMember(symbol); + } + if (symbol.flags & 8388608 /* Alias */) { + return getDeclaredTypeOfAlias(symbol); + } + return unknownType; } - // Get type from reference to named type that cannot be generic (enum or type parameter) - function getTypeFromNonGenericTypeReference(node, symbol) { + // A type reference is considered independent if each type argument is considered independent. + function isIndependentTypeReference(node) { if (node.typeArguments) { - error(node, ts.Diagnostics.Type_0_is_not_generic, symbolToString(symbol)); - return unknownType; + for (var _i = 0, _a = node.typeArguments; _i < _a.length; _i++) { + var typeNode = _a[_i]; + if (!isIndependentType(typeNode)) { + return false; + } + } } - return getDeclaredTypeOfSymbol(symbol); + return true; } - function getTypeReferenceName(node) { + // A type is considered independent if it the any, string, number, boolean, symbol, or void keyword, a string + // literal type, an array with an element type that is considered independent, or a type reference that is + // considered independent. + function isIndependentType(node) { switch (node.kind) { + case 117 /* AnyKeyword */: + case 132 /* StringKeyword */: + case 130 /* NumberKeyword */: + case 120 /* BooleanKeyword */: + case 133 /* SymbolKeyword */: + case 103 /* VoidKeyword */: + case 135 /* UndefinedKeyword */: + case 93 /* NullKeyword */: + case 127 /* NeverKeyword */: + case 166 /* LiteralType */: + return true; + case 160 /* ArrayType */: + return isIndependentType(node.elementType); case 155 /* TypeReference */: - return node.typeName; - case 267 /* JSDocTypeReference */: - return node.name; - case 194 /* ExpressionWithTypeArguments */: - // We only support expressions that are simple qualified names. For other - // expressions this produces undefined. - var expr = node.expression; - if (ts.isEntityNameExpression(expr)) { - return expr; - } + return isIndependentTypeReference(node); } - return undefined; + return false; } - function resolveTypeReferenceName(node, typeReferenceName) { - if (!typeReferenceName) { - return unknownSymbol; - } - return resolveEntityName(typeReferenceName, 793064 /* Type */) || unknownSymbol; + // A variable-like declaration is considered independent (free of this references) if it has a type annotation + // that specifies an independent type, or if it has no type annotation and no initializer (and thus of type any). + function isIndependentVariableLikeDeclaration(node) { + return node.type && isIndependentType(node.type) || !node.type && !node.initializer; } - function getTypeReferenceType(node, symbol) { - if (symbol === unknownSymbol) { - return unknownType; - } - if (symbol.flags & (32 /* Class */ | 64 /* Interface */)) { - return getTypeFromClassOrInterfaceReference(node, symbol); - } - if (symbol.flags & 524288 /* TypeAlias */) { - return getTypeFromTypeAliasReference(node, symbol); - } - if (symbol.flags & 107455 /* Value */ && node.kind === 267 /* JSDocTypeReference */) { - // A JSDocTypeReference may have resolved to a value (as opposed to a type). In - // that case, the type of this reference is just the type of the value we resolved - // to. - return getTypeOfSymbol(symbol); + // A function-like declaration is considered independent (free of this references) if it has a return type + // annotation that is considered independent and if each parameter is considered independent. + function isIndependentFunctionLikeDeclaration(node) { + if (node.kind !== 148 /* Constructor */ && (!node.type || !isIndependentType(node.type))) { + return false; } - return getTypeFromNonGenericTypeReference(node, symbol); - } - function getTypeFromTypeReference(node) { - var links = getNodeLinks(node); - if (!links.resolvedType) { - var symbol = void 0; - var type = void 0; - if (node.kind === 267 /* JSDocTypeReference */) { - var typeReferenceName = getTypeReferenceName(node); - symbol = resolveTypeReferenceName(node, typeReferenceName); - type = getTypeReferenceType(node, symbol); - } - else { - // We only support expressions that are simple qualified names. For other expressions this produces undefined. - var typeNameOrExpression = node.kind === 155 /* TypeReference */ - ? node.typeName - : ts.isEntityNameExpression(node.expression) - ? node.expression - : undefined; - symbol = typeNameOrExpression && resolveEntityName(typeNameOrExpression, 793064 /* Type */) || unknownSymbol; - type = symbol === unknownSymbol ? unknownType : - symbol.flags & (32 /* Class */ | 64 /* Interface */) ? getTypeFromClassOrInterfaceReference(node, symbol) : - symbol.flags & 524288 /* TypeAlias */ ? getTypeFromTypeAliasReference(node, symbol) : - getTypeFromNonGenericTypeReference(node, symbol); + for (var _i = 0, _a = node.parameters; _i < _a.length; _i++) { + var parameter = _a[_i]; + if (!isIndependentVariableLikeDeclaration(parameter)) { + return false; } - // Cache both the resolved symbol and the resolved type. The resolved symbol is needed in when we check the - // type reference in checkTypeReferenceOrExpressionWithTypeArguments. - links.resolvedSymbol = symbol; - links.resolvedType = type; - } - return links.resolvedType; - } - function getTypeFromTypeQueryNode(node) { - var links = getNodeLinks(node); - if (!links.resolvedType) { - // TypeScript 1.0 spec (April 2014): 3.6.3 - // The expression is processed as an identifier expression (section 4.3) - // or property access expression(section 4.10), - // the widened type(section 3.9) of which becomes the result. - links.resolvedType = getWidenedType(checkExpression(node.exprName)); } - return links.resolvedType; + return true; } - function getTypeOfGlobalSymbol(symbol, arity) { - function getTypeDeclaration(symbol) { - var declarations = symbol.declarations; - for (var _i = 0, declarations_3 = declarations; _i < declarations_3.length; _i++) { - var declaration = declarations_3[_i]; + // Returns true if the class or interface member given by the symbol is free of "this" references. The + // function may return false for symbols that are actually free of "this" references because it is not + // feasible to perform a complete analysis in all cases. In particular, property members with types + // inferred from their initializers and function members with inferred return types are conservatively + // assumed not to be free of "this" references. + function isIndependentMember(symbol) { + if (symbol.declarations && symbol.declarations.length === 1) { + var declaration = symbol.declarations[0]; + if (declaration) { switch (declaration.kind) { - case 221 /* ClassDeclaration */: - case 222 /* InterfaceDeclaration */: - case 224 /* EnumDeclaration */: - return declaration; + case 145 /* PropertyDeclaration */: + case 144 /* PropertySignature */: + return isIndependentVariableLikeDeclaration(declaration); + case 147 /* MethodDeclaration */: + case 146 /* MethodSignature */: + case 148 /* Constructor */: + return isIndependentFunctionLikeDeclaration(declaration); } } } - if (!symbol) { - return arity ? emptyGenericType : emptyObjectType; - } - var type = getDeclaredTypeOfSymbol(symbol); - if (!(type.flags & 2588672 /* ObjectType */)) { - error(getTypeDeclaration(symbol), ts.Diagnostics.Global_type_0_must_be_a_class_or_interface_type, symbol.name); - return arity ? emptyGenericType : emptyObjectType; - } - if ((type.typeParameters ? type.typeParameters.length : 0) !== arity) { - error(getTypeDeclaration(symbol), ts.Diagnostics.Global_type_0_must_have_1_type_parameter_s, symbol.name, arity); - return arity ? emptyGenericType : emptyObjectType; - } - return type; - } - function getGlobalValueSymbol(name) { - return getGlobalSymbol(name, 107455 /* Value */, ts.Diagnostics.Cannot_find_global_value_0); - } - function getGlobalTypeSymbol(name) { - return getGlobalSymbol(name, 793064 /* Type */, ts.Diagnostics.Cannot_find_global_type_0); - } - function getGlobalSymbol(name, meaning, diagnostic) { - return resolveName(undefined, name, meaning, diagnostic, name); - } - function getGlobalType(name, arity) { - if (arity === void 0) { arity = 0; } - return getTypeOfGlobalSymbol(getGlobalTypeSymbol(name), arity); - } - /** - * Returns a type that is inside a namespace at the global scope, e.g. - * getExportedTypeFromNamespace('JSX', 'Element') returns the JSX.Element type - */ - function getExportedTypeFromNamespace(namespace, name) { - var namespaceSymbol = getGlobalSymbol(namespace, 1920 /* Namespace */, /*diagnosticMessage*/ undefined); - var typeSymbol = namespaceSymbol && getSymbol(namespaceSymbol.exports, name, 793064 /* Type */); - return typeSymbol && getDeclaredTypeOfSymbol(typeSymbol); - } - /** - * Creates a TypeReference for a generic `TypedPropertyDescriptor`. - */ - function createTypedPropertyDescriptorType(propertyType) { - var globalTypedPropertyDescriptorType = getGlobalTypedPropertyDescriptorType(); - return globalTypedPropertyDescriptorType !== emptyGenericType - ? createTypeReference(globalTypedPropertyDescriptorType, [propertyType]) - : emptyObjectType; - } - /** - * Instantiates a global type that is generic with some element type, and returns that instantiation. - */ - function createTypeFromGenericGlobalType(genericGlobalType, typeArguments) { - return genericGlobalType !== emptyGenericType ? createTypeReference(genericGlobalType, typeArguments) : emptyObjectType; - } - function createIterableType(elementType) { - return createTypeFromGenericGlobalType(getGlobalIterableType(), [elementType]); + return false; } - function createIterableIteratorType(elementType) { - return createTypeFromGenericGlobalType(getGlobalIterableIteratorType(), [elementType]); + function createSymbolTable(symbols) { + var result = ts.createMap(); + for (var _i = 0, symbols_1 = symbols; _i < symbols_1.length; _i++) { + var symbol = symbols_1[_i]; + result[symbol.name] = symbol; + } + return result; } - function createArrayType(elementType) { - return createTypeFromGenericGlobalType(globalArrayType, [elementType]); + // The mappingThisOnly flag indicates that the only type parameter being mapped is "this". When the flag is true, + // we check symbols to see if we can quickly conclude they are free of "this" references, thus needing no instantiation. + function createInstantiatedSymbolTable(symbols, mapper, mappingThisOnly) { + var result = ts.createMap(); + for (var _i = 0, symbols_2 = symbols; _i < symbols_2.length; _i++) { + var symbol = symbols_2[_i]; + result[symbol.name] = mappingThisOnly && isIndependentMember(symbol) ? symbol : instantiateSymbol(symbol, mapper); + } + return result; } - function getTypeFromArrayTypeNode(node) { - var links = getNodeLinks(node); - if (!links.resolvedType) { - links.resolvedType = createArrayType(getTypeFromTypeNode(node.elementType)); + function addInheritedMembers(symbols, baseSymbols) { + for (var _i = 0, baseSymbols_1 = baseSymbols; _i < baseSymbols_1.length; _i++) { + var s = baseSymbols_1[_i]; + if (!symbols[s.name]) { + symbols[s.name] = s; + } } - return links.resolvedType; } - // We represent tuple types as type references to synthesized generic interface types created by - // this function. The types are of the form: - // - // interface Tuple extends Array { 0: T0, 1: T1, 2: T2, ... } - // - // Note that the generic type created by this function has no symbol associated with it. The same - // is true for each of the synthesized type parameters. - function createTupleTypeOfArity(arity) { - var typeParameters = []; - var properties = []; - for (var i = 0; i < arity; i++) { - var typeParameter = createType(16384 /* TypeParameter */); - typeParameters.push(typeParameter); - var property = createSymbol(4 /* Property */ | 67108864 /* Transient */, "" + i); - property.type = typeParameter; - properties.push(property); + function resolveDeclaredMembers(type) { + if (!type.declaredProperties) { + var symbol = type.symbol; + type.declaredProperties = getNamedMembers(symbol.members); + type.declaredCallSignatures = getSignaturesOfSymbol(symbol.members["__call"]); + type.declaredConstructSignatures = getSignaturesOfSymbol(symbol.members["__new"]); + type.declaredStringIndexInfo = getIndexInfoOfSymbol(symbol, 0 /* String */); + type.declaredNumberIndexInfo = getIndexInfoOfSymbol(symbol, 1 /* Number */); } - var type = createObjectType(262144 /* Tuple */ | 131072 /* Reference */); - type.typeParameters = typeParameters; - type.outerTypeParameters = undefined; - type.localTypeParameters = typeParameters; - type.instantiations = ts.createMap(); - type.instantiations[getTypeListId(type.typeParameters)] = type; - type.target = type; - type.typeArguments = type.typeParameters; - type.thisType = createType(16384 /* TypeParameter */ | 268435456 /* ThisType */); - type.thisType.constraint = type; - type.declaredProperties = properties; - type.declaredCallSignatures = emptyArray; - type.declaredConstructSignatures = emptyArray; - type.declaredStringIndexInfo = undefined; - type.declaredNumberIndexInfo = undefined; return type; } - function getTupleTypeOfArity(arity) { - return tupleTypes[arity] || (tupleTypes[arity] = createTupleTypeOfArity(arity)); - } - function createTupleType(elementTypes) { - return createTypeReference(getTupleTypeOfArity(elementTypes.length), elementTypes); - } - function getTypeFromTupleTypeNode(node) { - var links = getNodeLinks(node); - if (!links.resolvedType) { - links.resolvedType = createTupleType(ts.map(node.elementTypes, getTypeFromTypeNodeNoAlias)); + function getTypeWithThisArgument(type, thisArgument) { + if (type.flags & 131072 /* Reference */) { + return createTypeReference(type.target, ts.concatenate(type.typeArguments, [thisArgument || type.target.thisType])); } - return links.resolvedType; + return type; } - function binarySearchTypes(types, type) { - var low = 0; - var high = types.length - 1; - var typeId = type.id; - while (low <= high) { - var middle = low + ((high - low) >> 1); - var id = types[middle].id; - if (id === typeId) { - return middle; - } - else if (id > typeId) { - high = middle - 1; + function resolveObjectTypeMembers(type, source, typeParameters, typeArguments) { + var mapper; + var members; + var callSignatures; + var constructSignatures; + var stringIndexInfo; + var numberIndexInfo; + if (ts.rangeEquals(typeParameters, typeArguments, 0, typeParameters.length)) { + mapper = identityMapper; + members = source.symbol ? source.symbol.members : createSymbolTable(source.declaredProperties); + callSignatures = source.declaredCallSignatures; + constructSignatures = source.declaredConstructSignatures; + stringIndexInfo = source.declaredStringIndexInfo; + numberIndexInfo = source.declaredNumberIndexInfo; + } + else { + mapper = createTypeMapper(typeParameters, typeArguments); + members = createInstantiatedSymbolTable(source.declaredProperties, mapper, /*mappingThisOnly*/ typeParameters.length === 1); + callSignatures = instantiateList(source.declaredCallSignatures, mapper, instantiateSignature); + constructSignatures = instantiateList(source.declaredConstructSignatures, mapper, instantiateSignature); + stringIndexInfo = instantiateIndexInfo(source.declaredStringIndexInfo, mapper); + numberIndexInfo = instantiateIndexInfo(source.declaredNumberIndexInfo, mapper); + } + var baseTypes = getBaseTypes(source); + if (baseTypes.length) { + if (source.symbol && members === source.symbol.members) { + members = createSymbolTable(source.declaredProperties); } - else { - low = middle + 1; + var thisArgument = ts.lastOrUndefined(typeArguments); + for (var _i = 0, baseTypes_1 = baseTypes; _i < baseTypes_1.length; _i++) { + var baseType = baseTypes_1[_i]; + var instantiatedBaseType = thisArgument ? getTypeWithThisArgument(instantiateType(baseType, mapper), thisArgument) : baseType; + addInheritedMembers(members, getPropertiesOfObjectType(instantiatedBaseType)); + callSignatures = ts.concatenate(callSignatures, getSignaturesOfType(instantiatedBaseType, 0 /* Call */)); + constructSignatures = ts.concatenate(constructSignatures, getSignaturesOfType(instantiatedBaseType, 1 /* Construct */)); + stringIndexInfo = stringIndexInfo || getIndexInfoOfType(instantiatedBaseType, 0 /* String */); + numberIndexInfo = numberIndexInfo || getIndexInfoOfType(instantiatedBaseType, 1 /* Number */); } } - return ~low; - } - function containsType(types, type) { - return binarySearchTypes(types, type) >= 0; + setObjectTypeMembers(type, members, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo); } - function addTypeToUnion(typeSet, type) { - var flags = type.flags; - if (flags & 524288 /* Union */) { - addTypesToUnion(typeSet, type.types); - } - else if (flags & 1 /* Any */) { - typeSet.containsAny = true; - } - else if (!strictNullChecks && flags & 6144 /* Nullable */) { - if (flags & 2048 /* Undefined */) - typeSet.containsUndefined = true; - if (flags & 4096 /* Null */) - typeSet.containsNull = true; - if (!(flags & 33554432 /* ContainsWideningType */)) - typeSet.containsNonWideningType = true; + function resolveClassOrInterfaceMembers(type) { + resolveObjectTypeMembers(type, resolveDeclaredMembers(type), emptyArray, emptyArray); + } + function resolveTypeReferenceMembers(type) { + var source = resolveDeclaredMembers(type.target); + var typeParameters = ts.concatenate(source.typeParameters, [source.thisType]); + var typeArguments = type.typeArguments && type.typeArguments.length === typeParameters.length ? + type.typeArguments : ts.concatenate(type.typeArguments, [type]); + resolveObjectTypeMembers(type, source, typeParameters, typeArguments); + } + function createSignature(declaration, typeParameters, thisParameter, parameters, resolvedReturnType, typePredicate, minArgumentCount, hasRestParameter, hasLiteralTypes) { + var sig = new Signature(checker); + sig.declaration = declaration; + sig.typeParameters = typeParameters; + sig.parameters = parameters; + sig.thisParameter = thisParameter; + sig.resolvedReturnType = resolvedReturnType; + sig.typePredicate = typePredicate; + sig.minArgumentCount = minArgumentCount; + sig.hasRestParameter = hasRestParameter; + sig.hasLiteralTypes = hasLiteralTypes; + return sig; + } + function cloneSignature(sig) { + return createSignature(sig.declaration, sig.typeParameters, sig.thisParameter, sig.parameters, sig.resolvedReturnType, sig.typePredicate, sig.minArgumentCount, sig.hasRestParameter, sig.hasLiteralTypes); + } + function getDefaultConstructSignatures(classType) { + var baseConstructorType = getBaseConstructorTypeOfClass(classType); + var baseSignatures = getSignaturesOfType(baseConstructorType, 1 /* Construct */); + if (baseSignatures.length === 0) { + return [createSignature(undefined, classType.localTypeParameters, undefined, emptyArray, classType, /*typePredicate*/ undefined, 0, /*hasRestParameter*/ false, /*hasLiteralTypes*/ false)]; } - else if (!(flags & 8192 /* Never */)) { - if (flags & 2 /* String */) - typeSet.containsString = true; - if (flags & 4 /* Number */) - typeSet.containsNumber = true; - if (flags & 96 /* StringOrNumberLiteral */) - typeSet.containsStringOrNumberLiteral = true; - var len = typeSet.length; - var index = len && type.id > typeSet[len - 1].id ? ~len : binarySearchTypes(typeSet, type); - if (index < 0) { - if (!(flags & 2097152 /* Anonymous */ && type.symbol && type.symbol.flags & (16 /* Function */ | 8192 /* Method */) && containsIdenticalType(typeSet, type))) { - typeSet.splice(~index, 0, type); - } + var baseTypeNode = getBaseTypeNodeOfClass(classType); + var typeArguments = ts.map(baseTypeNode.typeArguments, getTypeFromTypeNodeNoAlias); + var typeArgCount = typeArguments ? typeArguments.length : 0; + var result = []; + for (var _i = 0, baseSignatures_1 = baseSignatures; _i < baseSignatures_1.length; _i++) { + var baseSig = baseSignatures_1[_i]; + var typeParamCount = baseSig.typeParameters ? baseSig.typeParameters.length : 0; + if (typeParamCount === typeArgCount) { + var sig = typeParamCount ? getSignatureInstantiation(baseSig, typeArguments) : cloneSignature(baseSig); + sig.typeParameters = classType.localTypeParameters; + sig.resolvedReturnType = classType; + result.push(sig); } } + return result; } - // Add the given types to the given type set. Order is preserved, duplicates are removed, - // and nested types of the given kind are flattened into the set. - function addTypesToUnion(typeSet, types) { - for (var _i = 0, types_4 = types; _i < types_4.length; _i++) { - var type = types_4[_i]; - addTypeToUnion(typeSet, type); + function findMatchingSignature(signatureList, signature, partialMatch, ignoreThisTypes, ignoreReturnTypes) { + for (var _i = 0, signatureList_1 = signatureList; _i < signatureList_1.length; _i++) { + var s = signatureList_1[_i]; + if (compareSignaturesIdentical(s, signature, partialMatch, ignoreThisTypes, ignoreReturnTypes, compareTypesIdentical)) { + return s; + } } } - function containsIdenticalType(types, type) { - for (var _i = 0, types_5 = types; _i < types_5.length; _i++) { - var t = types_5[_i]; - if (isTypeIdenticalTo(t, type)) { - return true; + function findMatchingSignatures(signatureLists, signature, listIndex) { + if (signature.typeParameters) { + // We require an exact match for generic signatures, so we only return signatures from the first + // signature list and only if they have exact matches in the other signature lists. + if (listIndex > 0) { + return undefined; + } + for (var i = 1; i < signatureLists.length; i++) { + if (!findMatchingSignature(signatureLists[i], signature, /*partialMatch*/ false, /*ignoreThisTypes*/ false, /*ignoreReturnTypes*/ false)) { + return undefined; + } } + return [signature]; } - return false; - } - function isSubtypeOfAny(candidate, types) { - for (var i = 0, len = types.length; i < len; i++) { - if (candidate !== types[i] && isTypeSubtypeOf(candidate, types[i])) { - return true; + var result = undefined; + for (var i = 0; i < signatureLists.length; i++) { + // Allow matching non-generic signatures to have excess parameters and different return types + var match = i === listIndex ? signature : findMatchingSignature(signatureLists[i], signature, /*partialMatch*/ true, /*ignoreThisTypes*/ true, /*ignoreReturnTypes*/ true); + if (!match) { + return undefined; + } + if (!ts.contains(result, match)) { + (result || (result = [])).push(match); } } - return false; + return result; } - function removeSubtypes(types) { - var i = types.length; - while (i > 0) { - i--; - if (isSubtypeOfAny(types[i], types)) { - ts.orderedRemoveItemAt(types, i); + // The signatures of a union type are those signatures that are present in each of the constituent types. + // Generic signatures must match exactly, but non-generic signatures are allowed to have extra optional + // parameters and may differ in return types. When signatures differ in return types, the resulting return + // type is the union of the constituent return types. + function getUnionSignatures(types, kind) { + var signatureLists = ts.map(types, function (t) { return getSignaturesOfType(t, kind); }); + var result = undefined; + for (var i = 0; i < signatureLists.length; i++) { + for (var _i = 0, _a = signatureLists[i]; _i < _a.length; _i++) { + var signature = _a[_i]; + // Only process signatures with parameter lists that aren't already in the result list + if (!result || !findMatchingSignature(result, signature, /*partialMatch*/ false, /*ignoreThisTypes*/ true, /*ignoreReturnTypes*/ true)) { + var unionSignatures = findMatchingSignatures(signatureLists, signature, i); + if (unionSignatures) { + var s = signature; + // Union the result types when more than one signature matches + if (unionSignatures.length > 1) { + s = cloneSignature(signature); + if (ts.forEach(unionSignatures, function (sig) { return sig.thisParameter; })) { + var thisType = getUnionType(ts.map(unionSignatures, function (sig) { return getTypeOfSymbol(sig.thisParameter) || anyType; }), /*subtypeReduction*/ true); + s.thisParameter = createTransientSymbol(signature.thisParameter, thisType); + } + // Clear resolved return type we possibly got from cloneSignature + s.resolvedReturnType = undefined; + s.unionSignatures = unionSignatures; + } + (result || (result = [])).push(s); + } + } } } + return result || emptyArray; } - function removeRedundantLiteralTypes(types) { - var i = types.length; - while (i > 0) { - i--; - var t = types[i]; - var remove = t.flags & 32 /* StringLiteral */ && types.containsString || - t.flags & 64 /* NumberLiteral */ && types.containsNumber || - t.flags & 96 /* StringOrNumberLiteral */ && t.flags & 16777216 /* FreshLiteral */ && containsType(types, t.regularType); - if (remove) { - ts.orderedRemoveItemAt(types, i); + function getUnionIndexInfo(types, kind) { + var indexTypes = []; + var isAnyReadonly = false; + for (var _i = 0, types_1 = types; _i < types_1.length; _i++) { + var type = types_1[_i]; + var indexInfo = getIndexInfoOfType(type, kind); + if (!indexInfo) { + return undefined; } + indexTypes.push(indexInfo.type); + isAnyReadonly = isAnyReadonly || indexInfo.isReadonly; } + return createIndexInfo(getUnionType(indexTypes, /*subtypeReduction*/ true), isAnyReadonly); } - // We sort and deduplicate the constituent types based on object identity. If the subtypeReduction - // flag is specified we also reduce the constituent type set to only include types that aren't subtypes - // of other types. Subtype reduction is expensive for large union types and is possible only when union - // types are known not to circularly reference themselves (as is the case with union types created by - // expression constructs such as array literals and the || and ?: operators). Named types can - // circularly reference themselves and therefore cannot be subtype reduced during their declaration. - // For example, "type Item = string | (() => Item" is a named type that circularly references itself. - function getUnionType(types, subtypeReduction, aliasSymbol, aliasTypeArguments) { - if (types.length === 0) { - return neverType; - } - if (types.length === 1) { - return types[0]; - } - var typeSet = []; - addTypesToUnion(typeSet, types); - if (typeSet.containsAny) { - return anyType; + function resolveUnionTypeMembers(type) { + // The members and properties collections are empty for union types. To get all properties of a union + // type use getPropertiesOfType (only the language service uses this). + var callSignatures = getUnionSignatures(type.types, 0 /* Call */); + var constructSignatures = getUnionSignatures(type.types, 1 /* Construct */); + var stringIndexInfo = getUnionIndexInfo(type.types, 0 /* String */); + var numberIndexInfo = getUnionIndexInfo(type.types, 1 /* Number */); + setObjectTypeMembers(type, emptySymbols, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo); + } + function intersectTypes(type1, type2) { + return !type1 ? type2 : !type2 ? type1 : getIntersectionType([type1, type2]); + } + function intersectIndexInfos(info1, info2) { + return !info1 ? info2 : !info2 ? info1 : createIndexInfo(getIntersectionType([info1.type, info2.type]), info1.isReadonly && info2.isReadonly); + } + function resolveIntersectionTypeMembers(type) { + // The members and properties collections are empty for intersection types. To get all properties of an + // intersection type use getPropertiesOfType (only the language service uses this). + var callSignatures = emptyArray; + var constructSignatures = emptyArray; + var stringIndexInfo = undefined; + var numberIndexInfo = undefined; + for (var _i = 0, _a = type.types; _i < _a.length; _i++) { + var t = _a[_i]; + callSignatures = ts.concatenate(callSignatures, getSignaturesOfType(t, 0 /* Call */)); + constructSignatures = ts.concatenate(constructSignatures, getSignaturesOfType(t, 1 /* Construct */)); + stringIndexInfo = intersectIndexInfos(stringIndexInfo, getIndexInfoOfType(t, 0 /* String */)); + numberIndexInfo = intersectIndexInfos(numberIndexInfo, getIndexInfoOfType(t, 1 /* Number */)); } - if (subtypeReduction) { - removeSubtypes(typeSet); + setObjectTypeMembers(type, emptySymbols, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo); + } + function resolveAnonymousTypeMembers(type) { + var symbol = type.symbol; + if (type.target) { + var members = createInstantiatedSymbolTable(getPropertiesOfObjectType(type.target), type.mapper, /*mappingThisOnly*/ false); + var callSignatures = instantiateList(getSignaturesOfType(type.target, 0 /* Call */), type.mapper, instantiateSignature); + var constructSignatures = instantiateList(getSignaturesOfType(type.target, 1 /* Construct */), type.mapper, instantiateSignature); + var stringIndexInfo = instantiateIndexInfo(getIndexInfoOfType(type.target, 0 /* String */), type.mapper); + var numberIndexInfo = instantiateIndexInfo(getIndexInfoOfType(type.target, 1 /* Number */), type.mapper); + setObjectTypeMembers(type, members, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo); } - else if (typeSet.containsStringOrNumberLiteral) { - removeRedundantLiteralTypes(typeSet); + else if (symbol.flags & 2048 /* TypeLiteral */) { + var members = symbol.members; + var callSignatures = getSignaturesOfSymbol(members["__call"]); + var constructSignatures = getSignaturesOfSymbol(members["__new"]); + var stringIndexInfo = getIndexInfoOfSymbol(symbol, 0 /* String */); + var numberIndexInfo = getIndexInfoOfSymbol(symbol, 1 /* Number */); + setObjectTypeMembers(type, members, callSignatures, constructSignatures, stringIndexInfo, numberIndexInfo); } - if (typeSet.length === 0) { - return typeSet.containsNull ? typeSet.containsNonWideningType ? nullType : nullWideningType : - typeSet.containsUndefined ? typeSet.containsNonWideningType ? undefinedType : undefinedWideningType : - neverType; + else { + // Combinations of function, class, enum and module + var members = emptySymbols; + var constructSignatures = emptyArray; + if (symbol.flags & 1952 /* HasExports */) { + members = getExportsOfSymbol(symbol); + } + if (symbol.flags & 32 /* Class */) { + var classType = getDeclaredTypeOfClassOrInterface(symbol); + constructSignatures = getSignaturesOfSymbol(symbol.members["__constructor"]); + if (!constructSignatures.length) { + constructSignatures = getDefaultConstructSignatures(classType); + } + var baseConstructorType = getBaseConstructorTypeOfClass(classType); + if (baseConstructorType.flags & 2588672 /* ObjectType */) { + members = createSymbolTable(getNamedMembers(members)); + addInheritedMembers(members, getPropertiesOfObjectType(baseConstructorType)); + } + } + var numberIndexInfo = symbol.flags & 384 /* Enum */ ? enumNumberIndexInfo : undefined; + setObjectTypeMembers(type, members, emptyArray, constructSignatures, undefined, numberIndexInfo); + // We resolve the members before computing the signatures because a signature may use + // typeof with a qualified name expression that circularly references the type we are + // in the process of resolving (see issue #6072). The temporarily empty signature list + // will never be observed because a qualified name can't reference signatures. + if (symbol.flags & (16 /* Function */ | 8192 /* Method */)) { + type.callSignatures = getSignaturesOfSymbol(symbol); + } } - return getUnionTypeFromSortedList(typeSet, aliasSymbol, aliasTypeArguments); } - // This function assumes the constituent type list is sorted and deduplicated. - function getUnionTypeFromSortedList(types, aliasSymbol, aliasTypeArguments) { - if (types.length === 0) { - return neverType; - } - if (types.length === 1) { - return types[0]; - } - var id = getTypeListId(types); - var type = unionTypes[id]; - if (!type) { - var propagatedFlags = getPropagatingFlagsOfTypes(types, /*excludeKinds*/ 6144 /* Nullable */); - type = unionTypes[id] = createObjectType(524288 /* Union */ | propagatedFlags); - type.types = types; - type.aliasSymbol = aliasSymbol; - type.aliasTypeArguments = aliasTypeArguments; + function resolveStructuredTypeMembers(type) { + if (!type.members) { + if (type.flags & 131072 /* Reference */) { + resolveTypeReferenceMembers(type); + } + else if (type.flags & (32768 /* Class */ | 65536 /* Interface */)) { + resolveClassOrInterfaceMembers(type); + } + else if (type.flags & 2097152 /* Anonymous */) { + resolveAnonymousTypeMembers(type); + } + else if (type.flags & 524288 /* Union */) { + resolveUnionTypeMembers(type); + } + else if (type.flags & 1048576 /* Intersection */) { + resolveIntersectionTypeMembers(type); + } } return type; } - function getTypeFromUnionTypeNode(node, aliasSymbol, aliasTypeArguments) { - var links = getNodeLinks(node); - if (!links.resolvedType) { - links.resolvedType = getUnionType(ts.map(node.types, getTypeFromTypeNodeNoAlias), /*subtypeReduction*/ false, aliasSymbol, aliasTypeArguments); + /** Return properties of an object type or an empty array for other types */ + function getPropertiesOfObjectType(type) { + if (type.flags & 2588672 /* ObjectType */) { + return resolveStructuredTypeMembers(type).properties; } - return links.resolvedType; + return emptyArray; } - function addTypeToIntersection(typeSet, type) { - if (type.flags & 1048576 /* Intersection */) { - addTypesToIntersection(typeSet, type.types); + /** If the given type is an object type and that type has a property by the given name, + * return the symbol for that property. Otherwise return undefined. */ + function getPropertyOfObjectType(type, name) { + if (type.flags & 2588672 /* ObjectType */) { + var resolved = resolveStructuredTypeMembers(type); + var symbol = resolved.members[name]; + if (symbol && symbolIsValue(symbol)) { + return symbol; + } } - else if (type.flags & 1 /* Any */) { - typeSet.containsAny = true; + } + function getPropertiesOfUnionOrIntersectionType(type) { + for (var _i = 0, _a = type.types; _i < _a.length; _i++) { + var current = _a[_i]; + for (var _b = 0, _c = getPropertiesOfType(current); _b < _c.length; _b++) { + var prop = _c[_b]; + getUnionOrIntersectionProperty(type, prop.name); + } + // The properties of a union type are those that are present in all constituent types, so + // we only need to check the properties of the first type + if (type.flags & 524288 /* Union */) { + break; + } } - else if (!(type.flags & 8192 /* Never */) && (strictNullChecks || !(type.flags & 6144 /* Nullable */)) && !ts.contains(typeSet, type)) { - typeSet.push(type); + var props = type.resolvedProperties; + if (props) { + var result = []; + for (var key in props) { + var prop = props[key]; + // We need to filter out partial properties in union types + if (!(prop.flags & 268435456 /* SyntheticProperty */ && prop.isPartial)) { + result.push(prop); + } + } + return result; } + return emptyArray; } - // Add the given types to the given type set. Order is preserved, duplicates are removed, - // and nested types of the given kind are flattened into the set. - function addTypesToIntersection(typeSet, types) { - for (var _i = 0, types_6 = types; _i < types_6.length; _i++) { - var type = types_6[_i]; - addTypeToIntersection(typeSet, type); + function getPropertiesOfType(type) { + type = getApparentType(type); + return type.flags & 1572864 /* UnionOrIntersection */ ? getPropertiesOfUnionOrIntersectionType(type) : getPropertiesOfObjectType(type); + } + /** + * The apparent type of a type parameter is the base constraint instantiated with the type parameter + * as the type argument for the 'this' type. + */ + function getApparentTypeOfTypeParameter(type) { + if (!type.resolvedApparentType) { + var constraintType = getConstraintOfTypeParameter(type); + while (constraintType && constraintType.flags & 16384 /* TypeParameter */) { + constraintType = getConstraintOfTypeParameter(constraintType); + } + type.resolvedApparentType = getTypeWithThisArgument(constraintType || emptyObjectType, type); } + return type.resolvedApparentType; } - // We do not perform structural deduplication on intersection types. Intersection types are created only by the & - // type operator and we can't reduce those because we want to support recursive intersection types. For example, - // a type alias of the form "type List = T & { next: List }" cannot be reduced during its declaration. - // Also, unlike union types, the order of the constituent types is preserved in order that overload resolution - // for intersections of types with signatures can be deterministic. - function getIntersectionType(types, aliasSymbol, aliasTypeArguments) { - if (types.length === 0) { - return emptyObjectType; + /** + * For a type parameter, return the base constraint of the type parameter. For the string, number, + * boolean, and symbol primitive types, return the corresponding object types. Otherwise return the + * type itself. Note that the apparent type of a union type is the union type itself. + */ + function getApparentType(type) { + if (type.flags & 16384 /* TypeParameter */) { + type = getApparentTypeOfTypeParameter(type); } - var typeSet = []; - addTypesToIntersection(typeSet, types); - if (typeSet.containsAny) { - return anyType; + if (type.flags & 34 /* StringLike */) { + type = globalStringType; } - if (typeSet.length === 1) { - return typeSet[0]; + else if (type.flags & 340 /* NumberLike */) { + type = globalNumberType; } - var id = getTypeListId(typeSet); - var type = intersectionTypes[id]; - if (!type) { - var propagatedFlags = getPropagatingFlagsOfTypes(typeSet, /*excludeKinds*/ 6144 /* Nullable */); - type = intersectionTypes[id] = createObjectType(1048576 /* Intersection */ | propagatedFlags); - type.types = typeSet; - type.aliasSymbol = aliasSymbol; - type.aliasTypeArguments = aliasTypeArguments; + else if (type.flags & 136 /* BooleanLike */) { + type = globalBooleanType; + } + else if (type.flags & 512 /* ESSymbol */) { + type = getGlobalESSymbolType(); } return type; } - function getTypeFromIntersectionTypeNode(node, aliasSymbol, aliasTypeArguments) { - var links = getNodeLinks(node); - if (!links.resolvedType) { - links.resolvedType = getIntersectionType(ts.map(node.types, getTypeFromTypeNodeNoAlias), aliasSymbol, aliasTypeArguments); + function createUnionOrIntersectionProperty(containingType, name) { + var types = containingType.types; + var props; + // Flags we want to propagate to the result if they exist in all source symbols + var commonFlags = (containingType.flags & 1048576 /* Intersection */) ? 536870912 /* Optional */ : 0 /* None */; + var isReadonly = false; + var isPartial = false; + for (var _i = 0, types_2 = types; _i < types_2.length; _i++) { + var current = types_2[_i]; + var type = getApparentType(current); + if (type !== unknownType) { + var prop = getPropertyOfType(type, name); + if (prop && !(getDeclarationModifierFlagsFromSymbol(prop) & (8 /* Private */ | 16 /* Protected */))) { + commonFlags &= prop.flags; + if (!props) { + props = [prop]; + } + else if (!ts.contains(props, prop)) { + props.push(prop); + } + if (isReadonlySymbol(prop)) { + isReadonly = true; + } + } + else if (containingType.flags & 524288 /* Union */) { + isPartial = true; + } + } } - return links.resolvedType; - } - function getTypeFromTypeLiteralOrFunctionOrConstructorTypeNode(node, aliasSymbol, aliasTypeArguments) { - var links = getNodeLinks(node); - if (!links.resolvedType) { - // Deferred resolution of members is handled by resolveObjectTypeMembers - var type = createObjectType(2097152 /* Anonymous */, node.symbol); - type.aliasSymbol = aliasSymbol; - type.aliasTypeArguments = aliasTypeArguments; - links.resolvedType = type; + if (!props) { + return undefined; } - return links.resolvedType; - } - function createLiteralType(flags, text) { - var type = createType(flags); - type.text = text; - return type; - } - function getFreshTypeOfLiteralType(type) { - if (type.flags & 96 /* StringOrNumberLiteral */ && !(type.flags & 16777216 /* FreshLiteral */)) { - if (!type.freshType) { - var freshType = createLiteralType(type.flags | 16777216 /* FreshLiteral */, type.text); - freshType.regularType = type; - type.freshType = freshType; + if (props.length === 1 && !isPartial) { + return props[0]; + } + var propTypes = []; + var declarations = []; + var commonType = undefined; + var hasNonUniformType = false; + for (var _a = 0, props_1 = props; _a < props_1.length; _a++) { + var prop = props_1[_a]; + if (prop.declarations) { + ts.addRange(declarations, prop.declarations); } - return type.freshType; + var type = getTypeOfSymbol(prop); + if (!commonType) { + commonType = type; + } + else if (type !== commonType) { + hasNonUniformType = true; + } + propTypes.push(type); } - return type; - } - function getRegularTypeOfLiteralType(type) { - return type.flags & 96 /* StringOrNumberLiteral */ && type.flags & 16777216 /* FreshLiteral */ ? type.regularType : type; - } - function getLiteralTypeForText(flags, text) { - var map = flags & 32 /* StringLiteral */ ? stringLiteralTypes : numericLiteralTypes; - return map[text] || (map[text] = createLiteralType(flags, text)); + var result = createSymbol(4 /* Property */ | 67108864 /* Transient */ | 268435456 /* SyntheticProperty */ | commonFlags, name); + result.containingType = containingType; + result.hasNonUniformType = hasNonUniformType; + result.isPartial = isPartial; + result.declarations = declarations; + result.isReadonly = isReadonly; + result.type = containingType.flags & 524288 /* Union */ ? getUnionType(propTypes) : getIntersectionType(propTypes); + return result; } - function getTypeFromLiteralTypeNode(node) { - var links = getNodeLinks(node); - if (!links.resolvedType) { - links.resolvedType = getRegularTypeOfLiteralType(checkExpression(node.literal)); + // Return the symbol for a given property in a union or intersection type, or undefined if the property + // does not exist in any constituent type. Note that the returned property may only be present in some + // constituents, in which case the isPartial flag is set when the containing type is union type. We need + // these partial properties when identifying discriminant properties, but otherwise they are filtered out + // and do not appear to be present in the union type. + function getUnionOrIntersectionProperty(type, name) { + var properties = type.resolvedProperties || (type.resolvedProperties = ts.createMap()); + var property = properties[name]; + if (!property) { + property = createUnionOrIntersectionProperty(type, name); + if (property) { + properties[name] = property; + } } - return links.resolvedType; + return property; } - function getTypeFromJSDocVariadicType(node) { - var links = getNodeLinks(node); - if (!links.resolvedType) { - var type = getTypeFromTypeNode(node.type); - links.resolvedType = type ? createArrayType(type) : unknownType; - } - return links.resolvedType; - } - function getTypeFromJSDocTupleType(node) { - var links = getNodeLinks(node); - if (!links.resolvedType) { - var types = ts.map(node.types, getTypeFromTypeNodeNoAlias); - links.resolvedType = createTupleType(types); - } - return links.resolvedType; + function getPropertyOfUnionOrIntersectionType(type, name) { + var property = getUnionOrIntersectionProperty(type, name); + // We need to filter out partial properties in union types + return property && !(property.flags & 268435456 /* SyntheticProperty */ && property.isPartial) ? property : undefined; } - function getThisType(node) { - var container = ts.getThisContainer(node, /*includeArrowFunctions*/ false); - var parent = container && container.parent; - if (parent && (ts.isClassLike(parent) || parent.kind === 222 /* InterfaceDeclaration */)) { - if (!(ts.getModifierFlags(container) & 32 /* Static */) && - (container.kind !== 148 /* Constructor */ || ts.isNodeDescendantOf(node, container.body))) { - return getDeclaredTypeOfClassOrInterface(getSymbolOfNode(parent)).thisType; + /** + * Return the symbol for the property with the given name in the given type. Creates synthetic union properties when + * necessary, maps primitive types and type parameters are to their apparent types, and augments with properties from + * Object and Function as appropriate. + * + * @param type a type to look up property from + * @param name a name of property to look up in a given type + */ + function getPropertyOfType(type, name) { + type = getApparentType(type); + if (type.flags & 2588672 /* ObjectType */) { + var resolved = resolveStructuredTypeMembers(type); + var symbol = resolved.members[name]; + if (symbol && symbolIsValue(symbol)) { + return symbol; + } + if (resolved === anyFunctionType || resolved.callSignatures.length || resolved.constructSignatures.length) { + var symbol_1 = getPropertyOfObjectType(globalFunctionType, name); + if (symbol_1) { + return symbol_1; + } } + return getPropertyOfObjectType(globalObjectType, name); } - error(node, ts.Diagnostics.A_this_type_is_available_only_in_a_non_static_member_of_a_class_or_interface); - return unknownType; + if (type.flags & 1572864 /* UnionOrIntersection */) { + return getPropertyOfUnionOrIntersectionType(type, name); + } + return undefined; } - function getTypeFromThisTypeNode(node) { - var links = getNodeLinks(node); - if (!links.resolvedType) { - links.resolvedType = getThisType(node); + function getSignaturesOfStructuredType(type, kind) { + if (type.flags & 4161536 /* StructuredType */) { + var resolved = resolveStructuredTypeMembers(type); + return kind === 0 /* Call */ ? resolved.callSignatures : resolved.constructSignatures; } - return links.resolvedType; + return emptyArray; } - function getTypeFromTypeNodeNoAlias(type) { - return getTypeFromTypeNode(type, /*aliasSymbol*/ undefined, /*aliasTypeArguments*/ undefined); + /** + * Return the signatures of the given kind in the given type. Creates synthetic union signatures when necessary and + * maps primitive types and type parameters are to their apparent types. + */ + function getSignaturesOfType(type, kind) { + return getSignaturesOfStructuredType(getApparentType(type), kind); } - function getTypeFromTypeNode(node, aliasSymbol, aliasTypeArguments) { - switch (node.kind) { - case 117 /* AnyKeyword */: - case 258 /* JSDocAllType */: - case 259 /* JSDocUnknownType */: - return anyType; - case 132 /* StringKeyword */: - return stringType; - case 130 /* NumberKeyword */: - return numberType; - case 120 /* BooleanKeyword */: - return booleanType; - case 133 /* SymbolKeyword */: - return esSymbolType; - case 103 /* VoidKeyword */: - return voidType; - case 135 /* UndefinedKeyword */: - return undefinedType; - case 93 /* NullKeyword */: - return nullType; - case 127 /* NeverKeyword */: - return neverType; - case 283 /* JSDocNullKeyword */: - return nullType; - case 284 /* JSDocUndefinedKeyword */: - return undefinedType; - case 285 /* JSDocNeverKeyword */: - return neverType; - case 165 /* ThisType */: - case 97 /* ThisKeyword */: - return getTypeFromThisTypeNode(node); - case 166 /* LiteralType */: - return getTypeFromLiteralTypeNode(node); - case 282 /* JSDocLiteralType */: - return getTypeFromLiteralTypeNode(node.literal); - case 155 /* TypeReference */: - case 267 /* JSDocTypeReference */: - return getTypeFromTypeReference(node); - case 154 /* TypePredicate */: - return booleanType; - case 194 /* ExpressionWithTypeArguments */: - return getTypeFromTypeReference(node); - case 158 /* TypeQuery */: - return getTypeFromTypeQueryNode(node); - case 160 /* ArrayType */: - case 260 /* JSDocArrayType */: - return getTypeFromArrayTypeNode(node); - case 161 /* TupleType */: - return getTypeFromTupleTypeNode(node); - case 162 /* UnionType */: - case 261 /* JSDocUnionType */: - return getTypeFromUnionTypeNode(node, aliasSymbol, aliasTypeArguments); - case 163 /* IntersectionType */: - return getTypeFromIntersectionTypeNode(node, aliasSymbol, aliasTypeArguments); - case 164 /* ParenthesizedType */: - case 263 /* JSDocNullableType */: - case 264 /* JSDocNonNullableType */: - case 271 /* JSDocConstructorType */: - case 272 /* JSDocThisType */: - case 268 /* JSDocOptionalType */: - return getTypeFromTypeNode(node.type); - case 265 /* JSDocRecordType */: - return getTypeFromTypeNode(node.literal); - case 156 /* FunctionType */: - case 157 /* ConstructorType */: - case 159 /* TypeLiteral */: - case 281 /* JSDocTypeLiteral */: - case 269 /* JSDocFunctionType */: - return getTypeFromTypeLiteralOrFunctionOrConstructorTypeNode(node, aliasSymbol, aliasTypeArguments); - // This function assumes that an identifier or qualified name is a type expression - // Callers should first ensure this by calling isTypeNode - case 69 /* Identifier */: - case 139 /* QualifiedName */: - var symbol = getSymbolAtLocation(node); - return symbol && getDeclaredTypeOfSymbol(symbol); - case 262 /* JSDocTupleType */: - return getTypeFromJSDocTupleType(node); - case 270 /* JSDocVariadicType */: - return getTypeFromJSDocVariadicType(node); - default: - return unknownType; + function getIndexInfoOfStructuredType(type, kind) { + if (type.flags & 4161536 /* StructuredType */) { + var resolved = resolveStructuredTypeMembers(type); + return kind === 0 /* String */ ? resolved.stringIndexInfo : resolved.numberIndexInfo; } } - function instantiateList(items, mapper, instantiator) { - if (items && items.length) { - var result = []; - for (var _i = 0, items_1 = items; _i < items_1.length; _i++) { - var v = items_1[_i]; - result.push(instantiator(v, mapper)); - } - return result; - } - return items; + function getIndexTypeOfStructuredType(type, kind) { + var info = getIndexInfoOfStructuredType(type, kind); + return info && info.type; } - function createUnaryTypeMapper(source, target) { - return function (t) { return t === source ? target : t; }; + // Return the indexing info of the given kind in the given type. Creates synthetic union index types when necessary and + // maps primitive types and type parameters are to their apparent types. + function getIndexInfoOfType(type, kind) { + return getIndexInfoOfStructuredType(getApparentType(type), kind); } - function createBinaryTypeMapper(source1, target1, source2, target2) { - return function (t) { return t === source1 ? target1 : t === source2 ? target2 : t; }; + // Return the index type of the given kind in the given type. Creates synthetic union index types when necessary and + // maps primitive types and type parameters are to their apparent types. + function getIndexTypeOfType(type, kind) { + return getIndexTypeOfStructuredType(getApparentType(type), kind); } - function createArrayTypeMapper(sources, targets) { - return function (t) { - for (var i = 0; i < sources.length; i++) { - if (t === sources[i]) { - return targets ? targets[i] : anyType; + function getImplicitIndexTypeOfType(type, kind) { + if (isObjectLiteralType(type)) { + var propTypes = []; + for (var _i = 0, _a = getPropertiesOfType(type); _i < _a.length; _i++) { + var prop = _a[_i]; + if (kind === 0 /* String */ || isNumericLiteralName(prop.name)) { + propTypes.push(getTypeOfSymbol(prop)); } } - return t; - }; + if (propTypes.length) { + return getUnionType(propTypes, /*subtypeReduction*/ true); + } + } + return undefined; } - function createTypeMapper(sources, targets) { - var count = sources.length; - var mapper = count == 1 ? createUnaryTypeMapper(sources[0], targets ? targets[0] : anyType) : - count == 2 ? createBinaryTypeMapper(sources[0], targets ? targets[0] : anyType, sources[1], targets ? targets[1] : anyType) : - createArrayTypeMapper(sources, targets); - mapper.mappedTypes = sources; - mapper.targetTypes = targets; - return mapper; + function getTypeParametersFromJSDocTemplate(declaration) { + if (declaration.flags & 1048576 /* JavaScriptFile */) { + var templateTag = ts.getJSDocTemplateTag(declaration); + if (templateTag) { + return getTypeParametersFromDeclaration(templateTag.typeParameters); + } + } + return undefined; } - function createTypeEraser(sources) { - return createTypeMapper(sources, undefined); + // Return list of type parameters with duplicates removed (duplicate identifier errors are generated in the actual + // type checking functions). + function getTypeParametersFromDeclaration(typeParameterDeclarations) { + var result = []; + ts.forEach(typeParameterDeclarations, function (node) { + var tp = getDeclaredTypeOfTypeParameter(node.symbol); + if (!ts.contains(result, tp)) { + result.push(tp); + } + }); + return result; } - function getInferenceMapper(context) { - if (!context.mapper) { - var mapper = function (t) { - var typeParameters = context.signature.typeParameters; - for (var i = 0; i < typeParameters.length; i++) { - if (t === typeParameters[i]) { - context.inferences[i].isFixed = true; - return getInferredType(context, i); - } - } - return t; - }; - mapper.mappedTypes = context.signature.typeParameters; - mapper.context = context; - context.mapper = mapper; + function symbolsToArray(symbols) { + var result = []; + for (var id in symbols) { + if (!isReservedMemberName(id)) { + result.push(symbols[id]); + } } - return context.mapper; - } - function identityMapper(type) { - return type; + return result; } - function combineTypeMappers(mapper1, mapper2) { - var mapper = function (t) { return instantiateType(mapper1(t), mapper2); }; - mapper.mappedTypes = mapper1.mappedTypes; - return mapper; + function isJSDocOptionalParameter(node) { + if (node.flags & 1048576 /* JavaScriptFile */) { + if (node.type && node.type.kind === 268 /* JSDocOptionalType */) { + return true; + } + var paramTag = ts.getCorrespondingJSDocParameterTag(node); + if (paramTag) { + if (paramTag.isBracketed) { + return true; + } + if (paramTag.typeExpression) { + return paramTag.typeExpression.type.kind === 268 /* JSDocOptionalType */; + } + } + } } - function cloneTypeParameter(typeParameter) { - var result = createType(16384 /* TypeParameter */); - result.symbol = typeParameter.symbol; - result.target = typeParameter; - return result; + function isOptionalParameter(node) { + if (ts.hasQuestionToken(node) || isJSDocOptionalParameter(node)) { + return true; + } + if (node.initializer) { + var signatureDeclaration = node.parent; + var signature = getSignatureFromDeclaration(signatureDeclaration); + var parameterIndex = ts.indexOf(signatureDeclaration.parameters, node); + ts.Debug.assert(parameterIndex >= 0); + return parameterIndex >= signature.minArgumentCount; + } + return false; } - function cloneTypePredicate(predicate, mapper) { - if (ts.isIdentifierTypePredicate(predicate)) { + function createTypePredicateFromTypePredicateNode(node) { + if (node.parameterName.kind === 69 /* Identifier */) { + var parameterName = node.parameterName; return { kind: 1 /* Identifier */, - parameterName: predicate.parameterName, - parameterIndex: predicate.parameterIndex, - type: instantiateType(predicate.type, mapper) + parameterName: parameterName ? parameterName.text : undefined, + parameterIndex: parameterName ? getTypePredicateParameterIndex(node.parent.parameters, parameterName) : undefined, + type: getTypeFromTypeNode(node.type) }; } else { return { kind: 0 /* This */, - type: instantiateType(predicate.type, mapper) + type: getTypeFromTypeNode(node.type) }; } } - function instantiateSignature(signature, mapper, eraseTypeParameters) { - var freshTypeParameters; - var freshTypePredicate; - if (signature.typeParameters && !eraseTypeParameters) { - // First create a fresh set of type parameters, then include a mapping from the old to the - // new type parameters in the mapper function. Finally store this mapper in the new type - // parameters such that we can use it when instantiating constraints. - freshTypeParameters = ts.map(signature.typeParameters, cloneTypeParameter); - mapper = combineTypeMappers(createTypeMapper(signature.typeParameters, freshTypeParameters), mapper); - for (var _i = 0, freshTypeParameters_1 = freshTypeParameters; _i < freshTypeParameters_1.length; _i++) { - var tp = freshTypeParameters_1[_i]; - tp.mapper = mapper; + function getSignatureFromDeclaration(declaration) { + var links = getNodeLinks(declaration); + if (!links.resolvedSignature) { + var parameters = []; + var hasLiteralTypes = false; + var minArgumentCount = -1; + var thisParameter = undefined; + var hasThisParameter = void 0; + var isJSConstructSignature = ts.isJSDocConstructSignature(declaration); + // If this is a JSDoc construct signature, then skip the first parameter in the + // parameter list. The first parameter represents the return type of the construct + // signature. + for (var i = isJSConstructSignature ? 1 : 0, n = declaration.parameters.length; i < n; i++) { + var param = declaration.parameters[i]; + var paramSymbol = param.symbol; + // Include parameter symbol instead of property symbol in the signature + if (paramSymbol && !!(paramSymbol.flags & 4 /* Property */) && !ts.isBindingPattern(param.name)) { + var resolvedSymbol = resolveName(param, paramSymbol.name, 107455 /* Value */, undefined, undefined); + paramSymbol = resolvedSymbol; + } + if (i === 0 && paramSymbol.name === "this") { + hasThisParameter = true; + thisParameter = param.symbol; + } + else { + parameters.push(paramSymbol); + } + if (param.type && param.type.kind === 166 /* LiteralType */) { + hasLiteralTypes = true; + } + if (param.initializer || param.questionToken || param.dotDotDotToken || isJSDocOptionalParameter(param)) { + if (minArgumentCount < 0) { + minArgumentCount = i - (hasThisParameter ? 1 : 0); + } + } + else { + // If we see any required parameters, it means the prior ones were not in fact optional. + minArgumentCount = -1; + } } + // If only one accessor includes a this-type annotation, the other behaves as if it had the same type annotation + if ((declaration.kind === 149 /* GetAccessor */ || declaration.kind === 150 /* SetAccessor */) && + !ts.hasDynamicName(declaration) && + (!hasThisParameter || !thisParameter)) { + var otherKind = declaration.kind === 149 /* GetAccessor */ ? 150 /* SetAccessor */ : 149 /* GetAccessor */; + var other = ts.getDeclarationOfKind(declaration.symbol, otherKind); + if (other) { + thisParameter = getAnnotatedAccessorThisParameter(other); + } + } + if (minArgumentCount < 0) { + minArgumentCount = declaration.parameters.length - (hasThisParameter ? 1 : 0); + } + if (isJSConstructSignature) { + minArgumentCount--; + } + if (!thisParameter && ts.isObjectLiteralMethod(declaration)) { + thisParameter = getContextualThisParameter(declaration); + } + var classType = declaration.kind === 148 /* Constructor */ ? + getDeclaredTypeOfClassOrInterface(getMergedSymbol(declaration.parent.symbol)) + : undefined; + var typeParameters = classType ? classType.localTypeParameters : + declaration.typeParameters ? getTypeParametersFromDeclaration(declaration.typeParameters) : + getTypeParametersFromJSDocTemplate(declaration); + var returnType = getSignatureReturnTypeFromDeclaration(declaration, minArgumentCount, isJSConstructSignature, classType); + var typePredicate = declaration.type && declaration.type.kind === 154 /* TypePredicate */ ? + createTypePredicateFromTypePredicateNode(declaration.type) : + undefined; + links.resolvedSignature = createSignature(declaration, typeParameters, thisParameter, parameters, returnType, typePredicate, minArgumentCount, ts.hasRestParameter(declaration), hasLiteralTypes); } - if (signature.typePredicate) { - freshTypePredicate = cloneTypePredicate(signature.typePredicate, mapper); - } - var result = createSignature(signature.declaration, freshTypeParameters, signature.thisParameter && instantiateSymbol(signature.thisParameter, mapper), instantiateList(signature.parameters, mapper, instantiateSymbol), instantiateType(signature.resolvedReturnType, mapper), freshTypePredicate, signature.minArgumentCount, signature.hasRestParameter, signature.hasLiteralTypes); - result.target = signature; - result.mapper = mapper; - return result; + return links.resolvedSignature; } - function instantiateSymbol(symbol, mapper) { - if (symbol.flags & 16777216 /* Instantiated */) { - var links = getSymbolLinks(symbol); - // If symbol being instantiated is itself a instantiation, fetch the original target and combine the - // type mappers. This ensures that original type identities are properly preserved and that aliases - // always reference a non-aliases. - symbol = links.target; - mapper = combineTypeMappers(links.mapper, mapper); + function getSignatureReturnTypeFromDeclaration(declaration, minArgumentCount, isJSConstructSignature, classType) { + if (isJSConstructSignature) { + return getTypeFromTypeNode(declaration.parameters[0].type); } - // Keep the flags from the symbol we're instantiating. Mark that is instantiated, and - // also transient so that we can just store data on it directly. - var result = createSymbol(16777216 /* Instantiated */ | 67108864 /* Transient */ | symbol.flags, symbol.name); - result.declarations = symbol.declarations; - result.parent = symbol.parent; - result.target = symbol; - result.mapper = mapper; - if (symbol.valueDeclaration) { - result.valueDeclaration = symbol.valueDeclaration; + else if (classType) { + return classType; } - return result; - } - function instantiateAnonymousType(type, mapper) { - if (mapper.instantiations) { - var cachedType = mapper.instantiations[type.id]; - if (cachedType) { - return cachedType; + else if (declaration.type) { + return getTypeFromTypeNode(declaration.type); + } + if (declaration.flags & 1048576 /* JavaScriptFile */) { + var type = getReturnTypeFromJSDocComment(declaration); + if (type && type !== unknownType) { + return type; } } - else { - mapper.instantiations = []; + // TypeScript 1.0 spec (April 2014): + // If only one accessor includes a type annotation, the other behaves as if it had the same type annotation. + if (declaration.kind === 149 /* GetAccessor */ && !ts.hasDynamicName(declaration)) { + var setter = ts.getDeclarationOfKind(declaration.symbol, 150 /* SetAccessor */); + return getAnnotatedAccessorType(setter); + } + if (ts.nodeIsMissing(declaration.body)) { + return anyType; } - // Mark the anonymous type as instantiated such that our infinite instantiation detection logic can recognize it - var result = createObjectType(2097152 /* Anonymous */ | 4194304 /* Instantiated */, type.symbol); - result.target = type; - result.mapper = mapper; - result.aliasSymbol = type.aliasSymbol; - result.aliasTypeArguments = mapper.targetTypes; - mapper.instantiations[type.id] = result; - return result; } - function isSymbolInScopeOfMappedTypeParameter(symbol, mapper) { - var mappedTypes = mapper.mappedTypes; - // Starting with the parent of the symbol's declaration, check if the mapper maps any of - // the type parameters introduced by enclosing declarations. We just pick the first - // declaration since multiple declarations will all have the same parent anyway. - var node = symbol.declarations[0].parent; - while (node) { + function getSignaturesOfSymbol(symbol) { + if (!symbol) + return emptyArray; + var result = []; + for (var i = 0, len = symbol.declarations.length; i < len; i++) { + var node = symbol.declarations[i]; switch (node.kind) { case 156 /* FunctionType */: case 157 /* ConstructorType */: @@ -26932,13734 +27068,14818 @@ var ts; case 150 /* SetAccessor */: case 179 /* FunctionExpression */: case 180 /* ArrowFunction */: - case 221 /* ClassDeclaration */: - case 192 /* ClassExpression */: - case 222 /* InterfaceDeclaration */: - case 223 /* TypeAliasDeclaration */: - var declaration = node; - if (declaration.typeParameters) { - for (var _i = 0, _a = declaration.typeParameters; _i < _a.length; _i++) { - var d = _a[_i]; - if (ts.contains(mappedTypes, getDeclaredTypeOfTypeParameter(getSymbolOfNode(d)))) { - return true; - } - } - } - if (ts.isClassLike(node) || node.kind === 222 /* InterfaceDeclaration */) { - var thisType = getDeclaredTypeOfClassOrInterface(getSymbolOfNode(node)).thisType; - if (thisType && ts.contains(mappedTypes, thisType)) { - return true; + case 269 /* JSDocFunctionType */: + // Don't include signature if node is the implementation of an overloaded function. A node is considered + // an implementation node if it has a body and the previous node is of the same kind and immediately + // precedes the implementation node (i.e. has the same parent and ends where the implementation starts). + if (i > 0 && node.body) { + var previous = symbol.declarations[i - 1]; + if (node.parent === previous.parent && node.kind === previous.kind && node.pos === previous.end) { + break; } } - break; - case 225 /* ModuleDeclaration */: - case 256 /* SourceFile */: - return false; + result.push(getSignatureFromDeclaration(node)); } - node = node.parent; } - return false; + return result; } - function instantiateType(type, mapper) { - if (type && mapper !== identityMapper) { - if (type.flags & 16384 /* TypeParameter */) { - return mapper(type); - } - if (type.flags & 2097152 /* Anonymous */) { - // If the anonymous type originates in a declaration of a function, method, class, or - // interface, in an object type literal, or in an object literal expression, we may need - // to instantiate the type because it might reference a type parameter. We skip instantiation - // if none of the type parameters that are in scope in the type's declaration are mapped by - // the given mapper, however we can only do that analysis if the type isn't itself an - // instantiation. - return type.symbol && - type.symbol.flags & (16 /* Function */ | 8192 /* Method */ | 32 /* Class */ | 2048 /* TypeLiteral */ | 4096 /* ObjectLiteral */) && - (type.flags & 4194304 /* Instantiated */ || isSymbolInScopeOfMappedTypeParameter(type.symbol, mapper)) ? - instantiateAnonymousType(type, mapper) : type; - } - if (type.flags & 131072 /* Reference */) { - return createTypeReference(type.target, instantiateList(type.typeArguments, mapper, instantiateType)); - } - if (type.flags & 524288 /* Union */ && !(type.flags & 8190 /* Primitive */)) { - return getUnionType(instantiateList(type.types, mapper, instantiateType), /*subtypeReduction*/ false, type.aliasSymbol, mapper.targetTypes); - } - if (type.flags & 1048576 /* Intersection */) { - return getIntersectionType(instantiateList(type.types, mapper, instantiateType), type.aliasSymbol, mapper.targetTypes); + function resolveExternalModuleTypeByLiteral(name) { + var moduleSym = resolveExternalModuleName(name, name); + if (moduleSym) { + var resolvedModuleSymbol = resolveExternalModuleSymbol(moduleSym); + if (resolvedModuleSymbol) { + return getTypeOfSymbol(resolvedModuleSymbol); } } - return type; - } - function instantiateIndexInfo(info, mapper) { - return info && createIndexInfo(instantiateType(info.type, mapper), info.isReadonly, info.declaration); + return anyType; } - // Returns true if the given expression contains (at any level of nesting) a function or arrow expression - // that is subject to contextual typing. - function isContextSensitive(node) { - ts.Debug.assert(node.kind !== 147 /* MethodDeclaration */ || ts.isObjectLiteralMethod(node)); - switch (node.kind) { - case 179 /* FunctionExpression */: - case 180 /* ArrowFunction */: - return isContextSensitiveFunctionLikeDeclaration(node); - case 171 /* ObjectLiteralExpression */: - return ts.forEach(node.properties, isContextSensitive); - case 170 /* ArrayLiteralExpression */: - return ts.forEach(node.elements, isContextSensitive); - case 188 /* ConditionalExpression */: - return isContextSensitive(node.whenTrue) || - isContextSensitive(node.whenFalse); - case 187 /* BinaryExpression */: - return node.operatorToken.kind === 52 /* BarBarToken */ && - (isContextSensitive(node.left) || isContextSensitive(node.right)); - case 253 /* PropertyAssignment */: - return isContextSensitive(node.initializer); - case 147 /* MethodDeclaration */: - case 146 /* MethodSignature */: - return isContextSensitiveFunctionLikeDeclaration(node); - case 178 /* ParenthesizedExpression */: - return isContextSensitive(node.expression); + function getThisTypeOfSignature(signature) { + if (signature.thisParameter) { + return getTypeOfSymbol(signature.thisParameter); } - return false; - } - function isContextSensitiveFunctionLikeDeclaration(node) { - var areAllParametersUntyped = !ts.forEach(node.parameters, function (p) { return p.type; }); - var isNullaryArrow = node.kind === 180 /* ArrowFunction */ && !node.parameters.length; - return !node.typeParameters && areAllParametersUntyped && !isNullaryArrow; - } - function isContextSensitiveFunctionOrObjectLiteralMethod(func) { - return (isFunctionExpressionOrArrowFunction(func) || ts.isObjectLiteralMethod(func)) && isContextSensitiveFunctionLikeDeclaration(func); } - function getTypeWithoutSignatures(type) { - if (type.flags & 2588672 /* ObjectType */) { - var resolved = resolveStructuredTypeMembers(type); - if (resolved.constructSignatures.length) { - var result = createObjectType(2097152 /* Anonymous */, type.symbol); - result.members = resolved.members; - result.properties = resolved.properties; - result.callSignatures = emptyArray; - result.constructSignatures = emptyArray; - type = result; + function getReturnTypeOfSignature(signature) { + if (!signature.resolvedReturnType) { + if (!pushTypeResolution(signature, 3 /* ResolvedReturnType */)) { + return unknownType; } + var type = void 0; + if (signature.target) { + type = instantiateType(getReturnTypeOfSignature(signature.target), signature.mapper); + } + else if (signature.unionSignatures) { + type = getUnionType(ts.map(signature.unionSignatures, getReturnTypeOfSignature), /*subtypeReduction*/ true); + } + else { + type = getReturnTypeFromBody(signature.declaration); + } + if (!popTypeResolution()) { + type = anyType; + if (compilerOptions.noImplicitAny) { + var declaration = signature.declaration; + if (declaration.name) { + error(declaration.name, ts.Diagnostics._0_implicitly_has_return_type_any_because_it_does_not_have_a_return_type_annotation_and_is_referenced_directly_or_indirectly_in_one_of_its_return_expressions, ts.declarationNameToString(declaration.name)); + } + else { + error(declaration, ts.Diagnostics.Function_implicitly_has_return_type_any_because_it_does_not_have_a_return_type_annotation_and_is_referenced_directly_or_indirectly_in_one_of_its_return_expressions); + } + } + } + signature.resolvedReturnType = type; } - return type; - } - // TYPE CHECKING - function isTypeIdenticalTo(source, target) { - return isTypeRelatedTo(source, target, identityRelation); - } - function compareTypesIdentical(source, target) { - return isTypeRelatedTo(source, target, identityRelation) ? -1 /* True */ : 0 /* False */; - } - function compareTypesAssignable(source, target) { - return isTypeRelatedTo(source, target, assignableRelation) ? -1 /* True */ : 0 /* False */; + return signature.resolvedReturnType; } - function isTypeSubtypeOf(source, target) { - return isTypeRelatedTo(source, target, subtypeRelation); + function getRestTypeOfSignature(signature) { + if (signature.hasRestParameter) { + var type = getTypeOfSymbol(ts.lastOrUndefined(signature.parameters)); + if (type.flags & 131072 /* Reference */ && type.target === globalArrayType) { + return type.typeArguments[0]; + } + } + return anyType; } - function isTypeAssignableTo(source, target) { - return isTypeRelatedTo(source, target, assignableRelation); + function getSignatureInstantiation(signature, typeArguments) { + return instantiateSignature(signature, createTypeMapper(signature.typeParameters, typeArguments), /*eraseTypeParameters*/ true); } - // A type S is considered to be an instance of a type T if S and T are the same type or if S is a - // subtype of T but not structurally identical to T. This specifically means that two distinct but - // structurally identical types (such as two classes) are not considered instances of each other. - function isTypeInstanceOf(source, target) { - return source === target || isTypeSubtypeOf(source, target) && !isTypeIdenticalTo(source, target); + function getErasedSignature(signature) { + if (!signature.typeParameters) + return signature; + if (!signature.erasedSignatureCache) { + signature.erasedSignatureCache = instantiateSignature(signature, createTypeEraser(signature.typeParameters), /*eraseTypeParameters*/ true); + } + return signature.erasedSignatureCache; } - /** - * This is *not* a bi-directional relationship. - * If one needs to check both directions for comparability, use a second call to this function or 'checkTypeComparableTo'. - */ - function isTypeComparableTo(source, target) { - return isTypeRelatedTo(source, target, comparableRelation); + function getOrCreateTypeFromSignature(signature) { + // There are two ways to declare a construct signature, one is by declaring a class constructor + // using the constructor keyword, and the other is declaring a bare construct signature in an + // object type literal or interface (using the new keyword). Each way of declaring a constructor + // will result in a different declaration kind. + if (!signature.isolatedSignatureType) { + var isConstructor = signature.declaration.kind === 148 /* Constructor */ || signature.declaration.kind === 152 /* ConstructSignature */; + var type = createObjectType(2097152 /* Anonymous */); + type.members = emptySymbols; + type.properties = emptyArray; + type.callSignatures = !isConstructor ? [signature] : emptyArray; + type.constructSignatures = isConstructor ? [signature] : emptyArray; + signature.isolatedSignatureType = type; + } + return signature.isolatedSignatureType; } - function areTypesComparable(type1, type2) { - return isTypeComparableTo(type1, type2) || isTypeComparableTo(type2, type1); + function getIndexSymbol(symbol) { + return symbol.members["__index"]; } - function checkTypeSubtypeOf(source, target, errorNode, headMessage, containingMessageChain) { - return checkTypeRelatedTo(source, target, subtypeRelation, errorNode, headMessage, containingMessageChain); + function getIndexDeclarationOfSymbol(symbol, kind) { + var syntaxKind = kind === 1 /* Number */ ? 130 /* NumberKeyword */ : 132 /* StringKeyword */; + var indexSymbol = getIndexSymbol(symbol); + if (indexSymbol) { + for (var _i = 0, _a = indexSymbol.declarations; _i < _a.length; _i++) { + var decl = _a[_i]; + var node = decl; + if (node.parameters.length === 1) { + var parameter = node.parameters[0]; + if (parameter && parameter.type && parameter.type.kind === syntaxKind) { + return node; + } + } + } + } + return undefined; } - function checkTypeAssignableTo(source, target, errorNode, headMessage, containingMessageChain) { - return checkTypeRelatedTo(source, target, assignableRelation, errorNode, headMessage, containingMessageChain); + function createIndexInfo(type, isReadonly, declaration) { + return { type: type, isReadonly: isReadonly, declaration: declaration }; } - /** - * This is *not* a bi-directional relationship. - * If one needs to check both directions for comparability, use a second call to this function or 'isTypeComparableTo'. - */ - function checkTypeComparableTo(source, target, errorNode, headMessage, containingMessageChain) { - return checkTypeRelatedTo(source, target, comparableRelation, errorNode, headMessage, containingMessageChain); + function getIndexInfoOfSymbol(symbol, kind) { + var declaration = getIndexDeclarationOfSymbol(symbol, kind); + if (declaration) { + return createIndexInfo(declaration.type ? getTypeFromTypeNode(declaration.type) : anyType, (ts.getModifierFlags(declaration) & 64 /* Readonly */) !== 0, declaration); + } + return undefined; } - function isSignatureAssignableTo(source, target, ignoreReturnTypes) { - return compareSignaturesRelated(source, target, ignoreReturnTypes, /*reportErrors*/ false, /*errorReporter*/ undefined, compareTypesAssignable) !== 0 /* False */; + function getConstraintDeclaration(type) { + return ts.getDeclarationOfKind(type.symbol, 141 /* TypeParameter */).constraint; } - /** - * See signatureRelatedTo, compareSignaturesIdentical - */ - function compareSignaturesRelated(source, target, ignoreReturnTypes, reportErrors, errorReporter, compareTypes) { - // TODO (drosen): De-duplicate code between related functions. - if (source === target) { - return -1 /* True */; - } - if (!target.hasRestParameter && source.minArgumentCount > target.parameters.length) { - return 0 /* False */; - } - // Spec 1.0 Section 3.8.3 & 3.8.4: - // M and N (the signatures) are instantiated using type Any as the type argument for all type parameters declared by M and N - source = getErasedSignature(source); - target = getErasedSignature(target); - var result = -1 /* True */; - var sourceThisType = getThisTypeOfSignature(source); - if (sourceThisType && sourceThisType !== voidType) { - var targetThisType = getThisTypeOfSignature(target); - if (targetThisType) { - // void sources are assignable to anything. - var related = compareTypes(sourceThisType, targetThisType, /*reportErrors*/ false) - || compareTypes(targetThisType, sourceThisType, reportErrors); - if (!related) { - if (reportErrors) { - errorReporter(ts.Diagnostics.The_this_types_of_each_signature_are_incompatible); - } - return 0 /* False */; - } - result &= related; + function hasConstraintReferenceTo(type, target) { + var checked; + while (type && type.flags & 16384 /* TypeParameter */ && !(type.isThisType) && !ts.contains(checked, type)) { + if (type === target) { + return true; } + (checked || (checked = [])).push(type); + var constraintDeclaration = getConstraintDeclaration(type); + type = constraintDeclaration && getTypeFromTypeNode(constraintDeclaration); } - var sourceMax = getNumNonRestParameters(source); - var targetMax = getNumNonRestParameters(target); - var checkCount = getNumParametersToCheckForSignatureRelatability(source, sourceMax, target, targetMax); - var sourceParams = source.parameters; - var targetParams = target.parameters; - for (var i = 0; i < checkCount; i++) { - var s = i < sourceMax ? getTypeOfParameter(sourceParams[i]) : getRestTypeOfSignature(source); - var t = i < targetMax ? getTypeOfParameter(targetParams[i]) : getRestTypeOfSignature(target); - var related = compareTypes(s, t, /*reportErrors*/ false) || compareTypes(t, s, reportErrors); - if (!related) { - if (reportErrors) { - errorReporter(ts.Diagnostics.Types_of_parameters_0_and_1_are_incompatible, sourceParams[i < sourceMax ? i : sourceMax].name, targetParams[i < targetMax ? i : targetMax].name); + return false; + } + function getConstraintOfTypeParameter(typeParameter) { + if (!typeParameter.constraint) { + if (typeParameter.target) { + var targetConstraint = getConstraintOfTypeParameter(typeParameter.target); + typeParameter.constraint = targetConstraint ? instantiateType(targetConstraint, typeParameter.mapper) : noConstraintType; + } + else { + var constraintDeclaration = getConstraintDeclaration(typeParameter); + var constraint = getTypeFromTypeNode(constraintDeclaration); + if (hasConstraintReferenceTo(constraint, typeParameter)) { + error(constraintDeclaration, ts.Diagnostics.Type_parameter_0_has_a_circular_constraint, typeToString(typeParameter)); + constraint = unknownType; } - return 0 /* False */; + typeParameter.constraint = constraint; } - result &= related; } - if (!ignoreReturnTypes) { - var targetReturnType = getReturnTypeOfSignature(target); - if (targetReturnType === voidType) { - return result; - } - var sourceReturnType = getReturnTypeOfSignature(source); - // The following block preserves behavior forbidding boolean returning functions from being assignable to type guard returning functions - if (target.typePredicate) { - if (source.typePredicate) { - result &= compareTypePredicateRelatedTo(source.typePredicate, target.typePredicate, reportErrors, errorReporter, compareTypes); + return typeParameter.constraint === noConstraintType ? undefined : typeParameter.constraint; + } + function getParentSymbolOfTypeParameter(typeParameter) { + return getSymbolOfNode(ts.getDeclarationOfKind(typeParameter.symbol, 141 /* TypeParameter */).parent); + } + function getTypeListId(types) { + var result = ""; + if (types) { + var length_3 = types.length; + var i = 0; + while (i < length_3) { + var startId = types[i].id; + var count = 1; + while (i + count < length_3 && types[i + count].id === startId + count) { + count++; } - else if (ts.isIdentifierTypePredicate(target.typePredicate)) { - if (reportErrors) { - errorReporter(ts.Diagnostics.Signature_0_must_have_a_type_predicate, signatureToString(source)); - } - return 0 /* False */; + if (result.length) { + result += ","; } - } - else { - result &= compareTypes(sourceReturnType, targetReturnType, reportErrors); + result += startId; + if (count > 1) { + result += ":" + count; + } + i += count; } } return result; } - function compareTypePredicateRelatedTo(source, target, reportErrors, errorReporter, compareTypes) { - if (source.kind !== target.kind) { - if (reportErrors) { - errorReporter(ts.Diagnostics.A_this_based_type_guard_is_not_compatible_with_a_parameter_based_type_guard); - errorReporter(ts.Diagnostics.Type_predicate_0_is_not_assignable_to_1, typePredicateToString(source), typePredicateToString(target)); - } - return 0 /* False */; - } - if (source.kind === 1 /* Identifier */) { - var sourceIdentifierPredicate = source; - var targetIdentifierPredicate = target; - if (sourceIdentifierPredicate.parameterIndex !== targetIdentifierPredicate.parameterIndex) { - if (reportErrors) { - errorReporter(ts.Diagnostics.Parameter_0_is_not_in_the_same_position_as_parameter_1, sourceIdentifierPredicate.parameterName, targetIdentifierPredicate.parameterName); - errorReporter(ts.Diagnostics.Type_predicate_0_is_not_assignable_to_1, typePredicateToString(source), typePredicateToString(target)); - } - return 0 /* False */; + // This function is used to propagate certain flags when creating new object type references and union types. + // It is only necessary to do so if a constituent type might be the undefined type, the null type, the type + // of an object literal or the anyFunctionType. This is because there are operations in the type checker + // that care about the presence of such types at arbitrary depth in a containing type. + function getPropagatingFlagsOfTypes(types, excludeKinds) { + var result = 0; + for (var _i = 0, types_3 = types; _i < types_3.length; _i++) { + var type = types_3[_i]; + if (!(type.flags & excludeKinds)) { + result |= type.flags; } } - var related = compareTypes(source.type, target.type, reportErrors); - if (related === 0 /* False */ && reportErrors) { - errorReporter(ts.Diagnostics.Type_predicate_0_is_not_assignable_to_1, typePredicateToString(source), typePredicateToString(target)); - } - return related; + return result & 234881024 /* PropagatingFlags */; } - function isImplementationCompatibleWithOverload(implementation, overload) { - var erasedSource = getErasedSignature(implementation); - var erasedTarget = getErasedSignature(overload); - // First see if the return types are compatible in either direction. - var sourceReturnType = getReturnTypeOfSignature(erasedSource); - var targetReturnType = getReturnTypeOfSignature(erasedTarget); - if (targetReturnType === voidType - || isTypeRelatedTo(targetReturnType, sourceReturnType, assignableRelation) - || isTypeRelatedTo(sourceReturnType, targetReturnType, assignableRelation)) { - return isSignatureAssignableTo(erasedSource, erasedTarget, /*ignoreReturnTypes*/ true); + function createTypeReference(target, typeArguments) { + var id = getTypeListId(typeArguments); + var type = target.instantiations[id]; + if (!type) { + var propagatedFlags = typeArguments ? getPropagatingFlagsOfTypes(typeArguments, /*excludeKinds*/ 0) : 0; + var flags = 131072 /* Reference */ | propagatedFlags; + type = target.instantiations[id] = createObjectType(flags, target.symbol); + type.target = target; + type.typeArguments = typeArguments; } - return false; + return type; } - function getNumNonRestParameters(signature) { - var numParams = signature.parameters.length; - return signature.hasRestParameter ? - numParams - 1 : - numParams; + function cloneTypeReference(source) { + var type = createObjectType(source.flags, source.symbol); + type.target = source.target; + type.typeArguments = source.typeArguments; + return type; } - function getNumParametersToCheckForSignatureRelatability(source, sourceNonRestParamCount, target, targetNonRestParamCount) { - if (source.hasRestParameter === target.hasRestParameter) { - if (source.hasRestParameter) { - // If both have rest parameters, get the max and add 1 to - // compensate for the rest parameter. - return Math.max(sourceNonRestParamCount, targetNonRestParamCount) + 1; - } - else { - return Math.min(sourceNonRestParamCount, targetNonRestParamCount); + function getTypeReferenceArity(type) { + return type.target.typeParameters ? type.target.typeParameters.length : 0; + } + // Get type from reference to class or interface + function getTypeFromClassOrInterfaceReference(node, symbol) { + var type = getDeclaredTypeOfSymbol(getMergedSymbol(symbol)); + var typeParameters = type.localTypeParameters; + if (typeParameters) { + if (!node.typeArguments || node.typeArguments.length !== typeParameters.length) { + error(node, ts.Diagnostics.Generic_type_0_requires_1_type_argument_s, typeToString(type, /*enclosingDeclaration*/ undefined, 1 /* WriteArrayAsGenericType */), typeParameters.length); + return unknownType; } + // In a type reference, the outer type parameters of the referenced class or interface are automatically + // supplied as type arguments and the type reference only specifies arguments for the local type parameters + // of the class or interface. + return createTypeReference(type, ts.concatenate(type.outerTypeParameters, ts.map(node.typeArguments, getTypeFromTypeNodeNoAlias))); } - else { - // Return the count for whichever signature doesn't have rest parameters. - return source.hasRestParameter ? - targetNonRestParamCount : - sourceNonRestParamCount; + if (node.typeArguments) { + error(node, ts.Diagnostics.Type_0_is_not_generic, typeToString(type)); + return unknownType; } + return type; } - function isEnumTypeRelatedTo(source, target, errorReporter) { - if (source === target) { - return true; + // Get type from reference to type alias. When a type alias is generic, the declared type of the type alias may include + // references to the type parameters of the alias. We replace those with the actual type arguments by instantiating the + // declared type. Instantiations are cached using the type identities of the type arguments as the key. + function getTypeFromTypeAliasReference(node, symbol) { + var type = getDeclaredTypeOfSymbol(symbol); + var links = getSymbolLinks(symbol); + var typeParameters = links.typeParameters; + if (typeParameters) { + if (!node.typeArguments || node.typeArguments.length !== typeParameters.length) { + error(node, ts.Diagnostics.Generic_type_0_requires_1_type_argument_s, symbolToString(symbol), typeParameters.length); + return unknownType; + } + var typeArguments = ts.map(node.typeArguments, getTypeFromTypeNodeNoAlias); + var id = getTypeListId(typeArguments); + return links.instantiations[id] || (links.instantiations[id] = instantiateType(type, createTypeMapper(typeParameters, typeArguments))); } - var id = source.id + "," + target.id; - if (enumRelation[id] !== undefined) { - return enumRelation[id]; + if (node.typeArguments) { + error(node, ts.Diagnostics.Type_0_is_not_generic, symbolToString(symbol)); + return unknownType; } - if (source.symbol.name !== target.symbol.name || - !(source.symbol.flags & 256 /* RegularEnum */) || !(target.symbol.flags & 256 /* RegularEnum */) || - (source.flags & 524288 /* Union */) !== (target.flags & 524288 /* Union */)) { - return enumRelation[id] = false; + return type; + } + // Get type from reference to named type that cannot be generic (enum or type parameter) + function getTypeFromNonGenericTypeReference(node, symbol) { + if (node.typeArguments) { + error(node, ts.Diagnostics.Type_0_is_not_generic, symbolToString(symbol)); + return unknownType; } - var targetEnumType = getTypeOfSymbol(target.symbol); - for (var _i = 0, _a = getPropertiesOfType(getTypeOfSymbol(source.symbol)); _i < _a.length; _i++) { - var property = _a[_i]; - if (property.flags & 8 /* EnumMember */) { - var targetProperty = getPropertyOfType(targetEnumType, property.name); - if (!targetProperty || !(targetProperty.flags & 8 /* EnumMember */)) { - if (errorReporter) { - errorReporter(ts.Diagnostics.Property_0_is_missing_in_type_1, property.name, typeToString(target, /*enclosingDeclaration*/ undefined, 128 /* UseFullyQualifiedType */)); - } - return enumRelation[id] = false; + return getDeclaredTypeOfSymbol(symbol); + } + function getTypeReferenceName(node) { + switch (node.kind) { + case 155 /* TypeReference */: + return node.typeName; + case 267 /* JSDocTypeReference */: + return node.name; + case 194 /* ExpressionWithTypeArguments */: + // We only support expressions that are simple qualified names. For other + // expressions this produces undefined. + var expr = node.expression; + if (ts.isEntityNameExpression(expr)) { + return expr; } - } } - return enumRelation[id] = true; + return undefined; } - function isSimpleTypeRelatedTo(source, target, relation, errorReporter) { - if (target.flags & 8192 /* Never */) - return false; - if (target.flags & 1 /* Any */ || source.flags & 8192 /* Never */) - return true; - if (source.flags & 34 /* StringLike */ && target.flags & 2 /* String */) - return true; - if (source.flags & 340 /* NumberLike */ && target.flags & 4 /* Number */) - return true; - if (source.flags & 136 /* BooleanLike */ && target.flags & 8 /* Boolean */) - return true; - if (source.flags & 256 /* EnumLiteral */ && target.flags & 16 /* Enum */ && source.baseType === target) - return true; - if (source.flags & 16 /* Enum */ && target.flags & 16 /* Enum */ && isEnumTypeRelatedTo(source, target, errorReporter)) - return true; - if (source.flags & 2048 /* Undefined */ && (!strictNullChecks || target.flags & (2048 /* Undefined */ | 1024 /* Void */))) - return true; - if (source.flags & 4096 /* Null */ && (!strictNullChecks || target.flags & 4096 /* Null */)) - return true; - if (relation === assignableRelation || relation === comparableRelation) { - if (source.flags & 1 /* Any */) - return true; - if ((source.flags & 4 /* Number */ | source.flags & 64 /* NumberLiteral */) && target.flags & 272 /* EnumLike */) - return true; - if (source.flags & 256 /* EnumLiteral */ && - target.flags & 256 /* EnumLiteral */ && - source.text === target.text && - isEnumTypeRelatedTo(source.baseType, target.baseType, errorReporter)) { - return true; - } - if (source.flags & 256 /* EnumLiteral */ && - target.flags & 16 /* Enum */ && - isEnumTypeRelatedTo(target, source.baseType, errorReporter)) { - return true; - } + function resolveTypeReferenceName(node, typeReferenceName) { + if (!typeReferenceName) { + return unknownSymbol; } - return false; + return resolveEntityName(typeReferenceName, 793064 /* Type */) || unknownSymbol; } - function isTypeRelatedTo(source, target, relation) { - if (source.flags & 96 /* StringOrNumberLiteral */ && source.flags & 16777216 /* FreshLiteral */) { - source = source.regularType; + function getTypeReferenceType(node, symbol) { + if (symbol === unknownSymbol) { + return unknownType; } - if (target.flags & 96 /* StringOrNumberLiteral */ && target.flags & 16777216 /* FreshLiteral */) { - target = target.regularType; + if (symbol.flags & (32 /* Class */ | 64 /* Interface */)) { + return getTypeFromClassOrInterfaceReference(node, symbol); } - if (source === target || relation !== identityRelation && isSimpleTypeRelatedTo(source, target, relation)) { - return true; + if (symbol.flags & 524288 /* TypeAlias */) { + return getTypeFromTypeAliasReference(node, symbol); } - if (source.flags & 2588672 /* ObjectType */ && target.flags & 2588672 /* ObjectType */) { - var id = relation !== identityRelation || source.id < target.id ? source.id + "," + target.id : target.id + "," + source.id; - var related = relation[id]; - if (related !== undefined) { - return related === 1 /* Succeeded */; - } + if (symbol.flags & 107455 /* Value */ && node.kind === 267 /* JSDocTypeReference */) { + // A JSDocTypeReference may have resolved to a value (as opposed to a type). In + // that case, the type of this reference is just the type of the value we resolved + // to. + return getTypeOfSymbol(symbol); } - if (source.flags & 4177920 /* StructuredOrTypeParameter */ || target.flags & 4177920 /* StructuredOrTypeParameter */) { - return checkTypeRelatedTo(source, target, relation, undefined, undefined, undefined); + return getTypeFromNonGenericTypeReference(node, symbol); + } + function getTypeFromTypeReference(node) { + var links = getNodeLinks(node); + if (!links.resolvedType) { + var symbol = void 0; + var type = void 0; + if (node.kind === 267 /* JSDocTypeReference */) { + var typeReferenceName = getTypeReferenceName(node); + symbol = resolveTypeReferenceName(node, typeReferenceName); + type = getTypeReferenceType(node, symbol); + } + else { + // We only support expressions that are simple qualified names. For other expressions this produces undefined. + var typeNameOrExpression = node.kind === 155 /* TypeReference */ + ? node.typeName + : ts.isEntityNameExpression(node.expression) + ? node.expression + : undefined; + symbol = typeNameOrExpression && resolveEntityName(typeNameOrExpression, 793064 /* Type */) || unknownSymbol; + type = symbol === unknownSymbol ? unknownType : + symbol.flags & (32 /* Class */ | 64 /* Interface */) ? getTypeFromClassOrInterfaceReference(node, symbol) : + symbol.flags & 524288 /* TypeAlias */ ? getTypeFromTypeAliasReference(node, symbol) : + getTypeFromNonGenericTypeReference(node, symbol); + } + // Cache both the resolved symbol and the resolved type. The resolved symbol is needed in when we check the + // type reference in checkTypeReferenceOrExpressionWithTypeArguments. + links.resolvedSymbol = symbol; + links.resolvedType = type; } - return false; + return links.resolvedType; } - /** - * Checks if 'source' is related to 'target' (e.g.: is a assignable to). - * @param source The left-hand-side of the relation. - * @param target The right-hand-side of the relation. - * @param relation The relation considered. One of 'identityRelation', 'subtypeRelation', 'assignableRelation', or 'comparableRelation'. - * Used as both to determine which checks are performed and as a cache of previously computed results. - * @param errorNode The suggested node upon which all errors will be reported, if defined. This may or may not be the actual node used. - * @param headMessage If the error chain should be prepended by a head message, then headMessage will be used. - * @param containingMessageChain A chain of errors to prepend any new errors found. - */ - function checkTypeRelatedTo(source, target, relation, errorNode, headMessage, containingMessageChain) { - var errorInfo; - var sourceStack; - var targetStack; - var maybeStack; - var expandingFlags; - var depth = 0; - var overflow = false; - ts.Debug.assert(relation !== identityRelation || !errorNode, "no error reporting in identity checking"); - var result = isRelatedTo(source, target, /*reportErrors*/ !!errorNode, headMessage); - if (overflow) { - error(errorNode, ts.Diagnostics.Excessive_stack_depth_comparing_types_0_and_1, typeToString(source), typeToString(target)); + function getTypeFromTypeQueryNode(node) { + var links = getNodeLinks(node); + if (!links.resolvedType) { + // TypeScript 1.0 spec (April 2014): 3.6.3 + // The expression is processed as an identifier expression (section 4.3) + // or property access expression(section 4.10), + // the widened type(section 3.9) of which becomes the result. + links.resolvedType = getWidenedType(checkExpression(node.exprName)); } - else if (errorInfo) { - if (containingMessageChain) { - errorInfo = ts.concatenateDiagnosticMessageChains(containingMessageChain, errorInfo); + return links.resolvedType; + } + function getTypeOfGlobalSymbol(symbol, arity) { + function getTypeDeclaration(symbol) { + var declarations = symbol.declarations; + for (var _i = 0, declarations_3 = declarations; _i < declarations_3.length; _i++) { + var declaration = declarations_3[_i]; + switch (declaration.kind) { + case 221 /* ClassDeclaration */: + case 222 /* InterfaceDeclaration */: + case 224 /* EnumDeclaration */: + return declaration; + } } - diagnostics.add(ts.createDiagnosticForNodeFromMessageChain(errorNode, errorInfo)); } - return result !== 0 /* False */; - function reportError(message, arg0, arg1, arg2) { - ts.Debug.assert(!!errorNode); - errorInfo = ts.chainDiagnosticMessages(errorInfo, message, arg0, arg1, arg2); + if (!symbol) { + return arity ? emptyGenericType : emptyObjectType; } - function reportRelationError(message, source, target) { - var sourceType = typeToString(source); - var targetType = typeToString(target); - if (sourceType === targetType) { - sourceType = typeToString(source, /*enclosingDeclaration*/ undefined, 128 /* UseFullyQualifiedType */); - targetType = typeToString(target, /*enclosingDeclaration*/ undefined, 128 /* UseFullyQualifiedType */); - } - if (!message) { - message = relation === comparableRelation ? - ts.Diagnostics.Type_0_is_not_comparable_to_type_1 : - ts.Diagnostics.Type_0_is_not_assignable_to_type_1; - } - reportError(message, sourceType, targetType); + var type = getDeclaredTypeOfSymbol(symbol); + if (!(type.flags & 2588672 /* ObjectType */)) { + error(getTypeDeclaration(symbol), ts.Diagnostics.Global_type_0_must_be_a_class_or_interface_type, symbol.name); + return arity ? emptyGenericType : emptyObjectType; } - function tryElaborateErrorsForPrimitivesAndObjects(source, target) { - var sourceType = typeToString(source); - var targetType = typeToString(target); - if ((globalStringType === source && stringType === target) || - (globalNumberType === source && numberType === target) || - (globalBooleanType === source && booleanType === target) || - (getGlobalESSymbolType() === source && esSymbolType === target)) { - reportError(ts.Diagnostics._0_is_a_primitive_but_1_is_a_wrapper_object_Prefer_using_0_when_possible, targetType, sourceType); - } + if ((type.typeParameters ? type.typeParameters.length : 0) !== arity) { + error(getTypeDeclaration(symbol), ts.Diagnostics.Global_type_0_must_have_1_type_parameter_s, symbol.name, arity); + return arity ? emptyGenericType : emptyObjectType; } - // Compare two types and return - // Ternary.True if they are related with no assumptions, - // Ternary.Maybe if they are related with assumptions of other relationships, or - // Ternary.False if they are not related. - function isRelatedTo(source, target, reportErrors, headMessage) { - var result; - if (source.flags & 96 /* StringOrNumberLiteral */ && source.flags & 16777216 /* FreshLiteral */) { - source = source.regularType; - } - if (target.flags & 96 /* StringOrNumberLiteral */ && target.flags & 16777216 /* FreshLiteral */) { - target = target.regularType; - } - // both types are the same - covers 'they are the same primitive type or both are Any' or the same type parameter cases - if (source === target) - return -1 /* True */; - if (relation === identityRelation) { - return isIdenticalTo(source, target); - } - if (isSimpleTypeRelatedTo(source, target, relation, reportErrors ? reportError : undefined)) - return -1 /* True */; - if (source.flags & 8388608 /* ObjectLiteral */ && source.flags & 16777216 /* FreshLiteral */) { - if (hasExcessProperties(source, target, reportErrors)) { - if (reportErrors) { - reportRelationError(headMessage, source, target); - } - return 0 /* False */; - } - // Above we check for excess properties with respect to the entire target type. When union - // and intersection types are further deconstructed on the target side, we don't want to - // make the check again (as it might fail for a partial target type). Therefore we obtain - // the regular source type and proceed with that. - if (target.flags & 1572864 /* UnionOrIntersection */) { - source = getRegularTypeOfObjectLiteral(source); - } - } - var saveErrorInfo = errorInfo; - // Note that these checks are specifically ordered to produce correct results. - if (source.flags & 524288 /* Union */) { - if (relation === comparableRelation) { - result = someTypeRelatedToType(source, target, reportErrors && !(source.flags & 8190 /* Primitive */)); - } - else { - result = eachTypeRelatedToType(source, target, reportErrors && !(source.flags & 8190 /* Primitive */)); - } - if (result) { - return result; - } - } - else if (target.flags & 1048576 /* Intersection */) { - result = typeRelatedToEachType(source, target, reportErrors); - if (result) { - return result; - } - } - else { - // It is necessary to try these "some" checks on both sides because there may be nested "each" checks - // on either side that need to be prioritized. For example, A | B = (A | B) & (C | D) or - // A & B = (A & B) | (C & D). - if (source.flags & 1048576 /* Intersection */) { - // Check to see if any constituents of the intersection are immediately related to the target. - // - // Don't report errors though. Checking whether a constituent is related to the source is not actually - // useful and leads to some confusing error messages. Instead it is better to let the below checks - // take care of this, or to not elaborate at all. For instance, - // - // - For an object type (such as 'C = A & B'), users are usually more interested in structural errors. - // - // - For a union type (such as '(A | B) = (C & D)'), it's better to hold onto the whole intersection - // than to report that 'D' is not assignable to 'A' or 'B'. - // - // - For a primitive type or type parameter (such as 'number = A & B') there is no point in - // breaking the intersection apart. - if (result = someTypeRelatedToType(source, target, /*reportErrors*/ false)) { - return result; - } - } - if (target.flags & 524288 /* Union */) { - if (result = typeRelatedToSomeType(source, target, reportErrors && !(source.flags & 8190 /* Primitive */) && !(target.flags & 8190 /* Primitive */))) { - return result; - } - } + return type; + } + function getGlobalValueSymbol(name) { + return getGlobalSymbol(name, 107455 /* Value */, ts.Diagnostics.Cannot_find_global_value_0); + } + function getGlobalTypeSymbol(name) { + return getGlobalSymbol(name, 793064 /* Type */, ts.Diagnostics.Cannot_find_global_type_0); + } + function getGlobalSymbol(name, meaning, diagnostic) { + return resolveName(undefined, name, meaning, diagnostic, name); + } + function getGlobalType(name, arity) { + if (arity === void 0) { arity = 0; } + return getTypeOfGlobalSymbol(getGlobalTypeSymbol(name), arity); + } + /** + * Returns a type that is inside a namespace at the global scope, e.g. + * getExportedTypeFromNamespace('JSX', 'Element') returns the JSX.Element type + */ + function getExportedTypeFromNamespace(namespace, name) { + var namespaceSymbol = getGlobalSymbol(namespace, 1920 /* Namespace */, /*diagnosticMessage*/ undefined); + var typeSymbol = namespaceSymbol && getSymbol(namespaceSymbol.exports, name, 793064 /* Type */); + return typeSymbol && getDeclaredTypeOfSymbol(typeSymbol); + } + /** + * Creates a TypeReference for a generic `TypedPropertyDescriptor`. + */ + function createTypedPropertyDescriptorType(propertyType) { + var globalTypedPropertyDescriptorType = getGlobalTypedPropertyDescriptorType(); + return globalTypedPropertyDescriptorType !== emptyGenericType + ? createTypeReference(globalTypedPropertyDescriptorType, [propertyType]) + : emptyObjectType; + } + /** + * Instantiates a global type that is generic with some element type, and returns that instantiation. + */ + function createTypeFromGenericGlobalType(genericGlobalType, typeArguments) { + return genericGlobalType !== emptyGenericType ? createTypeReference(genericGlobalType, typeArguments) : emptyObjectType; + } + function createIterableType(elementType) { + return createTypeFromGenericGlobalType(getGlobalIterableType(), [elementType]); + } + function createIterableIteratorType(elementType) { + return createTypeFromGenericGlobalType(getGlobalIterableIteratorType(), [elementType]); + } + function createArrayType(elementType) { + return createTypeFromGenericGlobalType(globalArrayType, [elementType]); + } + function getTypeFromArrayTypeNode(node) { + var links = getNodeLinks(node); + if (!links.resolvedType) { + links.resolvedType = createArrayType(getTypeFromTypeNode(node.elementType)); + } + return links.resolvedType; + } + // We represent tuple types as type references to synthesized generic interface types created by + // this function. The types are of the form: + // + // interface Tuple extends Array { 0: T0, 1: T1, 2: T2, ... } + // + // Note that the generic type created by this function has no symbol associated with it. The same + // is true for each of the synthesized type parameters. + function createTupleTypeOfArity(arity) { + var typeParameters = []; + var properties = []; + for (var i = 0; i < arity; i++) { + var typeParameter = createType(16384 /* TypeParameter */); + typeParameters.push(typeParameter); + var property = createSymbol(4 /* Property */ | 67108864 /* Transient */, "" + i); + property.type = typeParameter; + properties.push(property); + } + var type = createObjectType(262144 /* Tuple */ | 131072 /* Reference */); + type.typeParameters = typeParameters; + type.outerTypeParameters = undefined; + type.localTypeParameters = typeParameters; + type.instantiations = ts.createMap(); + type.instantiations[getTypeListId(type.typeParameters)] = type; + type.target = type; + type.typeArguments = type.typeParameters; + type.thisType = createType(16384 /* TypeParameter */); + type.thisType.isThisType = true; + type.thisType.constraint = type; + type.declaredProperties = properties; + type.declaredCallSignatures = emptyArray; + type.declaredConstructSignatures = emptyArray; + type.declaredStringIndexInfo = undefined; + type.declaredNumberIndexInfo = undefined; + return type; + } + function getTupleTypeOfArity(arity) { + return tupleTypes[arity] || (tupleTypes[arity] = createTupleTypeOfArity(arity)); + } + function createTupleType(elementTypes) { + return createTypeReference(getTupleTypeOfArity(elementTypes.length), elementTypes); + } + function getTypeFromTupleTypeNode(node) { + var links = getNodeLinks(node); + if (!links.resolvedType) { + links.resolvedType = createTupleType(ts.map(node.elementTypes, getTypeFromTypeNodeNoAlias)); + } + return links.resolvedType; + } + function binarySearchTypes(types, type) { + var low = 0; + var high = types.length - 1; + var typeId = type.id; + while (low <= high) { + var middle = low + ((high - low) >> 1); + var id = types[middle].id; + if (id === typeId) { + return middle; } - if (source.flags & 16384 /* TypeParameter */) { - var constraint = getConstraintOfTypeParameter(source); - if (!constraint || constraint.flags & 1 /* Any */) { - constraint = emptyObjectType; - } - // The constraint may need to be further instantiated with its 'this' type. - constraint = getTypeWithThisArgument(constraint, source); - // Report constraint errors only if the constraint is not the empty object type - var reportConstraintErrors = reportErrors && constraint !== emptyObjectType; - if (result = isRelatedTo(constraint, target, reportConstraintErrors)) { - errorInfo = saveErrorInfo; - return result; - } + else if (id > typeId) { + high = middle - 1; } else { - if (source.flags & 131072 /* Reference */ && target.flags & 131072 /* Reference */ && source.target === target.target) { - // We have type references to same target type, see if relationship holds for all type arguments - if (result = typeArgumentsRelatedTo(source, target, reportErrors)) { - return result; - } - } - // Even if relationship doesn't hold for unions, intersections, or generic type references, - // it may hold in a structural comparison. - var apparentSource = getApparentType(source); - // In a check of the form X = A & B, we will have previously checked if A relates to X or B relates - // to X. Failing both of those we want to check if the aggregation of A and B's members structurally - // relates to X. Thus, we include intersection types on the source side here. - if (apparentSource.flags & (2588672 /* ObjectType */ | 1048576 /* Intersection */) && target.flags & 2588672 /* ObjectType */) { - // Report structural errors only if we haven't reported any errors yet - var reportStructuralErrors = reportErrors && errorInfo === saveErrorInfo && !(source.flags & 8190 /* Primitive */); - if (result = objectTypeRelatedTo(apparentSource, source, target, reportStructuralErrors)) { - errorInfo = saveErrorInfo; - return result; - } - } - } - if (reportErrors) { - if (source.flags & 2588672 /* ObjectType */ && target.flags & 8190 /* Primitive */) { - tryElaborateErrorsForPrimitivesAndObjects(source, target); - } - else if (source.symbol && source.flags & 2588672 /* ObjectType */ && globalObjectType === source) { - reportError(ts.Diagnostics.The_Object_type_is_assignable_to_very_few_other_types_Did_you_mean_to_use_the_any_type_instead); - } - reportRelationError(headMessage, source, target); + low = middle + 1; } - return 0 /* False */; } - function isIdenticalTo(source, target) { - var result; - if (source.flags & 2588672 /* ObjectType */ && target.flags & 2588672 /* ObjectType */) { - if (source.flags & 131072 /* Reference */ && target.flags & 131072 /* Reference */ && source.target === target.target) { - // We have type references to same target type, see if all type arguments are identical - if (result = typeArgumentsRelatedTo(source, target, /*reportErrors*/ false)) { - return result; - } - } - return objectTypeRelatedTo(source, source, target, /*reportErrors*/ false); - } - if (source.flags & 524288 /* Union */ && target.flags & 524288 /* Union */ || - source.flags & 1048576 /* Intersection */ && target.flags & 1048576 /* Intersection */) { - if (result = eachTypeRelatedToSomeType(source, target, /*reportErrors*/ false)) { - if (result &= eachTypeRelatedToSomeType(target, source, /*reportErrors*/ false)) { - return result; - } - } - } - return 0 /* False */; + return ~low; + } + function containsType(types, type) { + return binarySearchTypes(types, type) >= 0; + } + function addTypeToUnion(typeSet, type) { + var flags = type.flags; + if (flags & 524288 /* Union */) { + addTypesToUnion(typeSet, type.types); } - // Check if a property with the given name is known anywhere in the given type. In an object type, a property - // is considered known if the object type is empty and the check is for assignability, if the object type has - // index signatures, or if the property is actually declared in the object type. In a union or intersection - // type, a property is considered known if it is known in any constituent type. - function isKnownProperty(type, name) { - if (type.flags & 2588672 /* ObjectType */) { - var resolved = resolveStructuredTypeMembers(type); - if ((relation === assignableRelation || relation === comparableRelation) && (type === globalObjectType || isEmptyObjectType(resolved)) || - resolved.stringIndexInfo || - (resolved.numberIndexInfo && isNumericLiteralName(name)) || - getPropertyOfType(type, name)) { - return true; - } - } - else if (type.flags & 1572864 /* UnionOrIntersection */) { - for (var _i = 0, _a = type.types; _i < _a.length; _i++) { - var t = _a[_i]; - if (isKnownProperty(t, name)) { - return true; - } - } - } - return false; + else if (flags & 1 /* Any */) { + typeSet.containsAny = true; } - function isEmptyObjectType(t) { - return t.properties.length === 0 && - t.callSignatures.length === 0 && - t.constructSignatures.length === 0 && - !t.stringIndexInfo && - !t.numberIndexInfo; + else if (!strictNullChecks && flags & 6144 /* Nullable */) { + if (flags & 2048 /* Undefined */) + typeSet.containsUndefined = true; + if (flags & 4096 /* Null */) + typeSet.containsNull = true; + if (!(flags & 33554432 /* ContainsWideningType */)) + typeSet.containsNonWideningType = true; } - function hasExcessProperties(source, target, reportErrors) { - if (!(target.flags & 536870912 /* ObjectLiteralPatternWithComputedProperties */) && maybeTypeOfKind(target, 2588672 /* ObjectType */)) { - for (var _i = 0, _a = getPropertiesOfObjectType(source); _i < _a.length; _i++) { - var prop = _a[_i]; - if (!isKnownProperty(target, prop.name)) { - if (reportErrors) { - // We know *exactly* where things went wrong when comparing the types. - // Use this property as the error node as this will be more helpful in - // reasoning about what went wrong. - ts.Debug.assert(!!errorNode); - errorNode = prop.valueDeclaration; - reportError(ts.Diagnostics.Object_literal_may_only_specify_known_properties_and_0_does_not_exist_in_type_1, symbolToString(prop), typeToString(target)); - } - return true; - } + else if (!(flags & 8192 /* Never */)) { + if (flags & 2 /* String */) + typeSet.containsString = true; + if (flags & 4 /* Number */) + typeSet.containsNumber = true; + if (flags & 96 /* StringOrNumberLiteral */) + typeSet.containsStringOrNumberLiteral = true; + var len = typeSet.length; + var index = len && type.id > typeSet[len - 1].id ? ~len : binarySearchTypes(typeSet, type); + if (index < 0) { + if (!(flags & 2097152 /* Anonymous */ && type.symbol && type.symbol.flags & (16 /* Function */ | 8192 /* Method */) && containsIdenticalType(typeSet, type))) { + typeSet.splice(~index, 0, type); } } - return false; } - function eachTypeRelatedToSomeType(source, target, reportErrors) { - var result = -1 /* True */; - var sourceTypes = source.types; - for (var _i = 0, sourceTypes_1 = sourceTypes; _i < sourceTypes_1.length; _i++) { - var sourceType = sourceTypes_1[_i]; - var related = typeRelatedToSomeType(sourceType, target, /*reportErrors*/ false); - if (!related) { - return 0 /* False */; - } - result &= related; - } - return result; + } + // Add the given types to the given type set. Order is preserved, duplicates are removed, + // and nested types of the given kind are flattened into the set. + function addTypesToUnion(typeSet, types) { + for (var _i = 0, types_4 = types; _i < types_4.length; _i++) { + var type = types_4[_i]; + addTypeToUnion(typeSet, type); } - function typeRelatedToSomeType(source, target, reportErrors) { - var targetTypes = target.types; - if (target.flags & 524288 /* Union */ && containsType(targetTypes, source)) { - return -1 /* True */; + } + function containsIdenticalType(types, type) { + for (var _i = 0, types_5 = types; _i < types_5.length; _i++) { + var t = types_5[_i]; + if (isTypeIdenticalTo(t, type)) { + return true; } - var len = targetTypes.length; - for (var i = 0; i < len; i++) { - var related = isRelatedTo(source, targetTypes[i], reportErrors && i === len - 1); - if (related) { - return related; - } + } + return false; + } + function isSubtypeOfAny(candidate, types) { + for (var i = 0, len = types.length; i < len; i++) { + if (candidate !== types[i] && isTypeSubtypeOf(candidate, types[i])) { + return true; } - return 0 /* False */; } - function typeRelatedToEachType(source, target, reportErrors) { - var result = -1 /* True */; - var targetTypes = target.types; - for (var _i = 0, targetTypes_1 = targetTypes; _i < targetTypes_1.length; _i++) { - var targetType = targetTypes_1[_i]; - var related = isRelatedTo(source, targetType, reportErrors); - if (!related) { - return 0 /* False */; + return false; + } + function isSetOfLiteralsFromSameEnum(types) { + var first = types[0]; + if (first.flags & 256 /* EnumLiteral */) { + var firstEnum = getParentOfSymbol(first.symbol); + for (var i = 1; i < types.length; i++) { + var other = types[i]; + if (!(other.flags & 256 /* EnumLiteral */) || (firstEnum !== getParentOfSymbol(other.symbol))) { + return false; } - result &= related; } - return result; + return true; } - function someTypeRelatedToType(source, target, reportErrors) { - var sourceTypes = source.types; - if (source.flags & 524288 /* Union */ && containsType(sourceTypes, target)) { - return -1 /* True */; - } - var len = sourceTypes.length; - for (var i = 0; i < len; i++) { - var related = isRelatedTo(sourceTypes[i], target, reportErrors && i === len - 1); - if (related) { - return related; - } - } - return 0 /* False */; - } - function eachTypeRelatedToType(source, target, reportErrors) { - var result = -1 /* True */; - var sourceTypes = source.types; - for (var _i = 0, sourceTypes_2 = sourceTypes; _i < sourceTypes_2.length; _i++) { - var sourceType = sourceTypes_2[_i]; - var related = isRelatedTo(sourceType, target, reportErrors); - if (!related) { - return 0 /* False */; - } - result &= related; - } - return result; - } - function typeArgumentsRelatedTo(source, target, reportErrors) { - var sources = source.typeArguments || emptyArray; - var targets = target.typeArguments || emptyArray; - if (sources.length !== targets.length && relation === identityRelation) { - return 0 /* False */; - } - var length = sources.length <= targets.length ? sources.length : targets.length; - var result = -1 /* True */; - for (var i = 0; i < length; i++) { - var related = isRelatedTo(sources[i], targets[i], reportErrors); - if (!related) { - return 0 /* False */; - } - result &= related; - } - return result; - } - // Determine if two object types are related by structure. First, check if the result is already available in the global cache. - // Second, check if we have already started a comparison of the given two types in which case we assume the result to be true. - // Third, check if both types are part of deeply nested chains of generic type instantiations and if so assume the types are - // equal and infinitely expanding. Fourth, if we have reached a depth of 100 nested comparisons, assume we have runaway recursion - // and issue an error. Otherwise, actually compare the structure of the two types. - function objectTypeRelatedTo(source, originalSource, target, reportErrors) { - if (overflow) { - return 0 /* False */; - } - var id = relation !== identityRelation || source.id < target.id ? source.id + "," + target.id : target.id + "," + source.id; - var related = relation[id]; - if (related !== undefined) { - if (reportErrors && related === 2 /* Failed */) { - // We are elaborating errors and the cached result is an unreported failure. Record the result as a reported - // failure and continue computing the relation such that errors get reported. - relation[id] = 3 /* FailedAndReported */; - } - else { - return related === 1 /* Succeeded */ ? -1 /* True */ : 0 /* False */; - } - } - if (depth > 0) { - for (var i = 0; i < depth; i++) { - // If source and target are already being compared, consider them related with assumptions - if (maybeStack[i][id]) { - return 1 /* Maybe */; - } - } - if (depth === 100) { - overflow = true; - return 0 /* False */; - } - } - else { - sourceStack = []; - targetStack = []; - maybeStack = []; - expandingFlags = 0; - } - sourceStack[depth] = source; - targetStack[depth] = target; - maybeStack[depth] = ts.createMap(); - maybeStack[depth][id] = 1 /* Succeeded */; - depth++; - var saveExpandingFlags = expandingFlags; - if (!(expandingFlags & 1) && isDeeplyNestedGeneric(source, sourceStack, depth)) - expandingFlags |= 1; - if (!(expandingFlags & 2) && isDeeplyNestedGeneric(target, targetStack, depth)) - expandingFlags |= 2; - var result; - if (expandingFlags === 3) { - result = 1 /* Maybe */; - } - else { - result = propertiesRelatedTo(source, target, reportErrors); - if (result) { - result &= signaturesRelatedTo(source, target, 0 /* Call */, reportErrors); - if (result) { - result &= signaturesRelatedTo(source, target, 1 /* Construct */, reportErrors); - if (result) { - result &= indexTypesRelatedTo(source, originalSource, target, 0 /* String */, reportErrors); - if (result) { - result &= indexTypesRelatedTo(source, originalSource, target, 1 /* Number */, reportErrors); - } - } - } - } - } - expandingFlags = saveExpandingFlags; - depth--; - if (result) { - var maybeCache = maybeStack[depth]; - // If result is definitely true, copy assumptions to global cache, else copy to next level up - var destinationCache = (result === -1 /* True */ || depth === 0) ? relation : maybeStack[depth - 1]; - ts.copyProperties(maybeCache, destinationCache); - } - else { - // A false result goes straight into global cache (when something is false under assumptions it - // will also be false without assumptions) - relation[id] = reportErrors ? 3 /* FailedAndReported */ : 2 /* Failed */; - } - return result; + return false; + } + function removeSubtypes(types) { + if (types.length === 0 || isSetOfLiteralsFromSameEnum(types)) { + return; } - function propertiesRelatedTo(source, target, reportErrors) { - if (relation === identityRelation) { - return propertiesIdenticalTo(source, target); - } - var result = -1 /* True */; - var properties = getPropertiesOfObjectType(target); - var requireOptionalProperties = relation === subtypeRelation && !(source.flags & 8388608 /* ObjectLiteral */); - for (var _i = 0, properties_2 = properties; _i < properties_2.length; _i++) { - var targetProp = properties_2[_i]; - var sourceProp = getPropertyOfType(source, targetProp.name); - if (sourceProp !== targetProp) { - if (!sourceProp) { - if (!(targetProp.flags & 536870912 /* Optional */) || requireOptionalProperties) { - if (reportErrors) { - reportError(ts.Diagnostics.Property_0_is_missing_in_type_1, symbolToString(targetProp), typeToString(source)); - } - return 0 /* False */; - } - } - else if (!(targetProp.flags & 134217728 /* Prototype */)) { - var sourcePropFlags = getDeclarationModifierFlagsFromSymbol(sourceProp); - var targetPropFlags = getDeclarationModifierFlagsFromSymbol(targetProp); - if (sourcePropFlags & 8 /* Private */ || targetPropFlags & 8 /* Private */) { - if (sourceProp.valueDeclaration !== targetProp.valueDeclaration) { - if (reportErrors) { - if (sourcePropFlags & 8 /* Private */ && targetPropFlags & 8 /* Private */) { - reportError(ts.Diagnostics.Types_have_separate_declarations_of_a_private_property_0, symbolToString(targetProp)); - } - else { - reportError(ts.Diagnostics.Property_0_is_private_in_type_1_but_not_in_type_2, symbolToString(targetProp), typeToString(sourcePropFlags & 8 /* Private */ ? source : target), typeToString(sourcePropFlags & 8 /* Private */ ? target : source)); - } - } - return 0 /* False */; - } - } - else if (targetPropFlags & 16 /* Protected */) { - var sourceDeclaredInClass = sourceProp.parent && sourceProp.parent.flags & 32 /* Class */; - var sourceClass = sourceDeclaredInClass ? getDeclaredTypeOfSymbol(getParentOfSymbol(sourceProp)) : undefined; - var targetClass = getDeclaredTypeOfSymbol(getParentOfSymbol(targetProp)); - if (!sourceClass || !hasBaseType(sourceClass, targetClass)) { - if (reportErrors) { - reportError(ts.Diagnostics.Property_0_is_protected_but_type_1_is_not_a_class_derived_from_2, symbolToString(targetProp), typeToString(sourceClass || source), typeToString(targetClass)); - } - return 0 /* False */; - } - } - else if (sourcePropFlags & 16 /* Protected */) { - if (reportErrors) { - reportError(ts.Diagnostics.Property_0_is_protected_in_type_1_but_public_in_type_2, symbolToString(targetProp), typeToString(source), typeToString(target)); - } - return 0 /* False */; - } - var related = isRelatedTo(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp), reportErrors); - if (!related) { - if (reportErrors) { - reportError(ts.Diagnostics.Types_of_property_0_are_incompatible, symbolToString(targetProp)); - } - return 0 /* False */; - } - result &= related; - if (sourceProp.flags & 536870912 /* Optional */ && !(targetProp.flags & 536870912 /* Optional */)) { - // TypeScript 1.0 spec (April 2014): 3.8.3 - // S is a subtype of a type T, and T is a supertype of S if ... - // S' and T are object types and, for each member M in T.. - // M is a property and S' contains a property N where - // if M is a required property, N is also a required property - // (M - property in T) - // (N - property in S) - if (reportErrors) { - reportError(ts.Diagnostics.Property_0_is_optional_in_type_1_but_required_in_type_2, symbolToString(targetProp), typeToString(source), typeToString(target)); - } - return 0 /* False */; - } - } - } + var i = types.length; + while (i > 0) { + i--; + if (isSubtypeOfAny(types[i], types)) { + ts.orderedRemoveItemAt(types, i); } - return result; } - function propertiesIdenticalTo(source, target) { - if (!(source.flags & 2588672 /* ObjectType */ && target.flags & 2588672 /* ObjectType */)) { - return 0 /* False */; - } - var sourceProperties = getPropertiesOfObjectType(source); - var targetProperties = getPropertiesOfObjectType(target); - if (sourceProperties.length !== targetProperties.length) { - return 0 /* False */; - } - var result = -1 /* True */; - for (var _i = 0, sourceProperties_1 = sourceProperties; _i < sourceProperties_1.length; _i++) { - var sourceProp = sourceProperties_1[_i]; - var targetProp = getPropertyOfObjectType(target, sourceProp.name); - if (!targetProp) { - return 0 /* False */; - } - var related = compareProperties(sourceProp, targetProp, isRelatedTo); - if (!related) { - return 0 /* False */; - } - result &= related; + } + function removeRedundantLiteralTypes(types) { + var i = types.length; + while (i > 0) { + i--; + var t = types[i]; + var remove = t.flags & 32 /* StringLiteral */ && types.containsString || + t.flags & 64 /* NumberLiteral */ && types.containsNumber || + t.flags & 96 /* StringOrNumberLiteral */ && t.flags & 16777216 /* FreshLiteral */ && containsType(types, t.regularType); + if (remove) { + ts.orderedRemoveItemAt(types, i); } - return result; } - function signaturesRelatedTo(source, target, kind, reportErrors) { - if (relation === identityRelation) { - return signaturesIdenticalTo(source, target, kind); - } - if (target === anyFunctionType || source === anyFunctionType) { - return -1 /* True */; - } - var sourceSignatures = getSignaturesOfType(source, kind); - var targetSignatures = getSignaturesOfType(target, kind); - if (kind === 1 /* Construct */ && sourceSignatures.length && targetSignatures.length) { - if (isAbstractConstructorType(source) && !isAbstractConstructorType(target)) { - // An abstract constructor type is not assignable to a non-abstract constructor type - // as it would otherwise be possible to new an abstract class. Note that the assignability - // check we perform for an extends clause excludes construct signatures from the target, - // so this check never proceeds. - if (reportErrors) { - reportError(ts.Diagnostics.Cannot_assign_an_abstract_constructor_type_to_a_non_abstract_constructor_type); - } - return 0 /* False */; - } - if (!constructorVisibilitiesAreCompatible(sourceSignatures[0], targetSignatures[0], reportErrors)) { - return 0 /* False */; - } - } - var result = -1 /* True */; - var saveErrorInfo = errorInfo; - outer: for (var _i = 0, targetSignatures_1 = targetSignatures; _i < targetSignatures_1.length; _i++) { - var t = targetSignatures_1[_i]; - // Only elaborate errors from the first failure - var shouldElaborateErrors = reportErrors; - for (var _a = 0, sourceSignatures_1 = sourceSignatures; _a < sourceSignatures_1.length; _a++) { - var s = sourceSignatures_1[_a]; - var related = signatureRelatedTo(s, t, shouldElaborateErrors); - if (related) { - result &= related; - errorInfo = saveErrorInfo; - continue outer; - } - shouldElaborateErrors = false; - } - if (shouldElaborateErrors) { - reportError(ts.Diagnostics.Type_0_provides_no_match_for_the_signature_1, typeToString(source), signatureToString(t, /*enclosingDeclaration*/ undefined, /*flags*/ undefined, kind)); - } - return 0 /* False */; - } - return result; + } + // We sort and deduplicate the constituent types based on object identity. If the subtypeReduction + // flag is specified we also reduce the constituent type set to only include types that aren't subtypes + // of other types. Subtype reduction is expensive for large union types and is possible only when union + // types are known not to circularly reference themselves (as is the case with union types created by + // expression constructs such as array literals and the || and ?: operators). Named types can + // circularly reference themselves and therefore cannot be subtype reduced during their declaration. + // For example, "type Item = string | (() => Item" is a named type that circularly references itself. + function getUnionType(types, subtypeReduction, aliasSymbol, aliasTypeArguments) { + if (types.length === 0) { + return neverType; } - /** - * See signatureAssignableTo, compareSignaturesIdentical - */ - function signatureRelatedTo(source, target, reportErrors) { - return compareSignaturesRelated(source, target, /*ignoreReturnTypes*/ false, reportErrors, reportError, isRelatedTo); + if (types.length === 1) { + return types[0]; } - function signaturesIdenticalTo(source, target, kind) { - var sourceSignatures = getSignaturesOfType(source, kind); - var targetSignatures = getSignaturesOfType(target, kind); - if (sourceSignatures.length !== targetSignatures.length) { - return 0 /* False */; - } - var result = -1 /* True */; - for (var i = 0, len = sourceSignatures.length; i < len; i++) { - var related = compareSignaturesIdentical(sourceSignatures[i], targetSignatures[i], /*partialMatch*/ false, /*ignoreThisTypes*/ false, /*ignoreReturnTypes*/ false, isRelatedTo); - if (!related) { - return 0 /* False */; - } - result &= related; - } - return result; + var typeSet = []; + addTypesToUnion(typeSet, types); + if (typeSet.containsAny) { + return anyType; } - function eachPropertyRelatedTo(source, target, kind, reportErrors) { - var result = -1 /* True */; - for (var _i = 0, _a = getPropertiesOfObjectType(source); _i < _a.length; _i++) { - var prop = _a[_i]; - if (kind === 0 /* String */ || isNumericLiteralName(prop.name)) { - var related = isRelatedTo(getTypeOfSymbol(prop), target, reportErrors); - if (!related) { - if (reportErrors) { - reportError(ts.Diagnostics.Property_0_is_incompatible_with_index_signature, symbolToString(prop)); - } - return 0 /* False */; - } - result &= related; - } - } - return result; + if (subtypeReduction) { + removeSubtypes(typeSet); } - function indexInfoRelatedTo(sourceInfo, targetInfo, reportErrors) { - var related = isRelatedTo(sourceInfo.type, targetInfo.type, reportErrors); - if (!related && reportErrors) { - reportError(ts.Diagnostics.Index_signatures_are_incompatible); - } - return related; + else if (typeSet.containsStringOrNumberLiteral) { + removeRedundantLiteralTypes(typeSet); } - function indexTypesRelatedTo(source, originalSource, target, kind, reportErrors) { - if (relation === identityRelation) { - return indexTypesIdenticalTo(source, target, kind); - } - var targetInfo = getIndexInfoOfType(target, kind); - if (!targetInfo || ((targetInfo.type.flags & 1 /* Any */) && !(originalSource.flags & 8190 /* Primitive */))) { - // Index signature of type any permits assignment from everything but primitives - return -1 /* True */; - } - var sourceInfo = getIndexInfoOfType(source, kind) || - kind === 1 /* Number */ && getIndexInfoOfType(source, 0 /* String */); - if (sourceInfo) { - return indexInfoRelatedTo(sourceInfo, targetInfo, reportErrors); - } - if (isObjectLiteralType(source)) { - var related = -1 /* True */; - if (kind === 0 /* String */) { - var sourceNumberInfo = getIndexInfoOfType(source, 1 /* Number */); - if (sourceNumberInfo) { - related = indexInfoRelatedTo(sourceNumberInfo, targetInfo, reportErrors); - } - } - if (related) { - related &= eachPropertyRelatedTo(source, targetInfo.type, kind, reportErrors); - } - return related; - } - if (reportErrors) { - reportError(ts.Diagnostics.Index_signature_is_missing_in_type_0, typeToString(source)); - } - return 0 /* False */; + if (typeSet.length === 0) { + return typeSet.containsNull ? typeSet.containsNonWideningType ? nullType : nullWideningType : + typeSet.containsUndefined ? typeSet.containsNonWideningType ? undefinedType : undefinedWideningType : + neverType; } - function indexTypesIdenticalTo(source, target, indexKind) { - var targetInfo = getIndexInfoOfType(target, indexKind); - var sourceInfo = getIndexInfoOfType(source, indexKind); - if (!sourceInfo && !targetInfo) { - return -1 /* True */; - } - if (sourceInfo && targetInfo && sourceInfo.isReadonly === targetInfo.isReadonly) { - return isRelatedTo(sourceInfo.type, targetInfo.type); - } - return 0 /* False */; + return getUnionTypeFromSortedList(typeSet, aliasSymbol, aliasTypeArguments); + } + // This function assumes the constituent type list is sorted and deduplicated. + function getUnionTypeFromSortedList(types, aliasSymbol, aliasTypeArguments) { + if (types.length === 0) { + return neverType; } - function constructorVisibilitiesAreCompatible(sourceSignature, targetSignature, reportErrors) { - if (!sourceSignature.declaration || !targetSignature.declaration) { - return true; - } - var sourceAccessibility = ts.getModifierFlags(sourceSignature.declaration) & 24 /* NonPublicAccessibilityModifier */; - var targetAccessibility = ts.getModifierFlags(targetSignature.declaration) & 24 /* NonPublicAccessibilityModifier */; - // A public, protected and private signature is assignable to a private signature. - if (targetAccessibility === 8 /* Private */) { - return true; - } - // A public and protected signature is assignable to a protected signature. - if (targetAccessibility === 16 /* Protected */ && sourceAccessibility !== 8 /* Private */) { - return true; - } - // Only a public signature is assignable to public signature. - if (targetAccessibility !== 16 /* Protected */ && !sourceAccessibility) { - return true; - } - if (reportErrors) { - reportError(ts.Diagnostics.Cannot_assign_a_0_constructor_type_to_a_1_constructor_type, visibilityToString(sourceAccessibility), visibilityToString(targetAccessibility)); - } - return false; + if (types.length === 1) { + return types[0]; } - } - // Return true if the given type is the constructor type for an abstract class - function isAbstractConstructorType(type) { - if (type.flags & 2097152 /* Anonymous */) { - var symbol = type.symbol; - if (symbol && symbol.flags & 32 /* Class */) { - var declaration = getClassLikeDeclarationOfSymbol(symbol); - if (declaration && ts.getModifierFlags(declaration) & 128 /* Abstract */) { - return true; - } - } + var id = getTypeListId(types); + var type = unionTypes[id]; + if (!type) { + var propagatedFlags = getPropagatingFlagsOfTypes(types, /*excludeKinds*/ 6144 /* Nullable */); + type = unionTypes[id] = createObjectType(524288 /* Union */ | propagatedFlags); + type.types = types; + type.aliasSymbol = aliasSymbol; + type.aliasTypeArguments = aliasTypeArguments; } - return false; + return type; } - // Return true if the given type is part of a deeply nested chain of generic instantiations. We consider this to be the case - // when structural type comparisons have been started for 10 or more instantiations of the same generic type. It is possible, - // though highly unlikely, for this test to be true in a situation where a chain of instantiations is not infinitely expanding. - // Effectively, we will generate a false positive when two types are structurally equal to at least 10 levels, but unequal at - // some level beyond that. - function isDeeplyNestedGeneric(type, stack, depth) { - // We track type references (created by createTypeReference) and instantiated types (created by instantiateType) - if (type.flags & (131072 /* Reference */ | 4194304 /* Instantiated */) && depth >= 5) { - var symbol = type.symbol; - var count = 0; - for (var i = 0; i < depth; i++) { - var t = stack[i]; - if (t.flags & (131072 /* Reference */ | 4194304 /* Instantiated */) && t.symbol === symbol) { - count++; - if (count >= 5) - return true; - } - } + function getTypeFromUnionTypeNode(node, aliasSymbol, aliasTypeArguments) { + var links = getNodeLinks(node); + if (!links.resolvedType) { + links.resolvedType = getUnionType(ts.map(node.types, getTypeFromTypeNodeNoAlias), /*subtypeReduction*/ false, aliasSymbol, aliasTypeArguments); } - return false; - } - function isPropertyIdenticalTo(sourceProp, targetProp) { - return compareProperties(sourceProp, targetProp, compareTypesIdentical) !== 0 /* False */; + return links.resolvedType; } - function compareProperties(sourceProp, targetProp, compareTypes) { - // Two members are considered identical when - // - they are public properties with identical names, optionality, and types, - // - they are private or protected properties originating in the same declaration and having identical types - if (sourceProp === targetProp) { - return -1 /* True */; - } - var sourcePropAccessibility = getDeclarationModifierFlagsFromSymbol(sourceProp) & 24 /* NonPublicAccessibilityModifier */; - var targetPropAccessibility = getDeclarationModifierFlagsFromSymbol(targetProp) & 24 /* NonPublicAccessibilityModifier */; - if (sourcePropAccessibility !== targetPropAccessibility) { - return 0 /* False */; + function addTypeToIntersection(typeSet, type) { + if (type.flags & 1048576 /* Intersection */) { + addTypesToIntersection(typeSet, type.types); } - if (sourcePropAccessibility) { - if (getTargetSymbol(sourceProp) !== getTargetSymbol(targetProp)) { - return 0 /* False */; - } + else if (type.flags & 1 /* Any */) { + typeSet.containsAny = true; } - else { - if ((sourceProp.flags & 536870912 /* Optional */) !== (targetProp.flags & 536870912 /* Optional */)) { - return 0 /* False */; - } + else if (!(type.flags & 8192 /* Never */) && (strictNullChecks || !(type.flags & 6144 /* Nullable */)) && !ts.contains(typeSet, type)) { + typeSet.push(type); } - if (isReadonlySymbol(sourceProp) !== isReadonlySymbol(targetProp)) { - return 0 /* False */; + } + // Add the given types to the given type set. Order is preserved, duplicates are removed, + // and nested types of the given kind are flattened into the set. + function addTypesToIntersection(typeSet, types) { + for (var _i = 0, types_6 = types; _i < types_6.length; _i++) { + var type = types_6[_i]; + addTypeToIntersection(typeSet, type); } - return compareTypes(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp)); } - function isMatchingSignature(source, target, partialMatch) { - // A source signature matches a target signature if the two signatures have the same number of required, - // optional, and rest parameters. - if (source.parameters.length === target.parameters.length && - source.minArgumentCount === target.minArgumentCount && - source.hasRestParameter === target.hasRestParameter) { - return true; + // We do not perform structural deduplication on intersection types. Intersection types are created only by the & + // type operator and we can't reduce those because we want to support recursive intersection types. For example, + // a type alias of the form "type List = T & { next: List }" cannot be reduced during its declaration. + // Also, unlike union types, the order of the constituent types is preserved in order that overload resolution + // for intersections of types with signatures can be deterministic. + function getIntersectionType(types, aliasSymbol, aliasTypeArguments) { + if (types.length === 0) { + return emptyObjectType; } - // A source signature partially matches a target signature if the target signature has no fewer required - // parameters and no more overall parameters than the source signature (where a signature with a rest - // parameter is always considered to have more overall parameters than one without). - var sourceRestCount = source.hasRestParameter ? 1 : 0; - var targetRestCount = target.hasRestParameter ? 1 : 0; - if (partialMatch && source.minArgumentCount <= target.minArgumentCount && (sourceRestCount > targetRestCount || - sourceRestCount === targetRestCount && source.parameters.length >= target.parameters.length)) { - return true; + var typeSet = []; + addTypesToIntersection(typeSet, types); + if (typeSet.containsAny) { + return anyType; } - return false; - } - /** - * See signatureRelatedTo, compareSignaturesIdentical - */ - function compareSignaturesIdentical(source, target, partialMatch, ignoreThisTypes, ignoreReturnTypes, compareTypes) { - // TODO (drosen): De-duplicate code between related functions. - if (source === target) { - return -1 /* True */; + if (typeSet.length === 1) { + return typeSet[0]; } - if (!(isMatchingSignature(source, target, partialMatch))) { - return 0 /* False */; + var id = getTypeListId(typeSet); + var type = intersectionTypes[id]; + if (!type) { + var propagatedFlags = getPropagatingFlagsOfTypes(typeSet, /*excludeKinds*/ 6144 /* Nullable */); + type = intersectionTypes[id] = createObjectType(1048576 /* Intersection */ | propagatedFlags); + type.types = typeSet; + type.aliasSymbol = aliasSymbol; + type.aliasTypeArguments = aliasTypeArguments; } - // Check that the two signatures have the same number of type parameters. We might consider - // also checking that any type parameter constraints match, but that would require instantiating - // the constraints with a common set of type arguments to get relatable entities in places where - // type parameters occur in the constraints. The complexity of doing that doesn't seem worthwhile, - // particularly as we're comparing erased versions of the signatures below. - if ((source.typeParameters ? source.typeParameters.length : 0) !== (target.typeParameters ? target.typeParameters.length : 0)) { - return 0 /* False */; + return type; + } + function getTypeFromIntersectionTypeNode(node, aliasSymbol, aliasTypeArguments) { + var links = getNodeLinks(node); + if (!links.resolvedType) { + links.resolvedType = getIntersectionType(ts.map(node.types, getTypeFromTypeNodeNoAlias), aliasSymbol, aliasTypeArguments); } - // Spec 1.0 Section 3.8.3 & 3.8.4: - // M and N (the signatures) are instantiated using type Any as the type argument for all type parameters declared by M and N - source = getErasedSignature(source); - target = getErasedSignature(target); - var result = -1 /* True */; - if (!ignoreThisTypes) { - var sourceThisType = getThisTypeOfSignature(source); - if (sourceThisType) { - var targetThisType = getThisTypeOfSignature(target); - if (targetThisType) { - var related = compareTypes(sourceThisType, targetThisType); - if (!related) { - return 0 /* False */; - } - result &= related; - } - } + return links.resolvedType; + } + function getTypeFromTypeLiteralOrFunctionOrConstructorTypeNode(node, aliasSymbol, aliasTypeArguments) { + var links = getNodeLinks(node); + if (!links.resolvedType) { + // Deferred resolution of members is handled by resolveObjectTypeMembers + var type = createObjectType(2097152 /* Anonymous */, node.symbol); + type.aliasSymbol = aliasSymbol; + type.aliasTypeArguments = aliasTypeArguments; + links.resolvedType = type; } - var targetLen = target.parameters.length; - for (var i = 0; i < targetLen; i++) { - var s = isRestParameterIndex(source, i) ? getRestTypeOfSignature(source) : getTypeOfParameter(source.parameters[i]); - var t = isRestParameterIndex(target, i) ? getRestTypeOfSignature(target) : getTypeOfParameter(target.parameters[i]); - var related = compareTypes(s, t); - if (!related) { - return 0 /* False */; + return links.resolvedType; + } + function createLiteralType(flags, text) { + var type = createType(flags); + type.text = text; + return type; + } + function getFreshTypeOfLiteralType(type) { + if (type.flags & 96 /* StringOrNumberLiteral */ && !(type.flags & 16777216 /* FreshLiteral */)) { + if (!type.freshType) { + var freshType = createLiteralType(type.flags | 16777216 /* FreshLiteral */, type.text); + freshType.regularType = type; + type.freshType = freshType; } - result &= related; - } - if (!ignoreReturnTypes) { - result &= compareTypes(getReturnTypeOfSignature(source), getReturnTypeOfSignature(target)); + return type.freshType; } - return result; + return type; } - function isRestParameterIndex(signature, parameterIndex) { - return signature.hasRestParameter && parameterIndex >= signature.parameters.length - 1; + function getRegularTypeOfLiteralType(type) { + return type.flags & 96 /* StringOrNumberLiteral */ && type.flags & 16777216 /* FreshLiteral */ ? type.regularType : type; } - function isSupertypeOfEach(candidate, types) { - for (var _i = 0, types_7 = types; _i < types_7.length; _i++) { - var t = types_7[_i]; - if (candidate !== t && !isTypeSubtypeOf(t, candidate)) - return false; - } - return true; + function getLiteralTypeForText(flags, text) { + var map = flags & 32 /* StringLiteral */ ? stringLiteralTypes : numericLiteralTypes; + return map[text] || (map[text] = createLiteralType(flags, text)); } - function literalTypesWithSameBaseType(types) { - var commonBaseType; - for (var _i = 0, types_8 = types; _i < types_8.length; _i++) { - var t = types_8[_i]; - var baseType = getBaseTypeOfLiteralType(t); - if (!commonBaseType) { - commonBaseType = baseType; - } - if (baseType === t || baseType !== commonBaseType) { - return false; - } + function getTypeFromLiteralTypeNode(node) { + var links = getNodeLinks(node); + if (!links.resolvedType) { + links.resolvedType = getRegularTypeOfLiteralType(checkExpression(node.literal)); } - return true; - } - // When the candidate types are all literal types with the same base type, the common - // supertype is a union of those literal types. Otherwise, the common supertype is the - // first type that is a supertype of each of the other types. - function getSupertypeOrUnion(types) { - return literalTypesWithSameBaseType(types) ? getUnionType(types) : ts.forEach(types, function (t) { return isSupertypeOfEach(t, types) ? t : undefined; }); + return links.resolvedType; } - function getCommonSupertype(types) { - if (!strictNullChecks) { - return getSupertypeOrUnion(types); + function getTypeFromJSDocVariadicType(node) { + var links = getNodeLinks(node); + if (!links.resolvedType) { + var type = getTypeFromTypeNode(node.type); + links.resolvedType = type ? createArrayType(type) : unknownType; } - var primaryTypes = ts.filter(types, function (t) { return !(t.flags & 6144 /* Nullable */); }); - if (!primaryTypes.length) { - return getUnionType(types, /*subtypeReduction*/ true); + return links.resolvedType; + } + function getTypeFromJSDocTupleType(node) { + var links = getNodeLinks(node); + if (!links.resolvedType) { + var types = ts.map(node.types, getTypeFromTypeNodeNoAlias); + links.resolvedType = createTupleType(types); } - var supertype = getSupertypeOrUnion(primaryTypes); - return supertype && includeFalsyTypes(supertype, getFalsyFlagsOfTypes(types) & 6144 /* Nullable */); + return links.resolvedType; } - function reportNoCommonSupertypeError(types, errorLocation, errorMessageChainHead) { - // The downfallType/bestSupertypeDownfallType is the first type that caused a particular candidate - // to not be the common supertype. So if it weren't for this one downfallType (and possibly others), - // the type in question could have been the common supertype. - var bestSupertype; - var bestSupertypeDownfallType; - var bestSupertypeScore = 0; - for (var i = 0; i < types.length; i++) { - var score = 0; - var downfallType = undefined; - for (var j = 0; j < types.length; j++) { - if (isTypeSubtypeOf(types[j], types[i])) { - score++; - } - else if (!downfallType) { - downfallType = types[j]; - } - } - ts.Debug.assert(!!downfallType, "If there is no common supertype, each type should have a downfallType"); - if (score > bestSupertypeScore) { - bestSupertype = types[i]; - bestSupertypeDownfallType = downfallType; - bestSupertypeScore = score; - } - // types.length - 1 is the maximum score, given that getCommonSupertype returned false - if (bestSupertypeScore === types.length - 1) { - break; + function getThisType(node) { + var container = ts.getThisContainer(node, /*includeArrowFunctions*/ false); + var parent = container && container.parent; + if (parent && (ts.isClassLike(parent) || parent.kind === 222 /* InterfaceDeclaration */)) { + if (!(ts.getModifierFlags(container) & 32 /* Static */) && + (container.kind !== 148 /* Constructor */ || ts.isNodeDescendantOf(node, container.body))) { + return getDeclaredTypeOfClassOrInterface(getSymbolOfNode(parent)).thisType; } } - // In the following errors, the {1} slot is before the {0} slot because checkTypeSubtypeOf supplies the - // subtype as the first argument to the error - checkTypeSubtypeOf(bestSupertypeDownfallType, bestSupertype, errorLocation, ts.Diagnostics.Type_argument_candidate_1_is_not_a_valid_type_argument_because_it_is_not_a_supertype_of_candidate_0, errorMessageChainHead); - } - function isArrayType(type) { - return type.flags & 131072 /* Reference */ && type.target === globalArrayType; + error(node, ts.Diagnostics.A_this_type_is_available_only_in_a_non_static_member_of_a_class_or_interface); + return unknownType; } - function isArrayLikeType(type) { - // A type is array-like if it is a reference to the global Array or global ReadonlyArray type, - // or if it is not the undefined or null type and if it is assignable to ReadonlyArray - return type.flags & 131072 /* Reference */ && (type.target === globalArrayType || type.target === globalReadonlyArrayType) || - !(type.flags & 6144 /* Nullable */) && isTypeAssignableTo(type, anyReadonlyArrayType); + function getTypeFromThisTypeNode(node) { + var links = getNodeLinks(node); + if (!links.resolvedType) { + links.resolvedType = getThisType(node); + } + return links.resolvedType; } - function isTupleLikeType(type) { - return !!getPropertyOfType(type, "0"); + function getTypeFromTypeNodeNoAlias(type) { + return getTypeFromTypeNode(type, /*aliasSymbol*/ undefined, /*aliasTypeArguments*/ undefined); } - function isUnitType(type) { - return (type.flags & (480 /* Literal */ | 2048 /* Undefined */ | 4096 /* Null */)) !== 0; + function getTypeFromTypeNode(node, aliasSymbol, aliasTypeArguments) { + switch (node.kind) { + case 117 /* AnyKeyword */: + case 258 /* JSDocAllType */: + case 259 /* JSDocUnknownType */: + return anyType; + case 132 /* StringKeyword */: + return stringType; + case 130 /* NumberKeyword */: + return numberType; + case 120 /* BooleanKeyword */: + return booleanType; + case 133 /* SymbolKeyword */: + return esSymbolType; + case 103 /* VoidKeyword */: + return voidType; + case 135 /* UndefinedKeyword */: + return undefinedType; + case 93 /* NullKeyword */: + return nullType; + case 127 /* NeverKeyword */: + return neverType; + case 283 /* JSDocNullKeyword */: + return nullType; + case 284 /* JSDocUndefinedKeyword */: + return undefinedType; + case 285 /* JSDocNeverKeyword */: + return neverType; + case 165 /* ThisType */: + case 97 /* ThisKeyword */: + return getTypeFromThisTypeNode(node); + case 166 /* LiteralType */: + return getTypeFromLiteralTypeNode(node); + case 282 /* JSDocLiteralType */: + return getTypeFromLiteralTypeNode(node.literal); + case 155 /* TypeReference */: + case 267 /* JSDocTypeReference */: + return getTypeFromTypeReference(node); + case 154 /* TypePredicate */: + return booleanType; + case 194 /* ExpressionWithTypeArguments */: + return getTypeFromTypeReference(node); + case 158 /* TypeQuery */: + return getTypeFromTypeQueryNode(node); + case 160 /* ArrayType */: + case 260 /* JSDocArrayType */: + return getTypeFromArrayTypeNode(node); + case 161 /* TupleType */: + return getTypeFromTupleTypeNode(node); + case 162 /* UnionType */: + case 261 /* JSDocUnionType */: + return getTypeFromUnionTypeNode(node, aliasSymbol, aliasTypeArguments); + case 163 /* IntersectionType */: + return getTypeFromIntersectionTypeNode(node, aliasSymbol, aliasTypeArguments); + case 164 /* ParenthesizedType */: + case 263 /* JSDocNullableType */: + case 264 /* JSDocNonNullableType */: + case 271 /* JSDocConstructorType */: + case 272 /* JSDocThisType */: + case 268 /* JSDocOptionalType */: + return getTypeFromTypeNode(node.type); + case 265 /* JSDocRecordType */: + return getTypeFromTypeNode(node.literal); + case 156 /* FunctionType */: + case 157 /* ConstructorType */: + case 159 /* TypeLiteral */: + case 281 /* JSDocTypeLiteral */: + case 269 /* JSDocFunctionType */: + return getTypeFromTypeLiteralOrFunctionOrConstructorTypeNode(node, aliasSymbol, aliasTypeArguments); + // This function assumes that an identifier or qualified name is a type expression + // Callers should first ensure this by calling isTypeNode + case 69 /* Identifier */: + case 139 /* QualifiedName */: + var symbol = getSymbolAtLocation(node); + return symbol && getDeclaredTypeOfSymbol(symbol); + case 262 /* JSDocTupleType */: + return getTypeFromJSDocTupleType(node); + case 270 /* JSDocVariadicType */: + return getTypeFromJSDocVariadicType(node); + default: + return unknownType; + } } - function isLiteralType(type) { - return type.flags & 8 /* Boolean */ ? true : - type.flags & 524288 /* Union */ ? type.flags & 16 /* Enum */ ? true : !ts.forEach(type.types, function (t) { return !isUnitType(t); }) : - isUnitType(type); + function instantiateList(items, mapper, instantiator) { + if (items && items.length) { + var result = []; + for (var _i = 0, items_1 = items; _i < items_1.length; _i++) { + var v = items_1[_i]; + result.push(instantiator(v, mapper)); + } + return result; + } + return items; } - function getBaseTypeOfLiteralType(type) { - return type.flags & 32 /* StringLiteral */ ? stringType : - type.flags & 64 /* NumberLiteral */ ? numberType : - type.flags & 128 /* BooleanLiteral */ ? booleanType : - type.flags & 256 /* EnumLiteral */ ? type.baseType : - type.flags & 524288 /* Union */ && !(type.flags & 16 /* Enum */) ? getUnionType(ts.map(type.types, getBaseTypeOfLiteralType)) : - type; + function createUnaryTypeMapper(source, target) { + return function (t) { return t === source ? target : t; }; } - function getWidenedLiteralType(type) { - return type.flags & 32 /* StringLiteral */ && type.flags & 16777216 /* FreshLiteral */ ? stringType : - type.flags & 64 /* NumberLiteral */ && type.flags & 16777216 /* FreshLiteral */ ? numberType : - type.flags & 128 /* BooleanLiteral */ ? booleanType : - type.flags & 256 /* EnumLiteral */ ? type.baseType : - type.flags & 524288 /* Union */ && !(type.flags & 16 /* Enum */) ? getUnionType(ts.map(type.types, getWidenedLiteralType)) : - type; + function createBinaryTypeMapper(source1, target1, source2, target2) { + return function (t) { return t === source1 ? target1 : t === source2 ? target2 : t; }; } - /** - * Check if a Type was written as a tuple type literal. - * Prefer using isTupleLikeType() unless the use of `elementTypes` is required. - */ - function isTupleType(type) { - return !!(type.flags & 131072 /* Reference */ && type.target.flags & 262144 /* Tuple */); + function createArrayTypeMapper(sources, targets) { + return function (t) { + for (var i = 0; i < sources.length; i++) { + if (t === sources[i]) { + return targets ? targets[i] : anyType; + } + } + return t; + }; } - function getFalsyFlagsOfTypes(types) { - var result = 0; - for (var _i = 0, types_9 = types; _i < types_9.length; _i++) { - var t = types_9[_i]; - result |= getFalsyFlags(t); - } - return result; + function createTypeMapper(sources, targets) { + var count = sources.length; + var mapper = count == 1 ? createUnaryTypeMapper(sources[0], targets ? targets[0] : anyType) : + count == 2 ? createBinaryTypeMapper(sources[0], targets ? targets[0] : anyType, sources[1], targets ? targets[1] : anyType) : + createArrayTypeMapper(sources, targets); + mapper.mappedTypes = sources; + mapper.targetTypes = targets; + return mapper; } - // Returns the String, Number, Boolean, StringLiteral, NumberLiteral, BooleanLiteral, Void, Undefined, or Null - // flags for the string, number, boolean, "", 0, false, void, undefined, or null types respectively. Returns - // no flags for all other types (including non-falsy literal types). - function getFalsyFlags(type) { - return type.flags & 524288 /* Union */ ? getFalsyFlagsOfTypes(type.types) : - type.flags & 32 /* StringLiteral */ ? type.text === "" ? 32 /* StringLiteral */ : 0 : - type.flags & 64 /* NumberLiteral */ ? type.text === "0" ? 64 /* NumberLiteral */ : 0 : - type.flags & 128 /* BooleanLiteral */ ? type === falseType ? 128 /* BooleanLiteral */ : 0 : - type.flags & 7406 /* PossiblyFalsy */; + function createTypeEraser(sources) { + return createTypeMapper(sources, undefined); } - function includeFalsyTypes(type, flags) { - if ((getFalsyFlags(type) & flags) === flags) { - return type; + function getInferenceMapper(context) { + if (!context.mapper) { + var mapper = function (t) { + var typeParameters = context.signature.typeParameters; + for (var i = 0; i < typeParameters.length; i++) { + if (t === typeParameters[i]) { + context.inferences[i].isFixed = true; + return getInferredType(context, i); + } + } + return t; + }; + mapper.mappedTypes = context.signature.typeParameters; + mapper.context = context; + context.mapper = mapper; } - var types = [type]; - if (flags & 34 /* StringLike */) - types.push(emptyStringType); - if (flags & 340 /* NumberLike */) - types.push(zeroType); - if (flags & 136 /* BooleanLike */) - types.push(falseType); - if (flags & 1024 /* Void */) - types.push(voidType); - if (flags & 2048 /* Undefined */) - types.push(undefinedType); - if (flags & 4096 /* Null */) - types.push(nullType); - return getUnionType(types, /*subtypeReduction*/ true); + return context.mapper; } - function removeDefinitelyFalsyTypes(type) { - return getFalsyFlags(type) & 7392 /* DefinitelyFalsy */ ? - filterType(type, function (t) { return !(getFalsyFlags(t) & 7392 /* DefinitelyFalsy */); }) : - type; + function identityMapper(type) { + return type; } - function getNonNullableType(type) { - return strictNullChecks ? getTypeWithFacts(type, 524288 /* NEUndefinedOrNull */) : type; + function combineTypeMappers(mapper1, mapper2) { + var mapper = function (t) { return instantiateType(mapper1(t), mapper2); }; + mapper.mappedTypes = mapper1.mappedTypes; + return mapper; } - /** - * Return true if type was inferred from an object literal or written as an object type literal - * with no call or construct signatures. - */ - function isObjectLiteralType(type) { - return type.symbol && (type.symbol.flags & (4096 /* ObjectLiteral */ | 2048 /* TypeLiteral */)) !== 0 && - getSignaturesOfType(type, 0 /* Call */).length === 0 && - getSignaturesOfType(type, 1 /* Construct */).length === 0; + function cloneTypeParameter(typeParameter) { + var result = createType(16384 /* TypeParameter */); + result.symbol = typeParameter.symbol; + result.target = typeParameter; + return result; } - function createTransientSymbol(source, type) { - var symbol = createSymbol(source.flags | 67108864 /* Transient */, source.name); - symbol.declarations = source.declarations; - symbol.parent = source.parent; - symbol.type = type; - symbol.target = source; - if (source.valueDeclaration) { - symbol.valueDeclaration = source.valueDeclaration; + function cloneTypePredicate(predicate, mapper) { + if (ts.isIdentifierTypePredicate(predicate)) { + return { + kind: 1 /* Identifier */, + parameterName: predicate.parameterName, + parameterIndex: predicate.parameterIndex, + type: instantiateType(predicate.type, mapper) + }; } - return symbol; - } - function transformTypeOfMembers(type, f) { - var members = ts.createMap(); - for (var _i = 0, _a = getPropertiesOfObjectType(type); _i < _a.length; _i++) { - var property = _a[_i]; - var original = getTypeOfSymbol(property); - var updated = f(original); - members[property.name] = updated === original ? property : createTransientSymbol(property, updated); + else { + return { + kind: 0 /* This */, + type: instantiateType(predicate.type, mapper) + }; } - ; - return members; } - /** - * If the the provided object literal is subject to the excess properties check, - * create a new that is exempt. Recursively mark object literal members as exempt. - * Leave signatures alone since they are not subject to the check. - */ - function getRegularTypeOfObjectLiteral(type) { - if (!(type.flags & 8388608 /* ObjectLiteral */ && type.flags & 16777216 /* FreshLiteral */)) { - return type; + function instantiateSignature(signature, mapper, eraseTypeParameters) { + var freshTypeParameters; + var freshTypePredicate; + if (signature.typeParameters && !eraseTypeParameters) { + // First create a fresh set of type parameters, then include a mapping from the old to the + // new type parameters in the mapper function. Finally store this mapper in the new type + // parameters such that we can use it when instantiating constraints. + freshTypeParameters = ts.map(signature.typeParameters, cloneTypeParameter); + mapper = combineTypeMappers(createTypeMapper(signature.typeParameters, freshTypeParameters), mapper); + for (var _i = 0, freshTypeParameters_1 = freshTypeParameters; _i < freshTypeParameters_1.length; _i++) { + var tp = freshTypeParameters_1[_i]; + tp.mapper = mapper; + } } - var regularType = type.regularType; - if (regularType) { - return regularType; + if (signature.typePredicate) { + freshTypePredicate = cloneTypePredicate(signature.typePredicate, mapper); } - var resolved = type; - var members = transformTypeOfMembers(type, getRegularTypeOfObjectLiteral); - var regularNew = createAnonymousType(resolved.symbol, members, resolved.callSignatures, resolved.constructSignatures, resolved.stringIndexInfo, resolved.numberIndexInfo); - regularNew.flags = resolved.flags & ~16777216 /* FreshLiteral */; - type.regularType = regularNew; - return regularNew; + var result = createSignature(signature.declaration, freshTypeParameters, signature.thisParameter && instantiateSymbol(signature.thisParameter, mapper), instantiateList(signature.parameters, mapper, instantiateSymbol), instantiateType(signature.resolvedReturnType, mapper), freshTypePredicate, signature.minArgumentCount, signature.hasRestParameter, signature.hasLiteralTypes); + result.target = signature; + result.mapper = mapper; + return result; } - function getWidenedTypeOfObjectLiteral(type) { - var members = transformTypeOfMembers(type, function (prop) { - var widened = getWidenedType(prop); - return prop === widened ? prop : widened; - }); - var stringIndexInfo = getIndexInfoOfType(type, 0 /* String */); - var numberIndexInfo = getIndexInfoOfType(type, 1 /* Number */); - return createAnonymousType(type.symbol, members, emptyArray, emptyArray, stringIndexInfo && createIndexInfo(getWidenedType(stringIndexInfo.type), stringIndexInfo.isReadonly), numberIndexInfo && createIndexInfo(getWidenedType(numberIndexInfo.type), numberIndexInfo.isReadonly)); + function instantiateSymbol(symbol, mapper) { + if (symbol.flags & 16777216 /* Instantiated */) { + var links = getSymbolLinks(symbol); + // If symbol being instantiated is itself a instantiation, fetch the original target and combine the + // type mappers. This ensures that original type identities are properly preserved and that aliases + // always reference a non-aliases. + symbol = links.target; + mapper = combineTypeMappers(links.mapper, mapper); + } + // Keep the flags from the symbol we're instantiating. Mark that is instantiated, and + // also transient so that we can just store data on it directly. + var result = createSymbol(16777216 /* Instantiated */ | 67108864 /* Transient */ | symbol.flags, symbol.name); + result.declarations = symbol.declarations; + result.parent = symbol.parent; + result.target = symbol; + result.mapper = mapper; + if (symbol.valueDeclaration) { + result.valueDeclaration = symbol.valueDeclaration; + } + return result; } - function getWidenedConstituentType(type) { - return type.flags & 6144 /* Nullable */ ? type : getWidenedType(type); + function instantiateAnonymousType(type, mapper) { + if (mapper.instantiations) { + var cachedType = mapper.instantiations[type.id]; + if (cachedType) { + return cachedType; + } + } + else { + mapper.instantiations = []; + } + // Mark the anonymous type as instantiated such that our infinite instantiation detection logic can recognize it + var result = createObjectType(2097152 /* Anonymous */ | 4194304 /* Instantiated */, type.symbol); + result.target = type; + result.mapper = mapper; + result.aliasSymbol = type.aliasSymbol; + result.aliasTypeArguments = mapper.targetTypes; + mapper.instantiations[type.id] = result; + return result; } - function getWidenedType(type) { - if (type.flags & 100663296 /* RequiresWidening */) { - if (type.flags & 6144 /* Nullable */) { - return anyType; + function isSymbolInScopeOfMappedTypeParameter(symbol, mapper) { + var mappedTypes = mapper.mappedTypes; + // Starting with the parent of the symbol's declaration, check if the mapper maps any of + // the type parameters introduced by enclosing declarations. We just pick the first + // declaration since multiple declarations will all have the same parent anyway. + var node = symbol.declarations[0].parent; + while (node) { + switch (node.kind) { + case 156 /* FunctionType */: + case 157 /* ConstructorType */: + case 220 /* FunctionDeclaration */: + case 147 /* MethodDeclaration */: + case 146 /* MethodSignature */: + case 148 /* Constructor */: + case 151 /* CallSignature */: + case 152 /* ConstructSignature */: + case 153 /* IndexSignature */: + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + case 179 /* FunctionExpression */: + case 180 /* ArrowFunction */: + case 221 /* ClassDeclaration */: + case 192 /* ClassExpression */: + case 222 /* InterfaceDeclaration */: + case 223 /* TypeAliasDeclaration */: + var declaration = node; + if (declaration.typeParameters) { + for (var _i = 0, _a = declaration.typeParameters; _i < _a.length; _i++) { + var d = _a[_i]; + if (ts.contains(mappedTypes, getDeclaredTypeOfTypeParameter(getSymbolOfNode(d)))) { + return true; + } + } + } + if (ts.isClassLike(node) || node.kind === 222 /* InterfaceDeclaration */) { + var thisType = getDeclaredTypeOfClassOrInterface(getSymbolOfNode(node)).thisType; + if (thisType && ts.contains(mappedTypes, thisType)) { + return true; + } + } + break; + case 225 /* ModuleDeclaration */: + case 256 /* SourceFile */: + return false; } - if (type.flags & 8388608 /* ObjectLiteral */) { - return getWidenedTypeOfObjectLiteral(type); + node = node.parent; + } + return false; + } + function instantiateType(type, mapper) { + if (type && mapper !== identityMapper) { + if (type.flags & 16384 /* TypeParameter */) { + return mapper(type); } - if (type.flags & 524288 /* Union */) { - return getUnionType(ts.map(type.types, getWidenedConstituentType)); + if (type.flags & 2097152 /* Anonymous */) { + // If the anonymous type originates in a declaration of a function, method, class, or + // interface, in an object type literal, or in an object literal expression, we may need + // to instantiate the type because it might reference a type parameter. We skip instantiation + // if none of the type parameters that are in scope in the type's declaration are mapped by + // the given mapper, however we can only do that analysis if the type isn't itself an + // instantiation. + return type.symbol && + type.symbol.flags & (16 /* Function */ | 8192 /* Method */ | 32 /* Class */ | 2048 /* TypeLiteral */ | 4096 /* ObjectLiteral */) && + (type.flags & 4194304 /* Instantiated */ || isSymbolInScopeOfMappedTypeParameter(type.symbol, mapper)) ? + instantiateAnonymousType(type, mapper) : type; } - if (isArrayType(type) || isTupleType(type)) { - return createTypeReference(type.target, ts.map(type.typeArguments, getWidenedType)); + if (type.flags & 131072 /* Reference */) { + return createTypeReference(type.target, instantiateList(type.typeArguments, mapper, instantiateType)); + } + if (type.flags & 524288 /* Union */ && !(type.flags & 8190 /* Primitive */)) { + return getUnionType(instantiateList(type.types, mapper, instantiateType), /*subtypeReduction*/ false, type.aliasSymbol, mapper.targetTypes); + } + if (type.flags & 1048576 /* Intersection */) { + return getIntersectionType(instantiateList(type.types, mapper, instantiateType), type.aliasSymbol, mapper.targetTypes); + } + } + return type; + } + function instantiateIndexInfo(info, mapper) { + return info && createIndexInfo(instantiateType(info.type, mapper), info.isReadonly, info.declaration); + } + // Returns true if the given expression contains (at any level of nesting) a function or arrow expression + // that is subject to contextual typing. + function isContextSensitive(node) { + ts.Debug.assert(node.kind !== 147 /* MethodDeclaration */ || ts.isObjectLiteralMethod(node)); + switch (node.kind) { + case 179 /* FunctionExpression */: + case 180 /* ArrowFunction */: + return isContextSensitiveFunctionLikeDeclaration(node); + case 171 /* ObjectLiteralExpression */: + return ts.forEach(node.properties, isContextSensitive); + case 170 /* ArrayLiteralExpression */: + return ts.forEach(node.elements, isContextSensitive); + case 188 /* ConditionalExpression */: + return isContextSensitive(node.whenTrue) || + isContextSensitive(node.whenFalse); + case 187 /* BinaryExpression */: + return node.operatorToken.kind === 52 /* BarBarToken */ && + (isContextSensitive(node.left) || isContextSensitive(node.right)); + case 253 /* PropertyAssignment */: + return isContextSensitive(node.initializer); + case 147 /* MethodDeclaration */: + case 146 /* MethodSignature */: + return isContextSensitiveFunctionLikeDeclaration(node); + case 178 /* ParenthesizedExpression */: + return isContextSensitive(node.expression); + } + return false; + } + function isContextSensitiveFunctionLikeDeclaration(node) { + var areAllParametersUntyped = !ts.forEach(node.parameters, function (p) { return p.type; }); + var isNullaryArrow = node.kind === 180 /* ArrowFunction */ && !node.parameters.length; + return !node.typeParameters && areAllParametersUntyped && !isNullaryArrow; + } + function isContextSensitiveFunctionOrObjectLiteralMethod(func) { + return (isFunctionExpressionOrArrowFunction(func) || ts.isObjectLiteralMethod(func)) && isContextSensitiveFunctionLikeDeclaration(func); + } + function getTypeWithoutSignatures(type) { + if (type.flags & 2588672 /* ObjectType */) { + var resolved = resolveStructuredTypeMembers(type); + if (resolved.constructSignatures.length) { + var result = createObjectType(2097152 /* Anonymous */, type.symbol); + result.members = resolved.members; + result.properties = resolved.properties; + result.callSignatures = emptyArray; + result.constructSignatures = emptyArray; + type = result; } } return type; } + // TYPE CHECKING + function isTypeIdenticalTo(source, target) { + return isTypeRelatedTo(source, target, identityRelation); + } + function compareTypesIdentical(source, target) { + return isTypeRelatedTo(source, target, identityRelation) ? -1 /* True */ : 0 /* False */; + } + function compareTypesAssignable(source, target) { + return isTypeRelatedTo(source, target, assignableRelation) ? -1 /* True */ : 0 /* False */; + } + function isTypeSubtypeOf(source, target) { + return isTypeRelatedTo(source, target, subtypeRelation); + } + function isTypeAssignableTo(source, target) { + return isTypeRelatedTo(source, target, assignableRelation); + } + // A type S is considered to be an instance of a type T if S and T are the same type or if S is a + // subtype of T but not structurally identical to T. This specifically means that two distinct but + // structurally identical types (such as two classes) are not considered instances of each other. + function isTypeInstanceOf(source, target) { + return source === target || isTypeSubtypeOf(source, target) && !isTypeIdenticalTo(source, target); + } /** - * Reports implicit any errors that occur as a result of widening 'null' and 'undefined' - * to 'any'. A call to reportWideningErrorsInType is normally accompanied by a call to - * getWidenedType. But in some cases getWidenedType is called without reporting errors - * (type argument inference is an example). - * - * The return value indicates whether an error was in fact reported. The particular circumstances - * are on a best effort basis. Currently, if the null or undefined that causes widening is inside - * an object literal property (arbitrarily deeply), this function reports an error. If no error is - * reported, reportImplicitAnyError is a suitable fallback to report a general error. + * This is *not* a bi-directional relationship. + * If one needs to check both directions for comparability, use a second call to this function or 'checkTypeComparableTo'. */ - function reportWideningErrorsInType(type) { - var errorReported = false; - if (type.flags & 524288 /* Union */) { - for (var _i = 0, _a = type.types; _i < _a.length; _i++) { - var t = _a[_i]; - if (reportWideningErrorsInType(t)) { - errorReported = true; + function isTypeComparableTo(source, target) { + return isTypeRelatedTo(source, target, comparableRelation); + } + function areTypesComparable(type1, type2) { + return isTypeComparableTo(type1, type2) || isTypeComparableTo(type2, type1); + } + function checkTypeSubtypeOf(source, target, errorNode, headMessage, containingMessageChain) { + return checkTypeRelatedTo(source, target, subtypeRelation, errorNode, headMessage, containingMessageChain); + } + function checkTypeAssignableTo(source, target, errorNode, headMessage, containingMessageChain) { + return checkTypeRelatedTo(source, target, assignableRelation, errorNode, headMessage, containingMessageChain); + } + /** + * This is *not* a bi-directional relationship. + * If one needs to check both directions for comparability, use a second call to this function or 'isTypeComparableTo'. + */ + function checkTypeComparableTo(source, target, errorNode, headMessage, containingMessageChain) { + return checkTypeRelatedTo(source, target, comparableRelation, errorNode, headMessage, containingMessageChain); + } + function isSignatureAssignableTo(source, target, ignoreReturnTypes) { + return compareSignaturesRelated(source, target, ignoreReturnTypes, /*reportErrors*/ false, /*errorReporter*/ undefined, compareTypesAssignable) !== 0 /* False */; + } + /** + * See signatureRelatedTo, compareSignaturesIdentical + */ + function compareSignaturesRelated(source, target, ignoreReturnTypes, reportErrors, errorReporter, compareTypes) { + // TODO (drosen): De-duplicate code between related functions. + if (source === target) { + return -1 /* True */; + } + if (!target.hasRestParameter && source.minArgumentCount > target.parameters.length) { + return 0 /* False */; + } + // Spec 1.0 Section 3.8.3 & 3.8.4: + // M and N (the signatures) are instantiated using type Any as the type argument for all type parameters declared by M and N + source = getErasedSignature(source); + target = getErasedSignature(target); + var result = -1 /* True */; + var sourceThisType = getThisTypeOfSignature(source); + if (sourceThisType && sourceThisType !== voidType) { + var targetThisType = getThisTypeOfSignature(target); + if (targetThisType) { + // void sources are assignable to anything. + var related = compareTypes(sourceThisType, targetThisType, /*reportErrors*/ false) + || compareTypes(targetThisType, sourceThisType, reportErrors); + if (!related) { + if (reportErrors) { + errorReporter(ts.Diagnostics.The_this_types_of_each_signature_are_incompatible); + } + return 0 /* False */; } + result &= related; } } - if (isArrayType(type) || isTupleType(type)) { - for (var _b = 0, _c = type.typeArguments; _b < _c.length; _b++) { - var t = _c[_b]; - if (reportWideningErrorsInType(t)) { - errorReported = true; + var sourceMax = getNumNonRestParameters(source); + var targetMax = getNumNonRestParameters(target); + var checkCount = getNumParametersToCheckForSignatureRelatability(source, sourceMax, target, targetMax); + var sourceParams = source.parameters; + var targetParams = target.parameters; + for (var i = 0; i < checkCount; i++) { + var s = i < sourceMax ? getTypeOfParameter(sourceParams[i]) : getRestTypeOfSignature(source); + var t = i < targetMax ? getTypeOfParameter(targetParams[i]) : getRestTypeOfSignature(target); + var related = compareTypes(s, t, /*reportErrors*/ false) || compareTypes(t, s, reportErrors); + if (!related) { + if (reportErrors) { + errorReporter(ts.Diagnostics.Types_of_parameters_0_and_1_are_incompatible, sourceParams[i < sourceMax ? i : sourceMax].name, targetParams[i < targetMax ? i : targetMax].name); } + return 0 /* False */; } + result &= related; } - if (type.flags & 8388608 /* ObjectLiteral */) { - for (var _d = 0, _e = getPropertiesOfObjectType(type); _d < _e.length; _d++) { - var p = _e[_d]; - var t = getTypeOfSymbol(p); - if (t.flags & 33554432 /* ContainsWideningType */) { - if (!reportWideningErrorsInType(t)) { - error(p.valueDeclaration, ts.Diagnostics.Object_literal_s_property_0_implicitly_has_an_1_type, p.name, typeToString(getWidenedType(t))); + if (!ignoreReturnTypes) { + var targetReturnType = getReturnTypeOfSignature(target); + if (targetReturnType === voidType) { + return result; + } + var sourceReturnType = getReturnTypeOfSignature(source); + // The following block preserves behavior forbidding boolean returning functions from being assignable to type guard returning functions + if (target.typePredicate) { + if (source.typePredicate) { + result &= compareTypePredicateRelatedTo(source.typePredicate, target.typePredicate, reportErrors, errorReporter, compareTypes); + } + else if (ts.isIdentifierTypePredicate(target.typePredicate)) { + if (reportErrors) { + errorReporter(ts.Diagnostics.Signature_0_must_have_a_type_predicate, signatureToString(source)); } - errorReported = true; + return 0 /* False */; } } + else { + result &= compareTypes(sourceReturnType, targetReturnType, reportErrors); + } } - return errorReported; + return result; } - function reportImplicitAnyError(declaration, type) { - var typeAsString = typeToString(getWidenedType(type)); - var diagnostic; - switch (declaration.kind) { - case 145 /* PropertyDeclaration */: - case 144 /* PropertySignature */: - diagnostic = ts.Diagnostics.Member_0_implicitly_has_an_1_type; - break; - case 142 /* Parameter */: - diagnostic = declaration.dotDotDotToken ? - ts.Diagnostics.Rest_parameter_0_implicitly_has_an_any_type : - ts.Diagnostics.Parameter_0_implicitly_has_an_1_type; - break; - case 169 /* BindingElement */: - diagnostic = ts.Diagnostics.Binding_element_0_implicitly_has_an_1_type; - break; - case 220 /* FunctionDeclaration */: - case 147 /* MethodDeclaration */: - case 146 /* MethodSignature */: - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - case 179 /* FunctionExpression */: - case 180 /* ArrowFunction */: - if (!declaration.name) { - error(declaration, ts.Diagnostics.Function_expression_which_lacks_return_type_annotation_implicitly_has_an_0_return_type, typeAsString); - return; - } - diagnostic = ts.Diagnostics._0_which_lacks_return_type_annotation_implicitly_has_an_1_return_type; - break; - default: - diagnostic = ts.Diagnostics.Variable_0_implicitly_has_an_1_type; + function compareTypePredicateRelatedTo(source, target, reportErrors, errorReporter, compareTypes) { + if (source.kind !== target.kind) { + if (reportErrors) { + errorReporter(ts.Diagnostics.A_this_based_type_guard_is_not_compatible_with_a_parameter_based_type_guard); + errorReporter(ts.Diagnostics.Type_predicate_0_is_not_assignable_to_1, typePredicateToString(source), typePredicateToString(target)); + } + return 0 /* False */; } - error(declaration, diagnostic, ts.declarationNameToString(declaration.name), typeAsString); - } - function reportErrorsFromWidening(declaration, type) { - if (produceDiagnostics && compilerOptions.noImplicitAny && type.flags & 33554432 /* ContainsWideningType */) { - // Report implicit any error within type if possible, otherwise report error on declaration - if (!reportWideningErrorsInType(type)) { - reportImplicitAnyError(declaration, type); + if (source.kind === 1 /* Identifier */) { + var sourceIdentifierPredicate = source; + var targetIdentifierPredicate = target; + if (sourceIdentifierPredicate.parameterIndex !== targetIdentifierPredicate.parameterIndex) { + if (reportErrors) { + errorReporter(ts.Diagnostics.Parameter_0_is_not_in_the_same_position_as_parameter_1, sourceIdentifierPredicate.parameterName, targetIdentifierPredicate.parameterName); + errorReporter(ts.Diagnostics.Type_predicate_0_is_not_assignable_to_1, typePredicateToString(source), typePredicateToString(target)); + } + return 0 /* False */; } } - } - function forEachMatchingParameterType(source, target, callback) { - var sourceMax = source.parameters.length; - var targetMax = target.parameters.length; - var count; - if (source.hasRestParameter && target.hasRestParameter) { - count = Math.max(sourceMax, targetMax); + var related = compareTypes(source.type, target.type, reportErrors); + if (related === 0 /* False */ && reportErrors) { + errorReporter(ts.Diagnostics.Type_predicate_0_is_not_assignable_to_1, typePredicateToString(source), typePredicateToString(target)); } - else if (source.hasRestParameter) { - count = targetMax; + return related; + } + function isImplementationCompatibleWithOverload(implementation, overload) { + var erasedSource = getErasedSignature(implementation); + var erasedTarget = getErasedSignature(overload); + // First see if the return types are compatible in either direction. + var sourceReturnType = getReturnTypeOfSignature(erasedSource); + var targetReturnType = getReturnTypeOfSignature(erasedTarget); + if (targetReturnType === voidType + || isTypeRelatedTo(targetReturnType, sourceReturnType, assignableRelation) + || isTypeRelatedTo(sourceReturnType, targetReturnType, assignableRelation)) { + return isSignatureAssignableTo(erasedSource, erasedTarget, /*ignoreReturnTypes*/ true); } - else if (target.hasRestParameter) { - count = sourceMax; + return false; + } + function getNumNonRestParameters(signature) { + var numParams = signature.parameters.length; + return signature.hasRestParameter ? + numParams - 1 : + numParams; + } + function getNumParametersToCheckForSignatureRelatability(source, sourceNonRestParamCount, target, targetNonRestParamCount) { + if (source.hasRestParameter === target.hasRestParameter) { + if (source.hasRestParameter) { + // If both have rest parameters, get the max and add 1 to + // compensate for the rest parameter. + return Math.max(sourceNonRestParamCount, targetNonRestParamCount) + 1; + } + else { + return Math.min(sourceNonRestParamCount, targetNonRestParamCount); + } } else { - count = Math.min(sourceMax, targetMax); - } - for (var i = 0; i < count; i++) { - callback(getTypeAtPosition(source, i), getTypeAtPosition(target, i)); + // Return the count for whichever signature doesn't have rest parameters. + return source.hasRestParameter ? + targetNonRestParamCount : + sourceNonRestParamCount; } } - function createInferenceContext(signature, inferUnionTypes) { - var inferences = ts.map(signature.typeParameters, createTypeInferencesObject); - return { - signature: signature, - inferUnionTypes: inferUnionTypes, - inferences: inferences, - inferredTypes: new Array(signature.typeParameters.length) - }; - } - function createTypeInferencesObject() { - return { - primary: undefined, - secondary: undefined, - topLevel: true, - isFixed: false - }; - } - // Return true if the given type could possibly reference a type parameter for which - // we perform type inference (i.e. a type parameter of a generic function). We cache - // results for union and intersection types for performance reasons. - function couldContainTypeParameters(type) { - return !!(type.flags & 16384 /* TypeParameter */ || - type.flags & 131072 /* Reference */ && ts.forEach(type.typeArguments, couldContainTypeParameters) || - type.flags & 2097152 /* Anonymous */ && type.symbol && type.symbol.flags & (8192 /* Method */ | 2048 /* TypeLiteral */ | 32 /* Class */) || - type.flags & 1572864 /* UnionOrIntersection */ && couldUnionOrIntersectionContainTypeParameters(type)); - } - function couldUnionOrIntersectionContainTypeParameters(type) { - if (type.couldContainTypeParameters === undefined) { - type.couldContainTypeParameters = ts.forEach(type.types, couldContainTypeParameters); + function isEnumTypeRelatedTo(source, target, errorReporter) { + if (source === target) { + return true; } - return type.couldContainTypeParameters; - } - function isTypeParameterAtTopLevel(type, typeParameter) { - return type === typeParameter || type.flags & 1572864 /* UnionOrIntersection */ && ts.forEach(type.types, function (t) { return isTypeParameterAtTopLevel(t, typeParameter); }); - } - function inferTypes(context, originalSource, originalTarget) { - var typeParameters = context.signature.typeParameters; - var sourceStack; - var targetStack; - var depth = 0; - var inferiority = 0; - var visited = ts.createMap(); - inferFromTypes(originalSource, originalTarget); - function isInProcess(source, target) { - for (var i = 0; i < depth; i++) { - if (source === sourceStack[i] && target === targetStack[i]) { - return true; - } - } - return false; + var id = source.id + "," + target.id; + if (enumRelation[id] !== undefined) { + return enumRelation[id]; } - function inferFromTypes(source, target) { - if (!couldContainTypeParameters(target)) { - return; - } - if (source.flags & 524288 /* Union */ && target.flags & 524288 /* Union */ && !(source.flags & 16 /* Enum */ && target.flags & 16 /* Enum */) || - source.flags & 1048576 /* Intersection */ && target.flags & 1048576 /* Intersection */) { - // Source and target are both unions or both intersections. If source and target - // are the same type, just relate each constituent type to itself. - if (source === target) { - for (var _i = 0, _a = source.types; _i < _a.length; _i++) { - var t = _a[_i]; - inferFromTypes(t, t); + if (source.symbol.name !== target.symbol.name || + !(source.symbol.flags & 256 /* RegularEnum */) || !(target.symbol.flags & 256 /* RegularEnum */) || + (source.flags & 524288 /* Union */) !== (target.flags & 524288 /* Union */)) { + return enumRelation[id] = false; + } + var targetEnumType = getTypeOfSymbol(target.symbol); + for (var _i = 0, _a = getPropertiesOfType(getTypeOfSymbol(source.symbol)); _i < _a.length; _i++) { + var property = _a[_i]; + if (property.flags & 8 /* EnumMember */) { + var targetProperty = getPropertyOfType(targetEnumType, property.name); + if (!targetProperty || !(targetProperty.flags & 8 /* EnumMember */)) { + if (errorReporter) { + errorReporter(ts.Diagnostics.Property_0_is_missing_in_type_1, property.name, typeToString(target, /*enclosingDeclaration*/ undefined, 128 /* UseFullyQualifiedType */)); } - return; + return enumRelation[id] = false; } - // Find each source constituent type that has an identically matching target constituent - // type, and for each such type infer from the type to itself. When inferring from a - // type to itself we effectively find all type parameter occurrences within that type - // and infer themselves as their type arguments. We have special handling for numeric - // and string literals because the number and string types are not represented as unions - // of all their possible values. - var matchingTypes = void 0; - for (var _b = 0, _c = source.types; _b < _c.length; _b++) { - var t = _c[_b]; - if (typeIdenticalToSomeType(t, target.types)) { - (matchingTypes || (matchingTypes = [])).push(t); - inferFromTypes(t, t); - } - else if (t.flags & (64 /* NumberLiteral */ | 32 /* StringLiteral */)) { - var b = getBaseTypeOfLiteralType(t); - if (typeIdenticalToSomeType(b, target.types)) { - (matchingTypes || (matchingTypes = [])).push(t, b); - } + } + } + return enumRelation[id] = true; + } + function isSimpleTypeRelatedTo(source, target, relation, errorReporter) { + if (target.flags & 8192 /* Never */) + return false; + if (target.flags & 1 /* Any */ || source.flags & 8192 /* Never */) + return true; + if (source.flags & 34 /* StringLike */ && target.flags & 2 /* String */) + return true; + if (source.flags & 340 /* NumberLike */ && target.flags & 4 /* Number */) + return true; + if (source.flags & 136 /* BooleanLike */ && target.flags & 8 /* Boolean */) + return true; + if (source.flags & 256 /* EnumLiteral */ && target.flags & 16 /* Enum */ && source.baseType === target) + return true; + if (source.flags & 16 /* Enum */ && target.flags & 16 /* Enum */ && isEnumTypeRelatedTo(source, target, errorReporter)) + return true; + if (source.flags & 2048 /* Undefined */ && (!strictNullChecks || target.flags & (2048 /* Undefined */ | 1024 /* Void */))) + return true; + if (source.flags & 4096 /* Null */ && (!strictNullChecks || target.flags & 4096 /* Null */)) + return true; + if (relation === assignableRelation || relation === comparableRelation) { + if (source.flags & 1 /* Any */) + return true; + if ((source.flags & 4 /* Number */ | source.flags & 64 /* NumberLiteral */) && target.flags & 272 /* EnumLike */) + return true; + if (source.flags & 256 /* EnumLiteral */ && + target.flags & 256 /* EnumLiteral */ && + source.text === target.text && + isEnumTypeRelatedTo(source.baseType, target.baseType, errorReporter)) { + return true; + } + if (source.flags & 256 /* EnumLiteral */ && + target.flags & 16 /* Enum */ && + isEnumTypeRelatedTo(target, source.baseType, errorReporter)) { + return true; + } + } + return false; + } + function isTypeRelatedTo(source, target, relation) { + if (source.flags & 96 /* StringOrNumberLiteral */ && source.flags & 16777216 /* FreshLiteral */) { + source = source.regularType; + } + if (target.flags & 96 /* StringOrNumberLiteral */ && target.flags & 16777216 /* FreshLiteral */) { + target = target.regularType; + } + if (source === target || relation !== identityRelation && isSimpleTypeRelatedTo(source, target, relation)) { + return true; + } + if (source.flags & 2588672 /* ObjectType */ && target.flags & 2588672 /* ObjectType */) { + var id = relation !== identityRelation || source.id < target.id ? source.id + "," + target.id : target.id + "," + source.id; + var related = relation[id]; + if (related !== undefined) { + return related === 1 /* Succeeded */; + } + } + if (source.flags & 4177920 /* StructuredOrTypeParameter */ || target.flags & 4177920 /* StructuredOrTypeParameter */) { + return checkTypeRelatedTo(source, target, relation, undefined, undefined, undefined); + } + return false; + } + /** + * Checks if 'source' is related to 'target' (e.g.: is a assignable to). + * @param source The left-hand-side of the relation. + * @param target The right-hand-side of the relation. + * @param relation The relation considered. One of 'identityRelation', 'subtypeRelation', 'assignableRelation', or 'comparableRelation'. + * Used as both to determine which checks are performed and as a cache of previously computed results. + * @param errorNode The suggested node upon which all errors will be reported, if defined. This may or may not be the actual node used. + * @param headMessage If the error chain should be prepended by a head message, then headMessage will be used. + * @param containingMessageChain A chain of errors to prepend any new errors found. + */ + function checkTypeRelatedTo(source, target, relation, errorNode, headMessage, containingMessageChain) { + var errorInfo; + var sourceStack; + var targetStack; + var maybeStack; + var expandingFlags; + var depth = 0; + var overflow = false; + ts.Debug.assert(relation !== identityRelation || !errorNode, "no error reporting in identity checking"); + var result = isRelatedTo(source, target, /*reportErrors*/ !!errorNode, headMessage); + if (overflow) { + error(errorNode, ts.Diagnostics.Excessive_stack_depth_comparing_types_0_and_1, typeToString(source), typeToString(target)); + } + else if (errorInfo) { + if (containingMessageChain) { + errorInfo = ts.concatenateDiagnosticMessageChains(containingMessageChain, errorInfo); + } + diagnostics.add(ts.createDiagnosticForNodeFromMessageChain(errorNode, errorInfo)); + } + return result !== 0 /* False */; + function reportError(message, arg0, arg1, arg2) { + ts.Debug.assert(!!errorNode); + errorInfo = ts.chainDiagnosticMessages(errorInfo, message, arg0, arg1, arg2); + } + function reportRelationError(message, source, target) { + var sourceType = typeToString(source); + var targetType = typeToString(target); + if (sourceType === targetType) { + sourceType = typeToString(source, /*enclosingDeclaration*/ undefined, 128 /* UseFullyQualifiedType */); + targetType = typeToString(target, /*enclosingDeclaration*/ undefined, 128 /* UseFullyQualifiedType */); + } + if (!message) { + message = relation === comparableRelation ? + ts.Diagnostics.Type_0_is_not_comparable_to_type_1 : + ts.Diagnostics.Type_0_is_not_assignable_to_type_1; + } + reportError(message, sourceType, targetType); + } + function tryElaborateErrorsForPrimitivesAndObjects(source, target) { + var sourceType = typeToString(source); + var targetType = typeToString(target); + if ((globalStringType === source && stringType === target) || + (globalNumberType === source && numberType === target) || + (globalBooleanType === source && booleanType === target) || + (getGlobalESSymbolType() === source && esSymbolType === target)) { + reportError(ts.Diagnostics._0_is_a_primitive_but_1_is_a_wrapper_object_Prefer_using_0_when_possible, targetType, sourceType); + } + } + // Compare two types and return + // Ternary.True if they are related with no assumptions, + // Ternary.Maybe if they are related with assumptions of other relationships, or + // Ternary.False if they are not related. + function isRelatedTo(source, target, reportErrors, headMessage) { + var result; + if (source.flags & 96 /* StringOrNumberLiteral */ && source.flags & 16777216 /* FreshLiteral */) { + source = source.regularType; + } + if (target.flags & 96 /* StringOrNumberLiteral */ && target.flags & 16777216 /* FreshLiteral */) { + target = target.regularType; + } + // both types are the same - covers 'they are the same primitive type or both are Any' or the same type parameter cases + if (source === target) + return -1 /* True */; + if (relation === identityRelation) { + return isIdenticalTo(source, target); + } + if (isSimpleTypeRelatedTo(source, target, relation, reportErrors ? reportError : undefined)) + return -1 /* True */; + if (source.flags & 8388608 /* ObjectLiteral */ && source.flags & 16777216 /* FreshLiteral */) { + if (hasExcessProperties(source, target, reportErrors)) { + if (reportErrors) { + reportRelationError(headMessage, source, target); } + return 0 /* False */; } - // Next, to improve the quality of inferences, reduce the source and target types by - // removing the identically matched constituents. For example, when inferring from - // 'string | string[]' to 'string | T' we reduce the types to 'string[]' and 'T'. - if (matchingTypes) { - source = removeTypesFromUnionOrIntersection(source, matchingTypes); - target = removeTypesFromUnionOrIntersection(target, matchingTypes); + // Above we check for excess properties with respect to the entire target type. When union + // and intersection types are further deconstructed on the target side, we don't want to + // make the check again (as it might fail for a partial target type). Therefore we obtain + // the regular source type and proceed with that. + if (target.flags & 1572864 /* UnionOrIntersection */) { + source = getRegularTypeOfObjectLiteral(source); } } - if (target.flags & 16384 /* TypeParameter */) { - // If target is a type parameter, make an inference, unless the source type contains - // the anyFunctionType (the wildcard type that's used to avoid contextually typing functions). - // Because the anyFunctionType is internal, it should not be exposed to the user by adding - // it as an inference candidate. Hopefully, a better candidate will come along that does - // not contain anyFunctionType when we come back to this argument for its second round - // of inference. - if (source.flags & 134217728 /* ContainsAnyFunctionType */) { - return; + var saveErrorInfo = errorInfo; + // Note that these checks are specifically ordered to produce correct results. + if (source.flags & 524288 /* Union */) { + if (relation === comparableRelation) { + result = someTypeRelatedToType(source, target, reportErrors && !(source.flags & 8190 /* Primitive */)); } - for (var i = 0; i < typeParameters.length; i++) { - if (target === typeParameters[i]) { - var inferences = context.inferences[i]; - if (!inferences.isFixed) { - // Any inferences that are made to a type parameter in a union type are inferior - // to inferences made to a flat (non-union) type. This is because if we infer to - // T | string[], we really don't know if we should be inferring to T or not (because - // the correct constituent on the target side could be string[]). Therefore, we put - // such inferior inferences into a secondary bucket, and only use them if the primary - // bucket is empty. - var candidates = inferiority ? - inferences.secondary || (inferences.secondary = []) : - inferences.primary || (inferences.primary = []); - if (!ts.contains(candidates, source)) { - candidates.push(source); - } - if (!isTypeParameterAtTopLevel(originalTarget, target)) { - inferences.topLevel = false; - } - } - return; - } + else { + result = eachTypeRelatedToType(source, target, reportErrors && !(source.flags & 8190 /* Primitive */)); + } + if (result) { + return result; } } - else if (source.flags & 131072 /* Reference */ && target.flags & 131072 /* Reference */ && source.target === target.target) { - // If source and target are references to the same generic type, infer from type arguments - var sourceTypes = source.typeArguments || emptyArray; - var targetTypes = target.typeArguments || emptyArray; - var count = sourceTypes.length < targetTypes.length ? sourceTypes.length : targetTypes.length; - for (var i = 0; i < count; i++) { - inferFromTypes(sourceTypes[i], targetTypes[i]); + else if (target.flags & 1048576 /* Intersection */) { + result = typeRelatedToEachType(source, target, reportErrors); + if (result) { + return result; } } - else if (target.flags & 1572864 /* UnionOrIntersection */) { - var targetTypes = target.types; - var typeParameterCount = 0; - var typeParameter = void 0; - // First infer to each type in union or intersection that isn't a type parameter - for (var _d = 0, targetTypes_2 = targetTypes; _d < targetTypes_2.length; _d++) { - var t = targetTypes_2[_d]; - if (t.flags & 16384 /* TypeParameter */ && ts.contains(typeParameters, t)) { - typeParameter = t; - typeParameterCount++; - } - else { - inferFromTypes(source, t); + else { + // It is necessary to try these "some" checks on both sides because there may be nested "each" checks + // on either side that need to be prioritized. For example, A | B = (A | B) & (C | D) or + // A & B = (A & B) | (C & D). + if (source.flags & 1048576 /* Intersection */) { + // Check to see if any constituents of the intersection are immediately related to the target. + // + // Don't report errors though. Checking whether a constituent is related to the source is not actually + // useful and leads to some confusing error messages. Instead it is better to let the below checks + // take care of this, or to not elaborate at all. For instance, + // + // - For an object type (such as 'C = A & B'), users are usually more interested in structural errors. + // + // - For a union type (such as '(A | B) = (C & D)'), it's better to hold onto the whole intersection + // than to report that 'D' is not assignable to 'A' or 'B'. + // + // - For a primitive type or type parameter (such as 'number = A & B') there is no point in + // breaking the intersection apart. + if (result = someTypeRelatedToType(source, target, /*reportErrors*/ false)) { + return result; } } - // Next, if target containings a single naked type parameter, make a secondary inference to that type - // parameter. This gives meaningful results for union types in co-variant positions and intersection - // types in contra-variant positions (such as callback parameters). - if (typeParameterCount === 1) { - inferiority++; - inferFromTypes(source, typeParameter); - inferiority--; + if (target.flags & 524288 /* Union */) { + if (result = typeRelatedToSomeType(source, target, reportErrors && !(source.flags & 8190 /* Primitive */) && !(target.flags & 8190 /* Primitive */))) { + return result; + } } } - else if (source.flags & 1572864 /* UnionOrIntersection */) { - // Source is a union or intersection type, infer from each constituent type - var sourceTypes = source.types; - for (var _e = 0, sourceTypes_3 = sourceTypes; _e < sourceTypes_3.length; _e++) { - var sourceType = sourceTypes_3[_e]; - inferFromTypes(sourceType, target); + if (source.flags & 16384 /* TypeParameter */) { + var constraint = getConstraintOfTypeParameter(source); + if (!constraint || constraint.flags & 1 /* Any */) { + constraint = emptyObjectType; + } + // The constraint may need to be further instantiated with its 'this' type. + constraint = getTypeWithThisArgument(constraint, source); + // Report constraint errors only if the constraint is not the empty object type + var reportConstraintErrors = reportErrors && constraint !== emptyObjectType; + if (result = isRelatedTo(constraint, target, reportConstraintErrors)) { + errorInfo = saveErrorInfo; + return result; } } else { - source = getApparentType(source); - if (source.flags & 2588672 /* ObjectType */) { - if (isInProcess(source, target)) { - return; + if (source.flags & 131072 /* Reference */ && target.flags & 131072 /* Reference */ && source.target === target.target) { + // We have type references to same target type, see if relationship holds for all type arguments + if (result = typeArgumentsRelatedTo(source, target, reportErrors)) { + return result; } - if (isDeeplyNestedGeneric(source, sourceStack, depth) && isDeeplyNestedGeneric(target, targetStack, depth)) { - return; + } + // Even if relationship doesn't hold for unions, intersections, or generic type references, + // it may hold in a structural comparison. + var apparentSource = getApparentType(source); + // In a check of the form X = A & B, we will have previously checked if A relates to X or B relates + // to X. Failing both of those we want to check if the aggregation of A and B's members structurally + // relates to X. Thus, we include intersection types on the source side here. + if (apparentSource.flags & (2588672 /* ObjectType */ | 1048576 /* Intersection */) && target.flags & 2588672 /* ObjectType */) { + // Report structural errors only if we haven't reported any errors yet + var reportStructuralErrors = reportErrors && errorInfo === saveErrorInfo && !(source.flags & 8190 /* Primitive */); + if (result = objectTypeRelatedTo(apparentSource, source, target, reportStructuralErrors)) { + errorInfo = saveErrorInfo; + return result; } - var key = source.id + "," + target.id; - if (visited[key]) { - return; + } + } + if (reportErrors) { + if (source.flags & 2588672 /* ObjectType */ && target.flags & 8190 /* Primitive */) { + tryElaborateErrorsForPrimitivesAndObjects(source, target); + } + else if (source.symbol && source.flags & 2588672 /* ObjectType */ && globalObjectType === source) { + reportError(ts.Diagnostics.The_Object_type_is_assignable_to_very_few_other_types_Did_you_mean_to_use_the_any_type_instead); + } + reportRelationError(headMessage, source, target); + } + return 0 /* False */; + } + function isIdenticalTo(source, target) { + var result; + if (source.flags & 2588672 /* ObjectType */ && target.flags & 2588672 /* ObjectType */) { + if (source.flags & 131072 /* Reference */ && target.flags & 131072 /* Reference */ && source.target === target.target) { + // We have type references to same target type, see if all type arguments are identical + if (result = typeArgumentsRelatedTo(source, target, /*reportErrors*/ false)) { + return result; } - visited[key] = true; - if (depth === 0) { - sourceStack = []; - targetStack = []; + } + return objectTypeRelatedTo(source, source, target, /*reportErrors*/ false); + } + if (source.flags & 524288 /* Union */ && target.flags & 524288 /* Union */ || + source.flags & 1048576 /* Intersection */ && target.flags & 1048576 /* Intersection */) { + if (result = eachTypeRelatedToSomeType(source, target, /*reportErrors*/ false)) { + if (result &= eachTypeRelatedToSomeType(target, source, /*reportErrors*/ false)) { + return result; } - sourceStack[depth] = source; - targetStack[depth] = target; - depth++; - inferFromProperties(source, target); - inferFromSignatures(source, target, 0 /* Call */); - inferFromSignatures(source, target, 1 /* Construct */); - inferFromIndexTypes(source, target); - depth--; } } + return 0 /* False */; } - function inferFromProperties(source, target) { - var properties = getPropertiesOfObjectType(target); - for (var _i = 0, properties_3 = properties; _i < properties_3.length; _i++) { - var targetProp = properties_3[_i]; - var sourceProp = getPropertyOfObjectType(source, targetProp.name); - if (sourceProp) { - inferFromTypes(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp)); + // Check if a property with the given name is known anywhere in the given type. In an object type, a property + // is considered known if the object type is empty and the check is for assignability, if the object type has + // index signatures, or if the property is actually declared in the object type. In a union or intersection + // type, a property is considered known if it is known in any constituent type. + function isKnownProperty(type, name) { + if (type.flags & 2588672 /* ObjectType */) { + var resolved = resolveStructuredTypeMembers(type); + if ((relation === assignableRelation || relation === comparableRelation) && (type === globalObjectType || isEmptyObjectType(resolved)) || + resolved.stringIndexInfo || + (resolved.numberIndexInfo && isNumericLiteralName(name)) || + getPropertyOfType(type, name)) { + return true; } } - } - function inferFromSignatures(source, target, kind) { - var sourceSignatures = getSignaturesOfType(source, kind); - var targetSignatures = getSignaturesOfType(target, kind); - var sourceLen = sourceSignatures.length; - var targetLen = targetSignatures.length; - var len = sourceLen < targetLen ? sourceLen : targetLen; - for (var i = 0; i < len; i++) { - inferFromSignature(getErasedSignature(sourceSignatures[sourceLen - len + i]), getErasedSignature(targetSignatures[targetLen - len + i])); + else if (type.flags & 1572864 /* UnionOrIntersection */) { + for (var _i = 0, _a = type.types; _i < _a.length; _i++) { + var t = _a[_i]; + if (isKnownProperty(t, name)) { + return true; + } + } } + return false; } - function inferFromParameterTypes(source, target) { - return inferFromTypes(source, target); + function isEmptyObjectType(t) { + return t.properties.length === 0 && + t.callSignatures.length === 0 && + t.constructSignatures.length === 0 && + !t.stringIndexInfo && + !t.numberIndexInfo; } - function inferFromSignature(source, target) { - forEachMatchingParameterType(source, target, inferFromParameterTypes); - if (source.typePredicate && target.typePredicate && source.typePredicate.kind === target.typePredicate.kind) { - inferFromTypes(source.typePredicate.type, target.typePredicate.type); + function hasExcessProperties(source, target, reportErrors) { + if (maybeTypeOfKind(target, 2588672 /* ObjectType */) && + (!(target.flags & 2588672 /* ObjectType */) || !target.isObjectLiteralPatternWithComputedProperties)) { + for (var _i = 0, _a = getPropertiesOfObjectType(source); _i < _a.length; _i++) { + var prop = _a[_i]; + if (!isKnownProperty(target, prop.name)) { + if (reportErrors) { + // We know *exactly* where things went wrong when comparing the types. + // Use this property as the error node as this will be more helpful in + // reasoning about what went wrong. + ts.Debug.assert(!!errorNode); + errorNode = prop.valueDeclaration; + reportError(ts.Diagnostics.Object_literal_may_only_specify_known_properties_and_0_does_not_exist_in_type_1, symbolToString(prop), typeToString(target)); + } + return true; + } + } } - else { - inferFromTypes(getReturnTypeOfSignature(source), getReturnTypeOfSignature(target)); + return false; + } + function eachTypeRelatedToSomeType(source, target, reportErrors) { + var result = -1 /* True */; + var sourceTypes = source.types; + for (var _i = 0, sourceTypes_1 = sourceTypes; _i < sourceTypes_1.length; _i++) { + var sourceType = sourceTypes_1[_i]; + var related = typeRelatedToSomeType(sourceType, target, /*reportErrors*/ false); + if (!related) { + return 0 /* False */; + } + result &= related; } + return result; } - function inferFromIndexTypes(source, target) { - var targetStringIndexType = getIndexTypeOfType(target, 0 /* String */); - if (targetStringIndexType) { - var sourceIndexType = getIndexTypeOfType(source, 0 /* String */) || - getImplicitIndexTypeOfType(source, 0 /* String */); - if (sourceIndexType) { - inferFromTypes(sourceIndexType, targetStringIndexType); + function typeRelatedToSomeType(source, target, reportErrors) { + var targetTypes = target.types; + if (target.flags & 524288 /* Union */ && containsType(targetTypes, source)) { + return -1 /* True */; + } + var len = targetTypes.length; + for (var i = 0; i < len; i++) { + var related = isRelatedTo(source, targetTypes[i], reportErrors && i === len - 1); + if (related) { + return related; } } - var targetNumberIndexType = getIndexTypeOfType(target, 1 /* Number */); - if (targetNumberIndexType) { - var sourceIndexType = getIndexTypeOfType(source, 1 /* Number */) || - getIndexTypeOfType(source, 0 /* String */) || - getImplicitIndexTypeOfType(source, 1 /* Number */); - if (sourceIndexType) { - inferFromTypes(sourceIndexType, targetNumberIndexType); + return 0 /* False */; + } + function typeRelatedToEachType(source, target, reportErrors) { + var result = -1 /* True */; + var targetTypes = target.types; + for (var _i = 0, targetTypes_1 = targetTypes; _i < targetTypes_1.length; _i++) { + var targetType = targetTypes_1[_i]; + var related = isRelatedTo(source, targetType, reportErrors); + if (!related) { + return 0 /* False */; } + result &= related; } + return result; } - } - function typeIdenticalToSomeType(type, types) { - for (var _i = 0, types_10 = types; _i < types_10.length; _i++) { - var t = types_10[_i]; - if (isTypeIdenticalTo(t, type)) { - return true; + function someTypeRelatedToType(source, target, reportErrors) { + var sourceTypes = source.types; + if (source.flags & 524288 /* Union */ && containsType(sourceTypes, target)) { + return -1 /* True */; + } + var len = sourceTypes.length; + for (var i = 0; i < len; i++) { + var related = isRelatedTo(sourceTypes[i], target, reportErrors && i === len - 1); + if (related) { + return related; + } } + return 0 /* False */; } - return false; - } - /** - * Return a new union or intersection type computed by removing a given set of types - * from a given union or intersection type. - */ - function removeTypesFromUnionOrIntersection(type, typesToRemove) { - var reducedTypes = []; - for (var _i = 0, _a = type.types; _i < _a.length; _i++) { - var t = _a[_i]; - if (!typeIdenticalToSomeType(t, typesToRemove)) { - reducedTypes.push(t); + function eachTypeRelatedToType(source, target, reportErrors) { + var result = -1 /* True */; + var sourceTypes = source.types; + for (var _i = 0, sourceTypes_2 = sourceTypes; _i < sourceTypes_2.length; _i++) { + var sourceType = sourceTypes_2[_i]; + var related = isRelatedTo(sourceType, target, reportErrors); + if (!related) { + return 0 /* False */; + } + result &= related; } + return result; } - return type.flags & 524288 /* Union */ ? getUnionType(reducedTypes) : getIntersectionType(reducedTypes); - } - function getInferenceCandidates(context, index) { - var inferences = context.inferences[index]; - return inferences.primary || inferences.secondary || emptyArray; - } - function hasPrimitiveConstraint(type) { - var constraint = getConstraintOfTypeParameter(type); - return constraint && maybeTypeOfKind(constraint, 8190 /* Primitive */); - } - function getInferredType(context, index) { - var inferredType = context.inferredTypes[index]; - var inferenceSucceeded; - if (!inferredType) { - var inferences = getInferenceCandidates(context, index); - if (inferences.length) { - // We widen inferred literal types if - // all inferences were made to top-level ocurrences of the type parameter, and - // the type parameter has no constraint or its constraint includes no primitive or literal types, and - // the type parameter was fixed during inference or does not occur at top-level in the return type. - var signature = context.signature; - var widenLiteralTypes = context.inferences[index].topLevel && - !hasPrimitiveConstraint(signature.typeParameters[index]) && - (context.inferences[index].isFixed || !isTypeParameterAtTopLevel(getReturnTypeOfSignature(signature), signature.typeParameters[index])); - var baseInferences = widenLiteralTypes ? ts.map(inferences, getWidenedLiteralType) : inferences; - // Infer widened union or supertype, or the unknown type for no common supertype - var unionOrSuperType = context.inferUnionTypes ? getUnionType(baseInferences, /*subtypeReduction*/ true) : getCommonSupertype(baseInferences); - inferredType = unionOrSuperType ? getWidenedType(unionOrSuperType) : unknownType; - inferenceSucceeded = !!unionOrSuperType; + function typeArgumentsRelatedTo(source, target, reportErrors) { + var sources = source.typeArguments || emptyArray; + var targets = target.typeArguments || emptyArray; + if (sources.length !== targets.length && relation === identityRelation) { + return 0 /* False */; + } + var length = sources.length <= targets.length ? sources.length : targets.length; + var result = -1 /* True */; + for (var i = 0; i < length; i++) { + var related = isRelatedTo(sources[i], targets[i], reportErrors); + if (!related) { + return 0 /* False */; + } + result &= related; + } + return result; + } + // Determine if two object types are related by structure. First, check if the result is already available in the global cache. + // Second, check if we have already started a comparison of the given two types in which case we assume the result to be true. + // Third, check if both types are part of deeply nested chains of generic type instantiations and if so assume the types are + // equal and infinitely expanding. Fourth, if we have reached a depth of 100 nested comparisons, assume we have runaway recursion + // and issue an error. Otherwise, actually compare the structure of the two types. + function objectTypeRelatedTo(source, originalSource, target, reportErrors) { + if (overflow) { + return 0 /* False */; + } + var id = relation !== identityRelation || source.id < target.id ? source.id + "," + target.id : target.id + "," + source.id; + var related = relation[id]; + if (related !== undefined) { + if (reportErrors && related === 2 /* Failed */) { + // We are elaborating errors and the cached result is an unreported failure. Record the result as a reported + // failure and continue computing the relation such that errors get reported. + relation[id] = 3 /* FailedAndReported */; + } + else { + return related === 1 /* Succeeded */ ? -1 /* True */ : 0 /* False */; + } + } + if (depth > 0) { + for (var i = 0; i < depth; i++) { + // If source and target are already being compared, consider them related with assumptions + if (maybeStack[i][id]) { + return 1 /* Maybe */; + } + } + if (depth === 100) { + overflow = true; + return 0 /* False */; + } } else { - // Infer the empty object type when no inferences were made. It is important to remember that - // in this case, inference still succeeds, meaning there is no error for not having inference - // candidates. An inference error only occurs when there are *conflicting* candidates, i.e. - // candidates with no common supertype. - inferredType = emptyObjectType; - inferenceSucceeded = true; + sourceStack = []; + targetStack = []; + maybeStack = []; + expandingFlags = 0; } - context.inferredTypes[index] = inferredType; - // Only do the constraint check if inference succeeded (to prevent cascading errors) - if (inferenceSucceeded) { - var constraint = getConstraintOfTypeParameter(context.signature.typeParameters[index]); - if (constraint) { - var instantiatedConstraint = instantiateType(constraint, getInferenceMapper(context)); - if (!isTypeAssignableTo(inferredType, getTypeWithThisArgument(instantiatedConstraint, inferredType))) { - context.inferredTypes[index] = inferredType = instantiatedConstraint; + sourceStack[depth] = source; + targetStack[depth] = target; + maybeStack[depth] = ts.createMap(); + maybeStack[depth][id] = 1 /* Succeeded */; + depth++; + var saveExpandingFlags = expandingFlags; + if (!(expandingFlags & 1) && isDeeplyNestedGeneric(source, sourceStack, depth)) + expandingFlags |= 1; + if (!(expandingFlags & 2) && isDeeplyNestedGeneric(target, targetStack, depth)) + expandingFlags |= 2; + var result; + if (expandingFlags === 3) { + result = 1 /* Maybe */; + } + else { + result = propertiesRelatedTo(source, target, reportErrors); + if (result) { + result &= signaturesRelatedTo(source, target, 0 /* Call */, reportErrors); + if (result) { + result &= signaturesRelatedTo(source, target, 1 /* Construct */, reportErrors); + if (result) { + result &= indexTypesRelatedTo(source, originalSource, target, 0 /* String */, reportErrors); + if (result) { + result &= indexTypesRelatedTo(source, originalSource, target, 1 /* Number */, reportErrors); + } + } } } } - else if (context.failedTypeParameterIndex === undefined || context.failedTypeParameterIndex > index) { - // If inference failed, it is necessary to record the index of the failed type parameter (the one we are on). - // It might be that inference has already failed on a later type parameter on a previous call to inferTypeArguments. - // So if this failure is on preceding type parameter, this type parameter is the new failure index. - context.failedTypeParameterIndex = index; + expandingFlags = saveExpandingFlags; + depth--; + if (result) { + var maybeCache = maybeStack[depth]; + // If result is definitely true, copy assumptions to global cache, else copy to next level up + var destinationCache = (result === -1 /* True */ || depth === 0) ? relation : maybeStack[depth - 1]; + ts.copyProperties(maybeCache, destinationCache); } + else { + // A false result goes straight into global cache (when something is false under assumptions it + // will also be false without assumptions) + relation[id] = reportErrors ? 3 /* FailedAndReported */ : 2 /* Failed */; + } + return result; } - return inferredType; - } - function getInferredTypes(context) { - for (var i = 0; i < context.inferredTypes.length; i++) { - getInferredType(context, i); - } - return context.inferredTypes; - } - // EXPRESSION TYPE CHECKING - function getResolvedSymbol(node) { - var links = getNodeLinks(node); - if (!links.resolvedSymbol) { - links.resolvedSymbol = !ts.nodeIsMissing(node) && resolveName(node, node.text, 107455 /* Value */ | 1048576 /* ExportValue */, ts.Diagnostics.Cannot_find_name_0, node) || unknownSymbol; - } - return links.resolvedSymbol; - } - function isInTypeQuery(node) { - // TypeScript 1.0 spec (April 2014): 3.6.3 - // A type query consists of the keyword typeof followed by an expression. - // The expression is restricted to a single identifier or a sequence of identifiers separated by periods - while (node) { - switch (node.kind) { - case 158 /* TypeQuery */: - return true; - case 69 /* Identifier */: - case 139 /* QualifiedName */: - node = node.parent; - continue; - default: - return false; + function propertiesRelatedTo(source, target, reportErrors) { + if (relation === identityRelation) { + return propertiesIdenticalTo(source, target); + } + var result = -1 /* True */; + var properties = getPropertiesOfObjectType(target); + var requireOptionalProperties = relation === subtypeRelation && !(source.flags & 8388608 /* ObjectLiteral */); + for (var _i = 0, properties_2 = properties; _i < properties_2.length; _i++) { + var targetProp = properties_2[_i]; + var sourceProp = getPropertyOfType(source, targetProp.name); + if (sourceProp !== targetProp) { + if (!sourceProp) { + if (!(targetProp.flags & 536870912 /* Optional */) || requireOptionalProperties) { + if (reportErrors) { + reportError(ts.Diagnostics.Property_0_is_missing_in_type_1, symbolToString(targetProp), typeToString(source)); + } + return 0 /* False */; + } + } + else if (!(targetProp.flags & 134217728 /* Prototype */)) { + var sourcePropFlags = getDeclarationModifierFlagsFromSymbol(sourceProp); + var targetPropFlags = getDeclarationModifierFlagsFromSymbol(targetProp); + if (sourcePropFlags & 8 /* Private */ || targetPropFlags & 8 /* Private */) { + if (sourceProp.valueDeclaration !== targetProp.valueDeclaration) { + if (reportErrors) { + if (sourcePropFlags & 8 /* Private */ && targetPropFlags & 8 /* Private */) { + reportError(ts.Diagnostics.Types_have_separate_declarations_of_a_private_property_0, symbolToString(targetProp)); + } + else { + reportError(ts.Diagnostics.Property_0_is_private_in_type_1_but_not_in_type_2, symbolToString(targetProp), typeToString(sourcePropFlags & 8 /* Private */ ? source : target), typeToString(sourcePropFlags & 8 /* Private */ ? target : source)); + } + } + return 0 /* False */; + } + } + else if (targetPropFlags & 16 /* Protected */) { + var sourceDeclaredInClass = sourceProp.parent && sourceProp.parent.flags & 32 /* Class */; + var sourceClass = sourceDeclaredInClass ? getDeclaredTypeOfSymbol(getParentOfSymbol(sourceProp)) : undefined; + var targetClass = getDeclaredTypeOfSymbol(getParentOfSymbol(targetProp)); + if (!sourceClass || !hasBaseType(sourceClass, targetClass)) { + if (reportErrors) { + reportError(ts.Diagnostics.Property_0_is_protected_but_type_1_is_not_a_class_derived_from_2, symbolToString(targetProp), typeToString(sourceClass || source), typeToString(targetClass)); + } + return 0 /* False */; + } + } + else if (sourcePropFlags & 16 /* Protected */) { + if (reportErrors) { + reportError(ts.Diagnostics.Property_0_is_protected_in_type_1_but_public_in_type_2, symbolToString(targetProp), typeToString(source), typeToString(target)); + } + return 0 /* False */; + } + var related = isRelatedTo(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp), reportErrors); + if (!related) { + if (reportErrors) { + reportError(ts.Diagnostics.Types_of_property_0_are_incompatible, symbolToString(targetProp)); + } + return 0 /* False */; + } + result &= related; + if (sourceProp.flags & 536870912 /* Optional */ && !(targetProp.flags & 536870912 /* Optional */)) { + // TypeScript 1.0 spec (April 2014): 3.8.3 + // S is a subtype of a type T, and T is a supertype of S if ... + // S' and T are object types and, for each member M in T.. + // M is a property and S' contains a property N where + // if M is a required property, N is also a required property + // (M - property in T) + // (N - property in S) + if (reportErrors) { + reportError(ts.Diagnostics.Property_0_is_optional_in_type_1_but_required_in_type_2, symbolToString(targetProp), typeToString(source), typeToString(target)); + } + return 0 /* False */; + } + } + } } + return result; } - ts.Debug.fail("should not get here"); - } - // Return the flow cache key for a "dotted name" (i.e. a sequence of identifiers - // separated by dots). The key consists of the id of the symbol referenced by the - // leftmost identifier followed by zero or more property names separated by dots. - // The result is undefined if the reference isn't a dotted name. - function getFlowCacheKey(node) { - if (node.kind === 69 /* Identifier */) { - var symbol = getResolvedSymbol(node); - return symbol !== unknownSymbol ? "" + getSymbolId(symbol) : undefined; + function propertiesIdenticalTo(source, target) { + if (!(source.flags & 2588672 /* ObjectType */ && target.flags & 2588672 /* ObjectType */)) { + return 0 /* False */; + } + var sourceProperties = getPropertiesOfObjectType(source); + var targetProperties = getPropertiesOfObjectType(target); + if (sourceProperties.length !== targetProperties.length) { + return 0 /* False */; + } + var result = -1 /* True */; + for (var _i = 0, sourceProperties_1 = sourceProperties; _i < sourceProperties_1.length; _i++) { + var sourceProp = sourceProperties_1[_i]; + var targetProp = getPropertyOfObjectType(target, sourceProp.name); + if (!targetProp) { + return 0 /* False */; + } + var related = compareProperties(sourceProp, targetProp, isRelatedTo); + if (!related) { + return 0 /* False */; + } + result &= related; + } + return result; } - if (node.kind === 97 /* ThisKeyword */) { - return "0"; + function signaturesRelatedTo(source, target, kind, reportErrors) { + if (relation === identityRelation) { + return signaturesIdenticalTo(source, target, kind); + } + if (target === anyFunctionType || source === anyFunctionType) { + return -1 /* True */; + } + var sourceSignatures = getSignaturesOfType(source, kind); + var targetSignatures = getSignaturesOfType(target, kind); + if (kind === 1 /* Construct */ && sourceSignatures.length && targetSignatures.length) { + if (isAbstractConstructorType(source) && !isAbstractConstructorType(target)) { + // An abstract constructor type is not assignable to a non-abstract constructor type + // as it would otherwise be possible to new an abstract class. Note that the assignability + // check we perform for an extends clause excludes construct signatures from the target, + // so this check never proceeds. + if (reportErrors) { + reportError(ts.Diagnostics.Cannot_assign_an_abstract_constructor_type_to_a_non_abstract_constructor_type); + } + return 0 /* False */; + } + if (!constructorVisibilitiesAreCompatible(sourceSignatures[0], targetSignatures[0], reportErrors)) { + return 0 /* False */; + } + } + var result = -1 /* True */; + var saveErrorInfo = errorInfo; + outer: for (var _i = 0, targetSignatures_1 = targetSignatures; _i < targetSignatures_1.length; _i++) { + var t = targetSignatures_1[_i]; + // Only elaborate errors from the first failure + var shouldElaborateErrors = reportErrors; + for (var _a = 0, sourceSignatures_1 = sourceSignatures; _a < sourceSignatures_1.length; _a++) { + var s = sourceSignatures_1[_a]; + var related = signatureRelatedTo(s, t, shouldElaborateErrors); + if (related) { + result &= related; + errorInfo = saveErrorInfo; + continue outer; + } + shouldElaborateErrors = false; + } + if (shouldElaborateErrors) { + reportError(ts.Diagnostics.Type_0_provides_no_match_for_the_signature_1, typeToString(source), signatureToString(t, /*enclosingDeclaration*/ undefined, /*flags*/ undefined, kind)); + } + return 0 /* False */; + } + return result; } - if (node.kind === 172 /* PropertyAccessExpression */) { - var key = getFlowCacheKey(node.expression); - return key && key + "." + node.name.text; + /** + * See signatureAssignableTo, compareSignaturesIdentical + */ + function signatureRelatedTo(source, target, reportErrors) { + return compareSignaturesRelated(source, target, /*ignoreReturnTypes*/ false, reportErrors, reportError, isRelatedTo); } - return undefined; - } - function getLeftmostIdentifierOrThis(node) { - switch (node.kind) { - case 69 /* Identifier */: - case 97 /* ThisKeyword */: - return node; - case 172 /* PropertyAccessExpression */: - return getLeftmostIdentifierOrThis(node.expression); + function signaturesIdenticalTo(source, target, kind) { + var sourceSignatures = getSignaturesOfType(source, kind); + var targetSignatures = getSignaturesOfType(target, kind); + if (sourceSignatures.length !== targetSignatures.length) { + return 0 /* False */; + } + var result = -1 /* True */; + for (var i = 0, len = sourceSignatures.length; i < len; i++) { + var related = compareSignaturesIdentical(sourceSignatures[i], targetSignatures[i], /*partialMatch*/ false, /*ignoreThisTypes*/ false, /*ignoreReturnTypes*/ false, isRelatedTo); + if (!related) { + return 0 /* False */; + } + result &= related; + } + return result; } - return undefined; - } - function isMatchingReference(source, target) { - switch (source.kind) { - case 69 /* Identifier */: - return target.kind === 69 /* Identifier */ && getResolvedSymbol(source) === getResolvedSymbol(target) || - (target.kind === 218 /* VariableDeclaration */ || target.kind === 169 /* BindingElement */) && - getExportSymbolOfValueSymbolIfExported(getResolvedSymbol(source)) === getSymbolOfNode(target); - case 97 /* ThisKeyword */: - return target.kind === 97 /* ThisKeyword */; - case 172 /* PropertyAccessExpression */: - return target.kind === 172 /* PropertyAccessExpression */ && - source.name.text === target.name.text && - isMatchingReference(source.expression, target.expression); + function eachPropertyRelatedTo(source, target, kind, reportErrors) { + var result = -1 /* True */; + for (var _i = 0, _a = getPropertiesOfObjectType(source); _i < _a.length; _i++) { + var prop = _a[_i]; + if (kind === 0 /* String */ || isNumericLiteralName(prop.name)) { + var related = isRelatedTo(getTypeOfSymbol(prop), target, reportErrors); + if (!related) { + if (reportErrors) { + reportError(ts.Diagnostics.Property_0_is_incompatible_with_index_signature, symbolToString(prop)); + } + return 0 /* False */; + } + result &= related; + } + } + return result; } - return false; - } - function containsMatchingReference(source, target) { - while (source.kind === 172 /* PropertyAccessExpression */) { - source = source.expression; - if (isMatchingReference(source, target)) { - return true; + function indexInfoRelatedTo(sourceInfo, targetInfo, reportErrors) { + var related = isRelatedTo(sourceInfo.type, targetInfo.type, reportErrors); + if (!related && reportErrors) { + reportError(ts.Diagnostics.Index_signatures_are_incompatible); } + return related; } - return false; - } - // Return true if target is a property access xxx.yyy, source is a property access xxx.zzz, the declared - // type of xxx is a union type, and yyy is a property that is possibly a discriminant. We consider a property - // a possible discriminant if its type differs in the constituents of containing union type, and if every - // choice is a unit type or a union of unit types. - function containsMatchingReferenceDiscriminant(source, target) { - return target.kind === 172 /* PropertyAccessExpression */ && - containsMatchingReference(source, target.expression) && - isDiscriminantProperty(getDeclaredTypeOfReference(target.expression), target.name.text); - } - function getDeclaredTypeOfReference(expr) { - if (expr.kind === 69 /* Identifier */) { - return getTypeOfSymbol(getResolvedSymbol(expr)); + function indexTypesRelatedTo(source, originalSource, target, kind, reportErrors) { + if (relation === identityRelation) { + return indexTypesIdenticalTo(source, target, kind); + } + var targetInfo = getIndexInfoOfType(target, kind); + if (!targetInfo || ((targetInfo.type.flags & 1 /* Any */) && !(originalSource.flags & 8190 /* Primitive */))) { + // Index signature of type any permits assignment from everything but primitives + return -1 /* True */; + } + var sourceInfo = getIndexInfoOfType(source, kind) || + kind === 1 /* Number */ && getIndexInfoOfType(source, 0 /* String */); + if (sourceInfo) { + return indexInfoRelatedTo(sourceInfo, targetInfo, reportErrors); + } + if (isObjectLiteralType(source)) { + var related = -1 /* True */; + if (kind === 0 /* String */) { + var sourceNumberInfo = getIndexInfoOfType(source, 1 /* Number */); + if (sourceNumberInfo) { + related = indexInfoRelatedTo(sourceNumberInfo, targetInfo, reportErrors); + } + } + if (related) { + related &= eachPropertyRelatedTo(source, targetInfo.type, kind, reportErrors); + } + return related; + } + if (reportErrors) { + reportError(ts.Diagnostics.Index_signature_is_missing_in_type_0, typeToString(source)); + } + return 0 /* False */; } - if (expr.kind === 172 /* PropertyAccessExpression */) { - var type = getDeclaredTypeOfReference(expr.expression); - return type && getTypeOfPropertyOfType(type, expr.name.text); + function indexTypesIdenticalTo(source, target, indexKind) { + var targetInfo = getIndexInfoOfType(target, indexKind); + var sourceInfo = getIndexInfoOfType(source, indexKind); + if (!sourceInfo && !targetInfo) { + return -1 /* True */; + } + if (sourceInfo && targetInfo && sourceInfo.isReadonly === targetInfo.isReadonly) { + return isRelatedTo(sourceInfo.type, targetInfo.type); + } + return 0 /* False */; } - return undefined; - } - function isDiscriminantProperty(type, name) { - if (type && type.flags & 524288 /* Union */) { - var prop = getPropertyOfType(type, name); - if (!prop) { - // The type may be a union that includes nullable or primitive types. If filtering - // those out produces a different type, get the property from that type instead. - // Effectively, we're checking if this *could* be a discriminant property once nullable - // and primitive types are removed by other type guards. - var filteredType = getTypeWithFacts(type, 4194304 /* Discriminatable */); - if (filteredType !== type && filteredType.flags & 524288 /* Union */) { - prop = getPropertyOfType(filteredType, name); - } + function constructorVisibilitiesAreCompatible(sourceSignature, targetSignature, reportErrors) { + if (!sourceSignature.declaration || !targetSignature.declaration) { + return true; } - if (prop && prop.flags & 268435456 /* SyntheticProperty */) { - if (prop.isDiscriminantProperty === undefined) { - prop.isDiscriminantProperty = !prop.hasCommonType && - isLiteralType(getTypeOfSymbol(prop)); - } - return prop.isDiscriminantProperty; + var sourceAccessibility = ts.getModifierFlags(sourceSignature.declaration) & 24 /* NonPublicAccessibilityModifier */; + var targetAccessibility = ts.getModifierFlags(targetSignature.declaration) & 24 /* NonPublicAccessibilityModifier */; + // A public, protected and private signature is assignable to a private signature. + if (targetAccessibility === 8 /* Private */) { + return true; + } + // A public and protected signature is assignable to a protected signature. + if (targetAccessibility === 16 /* Protected */ && sourceAccessibility !== 8 /* Private */) { + return true; + } + // Only a public signature is assignable to public signature. + if (targetAccessibility !== 16 /* Protected */ && !sourceAccessibility) { + return true; + } + if (reportErrors) { + reportError(ts.Diagnostics.Cannot_assign_a_0_constructor_type_to_a_1_constructor_type, visibilityToString(sourceAccessibility), visibilityToString(targetAccessibility)); } + return false; } - return false; } - function isOrContainsMatchingReference(source, target) { - return isMatchingReference(source, target) || containsMatchingReference(source, target); - } - function hasMatchingArgument(callExpression, reference) { - if (callExpression.arguments) { - for (var _i = 0, _a = callExpression.arguments; _i < _a.length; _i++) { - var argument = _a[_i]; - if (isOrContainsMatchingReference(reference, argument)) { + // Return true if the given type is the constructor type for an abstract class + function isAbstractConstructorType(type) { + if (type.flags & 2097152 /* Anonymous */) { + var symbol = type.symbol; + if (symbol && symbol.flags & 32 /* Class */) { + var declaration = getClassLikeDeclarationOfSymbol(symbol); + if (declaration && ts.getModifierFlags(declaration) & 128 /* Abstract */) { return true; } } } - if (callExpression.expression.kind === 172 /* PropertyAccessExpression */ && - isOrContainsMatchingReference(reference, callExpression.expression.expression)) { - return true; - } return false; } - function getFlowNodeId(flow) { - if (!flow.id) { - flow.id = nextFlowId; - nextFlowId++; - } - return flow.id; - } - function typeMaybeAssignableTo(source, target) { - if (!(source.flags & 524288 /* Union */)) { - return isTypeAssignableTo(source, target); - } - for (var _i = 0, _a = source.types; _i < _a.length; _i++) { - var t = _a[_i]; - if (isTypeAssignableTo(t, target)) { - return true; + // Return true if the given type is part of a deeply nested chain of generic instantiations. We consider this to be the case + // when structural type comparisons have been started for 10 or more instantiations of the same generic type. It is possible, + // though highly unlikely, for this test to be true in a situation where a chain of instantiations is not infinitely expanding. + // Effectively, we will generate a false positive when two types are structurally equal to at least 10 levels, but unequal at + // some level beyond that. + function isDeeplyNestedGeneric(type, stack, depth) { + // We track type references (created by createTypeReference) and instantiated types (created by instantiateType) + if (type.flags & (131072 /* Reference */ | 4194304 /* Instantiated */) && depth >= 5) { + var symbol = type.symbol; + var count = 0; + for (var i = 0; i < depth; i++) { + var t = stack[i]; + if (t.flags & (131072 /* Reference */ | 4194304 /* Instantiated */) && t.symbol === symbol) { + count++; + if (count >= 5) + return true; + } } } return false; } - // Remove those constituent types of declaredType to which no constituent type of assignedType is assignable. - // For example, when a variable of type number | string | boolean is assigned a value of type number | boolean, - // we remove type string. - function getAssignmentReducedType(declaredType, assignedType) { - if (declaredType !== assignedType) { - if (assignedType.flags & 8192 /* Never */) { - return assignedType; + function isPropertyIdenticalTo(sourceProp, targetProp) { + return compareProperties(sourceProp, targetProp, compareTypesIdentical) !== 0 /* False */; + } + function compareProperties(sourceProp, targetProp, compareTypes) { + // Two members are considered identical when + // - they are public properties with identical names, optionality, and types, + // - they are private or protected properties originating in the same declaration and having identical types + if (sourceProp === targetProp) { + return -1 /* True */; + } + var sourcePropAccessibility = getDeclarationModifierFlagsFromSymbol(sourceProp) & 24 /* NonPublicAccessibilityModifier */; + var targetPropAccessibility = getDeclarationModifierFlagsFromSymbol(targetProp) & 24 /* NonPublicAccessibilityModifier */; + if (sourcePropAccessibility !== targetPropAccessibility) { + return 0 /* False */; + } + if (sourcePropAccessibility) { + if (getTargetSymbol(sourceProp) !== getTargetSymbol(targetProp)) { + return 0 /* False */; } - var reducedType = filterType(declaredType, function (t) { return typeMaybeAssignableTo(assignedType, t); }); - if (!(reducedType.flags & 8192 /* Never */)) { - return reducedType; + } + else { + if ((sourceProp.flags & 536870912 /* Optional */) !== (targetProp.flags & 536870912 /* Optional */)) { + return 0 /* False */; } } - return declaredType; - } - function getTypeFactsOfTypes(types) { - var result = 0 /* None */; - for (var _i = 0, types_11 = types; _i < types_11.length; _i++) { - var t = types_11[_i]; - result |= getTypeFacts(t); + if (isReadonlySymbol(sourceProp) !== isReadonlySymbol(targetProp)) { + return 0 /* False */; } - return result; - } - function isFunctionObjectType(type) { - // We do a quick check for a "bind" property before performing the more expensive subtype - // check. This gives us a quicker out in the common case where an object type is not a function. - var resolved = resolveStructuredTypeMembers(type); - return !!(resolved.callSignatures.length || resolved.constructSignatures.length || - resolved.members["bind"] && isTypeSubtypeOf(type, globalFunctionType)); + return compareTypes(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp)); } - function getTypeFacts(type) { - var flags = type.flags; - if (flags & 2 /* String */) { - return strictNullChecks ? 4079361 /* StringStrictFacts */ : 4194049 /* StringFacts */; + function isMatchingSignature(source, target, partialMatch) { + // A source signature matches a target signature if the two signatures have the same number of required, + // optional, and rest parameters. + if (source.parameters.length === target.parameters.length && + source.minArgumentCount === target.minArgumentCount && + source.hasRestParameter === target.hasRestParameter) { + return true; } - if (flags & 32 /* StringLiteral */) { - return strictNullChecks ? - type.text === "" ? 3030785 /* EmptyStringStrictFacts */ : 1982209 /* NonEmptyStringStrictFacts */ : - type.text === "" ? 3145473 /* EmptyStringFacts */ : 4194049 /* NonEmptyStringFacts */; + // A source signature partially matches a target signature if the target signature has no fewer required + // parameters and no more overall parameters than the source signature (where a signature with a rest + // parameter is always considered to have more overall parameters than one without). + var sourceRestCount = source.hasRestParameter ? 1 : 0; + var targetRestCount = target.hasRestParameter ? 1 : 0; + if (partialMatch && source.minArgumentCount <= target.minArgumentCount && (sourceRestCount > targetRestCount || + sourceRestCount === targetRestCount && source.parameters.length >= target.parameters.length)) { + return true; } - if (flags & (4 /* Number */ | 16 /* Enum */)) { - return strictNullChecks ? 4079234 /* NumberStrictFacts */ : 4193922 /* NumberFacts */; + return false; + } + /** + * See signatureRelatedTo, compareSignaturesIdentical + */ + function compareSignaturesIdentical(source, target, partialMatch, ignoreThisTypes, ignoreReturnTypes, compareTypes) { + // TODO (drosen): De-duplicate code between related functions. + if (source === target) { + return -1 /* True */; } - if (flags & (64 /* NumberLiteral */ | 256 /* EnumLiteral */)) { - var isZero = type.text === "0"; - return strictNullChecks ? - isZero ? 3030658 /* ZeroStrictFacts */ : 1982082 /* NonZeroStrictFacts */ : - isZero ? 3145346 /* ZeroFacts */ : 4193922 /* NonZeroFacts */; + if (!(isMatchingSignature(source, target, partialMatch))) { + return 0 /* False */; } - if (flags & 8 /* Boolean */) { - return strictNullChecks ? 4078980 /* BooleanStrictFacts */ : 4193668 /* BooleanFacts */; + // Check that the two signatures have the same number of type parameters. We might consider + // also checking that any type parameter constraints match, but that would require instantiating + // the constraints with a common set of type arguments to get relatable entities in places where + // type parameters occur in the constraints. The complexity of doing that doesn't seem worthwhile, + // particularly as we're comparing erased versions of the signatures below. + if ((source.typeParameters ? source.typeParameters.length : 0) !== (target.typeParameters ? target.typeParameters.length : 0)) { + return 0 /* False */; } - if (flags & 136 /* BooleanLike */) { - return strictNullChecks ? - type === falseType ? 3030404 /* FalseStrictFacts */ : 1981828 /* TrueStrictFacts */ : - type === falseType ? 3145092 /* FalseFacts */ : 4193668 /* TrueFacts */; + // Spec 1.0 Section 3.8.3 & 3.8.4: + // M and N (the signatures) are instantiated using type Any as the type argument for all type parameters declared by M and N + source = getErasedSignature(source); + target = getErasedSignature(target); + var result = -1 /* True */; + if (!ignoreThisTypes) { + var sourceThisType = getThisTypeOfSignature(source); + if (sourceThisType) { + var targetThisType = getThisTypeOfSignature(target); + if (targetThisType) { + var related = compareTypes(sourceThisType, targetThisType); + if (!related) { + return 0 /* False */; + } + result &= related; + } + } } - if (flags & 2588672 /* ObjectType */) { - return isFunctionObjectType(type) ? - strictNullChecks ? 6164448 /* FunctionStrictFacts */ : 8376288 /* FunctionFacts */ : - strictNullChecks ? 6166480 /* ObjectStrictFacts */ : 8378320 /* ObjectFacts */; + var targetLen = target.parameters.length; + for (var i = 0; i < targetLen; i++) { + var s = isRestParameterIndex(source, i) ? getRestTypeOfSignature(source) : getTypeOfParameter(source.parameters[i]); + var t = isRestParameterIndex(target, i) ? getRestTypeOfSignature(target) : getTypeOfParameter(target.parameters[i]); + var related = compareTypes(s, t); + if (!related) { + return 0 /* False */; + } + result &= related; } - if (flags & (1024 /* Void */ | 2048 /* Undefined */)) { - return 2457472 /* UndefinedFacts */; + if (!ignoreReturnTypes) { + result &= compareTypes(getReturnTypeOfSignature(source), getReturnTypeOfSignature(target)); } - if (flags & 4096 /* Null */) { - return 2340752 /* NullFacts */; + return result; + } + function isRestParameterIndex(signature, parameterIndex) { + return signature.hasRestParameter && parameterIndex >= signature.parameters.length - 1; + } + function isSupertypeOfEach(candidate, types) { + for (var _i = 0, types_7 = types; _i < types_7.length; _i++) { + var t = types_7[_i]; + if (candidate !== t && !isTypeSubtypeOf(t, candidate)) + return false; } - if (flags & 512 /* ESSymbol */) { - return strictNullChecks ? 1981320 /* SymbolStrictFacts */ : 4193160 /* SymbolFacts */; + return true; + } + function literalTypesWithSameBaseType(types) { + var commonBaseType; + for (var _i = 0, types_8 = types; _i < types_8.length; _i++) { + var t = types_8[_i]; + var baseType = getBaseTypeOfLiteralType(t); + if (!commonBaseType) { + commonBaseType = baseType; + } + if (baseType === t || baseType !== commonBaseType) { + return false; + } } - if (flags & 16384 /* TypeParameter */) { - var constraint = getConstraintOfTypeParameter(type); - return getTypeFacts(constraint || emptyObjectType); + return true; + } + // When the candidate types are all literal types with the same base type, the common + // supertype is a union of those literal types. Otherwise, the common supertype is the + // first type that is a supertype of each of the other types. + function getSupertypeOrUnion(types) { + return literalTypesWithSameBaseType(types) ? getUnionType(types) : ts.forEach(types, function (t) { return isSupertypeOfEach(t, types) ? t : undefined; }); + } + function getCommonSupertype(types) { + if (!strictNullChecks) { + return getSupertypeOrUnion(types); } - if (flags & 1572864 /* UnionOrIntersection */) { - return getTypeFactsOfTypes(type.types); + var primaryTypes = ts.filter(types, function (t) { return !(t.flags & 6144 /* Nullable */); }); + if (!primaryTypes.length) { + return getUnionType(types, /*subtypeReduction*/ true); } - return 8388607 /* All */; - } - function getTypeWithFacts(type, include) { - return filterType(type, function (t) { return (getTypeFacts(t) & include) !== 0; }); + var supertype = getSupertypeOrUnion(primaryTypes); + return supertype && includeFalsyTypes(supertype, getFalsyFlagsOfTypes(types) & 6144 /* Nullable */); } - function getTypeWithDefault(type, defaultExpression) { - if (defaultExpression) { - var defaultType = checkExpression(defaultExpression); - return getUnionType([getTypeWithFacts(type, 131072 /* NEUndefined */), defaultType]); + function reportNoCommonSupertypeError(types, errorLocation, errorMessageChainHead) { + // The downfallType/bestSupertypeDownfallType is the first type that caused a particular candidate + // to not be the common supertype. So if it weren't for this one downfallType (and possibly others), + // the type in question could have been the common supertype. + var bestSupertype; + var bestSupertypeDownfallType; + var bestSupertypeScore = 0; + for (var i = 0; i < types.length; i++) { + var score = 0; + var downfallType = undefined; + for (var j = 0; j < types.length; j++) { + if (isTypeSubtypeOf(types[j], types[i])) { + score++; + } + else if (!downfallType) { + downfallType = types[j]; + } + } + ts.Debug.assert(!!downfallType, "If there is no common supertype, each type should have a downfallType"); + if (score > bestSupertypeScore) { + bestSupertype = types[i]; + bestSupertypeDownfallType = downfallType; + bestSupertypeScore = score; + } + // types.length - 1 is the maximum score, given that getCommonSupertype returned false + if (bestSupertypeScore === types.length - 1) { + break; + } } - return type; + // In the following errors, the {1} slot is before the {0} slot because checkTypeSubtypeOf supplies the + // subtype as the first argument to the error + checkTypeSubtypeOf(bestSupertypeDownfallType, bestSupertype, errorLocation, ts.Diagnostics.Type_argument_candidate_1_is_not_a_valid_type_argument_because_it_is_not_a_supertype_of_candidate_0, errorMessageChainHead); } - function getTypeOfDestructuredProperty(type, name) { - var text = getTextOfPropertyName(name); - return getTypeOfPropertyOfType(type, text) || - isNumericLiteralName(text) && getIndexTypeOfType(type, 1 /* Number */) || - getIndexTypeOfType(type, 0 /* String */) || - unknownType; + function isArrayType(type) { + return type.flags & 131072 /* Reference */ && type.target === globalArrayType; } - function getTypeOfDestructuredArrayElement(type, index) { - return isTupleLikeType(type) && getTypeOfPropertyOfType(type, "" + index) || - checkIteratedTypeOrElementType(type, /*errorNode*/ undefined, /*allowStringInput*/ false) || - unknownType; + function isArrayLikeType(type) { + // A type is array-like if it is a reference to the global Array or global ReadonlyArray type, + // or if it is not the undefined or null type and if it is assignable to ReadonlyArray + return type.flags & 131072 /* Reference */ && (type.target === globalArrayType || type.target === globalReadonlyArrayType) || + !(type.flags & 6144 /* Nullable */) && isTypeAssignableTo(type, anyReadonlyArrayType); } - function getTypeOfDestructuredSpreadElement(type) { - return createArrayType(checkIteratedTypeOrElementType(type, /*errorNode*/ undefined, /*allowStringInput*/ false) || unknownType); + function isTupleLikeType(type) { + return !!getPropertyOfType(type, "0"); } - function getAssignedTypeOfBinaryExpression(node) { - return node.parent.kind === 170 /* ArrayLiteralExpression */ || node.parent.kind === 253 /* PropertyAssignment */ ? - getTypeWithDefault(getAssignedType(node), node.right) : - checkExpression(node.right); + function isUnitType(type) { + return (type.flags & (480 /* Literal */ | 2048 /* Undefined */ | 4096 /* Null */)) !== 0; } - function getAssignedTypeOfArrayLiteralElement(node, element) { - return getTypeOfDestructuredArrayElement(getAssignedType(node), ts.indexOf(node.elements, element)); + function isLiteralType(type) { + return type.flags & 8 /* Boolean */ ? true : + type.flags & 524288 /* Union */ ? type.flags & 16 /* Enum */ ? true : !ts.forEach(type.types, function (t) { return !isUnitType(t); }) : + isUnitType(type); } - function getAssignedTypeOfSpreadElement(node) { - return getTypeOfDestructuredSpreadElement(getAssignedType(node.parent)); + function getBaseTypeOfLiteralType(type) { + return type.flags & 32 /* StringLiteral */ ? stringType : + type.flags & 64 /* NumberLiteral */ ? numberType : + type.flags & 128 /* BooleanLiteral */ ? booleanType : + type.flags & 256 /* EnumLiteral */ ? type.baseType : + type.flags & 524288 /* Union */ && !(type.flags & 16 /* Enum */) ? getUnionType(ts.map(type.types, getBaseTypeOfLiteralType)) : + type; } - function getAssignedTypeOfPropertyAssignment(node) { - return getTypeOfDestructuredProperty(getAssignedType(node.parent), node.name); + function getWidenedLiteralType(type) { + return type.flags & 32 /* StringLiteral */ && type.flags & 16777216 /* FreshLiteral */ ? stringType : + type.flags & 64 /* NumberLiteral */ && type.flags & 16777216 /* FreshLiteral */ ? numberType : + type.flags & 128 /* BooleanLiteral */ ? booleanType : + type.flags & 256 /* EnumLiteral */ ? type.baseType : + type.flags & 524288 /* Union */ && !(type.flags & 16 /* Enum */) ? getUnionType(ts.map(type.types, getWidenedLiteralType)) : + type; } - function getAssignedTypeOfShorthandPropertyAssignment(node) { - return getTypeWithDefault(getAssignedTypeOfPropertyAssignment(node), node.objectAssignmentInitializer); + /** + * Check if a Type was written as a tuple type literal. + * Prefer using isTupleLikeType() unless the use of `elementTypes` is required. + */ + function isTupleType(type) { + return !!(type.flags & 131072 /* Reference */ && type.target.flags & 262144 /* Tuple */); } - function getAssignedType(node) { - var parent = node.parent; - switch (parent.kind) { - case 207 /* ForInStatement */: - return stringType; - case 208 /* ForOfStatement */: - return checkRightHandSideOfForOf(parent.expression) || unknownType; - case 187 /* BinaryExpression */: - return getAssignedTypeOfBinaryExpression(parent); - case 181 /* DeleteExpression */: - return undefinedType; - case 170 /* ArrayLiteralExpression */: - return getAssignedTypeOfArrayLiteralElement(parent, node); - case 191 /* SpreadElementExpression */: - return getAssignedTypeOfSpreadElement(parent); - case 253 /* PropertyAssignment */: - return getAssignedTypeOfPropertyAssignment(parent); - case 254 /* ShorthandPropertyAssignment */: - return getAssignedTypeOfShorthandPropertyAssignment(parent); + function getFalsyFlagsOfTypes(types) { + var result = 0; + for (var _i = 0, types_9 = types; _i < types_9.length; _i++) { + var t = types_9[_i]; + result |= getFalsyFlags(t); } - return unknownType; - } - function getInitialTypeOfBindingElement(node) { - var pattern = node.parent; - var parentType = getInitialType(pattern.parent); - var type = pattern.kind === 167 /* ObjectBindingPattern */ ? - getTypeOfDestructuredProperty(parentType, node.propertyName || node.name) : - !node.dotDotDotToken ? - getTypeOfDestructuredArrayElement(parentType, ts.indexOf(pattern.elements, node)) : - getTypeOfDestructuredSpreadElement(parentType); - return getTypeWithDefault(type, node.initializer); + return result; } - function getTypeOfInitializer(node) { - // Return the cached type if one is available. If the type of the variable was inferred - // from its initializer, we'll already have cached the type. Otherwise we compute it now - // without caching such that transient types are reflected. - var links = getNodeLinks(node); - return links.resolvedType || checkExpression(node); + // Returns the String, Number, Boolean, StringLiteral, NumberLiteral, BooleanLiteral, Void, Undefined, or Null + // flags for the string, number, boolean, "", 0, false, void, undefined, or null types respectively. Returns + // no flags for all other types (including non-falsy literal types). + function getFalsyFlags(type) { + return type.flags & 524288 /* Union */ ? getFalsyFlagsOfTypes(type.types) : + type.flags & 32 /* StringLiteral */ ? type.text === "" ? 32 /* StringLiteral */ : 0 : + type.flags & 64 /* NumberLiteral */ ? type.text === "0" ? 64 /* NumberLiteral */ : 0 : + type.flags & 128 /* BooleanLiteral */ ? type === falseType ? 128 /* BooleanLiteral */ : 0 : + type.flags & 7406 /* PossiblyFalsy */; } - function getInitialTypeOfVariableDeclaration(node) { - if (node.initializer) { - return getTypeOfInitializer(node.initializer); - } - if (node.parent.parent.kind === 207 /* ForInStatement */) { - return stringType; - } - if (node.parent.parent.kind === 208 /* ForOfStatement */) { - return checkRightHandSideOfForOf(node.parent.parent.expression) || unknownType; + function includeFalsyTypes(type, flags) { + if ((getFalsyFlags(type) & flags) === flags) { + return type; } - return unknownType; + var types = [type]; + if (flags & 34 /* StringLike */) + types.push(emptyStringType); + if (flags & 340 /* NumberLike */) + types.push(zeroType); + if (flags & 136 /* BooleanLike */) + types.push(falseType); + if (flags & 1024 /* Void */) + types.push(voidType); + if (flags & 2048 /* Undefined */) + types.push(undefinedType); + if (flags & 4096 /* Null */) + types.push(nullType); + return getUnionType(types, /*subtypeReduction*/ true); } - function getInitialType(node) { - return node.kind === 218 /* VariableDeclaration */ ? - getInitialTypeOfVariableDeclaration(node) : - getInitialTypeOfBindingElement(node); + function removeDefinitelyFalsyTypes(type) { + return getFalsyFlags(type) & 7392 /* DefinitelyFalsy */ ? + filterType(type, function (t) { return !(getFalsyFlags(t) & 7392 /* DefinitelyFalsy */); }) : + type; } - function getInitialOrAssignedType(node) { - return node.kind === 218 /* VariableDeclaration */ || node.kind === 169 /* BindingElement */ ? - getInitialType(node) : - getAssignedType(node); + function getNonNullableType(type) { + return strictNullChecks ? getTypeWithFacts(type, 524288 /* NEUndefinedOrNull */) : type; } - function getReferenceCandidate(node) { - switch (node.kind) { - case 178 /* ParenthesizedExpression */: - return getReferenceCandidate(node.expression); - case 187 /* BinaryExpression */: - switch (node.operatorToken.kind) { - case 56 /* EqualsToken */: - return getReferenceCandidate(node.left); - case 24 /* CommaToken */: - return getReferenceCandidate(node.right); - } + /** + * Return true if type was inferred from an object literal or written as an object type literal + * with no call or construct signatures. + */ + function isObjectLiteralType(type) { + return type.symbol && (type.symbol.flags & (4096 /* ObjectLiteral */ | 2048 /* TypeLiteral */)) !== 0 && + getSignaturesOfType(type, 0 /* Call */).length === 0 && + getSignaturesOfType(type, 1 /* Construct */).length === 0; + } + function createTransientSymbol(source, type) { + var symbol = createSymbol(source.flags | 67108864 /* Transient */, source.name); + symbol.declarations = source.declarations; + symbol.parent = source.parent; + symbol.type = type; + symbol.target = source; + if (source.valueDeclaration) { + symbol.valueDeclaration = source.valueDeclaration; } - return node; + return symbol; } - function getTypeOfSwitchClause(clause) { - if (clause.kind === 249 /* CaseClause */) { - var caseType = getRegularTypeOfLiteralType(checkExpression(clause.expression)); - return isUnitType(caseType) ? caseType : undefined; + function transformTypeOfMembers(type, f) { + var members = ts.createMap(); + for (var _i = 0, _a = getPropertiesOfObjectType(type); _i < _a.length; _i++) { + var property = _a[_i]; + var original = getTypeOfSymbol(property); + var updated = f(original); + members[property.name] = updated === original ? property : createTransientSymbol(property, updated); } - return neverType; + ; + return members; } - function getSwitchClauseTypes(switchStatement) { - var links = getNodeLinks(switchStatement); - if (!links.switchTypes) { - // If all case clauses specify expressions that have unit types, we return an array - // of those unit types. Otherwise we return an empty array. - var types = ts.map(switchStatement.caseBlock.clauses, getTypeOfSwitchClause); - links.switchTypes = !ts.contains(types, undefined) ? types : emptyArray; + /** + * If the the provided object literal is subject to the excess properties check, + * create a new that is exempt. Recursively mark object literal members as exempt. + * Leave signatures alone since they are not subject to the check. + */ + function getRegularTypeOfObjectLiteral(type) { + if (!(type.flags & 8388608 /* ObjectLiteral */ && type.flags & 16777216 /* FreshLiteral */)) { + return type; } - return links.switchTypes; + var regularType = type.regularType; + if (regularType) { + return regularType; + } + var resolved = type; + var members = transformTypeOfMembers(type, getRegularTypeOfObjectLiteral); + var regularNew = createAnonymousType(resolved.symbol, members, resolved.callSignatures, resolved.constructSignatures, resolved.stringIndexInfo, resolved.numberIndexInfo); + regularNew.flags = resolved.flags & ~16777216 /* FreshLiteral */; + type.regularType = regularNew; + return regularNew; } - function eachTypeContainedIn(source, types) { - return source.flags & 524288 /* Union */ ? !ts.forEach(source.types, function (t) { return !ts.contains(types, t); }) : ts.contains(types, source); + function getWidenedTypeOfObjectLiteral(type) { + var members = transformTypeOfMembers(type, function (prop) { + var widened = getWidenedType(prop); + return prop === widened ? prop : widened; + }); + var stringIndexInfo = getIndexInfoOfType(type, 0 /* String */); + var numberIndexInfo = getIndexInfoOfType(type, 1 /* Number */); + return createAnonymousType(type.symbol, members, emptyArray, emptyArray, stringIndexInfo && createIndexInfo(getWidenedType(stringIndexInfo.type), stringIndexInfo.isReadonly), numberIndexInfo && createIndexInfo(getWidenedType(numberIndexInfo.type), numberIndexInfo.isReadonly)); } - function isTypeSubsetOf(source, target) { - return source === target || target.flags & 524288 /* Union */ && isTypeSubsetOfUnion(source, target); + function getWidenedConstituentType(type) { + return type.flags & 6144 /* Nullable */ ? type : getWidenedType(type); } - function isTypeSubsetOfUnion(source, target) { - if (source.flags & 524288 /* Union */) { - for (var _i = 0, _a = source.types; _i < _a.length; _i++) { - var t = _a[_i]; - if (!containsType(target.types, t)) { - return false; - } + function getWidenedType(type) { + if (type.flags & 100663296 /* RequiresWidening */) { + if (type.flags & 6144 /* Nullable */) { + return anyType; + } + if (type.flags & 8388608 /* ObjectLiteral */) { + return getWidenedTypeOfObjectLiteral(type); + } + if (type.flags & 524288 /* Union */) { + return getUnionType(ts.map(type.types, getWidenedConstituentType)); + } + if (isArrayType(type) || isTupleType(type)) { + return createTypeReference(type.target, ts.map(type.typeArguments, getWidenedType)); } - return true; - } - if (source.flags & 256 /* EnumLiteral */ && target.flags & 16 /* Enum */ && source.baseType === target) { - return true; } - return containsType(target.types, source); + return type; } - function filterType(type, f) { - if (type.flags & 524288 /* Union */) { - var types = type.types; - var filtered = ts.filter(types, f); - return filtered === types ? type : getUnionTypeFromSortedList(filtered); + /** + * Reports implicit any errors that occur as a result of widening 'null' and 'undefined' + * to 'any'. A call to reportWideningErrorsInType is normally accompanied by a call to + * getWidenedType. But in some cases getWidenedType is called without reporting errors + * (type argument inference is an example). + * + * The return value indicates whether an error was in fact reported. The particular circumstances + * are on a best effort basis. Currently, if the null or undefined that causes widening is inside + * an object literal property (arbitrarily deeply), this function reports an error. If no error is + * reported, reportImplicitAnyError is a suitable fallback to report a general error. + */ + function reportWideningErrorsInType(type) { + var errorReported = false; + if (type.flags & 524288 /* Union */) { + for (var _i = 0, _a = type.types; _i < _a.length; _i++) { + var t = _a[_i]; + if (reportWideningErrorsInType(t)) { + errorReported = true; + } + } } - return f(type) ? type : neverType; + if (isArrayType(type) || isTupleType(type)) { + for (var _b = 0, _c = type.typeArguments; _b < _c.length; _b++) { + var t = _c[_b]; + if (reportWideningErrorsInType(t)) { + errorReported = true; + } + } + } + if (type.flags & 8388608 /* ObjectLiteral */) { + for (var _d = 0, _e = getPropertiesOfObjectType(type); _d < _e.length; _d++) { + var p = _e[_d]; + var t = getTypeOfSymbol(p); + if (t.flags & 33554432 /* ContainsWideningType */) { + if (!reportWideningErrorsInType(t)) { + error(p.valueDeclaration, ts.Diagnostics.Object_literal_s_property_0_implicitly_has_an_1_type, p.name, typeToString(getWidenedType(t))); + } + errorReported = true; + } + } + } + return errorReported; } - function isIncomplete(flowType) { - return flowType.flags === 0; + function reportImplicitAnyError(declaration, type) { + var typeAsString = typeToString(getWidenedType(type)); + var diagnostic; + switch (declaration.kind) { + case 145 /* PropertyDeclaration */: + case 144 /* PropertySignature */: + diagnostic = ts.Diagnostics.Member_0_implicitly_has_an_1_type; + break; + case 142 /* Parameter */: + diagnostic = declaration.dotDotDotToken ? + ts.Diagnostics.Rest_parameter_0_implicitly_has_an_any_type : + ts.Diagnostics.Parameter_0_implicitly_has_an_1_type; + break; + case 169 /* BindingElement */: + diagnostic = ts.Diagnostics.Binding_element_0_implicitly_has_an_1_type; + break; + case 220 /* FunctionDeclaration */: + case 147 /* MethodDeclaration */: + case 146 /* MethodSignature */: + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + case 179 /* FunctionExpression */: + case 180 /* ArrowFunction */: + if (!declaration.name) { + error(declaration, ts.Diagnostics.Function_expression_which_lacks_return_type_annotation_implicitly_has_an_0_return_type, typeAsString); + return; + } + diagnostic = ts.Diagnostics._0_which_lacks_return_type_annotation_implicitly_has_an_1_return_type; + break; + default: + diagnostic = ts.Diagnostics.Variable_0_implicitly_has_an_1_type; + } + error(declaration, diagnostic, ts.declarationNameToString(declaration.name), typeAsString); } - function getTypeFromFlowType(flowType) { - return flowType.flags === 0 ? flowType.type : flowType; + function reportErrorsFromWidening(declaration, type) { + if (produceDiagnostics && compilerOptions.noImplicitAny && type.flags & 33554432 /* ContainsWideningType */) { + // Report implicit any error within type if possible, otherwise report error on declaration + if (!reportWideningErrorsInType(type)) { + reportImplicitAnyError(declaration, type); + } + } } - function createFlowType(type, incomplete) { - return incomplete ? { flags: 0, type: type } : type; + function forEachMatchingParameterType(source, target, callback) { + var sourceMax = source.parameters.length; + var targetMax = target.parameters.length; + var count; + if (source.hasRestParameter && target.hasRestParameter) { + count = Math.max(sourceMax, targetMax); + } + else if (source.hasRestParameter) { + count = targetMax; + } + else if (target.hasRestParameter) { + count = sourceMax; + } + else { + count = Math.min(sourceMax, targetMax); + } + for (var i = 0; i < count; i++) { + callback(getTypeAtPosition(source, i), getTypeAtPosition(target, i)); + } } - function getFlowTypeOfReference(reference, declaredType, assumeInitialized, flowContainer) { - var key; - if (!reference.flowNode || assumeInitialized && !(declaredType.flags & 4178943 /* Narrowable */)) { - return declaredType; + function createInferenceContext(signature, inferUnionTypes) { + var inferences = ts.map(signature.typeParameters, createTypeInferencesObject); + return { + signature: signature, + inferUnionTypes: inferUnionTypes, + inferences: inferences, + inferredTypes: new Array(signature.typeParameters.length) + }; + } + function createTypeInferencesObject() { + return { + primary: undefined, + secondary: undefined, + topLevel: true, + isFixed: false + }; + } + // Return true if the given type could possibly reference a type parameter for which + // we perform type inference (i.e. a type parameter of a generic function). We cache + // results for union and intersection types for performance reasons. + function couldContainTypeParameters(type) { + return !!(type.flags & 16384 /* TypeParameter */ || + type.flags & 131072 /* Reference */ && ts.forEach(type.typeArguments, couldContainTypeParameters) || + type.flags & 2097152 /* Anonymous */ && type.symbol && type.symbol.flags & (8192 /* Method */ | 2048 /* TypeLiteral */ | 32 /* Class */) || + type.flags & 1572864 /* UnionOrIntersection */ && couldUnionOrIntersectionContainTypeParameters(type)); + } + function couldUnionOrIntersectionContainTypeParameters(type) { + if (type.couldContainTypeParameters === undefined) { + type.couldContainTypeParameters = ts.forEach(type.types, couldContainTypeParameters); } - var initialType = assumeInitialized ? declaredType : includeFalsyTypes(declaredType, 2048 /* Undefined */); - var visitedFlowStart = visitedFlowCount; - var result = getTypeFromFlowType(getTypeAtFlowNode(reference.flowNode)); - visitedFlowCount = visitedFlowStart; - if (reference.parent.kind === 196 /* NonNullExpression */ && getTypeWithFacts(result, 524288 /* NEUndefinedOrNull */).flags & 8192 /* Never */) { - return declaredType; + return type.couldContainTypeParameters; + } + function isTypeParameterAtTopLevel(type, typeParameter) { + return type === typeParameter || type.flags & 1572864 /* UnionOrIntersection */ && ts.forEach(type.types, function (t) { return isTypeParameterAtTopLevel(t, typeParameter); }); + } + function inferTypes(context, originalSource, originalTarget) { + var typeParameters = context.signature.typeParameters; + var sourceStack; + var targetStack; + var depth = 0; + var inferiority = 0; + var visited = ts.createMap(); + inferFromTypes(originalSource, originalTarget); + function isInProcess(source, target) { + for (var i = 0; i < depth; i++) { + if (source === sourceStack[i] && target === targetStack[i]) { + return true; + } + } + return false; } - return result; - function getTypeAtFlowNode(flow) { - while (true) { - if (flow.flags & 512 /* Shared */) { - // We cache results of flow type resolution for shared nodes that were previously visited in - // the same getFlowTypeOfReference invocation. A node is considered shared when it is the - // antecedent of more than one node. - for (var i = visitedFlowStart; i < visitedFlowCount; i++) { - if (visitedFlowNodes[i] === flow) { - return visitedFlowTypes[i]; - } + function inferFromTypes(source, target) { + if (!couldContainTypeParameters(target)) { + return; + } + if (source.flags & 524288 /* Union */ && target.flags & 524288 /* Union */ && !(source.flags & 16 /* Enum */ && target.flags & 16 /* Enum */) || + source.flags & 1048576 /* Intersection */ && target.flags & 1048576 /* Intersection */) { + // Source and target are both unions or both intersections. If source and target + // are the same type, just relate each constituent type to itself. + if (source === target) { + for (var _i = 0, _a = source.types; _i < _a.length; _i++) { + var t = _a[_i]; + inferFromTypes(t, t); } + return; } - var type = void 0; - if (flow.flags & 16 /* Assignment */) { - type = getTypeAtFlowAssignment(flow); - if (!type) { - flow = flow.antecedent; - continue; + // Find each source constituent type that has an identically matching target constituent + // type, and for each such type infer from the type to itself. When inferring from a + // type to itself we effectively find all type parameter occurrences within that type + // and infer themselves as their type arguments. We have special handling for numeric + // and string literals because the number and string types are not represented as unions + // of all their possible values. + var matchingTypes = void 0; + for (var _b = 0, _c = source.types; _b < _c.length; _b++) { + var t = _c[_b]; + if (typeIdenticalToSomeType(t, target.types)) { + (matchingTypes || (matchingTypes = [])).push(t); + inferFromTypes(t, t); + } + else if (t.flags & (64 /* NumberLiteral */ | 32 /* StringLiteral */)) { + var b = getBaseTypeOfLiteralType(t); + if (typeIdenticalToSomeType(b, target.types)) { + (matchingTypes || (matchingTypes = [])).push(t, b); + } } } - else if (flow.flags & 96 /* Condition */) { - type = getTypeAtFlowCondition(flow); + // Next, to improve the quality of inferences, reduce the source and target types by + // removing the identically matched constituents. For example, when inferring from + // 'string | string[]' to 'string | T' we reduce the types to 'string[]' and 'T'. + if (matchingTypes) { + source = removeTypesFromUnionOrIntersection(source, matchingTypes); + target = removeTypesFromUnionOrIntersection(target, matchingTypes); } - else if (flow.flags & 128 /* SwitchClause */) { - type = getTypeAtSwitchClause(flow); + } + if (target.flags & 16384 /* TypeParameter */) { + // If target is a type parameter, make an inference, unless the source type contains + // the anyFunctionType (the wildcard type that's used to avoid contextually typing functions). + // Because the anyFunctionType is internal, it should not be exposed to the user by adding + // it as an inference candidate. Hopefully, a better candidate will come along that does + // not contain anyFunctionType when we come back to this argument for its second round + // of inference. + if (source.flags & 134217728 /* ContainsAnyFunctionType */) { + return; } - else if (flow.flags & 12 /* Label */) { - if (flow.antecedents.length === 1) { - flow = flow.antecedents[0]; - continue; + for (var i = 0; i < typeParameters.length; i++) { + if (target === typeParameters[i]) { + var inferences = context.inferences[i]; + if (!inferences.isFixed) { + // Any inferences that are made to a type parameter in a union type are inferior + // to inferences made to a flat (non-union) type. This is because if we infer to + // T | string[], we really don't know if we should be inferring to T or not (because + // the correct constituent on the target side could be string[]). Therefore, we put + // such inferior inferences into a secondary bucket, and only use them if the primary + // bucket is empty. + var candidates = inferiority ? + inferences.secondary || (inferences.secondary = []) : + inferences.primary || (inferences.primary = []); + if (!ts.contains(candidates, source)) { + candidates.push(source); + } + if (!isTypeParameterAtTopLevel(originalTarget, target)) { + inferences.topLevel = false; + } + } + return; } - type = flow.flags & 4 /* BranchLabel */ ? - getTypeAtFlowBranchLabel(flow) : - getTypeAtFlowLoopLabel(flow); } - else if (flow.flags & 2 /* Start */) { - // Check if we should continue with the control flow of the containing function. - var container = flow.container; - if (container && container !== flowContainer && reference.kind !== 172 /* PropertyAccessExpression */) { - flow = container.flowNode; - continue; - } - // At the top of the flow we have the initial type. - type = initialType; + } + else if (source.flags & 131072 /* Reference */ && target.flags & 131072 /* Reference */ && source.target === target.target) { + // If source and target are references to the same generic type, infer from type arguments + var sourceTypes = source.typeArguments || emptyArray; + var targetTypes = target.typeArguments || emptyArray; + var count = sourceTypes.length < targetTypes.length ? sourceTypes.length : targetTypes.length; + for (var i = 0; i < count; i++) { + inferFromTypes(sourceTypes[i], targetTypes[i]); } - else { - // Unreachable code errors are reported in the binding phase. Here we - // simply return the declared type to reduce follow-on errors. - type = declaredType; + } + else if (target.flags & 1572864 /* UnionOrIntersection */) { + var targetTypes = target.types; + var typeParameterCount = 0; + var typeParameter = void 0; + // First infer to each type in union or intersection that isn't a type parameter + for (var _d = 0, targetTypes_2 = targetTypes; _d < targetTypes_2.length; _d++) { + var t = targetTypes_2[_d]; + if (t.flags & 16384 /* TypeParameter */ && ts.contains(typeParameters, t)) { + typeParameter = t; + typeParameterCount++; + } + else { + inferFromTypes(source, t); + } } - if (flow.flags & 512 /* Shared */) { - // Record visited node and the associated type in the cache. - visitedFlowNodes[visitedFlowCount] = flow; - visitedFlowTypes[visitedFlowCount] = type; - visitedFlowCount++; + // Next, if target containings a single naked type parameter, make a secondary inference to that type + // parameter. This gives meaningful results for union types in co-variant positions and intersection + // types in contra-variant positions (such as callback parameters). + if (typeParameterCount === 1) { + inferiority++; + inferFromTypes(source, typeParameter); + inferiority--; } - return type; } - } - function getTypeAtFlowAssignment(flow) { - var node = flow.node; - // Assignments only narrow the computed type if the declared type is a union type. Thus, we - // only need to evaluate the assigned type if the declared type is a union type. - if (isMatchingReference(reference, node)) { - var isIncrementOrDecrement = node.parent.kind === 185 /* PrefixUnaryExpression */ || node.parent.kind === 186 /* PostfixUnaryExpression */; - return declaredType.flags & 524288 /* Union */ && !isIncrementOrDecrement ? - getAssignmentReducedType(declaredType, getInitialOrAssignedType(node)) : - declaredType; + else if (source.flags & 1572864 /* UnionOrIntersection */) { + // Source is a union or intersection type, infer from each constituent type + var sourceTypes = source.types; + for (var _e = 0, sourceTypes_3 = sourceTypes; _e < sourceTypes_3.length; _e++) { + var sourceType = sourceTypes_3[_e]; + inferFromTypes(sourceType, target); + } } - // We didn't have a direct match. However, if the reference is a dotted name, this - // may be an assignment to a left hand part of the reference. For example, for a - // reference 'x.y.z', we may be at an assignment to 'x.y' or 'x'. In that case, - // return the declared type. - if (containsMatchingReference(reference, node)) { - return declaredType; + else { + source = getApparentType(source); + if (source.flags & 2588672 /* ObjectType */) { + if (isInProcess(source, target)) { + return; + } + if (isDeeplyNestedGeneric(source, sourceStack, depth) && isDeeplyNestedGeneric(target, targetStack, depth)) { + return; + } + var key = source.id + "," + target.id; + if (visited[key]) { + return; + } + visited[key] = true; + if (depth === 0) { + sourceStack = []; + targetStack = []; + } + sourceStack[depth] = source; + targetStack[depth] = target; + depth++; + inferFromProperties(source, target); + inferFromSignatures(source, target, 0 /* Call */); + inferFromSignatures(source, target, 1 /* Construct */); + inferFromIndexTypes(source, target); + depth--; + } } - // Assignment doesn't affect reference - return undefined; } - function getTypeAtFlowCondition(flow) { - var flowType = getTypeAtFlowNode(flow.antecedent); - var type = getTypeFromFlowType(flowType); - if (!(type.flags & 8192 /* Never */)) { - // If we have an antecedent type (meaning we're reachable in some way), we first - // attempt to narrow the antecedent type. If that produces the never type, and if - // the antecedent type is incomplete (i.e. a transient type in a loop), then we - // take the type guard as an indication that control *could* reach here once we - // have the complete type. We proceed by switching to the silent never type which - // doesn't report errors when operators are applied to it. Note that this is the - // *only* place a silent never type is ever generated. - var assumeTrue = (flow.flags & 32 /* TrueCondition */) !== 0; - type = narrowType(type, flow.expression, assumeTrue); - if (type.flags & 8192 /* Never */ && isIncomplete(flowType)) { - type = silentNeverType; + function inferFromProperties(source, target) { + var properties = getPropertiesOfObjectType(target); + for (var _i = 0, properties_3 = properties; _i < properties_3.length; _i++) { + var targetProp = properties_3[_i]; + var sourceProp = getPropertyOfObjectType(source, targetProp.name); + if (sourceProp) { + inferFromTypes(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp)); } } - return createFlowType(type, isIncomplete(flowType)); } - function getTypeAtSwitchClause(flow) { - var flowType = getTypeAtFlowNode(flow.antecedent); - var type = getTypeFromFlowType(flowType); - var expr = flow.switchStatement.expression; - if (isMatchingReference(reference, expr)) { - type = narrowTypeBySwitchOnDiscriminant(type, flow.switchStatement, flow.clauseStart, flow.clauseEnd); - } - else if (isMatchingReferenceDiscriminant(expr)) { - type = narrowTypeByDiscriminant(type, expr, function (t) { return narrowTypeBySwitchOnDiscriminant(t, flow.switchStatement, flow.clauseStart, flow.clauseEnd); }); + function inferFromSignatures(source, target, kind) { + var sourceSignatures = getSignaturesOfType(source, kind); + var targetSignatures = getSignaturesOfType(target, kind); + var sourceLen = sourceSignatures.length; + var targetLen = targetSignatures.length; + var len = sourceLen < targetLen ? sourceLen : targetLen; + for (var i = 0; i < len; i++) { + inferFromSignature(getErasedSignature(sourceSignatures[sourceLen - len + i]), getErasedSignature(targetSignatures[targetLen - len + i])); } - return createFlowType(type, isIncomplete(flowType)); } - function getTypeAtFlowBranchLabel(flow) { - var antecedentTypes = []; - var subtypeReduction = false; - var seenIncomplete = false; - for (var _i = 0, _a = flow.antecedents; _i < _a.length; _i++) { - var antecedent = _a[_i]; - var flowType = getTypeAtFlowNode(antecedent); - var type = getTypeFromFlowType(flowType); - // If the type at a particular antecedent path is the declared type and the - // reference is known to always be assigned (i.e. when declared and initial types - // are the same), there is no reason to process more antecedents since the only - // possible outcome is subtypes that will be removed in the final union type anyway. - if (type === declaredType && declaredType === initialType) { - return type; - } - if (!ts.contains(antecedentTypes, type)) { - antecedentTypes.push(type); - } - // If an antecedent type is not a subset of the declared type, we need to perform - // subtype reduction. This happens when a "foreign" type is injected into the control - // flow using the instanceof operator or a user defined type predicate. - if (!isTypeSubsetOf(type, declaredType)) { - subtypeReduction = true; - } - if (isIncomplete(flowType)) { - seenIncomplete = true; - } - } - return createFlowType(getUnionType(antecedentTypes, subtypeReduction), seenIncomplete); + function inferFromParameterTypes(source, target) { + return inferFromTypes(source, target); } - function getTypeAtFlowLoopLabel(flow) { - // If we have previously computed the control flow type for the reference at - // this flow loop junction, return the cached type. - var id = getFlowNodeId(flow); - var cache = flowLoopCaches[id] || (flowLoopCaches[id] = ts.createMap()); - if (!key) { - key = getFlowCacheKey(reference); + function inferFromSignature(source, target) { + forEachMatchingParameterType(source, target, inferFromParameterTypes); + if (source.typePredicate && target.typePredicate && source.typePredicate.kind === target.typePredicate.kind) { + inferFromTypes(source.typePredicate.type, target.typePredicate.type); } - if (cache[key]) { - return cache[key]; + else { + inferFromTypes(getReturnTypeOfSignature(source), getReturnTypeOfSignature(target)); } - // If this flow loop junction and reference are already being processed, return - // the union of the types computed for each branch so far, marked as incomplete. - // We should never see an empty array here because the first antecedent of a loop - // junction is always the non-looping control flow path that leads to the top. - for (var i = flowLoopStart; i < flowLoopCount; i++) { - if (flowLoopNodes[i] === flow && flowLoopKeys[i] === key) { - return createFlowType(getUnionType(flowLoopTypes[i]), /*incomplete*/ true); + } + function inferFromIndexTypes(source, target) { + var targetStringIndexType = getIndexTypeOfType(target, 0 /* String */); + if (targetStringIndexType) { + var sourceIndexType = getIndexTypeOfType(source, 0 /* String */) || + getImplicitIndexTypeOfType(source, 0 /* String */); + if (sourceIndexType) { + inferFromTypes(sourceIndexType, targetStringIndexType); } } - // Add the flow loop junction and reference to the in-process stack and analyze - // each antecedent code path. - var antecedentTypes = []; - var subtypeReduction = false; - var firstAntecedentType; - flowLoopNodes[flowLoopCount] = flow; - flowLoopKeys[flowLoopCount] = key; - flowLoopTypes[flowLoopCount] = antecedentTypes; - for (var _i = 0, _a = flow.antecedents; _i < _a.length; _i++) { - var antecedent = _a[_i]; - flowLoopCount++; - var flowType = getTypeAtFlowNode(antecedent); - flowLoopCount--; - if (!firstAntecedentType) { - firstAntecedentType = flowType; - } - var type = getTypeFromFlowType(flowType); - // If we see a value appear in the cache it is a sign that control flow analysis - // was restarted and completed by checkExpressionCached. We can simply pick up - // the resulting type and bail out. - if (cache[key]) { - return cache[key]; - } - if (!ts.contains(antecedentTypes, type)) { - antecedentTypes.push(type); - } - // If an antecedent type is not a subset of the declared type, we need to perform - // subtype reduction. This happens when a "foreign" type is injected into the control - // flow using the instanceof operator or a user defined type predicate. - if (!isTypeSubsetOf(type, declaredType)) { - subtypeReduction = true; - } - // If the type at a particular antecedent path is the declared type there is no - // reason to process more antecedents since the only possible outcome is subtypes - // that will be removed in the final union type anyway. - if (type === declaredType) { - break; + var targetNumberIndexType = getIndexTypeOfType(target, 1 /* Number */); + if (targetNumberIndexType) { + var sourceIndexType = getIndexTypeOfType(source, 1 /* Number */) || + getIndexTypeOfType(source, 0 /* String */) || + getImplicitIndexTypeOfType(source, 1 /* Number */); + if (sourceIndexType) { + inferFromTypes(sourceIndexType, targetNumberIndexType); } } - // The result is incomplete if the first antecedent (the non-looping control flow path) - // is incomplete. - var result = getUnionType(antecedentTypes, subtypeReduction); - if (isIncomplete(firstAntecedentType)) { - return createFlowType(result, /*incomplete*/ true); - } - return cache[key] = result; - } - function isMatchingReferenceDiscriminant(expr) { - return expr.kind === 172 /* PropertyAccessExpression */ && - declaredType.flags & 524288 /* Union */ && - isMatchingReference(reference, expr.expression) && - isDiscriminantProperty(declaredType, expr.name.text); - } - function narrowTypeByDiscriminant(type, propAccess, narrowType) { - var propName = propAccess.name.text; - var propType = getTypeOfPropertyOfType(type, propName); - var narrowedPropType = propType && narrowType(propType); - return propType === narrowedPropType ? type : filterType(type, function (t) { return isTypeComparableTo(getTypeOfPropertyOfType(t, propName), narrowedPropType); }); - } - function narrowTypeByTruthiness(type, expr, assumeTrue) { - if (isMatchingReference(reference, expr)) { - return getTypeWithFacts(type, assumeTrue ? 1048576 /* Truthy */ : 2097152 /* Falsy */); - } - if (isMatchingReferenceDiscriminant(expr)) { - return narrowTypeByDiscriminant(type, expr, function (t) { return getTypeWithFacts(t, assumeTrue ? 1048576 /* Truthy */ : 2097152 /* Falsy */); }); - } - if (containsMatchingReferenceDiscriminant(reference, expr)) { - return declaredType; - } - return type; - } - function narrowTypeByBinaryExpression(type, expr, assumeTrue) { - switch (expr.operatorToken.kind) { - case 56 /* EqualsToken */: - return narrowTypeByTruthiness(type, expr.left, assumeTrue); - case 30 /* EqualsEqualsToken */: - case 31 /* ExclamationEqualsToken */: - case 32 /* EqualsEqualsEqualsToken */: - case 33 /* ExclamationEqualsEqualsToken */: - var operator_1 = expr.operatorToken.kind; - var left_1 = getReferenceCandidate(expr.left); - var right_1 = getReferenceCandidate(expr.right); - if (left_1.kind === 182 /* TypeOfExpression */ && right_1.kind === 9 /* StringLiteral */) { - return narrowTypeByTypeof(type, left_1, operator_1, right_1, assumeTrue); - } - if (right_1.kind === 182 /* TypeOfExpression */ && left_1.kind === 9 /* StringLiteral */) { - return narrowTypeByTypeof(type, right_1, operator_1, left_1, assumeTrue); - } - if (isMatchingReference(reference, left_1)) { - return narrowTypeByEquality(type, operator_1, right_1, assumeTrue); - } - if (isMatchingReference(reference, right_1)) { - return narrowTypeByEquality(type, operator_1, left_1, assumeTrue); - } - if (isMatchingReferenceDiscriminant(left_1)) { - return narrowTypeByDiscriminant(type, left_1, function (t) { return narrowTypeByEquality(t, operator_1, right_1, assumeTrue); }); - } - if (isMatchingReferenceDiscriminant(right_1)) { - return narrowTypeByDiscriminant(type, right_1, function (t) { return narrowTypeByEquality(t, operator_1, left_1, assumeTrue); }); - } - if (containsMatchingReferenceDiscriminant(reference, left_1) || containsMatchingReferenceDiscriminant(reference, right_1)) { - return declaredType; - } - break; - case 91 /* InstanceOfKeyword */: - return narrowTypeByInstanceof(type, expr, assumeTrue); - case 24 /* CommaToken */: - return narrowType(type, expr.right, assumeTrue); - } - return type; - } - function narrowTypeByEquality(type, operator, value, assumeTrue) { - if (type.flags & 1 /* Any */) { - return type; - } - if (operator === 31 /* ExclamationEqualsToken */ || operator === 33 /* ExclamationEqualsEqualsToken */) { - assumeTrue = !assumeTrue; - } - var valueType = checkExpression(value); - if (valueType.flags & 6144 /* Nullable */) { - if (!strictNullChecks) { - return type; - } - var doubleEquals = operator === 30 /* EqualsEqualsToken */ || operator === 31 /* ExclamationEqualsToken */; - var facts = doubleEquals ? - assumeTrue ? 65536 /* EQUndefinedOrNull */ : 524288 /* NEUndefinedOrNull */ : - value.kind === 93 /* NullKeyword */ ? - assumeTrue ? 32768 /* EQNull */ : 262144 /* NENull */ : - assumeTrue ? 16384 /* EQUndefined */ : 131072 /* NEUndefined */; - return getTypeWithFacts(type, facts); - } - if (type.flags & 2589191 /* NotUnionOrUnit */) { - return type; - } - if (assumeTrue) { - var narrowedType = filterType(type, function (t) { return areTypesComparable(t, valueType); }); - return narrowedType.flags & 8192 /* Never */ ? type : narrowedType; - } - if (isUnitType(valueType)) { - var regularType_1 = getRegularTypeOfLiteralType(valueType); - return filterType(type, function (t) { return getRegularTypeOfLiteralType(t) !== regularType_1; }); - } - return type; } - function narrowTypeByTypeof(type, typeOfExpr, operator, literal, assumeTrue) { - // We have '==', '!=', '====', or !==' operator with 'typeof xxx' and string literal operands - var target = getReferenceCandidate(typeOfExpr.expression); - if (!isMatchingReference(reference, target)) { - // For a reference of the form 'x.y', a 'typeof x === ...' type guard resets the - // narrowed type of 'y' to its declared type. - if (containsMatchingReference(reference, target)) { - return declaredType; - } - return type; - } - if (operator === 31 /* ExclamationEqualsToken */ || operator === 33 /* ExclamationEqualsEqualsToken */) { - assumeTrue = !assumeTrue; - } - if (assumeTrue && !(type.flags & 524288 /* Union */)) { - // We narrow a non-union type to an exact primitive type if the non-union type - // is a supertype of that primitive type. For example, type 'any' can be narrowed - // to one of the primitive types. - var targetType = typeofTypesByName[literal.text]; - if (targetType && isTypeSubtypeOf(targetType, type)) { - return targetType; - } + } + function typeIdenticalToSomeType(type, types) { + for (var _i = 0, types_10 = types; _i < types_10.length; _i++) { + var t = types_10[_i]; + if (isTypeIdenticalTo(t, type)) { + return true; } - var facts = assumeTrue ? - typeofEQFacts[literal.text] || 64 /* TypeofEQHostObject */ : - typeofNEFacts[literal.text] || 8192 /* TypeofNEHostObject */; - return getTypeWithFacts(type, facts); } - function narrowTypeBySwitchOnDiscriminant(type, switchStatement, clauseStart, clauseEnd) { - // We only narrow if all case expressions specify values with unit types - var switchTypes = getSwitchClauseTypes(switchStatement); - if (!switchTypes.length) { - return type; - } - var clauseTypes = switchTypes.slice(clauseStart, clauseEnd); - var hasDefaultClause = clauseStart === clauseEnd || ts.contains(clauseTypes, neverType); - var discriminantType = getUnionType(clauseTypes); - var caseType = discriminantType.flags & 8192 /* Never */ ? neverType : filterType(type, function (t) { return isTypeComparableTo(discriminantType, t); }); - if (!hasDefaultClause) { - return caseType; + return false; + } + /** + * Return a new union or intersection type computed by removing a given set of types + * from a given union or intersection type. + */ + function removeTypesFromUnionOrIntersection(type, typesToRemove) { + var reducedTypes = []; + for (var _i = 0, _a = type.types; _i < _a.length; _i++) { + var t = _a[_i]; + if (!typeIdenticalToSomeType(t, typesToRemove)) { + reducedTypes.push(t); } - var defaultType = filterType(type, function (t) { return !(isUnitType(t) && ts.contains(switchTypes, getRegularTypeOfLiteralType(t))); }); - return caseType.flags & 8192 /* Never */ ? defaultType : getUnionType([caseType, defaultType]); } - function narrowTypeByInstanceof(type, expr, assumeTrue) { - var left = getReferenceCandidate(expr.left); - if (!isMatchingReference(reference, left)) { - // For a reference of the form 'x.y', an 'x instanceof T' type guard resets the - // narrowed type of 'y' to its declared type. - if (containsMatchingReference(reference, left)) { - return declaredType; - } - return type; - } - // Check that right operand is a function type with a prototype property - var rightType = checkExpression(expr.right); - if (!isTypeSubtypeOf(rightType, globalFunctionType)) { - return type; - } - var targetType; - var prototypeProperty = getPropertyOfType(rightType, "prototype"); - if (prototypeProperty) { - // Target type is type of the prototype property - var prototypePropertyType = getTypeOfSymbol(prototypeProperty); - if (!isTypeAny(prototypePropertyType)) { - targetType = prototypePropertyType; - } + return type.flags & 524288 /* Union */ ? getUnionType(reducedTypes) : getIntersectionType(reducedTypes); + } + function getInferenceCandidates(context, index) { + var inferences = context.inferences[index]; + return inferences.primary || inferences.secondary || emptyArray; + } + function hasPrimitiveConstraint(type) { + var constraint = getConstraintOfTypeParameter(type); + return constraint && maybeTypeOfKind(constraint, 8190 /* Primitive */); + } + function getInferredType(context, index) { + var inferredType = context.inferredTypes[index]; + var inferenceSucceeded; + if (!inferredType) { + var inferences = getInferenceCandidates(context, index); + if (inferences.length) { + // We widen inferred literal types if + // all inferences were made to top-level ocurrences of the type parameter, and + // the type parameter has no constraint or its constraint includes no primitive or literal types, and + // the type parameter was fixed during inference or does not occur at top-level in the return type. + var signature = context.signature; + var widenLiteralTypes = context.inferences[index].topLevel && + !hasPrimitiveConstraint(signature.typeParameters[index]) && + (context.inferences[index].isFixed || !isTypeParameterAtTopLevel(getReturnTypeOfSignature(signature), signature.typeParameters[index])); + var baseInferences = widenLiteralTypes ? ts.map(inferences, getWidenedLiteralType) : inferences; + // Infer widened union or supertype, or the unknown type for no common supertype + var unionOrSuperType = context.inferUnionTypes ? getUnionType(baseInferences, /*subtypeReduction*/ true) : getCommonSupertype(baseInferences); + inferredType = unionOrSuperType ? getWidenedType(unionOrSuperType) : unknownType; + inferenceSucceeded = !!unionOrSuperType; } - // Don't narrow from 'any' if the target type is exactly 'Object' or 'Function' - if (isTypeAny(type) && (targetType === globalObjectType || targetType === globalFunctionType)) { - return type; + else { + // Infer the empty object type when no inferences were made. It is important to remember that + // in this case, inference still succeeds, meaning there is no error for not having inference + // candidates. An inference error only occurs when there are *conflicting* candidates, i.e. + // candidates with no common supertype. + inferredType = emptyObjectType; + inferenceSucceeded = true; } - if (!targetType) { - // Target type is type of construct signature - var constructSignatures = void 0; - if (rightType.flags & 65536 /* Interface */) { - constructSignatures = resolveDeclaredMembers(rightType).declaredConstructSignatures; - } - else if (rightType.flags & 2097152 /* Anonymous */) { - constructSignatures = getSignaturesOfType(rightType, 1 /* Construct */); - } - if (constructSignatures && constructSignatures.length) { - targetType = getUnionType(ts.map(constructSignatures, function (signature) { return getReturnTypeOfSignature(getErasedSignature(signature)); })); + context.inferredTypes[index] = inferredType; + // Only do the constraint check if inference succeeded (to prevent cascading errors) + if (inferenceSucceeded) { + var constraint = getConstraintOfTypeParameter(context.signature.typeParameters[index]); + if (constraint) { + var instantiatedConstraint = instantiateType(constraint, getInferenceMapper(context)); + if (!isTypeAssignableTo(inferredType, getTypeWithThisArgument(instantiatedConstraint, inferredType))) { + context.inferredTypes[index] = inferredType = instantiatedConstraint; + } } } - if (targetType) { - return getNarrowedType(type, targetType, assumeTrue); + else if (context.failedTypeParameterIndex === undefined || context.failedTypeParameterIndex > index) { + // If inference failed, it is necessary to record the index of the failed type parameter (the one we are on). + // It might be that inference has already failed on a later type parameter on a previous call to inferTypeArguments. + // So if this failure is on preceding type parameter, this type parameter is the new failure index. + context.failedTypeParameterIndex = index; } - return type; } - function getNarrowedType(type, candidate, assumeTrue) { - if (!assumeTrue) { - return filterType(type, function (t) { return !isTypeInstanceOf(t, candidate); }); - } - // If the current type is a union type, remove all constituents that couldn't be instances of - // the candidate type. If one or more constituents remain, return a union of those. - if (type.flags & 524288 /* Union */) { - var assignableType = filterType(type, function (t) { return isTypeInstanceOf(t, candidate); }); - if (!(assignableType.flags & 8192 /* Never */)) { - return assignableType; - } - } - // If the candidate type is a subtype of the target type, narrow to the candidate type. - // Otherwise, if the target type is assignable to the candidate type, keep the target type. - // Otherwise, if the candidate type is assignable to the target type, narrow to the candidate - // type. Otherwise, the types are completely unrelated, so narrow to an intersection of the - // two types. - var targetType = type.flags & 16384 /* TypeParameter */ ? getApparentType(type) : type; - return isTypeSubtypeOf(candidate, type) ? candidate : - isTypeAssignableTo(type, candidate) ? type : - isTypeAssignableTo(candidate, targetType) ? candidate : - getIntersectionType([type, candidate]); + return inferredType; + } + function getInferredTypes(context) { + for (var i = 0; i < context.inferredTypes.length; i++) { + getInferredType(context, i); } - function narrowTypeByTypePredicate(type, callExpression, assumeTrue) { - if (!hasMatchingArgument(callExpression, reference)) { - return type; - } - var signature = getResolvedSignature(callExpression); - var predicate = signature.typePredicate; - if (!predicate) { - return type; - } - // Don't narrow from 'any' if the predicate type is exactly 'Object' or 'Function' - if (isTypeAny(type) && (predicate.type === globalObjectType || predicate.type === globalFunctionType)) { - return type; - } - if (ts.isIdentifierTypePredicate(predicate)) { - var predicateArgument = callExpression.arguments[predicate.parameterIndex]; - if (predicateArgument) { - if (isMatchingReference(reference, predicateArgument)) { - return getNarrowedType(type, predicate.type, assumeTrue); - } - if (containsMatchingReference(reference, predicateArgument)) { - return declaredType; - } - } - } - else { - var invokedExpression = skipParenthesizedNodes(callExpression.expression); - if (invokedExpression.kind === 173 /* ElementAccessExpression */ || invokedExpression.kind === 172 /* PropertyAccessExpression */) { - var accessExpression = invokedExpression; - var possibleReference = skipParenthesizedNodes(accessExpression.expression); - if (isMatchingReference(reference, possibleReference)) { - return getNarrowedType(type, predicate.type, assumeTrue); - } - if (containsMatchingReference(reference, possibleReference)) { - return declaredType; - } - } - } - return type; + return context.inferredTypes; + } + // EXPRESSION TYPE CHECKING + function getResolvedSymbol(node) { + var links = getNodeLinks(node); + if (!links.resolvedSymbol) { + links.resolvedSymbol = !ts.nodeIsMissing(node) && resolveName(node, node.text, 107455 /* Value */ | 1048576 /* ExportValue */, ts.Diagnostics.Cannot_find_name_0, node) || unknownSymbol; } - // Narrow the given type based on the given expression having the assumed boolean value. The returned type - // will be a subtype or the same type as the argument. - function narrowType(type, expr, assumeTrue) { - switch (expr.kind) { + return links.resolvedSymbol; + } + function isInTypeQuery(node) { + // TypeScript 1.0 spec (April 2014): 3.6.3 + // A type query consists of the keyword typeof followed by an expression. + // The expression is restricted to a single identifier or a sequence of identifiers separated by periods + while (node) { + switch (node.kind) { + case 158 /* TypeQuery */: + return true; case 69 /* Identifier */: - case 97 /* ThisKeyword */: - case 172 /* PropertyAccessExpression */: - return narrowTypeByTruthiness(type, expr, assumeTrue); - case 174 /* CallExpression */: - return narrowTypeByTypePredicate(type, expr, assumeTrue); - case 178 /* ParenthesizedExpression */: - return narrowType(type, expr.expression, assumeTrue); - case 187 /* BinaryExpression */: - return narrowTypeByBinaryExpression(type, expr, assumeTrue); - case 185 /* PrefixUnaryExpression */: - if (expr.operator === 49 /* ExclamationToken */) { - return narrowType(type, expr.operand, !assumeTrue); - } - break; + case 139 /* QualifiedName */: + node = node.parent; + continue; + default: + return false; } - return type; } + ts.Debug.fail("should not get here"); } - function getTypeOfSymbolAtLocation(symbol, location) { - // If we have an identifier or a property access at the given location, if the location is - // an dotted name expression, and if the location is not an assignment target, obtain the type - // of the expression (which will reflect control flow analysis). If the expression indeed - // resolved to the given symbol, return the narrowed type. - if (location.kind === 69 /* Identifier */) { - if (ts.isRightSideOfQualifiedNameOrPropertyAccess(location)) { - location = location.parent; - } - if (ts.isExpression(location) && !ts.isAssignmentTarget(location)) { - var type = checkExpression(location); - if (getExportSymbolOfValueSymbolIfExported(getNodeLinks(location).resolvedSymbol) === symbol) { - return type; - } - } + // Return the flow cache key for a "dotted name" (i.e. a sequence of identifiers + // separated by dots). The key consists of the id of the symbol referenced by the + // leftmost identifier followed by zero or more property names separated by dots. + // The result is undefined if the reference isn't a dotted name. + function getFlowCacheKey(node) { + if (node.kind === 69 /* Identifier */) { + var symbol = getResolvedSymbol(node); + return symbol !== unknownSymbol ? "" + getSymbolId(symbol) : undefined; } - // The location isn't a reference to the given symbol, meaning we're being asked - // a hypothetical question of what type the symbol would have if there was a reference - // to it at the given location. Since we have no control flow information for the - // hypothetical reference (control flow information is created and attached by the - // binder), we simply return the declared type of the symbol. - return getTypeOfSymbol(symbol); - } - function skipParenthesizedNodes(expression) { - while (expression.kind === 178 /* ParenthesizedExpression */) { - expression = expression.expression; + if (node.kind === 97 /* ThisKeyword */) { + return "0"; } - return expression; + if (node.kind === 172 /* PropertyAccessExpression */) { + var key = getFlowCacheKey(node.expression); + return key && key + "." + node.name.text; + } + return undefined; } - function getControlFlowContainer(node) { - while (true) { - node = node.parent; - if (ts.isFunctionLike(node) && !ts.getImmediatelyInvokedFunctionExpression(node) || - node.kind === 226 /* ModuleBlock */ || - node.kind === 256 /* SourceFile */ || - node.kind === 145 /* PropertyDeclaration */) { + function getLeftmostIdentifierOrThis(node) { + switch (node.kind) { + case 69 /* Identifier */: + case 97 /* ThisKeyword */: return node; - } + case 172 /* PropertyAccessExpression */: + return getLeftmostIdentifierOrThis(node.expression); } + return undefined; } - // Check if a parameter is assigned anywhere within its declaring function. - function isParameterAssigned(symbol) { - var func = ts.getRootDeclaration(symbol.valueDeclaration).parent; - var links = getNodeLinks(func); - if (!(links.flags & 4194304 /* AssignmentsMarked */)) { - links.flags |= 4194304 /* AssignmentsMarked */; - if (!hasParentWithAssignmentsMarked(func)) { - markParameterAssignments(func); - } + function isMatchingReference(source, target) { + switch (source.kind) { + case 69 /* Identifier */: + return target.kind === 69 /* Identifier */ && getResolvedSymbol(source) === getResolvedSymbol(target) || + (target.kind === 218 /* VariableDeclaration */ || target.kind === 169 /* BindingElement */) && + getExportSymbolOfValueSymbolIfExported(getResolvedSymbol(source)) === getSymbolOfNode(target); + case 97 /* ThisKeyword */: + return target.kind === 97 /* ThisKeyword */; + case 172 /* PropertyAccessExpression */: + return target.kind === 172 /* PropertyAccessExpression */ && + source.name.text === target.name.text && + isMatchingReference(source.expression, target.expression); } - return symbol.isAssigned || false; + return false; } - function hasParentWithAssignmentsMarked(node) { - while (true) { - node = node.parent; - if (!node) { - return false; - } - if (ts.isFunctionLike(node) && getNodeLinks(node).flags & 4194304 /* AssignmentsMarked */) { + function containsMatchingReference(source, target) { + while (source.kind === 172 /* PropertyAccessExpression */) { + source = source.expression; + if (isMatchingReference(source, target)) { return true; } } + return false; } - function markParameterAssignments(node) { - if (node.kind === 69 /* Identifier */) { - if (ts.isAssignmentTarget(node)) { - var symbol = getResolvedSymbol(node); - if (symbol.valueDeclaration && ts.getRootDeclaration(symbol.valueDeclaration).kind === 142 /* Parameter */) { - symbol.isAssigned = true; - } - } + // Return true if target is a property access xxx.yyy, source is a property access xxx.zzz, the declared + // type of xxx is a union type, and yyy is a property that is possibly a discriminant. We consider a property + // a possible discriminant if its type differs in the constituents of containing union type, and if every + // choice is a unit type or a union of unit types. + function containsMatchingReferenceDiscriminant(source, target) { + return target.kind === 172 /* PropertyAccessExpression */ && + containsMatchingReference(source, target.expression) && + isDiscriminantProperty(getDeclaredTypeOfReference(target.expression), target.name.text); + } + function getDeclaredTypeOfReference(expr) { + if (expr.kind === 69 /* Identifier */) { + return getTypeOfSymbol(getResolvedSymbol(expr)); } - else { - ts.forEachChild(node, markParameterAssignments); + if (expr.kind === 172 /* PropertyAccessExpression */) { + var type = getDeclaredTypeOfReference(expr.expression); + return type && getTypeOfPropertyOfType(type, expr.name.text); } + return undefined; } - function checkIdentifier(node) { - var symbol = getResolvedSymbol(node); - // As noted in ECMAScript 6 language spec, arrow functions never have an arguments objects. - // Although in down-level emit of arrow function, we emit it using function expression which means that - // arguments objects will be bound to the inner object; emitting arrow function natively in ES6, arguments objects - // will be bound to non-arrow function that contain this arrow function. This results in inconsistent behavior. - // To avoid that we will give an error to users if they use arguments objects in arrow function so that they - // can explicitly bound arguments objects - if (symbol === argumentsSymbol) { - var container = ts.getContainingFunction(node); - if (languageVersion < 2 /* ES6 */) { - if (container.kind === 180 /* ArrowFunction */) { - error(node, ts.Diagnostics.The_arguments_object_cannot_be_referenced_in_an_arrow_function_in_ES3_and_ES5_Consider_using_a_standard_function_expression); - } - else if (ts.hasModifier(container, 256 /* Async */)) { - error(node, ts.Diagnostics.The_arguments_object_cannot_be_referenced_in_an_async_function_or_method_in_ES3_and_ES5_Consider_using_a_standard_function_or_method); + function isDiscriminantProperty(type, name) { + if (type && type.flags & 524288 /* Union */) { + var prop = getUnionOrIntersectionProperty(type, name); + if (prop && prop.flags & 268435456 /* SyntheticProperty */) { + if (prop.isDiscriminantProperty === undefined) { + prop.isDiscriminantProperty = prop.hasNonUniformType && isLiteralType(getTypeOfSymbol(prop)); } + return prop.isDiscriminantProperty; } - if (node.flags & 262144 /* AwaitContext */) { - getNodeLinks(container).flags |= 8192 /* CaptureArguments */; + } + return false; + } + function isOrContainsMatchingReference(source, target) { + return isMatchingReference(source, target) || containsMatchingReference(source, target); + } + function hasMatchingArgument(callExpression, reference) { + if (callExpression.arguments) { + for (var _i = 0, _a = callExpression.arguments; _i < _a.length; _i++) { + var argument = _a[_i]; + if (isOrContainsMatchingReference(reference, argument)) { + return true; + } } } - if (symbol.flags & 8388608 /* Alias */ && !isInTypeQuery(node) && !isConstEnumOrConstEnumOnlyModule(resolveAlias(symbol))) { - markAliasSymbolAsReferenced(symbol); + if (callExpression.expression.kind === 172 /* PropertyAccessExpression */ && + isOrContainsMatchingReference(reference, callExpression.expression.expression)) { + return true; } - var localOrExportSymbol = getExportSymbolOfValueSymbolIfExported(symbol); - if (localOrExportSymbol.flags & 32 /* Class */) { - var declaration_1 = localOrExportSymbol.valueDeclaration; - // Due to the emit for class decorators, any reference to the class from inside of the class body - // must instead be rewritten to point to a temporary variable to avoid issues with the double-bind - // behavior of class names in ES6. - if (languageVersion === 2 /* ES6 */ - && declaration_1.kind === 221 /* ClassDeclaration */ - && ts.nodeIsDecorated(declaration_1)) { - var container = ts.getContainingClass(node); - while (container !== undefined) { - if (container === declaration_1 && container.name !== node) { - getNodeLinks(declaration_1).flags |= 8388608 /* ClassWithConstructorReference */; - getNodeLinks(node).flags |= 16777216 /* ConstructorReferenceInClass */; - break; - } - container = ts.getContainingClass(container); - } - } - else if (declaration_1.kind === 192 /* ClassExpression */) { - // When we emit a class expression with static members that contain a reference - // to the constructor in the initializer, we will need to substitute that - // binding with an alias as the class name is not in scope. - var container = ts.getThisContainer(node, /*includeArrowFunctions*/ false); - while (container !== undefined) { - if (container.parent === declaration_1) { - if (container.kind === 145 /* PropertyDeclaration */ && ts.hasModifier(container, 32 /* Static */)) { - getNodeLinks(declaration_1).flags |= 8388608 /* ClassWithConstructorReference */; - getNodeLinks(node).flags |= 16777216 /* ConstructorReferenceInClass */; - } - break; - } - container = ts.getThisContainer(container, /*includeArrowFunctions*/ false); - } - } - } - checkCollisionWithCapturedSuperVariable(node, node); - checkCollisionWithCapturedThisVariable(node, node); - checkNestedBlockScopedBinding(node, symbol); - var type = getTypeOfSymbol(localOrExportSymbol); - var declaration = localOrExportSymbol.valueDeclaration; - // We only narrow variables and parameters occurring in a non-assignment position. For all other - // entities we simply return the declared type. - if (!(localOrExportSymbol.flags & 3 /* Variable */) || ts.isAssignmentTarget(node) || !declaration) { - return type; - } - // The declaration container is the innermost function that encloses the declaration of the variable - // or parameter. The flow container is the innermost function starting with which we analyze the control - // flow graph to determine the control flow based type. - var isParameter = ts.getRootDeclaration(declaration).kind === 142 /* Parameter */; - var declarationContainer = getControlFlowContainer(declaration); - var flowContainer = getControlFlowContainer(node); - var isOuterVariable = flowContainer !== declarationContainer; - // When the control flow originates in a function expression or arrow function and we are referencing - // a const variable or parameter from an outer function, we extend the origin of the control flow - // analysis to include the immediately enclosing function. - while (flowContainer !== declarationContainer && - (flowContainer.kind === 179 /* FunctionExpression */ || flowContainer.kind === 180 /* ArrowFunction */) && - (isReadonlySymbol(localOrExportSymbol) || isParameter && !isParameterAssigned(localOrExportSymbol))) { - flowContainer = getControlFlowContainer(flowContainer); - } - // We only look for uninitialized variables in strict null checking mode, and only when we can analyze - // the entire control flow graph from the variable's declaration (i.e. when the flow container and - // declaration container are the same). - var assumeInitialized = !strictNullChecks || (type.flags & 1 /* Any */) !== 0 || isParameter || - isOuterVariable || ts.isInAmbientContext(declaration); - var flowType = getFlowTypeOfReference(node, type, assumeInitialized, flowContainer); - // A variable is considered uninitialized when it is possible to analyze the entire control flow graph - // from declaration to use, and when the variable's declared type doesn't include undefined but the - // control flow based type does include undefined. - if (!assumeInitialized && !(getFalsyFlags(type) & 2048 /* Undefined */) && getFalsyFlags(flowType) & 2048 /* Undefined */) { - error(node, ts.Diagnostics.Variable_0_is_used_before_being_assigned, symbolToString(symbol)); - // Return the declared type to reduce follow-on errors - return type; + return false; + } + function getFlowNodeId(flow) { + if (!flow.id) { + flow.id = nextFlowId; + nextFlowId++; } - return flowType; + return flow.id; } - function isInsideFunction(node, threshold) { - var current = node; - while (current && current !== threshold) { - if (ts.isFunctionLike(current)) { + function typeMaybeAssignableTo(source, target) { + if (!(source.flags & 524288 /* Union */)) { + return isTypeAssignableTo(source, target); + } + for (var _i = 0, _a = source.types; _i < _a.length; _i++) { + var t = _a[_i]; + if (isTypeAssignableTo(t, target)) { return true; } - current = current.parent; } return false; } - function checkNestedBlockScopedBinding(node, symbol) { - if (languageVersion >= 2 /* ES6 */ || - (symbol.flags & (2 /* BlockScopedVariable */ | 32 /* Class */)) === 0 || - symbol.valueDeclaration.parent.kind === 252 /* CatchClause */) { - return; - } - // 1. walk from the use site up to the declaration and check - // if there is anything function like between declaration and use-site (is binding/class is captured in function). - // 2. walk from the declaration up to the boundary of lexical environment and check - // if there is an iteration statement in between declaration and boundary (is binding/class declared inside iteration statement) - var container = ts.getEnclosingBlockScopeContainer(symbol.valueDeclaration); - var usedInFunction = isInsideFunction(node.parent, container); - var current = container; - var containedInIterationStatement = false; - while (current && !ts.nodeStartsNewLexicalEnvironment(current)) { - if (ts.isIterationStatement(current, /*lookInLabeledStatements*/ false)) { - containedInIterationStatement = true; - break; - } - current = current.parent; - } - if (containedInIterationStatement) { - if (usedInFunction) { - // mark iteration statement as containing block-scoped binding captured in some function - getNodeLinks(current).flags |= 65536 /* LoopWithCapturedBlockScopedBinding */; + // Remove those constituent types of declaredType to which no constituent type of assignedType is assignable. + // For example, when a variable of type number | string | boolean is assigned a value of type number | boolean, + // we remove type string. + function getAssignmentReducedType(declaredType, assignedType) { + if (declaredType !== assignedType) { + if (assignedType.flags & 8192 /* Never */) { + return assignedType; } - // mark variables that are declared in loop initializer and reassigned inside the body of ForStatement. - // if body of ForStatement will be converted to function then we'll need a extra machinery to propagate reassigned values back. - if (container.kind === 206 /* ForStatement */ && - ts.getAncestor(symbol.valueDeclaration, 219 /* VariableDeclarationList */).parent === container && - isAssignedInBodyOfForStatement(node, container)) { - getNodeLinks(symbol.valueDeclaration).flags |= 2097152 /* NeedsLoopOutParameter */; + var reducedType = filterType(declaredType, function (t) { return typeMaybeAssignableTo(assignedType, t); }); + if (!(reducedType.flags & 8192 /* Never */)) { + return reducedType; } - // set 'declared inside loop' bit on the block-scoped binding - getNodeLinks(symbol.valueDeclaration).flags |= 262144 /* BlockScopedBindingInLoop */; } - if (usedInFunction) { - getNodeLinks(symbol.valueDeclaration).flags |= 131072 /* CapturedBlockScopedBinding */; + return declaredType; + } + function getTypeFactsOfTypes(types) { + var result = 0 /* None */; + for (var _i = 0, types_11 = types; _i < types_11.length; _i++) { + var t = types_11[_i]; + result |= getTypeFacts(t); } + return result; } - function isAssignedInBodyOfForStatement(node, container) { - var current = node; - // skip parenthesized nodes - while (current.parent.kind === 178 /* ParenthesizedExpression */) { - current = current.parent; + function isFunctionObjectType(type) { + // We do a quick check for a "bind" property before performing the more expensive subtype + // check. This gives us a quicker out in the common case where an object type is not a function. + var resolved = resolveStructuredTypeMembers(type); + return !!(resolved.callSignatures.length || resolved.constructSignatures.length || + resolved.members["bind"] && isTypeSubtypeOf(type, globalFunctionType)); + } + function getTypeFacts(type) { + var flags = type.flags; + if (flags & 2 /* String */) { + return strictNullChecks ? 4079361 /* StringStrictFacts */ : 4194049 /* StringFacts */; } - // check if node is used as LHS in some assignment expression - var isAssigned = false; - if (ts.isAssignmentTarget(current)) { - isAssigned = true; + if (flags & 32 /* StringLiteral */) { + return strictNullChecks ? + type.text === "" ? 3030785 /* EmptyStringStrictFacts */ : 1982209 /* NonEmptyStringStrictFacts */ : + type.text === "" ? 3145473 /* EmptyStringFacts */ : 4194049 /* NonEmptyStringFacts */; } - else if ((current.parent.kind === 185 /* PrefixUnaryExpression */ || current.parent.kind === 186 /* PostfixUnaryExpression */)) { - var expr = current.parent; - isAssigned = expr.operator === 41 /* PlusPlusToken */ || expr.operator === 42 /* MinusMinusToken */; + if (flags & (4 /* Number */ | 16 /* Enum */)) { + return strictNullChecks ? 4079234 /* NumberStrictFacts */ : 4193922 /* NumberFacts */; } - if (!isAssigned) { - return false; + if (flags & (64 /* NumberLiteral */ | 256 /* EnumLiteral */)) { + var isZero = type.text === "0"; + return strictNullChecks ? + isZero ? 3030658 /* ZeroStrictFacts */ : 1982082 /* NonZeroStrictFacts */ : + isZero ? 3145346 /* ZeroFacts */ : 4193922 /* NonZeroFacts */; } - // at this point we know that node is the target of assignment - // now check that modification happens inside the statement part of the ForStatement - while (current !== container) { - if (current === container.statement) { - return true; - } - else { - current = current.parent; - } + if (flags & 8 /* Boolean */) { + return strictNullChecks ? 4078980 /* BooleanStrictFacts */ : 4193668 /* BooleanFacts */; } - return false; - } - function captureLexicalThis(node, container) { - getNodeLinks(node).flags |= 2 /* LexicalThis */; - if (container.kind === 145 /* PropertyDeclaration */ || container.kind === 148 /* Constructor */) { - var classNode = container.parent; - getNodeLinks(classNode).flags |= 4 /* CaptureThis */; + if (flags & 136 /* BooleanLike */) { + return strictNullChecks ? + type === falseType ? 3030404 /* FalseStrictFacts */ : 1981828 /* TrueStrictFacts */ : + type === falseType ? 3145092 /* FalseFacts */ : 4193668 /* TrueFacts */; } - else { - getNodeLinks(container).flags |= 4 /* CaptureThis */; + if (flags & 2588672 /* ObjectType */) { + return isFunctionObjectType(type) ? + strictNullChecks ? 6164448 /* FunctionStrictFacts */ : 8376288 /* FunctionFacts */ : + strictNullChecks ? 6166480 /* ObjectStrictFacts */ : 8378320 /* ObjectFacts */; } - } - function findFirstSuperCall(n) { - if (ts.isSuperCallExpression(n)) { - return n; + if (flags & (1024 /* Void */ | 2048 /* Undefined */)) { + return 2457472 /* UndefinedFacts */; } - else if (ts.isFunctionLike(n)) { - return undefined; + if (flags & 4096 /* Null */) { + return 2340752 /* NullFacts */; } - return ts.forEachChild(n, findFirstSuperCall); + if (flags & 512 /* ESSymbol */) { + return strictNullChecks ? 1981320 /* SymbolStrictFacts */ : 4193160 /* SymbolFacts */; + } + if (flags & 16384 /* TypeParameter */) { + var constraint = getConstraintOfTypeParameter(type); + return getTypeFacts(constraint || emptyObjectType); + } + if (flags & 1572864 /* UnionOrIntersection */) { + return getTypeFactsOfTypes(type.types); + } + return 8388607 /* All */; } - /** - * Return a cached result if super-statement is already found. - * Otherwise, find a super statement in a given constructor function and cache the result in the node-links of the constructor - * - * @param constructor constructor-function to look for super statement - */ - function getSuperCallInConstructor(constructor) { - var links = getNodeLinks(constructor); - // Only trying to find super-call if we haven't yet tried to find one. Once we try, we will record the result - if (links.hasSuperCall === undefined) { - links.superCall = findFirstSuperCall(constructor.body); - links.hasSuperCall = links.superCall ? true : false; + function getTypeWithFacts(type, include) { + return filterType(type, function (t) { return (getTypeFacts(t) & include) !== 0; }); + } + function getTypeWithDefault(type, defaultExpression) { + if (defaultExpression) { + var defaultType = checkExpression(defaultExpression); + return getUnionType([getTypeWithFacts(type, 131072 /* NEUndefined */), defaultType]); } - return links.superCall; + return type; } - /** - * Check if the given class-declaration extends null then return true. - * Otherwise, return false - * @param classDecl a class declaration to check if it extends null - */ - function classDeclarationExtendsNull(classDecl) { - var classSymbol = getSymbolOfNode(classDecl); - var classInstanceType = getDeclaredTypeOfSymbol(classSymbol); - var baseConstructorType = getBaseConstructorTypeOfClass(classInstanceType); - return baseConstructorType === nullWideningType; + function getTypeOfDestructuredProperty(type, name) { + var text = getTextOfPropertyName(name); + return getTypeOfPropertyOfType(type, text) || + isNumericLiteralName(text) && getIndexTypeOfType(type, 1 /* Number */) || + getIndexTypeOfType(type, 0 /* String */) || + unknownType; } - function checkThisExpression(node) { - // Stop at the first arrow function so that we can - // tell whether 'this' needs to be captured. - var container = ts.getThisContainer(node, /* includeArrowFunctions */ true); - var needToCaptureLexicalThis = false; - if (container.kind === 148 /* Constructor */) { - var containingClassDecl = container.parent; - var baseTypeNode = ts.getClassExtendsHeritageClauseElement(containingClassDecl); - // If a containing class does not have extends clause or the class extends null - // skip checking whether super statement is called before "this" accessing. - if (baseTypeNode && !classDeclarationExtendsNull(containingClassDecl)) { - var superCall = getSuperCallInConstructor(container); - // We should give an error in the following cases: - // - No super-call - // - "this" is accessing before super-call. - // i.e super(this) - // this.x; super(); - // We want to make sure that super-call is done before accessing "this" so that - // "this" is not accessed as a parameter of the super-call. - if (!superCall || superCall.end > node.pos) { - // In ES6, super inside constructor of class-declaration has to precede "this" accessing - error(node, ts.Diagnostics.super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class); - } - } + function getTypeOfDestructuredArrayElement(type, index) { + return isTupleLikeType(type) && getTypeOfPropertyOfType(type, "" + index) || + checkIteratedTypeOrElementType(type, /*errorNode*/ undefined, /*allowStringInput*/ false) || + unknownType; + } + function getTypeOfDestructuredSpreadElement(type) { + return createArrayType(checkIteratedTypeOrElementType(type, /*errorNode*/ undefined, /*allowStringInput*/ false) || unknownType); + } + function getAssignedTypeOfBinaryExpression(node) { + return node.parent.kind === 170 /* ArrayLiteralExpression */ || node.parent.kind === 253 /* PropertyAssignment */ ? + getTypeWithDefault(getAssignedType(node), node.right) : + checkExpression(node.right); + } + function getAssignedTypeOfArrayLiteralElement(node, element) { + return getTypeOfDestructuredArrayElement(getAssignedType(node), ts.indexOf(node.elements, element)); + } + function getAssignedTypeOfSpreadElement(node) { + return getTypeOfDestructuredSpreadElement(getAssignedType(node.parent)); + } + function getAssignedTypeOfPropertyAssignment(node) { + return getTypeOfDestructuredProperty(getAssignedType(node.parent), node.name); + } + function getAssignedTypeOfShorthandPropertyAssignment(node) { + return getTypeWithDefault(getAssignedTypeOfPropertyAssignment(node), node.objectAssignmentInitializer); + } + function getAssignedType(node) { + var parent = node.parent; + switch (parent.kind) { + case 207 /* ForInStatement */: + return stringType; + case 208 /* ForOfStatement */: + return checkRightHandSideOfForOf(parent.expression) || unknownType; + case 187 /* BinaryExpression */: + return getAssignedTypeOfBinaryExpression(parent); + case 181 /* DeleteExpression */: + return undefinedType; + case 170 /* ArrayLiteralExpression */: + return getAssignedTypeOfArrayLiteralElement(parent, node); + case 191 /* SpreadElementExpression */: + return getAssignedTypeOfSpreadElement(parent); + case 253 /* PropertyAssignment */: + return getAssignedTypeOfPropertyAssignment(parent); + case 254 /* ShorthandPropertyAssignment */: + return getAssignedTypeOfShorthandPropertyAssignment(parent); } - // Now skip arrow functions to get the "real" owner of 'this'. - if (container.kind === 180 /* ArrowFunction */) { - container = ts.getThisContainer(container, /* includeArrowFunctions */ false); - // When targeting es6, arrow function lexically bind "this" so we do not need to do the work of binding "this" in emitted code - needToCaptureLexicalThis = (languageVersion < 2 /* ES6 */); + return unknownType; + } + function getInitialTypeOfBindingElement(node) { + var pattern = node.parent; + var parentType = getInitialType(pattern.parent); + var type = pattern.kind === 167 /* ObjectBindingPattern */ ? + getTypeOfDestructuredProperty(parentType, node.propertyName || node.name) : + !node.dotDotDotToken ? + getTypeOfDestructuredArrayElement(parentType, ts.indexOf(pattern.elements, node)) : + getTypeOfDestructuredSpreadElement(parentType); + return getTypeWithDefault(type, node.initializer); + } + function getTypeOfInitializer(node) { + // Return the cached type if one is available. If the type of the variable was inferred + // from its initializer, we'll already have cached the type. Otherwise we compute it now + // without caching such that transient types are reflected. + var links = getNodeLinks(node); + return links.resolvedType || checkExpression(node); + } + function getInitialTypeOfVariableDeclaration(node) { + if (node.initializer) { + return getTypeOfInitializer(node.initializer); } - switch (container.kind) { - case 225 /* ModuleDeclaration */: - error(node, ts.Diagnostics.this_cannot_be_referenced_in_a_module_or_namespace_body); - // do not return here so in case if lexical this is captured - it will be reflected in flags on NodeLinks - break; - case 224 /* EnumDeclaration */: - error(node, ts.Diagnostics.this_cannot_be_referenced_in_current_location); - // do not return here so in case if lexical this is captured - it will be reflected in flags on NodeLinks - break; - case 148 /* Constructor */: - if (isInConstructorArgumentInitializer(node, container)) { - error(node, ts.Diagnostics.this_cannot_be_referenced_in_constructor_arguments); - } - break; - case 145 /* PropertyDeclaration */: - case 144 /* PropertySignature */: - if (ts.getModifierFlags(container) & 32 /* Static */) { - error(node, ts.Diagnostics.this_cannot_be_referenced_in_a_static_property_initializer); - } - break; - case 140 /* ComputedPropertyName */: - error(node, ts.Diagnostics.this_cannot_be_referenced_in_a_computed_property_name); - break; + if (node.parent.parent.kind === 207 /* ForInStatement */) { + return stringType; } - if (needToCaptureLexicalThis) { - captureLexicalThis(node, container); + if (node.parent.parent.kind === 208 /* ForOfStatement */) { + return checkRightHandSideOfForOf(node.parent.parent.expression) || unknownType; } - if (ts.isFunctionLike(container) && - (!isInParameterInitializerBeforeContainingFunction(node) || getFunctionLikeThisParameter(container))) { - // Note: a parameter initializer should refer to class-this unless function-this is explicitly annotated. - // If this is a function in a JS file, it might be a class method. Check if it's the RHS - // of a x.prototype.y = function [name]() { .... } - if (container.kind === 179 /* FunctionExpression */ && - ts.isInJavaScriptFile(container.parent) && - ts.getSpecialPropertyAssignmentKind(container.parent) === 3 /* PrototypeProperty */) { - // Get the 'x' of 'x.prototype.y = f' (here, 'f' is 'container') - var className = container.parent // x.prototype.y = f - .left // x.prototype.y - .expression // x.prototype - .expression; // x - var classSymbol = checkExpression(className).symbol; - if (classSymbol && classSymbol.members && (classSymbol.flags & 16 /* Function */)) { - return getInferredClassType(classSymbol); + return unknownType; + } + function getInitialType(node) { + return node.kind === 218 /* VariableDeclaration */ ? + getInitialTypeOfVariableDeclaration(node) : + getInitialTypeOfBindingElement(node); + } + function getInitialOrAssignedType(node) { + return node.kind === 218 /* VariableDeclaration */ || node.kind === 169 /* BindingElement */ ? + getInitialType(node) : + getAssignedType(node); + } + function getReferenceCandidate(node) { + switch (node.kind) { + case 178 /* ParenthesizedExpression */: + return getReferenceCandidate(node.expression); + case 187 /* BinaryExpression */: + switch (node.operatorToken.kind) { + case 56 /* EqualsToken */: + return getReferenceCandidate(node.left); + case 24 /* CommaToken */: + return getReferenceCandidate(node.right); } - } - var thisType = getThisTypeOfDeclaration(container); - if (thisType) { - return thisType; - } - } - if (ts.isClassLike(container.parent)) { - var symbol = getSymbolOfNode(container.parent); - var type = ts.hasModifier(container, 32 /* Static */) ? getTypeOfSymbol(symbol) : getDeclaredTypeOfSymbol(symbol).thisType; - return getFlowTypeOfReference(node, type, /*assumeInitialized*/ true, /*flowContainer*/ undefined); } - if (ts.isInJavaScriptFile(node)) { - var type = getTypeForThisExpressionFromJSDoc(container); - if (type && type !== unknownType) { - return type; - } + return node; + } + function getTypeOfSwitchClause(clause) { + if (clause.kind === 249 /* CaseClause */) { + var caseType = getRegularTypeOfLiteralType(checkExpression(clause.expression)); + return isUnitType(caseType) ? caseType : undefined; } - if (compilerOptions.noImplicitThis) { - // With noImplicitThis, functions may not reference 'this' if it has type 'any' - error(node, ts.Diagnostics.this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation); + return neverType; + } + function getSwitchClauseTypes(switchStatement) { + var links = getNodeLinks(switchStatement); + if (!links.switchTypes) { + // If all case clauses specify expressions that have unit types, we return an array + // of those unit types. Otherwise we return an empty array. + var types = ts.map(switchStatement.caseBlock.clauses, getTypeOfSwitchClause); + links.switchTypes = !ts.contains(types, undefined) ? types : emptyArray; } - return anyType; + return links.switchTypes; } - function getTypeForThisExpressionFromJSDoc(node) { - var typeTag = ts.getJSDocTypeTag(node); - if (typeTag && typeTag.typeExpression && typeTag.typeExpression.type && typeTag.typeExpression.type.kind === 269 /* JSDocFunctionType */) { - var jsDocFunctionType = typeTag.typeExpression.type; - if (jsDocFunctionType.parameters.length > 0 && jsDocFunctionType.parameters[0].type.kind === 272 /* JSDocThisType */) { - return getTypeFromTypeNode(jsDocFunctionType.parameters[0].type); + function eachTypeContainedIn(source, types) { + return source.flags & 524288 /* Union */ ? !ts.forEach(source.types, function (t) { return !ts.contains(types, t); }) : ts.contains(types, source); + } + function isTypeSubsetOf(source, target) { + return source === target || target.flags & 524288 /* Union */ && isTypeSubsetOfUnion(source, target); + } + function isTypeSubsetOfUnion(source, target) { + if (source.flags & 524288 /* Union */) { + for (var _i = 0, _a = source.types; _i < _a.length; _i++) { + var t = _a[_i]; + if (!containsType(target.types, t)) { + return false; + } } + return true; + } + if (source.flags & 256 /* EnumLiteral */ && target.flags & 16 /* Enum */ && source.baseType === target) { + return true; } + return containsType(target.types, source); } - function isInConstructorArgumentInitializer(node, constructorDecl) { - for (var n = node; n && n !== constructorDecl; n = n.parent) { - if (n.kind === 142 /* Parameter */) { - return true; - } + function filterType(type, f) { + if (type.flags & 524288 /* Union */) { + var types = type.types; + var filtered = ts.filter(types, f); + return filtered === types ? type : getUnionTypeFromSortedList(filtered); } - return false; + return f(type) ? type : neverType; } - function checkSuperExpression(node) { - var isCallExpression = node.parent.kind === 174 /* CallExpression */ && node.parent.expression === node; - var container = ts.getSuperContainer(node, /*stopOnFunctions*/ true); - var needToCaptureLexicalThis = false; - if (!isCallExpression) { - // adjust the container reference in case if super is used inside arrow functions with arbitrary deep nesting - while (container && container.kind === 180 /* ArrowFunction */) { - container = ts.getSuperContainer(container, /*stopOnFunctions*/ true); - needToCaptureLexicalThis = languageVersion < 2 /* ES6 */; - } + function mapType(type, f) { + return type.flags & 524288 /* Union */ ? getUnionType(ts.map(type.types, f)) : f(type); + } + function extractTypesOfKind(type, kind) { + return filterType(type, function (t) { return (t.flags & kind) !== 0; }); + } + // Return a new type in which occurrences of the string and number primitive types in + // typeWithPrimitives have been replaced with occurrences of string literals and numeric + // literals in typeWithLiterals, respectively. + function replacePrimitivesWithLiterals(typeWithPrimitives, typeWithLiterals) { + if (isTypeSubsetOf(stringType, typeWithPrimitives) && maybeTypeOfKind(typeWithLiterals, 32 /* StringLiteral */) || + isTypeSubsetOf(numberType, typeWithPrimitives) && maybeTypeOfKind(typeWithLiterals, 64 /* NumberLiteral */)) { + return mapType(typeWithPrimitives, function (t) { + return t.flags & 2 /* String */ ? extractTypesOfKind(typeWithLiterals, 2 /* String */ | 32 /* StringLiteral */) : + t.flags & 4 /* Number */ ? extractTypesOfKind(typeWithLiterals, 4 /* Number */ | 64 /* NumberLiteral */) : + t; + }); } - var canUseSuperExpression = isLegalUsageOfSuperExpression(container); - var nodeCheckFlag = 0; - if (!canUseSuperExpression) { - // issue more specific error if super is used in computed property name - // class A { foo() { return "1" }} - // class B { - // [super.foo()]() {} - // } - var current = node; - while (current && current !== container && current.kind !== 140 /* ComputedPropertyName */) { - current = current.parent; - } - if (current && current.kind === 140 /* ComputedPropertyName */) { - error(node, ts.Diagnostics.super_cannot_be_referenced_in_a_computed_property_name); - } - else if (isCallExpression) { - error(node, ts.Diagnostics.Super_calls_are_not_permitted_outside_constructors_or_in_nested_functions_inside_constructors); - } - else if (!container || !container.parent || !(ts.isClassLike(container.parent) || container.parent.kind === 171 /* ObjectLiteralExpression */)) { - error(node, ts.Diagnostics.super_can_only_be_referenced_in_members_of_derived_classes_or_object_literal_expressions); - } - else { - error(node, ts.Diagnostics.super_property_access_is_permitted_only_in_a_constructor_member_function_or_member_accessor_of_a_derived_class); - } - return unknownType; - } - if ((ts.getModifierFlags(container) & 32 /* Static */) || isCallExpression) { - nodeCheckFlag = 512 /* SuperStatic */; + return typeWithPrimitives; + } + function isIncomplete(flowType) { + return flowType.flags === 0; + } + function getTypeFromFlowType(flowType) { + return flowType.flags === 0 ? flowType.type : flowType; + } + function createFlowType(type, incomplete) { + return incomplete ? { flags: 0, type: type } : type; + } + function getFlowTypeOfReference(reference, declaredType, assumeInitialized, flowContainer) { + var key; + if (!reference.flowNode || assumeInitialized && !(declaredType.flags & 4178943 /* Narrowable */)) { + return declaredType; } - else { - nodeCheckFlag = 256 /* SuperInstance */; + var initialType = assumeInitialized ? declaredType : + declaredType === autoType ? undefinedType : + includeFalsyTypes(declaredType, 2048 /* Undefined */); + var visitedFlowStart = visitedFlowCount; + var result = getTypeFromFlowType(getTypeAtFlowNode(reference.flowNode)); + visitedFlowCount = visitedFlowStart; + if (reference.parent.kind === 196 /* NonNullExpression */ && getTypeWithFacts(result, 524288 /* NEUndefinedOrNull */).flags & 8192 /* Never */) { + return declaredType; } - getNodeLinks(node).flags |= nodeCheckFlag; - // Due to how we emit async functions, we need to specialize the emit for an async method that contains a `super` reference. - // This is due to the fact that we emit the body of an async function inside of a generator function. As generator - // functions cannot reference `super`, we emit a helper inside of the method body, but outside of the generator. This helper - // uses an arrow function, which is permitted to reference `super`. - // - // There are two primary ways we can access `super` from within an async method. The first is getting the value of a property - // or indexed access on super, either as part of a right-hand-side expression or call expression. The second is when setting the value - // of a property or indexed access, either as part of an assignment expression or destructuring assignment. - // - // The simplest case is reading a value, in which case we will emit something like the following: - // - // // ts - // ... - // async asyncMethod() { - // let x = await super.asyncMethod(); - // return x; - // } - // ... - // - // // js - // ... - // asyncMethod() { - // const _super = name => super[name]; - // return __awaiter(this, arguments, Promise, function *() { - // let x = yield _super("asyncMethod").call(this); - // return x; - // }); - // } - // ... - // - // The more complex case is when we wish to assign a value, especially as part of a destructuring assignment. As both cases - // are legal in ES6, but also likely less frequent, we emit the same more complex helper for both scenarios: - // - // // ts - // ... - // async asyncMethod(ar: Promise) { - // [super.a, super.b] = await ar; - // } - // ... - // - // // js - // ... - // asyncMethod(ar) { - // const _super = (function (geti, seti) { - // const cache = Object.create(null); - // return name => cache[name] || (cache[name] = { get value() { return geti(name); }, set value(v) { seti(name, v); } }); - // })(name => super[name], (name, value) => super[name] = value); - // return __awaiter(this, arguments, Promise, function *() { - // [_super("a").value, _super("b").value] = yield ar; - // }); - // } - // ... - // - // This helper creates an object with a "value" property that wraps the `super` property or indexed access for both get and set. - // This is required for destructuring assignments, as a call expression cannot be used as the target of a destructuring assignment - // while a property access can. - if (container.kind === 147 /* MethodDeclaration */ && ts.getModifierFlags(container) & 256 /* Async */) { - if (ts.isSuperProperty(node.parent) && ts.isAssignmentTarget(node.parent)) { - getNodeLinks(container).flags |= 4096 /* AsyncMethodWithSuperBinding */; - } - else { - getNodeLinks(container).flags |= 2048 /* AsyncMethodWithSuper */; + return result; + function getTypeAtFlowNode(flow) { + while (true) { + if (flow.flags & 512 /* Shared */) { + // We cache results of flow type resolution for shared nodes that were previously visited in + // the same getFlowTypeOfReference invocation. A node is considered shared when it is the + // antecedent of more than one node. + for (var i = visitedFlowStart; i < visitedFlowCount; i++) { + if (visitedFlowNodes[i] === flow) { + return visitedFlowTypes[i]; + } + } + } + var type = void 0; + if (flow.flags & 16 /* Assignment */) { + type = getTypeAtFlowAssignment(flow); + if (!type) { + flow = flow.antecedent; + continue; + } + } + else if (flow.flags & 96 /* Condition */) { + type = getTypeAtFlowCondition(flow); + } + else if (flow.flags & 128 /* SwitchClause */) { + type = getTypeAtSwitchClause(flow); + } + else if (flow.flags & 12 /* Label */) { + if (flow.antecedents.length === 1) { + flow = flow.antecedents[0]; + continue; + } + type = flow.flags & 4 /* BranchLabel */ ? + getTypeAtFlowBranchLabel(flow) : + getTypeAtFlowLoopLabel(flow); + } + else if (flow.flags & 2 /* Start */) { + // Check if we should continue with the control flow of the containing function. + var container = flow.container; + if (container && container !== flowContainer && reference.kind !== 172 /* PropertyAccessExpression */) { + flow = container.flowNode; + continue; + } + // At the top of the flow we have the initial type. + type = initialType; + } + else { + // Unreachable code errors are reported in the binding phase. Here we + // simply return the declared type to reduce follow-on errors. + type = declaredType; + } + if (flow.flags & 512 /* Shared */) { + // Record visited node and the associated type in the cache. + visitedFlowNodes[visitedFlowCount] = flow; + visitedFlowTypes[visitedFlowCount] = type; + visitedFlowCount++; + } + return type; } } - if (needToCaptureLexicalThis) { - // call expressions are allowed only in constructors so they should always capture correct 'this' - // super property access expressions can also appear in arrow functions - - // in this case they should also use correct lexical this - captureLexicalThis(node.parent, container); - } - if (container.parent.kind === 171 /* ObjectLiteralExpression */) { - if (languageVersion < 2 /* ES6 */) { - error(node, ts.Diagnostics.super_is_only_allowed_in_members_of_object_literal_expressions_when_option_target_is_ES2015_or_higher); - return unknownType; - } - else { - // for object literal assume that type of 'super' is 'any' - return anyType; + function getTypeAtFlowAssignment(flow) { + var node = flow.node; + // Assignments only narrow the computed type if the declared type is a union type. Thus, we + // only need to evaluate the assigned type if the declared type is a union type. + if (isMatchingReference(reference, node)) { + if (node.parent.kind === 185 /* PrefixUnaryExpression */ || node.parent.kind === 186 /* PostfixUnaryExpression */) { + var flowType = getTypeAtFlowNode(flow.antecedent); + return createFlowType(getBaseTypeOfLiteralType(getTypeFromFlowType(flowType)), isIncomplete(flowType)); + } + return declaredType === autoType ? getBaseTypeOfLiteralType(getInitialOrAssignedType(node)) : + declaredType.flags & 524288 /* Union */ ? getAssignmentReducedType(declaredType, getInitialOrAssignedType(node)) : + declaredType; } - } - // at this point the only legal case for parent is ClassLikeDeclaration - var classLikeDeclaration = container.parent; - var classType = getDeclaredTypeOfSymbol(getSymbolOfNode(classLikeDeclaration)); - var baseClassType = classType && getBaseTypes(classType)[0]; - if (!baseClassType) { - if (!ts.getClassExtendsHeritageClauseElement(classLikeDeclaration)) { - error(node, ts.Diagnostics.super_can_only_be_referenced_in_a_derived_class); + // We didn't have a direct match. However, if the reference is a dotted name, this + // may be an assignment to a left hand part of the reference. For example, for a + // reference 'x.y.z', we may be at an assignment to 'x.y' or 'x'. In that case, + // return the declared type. + if (containsMatchingReference(reference, node)) { + return declaredType; } - return unknownType; - } - if (container.kind === 148 /* Constructor */ && isInConstructorArgumentInitializer(node, container)) { - // issue custom error message for super property access in constructor arguments (to be aligned with old compiler) - error(node, ts.Diagnostics.super_cannot_be_referenced_in_constructor_arguments); - return unknownType; + // Assignment doesn't affect reference + return undefined; } - return nodeCheckFlag === 512 /* SuperStatic */ - ? getBaseConstructorTypeOfClass(classType) - : getTypeWithThisArgument(baseClassType, classType.thisType); - function isLegalUsageOfSuperExpression(container) { - if (!container) { - return false; - } - if (isCallExpression) { - // TS 1.0 SPEC (April 2014): 4.8.1 - // Super calls are only permitted in constructors of derived classes - return container.kind === 148 /* Constructor */; - } - else { - // TS 1.0 SPEC (April 2014) - // 'super' property access is allowed - // - In a constructor, instance member function, instance member accessor, or instance member variable initializer where this references a derived class instance - // - In a static member function or static member accessor - // topmost container must be something that is directly nested in the class declaration\object literal expression - if (ts.isClassLike(container.parent) || container.parent.kind === 171 /* ObjectLiteralExpression */) { - if (ts.getModifierFlags(container) & 32 /* Static */) { - return container.kind === 147 /* MethodDeclaration */ || - container.kind === 146 /* MethodSignature */ || - container.kind === 149 /* GetAccessor */ || - container.kind === 150 /* SetAccessor */; - } - else { - return container.kind === 147 /* MethodDeclaration */ || - container.kind === 146 /* MethodSignature */ || - container.kind === 149 /* GetAccessor */ || - container.kind === 150 /* SetAccessor */ || - container.kind === 145 /* PropertyDeclaration */ || - container.kind === 144 /* PropertySignature */ || - container.kind === 148 /* Constructor */; - } + function getTypeAtFlowCondition(flow) { + var flowType = getTypeAtFlowNode(flow.antecedent); + var type = getTypeFromFlowType(flowType); + if (!(type.flags & 8192 /* Never */)) { + // If we have an antecedent type (meaning we're reachable in some way), we first + // attempt to narrow the antecedent type. If that produces the never type, and if + // the antecedent type is incomplete (i.e. a transient type in a loop), then we + // take the type guard as an indication that control *could* reach here once we + // have the complete type. We proceed by switching to the silent never type which + // doesn't report errors when operators are applied to it. Note that this is the + // *only* place a silent never type is ever generated. + var assumeTrue = (flow.flags & 32 /* TrueCondition */) !== 0; + type = narrowType(type, flow.expression, assumeTrue); + if (type.flags & 8192 /* Never */ && isIncomplete(flowType)) { + type = silentNeverType; } } - return false; + return createFlowType(type, isIncomplete(flowType)); } - } - function getContextualThisParameter(func) { - if (isContextSensitiveFunctionOrObjectLiteralMethod(func) && func.kind !== 180 /* ArrowFunction */) { - var contextualSignature = getContextualSignature(func); - if (contextualSignature) { - return contextualSignature.thisParameter; + function getTypeAtSwitchClause(flow) { + var flowType = getTypeAtFlowNode(flow.antecedent); + var type = getTypeFromFlowType(flowType); + var expr = flow.switchStatement.expression; + if (isMatchingReference(reference, expr)) { + type = narrowTypeBySwitchOnDiscriminant(type, flow.switchStatement, flow.clauseStart, flow.clauseEnd); + } + else if (isMatchingReferenceDiscriminant(expr)) { + type = narrowTypeByDiscriminant(type, expr, function (t) { return narrowTypeBySwitchOnDiscriminant(t, flow.switchStatement, flow.clauseStart, flow.clauseEnd); }); } + return createFlowType(type, isIncomplete(flowType)); } - return undefined; - } - // Return contextual type of parameter or undefined if no contextual type is available - function getContextuallyTypedParameterType(parameter) { - var func = parameter.parent; - if (isContextSensitiveFunctionOrObjectLiteralMethod(func)) { - var iife = ts.getImmediatelyInvokedFunctionExpression(func); - if (iife) { - var indexOfParameter = ts.indexOf(func.parameters, parameter); - if (iife.arguments && indexOfParameter < iife.arguments.length) { - if (parameter.dotDotDotToken) { - var restTypes = []; - for (var i = indexOfParameter; i < iife.arguments.length; i++) { - restTypes.push(getWidenedLiteralType(checkExpression(iife.arguments[i]))); - } - return createArrayType(getUnionType(restTypes)); - } - var links = getNodeLinks(iife); - var cached = links.resolvedSignature; - links.resolvedSignature = anySignature; - var type = getWidenedLiteralType(checkExpression(iife.arguments[indexOfParameter])); - links.resolvedSignature = cached; + function getTypeAtFlowBranchLabel(flow) { + var antecedentTypes = []; + var subtypeReduction = false; + var seenIncomplete = false; + for (var _i = 0, _a = flow.antecedents; _i < _a.length; _i++) { + var antecedent = _a[_i]; + var flowType = getTypeAtFlowNode(antecedent); + var type = getTypeFromFlowType(flowType); + // If the type at a particular antecedent path is the declared type and the + // reference is known to always be assigned (i.e. when declared and initial types + // are the same), there is no reason to process more antecedents since the only + // possible outcome is subtypes that will be removed in the final union type anyway. + if (type === declaredType && declaredType === initialType) { return type; } - } - var contextualSignature = getContextualSignature(func); - if (contextualSignature) { - var funcHasRestParameters = ts.hasRestParameter(func); - var len = func.parameters.length - (funcHasRestParameters ? 1 : 0); - var indexOfParameter = ts.indexOf(func.parameters, parameter); - if (indexOfParameter < len) { - return getTypeAtPosition(contextualSignature, indexOfParameter); + if (!ts.contains(antecedentTypes, type)) { + antecedentTypes.push(type); } - // If last parameter is contextually rest parameter get its type - if (funcHasRestParameters && - indexOfParameter === (func.parameters.length - 1) && - isRestParameterIndex(contextualSignature, func.parameters.length - 1)) { - return getTypeOfSymbol(ts.lastOrUndefined(contextualSignature.parameters)); + // If an antecedent type is not a subset of the declared type, we need to perform + // subtype reduction. This happens when a "foreign" type is injected into the control + // flow using the instanceof operator or a user defined type predicate. + if (!isTypeSubsetOf(type, declaredType)) { + subtypeReduction = true; + } + if (isIncomplete(flowType)) { + seenIncomplete = true; } } + return createFlowType(getUnionType(antecedentTypes, subtypeReduction), seenIncomplete); } - return undefined; - } - // In a variable, parameter or property declaration with a type annotation, - // the contextual type of an initializer expression is the type of the variable, parameter or property. - // Otherwise, in a parameter declaration of a contextually typed function expression, - // the contextual type of an initializer expression is the contextual type of the parameter. - // Otherwise, in a variable or parameter declaration with a binding pattern name, - // the contextual type of an initializer expression is the type implied by the binding pattern. - // Otherwise, in a binding pattern inside a variable or parameter declaration, - // the contextual type of an initializer expression is the type annotation of the containing declaration, if present. - function getContextualTypeForInitializerExpression(node) { - var declaration = node.parent; - if (node === declaration.initializer) { - if (declaration.type) { - return getTypeFromTypeNode(declaration.type); + function getTypeAtFlowLoopLabel(flow) { + // If we have previously computed the control flow type for the reference at + // this flow loop junction, return the cached type. + var id = getFlowNodeId(flow); + var cache = flowLoopCaches[id] || (flowLoopCaches[id] = ts.createMap()); + if (!key) { + key = getFlowCacheKey(reference); } - if (declaration.kind === 142 /* Parameter */) { - var type = getContextuallyTypedParameterType(declaration); - if (type) { - return type; - } + if (cache[key]) { + return cache[key]; } - if (ts.isBindingPattern(declaration.name)) { - return getTypeFromBindingPattern(declaration.name, /*includePatternInType*/ true, /*reportErrors*/ false); + // If this flow loop junction and reference are already being processed, return + // the union of the types computed for each branch so far, marked as incomplete. + // We should never see an empty array here because the first antecedent of a loop + // junction is always the non-looping control flow path that leads to the top. + for (var i = flowLoopStart; i < flowLoopCount; i++) { + if (flowLoopNodes[i] === flow && flowLoopKeys[i] === key) { + return createFlowType(getUnionType(flowLoopTypes[i]), /*incomplete*/ true); + } } - if (ts.isBindingPattern(declaration.parent)) { - var parentDeclaration = declaration.parent.parent; - var name_15 = declaration.propertyName || declaration.name; - if (ts.isVariableLike(parentDeclaration) && - parentDeclaration.type && - !ts.isBindingPattern(name_15)) { - var text = getTextOfPropertyName(name_15); - if (text) { - return getTypeOfPropertyOfType(getTypeFromTypeNode(parentDeclaration.type), text); - } + // Add the flow loop junction and reference to the in-process stack and analyze + // each antecedent code path. + var antecedentTypes = []; + var subtypeReduction = false; + var firstAntecedentType; + flowLoopNodes[flowLoopCount] = flow; + flowLoopKeys[flowLoopCount] = key; + flowLoopTypes[flowLoopCount] = antecedentTypes; + for (var _i = 0, _a = flow.antecedents; _i < _a.length; _i++) { + var antecedent = _a[_i]; + flowLoopCount++; + var flowType = getTypeAtFlowNode(antecedent); + flowLoopCount--; + if (!firstAntecedentType) { + firstAntecedentType = flowType; + } + var type = getTypeFromFlowType(flowType); + // If we see a value appear in the cache it is a sign that control flow analysis + // was restarted and completed by checkExpressionCached. We can simply pick up + // the resulting type and bail out. + if (cache[key]) { + return cache[key]; + } + if (!ts.contains(antecedentTypes, type)) { + antecedentTypes.push(type); + } + // If an antecedent type is not a subset of the declared type, we need to perform + // subtype reduction. This happens when a "foreign" type is injected into the control + // flow using the instanceof operator or a user defined type predicate. + if (!isTypeSubsetOf(type, declaredType)) { + subtypeReduction = true; + } + // If the type at a particular antecedent path is the declared type there is no + // reason to process more antecedents since the only possible outcome is subtypes + // that will be removed in the final union type anyway. + if (type === declaredType) { + break; } } - } - return undefined; - } - function getContextualTypeForReturnExpression(node) { - var func = ts.getContainingFunction(node); - if (ts.isAsyncFunctionLike(func)) { - var contextualReturnType = getContextualReturnType(func); - if (contextualReturnType) { - return getPromisedType(contextualReturnType); + // The result is incomplete if the first antecedent (the non-looping control flow path) + // is incomplete. + var result = getUnionType(antecedentTypes, subtypeReduction); + if (isIncomplete(firstAntecedentType)) { + return createFlowType(result, /*incomplete*/ true); } - return undefined; + return cache[key] = result; } - if (func && !func.asteriskToken) { - return getContextualReturnType(func); + function isMatchingReferenceDiscriminant(expr) { + return expr.kind === 172 /* PropertyAccessExpression */ && + declaredType.flags & 524288 /* Union */ && + isMatchingReference(reference, expr.expression) && + isDiscriminantProperty(declaredType, expr.name.text); } - return undefined; - } - function getContextualTypeForYieldOperand(node) { - var func = ts.getContainingFunction(node); - if (func) { - var contextualReturnType = getContextualReturnType(func); - if (contextualReturnType) { - return node.asteriskToken - ? contextualReturnType - : getElementTypeOfIterableIterator(contextualReturnType); + function narrowTypeByDiscriminant(type, propAccess, narrowType) { + var propName = propAccess.name.text; + var propType = getTypeOfPropertyOfType(type, propName); + var narrowedPropType = propType && narrowType(propType); + return propType === narrowedPropType ? type : filterType(type, function (t) { return isTypeComparableTo(getTypeOfPropertyOfType(t, propName), narrowedPropType); }); + } + function narrowTypeByTruthiness(type, expr, assumeTrue) { + if (isMatchingReference(reference, expr)) { + return getTypeWithFacts(type, assumeTrue ? 1048576 /* Truthy */ : 2097152 /* Falsy */); + } + if (isMatchingReferenceDiscriminant(expr)) { + return narrowTypeByDiscriminant(type, expr, function (t) { return getTypeWithFacts(t, assumeTrue ? 1048576 /* Truthy */ : 2097152 /* Falsy */); }); } + if (containsMatchingReferenceDiscriminant(reference, expr)) { + return declaredType; + } + return type; } - return undefined; - } - function isInParameterInitializerBeforeContainingFunction(node) { - while (node.parent && !ts.isFunctionLike(node.parent)) { - if (node.parent.kind === 142 /* Parameter */ && node.parent.initializer === node) { - return true; + function narrowTypeByBinaryExpression(type, expr, assumeTrue) { + switch (expr.operatorToken.kind) { + case 56 /* EqualsToken */: + return narrowTypeByTruthiness(type, expr.left, assumeTrue); + case 30 /* EqualsEqualsToken */: + case 31 /* ExclamationEqualsToken */: + case 32 /* EqualsEqualsEqualsToken */: + case 33 /* ExclamationEqualsEqualsToken */: + var operator_1 = expr.operatorToken.kind; + var left_1 = getReferenceCandidate(expr.left); + var right_1 = getReferenceCandidate(expr.right); + if (left_1.kind === 182 /* TypeOfExpression */ && right_1.kind === 9 /* StringLiteral */) { + return narrowTypeByTypeof(type, left_1, operator_1, right_1, assumeTrue); + } + if (right_1.kind === 182 /* TypeOfExpression */ && left_1.kind === 9 /* StringLiteral */) { + return narrowTypeByTypeof(type, right_1, operator_1, left_1, assumeTrue); + } + if (isMatchingReference(reference, left_1)) { + return narrowTypeByEquality(type, operator_1, right_1, assumeTrue); + } + if (isMatchingReference(reference, right_1)) { + return narrowTypeByEquality(type, operator_1, left_1, assumeTrue); + } + if (isMatchingReferenceDiscriminant(left_1)) { + return narrowTypeByDiscriminant(type, left_1, function (t) { return narrowTypeByEquality(t, operator_1, right_1, assumeTrue); }); + } + if (isMatchingReferenceDiscriminant(right_1)) { + return narrowTypeByDiscriminant(type, right_1, function (t) { return narrowTypeByEquality(t, operator_1, left_1, assumeTrue); }); + } + if (containsMatchingReferenceDiscriminant(reference, left_1) || containsMatchingReferenceDiscriminant(reference, right_1)) { + return declaredType; + } + break; + case 91 /* InstanceOfKeyword */: + return narrowTypeByInstanceof(type, expr, assumeTrue); + case 24 /* CommaToken */: + return narrowType(type, expr.right, assumeTrue); } - node = node.parent; + return type; } - return false; - } - function getContextualReturnType(functionDecl) { - // If the containing function has a return type annotation, is a constructor, or is a get accessor whose - // corresponding set accessor has a type annotation, return statements in the function are contextually typed - if (functionDecl.type || - functionDecl.kind === 148 /* Constructor */ || - functionDecl.kind === 149 /* GetAccessor */ && ts.getSetAccessorTypeAnnotationNode(ts.getDeclarationOfKind(functionDecl.symbol, 150 /* SetAccessor */))) { - return getReturnTypeOfSignature(getSignatureFromDeclaration(functionDecl)); - } - // Otherwise, if the containing function is contextually typed by a function type with exactly one call signature - // and that call signature is non-generic, return statements are contextually typed by the return type of the signature - var signature = getContextualSignatureForFunctionLikeDeclaration(functionDecl); - if (signature) { - return getReturnTypeOfSignature(signature); - } - return undefined; - } - // In a typed function call, an argument or substitution expression is contextually typed by the type of the corresponding parameter. - function getContextualTypeForArgument(callTarget, arg) { - var args = getEffectiveCallArguments(callTarget); - var argIndex = ts.indexOf(args, arg); - if (argIndex >= 0) { - var signature = getResolvedOrAnySignature(callTarget); - return getTypeAtPosition(signature, argIndex); + function narrowTypeByEquality(type, operator, value, assumeTrue) { + if (type.flags & 1 /* Any */) { + return type; + } + if (operator === 31 /* ExclamationEqualsToken */ || operator === 33 /* ExclamationEqualsEqualsToken */) { + assumeTrue = !assumeTrue; + } + var valueType = checkExpression(value); + if (valueType.flags & 6144 /* Nullable */) { + if (!strictNullChecks) { + return type; + } + var doubleEquals = operator === 30 /* EqualsEqualsToken */ || operator === 31 /* ExclamationEqualsToken */; + var facts = doubleEquals ? + assumeTrue ? 65536 /* EQUndefinedOrNull */ : 524288 /* NEUndefinedOrNull */ : + value.kind === 93 /* NullKeyword */ ? + assumeTrue ? 32768 /* EQNull */ : 262144 /* NENull */ : + assumeTrue ? 16384 /* EQUndefined */ : 131072 /* NEUndefined */; + return getTypeWithFacts(type, facts); + } + if (type.flags & 2589185 /* NotUnionOrUnit */) { + return type; + } + if (assumeTrue) { + var narrowedType = filterType(type, function (t) { return areTypesComparable(t, valueType); }); + return narrowedType.flags & 8192 /* Never */ ? type : replacePrimitivesWithLiterals(narrowedType, valueType); + } + if (isUnitType(valueType)) { + var regularType_1 = getRegularTypeOfLiteralType(valueType); + return filterType(type, function (t) { return getRegularTypeOfLiteralType(t) !== regularType_1; }); + } + return type; } - return undefined; - } - function getContextualTypeForSubstitutionExpression(template, substitutionExpression) { - if (template.parent.kind === 176 /* TaggedTemplateExpression */) { - return getContextualTypeForArgument(template.parent, substitutionExpression); + function narrowTypeByTypeof(type, typeOfExpr, operator, literal, assumeTrue) { + // We have '==', '!=', '====', or !==' operator with 'typeof xxx' and string literal operands + var target = getReferenceCandidate(typeOfExpr.expression); + if (!isMatchingReference(reference, target)) { + // For a reference of the form 'x.y', a 'typeof x === ...' type guard resets the + // narrowed type of 'y' to its declared type. + if (containsMatchingReference(reference, target)) { + return declaredType; + } + return type; + } + if (operator === 31 /* ExclamationEqualsToken */ || operator === 33 /* ExclamationEqualsEqualsToken */) { + assumeTrue = !assumeTrue; + } + if (assumeTrue && !(type.flags & 524288 /* Union */)) { + // We narrow a non-union type to an exact primitive type if the non-union type + // is a supertype of that primitive type. For example, type 'any' can be narrowed + // to one of the primitive types. + var targetType = typeofTypesByName[literal.text]; + if (targetType && isTypeSubtypeOf(targetType, type)) { + return targetType; + } + } + var facts = assumeTrue ? + typeofEQFacts[literal.text] || 64 /* TypeofEQHostObject */ : + typeofNEFacts[literal.text] || 8192 /* TypeofNEHostObject */; + return getTypeWithFacts(type, facts); } - return undefined; - } - function getContextualTypeForBinaryOperand(node) { - var binaryExpression = node.parent; - var operator = binaryExpression.operatorToken.kind; - if (operator >= 56 /* FirstAssignment */ && operator <= 68 /* LastAssignment */) { - // Don't do this for special property assignments to avoid circularity - if (ts.getSpecialPropertyAssignmentKind(binaryExpression) !== 0 /* None */) { - return undefined; + function narrowTypeBySwitchOnDiscriminant(type, switchStatement, clauseStart, clauseEnd) { + // We only narrow if all case expressions specify values with unit types + var switchTypes = getSwitchClauseTypes(switchStatement); + if (!switchTypes.length) { + return type; } - // In an assignment expression, the right operand is contextually typed by the type of the left operand. - if (node === binaryExpression.right) { - return checkExpression(binaryExpression.left); + var clauseTypes = switchTypes.slice(clauseStart, clauseEnd); + var hasDefaultClause = clauseStart === clauseEnd || ts.contains(clauseTypes, neverType); + var discriminantType = getUnionType(clauseTypes); + var caseType = discriminantType.flags & 8192 /* Never */ ? neverType : + replacePrimitivesWithLiterals(filterType(type, function (t) { return isTypeComparableTo(discriminantType, t); }), discriminantType); + if (!hasDefaultClause) { + return caseType; } + var defaultType = filterType(type, function (t) { return !(isUnitType(t) && ts.contains(switchTypes, getRegularTypeOfLiteralType(t))); }); + return caseType.flags & 8192 /* Never */ ? defaultType : getUnionType([caseType, defaultType]); } - else if (operator === 52 /* BarBarToken */) { - // When an || expression has a contextual type, the operands are contextually typed by that type. When an || - // expression has no contextual type, the right operand is contextually typed by the type of the left operand. - var type = getContextualType(binaryExpression); - if (!type && node === binaryExpression.right) { - type = checkExpression(binaryExpression.left); + function narrowTypeByInstanceof(type, expr, assumeTrue) { + var left = getReferenceCandidate(expr.left); + if (!isMatchingReference(reference, left)) { + // For a reference of the form 'x.y', an 'x instanceof T' type guard resets the + // narrowed type of 'y' to its declared type. + if (containsMatchingReference(reference, left)) { + return declaredType; + } + return type; + } + // Check that right operand is a function type with a prototype property + var rightType = checkExpression(expr.right); + if (!isTypeSubtypeOf(rightType, globalFunctionType)) { + return type; + } + var targetType; + var prototypeProperty = getPropertyOfType(rightType, "prototype"); + if (prototypeProperty) { + // Target type is type of the prototype property + var prototypePropertyType = getTypeOfSymbol(prototypeProperty); + if (!isTypeAny(prototypePropertyType)) { + targetType = prototypePropertyType; + } + } + // Don't narrow from 'any' if the target type is exactly 'Object' or 'Function' + if (isTypeAny(type) && (targetType === globalObjectType || targetType === globalFunctionType)) { + return type; + } + if (!targetType) { + // Target type is type of construct signature + var constructSignatures = void 0; + if (rightType.flags & 65536 /* Interface */) { + constructSignatures = resolveDeclaredMembers(rightType).declaredConstructSignatures; + } + else if (rightType.flags & 2097152 /* Anonymous */) { + constructSignatures = getSignaturesOfType(rightType, 1 /* Construct */); + } + if (constructSignatures && constructSignatures.length) { + targetType = getUnionType(ts.map(constructSignatures, function (signature) { return getReturnTypeOfSignature(getErasedSignature(signature)); })); + } + } + if (targetType) { + return getNarrowedType(type, targetType, assumeTrue); } return type; } - else if (operator === 51 /* AmpersandAmpersandToken */ || operator === 24 /* CommaToken */) { - if (node === binaryExpression.right) { - return getContextualType(binaryExpression); + function getNarrowedType(type, candidate, assumeTrue) { + if (!assumeTrue) { + return filterType(type, function (t) { return !isTypeInstanceOf(t, candidate); }); } - } - return undefined; - } - // Apply a mapping function to a contextual type and return the resulting type. If the contextual type - // is a union type, the mapping function is applied to each constituent type and a union of the resulting - // types is returned. - function applyToContextualType(type, mapper) { - if (!(type.flags & 524288 /* Union */)) { - return mapper(type); - } - var types = type.types; - var mappedType; - var mappedTypes; - for (var _i = 0, types_12 = types; _i < types_12.length; _i++) { - var current = types_12[_i]; - var t = mapper(current); - if (t) { - if (!mappedType) { - mappedType = t; + // If the current type is a union type, remove all constituents that couldn't be instances of + // the candidate type. If one or more constituents remain, return a union of those. + if (type.flags & 524288 /* Union */) { + var assignableType = filterType(type, function (t) { return isTypeInstanceOf(t, candidate); }); + if (!(assignableType.flags & 8192 /* Never */)) { + return assignableType; } - else if (!mappedTypes) { - mappedTypes = [mappedType, t]; + } + // If the candidate type is a subtype of the target type, narrow to the candidate type. + // Otherwise, if the target type is assignable to the candidate type, keep the target type. + // Otherwise, if the candidate type is assignable to the target type, narrow to the candidate + // type. Otherwise, the types are completely unrelated, so narrow to an intersection of the + // two types. + var targetType = type.flags & 16384 /* TypeParameter */ ? getApparentType(type) : type; + return isTypeSubtypeOf(candidate, type) ? candidate : + isTypeAssignableTo(type, candidate) ? type : + isTypeAssignableTo(candidate, targetType) ? candidate : + getIntersectionType([type, candidate]); + } + function narrowTypeByTypePredicate(type, callExpression, assumeTrue) { + if (!hasMatchingArgument(callExpression, reference)) { + return type; + } + var signature = getResolvedSignature(callExpression); + var predicate = signature.typePredicate; + if (!predicate) { + return type; + } + // Don't narrow from 'any' if the predicate type is exactly 'Object' or 'Function' + if (isTypeAny(type) && (predicate.type === globalObjectType || predicate.type === globalFunctionType)) { + return type; + } + if (ts.isIdentifierTypePredicate(predicate)) { + var predicateArgument = callExpression.arguments[predicate.parameterIndex]; + if (predicateArgument) { + if (isMatchingReference(reference, predicateArgument)) { + return getNarrowedType(type, predicate.type, assumeTrue); + } + if (containsMatchingReference(reference, predicateArgument)) { + return declaredType; + } } - else { - mappedTypes.push(t); + } + else { + var invokedExpression = skipParenthesizedNodes(callExpression.expression); + if (invokedExpression.kind === 173 /* ElementAccessExpression */ || invokedExpression.kind === 172 /* PropertyAccessExpression */) { + var accessExpression = invokedExpression; + var possibleReference = skipParenthesizedNodes(accessExpression.expression); + if (isMatchingReference(reference, possibleReference)) { + return getNarrowedType(type, predicate.type, assumeTrue); + } + if (containsMatchingReference(reference, possibleReference)) { + return declaredType; + } } } + return type; } - return mappedTypes ? getUnionType(mappedTypes) : mappedType; - } - function getTypeOfPropertyOfContextualType(type, name) { - return applyToContextualType(type, function (t) { - var prop = t.flags & 4161536 /* StructuredType */ ? getPropertyOfType(t, name) : undefined; - return prop ? getTypeOfSymbol(prop) : undefined; - }); - } - function getIndexTypeOfContextualType(type, kind) { - return applyToContextualType(type, function (t) { return getIndexTypeOfStructuredType(t, kind); }); - } - // Return true if the given contextual type is a tuple-like type - function contextualTypeIsTupleLikeType(type) { - return !!(type.flags & 524288 /* Union */ ? ts.forEach(type.types, isTupleLikeType) : isTupleLikeType(type)); - } - // In an object literal contextually typed by a type T, the contextual type of a property assignment is the type of - // the matching property in T, if one exists. Otherwise, it is the type of the numeric index signature in T, if one - // exists. Otherwise, it is the type of the string index signature in T, if one exists. - function getContextualTypeForObjectLiteralMethod(node) { - ts.Debug.assert(ts.isObjectLiteralMethod(node)); - if (isInsideWithStatementBody(node)) { - // We cannot answer semantic questions within a with block, do not proceed any further - return undefined; + // Narrow the given type based on the given expression having the assumed boolean value. The returned type + // will be a subtype or the same type as the argument. + function narrowType(type, expr, assumeTrue) { + switch (expr.kind) { + case 69 /* Identifier */: + case 97 /* ThisKeyword */: + case 172 /* PropertyAccessExpression */: + return narrowTypeByTruthiness(type, expr, assumeTrue); + case 174 /* CallExpression */: + return narrowTypeByTypePredicate(type, expr, assumeTrue); + case 178 /* ParenthesizedExpression */: + return narrowType(type, expr.expression, assumeTrue); + case 187 /* BinaryExpression */: + return narrowTypeByBinaryExpression(type, expr, assumeTrue); + case 185 /* PrefixUnaryExpression */: + if (expr.operator === 49 /* ExclamationToken */) { + return narrowType(type, expr.operand, !assumeTrue); + } + break; + } + return type; } - return getContextualTypeForObjectLiteralElement(node); } - function getContextualTypeForObjectLiteralElement(element) { - var objectLiteral = element.parent; - var type = getApparentTypeOfContextualType(objectLiteral); - if (type) { - if (!ts.hasDynamicName(element)) { - // For a (non-symbol) computed property, there is no reason to look up the name - // in the type. It will just be "__computed", which does not appear in any - // SymbolTable. - var symbolName = getSymbolOfNode(element).name; - var propertyType = getTypeOfPropertyOfContextualType(type, symbolName); - if (propertyType) { - return propertyType; + function getTypeOfSymbolAtLocation(symbol, location) { + // If we have an identifier or a property access at the given location, if the location is + // an dotted name expression, and if the location is not an assignment target, obtain the type + // of the expression (which will reflect control flow analysis). If the expression indeed + // resolved to the given symbol, return the narrowed type. + if (location.kind === 69 /* Identifier */) { + if (ts.isRightSideOfQualifiedNameOrPropertyAccess(location)) { + location = location.parent; + } + if (ts.isPartOfExpression(location) && !ts.isAssignmentTarget(location)) { + var type = checkExpression(location); + if (getExportSymbolOfValueSymbolIfExported(getNodeLinks(location).resolvedSymbol) === symbol) { + return type; } } - return isNumericName(element.name) && getIndexTypeOfContextualType(type, 1 /* Number */) || - getIndexTypeOfContextualType(type, 0 /* String */); } - return undefined; + // The location isn't a reference to the given symbol, meaning we're being asked + // a hypothetical question of what type the symbol would have if there was a reference + // to it at the given location. Since we have no control flow information for the + // hypothetical reference (control flow information is created and attached by the + // binder), we simply return the declared type of the symbol. + return getTypeOfSymbol(symbol); } - // In an array literal contextually typed by a type T, the contextual type of an element expression at index N is - // the type of the property with the numeric name N in T, if one exists. Otherwise, if T has a numeric index signature, - // it is the type of the numeric index signature in T. Otherwise, in ES6 and higher, the contextual type is the iterated - // type of T. - function getContextualTypeForElementExpression(node) { - var arrayLiteral = node.parent; - var type = getApparentTypeOfContextualType(arrayLiteral); - if (type) { - var index = ts.indexOf(arrayLiteral.elements, node); - return getTypeOfPropertyOfContextualType(type, "" + index) - || getIndexTypeOfContextualType(type, 1 /* Number */) - || (languageVersion >= 2 /* ES6 */ ? getElementTypeOfIterable(type, /*errorNode*/ undefined) : undefined); + function skipParenthesizedNodes(expression) { + while (expression.kind === 178 /* ParenthesizedExpression */) { + expression = expression.expression; } - return undefined; - } - // In a contextually typed conditional expression, the true/false expressions are contextually typed by the same type. - function getContextualTypeForConditionalOperand(node) { - var conditional = node.parent; - return node === conditional.whenTrue || node === conditional.whenFalse ? getContextualType(conditional) : undefined; + return expression; } - function getContextualTypeForJsxAttribute(attribute) { - var kind = attribute.kind; - var jsxElement = attribute.parent; - var attrsType = getJsxElementAttributesType(jsxElement); - if (attribute.kind === 246 /* JsxAttribute */) { - if (!attrsType || isTypeAny(attrsType)) { - return undefined; + function getControlFlowContainer(node) { + while (true) { + node = node.parent; + if (ts.isFunctionLike(node) && !ts.getImmediatelyInvokedFunctionExpression(node) || + node.kind === 226 /* ModuleBlock */ || + node.kind === 256 /* SourceFile */ || + node.kind === 145 /* PropertyDeclaration */) { + return node; } - return getTypeOfPropertyOfType(attrsType, attribute.name.text); - } - else if (attribute.kind === 247 /* JsxSpreadAttribute */) { - return attrsType; } - ts.Debug.fail("Expected JsxAttribute or JsxSpreadAttribute, got ts.SyntaxKind[" + kind + "]"); - } - // Return the contextual type for a given expression node. During overload resolution, a contextual type may temporarily - // be "pushed" onto a node using the contextualType property. - function getApparentTypeOfContextualType(node) { - var type = getContextualType(node); - return type && getApparentType(type); } - /** - * Woah! Do you really want to use this function? - * - * Unless you're trying to get the *non-apparent* type for a - * value-literal type or you're authoring relevant portions of this algorithm, - * you probably meant to use 'getApparentTypeOfContextualType'. - * Otherwise this may not be very useful. - * - * In cases where you *are* working on this function, you should understand - * when it is appropriate to use 'getContextualType' and 'getApparentTypeOfContextualType'. - * - * - Use 'getContextualType' when you are simply going to propagate the result to the expression. - * - Use 'getApparentTypeOfContextualType' when you're going to need the members of the type. - * - * @param node the expression whose contextual type will be returned. - * @returns the contextual type of an expression. - */ - function getContextualType(node) { - if (isInsideWithStatementBody(node)) { - // We cannot answer semantic questions within a with block, do not proceed any further - return undefined; - } - if (node.contextualType) { - return node.contextualType; - } - var parent = node.parent; - switch (parent.kind) { - case 218 /* VariableDeclaration */: - case 142 /* Parameter */: - case 145 /* PropertyDeclaration */: - case 144 /* PropertySignature */: - case 169 /* BindingElement */: - return getContextualTypeForInitializerExpression(node); - case 180 /* ArrowFunction */: - case 211 /* ReturnStatement */: - return getContextualTypeForReturnExpression(node); - case 190 /* YieldExpression */: - return getContextualTypeForYieldOperand(parent); - case 174 /* CallExpression */: - case 175 /* NewExpression */: - return getContextualTypeForArgument(parent, node); - case 177 /* TypeAssertionExpression */: - case 195 /* AsExpression */: - return getTypeFromTypeNode(parent.type); - case 187 /* BinaryExpression */: - return getContextualTypeForBinaryOperand(node); - case 253 /* PropertyAssignment */: - case 254 /* ShorthandPropertyAssignment */: - return getContextualTypeForObjectLiteralElement(parent); - case 170 /* ArrayLiteralExpression */: - return getContextualTypeForElementExpression(node); - case 188 /* ConditionalExpression */: - return getContextualTypeForConditionalOperand(node); - case 197 /* TemplateSpan */: - ts.Debug.assert(parent.parent.kind === 189 /* TemplateExpression */); - return getContextualTypeForSubstitutionExpression(parent.parent, node); - case 178 /* ParenthesizedExpression */: - return getContextualType(parent); - case 248 /* JsxExpression */: - return getContextualType(parent); - case 246 /* JsxAttribute */: - case 247 /* JsxSpreadAttribute */: - return getContextualTypeForJsxAttribute(parent); + // Check if a parameter is assigned anywhere within its declaring function. + function isParameterAssigned(symbol) { + var func = ts.getRootDeclaration(symbol.valueDeclaration).parent; + var links = getNodeLinks(func); + if (!(links.flags & 4194304 /* AssignmentsMarked */)) { + links.flags |= 4194304 /* AssignmentsMarked */; + if (!hasParentWithAssignmentsMarked(func)) { + markParameterAssignments(func); + } } - return undefined; + return symbol.isAssigned || false; } - // If the given type is an object or union type, if that type has a single signature, and if - // that signature is non-generic, return the signature. Otherwise return undefined. - function getNonGenericSignature(type) { - var signatures = getSignaturesOfStructuredType(type, 0 /* Call */); - if (signatures.length === 1) { - var signature = signatures[0]; - if (!signature.typeParameters) { - return signature; + function hasParentWithAssignmentsMarked(node) { + while (true) { + node = node.parent; + if (!node) { + return false; + } + if (ts.isFunctionLike(node) && getNodeLinks(node).flags & 4194304 /* AssignmentsMarked */) { + return true; } } } - function isFunctionExpressionOrArrowFunction(node) { - return node.kind === 179 /* FunctionExpression */ || node.kind === 180 /* ArrowFunction */; - } - function getContextualSignatureForFunctionLikeDeclaration(node) { - // Only function expressions, arrow functions, and object literal methods are contextually typed. - return isFunctionExpressionOrArrowFunction(node) || ts.isObjectLiteralMethod(node) - ? getContextualSignature(node) - : undefined; - } - function getContextualTypeForFunctionLikeDeclaration(node) { - return ts.isObjectLiteralMethod(node) ? - getContextualTypeForObjectLiteralMethod(node) : - getApparentTypeOfContextualType(node); - } - // Return the contextual signature for a given expression node. A contextual type provides a - // contextual signature if it has a single call signature and if that call signature is non-generic. - // If the contextual type is a union type, get the signature from each type possible and if they are - // all identical ignoring their return type, the result is same signature but with return type as - // union type of return types from these signatures - function getContextualSignature(node) { - ts.Debug.assert(node.kind !== 147 /* MethodDeclaration */ || ts.isObjectLiteralMethod(node)); - var type = getContextualTypeForFunctionLikeDeclaration(node); - if (!type) { - return undefined; - } - if (!(type.flags & 524288 /* Union */)) { - return getNonGenericSignature(type); - } - var signatureList; - var types = type.types; - for (var _i = 0, types_13 = types; _i < types_13.length; _i++) { - var current = types_13[_i]; - var signature = getNonGenericSignature(current); - if (signature) { - if (!signatureList) { - // This signature will contribute to contextual union signature - signatureList = [signature]; - } - else if (!compareSignaturesIdentical(signatureList[0], signature, /*partialMatch*/ false, /*ignoreThisTypes*/ true, /*ignoreReturnTypes*/ true, compareTypesIdentical)) { - // Signatures aren't identical, do not use - return undefined; - } - else { - // Use this signature for contextual union signature - signatureList.push(signature); + function markParameterAssignments(node) { + if (node.kind === 69 /* Identifier */) { + if (ts.isAssignmentTarget(node)) { + var symbol = getResolvedSymbol(node); + if (symbol.valueDeclaration && ts.getRootDeclaration(symbol.valueDeclaration).kind === 142 /* Parameter */) { + symbol.isAssigned = true; } } } - // Result is union of signatures collected (return type is union of return types of this signature set) - var result; - if (signatureList) { - result = cloneSignature(signatureList[0]); - // Clear resolved return type we possibly got from cloneSignature - result.resolvedReturnType = undefined; - result.unionSignatures = signatureList; + else { + ts.forEachChild(node, markParameterAssignments); } - return result; - } - /** - * Detect if the mapper implies an inference context. Specifically, there are 4 possible values - * for a mapper. Let's go through each one of them: - * - * 1. undefined - this means we are not doing inferential typing, but we may do contextual typing, - * which could cause us to assign a parameter a type - * 2. identityMapper - means we want to avoid assigning a parameter a type, whether or not we are in - * inferential typing (context is undefined for the identityMapper) - * 3. a mapper created by createInferenceMapper - we are doing inferential typing, we want to assign - * types to parameters and fix type parameters (context is defined) - * 4. an instantiation mapper created by createTypeMapper or createTypeEraser - this should never be - * passed as the contextual mapper when checking an expression (context is undefined for these) - * - * isInferentialContext is detecting if we are in case 3 - */ - function isInferentialContext(mapper) { - return mapper && mapper.context; - } - function checkSpreadElementExpression(node, contextualMapper) { - // It is usually not safe to call checkExpressionCached if we can be contextually typing. - // You can tell that we are contextually typing because of the contextualMapper parameter. - // While it is true that a spread element can have a contextual type, it does not do anything - // with this type. It is neither affected by it, nor does it propagate it to its operand. - // So the fact that contextualMapper is passed is not important, because the operand of a spread - // element is not contextually typed. - var arrayOrIterableType = checkExpressionCached(node.expression, contextualMapper); - return checkIteratedTypeOrElementType(arrayOrIterableType, node.expression, /*allowStringInput*/ false); } - function hasDefaultValue(node) { - return (node.kind === 169 /* BindingElement */ && !!node.initializer) || - (node.kind === 187 /* BinaryExpression */ && node.operatorToken.kind === 56 /* EqualsToken */); - } - function checkArrayLiteral(node, contextualMapper) { - var elements = node.elements; - var hasSpreadElement = false; - var elementTypes = []; - var inDestructuringPattern = ts.isAssignmentTarget(node); - for (var _i = 0, elements_1 = elements; _i < elements_1.length; _i++) { - var e = elements_1[_i]; - if (inDestructuringPattern && e.kind === 191 /* SpreadElementExpression */) { - // Given the following situation: - // var c: {}; - // [...c] = ["", 0]; - // - // c is represented in the tree as a spread element in an array literal. - // But c really functions as a rest element, and its purpose is to provide - // a contextual type for the right hand side of the assignment. Therefore, - // instead of calling checkExpression on "...c", which will give an error - // if c is not iterable/array-like, we need to act as if we are trying to - // get the contextual element type from it. So we do something similar to - // getContextualTypeForElementExpression, which will crucially not error - // if there is no index type / iterated type. - var restArrayType = checkExpression(e.expression, contextualMapper); - var restElementType = getIndexTypeOfType(restArrayType, 1 /* Number */) || - (languageVersion >= 2 /* ES6 */ ? getElementTypeOfIterable(restArrayType, /*errorNode*/ undefined) : undefined); - if (restElementType) { - elementTypes.push(restElementType); + function checkIdentifier(node) { + var symbol = getResolvedSymbol(node); + // As noted in ECMAScript 6 language spec, arrow functions never have an arguments objects. + // Although in down-level emit of arrow function, we emit it using function expression which means that + // arguments objects will be bound to the inner object; emitting arrow function natively in ES6, arguments objects + // will be bound to non-arrow function that contain this arrow function. This results in inconsistent behavior. + // To avoid that we will give an error to users if they use arguments objects in arrow function so that they + // can explicitly bound arguments objects + if (symbol === argumentsSymbol) { + var container = ts.getContainingFunction(node); + if (languageVersion < 2 /* ES6 */) { + if (container.kind === 180 /* ArrowFunction */) { + error(node, ts.Diagnostics.The_arguments_object_cannot_be_referenced_in_an_arrow_function_in_ES3_and_ES5_Consider_using_a_standard_function_expression); + } + else if (ts.hasModifier(container, 256 /* Async */)) { + error(node, ts.Diagnostics.The_arguments_object_cannot_be_referenced_in_an_async_function_or_method_in_ES3_and_ES5_Consider_using_a_standard_function_or_method); } } - else { - var type = checkExpressionForMutableLocation(e, contextualMapper); - elementTypes.push(type); + if (node.flags & 262144 /* AwaitContext */) { + getNodeLinks(container).flags |= 8192 /* CaptureArguments */; } - hasSpreadElement = hasSpreadElement || e.kind === 191 /* SpreadElementExpression */; } - if (!hasSpreadElement) { - // If array literal is actually a destructuring pattern, mark it as an implied type. We do this such - // that we get the same behavior for "var [x, y] = []" and "[x, y] = []". - if (inDestructuringPattern && elementTypes.length) { - var type = cloneTypeReference(createTupleType(elementTypes)); - type.pattern = node; - return type; + if (symbol.flags & 8388608 /* Alias */ && !isInTypeQuery(node) && !isConstEnumOrConstEnumOnlyModule(resolveAlias(symbol))) { + markAliasSymbolAsReferenced(symbol); + } + var localOrExportSymbol = getExportSymbolOfValueSymbolIfExported(symbol); + if (localOrExportSymbol.flags & 32 /* Class */) { + var declaration_1 = localOrExportSymbol.valueDeclaration; + // Due to the emit for class decorators, any reference to the class from inside of the class body + // must instead be rewritten to point to a temporary variable to avoid issues with the double-bind + // behavior of class names in ES6. + if (languageVersion === 2 /* ES6 */ + && declaration_1.kind === 221 /* ClassDeclaration */ + && ts.nodeIsDecorated(declaration_1)) { + var container = ts.getContainingClass(node); + while (container !== undefined) { + if (container === declaration_1 && container.name !== node) { + getNodeLinks(declaration_1).flags |= 8388608 /* ClassWithConstructorReference */; + getNodeLinks(node).flags |= 16777216 /* ConstructorReferenceInClass */; + break; + } + container = ts.getContainingClass(container); + } } - var contextualType = getApparentTypeOfContextualType(node); - if (contextualType && contextualTypeIsTupleLikeType(contextualType)) { - var pattern = contextualType.pattern; - // If array literal is contextually typed by a binding pattern or an assignment pattern, pad the resulting - // tuple type with the corresponding binding or assignment element types to make the lengths equal. - if (pattern && (pattern.kind === 168 /* ArrayBindingPattern */ || pattern.kind === 170 /* ArrayLiteralExpression */)) { - var patternElements = pattern.elements; - for (var i = elementTypes.length; i < patternElements.length; i++) { - var patternElement = patternElements[i]; - if (hasDefaultValue(patternElement)) { - elementTypes.push(contextualType.typeArguments[i]); - } - else { - if (patternElement.kind !== 193 /* OmittedExpression */) { - error(patternElement, ts.Diagnostics.Initializer_provides_no_value_for_this_binding_element_and_the_binding_element_has_no_default_value); - } - elementTypes.push(unknownType); + else if (declaration_1.kind === 192 /* ClassExpression */) { + // When we emit a class expression with static members that contain a reference + // to the constructor in the initializer, we will need to substitute that + // binding with an alias as the class name is not in scope. + var container = ts.getThisContainer(node, /*includeArrowFunctions*/ false); + while (container !== undefined) { + if (container.parent === declaration_1) { + if (container.kind === 145 /* PropertyDeclaration */ && ts.hasModifier(container, 32 /* Static */)) { + getNodeLinks(declaration_1).flags |= 8388608 /* ClassWithConstructorReference */; + getNodeLinks(node).flags |= 16777216 /* ConstructorReferenceInClass */; } + break; } + container = ts.getThisContainer(container, /*includeArrowFunctions*/ false); } - if (elementTypes.length) { - return createTupleType(elementTypes); + } + } + checkCollisionWithCapturedSuperVariable(node, node); + checkCollisionWithCapturedThisVariable(node, node); + checkNestedBlockScopedBinding(node, symbol); + var type = getTypeOfSymbol(localOrExportSymbol); + var declaration = localOrExportSymbol.valueDeclaration; + // We only narrow variables and parameters occurring in a non-assignment position. For all other + // entities we simply return the declared type. + if (!(localOrExportSymbol.flags & 3 /* Variable */) || ts.isAssignmentTarget(node) || !declaration) { + return type; + } + // The declaration container is the innermost function that encloses the declaration of the variable + // or parameter. The flow container is the innermost function starting with which we analyze the control + // flow graph to determine the control flow based type. + var isParameter = ts.getRootDeclaration(declaration).kind === 142 /* Parameter */; + var declarationContainer = getControlFlowContainer(declaration); + var flowContainer = getControlFlowContainer(node); + var isOuterVariable = flowContainer !== declarationContainer; + // When the control flow originates in a function expression or arrow function and we are referencing + // a const variable or parameter from an outer function, we extend the origin of the control flow + // analysis to include the immediately enclosing function. + while (flowContainer !== declarationContainer && + (flowContainer.kind === 179 /* FunctionExpression */ || + flowContainer.kind === 180 /* ArrowFunction */ || + ts.isObjectLiteralOrClassExpressionMethod(flowContainer)) && + (isReadonlySymbol(localOrExportSymbol) || isParameter && !isParameterAssigned(localOrExportSymbol))) { + flowContainer = getControlFlowContainer(flowContainer); + } + // We only look for uninitialized variables in strict null checking mode, and only when we can analyze + // the entire control flow graph from the variable's declaration (i.e. when the flow container and + // declaration container are the same). + var assumeInitialized = isParameter || isOuterVariable || + type !== autoType && (!strictNullChecks || (type.flags & 1 /* Any */) !== 0) || + ts.isInAmbientContext(declaration); + var flowType = getFlowTypeOfReference(node, type, assumeInitialized, flowContainer); + // A variable is considered uninitialized when it is possible to analyze the entire control flow graph + // from declaration to use, and when the variable's declared type doesn't include undefined but the + // control flow based type does include undefined. + if (type === autoType) { + if (flowType === autoType) { + if (compilerOptions.noImplicitAny) { + error(declaration.name, ts.Diagnostics.Variable_0_implicitly_has_type_any_in_some_locations_where_its_type_cannot_be_determined, symbolToString(symbol)); + error(node, ts.Diagnostics.Variable_0_implicitly_has_an_1_type, symbolToString(symbol), typeToString(anyType)); } + return anyType; } } - return createArrayType(elementTypes.length ? - getUnionType(elementTypes, /*subtypeReduction*/ true) : - strictNullChecks ? neverType : undefinedWideningType); - } - function isNumericName(name) { - return name.kind === 140 /* ComputedPropertyName */ ? isNumericComputedName(name) : isNumericLiteralName(name.text); - } - function isNumericComputedName(name) { - // It seems odd to consider an expression of type Any to result in a numeric name, - // but this behavior is consistent with checkIndexedAccess - return isTypeAnyOrAllConstituentTypesHaveKind(checkComputedPropertyName(name), 340 /* NumberLike */); - } - function isTypeAnyOrAllConstituentTypesHaveKind(type, kind) { - return isTypeAny(type) || isTypeOfKind(type, kind); - } - function isInfinityOrNaNString(name) { - return name === "Infinity" || name === "-Infinity" || name === "NaN"; - } - function isNumericLiteralName(name) { - // The intent of numeric names is that - // - they are names with text in a numeric form, and that - // - setting properties/indexing with them is always equivalent to doing so with the numeric literal 'numLit', - // acquired by applying the abstract 'ToNumber' operation on the name's text. - // - // The subtlety is in the latter portion, as we cannot reliably say that anything that looks like a numeric literal is a numeric name. - // In fact, it is the case that the text of the name must be equal to 'ToString(numLit)' for this to hold. - // - // Consider the property name '"0xF00D"'. When one indexes with '0xF00D', they are actually indexing with the value of 'ToString(0xF00D)' - // according to the ECMAScript specification, so it is actually as if the user indexed with the string '"61453"'. - // Thus, the text of all numeric literals equivalent to '61543' such as '0xF00D', '0xf00D', '0170015', etc. are not valid numeric names - // because their 'ToString' representation is not equal to their original text. - // This is motivated by ECMA-262 sections 9.3.1, 9.8.1, 11.1.5, and 11.2.1. - // - // Here, we test whether 'ToString(ToNumber(name))' is exactly equal to 'name'. - // The '+' prefix operator is equivalent here to applying the abstract ToNumber operation. - // Applying the 'toString()' method on a number gives us the abstract ToString operation on a number. - // - // Note that this accepts the values 'Infinity', '-Infinity', and 'NaN', and that this is intentional. - // This is desired behavior, because when indexing with them as numeric entities, you are indexing - // with the strings '"Infinity"', '"-Infinity"', and '"NaN"' respectively. - return (+name).toString() === name; + else if (!assumeInitialized && !(getFalsyFlags(type) & 2048 /* Undefined */) && getFalsyFlags(flowType) & 2048 /* Undefined */) { + error(node, ts.Diagnostics.Variable_0_is_used_before_being_assigned, symbolToString(symbol)); + // Return the declared type to reduce follow-on errors + return type; + } + return flowType; } - function checkComputedPropertyName(node) { - var links = getNodeLinks(node.expression); - if (!links.resolvedType) { - links.resolvedType = checkExpression(node.expression); - // This will allow types number, string, symbol or any. It will also allow enums, the unknown - // type, and any union of these types (like string | number). - if (!isTypeAnyOrAllConstituentTypesHaveKind(links.resolvedType, 340 /* NumberLike */ | 34 /* StringLike */ | 512 /* ESSymbol */)) { - error(node, ts.Diagnostics.A_computed_property_name_must_be_of_type_string_number_symbol_or_any); - } - else { - checkThatExpressionIsProperSymbolReference(node.expression, links.resolvedType, /*reportError*/ true); + function isInsideFunction(node, threshold) { + var current = node; + while (current && current !== threshold) { + if (ts.isFunctionLike(current)) { + return true; } + current = current.parent; } - return links.resolvedType; + return false; } - function getObjectLiteralIndexInfo(node, properties, kind) { - var propTypes = []; - for (var i = 0; i < properties.length; i++) { - if (kind === 0 /* String */ || isNumericName(node.properties[i].name)) { - propTypes.push(getTypeOfSymbol(properties[i])); + function checkNestedBlockScopedBinding(node, symbol) { + if (languageVersion >= 2 /* ES6 */ || + (symbol.flags & (2 /* BlockScopedVariable */ | 32 /* Class */)) === 0 || + symbol.valueDeclaration.parent.kind === 252 /* CatchClause */) { + return; + } + // 1. walk from the use site up to the declaration and check + // if there is anything function like between declaration and use-site (is binding/class is captured in function). + // 2. walk from the declaration up to the boundary of lexical environment and check + // if there is an iteration statement in between declaration and boundary (is binding/class declared inside iteration statement) + var container = ts.getEnclosingBlockScopeContainer(symbol.valueDeclaration); + var usedInFunction = isInsideFunction(node.parent, container); + var current = container; + var containedInIterationStatement = false; + while (current && !ts.nodeStartsNewLexicalEnvironment(current)) { + if (ts.isIterationStatement(current, /*lookInLabeledStatements*/ false)) { + containedInIterationStatement = true; + break; } + current = current.parent; } - var unionType = propTypes.length ? getUnionType(propTypes, /*subtypeReduction*/ true) : undefinedType; - return createIndexInfo(unionType, /*isReadonly*/ false); - } - function checkObjectLiteral(node, contextualMapper) { - var inDestructuringPattern = ts.isAssignmentTarget(node); - // Grammar checking - checkGrammarObjectLiteralExpression(node, inDestructuringPattern); - var propertiesTable = ts.createMap(); - var propertiesArray = []; - var contextualType = getApparentTypeOfContextualType(node); - var contextualTypeHasPattern = contextualType && contextualType.pattern && - (contextualType.pattern.kind === 167 /* ObjectBindingPattern */ || contextualType.pattern.kind === 171 /* ObjectLiteralExpression */); - var typeFlags = 0; - var patternWithComputedProperties = false; - var hasComputedStringProperty = false; - var hasComputedNumberProperty = false; - for (var _i = 0, _a = node.properties; _i < _a.length; _i++) { - var memberDecl = _a[_i]; - var member = memberDecl.symbol; - if (memberDecl.kind === 253 /* PropertyAssignment */ || - memberDecl.kind === 254 /* ShorthandPropertyAssignment */ || - ts.isObjectLiteralMethod(memberDecl)) { - var type = void 0; - if (memberDecl.kind === 253 /* PropertyAssignment */) { - type = checkPropertyAssignment(memberDecl, contextualMapper); - } - else if (memberDecl.kind === 147 /* MethodDeclaration */) { - type = checkObjectLiteralMethod(memberDecl, contextualMapper); - } - else { - ts.Debug.assert(memberDecl.kind === 254 /* ShorthandPropertyAssignment */); - type = checkExpressionForMutableLocation(memberDecl.name, contextualMapper); - } - typeFlags |= type.flags; - var prop = createSymbol(4 /* Property */ | 67108864 /* Transient */ | member.flags, member.name); - if (inDestructuringPattern) { - // If object literal is an assignment pattern and if the assignment pattern specifies a default value - // for the property, make the property optional. - var isOptional = (memberDecl.kind === 253 /* PropertyAssignment */ && hasDefaultValue(memberDecl.initializer)) || - (memberDecl.kind === 254 /* ShorthandPropertyAssignment */ && memberDecl.objectAssignmentInitializer); - if (isOptional) { - prop.flags |= 536870912 /* Optional */; - } - if (ts.hasDynamicName(memberDecl)) { - patternWithComputedProperties = true; - } - } - else if (contextualTypeHasPattern && !(contextualType.flags & 536870912 /* ObjectLiteralPatternWithComputedProperties */)) { - // If object literal is contextually typed by the implied type of a binding pattern, and if the - // binding pattern specifies a default value for the property, make the property optional. - var impliedProp = getPropertyOfType(contextualType, member.name); - if (impliedProp) { - prop.flags |= impliedProp.flags & 536870912 /* Optional */; - } - else if (!compilerOptions.suppressExcessPropertyErrors) { - error(memberDecl.name, ts.Diagnostics.Object_literal_may_only_specify_known_properties_and_0_does_not_exist_in_type_1, symbolToString(member), typeToString(contextualType)); - } - } - prop.declarations = member.declarations; - prop.parent = member.parent; - if (member.valueDeclaration) { - prop.valueDeclaration = member.valueDeclaration; - } - prop.type = type; - prop.target = member; - member = prop; + if (containedInIterationStatement) { + if (usedInFunction) { + // mark iteration statement as containing block-scoped binding captured in some function + getNodeLinks(current).flags |= 65536 /* LoopWithCapturedBlockScopedBinding */; } - else { - // TypeScript 1.0 spec (April 2014) - // A get accessor declaration is processed in the same manner as - // an ordinary function declaration(section 6.1) with no parameters. - // A set accessor declaration is processed in the same manner - // as an ordinary function declaration with a single parameter and a Void return type. - ts.Debug.assert(memberDecl.kind === 149 /* GetAccessor */ || memberDecl.kind === 150 /* SetAccessor */); - checkAccessorDeclaration(memberDecl); + // mark variables that are declared in loop initializer and reassigned inside the body of ForStatement. + // if body of ForStatement will be converted to function then we'll need a extra machinery to propagate reassigned values back. + if (container.kind === 206 /* ForStatement */ && + ts.getAncestor(symbol.valueDeclaration, 219 /* VariableDeclarationList */).parent === container && + isAssignedInBodyOfForStatement(node, container)) { + getNodeLinks(symbol.valueDeclaration).flags |= 2097152 /* NeedsLoopOutParameter */; } - if (ts.hasDynamicName(memberDecl)) { - if (isNumericName(memberDecl.name)) { - hasComputedNumberProperty = true; - } - else { - hasComputedStringProperty = true; - } + // set 'declared inside loop' bit on the block-scoped binding + getNodeLinks(symbol.valueDeclaration).flags |= 262144 /* BlockScopedBindingInLoop */; + } + if (usedInFunction) { + getNodeLinks(symbol.valueDeclaration).flags |= 131072 /* CapturedBlockScopedBinding */; + } + } + function isAssignedInBodyOfForStatement(node, container) { + var current = node; + // skip parenthesized nodes + while (current.parent.kind === 178 /* ParenthesizedExpression */) { + current = current.parent; + } + // check if node is used as LHS in some assignment expression + var isAssigned = false; + if (ts.isAssignmentTarget(current)) { + isAssigned = true; + } + else if ((current.parent.kind === 185 /* PrefixUnaryExpression */ || current.parent.kind === 186 /* PostfixUnaryExpression */)) { + var expr = current.parent; + isAssigned = expr.operator === 41 /* PlusPlusToken */ || expr.operator === 42 /* MinusMinusToken */; + } + if (!isAssigned) { + return false; + } + // at this point we know that node is the target of assignment + // now check that modification happens inside the statement part of the ForStatement + while (current !== container) { + if (current === container.statement) { + return true; } else { - propertiesTable[member.name] = member; - } - propertiesArray.push(member); - } - // If object literal is contextually typed by the implied type of a binding pattern, augment the result - // type with those properties for which the binding pattern specifies a default value. - if (contextualTypeHasPattern) { - for (var _b = 0, _c = getPropertiesOfType(contextualType); _b < _c.length; _b++) { - var prop = _c[_b]; - if (!propertiesTable[prop.name]) { - if (!(prop.flags & 536870912 /* Optional */)) { - error(prop.valueDeclaration || prop.bindingElement, ts.Diagnostics.Initializer_provides_no_value_for_this_binding_element_and_the_binding_element_has_no_default_value); - } - propertiesTable[prop.name] = prop; - propertiesArray.push(prop); - } + current = current.parent; } } - var stringIndexInfo = hasComputedStringProperty ? getObjectLiteralIndexInfo(node, propertiesArray, 0 /* String */) : undefined; - var numberIndexInfo = hasComputedNumberProperty ? getObjectLiteralIndexInfo(node, propertiesArray, 1 /* Number */) : undefined; - var result = createAnonymousType(node.symbol, propertiesTable, emptyArray, emptyArray, stringIndexInfo, numberIndexInfo); - var freshObjectLiteralFlag = compilerOptions.suppressExcessPropertyErrors ? 0 : 16777216 /* FreshLiteral */; - result.flags |= 8388608 /* ObjectLiteral */ | 67108864 /* ContainsObjectLiteral */ | freshObjectLiteralFlag | (typeFlags & 234881024 /* PropagatingFlags */) | (patternWithComputedProperties ? 536870912 /* ObjectLiteralPatternWithComputedProperties */ : 0); - if (inDestructuringPattern) { - result.pattern = node; - } - return result; - } - function checkJsxSelfClosingElement(node) { - checkJsxOpeningLikeElement(node); - return jsxElementType || anyType; + return false; } - function checkJsxElement(node) { - // Check attributes - checkJsxOpeningLikeElement(node.openingElement); - // Perform resolution on the closing tag so that rename/go to definition/etc work - if (isJsxIntrinsicIdentifier(node.closingElement.tagName)) { - getIntrinsicTagSymbol(node.closingElement); + function captureLexicalThis(node, container) { + getNodeLinks(node).flags |= 2 /* LexicalThis */; + if (container.kind === 145 /* PropertyDeclaration */ || container.kind === 148 /* Constructor */) { + var classNode = container.parent; + getNodeLinks(classNode).flags |= 4 /* CaptureThis */; } else { - checkExpression(node.closingElement.tagName); + getNodeLinks(container).flags |= 4 /* CaptureThis */; } - // Check children - for (var _i = 0, _a = node.children; _i < _a.length; _i++) { - var child = _a[_i]; - switch (child.kind) { - case 248 /* JsxExpression */: - checkJsxExpression(child); - break; - case 241 /* JsxElement */: - checkJsxElement(child); - break; - case 242 /* JsxSelfClosingElement */: - checkJsxSelfClosingElement(child); - break; - } + } + function findFirstSuperCall(n) { + if (ts.isSuperCall(n)) { + return n; } - return jsxElementType || anyType; + else if (ts.isFunctionLike(n)) { + return undefined; + } + return ts.forEachChild(n, findFirstSuperCall); } /** - * Returns true iff the JSX element name would be a valid JS identifier, ignoring restrictions about keywords not being identifiers + * Return a cached result if super-statement is already found. + * Otherwise, find a super statement in a given constructor function and cache the result in the node-links of the constructor + * + * @param constructor constructor-function to look for super statement */ - function isUnhyphenatedJsxName(name) { - // - is the only character supported in JSX attribute names that isn't valid in JavaScript identifiers - return name.indexOf("-") < 0; + function getSuperCallInConstructor(constructor) { + var links = getNodeLinks(constructor); + // Only trying to find super-call if we haven't yet tried to find one. Once we try, we will record the result + if (links.hasSuperCall === undefined) { + links.superCall = findFirstSuperCall(constructor.body); + links.hasSuperCall = links.superCall ? true : false; + } + return links.superCall; } /** - * Returns true iff React would emit this tag name as a string rather than an identifier or qualified name + * Check if the given class-declaration extends null then return true. + * Otherwise, return false + * @param classDecl a class declaration to check if it extends null */ - function isJsxIntrinsicIdentifier(tagName) { - // TODO (yuisu): comment - if (tagName.kind === 172 /* PropertyAccessExpression */ || tagName.kind === 97 /* ThisKeyword */) { - return false; + function classDeclarationExtendsNull(classDecl) { + var classSymbol = getSymbolOfNode(classDecl); + var classInstanceType = getDeclaredTypeOfSymbol(classSymbol); + var baseConstructorType = getBaseConstructorTypeOfClass(classInstanceType); + return baseConstructorType === nullWideningType; + } + function checkThisExpression(node) { + // Stop at the first arrow function so that we can + // tell whether 'this' needs to be captured. + var container = ts.getThisContainer(node, /* includeArrowFunctions */ true); + var needToCaptureLexicalThis = false; + if (container.kind === 148 /* Constructor */) { + var containingClassDecl = container.parent; + var baseTypeNode = ts.getClassExtendsHeritageClauseElement(containingClassDecl); + // If a containing class does not have extends clause or the class extends null + // skip checking whether super statement is called before "this" accessing. + if (baseTypeNode && !classDeclarationExtendsNull(containingClassDecl)) { + var superCall = getSuperCallInConstructor(container); + // We should give an error in the following cases: + // - No super-call + // - "this" is accessing before super-call. + // i.e super(this) + // this.x; super(); + // We want to make sure that super-call is done before accessing "this" so that + // "this" is not accessed as a parameter of the super-call. + if (!superCall || superCall.end > node.pos) { + // In ES6, super inside constructor of class-declaration has to precede "this" accessing + error(node, ts.Diagnostics.super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class); + } + } } - else { - return ts.isIntrinsicJsxName(tagName.text); + // Now skip arrow functions to get the "real" owner of 'this'. + if (container.kind === 180 /* ArrowFunction */) { + container = ts.getThisContainer(container, /* includeArrowFunctions */ false); + // When targeting es6, arrow function lexically bind "this" so we do not need to do the work of binding "this" in emitted code + needToCaptureLexicalThis = (languageVersion < 2 /* ES6 */); } - } - function checkJsxAttribute(node, elementAttributesType, nameTable) { - var correspondingPropType = undefined; - // Look up the corresponding property for this attribute - if (elementAttributesType === emptyObjectType && isUnhyphenatedJsxName(node.name.text)) { - // If there is no 'props' property, you may not have non-"data-" attributes - error(node.parent, ts.Diagnostics.JSX_element_class_does_not_support_attributes_because_it_does_not_have_a_0_property, getJsxElementPropertiesName()); - } - else if (elementAttributesType && !isTypeAny(elementAttributesType)) { - var correspondingPropSymbol = getPropertyOfType(elementAttributesType, node.name.text); - correspondingPropType = correspondingPropSymbol && getTypeOfSymbol(correspondingPropSymbol); - if (isUnhyphenatedJsxName(node.name.text)) { - var attributeType = getTypeOfPropertyOfType(elementAttributesType, getTextOfPropertyName(node.name)) || getIndexTypeOfType(elementAttributesType, 0 /* String */); - if (attributeType) { - correspondingPropType = attributeType; + switch (container.kind) { + case 225 /* ModuleDeclaration */: + error(node, ts.Diagnostics.this_cannot_be_referenced_in_a_module_or_namespace_body); + // do not return here so in case if lexical this is captured - it will be reflected in flags on NodeLinks + break; + case 224 /* EnumDeclaration */: + error(node, ts.Diagnostics.this_cannot_be_referenced_in_current_location); + // do not return here so in case if lexical this is captured - it will be reflected in flags on NodeLinks + break; + case 148 /* Constructor */: + if (isInConstructorArgumentInitializer(node, container)) { + error(node, ts.Diagnostics.this_cannot_be_referenced_in_constructor_arguments); } - else { - // If there's no corresponding property with this name, error - if (!correspondingPropType) { - error(node.name, ts.Diagnostics.Property_0_does_not_exist_on_type_1, node.name.text, typeToString(elementAttributesType)); - return unknownType; - } + break; + case 145 /* PropertyDeclaration */: + case 144 /* PropertySignature */: + if (ts.getModifierFlags(container) & 32 /* Static */) { + error(node, ts.Diagnostics.this_cannot_be_referenced_in_a_static_property_initializer); } - } + break; + case 140 /* ComputedPropertyName */: + error(node, ts.Diagnostics.this_cannot_be_referenced_in_a_computed_property_name); + break; } - var exprType; - if (node.initializer) { - exprType = checkExpression(node.initializer); + if (needToCaptureLexicalThis) { + captureLexicalThis(node, container); } - else { - // is sugar for - exprType = booleanType; + if (ts.isFunctionLike(container) && + (!isInParameterInitializerBeforeContainingFunction(node) || ts.getThisParameter(container))) { + // Note: a parameter initializer should refer to class-this unless function-this is explicitly annotated. + // If this is a function in a JS file, it might be a class method. Check if it's the RHS + // of a x.prototype.y = function [name]() { .... } + if (container.kind === 179 /* FunctionExpression */ && + ts.isInJavaScriptFile(container.parent) && + ts.getSpecialPropertyAssignmentKind(container.parent) === 3 /* PrototypeProperty */) { + // Get the 'x' of 'x.prototype.y = f' (here, 'f' is 'container') + var className = container.parent // x.prototype.y = f + .left // x.prototype.y + .expression // x.prototype + .expression; // x + var classSymbol = checkExpression(className).symbol; + if (classSymbol && classSymbol.members && (classSymbol.flags & 16 /* Function */)) { + return getInferredClassType(classSymbol); + } + } + var thisType = getThisTypeOfDeclaration(container); + if (thisType) { + return thisType; + } } - if (correspondingPropType) { - checkTypeAssignableTo(exprType, correspondingPropType, node); + if (ts.isClassLike(container.parent)) { + var symbol = getSymbolOfNode(container.parent); + var type = ts.hasModifier(container, 32 /* Static */) ? getTypeOfSymbol(symbol) : getDeclaredTypeOfSymbol(symbol).thisType; + return getFlowTypeOfReference(node, type, /*assumeInitialized*/ true, /*flowContainer*/ undefined); } - nameTable[node.name.text] = true; - return exprType; - } - function checkJsxSpreadAttribute(node, elementAttributesType, nameTable) { - var type = checkExpression(node.expression); - var props = getPropertiesOfType(type); - for (var _i = 0, props_2 = props; _i < props_2.length; _i++) { - var prop = props_2[_i]; - // Is there a corresponding property in the element attributes type? Skip checking of properties - // that have already been assigned to, as these are not actually pushed into the resulting type - if (!nameTable[prop.name]) { - var targetPropSym = getPropertyOfType(elementAttributesType, prop.name); - if (targetPropSym) { - var msg = ts.chainDiagnosticMessages(undefined, ts.Diagnostics.Property_0_of_JSX_spread_attribute_is_not_assignable_to_target_property, prop.name); - checkTypeAssignableTo(getTypeOfSymbol(prop), getTypeOfSymbol(targetPropSym), node, undefined, msg); - } - nameTable[prop.name] = true; + if (ts.isInJavaScriptFile(node)) { + var type = getTypeForThisExpressionFromJSDoc(container); + if (type && type !== unknownType) { + return type; } } - return type; - } - function getJsxType(name) { - if (jsxTypes[name] === undefined) { - return jsxTypes[name] = getExportedTypeFromNamespace(JsxNames.JSX, name) || unknownType; + if (compilerOptions.noImplicitThis) { + // With noImplicitThis, functions may not reference 'this' if it has type 'any' + error(node, ts.Diagnostics.this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation); } - return jsxTypes[name]; + return anyType; } - /** - * Looks up an intrinsic tag name and returns a symbol that either points to an intrinsic - * property (in which case nodeLinks.jsxFlags will be IntrinsicNamedElement) or an intrinsic - * string index signature (in which case nodeLinks.jsxFlags will be IntrinsicIndexedElement). - * May also return unknownSymbol if both of these lookups fail. - */ - function getIntrinsicTagSymbol(node) { - var links = getNodeLinks(node); - if (!links.resolvedSymbol) { - var intrinsicElementsType = getJsxType(JsxNames.IntrinsicElements); - if (intrinsicElementsType !== unknownType) { - // Property case - var intrinsicProp = getPropertyOfType(intrinsicElementsType, node.tagName.text); - if (intrinsicProp) { - links.jsxFlags |= 1 /* IntrinsicNamedElement */; - return links.resolvedSymbol = intrinsicProp; - } - // Intrinsic string indexer case - var indexSignatureType = getIndexTypeOfType(intrinsicElementsType, 0 /* String */); - if (indexSignatureType) { - links.jsxFlags |= 2 /* IntrinsicIndexedElement */; - return links.resolvedSymbol = intrinsicElementsType.symbol; - } - // Wasn't found - error(node, ts.Diagnostics.Property_0_does_not_exist_on_type_1, node.tagName.text, "JSX." + JsxNames.IntrinsicElements); - return links.resolvedSymbol = unknownSymbol; - } - else { - if (compilerOptions.noImplicitAny) { - error(node, ts.Diagnostics.JSX_element_implicitly_has_type_any_because_no_interface_JSX_0_exists, JsxNames.IntrinsicElements); - } - return links.resolvedSymbol = unknownSymbol; + function getTypeForThisExpressionFromJSDoc(node) { + var typeTag = ts.getJSDocTypeTag(node); + if (typeTag && typeTag.typeExpression && typeTag.typeExpression.type && typeTag.typeExpression.type.kind === 269 /* JSDocFunctionType */) { + var jsDocFunctionType = typeTag.typeExpression.type; + if (jsDocFunctionType.parameters.length > 0 && jsDocFunctionType.parameters[0].type.kind === 272 /* JSDocThisType */) { + return getTypeFromTypeNode(jsDocFunctionType.parameters[0].type); } } - return links.resolvedSymbol; } - /** - * Given a JSX element that is a class element, finds the Element Instance Type. If the - * element is not a class element, or the class element type cannot be determined, returns 'undefined'. - * For example, in the element , the element instance type is `MyClass` (not `typeof MyClass`). - */ - function getJsxElementInstanceType(node, valueType) { - ts.Debug.assert(!(valueType.flags & 524288 /* Union */)); - if (isTypeAny(valueType)) { - // Short-circuit if the class tag is using an element type 'any' - return anyType; - } - // Resolve the signatures, preferring constructor - var signatures = getSignaturesOfType(valueType, 1 /* Construct */); - if (signatures.length === 0) { - // No construct signatures, try call signatures - signatures = getSignaturesOfType(valueType, 0 /* Call */); - if (signatures.length === 0) { - // We found no signatures at all, which is an error - error(node.tagName, ts.Diagnostics.JSX_element_type_0_does_not_have_any_construct_or_call_signatures, ts.getTextOfNode(node.tagName)); - return unknownType; + function isInConstructorArgumentInitializer(node, constructorDecl) { + for (var n = node; n && n !== constructorDecl; n = n.parent) { + if (n.kind === 142 /* Parameter */) { + return true; } } - return getUnionType(signatures.map(getReturnTypeOfSignature), /*subtypeReduction*/ true); + return false; } - /// e.g. "props" for React.d.ts, - /// or 'undefined' if ElementAttributesProperty doesn't exist (which means all - /// non-intrinsic elements' attributes type is 'any'), - /// or '' if it has 0 properties (which means every - /// non-intrinsic elements' attributes type is the element instance type) - function getJsxElementPropertiesName() { - // JSX - var jsxNamespace = getGlobalSymbol(JsxNames.JSX, 1920 /* Namespace */, /*diagnosticMessage*/ undefined); - // JSX.ElementAttributesProperty [symbol] - var attribsPropTypeSym = jsxNamespace && getSymbol(jsxNamespace.exports, JsxNames.ElementAttributesPropertyNameContainer, 793064 /* Type */); - // JSX.ElementAttributesProperty [type] - var attribPropType = attribsPropTypeSym && getDeclaredTypeOfSymbol(attribsPropTypeSym); - // The properties of JSX.ElementAttributesProperty - var attribProperties = attribPropType && getPropertiesOfType(attribPropType); - if (attribProperties) { - // Element Attributes has zero properties, so the element attributes type will be the class instance type - if (attribProperties.length === 0) { - return ""; + function checkSuperExpression(node) { + var isCallExpression = node.parent.kind === 174 /* CallExpression */ && node.parent.expression === node; + var container = ts.getSuperContainer(node, /*stopOnFunctions*/ true); + var needToCaptureLexicalThis = false; + // adjust the container reference in case if super is used inside arrow functions with arbitrarily deep nesting + if (!isCallExpression) { + while (container && container.kind === 180 /* ArrowFunction */) { + container = ts.getSuperContainer(container, /*stopOnFunctions*/ true); + needToCaptureLexicalThis = languageVersion < 2 /* ES6 */; } - else if (attribProperties.length === 1) { - return attribProperties[0].name; + } + var canUseSuperExpression = isLegalUsageOfSuperExpression(container); + var nodeCheckFlag = 0; + if (!canUseSuperExpression) { + // issue more specific error if super is used in computed property name + // class A { foo() { return "1" }} + // class B { + // [super.foo()]() {} + // } + var current = node; + while (current && current !== container && current.kind !== 140 /* ComputedPropertyName */) { + current = current.parent; + } + if (current && current.kind === 140 /* ComputedPropertyName */) { + error(node, ts.Diagnostics.super_cannot_be_referenced_in_a_computed_property_name); + } + else if (isCallExpression) { + error(node, ts.Diagnostics.Super_calls_are_not_permitted_outside_constructors_or_in_nested_functions_inside_constructors); + } + else if (!container || !container.parent || !(ts.isClassLike(container.parent) || container.parent.kind === 171 /* ObjectLiteralExpression */)) { + error(node, ts.Diagnostics.super_can_only_be_referenced_in_members_of_derived_classes_or_object_literal_expressions); } else { - error(attribsPropTypeSym.declarations[0], ts.Diagnostics.The_global_type_JSX_0_may_not_have_more_than_one_property, JsxNames.ElementAttributesPropertyNameContainer); - return undefined; + error(node, ts.Diagnostics.super_property_access_is_permitted_only_in_a_constructor_member_function_or_member_accessor_of_a_derived_class); } + return unknownType; } - else { - // No interface exists, so the element attributes type will be an implicit any - return undefined; - } - } - /** - * Given React element instance type and the class type, resolve the Jsx type - * Pass elemType to handle individual type in the union typed element type. - */ - function getResolvedJsxType(node, elemType, elemClassType) { - if (!elemType) { - elemType = checkExpression(node.tagName); - } - if (elemType.flags & 524288 /* Union */) { - var types = elemType.types; - return getUnionType(types.map(function (type) { - return getResolvedJsxType(node, type, elemClassType); - }), /*subtypeReduction*/ true); + if ((ts.getModifierFlags(container) & 32 /* Static */) || isCallExpression) { + nodeCheckFlag = 512 /* SuperStatic */; } - // If the elemType is a string type, we have to return anyType to prevent an error downstream as we will try to find construct or call signature of the type - if (elemType.flags & 2 /* String */) { - return anyType; + else { + nodeCheckFlag = 256 /* SuperInstance */; } - else if (elemType.flags & 32 /* StringLiteral */) { - // If the elemType is a stringLiteral type, we can then provide a check to make sure that the string literal type is one of the Jsx intrinsic element type - var intrinsicElementsType = getJsxType(JsxNames.IntrinsicElements); - if (intrinsicElementsType !== unknownType) { - var stringLiteralTypeName = elemType.text; - var intrinsicProp = getPropertyOfType(intrinsicElementsType, stringLiteralTypeName); - if (intrinsicProp) { - return getTypeOfSymbol(intrinsicProp); - } - var indexSignatureType = getIndexTypeOfType(intrinsicElementsType, 0 /* String */); - if (indexSignatureType) { - return indexSignatureType; - } - error(node, ts.Diagnostics.Property_0_does_not_exist_on_type_1, stringLiteralTypeName, "JSX." + JsxNames.IntrinsicElements); + getNodeLinks(node).flags |= nodeCheckFlag; + // Due to how we emit async functions, we need to specialize the emit for an async method that contains a `super` reference. + // This is due to the fact that we emit the body of an async function inside of a generator function. As generator + // functions cannot reference `super`, we emit a helper inside of the method body, but outside of the generator. This helper + // uses an arrow function, which is permitted to reference `super`. + // + // There are two primary ways we can access `super` from within an async method. The first is getting the value of a property + // or indexed access on super, either as part of a right-hand-side expression or call expression. The second is when setting the value + // of a property or indexed access, either as part of an assignment expression or destructuring assignment. + // + // The simplest case is reading a value, in which case we will emit something like the following: + // + // // ts + // ... + // async asyncMethod() { + // let x = await super.asyncMethod(); + // return x; + // } + // ... + // + // // js + // ... + // asyncMethod() { + // const _super = name => super[name]; + // return __awaiter(this, arguments, Promise, function *() { + // let x = yield _super("asyncMethod").call(this); + // return x; + // }); + // } + // ... + // + // The more complex case is when we wish to assign a value, especially as part of a destructuring assignment. As both cases + // are legal in ES6, but also likely less frequent, we emit the same more complex helper for both scenarios: + // + // // ts + // ... + // async asyncMethod(ar: Promise) { + // [super.a, super.b] = await ar; + // } + // ... + // + // // js + // ... + // asyncMethod(ar) { + // const _super = (function (geti, seti) { + // const cache = Object.create(null); + // return name => cache[name] || (cache[name] = { get value() { return geti(name); }, set value(v) { seti(name, v); } }); + // })(name => super[name], (name, value) => super[name] = value); + // return __awaiter(this, arguments, Promise, function *() { + // [_super("a").value, _super("b").value] = yield ar; + // }); + // } + // ... + // + // This helper creates an object with a "value" property that wraps the `super` property or indexed access for both get and set. + // This is required for destructuring assignments, as a call expression cannot be used as the target of a destructuring assignment + // while a property access can. + if (container.kind === 147 /* MethodDeclaration */ && ts.getModifierFlags(container) & 256 /* Async */) { + if (ts.isSuperProperty(node.parent) && ts.isAssignmentTarget(node.parent)) { + getNodeLinks(container).flags |= 4096 /* AsyncMethodWithSuperBinding */; } - // If we need to report an error, we already done so here. So just return any to prevent any more error downstream - return anyType; - } - // Get the element instance type (the result of newing or invoking this tag) - var elemInstanceType = getJsxElementInstanceType(node, elemType); - if (!elemClassType || !isTypeAssignableTo(elemInstanceType, elemClassType)) { - // Is this is a stateless function component? See if its single signature's return type is - // assignable to the JSX Element Type - if (jsxElementType) { - var callSignatures = elemType && getSignaturesOfType(elemType, 0 /* Call */); - var callSignature = callSignatures && callSignatures.length > 0 && callSignatures[0]; - var callReturnType = callSignature && getReturnTypeOfSignature(callSignature); - var paramType = callReturnType && (callSignature.parameters.length === 0 ? emptyObjectType : getTypeOfSymbol(callSignature.parameters[0])); - if (callReturnType && isTypeAssignableTo(callReturnType, jsxElementType)) { - // Intersect in JSX.IntrinsicAttributes if it exists - var intrinsicAttributes = getJsxType(JsxNames.IntrinsicAttributes); - if (intrinsicAttributes !== unknownType) { - paramType = intersectTypes(intrinsicAttributes, paramType); - } - return paramType; - } + else { + getNodeLinks(container).flags |= 2048 /* AsyncMethodWithSuper */; } } - // Issue an error if this return type isn't assignable to JSX.ElementClass - if (elemClassType) { - checkTypeRelatedTo(elemInstanceType, elemClassType, assignableRelation, node, ts.Diagnostics.JSX_element_type_0_is_not_a_constructor_function_for_JSX_elements); + if (needToCaptureLexicalThis) { + // call expressions are allowed only in constructors so they should always capture correct 'this' + // super property access expressions can also appear in arrow functions - + // in this case they should also use correct lexical this + captureLexicalThis(node.parent, container); } - if (isTypeAny(elemInstanceType)) { - return elemInstanceType; + if (container.parent.kind === 171 /* ObjectLiteralExpression */) { + if (languageVersion < 2 /* ES6 */) { + error(node, ts.Diagnostics.super_is_only_allowed_in_members_of_object_literal_expressions_when_option_target_is_ES2015_or_higher); + return unknownType; + } + else { + // for object literal assume that type of 'super' is 'any' + return anyType; + } } - var propsName = getJsxElementPropertiesName(); - if (propsName === undefined) { - // There is no type ElementAttributesProperty, return 'any' - return anyType; + // at this point the only legal case for parent is ClassLikeDeclaration + var classLikeDeclaration = container.parent; + var classType = getDeclaredTypeOfSymbol(getSymbolOfNode(classLikeDeclaration)); + var baseClassType = classType && getBaseTypes(classType)[0]; + if (!baseClassType) { + if (!ts.getClassExtendsHeritageClauseElement(classLikeDeclaration)) { + error(node, ts.Diagnostics.super_can_only_be_referenced_in_a_derived_class); + } + return unknownType; } - else if (propsName === "") { - // If there is no e.g. 'props' member in ElementAttributesProperty, use the element class type instead - return elemInstanceType; + if (container.kind === 148 /* Constructor */ && isInConstructorArgumentInitializer(node, container)) { + // issue custom error message for super property access in constructor arguments (to be aligned with old compiler) + error(node, ts.Diagnostics.super_cannot_be_referenced_in_constructor_arguments); + return unknownType; } - else { - var attributesType = getTypeOfPropertyOfType(elemInstanceType, propsName); - if (!attributesType) { - // There is no property named 'props' on this instance type - return emptyObjectType; - } - else if (isTypeAny(attributesType) || (attributesType === unknownType)) { - // Props is of type 'any' or unknown - return attributesType; + return nodeCheckFlag === 512 /* SuperStatic */ + ? getBaseConstructorTypeOfClass(classType) + : getTypeWithThisArgument(baseClassType, classType.thisType); + function isLegalUsageOfSuperExpression(container) { + if (!container) { + return false; } - else if (attributesType.flags & 524288 /* Union */) { - // Props cannot be a union type - error(node.tagName, ts.Diagnostics.JSX_element_attributes_type_0_may_not_be_a_union_type, typeToString(attributesType)); - return anyType; + if (isCallExpression) { + // TS 1.0 SPEC (April 2014): 4.8.1 + // Super calls are only permitted in constructors of derived classes + return container.kind === 148 /* Constructor */; } else { - // Normal case -- add in IntrinsicClassElements and IntrinsicElements - var apparentAttributesType = attributesType; - var intrinsicClassAttribs = getJsxType(JsxNames.IntrinsicClassAttributes); - if (intrinsicClassAttribs !== unknownType) { - var typeParams = getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(intrinsicClassAttribs.symbol); - if (typeParams) { - if (typeParams.length === 1) { - apparentAttributesType = intersectTypes(createTypeReference(intrinsicClassAttribs, [elemInstanceType]), apparentAttributesType); - } + // TS 1.0 SPEC (April 2014) + // 'super' property access is allowed + // - In a constructor, instance member function, instance member accessor, or instance member variable initializer where this references a derived class instance + // - In a static member function or static member accessor + // topmost container must be something that is directly nested in the class declaration\object literal expression + if (ts.isClassLike(container.parent) || container.parent.kind === 171 /* ObjectLiteralExpression */) { + if (ts.getModifierFlags(container) & 32 /* Static */) { + return container.kind === 147 /* MethodDeclaration */ || + container.kind === 146 /* MethodSignature */ || + container.kind === 149 /* GetAccessor */ || + container.kind === 150 /* SetAccessor */; } else { - apparentAttributesType = intersectTypes(attributesType, intrinsicClassAttribs); + return container.kind === 147 /* MethodDeclaration */ || + container.kind === 146 /* MethodSignature */ || + container.kind === 149 /* GetAccessor */ || + container.kind === 150 /* SetAccessor */ || + container.kind === 145 /* PropertyDeclaration */ || + container.kind === 144 /* PropertySignature */ || + container.kind === 148 /* Constructor */; } } - var intrinsicAttribs = getJsxType(JsxNames.IntrinsicAttributes); - if (intrinsicAttribs !== unknownType) { - apparentAttributesType = intersectTypes(intrinsicAttribs, apparentAttributesType); - } - return apparentAttributesType; } + return false; } } - /** - * Given an opening/self-closing element, get the 'element attributes type', i.e. the type that tells - * us which attributes are valid on a given element. - */ - function getJsxElementAttributesType(node) { - var links = getNodeLinks(node); - if (!links.resolvedJsxType) { - if (isJsxIntrinsicIdentifier(node.tagName)) { - var symbol = getIntrinsicTagSymbol(node); - if (links.jsxFlags & 1 /* IntrinsicNamedElement */) { - return links.resolvedJsxType = getTypeOfSymbol(symbol); + function getContextualThisParameter(func) { + if (isContextSensitiveFunctionOrObjectLiteralMethod(func) && func.kind !== 180 /* ArrowFunction */) { + var contextualSignature = getContextualSignature(func); + if (contextualSignature) { + return contextualSignature.thisParameter; + } + } + return undefined; + } + // Return contextual type of parameter or undefined if no contextual type is available + function getContextuallyTypedParameterType(parameter) { + var func = parameter.parent; + if (isContextSensitiveFunctionOrObjectLiteralMethod(func)) { + var iife = ts.getImmediatelyInvokedFunctionExpression(func); + if (iife) { + var indexOfParameter = ts.indexOf(func.parameters, parameter); + if (iife.arguments && indexOfParameter < iife.arguments.length) { + if (parameter.dotDotDotToken) { + var restTypes = []; + for (var i = indexOfParameter; i < iife.arguments.length; i++) { + restTypes.push(getWidenedLiteralType(checkExpression(iife.arguments[i]))); + } + return createArrayType(getUnionType(restTypes)); + } + var links = getNodeLinks(iife); + var cached = links.resolvedSignature; + links.resolvedSignature = anySignature; + var type = getWidenedLiteralType(checkExpression(iife.arguments[indexOfParameter])); + links.resolvedSignature = cached; + return type; } - else if (links.jsxFlags & 2 /* IntrinsicIndexedElement */) { - return links.resolvedJsxType = getIndexInfoOfSymbol(symbol, 0 /* String */).type; + } + var contextualSignature = getContextualSignature(func); + if (contextualSignature) { + var funcHasRestParameters = ts.hasRestParameter(func); + var len = func.parameters.length - (funcHasRestParameters ? 1 : 0); + var indexOfParameter = ts.indexOf(func.parameters, parameter); + if (indexOfParameter < len) { + return getTypeAtPosition(contextualSignature, indexOfParameter); } - else { - return links.resolvedJsxType = unknownType; + // If last parameter is contextually rest parameter get its type + if (funcHasRestParameters && + indexOfParameter === (func.parameters.length - 1) && + isRestParameterIndex(contextualSignature, func.parameters.length - 1)) { + return getTypeOfSymbol(ts.lastOrUndefined(contextualSignature.parameters)); } } - else { - var elemClassType = getJsxGlobalElementClassType(); - return links.resolvedJsxType = getResolvedJsxType(node, undefined, elemClassType); - } } - return links.resolvedJsxType; + return undefined; } - /** - * Given a JSX attribute, returns the symbol for the corresponds property - * of the element attributes type. Will return unknownSymbol for attributes - * that have no matching element attributes type property. - */ - function getJsxAttributePropertySymbol(attrib) { - var attributesType = getJsxElementAttributesType(attrib.parent); - var prop = getPropertyOfType(attributesType, attrib.name.text); - return prop || unknownSymbol; - } - function getJsxGlobalElementClassType() { - if (!jsxElementClassType) { - jsxElementClassType = getExportedTypeFromNamespace(JsxNames.JSX, JsxNames.ElementClass); - } - return jsxElementClassType; - } - /// Returns all the properties of the Jsx.IntrinsicElements interface - function getJsxIntrinsicTagNames() { - var intrinsics = getJsxType(JsxNames.IntrinsicElements); - return intrinsics ? getPropertiesOfType(intrinsics) : emptyArray; - } - function checkJsxPreconditions(errorNode) { - // Preconditions for using JSX - if ((compilerOptions.jsx || 0 /* None */) === 0 /* None */) { - error(errorNode, ts.Diagnostics.Cannot_use_JSX_unless_the_jsx_flag_is_provided); - } - if (jsxElementType === undefined) { - if (compilerOptions.noImplicitAny) { - error(errorNode, ts.Diagnostics.JSX_element_implicitly_has_type_any_because_the_global_type_JSX_Element_does_not_exist); - } - } - } - function checkJsxOpeningLikeElement(node) { - checkGrammarJsxElement(node); - checkJsxPreconditions(node); - // The reactNamespace symbol should be marked as 'used' so we don't incorrectly elide its import. And if there - // is no reactNamespace symbol in scope when targeting React emit, we should issue an error. - var reactRefErr = compilerOptions.jsx === 2 /* React */ ? ts.Diagnostics.Cannot_find_name_0 : undefined; - var reactNamespace = compilerOptions.reactNamespace ? compilerOptions.reactNamespace : "React"; - var reactSym = resolveName(node.tagName, reactNamespace, 107455 /* Value */, reactRefErr, reactNamespace); - if (reactSym) { - getSymbolLinks(reactSym).referenced = true; - } - var targetAttributesType = getJsxElementAttributesType(node); - var nameTable = ts.createMap(); - // Process this array in right-to-left order so we know which - // attributes (mostly from spreads) are being overwritten and - // thus should have their types ignored - var sawSpreadedAny = false; - for (var i = node.attributes.length - 1; i >= 0; i--) { - if (node.attributes[i].kind === 246 /* JsxAttribute */) { - checkJsxAttribute((node.attributes[i]), targetAttributesType, nameTable); + // In a variable, parameter or property declaration with a type annotation, + // the contextual type of an initializer expression is the type of the variable, parameter or property. + // Otherwise, in a parameter declaration of a contextually typed function expression, + // the contextual type of an initializer expression is the contextual type of the parameter. + // Otherwise, in a variable or parameter declaration with a binding pattern name, + // the contextual type of an initializer expression is the type implied by the binding pattern. + // Otherwise, in a binding pattern inside a variable or parameter declaration, + // the contextual type of an initializer expression is the type annotation of the containing declaration, if present. + function getContextualTypeForInitializerExpression(node) { + var declaration = node.parent; + if (node === declaration.initializer) { + if (declaration.type) { + return getTypeFromTypeNode(declaration.type); } - else { - ts.Debug.assert(node.attributes[i].kind === 247 /* JsxSpreadAttribute */); - var spreadType = checkJsxSpreadAttribute((node.attributes[i]), targetAttributesType, nameTable); - if (isTypeAny(spreadType)) { - sawSpreadedAny = true; + if (declaration.kind === 142 /* Parameter */) { + var type = getContextuallyTypedParameterType(declaration); + if (type) { + return type; } } - } - // Check that all required properties have been provided. If an 'any' - // was spreaded in, though, assume that it provided all required properties - if (targetAttributesType && !sawSpreadedAny) { - var targetProperties = getPropertiesOfType(targetAttributesType); - for (var i = 0; i < targetProperties.length; i++) { - if (!(targetProperties[i].flags & 536870912 /* Optional */) && - !nameTable[targetProperties[i].name]) { - error(node, ts.Diagnostics.Property_0_is_missing_in_type_1, targetProperties[i].name, typeToString(targetAttributesType)); + if (ts.isBindingPattern(declaration.name)) { + return getTypeFromBindingPattern(declaration.name, /*includePatternInType*/ true, /*reportErrors*/ false); + } + if (ts.isBindingPattern(declaration.parent)) { + var parentDeclaration = declaration.parent.parent; + var name_15 = declaration.propertyName || declaration.name; + if (ts.isVariableLike(parentDeclaration) && + parentDeclaration.type && + !ts.isBindingPattern(name_15)) { + var text = getTextOfPropertyName(name_15); + if (text) { + return getTypeOfPropertyOfType(getTypeFromTypeNode(parentDeclaration.type), text); + } } } } + return undefined; } - function checkJsxExpression(node) { - if (node.expression) { - return checkExpression(node.expression); + function getContextualTypeForReturnExpression(node) { + var func = ts.getContainingFunction(node); + if (ts.isAsyncFunctionLike(func)) { + var contextualReturnType = getContextualReturnType(func); + if (contextualReturnType) { + return getPromisedType(contextualReturnType); + } + return undefined; } - else { - return unknownType; + if (func && !func.asteriskToken) { + return getContextualReturnType(func); } + return undefined; } - // If a symbol is a synthesized symbol with no value declaration, we assume it is a property. Example of this are the synthesized - // '.prototype' property as well as synthesized tuple index properties. - function getDeclarationKindFromSymbol(s) { - return s.valueDeclaration ? s.valueDeclaration.kind : 145 /* PropertyDeclaration */; - } - function getDeclarationModifierFlagsFromSymbol(s) { - return s.valueDeclaration ? ts.getCombinedModifierFlags(s.valueDeclaration) : s.flags & 134217728 /* Prototype */ ? 4 /* Public */ | 32 /* Static */ : 0; - } - function getDeclarationNodeFlagsFromSymbol(s) { - return s.valueDeclaration ? ts.getCombinedNodeFlags(s.valueDeclaration) : 0; - } - /** - * Check whether the requested property access is valid. - * Returns true if node is a valid property access, and false otherwise. - * @param node The node to be checked. - * @param left The left hand side of the property access (e.g.: the super in `super.foo`). - * @param type The type of left. - * @param prop The symbol for the right hand side of the property access. - */ - function checkClassPropertyAccess(node, left, type, prop) { - var flags = getDeclarationModifierFlagsFromSymbol(prop); - var declaringClass = getDeclaredTypeOfSymbol(getParentOfSymbol(prop)); - var errorNode = node.kind === 172 /* PropertyAccessExpression */ || node.kind === 218 /* VariableDeclaration */ ? - node.name : - node.right; - if (left.kind === 95 /* SuperKeyword */) { - // TS 1.0 spec (April 2014): 4.8.2 - // - In a constructor, instance member function, instance member accessor, or - // instance member variable initializer where this references a derived class instance, - // a super property access is permitted and must specify a public instance member function of the base class. - // - In a static member function or static member accessor - // where this references the constructor function object of a derived class, - // a super property access is permitted and must specify a public static member function of the base class. - if (languageVersion < 2 /* ES6 */ && getDeclarationKindFromSymbol(prop) !== 147 /* MethodDeclaration */) { - // `prop` refers to a *property* declared in the super class - // rather than a *method*, so it does not satisfy the above criteria. - error(errorNode, ts.Diagnostics.Only_public_and_protected_methods_of_the_base_class_are_accessible_via_the_super_keyword); - return false; - } - if (flags & 128 /* Abstract */) { - // A method cannot be accessed in a super property access if the method is abstract. - // This error could mask a private property access error. But, a member - // cannot simultaneously be private and abstract, so this will trigger an - // additional error elsewhere. - error(errorNode, ts.Diagnostics.Abstract_method_0_in_class_1_cannot_be_accessed_via_super_expression, symbolToString(prop), typeToString(declaringClass)); - return false; + function getContextualTypeForYieldOperand(node) { + var func = ts.getContainingFunction(node); + if (func) { + var contextualReturnType = getContextualReturnType(func); + if (contextualReturnType) { + return node.asteriskToken + ? contextualReturnType + : getElementTypeOfIterableIterator(contextualReturnType); } } - // Public properties are otherwise accessible. - if (!(flags & 24 /* NonPublicAccessibilityModifier */)) { - return true; - } - // Property is known to be private or protected at this point - // Private property is accessible if the property is within the declaring class - if (flags & 8 /* Private */) { - var declaringClassDeclaration = getClassLikeDeclarationOfSymbol(getParentOfSymbol(prop)); - if (!isNodeWithinClass(node, declaringClassDeclaration)) { - error(errorNode, ts.Diagnostics.Property_0_is_private_and_only_accessible_within_class_1, symbolToString(prop), typeToString(declaringClass)); - return false; + return undefined; + } + function isInParameterInitializerBeforeContainingFunction(node) { + while (node.parent && !ts.isFunctionLike(node.parent)) { + if (node.parent.kind === 142 /* Parameter */ && node.parent.initializer === node) { + return true; } - return true; - } - // Property is known to be protected at this point - // All protected properties of a supertype are accessible in a super access - if (left.kind === 95 /* SuperKeyword */) { - return true; - } - // Get the enclosing class that has the declaring class as its base type - var enclosingClass = forEachEnclosingClass(node, function (enclosingDeclaration) { - var enclosingClass = getDeclaredTypeOfSymbol(getSymbolOfNode(enclosingDeclaration)); - return hasBaseType(enclosingClass, declaringClass) ? enclosingClass : undefined; - }); - // A protected property is accessible if the property is within the declaring class or classes derived from it - if (!enclosingClass) { - error(errorNode, ts.Diagnostics.Property_0_is_protected_and_only_accessible_within_class_1_and_its_subclasses, symbolToString(prop), typeToString(declaringClass)); - return false; - } - // No further restrictions for static properties - if (flags & 32 /* Static */) { - return true; + node = node.parent; } - // An instance property must be accessed through an instance of the enclosing class - if (type.flags & 268435456 /* ThisType */) { - // get the original type -- represented as the type constraint of the 'this' type - type = getConstraintOfTypeParameter(type); + return false; + } + function getContextualReturnType(functionDecl) { + // If the containing function has a return type annotation, is a constructor, or is a get accessor whose + // corresponding set accessor has a type annotation, return statements in the function are contextually typed + if (functionDecl.type || + functionDecl.kind === 148 /* Constructor */ || + functionDecl.kind === 149 /* GetAccessor */ && ts.getSetAccessorTypeAnnotationNode(ts.getDeclarationOfKind(functionDecl.symbol, 150 /* SetAccessor */))) { + return getReturnTypeOfSignature(getSignatureFromDeclaration(functionDecl)); } - // TODO: why is the first part of this check here? - if (!(getTargetType(type).flags & (32768 /* Class */ | 65536 /* Interface */) && hasBaseType(type, enclosingClass))) { - error(errorNode, ts.Diagnostics.Property_0_is_protected_and_only_accessible_through_an_instance_of_class_1, symbolToString(prop), typeToString(enclosingClass)); - return false; + // Otherwise, if the containing function is contextually typed by a function type with exactly one call signature + // and that call signature is non-generic, return statements are contextually typed by the return type of the signature + var signature = getContextualSignatureForFunctionLikeDeclaration(functionDecl); + if (signature) { + return getReturnTypeOfSignature(signature); } - return true; + return undefined; } - function checkNonNullExpression(node) { - var type = checkExpression(node); - if (strictNullChecks) { - var kind = getFalsyFlags(type) & 6144 /* Nullable */; - if (kind) { - error(node, kind & 2048 /* Undefined */ ? kind & 4096 /* Null */ ? - ts.Diagnostics.Object_is_possibly_null_or_undefined : - ts.Diagnostics.Object_is_possibly_undefined : - ts.Diagnostics.Object_is_possibly_null); - } - return getNonNullableType(type); + // In a typed function call, an argument or substitution expression is contextually typed by the type of the corresponding parameter. + function getContextualTypeForArgument(callTarget, arg) { + var args = getEffectiveCallArguments(callTarget); + var argIndex = ts.indexOf(args, arg); + if (argIndex >= 0) { + var signature = getResolvedOrAnySignature(callTarget); + return getTypeAtPosition(signature, argIndex); } - return type; - } - function checkPropertyAccessExpression(node) { - return checkPropertyAccessExpressionOrQualifiedName(node, node.expression, node.name); - } - function checkQualifiedName(node) { - return checkPropertyAccessExpressionOrQualifiedName(node, node.left, node.right); + return undefined; } - function checkPropertyAccessExpressionOrQualifiedName(node, left, right) { - var type = checkNonNullExpression(left); - if (isTypeAny(type) || type === silentNeverType) { - return type; - } - var apparentType = getApparentType(getWidenedType(type)); - if (apparentType === unknownType || (type.flags & 16384 /* TypeParameter */ && isTypeAny(apparentType))) { - // handle cases when type is Type parameter with invalid or any constraint - return apparentType; + function getContextualTypeForSubstitutionExpression(template, substitutionExpression) { + if (template.parent.kind === 176 /* TaggedTemplateExpression */) { + return getContextualTypeForArgument(template.parent, substitutionExpression); } - var prop = getPropertyOfType(apparentType, right.text); - if (!prop) { - if (right.text && !checkAndReportErrorForExtendingInterface(node)) { - reportNonexistentProperty(right, type.flags & 268435456 /* ThisType */ ? apparentType : type); + return undefined; + } + function getContextualTypeForBinaryOperand(node) { + var binaryExpression = node.parent; + var operator = binaryExpression.operatorToken.kind; + if (operator >= 56 /* FirstAssignment */ && operator <= 68 /* LastAssignment */) { + // Don't do this for special property assignments to avoid circularity + if (ts.getSpecialPropertyAssignmentKind(binaryExpression) !== 0 /* None */) { + return undefined; } - return unknownType; - } - if (noUnusedIdentifiers && - (prop.flags & 106500 /* ClassMember */) && - prop.valueDeclaration && (ts.getModifierFlags(prop.valueDeclaration) & 8 /* Private */)) { - if (prop.flags & 16777216 /* Instantiated */) { - getSymbolLinks(prop).target.isReferenced = true; + // In an assignment expression, the right operand is contextually typed by the type of the left operand. + if (node === binaryExpression.right) { + return checkExpression(binaryExpression.left); } - else { - prop.isReferenced = true; + } + else if (operator === 52 /* BarBarToken */) { + // When an || expression has a contextual type, the operands are contextually typed by that type. When an || + // expression has no contextual type, the right operand is contextually typed by the type of the left operand. + var type = getContextualType(binaryExpression); + if (!type && node === binaryExpression.right) { + type = checkExpression(binaryExpression.left); } + return type; } - getNodeLinks(node).resolvedSymbol = prop; - if (prop.parent && prop.parent.flags & 32 /* Class */) { - checkClassPropertyAccess(node, left, apparentType, prop); + else if (operator === 51 /* AmpersandAmpersandToken */ || operator === 24 /* CommaToken */) { + if (node === binaryExpression.right) { + return getContextualType(binaryExpression); + } } - var propType = getTypeOfSymbol(prop); - // Only compute control flow type if this is a property access expression that isn't an - // assignment target, and the referenced property was declared as a variable, property, - // accessor, or optional method. - if (node.kind !== 172 /* PropertyAccessExpression */ || ts.isAssignmentTarget(node) || - !(prop.flags & (3 /* Variable */ | 4 /* Property */ | 98304 /* Accessor */)) && - !(prop.flags & 8192 /* Method */ && propType.flags & 524288 /* Union */)) { - return propType; + return undefined; + } + // Apply a mapping function to a contextual type and return the resulting type. If the contextual type + // is a union type, the mapping function is applied to each constituent type and a union of the resulting + // types is returned. + function applyToContextualType(type, mapper) { + if (!(type.flags & 524288 /* Union */)) { + return mapper(type); } - return getFlowTypeOfReference(node, propType, /*assumeInitialized*/ true, /*flowContainer*/ undefined); - function reportNonexistentProperty(propNode, containingType) { - var errorInfo; - if (containingType.flags & 524288 /* Union */ && !(containingType.flags & 8190 /* Primitive */)) { - for (var _i = 0, _a = containingType.types; _i < _a.length; _i++) { - var subtype = _a[_i]; - if (!getPropertyOfType(subtype, propNode.text)) { - errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Property_0_does_not_exist_on_type_1, ts.declarationNameToString(propNode), typeToString(subtype)); - break; - } + var types = type.types; + var mappedType; + var mappedTypes; + for (var _i = 0, types_12 = types; _i < types_12.length; _i++) { + var current = types_12[_i]; + var t = mapper(current); + if (t) { + if (!mappedType) { + mappedType = t; + } + else if (!mappedTypes) { + mappedTypes = [mappedType, t]; + } + else { + mappedTypes.push(t); } } - errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Property_0_does_not_exist_on_type_1, ts.declarationNameToString(propNode), typeToString(containingType)); - diagnostics.add(ts.createDiagnosticForNodeFromMessageChain(propNode, errorInfo)); } + return mappedTypes ? getUnionType(mappedTypes) : mappedType; } - function isValidPropertyAccess(node, propertyName) { - var left = node.kind === 172 /* PropertyAccessExpression */ - ? node.expression - : node.left; - var type = checkExpression(left); - if (type !== unknownType && !isTypeAny(type)) { - var prop = getPropertyOfType(getWidenedType(type), propertyName); - if (prop && prop.parent && prop.parent.flags & 32 /* Class */) { - return checkClassPropertyAccess(node, left, type, prop); - } + function getTypeOfPropertyOfContextualType(type, name) { + return applyToContextualType(type, function (t) { + var prop = t.flags & 4161536 /* StructuredType */ ? getPropertyOfType(t, name) : undefined; + return prop ? getTypeOfSymbol(prop) : undefined; + }); + } + function getIndexTypeOfContextualType(type, kind) { + return applyToContextualType(type, function (t) { return getIndexTypeOfStructuredType(t, kind); }); + } + // Return true if the given contextual type is a tuple-like type + function contextualTypeIsTupleLikeType(type) { + return !!(type.flags & 524288 /* Union */ ? ts.forEach(type.types, isTupleLikeType) : isTupleLikeType(type)); + } + // In an object literal contextually typed by a type T, the contextual type of a property assignment is the type of + // the matching property in T, if one exists. Otherwise, it is the type of the numeric index signature in T, if one + // exists. Otherwise, it is the type of the string index signature in T, if one exists. + function getContextualTypeForObjectLiteralMethod(node) { + ts.Debug.assert(ts.isObjectLiteralMethod(node)); + if (isInsideWithStatementBody(node)) { + // We cannot answer semantic questions within a with block, do not proceed any further + return undefined; } - return true; + return getContextualTypeForObjectLiteralElement(node); } - /** - * Return the symbol of the for-in variable declared or referenced by the given for-in statement. - */ - function getForInVariableSymbol(node) { - var initializer = node.initializer; - if (initializer.kind === 219 /* VariableDeclarationList */) { - var variable = initializer.declarations[0]; - if (variable && !ts.isBindingPattern(variable.name)) { - return getSymbolOfNode(variable); + function getContextualTypeForObjectLiteralElement(element) { + var objectLiteral = element.parent; + var type = getApparentTypeOfContextualType(objectLiteral); + if (type) { + if (!ts.hasDynamicName(element)) { + // For a (non-symbol) computed property, there is no reason to look up the name + // in the type. It will just be "__computed", which does not appear in any + // SymbolTable. + var symbolName = getSymbolOfNode(element).name; + var propertyType = getTypeOfPropertyOfContextualType(type, symbolName); + if (propertyType) { + return propertyType; + } } + return isNumericName(element.name) && getIndexTypeOfContextualType(type, 1 /* Number */) || + getIndexTypeOfContextualType(type, 0 /* String */); } - else if (initializer.kind === 69 /* Identifier */) { - return getResolvedSymbol(initializer); + return undefined; + } + // In an array literal contextually typed by a type T, the contextual type of an element expression at index N is + // the type of the property with the numeric name N in T, if one exists. Otherwise, if T has a numeric index signature, + // it is the type of the numeric index signature in T. Otherwise, in ES6 and higher, the contextual type is the iterated + // type of T. + function getContextualTypeForElementExpression(node) { + var arrayLiteral = node.parent; + var type = getApparentTypeOfContextualType(arrayLiteral); + if (type) { + var index = ts.indexOf(arrayLiteral.elements, node); + return getTypeOfPropertyOfContextualType(type, "" + index) + || getIndexTypeOfContextualType(type, 1 /* Number */) + || (languageVersion >= 2 /* ES6 */ ? getElementTypeOfIterable(type, /*errorNode*/ undefined) : undefined); } return undefined; } - /** - * Return true if the given type is considered to have numeric property names. - */ - function hasNumericPropertyNames(type) { - return getIndexTypeOfType(type, 1 /* Number */) && !getIndexTypeOfType(type, 0 /* String */); + // In a contextually typed conditional expression, the true/false expressions are contextually typed by the same type. + function getContextualTypeForConditionalOperand(node) { + var conditional = node.parent; + return node === conditional.whenTrue || node === conditional.whenFalse ? getContextualType(conditional) : undefined; } - /** - * Return true if given node is an expression consisting of an identifier (possibly parenthesized) - * that references a for-in variable for an object with numeric property names. - */ - function isForInVariableForNumericPropertyNames(expr) { - var e = skipParenthesizedNodes(expr); - if (e.kind === 69 /* Identifier */) { - var symbol = getResolvedSymbol(e); - if (symbol.flags & 3 /* Variable */) { - var child = expr; - var node = expr.parent; - while (node) { - if (node.kind === 207 /* ForInStatement */ && - child === node.statement && - getForInVariableSymbol(node) === symbol && - hasNumericPropertyNames(checkExpression(node.expression))) { - return true; - } - child = node; - node = node.parent; - } + function getContextualTypeForJsxAttribute(attribute) { + var kind = attribute.kind; + var jsxElement = attribute.parent; + var attrsType = getJsxElementAttributesType(jsxElement); + if (attribute.kind === 246 /* JsxAttribute */) { + if (!attrsType || isTypeAny(attrsType)) { + return undefined; } + return getTypeOfPropertyOfType(attrsType, attribute.name.text); } - return false; + else if (attribute.kind === 247 /* JsxSpreadAttribute */) { + return attrsType; + } + ts.Debug.fail("Expected JsxAttribute or JsxSpreadAttribute, got ts.SyntaxKind[" + kind + "]"); } - function checkIndexedAccess(node) { - // Grammar checking - if (!node.argumentExpression) { - var sourceFile = ts.getSourceFileOfNode(node); - if (node.parent.kind === 175 /* NewExpression */ && node.parent.expression === node) { - var start = ts.skipTrivia(sourceFile.text, node.expression.end); - var end = node.end; - grammarErrorAtPos(sourceFile, start, end - start, ts.Diagnostics.new_T_cannot_be_used_to_create_an_array_Use_new_Array_T_instead); - } - else { - var start = node.end - "]".length; - var end = node.end; - grammarErrorAtPos(sourceFile, start, end - start, ts.Diagnostics.Expression_expected); - } + // Return the contextual type for a given expression node. During overload resolution, a contextual type may temporarily + // be "pushed" onto a node using the contextualType property. + function getApparentTypeOfContextualType(node) { + var type = getContextualType(node); + return type && getApparentType(type); + } + /** + * Woah! Do you really want to use this function? + * + * Unless you're trying to get the *non-apparent* type for a + * value-literal type or you're authoring relevant portions of this algorithm, + * you probably meant to use 'getApparentTypeOfContextualType'. + * Otherwise this may not be very useful. + * + * In cases where you *are* working on this function, you should understand + * when it is appropriate to use 'getContextualType' and 'getApparentTypeOfContextualType'. + * + * - Use 'getContextualType' when you are simply going to propagate the result to the expression. + * - Use 'getApparentTypeOfContextualType' when you're going to need the members of the type. + * + * @param node the expression whose contextual type will be returned. + * @returns the contextual type of an expression. + */ + function getContextualType(node) { + if (isInsideWithStatementBody(node)) { + // We cannot answer semantic questions within a with block, do not proceed any further + return undefined; } - // Obtain base constraint such that we can bail out if the constraint is an unknown type - var objectType = getApparentType(checkNonNullExpression(node.expression)); - var indexType = node.argumentExpression ? checkExpression(node.argumentExpression) : unknownType; - if (objectType === unknownType || objectType === silentNeverType) { - return objectType; + if (node.contextualType) { + return node.contextualType; } - var isConstEnum = isConstEnumObjectType(objectType); - if (isConstEnum && - (!node.argumentExpression || node.argumentExpression.kind !== 9 /* StringLiteral */)) { - error(node.argumentExpression, ts.Diagnostics.A_const_enum_member_can_only_be_accessed_using_a_string_literal); - return unknownType; - } - // TypeScript 1.0 spec (April 2014): 4.10 Property Access - // - If IndexExpr is a string literal or a numeric literal and ObjExpr's apparent type has a property with the name - // given by that literal(converted to its string representation in the case of a numeric literal), the property access is of the type of that property. - // - Otherwise, if ObjExpr's apparent type has a numeric index signature and IndexExpr is of type Any, the Number primitive type, or an enum type, - // the property access is of the type of that index signature. - // - Otherwise, if ObjExpr's apparent type has a string index signature and IndexExpr is of type Any, the String or Number primitive type, or an enum type, - // the property access is of the type of that index signature. - // - Otherwise, if IndexExpr is of type Any, the String or Number primitive type, or an enum type, the property access is of type Any. - // See if we can index as a property. - if (node.argumentExpression) { - var name_16 = getPropertyNameForIndexedAccess(node.argumentExpression, indexType); - if (name_16 !== undefined) { - var prop = getPropertyOfType(objectType, name_16); - if (prop) { - getNodeLinks(node).resolvedSymbol = prop; - return getTypeOfSymbol(prop); - } - else if (isConstEnum) { - error(node.argumentExpression, ts.Diagnostics.Property_0_does_not_exist_on_const_enum_1, name_16, symbolToString(objectType.symbol)); - return unknownType; - } - } + var parent = node.parent; + switch (parent.kind) { + case 218 /* VariableDeclaration */: + case 142 /* Parameter */: + case 145 /* PropertyDeclaration */: + case 144 /* PropertySignature */: + case 169 /* BindingElement */: + return getContextualTypeForInitializerExpression(node); + case 180 /* ArrowFunction */: + case 211 /* ReturnStatement */: + return getContextualTypeForReturnExpression(node); + case 190 /* YieldExpression */: + return getContextualTypeForYieldOperand(parent); + case 174 /* CallExpression */: + case 175 /* NewExpression */: + return getContextualTypeForArgument(parent, node); + case 177 /* TypeAssertionExpression */: + case 195 /* AsExpression */: + return getTypeFromTypeNode(parent.type); + case 187 /* BinaryExpression */: + return getContextualTypeForBinaryOperand(node); + case 253 /* PropertyAssignment */: + case 254 /* ShorthandPropertyAssignment */: + return getContextualTypeForObjectLiteralElement(parent); + case 170 /* ArrayLiteralExpression */: + return getContextualTypeForElementExpression(node); + case 188 /* ConditionalExpression */: + return getContextualTypeForConditionalOperand(node); + case 197 /* TemplateSpan */: + ts.Debug.assert(parent.parent.kind === 189 /* TemplateExpression */); + return getContextualTypeForSubstitutionExpression(parent.parent, node); + case 178 /* ParenthesizedExpression */: + return getContextualType(parent); + case 248 /* JsxExpression */: + return getContextualType(parent); + case 246 /* JsxAttribute */: + case 247 /* JsxSpreadAttribute */: + return getContextualTypeForJsxAttribute(parent); } - // Check for compatible indexer types. - var allowedNullableFlags = strictNullChecks ? 0 : 6144 /* Nullable */; - if (isTypeAnyOrAllConstituentTypesHaveKind(indexType, 34 /* StringLike */ | 340 /* NumberLike */ | 512 /* ESSymbol */ | allowedNullableFlags)) { - // Try to use a number indexer. - if (isTypeAnyOrAllConstituentTypesHaveKind(indexType, 340 /* NumberLike */ | allowedNullableFlags) || isForInVariableForNumericPropertyNames(node.argumentExpression)) { - var numberIndexInfo = getIndexInfoOfType(objectType, 1 /* Number */); - if (numberIndexInfo) { - getNodeLinks(node).resolvedIndexInfo = numberIndexInfo; - return numberIndexInfo.type; - } - } - // Try to use string indexing. - var stringIndexInfo = getIndexInfoOfType(objectType, 0 /* String */); - if (stringIndexInfo) { - getNodeLinks(node).resolvedIndexInfo = stringIndexInfo; - return stringIndexInfo.type; - } - // Fall back to any. - if (compilerOptions.noImplicitAny && !compilerOptions.suppressImplicitAnyIndexErrors && !isTypeAny(objectType)) { - error(node, getIndexTypeOfType(objectType, 1 /* Number */) ? - ts.Diagnostics.Element_implicitly_has_an_any_type_because_index_expression_is_not_of_type_number : - ts.Diagnostics.Index_signature_of_object_type_implicitly_has_an_any_type); + return undefined; + } + // If the given type is an object or union type, if that type has a single signature, and if + // that signature is non-generic, return the signature. Otherwise return undefined. + function getNonGenericSignature(type) { + var signatures = getSignaturesOfStructuredType(type, 0 /* Call */); + if (signatures.length === 1) { + var signature = signatures[0]; + if (!signature.typeParameters) { + return signature; } - return anyType; } - // REVIEW: Users should know the type that was actually used. - error(node, ts.Diagnostics.An_index_expression_argument_must_be_of_type_string_number_symbol_or_any); - return unknownType; } - /** - * If indexArgumentExpression is a string literal or number literal, returns its text. - * If indexArgumentExpression is a constant value, returns its string value. - * If indexArgumentExpression is a well known symbol, returns the property name corresponding - * to this symbol, as long as it is a proper symbol reference. - * Otherwise, returns undefined. - */ - function getPropertyNameForIndexedAccess(indexArgumentExpression, indexArgumentType) { - if (indexArgumentExpression.kind === 9 /* StringLiteral */ || indexArgumentExpression.kind === 8 /* NumericLiteral */) { - return indexArgumentExpression.text; + function isFunctionExpressionOrArrowFunction(node) { + return node.kind === 179 /* FunctionExpression */ || node.kind === 180 /* ArrowFunction */; + } + function getContextualSignatureForFunctionLikeDeclaration(node) { + // Only function expressions, arrow functions, and object literal methods are contextually typed. + return isFunctionExpressionOrArrowFunction(node) || ts.isObjectLiteralMethod(node) + ? getContextualSignature(node) + : undefined; + } + function getContextualTypeForFunctionLikeDeclaration(node) { + return ts.isObjectLiteralMethod(node) ? + getContextualTypeForObjectLiteralMethod(node) : + getApparentTypeOfContextualType(node); + } + // Return the contextual signature for a given expression node. A contextual type provides a + // contextual signature if it has a single call signature and if that call signature is non-generic. + // If the contextual type is a union type, get the signature from each type possible and if they are + // all identical ignoring their return type, the result is same signature but with return type as + // union type of return types from these signatures + function getContextualSignature(node) { + ts.Debug.assert(node.kind !== 147 /* MethodDeclaration */ || ts.isObjectLiteralMethod(node)); + var type = getContextualTypeForFunctionLikeDeclaration(node); + if (!type) { + return undefined; } - if (indexArgumentExpression.kind === 173 /* ElementAccessExpression */ || indexArgumentExpression.kind === 172 /* PropertyAccessExpression */) { - var value = getConstantValue(indexArgumentExpression); - if (value !== undefined) { - return value.toString(); + if (!(type.flags & 524288 /* Union */)) { + return getNonGenericSignature(type); + } + var signatureList; + var types = type.types; + for (var _i = 0, types_13 = types; _i < types_13.length; _i++) { + var current = types_13[_i]; + var signature = getNonGenericSignature(current); + if (signature) { + if (!signatureList) { + // This signature will contribute to contextual union signature + signatureList = [signature]; + } + else if (!compareSignaturesIdentical(signatureList[0], signature, /*partialMatch*/ false, /*ignoreThisTypes*/ true, /*ignoreReturnTypes*/ true, compareTypesIdentical)) { + // Signatures aren't identical, do not use + return undefined; + } + else { + // Use this signature for contextual union signature + signatureList.push(signature); + } } } - if (checkThatExpressionIsProperSymbolReference(indexArgumentExpression, indexArgumentType, /*reportError*/ false)) { - var rightHandSideName = indexArgumentExpression.name.text; - return ts.getPropertyNameForKnownSymbolName(rightHandSideName); + // Result is union of signatures collected (return type is union of return types of this signature set) + var result; + if (signatureList) { + result = cloneSignature(signatureList[0]); + // Clear resolved return type we possibly got from cloneSignature + result.resolvedReturnType = undefined; + result.unionSignatures = signatureList; } - return undefined; + return result; } /** - * A proper symbol reference requires the following: - * 1. The property access denotes a property that exists - * 2. The expression is of the form Symbol. - * 3. The property access is of the primitive type symbol. - * 4. Symbol in this context resolves to the global Symbol object + * Detect if the mapper implies an inference context. Specifically, there are 4 possible values + * for a mapper. Let's go through each one of them: + * + * 1. undefined - this means we are not doing inferential typing, but we may do contextual typing, + * which could cause us to assign a parameter a type + * 2. identityMapper - means we want to avoid assigning a parameter a type, whether or not we are in + * inferential typing (context is undefined for the identityMapper) + * 3. a mapper created by createInferenceMapper - we are doing inferential typing, we want to assign + * types to parameters and fix type parameters (context is defined) + * 4. an instantiation mapper created by createTypeMapper or createTypeEraser - this should never be + * passed as the contextual mapper when checking an expression (context is undefined for these) + * + * isInferentialContext is detecting if we are in case 3 */ - function checkThatExpressionIsProperSymbolReference(expression, expressionType, reportError) { - if (expressionType === unknownType) { - // There is already an error, so no need to report one. - return false; - } - if (!ts.isWellKnownSymbolSyntactically(expression)) { - return false; - } - // Make sure the property type is the primitive symbol type - if ((expressionType.flags & 512 /* ESSymbol */) === 0) { - if (reportError) { - error(expression, ts.Diagnostics.A_computed_property_name_of_the_form_0_must_be_of_type_symbol, ts.getTextOfNode(expression)); - } - return false; - } - // The name is Symbol., so make sure Symbol actually resolves to the - // global Symbol object - var leftHandSide = expression.expression; - var leftHandSideSymbol = getResolvedSymbol(leftHandSide); - if (!leftHandSideSymbol) { - return false; - } - var globalESSymbol = getGlobalESSymbolConstructorSymbol(); - if (!globalESSymbol) { - // Already errored when we tried to look up the symbol - return false; - } - if (leftHandSideSymbol !== globalESSymbol) { - if (reportError) { - error(leftHandSide, ts.Diagnostics.Symbol_reference_does_not_refer_to_the_global_Symbol_constructor_object); - } - return false; - } - return true; + function isInferentialContext(mapper) { + return mapper && mapper.context; } - function resolveUntypedCall(node) { - if (node.kind === 176 /* TaggedTemplateExpression */) { - checkExpression(node.template); - } - else if (node.kind !== 143 /* Decorator */) { - ts.forEach(node.arguments, function (argument) { - checkExpression(argument); - }); - } - return anySignature; + function checkSpreadElementExpression(node, contextualMapper) { + // It is usually not safe to call checkExpressionCached if we can be contextually typing. + // You can tell that we are contextually typing because of the contextualMapper parameter. + // While it is true that a spread element can have a contextual type, it does not do anything + // with this type. It is neither affected by it, nor does it propagate it to its operand. + // So the fact that contextualMapper is passed is not important, because the operand of a spread + // element is not contextually typed. + var arrayOrIterableType = checkExpressionCached(node.expression, contextualMapper); + return checkIteratedTypeOrElementType(arrayOrIterableType, node.expression, /*allowStringInput*/ false); } - function resolveErrorCall(node) { - resolveUntypedCall(node); - return unknownSignature; + function hasDefaultValue(node) { + return (node.kind === 169 /* BindingElement */ && !!node.initializer) || + (node.kind === 187 /* BinaryExpression */ && node.operatorToken.kind === 56 /* EqualsToken */); } - // Re-order candidate signatures into the result array. Assumes the result array to be empty. - // The candidate list orders groups in reverse, but within a group signatures are kept in declaration order - // A nit here is that we reorder only signatures that belong to the same symbol, - // so order how inherited signatures are processed is still preserved. - // interface A { (x: string): void } - // interface B extends A { (x: 'foo'): string } - // const b: B; - // b('foo') // <- here overloads should be processed as [(x:'foo'): string, (x: string): void] - function reorderCandidates(signatures, result) { - var lastParent; - var lastSymbol; - var cutoffIndex = 0; - var index; - var specializedIndex = -1; - var spliceIndex; - ts.Debug.assert(!result.length); - for (var _i = 0, signatures_2 = signatures; _i < signatures_2.length; _i++) { - var signature = signatures_2[_i]; - var symbol = signature.declaration && getSymbolOfNode(signature.declaration); - var parent_10 = signature.declaration && signature.declaration.parent; - if (!lastSymbol || symbol === lastSymbol) { - if (lastParent && parent_10 === lastParent) { - index++; - } - else { - lastParent = parent_10; - index = cutoffIndex; + function checkArrayLiteral(node, contextualMapper) { + var elements = node.elements; + var hasSpreadElement = false; + var elementTypes = []; + var inDestructuringPattern = ts.isAssignmentTarget(node); + for (var _i = 0, elements_1 = elements; _i < elements_1.length; _i++) { + var e = elements_1[_i]; + if (inDestructuringPattern && e.kind === 191 /* SpreadElementExpression */) { + // Given the following situation: + // var c: {}; + // [...c] = ["", 0]; + // + // c is represented in the tree as a spread element in an array literal. + // But c really functions as a rest element, and its purpose is to provide + // a contextual type for the right hand side of the assignment. Therefore, + // instead of calling checkExpression on "...c", which will give an error + // if c is not iterable/array-like, we need to act as if we are trying to + // get the contextual element type from it. So we do something similar to + // getContextualTypeForElementExpression, which will crucially not error + // if there is no index type / iterated type. + var restArrayType = checkExpression(e.expression, contextualMapper); + var restElementType = getIndexTypeOfType(restArrayType, 1 /* Number */) || + (languageVersion >= 2 /* ES6 */ ? getElementTypeOfIterable(restArrayType, /*errorNode*/ undefined) : undefined); + if (restElementType) { + elementTypes.push(restElementType); } } else { - // current declaration belongs to a different symbol - // set cutoffIndex so re-orderings in the future won't change result set from 0 to cutoffIndex - index = cutoffIndex = result.length; - lastParent = parent_10; + var type = checkExpressionForMutableLocation(e, contextualMapper); + elementTypes.push(type); } - lastSymbol = symbol; - // specialized signatures always need to be placed before non-specialized signatures regardless - // of the cutoff position; see GH#1133 - if (signature.hasLiteralTypes) { - specializedIndex++; - spliceIndex = specializedIndex; - // The cutoff index always needs to be greater than or equal to the specialized signature index - // in order to prevent non-specialized signatures from being added before a specialized - // signature. - cutoffIndex++; + hasSpreadElement = hasSpreadElement || e.kind === 191 /* SpreadElementExpression */; + } + if (!hasSpreadElement) { + // If array literal is actually a destructuring pattern, mark it as an implied type. We do this such + // that we get the same behavior for "var [x, y] = []" and "[x, y] = []". + if (inDestructuringPattern && elementTypes.length) { + var type = cloneTypeReference(createTupleType(elementTypes)); + type.pattern = node; + return type; } - else { - spliceIndex = index; + var contextualType = getApparentTypeOfContextualType(node); + if (contextualType && contextualTypeIsTupleLikeType(contextualType)) { + var pattern = contextualType.pattern; + // If array literal is contextually typed by a binding pattern or an assignment pattern, pad the resulting + // tuple type with the corresponding binding or assignment element types to make the lengths equal. + if (pattern && (pattern.kind === 168 /* ArrayBindingPattern */ || pattern.kind === 170 /* ArrayLiteralExpression */)) { + var patternElements = pattern.elements; + for (var i = elementTypes.length; i < patternElements.length; i++) { + var patternElement = patternElements[i]; + if (hasDefaultValue(patternElement)) { + elementTypes.push(contextualType.typeArguments[i]); + } + else { + if (patternElement.kind !== 193 /* OmittedExpression */) { + error(patternElement, ts.Diagnostics.Initializer_provides_no_value_for_this_binding_element_and_the_binding_element_has_no_default_value); + } + elementTypes.push(unknownType); + } + } + } + if (elementTypes.length) { + return createTupleType(elementTypes); + } } - result.splice(spliceIndex, 0, signature); } + return createArrayType(elementTypes.length ? + getUnionType(elementTypes, /*subtypeReduction*/ true) : + strictNullChecks ? neverType : undefinedWideningType); } - function getSpreadArgumentIndex(args) { - for (var i = 0; i < args.length; i++) { - var arg = args[i]; - if (arg && arg.kind === 191 /* SpreadElementExpression */) { - return i; - } - } - return -1; + function isNumericName(name) { + return name.kind === 140 /* ComputedPropertyName */ ? isNumericComputedName(name) : isNumericLiteralName(name.text); } - function hasCorrectArity(node, args, signature, signatureHelpTrailingComma) { - if (signatureHelpTrailingComma === void 0) { signatureHelpTrailingComma = false; } - var argCount; // Apparent number of arguments we will have in this call - var typeArguments; // Type arguments (undefined if none) - var callIsIncomplete; // In incomplete call we want to be lenient when we have too few arguments - var isDecorator; - var spreadArgIndex = -1; - if (node.kind === 176 /* TaggedTemplateExpression */) { - var tagExpression = node; - // Even if the call is incomplete, we'll have a missing expression as our last argument, - // so we can say the count is just the arg list length - argCount = args.length; - typeArguments = undefined; - if (tagExpression.template.kind === 189 /* TemplateExpression */) { - // If a tagged template expression lacks a tail literal, the call is incomplete. - // Specifically, a template only can end in a TemplateTail or a Missing literal. - var templateExpression = tagExpression.template; - var lastSpan = ts.lastOrUndefined(templateExpression.templateSpans); - ts.Debug.assert(lastSpan !== undefined); // we should always have at least one span. - callIsIncomplete = ts.nodeIsMissing(lastSpan.literal) || !!lastSpan.literal.isUnterminated; + function isNumericComputedName(name) { + // It seems odd to consider an expression of type Any to result in a numeric name, + // but this behavior is consistent with checkIndexedAccess + return isTypeAnyOrAllConstituentTypesHaveKind(checkComputedPropertyName(name), 340 /* NumberLike */); + } + function isTypeAnyOrAllConstituentTypesHaveKind(type, kind) { + return isTypeAny(type) || isTypeOfKind(type, kind); + } + function isInfinityOrNaNString(name) { + return name === "Infinity" || name === "-Infinity" || name === "NaN"; + } + function isNumericLiteralName(name) { + // The intent of numeric names is that + // - they are names with text in a numeric form, and that + // - setting properties/indexing with them is always equivalent to doing so with the numeric literal 'numLit', + // acquired by applying the abstract 'ToNumber' operation on the name's text. + // + // The subtlety is in the latter portion, as we cannot reliably say that anything that looks like a numeric literal is a numeric name. + // In fact, it is the case that the text of the name must be equal to 'ToString(numLit)' for this to hold. + // + // Consider the property name '"0xF00D"'. When one indexes with '0xF00D', they are actually indexing with the value of 'ToString(0xF00D)' + // according to the ECMAScript specification, so it is actually as if the user indexed with the string '"61453"'. + // Thus, the text of all numeric literals equivalent to '61543' such as '0xF00D', '0xf00D', '0170015', etc. are not valid numeric names + // because their 'ToString' representation is not equal to their original text. + // This is motivated by ECMA-262 sections 9.3.1, 9.8.1, 11.1.5, and 11.2.1. + // + // Here, we test whether 'ToString(ToNumber(name))' is exactly equal to 'name'. + // The '+' prefix operator is equivalent here to applying the abstract ToNumber operation. + // Applying the 'toString()' method on a number gives us the abstract ToString operation on a number. + // + // Note that this accepts the values 'Infinity', '-Infinity', and 'NaN', and that this is intentional. + // This is desired behavior, because when indexing with them as numeric entities, you are indexing + // with the strings '"Infinity"', '"-Infinity"', and '"NaN"' respectively. + return (+name).toString() === name; + } + function checkComputedPropertyName(node) { + var links = getNodeLinks(node.expression); + if (!links.resolvedType) { + links.resolvedType = checkExpression(node.expression); + // This will allow types number, string, symbol or any. It will also allow enums, the unknown + // type, and any union of these types (like string | number). + if (!isTypeAnyOrAllConstituentTypesHaveKind(links.resolvedType, 340 /* NumberLike */ | 34 /* StringLike */ | 512 /* ESSymbol */)) { + error(node, ts.Diagnostics.A_computed_property_name_must_be_of_type_string_number_symbol_or_any); } else { - // If the template didn't end in a backtick, or its beginning occurred right prior to EOF, - // then this might actually turn out to be a TemplateHead in the future; - // so we consider the call to be incomplete. - var templateLiteral = tagExpression.template; - ts.Debug.assert(templateLiteral.kind === 11 /* NoSubstitutionTemplateLiteral */); - callIsIncomplete = !!templateLiteral.isUnterminated; - } - } - else if (node.kind === 143 /* Decorator */) { - isDecorator = true; - typeArguments = undefined; - argCount = getEffectiveArgumentCount(node, /*args*/ undefined, signature); - } - else { - var callExpression = node; - if (!callExpression.arguments) { - // This only happens when we have something of the form: 'new C' - ts.Debug.assert(callExpression.kind === 175 /* NewExpression */); - return signature.minArgumentCount === 0; + checkThatExpressionIsProperSymbolReference(node.expression, links.resolvedType, /*reportError*/ true); } - argCount = signatureHelpTrailingComma ? args.length + 1 : args.length; - // If we are missing the close paren, the call is incomplete. - callIsIncomplete = callExpression.arguments.end === callExpression.end; - typeArguments = callExpression.typeArguments; - spreadArgIndex = getSpreadArgumentIndex(args); - } - // If the user supplied type arguments, but the number of type arguments does not match - // the declared number of type parameters, the call has an incorrect arity. - var hasRightNumberOfTypeArgs = !typeArguments || - (signature.typeParameters && typeArguments.length === signature.typeParameters.length); - if (!hasRightNumberOfTypeArgs) { - return false; - } - // If spread arguments are present, check that they correspond to a rest parameter. If so, no - // further checking is necessary. - if (spreadArgIndex >= 0) { - return isRestParameterIndex(signature, spreadArgIndex); - } - // Too many arguments implies incorrect arity. - if (!signature.hasRestParameter && argCount > signature.parameters.length) { - return false; } - // If the call is incomplete, we should skip the lower bound check. - var hasEnoughArguments = argCount >= signature.minArgumentCount; - return callIsIncomplete || hasEnoughArguments; + return links.resolvedType; } - // If type has a single call signature and no other members, return that signature. Otherwise, return undefined. - function getSingleCallSignature(type) { - if (type.flags & 2588672 /* ObjectType */) { - var resolved = resolveStructuredTypeMembers(type); - if (resolved.callSignatures.length === 1 && resolved.constructSignatures.length === 0 && - resolved.properties.length === 0 && !resolved.stringIndexInfo && !resolved.numberIndexInfo) { - return resolved.callSignatures[0]; + function getObjectLiteralIndexInfo(node, properties, kind) { + var propTypes = []; + for (var i = 0; i < properties.length; i++) { + if (kind === 0 /* String */ || isNumericName(node.properties[i].name)) { + propTypes.push(getTypeOfSymbol(properties[i])); } } - return undefined; - } - // Instantiate a generic signature in the context of a non-generic signature (section 3.8.5 in TypeScript spec) - function instantiateSignatureInContextOf(signature, contextualSignature, contextualMapper) { - var context = createInferenceContext(signature, /*inferUnionTypes*/ true); - forEachMatchingParameterType(contextualSignature, signature, function (source, target) { - // Type parameters from outer context referenced by source type are fixed by instantiation of the source type - inferTypes(context, instantiateType(source, contextualMapper), target); - }); - return getSignatureInstantiation(signature, getInferredTypes(context)); + var unionType = propTypes.length ? getUnionType(propTypes, /*subtypeReduction*/ true) : undefinedType; + return createIndexInfo(unionType, /*isReadonly*/ false); } - function inferTypeArguments(node, signature, args, excludeArgument, context) { - var typeParameters = signature.typeParameters; - var inferenceMapper = getInferenceMapper(context); - // Clear out all the inference results from the last time inferTypeArguments was called on this context - for (var i = 0; i < typeParameters.length; i++) { - // As an optimization, we don't have to clear (and later recompute) inferred types - // for type parameters that have already been fixed on the previous call to inferTypeArguments. - // It would be just as correct to reset all of them. But then we'd be repeating the same work - // for the type parameters that were fixed, namely the work done by getInferredType. - if (!context.inferences[i].isFixed) { - context.inferredTypes[i] = undefined; - } - } - // On this call to inferTypeArguments, we may get more inferences for certain type parameters that were not - // fixed last time. This means that a type parameter that failed inference last time may succeed this time, - // or vice versa. Therefore, the failedTypeParameterIndex is useless if it points to an unfixed type parameter, - // because it may change. So here we reset it. However, getInferredType will not revisit any type parameters - // that were previously fixed. So if a fixed type parameter failed previously, it will fail again because - // it will contain the exact same set of inferences. So if we reset the index from a fixed type parameter, - // we will lose information that we won't recover this time around. - if (context.failedTypeParameterIndex !== undefined && !context.inferences[context.failedTypeParameterIndex].isFixed) { - context.failedTypeParameterIndex = undefined; - } - var thisType = getThisTypeOfSignature(signature); - if (thisType) { - var thisArgumentNode = getThisArgumentOfCall(node); - var thisArgumentType = thisArgumentNode ? checkExpression(thisArgumentNode) : voidType; - inferTypes(context, thisArgumentType, thisType); - } - // We perform two passes over the arguments. In the first pass we infer from all arguments, but use - // wildcards for all context sensitive function expressions. - var argCount = getEffectiveArgumentCount(node, args, signature); - for (var i = 0; i < argCount; i++) { - var arg = getEffectiveArgument(node, args, i); - // If the effective argument is 'undefined', then it is an argument that is present but is synthetic. - if (arg === undefined || arg.kind !== 193 /* OmittedExpression */) { - var paramType = getTypeAtPosition(signature, i); - var argType = getEffectiveArgumentType(node, i, arg); - // If the effective argument type is 'undefined', there is no synthetic type - // for the argument. In that case, we should check the argument. - if (argType === undefined) { - // For context sensitive arguments we pass the identityMapper, which is a signal to treat all - // context sensitive function expressions as wildcards - var mapper = excludeArgument && excludeArgument[i] !== undefined ? identityMapper : inferenceMapper; - argType = checkExpressionWithContextualType(arg, paramType, mapper); + function checkObjectLiteral(node, contextualMapper) { + var inDestructuringPattern = ts.isAssignmentTarget(node); + // Grammar checking + checkGrammarObjectLiteralExpression(node, inDestructuringPattern); + var propertiesTable = ts.createMap(); + var propertiesArray = []; + var contextualType = getApparentTypeOfContextualType(node); + var contextualTypeHasPattern = contextualType && contextualType.pattern && + (contextualType.pattern.kind === 167 /* ObjectBindingPattern */ || contextualType.pattern.kind === 171 /* ObjectLiteralExpression */); + var typeFlags = 0; + var patternWithComputedProperties = false; + var hasComputedStringProperty = false; + var hasComputedNumberProperty = false; + for (var _i = 0, _a = node.properties; _i < _a.length; _i++) { + var memberDecl = _a[_i]; + var member = memberDecl.symbol; + if (memberDecl.kind === 253 /* PropertyAssignment */ || + memberDecl.kind === 254 /* ShorthandPropertyAssignment */ || + ts.isObjectLiteralMethod(memberDecl)) { + var type = void 0; + if (memberDecl.kind === 253 /* PropertyAssignment */) { + type = checkPropertyAssignment(memberDecl, contextualMapper); } - inferTypes(context, argType, paramType); + else if (memberDecl.kind === 147 /* MethodDeclaration */) { + type = checkObjectLiteralMethod(memberDecl, contextualMapper); + } + else { + ts.Debug.assert(memberDecl.kind === 254 /* ShorthandPropertyAssignment */); + type = checkExpressionForMutableLocation(memberDecl.name, contextualMapper); + } + typeFlags |= type.flags; + var prop = createSymbol(4 /* Property */ | 67108864 /* Transient */ | member.flags, member.name); + if (inDestructuringPattern) { + // If object literal is an assignment pattern and if the assignment pattern specifies a default value + // for the property, make the property optional. + var isOptional = (memberDecl.kind === 253 /* PropertyAssignment */ && hasDefaultValue(memberDecl.initializer)) || + (memberDecl.kind === 254 /* ShorthandPropertyAssignment */ && memberDecl.objectAssignmentInitializer); + if (isOptional) { + prop.flags |= 536870912 /* Optional */; + } + if (ts.hasDynamicName(memberDecl)) { + patternWithComputedProperties = true; + } + } + else if (contextualTypeHasPattern && + !(contextualType.flags & 2588672 /* ObjectType */ && contextualType.isObjectLiteralPatternWithComputedProperties)) { + // If object literal is contextually typed by the implied type of a binding pattern, and if the + // binding pattern specifies a default value for the property, make the property optional. + var impliedProp = getPropertyOfType(contextualType, member.name); + if (impliedProp) { + prop.flags |= impliedProp.flags & 536870912 /* Optional */; + } + else if (!compilerOptions.suppressExcessPropertyErrors) { + error(memberDecl.name, ts.Diagnostics.Object_literal_may_only_specify_known_properties_and_0_does_not_exist_in_type_1, symbolToString(member), typeToString(contextualType)); + } + } + prop.declarations = member.declarations; + prop.parent = member.parent; + if (member.valueDeclaration) { + prop.valueDeclaration = member.valueDeclaration; + } + prop.type = type; + prop.target = member; + member = prop; } - } - // In the second pass we visit only context sensitive arguments, and only those that aren't excluded, this - // time treating function expressions normally (which may cause previously inferred type arguments to be fixed - // as we construct types for contextually typed parameters) - // Decorators will not have `excludeArgument`, as their arguments cannot be contextually typed. - // Tagged template expressions will always have `undefined` for `excludeArgument[0]`. - if (excludeArgument) { - for (var i = 0; i < argCount; i++) { - // No need to check for omitted args and template expressions, their exclusion value is always undefined - if (excludeArgument[i] === false) { - var arg = args[i]; - var paramType = getTypeAtPosition(signature, i); - inferTypes(context, checkExpressionWithContextualType(arg, paramType, inferenceMapper), paramType); + else { + // TypeScript 1.0 spec (April 2014) + // A get accessor declaration is processed in the same manner as + // an ordinary function declaration(section 6.1) with no parameters. + // A set accessor declaration is processed in the same manner + // as an ordinary function declaration with a single parameter and a Void return type. + ts.Debug.assert(memberDecl.kind === 149 /* GetAccessor */ || memberDecl.kind === 150 /* SetAccessor */); + checkAccessorDeclaration(memberDecl); + } + if (ts.hasDynamicName(memberDecl)) { + if (isNumericName(memberDecl.name)) { + hasComputedNumberProperty = true; + } + else { + hasComputedStringProperty = true; } } + else { + propertiesTable[member.name] = member; + } + propertiesArray.push(member); } - getInferredTypes(context); - } - function checkTypeArguments(signature, typeArgumentNodes, typeArgumentTypes, reportErrors, headMessage) { - var typeParameters = signature.typeParameters; - var typeArgumentsAreAssignable = true; - var mapper; - for (var i = 0; i < typeParameters.length; i++) { - if (typeArgumentsAreAssignable /* so far */) { - var constraint = getConstraintOfTypeParameter(typeParameters[i]); - if (constraint) { - var errorInfo = void 0; - var typeArgumentHeadMessage = ts.Diagnostics.Type_0_does_not_satisfy_the_constraint_1; - if (reportErrors && headMessage) { - errorInfo = ts.chainDiagnosticMessages(errorInfo, typeArgumentHeadMessage); - typeArgumentHeadMessage = headMessage; - } - if (!mapper) { - mapper = createTypeMapper(typeParameters, typeArgumentTypes); + // If object literal is contextually typed by the implied type of a binding pattern, augment the result + // type with those properties for which the binding pattern specifies a default value. + if (contextualTypeHasPattern) { + for (var _b = 0, _c = getPropertiesOfType(contextualType); _b < _c.length; _b++) { + var prop = _c[_b]; + if (!propertiesTable[prop.name]) { + if (!(prop.flags & 536870912 /* Optional */)) { + error(prop.valueDeclaration || prop.bindingElement, ts.Diagnostics.Initializer_provides_no_value_for_this_binding_element_and_the_binding_element_has_no_default_value); } - var typeArgument = typeArgumentTypes[i]; - typeArgumentsAreAssignable = checkTypeAssignableTo(typeArgument, getTypeWithThisArgument(instantiateType(constraint, mapper), typeArgument), reportErrors ? typeArgumentNodes[i] : undefined, typeArgumentHeadMessage, errorInfo); + propertiesTable[prop.name] = prop; + propertiesArray.push(prop); } } } - return typeArgumentsAreAssignable; + var stringIndexInfo = hasComputedStringProperty ? getObjectLiteralIndexInfo(node, propertiesArray, 0 /* String */) : undefined; + var numberIndexInfo = hasComputedNumberProperty ? getObjectLiteralIndexInfo(node, propertiesArray, 1 /* Number */) : undefined; + var result = createAnonymousType(node.symbol, propertiesTable, emptyArray, emptyArray, stringIndexInfo, numberIndexInfo); + var freshObjectLiteralFlag = compilerOptions.suppressExcessPropertyErrors ? 0 : 16777216 /* FreshLiteral */; + result.flags |= 8388608 /* ObjectLiteral */ | 67108864 /* ContainsObjectLiteral */ | freshObjectLiteralFlag | (typeFlags & 234881024 /* PropagatingFlags */); + if (patternWithComputedProperties) { + result.isObjectLiteralPatternWithComputedProperties = true; + } + if (inDestructuringPattern) { + result.pattern = node; + } + return result; } - function checkApplicableSignature(node, args, signature, relation, excludeArgument, reportErrors) { - var thisType = getThisTypeOfSignature(signature); - if (thisType && thisType !== voidType && node.kind !== 175 /* NewExpression */) { - // If the called expression is not of the form `x.f` or `x["f"]`, then sourceType = voidType - // If the signature's 'this' type is voidType, then the check is skipped -- anything is compatible. - // If the expression is a new expression, then the check is skipped. - var thisArgumentNode = getThisArgumentOfCall(node); - var thisArgumentType = thisArgumentNode ? checkExpression(thisArgumentNode) : voidType; - var errorNode = reportErrors ? (thisArgumentNode || node) : undefined; - var headMessage_1 = ts.Diagnostics.The_this_context_of_type_0_is_not_assignable_to_method_s_this_of_type_1; - if (!checkTypeRelatedTo(thisArgumentType, getThisTypeOfSignature(signature), relation, errorNode, headMessage_1)) { - return false; - } + function checkJsxSelfClosingElement(node) { + checkJsxOpeningLikeElement(node); + return jsxElementType || anyType; + } + function checkJsxElement(node) { + // Check attributes + checkJsxOpeningLikeElement(node.openingElement); + // Perform resolution on the closing tag so that rename/go to definition/etc work + if (isJsxIntrinsicIdentifier(node.closingElement.tagName)) { + getIntrinsicTagSymbol(node.closingElement); } - var headMessage = ts.Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1; - var argCount = getEffectiveArgumentCount(node, args, signature); - for (var i = 0; i < argCount; i++) { - var arg = getEffectiveArgument(node, args, i); - // If the effective argument is 'undefined', then it is an argument that is present but is synthetic. - if (arg === undefined || arg.kind !== 193 /* OmittedExpression */) { - // Check spread elements against rest type (from arity check we know spread argument corresponds to a rest parameter) - var paramType = getTypeAtPosition(signature, i); - var argType = getEffectiveArgumentType(node, i, arg); - // If the effective argument type is 'undefined', there is no synthetic type - // for the argument. In that case, we should check the argument. - if (argType === undefined) { - argType = checkExpressionWithContextualType(arg, paramType, excludeArgument && excludeArgument[i] ? identityMapper : undefined); - } - // Use argument expression as error location when reporting errors - var errorNode = reportErrors ? getEffectiveArgumentErrorNode(node, i, arg) : undefined; - if (!checkTypeRelatedTo(argType, paramType, relation, errorNode, headMessage)) { - return false; - } + else { + checkExpression(node.closingElement.tagName); + } + // Check children + for (var _i = 0, _a = node.children; _i < _a.length; _i++) { + var child = _a[_i]; + switch (child.kind) { + case 248 /* JsxExpression */: + checkJsxExpression(child); + break; + case 241 /* JsxElement */: + checkJsxElement(child); + break; + case 242 /* JsxSelfClosingElement */: + checkJsxSelfClosingElement(child); + break; } } - return true; + return jsxElementType || anyType; } /** - * Returns the this argument in calls like x.f(...) and x[f](...). Undefined otherwise. + * Returns true iff the JSX element name would be a valid JS identifier, ignoring restrictions about keywords not being identifiers */ - function getThisArgumentOfCall(node) { - if (node.kind === 174 /* CallExpression */) { - var callee = node.expression; - if (callee.kind === 172 /* PropertyAccessExpression */) { - return callee.expression; - } - else if (callee.kind === 173 /* ElementAccessExpression */) { - return callee.expression; - } - } + function isUnhyphenatedJsxName(name) { + // - is the only character supported in JSX attribute names that isn't valid in JavaScript identifiers + return name.indexOf("-") < 0; } /** - * Returns the effective arguments for an expression that works like a function invocation. - * - * If 'node' is a CallExpression or a NewExpression, then its argument list is returned. - * If 'node' is a TaggedTemplateExpression, a new argument list is constructed from the substitution - * expressions, where the first element of the list is `undefined`. - * If 'node' is a Decorator, the argument list will be `undefined`, and its arguments and types - * will be supplied from calls to `getEffectiveArgumentCount` and `getEffectiveArgumentType`. + * Returns true iff React would emit this tag name as a string rather than an identifier or qualified name */ - function getEffectiveCallArguments(node) { - var args; - if (node.kind === 176 /* TaggedTemplateExpression */) { - var template = node.template; - args = [undefined]; - if (template.kind === 189 /* TemplateExpression */) { - ts.forEach(template.templateSpans, function (span) { - args.push(span.expression); - }); - } - } - else if (node.kind === 143 /* Decorator */) { - // For a decorator, we return undefined as we will determine - // the number and types of arguments for a decorator using - // `getEffectiveArgumentCount` and `getEffectiveArgumentType` below. - return undefined; + function isJsxIntrinsicIdentifier(tagName) { + // TODO (yuisu): comment + if (tagName.kind === 172 /* PropertyAccessExpression */ || tagName.kind === 97 /* ThisKeyword */) { + return false; } else { - args = node.arguments || emptyArray; + return ts.isIntrinsicJsxName(tagName.text); } - return args; } - /** - * Returns the effective argument count for a node that works like a function invocation. - * If 'node' is a Decorator, the number of arguments is derived from the decoration - * target and the signature: - * If 'node.target' is a class declaration or class expression, the effective argument - * count is 1. - * If 'node.target' is a parameter declaration, the effective argument count is 3. - * If 'node.target' is a property declaration, the effective argument count is 2. - * If 'node.target' is a method or accessor declaration, the effective argument count - * is 3, although it can be 2 if the signature only accepts two arguments, allowing - * us to match a property decorator. - * Otherwise, the argument count is the length of the 'args' array. - */ - function getEffectiveArgumentCount(node, args, signature) { - if (node.kind === 143 /* Decorator */) { - switch (node.parent.kind) { - case 221 /* ClassDeclaration */: - case 192 /* ClassExpression */: - // A class decorator will have one argument (see `ClassDecorator` in core.d.ts) - return 1; - case 145 /* PropertyDeclaration */: - // A property declaration decorator will have two arguments (see - // `PropertyDecorator` in core.d.ts) - return 2; - case 147 /* MethodDeclaration */: - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - // A method or accessor declaration decorator will have two or three arguments (see - // `PropertyDecorator` and `MethodDecorator` in core.d.ts) - // If we are emitting decorators for ES3, we will only pass two arguments. - if (languageVersion === 0 /* ES3 */) { - return 2; + function checkJsxAttribute(node, elementAttributesType, nameTable) { + var correspondingPropType = undefined; + // Look up the corresponding property for this attribute + if (elementAttributesType === emptyObjectType && isUnhyphenatedJsxName(node.name.text)) { + // If there is no 'props' property, you may not have non-"data-" attributes + error(node.parent, ts.Diagnostics.JSX_element_class_does_not_support_attributes_because_it_does_not_have_a_0_property, getJsxElementPropertiesName()); + } + else if (elementAttributesType && !isTypeAny(elementAttributesType)) { + var correspondingPropSymbol = getPropertyOfType(elementAttributesType, node.name.text); + correspondingPropType = correspondingPropSymbol && getTypeOfSymbol(correspondingPropSymbol); + if (isUnhyphenatedJsxName(node.name.text)) { + var attributeType = getTypeOfPropertyOfType(elementAttributesType, getTextOfPropertyName(node.name)) || getIndexTypeOfType(elementAttributesType, 0 /* String */); + if (attributeType) { + correspondingPropType = attributeType; + } + else { + // If there's no corresponding property with this name, error + if (!correspondingPropType) { + error(node.name, ts.Diagnostics.Property_0_does_not_exist_on_type_1, node.name.text, typeToString(elementAttributesType)); + return unknownType; } - // If the method decorator signature only accepts a target and a key, we will only - // type check those arguments. - return signature.parameters.length >= 3 ? 3 : 2; - case 142 /* Parameter */: - // A parameter declaration decorator will have three arguments (see - // `ParameterDecorator` in core.d.ts) - return 3; + } } } + var exprType; + if (node.initializer) { + exprType = checkExpression(node.initializer); + } else { - return args.length; + // is sugar for + exprType = booleanType; } - } - /** - * Returns the effective type of the first argument to a decorator. - * If 'node' is a class declaration or class expression, the effective argument type - * is the type of the static side of the class. - * If 'node' is a parameter declaration, the effective argument type is either the type - * of the static or instance side of the class for the parameter's parent method, - * depending on whether the method is declared static. - * For a constructor, the type is always the type of the static side of the class. - * If 'node' is a property, method, or accessor declaration, the effective argument - * type is the type of the static or instance side of the parent class for class - * element, depending on whether the element is declared static. - */ - function getEffectiveDecoratorFirstArgumentType(node) { - // The first argument to a decorator is its `target`. - if (node.kind === 221 /* ClassDeclaration */) { - // For a class decorator, the `target` is the type of the class (e.g. the - // "static" or "constructor" side of the class) - var classSymbol = getSymbolOfNode(node); - return getTypeOfSymbol(classSymbol); + if (correspondingPropType) { + checkTypeAssignableTo(exprType, correspondingPropType, node); } - if (node.kind === 142 /* Parameter */) { - // For a parameter decorator, the `target` is the parent type of the - // parameter's containing method. - node = node.parent; - if (node.kind === 148 /* Constructor */) { - var classSymbol = getSymbolOfNode(node); - return getTypeOfSymbol(classSymbol); + nameTable[node.name.text] = true; + return exprType; + } + function checkJsxSpreadAttribute(node, elementAttributesType, nameTable) { + var type = checkExpression(node.expression); + var props = getPropertiesOfType(type); + for (var _i = 0, props_2 = props; _i < props_2.length; _i++) { + var prop = props_2[_i]; + // Is there a corresponding property in the element attributes type? Skip checking of properties + // that have already been assigned to, as these are not actually pushed into the resulting type + if (!nameTable[prop.name]) { + var targetPropSym = getPropertyOfType(elementAttributesType, prop.name); + if (targetPropSym) { + var msg = ts.chainDiagnosticMessages(undefined, ts.Diagnostics.Property_0_of_JSX_spread_attribute_is_not_assignable_to_target_property, prop.name); + checkTypeAssignableTo(getTypeOfSymbol(prop), getTypeOfSymbol(targetPropSym), node, undefined, msg); + } + nameTable[prop.name] = true; } } - if (node.kind === 145 /* PropertyDeclaration */ || - node.kind === 147 /* MethodDeclaration */ || - node.kind === 149 /* GetAccessor */ || - node.kind === 150 /* SetAccessor */) { - // For a property or method decorator, the `target` is the - // "static"-side type of the parent of the member if the member is - // declared "static"; otherwise, it is the "instance"-side type of the - // parent of the member. - return getParentTypeOfClassElement(node); + return type; + } + function getJsxType(name) { + if (jsxTypes[name] === undefined) { + return jsxTypes[name] = getExportedTypeFromNamespace(JsxNames.JSX, name) || unknownType; } - ts.Debug.fail("Unsupported decorator target."); - return unknownType; + return jsxTypes[name]; } /** - * Returns the effective type for the second argument to a decorator. - * If 'node' is a parameter, its effective argument type is one of the following: - * If 'node.parent' is a constructor, the effective argument type is 'any', as we - * will emit `undefined`. - * If 'node.parent' is a member with an identifier, numeric, or string literal name, - * the effective argument type will be a string literal type for the member name. - * If 'node.parent' is a computed property name, the effective argument type will - * either be a symbol type or the string type. - * If 'node' is a member with an identifier, numeric, or string literal name, the - * effective argument type will be a string literal type for the member name. - * If 'node' is a computed property name, the effective argument type will either - * be a symbol type or the string type. - * A class decorator does not have a second argument type. + * Looks up an intrinsic tag name and returns a symbol that either points to an intrinsic + * property (in which case nodeLinks.jsxFlags will be IntrinsicNamedElement) or an intrinsic + * string index signature (in which case nodeLinks.jsxFlags will be IntrinsicIndexedElement). + * May also return unknownSymbol if both of these lookups fail. */ - function getEffectiveDecoratorSecondArgumentType(node) { - // The second argument to a decorator is its `propertyKey` - if (node.kind === 221 /* ClassDeclaration */) { - ts.Debug.fail("Class decorators should not have a second synthetic argument."); - return unknownType; - } - if (node.kind === 142 /* Parameter */) { - node = node.parent; - if (node.kind === 148 /* Constructor */) { - // For a constructor parameter decorator, the `propertyKey` will be `undefined`. - return anyType; + function getIntrinsicTagSymbol(node) { + var links = getNodeLinks(node); + if (!links.resolvedSymbol) { + var intrinsicElementsType = getJsxType(JsxNames.IntrinsicElements); + if (intrinsicElementsType !== unknownType) { + // Property case + var intrinsicProp = getPropertyOfType(intrinsicElementsType, node.tagName.text); + if (intrinsicProp) { + links.jsxFlags |= 1 /* IntrinsicNamedElement */; + return links.resolvedSymbol = intrinsicProp; + } + // Intrinsic string indexer case + var indexSignatureType = getIndexTypeOfType(intrinsicElementsType, 0 /* String */); + if (indexSignatureType) { + links.jsxFlags |= 2 /* IntrinsicIndexedElement */; + return links.resolvedSymbol = intrinsicElementsType.symbol; + } + // Wasn't found + error(node, ts.Diagnostics.Property_0_does_not_exist_on_type_1, node.tagName.text, "JSX." + JsxNames.IntrinsicElements); + return links.resolvedSymbol = unknownSymbol; } - } - if (node.kind === 145 /* PropertyDeclaration */ || - node.kind === 147 /* MethodDeclaration */ || - node.kind === 149 /* GetAccessor */ || - node.kind === 150 /* SetAccessor */) { - // The `propertyKey` for a property or method decorator will be a - // string literal type if the member name is an identifier, number, or string; - // otherwise, if the member name is a computed property name it will - // be either string or symbol. - var element = node; - switch (element.name.kind) { - case 69 /* Identifier */: - case 8 /* NumericLiteral */: - case 9 /* StringLiteral */: - return getLiteralTypeForText(32 /* StringLiteral */, element.name.text); - case 140 /* ComputedPropertyName */: - var nameType = checkComputedPropertyName(element.name); - if (isTypeOfKind(nameType, 512 /* ESSymbol */)) { - return nameType; - } - else { - return stringType; - } - default: - ts.Debug.fail("Unsupported property name."); - return unknownType; + else { + if (compilerOptions.noImplicitAny) { + error(node, ts.Diagnostics.JSX_element_implicitly_has_type_any_because_no_interface_JSX_0_exists, JsxNames.IntrinsicElements); + } + return links.resolvedSymbol = unknownSymbol; } } - ts.Debug.fail("Unsupported decorator target."); - return unknownType; + return links.resolvedSymbol; } /** - * Returns the effective argument type for the third argument to a decorator. - * If 'node' is a parameter, the effective argument type is the number type. - * If 'node' is a method or accessor, the effective argument type is a - * `TypedPropertyDescriptor` instantiated with the type of the member. - * Class and property decorators do not have a third effective argument. - */ - function getEffectiveDecoratorThirdArgumentType(node) { - // The third argument to a decorator is either its `descriptor` for a method decorator - // or its `parameterIndex` for a parameter decorator - if (node.kind === 221 /* ClassDeclaration */) { - ts.Debug.fail("Class decorators should not have a third synthetic argument."); - return unknownType; - } - if (node.kind === 142 /* Parameter */) { - // The `parameterIndex` for a parameter decorator is always a number - return numberType; - } - if (node.kind === 145 /* PropertyDeclaration */) { - ts.Debug.fail("Property decorators should not have a third synthetic argument."); - return unknownType; + * Given a JSX element that is a class element, finds the Element Instance Type. If the + * element is not a class element, or the class element type cannot be determined, returns 'undefined'. + * For example, in the element , the element instance type is `MyClass` (not `typeof MyClass`). + */ + function getJsxElementInstanceType(node, valueType) { + ts.Debug.assert(!(valueType.flags & 524288 /* Union */)); + if (isTypeAny(valueType)) { + // Short-circuit if the class tag is using an element type 'any' + return anyType; } - if (node.kind === 147 /* MethodDeclaration */ || - node.kind === 149 /* GetAccessor */ || - node.kind === 150 /* SetAccessor */) { - // The `descriptor` for a method decorator will be a `TypedPropertyDescriptor` - // for the type of the member. - var propertyType = getTypeOfNode(node); - return createTypedPropertyDescriptorType(propertyType); - } - ts.Debug.fail("Unsupported decorator target."); - return unknownType; - } - /** - * Returns the effective argument type for the provided argument to a decorator. - */ - function getEffectiveDecoratorArgumentType(node, argIndex) { - if (argIndex === 0) { - return getEffectiveDecoratorFirstArgumentType(node.parent); - } - else if (argIndex === 1) { - return getEffectiveDecoratorSecondArgumentType(node.parent); - } - else if (argIndex === 2) { - return getEffectiveDecoratorThirdArgumentType(node.parent); + // Resolve the signatures, preferring constructor + var signatures = getSignaturesOfType(valueType, 1 /* Construct */); + if (signatures.length === 0) { + // No construct signatures, try call signatures + signatures = getSignaturesOfType(valueType, 0 /* Call */); + if (signatures.length === 0) { + // We found no signatures at all, which is an error + error(node.tagName, ts.Diagnostics.JSX_element_type_0_does_not_have_any_construct_or_call_signatures, ts.getTextOfNode(node.tagName)); + return unknownType; + } } - ts.Debug.fail("Decorators should not have a fourth synthetic argument."); - return unknownType; + return getUnionType(signatures.map(getReturnTypeOfSignature), /*subtypeReduction*/ true); } - /** - * Gets the effective argument type for an argument in a call expression. - */ - function getEffectiveArgumentType(node, argIndex, arg) { - // Decorators provide special arguments, a tagged template expression provides - // a special first argument, and string literals get string literal types - // unless we're reporting errors - if (node.kind === 143 /* Decorator */) { - return getEffectiveDecoratorArgumentType(node, argIndex); - } - else if (argIndex === 0 && node.kind === 176 /* TaggedTemplateExpression */) { - return getGlobalTemplateStringsArrayType(); + /// e.g. "props" for React.d.ts, + /// or 'undefined' if ElementAttributesProperty doesn't exist (which means all + /// non-intrinsic elements' attributes type is 'any'), + /// or '' if it has 0 properties (which means every + /// non-intrinsic elements' attributes type is the element instance type) + function getJsxElementPropertiesName() { + // JSX + var jsxNamespace = getGlobalSymbol(JsxNames.JSX, 1920 /* Namespace */, /*diagnosticMessage*/ undefined); + // JSX.ElementAttributesProperty [symbol] + var attribsPropTypeSym = jsxNamespace && getSymbol(jsxNamespace.exports, JsxNames.ElementAttributesPropertyNameContainer, 793064 /* Type */); + // JSX.ElementAttributesProperty [type] + var attribPropType = attribsPropTypeSym && getDeclaredTypeOfSymbol(attribsPropTypeSym); + // The properties of JSX.ElementAttributesProperty + var attribProperties = attribPropType && getPropertiesOfType(attribPropType); + if (attribProperties) { + // Element Attributes has zero properties, so the element attributes type will be the class instance type + if (attribProperties.length === 0) { + return ""; + } + else if (attribProperties.length === 1) { + return attribProperties[0].name; + } + else { + error(attribsPropTypeSym.declarations[0], ts.Diagnostics.The_global_type_JSX_0_may_not_have_more_than_one_property, JsxNames.ElementAttributesPropertyNameContainer); + return undefined; + } } - // This is not a synthetic argument, so we return 'undefined' - // to signal that the caller needs to check the argument. - return undefined; - } - /** - * Gets the effective argument expression for an argument in a call expression. - */ - function getEffectiveArgument(node, args, argIndex) { - // For a decorator or the first argument of a tagged template expression we return undefined. - if (node.kind === 143 /* Decorator */ || - (argIndex === 0 && node.kind === 176 /* TaggedTemplateExpression */)) { + else { + // No interface exists, so the element attributes type will be an implicit any return undefined; } - return args[argIndex]; } /** - * Gets the error node to use when reporting errors for an effective argument. - */ - function getEffectiveArgumentErrorNode(node, argIndex, arg) { - if (node.kind === 143 /* Decorator */) { - // For a decorator, we use the expression of the decorator for error reporting. - return node.expression; + * Given React element instance type and the class type, resolve the Jsx type + * Pass elemType to handle individual type in the union typed element type. + */ + function getResolvedJsxType(node, elemType, elemClassType) { + if (!elemType) { + elemType = checkExpression(node.tagName); } - else if (argIndex === 0 && node.kind === 176 /* TaggedTemplateExpression */) { - // For a the first argument of a tagged template expression, we use the template of the tag for error reporting. - return node.template; + if (elemType.flags & 524288 /* Union */) { + var types = elemType.types; + return getUnionType(types.map(function (type) { + return getResolvedJsxType(node, type, elemClassType); + }), /*subtypeReduction*/ true); } - else { - return arg; + // If the elemType is a string type, we have to return anyType to prevent an error downstream as we will try to find construct or call signature of the type + if (elemType.flags & 2 /* String */) { + return anyType; } - } - function resolveCall(node, signatures, candidatesOutArray, headMessage) { - var isTaggedTemplate = node.kind === 176 /* TaggedTemplateExpression */; - var isDecorator = node.kind === 143 /* Decorator */; - var typeArguments; - if (!isTaggedTemplate && !isDecorator) { - typeArguments = node.typeArguments; - // We already perform checking on the type arguments on the class declaration itself. - if (node.expression.kind !== 95 /* SuperKeyword */) { - ts.forEach(typeArguments, checkSourceElement); + else if (elemType.flags & 32 /* StringLiteral */) { + // If the elemType is a stringLiteral type, we can then provide a check to make sure that the string literal type is one of the Jsx intrinsic element type + var intrinsicElementsType = getJsxType(JsxNames.IntrinsicElements); + if (intrinsicElementsType !== unknownType) { + var stringLiteralTypeName = elemType.text; + var intrinsicProp = getPropertyOfType(intrinsicElementsType, stringLiteralTypeName); + if (intrinsicProp) { + return getTypeOfSymbol(intrinsicProp); + } + var indexSignatureType = getIndexTypeOfType(intrinsicElementsType, 0 /* String */); + if (indexSignatureType) { + return indexSignatureType; + } + error(node, ts.Diagnostics.Property_0_does_not_exist_on_type_1, stringLiteralTypeName, "JSX." + JsxNames.IntrinsicElements); } + // If we need to report an error, we already done so here. So just return any to prevent any more error downstream + return anyType; } - var candidates = candidatesOutArray || []; - // reorderCandidates fills up the candidates array directly - reorderCandidates(signatures, candidates); - if (!candidates.length) { - reportError(ts.Diagnostics.Supplied_parameters_do_not_match_any_signature_of_call_target); - return resolveErrorCall(node); - } - var args = getEffectiveCallArguments(node); - // The following applies to any value of 'excludeArgument[i]': - // - true: the argument at 'i' is susceptible to a one-time permanent contextual typing. - // - undefined: the argument at 'i' is *not* susceptible to permanent contextual typing. - // - false: the argument at 'i' *was* and *has been* permanently contextually typed. - // - // The idea is that we will perform type argument inference & assignability checking once - // without using the susceptible parameters that are functions, and once more for each of those - // parameters, contextually typing each as we go along. - // - // For a tagged template, then the first argument be 'undefined' if necessary - // because it represents a TemplateStringsArray. - // - // For a decorator, no arguments are susceptible to contextual typing due to the fact - // decorators are applied to a declaration by the emitter, and not to an expression. - var excludeArgument; - if (!isDecorator) { - // We do not need to call `getEffectiveArgumentCount` here as it only - // applies when calculating the number of arguments for a decorator. - for (var i = isTaggedTemplate ? 1 : 0; i < args.length; i++) { - if (isContextSensitive(args[i])) { - if (!excludeArgument) { - excludeArgument = new Array(args.length); + // Get the element instance type (the result of newing or invoking this tag) + var elemInstanceType = getJsxElementInstanceType(node, elemType); + if (!elemClassType || !isTypeAssignableTo(elemInstanceType, elemClassType)) { + // Is this is a stateless function component? See if its single signature's return type is + // assignable to the JSX Element Type + if (jsxElementType) { + var callSignatures = elemType && getSignaturesOfType(elemType, 0 /* Call */); + var callSignature = callSignatures && callSignatures.length > 0 && callSignatures[0]; + var callReturnType = callSignature && getReturnTypeOfSignature(callSignature); + var paramType = callReturnType && (callSignature.parameters.length === 0 ? emptyObjectType : getTypeOfSymbol(callSignature.parameters[0])); + if (callReturnType && isTypeAssignableTo(callReturnType, jsxElementType)) { + // Intersect in JSX.IntrinsicAttributes if it exists + var intrinsicAttributes = getJsxType(JsxNames.IntrinsicAttributes); + if (intrinsicAttributes !== unknownType) { + paramType = intersectTypes(intrinsicAttributes, paramType); } - excludeArgument[i] = true; + return paramType; } } } - // The following variables are captured and modified by calls to chooseOverload. - // If overload resolution or type argument inference fails, we want to report the - // best error possible. The best error is one which says that an argument was not - // assignable to a parameter. This implies that everything else about the overload - // was fine. So if there is any overload that is only incorrect because of an - // argument, we will report an error on that one. - // - // function foo(s: string) {} - // function foo(n: number) {} // Report argument error on this overload - // function foo() {} - // foo(true); - // - // If none of the overloads even made it that far, there are two possibilities. - // There was a problem with type arguments for some overload, in which case - // report an error on that. Or none of the overloads even had correct arity, - // in which case give an arity error. - // - // function foo(x: T, y: T) {} // Report type argument inference error - // function foo() {} - // foo(0, true); - // - var candidateForArgumentError; - var candidateForTypeArgumentError; - var resultOfFailedInference; - var result; - // If we are in signature help, a trailing comma indicates that we intend to provide another argument, - // so we will only accept overloads with arity at least 1 higher than the current number of provided arguments. - var signatureHelpTrailingComma = candidatesOutArray && node.kind === 174 /* CallExpression */ && node.arguments.hasTrailingComma; - // Section 4.12.1: - // if the candidate list contains one or more signatures for which the type of each argument - // expression is a subtype of each corresponding parameter type, the return type of the first - // of those signatures becomes the return type of the function call. - // Otherwise, the return type of the first signature in the candidate list becomes the return - // type of the function call. - // - // Whether the call is an error is determined by assignability of the arguments. The subtype pass - // is just important for choosing the best signature. So in the case where there is only one - // signature, the subtype pass is useless. So skipping it is an optimization. - if (candidates.length > 1) { - result = chooseOverload(candidates, subtypeRelation, signatureHelpTrailingComma); - } - if (!result) { - // Reinitialize these pointers for round two - candidateForArgumentError = undefined; - candidateForTypeArgumentError = undefined; - resultOfFailedInference = undefined; - result = chooseOverload(candidates, assignableRelation, signatureHelpTrailingComma); + // Issue an error if this return type isn't assignable to JSX.ElementClass + if (elemClassType) { + checkTypeRelatedTo(elemInstanceType, elemClassType, assignableRelation, node, ts.Diagnostics.JSX_element_type_0_is_not_a_constructor_function_for_JSX_elements); } - if (result) { - return result; + if (isTypeAny(elemInstanceType)) { + return elemInstanceType; } - // No signatures were applicable. Now report errors based on the last applicable signature with - // no arguments excluded from assignability checks. - // If candidate is undefined, it means that no candidates had a suitable arity. In that case, - // skip the checkApplicableSignature check. - if (candidateForArgumentError) { - // excludeArgument is undefined, in this case also equivalent to [undefined, undefined, ...] - // The importance of excludeArgument is to prevent us from typing function expression parameters - // in arguments too early. If possible, we'd like to only type them once we know the correct - // overload. However, this matters for the case where the call is correct. When the call is - // an error, we don't need to exclude any arguments, although it would cause no harm to do so. - checkApplicableSignature(node, args, candidateForArgumentError, assignableRelation, /*excludeArgument*/ undefined, /*reportErrors*/ true); + var propsName = getJsxElementPropertiesName(); + if (propsName === undefined) { + // There is no type ElementAttributesProperty, return 'any' + return anyType; } - else if (candidateForTypeArgumentError) { - if (!isTaggedTemplate && !isDecorator && typeArguments) { - var typeArguments_2 = node.typeArguments; - checkTypeArguments(candidateForTypeArgumentError, typeArguments_2, ts.map(typeArguments_2, getTypeFromTypeNodeNoAlias), /*reportErrors*/ true, headMessage); - } - else { - ts.Debug.assert(resultOfFailedInference.failedTypeParameterIndex >= 0); - var failedTypeParameter = candidateForTypeArgumentError.typeParameters[resultOfFailedInference.failedTypeParameterIndex]; - var inferenceCandidates = getInferenceCandidates(resultOfFailedInference, resultOfFailedInference.failedTypeParameterIndex); - var diagnosticChainHead = ts.chainDiagnosticMessages(/*details*/ undefined, // details will be provided by call to reportNoCommonSupertypeError - ts.Diagnostics.The_type_argument_for_type_parameter_0_cannot_be_inferred_from_the_usage_Consider_specifying_the_type_arguments_explicitly, typeToString(failedTypeParameter)); - if (headMessage) { - diagnosticChainHead = ts.chainDiagnosticMessages(diagnosticChainHead, headMessage); - } - reportNoCommonSupertypeError(inferenceCandidates, node.expression || node.tag, diagnosticChainHead); - } + else if (propsName === "") { + // If there is no e.g. 'props' member in ElementAttributesProperty, use the element class type instead + return elemInstanceType; } else { - reportError(ts.Diagnostics.Supplied_parameters_do_not_match_any_signature_of_call_target); - } - // No signature was applicable. We have already reported the errors for the invalid signature. - // If this is a type resolution session, e.g. Language Service, try to get better information that anySignature. - // Pick the first candidate that matches the arity. This way we can get a contextual type for cases like: - // declare function f(a: { xa: number; xb: number; }); - // f({ | - if (!produceDiagnostics) { - for (var _i = 0, candidates_1 = candidates; _i < candidates_1.length; _i++) { - var candidate = candidates_1[_i]; - if (hasCorrectArity(node, args, candidate)) { - if (candidate.typeParameters && typeArguments) { - candidate = getSignatureInstantiation(candidate, ts.map(typeArguments, getTypeFromTypeNodeNoAlias)); - } - return candidate; - } + var attributesType = getTypeOfPropertyOfType(elemInstanceType, propsName); + if (!attributesType) { + // There is no property named 'props' on this instance type + return emptyObjectType; } - } - return resolveErrorCall(node); - function reportError(message, arg0, arg1, arg2) { - var errorInfo; - errorInfo = ts.chainDiagnosticMessages(errorInfo, message, arg0, arg1, arg2); - if (headMessage) { - errorInfo = ts.chainDiagnosticMessages(errorInfo, headMessage); + else if (isTypeAny(attributesType) || (attributesType === unknownType)) { + // Props is of type 'any' or unknown + return attributesType; } - diagnostics.add(ts.createDiagnosticForNodeFromMessageChain(node, errorInfo)); - } - function chooseOverload(candidates, relation, signatureHelpTrailingComma) { - if (signatureHelpTrailingComma === void 0) { signatureHelpTrailingComma = false; } - for (var _i = 0, candidates_2 = candidates; _i < candidates_2.length; _i++) { - var originalCandidate = candidates_2[_i]; - if (!hasCorrectArity(node, args, originalCandidate, signatureHelpTrailingComma)) { - continue; - } - var candidate = void 0; - var typeArgumentsAreValid = void 0; - var inferenceContext = originalCandidate.typeParameters - ? createInferenceContext(originalCandidate, /*inferUnionTypes*/ false) - : undefined; - while (true) { - candidate = originalCandidate; - if (candidate.typeParameters) { - var typeArgumentTypes = void 0; - if (typeArguments) { - typeArgumentTypes = ts.map(typeArguments, getTypeFromTypeNodeNoAlias); - typeArgumentsAreValid = checkTypeArguments(candidate, typeArguments, typeArgumentTypes, /*reportErrors*/ false); - } - else { - inferTypeArguments(node, candidate, args, excludeArgument, inferenceContext); - typeArgumentsAreValid = inferenceContext.failedTypeParameterIndex === undefined; - typeArgumentTypes = inferenceContext.inferredTypes; - } - if (!typeArgumentsAreValid) { - break; + else if (attributesType.flags & 524288 /* Union */) { + // Props cannot be a union type + error(node.tagName, ts.Diagnostics.JSX_element_attributes_type_0_may_not_be_a_union_type, typeToString(attributesType)); + return anyType; + } + else { + // Normal case -- add in IntrinsicClassElements and IntrinsicElements + var apparentAttributesType = attributesType; + var intrinsicClassAttribs = getJsxType(JsxNames.IntrinsicClassAttributes); + if (intrinsicClassAttribs !== unknownType) { + var typeParams = getLocalTypeParametersOfClassOrInterfaceOrTypeAlias(intrinsicClassAttribs.symbol); + if (typeParams) { + if (typeParams.length === 1) { + apparentAttributesType = intersectTypes(createTypeReference(intrinsicClassAttribs, [elemInstanceType]), apparentAttributesType); } - candidate = getSignatureInstantiation(candidate, typeArgumentTypes); - } - if (!checkApplicableSignature(node, args, candidate, relation, excludeArgument, /*reportErrors*/ false)) { - break; - } - var index = excludeArgument ? ts.indexOf(excludeArgument, true) : -1; - if (index < 0) { - return candidate; - } - excludeArgument[index] = false; - } - // A post-mortem of this iteration of the loop. The signature was not applicable, - // so we want to track it as a candidate for reporting an error. If the candidate - // had no type parameters, or had no issues related to type arguments, we can - // report an error based on the arguments. If there was an issue with type - // arguments, then we can only report an error based on the type arguments. - if (originalCandidate.typeParameters) { - var instantiatedCandidate = candidate; - if (typeArgumentsAreValid) { - candidateForArgumentError = instantiatedCandidate; } else { - candidateForTypeArgumentError = originalCandidate; - if (!typeArguments) { - resultOfFailedInference = inferenceContext; - } + apparentAttributesType = intersectTypes(attributesType, intrinsicClassAttribs); } } - else { - ts.Debug.assert(originalCandidate === candidate); - candidateForArgumentError = originalCandidate; + var intrinsicAttribs = getJsxType(JsxNames.IntrinsicAttributes); + if (intrinsicAttribs !== unknownType) { + apparentAttributesType = intersectTypes(intrinsicAttribs, apparentAttributesType); } + return apparentAttributesType; } - return undefined; } } - function resolveCallExpression(node, candidatesOutArray) { - if (node.expression.kind === 95 /* SuperKeyword */) { - var superType = checkSuperExpression(node.expression); - if (superType !== unknownType) { - // In super call, the candidate signatures are the matching arity signatures of the base constructor function instantiated - // with the type arguments specified in the extends clause. - var baseTypeNode = ts.getClassExtendsHeritageClauseElement(ts.getContainingClass(node)); - if (baseTypeNode) { - var baseConstructors = getInstantiatedConstructorsForTypeArguments(superType, baseTypeNode.typeArguments); - return resolveCall(node, baseConstructors, candidatesOutArray); + /** + * Given an opening/self-closing element, get the 'element attributes type', i.e. the type that tells + * us which attributes are valid on a given element. + */ + function getJsxElementAttributesType(node) { + var links = getNodeLinks(node); + if (!links.resolvedJsxType) { + if (isJsxIntrinsicIdentifier(node.tagName)) { + var symbol = getIntrinsicTagSymbol(node); + if (links.jsxFlags & 1 /* IntrinsicNamedElement */) { + return links.resolvedJsxType = getTypeOfSymbol(symbol); + } + else if (links.jsxFlags & 2 /* IntrinsicIndexedElement */) { + return links.resolvedJsxType = getIndexInfoOfSymbol(symbol, 0 /* String */).type; + } + else { + return links.resolvedJsxType = unknownType; } - } - return resolveUntypedCall(node); - } - var funcType = checkNonNullExpression(node.expression); - if (funcType === silentNeverType) { - return silentNeverSignature; - } - var apparentType = getApparentType(funcType); - if (apparentType === unknownType) { - // Another error has already been reported - return resolveErrorCall(node); - } - // Technically, this signatures list may be incomplete. We are taking the apparent type, - // but we are not including call signatures that may have been added to the Object or - // Function interface, since they have none by default. This is a bit of a leap of faith - // that the user will not add any. - var callSignatures = getSignaturesOfType(apparentType, 0 /* Call */); - var constructSignatures = getSignaturesOfType(apparentType, 1 /* Construct */); - // TS 1.0 Spec: 4.12 - // In an untyped function call no TypeArgs are permitted, Args can be any argument list, no contextual - // types are provided for the argument expressions, and the result is always of type Any. - if (isUntypedFunctionCall(funcType, apparentType, callSignatures.length, constructSignatures.length)) { - // The unknownType indicates that an error already occurred (and was reported). No - // need to report another error in this case. - if (funcType !== unknownType && node.typeArguments) { - error(node, ts.Diagnostics.Untyped_function_calls_may_not_accept_type_arguments); - } - return resolveUntypedCall(node); - } - // If FuncExpr's apparent type(section 3.8.1) is a function type, the call is a typed function call. - // TypeScript employs overload resolution in typed function calls in order to support functions - // with multiple call signatures. - if (!callSignatures.length) { - if (constructSignatures.length) { - error(node, ts.Diagnostics.Value_of_type_0_is_not_callable_Did_you_mean_to_include_new, typeToString(funcType)); } else { - error(node, ts.Diagnostics.Cannot_invoke_an_expression_whose_type_lacks_a_call_signature_Type_0_has_no_compatible_call_signatures, typeToString(apparentType)); + var elemClassType = getJsxGlobalElementClassType(); + return links.resolvedJsxType = getResolvedJsxType(node, undefined, elemClassType); } - return resolveErrorCall(node); } - return resolveCall(node, callSignatures, candidatesOutArray); + return links.resolvedJsxType; } /** - * TS 1.0 spec: 4.12 - * If FuncExpr is of type Any, or of an object type that has no call or construct signatures - * but is a subtype of the Function interface, the call is an untyped function call. + * Given a JSX attribute, returns the symbol for the corresponds property + * of the element attributes type. Will return unknownSymbol for attributes + * that have no matching element attributes type property. */ - function isUntypedFunctionCall(funcType, apparentFuncType, numCallSignatures, numConstructSignatures) { - if (isTypeAny(funcType)) { - return true; + function getJsxAttributePropertySymbol(attrib) { + var attributesType = getJsxElementAttributesType(attrib.parent); + var prop = getPropertyOfType(attributesType, attrib.name.text); + return prop || unknownSymbol; + } + function getJsxGlobalElementClassType() { + if (!jsxElementClassType) { + jsxElementClassType = getExportedTypeFromNamespace(JsxNames.JSX, JsxNames.ElementClass); } - if (isTypeAny(apparentFuncType) && funcType.flags & 16384 /* TypeParameter */) { - return true; + return jsxElementClassType; + } + /// Returns all the properties of the Jsx.IntrinsicElements interface + function getJsxIntrinsicTagNames() { + var intrinsics = getJsxType(JsxNames.IntrinsicElements); + return intrinsics ? getPropertiesOfType(intrinsics) : emptyArray; + } + function checkJsxPreconditions(errorNode) { + // Preconditions for using JSX + if ((compilerOptions.jsx || 0 /* None */) === 0 /* None */) { + error(errorNode, ts.Diagnostics.Cannot_use_JSX_unless_the_jsx_flag_is_provided); } - if (!numCallSignatures && !numConstructSignatures) { - // We exclude union types because we may have a union of function types that happen to have - // no common signatures. - if (funcType.flags & 524288 /* Union */) { - return false; + if (jsxElementType === undefined) { + if (compilerOptions.noImplicitAny) { + error(errorNode, ts.Diagnostics.JSX_element_implicitly_has_type_any_because_the_global_type_JSX_Element_does_not_exist); } - return isTypeAssignableTo(funcType, globalFunctionType); } - return false; } - function resolveNewExpression(node, candidatesOutArray) { - if (node.arguments && languageVersion < 1 /* ES5 */) { - var spreadIndex = getSpreadArgumentIndex(node.arguments); - if (spreadIndex >= 0) { - error(node.arguments[spreadIndex], ts.Diagnostics.Spread_operator_in_new_expressions_is_only_available_when_targeting_ECMAScript_5_and_higher); - } - } - var expressionType = checkNonNullExpression(node.expression); - if (expressionType === silentNeverType) { - return silentNeverSignature; + function checkJsxOpeningLikeElement(node) { + checkGrammarJsxElement(node); + checkJsxPreconditions(node); + // The reactNamespace symbol should be marked as 'used' so we don't incorrectly elide its import. And if there + // is no reactNamespace symbol in scope when targeting React emit, we should issue an error. + var reactRefErr = compilerOptions.jsx === 2 /* React */ ? ts.Diagnostics.Cannot_find_name_0 : undefined; + var reactNamespace = compilerOptions.reactNamespace ? compilerOptions.reactNamespace : "React"; + var reactSym = resolveName(node.tagName, reactNamespace, 107455 /* Value */, reactRefErr, reactNamespace); + if (reactSym) { + getSymbolLinks(reactSym).referenced = true; } - // If expressionType's apparent type(section 3.8.1) is an object type with one or - // more construct signatures, the expression is processed in the same manner as a - // function call, but using the construct signatures as the initial set of candidate - // signatures for overload resolution. The result type of the function call becomes - // the result type of the operation. - expressionType = getApparentType(expressionType); - if (expressionType === unknownType) { - // Another error has already been reported - return resolveErrorCall(node); - } - // If the expression is a class of abstract type, then it cannot be instantiated. - // Note, only class declarations can be declared abstract. - // In the case of a merged class-module or class-interface declaration, - // only the class declaration node will have the Abstract flag set. - var valueDecl = expressionType.symbol && getClassLikeDeclarationOfSymbol(expressionType.symbol); - if (valueDecl && ts.getModifierFlags(valueDecl) & 128 /* Abstract */) { - error(node, ts.Diagnostics.Cannot_create_an_instance_of_the_abstract_class_0, ts.declarationNameToString(valueDecl.name)); - return resolveErrorCall(node); - } - // TS 1.0 spec: 4.11 - // If expressionType is of type Any, Args can be any argument - // list and the result of the operation is of type Any. - if (isTypeAny(expressionType)) { - if (node.typeArguments) { - error(node, ts.Diagnostics.Untyped_function_calls_may_not_accept_type_arguments); + var targetAttributesType = getJsxElementAttributesType(node); + var nameTable = ts.createMap(); + // Process this array in right-to-left order so we know which + // attributes (mostly from spreads) are being overwritten and + // thus should have their types ignored + var sawSpreadedAny = false; + for (var i = node.attributes.length - 1; i >= 0; i--) { + if (node.attributes[i].kind === 246 /* JsxAttribute */) { + checkJsxAttribute((node.attributes[i]), targetAttributesType, nameTable); } - return resolveUntypedCall(node); - } - // Technically, this signatures list may be incomplete. We are taking the apparent type, - // but we are not including construct signatures that may have been added to the Object or - // Function interface, since they have none by default. This is a bit of a leap of faith - // that the user will not add any. - var constructSignatures = getSignaturesOfType(expressionType, 1 /* Construct */); - if (constructSignatures.length) { - if (!isConstructorAccessible(node, constructSignatures[0])) { - return resolveErrorCall(node); + else { + ts.Debug.assert(node.attributes[i].kind === 247 /* JsxSpreadAttribute */); + var spreadType = checkJsxSpreadAttribute((node.attributes[i]), targetAttributesType, nameTable); + if (isTypeAny(spreadType)) { + sawSpreadedAny = true; + } } - return resolveCall(node, constructSignatures, candidatesOutArray); } - // If expressionType's apparent type is an object type with no construct signatures but - // one or more call signatures, the expression is processed as a function call. A compile-time - // error occurs if the result of the function call is not Void. The type of the result of the - // operation is Any. It is an error to have a Void this type. - var callSignatures = getSignaturesOfType(expressionType, 0 /* Call */); - if (callSignatures.length) { - var signature = resolveCall(node, callSignatures, candidatesOutArray); - if (getReturnTypeOfSignature(signature) !== voidType) { - error(node, ts.Diagnostics.Only_a_void_function_can_be_called_with_the_new_keyword); - } - if (getThisTypeOfSignature(signature) === voidType) { - error(node, ts.Diagnostics.A_function_that_is_called_with_the_new_keyword_cannot_have_a_this_type_that_is_void); + // Check that all required properties have been provided. If an 'any' + // was spreaded in, though, assume that it provided all required properties + if (targetAttributesType && !sawSpreadedAny) { + var targetProperties = getPropertiesOfType(targetAttributesType); + for (var i = 0; i < targetProperties.length; i++) { + if (!(targetProperties[i].flags & 536870912 /* Optional */) && + !nameTable[targetProperties[i].name]) { + error(node, ts.Diagnostics.Property_0_is_missing_in_type_1, targetProperties[i].name, typeToString(targetAttributesType)); + } } - return signature; } - error(node, ts.Diagnostics.Cannot_use_new_with_an_expression_whose_type_lacks_a_call_or_construct_signature); - return resolveErrorCall(node); } - function isConstructorAccessible(node, signature) { - if (!signature || !signature.declaration) { - return true; + function checkJsxExpression(node) { + if (node.expression) { + return checkExpression(node.expression); } - var declaration = signature.declaration; - var modifiers = ts.getModifierFlags(declaration); - // Public constructor is accessible. - if (!(modifiers & 24 /* NonPublicAccessibilityModifier */)) { - return true; + else { + return unknownType; } - var declaringClassDeclaration = getClassLikeDeclarationOfSymbol(declaration.parent.symbol); - var declaringClass = getDeclaredTypeOfSymbol(declaration.parent.symbol); - // A private or protected constructor can only be instantiated within its own class (or a subclass, for protected) - if (!isNodeWithinClass(node, declaringClassDeclaration)) { - var containingClass = ts.getContainingClass(node); - if (containingClass) { - var containingType = getTypeOfNode(containingClass); - var baseTypes = getBaseTypes(containingType); - if (baseTypes.length) { - var baseType = baseTypes[0]; - if (modifiers & 16 /* Protected */ && - baseType.symbol === declaration.parent.symbol) { - return true; - } - } - } - if (modifiers & 8 /* Private */) { - error(node, ts.Diagnostics.Constructor_of_class_0_is_private_and_only_accessible_within_the_class_declaration, typeToString(declaringClass)); + } + // If a symbol is a synthesized symbol with no value declaration, we assume it is a property. Example of this are the synthesized + // '.prototype' property as well as synthesized tuple index properties. + function getDeclarationKindFromSymbol(s) { + return s.valueDeclaration ? s.valueDeclaration.kind : 145 /* PropertyDeclaration */; + } + function getDeclarationModifierFlagsFromSymbol(s) { + return s.valueDeclaration ? ts.getCombinedModifierFlags(s.valueDeclaration) : s.flags & 134217728 /* Prototype */ ? 4 /* Public */ | 32 /* Static */ : 0; + } + function getDeclarationNodeFlagsFromSymbol(s) { + return s.valueDeclaration ? ts.getCombinedNodeFlags(s.valueDeclaration) : 0; + } + /** + * Check whether the requested property access is valid. + * Returns true if node is a valid property access, and false otherwise. + * @param node The node to be checked. + * @param left The left hand side of the property access (e.g.: the super in `super.foo`). + * @param type The type of left. + * @param prop The symbol for the right hand side of the property access. + */ + function checkClassPropertyAccess(node, left, type, prop) { + var flags = getDeclarationModifierFlagsFromSymbol(prop); + var declaringClass = getDeclaredTypeOfSymbol(getParentOfSymbol(prop)); + var errorNode = node.kind === 172 /* PropertyAccessExpression */ || node.kind === 218 /* VariableDeclaration */ ? + node.name : + node.right; + if (left.kind === 95 /* SuperKeyword */) { + // TS 1.0 spec (April 2014): 4.8.2 + // - In a constructor, instance member function, instance member accessor, or + // instance member variable initializer where this references a derived class instance, + // a super property access is permitted and must specify a public instance member function of the base class. + // - In a static member function or static member accessor + // where this references the constructor function object of a derived class, + // a super property access is permitted and must specify a public static member function of the base class. + if (languageVersion < 2 /* ES6 */ && getDeclarationKindFromSymbol(prop) !== 147 /* MethodDeclaration */) { + // `prop` refers to a *property* declared in the super class + // rather than a *method*, so it does not satisfy the above criteria. + error(errorNode, ts.Diagnostics.Only_public_and_protected_methods_of_the_base_class_are_accessible_via_the_super_keyword); + return false; } - if (modifiers & 16 /* Protected */) { - error(node, ts.Diagnostics.Constructor_of_class_0_is_protected_and_only_accessible_within_the_class_declaration, typeToString(declaringClass)); + if (flags & 128 /* Abstract */) { + // A method cannot be accessed in a super property access if the method is abstract. + // This error could mask a private property access error. But, a member + // cannot simultaneously be private and abstract, so this will trigger an + // additional error elsewhere. + error(errorNode, ts.Diagnostics.Abstract_method_0_in_class_1_cannot_be_accessed_via_super_expression, symbolToString(prop), typeToString(declaringClass)); + return false; } - return false; } - return true; - } - function resolveTaggedTemplateExpression(node, candidatesOutArray) { - var tagType = checkExpression(node.tag); - var apparentType = getApparentType(tagType); - if (apparentType === unknownType) { - // Another error has already been reported - return resolveErrorCall(node); + // Public properties are otherwise accessible. + if (!(flags & 24 /* NonPublicAccessibilityModifier */)) { + return true; } - var callSignatures = getSignaturesOfType(apparentType, 0 /* Call */); - var constructSignatures = getSignaturesOfType(apparentType, 1 /* Construct */); - if (isUntypedFunctionCall(tagType, apparentType, callSignatures.length, constructSignatures.length)) { - return resolveUntypedCall(node); + // Property is known to be private or protected at this point + // Private property is accessible if the property is within the declaring class + if (flags & 8 /* Private */) { + var declaringClassDeclaration = getClassLikeDeclarationOfSymbol(getParentOfSymbol(prop)); + if (!isNodeWithinClass(node, declaringClassDeclaration)) { + error(errorNode, ts.Diagnostics.Property_0_is_private_and_only_accessible_within_class_1, symbolToString(prop), typeToString(declaringClass)); + return false; + } + return true; } - if (!callSignatures.length) { - error(node, ts.Diagnostics.Cannot_invoke_an_expression_whose_type_lacks_a_call_signature_Type_0_has_no_compatible_call_signatures, typeToString(apparentType)); - return resolveErrorCall(node); + // Property is known to be protected at this point + // All protected properties of a supertype are accessible in a super access + if (left.kind === 95 /* SuperKeyword */) { + return true; } - return resolveCall(node, callSignatures, candidatesOutArray); - } - /** - * Gets the localized diagnostic head message to use for errors when resolving a decorator as a call expression. - */ - function getDiagnosticHeadMessageForDecoratorResolution(node) { - switch (node.parent.kind) { - case 221 /* ClassDeclaration */: - case 192 /* ClassExpression */: - return ts.Diagnostics.Unable_to_resolve_signature_of_class_decorator_when_called_as_an_expression; - case 142 /* Parameter */: - return ts.Diagnostics.Unable_to_resolve_signature_of_parameter_decorator_when_called_as_an_expression; - case 145 /* PropertyDeclaration */: - return ts.Diagnostics.Unable_to_resolve_signature_of_property_decorator_when_called_as_an_expression; - case 147 /* MethodDeclaration */: - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - return ts.Diagnostics.Unable_to_resolve_signature_of_method_decorator_when_called_as_an_expression; + // Get the enclosing class that has the declaring class as its base type + var enclosingClass = forEachEnclosingClass(node, function (enclosingDeclaration) { + var enclosingClass = getDeclaredTypeOfSymbol(getSymbolOfNode(enclosingDeclaration)); + return hasBaseType(enclosingClass, declaringClass) ? enclosingClass : undefined; + }); + // A protected property is accessible if the property is within the declaring class or classes derived from it + if (!enclosingClass) { + error(errorNode, ts.Diagnostics.Property_0_is_protected_and_only_accessible_within_class_1_and_its_subclasses, symbolToString(prop), typeToString(declaringClass)); + return false; } - } - /** - * Resolves a decorator as if it were a call expression. - */ - function resolveDecorator(node, candidatesOutArray) { - var funcType = checkExpression(node.expression); - var apparentType = getApparentType(funcType); - if (apparentType === unknownType) { - return resolveErrorCall(node); + // No further restrictions for static properties + if (flags & 32 /* Static */) { + return true; } - var callSignatures = getSignaturesOfType(apparentType, 0 /* Call */); - var constructSignatures = getSignaturesOfType(apparentType, 1 /* Construct */); - if (isUntypedFunctionCall(funcType, apparentType, callSignatures.length, constructSignatures.length)) { - return resolveUntypedCall(node); + // An instance property must be accessed through an instance of the enclosing class + if (type.flags & 16384 /* TypeParameter */ && type.isThisType) { + // get the original type -- represented as the type constraint of the 'this' type + type = getConstraintOfTypeParameter(type); } - var headMessage = getDiagnosticHeadMessageForDecoratorResolution(node); - if (!callSignatures.length) { - var errorInfo = void 0; - errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Cannot_invoke_an_expression_whose_type_lacks_a_call_signature_Type_0_has_no_compatible_call_signatures, typeToString(apparentType)); - errorInfo = ts.chainDiagnosticMessages(errorInfo, headMessage); - diagnostics.add(ts.createDiagnosticForNodeFromMessageChain(node, errorInfo)); - return resolveErrorCall(node); + // TODO: why is the first part of this check here? + if (!(getTargetType(type).flags & (32768 /* Class */ | 65536 /* Interface */) && hasBaseType(type, enclosingClass))) { + error(errorNode, ts.Diagnostics.Property_0_is_protected_and_only_accessible_through_an_instance_of_class_1, symbolToString(prop), typeToString(enclosingClass)); + return false; } - return resolveCall(node, callSignatures, candidatesOutArray, headMessage); + return true; } - function resolveSignature(node, candidatesOutArray) { - switch (node.kind) { - case 174 /* CallExpression */: - return resolveCallExpression(node, candidatesOutArray); - case 175 /* NewExpression */: - return resolveNewExpression(node, candidatesOutArray); - case 176 /* TaggedTemplateExpression */: - return resolveTaggedTemplateExpression(node, candidatesOutArray); - case 143 /* Decorator */: - return resolveDecorator(node, candidatesOutArray); + function checkNonNullExpression(node) { + var type = checkExpression(node); + if (strictNullChecks) { + var kind = getFalsyFlags(type) & 6144 /* Nullable */; + if (kind) { + error(node, kind & 2048 /* Undefined */ ? kind & 4096 /* Null */ ? + ts.Diagnostics.Object_is_possibly_null_or_undefined : + ts.Diagnostics.Object_is_possibly_undefined : + ts.Diagnostics.Object_is_possibly_null); + } + return getNonNullableType(type); } - ts.Debug.fail("Branch in 'resolveSignature' should be unreachable."); + return type; } - // candidatesOutArray is passed by signature help in the language service, and collectCandidates - // must fill it up with the appropriate candidate signatures - function getResolvedSignature(node, candidatesOutArray) { - var links = getNodeLinks(node); - // If getResolvedSignature has already been called, we will have cached the resolvedSignature. - // However, it is possible that either candidatesOutArray was not passed in the first time, - // or that a different candidatesOutArray was passed in. Therefore, we need to redo the work - // to correctly fill the candidatesOutArray. - var cached = links.resolvedSignature; - if (cached && cached !== resolvingSignature && !candidatesOutArray) { - return cached; - } - links.resolvedSignature = resolvingSignature; - var result = resolveSignature(node, candidatesOutArray); - // If signature resolution originated in control flow type analysis (for example to compute the - // assigned type in a flow assignment) we don't cache the result as it may be based on temporary - // types from the control flow analysis. - links.resolvedSignature = flowLoopStart === flowLoopCount ? result : cached; - return result; + function checkPropertyAccessExpression(node) { + return checkPropertyAccessExpressionOrQualifiedName(node, node.expression, node.name); } - function getResolvedOrAnySignature(node) { - // If we're already in the process of resolving the given signature, don't resolve again as - // that could cause infinite recursion. Instead, return anySignature. - return getNodeLinks(node).resolvedSignature === resolvingSignature ? resolvingSignature : getResolvedSignature(node); + function checkQualifiedName(node) { + return checkPropertyAccessExpressionOrQualifiedName(node, node.left, node.right); } - function getInferredClassType(symbol) { - var links = getSymbolLinks(symbol); - if (!links.inferredClassType) { - links.inferredClassType = createAnonymousType(symbol, symbol.members, emptyArray, emptyArray, /*stringIndexType*/ undefined, /*numberIndexType*/ undefined); + function checkPropertyAccessExpressionOrQualifiedName(node, left, right) { + var type = checkNonNullExpression(left); + if (isTypeAny(type) || type === silentNeverType) { + return type; } - return links.inferredClassType; - } - /** - * Syntactically and semantically checks a call or new expression. - * @param node The call/new expression to be checked. - * @returns On success, the expression's signature's return type. On failure, anyType. - */ - function checkCallExpression(node) { - // Grammar checking; stop grammar-checking if checkGrammarTypeArguments return true - checkGrammarTypeArguments(node, node.typeArguments) || checkGrammarArguments(node, node.arguments); - var signature = getResolvedSignature(node); - if (node.expression.kind === 95 /* SuperKeyword */) { - return voidType; + var apparentType = getApparentType(getWidenedType(type)); + if (apparentType === unknownType || (type.flags & 16384 /* TypeParameter */ && isTypeAny(apparentType))) { + // handle cases when type is Type parameter with invalid or any constraint + return apparentType; } - if (node.kind === 175 /* NewExpression */) { - var declaration = signature.declaration; - if (declaration && - declaration.kind !== 148 /* Constructor */ && - declaration.kind !== 152 /* ConstructSignature */ && - declaration.kind !== 157 /* ConstructorType */ && - !ts.isJSDocConstructSignature(declaration)) { - // When resolved signature is a call signature (and not a construct signature) the result type is any, unless - // the declaring function had members created through 'x.prototype.y = expr' or 'this.y = expr' psuedodeclarations - // in a JS file - // Note:JS inferred classes might come from a variable declaration instead of a function declaration. - // In this case, using getResolvedSymbol directly is required to avoid losing the members from the declaration. - var funcSymbol = node.expression.kind === 69 /* Identifier */ ? - getResolvedSymbol(node.expression) : - checkExpression(node.expression).symbol; - if (funcSymbol && funcSymbol.members && (funcSymbol.flags & 16 /* Function */ || ts.isDeclarationOfFunctionExpression(funcSymbol))) { - return getInferredClassType(funcSymbol); - } - else if (compilerOptions.noImplicitAny) { - error(node, ts.Diagnostics.new_expression_whose_target_lacks_a_construct_signature_implicitly_has_an_any_type); - } - return anyType; + var prop = getPropertyOfType(apparentType, right.text); + if (!prop) { + if (right.text && !checkAndReportErrorForExtendingInterface(node)) { + reportNonexistentProperty(right, type.flags & 16384 /* TypeParameter */ && type.isThisType ? apparentType : type); } + return unknownType; } - // In JavaScript files, calls to any identifier 'require' are treated as external module imports - if (ts.isInJavaScriptFile(node) && ts.isRequireCall(node, /*checkArgumentIsStringLiteral*/ true)) { - return resolveExternalModuleTypeByLiteral(node.arguments[0]); + if (noUnusedIdentifiers && + (prop.flags & 106500 /* ClassMember */) && + prop.valueDeclaration && (ts.getModifierFlags(prop.valueDeclaration) & 8 /* Private */)) { + if (prop.flags & 16777216 /* Instantiated */) { + getSymbolLinks(prop).target.isReferenced = true; + } + else { + prop.isReferenced = true; + } } - return getReturnTypeOfSignature(signature); - } - function checkTaggedTemplateExpression(node) { - return getReturnTypeOfSignature(getResolvedSignature(node)); - } - function checkAssertion(node) { - var exprType = getRegularTypeOfObjectLiteral(getBaseTypeOfLiteralType(checkExpression(node.expression))); - checkSourceElement(node.type); - var targetType = getTypeFromTypeNode(node.type); - if (produceDiagnostics && targetType !== unknownType) { - var widenedType = getWidenedType(exprType); - if (!isTypeComparableTo(targetType, widenedType)) { - checkTypeComparableTo(exprType, targetType, node, ts.Diagnostics.Type_0_cannot_be_converted_to_type_1); + getNodeLinks(node).resolvedSymbol = prop; + if (prop.parent && prop.parent.flags & 32 /* Class */) { + checkClassPropertyAccess(node, left, apparentType, prop); + } + var propType = getTypeOfSymbol(prop); + // Only compute control flow type if this is a property access expression that isn't an + // assignment target, and the referenced property was declared as a variable, property, + // accessor, or optional method. + if (node.kind !== 172 /* PropertyAccessExpression */ || ts.isAssignmentTarget(node) || + !(prop.flags & (3 /* Variable */ | 4 /* Property */ | 98304 /* Accessor */)) && + !(prop.flags & 8192 /* Method */ && propType.flags & 524288 /* Union */)) { + return propType; + } + return getFlowTypeOfReference(node, propType, /*assumeInitialized*/ true, /*flowContainer*/ undefined); + function reportNonexistentProperty(propNode, containingType) { + var errorInfo; + if (containingType.flags & 524288 /* Union */ && !(containingType.flags & 8190 /* Primitive */)) { + for (var _i = 0, _a = containingType.types; _i < _a.length; _i++) { + var subtype = _a[_i]; + if (!getPropertyOfType(subtype, propNode.text)) { + errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Property_0_does_not_exist_on_type_1, ts.declarationNameToString(propNode), typeToString(subtype)); + break; + } + } } + errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Property_0_does_not_exist_on_type_1, ts.declarationNameToString(propNode), typeToString(containingType)); + diagnostics.add(ts.createDiagnosticForNodeFromMessageChain(propNode, errorInfo)); } - return targetType; - } - function checkNonNullAssertion(node) { - return getNonNullableType(checkExpression(node.expression)); } - function getTypeOfParameter(symbol) { - var type = getTypeOfSymbol(symbol); - if (strictNullChecks) { - var declaration = symbol.valueDeclaration; - if (declaration && declaration.initializer) { - return includeFalsyTypes(type, 2048 /* Undefined */); + function isValidPropertyAccess(node, propertyName) { + var left = node.kind === 172 /* PropertyAccessExpression */ + ? node.expression + : node.left; + var type = checkExpression(left); + if (type !== unknownType && !isTypeAny(type)) { + var prop = getPropertyOfType(getWidenedType(type), propertyName); + if (prop && prop.parent && prop.parent.flags & 32 /* Class */) { + return checkClassPropertyAccess(node, left, type, prop); } } - return type; - } - function getTypeAtPosition(signature, pos) { - return signature.hasRestParameter ? - pos < signature.parameters.length - 1 ? getTypeOfParameter(signature.parameters[pos]) : getRestTypeOfSignature(signature) : - pos < signature.parameters.length ? getTypeOfParameter(signature.parameters[pos]) : anyType; + return true; } - function assignContextualParameterTypes(signature, context, mapper) { - var len = signature.parameters.length - (signature.hasRestParameter ? 1 : 0); - if (context.thisParameter) { - if (!signature.thisParameter) { - signature.thisParameter = createTransientSymbol(context.thisParameter, undefined); + /** + * Return the symbol of the for-in variable declared or referenced by the given for-in statement. + */ + function getForInVariableSymbol(node) { + var initializer = node.initializer; + if (initializer.kind === 219 /* VariableDeclarationList */) { + var variable = initializer.declarations[0]; + if (variable && !ts.isBindingPattern(variable.name)) { + return getSymbolOfNode(variable); } - assignTypeToParameterAndFixTypeParameters(signature.thisParameter, getTypeOfSymbol(context.thisParameter), mapper); - } - for (var i = 0; i < len; i++) { - var parameter = signature.parameters[i]; - var contextualParameterType = getTypeAtPosition(context, i); - assignTypeToParameterAndFixTypeParameters(parameter, contextualParameterType, mapper); } - if (signature.hasRestParameter && isRestParameterIndex(context, signature.parameters.length - 1)) { - var parameter = ts.lastOrUndefined(signature.parameters); - var contextualParameterType = getTypeOfSymbol(ts.lastOrUndefined(context.parameters)); - assignTypeToParameterAndFixTypeParameters(parameter, contextualParameterType, mapper); + else if (initializer.kind === 69 /* Identifier */) { + return getResolvedSymbol(initializer); } + return undefined; } - // When contextual typing assigns a type to a parameter that contains a binding pattern, we also need to push - // the destructured type into the contained binding elements. - function assignBindingElementTypes(node) { - if (ts.isBindingPattern(node.name)) { - for (var _i = 0, _a = node.name.elements; _i < _a.length; _i++) { - var element = _a[_i]; - if (!ts.isOmittedExpression(element)) { - if (element.name.kind === 69 /* Identifier */) { - getSymbolLinks(getSymbolOfNode(element)).type = getTypeForBindingElement(element); + /** + * Return true if the given type is considered to have numeric property names. + */ + function hasNumericPropertyNames(type) { + return getIndexTypeOfType(type, 1 /* Number */) && !getIndexTypeOfType(type, 0 /* String */); + } + /** + * Return true if given node is an expression consisting of an identifier (possibly parenthesized) + * that references a for-in variable for an object with numeric property names. + */ + function isForInVariableForNumericPropertyNames(expr) { + var e = skipParenthesizedNodes(expr); + if (e.kind === 69 /* Identifier */) { + var symbol = getResolvedSymbol(e); + if (symbol.flags & 3 /* Variable */) { + var child = expr; + var node = expr.parent; + while (node) { + if (node.kind === 207 /* ForInStatement */ && + child === node.statement && + getForInVariableSymbol(node) === symbol && + hasNumericPropertyNames(checkExpression(node.expression))) { + return true; } - assignBindingElementTypes(element); + child = node; + node = node.parent; } } } + return false; } - function assignTypeToParameterAndFixTypeParameters(parameter, contextualType, mapper) { - var links = getSymbolLinks(parameter); - if (!links.type) { - links.type = instantiateType(contextualType, mapper); - // if inference didn't come up with anything but {}, fall back to the binding pattern if present. - if (links.type === emptyObjectType && - (parameter.valueDeclaration.name.kind === 167 /* ObjectBindingPattern */ || - parameter.valueDeclaration.name.kind === 168 /* ArrayBindingPattern */)) { - links.type = getTypeFromBindingPattern(parameter.valueDeclaration.name); + function checkIndexedAccess(node) { + // Grammar checking + if (!node.argumentExpression) { + var sourceFile = ts.getSourceFileOfNode(node); + if (node.parent.kind === 175 /* NewExpression */ && node.parent.expression === node) { + var start = ts.skipTrivia(sourceFile.text, node.expression.end); + var end = node.end; + grammarErrorAtPos(sourceFile, start, end - start, ts.Diagnostics.new_T_cannot_be_used_to_create_an_array_Use_new_Array_T_instead); + } + else { + var start = node.end - "]".length; + var end = node.end; + grammarErrorAtPos(sourceFile, start, end - start, ts.Diagnostics.Expression_expected); } - assignBindingElementTypes(parameter.valueDeclaration); } - else if (isInferentialContext(mapper)) { - // Even if the parameter already has a type, it might be because it was given a type while - // processing the function as an argument to a prior signature during overload resolution. - // If this was the case, it may have caused some type parameters to be fixed. So here, - // we need to ensure that type parameters at the same positions get fixed again. This is - // done by calling instantiateType to attach the mapper to the contextualType, and then - // calling inferTypes to force a walk of contextualType so that all the correct fixing - // happens. The choice to pass in links.type may seem kind of arbitrary, but it serves - // to make sure that all the correct positions in contextualType are reached by the walk. - // Here is an example: - // - // interface Base { - // baseProp; - // } - // interface Derived extends Base { - // toBase(): Base; - // } - // - // var derived: Derived; - // - // declare function foo(x: T, func: (p: T) => T): T; - // declare function foo(x: T, func: (p: T) => T): T; - // - // var result = foo(derived, d => d.toBase()); - // - // We are typing d while checking the second overload. But we've already given d - // a type (Derived) from the first overload. However, we still want to fix the - // T in the second overload so that we do not infer Base as a candidate for T - // (inferring Base would make type argument inference inconsistent between the two - // overloads). - inferTypes(mapper.context, links.type, instantiateType(contextualType, mapper)); - } - } - function getReturnTypeFromJSDocComment(func) { - var returnTag = ts.getJSDocReturnTag(func); - if (returnTag && returnTag.typeExpression) { - return getTypeFromTypeNode(returnTag.typeExpression.type); - } - return undefined; - } - function createPromiseType(promisedType) { - // creates a `Promise` type where `T` is the promisedType argument - var globalPromiseType = getGlobalPromiseType(); - if (globalPromiseType !== emptyGenericType) { - // if the promised type is itself a promise, get the underlying type; otherwise, fallback to the promised type - promisedType = getAwaitedType(promisedType); - return createTypeReference(globalPromiseType, [promisedType]); - } - return emptyObjectType; - } - function createPromiseReturnType(func, promisedType) { - var promiseType = createPromiseType(promisedType); - if (promiseType === emptyObjectType) { - error(func, ts.Diagnostics.An_async_function_or_method_must_have_a_valid_awaitable_return_type); - return unknownType; + // Obtain base constraint such that we can bail out if the constraint is an unknown type + var objectType = getApparentType(checkNonNullExpression(node.expression)); + var indexType = node.argumentExpression ? checkExpression(node.argumentExpression) : unknownType; + if (objectType === unknownType || objectType === silentNeverType) { + return objectType; } - return promiseType; - } - function getReturnTypeFromBody(func, contextualMapper) { - var contextualSignature = getContextualSignatureForFunctionLikeDeclaration(func); - if (!func.body) { + var isConstEnum = isConstEnumObjectType(objectType); + if (isConstEnum && + (!node.argumentExpression || node.argumentExpression.kind !== 9 /* StringLiteral */)) { + error(node.argumentExpression, ts.Diagnostics.A_const_enum_member_can_only_be_accessed_using_a_string_literal); return unknownType; } - var isAsync = ts.isAsyncFunctionLike(func); - var type; - if (func.body.kind !== 199 /* Block */) { - type = checkExpressionCached(func.body, contextualMapper); - if (isAsync) { - // From within an async function you can return either a non-promise value or a promise. Any - // Promise/A+ compatible implementation will always assimilate any foreign promise, so the - // return type of the body should be unwrapped to its awaited type, which we will wrap in - // the native Promise type later in this function. - type = checkAwaitedType(type, func, ts.Diagnostics.Return_expression_in_async_function_does_not_have_a_valid_callable_then_member); + // TypeScript 1.0 spec (April 2014): 4.10 Property Access + // - If IndexExpr is a string literal or a numeric literal and ObjExpr's apparent type has a property with the name + // given by that literal(converted to its string representation in the case of a numeric literal), the property access is of the type of that property. + // - Otherwise, if ObjExpr's apparent type has a numeric index signature and IndexExpr is of type Any, the Number primitive type, or an enum type, + // the property access is of the type of that index signature. + // - Otherwise, if ObjExpr's apparent type has a string index signature and IndexExpr is of type Any, the String or Number primitive type, or an enum type, + // the property access is of the type of that index signature. + // - Otherwise, if IndexExpr is of type Any, the String or Number primitive type, or an enum type, the property access is of type Any. + // See if we can index as a property. + if (node.argumentExpression) { + var name_16 = getPropertyNameForIndexedAccess(node.argumentExpression, indexType); + if (name_16 !== undefined) { + var prop = getPropertyOfType(objectType, name_16); + if (prop) { + getNodeLinks(node).resolvedSymbol = prop; + return getTypeOfSymbol(prop); + } + else if (isConstEnum) { + error(node.argumentExpression, ts.Diagnostics.Property_0_does_not_exist_on_const_enum_1, name_16, symbolToString(objectType.symbol)); + return unknownType; + } } } - else { - var types = void 0; - var funcIsGenerator = !!func.asteriskToken; - if (funcIsGenerator) { - types = checkAndAggregateYieldOperandTypes(func, contextualMapper); - if (types.length === 0) { - var iterableIteratorAny = createIterableIteratorType(anyType); - if (compilerOptions.noImplicitAny) { - error(func.asteriskToken, ts.Diagnostics.Generator_implicitly_has_type_0_because_it_does_not_yield_any_values_Consider_supplying_a_return_type, typeToString(iterableIteratorAny)); - } - return iterableIteratorAny; + // Check for compatible indexer types. + var allowedNullableFlags = strictNullChecks ? 0 : 6144 /* Nullable */; + if (isTypeAnyOrAllConstituentTypesHaveKind(indexType, 34 /* StringLike */ | 340 /* NumberLike */ | 512 /* ESSymbol */ | allowedNullableFlags)) { + // Try to use a number indexer. + if (isTypeAnyOrAllConstituentTypesHaveKind(indexType, 340 /* NumberLike */ | allowedNullableFlags) || isForInVariableForNumericPropertyNames(node.argumentExpression)) { + var numberIndexInfo = getIndexInfoOfType(objectType, 1 /* Number */); + if (numberIndexInfo) { + getNodeLinks(node).resolvedIndexInfo = numberIndexInfo; + return numberIndexInfo.type; } } - else { - types = checkAndAggregateReturnExpressionTypes(func, contextualMapper); - if (!types) { - // For an async function, the return type will not be never, but rather a Promise for never. - return isAsync ? createPromiseReturnType(func, neverType) : neverType; - } - if (types.length === 0) { - // For an async function, the return type will not be void, but rather a Promise for void. - return isAsync ? createPromiseReturnType(func, voidType) : voidType; - } + // Try to use string indexing. + var stringIndexInfo = getIndexInfoOfType(objectType, 0 /* String */); + if (stringIndexInfo) { + getNodeLinks(node).resolvedIndexInfo = stringIndexInfo; + return stringIndexInfo.type; } - // Return a union of the return expression types. - type = getUnionType(types, /*subtypeReduction*/ true); - if (funcIsGenerator) { - type = createIterableIteratorType(type); + // Fall back to any. + if (compilerOptions.noImplicitAny && !compilerOptions.suppressImplicitAnyIndexErrors && !isTypeAny(objectType)) { + error(node, getIndexTypeOfType(objectType, 1 /* Number */) ? + ts.Diagnostics.Element_implicitly_has_an_any_type_because_index_expression_is_not_of_type_number : + ts.Diagnostics.Index_signature_of_object_type_implicitly_has_an_any_type); } + return anyType; } - if (!contextualSignature) { - reportErrorsFromWidening(func, type); - } - if (isUnitType(type) && !(contextualSignature && isLiteralContextualType(getReturnTypeOfSignature(contextualSignature)))) { - type = getWidenedLiteralType(type); - } - var widenedType = getWidenedType(type); - // From within an async function you can return either a non-promise value or a promise. Any - // Promise/A+ compatible implementation will always assimilate any foreign promise, so the - // return type of the body is awaited type of the body, wrapped in a native Promise type. - return isAsync ? createPromiseReturnType(func, widenedType) : widenedType; + // REVIEW: Users should know the type that was actually used. + error(node, ts.Diagnostics.An_index_expression_argument_must_be_of_type_string_number_symbol_or_any); + return unknownType; } - function checkAndAggregateYieldOperandTypes(func, contextualMapper) { - var aggregatedTypes = []; - ts.forEachYieldExpression(func.body, function (yieldExpression) { - var expr = yieldExpression.expression; - if (expr) { - var type = checkExpressionCached(expr, contextualMapper); - if (yieldExpression.asteriskToken) { - // A yield* expression effectively yields everything that its operand yields - type = checkElementTypeOfIterable(type, yieldExpression.expression); - } - if (!ts.contains(aggregatedTypes, type)) { - aggregatedTypes.push(type); - } + /** + * If indexArgumentExpression is a string literal or number literal, returns its text. + * If indexArgumentExpression is a constant value, returns its string value. + * If indexArgumentExpression is a well known symbol, returns the property name corresponding + * to this symbol, as long as it is a proper symbol reference. + * Otherwise, returns undefined. + */ + function getPropertyNameForIndexedAccess(indexArgumentExpression, indexArgumentType) { + if (indexArgumentExpression.kind === 9 /* StringLiteral */ || indexArgumentExpression.kind === 8 /* NumericLiteral */) { + return indexArgumentExpression.text; + } + if (indexArgumentExpression.kind === 173 /* ElementAccessExpression */ || indexArgumentExpression.kind === 172 /* PropertyAccessExpression */) { + var value = getConstantValue(indexArgumentExpression); + if (value !== undefined) { + return value.toString(); } - }); - return aggregatedTypes; + } + if (checkThatExpressionIsProperSymbolReference(indexArgumentExpression, indexArgumentType, /*reportError*/ false)) { + var rightHandSideName = indexArgumentExpression.name.text; + return ts.getPropertyNameForKnownSymbolName(rightHandSideName); + } + return undefined; } - function isExhaustiveSwitchStatement(node) { - if (!node.possiblyExhaustive) { + /** + * A proper symbol reference requires the following: + * 1. The property access denotes a property that exists + * 2. The expression is of the form Symbol. + * 3. The property access is of the primitive type symbol. + * 4. Symbol in this context resolves to the global Symbol object + */ + function checkThatExpressionIsProperSymbolReference(expression, expressionType, reportError) { + if (expressionType === unknownType) { + // There is already an error, so no need to report one. return false; } - var type = checkExpression(node.expression); - if (!isLiteralType(type)) { + if (!ts.isWellKnownSymbolSyntactically(expression)) { return false; } - var switchTypes = getSwitchClauseTypes(node); - if (!switchTypes.length) { + // Make sure the property type is the primitive symbol type + if ((expressionType.flags & 512 /* ESSymbol */) === 0) { + if (reportError) { + error(expression, ts.Diagnostics.A_computed_property_name_of_the_form_0_must_be_of_type_symbol, ts.getTextOfNode(expression)); + } return false; } - return eachTypeContainedIn(type, switchTypes); - } - function functionHasImplicitReturn(func) { - if (!(func.flags & 128 /* HasImplicitReturn */)) { + // The name is Symbol., so make sure Symbol actually resolves to the + // global Symbol object + var leftHandSide = expression.expression; + var leftHandSideSymbol = getResolvedSymbol(leftHandSide); + if (!leftHandSideSymbol) { return false; } - var lastStatement = ts.lastOrUndefined(func.body.statements); - if (lastStatement && lastStatement.kind === 213 /* SwitchStatement */ && isExhaustiveSwitchStatement(lastStatement)) { + var globalESSymbol = getGlobalESSymbolConstructorSymbol(); + if (!globalESSymbol) { + // Already errored when we tried to look up the symbol + return false; + } + if (leftHandSideSymbol !== globalESSymbol) { + if (reportError) { + error(leftHandSide, ts.Diagnostics.Symbol_reference_does_not_refer_to_the_global_Symbol_constructor_object); + } return false; } return true; } - function checkAndAggregateReturnExpressionTypes(func, contextualMapper) { - var isAsync = ts.isAsyncFunctionLike(func); - var aggregatedTypes = []; - var hasReturnWithNoExpression = functionHasImplicitReturn(func); - var hasReturnOfTypeNever = false; - ts.forEachReturnStatement(func.body, function (returnStatement) { - var expr = returnStatement.expression; - if (expr) { - var type = checkExpressionCached(expr, contextualMapper); - if (isAsync) { - // From within an async function you can return either a non-promise value or a promise. Any - // Promise/A+ compatible implementation will always assimilate any foreign promise, so the - // return type of the body should be unwrapped to its awaited type, which should be wrapped in - // the native Promise type by the caller. - type = checkAwaitedType(type, func, ts.Diagnostics.Return_expression_in_async_function_does_not_have_a_valid_callable_then_member); - } - if (type.flags & 8192 /* Never */) { - hasReturnOfTypeNever = true; + function resolveUntypedCall(node) { + if (node.kind === 176 /* TaggedTemplateExpression */) { + checkExpression(node.template); + } + else if (node.kind !== 143 /* Decorator */) { + ts.forEach(node.arguments, function (argument) { + checkExpression(argument); + }); + } + return anySignature; + } + function resolveErrorCall(node) { + resolveUntypedCall(node); + return unknownSignature; + } + // Re-order candidate signatures into the result array. Assumes the result array to be empty. + // The candidate list orders groups in reverse, but within a group signatures are kept in declaration order + // A nit here is that we reorder only signatures that belong to the same symbol, + // so order how inherited signatures are processed is still preserved. + // interface A { (x: string): void } + // interface B extends A { (x: 'foo'): string } + // const b: B; + // b('foo') // <- here overloads should be processed as [(x:'foo'): string, (x: string): void] + function reorderCandidates(signatures, result) { + var lastParent; + var lastSymbol; + var cutoffIndex = 0; + var index; + var specializedIndex = -1; + var spliceIndex; + ts.Debug.assert(!result.length); + for (var _i = 0, signatures_2 = signatures; _i < signatures_2.length; _i++) { + var signature = signatures_2[_i]; + var symbol = signature.declaration && getSymbolOfNode(signature.declaration); + var parent_11 = signature.declaration && signature.declaration.parent; + if (!lastSymbol || symbol === lastSymbol) { + if (lastParent && parent_11 === lastParent) { + index++; } - else if (!ts.contains(aggregatedTypes, type)) { - aggregatedTypes.push(type); + else { + lastParent = parent_11; + index = cutoffIndex; } } else { - hasReturnWithNoExpression = true; + // current declaration belongs to a different symbol + // set cutoffIndex so re-orderings in the future won't change result set from 0 to cutoffIndex + index = cutoffIndex = result.length; + lastParent = parent_11; } - }); - if (aggregatedTypes.length === 0 && !hasReturnWithNoExpression && (hasReturnOfTypeNever || - func.kind === 179 /* FunctionExpression */ || func.kind === 180 /* ArrowFunction */)) { - return undefined; + lastSymbol = symbol; + // specialized signatures always need to be placed before non-specialized signatures regardless + // of the cutoff position; see GH#1133 + if (signature.hasLiteralTypes) { + specializedIndex++; + spliceIndex = specializedIndex; + // The cutoff index always needs to be greater than or equal to the specialized signature index + // in order to prevent non-specialized signatures from being added before a specialized + // signature. + cutoffIndex++; + } + else { + spliceIndex = index; + } + result.splice(spliceIndex, 0, signature); } - if (strictNullChecks && aggregatedTypes.length && hasReturnWithNoExpression) { - if (!ts.contains(aggregatedTypes, undefinedType)) { - aggregatedTypes.push(undefinedType); + } + function getSpreadArgumentIndex(args) { + for (var i = 0; i < args.length; i++) { + var arg = args[i]; + if (arg && arg.kind === 191 /* SpreadElementExpression */) { + return i; } } - return aggregatedTypes; + return -1; } - /** - * TypeScript Specification 1.0 (6.3) - July 2014 - * An explicitly typed function whose return type isn't the Void type, - * the Any type, or a union type containing the Void or Any type as a constituent - * must have at least one return statement somewhere in its body. - * An exception to this rule is if the function implementation consists of a single 'throw' statement. - * - * @param returnType - return type of the function, can be undefined if return type is not explicitly specified - */ - function checkAllCodePathsInNonVoidFunctionReturnOrThrow(func, returnType) { - if (!produceDiagnostics) { - return; + function hasCorrectArity(node, args, signature, signatureHelpTrailingComma) { + if (signatureHelpTrailingComma === void 0) { signatureHelpTrailingComma = false; } + var argCount; // Apparent number of arguments we will have in this call + var typeArguments; // Type arguments (undefined if none) + var callIsIncomplete; // In incomplete call we want to be lenient when we have too few arguments + var isDecorator; + var spreadArgIndex = -1; + if (node.kind === 176 /* TaggedTemplateExpression */) { + var tagExpression = node; + // Even if the call is incomplete, we'll have a missing expression as our last argument, + // so we can say the count is just the arg list length + argCount = args.length; + typeArguments = undefined; + if (tagExpression.template.kind === 189 /* TemplateExpression */) { + // If a tagged template expression lacks a tail literal, the call is incomplete. + // Specifically, a template only can end in a TemplateTail or a Missing literal. + var templateExpression = tagExpression.template; + var lastSpan = ts.lastOrUndefined(templateExpression.templateSpans); + ts.Debug.assert(lastSpan !== undefined); // we should always have at least one span. + callIsIncomplete = ts.nodeIsMissing(lastSpan.literal) || !!lastSpan.literal.isUnterminated; + } + else { + // If the template didn't end in a backtick, or its beginning occurred right prior to EOF, + // then this might actually turn out to be a TemplateHead in the future; + // so we consider the call to be incomplete. + var templateLiteral = tagExpression.template; + ts.Debug.assert(templateLiteral.kind === 11 /* NoSubstitutionTemplateLiteral */); + callIsIncomplete = !!templateLiteral.isUnterminated; + } } - // Functions with with an explicitly specified 'void' or 'any' return type don't need any return expressions. - if (returnType && maybeTypeOfKind(returnType, 1 /* Any */ | 1024 /* Void */)) { - return; + else if (node.kind === 143 /* Decorator */) { + isDecorator = true; + typeArguments = undefined; + argCount = getEffectiveArgumentCount(node, /*args*/ undefined, signature); } - // If all we have is a function signature, or an arrow function with an expression body, then there is nothing to check. - // also if HasImplicitReturn flag is not set this means that all codepaths in function body end with return or throw - if (ts.nodeIsMissing(func.body) || func.body.kind !== 199 /* Block */ || !functionHasImplicitReturn(func)) { - return; + else { + var callExpression = node; + if (!callExpression.arguments) { + // This only happens when we have something of the form: 'new C' + ts.Debug.assert(callExpression.kind === 175 /* NewExpression */); + return signature.minArgumentCount === 0; + } + argCount = signatureHelpTrailingComma ? args.length + 1 : args.length; + // If we are missing the close paren, the call is incomplete. + callIsIncomplete = callExpression.arguments.end === callExpression.end; + typeArguments = callExpression.typeArguments; + spreadArgIndex = getSpreadArgumentIndex(args); } - var hasExplicitReturn = func.flags & 256 /* HasExplicitReturn */; - if (returnType && returnType.flags & 8192 /* Never */) { - error(func.type, ts.Diagnostics.A_function_returning_never_cannot_have_a_reachable_end_point); + // If the user supplied type arguments, but the number of type arguments does not match + // the declared number of type parameters, the call has an incorrect arity. + var hasRightNumberOfTypeArgs = !typeArguments || + (signature.typeParameters && typeArguments.length === signature.typeParameters.length); + if (!hasRightNumberOfTypeArgs) { + return false; } - else if (returnType && !hasExplicitReturn) { - // minimal check: function has syntactic return type annotation and no explicit return statements in the body - // this function does not conform to the specification. - // NOTE: having returnType !== undefined is a precondition for entering this branch so func.type will always be present - error(func.type, ts.Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value); + // If spread arguments are present, check that they correspond to a rest parameter. If so, no + // further checking is necessary. + if (spreadArgIndex >= 0) { + return isRestParameterIndex(signature, spreadArgIndex); } - else if (returnType && strictNullChecks && !isTypeAssignableTo(undefinedType, returnType)) { - error(func.type, ts.Diagnostics.Function_lacks_ending_return_statement_and_return_type_does_not_include_undefined); + // Too many arguments implies incorrect arity. + if (!signature.hasRestParameter && argCount > signature.parameters.length) { + return false; } - else if (compilerOptions.noImplicitReturns) { - if (!returnType) { - // If return type annotation is omitted check if function has any explicit return statements. - // If it does not have any - its inferred return type is void - don't do any checks. - // Otherwise get inferred return type from function body and report error only if it is not void / anytype - if (!hasExplicitReturn) { - return; - } - var inferredReturnType = getReturnTypeOfSignature(getSignatureFromDeclaration(func)); - if (isUnwrappedReturnTypeVoidOrAny(func, inferredReturnType)) { - return; - } + // If the call is incomplete, we should skip the lower bound check. + var hasEnoughArguments = argCount >= signature.minArgumentCount; + return callIsIncomplete || hasEnoughArguments; + } + // If type has a single call signature and no other members, return that signature. Otherwise, return undefined. + function getSingleCallSignature(type) { + if (type.flags & 2588672 /* ObjectType */) { + var resolved = resolveStructuredTypeMembers(type); + if (resolved.callSignatures.length === 1 && resolved.constructSignatures.length === 0 && + resolved.properties.length === 0 && !resolved.stringIndexInfo && !resolved.numberIndexInfo) { + return resolved.callSignatures[0]; } - error(func.type || func, ts.Diagnostics.Not_all_code_paths_return_a_value); } + return undefined; } - function checkFunctionExpressionOrObjectLiteralMethod(node, contextualMapper) { - ts.Debug.assert(node.kind !== 147 /* MethodDeclaration */ || ts.isObjectLiteralMethod(node)); - // Grammar checking - var hasGrammarError = checkGrammarFunctionLikeDeclaration(node); - if (!hasGrammarError && node.kind === 179 /* FunctionExpression */) { - checkGrammarForGenerator(node); - } - // The identityMapper object is used to indicate that function expressions are wildcards - if (contextualMapper === identityMapper && isContextSensitive(node)) { - checkNodeDeferred(node); - return anyFunctionType; - } - var links = getNodeLinks(node); - var type = getTypeOfSymbol(node.symbol); - var contextSensitive = isContextSensitive(node); - var mightFixTypeParameters = contextSensitive && isInferentialContext(contextualMapper); - // Check if function expression is contextually typed and assign parameter types if so. - // See the comment in assignTypeToParameterAndFixTypeParameters to understand why we need to - // check mightFixTypeParameters. - if (mightFixTypeParameters || !(links.flags & 1024 /* ContextChecked */)) { - var contextualSignature = getContextualSignature(node); - // If a type check is started at a function expression that is an argument of a function call, obtaining the - // contextual type may recursively get back to here during overload resolution of the call. If so, we will have - // already assigned contextual types. - var contextChecked = !!(links.flags & 1024 /* ContextChecked */); - if (mightFixTypeParameters || !contextChecked) { - links.flags |= 1024 /* ContextChecked */; - if (contextualSignature) { - var signature = getSignaturesOfType(type, 0 /* Call */)[0]; - if (contextSensitive) { - assignContextualParameterTypes(signature, contextualSignature, contextualMapper || identityMapper); - } - if (mightFixTypeParameters || !node.type && !signature.resolvedReturnType) { - var returnType = getReturnTypeFromBody(node, contextualMapper); - if (!signature.resolvedReturnType) { - signature.resolvedReturnType = returnType; - } - } - } - if (!contextChecked) { - checkSignatureDeclaration(node); - checkNodeDeferred(node); - } + // Instantiate a generic signature in the context of a non-generic signature (section 3.8.5 in TypeScript spec) + function instantiateSignatureInContextOf(signature, contextualSignature, contextualMapper) { + var context = createInferenceContext(signature, /*inferUnionTypes*/ true); + forEachMatchingParameterType(contextualSignature, signature, function (source, target) { + // Type parameters from outer context referenced by source type are fixed by instantiation of the source type + inferTypes(context, instantiateType(source, contextualMapper), target); + }); + return getSignatureInstantiation(signature, getInferredTypes(context)); + } + function inferTypeArguments(node, signature, args, excludeArgument, context) { + var typeParameters = signature.typeParameters; + var inferenceMapper = getInferenceMapper(context); + // Clear out all the inference results from the last time inferTypeArguments was called on this context + for (var i = 0; i < typeParameters.length; i++) { + // As an optimization, we don't have to clear (and later recompute) inferred types + // for type parameters that have already been fixed on the previous call to inferTypeArguments. + // It would be just as correct to reset all of them. But then we'd be repeating the same work + // for the type parameters that were fixed, namely the work done by getInferredType. + if (!context.inferences[i].isFixed) { + context.inferredTypes[i] = undefined; } } - if (produceDiagnostics && node.kind !== 147 /* MethodDeclaration */ && node.kind !== 146 /* MethodSignature */) { - checkCollisionWithCapturedSuperVariable(node, node.name); - checkCollisionWithCapturedThisVariable(node, node.name); + // On this call to inferTypeArguments, we may get more inferences for certain type parameters that were not + // fixed last time. This means that a type parameter that failed inference last time may succeed this time, + // or vice versa. Therefore, the failedTypeParameterIndex is useless if it points to an unfixed type parameter, + // because it may change. So here we reset it. However, getInferredType will not revisit any type parameters + // that were previously fixed. So if a fixed type parameter failed previously, it will fail again because + // it will contain the exact same set of inferences. So if we reset the index from a fixed type parameter, + // we will lose information that we won't recover this time around. + if (context.failedTypeParameterIndex !== undefined && !context.inferences[context.failedTypeParameterIndex].isFixed) { + context.failedTypeParameterIndex = undefined; } - return type; - } - function checkFunctionExpressionOrObjectLiteralMethodDeferred(node) { - ts.Debug.assert(node.kind !== 147 /* MethodDeclaration */ || ts.isObjectLiteralMethod(node)); - var isAsync = ts.isAsyncFunctionLike(node); - var returnOrPromisedType = node.type && (isAsync ? checkAsyncFunctionReturnType(node) : getTypeFromTypeNode(node.type)); - if (!node.asteriskToken) { - // return is not necessary in the body of generators - checkAllCodePathsInNonVoidFunctionReturnOrThrow(node, returnOrPromisedType); + var thisType = getThisTypeOfSignature(signature); + if (thisType) { + var thisArgumentNode = getThisArgumentOfCall(node); + var thisArgumentType = thisArgumentNode ? checkExpression(thisArgumentNode) : voidType; + inferTypes(context, thisArgumentType, thisType); } - if (node.body) { - if (!node.type) { - // There are some checks that are only performed in getReturnTypeFromBody, that may produce errors - // we need. An example is the noImplicitAny errors resulting from widening the return expression - // of a function. Because checking of function expression bodies is deferred, there was never an - // appropriate time to do this during the main walk of the file (see the comment at the top of - // checkFunctionExpressionBodies). So it must be done now. - getReturnTypeOfSignature(getSignatureFromDeclaration(node)); - } - if (node.body.kind === 199 /* Block */) { - checkSourceElement(node.body); - } - else { - // From within an async function you can return either a non-promise value or a promise. Any - // Promise/A+ compatible implementation will always assimilate any foreign promise, so we - // should not be checking assignability of a promise to the return type. Instead, we need to - // check assignability of the awaited type of the expression body against the promised type of - // its return type annotation. - var exprType = checkExpression(node.body); - if (returnOrPromisedType) { - if (isAsync) { - var awaitedType = checkAwaitedType(exprType, node.body, ts.Diagnostics.Expression_body_for_async_arrow_function_does_not_have_a_valid_callable_then_member); - checkTypeAssignableTo(awaitedType, returnOrPromisedType, node.body); - } - else { - checkTypeAssignableTo(exprType, returnOrPromisedType, node.body); - } + // We perform two passes over the arguments. In the first pass we infer from all arguments, but use + // wildcards for all context sensitive function expressions. + var argCount = getEffectiveArgumentCount(node, args, signature); + for (var i = 0; i < argCount; i++) { + var arg = getEffectiveArgument(node, args, i); + // If the effective argument is 'undefined', then it is an argument that is present but is synthetic. + if (arg === undefined || arg.kind !== 193 /* OmittedExpression */) { + var paramType = getTypeAtPosition(signature, i); + var argType = getEffectiveArgumentType(node, i, arg); + // If the effective argument type is 'undefined', there is no synthetic type + // for the argument. In that case, we should check the argument. + if (argType === undefined) { + // For context sensitive arguments we pass the identityMapper, which is a signal to treat all + // context sensitive function expressions as wildcards + var mapper = excludeArgument && excludeArgument[i] !== undefined ? identityMapper : inferenceMapper; + argType = checkExpressionWithContextualType(arg, paramType, mapper); } + inferTypes(context, argType, paramType); } - registerForUnusedIdentifiersCheck(node); - } - } - function checkArithmeticOperandType(operand, type, diagnostic) { - if (!isTypeAnyOrAllConstituentTypesHaveKind(type, 340 /* NumberLike */)) { - error(operand, diagnostic); - return false; } - return true; - } - function isReadonlySymbol(symbol) { - // The following symbols are considered read-only: - // Properties with a 'readonly' modifier - // Variables declared with 'const' - // Get accessors without matching set accessors - // Enum members - // Unions and intersections of the above (unions and intersections eagerly set isReadonly on creation) - return symbol.isReadonly || - symbol.flags & 4 /* Property */ && (getDeclarationModifierFlagsFromSymbol(symbol) & 64 /* Readonly */) !== 0 || - symbol.flags & 3 /* Variable */ && (getDeclarationNodeFlagsFromSymbol(symbol) & 2 /* Const */) !== 0 || - symbol.flags & 98304 /* Accessor */ && !(symbol.flags & 65536 /* SetAccessor */) || - (symbol.flags & 8 /* EnumMember */) !== 0; - } - function isReferenceToReadonlyEntity(expr, symbol) { - if (isReadonlySymbol(symbol)) { - // Allow assignments to readonly properties within constructors of the same class declaration. - if (symbol.flags & 4 /* Property */ && - (expr.kind === 172 /* PropertyAccessExpression */ || expr.kind === 173 /* ElementAccessExpression */) && - expr.expression.kind === 97 /* ThisKeyword */) { - // Look for if this is the constructor for the class that `symbol` is a property of. - var func = ts.getContainingFunction(expr); - if (!(func && func.kind === 148 /* Constructor */)) - return true; - // If func.parent is a class and symbol is a (readonly) property of that class, or - // if func is a constructor and symbol is a (readonly) parameter property declared in it, - // then symbol is writeable here. - return !(func.parent === symbol.valueDeclaration.parent || func === symbol.valueDeclaration.parent); + // In the second pass we visit only context sensitive arguments, and only those that aren't excluded, this + // time treating function expressions normally (which may cause previously inferred type arguments to be fixed + // as we construct types for contextually typed parameters) + // Decorators will not have `excludeArgument`, as their arguments cannot be contextually typed. + // Tagged template expressions will always have `undefined` for `excludeArgument[0]`. + if (excludeArgument) { + for (var i = 0; i < argCount; i++) { + // No need to check for omitted args and template expressions, their exclusion value is always undefined + if (excludeArgument[i] === false) { + var arg = args[i]; + var paramType = getTypeAtPosition(signature, i); + inferTypes(context, checkExpressionWithContextualType(arg, paramType, inferenceMapper), paramType); + } } - return true; } - return false; + getInferredTypes(context); } - function isReferenceThroughNamespaceImport(expr) { - if (expr.kind === 172 /* PropertyAccessExpression */ || expr.kind === 173 /* ElementAccessExpression */) { - var node = skipParenthesizedNodes(expr.expression); - if (node.kind === 69 /* Identifier */) { - var symbol = getNodeLinks(node).resolvedSymbol; - if (symbol.flags & 8388608 /* Alias */) { - var declaration = getDeclarationOfAliasSymbol(symbol); - return declaration && declaration.kind === 232 /* NamespaceImport */; + function checkTypeArguments(signature, typeArgumentNodes, typeArgumentTypes, reportErrors, headMessage) { + var typeParameters = signature.typeParameters; + var typeArgumentsAreAssignable = true; + var mapper; + for (var i = 0; i < typeParameters.length; i++) { + if (typeArgumentsAreAssignable /* so far */) { + var constraint = getConstraintOfTypeParameter(typeParameters[i]); + if (constraint) { + var errorInfo = void 0; + var typeArgumentHeadMessage = ts.Diagnostics.Type_0_does_not_satisfy_the_constraint_1; + if (reportErrors && headMessage) { + errorInfo = ts.chainDiagnosticMessages(errorInfo, typeArgumentHeadMessage); + typeArgumentHeadMessage = headMessage; + } + if (!mapper) { + mapper = createTypeMapper(typeParameters, typeArgumentTypes); + } + var typeArgument = typeArgumentTypes[i]; + typeArgumentsAreAssignable = checkTypeAssignableTo(typeArgument, getTypeWithThisArgument(instantiateType(constraint, mapper), typeArgument), reportErrors ? typeArgumentNodes[i] : undefined, typeArgumentHeadMessage, errorInfo); } } } - return false; + return typeArgumentsAreAssignable; } - function checkReferenceExpression(expr, invalidReferenceMessage, constantVariableMessage) { - // References are combinations of identifiers, parentheses, and property accesses. - var node = skipParenthesizedNodes(expr); - if (node.kind !== 69 /* Identifier */ && node.kind !== 172 /* PropertyAccessExpression */ && node.kind !== 173 /* ElementAccessExpression */) { - error(expr, invalidReferenceMessage); - return false; + function checkApplicableSignature(node, args, signature, relation, excludeArgument, reportErrors) { + var thisType = getThisTypeOfSignature(signature); + if (thisType && thisType !== voidType && node.kind !== 175 /* NewExpression */) { + // If the called expression is not of the form `x.f` or `x["f"]`, then sourceType = voidType + // If the signature's 'this' type is voidType, then the check is skipped -- anything is compatible. + // If the expression is a new expression, then the check is skipped. + var thisArgumentNode = getThisArgumentOfCall(node); + var thisArgumentType = thisArgumentNode ? checkExpression(thisArgumentNode) : voidType; + var errorNode = reportErrors ? (thisArgumentNode || node) : undefined; + var headMessage_1 = ts.Diagnostics.The_this_context_of_type_0_is_not_assignable_to_method_s_this_of_type_1; + if (!checkTypeRelatedTo(thisArgumentType, getThisTypeOfSignature(signature), relation, errorNode, headMessage_1)) { + return false; + } } - // Because we get the symbol from the resolvedSymbol property, it might be of kind - // SymbolFlags.ExportValue. In this case it is necessary to get the actual export - // symbol, which will have the correct flags set on it. - var links = getNodeLinks(node); - var symbol = getExportSymbolOfValueSymbolIfExported(links.resolvedSymbol); - if (symbol) { - if (symbol !== unknownSymbol && symbol !== argumentsSymbol) { - // Only variables (and not functions, classes, namespaces, enum objects, or enum members) - // are considered references when referenced using a simple identifier. - if (node.kind === 69 /* Identifier */ && !(symbol.flags & 3 /* Variable */)) { - error(expr, invalidReferenceMessage); - return false; + var headMessage = ts.Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1; + var argCount = getEffectiveArgumentCount(node, args, signature); + for (var i = 0; i < argCount; i++) { + var arg = getEffectiveArgument(node, args, i); + // If the effective argument is 'undefined', then it is an argument that is present but is synthetic. + if (arg === undefined || arg.kind !== 193 /* OmittedExpression */) { + // Check spread elements against rest type (from arity check we know spread argument corresponds to a rest parameter) + var paramType = getTypeAtPosition(signature, i); + var argType = getEffectiveArgumentType(node, i, arg); + // If the effective argument type is 'undefined', there is no synthetic type + // for the argument. In that case, we should check the argument. + if (argType === undefined) { + argType = checkExpressionWithContextualType(arg, paramType, excludeArgument && excludeArgument[i] ? identityMapper : undefined); } - if (isReferenceToReadonlyEntity(node, symbol) || isReferenceThroughNamespaceImport(node)) { - error(expr, constantVariableMessage); + // Use argument expression as error location when reporting errors + var errorNode = reportErrors ? getEffectiveArgumentErrorNode(node, i, arg) : undefined; + if (!checkTypeRelatedTo(argType, paramType, relation, errorNode, headMessage)) { return false; } } } - else if (node.kind === 173 /* ElementAccessExpression */) { - if (links.resolvedIndexInfo && links.resolvedIndexInfo.isReadonly) { - error(expr, constantVariableMessage); - return false; - } - } return true; } - function checkDeleteExpression(node) { - checkExpression(node.expression); - return booleanType; - } - function checkTypeOfExpression(node) { - checkExpression(node.expression); - return stringType; - } - function checkVoidExpression(node) { - checkExpression(node.expression); - return undefinedWideningType; - } - function checkAwaitExpression(node) { - // Grammar checking - if (produceDiagnostics) { - if (!(node.flags & 262144 /* AwaitContext */)) { - grammarErrorOnFirstToken(node, ts.Diagnostics.await_expression_is_only_allowed_within_an_async_function); + /** + * Returns the this argument in calls like x.f(...) and x[f](...). Undefined otherwise. + */ + function getThisArgumentOfCall(node) { + if (node.kind === 174 /* CallExpression */) { + var callee = node.expression; + if (callee.kind === 172 /* PropertyAccessExpression */) { + return callee.expression; } - if (isInParameterInitializerBeforeContainingFunction(node)) { - error(node, ts.Diagnostics.await_expressions_cannot_be_used_in_a_parameter_initializer); + else if (callee.kind === 173 /* ElementAccessExpression */) { + return callee.expression; } } - var operandType = checkExpression(node.expression); - return checkAwaitedType(operandType, node); } - function checkPrefixUnaryExpression(node) { - var operandType = checkExpression(node.operand); - if (operandType === silentNeverType) { - return silentNeverType; + /** + * Returns the effective arguments for an expression that works like a function invocation. + * + * If 'node' is a CallExpression or a NewExpression, then its argument list is returned. + * If 'node' is a TaggedTemplateExpression, a new argument list is constructed from the substitution + * expressions, where the first element of the list is `undefined`. + * If 'node' is a Decorator, the argument list will be `undefined`, and its arguments and types + * will be supplied from calls to `getEffectiveArgumentCount` and `getEffectiveArgumentType`. + */ + function getEffectiveCallArguments(node) { + var args; + if (node.kind === 176 /* TaggedTemplateExpression */) { + var template = node.template; + args = [undefined]; + if (template.kind === 189 /* TemplateExpression */) { + ts.forEach(template.templateSpans, function (span) { + args.push(span.expression); + }); + } } - if (node.operator === 36 /* MinusToken */ && node.operand.kind === 8 /* NumericLiteral */) { - return getFreshTypeOfLiteralType(getLiteralTypeForText(64 /* NumberLiteral */, "" + -node.operand.text)); + else if (node.kind === 143 /* Decorator */) { + // For a decorator, we return undefined as we will determine + // the number and types of arguments for a decorator using + // `getEffectiveArgumentCount` and `getEffectiveArgumentType` below. + return undefined; } - switch (node.operator) { - case 35 /* PlusToken */: - case 36 /* MinusToken */: - case 50 /* TildeToken */: - if (maybeTypeOfKind(operandType, 512 /* ESSymbol */)) { - error(node.operand, ts.Diagnostics.The_0_operator_cannot_be_applied_to_type_symbol, ts.tokenToString(node.operator)); - } - return numberType; - case 49 /* ExclamationToken */: - var facts = getTypeFacts(operandType) & (1048576 /* Truthy */ | 2097152 /* Falsy */); - return facts === 1048576 /* Truthy */ ? falseType : - facts === 2097152 /* Falsy */ ? trueType : - booleanType; - case 41 /* PlusPlusToken */: - case 42 /* MinusMinusToken */: - var ok = checkArithmeticOperandType(node.operand, getNonNullableType(operandType), ts.Diagnostics.An_arithmetic_operand_must_be_of_type_any_number_or_an_enum_type); - if (ok) { - // run check only if former checks succeeded to avoid reporting cascading errors - checkReferenceExpression(node.operand, ts.Diagnostics.The_operand_of_an_increment_or_decrement_operator_must_be_a_variable_property_or_indexer, ts.Diagnostics.The_operand_of_an_increment_or_decrement_operator_cannot_be_a_constant_or_a_read_only_property); - } - return numberType; + else { + args = node.arguments || emptyArray; } - return unknownType; + return args; } - function checkPostfixUnaryExpression(node) { - var operandType = checkExpression(node.operand); - if (operandType === silentNeverType) { - return silentNeverType; + /** + * Returns the effective argument count for a node that works like a function invocation. + * If 'node' is a Decorator, the number of arguments is derived from the decoration + * target and the signature: + * If 'node.target' is a class declaration or class expression, the effective argument + * count is 1. + * If 'node.target' is a parameter declaration, the effective argument count is 3. + * If 'node.target' is a property declaration, the effective argument count is 2. + * If 'node.target' is a method or accessor declaration, the effective argument count + * is 3, although it can be 2 if the signature only accepts two arguments, allowing + * us to match a property decorator. + * Otherwise, the argument count is the length of the 'args' array. + */ + function getEffectiveArgumentCount(node, args, signature) { + if (node.kind === 143 /* Decorator */) { + switch (node.parent.kind) { + case 221 /* ClassDeclaration */: + case 192 /* ClassExpression */: + // A class decorator will have one argument (see `ClassDecorator` in core.d.ts) + return 1; + case 145 /* PropertyDeclaration */: + // A property declaration decorator will have two arguments (see + // `PropertyDecorator` in core.d.ts) + return 2; + case 147 /* MethodDeclaration */: + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + // A method or accessor declaration decorator will have two or three arguments (see + // `PropertyDecorator` and `MethodDecorator` in core.d.ts) + // If we are emitting decorators for ES3, we will only pass two arguments. + if (languageVersion === 0 /* ES3 */) { + return 2; + } + // If the method decorator signature only accepts a target and a key, we will only + // type check those arguments. + return signature.parameters.length >= 3 ? 3 : 2; + case 142 /* Parameter */: + // A parameter declaration decorator will have three arguments (see + // `ParameterDecorator` in core.d.ts) + return 3; + } } - var ok = checkArithmeticOperandType(node.operand, getNonNullableType(operandType), ts.Diagnostics.An_arithmetic_operand_must_be_of_type_any_number_or_an_enum_type); - if (ok) { - // run check only if former checks succeeded to avoid reporting cascading errors - checkReferenceExpression(node.operand, ts.Diagnostics.The_operand_of_an_increment_or_decrement_operator_must_be_a_variable_property_or_indexer, ts.Diagnostics.The_operand_of_an_increment_or_decrement_operator_cannot_be_a_constant_or_a_read_only_property); + else { + return args.length; } - return numberType; } - // Return true if type might be of the given kind. A union or intersection type might be of a given - // kind if at least one constituent type is of the given kind. - function maybeTypeOfKind(type, kind) { - if (type.flags & kind) { - return true; + /** + * Returns the effective type of the first argument to a decorator. + * If 'node' is a class declaration or class expression, the effective argument type + * is the type of the static side of the class. + * If 'node' is a parameter declaration, the effective argument type is either the type + * of the static or instance side of the class for the parameter's parent method, + * depending on whether the method is declared static. + * For a constructor, the type is always the type of the static side of the class. + * If 'node' is a property, method, or accessor declaration, the effective argument + * type is the type of the static or instance side of the parent class for class + * element, depending on whether the element is declared static. + */ + function getEffectiveDecoratorFirstArgumentType(node) { + // The first argument to a decorator is its `target`. + if (node.kind === 221 /* ClassDeclaration */) { + // For a class decorator, the `target` is the type of the class (e.g. the + // "static" or "constructor" side of the class) + var classSymbol = getSymbolOfNode(node); + return getTypeOfSymbol(classSymbol); } - if (type.flags & 1572864 /* UnionOrIntersection */) { - var types = type.types; - for (var _i = 0, types_14 = types; _i < types_14.length; _i++) { - var t = types_14[_i]; - if (maybeTypeOfKind(t, kind)) { - return true; - } + if (node.kind === 142 /* Parameter */) { + // For a parameter decorator, the `target` is the parent type of the + // parameter's containing method. + node = node.parent; + if (node.kind === 148 /* Constructor */) { + var classSymbol = getSymbolOfNode(node); + return getTypeOfSymbol(classSymbol); } } - return false; + if (node.kind === 145 /* PropertyDeclaration */ || + node.kind === 147 /* MethodDeclaration */ || + node.kind === 149 /* GetAccessor */ || + node.kind === 150 /* SetAccessor */) { + // For a property or method decorator, the `target` is the + // "static"-side type of the parent of the member if the member is + // declared "static"; otherwise, it is the "instance"-side type of the + // parent of the member. + return getParentTypeOfClassElement(node); + } + ts.Debug.fail("Unsupported decorator target."); + return unknownType; } - // Return true if type is of the given kind. A union type is of a given kind if all constituent types - // are of the given kind. An intersection type is of a given kind if at least one constituent type is - // of the given kind. - function isTypeOfKind(type, kind) { - if (type.flags & kind) { - return true; + /** + * Returns the effective type for the second argument to a decorator. + * If 'node' is a parameter, its effective argument type is one of the following: + * If 'node.parent' is a constructor, the effective argument type is 'any', as we + * will emit `undefined`. + * If 'node.parent' is a member with an identifier, numeric, or string literal name, + * the effective argument type will be a string literal type for the member name. + * If 'node.parent' is a computed property name, the effective argument type will + * either be a symbol type or the string type. + * If 'node' is a member with an identifier, numeric, or string literal name, the + * effective argument type will be a string literal type for the member name. + * If 'node' is a computed property name, the effective argument type will either + * be a symbol type or the string type. + * A class decorator does not have a second argument type. + */ + function getEffectiveDecoratorSecondArgumentType(node) { + // The second argument to a decorator is its `propertyKey` + if (node.kind === 221 /* ClassDeclaration */) { + ts.Debug.fail("Class decorators should not have a second synthetic argument."); + return unknownType; } - if (type.flags & 524288 /* Union */) { - var types = type.types; - for (var _i = 0, types_15 = types; _i < types_15.length; _i++) { - var t = types_15[_i]; - if (!isTypeOfKind(t, kind)) { - return false; - } + if (node.kind === 142 /* Parameter */) { + node = node.parent; + if (node.kind === 148 /* Constructor */) { + // For a constructor parameter decorator, the `propertyKey` will be `undefined`. + return anyType; } - return true; } - if (type.flags & 1048576 /* Intersection */) { - var types = type.types; - for (var _a = 0, types_16 = types; _a < types_16.length; _a++) { - var t = types_16[_a]; - if (isTypeOfKind(t, kind)) { - return true; - } + if (node.kind === 145 /* PropertyDeclaration */ || + node.kind === 147 /* MethodDeclaration */ || + node.kind === 149 /* GetAccessor */ || + node.kind === 150 /* SetAccessor */) { + // The `propertyKey` for a property or method decorator will be a + // string literal type if the member name is an identifier, number, or string; + // otherwise, if the member name is a computed property name it will + // be either string or symbol. + var element = node; + switch (element.name.kind) { + case 69 /* Identifier */: + case 8 /* NumericLiteral */: + case 9 /* StringLiteral */: + return getLiteralTypeForText(32 /* StringLiteral */, element.name.text); + case 140 /* ComputedPropertyName */: + var nameType = checkComputedPropertyName(element.name); + if (isTypeOfKind(nameType, 512 /* ESSymbol */)) { + return nameType; + } + else { + return stringType; + } + default: + ts.Debug.fail("Unsupported property name."); + return unknownType; } } - return false; - } - function isConstEnumObjectType(type) { - return type.flags & (2588672 /* ObjectType */ | 2097152 /* Anonymous */) && type.symbol && isConstEnumSymbol(type.symbol); - } - function isConstEnumSymbol(symbol) { - return (symbol.flags & 128 /* ConstEnum */) !== 0; + ts.Debug.fail("Unsupported decorator target."); + return unknownType; } - function checkInstanceOfExpression(left, right, leftType, rightType) { - if (leftType === silentNeverType || rightType === silentNeverType) { - return silentNeverType; - } - // TypeScript 1.0 spec (April 2014): 4.15.4 - // The instanceof operator requires the left operand to be of type Any, an object type, or a type parameter type, - // and the right operand to be of type Any or a subtype of the 'Function' interface type. - // The result is always of the Boolean primitive type. - // NOTE: do not raise error if leftType is unknown as related error was already reported - if (isTypeOfKind(leftType, 8190 /* Primitive */)) { - error(left, ts.Diagnostics.The_left_hand_side_of_an_instanceof_expression_must_be_of_type_any_an_object_type_or_a_type_parameter); - } - // NOTE: do not raise error if right is unknown as related error was already reported - if (!(isTypeAny(rightType) || isTypeSubtypeOf(rightType, globalFunctionType))) { - error(right, ts.Diagnostics.The_right_hand_side_of_an_instanceof_expression_must_be_of_type_any_or_of_a_type_assignable_to_the_Function_interface_type); + /** + * Returns the effective argument type for the third argument to a decorator. + * If 'node' is a parameter, the effective argument type is the number type. + * If 'node' is a method or accessor, the effective argument type is a + * `TypedPropertyDescriptor` instantiated with the type of the member. + * Class and property decorators do not have a third effective argument. + */ + function getEffectiveDecoratorThirdArgumentType(node) { + // The third argument to a decorator is either its `descriptor` for a method decorator + // or its `parameterIndex` for a parameter decorator + if (node.kind === 221 /* ClassDeclaration */) { + ts.Debug.fail("Class decorators should not have a third synthetic argument."); + return unknownType; } - return booleanType; - } - function checkInExpression(left, right, leftType, rightType) { - if (leftType === silentNeverType || rightType === silentNeverType) { - return silentNeverType; + if (node.kind === 142 /* Parameter */) { + // The `parameterIndex` for a parameter decorator is always a number + return numberType; } - // TypeScript 1.0 spec (April 2014): 4.15.5 - // The in operator requires the left operand to be of type Any, the String primitive type, or the Number primitive type, - // and the right operand to be of type Any, an object type, or a type parameter type. - // The result is always of the Boolean primitive type. - if (!isTypeAnyOrAllConstituentTypesHaveKind(leftType, 34 /* StringLike */ | 340 /* NumberLike */ | 512 /* ESSymbol */)) { - error(left, ts.Diagnostics.The_left_hand_side_of_an_in_expression_must_be_of_type_any_string_number_or_symbol); + if (node.kind === 145 /* PropertyDeclaration */) { + ts.Debug.fail("Property decorators should not have a third synthetic argument."); + return unknownType; } - if (!isTypeAnyOrAllConstituentTypesHaveKind(rightType, 2588672 /* ObjectType */ | 16384 /* TypeParameter */)) { - error(right, ts.Diagnostics.The_right_hand_side_of_an_in_expression_must_be_of_type_any_an_object_type_or_a_type_parameter); + if (node.kind === 147 /* MethodDeclaration */ || + node.kind === 149 /* GetAccessor */ || + node.kind === 150 /* SetAccessor */) { + // The `descriptor` for a method decorator will be a `TypedPropertyDescriptor` + // for the type of the member. + var propertyType = getTypeOfNode(node); + return createTypedPropertyDescriptorType(propertyType); } - return booleanType; + ts.Debug.fail("Unsupported decorator target."); + return unknownType; } - function checkObjectLiteralAssignment(node, sourceType, contextualMapper) { - var properties = node.properties; - for (var _i = 0, properties_4 = properties; _i < properties_4.length; _i++) { - var p = properties_4[_i]; - checkObjectLiteralDestructuringPropertyAssignment(sourceType, p, contextualMapper); + /** + * Returns the effective argument type for the provided argument to a decorator. + */ + function getEffectiveDecoratorArgumentType(node, argIndex) { + if (argIndex === 0) { + return getEffectiveDecoratorFirstArgumentType(node.parent); } - return sourceType; + else if (argIndex === 1) { + return getEffectiveDecoratorSecondArgumentType(node.parent); + } + else if (argIndex === 2) { + return getEffectiveDecoratorThirdArgumentType(node.parent); + } + ts.Debug.fail("Decorators should not have a fourth synthetic argument."); + return unknownType; } - function checkObjectLiteralDestructuringPropertyAssignment(objectLiteralType, property, contextualMapper) { - if (property.kind === 253 /* PropertyAssignment */ || property.kind === 254 /* ShorthandPropertyAssignment */) { - var name_17 = property.name; - if (name_17.kind === 140 /* ComputedPropertyName */) { - checkComputedPropertyName(name_17); - } - if (isComputedNonLiteralName(name_17)) { - return undefined; + /** + * Gets the effective argument type for an argument in a call expression. + */ + function getEffectiveArgumentType(node, argIndex, arg) { + // Decorators provide special arguments, a tagged template expression provides + // a special first argument, and string literals get string literal types + // unless we're reporting errors + if (node.kind === 143 /* Decorator */) { + return getEffectiveDecoratorArgumentType(node, argIndex); + } + else if (argIndex === 0 && node.kind === 176 /* TaggedTemplateExpression */) { + return getGlobalTemplateStringsArrayType(); + } + // This is not a synthetic argument, so we return 'undefined' + // to signal that the caller needs to check the argument. + return undefined; + } + /** + * Gets the effective argument expression for an argument in a call expression. + */ + function getEffectiveArgument(node, args, argIndex) { + // For a decorator or the first argument of a tagged template expression we return undefined. + if (node.kind === 143 /* Decorator */ || + (argIndex === 0 && node.kind === 176 /* TaggedTemplateExpression */)) { + return undefined; + } + return args[argIndex]; + } + /** + * Gets the error node to use when reporting errors for an effective argument. + */ + function getEffectiveArgumentErrorNode(node, argIndex, arg) { + if (node.kind === 143 /* Decorator */) { + // For a decorator, we use the expression of the decorator for error reporting. + return node.expression; + } + else if (argIndex === 0 && node.kind === 176 /* TaggedTemplateExpression */) { + // For a the first argument of a tagged template expression, we use the template of the tag for error reporting. + return node.template; + } + else { + return arg; + } + } + function resolveCall(node, signatures, candidatesOutArray, headMessage) { + var isTaggedTemplate = node.kind === 176 /* TaggedTemplateExpression */; + var isDecorator = node.kind === 143 /* Decorator */; + var typeArguments; + if (!isTaggedTemplate && !isDecorator) { + typeArguments = node.typeArguments; + // We already perform checking on the type arguments on the class declaration itself. + if (node.expression.kind !== 95 /* SuperKeyword */) { + ts.forEach(typeArguments, checkSourceElement); } - var text = getTextOfPropertyName(name_17); - var type = isTypeAny(objectLiteralType) - ? objectLiteralType - : getTypeOfPropertyOfType(objectLiteralType, text) || - isNumericLiteralName(text) && getIndexTypeOfType(objectLiteralType, 1 /* Number */) || - getIndexTypeOfType(objectLiteralType, 0 /* String */); - if (type) { - if (property.kind === 254 /* ShorthandPropertyAssignment */) { - return checkDestructuringAssignment(property, type); - } - else { - // non-shorthand property assignments should always have initializers - return checkDestructuringAssignment(property.initializer, type); + } + var candidates = candidatesOutArray || []; + // reorderCandidates fills up the candidates array directly + reorderCandidates(signatures, candidates); + if (!candidates.length) { + reportError(ts.Diagnostics.Supplied_parameters_do_not_match_any_signature_of_call_target); + return resolveErrorCall(node); + } + var args = getEffectiveCallArguments(node); + // The following applies to any value of 'excludeArgument[i]': + // - true: the argument at 'i' is susceptible to a one-time permanent contextual typing. + // - undefined: the argument at 'i' is *not* susceptible to permanent contextual typing. + // - false: the argument at 'i' *was* and *has been* permanently contextually typed. + // + // The idea is that we will perform type argument inference & assignability checking once + // without using the susceptible parameters that are functions, and once more for each of those + // parameters, contextually typing each as we go along. + // + // For a tagged template, then the first argument be 'undefined' if necessary + // because it represents a TemplateStringsArray. + // + // For a decorator, no arguments are susceptible to contextual typing due to the fact + // decorators are applied to a declaration by the emitter, and not to an expression. + var excludeArgument; + if (!isDecorator) { + // We do not need to call `getEffectiveArgumentCount` here as it only + // applies when calculating the number of arguments for a decorator. + for (var i = isTaggedTemplate ? 1 : 0; i < args.length; i++) { + if (isContextSensitive(args[i])) { + if (!excludeArgument) { + excludeArgument = new Array(args.length); + } + excludeArgument[i] = true; } } + } + // The following variables are captured and modified by calls to chooseOverload. + // If overload resolution or type argument inference fails, we want to report the + // best error possible. The best error is one which says that an argument was not + // assignable to a parameter. This implies that everything else about the overload + // was fine. So if there is any overload that is only incorrect because of an + // argument, we will report an error on that one. + // + // function foo(s: string) {} + // function foo(n: number) {} // Report argument error on this overload + // function foo() {} + // foo(true); + // + // If none of the overloads even made it that far, there are two possibilities. + // There was a problem with type arguments for some overload, in which case + // report an error on that. Or none of the overloads even had correct arity, + // in which case give an arity error. + // + // function foo(x: T, y: T) {} // Report type argument inference error + // function foo() {} + // foo(0, true); + // + var candidateForArgumentError; + var candidateForTypeArgumentError; + var resultOfFailedInference; + var result; + // If we are in signature help, a trailing comma indicates that we intend to provide another argument, + // so we will only accept overloads with arity at least 1 higher than the current number of provided arguments. + var signatureHelpTrailingComma = candidatesOutArray && node.kind === 174 /* CallExpression */ && node.arguments.hasTrailingComma; + // Section 4.12.1: + // if the candidate list contains one or more signatures for which the type of each argument + // expression is a subtype of each corresponding parameter type, the return type of the first + // of those signatures becomes the return type of the function call. + // Otherwise, the return type of the first signature in the candidate list becomes the return + // type of the function call. + // + // Whether the call is an error is determined by assignability of the arguments. The subtype pass + // is just important for choosing the best signature. So in the case where there is only one + // signature, the subtype pass is useless. So skipping it is an optimization. + if (candidates.length > 1) { + result = chooseOverload(candidates, subtypeRelation, signatureHelpTrailingComma); + } + if (!result) { + // Reinitialize these pointers for round two + candidateForArgumentError = undefined; + candidateForTypeArgumentError = undefined; + resultOfFailedInference = undefined; + result = chooseOverload(candidates, assignableRelation, signatureHelpTrailingComma); + } + if (result) { + return result; + } + // No signatures were applicable. Now report errors based on the last applicable signature with + // no arguments excluded from assignability checks. + // If candidate is undefined, it means that no candidates had a suitable arity. In that case, + // skip the checkApplicableSignature check. + if (candidateForArgumentError) { + // excludeArgument is undefined, in this case also equivalent to [undefined, undefined, ...] + // The importance of excludeArgument is to prevent us from typing function expression parameters + // in arguments too early. If possible, we'd like to only type them once we know the correct + // overload. However, this matters for the case where the call is correct. When the call is + // an error, we don't need to exclude any arguments, although it would cause no harm to do so. + checkApplicableSignature(node, args, candidateForArgumentError, assignableRelation, /*excludeArgument*/ undefined, /*reportErrors*/ true); + } + else if (candidateForTypeArgumentError) { + if (!isTaggedTemplate && !isDecorator && typeArguments) { + var typeArguments_2 = node.typeArguments; + checkTypeArguments(candidateForTypeArgumentError, typeArguments_2, ts.map(typeArguments_2, getTypeFromTypeNodeNoAlias), /*reportErrors*/ true, headMessage); + } else { - error(name_17, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(objectLiteralType), ts.declarationNameToString(name_17)); + ts.Debug.assert(resultOfFailedInference.failedTypeParameterIndex >= 0); + var failedTypeParameter = candidateForTypeArgumentError.typeParameters[resultOfFailedInference.failedTypeParameterIndex]; + var inferenceCandidates = getInferenceCandidates(resultOfFailedInference, resultOfFailedInference.failedTypeParameterIndex); + var diagnosticChainHead = ts.chainDiagnosticMessages(/*details*/ undefined, // details will be provided by call to reportNoCommonSupertypeError + ts.Diagnostics.The_type_argument_for_type_parameter_0_cannot_be_inferred_from_the_usage_Consider_specifying_the_type_arguments_explicitly, typeToString(failedTypeParameter)); + if (headMessage) { + diagnosticChainHead = ts.chainDiagnosticMessages(diagnosticChainHead, headMessage); + } + reportNoCommonSupertypeError(inferenceCandidates, node.expression || node.tag, diagnosticChainHead); } } else { - error(property, ts.Diagnostics.Property_assignment_expected); + reportError(ts.Diagnostics.Supplied_parameters_do_not_match_any_signature_of_call_target); } - } - function checkArrayLiteralAssignment(node, sourceType, contextualMapper) { - // This elementType will be used if the specific property corresponding to this index is not - // present (aka the tuple element property). This call also checks that the parentType is in - // fact an iterable or array (depending on target language). - var elementType = checkIteratedTypeOrElementType(sourceType, node, /*allowStringInput*/ false) || unknownType; - var elements = node.elements; - for (var i = 0; i < elements.length; i++) { - checkArrayLiteralDestructuringElementAssignment(node, sourceType, i, elementType, contextualMapper); + // No signature was applicable. We have already reported the errors for the invalid signature. + // If this is a type resolution session, e.g. Language Service, try to get better information that anySignature. + // Pick the first candidate that matches the arity. This way we can get a contextual type for cases like: + // declare function f(a: { xa: number; xb: number; }); + // f({ | + if (!produceDiagnostics) { + for (var _i = 0, candidates_1 = candidates; _i < candidates_1.length; _i++) { + var candidate = candidates_1[_i]; + if (hasCorrectArity(node, args, candidate)) { + if (candidate.typeParameters && typeArguments) { + candidate = getSignatureInstantiation(candidate, ts.map(typeArguments, getTypeFromTypeNodeNoAlias)); + } + return candidate; + } + } } - return sourceType; - } - function checkArrayLiteralDestructuringElementAssignment(node, sourceType, elementIndex, elementType, contextualMapper) { - var elements = node.elements; - var element = elements[elementIndex]; - if (element.kind !== 193 /* OmittedExpression */) { - if (element.kind !== 191 /* SpreadElementExpression */) { - var propName = "" + elementIndex; - var type = isTypeAny(sourceType) - ? sourceType - : isTupleLikeType(sourceType) - ? getTypeOfPropertyOfType(sourceType, propName) - : elementType; - if (type) { - return checkDestructuringAssignment(element, type, contextualMapper); + return resolveErrorCall(node); + function reportError(message, arg0, arg1, arg2) { + var errorInfo; + errorInfo = ts.chainDiagnosticMessages(errorInfo, message, arg0, arg1, arg2); + if (headMessage) { + errorInfo = ts.chainDiagnosticMessages(errorInfo, headMessage); + } + diagnostics.add(ts.createDiagnosticForNodeFromMessageChain(node, errorInfo)); + } + function chooseOverload(candidates, relation, signatureHelpTrailingComma) { + if (signatureHelpTrailingComma === void 0) { signatureHelpTrailingComma = false; } + for (var _i = 0, candidates_2 = candidates; _i < candidates_2.length; _i++) { + var originalCandidate = candidates_2[_i]; + if (!hasCorrectArity(node, args, originalCandidate, signatureHelpTrailingComma)) { + continue; } - else { - // We still need to check element expression here because we may need to set appropriate flag on the expression - // such as NodeCheckFlags.LexicalThis on "this"expression. - checkExpression(element); - if (isTupleType(sourceType)) { - error(element, ts.Diagnostics.Tuple_type_0_with_length_1_cannot_be_assigned_to_tuple_with_length_2, typeToString(sourceType), getTypeReferenceArity(sourceType), elements.length); + var candidate = void 0; + var typeArgumentsAreValid = void 0; + var inferenceContext = originalCandidate.typeParameters + ? createInferenceContext(originalCandidate, /*inferUnionTypes*/ false) + : undefined; + while (true) { + candidate = originalCandidate; + if (candidate.typeParameters) { + var typeArgumentTypes = void 0; + if (typeArguments) { + typeArgumentTypes = ts.map(typeArguments, getTypeFromTypeNodeNoAlias); + typeArgumentsAreValid = checkTypeArguments(candidate, typeArguments, typeArgumentTypes, /*reportErrors*/ false); + } + else { + inferTypeArguments(node, candidate, args, excludeArgument, inferenceContext); + typeArgumentsAreValid = inferenceContext.failedTypeParameterIndex === undefined; + typeArgumentTypes = inferenceContext.inferredTypes; + } + if (!typeArgumentsAreValid) { + break; + } + candidate = getSignatureInstantiation(candidate, typeArgumentTypes); } - else { - error(element, ts.Diagnostics.Type_0_has_no_property_1, typeToString(sourceType), propName); + if (!checkApplicableSignature(node, args, candidate, relation, excludeArgument, /*reportErrors*/ false)) { + break; } + var index = excludeArgument ? ts.indexOf(excludeArgument, true) : -1; + if (index < 0) { + return candidate; + } + excludeArgument[index] = false; } - } - else { - if (elementIndex < elements.length - 1) { - error(element, ts.Diagnostics.A_rest_element_must_be_last_in_an_array_destructuring_pattern); - } - else { - var restExpression = element.expression; - if (restExpression.kind === 187 /* BinaryExpression */ && restExpression.operatorToken.kind === 56 /* EqualsToken */) { - error(restExpression.operatorToken, ts.Diagnostics.A_rest_element_cannot_have_an_initializer); + // A post-mortem of this iteration of the loop. The signature was not applicable, + // so we want to track it as a candidate for reporting an error. If the candidate + // had no type parameters, or had no issues related to type arguments, we can + // report an error based on the arguments. If there was an issue with type + // arguments, then we can only report an error based on the type arguments. + if (originalCandidate.typeParameters) { + var instantiatedCandidate = candidate; + if (typeArgumentsAreValid) { + candidateForArgumentError = instantiatedCandidate; } else { - return checkDestructuringAssignment(restExpression, createArrayType(elementType), contextualMapper); + candidateForTypeArgumentError = originalCandidate; + if (!typeArguments) { + resultOfFailedInference = inferenceContext; + } } } + else { + ts.Debug.assert(originalCandidate === candidate); + candidateForArgumentError = originalCandidate; + } } + return undefined; } - return undefined; } - function checkDestructuringAssignment(exprOrAssignment, sourceType, contextualMapper) { - var target; - if (exprOrAssignment.kind === 254 /* ShorthandPropertyAssignment */) { - var prop = exprOrAssignment; - if (prop.objectAssignmentInitializer) { - // In strict null checking mode, if a default value of a non-undefined type is specified, remove - // undefined from the final type. - if (strictNullChecks && - !(getFalsyFlags(checkExpression(prop.objectAssignmentInitializer)) & 2048 /* Undefined */)) { - sourceType = getTypeWithFacts(sourceType, 131072 /* NEUndefined */); + function resolveCallExpression(node, candidatesOutArray) { + if (node.expression.kind === 95 /* SuperKeyword */) { + var superType = checkSuperExpression(node.expression); + if (superType !== unknownType) { + // In super call, the candidate signatures are the matching arity signatures of the base constructor function instantiated + // with the type arguments specified in the extends clause. + var baseTypeNode = ts.getClassExtendsHeritageClauseElement(ts.getContainingClass(node)); + if (baseTypeNode) { + var baseConstructors = getInstantiatedConstructorsForTypeArguments(superType, baseTypeNode.typeArguments); + return resolveCall(node, baseConstructors, candidatesOutArray); } - checkBinaryLikeExpression(prop.name, prop.equalsToken, prop.objectAssignmentInitializer, contextualMapper); } - target = exprOrAssignment.name; - } - else { - target = exprOrAssignment; + return resolveUntypedCall(node); } - if (target.kind === 187 /* BinaryExpression */ && target.operatorToken.kind === 56 /* EqualsToken */) { - checkBinaryExpression(target, contextualMapper); - target = target.left; + var funcType = checkNonNullExpression(node.expression); + if (funcType === silentNeverType) { + return silentNeverSignature; } - if (target.kind === 171 /* ObjectLiteralExpression */) { - return checkObjectLiteralAssignment(target, sourceType, contextualMapper); + var apparentType = getApparentType(funcType); + if (apparentType === unknownType) { + // Another error has already been reported + return resolveErrorCall(node); } - if (target.kind === 170 /* ArrayLiteralExpression */) { - return checkArrayLiteralAssignment(target, sourceType, contextualMapper); + // Technically, this signatures list may be incomplete. We are taking the apparent type, + // but we are not including call signatures that may have been added to the Object or + // Function interface, since they have none by default. This is a bit of a leap of faith + // that the user will not add any. + var callSignatures = getSignaturesOfType(apparentType, 0 /* Call */); + var constructSignatures = getSignaturesOfType(apparentType, 1 /* Construct */); + // TS 1.0 Spec: 4.12 + // In an untyped function call no TypeArgs are permitted, Args can be any argument list, no contextual + // types are provided for the argument expressions, and the result is always of type Any. + if (isUntypedFunctionCall(funcType, apparentType, callSignatures.length, constructSignatures.length)) { + // The unknownType indicates that an error already occurred (and was reported). No + // need to report another error in this case. + if (funcType !== unknownType && node.typeArguments) { + error(node, ts.Diagnostics.Untyped_function_calls_may_not_accept_type_arguments); + } + return resolveUntypedCall(node); } - return checkReferenceAssignment(target, sourceType, contextualMapper); - } - function checkReferenceAssignment(target, sourceType, contextualMapper) { - var targetType = checkExpression(target, contextualMapper); - if (checkReferenceExpression(target, ts.Diagnostics.Invalid_left_hand_side_of_assignment_expression, ts.Diagnostics.Left_hand_side_of_assignment_expression_cannot_be_a_constant_or_a_read_only_property)) { - checkTypeAssignableTo(sourceType, targetType, target, /*headMessage*/ undefined); + // If FuncExpr's apparent type(section 3.8.1) is a function type, the call is a typed function call. + // TypeScript employs overload resolution in typed function calls in order to support functions + // with multiple call signatures. + if (!callSignatures.length) { + if (constructSignatures.length) { + error(node, ts.Diagnostics.Value_of_type_0_is_not_callable_Did_you_mean_to_include_new, typeToString(funcType)); + } + else { + error(node, ts.Diagnostics.Cannot_invoke_an_expression_whose_type_lacks_a_call_signature_Type_0_has_no_compatible_call_signatures, typeToString(apparentType)); + } + return resolveErrorCall(node); } - return sourceType; + return resolveCall(node, callSignatures, candidatesOutArray); } /** - * This is a *shallow* check: An expression is side-effect-free if the - * evaluation of the expression *itself* cannot produce side effects. - * For example, x++ / 3 is side-effect free because the / operator - * does not have side effects. - * The intent is to "smell test" an expression for correctness in positions where - * its value is discarded (e.g. the left side of the comma operator). + * TS 1.0 spec: 4.12 + * If FuncExpr is of type Any, or of an object type that has no call or construct signatures + * but is a subtype of the Function interface, the call is an untyped function call. */ - function isSideEffectFree(node) { - node = ts.skipParentheses(node); - switch (node.kind) { - case 69 /* Identifier */: - case 9 /* StringLiteral */: - case 10 /* RegularExpressionLiteral */: - case 176 /* TaggedTemplateExpression */: - case 189 /* TemplateExpression */: - case 11 /* NoSubstitutionTemplateLiteral */: - case 8 /* NumericLiteral */: - case 99 /* TrueKeyword */: - case 84 /* FalseKeyword */: - case 93 /* NullKeyword */: - case 135 /* UndefinedKeyword */: - case 179 /* FunctionExpression */: - case 192 /* ClassExpression */: - case 180 /* ArrowFunction */: - case 170 /* ArrayLiteralExpression */: - case 171 /* ObjectLiteralExpression */: - case 182 /* TypeOfExpression */: - case 196 /* NonNullExpression */: - case 242 /* JsxSelfClosingElement */: - case 241 /* JsxElement */: - return true; - case 188 /* ConditionalExpression */: - return isSideEffectFree(node.whenTrue) && - isSideEffectFree(node.whenFalse); - case 187 /* BinaryExpression */: - if (ts.isAssignmentOperator(node.operatorToken.kind)) { - return false; - } - return isSideEffectFree(node.left) && - isSideEffectFree(node.right); - case 185 /* PrefixUnaryExpression */: - case 186 /* PostfixUnaryExpression */: - // Unary operators ~, !, +, and - have no side effects. - // The rest do. - switch (node.operator) { - case 49 /* ExclamationToken */: - case 35 /* PlusToken */: - case 36 /* MinusToken */: - case 50 /* TildeToken */: - return true; - } - return false; - // Some forms listed here for clarity - case 183 /* VoidExpression */: // Explicit opt-out - case 177 /* TypeAssertionExpression */: // Not SEF, but can produce useful type warnings - case 195 /* AsExpression */: // Not SEF, but can produce useful type warnings - default: + function isUntypedFunctionCall(funcType, apparentFuncType, numCallSignatures, numConstructSignatures) { + if (isTypeAny(funcType)) { + return true; + } + if (isTypeAny(apparentFuncType) && funcType.flags & 16384 /* TypeParameter */) { + return true; + } + if (!numCallSignatures && !numConstructSignatures) { + // We exclude union types because we may have a union of function types that happen to have + // no common signatures. + if (funcType.flags & 524288 /* Union */) { return false; + } + return isTypeAssignableTo(funcType, globalFunctionType); } + return false; } - function isTypeEqualityComparableTo(source, target) { - return (target.flags & 6144 /* Nullable */) !== 0 || isTypeComparableTo(source, target); - } - function getBestChoiceType(type1, type2) { - var firstAssignableToSecond = isTypeAssignableTo(type1, type2); - var secondAssignableToFirst = isTypeAssignableTo(type2, type1); - return secondAssignableToFirst && !firstAssignableToSecond ? type1 : - firstAssignableToSecond && !secondAssignableToFirst ? type2 : - getUnionType([type1, type2], /*subtypeReduction*/ true); - } - function checkBinaryExpression(node, contextualMapper) { - return checkBinaryLikeExpression(node.left, node.operatorToken, node.right, contextualMapper, node); - } - function checkBinaryLikeExpression(left, operatorToken, right, contextualMapper, errorNode) { - var operator = operatorToken.kind; - if (operator === 56 /* EqualsToken */ && (left.kind === 171 /* ObjectLiteralExpression */ || left.kind === 170 /* ArrayLiteralExpression */)) { - return checkDestructuringAssignment(left, checkExpression(right, contextualMapper), contextualMapper); + function resolveNewExpression(node, candidatesOutArray) { + if (node.arguments && languageVersion < 1 /* ES5 */) { + var spreadIndex = getSpreadArgumentIndex(node.arguments); + if (spreadIndex >= 0) { + error(node.arguments[spreadIndex], ts.Diagnostics.Spread_operator_in_new_expressions_is_only_available_when_targeting_ECMAScript_5_and_higher); + } } - var leftType = checkExpression(left, contextualMapper); - var rightType = checkExpression(right, contextualMapper); - switch (operator) { - case 37 /* AsteriskToken */: - case 38 /* AsteriskAsteriskToken */: - case 59 /* AsteriskEqualsToken */: - case 60 /* AsteriskAsteriskEqualsToken */: - case 39 /* SlashToken */: - case 61 /* SlashEqualsToken */: - case 40 /* PercentToken */: - case 62 /* PercentEqualsToken */: - case 36 /* MinusToken */: - case 58 /* MinusEqualsToken */: - case 43 /* LessThanLessThanToken */: - case 63 /* LessThanLessThanEqualsToken */: - case 44 /* GreaterThanGreaterThanToken */: - case 64 /* GreaterThanGreaterThanEqualsToken */: - case 45 /* GreaterThanGreaterThanGreaterThanToken */: - case 65 /* GreaterThanGreaterThanGreaterThanEqualsToken */: - case 47 /* BarToken */: - case 67 /* BarEqualsToken */: - case 48 /* CaretToken */: - case 68 /* CaretEqualsToken */: - case 46 /* AmpersandToken */: - case 66 /* AmpersandEqualsToken */: - if (leftType === silentNeverType || rightType === silentNeverType) { - return silentNeverType; - } - // TypeScript 1.0 spec (April 2014): 4.19.1 - // These operators require their operands to be of type Any, the Number primitive type, - // or an enum type. Operands of an enum type are treated - // as having the primitive type Number. If one operand is the null or undefined value, - // it is treated as having the type of the other operand. - // The result is always of the Number primitive type. - if (leftType.flags & 6144 /* Nullable */) - leftType = rightType; - if (rightType.flags & 6144 /* Nullable */) - rightType = leftType; - leftType = getNonNullableType(leftType); - rightType = getNonNullableType(rightType); - var suggestedOperator = void 0; - // if a user tries to apply a bitwise operator to 2 boolean operands - // try and return them a helpful suggestion - if ((leftType.flags & 136 /* BooleanLike */) && - (rightType.flags & 136 /* BooleanLike */) && - (suggestedOperator = getSuggestedBooleanOperator(operatorToken.kind)) !== undefined) { - error(errorNode || operatorToken, ts.Diagnostics.The_0_operator_is_not_allowed_for_boolean_types_Consider_using_1_instead, ts.tokenToString(operatorToken.kind), ts.tokenToString(suggestedOperator)); - } - else { - // otherwise just check each operand separately and report errors as normal - var leftOk = checkArithmeticOperandType(left, leftType, ts.Diagnostics.The_left_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_or_an_enum_type); - var rightOk = checkArithmeticOperandType(right, rightType, ts.Diagnostics.The_right_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_or_an_enum_type); - if (leftOk && rightOk) { - checkAssignmentOperator(numberType); - } - } - return numberType; - case 35 /* PlusToken */: - case 57 /* PlusEqualsToken */: - if (leftType === silentNeverType || rightType === silentNeverType) { - return silentNeverType; - } - // TypeScript 1.0 spec (April 2014): 4.19.2 - // The binary + operator requires both operands to be of the Number primitive type or an enum type, - // or at least one of the operands to be of type Any or the String primitive type. - // If one operand is the null or undefined value, it is treated as having the type of the other operand. - if (leftType.flags & 6144 /* Nullable */) - leftType = rightType; - if (rightType.flags & 6144 /* Nullable */) - rightType = leftType; - leftType = getNonNullableType(leftType); - rightType = getNonNullableType(rightType); - var resultType = void 0; - if (isTypeOfKind(leftType, 340 /* NumberLike */) && isTypeOfKind(rightType, 340 /* NumberLike */)) { - // Operands of an enum type are treated as having the primitive type Number. - // If both operands are of the Number primitive type, the result is of the Number primitive type. - resultType = numberType; - } - else { - if (isTypeOfKind(leftType, 34 /* StringLike */) || isTypeOfKind(rightType, 34 /* StringLike */)) { - // If one or both operands are of the String primitive type, the result is of the String primitive type. - resultType = stringType; - } - else if (isTypeAny(leftType) || isTypeAny(rightType)) { - // Otherwise, the result is of type Any. - // NOTE: unknown type here denotes error type. Old compiler treated this case as any type so do we. - resultType = leftType === unknownType || rightType === unknownType ? unknownType : anyType; - } - // Symbols are not allowed at all in arithmetic expressions - if (resultType && !checkForDisallowedESSymbolOperand(operator)) { - return resultType; - } - } - if (!resultType) { - reportOperatorError(); - return anyType; - } - if (operator === 57 /* PlusEqualsToken */) { - checkAssignmentOperator(resultType); - } - return resultType; - case 25 /* LessThanToken */: - case 27 /* GreaterThanToken */: - case 28 /* LessThanEqualsToken */: - case 29 /* GreaterThanEqualsToken */: - if (checkForDisallowedESSymbolOperand(operator)) { - if (!isTypeComparableTo(leftType, rightType) && !isTypeComparableTo(rightType, leftType)) { - reportOperatorError(); - } - } - return booleanType; - case 30 /* EqualsEqualsToken */: - case 31 /* ExclamationEqualsToken */: - case 32 /* EqualsEqualsEqualsToken */: - case 33 /* ExclamationEqualsEqualsToken */: - var leftIsLiteral = isLiteralType(leftType); - var rightIsLiteral = isLiteralType(rightType); - if (!leftIsLiteral || !rightIsLiteral) { - leftType = leftIsLiteral ? getBaseTypeOfLiteralType(leftType) : leftType; - rightType = rightIsLiteral ? getBaseTypeOfLiteralType(rightType) : rightType; - } - if (!isTypeEqualityComparableTo(leftType, rightType) && !isTypeEqualityComparableTo(rightType, leftType)) { - reportOperatorError(); - } - return booleanType; - case 91 /* InstanceOfKeyword */: - return checkInstanceOfExpression(left, right, leftType, rightType); - case 90 /* InKeyword */: - return checkInExpression(left, right, leftType, rightType); - case 51 /* AmpersandAmpersandToken */: - return getTypeFacts(leftType) & 1048576 /* Truthy */ ? - includeFalsyTypes(rightType, getFalsyFlags(strictNullChecks ? leftType : getBaseTypeOfLiteralType(rightType))) : - leftType; - case 52 /* BarBarToken */: - return getTypeFacts(leftType) & 2097152 /* Falsy */ ? - getBestChoiceType(removeDefinitelyFalsyTypes(leftType), rightType) : - leftType; - case 56 /* EqualsToken */: - checkAssignmentOperator(rightType); - return getRegularTypeOfObjectLiteral(rightType); - case 24 /* CommaToken */: - if (!compilerOptions.allowUnreachableCode && isSideEffectFree(left)) { - error(left, ts.Diagnostics.Left_side_of_comma_operator_is_unused_and_has_no_side_effects); - } - return rightType; + var expressionType = checkNonNullExpression(node.expression); + if (expressionType === silentNeverType) { + return silentNeverSignature; } - // Return true if there was no error, false if there was an error. - function checkForDisallowedESSymbolOperand(operator) { - var offendingSymbolOperand = maybeTypeOfKind(leftType, 512 /* ESSymbol */) ? left : - maybeTypeOfKind(rightType, 512 /* ESSymbol */) ? right : - undefined; - if (offendingSymbolOperand) { - error(offendingSymbolOperand, ts.Diagnostics.The_0_operator_cannot_be_applied_to_type_symbol, ts.tokenToString(operator)); - return false; - } - return true; + // If expressionType's apparent type(section 3.8.1) is an object type with one or + // more construct signatures, the expression is processed in the same manner as a + // function call, but using the construct signatures as the initial set of candidate + // signatures for overload resolution. The result type of the function call becomes + // the result type of the operation. + expressionType = getApparentType(expressionType); + if (expressionType === unknownType) { + // Another error has already been reported + return resolveErrorCall(node); } - function getSuggestedBooleanOperator(operator) { - switch (operator) { - case 47 /* BarToken */: - case 67 /* BarEqualsToken */: - return 52 /* BarBarToken */; - case 48 /* CaretToken */: - case 68 /* CaretEqualsToken */: - return 33 /* ExclamationEqualsEqualsToken */; - case 46 /* AmpersandToken */: - case 66 /* AmpersandEqualsToken */: - return 51 /* AmpersandAmpersandToken */; - default: - return undefined; - } + // If the expression is a class of abstract type, then it cannot be instantiated. + // Note, only class declarations can be declared abstract. + // In the case of a merged class-module or class-interface declaration, + // only the class declaration node will have the Abstract flag set. + var valueDecl = expressionType.symbol && getClassLikeDeclarationOfSymbol(expressionType.symbol); + if (valueDecl && ts.getModifierFlags(valueDecl) & 128 /* Abstract */) { + error(node, ts.Diagnostics.Cannot_create_an_instance_of_the_abstract_class_0, ts.declarationNameToString(valueDecl.name)); + return resolveErrorCall(node); } - function checkAssignmentOperator(valueType) { - if (produceDiagnostics && operator >= 56 /* FirstAssignment */ && operator <= 68 /* LastAssignment */) { - // TypeScript 1.0 spec (April 2014): 4.17 - // An assignment of the form - // VarExpr = ValueExpr - // requires VarExpr to be classified as a reference - // A compound assignment furthermore requires VarExpr to be classified as a reference (section 4.1) - // and the type of the non - compound operation to be assignable to the type of VarExpr. - var ok = checkReferenceExpression(left, ts.Diagnostics.Invalid_left_hand_side_of_assignment_expression, ts.Diagnostics.Left_hand_side_of_assignment_expression_cannot_be_a_constant_or_a_read_only_property); - // Use default messages - if (ok) { - // to avoid cascading errors check assignability only if 'isReference' check succeeded and no errors were reported - checkTypeAssignableTo(valueType, leftType, left, /*headMessage*/ undefined); - } + // TS 1.0 spec: 4.11 + // If expressionType is of type Any, Args can be any argument + // list and the result of the operation is of type Any. + if (isTypeAny(expressionType)) { + if (node.typeArguments) { + error(node, ts.Diagnostics.Untyped_function_calls_may_not_accept_type_arguments); } + return resolveUntypedCall(node); } - function reportOperatorError() { - error(errorNode || operatorToken, ts.Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2, ts.tokenToString(operatorToken.kind), typeToString(leftType), typeToString(rightType)); + // Technically, this signatures list may be incomplete. We are taking the apparent type, + // but we are not including construct signatures that may have been added to the Object or + // Function interface, since they have none by default. This is a bit of a leap of faith + // that the user will not add any. + var constructSignatures = getSignaturesOfType(expressionType, 1 /* Construct */); + if (constructSignatures.length) { + if (!isConstructorAccessible(node, constructSignatures[0])) { + return resolveErrorCall(node); + } + return resolveCall(node, constructSignatures, candidatesOutArray); } - } - function isYieldExpressionInClass(node) { - var current = node; - var parent = node.parent; - while (parent) { - if (ts.isFunctionLike(parent) && current === parent.body) { - return false; + // If expressionType's apparent type is an object type with no construct signatures but + // one or more call signatures, the expression is processed as a function call. A compile-time + // error occurs if the result of the function call is not Void. The type of the result of the + // operation is Any. It is an error to have a Void this type. + var callSignatures = getSignaturesOfType(expressionType, 0 /* Call */); + if (callSignatures.length) { + var signature = resolveCall(node, callSignatures, candidatesOutArray); + if (getReturnTypeOfSignature(signature) !== voidType) { + error(node, ts.Diagnostics.Only_a_void_function_can_be_called_with_the_new_keyword); } - else if (ts.isClassLike(current)) { - return true; + if (getThisTypeOfSignature(signature) === voidType) { + error(node, ts.Diagnostics.A_function_that_is_called_with_the_new_keyword_cannot_have_a_this_type_that_is_void); } - current = parent; - parent = parent.parent; + return signature; } - return false; + error(node, ts.Diagnostics.Cannot_use_new_with_an_expression_whose_type_lacks_a_call_or_construct_signature); + return resolveErrorCall(node); } - function checkYieldExpression(node) { - // Grammar checking - if (produceDiagnostics) { - if (!(node.flags & 65536 /* YieldContext */) || isYieldExpressionInClass(node)) { - grammarErrorOnFirstToken(node, ts.Diagnostics.A_yield_expression_is_only_allowed_in_a_generator_body); - } - if (isInParameterInitializerBeforeContainingFunction(node)) { - error(node, ts.Diagnostics.yield_expressions_cannot_be_used_in_a_parameter_initializer); - } + function isConstructorAccessible(node, signature) { + if (!signature || !signature.declaration) { + return true; } - if (node.expression) { - var func = ts.getContainingFunction(node); - // If the user's code is syntactically correct, the func should always have a star. After all, - // we are in a yield context. - if (func && func.asteriskToken) { - var expressionType = checkExpressionCached(node.expression, /*contextualMapper*/ undefined); - var expressionElementType = void 0; - var nodeIsYieldStar = !!node.asteriskToken; - if (nodeIsYieldStar) { - expressionElementType = checkElementTypeOfIterable(expressionType, node.expression); - } - // There is no point in doing an assignability check if the function - // has no explicit return type because the return type is directly computed - // from the yield expressions. - if (func.type) { - var signatureElementType = getElementTypeOfIterableIterator(getTypeFromTypeNode(func.type)) || anyType; - if (nodeIsYieldStar) { - checkTypeAssignableTo(expressionElementType, signatureElementType, node.expression, /*headMessage*/ undefined); - } - else { - checkTypeAssignableTo(expressionType, signatureElementType, node.expression, /*headMessage*/ undefined); + var declaration = signature.declaration; + var modifiers = ts.getModifierFlags(declaration); + // Public constructor is accessible. + if (!(modifiers & 24 /* NonPublicAccessibilityModifier */)) { + return true; + } + var declaringClassDeclaration = getClassLikeDeclarationOfSymbol(declaration.parent.symbol); + var declaringClass = getDeclaredTypeOfSymbol(declaration.parent.symbol); + // A private or protected constructor can only be instantiated within its own class (or a subclass, for protected) + if (!isNodeWithinClass(node, declaringClassDeclaration)) { + var containingClass = ts.getContainingClass(node); + if (containingClass) { + var containingType = getTypeOfNode(containingClass); + var baseTypes = getBaseTypes(containingType); + if (baseTypes.length) { + var baseType = baseTypes[0]; + if (modifiers & 16 /* Protected */ && + baseType.symbol === declaration.parent.symbol) { + return true; } } } + if (modifiers & 8 /* Private */) { + error(node, ts.Diagnostics.Constructor_of_class_0_is_private_and_only_accessible_within_the_class_declaration, typeToString(declaringClass)); + } + if (modifiers & 16 /* Protected */) { + error(node, ts.Diagnostics.Constructor_of_class_0_is_protected_and_only_accessible_within_the_class_declaration, typeToString(declaringClass)); + } + return false; } - // Both yield and yield* expressions have type 'any' - return anyType; - } - function checkConditionalExpression(node, contextualMapper) { - checkExpression(node.condition); - var type1 = checkExpression(node.whenTrue, contextualMapper); - var type2 = checkExpression(node.whenFalse, contextualMapper); - return getBestChoiceType(type1, type2); + return true; } - function checkLiteralExpression(node) { - if (node.kind === 8 /* NumericLiteral */) { - checkGrammarNumericLiteral(node); + function resolveTaggedTemplateExpression(node, candidatesOutArray) { + var tagType = checkExpression(node.tag); + var apparentType = getApparentType(tagType); + if (apparentType === unknownType) { + // Another error has already been reported + return resolveErrorCall(node); } - switch (node.kind) { - case 9 /* StringLiteral */: - return getFreshTypeOfLiteralType(getLiteralTypeForText(32 /* StringLiteral */, node.text)); - case 8 /* NumericLiteral */: - return getFreshTypeOfLiteralType(getLiteralTypeForText(64 /* NumberLiteral */, node.text)); - case 99 /* TrueKeyword */: - return trueType; - case 84 /* FalseKeyword */: - return falseType; + var callSignatures = getSignaturesOfType(apparentType, 0 /* Call */); + var constructSignatures = getSignaturesOfType(apparentType, 1 /* Construct */); + if (isUntypedFunctionCall(tagType, apparentType, callSignatures.length, constructSignatures.length)) { + return resolveUntypedCall(node); + } + if (!callSignatures.length) { + error(node, ts.Diagnostics.Cannot_invoke_an_expression_whose_type_lacks_a_call_signature_Type_0_has_no_compatible_call_signatures, typeToString(apparentType)); + return resolveErrorCall(node); } + return resolveCall(node, callSignatures, candidatesOutArray); } - function checkTemplateExpression(node) { - // We just want to check each expressions, but we are unconcerned with - // the type of each expression, as any value may be coerced into a string. - // It is worth asking whether this is what we really want though. - // A place where we actually *are* concerned with the expressions' types are - // in tagged templates. - ts.forEach(node.templateSpans, function (templateSpan) { - checkExpression(templateSpan.expression); - }); - return stringType; + /** + * Gets the localized diagnostic head message to use for errors when resolving a decorator as a call expression. + */ + function getDiagnosticHeadMessageForDecoratorResolution(node) { + switch (node.parent.kind) { + case 221 /* ClassDeclaration */: + case 192 /* ClassExpression */: + return ts.Diagnostics.Unable_to_resolve_signature_of_class_decorator_when_called_as_an_expression; + case 142 /* Parameter */: + return ts.Diagnostics.Unable_to_resolve_signature_of_parameter_decorator_when_called_as_an_expression; + case 145 /* PropertyDeclaration */: + return ts.Diagnostics.Unable_to_resolve_signature_of_property_decorator_when_called_as_an_expression; + case 147 /* MethodDeclaration */: + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + return ts.Diagnostics.Unable_to_resolve_signature_of_method_decorator_when_called_as_an_expression; + } } - function checkExpressionWithContextualType(node, contextualType, contextualMapper) { - var saveContextualType = node.contextualType; - node.contextualType = contextualType; - var result = checkExpression(node, contextualMapper); - node.contextualType = saveContextualType; - return result; + /** + * Resolves a decorator as if it were a call expression. + */ + function resolveDecorator(node, candidatesOutArray) { + var funcType = checkExpression(node.expression); + var apparentType = getApparentType(funcType); + if (apparentType === unknownType) { + return resolveErrorCall(node); + } + var callSignatures = getSignaturesOfType(apparentType, 0 /* Call */); + var constructSignatures = getSignaturesOfType(apparentType, 1 /* Construct */); + if (isUntypedFunctionCall(funcType, apparentType, callSignatures.length, constructSignatures.length)) { + return resolveUntypedCall(node); + } + var headMessage = getDiagnosticHeadMessageForDecoratorResolution(node); + if (!callSignatures.length) { + var errorInfo = void 0; + errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Cannot_invoke_an_expression_whose_type_lacks_a_call_signature_Type_0_has_no_compatible_call_signatures, typeToString(apparentType)); + errorInfo = ts.chainDiagnosticMessages(errorInfo, headMessage); + diagnostics.add(ts.createDiagnosticForNodeFromMessageChain(node, errorInfo)); + return resolveErrorCall(node); + } + return resolveCall(node, callSignatures, candidatesOutArray, headMessage); } - function checkExpressionCached(node, contextualMapper) { + function resolveSignature(node, candidatesOutArray) { + switch (node.kind) { + case 174 /* CallExpression */: + return resolveCallExpression(node, candidatesOutArray); + case 175 /* NewExpression */: + return resolveNewExpression(node, candidatesOutArray); + case 176 /* TaggedTemplateExpression */: + return resolveTaggedTemplateExpression(node, candidatesOutArray); + case 143 /* Decorator */: + return resolveDecorator(node, candidatesOutArray); + } + ts.Debug.fail("Branch in 'resolveSignature' should be unreachable."); + } + // candidatesOutArray is passed by signature help in the language service, and collectCandidates + // must fill it up with the appropriate candidate signatures + function getResolvedSignature(node, candidatesOutArray) { var links = getNodeLinks(node); - if (!links.resolvedType) { - // When computing a type that we're going to cache, we need to ignore any ongoing control flow - // analysis because variables may have transient types in indeterminable states. Moving flowLoopStart - // to the top of the stack ensures all transient types are computed from a known point. - var saveFlowLoopStart = flowLoopStart; - flowLoopStart = flowLoopCount; - links.resolvedType = checkExpression(node, contextualMapper); - flowLoopStart = saveFlowLoopStart; + // If getResolvedSignature has already been called, we will have cached the resolvedSignature. + // However, it is possible that either candidatesOutArray was not passed in the first time, + // or that a different candidatesOutArray was passed in. Therefore, we need to redo the work + // to correctly fill the candidatesOutArray. + var cached = links.resolvedSignature; + if (cached && cached !== resolvingSignature && !candidatesOutArray) { + return cached; } - return links.resolvedType; + links.resolvedSignature = resolvingSignature; + var result = resolveSignature(node, candidatesOutArray); + // If signature resolution originated in control flow type analysis (for example to compute the + // assigned type in a flow assignment) we don't cache the result as it may be based on temporary + // types from the control flow analysis. + links.resolvedSignature = flowLoopStart === flowLoopCount ? result : cached; + return result; } - function isTypeAssertion(node) { - node = skipParenthesizedNodes(node); - return node.kind === 177 /* TypeAssertionExpression */ || node.kind === 195 /* AsExpression */; + function getResolvedOrAnySignature(node) { + // If we're already in the process of resolving the given signature, don't resolve again as + // that could cause infinite recursion. Instead, return anySignature. + return getNodeLinks(node).resolvedSignature === resolvingSignature ? resolvingSignature : getResolvedSignature(node); } - function checkDeclarationInitializer(declaration) { - var type = checkExpressionCached(declaration.initializer); - return ts.getCombinedNodeFlags(declaration) & 2 /* Const */ || - ts.getCombinedModifierFlags(declaration) & 64 /* Readonly */ || - isTypeAssertion(declaration.initializer) ? type : getWidenedLiteralType(type); + function getInferredClassType(symbol) { + var links = getSymbolLinks(symbol); + if (!links.inferredClassType) { + links.inferredClassType = createAnonymousType(symbol, symbol.members, emptyArray, emptyArray, /*stringIndexType*/ undefined, /*numberIndexType*/ undefined); + } + return links.inferredClassType; } - function isLiteralContextualType(contextualType) { - if (contextualType) { - if (contextualType.flags & 16384 /* TypeParameter */) { - var apparentType = getApparentTypeOfTypeParameter(contextualType); - // If the type parameter is constrained to the base primitive type we're checking for, - // consider this a literal context. For example, given a type parameter 'T extends string', - // this causes us to infer string literal types for T. - if (apparentType.flags & (2 /* String */ | 4 /* Number */ | 8 /* Boolean */ | 16 /* Enum */)) { - return true; + /** + * Syntactically and semantically checks a call or new expression. + * @param node The call/new expression to be checked. + * @returns On success, the expression's signature's return type. On failure, anyType. + */ + function checkCallExpression(node) { + // Grammar checking; stop grammar-checking if checkGrammarTypeArguments return true + checkGrammarTypeArguments(node, node.typeArguments) || checkGrammarArguments(node, node.arguments); + var signature = getResolvedSignature(node); + if (node.expression.kind === 95 /* SuperKeyword */) { + return voidType; + } + if (node.kind === 175 /* NewExpression */) { + var declaration = signature.declaration; + if (declaration && + declaration.kind !== 148 /* Constructor */ && + declaration.kind !== 152 /* ConstructSignature */ && + declaration.kind !== 157 /* ConstructorType */ && + !ts.isJSDocConstructSignature(declaration)) { + // When resolved signature is a call signature (and not a construct signature) the result type is any, unless + // the declaring function had members created through 'x.prototype.y = expr' or 'this.y = expr' psuedodeclarations + // in a JS file + // Note:JS inferred classes might come from a variable declaration instead of a function declaration. + // In this case, using getResolvedSymbol directly is required to avoid losing the members from the declaration. + var funcSymbol = node.expression.kind === 69 /* Identifier */ ? + getResolvedSymbol(node.expression) : + checkExpression(node.expression).symbol; + if (funcSymbol && funcSymbol.members && (funcSymbol.flags & 16 /* Function */ || ts.isDeclarationOfFunctionExpression(funcSymbol))) { + return getInferredClassType(funcSymbol); } - contextualType = apparentType; + else if (compilerOptions.noImplicitAny) { + error(node, ts.Diagnostics.new_expression_whose_target_lacks_a_construct_signature_implicitly_has_an_any_type); + } + return anyType; } - return maybeTypeOfKind(contextualType, 480 /* Literal */); } - return false; - } - function checkExpressionForMutableLocation(node, contextualMapper) { - var type = checkExpression(node, contextualMapper); - return isTypeAssertion(node) || isLiteralContextualType(getContextualType(node)) ? type : getWidenedLiteralType(type); - } - function checkPropertyAssignment(node, contextualMapper) { - // Do not use hasDynamicName here, because that returns false for well known symbols. - // We want to perform checkComputedPropertyName for all computed properties, including - // well known symbols. - if (node.name.kind === 140 /* ComputedPropertyName */) { - checkComputedPropertyName(node.name); + // In JavaScript files, calls to any identifier 'require' are treated as external module imports + if (ts.isInJavaScriptFile(node) && ts.isRequireCall(node, /*checkArgumentIsStringLiteral*/ true)) { + return resolveExternalModuleTypeByLiteral(node.arguments[0]); } - return checkExpressionForMutableLocation(node.initializer, contextualMapper); + return getReturnTypeOfSignature(signature); } - function checkObjectLiteralMethod(node, contextualMapper) { - // Grammar checking - checkGrammarMethod(node); - // Do not use hasDynamicName here, because that returns false for well known symbols. - // We want to perform checkComputedPropertyName for all computed properties, including - // well known symbols. - if (node.name.kind === 140 /* ComputedPropertyName */) { - checkComputedPropertyName(node.name); - } - var uninstantiatedType = checkFunctionExpressionOrObjectLiteralMethod(node, contextualMapper); - return instantiateTypeWithSingleGenericCallSignature(node, uninstantiatedType, contextualMapper); + function checkTaggedTemplateExpression(node) { + return getReturnTypeOfSignature(getResolvedSignature(node)); } - function instantiateTypeWithSingleGenericCallSignature(node, type, contextualMapper) { - if (isInferentialContext(contextualMapper)) { - var signature = getSingleCallSignature(type); - if (signature && signature.typeParameters) { - var contextualType = getApparentTypeOfContextualType(node); - if (contextualType) { - var contextualSignature = getSingleCallSignature(contextualType); - if (contextualSignature && !contextualSignature.typeParameters) { - return getOrCreateTypeFromSignature(instantiateSignatureInContextOf(signature, contextualSignature, contextualMapper)); - } - } + function checkAssertion(node) { + var exprType = getRegularTypeOfObjectLiteral(getBaseTypeOfLiteralType(checkExpression(node.expression))); + checkSourceElement(node.type); + var targetType = getTypeFromTypeNode(node.type); + if (produceDiagnostics && targetType !== unknownType) { + var widenedType = getWidenedType(exprType); + if (!isTypeComparableTo(targetType, widenedType)) { + checkTypeComparableTo(exprType, targetType, node, ts.Diagnostics.Type_0_cannot_be_converted_to_type_1); } } - return type; + return targetType; } - // Checks an expression and returns its type. The contextualMapper parameter serves two purposes: When - // contextualMapper is not undefined and not equal to the identityMapper function object it indicates that the - // expression is being inferentially typed (section 4.15.2 in spec) and provides the type mapper to use in - // conjunction with the generic contextual type. When contextualMapper is equal to the identityMapper function - // object, it serves as an indicator that all contained function and arrow expressions should be considered to - // have the wildcard function type; this form of type check is used during overload resolution to exclude - // contextually typed function and arrow expressions in the initial phase. - function checkExpression(node, contextualMapper) { - var type; - if (node.kind === 139 /* QualifiedName */) { - type = checkQualifiedName(node); - } - else { - var uninstantiatedType = checkExpressionWorker(node, contextualMapper); - type = instantiateTypeWithSingleGenericCallSignature(node, uninstantiatedType, contextualMapper); - } - if (isConstEnumObjectType(type)) { - // enum object type for const enums are only permitted in: - // - 'left' in property access - // - 'object' in indexed access - // - target in rhs of import statement - var ok = (node.parent.kind === 172 /* PropertyAccessExpression */ && node.parent.expression === node) || - (node.parent.kind === 173 /* ElementAccessExpression */ && node.parent.expression === node) || - ((node.kind === 69 /* Identifier */ || node.kind === 139 /* QualifiedName */) && isInRightSideOfImportOrExportAssignment(node)); - if (!ok) { - error(node, ts.Diagnostics.const_enums_can_only_be_used_in_property_or_index_access_expressions_or_the_right_hand_side_of_an_import_declaration_or_export_assignment); + function checkNonNullAssertion(node) { + return getNonNullableType(checkExpression(node.expression)); + } + function getTypeOfParameter(symbol) { + var type = getTypeOfSymbol(symbol); + if (strictNullChecks) { + var declaration = symbol.valueDeclaration; + if (declaration && declaration.initializer) { + return includeFalsyTypes(type, 2048 /* Undefined */); } } return type; } - function checkExpressionWorker(node, contextualMapper) { - switch (node.kind) { - case 69 /* Identifier */: - return checkIdentifier(node); - case 97 /* ThisKeyword */: - return checkThisExpression(node); - case 95 /* SuperKeyword */: - return checkSuperExpression(node); - case 93 /* NullKeyword */: - return nullWideningType; - case 9 /* StringLiteral */: - case 8 /* NumericLiteral */: - case 99 /* TrueKeyword */: - case 84 /* FalseKeyword */: - return checkLiteralExpression(node); - case 189 /* TemplateExpression */: - return checkTemplateExpression(node); - case 11 /* NoSubstitutionTemplateLiteral */: - return stringType; - case 10 /* RegularExpressionLiteral */: - return globalRegExpType; - case 170 /* ArrayLiteralExpression */: - return checkArrayLiteral(node, contextualMapper); - case 171 /* ObjectLiteralExpression */: - return checkObjectLiteral(node, contextualMapper); - case 172 /* PropertyAccessExpression */: - return checkPropertyAccessExpression(node); - case 173 /* ElementAccessExpression */: - return checkIndexedAccess(node); - case 174 /* CallExpression */: - case 175 /* NewExpression */: - return checkCallExpression(node); - case 176 /* TaggedTemplateExpression */: - return checkTaggedTemplateExpression(node); - case 178 /* ParenthesizedExpression */: - return checkExpression(node.expression, contextualMapper); - case 192 /* ClassExpression */: - return checkClassExpression(node); - case 179 /* FunctionExpression */: - case 180 /* ArrowFunction */: - return checkFunctionExpressionOrObjectLiteralMethod(node, contextualMapper); - case 182 /* TypeOfExpression */: - return checkTypeOfExpression(node); - case 177 /* TypeAssertionExpression */: - case 195 /* AsExpression */: - return checkAssertion(node); - case 196 /* NonNullExpression */: - return checkNonNullAssertion(node); - case 181 /* DeleteExpression */: - return checkDeleteExpression(node); - case 183 /* VoidExpression */: - return checkVoidExpression(node); - case 184 /* AwaitExpression */: - return checkAwaitExpression(node); - case 185 /* PrefixUnaryExpression */: - return checkPrefixUnaryExpression(node); - case 186 /* PostfixUnaryExpression */: - return checkPostfixUnaryExpression(node); - case 187 /* BinaryExpression */: - return checkBinaryExpression(node, contextualMapper); - case 188 /* ConditionalExpression */: - return checkConditionalExpression(node, contextualMapper); - case 191 /* SpreadElementExpression */: - return checkSpreadElementExpression(node, contextualMapper); - case 193 /* OmittedExpression */: - return undefinedWideningType; - case 190 /* YieldExpression */: - return checkYieldExpression(node); - case 248 /* JsxExpression */: - return checkJsxExpression(node); - case 241 /* JsxElement */: - return checkJsxElement(node); - case 242 /* JsxSelfClosingElement */: - return checkJsxSelfClosingElement(node); - case 243 /* JsxOpeningElement */: - ts.Debug.fail("Shouldn't ever directly check a JsxOpeningElement"); - } - return unknownType; + function getTypeAtPosition(signature, pos) { + return signature.hasRestParameter ? + pos < signature.parameters.length - 1 ? getTypeOfParameter(signature.parameters[pos]) : getRestTypeOfSignature(signature) : + pos < signature.parameters.length ? getTypeOfParameter(signature.parameters[pos]) : anyType; } - // DECLARATION AND STATEMENT TYPE CHECKING - function checkTypeParameter(node) { - // Grammar Checking - if (node.expression) { - grammarErrorOnFirstToken(node.expression, ts.Diagnostics.Type_expected); + function assignContextualParameterTypes(signature, context, mapper) { + var len = signature.parameters.length - (signature.hasRestParameter ? 1 : 0); + if (context.thisParameter) { + if (!signature.thisParameter) { + signature.thisParameter = createTransientSymbol(context.thisParameter, undefined); + } + assignTypeToParameterAndFixTypeParameters(signature.thisParameter, getTypeOfSymbol(context.thisParameter), mapper); } - checkSourceElement(node.constraint); - getConstraintOfTypeParameter(getDeclaredTypeOfTypeParameter(getSymbolOfNode(node))); - if (produceDiagnostics) { - checkTypeNameIsReserved(node.name, ts.Diagnostics.Type_parameter_name_cannot_be_0); + for (var i = 0; i < len; i++) { + var parameter = signature.parameters[i]; + var contextualParameterType = getTypeAtPosition(context, i); + assignTypeToParameterAndFixTypeParameters(parameter, contextualParameterType, mapper); + } + if (signature.hasRestParameter && isRestParameterIndex(context, signature.parameters.length - 1)) { + var parameter = ts.lastOrUndefined(signature.parameters); + var contextualParameterType = getTypeOfSymbol(ts.lastOrUndefined(context.parameters)); + assignTypeToParameterAndFixTypeParameters(parameter, contextualParameterType, mapper); } } - function checkParameter(node) { - // Grammar checking - // It is a SyntaxError if the Identifier "eval" or the Identifier "arguments" occurs as the - // Identifier in a PropertySetParameterList of a PropertyAssignment that is contained in strict code - // or if its FunctionBody is strict code(11.1.5). - // Grammar checking - checkGrammarDecorators(node) || checkGrammarModifiers(node); - checkVariableLikeDeclaration(node); - var func = ts.getContainingFunction(node); - if (ts.getModifierFlags(node) & 92 /* ParameterPropertyModifier */) { - func = ts.getContainingFunction(node); - if (!(func.kind === 148 /* Constructor */ && ts.nodeIsPresent(func.body))) { - error(node, ts.Diagnostics.A_parameter_property_is_only_allowed_in_a_constructor_implementation); + // When contextual typing assigns a type to a parameter that contains a binding pattern, we also need to push + // the destructured type into the contained binding elements. + function assignBindingElementTypes(node) { + if (ts.isBindingPattern(node.name)) { + for (var _i = 0, _a = node.name.elements; _i < _a.length; _i++) { + var element = _a[_i]; + if (!ts.isOmittedExpression(element)) { + if (element.name.kind === 69 /* Identifier */) { + getSymbolLinks(getSymbolOfNode(element)).type = getTypeForBindingElement(element); + } + assignBindingElementTypes(element); + } } } - if (node.questionToken && ts.isBindingPattern(node.name) && func.body) { - error(node, ts.Diagnostics.A_binding_pattern_parameter_cannot_be_optional_in_an_implementation_signature); - } - if (node.name.text === "this") { - if (ts.indexOf(func.parameters, node) !== 0) { - error(node, ts.Diagnostics.A_this_parameter_must_be_the_first_parameter); - } - if (func.kind === 148 /* Constructor */ || func.kind === 152 /* ConstructSignature */ || func.kind === 157 /* ConstructorType */) { - error(node, ts.Diagnostics.A_constructor_cannot_have_a_this_parameter); + } + function assignTypeToParameterAndFixTypeParameters(parameter, contextualType, mapper) { + var links = getSymbolLinks(parameter); + if (!links.type) { + links.type = instantiateType(contextualType, mapper); + // if inference didn't come up with anything but {}, fall back to the binding pattern if present. + if (links.type === emptyObjectType && + (parameter.valueDeclaration.name.kind === 167 /* ObjectBindingPattern */ || + parameter.valueDeclaration.name.kind === 168 /* ArrayBindingPattern */)) { + links.type = getTypeFromBindingPattern(parameter.valueDeclaration.name); } + assignBindingElementTypes(parameter.valueDeclaration); } - // Only check rest parameter type if it's not a binding pattern. Since binding patterns are - // not allowed in a rest parameter, we already have an error from checkGrammarParameterList. - if (node.dotDotDotToken && !ts.isBindingPattern(node.name) && !isArrayType(getTypeOfSymbol(node.symbol))) { - error(node, ts.Diagnostics.A_rest_parameter_must_be_of_an_array_type); + else if (isInferentialContext(mapper)) { + // Even if the parameter already has a type, it might be because it was given a type while + // processing the function as an argument to a prior signature during overload resolution. + // If this was the case, it may have caused some type parameters to be fixed. So here, + // we need to ensure that type parameters at the same positions get fixed again. This is + // done by calling instantiateType to attach the mapper to the contextualType, and then + // calling inferTypes to force a walk of contextualType so that all the correct fixing + // happens. The choice to pass in links.type may seem kind of arbitrary, but it serves + // to make sure that all the correct positions in contextualType are reached by the walk. + // Here is an example: + // + // interface Base { + // baseProp; + // } + // interface Derived extends Base { + // toBase(): Base; + // } + // + // var derived: Derived; + // + // declare function foo(x: T, func: (p: T) => T): T; + // declare function foo(x: T, func: (p: T) => T): T; + // + // var result = foo(derived, d => d.toBase()); + // + // We are typing d while checking the second overload. But we've already given d + // a type (Derived) from the first overload. However, we still want to fix the + // T in the second overload so that we do not infer Base as a candidate for T + // (inferring Base would make type argument inference inconsistent between the two + // overloads). + inferTypes(mapper.context, links.type, instantiateType(contextualType, mapper)); } } - function isSyntacticallyValidGenerator(node) { - if (!node.asteriskToken || !node.body) { - return false; + function getReturnTypeFromJSDocComment(func) { + var returnTag = ts.getJSDocReturnTag(func); + if (returnTag && returnTag.typeExpression) { + return getTypeFromTypeNode(returnTag.typeExpression.type); } - return node.kind === 147 /* MethodDeclaration */ || - node.kind === 220 /* FunctionDeclaration */ || - node.kind === 179 /* FunctionExpression */; + return undefined; } - function getTypePredicateParameterIndex(parameterList, parameter) { - if (parameterList) { - for (var i = 0; i < parameterList.length; i++) { - var param = parameterList[i]; - if (param.name.kind === 69 /* Identifier */ && - param.name.text === parameter.text) { - return i; - } - } + function createPromiseType(promisedType) { + // creates a `Promise` type where `T` is the promisedType argument + var globalPromiseType = getGlobalPromiseType(); + if (globalPromiseType !== emptyGenericType) { + // if the promised type is itself a promise, get the underlying type; otherwise, fallback to the promised type + promisedType = getAwaitedType(promisedType); + return createTypeReference(globalPromiseType, [promisedType]); } - return -1; + return emptyObjectType; } - function checkTypePredicate(node) { - var parent = getTypePredicateParent(node); - if (!parent) { - // The parent must not be valid. - error(node, ts.Diagnostics.A_type_predicate_is_only_allowed_in_return_type_position_for_functions_and_methods); - return; + function createPromiseReturnType(func, promisedType) { + var promiseType = createPromiseType(promisedType); + if (promiseType === emptyObjectType) { + error(func, ts.Diagnostics.An_async_function_or_method_must_have_a_valid_awaitable_return_type); + return unknownType; } - var typePredicate = getSignatureFromDeclaration(parent).typePredicate; - if (!typePredicate) { - return; + return promiseType; + } + function getReturnTypeFromBody(func, contextualMapper) { + var contextualSignature = getContextualSignatureForFunctionLikeDeclaration(func); + if (!func.body) { + return unknownType; } - var parameterName = node.parameterName; - if (ts.isThisTypePredicate(typePredicate)) { - getTypeFromThisTypeNode(parameterName); + var isAsync = ts.isAsyncFunctionLike(func); + var type; + if (func.body.kind !== 199 /* Block */) { + type = checkExpressionCached(func.body, contextualMapper); + if (isAsync) { + // From within an async function you can return either a non-promise value or a promise. Any + // Promise/A+ compatible implementation will always assimilate any foreign promise, so the + // return type of the body should be unwrapped to its awaited type, which we will wrap in + // the native Promise type later in this function. + type = checkAwaitedType(type, func, ts.Diagnostics.Return_expression_in_async_function_does_not_have_a_valid_callable_then_member); + } } else { - if (typePredicate.parameterIndex >= 0) { - if (parent.parameters[typePredicate.parameterIndex].dotDotDotToken) { - error(parameterName, ts.Diagnostics.A_type_predicate_cannot_reference_a_rest_parameter); - } - else { - var leadingError = ts.chainDiagnosticMessages(undefined, ts.Diagnostics.A_type_predicate_s_type_must_be_assignable_to_its_parameter_s_type); - checkTypeAssignableTo(typePredicate.type, getTypeOfNode(parent.parameters[typePredicate.parameterIndex]), node.type, - /*headMessage*/ undefined, leadingError); + var types = void 0; + var funcIsGenerator = !!func.asteriskToken; + if (funcIsGenerator) { + types = checkAndAggregateYieldOperandTypes(func, contextualMapper); + if (types.length === 0) { + var iterableIteratorAny = createIterableIteratorType(anyType); + if (compilerOptions.noImplicitAny) { + error(func.asteriskToken, ts.Diagnostics.Generator_implicitly_has_type_0_because_it_does_not_yield_any_values_Consider_supplying_a_return_type, typeToString(iterableIteratorAny)); + } + return iterableIteratorAny; } } - else if (parameterName) { - var hasReportedError = false; - for (var _i = 0, _a = parent.parameters; _i < _a.length; _i++) { - var name_18 = _a[_i].name; - if (ts.isBindingPattern(name_18) && - checkIfTypePredicateVariableIsDeclaredInBindingPattern(name_18, parameterName, typePredicate.parameterName)) { - hasReportedError = true; - break; - } + else { + types = checkAndAggregateReturnExpressionTypes(func, contextualMapper); + if (!types) { + // For an async function, the return type will not be never, but rather a Promise for never. + return isAsync ? createPromiseReturnType(func, neverType) : neverType; } - if (!hasReportedError) { - error(node.parameterName, ts.Diagnostics.Cannot_find_parameter_0, typePredicate.parameterName); + if (types.length === 0) { + // For an async function, the return type will not be void, but rather a Promise for void. + return isAsync ? createPromiseReturnType(func, voidType) : voidType; } } + // Return a union of the return expression types. + type = getUnionType(types, /*subtypeReduction*/ true); + if (funcIsGenerator) { + type = createIterableIteratorType(type); + } } - } - function getTypePredicateParent(node) { - switch (node.parent.kind) { - case 180 /* ArrowFunction */: - case 151 /* CallSignature */: - case 220 /* FunctionDeclaration */: - case 179 /* FunctionExpression */: - case 156 /* FunctionType */: - case 147 /* MethodDeclaration */: - case 146 /* MethodSignature */: - var parent_11 = node.parent; - if (node === parent_11.type) { - return parent_11; - } + if (!contextualSignature) { + reportErrorsFromWidening(func, type); + } + if (isUnitType(type) && + !(contextualSignature && + isLiteralContextualType(contextualSignature === getSignatureFromDeclaration(func) ? type : getReturnTypeOfSignature(contextualSignature)))) { + type = getWidenedLiteralType(type); } + var widenedType = getWidenedType(type); + // From within an async function you can return either a non-promise value or a promise. Any + // Promise/A+ compatible implementation will always assimilate any foreign promise, so the + // return type of the body is awaited type of the body, wrapped in a native Promise type. + return isAsync ? createPromiseReturnType(func, widenedType) : widenedType; } - function checkIfTypePredicateVariableIsDeclaredInBindingPattern(pattern, predicateVariableNode, predicateVariableName) { - for (var _i = 0, _a = pattern.elements; _i < _a.length; _i++) { - var element = _a[_i]; - if (ts.isOmittedExpression(element)) { - continue; - } - var name_19 = element.name; - if (name_19.kind === 69 /* Identifier */ && - name_19.text === predicateVariableName) { - error(predicateVariableNode, ts.Diagnostics.A_type_predicate_cannot_reference_element_0_in_a_binding_pattern, predicateVariableName); - return true; - } - else if (name_19.kind === 168 /* ArrayBindingPattern */ || - name_19.kind === 167 /* ObjectBindingPattern */) { - if (checkIfTypePredicateVariableIsDeclaredInBindingPattern(name_19, predicateVariableNode, predicateVariableName)) { - return true; + function checkAndAggregateYieldOperandTypes(func, contextualMapper) { + var aggregatedTypes = []; + ts.forEachYieldExpression(func.body, function (yieldExpression) { + var expr = yieldExpression.expression; + if (expr) { + var type = checkExpressionCached(expr, contextualMapper); + if (yieldExpression.asteriskToken) { + // A yield* expression effectively yields everything that its operand yields + type = checkElementTypeOfIterable(type, yieldExpression.expression); + } + if (!ts.contains(aggregatedTypes, type)) { + aggregatedTypes.push(type); } } - } + }); + return aggregatedTypes; } - function checkSignatureDeclaration(node) { - // Grammar checking - if (node.kind === 153 /* IndexSignature */) { - checkGrammarIndexSignature(node); + function isExhaustiveSwitchStatement(node) { + if (!node.possiblyExhaustive) { + return false; } - else if (node.kind === 156 /* FunctionType */ || node.kind === 220 /* FunctionDeclaration */ || node.kind === 157 /* ConstructorType */ || - node.kind === 151 /* CallSignature */ || node.kind === 148 /* Constructor */ || - node.kind === 152 /* ConstructSignature */) { - checkGrammarFunctionLikeDeclaration(node); + var type = checkExpression(node.expression); + if (!isLiteralType(type)) { + return false; } - checkTypeParameters(node.typeParameters); - ts.forEach(node.parameters, checkParameter); - if (node.type) { - checkSourceElement(node.type); + var switchTypes = getSwitchClauseTypes(node); + if (!switchTypes.length) { + return false; } - if (produceDiagnostics) { - checkCollisionWithArgumentsInGeneratedCode(node); - if (compilerOptions.noImplicitAny && !node.type) { - switch (node.kind) { - case 152 /* ConstructSignature */: - error(node, ts.Diagnostics.Construct_signature_which_lacks_return_type_annotation_implicitly_has_an_any_return_type); - break; - case 151 /* CallSignature */: - error(node, ts.Diagnostics.Call_signature_which_lacks_return_type_annotation_implicitly_has_an_any_return_type); - break; + return eachTypeContainedIn(type, switchTypes); + } + function functionHasImplicitReturn(func) { + if (!(func.flags & 128 /* HasImplicitReturn */)) { + return false; + } + var lastStatement = ts.lastOrUndefined(func.body.statements); + if (lastStatement && lastStatement.kind === 213 /* SwitchStatement */ && isExhaustiveSwitchStatement(lastStatement)) { + return false; + } + return true; + } + function checkAndAggregateReturnExpressionTypes(func, contextualMapper) { + var isAsync = ts.isAsyncFunctionLike(func); + var aggregatedTypes = []; + var hasReturnWithNoExpression = functionHasImplicitReturn(func); + var hasReturnOfTypeNever = false; + ts.forEachReturnStatement(func.body, function (returnStatement) { + var expr = returnStatement.expression; + if (expr) { + var type = checkExpressionCached(expr, contextualMapper); + if (isAsync) { + // From within an async function you can return either a non-promise value or a promise. Any + // Promise/A+ compatible implementation will always assimilate any foreign promise, so the + // return type of the body should be unwrapped to its awaited type, which should be wrapped in + // the native Promise type by the caller. + type = checkAwaitedType(type, func, ts.Diagnostics.Return_expression_in_async_function_does_not_have_a_valid_callable_then_member); } - } - if (node.type) { - if (languageVersion >= 2 /* ES6 */ && isSyntacticallyValidGenerator(node)) { - var returnType = getTypeFromTypeNode(node.type); - if (returnType === voidType) { - error(node.type, ts.Diagnostics.A_generator_cannot_have_a_void_type_annotation); - } - else { - var generatorElementType = getElementTypeOfIterableIterator(returnType) || anyType; - var iterableIteratorInstantiation = createIterableIteratorType(generatorElementType); - // Naively, one could check that IterableIterator is assignable to the return type annotation. - // However, that would not catch the error in the following case. - // - // interface BadGenerator extends Iterable, Iterator { } - // function* g(): BadGenerator { } // Iterable and Iterator have different types! - // - checkTypeAssignableTo(iterableIteratorInstantiation, returnType, node.type); - } + if (type.flags & 8192 /* Never */) { + hasReturnOfTypeNever = true; } - else if (ts.isAsyncFunctionLike(node)) { - checkAsyncFunctionReturnType(node); + else if (!ts.contains(aggregatedTypes, type)) { + aggregatedTypes.push(type); } } - if (noUnusedIdentifiers && !node.body) { - checkUnusedTypeParameters(node); + else { + hasReturnWithNoExpression = true; + } + }); + if (aggregatedTypes.length === 0 && !hasReturnWithNoExpression && (hasReturnOfTypeNever || + func.kind === 179 /* FunctionExpression */ || func.kind === 180 /* ArrowFunction */)) { + return undefined; + } + if (strictNullChecks && aggregatedTypes.length && hasReturnWithNoExpression) { + if (!ts.contains(aggregatedTypes, undefinedType)) { + aggregatedTypes.push(undefinedType); } } + return aggregatedTypes; } - function checkClassForDuplicateDeclarations(node) { - var Accessor; - (function (Accessor) { - Accessor[Accessor["Getter"] = 1] = "Getter"; - Accessor[Accessor["Setter"] = 2] = "Setter"; - Accessor[Accessor["Property"] = 3] = "Property"; - })(Accessor || (Accessor = {})); - var instanceNames = ts.createMap(); - var staticNames = ts.createMap(); - for (var _i = 0, _a = node.members; _i < _a.length; _i++) { - var member = _a[_i]; - if (member.kind === 148 /* Constructor */) { - for (var _b = 0, _c = member.parameters; _b < _c.length; _b++) { - var param = _c[_b]; - if (ts.isParameterPropertyDeclaration(param)) { - addName(instanceNames, param.name, param.name.text, 3 /* Property */); - } - } - } - else { - var isStatic = ts.forEach(member.modifiers, function (m) { return m.kind === 113 /* StaticKeyword */; }); - var names = isStatic ? staticNames : instanceNames; - var memberName = member.name && ts.getPropertyNameForPropertyNameNode(member.name); - if (memberName) { - switch (member.kind) { - case 149 /* GetAccessor */: - addName(names, member.name, memberName, 1 /* Getter */); - break; - case 150 /* SetAccessor */: - addName(names, member.name, memberName, 2 /* Setter */); - break; - case 145 /* PropertyDeclaration */: - addName(names, member.name, memberName, 3 /* Property */); - break; - } - } - } + /** + * TypeScript Specification 1.0 (6.3) - July 2014 + * An explicitly typed function whose return type isn't the Void type, + * the Any type, or a union type containing the Void or Any type as a constituent + * must have at least one return statement somewhere in its body. + * An exception to this rule is if the function implementation consists of a single 'throw' statement. + * + * @param returnType - return type of the function, can be undefined if return type is not explicitly specified + */ + function checkAllCodePathsInNonVoidFunctionReturnOrThrow(func, returnType) { + if (!produceDiagnostics) { + return; } - function addName(names, location, name, meaning) { - var prev = names[name]; - if (prev) { - if (prev & meaning) { - error(location, ts.Diagnostics.Duplicate_identifier_0, ts.getTextOfNode(location)); - } - else { - names[name] = prev | meaning; - } - } - else { - names[name] = meaning; - } + // Functions with with an explicitly specified 'void' or 'any' return type don't need any return expressions. + if (returnType && maybeTypeOfKind(returnType, 1 /* Any */ | 1024 /* Void */)) { + return; } - } - function checkObjectTypeForDuplicateDeclarations(node) { - var names = ts.createMap(); - for (var _i = 0, _a = node.members; _i < _a.length; _i++) { - var member = _a[_i]; - if (member.kind == 144 /* PropertySignature */) { - var memberName = void 0; - switch (member.name.kind) { - case 9 /* StringLiteral */: - case 8 /* NumericLiteral */: - case 69 /* Identifier */: - memberName = member.name.text; - break; - default: - continue; - } - if (names[memberName]) { - error(member.symbol.valueDeclaration.name, ts.Diagnostics.Duplicate_identifier_0, memberName); - error(member.name, ts.Diagnostics.Duplicate_identifier_0, memberName); - } - else { - names[memberName] = true; - } - } + // If all we have is a function signature, or an arrow function with an expression body, then there is nothing to check. + // also if HasImplicitReturn flag is not set this means that all codepaths in function body end with return or throw + if (ts.nodeIsMissing(func.body) || func.body.kind !== 199 /* Block */ || !functionHasImplicitReturn(func)) { + return; } - } - function checkTypeForDuplicateIndexSignatures(node) { - if (node.kind === 222 /* InterfaceDeclaration */) { - var nodeSymbol = getSymbolOfNode(node); - // in case of merging interface declaration it is possible that we'll enter this check procedure several times for every declaration - // to prevent this run check only for the first declaration of a given kind - if (nodeSymbol.declarations.length > 0 && nodeSymbol.declarations[0] !== node) { - return; - } + var hasExplicitReturn = func.flags & 256 /* HasExplicitReturn */; + if (returnType && returnType.flags & 8192 /* Never */) { + error(func.type, ts.Diagnostics.A_function_returning_never_cannot_have_a_reachable_end_point); } - // TypeScript 1.0 spec (April 2014) - // 3.7.4: An object type can contain at most one string index signature and one numeric index signature. - // 8.5: A class declaration can have at most one string index member declaration and one numeric index member declaration - var indexSymbol = getIndexSymbol(getSymbolOfNode(node)); - if (indexSymbol) { - var seenNumericIndexer = false; - var seenStringIndexer = false; - for (var _i = 0, _a = indexSymbol.declarations; _i < _a.length; _i++) { - var decl = _a[_i]; - var declaration = decl; - if (declaration.parameters.length === 1 && declaration.parameters[0].type) { - switch (declaration.parameters[0].type.kind) { - case 132 /* StringKeyword */: - if (!seenStringIndexer) { - seenStringIndexer = true; - } - else { - error(declaration, ts.Diagnostics.Duplicate_string_index_signature); - } - break; - case 130 /* NumberKeyword */: - if (!seenNumericIndexer) { - seenNumericIndexer = true; - } - else { - error(declaration, ts.Diagnostics.Duplicate_number_index_signature); - } - break; - } + else if (returnType && !hasExplicitReturn) { + // minimal check: function has syntactic return type annotation and no explicit return statements in the body + // this function does not conform to the specification. + // NOTE: having returnType !== undefined is a precondition for entering this branch so func.type will always be present + error(func.type, ts.Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value); + } + else if (returnType && strictNullChecks && !isTypeAssignableTo(undefinedType, returnType)) { + error(func.type, ts.Diagnostics.Function_lacks_ending_return_statement_and_return_type_does_not_include_undefined); + } + else if (compilerOptions.noImplicitReturns) { + if (!returnType) { + // If return type annotation is omitted check if function has any explicit return statements. + // If it does not have any - its inferred return type is void - don't do any checks. + // Otherwise get inferred return type from function body and report error only if it is not void / anytype + if (!hasExplicitReturn) { + return; + } + var inferredReturnType = getReturnTypeOfSignature(getSignatureFromDeclaration(func)); + if (isUnwrappedReturnTypeVoidOrAny(func, inferredReturnType)) { + return; } } + error(func.type || func, ts.Diagnostics.Not_all_code_paths_return_a_value); } } - function checkPropertyDeclaration(node) { - // Grammar checking - checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarProperty(node) || checkGrammarComputedPropertyName(node.name); - checkVariableLikeDeclaration(node); - } - function checkMethodDeclaration(node) { + function checkFunctionExpressionOrObjectLiteralMethod(node, contextualMapper) { + ts.Debug.assert(node.kind !== 147 /* MethodDeclaration */ || ts.isObjectLiteralMethod(node)); // Grammar checking - checkGrammarMethod(node) || checkGrammarComputedPropertyName(node.name); - // Grammar checking for modifiers is done inside the function checkGrammarFunctionLikeDeclaration - checkFunctionOrMethodDeclaration(node); - // Abstract methods cannot have an implementation. - // Extra checks are to avoid reporting multiple errors relating to the "abstractness" of the node. - if (ts.getModifierFlags(node) & 128 /* Abstract */ && node.body) { - error(node, ts.Diagnostics.Method_0_cannot_have_an_implementation_because_it_is_marked_abstract, ts.declarationNameToString(node.name)); - } - } - function checkConstructorDeclaration(node) { - // Grammar check on signature of constructor and modifier of the constructor is done in checkSignatureDeclaration function. - checkSignatureDeclaration(node); - // Grammar check for checking only related to constructorDeclaration - checkGrammarConstructorTypeParameters(node) || checkGrammarConstructorTypeAnnotation(node); - checkSourceElement(node.body); - registerForUnusedIdentifiersCheck(node); - var symbol = getSymbolOfNode(node); - var firstDeclaration = ts.getDeclarationOfKind(symbol, node.kind); - // Only type check the symbol once - if (node === firstDeclaration) { - checkFunctionOrConstructorSymbol(symbol); - } - // exit early in the case of signature - super checks are not relevant to them - if (ts.nodeIsMissing(node.body)) { - return; - } - if (!produceDiagnostics) { - return; - } - function containsSuperCallAsComputedPropertyName(n) { - return n.name && containsSuperCall(n.name); - } - function containsSuperCall(n) { - if (ts.isSuperCallExpression(n)) { - return true; - } - else if (ts.isFunctionLike(n)) { - return false; - } - else if (ts.isClassLike(n)) { - return ts.forEach(n.members, containsSuperCallAsComputedPropertyName); - } - return ts.forEachChild(n, containsSuperCall); - } - function markThisReferencesAsErrors(n) { - if (n.kind === 97 /* ThisKeyword */) { - error(n, ts.Diagnostics.this_cannot_be_referenced_in_current_location); - } - else if (n.kind !== 179 /* FunctionExpression */ && n.kind !== 220 /* FunctionDeclaration */) { - ts.forEachChild(n, markThisReferencesAsErrors); - } + var hasGrammarError = checkGrammarFunctionLikeDeclaration(node); + if (!hasGrammarError && node.kind === 179 /* FunctionExpression */) { + checkGrammarForGenerator(node); } - function isInstancePropertyWithInitializer(n) { - return n.kind === 145 /* PropertyDeclaration */ && - !(ts.getModifierFlags(n) & 32 /* Static */) && - !!n.initializer; + // The identityMapper object is used to indicate that function expressions are wildcards + if (contextualMapper === identityMapper && isContextSensitive(node)) { + checkNodeDeferred(node); + return anyFunctionType; } - // TS 1.0 spec (April 2014): 8.3.2 - // Constructors of classes with no extends clause may not contain super calls, whereas - // constructors of derived classes must contain at least one super call somewhere in their function body. - var containingClassDecl = node.parent; - if (ts.getClassExtendsHeritageClauseElement(containingClassDecl)) { - var classExtendsNull = classDeclarationExtendsNull(containingClassDecl); - var superCall = getSuperCallInConstructor(node); - if (superCall) { - if (classExtendsNull) { - error(superCall, ts.Diagnostics.A_constructor_cannot_contain_a_super_call_when_its_class_extends_null); - } - // The first statement in the body of a constructor (excluding prologue directives) must be a super call - // if both of the following are true: - // - The containing class is a derived class. - // - The constructor declares parameter properties - // or the containing class declares instance member variables with initializers. - var superCallShouldBeFirst = ts.forEach(node.parent.members, isInstancePropertyWithInitializer) || - ts.forEach(node.parameters, function (p) { return ts.getModifierFlags(p) & 92 /* ParameterPropertyModifier */; }); - // Skip past any prologue directives to find the first statement - // to ensure that it was a super call. - if (superCallShouldBeFirst) { - var statements = node.body.statements; - var superCallStatement = void 0; - for (var _i = 0, statements_2 = statements; _i < statements_2.length; _i++) { - var statement = statements_2[_i]; - if (statement.kind === 202 /* ExpressionStatement */ && ts.isSuperCallExpression(statement.expression)) { - superCallStatement = statement; - break; - } - if (!ts.isPrologueDirective(statement)) { - break; - } + var links = getNodeLinks(node); + var type = getTypeOfSymbol(node.symbol); + var contextSensitive = isContextSensitive(node); + var mightFixTypeParameters = contextSensitive && isInferentialContext(contextualMapper); + // Check if function expression is contextually typed and assign parameter types if so. + // See the comment in assignTypeToParameterAndFixTypeParameters to understand why we need to + // check mightFixTypeParameters. + if (mightFixTypeParameters || !(links.flags & 1024 /* ContextChecked */)) { + var contextualSignature = getContextualSignature(node); + // If a type check is started at a function expression that is an argument of a function call, obtaining the + // contextual type may recursively get back to here during overload resolution of the call. If so, we will have + // already assigned contextual types. + var contextChecked = !!(links.flags & 1024 /* ContextChecked */); + if (mightFixTypeParameters || !contextChecked) { + links.flags |= 1024 /* ContextChecked */; + if (contextualSignature) { + var signature = getSignaturesOfType(type, 0 /* Call */)[0]; + if (contextSensitive) { + assignContextualParameterTypes(signature, contextualSignature, contextualMapper || identityMapper); } - if (!superCallStatement) { - error(node, ts.Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_when_a_class_contains_initialized_properties_or_has_parameter_properties); + if (mightFixTypeParameters || !node.type && !signature.resolvedReturnType) { + var returnType = getReturnTypeFromBody(node, contextualMapper); + if (!signature.resolvedReturnType) { + signature.resolvedReturnType = returnType; + } } } - } - else if (!classExtendsNull) { - error(node, ts.Diagnostics.Constructors_for_derived_classes_must_contain_a_super_call); + if (!contextChecked) { + checkSignatureDeclaration(node); + checkNodeDeferred(node); + } } } + if (produceDiagnostics && node.kind !== 147 /* MethodDeclaration */) { + checkCollisionWithCapturedSuperVariable(node, node.name); + checkCollisionWithCapturedThisVariable(node, node.name); + } + return type; } - function checkAccessorDeclaration(node) { - if (produceDiagnostics) { - // Grammar checking accessors - checkGrammarFunctionLikeDeclaration(node) || checkGrammarAccessor(node) || checkGrammarComputedPropertyName(node.name); - checkDecorators(node); - checkSignatureDeclaration(node); - if (node.kind === 149 /* GetAccessor */) { - if (!ts.isInAmbientContext(node) && ts.nodeIsPresent(node.body) && (node.flags & 128 /* HasImplicitReturn */)) { - if (!(node.flags & 256 /* HasExplicitReturn */)) { - error(node.name, ts.Diagnostics.A_get_accessor_must_return_a_value); - } - } + function checkFunctionExpressionOrObjectLiteralMethodDeferred(node) { + ts.Debug.assert(node.kind !== 147 /* MethodDeclaration */ || ts.isObjectLiteralMethod(node)); + var isAsync = ts.isAsyncFunctionLike(node); + var returnOrPromisedType = node.type && (isAsync ? checkAsyncFunctionReturnType(node) : getTypeFromTypeNode(node.type)); + if (!node.asteriskToken) { + // return is not necessary in the body of generators + checkAllCodePathsInNonVoidFunctionReturnOrThrow(node, returnOrPromisedType); + } + if (node.body) { + if (!node.type) { + // There are some checks that are only performed in getReturnTypeFromBody, that may produce errors + // we need. An example is the noImplicitAny errors resulting from widening the return expression + // of a function. Because checking of function expression bodies is deferred, there was never an + // appropriate time to do this during the main walk of the file (see the comment at the top of + // checkFunctionExpressionBodies). So it must be done now. + getReturnTypeOfSignature(getSignatureFromDeclaration(node)); } - // Do not use hasDynamicName here, because that returns false for well known symbols. - // We want to perform checkComputedPropertyName for all computed properties, including - // well known symbols. - if (node.name.kind === 140 /* ComputedPropertyName */) { - checkComputedPropertyName(node.name); + if (node.body.kind === 199 /* Block */) { + checkSourceElement(node.body); } - if (!ts.hasDynamicName(node)) { - // TypeScript 1.0 spec (April 2014): 8.4.3 - // Accessors for the same member name must specify the same accessibility. - var otherKind = node.kind === 149 /* GetAccessor */ ? 150 /* SetAccessor */ : 149 /* GetAccessor */; - var otherAccessor = ts.getDeclarationOfKind(node.symbol, otherKind); - if (otherAccessor) { - if ((ts.getModifierFlags(node) & 28 /* AccessibilityModifier */) !== (ts.getModifierFlags(otherAccessor) & 28 /* AccessibilityModifier */)) { - error(node.name, ts.Diagnostics.Getter_and_setter_accessors_do_not_agree_in_visibility); + else { + // From within an async function you can return either a non-promise value or a promise. Any + // Promise/A+ compatible implementation will always assimilate any foreign promise, so we + // should not be checking assignability of a promise to the return type. Instead, we need to + // check assignability of the awaited type of the expression body against the promised type of + // its return type annotation. + var exprType = checkExpression(node.body); + if (returnOrPromisedType) { + if (isAsync) { + var awaitedType = checkAwaitedType(exprType, node.body, ts.Diagnostics.Expression_body_for_async_arrow_function_does_not_have_a_valid_callable_then_member); + checkTypeAssignableTo(awaitedType, returnOrPromisedType, node.body); } - if (ts.hasModifier(node, 128 /* Abstract */) !== ts.hasModifier(otherAccessor, 128 /* Abstract */)) { - error(node.name, ts.Diagnostics.Accessors_must_both_be_abstract_or_non_abstract); + else { + checkTypeAssignableTo(exprType, returnOrPromisedType, node.body); } - // TypeScript 1.0 spec (April 2014): 4.5 - // If both accessors include type annotations, the specified types must be identical. - checkAccessorDeclarationTypesIdentical(node, otherAccessor, getAnnotatedAccessorType, ts.Diagnostics.get_and_set_accessor_must_have_the_same_type); - checkAccessorDeclarationTypesIdentical(node, otherAccessor, getThisTypeOfDeclaration, ts.Diagnostics.get_and_set_accessor_must_have_the_same_this_type); } } - var returnType = getTypeOfAccessors(getSymbolOfNode(node)); - if (node.kind === 149 /* GetAccessor */) { - checkAllCodePathsInNonVoidFunctionReturnOrThrow(node, returnType); - } - } - if (node.parent.kind !== 171 /* ObjectLiteralExpression */) { - checkSourceElement(node.body); registerForUnusedIdentifiersCheck(node); } - else { - checkNodeDeferred(node); - } } - function checkAccessorDeclarationTypesIdentical(first, second, getAnnotatedType, message) { - var firstType = getAnnotatedType(first); - var secondType = getAnnotatedType(second); - if (firstType && secondType && !isTypeIdenticalTo(firstType, secondType)) { - error(first, message); + function checkArithmeticOperandType(operand, type, diagnostic) { + if (!isTypeAnyOrAllConstituentTypesHaveKind(type, 340 /* NumberLike */)) { + error(operand, diagnostic); + return false; } + return true; } - function checkAccessorDeferred(node) { - checkSourceElement(node.body); - registerForUnusedIdentifiersCheck(node); + function isReadonlySymbol(symbol) { + // The following symbols are considered read-only: + // Properties with a 'readonly' modifier + // Variables declared with 'const' + // Get accessors without matching set accessors + // Enum members + // Unions and intersections of the above (unions and intersections eagerly set isReadonly on creation) + return symbol.isReadonly || + symbol.flags & 4 /* Property */ && (getDeclarationModifierFlagsFromSymbol(symbol) & 64 /* Readonly */) !== 0 || + symbol.flags & 3 /* Variable */ && (getDeclarationNodeFlagsFromSymbol(symbol) & 2 /* Const */) !== 0 || + symbol.flags & 98304 /* Accessor */ && !(symbol.flags & 65536 /* SetAccessor */) || + (symbol.flags & 8 /* EnumMember */) !== 0; } - function checkMissingDeclaration(node) { - checkDecorators(node); + function isReferenceToReadonlyEntity(expr, symbol) { + if (isReadonlySymbol(symbol)) { + // Allow assignments to readonly properties within constructors of the same class declaration. + if (symbol.flags & 4 /* Property */ && + (expr.kind === 172 /* PropertyAccessExpression */ || expr.kind === 173 /* ElementAccessExpression */) && + expr.expression.kind === 97 /* ThisKeyword */) { + // Look for if this is the constructor for the class that `symbol` is a property of. + var func = ts.getContainingFunction(expr); + if (!(func && func.kind === 148 /* Constructor */)) + return true; + // If func.parent is a class and symbol is a (readonly) property of that class, or + // if func is a constructor and symbol is a (readonly) parameter property declared in it, + // then symbol is writeable here. + return !(func.parent === symbol.valueDeclaration.parent || func === symbol.valueDeclaration.parent); + } + return true; + } + return false; } - function checkTypeArgumentConstraints(typeParameters, typeArgumentNodes) { - var typeArguments; - var mapper; - var result = true; - for (var i = 0; i < typeParameters.length; i++) { - var constraint = getConstraintOfTypeParameter(typeParameters[i]); - if (constraint) { - if (!typeArguments) { - typeArguments = ts.map(typeArgumentNodes, getTypeFromTypeNodeNoAlias); - mapper = createTypeMapper(typeParameters, typeArguments); + function isReferenceThroughNamespaceImport(expr) { + if (expr.kind === 172 /* PropertyAccessExpression */ || expr.kind === 173 /* ElementAccessExpression */) { + var node = skipParenthesizedNodes(expr.expression); + if (node.kind === 69 /* Identifier */) { + var symbol = getNodeLinks(node).resolvedSymbol; + if (symbol.flags & 8388608 /* Alias */) { + var declaration = getDeclarationOfAliasSymbol(symbol); + return declaration && declaration.kind === 232 /* NamespaceImport */; } - var typeArgument = typeArguments[i]; - result = result && checkTypeAssignableTo(typeArgument, getTypeWithThisArgument(instantiateType(constraint, mapper), typeArgument), typeArgumentNodes[i], ts.Diagnostics.Type_0_does_not_satisfy_the_constraint_1); } } - return result; + return false; } - function checkTypeReferenceNode(node) { - checkGrammarTypeArguments(node, node.typeArguments); - var type = getTypeFromTypeReference(node); - if (type !== unknownType) { - if (node.typeArguments) { - // Do type argument local checks only if referenced type is successfully resolved - ts.forEach(node.typeArguments, checkSourceElement); - if (produceDiagnostics) { - var symbol = getNodeLinks(node).resolvedSymbol; - var typeParameters = symbol.flags & 524288 /* TypeAlias */ ? getSymbolLinks(symbol).typeParameters : type.target.localTypeParameters; - checkTypeArgumentConstraints(typeParameters, node.typeArguments); + function checkReferenceExpression(expr, invalidReferenceMessage, constantVariableMessage) { + // References are combinations of identifiers, parentheses, and property accesses. + var node = skipParenthesizedNodes(expr); + if (node.kind !== 69 /* Identifier */ && node.kind !== 172 /* PropertyAccessExpression */ && node.kind !== 173 /* ElementAccessExpression */) { + error(expr, invalidReferenceMessage); + return false; + } + // Because we get the symbol from the resolvedSymbol property, it might be of kind + // SymbolFlags.ExportValue. In this case it is necessary to get the actual export + // symbol, which will have the correct flags set on it. + var links = getNodeLinks(node); + var symbol = getExportSymbolOfValueSymbolIfExported(links.resolvedSymbol); + if (symbol) { + if (symbol !== unknownSymbol && symbol !== argumentsSymbol) { + // Only variables (and not functions, classes, namespaces, enum objects, or enum members) + // are considered references when referenced using a simple identifier. + if (node.kind === 69 /* Identifier */ && !(symbol.flags & 3 /* Variable */)) { + error(expr, invalidReferenceMessage); + return false; + } + if (isReferenceToReadonlyEntity(node, symbol) || isReferenceThroughNamespaceImport(node)) { + error(expr, constantVariableMessage); + return false; } } - if (type.flags & 16 /* Enum */ && !type.memberTypes && getNodeLinks(node).resolvedSymbol.flags & 8 /* EnumMember */) { - error(node, ts.Diagnostics.Enum_type_0_has_members_with_initializers_that_are_not_literals, typeToString(type)); + } + else if (node.kind === 173 /* ElementAccessExpression */) { + if (links.resolvedIndexInfo && links.resolvedIndexInfo.isReadonly) { + error(expr, constantVariableMessage); + return false; } } + return true; } - function checkTypeQuery(node) { - getTypeFromTypeQueryNode(node); + function checkDeleteExpression(node) { + checkExpression(node.expression); + return booleanType; } - function checkTypeLiteral(node) { - ts.forEach(node.members, checkSourceElement); - if (produceDiagnostics) { - var type = getTypeFromTypeLiteralOrFunctionOrConstructorTypeNode(node); - checkIndexConstraints(type); - checkTypeForDuplicateIndexSignatures(node); - checkObjectTypeForDuplicateDeclarations(node); - } + function checkTypeOfExpression(node) { + checkExpression(node.expression); + return stringType; } - function checkArrayType(node) { - checkSourceElement(node.elementType); + function checkVoidExpression(node) { + checkExpression(node.expression); + return undefinedWideningType; } - function checkTupleType(node) { + function checkAwaitExpression(node) { // Grammar checking - var hasErrorFromDisallowedTrailingComma = checkGrammarForDisallowedTrailingComma(node.elementTypes); - if (!hasErrorFromDisallowedTrailingComma && node.elementTypes.length === 0) { - grammarErrorOnNode(node, ts.Diagnostics.A_tuple_type_element_list_cannot_be_empty); + if (produceDiagnostics) { + if (!(node.flags & 262144 /* AwaitContext */)) { + grammarErrorOnFirstToken(node, ts.Diagnostics.await_expression_is_only_allowed_within_an_async_function); + } + if (isInParameterInitializerBeforeContainingFunction(node)) { + error(node, ts.Diagnostics.await_expressions_cannot_be_used_in_a_parameter_initializer); + } } - ts.forEach(node.elementTypes, checkSourceElement); - } - function checkUnionOrIntersectionType(node) { - ts.forEach(node.types, checkSourceElement); - } - function isPrivateWithinAmbient(node) { - return (ts.getModifierFlags(node) & 8 /* Private */) && ts.isInAmbientContext(node); + var operandType = checkExpression(node.expression); + return checkAwaitedType(operandType, node); } - function getEffectiveDeclarationFlags(n, flagsToCheck) { - var flags = ts.getCombinedModifierFlags(n); - // children of classes (even ambient classes) should not be marked as ambient or export - // because those flags have no useful semantics there. - if (n.parent.kind !== 222 /* InterfaceDeclaration */ && - n.parent.kind !== 221 /* ClassDeclaration */ && - n.parent.kind !== 192 /* ClassExpression */ && - ts.isInAmbientContext(n)) { - if (!(flags & 2 /* Ambient */)) { - // It is nested in an ambient context, which means it is automatically exported - flags |= 1 /* Export */; - } - flags |= 2 /* Ambient */; + function checkPrefixUnaryExpression(node) { + var operandType = checkExpression(node.operand); + if (operandType === silentNeverType) { + return silentNeverType; } - return flags & flagsToCheck; - } - function checkFunctionOrConstructorSymbol(symbol) { - if (!produceDiagnostics) { - return; + if (node.operator === 36 /* MinusToken */ && node.operand.kind === 8 /* NumericLiteral */) { + return getFreshTypeOfLiteralType(getLiteralTypeForText(64 /* NumberLiteral */, "" + -node.operand.text)); } - function getCanonicalOverload(overloads, implementation) { - // Consider the canonical set of flags to be the flags of the bodyDeclaration or the first declaration - // Error on all deviations from this canonical set of flags - // The caveat is that if some overloads are defined in lib.d.ts, we don't want to - // report the errors on those. To achieve this, we will say that the implementation is - // the canonical signature only if it is in the same container as the first overload - var implementationSharesContainerWithFirstOverload = implementation !== undefined && implementation.parent === overloads[0].parent; - return implementationSharesContainerWithFirstOverload ? implementation : overloads[0]; + switch (node.operator) { + case 35 /* PlusToken */: + case 36 /* MinusToken */: + case 50 /* TildeToken */: + if (maybeTypeOfKind(operandType, 512 /* ESSymbol */)) { + error(node.operand, ts.Diagnostics.The_0_operator_cannot_be_applied_to_type_symbol, ts.tokenToString(node.operator)); + } + return numberType; + case 49 /* ExclamationToken */: + var facts = getTypeFacts(operandType) & (1048576 /* Truthy */ | 2097152 /* Falsy */); + return facts === 1048576 /* Truthy */ ? falseType : + facts === 2097152 /* Falsy */ ? trueType : + booleanType; + case 41 /* PlusPlusToken */: + case 42 /* MinusMinusToken */: + var ok = checkArithmeticOperandType(node.operand, getNonNullableType(operandType), ts.Diagnostics.An_arithmetic_operand_must_be_of_type_any_number_or_an_enum_type); + if (ok) { + // run check only if former checks succeeded to avoid reporting cascading errors + checkReferenceExpression(node.operand, ts.Diagnostics.The_operand_of_an_increment_or_decrement_operator_must_be_a_variable_property_or_indexer, ts.Diagnostics.The_operand_of_an_increment_or_decrement_operator_cannot_be_a_constant_or_a_read_only_property); + } + return numberType; } - function checkFlagAgreementBetweenOverloads(overloads, implementation, flagsToCheck, someOverloadFlags, allOverloadFlags) { - // Error if some overloads have a flag that is not shared by all overloads. To find the - // deviations, we XOR someOverloadFlags with allOverloadFlags - var someButNotAllOverloadFlags = someOverloadFlags ^ allOverloadFlags; - if (someButNotAllOverloadFlags !== 0) { - var canonicalFlags_1 = getEffectiveDeclarationFlags(getCanonicalOverload(overloads, implementation), flagsToCheck); - ts.forEach(overloads, function (o) { - var deviation = getEffectiveDeclarationFlags(o, flagsToCheck) ^ canonicalFlags_1; - if (deviation & 1 /* Export */) { - error(o.name, ts.Diagnostics.Overload_signatures_must_all_be_exported_or_non_exported); - } - else if (deviation & 2 /* Ambient */) { - error(o.name, ts.Diagnostics.Overload_signatures_must_all_be_ambient_or_non_ambient); - } - else if (deviation & (8 /* Private */ | 16 /* Protected */)) { - error(o.name || o, ts.Diagnostics.Overload_signatures_must_all_be_public_private_or_protected); - } - else if (deviation & 128 /* Abstract */) { - error(o.name, ts.Diagnostics.Overload_signatures_must_all_be_abstract_or_non_abstract); - } - }); - } + return unknownType; + } + function checkPostfixUnaryExpression(node) { + var operandType = checkExpression(node.operand); + if (operandType === silentNeverType) { + return silentNeverType; } - function checkQuestionTokenAgreementBetweenOverloads(overloads, implementation, someHaveQuestionToken, allHaveQuestionToken) { - if (someHaveQuestionToken !== allHaveQuestionToken) { - var canonicalHasQuestionToken_1 = ts.hasQuestionToken(getCanonicalOverload(overloads, implementation)); - ts.forEach(overloads, function (o) { - var deviation = ts.hasQuestionToken(o) !== canonicalHasQuestionToken_1; - if (deviation) { - error(o.name, ts.Diagnostics.Overload_signatures_must_all_be_optional_or_required); - } - }); - } + var ok = checkArithmeticOperandType(node.operand, getNonNullableType(operandType), ts.Diagnostics.An_arithmetic_operand_must_be_of_type_any_number_or_an_enum_type); + if (ok) { + // run check only if former checks succeeded to avoid reporting cascading errors + checkReferenceExpression(node.operand, ts.Diagnostics.The_operand_of_an_increment_or_decrement_operator_must_be_a_variable_property_or_indexer, ts.Diagnostics.The_operand_of_an_increment_or_decrement_operator_cannot_be_a_constant_or_a_read_only_property); } - var flagsToCheck = 1 /* Export */ | 2 /* Ambient */ | 8 /* Private */ | 16 /* Protected */ | 128 /* Abstract */; - var someNodeFlags = 0 /* None */; - var allNodeFlags = flagsToCheck; - var someHaveQuestionToken = false; - var allHaveQuestionToken = true; - var hasOverloads = false; - var bodyDeclaration; - var lastSeenNonAmbientDeclaration; - var previousDeclaration; - var declarations = symbol.declarations; - var isConstructor = (symbol.flags & 16384 /* Constructor */) !== 0; - function reportImplementationExpectedError(node) { - if (node.name && ts.nodeIsMissing(node.name)) { - return; - } - var seen = false; - var subsequentNode = ts.forEachChild(node.parent, function (c) { - if (seen) { - return c; - } - else { - seen = c === node; - } - }); - // We may be here because of some extra nodes between overloads that could not be parsed into a valid node. - // In this case the subsequent node is not really consecutive (.pos !== node.end), and we must ignore it here. - if (subsequentNode && subsequentNode.pos === node.end) { - if (subsequentNode.kind === node.kind) { - var errorNode_1 = subsequentNode.name || subsequentNode; - // TODO(jfreeman): These are methods, so handle computed name case - if (node.name && subsequentNode.name && node.name.text === subsequentNode.name.text) { - var reportError = (node.kind === 147 /* MethodDeclaration */ || node.kind === 146 /* MethodSignature */) && - (ts.getModifierFlags(node) & 32 /* Static */) !== (ts.getModifierFlags(subsequentNode) & 32 /* Static */); - // we can get here in two cases - // 1. mixed static and instance class members - // 2. something with the same name was defined before the set of overloads that prevents them from merging - // here we'll report error only for the first case since for second we should already report error in binder - if (reportError) { - var diagnostic = ts.getModifierFlags(node) & 32 /* Static */ ? ts.Diagnostics.Function_overload_must_be_static : ts.Diagnostics.Function_overload_must_not_be_static; - error(errorNode_1, diagnostic); - } - return; - } - else if (ts.nodeIsPresent(subsequentNode.body)) { - error(errorNode_1, ts.Diagnostics.Function_implementation_name_must_be_0, ts.declarationNameToString(node.name)); - return; - } + return numberType; + } + // Return true if type might be of the given kind. A union or intersection type might be of a given + // kind if at least one constituent type is of the given kind. + function maybeTypeOfKind(type, kind) { + if (type.flags & kind) { + return true; + } + if (type.flags & 1572864 /* UnionOrIntersection */) { + var types = type.types; + for (var _i = 0, types_14 = types; _i < types_14.length; _i++) { + var t = types_14[_i]; + if (maybeTypeOfKind(t, kind)) { + return true; } } - var errorNode = node.name || node; - if (isConstructor) { - error(errorNode, ts.Diagnostics.Constructor_implementation_is_missing); - } - else { - // Report different errors regarding non-consecutive blocks of declarations depending on whether - // the node in question is abstract. - if (ts.getModifierFlags(node) & 128 /* Abstract */) { - error(errorNode, ts.Diagnostics.All_declarations_of_an_abstract_method_must_be_consecutive); - } - else { - error(errorNode, ts.Diagnostics.Function_implementation_is_missing_or_not_immediately_following_the_declaration); + } + return false; + } + // Return true if type is of the given kind. A union type is of a given kind if all constituent types + // are of the given kind. An intersection type is of a given kind if at least one constituent type is + // of the given kind. + function isTypeOfKind(type, kind) { + if (type.flags & kind) { + return true; + } + if (type.flags & 524288 /* Union */) { + var types = type.types; + for (var _i = 0, types_15 = types; _i < types_15.length; _i++) { + var t = types_15[_i]; + if (!isTypeOfKind(t, kind)) { + return false; } } + return true; } - var duplicateFunctionDeclaration = false; - var multipleConstructorImplementation = false; - for (var _i = 0, declarations_4 = declarations; _i < declarations_4.length; _i++) { - var current = declarations_4[_i]; - var node = current; - var inAmbientContext = ts.isInAmbientContext(node); - var inAmbientContextOrInterface = node.parent.kind === 222 /* InterfaceDeclaration */ || node.parent.kind === 159 /* TypeLiteral */ || inAmbientContext; - if (inAmbientContextOrInterface) { - // check if declarations are consecutive only if they are non-ambient - // 1. ambient declarations can be interleaved - // i.e. this is legal - // declare function foo(); - // declare function bar(); - // declare function foo(); - // 2. mixing ambient and non-ambient declarations is a separate error that will be reported - do not want to report an extra one - previousDeclaration = undefined; - } - if (node.kind === 220 /* FunctionDeclaration */ || node.kind === 147 /* MethodDeclaration */ || node.kind === 146 /* MethodSignature */ || node.kind === 148 /* Constructor */) { - var currentNodeFlags = getEffectiveDeclarationFlags(node, flagsToCheck); - someNodeFlags |= currentNodeFlags; - allNodeFlags &= currentNodeFlags; - someHaveQuestionToken = someHaveQuestionToken || ts.hasQuestionToken(node); - allHaveQuestionToken = allHaveQuestionToken && ts.hasQuestionToken(node); - if (ts.nodeIsPresent(node.body) && bodyDeclaration) { - if (isConstructor) { - multipleConstructorImplementation = true; - } - else { - duplicateFunctionDeclaration = true; - } - } - else if (previousDeclaration && previousDeclaration.parent === node.parent && previousDeclaration.end !== node.pos) { - reportImplementationExpectedError(previousDeclaration); - } - if (ts.nodeIsPresent(node.body)) { - if (!bodyDeclaration) { - bodyDeclaration = node; - } - } - else { - hasOverloads = true; - } - previousDeclaration = node; - if (!inAmbientContextOrInterface) { - lastSeenNonAmbientDeclaration = node; + if (type.flags & 1048576 /* Intersection */) { + var types = type.types; + for (var _a = 0, types_16 = types; _a < types_16.length; _a++) { + var t = types_16[_a]; + if (isTypeOfKind(t, kind)) { + return true; } } } - if (multipleConstructorImplementation) { - ts.forEach(declarations, function (declaration) { - error(declaration, ts.Diagnostics.Multiple_constructor_implementations_are_not_allowed); - }); - } - if (duplicateFunctionDeclaration) { - ts.forEach(declarations, function (declaration) { - error(declaration.name, ts.Diagnostics.Duplicate_function_implementation); - }); + return false; + } + function isConstEnumObjectType(type) { + return type.flags & (2588672 /* ObjectType */ | 2097152 /* Anonymous */) && type.symbol && isConstEnumSymbol(type.symbol); + } + function isConstEnumSymbol(symbol) { + return (symbol.flags & 128 /* ConstEnum */) !== 0; + } + function checkInstanceOfExpression(left, right, leftType, rightType) { + if (leftType === silentNeverType || rightType === silentNeverType) { + return silentNeverType; } - // Abstract methods can't have an implementation -- in particular, they don't need one. - if (lastSeenNonAmbientDeclaration && !lastSeenNonAmbientDeclaration.body && - !(ts.getModifierFlags(lastSeenNonAmbientDeclaration) & 128 /* Abstract */) && !lastSeenNonAmbientDeclaration.questionToken) { - reportImplementationExpectedError(lastSeenNonAmbientDeclaration); + // TypeScript 1.0 spec (April 2014): 4.15.4 + // The instanceof operator requires the left operand to be of type Any, an object type, or a type parameter type, + // and the right operand to be of type Any or a subtype of the 'Function' interface type. + // The result is always of the Boolean primitive type. + // NOTE: do not raise error if leftType is unknown as related error was already reported + if (isTypeOfKind(leftType, 8190 /* Primitive */)) { + error(left, ts.Diagnostics.The_left_hand_side_of_an_instanceof_expression_must_be_of_type_any_an_object_type_or_a_type_parameter); } - if (hasOverloads) { - checkFlagAgreementBetweenOverloads(declarations, bodyDeclaration, flagsToCheck, someNodeFlags, allNodeFlags); - checkQuestionTokenAgreementBetweenOverloads(declarations, bodyDeclaration, someHaveQuestionToken, allHaveQuestionToken); - if (bodyDeclaration) { - var signatures = getSignaturesOfSymbol(symbol); - var bodySignature = getSignatureFromDeclaration(bodyDeclaration); - for (var _a = 0, signatures_3 = signatures; _a < signatures_3.length; _a++) { - var signature = signatures_3[_a]; - if (!isImplementationCompatibleWithOverload(bodySignature, signature)) { - error(signature.declaration, ts.Diagnostics.Overload_signature_is_not_compatible_with_function_implementation); - break; - } - } - } + // NOTE: do not raise error if right is unknown as related error was already reported + if (!(isTypeAny(rightType) || isTypeSubtypeOf(rightType, globalFunctionType))) { + error(right, ts.Diagnostics.The_right_hand_side_of_an_instanceof_expression_must_be_of_type_any_or_of_a_type_assignable_to_the_Function_interface_type); } + return booleanType; } - function checkExportsOnMergedDeclarations(node) { - if (!produceDiagnostics) { - return; + function checkInExpression(left, right, leftType, rightType) { + if (leftType === silentNeverType || rightType === silentNeverType) { + return silentNeverType; } - // if localSymbol is defined on node then node itself is exported - check is required - var symbol = node.localSymbol; - if (!symbol) { - // local symbol is undefined => this declaration is non-exported. - // however symbol might contain other declarations that are exported - symbol = getSymbolOfNode(node); - if (!(symbol.flags & 7340032 /* Export */)) { - // this is a pure local symbol (all declarations are non-exported) - no need to check anything - return; - } + // TypeScript 1.0 spec (April 2014): 4.15.5 + // The in operator requires the left operand to be of type Any, the String primitive type, or the Number primitive type, + // and the right operand to be of type Any, an object type, or a type parameter type. + // The result is always of the Boolean primitive type. + if (!isTypeAnyOrAllConstituentTypesHaveKind(leftType, 34 /* StringLike */ | 340 /* NumberLike */ | 512 /* ESSymbol */)) { + error(left, ts.Diagnostics.The_left_hand_side_of_an_in_expression_must_be_of_type_any_string_number_or_symbol); } - // run the check only for the first declaration in the list - if (ts.getDeclarationOfKind(symbol, node.kind) !== node) { - return; + if (!isTypeAnyOrAllConstituentTypesHaveKind(rightType, 2588672 /* ObjectType */ | 16384 /* TypeParameter */)) { + error(right, ts.Diagnostics.The_right_hand_side_of_an_in_expression_must_be_of_type_any_an_object_type_or_a_type_parameter); } - // we use SymbolFlags.ExportValue, SymbolFlags.ExportType and SymbolFlags.ExportNamespace - // to denote disjoint declarationSpaces (without making new enum type). - var exportedDeclarationSpaces = 0 /* None */; - var nonExportedDeclarationSpaces = 0 /* None */; - var defaultExportedDeclarationSpaces = 0 /* None */; - for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { - var d = _a[_i]; - var declarationSpaces = getDeclarationSpaces(d); - var effectiveDeclarationFlags = getEffectiveDeclarationFlags(d, 1 /* Export */ | 512 /* Default */); - if (effectiveDeclarationFlags & 1 /* Export */) { - if (effectiveDeclarationFlags & 512 /* Default */) { - defaultExportedDeclarationSpaces |= declarationSpaces; + return booleanType; + } + function checkObjectLiteralAssignment(node, sourceType, contextualMapper) { + var properties = node.properties; + for (var _i = 0, properties_4 = properties; _i < properties_4.length; _i++) { + var p = properties_4[_i]; + checkObjectLiteralDestructuringPropertyAssignment(sourceType, p, contextualMapper); + } + return sourceType; + } + function checkObjectLiteralDestructuringPropertyAssignment(objectLiteralType, property, contextualMapper) { + if (property.kind === 253 /* PropertyAssignment */ || property.kind === 254 /* ShorthandPropertyAssignment */) { + var name_17 = property.name; + if (name_17.kind === 140 /* ComputedPropertyName */) { + checkComputedPropertyName(name_17); + } + if (isComputedNonLiteralName(name_17)) { + return undefined; + } + var text = getTextOfPropertyName(name_17); + var type = isTypeAny(objectLiteralType) + ? objectLiteralType + : getTypeOfPropertyOfType(objectLiteralType, text) || + isNumericLiteralName(text) && getIndexTypeOfType(objectLiteralType, 1 /* Number */) || + getIndexTypeOfType(objectLiteralType, 0 /* String */); + if (type) { + if (property.kind === 254 /* ShorthandPropertyAssignment */) { + return checkDestructuringAssignment(property, type); } else { - exportedDeclarationSpaces |= declarationSpaces; + // non-shorthand property assignments should always have initializers + return checkDestructuringAssignment(property.initializer, type); } } else { - nonExportedDeclarationSpaces |= declarationSpaces; + error(name_17, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(objectLiteralType), ts.declarationNameToString(name_17)); } } - // Spaces for anything not declared a 'default export'. - var nonDefaultExportedDeclarationSpaces = exportedDeclarationSpaces | nonExportedDeclarationSpaces; - var commonDeclarationSpacesForExportsAndLocals = exportedDeclarationSpaces & nonExportedDeclarationSpaces; - var commonDeclarationSpacesForDefaultAndNonDefault = defaultExportedDeclarationSpaces & nonDefaultExportedDeclarationSpaces; - if (commonDeclarationSpacesForExportsAndLocals || commonDeclarationSpacesForDefaultAndNonDefault) { - // declaration spaces for exported and non-exported declarations intersect - for (var _b = 0, _c = symbol.declarations; _b < _c.length; _b++) { - var d = _c[_b]; - var declarationSpaces = getDeclarationSpaces(d); - // Only error on the declarations that contributed to the intersecting spaces. - if (declarationSpaces & commonDeclarationSpacesForDefaultAndNonDefault) { - error(d.name, ts.Diagnostics.Merged_declaration_0_cannot_include_a_default_export_declaration_Consider_adding_a_separate_export_default_0_declaration_instead, ts.declarationNameToString(d.name)); - } - else if (declarationSpaces & commonDeclarationSpacesForExportsAndLocals) { - error(d.name, ts.Diagnostics.Individual_declarations_in_merged_declaration_0_must_be_all_exported_or_all_local, ts.declarationNameToString(d.name)); - } - } + else { + error(property, ts.Diagnostics.Property_assignment_expected); } - function getDeclarationSpaces(d) { - switch (d.kind) { - case 222 /* InterfaceDeclaration */: - return 2097152 /* ExportType */; - case 225 /* ModuleDeclaration */: - return ts.isAmbientModule(d) || ts.getModuleInstanceState(d) !== 0 /* NonInstantiated */ - ? 4194304 /* ExportNamespace */ | 1048576 /* ExportValue */ - : 4194304 /* ExportNamespace */; - case 221 /* ClassDeclaration */: - case 224 /* EnumDeclaration */: - return 2097152 /* ExportType */ | 1048576 /* ExportValue */; - case 229 /* ImportEqualsDeclaration */: - var result_2 = 0; - var target = resolveAlias(getSymbolOfNode(d)); - ts.forEach(target.declarations, function (d) { result_2 |= getDeclarationSpaces(d); }); - return result_2; - default: - return 1048576 /* ExportValue */; - } + } + function checkArrayLiteralAssignment(node, sourceType, contextualMapper) { + // This elementType will be used if the specific property corresponding to this index is not + // present (aka the tuple element property). This call also checks that the parentType is in + // fact an iterable or array (depending on target language). + var elementType = checkIteratedTypeOrElementType(sourceType, node, /*allowStringInput*/ false) || unknownType; + var elements = node.elements; + for (var i = 0; i < elements.length; i++) { + checkArrayLiteralDestructuringElementAssignment(node, sourceType, i, elementType, contextualMapper); } + return sourceType; } - function checkNonThenableType(type, location, message) { - type = getWidenedType(type); - if (!isTypeAny(type) && !isTypeNever(type) && isTypeAssignableTo(type, getGlobalThenableType())) { - if (location) { - if (!message) { - message = ts.Diagnostics.Operand_for_await_does_not_have_a_valid_callable_then_member; + function checkArrayLiteralDestructuringElementAssignment(node, sourceType, elementIndex, elementType, contextualMapper) { + var elements = node.elements; + var element = elements[elementIndex]; + if (element.kind !== 193 /* OmittedExpression */) { + if (element.kind !== 191 /* SpreadElementExpression */) { + var propName = "" + elementIndex; + var type = isTypeAny(sourceType) + ? sourceType + : isTupleLikeType(sourceType) + ? getTypeOfPropertyOfType(sourceType, propName) + : elementType; + if (type) { + return checkDestructuringAssignment(element, type, contextualMapper); + } + else { + // We still need to check element expression here because we may need to set appropriate flag on the expression + // such as NodeCheckFlags.LexicalThis on "this"expression. + checkExpression(element); + if (isTupleType(sourceType)) { + error(element, ts.Diagnostics.Tuple_type_0_with_length_1_cannot_be_assigned_to_tuple_with_length_2, typeToString(sourceType), getTypeReferenceArity(sourceType), elements.length); + } + else { + error(element, ts.Diagnostics.Type_0_has_no_property_1, typeToString(sourceType), propName); + } + } + } + else { + if (elementIndex < elements.length - 1) { + error(element, ts.Diagnostics.A_rest_element_must_be_last_in_an_array_destructuring_pattern); + } + else { + var restExpression = element.expression; + if (restExpression.kind === 187 /* BinaryExpression */ && restExpression.operatorToken.kind === 56 /* EqualsToken */) { + error(restExpression.operatorToken, ts.Diagnostics.A_rest_element_cannot_have_an_initializer); + } + else { + return checkDestructuringAssignment(restExpression, createArrayType(elementType), contextualMapper); + } } - error(location, message); } - return unknownType; } - return type; + return undefined; } - /** - * Gets the "promised type" of a promise. - * @param type The type of the promise. - * @remarks The "promised type" of a type is the type of the "value" parameter of the "onfulfilled" callback. - */ - function getPromisedType(promise) { - // - // { // promise - // then( // thenFunction - // onfulfilled: ( // onfulfilledParameterType - // value: T // valueParameterType - // ) => any - // ): any; - // } - // - if (isTypeAny(promise)) { - return undefined; - } - if (promise.flags & 131072 /* Reference */) { - if (promise.target === tryGetGlobalPromiseType() - || promise.target === getGlobalPromiseLikeType()) { - return promise.typeArguments[0]; + function checkDestructuringAssignment(exprOrAssignment, sourceType, contextualMapper) { + var target; + if (exprOrAssignment.kind === 254 /* ShorthandPropertyAssignment */) { + var prop = exprOrAssignment; + if (prop.objectAssignmentInitializer) { + // In strict null checking mode, if a default value of a non-undefined type is specified, remove + // undefined from the final type. + if (strictNullChecks && + !(getFalsyFlags(checkExpression(prop.objectAssignmentInitializer)) & 2048 /* Undefined */)) { + sourceType = getTypeWithFacts(sourceType, 131072 /* NEUndefined */); + } + checkBinaryLikeExpression(prop.name, prop.equalsToken, prop.objectAssignmentInitializer, contextualMapper); } + target = exprOrAssignment.name; } - var globalPromiseLikeType = getInstantiatedGlobalPromiseLikeType(); - if (globalPromiseLikeType === emptyObjectType || !isTypeAssignableTo(promise, globalPromiseLikeType)) { - return undefined; + else { + target = exprOrAssignment; } - var thenFunction = getTypeOfPropertyOfType(promise, "then"); - if (!thenFunction || isTypeAny(thenFunction)) { - return undefined; + if (target.kind === 187 /* BinaryExpression */ && target.operatorToken.kind === 56 /* EqualsToken */) { + checkBinaryExpression(target, contextualMapper); + target = target.left; } - var thenSignatures = getSignaturesOfType(thenFunction, 0 /* Call */); - if (thenSignatures.length === 0) { - return undefined; + if (target.kind === 171 /* ObjectLiteralExpression */) { + return checkObjectLiteralAssignment(target, sourceType, contextualMapper); } - var onfulfilledParameterType = getTypeWithFacts(getUnionType(ts.map(thenSignatures, getTypeOfFirstParameterOfSignature)), 131072 /* NEUndefined */); - if (isTypeAny(onfulfilledParameterType)) { - return undefined; + if (target.kind === 170 /* ArrayLiteralExpression */) { + return checkArrayLiteralAssignment(target, sourceType, contextualMapper); } - var onfulfilledParameterSignatures = getSignaturesOfType(onfulfilledParameterType, 0 /* Call */); - if (onfulfilledParameterSignatures.length === 0) { - return undefined; + return checkReferenceAssignment(target, sourceType, contextualMapper); + } + function checkReferenceAssignment(target, sourceType, contextualMapper) { + var targetType = checkExpression(target, contextualMapper); + if (checkReferenceExpression(target, ts.Diagnostics.Invalid_left_hand_side_of_assignment_expression, ts.Diagnostics.Left_hand_side_of_assignment_expression_cannot_be_a_constant_or_a_read_only_property)) { + checkTypeAssignableTo(sourceType, targetType, target, /*headMessage*/ undefined); } - return getUnionType(ts.map(onfulfilledParameterSignatures, getTypeOfFirstParameterOfSignature), /*subtypeReduction*/ true); - } - function getTypeOfFirstParameterOfSignature(signature) { - return signature.parameters.length > 0 ? getTypeAtPosition(signature, 0) : neverType; + return sourceType; } /** - * Gets the "awaited type" of a type. - * @param type The type to await. - * @remarks The "awaited type" of an expression is its "promised type" if the expression is a - * Promise-like type; otherwise, it is the type of the expression. This is used to reflect - * The runtime behavior of the `await` keyword. - */ - function getAwaitedType(type) { - return checkAwaitedType(type, /*location*/ undefined, /*message*/ undefined); + * This is a *shallow* check: An expression is side-effect-free if the + * evaluation of the expression *itself* cannot produce side effects. + * For example, x++ / 3 is side-effect free because the / operator + * does not have side effects. + * The intent is to "smell test" an expression for correctness in positions where + * its value is discarded (e.g. the left side of the comma operator). + */ + function isSideEffectFree(node) { + node = ts.skipParentheses(node); + switch (node.kind) { + case 69 /* Identifier */: + case 9 /* StringLiteral */: + case 10 /* RegularExpressionLiteral */: + case 176 /* TaggedTemplateExpression */: + case 189 /* TemplateExpression */: + case 11 /* NoSubstitutionTemplateLiteral */: + case 8 /* NumericLiteral */: + case 99 /* TrueKeyword */: + case 84 /* FalseKeyword */: + case 93 /* NullKeyword */: + case 135 /* UndefinedKeyword */: + case 179 /* FunctionExpression */: + case 192 /* ClassExpression */: + case 180 /* ArrowFunction */: + case 170 /* ArrayLiteralExpression */: + case 171 /* ObjectLiteralExpression */: + case 182 /* TypeOfExpression */: + case 196 /* NonNullExpression */: + case 242 /* JsxSelfClosingElement */: + case 241 /* JsxElement */: + return true; + case 188 /* ConditionalExpression */: + return isSideEffectFree(node.whenTrue) && + isSideEffectFree(node.whenFalse); + case 187 /* BinaryExpression */: + if (ts.isAssignmentOperator(node.operatorToken.kind)) { + return false; + } + return isSideEffectFree(node.left) && + isSideEffectFree(node.right); + case 185 /* PrefixUnaryExpression */: + case 186 /* PostfixUnaryExpression */: + // Unary operators ~, !, +, and - have no side effects. + // The rest do. + switch (node.operator) { + case 49 /* ExclamationToken */: + case 35 /* PlusToken */: + case 36 /* MinusToken */: + case 50 /* TildeToken */: + return true; + } + return false; + // Some forms listed here for clarity + case 183 /* VoidExpression */: // Explicit opt-out + case 177 /* TypeAssertionExpression */: // Not SEF, but can produce useful type warnings + case 195 /* AsExpression */: // Not SEF, but can produce useful type warnings + default: + return false; + } } - function checkAwaitedType(type, location, message) { - return checkAwaitedTypeWorker(type); - function checkAwaitedTypeWorker(type) { - if (type.flags & 524288 /* Union */) { - var types = []; - for (var _i = 0, _a = type.types; _i < _a.length; _i++) { - var constituentType = _a[_i]; - types.push(checkAwaitedTypeWorker(constituentType)); + function isTypeEqualityComparableTo(source, target) { + return (target.flags & 6144 /* Nullable */) !== 0 || isTypeComparableTo(source, target); + } + function getBestChoiceType(type1, type2) { + var firstAssignableToSecond = isTypeAssignableTo(type1, type2); + var secondAssignableToFirst = isTypeAssignableTo(type2, type1); + return secondAssignableToFirst && !firstAssignableToSecond ? type1 : + firstAssignableToSecond && !secondAssignableToFirst ? type2 : + getUnionType([type1, type2], /*subtypeReduction*/ true); + } + function checkBinaryExpression(node, contextualMapper) { + return checkBinaryLikeExpression(node.left, node.operatorToken, node.right, contextualMapper, node); + } + function checkBinaryLikeExpression(left, operatorToken, right, contextualMapper, errorNode) { + var operator = operatorToken.kind; + if (operator === 56 /* EqualsToken */ && (left.kind === 171 /* ObjectLiteralExpression */ || left.kind === 170 /* ArrayLiteralExpression */)) { + return checkDestructuringAssignment(left, checkExpression(right, contextualMapper), contextualMapper); + } + var leftType = checkExpression(left, contextualMapper); + var rightType = checkExpression(right, contextualMapper); + switch (operator) { + case 37 /* AsteriskToken */: + case 38 /* AsteriskAsteriskToken */: + case 59 /* AsteriskEqualsToken */: + case 60 /* AsteriskAsteriskEqualsToken */: + case 39 /* SlashToken */: + case 61 /* SlashEqualsToken */: + case 40 /* PercentToken */: + case 62 /* PercentEqualsToken */: + case 36 /* MinusToken */: + case 58 /* MinusEqualsToken */: + case 43 /* LessThanLessThanToken */: + case 63 /* LessThanLessThanEqualsToken */: + case 44 /* GreaterThanGreaterThanToken */: + case 64 /* GreaterThanGreaterThanEqualsToken */: + case 45 /* GreaterThanGreaterThanGreaterThanToken */: + case 65 /* GreaterThanGreaterThanGreaterThanEqualsToken */: + case 47 /* BarToken */: + case 67 /* BarEqualsToken */: + case 48 /* CaretToken */: + case 68 /* CaretEqualsToken */: + case 46 /* AmpersandToken */: + case 66 /* AmpersandEqualsToken */: + if (leftType === silentNeverType || rightType === silentNeverType) { + return silentNeverType; } - return getUnionType(types, /*subtypeReduction*/ true); - } - else { - var promisedType = getPromisedType(type); - if (promisedType === undefined) { - // The type was not a PromiseLike, so it could not be unwrapped any further. - // As long as the type does not have a callable "then" property, it is - // safe to return the type; otherwise, an error will have been reported in - // the call to checkNonThenableType and we will return unknownType. - // - // An example of a non-promise "thenable" might be: - // - // await { then(): void {} } - // - // The "thenable" does not match the minimal definition for a PromiseLike. When - // a Promise/A+-compatible or ES6 promise tries to adopt this value, the promise - // will never settle. We treat this as an error to help flag an early indicator - // of a runtime problem. If the user wants to return this value from an async - // function, they would need to wrap it in some other value. If they want it to - // be treated as a promise, they can cast to . - return checkNonThenableType(type, location, message); + // TypeScript 1.0 spec (April 2014): 4.19.1 + // These operators require their operands to be of type Any, the Number primitive type, + // or an enum type. Operands of an enum type are treated + // as having the primitive type Number. If one operand is the null or undefined value, + // it is treated as having the type of the other operand. + // The result is always of the Number primitive type. + if (leftType.flags & 6144 /* Nullable */) + leftType = rightType; + if (rightType.flags & 6144 /* Nullable */) + rightType = leftType; + leftType = getNonNullableType(leftType); + rightType = getNonNullableType(rightType); + var suggestedOperator = void 0; + // if a user tries to apply a bitwise operator to 2 boolean operands + // try and return them a helpful suggestion + if ((leftType.flags & 136 /* BooleanLike */) && + (rightType.flags & 136 /* BooleanLike */) && + (suggestedOperator = getSuggestedBooleanOperator(operatorToken.kind)) !== undefined) { + error(errorNode || operatorToken, ts.Diagnostics.The_0_operator_is_not_allowed_for_boolean_types_Consider_using_1_instead, ts.tokenToString(operatorToken.kind), ts.tokenToString(suggestedOperator)); } else { - if (type.id === promisedType.id || ts.indexOf(awaitedTypeStack, promisedType.id) >= 0) { - // We have a bad actor in the form of a promise whose promised type is - // the same promise type, or a mutually recursive promise. Return the - // unknown type as we cannot guess the shape. If this were the actual - // case in the JavaScript, this Promise would never resolve. - // - // An example of a bad actor with a singly-recursive promise type might - // be: - // - // interface BadPromise { - // then( - // onfulfilled: (value: BadPromise) => any, - // onrejected: (error: any) => any): BadPromise; - // } - // - // The above interface will pass the PromiseLike check, and return a - // promised type of `BadPromise`. Since this is a self reference, we - // don't want to keep recursing ad infinitum. - // - // An example of a bad actor in the form of a mutually-recursive - // promise type might be: - // - // interface BadPromiseA { - // then( - // onfulfilled: (value: BadPromiseB) => any, - // onrejected: (error: any) => any): BadPromiseB; - // } - // - // interface BadPromiseB { - // then( - // onfulfilled: (value: BadPromiseA) => any, - // onrejected: (error: any) => any): BadPromiseA; - // } - // - if (location) { - error(location, ts.Diagnostics._0_is_referenced_directly_or_indirectly_in_the_fulfillment_callback_of_its_own_then_method, symbolToString(type.symbol)); - } - return unknownType; + // otherwise just check each operand separately and report errors as normal + var leftOk = checkArithmeticOperandType(left, leftType, ts.Diagnostics.The_left_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_or_an_enum_type); + var rightOk = checkArithmeticOperandType(right, rightType, ts.Diagnostics.The_right_hand_side_of_an_arithmetic_operation_must_be_of_type_any_number_or_an_enum_type); + if (leftOk && rightOk) { + checkAssignmentOperator(numberType); } - // Keep track of the type we're about to unwrap to avoid bad recursive promise types. - // See the comments above for more information. - awaitedTypeStack.push(type.id); - var awaitedType = checkAwaitedTypeWorker(promisedType); - awaitedTypeStack.pop(); - return awaitedType; } + return numberType; + case 35 /* PlusToken */: + case 57 /* PlusEqualsToken */: + if (leftType === silentNeverType || rightType === silentNeverType) { + return silentNeverType; + } + // TypeScript 1.0 spec (April 2014): 4.19.2 + // The binary + operator requires both operands to be of the Number primitive type or an enum type, + // or at least one of the operands to be of type Any or the String primitive type. + // If one operand is the null or undefined value, it is treated as having the type of the other operand. + if (leftType.flags & 6144 /* Nullable */) + leftType = rightType; + if (rightType.flags & 6144 /* Nullable */) + rightType = leftType; + leftType = getNonNullableType(leftType); + rightType = getNonNullableType(rightType); + var resultType = void 0; + if (isTypeOfKind(leftType, 340 /* NumberLike */) && isTypeOfKind(rightType, 340 /* NumberLike */)) { + // Operands of an enum type are treated as having the primitive type Number. + // If both operands are of the Number primitive type, the result is of the Number primitive type. + resultType = numberType; + } + else { + if (isTypeOfKind(leftType, 34 /* StringLike */) || isTypeOfKind(rightType, 34 /* StringLike */)) { + // If one or both operands are of the String primitive type, the result is of the String primitive type. + resultType = stringType; + } + else if (isTypeAny(leftType) || isTypeAny(rightType)) { + // Otherwise, the result is of type Any. + // NOTE: unknown type here denotes error type. Old compiler treated this case as any type so do we. + resultType = leftType === unknownType || rightType === unknownType ? unknownType : anyType; + } + // Symbols are not allowed at all in arithmetic expressions + if (resultType && !checkForDisallowedESSymbolOperand(operator)) { + return resultType; + } + } + if (!resultType) { + reportOperatorError(); + return anyType; + } + if (operator === 57 /* PlusEqualsToken */) { + checkAssignmentOperator(resultType); + } + return resultType; + case 25 /* LessThanToken */: + case 27 /* GreaterThanToken */: + case 28 /* LessThanEqualsToken */: + case 29 /* GreaterThanEqualsToken */: + if (checkForDisallowedESSymbolOperand(operator)) { + if (!isTypeComparableTo(leftType, rightType) && !isTypeComparableTo(rightType, leftType)) { + reportOperatorError(); + } + } + return booleanType; + case 30 /* EqualsEqualsToken */: + case 31 /* ExclamationEqualsToken */: + case 32 /* EqualsEqualsEqualsToken */: + case 33 /* ExclamationEqualsEqualsToken */: + var leftIsLiteral = isLiteralType(leftType); + var rightIsLiteral = isLiteralType(rightType); + if (!leftIsLiteral || !rightIsLiteral) { + leftType = leftIsLiteral ? getBaseTypeOfLiteralType(leftType) : leftType; + rightType = rightIsLiteral ? getBaseTypeOfLiteralType(rightType) : rightType; + } + if (!isTypeEqualityComparableTo(leftType, rightType) && !isTypeEqualityComparableTo(rightType, leftType)) { + reportOperatorError(); + } + return booleanType; + case 91 /* InstanceOfKeyword */: + return checkInstanceOfExpression(left, right, leftType, rightType); + case 90 /* InKeyword */: + return checkInExpression(left, right, leftType, rightType); + case 51 /* AmpersandAmpersandToken */: + return getTypeFacts(leftType) & 1048576 /* Truthy */ ? + includeFalsyTypes(rightType, getFalsyFlags(strictNullChecks ? leftType : getBaseTypeOfLiteralType(rightType))) : + leftType; + case 52 /* BarBarToken */: + return getTypeFacts(leftType) & 2097152 /* Falsy */ ? + getBestChoiceType(removeDefinitelyFalsyTypes(leftType), rightType) : + leftType; + case 56 /* EqualsToken */: + checkAssignmentOperator(rightType); + return getRegularTypeOfObjectLiteral(rightType); + case 24 /* CommaToken */: + if (!compilerOptions.allowUnreachableCode && isSideEffectFree(left)) { + error(left, ts.Diagnostics.Left_side_of_comma_operator_is_unused_and_has_no_side_effects); + } + return rightType; + } + // Return true if there was no error, false if there was an error. + function checkForDisallowedESSymbolOperand(operator) { + var offendingSymbolOperand = maybeTypeOfKind(leftType, 512 /* ESSymbol */) ? left : + maybeTypeOfKind(rightType, 512 /* ESSymbol */) ? right : + undefined; + if (offendingSymbolOperand) { + error(offendingSymbolOperand, ts.Diagnostics.The_0_operator_cannot_be_applied_to_type_symbol, ts.tokenToString(operator)); + return false; } + return true; } - } - /** - * Checks that the return type provided is an instantiation of the global Promise type - * and returns the awaited type of the return type. - * - * @param returnType The return type of a FunctionLikeDeclaration - * @param location The node on which to report the error. - */ - function checkCorrectPromiseType(returnType, location, diagnostic, typeName) { - if (returnType === unknownType) { - // The return type already had some other error, so we ignore and return - // the unknown type. - return unknownType; + function getSuggestedBooleanOperator(operator) { + switch (operator) { + case 47 /* BarToken */: + case 67 /* BarEqualsToken */: + return 52 /* BarBarToken */; + case 48 /* CaretToken */: + case 68 /* CaretEqualsToken */: + return 33 /* ExclamationEqualsEqualsToken */; + case 46 /* AmpersandToken */: + case 66 /* AmpersandEqualsToken */: + return 51 /* AmpersandAmpersandToken */; + default: + return undefined; + } } - var globalPromiseType = getGlobalPromiseType(); - if (globalPromiseType === emptyGenericType - || globalPromiseType === getTargetType(returnType)) { - // Either we couldn't resolve the global promise type, which would have already - // reported an error, or we could resolve it and the return type is a valid type - // reference to the global type. In either case, we return the awaited type for - // the return type. - return checkAwaitedType(returnType, location, ts.Diagnostics.An_async_function_or_method_must_have_a_valid_awaitable_return_type); + function checkAssignmentOperator(valueType) { + if (produceDiagnostics && operator >= 56 /* FirstAssignment */ && operator <= 68 /* LastAssignment */) { + // TypeScript 1.0 spec (April 2014): 4.17 + // An assignment of the form + // VarExpr = ValueExpr + // requires VarExpr to be classified as a reference + // A compound assignment furthermore requires VarExpr to be classified as a reference (section 4.1) + // and the type of the non - compound operation to be assignable to the type of VarExpr. + var ok = checkReferenceExpression(left, ts.Diagnostics.Invalid_left_hand_side_of_assignment_expression, ts.Diagnostics.Left_hand_side_of_assignment_expression_cannot_be_a_constant_or_a_read_only_property); + // Use default messages + if (ok) { + // to avoid cascading errors check assignability only if 'isReference' check succeeded and no errors were reported + checkTypeAssignableTo(valueType, leftType, left, /*headMessage*/ undefined); + } + } } - // The promise type was not a valid type reference to the global promise type, so we - // report an error and return the unknown type. - error(location, diagnostic, typeName); - return unknownType; - } - /** - * Checks the return type of an async function to ensure it is a compatible - * Promise implementation. - * @param node The signature to check - * @param returnType The return type for the function - * @remarks - * This checks that an async function has a valid Promise-compatible return type, - * and returns the *awaited type* of the promise. An async function has a valid - * Promise-compatible return type if the resolved value of the return type has a - * construct signature that takes in an `initializer` function that in turn supplies - * a `resolve` function as one of its arguments and results in an object with a - * callable `then` signature. - */ - function checkAsyncFunctionReturnType(node) { - if (languageVersion >= 2 /* ES6 */) { - var returnType = getTypeFromTypeNode(node.type); - return checkCorrectPromiseType(returnType, node.type, ts.Diagnostics.The_return_type_of_an_async_function_or_method_must_be_the_global_Promise_T_type); + function reportOperatorError() { + error(errorNode || operatorToken, ts.Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2, ts.tokenToString(operatorToken.kind), typeToString(leftType), typeToString(rightType)); } - var globalPromiseConstructorLikeType = getGlobalPromiseConstructorLikeType(); - if (globalPromiseConstructorLikeType === emptyObjectType) { - // If we couldn't resolve the global PromiseConstructorLike type we cannot verify - // compatibility with __awaiter. - return unknownType; + } + function isYieldExpressionInClass(node) { + var current = node; + var parent = node.parent; + while (parent) { + if (ts.isFunctionLike(parent) && current === parent.body) { + return false; + } + else if (ts.isClassLike(current)) { + return true; + } + current = parent; + parent = parent.parent; } - // As part of our emit for an async function, we will need to emit the entity name of - // the return type annotation as an expression. To meet the necessary runtime semantics - // for __awaiter, we must also check that the type of the declaration (e.g. the static - // side or "constructor" of the promise type) is compatible `PromiseConstructorLike`. - // - // An example might be (from lib.es6.d.ts): - // - // interface Promise { ... } - // interface PromiseConstructor { - // new (...): Promise; - // } - // declare var Promise: PromiseConstructor; - // - // When an async function declares a return type annotation of `Promise`, we - // need to get the type of the `Promise` variable declaration above, which would - // be `PromiseConstructor`. - // - // The same case applies to a class: - // - // declare class Promise { - // constructor(...); - // then(...): Promise; - // } - // - // When we get the type of the `Promise` symbol here, we get the type of the static - // side of the `Promise` class, which would be `{ new (...): Promise }`. - var promiseType = getTypeFromTypeNode(node.type); - if (promiseType === unknownType && compilerOptions.isolatedModules) { - // If we are compiling with isolatedModules, we may not be able to resolve the - // type as a value. As such, we will just return unknownType; - return unknownType; + return false; + } + function checkYieldExpression(node) { + // Grammar checking + if (produceDiagnostics) { + if (!(node.flags & 65536 /* YieldContext */) || isYieldExpressionInClass(node)) { + grammarErrorOnFirstToken(node, ts.Diagnostics.A_yield_expression_is_only_allowed_in_a_generator_body); + } + if (isInParameterInitializerBeforeContainingFunction(node)) { + error(node, ts.Diagnostics.yield_expressions_cannot_be_used_in_a_parameter_initializer); + } } - var promiseConstructor = getNodeLinks(node.type).resolvedSymbol; - if (!promiseConstructor || !symbolIsValue(promiseConstructor)) { - // try to fall back to global promise type. - var typeName = promiseConstructor - ? symbolToString(promiseConstructor) - : typeToString(promiseType); - return checkCorrectPromiseType(promiseType, node.type, ts.Diagnostics.Type_0_is_not_a_valid_async_function_return_type, typeName); + if (node.expression) { + var func = ts.getContainingFunction(node); + // If the user's code is syntactically correct, the func should always have a star. After all, + // we are in a yield context. + if (func && func.asteriskToken) { + var expressionType = checkExpressionCached(node.expression, /*contextualMapper*/ undefined); + var expressionElementType = void 0; + var nodeIsYieldStar = !!node.asteriskToken; + if (nodeIsYieldStar) { + expressionElementType = checkElementTypeOfIterable(expressionType, node.expression); + } + // There is no point in doing an assignability check if the function + // has no explicit return type because the return type is directly computed + // from the yield expressions. + if (func.type) { + var signatureElementType = getElementTypeOfIterableIterator(getTypeFromTypeNode(func.type)) || anyType; + if (nodeIsYieldStar) { + checkTypeAssignableTo(expressionElementType, signatureElementType, node.expression, /*headMessage*/ undefined); + } + else { + checkTypeAssignableTo(expressionType, signatureElementType, node.expression, /*headMessage*/ undefined); + } + } + } } - // If the Promise constructor, resolved locally, is an alias symbol we should mark it as referenced. - checkReturnTypeAnnotationAsExpression(node); - // Validate the promise constructor type. - var promiseConstructorType = getTypeOfSymbol(promiseConstructor); - if (!checkTypeAssignableTo(promiseConstructorType, globalPromiseConstructorLikeType, node.type, ts.Diagnostics.Type_0_is_not_a_valid_async_function_return_type)) { - return unknownType; + // Both yield and yield* expressions have type 'any' + return anyType; + } + function checkConditionalExpression(node, contextualMapper) { + checkExpression(node.condition); + var type1 = checkExpression(node.whenTrue, contextualMapper); + var type2 = checkExpression(node.whenFalse, contextualMapper); + return getBestChoiceType(type1, type2); + } + function checkLiteralExpression(node) { + if (node.kind === 8 /* NumericLiteral */) { + checkGrammarNumericLiteral(node); } - // Verify there is no local declaration that could collide with the promise constructor. - var promiseName = ts.getEntityNameFromTypeNode(node.type); - var promiseNameOrNamespaceRoot = getFirstIdentifier(promiseName); - var rootSymbol = getSymbol(node.locals, promiseNameOrNamespaceRoot.text, 107455 /* Value */); - if (rootSymbol) { - error(rootSymbol.valueDeclaration, ts.Diagnostics.Duplicate_identifier_0_Compiler_uses_declaration_1_to_support_async_functions, promiseNameOrNamespaceRoot.text, getFullyQualifiedName(promiseConstructor)); - return unknownType; + switch (node.kind) { + case 9 /* StringLiteral */: + return getFreshTypeOfLiteralType(getLiteralTypeForText(32 /* StringLiteral */, node.text)); + case 8 /* NumericLiteral */: + return getFreshTypeOfLiteralType(getLiteralTypeForText(64 /* NumberLiteral */, node.text)); + case 99 /* TrueKeyword */: + return trueType; + case 84 /* FalseKeyword */: + return falseType; } - // Get and return the awaited type of the return type. - return checkAwaitedType(promiseType, node, ts.Diagnostics.An_async_function_or_method_must_have_a_valid_awaitable_return_type); } - /** Check a decorator */ - function checkDecorator(node) { - var signature = getResolvedSignature(node); - var returnType = getReturnTypeOfSignature(signature); - if (returnType.flags & 1 /* Any */) { - return; - } - var expectedReturnType; - var headMessage = getDiagnosticHeadMessageForDecoratorResolution(node); - var errorInfo; - switch (node.parent.kind) { - case 221 /* ClassDeclaration */: - var classSymbol = getSymbolOfNode(node.parent); - var classConstructorType = getTypeOfSymbol(classSymbol); - expectedReturnType = getUnionType([classConstructorType, voidType]); - break; - case 142 /* Parameter */: - expectedReturnType = voidType; - errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.The_return_type_of_a_parameter_decorator_function_must_be_either_void_or_any); - break; - case 145 /* PropertyDeclaration */: - expectedReturnType = voidType; - errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.The_return_type_of_a_property_decorator_function_must_be_either_void_or_any); - break; - case 147 /* MethodDeclaration */: - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - var methodType = getTypeOfNode(node.parent); - var descriptorType = createTypedPropertyDescriptorType(methodType); - expectedReturnType = getUnionType([descriptorType, voidType]); - break; + function checkTemplateExpression(node) { + // We just want to check each expressions, but we are unconcerned with + // the type of each expression, as any value may be coerced into a string. + // It is worth asking whether this is what we really want though. + // A place where we actually *are* concerned with the expressions' types are + // in tagged templates. + ts.forEach(node.templateSpans, function (templateSpan) { + checkExpression(templateSpan.expression); + }); + return stringType; + } + function checkExpressionWithContextualType(node, contextualType, contextualMapper) { + var saveContextualType = node.contextualType; + node.contextualType = contextualType; + var result = checkExpression(node, contextualMapper); + node.contextualType = saveContextualType; + return result; + } + function checkExpressionCached(node, contextualMapper) { + var links = getNodeLinks(node); + if (!links.resolvedType) { + // When computing a type that we're going to cache, we need to ignore any ongoing control flow + // analysis because variables may have transient types in indeterminable states. Moving flowLoopStart + // to the top of the stack ensures all transient types are computed from a known point. + var saveFlowLoopStart = flowLoopStart; + flowLoopStart = flowLoopCount; + links.resolvedType = checkExpression(node, contextualMapper); + flowLoopStart = saveFlowLoopStart; } - checkTypeAssignableTo(returnType, expectedReturnType, node, headMessage, errorInfo); + return links.resolvedType; } - /** Checks a type reference node as an expression. */ - function checkTypeNodeAsExpression(node) { - // When we are emitting type metadata for decorators, we need to try to check the type - // as if it were an expression so that we can emit the type in a value position when we - // serialize the type metadata. - if (node && node.kind === 155 /* TypeReference */) { - var root = getFirstIdentifier(node.typeName); - var meaning = root.parent.kind === 155 /* TypeReference */ ? 793064 /* Type */ : 1920 /* Namespace */; - // Resolve type so we know which symbol is referenced - var rootSymbol = resolveName(root, root.text, meaning | 8388608 /* Alias */, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined); - // Resolved symbol is alias - if (rootSymbol && rootSymbol.flags & 8388608 /* Alias */) { - var aliasTarget = resolveAlias(rootSymbol); - // If alias has value symbol - mark alias as referenced - if (aliasTarget.flags & 107455 /* Value */ && !isConstEnumOrConstEnumOnlyModule(resolveAlias(rootSymbol))) { - markAliasSymbolAsReferenced(rootSymbol); + function isTypeAssertion(node) { + node = skipParenthesizedNodes(node); + return node.kind === 177 /* TypeAssertionExpression */ || node.kind === 195 /* AsExpression */; + } + function checkDeclarationInitializer(declaration) { + var type = checkExpressionCached(declaration.initializer); + return ts.getCombinedNodeFlags(declaration) & 2 /* Const */ || + ts.getCombinedModifierFlags(declaration) & 64 /* Readonly */ || + isTypeAssertion(declaration.initializer) ? type : getWidenedLiteralType(type); + } + function isLiteralContextualType(contextualType) { + if (contextualType) { + if (contextualType.flags & 16384 /* TypeParameter */) { + var apparentType = getApparentTypeOfTypeParameter(contextualType); + // If the type parameter is constrained to the base primitive type we're checking for, + // consider this a literal context. For example, given a type parameter 'T extends string', + // this causes us to infer string literal types for T. + if (apparentType.flags & (2 /* String */ | 4 /* Number */ | 8 /* Boolean */ | 16 /* Enum */)) { + return true; } + contextualType = apparentType; } + return maybeTypeOfKind(contextualType, 480 /* Literal */); } + return false; } - /** - * Checks the type annotation of an accessor declaration or property declaration as - * an expression if it is a type reference to a type with a value declaration. - */ - function checkTypeAnnotationAsExpression(node) { - checkTypeNodeAsExpression(node.type); + function checkExpressionForMutableLocation(node, contextualMapper) { + var type = checkExpression(node, contextualMapper); + return isTypeAssertion(node) || isLiteralContextualType(getContextualType(node)) ? type : getWidenedLiteralType(type); } - function checkReturnTypeAnnotationAsExpression(node) { - checkTypeNodeAsExpression(node.type); + function checkPropertyAssignment(node, contextualMapper) { + // Do not use hasDynamicName here, because that returns false for well known symbols. + // We want to perform checkComputedPropertyName for all computed properties, including + // well known symbols. + if (node.name.kind === 140 /* ComputedPropertyName */) { + checkComputedPropertyName(node.name); + } + return checkExpressionForMutableLocation(node.initializer, contextualMapper); } - /** Checks the type annotation of the parameters of a function/method or the constructor of a class as expressions */ - function checkParameterTypeAnnotationsAsExpressions(node) { - // ensure all type annotations with a value declaration are checked as an expression - for (var _i = 0, _a = node.parameters; _i < _a.length; _i++) { - var parameter = _a[_i]; - checkTypeAnnotationAsExpression(parameter); + function checkObjectLiteralMethod(node, contextualMapper) { + // Grammar checking + checkGrammarMethod(node); + // Do not use hasDynamicName here, because that returns false for well known symbols. + // We want to perform checkComputedPropertyName for all computed properties, including + // well known symbols. + if (node.name.kind === 140 /* ComputedPropertyName */) { + checkComputedPropertyName(node.name); } + var uninstantiatedType = checkFunctionExpressionOrObjectLiteralMethod(node, contextualMapper); + return instantiateTypeWithSingleGenericCallSignature(node, uninstantiatedType, contextualMapper); } - /** Check the decorators of a node */ - function checkDecorators(node) { - if (!node.decorators) { - return; + function instantiateTypeWithSingleGenericCallSignature(node, type, contextualMapper) { + if (isInferentialContext(contextualMapper)) { + var signature = getSingleCallSignature(type); + if (signature && signature.typeParameters) { + var contextualType = getApparentTypeOfContextualType(node); + if (contextualType) { + var contextualSignature = getSingleCallSignature(contextualType); + if (contextualSignature && !contextualSignature.typeParameters) { + return getOrCreateTypeFromSignature(instantiateSignatureInContextOf(signature, contextualSignature, contextualMapper)); + } + } + } } - // skip this check for nodes that cannot have decorators. These should have already had an error reported by - // checkGrammarDecorators. - if (!ts.nodeCanBeDecorated(node)) { - return; + return type; + } + // Checks an expression and returns its type. The contextualMapper parameter serves two purposes: When + // contextualMapper is not undefined and not equal to the identityMapper function object it indicates that the + // expression is being inferentially typed (section 4.15.2 in spec) and provides the type mapper to use in + // conjunction with the generic contextual type. When contextualMapper is equal to the identityMapper function + // object, it serves as an indicator that all contained function and arrow expressions should be considered to + // have the wildcard function type; this form of type check is used during overload resolution to exclude + // contextually typed function and arrow expressions in the initial phase. + function checkExpression(node, contextualMapper) { + var type; + if (node.kind === 139 /* QualifiedName */) { + type = checkQualifiedName(node); } - if (!compilerOptions.experimentalDecorators) { - error(node, ts.Diagnostics.Experimental_support_for_decorators_is_a_feature_that_is_subject_to_change_in_a_future_release_Set_the_experimentalDecorators_option_to_remove_this_warning); + else { + var uninstantiatedType = checkExpressionWorker(node, contextualMapper); + type = instantiateTypeWithSingleGenericCallSignature(node, uninstantiatedType, contextualMapper); } - if (compilerOptions.emitDecoratorMetadata) { - // we only need to perform these checks if we are emitting serialized type metadata for the target of a decorator. - switch (node.kind) { - case 221 /* ClassDeclaration */: - var constructor = ts.getFirstConstructorWithBody(node); - if (constructor) { - checkParameterTypeAnnotationsAsExpressions(constructor); - } - break; - case 147 /* MethodDeclaration */: - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - checkParameterTypeAnnotationsAsExpressions(node); - checkReturnTypeAnnotationAsExpression(node); - break; - case 145 /* PropertyDeclaration */: - case 142 /* Parameter */: - checkTypeAnnotationAsExpression(node); - break; + if (isConstEnumObjectType(type)) { + // enum object type for const enums are only permitted in: + // - 'left' in property access + // - 'object' in indexed access + // - target in rhs of import statement + var ok = (node.parent.kind === 172 /* PropertyAccessExpression */ && node.parent.expression === node) || + (node.parent.kind === 173 /* ElementAccessExpression */ && node.parent.expression === node) || + ((node.kind === 69 /* Identifier */ || node.kind === 139 /* QualifiedName */) && isInRightSideOfImportOrExportAssignment(node)); + if (!ok) { + error(node, ts.Diagnostics.const_enums_can_only_be_used_in_property_or_index_access_expressions_or_the_right_hand_side_of_an_import_declaration_or_export_assignment); } } - ts.forEach(node.decorators, checkDecorator); + return type; } - function checkFunctionDeclaration(node) { - if (produceDiagnostics) { - checkFunctionOrMethodDeclaration(node) || checkGrammarForGenerator(node); - checkCollisionWithCapturedSuperVariable(node, node.name); - checkCollisionWithCapturedThisVariable(node, node.name); - checkCollisionWithRequireExportsInGeneratedCode(node, node.name); - checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); + function checkExpressionWorker(node, contextualMapper) { + switch (node.kind) { + case 69 /* Identifier */: + return checkIdentifier(node); + case 97 /* ThisKeyword */: + return checkThisExpression(node); + case 95 /* SuperKeyword */: + return checkSuperExpression(node); + case 93 /* NullKeyword */: + return nullWideningType; + case 9 /* StringLiteral */: + case 8 /* NumericLiteral */: + case 99 /* TrueKeyword */: + case 84 /* FalseKeyword */: + return checkLiteralExpression(node); + case 189 /* TemplateExpression */: + return checkTemplateExpression(node); + case 11 /* NoSubstitutionTemplateLiteral */: + return stringType; + case 10 /* RegularExpressionLiteral */: + return globalRegExpType; + case 170 /* ArrayLiteralExpression */: + return checkArrayLiteral(node, contextualMapper); + case 171 /* ObjectLiteralExpression */: + return checkObjectLiteral(node, contextualMapper); + case 172 /* PropertyAccessExpression */: + return checkPropertyAccessExpression(node); + case 173 /* ElementAccessExpression */: + return checkIndexedAccess(node); + case 174 /* CallExpression */: + case 175 /* NewExpression */: + return checkCallExpression(node); + case 176 /* TaggedTemplateExpression */: + return checkTaggedTemplateExpression(node); + case 178 /* ParenthesizedExpression */: + return checkExpression(node.expression, contextualMapper); + case 192 /* ClassExpression */: + return checkClassExpression(node); + case 179 /* FunctionExpression */: + case 180 /* ArrowFunction */: + return checkFunctionExpressionOrObjectLiteralMethod(node, contextualMapper); + case 182 /* TypeOfExpression */: + return checkTypeOfExpression(node); + case 177 /* TypeAssertionExpression */: + case 195 /* AsExpression */: + return checkAssertion(node); + case 196 /* NonNullExpression */: + return checkNonNullAssertion(node); + case 181 /* DeleteExpression */: + return checkDeleteExpression(node); + case 183 /* VoidExpression */: + return checkVoidExpression(node); + case 184 /* AwaitExpression */: + return checkAwaitExpression(node); + case 185 /* PrefixUnaryExpression */: + return checkPrefixUnaryExpression(node); + case 186 /* PostfixUnaryExpression */: + return checkPostfixUnaryExpression(node); + case 187 /* BinaryExpression */: + return checkBinaryExpression(node, contextualMapper); + case 188 /* ConditionalExpression */: + return checkConditionalExpression(node, contextualMapper); + case 191 /* SpreadElementExpression */: + return checkSpreadElementExpression(node, contextualMapper); + case 193 /* OmittedExpression */: + return undefinedWideningType; + case 190 /* YieldExpression */: + return checkYieldExpression(node); + case 248 /* JsxExpression */: + return checkJsxExpression(node); + case 241 /* JsxElement */: + return checkJsxElement(node); + case 242 /* JsxSelfClosingElement */: + return checkJsxSelfClosingElement(node); + case 243 /* JsxOpeningElement */: + ts.Debug.fail("Shouldn't ever directly check a JsxOpeningElement"); } + return unknownType; } - function checkFunctionOrMethodDeclaration(node) { - checkDecorators(node); - checkSignatureDeclaration(node); - var isAsync = ts.isAsyncFunctionLike(node); - // Do not use hasDynamicName here, because that returns false for well known symbols. - // We want to perform checkComputedPropertyName for all computed properties, including - // well known symbols. - if (node.name && node.name.kind === 140 /* ComputedPropertyName */) { - // This check will account for methods in class/interface declarations, - // as well as accessors in classes/object literals - checkComputedPropertyName(node.name); + // DECLARATION AND STATEMENT TYPE CHECKING + function checkTypeParameter(node) { + // Grammar Checking + if (node.expression) { + grammarErrorOnFirstToken(node.expression, ts.Diagnostics.Type_expected); } - if (!ts.hasDynamicName(node)) { - // first we want to check the local symbol that contain this declaration - // - if node.localSymbol !== undefined - this is current declaration is exported and localSymbol points to the local symbol - // - if node.localSymbol === undefined - this node is non-exported so we can just pick the result of getSymbolOfNode - var symbol = getSymbolOfNode(node); - var localSymbol = node.localSymbol || symbol; - // Since the javascript won't do semantic analysis like typescript, - // if the javascript file comes before the typescript file and both contain same name functions, - // checkFunctionOrConstructorSymbol wouldn't be called if we didnt ignore javascript function. - var firstDeclaration = ts.forEach(localSymbol.declarations, - // Get first non javascript function declaration - function (declaration) { return declaration.kind === node.kind && !ts.isSourceFileJavaScript(ts.getSourceFileOfNode(declaration)) ? - declaration : undefined; }); - // Only type check the symbol once - if (node === firstDeclaration) { - checkFunctionOrConstructorSymbol(localSymbol); - } - if (symbol.parent) { - // run check once for the first declaration - if (ts.getDeclarationOfKind(symbol, node.kind) === node) { - // run check on export symbol to check that modifiers agree across all exported declarations - checkFunctionOrConstructorSymbol(symbol); - } + checkSourceElement(node.constraint); + getConstraintOfTypeParameter(getDeclaredTypeOfTypeParameter(getSymbolOfNode(node))); + if (produceDiagnostics) { + checkTypeNameIsReserved(node.name, ts.Diagnostics.Type_parameter_name_cannot_be_0); + } + } + function checkParameter(node) { + // Grammar checking + // It is a SyntaxError if the Identifier "eval" or the Identifier "arguments" occurs as the + // Identifier in a PropertySetParameterList of a PropertyAssignment that is contained in strict code + // or if its FunctionBody is strict code(11.1.5). + // Grammar checking + checkGrammarDecorators(node) || checkGrammarModifiers(node); + checkVariableLikeDeclaration(node); + var func = ts.getContainingFunction(node); + if (ts.getModifierFlags(node) & 92 /* ParameterPropertyModifier */) { + func = ts.getContainingFunction(node); + if (!(func.kind === 148 /* Constructor */ && ts.nodeIsPresent(func.body))) { + error(node, ts.Diagnostics.A_parameter_property_is_only_allowed_in_a_constructor_implementation); } } - checkSourceElement(node.body); - if (!node.asteriskToken) { - var returnOrPromisedType = node.type && (isAsync ? checkAsyncFunctionReturnType(node) : getTypeFromTypeNode(node.type)); - checkAllCodePathsInNonVoidFunctionReturnOrThrow(node, returnOrPromisedType); + if (node.questionToken && ts.isBindingPattern(node.name) && func.body) { + error(node, ts.Diagnostics.A_binding_pattern_parameter_cannot_be_optional_in_an_implementation_signature); } - if (produceDiagnostics && !node.type) { - // Report an implicit any error if there is no body, no explicit return type, and node is not a private method - // in an ambient context - if (compilerOptions.noImplicitAny && ts.nodeIsMissing(node.body) && !isPrivateWithinAmbient(node)) { - reportImplicitAnyError(node, anyType); + if (node.name.text === "this") { + if (ts.indexOf(func.parameters, node) !== 0) { + error(node, ts.Diagnostics.A_this_parameter_must_be_the_first_parameter); } - if (node.asteriskToken && ts.nodeIsPresent(node.body)) { - // A generator with a body and no type annotation can still cause errors. It can error if the - // yielded values have no common supertype, or it can give an implicit any error if it has no - // yielded values. The only way to trigger these errors is to try checking its return type. - getReturnTypeOfSignature(getSignatureFromDeclaration(node)); + if (func.kind === 148 /* Constructor */ || func.kind === 152 /* ConstructSignature */ || func.kind === 157 /* ConstructorType */) { + error(node, ts.Diagnostics.A_constructor_cannot_have_a_this_parameter); } } - registerForUnusedIdentifiersCheck(node); + // Only check rest parameter type if it's not a binding pattern. Since binding patterns are + // not allowed in a rest parameter, we already have an error from checkGrammarParameterList. + if (node.dotDotDotToken && !ts.isBindingPattern(node.name) && !isArrayType(getTypeOfSymbol(node.symbol))) { + error(node, ts.Diagnostics.A_rest_parameter_must_be_of_an_array_type); + } } - function registerForUnusedIdentifiersCheck(node) { - if (deferredUnusedIdentifierNodes) { - deferredUnusedIdentifierNodes.push(node); + function isSyntacticallyValidGenerator(node) { + if (!node.asteriskToken || !node.body) { + return false; } + return node.kind === 147 /* MethodDeclaration */ || + node.kind === 220 /* FunctionDeclaration */ || + node.kind === 179 /* FunctionExpression */; } - function checkUnusedIdentifiers() { - if (deferredUnusedIdentifierNodes) { - for (var _i = 0, deferredUnusedIdentifierNodes_1 = deferredUnusedIdentifierNodes; _i < deferredUnusedIdentifierNodes_1.length; _i++) { - var node = deferredUnusedIdentifierNodes_1[_i]; - switch (node.kind) { - case 256 /* SourceFile */: - case 225 /* ModuleDeclaration */: - checkUnusedModuleMembers(node); - break; - case 221 /* ClassDeclaration */: - case 192 /* ClassExpression */: - checkUnusedClassMembers(node); - checkUnusedTypeParameters(node); - break; - case 222 /* InterfaceDeclaration */: - checkUnusedTypeParameters(node); - break; - case 199 /* Block */: - case 227 /* CaseBlock */: - case 206 /* ForStatement */: - case 207 /* ForInStatement */: - case 208 /* ForOfStatement */: - checkUnusedLocalsAndParameters(node); - break; - case 148 /* Constructor */: - case 179 /* FunctionExpression */: - case 220 /* FunctionDeclaration */: - case 180 /* ArrowFunction */: - case 147 /* MethodDeclaration */: - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - if (node.body) { - checkUnusedLocalsAndParameters(node); - } - checkUnusedTypeParameters(node); - break; - case 146 /* MethodSignature */: - case 151 /* CallSignature */: - case 152 /* ConstructSignature */: - case 153 /* IndexSignature */: - case 156 /* FunctionType */: - case 157 /* ConstructorType */: - checkUnusedTypeParameters(node); - break; + function getTypePredicateParameterIndex(parameterList, parameter) { + if (parameterList) { + for (var i = 0; i < parameterList.length; i++) { + var param = parameterList[i]; + if (param.name.kind === 69 /* Identifier */ && + param.name.text === parameter.text) { + return i; } - ; } } + return -1; } - function checkUnusedLocalsAndParameters(node) { - if (node.parent.kind !== 222 /* InterfaceDeclaration */ && noUnusedIdentifiers && !ts.isInAmbientContext(node)) { - var _loop_1 = function (key) { - var local = node.locals[key]; - if (!local.isReferenced) { - if (local.valueDeclaration && local.valueDeclaration.kind === 142 /* Parameter */) { - var parameter = local.valueDeclaration; - if (compilerOptions.noUnusedParameters && - !ts.isParameterPropertyDeclaration(parameter) && - !parameterIsThisKeyword(parameter) && - !parameterNameStartsWithUnderscore(parameter)) { - error(local.valueDeclaration.name, ts.Diagnostics._0_is_declared_but_never_used, local.name); - } - } - else if (compilerOptions.noUnusedLocals) { - ts.forEach(local.declarations, function (d) { return error(d.name || d, ts.Diagnostics._0_is_declared_but_never_used, local.name); }); - } + function checkTypePredicate(node) { + var parent = getTypePredicateParent(node); + if (!parent) { + // The parent must not be valid. + error(node, ts.Diagnostics.A_type_predicate_is_only_allowed_in_return_type_position_for_functions_and_methods); + return; + } + var typePredicate = getSignatureFromDeclaration(parent).typePredicate; + if (!typePredicate) { + return; + } + var parameterName = node.parameterName; + if (ts.isThisTypePredicate(typePredicate)) { + getTypeFromThisTypeNode(parameterName); + } + else { + if (typePredicate.parameterIndex >= 0) { + if (parent.parameters[typePredicate.parameterIndex].dotDotDotToken) { + error(parameterName, ts.Diagnostics.A_type_predicate_cannot_reference_a_rest_parameter); + } + else { + var leadingError = ts.chainDiagnosticMessages(undefined, ts.Diagnostics.A_type_predicate_s_type_must_be_assignable_to_its_parameter_s_type); + checkTypeAssignableTo(typePredicate.type, getTypeOfNode(parent.parameters[typePredicate.parameterIndex]), node.type, + /*headMessage*/ undefined, leadingError); } - }; - for (var key in node.locals) { - _loop_1(key); } - } - } - function parameterIsThisKeyword(parameter) { - return parameter.name && parameter.name.originalKeywordKind === 97 /* ThisKeyword */; - } - function parameterNameStartsWithUnderscore(parameter) { - return parameter.name && parameter.name.kind === 69 /* Identifier */ && parameter.name.text.charCodeAt(0) === 95 /* _ */; - } - function checkUnusedClassMembers(node) { - if (compilerOptions.noUnusedLocals && !ts.isInAmbientContext(node)) { - if (node.members) { - for (var _i = 0, _a = node.members; _i < _a.length; _i++) { - var member = _a[_i]; - if (member.kind === 147 /* MethodDeclaration */ || member.kind === 145 /* PropertyDeclaration */) { - if (!member.symbol.isReferenced && ts.getModifierFlags(member) & 8 /* Private */) { - error(member.name, ts.Diagnostics._0_is_declared_but_never_used, member.symbol.name); - } - } - else if (member.kind === 148 /* Constructor */) { - for (var _b = 0, _c = member.parameters; _b < _c.length; _b++) { - var parameter = _c[_b]; - if (!parameter.symbol.isReferenced && ts.getModifierFlags(parameter) & 8 /* Private */) { - error(parameter.name, ts.Diagnostics.Property_0_is_declared_but_never_used, parameter.symbol.name); - } - } + else if (parameterName) { + var hasReportedError = false; + for (var _i = 0, _a = parent.parameters; _i < _a.length; _i++) { + var name_18 = _a[_i].name; + if (ts.isBindingPattern(name_18) && + checkIfTypePredicateVariableIsDeclaredInBindingPattern(name_18, parameterName, typePredicate.parameterName)) { + hasReportedError = true; + break; } } + if (!hasReportedError) { + error(node.parameterName, ts.Diagnostics.Cannot_find_parameter_0, typePredicate.parameterName); + } } } } - function checkUnusedTypeParameters(node) { - if (compilerOptions.noUnusedLocals && !ts.isInAmbientContext(node)) { - if (node.typeParameters) { - // Only report errors on the last declaration for the type parameter container; - // this ensures that all uses have been accounted for. - var symbol = getSymbolOfNode(node); - var lastDeclaration = symbol && symbol.declarations && ts.lastOrUndefined(symbol.declarations); - if (lastDeclaration !== node) { - return; - } - for (var _i = 0, _a = node.typeParameters; _i < _a.length; _i++) { - var typeParameter = _a[_i]; - if (!getMergedSymbol(typeParameter.symbol).isReferenced) { - error(typeParameter.name, ts.Diagnostics._0_is_declared_but_never_used, typeParameter.symbol.name); - } + function getTypePredicateParent(node) { + switch (node.parent.kind) { + case 180 /* ArrowFunction */: + case 151 /* CallSignature */: + case 220 /* FunctionDeclaration */: + case 179 /* FunctionExpression */: + case 156 /* FunctionType */: + case 147 /* MethodDeclaration */: + case 146 /* MethodSignature */: + var parent_12 = node.parent; + if (node === parent_12.type) { + return parent_12; } - } } } - function checkUnusedModuleMembers(node) { - if (compilerOptions.noUnusedLocals && !ts.isInAmbientContext(node)) { - for (var key in node.locals) { - var local = node.locals[key]; - if (!local.isReferenced && !local.exportSymbol) { - for (var _i = 0, _a = local.declarations; _i < _a.length; _i++) { - var declaration = _a[_i]; - if (!ts.isAmbientModule(declaration)) { - error(declaration.name, ts.Diagnostics._0_is_declared_but_never_used, local.name); - } - } + function checkIfTypePredicateVariableIsDeclaredInBindingPattern(pattern, predicateVariableNode, predicateVariableName) { + for (var _i = 0, _a = pattern.elements; _i < _a.length; _i++) { + var element = _a[_i]; + if (ts.isOmittedExpression(element)) { + continue; + } + var name_19 = element.name; + if (name_19.kind === 69 /* Identifier */ && + name_19.text === predicateVariableName) { + error(predicateVariableNode, ts.Diagnostics.A_type_predicate_cannot_reference_element_0_in_a_binding_pattern, predicateVariableName); + return true; + } + else if (name_19.kind === 168 /* ArrayBindingPattern */ || + name_19.kind === 167 /* ObjectBindingPattern */) { + if (checkIfTypePredicateVariableIsDeclaredInBindingPattern(name_19, predicateVariableNode, predicateVariableName)) { + return true; } } } } - function checkBlock(node) { - // Grammar checking for SyntaxKind.Block - if (node.kind === 199 /* Block */) { - checkGrammarStatementInAmbientContext(node); + function checkSignatureDeclaration(node) { + // Grammar checking + if (node.kind === 153 /* IndexSignature */) { + checkGrammarIndexSignature(node); } - ts.forEach(node.statements, checkSourceElement); - if (node.locals) { - registerForUnusedIdentifiersCheck(node); + else if (node.kind === 156 /* FunctionType */ || node.kind === 220 /* FunctionDeclaration */ || node.kind === 157 /* ConstructorType */ || + node.kind === 151 /* CallSignature */ || node.kind === 148 /* Constructor */ || + node.kind === 152 /* ConstructSignature */) { + checkGrammarFunctionLikeDeclaration(node); } - } - function checkCollisionWithArgumentsInGeneratedCode(node) { - // no rest parameters \ declaration context \ overload - no codegen impact - if (!ts.hasDeclaredRestParameter(node) || ts.isInAmbientContext(node) || ts.nodeIsMissing(node.body)) { - return; + checkTypeParameters(node.typeParameters); + ts.forEach(node.parameters, checkParameter); + if (node.type) { + checkSourceElement(node.type); } - ts.forEach(node.parameters, function (p) { - if (p.name && !ts.isBindingPattern(p.name) && p.name.text === argumentsSymbol.name) { - error(p, ts.Diagnostics.Duplicate_identifier_arguments_Compiler_uses_arguments_to_initialize_rest_parameters); + if (produceDiagnostics) { + checkCollisionWithArgumentsInGeneratedCode(node); + if (compilerOptions.noImplicitAny && !node.type) { + switch (node.kind) { + case 152 /* ConstructSignature */: + error(node, ts.Diagnostics.Construct_signature_which_lacks_return_type_annotation_implicitly_has_an_any_return_type); + break; + case 151 /* CallSignature */: + error(node, ts.Diagnostics.Call_signature_which_lacks_return_type_annotation_implicitly_has_an_any_return_type); + break; + } } - }); - } - function needCollisionCheckForIdentifier(node, identifier, name) { - if (!(identifier && identifier.text === name)) { - return false; - } - if (node.kind === 145 /* PropertyDeclaration */ || - node.kind === 144 /* PropertySignature */ || - node.kind === 147 /* MethodDeclaration */ || - node.kind === 146 /* MethodSignature */ || - node.kind === 149 /* GetAccessor */ || - node.kind === 150 /* SetAccessor */) { - // it is ok to have member named '_super' or '_this' - member access is always qualified - return false; - } - if (ts.isInAmbientContext(node)) { - // ambient context - no codegen impact - return false; - } - var root = ts.getRootDeclaration(node); - if (root.kind === 142 /* Parameter */ && ts.nodeIsMissing(root.parent.body)) { - // just an overload - no codegen impact - return false; - } - return true; - } - function checkCollisionWithCapturedThisVariable(node, name) { - if (needCollisionCheckForIdentifier(node, name, "_this")) { - potentialThisCollisions.push(node); - } - } - // this function will run after checking the source file so 'CaptureThis' is correct for all nodes - function checkIfThisIsCapturedInEnclosingScope(node) { - var current = node; - while (current) { - if (getNodeCheckFlags(current) & 4 /* CaptureThis */) { - var isDeclaration_1 = node.kind !== 69 /* Identifier */; - if (isDeclaration_1) { - error(node.name, ts.Diagnostics.Duplicate_identifier_this_Compiler_uses_variable_declaration_this_to_capture_this_reference); + if (node.type) { + if (languageVersion >= 2 /* ES6 */ && isSyntacticallyValidGenerator(node)) { + var returnType = getTypeFromTypeNode(node.type); + if (returnType === voidType) { + error(node.type, ts.Diagnostics.A_generator_cannot_have_a_void_type_annotation); + } + else { + var generatorElementType = getElementTypeOfIterableIterator(returnType) || anyType; + var iterableIteratorInstantiation = createIterableIteratorType(generatorElementType); + // Naively, one could check that IterableIterator is assignable to the return type annotation. + // However, that would not catch the error in the following case. + // + // interface BadGenerator extends Iterable, Iterator { } + // function* g(): BadGenerator { } // Iterable and Iterator have different types! + // + checkTypeAssignableTo(iterableIteratorInstantiation, returnType, node.type); + } } - else { - error(node, ts.Diagnostics.Expression_resolves_to_variable_declaration_this_that_compiler_uses_to_capture_this_reference); + else if (ts.isAsyncFunctionLike(node)) { + checkAsyncFunctionReturnType(node); } - return; } - current = current.parent; + if (noUnusedIdentifiers && !node.body) { + checkUnusedTypeParameters(node); + } } } - function checkCollisionWithCapturedSuperVariable(node, name) { - if (!needCollisionCheckForIdentifier(node, name, "_super")) { - return; - } - // bubble up and find containing type - var enclosingClass = ts.getContainingClass(node); - // if containing type was not found or it is ambient - exit (no codegen) - if (!enclosingClass || ts.isInAmbientContext(enclosingClass)) { - return; - } - if (ts.getClassExtendsHeritageClauseElement(enclosingClass)) { - var isDeclaration_2 = node.kind !== 69 /* Identifier */; - if (isDeclaration_2) { - error(node, ts.Diagnostics.Duplicate_identifier_super_Compiler_uses_super_to_capture_base_class_reference); + function checkClassForDuplicateDeclarations(node) { + var Accessor; + (function (Accessor) { + Accessor[Accessor["Getter"] = 1] = "Getter"; + Accessor[Accessor["Setter"] = 2] = "Setter"; + Accessor[Accessor["Property"] = 3] = "Property"; + })(Accessor || (Accessor = {})); + var instanceNames = ts.createMap(); + var staticNames = ts.createMap(); + for (var _i = 0, _a = node.members; _i < _a.length; _i++) { + var member = _a[_i]; + if (member.kind === 148 /* Constructor */) { + for (var _b = 0, _c = member.parameters; _b < _c.length; _b++) { + var param = _c[_b]; + if (ts.isParameterPropertyDeclaration(param)) { + addName(instanceNames, param.name, param.name.text, 3 /* Property */); + } + } } else { - error(node, ts.Diagnostics.Expression_resolves_to_super_that_compiler_uses_to_capture_base_class_reference); + var isStatic = ts.forEach(member.modifiers, function (m) { return m.kind === 113 /* StaticKeyword */; }); + var names = isStatic ? staticNames : instanceNames; + var memberName = member.name && ts.getPropertyNameForPropertyNameNode(member.name); + if (memberName) { + switch (member.kind) { + case 149 /* GetAccessor */: + addName(names, member.name, memberName, 1 /* Getter */); + break; + case 150 /* SetAccessor */: + addName(names, member.name, memberName, 2 /* Setter */); + break; + case 145 /* PropertyDeclaration */: + addName(names, member.name, memberName, 3 /* Property */); + break; + } + } } } - } - function checkCollisionWithRequireExportsInGeneratedCode(node, name) { - if (!needCollisionCheckForIdentifier(node, name, "require") && !needCollisionCheckForIdentifier(node, name, "exports")) { - return; - } - // Uninstantiated modules shouldnt do this check - if (node.kind === 225 /* ModuleDeclaration */ && ts.getModuleInstanceState(node) !== 1 /* Instantiated */) { - return; - } - // In case of variable declaration, node.parent is variable statement so look at the variable statement's parent - var parent = getDeclarationContainer(node); - if (parent.kind === 256 /* SourceFile */ && ts.isExternalOrCommonJsModule(parent)) { - // If the declaration happens to be in external module, report error that require and exports are reserved keywords - error(name, ts.Diagnostics.Duplicate_identifier_0_Compiler_reserves_name_1_in_top_level_scope_of_a_module, ts.declarationNameToString(name), ts.declarationNameToString(name)); - } - } - function checkCollisionWithGlobalPromiseInGeneratedCode(node, name) { - if (!needCollisionCheckForIdentifier(node, name, "Promise")) { - return; - } - // Uninstantiated modules shouldnt do this check - if (node.kind === 225 /* ModuleDeclaration */ && ts.getModuleInstanceState(node) !== 1 /* Instantiated */) { - return; - } - // In case of variable declaration, node.parent is variable statement so look at the variable statement's parent - var parent = getDeclarationContainer(node); - if (parent.kind === 256 /* SourceFile */ && ts.isExternalOrCommonJsModule(parent) && parent.flags & 8192 /* HasAsyncFunctions */) { - // If the declaration happens to be in external module, report error that Promise is a reserved identifier. - error(name, ts.Diagnostics.Duplicate_identifier_0_Compiler_reserves_name_1_in_top_level_scope_of_a_module_containing_async_functions, ts.declarationNameToString(name), ts.declarationNameToString(name)); + function addName(names, location, name, meaning) { + var prev = names[name]; + if (prev) { + if (prev & meaning) { + error(location, ts.Diagnostics.Duplicate_identifier_0, ts.getTextOfNode(location)); + } + else { + names[name] = prev | meaning; + } + } + else { + names[name] = meaning; + } } } - function checkVarDeclaredNamesNotShadowed(node) { - // - ScriptBody : StatementList - // It is a Syntax Error if any element of the LexicallyDeclaredNames of StatementList - // also occurs in the VarDeclaredNames of StatementList. - // - Block : { StatementList } - // It is a Syntax Error if any element of the LexicallyDeclaredNames of StatementList - // also occurs in the VarDeclaredNames of StatementList. - // Variable declarations are hoisted to the top of their function scope. They can shadow - // block scoped declarations, which bind tighter. this will not be flagged as duplicate definition - // by the binder as the declaration scope is different. - // A non-initialized declaration is a no-op as the block declaration will resolve before the var - // declaration. the problem is if the declaration has an initializer. this will act as a write to the - // block declared value. this is fine for let, but not const. - // Only consider declarations with initializers, uninitialized const declarations will not - // step on a let/const variable. - // Do not consider const and const declarations, as duplicate block-scoped declarations - // are handled by the binder. - // We are only looking for const declarations that step on let\const declarations from a - // different scope. e.g.: - // { - // const x = 0; // localDeclarationSymbol obtained after name resolution will correspond to this declaration - // const x = 0; // symbol for this declaration will be 'symbol' - // } - // skip block-scoped variables and parameters - if ((ts.getCombinedNodeFlags(node) & 3 /* BlockScoped */) !== 0 || ts.isParameterDeclaration(node)) { - return; - } - // skip variable declarations that don't have initializers - // NOTE: in ES6 spec initializer is required in variable declarations where name is binding pattern - // so we'll always treat binding elements as initialized - if (node.kind === 218 /* VariableDeclaration */ && !node.initializer) { - return; - } - var symbol = getSymbolOfNode(node); - if (symbol.flags & 1 /* FunctionScopedVariable */) { - var localDeclarationSymbol = resolveName(node, node.name.text, 3 /* Variable */, /*nodeNotFoundErrorMessage*/ undefined, /*nameArg*/ undefined); - if (localDeclarationSymbol && - localDeclarationSymbol !== symbol && - localDeclarationSymbol.flags & 2 /* BlockScopedVariable */) { - if (getDeclarationNodeFlagsFromSymbol(localDeclarationSymbol) & 3 /* BlockScoped */) { - var varDeclList = ts.getAncestor(localDeclarationSymbol.valueDeclaration, 219 /* VariableDeclarationList */); - var container = varDeclList.parent.kind === 200 /* VariableStatement */ && varDeclList.parent.parent - ? varDeclList.parent.parent - : undefined; - // names of block-scoped and function scoped variables can collide only - // if block scoped variable is defined in the function\module\source file scope (because of variable hoisting) - var namesShareScope = container && - (container.kind === 199 /* Block */ && ts.isFunctionLike(container.parent) || - container.kind === 226 /* ModuleBlock */ || - container.kind === 225 /* ModuleDeclaration */ || - container.kind === 256 /* SourceFile */); - // here we know that function scoped variable is shadowed by block scoped one - // if they are defined in the same scope - binder has already reported redeclaration error - // otherwise if variable has an initializer - show error that initialization will fail - // since LHS will be block scoped name instead of function scoped - if (!namesShareScope) { - var name_20 = symbolToString(localDeclarationSymbol); - error(node, ts.Diagnostics.Cannot_initialize_outer_scoped_variable_0_in_the_same_scope_as_block_scoped_declaration_1, name_20, name_20); - } + function checkObjectTypeForDuplicateDeclarations(node) { + var names = ts.createMap(); + for (var _i = 0, _a = node.members; _i < _a.length; _i++) { + var member = _a[_i]; + if (member.kind == 144 /* PropertySignature */) { + var memberName = void 0; + switch (member.name.kind) { + case 9 /* StringLiteral */: + case 8 /* NumericLiteral */: + case 69 /* Identifier */: + memberName = member.name.text; + break; + default: + continue; + } + if (names[memberName]) { + error(member.symbol.valueDeclaration.name, ts.Diagnostics.Duplicate_identifier_0, memberName); + error(member.name, ts.Diagnostics.Duplicate_identifier_0, memberName); + } + else { + names[memberName] = true; } } } } - // Check that a parameter initializer contains no references to parameters declared to the right of itself - function checkParameterInitializer(node) { - if (ts.getRootDeclaration(node).kind !== 142 /* Parameter */) { - return; - } - var func = ts.getContainingFunction(node); - visit(node.initializer); - function visit(n) { - if (ts.isTypeNode(n) || ts.isDeclarationName(n)) { - // do not dive in types - // skip declaration names (i.e. in object literal expressions) + function checkTypeForDuplicateIndexSignatures(node) { + if (node.kind === 222 /* InterfaceDeclaration */) { + var nodeSymbol = getSymbolOfNode(node); + // in case of merging interface declaration it is possible that we'll enter this check procedure several times for every declaration + // to prevent this run check only for the first declaration of a given kind + if (nodeSymbol.declarations.length > 0 && nodeSymbol.declarations[0] !== node) { return; } - if (n.kind === 172 /* PropertyAccessExpression */) { - // skip property names in property access expression - return visit(n.expression); - } - else if (n.kind === 69 /* Identifier */) { - // check FunctionLikeDeclaration.locals (stores parameters\function local variable) - // if it contains entry with a specified name - var symbol = resolveName(n, n.text, 107455 /* Value */ | 8388608 /* Alias */, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined); - if (!symbol || symbol === unknownSymbol || !symbol.valueDeclaration) { - return; - } - if (symbol.valueDeclaration === node) { - error(n, ts.Diagnostics.Parameter_0_cannot_be_referenced_in_its_initializer, ts.declarationNameToString(node.name)); - return; - } - // locals map for function contain both parameters and function locals - // so we need to do a bit of extra work to check if reference is legal - var enclosingContainer = ts.getEnclosingBlockScopeContainer(symbol.valueDeclaration); - if (enclosingContainer === func) { - if (symbol.valueDeclaration.kind === 142 /* Parameter */) { - // it is ok to reference parameter in initializer if either - // - parameter is located strictly on the left of current parameter declaration - if (symbol.valueDeclaration.pos < node.pos) { - return; - } - // - parameter is wrapped in function-like entity - var current = n; - while (current !== node.initializer) { - if (ts.isFunctionLike(current.parent)) { - return; + } + // TypeScript 1.0 spec (April 2014) + // 3.7.4: An object type can contain at most one string index signature and one numeric index signature. + // 8.5: A class declaration can have at most one string index member declaration and one numeric index member declaration + var indexSymbol = getIndexSymbol(getSymbolOfNode(node)); + if (indexSymbol) { + var seenNumericIndexer = false; + var seenStringIndexer = false; + for (var _i = 0, _a = indexSymbol.declarations; _i < _a.length; _i++) { + var decl = _a[_i]; + var declaration = decl; + if (declaration.parameters.length === 1 && declaration.parameters[0].type) { + switch (declaration.parameters[0].type.kind) { + case 132 /* StringKeyword */: + if (!seenStringIndexer) { + seenStringIndexer = true; } - // computed property names/initializers in instance property declaration of class like entities - // are executed in constructor and thus deferred - if (current.parent.kind === 145 /* PropertyDeclaration */ && - !(ts.hasModifier(current.parent, 32 /* Static */)) && - ts.isClassLike(current.parent.parent)) { - return; + else { + error(declaration, ts.Diagnostics.Duplicate_string_index_signature); } - current = current.parent; - } + break; + case 130 /* NumberKeyword */: + if (!seenNumericIndexer) { + seenNumericIndexer = true; + } + else { + error(declaration, ts.Diagnostics.Duplicate_number_index_signature); + } + break; } - error(n, ts.Diagnostics.Initializer_of_parameter_0_cannot_reference_identifier_1_declared_after_it, ts.declarationNameToString(node.name), ts.declarationNameToString(n)); } } - else { - return ts.forEachChild(n, visit); - } } } - // Check variable, parameter, or property declaration - function checkVariableLikeDeclaration(node) { - checkDecorators(node); - checkSourceElement(node.type); - // For a computed property, just check the initializer and exit - // Do not use hasDynamicName here, because that returns false for well known symbols. - // We want to perform checkComputedPropertyName for all computed properties, including - // well known symbols. - if (node.name.kind === 140 /* ComputedPropertyName */) { - checkComputedPropertyName(node.name); - if (node.initializer) { - checkExpressionCached(node.initializer); - } - } - if (node.kind === 169 /* BindingElement */) { - // check computed properties inside property names of binding elements - if (node.propertyName && node.propertyName.kind === 140 /* ComputedPropertyName */) { - checkComputedPropertyName(node.propertyName); - } - // check private/protected variable access - var parent_12 = node.parent.parent; - var parentType = getTypeForBindingElementParent(parent_12); - var name_21 = node.propertyName || node.name; - var property = getPropertyOfType(parentType, getTextOfPropertyName(name_21)); - if (parent_12.initializer && property && getParentOfSymbol(property)) { - checkClassPropertyAccess(parent_12, parent_12.initializer, parentType, property); - } + function checkPropertyDeclaration(node) { + // Grammar checking + checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarProperty(node) || checkGrammarComputedPropertyName(node.name); + checkVariableLikeDeclaration(node); + } + function checkMethodDeclaration(node) { + // Grammar checking + checkGrammarMethod(node) || checkGrammarComputedPropertyName(node.name); + // Grammar checking for modifiers is done inside the function checkGrammarFunctionLikeDeclaration + checkFunctionOrMethodDeclaration(node); + // Abstract methods cannot have an implementation. + // Extra checks are to avoid reporting multiple errors relating to the "abstractness" of the node. + if (ts.getModifierFlags(node) & 128 /* Abstract */ && node.body) { + error(node, ts.Diagnostics.Method_0_cannot_have_an_implementation_because_it_is_marked_abstract, ts.declarationNameToString(node.name)); } - // For a binding pattern, check contained binding elements - if (ts.isBindingPattern(node.name)) { - ts.forEach(node.name.elements, checkSourceElement); + } + function checkConstructorDeclaration(node) { + // Grammar check on signature of constructor and modifier of the constructor is done in checkSignatureDeclaration function. + checkSignatureDeclaration(node); + // Grammar check for checking only related to constructorDeclaration + checkGrammarConstructorTypeParameters(node) || checkGrammarConstructorTypeAnnotation(node); + checkSourceElement(node.body); + registerForUnusedIdentifiersCheck(node); + var symbol = getSymbolOfNode(node); + var firstDeclaration = ts.getDeclarationOfKind(symbol, node.kind); + // Only type check the symbol once + if (node === firstDeclaration) { + checkFunctionOrConstructorSymbol(symbol); } - // For a parameter declaration with an initializer, error and exit if the containing function doesn't have a body - if (node.initializer && ts.getRootDeclaration(node).kind === 142 /* Parameter */ && ts.nodeIsMissing(ts.getContainingFunction(node).body)) { - error(node, ts.Diagnostics.A_parameter_initializer_is_only_allowed_in_a_function_or_constructor_implementation); + // exit early in the case of signature - super checks are not relevant to them + if (ts.nodeIsMissing(node.body)) { return; } - // For a binding pattern, validate the initializer and exit - if (ts.isBindingPattern(node.name)) { - // Don't validate for-in initializer as it is already an error - if (node.initializer && node.parent.parent.kind !== 207 /* ForInStatement */) { - checkTypeAssignableTo(checkExpressionCached(node.initializer), getWidenedTypeForVariableLikeDeclaration(node), node, /*headMessage*/ undefined); - checkParameterInitializer(node); - } + if (!produceDiagnostics) { return; } - var symbol = getSymbolOfNode(node); - var type = getTypeOfVariableOrParameterOrProperty(symbol); - if (node === symbol.valueDeclaration) { - // Node is the primary declaration of the symbol, just validate the initializer - // Don't validate for-in initializer as it is already an error - if (node.initializer && node.parent.parent.kind !== 207 /* ForInStatement */) { - checkTypeAssignableTo(checkExpressionCached(node.initializer), type, node, /*headMessage*/ undefined); - checkParameterInitializer(node); - } + function containsSuperCallAsComputedPropertyName(n) { + return n.name && containsSuperCall(n.name); } - else { - // Node is a secondary declaration, check that type is identical to primary declaration and check that - // initializer is consistent with type associated with the node - var declarationType = getWidenedTypeForVariableLikeDeclaration(node); - if (type !== unknownType && declarationType !== unknownType && !isTypeIdenticalTo(type, declarationType)) { - error(node.name, ts.Diagnostics.Subsequent_variable_declarations_must_have_the_same_type_Variable_0_must_be_of_type_1_but_here_has_type_2, ts.declarationNameToString(node.name), typeToString(type), typeToString(declarationType)); + function containsSuperCall(n) { + if (ts.isSuperCall(n)) { + return true; } - if (node.initializer) { - checkTypeAssignableTo(checkExpressionCached(node.initializer), declarationType, node, /*headMessage*/ undefined); + else if (ts.isFunctionLike(n)) { + return false; } - if (!areDeclarationFlagsIdentical(node, symbol.valueDeclaration)) { - error(symbol.valueDeclaration.name, ts.Diagnostics.All_declarations_of_0_must_have_identical_modifiers, ts.declarationNameToString(node.name)); - error(node.name, ts.Diagnostics.All_declarations_of_0_must_have_identical_modifiers, ts.declarationNameToString(node.name)); + else if (ts.isClassLike(n)) { + return ts.forEach(n.members, containsSuperCallAsComputedPropertyName); } + return ts.forEachChild(n, containsSuperCall); } - if (node.kind !== 145 /* PropertyDeclaration */ && node.kind !== 144 /* PropertySignature */) { - // We know we don't have a binding pattern or computed name here - checkExportsOnMergedDeclarations(node); - if (node.kind === 218 /* VariableDeclaration */ || node.kind === 169 /* BindingElement */) { - checkVarDeclaredNamesNotShadowed(node); + function markThisReferencesAsErrors(n) { + if (n.kind === 97 /* ThisKeyword */) { + error(n, ts.Diagnostics.this_cannot_be_referenced_in_current_location); + } + else if (n.kind !== 179 /* FunctionExpression */ && n.kind !== 220 /* FunctionDeclaration */) { + ts.forEachChild(n, markThisReferencesAsErrors); } - checkCollisionWithCapturedSuperVariable(node, node.name); - checkCollisionWithCapturedThisVariable(node, node.name); - checkCollisionWithRequireExportsInGeneratedCode(node, node.name); - checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); - } - } - function areDeclarationFlagsIdentical(left, right) { - if ((left.kind === 142 /* Parameter */ && right.kind === 218 /* VariableDeclaration */) || - (left.kind === 218 /* VariableDeclaration */ && right.kind === 142 /* Parameter */)) { - // Differences in optionality between parameters and variables are allowed. - return true; } - if (ts.hasQuestionToken(left) !== ts.hasQuestionToken(right)) { - return false; + function isInstancePropertyWithInitializer(n) { + return n.kind === 145 /* PropertyDeclaration */ && + !(ts.getModifierFlags(n) & 32 /* Static */) && + !!n.initializer; } - var interestingFlags = 8 /* Private */ | - 16 /* Protected */ | - 256 /* Async */ | - 128 /* Abstract */ | - 64 /* Readonly */ | - 32 /* Static */; - return (ts.getModifierFlags(left) & interestingFlags) === (ts.getModifierFlags(right) & interestingFlags); - } - function checkVariableDeclaration(node) { - checkGrammarVariableDeclaration(node); - return checkVariableLikeDeclaration(node); - } - function checkBindingElement(node) { - checkGrammarBindingElement(node); - return checkVariableLikeDeclaration(node); - } - function checkVariableStatement(node) { - // Grammar checking - checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarVariableDeclarationList(node.declarationList) || checkGrammarForDisallowedLetOrConstStatement(node); - ts.forEach(node.declarationList.declarations, checkSourceElement); - } - function checkGrammarDisallowedModifiersOnObjectLiteralExpressionMethod(node) { - // We only disallow modifier on a method declaration if it is a property of object-literal-expression - if (node.modifiers && node.parent.kind === 171 /* ObjectLiteralExpression */) { - if (ts.isAsyncFunctionLike(node)) { - if (node.modifiers.length > 1) { - return grammarErrorOnFirstToken(node, ts.Diagnostics.Modifiers_cannot_appear_here); + // TS 1.0 spec (April 2014): 8.3.2 + // Constructors of classes with no extends clause may not contain super calls, whereas + // constructors of derived classes must contain at least one super call somewhere in their function body. + var containingClassDecl = node.parent; + if (ts.getClassExtendsHeritageClauseElement(containingClassDecl)) { + captureLexicalThis(node.parent, containingClassDecl); + var classExtendsNull = classDeclarationExtendsNull(containingClassDecl); + var superCall = getSuperCallInConstructor(node); + if (superCall) { + if (classExtendsNull) { + error(superCall, ts.Diagnostics.A_constructor_cannot_contain_a_super_call_when_its_class_extends_null); + } + // The first statement in the body of a constructor (excluding prologue directives) must be a super call + // if both of the following are true: + // - The containing class is a derived class. + // - The constructor declares parameter properties + // or the containing class declares instance member variables with initializers. + var superCallShouldBeFirst = ts.forEach(node.parent.members, isInstancePropertyWithInitializer) || + ts.forEach(node.parameters, function (p) { return ts.getModifierFlags(p) & 92 /* ParameterPropertyModifier */; }); + // Skip past any prologue directives to find the first statement + // to ensure that it was a super call. + if (superCallShouldBeFirst) { + var statements = node.body.statements; + var superCallStatement = void 0; + for (var _i = 0, statements_2 = statements; _i < statements_2.length; _i++) { + var statement = statements_2[_i]; + if (statement.kind === 202 /* ExpressionStatement */ && ts.isSuperCall(statement.expression)) { + superCallStatement = statement; + break; + } + if (!ts.isPrologueDirective(statement)) { + break; + } + } + if (!superCallStatement) { + error(node, ts.Diagnostics.A_super_call_must_be_the_first_statement_in_the_constructor_when_a_class_contains_initialized_properties_or_has_parameter_properties); + } } } - else { - return grammarErrorOnFirstToken(node, ts.Diagnostics.Modifiers_cannot_appear_here); + else if (!classExtendsNull) { + error(node, ts.Diagnostics.Constructors_for_derived_classes_must_contain_a_super_call); } } } - function checkExpressionStatement(node) { - // Grammar checking - checkGrammarStatementInAmbientContext(node); - checkExpression(node.expression); - } - function checkIfStatement(node) { - // Grammar checking - checkGrammarStatementInAmbientContext(node); - checkExpression(node.expression); - checkSourceElement(node.thenStatement); - if (node.thenStatement.kind === 201 /* EmptyStatement */) { - error(node.thenStatement, ts.Diagnostics.The_body_of_an_if_statement_cannot_be_the_empty_statement); - } - checkSourceElement(node.elseStatement); - } - function checkDoStatement(node) { - // Grammar checking - checkGrammarStatementInAmbientContext(node); - checkSourceElement(node.statement); - checkExpression(node.expression); - } - function checkWhileStatement(node) { - // Grammar checking - checkGrammarStatementInAmbientContext(node); - checkExpression(node.expression); - checkSourceElement(node.statement); - } - function checkForStatement(node) { - // Grammar checking - if (!checkGrammarStatementInAmbientContext(node)) { - if (node.initializer && node.initializer.kind === 219 /* VariableDeclarationList */) { - checkGrammarVariableDeclarationList(node.initializer); + function checkAccessorDeclaration(node) { + if (produceDiagnostics) { + // Grammar checking accessors + checkGrammarFunctionLikeDeclaration(node) || checkGrammarAccessor(node) || checkGrammarComputedPropertyName(node.name); + checkDecorators(node); + checkSignatureDeclaration(node); + if (node.kind === 149 /* GetAccessor */) { + if (!ts.isInAmbientContext(node) && ts.nodeIsPresent(node.body) && (node.flags & 128 /* HasImplicitReturn */)) { + if (!(node.flags & 256 /* HasExplicitReturn */)) { + error(node.name, ts.Diagnostics.A_get_accessor_must_return_a_value); + } + } } - } - if (node.initializer) { - if (node.initializer.kind === 219 /* VariableDeclarationList */) { - ts.forEach(node.initializer.declarations, checkVariableDeclaration); + // Do not use hasDynamicName here, because that returns false for well known symbols. + // We want to perform checkComputedPropertyName for all computed properties, including + // well known symbols. + if (node.name.kind === 140 /* ComputedPropertyName */) { + checkComputedPropertyName(node.name); } - else { - checkExpression(node.initializer); + if (!ts.hasDynamicName(node)) { + // TypeScript 1.0 spec (April 2014): 8.4.3 + // Accessors for the same member name must specify the same accessibility. + var otherKind = node.kind === 149 /* GetAccessor */ ? 150 /* SetAccessor */ : 149 /* GetAccessor */; + var otherAccessor = ts.getDeclarationOfKind(node.symbol, otherKind); + if (otherAccessor) { + if ((ts.getModifierFlags(node) & 28 /* AccessibilityModifier */) !== (ts.getModifierFlags(otherAccessor) & 28 /* AccessibilityModifier */)) { + error(node.name, ts.Diagnostics.Getter_and_setter_accessors_do_not_agree_in_visibility); + } + if (ts.hasModifier(node, 128 /* Abstract */) !== ts.hasModifier(otherAccessor, 128 /* Abstract */)) { + error(node.name, ts.Diagnostics.Accessors_must_both_be_abstract_or_non_abstract); + } + // TypeScript 1.0 spec (April 2014): 4.5 + // If both accessors include type annotations, the specified types must be identical. + checkAccessorDeclarationTypesIdentical(node, otherAccessor, getAnnotatedAccessorType, ts.Diagnostics.get_and_set_accessor_must_have_the_same_type); + checkAccessorDeclarationTypesIdentical(node, otherAccessor, getThisTypeOfDeclaration, ts.Diagnostics.get_and_set_accessor_must_have_the_same_this_type); + } + } + var returnType = getTypeOfAccessors(getSymbolOfNode(node)); + if (node.kind === 149 /* GetAccessor */) { + checkAllCodePathsInNonVoidFunctionReturnOrThrow(node, returnType); } } - if (node.condition) - checkExpression(node.condition); - if (node.incrementor) - checkExpression(node.incrementor); - checkSourceElement(node.statement); - if (node.locals) { + if (node.parent.kind !== 171 /* ObjectLiteralExpression */) { + checkSourceElement(node.body); registerForUnusedIdentifiersCheck(node); } - } - function checkForOfStatement(node) { - checkGrammarForInOrForOfStatement(node); - // Check the LHS and RHS - // If the LHS is a declaration, just check it as a variable declaration, which will in turn check the RHS - // via checkRightHandSideOfForOf. - // If the LHS is an expression, check the LHS, as a destructuring assignment or as a reference. - // Then check that the RHS is assignable to it. - if (node.initializer.kind === 219 /* VariableDeclarationList */) { - checkForInOrForOfVariableDeclaration(node); - } else { - var varExpr = node.initializer; - var iteratedType = checkRightHandSideOfForOf(node.expression); - // There may be a destructuring assignment on the left side - if (varExpr.kind === 170 /* ArrayLiteralExpression */ || varExpr.kind === 171 /* ObjectLiteralExpression */) { - // iteratedType may be undefined. In this case, we still want to check the structure of - // varExpr, in particular making sure it's a valid LeftHandSideExpression. But we'd like - // to short circuit the type relation checking as much as possible, so we pass the unknownType. - checkDestructuringAssignment(varExpr, iteratedType || unknownType); - } - else { - var leftType = checkExpression(varExpr); - checkReferenceExpression(varExpr, /*invalidReferenceMessage*/ ts.Diagnostics.Invalid_left_hand_side_in_for_of_statement, - /*constantVariableMessage*/ ts.Diagnostics.The_left_hand_side_of_a_for_of_statement_cannot_be_a_constant_or_a_read_only_property); - // iteratedType will be undefined if the rightType was missing properties/signatures - // required to get its iteratedType (like [Symbol.iterator] or next). This may be - // because we accessed properties from anyType, or it may have led to an error inside - // getElementTypeOfIterable. - if (iteratedType) { - checkTypeAssignableTo(iteratedType, leftType, varExpr, /*headMessage*/ undefined); - } - } + checkNodeDeferred(node); } - checkSourceElement(node.statement); - if (node.locals) { - registerForUnusedIdentifiersCheck(node); + } + function checkAccessorDeclarationTypesIdentical(first, second, getAnnotatedType, message) { + var firstType = getAnnotatedType(first); + var secondType = getAnnotatedType(second); + if (firstType && secondType && !isTypeIdenticalTo(firstType, secondType)) { + error(first, message); } } - function checkForInStatement(node) { - // Grammar checking - checkGrammarForInOrForOfStatement(node); - // TypeScript 1.0 spec (April 2014): 5.4 - // In a 'for-in' statement of the form - // for (let VarDecl in Expr) Statement - // VarDecl must be a variable declaration without a type annotation that declares a variable of type Any, - // and Expr must be an expression of type Any, an object type, or a type parameter type. - if (node.initializer.kind === 219 /* VariableDeclarationList */) { - var variable = node.initializer.declarations[0]; - if (variable && ts.isBindingPattern(variable.name)) { - error(variable.name, ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_a_destructuring_pattern); + function checkAccessorDeferred(node) { + checkSourceElement(node.body); + registerForUnusedIdentifiersCheck(node); + } + function checkMissingDeclaration(node) { + checkDecorators(node); + } + function checkTypeArgumentConstraints(typeParameters, typeArgumentNodes) { + var typeArguments; + var mapper; + var result = true; + for (var i = 0; i < typeParameters.length; i++) { + var constraint = getConstraintOfTypeParameter(typeParameters[i]); + if (constraint) { + if (!typeArguments) { + typeArguments = ts.map(typeArgumentNodes, getTypeFromTypeNodeNoAlias); + mapper = createTypeMapper(typeParameters, typeArguments); + } + var typeArgument = typeArguments[i]; + result = result && checkTypeAssignableTo(typeArgument, getTypeWithThisArgument(instantiateType(constraint, mapper), typeArgument), typeArgumentNodes[i], ts.Diagnostics.Type_0_does_not_satisfy_the_constraint_1); } - checkForInOrForOfVariableDeclaration(node); } - else { - // In a 'for-in' statement of the form - // for (Var in Expr) Statement - // Var must be an expression classified as a reference of type Any or the String primitive type, - // and Expr must be an expression of type Any, an object type, or a type parameter type. - var varExpr = node.initializer; - var leftType = checkExpression(varExpr); - if (varExpr.kind === 170 /* ArrayLiteralExpression */ || varExpr.kind === 171 /* ObjectLiteralExpression */) { - error(varExpr, ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_a_destructuring_pattern); - } - else if (!isTypeAnyOrAllConstituentTypesHaveKind(leftType, 34 /* StringLike */)) { - error(varExpr, ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_must_be_of_type_string_or_any); + return result; + } + function checkTypeReferenceNode(node) { + checkGrammarTypeArguments(node, node.typeArguments); + var type = getTypeFromTypeReference(node); + if (type !== unknownType) { + if (node.typeArguments) { + // Do type argument local checks only if referenced type is successfully resolved + ts.forEach(node.typeArguments, checkSourceElement); + if (produceDiagnostics) { + var symbol = getNodeLinks(node).resolvedSymbol; + var typeParameters = symbol.flags & 524288 /* TypeAlias */ ? getSymbolLinks(symbol).typeParameters : type.target.localTypeParameters; + checkTypeArgumentConstraints(typeParameters, node.typeArguments); + } } - else { - // run check only former check succeeded to avoid cascading errors - checkReferenceExpression(varExpr, ts.Diagnostics.Invalid_left_hand_side_in_for_in_statement, ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_a_constant_or_a_read_only_property); + if (type.flags & 16 /* Enum */ && !type.memberTypes && getNodeLinks(node).resolvedSymbol.flags & 8 /* EnumMember */) { + error(node, ts.Diagnostics.Enum_type_0_has_members_with_initializers_that_are_not_literals, typeToString(type)); } } - var rightType = checkNonNullExpression(node.expression); - // unknownType is returned i.e. if node.expression is identifier whose name cannot be resolved - // in this case error about missing name is already reported - do not report extra one - if (!isTypeAnyOrAllConstituentTypesHaveKind(rightType, 2588672 /* ObjectType */ | 16384 /* TypeParameter */)) { - error(node.expression, ts.Diagnostics.The_right_hand_side_of_a_for_in_statement_must_be_of_type_any_an_object_type_or_a_type_parameter); - } - checkSourceElement(node.statement); - if (node.locals) { - registerForUnusedIdentifiersCheck(node); - } } - function checkForInOrForOfVariableDeclaration(iterationStatement) { - var variableDeclarationList = iterationStatement.initializer; - // checkGrammarForInOrForOfStatement will check that there is exactly one declaration. - if (variableDeclarationList.declarations.length >= 1) { - var decl = variableDeclarationList.declarations[0]; - checkVariableDeclaration(decl); + function checkTypeQuery(node) { + getTypeFromTypeQueryNode(node); + } + function checkTypeLiteral(node) { + ts.forEach(node.members, checkSourceElement); + if (produceDiagnostics) { + var type = getTypeFromTypeLiteralOrFunctionOrConstructorTypeNode(node); + checkIndexConstraints(type); + checkTypeForDuplicateIndexSignatures(node); + checkObjectTypeForDuplicateDeclarations(node); } } - function checkRightHandSideOfForOf(rhsExpression) { - var expressionType = checkNonNullExpression(rhsExpression); - return checkIteratedTypeOrElementType(expressionType, rhsExpression, /*allowStringInput*/ true); + function checkArrayType(node) { + checkSourceElement(node.elementType); } - function checkIteratedTypeOrElementType(inputType, errorNode, allowStringInput) { - if (isTypeAny(inputType)) { - return inputType; - } - if (languageVersion >= 2 /* ES6 */) { - return checkElementTypeOfIterable(inputType, errorNode); - } - if (allowStringInput) { - return checkElementTypeOfArrayOrString(inputType, errorNode); + function checkTupleType(node) { + // Grammar checking + var hasErrorFromDisallowedTrailingComma = checkGrammarForDisallowedTrailingComma(node.elementTypes); + if (!hasErrorFromDisallowedTrailingComma && node.elementTypes.length === 0) { + grammarErrorOnNode(node, ts.Diagnostics.A_tuple_type_element_list_cannot_be_empty); } - if (isArrayLikeType(inputType)) { - var indexType = getIndexTypeOfType(inputType, 1 /* Number */); - if (indexType) { - return indexType; + ts.forEach(node.elementTypes, checkSourceElement); + } + function checkUnionOrIntersectionType(node) { + ts.forEach(node.types, checkSourceElement); + } + function isPrivateWithinAmbient(node) { + return (ts.getModifierFlags(node) & 8 /* Private */) && ts.isInAmbientContext(node); + } + function getEffectiveDeclarationFlags(n, flagsToCheck) { + var flags = ts.getCombinedModifierFlags(n); + // children of classes (even ambient classes) should not be marked as ambient or export + // because those flags have no useful semantics there. + if (n.parent.kind !== 222 /* InterfaceDeclaration */ && + n.parent.kind !== 221 /* ClassDeclaration */ && + n.parent.kind !== 192 /* ClassExpression */ && + ts.isInAmbientContext(n)) { + if (!(flags & 2 /* Ambient */)) { + // It is nested in an ambient context, which means it is automatically exported + flags |= 1 /* Export */; } + flags |= 2 /* Ambient */; } - if (errorNode) { - error(errorNode, ts.Diagnostics.Type_0_is_not_an_array_type, typeToString(inputType)); - } - return unknownType; + return flags & flagsToCheck; } - /** - * When errorNode is undefined, it means we should not report any errors. - */ - function checkElementTypeOfIterable(iterable, errorNode) { - var elementType = getElementTypeOfIterable(iterable, errorNode); - // Now even though we have extracted the iteratedType, we will have to validate that the type - // passed in is actually an Iterable. - if (errorNode && elementType) { - checkTypeAssignableTo(iterable, createIterableType(elementType), errorNode); + function checkFunctionOrConstructorSymbol(symbol) { + if (!produceDiagnostics) { + return; } - return elementType || anyType; - } - /** - * We want to treat type as an iterable, and get the type it is an iterable of. The iterable - * must have the following structure (annotated with the names of the variables below): - * - * { // iterable - * [Symbol.iterator]: { // iteratorFunction - * (): Iterator - * } - * } - * - * T is the type we are after. At every level that involves analyzing return types - * of signatures, we union the return types of all the signatures. - * - * Another thing to note is that at any step of this process, we could run into a dead end, - * meaning either the property is missing, or we run into the anyType. If either of these things - * happens, we return undefined to signal that we could not find the iterated type. If a property - * is missing, and the previous step did not result in 'any', then we also give an error if the - * caller requested it. Then the caller can decide what to do in the case where there is no iterated - * type. This is different from returning anyType, because that would signify that we have matched the - * whole pattern and that T (above) is 'any'. - */ - function getElementTypeOfIterable(type, errorNode) { - if (isTypeAny(type)) { - return undefined; + function getCanonicalOverload(overloads, implementation) { + // Consider the canonical set of flags to be the flags of the bodyDeclaration or the first declaration + // Error on all deviations from this canonical set of flags + // The caveat is that if some overloads are defined in lib.d.ts, we don't want to + // report the errors on those. To achieve this, we will say that the implementation is + // the canonical signature only if it is in the same container as the first overload + var implementationSharesContainerWithFirstOverload = implementation !== undefined && implementation.parent === overloads[0].parent; + return implementationSharesContainerWithFirstOverload ? implementation : overloads[0]; } - var typeAsIterable = type; - if (!typeAsIterable.iterableElementType) { - // As an optimization, if the type is instantiated directly using the globalIterableType (Iterable), - // then just grab its type argument. - if ((type.flags & 131072 /* Reference */) && type.target === getGlobalIterableType()) { - typeAsIterable.iterableElementType = type.typeArguments[0]; - } - else { - var iteratorFunction = getTypeOfPropertyOfType(type, ts.getPropertyNameForKnownSymbolName("iterator")); - if (isTypeAny(iteratorFunction)) { - return undefined; - } - var iteratorFunctionSignatures = iteratorFunction ? getSignaturesOfType(iteratorFunction, 0 /* Call */) : emptyArray; - if (iteratorFunctionSignatures.length === 0) { - if (errorNode) { - error(errorNode, ts.Diagnostics.Type_must_have_a_Symbol_iterator_method_that_returns_an_iterator); + function checkFlagAgreementBetweenOverloads(overloads, implementation, flagsToCheck, someOverloadFlags, allOverloadFlags) { + // Error if some overloads have a flag that is not shared by all overloads. To find the + // deviations, we XOR someOverloadFlags with allOverloadFlags + var someButNotAllOverloadFlags = someOverloadFlags ^ allOverloadFlags; + if (someButNotAllOverloadFlags !== 0) { + var canonicalFlags_1 = getEffectiveDeclarationFlags(getCanonicalOverload(overloads, implementation), flagsToCheck); + ts.forEach(overloads, function (o) { + var deviation = getEffectiveDeclarationFlags(o, flagsToCheck) ^ canonicalFlags_1; + if (deviation & 1 /* Export */) { + error(o.name, ts.Diagnostics.Overload_signatures_must_all_be_exported_or_non_exported); } - return undefined; - } - typeAsIterable.iterableElementType = getElementTypeOfIterator(getUnionType(ts.map(iteratorFunctionSignatures, getReturnTypeOfSignature), /*subtypeReduction*/ true), errorNode); + else if (deviation & 2 /* Ambient */) { + error(o.name, ts.Diagnostics.Overload_signatures_must_all_be_ambient_or_non_ambient); + } + else if (deviation & (8 /* Private */ | 16 /* Protected */)) { + error(o.name || o, ts.Diagnostics.Overload_signatures_must_all_be_public_private_or_protected); + } + else if (deviation & 128 /* Abstract */) { + error(o.name, ts.Diagnostics.Overload_signatures_must_all_be_abstract_or_non_abstract); + } + }); } } - return typeAsIterable.iterableElementType; - } - /** - * This function has very similar logic as getElementTypeOfIterable, except that it operates on - * Iterators instead of Iterables. Here is the structure: - * - * { // iterator - * next: { // iteratorNextFunction - * (): { // iteratorNextResult - * value: T // iteratorNextValue - * } - * } - * } - * - */ - function getElementTypeOfIterator(type, errorNode) { - if (isTypeAny(type)) { - return undefined; + function checkQuestionTokenAgreementBetweenOverloads(overloads, implementation, someHaveQuestionToken, allHaveQuestionToken) { + if (someHaveQuestionToken !== allHaveQuestionToken) { + var canonicalHasQuestionToken_1 = ts.hasQuestionToken(getCanonicalOverload(overloads, implementation)); + ts.forEach(overloads, function (o) { + var deviation = ts.hasQuestionToken(o) !== canonicalHasQuestionToken_1; + if (deviation) { + error(o.name, ts.Diagnostics.Overload_signatures_must_all_be_optional_or_required); + } + }); + } } - var typeAsIterator = type; - if (!typeAsIterator.iteratorElementType) { - // As an optimization, if the type is instantiated directly using the globalIteratorType (Iterator), - // then just grab its type argument. - if ((type.flags & 131072 /* Reference */) && type.target === getGlobalIteratorType()) { - typeAsIterator.iteratorElementType = type.typeArguments[0]; + var flagsToCheck = 1 /* Export */ | 2 /* Ambient */ | 8 /* Private */ | 16 /* Protected */ | 128 /* Abstract */; + var someNodeFlags = 0 /* None */; + var allNodeFlags = flagsToCheck; + var someHaveQuestionToken = false; + var allHaveQuestionToken = true; + var hasOverloads = false; + var bodyDeclaration; + var lastSeenNonAmbientDeclaration; + var previousDeclaration; + var declarations = symbol.declarations; + var isConstructor = (symbol.flags & 16384 /* Constructor */) !== 0; + function reportImplementationExpectedError(node) { + if (node.name && ts.nodeIsMissing(node.name)) { + return; } - else { - var iteratorNextFunction = getTypeOfPropertyOfType(type, "next"); - if (isTypeAny(iteratorNextFunction)) { - return undefined; - } - var iteratorNextFunctionSignatures = iteratorNextFunction ? getSignaturesOfType(iteratorNextFunction, 0 /* Call */) : emptyArray; - if (iteratorNextFunctionSignatures.length === 0) { - if (errorNode) { - error(errorNode, ts.Diagnostics.An_iterator_must_have_a_next_method); - } - return undefined; - } - var iteratorNextResult = getUnionType(ts.map(iteratorNextFunctionSignatures, getReturnTypeOfSignature), /*subtypeReduction*/ true); - if (isTypeAny(iteratorNextResult)) { - return undefined; + var seen = false; + var subsequentNode = ts.forEachChild(node.parent, function (c) { + if (seen) { + return c; } - var iteratorNextValue = getTypeOfPropertyOfType(iteratorNextResult, "value"); - if (!iteratorNextValue) { - if (errorNode) { - error(errorNode, ts.Diagnostics.The_type_returned_by_the_next_method_of_an_iterator_must_have_a_value_property); - } - return undefined; + else { + seen = c === node; } - typeAsIterator.iteratorElementType = iteratorNextValue; - } - } - return typeAsIterator.iteratorElementType; - } - function getElementTypeOfIterableIterator(type) { - if (isTypeAny(type)) { - return undefined; - } - // As an optimization, if the type is instantiated directly using the globalIterableIteratorType (IterableIterator), - // then just grab its type argument. - if ((type.flags & 131072 /* Reference */) && type.target === getGlobalIterableIteratorType()) { - return type.typeArguments[0]; - } - return getElementTypeOfIterable(type, /*errorNode*/ undefined) || - getElementTypeOfIterator(type, /*errorNode*/ undefined); - } - /** - * This function does the following steps: - * 1. Break up arrayOrStringType (possibly a union) into its string constituents and array constituents. - * 2. Take the element types of the array constituents. - * 3. Return the union of the element types, and string if there was a string constituent. - * - * For example: - * string -> string - * number[] -> number - * string[] | number[] -> string | number - * string | number[] -> string | number - * string | string[] | number[] -> string | number - * - * It also errors if: - * 1. Some constituent is neither a string nor an array. - * 2. Some constituent is a string and target is less than ES5 (because in ES3 string is not indexable). - */ - function checkElementTypeOfArrayOrString(arrayOrStringType, errorNode) { - ts.Debug.assert(languageVersion < 2 /* ES6 */); - // After we remove all types that are StringLike, we will know if there was a string constituent - // based on whether the remaining type is the same as the initial type. - var arrayType = arrayOrStringType; - if (arrayOrStringType.flags & 524288 /* Union */) { - arrayType = getUnionType(ts.filter(arrayOrStringType.types, function (t) { return !(t.flags & 34 /* StringLike */); }), /*subtypeReduction*/ true); - } - else if (arrayOrStringType.flags & 34 /* StringLike */) { - arrayType = neverType; - } - var hasStringConstituent = arrayOrStringType !== arrayType; - var reportedError = false; - if (hasStringConstituent) { - if (languageVersion < 1 /* ES5 */) { - error(errorNode, ts.Diagnostics.Using_a_string_in_a_for_of_statement_is_only_supported_in_ECMAScript_5_and_higher); - reportedError = true; - } - // Now that we've removed all the StringLike types, if no constituents remain, then the entire - // arrayOrStringType was a string. - if (arrayType.flags & 8192 /* Never */) { - return stringType; - } - } - if (!isArrayLikeType(arrayType)) { - if (!reportedError) { - // Which error we report depends on whether there was a string constituent. For example, - // if the input type is number | string, we want to say that number is not an array type. - // But if the input was just number, we want to say that number is not an array type - // or a string type. - var diagnostic = hasStringConstituent - ? ts.Diagnostics.Type_0_is_not_an_array_type - : ts.Diagnostics.Type_0_is_not_an_array_type_or_a_string_type; - error(errorNode, diagnostic, typeToString(arrayType)); - } - return hasStringConstituent ? stringType : unknownType; - } - var arrayElementType = getIndexTypeOfType(arrayType, 1 /* Number */) || unknownType; - if (hasStringConstituent) { - // This is just an optimization for the case where arrayOrStringType is string | string[] - if (arrayElementType.flags & 34 /* StringLike */) { - return stringType; - } - return getUnionType([arrayElementType, stringType], /*subtypeReduction*/ true); - } - return arrayElementType; - } - function checkBreakOrContinueStatement(node) { - // Grammar checking - checkGrammarStatementInAmbientContext(node) || checkGrammarBreakOrContinueStatement(node); - // TODO: Check that target label is valid - } - function isGetAccessorWithAnnotatedSetAccessor(node) { - return !!(node.kind === 149 /* GetAccessor */ && ts.getSetAccessorTypeAnnotationNode(ts.getDeclarationOfKind(node.symbol, 150 /* SetAccessor */))); - } - function isUnwrappedReturnTypeVoidOrAny(func, returnType) { - var unwrappedReturnType = ts.isAsyncFunctionLike(func) ? getPromisedType(returnType) : returnType; - return unwrappedReturnType && maybeTypeOfKind(unwrappedReturnType, 1024 /* Void */ | 1 /* Any */); - } - function checkReturnStatement(node) { - // Grammar checking - if (!checkGrammarStatementInAmbientContext(node)) { - var functionBlock = ts.getContainingFunction(node); - if (!functionBlock) { - grammarErrorOnFirstToken(node, ts.Diagnostics.A_return_statement_can_only_be_used_within_a_function_body); - } - } - var func = ts.getContainingFunction(node); - if (func) { - var signature = getSignatureFromDeclaration(func); - var returnType = getReturnTypeOfSignature(signature); - if (strictNullChecks || node.expression || returnType.flags & 8192 /* Never */) { - var exprType = node.expression ? checkExpressionCached(node.expression) : undefinedType; - if (func.asteriskToken) { - // A generator does not need its return expressions checked against its return type. - // Instead, the yield expressions are checked against the element type. - // TODO: Check return expressions of generators when return type tracking is added - // for generators. - return; - } - if (func.kind === 150 /* SetAccessor */) { - if (node.expression) { - error(node.expression, ts.Diagnostics.Setters_cannot_return_a_value); - } - } - else if (func.kind === 148 /* Constructor */) { - if (node.expression && !checkTypeAssignableTo(exprType, returnType, node.expression)) { - error(node.expression, ts.Diagnostics.Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class); - } - } - else if (func.type || isGetAccessorWithAnnotatedSetAccessor(func)) { - if (ts.isAsyncFunctionLike(func)) { - var promisedType = getPromisedType(returnType); - var awaitedType = checkAwaitedType(exprType, node.expression || node, ts.Diagnostics.Return_expression_in_async_function_does_not_have_a_valid_callable_then_member); - if (promisedType) { - // If the function has a return type, but promisedType is - // undefined, an error will be reported in checkAsyncFunctionReturnType - // so we don't need to report one here. - checkTypeAssignableTo(awaitedType, promisedType, node.expression || node); + }); + // We may be here because of some extra nodes between overloads that could not be parsed into a valid node. + // In this case the subsequent node is not really consecutive (.pos !== node.end), and we must ignore it here. + if (subsequentNode && subsequentNode.pos === node.end) { + if (subsequentNode.kind === node.kind) { + var errorNode_1 = subsequentNode.name || subsequentNode; + // TODO(jfreeman): These are methods, so handle computed name case + if (node.name && subsequentNode.name && node.name.text === subsequentNode.name.text) { + var reportError = (node.kind === 147 /* MethodDeclaration */ || node.kind === 146 /* MethodSignature */) && + (ts.getModifierFlags(node) & 32 /* Static */) !== (ts.getModifierFlags(subsequentNode) & 32 /* Static */); + // we can get here in two cases + // 1. mixed static and instance class members + // 2. something with the same name was defined before the set of overloads that prevents them from merging + // here we'll report error only for the first case since for second we should already report error in binder + if (reportError) { + var diagnostic = ts.getModifierFlags(node) & 32 /* Static */ ? ts.Diagnostics.Function_overload_must_be_static : ts.Diagnostics.Function_overload_must_not_be_static; + error(errorNode_1, diagnostic); } + return; } - else { - checkTypeAssignableTo(exprType, returnType, node.expression || node); + else if (ts.nodeIsPresent(subsequentNode.body)) { + error(errorNode_1, ts.Diagnostics.Function_implementation_name_must_be_0, ts.declarationNameToString(node.name)); + return; } } } - else if (func.kind !== 148 /* Constructor */ && compilerOptions.noImplicitReturns && !isUnwrappedReturnTypeVoidOrAny(func, returnType)) { - // The function has a return type, but the return statement doesn't have an expression. - error(node, ts.Diagnostics.Not_all_code_paths_return_a_value); - } - } - } - function checkWithStatement(node) { - // Grammar checking for withStatement - if (!checkGrammarStatementInAmbientContext(node)) { - if (node.flags & 262144 /* AwaitContext */) { - grammarErrorOnFirstToken(node, ts.Diagnostics.with_statements_are_not_allowed_in_an_async_function_block); + var errorNode = node.name || node; + if (isConstructor) { + error(errorNode, ts.Diagnostics.Constructor_implementation_is_missing); } - } - checkExpression(node.expression); - error(node.expression, ts.Diagnostics.All_symbols_within_a_with_block_will_be_resolved_to_any); - } - function checkSwitchStatement(node) { - // Grammar checking - checkGrammarStatementInAmbientContext(node); - var firstDefaultClause; - var hasDuplicateDefaultClause = false; - var expressionType = checkExpression(node.expression); - ts.forEach(node.caseBlock.clauses, function (clause) { - // Grammar check for duplicate default clauses, skip if we already report duplicate default clause - if (clause.kind === 250 /* DefaultClause */ && !hasDuplicateDefaultClause) { - if (firstDefaultClause === undefined) { - firstDefaultClause = clause; + else { + // Report different errors regarding non-consecutive blocks of declarations depending on whether + // the node in question is abstract. + if (ts.getModifierFlags(node) & 128 /* Abstract */) { + error(errorNode, ts.Diagnostics.All_declarations_of_an_abstract_method_must_be_consecutive); } else { - var sourceFile = ts.getSourceFileOfNode(node); - var start = ts.skipTrivia(sourceFile.text, clause.pos); - var end = clause.statements.length > 0 ? clause.statements[0].pos : clause.end; - grammarErrorAtPos(sourceFile, start, end - start, ts.Diagnostics.A_default_clause_cannot_appear_more_than_once_in_a_switch_statement); - hasDuplicateDefaultClause = true; - } - } - if (produceDiagnostics && clause.kind === 249 /* CaseClause */) { - var caseClause = clause; - // TypeScript 1.0 spec (April 2014): 5.9 - // In a 'switch' statement, each 'case' expression must be of a type that is comparable - // to or from the type of the 'switch' expression. - var caseType = checkExpression(caseClause.expression); - if (!isTypeEqualityComparableTo(expressionType, caseType)) { - // expressionType is not comparable to caseType, try the reversed check and report errors if it fails - checkTypeComparableTo(caseType, expressionType, caseClause.expression, /*headMessage*/ undefined); - } - } - ts.forEach(clause.statements, checkSourceElement); - }); - if (node.caseBlock.locals) { - registerForUnusedIdentifiersCheck(node.caseBlock); - } - } - function checkLabeledStatement(node) { - // Grammar checking - if (!checkGrammarStatementInAmbientContext(node)) { - var current = node.parent; - while (current) { - if (ts.isFunctionLike(current)) { - break; - } - if (current.kind === 214 /* LabeledStatement */ && current.label.text === node.label.text) { - var sourceFile = ts.getSourceFileOfNode(node); - grammarErrorOnNode(node.label, ts.Diagnostics.Duplicate_label_0, ts.getTextOfNodeFromSourceText(sourceFile.text, node.label)); - break; + error(errorNode, ts.Diagnostics.Function_implementation_is_missing_or_not_immediately_following_the_declaration); } - current = current.parent; } } - // ensure that label is unique - checkSourceElement(node.statement); - } - function checkThrowStatement(node) { - // Grammar checking - if (!checkGrammarStatementInAmbientContext(node)) { - if (node.expression === undefined) { - grammarErrorAfterFirstToken(node, ts.Diagnostics.Line_break_not_permitted_here); + var duplicateFunctionDeclaration = false; + var multipleConstructorImplementation = false; + for (var _i = 0, declarations_4 = declarations; _i < declarations_4.length; _i++) { + var current = declarations_4[_i]; + var node = current; + var inAmbientContext = ts.isInAmbientContext(node); + var inAmbientContextOrInterface = node.parent.kind === 222 /* InterfaceDeclaration */ || node.parent.kind === 159 /* TypeLiteral */ || inAmbientContext; + if (inAmbientContextOrInterface) { + // check if declarations are consecutive only if they are non-ambient + // 1. ambient declarations can be interleaved + // i.e. this is legal + // declare function foo(); + // declare function bar(); + // declare function foo(); + // 2. mixing ambient and non-ambient declarations is a separate error that will be reported - do not want to report an extra one + previousDeclaration = undefined; } - } - if (node.expression) { - checkExpression(node.expression); - } - } - function checkTryStatement(node) { - // Grammar checking - checkGrammarStatementInAmbientContext(node); - checkBlock(node.tryBlock); - var catchClause = node.catchClause; - if (catchClause) { - // Grammar checking - if (catchClause.variableDeclaration) { - if (catchClause.variableDeclaration.name.kind !== 69 /* Identifier */) { - grammarErrorOnFirstToken(catchClause.variableDeclaration.name, ts.Diagnostics.Catch_clause_variable_name_must_be_an_identifier); + if (node.kind === 220 /* FunctionDeclaration */ || node.kind === 147 /* MethodDeclaration */ || node.kind === 146 /* MethodSignature */ || node.kind === 148 /* Constructor */) { + var currentNodeFlags = getEffectiveDeclarationFlags(node, flagsToCheck); + someNodeFlags |= currentNodeFlags; + allNodeFlags &= currentNodeFlags; + someHaveQuestionToken = someHaveQuestionToken || ts.hasQuestionToken(node); + allHaveQuestionToken = allHaveQuestionToken && ts.hasQuestionToken(node); + if (ts.nodeIsPresent(node.body) && bodyDeclaration) { + if (isConstructor) { + multipleConstructorImplementation = true; + } + else { + duplicateFunctionDeclaration = true; + } } - else if (catchClause.variableDeclaration.type) { - grammarErrorOnFirstToken(catchClause.variableDeclaration.type, ts.Diagnostics.Catch_clause_variable_cannot_have_a_type_annotation); + else if (previousDeclaration && previousDeclaration.parent === node.parent && previousDeclaration.end !== node.pos) { + reportImplementationExpectedError(previousDeclaration); } - else if (catchClause.variableDeclaration.initializer) { - grammarErrorOnFirstToken(catchClause.variableDeclaration.initializer, ts.Diagnostics.Catch_clause_variable_cannot_have_an_initializer); + if (ts.nodeIsPresent(node.body)) { + if (!bodyDeclaration) { + bodyDeclaration = node; + } } else { - var identifierName = catchClause.variableDeclaration.name.text; - var locals = catchClause.block.locals; - if (locals) { - var localSymbol = locals[identifierName]; - if (localSymbol && (localSymbol.flags & 2 /* BlockScopedVariable */) !== 0) { - grammarErrorOnNode(localSymbol.valueDeclaration, ts.Diagnostics.Cannot_redeclare_identifier_0_in_catch_clause, identifierName); - } - } + hasOverloads = true; + } + previousDeclaration = node; + if (!inAmbientContextOrInterface) { + lastSeenNonAmbientDeclaration = node; } } - checkBlock(catchClause.block); } - if (node.finallyBlock) { - checkBlock(node.finallyBlock); + if (multipleConstructorImplementation) { + ts.forEach(declarations, function (declaration) { + error(declaration, ts.Diagnostics.Multiple_constructor_implementations_are_not_allowed); + }); } - } - function checkIndexConstraints(type) { - var declaredNumberIndexer = getIndexDeclarationOfSymbol(type.symbol, 1 /* Number */); - var declaredStringIndexer = getIndexDeclarationOfSymbol(type.symbol, 0 /* String */); - var stringIndexType = getIndexTypeOfType(type, 0 /* String */); - var numberIndexType = getIndexTypeOfType(type, 1 /* Number */); - if (stringIndexType || numberIndexType) { - ts.forEach(getPropertiesOfObjectType(type), function (prop) { - var propType = getTypeOfSymbol(prop); - checkIndexConstraintForProperty(prop, propType, type, declaredStringIndexer, stringIndexType, 0 /* String */); - checkIndexConstraintForProperty(prop, propType, type, declaredNumberIndexer, numberIndexType, 1 /* Number */); + if (duplicateFunctionDeclaration) { + ts.forEach(declarations, function (declaration) { + error(declaration.name, ts.Diagnostics.Duplicate_function_implementation); }); - if (type.flags & 32768 /* Class */ && ts.isClassLike(type.symbol.valueDeclaration)) { - var classDeclaration = type.symbol.valueDeclaration; - for (var _i = 0, _a = classDeclaration.members; _i < _a.length; _i++) { - var member = _a[_i]; - // Only process instance properties with computed names here. - // Static properties cannot be in conflict with indexers, - // and properties with literal names were already checked. - if (!(ts.getModifierFlags(member) & 32 /* Static */) && ts.hasDynamicName(member)) { - var propType = getTypeOfSymbol(member.symbol); - checkIndexConstraintForProperty(member.symbol, propType, type, declaredStringIndexer, stringIndexType, 0 /* String */); - checkIndexConstraintForProperty(member.symbol, propType, type, declaredNumberIndexer, numberIndexType, 1 /* Number */); + } + // Abstract methods can't have an implementation -- in particular, they don't need one. + if (lastSeenNonAmbientDeclaration && !lastSeenNonAmbientDeclaration.body && + !(ts.getModifierFlags(lastSeenNonAmbientDeclaration) & 128 /* Abstract */) && !lastSeenNonAmbientDeclaration.questionToken) { + reportImplementationExpectedError(lastSeenNonAmbientDeclaration); + } + if (hasOverloads) { + checkFlagAgreementBetweenOverloads(declarations, bodyDeclaration, flagsToCheck, someNodeFlags, allNodeFlags); + checkQuestionTokenAgreementBetweenOverloads(declarations, bodyDeclaration, someHaveQuestionToken, allHaveQuestionToken); + if (bodyDeclaration) { + var signatures = getSignaturesOfSymbol(symbol); + var bodySignature = getSignatureFromDeclaration(bodyDeclaration); + for (var _a = 0, signatures_3 = signatures; _a < signatures_3.length; _a++) { + var signature = signatures_3[_a]; + if (!isImplementationCompatibleWithOverload(bodySignature, signature)) { + error(signature.declaration, ts.Diagnostics.Overload_signature_is_not_compatible_with_function_implementation); + break; } } } } - var errorNode; - if (stringIndexType && numberIndexType) { - errorNode = declaredNumberIndexer || declaredStringIndexer; - // condition 'errorNode === undefined' may appear if types does not declare nor string neither number indexer - if (!errorNode && (type.flags & 65536 /* Interface */)) { - var someBaseTypeHasBothIndexers = ts.forEach(getBaseTypes(type), function (base) { return getIndexTypeOfType(base, 0 /* String */) && getIndexTypeOfType(base, 1 /* Number */); }); - errorNode = someBaseTypeHasBothIndexers ? undefined : type.symbol.declarations[0]; - } - } - if (errorNode && !isTypeAssignableTo(numberIndexType, stringIndexType)) { - error(errorNode, ts.Diagnostics.Numeric_index_type_0_is_not_assignable_to_string_index_type_1, typeToString(numberIndexType), typeToString(stringIndexType)); + } + function checkExportsOnMergedDeclarations(node) { + if (!produceDiagnostics) { + return; } - function checkIndexConstraintForProperty(prop, propertyType, containingType, indexDeclaration, indexType, indexKind) { - if (!indexType) { - return; - } - // index is numeric and property name is not valid numeric literal - if (indexKind === 1 /* Number */ && !isNumericName(prop.valueDeclaration.name)) { + // if localSymbol is defined on node then node itself is exported - check is required + var symbol = node.localSymbol; + if (!symbol) { + // local symbol is undefined => this declaration is non-exported. + // however symbol might contain other declarations that are exported + symbol = getSymbolOfNode(node); + if (!(symbol.flags & 7340032 /* Export */)) { + // this is a pure local symbol (all declarations are non-exported) - no need to check anything return; } - // perform property check if property or indexer is declared in 'type' - // this allows to rule out cases when both property and indexer are inherited from the base class - var errorNode; - if (prop.valueDeclaration.name.kind === 140 /* ComputedPropertyName */ || prop.parent === containingType.symbol) { - errorNode = prop.valueDeclaration; - } - else if (indexDeclaration) { - errorNode = indexDeclaration; - } - else if (containingType.flags & 65536 /* Interface */) { - // for interfaces property and indexer might be inherited from different bases - // check if any base class already has both property and indexer. - // check should be performed only if 'type' is the first type that brings property\indexer together - var someBaseClassHasBothPropertyAndIndexer = ts.forEach(getBaseTypes(containingType), function (base) { return getPropertyOfObjectType(base, prop.name) && getIndexTypeOfType(base, indexKind); }); - errorNode = someBaseClassHasBothPropertyAndIndexer ? undefined : containingType.symbol.declarations[0]; - } - if (errorNode && !isTypeAssignableTo(propertyType, indexType)) { - var errorMessage = indexKind === 0 /* String */ - ? ts.Diagnostics.Property_0_of_type_1_is_not_assignable_to_string_index_type_2 - : ts.Diagnostics.Property_0_of_type_1_is_not_assignable_to_numeric_index_type_2; - error(errorNode, errorMessage, symbolToString(prop), typeToString(propertyType), typeToString(indexType)); - } } - } - function checkTypeNameIsReserved(name, message) { - // TS 1.0 spec (April 2014): 3.6.1 - // The predefined type keywords are reserved and cannot be used as names of user defined types. - switch (name.text) { - case "any": - case "number": - case "boolean": - case "string": - case "symbol": - case "void": - error(name, message, name.text); + // run the check only for the first declaration in the list + if (ts.getDeclarationOfKind(symbol, node.kind) !== node) { + return; } - } - /** Check each type parameter and check that type parameters have no duplicate type parameter declarations */ - function checkTypeParameters(typeParameterDeclarations) { - if (typeParameterDeclarations) { - for (var i = 0, n = typeParameterDeclarations.length; i < n; i++) { - var node = typeParameterDeclarations[i]; - checkTypeParameter(node); - if (produceDiagnostics) { - for (var j = 0; j < i; j++) { - if (typeParameterDeclarations[j].symbol === node.symbol) { - error(node.name, ts.Diagnostics.Duplicate_identifier_0, ts.declarationNameToString(node.name)); - } - } + // we use SymbolFlags.ExportValue, SymbolFlags.ExportType and SymbolFlags.ExportNamespace + // to denote disjoint declarationSpaces (without making new enum type). + var exportedDeclarationSpaces = 0 /* None */; + var nonExportedDeclarationSpaces = 0 /* None */; + var defaultExportedDeclarationSpaces = 0 /* None */; + for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { + var d = _a[_i]; + var declarationSpaces = getDeclarationSpaces(d); + var effectiveDeclarationFlags = getEffectiveDeclarationFlags(d, 1 /* Export */ | 512 /* Default */); + if (effectiveDeclarationFlags & 1 /* Export */) { + if (effectiveDeclarationFlags & 512 /* Default */) { + defaultExportedDeclarationSpaces |= declarationSpaces; + } + else { + exportedDeclarationSpaces |= declarationSpaces; } } + else { + nonExportedDeclarationSpaces |= declarationSpaces; + } } - } - /** Check that type parameter lists are identical across multiple declarations */ - function checkTypeParameterListsIdentical(node, symbol) { - if (symbol.declarations.length === 1) { - return; - } - var firstDecl; - for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { - var declaration = _a[_i]; - if (declaration.kind === 221 /* ClassDeclaration */ || declaration.kind === 222 /* InterfaceDeclaration */) { - if (!firstDecl) { - firstDecl = declaration; + // Spaces for anything not declared a 'default export'. + var nonDefaultExportedDeclarationSpaces = exportedDeclarationSpaces | nonExportedDeclarationSpaces; + var commonDeclarationSpacesForExportsAndLocals = exportedDeclarationSpaces & nonExportedDeclarationSpaces; + var commonDeclarationSpacesForDefaultAndNonDefault = defaultExportedDeclarationSpaces & nonDefaultExportedDeclarationSpaces; + if (commonDeclarationSpacesForExportsAndLocals || commonDeclarationSpacesForDefaultAndNonDefault) { + // declaration spaces for exported and non-exported declarations intersect + for (var _b = 0, _c = symbol.declarations; _b < _c.length; _b++) { + var d = _c[_b]; + var declarationSpaces = getDeclarationSpaces(d); + // Only error on the declarations that contributed to the intersecting spaces. + if (declarationSpaces & commonDeclarationSpacesForDefaultAndNonDefault) { + error(d.name, ts.Diagnostics.Merged_declaration_0_cannot_include_a_default_export_declaration_Consider_adding_a_separate_export_default_0_declaration_instead, ts.declarationNameToString(d.name)); } - else if (!areTypeParametersIdentical(firstDecl.typeParameters, node.typeParameters)) { - error(node.name, ts.Diagnostics.All_declarations_of_0_must_have_identical_type_parameters, node.name.text); + else if (declarationSpaces & commonDeclarationSpacesForExportsAndLocals) { + error(d.name, ts.Diagnostics.Individual_declarations_in_merged_declaration_0_must_be_all_exported_or_all_local, ts.declarationNameToString(d.name)); } } } - } - function checkClassExpression(node) { - checkClassLikeDeclaration(node); - checkNodeDeferred(node); - return getTypeOfSymbol(getSymbolOfNode(node)); - } - function checkClassExpressionDeferred(node) { - ts.forEach(node.members, checkSourceElement); - registerForUnusedIdentifiersCheck(node); - } - function checkClassDeclaration(node) { - if (!node.name && !(ts.getModifierFlags(node) & 512 /* Default */)) { - grammarErrorOnFirstToken(node, ts.Diagnostics.A_class_declaration_without_the_default_modifier_must_have_a_name); + function getDeclarationSpaces(d) { + switch (d.kind) { + case 222 /* InterfaceDeclaration */: + return 2097152 /* ExportType */; + case 225 /* ModuleDeclaration */: + return ts.isAmbientModule(d) || ts.getModuleInstanceState(d) !== 0 /* NonInstantiated */ + ? 4194304 /* ExportNamespace */ | 1048576 /* ExportValue */ + : 4194304 /* ExportNamespace */; + case 221 /* ClassDeclaration */: + case 224 /* EnumDeclaration */: + return 2097152 /* ExportType */ | 1048576 /* ExportValue */; + case 229 /* ImportEqualsDeclaration */: + var result_2 = 0; + var target = resolveAlias(getSymbolOfNode(d)); + ts.forEach(target.declarations, function (d) { result_2 |= getDeclarationSpaces(d); }); + return result_2; + default: + return 1048576 /* ExportValue */; + } } - checkClassLikeDeclaration(node); - ts.forEach(node.members, checkSourceElement); - registerForUnusedIdentifiersCheck(node); } - function checkClassLikeDeclaration(node) { - checkGrammarClassDeclarationHeritageClauses(node); - checkDecorators(node); - if (node.name) { - checkTypeNameIsReserved(node.name, ts.Diagnostics.Class_name_cannot_be_0); - checkCollisionWithCapturedThisVariable(node, node.name); - checkCollisionWithRequireExportsInGeneratedCode(node, node.name); - checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); - } - checkTypeParameters(node.typeParameters); - checkExportsOnMergedDeclarations(node); - var symbol = getSymbolOfNode(node); - var type = getDeclaredTypeOfSymbol(symbol); - var typeWithThis = getTypeWithThisArgument(type); - var staticType = getTypeOfSymbol(symbol); - checkTypeParameterListsIdentical(node, symbol); - checkClassForDuplicateDeclarations(node); - var baseTypeNode = ts.getClassExtendsHeritageClauseElement(node); - if (baseTypeNode) { - var baseTypes = getBaseTypes(type); - if (baseTypes.length && produceDiagnostics) { - var baseType_1 = baseTypes[0]; - var staticBaseType = getBaseConstructorTypeOfClass(type); - checkBaseTypeAccessibility(staticBaseType, baseTypeNode); - checkSourceElement(baseTypeNode.expression); - if (baseTypeNode.typeArguments) { - ts.forEach(baseTypeNode.typeArguments, checkSourceElement); - for (var _i = 0, _a = getConstructorsForTypeArguments(staticBaseType, baseTypeNode.typeArguments); _i < _a.length; _i++) { - var constructor = _a[_i]; - if (!checkTypeArgumentConstraints(constructor.typeParameters, baseTypeNode.typeArguments)) { - break; - } - } - } - checkTypeAssignableTo(typeWithThis, getTypeWithThisArgument(baseType_1, type.thisType), node.name || node, ts.Diagnostics.Class_0_incorrectly_extends_base_class_1); - checkTypeAssignableTo(staticType, getTypeWithoutSignatures(staticBaseType), node.name || node, ts.Diagnostics.Class_static_side_0_incorrectly_extends_base_class_static_side_1); - if (baseType_1.symbol.valueDeclaration && !ts.isInAmbientContext(baseType_1.symbol.valueDeclaration)) { - if (!isBlockScopedNameDeclaredBeforeUse(baseType_1.symbol.valueDeclaration, node)) { - error(baseTypeNode, ts.Diagnostics.A_class_must_be_declared_after_its_base_class); - } - } - if (!(staticBaseType.symbol && staticBaseType.symbol.flags & 32 /* Class */)) { - // When the static base type is a "class-like" constructor function (but not actually a class), we verify - // that all instantiated base constructor signatures return the same type. We can simply compare the type - // references (as opposed to checking the structure of the types) because elsewhere we have already checked - // that the base type is a class or interface type (and not, for example, an anonymous object type). - var constructors = getInstantiatedConstructorsForTypeArguments(staticBaseType, baseTypeNode.typeArguments); - if (ts.forEach(constructors, function (sig) { return getReturnTypeOfSignature(sig) !== baseType_1; })) { - error(baseTypeNode.expression, ts.Diagnostics.Base_constructors_must_all_have_the_same_return_type); - } + function checkNonThenableType(type, location, message) { + type = getWidenedType(type); + if (!isTypeAny(type) && !isTypeNever(type) && isTypeAssignableTo(type, getGlobalThenableType())) { + if (location) { + if (!message) { + message = ts.Diagnostics.Operand_for_await_does_not_have_a_valid_callable_then_member; } - checkKindsOfPropertyMemberOverrides(type, baseType_1); + error(location, message); } + return unknownType; } - var implementedTypeNodes = ts.getClassImplementsHeritageClauseElements(node); - if (implementedTypeNodes) { - for (var _b = 0, implementedTypeNodes_1 = implementedTypeNodes; _b < implementedTypeNodes_1.length; _b++) { - var typeRefNode = implementedTypeNodes_1[_b]; - if (!ts.isEntityNameExpression(typeRefNode.expression)) { - error(typeRefNode.expression, ts.Diagnostics.A_class_can_only_implement_an_identifier_Slashqualified_name_with_optional_type_arguments); - } - checkTypeReferenceNode(typeRefNode); - if (produceDiagnostics) { - var t = getTypeFromTypeNode(typeRefNode); - if (t !== unknownType) { - var declaredType = (t.flags & 131072 /* Reference */) ? t.target : t; - if (declaredType.flags & (32768 /* Class */ | 65536 /* Interface */)) { - checkTypeAssignableTo(typeWithThis, getTypeWithThisArgument(t, type.thisType), node.name || node, ts.Diagnostics.Class_0_incorrectly_implements_interface_1); - } - else { - error(typeRefNode, ts.Diagnostics.A_class_may_only_implement_another_class_or_interface); - } - } - } + return type; + } + /** + * Gets the "promised type" of a promise. + * @param type The type of the promise. + * @remarks The "promised type" of a type is the type of the "value" parameter of the "onfulfilled" callback. + */ + function getPromisedType(promise) { + // + // { // promise + // then( // thenFunction + // onfulfilled: ( // onfulfilledParameterType + // value: T // valueParameterType + // ) => any + // ): any; + // } + // + if (isTypeAny(promise)) { + return undefined; + } + if (promise.flags & 131072 /* Reference */) { + if (promise.target === tryGetGlobalPromiseType() + || promise.target === getGlobalPromiseLikeType()) { + return promise.typeArguments[0]; } } - if (produceDiagnostics) { - checkIndexConstraints(type); - checkTypeForDuplicateIndexSignatures(node); + var globalPromiseLikeType = getInstantiatedGlobalPromiseLikeType(); + if (globalPromiseLikeType === emptyObjectType || !isTypeAssignableTo(promise, globalPromiseLikeType)) { + return undefined; } - } - function checkBaseTypeAccessibility(type, node) { - var signatures = getSignaturesOfType(type, 1 /* Construct */); - if (signatures.length) { - var declaration = signatures[0].declaration; - if (declaration && ts.getModifierFlags(declaration) & 8 /* Private */) { - var typeClassDeclaration = getClassLikeDeclarationOfSymbol(type.symbol); - if (!isNodeWithinClass(node, typeClassDeclaration)) { - error(node, ts.Diagnostics.Cannot_extend_a_class_0_Class_constructor_is_marked_as_private, node.expression.text); - } - } + var thenFunction = getTypeOfPropertyOfType(promise, "then"); + if (!thenFunction || isTypeAny(thenFunction)) { + return undefined; + } + var thenSignatures = getSignaturesOfType(thenFunction, 0 /* Call */); + if (thenSignatures.length === 0) { + return undefined; + } + var onfulfilledParameterType = getTypeWithFacts(getUnionType(ts.map(thenSignatures, getTypeOfFirstParameterOfSignature)), 131072 /* NEUndefined */); + if (isTypeAny(onfulfilledParameterType)) { + return undefined; + } + var onfulfilledParameterSignatures = getSignaturesOfType(onfulfilledParameterType, 0 /* Call */); + if (onfulfilledParameterSignatures.length === 0) { + return undefined; } + return getUnionType(ts.map(onfulfilledParameterSignatures, getTypeOfFirstParameterOfSignature), /*subtypeReduction*/ true); } - function getTargetSymbol(s) { - // if symbol is instantiated its flags are not copied from the 'target' - // so we'll need to get back original 'target' symbol to work with correct set of flags - return s.flags & 16777216 /* Instantiated */ ? getSymbolLinks(s).target : s; + function getTypeOfFirstParameterOfSignature(signature) { + return signature.parameters.length > 0 ? getTypeAtPosition(signature, 0) : neverType; } - function getClassLikeDeclarationOfSymbol(symbol) { - return ts.forEach(symbol.declarations, function (d) { return ts.isClassLike(d) ? d : undefined; }); + /** + * Gets the "awaited type" of a type. + * @param type The type to await. + * @remarks The "awaited type" of an expression is its "promised type" if the expression is a + * Promise-like type; otherwise, it is the type of the expression. This is used to reflect + * The runtime behavior of the `await` keyword. + */ + function getAwaitedType(type) { + return checkAwaitedType(type, /*location*/ undefined, /*message*/ undefined); } - function checkKindsOfPropertyMemberOverrides(type, baseType) { - // TypeScript 1.0 spec (April 2014): 8.2.3 - // A derived class inherits all members from its base class it doesn't override. - // Inheritance means that a derived class implicitly contains all non - overridden members of the base class. - // Both public and private property members are inherited, but only public property members can be overridden. - // A property member in a derived class is said to override a property member in a base class - // when the derived class property member has the same name and kind(instance or static) - // as the base class property member. - // The type of an overriding property member must be assignable(section 3.8.4) - // to the type of the overridden property member, or otherwise a compile - time error occurs. - // Base class instance member functions can be overridden by derived class instance member functions, - // but not by other kinds of members. - // Base class instance member variables and accessors can be overridden by - // derived class instance member variables and accessors, but not by other kinds of members. - // NOTE: assignability is checked in checkClassDeclaration - var baseProperties = getPropertiesOfObjectType(baseType); - for (var _i = 0, baseProperties_1 = baseProperties; _i < baseProperties_1.length; _i++) { - var baseProperty = baseProperties_1[_i]; - var base = getTargetSymbol(baseProperty); - if (base.flags & 134217728 /* Prototype */) { - continue; + function checkAwaitedType(type, location, message) { + return checkAwaitedTypeWorker(type); + function checkAwaitedTypeWorker(type) { + if (type.flags & 524288 /* Union */) { + var types = []; + for (var _i = 0, _a = type.types; _i < _a.length; _i++) { + var constituentType = _a[_i]; + types.push(checkAwaitedTypeWorker(constituentType)); + } + return getUnionType(types, /*subtypeReduction*/ true); } - var derived = getTargetSymbol(getPropertyOfObjectType(type, base.name)); - var baseDeclarationFlags = getDeclarationModifierFlagsFromSymbol(base); - ts.Debug.assert(!!derived, "derived should point to something, even if it is the base class' declaration."); - if (derived) { - // In order to resolve whether the inherited method was overridden in the base class or not, - // we compare the Symbols obtained. Since getTargetSymbol returns the symbol on the *uninstantiated* - // type declaration, derived and base resolve to the same symbol even in the case of generic classes. - if (derived === base) { - // derived class inherits base without override/redeclaration - var derivedClassDecl = getClassLikeDeclarationOfSymbol(type.symbol); - // It is an error to inherit an abstract member without implementing it or being declared abstract. - // If there is no declaration for the derived class (as in the case of class expressions), - // then the class cannot be declared abstract. - if (baseDeclarationFlags & 128 /* Abstract */ && (!derivedClassDecl || !(ts.getModifierFlags(derivedClassDecl) & 128 /* Abstract */))) { - if (derivedClassDecl.kind === 192 /* ClassExpression */) { - error(derivedClassDecl, ts.Diagnostics.Non_abstract_class_expression_does_not_implement_inherited_abstract_member_0_from_class_1, symbolToString(baseProperty), typeToString(baseType)); - } - else { - error(derivedClassDecl, ts.Diagnostics.Non_abstract_class_0_does_not_implement_inherited_abstract_member_1_from_class_2, typeToString(type), symbolToString(baseProperty), typeToString(baseType)); - } - } + else { + var promisedType = getPromisedType(type); + if (promisedType === undefined) { + // The type was not a PromiseLike, so it could not be unwrapped any further. + // As long as the type does not have a callable "then" property, it is + // safe to return the type; otherwise, an error will have been reported in + // the call to checkNonThenableType and we will return unknownType. + // + // An example of a non-promise "thenable" might be: + // + // await { then(): void {} } + // + // The "thenable" does not match the minimal definition for a PromiseLike. When + // a Promise/A+-compatible or ES6 promise tries to adopt this value, the promise + // will never settle. We treat this as an error to help flag an early indicator + // of a runtime problem. If the user wants to return this value from an async + // function, they would need to wrap it in some other value. If they want it to + // be treated as a promise, they can cast to . + return checkNonThenableType(type, location, message); } else { - // derived overrides base. - var derivedDeclarationFlags = getDeclarationModifierFlagsFromSymbol(derived); - if ((baseDeclarationFlags & 8 /* Private */) || (derivedDeclarationFlags & 8 /* Private */)) { - // either base or derived property is private - not override, skip it - continue; - } - if ((baseDeclarationFlags & 32 /* Static */) !== (derivedDeclarationFlags & 32 /* Static */)) { - // value of 'static' is not the same for properties - not override, skip it - continue; - } - if ((base.flags & derived.flags & 8192 /* Method */) || ((base.flags & 98308 /* PropertyOrAccessor */) && (derived.flags & 98308 /* PropertyOrAccessor */))) { - // method is overridden with method or property/accessor is overridden with property/accessor - correct case - continue; - } - var errorMessage = void 0; - if (base.flags & 8192 /* Method */) { - if (derived.flags & 98304 /* Accessor */) { - errorMessage = ts.Diagnostics.Class_0_defines_instance_member_function_1_but_extended_class_2_defines_it_as_instance_member_accessor; - } - else { - ts.Debug.assert((derived.flags & 4 /* Property */) !== 0); - errorMessage = ts.Diagnostics.Class_0_defines_instance_member_function_1_but_extended_class_2_defines_it_as_instance_member_property; + if (type.id === promisedType.id || ts.indexOf(awaitedTypeStack, promisedType.id) >= 0) { + // We have a bad actor in the form of a promise whose promised type is + // the same promise type, or a mutually recursive promise. Return the + // unknown type as we cannot guess the shape. If this were the actual + // case in the JavaScript, this Promise would never resolve. + // + // An example of a bad actor with a singly-recursive promise type might + // be: + // + // interface BadPromise { + // then( + // onfulfilled: (value: BadPromise) => any, + // onrejected: (error: any) => any): BadPromise; + // } + // + // The above interface will pass the PromiseLike check, and return a + // promised type of `BadPromise`. Since this is a self reference, we + // don't want to keep recursing ad infinitum. + // + // An example of a bad actor in the form of a mutually-recursive + // promise type might be: + // + // interface BadPromiseA { + // then( + // onfulfilled: (value: BadPromiseB) => any, + // onrejected: (error: any) => any): BadPromiseB; + // } + // + // interface BadPromiseB { + // then( + // onfulfilled: (value: BadPromiseA) => any, + // onrejected: (error: any) => any): BadPromiseA; + // } + // + if (location) { + error(location, ts.Diagnostics._0_is_referenced_directly_or_indirectly_in_the_fulfillment_callback_of_its_own_then_method, symbolToString(type.symbol)); } + return unknownType; } - else if (base.flags & 4 /* Property */) { - ts.Debug.assert((derived.flags & 8192 /* Method */) !== 0); - errorMessage = ts.Diagnostics.Class_0_defines_instance_member_property_1_but_extended_class_2_defines_it_as_instance_member_function; - } - else { - ts.Debug.assert((base.flags & 98304 /* Accessor */) !== 0); - ts.Debug.assert((derived.flags & 8192 /* Method */) !== 0); - errorMessage = ts.Diagnostics.Class_0_defines_instance_member_accessor_1_but_extended_class_2_defines_it_as_instance_member_function; - } - error(derived.valueDeclaration.name, errorMessage, typeToString(baseType), symbolToString(base), typeToString(type)); + // Keep track of the type we're about to unwrap to avoid bad recursive promise types. + // See the comments above for more information. + awaitedTypeStack.push(type.id); + var awaitedType = checkAwaitedTypeWorker(promisedType); + awaitedTypeStack.pop(); + return awaitedType; } } } } - function isAccessor(kind) { - return kind === 149 /* GetAccessor */ || kind === 150 /* SetAccessor */; + /** + * Checks that the return type provided is an instantiation of the global Promise type + * and returns the awaited type of the return type. + * + * @param returnType The return type of a FunctionLikeDeclaration + * @param location The node on which to report the error. + */ + function checkCorrectPromiseType(returnType, location, diagnostic, typeName) { + if (returnType === unknownType) { + // The return type already had some other error, so we ignore and return + // the unknown type. + return unknownType; + } + var globalPromiseType = getGlobalPromiseType(); + if (globalPromiseType === emptyGenericType + || globalPromiseType === getTargetType(returnType)) { + // Either we couldn't resolve the global promise type, which would have already + // reported an error, or we could resolve it and the return type is a valid type + // reference to the global type. In either case, we return the awaited type for + // the return type. + return checkAwaitedType(returnType, location, ts.Diagnostics.An_async_function_or_method_must_have_a_valid_awaitable_return_type); + } + // The promise type was not a valid type reference to the global promise type, so we + // report an error and return the unknown type. + error(location, diagnostic, typeName); + return unknownType; } - function areTypeParametersIdentical(list1, list2) { - if (!list1 && !list2) { - return true; + /** + * Checks the return type of an async function to ensure it is a compatible + * Promise implementation. + * @param node The signature to check + * @param returnType The return type for the function + * @remarks + * This checks that an async function has a valid Promise-compatible return type, + * and returns the *awaited type* of the promise. An async function has a valid + * Promise-compatible return type if the resolved value of the return type has a + * construct signature that takes in an `initializer` function that in turn supplies + * a `resolve` function as one of its arguments and results in an object with a + * callable `then` signature. + */ + function checkAsyncFunctionReturnType(node) { + if (languageVersion >= 2 /* ES6 */) { + var returnType = getTypeFromTypeNode(node.type); + return checkCorrectPromiseType(returnType, node.type, ts.Diagnostics.The_return_type_of_an_async_function_or_method_must_be_the_global_Promise_T_type); } - if (!list1 || !list2 || list1.length !== list2.length) { - return false; + var globalPromiseConstructorLikeType = getGlobalPromiseConstructorLikeType(); + if (globalPromiseConstructorLikeType === emptyObjectType) { + // If we couldn't resolve the global PromiseConstructorLike type we cannot verify + // compatibility with __awaiter. + return unknownType; } - // TypeScript 1.0 spec (April 2014): - // When a generic interface has multiple declarations, all declarations must have identical type parameter - // lists, i.e. identical type parameter names with identical constraints in identical order. - for (var i = 0, len = list1.length; i < len; i++) { - var tp1 = list1[i]; - var tp2 = list2[i]; - if (tp1.name.text !== tp2.name.text) { - return false; - } - if (!tp1.constraint && !tp2.constraint) { - continue; - } - if (!tp1.constraint || !tp2.constraint) { - return false; - } - if (!isTypeIdenticalTo(getTypeFromTypeNode(tp1.constraint), getTypeFromTypeNode(tp2.constraint))) { - return false; - } + // As part of our emit for an async function, we will need to emit the entity name of + // the return type annotation as an expression. To meet the necessary runtime semantics + // for __awaiter, we must also check that the type of the declaration (e.g. the static + // side or "constructor" of the promise type) is compatible `PromiseConstructorLike`. + // + // An example might be (from lib.es6.d.ts): + // + // interface Promise { ... } + // interface PromiseConstructor { + // new (...): Promise; + // } + // declare var Promise: PromiseConstructor; + // + // When an async function declares a return type annotation of `Promise`, we + // need to get the type of the `Promise` variable declaration above, which would + // be `PromiseConstructor`. + // + // The same case applies to a class: + // + // declare class Promise { + // constructor(...); + // then(...): Promise; + // } + // + // When we get the type of the `Promise` symbol here, we get the type of the static + // side of the `Promise` class, which would be `{ new (...): Promise }`. + var promiseType = getTypeFromTypeNode(node.type); + if (promiseType === unknownType && compilerOptions.isolatedModules) { + // If we are compiling with isolatedModules, we may not be able to resolve the + // type as a value. As such, we will just return unknownType; + return unknownType; } - return true; + var promiseConstructor = getNodeLinks(node.type).resolvedSymbol; + if (!promiseConstructor || !symbolIsValue(promiseConstructor)) { + // try to fall back to global promise type. + var typeName = promiseConstructor + ? symbolToString(promiseConstructor) + : typeToString(promiseType); + return checkCorrectPromiseType(promiseType, node.type, ts.Diagnostics.Type_0_is_not_a_valid_async_function_return_type, typeName); + } + // If the Promise constructor, resolved locally, is an alias symbol we should mark it as referenced. + checkReturnTypeAnnotationAsExpression(node); + // Validate the promise constructor type. + var promiseConstructorType = getTypeOfSymbol(promiseConstructor); + if (!checkTypeAssignableTo(promiseConstructorType, globalPromiseConstructorLikeType, node.type, ts.Diagnostics.Type_0_is_not_a_valid_async_function_return_type)) { + return unknownType; + } + // Verify there is no local declaration that could collide with the promise constructor. + var promiseName = ts.getEntityNameFromTypeNode(node.type); + var promiseNameOrNamespaceRoot = getFirstIdentifier(promiseName); + var rootSymbol = getSymbol(node.locals, promiseNameOrNamespaceRoot.text, 107455 /* Value */); + if (rootSymbol) { + error(rootSymbol.valueDeclaration, ts.Diagnostics.Duplicate_identifier_0_Compiler_uses_declaration_1_to_support_async_functions, promiseNameOrNamespaceRoot.text, getFullyQualifiedName(promiseConstructor)); + return unknownType; + } + // Get and return the awaited type of the return type. + return checkAwaitedType(promiseType, node, ts.Diagnostics.An_async_function_or_method_must_have_a_valid_awaitable_return_type); } - function checkInheritedPropertiesAreIdentical(type, typeNode) { - var baseTypes = getBaseTypes(type); - if (baseTypes.length < 2) { - return true; + /** Check a decorator */ + function checkDecorator(node) { + var signature = getResolvedSignature(node); + var returnType = getReturnTypeOfSignature(signature); + if (returnType.flags & 1 /* Any */) { + return; } - var seen = ts.createMap(); - ts.forEach(resolveDeclaredMembers(type).declaredProperties, function (p) { seen[p.name] = { prop: p, containingType: type }; }); - var ok = true; - for (var _i = 0, baseTypes_2 = baseTypes; _i < baseTypes_2.length; _i++) { - var base = baseTypes_2[_i]; - var properties = getPropertiesOfObjectType(getTypeWithThisArgument(base, type.thisType)); - for (var _a = 0, properties_5 = properties; _a < properties_5.length; _a++) { - var prop = properties_5[_a]; - var existing = seen[prop.name]; - if (!existing) { - seen[prop.name] = { prop: prop, containingType: base }; - } - else { - var isInheritedProperty = existing.containingType !== type; - if (isInheritedProperty && !isPropertyIdenticalTo(existing.prop, prop)) { - ok = false; - var typeName1 = typeToString(existing.containingType); - var typeName2 = typeToString(base); - var errorInfo = ts.chainDiagnosticMessages(undefined, ts.Diagnostics.Named_property_0_of_types_1_and_2_are_not_identical, symbolToString(prop), typeName1, typeName2); - errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Interface_0_cannot_simultaneously_extend_types_1_and_2, typeToString(type), typeName1, typeName2); - diagnostics.add(ts.createDiagnosticForNodeFromMessageChain(typeNode, errorInfo)); - } + var expectedReturnType; + var headMessage = getDiagnosticHeadMessageForDecoratorResolution(node); + var errorInfo; + switch (node.parent.kind) { + case 221 /* ClassDeclaration */: + var classSymbol = getSymbolOfNode(node.parent); + var classConstructorType = getTypeOfSymbol(classSymbol); + expectedReturnType = getUnionType([classConstructorType, voidType]); + break; + case 142 /* Parameter */: + expectedReturnType = voidType; + errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.The_return_type_of_a_parameter_decorator_function_must_be_either_void_or_any); + break; + case 145 /* PropertyDeclaration */: + expectedReturnType = voidType; + errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.The_return_type_of_a_property_decorator_function_must_be_either_void_or_any); + break; + case 147 /* MethodDeclaration */: + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + var methodType = getTypeOfNode(node.parent); + var descriptorType = createTypedPropertyDescriptorType(methodType); + expectedReturnType = getUnionType([descriptorType, voidType]); + break; + } + checkTypeAssignableTo(returnType, expectedReturnType, node, headMessage, errorInfo); + } + /** Checks a type reference node as an expression. */ + function checkTypeNodeAsExpression(node) { + // When we are emitting type metadata for decorators, we need to try to check the type + // as if it were an expression so that we can emit the type in a value position when we + // serialize the type metadata. + if (node && node.kind === 155 /* TypeReference */) { + var root = getFirstIdentifier(node.typeName); + var meaning = root.parent.kind === 155 /* TypeReference */ ? 793064 /* Type */ : 1920 /* Namespace */; + // Resolve type so we know which symbol is referenced + var rootSymbol = resolveName(root, root.text, meaning | 8388608 /* Alias */, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined); + // Resolved symbol is alias + if (rootSymbol && rootSymbol.flags & 8388608 /* Alias */) { + var aliasTarget = resolveAlias(rootSymbol); + // If alias has value symbol - mark alias as referenced + if (aliasTarget.flags & 107455 /* Value */ && !isConstEnumOrConstEnumOnlyModule(resolveAlias(rootSymbol))) { + markAliasSymbolAsReferenced(rootSymbol); } } } - return ok; } - function checkInterfaceDeclaration(node) { - // Grammar checking - checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarInterfaceDeclaration(node); - checkTypeParameters(node.typeParameters); - if (produceDiagnostics) { - checkTypeNameIsReserved(node.name, ts.Diagnostics.Interface_name_cannot_be_0); - checkExportsOnMergedDeclarations(node); - var symbol = getSymbolOfNode(node); - checkTypeParameterListsIdentical(node, symbol); - // Only check this symbol once - var firstInterfaceDecl = ts.getDeclarationOfKind(symbol, 222 /* InterfaceDeclaration */); - if (node === firstInterfaceDecl) { - var type = getDeclaredTypeOfSymbol(symbol); - var typeWithThis = getTypeWithThisArgument(type); - // run subsequent checks only if first set succeeded - if (checkInheritedPropertiesAreIdentical(type, node.name)) { - for (var _i = 0, _a = getBaseTypes(type); _i < _a.length; _i++) { - var baseType = _a[_i]; - checkTypeAssignableTo(typeWithThis, getTypeWithThisArgument(baseType, type.thisType), node.name, ts.Diagnostics.Interface_0_incorrectly_extends_interface_1); + /** + * Checks the type annotation of an accessor declaration or property declaration as + * an expression if it is a type reference to a type with a value declaration. + */ + function checkTypeAnnotationAsExpression(node) { + checkTypeNodeAsExpression(node.type); + } + function checkReturnTypeAnnotationAsExpression(node) { + checkTypeNodeAsExpression(node.type); + } + /** Checks the type annotation of the parameters of a function/method or the constructor of a class as expressions */ + function checkParameterTypeAnnotationsAsExpressions(node) { + // ensure all type annotations with a value declaration are checked as an expression + for (var _i = 0, _a = node.parameters; _i < _a.length; _i++) { + var parameter = _a[_i]; + checkTypeAnnotationAsExpression(parameter); + } + } + /** Check the decorators of a node */ + function checkDecorators(node) { + if (!node.decorators) { + return; + } + // skip this check for nodes that cannot have decorators. These should have already had an error reported by + // checkGrammarDecorators. + if (!ts.nodeCanBeDecorated(node)) { + return; + } + if (!compilerOptions.experimentalDecorators) { + error(node, ts.Diagnostics.Experimental_support_for_decorators_is_a_feature_that_is_subject_to_change_in_a_future_release_Set_the_experimentalDecorators_option_to_remove_this_warning); + } + if (compilerOptions.emitDecoratorMetadata) { + // we only need to perform these checks if we are emitting serialized type metadata for the target of a decorator. + switch (node.kind) { + case 221 /* ClassDeclaration */: + var constructor = ts.getFirstConstructorWithBody(node); + if (constructor) { + checkParameterTypeAnnotationsAsExpressions(constructor); } - checkIndexConstraints(type); + break; + case 147 /* MethodDeclaration */: + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + checkParameterTypeAnnotationsAsExpressions(node); + checkReturnTypeAnnotationAsExpression(node); + break; + case 145 /* PropertyDeclaration */: + case 142 /* Parameter */: + checkTypeAnnotationAsExpression(node); + break; + } + } + ts.forEach(node.decorators, checkDecorator); + } + function checkFunctionDeclaration(node) { + if (produceDiagnostics) { + checkFunctionOrMethodDeclaration(node) || checkGrammarForGenerator(node); + checkCollisionWithCapturedSuperVariable(node, node.name); + checkCollisionWithCapturedThisVariable(node, node.name); + checkCollisionWithRequireExportsInGeneratedCode(node, node.name); + checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); + } + } + function checkFunctionOrMethodDeclaration(node) { + checkDecorators(node); + checkSignatureDeclaration(node); + var isAsync = ts.isAsyncFunctionLike(node); + // Do not use hasDynamicName here, because that returns false for well known symbols. + // We want to perform checkComputedPropertyName for all computed properties, including + // well known symbols. + if (node.name && node.name.kind === 140 /* ComputedPropertyName */) { + // This check will account for methods in class/interface declarations, + // as well as accessors in classes/object literals + checkComputedPropertyName(node.name); + } + if (!ts.hasDynamicName(node)) { + // first we want to check the local symbol that contain this declaration + // - if node.localSymbol !== undefined - this is current declaration is exported and localSymbol points to the local symbol + // - if node.localSymbol === undefined - this node is non-exported so we can just pick the result of getSymbolOfNode + var symbol = getSymbolOfNode(node); + var localSymbol = node.localSymbol || symbol; + // Since the javascript won't do semantic analysis like typescript, + // if the javascript file comes before the typescript file and both contain same name functions, + // checkFunctionOrConstructorSymbol wouldn't be called if we didnt ignore javascript function. + var firstDeclaration = ts.forEach(localSymbol.declarations, + // Get first non javascript function declaration + function (declaration) { return declaration.kind === node.kind && !ts.isSourceFileJavaScript(ts.getSourceFileOfNode(declaration)) ? + declaration : undefined; }); + // Only type check the symbol once + if (node === firstDeclaration) { + checkFunctionOrConstructorSymbol(localSymbol); + } + if (symbol.parent) { + // run check once for the first declaration + if (ts.getDeclarationOfKind(symbol, node.kind) === node) { + // run check on export symbol to check that modifiers agree across all exported declarations + checkFunctionOrConstructorSymbol(symbol); } } - checkObjectTypeForDuplicateDeclarations(node); } - ts.forEach(ts.getInterfaceBaseTypeNodes(node), function (heritageElement) { - if (!ts.isEntityNameExpression(heritageElement.expression)) { - error(heritageElement.expression, ts.Diagnostics.An_interface_can_only_extend_an_identifier_Slashqualified_name_with_optional_type_arguments); + checkSourceElement(node.body); + if (!node.asteriskToken) { + var returnOrPromisedType = node.type && (isAsync ? checkAsyncFunctionReturnType(node) : getTypeFromTypeNode(node.type)); + checkAllCodePathsInNonVoidFunctionReturnOrThrow(node, returnOrPromisedType); + } + if (produceDiagnostics && !node.type) { + // Report an implicit any error if there is no body, no explicit return type, and node is not a private method + // in an ambient context + if (compilerOptions.noImplicitAny && ts.nodeIsMissing(node.body) && !isPrivateWithinAmbient(node)) { + reportImplicitAnyError(node, anyType); + } + if (node.asteriskToken && ts.nodeIsPresent(node.body)) { + // A generator with a body and no type annotation can still cause errors. It can error if the + // yielded values have no common supertype, or it can give an implicit any error if it has no + // yielded values. The only way to trigger these errors is to try checking its return type. + getReturnTypeOfSignature(getSignatureFromDeclaration(node)); } - checkTypeReferenceNode(heritageElement); - }); - ts.forEach(node.members, checkSourceElement); - if (produceDiagnostics) { - checkTypeForDuplicateIndexSignatures(node); - registerForUnusedIdentifiersCheck(node); } + registerForUnusedIdentifiersCheck(node); } - function checkTypeAliasDeclaration(node) { - // Grammar checking - checkGrammarDecorators(node) || checkGrammarModifiers(node); - checkTypeNameIsReserved(node.name, ts.Diagnostics.Type_alias_name_cannot_be_0); - checkSourceElement(node.type); + function registerForUnusedIdentifiersCheck(node) { + if (deferredUnusedIdentifierNodes) { + deferredUnusedIdentifierNodes.push(node); + } } - function computeEnumMemberValues(node) { - var nodeLinks = getNodeLinks(node); - if (!(nodeLinks.flags & 16384 /* EnumValuesComputed */)) { - var enumSymbol = getSymbolOfNode(node); - var enumType = getDeclaredTypeOfSymbol(enumSymbol); - var autoValue = 0; // set to undefined when enum member is non-constant - var ambient = ts.isInAmbientContext(node); - var enumIsConst = ts.isConst(node); - for (var _i = 0, _a = node.members; _i < _a.length; _i++) { - var member = _a[_i]; - if (isComputedNonLiteralName(member.name)) { - error(member.name, ts.Diagnostics.Computed_property_names_are_not_allowed_in_enums); - } - else { - var text = getTextOfPropertyName(member.name); - if (isNumericLiteralName(text) && !isInfinityOrNaNString(text)) { - error(member.name, ts.Diagnostics.An_enum_member_cannot_have_a_numeric_name); - } - } - var previousEnumMemberIsNonConstant = autoValue === undefined; - var initializer = member.initializer; - if (initializer) { - autoValue = computeConstantValueForEnumMemberInitializer(initializer, enumType, enumIsConst, ambient); - } - else if (ambient && !enumIsConst) { - // In ambient enum declarations that specify no const modifier, enum member declarations - // that omit a value are considered computed members (as opposed to having auto-incremented values assigned). - autoValue = undefined; - } - else if (previousEnumMemberIsNonConstant) { - // If the member declaration specifies no value, the member is considered a constant enum member. - // If the member is the first member in the enum declaration, it is assigned the value zero. - // Otherwise, it is assigned the value of the immediately preceding member plus one, - // and an error occurs if the immediately preceding member is not a constant enum member - error(member.name, ts.Diagnostics.Enum_member_must_have_initializer); - } - if (autoValue !== undefined) { - getNodeLinks(member).enumMemberValue = autoValue; - autoValue++; + function checkUnusedIdentifiers() { + if (deferredUnusedIdentifierNodes) { + for (var _i = 0, deferredUnusedIdentifierNodes_1 = deferredUnusedIdentifierNodes; _i < deferredUnusedIdentifierNodes_1.length; _i++) { + var node = deferredUnusedIdentifierNodes_1[_i]; + switch (node.kind) { + case 256 /* SourceFile */: + case 225 /* ModuleDeclaration */: + checkUnusedModuleMembers(node); + break; + case 221 /* ClassDeclaration */: + case 192 /* ClassExpression */: + checkUnusedClassMembers(node); + checkUnusedTypeParameters(node); + break; + case 222 /* InterfaceDeclaration */: + checkUnusedTypeParameters(node); + break; + case 199 /* Block */: + case 227 /* CaseBlock */: + case 206 /* ForStatement */: + case 207 /* ForInStatement */: + case 208 /* ForOfStatement */: + checkUnusedLocalsAndParameters(node); + break; + case 148 /* Constructor */: + case 179 /* FunctionExpression */: + case 220 /* FunctionDeclaration */: + case 180 /* ArrowFunction */: + case 147 /* MethodDeclaration */: + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + if (node.body) { + checkUnusedLocalsAndParameters(node); + } + checkUnusedTypeParameters(node); + break; + case 146 /* MethodSignature */: + case 151 /* CallSignature */: + case 152 /* ConstructSignature */: + case 153 /* IndexSignature */: + case 156 /* FunctionType */: + case 157 /* ConstructorType */: + checkUnusedTypeParameters(node); + break; } + ; } - nodeLinks.flags |= 16384 /* EnumValuesComputed */; } - function computeConstantValueForEnumMemberInitializer(initializer, enumType, enumIsConst, ambient) { - // Controls if error should be reported after evaluation of constant value is completed - // Can be false if another more precise error was already reported during evaluation. - var reportError = true; - var value = evalConstant(initializer); - if (reportError) { - if (value === undefined) { - if (enumIsConst) { - error(initializer, ts.Diagnostics.In_const_enum_declarations_member_initializer_must_be_constant_expression); - } - else if (ambient) { - error(initializer, ts.Diagnostics.In_ambient_enum_declarations_member_initializer_must_be_constant_expression); - } - else { - // Only here do we need to check that the initializer is assignable to the enum type. - checkTypeAssignableTo(checkExpression(initializer), enumType, initializer, /*headMessage*/ undefined); - } - } - else if (enumIsConst) { - if (isNaN(value)) { - error(initializer, ts.Diagnostics.const_enum_member_initializer_was_evaluated_to_disallowed_value_NaN); + } + function checkUnusedLocalsAndParameters(node) { + if (node.parent.kind !== 222 /* InterfaceDeclaration */ && noUnusedIdentifiers && !ts.isInAmbientContext(node)) { + var _loop_1 = function (key) { + var local = node.locals[key]; + if (!local.isReferenced) { + if (local.valueDeclaration && local.valueDeclaration.kind === 142 /* Parameter */) { + var parameter = local.valueDeclaration; + if (compilerOptions.noUnusedParameters && + !ts.isParameterPropertyDeclaration(parameter) && + !ts.parameterIsThisKeyword(parameter) && + !parameterNameStartsWithUnderscore(parameter)) { + error(local.valueDeclaration.name, ts.Diagnostics._0_is_declared_but_never_used, local.name); + } } - else if (!isFinite(value)) { - error(initializer, ts.Diagnostics.const_enum_member_initializer_was_evaluated_to_a_non_finite_value); + else if (compilerOptions.noUnusedLocals) { + ts.forEach(local.declarations, function (d) { return error(d.name || d, ts.Diagnostics._0_is_declared_but_never_used, local.name); }); } } + }; + for (var key in node.locals) { + _loop_1(key); } - return value; - function evalConstant(e) { - switch (e.kind) { - case 185 /* PrefixUnaryExpression */: - var value_1 = evalConstant(e.operand); - if (value_1 === undefined) { - return undefined; - } - switch (e.operator) { - case 35 /* PlusToken */: return value_1; - case 36 /* MinusToken */: return -value_1; - case 50 /* TildeToken */: return ~value_1; - } - return undefined; - case 187 /* BinaryExpression */: - var left = evalConstant(e.left); - if (left === undefined) { - return undefined; - } - var right = evalConstant(e.right); - if (right === undefined) { - return undefined; - } - switch (e.operatorToken.kind) { - case 47 /* BarToken */: return left | right; - case 46 /* AmpersandToken */: return left & right; - case 44 /* GreaterThanGreaterThanToken */: return left >> right; - case 45 /* GreaterThanGreaterThanGreaterThanToken */: return left >>> right; - case 43 /* LessThanLessThanToken */: return left << right; - case 48 /* CaretToken */: return left ^ right; - case 37 /* AsteriskToken */: return left * right; - case 39 /* SlashToken */: return left / right; - case 35 /* PlusToken */: return left + right; - case 36 /* MinusToken */: return left - right; - case 40 /* PercentToken */: return left % right; - } - return undefined; - case 8 /* NumericLiteral */: - return +e.text; - case 178 /* ParenthesizedExpression */: - return evalConstant(e.expression); - case 69 /* Identifier */: - case 173 /* ElementAccessExpression */: - case 172 /* PropertyAccessExpression */: - var member = initializer.parent; - var currentType = getTypeOfSymbol(getSymbolOfNode(member.parent)); - var enumType_1; - var propertyName = void 0; - if (e.kind === 69 /* Identifier */) { - // unqualified names can refer to member that reside in different declaration of the enum so just doing name resolution won't work. - // instead pick current enum type and later try to fetch member from the type - enumType_1 = currentType; - propertyName = e.text; + } + } + function parameterNameStartsWithUnderscore(parameter) { + return parameter.name && parameter.name.kind === 69 /* Identifier */ && parameter.name.text.charCodeAt(0) === 95 /* _ */; + } + function checkUnusedClassMembers(node) { + if (compilerOptions.noUnusedLocals && !ts.isInAmbientContext(node)) { + if (node.members) { + for (var _i = 0, _a = node.members; _i < _a.length; _i++) { + var member = _a[_i]; + if (member.kind === 147 /* MethodDeclaration */ || member.kind === 145 /* PropertyDeclaration */) { + if (!member.symbol.isReferenced && ts.getModifierFlags(member) & 8 /* Private */) { + error(member.name, ts.Diagnostics._0_is_declared_but_never_used, member.symbol.name); } - else { - var expression = void 0; - if (e.kind === 173 /* ElementAccessExpression */) { - if (e.argumentExpression === undefined || - e.argumentExpression.kind !== 9 /* StringLiteral */) { - return undefined; - } - expression = e.expression; - propertyName = e.argumentExpression.text; - } - else { - expression = e.expression; - propertyName = e.name.text; - } - // expression part in ElementAccess\PropertyAccess should be either identifier or dottedName - var current = expression; - while (current) { - if (current.kind === 69 /* Identifier */) { - break; - } - else if (current.kind === 172 /* PropertyAccessExpression */) { - current = current.expression; - } - else { - return undefined; - } - } - enumType_1 = checkExpression(expression); - // allow references to constant members of other enums - if (!(enumType_1.symbol && (enumType_1.symbol.flags & 384 /* Enum */))) { - return undefined; + } + else if (member.kind === 148 /* Constructor */) { + for (var _b = 0, _c = member.parameters; _b < _c.length; _b++) { + var parameter = _c[_b]; + if (!parameter.symbol.isReferenced && ts.getModifierFlags(parameter) & 8 /* Private */) { + error(parameter.name, ts.Diagnostics.Property_0_is_declared_but_never_used, parameter.symbol.name); } } - if (propertyName === undefined) { - return undefined; - } - var property = getPropertyOfObjectType(enumType_1, propertyName); - if (!property || !(property.flags & 8 /* EnumMember */)) { - return undefined; - } - var propertyDecl = property.valueDeclaration; - // self references are illegal - if (member === propertyDecl) { - return undefined; - } - // illegal case: forward reference - if (!isBlockScopedNameDeclaredBeforeUse(propertyDecl, member)) { - reportError = false; - error(e, ts.Diagnostics.A_member_initializer_in_a_enum_declaration_cannot_reference_members_declared_after_it_including_members_defined_in_other_enums); - return undefined; - } - return getNodeLinks(propertyDecl).enumMemberValue; + } } } } } - function checkEnumDeclaration(node) { - if (!produceDiagnostics) { - return; - } - // Grammar checking - checkGrammarDecorators(node) || checkGrammarModifiers(node); - checkTypeNameIsReserved(node.name, ts.Diagnostics.Enum_name_cannot_be_0); - checkCollisionWithCapturedThisVariable(node, node.name); - checkCollisionWithRequireExportsInGeneratedCode(node, node.name); - checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); - checkExportsOnMergedDeclarations(node); - computeEnumMemberValues(node); - var enumIsConst = ts.isConst(node); - if (compilerOptions.isolatedModules && enumIsConst && ts.isInAmbientContext(node)) { - error(node.name, ts.Diagnostics.Ambient_const_enums_are_not_allowed_when_the_isolatedModules_flag_is_provided); - } - // Spec 2014 - Section 9.3: - // It isn't possible for one enum declaration to continue the automatic numbering sequence of another, - // and when an enum type has multiple declarations, only one declaration is permitted to omit a value - // for the first member. - // - // Only perform this check once per symbol - var enumSymbol = getSymbolOfNode(node); - var firstDeclaration = ts.getDeclarationOfKind(enumSymbol, node.kind); - if (node === firstDeclaration) { - if (enumSymbol.declarations.length > 1) { - // check that const is placed\omitted on all enum declarations - ts.forEach(enumSymbol.declarations, function (decl) { - if (ts.isConstEnumDeclaration(decl) !== enumIsConst) { - error(decl.name, ts.Diagnostics.Enum_declarations_must_all_be_const_or_non_const); - } - }); - } - var seenEnumMissingInitialInitializer_1 = false; - ts.forEach(enumSymbol.declarations, function (declaration) { - // return true if we hit a violation of the rule, false otherwise - if (declaration.kind !== 224 /* EnumDeclaration */) { - return false; - } - var enumDeclaration = declaration; - if (!enumDeclaration.members.length) { - return false; + function checkUnusedTypeParameters(node) { + if (compilerOptions.noUnusedLocals && !ts.isInAmbientContext(node)) { + if (node.typeParameters) { + // Only report errors on the last declaration for the type parameter container; + // this ensures that all uses have been accounted for. + var symbol = getSymbolOfNode(node); + var lastDeclaration = symbol && symbol.declarations && ts.lastOrUndefined(symbol.declarations); + if (lastDeclaration !== node) { + return; } - var firstEnumMember = enumDeclaration.members[0]; - if (!firstEnumMember.initializer) { - if (seenEnumMissingInitialInitializer_1) { - error(firstEnumMember.name, ts.Diagnostics.In_an_enum_with_multiple_declarations_only_one_declaration_can_omit_an_initializer_for_its_first_enum_element); - } - else { - seenEnumMissingInitialInitializer_1 = true; + for (var _i = 0, _a = node.typeParameters; _i < _a.length; _i++) { + var typeParameter = _a[_i]; + if (!getMergedSymbol(typeParameter.symbol).isReferenced) { + error(typeParameter.name, ts.Diagnostics._0_is_declared_but_never_used, typeParameter.symbol.name); } } - }); + } } } - function getFirstNonAmbientClassOrFunctionDeclaration(symbol) { - var declarations = symbol.declarations; - for (var _i = 0, declarations_5 = declarations; _i < declarations_5.length; _i++) { - var declaration = declarations_5[_i]; - if ((declaration.kind === 221 /* ClassDeclaration */ || - (declaration.kind === 220 /* FunctionDeclaration */ && ts.nodeIsPresent(declaration.body))) && - !ts.isInAmbientContext(declaration)) { - return declaration; + function checkUnusedModuleMembers(node) { + if (compilerOptions.noUnusedLocals && !ts.isInAmbientContext(node)) { + for (var key in node.locals) { + var local = node.locals[key]; + if (!local.isReferenced && !local.exportSymbol) { + for (var _i = 0, _a = local.declarations; _i < _a.length; _i++) { + var declaration = _a[_i]; + if (!ts.isAmbientModule(declaration)) { + error(declaration.name, ts.Diagnostics._0_is_declared_but_never_used, local.name); + } + } + } } } - return undefined; } - function inSameLexicalScope(node1, node2) { - var container1 = ts.getEnclosingBlockScopeContainer(node1); - var container2 = ts.getEnclosingBlockScopeContainer(node2); - if (isGlobalSourceFile(container1)) { - return isGlobalSourceFile(container2); - } - else if (isGlobalSourceFile(container2)) { - return false; + function checkBlock(node) { + // Grammar checking for SyntaxKind.Block + if (node.kind === 199 /* Block */) { + checkGrammarStatementInAmbientContext(node); } - else { - return container1 === container2; + ts.forEach(node.statements, checkSourceElement); + if (node.locals) { + registerForUnusedIdentifiersCheck(node); } } - function checkModuleDeclaration(node) { - if (produceDiagnostics) { - // Grammar checking - var isGlobalAugmentation = ts.isGlobalScopeAugmentation(node); - var inAmbientContext = ts.isInAmbientContext(node); - if (isGlobalAugmentation && !inAmbientContext) { - error(node.name, ts.Diagnostics.Augmentations_for_the_global_scope_should_have_declare_modifier_unless_they_appear_in_already_ambient_context); + function checkCollisionWithArgumentsInGeneratedCode(node) { + // no rest parameters \ declaration context \ overload - no codegen impact + if (!ts.hasDeclaredRestParameter(node) || ts.isInAmbientContext(node) || ts.nodeIsMissing(node.body)) { + return; + } + ts.forEach(node.parameters, function (p) { + if (p.name && !ts.isBindingPattern(p.name) && p.name.text === argumentsSymbol.name) { + error(p, ts.Diagnostics.Duplicate_identifier_arguments_Compiler_uses_arguments_to_initialize_rest_parameters); } - var isAmbientExternalModule = ts.isAmbientModule(node); - var contextErrorMessage = isAmbientExternalModule - ? ts.Diagnostics.An_ambient_module_declaration_is_only_allowed_at_the_top_level_in_a_file - : ts.Diagnostics.A_namespace_declaration_is_only_allowed_in_a_namespace_or_module; - if (checkGrammarModuleElementContext(node, contextErrorMessage)) { - // If we hit a module declaration in an illegal context, just bail out to avoid cascading errors. - return; - } - if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node)) { - if (!inAmbientContext && node.name.kind === 9 /* StringLiteral */) { - grammarErrorOnNode(node.name, ts.Diagnostics.Only_ambient_modules_can_use_quoted_names); - } - } - checkCollisionWithCapturedThisVariable(node, node.name); - checkCollisionWithRequireExportsInGeneratedCode(node, node.name); - checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); - checkExportsOnMergedDeclarations(node); - var symbol = getSymbolOfNode(node); - // The following checks only apply on a non-ambient instantiated module declaration. - if (symbol.flags & 512 /* ValueModule */ - && symbol.declarations.length > 1 - && !inAmbientContext - && ts.isInstantiatedModule(node, compilerOptions.preserveConstEnums || compilerOptions.isolatedModules)) { - var firstNonAmbientClassOrFunc = getFirstNonAmbientClassOrFunctionDeclaration(symbol); - if (firstNonAmbientClassOrFunc) { - if (ts.getSourceFileOfNode(node) !== ts.getSourceFileOfNode(firstNonAmbientClassOrFunc)) { - error(node.name, ts.Diagnostics.A_namespace_declaration_cannot_be_in_a_different_file_from_a_class_or_function_with_which_it_is_merged); - } - else if (node.pos < firstNonAmbientClassOrFunc.pos) { - error(node.name, ts.Diagnostics.A_namespace_declaration_cannot_be_located_prior_to_a_class_or_function_with_which_it_is_merged); - } - } - // if the module merges with a class declaration in the same lexical scope, - // we need to track this to ensure the correct emit. - var mergedClass = ts.getDeclarationOfKind(symbol, 221 /* ClassDeclaration */); - if (mergedClass && - inSameLexicalScope(node, mergedClass)) { - getNodeLinks(node).flags |= 32768 /* LexicalModuleMergesWithClass */; - } - } - if (isAmbientExternalModule) { - if (ts.isExternalModuleAugmentation(node)) { - // body of the augmentation should be checked for consistency only if augmentation was applied to its target (either global scope or module) - // otherwise we'll be swamped in cascading errors. - // We can detect if augmentation was applied using following rules: - // - augmentation for a global scope is always applied - // - augmentation for some external module is applied if symbol for augmentation is merged (it was combined with target module). - var checkBody = isGlobalAugmentation || (getSymbolOfNode(node).flags & 33554432 /* Merged */); - if (checkBody && node.body) { - // body of ambient external module is always a module block - for (var _i = 0, _a = node.body.statements; _i < _a.length; _i++) { - var statement = _a[_i]; - checkModuleAugmentationElement(statement, isGlobalAugmentation); - } - } - } - else if (isGlobalSourceFile(node.parent)) { - if (isGlobalAugmentation) { - error(node.name, ts.Diagnostics.Augmentations_for_the_global_scope_can_only_be_directly_nested_in_external_modules_or_ambient_module_declarations); - } - else if (ts.isExternalModuleNameRelative(node.name.text)) { - error(node.name, ts.Diagnostics.Ambient_module_declaration_cannot_specify_relative_module_name); - } - } - else { - if (isGlobalAugmentation) { - error(node.name, ts.Diagnostics.Augmentations_for_the_global_scope_can_only_be_directly_nested_in_external_modules_or_ambient_module_declarations); - } - else { - // Node is not an augmentation and is not located on the script level. - // This means that this is declaration of ambient module that is located in other module or namespace which is prohibited. - error(node.name, ts.Diagnostics.Ambient_modules_cannot_be_nested_in_other_modules_or_namespaces); - } - } - } - } - if (node.body) { - checkSourceElement(node.body); - if (!ts.isGlobalScopeAugmentation(node)) { - registerForUnusedIdentifiersCheck(node); - } - } - } - function checkModuleAugmentationElement(node, isGlobalAugmentation) { - switch (node.kind) { - case 200 /* VariableStatement */: - // error each individual name in variable statement instead of marking the entire variable statement - for (var _i = 0, _a = node.declarationList.declarations; _i < _a.length; _i++) { - var decl = _a[_i]; - checkModuleAugmentationElement(decl, isGlobalAugmentation); - } - break; - case 235 /* ExportAssignment */: - case 236 /* ExportDeclaration */: - grammarErrorOnFirstToken(node, ts.Diagnostics.Exports_and_export_assignments_are_not_permitted_in_module_augmentations); - break; - case 229 /* ImportEqualsDeclaration */: - case 230 /* ImportDeclaration */: - grammarErrorOnFirstToken(node, ts.Diagnostics.Imports_are_not_permitted_in_module_augmentations_Consider_moving_them_to_the_enclosing_external_module); - break; - case 169 /* BindingElement */: - case 218 /* VariableDeclaration */: - var name_22 = node.name; - if (ts.isBindingPattern(name_22)) { - for (var _b = 0, _c = name_22.elements; _b < _c.length; _b++) { - var el = _c[_b]; - // mark individual names in binding pattern - checkModuleAugmentationElement(el, isGlobalAugmentation); - } - break; - } - // fallthrough - case 221 /* ClassDeclaration */: - case 224 /* EnumDeclaration */: - case 220 /* FunctionDeclaration */: - case 222 /* InterfaceDeclaration */: - case 225 /* ModuleDeclaration */: - case 223 /* TypeAliasDeclaration */: - if (isGlobalAugmentation) { - return; - } - var symbol = getSymbolOfNode(node); - if (symbol) { - // module augmentations cannot introduce new names on the top level scope of the module - // this is done it two steps - // 1. quick check - if symbol for node is not merged - this is local symbol to this augmentation - report error - // 2. main check - report error if value declaration of the parent symbol is module augmentation) - var reportError = !(symbol.flags & 33554432 /* Merged */); - if (!reportError) { - // symbol should not originate in augmentation - reportError = ts.isExternalModuleAugmentation(symbol.parent.declarations[0]); - } - } - break; - } + }); } - function getFirstIdentifier(node) { - switch (node.kind) { - case 69 /* Identifier */: - return node; - case 139 /* QualifiedName */: - do { - node = node.left; - } while (node.kind !== 69 /* Identifier */); - return node; - case 172 /* PropertyAccessExpression */: - do { - node = node.expression; - } while (node.kind !== 69 /* Identifier */); - return node; + function needCollisionCheckForIdentifier(node, identifier, name) { + if (!(identifier && identifier.text === name)) { + return false; } - } - function checkExternalImportOrExportDeclaration(node) { - var moduleName = ts.getExternalModuleName(node); - if (!ts.nodeIsMissing(moduleName) && moduleName.kind !== 9 /* StringLiteral */) { - error(moduleName, ts.Diagnostics.String_literal_expected); + if (node.kind === 145 /* PropertyDeclaration */ || + node.kind === 144 /* PropertySignature */ || + node.kind === 147 /* MethodDeclaration */ || + node.kind === 146 /* MethodSignature */ || + node.kind === 149 /* GetAccessor */ || + node.kind === 150 /* SetAccessor */) { + // it is ok to have member named '_super' or '_this' - member access is always qualified return false; } - var inAmbientExternalModule = node.parent.kind === 226 /* ModuleBlock */ && ts.isAmbientModule(node.parent.parent); - if (node.parent.kind !== 256 /* SourceFile */ && !inAmbientExternalModule) { - error(moduleName, node.kind === 236 /* ExportDeclaration */ ? - ts.Diagnostics.Export_declarations_are_not_permitted_in_a_namespace : - ts.Diagnostics.Import_declarations_in_a_namespace_cannot_reference_a_module); + if (ts.isInAmbientContext(node)) { + // ambient context - no codegen impact return false; } - if (inAmbientExternalModule && ts.isExternalModuleNameRelative(moduleName.text)) { - // we have already reported errors on top level imports\exports in external module augmentations in checkModuleDeclaration - // no need to do this again. - if (!isTopLevelInExternalModuleAugmentation(node)) { - // TypeScript 1.0 spec (April 2013): 12.1.6 - // An ExternalImportDeclaration in an AmbientExternalModuleDeclaration may reference - // other external modules only through top - level external module names. - // Relative external module names are not permitted. - error(node, ts.Diagnostics.Import_or_export_declaration_in_an_ambient_module_declaration_cannot_reference_module_through_relative_module_name); - return false; - } + var root = ts.getRootDeclaration(node); + if (root.kind === 142 /* Parameter */ && ts.nodeIsMissing(root.parent.body)) { + // just an overload - no codegen impact + return false; } return true; } - function checkAliasSymbol(node) { - var symbol = getSymbolOfNode(node); - var target = resolveAlias(symbol); - if (target !== unknownSymbol) { - // For external modules symbol represent local symbol for an alias. - // This local symbol will merge any other local declarations (excluding other aliases) - // and symbol.flags will contains combined representation for all merged declaration. - // Based on symbol.flags we can compute a set of excluded meanings (meaning that resolved alias should not have, - // otherwise it will conflict with some local declaration). Note that in addition to normal flags we include matching SymbolFlags.Export* - // in order to prevent collisions with declarations that were exported from the current module (they still contribute to local names). - var excludedMeanings = (symbol.flags & (107455 /* Value */ | 1048576 /* ExportValue */) ? 107455 /* Value */ : 0) | - (symbol.flags & 793064 /* Type */ ? 793064 /* Type */ : 0) | - (symbol.flags & 1920 /* Namespace */ ? 1920 /* Namespace */ : 0); - if (target.flags & excludedMeanings) { - var message = node.kind === 238 /* ExportSpecifier */ ? - ts.Diagnostics.Export_declaration_conflicts_with_exported_declaration_of_0 : - ts.Diagnostics.Import_declaration_conflicts_with_local_declaration_of_0; - error(node, message, symbolToString(symbol)); - } + function checkCollisionWithCapturedThisVariable(node, name) { + if (needCollisionCheckForIdentifier(node, name, "_this")) { + potentialThisCollisions.push(node); } } - function checkImportBinding(node) { - checkCollisionWithCapturedThisVariable(node, node.name); - checkCollisionWithRequireExportsInGeneratedCode(node, node.name); - checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); - checkAliasSymbol(node); - } - function checkImportDeclaration(node) { - if (checkGrammarModuleElementContext(node, ts.Diagnostics.An_import_declaration_can_only_be_used_in_a_namespace_or_module)) { - // If we hit an import declaration in an illegal context, just bail out to avoid cascading errors. - return; - } - if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node) && ts.getModifierFlags(node) !== 0) { - grammarErrorOnFirstToken(node, ts.Diagnostics.An_import_declaration_cannot_have_modifiers); - } - if (checkExternalImportOrExportDeclaration(node)) { - var importClause = node.importClause; - if (importClause) { - if (importClause.name) { - checkImportBinding(importClause); + // this function will run after checking the source file so 'CaptureThis' is correct for all nodes + function checkIfThisIsCapturedInEnclosingScope(node) { + var current = node; + while (current) { + if (getNodeCheckFlags(current) & 4 /* CaptureThis */) { + var isDeclaration_1 = node.kind !== 69 /* Identifier */; + if (isDeclaration_1) { + error(node.name, ts.Diagnostics.Duplicate_identifier_this_Compiler_uses_variable_declaration_this_to_capture_this_reference); } - if (importClause.namedBindings) { - if (importClause.namedBindings.kind === 232 /* NamespaceImport */) { - checkImportBinding(importClause.namedBindings); - } - else { - ts.forEach(importClause.namedBindings.elements, checkImportBinding); - } + else { + error(node, ts.Diagnostics.Expression_resolves_to_variable_declaration_this_that_compiler_uses_to_capture_this_reference); } + return; } + current = current.parent; } } - function checkImportEqualsDeclaration(node) { - if (checkGrammarModuleElementContext(node, ts.Diagnostics.An_import_declaration_can_only_be_used_in_a_namespace_or_module)) { - // If we hit an import declaration in an illegal context, just bail out to avoid cascading errors. + function checkCollisionWithCapturedSuperVariable(node, name) { + if (!needCollisionCheckForIdentifier(node, name, "_super")) { return; } - checkGrammarDecorators(node) || checkGrammarModifiers(node); - if (ts.isInternalModuleImportEqualsDeclaration(node) || checkExternalImportOrExportDeclaration(node)) { - checkImportBinding(node); - if (ts.getModifierFlags(node) & 1 /* Export */) { - markExportAsReferenced(node); - } - if (ts.isInternalModuleImportEqualsDeclaration(node)) { - var target = resolveAlias(getSymbolOfNode(node)); - if (target !== unknownSymbol) { - if (target.flags & 107455 /* Value */) { - // Target is a value symbol, check that it is not hidden by a local declaration with the same name - var moduleName = getFirstIdentifier(node.moduleReference); - if (!(resolveEntityName(moduleName, 107455 /* Value */ | 1920 /* Namespace */).flags & 1920 /* Namespace */)) { - error(moduleName, ts.Diagnostics.Module_0_is_hidden_by_a_local_declaration_with_the_same_name, ts.declarationNameToString(moduleName)); - } - } - if (target.flags & 793064 /* Type */) { - checkTypeNameIsReserved(node.name, ts.Diagnostics.Import_name_cannot_be_0); - } - } + // bubble up and find containing type + var enclosingClass = ts.getContainingClass(node); + // if containing type was not found or it is ambient - exit (no codegen) + if (!enclosingClass || ts.isInAmbientContext(enclosingClass)) { + return; + } + if (ts.getClassExtendsHeritageClauseElement(enclosingClass)) { + var isDeclaration_2 = node.kind !== 69 /* Identifier */; + if (isDeclaration_2) { + error(node, ts.Diagnostics.Duplicate_identifier_super_Compiler_uses_super_to_capture_base_class_reference); } else { - if (modulekind === ts.ModuleKind.ES6 && !ts.isInAmbientContext(node)) { - // Import equals declaration is deprecated in es6 or above - grammarErrorOnNode(node, ts.Diagnostics.Import_assignment_cannot_be_used_when_targeting_ECMAScript_2015_modules_Consider_using_import_Asterisk_as_ns_from_mod_import_a_from_mod_import_d_from_mod_or_another_module_format_instead); - } + error(node, ts.Diagnostics.Expression_resolves_to_super_that_compiler_uses_to_capture_base_class_reference); } } } - function checkExportDeclaration(node) { - if (checkGrammarModuleElementContext(node, ts.Diagnostics.An_export_declaration_can_only_be_used_in_a_module)) { - // If we hit an export in an illegal context, just bail out to avoid cascading errors. + function checkCollisionWithRequireExportsInGeneratedCode(node, name) { + // No need to check for require or exports for ES6 modules and later + if (modulekind >= ts.ModuleKind.ES6) { return; } - if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node) && ts.getModifierFlags(node) !== 0) { - grammarErrorOnFirstToken(node, ts.Diagnostics.An_export_declaration_cannot_have_modifiers); - } - if (!node.moduleSpecifier || checkExternalImportOrExportDeclaration(node)) { - if (node.exportClause) { - // export { x, y } - // export { x, y } from "foo" - ts.forEach(node.exportClause.elements, checkExportSpecifier); - var inAmbientExternalModule = node.parent.kind === 226 /* ModuleBlock */ && ts.isAmbientModule(node.parent.parent); - if (node.parent.kind !== 256 /* SourceFile */ && !inAmbientExternalModule) { - error(node, ts.Diagnostics.Export_declarations_are_not_permitted_in_a_namespace); - } - } - else { - // export * from "foo" - var moduleSymbol = resolveExternalModuleName(node, node.moduleSpecifier); - if (moduleSymbol && hasExportAssignmentSymbol(moduleSymbol)) { - error(node.moduleSpecifier, ts.Diagnostics.Module_0_uses_export_and_cannot_be_used_with_export_Asterisk, symbolToString(moduleSymbol)); - } - } + if (!needCollisionCheckForIdentifier(node, name, "require") && !needCollisionCheckForIdentifier(node, name, "exports")) { + return; } - } - function checkGrammarModuleElementContext(node, errorMessage) { - if (node.parent.kind !== 256 /* SourceFile */ && node.parent.kind !== 226 /* ModuleBlock */ && node.parent.kind !== 225 /* ModuleDeclaration */) { - return grammarErrorOnFirstToken(node, errorMessage); + // Uninstantiated modules shouldnt do this check + if (node.kind === 225 /* ModuleDeclaration */ && ts.getModuleInstanceState(node) !== 1 /* Instantiated */) { + return; } - } - function checkExportSpecifier(node) { - checkAliasSymbol(node); - if (!node.parent.parent.moduleSpecifier) { - var exportedName = node.propertyName || node.name; - // find immediate value referenced by exported name (SymbolFlags.Alias is set so we don't chase down aliases) - var symbol = resolveName(exportedName, exportedName.text, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */ | 8388608 /* Alias */, - /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined); - if (symbol && (symbol === undefinedSymbol || isGlobalSourceFile(getDeclarationContainer(symbol.declarations[0])))) { - error(exportedName, ts.Diagnostics.Cannot_export_0_Only_local_declarations_can_be_exported_from_a_module, exportedName.text); - } - else { - markExportAsReferenced(node); - } + // In case of variable declaration, node.parent is variable statement so look at the variable statement's parent + var parent = getDeclarationContainer(node); + if (parent.kind === 256 /* SourceFile */ && ts.isExternalOrCommonJsModule(parent)) { + // If the declaration happens to be in external module, report error that require and exports are reserved keywords + error(name, ts.Diagnostics.Duplicate_identifier_0_Compiler_reserves_name_1_in_top_level_scope_of_a_module, ts.declarationNameToString(name), ts.declarationNameToString(name)); } } - function checkExportAssignment(node) { - if (checkGrammarModuleElementContext(node, ts.Diagnostics.An_export_assignment_can_only_be_used_in_a_module)) { - // If we hit an export assignment in an illegal context, just bail out to avoid cascading errors. + function checkCollisionWithGlobalPromiseInGeneratedCode(node, name) { + if (!needCollisionCheckForIdentifier(node, name, "Promise")) { return; } - var container = node.parent.kind === 256 /* SourceFile */ ? node.parent : node.parent.parent; - if (container.kind === 225 /* ModuleDeclaration */ && !ts.isAmbientModule(container)) { - error(node, ts.Diagnostics.An_export_assignment_cannot_be_used_in_a_namespace); + // Uninstantiated modules shouldnt do this check + if (node.kind === 225 /* ModuleDeclaration */ && ts.getModuleInstanceState(node) !== 1 /* Instantiated */) { return; } - // Grammar checking - if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node) && ts.getModifierFlags(node) !== 0) { - grammarErrorOnFirstToken(node, ts.Diagnostics.An_export_assignment_cannot_have_modifiers); + // In case of variable declaration, node.parent is variable statement so look at the variable statement's parent + var parent = getDeclarationContainer(node); + if (parent.kind === 256 /* SourceFile */ && ts.isExternalOrCommonJsModule(parent) && parent.flags & 8192 /* HasAsyncFunctions */) { + // If the declaration happens to be in external module, report error that Promise is a reserved identifier. + error(name, ts.Diagnostics.Duplicate_identifier_0_Compiler_reserves_name_1_in_top_level_scope_of_a_module_containing_async_functions, ts.declarationNameToString(name), ts.declarationNameToString(name)); } - if (node.expression.kind === 69 /* Identifier */) { - markExportAsReferenced(node); + } + function checkVarDeclaredNamesNotShadowed(node) { + // - ScriptBody : StatementList + // It is a Syntax Error if any element of the LexicallyDeclaredNames of StatementList + // also occurs in the VarDeclaredNames of StatementList. + // - Block : { StatementList } + // It is a Syntax Error if any element of the LexicallyDeclaredNames of StatementList + // also occurs in the VarDeclaredNames of StatementList. + // Variable declarations are hoisted to the top of their function scope. They can shadow + // block scoped declarations, which bind tighter. this will not be flagged as duplicate definition + // by the binder as the declaration scope is different. + // A non-initialized declaration is a no-op as the block declaration will resolve before the var + // declaration. the problem is if the declaration has an initializer. this will act as a write to the + // block declared value. this is fine for let, but not const. + // Only consider declarations with initializers, uninitialized const declarations will not + // step on a let/const variable. + // Do not consider const and const declarations, as duplicate block-scoped declarations + // are handled by the binder. + // We are only looking for const declarations that step on let\const declarations from a + // different scope. e.g.: + // { + // const x = 0; // localDeclarationSymbol obtained after name resolution will correspond to this declaration + // const x = 0; // symbol for this declaration will be 'symbol' + // } + // skip block-scoped variables and parameters + if ((ts.getCombinedNodeFlags(node) & 3 /* BlockScoped */) !== 0 || ts.isParameterDeclaration(node)) { + return; } - else { - checkExpressionCached(node.expression); + // skip variable declarations that don't have initializers + // NOTE: in ES6 spec initializer is required in variable declarations where name is binding pattern + // so we'll always treat binding elements as initialized + if (node.kind === 218 /* VariableDeclaration */ && !node.initializer) { + return; } - checkExternalModuleExports(container); - if (node.isExportEquals && !ts.isInAmbientContext(node)) { - if (modulekind === ts.ModuleKind.ES6) { - // export assignment is not supported in es6 modules - grammarErrorOnNode(node, ts.Diagnostics.Export_assignment_cannot_be_used_when_targeting_ECMAScript_2015_modules_Consider_using_export_default_or_another_module_format_instead); - } - else if (modulekind === ts.ModuleKind.System) { - // system modules does not support export assignment - grammarErrorOnNode(node, ts.Diagnostics.Export_assignment_is_not_supported_when_module_flag_is_system); + var symbol = getSymbolOfNode(node); + if (symbol.flags & 1 /* FunctionScopedVariable */) { + var localDeclarationSymbol = resolveName(node, node.name.text, 3 /* Variable */, /*nodeNotFoundErrorMessage*/ undefined, /*nameArg*/ undefined); + if (localDeclarationSymbol && + localDeclarationSymbol !== symbol && + localDeclarationSymbol.flags & 2 /* BlockScopedVariable */) { + if (getDeclarationNodeFlagsFromSymbol(localDeclarationSymbol) & 3 /* BlockScoped */) { + var varDeclList = ts.getAncestor(localDeclarationSymbol.valueDeclaration, 219 /* VariableDeclarationList */); + var container = varDeclList.parent.kind === 200 /* VariableStatement */ && varDeclList.parent.parent + ? varDeclList.parent.parent + : undefined; + // names of block-scoped and function scoped variables can collide only + // if block scoped variable is defined in the function\module\source file scope (because of variable hoisting) + var namesShareScope = container && + (container.kind === 199 /* Block */ && ts.isFunctionLike(container.parent) || + container.kind === 226 /* ModuleBlock */ || + container.kind === 225 /* ModuleDeclaration */ || + container.kind === 256 /* SourceFile */); + // here we know that function scoped variable is shadowed by block scoped one + // if they are defined in the same scope - binder has already reported redeclaration error + // otherwise if variable has an initializer - show error that initialization will fail + // since LHS will be block scoped name instead of function scoped + if (!namesShareScope) { + var name_20 = symbolToString(localDeclarationSymbol); + error(node, ts.Diagnostics.Cannot_initialize_outer_scoped_variable_0_in_the_same_scope_as_block_scoped_declaration_1, name_20, name_20); + } + } } } } - function hasExportedMembers(moduleSymbol) { - for (var id in moduleSymbol.exports) { - if (id !== "export=") { - return true; - } + // Check that a parameter initializer contains no references to parameters declared to the right of itself + function checkParameterInitializer(node) { + if (ts.getRootDeclaration(node).kind !== 142 /* Parameter */) { + return; } - return false; - } - function checkExternalModuleExports(node) { - var moduleSymbol = getSymbolOfNode(node); - var links = getSymbolLinks(moduleSymbol); - if (!links.exportsChecked) { - var exportEqualsSymbol = moduleSymbol.exports["export="]; - if (exportEqualsSymbol && hasExportedMembers(moduleSymbol)) { - var declaration = getDeclarationOfAliasSymbol(exportEqualsSymbol) || exportEqualsSymbol.valueDeclaration; - if (!isTopLevelInExternalModuleAugmentation(declaration)) { - error(declaration, ts.Diagnostics.An_export_assignment_cannot_be_used_in_a_module_with_other_exported_elements); - } + var func = ts.getContainingFunction(node); + visit(node.initializer); + function visit(n) { + if (ts.isTypeNode(n) || ts.isDeclarationName(n)) { + // do not dive in types + // skip declaration names (i.e. in object literal expressions) + return; } - // Checks for export * conflicts - var exports = getExportsOfModule(moduleSymbol); - for (var id in exports) { - if (id === "__export") { - continue; - } - var _a = exports[id], declarations = _a.declarations, flags = _a.flags; - // ECMA262: 15.2.1.1 It is a Syntax Error if the ExportedNames of ModuleItemList contains any duplicate entries. - // (TS Exceptions: namespaces, function overloads, enums, and interfaces) - if (flags & (1920 /* Namespace */ | 64 /* Interface */ | 384 /* Enum */)) { - continue; + if (n.kind === 172 /* PropertyAccessExpression */) { + // skip property names in property access expression + return visit(n.expression); + } + else if (n.kind === 69 /* Identifier */) { + // check FunctionLikeDeclaration.locals (stores parameters\function local variable) + // if it contains entry with a specified name + var symbol = resolveName(n, n.text, 107455 /* Value */ | 8388608 /* Alias */, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined); + if (!symbol || symbol === unknownSymbol || !symbol.valueDeclaration) { + return; } - var exportedDeclarationsCount = ts.countWhere(declarations, isNotOverload); - if (flags & 524288 /* TypeAlias */ && exportedDeclarationsCount <= 2) { - // it is legal to merge type alias with other values - // so count should be either 1 (just type alias) or 2 (type alias + merged value) - continue; + if (symbol.valueDeclaration === node) { + error(n, ts.Diagnostics.Parameter_0_cannot_be_referenced_in_its_initializer, ts.declarationNameToString(node.name)); + return; } - if (exportedDeclarationsCount > 1) { - for (var _i = 0, declarations_6 = declarations; _i < declarations_6.length; _i++) { - var declaration = declarations_6[_i]; - if (isNotOverload(declaration)) { - diagnostics.add(ts.createDiagnosticForNode(declaration, ts.Diagnostics.Cannot_redeclare_exported_variable_0, id)); + // locals map for function contain both parameters and function locals + // so we need to do a bit of extra work to check if reference is legal + var enclosingContainer = ts.getEnclosingBlockScopeContainer(symbol.valueDeclaration); + if (enclosingContainer === func) { + if (symbol.valueDeclaration.kind === 142 /* Parameter */) { + // it is ok to reference parameter in initializer if either + // - parameter is located strictly on the left of current parameter declaration + if (symbol.valueDeclaration.pos < node.pos) { + return; + } + // - parameter is wrapped in function-like entity + var current = n; + while (current !== node.initializer) { + if (ts.isFunctionLike(current.parent)) { + return; + } + // computed property names/initializers in instance property declaration of class like entities + // are executed in constructor and thus deferred + if (current.parent.kind === 145 /* PropertyDeclaration */ && + !(ts.hasModifier(current.parent, 32 /* Static */)) && + ts.isClassLike(current.parent.parent)) { + return; + } + current = current.parent; } } + error(n, ts.Diagnostics.Initializer_of_parameter_0_cannot_reference_identifier_1_declared_after_it, ts.declarationNameToString(node.name), ts.declarationNameToString(n)); } } - links.exportsChecked = true; - } - function isNotOverload(declaration) { - return declaration.kind !== 220 /* FunctionDeclaration */ || !!declaration.body; + else { + return ts.forEachChild(n, visit); + } } } - function checkSourceElement(node) { - if (!node) { - return; + function convertAutoToAny(type) { + return type === autoType ? anyType : type; + } + // Check variable, parameter, or property declaration + function checkVariableLikeDeclaration(node) { + checkDecorators(node); + checkSourceElement(node.type); + // For a computed property, just check the initializer and exit + // Do not use hasDynamicName here, because that returns false for well known symbols. + // We want to perform checkComputedPropertyName for all computed properties, including + // well known symbols. + if (node.name.kind === 140 /* ComputedPropertyName */) { + checkComputedPropertyName(node.name); + if (node.initializer) { + checkExpressionCached(node.initializer); + } } - var kind = node.kind; - if (cancellationToken) { - // Only bother checking on a few construct kinds. We don't want to be excessively - // hitting the cancellation token on every node we check. - switch (kind) { - case 225 /* ModuleDeclaration */: - case 221 /* ClassDeclaration */: - case 222 /* InterfaceDeclaration */: - case 220 /* FunctionDeclaration */: - cancellationToken.throwIfCancellationRequested(); + if (node.kind === 169 /* BindingElement */) { + // check computed properties inside property names of binding elements + if (node.propertyName && node.propertyName.kind === 140 /* ComputedPropertyName */) { + checkComputedPropertyName(node.propertyName); + } + // check private/protected variable access + var parent_13 = node.parent.parent; + var parentType = getTypeForBindingElementParent(parent_13); + var name_21 = node.propertyName || node.name; + var property = getPropertyOfType(parentType, getTextOfPropertyName(name_21)); + if (parent_13.initializer && property && getParentOfSymbol(property)) { + checkClassPropertyAccess(parent_13, parent_13.initializer, parentType, property); } } - switch (kind) { - case 141 /* TypeParameter */: - return checkTypeParameter(node); - case 142 /* Parameter */: - return checkParameter(node); - case 145 /* PropertyDeclaration */: - case 144 /* PropertySignature */: - return checkPropertyDeclaration(node); - case 156 /* FunctionType */: - case 157 /* ConstructorType */: - case 151 /* CallSignature */: - case 152 /* ConstructSignature */: - return checkSignatureDeclaration(node); - case 153 /* IndexSignature */: - return checkSignatureDeclaration(node); - case 147 /* MethodDeclaration */: - case 146 /* MethodSignature */: - return checkMethodDeclaration(node); - case 148 /* Constructor */: - return checkConstructorDeclaration(node); - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - return checkAccessorDeclaration(node); - case 155 /* TypeReference */: - return checkTypeReferenceNode(node); - case 154 /* TypePredicate */: - return checkTypePredicate(node); - case 158 /* TypeQuery */: - return checkTypeQuery(node); - case 159 /* TypeLiteral */: - return checkTypeLiteral(node); - case 160 /* ArrayType */: - return checkArrayType(node); - case 161 /* TupleType */: - return checkTupleType(node); - case 162 /* UnionType */: - case 163 /* IntersectionType */: - return checkUnionOrIntersectionType(node); - case 164 /* ParenthesizedType */: - return checkSourceElement(node.type); - case 220 /* FunctionDeclaration */: - return checkFunctionDeclaration(node); - case 199 /* Block */: - case 226 /* ModuleBlock */: - return checkBlock(node); - case 200 /* VariableStatement */: - return checkVariableStatement(node); - case 202 /* ExpressionStatement */: - return checkExpressionStatement(node); - case 203 /* IfStatement */: - return checkIfStatement(node); - case 204 /* DoStatement */: - return checkDoStatement(node); - case 205 /* WhileStatement */: - return checkWhileStatement(node); - case 206 /* ForStatement */: - return checkForStatement(node); - case 207 /* ForInStatement */: - return checkForInStatement(node); - case 208 /* ForOfStatement */: - return checkForOfStatement(node); - case 209 /* ContinueStatement */: - case 210 /* BreakStatement */: - return checkBreakOrContinueStatement(node); - case 211 /* ReturnStatement */: - return checkReturnStatement(node); - case 212 /* WithStatement */: - return checkWithStatement(node); - case 213 /* SwitchStatement */: - return checkSwitchStatement(node); - case 214 /* LabeledStatement */: - return checkLabeledStatement(node); - case 215 /* ThrowStatement */: - return checkThrowStatement(node); - case 216 /* TryStatement */: - return checkTryStatement(node); - case 218 /* VariableDeclaration */: - return checkVariableDeclaration(node); - case 169 /* BindingElement */: - return checkBindingElement(node); - case 221 /* ClassDeclaration */: - return checkClassDeclaration(node); - case 222 /* InterfaceDeclaration */: - return checkInterfaceDeclaration(node); - case 223 /* TypeAliasDeclaration */: - return checkTypeAliasDeclaration(node); - case 224 /* EnumDeclaration */: - return checkEnumDeclaration(node); - case 225 /* ModuleDeclaration */: - return checkModuleDeclaration(node); - case 230 /* ImportDeclaration */: - return checkImportDeclaration(node); - case 229 /* ImportEqualsDeclaration */: - return checkImportEqualsDeclaration(node); - case 236 /* ExportDeclaration */: - return checkExportDeclaration(node); - case 235 /* ExportAssignment */: - return checkExportAssignment(node); - case 201 /* EmptyStatement */: - checkGrammarStatementInAmbientContext(node); - return; - case 217 /* DebuggerStatement */: - checkGrammarStatementInAmbientContext(node); - return; - case 239 /* MissingDeclaration */: - return checkMissingDeclaration(node); + // For a binding pattern, check contained binding elements + if (ts.isBindingPattern(node.name)) { + ts.forEach(node.name.elements, checkSourceElement); } - } - // Function and class expression bodies are checked after all statements in the enclosing body. This is - // to ensure constructs like the following are permitted: - // const foo = function () { - // const s = foo(); - // return "hello"; - // } - // Here, performing a full type check of the body of the function expression whilst in the process of - // determining the type of foo would cause foo to be given type any because of the recursive reference. - // Delaying the type check of the body ensures foo has been assigned a type. - function checkNodeDeferred(node) { - if (deferredNodes) { - deferredNodes.push(node); + // For a parameter declaration with an initializer, error and exit if the containing function doesn't have a body + if (node.initializer && ts.getRootDeclaration(node).kind === 142 /* Parameter */ && ts.nodeIsMissing(ts.getContainingFunction(node).body)) { + error(node, ts.Diagnostics.A_parameter_initializer_is_only_allowed_in_a_function_or_constructor_implementation); + return; } - } - function checkDeferredNodes() { - for (var _i = 0, deferredNodes_1 = deferredNodes; _i < deferredNodes_1.length; _i++) { - var node = deferredNodes_1[_i]; - switch (node.kind) { - case 179 /* FunctionExpression */: - case 180 /* ArrowFunction */: - case 147 /* MethodDeclaration */: - case 146 /* MethodSignature */: - checkFunctionExpressionOrObjectLiteralMethodDeferred(node); - break; - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - checkAccessorDeferred(node); - break; - case 192 /* ClassExpression */: - checkClassExpressionDeferred(node); - break; + // For a binding pattern, validate the initializer and exit + if (ts.isBindingPattern(node.name)) { + // Don't validate for-in initializer as it is already an error + if (node.initializer && node.parent.parent.kind !== 207 /* ForInStatement */) { + checkTypeAssignableTo(checkExpressionCached(node.initializer), getWidenedTypeForVariableLikeDeclaration(node), node, /*headMessage*/ undefined); + checkParameterInitializer(node); } + return; } - } - function checkSourceFile(node) { - ts.performance.mark("beforeCheck"); - checkSourceFileWorker(node); - ts.performance.mark("afterCheck"); - ts.performance.measure("Check", "beforeCheck", "afterCheck"); - } - // Fully type check a source file and collect the relevant diagnostics. - function checkSourceFileWorker(node) { - var links = getNodeLinks(node); - if (!(links.flags & 1 /* TypeChecked */)) { - // If skipLibCheck is enabled, skip type checking if file is a declaration file. - // If skipDefaultLibCheck is enabled, skip type checking if file contains a - // '/// ' directive. - if (compilerOptions.skipLibCheck && node.isDeclarationFile || compilerOptions.skipDefaultLibCheck && node.hasNoDefaultLib) { - return; + var symbol = getSymbolOfNode(node); + var type = convertAutoToAny(getTypeOfVariableOrParameterOrProperty(symbol)); + if (node === symbol.valueDeclaration) { + // Node is the primary declaration of the symbol, just validate the initializer + // Don't validate for-in initializer as it is already an error + if (node.initializer && node.parent.parent.kind !== 207 /* ForInStatement */) { + checkTypeAssignableTo(checkExpressionCached(node.initializer), type, node, /*headMessage*/ undefined); + checkParameterInitializer(node); } - // Grammar checking - checkGrammarSourceFile(node); - potentialThisCollisions.length = 0; - deferredNodes = []; - deferredUnusedIdentifierNodes = produceDiagnostics && noUnusedIdentifiers ? [] : undefined; - ts.forEach(node.statements, checkSourceElement); - checkDeferredNodes(); - if (ts.isExternalModule(node)) { - registerForUnusedIdentifiersCheck(node); + } + else { + // Node is a secondary declaration, check that type is identical to primary declaration and check that + // initializer is consistent with type associated with the node + var declarationType = convertAutoToAny(getWidenedTypeForVariableLikeDeclaration(node)); + if (type !== unknownType && declarationType !== unknownType && !isTypeIdenticalTo(type, declarationType)) { + error(node.name, ts.Diagnostics.Subsequent_variable_declarations_must_have_the_same_type_Variable_0_must_be_of_type_1_but_here_has_type_2, ts.declarationNameToString(node.name), typeToString(type), typeToString(declarationType)); } - if (!node.isDeclarationFile) { - checkUnusedIdentifiers(); + if (node.initializer) { + checkTypeAssignableTo(checkExpressionCached(node.initializer), declarationType, node, /*headMessage*/ undefined); } - deferredNodes = undefined; - deferredUnusedIdentifierNodes = undefined; - if (ts.isExternalOrCommonJsModule(node)) { - checkExternalModuleExports(node); + if (!areDeclarationFlagsIdentical(node, symbol.valueDeclaration)) { + error(symbol.valueDeclaration.name, ts.Diagnostics.All_declarations_of_0_must_have_identical_modifiers, ts.declarationNameToString(node.name)); + error(node.name, ts.Diagnostics.All_declarations_of_0_must_have_identical_modifiers, ts.declarationNameToString(node.name)); } - if (potentialThisCollisions.length) { - ts.forEach(potentialThisCollisions, checkIfThisIsCapturedInEnclosingScope); - potentialThisCollisions.length = 0; + } + if (node.kind !== 145 /* PropertyDeclaration */ && node.kind !== 144 /* PropertySignature */) { + // We know we don't have a binding pattern or computed name here + checkExportsOnMergedDeclarations(node); + if (node.kind === 218 /* VariableDeclaration */ || node.kind === 169 /* BindingElement */) { + checkVarDeclaredNamesNotShadowed(node); } - links.flags |= 1 /* TypeChecked */; + checkCollisionWithCapturedSuperVariable(node, node.name); + checkCollisionWithCapturedThisVariable(node, node.name); + checkCollisionWithRequireExportsInGeneratedCode(node, node.name); + checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); } } - function getDiagnostics(sourceFile, ct) { - try { - // Record the cancellation token so it can be checked later on during checkSourceElement. - // Do this in a finally block so we can ensure that it gets reset back to nothing after - // this call is done. - cancellationToken = ct; - return getDiagnosticsWorker(sourceFile); + function areDeclarationFlagsIdentical(left, right) { + if ((left.kind === 142 /* Parameter */ && right.kind === 218 /* VariableDeclaration */) || + (left.kind === 218 /* VariableDeclaration */ && right.kind === 142 /* Parameter */)) { + // Differences in optionality between parameters and variables are allowed. + return true; } - finally { - cancellationToken = undefined; + if (ts.hasQuestionToken(left) !== ts.hasQuestionToken(right)) { + return false; } + var interestingFlags = 8 /* Private */ | + 16 /* Protected */ | + 256 /* Async */ | + 128 /* Abstract */ | + 64 /* Readonly */ | + 32 /* Static */; + return (ts.getModifierFlags(left) & interestingFlags) === (ts.getModifierFlags(right) & interestingFlags); } - function getDiagnosticsWorker(sourceFile) { - throwIfNonDiagnosticsProducing(); - if (sourceFile) { - checkSourceFile(sourceFile); - return diagnostics.getDiagnostics(sourceFile.fileName); - } - ts.forEach(host.getSourceFiles(), checkSourceFile); - return diagnostics.getDiagnostics(); + function checkVariableDeclaration(node) { + checkGrammarVariableDeclaration(node); + return checkVariableLikeDeclaration(node); } - function getGlobalDiagnostics() { - throwIfNonDiagnosticsProducing(); - return diagnostics.getGlobalDiagnostics(); + function checkBindingElement(node) { + checkGrammarBindingElement(node); + return checkVariableLikeDeclaration(node); } - function throwIfNonDiagnosticsProducing() { - if (!produceDiagnostics) { - throw new Error("Trying to get diagnostics from a type checker that does not produce them."); - } + function checkVariableStatement(node) { + // Grammar checking + checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarVariableDeclarationList(node.declarationList) || checkGrammarForDisallowedLetOrConstStatement(node); + ts.forEach(node.declarationList.declarations, checkSourceElement); } - // Language service support - function isInsideWithStatementBody(node) { - if (node) { - while (node.parent) { - if (node.parent.kind === 212 /* WithStatement */ && node.parent.statement === node) { - return true; + function checkGrammarDisallowedModifiersOnObjectLiteralExpressionMethod(node) { + // We only disallow modifier on a method declaration if it is a property of object-literal-expression + if (node.modifiers && node.parent.kind === 171 /* ObjectLiteralExpression */) { + if (ts.isAsyncFunctionLike(node)) { + if (node.modifiers.length > 1) { + return grammarErrorOnFirstToken(node, ts.Diagnostics.Modifiers_cannot_appear_here); } - node = node.parent; + } + else { + return grammarErrorOnFirstToken(node, ts.Diagnostics.Modifiers_cannot_appear_here); } } - return false; } - function getSymbolsInScope(location, meaning) { - var symbols = ts.createMap(); - var memberFlags = 0 /* None */; - if (isInsideWithStatementBody(location)) { - // We cannot answer semantic questions within a with block, do not proceed any further - return []; + function checkExpressionStatement(node) { + // Grammar checking + checkGrammarStatementInAmbientContext(node); + checkExpression(node.expression); + } + function checkIfStatement(node) { + // Grammar checking + checkGrammarStatementInAmbientContext(node); + checkExpression(node.expression); + checkSourceElement(node.thenStatement); + if (node.thenStatement.kind === 201 /* EmptyStatement */) { + error(node.thenStatement, ts.Diagnostics.The_body_of_an_if_statement_cannot_be_the_empty_statement); } - populateSymbols(); - return symbolsToArray(symbols); - function populateSymbols() { - while (location) { - if (location.locals && !isGlobalSourceFile(location)) { - copySymbols(location.locals, meaning); - } - switch (location.kind) { - case 256 /* SourceFile */: - if (!ts.isExternalOrCommonJsModule(location)) { - break; - } - case 225 /* ModuleDeclaration */: - copySymbols(getSymbolOfNode(location).exports, meaning & 8914931 /* ModuleMember */); - break; - case 224 /* EnumDeclaration */: - copySymbols(getSymbolOfNode(location).exports, meaning & 8 /* EnumMember */); - break; - case 192 /* ClassExpression */: - var className = location.name; - if (className) { - copySymbol(location.symbol, meaning); - } - // fall through; this fall-through is necessary because we would like to handle - // type parameter inside class expression similar to how we handle it in classDeclaration and interface Declaration - case 221 /* ClassDeclaration */: - case 222 /* InterfaceDeclaration */: - // If we didn't come from static member of class or interface, - // add the type parameters into the symbol table - // (type parameters of classDeclaration/classExpression and interface are in member property of the symbol. - // Note: that the memberFlags come from previous iteration. - if (!(memberFlags & 32 /* Static */)) { - copySymbols(getSymbolOfNode(location).members, meaning & 793064 /* Type */); - } - break; - case 179 /* FunctionExpression */: - var funcName = location.name; - if (funcName) { - copySymbol(location.symbol, meaning); - } - break; - } - if (ts.introducesArgumentsExoticObject(location)) { - copySymbol(argumentsSymbol, meaning); - } - memberFlags = ts.getModifierFlags(location); - location = location.parent; + checkSourceElement(node.elseStatement); + } + function checkDoStatement(node) { + // Grammar checking + checkGrammarStatementInAmbientContext(node); + checkSourceElement(node.statement); + checkExpression(node.expression); + } + function checkWhileStatement(node) { + // Grammar checking + checkGrammarStatementInAmbientContext(node); + checkExpression(node.expression); + checkSourceElement(node.statement); + } + function checkForStatement(node) { + // Grammar checking + if (!checkGrammarStatementInAmbientContext(node)) { + if (node.initializer && node.initializer.kind === 219 /* VariableDeclarationList */) { + checkGrammarVariableDeclarationList(node.initializer); } - copySymbols(globals, meaning); } - /** - * Copy the given symbol into symbol tables if the symbol has the given meaning - * and it doesn't already existed in the symbol table - * @param key a key for storing in symbol table; if undefined, use symbol.name - * @param symbol the symbol to be added into symbol table - * @param meaning meaning of symbol to filter by before adding to symbol table - */ - function copySymbol(symbol, meaning) { - if (symbol.flags & meaning) { - var id = symbol.name; - // We will copy all symbol regardless of its reserved name because - // symbolsToArray will check whether the key is a reserved name and - // it will not copy symbol with reserved name to the array - if (!symbols[id]) { - symbols[id] = symbol; - } + if (node.initializer) { + if (node.initializer.kind === 219 /* VariableDeclarationList */) { + ts.forEach(node.initializer.declarations, checkVariableDeclaration); } - } - function copySymbols(source, meaning) { - if (meaning) { - for (var id in source) { - var symbol = source[id]; - copySymbol(symbol, meaning); - } + else { + checkExpression(node.initializer); } } - } - function isTypeDeclarationName(name) { - return name.kind === 69 /* Identifier */ && - isTypeDeclaration(name.parent) && - name.parent.name === name; - } - function isTypeDeclaration(node) { - switch (node.kind) { - case 141 /* TypeParameter */: - case 221 /* ClassDeclaration */: - case 222 /* InterfaceDeclaration */: - case 223 /* TypeAliasDeclaration */: - case 224 /* EnumDeclaration */: - return true; + if (node.condition) + checkExpression(node.condition); + if (node.incrementor) + checkExpression(node.incrementor); + checkSourceElement(node.statement); + if (node.locals) { + registerForUnusedIdentifiersCheck(node); } } - // True if the given identifier is part of a type reference - function isTypeReferenceIdentifier(entityName) { - var node = entityName; - while (node.parent && node.parent.kind === 139 /* QualifiedName */) { - node = node.parent; + function checkForOfStatement(node) { + checkGrammarForInOrForOfStatement(node); + // Check the LHS and RHS + // If the LHS is a declaration, just check it as a variable declaration, which will in turn check the RHS + // via checkRightHandSideOfForOf. + // If the LHS is an expression, check the LHS, as a destructuring assignment or as a reference. + // Then check that the RHS is assignable to it. + if (node.initializer.kind === 219 /* VariableDeclarationList */) { + checkForInOrForOfVariableDeclaration(node); } - return node.parent && (node.parent.kind === 155 /* TypeReference */ || node.parent.kind === 267 /* JSDocTypeReference */); - } - function isHeritageClauseElementIdentifier(entityName) { - var node = entityName; - while (node.parent && node.parent.kind === 172 /* PropertyAccessExpression */) { - node = node.parent; + else { + var varExpr = node.initializer; + var iteratedType = checkRightHandSideOfForOf(node.expression); + // There may be a destructuring assignment on the left side + if (varExpr.kind === 170 /* ArrayLiteralExpression */ || varExpr.kind === 171 /* ObjectLiteralExpression */) { + // iteratedType may be undefined. In this case, we still want to check the structure of + // varExpr, in particular making sure it's a valid LeftHandSideExpression. But we'd like + // to short circuit the type relation checking as much as possible, so we pass the unknownType. + checkDestructuringAssignment(varExpr, iteratedType || unknownType); + } + else { + var leftType = checkExpression(varExpr); + checkReferenceExpression(varExpr, /*invalidReferenceMessage*/ ts.Diagnostics.Invalid_left_hand_side_in_for_of_statement, + /*constantVariableMessage*/ ts.Diagnostics.The_left_hand_side_of_a_for_of_statement_cannot_be_a_constant_or_a_read_only_property); + // iteratedType will be undefined if the rightType was missing properties/signatures + // required to get its iteratedType (like [Symbol.iterator] or next). This may be + // because we accessed properties from anyType, or it may have led to an error inside + // getElementTypeOfIterable. + if (iteratedType) { + checkTypeAssignableTo(iteratedType, leftType, varExpr, /*headMessage*/ undefined); + } + } } - return node.parent && node.parent.kind === 194 /* ExpressionWithTypeArguments */; - } - function forEachEnclosingClass(node, callback) { - var result; - while (true) { - node = ts.getContainingClass(node); - if (!node) - break; - if (result = callback(node)) - break; + checkSourceElement(node.statement); + if (node.locals) { + registerForUnusedIdentifiersCheck(node); } - return result; - } - function isNodeWithinClass(node, classDeclaration) { - return !!forEachEnclosingClass(node, function (n) { return n === classDeclaration; }); } - function getLeftSideOfImportEqualsOrExportAssignment(nodeOnRightSide) { - while (nodeOnRightSide.parent.kind === 139 /* QualifiedName */) { - nodeOnRightSide = nodeOnRightSide.parent; + function checkForInStatement(node) { + // Grammar checking + checkGrammarForInOrForOfStatement(node); + // TypeScript 1.0 spec (April 2014): 5.4 + // In a 'for-in' statement of the form + // for (let VarDecl in Expr) Statement + // VarDecl must be a variable declaration without a type annotation that declares a variable of type Any, + // and Expr must be an expression of type Any, an object type, or a type parameter type. + if (node.initializer.kind === 219 /* VariableDeclarationList */) { + var variable = node.initializer.declarations[0]; + if (variable && ts.isBindingPattern(variable.name)) { + error(variable.name, ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_a_destructuring_pattern); + } + checkForInOrForOfVariableDeclaration(node); } - if (nodeOnRightSide.parent.kind === 229 /* ImportEqualsDeclaration */) { - return nodeOnRightSide.parent.moduleReference === nodeOnRightSide && nodeOnRightSide.parent; + else { + // In a 'for-in' statement of the form + // for (Var in Expr) Statement + // Var must be an expression classified as a reference of type Any or the String primitive type, + // and Expr must be an expression of type Any, an object type, or a type parameter type. + var varExpr = node.initializer; + var leftType = checkExpression(varExpr); + if (varExpr.kind === 170 /* ArrayLiteralExpression */ || varExpr.kind === 171 /* ObjectLiteralExpression */) { + error(varExpr, ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_a_destructuring_pattern); + } + else if (!isTypeAnyOrAllConstituentTypesHaveKind(leftType, 34 /* StringLike */)) { + error(varExpr, ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_must_be_of_type_string_or_any); + } + else { + // run check only former check succeeded to avoid cascading errors + checkReferenceExpression(varExpr, ts.Diagnostics.Invalid_left_hand_side_in_for_in_statement, ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_be_a_constant_or_a_read_only_property); + } } - if (nodeOnRightSide.parent.kind === 235 /* ExportAssignment */) { - return nodeOnRightSide.parent.expression === nodeOnRightSide && nodeOnRightSide.parent; + var rightType = checkNonNullExpression(node.expression); + // unknownType is returned i.e. if node.expression is identifier whose name cannot be resolved + // in this case error about missing name is already reported - do not report extra one + if (!isTypeAnyOrAllConstituentTypesHaveKind(rightType, 2588672 /* ObjectType */ | 16384 /* TypeParameter */)) { + error(node.expression, ts.Diagnostics.The_right_hand_side_of_a_for_in_statement_must_be_of_type_any_an_object_type_or_a_type_parameter); + } + checkSourceElement(node.statement); + if (node.locals) { + registerForUnusedIdentifiersCheck(node); } - return undefined; } - function isInRightSideOfImportOrExportAssignment(node) { - return getLeftSideOfImportEqualsOrExportAssignment(node) !== undefined; + function checkForInOrForOfVariableDeclaration(iterationStatement) { + var variableDeclarationList = iterationStatement.initializer; + // checkGrammarForInOrForOfStatement will check that there is exactly one declaration. + if (variableDeclarationList.declarations.length >= 1) { + var decl = variableDeclarationList.declarations[0]; + checkVariableDeclaration(decl); + } } - function getSymbolOfEntityNameOrPropertyAccessExpression(entityName) { - if (ts.isDeclarationName(entityName)) { - return getSymbolOfNode(entityName.parent); + function checkRightHandSideOfForOf(rhsExpression) { + var expressionType = checkNonNullExpression(rhsExpression); + return checkIteratedTypeOrElementType(expressionType, rhsExpression, /*allowStringInput*/ true); + } + function checkIteratedTypeOrElementType(inputType, errorNode, allowStringInput) { + if (isTypeAny(inputType)) { + return inputType; } - if (ts.isInJavaScriptFile(entityName) && entityName.parent.kind === 172 /* PropertyAccessExpression */) { - var specialPropertyAssignmentKind = ts.getSpecialPropertyAssignmentKind(entityName.parent.parent); - switch (specialPropertyAssignmentKind) { - case 1 /* ExportsProperty */: - case 3 /* PrototypeProperty */: - return getSymbolOfNode(entityName.parent); - case 4 /* ThisProperty */: - case 2 /* ModuleExports */: - return getSymbolOfNode(entityName.parent.parent); - default: + if (languageVersion >= 2 /* ES6 */) { + return checkElementTypeOfIterable(inputType, errorNode); + } + if (allowStringInput) { + return checkElementTypeOfArrayOrString(inputType, errorNode); + } + if (isArrayLikeType(inputType)) { + var indexType = getIndexTypeOfType(inputType, 1 /* Number */); + if (indexType) { + return indexType; } } - if (entityName.parent.kind === 235 /* ExportAssignment */ && ts.isEntityNameExpression(entityName)) { - return resolveEntityName(entityName, - /*all meanings*/ 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */ | 8388608 /* Alias */); + if (errorNode) { + error(errorNode, ts.Diagnostics.Type_0_is_not_an_array_type, typeToString(inputType)); } - if (entityName.kind !== 172 /* PropertyAccessExpression */ && isInRightSideOfImportOrExportAssignment(entityName)) { - // Since we already checked for ExportAssignment, this really could only be an Import - var importEqualsDeclaration = ts.getAncestor(entityName, 229 /* ImportEqualsDeclaration */); - ts.Debug.assert(importEqualsDeclaration !== undefined); - return getSymbolOfPartOfRightHandSideOfImportEquals(entityName, importEqualsDeclaration, /*dontResolveAlias*/ true); + return unknownType; + } + /** + * When errorNode is undefined, it means we should not report any errors. + */ + function checkElementTypeOfIterable(iterable, errorNode) { + var elementType = getElementTypeOfIterable(iterable, errorNode); + // Now even though we have extracted the iteratedType, we will have to validate that the type + // passed in is actually an Iterable. + if (errorNode && elementType) { + checkTypeAssignableTo(iterable, createIterableType(elementType), errorNode); } - if (ts.isRightSideOfQualifiedNameOrPropertyAccess(entityName)) { - entityName = entityName.parent; + return elementType || anyType; + } + /** + * We want to treat type as an iterable, and get the type it is an iterable of. The iterable + * must have the following structure (annotated with the names of the variables below): + * + * { // iterable + * [Symbol.iterator]: { // iteratorFunction + * (): Iterator + * } + * } + * + * T is the type we are after. At every level that involves analyzing return types + * of signatures, we union the return types of all the signatures. + * + * Another thing to note is that at any step of this process, we could run into a dead end, + * meaning either the property is missing, or we run into the anyType. If either of these things + * happens, we return undefined to signal that we could not find the iterated type. If a property + * is missing, and the previous step did not result in 'any', then we also give an error if the + * caller requested it. Then the caller can decide what to do in the case where there is no iterated + * type. This is different from returning anyType, because that would signify that we have matched the + * whole pattern and that T (above) is 'any'. + */ + function getElementTypeOfIterable(type, errorNode) { + if (isTypeAny(type)) { + return undefined; } - if (isHeritageClauseElementIdentifier(entityName)) { - var meaning = 0 /* None */; - // In an interface or class, we're definitely interested in a type. - if (entityName.parent.kind === 194 /* ExpressionWithTypeArguments */) { - meaning = 793064 /* Type */; - // In a class 'extends' clause we are also looking for a value. - if (ts.isExpressionWithTypeArgumentsInClassExtendsClause(entityName.parent)) { - meaning |= 107455 /* Value */; - } + var typeAsIterable = type; + if (!typeAsIterable.iterableElementType) { + // As an optimization, if the type is instantiated directly using the globalIterableType (Iterable), + // then just grab its type argument. + if ((type.flags & 131072 /* Reference */) && type.target === getGlobalIterableType()) { + typeAsIterable.iterableElementType = type.typeArguments[0]; } else { - meaning = 1920 /* Namespace */; + var iteratorFunction = getTypeOfPropertyOfType(type, ts.getPropertyNameForKnownSymbolName("iterator")); + if (isTypeAny(iteratorFunction)) { + return undefined; + } + var iteratorFunctionSignatures = iteratorFunction ? getSignaturesOfType(iteratorFunction, 0 /* Call */) : emptyArray; + if (iteratorFunctionSignatures.length === 0) { + if (errorNode) { + error(errorNode, ts.Diagnostics.Type_must_have_a_Symbol_iterator_method_that_returns_an_iterator); + } + return undefined; + } + typeAsIterable.iterableElementType = getElementTypeOfIterator(getUnionType(ts.map(iteratorFunctionSignatures, getReturnTypeOfSignature), /*subtypeReduction*/ true), errorNode); } - meaning |= 8388608 /* Alias */; - return resolveEntityName(entityName, meaning); } - else if (ts.isPartOfExpression(entityName)) { - if (ts.nodeIsMissing(entityName)) { - // Missing entity name. - return undefined; + return typeAsIterable.iterableElementType; + } + /** + * This function has very similar logic as getElementTypeOfIterable, except that it operates on + * Iterators instead of Iterables. Here is the structure: + * + * { // iterator + * next: { // iteratorNextFunction + * (): { // iteratorNextResult + * value: T // iteratorNextValue + * } + * } + * } + * + */ + function getElementTypeOfIterator(type, errorNode) { + if (isTypeAny(type)) { + return undefined; + } + var typeAsIterator = type; + if (!typeAsIterator.iteratorElementType) { + // As an optimization, if the type is instantiated directly using the globalIteratorType (Iterator), + // then just grab its type argument. + if ((type.flags & 131072 /* Reference */) && type.target === getGlobalIteratorType()) { + typeAsIterator.iteratorElementType = type.typeArguments[0]; } - if (entityName.kind === 69 /* Identifier */) { - if (ts.isJSXTagName(entityName) && isJsxIntrinsicIdentifier(entityName)) { - return getIntrinsicTagSymbol(entityName.parent); + else { + var iteratorNextFunction = getTypeOfPropertyOfType(type, "next"); + if (isTypeAny(iteratorNextFunction)) { + return undefined; } - return resolveEntityName(entityName, 107455 /* Value */, /*ignoreErrors*/ false, /*dontResolveAlias*/ true); - } - else if (entityName.kind === 172 /* PropertyAccessExpression */) { - var symbol = getNodeLinks(entityName).resolvedSymbol; - if (!symbol) { - checkPropertyAccessExpression(entityName); + var iteratorNextFunctionSignatures = iteratorNextFunction ? getSignaturesOfType(iteratorNextFunction, 0 /* Call */) : emptyArray; + if (iteratorNextFunctionSignatures.length === 0) { + if (errorNode) { + error(errorNode, ts.Diagnostics.An_iterator_must_have_a_next_method); + } + return undefined; } - return getNodeLinks(entityName).resolvedSymbol; - } - else if (entityName.kind === 139 /* QualifiedName */) { - var symbol = getNodeLinks(entityName).resolvedSymbol; - if (!symbol) { - checkQualifiedName(entityName); + var iteratorNextResult = getUnionType(ts.map(iteratorNextFunctionSignatures, getReturnTypeOfSignature), /*subtypeReduction*/ true); + if (isTypeAny(iteratorNextResult)) { + return undefined; } - return getNodeLinks(entityName).resolvedSymbol; + var iteratorNextValue = getTypeOfPropertyOfType(iteratorNextResult, "value"); + if (!iteratorNextValue) { + if (errorNode) { + error(errorNode, ts.Diagnostics.The_type_returned_by_the_next_method_of_an_iterator_must_have_a_value_property); + } + return undefined; + } + typeAsIterator.iteratorElementType = iteratorNextValue; } } - else if (isTypeReferenceIdentifier(entityName)) { - var meaning = (entityName.parent.kind === 155 /* TypeReference */ || entityName.parent.kind === 267 /* JSDocTypeReference */) ? 793064 /* Type */ : 1920 /* Namespace */; - return resolveEntityName(entityName, meaning, /*ignoreErrors*/ false, /*dontResolveAlias*/ true); - } - else if (entityName.parent.kind === 246 /* JsxAttribute */) { - return getJsxAttributePropertySymbol(entityName.parent); + return typeAsIterator.iteratorElementType; + } + function getElementTypeOfIterableIterator(type) { + if (isTypeAny(type)) { + return undefined; } - if (entityName.parent.kind === 154 /* TypePredicate */) { - return resolveEntityName(entityName, /*meaning*/ 1 /* FunctionScopedVariable */); + // As an optimization, if the type is instantiated directly using the globalIterableIteratorType (IterableIterator), + // then just grab its type argument. + if ((type.flags & 131072 /* Reference */) && type.target === getGlobalIterableIteratorType()) { + return type.typeArguments[0]; } - // Do we want to return undefined here? - return undefined; + return getElementTypeOfIterable(type, /*errorNode*/ undefined) || + getElementTypeOfIterator(type, /*errorNode*/ undefined); } - function getSymbolAtLocation(node) { - if (node.kind === 256 /* SourceFile */) { - return ts.isExternalModule(node) ? getMergedSymbol(node.symbol) : undefined; + /** + * This function does the following steps: + * 1. Break up arrayOrStringType (possibly a union) into its string constituents and array constituents. + * 2. Take the element types of the array constituents. + * 3. Return the union of the element types, and string if there was a string constituent. + * + * For example: + * string -> string + * number[] -> number + * string[] | number[] -> string | number + * string | number[] -> string | number + * string | string[] | number[] -> string | number + * + * It also errors if: + * 1. Some constituent is neither a string nor an array. + * 2. Some constituent is a string and target is less than ES5 (because in ES3 string is not indexable). + */ + function checkElementTypeOfArrayOrString(arrayOrStringType, errorNode) { + ts.Debug.assert(languageVersion < 2 /* ES6 */); + // After we remove all types that are StringLike, we will know if there was a string constituent + // based on whether the remaining type is the same as the initial type. + var arrayType = arrayOrStringType; + if (arrayOrStringType.flags & 524288 /* Union */) { + arrayType = getUnionType(ts.filter(arrayOrStringType.types, function (t) { return !(t.flags & 34 /* StringLike */); }), /*subtypeReduction*/ true); } - if (isInsideWithStatementBody(node)) { - // We cannot answer semantic questions within a with block, do not proceed any further - return undefined; + else if (arrayOrStringType.flags & 34 /* StringLike */) { + arrayType = neverType; } - if (ts.isDeclarationName(node)) { - // This is a declaration, call getSymbolOfNode - return getSymbolOfNode(node.parent); + var hasStringConstituent = arrayOrStringType !== arrayType; + var reportedError = false; + if (hasStringConstituent) { + if (languageVersion < 1 /* ES5 */) { + error(errorNode, ts.Diagnostics.Using_a_string_in_a_for_of_statement_is_only_supported_in_ECMAScript_5_and_higher); + reportedError = true; + } + // Now that we've removed all the StringLike types, if no constituents remain, then the entire + // arrayOrStringType was a string. + if (arrayType.flags & 8192 /* Never */) { + return stringType; + } } - else if (ts.isLiteralComputedPropertyDeclarationName(node)) { - return getSymbolOfNode(node.parent.parent); + if (!isArrayLikeType(arrayType)) { + if (!reportedError) { + // Which error we report depends on whether there was a string constituent. For example, + // if the input type is number | string, we want to say that number is not an array type. + // But if the input was just number, we want to say that number is not an array type + // or a string type. + var diagnostic = hasStringConstituent + ? ts.Diagnostics.Type_0_is_not_an_array_type + : ts.Diagnostics.Type_0_is_not_an_array_type_or_a_string_type; + error(errorNode, diagnostic, typeToString(arrayType)); + } + return hasStringConstituent ? stringType : unknownType; } - if (node.kind === 69 /* Identifier */) { - if (isInRightSideOfImportOrExportAssignment(node)) { - return getSymbolOfEntityNameOrPropertyAccessExpression(node); + var arrayElementType = getIndexTypeOfType(arrayType, 1 /* Number */) || unknownType; + if (hasStringConstituent) { + // This is just an optimization for the case where arrayOrStringType is string | string[] + if (arrayElementType.flags & 34 /* StringLike */) { + return stringType; } - else if (node.parent.kind === 169 /* BindingElement */ && - node.parent.parent.kind === 167 /* ObjectBindingPattern */ && - node === node.parent.propertyName) { - var typeOfPattern = getTypeOfNode(node.parent.parent); - var propertyDeclaration = typeOfPattern && getPropertyOfType(typeOfPattern, node.text); - if (propertyDeclaration) { - return propertyDeclaration; - } + return getUnionType([arrayElementType, stringType], /*subtypeReduction*/ true); + } + return arrayElementType; + } + function checkBreakOrContinueStatement(node) { + // Grammar checking + checkGrammarStatementInAmbientContext(node) || checkGrammarBreakOrContinueStatement(node); + // TODO: Check that target label is valid + } + function isGetAccessorWithAnnotatedSetAccessor(node) { + return !!(node.kind === 149 /* GetAccessor */ && ts.getSetAccessorTypeAnnotationNode(ts.getDeclarationOfKind(node.symbol, 150 /* SetAccessor */))); + } + function isUnwrappedReturnTypeVoidOrAny(func, returnType) { + var unwrappedReturnType = ts.isAsyncFunctionLike(func) ? getPromisedType(returnType) : returnType; + return unwrappedReturnType && maybeTypeOfKind(unwrappedReturnType, 1024 /* Void */ | 1 /* Any */); + } + function checkReturnStatement(node) { + // Grammar checking + if (!checkGrammarStatementInAmbientContext(node)) { + var functionBlock = ts.getContainingFunction(node); + if (!functionBlock) { + grammarErrorOnFirstToken(node, ts.Diagnostics.A_return_statement_can_only_be_used_within_a_function_body); } } - switch (node.kind) { - case 69 /* Identifier */: - case 172 /* PropertyAccessExpression */: - case 139 /* QualifiedName */: - return getSymbolOfEntityNameOrPropertyAccessExpression(node); - case 97 /* ThisKeyword */: - var container = ts.getThisContainer(node, /*includeArrowFunctions*/ false); - if (ts.isFunctionLike(container)) { - var sig = getSignatureFromDeclaration(container); - if (sig.thisParameter) { - return sig.thisParameter; - } + var func = ts.getContainingFunction(node); + if (func) { + var signature = getSignatureFromDeclaration(func); + var returnType = getReturnTypeOfSignature(signature); + if (strictNullChecks || node.expression || returnType.flags & 8192 /* Never */) { + var exprType = node.expression ? checkExpressionCached(node.expression) : undefinedType; + if (func.asteriskToken) { + // A generator does not need its return expressions checked against its return type. + // Instead, the yield expressions are checked against the element type. + // TODO: Check return expressions of generators when return type tracking is added + // for generators. + return; } - // fallthrough - case 95 /* SuperKeyword */: - var type = ts.isPartOfExpression(node) ? checkExpression(node) : getTypeFromTypeNode(node); - return type.symbol; - case 165 /* ThisType */: - return getTypeFromTypeNode(node).symbol; - case 121 /* ConstructorKeyword */: - // constructor keyword for an overload, should take us to the definition if it exist - var constructorDeclaration = node.parent; - if (constructorDeclaration && constructorDeclaration.kind === 148 /* Constructor */) { - return constructorDeclaration.parent.symbol; + if (func.kind === 150 /* SetAccessor */) { + if (node.expression) { + error(node.expression, ts.Diagnostics.Setters_cannot_return_a_value); + } } - return undefined; - case 9 /* StringLiteral */: - // External module name in an import declaration - if ((ts.isExternalModuleImportEqualsDeclaration(node.parent.parent) && - ts.getExternalModuleImportEqualsDeclarationExpression(node.parent.parent) === node) || - ((node.parent.kind === 230 /* ImportDeclaration */ || node.parent.kind === 236 /* ExportDeclaration */) && - node.parent.moduleSpecifier === node)) { - return resolveExternalModuleName(node, node); + else if (func.kind === 148 /* Constructor */) { + if (node.expression && !checkTypeAssignableTo(exprType, returnType, node.expression)) { + error(node.expression, ts.Diagnostics.Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class); + } } - // Fall through - case 8 /* NumericLiteral */: - // index access - if (node.parent.kind === 173 /* ElementAccessExpression */ && node.parent.argumentExpression === node) { - var objectType = checkExpression(node.parent.expression); - if (objectType === unknownType) - return undefined; - var apparentType = getApparentType(objectType); - if (apparentType === unknownType) - return undefined; - return getPropertyOfType(apparentType, node.text); + else if (func.type || isGetAccessorWithAnnotatedSetAccessor(func)) { + if (ts.isAsyncFunctionLike(func)) { + var promisedType = getPromisedType(returnType); + var awaitedType = checkAwaitedType(exprType, node.expression || node, ts.Diagnostics.Return_expression_in_async_function_does_not_have_a_valid_callable_then_member); + if (promisedType) { + // If the function has a return type, but promisedType is + // undefined, an error will be reported in checkAsyncFunctionReturnType + // so we don't need to report one here. + checkTypeAssignableTo(awaitedType, promisedType, node.expression || node); + } + } + else { + checkTypeAssignableTo(exprType, returnType, node.expression || node); + } } - break; + } + else if (func.kind !== 148 /* Constructor */ && compilerOptions.noImplicitReturns && !isUnwrappedReturnTypeVoidOrAny(func, returnType)) { + // The function has a return type, but the return statement doesn't have an expression. + error(node, ts.Diagnostics.Not_all_code_paths_return_a_value); + } } - return undefined; } - function getShorthandAssignmentValueSymbol(location) { - // The function returns a value symbol of an identifier in the short-hand property assignment. - // This is necessary as an identifier in short-hand property assignment can contains two meaning: - // property name and property value. - if (location && location.kind === 254 /* ShorthandPropertyAssignment */) { - return resolveEntityName(location.name, 107455 /* Value */ | 8388608 /* Alias */); + function checkWithStatement(node) { + // Grammar checking for withStatement + if (!checkGrammarStatementInAmbientContext(node)) { + if (node.flags & 262144 /* AwaitContext */) { + grammarErrorOnFirstToken(node, ts.Diagnostics.with_statements_are_not_allowed_in_an_async_function_block); + } + } + checkExpression(node.expression); + var sourceFile = ts.getSourceFileOfNode(node); + if (!hasParseDiagnostics(sourceFile)) { + var start = ts.getSpanOfTokenAtPosition(sourceFile, node.pos).start; + var end = node.statement.pos; + grammarErrorAtPos(sourceFile, start, end - start, ts.Diagnostics.The_with_statement_is_not_supported_All_symbols_in_a_with_block_will_have_type_any); } - return undefined; } - /** Returns the target of an export specifier without following aliases */ - function getExportSpecifierLocalTargetSymbol(node) { - return node.parent.parent.moduleSpecifier ? - getExternalModuleMember(node.parent.parent, node) : - resolveEntityName(node.propertyName || node.name, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */ | 8388608 /* Alias */); + function checkSwitchStatement(node) { + // Grammar checking + checkGrammarStatementInAmbientContext(node); + var firstDefaultClause; + var hasDuplicateDefaultClause = false; + var expressionType = checkExpression(node.expression); + ts.forEach(node.caseBlock.clauses, function (clause) { + // Grammar check for duplicate default clauses, skip if we already report duplicate default clause + if (clause.kind === 250 /* DefaultClause */ && !hasDuplicateDefaultClause) { + if (firstDefaultClause === undefined) { + firstDefaultClause = clause; + } + else { + var sourceFile = ts.getSourceFileOfNode(node); + var start = ts.skipTrivia(sourceFile.text, clause.pos); + var end = clause.statements.length > 0 ? clause.statements[0].pos : clause.end; + grammarErrorAtPos(sourceFile, start, end - start, ts.Diagnostics.A_default_clause_cannot_appear_more_than_once_in_a_switch_statement); + hasDuplicateDefaultClause = true; + } + } + if (produceDiagnostics && clause.kind === 249 /* CaseClause */) { + var caseClause = clause; + // TypeScript 1.0 spec (April 2014): 5.9 + // In a 'switch' statement, each 'case' expression must be of a type that is comparable + // to or from the type of the 'switch' expression. + var caseType = checkExpression(caseClause.expression); + if (!isTypeEqualityComparableTo(expressionType, caseType)) { + // expressionType is not comparable to caseType, try the reversed check and report errors if it fails + checkTypeComparableTo(caseType, expressionType, caseClause.expression, /*headMessage*/ undefined); + } + } + ts.forEach(clause.statements, checkSourceElement); + }); + if (node.caseBlock.locals) { + registerForUnusedIdentifiersCheck(node.caseBlock); + } } - function getTypeOfNode(node) { - if (isInsideWithStatementBody(node)) { - // We cannot answer semantic questions within a with block, do not proceed any further - return unknownType; + function checkLabeledStatement(node) { + // Grammar checking + if (!checkGrammarStatementInAmbientContext(node)) { + var current = node.parent; + while (current) { + if (ts.isFunctionLike(current)) { + break; + } + if (current.kind === 214 /* LabeledStatement */ && current.label.text === node.label.text) { + var sourceFile = ts.getSourceFileOfNode(node); + grammarErrorOnNode(node.label, ts.Diagnostics.Duplicate_label_0, ts.getTextOfNodeFromSourceText(sourceFile.text, node.label)); + break; + } + current = current.parent; + } } - if (ts.isPartOfTypeNode(node)) { - return getTypeFromTypeNode(node); - } - if (ts.isPartOfExpression(node)) { - return getTypeOfExpression(node); - } - if (ts.isExpressionWithTypeArgumentsInClassExtendsClause(node)) { - // A SyntaxKind.ExpressionWithTypeArguments is considered a type node, except when it occurs in the - // extends clause of a class. We handle that case here. - return getBaseTypes(getDeclaredTypeOfSymbol(getSymbolOfNode(node.parent.parent)))[0]; - } - if (isTypeDeclaration(node)) { - // In this case, we call getSymbolOfNode instead of getSymbolAtLocation because it is a declaration - var symbol = getSymbolOfNode(node); - return getDeclaredTypeOfSymbol(symbol); - } - if (isTypeDeclarationName(node)) { - var symbol = getSymbolAtLocation(node); - return symbol && getDeclaredTypeOfSymbol(symbol); - } - if (ts.isDeclaration(node)) { - // In this case, we call getSymbolOfNode instead of getSymbolAtLocation because it is a declaration - var symbol = getSymbolOfNode(node); - return getTypeOfSymbol(symbol); - } - if (ts.isDeclarationName(node)) { - var symbol = getSymbolAtLocation(node); - return symbol && getTypeOfSymbol(symbol); - } - if (ts.isBindingPattern(node)) { - return getTypeForVariableLikeDeclaration(node.parent, /*includeOptionality*/ true); - } - if (isInRightSideOfImportOrExportAssignment(node)) { - var symbol = getSymbolAtLocation(node); - var declaredType = symbol && getDeclaredTypeOfSymbol(symbol); - return declaredType !== unknownType ? declaredType : getTypeOfSymbol(symbol); - } - return unknownType; + // ensure that label is unique + checkSourceElement(node.statement); } - // Gets the type of object literal or array literal of destructuring assignment. - // { a } from - // for ( { a } of elems) { - // } - // [ a ] from - // [a] = [ some array ...] - function getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(expr) { - ts.Debug.assert(expr.kind === 171 /* ObjectLiteralExpression */ || expr.kind === 170 /* ArrayLiteralExpression */); - // If this is from "for of" - // for ( { a } of elems) { - // } - if (expr.parent.kind === 208 /* ForOfStatement */) { - var iteratedType = checkRightHandSideOfForOf(expr.parent.expression); - return checkDestructuringAssignment(expr, iteratedType || unknownType); - } - // If this is from "for" initializer - // for ({a } = elems[0];.....) { } - if (expr.parent.kind === 187 /* BinaryExpression */) { - var iteratedType = checkExpression(expr.parent.right); - return checkDestructuringAssignment(expr, iteratedType || unknownType); - } - // If this is from nested object binding pattern - // for ({ skills: { primary, secondary } } = multiRobot, i = 0; i < 1; i++) { - if (expr.parent.kind === 253 /* PropertyAssignment */) { - var typeOfParentObjectLiteral = getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(expr.parent.parent); - return checkObjectLiteralDestructuringPropertyAssignment(typeOfParentObjectLiteral || unknownType, expr.parent); + function checkThrowStatement(node) { + // Grammar checking + if (!checkGrammarStatementInAmbientContext(node)) { + if (node.expression === undefined) { + grammarErrorAfterFirstToken(node, ts.Diagnostics.Line_break_not_permitted_here); + } } - // Array literal assignment - array destructuring pattern - ts.Debug.assert(expr.parent.kind === 170 /* ArrayLiteralExpression */); - // [{ property1: p1, property2 }] = elems; - var typeOfArrayLiteral = getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(expr.parent); - var elementType = checkIteratedTypeOrElementType(typeOfArrayLiteral || unknownType, expr.parent, /*allowStringInput*/ false) || unknownType; - return checkArrayLiteralDestructuringElementAssignment(expr.parent, typeOfArrayLiteral, ts.indexOf(expr.parent.elements, expr), elementType || unknownType); - } - // Gets the property symbol corresponding to the property in destructuring assignment - // 'property1' from - // for ( { property1: a } of elems) { - // } - // 'property1' at location 'a' from: - // [a] = [ property1, property2 ] - function getPropertySymbolOfDestructuringAssignment(location) { - // Get the type of the object or array literal and then look for property of given name in the type - var typeOfObjectLiteral = getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(location.parent.parent); - return typeOfObjectLiteral && getPropertyOfType(typeOfObjectLiteral, location.text); - } - function getTypeOfExpression(expr) { - if (ts.isRightSideOfQualifiedNameOrPropertyAccess(expr)) { - expr = expr.parent; + if (node.expression) { + checkExpression(node.expression); } - return getRegularTypeOfLiteralType(checkExpression(expr)); - } - /** - * Gets either the static or instance type of a class element, based on - * whether the element is declared as "static". - */ - function getParentTypeOfClassElement(node) { - var classSymbol = getSymbolOfNode(node.parent); - return ts.getModifierFlags(node) & 32 /* Static */ - ? getTypeOfSymbol(classSymbol) - : getDeclaredTypeOfSymbol(classSymbol); } - // Return the list of properties of the given type, augmented with properties from Function - // if the type has call or construct signatures - function getAugmentedPropertiesOfType(type) { - type = getApparentType(type); - var propsByName = createSymbolTable(getPropertiesOfType(type)); - if (getSignaturesOfType(type, 0 /* Call */).length || getSignaturesOfType(type, 1 /* Construct */).length) { - ts.forEach(getPropertiesOfType(globalFunctionType), function (p) { - if (!propsByName[p.name]) { - propsByName[p.name] = p; + function checkTryStatement(node) { + // Grammar checking + checkGrammarStatementInAmbientContext(node); + checkBlock(node.tryBlock); + var catchClause = node.catchClause; + if (catchClause) { + // Grammar checking + if (catchClause.variableDeclaration) { + if (catchClause.variableDeclaration.name.kind !== 69 /* Identifier */) { + grammarErrorOnFirstToken(catchClause.variableDeclaration.name, ts.Diagnostics.Catch_clause_variable_name_must_be_an_identifier); } - }); - } - return getNamedMembers(propsByName); - } - function getRootSymbols(symbol) { - if (symbol.flags & 268435456 /* SyntheticProperty */) { - var symbols_3 = []; - var name_23 = symbol.name; - ts.forEach(getSymbolLinks(symbol).containingType.types, function (t) { - var symbol = getPropertyOfType(t, name_23); - if (symbol) { - symbols_3.push(symbol); + else if (catchClause.variableDeclaration.type) { + grammarErrorOnFirstToken(catchClause.variableDeclaration.type, ts.Diagnostics.Catch_clause_variable_cannot_have_a_type_annotation); + } + else if (catchClause.variableDeclaration.initializer) { + grammarErrorOnFirstToken(catchClause.variableDeclaration.initializer, ts.Diagnostics.Catch_clause_variable_cannot_have_an_initializer); + } + else { + var identifierName = catchClause.variableDeclaration.name.text; + var locals = catchClause.block.locals; + if (locals) { + var localSymbol = locals[identifierName]; + if (localSymbol && (localSymbol.flags & 2 /* BlockScopedVariable */) !== 0) { + grammarErrorOnNode(localSymbol.valueDeclaration, ts.Diagnostics.Cannot_redeclare_identifier_0_in_catch_clause, identifierName); + } + } } - }); - return symbols_3; - } - else if (symbol.flags & 67108864 /* Transient */) { - var target = void 0; - var next = symbol; - while (next = getSymbolLinks(next).target) { - target = next; - } - if (target) { - return [target]; } + checkBlock(catchClause.block); + } + if (node.finallyBlock) { + checkBlock(node.finallyBlock); } - return [symbol]; } - // Emitter support - function isArgumentsLocalBinding(node) { - if (!ts.isGeneratedIdentifier(node)) { - node = ts.getParseTreeNode(node, ts.isIdentifier); - if (node) { - return getReferencedValueSymbol(node) === argumentsSymbol; + function checkIndexConstraints(type) { + var declaredNumberIndexer = getIndexDeclarationOfSymbol(type.symbol, 1 /* Number */); + var declaredStringIndexer = getIndexDeclarationOfSymbol(type.symbol, 0 /* String */); + var stringIndexType = getIndexTypeOfType(type, 0 /* String */); + var numberIndexType = getIndexTypeOfType(type, 1 /* Number */); + if (stringIndexType || numberIndexType) { + ts.forEach(getPropertiesOfObjectType(type), function (prop) { + var propType = getTypeOfSymbol(prop); + checkIndexConstraintForProperty(prop, propType, type, declaredStringIndexer, stringIndexType, 0 /* String */); + checkIndexConstraintForProperty(prop, propType, type, declaredNumberIndexer, numberIndexType, 1 /* Number */); + }); + if (type.flags & 32768 /* Class */ && ts.isClassLike(type.symbol.valueDeclaration)) { + var classDeclaration = type.symbol.valueDeclaration; + for (var _i = 0, _a = classDeclaration.members; _i < _a.length; _i++) { + var member = _a[_i]; + // Only process instance properties with computed names here. + // Static properties cannot be in conflict with indexers, + // and properties with literal names were already checked. + if (!(ts.getModifierFlags(member) & 32 /* Static */) && ts.hasDynamicName(member)) { + var propType = getTypeOfSymbol(member.symbol); + checkIndexConstraintForProperty(member.symbol, propType, type, declaredStringIndexer, stringIndexType, 0 /* String */); + checkIndexConstraintForProperty(member.symbol, propType, type, declaredNumberIndexer, numberIndexType, 1 /* Number */); + } + } } } - return false; - } - function moduleExportsSomeValue(moduleReferenceExpression) { - var moduleSymbol = resolveExternalModuleName(moduleReferenceExpression.parent, moduleReferenceExpression); - if (!moduleSymbol || ts.isShorthandAmbientModuleSymbol(moduleSymbol)) { - // If the module is not found or is shorthand, assume that it may export a value. - return true; + var errorNode; + if (stringIndexType && numberIndexType) { + errorNode = declaredNumberIndexer || declaredStringIndexer; + // condition 'errorNode === undefined' may appear if types does not declare nor string neither number indexer + if (!errorNode && (type.flags & 65536 /* Interface */)) { + var someBaseTypeHasBothIndexers = ts.forEach(getBaseTypes(type), function (base) { return getIndexTypeOfType(base, 0 /* String */) && getIndexTypeOfType(base, 1 /* Number */); }); + errorNode = someBaseTypeHasBothIndexers ? undefined : type.symbol.declarations[0]; + } } - var hasExportAssignment = hasExportAssignmentSymbol(moduleSymbol); - // if module has export assignment then 'resolveExternalModuleSymbol' will return resolved symbol for export assignment - // otherwise it will return moduleSymbol itself - moduleSymbol = resolveExternalModuleSymbol(moduleSymbol); - var symbolLinks = getSymbolLinks(moduleSymbol); - if (symbolLinks.exportsSomeValue === undefined) { - // for export assignments - check if resolved symbol for RHS is itself a value - // otherwise - check if at least one export is value - symbolLinks.exportsSomeValue = hasExportAssignment - ? !!(moduleSymbol.flags & 107455 /* Value */) - : ts.forEachProperty(getExportsOfModule(moduleSymbol), isValue); + if (errorNode && !isTypeAssignableTo(numberIndexType, stringIndexType)) { + error(errorNode, ts.Diagnostics.Numeric_index_type_0_is_not_assignable_to_string_index_type_1, typeToString(numberIndexType), typeToString(stringIndexType)); } - return symbolLinks.exportsSomeValue; - function isValue(s) { - s = resolveSymbol(s); - return s && !!(s.flags & 107455 /* Value */); + function checkIndexConstraintForProperty(prop, propertyType, containingType, indexDeclaration, indexType, indexKind) { + if (!indexType) { + return; + } + // index is numeric and property name is not valid numeric literal + if (indexKind === 1 /* Number */ && !isNumericName(prop.valueDeclaration.name)) { + return; + } + // perform property check if property or indexer is declared in 'type' + // this allows to rule out cases when both property and indexer are inherited from the base class + var errorNode; + if (prop.valueDeclaration.name.kind === 140 /* ComputedPropertyName */ || prop.parent === containingType.symbol) { + errorNode = prop.valueDeclaration; + } + else if (indexDeclaration) { + errorNode = indexDeclaration; + } + else if (containingType.flags & 65536 /* Interface */) { + // for interfaces property and indexer might be inherited from different bases + // check if any base class already has both property and indexer. + // check should be performed only if 'type' is the first type that brings property\indexer together + var someBaseClassHasBothPropertyAndIndexer = ts.forEach(getBaseTypes(containingType), function (base) { return getPropertyOfObjectType(base, prop.name) && getIndexTypeOfType(base, indexKind); }); + errorNode = someBaseClassHasBothPropertyAndIndexer ? undefined : containingType.symbol.declarations[0]; + } + if (errorNode && !isTypeAssignableTo(propertyType, indexType)) { + var errorMessage = indexKind === 0 /* String */ + ? ts.Diagnostics.Property_0_of_type_1_is_not_assignable_to_string_index_type_2 + : ts.Diagnostics.Property_0_of_type_1_is_not_assignable_to_numeric_index_type_2; + error(errorNode, errorMessage, symbolToString(prop), typeToString(propertyType), typeToString(indexType)); + } } } - function isNameOfModuleOrEnumDeclaration(node) { - var parent = node.parent; - return ts.isModuleOrEnumDeclaration(parent) && node === parent.name; + function checkTypeNameIsReserved(name, message) { + // TS 1.0 spec (April 2014): 3.6.1 + // The predefined type keywords are reserved and cannot be used as names of user defined types. + switch (name.text) { + case "any": + case "number": + case "boolean": + case "string": + case "symbol": + case "void": + error(name, message, name.text); + } } - // When resolved as an expression identifier, if the given node references an exported entity, return the declaration - // node of the exported entity's container. Otherwise, return undefined. - function getReferencedExportContainer(node, prefixLocals) { - node = ts.getParseTreeNode(node, ts.isIdentifier); - if (node) { - // When resolving the export container for the name of a module or enum - // declaration, we need to start resolution at the declaration's container. - // Otherwise, we could incorrectly resolve the export container as the - // declaration if it contains an exported member with the same name. - var symbol = getReferencedValueSymbol(node, /*startInDeclarationContainer*/ isNameOfModuleOrEnumDeclaration(node)); - if (symbol) { - if (symbol.flags & 1048576 /* ExportValue */) { - // If we reference an exported entity within the same module declaration, then whether - // we prefix depends on the kind of entity. SymbolFlags.ExportHasLocal encompasses all the - // kinds that we do NOT prefix. - var exportSymbol = getMergedSymbol(symbol.exportSymbol); - if (!prefixLocals && exportSymbol.flags & 944 /* ExportHasLocal */) { - return undefined; - } - symbol = exportSymbol; - } - var parentSymbol = getParentOfSymbol(symbol); - if (parentSymbol) { - if (parentSymbol.flags & 512 /* ValueModule */ && parentSymbol.valueDeclaration.kind === 256 /* SourceFile */) { - var symbolFile = parentSymbol.valueDeclaration; - var referenceFile = ts.getSourceFileOfNode(node); - // If `node` accesses an export and that export isn't in the same file, then symbol is a namespace export, so return undefined. - var symbolIsUmdExport = symbolFile !== referenceFile; - return symbolIsUmdExport ? undefined : symbolFile; - } - for (var n = node.parent; n; n = n.parent) { - if (ts.isModuleOrEnumDeclaration(n) && getSymbolOfNode(n) === parentSymbol) { - return n; + /** Check each type parameter and check that type parameters have no duplicate type parameter declarations */ + function checkTypeParameters(typeParameterDeclarations) { + if (typeParameterDeclarations) { + for (var i = 0, n = typeParameterDeclarations.length; i < n; i++) { + var node = typeParameterDeclarations[i]; + checkTypeParameter(node); + if (produceDiagnostics) { + for (var j = 0; j < i; j++) { + if (typeParameterDeclarations[j].symbol === node.symbol) { + error(node.name, ts.Diagnostics.Duplicate_identifier_0, ts.declarationNameToString(node.name)); } } } } } } - // When resolved as an expression identifier, if the given node references an import, return the declaration of - // that import. Otherwise, return undefined. - function getReferencedImportDeclaration(node) { - node = ts.getParseTreeNode(node, ts.isIdentifier); - if (node) { - var symbol = getReferencedValueSymbol(node); - if (symbol && symbol.flags & 8388608 /* Alias */) { - return getDeclarationOfAliasSymbol(symbol); + /** Check that type parameter lists are identical across multiple declarations */ + function checkTypeParameterListsIdentical(node, symbol) { + if (symbol.declarations.length === 1) { + return; + } + var firstDecl; + for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { + var declaration = _a[_i]; + if (declaration.kind === 221 /* ClassDeclaration */ || declaration.kind === 222 /* InterfaceDeclaration */) { + if (!firstDecl) { + firstDecl = declaration; + } + else if (!areTypeParametersIdentical(firstDecl.typeParameters, node.typeParameters)) { + error(node.name, ts.Diagnostics.All_declarations_of_0_must_have_identical_type_parameters, node.name.text); + } } } - return undefined; } - function isSymbolOfDeclarationWithCollidingName(symbol) { - if (symbol.flags & 418 /* BlockScoped */) { - var links = getSymbolLinks(symbol); - if (links.isDeclarationWithCollidingName === undefined) { - var container = ts.getEnclosingBlockScopeContainer(symbol.valueDeclaration); - if (ts.isStatementWithLocals(container)) { - var nodeLinks_1 = getNodeLinks(symbol.valueDeclaration); - if (!!resolveName(container.parent, symbol.name, 107455 /* Value */, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined)) { - // redeclaration - always should be renamed - links.isDeclarationWithCollidingName = true; + function checkClassExpression(node) { + checkClassLikeDeclaration(node); + checkNodeDeferred(node); + return getTypeOfSymbol(getSymbolOfNode(node)); + } + function checkClassExpressionDeferred(node) { + ts.forEach(node.members, checkSourceElement); + registerForUnusedIdentifiersCheck(node); + } + function checkClassDeclaration(node) { + if (!node.name && !(ts.getModifierFlags(node) & 512 /* Default */)) { + grammarErrorOnFirstToken(node, ts.Diagnostics.A_class_declaration_without_the_default_modifier_must_have_a_name); + } + checkClassLikeDeclaration(node); + ts.forEach(node.members, checkSourceElement); + registerForUnusedIdentifiersCheck(node); + } + function checkClassLikeDeclaration(node) { + checkGrammarClassDeclarationHeritageClauses(node); + checkDecorators(node); + if (node.name) { + checkTypeNameIsReserved(node.name, ts.Diagnostics.Class_name_cannot_be_0); + checkCollisionWithCapturedThisVariable(node, node.name); + checkCollisionWithRequireExportsInGeneratedCode(node, node.name); + checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); + } + checkTypeParameters(node.typeParameters); + checkExportsOnMergedDeclarations(node); + var symbol = getSymbolOfNode(node); + var type = getDeclaredTypeOfSymbol(symbol); + var typeWithThis = getTypeWithThisArgument(type); + var staticType = getTypeOfSymbol(symbol); + checkTypeParameterListsIdentical(node, symbol); + checkClassForDuplicateDeclarations(node); + var baseTypeNode = ts.getClassExtendsHeritageClauseElement(node); + if (baseTypeNode) { + var baseTypes = getBaseTypes(type); + if (baseTypes.length && produceDiagnostics) { + var baseType_1 = baseTypes[0]; + var staticBaseType = getBaseConstructorTypeOfClass(type); + checkBaseTypeAccessibility(staticBaseType, baseTypeNode); + checkSourceElement(baseTypeNode.expression); + if (baseTypeNode.typeArguments) { + ts.forEach(baseTypeNode.typeArguments, checkSourceElement); + for (var _i = 0, _a = getConstructorsForTypeArguments(staticBaseType, baseTypeNode.typeArguments); _i < _a.length; _i++) { + var constructor = _a[_i]; + if (!checkTypeArgumentConstraints(constructor.typeParameters, baseTypeNode.typeArguments)) { + break; + } } - else if (nodeLinks_1.flags & 131072 /* CapturedBlockScopedBinding */) { - // binding is captured in the function - // should be renamed if: - // - binding is not top level - top level bindings never collide with anything - // AND - // - binding is not declared in loop, should be renamed to avoid name reuse across siblings - // let a, b - // { let x = 1; a = () => x; } - // { let x = 100; b = () => x; } - // console.log(a()); // should print '1' - // console.log(b()); // should print '100' - // OR - // - binding is declared inside loop but not in inside initializer of iteration statement or directly inside loop body - // * variables from initializer are passed to rewritten loop body as parameters so they are not captured directly - // * variables that are declared immediately in loop body will become top level variable after loop is rewritten and thus - // they will not collide with anything - var isDeclaredInLoop = nodeLinks_1.flags & 262144 /* BlockScopedBindingInLoop */; - var inLoopInitializer = ts.isIterationStatement(container, /*lookInLabeledStatements*/ false); - var inLoopBodyBlock = container.kind === 199 /* Block */ && ts.isIterationStatement(container.parent, /*lookInLabeledStatements*/ false); - links.isDeclarationWithCollidingName = !ts.isBlockScopedContainerTopLevel(container) && (!isDeclaredInLoop || (!inLoopInitializer && !inLoopBodyBlock)); + } + checkTypeAssignableTo(typeWithThis, getTypeWithThisArgument(baseType_1, type.thisType), node.name || node, ts.Diagnostics.Class_0_incorrectly_extends_base_class_1); + checkTypeAssignableTo(staticType, getTypeWithoutSignatures(staticBaseType), node.name || node, ts.Diagnostics.Class_static_side_0_incorrectly_extends_base_class_static_side_1); + if (baseType_1.symbol.valueDeclaration && !ts.isInAmbientContext(baseType_1.symbol.valueDeclaration)) { + if (!isBlockScopedNameDeclaredBeforeUse(baseType_1.symbol.valueDeclaration, node)) { + error(baseTypeNode, ts.Diagnostics.A_class_must_be_declared_after_its_base_class); } - else { - links.isDeclarationWithCollidingName = false; + } + if (!(staticBaseType.symbol && staticBaseType.symbol.flags & 32 /* Class */)) { + // When the static base type is a "class-like" constructor function (but not actually a class), we verify + // that all instantiated base constructor signatures return the same type. We can simply compare the type + // references (as opposed to checking the structure of the types) because elsewhere we have already checked + // that the base type is a class or interface type (and not, for example, an anonymous object type). + var constructors = getInstantiatedConstructorsForTypeArguments(staticBaseType, baseTypeNode.typeArguments); + if (ts.forEach(constructors, function (sig) { return getReturnTypeOfSignature(sig) !== baseType_1; })) { + error(baseTypeNode.expression, ts.Diagnostics.Base_constructors_must_all_have_the_same_return_type); } } + checkKindsOfPropertyMemberOverrides(type, baseType_1); } - return links.isDeclarationWithCollidingName; } - return false; - } - // When resolved as an expression identifier, if the given node references a nested block scoped entity with - // a name that either hides an existing name or might hide it when compiled downlevel, - // return the declaration of that entity. Otherwise, return undefined. - function getReferencedDeclarationWithCollidingName(node) { - if (!ts.isGeneratedIdentifier(node)) { - node = ts.getParseTreeNode(node, ts.isIdentifier); - if (node) { - var symbol = getReferencedValueSymbol(node); - if (symbol && isSymbolOfDeclarationWithCollidingName(symbol)) { - return symbol.valueDeclaration; + var implementedTypeNodes = ts.getClassImplementsHeritageClauseElements(node); + if (implementedTypeNodes) { + for (var _b = 0, implementedTypeNodes_1 = implementedTypeNodes; _b < implementedTypeNodes_1.length; _b++) { + var typeRefNode = implementedTypeNodes_1[_b]; + if (!ts.isEntityNameExpression(typeRefNode.expression)) { + error(typeRefNode.expression, ts.Diagnostics.A_class_can_only_implement_an_identifier_Slashqualified_name_with_optional_type_arguments); + } + checkTypeReferenceNode(typeRefNode); + if (produceDiagnostics) { + var t = getTypeFromTypeNode(typeRefNode); + if (t !== unknownType) { + var declaredType = (t.flags & 131072 /* Reference */) ? t.target : t; + if (declaredType.flags & (32768 /* Class */ | 65536 /* Interface */)) { + checkTypeAssignableTo(typeWithThis, getTypeWithThisArgument(t, type.thisType), node.name || node, ts.Diagnostics.Class_0_incorrectly_implements_interface_1); + } + else { + error(typeRefNode, ts.Diagnostics.A_class_may_only_implement_another_class_or_interface); + } + } } } } - return undefined; + if (produceDiagnostics) { + checkIndexConstraints(type); + checkTypeForDuplicateIndexSignatures(node); + } } - // Return true if the given node is a declaration of a nested block scoped entity with a name that either hides an - // existing name or might hide a name when compiled downlevel - function isDeclarationWithCollidingName(node) { - node = ts.getParseTreeNode(node, ts.isDeclaration); - if (node) { - var symbol = getSymbolOfNode(node); - if (symbol) { - return isSymbolOfDeclarationWithCollidingName(symbol); + function checkBaseTypeAccessibility(type, node) { + var signatures = getSignaturesOfType(type, 1 /* Construct */); + if (signatures.length) { + var declaration = signatures[0].declaration; + if (declaration && ts.getModifierFlags(declaration) & 8 /* Private */) { + var typeClassDeclaration = getClassLikeDeclarationOfSymbol(type.symbol); + if (!isNodeWithinClass(node, typeClassDeclaration)) { + error(node, ts.Diagnostics.Cannot_extend_a_class_0_Class_constructor_is_marked_as_private, node.expression.text); + } } } - return false; } - function isValueAliasDeclaration(node) { - node = ts.getParseTreeNode(node); - if (node === undefined) { - // A synthesized node comes from an emit transformation and is always a value. - return true; - } - switch (node.kind) { - case 229 /* ImportEqualsDeclaration */: - case 231 /* ImportClause */: - case 232 /* NamespaceImport */: - case 234 /* ImportSpecifier */: - case 238 /* ExportSpecifier */: - return isAliasResolvedToValue(getSymbolOfNode(node) || unknownSymbol); - case 236 /* ExportDeclaration */: - var exportClause = node.exportClause; - return exportClause && ts.forEach(exportClause.elements, isValueAliasDeclaration); - case 235 /* ExportAssignment */: - return node.expression - && node.expression.kind === 69 /* Identifier */ - ? isAliasResolvedToValue(getSymbolOfNode(node) || unknownSymbol) - : true; - } - return false; - } - function isTopLevelValueImportEqualsWithEntityName(node) { - node = ts.getParseTreeNode(node, ts.isImportEqualsDeclaration); - if (node === undefined || node.parent.kind !== 256 /* SourceFile */ || !ts.isInternalModuleImportEqualsDeclaration(node)) { - // parent is not source file or it is not reference to internal module - return false; - } - var isValue = isAliasResolvedToValue(getSymbolOfNode(node)); - return isValue && node.moduleReference && !ts.nodeIsMissing(node.moduleReference); - } - function isAliasResolvedToValue(symbol) { - var target = resolveAlias(symbol); - if (target === unknownSymbol) { - return true; - } - // const enums and modules that contain only const enums are not considered values from the emit perspective - // unless 'preserveConstEnums' option is set to true - return target.flags & 107455 /* Value */ && - (compilerOptions.preserveConstEnums || !isConstEnumOrConstEnumOnlyModule(target)); + function getTargetSymbol(s) { + // if symbol is instantiated its flags are not copied from the 'target' + // so we'll need to get back original 'target' symbol to work with correct set of flags + return s.flags & 16777216 /* Instantiated */ ? getSymbolLinks(s).target : s; } - function isConstEnumOrConstEnumOnlyModule(s) { - return isConstEnumSymbol(s) || s.constEnumOnlyModule; + function getClassLikeDeclarationOfSymbol(symbol) { + return ts.forEach(symbol.declarations, function (d) { return ts.isClassLike(d) ? d : undefined; }); } - function isReferencedAliasDeclaration(node, checkChildren) { - node = ts.getParseTreeNode(node); - // Purely synthesized nodes are always emitted. - if (node === undefined) { - return true; - } - if (ts.isAliasSymbolDeclaration(node)) { - var symbol = getSymbolOfNode(node); - if (symbol && getSymbolLinks(symbol).referenced) { - return true; + function checkKindsOfPropertyMemberOverrides(type, baseType) { + // TypeScript 1.0 spec (April 2014): 8.2.3 + // A derived class inherits all members from its base class it doesn't override. + // Inheritance means that a derived class implicitly contains all non - overridden members of the base class. + // Both public and private property members are inherited, but only public property members can be overridden. + // A property member in a derived class is said to override a property member in a base class + // when the derived class property member has the same name and kind(instance or static) + // as the base class property member. + // The type of an overriding property member must be assignable(section 3.8.4) + // to the type of the overridden property member, or otherwise a compile - time error occurs. + // Base class instance member functions can be overridden by derived class instance member functions, + // but not by other kinds of members. + // Base class instance member variables and accessors can be overridden by + // derived class instance member variables and accessors, but not by other kinds of members. + // NOTE: assignability is checked in checkClassDeclaration + var baseProperties = getPropertiesOfObjectType(baseType); + for (var _i = 0, baseProperties_1 = baseProperties; _i < baseProperties_1.length; _i++) { + var baseProperty = baseProperties_1[_i]; + var base = getTargetSymbol(baseProperty); + if (base.flags & 134217728 /* Prototype */) { + continue; } - } - if (checkChildren) { - return ts.forEachChild(node, function (node) { return isReferencedAliasDeclaration(node, checkChildren); }); - } - return false; - } - function isImplementationOfOverload(node) { - if (ts.nodeIsPresent(node.body)) { - var symbol = getSymbolOfNode(node); - var signaturesOfSymbol = getSignaturesOfSymbol(symbol); - // If this function body corresponds to function with multiple signature, it is implementation of overload - // e.g.: function foo(a: string): string; - // function foo(a: number): number; - // function foo(a: any) { // This is implementation of the overloads - // return a; - // } - return signaturesOfSymbol.length > 1 || - // If there is single signature for the symbol, it is overload if that signature isn't coming from the node - // e.g.: function foo(a: string): string; - // function foo(a: any) { // This is implementation of the overloads - // return a; - // } - (signaturesOfSymbol.length === 1 && signaturesOfSymbol[0].declaration !== node); - } - return false; - } - function getNodeCheckFlags(node) { - node = ts.getParseTreeNode(node); - return node ? getNodeLinks(node).flags : undefined; - } - function getEnumMemberValue(node) { - computeEnumMemberValues(node.parent); - return getNodeLinks(node).enumMemberValue; - } - function getConstantValue(node) { - if (node.kind === 255 /* EnumMember */) { - return getEnumMemberValue(node); - } - var symbol = getNodeLinks(node).resolvedSymbol; - if (symbol && (symbol.flags & 8 /* EnumMember */)) { - // inline property\index accesses only for const enums - if (ts.isConstEnumDeclaration(symbol.valueDeclaration.parent)) { - return getEnumMemberValue(symbol.valueDeclaration); + var derived = getTargetSymbol(getPropertyOfObjectType(type, base.name)); + var baseDeclarationFlags = getDeclarationModifierFlagsFromSymbol(base); + ts.Debug.assert(!!derived, "derived should point to something, even if it is the base class' declaration."); + if (derived) { + // In order to resolve whether the inherited method was overridden in the base class or not, + // we compare the Symbols obtained. Since getTargetSymbol returns the symbol on the *uninstantiated* + // type declaration, derived and base resolve to the same symbol even in the case of generic classes. + if (derived === base) { + // derived class inherits base without override/redeclaration + var derivedClassDecl = getClassLikeDeclarationOfSymbol(type.symbol); + // It is an error to inherit an abstract member without implementing it or being declared abstract. + // If there is no declaration for the derived class (as in the case of class expressions), + // then the class cannot be declared abstract. + if (baseDeclarationFlags & 128 /* Abstract */ && (!derivedClassDecl || !(ts.getModifierFlags(derivedClassDecl) & 128 /* Abstract */))) { + if (derivedClassDecl.kind === 192 /* ClassExpression */) { + error(derivedClassDecl, ts.Diagnostics.Non_abstract_class_expression_does_not_implement_inherited_abstract_member_0_from_class_1, symbolToString(baseProperty), typeToString(baseType)); + } + else { + error(derivedClassDecl, ts.Diagnostics.Non_abstract_class_0_does_not_implement_inherited_abstract_member_1_from_class_2, typeToString(type), symbolToString(baseProperty), typeToString(baseType)); + } + } + } + else { + // derived overrides base. + var derivedDeclarationFlags = getDeclarationModifierFlagsFromSymbol(derived); + if ((baseDeclarationFlags & 8 /* Private */) || (derivedDeclarationFlags & 8 /* Private */)) { + // either base or derived property is private - not override, skip it + continue; + } + if ((baseDeclarationFlags & 32 /* Static */) !== (derivedDeclarationFlags & 32 /* Static */)) { + // value of 'static' is not the same for properties - not override, skip it + continue; + } + if ((base.flags & derived.flags & 8192 /* Method */) || ((base.flags & 98308 /* PropertyOrAccessor */) && (derived.flags & 98308 /* PropertyOrAccessor */))) { + // method is overridden with method or property/accessor is overridden with property/accessor - correct case + continue; + } + var errorMessage = void 0; + if (base.flags & 8192 /* Method */) { + if (derived.flags & 98304 /* Accessor */) { + errorMessage = ts.Diagnostics.Class_0_defines_instance_member_function_1_but_extended_class_2_defines_it_as_instance_member_accessor; + } + else { + ts.Debug.assert((derived.flags & 4 /* Property */) !== 0); + errorMessage = ts.Diagnostics.Class_0_defines_instance_member_function_1_but_extended_class_2_defines_it_as_instance_member_property; + } + } + else if (base.flags & 4 /* Property */) { + ts.Debug.assert((derived.flags & 8192 /* Method */) !== 0); + errorMessage = ts.Diagnostics.Class_0_defines_instance_member_property_1_but_extended_class_2_defines_it_as_instance_member_function; + } + else { + ts.Debug.assert((base.flags & 98304 /* Accessor */) !== 0); + ts.Debug.assert((derived.flags & 8192 /* Method */) !== 0); + errorMessage = ts.Diagnostics.Class_0_defines_instance_member_accessor_1_but_extended_class_2_defines_it_as_instance_member_function; + } + error(derived.valueDeclaration.name, errorMessage, typeToString(baseType), symbolToString(base), typeToString(type)); + } } } - return undefined; } - function isFunctionType(type) { - return type.flags & 2588672 /* ObjectType */ && getSignaturesOfType(type, 0 /* Call */).length > 0; + function isAccessor(kind) { + return kind === 149 /* GetAccessor */ || kind === 150 /* SetAccessor */; } - function getTypeReferenceSerializationKind(typeName, location) { - // Resolve the symbol as a value to ensure the type can be reached at runtime during emit. - var valueSymbol = resolveEntityName(typeName, 107455 /* Value */, /*ignoreErrors*/ true, /*dontResolveAlias*/ false, location); - var globalPromiseSymbol = tryGetGlobalPromiseConstructorSymbol(); - if (globalPromiseSymbol && valueSymbol === globalPromiseSymbol) { - return ts.TypeReferenceSerializationKind.Promise; - } - var constructorType = valueSymbol ? getTypeOfSymbol(valueSymbol) : undefined; - if (constructorType && isConstructorType(constructorType)) { - return ts.TypeReferenceSerializationKind.TypeWithConstructSignatureAndValue; - } - // Resolve the symbol as a type so that we can provide a more useful hint for the type serializer. - var typeSymbol = resolveEntityName(typeName, 793064 /* Type */, /*ignoreErrors*/ true, /*dontResolveAlias*/ false, location); - // We might not be able to resolve type symbol so use unknown type in that case (eg error case) - if (!typeSymbol) { - return ts.TypeReferenceSerializationKind.ObjectType; - } - var type = getDeclaredTypeOfSymbol(typeSymbol); - if (type === unknownType) { - return ts.TypeReferenceSerializationKind.Unknown; - } - else if (type.flags & 1 /* Any */) { - return ts.TypeReferenceSerializationKind.ObjectType; - } - else if (isTypeOfKind(type, 1024 /* Void */ | 6144 /* Nullable */ | 8192 /* Never */)) { - return ts.TypeReferenceSerializationKind.VoidNullableOrNeverType; - } - else if (isTypeOfKind(type, 136 /* BooleanLike */)) { - return ts.TypeReferenceSerializationKind.BooleanType; - } - else if (isTypeOfKind(type, 340 /* NumberLike */)) { - return ts.TypeReferenceSerializationKind.NumberLikeType; - } - else if (isTypeOfKind(type, 34 /* StringLike */)) { - return ts.TypeReferenceSerializationKind.StringLikeType; - } - else if (isTupleType(type)) { - return ts.TypeReferenceSerializationKind.ArrayLikeType; - } - else if (isTypeOfKind(type, 512 /* ESSymbol */)) { - return ts.TypeReferenceSerializationKind.ESSymbolType; - } - else if (isFunctionType(type)) { - return ts.TypeReferenceSerializationKind.TypeWithCallSignature; + function areTypeParametersIdentical(list1, list2) { + if (!list1 && !list2) { + return true; } - else if (isArrayType(type)) { - return ts.TypeReferenceSerializationKind.ArrayLikeType; + if (!list1 || !list2 || list1.length !== list2.length) { + return false; } - else { - return ts.TypeReferenceSerializationKind.ObjectType; + // TypeScript 1.0 spec (April 2014): + // When a generic interface has multiple declarations, all declarations must have identical type parameter + // lists, i.e. identical type parameter names with identical constraints in identical order. + for (var i = 0, len = list1.length; i < len; i++) { + var tp1 = list1[i]; + var tp2 = list2[i]; + if (tp1.name.text !== tp2.name.text) { + return false; + } + if (!tp1.constraint && !tp2.constraint) { + continue; + } + if (!tp1.constraint || !tp2.constraint) { + return false; + } + if (!isTypeIdenticalTo(getTypeFromTypeNode(tp1.constraint), getTypeFromTypeNode(tp2.constraint))) { + return false; + } } + return true; } - function writeTypeOfDeclaration(declaration, enclosingDeclaration, flags, writer) { - // Get type of the symbol if this is the valid symbol otherwise get type at location - var symbol = getSymbolOfNode(declaration); - var type = symbol && !(symbol.flags & (2048 /* TypeLiteral */ | 131072 /* Signature */)) - ? getWidenedLiteralType(getTypeOfSymbol(symbol)) - : unknownType; - getSymbolDisplayBuilder().buildTypeDisplay(type, writer, enclosingDeclaration, flags); - } - function writeReturnTypeOfSignatureDeclaration(signatureDeclaration, enclosingDeclaration, flags, writer) { - var signature = getSignatureFromDeclaration(signatureDeclaration); - getSymbolDisplayBuilder().buildTypeDisplay(getReturnTypeOfSignature(signature), writer, enclosingDeclaration, flags); - } - function writeTypeOfExpression(expr, enclosingDeclaration, flags, writer) { - var type = getWidenedType(getTypeOfExpression(expr)); - getSymbolDisplayBuilder().buildTypeDisplay(type, writer, enclosingDeclaration, flags); - } - function writeBaseConstructorTypeOfClass(node, enclosingDeclaration, flags, writer) { - var classType = getDeclaredTypeOfSymbol(getSymbolOfNode(node)); - resolveBaseTypesOfClass(classType); - var baseType = classType.resolvedBaseTypes.length ? classType.resolvedBaseTypes[0] : unknownType; - getSymbolDisplayBuilder().buildTypeDisplay(baseType, writer, enclosingDeclaration, flags); - } - function hasGlobalName(name) { - return !!globals[name]; - } - function getReferencedValueSymbol(reference, startInDeclarationContainer) { - var resolvedSymbol = getNodeLinks(reference).resolvedSymbol; - if (resolvedSymbol) { - return resolvedSymbol; + function checkInheritedPropertiesAreIdentical(type, typeNode) { + var baseTypes = getBaseTypes(type); + if (baseTypes.length < 2) { + return true; } - var location = reference; - if (startInDeclarationContainer) { - // When resolving the name of a declaration as a value, we need to start resolution - // at a point outside of the declaration. - var parent_13 = reference.parent; - if (ts.isDeclaration(parent_13) && reference === parent_13.name) { - location = getDeclarationContainer(parent_13); + var seen = ts.createMap(); + ts.forEach(resolveDeclaredMembers(type).declaredProperties, function (p) { seen[p.name] = { prop: p, containingType: type }; }); + var ok = true; + for (var _i = 0, baseTypes_2 = baseTypes; _i < baseTypes_2.length; _i++) { + var base = baseTypes_2[_i]; + var properties = getPropertiesOfObjectType(getTypeWithThisArgument(base, type.thisType)); + for (var _a = 0, properties_5 = properties; _a < properties_5.length; _a++) { + var prop = properties_5[_a]; + var existing = seen[prop.name]; + if (!existing) { + seen[prop.name] = { prop: prop, containingType: base }; + } + else { + var isInheritedProperty = existing.containingType !== type; + if (isInheritedProperty && !isPropertyIdenticalTo(existing.prop, prop)) { + ok = false; + var typeName1 = typeToString(existing.containingType); + var typeName2 = typeToString(base); + var errorInfo = ts.chainDiagnosticMessages(undefined, ts.Diagnostics.Named_property_0_of_types_1_and_2_are_not_identical, symbolToString(prop), typeName1, typeName2); + errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Interface_0_cannot_simultaneously_extend_types_1_and_2, typeToString(type), typeName1, typeName2); + diagnostics.add(ts.createDiagnosticForNodeFromMessageChain(typeNode, errorInfo)); + } + } } } - return resolveName(location, reference.text, 107455 /* Value */ | 1048576 /* ExportValue */ | 8388608 /* Alias */, /*nodeNotFoundMessage*/ undefined, /*nameArg*/ undefined); + return ok; } - function getReferencedValueDeclaration(reference) { - if (!ts.isGeneratedIdentifier(reference)) { - reference = ts.getParseTreeNode(reference, ts.isIdentifier); - if (reference) { - var symbol = getReferencedValueSymbol(reference); - if (symbol) { - return getExportSymbolOfValueSymbolIfExported(symbol).valueDeclaration; + function checkInterfaceDeclaration(node) { + // Grammar checking + checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarInterfaceDeclaration(node); + checkTypeParameters(node.typeParameters); + if (produceDiagnostics) { + checkTypeNameIsReserved(node.name, ts.Diagnostics.Interface_name_cannot_be_0); + checkExportsOnMergedDeclarations(node); + var symbol = getSymbolOfNode(node); + checkTypeParameterListsIdentical(node, symbol); + // Only check this symbol once + var firstInterfaceDecl = ts.getDeclarationOfKind(symbol, 222 /* InterfaceDeclaration */); + if (node === firstInterfaceDecl) { + var type = getDeclaredTypeOfSymbol(symbol); + var typeWithThis = getTypeWithThisArgument(type); + // run subsequent checks only if first set succeeded + if (checkInheritedPropertiesAreIdentical(type, node.name)) { + for (var _i = 0, _a = getBaseTypes(type); _i < _a.length; _i++) { + var baseType = _a[_i]; + checkTypeAssignableTo(typeWithThis, getTypeWithThisArgument(baseType, type.thisType), node.name, ts.Diagnostics.Interface_0_incorrectly_extends_interface_1); + } + checkIndexConstraints(type); } } + checkObjectTypeForDuplicateDeclarations(node); } - return undefined; - } - function isLiteralConstDeclaration(node) { - if (ts.isConst(node)) { - var type = getTypeOfSymbol(getSymbolOfNode(node)); - return !!(type.flags & 96 /* StringOrNumberLiteral */ && type.flags & 16777216 /* FreshLiteral */); + ts.forEach(ts.getInterfaceBaseTypeNodes(node), function (heritageElement) { + if (!ts.isEntityNameExpression(heritageElement.expression)) { + error(heritageElement.expression, ts.Diagnostics.An_interface_can_only_extend_an_identifier_Slashqualified_name_with_optional_type_arguments); + } + checkTypeReferenceNode(heritageElement); + }); + ts.forEach(node.members, checkSourceElement); + if (produceDiagnostics) { + checkTypeForDuplicateIndexSignatures(node); + registerForUnusedIdentifiersCheck(node); } - return false; } - function writeLiteralConstValue(node, writer) { - var type = getTypeOfSymbol(getSymbolOfNode(node)); - writer.writeStringLiteral(literalTypeToString(type)); + function checkTypeAliasDeclaration(node) { + // Grammar checking + checkGrammarDecorators(node) || checkGrammarModifiers(node); + checkTypeNameIsReserved(node.name, ts.Diagnostics.Type_alias_name_cannot_be_0); + checkSourceElement(node.type); } - function createResolver() { - // this variable and functions that use it are deliberately moved here from the outer scope - // to avoid scope pollution - var resolvedTypeReferenceDirectives = host.getResolvedTypeReferenceDirectives(); - var fileToDirective; - if (resolvedTypeReferenceDirectives) { - // populate reverse mapping: file path -> type reference directive that was resolved to this file - fileToDirective = ts.createFileMap(); - for (var key in resolvedTypeReferenceDirectives) { - var resolvedDirective = resolvedTypeReferenceDirectives[key]; - if (!resolvedDirective) { - continue; + function computeEnumMemberValues(node) { + var nodeLinks = getNodeLinks(node); + if (!(nodeLinks.flags & 16384 /* EnumValuesComputed */)) { + var enumSymbol = getSymbolOfNode(node); + var enumType = getDeclaredTypeOfSymbol(enumSymbol); + var autoValue = 0; // set to undefined when enum member is non-constant + var ambient = ts.isInAmbientContext(node); + var enumIsConst = ts.isConst(node); + for (var _i = 0, _a = node.members; _i < _a.length; _i++) { + var member = _a[_i]; + if (isComputedNonLiteralName(member.name)) { + error(member.name, ts.Diagnostics.Computed_property_names_are_not_allowed_in_enums); } - var file = host.getSourceFile(resolvedDirective.resolvedFileName); - fileToDirective.set(file.path, key); - } - } - return { - getReferencedExportContainer: getReferencedExportContainer, - getReferencedImportDeclaration: getReferencedImportDeclaration, - getReferencedDeclarationWithCollidingName: getReferencedDeclarationWithCollidingName, - isDeclarationWithCollidingName: isDeclarationWithCollidingName, - isValueAliasDeclaration: isValueAliasDeclaration, - hasGlobalName: hasGlobalName, - isReferencedAliasDeclaration: isReferencedAliasDeclaration, - getNodeCheckFlags: getNodeCheckFlags, - isTopLevelValueImportEqualsWithEntityName: isTopLevelValueImportEqualsWithEntityName, - isDeclarationVisible: isDeclarationVisible, - isImplementationOfOverload: isImplementationOfOverload, - writeTypeOfDeclaration: writeTypeOfDeclaration, - writeReturnTypeOfSignatureDeclaration: writeReturnTypeOfSignatureDeclaration, - writeTypeOfExpression: writeTypeOfExpression, - writeBaseConstructorTypeOfClass: writeBaseConstructorTypeOfClass, - isSymbolAccessible: isSymbolAccessible, - isEntityNameVisible: isEntityNameVisible, - getConstantValue: getConstantValue, - collectLinkedAliases: collectLinkedAliases, - getReferencedValueDeclaration: getReferencedValueDeclaration, - getTypeReferenceSerializationKind: getTypeReferenceSerializationKind, - isOptionalParameter: isOptionalParameter, - moduleExportsSomeValue: moduleExportsSomeValue, - isArgumentsLocalBinding: isArgumentsLocalBinding, - getExternalModuleFileFromDeclaration: getExternalModuleFileFromDeclaration, - getTypeReferenceDirectivesForEntityName: getTypeReferenceDirectivesForEntityName, - getTypeReferenceDirectivesForSymbol: getTypeReferenceDirectivesForSymbol, - isLiteralConstDeclaration: isLiteralConstDeclaration, - writeLiteralConstValue: writeLiteralConstValue - }; - // defined here to avoid outer scope pollution - function getTypeReferenceDirectivesForEntityName(node) { - // program does not have any files with type reference directives - bail out - if (!fileToDirective) { - return undefined; - } - // property access can only be used as values - // qualified names can only be used as types\namespaces - // identifiers are treated as values only if they appear in type queries - var meaning = (node.kind === 172 /* PropertyAccessExpression */) || (node.kind === 69 /* Identifier */ && isInTypeQuery(node)) - ? 107455 /* Value */ | 1048576 /* ExportValue */ - : 793064 /* Type */ | 1920 /* Namespace */; - var symbol = resolveEntityName(node, meaning, /*ignoreErrors*/ true); - return symbol && symbol !== unknownSymbol ? getTypeReferenceDirectivesForSymbol(symbol, meaning) : undefined; - } - // defined here to avoid outer scope pollution - function getTypeReferenceDirectivesForSymbol(symbol, meaning) { - // program does not have any files with type reference directives - bail out - if (!fileToDirective) { - return undefined; - } - if (!isSymbolFromTypeDeclarationFile(symbol)) { - return undefined; - } - // check what declarations in the symbol can contribute to the target meaning - var typeReferenceDirectives; - for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { - var decl = _a[_i]; - // check meaning of the local symbol to see if declaration needs to be analyzed further - if (decl.symbol && decl.symbol.flags & meaning) { - var file = ts.getSourceFileOfNode(decl); - var typeReferenceDirective = fileToDirective.get(file.path); - if (typeReferenceDirective) { - (typeReferenceDirectives || (typeReferenceDirectives = [])).push(typeReferenceDirective); + else { + var text = getTextOfPropertyName(member.name); + if (isNumericLiteralName(text) && !isInfinityOrNaNString(text)) { + error(member.name, ts.Diagnostics.An_enum_member_cannot_have_a_numeric_name); } } - } - return typeReferenceDirectives; - } - function isSymbolFromTypeDeclarationFile(symbol) { - // bail out if symbol does not have associated declarations (i.e. this is transient symbol created for property in binding pattern) - if (!symbol.declarations) { - return false; - } - // walk the parent chain for symbols to make sure that top level parent symbol is in the global scope - // external modules cannot define or contribute to type declaration files - var current = symbol; - while (true) { - var parent_14 = getParentOfSymbol(current); - if (parent_14) { - current = parent_14; + var previousEnumMemberIsNonConstant = autoValue === undefined; + var initializer = member.initializer; + if (initializer) { + autoValue = computeConstantValueForEnumMemberInitializer(initializer, enumType, enumIsConst, ambient); } - else { - break; + else if (ambient && !enumIsConst) { + // In ambient enum declarations that specify no const modifier, enum member declarations + // that omit a value are considered computed members (as opposed to having auto-incremented values assigned). + autoValue = undefined; } - } - if (current.valueDeclaration && current.valueDeclaration.kind === 256 /* SourceFile */ && current.flags & 512 /* ValueModule */) { - return false; - } - // check that at least one declaration of top level symbol originates from type declaration file - for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { - var decl = _a[_i]; - var file = ts.getSourceFileOfNode(decl); - if (fileToDirective.contains(file.path)) { - return true; + else if (previousEnumMemberIsNonConstant) { + // If the member declaration specifies no value, the member is considered a constant enum member. + // If the member is the first member in the enum declaration, it is assigned the value zero. + // Otherwise, it is assigned the value of the immediately preceding member plus one, + // and an error occurs if the immediately preceding member is not a constant enum member + error(member.name, ts.Diagnostics.Enum_member_must_have_initializer); + } + if (autoValue !== undefined) { + getNodeLinks(member).enumMemberValue = autoValue; + autoValue++; } } - return false; - } - } - function getExternalModuleFileFromDeclaration(declaration) { - var specifier = ts.getExternalModuleName(declaration); - var moduleSymbol = resolveExternalModuleNameWorker(specifier, specifier, /*moduleNotFoundError*/ undefined); - if (!moduleSymbol) { - return undefined; - } - return ts.getDeclarationOfKind(moduleSymbol, 256 /* SourceFile */); - } - function initializeTypeChecker() { - // Bind all source files and propagate errors - for (var _i = 0, _a = host.getSourceFiles(); _i < _a.length; _i++) { - var file = _a[_i]; - ts.bindSourceFile(file, compilerOptions); + nodeLinks.flags |= 16384 /* EnumValuesComputed */; } - // Initialize global symbol table - var augmentations; - var requestedExternalEmitHelpers = 0; - var firstFileRequestingExternalHelpers; - for (var _b = 0, _c = host.getSourceFiles(); _b < _c.length; _b++) { - var file = _c[_b]; - if (!ts.isExternalOrCommonJsModule(file)) { - mergeSymbolTable(globals, file.locals); - } - if (file.patternAmbientModules && file.patternAmbientModules.length) { - patternAmbientModules = ts.concatenate(patternAmbientModules, file.patternAmbientModules); - } - if (file.moduleAugmentations.length) { - (augmentations || (augmentations = [])).push(file.moduleAugmentations); - } - if (file.symbol && file.symbol.globalExports) { - // Merge in UMD exports with first-in-wins semantics (see #9771) - var source = file.symbol.globalExports; - for (var id in source) { - if (!(id in globals)) { - globals[id] = source[id]; + function computeConstantValueForEnumMemberInitializer(initializer, enumType, enumIsConst, ambient) { + // Controls if error should be reported after evaluation of constant value is completed + // Can be false if another more precise error was already reported during evaluation. + var reportError = true; + var value = evalConstant(initializer); + if (reportError) { + if (value === undefined) { + if (enumIsConst) { + error(initializer, ts.Diagnostics.In_const_enum_declarations_member_initializer_must_be_constant_expression); + } + else if (ambient) { + error(initializer, ts.Diagnostics.In_ambient_enum_declarations_member_initializer_must_be_constant_expression); + } + else { + // Only here do we need to check that the initializer is assignable to the enum type. + checkTypeAssignableTo(checkExpression(initializer), enumType, initializer, /*headMessage*/ undefined); } } - } - if ((compilerOptions.isolatedModules || ts.isExternalModule(file)) && !file.isDeclarationFile) { - var fileRequestedExternalEmitHelpers = file.flags & 31744 /* EmitHelperFlags */; - if (fileRequestedExternalEmitHelpers) { - requestedExternalEmitHelpers |= fileRequestedExternalEmitHelpers; - if (firstFileRequestingExternalHelpers === undefined) { - firstFileRequestingExternalHelpers = file; + else if (enumIsConst) { + if (isNaN(value)) { + error(initializer, ts.Diagnostics.const_enum_member_initializer_was_evaluated_to_disallowed_value_NaN); + } + else if (!isFinite(value)) { + error(initializer, ts.Diagnostics.const_enum_member_initializer_was_evaluated_to_a_non_finite_value); } } } - } - if (augmentations) { - // merge module augmentations. - // this needs to be done after global symbol table is initialized to make sure that all ambient modules are indexed - for (var _d = 0, augmentations_1 = augmentations; _d < augmentations_1.length; _d++) { - var list = augmentations_1[_d]; - for (var _e = 0, list_1 = list; _e < list_1.length; _e++) { - var augmentation = list_1[_e]; - mergeModuleAugmentation(augmentation); + return value; + function evalConstant(e) { + switch (e.kind) { + case 185 /* PrefixUnaryExpression */: + var value_1 = evalConstant(e.operand); + if (value_1 === undefined) { + return undefined; + } + switch (e.operator) { + case 35 /* PlusToken */: return value_1; + case 36 /* MinusToken */: return -value_1; + case 50 /* TildeToken */: return ~value_1; + } + return undefined; + case 187 /* BinaryExpression */: + var left = evalConstant(e.left); + if (left === undefined) { + return undefined; + } + var right = evalConstant(e.right); + if (right === undefined) { + return undefined; + } + switch (e.operatorToken.kind) { + case 47 /* BarToken */: return left | right; + case 46 /* AmpersandToken */: return left & right; + case 44 /* GreaterThanGreaterThanToken */: return left >> right; + case 45 /* GreaterThanGreaterThanGreaterThanToken */: return left >>> right; + case 43 /* LessThanLessThanToken */: return left << right; + case 48 /* CaretToken */: return left ^ right; + case 37 /* AsteriskToken */: return left * right; + case 39 /* SlashToken */: return left / right; + case 35 /* PlusToken */: return left + right; + case 36 /* MinusToken */: return left - right; + case 40 /* PercentToken */: return left % right; + } + return undefined; + case 8 /* NumericLiteral */: + return +e.text; + case 178 /* ParenthesizedExpression */: + return evalConstant(e.expression); + case 69 /* Identifier */: + case 173 /* ElementAccessExpression */: + case 172 /* PropertyAccessExpression */: + var member = initializer.parent; + var currentType = getTypeOfSymbol(getSymbolOfNode(member.parent)); + var enumType_1; + var propertyName = void 0; + if (e.kind === 69 /* Identifier */) { + // unqualified names can refer to member that reside in different declaration of the enum so just doing name resolution won't work. + // instead pick current enum type and later try to fetch member from the type + enumType_1 = currentType; + propertyName = e.text; + } + else { + var expression = void 0; + if (e.kind === 173 /* ElementAccessExpression */) { + if (e.argumentExpression === undefined || + e.argumentExpression.kind !== 9 /* StringLiteral */) { + return undefined; + } + expression = e.expression; + propertyName = e.argumentExpression.text; + } + else { + expression = e.expression; + propertyName = e.name.text; + } + // expression part in ElementAccess\PropertyAccess should be either identifier or dottedName + var current = expression; + while (current) { + if (current.kind === 69 /* Identifier */) { + break; + } + else if (current.kind === 172 /* PropertyAccessExpression */) { + current = current.expression; + } + else { + return undefined; + } + } + enumType_1 = checkExpression(expression); + // allow references to constant members of other enums + if (!(enumType_1.symbol && (enumType_1.symbol.flags & 384 /* Enum */))) { + return undefined; + } + } + if (propertyName === undefined) { + return undefined; + } + var property = getPropertyOfObjectType(enumType_1, propertyName); + if (!property || !(property.flags & 8 /* EnumMember */)) { + return undefined; + } + var propertyDecl = property.valueDeclaration; + // self references are illegal + if (member === propertyDecl) { + return undefined; + } + // illegal case: forward reference + if (!isBlockScopedNameDeclaredBeforeUse(propertyDecl, member)) { + reportError = false; + error(e, ts.Diagnostics.A_member_initializer_in_a_enum_declaration_cannot_reference_members_declared_after_it_including_members_defined_in_other_enums); + return undefined; + } + return getNodeLinks(propertyDecl).enumMemberValue; } } } - // Setup global builtins - addToSymbolTable(globals, builtinGlobals, ts.Diagnostics.Declaration_name_conflicts_with_built_in_global_identifier_0); - getSymbolLinks(undefinedSymbol).type = undefinedWideningType; - getSymbolLinks(argumentsSymbol).type = getGlobalType("IArguments"); - getSymbolLinks(unknownSymbol).type = unknownType; - // Initialize special types - globalArrayType = getGlobalType("Array", /*arity*/ 1); - globalObjectType = getGlobalType("Object"); - globalFunctionType = getGlobalType("Function"); - globalStringType = getGlobalType("String"); - globalNumberType = getGlobalType("Number"); - globalBooleanType = getGlobalType("Boolean"); - globalRegExpType = getGlobalType("RegExp"); - jsxElementType = getExportedTypeFromNamespace("JSX", JsxNames.Element); - getGlobalClassDecoratorType = ts.memoize(function () { return getGlobalType("ClassDecorator"); }); - getGlobalPropertyDecoratorType = ts.memoize(function () { return getGlobalType("PropertyDecorator"); }); - getGlobalMethodDecoratorType = ts.memoize(function () { return getGlobalType("MethodDecorator"); }); - getGlobalParameterDecoratorType = ts.memoize(function () { return getGlobalType("ParameterDecorator"); }); - getGlobalTypedPropertyDescriptorType = ts.memoize(function () { return getGlobalType("TypedPropertyDescriptor", /*arity*/ 1); }); - getGlobalESSymbolConstructorSymbol = ts.memoize(function () { return getGlobalValueSymbol("Symbol"); }); - getGlobalPromiseType = ts.memoize(function () { return getGlobalType("Promise", /*arity*/ 1); }); - tryGetGlobalPromiseType = ts.memoize(function () { return getGlobalSymbol("Promise", 793064 /* Type */, /*diagnostic*/ undefined) && getGlobalPromiseType(); }); - getGlobalPromiseLikeType = ts.memoize(function () { return getGlobalType("PromiseLike", /*arity*/ 1); }); - getInstantiatedGlobalPromiseLikeType = ts.memoize(createInstantiatedPromiseLikeType); - getGlobalPromiseConstructorSymbol = ts.memoize(function () { return getGlobalValueSymbol("Promise"); }); - tryGetGlobalPromiseConstructorSymbol = ts.memoize(function () { return getGlobalSymbol("Promise", 107455 /* Value */, /*diagnostic*/ undefined) && getGlobalPromiseConstructorSymbol(); }); - getGlobalPromiseConstructorLikeType = ts.memoize(function () { return getGlobalType("PromiseConstructorLike"); }); - getGlobalThenableType = ts.memoize(createThenableType); - getGlobalTemplateStringsArrayType = ts.memoize(function () { return getGlobalType("TemplateStringsArray"); }); - if (languageVersion >= 2 /* ES6 */) { - getGlobalESSymbolType = ts.memoize(function () { return getGlobalType("Symbol"); }); - getGlobalIterableType = ts.memoize(function () { return getGlobalType("Iterable", /*arity*/ 1); }); - getGlobalIteratorType = ts.memoize(function () { return getGlobalType("Iterator", /*arity*/ 1); }); - getGlobalIterableIteratorType = ts.memoize(function () { return getGlobalType("IterableIterator", /*arity*/ 1); }); + } + function checkEnumDeclaration(node) { + if (!produceDiagnostics) { + return; } - else { - getGlobalESSymbolType = ts.memoize(function () { return emptyObjectType; }); - getGlobalIterableType = ts.memoize(function () { return emptyGenericType; }); - getGlobalIteratorType = ts.memoize(function () { return emptyGenericType; }); - getGlobalIterableIteratorType = ts.memoize(function () { return emptyGenericType; }); + // Grammar checking + checkGrammarDecorators(node) || checkGrammarModifiers(node); + checkTypeNameIsReserved(node.name, ts.Diagnostics.Enum_name_cannot_be_0); + checkCollisionWithCapturedThisVariable(node, node.name); + checkCollisionWithRequireExportsInGeneratedCode(node, node.name); + checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); + checkExportsOnMergedDeclarations(node); + computeEnumMemberValues(node); + var enumIsConst = ts.isConst(node); + if (compilerOptions.isolatedModules && enumIsConst && ts.isInAmbientContext(node)) { + error(node.name, ts.Diagnostics.Ambient_const_enums_are_not_allowed_when_the_isolatedModules_flag_is_provided); } - anyArrayType = createArrayType(anyType); - var symbol = getGlobalSymbol("ReadonlyArray", 793064 /* Type */, /*diagnostic*/ undefined); - globalReadonlyArrayType = symbol && getTypeOfGlobalSymbol(symbol, /*arity*/ 1); - anyReadonlyArrayType = globalReadonlyArrayType ? createTypeFromGenericGlobalType(globalReadonlyArrayType, [anyType]) : anyArrayType; - // If we have specified that we are importing helpers, we should report global - // errors if we cannot resolve the helpers external module, or if it does not have - // the necessary helpers exported. - if (compilerOptions.importHelpers && firstFileRequestingExternalHelpers) { - // Find the first reference to the helpers module. - var helpersModule = resolveExternalModule(firstFileRequestingExternalHelpers, ts.externalHelpersModuleNameText, ts.Diagnostics.Cannot_find_module_0, - /*errorNode*/ undefined); - // If we found the module, report errors if it does not have the necessary exports. - if (helpersModule) { - var exports = helpersModule.exports; - if (requestedExternalEmitHelpers & 1024 /* HasClassExtends */ && languageVersion < 2 /* ES6 */) { - verifyHelperSymbol(exports, "__extends", 107455 /* Value */); - } - if (requestedExternalEmitHelpers & 16384 /* HasJsxSpreadAttributes */ && compilerOptions.jsx !== 1 /* Preserve */) { - verifyHelperSymbol(exports, "__assign", 107455 /* Value */); - } - if (requestedExternalEmitHelpers & 2048 /* HasDecorators */) { - verifyHelperSymbol(exports, "__decorate", 107455 /* Value */); - if (compilerOptions.emitDecoratorMetadata) { - verifyHelperSymbol(exports, "__metadata", 107455 /* Value */); + // Spec 2014 - Section 9.3: + // It isn't possible for one enum declaration to continue the automatic numbering sequence of another, + // and when an enum type has multiple declarations, only one declaration is permitted to omit a value + // for the first member. + // + // Only perform this check once per symbol + var enumSymbol = getSymbolOfNode(node); + var firstDeclaration = ts.getDeclarationOfKind(enumSymbol, node.kind); + if (node === firstDeclaration) { + if (enumSymbol.declarations.length > 1) { + // check that const is placed\omitted on all enum declarations + ts.forEach(enumSymbol.declarations, function (decl) { + if (ts.isConstEnumDeclaration(decl) !== enumIsConst) { + error(decl.name, ts.Diagnostics.Enum_declarations_must_all_be_const_or_non_const); } + }); + } + var seenEnumMissingInitialInitializer_1 = false; + ts.forEach(enumSymbol.declarations, function (declaration) { + // return true if we hit a violation of the rule, false otherwise + if (declaration.kind !== 224 /* EnumDeclaration */) { + return false; } - if (requestedExternalEmitHelpers & 4096 /* HasParamDecorators */) { - verifyHelperSymbol(exports, "__param", 107455 /* Value */); + var enumDeclaration = declaration; + if (!enumDeclaration.members.length) { + return false; } - if (requestedExternalEmitHelpers & 8192 /* HasAsyncFunctions */) { - verifyHelperSymbol(exports, "__awaiter", 107455 /* Value */); - if (languageVersion < 2 /* ES6 */) { - verifyHelperSymbol(exports, "__generator", 107455 /* Value */); + var firstEnumMember = enumDeclaration.members[0]; + if (!firstEnumMember.initializer) { + if (seenEnumMissingInitialInitializer_1) { + error(firstEnumMember.name, ts.Diagnostics.In_an_enum_with_multiple_declarations_only_one_declaration_can_omit_an_initializer_for_its_first_enum_element); + } + else { + seenEnumMissingInitialInitializer_1 = true; } } - } + }); } } - function verifyHelperSymbol(symbols, name, meaning) { - var symbol = getSymbol(symbols, ts.escapeIdentifier(name), meaning); - if (!symbol) { - error(/*location*/ undefined, ts.Diagnostics.Module_0_has_no_exported_member_1, ts.externalHelpersModuleNameText, name); + function getFirstNonAmbientClassOrFunctionDeclaration(symbol) { + var declarations = symbol.declarations; + for (var _i = 0, declarations_5 = declarations; _i < declarations_5.length; _i++) { + var declaration = declarations_5[_i]; + if ((declaration.kind === 221 /* ClassDeclaration */ || + (declaration.kind === 220 /* FunctionDeclaration */ && ts.nodeIsPresent(declaration.body))) && + !ts.isInAmbientContext(declaration)) { + return declaration; + } } + return undefined; } - function createInstantiatedPromiseLikeType() { - var promiseLikeType = getGlobalPromiseLikeType(); - if (promiseLikeType !== emptyGenericType) { - return createTypeReference(promiseLikeType, [anyType]); + function inSameLexicalScope(node1, node2) { + var container1 = ts.getEnclosingBlockScopeContainer(node1); + var container2 = ts.getEnclosingBlockScopeContainer(node2); + if (isGlobalSourceFile(container1)) { + return isGlobalSourceFile(container2); } - return emptyObjectType; - } - function createThenableType() { - // build the thenable type that is used to verify against a non-promise "thenable" operand to `await`. - var thenPropertySymbol = createSymbol(67108864 /* Transient */ | 4 /* Property */, "then"); - getSymbolLinks(thenPropertySymbol).type = globalFunctionType; - var thenableType = createObjectType(2097152 /* Anonymous */); - thenableType.properties = [thenPropertySymbol]; - thenableType.members = createSymbolTable(thenableType.properties); - thenableType.callSignatures = []; - thenableType.constructSignatures = []; - return thenableType; - } - // GRAMMAR CHECKING - function checkGrammarDecorators(node) { - if (!node.decorators) { + else if (isGlobalSourceFile(container2)) { return false; } - if (!ts.nodeCanBeDecorated(node)) { - if (node.kind === 147 /* MethodDeclaration */ && !ts.nodeIsPresent(node.body)) { - return grammarErrorOnFirstToken(node, ts.Diagnostics.A_decorator_can_only_decorate_a_method_implementation_not_an_overload); - } - else { - return grammarErrorOnFirstToken(node, ts.Diagnostics.Decorators_are_not_valid_here); - } - } - else if (node.kind === 149 /* GetAccessor */ || node.kind === 150 /* SetAccessor */) { - var accessors = ts.getAllAccessorDeclarations(node.parent.members, node); - if (accessors.firstAccessor.decorators && node === accessors.secondAccessor) { - return grammarErrorOnFirstToken(node, ts.Diagnostics.Decorators_cannot_be_applied_to_multiple_get_Slashset_accessors_of_the_same_name); - } + else { + return container1 === container2; } - return false; } - function checkGrammarModifiers(node) { - var quickResult = reportObviousModifierErrors(node); - if (quickResult !== undefined) { - return quickResult; - } - var lastStatic, lastPrivate, lastProtected, lastDeclare, lastAsync, lastReadonly; - var flags = 0 /* None */; - for (var _i = 0, _a = node.modifiers; _i < _a.length; _i++) { - var modifier = _a[_i]; - if (modifier.kind !== 128 /* ReadonlyKeyword */) { - if (node.kind === 144 /* PropertySignature */ || node.kind === 146 /* MethodSignature */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_type_member, ts.tokenToString(modifier.kind)); - } - if (node.kind === 153 /* IndexSignature */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_an_index_signature, ts.tokenToString(modifier.kind)); + function checkModuleDeclaration(node) { + if (produceDiagnostics) { + // Grammar checking + var isGlobalAugmentation = ts.isGlobalScopeAugmentation(node); + var inAmbientContext = ts.isInAmbientContext(node); + if (isGlobalAugmentation && !inAmbientContext) { + error(node.name, ts.Diagnostics.Augmentations_for_the_global_scope_should_have_declare_modifier_unless_they_appear_in_already_ambient_context); + } + var isAmbientExternalModule = ts.isAmbientModule(node); + var contextErrorMessage = isAmbientExternalModule + ? ts.Diagnostics.An_ambient_module_declaration_is_only_allowed_at_the_top_level_in_a_file + : ts.Diagnostics.A_namespace_declaration_is_only_allowed_in_a_namespace_or_module; + if (checkGrammarModuleElementContext(node, contextErrorMessage)) { + // If we hit a module declaration in an illegal context, just bail out to avoid cascading errors. + return; + } + if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node)) { + if (!inAmbientContext && node.name.kind === 9 /* StringLiteral */) { + grammarErrorOnNode(node.name, ts.Diagnostics.Only_ambient_modules_can_use_quoted_names); } } - switch (modifier.kind) { - case 74 /* ConstKeyword */: - if (node.kind !== 224 /* EnumDeclaration */ && node.parent.kind === 221 /* ClassDeclaration */) { - return grammarErrorOnNode(node, ts.Diagnostics.A_class_member_cannot_have_the_0_keyword, ts.tokenToString(74 /* ConstKeyword */)); - } - break; - case 112 /* PublicKeyword */: - case 111 /* ProtectedKeyword */: - case 110 /* PrivateKeyword */: - var text = visibilityToString(ts.modifierToFlag(modifier.kind)); - if (modifier.kind === 111 /* ProtectedKeyword */) { - lastProtected = modifier; - } - else if (modifier.kind === 110 /* PrivateKeyword */) { - lastPrivate = modifier; - } - if (flags & 28 /* AccessibilityModifier */) { - return grammarErrorOnNode(modifier, ts.Diagnostics.Accessibility_modifier_already_seen); - } - else if (flags & 32 /* Static */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, text, "static"); - } - else if (flags & 64 /* Readonly */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, text, "readonly"); - } - else if (flags & 256 /* Async */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, text, "async"); + if (ts.isIdentifier(node.name)) { + checkCollisionWithCapturedThisVariable(node, node.name); + checkCollisionWithRequireExportsInGeneratedCode(node, node.name); + checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); + } + checkExportsOnMergedDeclarations(node); + var symbol = getSymbolOfNode(node); + // The following checks only apply on a non-ambient instantiated module declaration. + if (symbol.flags & 512 /* ValueModule */ + && symbol.declarations.length > 1 + && !inAmbientContext + && ts.isInstantiatedModule(node, compilerOptions.preserveConstEnums || compilerOptions.isolatedModules)) { + var firstNonAmbientClassOrFunc = getFirstNonAmbientClassOrFunctionDeclaration(symbol); + if (firstNonAmbientClassOrFunc) { + if (ts.getSourceFileOfNode(node) !== ts.getSourceFileOfNode(firstNonAmbientClassOrFunc)) { + error(node.name, ts.Diagnostics.A_namespace_declaration_cannot_be_in_a_different_file_from_a_class_or_function_with_which_it_is_merged); } - else if (node.parent.kind === 226 /* ModuleBlock */ || node.parent.kind === 256 /* SourceFile */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_module_or_namespace_element, text); + else if (node.pos < firstNonAmbientClassOrFunc.pos) { + error(node.name, ts.Diagnostics.A_namespace_declaration_cannot_be_located_prior_to_a_class_or_function_with_which_it_is_merged); } - else if (flags & 128 /* Abstract */) { - if (modifier.kind === 110 /* PrivateKeyword */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_be_used_with_1_modifier, text, "abstract"); - } - else { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, text, "abstract"); + } + // if the module merges with a class declaration in the same lexical scope, + // we need to track this to ensure the correct emit. + var mergedClass = ts.getDeclarationOfKind(symbol, 221 /* ClassDeclaration */); + if (mergedClass && + inSameLexicalScope(node, mergedClass)) { + getNodeLinks(node).flags |= 32768 /* LexicalModuleMergesWithClass */; + } + } + if (isAmbientExternalModule) { + if (ts.isExternalModuleAugmentation(node)) { + // body of the augmentation should be checked for consistency only if augmentation was applied to its target (either global scope or module) + // otherwise we'll be swamped in cascading errors. + // We can detect if augmentation was applied using following rules: + // - augmentation for a global scope is always applied + // - augmentation for some external module is applied if symbol for augmentation is merged (it was combined with target module). + var checkBody = isGlobalAugmentation || (getSymbolOfNode(node).flags & 33554432 /* Merged */); + if (checkBody && node.body) { + // body of ambient external module is always a module block + for (var _i = 0, _a = node.body.statements; _i < _a.length; _i++) { + var statement = _a[_i]; + checkModuleAugmentationElement(statement, isGlobalAugmentation); } } - flags |= ts.modifierToFlag(modifier.kind); - break; - case 113 /* StaticKeyword */: - if (flags & 32 /* Static */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_already_seen, "static"); - } - else if (flags & 64 /* Readonly */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, "static", "readonly"); + } + else if (isGlobalSourceFile(node.parent)) { + if (isGlobalAugmentation) { + error(node.name, ts.Diagnostics.Augmentations_for_the_global_scope_can_only_be_directly_nested_in_external_modules_or_ambient_module_declarations); } - else if (flags & 256 /* Async */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, "static", "async"); + else if (ts.isExternalModuleNameRelative(node.name.text)) { + error(node.name, ts.Diagnostics.Ambient_module_declaration_cannot_specify_relative_module_name); } - else if (node.parent.kind === 226 /* ModuleBlock */ || node.parent.kind === 256 /* SourceFile */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_module_or_namespace_element, "static"); + } + else { + if (isGlobalAugmentation) { + error(node.name, ts.Diagnostics.Augmentations_for_the_global_scope_can_only_be_directly_nested_in_external_modules_or_ambient_module_declarations); } - else if (node.kind === 142 /* Parameter */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_parameter, "static"); + else { + // Node is not an augmentation and is not located on the script level. + // This means that this is declaration of ambient module that is located in other module or namespace which is prohibited. + error(node.name, ts.Diagnostics.Ambient_modules_cannot_be_nested_in_other_modules_or_namespaces); } - else if (flags & 128 /* Abstract */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "static", "abstract"); + } + } + } + if (node.body) { + checkSourceElement(node.body); + if (!ts.isGlobalScopeAugmentation(node)) { + registerForUnusedIdentifiersCheck(node); + } + } + } + function checkModuleAugmentationElement(node, isGlobalAugmentation) { + switch (node.kind) { + case 200 /* VariableStatement */: + // error each individual name in variable statement instead of marking the entire variable statement + for (var _i = 0, _a = node.declarationList.declarations; _i < _a.length; _i++) { + var decl = _a[_i]; + checkModuleAugmentationElement(decl, isGlobalAugmentation); + } + break; + case 235 /* ExportAssignment */: + case 236 /* ExportDeclaration */: + grammarErrorOnFirstToken(node, ts.Diagnostics.Exports_and_export_assignments_are_not_permitted_in_module_augmentations); + break; + case 229 /* ImportEqualsDeclaration */: + case 230 /* ImportDeclaration */: + grammarErrorOnFirstToken(node, ts.Diagnostics.Imports_are_not_permitted_in_module_augmentations_Consider_moving_them_to_the_enclosing_external_module); + break; + case 169 /* BindingElement */: + case 218 /* VariableDeclaration */: + var name_22 = node.name; + if (ts.isBindingPattern(name_22)) { + for (var _b = 0, _c = name_22.elements; _b < _c.length; _b++) { + var el = _c[_b]; + // mark individual names in binding pattern + checkModuleAugmentationElement(el, isGlobalAugmentation); } - flags |= 32 /* Static */; - lastStatic = modifier; break; - case 128 /* ReadonlyKeyword */: - if (flags & 64 /* Readonly */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_already_seen, "readonly"); - } - else if (node.kind !== 145 /* PropertyDeclaration */ && node.kind !== 144 /* PropertySignature */ && node.kind !== 153 /* IndexSignature */ && node.kind !== 142 /* Parameter */) { - // If node.kind === SyntaxKind.Parameter, checkParameter report an error if it's not a parameter property. - return grammarErrorOnNode(modifier, ts.Diagnostics.readonly_modifier_can_only_appear_on_a_property_declaration_or_index_signature); - } - flags |= 64 /* Readonly */; - lastReadonly = modifier; - break; - case 82 /* ExportKeyword */: - if (flags & 1 /* Export */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_already_seen, "export"); - } - else if (flags & 2 /* Ambient */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, "export", "declare"); - } - else if (flags & 128 /* Abstract */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, "export", "abstract"); - } - else if (flags & 256 /* Async */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, "export", "async"); - } - else if (node.parent.kind === 221 /* ClassDeclaration */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_class_element, "export"); - } - else if (node.kind === 142 /* Parameter */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_parameter, "export"); - } - flags |= 1 /* Export */; - break; - case 122 /* DeclareKeyword */: - if (flags & 2 /* Ambient */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_already_seen, "declare"); - } - else if (flags & 256 /* Async */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_be_used_in_an_ambient_context, "async"); - } - else if (node.parent.kind === 221 /* ClassDeclaration */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_class_element, "declare"); - } - else if (node.kind === 142 /* Parameter */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_parameter, "declare"); - } - else if (ts.isInAmbientContext(node.parent) && node.parent.kind === 226 /* ModuleBlock */) { - return grammarErrorOnNode(modifier, ts.Diagnostics.A_declare_modifier_cannot_be_used_in_an_already_ambient_context); - } - flags |= 2 /* Ambient */; - lastDeclare = modifier; - break; - case 115 /* AbstractKeyword */: - if (flags & 128 /* Abstract */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_already_seen, "abstract"); - } - if (node.kind !== 221 /* ClassDeclaration */) { - if (node.kind !== 147 /* MethodDeclaration */ && - node.kind !== 145 /* PropertyDeclaration */ && - node.kind !== 149 /* GetAccessor */ && - node.kind !== 150 /* SetAccessor */) { - return grammarErrorOnNode(modifier, ts.Diagnostics.abstract_modifier_can_only_appear_on_a_class_method_or_property_declaration); - } - if (!(node.parent.kind === 221 /* ClassDeclaration */ && ts.getModifierFlags(node.parent) & 128 /* Abstract */)) { - return grammarErrorOnNode(modifier, ts.Diagnostics.Abstract_methods_can_only_appear_within_an_abstract_class); - } - if (flags & 32 /* Static */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "static", "abstract"); - } - if (flags & 8 /* Private */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "private", "abstract"); - } - } - flags |= 128 /* Abstract */; - break; - case 118 /* AsyncKeyword */: - if (flags & 256 /* Async */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_already_seen, "async"); - } - else if (flags & 2 /* Ambient */ || ts.isInAmbientContext(node.parent)) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_be_used_in_an_ambient_context, "async"); - } - else if (node.kind === 142 /* Parameter */) { - return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_parameter, "async"); + } + // fallthrough + case 221 /* ClassDeclaration */: + case 224 /* EnumDeclaration */: + case 220 /* FunctionDeclaration */: + case 222 /* InterfaceDeclaration */: + case 225 /* ModuleDeclaration */: + case 223 /* TypeAliasDeclaration */: + if (isGlobalAugmentation) { + return; + } + var symbol = getSymbolOfNode(node); + if (symbol) { + // module augmentations cannot introduce new names on the top level scope of the module + // this is done it two steps + // 1. quick check - if symbol for node is not merged - this is local symbol to this augmentation - report error + // 2. main check - report error if value declaration of the parent symbol is module augmentation) + var reportError = !(symbol.flags & 33554432 /* Merged */); + if (!reportError) { + // symbol should not originate in augmentation + reportError = ts.isExternalModuleAugmentation(symbol.parent.declarations[0]); } - flags |= 256 /* Async */; - lastAsync = modifier; - break; - } + } + break; } - if (node.kind === 148 /* Constructor */) { - if (flags & 32 /* Static */) { - return grammarErrorOnNode(lastStatic, ts.Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "static"); - } - if (flags & 128 /* Abstract */) { - return grammarErrorOnNode(lastStatic, ts.Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "abstract"); - } - else if (flags & 256 /* Async */) { - return grammarErrorOnNode(lastAsync, ts.Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "async"); - } - else if (flags & 64 /* Readonly */) { - return grammarErrorOnNode(lastReadonly, ts.Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "readonly"); - } - return; + } + function getFirstIdentifier(node) { + switch (node.kind) { + case 69 /* Identifier */: + return node; + case 139 /* QualifiedName */: + do { + node = node.left; + } while (node.kind !== 69 /* Identifier */); + return node; + case 172 /* PropertyAccessExpression */: + do { + node = node.expression; + } while (node.kind !== 69 /* Identifier */); + return node; } - else if ((node.kind === 230 /* ImportDeclaration */ || node.kind === 229 /* ImportEqualsDeclaration */) && flags & 2 /* Ambient */) { - return grammarErrorOnNode(lastDeclare, ts.Diagnostics.A_0_modifier_cannot_be_used_with_an_import_declaration, "declare"); + } + function checkExternalImportOrExportDeclaration(node) { + var moduleName = ts.getExternalModuleName(node); + if (!ts.nodeIsMissing(moduleName) && moduleName.kind !== 9 /* StringLiteral */) { + error(moduleName, ts.Diagnostics.String_literal_expected); + return false; } - else if (node.kind === 142 /* Parameter */ && (flags & 92 /* ParameterPropertyModifier */) && ts.isBindingPattern(node.name)) { - return grammarErrorOnNode(node, ts.Diagnostics.A_parameter_property_may_not_be_declared_using_a_binding_pattern); + var inAmbientExternalModule = node.parent.kind === 226 /* ModuleBlock */ && ts.isAmbientModule(node.parent.parent); + if (node.parent.kind !== 256 /* SourceFile */ && !inAmbientExternalModule) { + error(moduleName, node.kind === 236 /* ExportDeclaration */ ? + ts.Diagnostics.Export_declarations_are_not_permitted_in_a_namespace : + ts.Diagnostics.Import_declarations_in_a_namespace_cannot_reference_a_module); + return false; } - else if (node.kind === 142 /* Parameter */ && (flags & 92 /* ParameterPropertyModifier */) && node.dotDotDotToken) { - return grammarErrorOnNode(node, ts.Diagnostics.A_parameter_property_cannot_be_declared_using_a_rest_parameter); + if (inAmbientExternalModule && ts.isExternalModuleNameRelative(moduleName.text)) { + // we have already reported errors on top level imports\exports in external module augmentations in checkModuleDeclaration + // no need to do this again. + if (!isTopLevelInExternalModuleAugmentation(node)) { + // TypeScript 1.0 spec (April 2013): 12.1.6 + // An ExternalImportDeclaration in an AmbientExternalModuleDeclaration may reference + // other external modules only through top - level external module names. + // Relative external module names are not permitted. + error(node, ts.Diagnostics.Import_or_export_declaration_in_an_ambient_module_declaration_cannot_reference_module_through_relative_module_name); + return false; + } } - if (flags & 256 /* Async */) { - return checkGrammarAsyncModifier(node, lastAsync); + return true; + } + function checkAliasSymbol(node) { + var symbol = getSymbolOfNode(node); + var target = resolveAlias(symbol); + if (target !== unknownSymbol) { + // For external modules symbol represent local symbol for an alias. + // This local symbol will merge any other local declarations (excluding other aliases) + // and symbol.flags will contains combined representation for all merged declaration. + // Based on symbol.flags we can compute a set of excluded meanings (meaning that resolved alias should not have, + // otherwise it will conflict with some local declaration). Note that in addition to normal flags we include matching SymbolFlags.Export* + // in order to prevent collisions with declarations that were exported from the current module (they still contribute to local names). + var excludedMeanings = (symbol.flags & (107455 /* Value */ | 1048576 /* ExportValue */) ? 107455 /* Value */ : 0) | + (symbol.flags & 793064 /* Type */ ? 793064 /* Type */ : 0) | + (symbol.flags & 1920 /* Namespace */ ? 1920 /* Namespace */ : 0); + if (target.flags & excludedMeanings) { + var message = node.kind === 238 /* ExportSpecifier */ ? + ts.Diagnostics.Export_declaration_conflicts_with_exported_declaration_of_0 : + ts.Diagnostics.Import_declaration_conflicts_with_local_declaration_of_0; + error(node, message, symbolToString(symbol)); + } } } - /** - * true | false: Early return this value from checkGrammarModifiers. - * undefined: Need to do full checking on the modifiers. - */ - function reportObviousModifierErrors(node) { - return !node.modifiers - ? false - : shouldReportBadModifier(node) - ? grammarErrorOnFirstToken(node, ts.Diagnostics.Modifiers_cannot_appear_here) - : undefined; + function checkImportBinding(node) { + checkCollisionWithCapturedThisVariable(node, node.name); + checkCollisionWithRequireExportsInGeneratedCode(node, node.name); + checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name); + checkAliasSymbol(node); } - function shouldReportBadModifier(node) { - switch (node.kind) { - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - case 148 /* Constructor */: - case 145 /* PropertyDeclaration */: - case 144 /* PropertySignature */: - case 147 /* MethodDeclaration */: - case 146 /* MethodSignature */: - case 153 /* IndexSignature */: - case 225 /* ModuleDeclaration */: - case 230 /* ImportDeclaration */: - case 229 /* ImportEqualsDeclaration */: - case 236 /* ExportDeclaration */: - case 235 /* ExportAssignment */: - case 179 /* FunctionExpression */: - case 180 /* ArrowFunction */: - case 142 /* Parameter */: - return false; - default: - if (node.parent.kind === 226 /* ModuleBlock */ || node.parent.kind === 256 /* SourceFile */) { - return false; + function checkImportDeclaration(node) { + if (checkGrammarModuleElementContext(node, ts.Diagnostics.An_import_declaration_can_only_be_used_in_a_namespace_or_module)) { + // If we hit an import declaration in an illegal context, just bail out to avoid cascading errors. + return; + } + if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node) && ts.getModifierFlags(node) !== 0) { + grammarErrorOnFirstToken(node, ts.Diagnostics.An_import_declaration_cannot_have_modifiers); + } + if (checkExternalImportOrExportDeclaration(node)) { + var importClause = node.importClause; + if (importClause) { + if (importClause.name) { + checkImportBinding(importClause); } - switch (node.kind) { - case 220 /* FunctionDeclaration */: - return nodeHasAnyModifiersExcept(node, 118 /* AsyncKeyword */); - case 221 /* ClassDeclaration */: - return nodeHasAnyModifiersExcept(node, 115 /* AbstractKeyword */); - case 222 /* InterfaceDeclaration */: - case 200 /* VariableStatement */: - case 223 /* TypeAliasDeclaration */: - return true; - case 224 /* EnumDeclaration */: - return nodeHasAnyModifiersExcept(node, 74 /* ConstKeyword */); - default: - ts.Debug.fail(); - return false; + if (importClause.namedBindings) { + if (importClause.namedBindings.kind === 232 /* NamespaceImport */) { + checkImportBinding(importClause.namedBindings); + } + else { + ts.forEach(importClause.namedBindings.elements, checkImportBinding); + } } + } } } - function nodeHasAnyModifiersExcept(node, allowedModifier) { - return node.modifiers.length > 1 || node.modifiers[0].kind !== allowedModifier; - } - function checkGrammarAsyncModifier(node, asyncModifier) { - switch (node.kind) { - case 147 /* MethodDeclaration */: - case 220 /* FunctionDeclaration */: - case 179 /* FunctionExpression */: - case 180 /* ArrowFunction */: - if (!node.asteriskToken) { - return false; - } - break; + function checkImportEqualsDeclaration(node) { + if (checkGrammarModuleElementContext(node, ts.Diagnostics.An_import_declaration_can_only_be_used_in_a_namespace_or_module)) { + // If we hit an import declaration in an illegal context, just bail out to avoid cascading errors. + return; } - return grammarErrorOnNode(asyncModifier, ts.Diagnostics._0_modifier_cannot_be_used_here, "async"); - } - function checkGrammarForDisallowedTrailingComma(list) { - if (list && list.hasTrailingComma) { - var start = list.end - ",".length; - var end = list.end; - var sourceFile = ts.getSourceFileOfNode(list[0]); - return grammarErrorAtPos(sourceFile, start, end - start, ts.Diagnostics.Trailing_comma_not_allowed); + checkGrammarDecorators(node) || checkGrammarModifiers(node); + if (ts.isInternalModuleImportEqualsDeclaration(node) || checkExternalImportOrExportDeclaration(node)) { + checkImportBinding(node); + if (ts.getModifierFlags(node) & 1 /* Export */) { + markExportAsReferenced(node); + } + if (ts.isInternalModuleImportEqualsDeclaration(node)) { + var target = resolveAlias(getSymbolOfNode(node)); + if (target !== unknownSymbol) { + if (target.flags & 107455 /* Value */) { + // Target is a value symbol, check that it is not hidden by a local declaration with the same name + var moduleName = getFirstIdentifier(node.moduleReference); + if (!(resolveEntityName(moduleName, 107455 /* Value */ | 1920 /* Namespace */).flags & 1920 /* Namespace */)) { + error(moduleName, ts.Diagnostics.Module_0_is_hidden_by_a_local_declaration_with_the_same_name, ts.declarationNameToString(moduleName)); + } + } + if (target.flags & 793064 /* Type */) { + checkTypeNameIsReserved(node.name, ts.Diagnostics.Import_name_cannot_be_0); + } + } + } + else { + if (modulekind === ts.ModuleKind.ES6 && !ts.isInAmbientContext(node)) { + // Import equals declaration is deprecated in es6 or above + grammarErrorOnNode(node, ts.Diagnostics.Import_assignment_cannot_be_used_when_targeting_ECMAScript_2015_modules_Consider_using_import_Asterisk_as_ns_from_mod_import_a_from_mod_import_d_from_mod_or_another_module_format_instead); + } + } } } - function checkGrammarTypeParameterList(node, typeParameters, file) { - if (checkGrammarForDisallowedTrailingComma(typeParameters)) { - return true; + function checkExportDeclaration(node) { + if (checkGrammarModuleElementContext(node, ts.Diagnostics.An_export_declaration_can_only_be_used_in_a_module)) { + // If we hit an export in an illegal context, just bail out to avoid cascading errors. + return; } - if (typeParameters && typeParameters.length === 0) { - var start = typeParameters.pos - "<".length; - var end = ts.skipTrivia(file.text, typeParameters.end) + ">".length; - return grammarErrorAtPos(file, start, end - start, ts.Diagnostics.Type_parameter_list_cannot_be_empty); + if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node) && ts.getModifierFlags(node) !== 0) { + grammarErrorOnFirstToken(node, ts.Diagnostics.An_export_declaration_cannot_have_modifiers); } - } - function checkGrammarParameterList(parameters) { - var seenOptionalParameter = false; - var parameterCount = parameters.length; - for (var i = 0; i < parameterCount; i++) { - var parameter = parameters[i]; - if (parameter.dotDotDotToken) { - if (i !== (parameterCount - 1)) { - return grammarErrorOnNode(parameter.dotDotDotToken, ts.Diagnostics.A_rest_parameter_must_be_last_in_a_parameter_list); - } - if (ts.isBindingPattern(parameter.name)) { - return grammarErrorOnNode(parameter.name, ts.Diagnostics.A_rest_element_cannot_contain_a_binding_pattern); - } - if (parameter.questionToken) { - return grammarErrorOnNode(parameter.questionToken, ts.Diagnostics.A_rest_parameter_cannot_be_optional); - } - if (parameter.initializer) { - return grammarErrorOnNode(parameter.name, ts.Diagnostics.A_rest_parameter_cannot_have_an_initializer); + if (!node.moduleSpecifier || checkExternalImportOrExportDeclaration(node)) { + if (node.exportClause) { + // export { x, y } + // export { x, y } from "foo" + ts.forEach(node.exportClause.elements, checkExportSpecifier); + var inAmbientExternalModule = node.parent.kind === 226 /* ModuleBlock */ && ts.isAmbientModule(node.parent.parent); + if (node.parent.kind !== 256 /* SourceFile */ && !inAmbientExternalModule) { + error(node, ts.Diagnostics.Export_declarations_are_not_permitted_in_a_namespace); } } - else if (parameter.questionToken) { - seenOptionalParameter = true; - if (parameter.initializer) { - return grammarErrorOnNode(parameter.name, ts.Diagnostics.Parameter_cannot_have_question_mark_and_initializer); + else { + // export * from "foo" + var moduleSymbol = resolveExternalModuleName(node, node.moduleSpecifier); + if (moduleSymbol && hasExportAssignmentSymbol(moduleSymbol)) { + error(node.moduleSpecifier, ts.Diagnostics.Module_0_uses_export_and_cannot_be_used_with_export_Asterisk, symbolToString(moduleSymbol)); } } - else if (seenOptionalParameter && !parameter.initializer) { - return grammarErrorOnNode(parameter.name, ts.Diagnostics.A_required_parameter_cannot_follow_an_optional_parameter); - } } } - function checkGrammarFunctionLikeDeclaration(node) { - // Prevent cascading error by short-circuit - var file = ts.getSourceFileOfNode(node); - return checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarTypeParameterList(node, node.typeParameters, file) || - checkGrammarParameterList(node.parameters) || checkGrammarArrowFunction(node, file); - } - function checkGrammarArrowFunction(node, file) { - if (node.kind === 180 /* ArrowFunction */) { - var arrowFunction = node; - var startLine = ts.getLineAndCharacterOfPosition(file, arrowFunction.equalsGreaterThanToken.pos).line; - var endLine = ts.getLineAndCharacterOfPosition(file, arrowFunction.equalsGreaterThanToken.end).line; - if (startLine !== endLine) { - return grammarErrorOnNode(arrowFunction.equalsGreaterThanToken, ts.Diagnostics.Line_terminator_not_permitted_before_arrow); - } + function checkGrammarModuleElementContext(node, errorMessage) { + var isInAppropriateContext = node.parent.kind === 256 /* SourceFile */ || node.parent.kind === 226 /* ModuleBlock */ || node.parent.kind === 225 /* ModuleDeclaration */; + if (!isInAppropriateContext) { + grammarErrorOnFirstToken(node, errorMessage); } - return false; + return !isInAppropriateContext; } - function checkGrammarIndexSignatureParameters(node) { - var parameter = node.parameters[0]; - if (node.parameters.length !== 1) { - if (parameter) { - return grammarErrorOnNode(parameter.name, ts.Diagnostics.An_index_signature_must_have_exactly_one_parameter); + function checkExportSpecifier(node) { + checkAliasSymbol(node); + if (!node.parent.parent.moduleSpecifier) { + var exportedName = node.propertyName || node.name; + // find immediate value referenced by exported name (SymbolFlags.Alias is set so we don't chase down aliases) + var symbol = resolveName(exportedName, exportedName.text, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */ | 8388608 /* Alias */, + /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined); + if (symbol && (symbol === undefinedSymbol || isGlobalSourceFile(getDeclarationContainer(symbol.declarations[0])))) { + error(exportedName, ts.Diagnostics.Cannot_export_0_Only_local_declarations_can_be_exported_from_a_module, exportedName.text); } else { - return grammarErrorOnNode(node, ts.Diagnostics.An_index_signature_must_have_exactly_one_parameter); + markExportAsReferenced(node); } } - if (parameter.dotDotDotToken) { - return grammarErrorOnNode(parameter.dotDotDotToken, ts.Diagnostics.An_index_signature_cannot_have_a_rest_parameter); - } - if (ts.getModifierFlags(parameter) !== 0) { - return grammarErrorOnNode(parameter.name, ts.Diagnostics.An_index_signature_parameter_cannot_have_an_accessibility_modifier); + } + function checkExportAssignment(node) { + if (checkGrammarModuleElementContext(node, ts.Diagnostics.An_export_assignment_can_only_be_used_in_a_module)) { + // If we hit an export assignment in an illegal context, just bail out to avoid cascading errors. + return; } - if (parameter.questionToken) { - return grammarErrorOnNode(parameter.questionToken, ts.Diagnostics.An_index_signature_parameter_cannot_have_a_question_mark); + var container = node.parent.kind === 256 /* SourceFile */ ? node.parent : node.parent.parent; + if (container.kind === 225 /* ModuleDeclaration */ && !ts.isAmbientModule(container)) { + error(node, ts.Diagnostics.An_export_assignment_cannot_be_used_in_a_namespace); + return; } - if (parameter.initializer) { - return grammarErrorOnNode(parameter.name, ts.Diagnostics.An_index_signature_parameter_cannot_have_an_initializer); + // Grammar checking + if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node) && ts.getModifierFlags(node) !== 0) { + grammarErrorOnFirstToken(node, ts.Diagnostics.An_export_assignment_cannot_have_modifiers); } - if (!parameter.type) { - return grammarErrorOnNode(parameter.name, ts.Diagnostics.An_index_signature_parameter_must_have_a_type_annotation); + if (node.expression.kind === 69 /* Identifier */) { + markExportAsReferenced(node); } - if (parameter.type.kind !== 132 /* StringKeyword */ && parameter.type.kind !== 130 /* NumberKeyword */) { - return grammarErrorOnNode(parameter.name, ts.Diagnostics.An_index_signature_parameter_type_must_be_string_or_number); + else { + checkExpressionCached(node.expression); } - if (!node.type) { - return grammarErrorOnNode(node, ts.Diagnostics.An_index_signature_must_have_a_type_annotation); + checkExternalModuleExports(container); + if (node.isExportEquals && !ts.isInAmbientContext(node)) { + if (modulekind === ts.ModuleKind.ES6) { + // export assignment is not supported in es6 modules + grammarErrorOnNode(node, ts.Diagnostics.Export_assignment_cannot_be_used_when_targeting_ECMAScript_2015_modules_Consider_using_export_default_or_another_module_format_instead); + } + else if (modulekind === ts.ModuleKind.System) { + // system modules does not support export assignment + grammarErrorOnNode(node, ts.Diagnostics.Export_assignment_is_not_supported_when_module_flag_is_system); + } } } - function checkGrammarIndexSignature(node) { - // Prevent cascading error by short-circuit - return checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarIndexSignatureParameters(node); - } - function checkGrammarForAtLeastOneTypeArgument(node, typeArguments) { - if (typeArguments && typeArguments.length === 0) { - var sourceFile = ts.getSourceFileOfNode(node); - var start = typeArguments.pos - "<".length; - var end = ts.skipTrivia(sourceFile.text, typeArguments.end) + ">".length; - return grammarErrorAtPos(sourceFile, start, end - start, ts.Diagnostics.Type_argument_list_cannot_be_empty); + function hasExportedMembers(moduleSymbol) { + for (var id in moduleSymbol.exports) { + if (id !== "export=") { + return true; + } } + return false; } - function checkGrammarTypeArguments(node, typeArguments) { - return checkGrammarForDisallowedTrailingComma(typeArguments) || - checkGrammarForAtLeastOneTypeArgument(node, typeArguments); - } - function checkGrammarForOmittedArgument(node, args) { - if (args) { - var sourceFile = ts.getSourceFileOfNode(node); - for (var _i = 0, args_2 = args; _i < args_2.length; _i++) { - var arg = args_2[_i]; - if (arg.kind === 193 /* OmittedExpression */) { - return grammarErrorAtPos(sourceFile, arg.pos, 0, ts.Diagnostics.Argument_expression_expected); + function checkExternalModuleExports(node) { + var moduleSymbol = getSymbolOfNode(node); + var links = getSymbolLinks(moduleSymbol); + if (!links.exportsChecked) { + var exportEqualsSymbol = moduleSymbol.exports["export="]; + if (exportEqualsSymbol && hasExportedMembers(moduleSymbol)) { + var declaration = getDeclarationOfAliasSymbol(exportEqualsSymbol) || exportEqualsSymbol.valueDeclaration; + if (!isTopLevelInExternalModuleAugmentation(declaration)) { + error(declaration, ts.Diagnostics.An_export_assignment_cannot_be_used_in_a_module_with_other_exported_elements); } } + // Checks for export * conflicts + var exports = getExportsOfModule(moduleSymbol); + for (var id in exports) { + if (id === "__export") { + continue; + } + var _a = exports[id], declarations = _a.declarations, flags = _a.flags; + // ECMA262: 15.2.1.1 It is a Syntax Error if the ExportedNames of ModuleItemList contains any duplicate entries. + // (TS Exceptions: namespaces, function overloads, enums, and interfaces) + if (flags & (1920 /* Namespace */ | 64 /* Interface */ | 384 /* Enum */)) { + continue; + } + var exportedDeclarationsCount = ts.countWhere(declarations, isNotOverload); + if (flags & 524288 /* TypeAlias */ && exportedDeclarationsCount <= 2) { + // it is legal to merge type alias with other values + // so count should be either 1 (just type alias) or 2 (type alias + merged value) + continue; + } + if (exportedDeclarationsCount > 1) { + for (var _i = 0, declarations_6 = declarations; _i < declarations_6.length; _i++) { + var declaration = declarations_6[_i]; + if (isNotOverload(declaration)) { + diagnostics.add(ts.createDiagnosticForNode(declaration, ts.Diagnostics.Cannot_redeclare_exported_variable_0, id)); + } + } + } + } + links.exportsChecked = true; } - } - function checkGrammarArguments(node, args) { - return checkGrammarForOmittedArgument(node, args); - } - function checkGrammarHeritageClause(node) { - var types = node.types; - if (checkGrammarForDisallowedTrailingComma(types)) { - return true; - } - if (types && types.length === 0) { - var listType = ts.tokenToString(node.token); - var sourceFile = ts.getSourceFileOfNode(node); - return grammarErrorAtPos(sourceFile, types.pos, 0, ts.Diagnostics._0_list_cannot_be_empty, listType); + function isNotOverload(declaration) { + return (declaration.kind !== 220 /* FunctionDeclaration */ && declaration.kind !== 147 /* MethodDeclaration */) || + !!declaration.body; } } - function checkGrammarClassDeclarationHeritageClauses(node) { - var seenExtendsClause = false; - var seenImplementsClause = false; - if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node) && node.heritageClauses) { - for (var _i = 0, _a = node.heritageClauses; _i < _a.length; _i++) { - var heritageClause = _a[_i]; - if (heritageClause.token === 83 /* ExtendsKeyword */) { - if (seenExtendsClause) { - return grammarErrorOnFirstToken(heritageClause, ts.Diagnostics.extends_clause_already_seen); - } - if (seenImplementsClause) { - return grammarErrorOnFirstToken(heritageClause, ts.Diagnostics.extends_clause_must_precede_implements_clause); - } - if (heritageClause.types.length > 1) { - return grammarErrorOnFirstToken(heritageClause.types[1], ts.Diagnostics.Classes_can_only_extend_a_single_class); - } - seenExtendsClause = true; - } - else { - ts.Debug.assert(heritageClause.token === 106 /* ImplementsKeyword */); - if (seenImplementsClause) { - return grammarErrorOnFirstToken(heritageClause, ts.Diagnostics.implements_clause_already_seen); - } - seenImplementsClause = true; - } - // Grammar checking heritageClause inside class declaration - checkGrammarHeritageClause(heritageClause); - } + function checkSourceElement(node) { + if (!node) { + return; } - } - function checkGrammarInterfaceDeclaration(node) { - var seenExtendsClause = false; - if (node.heritageClauses) { - for (var _i = 0, _a = node.heritageClauses; _i < _a.length; _i++) { - var heritageClause = _a[_i]; - if (heritageClause.token === 83 /* ExtendsKeyword */) { - if (seenExtendsClause) { - return grammarErrorOnFirstToken(heritageClause, ts.Diagnostics.extends_clause_already_seen); - } - seenExtendsClause = true; - } - else { - ts.Debug.assert(heritageClause.token === 106 /* ImplementsKeyword */); - return grammarErrorOnFirstToken(heritageClause, ts.Diagnostics.Interface_declaration_cannot_have_implements_clause); - } - // Grammar checking heritageClause inside class declaration - checkGrammarHeritageClause(heritageClause); + var kind = node.kind; + if (cancellationToken) { + // Only bother checking on a few construct kinds. We don't want to be excessively + // hitting the cancellation token on every node we check. + switch (kind) { + case 225 /* ModuleDeclaration */: + case 221 /* ClassDeclaration */: + case 222 /* InterfaceDeclaration */: + case 220 /* FunctionDeclaration */: + cancellationToken.throwIfCancellationRequested(); } } - return false; - } - function checkGrammarComputedPropertyName(node) { - // If node is not a computedPropertyName, just skip the grammar checking - if (node.kind !== 140 /* ComputedPropertyName */) { - return false; + switch (kind) { + case 141 /* TypeParameter */: + return checkTypeParameter(node); + case 142 /* Parameter */: + return checkParameter(node); + case 145 /* PropertyDeclaration */: + case 144 /* PropertySignature */: + return checkPropertyDeclaration(node); + case 156 /* FunctionType */: + case 157 /* ConstructorType */: + case 151 /* CallSignature */: + case 152 /* ConstructSignature */: + return checkSignatureDeclaration(node); + case 153 /* IndexSignature */: + return checkSignatureDeclaration(node); + case 147 /* MethodDeclaration */: + case 146 /* MethodSignature */: + return checkMethodDeclaration(node); + case 148 /* Constructor */: + return checkConstructorDeclaration(node); + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + return checkAccessorDeclaration(node); + case 155 /* TypeReference */: + return checkTypeReferenceNode(node); + case 154 /* TypePredicate */: + return checkTypePredicate(node); + case 158 /* TypeQuery */: + return checkTypeQuery(node); + case 159 /* TypeLiteral */: + return checkTypeLiteral(node); + case 160 /* ArrayType */: + return checkArrayType(node); + case 161 /* TupleType */: + return checkTupleType(node); + case 162 /* UnionType */: + case 163 /* IntersectionType */: + return checkUnionOrIntersectionType(node); + case 164 /* ParenthesizedType */: + return checkSourceElement(node.type); + case 220 /* FunctionDeclaration */: + return checkFunctionDeclaration(node); + case 199 /* Block */: + case 226 /* ModuleBlock */: + return checkBlock(node); + case 200 /* VariableStatement */: + return checkVariableStatement(node); + case 202 /* ExpressionStatement */: + return checkExpressionStatement(node); + case 203 /* IfStatement */: + return checkIfStatement(node); + case 204 /* DoStatement */: + return checkDoStatement(node); + case 205 /* WhileStatement */: + return checkWhileStatement(node); + case 206 /* ForStatement */: + return checkForStatement(node); + case 207 /* ForInStatement */: + return checkForInStatement(node); + case 208 /* ForOfStatement */: + return checkForOfStatement(node); + case 209 /* ContinueStatement */: + case 210 /* BreakStatement */: + return checkBreakOrContinueStatement(node); + case 211 /* ReturnStatement */: + return checkReturnStatement(node); + case 212 /* WithStatement */: + return checkWithStatement(node); + case 213 /* SwitchStatement */: + return checkSwitchStatement(node); + case 214 /* LabeledStatement */: + return checkLabeledStatement(node); + case 215 /* ThrowStatement */: + return checkThrowStatement(node); + case 216 /* TryStatement */: + return checkTryStatement(node); + case 218 /* VariableDeclaration */: + return checkVariableDeclaration(node); + case 169 /* BindingElement */: + return checkBindingElement(node); + case 221 /* ClassDeclaration */: + return checkClassDeclaration(node); + case 222 /* InterfaceDeclaration */: + return checkInterfaceDeclaration(node); + case 223 /* TypeAliasDeclaration */: + return checkTypeAliasDeclaration(node); + case 224 /* EnumDeclaration */: + return checkEnumDeclaration(node); + case 225 /* ModuleDeclaration */: + return checkModuleDeclaration(node); + case 230 /* ImportDeclaration */: + return checkImportDeclaration(node); + case 229 /* ImportEqualsDeclaration */: + return checkImportEqualsDeclaration(node); + case 236 /* ExportDeclaration */: + return checkExportDeclaration(node); + case 235 /* ExportAssignment */: + return checkExportAssignment(node); + case 201 /* EmptyStatement */: + checkGrammarStatementInAmbientContext(node); + return; + case 217 /* DebuggerStatement */: + checkGrammarStatementInAmbientContext(node); + return; + case 239 /* MissingDeclaration */: + return checkMissingDeclaration(node); } - var computedPropertyName = node; - if (computedPropertyName.expression.kind === 187 /* BinaryExpression */ && computedPropertyName.expression.operatorToken.kind === 24 /* CommaToken */) { - return grammarErrorOnNode(computedPropertyName.expression, ts.Diagnostics.A_comma_expression_is_not_allowed_in_a_computed_property_name); + } + // Function and class expression bodies are checked after all statements in the enclosing body. This is + // to ensure constructs like the following are permitted: + // const foo = function () { + // const s = foo(); + // return "hello"; + // } + // Here, performing a full type check of the body of the function expression whilst in the process of + // determining the type of foo would cause foo to be given type any because of the recursive reference. + // Delaying the type check of the body ensures foo has been assigned a type. + function checkNodeDeferred(node) { + if (deferredNodes) { + deferredNodes.push(node); } } - function checkGrammarForGenerator(node) { - if (node.asteriskToken) { - ts.Debug.assert(node.kind === 220 /* FunctionDeclaration */ || - node.kind === 179 /* FunctionExpression */ || - node.kind === 147 /* MethodDeclaration */); - if (ts.isInAmbientContext(node)) { - return grammarErrorOnNode(node.asteriskToken, ts.Diagnostics.Generators_are_not_allowed_in_an_ambient_context); - } - if (!node.body) { - return grammarErrorOnNode(node.asteriskToken, ts.Diagnostics.An_overload_signature_cannot_be_declared_as_a_generator); - } - if (languageVersion < 2 /* ES6 */) { - return grammarErrorOnNode(node.asteriskToken, ts.Diagnostics.Generators_are_only_available_when_targeting_ECMAScript_2015_or_higher); + function checkDeferredNodes() { + for (var _i = 0, deferredNodes_1 = deferredNodes; _i < deferredNodes_1.length; _i++) { + var node = deferredNodes_1[_i]; + switch (node.kind) { + case 179 /* FunctionExpression */: + case 180 /* ArrowFunction */: + case 147 /* MethodDeclaration */: + case 146 /* MethodSignature */: + checkFunctionExpressionOrObjectLiteralMethodDeferred(node); + break; + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + checkAccessorDeferred(node); + break; + case 192 /* ClassExpression */: + checkClassExpressionDeferred(node); + break; } } } - function checkGrammarForInvalidQuestionMark(node, questionToken, message) { - if (questionToken) { - return grammarErrorOnNode(questionToken, message); - } + function checkSourceFile(node) { + ts.performance.mark("beforeCheck"); + checkSourceFileWorker(node); + ts.performance.mark("afterCheck"); + ts.performance.measure("Check", "beforeCheck", "afterCheck"); } - function checkGrammarObjectLiteralExpression(node, inDestructuring) { - var seen = ts.createMap(); - var Property = 1; - var GetAccessor = 2; - var SetAccessor = 4; - var GetOrSetAccessor = GetAccessor | SetAccessor; - for (var _i = 0, _a = node.properties; _i < _a.length; _i++) { - var prop = _a[_i]; - var name_24 = prop.name; - if (prop.kind === 193 /* OmittedExpression */ || - name_24.kind === 140 /* ComputedPropertyName */) { - // If the name is not a ComputedPropertyName, the grammar checking will skip it - checkGrammarComputedPropertyName(name_24); - } - if (prop.kind === 254 /* ShorthandPropertyAssignment */ && !inDestructuring && prop.objectAssignmentInitializer) { - // having objectAssignmentInitializer is only valid in ObjectAssignmentPattern - // outside of destructuring it is a syntax error - return grammarErrorOnNode(prop.equalsToken, ts.Diagnostics.can_only_be_used_in_an_object_literal_property_inside_a_destructuring_assignment); - } - // Modifiers are never allowed on properties except for 'async' on a method declaration - if (prop.modifiers) { - for (var _b = 0, _c = prop.modifiers; _b < _c.length; _b++) { - var mod = _c[_b]; - if (mod.kind !== 118 /* AsyncKeyword */ || prop.kind !== 147 /* MethodDeclaration */) { - grammarErrorOnNode(mod, ts.Diagnostics._0_modifier_cannot_be_used_here, ts.getTextOfNode(mod)); - } - } - } - // ECMA-262 11.1.5 Object Initializer - // If previous is not undefined then throw a SyntaxError exception if any of the following conditions are true - // a.This production is contained in strict code and IsDataDescriptor(previous) is true and - // IsDataDescriptor(propId.descriptor) is true. - // b.IsDataDescriptor(previous) is true and IsAccessorDescriptor(propId.descriptor) is true. - // c.IsAccessorDescriptor(previous) is true and IsDataDescriptor(propId.descriptor) is true. - // d.IsAccessorDescriptor(previous) is true and IsAccessorDescriptor(propId.descriptor) is true - // and either both previous and propId.descriptor have[[Get]] fields or both previous and propId.descriptor have[[Set]] fields - var currentKind = void 0; - if (prop.kind === 253 /* PropertyAssignment */ || prop.kind === 254 /* ShorthandPropertyAssignment */) { - // Grammar checking for computedPropertyName and shorthandPropertyAssignment - checkGrammarForInvalidQuestionMark(prop, prop.questionToken, ts.Diagnostics.An_object_member_cannot_be_declared_optional); - if (name_24.kind === 8 /* NumericLiteral */) { - checkGrammarNumericLiteral(name_24); - } - currentKind = Property; - } - else if (prop.kind === 147 /* MethodDeclaration */) { - currentKind = Property; - } - else if (prop.kind === 149 /* GetAccessor */) { - currentKind = GetAccessor; - } - else if (prop.kind === 150 /* SetAccessor */) { - currentKind = SetAccessor; + // Fully type check a source file and collect the relevant diagnostics. + function checkSourceFileWorker(node) { + var links = getNodeLinks(node); + if (!(links.flags & 1 /* TypeChecked */)) { + // If skipLibCheck is enabled, skip type checking if file is a declaration file. + // If skipDefaultLibCheck is enabled, skip type checking if file contains a + // '/// ' directive. + if (compilerOptions.skipLibCheck && node.isDeclarationFile || compilerOptions.skipDefaultLibCheck && node.hasNoDefaultLib) { + return; } - else { - ts.Debug.fail("Unexpected syntax kind:" + prop.kind); + // Grammar checking + checkGrammarSourceFile(node); + potentialThisCollisions.length = 0; + deferredNodes = []; + deferredUnusedIdentifierNodes = produceDiagnostics && noUnusedIdentifiers ? [] : undefined; + ts.forEach(node.statements, checkSourceElement); + checkDeferredNodes(); + if (ts.isExternalModule(node)) { + registerForUnusedIdentifiersCheck(node); } - var effectiveName = ts.getPropertyNameForPropertyNameNode(name_24); - if (effectiveName === undefined) { - continue; + if (!node.isDeclarationFile) { + checkUnusedIdentifiers(); } - if (!seen[effectiveName]) { - seen[effectiveName] = currentKind; + deferredNodes = undefined; + deferredUnusedIdentifierNodes = undefined; + if (ts.isExternalOrCommonJsModule(node)) { + checkExternalModuleExports(node); } - else { - var existingKind = seen[effectiveName]; - if (currentKind === Property && existingKind === Property) { - grammarErrorOnNode(name_24, ts.Diagnostics.Duplicate_identifier_0, ts.getTextOfNode(name_24)); - } - else if ((currentKind & GetOrSetAccessor) && (existingKind & GetOrSetAccessor)) { - if (existingKind !== GetOrSetAccessor && currentKind !== existingKind) { - seen[effectiveName] = currentKind | existingKind; - } - else { - return grammarErrorOnNode(name_24, ts.Diagnostics.An_object_literal_cannot_have_multiple_get_Slashset_accessors_with_the_same_name); - } - } - else { - return grammarErrorOnNode(name_24, ts.Diagnostics.An_object_literal_cannot_have_property_and_accessor_with_the_same_name); - } + if (potentialThisCollisions.length) { + ts.forEach(potentialThisCollisions, checkIfThisIsCapturedInEnclosingScope); + potentialThisCollisions.length = 0; } + links.flags |= 1 /* TypeChecked */; } } - function checkGrammarJsxElement(node) { - var seen = ts.createMap(); - for (var _i = 0, _a = node.attributes; _i < _a.length; _i++) { - var attr = _a[_i]; - if (attr.kind === 247 /* JsxSpreadAttribute */) { - continue; - } - var jsxAttr = attr; - var name_25 = jsxAttr.name; - if (!seen[name_25.text]) { - seen[name_25.text] = true; - } - else { - return grammarErrorOnNode(name_25, ts.Diagnostics.JSX_elements_cannot_have_multiple_attributes_with_the_same_name); - } - var initializer = jsxAttr.initializer; - if (initializer && initializer.kind === 248 /* JsxExpression */ && !initializer.expression) { - return grammarErrorOnNode(jsxAttr.initializer, ts.Diagnostics.JSX_attributes_must_only_be_assigned_a_non_empty_expression); - } + function getDiagnostics(sourceFile, ct) { + try { + // Record the cancellation token so it can be checked later on during checkSourceElement. + // Do this in a finally block so we can ensure that it gets reset back to nothing after + // this call is done. + cancellationToken = ct; + return getDiagnosticsWorker(sourceFile); + } + finally { + cancellationToken = undefined; } } - function checkGrammarForInOrForOfStatement(forInOrOfStatement) { - if (checkGrammarStatementInAmbientContext(forInOrOfStatement)) { - return true; + function getDiagnosticsWorker(sourceFile) { + throwIfNonDiagnosticsProducing(); + if (sourceFile) { + checkSourceFile(sourceFile); + return diagnostics.getDiagnostics(sourceFile.fileName); } - if (forInOrOfStatement.initializer.kind === 219 /* VariableDeclarationList */) { - var variableList = forInOrOfStatement.initializer; - if (!checkGrammarVariableDeclarationList(variableList)) { - var declarations = variableList.declarations; - // declarations.length can be zero if there is an error in variable declaration in for-of or for-in - // See http://www.ecma-international.org/ecma-262/6.0/#sec-for-in-and-for-of-statements for details - // For example: - // var let = 10; - // for (let of [1,2,3]) {} // this is invalid ES6 syntax - // for (let in [1,2,3]) {} // this is invalid ES6 syntax - // We will then want to skip on grammar checking on variableList declaration - if (!declarations.length) { - return false; - } - if (declarations.length > 1) { - var diagnostic = forInOrOfStatement.kind === 207 /* ForInStatement */ - ? ts.Diagnostics.Only_a_single_variable_declaration_is_allowed_in_a_for_in_statement - : ts.Diagnostics.Only_a_single_variable_declaration_is_allowed_in_a_for_of_statement; - return grammarErrorOnFirstToken(variableList.declarations[1], diagnostic); - } - var firstDeclaration = declarations[0]; - if (firstDeclaration.initializer) { - var diagnostic = forInOrOfStatement.kind === 207 /* ForInStatement */ - ? ts.Diagnostics.The_variable_declaration_of_a_for_in_statement_cannot_have_an_initializer - : ts.Diagnostics.The_variable_declaration_of_a_for_of_statement_cannot_have_an_initializer; - return grammarErrorOnNode(firstDeclaration.name, diagnostic); - } - if (firstDeclaration.type) { - var diagnostic = forInOrOfStatement.kind === 207 /* ForInStatement */ - ? ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_use_a_type_annotation - : ts.Diagnostics.The_left_hand_side_of_a_for_of_statement_cannot_use_a_type_annotation; - return grammarErrorOnNode(firstDeclaration, diagnostic); + ts.forEach(host.getSourceFiles(), checkSourceFile); + return diagnostics.getDiagnostics(); + } + function getGlobalDiagnostics() { + throwIfNonDiagnosticsProducing(); + return diagnostics.getGlobalDiagnostics(); + } + function throwIfNonDiagnosticsProducing() { + if (!produceDiagnostics) { + throw new Error("Trying to get diagnostics from a type checker that does not produce them."); + } + } + // Language service support + function isInsideWithStatementBody(node) { + if (node) { + while (node.parent) { + if (node.parent.kind === 212 /* WithStatement */ && node.parent.statement === node) { + return true; } + node = node.parent; } } return false; } - function checkGrammarAccessor(accessor) { - var kind = accessor.kind; - if (languageVersion < 1 /* ES5 */) { - return grammarErrorOnNode(accessor.name, ts.Diagnostics.Accessors_are_only_available_when_targeting_ECMAScript_5_and_higher); - } - else if (ts.isInAmbientContext(accessor)) { - return grammarErrorOnNode(accessor.name, ts.Diagnostics.An_accessor_cannot_be_declared_in_an_ambient_context); - } - else if (accessor.body === undefined && !(ts.getModifierFlags(accessor) & 128 /* Abstract */)) { - return grammarErrorAtPos(ts.getSourceFileOfNode(accessor), accessor.end - 1, ";".length, ts.Diagnostics._0_expected, "{"); - } - else if (accessor.typeParameters) { - return grammarErrorOnNode(accessor.name, ts.Diagnostics.An_accessor_cannot_have_type_parameters); - } - else if (!doesAccessorHaveCorrectParameterCount(accessor)) { - return grammarErrorOnNode(accessor.name, kind === 149 /* GetAccessor */ ? - ts.Diagnostics.A_get_accessor_cannot_have_parameters : - ts.Diagnostics.A_set_accessor_must_have_exactly_one_parameter); + function getSymbolsInScope(location, meaning) { + var symbols = ts.createMap(); + var memberFlags = 0 /* None */; + if (isInsideWithStatementBody(location)) { + // We cannot answer semantic questions within a with block, do not proceed any further + return []; } - else if (kind === 150 /* SetAccessor */) { - if (accessor.type) { - return grammarErrorOnNode(accessor.name, ts.Diagnostics.A_set_accessor_cannot_have_a_return_type_annotation); - } - else { - var parameter = accessor.parameters[0]; - if (parameter.dotDotDotToken) { - return grammarErrorOnNode(parameter.dotDotDotToken, ts.Diagnostics.A_set_accessor_cannot_have_rest_parameter); + populateSymbols(); + return symbolsToArray(symbols); + function populateSymbols() { + while (location) { + if (location.locals && !isGlobalSourceFile(location)) { + copySymbols(location.locals, meaning); } - else if (parameter.questionToken) { - return grammarErrorOnNode(parameter.questionToken, ts.Diagnostics.A_set_accessor_cannot_have_an_optional_parameter); + switch (location.kind) { + case 256 /* SourceFile */: + if (!ts.isExternalOrCommonJsModule(location)) { + break; + } + case 225 /* ModuleDeclaration */: + copySymbols(getSymbolOfNode(location).exports, meaning & 8914931 /* ModuleMember */); + break; + case 224 /* EnumDeclaration */: + copySymbols(getSymbolOfNode(location).exports, meaning & 8 /* EnumMember */); + break; + case 192 /* ClassExpression */: + var className = location.name; + if (className) { + copySymbol(location.symbol, meaning); + } + // fall through; this fall-through is necessary because we would like to handle + // type parameter inside class expression similar to how we handle it in classDeclaration and interface Declaration + case 221 /* ClassDeclaration */: + case 222 /* InterfaceDeclaration */: + // If we didn't come from static member of class or interface, + // add the type parameters into the symbol table + // (type parameters of classDeclaration/classExpression and interface are in member property of the symbol. + // Note: that the memberFlags come from previous iteration. + if (!(memberFlags & 32 /* Static */)) { + copySymbols(getSymbolOfNode(location).members, meaning & 793064 /* Type */); + } + break; + case 179 /* FunctionExpression */: + var funcName = location.name; + if (funcName) { + copySymbol(location.symbol, meaning); + } + break; } - else if (parameter.initializer) { - return grammarErrorOnNode(accessor.name, ts.Diagnostics.A_set_accessor_parameter_cannot_have_an_initializer); + if (ts.introducesArgumentsExoticObject(location)) { + copySymbol(argumentsSymbol, meaning); } + memberFlags = ts.getModifierFlags(location); + location = location.parent; } + copySymbols(globals, meaning); } - } - /** Does the accessor have the right number of parameters? - - A get accessor has no parameters or a single `this` parameter. - A set accessor has one parameter or a `this` parameter and one more parameter */ - function doesAccessorHaveCorrectParameterCount(accessor) { - return getAccessorThisParameter(accessor) || accessor.parameters.length === (accessor.kind === 149 /* GetAccessor */ ? 0 : 1); - } - function getAccessorThisParameter(accessor) { - if (accessor.parameters.length === (accessor.kind === 149 /* GetAccessor */ ? 1 : 2) && - accessor.parameters[0].name.kind === 69 /* Identifier */ && - accessor.parameters[0].name.originalKeywordKind === 97 /* ThisKeyword */) { - return accessor.parameters[0]; - } - } - function getFunctionLikeThisParameter(func) { - if (func.parameters.length && - func.parameters[0].name.kind === 69 /* Identifier */ && - func.parameters[0].name.originalKeywordKind === 97 /* ThisKeyword */) { - return func.parameters[0]; - } - } - function checkGrammarForNonSymbolComputedProperty(node, message) { - if (ts.isDynamicName(node)) { - return grammarErrorOnNode(node, message); + /** + * Copy the given symbol into symbol tables if the symbol has the given meaning + * and it doesn't already existed in the symbol table + * @param key a key for storing in symbol table; if undefined, use symbol.name + * @param symbol the symbol to be added into symbol table + * @param meaning meaning of symbol to filter by before adding to symbol table + */ + function copySymbol(symbol, meaning) { + if (symbol.flags & meaning) { + var id = symbol.name; + // We will copy all symbol regardless of its reserved name because + // symbolsToArray will check whether the key is a reserved name and + // it will not copy symbol with reserved name to the array + if (!symbols[id]) { + symbols[id] = symbol; + } + } } - } - function checkGrammarMethod(node) { - if (checkGrammarDisallowedModifiersOnObjectLiteralExpressionMethod(node) || - checkGrammarFunctionLikeDeclaration(node) || - checkGrammarForGenerator(node)) { - return true; + function copySymbols(source, meaning) { + if (meaning) { + for (var id in source) { + var symbol = source[id]; + copySymbol(symbol, meaning); + } + } } - if (node.parent.kind === 171 /* ObjectLiteralExpression */) { - if (checkGrammarForInvalidQuestionMark(node, node.questionToken, ts.Diagnostics.An_object_member_cannot_be_declared_optional)) { + } + function isTypeDeclarationName(name) { + return name.kind === 69 /* Identifier */ && + isTypeDeclaration(name.parent) && + name.parent.name === name; + } + function isTypeDeclaration(node) { + switch (node.kind) { + case 141 /* TypeParameter */: + case 221 /* ClassDeclaration */: + case 222 /* InterfaceDeclaration */: + case 223 /* TypeAliasDeclaration */: + case 224 /* EnumDeclaration */: return true; - } - else if (node.body === undefined) { - return grammarErrorAtPos(ts.getSourceFileOfNode(node), node.end - 1, ";".length, ts.Diagnostics._0_expected, "{"); - } } - if (ts.isClassLike(node.parent)) { - // Technically, computed properties in ambient contexts is disallowed - // for property declarations and accessors too, not just methods. - // However, property declarations disallow computed names in general, - // and accessors are not allowed in ambient contexts in general, - // so this error only really matters for methods. - if (ts.isInAmbientContext(node)) { - return checkGrammarForNonSymbolComputedProperty(node.name, ts.Diagnostics.A_computed_property_name_in_an_ambient_context_must_directly_refer_to_a_built_in_symbol); - } - else if (!node.body) { - return checkGrammarForNonSymbolComputedProperty(node.name, ts.Diagnostics.A_computed_property_name_in_a_method_overload_must_directly_refer_to_a_built_in_symbol); - } + } + // True if the given identifier is part of a type reference + function isTypeReferenceIdentifier(entityName) { + var node = entityName; + while (node.parent && node.parent.kind === 139 /* QualifiedName */) { + node = node.parent; } - else if (node.parent.kind === 222 /* InterfaceDeclaration */) { - return checkGrammarForNonSymbolComputedProperty(node.name, ts.Diagnostics.A_computed_property_name_in_an_interface_must_directly_refer_to_a_built_in_symbol); + return node.parent && (node.parent.kind === 155 /* TypeReference */ || node.parent.kind === 267 /* JSDocTypeReference */); + } + function isHeritageClauseElementIdentifier(entityName) { + var node = entityName; + while (node.parent && node.parent.kind === 172 /* PropertyAccessExpression */) { + node = node.parent; } - else if (node.parent.kind === 159 /* TypeLiteral */) { - return checkGrammarForNonSymbolComputedProperty(node.name, ts.Diagnostics.A_computed_property_name_in_a_type_literal_must_directly_refer_to_a_built_in_symbol); + return node.parent && node.parent.kind === 194 /* ExpressionWithTypeArguments */; + } + function forEachEnclosingClass(node, callback) { + var result; + while (true) { + node = ts.getContainingClass(node); + if (!node) + break; + if (result = callback(node)) + break; } + return result; } - function checkGrammarBreakOrContinueStatement(node) { - var current = node; - while (current) { - if (ts.isFunctionLike(current)) { - return grammarErrorOnNode(node, ts.Diagnostics.Jump_target_cannot_cross_function_boundary); - } - switch (current.kind) { - case 214 /* LabeledStatement */: - if (node.label && current.label.text === node.label.text) { - // found matching label - verify that label usage is correct - // continue can only target labels that are on iteration statements - var isMisplacedContinueLabel = node.kind === 209 /* ContinueStatement */ - && !ts.isIterationStatement(current.statement, /*lookInLabeledStatement*/ true); - if (isMisplacedContinueLabel) { - return grammarErrorOnNode(node, ts.Diagnostics.A_continue_statement_can_only_jump_to_a_label_of_an_enclosing_iteration_statement); - } - return false; - } - break; - case 213 /* SwitchStatement */: - if (node.kind === 210 /* BreakStatement */ && !node.label) { - // unlabeled break within switch statement - ok - return false; - } - break; - default: - if (ts.isIterationStatement(current, /*lookInLabeledStatement*/ false) && !node.label) { - // unlabeled break or continue within iteration statement - ok - return false; - } - break; - } - current = current.parent; + function isNodeWithinClass(node, classDeclaration) { + return !!forEachEnclosingClass(node, function (n) { return n === classDeclaration; }); + } + function getLeftSideOfImportEqualsOrExportAssignment(nodeOnRightSide) { + while (nodeOnRightSide.parent.kind === 139 /* QualifiedName */) { + nodeOnRightSide = nodeOnRightSide.parent; } - if (node.label) { - var message = node.kind === 210 /* BreakStatement */ - ? ts.Diagnostics.A_break_statement_can_only_jump_to_a_label_of_an_enclosing_statement - : ts.Diagnostics.A_continue_statement_can_only_jump_to_a_label_of_an_enclosing_iteration_statement; - return grammarErrorOnNode(node, message); + if (nodeOnRightSide.parent.kind === 229 /* ImportEqualsDeclaration */) { + return nodeOnRightSide.parent.moduleReference === nodeOnRightSide && nodeOnRightSide.parent; } - else { - var message = node.kind === 210 /* BreakStatement */ - ? ts.Diagnostics.A_break_statement_can_only_be_used_within_an_enclosing_iteration_or_switch_statement - : ts.Diagnostics.A_continue_statement_can_only_be_used_within_an_enclosing_iteration_statement; - return grammarErrorOnNode(node, message); + if (nodeOnRightSide.parent.kind === 235 /* ExportAssignment */) { + return nodeOnRightSide.parent.expression === nodeOnRightSide && nodeOnRightSide.parent; } + return undefined; } - function checkGrammarBindingElement(node) { - if (node.dotDotDotToken) { - var elements = node.parent.elements; - if (node !== ts.lastOrUndefined(elements)) { - return grammarErrorOnNode(node, ts.Diagnostics.A_rest_element_must_be_last_in_an_array_destructuring_pattern); + function isInRightSideOfImportOrExportAssignment(node) { + return getLeftSideOfImportEqualsOrExportAssignment(node) !== undefined; + } + function getSymbolOfEntityNameOrPropertyAccessExpression(entityName) { + if (ts.isDeclarationName(entityName)) { + return getSymbolOfNode(entityName.parent); + } + if (ts.isInJavaScriptFile(entityName) && entityName.parent.kind === 172 /* PropertyAccessExpression */) { + var specialPropertyAssignmentKind = ts.getSpecialPropertyAssignmentKind(entityName.parent.parent); + switch (specialPropertyAssignmentKind) { + case 1 /* ExportsProperty */: + case 3 /* PrototypeProperty */: + return getSymbolOfNode(entityName.parent); + case 4 /* ThisProperty */: + case 2 /* ModuleExports */: + return getSymbolOfNode(entityName.parent.parent); + default: } - if (node.name.kind === 168 /* ArrayBindingPattern */ || node.name.kind === 167 /* ObjectBindingPattern */) { - return grammarErrorOnNode(node.name, ts.Diagnostics.A_rest_element_cannot_contain_a_binding_pattern); + } + if (entityName.parent.kind === 235 /* ExportAssignment */ && ts.isEntityNameExpression(entityName)) { + return resolveEntityName(entityName, + /*all meanings*/ 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */ | 8388608 /* Alias */); + } + if (entityName.kind !== 172 /* PropertyAccessExpression */ && isInRightSideOfImportOrExportAssignment(entityName)) { + // Since we already checked for ExportAssignment, this really could only be an Import + var importEqualsDeclaration = ts.getAncestor(entityName, 229 /* ImportEqualsDeclaration */); + ts.Debug.assert(importEqualsDeclaration !== undefined); + return getSymbolOfPartOfRightHandSideOfImportEquals(entityName, importEqualsDeclaration, /*dontResolveAlias*/ true); + } + if (ts.isRightSideOfQualifiedNameOrPropertyAccess(entityName)) { + entityName = entityName.parent; + } + if (isHeritageClauseElementIdentifier(entityName)) { + var meaning = 0 /* None */; + // In an interface or class, we're definitely interested in a type. + if (entityName.parent.kind === 194 /* ExpressionWithTypeArguments */) { + meaning = 793064 /* Type */; + // In a class 'extends' clause we are also looking for a value. + if (ts.isExpressionWithTypeArgumentsInClassExtendsClause(entityName.parent)) { + meaning |= 107455 /* Value */; + } } - if (node.initializer) { - // Error on equals token which immediate precedes the initializer - return grammarErrorAtPos(ts.getSourceFileOfNode(node), node.initializer.pos - 1, 1, ts.Diagnostics.A_rest_element_cannot_have_an_initializer); + else { + meaning = 1920 /* Namespace */; } + meaning |= 8388608 /* Alias */; + return resolveEntityName(entityName, meaning); } - } - function isStringOrNumberLiteralExpression(expr) { - return expr.kind === 9 /* StringLiteral */ || expr.kind === 8 /* NumericLiteral */ || - expr.kind === 185 /* PrefixUnaryExpression */ && expr.operator === 36 /* MinusToken */ && - expr.operand.kind === 8 /* NumericLiteral */; - } - function checkGrammarVariableDeclaration(node) { - if (node.parent.parent.kind !== 207 /* ForInStatement */ && node.parent.parent.kind !== 208 /* ForOfStatement */) { - if (ts.isInAmbientContext(node)) { - if (node.initializer) { - if (ts.isConst(node) && !node.type) { - if (!isStringOrNumberLiteralExpression(node.initializer)) { - return grammarErrorOnNode(node.initializer, ts.Diagnostics.A_const_initializer_in_an_ambient_context_must_be_a_string_or_numeric_literal); - } - } - else { - // Error on equals token which immediate precedes the initializer - var equalsTokenLength = "=".length; - return grammarErrorAtPos(ts.getSourceFileOfNode(node), node.initializer.pos - equalsTokenLength, equalsTokenLength, ts.Diagnostics.Initializers_are_not_allowed_in_ambient_contexts); - } - } - if (node.initializer && !(ts.isConst(node) && isStringOrNumberLiteralExpression(node.initializer))) { - // Error on equals token which immediate precedes the initializer - var equalsTokenLength = "=".length; - return grammarErrorAtPos(ts.getSourceFileOfNode(node), node.initializer.pos - equalsTokenLength, equalsTokenLength, ts.Diagnostics.Initializers_are_not_allowed_in_ambient_contexts); + else if (ts.isPartOfExpression(entityName)) { + if (ts.nodeIsMissing(entityName)) { + // Missing entity name. + return undefined; + } + if (entityName.kind === 69 /* Identifier */) { + if (ts.isJSXTagName(entityName) && isJsxIntrinsicIdentifier(entityName)) { + return getIntrinsicTagSymbol(entityName.parent); } + return resolveEntityName(entityName, 107455 /* Value */, /*ignoreErrors*/ false, /*dontResolveAlias*/ true); } - else if (!node.initializer) { - if (ts.isBindingPattern(node.name) && !ts.isBindingPattern(node.parent)) { - return grammarErrorOnNode(node, ts.Diagnostics.A_destructuring_declaration_must_have_an_initializer); + else if (entityName.kind === 172 /* PropertyAccessExpression */) { + var symbol = getNodeLinks(entityName).resolvedSymbol; + if (!symbol) { + checkPropertyAccessExpression(entityName); } - if (ts.isConst(node)) { - return grammarErrorOnNode(node, ts.Diagnostics.const_declarations_must_be_initialized); + return getNodeLinks(entityName).resolvedSymbol; + } + else if (entityName.kind === 139 /* QualifiedName */) { + var symbol = getNodeLinks(entityName).resolvedSymbol; + if (!symbol) { + checkQualifiedName(entityName); } + return getNodeLinks(entityName).resolvedSymbol; } } - var checkLetConstNames = (ts.isLet(node) || ts.isConst(node)); - // 1. LexicalDeclaration : LetOrConst BindingList ; - // It is a Syntax Error if the BoundNames of BindingList contains "let". - // 2. ForDeclaration: ForDeclaration : LetOrConst ForBinding - // It is a Syntax Error if the BoundNames of ForDeclaration contains "let". - // It is a SyntaxError if a VariableDeclaration or VariableDeclarationNoIn occurs within strict code - // and its Identifier is eval or arguments - return checkLetConstNames && checkGrammarNameInLetOrConstDeclarations(node.name); - } - function checkGrammarNameInLetOrConstDeclarations(name) { - if (name.kind === 69 /* Identifier */) { - if (name.originalKeywordKind === 108 /* LetKeyword */) { - return grammarErrorOnNode(name, ts.Diagnostics.let_is_not_allowed_to_be_used_as_a_name_in_let_or_const_declarations); - } + else if (isTypeReferenceIdentifier(entityName)) { + var meaning = (entityName.parent.kind === 155 /* TypeReference */ || entityName.parent.kind === 267 /* JSDocTypeReference */) ? 793064 /* Type */ : 1920 /* Namespace */; + return resolveEntityName(entityName, meaning, /*ignoreErrors*/ false, /*dontResolveAlias*/ true); } - else { - var elements = name.elements; - for (var _i = 0, elements_2 = elements; _i < elements_2.length; _i++) { - var element = elements_2[_i]; - if (!ts.isOmittedExpression(element)) { - checkGrammarNameInLetOrConstDeclarations(element.name); - } - } + else if (entityName.parent.kind === 246 /* JsxAttribute */) { + return getJsxAttributePropertySymbol(entityName.parent); + } + if (entityName.parent.kind === 154 /* TypePredicate */) { + return resolveEntityName(entityName, /*meaning*/ 1 /* FunctionScopedVariable */); } + // Do we want to return undefined here? + return undefined; } - function checkGrammarVariableDeclarationList(declarationList) { - var declarations = declarationList.declarations; - if (checkGrammarForDisallowedTrailingComma(declarationList.declarations)) { - return true; + function getSymbolAtLocation(node) { + if (node.kind === 256 /* SourceFile */) { + return ts.isExternalModule(node) ? getMergedSymbol(node.symbol) : undefined; } - if (!declarationList.declarations.length) { - return grammarErrorAtPos(ts.getSourceFileOfNode(declarationList), declarations.pos, declarations.end - declarations.pos, ts.Diagnostics.Variable_declaration_list_cannot_be_empty); + if (isInsideWithStatementBody(node)) { + // We cannot answer semantic questions within a with block, do not proceed any further + return undefined; } - } - function allowLetAndConstDeclarations(parent) { - switch (parent.kind) { - case 203 /* IfStatement */: - case 204 /* DoStatement */: - case 205 /* WhileStatement */: - case 212 /* WithStatement */: - case 206 /* ForStatement */: - case 207 /* ForInStatement */: - case 208 /* ForOfStatement */: - return false; - case 214 /* LabeledStatement */: - return allowLetAndConstDeclarations(parent.parent); + if (ts.isDeclarationName(node)) { + // This is a declaration, call getSymbolOfNode + return getSymbolOfNode(node.parent); } - return true; - } - function checkGrammarForDisallowedLetOrConstStatement(node) { - if (!allowLetAndConstDeclarations(node.parent)) { - if (ts.isLet(node.declarationList)) { - return grammarErrorOnNode(node, ts.Diagnostics.let_declarations_can_only_be_declared_inside_a_block); + else if (ts.isLiteralComputedPropertyDeclarationName(node)) { + return getSymbolOfNode(node.parent.parent); + } + if (node.kind === 69 /* Identifier */) { + if (isInRightSideOfImportOrExportAssignment(node)) { + return getSymbolOfEntityNameOrPropertyAccessExpression(node); } - else if (ts.isConst(node.declarationList)) { - return grammarErrorOnNode(node, ts.Diagnostics.const_declarations_can_only_be_declared_inside_a_block); + else if (node.parent.kind === 169 /* BindingElement */ && + node.parent.parent.kind === 167 /* ObjectBindingPattern */ && + node === node.parent.propertyName) { + var typeOfPattern = getTypeOfNode(node.parent.parent); + var propertyDeclaration = typeOfPattern && getPropertyOfType(typeOfPattern, node.text); + if (propertyDeclaration) { + return propertyDeclaration; + } } } - } - function hasParseDiagnostics(sourceFile) { - return sourceFile.parseDiagnostics.length > 0; - } - function grammarErrorOnFirstToken(node, message, arg0, arg1, arg2) { - var sourceFile = ts.getSourceFileOfNode(node); - if (!hasParseDiagnostics(sourceFile)) { - var span_4 = ts.getSpanOfTokenAtPosition(sourceFile, node.pos); - diagnostics.add(ts.createFileDiagnostic(sourceFile, span_4.start, span_4.length, message, arg0, arg1, arg2)); - return true; + switch (node.kind) { + case 69 /* Identifier */: + case 172 /* PropertyAccessExpression */: + case 139 /* QualifiedName */: + return getSymbolOfEntityNameOrPropertyAccessExpression(node); + case 97 /* ThisKeyword */: + var container = ts.getThisContainer(node, /*includeArrowFunctions*/ false); + if (ts.isFunctionLike(container)) { + var sig = getSignatureFromDeclaration(container); + if (sig.thisParameter) { + return sig.thisParameter; + } + } + // fallthrough + case 95 /* SuperKeyword */: + var type = ts.isPartOfExpression(node) ? checkExpression(node) : getTypeFromTypeNode(node); + return type.symbol; + case 165 /* ThisType */: + return getTypeFromTypeNode(node).symbol; + case 121 /* ConstructorKeyword */: + // constructor keyword for an overload, should take us to the definition if it exist + var constructorDeclaration = node.parent; + if (constructorDeclaration && constructorDeclaration.kind === 148 /* Constructor */) { + return constructorDeclaration.parent.symbol; + } + return undefined; + case 9 /* StringLiteral */: + // External module name in an import declaration + if ((ts.isExternalModuleImportEqualsDeclaration(node.parent.parent) && + ts.getExternalModuleImportEqualsDeclarationExpression(node.parent.parent) === node) || + ((node.parent.kind === 230 /* ImportDeclaration */ || node.parent.kind === 236 /* ExportDeclaration */) && + node.parent.moduleSpecifier === node)) { + return resolveExternalModuleName(node, node); + } + if (ts.isInJavaScriptFile(node) && ts.isRequireCall(node.parent, /*checkArgumentIsStringLiteral*/ false)) { + return resolveExternalModuleName(node, node); + } + // Fall through + case 8 /* NumericLiteral */: + // index access + if (node.parent.kind === 173 /* ElementAccessExpression */ && node.parent.argumentExpression === node) { + var objectType = checkExpression(node.parent.expression); + if (objectType === unknownType) + return undefined; + var apparentType = getApparentType(objectType); + if (apparentType === unknownType) + return undefined; + return getPropertyOfType(apparentType, node.text); + } + break; } + return undefined; } - function grammarErrorAtPos(sourceFile, start, length, message, arg0, arg1, arg2) { - if (!hasParseDiagnostics(sourceFile)) { - diagnostics.add(ts.createFileDiagnostic(sourceFile, start, length, message, arg0, arg1, arg2)); - return true; + function getShorthandAssignmentValueSymbol(location) { + // The function returns a value symbol of an identifier in the short-hand property assignment. + // This is necessary as an identifier in short-hand property assignment can contains two meaning: + // property name and property value. + if (location && location.kind === 254 /* ShorthandPropertyAssignment */) { + return resolveEntityName(location.name, 107455 /* Value */ | 8388608 /* Alias */); } + return undefined; } - function grammarErrorOnNode(node, message, arg0, arg1, arg2) { - var sourceFile = ts.getSourceFileOfNode(node); - if (!hasParseDiagnostics(sourceFile)) { - diagnostics.add(ts.createDiagnosticForNode(node, message, arg0, arg1, arg2)); - return true; - } + /** Returns the target of an export specifier without following aliases */ + function getExportSpecifierLocalTargetSymbol(node) { + return node.parent.parent.moduleSpecifier ? + getExternalModuleMember(node.parent.parent, node) : + resolveEntityName(node.propertyName || node.name, 107455 /* Value */ | 793064 /* Type */ | 1920 /* Namespace */ | 8388608 /* Alias */); } - function checkGrammarConstructorTypeParameters(node) { - if (node.typeParameters) { - return grammarErrorAtPos(ts.getSourceFileOfNode(node), node.typeParameters.pos, node.typeParameters.end - node.typeParameters.pos, ts.Diagnostics.Type_parameters_cannot_appear_on_a_constructor_declaration); + function getTypeOfNode(node) { + if (isInsideWithStatementBody(node)) { + // We cannot answer semantic questions within a with block, do not proceed any further + return unknownType; } - } - function checkGrammarConstructorTypeAnnotation(node) { - if (node.type) { - return grammarErrorOnNode(node.type, ts.Diagnostics.Type_annotation_cannot_appear_on_a_constructor_declaration); + if (ts.isPartOfTypeNode(node)) { + return getTypeFromTypeNode(node); } - } - function checkGrammarProperty(node) { - if (ts.isClassLike(node.parent)) { - if (checkGrammarForNonSymbolComputedProperty(node.name, ts.Diagnostics.A_computed_property_name_in_a_class_property_declaration_must_directly_refer_to_a_built_in_symbol)) { - return true; - } + if (ts.isPartOfExpression(node)) { + return getTypeOfExpression(node); } - else if (node.parent.kind === 222 /* InterfaceDeclaration */) { - if (checkGrammarForNonSymbolComputedProperty(node.name, ts.Diagnostics.A_computed_property_name_in_an_interface_must_directly_refer_to_a_built_in_symbol)) { - return true; - } - if (node.initializer) { - return grammarErrorOnNode(node.initializer, ts.Diagnostics.An_interface_property_cannot_have_an_initializer); - } + if (ts.isExpressionWithTypeArgumentsInClassExtendsClause(node)) { + // A SyntaxKind.ExpressionWithTypeArguments is considered a type node, except when it occurs in the + // extends clause of a class. We handle that case here. + return getBaseTypes(getDeclaredTypeOfSymbol(getSymbolOfNode(node.parent.parent)))[0]; } - else if (node.parent.kind === 159 /* TypeLiteral */) { - if (checkGrammarForNonSymbolComputedProperty(node.name, ts.Diagnostics.A_computed_property_name_in_a_type_literal_must_directly_refer_to_a_built_in_symbol)) { - return true; - } - if (node.initializer) { - return grammarErrorOnNode(node.initializer, ts.Diagnostics.A_type_literal_property_cannot_have_an_initializer); - } + if (isTypeDeclaration(node)) { + // In this case, we call getSymbolOfNode instead of getSymbolAtLocation because it is a declaration + var symbol = getSymbolOfNode(node); + return getDeclaredTypeOfSymbol(symbol); } - if (ts.isInAmbientContext(node) && node.initializer) { - return grammarErrorOnFirstToken(node.initializer, ts.Diagnostics.Initializers_are_not_allowed_in_ambient_contexts); + if (isTypeDeclarationName(node)) { + var symbol = getSymbolAtLocation(node); + return symbol && getDeclaredTypeOfSymbol(symbol); } - } - function checkGrammarTopLevelElementForRequiredDeclareModifier(node) { - // A declare modifier is required for any top level .d.ts declaration except export=, export default, export as namespace - // interfaces and imports categories: - // - // DeclarationElement: - // ExportAssignment - // export_opt InterfaceDeclaration - // export_opt TypeAliasDeclaration - // export_opt ImportDeclaration - // export_opt ExternalImportDeclaration - // export_opt AmbientDeclaration - // - // TODO: The spec needs to be amended to reflect this grammar. - if (node.kind === 222 /* InterfaceDeclaration */ || - node.kind === 223 /* TypeAliasDeclaration */ || - node.kind === 230 /* ImportDeclaration */ || - node.kind === 229 /* ImportEqualsDeclaration */ || - node.kind === 236 /* ExportDeclaration */ || - node.kind === 235 /* ExportAssignment */ || - node.kind === 228 /* NamespaceExportDeclaration */ || - ts.getModifierFlags(node) & (2 /* Ambient */ | 1 /* Export */ | 512 /* Default */)) { - return false; + if (ts.isDeclaration(node)) { + // In this case, we call getSymbolOfNode instead of getSymbolAtLocation because it is a declaration + var symbol = getSymbolOfNode(node); + return getTypeOfSymbol(symbol); } - return grammarErrorOnFirstToken(node, ts.Diagnostics.A_declare_modifier_is_required_for_a_top_level_declaration_in_a_d_ts_file); - } - function checkGrammarTopLevelElementsForRequiredDeclareModifier(file) { - for (var _i = 0, _a = file.statements; _i < _a.length; _i++) { - var decl = _a[_i]; - if (ts.isDeclaration(decl) || decl.kind === 200 /* VariableStatement */) { - if (checkGrammarTopLevelElementForRequiredDeclareModifier(decl)) { - return true; - } - } + if (ts.isDeclarationName(node)) { + var symbol = getSymbolAtLocation(node); + return symbol && getTypeOfSymbol(symbol); } + if (ts.isBindingPattern(node)) { + return getTypeForVariableLikeDeclaration(node.parent, /*includeOptionality*/ true); + } + if (isInRightSideOfImportOrExportAssignment(node)) { + var symbol = getSymbolAtLocation(node); + var declaredType = symbol && getDeclaredTypeOfSymbol(symbol); + return declaredType !== unknownType ? declaredType : getTypeOfSymbol(symbol); + } + return unknownType; } - function checkGrammarSourceFile(node) { - return ts.isInAmbientContext(node) && checkGrammarTopLevelElementsForRequiredDeclareModifier(node); - } - function checkGrammarStatementInAmbientContext(node) { - if (ts.isInAmbientContext(node)) { - // An accessors is already reported about the ambient context - if (isAccessor(node.parent.kind)) { - return getNodeLinks(node).hasReportedStatementInAmbientContext = true; + // Gets the type of object literal or array literal of destructuring assignment. + // { a } from + // for ( { a } of elems) { + // } + // [ a ] from + // [a] = [ some array ...] + function getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(expr) { + ts.Debug.assert(expr.kind === 171 /* ObjectLiteralExpression */ || expr.kind === 170 /* ArrayLiteralExpression */); + // If this is from "for of" + // for ( { a } of elems) { + // } + if (expr.parent.kind === 208 /* ForOfStatement */) { + var iteratedType = checkRightHandSideOfForOf(expr.parent.expression); + return checkDestructuringAssignment(expr, iteratedType || unknownType); + } + // If this is from "for" initializer + // for ({a } = elems[0];.....) { } + if (expr.parent.kind === 187 /* BinaryExpression */) { + var iteratedType = checkExpression(expr.parent.right); + return checkDestructuringAssignment(expr, iteratedType || unknownType); + } + // If this is from nested object binding pattern + // for ({ skills: { primary, secondary } } = multiRobot, i = 0; i < 1; i++) { + if (expr.parent.kind === 253 /* PropertyAssignment */) { + var typeOfParentObjectLiteral = getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(expr.parent.parent); + return checkObjectLiteralDestructuringPropertyAssignment(typeOfParentObjectLiteral || unknownType, expr.parent); + } + // Array literal assignment - array destructuring pattern + ts.Debug.assert(expr.parent.kind === 170 /* ArrayLiteralExpression */); + // [{ property1: p1, property2 }] = elems; + var typeOfArrayLiteral = getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(expr.parent); + var elementType = checkIteratedTypeOrElementType(typeOfArrayLiteral || unknownType, expr.parent, /*allowStringInput*/ false) || unknownType; + return checkArrayLiteralDestructuringElementAssignment(expr.parent, typeOfArrayLiteral, ts.indexOf(expr.parent.elements, expr), elementType || unknownType); + } + // Gets the property symbol corresponding to the property in destructuring assignment + // 'property1' from + // for ( { property1: a } of elems) { + // } + // 'property1' at location 'a' from: + // [a] = [ property1, property2 ] + function getPropertySymbolOfDestructuringAssignment(location) { + // Get the type of the object or array literal and then look for property of given name in the type + var typeOfObjectLiteral = getTypeOfArrayLiteralOrObjectLiteralDestructuringAssignment(location.parent.parent); + return typeOfObjectLiteral && getPropertyOfType(typeOfObjectLiteral, location.text); + } + function getTypeOfExpression(expr) { + if (ts.isRightSideOfQualifiedNameOrPropertyAccess(expr)) { + expr = expr.parent; + } + return getRegularTypeOfLiteralType(checkExpression(expr)); + } + /** + * Gets either the static or instance type of a class element, based on + * whether the element is declared as "static". + */ + function getParentTypeOfClassElement(node) { + var classSymbol = getSymbolOfNode(node.parent); + return ts.getModifierFlags(node) & 32 /* Static */ + ? getTypeOfSymbol(classSymbol) + : getDeclaredTypeOfSymbol(classSymbol); + } + // Return the list of properties of the given type, augmented with properties from Function + // if the type has call or construct signatures + function getAugmentedPropertiesOfType(type) { + type = getApparentType(type); + var propsByName = createSymbolTable(getPropertiesOfType(type)); + if (getSignaturesOfType(type, 0 /* Call */).length || getSignaturesOfType(type, 1 /* Construct */).length) { + ts.forEach(getPropertiesOfType(globalFunctionType), function (p) { + if (!propsByName[p.name]) { + propsByName[p.name] = p; + } + }); + } + return getNamedMembers(propsByName); + } + function getRootSymbols(symbol) { + if (symbol.flags & 268435456 /* SyntheticProperty */) { + var symbols_3 = []; + var name_23 = symbol.name; + ts.forEach(getSymbolLinks(symbol).containingType.types, function (t) { + var symbol = getPropertyOfType(t, name_23); + if (symbol) { + symbols_3.push(symbol); + } + }); + return symbols_3; + } + else if (symbol.flags & 67108864 /* Transient */) { + var target = void 0; + var next = symbol; + while (next = getSymbolLinks(next).target) { + target = next; } - // Find containing block which is either Block, ModuleBlock, SourceFile - var links = getNodeLinks(node); - if (!links.hasReportedStatementInAmbientContext && ts.isFunctionLike(node.parent)) { - return getNodeLinks(node).hasReportedStatementInAmbientContext = grammarErrorOnFirstToken(node, ts.Diagnostics.An_implementation_cannot_be_declared_in_ambient_contexts); + if (target) { + return [target]; } - // We are either parented by another statement, or some sort of block. - // If we're in a block, we only want to really report an error once - // to prevent noisiness. So use a bit on the block to indicate if - // this has already been reported, and don't report if it has. - // - if (node.parent.kind === 199 /* Block */ || node.parent.kind === 226 /* ModuleBlock */ || node.parent.kind === 256 /* SourceFile */) { - var links_1 = getNodeLinks(node.parent); - // Check if the containing block ever report this error - if (!links_1.hasReportedStatementInAmbientContext) { - return links_1.hasReportedStatementInAmbientContext = grammarErrorOnFirstToken(node, ts.Diagnostics.Statements_are_not_allowed_in_ambient_contexts); + } + return [symbol]; + } + // Emitter support + function isArgumentsLocalBinding(node) { + if (!ts.isGeneratedIdentifier(node)) { + node = ts.getParseTreeNode(node, ts.isIdentifier); + if (node) { + return getReferencedValueSymbol(node) === argumentsSymbol; + } + } + return false; + } + function moduleExportsSomeValue(moduleReferenceExpression) { + var moduleSymbol = resolveExternalModuleName(moduleReferenceExpression.parent, moduleReferenceExpression); + if (!moduleSymbol || ts.isShorthandAmbientModuleSymbol(moduleSymbol)) { + // If the module is not found or is shorthand, assume that it may export a value. + return true; + } + var hasExportAssignment = hasExportAssignmentSymbol(moduleSymbol); + // if module has export assignment then 'resolveExternalModuleSymbol' will return resolved symbol for export assignment + // otherwise it will return moduleSymbol itself + moduleSymbol = resolveExternalModuleSymbol(moduleSymbol); + var symbolLinks = getSymbolLinks(moduleSymbol); + if (symbolLinks.exportsSomeValue === undefined) { + // for export assignments - check if resolved symbol for RHS is itself a value + // otherwise - check if at least one export is value + symbolLinks.exportsSomeValue = hasExportAssignment + ? !!(moduleSymbol.flags & 107455 /* Value */) + : ts.forEachProperty(getExportsOfModule(moduleSymbol), isValue); + } + return symbolLinks.exportsSomeValue; + function isValue(s) { + s = resolveSymbol(s); + return s && !!(s.flags & 107455 /* Value */); + } + } + function isNameOfModuleOrEnumDeclaration(node) { + var parent = node.parent; + return ts.isModuleOrEnumDeclaration(parent) && node === parent.name; + } + // When resolved as an expression identifier, if the given node references an exported entity, return the declaration + // node of the exported entity's container. Otherwise, return undefined. + function getReferencedExportContainer(node, prefixLocals) { + node = ts.getParseTreeNode(node, ts.isIdentifier); + if (node) { + // When resolving the export container for the name of a module or enum + // declaration, we need to start resolution at the declaration's container. + // Otherwise, we could incorrectly resolve the export container as the + // declaration if it contains an exported member with the same name. + var symbol = getReferencedValueSymbol(node, /*startInDeclarationContainer*/ isNameOfModuleOrEnumDeclaration(node)); + if (symbol) { + if (symbol.flags & 1048576 /* ExportValue */) { + // If we reference an exported entity within the same module declaration, then whether + // we prefix depends on the kind of entity. SymbolFlags.ExportHasLocal encompasses all the + // kinds that we do NOT prefix. + var exportSymbol = getMergedSymbol(symbol.exportSymbol); + if (!prefixLocals && exportSymbol.flags & 944 /* ExportHasLocal */) { + return undefined; + } + symbol = exportSymbol; + } + var parentSymbol = getParentOfSymbol(symbol); + if (parentSymbol) { + if (parentSymbol.flags & 512 /* ValueModule */ && parentSymbol.valueDeclaration.kind === 256 /* SourceFile */) { + var symbolFile = parentSymbol.valueDeclaration; + var referenceFile = ts.getSourceFileOfNode(node); + // If `node` accesses an export and that export isn't in the same file, then symbol is a namespace export, so return undefined. + var symbolIsUmdExport = symbolFile !== referenceFile; + return symbolIsUmdExport ? undefined : symbolFile; + } + for (var n = node.parent; n; n = n.parent) { + if (ts.isModuleOrEnumDeclaration(n) && getSymbolOfNode(n) === parentSymbol) { + return n; + } + } } } - else { + } + } + // When resolved as an expression identifier, if the given node references an import, return the declaration of + // that import. Otherwise, return undefined. + function getReferencedImportDeclaration(node) { + node = ts.getParseTreeNode(node, ts.isIdentifier); + if (node) { + var symbol = getReferencedValueSymbol(node); + if (symbol && symbol.flags & 8388608 /* Alias */) { + return getDeclarationOfAliasSymbol(symbol); } } + return undefined; } - function checkGrammarNumericLiteral(node) { - // Grammar checking - if (node.isOctalLiteral && languageVersion >= 1 /* ES5 */) { - return grammarErrorOnNode(node, ts.Diagnostics.Octal_literals_are_not_available_when_targeting_ECMAScript_5_and_higher); + function isSymbolOfDeclarationWithCollidingName(symbol) { + if (symbol.flags & 418 /* BlockScoped */) { + var links = getSymbolLinks(symbol); + if (links.isDeclarationWithCollidingName === undefined) { + var container = ts.getEnclosingBlockScopeContainer(symbol.valueDeclaration); + if (ts.isStatementWithLocals(container)) { + var nodeLinks_1 = getNodeLinks(symbol.valueDeclaration); + if (!!resolveName(container.parent, symbol.name, 107455 /* Value */, /*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined)) { + // redeclaration - always should be renamed + links.isDeclarationWithCollidingName = true; + } + else if (nodeLinks_1.flags & 131072 /* CapturedBlockScopedBinding */) { + // binding is captured in the function + // should be renamed if: + // - binding is not top level - top level bindings never collide with anything + // AND + // - binding is not declared in loop, should be renamed to avoid name reuse across siblings + // let a, b + // { let x = 1; a = () => x; } + // { let x = 100; b = () => x; } + // console.log(a()); // should print '1' + // console.log(b()); // should print '100' + // OR + // - binding is declared inside loop but not in inside initializer of iteration statement or directly inside loop body + // * variables from initializer are passed to rewritten loop body as parameters so they are not captured directly + // * variables that are declared immediately in loop body will become top level variable after loop is rewritten and thus + // they will not collide with anything + var isDeclaredInLoop = nodeLinks_1.flags & 262144 /* BlockScopedBindingInLoop */; + var inLoopInitializer = ts.isIterationStatement(container, /*lookInLabeledStatements*/ false); + var inLoopBodyBlock = container.kind === 199 /* Block */ && ts.isIterationStatement(container.parent, /*lookInLabeledStatements*/ false); + links.isDeclarationWithCollidingName = !ts.isBlockScopedContainerTopLevel(container) && (!isDeclaredInLoop || (!inLoopInitializer && !inLoopBodyBlock)); + } + else { + links.isDeclarationWithCollidingName = false; + } + } + } + return links.isDeclarationWithCollidingName; } + return false; } - function grammarErrorAfterFirstToken(node, message, arg0, arg1, arg2) { - var sourceFile = ts.getSourceFileOfNode(node); - if (!hasParseDiagnostics(sourceFile)) { - var span_5 = ts.getSpanOfTokenAtPosition(sourceFile, node.pos); - diagnostics.add(ts.createFileDiagnostic(sourceFile, ts.textSpanEnd(span_5), /*length*/ 0, message, arg0, arg1, arg2)); + // When resolved as an expression identifier, if the given node references a nested block scoped entity with + // a name that either hides an existing name or might hide it when compiled downlevel, + // return the declaration of that entity. Otherwise, return undefined. + function getReferencedDeclarationWithCollidingName(node) { + if (!ts.isGeneratedIdentifier(node)) { + node = ts.getParseTreeNode(node, ts.isIdentifier); + if (node) { + var symbol = getReferencedValueSymbol(node); + if (symbol && isSymbolOfDeclarationWithCollidingName(symbol)) { + return symbol.valueDeclaration; + } + } + } + return undefined; + } + // Return true if the given node is a declaration of a nested block scoped entity with a name that either hides an + // existing name or might hide a name when compiled downlevel + function isDeclarationWithCollidingName(node) { + node = ts.getParseTreeNode(node, ts.isDeclaration); + if (node) { + var symbol = getSymbolOfNode(node); + if (symbol) { + return isSymbolOfDeclarationWithCollidingName(symbol); + } + } + return false; + } + function isValueAliasDeclaration(node) { + node = ts.getParseTreeNode(node); + if (node === undefined) { + // A synthesized node comes from an emit transformation and is always a value. + return true; + } + switch (node.kind) { + case 229 /* ImportEqualsDeclaration */: + case 231 /* ImportClause */: + case 232 /* NamespaceImport */: + case 234 /* ImportSpecifier */: + case 238 /* ExportSpecifier */: + return isAliasResolvedToValue(getSymbolOfNode(node) || unknownSymbol); + case 236 /* ExportDeclaration */: + var exportClause = node.exportClause; + return exportClause && ts.forEach(exportClause.elements, isValueAliasDeclaration); + case 235 /* ExportAssignment */: + return node.expression + && node.expression.kind === 69 /* Identifier */ + ? isAliasResolvedToValue(getSymbolOfNode(node) || unknownSymbol) + : true; + } + return false; + } + function isTopLevelValueImportEqualsWithEntityName(node) { + node = ts.getParseTreeNode(node, ts.isImportEqualsDeclaration); + if (node === undefined || node.parent.kind !== 256 /* SourceFile */ || !ts.isInternalModuleImportEqualsDeclaration(node)) { + // parent is not source file or it is not reference to internal module + return false; + } + var isValue = isAliasResolvedToValue(getSymbolOfNode(node)); + return isValue && node.moduleReference && !ts.nodeIsMissing(node.moduleReference); + } + function isAliasResolvedToValue(symbol) { + var target = resolveAlias(symbol); + if (target === unknownSymbol) { return true; } + // const enums and modules that contain only const enums are not considered values from the emit perspective + // unless 'preserveConstEnums' option is set to true + return target.flags & 107455 /* Value */ && + (compilerOptions.preserveConstEnums || !isConstEnumOrConstEnumOnlyModule(target)); } - function getAmbientModules() { - var result = []; - for (var sym in globals) { - if (ambientModuleSymbolRegex.test(sym)) { - result.push(globals[sym]); + function isConstEnumOrConstEnumOnlyModule(s) { + return isConstEnumSymbol(s) || s.constEnumOnlyModule; + } + function isReferencedAliasDeclaration(node, checkChildren) { + node = ts.getParseTreeNode(node); + // Purely synthesized nodes are always emitted. + if (node === undefined) { + return true; + } + if (ts.isAliasSymbolDeclaration(node)) { + var symbol = getSymbolOfNode(node); + if (symbol && getSymbolLinks(symbol).referenced) { + return true; } } - return result; + if (checkChildren) { + return ts.forEachChild(node, function (node) { return isReferencedAliasDeclaration(node, checkChildren); }); + } + return false; } - } - ts.createTypeChecker = createTypeChecker; -})(ts || (ts = {})); -/// -/// -/// -/* @internal */ -var ts; -(function (ts) { - ; - /** - * This map contains information about the shape of each Node in "types.ts" pertaining to how - * each node should be traversed during a transformation. - * - * Each edge corresponds to a property in a Node subtype that should be traversed when visiting - * each child. The properties are assigned in the order in which traversal should occur. - * - * We only add entries for nodes that do not have a create/update pair defined in factory.ts - * - * NOTE: This needs to be kept up to date with changes to nodes in "types.ts". Currently, this - * map is not comprehensive. Only node edges relevant to tree transformation are - * currently defined. We may extend this to be more comprehensive, and eventually - * supplant the existing `forEachChild` implementation if performance is not - * significantly impacted. - */ - var nodeEdgeTraversalMap = ts.createMap((_a = {}, - _a[139 /* QualifiedName */] = [ - { name: "left", test: ts.isEntityName }, - { name: "right", test: ts.isIdentifier } - ], - _a[143 /* Decorator */] = [ - { name: "expression", test: ts.isLeftHandSideExpression } - ], - _a[177 /* TypeAssertionExpression */] = [ - { name: "type", test: ts.isTypeNode }, - { name: "expression", test: ts.isUnaryExpression } - ], - _a[195 /* AsExpression */] = [ - { name: "expression", test: ts.isExpression }, - { name: "type", test: ts.isTypeNode } - ], - _a[196 /* NonNullExpression */] = [ - { name: "expression", test: ts.isLeftHandSideExpression } - ], - _a[224 /* EnumDeclaration */] = [ - { name: "decorators", test: ts.isDecorator }, - { name: "modifiers", test: ts.isModifier }, - { name: "name", test: ts.isIdentifier }, - { name: "members", test: ts.isEnumMember } - ], - _a[225 /* ModuleDeclaration */] = [ - { name: "decorators", test: ts.isDecorator }, - { name: "modifiers", test: ts.isModifier }, - { name: "name", test: ts.isModuleName }, - { name: "body", test: ts.isModuleBody } - ], - _a[226 /* ModuleBlock */] = [ - { name: "statements", test: ts.isStatement } - ], - _a[229 /* ImportEqualsDeclaration */] = [ - { name: "decorators", test: ts.isDecorator }, - { name: "modifiers", test: ts.isModifier }, - { name: "name", test: ts.isIdentifier }, - { name: "moduleReference", test: ts.isModuleReference } - ], - _a[240 /* ExternalModuleReference */] = [ - { name: "expression", test: ts.isExpression, optional: true } - ], - _a[255 /* EnumMember */] = [ - { name: "name", test: ts.isPropertyName }, - { name: "initializer", test: ts.isExpression, optional: true, parenthesize: ts.parenthesizeExpressionForList } - ], - _a)); - function reduceNode(node, f, initial) { - return node ? f(initial, node) : initial; - } - /** - * Similar to `reduceLeft`, performs a reduction against each child of a node. - * NOTE: Unlike `forEachChild`, this does *not* visit every node. Only nodes added to the - * `nodeEdgeTraversalMap` above will be visited. - * - * @param node The node containing the children to reduce. - * @param f The callback function - * @param initial The initial value to supply to the reduction. - */ - function reduceEachChild(node, f, initial) { - if (node === undefined) { - return initial; + function isImplementationOfOverload(node) { + if (ts.nodeIsPresent(node.body)) { + var symbol = getSymbolOfNode(node); + var signaturesOfSymbol = getSignaturesOfSymbol(symbol); + // If this function body corresponds to function with multiple signature, it is implementation of overload + // e.g.: function foo(a: string): string; + // function foo(a: number): number; + // function foo(a: any) { // This is implementation of the overloads + // return a; + // } + return signaturesOfSymbol.length > 1 || + // If there is single signature for the symbol, it is overload if that signature isn't coming from the node + // e.g.: function foo(a: string): string; + // function foo(a: any) { // This is implementation of the overloads + // return a; + // } + (signaturesOfSymbol.length === 1 && signaturesOfSymbol[0].declaration !== node); + } + return false; } - var kind = node.kind; - // No need to visit nodes with no children. - if ((kind > 0 /* FirstToken */ && kind <= 138 /* LastToken */)) { - return initial; + function getNodeCheckFlags(node) { + node = ts.getParseTreeNode(node); + return node ? getNodeLinks(node).flags : undefined; } - // We do not yet support types. - if ((kind >= 154 /* TypePredicate */ && kind <= 166 /* LiteralType */)) { - return initial; + function getEnumMemberValue(node) { + computeEnumMemberValues(node.parent); + return getNodeLinks(node).enumMemberValue; } - var result = initial; - switch (node.kind) { - // Leaf nodes - case 198 /* SemicolonClassElement */: - case 201 /* EmptyStatement */: - case 193 /* OmittedExpression */: - case 217 /* DebuggerStatement */: - case 287 /* NotEmittedStatement */: - // No need to visit nodes with no children. - break; - // Names - case 140 /* ComputedPropertyName */: - result = reduceNode(node.expression, f, result); - break; - // Signature elements - case 142 /* Parameter */: - result = ts.reduceLeft(node.decorators, f, result); - result = ts.reduceLeft(node.modifiers, f, result); - result = reduceNode(node.name, f, result); - result = reduceNode(node.type, f, result); - result = reduceNode(node.initializer, f, result); - break; - case 143 /* Decorator */: - result = reduceNode(node.expression, f, result); - break; - // Type member - case 145 /* PropertyDeclaration */: - result = ts.reduceLeft(node.decorators, f, result); - result = ts.reduceLeft(node.modifiers, f, result); - result = reduceNode(node.name, f, result); - result = reduceNode(node.type, f, result); - result = reduceNode(node.initializer, f, result); - break; - case 147 /* MethodDeclaration */: - result = ts.reduceLeft(node.decorators, f, result); - result = ts.reduceLeft(node.modifiers, f, result); - result = reduceNode(node.name, f, result); - result = ts.reduceLeft(node.typeParameters, f, result); - result = ts.reduceLeft(node.parameters, f, result); - result = reduceNode(node.type, f, result); - result = reduceNode(node.body, f, result); - break; - case 148 /* Constructor */: - result = ts.reduceLeft(node.modifiers, f, result); - result = ts.reduceLeft(node.parameters, f, result); - result = reduceNode(node.body, f, result); - break; - case 149 /* GetAccessor */: - result = ts.reduceLeft(node.decorators, f, result); - result = ts.reduceLeft(node.modifiers, f, result); - result = reduceNode(node.name, f, result); - result = ts.reduceLeft(node.parameters, f, result); - result = reduceNode(node.type, f, result); - result = reduceNode(node.body, f, result); - break; - case 150 /* SetAccessor */: - result = ts.reduceLeft(node.decorators, f, result); - result = ts.reduceLeft(node.modifiers, f, result); - result = reduceNode(node.name, f, result); - result = ts.reduceLeft(node.parameters, f, result); - result = reduceNode(node.body, f, result); - break; - // Binding patterns - case 167 /* ObjectBindingPattern */: - case 168 /* ArrayBindingPattern */: - result = ts.reduceLeft(node.elements, f, result); - break; - case 169 /* BindingElement */: - result = reduceNode(node.propertyName, f, result); - result = reduceNode(node.name, f, result); - result = reduceNode(node.initializer, f, result); - break; - // Expression - case 170 /* ArrayLiteralExpression */: - result = ts.reduceLeft(node.elements, f, result); - break; - case 171 /* ObjectLiteralExpression */: - result = ts.reduceLeft(node.properties, f, result); - break; - case 172 /* PropertyAccessExpression */: - result = reduceNode(node.expression, f, result); - result = reduceNode(node.name, f, result); - break; - case 173 /* ElementAccessExpression */: - result = reduceNode(node.expression, f, result); - result = reduceNode(node.argumentExpression, f, result); - break; - case 174 /* CallExpression */: - result = reduceNode(node.expression, f, result); - result = ts.reduceLeft(node.typeArguments, f, result); - result = ts.reduceLeft(node.arguments, f, result); - break; - case 175 /* NewExpression */: - result = reduceNode(node.expression, f, result); - result = ts.reduceLeft(node.typeArguments, f, result); - result = ts.reduceLeft(node.arguments, f, result); - break; - case 176 /* TaggedTemplateExpression */: - result = reduceNode(node.tag, f, result); - result = reduceNode(node.template, f, result); - break; - case 179 /* FunctionExpression */: - result = ts.reduceLeft(node.modifiers, f, result); - result = reduceNode(node.name, f, result); - result = ts.reduceLeft(node.typeParameters, f, result); - result = ts.reduceLeft(node.parameters, f, result); - result = reduceNode(node.type, f, result); - result = reduceNode(node.body, f, result); - break; - case 180 /* ArrowFunction */: - result = ts.reduceLeft(node.modifiers, f, result); - result = ts.reduceLeft(node.typeParameters, f, result); - result = ts.reduceLeft(node.parameters, f, result); - result = reduceNode(node.type, f, result); - result = reduceNode(node.body, f, result); - break; - case 178 /* ParenthesizedExpression */: - case 181 /* DeleteExpression */: - case 182 /* TypeOfExpression */: - case 183 /* VoidExpression */: - case 184 /* AwaitExpression */: - case 190 /* YieldExpression */: - case 191 /* SpreadElementExpression */: - case 196 /* NonNullExpression */: - result = reduceNode(node.expression, f, result); - break; - case 185 /* PrefixUnaryExpression */: - case 186 /* PostfixUnaryExpression */: - result = reduceNode(node.operand, f, result); - break; - case 187 /* BinaryExpression */: - result = reduceNode(node.left, f, result); - result = reduceNode(node.right, f, result); - break; - case 188 /* ConditionalExpression */: - result = reduceNode(node.condition, f, result); - result = reduceNode(node.whenTrue, f, result); - result = reduceNode(node.whenFalse, f, result); - break; - case 189 /* TemplateExpression */: - result = reduceNode(node.head, f, result); - result = ts.reduceLeft(node.templateSpans, f, result); - break; - case 192 /* ClassExpression */: - result = ts.reduceLeft(node.modifiers, f, result); - result = reduceNode(node.name, f, result); - result = ts.reduceLeft(node.typeParameters, f, result); - result = ts.reduceLeft(node.heritageClauses, f, result); - result = ts.reduceLeft(node.members, f, result); - break; - case 194 /* ExpressionWithTypeArguments */: - result = reduceNode(node.expression, f, result); - result = ts.reduceLeft(node.typeArguments, f, result); - break; - // Misc - case 197 /* TemplateSpan */: - result = reduceNode(node.expression, f, result); - result = reduceNode(node.literal, f, result); - break; - // Element - case 199 /* Block */: - result = ts.reduceLeft(node.statements, f, result); - break; - case 200 /* VariableStatement */: - result = ts.reduceLeft(node.modifiers, f, result); - result = reduceNode(node.declarationList, f, result); - break; - case 202 /* ExpressionStatement */: - result = reduceNode(node.expression, f, result); - break; - case 203 /* IfStatement */: - result = reduceNode(node.expression, f, result); - result = reduceNode(node.thenStatement, f, result); - result = reduceNode(node.elseStatement, f, result); - break; - case 204 /* DoStatement */: - result = reduceNode(node.statement, f, result); - result = reduceNode(node.expression, f, result); - break; - case 205 /* WhileStatement */: - case 212 /* WithStatement */: - result = reduceNode(node.expression, f, result); - result = reduceNode(node.statement, f, result); - break; - case 206 /* ForStatement */: - result = reduceNode(node.initializer, f, result); - result = reduceNode(node.condition, f, result); - result = reduceNode(node.incrementor, f, result); - result = reduceNode(node.statement, f, result); - break; - case 207 /* ForInStatement */: - case 208 /* ForOfStatement */: - result = reduceNode(node.initializer, f, result); - result = reduceNode(node.expression, f, result); - result = reduceNode(node.statement, f, result); - break; - case 211 /* ReturnStatement */: - case 215 /* ThrowStatement */: - result = reduceNode(node.expression, f, result); - break; - case 213 /* SwitchStatement */: - result = reduceNode(node.expression, f, result); - result = reduceNode(node.caseBlock, f, result); - break; - case 214 /* LabeledStatement */: - result = reduceNode(node.label, f, result); - result = reduceNode(node.statement, f, result); - break; - case 216 /* TryStatement */: - result = reduceNode(node.tryBlock, f, result); - result = reduceNode(node.catchClause, f, result); - result = reduceNode(node.finallyBlock, f, result); - break; - case 218 /* VariableDeclaration */: - result = reduceNode(node.name, f, result); - result = reduceNode(node.type, f, result); - result = reduceNode(node.initializer, f, result); - break; - case 219 /* VariableDeclarationList */: - result = ts.reduceLeft(node.declarations, f, result); - break; - case 220 /* FunctionDeclaration */: - result = ts.reduceLeft(node.decorators, f, result); - result = ts.reduceLeft(node.modifiers, f, result); - result = reduceNode(node.name, f, result); - result = ts.reduceLeft(node.typeParameters, f, result); - result = ts.reduceLeft(node.parameters, f, result); - result = reduceNode(node.type, f, result); - result = reduceNode(node.body, f, result); - break; - case 221 /* ClassDeclaration */: - result = ts.reduceLeft(node.decorators, f, result); - result = ts.reduceLeft(node.modifiers, f, result); - result = reduceNode(node.name, f, result); - result = ts.reduceLeft(node.typeParameters, f, result); - result = ts.reduceLeft(node.heritageClauses, f, result); - result = ts.reduceLeft(node.members, f, result); - break; - case 227 /* CaseBlock */: - result = ts.reduceLeft(node.clauses, f, result); - break; - case 230 /* ImportDeclaration */: - result = ts.reduceLeft(node.decorators, f, result); - result = ts.reduceLeft(node.modifiers, f, result); - result = reduceNode(node.importClause, f, result); - result = reduceNode(node.moduleSpecifier, f, result); - break; - case 231 /* ImportClause */: - result = reduceNode(node.name, f, result); - result = reduceNode(node.namedBindings, f, result); - break; - case 232 /* NamespaceImport */: - result = reduceNode(node.name, f, result); - break; - case 233 /* NamedImports */: - case 237 /* NamedExports */: - result = ts.reduceLeft(node.elements, f, result); - break; - case 234 /* ImportSpecifier */: - case 238 /* ExportSpecifier */: - result = reduceNode(node.propertyName, f, result); - result = reduceNode(node.name, f, result); - break; - case 235 /* ExportAssignment */: - result = ts.reduceLeft(node.decorators, f, result); - result = ts.reduceLeft(node.modifiers, f, result); - result = reduceNode(node.expression, f, result); - break; - case 236 /* ExportDeclaration */: - result = ts.reduceLeft(node.decorators, f, result); - result = ts.reduceLeft(node.modifiers, f, result); - result = reduceNode(node.exportClause, f, result); - result = reduceNode(node.moduleSpecifier, f, result); - break; - // JSX - case 241 /* JsxElement */: - result = reduceNode(node.openingElement, f, result); - result = ts.reduceLeft(node.children, f, result); - result = reduceNode(node.closingElement, f, result); - break; - case 242 /* JsxSelfClosingElement */: - case 243 /* JsxOpeningElement */: - result = reduceNode(node.tagName, f, result); - result = ts.reduceLeft(node.attributes, f, result); - break; - case 245 /* JsxClosingElement */: - result = reduceNode(node.tagName, f, result); - break; - case 246 /* JsxAttribute */: - result = reduceNode(node.name, f, result); - result = reduceNode(node.initializer, f, result); - break; - case 247 /* JsxSpreadAttribute */: - result = reduceNode(node.expression, f, result); - break; - case 248 /* JsxExpression */: - result = reduceNode(node.expression, f, result); - break; - // Clauses - case 249 /* CaseClause */: - result = reduceNode(node.expression, f, result); - // fall-through - case 250 /* DefaultClause */: - result = ts.reduceLeft(node.statements, f, result); - break; - case 251 /* HeritageClause */: - result = ts.reduceLeft(node.types, f, result); - break; - case 252 /* CatchClause */: - result = reduceNode(node.variableDeclaration, f, result); - result = reduceNode(node.block, f, result); - break; - // Property assignments - case 253 /* PropertyAssignment */: - result = reduceNode(node.name, f, result); - result = reduceNode(node.initializer, f, result); - break; - case 254 /* ShorthandPropertyAssignment */: - result = reduceNode(node.name, f, result); - result = reduceNode(node.objectAssignmentInitializer, f, result); - break; - // Top-level nodes - case 256 /* SourceFile */: - result = ts.reduceLeft(node.statements, f, result); - break; - case 288 /* PartiallyEmittedExpression */: - result = reduceNode(node.expression, f, result); - break; - default: - var edgeTraversalPath = nodeEdgeTraversalMap[kind]; - if (edgeTraversalPath) { - for (var _i = 0, edgeTraversalPath_1 = edgeTraversalPath; _i < edgeTraversalPath_1.length; _i++) { - var edge = edgeTraversalPath_1[_i]; - var value = node[edge.name]; - if (value !== undefined) { - result = ts.isArray(value) - ? ts.reduceLeft(value, f, result) - : f(result, value); - } - } + function getConstantValue(node) { + if (node.kind === 255 /* EnumMember */) { + return getEnumMemberValue(node); + } + var symbol = getNodeLinks(node).resolvedSymbol; + if (symbol && (symbol.flags & 8 /* EnumMember */)) { + // inline property\index accesses only for const enums + if (ts.isConstEnumDeclaration(symbol.valueDeclaration.parent)) { + return getEnumMemberValue(symbol.valueDeclaration); } - break; - } - return result; - } - ts.reduceEachChild = reduceEachChild; - function visitNode(node, visitor, test, optional, lift, parenthesize, parentNode) { - if (node === undefined) { - return undefined; - } - var visited = visitor(node); - if (visited === node) { - return node; - } - var visitedNode; - if (visited === undefined) { - if (!optional) { - Debug.failNotOptional(); } return undefined; } - else if (ts.isArray(visited)) { - visitedNode = (lift || extractSingleNode)(visited); + function isFunctionType(type) { + return type.flags & 2588672 /* ObjectType */ && getSignaturesOfType(type, 0 /* Call */).length > 0; } - else { - visitedNode = visited; + function getTypeReferenceSerializationKind(typeName, location) { + // Resolve the symbol as a value to ensure the type can be reached at runtime during emit. + var valueSymbol = resolveEntityName(typeName, 107455 /* Value */, /*ignoreErrors*/ true, /*dontResolveAlias*/ false, location); + var globalPromiseSymbol = tryGetGlobalPromiseConstructorSymbol(); + if (globalPromiseSymbol && valueSymbol === globalPromiseSymbol) { + return ts.TypeReferenceSerializationKind.Promise; + } + var constructorType = valueSymbol ? getTypeOfSymbol(valueSymbol) : undefined; + if (constructorType && isConstructorType(constructorType)) { + return ts.TypeReferenceSerializationKind.TypeWithConstructSignatureAndValue; + } + // Resolve the symbol as a type so that we can provide a more useful hint for the type serializer. + var typeSymbol = resolveEntityName(typeName, 793064 /* Type */, /*ignoreErrors*/ true, /*dontResolveAlias*/ false, location); + // We might not be able to resolve type symbol so use unknown type in that case (eg error case) + if (!typeSymbol) { + return ts.TypeReferenceSerializationKind.ObjectType; + } + var type = getDeclaredTypeOfSymbol(typeSymbol); + if (type === unknownType) { + return ts.TypeReferenceSerializationKind.Unknown; + } + else if (type.flags & 1 /* Any */) { + return ts.TypeReferenceSerializationKind.ObjectType; + } + else if (isTypeOfKind(type, 1024 /* Void */ | 6144 /* Nullable */ | 8192 /* Never */)) { + return ts.TypeReferenceSerializationKind.VoidNullableOrNeverType; + } + else if (isTypeOfKind(type, 136 /* BooleanLike */)) { + return ts.TypeReferenceSerializationKind.BooleanType; + } + else if (isTypeOfKind(type, 340 /* NumberLike */)) { + return ts.TypeReferenceSerializationKind.NumberLikeType; + } + else if (isTypeOfKind(type, 34 /* StringLike */)) { + return ts.TypeReferenceSerializationKind.StringLikeType; + } + else if (isTupleType(type)) { + return ts.TypeReferenceSerializationKind.ArrayLikeType; + } + else if (isTypeOfKind(type, 512 /* ESSymbol */)) { + return ts.TypeReferenceSerializationKind.ESSymbolType; + } + else if (isFunctionType(type)) { + return ts.TypeReferenceSerializationKind.TypeWithCallSignature; + } + else if (isArrayType(type)) { + return ts.TypeReferenceSerializationKind.ArrayLikeType; + } + else { + return ts.TypeReferenceSerializationKind.ObjectType; + } } - if (parenthesize !== undefined) { - visitedNode = parenthesize(visitedNode, parentNode); + function writeTypeOfDeclaration(declaration, enclosingDeclaration, flags, writer) { + // Get type of the symbol if this is the valid symbol otherwise get type at location + var symbol = getSymbolOfNode(declaration); + var type = symbol && !(symbol.flags & (2048 /* TypeLiteral */ | 131072 /* Signature */)) + ? getWidenedLiteralType(getTypeOfSymbol(symbol)) + : unknownType; + getSymbolDisplayBuilder().buildTypeDisplay(type, writer, enclosingDeclaration, flags); } - Debug.assertNode(visitedNode, test); - aggregateTransformFlags(visitedNode); - return visitedNode; - } - ts.visitNode = visitNode; - function visitNodes(nodes, visitor, test, start, count, parenthesize, parentNode) { - if (nodes === undefined) { - return undefined; + function writeReturnTypeOfSignatureDeclaration(signatureDeclaration, enclosingDeclaration, flags, writer) { + var signature = getSignatureFromDeclaration(signatureDeclaration); + getSymbolDisplayBuilder().buildTypeDisplay(getReturnTypeOfSignature(signature), writer, enclosingDeclaration, flags); } - var updated; - // Ensure start and count have valid values - var length = nodes.length; - if (start === undefined || start < 0) { - start = 0; + function writeTypeOfExpression(expr, enclosingDeclaration, flags, writer) { + var type = getWidenedType(getTypeOfExpression(expr)); + getSymbolDisplayBuilder().buildTypeDisplay(type, writer, enclosingDeclaration, flags); } - if (count === undefined || count > length - start) { - count = length - start; + function writeBaseConstructorTypeOfClass(node, enclosingDeclaration, flags, writer) { + var classType = getDeclaredTypeOfSymbol(getSymbolOfNode(node)); + resolveBaseTypesOfClass(classType); + var baseType = classType.resolvedBaseTypes.length ? classType.resolvedBaseTypes[0] : unknownType; + getSymbolDisplayBuilder().buildTypeDisplay(baseType, writer, enclosingDeclaration, flags); } - if (start > 0 || count < length) { - // If we are not visiting all of the original nodes, we must always create a new array. - // Since this is a fragment of a node array, we do not copy over the previous location - // and will only copy over `hasTrailingComma` if we are including the last element. - updated = ts.createNodeArray([], /*location*/ undefined, - /*hasTrailingComma*/ nodes.hasTrailingComma && start + count === length); + function hasGlobalName(name) { + return !!globals[name]; } - // Visit each original node. - for (var i = 0; i < count; i++) { - var node = nodes[i + start]; - var visited = node !== undefined ? visitor(node) : undefined; - if (updated !== undefined || visited === undefined || visited !== node) { - if (updated === undefined) { - // Ensure we have a copy of `nodes`, up to the current index. - updated = ts.createNodeArray(nodes.slice(0, i), /*location*/ nodes, nodes.hasTrailingComma); + function getReferencedValueSymbol(reference, startInDeclarationContainer) { + var resolvedSymbol = getNodeLinks(reference).resolvedSymbol; + if (resolvedSymbol) { + return resolvedSymbol; + } + var location = reference; + if (startInDeclarationContainer) { + // When resolving the name of a declaration as a value, we need to start resolution + // at a point outside of the declaration. + var parent_14 = reference.parent; + if (ts.isDeclaration(parent_14) && reference === parent_14.name) { + location = getDeclarationContainer(parent_14); } - if (visited) { - if (ts.isArray(visited)) { - for (var _i = 0, visited_1 = visited; _i < visited_1.length; _i++) { - var visitedNode = visited_1[_i]; - visitedNode = parenthesize - ? parenthesize(visitedNode, parentNode) - : visitedNode; - Debug.assertNode(visitedNode, test); - aggregateTransformFlags(visitedNode); - updated.push(visitedNode); - } - } - else { - var visitedNode = parenthesize - ? parenthesize(visited, parentNode) - : visited; - Debug.assertNode(visitedNode, test); - aggregateTransformFlags(visitedNode); - updated.push(visitedNode); + } + return resolveName(location, reference.text, 107455 /* Value */ | 1048576 /* ExportValue */ | 8388608 /* Alias */, /*nodeNotFoundMessage*/ undefined, /*nameArg*/ undefined); + } + function getReferencedValueDeclaration(reference) { + if (!ts.isGeneratedIdentifier(reference)) { + reference = ts.getParseTreeNode(reference, ts.isIdentifier); + if (reference) { + var symbol = getReferencedValueSymbol(reference); + if (symbol) { + return getExportSymbolOfValueSymbolIfExported(symbol).valueDeclaration; } } } - } - return updated || nodes; - } - ts.visitNodes = visitNodes; - function visitEachChild(node, visitor, context) { - if (node === undefined) { return undefined; } - var kind = node.kind; - // No need to visit nodes with no children. - if ((kind > 0 /* FirstToken */ && kind <= 138 /* LastToken */)) { - return node; + function isLiteralConstDeclaration(node) { + if (ts.isConst(node)) { + var type = getTypeOfSymbol(getSymbolOfNode(node)); + return !!(type.flags & 96 /* StringOrNumberLiteral */ && type.flags & 16777216 /* FreshLiteral */); + } + return false; } - // We do not yet support types. - if ((kind >= 154 /* TypePredicate */ && kind <= 166 /* LiteralType */)) { - return node; + function writeLiteralConstValue(node, writer) { + var type = getTypeOfSymbol(getSymbolOfNode(node)); + writer.writeStringLiteral(literalTypeToString(type)); } - switch (node.kind) { - case 198 /* SemicolonClassElement */: - case 201 /* EmptyStatement */: - case 193 /* OmittedExpression */: - case 217 /* DebuggerStatement */: - // No need to visit nodes with no children. - return node; - // Names - case 140 /* ComputedPropertyName */: - return ts.updateComputedPropertyName(node, visitNode(node.expression, visitor, ts.isExpression)); - // Signature elements - case 142 /* Parameter */: - return ts.updateParameterDeclaration(node, visitNodes(node.decorators, visitor, ts.isDecorator), visitNodes(node.modifiers, visitor, ts.isModifier), visitNode(node.name, visitor, ts.isBindingName), visitNode(node.type, visitor, ts.isTypeNode, /*optional*/ true), visitNode(node.initializer, visitor, ts.isExpression, /*optional*/ true)); - // Type member - case 145 /* PropertyDeclaration */: - return ts.updateProperty(node, visitNodes(node.decorators, visitor, ts.isDecorator), visitNodes(node.modifiers, visitor, ts.isModifier), visitNode(node.name, visitor, ts.isPropertyName), visitNode(node.type, visitor, ts.isTypeNode, /*optional*/ true), visitNode(node.initializer, visitor, ts.isExpression, /*optional*/ true)); - case 147 /* MethodDeclaration */: - return ts.updateMethod(node, visitNodes(node.decorators, visitor, ts.isDecorator), visitNodes(node.modifiers, visitor, ts.isModifier), visitNode(node.name, visitor, ts.isPropertyName), visitNodes(node.typeParameters, visitor, ts.isTypeParameter), (context.startLexicalEnvironment(), visitNodes(node.parameters, visitor, ts.isParameter)), visitNode(node.type, visitor, ts.isTypeNode, /*optional*/ true), mergeFunctionBodyLexicalEnvironment(visitNode(node.body, visitor, ts.isFunctionBody, /*optional*/ true), context.endLexicalEnvironment())); - case 148 /* Constructor */: - return ts.updateConstructor(node, visitNodes(node.decorators, visitor, ts.isDecorator), visitNodes(node.modifiers, visitor, ts.isModifier), (context.startLexicalEnvironment(), visitNodes(node.parameters, visitor, ts.isParameter)), mergeFunctionBodyLexicalEnvironment(visitNode(node.body, visitor, ts.isFunctionBody, /*optional*/ true), context.endLexicalEnvironment())); - case 149 /* GetAccessor */: - return ts.updateGetAccessor(node, visitNodes(node.decorators, visitor, ts.isDecorator), visitNodes(node.modifiers, visitor, ts.isModifier), visitNode(node.name, visitor, ts.isPropertyName), (context.startLexicalEnvironment(), visitNodes(node.parameters, visitor, ts.isParameter)), visitNode(node.type, visitor, ts.isTypeNode, /*optional*/ true), mergeFunctionBodyLexicalEnvironment(visitNode(node.body, visitor, ts.isFunctionBody, /*optional*/ true), context.endLexicalEnvironment())); - case 150 /* SetAccessor */: - return ts.updateSetAccessor(node, visitNodes(node.decorators, visitor, ts.isDecorator), visitNodes(node.modifiers, visitor, ts.isModifier), visitNode(node.name, visitor, ts.isPropertyName), (context.startLexicalEnvironment(), visitNodes(node.parameters, visitor, ts.isParameter)), mergeFunctionBodyLexicalEnvironment(visitNode(node.body, visitor, ts.isFunctionBody, /*optional*/ true), context.endLexicalEnvironment())); - // Binding patterns - case 167 /* ObjectBindingPattern */: - return ts.updateObjectBindingPattern(node, visitNodes(node.elements, visitor, ts.isBindingElement)); - case 168 /* ArrayBindingPattern */: - return ts.updateArrayBindingPattern(node, visitNodes(node.elements, visitor, ts.isArrayBindingElement)); - case 169 /* BindingElement */: - return ts.updateBindingElement(node, visitNode(node.propertyName, visitor, ts.isPropertyName, /*optional*/ true), visitNode(node.name, visitor, ts.isBindingName), visitNode(node.initializer, visitor, ts.isExpression, /*optional*/ true)); - // Expression + function createResolver() { + // this variable and functions that use it are deliberately moved here from the outer scope + // to avoid scope pollution + var resolvedTypeReferenceDirectives = host.getResolvedTypeReferenceDirectives(); + var fileToDirective; + if (resolvedTypeReferenceDirectives) { + // populate reverse mapping: file path -> type reference directive that was resolved to this file + fileToDirective = ts.createFileMap(); + for (var key in resolvedTypeReferenceDirectives) { + var resolvedDirective = resolvedTypeReferenceDirectives[key]; + if (!resolvedDirective) { + continue; + } + var file = host.getSourceFile(resolvedDirective.resolvedFileName); + fileToDirective.set(file.path, key); + } + } + return { + getReferencedExportContainer: getReferencedExportContainer, + getReferencedImportDeclaration: getReferencedImportDeclaration, + getReferencedDeclarationWithCollidingName: getReferencedDeclarationWithCollidingName, + isDeclarationWithCollidingName: isDeclarationWithCollidingName, + isValueAliasDeclaration: isValueAliasDeclaration, + hasGlobalName: hasGlobalName, + isReferencedAliasDeclaration: isReferencedAliasDeclaration, + getNodeCheckFlags: getNodeCheckFlags, + isTopLevelValueImportEqualsWithEntityName: isTopLevelValueImportEqualsWithEntityName, + isDeclarationVisible: isDeclarationVisible, + isImplementationOfOverload: isImplementationOfOverload, + writeTypeOfDeclaration: writeTypeOfDeclaration, + writeReturnTypeOfSignatureDeclaration: writeReturnTypeOfSignatureDeclaration, + writeTypeOfExpression: writeTypeOfExpression, + writeBaseConstructorTypeOfClass: writeBaseConstructorTypeOfClass, + isSymbolAccessible: isSymbolAccessible, + isEntityNameVisible: isEntityNameVisible, + getConstantValue: getConstantValue, + collectLinkedAliases: collectLinkedAliases, + getReferencedValueDeclaration: getReferencedValueDeclaration, + getTypeReferenceSerializationKind: getTypeReferenceSerializationKind, + isOptionalParameter: isOptionalParameter, + moduleExportsSomeValue: moduleExportsSomeValue, + isArgumentsLocalBinding: isArgumentsLocalBinding, + getExternalModuleFileFromDeclaration: getExternalModuleFileFromDeclaration, + getTypeReferenceDirectivesForEntityName: getTypeReferenceDirectivesForEntityName, + getTypeReferenceDirectivesForSymbol: getTypeReferenceDirectivesForSymbol, + isLiteralConstDeclaration: isLiteralConstDeclaration, + writeLiteralConstValue: writeLiteralConstValue + }; + // defined here to avoid outer scope pollution + function getTypeReferenceDirectivesForEntityName(node) { + // program does not have any files with type reference directives - bail out + if (!fileToDirective) { + return undefined; + } + // property access can only be used as values + // qualified names can only be used as types\namespaces + // identifiers are treated as values only if they appear in type queries + var meaning = (node.kind === 172 /* PropertyAccessExpression */) || (node.kind === 69 /* Identifier */ && isInTypeQuery(node)) + ? 107455 /* Value */ | 1048576 /* ExportValue */ + : 793064 /* Type */ | 1920 /* Namespace */; + var symbol = resolveEntityName(node, meaning, /*ignoreErrors*/ true); + return symbol && symbol !== unknownSymbol ? getTypeReferenceDirectivesForSymbol(symbol, meaning) : undefined; + } + // defined here to avoid outer scope pollution + function getTypeReferenceDirectivesForSymbol(symbol, meaning) { + // program does not have any files with type reference directives - bail out + if (!fileToDirective) { + return undefined; + } + if (!isSymbolFromTypeDeclarationFile(symbol)) { + return undefined; + } + // check what declarations in the symbol can contribute to the target meaning + var typeReferenceDirectives; + for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { + var decl = _a[_i]; + // check meaning of the local symbol to see if declaration needs to be analyzed further + if (decl.symbol && decl.symbol.flags & meaning) { + var file = ts.getSourceFileOfNode(decl); + var typeReferenceDirective = fileToDirective.get(file.path); + if (typeReferenceDirective) { + (typeReferenceDirectives || (typeReferenceDirectives = [])).push(typeReferenceDirective); + } + } + } + return typeReferenceDirectives; + } + function isSymbolFromTypeDeclarationFile(symbol) { + // bail out if symbol does not have associated declarations (i.e. this is transient symbol created for property in binding pattern) + if (!symbol.declarations) { + return false; + } + // walk the parent chain for symbols to make sure that top level parent symbol is in the global scope + // external modules cannot define or contribute to type declaration files + var current = symbol; + while (true) { + var parent_15 = getParentOfSymbol(current); + if (parent_15) { + current = parent_15; + } + else { + break; + } + } + if (current.valueDeclaration && current.valueDeclaration.kind === 256 /* SourceFile */ && current.flags & 512 /* ValueModule */) { + return false; + } + // check that at least one declaration of top level symbol originates from type declaration file + for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { + var decl = _a[_i]; + var file = ts.getSourceFileOfNode(decl); + if (fileToDirective.contains(file.path)) { + return true; + } + } + return false; + } + } + function getExternalModuleFileFromDeclaration(declaration) { + var specifier = ts.getExternalModuleName(declaration); + var moduleSymbol = resolveExternalModuleNameWorker(specifier, specifier, /*moduleNotFoundError*/ undefined); + if (!moduleSymbol) { + return undefined; + } + return ts.getDeclarationOfKind(moduleSymbol, 256 /* SourceFile */); + } + function initializeTypeChecker() { + // Bind all source files and propagate errors + for (var _i = 0, _a = host.getSourceFiles(); _i < _a.length; _i++) { + var file = _a[_i]; + ts.bindSourceFile(file, compilerOptions); + } + // Initialize global symbol table + var augmentations; + var requestedExternalEmitHelpers = 0; + var firstFileRequestingExternalHelpers; + for (var _b = 0, _c = host.getSourceFiles(); _b < _c.length; _b++) { + var file = _c[_b]; + if (!ts.isExternalOrCommonJsModule(file)) { + mergeSymbolTable(globals, file.locals); + } + if (file.patternAmbientModules && file.patternAmbientModules.length) { + patternAmbientModules = ts.concatenate(patternAmbientModules, file.patternAmbientModules); + } + if (file.moduleAugmentations.length) { + (augmentations || (augmentations = [])).push(file.moduleAugmentations); + } + if (file.symbol && file.symbol.globalExports) { + // Merge in UMD exports with first-in-wins semantics (see #9771) + var source = file.symbol.globalExports; + for (var id in source) { + if (!(id in globals)) { + globals[id] = source[id]; + } + } + } + if ((compilerOptions.isolatedModules || ts.isExternalModule(file)) && !file.isDeclarationFile) { + var fileRequestedExternalEmitHelpers = file.flags & 31744 /* EmitHelperFlags */; + if (fileRequestedExternalEmitHelpers) { + requestedExternalEmitHelpers |= fileRequestedExternalEmitHelpers; + if (firstFileRequestingExternalHelpers === undefined) { + firstFileRequestingExternalHelpers = file; + } + } + } + } + if (augmentations) { + // merge module augmentations. + // this needs to be done after global symbol table is initialized to make sure that all ambient modules are indexed + for (var _d = 0, augmentations_1 = augmentations; _d < augmentations_1.length; _d++) { + var list = augmentations_1[_d]; + for (var _e = 0, list_1 = list; _e < list_1.length; _e++) { + var augmentation = list_1[_e]; + mergeModuleAugmentation(augmentation); + } + } + } + // Setup global builtins + addToSymbolTable(globals, builtinGlobals, ts.Diagnostics.Declaration_name_conflicts_with_built_in_global_identifier_0); + getSymbolLinks(undefinedSymbol).type = undefinedWideningType; + getSymbolLinks(argumentsSymbol).type = getGlobalType("IArguments"); + getSymbolLinks(unknownSymbol).type = unknownType; + // Initialize special types + globalArrayType = getGlobalType("Array", /*arity*/ 1); + globalObjectType = getGlobalType("Object"); + globalFunctionType = getGlobalType("Function"); + globalStringType = getGlobalType("String"); + globalNumberType = getGlobalType("Number"); + globalBooleanType = getGlobalType("Boolean"); + globalRegExpType = getGlobalType("RegExp"); + jsxElementType = getExportedTypeFromNamespace("JSX", JsxNames.Element); + getGlobalClassDecoratorType = ts.memoize(function () { return getGlobalType("ClassDecorator"); }); + getGlobalPropertyDecoratorType = ts.memoize(function () { return getGlobalType("PropertyDecorator"); }); + getGlobalMethodDecoratorType = ts.memoize(function () { return getGlobalType("MethodDecorator"); }); + getGlobalParameterDecoratorType = ts.memoize(function () { return getGlobalType("ParameterDecorator"); }); + getGlobalTypedPropertyDescriptorType = ts.memoize(function () { return getGlobalType("TypedPropertyDescriptor", /*arity*/ 1); }); + getGlobalESSymbolConstructorSymbol = ts.memoize(function () { return getGlobalValueSymbol("Symbol"); }); + getGlobalPromiseType = ts.memoize(function () { return getGlobalType("Promise", /*arity*/ 1); }); + tryGetGlobalPromiseType = ts.memoize(function () { return getGlobalSymbol("Promise", 793064 /* Type */, /*diagnostic*/ undefined) && getGlobalPromiseType(); }); + getGlobalPromiseLikeType = ts.memoize(function () { return getGlobalType("PromiseLike", /*arity*/ 1); }); + getInstantiatedGlobalPromiseLikeType = ts.memoize(createInstantiatedPromiseLikeType); + getGlobalPromiseConstructorSymbol = ts.memoize(function () { return getGlobalValueSymbol("Promise"); }); + tryGetGlobalPromiseConstructorSymbol = ts.memoize(function () { return getGlobalSymbol("Promise", 107455 /* Value */, /*diagnostic*/ undefined) && getGlobalPromiseConstructorSymbol(); }); + getGlobalPromiseConstructorLikeType = ts.memoize(function () { return getGlobalType("PromiseConstructorLike"); }); + getGlobalThenableType = ts.memoize(createThenableType); + getGlobalTemplateStringsArrayType = ts.memoize(function () { return getGlobalType("TemplateStringsArray"); }); + if (languageVersion >= 2 /* ES6 */) { + getGlobalESSymbolType = ts.memoize(function () { return getGlobalType("Symbol"); }); + getGlobalIterableType = ts.memoize(function () { return getGlobalType("Iterable", /*arity*/ 1); }); + getGlobalIteratorType = ts.memoize(function () { return getGlobalType("Iterator", /*arity*/ 1); }); + getGlobalIterableIteratorType = ts.memoize(function () { return getGlobalType("IterableIterator", /*arity*/ 1); }); + } + else { + getGlobalESSymbolType = ts.memoize(function () { return emptyObjectType; }); + getGlobalIterableType = ts.memoize(function () { return emptyGenericType; }); + getGlobalIteratorType = ts.memoize(function () { return emptyGenericType; }); + getGlobalIterableIteratorType = ts.memoize(function () { return emptyGenericType; }); + } + anyArrayType = createArrayType(anyType); + var symbol = getGlobalSymbol("ReadonlyArray", 793064 /* Type */, /*diagnostic*/ undefined); + globalReadonlyArrayType = symbol && getTypeOfGlobalSymbol(symbol, /*arity*/ 1); + anyReadonlyArrayType = globalReadonlyArrayType ? createTypeFromGenericGlobalType(globalReadonlyArrayType, [anyType]) : anyArrayType; + // If we have specified that we are importing helpers, we should report global + // errors if we cannot resolve the helpers external module, or if it does not have + // the necessary helpers exported. + if (compilerOptions.importHelpers && firstFileRequestingExternalHelpers) { + // Find the first reference to the helpers module. + var helpersModule = resolveExternalModule(firstFileRequestingExternalHelpers, ts.externalHelpersModuleNameText, ts.Diagnostics.Cannot_find_module_0, + /*errorNode*/ undefined); + // If we found the module, report errors if it does not have the necessary exports. + if (helpersModule) { + var exports = helpersModule.exports; + if (requestedExternalEmitHelpers & 1024 /* HasClassExtends */ && languageVersion < 2 /* ES6 */) { + verifyHelperSymbol(exports, "__extends", 107455 /* Value */); + } + if (requestedExternalEmitHelpers & 16384 /* HasJsxSpreadAttributes */ && compilerOptions.jsx !== 1 /* Preserve */) { + verifyHelperSymbol(exports, "__assign", 107455 /* Value */); + } + if (requestedExternalEmitHelpers & 2048 /* HasDecorators */) { + verifyHelperSymbol(exports, "__decorate", 107455 /* Value */); + if (compilerOptions.emitDecoratorMetadata) { + verifyHelperSymbol(exports, "__metadata", 107455 /* Value */); + } + } + if (requestedExternalEmitHelpers & 4096 /* HasParamDecorators */) { + verifyHelperSymbol(exports, "__param", 107455 /* Value */); + } + if (requestedExternalEmitHelpers & 8192 /* HasAsyncFunctions */) { + verifyHelperSymbol(exports, "__awaiter", 107455 /* Value */); + if (languageVersion < 2 /* ES6 */) { + verifyHelperSymbol(exports, "__generator", 107455 /* Value */); + } + } + } + } + } + function verifyHelperSymbol(symbols, name, meaning) { + var symbol = getSymbol(symbols, ts.escapeIdentifier(name), meaning); + if (!symbol) { + error(/*location*/ undefined, ts.Diagnostics.Module_0_has_no_exported_member_1, ts.externalHelpersModuleNameText, name); + } + } + function createInstantiatedPromiseLikeType() { + var promiseLikeType = getGlobalPromiseLikeType(); + if (promiseLikeType !== emptyGenericType) { + return createTypeReference(promiseLikeType, [anyType]); + } + return emptyObjectType; + } + function createThenableType() { + // build the thenable type that is used to verify against a non-promise "thenable" operand to `await`. + var thenPropertySymbol = createSymbol(67108864 /* Transient */ | 4 /* Property */, "then"); + getSymbolLinks(thenPropertySymbol).type = globalFunctionType; + var thenableType = createObjectType(2097152 /* Anonymous */); + thenableType.properties = [thenPropertySymbol]; + thenableType.members = createSymbolTable(thenableType.properties); + thenableType.callSignatures = []; + thenableType.constructSignatures = []; + return thenableType; + } + // GRAMMAR CHECKING + function checkGrammarDecorators(node) { + if (!node.decorators) { + return false; + } + if (!ts.nodeCanBeDecorated(node)) { + if (node.kind === 147 /* MethodDeclaration */ && !ts.nodeIsPresent(node.body)) { + return grammarErrorOnFirstToken(node, ts.Diagnostics.A_decorator_can_only_decorate_a_method_implementation_not_an_overload); + } + else { + return grammarErrorOnFirstToken(node, ts.Diagnostics.Decorators_are_not_valid_here); + } + } + else if (node.kind === 149 /* GetAccessor */ || node.kind === 150 /* SetAccessor */) { + var accessors = ts.getAllAccessorDeclarations(node.parent.members, node); + if (accessors.firstAccessor.decorators && node === accessors.secondAccessor) { + return grammarErrorOnFirstToken(node, ts.Diagnostics.Decorators_cannot_be_applied_to_multiple_get_Slashset_accessors_of_the_same_name); + } + } + return false; + } + function checkGrammarModifiers(node) { + var quickResult = reportObviousModifierErrors(node); + if (quickResult !== undefined) { + return quickResult; + } + var lastStatic, lastPrivate, lastProtected, lastDeclare, lastAsync, lastReadonly; + var flags = 0 /* None */; + for (var _i = 0, _a = node.modifiers; _i < _a.length; _i++) { + var modifier = _a[_i]; + if (modifier.kind !== 128 /* ReadonlyKeyword */) { + if (node.kind === 144 /* PropertySignature */ || node.kind === 146 /* MethodSignature */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_type_member, ts.tokenToString(modifier.kind)); + } + if (node.kind === 153 /* IndexSignature */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_an_index_signature, ts.tokenToString(modifier.kind)); + } + } + switch (modifier.kind) { + case 74 /* ConstKeyword */: + if (node.kind !== 224 /* EnumDeclaration */ && node.parent.kind === 221 /* ClassDeclaration */) { + return grammarErrorOnNode(node, ts.Diagnostics.A_class_member_cannot_have_the_0_keyword, ts.tokenToString(74 /* ConstKeyword */)); + } + break; + case 112 /* PublicKeyword */: + case 111 /* ProtectedKeyword */: + case 110 /* PrivateKeyword */: + var text = visibilityToString(ts.modifierToFlag(modifier.kind)); + if (modifier.kind === 111 /* ProtectedKeyword */) { + lastProtected = modifier; + } + else if (modifier.kind === 110 /* PrivateKeyword */) { + lastPrivate = modifier; + } + if (flags & 28 /* AccessibilityModifier */) { + return grammarErrorOnNode(modifier, ts.Diagnostics.Accessibility_modifier_already_seen); + } + else if (flags & 32 /* Static */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, text, "static"); + } + else if (flags & 64 /* Readonly */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, text, "readonly"); + } + else if (flags & 256 /* Async */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, text, "async"); + } + else if (node.parent.kind === 226 /* ModuleBlock */ || node.parent.kind === 256 /* SourceFile */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_module_or_namespace_element, text); + } + else if (flags & 128 /* Abstract */) { + if (modifier.kind === 110 /* PrivateKeyword */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_be_used_with_1_modifier, text, "abstract"); + } + else { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, text, "abstract"); + } + } + flags |= ts.modifierToFlag(modifier.kind); + break; + case 113 /* StaticKeyword */: + if (flags & 32 /* Static */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_already_seen, "static"); + } + else if (flags & 64 /* Readonly */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, "static", "readonly"); + } + else if (flags & 256 /* Async */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, "static", "async"); + } + else if (node.parent.kind === 226 /* ModuleBlock */ || node.parent.kind === 256 /* SourceFile */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_module_or_namespace_element, "static"); + } + else if (node.kind === 142 /* Parameter */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_parameter, "static"); + } + else if (flags & 128 /* Abstract */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "static", "abstract"); + } + flags |= 32 /* Static */; + lastStatic = modifier; + break; + case 128 /* ReadonlyKeyword */: + if (flags & 64 /* Readonly */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_already_seen, "readonly"); + } + else if (node.kind !== 145 /* PropertyDeclaration */ && node.kind !== 144 /* PropertySignature */ && node.kind !== 153 /* IndexSignature */ && node.kind !== 142 /* Parameter */) { + // If node.kind === SyntaxKind.Parameter, checkParameter report an error if it's not a parameter property. + return grammarErrorOnNode(modifier, ts.Diagnostics.readonly_modifier_can_only_appear_on_a_property_declaration_or_index_signature); + } + flags |= 64 /* Readonly */; + lastReadonly = modifier; + break; + case 82 /* ExportKeyword */: + if (flags & 1 /* Export */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_already_seen, "export"); + } + else if (flags & 2 /* Ambient */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, "export", "declare"); + } + else if (flags & 128 /* Abstract */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, "export", "abstract"); + } + else if (flags & 256 /* Async */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_must_precede_1_modifier, "export", "async"); + } + else if (node.parent.kind === 221 /* ClassDeclaration */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_class_element, "export"); + } + else if (node.kind === 142 /* Parameter */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_parameter, "export"); + } + flags |= 1 /* Export */; + break; + case 122 /* DeclareKeyword */: + if (flags & 2 /* Ambient */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_already_seen, "declare"); + } + else if (flags & 256 /* Async */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_be_used_in_an_ambient_context, "async"); + } + else if (node.parent.kind === 221 /* ClassDeclaration */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_class_element, "declare"); + } + else if (node.kind === 142 /* Parameter */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_parameter, "declare"); + } + else if (ts.isInAmbientContext(node.parent) && node.parent.kind === 226 /* ModuleBlock */) { + return grammarErrorOnNode(modifier, ts.Diagnostics.A_declare_modifier_cannot_be_used_in_an_already_ambient_context); + } + flags |= 2 /* Ambient */; + lastDeclare = modifier; + break; + case 115 /* AbstractKeyword */: + if (flags & 128 /* Abstract */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_already_seen, "abstract"); + } + if (node.kind !== 221 /* ClassDeclaration */) { + if (node.kind !== 147 /* MethodDeclaration */ && + node.kind !== 145 /* PropertyDeclaration */ && + node.kind !== 149 /* GetAccessor */ && + node.kind !== 150 /* SetAccessor */) { + return grammarErrorOnNode(modifier, ts.Diagnostics.abstract_modifier_can_only_appear_on_a_class_method_or_property_declaration); + } + if (!(node.parent.kind === 221 /* ClassDeclaration */ && ts.getModifierFlags(node.parent) & 128 /* Abstract */)) { + return grammarErrorOnNode(modifier, ts.Diagnostics.Abstract_methods_can_only_appear_within_an_abstract_class); + } + if (flags & 32 /* Static */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "static", "abstract"); + } + if (flags & 8 /* Private */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_be_used_with_1_modifier, "private", "abstract"); + } + } + flags |= 128 /* Abstract */; + break; + case 118 /* AsyncKeyword */: + if (flags & 256 /* Async */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_already_seen, "async"); + } + else if (flags & 2 /* Ambient */ || ts.isInAmbientContext(node.parent)) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_be_used_in_an_ambient_context, "async"); + } + else if (node.kind === 142 /* Parameter */) { + return grammarErrorOnNode(modifier, ts.Diagnostics._0_modifier_cannot_appear_on_a_parameter, "async"); + } + flags |= 256 /* Async */; + lastAsync = modifier; + break; + } + } + if (node.kind === 148 /* Constructor */) { + if (flags & 32 /* Static */) { + return grammarErrorOnNode(lastStatic, ts.Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "static"); + } + if (flags & 128 /* Abstract */) { + return grammarErrorOnNode(lastStatic, ts.Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "abstract"); + } + else if (flags & 256 /* Async */) { + return grammarErrorOnNode(lastAsync, ts.Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "async"); + } + else if (flags & 64 /* Readonly */) { + return grammarErrorOnNode(lastReadonly, ts.Diagnostics._0_modifier_cannot_appear_on_a_constructor_declaration, "readonly"); + } + return; + } + else if ((node.kind === 230 /* ImportDeclaration */ || node.kind === 229 /* ImportEqualsDeclaration */) && flags & 2 /* Ambient */) { + return grammarErrorOnNode(lastDeclare, ts.Diagnostics.A_0_modifier_cannot_be_used_with_an_import_declaration, "declare"); + } + else if (node.kind === 142 /* Parameter */ && (flags & 92 /* ParameterPropertyModifier */) && ts.isBindingPattern(node.name)) { + return grammarErrorOnNode(node, ts.Diagnostics.A_parameter_property_may_not_be_declared_using_a_binding_pattern); + } + else if (node.kind === 142 /* Parameter */ && (flags & 92 /* ParameterPropertyModifier */) && node.dotDotDotToken) { + return grammarErrorOnNode(node, ts.Diagnostics.A_parameter_property_cannot_be_declared_using_a_rest_parameter); + } + if (flags & 256 /* Async */) { + return checkGrammarAsyncModifier(node, lastAsync); + } + } + /** + * true | false: Early return this value from checkGrammarModifiers. + * undefined: Need to do full checking on the modifiers. + */ + function reportObviousModifierErrors(node) { + return !node.modifiers + ? false + : shouldReportBadModifier(node) + ? grammarErrorOnFirstToken(node, ts.Diagnostics.Modifiers_cannot_appear_here) + : undefined; + } + function shouldReportBadModifier(node) { + switch (node.kind) { + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + case 148 /* Constructor */: + case 145 /* PropertyDeclaration */: + case 144 /* PropertySignature */: + case 147 /* MethodDeclaration */: + case 146 /* MethodSignature */: + case 153 /* IndexSignature */: + case 225 /* ModuleDeclaration */: + case 230 /* ImportDeclaration */: + case 229 /* ImportEqualsDeclaration */: + case 236 /* ExportDeclaration */: + case 235 /* ExportAssignment */: + case 179 /* FunctionExpression */: + case 180 /* ArrowFunction */: + case 142 /* Parameter */: + return false; + default: + if (node.parent.kind === 226 /* ModuleBlock */ || node.parent.kind === 256 /* SourceFile */) { + return false; + } + switch (node.kind) { + case 220 /* FunctionDeclaration */: + return nodeHasAnyModifiersExcept(node, 118 /* AsyncKeyword */); + case 221 /* ClassDeclaration */: + return nodeHasAnyModifiersExcept(node, 115 /* AbstractKeyword */); + case 222 /* InterfaceDeclaration */: + case 200 /* VariableStatement */: + case 223 /* TypeAliasDeclaration */: + return true; + case 224 /* EnumDeclaration */: + return nodeHasAnyModifiersExcept(node, 74 /* ConstKeyword */); + default: + ts.Debug.fail(); + return false; + } + } + } + function nodeHasAnyModifiersExcept(node, allowedModifier) { + return node.modifiers.length > 1 || node.modifiers[0].kind !== allowedModifier; + } + function checkGrammarAsyncModifier(node, asyncModifier) { + switch (node.kind) { + case 147 /* MethodDeclaration */: + case 220 /* FunctionDeclaration */: + case 179 /* FunctionExpression */: + case 180 /* ArrowFunction */: + if (!node.asteriskToken) { + return false; + } + break; + } + return grammarErrorOnNode(asyncModifier, ts.Diagnostics._0_modifier_cannot_be_used_here, "async"); + } + function checkGrammarForDisallowedTrailingComma(list) { + if (list && list.hasTrailingComma) { + var start = list.end - ",".length; + var end = list.end; + var sourceFile = ts.getSourceFileOfNode(list[0]); + return grammarErrorAtPos(sourceFile, start, end - start, ts.Diagnostics.Trailing_comma_not_allowed); + } + } + function checkGrammarTypeParameterList(node, typeParameters, file) { + if (checkGrammarForDisallowedTrailingComma(typeParameters)) { + return true; + } + if (typeParameters && typeParameters.length === 0) { + var start = typeParameters.pos - "<".length; + var end = ts.skipTrivia(file.text, typeParameters.end) + ">".length; + return grammarErrorAtPos(file, start, end - start, ts.Diagnostics.Type_parameter_list_cannot_be_empty); + } + } + function checkGrammarParameterList(parameters) { + var seenOptionalParameter = false; + var parameterCount = parameters.length; + for (var i = 0; i < parameterCount; i++) { + var parameter = parameters[i]; + if (parameter.dotDotDotToken) { + if (i !== (parameterCount - 1)) { + return grammarErrorOnNode(parameter.dotDotDotToken, ts.Diagnostics.A_rest_parameter_must_be_last_in_a_parameter_list); + } + if (ts.isBindingPattern(parameter.name)) { + return grammarErrorOnNode(parameter.name, ts.Diagnostics.A_rest_element_cannot_contain_a_binding_pattern); + } + if (parameter.questionToken) { + return grammarErrorOnNode(parameter.questionToken, ts.Diagnostics.A_rest_parameter_cannot_be_optional); + } + if (parameter.initializer) { + return grammarErrorOnNode(parameter.name, ts.Diagnostics.A_rest_parameter_cannot_have_an_initializer); + } + } + else if (parameter.questionToken) { + seenOptionalParameter = true; + if (parameter.initializer) { + return grammarErrorOnNode(parameter.name, ts.Diagnostics.Parameter_cannot_have_question_mark_and_initializer); + } + } + else if (seenOptionalParameter && !parameter.initializer) { + return grammarErrorOnNode(parameter.name, ts.Diagnostics.A_required_parameter_cannot_follow_an_optional_parameter); + } + } + } + function checkGrammarFunctionLikeDeclaration(node) { + // Prevent cascading error by short-circuit + var file = ts.getSourceFileOfNode(node); + return checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarTypeParameterList(node, node.typeParameters, file) || + checkGrammarParameterList(node.parameters) || checkGrammarArrowFunction(node, file); + } + function checkGrammarArrowFunction(node, file) { + if (node.kind === 180 /* ArrowFunction */) { + var arrowFunction = node; + var startLine = ts.getLineAndCharacterOfPosition(file, arrowFunction.equalsGreaterThanToken.pos).line; + var endLine = ts.getLineAndCharacterOfPosition(file, arrowFunction.equalsGreaterThanToken.end).line; + if (startLine !== endLine) { + return grammarErrorOnNode(arrowFunction.equalsGreaterThanToken, ts.Diagnostics.Line_terminator_not_permitted_before_arrow); + } + } + return false; + } + function checkGrammarIndexSignatureParameters(node) { + var parameter = node.parameters[0]; + if (node.parameters.length !== 1) { + if (parameter) { + return grammarErrorOnNode(parameter.name, ts.Diagnostics.An_index_signature_must_have_exactly_one_parameter); + } + else { + return grammarErrorOnNode(node, ts.Diagnostics.An_index_signature_must_have_exactly_one_parameter); + } + } + if (parameter.dotDotDotToken) { + return grammarErrorOnNode(parameter.dotDotDotToken, ts.Diagnostics.An_index_signature_cannot_have_a_rest_parameter); + } + if (ts.getModifierFlags(parameter) !== 0) { + return grammarErrorOnNode(parameter.name, ts.Diagnostics.An_index_signature_parameter_cannot_have_an_accessibility_modifier); + } + if (parameter.questionToken) { + return grammarErrorOnNode(parameter.questionToken, ts.Diagnostics.An_index_signature_parameter_cannot_have_a_question_mark); + } + if (parameter.initializer) { + return grammarErrorOnNode(parameter.name, ts.Diagnostics.An_index_signature_parameter_cannot_have_an_initializer); + } + if (!parameter.type) { + return grammarErrorOnNode(parameter.name, ts.Diagnostics.An_index_signature_parameter_must_have_a_type_annotation); + } + if (parameter.type.kind !== 132 /* StringKeyword */ && parameter.type.kind !== 130 /* NumberKeyword */) { + return grammarErrorOnNode(parameter.name, ts.Diagnostics.An_index_signature_parameter_type_must_be_string_or_number); + } + if (!node.type) { + return grammarErrorOnNode(node, ts.Diagnostics.An_index_signature_must_have_a_type_annotation); + } + } + function checkGrammarIndexSignature(node) { + // Prevent cascading error by short-circuit + return checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarIndexSignatureParameters(node); + } + function checkGrammarForAtLeastOneTypeArgument(node, typeArguments) { + if (typeArguments && typeArguments.length === 0) { + var sourceFile = ts.getSourceFileOfNode(node); + var start = typeArguments.pos - "<".length; + var end = ts.skipTrivia(sourceFile.text, typeArguments.end) + ">".length; + return grammarErrorAtPos(sourceFile, start, end - start, ts.Diagnostics.Type_argument_list_cannot_be_empty); + } + } + function checkGrammarTypeArguments(node, typeArguments) { + return checkGrammarForDisallowedTrailingComma(typeArguments) || + checkGrammarForAtLeastOneTypeArgument(node, typeArguments); + } + function checkGrammarForOmittedArgument(node, args) { + if (args) { + var sourceFile = ts.getSourceFileOfNode(node); + for (var _i = 0, args_4 = args; _i < args_4.length; _i++) { + var arg = args_4[_i]; + if (arg.kind === 193 /* OmittedExpression */) { + return grammarErrorAtPos(sourceFile, arg.pos, 0, ts.Diagnostics.Argument_expression_expected); + } + } + } + } + function checkGrammarArguments(node, args) { + return checkGrammarForOmittedArgument(node, args); + } + function checkGrammarHeritageClause(node) { + var types = node.types; + if (checkGrammarForDisallowedTrailingComma(types)) { + return true; + } + if (types && types.length === 0) { + var listType = ts.tokenToString(node.token); + var sourceFile = ts.getSourceFileOfNode(node); + return grammarErrorAtPos(sourceFile, types.pos, 0, ts.Diagnostics._0_list_cannot_be_empty, listType); + } + } + function checkGrammarClassDeclarationHeritageClauses(node) { + var seenExtendsClause = false; + var seenImplementsClause = false; + if (!checkGrammarDecorators(node) && !checkGrammarModifiers(node) && node.heritageClauses) { + for (var _i = 0, _a = node.heritageClauses; _i < _a.length; _i++) { + var heritageClause = _a[_i]; + if (heritageClause.token === 83 /* ExtendsKeyword */) { + if (seenExtendsClause) { + return grammarErrorOnFirstToken(heritageClause, ts.Diagnostics.extends_clause_already_seen); + } + if (seenImplementsClause) { + return grammarErrorOnFirstToken(heritageClause, ts.Diagnostics.extends_clause_must_precede_implements_clause); + } + if (heritageClause.types.length > 1) { + return grammarErrorOnFirstToken(heritageClause.types[1], ts.Diagnostics.Classes_can_only_extend_a_single_class); + } + seenExtendsClause = true; + } + else { + ts.Debug.assert(heritageClause.token === 106 /* ImplementsKeyword */); + if (seenImplementsClause) { + return grammarErrorOnFirstToken(heritageClause, ts.Diagnostics.implements_clause_already_seen); + } + seenImplementsClause = true; + } + // Grammar checking heritageClause inside class declaration + checkGrammarHeritageClause(heritageClause); + } + } + } + function checkGrammarInterfaceDeclaration(node) { + var seenExtendsClause = false; + if (node.heritageClauses) { + for (var _i = 0, _a = node.heritageClauses; _i < _a.length; _i++) { + var heritageClause = _a[_i]; + if (heritageClause.token === 83 /* ExtendsKeyword */) { + if (seenExtendsClause) { + return grammarErrorOnFirstToken(heritageClause, ts.Diagnostics.extends_clause_already_seen); + } + seenExtendsClause = true; + } + else { + ts.Debug.assert(heritageClause.token === 106 /* ImplementsKeyword */); + return grammarErrorOnFirstToken(heritageClause, ts.Diagnostics.Interface_declaration_cannot_have_implements_clause); + } + // Grammar checking heritageClause inside class declaration + checkGrammarHeritageClause(heritageClause); + } + } + return false; + } + function checkGrammarComputedPropertyName(node) { + // If node is not a computedPropertyName, just skip the grammar checking + if (node.kind !== 140 /* ComputedPropertyName */) { + return false; + } + var computedPropertyName = node; + if (computedPropertyName.expression.kind === 187 /* BinaryExpression */ && computedPropertyName.expression.operatorToken.kind === 24 /* CommaToken */) { + return grammarErrorOnNode(computedPropertyName.expression, ts.Diagnostics.A_comma_expression_is_not_allowed_in_a_computed_property_name); + } + } + function checkGrammarForGenerator(node) { + if (node.asteriskToken) { + ts.Debug.assert(node.kind === 220 /* FunctionDeclaration */ || + node.kind === 179 /* FunctionExpression */ || + node.kind === 147 /* MethodDeclaration */); + if (ts.isInAmbientContext(node)) { + return grammarErrorOnNode(node.asteriskToken, ts.Diagnostics.Generators_are_not_allowed_in_an_ambient_context); + } + if (!node.body) { + return grammarErrorOnNode(node.asteriskToken, ts.Diagnostics.An_overload_signature_cannot_be_declared_as_a_generator); + } + if (languageVersion < 2 /* ES6 */) { + return grammarErrorOnNode(node.asteriskToken, ts.Diagnostics.Generators_are_only_available_when_targeting_ECMAScript_2015_or_higher); + } + } + } + function checkGrammarForInvalidQuestionMark(node, questionToken, message) { + if (questionToken) { + return grammarErrorOnNode(questionToken, message); + } + } + function checkGrammarObjectLiteralExpression(node, inDestructuring) { + var seen = ts.createMap(); + var Property = 1; + var GetAccessor = 2; + var SetAccessor = 4; + var GetOrSetAccessor = GetAccessor | SetAccessor; + for (var _i = 0, _a = node.properties; _i < _a.length; _i++) { + var prop = _a[_i]; + var name_24 = prop.name; + if (name_24.kind === 140 /* ComputedPropertyName */) { + // If the name is not a ComputedPropertyName, the grammar checking will skip it + checkGrammarComputedPropertyName(name_24); + } + if (prop.kind === 254 /* ShorthandPropertyAssignment */ && !inDestructuring && prop.objectAssignmentInitializer) { + // having objectAssignmentInitializer is only valid in ObjectAssignmentPattern + // outside of destructuring it is a syntax error + return grammarErrorOnNode(prop.equalsToken, ts.Diagnostics.can_only_be_used_in_an_object_literal_property_inside_a_destructuring_assignment); + } + // Modifiers are never allowed on properties except for 'async' on a method declaration + if (prop.modifiers) { + for (var _b = 0, _c = prop.modifiers; _b < _c.length; _b++) { + var mod = _c[_b]; + if (mod.kind !== 118 /* AsyncKeyword */ || prop.kind !== 147 /* MethodDeclaration */) { + grammarErrorOnNode(mod, ts.Diagnostics._0_modifier_cannot_be_used_here, ts.getTextOfNode(mod)); + } + } + } + // ECMA-262 11.1.5 Object Initializer + // If previous is not undefined then throw a SyntaxError exception if any of the following conditions are true + // a.This production is contained in strict code and IsDataDescriptor(previous) is true and + // IsDataDescriptor(propId.descriptor) is true. + // b.IsDataDescriptor(previous) is true and IsAccessorDescriptor(propId.descriptor) is true. + // c.IsAccessorDescriptor(previous) is true and IsDataDescriptor(propId.descriptor) is true. + // d.IsAccessorDescriptor(previous) is true and IsAccessorDescriptor(propId.descriptor) is true + // and either both previous and propId.descriptor have[[Get]] fields or both previous and propId.descriptor have[[Set]] fields + var currentKind = void 0; + if (prop.kind === 253 /* PropertyAssignment */ || prop.kind === 254 /* ShorthandPropertyAssignment */) { + // Grammar checking for computedPropertyName and shorthandPropertyAssignment + checkGrammarForInvalidQuestionMark(prop, prop.questionToken, ts.Diagnostics.An_object_member_cannot_be_declared_optional); + if (name_24.kind === 8 /* NumericLiteral */) { + checkGrammarNumericLiteral(name_24); + } + currentKind = Property; + } + else if (prop.kind === 147 /* MethodDeclaration */) { + currentKind = Property; + } + else if (prop.kind === 149 /* GetAccessor */) { + currentKind = GetAccessor; + } + else if (prop.kind === 150 /* SetAccessor */) { + currentKind = SetAccessor; + } + else { + ts.Debug.fail("Unexpected syntax kind:" + prop.kind); + } + var effectiveName = ts.getPropertyNameForPropertyNameNode(name_24); + if (effectiveName === undefined) { + continue; + } + if (!seen[effectiveName]) { + seen[effectiveName] = currentKind; + } + else { + var existingKind = seen[effectiveName]; + if (currentKind === Property && existingKind === Property) { + grammarErrorOnNode(name_24, ts.Diagnostics.Duplicate_identifier_0, ts.getTextOfNode(name_24)); + } + else if ((currentKind & GetOrSetAccessor) && (existingKind & GetOrSetAccessor)) { + if (existingKind !== GetOrSetAccessor && currentKind !== existingKind) { + seen[effectiveName] = currentKind | existingKind; + } + else { + return grammarErrorOnNode(name_24, ts.Diagnostics.An_object_literal_cannot_have_multiple_get_Slashset_accessors_with_the_same_name); + } + } + else { + return grammarErrorOnNode(name_24, ts.Diagnostics.An_object_literal_cannot_have_property_and_accessor_with_the_same_name); + } + } + } + } + function checkGrammarJsxElement(node) { + var seen = ts.createMap(); + for (var _i = 0, _a = node.attributes; _i < _a.length; _i++) { + var attr = _a[_i]; + if (attr.kind === 247 /* JsxSpreadAttribute */) { + continue; + } + var jsxAttr = attr; + var name_25 = jsxAttr.name; + if (!seen[name_25.text]) { + seen[name_25.text] = true; + } + else { + return grammarErrorOnNode(name_25, ts.Diagnostics.JSX_elements_cannot_have_multiple_attributes_with_the_same_name); + } + var initializer = jsxAttr.initializer; + if (initializer && initializer.kind === 248 /* JsxExpression */ && !initializer.expression) { + return grammarErrorOnNode(jsxAttr.initializer, ts.Diagnostics.JSX_attributes_must_only_be_assigned_a_non_empty_expression); + } + } + } + function checkGrammarForInOrForOfStatement(forInOrOfStatement) { + if (checkGrammarStatementInAmbientContext(forInOrOfStatement)) { + return true; + } + if (forInOrOfStatement.initializer.kind === 219 /* VariableDeclarationList */) { + var variableList = forInOrOfStatement.initializer; + if (!checkGrammarVariableDeclarationList(variableList)) { + var declarations = variableList.declarations; + // declarations.length can be zero if there is an error in variable declaration in for-of or for-in + // See http://www.ecma-international.org/ecma-262/6.0/#sec-for-in-and-for-of-statements for details + // For example: + // var let = 10; + // for (let of [1,2,3]) {} // this is invalid ES6 syntax + // for (let in [1,2,3]) {} // this is invalid ES6 syntax + // We will then want to skip on grammar checking on variableList declaration + if (!declarations.length) { + return false; + } + if (declarations.length > 1) { + var diagnostic = forInOrOfStatement.kind === 207 /* ForInStatement */ + ? ts.Diagnostics.Only_a_single_variable_declaration_is_allowed_in_a_for_in_statement + : ts.Diagnostics.Only_a_single_variable_declaration_is_allowed_in_a_for_of_statement; + return grammarErrorOnFirstToken(variableList.declarations[1], diagnostic); + } + var firstDeclaration = declarations[0]; + if (firstDeclaration.initializer) { + var diagnostic = forInOrOfStatement.kind === 207 /* ForInStatement */ + ? ts.Diagnostics.The_variable_declaration_of_a_for_in_statement_cannot_have_an_initializer + : ts.Diagnostics.The_variable_declaration_of_a_for_of_statement_cannot_have_an_initializer; + return grammarErrorOnNode(firstDeclaration.name, diagnostic); + } + if (firstDeclaration.type) { + var diagnostic = forInOrOfStatement.kind === 207 /* ForInStatement */ + ? ts.Diagnostics.The_left_hand_side_of_a_for_in_statement_cannot_use_a_type_annotation + : ts.Diagnostics.The_left_hand_side_of_a_for_of_statement_cannot_use_a_type_annotation; + return grammarErrorOnNode(firstDeclaration, diagnostic); + } + } + } + return false; + } + function checkGrammarAccessor(accessor) { + var kind = accessor.kind; + if (languageVersion < 1 /* ES5 */) { + return grammarErrorOnNode(accessor.name, ts.Diagnostics.Accessors_are_only_available_when_targeting_ECMAScript_5_and_higher); + } + else if (ts.isInAmbientContext(accessor)) { + return grammarErrorOnNode(accessor.name, ts.Diagnostics.An_accessor_cannot_be_declared_in_an_ambient_context); + } + else if (accessor.body === undefined && !(ts.getModifierFlags(accessor) & 128 /* Abstract */)) { + return grammarErrorAtPos(ts.getSourceFileOfNode(accessor), accessor.end - 1, ";".length, ts.Diagnostics._0_expected, "{"); + } + else if (accessor.typeParameters) { + return grammarErrorOnNode(accessor.name, ts.Diagnostics.An_accessor_cannot_have_type_parameters); + } + else if (!doesAccessorHaveCorrectParameterCount(accessor)) { + return grammarErrorOnNode(accessor.name, kind === 149 /* GetAccessor */ ? + ts.Diagnostics.A_get_accessor_cannot_have_parameters : + ts.Diagnostics.A_set_accessor_must_have_exactly_one_parameter); + } + else if (kind === 150 /* SetAccessor */) { + if (accessor.type) { + return grammarErrorOnNode(accessor.name, ts.Diagnostics.A_set_accessor_cannot_have_a_return_type_annotation); + } + else { + var parameter = accessor.parameters[0]; + if (parameter.dotDotDotToken) { + return grammarErrorOnNode(parameter.dotDotDotToken, ts.Diagnostics.A_set_accessor_cannot_have_rest_parameter); + } + else if (parameter.questionToken) { + return grammarErrorOnNode(parameter.questionToken, ts.Diagnostics.A_set_accessor_cannot_have_an_optional_parameter); + } + else if (parameter.initializer) { + return grammarErrorOnNode(accessor.name, ts.Diagnostics.A_set_accessor_parameter_cannot_have_an_initializer); + } + } + } + } + /** Does the accessor have the right number of parameters? + + A get accessor has no parameters or a single `this` parameter. + A set accessor has one parameter or a `this` parameter and one more parameter */ + function doesAccessorHaveCorrectParameterCount(accessor) { + return getAccessorThisParameter(accessor) || accessor.parameters.length === (accessor.kind === 149 /* GetAccessor */ ? 0 : 1); + } + function getAccessorThisParameter(accessor) { + if (accessor.parameters.length === (accessor.kind === 149 /* GetAccessor */ ? 1 : 2)) { + return ts.getThisParameter(accessor); + } + } + function checkGrammarForNonSymbolComputedProperty(node, message) { + if (ts.isDynamicName(node)) { + return grammarErrorOnNode(node, message); + } + } + function checkGrammarMethod(node) { + if (checkGrammarDisallowedModifiersOnObjectLiteralExpressionMethod(node) || + checkGrammarFunctionLikeDeclaration(node) || + checkGrammarForGenerator(node)) { + return true; + } + if (node.parent.kind === 171 /* ObjectLiteralExpression */) { + if (checkGrammarForInvalidQuestionMark(node, node.questionToken, ts.Diagnostics.An_object_member_cannot_be_declared_optional)) { + return true; + } + else if (node.body === undefined) { + return grammarErrorAtPos(ts.getSourceFileOfNode(node), node.end - 1, ";".length, ts.Diagnostics._0_expected, "{"); + } + } + if (ts.isClassLike(node.parent)) { + // Technically, computed properties in ambient contexts is disallowed + // for property declarations and accessors too, not just methods. + // However, property declarations disallow computed names in general, + // and accessors are not allowed in ambient contexts in general, + // so this error only really matters for methods. + if (ts.isInAmbientContext(node)) { + return checkGrammarForNonSymbolComputedProperty(node.name, ts.Diagnostics.A_computed_property_name_in_an_ambient_context_must_directly_refer_to_a_built_in_symbol); + } + else if (!node.body) { + return checkGrammarForNonSymbolComputedProperty(node.name, ts.Diagnostics.A_computed_property_name_in_a_method_overload_must_directly_refer_to_a_built_in_symbol); + } + } + else if (node.parent.kind === 222 /* InterfaceDeclaration */) { + return checkGrammarForNonSymbolComputedProperty(node.name, ts.Diagnostics.A_computed_property_name_in_an_interface_must_directly_refer_to_a_built_in_symbol); + } + else if (node.parent.kind === 159 /* TypeLiteral */) { + return checkGrammarForNonSymbolComputedProperty(node.name, ts.Diagnostics.A_computed_property_name_in_a_type_literal_must_directly_refer_to_a_built_in_symbol); + } + } + function checkGrammarBreakOrContinueStatement(node) { + var current = node; + while (current) { + if (ts.isFunctionLike(current)) { + return grammarErrorOnNode(node, ts.Diagnostics.Jump_target_cannot_cross_function_boundary); + } + switch (current.kind) { + case 214 /* LabeledStatement */: + if (node.label && current.label.text === node.label.text) { + // found matching label - verify that label usage is correct + // continue can only target labels that are on iteration statements + var isMisplacedContinueLabel = node.kind === 209 /* ContinueStatement */ + && !ts.isIterationStatement(current.statement, /*lookInLabeledStatement*/ true); + if (isMisplacedContinueLabel) { + return grammarErrorOnNode(node, ts.Diagnostics.A_continue_statement_can_only_jump_to_a_label_of_an_enclosing_iteration_statement); + } + return false; + } + break; + case 213 /* SwitchStatement */: + if (node.kind === 210 /* BreakStatement */ && !node.label) { + // unlabeled break within switch statement - ok + return false; + } + break; + default: + if (ts.isIterationStatement(current, /*lookInLabeledStatement*/ false) && !node.label) { + // unlabeled break or continue within iteration statement - ok + return false; + } + break; + } + current = current.parent; + } + if (node.label) { + var message = node.kind === 210 /* BreakStatement */ + ? ts.Diagnostics.A_break_statement_can_only_jump_to_a_label_of_an_enclosing_statement + : ts.Diagnostics.A_continue_statement_can_only_jump_to_a_label_of_an_enclosing_iteration_statement; + return grammarErrorOnNode(node, message); + } + else { + var message = node.kind === 210 /* BreakStatement */ + ? ts.Diagnostics.A_break_statement_can_only_be_used_within_an_enclosing_iteration_or_switch_statement + : ts.Diagnostics.A_continue_statement_can_only_be_used_within_an_enclosing_iteration_statement; + return grammarErrorOnNode(node, message); + } + } + function checkGrammarBindingElement(node) { + if (node.dotDotDotToken) { + var elements = node.parent.elements; + if (node !== ts.lastOrUndefined(elements)) { + return grammarErrorOnNode(node, ts.Diagnostics.A_rest_element_must_be_last_in_an_array_destructuring_pattern); + } + if (node.name.kind === 168 /* ArrayBindingPattern */ || node.name.kind === 167 /* ObjectBindingPattern */) { + return grammarErrorOnNode(node.name, ts.Diagnostics.A_rest_element_cannot_contain_a_binding_pattern); + } + if (node.initializer) { + // Error on equals token which immediate precedes the initializer + return grammarErrorAtPos(ts.getSourceFileOfNode(node), node.initializer.pos - 1, 1, ts.Diagnostics.A_rest_element_cannot_have_an_initializer); + } + } + } + function isStringOrNumberLiteralExpression(expr) { + return expr.kind === 9 /* StringLiteral */ || expr.kind === 8 /* NumericLiteral */ || + expr.kind === 185 /* PrefixUnaryExpression */ && expr.operator === 36 /* MinusToken */ && + expr.operand.kind === 8 /* NumericLiteral */; + } + function checkGrammarVariableDeclaration(node) { + if (node.parent.parent.kind !== 207 /* ForInStatement */ && node.parent.parent.kind !== 208 /* ForOfStatement */) { + if (ts.isInAmbientContext(node)) { + if (node.initializer) { + if (ts.isConst(node) && !node.type) { + if (!isStringOrNumberLiteralExpression(node.initializer)) { + return grammarErrorOnNode(node.initializer, ts.Diagnostics.A_const_initializer_in_an_ambient_context_must_be_a_string_or_numeric_literal); + } + } + else { + // Error on equals token which immediate precedes the initializer + var equalsTokenLength = "=".length; + return grammarErrorAtPos(ts.getSourceFileOfNode(node), node.initializer.pos - equalsTokenLength, equalsTokenLength, ts.Diagnostics.Initializers_are_not_allowed_in_ambient_contexts); + } + } + if (node.initializer && !(ts.isConst(node) && isStringOrNumberLiteralExpression(node.initializer))) { + // Error on equals token which immediate precedes the initializer + var equalsTokenLength = "=".length; + return grammarErrorAtPos(ts.getSourceFileOfNode(node), node.initializer.pos - equalsTokenLength, equalsTokenLength, ts.Diagnostics.Initializers_are_not_allowed_in_ambient_contexts); + } + } + else if (!node.initializer) { + if (ts.isBindingPattern(node.name) && !ts.isBindingPattern(node.parent)) { + return grammarErrorOnNode(node, ts.Diagnostics.A_destructuring_declaration_must_have_an_initializer); + } + if (ts.isConst(node)) { + return grammarErrorOnNode(node, ts.Diagnostics.const_declarations_must_be_initialized); + } + } + } + var checkLetConstNames = (ts.isLet(node) || ts.isConst(node)); + // 1. LexicalDeclaration : LetOrConst BindingList ; + // It is a Syntax Error if the BoundNames of BindingList contains "let". + // 2. ForDeclaration: ForDeclaration : LetOrConst ForBinding + // It is a Syntax Error if the BoundNames of ForDeclaration contains "let". + // It is a SyntaxError if a VariableDeclaration or VariableDeclarationNoIn occurs within strict code + // and its Identifier is eval or arguments + return checkLetConstNames && checkGrammarNameInLetOrConstDeclarations(node.name); + } + function checkGrammarNameInLetOrConstDeclarations(name) { + if (name.kind === 69 /* Identifier */) { + if (name.originalKeywordKind === 108 /* LetKeyword */) { + return grammarErrorOnNode(name, ts.Diagnostics.let_is_not_allowed_to_be_used_as_a_name_in_let_or_const_declarations); + } + } + else { + var elements = name.elements; + for (var _i = 0, elements_2 = elements; _i < elements_2.length; _i++) { + var element = elements_2[_i]; + if (!ts.isOmittedExpression(element)) { + checkGrammarNameInLetOrConstDeclarations(element.name); + } + } + } + } + function checkGrammarVariableDeclarationList(declarationList) { + var declarations = declarationList.declarations; + if (checkGrammarForDisallowedTrailingComma(declarationList.declarations)) { + return true; + } + if (!declarationList.declarations.length) { + return grammarErrorAtPos(ts.getSourceFileOfNode(declarationList), declarations.pos, declarations.end - declarations.pos, ts.Diagnostics.Variable_declaration_list_cannot_be_empty); + } + } + function allowLetAndConstDeclarations(parent) { + switch (parent.kind) { + case 203 /* IfStatement */: + case 204 /* DoStatement */: + case 205 /* WhileStatement */: + case 212 /* WithStatement */: + case 206 /* ForStatement */: + case 207 /* ForInStatement */: + case 208 /* ForOfStatement */: + return false; + case 214 /* LabeledStatement */: + return allowLetAndConstDeclarations(parent.parent); + } + return true; + } + function checkGrammarForDisallowedLetOrConstStatement(node) { + if (!allowLetAndConstDeclarations(node.parent)) { + if (ts.isLet(node.declarationList)) { + return grammarErrorOnNode(node, ts.Diagnostics.let_declarations_can_only_be_declared_inside_a_block); + } + else if (ts.isConst(node.declarationList)) { + return grammarErrorOnNode(node, ts.Diagnostics.const_declarations_can_only_be_declared_inside_a_block); + } + } + } + function hasParseDiagnostics(sourceFile) { + return sourceFile.parseDiagnostics.length > 0; + } + function grammarErrorOnFirstToken(node, message, arg0, arg1, arg2) { + var sourceFile = ts.getSourceFileOfNode(node); + if (!hasParseDiagnostics(sourceFile)) { + var span_4 = ts.getSpanOfTokenAtPosition(sourceFile, node.pos); + diagnostics.add(ts.createFileDiagnostic(sourceFile, span_4.start, span_4.length, message, arg0, arg1, arg2)); + return true; + } + } + function grammarErrorAtPos(sourceFile, start, length, message, arg0, arg1, arg2) { + if (!hasParseDiagnostics(sourceFile)) { + diagnostics.add(ts.createFileDiagnostic(sourceFile, start, length, message, arg0, arg1, arg2)); + return true; + } + } + function grammarErrorOnNode(node, message, arg0, arg1, arg2) { + var sourceFile = ts.getSourceFileOfNode(node); + if (!hasParseDiagnostics(sourceFile)) { + diagnostics.add(ts.createDiagnosticForNode(node, message, arg0, arg1, arg2)); + return true; + } + } + function checkGrammarConstructorTypeParameters(node) { + if (node.typeParameters) { + return grammarErrorAtPos(ts.getSourceFileOfNode(node), node.typeParameters.pos, node.typeParameters.end - node.typeParameters.pos, ts.Diagnostics.Type_parameters_cannot_appear_on_a_constructor_declaration); + } + } + function checkGrammarConstructorTypeAnnotation(node) { + if (node.type) { + return grammarErrorOnNode(node.type, ts.Diagnostics.Type_annotation_cannot_appear_on_a_constructor_declaration); + } + } + function checkGrammarProperty(node) { + if (ts.isClassLike(node.parent)) { + if (checkGrammarForNonSymbolComputedProperty(node.name, ts.Diagnostics.A_computed_property_name_in_a_class_property_declaration_must_directly_refer_to_a_built_in_symbol)) { + return true; + } + } + else if (node.parent.kind === 222 /* InterfaceDeclaration */) { + if (checkGrammarForNonSymbolComputedProperty(node.name, ts.Diagnostics.A_computed_property_name_in_an_interface_must_directly_refer_to_a_built_in_symbol)) { + return true; + } + if (node.initializer) { + return grammarErrorOnNode(node.initializer, ts.Diagnostics.An_interface_property_cannot_have_an_initializer); + } + } + else if (node.parent.kind === 159 /* TypeLiteral */) { + if (checkGrammarForNonSymbolComputedProperty(node.name, ts.Diagnostics.A_computed_property_name_in_a_type_literal_must_directly_refer_to_a_built_in_symbol)) { + return true; + } + if (node.initializer) { + return grammarErrorOnNode(node.initializer, ts.Diagnostics.A_type_literal_property_cannot_have_an_initializer); + } + } + if (ts.isInAmbientContext(node) && node.initializer) { + return grammarErrorOnFirstToken(node.initializer, ts.Diagnostics.Initializers_are_not_allowed_in_ambient_contexts); + } + } + function checkGrammarTopLevelElementForRequiredDeclareModifier(node) { + // A declare modifier is required for any top level .d.ts declaration except export=, export default, export as namespace + // interfaces and imports categories: + // + // DeclarationElement: + // ExportAssignment + // export_opt InterfaceDeclaration + // export_opt TypeAliasDeclaration + // export_opt ImportDeclaration + // export_opt ExternalImportDeclaration + // export_opt AmbientDeclaration + // + // TODO: The spec needs to be amended to reflect this grammar. + if (node.kind === 222 /* InterfaceDeclaration */ || + node.kind === 223 /* TypeAliasDeclaration */ || + node.kind === 230 /* ImportDeclaration */ || + node.kind === 229 /* ImportEqualsDeclaration */ || + node.kind === 236 /* ExportDeclaration */ || + node.kind === 235 /* ExportAssignment */ || + node.kind === 228 /* NamespaceExportDeclaration */ || + ts.getModifierFlags(node) & (2 /* Ambient */ | 1 /* Export */ | 512 /* Default */)) { + return false; + } + return grammarErrorOnFirstToken(node, ts.Diagnostics.A_declare_modifier_is_required_for_a_top_level_declaration_in_a_d_ts_file); + } + function checkGrammarTopLevelElementsForRequiredDeclareModifier(file) { + for (var _i = 0, _a = file.statements; _i < _a.length; _i++) { + var decl = _a[_i]; + if (ts.isDeclaration(decl) || decl.kind === 200 /* VariableStatement */) { + if (checkGrammarTopLevelElementForRequiredDeclareModifier(decl)) { + return true; + } + } + } + } + function checkGrammarSourceFile(node) { + return ts.isInAmbientContext(node) && checkGrammarTopLevelElementsForRequiredDeclareModifier(node); + } + function checkGrammarStatementInAmbientContext(node) { + if (ts.isInAmbientContext(node)) { + // An accessors is already reported about the ambient context + if (isAccessor(node.parent.kind)) { + return getNodeLinks(node).hasReportedStatementInAmbientContext = true; + } + // Find containing block which is either Block, ModuleBlock, SourceFile + var links = getNodeLinks(node); + if (!links.hasReportedStatementInAmbientContext && ts.isFunctionLike(node.parent)) { + return getNodeLinks(node).hasReportedStatementInAmbientContext = grammarErrorOnFirstToken(node, ts.Diagnostics.An_implementation_cannot_be_declared_in_ambient_contexts); + } + // We are either parented by another statement, or some sort of block. + // If we're in a block, we only want to really report an error once + // to prevent noisiness. So use a bit on the block to indicate if + // this has already been reported, and don't report if it has. + // + if (node.parent.kind === 199 /* Block */ || node.parent.kind === 226 /* ModuleBlock */ || node.parent.kind === 256 /* SourceFile */) { + var links_1 = getNodeLinks(node.parent); + // Check if the containing block ever report this error + if (!links_1.hasReportedStatementInAmbientContext) { + return links_1.hasReportedStatementInAmbientContext = grammarErrorOnFirstToken(node, ts.Diagnostics.Statements_are_not_allowed_in_ambient_contexts); + } + } + else { + } + } + } + function checkGrammarNumericLiteral(node) { + // Grammar checking + if (node.isOctalLiteral && languageVersion >= 1 /* ES5 */) { + return grammarErrorOnNode(node, ts.Diagnostics.Octal_literals_are_not_available_when_targeting_ECMAScript_5_and_higher); + } + } + function grammarErrorAfterFirstToken(node, message, arg0, arg1, arg2) { + var sourceFile = ts.getSourceFileOfNode(node); + if (!hasParseDiagnostics(sourceFile)) { + var span_5 = ts.getSpanOfTokenAtPosition(sourceFile, node.pos); + diagnostics.add(ts.createFileDiagnostic(sourceFile, ts.textSpanEnd(span_5), /*length*/ 0, message, arg0, arg1, arg2)); + return true; + } + } + function getAmbientModules() { + var result = []; + for (var sym in globals) { + if (ambientModuleSymbolRegex.test(sym)) { + result.push(globals[sym]); + } + } + return result; + } + } + ts.createTypeChecker = createTypeChecker; +})(ts || (ts = {})); +/// +/// +/// +/* @internal */ +var ts; +(function (ts) { + ; + /** + * This map contains information about the shape of each Node in "types.ts" pertaining to how + * each node should be traversed during a transformation. + * + * Each edge corresponds to a property in a Node subtype that should be traversed when visiting + * each child. The properties are assigned in the order in which traversal should occur. + * + * We only add entries for nodes that do not have a create/update pair defined in factory.ts + * + * NOTE: This needs to be kept up to date with changes to nodes in "types.ts". Currently, this + * map is not comprehensive. Only node edges relevant to tree transformation are + * currently defined. We may extend this to be more comprehensive, and eventually + * supplant the existing `forEachChild` implementation if performance is not + * significantly impacted. + */ + var nodeEdgeTraversalMap = ts.createMap((_a = {}, + _a[139 /* QualifiedName */] = [ + { name: "left", test: ts.isEntityName }, + { name: "right", test: ts.isIdentifier } + ], + _a[143 /* Decorator */] = [ + { name: "expression", test: ts.isLeftHandSideExpression } + ], + _a[177 /* TypeAssertionExpression */] = [ + { name: "type", test: ts.isTypeNode }, + { name: "expression", test: ts.isUnaryExpression } + ], + _a[195 /* AsExpression */] = [ + { name: "expression", test: ts.isExpression }, + { name: "type", test: ts.isTypeNode } + ], + _a[196 /* NonNullExpression */] = [ + { name: "expression", test: ts.isLeftHandSideExpression } + ], + _a[224 /* EnumDeclaration */] = [ + { name: "decorators", test: ts.isDecorator }, + { name: "modifiers", test: ts.isModifier }, + { name: "name", test: ts.isIdentifier }, + { name: "members", test: ts.isEnumMember } + ], + _a[225 /* ModuleDeclaration */] = [ + { name: "decorators", test: ts.isDecorator }, + { name: "modifiers", test: ts.isModifier }, + { name: "name", test: ts.isModuleName }, + { name: "body", test: ts.isModuleBody } + ], + _a[226 /* ModuleBlock */] = [ + { name: "statements", test: ts.isStatement } + ], + _a[229 /* ImportEqualsDeclaration */] = [ + { name: "decorators", test: ts.isDecorator }, + { name: "modifiers", test: ts.isModifier }, + { name: "name", test: ts.isIdentifier }, + { name: "moduleReference", test: ts.isModuleReference } + ], + _a[240 /* ExternalModuleReference */] = [ + { name: "expression", test: ts.isExpression, optional: true } + ], + _a[255 /* EnumMember */] = [ + { name: "name", test: ts.isPropertyName }, + { name: "initializer", test: ts.isExpression, optional: true, parenthesize: ts.parenthesizeExpressionForList } + ], + _a)); + function reduceNode(node, f, initial) { + return node ? f(initial, node) : initial; + } + /** + * Similar to `reduceLeft`, performs a reduction against each child of a node. + * NOTE: Unlike `forEachChild`, this does *not* visit every node. Only nodes added to the + * `nodeEdgeTraversalMap` above will be visited. + * + * @param node The node containing the children to reduce. + * @param f The callback function + * @param initial The initial value to supply to the reduction. + */ + function reduceEachChild(node, f, initial) { + if (node === undefined) { + return initial; + } + var kind = node.kind; + // No need to visit nodes with no children. + if ((kind > 0 /* FirstToken */ && kind <= 138 /* LastToken */)) { + return initial; + } + // We do not yet support types. + if ((kind >= 154 /* TypePredicate */ && kind <= 166 /* LiteralType */)) { + return initial; + } + var result = initial; + switch (node.kind) { + // Leaf nodes + case 198 /* SemicolonClassElement */: + case 201 /* EmptyStatement */: + case 193 /* OmittedExpression */: + case 217 /* DebuggerStatement */: + case 287 /* NotEmittedStatement */: + // No need to visit nodes with no children. + break; + // Names + case 140 /* ComputedPropertyName */: + result = reduceNode(node.expression, f, result); + break; + // Signature elements + case 142 /* Parameter */: + result = ts.reduceLeft(node.decorators, f, result); + result = ts.reduceLeft(node.modifiers, f, result); + result = reduceNode(node.name, f, result); + result = reduceNode(node.type, f, result); + result = reduceNode(node.initializer, f, result); + break; + case 143 /* Decorator */: + result = reduceNode(node.expression, f, result); + break; + // Type member + case 145 /* PropertyDeclaration */: + result = ts.reduceLeft(node.decorators, f, result); + result = ts.reduceLeft(node.modifiers, f, result); + result = reduceNode(node.name, f, result); + result = reduceNode(node.type, f, result); + result = reduceNode(node.initializer, f, result); + break; + case 147 /* MethodDeclaration */: + result = ts.reduceLeft(node.decorators, f, result); + result = ts.reduceLeft(node.modifiers, f, result); + result = reduceNode(node.name, f, result); + result = ts.reduceLeft(node.typeParameters, f, result); + result = ts.reduceLeft(node.parameters, f, result); + result = reduceNode(node.type, f, result); + result = reduceNode(node.body, f, result); + break; + case 148 /* Constructor */: + result = ts.reduceLeft(node.modifiers, f, result); + result = ts.reduceLeft(node.parameters, f, result); + result = reduceNode(node.body, f, result); + break; + case 149 /* GetAccessor */: + result = ts.reduceLeft(node.decorators, f, result); + result = ts.reduceLeft(node.modifiers, f, result); + result = reduceNode(node.name, f, result); + result = ts.reduceLeft(node.parameters, f, result); + result = reduceNode(node.type, f, result); + result = reduceNode(node.body, f, result); + break; + case 150 /* SetAccessor */: + result = ts.reduceLeft(node.decorators, f, result); + result = ts.reduceLeft(node.modifiers, f, result); + result = reduceNode(node.name, f, result); + result = ts.reduceLeft(node.parameters, f, result); + result = reduceNode(node.body, f, result); + break; + // Binding patterns + case 167 /* ObjectBindingPattern */: + case 168 /* ArrayBindingPattern */: + result = ts.reduceLeft(node.elements, f, result); + break; + case 169 /* BindingElement */: + result = reduceNode(node.propertyName, f, result); + result = reduceNode(node.name, f, result); + result = reduceNode(node.initializer, f, result); + break; + // Expression + case 170 /* ArrayLiteralExpression */: + result = ts.reduceLeft(node.elements, f, result); + break; + case 171 /* ObjectLiteralExpression */: + result = ts.reduceLeft(node.properties, f, result); + break; + case 172 /* PropertyAccessExpression */: + result = reduceNode(node.expression, f, result); + result = reduceNode(node.name, f, result); + break; + case 173 /* ElementAccessExpression */: + result = reduceNode(node.expression, f, result); + result = reduceNode(node.argumentExpression, f, result); + break; + case 174 /* CallExpression */: + result = reduceNode(node.expression, f, result); + result = ts.reduceLeft(node.typeArguments, f, result); + result = ts.reduceLeft(node.arguments, f, result); + break; + case 175 /* NewExpression */: + result = reduceNode(node.expression, f, result); + result = ts.reduceLeft(node.typeArguments, f, result); + result = ts.reduceLeft(node.arguments, f, result); + break; + case 176 /* TaggedTemplateExpression */: + result = reduceNode(node.tag, f, result); + result = reduceNode(node.template, f, result); + break; + case 179 /* FunctionExpression */: + result = ts.reduceLeft(node.modifiers, f, result); + result = reduceNode(node.name, f, result); + result = ts.reduceLeft(node.typeParameters, f, result); + result = ts.reduceLeft(node.parameters, f, result); + result = reduceNode(node.type, f, result); + result = reduceNode(node.body, f, result); + break; + case 180 /* ArrowFunction */: + result = ts.reduceLeft(node.modifiers, f, result); + result = ts.reduceLeft(node.typeParameters, f, result); + result = ts.reduceLeft(node.parameters, f, result); + result = reduceNode(node.type, f, result); + result = reduceNode(node.body, f, result); + break; + case 178 /* ParenthesizedExpression */: + case 181 /* DeleteExpression */: + case 182 /* TypeOfExpression */: + case 183 /* VoidExpression */: + case 184 /* AwaitExpression */: + case 190 /* YieldExpression */: + case 191 /* SpreadElementExpression */: + case 196 /* NonNullExpression */: + result = reduceNode(node.expression, f, result); + break; + case 185 /* PrefixUnaryExpression */: + case 186 /* PostfixUnaryExpression */: + result = reduceNode(node.operand, f, result); + break; + case 187 /* BinaryExpression */: + result = reduceNode(node.left, f, result); + result = reduceNode(node.right, f, result); + break; + case 188 /* ConditionalExpression */: + result = reduceNode(node.condition, f, result); + result = reduceNode(node.whenTrue, f, result); + result = reduceNode(node.whenFalse, f, result); + break; + case 189 /* TemplateExpression */: + result = reduceNode(node.head, f, result); + result = ts.reduceLeft(node.templateSpans, f, result); + break; + case 192 /* ClassExpression */: + result = ts.reduceLeft(node.modifiers, f, result); + result = reduceNode(node.name, f, result); + result = ts.reduceLeft(node.typeParameters, f, result); + result = ts.reduceLeft(node.heritageClauses, f, result); + result = ts.reduceLeft(node.members, f, result); + break; + case 194 /* ExpressionWithTypeArguments */: + result = reduceNode(node.expression, f, result); + result = ts.reduceLeft(node.typeArguments, f, result); + break; + // Misc + case 197 /* TemplateSpan */: + result = reduceNode(node.expression, f, result); + result = reduceNode(node.literal, f, result); + break; + // Element + case 199 /* Block */: + result = ts.reduceLeft(node.statements, f, result); + break; + case 200 /* VariableStatement */: + result = ts.reduceLeft(node.modifiers, f, result); + result = reduceNode(node.declarationList, f, result); + break; + case 202 /* ExpressionStatement */: + result = reduceNode(node.expression, f, result); + break; + case 203 /* IfStatement */: + result = reduceNode(node.expression, f, result); + result = reduceNode(node.thenStatement, f, result); + result = reduceNode(node.elseStatement, f, result); + break; + case 204 /* DoStatement */: + result = reduceNode(node.statement, f, result); + result = reduceNode(node.expression, f, result); + break; + case 205 /* WhileStatement */: + case 212 /* WithStatement */: + result = reduceNode(node.expression, f, result); + result = reduceNode(node.statement, f, result); + break; + case 206 /* ForStatement */: + result = reduceNode(node.initializer, f, result); + result = reduceNode(node.condition, f, result); + result = reduceNode(node.incrementor, f, result); + result = reduceNode(node.statement, f, result); + break; + case 207 /* ForInStatement */: + case 208 /* ForOfStatement */: + result = reduceNode(node.initializer, f, result); + result = reduceNode(node.expression, f, result); + result = reduceNode(node.statement, f, result); + break; + case 211 /* ReturnStatement */: + case 215 /* ThrowStatement */: + result = reduceNode(node.expression, f, result); + break; + case 213 /* SwitchStatement */: + result = reduceNode(node.expression, f, result); + result = reduceNode(node.caseBlock, f, result); + break; + case 214 /* LabeledStatement */: + result = reduceNode(node.label, f, result); + result = reduceNode(node.statement, f, result); + break; + case 216 /* TryStatement */: + result = reduceNode(node.tryBlock, f, result); + result = reduceNode(node.catchClause, f, result); + result = reduceNode(node.finallyBlock, f, result); + break; + case 218 /* VariableDeclaration */: + result = reduceNode(node.name, f, result); + result = reduceNode(node.type, f, result); + result = reduceNode(node.initializer, f, result); + break; + case 219 /* VariableDeclarationList */: + result = ts.reduceLeft(node.declarations, f, result); + break; + case 220 /* FunctionDeclaration */: + result = ts.reduceLeft(node.decorators, f, result); + result = ts.reduceLeft(node.modifiers, f, result); + result = reduceNode(node.name, f, result); + result = ts.reduceLeft(node.typeParameters, f, result); + result = ts.reduceLeft(node.parameters, f, result); + result = reduceNode(node.type, f, result); + result = reduceNode(node.body, f, result); + break; + case 221 /* ClassDeclaration */: + result = ts.reduceLeft(node.decorators, f, result); + result = ts.reduceLeft(node.modifiers, f, result); + result = reduceNode(node.name, f, result); + result = ts.reduceLeft(node.typeParameters, f, result); + result = ts.reduceLeft(node.heritageClauses, f, result); + result = ts.reduceLeft(node.members, f, result); + break; + case 227 /* CaseBlock */: + result = ts.reduceLeft(node.clauses, f, result); + break; + case 230 /* ImportDeclaration */: + result = ts.reduceLeft(node.decorators, f, result); + result = ts.reduceLeft(node.modifiers, f, result); + result = reduceNode(node.importClause, f, result); + result = reduceNode(node.moduleSpecifier, f, result); + break; + case 231 /* ImportClause */: + result = reduceNode(node.name, f, result); + result = reduceNode(node.namedBindings, f, result); + break; + case 232 /* NamespaceImport */: + result = reduceNode(node.name, f, result); + break; + case 233 /* NamedImports */: + case 237 /* NamedExports */: + result = ts.reduceLeft(node.elements, f, result); + break; + case 234 /* ImportSpecifier */: + case 238 /* ExportSpecifier */: + result = reduceNode(node.propertyName, f, result); + result = reduceNode(node.name, f, result); + break; + case 235 /* ExportAssignment */: + result = ts.reduceLeft(node.decorators, f, result); + result = ts.reduceLeft(node.modifiers, f, result); + result = reduceNode(node.expression, f, result); + break; + case 236 /* ExportDeclaration */: + result = ts.reduceLeft(node.decorators, f, result); + result = ts.reduceLeft(node.modifiers, f, result); + result = reduceNode(node.exportClause, f, result); + result = reduceNode(node.moduleSpecifier, f, result); + break; + // JSX + case 241 /* JsxElement */: + result = reduceNode(node.openingElement, f, result); + result = ts.reduceLeft(node.children, f, result); + result = reduceNode(node.closingElement, f, result); + break; + case 242 /* JsxSelfClosingElement */: + case 243 /* JsxOpeningElement */: + result = reduceNode(node.tagName, f, result); + result = ts.reduceLeft(node.attributes, f, result); + break; + case 245 /* JsxClosingElement */: + result = reduceNode(node.tagName, f, result); + break; + case 246 /* JsxAttribute */: + result = reduceNode(node.name, f, result); + result = reduceNode(node.initializer, f, result); + break; + case 247 /* JsxSpreadAttribute */: + result = reduceNode(node.expression, f, result); + break; + case 248 /* JsxExpression */: + result = reduceNode(node.expression, f, result); + break; + // Clauses + case 249 /* CaseClause */: + result = reduceNode(node.expression, f, result); + // fall-through + case 250 /* DefaultClause */: + result = ts.reduceLeft(node.statements, f, result); + break; + case 251 /* HeritageClause */: + result = ts.reduceLeft(node.types, f, result); + break; + case 252 /* CatchClause */: + result = reduceNode(node.variableDeclaration, f, result); + result = reduceNode(node.block, f, result); + break; + // Property assignments + case 253 /* PropertyAssignment */: + result = reduceNode(node.name, f, result); + result = reduceNode(node.initializer, f, result); + break; + case 254 /* ShorthandPropertyAssignment */: + result = reduceNode(node.name, f, result); + result = reduceNode(node.objectAssignmentInitializer, f, result); + break; + // Top-level nodes + case 256 /* SourceFile */: + result = ts.reduceLeft(node.statements, f, result); + break; + case 288 /* PartiallyEmittedExpression */: + result = reduceNode(node.expression, f, result); + break; + default: + var edgeTraversalPath = nodeEdgeTraversalMap[kind]; + if (edgeTraversalPath) { + for (var _i = 0, edgeTraversalPath_1 = edgeTraversalPath; _i < edgeTraversalPath_1.length; _i++) { + var edge = edgeTraversalPath_1[_i]; + var value = node[edge.name]; + if (value !== undefined) { + result = ts.isArray(value) + ? ts.reduceLeft(value, f, result) + : f(result, value); + } + } + } + break; + } + return result; + } + ts.reduceEachChild = reduceEachChild; + function visitNode(node, visitor, test, optional, lift, parenthesize, parentNode) { + if (node === undefined) { + return undefined; + } + var visited = visitor(node); + if (visited === node) { + return node; + } + var visitedNode; + if (visited === undefined) { + if (!optional) { + Debug.failNotOptional(); + } + return undefined; + } + else if (ts.isArray(visited)) { + visitedNode = (lift || extractSingleNode)(visited); + } + else { + visitedNode = visited; + } + if (parenthesize !== undefined) { + visitedNode = parenthesize(visitedNode, parentNode); + } + Debug.assertNode(visitedNode, test); + aggregateTransformFlags(visitedNode); + return visitedNode; + } + ts.visitNode = visitNode; + function visitNodes(nodes, visitor, test, start, count, parenthesize, parentNode) { + if (nodes === undefined) { + return undefined; + } + var updated; + // Ensure start and count have valid values + var length = nodes.length; + if (start === undefined || start < 0) { + start = 0; + } + if (count === undefined || count > length - start) { + count = length - start; + } + if (start > 0 || count < length) { + // If we are not visiting all of the original nodes, we must always create a new array. + // Since this is a fragment of a node array, we do not copy over the previous location + // and will only copy over `hasTrailingComma` if we are including the last element. + updated = ts.createNodeArray([], /*location*/ undefined, + /*hasTrailingComma*/ nodes.hasTrailingComma && start + count === length); + } + // Visit each original node. + for (var i = 0; i < count; i++) { + var node = nodes[i + start]; + var visited = node !== undefined ? visitor(node) : undefined; + if (updated !== undefined || visited === undefined || visited !== node) { + if (updated === undefined) { + // Ensure we have a copy of `nodes`, up to the current index. + updated = ts.createNodeArray(nodes.slice(0, i), /*location*/ nodes, nodes.hasTrailingComma); + } + if (visited) { + if (ts.isArray(visited)) { + for (var _i = 0, visited_1 = visited; _i < visited_1.length; _i++) { + var visitedNode = visited_1[_i]; + visitedNode = parenthesize + ? parenthesize(visitedNode, parentNode) + : visitedNode; + Debug.assertNode(visitedNode, test); + aggregateTransformFlags(visitedNode); + updated.push(visitedNode); + } + } + else { + var visitedNode = parenthesize + ? parenthesize(visited, parentNode) + : visited; + Debug.assertNode(visitedNode, test); + aggregateTransformFlags(visitedNode); + updated.push(visitedNode); + } + } + } + } + return updated || nodes; + } + ts.visitNodes = visitNodes; + function visitEachChild(node, visitor, context) { + if (node === undefined) { + return undefined; + } + var kind = node.kind; + // No need to visit nodes with no children. + if ((kind > 0 /* FirstToken */ && kind <= 138 /* LastToken */)) { + return node; + } + // We do not yet support types. + if ((kind >= 154 /* TypePredicate */ && kind <= 166 /* LiteralType */)) { + return node; + } + switch (node.kind) { + case 198 /* SemicolonClassElement */: + case 201 /* EmptyStatement */: + case 193 /* OmittedExpression */: + case 217 /* DebuggerStatement */: + // No need to visit nodes with no children. + return node; + // Names + case 140 /* ComputedPropertyName */: + return ts.updateComputedPropertyName(node, visitNode(node.expression, visitor, ts.isExpression)); + // Signature elements + case 142 /* Parameter */: + return ts.updateParameterDeclaration(node, visitNodes(node.decorators, visitor, ts.isDecorator), visitNodes(node.modifiers, visitor, ts.isModifier), visitNode(node.name, visitor, ts.isBindingName), visitNode(node.type, visitor, ts.isTypeNode, /*optional*/ true), visitNode(node.initializer, visitor, ts.isExpression, /*optional*/ true)); + // Type member + case 145 /* PropertyDeclaration */: + return ts.updateProperty(node, visitNodes(node.decorators, visitor, ts.isDecorator), visitNodes(node.modifiers, visitor, ts.isModifier), visitNode(node.name, visitor, ts.isPropertyName), visitNode(node.type, visitor, ts.isTypeNode, /*optional*/ true), visitNode(node.initializer, visitor, ts.isExpression, /*optional*/ true)); + case 147 /* MethodDeclaration */: + return ts.updateMethod(node, visitNodes(node.decorators, visitor, ts.isDecorator), visitNodes(node.modifiers, visitor, ts.isModifier), visitNode(node.name, visitor, ts.isPropertyName), visitNodes(node.typeParameters, visitor, ts.isTypeParameter), (context.startLexicalEnvironment(), visitNodes(node.parameters, visitor, ts.isParameter)), visitNode(node.type, visitor, ts.isTypeNode, /*optional*/ true), mergeFunctionBodyLexicalEnvironment(visitNode(node.body, visitor, ts.isFunctionBody, /*optional*/ true), context.endLexicalEnvironment())); + case 148 /* Constructor */: + return ts.updateConstructor(node, visitNodes(node.decorators, visitor, ts.isDecorator), visitNodes(node.modifiers, visitor, ts.isModifier), (context.startLexicalEnvironment(), visitNodes(node.parameters, visitor, ts.isParameter)), mergeFunctionBodyLexicalEnvironment(visitNode(node.body, visitor, ts.isFunctionBody, /*optional*/ true), context.endLexicalEnvironment())); + case 149 /* GetAccessor */: + return ts.updateGetAccessor(node, visitNodes(node.decorators, visitor, ts.isDecorator), visitNodes(node.modifiers, visitor, ts.isModifier), visitNode(node.name, visitor, ts.isPropertyName), (context.startLexicalEnvironment(), visitNodes(node.parameters, visitor, ts.isParameter)), visitNode(node.type, visitor, ts.isTypeNode, /*optional*/ true), mergeFunctionBodyLexicalEnvironment(visitNode(node.body, visitor, ts.isFunctionBody, /*optional*/ true), context.endLexicalEnvironment())); + case 150 /* SetAccessor */: + return ts.updateSetAccessor(node, visitNodes(node.decorators, visitor, ts.isDecorator), visitNodes(node.modifiers, visitor, ts.isModifier), visitNode(node.name, visitor, ts.isPropertyName), (context.startLexicalEnvironment(), visitNodes(node.parameters, visitor, ts.isParameter)), mergeFunctionBodyLexicalEnvironment(visitNode(node.body, visitor, ts.isFunctionBody, /*optional*/ true), context.endLexicalEnvironment())); + // Binding patterns + case 167 /* ObjectBindingPattern */: + return ts.updateObjectBindingPattern(node, visitNodes(node.elements, visitor, ts.isBindingElement)); + case 168 /* ArrayBindingPattern */: + return ts.updateArrayBindingPattern(node, visitNodes(node.elements, visitor, ts.isArrayBindingElement)); + case 169 /* BindingElement */: + return ts.updateBindingElement(node, visitNode(node.propertyName, visitor, ts.isPropertyName, /*optional*/ true), visitNode(node.name, visitor, ts.isBindingName), visitNode(node.initializer, visitor, ts.isExpression, /*optional*/ true)); + // Expression case 170 /* ArrayLiteralExpression */: return ts.updateArrayLiteral(node, visitNodes(node.elements, visitor, ts.isExpression)); case 171 /* ObjectLiteralExpression */: @@ -40673,7 +41893,7 @@ var ts; case 175 /* NewExpression */: return ts.updateNew(node, visitNode(node.expression, visitor, ts.isExpression), visitNodes(node.typeArguments, visitor, ts.isTypeNode), visitNodes(node.arguments, visitor, ts.isExpression)); case 176 /* TaggedTemplateExpression */: - return ts.updateTaggedTemplate(node, visitNode(node.tag, visitor, ts.isExpression), visitNode(node.template, visitor, ts.isTemplate)); + return ts.updateTaggedTemplate(node, visitNode(node.tag, visitor, ts.isExpression), visitNode(node.template, visitor, ts.isTemplateLiteral)); case 178 /* ParenthesizedExpression */: return ts.updateParen(node, visitNode(node.expression, visitor, ts.isExpression)); case 179 /* FunctionExpression */: @@ -40697,7 +41917,7 @@ var ts; case 188 /* ConditionalExpression */: return ts.updateConditional(node, visitNode(node.condition, visitor, ts.isExpression), visitNode(node.whenTrue, visitor, ts.isExpression), visitNode(node.whenFalse, visitor, ts.isExpression)); case 189 /* TemplateExpression */: - return ts.updateTemplateExpression(node, visitNode(node.head, visitor, ts.isTemplateLiteralFragment), visitNodes(node.templateSpans, visitor, ts.isTemplateSpan)); + return ts.updateTemplateExpression(node, visitNode(node.head, visitor, ts.isTemplateHead), visitNodes(node.templateSpans, visitor, ts.isTemplateSpan)); case 190 /* YieldExpression */: return ts.updateYield(node, visitNode(node.expression, visitor, ts.isExpression)); case 191 /* SpreadElementExpression */: @@ -40708,7 +41928,7 @@ var ts; return ts.updateExpressionWithTypeArguments(node, visitNodes(node.typeArguments, visitor, ts.isTypeNode), visitNode(node.expression, visitor, ts.isExpression)); // Misc case 197 /* TemplateSpan */: - return ts.updateTemplateSpan(node, visitNode(node.expression, visitor, ts.isExpression), visitNode(node.literal, visitor, ts.isTemplateLiteralFragment)); + return ts.updateTemplateSpan(node, visitNode(node.expression, visitor, ts.isExpression), visitNode(node.literal, visitor, ts.isTemplateMiddleOrTemplateTail)); // Element case 199 /* Block */: return ts.updateBlock(node, visitNodes(node.statements, visitor, ts.isStatement)); @@ -41055,7 +42275,7 @@ var ts; var expression = ts.createAssignment(name, value, location); // NOTE: this completely disables source maps, but aligns with the behavior of // `emitAssignment` in the old emitter. - context.setNodeEmitFlags(expression, 2048 /* NoNestedSourceMaps */); + ts.setEmitFlags(expression, 2048 /* NoNestedSourceMaps */); ts.aggregateTransformFlags(expression); expressions.push(expression); } @@ -41081,7 +42301,7 @@ var ts; var declaration = ts.createVariableDeclaration(name, /*type*/ undefined, value, location); // NOTE: this completely disables source maps, but aligns with the behavior of // `emitAssignment` in the old emitter. - context.setNodeEmitFlags(declaration, 2048 /* NoNestedSourceMaps */); + ts.setEmitFlags(declaration, 2048 /* NoNestedSourceMaps */); ts.aggregateTransformFlags(declaration); declarations.push(declaration); } @@ -41114,7 +42334,7 @@ var ts; declaration.original = original; // NOTE: this completely disables source maps, but aligns with the behavior of // `emitAssignment` in the old emitter. - context.setNodeEmitFlags(declaration, 2048 /* NoNestedSourceMaps */); + ts.setEmitFlags(declaration, 2048 /* NoNestedSourceMaps */); declarations.push(declaration); ts.aggregateTransformFlags(declaration); } @@ -41164,7 +42384,7 @@ var ts; expression.original = original; // NOTE: this completely disables source maps, but aligns with the behavior of // `emitAssignment` in the old emitter. - context.setNodeEmitFlags(expression, 2048 /* NoNestedSourceMaps */); + ts.setEmitFlags(expression, 2048 /* NoNestedSourceMaps */); pendingAssignments.push(expression); return expression; } @@ -41210,8 +42430,8 @@ var ts; } else { var name_26 = ts.getMutableClone(target); - context.setSourceMapRange(name_26, target); - context.setCommentRange(name_26, target); + ts.setSourceMapRange(name_26, target); + ts.setCommentRange(name_26, target); emitAssignment(name_26, value, location, /*original*/ undefined); } } @@ -41380,7 +42600,7 @@ var ts; TypeScriptSubstitutionFlags[TypeScriptSubstitutionFlags["NonQualifiedEnumMembers"] = 8] = "NonQualifiedEnumMembers"; })(TypeScriptSubstitutionFlags || (TypeScriptSubstitutionFlags = {})); function transformTypeScript(context) { - var getNodeEmitFlags = context.getNodeEmitFlags, setNodeEmitFlags = context.setNodeEmitFlags, setCommentRange = context.setCommentRange, setSourceMapRange = context.setSourceMapRange, startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration; + var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration; var resolver = context.getEmitResolver(); var compilerOptions = context.getCompilerOptions(); var languageVersion = ts.getEmitScriptTarget(compilerOptions); @@ -41391,11 +42611,15 @@ var ts; // Set new transformation hooks. context.onEmitNode = onEmitNode; context.onSubstituteNode = onSubstituteNode; + // Enable substitution for property/element access to emit const enum values. + context.enableSubstitution(172 /* PropertyAccessExpression */); + context.enableSubstitution(173 /* ElementAccessExpression */); // These variables contain state that changes as we descend into the tree. var currentSourceFile; var currentNamespace; var currentNamespaceContainerName; var currentScope; + var currentScopeFirstDeclarationsOfName; var currentSourceFileExternalHelpersModuleName; /** * Keeps track of whether expression substitution has been enabled for specific edge cases. @@ -41424,6 +42648,9 @@ var ts; * @param node A SourceFile node. */ function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } return ts.visitNode(node, visitor, ts.isSourceFile); } /** @@ -41434,10 +42661,14 @@ var ts; function saveStateAndInvoke(node, f) { // Save state var savedCurrentScope = currentScope; + var savedCurrentScopeFirstDeclarationsOfName = currentScopeFirstDeclarationsOfName; // Handle state changes before visiting a node. onBeforeVisitNode(node); var visited = f(node); // Restore state + if (currentScope !== savedCurrentScope) { + currentScopeFirstDeclarationsOfName = savedCurrentScopeFirstDeclarationsOfName; + } currentScope = savedCurrentScope; return visited; } @@ -41702,11 +42933,23 @@ var ts; case 226 /* ModuleBlock */: case 199 /* Block */: currentScope = node; + currentScopeFirstDeclarationsOfName = undefined; + break; + case 221 /* ClassDeclaration */: + case 220 /* FunctionDeclaration */: + if (ts.hasModifier(node, 2 /* Ambient */)) { + break; + } + recordEmittedDeclarationInScope(node); break; } } function visitSourceFile(node) { currentSourceFile = node; + // ensure "use strict" is emitted in all scenarios in alwaysStrict mode + if (compilerOptions.alwaysStrict) { + node = ts.ensureUseStrict(node); + } // If the source file requires any helpers and is an external module, and // the importHelpers compiler option is enabled, emit a synthesized import // statement for the helpers library. @@ -41733,7 +42976,7 @@ var ts; else { node = ts.visitEachChild(node, visitor, context); } - setNodeEmitFlags(node, 1 /* EmitEmitHelpers */ | node.emitFlags); + ts.setEmitFlags(node, 1 /* EmitEmitHelpers */ | ts.getEmitFlags(node)); return node; } /** @@ -41792,7 +43035,7 @@ var ts; // To better align with the old emitter, we should not emit a trailing source map // entry if the class has static properties. if (staticProperties.length > 0) { - setNodeEmitFlags(classDeclaration, 1024 /* NoTrailingSourceMap */ | getNodeEmitFlags(classDeclaration)); + ts.setEmitFlags(classDeclaration, 1024 /* NoTrailingSourceMap */ | ts.getEmitFlags(classDeclaration)); } statements.push(classDeclaration); } @@ -41951,7 +43194,7 @@ var ts; /*type*/ undefined, classExpression) ]), /*location*/ location); - setCommentRange(transformedClassExpression, node); + ts.setCommentRange(transformedClassExpression, node); statements.push(ts.setOriginalNode( /*node*/ transformedClassExpression, /*original*/ node)); @@ -41996,7 +43239,7 @@ var ts; } // To preserve the behavior of the old emitter, we explicitly indent // the body of a class with static initializers. - setNodeEmitFlags(classExpression, 524288 /* Indented */ | getNodeEmitFlags(classExpression)); + ts.setEmitFlags(classExpression, 524288 /* Indented */ | ts.getEmitFlags(classExpression)); expressions.push(ts.startOnNewLine(ts.createAssignment(temp, classExpression))); ts.addRange(expressions, generateInitializedPropertyExpressions(node, staticProperties, temp)); expressions.push(ts.startOnNewLine(temp)); @@ -42151,7 +43394,7 @@ var ts; return index; } var statement = statements[index]; - if (statement.kind === 202 /* ExpressionStatement */ && ts.isSuperCallExpression(statement.expression)) { + if (statement.kind === 202 /* ExpressionStatement */ && ts.isSuperCall(statement.expression)) { result.push(ts.visitNode(statement, visitor, ts.isStatement)); return index + 1; } @@ -42185,9 +43428,9 @@ var ts; ts.Debug.assert(ts.isIdentifier(node.name)); var name = node.name; var propertyName = ts.getMutableClone(name); - setNodeEmitFlags(propertyName, 49152 /* NoComments */ | 1536 /* NoSourceMap */); + ts.setEmitFlags(propertyName, 49152 /* NoComments */ | 1536 /* NoSourceMap */); var localName = ts.getMutableClone(name); - setNodeEmitFlags(localName, 49152 /* NoComments */); + ts.setEmitFlags(localName, 49152 /* NoComments */); return ts.startOnNewLine(ts.createStatement(ts.createAssignment(ts.createPropertyAccess(ts.createThis(), propertyName, /*location*/ node.name), localName), /*location*/ ts.moveRangePos(node, -1))); @@ -42239,8 +43482,8 @@ var ts; for (var _i = 0, properties_7 = properties; _i < properties_7.length; _i++) { var property = properties_7[_i]; var statement = ts.createStatement(transformInitializedProperty(node, property, receiver)); - setSourceMapRange(statement, ts.moveRangePastModifiers(property)); - setCommentRange(statement, property); + ts.setSourceMapRange(statement, ts.moveRangePastModifiers(property)); + ts.setCommentRange(statement, property); statements.push(statement); } } @@ -42257,8 +43500,8 @@ var ts; var property = properties_8[_i]; var expression = transformInitializedProperty(node, property, receiver); expression.startsOnNewLine = true; - setSourceMapRange(expression, ts.moveRangePastModifiers(property)); - setCommentRange(expression, property); + ts.setSourceMapRange(expression, ts.moveRangePastModifiers(property)); + ts.setCommentRange(expression, property); expressions.push(expression); } return expressions; @@ -42524,7 +43767,7 @@ var ts; : ts.createNull() : undefined; var helper = ts.createDecorateHelper(currentSourceFileExternalHelpersModuleName, decoratorExpressions, prefix, memberName, descriptor, ts.moveRangePastDecorators(member)); - setNodeEmitFlags(helper, 49152 /* NoComments */); + ts.setEmitFlags(helper, 49152 /* NoComments */); return helper; } /** @@ -42562,12 +43805,12 @@ var ts; if (decoratedClassAlias) { var expression = ts.createAssignment(decoratedClassAlias, ts.createDecorateHelper(currentSourceFileExternalHelpersModuleName, decoratorExpressions, getDeclarationName(node))); var result = ts.createAssignment(getDeclarationName(node), expression, ts.moveRangePastDecorators(node)); - setNodeEmitFlags(result, 49152 /* NoComments */); + ts.setEmitFlags(result, 49152 /* NoComments */); return result; } else { var result = ts.createAssignment(getDeclarationName(node), ts.createDecorateHelper(currentSourceFileExternalHelpersModuleName, decoratorExpressions, getDeclarationName(node)), ts.moveRangePastDecorators(node)); - setNodeEmitFlags(result, 49152 /* NoComments */); + ts.setEmitFlags(result, 49152 /* NoComments */); return result; } } @@ -42593,7 +43836,7 @@ var ts; var decorator = decorators_1[_i]; var helper = ts.createParamHelper(currentSourceFileExternalHelpersModuleName, transformDecorator(decorator), parameterOffset, /*location*/ decorator.expression); - setNodeEmitFlags(helper, 49152 /* NoComments */); + ts.setEmitFlags(helper, 49152 /* NoComments */); expressions.push(helper); } } @@ -42824,10 +44067,38 @@ var ts; : ts.createIdentifier("Symbol"); case 155 /* TypeReference */: return serializeTypeReferenceNode(node); + case 163 /* IntersectionType */: + case 162 /* UnionType */: + { + var unionOrIntersection = node; + var serializedUnion = void 0; + for (var _i = 0, _a = unionOrIntersection.types; _i < _a.length; _i++) { + var typeNode = _a[_i]; + var serializedIndividual = serializeTypeNode(typeNode); + // Non identifier + if (serializedIndividual.kind !== 69 /* Identifier */) { + serializedUnion = undefined; + break; + } + // One of the individual is global object, return immediately + if (serializedIndividual.text === "Object") { + return serializedIndividual; + } + // Different types + if (serializedUnion && serializedUnion.text !== serializedIndividual.text) { + serializedUnion = undefined; + break; + } + serializedUnion = serializedIndividual; + } + // If we were able to find common type + if (serializedUnion) { + return serializedUnion; + } + } + // Fallthrough case 158 /* TypeQuery */: case 159 /* TypeLiteral */: - case 162 /* UnionType */: - case 163 /* IntersectionType */: case 117 /* AnyKeyword */: case 165 /* ThisType */: break; @@ -43027,8 +44298,8 @@ var ts; /*location*/ node); // While we emit the source map for the node after skipping decorators and modifiers, // we need to emit the comments for the original range. - setCommentRange(method, node); - setSourceMapRange(method, ts.moveRangePastDecorators(node)); + ts.setCommentRange(method, node); + ts.setSourceMapRange(method, ts.moveRangePastDecorators(node)); ts.setOriginalNode(method, node); return method; } @@ -43060,8 +44331,8 @@ var ts; /*location*/ node); // While we emit the source map for the node after skipping decorators and modifiers, // we need to emit the comments for the original range. - setCommentRange(accessor, node); - setSourceMapRange(accessor, ts.moveRangePastDecorators(node)); + ts.setCommentRange(accessor, node); + ts.setSourceMapRange(accessor, ts.moveRangePastDecorators(node)); ts.setOriginalNode(accessor, node); return accessor; } @@ -43083,8 +44354,8 @@ var ts; /*location*/ node); // While we emit the source map for the node after skipping decorators and modifiers, // we need to emit the comments for the original range. - setCommentRange(accessor, node); - setSourceMapRange(accessor, ts.moveRangePastDecorators(node)); + ts.setCommentRange(accessor, node); + ts.setSourceMapRange(accessor, ts.moveRangePastDecorators(node)); ts.setOriginalNode(accessor, node); return accessor; } @@ -43220,11 +44491,11 @@ var ts; if (languageVersion >= 2 /* ES6 */) { if (resolver.getNodeCheckFlags(node) & 4096 /* AsyncMethodWithSuperBinding */) { enableSubstitutionForAsyncMethodsWithSuper(); - setNodeEmitFlags(block, 8 /* EmitAdvancedSuperHelper */); + ts.setEmitFlags(block, 8 /* EmitAdvancedSuperHelper */); } else if (resolver.getNodeCheckFlags(node) & 2048 /* AsyncMethodWithSuper */) { enableSubstitutionForAsyncMethodsWithSuper(); - setNodeEmitFlags(block, 4 /* EmitSuperHelper */); + ts.setEmitFlags(block, 4 /* EmitSuperHelper */); } } return block; @@ -43244,7 +44515,7 @@ var ts; * @param node The parameter declaration node. */ function visitParameter(node) { - if (node.name && ts.isIdentifier(node.name) && node.name.originalKeywordKind === 97 /* ThisKeyword */) { + if (ts.parameterIsThisKeyword(node)) { return undefined; } var parameter = ts.createParameterDeclaration( @@ -43256,9 +44527,9 @@ var ts; // While we emit the source map for the node after skipping decorators and modifiers, // we need to emit the comments for the original range. ts.setOriginalNode(parameter, node); - setCommentRange(parameter, node); - setSourceMapRange(parameter, ts.moveRangePastModifiers(node)); - setNodeEmitFlags(parameter.name, 1024 /* NoTrailingSourceMap */); + ts.setCommentRange(parameter, node); + ts.setSourceMapRange(parameter, ts.moveRangePastModifiers(node)); + ts.setEmitFlags(parameter.name, 1024 /* NoTrailingSourceMap */); return parameter; } /** @@ -43351,8 +44622,9 @@ var ts; || compilerOptions.isolatedModules; } function shouldEmitVarForEnumDeclaration(node) { - return !ts.hasModifier(node, 1 /* Export */) - || (isES6ExportedDeclaration(node) && ts.isFirstDeclarationOfKind(node, node.kind)); + return isFirstEmittedDeclarationInScope(node) + && (!ts.hasModifier(node, 1 /* Export */) + || isES6ExportedDeclaration(node)); } /* * Adds a trailing VariableStatement for an enum or module declaration. @@ -43361,7 +44633,7 @@ var ts; var statement = ts.createVariableStatement( /*modifiers*/ undefined, [ts.createVariableDeclaration(getDeclarationName(node), /*type*/ undefined, getExportName(node))]); - setSourceMapRange(statement, node); + ts.setSourceMapRange(statement, node); statements.push(statement); } /** @@ -43382,6 +44654,7 @@ var ts; // If needed, we should emit a variable declaration for the enum. If we emit // a leading variable declaration, we should not emit leading comments for the // enum body. + recordEmittedDeclarationInScope(node); if (shouldEmitVarForEnumDeclaration(node)) { addVarForEnumOrModuleDeclaration(statements, node); // We should still emit the comments if we are emitting a system module. @@ -43407,7 +44680,7 @@ var ts; /*typeArguments*/ undefined, [ts.createLogicalOr(exportName, ts.createAssignment(exportName, ts.createObjectLiteral()))]), /*location*/ node); ts.setOriginalNode(enumStatement, node); - setNodeEmitFlags(enumStatement, emitFlags); + ts.setEmitFlags(enumStatement, emitFlags); statements.push(enumStatement); if (isNamespaceExport(node)) { addVarForEnumExportedFromNamespace(statements, node); @@ -43473,18 +44746,43 @@ var ts; function shouldEmitModuleDeclaration(node) { return ts.isInstantiatedModule(node, compilerOptions.preserveConstEnums || compilerOptions.isolatedModules); } - function isModuleMergedWithES6Class(node) { - return languageVersion === 2 /* ES6 */ - && ts.isMergedWithClass(node); - } function isES6ExportedDeclaration(node) { return isExternalModuleExport(node) && moduleKind === ts.ModuleKind.ES6; } + /** + * Records that a declaration was emitted in the current scope, if it was the first + * declaration for the provided symbol. + * + * NOTE: if there is ever a transformation above this one, we may not be able to rely + * on symbol names. + */ + function recordEmittedDeclarationInScope(node) { + var name = node.symbol && node.symbol.name; + if (name) { + if (!currentScopeFirstDeclarationsOfName) { + currentScopeFirstDeclarationsOfName = ts.createMap(); + } + if (!(name in currentScopeFirstDeclarationsOfName)) { + currentScopeFirstDeclarationsOfName[name] = node; + } + } + } + /** + * Determines whether a declaration is the first declaration with the same name emitted + * in the current scope. + */ + function isFirstEmittedDeclarationInScope(node) { + if (currentScopeFirstDeclarationsOfName) { + var name_28 = node.symbol && node.symbol.name; + if (name_28) { + return currentScopeFirstDeclarationsOfName[name_28] === node; + } + } + return false; + } function shouldEmitVarForModuleDeclaration(node) { - return !isModuleMergedWithES6Class(node) - && (!isES6ExportedDeclaration(node) - || ts.isFirstDeclarationOfKind(node, node.kind)); + return isFirstEmittedDeclarationInScope(node); } /** * Adds a leading VariableStatement for a enum or module declaration. @@ -43499,10 +44797,10 @@ var ts; ts.setOriginalNode(statement, /*original*/ node); // Adjust the source map emit to match the old emitter. if (node.kind === 224 /* EnumDeclaration */) { - setSourceMapRange(statement.declarationList, node); + ts.setSourceMapRange(statement.declarationList, node); } else { - setSourceMapRange(statement, node); + ts.setSourceMapRange(statement, node); } // Trailing comments for module declaration should be emitted after the function closure // instead of the variable statement: @@ -43522,8 +44820,8 @@ var ts; // } // })(m1 || (m1 = {})); // trailing comment module // - setCommentRange(statement, node); - setNodeEmitFlags(statement, 32768 /* NoTrailingComments */); + ts.setCommentRange(statement, node); + ts.setEmitFlags(statement, 32768 /* NoTrailingComments */); statements.push(statement); } /** @@ -43546,6 +44844,7 @@ var ts; // If needed, we should emit a variable declaration for the module. If we emit // a leading variable declaration, we should not emit leading comments for the // module body. + recordEmittedDeclarationInScope(node); if (shouldEmitVarForModuleDeclaration(node)) { addVarForEnumOrModuleDeclaration(statements, node); // We should still emit the comments if we are emitting a system module. @@ -43579,7 +44878,7 @@ var ts; /*typeArguments*/ undefined, [moduleArg]), /*location*/ node); ts.setOriginalNode(moduleStatement, node); - setNodeEmitFlags(moduleStatement, emitFlags); + ts.setEmitFlags(moduleStatement, emitFlags); statements.push(moduleStatement); return statements; } @@ -43591,8 +44890,10 @@ var ts; function transformModuleBody(node, namespaceLocalName) { var savedCurrentNamespaceContainerName = currentNamespaceContainerName; var savedCurrentNamespace = currentNamespace; + var savedCurrentScopeFirstDeclarationsOfName = currentScopeFirstDeclarationsOfName; currentNamespaceContainerName = namespaceLocalName; currentNamespace = node; + currentScopeFirstDeclarationsOfName = undefined; var statements = []; startLexicalEnvironment(); var statementsLocation; @@ -43619,6 +44920,7 @@ var ts; ts.addRange(statements, endLexicalEnvironment()); currentNamespaceContainerName = savedCurrentNamespaceContainerName; currentNamespace = savedCurrentNamespace; + currentScopeFirstDeclarationsOfName = savedCurrentScopeFirstDeclarationsOfName; var block = ts.createBlock(ts.createNodeArray(statements, /*location*/ statementsLocation), /*location*/ blockLocation, @@ -43644,7 +44946,7 @@ var ts; // })(hello || (hello = {})); // We only want to emit comment on the namespace which contains block body itself, not the containing namespaces. if (body.kind !== 226 /* ModuleBlock */) { - setNodeEmitFlags(block, block.emitFlags | 49152 /* NoComments */); + ts.setEmitFlags(block, ts.getEmitFlags(block) | 49152 /* NoComments */); } return block; } @@ -43680,7 +44982,7 @@ var ts; return undefined; } var moduleReference = ts.createExpressionFromEntityName(node.moduleReference); - setNodeEmitFlags(moduleReference, 49152 /* NoComments */ | 65536 /* NoNestedComments */); + ts.setEmitFlags(moduleReference, 49152 /* NoComments */ | 65536 /* NoNestedComments */); if (isNamedExternalModuleExport(node) || !isNamespaceExport(node)) { // export var ${name} = ${moduleReference}; // var ${name} = ${moduleReference}; @@ -43736,9 +45038,9 @@ var ts; } function addExportMemberAssignment(statements, node) { var expression = ts.createAssignment(getExportName(node), getLocalName(node, /*noSourceMaps*/ true)); - setSourceMapRange(expression, ts.createRange(node.name.pos, node.end)); + ts.setSourceMapRange(expression, ts.createRange(node.name.pos, node.end)); var statement = ts.createStatement(expression); - setSourceMapRange(statement, ts.createRange(-1, node.end)); + ts.setSourceMapRange(statement, ts.createRange(-1, node.end)); statements.push(statement); } function createNamespaceExport(exportName, exportValue, location) { @@ -43761,7 +45063,7 @@ var ts; emitFlags |= 1536 /* NoSourceMap */; } if (emitFlags) { - setNodeEmitFlags(qualifiedName, emitFlags); + ts.setEmitFlags(qualifiedName, emitFlags); } return qualifiedName; } @@ -43773,7 +45075,7 @@ var ts; */ function getNamespaceParameterName(node) { var name = ts.getGeneratedNameForNode(node); - setSourceMapRange(name, node.name); + ts.setSourceMapRange(name, node.name); return name; } /** @@ -43822,8 +45124,8 @@ var ts; */ function getDeclarationName(node, allowComments, allowSourceMaps, emitFlags) { if (node.name) { - var name_28 = ts.getMutableClone(node.name); - emitFlags |= getNodeEmitFlags(node.name); + var name_29 = ts.getMutableClone(node.name); + emitFlags |= ts.getEmitFlags(node.name); if (!allowSourceMaps) { emitFlags |= 1536 /* NoSourceMap */; } @@ -43831,9 +45133,9 @@ var ts; emitFlags |= 49152 /* NoComments */; } if (emitFlags) { - setNodeEmitFlags(name_28, emitFlags); + ts.setEmitFlags(name_29, emitFlags); } - return name_28; + return name_29; } else { return ts.getGeneratedNameForNode(node); @@ -43910,7 +45212,7 @@ var ts; * @param node The node to emit. * @param emit A callback used to emit the node in the printer. */ - function onEmitNode(node, emit) { + function onEmitNode(emitContext, node, emitCallback) { var savedApplicableSubstitutions = applicableSubstitutions; var savedCurrentSuperContainer = currentSuperContainer; // If we need to support substitutions for `super` in an async method, @@ -43924,7 +45226,7 @@ var ts; if (enabledSubstitutions & 8 /* NonQualifiedEnumMembers */ && isTransformedEnumDeclaration(node)) { applicableSubstitutions |= 8 /* NonQualifiedEnumMembers */; } - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); applicableSubstitutions = savedApplicableSubstitutions; currentSuperContainer = savedCurrentSuperContainer; } @@ -43935,9 +45237,9 @@ var ts; * @param isExpression A value indicating whether the node is to be used in an expression * position. */ - function onSubstituteNode(node, isExpression) { - node = previousOnSubstituteNode(node, isExpression); - if (isExpression) { + function onSubstituteNode(emitContext, node) { + node = previousOnSubstituteNode(emitContext, node); + if (emitContext === 1 /* Expression */) { return substituteExpression(node); } else if (ts.isShorthandPropertyAssignment(node)) { @@ -43947,16 +45249,16 @@ var ts; } function substituteShorthandPropertyAssignment(node) { if (enabledSubstitutions & 2 /* NamespaceExports */) { - var name_29 = node.name; - var exportedName = trySubstituteNamespaceExportedName(name_29); + var name_30 = node.name; + var exportedName = trySubstituteNamespaceExportedName(name_30); if (exportedName) { // A shorthand property with an assignment initializer is probably part of a // destructuring assignment if (node.objectAssignmentInitializer) { var initializer = ts.createAssignment(exportedName, node.objectAssignmentInitializer); - return ts.createPropertyAssignment(name_29, initializer, /*location*/ node); + return ts.createPropertyAssignment(name_30, initializer, /*location*/ node); } - return ts.createPropertyAssignment(name_29, exportedName, /*location*/ node); + return ts.createPropertyAssignment(name_30, exportedName, /*location*/ node); } } return node; @@ -43965,16 +45267,15 @@ var ts; switch (node.kind) { case 69 /* Identifier */: return substituteExpressionIdentifier(node); - } - if (enabledSubstitutions & 4 /* AsyncMethodsWithSuper */) { - switch (node.kind) { - case 174 /* CallExpression */: + case 172 /* PropertyAccessExpression */: + return substitutePropertyAccessExpression(node); + case 173 /* ElementAccessExpression */: + return substituteElementAccessExpression(node); + case 174 /* CallExpression */: + if (enabledSubstitutions & 4 /* AsyncMethodsWithSuper */) { return substituteCallExpression(node); - case 172 /* PropertyAccessExpression */: - return substitutePropertyAccessExpression(node); - case 173 /* ElementAccessExpression */: - return substituteElementAccessExpression(node); - } + } + break; } return node; } @@ -43996,8 +45297,8 @@ var ts; var classAlias = classAliases[declaration.id]; if (classAlias) { var clone_4 = ts.getSynthesizedClone(classAlias); - setSourceMapRange(clone_4, node); - setCommentRange(clone_4, node); + ts.setSourceMapRange(clone_4, node); + ts.setCommentRange(clone_4, node); return clone_4; } } @@ -44007,7 +45308,7 @@ var ts; } function trySubstituteNamespaceExportedName(node) { // If this is explicitly a local name, do not substitute. - if (enabledSubstitutions & applicableSubstitutions && (getNodeEmitFlags(node) & 262144 /* LocalName */) === 0) { + if (enabledSubstitutions & applicableSubstitutions && (ts.getEmitFlags(node) & 262144 /* LocalName */) === 0) { // If we are nested within a namespace declaration, we may need to qualifiy // an identifier that is exported from a merged namespace. var container = resolver.getReferencedExportContainer(node, /*prefixLocals*/ false); @@ -44038,23 +45339,48 @@ var ts; return node; } function substitutePropertyAccessExpression(node) { - if (node.expression.kind === 95 /* SuperKeyword */) { + if (enabledSubstitutions & 4 /* AsyncMethodsWithSuper */ && node.expression.kind === 95 /* SuperKeyword */) { var flags = getSuperContainerAsyncMethodFlags(); if (flags) { return createSuperAccessInAsyncMethod(ts.createLiteral(node.name.text), flags, node); } } - return node; + return substituteConstantValue(node); } function substituteElementAccessExpression(node) { - if (node.expression.kind === 95 /* SuperKeyword */) { + if (enabledSubstitutions & 4 /* AsyncMethodsWithSuper */ && node.expression.kind === 95 /* SuperKeyword */) { var flags = getSuperContainerAsyncMethodFlags(); if (flags) { return createSuperAccessInAsyncMethod(node.argumentExpression, flags, node); } } + return substituteConstantValue(node); + } + function substituteConstantValue(node) { + var constantValue = tryGetConstEnumValue(node); + if (constantValue !== undefined) { + var substitute = ts.createLiteral(constantValue); + ts.setSourceMapRange(substitute, node); + ts.setCommentRange(substitute, node); + if (!compilerOptions.removeComments) { + var propertyName = ts.isPropertyAccessExpression(node) + ? ts.declarationNameToString(node.name) + : ts.getTextOfNode(node.argumentExpression); + substitute.trailingComment = " " + propertyName + " "; + } + ts.setConstantValue(node, constantValue); + return substitute; + } return node; } + function tryGetConstEnumValue(node) { + if (compilerOptions.isolatedModules) { + return undefined; + } + return ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node) + ? resolver.getConstantValue(node) + : undefined; + } function createSuperAccessInAsyncMethod(argumentExpression, flags, location) { if (flags & 4096 /* AsyncMethodWithSuperBinding */) { return ts.createPropertyAccess(ts.createCall(ts.createIdentifier("_super"), @@ -44083,6 +45409,9 @@ var ts; var currentSourceFile; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } if (ts.isExternalModule(node) || compilerOptions.isolatedModules) { currentSourceFile = node; return ts.visitEachChild(node, visitor, context); @@ -44201,7 +45530,7 @@ var ts; var ts; (function (ts) { function transformSystemModule(context) { - var getNodeEmitFlags = context.getNodeEmitFlags, setNodeEmitFlags = context.setNodeEmitFlags, startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration, hoistFunctionDeclaration = context.hoistFunctionDeclaration; + var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration, hoistFunctionDeclaration = context.hoistFunctionDeclaration; var compilerOptions = context.getCompilerOptions(); var resolver = context.getEmitResolver(); var host = context.getEmitHost(); @@ -44230,6 +45559,9 @@ var ts; var currentNode; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } if (ts.isExternalModule(node) || compilerOptions.isolatedModules) { currentSourceFile = node; currentNode = node; @@ -44283,7 +45615,7 @@ var ts; ts.createParameter(exportFunctionForFile), ts.createParameter(contextObjectForFile) ], - /*type*/ undefined, setNodeEmitFlags(ts.createBlock(statements, /*location*/ undefined, /*multiLine*/ true), 1 /* EmitEmitHelpers */)); + /*type*/ undefined, ts.setEmitFlags(ts.createBlock(statements, /*location*/ undefined, /*multiLine*/ true), 1 /* EmitEmitHelpers */)); // Write the call to `System.register` // Clear the emit-helpers flag for later passes since we'll have already used it in the module body // So the helper will be emit at the correct position instead of at the top of the source-file @@ -44292,7 +45624,7 @@ var ts; /*typeArguments*/ undefined, moduleName ? [moduleName, dependencies, body] : [dependencies, body])) - ], /*nodeEmitFlags*/ ~1 /* EmitEmitHelpers */ & getNodeEmitFlags(node)); + ], /*nodeEmitFlags*/ ~1 /* EmitEmitHelpers */ & ts.getEmitFlags(node)); var _a; } /** @@ -44676,17 +46008,17 @@ var ts; function visitFunctionDeclaration(node) { if (ts.hasModifier(node, 1 /* Export */)) { // If the function is exported, ensure it has a name and rewrite the function without any export flags. - var name_30 = node.name || ts.getGeneratedNameForNode(node); + var name_31 = node.name || ts.getGeneratedNameForNode(node); var newNode = ts.createFunctionDeclaration( /*decorators*/ undefined, - /*modifiers*/ undefined, node.asteriskToken, name_30, + /*modifiers*/ undefined, node.asteriskToken, name_31, /*typeParameters*/ undefined, node.parameters, /*type*/ undefined, node.body, /*location*/ node); // Record a declaration export in the outer module body function. recordExportedFunctionDeclaration(node); if (!ts.hasModifier(node, 512 /* Default */)) { - recordExportName(name_30); + recordExportName(name_31); } ts.setOriginalNode(newNode, node); node = newNode; @@ -44698,16 +46030,16 @@ var ts; function visitExpressionStatement(node) { var originalNode = ts.getOriginalNode(node); if ((originalNode.kind === 225 /* ModuleDeclaration */ || originalNode.kind === 224 /* EnumDeclaration */) && ts.hasModifier(originalNode, 1 /* Export */)) { - var name_31 = getDeclarationName(originalNode); + var name_32 = getDeclarationName(originalNode); // We only need to hoistVariableDeclaration for EnumDeclaration // as ModuleDeclaration is already hoisted when the transformer call visitVariableStatement // which then call transformsVariable for each declaration in declarationList if (originalNode.kind === 224 /* EnumDeclaration */) { - hoistVariableDeclaration(name_31); + hoistVariableDeclaration(name_32); } return [ node, - createExportStatement(name_31, name_31) + createExportStatement(name_32, name_32) ]; } return node; @@ -44952,14 +46284,14 @@ var ts; // // Substitutions // - function onEmitNode(node, emit) { + function onEmitNode(emitContext, node, emitCallback) { if (node.kind === 256 /* SourceFile */) { exportFunctionForFile = exportFunctionForFileMap[ts.getOriginalNodeId(node)]; - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); exportFunctionForFile = undefined; } else { - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); } } /** @@ -44969,9 +46301,9 @@ var ts; * @param isExpression A value indicating whether the node is to be used in an expression * position. */ - function onSubstituteNode(node, isExpression) { - node = previousOnSubstituteNode(node, isExpression); - if (isExpression) { + function onSubstituteNode(emitContext, node) { + node = previousOnSubstituteNode(emitContext, node); + if (emitContext === 1 /* Expression */) { return substituteExpression(node); } return node; @@ -45013,7 +46345,7 @@ var ts; return node; } function substituteAssignmentExpression(node) { - setNodeEmitFlags(node, 128 /* NoSubstitution */); + ts.setEmitFlags(node, 128 /* NoSubstitution */); var left = node.left; switch (left.kind) { case 69 /* Identifier */: @@ -45118,7 +46450,7 @@ var ts; var exportDeclaration = resolver.getReferencedExportContainer(operand); if (exportDeclaration) { var expr = ts.createPrefix(node.operator, operand, node); - setNodeEmitFlags(expr, 128 /* NoSubstitution */); + ts.setEmitFlags(expr, 128 /* NoSubstitution */); var call = createExportExpression(operand, expr); if (node.kind === 185 /* PrefixUnaryExpression */) { return call; @@ -45165,7 +46497,7 @@ var ts; ts.createForIn(ts.createVariableDeclarationList([ ts.createVariableDeclaration(n, /*type*/ undefined) ]), m, ts.createBlock([ - setNodeEmitFlags(ts.createIf(condition, ts.createStatement(ts.createAssignment(ts.createElementAccess(exports, n), ts.createElementAccess(m, n)))), 32 /* SingleLine */) + ts.setEmitFlags(ts.createIf(condition, ts.createStatement(ts.createAssignment(ts.createElementAccess(exports, n), ts.createElementAccess(m, n)))), 32 /* SingleLine */) ])), ts.createStatement(ts.createCall(exportFunctionForFile, /*typeArguments*/ undefined, [exports])) @@ -45283,7 +46615,7 @@ var ts; function updateSourceFile(node, statements, nodeEmitFlags) { var updated = ts.getMutableClone(node); updated.statements = ts.createNodeArray(statements, node.statements); - setNodeEmitFlags(updated, nodeEmitFlags); + ts.setEmitFlags(updated, nodeEmitFlags); return updated; } } @@ -45301,7 +46633,7 @@ var ts; _a[ts.ModuleKind.AMD] = transformAMDModule, _a[ts.ModuleKind.UMD] = transformUMDModule, _a)); - var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration, setNodeEmitFlags = context.setNodeEmitFlags, getNodeEmitFlags = context.getNodeEmitFlags, setSourceMapRange = context.setSourceMapRange; + var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration; var compilerOptions = context.getCompilerOptions(); var resolver = context.getEmitResolver(); var host = context.getEmitHost(); @@ -45333,6 +46665,9 @@ var ts; * @param node The SourceFile node. */ function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } if (ts.isExternalModule(node) || compilerOptions.isolatedModules) { currentSourceFile = node; // Collect information about the external module. @@ -45365,7 +46700,7 @@ var ts; addExportEqualsIfNeeded(statements, /*emitAsReturn*/ false); var updated = updateSourceFile(node, statements); if (hasExportStarsToExportValues) { - setNodeEmitFlags(updated, 2 /* EmitExportStar */ | getNodeEmitFlags(node)); + ts.setEmitFlags(updated, 2 /* EmitExportStar */ | ts.getEmitFlags(node)); } return updated; } @@ -45386,7 +46721,7 @@ var ts; */ function transformUMDModule(node) { var define = ts.createIdentifier("define"); - setNodeEmitFlags(define, 16 /* UMDDefine */); + ts.setEmitFlags(define, 16 /* UMDDefine */); return transformAsynchronousModule(node, define, /*moduleName*/ undefined, /*includeNonAmdDependencies*/ false); } /** @@ -45466,7 +46801,7 @@ var ts; if (hasExportStarsToExportValues) { // If we have any `export * from ...` declarations // we need to inform the emitter to add the __export helper. - setNodeEmitFlags(body, 2 /* EmitExportStar */); + ts.setEmitFlags(body, 2 /* EmitExportStar */); } return body; } @@ -45475,13 +46810,13 @@ var ts; if (emitAsReturn) { var statement = ts.createReturn(exportEquals.expression, /*location*/ exportEquals); - setNodeEmitFlags(statement, 12288 /* NoTokenSourceMaps */ | 49152 /* NoComments */); + ts.setEmitFlags(statement, 12288 /* NoTokenSourceMaps */ | 49152 /* NoComments */); statements.push(statement); } else { var statement = ts.createStatement(ts.createAssignment(ts.createPropertyAccess(ts.createIdentifier("module"), "exports"), exportEquals.expression), /*location*/ exportEquals); - setNodeEmitFlags(statement, 49152 /* NoComments */); + ts.setEmitFlags(statement, 49152 /* NoComments */); statements.push(statement); } } @@ -45574,7 +46909,7 @@ var ts; } // Set emitFlags on the name of the importEqualsDeclaration // This is so the printer will not substitute the identifier - setNodeEmitFlags(node.name, 128 /* NoSubstitution */); + ts.setEmitFlags(node.name, 128 /* NoSubstitution */); var statements = []; if (moduleKind !== ts.ModuleKind.AMD) { if (ts.hasModifier(node, 1 /* Export */)) { @@ -45678,16 +47013,16 @@ var ts; else { var names = ts.reduceEachChild(node, collectExportMembers, []); for (var _i = 0, names_1 = names; _i < names_1.length; _i++) { - var name_32 = names_1[_i]; - addExportMemberAssignments(statements, name_32); + var name_33 = names_1[_i]; + addExportMemberAssignments(statements, name_33); } } } function collectExportMembers(names, node) { if (ts.isAliasSymbolDeclaration(node) && resolver.isValueAliasDeclaration(node) && ts.isDeclaration(node)) { - var name_33 = node.name; - if (ts.isIdentifier(name_33)) { - names.push(name_33); + var name_34 = node.name; + if (ts.isIdentifier(name_34)) { + names.push(name_34); } } return ts.reduceEachChild(node, collectExportMembers, names); @@ -45706,7 +47041,7 @@ var ts; addExportDefault(statements, getDeclarationName(node), /*location*/ node); } else { - statements.push(createExportStatement(node.name, setNodeEmitFlags(ts.getSynthesizedClone(node.name), 262144 /* LocalName */), /*location*/ node)); + statements.push(createExportStatement(node.name, ts.setEmitFlags(ts.getSynthesizedClone(node.name), 262144 /* LocalName */), /*location*/ node)); } } function visitVariableStatement(node) { @@ -45856,20 +47191,20 @@ var ts; /*modifiers*/ undefined, [ts.createVariableDeclaration(getDeclarationName(node), /*type*/ undefined, ts.createPropertyAccess(ts.createIdentifier("exports"), getDeclarationName(node)))], /*location*/ node); - setNodeEmitFlags(transformedStatement, 49152 /* NoComments */); + ts.setEmitFlags(transformedStatement, 49152 /* NoComments */); statements.push(transformedStatement); } function getDeclarationName(node) { return node.name ? ts.getSynthesizedClone(node.name) : ts.getGeneratedNameForNode(node); } - function onEmitNode(node, emit) { + function onEmitNode(emitContext, node, emitCallback) { if (node.kind === 256 /* SourceFile */) { bindingNameExportSpecifiersMap = bindingNameExportSpecifiersForFileMap[ts.getOriginalNodeId(node)]; - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); bindingNameExportSpecifiersMap = undefined; } else { - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); } } /** @@ -45879,9 +47214,9 @@ var ts; * @param isExpression A value indicating whether the node is to be used in an expression * position. */ - function onSubstituteNode(node, isExpression) { - node = previousOnSubstituteNode(node, isExpression); - if (isExpression) { + function onSubstituteNode(emitContext, node) { + node = previousOnSubstituteNode(emitContext, node); + if (emitContext === 1 /* Expression */) { return substituteExpression(node); } else if (ts.isShorthandPropertyAssignment(node)) { @@ -45925,7 +47260,7 @@ var ts; // If the left-hand-side of the binaryExpression is an identifier and its is export through export Specifier if (ts.isIdentifier(left) && ts.isAssignmentOperator(node.operatorToken.kind)) { if (bindingNameExportSpecifiersMap && ts.hasProperty(bindingNameExportSpecifiersMap, left.text)) { - setNodeEmitFlags(node, 128 /* NoSubstitution */); + ts.setEmitFlags(node, 128 /* NoSubstitution */); var nestedExportAssignment = void 0; for (var _i = 0, _a = bindingNameExportSpecifiersMap[left.text]; _i < _a.length; _i++) { var specifier = _a[_i]; @@ -45945,13 +47280,13 @@ var ts; var operand = node.operand; if (ts.isIdentifier(operand) && bindingNameExportSpecifiersForFileMap) { if (bindingNameExportSpecifiersMap && ts.hasProperty(bindingNameExportSpecifiersMap, operand.text)) { - setNodeEmitFlags(node, 128 /* NoSubstitution */); + ts.setEmitFlags(node, 128 /* NoSubstitution */); var transformedUnaryExpression = void 0; if (node.kind === 186 /* PostfixUnaryExpression */) { - transformedUnaryExpression = ts.createBinary(operand, ts.createNode(operator === 41 /* PlusPlusToken */ ? 57 /* PlusEqualsToken */ : 58 /* MinusEqualsToken */), ts.createLiteral(1), + transformedUnaryExpression = ts.createBinary(operand, ts.createToken(operator === 41 /* PlusPlusToken */ ? 57 /* PlusEqualsToken */ : 58 /* MinusEqualsToken */), ts.createLiteral(1), /*location*/ node); // We have to set no substitution flag here to prevent visit the binary expression and substitute it again as we will preform all necessary substitution in here - setNodeEmitFlags(transformedUnaryExpression, 128 /* NoSubstitution */); + ts.setEmitFlags(transformedUnaryExpression, 128 /* NoSubstitution */); } var nestedExportAssignment = void 0; for (var _i = 0, _a = bindingNameExportSpecifiersMap[operand.text]; _i < _a.length; _i++) { @@ -45966,7 +47301,7 @@ var ts; return node; } function trySubstituteExportedName(node) { - var emitFlags = getNodeEmitFlags(node); + var emitFlags = ts.getEmitFlags(node); if ((emitFlags & 262144 /* LocalName */) === 0) { var container = resolver.getReferencedExportContainer(node, (emitFlags & 131072 /* ExportName */) !== 0); if (container) { @@ -45979,7 +47314,7 @@ var ts; return undefined; } function trySubstituteImportedName(node) { - if ((getNodeEmitFlags(node) & 262144 /* LocalName */) === 0) { + if ((ts.getEmitFlags(node) & 262144 /* LocalName */) === 0) { var declaration = resolver.getReferencedImportDeclaration(node); if (declaration) { if (ts.isImportClause(declaration)) { @@ -45994,14 +47329,14 @@ var ts; } } else if (ts.isImportSpecifier(declaration)) { - var name_34 = declaration.propertyName || declaration.name; - if (name_34.originalKeywordKind === 77 /* DefaultKeyword */ && languageVersion <= 0 /* ES3 */) { + var name_35 = declaration.propertyName || declaration.name; + if (name_35.originalKeywordKind === 77 /* DefaultKeyword */ && languageVersion <= 0 /* ES3 */) { // TODO: ES3 transform to handle x.default -> x["default"] - return ts.createElementAccess(ts.getGeneratedNameForNode(declaration.parent.parent.parent), ts.createLiteral(name_34.text), + return ts.createElementAccess(ts.getGeneratedNameForNode(declaration.parent.parent.parent), ts.createLiteral(name_35.text), /*location*/ node); } else { - return ts.createPropertyAccess(ts.getGeneratedNameForNode(declaration.parent.parent.parent), ts.getSynthesizedClone(name_34), + return ts.createPropertyAccess(ts.getGeneratedNameForNode(declaration.parent.parent.parent), ts.getSynthesizedClone(name_35), /*location*/ node); } } @@ -46025,7 +47360,7 @@ var ts; var statement = ts.createStatement(createExportAssignment(name, value)); statement.startsOnNewLine = true; if (location) { - setSourceMapRange(statement, location); + ts.setSourceMapRange(statement, location); } return statement; } @@ -46063,7 +47398,7 @@ var ts; if (includeNonAmdDependencies && importAliasName) { // Set emitFlags on the name of the classDeclaration // This is so that when printer will not substitute the identifier - setNodeEmitFlags(importAliasName, 128 /* NoSubstitution */); + ts.setEmitFlags(importAliasName, 128 /* NoSubstitution */); aliasedModuleNames.push(externalModuleName); importAliasNames.push(ts.createParameter(importAliasName)); } @@ -46098,6 +47433,9 @@ var ts; * @param node A SourceFile node. */ function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } currentSourceFile = node; node = ts.visitEachChild(node, visitor, context); currentSourceFile = undefined; @@ -46280,12 +47618,12 @@ var ts; return getTagName(node.openingElement); } else { - var name_35 = node.tagName; - if (ts.isIdentifier(name_35) && ts.isIntrinsicJsxName(name_35.text)) { - return ts.createLiteral(name_35.text); + var name_36 = node.tagName; + if (ts.isIdentifier(name_36) && ts.isIntrinsicJsxName(name_36.text)) { + return ts.createLiteral(name_36.text); } else { - return ts.createExpressionFromEntityName(name_35); + return ts.createExpressionFromEntityName(name_36); } } } @@ -46575,6 +47913,9 @@ var ts; var hoistVariableDeclaration = context.hoistVariableDeclaration; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } return ts.visitEachChild(node, visitor, context); } function visitor(node) { @@ -46820,7 +48161,7 @@ var ts; _a[7 /* Endfinally */] = "endfinally", _a)); function transformGenerators(context) { - var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistFunctionDeclaration = context.hoistFunctionDeclaration, hoistVariableDeclaration = context.hoistVariableDeclaration, setSourceMapRange = context.setSourceMapRange, setCommentRange = context.setCommentRange, setNodeEmitFlags = context.setNodeEmitFlags; + var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistFunctionDeclaration = context.hoistFunctionDeclaration, hoistVariableDeclaration = context.hoistVariableDeclaration; var compilerOptions = context.getCompilerOptions(); var languageVersion = ts.getEmitScriptTarget(compilerOptions); var resolver = context.getEmitResolver(); @@ -46870,6 +48211,9 @@ var ts; var withBlockStack; // A stack containing `with` blocks. return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } if (node.transformFlags & 1024 /* ContainsGenerator */) { currentSourceFile = node; node = ts.visitEachChild(node, visitor, context); @@ -47011,7 +48355,7 @@ var ts; */ function visitFunctionDeclaration(node) { // Currently, we only support generators that were originally async functions. - if (node.asteriskToken && node.emitFlags & 2097152 /* AsyncFunctionBody */) { + if (node.asteriskToken && ts.getEmitFlags(node) & 2097152 /* AsyncFunctionBody */) { node = ts.setOriginalNode(ts.createFunctionDeclaration( /*decorators*/ undefined, /*modifiers*/ undefined, @@ -47050,7 +48394,7 @@ var ts; */ function visitFunctionExpression(node) { // Currently, we only support generators that were originally async functions. - if (node.asteriskToken && node.emitFlags & 2097152 /* AsyncFunctionBody */) { + if (node.asteriskToken && ts.getEmitFlags(node) & 2097152 /* AsyncFunctionBody */) { node = ts.setOriginalNode(ts.createFunctionExpression( /*asteriskToken*/ undefined, node.name, /*typeParameters*/ undefined, node.parameters, @@ -47099,6 +48443,7 @@ var ts; var savedBlocks = blocks; var savedBlockOffsets = blockOffsets; var savedBlockActions = blockActions; + var savedBlockStack = blockStack; var savedLabelOffsets = labelOffsets; var savedLabelExpressions = labelExpressions; var savedNextLabelId = nextLabelId; @@ -47112,6 +48457,7 @@ var ts; blocks = undefined; blockOffsets = undefined; blockActions = undefined; + blockStack = undefined; labelOffsets = undefined; labelExpressions = undefined; nextLabelId = 1; @@ -47132,6 +48478,7 @@ var ts; blocks = savedBlocks; blockOffsets = savedBlockOffsets; blockActions = savedBlockActions; + blockStack = savedBlockStack; labelOffsets = savedLabelOffsets; labelExpressions = savedLabelExpressions; nextLabelId = savedNextLabelId; @@ -47156,7 +48503,7 @@ var ts; } else { // Do not hoist custom prologues. - if (node.emitFlags & 8388608 /* CustomPrologue */) { + if (ts.getEmitFlags(node) & 8388608 /* CustomPrologue */) { return node; } for (var _i = 0, _a = node.declarationList.declarations; _i < _a.length; _i++) { @@ -48202,9 +49549,9 @@ var ts; } return -1; } - function onSubstituteNode(node, isExpression) { - node = previousOnSubstituteNode(node, isExpression); - if (isExpression) { + function onSubstituteNode(emitContext, node) { + node = previousOnSubstituteNode(emitContext, node); + if (emitContext === 1 /* Expression */) { return substituteExpression(node); } return node; @@ -48221,11 +49568,11 @@ var ts; if (ts.isIdentifier(original) && original.parent) { var declaration = resolver.getReferencedValueDeclaration(original); if (declaration) { - var name_36 = ts.getProperty(renamedCatchVariableDeclarations, String(ts.getOriginalNodeId(declaration))); - if (name_36) { - var clone_7 = ts.getMutableClone(name_36); - setSourceMapRange(clone_7, node); - setCommentRange(clone_7, node); + var name_37 = ts.getProperty(renamedCatchVariableDeclarations, String(ts.getOriginalNodeId(declaration))); + if (name_37) { + var clone_7 = ts.getMutableClone(name_37); + ts.setSourceMapRange(clone_7, node); + ts.setCommentRange(clone_7, node); return clone_7; } } @@ -48815,7 +50162,7 @@ var ts; return ts.createCall(ts.createHelperName(currentSourceFile.externalHelpersModuleName, "__generator"), /*typeArguments*/ undefined, [ ts.createThis(), - setNodeEmitFlags(ts.createFunctionExpression( + ts.setEmitFlags(ts.createFunctionExpression( /*asteriskToken*/ undefined, /*name*/ undefined, /*typeParameters*/ undefined, [ts.createParameter(state)], @@ -49218,8 +50565,32 @@ var ts; Jump[Jump["Continue"] = 4] = "Continue"; Jump[Jump["Return"] = 8] = "Return"; })(Jump || (Jump = {})); + var SuperCaptureResult; + (function (SuperCaptureResult) { + /** + * A capture may have been added for calls to 'super', but + * the caller should emit subsequent statements normally. + */ + SuperCaptureResult[SuperCaptureResult["NoReplacement"] = 0] = "NoReplacement"; + /** + * A call to 'super()' got replaced with a capturing statement like: + * + * var _this = _super.call(...) || this; + * + * Callers should skip the current statement. + */ + SuperCaptureResult[SuperCaptureResult["ReplaceSuperCapture"] = 1] = "ReplaceSuperCapture"; + /** + * A call to 'super()' got replaced with a capturing statement like: + * + * return _super.call(...) || this; + * + * Callers should skip the current statement and avoid any returns of '_this'. + */ + SuperCaptureResult[SuperCaptureResult["ReplaceWithReturn"] = 2] = "ReplaceWithReturn"; + })(SuperCaptureResult || (SuperCaptureResult = {})); function transformES6(context) { - var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration, getNodeEmitFlags = context.getNodeEmitFlags, setNodeEmitFlags = context.setNodeEmitFlags, getCommentRange = context.getCommentRange, setCommentRange = context.setCommentRange, getSourceMapRange = context.getSourceMapRange, setSourceMapRange = context.setSourceMapRange, setTokenSourceMapRange = context.setTokenSourceMapRange; + var startLexicalEnvironment = context.startLexicalEnvironment, endLexicalEnvironment = context.endLexicalEnvironment, hoistVariableDeclaration = context.hoistVariableDeclaration; var resolver = context.getEmitResolver(); var previousOnSubstituteNode = context.onSubstituteNode; var previousOnEmitNode = context.onEmitNode; @@ -49247,6 +50618,9 @@ var ts; var enabledSubstitutions; return transformSourceFile; function transformSourceFile(node) { + if (ts.isDeclarationFile(node)) { + return node; + } currentSourceFile = node; currentText = node.text; return ts.visitNode(node, visitor, ts.isSourceFile); @@ -49418,7 +50792,7 @@ var ts; enclosingFunction = currentNode; if (currentNode.kind !== 180 /* ArrowFunction */) { enclosingNonArrowFunction = currentNode; - if (!(currentNode.emitFlags & 2097152 /* AsyncFunctionBody */)) { + if (!(ts.getEmitFlags(currentNode) & 2097152 /* AsyncFunctionBody */)) { enclosingNonAsyncFunctionBody = currentNode; } } @@ -49634,17 +51008,17 @@ var ts; // To preserve the behavior of the old emitter, we explicitly indent // the body of the function here if it was requested in an earlier // transformation. - if (getNodeEmitFlags(node) & 524288 /* Indented */) { - setNodeEmitFlags(classFunction, 524288 /* Indented */); + if (ts.getEmitFlags(node) & 524288 /* Indented */) { + ts.setEmitFlags(classFunction, 524288 /* Indented */); } // "inner" and "outer" below are added purely to preserve source map locations from // the old emitter var inner = ts.createPartiallyEmittedExpression(classFunction); inner.end = node.end; - setNodeEmitFlags(inner, 49152 /* NoComments */); + ts.setEmitFlags(inner, 49152 /* NoComments */); var outer = ts.createPartiallyEmittedExpression(inner); outer.end = ts.skipTrivia(currentText, node.pos); - setNodeEmitFlags(outer, 49152 /* NoComments */); + ts.setEmitFlags(outer, 49152 /* NoComments */); return ts.createParen(ts.createCall(outer, /*typeArguments*/ undefined, extendsClauseElement ? [ts.visitNode(extendsClauseElement.expression, visitor, ts.isExpression)] @@ -49669,14 +51043,14 @@ var ts; // emit with the original emitter. var outer = ts.createPartiallyEmittedExpression(localName); outer.end = closingBraceLocation.end; - setNodeEmitFlags(outer, 49152 /* NoComments */); + ts.setEmitFlags(outer, 49152 /* NoComments */); var statement = ts.createReturn(outer); statement.pos = closingBraceLocation.pos; - setNodeEmitFlags(statement, 49152 /* NoComments */ | 12288 /* NoTokenSourceMaps */); + ts.setEmitFlags(statement, 49152 /* NoComments */ | 12288 /* NoTokenSourceMaps */); statements.push(statement); ts.addRange(statements, endLexicalEnvironment()); var block = ts.createBlock(ts.createNodeArray(statements, /*location*/ node.members), /*location*/ undefined, /*multiLine*/ true); - setNodeEmitFlags(block, 49152 /* NoComments */); + ts.setEmitFlags(block, 49152 /* NoComments */); return block; } /** @@ -49702,13 +51076,17 @@ var ts; function addConstructor(statements, node, extendsClauseElement) { var constructor = ts.getFirstConstructorWithBody(node); var hasSynthesizedSuper = hasSynthesizedDefaultSuperCall(constructor, extendsClauseElement !== undefined); - statements.push(ts.createFunctionDeclaration( + var constructorFunction = ts.createFunctionDeclaration( /*decorators*/ undefined, /*modifiers*/ undefined, /*asteriskToken*/ undefined, getDeclarationName(node), /*typeParameters*/ undefined, transformConstructorParameters(constructor, hasSynthesizedSuper), /*type*/ undefined, transformConstructorBody(constructor, node, extendsClauseElement, hasSynthesizedSuper), - /*location*/ constructor || node)); + /*location*/ constructor || node); + if (extendsClauseElement) { + ts.setEmitFlags(constructorFunction, 256 /* CapturesThis */); + } + statements.push(constructorFunction); } /** * Transforms the parameters of the constructor declaration of a class. @@ -49740,51 +51118,146 @@ var ts; function transformConstructorBody(constructor, node, extendsClauseElement, hasSynthesizedSuper) { var statements = []; startLexicalEnvironment(); + var statementOffset = -1; + if (hasSynthesizedSuper) { + // If a super call has already been synthesized, + // we're going to assume that we should just transform everything after that. + // The assumption is that no prior step in the pipeline has added any prologue directives. + statementOffset = 1; + } + else if (constructor) { + // Otherwise, try to emit all potential prologue directives first. + statementOffset = ts.addPrologueDirectives(statements, constructor.body.statements, /*ensureUseStrict*/ false, visitor); + } if (constructor) { - addCaptureThisForNodeIfNeeded(statements, constructor); addDefaultValueAssignmentsIfNeeded(statements, constructor); addRestParameterIfNeeded(statements, constructor, hasSynthesizedSuper); + ts.Debug.assert(statementOffset >= 0, "statementOffset not initialized correctly!"); + } + var superCaptureStatus = declareOrCaptureOrReturnThisForConstructorIfNeeded(statements, constructor, !!extendsClauseElement, hasSynthesizedSuper, statementOffset); + // The last statement expression was replaced. Skip it. + if (superCaptureStatus === 1 /* ReplaceSuperCapture */ || superCaptureStatus === 2 /* ReplaceWithReturn */) { + statementOffset++; } - addDefaultSuperCallIfNeeded(statements, constructor, extendsClauseElement, hasSynthesizedSuper); if (constructor) { - var body = saveStateAndInvoke(constructor, hasSynthesizedSuper ? transformConstructorBodyWithSynthesizedSuper : transformConstructorBodyWithoutSynthesizedSuper); + var body = saveStateAndInvoke(constructor, function (constructor) { return ts.visitNodes(constructor.body.statements, visitor, ts.isStatement, /*start*/ statementOffset); }); ts.addRange(statements, body); } + // Return `_this` unless we're sure enough that it would be pointless to add a return statement. + // If there's a constructor that we can tell returns in enough places, then we *do not* want to add a return. + if (extendsClauseElement + && superCaptureStatus !== 2 /* ReplaceWithReturn */ + && !(constructor && isSufficientlyCoveredByReturnStatements(constructor.body))) { + statements.push(ts.createReturn(ts.createIdentifier("_this"))); + } ts.addRange(statements, endLexicalEnvironment()); var block = ts.createBlock(ts.createNodeArray(statements, /*location*/ constructor ? constructor.body.statements : node.members), /*location*/ constructor ? constructor.body : node, /*multiLine*/ true); if (!constructor) { - setNodeEmitFlags(block, 49152 /* NoComments */); + ts.setEmitFlags(block, 49152 /* NoComments */); } return block; } - function transformConstructorBodyWithSynthesizedSuper(node) { - return ts.visitNodes(node.body.statements, visitor, ts.isStatement, 1); - } - function transformConstructorBodyWithoutSynthesizedSuper(node) { - return ts.visitNodes(node.body.statements, visitor, ts.isStatement, 0); + /** + * We want to try to avoid emitting a return statement in certain cases if a user already returned something. + * It would generate obviously dead code, so we'll try to make things a little bit prettier + * by doing a minimal check on whether some common patterns always explicitly return. + */ + function isSufficientlyCoveredByReturnStatements(statement) { + // A return statement is considered covered. + if (statement.kind === 211 /* ReturnStatement */) { + return true; + } + else if (statement.kind === 203 /* IfStatement */) { + var ifStatement = statement; + if (ifStatement.elseStatement) { + return isSufficientlyCoveredByReturnStatements(ifStatement.thenStatement) && + isSufficientlyCoveredByReturnStatements(ifStatement.elseStatement); + } + } + else if (statement.kind === 199 /* Block */) { + var lastStatement = ts.lastOrUndefined(statement.statements); + if (lastStatement && isSufficientlyCoveredByReturnStatements(lastStatement)) { + return true; + } + } + return false; } /** - * Adds a synthesized call to `_super` if it is needed. + * Declares a `_this` variable for derived classes and for when arrow functions capture `this`. * - * @param statements The statements for the new constructor body. - * @param constructor The constructor for the class. - * @param extendsClauseElement The expression for the class `extends` clause. - * @param hasSynthesizedSuper A value indicating whether the constructor starts with a - * synthesized `super` call. + * @returns The new statement offset into the `statements` array. */ - function addDefaultSuperCallIfNeeded(statements, constructor, extendsClauseElement, hasSynthesizedSuper) { - // If the TypeScript transformer needed to synthesize a constructor for property - // initializers, it would have also added a synthetic `...args` parameter and - // `super` call. - // If this is the case, or if the class has an `extends` clause but no - // constructor, we emit a synthesized call to `_super`. - if (constructor ? hasSynthesizedSuper : extendsClauseElement) { - statements.push(ts.createStatement(ts.createFunctionApply(ts.createIdentifier("_super"), ts.createThis(), ts.createIdentifier("arguments")), - /*location*/ extendsClauseElement)); + function declareOrCaptureOrReturnThisForConstructorIfNeeded(statements, ctor, hasExtendsClause, hasSynthesizedSuper, statementOffset) { + // If this isn't a derived class, just capture 'this' for arrow functions if necessary. + if (!hasExtendsClause) { + if (ctor) { + addCaptureThisForNodeIfNeeded(statements, ctor); + } + return 0 /* NoReplacement */; + } + // We must be here because the user didn't write a constructor + // but we needed to call 'super(...args)' anyway as per 14.5.14 of the ES2016 spec. + // If that's the case we can just immediately return the result of a 'super()' call. + if (!ctor) { + statements.push(ts.createReturn(createDefaultSuperCallOrThis())); + return 2 /* ReplaceWithReturn */; + } + // The constructor exists, but it and the 'super()' call it contains were generated + // for something like property initializers. + // Create a captured '_this' variable and assume it will subsequently be used. + if (hasSynthesizedSuper) { + captureThisForNode(statements, ctor, createDefaultSuperCallOrThis()); + enableSubstitutionsForCapturedThis(); + return 1 /* ReplaceSuperCapture */; } + // Most of the time, a 'super' call will be the first real statement in a constructor body. + // In these cases, we'd like to transform these into a *single* statement instead of a declaration + // followed by an assignment statement for '_this'. For instance, if we emitted without an initializer, + // we'd get: + // + // var _this; + // _this = _super.call(...) || this; + // + // instead of + // + // var _this = _super.call(...) || this; + // + // Additionally, if the 'super()' call is the last statement, we should just avoid capturing + // entirely and immediately return the result like so: + // + // return _super.call(...) || this; + // + var firstStatement; + var superCallExpression; + var ctorStatements = ctor.body.statements; + if (statementOffset < ctorStatements.length) { + firstStatement = ctorStatements[statementOffset]; + if (firstStatement.kind === 202 /* ExpressionStatement */ && ts.isSuperCall(firstStatement.expression)) { + var superCall = firstStatement.expression; + superCallExpression = ts.setOriginalNode(saveStateAndInvoke(superCall, visitImmediateSuperCallInBody), superCall); + } + } + // Return the result if we have an immediate super() call on the last statement. + if (superCallExpression && statementOffset === ctorStatements.length - 1) { + statements.push(ts.createReturn(superCallExpression)); + return 2 /* ReplaceWithReturn */; + } + // Perform the capture. + captureThisForNode(statements, ctor, superCallExpression, firstStatement); + // If we're actually replacing the original statement, we need to signal this to the caller. + if (superCallExpression) { + return 1 /* ReplaceSuperCapture */; + } + return 0 /* NoReplacement */; + } + function createDefaultSuperCallOrThis() { + var actualThis = ts.createThis(); + ts.setEmitFlags(actualThis, 128 /* NoSubstitution */); + var superCall = ts.createFunctionApply(ts.createIdentifier("_super"), actualThis, ts.createIdentifier("arguments")); + return ts.createLogicalOr(superCall, actualThis); } /** * Visits a parameter declaration. @@ -49837,17 +51310,17 @@ var ts; } for (var _i = 0, _a = node.parameters; _i < _a.length; _i++) { var parameter = _a[_i]; - var name_37 = parameter.name, initializer = parameter.initializer, dotDotDotToken = parameter.dotDotDotToken; + var name_38 = parameter.name, initializer = parameter.initializer, dotDotDotToken = parameter.dotDotDotToken; // A rest parameter cannot have a binding pattern or an initializer, // so let's just ignore it. if (dotDotDotToken) { continue; } - if (ts.isBindingPattern(name_37)) { - addDefaultValueAssignmentForBindingPattern(statements, parameter, name_37, initializer); + if (ts.isBindingPattern(name_38)) { + addDefaultValueAssignmentForBindingPattern(statements, parameter, name_38, initializer); } else if (initializer) { - addDefaultValueAssignmentForInitializer(statements, parameter, name_37, initializer); + addDefaultValueAssignmentForInitializer(statements, parameter, name_38, initializer); } } } @@ -49865,11 +51338,11 @@ var ts; // we usually don't want to emit a var declaration; however, in the presence // of an initializer, we must emit that expression to preserve side effects. if (name.elements.length > 0) { - statements.push(setNodeEmitFlags(ts.createVariableStatement( + statements.push(ts.setEmitFlags(ts.createVariableStatement( /*modifiers*/ undefined, ts.createVariableDeclarationList(ts.flattenParameterDestructuring(context, parameter, temp, visitor))), 8388608 /* CustomPrologue */)); } else if (initializer) { - statements.push(setNodeEmitFlags(ts.createStatement(ts.createAssignment(temp, ts.visitNode(initializer, visitor, ts.isExpression))), 8388608 /* CustomPrologue */)); + statements.push(ts.setEmitFlags(ts.createStatement(ts.createAssignment(temp, ts.visitNode(initializer, visitor, ts.isExpression))), 8388608 /* CustomPrologue */)); } } /** @@ -49882,14 +51355,14 @@ var ts; */ function addDefaultValueAssignmentForInitializer(statements, parameter, name, initializer) { initializer = ts.visitNode(initializer, visitor, ts.isExpression); - var statement = ts.createIf(ts.createStrictEquality(ts.getSynthesizedClone(name), ts.createVoidZero()), setNodeEmitFlags(ts.createBlock([ - ts.createStatement(ts.createAssignment(setNodeEmitFlags(ts.getMutableClone(name), 1536 /* NoSourceMap */), setNodeEmitFlags(initializer, 1536 /* NoSourceMap */ | getNodeEmitFlags(initializer)), + var statement = ts.createIf(ts.createStrictEquality(ts.getSynthesizedClone(name), ts.createVoidZero()), ts.setEmitFlags(ts.createBlock([ + ts.createStatement(ts.createAssignment(ts.setEmitFlags(ts.getMutableClone(name), 1536 /* NoSourceMap */), ts.setEmitFlags(initializer, 1536 /* NoSourceMap */ | ts.getEmitFlags(initializer)), /*location*/ parameter)) ], /*location*/ parameter), 32 /* SingleLine */ | 1024 /* NoTrailingSourceMap */ | 12288 /* NoTokenSourceMaps */), /*elseStatement*/ undefined, /*location*/ parameter); statement.startsOnNewLine = true; - setNodeEmitFlags(statement, 12288 /* NoTokenSourceMaps */ | 1024 /* NoTrailingSourceMap */ | 8388608 /* CustomPrologue */); + ts.setEmitFlags(statement, 12288 /* NoTokenSourceMaps */ | 1024 /* NoTrailingSourceMap */ | 8388608 /* CustomPrologue */); statements.push(statement); } /** @@ -49919,13 +51392,13 @@ var ts; } // `declarationName` is the name of the local declaration for the parameter. var declarationName = ts.getMutableClone(parameter.name); - setNodeEmitFlags(declarationName, 1536 /* NoSourceMap */); + ts.setEmitFlags(declarationName, 1536 /* NoSourceMap */); // `expressionName` is the name of the parameter used in expressions. var expressionName = ts.getSynthesizedClone(parameter.name); var restIndex = node.parameters.length - 1; var temp = ts.createLoopVariable(); // var param = []; - statements.push(setNodeEmitFlags(ts.createVariableStatement( + statements.push(ts.setEmitFlags(ts.createVariableStatement( /*modifiers*/ undefined, ts.createVariableDeclarationList([ ts.createVariableDeclaration(declarationName, /*type*/ undefined, ts.createArrayLiteral([])) @@ -49941,7 +51414,7 @@ var ts; ts.startOnNewLine(ts.createStatement(ts.createAssignment(ts.createElementAccess(expressionName, ts.createSubtract(temp, ts.createLiteral(restIndex))), ts.createElementAccess(ts.createIdentifier("arguments"), temp)), /*location*/ parameter)) ])); - setNodeEmitFlags(forStatement, 8388608 /* CustomPrologue */); + ts.setEmitFlags(forStatement, 8388608 /* CustomPrologue */); ts.startOnNewLine(forStatement); statements.push(forStatement); } @@ -49953,17 +51426,20 @@ var ts; */ function addCaptureThisForNodeIfNeeded(statements, node) { if (node.transformFlags & 16384 /* ContainsCapturedLexicalThis */ && node.kind !== 180 /* ArrowFunction */) { - enableSubstitutionsForCapturedThis(); - var captureThisStatement = ts.createVariableStatement( - /*modifiers*/ undefined, ts.createVariableDeclarationList([ - ts.createVariableDeclaration("_this", - /*type*/ undefined, ts.createThis()) - ])); - setNodeEmitFlags(captureThisStatement, 49152 /* NoComments */ | 8388608 /* CustomPrologue */); - setSourceMapRange(captureThisStatement, node); - statements.push(captureThisStatement); + captureThisForNode(statements, node, ts.createThis()); } } + function captureThisForNode(statements, node, initializer, originalStatement) { + enableSubstitutionsForCapturedThis(); + var captureThisStatement = ts.createVariableStatement( + /*modifiers*/ undefined, ts.createVariableDeclarationList([ + ts.createVariableDeclaration("_this", + /*type*/ undefined, initializer) + ]), originalStatement); + ts.setEmitFlags(captureThisStatement, 49152 /* NoComments */ | 8388608 /* CustomPrologue */); + ts.setSourceMapRange(captureThisStatement, node); + statements.push(captureThisStatement); + } /** * Adds statements to the class body function for a class to define the members of the * class. @@ -50012,20 +51488,20 @@ var ts; * @param member The MethodDeclaration node. */ function transformClassMethodDeclarationToStatement(receiver, member) { - var commentRange = getCommentRange(member); - var sourceMapRange = getSourceMapRange(member); + var commentRange = ts.getCommentRange(member); + var sourceMapRange = ts.getSourceMapRange(member); var func = transformFunctionLikeToExpression(member, /*location*/ member, /*name*/ undefined); - setNodeEmitFlags(func, 49152 /* NoComments */); - setSourceMapRange(func, sourceMapRange); + ts.setEmitFlags(func, 49152 /* NoComments */); + ts.setSourceMapRange(func, sourceMapRange); var statement = ts.createStatement(ts.createAssignment(ts.createMemberAccessForPropertyName(receiver, ts.visitNode(member.name, visitor, ts.isPropertyName), /*location*/ member.name), func), /*location*/ member); ts.setOriginalNode(statement, member); - setCommentRange(statement, commentRange); + ts.setCommentRange(statement, commentRange); // The location for the statement is used to emit comments only. // No source map should be emitted for this statement to align with the // old emitter. - setNodeEmitFlags(statement, 1536 /* NoSourceMap */); + ts.setEmitFlags(statement, 1536 /* NoSourceMap */); return statement; } /** @@ -50036,11 +51512,11 @@ var ts; */ function transformAccessorsToStatement(receiver, accessors) { var statement = ts.createStatement(transformAccessorsToExpression(receiver, accessors, /*startsOnNewLine*/ false), - /*location*/ getSourceMapRange(accessors.firstAccessor)); + /*location*/ ts.getSourceMapRange(accessors.firstAccessor)); // The location for the statement is used to emit source maps only. // No comments should be emitted for this statement to align with the // old emitter. - setNodeEmitFlags(statement, 49152 /* NoComments */); + ts.setEmitFlags(statement, 49152 /* NoComments */); return statement; } /** @@ -50054,24 +51530,24 @@ var ts; // To align with source maps in the old emitter, the receiver and property name // arguments are both mapped contiguously to the accessor name. var target = ts.getMutableClone(receiver); - setNodeEmitFlags(target, 49152 /* NoComments */ | 1024 /* NoTrailingSourceMap */); - setSourceMapRange(target, firstAccessor.name); + ts.setEmitFlags(target, 49152 /* NoComments */ | 1024 /* NoTrailingSourceMap */); + ts.setSourceMapRange(target, firstAccessor.name); var propertyName = ts.createExpressionForPropertyName(ts.visitNode(firstAccessor.name, visitor, ts.isPropertyName)); - setNodeEmitFlags(propertyName, 49152 /* NoComments */ | 512 /* NoLeadingSourceMap */); - setSourceMapRange(propertyName, firstAccessor.name); + ts.setEmitFlags(propertyName, 49152 /* NoComments */ | 512 /* NoLeadingSourceMap */); + ts.setSourceMapRange(propertyName, firstAccessor.name); var properties = []; if (getAccessor) { var getterFunction = transformFunctionLikeToExpression(getAccessor, /*location*/ undefined, /*name*/ undefined); - setSourceMapRange(getterFunction, getSourceMapRange(getAccessor)); + ts.setSourceMapRange(getterFunction, ts.getSourceMapRange(getAccessor)); var getter = ts.createPropertyAssignment("get", getterFunction); - setCommentRange(getter, getCommentRange(getAccessor)); + ts.setCommentRange(getter, ts.getCommentRange(getAccessor)); properties.push(getter); } if (setAccessor) { var setterFunction = transformFunctionLikeToExpression(setAccessor, /*location*/ undefined, /*name*/ undefined); - setSourceMapRange(setterFunction, getSourceMapRange(setAccessor)); + ts.setSourceMapRange(setterFunction, ts.getSourceMapRange(setAccessor)); var setter = ts.createPropertyAssignment("set", setterFunction); - setCommentRange(setter, getCommentRange(setAccessor)); + ts.setCommentRange(setter, ts.getCommentRange(setAccessor)); properties.push(setter); } properties.push(ts.createPropertyAssignment("enumerable", ts.createLiteral(true)), ts.createPropertyAssignment("configurable", ts.createLiteral(true))); @@ -50096,7 +51572,7 @@ var ts; enableSubstitutionsForCapturedThis(); } var func = transformFunctionLikeToExpression(node, /*location*/ node, /*name*/ undefined); - setNodeEmitFlags(func, 256 /* CapturesThis */); + ts.setEmitFlags(func, 256 /* CapturesThis */); return func; } /** @@ -50191,7 +51667,7 @@ var ts; } var expression = ts.visitNode(body, visitor, ts.isExpression); var returnStatement = ts.createReturn(expression, /*location*/ body); - setNodeEmitFlags(returnStatement, 12288 /* NoTokenSourceMaps */ | 1024 /* NoTrailingSourceMap */ | 32768 /* NoTrailingComments */); + ts.setEmitFlags(returnStatement, 12288 /* NoTokenSourceMaps */ | 1024 /* NoTrailingSourceMap */ | 32768 /* NoTrailingComments */); statements.push(returnStatement); // To align with the source map emit for the old emitter, we set a custom // source map location for the close brace. @@ -50205,10 +51681,10 @@ var ts; } var block = ts.createBlock(ts.createNodeArray(statements, statementsLocation), node.body, multiLine); if (!multiLine && singleLine) { - setNodeEmitFlags(block, 32 /* SingleLine */); + ts.setEmitFlags(block, 32 /* SingleLine */); } if (closeBraceLocation) { - setTokenSourceMapRange(block, 16 /* CloseBraceToken */, closeBraceLocation); + ts.setTokenSourceMapRange(block, 16 /* CloseBraceToken */, closeBraceLocation); } ts.setOriginalNode(block, node.body); return block; @@ -50274,7 +51750,7 @@ var ts; assignment = ts.flattenVariableDestructuringToExpression(context, decl, hoistVariableDeclaration, /*nameSubstitution*/ undefined, visitor); } else { - assignment = ts.createBinary(decl.name, 56 /* EqualsToken */, decl.initializer); + assignment = ts.createBinary(decl.name, 56 /* EqualsToken */, ts.visitNode(decl.initializer, visitor, ts.isExpression)); } (assignments || (assignments = [])).push(assignment); } @@ -50303,7 +51779,7 @@ var ts; : visitVariableDeclaration)); var declarationList = ts.createVariableDeclarationList(declarations, /*location*/ node); ts.setOriginalNode(declarationList, node); - setCommentRange(declarationList, node); + ts.setCommentRange(declarationList, node); if (node.transformFlags & 2097152 /* ContainsBindingPattern */ && (ts.isBindingPattern(node.declarations[0].name) || ts.isBindingPattern(ts.lastOrUndefined(node.declarations).name))) { @@ -50311,7 +51787,7 @@ var ts; // the source map range for the declaration list. var firstDeclaration = ts.firstOrUndefined(declarations); var lastDeclaration = ts.lastOrUndefined(declarations); - setSourceMapRange(declarationList, ts.createRange(firstDeclaration.pos, lastDeclaration.end)); + ts.setSourceMapRange(declarationList, ts.createRange(firstDeclaration.pos, lastDeclaration.end)); } return declarationList; } @@ -50501,7 +51977,7 @@ var ts; // emitter. var firstDeclaration = declarations[0]; var lastDeclaration = ts.lastOrUndefined(declarations); - setSourceMapRange(declarationList, ts.createRange(firstDeclaration.pos, lastDeclaration.end)); + ts.setSourceMapRange(declarationList, ts.createRange(firstDeclaration.pos, lastDeclaration.end)); statements.push(ts.createVariableStatement( /*modifiers*/ undefined, declarationList)); } @@ -50549,12 +52025,12 @@ var ts; } } // The old emitter does not emit source maps for the expression - setNodeEmitFlags(expression, 1536 /* NoSourceMap */ | getNodeEmitFlags(expression)); + ts.setEmitFlags(expression, 1536 /* NoSourceMap */ | ts.getEmitFlags(expression)); // The old emitter does not emit source maps for the block. // We add the location to preserve comments. var body = ts.createBlock(ts.createNodeArray(statements, /*location*/ statementsLocation), /*location*/ bodyLocation); - setNodeEmitFlags(body, 1536 /* NoSourceMap */ | 12288 /* NoTokenSourceMaps */); + ts.setEmitFlags(body, 1536 /* NoSourceMap */ | 12288 /* NoTokenSourceMaps */); var forStatement = ts.createFor(ts.createVariableDeclarationList([ ts.createVariableDeclaration(counter, /*type*/ undefined, ts.createLiteral(0), /*location*/ ts.moveRangePos(node.expression, -1)), ts.createVariableDeclaration(rhsReference, /*type*/ undefined, expression, /*location*/ node.expression) @@ -50562,7 +52038,7 @@ var ts; /*location*/ node.expression), ts.createPostfixIncrement(counter, /*location*/ node.expression), body, /*location*/ node); // Disable trailing source maps for the OpenParenToken to align source map emit with the old emitter. - setNodeEmitFlags(forStatement, 8192 /* NoTokenTrailingSourceMaps */); + ts.setEmitFlags(forStatement, 8192 /* NoTokenTrailingSourceMaps */); return forStatement; } /** @@ -50591,7 +52067,7 @@ var ts; var temp = ts.createTempVariable(hoistVariableDeclaration); // Write out the first non-computed properties, then emit the rest through indexing on the temp variable. var expressions = []; - var assignment = ts.createAssignment(temp, setNodeEmitFlags(ts.createObjectLiteral(ts.visitNodes(properties, visitor, ts.isObjectLiteralElementLike, 0, numInitialProperties), + var assignment = ts.createAssignment(temp, ts.setEmitFlags(ts.createObjectLiteral(ts.visitNodes(properties, visitor, ts.isObjectLiteralElementLike, 0, numInitialProperties), /*location*/ undefined, node.multiLine), 524288 /* Indented */)); if (node.multiLine) { assignment.startsOnNewLine = true; @@ -50698,7 +52174,7 @@ var ts; loopBody = ts.createBlock([loopBody], /*location*/ undefined, /*multiline*/ true); } var isAsyncBlockContainingAwait = enclosingNonArrowFunction - && (enclosingNonArrowFunction.emitFlags & 2097152 /* AsyncFunctionBody */) !== 0 + && (ts.getEmitFlags(enclosingNonArrowFunction) & 2097152 /* AsyncFunctionBody */) !== 0 && (node.statement.transformFlags & 4194304 /* ContainsYield */) !== 0; var loopBodyFlags = 0; if (currentState.containsLexicalThis) { @@ -50710,7 +52186,7 @@ var ts; var convertedLoopVariable = ts.createVariableStatement( /*modifiers*/ undefined, ts.createVariableDeclarationList([ ts.createVariableDeclaration(functionName, - /*type*/ undefined, setNodeEmitFlags(ts.createFunctionExpression(isAsyncBlockContainingAwait ? ts.createToken(37 /* AsteriskToken */) : undefined, + /*type*/ undefined, ts.setEmitFlags(ts.createFunctionExpression(isAsyncBlockContainingAwait ? ts.createToken(37 /* AsteriskToken */) : undefined, /*name*/ undefined, /*typeParameters*/ undefined, loopParameters, /*type*/ undefined, loopBody), loopBodyFlags)) @@ -50756,8 +52232,8 @@ var ts; extraVariableDeclarations = []; } // hoist collected variable declarations - for (var name_38 in currentState.hoistedLocalVariables) { - var identifier = currentState.hoistedLocalVariables[name_38]; + for (var _b = 0, _c = currentState.hoistedLocalVariables; _b < _c.length; _b++) { + var identifier = _c[_b]; extraVariableDeclarations.push(ts.createVariableDeclaration(identifier)); } } @@ -50767,8 +52243,8 @@ var ts; if (!extraVariableDeclarations) { extraVariableDeclarations = []; } - for (var _b = 0, loopOutParameters_1 = loopOutParameters; _b < loopOutParameters_1.length; _b++) { - var outParam = loopOutParameters_1[_b]; + for (var _d = 0, loopOutParameters_1 = loopOutParameters; _d < loopOutParameters_1.length; _d++) { + var outParam = loopOutParameters_1[_d]; extraVariableDeclarations.push(ts.createVariableDeclaration(outParam.outParamName)); } } @@ -51003,7 +52479,7 @@ var ts; // Methods with computed property names are handled in visitObjectLiteralExpression. ts.Debug.assert(!ts.isComputedPropertyName(node.name)); var functionExpression = transformFunctionLikeToExpression(node, /*location*/ ts.moveRangePos(node, -1), /*name*/ undefined); - setNodeEmitFlags(functionExpression, 16384 /* NoLeadingComments */ | getNodeEmitFlags(functionExpression)); + ts.setEmitFlags(functionExpression, 16384 /* NoLeadingComments */ | ts.getEmitFlags(functionExpression)); return ts.createPropertyAssignment(node.name, functionExpression, /*location*/ node); } @@ -51040,9 +52516,19 @@ var ts; * @param node a CallExpression. */ function visitCallExpression(node) { + return visitCallExpressionWithPotentialCapturedThisAssignment(node, /*assignToCapturedThis*/ true); + } + function visitImmediateSuperCallInBody(node) { + return visitCallExpressionWithPotentialCapturedThisAssignment(node, /*assignToCapturedThis*/ false); + } + function visitCallExpressionWithPotentialCapturedThisAssignment(node, assignToCapturedThis) { // We are here either because SuperKeyword was used somewhere in the expression, or // because we contain a SpreadElementExpression. var _a = ts.createCallBinding(node.expression, hoistVariableDeclaration), target = _a.target, thisArg = _a.thisArg; + if (node.expression.kind === 95 /* SuperKeyword */) { + ts.setEmitFlags(thisArg, 128 /* NoSubstitution */); + } + var resultingCall; if (node.transformFlags & 262144 /* ContainsSpreadElementExpression */) { // [source] // f(...a, b) @@ -51057,7 +52543,7 @@ var ts; // _super.apply(this, a.concat([b])) // _super.m.apply(this, a.concat([b])) // _super.prototype.m.apply(this, a.concat([b])) - return ts.createFunctionApply(ts.visitNode(target, visitor, ts.isExpression), ts.visitNode(thisArg, visitor, ts.isExpression), transformAndSpreadElements(node.arguments, /*needsUniqueCopy*/ false, /*multiLine*/ false, /*hasTrailingComma*/ false)); + resultingCall = ts.createFunctionApply(ts.visitNode(target, visitor, ts.isExpression), ts.visitNode(thisArg, visitor, ts.isExpression), transformAndSpreadElements(node.arguments, /*needsUniqueCopy*/ false, /*multiLine*/ false, /*hasTrailingComma*/ false)); } else { // [source] @@ -51069,9 +52555,18 @@ var ts; // _super.call(this, a) // _super.m.call(this, a) // _super.prototype.m.call(this, a) - return ts.createFunctionCall(ts.visitNode(target, visitor, ts.isExpression), ts.visitNode(thisArg, visitor, ts.isExpression), ts.visitNodes(node.arguments, visitor, ts.isExpression), + resultingCall = ts.createFunctionCall(ts.visitNode(target, visitor, ts.isExpression), ts.visitNode(thisArg, visitor, ts.isExpression), ts.visitNodes(node.arguments, visitor, ts.isExpression), /*location*/ node); } + if (node.expression.kind === 95 /* SuperKeyword */) { + var actualThis = ts.createThis(); + ts.setEmitFlags(actualThis, 128 /* NoSubstitution */); + var initializer = ts.createLogicalOr(resultingCall, actualThis); + return assignToCapturedThis + ? ts.createAssignment(ts.createIdentifier("_this"), initializer) + : initializer; + } + return resultingCall; } /** * Visits a NewExpression that contains a spread element. @@ -51313,13 +52808,13 @@ var ts; * * @param node The node to be printed. */ - function onEmitNode(node, emit) { + function onEmitNode(emitContext, node, emitCallback) { var savedEnclosingFunction = enclosingFunction; if (enabledSubstitutions & 1 /* CapturedThis */ && ts.isFunctionLike(node)) { // If we are tracking a captured `this`, keep track of the enclosing function. enclosingFunction = node; } - previousOnEmitNode(node, emit); + previousOnEmitNode(emitContext, node, emitCallback); enclosingFunction = savedEnclosingFunction; } /** @@ -51356,9 +52851,9 @@ var ts; * @param isExpression A value indicating whether the node is to be used in an expression * position. */ - function onSubstituteNode(node, isExpression) { - node = previousOnSubstituteNode(node, isExpression); - if (isExpression) { + function onSubstituteNode(emitContext, node) { + node = previousOnSubstituteNode(emitContext, node); + if (emitContext === 1 /* Expression */) { return substituteExpression(node); } if (ts.isIdentifier(node)) { @@ -51434,7 +52929,7 @@ var ts; function substituteThisKeyword(node) { if (enabledSubstitutions & 1 /* CapturedThis */ && enclosingFunction - && enclosingFunction.emitFlags & 256 /* CapturesThis */) { + && ts.getEmitFlags(enclosingFunction) & 256 /* CapturesThis */) { return ts.createIdentifier("_this", /*location*/ node); } return node; @@ -51461,7 +52956,7 @@ var ts; function getDeclarationName(node, allowComments, allowSourceMaps, emitFlags) { if (node.name && !ts.isGeneratedIdentifier(node.name)) { var name_39 = ts.getMutableClone(node.name); - emitFlags |= getNodeEmitFlags(node.name); + emitFlags |= ts.getEmitFlags(node.name); if (!allowSourceMaps) { emitFlags |= 1536 /* NoSourceMap */; } @@ -51469,7 +52964,7 @@ var ts; emitFlags |= 49152 /* NoComments */; } if (emitFlags) { - setNodeEmitFlags(name_39, emitFlags); + ts.setEmitFlags(name_39, emitFlags); } return name_39; } @@ -51552,13 +53047,6 @@ var ts; return transformers; } ts.getTransformers = getTransformers; - /** - * Tracks a monotonically increasing transformation id used to associate a node with a specific - * transformation. This ensures transient properties related to transformations can be safely - * stored on source tree nodes that may be reused across multiple transformations (such as - * with compile-on-save). - */ - var nextTransformId = 1; /** * Transforms an array of SourceFiles by passing them through each transformer. * @@ -51568,16 +53056,9 @@ var ts; * @param transforms An array of Transformers. */ function transformFiles(resolver, host, sourceFiles, transformers) { - var transformId = nextTransformId; - nextTransformId++; - var tokenSourceMapRanges = ts.createMap(); var lexicalEnvironmentVariableDeclarationsStack = []; var lexicalEnvironmentFunctionDeclarationsStack = []; var enabledSyntaxKindFeatures = new Array(289 /* Count */); - var parseTreeNodesWithAnnotations = []; - var lastTokenSourceMapRangeNode; - var lastTokenSourceMapRangeToken; - var lastTokenSourceMapRange; var lexicalEnvironmentStackOffset = 0; var hoistedVariableDeclarations; var hoistedFunctionDeclarations; @@ -51588,56 +53069,27 @@ var ts; getCompilerOptions: function () { return host.getCompilerOptions(); }, getEmitResolver: function () { return resolver; }, getEmitHost: function () { return host; }, - getNodeEmitFlags: getNodeEmitFlags, - setNodeEmitFlags: setNodeEmitFlags, - getSourceMapRange: getSourceMapRange, - setSourceMapRange: setSourceMapRange, - getTokenSourceMapRange: getTokenSourceMapRange, - setTokenSourceMapRange: setTokenSourceMapRange, - getCommentRange: getCommentRange, - setCommentRange: setCommentRange, hoistVariableDeclaration: hoistVariableDeclaration, hoistFunctionDeclaration: hoistFunctionDeclaration, startLexicalEnvironment: startLexicalEnvironment, endLexicalEnvironment: endLexicalEnvironment, - onSubstituteNode: onSubstituteNode, + onSubstituteNode: function (emitContext, node) { return node; }, enableSubstitution: enableSubstitution, isSubstitutionEnabled: isSubstitutionEnabled, - onEmitNode: onEmitNode, + onEmitNode: function (node, emitContext, emitCallback) { return emitCallback(node, emitContext); }, enableEmitNotification: enableEmitNotification, isEmitNotificationEnabled: isEmitNotificationEnabled }; // Chain together and initialize each transformer. - var transformation = chain.apply(void 0, transformers)(context); + var transformation = ts.chain.apply(void 0, transformers)(context); // Transform each source file. var transformed = ts.map(sourceFiles, transformSourceFile); // Disable modification of the lexical environment. lexicalEnvironmentDisabled = true; return { - getSourceFiles: function () { return transformed; }, - getTokenSourceMapRange: getTokenSourceMapRange, - isSubstitutionEnabled: isSubstitutionEnabled, - isEmitNotificationEnabled: isEmitNotificationEnabled, - onSubstituteNode: context.onSubstituteNode, - onEmitNode: context.onEmitNode, - dispose: function () { - // During transformation we may need to annotate a parse tree node with transient - // transformation properties. As parse tree nodes live longer than transformation - // nodes, we need to make sure we reclaim any memory allocated for custom ranges - // from these nodes to ensure we do not hold onto entire subtrees just for position - // information. We also need to reset these nodes to a pre-transformation state - // for incremental parsing scenarios so that we do not impact later emit. - for (var _i = 0, parseTreeNodesWithAnnotations_1 = parseTreeNodesWithAnnotations; _i < parseTreeNodesWithAnnotations_1.length; _i++) { - var node = parseTreeNodesWithAnnotations_1[_i]; - if (node.transformId === transformId) { - node.transformId = 0; - node.emitFlags = 0; - node.commentRange = undefined; - node.sourceMapRange = undefined; - } - } - parseTreeNodesWithAnnotations.length = 0; - } + transformed: transformed, + emitNodeWithSubstitution: emitNodeWithSubstitution, + emitNodeWithNotification: emitNodeWithNotification }; /** * Transforms a source file. @@ -51660,17 +53112,27 @@ var ts; * Determines whether expression substitutions are enabled for the provided node. */ function isSubstitutionEnabled(node) { - return (enabledSyntaxKindFeatures[node.kind] & 1 /* Substitution */) !== 0; + return (enabledSyntaxKindFeatures[node.kind] & 1 /* Substitution */) !== 0 + && (ts.getEmitFlags(node) & 128 /* NoSubstitution */) === 0; } /** - * Default hook for node substitutions. + * Emits a node with possible substitution. * - * @param node The node to substitute. - * @param isExpression A value indicating whether the node is to be used in an expression - * position. + * @param emitContext The current emit context. + * @param node The node to emit. + * @param emitCallback The callback used to emit the node or its substitute. */ - function onSubstituteNode(node, isExpression) { - return node; + function emitNodeWithSubstitution(emitContext, node, emitCallback) { + if (node) { + if (isSubstitutionEnabled(node)) { + var substitute = context.onSubstituteNode(emitContext, node); + if (substitute && substitute !== node) { + emitCallback(emitContext, substitute); + return; + } + } + emitCallback(emitContext, node); + } } /** * Enables before/after emit notifications in the pretty printer for the provided SyntaxKind. @@ -51684,141 +53146,24 @@ var ts; */ function isEmitNotificationEnabled(node) { return (enabledSyntaxKindFeatures[node.kind] & 2 /* EmitNotifications */) !== 0 - || (getNodeEmitFlags(node) & 64 /* AdviseOnEmitNode */) !== 0; + || (ts.getEmitFlags(node) & 64 /* AdviseOnEmitNode */) !== 0; } /** - * Default hook for node emit. + * Emits a node with possible emit notification. * + * @param emitContext The current emit context. * @param node The node to emit. - * @param emit A callback used to emit the node in the printer. - */ - function onEmitNode(node, emit) { - emit(node); - } - /** - * Associates a node with the current transformation, initializing - * various transient transformation properties. - * - * @param node The node. + * @param emitCallback The callback used to emit the node. */ - function beforeSetAnnotation(node) { - if ((node.flags & 8 /* Synthesized */) === 0 && node.transformId !== transformId) { - // To avoid holding onto transformation artifacts, we keep track of any - // parse tree node we are annotating. This allows us to clean them up after - // all transformations have completed. - parseTreeNodesWithAnnotations.push(node); - node.transformId = transformId; - } - } - /** - * Gets flags that control emit behavior of a node. - * - * If the node does not have its own NodeEmitFlags set, the node emit flags of its - * original pointer are used. - * - * @param node The node. - */ - function getNodeEmitFlags(node) { - return node.emitFlags; - } - /** - * Sets flags that control emit behavior of a node. - * - * @param node The node. - * @param emitFlags The NodeEmitFlags for the node. - */ - function setNodeEmitFlags(node, emitFlags) { - beforeSetAnnotation(node); - node.emitFlags = emitFlags; - return node; - } - /** - * Gets a custom text range to use when emitting source maps. - * - * If a node does not have its own custom source map text range, the custom source map - * text range of its original pointer is used. - * - * @param node The node. - */ - function getSourceMapRange(node) { - return node.sourceMapRange || node; - } - /** - * Sets a custom text range to use when emitting source maps. - * - * @param node The node. - * @param range The text range. - */ - function setSourceMapRange(node, range) { - beforeSetAnnotation(node); - node.sourceMapRange = range; - return node; - } - /** - * Gets the TextRange to use for source maps for a token of a node. - * - * If a node does not have its own custom source map text range for a token, the custom - * source map text range for the token of its original pointer is used. - * - * @param node The node. - * @param token The token. - */ - function getTokenSourceMapRange(node, token) { - // As a performance optimization, use the cached value of the most recent node. - // This helps for cases where this function is called repeatedly for the same node. - if (lastTokenSourceMapRangeNode === node && lastTokenSourceMapRangeToken === token) { - return lastTokenSourceMapRange; - } - // Get the custom token source map range for a node or from one of its original nodes. - // Custom token ranges are not stored on the node to avoid the GC burden. - var range; - var current = node; - while (current) { - range = current.id ? tokenSourceMapRanges[current.id + "-" + token] : undefined; - if (range !== undefined) { - break; + function emitNodeWithNotification(emitContext, node, emitCallback) { + if (node) { + if (isEmitNotificationEnabled(node)) { + context.onEmitNode(emitContext, node, emitCallback); + } + else { + emitCallback(emitContext, node); } - current = current.original; } - // Cache the most recently requested value. - lastTokenSourceMapRangeNode = node; - lastTokenSourceMapRangeToken = token; - lastTokenSourceMapRange = range; - return range; - } - /** - * Sets the TextRange to use for source maps for a token of a node. - * - * @param node The node. - * @param token The token. - * @param range The text range. - */ - function setTokenSourceMapRange(node, token, range) { - // Cache the most recently requested value. - lastTokenSourceMapRangeNode = node; - lastTokenSourceMapRangeToken = token; - lastTokenSourceMapRange = range; - tokenSourceMapRanges[ts.getNodeId(node) + "-" + token] = range; - return node; - } - /** - * Gets a custom text range to use when emitting comments. - * - * If a node does not have its own custom source map text range, the custom source map - * text range of its original pointer is used. - * - * @param node The node. - */ - function getCommentRange(node) { - return node.commentRange || node; - } - /** - * Sets a custom text range to use when emitting comments. - */ - function setCommentRange(node, range) { - beforeSetAnnotation(node); - node.commentRange = range; - return node; } /** * Records a hoisted variable declaration for the provided name within a lexical environment. @@ -51891,95 +53236,12 @@ var ts; } } ts.transformFiles = transformFiles; - function chain(a, b, c, d, e) { - if (e) { - var args_3 = []; - for (var i = 0; i < arguments.length; i++) { - args_3[i] = arguments[i]; - } - return function (t) { return compose.apply(void 0, ts.map(args_3, function (f) { return f(t); })); }; - } - else if (d) { - return function (t) { return compose(a(t), b(t), c(t), d(t)); }; - } - else if (c) { - return function (t) { return compose(a(t), b(t), c(t)); }; - } - else if (b) { - return function (t) { return compose(a(t), b(t)); }; - } - else if (a) { - return function (t) { return compose(a(t)); }; - } - else { - return function (t) { return function (u) { return u; }; }; - } - } - function compose(a, b, c, d, e) { - if (e) { - var args_4 = []; - for (var i = 0; i < arguments.length; i++) { - args_4[i] = arguments[i]; - } - return function (t) { return ts.reduceLeft(args_4, function (u, f) { return f(u); }, t); }; - } - else if (d) { - return function (t) { return d(c(b(a(t)))); }; - } - else if (c) { - return function (t) { return c(b(a(t))); }; - } - else if (b) { - return function (t) { return b(a(t)); }; - } - else if (a) { - return function (t) { return a(t); }; - } - else { - return function (t) { return t; }; - } - } var _a; })(ts || (ts = {})); /// /* @internal */ var ts; (function (ts) { - function createSourceMapWriter(host, writer) { - var compilerOptions = host.getCompilerOptions(); - if (compilerOptions.sourceMap || compilerOptions.inlineSourceMap) { - if (compilerOptions.extendedDiagnostics) { - return createSourceMapWriterWithExtendedDiagnostics(host, writer); - } - return createSourceMapWriterWorker(host, writer); - } - else { - return getNullSourceMapWriter(); - } - } - ts.createSourceMapWriter = createSourceMapWriter; - var nullSourceMapWriter; - function getNullSourceMapWriter() { - if (nullSourceMapWriter === undefined) { - nullSourceMapWriter = { - initialize: function (filePath, sourceMapFilePath, sourceFiles, isBundledEmit) { }, - reset: function () { }, - getSourceMapData: function () { return undefined; }, - setSourceFile: function (sourceFile) { }, - emitPos: function (pos) { }, - emitStart: function (range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { }, - emitEnd: function (range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { }, - emitTokenStart: function (token, pos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { return -1; }, - emitTokenEnd: function (token, end, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { return -1; }, - changeEmitSourcePos: function () { }, - stopOverridingSpan: function () { }, - getText: function () { return undefined; }, - getSourceMappingURL: function () { return undefined; } - }; - } - return nullSourceMapWriter; - } - ts.getNullSourceMapWriter = getNullSourceMapWriter; // Used for initialize lastEncodedSourceMapSpan and reset lastEncodedSourceMapSpan when updateLastEncodedAndRecordedSpans var defaultLastEncodedSourceMapSpan = { emittedLine: 1, @@ -51988,14 +53250,12 @@ var ts; sourceColumn: 1, sourceIndex: 0 }; - function createSourceMapWriterWorker(host, writer) { + function createSourceMapWriter(host, writer) { var compilerOptions = host.getCompilerOptions(); var extendedDiagnostics = compilerOptions.extendedDiagnostics; var currentSourceFile; var currentSourceText; var sourceMapDir; // The directory in which sourcemap will be - var stopOverridingSpan = false; - var modifyLastSourcePos = false; // Current source map file and its index in the sources list var sourceMapSourceIndex; // Last recorded and encoded spans @@ -52004,24 +53264,15 @@ var ts; var lastEncodedNameIndex; // Source map data var sourceMapData; - // This keeps track of the number of times `disable` has been called without a - // corresponding call to `enable`. As long as this value is non-zero, mappings will not - // be recorded. - // This is primarily used to provide a better experience when debugging binding - // patterns and destructuring assignments for simple expressions. - var disableDepth; + var disabled = !(compilerOptions.sourceMap || compilerOptions.inlineSourceMap); return { initialize: initialize, reset: reset, getSourceMapData: function () { return sourceMapData; }, setSourceFile: setSourceFile, emitPos: emitPos, - emitStart: emitStart, - emitEnd: emitEnd, - emitTokenStart: emitTokenStart, - emitTokenEnd: emitTokenEnd, - changeEmitSourcePos: changeEmitSourcePos, - stopOverridingSpan: function () { return stopOverridingSpan = true; }, + emitNodeWithSourceMap: emitNodeWithSourceMap, + emitTokenWithSourceMap: emitTokenWithSourceMap, getText: getText, getSourceMappingURL: getSourceMappingURL }; @@ -52034,12 +53285,14 @@ var ts; * @param isBundledEmit A value indicating whether the generated output file is a bundle. */ function initialize(filePath, sourceMapFilePath, sourceFiles, isBundledEmit) { + if (disabled) { + return; + } if (sourceMapData) { reset(); } currentSourceFile = undefined; currentSourceText = undefined; - disableDepth = 0; // Current source map file and its index in the sources list sourceMapSourceIndex = -1; // Last recorded and encoded spans @@ -52093,6 +53346,9 @@ var ts; * Reset the SourceMapWriter to an empty state. */ function reset() { + if (disabled) { + return; + } currentSourceFile = undefined; sourceMapDir = undefined; sourceMapSourceIndex = undefined; @@ -52100,56 +53356,6 @@ var ts; lastEncodedSourceMapSpan = undefined; lastEncodedNameIndex = undefined; sourceMapData = undefined; - disableDepth = 0; - } - /** - * Re-enables the recording of mappings. - */ - function enable() { - if (disableDepth > 0) { - disableDepth--; - } - } - /** - * Disables the recording of mappings. - */ - function disable() { - disableDepth++; - } - function updateLastEncodedAndRecordedSpans() { - if (modifyLastSourcePos) { - // Reset the source pos - modifyLastSourcePos = false; - // Change Last recorded Map with last encoded emit line and character - lastRecordedSourceMapSpan.emittedLine = lastEncodedSourceMapSpan.emittedLine; - lastRecordedSourceMapSpan.emittedColumn = lastEncodedSourceMapSpan.emittedColumn; - // Pop sourceMapDecodedMappings to remove last entry - sourceMapData.sourceMapDecodedMappings.pop(); - // Point the lastEncodedSourceMapSpace to the previous encoded sourceMapSpan - // If the list is empty which indicates that we are at the beginning of the file, - // we have to reset it to default value (same value when we first initialize sourceMapWriter) - lastEncodedSourceMapSpan = sourceMapData.sourceMapDecodedMappings.length ? - sourceMapData.sourceMapDecodedMappings[sourceMapData.sourceMapDecodedMappings.length - 1] : - defaultLastEncodedSourceMapSpan; - // TODO: Update lastEncodedNameIndex - // Since we dont support this any more, lets not worry about it right now. - // When we start supporting nameIndex, we will get back to this - // Change the encoded source map - var sourceMapMappings = sourceMapData.sourceMapMappings; - var lenthToSet = sourceMapMappings.length - 1; - for (; lenthToSet >= 0; lenthToSet--) { - var currentChar = sourceMapMappings.charAt(lenthToSet); - if (currentChar === ",") { - // Separator for the entry found - break; - } - if (currentChar === ";" && lenthToSet !== 0 && sourceMapMappings.charAt(lenthToSet - 1) !== ";") { - // Last line separator found - break; - } - } - sourceMapData.sourceMapMappings = sourceMapMappings.substr(0, Math.max(0, lenthToSet)); - } } // Encoding for sourcemap span function encodeLastRecordedSourceMapSpan() { @@ -52197,7 +53403,7 @@ var ts; * @param pos The position. */ function emitPos(pos) { - if (ts.positionIsSynthesized(pos) || disableDepth > 0) { + if (disabled || ts.positionIsSynthesized(pos)) { return; } if (extendedDiagnostics) { @@ -52226,84 +53432,78 @@ var ts; sourceColumn: sourceLinePos.character, sourceIndex: sourceMapSourceIndex }; - stopOverridingSpan = false; } - else if (!stopOverridingSpan) { + else { // Take the new pos instead since there is no change in emittedLine and column since last location lastRecordedSourceMapSpan.sourceLine = sourceLinePos.line; lastRecordedSourceMapSpan.sourceColumn = sourceLinePos.character; lastRecordedSourceMapSpan.sourceIndex = sourceMapSourceIndex; } - updateLastEncodedAndRecordedSpans(); if (extendedDiagnostics) { ts.performance.mark("afterSourcemap"); ts.performance.measure("Source Map", "beforeSourcemap", "afterSourcemap"); } } - function getStartPosPastDecorators(range) { - var rangeHasDecorators = !!range.decorators; - return ts.skipTrivia(currentSourceText, rangeHasDecorators ? range.decorators.end : range.pos); - } - function emitStart(range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { - if (contextNode) { - if (!ignoreNodeCallback(contextNode)) { - range = getTextRangeCallback(contextNode) || range; - emitPos(getStartPosPastDecorators(range)); - } - if (ignoreChildrenCallback(contextNode)) { - disable(); - } - } - else { - emitPos(getStartPosPastDecorators(range)); + /** + * Emits a node with possible leading and trailing source maps. + * + * @param node The node to emit. + * @param emitCallback The callback used to emit the node. + */ + function emitNodeWithSourceMap(emitContext, node, emitCallback) { + if (disabled) { + return emitCallback(emitContext, node); } - } - function emitEnd(range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { - if (contextNode) { - if (ignoreChildrenCallback(contextNode)) { - enable(); - } - if (!ignoreNodeCallback(contextNode)) { - range = getTextRangeCallback(contextNode) || range; - emitPos(range.end); + if (node) { + var emitNode = node.emitNode; + var emitFlags = emitNode && emitNode.flags; + var _a = emitNode && emitNode.sourceMapRange || node, pos = _a.pos, end = _a.end; + if (node.kind !== 287 /* NotEmittedStatement */ + && (emitFlags & 512 /* NoLeadingSourceMap */) === 0 + && pos >= 0) { + emitPos(ts.skipTrivia(currentSourceText, pos)); + } + if (emitFlags & 2048 /* NoNestedSourceMaps */) { + disabled = true; + emitCallback(emitContext, node); + disabled = false; } - } - else { - emitPos(range.end); - } - stopOverridingSpan = false; - } - function emitTokenStart(token, tokenStartPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { - if (contextNode) { - if (ignoreTokenCallback(contextNode, token)) { - return ts.skipTrivia(currentSourceText, tokenStartPos); + else { + emitCallback(emitContext, node); } - var range = getTokenTextRangeCallback(contextNode, token); - if (range) { - tokenStartPos = range.pos; + if (node.kind !== 287 /* NotEmittedStatement */ + && (emitFlags & 1024 /* NoTrailingSourceMap */) === 0 + && end >= 0) { + emitPos(end); } } - tokenStartPos = ts.skipTrivia(currentSourceText, tokenStartPos); - emitPos(tokenStartPos); - return tokenStartPos; } - function emitTokenEnd(token, tokenEndPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { - if (contextNode) { - if (ignoreTokenCallback(contextNode, token)) { - return tokenEndPos; - } - var range = getTokenTextRangeCallback(contextNode, token); - if (range) { - tokenEndPos = range.end; - } + /** + * Emits a token of a node with possible leading and trailing source maps. + * + * @param node The node containing the token. + * @param token The token to emit. + * @param tokenStartPos The start pos of the token. + * @param emitCallback The callback used to emit the token. + */ + function emitTokenWithSourceMap(node, token, tokenPos, emitCallback) { + if (disabled) { + return emitCallback(token, tokenPos); } - emitPos(tokenEndPos); - return tokenEndPos; - } - // @deprecated - function changeEmitSourcePos() { - ts.Debug.assert(!modifyLastSourcePos); - modifyLastSourcePos = true; + var emitNode = node && node.emitNode; + var emitFlags = emitNode && emitNode.flags; + var range = emitNode && emitNode.tokenSourceMapRanges && emitNode.tokenSourceMapRanges[token]; + tokenPos = ts.skipTrivia(currentSourceText, range ? range.pos : tokenPos); + if ((emitFlags & 4096 /* NoTokenLeadingSourceMaps */) === 0 && tokenPos >= 0) { + emitPos(tokenPos); + } + tokenPos = emitCallback(token, tokenPos); + if (range) + tokenPos = range.end; + if ((emitFlags & 8192 /* NoTokenTrailingSourceMaps */) === 0 && tokenPos >= 0) { + emitPos(tokenPos); + } + return tokenPos; } /** * Set the current source file. @@ -52311,6 +53511,9 @@ var ts; * @param sourceFile The source file. */ function setSourceFile(sourceFile) { + if (disabled) { + return; + } currentSourceFile = sourceFile; currentSourceText = currentSourceFile.text; // Add the file to tsFilePaths @@ -52334,6 +53537,9 @@ var ts; * Gets the text for the source map. */ function getText() { + if (disabled) { + return; + } encodeLastRecordedSourceMapSpan(); return ts.stringify({ version: 3, @@ -52349,6 +53555,9 @@ var ts; * Gets the SourceMappingURL for the source map. */ function getSourceMappingURL() { + if (disabled) { + return; + } if (compilerOptions.inlineSourceMap) { // Encode the sourceMap into the sourceMap url var base64SourceMapText = ts.convertToBase64(getText()); @@ -52359,46 +53568,7 @@ var ts; } } } - function createSourceMapWriterWithExtendedDiagnostics(host, writer) { - var _a = createSourceMapWriterWorker(host, writer), initialize = _a.initialize, reset = _a.reset, getSourceMapData = _a.getSourceMapData, setSourceFile = _a.setSourceFile, emitPos = _a.emitPos, emitStart = _a.emitStart, emitEnd = _a.emitEnd, emitTokenStart = _a.emitTokenStart, emitTokenEnd = _a.emitTokenEnd, changeEmitSourcePos = _a.changeEmitSourcePos, stopOverridingSpan = _a.stopOverridingSpan, getText = _a.getText, getSourceMappingURL = _a.getSourceMappingURL; - return { - initialize: initialize, - reset: reset, - getSourceMapData: getSourceMapData, - setSourceFile: setSourceFile, - emitPos: function (pos) { - ts.performance.mark("sourcemapStart"); - emitPos(pos); - ts.performance.measure("sourceMapTime", "sourcemapStart"); - }, - emitStart: function (range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { - ts.performance.mark("emitSourcemap:emitStart"); - emitStart(range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback); - ts.performance.measure("sourceMapTime", "emitSourcemap:emitStart"); - }, - emitEnd: function (range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback) { - ts.performance.mark("emitSourcemap:emitEnd"); - emitEnd(range, contextNode, ignoreNodeCallback, ignoreChildrenCallback, getTextRangeCallback); - ts.performance.measure("sourceMapTime", "emitSourcemap:emitEnd"); - }, - emitTokenStart: function (token, tokenStartPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { - ts.performance.mark("emitSourcemap:emitTokenStart"); - tokenStartPos = emitTokenStart(token, tokenStartPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback); - ts.performance.measure("sourceMapTime", "emitSourcemap:emitTokenStart"); - return tokenStartPos; - }, - emitTokenEnd: function (token, tokenEndPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback) { - ts.performance.mark("emitSourcemap:emitTokenEnd"); - tokenEndPos = emitTokenEnd(token, tokenEndPos, contextNode, ignoreTokenCallback, getTokenTextRangeCallback); - ts.performance.measure("sourceMapTime", "emitSourcemap:emitTokenEnd"); - return tokenEndPos; - }, - changeEmitSourcePos: changeEmitSourcePos, - stopOverridingSpan: stopOverridingSpan, - getText: getText, - getSourceMappingURL: getSourceMappingURL - }; - } + ts.createSourceMapWriter = createSourceMapWriter; var base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; function base64FormatEncode(inValue) { if (inValue < 64) { @@ -52457,21 +53627,23 @@ var ts; emitBodyWithDetachedComments: emitBodyWithDetachedComments, emitTrailingCommentsOfPosition: emitTrailingCommentsOfPosition }; - function emitNodeWithComments(node, emitCallback) { + function emitNodeWithComments(emitContext, node, emitCallback) { if (disabled) { - emitCallback(node); + emitCallback(emitContext, node); return; } if (node) { - var _a = node.commentRange || node, pos = _a.pos, end = _a.end; - var emitFlags = node.emitFlags; + var _a = ts.getCommentRange(node), pos = _a.pos, end = _a.end; + var emitFlags = ts.getEmitFlags(node); if ((pos < 0 && end < 0) || (pos === end)) { // Both pos and end are synthesized, so just emit the node without comments. if (emitFlags & 65536 /* NoNestedComments */) { - disableCommentsAndEmit(node, emitCallback); + disabled = true; + emitCallback(emitContext, node); + disabled = false; } else { - emitCallback(node); + emitCallback(emitContext, node); } } else { @@ -52505,10 +53677,12 @@ var ts; ts.performance.measure("commentTime", "preEmitNodeWithComment"); } if (emitFlags & 65536 /* NoNestedComments */) { - disableCommentsAndEmit(node, emitCallback); + disabled = true; + emitCallback(emitContext, node); + disabled = false; } else { - emitCallback(node); + emitCallback(emitContext, node); } if (extendedDiagnostics) { ts.performance.mark("beginEmitNodeWithComment"); @@ -52533,7 +53707,7 @@ var ts; ts.performance.mark("preEmitBodyWithDetachedComments"); } var pos = detachedRange.pos, end = detachedRange.end; - var emitFlags = node.emitFlags; + var emitFlags = ts.getEmitFlags(node); var skipLeadingComments = pos < 0 || (emitFlags & 16384 /* NoLeadingComments */) !== 0; var skipTrailingComments = disabled || end < 0 || (emitFlags & 32768 /* NoTrailingComments */) !== 0; if (!skipLeadingComments) { @@ -52542,8 +53716,10 @@ var ts; if (extendedDiagnostics) { ts.performance.measure("commentTime", "preEmitBodyWithDetachedComments"); } - if (emitFlags & 65536 /* NoNestedComments */) { - disableCommentsAndEmit(node, emitCallback); + if (emitFlags & 65536 /* NoNestedComments */ && !disabled) { + disabled = true; + emitCallback(node); + disabled = false; } else { emitCallback(node); @@ -52664,16 +53840,6 @@ var ts; currentLineMap = ts.getLineStarts(currentSourceFile); detachedCommentsInfo = undefined; } - function disableCommentsAndEmit(node, emitCallback) { - if (disabled) { - emitCallback(node); - } - else { - disabled = true; - emitCallback(node); - disabled = false; - } - } function hasDetachedComments(pos) { return detachedCommentsInfo !== undefined && ts.lastOrUndefined(detachedCommentsInfo).nodePos === pos; } @@ -52735,11 +53901,11 @@ var ts; return declarationDiagnostics.getDiagnostics(targetSourceFile ? targetSourceFile.fileName : undefined); function getDeclarationDiagnosticsFromFile(_a, sources, isBundledEmit) { var declarationFilePath = _a.declarationFilePath; - emitDeclarations(host, resolver, declarationDiagnostics, declarationFilePath, sources, isBundledEmit); + emitDeclarations(host, resolver, declarationDiagnostics, declarationFilePath, sources, isBundledEmit, /*emitOnlyDtsFiles*/ false); } } ts.getDeclarationDiagnostics = getDeclarationDiagnostics; - function emitDeclarations(host, resolver, emitterDiagnostics, declarationFilePath, sourceFiles, isBundledEmit) { + function emitDeclarations(host, resolver, emitterDiagnostics, declarationFilePath, sourceFiles, isBundledEmit, emitOnlyDtsFiles) { var newLine = host.getNewLine(); var compilerOptions = host.getCompilerOptions(); var write; @@ -52786,7 +53952,7 @@ var ts; // global file reference is added only // - if it is not bundled emit (because otherwise it would be self reference) // - and it is not already added - if (writeReferencePath(referencedFile, !isBundledEmit && !addedGlobalFileReference)) { + if (writeReferencePath(referencedFile, !isBundledEmit && !addedGlobalFileReference, emitOnlyDtsFiles)) { addedGlobalFileReference = true; } emittedReferencedFiles.push(referencedFile); @@ -52987,7 +54153,7 @@ var ts; } else { errorNameNode = declaration.name; - resolver.writeTypeOfDeclaration(declaration, enclosingDeclaration, 2 /* UseTypeOfFunction */, writer); + resolver.writeTypeOfDeclaration(declaration, enclosingDeclaration, 2 /* UseTypeOfFunction */ | 1024 /* UseTypeAliasValue */, writer); errorNameNode = undefined; } } @@ -53000,7 +54166,7 @@ var ts; } else { errorNameNode = signature.name; - resolver.writeReturnTypeOfSignatureDeclaration(signature, enclosingDeclaration, 2 /* UseTypeOfFunction */, writer); + resolver.writeReturnTypeOfSignatureDeclaration(signature, enclosingDeclaration, 2 /* UseTypeOfFunction */ | 1024 /* UseTypeAliasValue */, writer); errorNameNode = undefined; } } @@ -53202,7 +54368,7 @@ var ts; write(tempVarName); write(": "); writer.getSymbolAccessibilityDiagnostic = getDefaultExportAccessibilityDiagnostic; - resolver.writeTypeOfExpression(node.expression, enclosingDeclaration, 2 /* UseTypeOfFunction */, writer); + resolver.writeTypeOfExpression(node.expression, enclosingDeclaration, 2 /* UseTypeOfFunction */ | 1024 /* UseTypeAliasValue */, writer); write(";"); writeLine(); write(node.isExportEquals ? "export = " : "export default "); @@ -53624,7 +54790,7 @@ var ts; } else { writer.getSymbolAccessibilityDiagnostic = getHeritageClauseVisibilityError; - resolver.writeBaseConstructorTypeOfClass(enclosingDeclaration, enclosingDeclaration, 2 /* UseTypeOfFunction */, writer); + resolver.writeBaseConstructorTypeOfClass(enclosingDeclaration, enclosingDeclaration, 2 /* UseTypeOfFunction */ | 1024 /* UseTypeAliasValue */, writer); } function getHeritageClauseVisibilityError(symbolAccessibilityResult) { var diagnosticMessage; @@ -53772,3801 +54938,3064 @@ var ts; } } } - function getVariableDeclarationTypeVisibilityError(symbolAccessibilityResult) { - var diagnosticMessage = getVariableDeclarationTypeVisibilityDiagnosticMessage(symbolAccessibilityResult); - return diagnosticMessage !== undefined ? { - diagnosticMessage: diagnosticMessage, - errorNode: node, - typeName: node.name - } : undefined; - } - function emitBindingPattern(bindingPattern) { - // Only select non-omitted expression from the bindingPattern's elements. - // We have to do this to avoid emitting trailing commas. - // For example: - // original: var [, c,,] = [ 2,3,4] - // emitted: declare var c: number; // instead of declare var c:number, ; - var elements = []; - for (var _i = 0, _a = bindingPattern.elements; _i < _a.length; _i++) { - var element = _a[_i]; - if (element.kind !== 193 /* OmittedExpression */) { - elements.push(element); - } - } - emitCommaList(elements, emitBindingElement); - } - function emitBindingElement(bindingElement) { - function getBindingElementTypeVisibilityError(symbolAccessibilityResult) { - var diagnosticMessage = getVariableDeclarationTypeVisibilityDiagnosticMessage(symbolAccessibilityResult); - return diagnosticMessage !== undefined ? { - diagnosticMessage: diagnosticMessage, - errorNode: bindingElement, - typeName: bindingElement.name - } : undefined; - } - if (bindingElement.name) { - if (ts.isBindingPattern(bindingElement.name)) { - emitBindingPattern(bindingElement.name); - } - else { - writeTextOfNode(currentText, bindingElement.name); - writeTypeOfDeclaration(bindingElement, /*type*/ undefined, getBindingElementTypeVisibilityError); - } - } - } - } - function emitTypeOfVariableDeclarationFromTypeLiteral(node) { - // if this is property of type literal, - // or is parameter of method/call/construct/index signature of type literal - // emit only if type is specified - if (node.type) { - write(": "); - emitType(node.type); - } - } - function isVariableStatementVisible(node) { - return ts.forEach(node.declarationList.declarations, function (varDeclaration) { return resolver.isDeclarationVisible(varDeclaration); }); - } - function writeVariableStatement(node) { - emitJsDocComments(node); - emitModuleElementDeclarationFlags(node); - if (ts.isLet(node.declarationList)) { - write("let "); - } - else if (ts.isConst(node.declarationList)) { - write("const "); - } - else { - write("var "); - } - emitCommaList(node.declarationList.declarations, emitVariableDeclaration, resolver.isDeclarationVisible); - write(";"); - writeLine(); - } - function emitAccessorDeclaration(node) { - if (ts.hasDynamicName(node)) { - return; - } - var accessors = ts.getAllAccessorDeclarations(node.parent.members, node); - var accessorWithTypeAnnotation; - if (node === accessors.firstAccessor) { - emitJsDocComments(accessors.getAccessor); - emitJsDocComments(accessors.setAccessor); - emitClassMemberDeclarationFlags(ts.getModifierFlags(node) | (accessors.setAccessor ? 0 : 64 /* Readonly */)); - writeTextOfNode(currentText, node.name); - if (!ts.hasModifier(node, 8 /* Private */)) { - accessorWithTypeAnnotation = node; - var type = getTypeAnnotationFromAccessor(node); - if (!type) { - // couldn't get type for the first accessor, try the another one - var anotherAccessor = node.kind === 149 /* GetAccessor */ ? accessors.setAccessor : accessors.getAccessor; - type = getTypeAnnotationFromAccessor(anotherAccessor); - if (type) { - accessorWithTypeAnnotation = anotherAccessor; - } - } - writeTypeOfDeclaration(node, type, getAccessorDeclarationTypeVisibilityError); - } - write(";"); - writeLine(); - } - function getTypeAnnotationFromAccessor(accessor) { - if (accessor) { - return accessor.kind === 149 /* GetAccessor */ - ? accessor.type // Getter - return type - : accessor.parameters.length > 0 - ? accessor.parameters[0].type // Setter parameter type - : undefined; - } - } - function getAccessorDeclarationTypeVisibilityError(symbolAccessibilityResult) { - var diagnosticMessage; - if (accessorWithTypeAnnotation.kind === 150 /* SetAccessor */) { - // Setters have to have type named and cannot infer it so, the type should always be named - if (ts.hasModifier(accessorWithTypeAnnotation.parent, 32 /* Static */)) { - diagnosticMessage = symbolAccessibilityResult.errorModuleName ? - ts.Diagnostics.Parameter_0_of_public_static_property_setter_from_exported_class_has_or_is_using_name_1_from_private_module_2 : - ts.Diagnostics.Parameter_0_of_public_static_property_setter_from_exported_class_has_or_is_using_private_name_1; - } - else { - diagnosticMessage = symbolAccessibilityResult.errorModuleName ? - ts.Diagnostics.Parameter_0_of_public_property_setter_from_exported_class_has_or_is_using_name_1_from_private_module_2 : - ts.Diagnostics.Parameter_0_of_public_property_setter_from_exported_class_has_or_is_using_private_name_1; - } - return { - diagnosticMessage: diagnosticMessage, - errorNode: accessorWithTypeAnnotation.parameters[0], - // TODO(jfreeman): Investigate why we are passing node.name instead of node.parameters[0].name - typeName: accessorWithTypeAnnotation.name - }; - } - else { - if (ts.hasModifier(accessorWithTypeAnnotation, 32 /* Static */)) { - diagnosticMessage = symbolAccessibilityResult.errorModuleName ? - symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? - ts.Diagnostics.Return_type_of_public_static_property_getter_from_exported_class_has_or_is_using_name_0_from_external_module_1_but_cannot_be_named : - ts.Diagnostics.Return_type_of_public_static_property_getter_from_exported_class_has_or_is_using_name_0_from_private_module_1 : - ts.Diagnostics.Return_type_of_public_static_property_getter_from_exported_class_has_or_is_using_private_name_0; - } - else { - diagnosticMessage = symbolAccessibilityResult.errorModuleName ? - symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? - ts.Diagnostics.Return_type_of_public_property_getter_from_exported_class_has_or_is_using_name_0_from_external_module_1_but_cannot_be_named : - ts.Diagnostics.Return_type_of_public_property_getter_from_exported_class_has_or_is_using_name_0_from_private_module_1 : - ts.Diagnostics.Return_type_of_public_property_getter_from_exported_class_has_or_is_using_private_name_0; - } - return { - diagnosticMessage: diagnosticMessage, - errorNode: accessorWithTypeAnnotation.name, - typeName: undefined - }; - } - } - } - function writeFunctionDeclaration(node) { - if (ts.hasDynamicName(node)) { - return; - } - // If we are emitting Method/Constructor it isn't moduleElement and hence already determined to be emitting - // so no need to verify if the declaration is visible - if (!resolver.isImplementationOfOverload(node)) { - emitJsDocComments(node); - if (node.kind === 220 /* FunctionDeclaration */) { - emitModuleElementDeclarationFlags(node); - } - else if (node.kind === 147 /* MethodDeclaration */ || node.kind === 148 /* Constructor */) { - emitClassMemberDeclarationFlags(ts.getModifierFlags(node)); - } - if (node.kind === 220 /* FunctionDeclaration */) { - write("function "); - writeTextOfNode(currentText, node.name); - } - else if (node.kind === 148 /* Constructor */) { - write("constructor"); - } - else { - writeTextOfNode(currentText, node.name); - if (ts.hasQuestionToken(node)) { - write("?"); - } - } - emitSignatureDeclaration(node); - } - } - function emitSignatureDeclarationWithJsDocComments(node) { - emitJsDocComments(node); - emitSignatureDeclaration(node); - } - function emitSignatureDeclaration(node) { - var prevEnclosingDeclaration = enclosingDeclaration; - enclosingDeclaration = node; - var closeParenthesizedFunctionType = false; - if (node.kind === 153 /* IndexSignature */) { - // Index signature can have readonly modifier - emitClassMemberDeclarationFlags(ts.getModifierFlags(node)); - write("["); - } - else { - // Construct signature or constructor type write new Signature - if (node.kind === 152 /* ConstructSignature */ || node.kind === 157 /* ConstructorType */) { - write("new "); - } - else if (node.kind === 156 /* FunctionType */) { - var currentOutput = writer.getText(); - // Do not generate incorrect type when function type with type parameters is type argument - // This could happen if user used space between two '<' making it error free - // e.g var x: A< (a: Tany)=>Tany>; - if (node.typeParameters && currentOutput.charAt(currentOutput.length - 1) === "<") { - closeParenthesizedFunctionType = true; - write("("); - } - } - emitTypeParameters(node.typeParameters); - write("("); - } - // Parameters - emitCommaList(node.parameters, emitParameterDeclaration); - if (node.kind === 153 /* IndexSignature */) { - write("]"); - } - else { - write(")"); - } - // If this is not a constructor and is not private, emit the return type - var isFunctionTypeOrConstructorType = node.kind === 156 /* FunctionType */ || node.kind === 157 /* ConstructorType */; - if (isFunctionTypeOrConstructorType || node.parent.kind === 159 /* TypeLiteral */) { - // Emit type literal signature return type only if specified - if (node.type) { - write(isFunctionTypeOrConstructorType ? " => " : ": "); - emitType(node.type); - } - } - else if (node.kind !== 148 /* Constructor */ && !ts.hasModifier(node, 8 /* Private */)) { - writeReturnTypeAtSignature(node, getReturnTypeVisibilityError); - } - enclosingDeclaration = prevEnclosingDeclaration; - if (!isFunctionTypeOrConstructorType) { - write(";"); - writeLine(); - } - else if (closeParenthesizedFunctionType) { - write(")"); - } - function getReturnTypeVisibilityError(symbolAccessibilityResult) { - var diagnosticMessage; - switch (node.kind) { - case 152 /* ConstructSignature */: - // Interfaces cannot have return types that cannot be named - diagnosticMessage = symbolAccessibilityResult.errorModuleName ? - ts.Diagnostics.Return_type_of_constructor_signature_from_exported_interface_has_or_is_using_name_0_from_private_module_1 : - ts.Diagnostics.Return_type_of_constructor_signature_from_exported_interface_has_or_is_using_private_name_0; - break; - case 151 /* CallSignature */: - // Interfaces cannot have return types that cannot be named - diagnosticMessage = symbolAccessibilityResult.errorModuleName ? - ts.Diagnostics.Return_type_of_call_signature_from_exported_interface_has_or_is_using_name_0_from_private_module_1 : - ts.Diagnostics.Return_type_of_call_signature_from_exported_interface_has_or_is_using_private_name_0; - break; - case 153 /* IndexSignature */: - // Interfaces cannot have return types that cannot be named - diagnosticMessage = symbolAccessibilityResult.errorModuleName ? - ts.Diagnostics.Return_type_of_index_signature_from_exported_interface_has_or_is_using_name_0_from_private_module_1 : - ts.Diagnostics.Return_type_of_index_signature_from_exported_interface_has_or_is_using_private_name_0; - break; - case 147 /* MethodDeclaration */: - case 146 /* MethodSignature */: - if (ts.hasModifier(node, 32 /* Static */)) { - diagnosticMessage = symbolAccessibilityResult.errorModuleName ? - symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? - ts.Diagnostics.Return_type_of_public_static_method_from_exported_class_has_or_is_using_name_0_from_external_module_1_but_cannot_be_named : - ts.Diagnostics.Return_type_of_public_static_method_from_exported_class_has_or_is_using_name_0_from_private_module_1 : - ts.Diagnostics.Return_type_of_public_static_method_from_exported_class_has_or_is_using_private_name_0; - } - else if (node.parent.kind === 221 /* ClassDeclaration */) { - diagnosticMessage = symbolAccessibilityResult.errorModuleName ? - symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? - ts.Diagnostics.Return_type_of_public_method_from_exported_class_has_or_is_using_name_0_from_external_module_1_but_cannot_be_named : - ts.Diagnostics.Return_type_of_public_method_from_exported_class_has_or_is_using_name_0_from_private_module_1 : - ts.Diagnostics.Return_type_of_public_method_from_exported_class_has_or_is_using_private_name_0; - } - else { - // Interfaces cannot have return types that cannot be named - diagnosticMessage = symbolAccessibilityResult.errorModuleName ? - ts.Diagnostics.Return_type_of_method_from_exported_interface_has_or_is_using_name_0_from_private_module_1 : - ts.Diagnostics.Return_type_of_method_from_exported_interface_has_or_is_using_private_name_0; - } - break; - case 220 /* FunctionDeclaration */: - diagnosticMessage = symbolAccessibilityResult.errorModuleName ? - symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? - ts.Diagnostics.Return_type_of_exported_function_has_or_is_using_name_0_from_external_module_1_but_cannot_be_named : - ts.Diagnostics.Return_type_of_exported_function_has_or_is_using_name_0_from_private_module_1 : - ts.Diagnostics.Return_type_of_exported_function_has_or_is_using_private_name_0; - break; - default: - ts.Debug.fail("This is unknown kind for signature: " + node.kind); - } - return { - diagnosticMessage: diagnosticMessage, - errorNode: node.name || node - }; - } - } - function emitParameterDeclaration(node) { - increaseIndent(); - emitJsDocComments(node); - if (node.dotDotDotToken) { - write("..."); - } - if (ts.isBindingPattern(node.name)) { - // For bindingPattern, we can't simply writeTextOfNode from the source file - // because we want to omit the initializer and using writeTextOfNode will result in initializer get emitted. - // Therefore, we will have to recursively emit each element in the bindingPattern. - emitBindingPattern(node.name); - } - else { - writeTextOfNode(currentText, node.name); - } - if (resolver.isOptionalParameter(node)) { - write("?"); - } - decreaseIndent(); - if (node.parent.kind === 156 /* FunctionType */ || - node.parent.kind === 157 /* ConstructorType */ || - node.parent.parent.kind === 159 /* TypeLiteral */) { - emitTypeOfVariableDeclarationFromTypeLiteral(node); - } - else if (!ts.hasModifier(node.parent, 8 /* Private */)) { - writeTypeOfDeclaration(node, node.type, getParameterDeclarationTypeVisibilityError); - } - function getParameterDeclarationTypeVisibilityError(symbolAccessibilityResult) { - var diagnosticMessage = getParameterDeclarationTypeVisibilityDiagnosticMessage(symbolAccessibilityResult); - return diagnosticMessage !== undefined ? { - diagnosticMessage: diagnosticMessage, - errorNode: node, - typeName: node.name - } : undefined; - } - function getParameterDeclarationTypeVisibilityDiagnosticMessage(symbolAccessibilityResult) { - switch (node.parent.kind) { - case 148 /* Constructor */: - return symbolAccessibilityResult.errorModuleName ? - symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? - ts.Diagnostics.Parameter_0_of_constructor_from_exported_class_has_or_is_using_name_1_from_external_module_2_but_cannot_be_named : - ts.Diagnostics.Parameter_0_of_constructor_from_exported_class_has_or_is_using_name_1_from_private_module_2 : - ts.Diagnostics.Parameter_0_of_constructor_from_exported_class_has_or_is_using_private_name_1; - case 152 /* ConstructSignature */: - // Interfaces cannot have parameter types that cannot be named - return symbolAccessibilityResult.errorModuleName ? - ts.Diagnostics.Parameter_0_of_constructor_signature_from_exported_interface_has_or_is_using_name_1_from_private_module_2 : - ts.Diagnostics.Parameter_0_of_constructor_signature_from_exported_interface_has_or_is_using_private_name_1; - case 151 /* CallSignature */: - // Interfaces cannot have parameter types that cannot be named - return symbolAccessibilityResult.errorModuleName ? - ts.Diagnostics.Parameter_0_of_call_signature_from_exported_interface_has_or_is_using_name_1_from_private_module_2 : - ts.Diagnostics.Parameter_0_of_call_signature_from_exported_interface_has_or_is_using_private_name_1; - case 147 /* MethodDeclaration */: - case 146 /* MethodSignature */: - if (ts.hasModifier(node.parent, 32 /* Static */)) { - return symbolAccessibilityResult.errorModuleName ? - symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? - ts.Diagnostics.Parameter_0_of_public_static_method_from_exported_class_has_or_is_using_name_1_from_external_module_2_but_cannot_be_named : - ts.Diagnostics.Parameter_0_of_public_static_method_from_exported_class_has_or_is_using_name_1_from_private_module_2 : - ts.Diagnostics.Parameter_0_of_public_static_method_from_exported_class_has_or_is_using_private_name_1; - } - else if (node.parent.parent.kind === 221 /* ClassDeclaration */) { - return symbolAccessibilityResult.errorModuleName ? - symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? - ts.Diagnostics.Parameter_0_of_public_method_from_exported_class_has_or_is_using_name_1_from_external_module_2_but_cannot_be_named : - ts.Diagnostics.Parameter_0_of_public_method_from_exported_class_has_or_is_using_name_1_from_private_module_2 : - ts.Diagnostics.Parameter_0_of_public_method_from_exported_class_has_or_is_using_private_name_1; - } - else { - // Interfaces cannot have parameter types that cannot be named - return symbolAccessibilityResult.errorModuleName ? - ts.Diagnostics.Parameter_0_of_method_from_exported_interface_has_or_is_using_name_1_from_private_module_2 : - ts.Diagnostics.Parameter_0_of_method_from_exported_interface_has_or_is_using_private_name_1; - } - case 220 /* FunctionDeclaration */: - return symbolAccessibilityResult.errorModuleName ? - symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? - ts.Diagnostics.Parameter_0_of_exported_function_has_or_is_using_name_1_from_external_module_2_but_cannot_be_named : - ts.Diagnostics.Parameter_0_of_exported_function_has_or_is_using_name_1_from_private_module_2 : - ts.Diagnostics.Parameter_0_of_exported_function_has_or_is_using_private_name_1; - default: - ts.Debug.fail("This is unknown parent for parameter: " + node.parent.kind); - } + function getVariableDeclarationTypeVisibilityError(symbolAccessibilityResult) { + var diagnosticMessage = getVariableDeclarationTypeVisibilityDiagnosticMessage(symbolAccessibilityResult); + return diagnosticMessage !== undefined ? { + diagnosticMessage: diagnosticMessage, + errorNode: node, + typeName: node.name + } : undefined; } function emitBindingPattern(bindingPattern) { - // We have to explicitly emit square bracket and bracket because these tokens are not store inside the node. - if (bindingPattern.kind === 167 /* ObjectBindingPattern */) { - write("{"); - emitCommaList(bindingPattern.elements, emitBindingElement); - write("}"); - } - else if (bindingPattern.kind === 168 /* ArrayBindingPattern */) { - write("["); - var elements = bindingPattern.elements; - emitCommaList(elements, emitBindingElement); - if (elements && elements.hasTrailingComma) { - write(", "); + // Only select non-omitted expression from the bindingPattern's elements. + // We have to do this to avoid emitting trailing commas. + // For example: + // original: var [, c,,] = [ 2,3,4] + // emitted: declare var c: number; // instead of declare var c:number, ; + var elements = []; + for (var _i = 0, _a = bindingPattern.elements; _i < _a.length; _i++) { + var element = _a[_i]; + if (element.kind !== 193 /* OmittedExpression */) { + elements.push(element); } - write("]"); } + emitCommaList(elements, emitBindingElement); } function emitBindingElement(bindingElement) { - if (bindingElement.kind === 193 /* OmittedExpression */) { - // If bindingElement is an omittedExpression (i.e. containing elision), - // we will emit blank space (although this may differ from users' original code, - // it allows emitSeparatedList to write separator appropriately) - // Example: - // original: function foo([, x, ,]) {} - // emit : function foo([ , x, , ]) {} - write(" "); + function getBindingElementTypeVisibilityError(symbolAccessibilityResult) { + var diagnosticMessage = getVariableDeclarationTypeVisibilityDiagnosticMessage(symbolAccessibilityResult); + return diagnosticMessage !== undefined ? { + diagnosticMessage: diagnosticMessage, + errorNode: bindingElement, + typeName: bindingElement.name + } : undefined; } - else if (bindingElement.kind === 169 /* BindingElement */) { - if (bindingElement.propertyName) { - // bindingElement has propertyName property in the following case: - // { y: [a,b,c] ...} -> bindingPattern will have a property called propertyName for "y" - // We have to explicitly emit the propertyName before descending into its binding elements. - // Example: - // original: function foo({y: [a,b,c]}) {} - // emit : declare function foo({y: [a, b, c]}: { y: [any, any, any] }) void; - writeTextOfNode(currentText, bindingElement.propertyName); - write(": "); + if (bindingElement.name) { + if (ts.isBindingPattern(bindingElement.name)) { + emitBindingPattern(bindingElement.name); } - if (bindingElement.name) { - if (ts.isBindingPattern(bindingElement.name)) { - // If it is a nested binding pattern, we will recursively descend into each element and emit each one separately. - // In the case of rest element, we will omit rest element. - // Example: - // original: function foo([a, [[b]], c] = [1,[["string"]], 3]) {} - // emit : declare function foo([a, [[b]], c]: [number, [[string]], number]): void; - // original with rest: function foo([a, ...c]) {} - // emit : declare function foo([a, ...c]): void; - emitBindingPattern(bindingElement.name); - } - else { - ts.Debug.assert(bindingElement.name.kind === 69 /* Identifier */); - // If the node is just an identifier, we will simply emit the text associated with the node's name - // Example: - // original: function foo({y = 10, x}) {} - // emit : declare function foo({y, x}: {number, any}): void; - if (bindingElement.dotDotDotToken) { - write("..."); - } - writeTextOfNode(currentText, bindingElement.name); - } + else { + writeTextOfNode(currentText, bindingElement.name); + writeTypeOfDeclaration(bindingElement, /*type*/ undefined, getBindingElementTypeVisibilityError); } } } } - function emitNode(node) { - switch (node.kind) { - case 220 /* FunctionDeclaration */: - case 225 /* ModuleDeclaration */: - case 229 /* ImportEqualsDeclaration */: - case 222 /* InterfaceDeclaration */: - case 221 /* ClassDeclaration */: - case 223 /* TypeAliasDeclaration */: - case 224 /* EnumDeclaration */: - return emitModuleElement(node, isModuleElementVisible(node)); - case 200 /* VariableStatement */: - return emitModuleElement(node, isVariableStatementVisible(node)); - case 230 /* ImportDeclaration */: - // Import declaration without import clause is visible, otherwise it is not visible - return emitModuleElement(node, /*isModuleElementVisible*/ !node.importClause); - case 236 /* ExportDeclaration */: - return emitExportDeclaration(node); - case 148 /* Constructor */: - case 147 /* MethodDeclaration */: - case 146 /* MethodSignature */: - return writeFunctionDeclaration(node); - case 152 /* ConstructSignature */: - case 151 /* CallSignature */: - case 153 /* IndexSignature */: - return emitSignatureDeclarationWithJsDocComments(node); - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - return emitAccessorDeclaration(node); - case 145 /* PropertyDeclaration */: - case 144 /* PropertySignature */: - return emitPropertyDeclaration(node); - case 255 /* EnumMember */: - return emitEnumMemberDeclaration(node); - case 235 /* ExportAssignment */: - return emitExportAssignment(node); - case 256 /* SourceFile */: - return emitSourceFile(node); + function emitTypeOfVariableDeclarationFromTypeLiteral(node) { + // if this is property of type literal, + // or is parameter of method/call/construct/index signature of type literal + // emit only if type is specified + if (node.type) { + write(": "); + emitType(node.type); } } - /** - * Adds the reference to referenced file, returns true if global file reference was emitted - * @param referencedFile - * @param addBundledFileReference Determines if global file reference corresponding to bundled file should be emitted or not - */ - function writeReferencePath(referencedFile, addBundledFileReference) { - var declFileName; - var addedBundledEmitReference = false; - if (ts.isDeclarationFile(referencedFile)) { - // Declaration file, use declaration file name - declFileName = referencedFile.fileName; + function isVariableStatementVisible(node) { + return ts.forEach(node.declarationList.declarations, function (varDeclaration) { return resolver.isDeclarationVisible(varDeclaration); }); + } + function writeVariableStatement(node) { + emitJsDocComments(node); + emitModuleElementDeclarationFlags(node); + if (ts.isLet(node.declarationList)) { + write("let "); + } + else if (ts.isConst(node.declarationList)) { + write("const "); } else { - // Get the declaration file path - ts.forEachExpectedEmitFile(host, getDeclFileName, referencedFile); + write("var "); } - if (declFileName) { - declFileName = ts.getRelativePathToDirectoryOrUrl(ts.getDirectoryPath(ts.normalizeSlashes(declarationFilePath)), declFileName, host.getCurrentDirectory(), host.getCanonicalFileName, - /*isAbsolutePathAnUrl*/ false); - referencesOutput += "/// " + newLine; + emitCommaList(node.declarationList.declarations, emitVariableDeclaration, resolver.isDeclarationVisible); + write(";"); + writeLine(); + } + function emitAccessorDeclaration(node) { + if (ts.hasDynamicName(node)) { + return; } - return addedBundledEmitReference; - function getDeclFileName(emitFileNames, sourceFiles, isBundledEmit) { - // Dont add reference path to this file if it is a bundled emit and caller asked not emit bundled file path - if (isBundledEmit && !addBundledFileReference) { - return; + var accessors = ts.getAllAccessorDeclarations(node.parent.members, node); + var accessorWithTypeAnnotation; + if (node === accessors.firstAccessor) { + emitJsDocComments(accessors.getAccessor); + emitJsDocComments(accessors.setAccessor); + emitClassMemberDeclarationFlags(ts.getModifierFlags(node) | (accessors.setAccessor ? 0 : 64 /* Readonly */)); + writeTextOfNode(currentText, node.name); + if (!ts.hasModifier(node, 8 /* Private */)) { + accessorWithTypeAnnotation = node; + var type = getTypeAnnotationFromAccessor(node); + if (!type) { + // couldn't get type for the first accessor, try the another one + var anotherAccessor = node.kind === 149 /* GetAccessor */ ? accessors.setAccessor : accessors.getAccessor; + type = getTypeAnnotationFromAccessor(anotherAccessor); + if (type) { + accessorWithTypeAnnotation = anotherAccessor; + } + } + writeTypeOfDeclaration(node, type, getAccessorDeclarationTypeVisibilityError); } - ts.Debug.assert(!!emitFileNames.declarationFilePath || ts.isSourceFileJavaScript(referencedFile), "Declaration file is not present only for javascript files"); - declFileName = emitFileNames.declarationFilePath || emitFileNames.jsFilePath; - addedBundledEmitReference = isBundledEmit; + write(";"); + writeLine(); } - } - } - /* @internal */ - function writeDeclarationFile(declarationFilePath, sourceFiles, isBundledEmit, host, resolver, emitterDiagnostics) { - var emitDeclarationResult = emitDeclarations(host, resolver, emitterDiagnostics, declarationFilePath, sourceFiles, isBundledEmit); - var emitSkipped = emitDeclarationResult.reportedDeclarationError || host.isEmitBlocked(declarationFilePath) || host.getCompilerOptions().noEmit; - if (!emitSkipped) { - var declarationOutput = emitDeclarationResult.referencesOutput - + getDeclarationOutput(emitDeclarationResult.synchronousDeclarationOutput, emitDeclarationResult.moduleElementDeclarationEmitInfo); - ts.writeFile(host, emitterDiagnostics, declarationFilePath, declarationOutput, host.getCompilerOptions().emitBOM, sourceFiles); - } - return emitSkipped; - function getDeclarationOutput(synchronousDeclarationOutput, moduleElementDeclarationEmitInfo) { - var appliedSyncOutputPos = 0; - var declarationOutput = ""; - // apply asynchronous additions to the synchronous output - ts.forEach(moduleElementDeclarationEmitInfo, function (aliasEmitInfo) { - if (aliasEmitInfo.asynchronousOutput) { - declarationOutput += synchronousDeclarationOutput.substring(appliedSyncOutputPos, aliasEmitInfo.outputPos); - declarationOutput += getDeclarationOutput(aliasEmitInfo.asynchronousOutput, aliasEmitInfo.subModuleElementDeclarationEmitInfo); - appliedSyncOutputPos = aliasEmitInfo.outputPos; + function getTypeAnnotationFromAccessor(accessor) { + if (accessor) { + return accessor.kind === 149 /* GetAccessor */ + ? accessor.type // Getter - return type + : accessor.parameters.length > 0 + ? accessor.parameters[0].type // Setter parameter type + : undefined; } - }); - declarationOutput += synchronousDeclarationOutput.substring(appliedSyncOutputPos); - return declarationOutput; - } - } - ts.writeDeclarationFile = writeDeclarationFile; -})(ts || (ts = {})); -/// -/// -/// -/// -/// -/* @internal */ -var ts; -(function (ts) { - // Flags enum to track count of temp variables and a few dedicated names - var TempFlags; - (function (TempFlags) { - TempFlags[TempFlags["Auto"] = 0] = "Auto"; - TempFlags[TempFlags["CountMask"] = 268435455] = "CountMask"; - TempFlags[TempFlags["_i"] = 268435456] = "_i"; - })(TempFlags || (TempFlags = {})); - // targetSourceFile is when users only want one file in entire project to be emitted. This is used in compileOnSave feature - function emitFiles(resolver, host, targetSourceFile) { - var delimiters = createDelimiterMap(); - var brackets = createBracketsMap(); - // emit output for the __extends helper function - var extendsHelper = "\nvar __extends = (this && this.__extends) || function (d, b) {\n for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];\n function __() { this.constructor = d; }\n d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\n};"; - // Emit output for the __assign helper function. - // This is typically used for JSX spread attributes, - // and can be used for object literal spread properties. - var assignHelper = "\nvar __assign = (this && this.__assign) || Object.assign || function(t) {\n for (var s, i = 1, n = arguments.length; i < n; i++) {\n s = arguments[i];\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))\n t[p] = s[p];\n }\n return t;\n};"; - // emit output for the __decorate helper function - var decorateHelper = "\nvar __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {\n var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;\n if (typeof Reflect === \"object\" && typeof Reflect.decorate === \"function\") r = Reflect.decorate(decorators, target, key, desc);\n else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;\n return c > 3 && r && Object.defineProperty(target, key, r), r;\n};"; - // emit output for the __metadata helper function - var metadataHelper = "\nvar __metadata = (this && this.__metadata) || function (k, v) {\n if (typeof Reflect === \"object\" && typeof Reflect.metadata === \"function\") return Reflect.metadata(k, v);\n};"; - // emit output for the __param helper function - var paramHelper = "\nvar __param = (this && this.__param) || function (paramIndex, decorator) {\n return function (target, key) { decorator(target, key, paramIndex); }\n};"; - // emit output for the __awaiter helper function - var awaiterHelper = "\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments)).next());\n });\n};"; - // The __generator helper is used by down-level transformations to emulate the runtime - // semantics of an ES2015 generator function. When called, this helper returns an - // object that implements the Iterator protocol, in that it has `next`, `return`, and - // `throw` methods that step through the generator when invoked. - // - // parameters: - // thisArg The value to use as the `this` binding for the transformed generator body. - // body A function that acts as the transformed generator body. - // - // variables: - // _ Persistent state for the generator that is shared between the helper and the - // generator body. The state object has the following members: - // sent() - A method that returns or throws the current completion value. - // label - The next point at which to resume evaluation of the generator body. - // trys - A stack of protected regions (try/catch/finally blocks). - // ops - A stack of pending instructions when inside of a finally block. - // f A value indicating whether the generator is executing. - // y An iterator to delegate for a yield*. - // t A temporary variable that holds one of the following values (note that these - // cases do not overlap): - // - The completion value when resuming from a `yield` or `yield*`. - // - The error value for a catch block. - // - The current protected region (array of try/catch/finally/end labels). - // - The verb (`next`, `throw`, or `return` method) to delegate to the expression - // of a `yield*`. - // - The result of evaluating the verb delegated to the expression of a `yield*`. - // - // functions: - // verb(n) Creates a bound callback to the `step` function for opcode `n`. - // step(op) Evaluates opcodes in a generator body until execution is suspended or - // completed. - // - // The __generator helper understands a limited set of instructions: - // 0: next(value?) - Start or resume the generator with the specified value. - // 1: throw(error) - Resume the generator with an exception. If the generator is - // suspended inside of one or more protected regions, evaluates - // any intervening finally blocks between the current label and - // the nearest catch block or function boundary. If uncaught, the - // exception is thrown to the caller. - // 2: return(value?) - Resume the generator as if with a return. If the generator is - // suspended inside of one or more protected regions, evaluates any - // intervening finally blocks. - // 3: break(label) - Jump to the specified label. If the label is outside of the - // current protected region, evaluates any intervening finally - // blocks. - // 4: yield(value?) - Yield execution to the caller with an optional value. When - // resumed, the generator will continue at the next label. - // 5: yield*(value) - Delegates evaluation to the supplied iterator. When - // delegation completes, the generator will continue at the next - // label. - // 6: catch(error) - Handles an exception thrown from within the generator body. If - // the current label is inside of one or more protected regions, - // evaluates any intervening finally blocks between the current - // label and the nearest catch block or function boundary. If - // uncaught, the exception is thrown to the caller. - // 7: endfinally - Ends a finally block, resuming the last instruction prior to - // entering a finally block. - // - // For examples of how these are used, see the comments in ./transformers/generators.ts - var generatorHelper = "\nvar __generator = (this && this.__generator) || function (thisArg, body) {\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t;\n return { next: verb(0), \"throw\": verb(1), \"return\": verb(2) };\n function verb(n) { return function (v) { return step([n, v]); }; }\n function step(op) {\n if (f) throw new TypeError(\"Generator is already executing.\");\n while (_) try {\n if (f = 1, y && (t = y[op[0] & 2 ? \"return\" : op[0] ? \"throw\" : \"next\"]) && !(t = t.call(y, op[1])).done) return t;\n if (y = 0, t) op = [0, t.value];\n switch (op[0]) {\n case 0: case 1: t = op; break;\n case 4: _.label++; return { value: op[1], done: false };\n case 5: _.label++; y = op[1]; op = [0]; continue;\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\n default:\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\n if (t[2]) _.ops.pop();\n _.trys.pop(); continue;\n }\n op = body.call(thisArg, _);\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\n }\n};"; - // emit output for the __export helper function - var exportStarHelper = "\nfunction __export(m) {\n for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];\n}"; - // emit output for the UMD helper function. - var umdHelper = "\n(function (dependencies, factory) {\n if (typeof module === 'object' && typeof module.exports === 'object') {\n var v = factory(require, exports); if (v !== undefined) module.exports = v;\n }\n else if (typeof define === 'function' && define.amd) {\n define(dependencies, factory);\n }\n})"; - var superHelper = "\nconst _super = name => super[name];"; - var advancedSuperHelper = "\nconst _super = (function (geti, seti) {\n const cache = Object.create(null);\n return name => cache[name] || (cache[name] = { get value() { return geti(name); }, set value(v) { seti(name, v); } });\n})(name => super[name], (name, value) => super[name] = value);"; - var compilerOptions = host.getCompilerOptions(); - var languageVersion = ts.getEmitScriptTarget(compilerOptions); - var moduleKind = ts.getEmitModuleKind(compilerOptions); - var sourceMapDataList = compilerOptions.sourceMap || compilerOptions.inlineSourceMap ? [] : undefined; - var emittedFilesList = compilerOptions.listEmittedFiles ? [] : undefined; - var emitterDiagnostics = ts.createDiagnosticCollection(); - var newLine = host.getNewLine(); - var transformers = ts.getTransformers(compilerOptions); - var writer = ts.createTextWriter(newLine); - var write = writer.write, writeLine = writer.writeLine, increaseIndent = writer.increaseIndent, decreaseIndent = writer.decreaseIndent; - var sourceMap = ts.createSourceMapWriter(host, writer); - var emitStart = sourceMap.emitStart, emitEnd = sourceMap.emitEnd, emitTokenStart = sourceMap.emitTokenStart, emitTokenEnd = sourceMap.emitTokenEnd; - var comments = ts.createCommentWriter(host, writer, sourceMap); - var emitNodeWithComments = comments.emitNodeWithComments, emitBodyWithDetachedComments = comments.emitBodyWithDetachedComments, emitTrailingCommentsOfPosition = comments.emitTrailingCommentsOfPosition; - var nodeIdToGeneratedName; - var autoGeneratedIdToGeneratedName; - var generatedNameSet; - var tempFlags; - var currentSourceFile; - var currentText; - var currentFileIdentifiers; - var extendsEmitted; - var assignEmitted; - var decorateEmitted; - var paramEmitted; - var awaiterEmitted; - var isOwnFileEmit; - var emitSkipped = false; - ts.performance.mark("beforeTransform"); - // Transform the source files - var transformed = ts.transformFiles(resolver, host, ts.getSourceFilesToEmit(host, targetSourceFile), transformers); - ts.performance.measure("transformTime", "beforeTransform"); - // Extract helpers from the result - var getTokenSourceMapRange = transformed.getTokenSourceMapRange, isSubstitutionEnabled = transformed.isSubstitutionEnabled, isEmitNotificationEnabled = transformed.isEmitNotificationEnabled, onSubstituteNode = transformed.onSubstituteNode, onEmitNode = transformed.onEmitNode; - ts.performance.mark("beforePrint"); - // Emit each output file - ts.forEachTransformedEmitFile(host, transformed.getSourceFiles(), emitFile); - // Clean up after transformation - transformed.dispose(); - ts.performance.measure("printTime", "beforePrint"); - return { - emitSkipped: emitSkipped, - diagnostics: emitterDiagnostics.getDiagnostics(), - emittedFiles: emittedFilesList, - sourceMaps: sourceMapDataList - }; - function emitFile(jsFilePath, sourceMapFilePath, declarationFilePath, sourceFiles, isBundledEmit) { - // Make sure not to write js file and source map file if any of them cannot be written - if (!host.isEmitBlocked(jsFilePath) && !compilerOptions.noEmit) { - printFile(jsFilePath, sourceMapFilePath, sourceFiles, isBundledEmit); - } - else { - emitSkipped = true; - } - if (declarationFilePath) { - emitSkipped = ts.writeDeclarationFile(declarationFilePath, ts.getOriginalSourceFiles(sourceFiles), isBundledEmit, host, resolver, emitterDiagnostics) || emitSkipped; } - if (!emitSkipped && emittedFilesList) { - emittedFilesList.push(jsFilePath); - if (sourceMapFilePath) { - emittedFilesList.push(sourceMapFilePath); - } - if (declarationFilePath) { - emittedFilesList.push(declarationFilePath); + function getAccessorDeclarationTypeVisibilityError(symbolAccessibilityResult) { + var diagnosticMessage; + if (accessorWithTypeAnnotation.kind === 150 /* SetAccessor */) { + // Setters have to have type named and cannot infer it so, the type should always be named + if (ts.hasModifier(accessorWithTypeAnnotation.parent, 32 /* Static */)) { + diagnosticMessage = symbolAccessibilityResult.errorModuleName ? + ts.Diagnostics.Parameter_0_of_public_static_property_setter_from_exported_class_has_or_is_using_name_1_from_private_module_2 : + ts.Diagnostics.Parameter_0_of_public_static_property_setter_from_exported_class_has_or_is_using_private_name_1; + } + else { + diagnosticMessage = symbolAccessibilityResult.errorModuleName ? + ts.Diagnostics.Parameter_0_of_public_property_setter_from_exported_class_has_or_is_using_name_1_from_private_module_2 : + ts.Diagnostics.Parameter_0_of_public_property_setter_from_exported_class_has_or_is_using_private_name_1; + } + return { + diagnosticMessage: diagnosticMessage, + errorNode: accessorWithTypeAnnotation.parameters[0], + // TODO(jfreeman): Investigate why we are passing node.name instead of node.parameters[0].name + typeName: accessorWithTypeAnnotation.name + }; } - } - } - function printFile(jsFilePath, sourceMapFilePath, sourceFiles, isBundledEmit) { - sourceMap.initialize(jsFilePath, sourceMapFilePath, sourceFiles, isBundledEmit); - nodeIdToGeneratedName = []; - autoGeneratedIdToGeneratedName = []; - generatedNameSet = ts.createMap(); - isOwnFileEmit = !isBundledEmit; - // Emit helpers from all the files - if (isBundledEmit && moduleKind) { - for (var _a = 0, sourceFiles_4 = sourceFiles; _a < sourceFiles_4.length; _a++) { - var sourceFile = sourceFiles_4[_a]; - emitEmitHelpers(sourceFile); + else { + if (ts.hasModifier(accessorWithTypeAnnotation, 32 /* Static */)) { + diagnosticMessage = symbolAccessibilityResult.errorModuleName ? + symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? + ts.Diagnostics.Return_type_of_public_static_property_getter_from_exported_class_has_or_is_using_name_0_from_external_module_1_but_cannot_be_named : + ts.Diagnostics.Return_type_of_public_static_property_getter_from_exported_class_has_or_is_using_name_0_from_private_module_1 : + ts.Diagnostics.Return_type_of_public_static_property_getter_from_exported_class_has_or_is_using_private_name_0; + } + else { + diagnosticMessage = symbolAccessibilityResult.errorModuleName ? + symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? + ts.Diagnostics.Return_type_of_public_property_getter_from_exported_class_has_or_is_using_name_0_from_external_module_1_but_cannot_be_named : + ts.Diagnostics.Return_type_of_public_property_getter_from_exported_class_has_or_is_using_name_0_from_private_module_1 : + ts.Diagnostics.Return_type_of_public_property_getter_from_exported_class_has_or_is_using_private_name_0; + } + return { + diagnosticMessage: diagnosticMessage, + errorNode: accessorWithTypeAnnotation.name, + typeName: undefined + }; } } - // Print each transformed source file. - ts.forEach(sourceFiles, printSourceFile); - writeLine(); - var sourceMappingURL = sourceMap.getSourceMappingURL(); - if (sourceMappingURL) { - write("//# " + "sourceMappingURL" + "=" + sourceMappingURL); // Sometimes tools can sometimes see this line as a source mapping url comment - } - // Write the source map - if (compilerOptions.sourceMap && !compilerOptions.inlineSourceMap) { - ts.writeFile(host, emitterDiagnostics, sourceMapFilePath, sourceMap.getText(), /*writeByteOrderMark*/ false); - } - // Record source map data for the test harness. - if (sourceMapDataList) { - sourceMapDataList.push(sourceMap.getSourceMapData()); - } - // Write the output file - ts.writeFile(host, emitterDiagnostics, jsFilePath, writer.getText(), compilerOptions.emitBOM); - // Reset state - sourceMap.reset(); - comments.reset(); - writer.reset(); - tempFlags = 0 /* Auto */; - currentSourceFile = undefined; - currentText = undefined; - extendsEmitted = false; - assignEmitted = false; - decorateEmitted = false; - paramEmitted = false; - awaiterEmitted = false; - isOwnFileEmit = false; - } - function printSourceFile(node) { - currentSourceFile = node; - currentText = node.text; - currentFileIdentifiers = node.identifiers; - sourceMap.setSourceFile(node); - comments.setSourceFile(node); - emitNodeWithNotification(node, emitWorker); - } - /** - * Emits a node. - */ - function emit(node) { - emitNodeWithNotification(node, emitWithComments); - } - /** - * Emits a node with comments. - * - * NOTE: Do not call this method directly. It is part of the emit pipeline - * and should only be called indirectly from emit. - */ - function emitWithComments(node) { - emitNodeWithComments(node, emitWithSourceMap); } - /** - * Emits a node with source maps. - * - * NOTE: Do not call this method directly. It is part of the emit pipeline - * and should only be called indirectly from emitWithComments. - */ - function emitWithSourceMap(node) { - emitNodeWithSourceMap(node, emitWorker); - } - function emitIdentifierName(node) { - if (node) { - emitNodeWithNotification(node, emitIdentifierNameWithComments); + function writeFunctionDeclaration(node) { + if (ts.hasDynamicName(node)) { + return; } - } - function emitIdentifierNameWithComments(node) { - emitNodeWithComments(node, emitWorker); - } - /** - * Emits an expression node. - */ - function emitExpression(node) { - emitNodeWithNotification(node, emitExpressionWithComments); - } - /** - * Emits an expression with comments. - * - * NOTE: Do not call this method directly. It is part of the emitExpression pipeline - * and should only be called indirectly from emitExpression. - */ - function emitExpressionWithComments(node) { - emitNodeWithComments(node, emitExpressionWithSourceMap); - } - /** - * Emits an expression with source maps. - * - * NOTE: Do not call this method directly. It is part of the emitExpression pipeline - * and should only be called indirectly from emitExpressionWithComments. - */ - function emitExpressionWithSourceMap(node) { - emitNodeWithSourceMap(node, emitExpressionWorker); - } - /** - * Emits a node with emit notification if available. - */ - function emitNodeWithNotification(node, emitCallback) { - if (node) { - if (isEmitNotificationEnabled(node)) { - onEmitNode(node, emitCallback); + // If we are emitting Method/Constructor it isn't moduleElement and hence already determined to be emitting + // so no need to verify if the declaration is visible + if (!resolver.isImplementationOfOverload(node)) { + emitJsDocComments(node); + if (node.kind === 220 /* FunctionDeclaration */) { + emitModuleElementDeclarationFlags(node); + } + else if (node.kind === 147 /* MethodDeclaration */ || node.kind === 148 /* Constructor */) { + emitClassMemberDeclarationFlags(ts.getModifierFlags(node)); + } + if (node.kind === 220 /* FunctionDeclaration */) { + write("function "); + writeTextOfNode(currentText, node.name); + } + else if (node.kind === 148 /* Constructor */) { + write("constructor"); } else { - emitCallback(node); + writeTextOfNode(currentText, node.name); + if (ts.hasQuestionToken(node)) { + write("?"); + } } + emitSignatureDeclaration(node); } } - function emitNodeWithSourceMap(node, emitCallback) { - if (node) { - emitStart(/*range*/ node, /*contextNode*/ node, shouldSkipLeadingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange); - emitCallback(node); - emitEnd(/*range*/ node, /*contextNode*/ node, shouldSkipTrailingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange); - } - } - function getSourceMapRange(node) { - return node.sourceMapRange || node; - } - /** - * Determines whether to skip leading comment emit for a node. - * - * We do not emit comments for NotEmittedStatement nodes or any node that has - * NodeEmitFlags.NoLeadingComments. - * - * @param node A Node. - */ - function shouldSkipLeadingCommentsForNode(node) { - return ts.isNotEmittedStatement(node) - || (node.emitFlags & 16384 /* NoLeadingComments */) !== 0; - } - /** - * Determines whether to skip source map emit for the start position of a node. - * - * We do not emit source maps for NotEmittedStatement nodes or any node that - * has NodeEmitFlags.NoLeadingSourceMap. - * - * @param node A Node. - */ - function shouldSkipLeadingSourceMapForNode(node) { - return ts.isNotEmittedStatement(node) - || (node.emitFlags & 512 /* NoLeadingSourceMap */) !== 0; - } - /** - * Determines whether to skip source map emit for the end position of a node. - * - * We do not emit source maps for NotEmittedStatement nodes or any node that - * has NodeEmitFlags.NoTrailingSourceMap. - * - * @param node A Node. - */ - function shouldSkipTrailingSourceMapForNode(node) { - return ts.isNotEmittedStatement(node) - || (node.emitFlags & 1024 /* NoTrailingSourceMap */) !== 0; - } - /** - * Determines whether to skip source map emit for a node and its children. - * - * We do not emit source maps for a node that has NodeEmitFlags.NoNestedSourceMaps. - */ - function shouldSkipSourceMapForChildren(node) { - return (node.emitFlags & 2048 /* NoNestedSourceMaps */) !== 0; + function emitSignatureDeclarationWithJsDocComments(node) { + emitJsDocComments(node); + emitSignatureDeclaration(node); } - function emitWorker(node) { - if (tryEmitSubstitute(node, emitWorker, /*isExpression*/ false)) { - return; + function emitSignatureDeclaration(node) { + var prevEnclosingDeclaration = enclosingDeclaration; + enclosingDeclaration = node; + var closeParenthesizedFunctionType = false; + if (node.kind === 153 /* IndexSignature */) { + // Index signature can have readonly modifier + emitClassMemberDeclarationFlags(ts.getModifierFlags(node)); + write("["); } - var kind = node.kind; - switch (kind) { - // Pseudo-literals - case 12 /* TemplateHead */: - case 13 /* TemplateMiddle */: - case 14 /* TemplateTail */: - return emitLiteral(node); - // Identifiers - case 69 /* Identifier */: - return emitIdentifier(node); - // Reserved words - case 74 /* ConstKeyword */: - case 77 /* DefaultKeyword */: - case 82 /* ExportKeyword */: - case 103 /* VoidKeyword */: - // Strict mode reserved words - case 110 /* PrivateKeyword */: - case 111 /* ProtectedKeyword */: - case 112 /* PublicKeyword */: - case 113 /* StaticKeyword */: - // Contextual keywords - case 115 /* AbstractKeyword */: - case 117 /* AnyKeyword */: - case 118 /* AsyncKeyword */: - case 120 /* BooleanKeyword */: - case 122 /* DeclareKeyword */: - case 130 /* NumberKeyword */: - case 128 /* ReadonlyKeyword */: - case 132 /* StringKeyword */: - case 133 /* SymbolKeyword */: - case 137 /* GlobalKeyword */: - return writeTokenNode(node); - // Parse tree nodes - // Names - case 139 /* QualifiedName */: - return emitQualifiedName(node); - case 140 /* ComputedPropertyName */: - return emitComputedPropertyName(node); - // Signature elements - case 141 /* TypeParameter */: - return emitTypeParameter(node); - case 142 /* Parameter */: - return emitParameter(node); - case 143 /* Decorator */: - return emitDecorator(node); - // Type members - case 144 /* PropertySignature */: - return emitPropertySignature(node); - case 145 /* PropertyDeclaration */: - return emitPropertyDeclaration(node); - case 146 /* MethodSignature */: - return emitMethodSignature(node); - case 147 /* MethodDeclaration */: - return emitMethodDeclaration(node); - case 148 /* Constructor */: - return emitConstructor(node); - case 149 /* GetAccessor */: - case 150 /* SetAccessor */: - return emitAccessorDeclaration(node); - case 151 /* CallSignature */: - return emitCallSignature(node); - case 152 /* ConstructSignature */: - return emitConstructSignature(node); - case 153 /* IndexSignature */: - return emitIndexSignature(node); - // Types - case 154 /* TypePredicate */: - return emitTypePredicate(node); - case 155 /* TypeReference */: - return emitTypeReference(node); - case 156 /* FunctionType */: - return emitFunctionType(node); - case 157 /* ConstructorType */: - return emitConstructorType(node); - case 158 /* TypeQuery */: - return emitTypeQuery(node); - case 159 /* TypeLiteral */: - return emitTypeLiteral(node); - case 160 /* ArrayType */: - return emitArrayType(node); - case 161 /* TupleType */: - return emitTupleType(node); - case 162 /* UnionType */: - return emitUnionType(node); - case 163 /* IntersectionType */: - return emitIntersectionType(node); - case 164 /* ParenthesizedType */: - return emitParenthesizedType(node); - case 194 /* ExpressionWithTypeArguments */: - return emitExpressionWithTypeArguments(node); - case 165 /* ThisType */: - return emitThisType(node); - case 166 /* LiteralType */: - return emitLiteralType(node); - // Binding patterns - case 167 /* ObjectBindingPattern */: - return emitObjectBindingPattern(node); - case 168 /* ArrayBindingPattern */: - return emitArrayBindingPattern(node); - case 169 /* BindingElement */: - return emitBindingElement(node); - // Misc - case 197 /* TemplateSpan */: - return emitTemplateSpan(node); - case 198 /* SemicolonClassElement */: - return emitSemicolonClassElement(node); - // Statements - case 199 /* Block */: - return emitBlock(node); - case 200 /* VariableStatement */: - return emitVariableStatement(node); - case 201 /* EmptyStatement */: - return emitEmptyStatement(node); - case 202 /* ExpressionStatement */: - return emitExpressionStatement(node); - case 203 /* IfStatement */: - return emitIfStatement(node); - case 204 /* DoStatement */: - return emitDoStatement(node); - case 205 /* WhileStatement */: - return emitWhileStatement(node); - case 206 /* ForStatement */: - return emitForStatement(node); - case 207 /* ForInStatement */: - return emitForInStatement(node); - case 208 /* ForOfStatement */: - return emitForOfStatement(node); - case 209 /* ContinueStatement */: - return emitContinueStatement(node); - case 210 /* BreakStatement */: - return emitBreakStatement(node); - case 211 /* ReturnStatement */: - return emitReturnStatement(node); - case 212 /* WithStatement */: - return emitWithStatement(node); - case 213 /* SwitchStatement */: - return emitSwitchStatement(node); - case 214 /* LabeledStatement */: - return emitLabeledStatement(node); - case 215 /* ThrowStatement */: - return emitThrowStatement(node); - case 216 /* TryStatement */: - return emitTryStatement(node); - case 217 /* DebuggerStatement */: - return emitDebuggerStatement(node); - // Declarations - case 218 /* VariableDeclaration */: - return emitVariableDeclaration(node); - case 219 /* VariableDeclarationList */: - return emitVariableDeclarationList(node); - case 220 /* FunctionDeclaration */: - return emitFunctionDeclaration(node); - case 221 /* ClassDeclaration */: - return emitClassDeclaration(node); - case 222 /* InterfaceDeclaration */: - return emitInterfaceDeclaration(node); - case 223 /* TypeAliasDeclaration */: - return emitTypeAliasDeclaration(node); - case 224 /* EnumDeclaration */: - return emitEnumDeclaration(node); - case 225 /* ModuleDeclaration */: - return emitModuleDeclaration(node); - case 226 /* ModuleBlock */: - return emitModuleBlock(node); - case 227 /* CaseBlock */: - return emitCaseBlock(node); - case 229 /* ImportEqualsDeclaration */: - return emitImportEqualsDeclaration(node); - case 230 /* ImportDeclaration */: - return emitImportDeclaration(node); - case 231 /* ImportClause */: - return emitImportClause(node); - case 232 /* NamespaceImport */: - return emitNamespaceImport(node); - case 233 /* NamedImports */: - return emitNamedImports(node); - case 234 /* ImportSpecifier */: - return emitImportSpecifier(node); - case 235 /* ExportAssignment */: - return emitExportAssignment(node); - case 236 /* ExportDeclaration */: - return emitExportDeclaration(node); - case 237 /* NamedExports */: - return emitNamedExports(node); - case 238 /* ExportSpecifier */: - return emitExportSpecifier(node); - case 239 /* MissingDeclaration */: - return; - // Module references - case 240 /* ExternalModuleReference */: - return emitExternalModuleReference(node); - // JSX (non-expression) - case 244 /* JsxText */: - return emitJsxText(node); - case 243 /* JsxOpeningElement */: - return emitJsxOpeningElement(node); - case 245 /* JsxClosingElement */: - return emitJsxClosingElement(node); - case 246 /* JsxAttribute */: - return emitJsxAttribute(node); - case 247 /* JsxSpreadAttribute */: - return emitJsxSpreadAttribute(node); - case 248 /* JsxExpression */: - return emitJsxExpression(node); - // Clauses - case 249 /* CaseClause */: - return emitCaseClause(node); - case 250 /* DefaultClause */: - return emitDefaultClause(node); - case 251 /* HeritageClause */: - return emitHeritageClause(node); - case 252 /* CatchClause */: - return emitCatchClause(node); - // Property assignments - case 253 /* PropertyAssignment */: - return emitPropertyAssignment(node); - case 254 /* ShorthandPropertyAssignment */: - return emitShorthandPropertyAssignment(node); - // Enum - case 255 /* EnumMember */: - return emitEnumMember(node); - // Top-level nodes - case 256 /* SourceFile */: - return emitSourceFile(node); + else { + // Construct signature or constructor type write new Signature + if (node.kind === 152 /* ConstructSignature */ || node.kind === 157 /* ConstructorType */) { + write("new "); + } + else if (node.kind === 156 /* FunctionType */) { + var currentOutput = writer.getText(); + // Do not generate incorrect type when function type with type parameters is type argument + // This could happen if user used space between two '<' making it error free + // e.g var x: A< (a: Tany)=>Tany>; + if (node.typeParameters && currentOutput.charAt(currentOutput.length - 1) === "<") { + closeParenthesizedFunctionType = true; + write("("); + } + } + emitTypeParameters(node.typeParameters); + write("("); } - if (ts.isExpression(node)) { - return emitExpressionWorker(node); + // Parameters + emitCommaList(node.parameters, emitParameterDeclaration); + if (node.kind === 153 /* IndexSignature */) { + write("]"); + } + else { + write(")"); + } + // If this is not a constructor and is not private, emit the return type + var isFunctionTypeOrConstructorType = node.kind === 156 /* FunctionType */ || node.kind === 157 /* ConstructorType */; + if (isFunctionTypeOrConstructorType || node.parent.kind === 159 /* TypeLiteral */) { + // Emit type literal signature return type only if specified + if (node.type) { + write(isFunctionTypeOrConstructorType ? " => " : ": "); + emitType(node.type); + } + } + else if (node.kind !== 148 /* Constructor */ && !ts.hasModifier(node, 8 /* Private */)) { + writeReturnTypeAtSignature(node, getReturnTypeVisibilityError); + } + enclosingDeclaration = prevEnclosingDeclaration; + if (!isFunctionTypeOrConstructorType) { + write(";"); + writeLine(); + } + else if (closeParenthesizedFunctionType) { + write(")"); + } + function getReturnTypeVisibilityError(symbolAccessibilityResult) { + var diagnosticMessage; + switch (node.kind) { + case 152 /* ConstructSignature */: + // Interfaces cannot have return types that cannot be named + diagnosticMessage = symbolAccessibilityResult.errorModuleName ? + ts.Diagnostics.Return_type_of_constructor_signature_from_exported_interface_has_or_is_using_name_0_from_private_module_1 : + ts.Diagnostics.Return_type_of_constructor_signature_from_exported_interface_has_or_is_using_private_name_0; + break; + case 151 /* CallSignature */: + // Interfaces cannot have return types that cannot be named + diagnosticMessage = symbolAccessibilityResult.errorModuleName ? + ts.Diagnostics.Return_type_of_call_signature_from_exported_interface_has_or_is_using_name_0_from_private_module_1 : + ts.Diagnostics.Return_type_of_call_signature_from_exported_interface_has_or_is_using_private_name_0; + break; + case 153 /* IndexSignature */: + // Interfaces cannot have return types that cannot be named + diagnosticMessage = symbolAccessibilityResult.errorModuleName ? + ts.Diagnostics.Return_type_of_index_signature_from_exported_interface_has_or_is_using_name_0_from_private_module_1 : + ts.Diagnostics.Return_type_of_index_signature_from_exported_interface_has_or_is_using_private_name_0; + break; + case 147 /* MethodDeclaration */: + case 146 /* MethodSignature */: + if (ts.hasModifier(node, 32 /* Static */)) { + diagnosticMessage = symbolAccessibilityResult.errorModuleName ? + symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? + ts.Diagnostics.Return_type_of_public_static_method_from_exported_class_has_or_is_using_name_0_from_external_module_1_but_cannot_be_named : + ts.Diagnostics.Return_type_of_public_static_method_from_exported_class_has_or_is_using_name_0_from_private_module_1 : + ts.Diagnostics.Return_type_of_public_static_method_from_exported_class_has_or_is_using_private_name_0; + } + else if (node.parent.kind === 221 /* ClassDeclaration */) { + diagnosticMessage = symbolAccessibilityResult.errorModuleName ? + symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? + ts.Diagnostics.Return_type_of_public_method_from_exported_class_has_or_is_using_name_0_from_external_module_1_but_cannot_be_named : + ts.Diagnostics.Return_type_of_public_method_from_exported_class_has_or_is_using_name_0_from_private_module_1 : + ts.Diagnostics.Return_type_of_public_method_from_exported_class_has_or_is_using_private_name_0; + } + else { + // Interfaces cannot have return types that cannot be named + diagnosticMessage = symbolAccessibilityResult.errorModuleName ? + ts.Diagnostics.Return_type_of_method_from_exported_interface_has_or_is_using_name_0_from_private_module_1 : + ts.Diagnostics.Return_type_of_method_from_exported_interface_has_or_is_using_private_name_0; + } + break; + case 220 /* FunctionDeclaration */: + diagnosticMessage = symbolAccessibilityResult.errorModuleName ? + symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? + ts.Diagnostics.Return_type_of_exported_function_has_or_is_using_name_0_from_external_module_1_but_cannot_be_named : + ts.Diagnostics.Return_type_of_exported_function_has_or_is_using_name_0_from_private_module_1 : + ts.Diagnostics.Return_type_of_exported_function_has_or_is_using_private_name_0; + break; + default: + ts.Debug.fail("This is unknown kind for signature: " + node.kind); + } + return { + diagnosticMessage: diagnosticMessage, + errorNode: node.name || node + }; } } - function emitExpressionWorker(node) { - if (tryEmitSubstitute(node, emitExpressionWorker, /*isExpression*/ true)) { - return; + function emitParameterDeclaration(node) { + increaseIndent(); + emitJsDocComments(node); + if (node.dotDotDotToken) { + write("..."); } - var kind = node.kind; - switch (kind) { - // Literals - case 8 /* NumericLiteral */: - return emitNumericLiteral(node); - case 9 /* StringLiteral */: - case 10 /* RegularExpressionLiteral */: - case 11 /* NoSubstitutionTemplateLiteral */: - return emitLiteral(node); - // Identifiers - case 69 /* Identifier */: - return emitIdentifier(node); - // Reserved words - case 84 /* FalseKeyword */: - case 93 /* NullKeyword */: - case 95 /* SuperKeyword */: - case 99 /* TrueKeyword */: - case 97 /* ThisKeyword */: - return writeTokenNode(node); - // Expressions - case 170 /* ArrayLiteralExpression */: - return emitArrayLiteralExpression(node); - case 171 /* ObjectLiteralExpression */: - return emitObjectLiteralExpression(node); - case 172 /* PropertyAccessExpression */: - return emitPropertyAccessExpression(node); - case 173 /* ElementAccessExpression */: - return emitElementAccessExpression(node); - case 174 /* CallExpression */: - return emitCallExpression(node); - case 175 /* NewExpression */: - return emitNewExpression(node); - case 176 /* TaggedTemplateExpression */: - return emitTaggedTemplateExpression(node); - case 177 /* TypeAssertionExpression */: - return emitTypeAssertionExpression(node); - case 178 /* ParenthesizedExpression */: - return emitParenthesizedExpression(node); - case 179 /* FunctionExpression */: - return emitFunctionExpression(node); - case 180 /* ArrowFunction */: - return emitArrowFunction(node); - case 181 /* DeleteExpression */: - return emitDeleteExpression(node); - case 182 /* TypeOfExpression */: - return emitTypeOfExpression(node); - case 183 /* VoidExpression */: - return emitVoidExpression(node); - case 184 /* AwaitExpression */: - return emitAwaitExpression(node); - case 185 /* PrefixUnaryExpression */: - return emitPrefixUnaryExpression(node); - case 186 /* PostfixUnaryExpression */: - return emitPostfixUnaryExpression(node); - case 187 /* BinaryExpression */: - return emitBinaryExpression(node); - case 188 /* ConditionalExpression */: - return emitConditionalExpression(node); - case 189 /* TemplateExpression */: - return emitTemplateExpression(node); - case 190 /* YieldExpression */: - return emitYieldExpression(node); - case 191 /* SpreadElementExpression */: - return emitSpreadElementExpression(node); - case 192 /* ClassExpression */: - return emitClassExpression(node); - case 193 /* OmittedExpression */: - return; - case 195 /* AsExpression */: - return emitAsExpression(node); - case 196 /* NonNullExpression */: - return emitNonNullExpression(node); - // JSX - case 241 /* JsxElement */: - return emitJsxElement(node); - case 242 /* JsxSelfClosingElement */: - return emitJsxSelfClosingElement(node); - // Transformation nodes - case 288 /* PartiallyEmittedExpression */: - return emitPartiallyEmittedExpression(node); + if (ts.isBindingPattern(node.name)) { + // For bindingPattern, we can't simply writeTextOfNode from the source file + // because we want to omit the initializer and using writeTextOfNode will result in initializer get emitted. + // Therefore, we will have to recursively emit each element in the bindingPattern. + emitBindingPattern(node.name); + } + else { + writeTextOfNode(currentText, node.name); + } + if (resolver.isOptionalParameter(node)) { + write("?"); + } + decreaseIndent(); + if (node.parent.kind === 156 /* FunctionType */ || + node.parent.kind === 157 /* ConstructorType */ || + node.parent.parent.kind === 159 /* TypeLiteral */) { + emitTypeOfVariableDeclarationFromTypeLiteral(node); + } + else if (!ts.hasModifier(node.parent, 8 /* Private */)) { + writeTypeOfDeclaration(node, node.type, getParameterDeclarationTypeVisibilityError); + } + function getParameterDeclarationTypeVisibilityError(symbolAccessibilityResult) { + var diagnosticMessage = getParameterDeclarationTypeVisibilityDiagnosticMessage(symbolAccessibilityResult); + return diagnosticMessage !== undefined ? { + diagnosticMessage: diagnosticMessage, + errorNode: node, + typeName: node.name + } : undefined; + } + function getParameterDeclarationTypeVisibilityDiagnosticMessage(symbolAccessibilityResult) { + switch (node.parent.kind) { + case 148 /* Constructor */: + return symbolAccessibilityResult.errorModuleName ? + symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? + ts.Diagnostics.Parameter_0_of_constructor_from_exported_class_has_or_is_using_name_1_from_external_module_2_but_cannot_be_named : + ts.Diagnostics.Parameter_0_of_constructor_from_exported_class_has_or_is_using_name_1_from_private_module_2 : + ts.Diagnostics.Parameter_0_of_constructor_from_exported_class_has_or_is_using_private_name_1; + case 152 /* ConstructSignature */: + // Interfaces cannot have parameter types that cannot be named + return symbolAccessibilityResult.errorModuleName ? + ts.Diagnostics.Parameter_0_of_constructor_signature_from_exported_interface_has_or_is_using_name_1_from_private_module_2 : + ts.Diagnostics.Parameter_0_of_constructor_signature_from_exported_interface_has_or_is_using_private_name_1; + case 151 /* CallSignature */: + // Interfaces cannot have parameter types that cannot be named + return symbolAccessibilityResult.errorModuleName ? + ts.Diagnostics.Parameter_0_of_call_signature_from_exported_interface_has_or_is_using_name_1_from_private_module_2 : + ts.Diagnostics.Parameter_0_of_call_signature_from_exported_interface_has_or_is_using_private_name_1; + case 147 /* MethodDeclaration */: + case 146 /* MethodSignature */: + if (ts.hasModifier(node.parent, 32 /* Static */)) { + return symbolAccessibilityResult.errorModuleName ? + symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? + ts.Diagnostics.Parameter_0_of_public_static_method_from_exported_class_has_or_is_using_name_1_from_external_module_2_but_cannot_be_named : + ts.Diagnostics.Parameter_0_of_public_static_method_from_exported_class_has_or_is_using_name_1_from_private_module_2 : + ts.Diagnostics.Parameter_0_of_public_static_method_from_exported_class_has_or_is_using_private_name_1; + } + else if (node.parent.parent.kind === 221 /* ClassDeclaration */) { + return symbolAccessibilityResult.errorModuleName ? + symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? + ts.Diagnostics.Parameter_0_of_public_method_from_exported_class_has_or_is_using_name_1_from_external_module_2_but_cannot_be_named : + ts.Diagnostics.Parameter_0_of_public_method_from_exported_class_has_or_is_using_name_1_from_private_module_2 : + ts.Diagnostics.Parameter_0_of_public_method_from_exported_class_has_or_is_using_private_name_1; + } + else { + // Interfaces cannot have parameter types that cannot be named + return symbolAccessibilityResult.errorModuleName ? + ts.Diagnostics.Parameter_0_of_method_from_exported_interface_has_or_is_using_name_1_from_private_module_2 : + ts.Diagnostics.Parameter_0_of_method_from_exported_interface_has_or_is_using_private_name_1; + } + case 220 /* FunctionDeclaration */: + return symbolAccessibilityResult.errorModuleName ? + symbolAccessibilityResult.accessibility === 2 /* CannotBeNamed */ ? + ts.Diagnostics.Parameter_0_of_exported_function_has_or_is_using_name_1_from_external_module_2_but_cannot_be_named : + ts.Diagnostics.Parameter_0_of_exported_function_has_or_is_using_name_1_from_private_module_2 : + ts.Diagnostics.Parameter_0_of_exported_function_has_or_is_using_private_name_1; + default: + ts.Debug.fail("This is unknown parent for parameter: " + node.parent.kind); + } } - } - // - // Literals/Pseudo-literals - // - // SyntaxKind.NumericLiteral - function emitNumericLiteral(node) { - emitLiteral(node); - if (node.trailingComment) { - write(" /*" + node.trailingComment + "*/"); + function emitBindingPattern(bindingPattern) { + // We have to explicitly emit square bracket and bracket because these tokens are not store inside the node. + if (bindingPattern.kind === 167 /* ObjectBindingPattern */) { + write("{"); + emitCommaList(bindingPattern.elements, emitBindingElement); + write("}"); + } + else if (bindingPattern.kind === 168 /* ArrayBindingPattern */) { + write("["); + var elements = bindingPattern.elements; + emitCommaList(elements, emitBindingElement); + if (elements && elements.hasTrailingComma) { + write(", "); + } + write("]"); + } } - } - // SyntaxKind.StringLiteral - // SyntaxKind.RegularExpressionLiteral - // SyntaxKind.NoSubstitutionTemplateLiteral - // SyntaxKind.TemplateHead - // SyntaxKind.TemplateMiddle - // SyntaxKind.TemplateTail - function emitLiteral(node) { - var text = getLiteralTextOfNode(node); - if ((compilerOptions.sourceMap || compilerOptions.inlineSourceMap) - && (node.kind === 9 /* StringLiteral */ || ts.isTemplateLiteralKind(node.kind))) { - writer.writeLiteral(text); + function emitBindingElement(bindingElement) { + if (bindingElement.kind === 193 /* OmittedExpression */) { + // If bindingElement is an omittedExpression (i.e. containing elision), + // we will emit blank space (although this may differ from users' original code, + // it allows emitSeparatedList to write separator appropriately) + // Example: + // original: function foo([, x, ,]) {} + // emit : function foo([ , x, , ]) {} + write(" "); + } + else if (bindingElement.kind === 169 /* BindingElement */) { + if (bindingElement.propertyName) { + // bindingElement has propertyName property in the following case: + // { y: [a,b,c] ...} -> bindingPattern will have a property called propertyName for "y" + // We have to explicitly emit the propertyName before descending into its binding elements. + // Example: + // original: function foo({y: [a,b,c]}) {} + // emit : declare function foo({y: [a, b, c]}: { y: [any, any, any] }) void; + writeTextOfNode(currentText, bindingElement.propertyName); + write(": "); + } + if (bindingElement.name) { + if (ts.isBindingPattern(bindingElement.name)) { + // If it is a nested binding pattern, we will recursively descend into each element and emit each one separately. + // In the case of rest element, we will omit rest element. + // Example: + // original: function foo([a, [[b]], c] = [1,[["string"]], 3]) {} + // emit : declare function foo([a, [[b]], c]: [number, [[string]], number]): void; + // original with rest: function foo([a, ...c]) {} + // emit : declare function foo([a, ...c]): void; + emitBindingPattern(bindingElement.name); + } + else { + ts.Debug.assert(bindingElement.name.kind === 69 /* Identifier */); + // If the node is just an identifier, we will simply emit the text associated with the node's name + // Example: + // original: function foo({y = 10, x}) {} + // emit : declare function foo({y, x}: {number, any}): void; + if (bindingElement.dotDotDotToken) { + write("..."); + } + writeTextOfNode(currentText, bindingElement.name); + } + } + } } - else { - write(text); + } + function emitNode(node) { + switch (node.kind) { + case 220 /* FunctionDeclaration */: + case 225 /* ModuleDeclaration */: + case 229 /* ImportEqualsDeclaration */: + case 222 /* InterfaceDeclaration */: + case 221 /* ClassDeclaration */: + case 223 /* TypeAliasDeclaration */: + case 224 /* EnumDeclaration */: + return emitModuleElement(node, isModuleElementVisible(node)); + case 200 /* VariableStatement */: + return emitModuleElement(node, isVariableStatementVisible(node)); + case 230 /* ImportDeclaration */: + // Import declaration without import clause is visible, otherwise it is not visible + return emitModuleElement(node, /*isModuleElementVisible*/ !node.importClause); + case 236 /* ExportDeclaration */: + return emitExportDeclaration(node); + case 148 /* Constructor */: + case 147 /* MethodDeclaration */: + case 146 /* MethodSignature */: + return writeFunctionDeclaration(node); + case 152 /* ConstructSignature */: + case 151 /* CallSignature */: + case 153 /* IndexSignature */: + return emitSignatureDeclarationWithJsDocComments(node); + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + return emitAccessorDeclaration(node); + case 145 /* PropertyDeclaration */: + case 144 /* PropertySignature */: + return emitPropertyDeclaration(node); + case 255 /* EnumMember */: + return emitEnumMemberDeclaration(node); + case 235 /* ExportAssignment */: + return emitExportAssignment(node); + case 256 /* SourceFile */: + return emitSourceFile(node); } } - // - // Identifiers - // - function emitIdentifier(node) { - if (node.emitFlags & 16 /* UMDDefine */) { - writeLines(umdHelper); + /** + * Adds the reference to referenced file, returns true if global file reference was emitted + * @param referencedFile + * @param addBundledFileReference Determines if global file reference corresponding to bundled file should be emitted or not + */ + function writeReferencePath(referencedFile, addBundledFileReference, emitOnlyDtsFiles) { + var declFileName; + var addedBundledEmitReference = false; + if (ts.isDeclarationFile(referencedFile)) { + // Declaration file, use declaration file name + declFileName = referencedFile.fileName; } else { - write(getTextOfNode(node, /*includeTrivia*/ false)); + // Get the declaration file path + ts.forEachExpectedEmitFile(host, getDeclFileName, referencedFile, emitOnlyDtsFiles); } - } - // - // Names - // - function emitQualifiedName(node) { - emitEntityName(node.left); - write("."); - emit(node.right); - } - function emitEntityName(node) { - if (node.kind === 69 /* Identifier */) { - emitExpression(node); + if (declFileName) { + declFileName = ts.getRelativePathToDirectoryOrUrl(ts.getDirectoryPath(ts.normalizeSlashes(declarationFilePath)), declFileName, host.getCurrentDirectory(), host.getCanonicalFileName, + /*isAbsolutePathAnUrl*/ false); + referencesOutput += "/// " + newLine; } - else { - emit(node); + return addedBundledEmitReference; + function getDeclFileName(emitFileNames, sourceFiles, isBundledEmit) { + // Dont add reference path to this file if it is a bundled emit and caller asked not emit bundled file path + if (isBundledEmit && !addBundledFileReference) { + return; + } + ts.Debug.assert(!!emitFileNames.declarationFilePath || ts.isSourceFileJavaScript(referencedFile), "Declaration file is not present only for javascript files"); + declFileName = emitFileNames.declarationFilePath || emitFileNames.jsFilePath; + addedBundledEmitReference = isBundledEmit; } } - function emitComputedPropertyName(node) { - write("["); - emitExpression(node.expression); - write("]"); - } - // - // Signature elements - // - function emitTypeParameter(node) { - emit(node.name); - emitWithPrefix(" extends ", node.constraint); - } - function emitParameter(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - writeIfPresent(node.dotDotDotToken, "..."); - emit(node.name); - writeIfPresent(node.questionToken, "?"); - emitExpressionWithPrefix(" = ", node.initializer); - emitWithPrefix(": ", node.type); + } + /* @internal */ + function writeDeclarationFile(declarationFilePath, sourceFiles, isBundledEmit, host, resolver, emitterDiagnostics, emitOnlyDtsFiles) { + var emitDeclarationResult = emitDeclarations(host, resolver, emitterDiagnostics, declarationFilePath, sourceFiles, isBundledEmit, emitOnlyDtsFiles); + var emitSkipped = emitDeclarationResult.reportedDeclarationError || host.isEmitBlocked(declarationFilePath) || host.getCompilerOptions().noEmit; + if (!emitSkipped) { + var declarationOutput = emitDeclarationResult.referencesOutput + + getDeclarationOutput(emitDeclarationResult.synchronousDeclarationOutput, emitDeclarationResult.moduleElementDeclarationEmitInfo); + ts.writeFile(host, emitterDiagnostics, declarationFilePath, declarationOutput, host.getCompilerOptions().emitBOM, sourceFiles); } - function emitDecorator(decorator) { - write("@"); - emitExpression(decorator.expression); + return emitSkipped; + function getDeclarationOutput(synchronousDeclarationOutput, moduleElementDeclarationEmitInfo) { + var appliedSyncOutputPos = 0; + var declarationOutput = ""; + // apply asynchronous additions to the synchronous output + ts.forEach(moduleElementDeclarationEmitInfo, function (aliasEmitInfo) { + if (aliasEmitInfo.asynchronousOutput) { + declarationOutput += synchronousDeclarationOutput.substring(appliedSyncOutputPos, aliasEmitInfo.outputPos); + declarationOutput += getDeclarationOutput(aliasEmitInfo.asynchronousOutput, aliasEmitInfo.subModuleElementDeclarationEmitInfo); + appliedSyncOutputPos = aliasEmitInfo.outputPos; + } + }); + declarationOutput += synchronousDeclarationOutput.substring(appliedSyncOutputPos); + return declarationOutput; } + } + ts.writeDeclarationFile = writeDeclarationFile; +})(ts || (ts = {})); +/// +/// +/// +/// +/// +/* @internal */ +var ts; +(function (ts) { + // Flags enum to track count of temp variables and a few dedicated names + var TempFlags; + (function (TempFlags) { + TempFlags[TempFlags["Auto"] = 0] = "Auto"; + TempFlags[TempFlags["CountMask"] = 268435455] = "CountMask"; + TempFlags[TempFlags["_i"] = 268435456] = "_i"; + })(TempFlags || (TempFlags = {})); + var id = function (s) { return s; }; + var nullTransformers = [function (ctx) { return id; }]; + // targetSourceFile is when users only want one file in entire project to be emitted. This is used in compileOnSave feature + function emitFiles(resolver, host, targetSourceFile, emitOnlyDtsFiles) { + var delimiters = createDelimiterMap(); + var brackets = createBracketsMap(); + // emit output for the __extends helper function + var extendsHelper = "\nvar __extends = (this && this.__extends) || function (d, b) {\n for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];\n function __() { this.constructor = d; }\n d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\n};"; + // Emit output for the __assign helper function. + // This is typically used for JSX spread attributes, + // and can be used for object literal spread properties. + var assignHelper = "\nvar __assign = (this && this.__assign) || Object.assign || function(t) {\n for (var s, i = 1, n = arguments.length; i < n; i++) {\n s = arguments[i];\n for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))\n t[p] = s[p];\n }\n return t;\n};"; + // emit output for the __decorate helper function + var decorateHelper = "\nvar __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {\n var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;\n if (typeof Reflect === \"object\" && typeof Reflect.decorate === \"function\") r = Reflect.decorate(decorators, target, key, desc);\n else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;\n return c > 3 && r && Object.defineProperty(target, key, r), r;\n};"; + // emit output for the __metadata helper function + var metadataHelper = "\nvar __metadata = (this && this.__metadata) || function (k, v) {\n if (typeof Reflect === \"object\" && typeof Reflect.metadata === \"function\") return Reflect.metadata(k, v);\n};"; + // emit output for the __param helper function + var paramHelper = "\nvar __param = (this && this.__param) || function (paramIndex, decorator) {\n return function (target, key) { decorator(target, key, paramIndex); }\n};"; + // emit output for the __awaiter helper function + var awaiterHelper = "\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments)).next());\n });\n};"; + // The __generator helper is used by down-level transformations to emulate the runtime + // semantics of an ES2015 generator function. When called, this helper returns an + // object that implements the Iterator protocol, in that it has `next`, `return`, and + // `throw` methods that step through the generator when invoked. // - // Type members - // - function emitPropertySignature(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - emit(node.name); - writeIfPresent(node.questionToken, "?"); - emitWithPrefix(": ", node.type); - write(";"); - } - function emitPropertyDeclaration(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - emit(node.name); - emitWithPrefix(": ", node.type); - emitExpressionWithPrefix(" = ", node.initializer); - write(";"); - } - function emitMethodSignature(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - emit(node.name); - writeIfPresent(node.questionToken, "?"); - emitTypeParameters(node, node.typeParameters); - emitParameters(node, node.parameters); - emitWithPrefix(": ", node.type); - write(";"); - } - function emitMethodDeclaration(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - writeIfPresent(node.asteriskToken, "*"); - emit(node.name); - emitSignatureAndBody(node, emitSignatureHead); - } - function emitConstructor(node) { - emitModifiers(node, node.modifiers); - write("constructor"); - emitSignatureAndBody(node, emitSignatureHead); - } - function emitAccessorDeclaration(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - write(node.kind === 149 /* GetAccessor */ ? "get " : "set "); - emit(node.name); - emitSignatureAndBody(node, emitSignatureHead); - } - function emitCallSignature(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - emitTypeParameters(node, node.typeParameters); - emitParameters(node, node.parameters); - emitWithPrefix(": ", node.type); - write(";"); - } - function emitConstructSignature(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - write("new "); - emitTypeParameters(node, node.typeParameters); - emitParameters(node, node.parameters); - emitWithPrefix(": ", node.type); - write(";"); - } - function emitIndexSignature(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - emitParametersForIndexSignature(node, node.parameters); - emitWithPrefix(": ", node.type); - write(";"); - } - function emitSemicolonClassElement(node) { - write(";"); - } + // parameters: + // thisArg The value to use as the `this` binding for the transformed generator body. + // body A function that acts as the transformed generator body. // - // Types + // variables: + // _ Persistent state for the generator that is shared between the helper and the + // generator body. The state object has the following members: + // sent() - A method that returns or throws the current completion value. + // label - The next point at which to resume evaluation of the generator body. + // trys - A stack of protected regions (try/catch/finally blocks). + // ops - A stack of pending instructions when inside of a finally block. + // f A value indicating whether the generator is executing. + // y An iterator to delegate for a yield*. + // t A temporary variable that holds one of the following values (note that these + // cases do not overlap): + // - The completion value when resuming from a `yield` or `yield*`. + // - The error value for a catch block. + // - The current protected region (array of try/catch/finally/end labels). + // - The verb (`next`, `throw`, or `return` method) to delegate to the expression + // of a `yield*`. + // - The result of evaluating the verb delegated to the expression of a `yield*`. // - function emitTypePredicate(node) { - emit(node.parameterName); - write(" is "); - emit(node.type); - } - function emitTypeReference(node) { - emit(node.typeName); - emitTypeArguments(node, node.typeArguments); - } - function emitFunctionType(node) { - emitTypeParameters(node, node.typeParameters); - emitParametersForArrow(node, node.parameters); - write(" => "); - emit(node.type); - } - function emitConstructorType(node) { - write("new "); - emitTypeParameters(node, node.typeParameters); - emitParametersForArrow(node, node.parameters); - write(" => "); - emit(node.type); - } - function emitTypeQuery(node) { - write("typeof "); - emit(node.exprName); - } - function emitTypeLiteral(node) { - write("{"); - emitList(node, node.members, 65 /* TypeLiteralMembers */); - write("}"); - } - function emitArrayType(node) { - emit(node.elementType); - write("[]"); - } - function emitTupleType(node) { - write("["); - emitList(node, node.elementTypes, 336 /* TupleTypeElements */); - write("]"); - } - function emitUnionType(node) { - emitList(node, node.types, 260 /* UnionTypeConstituents */); - } - function emitIntersectionType(node) { - emitList(node, node.types, 264 /* IntersectionTypeConstituents */); - } - function emitParenthesizedType(node) { - write("("); - emit(node.type); - write(")"); - } - function emitThisType(node) { - write("this"); - } - function emitLiteralType(node) { - emitExpression(node.literal); - } + // functions: + // verb(n) Creates a bound callback to the `step` function for opcode `n`. + // step(op) Evaluates opcodes in a generator body until execution is suspended or + // completed. // - // Binding patterns + // The __generator helper understands a limited set of instructions: + // 0: next(value?) - Start or resume the generator with the specified value. + // 1: throw(error) - Resume the generator with an exception. If the generator is + // suspended inside of one or more protected regions, evaluates + // any intervening finally blocks between the current label and + // the nearest catch block or function boundary. If uncaught, the + // exception is thrown to the caller. + // 2: return(value?) - Resume the generator as if with a return. If the generator is + // suspended inside of one or more protected regions, evaluates any + // intervening finally blocks. + // 3: break(label) - Jump to the specified label. If the label is outside of the + // current protected region, evaluates any intervening finally + // blocks. + // 4: yield(value?) - Yield execution to the caller with an optional value. When + // resumed, the generator will continue at the next label. + // 5: yield*(value) - Delegates evaluation to the supplied iterator. When + // delegation completes, the generator will continue at the next + // label. + // 6: catch(error) - Handles an exception thrown from within the generator body. If + // the current label is inside of one or more protected regions, + // evaluates any intervening finally blocks between the current + // label and the nearest catch block or function boundary. If + // uncaught, the exception is thrown to the caller. + // 7: endfinally - Ends a finally block, resuming the last instruction prior to + // entering a finally block. // - function emitObjectBindingPattern(node) { - var elements = node.elements; - if (elements.length === 0) { - write("{}"); - } - else { - write("{"); - emitList(node, elements, 432 /* ObjectBindingPatternElements */); - write("}"); - } - } - function emitArrayBindingPattern(node) { - var elements = node.elements; - if (elements.length === 0) { - write("[]"); - } - else { - write("["); - emitList(node, node.elements, 304 /* ArrayBindingPatternElements */); - write("]"); - } - } - function emitBindingElement(node) { - emitWithSuffix(node.propertyName, ": "); - writeIfPresent(node.dotDotDotToken, "..."); - emit(node.name); - emitExpressionWithPrefix(" = ", node.initializer); + // For examples of how these are used, see the comments in ./transformers/generators.ts + var generatorHelper = "\nvar __generator = (this && this.__generator) || function (thisArg, body) {\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t;\n return { next: verb(0), \"throw\": verb(1), \"return\": verb(2) };\n function verb(n) { return function (v) { return step([n, v]); }; }\n function step(op) {\n if (f) throw new TypeError(\"Generator is already executing.\");\n while (_) try {\n if (f = 1, y && (t = y[op[0] & 2 ? \"return\" : op[0] ? \"throw\" : \"next\"]) && !(t = t.call(y, op[1])).done) return t;\n if (y = 0, t) op = [0, t.value];\n switch (op[0]) {\n case 0: case 1: t = op; break;\n case 4: _.label++; return { value: op[1], done: false };\n case 5: _.label++; y = op[1]; op = [0]; continue;\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\n default:\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\n if (t[2]) _.ops.pop();\n _.trys.pop(); continue;\n }\n op = body.call(thisArg, _);\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\n }\n};"; + // emit output for the __export helper function + var exportStarHelper = "\nfunction __export(m) {\n for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];\n}"; + // emit output for the UMD helper function. + var umdHelper = "\n(function (dependencies, factory) {\n if (typeof module === 'object' && typeof module.exports === 'object') {\n var v = factory(require, exports); if (v !== undefined) module.exports = v;\n }\n else if (typeof define === 'function' && define.amd) {\n define(dependencies, factory);\n }\n})"; + var superHelper = "\nconst _super = name => super[name];"; + var advancedSuperHelper = "\nconst _super = (function (geti, seti) {\n const cache = Object.create(null);\n return name => cache[name] || (cache[name] = { get value() { return geti(name); }, set value(v) { seti(name, v); } });\n})(name => super[name], (name, value) => super[name] = value);"; + var compilerOptions = host.getCompilerOptions(); + var languageVersion = ts.getEmitScriptTarget(compilerOptions); + var moduleKind = ts.getEmitModuleKind(compilerOptions); + var sourceMapDataList = compilerOptions.sourceMap || compilerOptions.inlineSourceMap ? [] : undefined; + var emittedFilesList = compilerOptions.listEmittedFiles ? [] : undefined; + var emitterDiagnostics = ts.createDiagnosticCollection(); + var newLine = host.getNewLine(); + var transformers = emitOnlyDtsFiles ? nullTransformers : ts.getTransformers(compilerOptions); + var writer = ts.createTextWriter(newLine); + var write = writer.write, writeLine = writer.writeLine, increaseIndent = writer.increaseIndent, decreaseIndent = writer.decreaseIndent; + var sourceMap = ts.createSourceMapWriter(host, writer); + var emitNodeWithSourceMap = sourceMap.emitNodeWithSourceMap, emitTokenWithSourceMap = sourceMap.emitTokenWithSourceMap; + var comments = ts.createCommentWriter(host, writer, sourceMap); + var emitNodeWithComments = comments.emitNodeWithComments, emitBodyWithDetachedComments = comments.emitBodyWithDetachedComments, emitTrailingCommentsOfPosition = comments.emitTrailingCommentsOfPosition; + var nodeIdToGeneratedName; + var autoGeneratedIdToGeneratedName; + var generatedNameSet; + var tempFlags; + var currentSourceFile; + var currentText; + var currentFileIdentifiers; + var extendsEmitted; + var assignEmitted; + var decorateEmitted; + var paramEmitted; + var awaiterEmitted; + var isOwnFileEmit; + var emitSkipped = false; + var sourceFiles = ts.getSourceFilesToEmit(host, targetSourceFile); + // Transform the source files + ts.performance.mark("beforeTransform"); + var _a = ts.transformFiles(resolver, host, sourceFiles, transformers), transformed = _a.transformed, emitNodeWithSubstitution = _a.emitNodeWithSubstitution, emitNodeWithNotification = _a.emitNodeWithNotification; + ts.performance.measure("transformTime", "beforeTransform"); + // Emit each output file + ts.performance.mark("beforePrint"); + ts.forEachTransformedEmitFile(host, transformed, emitFile, emitOnlyDtsFiles); + ts.performance.measure("printTime", "beforePrint"); + // Clean up emit nodes on parse tree + for (var _b = 0, sourceFiles_4 = sourceFiles; _b < sourceFiles_4.length; _b++) { + var sourceFile = sourceFiles_4[_b]; + ts.disposeEmitNodes(sourceFile); } - // - // Expressions - // - function emitArrayLiteralExpression(node) { - var elements = node.elements; - if (elements.length === 0) { - write("[]"); + return { + emitSkipped: emitSkipped, + diagnostics: emitterDiagnostics.getDiagnostics(), + emittedFiles: emittedFilesList, + sourceMaps: sourceMapDataList + }; + function emitFile(jsFilePath, sourceMapFilePath, declarationFilePath, sourceFiles, isBundledEmit) { + // Make sure not to write js file and source map file if any of them cannot be written + if (!host.isEmitBlocked(jsFilePath) && !compilerOptions.noEmit) { + if (!emitOnlyDtsFiles) { + printFile(jsFilePath, sourceMapFilePath, sourceFiles, isBundledEmit); + } } else { - var preferNewLine = node.multiLine ? 32768 /* PreferNewLine */ : 0 /* None */; - emitExpressionList(node, elements, 4466 /* ArrayLiteralExpressionElements */ | preferNewLine); + emitSkipped = true; } - } - function emitObjectLiteralExpression(node) { - var properties = node.properties; - if (properties.length === 0) { - write("{}"); + if (declarationFilePath) { + emitSkipped = ts.writeDeclarationFile(declarationFilePath, ts.getOriginalSourceFiles(sourceFiles), isBundledEmit, host, resolver, emitterDiagnostics, emitOnlyDtsFiles) || emitSkipped; } - else { - var indentedFlag = node.emitFlags & 524288 /* Indented */; - if (indentedFlag) { - increaseIndent(); + if (!emitSkipped && emittedFilesList) { + if (!emitOnlyDtsFiles) { + emittedFilesList.push(jsFilePath); } - var preferNewLine = node.multiLine ? 32768 /* PreferNewLine */ : 0 /* None */; - var allowTrailingComma = languageVersion >= 1 /* ES5 */ ? 32 /* AllowTrailingComma */ : 0 /* None */; - emitList(node, properties, 978 /* ObjectLiteralExpressionProperties */ | allowTrailingComma | preferNewLine); - if (indentedFlag) { - decreaseIndent(); + if (sourceMapFilePath) { + emittedFilesList.push(sourceMapFilePath); + } + if (declarationFilePath) { + emittedFilesList.push(declarationFilePath); } } } - function emitPropertyAccessExpression(node) { - if (tryEmitConstantValue(node)) { - return; - } - var indentBeforeDot = false; - var indentAfterDot = false; - if (!(node.emitFlags & 1048576 /* NoIndentation */)) { - var dotRangeStart = node.expression.end; - var dotRangeEnd = ts.skipTrivia(currentText, node.expression.end) + 1; - var dotToken = { kind: 21 /* DotToken */, pos: dotRangeStart, end: dotRangeEnd }; - indentBeforeDot = needsIndentation(node, node.expression, dotToken); - indentAfterDot = needsIndentation(node, dotToken, node.name); - } - var shouldEmitDotDot = !indentBeforeDot && needsDotDotForPropertyAccess(node.expression); - emitExpression(node.expression); - increaseIndentIf(indentBeforeDot); - write(shouldEmitDotDot ? ".." : "."); - increaseIndentIf(indentAfterDot); - emit(node.name); - decreaseIndentIf(indentBeforeDot, indentAfterDot); - } - // 1..toString is a valid property access, emit a dot after the literal - // Also emit a dot if expression is a integer const enum value - it will appear in generated code as numeric literal - function needsDotDotForPropertyAccess(expression) { - if (expression.kind === 8 /* NumericLiteral */) { - // check if numeric literal was originally written with a dot - var text = getLiteralTextOfNode(expression); - return text.indexOf(ts.tokenToString(21 /* DotToken */)) < 0; + function printFile(jsFilePath, sourceMapFilePath, sourceFiles, isBundledEmit) { + sourceMap.initialize(jsFilePath, sourceMapFilePath, sourceFiles, isBundledEmit); + nodeIdToGeneratedName = []; + autoGeneratedIdToGeneratedName = []; + generatedNameSet = ts.createMap(); + isOwnFileEmit = !isBundledEmit; + // Emit helpers from all the files + if (isBundledEmit && moduleKind) { + for (var _a = 0, sourceFiles_5 = sourceFiles; _a < sourceFiles_5.length; _a++) { + var sourceFile = sourceFiles_5[_a]; + emitEmitHelpers(sourceFile); + } } - else { - // check if constant enum value is integer - var constantValue = tryGetConstEnumValue(expression); - // isFinite handles cases when constantValue is undefined - return isFinite(constantValue) && Math.floor(constantValue) === constantValue; + // Print each transformed source file. + ts.forEach(sourceFiles, printSourceFile); + writeLine(); + var sourceMappingURL = sourceMap.getSourceMappingURL(); + if (sourceMappingURL) { + write("//# " + "sourceMappingURL" + "=" + sourceMappingURL); // Sometimes tools can sometimes see this line as a source mapping url comment } - } - function emitElementAccessExpression(node) { - if (tryEmitConstantValue(node)) { - return; + // Write the source map + if (compilerOptions.sourceMap && !compilerOptions.inlineSourceMap) { + ts.writeFile(host, emitterDiagnostics, sourceMapFilePath, sourceMap.getText(), /*writeByteOrderMark*/ false); } - emitExpression(node.expression); - write("["); - emitExpression(node.argumentExpression); - write("]"); - } - function emitCallExpression(node) { - emitExpression(node.expression); - emitExpressionList(node, node.arguments, 1296 /* CallExpressionArguments */); - } - function emitNewExpression(node) { - write("new "); - emitExpression(node.expression); - emitExpressionList(node, node.arguments, 9488 /* NewExpressionArguments */); - } - function emitTaggedTemplateExpression(node) { - emitExpression(node.tag); - write(" "); - emitExpression(node.template); - } - function emitTypeAssertionExpression(node) { - if (node.type) { - write("<"); - emit(node.type); - write(">"); + // Record source map data for the test harness. + if (sourceMapDataList) { + sourceMapDataList.push(sourceMap.getSourceMapData()); } - emitExpression(node.expression); - } - function emitParenthesizedExpression(node) { - write("("); - emitExpression(node.expression); - write(")"); - } - function emitFunctionExpression(node) { - emitFunctionDeclarationOrExpression(node); - } - function emitArrowFunction(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - emitSignatureAndBody(node, emitArrowFunctionHead); + // Write the output file + ts.writeFile(host, emitterDiagnostics, jsFilePath, writer.getText(), compilerOptions.emitBOM); + // Reset state + sourceMap.reset(); + comments.reset(); + writer.reset(); + tempFlags = 0 /* Auto */; + currentSourceFile = undefined; + currentText = undefined; + extendsEmitted = false; + assignEmitted = false; + decorateEmitted = false; + paramEmitted = false; + awaiterEmitted = false; + isOwnFileEmit = false; } - function emitArrowFunctionHead(node) { - emitTypeParameters(node, node.typeParameters); - emitParametersForArrow(node, node.parameters); - emitWithPrefix(": ", node.type); - write(" =>"); + function printSourceFile(node) { + currentSourceFile = node; + currentText = node.text; + currentFileIdentifiers = node.identifiers; + sourceMap.setSourceFile(node); + comments.setSourceFile(node); + pipelineEmitWithNotification(0 /* SourceFile */, node); } - function emitDeleteExpression(node) { - write("delete "); - emitExpression(node.expression); + /** + * Emits a node. + */ + function emit(node) { + pipelineEmitWithNotification(3 /* Unspecified */, node); } - function emitTypeOfExpression(node) { - write("typeof "); - emitExpression(node.expression); + /** + * Emits an IdentifierName. + */ + function emitIdentifierName(node) { + pipelineEmitWithNotification(2 /* IdentifierName */, node); } - function emitVoidExpression(node) { - write("void "); - emitExpression(node.expression); + /** + * Emits an expression node. + */ + function emitExpression(node) { + pipelineEmitWithNotification(1 /* Expression */, node); } - function emitAwaitExpression(node) { - write("await "); - emitExpression(node.expression); + /** + * Emits a node with possible notification. + * + * NOTE: Do not call this method directly. It is part of the emit pipeline + * and should only be called from printSourceFile, emit, emitExpression, or + * emitIdentifierName. + */ + function pipelineEmitWithNotification(emitContext, node) { + emitNodeWithNotification(emitContext, node, pipelineEmitWithComments); } - function emitPrefixUnaryExpression(node) { - writeTokenText(node.operator); - if (shouldEmitWhitespaceBeforeOperand(node)) { - write(" "); + /** + * Emits a node with comments. + * + * NOTE: Do not call this method directly. It is part of the emit pipeline + * and should only be called indirectly from pipelineEmitWithNotification. + */ + function pipelineEmitWithComments(emitContext, node) { + // Do not emit comments for SourceFile + if (emitContext === 0 /* SourceFile */) { + pipelineEmitWithSourceMap(emitContext, node); + return; } - emitExpression(node.operand); - } - function shouldEmitWhitespaceBeforeOperand(node) { - // In some cases, we need to emit a space between the operator and the operand. One obvious case - // is when the operator is an identifier, like delete or typeof. We also need to do this for plus - // and minus expressions in certain cases. Specifically, consider the following two cases (parens - // are just for clarity of exposition, and not part of the source code): - // - // (+(+1)) - // (+(++1)) - // - // We need to emit a space in both cases. In the first case, the absence of a space will make - // the resulting expression a prefix increment operation. And in the second, it will make the resulting - // expression a prefix increment whose operand is a plus expression - (++(+x)) - // The same is true of minus of course. - var operand = node.operand; - return operand.kind === 185 /* PrefixUnaryExpression */ - && ((node.operator === 35 /* PlusToken */ && (operand.operator === 35 /* PlusToken */ || operand.operator === 41 /* PlusPlusToken */)) - || (node.operator === 36 /* MinusToken */ && (operand.operator === 36 /* MinusToken */ || operand.operator === 42 /* MinusMinusToken */))); - } - function emitPostfixUnaryExpression(node) { - emitExpression(node.operand); - writeTokenText(node.operator); - } - function emitBinaryExpression(node) { - var isCommaOperator = node.operatorToken.kind !== 24 /* CommaToken */; - var indentBeforeOperator = needsIndentation(node, node.left, node.operatorToken); - var indentAfterOperator = needsIndentation(node, node.operatorToken, node.right); - emitExpression(node.left); - increaseIndentIf(indentBeforeOperator, isCommaOperator ? " " : undefined); - writeTokenText(node.operatorToken.kind); - increaseIndentIf(indentAfterOperator, " "); - emitExpression(node.right); - decreaseIndentIf(indentBeforeOperator, indentAfterOperator); - } - function emitConditionalExpression(node) { - var indentBeforeQuestion = needsIndentation(node, node.condition, node.questionToken); - var indentAfterQuestion = needsIndentation(node, node.questionToken, node.whenTrue); - var indentBeforeColon = needsIndentation(node, node.whenTrue, node.colonToken); - var indentAfterColon = needsIndentation(node, node.colonToken, node.whenFalse); - emitExpression(node.condition); - increaseIndentIf(indentBeforeQuestion, " "); - write("?"); - increaseIndentIf(indentAfterQuestion, " "); - emitExpression(node.whenTrue); - decreaseIndentIf(indentBeforeQuestion, indentAfterQuestion); - increaseIndentIf(indentBeforeColon, " "); - write(":"); - increaseIndentIf(indentAfterColon, " "); - emitExpression(node.whenFalse); - decreaseIndentIf(indentBeforeColon, indentAfterColon); + emitNodeWithComments(emitContext, node, pipelineEmitWithSourceMap); } - function emitTemplateExpression(node) { - emit(node.head); - emitList(node, node.templateSpans, 131072 /* TemplateExpressionSpans */); + /** + * Emits a node with source maps. + * + * NOTE: Do not call this method directly. It is part of the emit pipeline + * and should only be called indirectly from pipelineEmitWithComments. + */ + function pipelineEmitWithSourceMap(emitContext, node) { + // Do not emit source mappings for SourceFile or IdentifierName + if (emitContext === 0 /* SourceFile */ + || emitContext === 2 /* IdentifierName */) { + pipelineEmitWithSubstitution(emitContext, node); + return; + } + emitNodeWithSourceMap(emitContext, node, pipelineEmitWithSubstitution); } - function emitYieldExpression(node) { - write(node.asteriskToken ? "yield*" : "yield"); - emitExpressionWithPrefix(" ", node.expression); + /** + * Emits a node with possible substitution. + * + * NOTE: Do not call this method directly. It is part of the emit pipeline + * and should only be called indirectly from pipelineEmitWithSourceMap or + * pipelineEmitInUnspecifiedContext (when picking a more specific context). + */ + function pipelineEmitWithSubstitution(emitContext, node) { + emitNodeWithSubstitution(emitContext, node, pipelineEmitForContext); } - function emitSpreadElementExpression(node) { - write("..."); - emitExpression(node.expression); + /** + * Emits a node. + * + * NOTE: Do not call this method directly. It is part of the emit pipeline + * and should only be called indirectly from pipelineEmitWithSubstitution. + */ + function pipelineEmitForContext(emitContext, node) { + switch (emitContext) { + case 0 /* SourceFile */: return pipelineEmitInSourceFileContext(node); + case 2 /* IdentifierName */: return pipelineEmitInIdentifierNameContext(node); + case 3 /* Unspecified */: return pipelineEmitInUnspecifiedContext(node); + case 1 /* Expression */: return pipelineEmitInExpressionContext(node); + } } - function emitClassExpression(node) { - emitClassDeclarationOrExpression(node); + /** + * Emits a node in the SourceFile EmitContext. + * + * NOTE: Do not call this method directly. It is part of the emit pipeline + * and should only be called indirectly from pipelineEmitForContext. + */ + function pipelineEmitInSourceFileContext(node) { + var kind = node.kind; + switch (kind) { + // Top-level nodes + case 256 /* SourceFile */: + return emitSourceFile(node); + } } - function emitExpressionWithTypeArguments(node) { - emitExpression(node.expression); - emitTypeArguments(node, node.typeArguments); + /** + * Emits a node in the IdentifierName EmitContext. + * + * NOTE: Do not call this method directly. It is part of the emit pipeline + * and should only be called indirectly from pipelineEmitForContext. + */ + function pipelineEmitInIdentifierNameContext(node) { + var kind = node.kind; + switch (kind) { + // Identifiers + case 69 /* Identifier */: + return emitIdentifier(node); + } } - function emitAsExpression(node) { - emitExpression(node.expression); - if (node.type) { - write(" as "); - emit(node.type); + /** + * Emits a node in the Unspecified EmitContext. + * + * NOTE: Do not call this method directly. It is part of the emit pipeline + * and should only be called indirectly from pipelineEmitForContext. + */ + function pipelineEmitInUnspecifiedContext(node) { + var kind = node.kind; + switch (kind) { + // Pseudo-literals + case 12 /* TemplateHead */: + case 13 /* TemplateMiddle */: + case 14 /* TemplateTail */: + return emitLiteral(node); + // Identifiers + case 69 /* Identifier */: + return emitIdentifier(node); + // Reserved words + case 74 /* ConstKeyword */: + case 77 /* DefaultKeyword */: + case 82 /* ExportKeyword */: + case 103 /* VoidKeyword */: + // Strict mode reserved words + case 110 /* PrivateKeyword */: + case 111 /* ProtectedKeyword */: + case 112 /* PublicKeyword */: + case 113 /* StaticKeyword */: + // Contextual keywords + case 115 /* AbstractKeyword */: + case 117 /* AnyKeyword */: + case 118 /* AsyncKeyword */: + case 120 /* BooleanKeyword */: + case 122 /* DeclareKeyword */: + case 130 /* NumberKeyword */: + case 128 /* ReadonlyKeyword */: + case 132 /* StringKeyword */: + case 133 /* SymbolKeyword */: + case 137 /* GlobalKeyword */: + writeTokenText(kind); + return; + // Parse tree nodes + // Names + case 139 /* QualifiedName */: + return emitQualifiedName(node); + case 140 /* ComputedPropertyName */: + return emitComputedPropertyName(node); + // Signature elements + case 141 /* TypeParameter */: + return emitTypeParameter(node); + case 142 /* Parameter */: + return emitParameter(node); + case 143 /* Decorator */: + return emitDecorator(node); + // Type members + case 144 /* PropertySignature */: + return emitPropertySignature(node); + case 145 /* PropertyDeclaration */: + return emitPropertyDeclaration(node); + case 146 /* MethodSignature */: + return emitMethodSignature(node); + case 147 /* MethodDeclaration */: + return emitMethodDeclaration(node); + case 148 /* Constructor */: + return emitConstructor(node); + case 149 /* GetAccessor */: + case 150 /* SetAccessor */: + return emitAccessorDeclaration(node); + case 151 /* CallSignature */: + return emitCallSignature(node); + case 152 /* ConstructSignature */: + return emitConstructSignature(node); + case 153 /* IndexSignature */: + return emitIndexSignature(node); + // Types + case 154 /* TypePredicate */: + return emitTypePredicate(node); + case 155 /* TypeReference */: + return emitTypeReference(node); + case 156 /* FunctionType */: + return emitFunctionType(node); + case 157 /* ConstructorType */: + return emitConstructorType(node); + case 158 /* TypeQuery */: + return emitTypeQuery(node); + case 159 /* TypeLiteral */: + return emitTypeLiteral(node); + case 160 /* ArrayType */: + return emitArrayType(node); + case 161 /* TupleType */: + return emitTupleType(node); + case 162 /* UnionType */: + return emitUnionType(node); + case 163 /* IntersectionType */: + return emitIntersectionType(node); + case 164 /* ParenthesizedType */: + return emitParenthesizedType(node); + case 194 /* ExpressionWithTypeArguments */: + return emitExpressionWithTypeArguments(node); + case 165 /* ThisType */: + return emitThisType(node); + case 166 /* LiteralType */: + return emitLiteralType(node); + // Binding patterns + case 167 /* ObjectBindingPattern */: + return emitObjectBindingPattern(node); + case 168 /* ArrayBindingPattern */: + return emitArrayBindingPattern(node); + case 169 /* BindingElement */: + return emitBindingElement(node); + // Misc + case 197 /* TemplateSpan */: + return emitTemplateSpan(node); + case 198 /* SemicolonClassElement */: + return emitSemicolonClassElement(node); + // Statements + case 199 /* Block */: + return emitBlock(node); + case 200 /* VariableStatement */: + return emitVariableStatement(node); + case 201 /* EmptyStatement */: + return emitEmptyStatement(node); + case 202 /* ExpressionStatement */: + return emitExpressionStatement(node); + case 203 /* IfStatement */: + return emitIfStatement(node); + case 204 /* DoStatement */: + return emitDoStatement(node); + case 205 /* WhileStatement */: + return emitWhileStatement(node); + case 206 /* ForStatement */: + return emitForStatement(node); + case 207 /* ForInStatement */: + return emitForInStatement(node); + case 208 /* ForOfStatement */: + return emitForOfStatement(node); + case 209 /* ContinueStatement */: + return emitContinueStatement(node); + case 210 /* BreakStatement */: + return emitBreakStatement(node); + case 211 /* ReturnStatement */: + return emitReturnStatement(node); + case 212 /* WithStatement */: + return emitWithStatement(node); + case 213 /* SwitchStatement */: + return emitSwitchStatement(node); + case 214 /* LabeledStatement */: + return emitLabeledStatement(node); + case 215 /* ThrowStatement */: + return emitThrowStatement(node); + case 216 /* TryStatement */: + return emitTryStatement(node); + case 217 /* DebuggerStatement */: + return emitDebuggerStatement(node); + // Declarations + case 218 /* VariableDeclaration */: + return emitVariableDeclaration(node); + case 219 /* VariableDeclarationList */: + return emitVariableDeclarationList(node); + case 220 /* FunctionDeclaration */: + return emitFunctionDeclaration(node); + case 221 /* ClassDeclaration */: + return emitClassDeclaration(node); + case 222 /* InterfaceDeclaration */: + return emitInterfaceDeclaration(node); + case 223 /* TypeAliasDeclaration */: + return emitTypeAliasDeclaration(node); + case 224 /* EnumDeclaration */: + return emitEnumDeclaration(node); + case 225 /* ModuleDeclaration */: + return emitModuleDeclaration(node); + case 226 /* ModuleBlock */: + return emitModuleBlock(node); + case 227 /* CaseBlock */: + return emitCaseBlock(node); + case 229 /* ImportEqualsDeclaration */: + return emitImportEqualsDeclaration(node); + case 230 /* ImportDeclaration */: + return emitImportDeclaration(node); + case 231 /* ImportClause */: + return emitImportClause(node); + case 232 /* NamespaceImport */: + return emitNamespaceImport(node); + case 233 /* NamedImports */: + return emitNamedImports(node); + case 234 /* ImportSpecifier */: + return emitImportSpecifier(node); + case 235 /* ExportAssignment */: + return emitExportAssignment(node); + case 236 /* ExportDeclaration */: + return emitExportDeclaration(node); + case 237 /* NamedExports */: + return emitNamedExports(node); + case 238 /* ExportSpecifier */: + return emitExportSpecifier(node); + case 239 /* MissingDeclaration */: + return; + // Module references + case 240 /* ExternalModuleReference */: + return emitExternalModuleReference(node); + // JSX (non-expression) + case 244 /* JsxText */: + return emitJsxText(node); + case 243 /* JsxOpeningElement */: + return emitJsxOpeningElement(node); + case 245 /* JsxClosingElement */: + return emitJsxClosingElement(node); + case 246 /* JsxAttribute */: + return emitJsxAttribute(node); + case 247 /* JsxSpreadAttribute */: + return emitJsxSpreadAttribute(node); + case 248 /* JsxExpression */: + return emitJsxExpression(node); + // Clauses + case 249 /* CaseClause */: + return emitCaseClause(node); + case 250 /* DefaultClause */: + return emitDefaultClause(node); + case 251 /* HeritageClause */: + return emitHeritageClause(node); + case 252 /* CatchClause */: + return emitCatchClause(node); + // Property assignments + case 253 /* PropertyAssignment */: + return emitPropertyAssignment(node); + case 254 /* ShorthandPropertyAssignment */: + return emitShorthandPropertyAssignment(node); + // Enum + case 255 /* EnumMember */: + return emitEnumMember(node); + } + // If the node is an expression, try to emit it as an expression with + // substitution. + if (ts.isExpression(node)) { + return pipelineEmitWithSubstitution(1 /* Expression */, node); } } - function emitNonNullExpression(node) { - emitExpression(node.expression); - write("!"); + /** + * Emits a node in the Expression EmitContext. + * + * NOTE: Do not call this method directly. It is part of the emit pipeline + * and should only be called indirectly from pipelineEmitForContext. + */ + function pipelineEmitInExpressionContext(node) { + var kind = node.kind; + switch (kind) { + // Literals + case 8 /* NumericLiteral */: + return emitNumericLiteral(node); + case 9 /* StringLiteral */: + case 10 /* RegularExpressionLiteral */: + case 11 /* NoSubstitutionTemplateLiteral */: + return emitLiteral(node); + // Identifiers + case 69 /* Identifier */: + return emitIdentifier(node); + // Reserved words + case 84 /* FalseKeyword */: + case 93 /* NullKeyword */: + case 95 /* SuperKeyword */: + case 99 /* TrueKeyword */: + case 97 /* ThisKeyword */: + writeTokenText(kind); + return; + // Expressions + case 170 /* ArrayLiteralExpression */: + return emitArrayLiteralExpression(node); + case 171 /* ObjectLiteralExpression */: + return emitObjectLiteralExpression(node); + case 172 /* PropertyAccessExpression */: + return emitPropertyAccessExpression(node); + case 173 /* ElementAccessExpression */: + return emitElementAccessExpression(node); + case 174 /* CallExpression */: + return emitCallExpression(node); + case 175 /* NewExpression */: + return emitNewExpression(node); + case 176 /* TaggedTemplateExpression */: + return emitTaggedTemplateExpression(node); + case 177 /* TypeAssertionExpression */: + return emitTypeAssertionExpression(node); + case 178 /* ParenthesizedExpression */: + return emitParenthesizedExpression(node); + case 179 /* FunctionExpression */: + return emitFunctionExpression(node); + case 180 /* ArrowFunction */: + return emitArrowFunction(node); + case 181 /* DeleteExpression */: + return emitDeleteExpression(node); + case 182 /* TypeOfExpression */: + return emitTypeOfExpression(node); + case 183 /* VoidExpression */: + return emitVoidExpression(node); + case 184 /* AwaitExpression */: + return emitAwaitExpression(node); + case 185 /* PrefixUnaryExpression */: + return emitPrefixUnaryExpression(node); + case 186 /* PostfixUnaryExpression */: + return emitPostfixUnaryExpression(node); + case 187 /* BinaryExpression */: + return emitBinaryExpression(node); + case 188 /* ConditionalExpression */: + return emitConditionalExpression(node); + case 189 /* TemplateExpression */: + return emitTemplateExpression(node); + case 190 /* YieldExpression */: + return emitYieldExpression(node); + case 191 /* SpreadElementExpression */: + return emitSpreadElementExpression(node); + case 192 /* ClassExpression */: + return emitClassExpression(node); + case 193 /* OmittedExpression */: + return; + case 195 /* AsExpression */: + return emitAsExpression(node); + case 196 /* NonNullExpression */: + return emitNonNullExpression(node); + // JSX + case 241 /* JsxElement */: + return emitJsxElement(node); + case 242 /* JsxSelfClosingElement */: + return emitJsxSelfClosingElement(node); + // Transformation nodes + case 288 /* PartiallyEmittedExpression */: + return emitPartiallyEmittedExpression(node); + } } // - // Misc + // Literals/Pseudo-literals // - function emitTemplateSpan(node) { - emitExpression(node.expression); - emit(node.literal); + // SyntaxKind.NumericLiteral + function emitNumericLiteral(node) { + emitLiteral(node); + if (node.trailingComment) { + write(" /*" + node.trailingComment + "*/"); + } } - // - // Statements - // - function emitBlock(node, format) { - if (isSingleLineEmptyBlock(node)) { - writeToken(15 /* OpenBraceToken */, node.pos, /*contextNode*/ node); - write(" "); - writeToken(16 /* CloseBraceToken */, node.statements.end, /*contextNode*/ node); + // SyntaxKind.StringLiteral + // SyntaxKind.RegularExpressionLiteral + // SyntaxKind.NoSubstitutionTemplateLiteral + // SyntaxKind.TemplateHead + // SyntaxKind.TemplateMiddle + // SyntaxKind.TemplateTail + function emitLiteral(node) { + var text = getLiteralTextOfNode(node); + if ((compilerOptions.sourceMap || compilerOptions.inlineSourceMap) + && (node.kind === 9 /* StringLiteral */ || ts.isTemplateLiteralKind(node.kind))) { + writer.writeLiteral(text); } else { - writeToken(15 /* OpenBraceToken */, node.pos, /*contextNode*/ node); - emitBlockStatements(node); - writeToken(16 /* CloseBraceToken */, node.statements.end, /*contextNode*/ node); + write(text); } } - function emitBlockStatements(node) { - if (node.emitFlags & 32 /* SingleLine */) { - emitList(node, node.statements, 384 /* SingleLineBlockStatements */); + // + // Identifiers + // + function emitIdentifier(node) { + if (ts.getEmitFlags(node) & 16 /* UMDDefine */) { + writeLines(umdHelper); } else { - emitList(node, node.statements, 65 /* MultiLineBlockStatements */); + write(getTextOfNode(node, /*includeTrivia*/ false)); } } - function emitVariableStatement(node) { - emitModifiers(node, node.modifiers); - emit(node.declarationList); - write(";"); - } - function emitEmptyStatement(node) { - write(";"); - } - function emitExpressionStatement(node) { - emitExpression(node.expression); - write(";"); - } - function emitIfStatement(node) { - var openParenPos = writeToken(88 /* IfKeyword */, node.pos, node); - write(" "); - writeToken(17 /* OpenParenToken */, openParenPos, node); - emitExpression(node.expression); - writeToken(18 /* CloseParenToken */, node.expression.end, node); - emitEmbeddedStatement(node.thenStatement); - if (node.elseStatement) { - writeLine(); - writeToken(80 /* ElseKeyword */, node.thenStatement.end, node); - if (node.elseStatement.kind === 203 /* IfStatement */) { - write(" "); - emit(node.elseStatement); - } - else { - emitEmbeddedStatement(node.elseStatement); - } - } + // + // Names + // + function emitQualifiedName(node) { + emitEntityName(node.left); + write("."); + emit(node.right); } - function emitDoStatement(node) { - write("do"); - emitEmbeddedStatement(node.statement); - if (ts.isBlock(node.statement)) { - write(" "); + function emitEntityName(node) { + if (node.kind === 69 /* Identifier */) { + emitExpression(node); } else { - writeLine(); - } - write("while ("); - emitExpression(node.expression); - write(");"); - } - function emitWhileStatement(node) { - write("while ("); - emitExpression(node.expression); - write(")"); - emitEmbeddedStatement(node.statement); - } - function emitForStatement(node) { - var openParenPos = writeToken(86 /* ForKeyword */, node.pos); - write(" "); - writeToken(17 /* OpenParenToken */, openParenPos, /*contextNode*/ node); - emitForBinding(node.initializer); - write(";"); - emitExpressionWithPrefix(" ", node.condition); - write(";"); - emitExpressionWithPrefix(" ", node.incrementor); - write(")"); - emitEmbeddedStatement(node.statement); - } - function emitForInStatement(node) { - var openParenPos = writeToken(86 /* ForKeyword */, node.pos); - write(" "); - writeToken(17 /* OpenParenToken */, openParenPos); - emitForBinding(node.initializer); - write(" in "); - emitExpression(node.expression); - writeToken(18 /* CloseParenToken */, node.expression.end); - emitEmbeddedStatement(node.statement); - } - function emitForOfStatement(node) { - var openParenPos = writeToken(86 /* ForKeyword */, node.pos); - write(" "); - writeToken(17 /* OpenParenToken */, openParenPos); - emitForBinding(node.initializer); - write(" of "); - emitExpression(node.expression); - writeToken(18 /* CloseParenToken */, node.expression.end); - emitEmbeddedStatement(node.statement); - } - function emitForBinding(node) { - if (node !== undefined) { - if (node.kind === 219 /* VariableDeclarationList */) { - emit(node); - } - else { - emitExpression(node); - } + emit(node); } } - function emitContinueStatement(node) { - writeToken(75 /* ContinueKeyword */, node.pos); - emitWithPrefix(" ", node.label); - write(";"); - } - function emitBreakStatement(node) { - writeToken(70 /* BreakKeyword */, node.pos); - emitWithPrefix(" ", node.label); - write(";"); - } - function emitReturnStatement(node) { - writeToken(94 /* ReturnKeyword */, node.pos, /*contextNode*/ node); - emitExpressionWithPrefix(" ", node.expression); - write(";"); - } - function emitWithStatement(node) { - write("with ("); - emitExpression(node.expression); - write(")"); - emitEmbeddedStatement(node.statement); - } - function emitSwitchStatement(node) { - var openParenPos = writeToken(96 /* SwitchKeyword */, node.pos); - write(" "); - writeToken(17 /* OpenParenToken */, openParenPos); + function emitComputedPropertyName(node) { + write("["); emitExpression(node.expression); - writeToken(18 /* CloseParenToken */, node.expression.end); - write(" "); - emit(node.caseBlock); - } - function emitLabeledStatement(node) { - emit(node.label); - write(": "); - emit(node.statement); - } - function emitThrowStatement(node) { - write("throw"); - emitExpressionWithPrefix(" ", node.expression); - write(";"); - } - function emitTryStatement(node) { - write("try "); - emit(node.tryBlock); - emit(node.catchClause); - if (node.finallyBlock) { - writeLine(); - write("finally "); - emit(node.finallyBlock); - } - } - function emitDebuggerStatement(node) { - writeToken(76 /* DebuggerKeyword */, node.pos); - write(";"); + write("]"); } // - // Declarations + // Signature elements // - function emitVariableDeclaration(node) { + function emitTypeParameter(node) { emit(node.name); - emitExpressionWithPrefix(" = ", node.initializer); - } - function emitVariableDeclarationList(node) { - write(ts.isLet(node) ? "let " : ts.isConst(node) ? "const " : "var "); - emitList(node, node.declarations, 272 /* VariableDeclarationList */); - } - function emitFunctionDeclaration(node) { - emitFunctionDeclarationOrExpression(node); + emitWithPrefix(" extends ", node.constraint); } - function emitFunctionDeclarationOrExpression(node) { + function emitParameter(node) { emitDecorators(node, node.decorators); emitModifiers(node, node.modifiers); - write(node.asteriskToken ? "function* " : "function "); - emitIdentifierName(node.name); - emitSignatureAndBody(node, emitSignatureHead); - } - function emitSignatureAndBody(node, emitSignatureHead) { - var body = node.body; - if (body) { - if (ts.isBlock(body)) { - var indentedFlag = node.emitFlags & 524288 /* Indented */; - if (indentedFlag) { - increaseIndent(); - } - if (node.emitFlags & 4194304 /* ReuseTempVariableScope */) { - emitSignatureHead(node); - emitBlockFunctionBody(node, body); - } - else { - var savedTempFlags = tempFlags; - tempFlags = 0; - emitSignatureHead(node); - emitBlockFunctionBody(node, body); - tempFlags = savedTempFlags; - } - if (indentedFlag) { - decreaseIndent(); - } - } - else { - emitSignatureHead(node); - write(" "); - emitExpression(body); - } - } - else { - emitSignatureHead(node); - write(";"); - } - } - function emitSignatureHead(node) { - emitTypeParameters(node, node.typeParameters); - emitParameters(node, node.parameters); + writeIfPresent(node.dotDotDotToken, "..."); + emit(node.name); + writeIfPresent(node.questionToken, "?"); + emitExpressionWithPrefix(" = ", node.initializer); emitWithPrefix(": ", node.type); } - function shouldEmitBlockFunctionBodyOnSingleLine(parentNode, body) { - // We must emit a function body as a single-line body in the following case: - // * The body has NodeEmitFlags.SingleLine specified. - // We must emit a function body as a multi-line body in the following cases: - // * The body is explicitly marked as multi-line. - // * A non-synthesized body's start and end position are on different lines. - // * Any statement in the body starts on a new line. - if (body.emitFlags & 32 /* SingleLine */) { - return true; - } - if (body.multiLine) { - return false; - } - if (!ts.nodeIsSynthesized(body) && !ts.rangeIsOnSingleLine(body, currentSourceFile)) { - return false; - } - if (shouldWriteLeadingLineTerminator(body, body.statements, 2 /* PreserveLines */) - || shouldWriteClosingLineTerminator(body, body.statements, 2 /* PreserveLines */)) { - return false; - } - var previousStatement; - for (var _a = 0, _b = body.statements; _a < _b.length; _a++) { - var statement = _b[_a]; - if (shouldWriteSeparatingLineTerminator(previousStatement, statement, 2 /* PreserveLines */)) { - return false; - } - previousStatement = statement; - } - return true; - } - function emitBlockFunctionBody(parentNode, body) { - write(" {"); - increaseIndent(); - emitBodyWithDetachedComments(body, body.statements, shouldEmitBlockFunctionBodyOnSingleLine(parentNode, body) - ? emitBlockFunctionBodyOnSingleLine - : emitBlockFunctionBodyWorker); - decreaseIndent(); - writeToken(16 /* CloseBraceToken */, body.statements.end, body); - } - function emitBlockFunctionBodyOnSingleLine(body) { - emitBlockFunctionBodyWorker(body, /*emitBlockFunctionBodyOnSingleLine*/ true); - } - function emitBlockFunctionBodyWorker(body, emitBlockFunctionBodyOnSingleLine) { - // Emit all the prologue directives (like "use strict"). - var statementOffset = emitPrologueDirectives(body.statements, /*startWithNewLine*/ true); - var helpersEmitted = emitHelpers(body); - if (statementOffset === 0 && !helpersEmitted && emitBlockFunctionBodyOnSingleLine) { - decreaseIndent(); - emitList(body, body.statements, 384 /* SingleLineFunctionBodyStatements */); - increaseIndent(); - } - else { - emitList(body, body.statements, 1 /* MultiLineFunctionBodyStatements */, statementOffset); - } - } - function emitClassDeclaration(node) { - emitClassDeclarationOrExpression(node); - } - function emitClassDeclarationOrExpression(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - write("class"); - emitNodeWithPrefix(" ", node.name, emitIdentifierName); - var indentedFlag = node.emitFlags & 524288 /* Indented */; - if (indentedFlag) { - increaseIndent(); - } - emitTypeParameters(node, node.typeParameters); - emitList(node, node.heritageClauses, 256 /* ClassHeritageClauses */); - var savedTempFlags = tempFlags; - tempFlags = 0; - write(" {"); - emitList(node, node.members, 65 /* ClassMembers */); - write("}"); - if (indentedFlag) { - decreaseIndent(); - } - tempFlags = savedTempFlags; - } - function emitInterfaceDeclaration(node) { - emitDecorators(node, node.decorators); - emitModifiers(node, node.modifiers); - write("interface "); - emit(node.name); - emitTypeParameters(node, node.typeParameters); - emitList(node, node.heritageClauses, 256 /* HeritageClauses */); - write(" {"); - emitList(node, node.members, 65 /* InterfaceMembers */); - write("}"); + function emitDecorator(decorator) { + write("@"); + emitExpression(decorator.expression); } - function emitTypeAliasDeclaration(node) { + // + // Type members + // + function emitPropertySignature(node) { emitDecorators(node, node.decorators); emitModifiers(node, node.modifiers); - write("type "); - emit(node.name); - emitTypeParameters(node, node.typeParameters); - write(" = "); - emit(node.type); - write(";"); - } - function emitEnumDeclaration(node) { - emitModifiers(node, node.modifiers); - write("enum "); - emit(node.name); - var savedTempFlags = tempFlags; - tempFlags = 0; - write(" {"); - emitList(node, node.members, 81 /* EnumMembers */); - write("}"); - tempFlags = savedTempFlags; - } - function emitModuleDeclaration(node) { - emitModifiers(node, node.modifiers); - write(node.flags & 16 /* Namespace */ ? "namespace " : "module "); emit(node.name); - var body = node.body; - while (body.kind === 225 /* ModuleDeclaration */) { - write("."); - emit(body.name); - body = body.body; - } - write(" "); - emit(body); - } - function emitModuleBlock(node) { - if (isSingleLineEmptyBlock(node)) { - write("{ }"); - } - else { - var savedTempFlags = tempFlags; - tempFlags = 0; - write("{"); - increaseIndent(); - emitBlockStatements(node); - write("}"); - tempFlags = savedTempFlags; - } - } - function emitCaseBlock(node) { - writeToken(15 /* OpenBraceToken */, node.pos); - emitList(node, node.clauses, 65 /* CaseBlockClauses */); - writeToken(16 /* CloseBraceToken */, node.clauses.end); + writeIfPresent(node.questionToken, "?"); + emitWithPrefix(": ", node.type); + write(";"); } - function emitImportEqualsDeclaration(node) { + function emitPropertyDeclaration(node) { + emitDecorators(node, node.decorators); emitModifiers(node, node.modifiers); - write("import "); emit(node.name); - write(" = "); - emitModuleReference(node.moduleReference); + emitWithPrefix(": ", node.type); + emitExpressionWithPrefix(" = ", node.initializer); write(";"); } - function emitModuleReference(node) { - if (node.kind === 69 /* Identifier */) { - emitExpression(node); - } - else { - emit(node); - } - } - function emitImportDeclaration(node) { + function emitMethodSignature(node) { + emitDecorators(node, node.decorators); emitModifiers(node, node.modifiers); - write("import "); - if (node.importClause) { - emit(node.importClause); - write(" from "); - } - emitExpression(node.moduleSpecifier); + emit(node.name); + writeIfPresent(node.questionToken, "?"); + emitTypeParameters(node, node.typeParameters); + emitParameters(node, node.parameters); + emitWithPrefix(": ", node.type); write(";"); } - function emitImportClause(node) { + function emitMethodDeclaration(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + writeIfPresent(node.asteriskToken, "*"); emit(node.name); - if (node.name && node.namedBindings) { - write(", "); - } - emit(node.namedBindings); + emitSignatureAndBody(node, emitSignatureHead); } - function emitNamespaceImport(node) { - write("* as "); + function emitConstructor(node) { + emitModifiers(node, node.modifiers); + write("constructor"); + emitSignatureAndBody(node, emitSignatureHead); + } + function emitAccessorDeclaration(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + write(node.kind === 149 /* GetAccessor */ ? "get " : "set "); emit(node.name); + emitSignatureAndBody(node, emitSignatureHead); } - function emitNamedImports(node) { - emitNamedImportsOrExports(node); + function emitCallSignature(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + emitTypeParameters(node, node.typeParameters); + emitParameters(node, node.parameters); + emitWithPrefix(": ", node.type); + write(";"); } - function emitImportSpecifier(node) { - emitImportOrExportSpecifier(node); + function emitConstructSignature(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + write("new "); + emitTypeParameters(node, node.typeParameters); + emitParameters(node, node.parameters); + emitWithPrefix(": ", node.type); + write(";"); } - function emitExportAssignment(node) { - write(node.isExportEquals ? "export = " : "export default "); - emitExpression(node.expression); + function emitIndexSignature(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + emitParametersForIndexSignature(node, node.parameters); + emitWithPrefix(": ", node.type); write(";"); } - function emitExportDeclaration(node) { - write("export "); - if (node.exportClause) { - emit(node.exportClause); - } - else { - write("*"); - } - if (node.moduleSpecifier) { - write(" from "); - emitExpression(node.moduleSpecifier); - } + function emitSemicolonClassElement(node) { write(";"); } - function emitNamedExports(node) { - emitNamedImportsOrExports(node); + // + // Types + // + function emitTypePredicate(node) { + emit(node.parameterName); + write(" is "); + emit(node.type); } - function emitExportSpecifier(node) { - emitImportOrExportSpecifier(node); + function emitTypeReference(node) { + emit(node.typeName); + emitTypeArguments(node, node.typeArguments); } - function emitNamedImportsOrExports(node) { - write("{"); - emitList(node, node.elements, 432 /* NamedImportsOrExportsElements */); - write("}"); + function emitFunctionType(node) { + emitTypeParameters(node, node.typeParameters); + emitParametersForArrow(node, node.parameters); + write(" => "); + emit(node.type); } - function emitImportOrExportSpecifier(node) { - if (node.propertyName) { - emit(node.propertyName); - write(" as "); - } - emit(node.name); + function emitConstructorType(node) { + write("new "); + emitTypeParameters(node, node.typeParameters); + emitParametersForArrow(node, node.parameters); + write(" => "); + emit(node.type); } - // - // Module references - // - function emitExternalModuleReference(node) { - write("require("); - emitExpression(node.expression); - write(")"); + function emitTypeQuery(node) { + write("typeof "); + emit(node.exprName); } - // - // JSX - // - function emitJsxElement(node) { - emit(node.openingElement); - emitList(node, node.children, 131072 /* JsxElementChildren */); - emit(node.closingElement); + function emitTypeLiteral(node) { + write("{"); + emitList(node, node.members, 65 /* TypeLiteralMembers */); + write("}"); } - function emitJsxSelfClosingElement(node) { - write("<"); - emitJsxTagName(node.tagName); - write(" "); - emitList(node, node.attributes, 131328 /* JsxElementAttributes */); - write("/>"); + function emitArrayType(node) { + emit(node.elementType); + write("[]"); } - function emitJsxOpeningElement(node) { - write("<"); - emitJsxTagName(node.tagName); - writeIfAny(node.attributes, " "); - emitList(node, node.attributes, 131328 /* JsxElementAttributes */); - write(">"); + function emitTupleType(node) { + write("["); + emitList(node, node.elementTypes, 336 /* TupleTypeElements */); + write("]"); } - function emitJsxText(node) { - writer.writeLiteral(getTextOfNode(node, /*includeTrivia*/ true)); + function emitUnionType(node) { + emitList(node, node.types, 260 /* UnionTypeConstituents */); } - function emitJsxClosingElement(node) { - write(""); + function emitIntersectionType(node) { + emitList(node, node.types, 264 /* IntersectionTypeConstituents */); } - function emitJsxAttribute(node) { - emit(node.name); - emitWithPrefix("=", node.initializer); + function emitParenthesizedType(node) { + write("("); + emit(node.type); + write(")"); } - function emitJsxSpreadAttribute(node) { - write("{..."); - emitExpression(node.expression); - write("}"); + function emitThisType(node) { + write("this"); } - function emitJsxExpression(node) { - if (node.expression) { + function emitLiteralType(node) { + emitExpression(node.literal); + } + // + // Binding patterns + // + function emitObjectBindingPattern(node) { + var elements = node.elements; + if (elements.length === 0) { + write("{}"); + } + else { write("{"); - emitExpression(node.expression); + emitList(node, elements, 432 /* ObjectBindingPatternElements */); write("}"); } } - function emitJsxTagName(node) { - if (node.kind === 69 /* Identifier */) { - emitExpression(node); + function emitArrayBindingPattern(node) { + var elements = node.elements; + if (elements.length === 0) { + write("[]"); } else { - emit(node); + write("["); + emitList(node, node.elements, 304 /* ArrayBindingPatternElements */); + write("]"); } } + function emitBindingElement(node) { + emitWithSuffix(node.propertyName, ": "); + writeIfPresent(node.dotDotDotToken, "..."); + emit(node.name); + emitExpressionWithPrefix(" = ", node.initializer); + } // - // Clauses + // Expressions // - function emitCaseClause(node) { - write("case "); - emitExpression(node.expression); - write(":"); - emitCaseOrDefaultClauseStatements(node, node.statements); - } - function emitDefaultClause(node) { - write("default:"); - emitCaseOrDefaultClauseStatements(node, node.statements); + function emitArrayLiteralExpression(node) { + var elements = node.elements; + if (elements.length === 0) { + write("[]"); + } + else { + var preferNewLine = node.multiLine ? 32768 /* PreferNewLine */ : 0 /* None */; + emitExpressionList(node, elements, 4466 /* ArrayLiteralExpressionElements */ | preferNewLine); + } } - function emitCaseOrDefaultClauseStatements(parentNode, statements) { - var emitAsSingleStatement = statements.length === 1 && - ( - // treat synthesized nodes as located on the same line for emit purposes - ts.nodeIsSynthesized(parentNode) || - ts.nodeIsSynthesized(statements[0]) || - ts.rangeStartPositionsAreOnSameLine(parentNode, statements[0], currentSourceFile)); - if (emitAsSingleStatement) { - write(" "); - emit(statements[0]); + function emitObjectLiteralExpression(node) { + var properties = node.properties; + if (properties.length === 0) { + write("{}"); } else { - emitList(parentNode, statements, 81985 /* CaseOrDefaultClauseStatements */); + var indentedFlag = ts.getEmitFlags(node) & 524288 /* Indented */; + if (indentedFlag) { + increaseIndent(); + } + var preferNewLine = node.multiLine ? 32768 /* PreferNewLine */ : 0 /* None */; + var allowTrailingComma = languageVersion >= 1 /* ES5 */ ? 32 /* AllowTrailingComma */ : 0 /* None */; + emitList(node, properties, 978 /* ObjectLiteralExpressionProperties */ | allowTrailingComma | preferNewLine); + if (indentedFlag) { + decreaseIndent(); + } } } - function emitHeritageClause(node) { - write(" "); - writeTokenText(node.token); - write(" "); - emitList(node, node.types, 272 /* HeritageClauseTypes */); + function emitPropertyAccessExpression(node) { + var indentBeforeDot = false; + var indentAfterDot = false; + if (!(ts.getEmitFlags(node) & 1048576 /* NoIndentation */)) { + var dotRangeStart = node.expression.end; + var dotRangeEnd = ts.skipTrivia(currentText, node.expression.end) + 1; + var dotToken = { kind: 21 /* DotToken */, pos: dotRangeStart, end: dotRangeEnd }; + indentBeforeDot = needsIndentation(node, node.expression, dotToken); + indentAfterDot = needsIndentation(node, dotToken, node.name); + } + emitExpression(node.expression); + increaseIndentIf(indentBeforeDot); + var shouldEmitDotDot = !indentBeforeDot && needsDotDotForPropertyAccess(node.expression); + write(shouldEmitDotDot ? ".." : "."); + increaseIndentIf(indentAfterDot); + emit(node.name); + decreaseIndentIf(indentBeforeDot, indentAfterDot); } - function emitCatchClause(node) { - writeLine(); - var openParenPos = writeToken(72 /* CatchKeyword */, node.pos); - write(" "); - writeToken(17 /* OpenParenToken */, openParenPos); - emit(node.variableDeclaration); - writeToken(18 /* CloseParenToken */, node.variableDeclaration ? node.variableDeclaration.end : openParenPos); + // 1..toString is a valid property access, emit a dot after the literal + // Also emit a dot if expression is a integer const enum value - it will appear in generated code as numeric literal + function needsDotDotForPropertyAccess(expression) { + if (expression.kind === 8 /* NumericLiteral */) { + // check if numeric literal was originally written with a dot + var text = getLiteralTextOfNode(expression); + return text.indexOf(ts.tokenToString(21 /* DotToken */)) < 0; + } + else if (ts.isPropertyAccessExpression(expression) || ts.isElementAccessExpression(expression)) { + // check if constant enum value is integer + var constantValue = ts.getConstantValue(expression); + // isFinite handles cases when constantValue is undefined + return isFinite(constantValue) + && Math.floor(constantValue) === constantValue + && compilerOptions.removeComments; + } + } + function emitElementAccessExpression(node) { + emitExpression(node.expression); + write("["); + emitExpression(node.argumentExpression); + write("]"); + } + function emitCallExpression(node) { + emitExpression(node.expression); + emitExpressionList(node, node.arguments, 1296 /* CallExpressionArguments */); + } + function emitNewExpression(node) { + write("new "); + emitExpression(node.expression); + emitExpressionList(node, node.arguments, 9488 /* NewExpressionArguments */); + } + function emitTaggedTemplateExpression(node) { + emitExpression(node.tag); write(" "); - emit(node.block); + emitExpression(node.template); } - // - // Property assignments - // - function emitPropertyAssignment(node) { - emit(node.name); - write(": "); - // This is to ensure that we emit comment in the following case: - // For example: - // obj = { - // id: /*comment1*/ ()=>void - // } - // "comment1" is not considered to be leading comment for node.initializer - // but rather a trailing comment on the previous node. - var initializer = node.initializer; - if (!shouldSkipLeadingCommentsForNode(initializer)) { - var commentRange = initializer.commentRange || initializer; - emitTrailingCommentsOfPosition(commentRange.pos); + function emitTypeAssertionExpression(node) { + if (node.type) { + write("<"); + emit(node.type); + write(">"); } - emitExpression(initializer); + emitExpression(node.expression); } - function emitShorthandPropertyAssignment(node) { - emit(node.name); - if (node.objectAssignmentInitializer) { - write(" = "); - emitExpression(node.objectAssignmentInitializer); - } + function emitParenthesizedExpression(node) { + write("("); + emitExpression(node.expression); + write(")"); } - // - // Enum - // - function emitEnumMember(node) { - emit(node.name); - emitExpressionWithPrefix(" = ", node.initializer); + function emitFunctionExpression(node) { + emitFunctionDeclarationOrExpression(node); } - // - // Top-level nodes - // - function emitSourceFile(node) { - writeLine(); - emitShebang(); - emitBodyWithDetachedComments(node, node.statements, emitSourceFileWorker); + function emitArrowFunction(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + emitSignatureAndBody(node, emitArrowFunctionHead); } - function emitSourceFileWorker(node) { - var statements = node.statements; - var statementOffset = emitPrologueDirectives(statements); - var savedTempFlags = tempFlags; - tempFlags = 0; - emitHelpers(node); - emitList(node, statements, 1 /* MultiLine */, statementOffset); - tempFlags = savedTempFlags; + function emitArrowFunctionHead(node) { + emitTypeParameters(node, node.typeParameters); + emitParametersForArrow(node, node.parameters); + emitWithPrefix(": ", node.type); + write(" =>"); } - // Transformation nodes - function emitPartiallyEmittedExpression(node) { + function emitDeleteExpression(node) { + write("delete "); emitExpression(node.expression); } - /** - * Emits any prologue directives at the start of a Statement list, returning the - * number of prologue directives written to the output. - */ - function emitPrologueDirectives(statements, startWithNewLine) { - for (var i = 0; i < statements.length; i++) { - if (ts.isPrologueDirective(statements[i])) { - if (startWithNewLine || i > 0) { - writeLine(); - } - emit(statements[i]); - } - else { - // return index of the first non prologue directive - return i; - } - } - return statements.length; + function emitTypeOfExpression(node) { + write("typeof "); + emitExpression(node.expression); } - function emitHelpers(node) { - var emitFlags = node.emitFlags; - var helpersEmitted = false; - if (emitFlags & 1 /* EmitEmitHelpers */) { - helpersEmitted = emitEmitHelpers(currentSourceFile); - } - if (emitFlags & 2 /* EmitExportStar */) { - writeLines(exportStarHelper); - helpersEmitted = true; - } - if (emitFlags & 4 /* EmitSuperHelper */) { - writeLines(superHelper); - helpersEmitted = true; - } - if (emitFlags & 8 /* EmitAdvancedSuperHelper */) { - writeLines(advancedSuperHelper); - helpersEmitted = true; + function emitVoidExpression(node) { + write("void "); + emitExpression(node.expression); + } + function emitAwaitExpression(node) { + write("await "); + emitExpression(node.expression); + } + function emitPrefixUnaryExpression(node) { + writeTokenText(node.operator); + if (shouldEmitWhitespaceBeforeOperand(node)) { + write(" "); } - return helpersEmitted; + emitExpression(node.operand); + } + function shouldEmitWhitespaceBeforeOperand(node) { + // In some cases, we need to emit a space between the operator and the operand. One obvious case + // is when the operator is an identifier, like delete or typeof. We also need to do this for plus + // and minus expressions in certain cases. Specifically, consider the following two cases (parens + // are just for clarity of exposition, and not part of the source code): + // + // (+(+1)) + // (+(++1)) + // + // We need to emit a space in both cases. In the first case, the absence of a space will make + // the resulting expression a prefix increment operation. And in the second, it will make the resulting + // expression a prefix increment whose operand is a plus expression - (++(+x)) + // The same is true of minus of course. + var operand = node.operand; + return operand.kind === 185 /* PrefixUnaryExpression */ + && ((node.operator === 35 /* PlusToken */ && (operand.operator === 35 /* PlusToken */ || operand.operator === 41 /* PlusPlusToken */)) + || (node.operator === 36 /* MinusToken */ && (operand.operator === 36 /* MinusToken */ || operand.operator === 42 /* MinusMinusToken */))); + } + function emitPostfixUnaryExpression(node) { + emitExpression(node.operand); + writeTokenText(node.operator); + } + function emitBinaryExpression(node) { + var isCommaOperator = node.operatorToken.kind !== 24 /* CommaToken */; + var indentBeforeOperator = needsIndentation(node, node.left, node.operatorToken); + var indentAfterOperator = needsIndentation(node, node.operatorToken, node.right); + emitExpression(node.left); + increaseIndentIf(indentBeforeOperator, isCommaOperator ? " " : undefined); + writeTokenText(node.operatorToken.kind); + increaseIndentIf(indentAfterOperator, " "); + emitExpression(node.right); + decreaseIndentIf(indentBeforeOperator, indentAfterOperator); + } + function emitConditionalExpression(node) { + var indentBeforeQuestion = needsIndentation(node, node.condition, node.questionToken); + var indentAfterQuestion = needsIndentation(node, node.questionToken, node.whenTrue); + var indentBeforeColon = needsIndentation(node, node.whenTrue, node.colonToken); + var indentAfterColon = needsIndentation(node, node.colonToken, node.whenFalse); + emitExpression(node.condition); + increaseIndentIf(indentBeforeQuestion, " "); + write("?"); + increaseIndentIf(indentAfterQuestion, " "); + emitExpression(node.whenTrue); + decreaseIndentIf(indentBeforeQuestion, indentAfterQuestion); + increaseIndentIf(indentBeforeColon, " "); + write(":"); + increaseIndentIf(indentAfterColon, " "); + emitExpression(node.whenFalse); + decreaseIndentIf(indentBeforeColon, indentAfterColon); + } + function emitTemplateExpression(node) { + emit(node.head); + emitList(node, node.templateSpans, 131072 /* TemplateExpressionSpans */); + } + function emitYieldExpression(node) { + write(node.asteriskToken ? "yield*" : "yield"); + emitExpressionWithPrefix(" ", node.expression); + } + function emitSpreadElementExpression(node) { + write("..."); + emitExpression(node.expression); + } + function emitClassExpression(node) { + emitClassDeclarationOrExpression(node); } - function emitEmitHelpers(node) { - // Only emit helpers if the user did not say otherwise. - if (compilerOptions.noEmitHelpers) { - return false; - } - // Don't emit helpers if we can import them. - if (compilerOptions.importHelpers - && (ts.isExternalModule(node) || compilerOptions.isolatedModules)) { - return false; - } - var helpersEmitted = false; - // Only Emit __extends function when target ES5. - // For target ES6 and above, we can emit classDeclaration as is. - if ((languageVersion < 2 /* ES6 */) && (!extendsEmitted && node.flags & 1024 /* HasClassExtends */)) { - writeLines(extendsHelper); - extendsEmitted = true; - helpersEmitted = true; - } - if (compilerOptions.jsx !== 1 /* Preserve */ && !assignEmitted && (node.flags & 16384 /* HasJsxSpreadAttributes */)) { - writeLines(assignHelper); - assignEmitted = true; - } - if (!decorateEmitted && node.flags & 2048 /* HasDecorators */) { - writeLines(decorateHelper); - if (compilerOptions.emitDecoratorMetadata) { - writeLines(metadataHelper); - } - decorateEmitted = true; - helpersEmitted = true; - } - if (!paramEmitted && node.flags & 4096 /* HasParamDecorators */) { - writeLines(paramHelper); - paramEmitted = true; - helpersEmitted = true; - } - if (!awaiterEmitted && node.flags & 8192 /* HasAsyncFunctions */) { - writeLines(awaiterHelper); - if (languageVersion < 2 /* ES6 */) { - writeLines(generatorHelper); - } - awaiterEmitted = true; - helpersEmitted = true; - } - if (helpersEmitted) { - writeLine(); - } - return helpersEmitted; + function emitExpressionWithTypeArguments(node) { + emitExpression(node.expression); + emitTypeArguments(node, node.typeArguments); } - function writeLines(text) { - var lines = text.split(/\r\n|\r|\n/g); - for (var i = 0; i < lines.length; i++) { - var line = lines[i]; - if (line.length) { - if (i > 0) { - writeLine(); - } - write(line); - } + function emitAsExpression(node) { + emitExpression(node.expression); + if (node.type) { + write(" as "); + emit(node.type); } } + function emitNonNullExpression(node) { + emitExpression(node.expression); + write("!"); + } // - // Helpers + // Misc // - function emitShebang() { - var shebang = ts.getShebang(currentText); - if (shebang) { - write(shebang); - writeLine(); - } + function emitTemplateSpan(node) { + emitExpression(node.expression); + emit(node.literal); } - function emitModifiers(node, modifiers) { - if (modifiers && modifiers.length) { - emitList(node, modifiers, 256 /* Modifiers */); + // + // Statements + // + function emitBlock(node, format) { + if (isSingleLineEmptyBlock(node)) { + writeToken(15 /* OpenBraceToken */, node.pos, /*contextNode*/ node); write(" "); + writeToken(16 /* CloseBraceToken */, node.statements.end, /*contextNode*/ node); + } + else { + writeToken(15 /* OpenBraceToken */, node.pos, /*contextNode*/ node); + emitBlockStatements(node); + writeToken(16 /* CloseBraceToken */, node.statements.end, /*contextNode*/ node); } } - function emitWithPrefix(prefix, node) { - emitNodeWithPrefix(prefix, node, emit); + function emitBlockStatements(node) { + if (ts.getEmitFlags(node) & 32 /* SingleLine */) { + emitList(node, node.statements, 384 /* SingleLineBlockStatements */); + } + else { + emitList(node, node.statements, 65 /* MultiLineBlockStatements */); + } } - function emitExpressionWithPrefix(prefix, node) { - emitNodeWithPrefix(prefix, node, emitExpression); + function emitVariableStatement(node) { + emitModifiers(node, node.modifiers); + emit(node.declarationList); + write(";"); } - function emitNodeWithPrefix(prefix, node, emit) { - if (node) { - write(prefix); - emit(node); - } + function emitEmptyStatement(node) { + write(";"); } - function emitWithSuffix(node, suffix) { - if (node) { - emit(node); - write(suffix); - } + function emitExpressionStatement(node) { + emitExpression(node.expression); + write(";"); } - function tryEmitSubstitute(node, emitNode, isExpression) { - if (isSubstitutionEnabled(node) && (node.emitFlags & 128 /* NoSubstitution */) === 0) { - var substitute = onSubstituteNode(node, isExpression); - if (substitute !== node) { - substitute.emitFlags |= 128 /* NoSubstitution */; - emitNode(substitute); - return true; + function emitIfStatement(node) { + var openParenPos = writeToken(88 /* IfKeyword */, node.pos, node); + write(" "); + writeToken(17 /* OpenParenToken */, openParenPos, node); + emitExpression(node.expression); + writeToken(18 /* CloseParenToken */, node.expression.end, node); + emitEmbeddedStatement(node.thenStatement); + if (node.elseStatement) { + writeLine(); + writeToken(80 /* ElseKeyword */, node.thenStatement.end, node); + if (node.elseStatement.kind === 203 /* IfStatement */) { + write(" "); + emit(node.elseStatement); } - } - return false; - } - function tryEmitConstantValue(node) { - var constantValue = tryGetConstEnumValue(node); - if (constantValue !== undefined) { - write(String(constantValue)); - if (!compilerOptions.removeComments) { - var propertyName = ts.isPropertyAccessExpression(node) - ? ts.declarationNameToString(node.name) - : getTextOfNode(node.argumentExpression); - write(" /* " + propertyName + " */"); + else { + emitEmbeddedStatement(node.elseStatement); } - return true; } - return false; } - function emitEmbeddedStatement(node) { - if (ts.isBlock(node)) { + function emitDoStatement(node) { + write("do"); + emitEmbeddedStatement(node.statement); + if (ts.isBlock(node.statement)) { write(" "); - emit(node); } else { writeLine(); - increaseIndent(); - emit(node); - decreaseIndent(); } + write("while ("); + emitExpression(node.expression); + write(");"); } - function emitDecorators(parentNode, decorators) { - emitList(parentNode, decorators, 24577 /* Decorators */); + function emitWhileStatement(node) { + write("while ("); + emitExpression(node.expression); + write(")"); + emitEmbeddedStatement(node.statement); } - function emitTypeArguments(parentNode, typeArguments) { - emitList(parentNode, typeArguments, 26960 /* TypeArguments */); + function emitForStatement(node) { + var openParenPos = writeToken(86 /* ForKeyword */, node.pos); + write(" "); + writeToken(17 /* OpenParenToken */, openParenPos, /*contextNode*/ node); + emitForBinding(node.initializer); + write(";"); + emitExpressionWithPrefix(" ", node.condition); + write(";"); + emitExpressionWithPrefix(" ", node.incrementor); + write(")"); + emitEmbeddedStatement(node.statement); } - function emitTypeParameters(parentNode, typeParameters) { - emitList(parentNode, typeParameters, 26960 /* TypeParameters */); + function emitForInStatement(node) { + var openParenPos = writeToken(86 /* ForKeyword */, node.pos); + write(" "); + writeToken(17 /* OpenParenToken */, openParenPos); + emitForBinding(node.initializer); + write(" in "); + emitExpression(node.expression); + writeToken(18 /* CloseParenToken */, node.expression.end); + emitEmbeddedStatement(node.statement); } - function emitParameters(parentNode, parameters) { - emitList(parentNode, parameters, 1360 /* Parameters */); + function emitForOfStatement(node) { + var openParenPos = writeToken(86 /* ForKeyword */, node.pos); + write(" "); + writeToken(17 /* OpenParenToken */, openParenPos); + emitForBinding(node.initializer); + write(" of "); + emitExpression(node.expression); + writeToken(18 /* CloseParenToken */, node.expression.end); + emitEmbeddedStatement(node.statement); } - function emitParametersForArrow(parentNode, parameters) { - if (parameters && - parameters.length === 1 && - parameters[0].type === undefined && - parameters[0].pos === parentNode.pos) { - emit(parameters[0]); - } - else { - emitParameters(parentNode, parameters); + function emitForBinding(node) { + if (node !== undefined) { + if (node.kind === 219 /* VariableDeclarationList */) { + emit(node); + } + else { + emitExpression(node); + } } } - function emitParametersForIndexSignature(parentNode, parameters) { - emitList(parentNode, parameters, 4432 /* IndexSignatureParameters */); + function emitContinueStatement(node) { + writeToken(75 /* ContinueKeyword */, node.pos); + emitWithPrefix(" ", node.label); + write(";"); } - function emitList(parentNode, children, format, start, count) { - emitNodeList(emit, parentNode, children, format, start, count); + function emitBreakStatement(node) { + writeToken(70 /* BreakKeyword */, node.pos); + emitWithPrefix(" ", node.label); + write(";"); } - function emitExpressionList(parentNode, children, format, start, count) { - emitNodeList(emitExpression, parentNode, children, format, start, count); + function emitReturnStatement(node) { + writeToken(94 /* ReturnKeyword */, node.pos, /*contextNode*/ node); + emitExpressionWithPrefix(" ", node.expression); + write(";"); } - function emitNodeList(emit, parentNode, children, format, start, count) { - if (start === void 0) { start = 0; } - if (count === void 0) { count = children ? children.length - start : 0; } - var isUndefined = children === undefined; - if (isUndefined && format & 8192 /* OptionalIfUndefined */) { - return; - } - var isEmpty = isUndefined || children.length === 0 || start >= children.length || count === 0; - if (isEmpty && format & 16384 /* OptionalIfEmpty */) { - return; - } - if (format & 7680 /* BracketsMask */) { - write(getOpeningBracket(format)); - } - if (isEmpty) { - // Write a line terminator if the parent node was multi-line - if (format & 1 /* MultiLine */) { - writeLine(); - } - else if (format & 128 /* SpaceBetweenBraces */) { - write(" "); - } + function emitWithStatement(node) { + write("with ("); + emitExpression(node.expression); + write(")"); + emitEmbeddedStatement(node.statement); + } + function emitSwitchStatement(node) { + var openParenPos = writeToken(96 /* SwitchKeyword */, node.pos); + write(" "); + writeToken(17 /* OpenParenToken */, openParenPos); + emitExpression(node.expression); + writeToken(18 /* CloseParenToken */, node.expression.end); + write(" "); + emit(node.caseBlock); + } + function emitLabeledStatement(node) { + emit(node.label); + write(": "); + emit(node.statement); + } + function emitThrowStatement(node) { + write("throw"); + emitExpressionWithPrefix(" ", node.expression); + write(";"); + } + function emitTryStatement(node) { + write("try "); + emit(node.tryBlock); + emit(node.catchClause); + if (node.finallyBlock) { + writeLine(); + write("finally "); + emit(node.finallyBlock); } - else { - // Write the opening line terminator or leading whitespace. - var mayEmitInterveningComments = (format & 131072 /* NoInterveningComments */) === 0; - var shouldEmitInterveningComments = mayEmitInterveningComments; - if (shouldWriteLeadingLineTerminator(parentNode, children, format)) { - writeLine(); - shouldEmitInterveningComments = false; - } - else if (format & 128 /* SpaceBetweenBraces */) { - write(" "); - } - // Increase the indent, if requested. - if (format & 64 /* Indented */) { - increaseIndent(); - } - // Emit each child. - var previousSibling = void 0; - var shouldDecreaseIndentAfterEmit = void 0; - var delimiter = getDelimiter(format); - for (var i = 0; i < count; i++) { - var child = children[start + i]; - // Write the delimiter if this is not the first node. - if (previousSibling) { - write(delimiter); - // Write either a line terminator or whitespace to separate the elements. - if (shouldWriteSeparatingLineTerminator(previousSibling, child, format)) { - // If a synthesized node in a single-line list starts on a new - // line, we should increase the indent. - if ((format & (3 /* LinesMask */ | 64 /* Indented */)) === 0 /* SingleLine */) { - increaseIndent(); - shouldDecreaseIndentAfterEmit = true; - } - writeLine(); - shouldEmitInterveningComments = false; - } - else if (previousSibling && format & 256 /* SpaceBetweenSiblings */) { - write(" "); - } + } + function emitDebuggerStatement(node) { + writeToken(76 /* DebuggerKeyword */, node.pos); + write(";"); + } + // + // Declarations + // + function emitVariableDeclaration(node) { + emit(node.name); + emitExpressionWithPrefix(" = ", node.initializer); + } + function emitVariableDeclarationList(node) { + write(ts.isLet(node) ? "let " : ts.isConst(node) ? "const " : "var "); + emitList(node, node.declarations, 272 /* VariableDeclarationList */); + } + function emitFunctionDeclaration(node) { + emitFunctionDeclarationOrExpression(node); + } + function emitFunctionDeclarationOrExpression(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + write(node.asteriskToken ? "function* " : "function "); + emitIdentifierName(node.name); + emitSignatureAndBody(node, emitSignatureHead); + } + function emitSignatureAndBody(node, emitSignatureHead) { + var body = node.body; + if (body) { + if (ts.isBlock(body)) { + var indentedFlag = ts.getEmitFlags(node) & 524288 /* Indented */; + if (indentedFlag) { + increaseIndent(); } - if (shouldEmitInterveningComments) { - var commentRange = child.commentRange || child; - emitTrailingCommentsOfPosition(commentRange.pos); + if (ts.getEmitFlags(node) & 4194304 /* ReuseTempVariableScope */) { + emitSignatureHead(node); + emitBlockFunctionBody(node, body); } else { - shouldEmitInterveningComments = mayEmitInterveningComments; + var savedTempFlags = tempFlags; + tempFlags = 0; + emitSignatureHead(node); + emitBlockFunctionBody(node, body); + tempFlags = savedTempFlags; } - // Emit this child. - emit(child); - if (shouldDecreaseIndentAfterEmit) { + if (indentedFlag) { decreaseIndent(); - shouldDecreaseIndentAfterEmit = false; } - previousSibling = child; - } - // Write a trailing comma, if requested. - var hasTrailingComma = (format & 32 /* AllowTrailingComma */) && children.hasTrailingComma; - if (format & 16 /* CommaDelimited */ && hasTrailingComma) { - write(","); - } - // Decrease the indent, if requested. - if (format & 64 /* Indented */) { - decreaseIndent(); - } - // Write the closing line terminator or closing whitespace. - if (shouldWriteClosingLineTerminator(parentNode, children, format)) { - writeLine(); } - else if (format & 128 /* SpaceBetweenBraces */) { + else { + emitSignatureHead(node); write(" "); + emitExpression(body); } } - if (format & 7680 /* BracketsMask */) { - write(getClosingBracket(format)); - } - } - function writeIfAny(nodes, text) { - if (nodes && nodes.length > 0) { - write(text); - } - } - function writeIfPresent(node, text) { - if (node !== undefined) { - write(text); - } - } - function writeToken(token, pos, contextNode) { - var tokenStartPos = emitTokenStart(token, pos, contextNode, shouldSkipLeadingSourceMapForToken, getTokenSourceMapRange); - var tokenEndPos = writeTokenText(token, tokenStartPos); - return emitTokenEnd(token, tokenEndPos, contextNode, shouldSkipTrailingSourceMapForToken, getTokenSourceMapRange); - } - function shouldSkipLeadingSourceMapForToken(contextNode) { - return (contextNode.emitFlags & 4096 /* NoTokenLeadingSourceMaps */) !== 0; - } - function shouldSkipTrailingSourceMapForToken(contextNode) { - return (contextNode.emitFlags & 8192 /* NoTokenTrailingSourceMaps */) !== 0; - } - function writeTokenText(token, pos) { - var tokenString = ts.tokenToString(token); - write(tokenString); - return ts.positionIsSynthesized(pos) ? -1 : pos + tokenString.length; - } - function writeTokenNode(node) { - if (node) { - emitStart(/*range*/ node, /*contextNode*/ node, shouldSkipLeadingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange); - writeTokenText(node.kind); - emitEnd(/*range*/ node, /*contextNode*/ node, shouldSkipTrailingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange); - } - } - function increaseIndentIf(value, valueToWriteWhenNotIndenting) { - if (value) { - increaseIndent(); - writeLine(); - } - else if (valueToWriteWhenNotIndenting) { - write(valueToWriteWhenNotIndenting); + else { + emitSignatureHead(node); + write(";"); } } - // Helper function to decrease the indent if we previously indented. Allows multiple - // previous indent values to be considered at a time. This also allows caller to just - // call this once, passing in all their appropriate indent values, instead of needing - // to call this helper function multiple times. - function decreaseIndentIf(value1, value2) { - if (value1) { - decreaseIndent(); - } - if (value2) { - decreaseIndent(); - } + function emitSignatureHead(node) { + emitTypeParameters(node, node.typeParameters); + emitParameters(node, node.parameters); + emitWithPrefix(": ", node.type); } - function shouldWriteLeadingLineTerminator(parentNode, children, format) { - if (format & 1 /* MultiLine */) { + function shouldEmitBlockFunctionBodyOnSingleLine(parentNode, body) { + // We must emit a function body as a single-line body in the following case: + // * The body has NodeEmitFlags.SingleLine specified. + // We must emit a function body as a multi-line body in the following cases: + // * The body is explicitly marked as multi-line. + // * A non-synthesized body's start and end position are on different lines. + // * Any statement in the body starts on a new line. + if (ts.getEmitFlags(body) & 32 /* SingleLine */) { return true; } - if (format & 2 /* PreserveLines */) { - if (format & 32768 /* PreferNewLine */) { - return true; - } - var firstChild = children[0]; - if (firstChild === undefined) { - return !ts.rangeIsOnSingleLine(parentNode, currentSourceFile); - } - else if (ts.positionIsSynthesized(parentNode.pos) || ts.nodeIsSynthesized(firstChild)) { - return synthesizedNodeStartsOnNewLine(firstChild, format); - } - else { - return !ts.rangeStartPositionsAreOnSameLine(parentNode, firstChild, currentSourceFile); - } + if (body.multiLine) { + return false; } - else { + if (!ts.nodeIsSynthesized(body) && !ts.rangeIsOnSingleLine(body, currentSourceFile)) { return false; } - } - function shouldWriteSeparatingLineTerminator(previousNode, nextNode, format) { - if (format & 1 /* MultiLine */) { - return true; + if (shouldWriteLeadingLineTerminator(body, body.statements, 2 /* PreserveLines */) + || shouldWriteClosingLineTerminator(body, body.statements, 2 /* PreserveLines */)) { + return false; } - else if (format & 2 /* PreserveLines */) { - if (previousNode === undefined || nextNode === undefined) { + var previousStatement; + for (var _a = 0, _b = body.statements; _a < _b.length; _a++) { + var statement = _b[_a]; + if (shouldWriteSeparatingLineTerminator(previousStatement, statement, 2 /* PreserveLines */)) { return false; } - else if (ts.nodeIsSynthesized(previousNode) || ts.nodeIsSynthesized(nextNode)) { - return synthesizedNodeStartsOnNewLine(previousNode, format) || synthesizedNodeStartsOnNewLine(nextNode, format); - } - else { - return !ts.rangeEndIsOnSameLineAsRangeStart(previousNode, nextNode, currentSourceFile); - } + previousStatement = statement; + } + return true; + } + function emitBlockFunctionBody(parentNode, body) { + write(" {"); + increaseIndent(); + emitBodyWithDetachedComments(body, body.statements, shouldEmitBlockFunctionBodyOnSingleLine(parentNode, body) + ? emitBlockFunctionBodyOnSingleLine + : emitBlockFunctionBodyWorker); + decreaseIndent(); + writeToken(16 /* CloseBraceToken */, body.statements.end, body); + } + function emitBlockFunctionBodyOnSingleLine(body) { + emitBlockFunctionBodyWorker(body, /*emitBlockFunctionBodyOnSingleLine*/ true); + } + function emitBlockFunctionBodyWorker(body, emitBlockFunctionBodyOnSingleLine) { + // Emit all the prologue directives (like "use strict"). + var statementOffset = emitPrologueDirectives(body.statements, /*startWithNewLine*/ true); + var helpersEmitted = emitHelpers(body); + if (statementOffset === 0 && !helpersEmitted && emitBlockFunctionBodyOnSingleLine) { + decreaseIndent(); + emitList(body, body.statements, 384 /* SingleLineFunctionBodyStatements */); + increaseIndent(); } else { - return nextNode.startsOnNewLine; + emitList(body, body.statements, 1 /* MultiLineFunctionBodyStatements */, statementOffset); } } - function shouldWriteClosingLineTerminator(parentNode, children, format) { - if (format & 1 /* MultiLine */) { - return (format & 65536 /* NoTrailingNewLine */) === 0; - } - else if (format & 2 /* PreserveLines */) { - if (format & 32768 /* PreferNewLine */) { - return true; - } - var lastChild = ts.lastOrUndefined(children); - if (lastChild === undefined) { - return !ts.rangeIsOnSingleLine(parentNode, currentSourceFile); - } - else if (ts.positionIsSynthesized(parentNode.pos) || ts.nodeIsSynthesized(lastChild)) { - return synthesizedNodeStartsOnNewLine(lastChild, format); - } - else { - return !ts.rangeEndPositionsAreOnSameLine(parentNode, lastChild, currentSourceFile); - } + function emitClassDeclaration(node) { + emitClassDeclarationOrExpression(node); + } + function emitClassDeclarationOrExpression(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + write("class"); + emitNodeWithPrefix(" ", node.name, emitIdentifierName); + var indentedFlag = ts.getEmitFlags(node) & 524288 /* Indented */; + if (indentedFlag) { + increaseIndent(); } - else { - return false; + emitTypeParameters(node, node.typeParameters); + emitList(node, node.heritageClauses, 256 /* ClassHeritageClauses */); + var savedTempFlags = tempFlags; + tempFlags = 0; + write(" {"); + emitList(node, node.members, 65 /* ClassMembers */); + write("}"); + if (indentedFlag) { + decreaseIndent(); } + tempFlags = savedTempFlags; } - function synthesizedNodeStartsOnNewLine(node, format) { - if (ts.nodeIsSynthesized(node)) { - var startsOnNewLine = node.startsOnNewLine; - if (startsOnNewLine === undefined) { - return (format & 32768 /* PreferNewLine */) !== 0; - } - return startsOnNewLine; - } - return (format & 32768 /* PreferNewLine */) !== 0; + function emitInterfaceDeclaration(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + write("interface "); + emit(node.name); + emitTypeParameters(node, node.typeParameters); + emitList(node, node.heritageClauses, 256 /* HeritageClauses */); + write(" {"); + emitList(node, node.members, 65 /* InterfaceMembers */); + write("}"); } - function needsIndentation(parent, node1, node2) { - parent = skipSynthesizedParentheses(parent); - node1 = skipSynthesizedParentheses(node1); - node2 = skipSynthesizedParentheses(node2); - // Always use a newline for synthesized code if the synthesizer desires it. - if (node2.startsOnNewLine) { - return true; - } - return !ts.nodeIsSynthesized(parent) - && !ts.nodeIsSynthesized(node1) - && !ts.nodeIsSynthesized(node2) - && !ts.rangeEndIsOnSameLineAsRangeStart(node1, node2, currentSourceFile); + function emitTypeAliasDeclaration(node) { + emitDecorators(node, node.decorators); + emitModifiers(node, node.modifiers); + write("type "); + emit(node.name); + emitTypeParameters(node, node.typeParameters); + write(" = "); + emit(node.type); + write(";"); } - function skipSynthesizedParentheses(node) { - while (node.kind === 178 /* ParenthesizedExpression */ && ts.nodeIsSynthesized(node)) { - node = node.expression; + function emitEnumDeclaration(node) { + emitModifiers(node, node.modifiers); + write("enum "); + emit(node.name); + var savedTempFlags = tempFlags; + tempFlags = 0; + write(" {"); + emitList(node, node.members, 81 /* EnumMembers */); + write("}"); + tempFlags = savedTempFlags; + } + function emitModuleDeclaration(node) { + emitModifiers(node, node.modifiers); + write(node.flags & 16 /* Namespace */ ? "namespace " : "module "); + emit(node.name); + var body = node.body; + while (body.kind === 225 /* ModuleDeclaration */) { + write("."); + emit(body.name); + body = body.body; } - return node; + write(" "); + emit(body); } - function getTextOfNode(node, includeTrivia) { - if (ts.isGeneratedIdentifier(node)) { - return getGeneratedIdentifier(node); + function emitModuleBlock(node) { + if (isEmptyBlock(node)) { + write("{ }"); } - else if (ts.isIdentifier(node) && (ts.nodeIsSynthesized(node) || !node.parent)) { - return ts.unescapeIdentifier(node.text); + else { + var savedTempFlags = tempFlags; + tempFlags = 0; + write("{"); + increaseIndent(); + emitBlockStatements(node); + write("}"); + tempFlags = savedTempFlags; } - else if (node.kind === 9 /* StringLiteral */ && node.textSourceNode) { - return getTextOfNode(node.textSourceNode, includeTrivia); + } + function emitCaseBlock(node) { + writeToken(15 /* OpenBraceToken */, node.pos); + emitList(node, node.clauses, 65 /* CaseBlockClauses */); + writeToken(16 /* CloseBraceToken */, node.clauses.end); + } + function emitImportEqualsDeclaration(node) { + emitModifiers(node, node.modifiers); + write("import "); + emit(node.name); + write(" = "); + emitModuleReference(node.moduleReference); + write(";"); + } + function emitModuleReference(node) { + if (node.kind === 69 /* Identifier */) { + emitExpression(node); } - else if (ts.isLiteralExpression(node) && (ts.nodeIsSynthesized(node) || !node.parent)) { - return node.text; + else { + emit(node); } - return ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, node, includeTrivia); } - function getLiteralTextOfNode(node) { - if (node.kind === 9 /* StringLiteral */ && node.textSourceNode) { - var textSourceNode = node.textSourceNode; - if (ts.isIdentifier(textSourceNode)) { - return "\"" + ts.escapeNonAsciiCharacters(ts.escapeString(getTextOfNode(textSourceNode))) + "\""; - } - else { - return getLiteralTextOfNode(textSourceNode); - } + function emitImportDeclaration(node) { + emitModifiers(node, node.modifiers); + write("import "); + if (node.importClause) { + emit(node.importClause); + write(" from "); } - return ts.getLiteralText(node, currentSourceFile, languageVersion); + emitExpression(node.moduleSpecifier); + write(";"); } - function tryGetConstEnumValue(node) { - if (compilerOptions.isolatedModules) { - return undefined; + function emitImportClause(node) { + emit(node.name); + if (node.name && node.namedBindings) { + write(", "); } - return ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node) - ? resolver.getConstantValue(node) - : undefined; + emit(node.namedBindings); } - function isSingleLineEmptyBlock(block) { - return !block.multiLine - && block.statements.length === 0 - && ts.rangeEndIsOnSameLineAsRangeStart(block, block, currentSourceFile); + function emitNamespaceImport(node) { + write("* as "); + emit(node.name); } - function isUniqueName(name) { - return !resolver.hasGlobalName(name) && - !ts.hasProperty(currentFileIdentifiers, name) && - !ts.hasProperty(generatedNameSet, name); + function emitNamedImports(node) { + emitNamedImportsOrExports(node); } - function isUniqueLocalName(name, container) { - for (var node = container; ts.isNodeDescendantOf(node, container); node = node.nextContainer) { - if (node.locals && ts.hasProperty(node.locals, name)) { - // We conservatively include alias symbols to cover cases where they're emitted as locals - if (node.locals[name].flags & (107455 /* Value */ | 1048576 /* ExportValue */ | 8388608 /* Alias */)) { - return false; - } - } - } - return true; + function emitImportSpecifier(node) { + emitImportOrExportSpecifier(node); } - /** - * Return the next available name in the pattern _a ... _z, _0, _1, ... - * TempFlags._i or TempFlags._n may be used to express a preference for that dedicated name. - * Note that names generated by makeTempVariableName and makeUniqueName will never conflict. - */ - function makeTempVariableName(flags) { - if (flags && !(tempFlags & flags)) { - var name_41 = flags === 268435456 /* _i */ ? "_i" : "_n"; - if (isUniqueName(name_41)) { - tempFlags |= flags; - return name_41; - } - } - while (true) { - var count = tempFlags & 268435455 /* CountMask */; - tempFlags++; - // Skip over 'i' and 'n' - if (count !== 8 && count !== 13) { - var name_42 = count < 26 - ? "_" + String.fromCharCode(97 /* a */ + count) - : "_" + (count - 26); - if (isUniqueName(name_42)) { - return name_42; - } - } - } + function emitExportAssignment(node) { + write(node.isExportEquals ? "export = " : "export default "); + emitExpression(node.expression); + write(";"); } - // Generate a name that is unique within the current file and doesn't conflict with any names - // in global scope. The name is formed by adding an '_n' suffix to the specified base name, - // where n is a positive integer. Note that names generated by makeTempVariableName and - // makeUniqueName are guaranteed to never conflict. - function makeUniqueName(baseName) { - // Find the first unique 'name_n', where n is a positive number - if (baseName.charCodeAt(baseName.length - 1) !== 95 /* _ */) { - baseName += "_"; + function emitExportDeclaration(node) { + write("export "); + if (node.exportClause) { + emit(node.exportClause); } - var i = 1; - while (true) { - var generatedName = baseName + i; - if (isUniqueName(generatedName)) { - return generatedNameSet[generatedName] = generatedName; - } - i++; + else { + write("*"); } + if (node.moduleSpecifier) { + write(" from "); + emitExpression(node.moduleSpecifier); + } + write(";"); } - function generateNameForModuleOrEnum(node) { - var name = getTextOfNode(node.name); - // Use module/enum name itself if it is unique, otherwise make a unique variation - return isUniqueLocalName(name, node) ? name : makeUniqueName(name); - } - function generateNameForImportOrExportDeclaration(node) { - var expr = ts.getExternalModuleName(node); - var baseName = expr.kind === 9 /* StringLiteral */ ? - ts.escapeIdentifier(ts.makeIdentifierFromModuleName(expr.text)) : "module"; - return makeUniqueName(baseName); + function emitNamedExports(node) { + emitNamedImportsOrExports(node); } - function generateNameForExportDefault() { - return makeUniqueName("default"); + function emitExportSpecifier(node) { + emitImportOrExportSpecifier(node); } - function generateNameForClassExpression() { - return makeUniqueName("class"); + function emitNamedImportsOrExports(node) { + write("{"); + emitList(node, node.elements, 432 /* NamedImportsOrExportsElements */); + write("}"); } - /** - * Generates a unique name from a node. - * - * @param node A node. - */ - function generateNameForNode(node) { - switch (node.kind) { - case 69 /* Identifier */: - return makeUniqueName(getTextOfNode(node)); - case 225 /* ModuleDeclaration */: - case 224 /* EnumDeclaration */: - return generateNameForModuleOrEnum(node); - case 230 /* ImportDeclaration */: - case 236 /* ExportDeclaration */: - return generateNameForImportOrExportDeclaration(node); - case 220 /* FunctionDeclaration */: - case 221 /* ClassDeclaration */: - case 235 /* ExportAssignment */: - return generateNameForExportDefault(); - case 192 /* ClassExpression */: - return generateNameForClassExpression(); - default: - return makeTempVariableName(0 /* Auto */); + function emitImportOrExportSpecifier(node) { + if (node.propertyName) { + emit(node.propertyName); + write(" as "); } + emit(node.name); } - /** - * Generates a unique identifier for a node. - * - * @param name A generated name. - */ - function generateName(name) { - switch (name.autoGenerateKind) { - case 1 /* Auto */: - return makeTempVariableName(0 /* Auto */); - case 2 /* Loop */: - return makeTempVariableName(268435456 /* _i */); - case 3 /* Unique */: - return makeUniqueName(name.text); - } - ts.Debug.fail("Unsupported GeneratedIdentifierKind."); + // + // Module references + // + function emitExternalModuleReference(node) { + write("require("); + emitExpression(node.expression); + write(")"); } - /** - * Gets the node from which a name should be generated. - * - * @param name A generated name wrapper. - */ - function getNodeForGeneratedName(name) { - var autoGenerateId = name.autoGenerateId; - var node = name; - var original = node.original; - while (original) { - node = original; - // if "node" is a different generated name (having a different - // "autoGenerateId"), use it and stop traversing. - if (ts.isIdentifier(node) - && node.autoGenerateKind === 4 /* Node */ - && node.autoGenerateId !== autoGenerateId) { - break; - } - original = node.original; - } - // otherwise, return the original node for the source; - return node; + // + // JSX + // + function emitJsxElement(node) { + emit(node.openingElement); + emitList(node, node.children, 131072 /* JsxElementChildren */); + emit(node.closingElement); } - /** - * Gets the generated identifier text from a generated identifier. - * - * @param name The generated identifier. - */ - function getGeneratedIdentifier(name) { - if (name.autoGenerateKind === 4 /* Node */) { - // Generated names generate unique names based on their original node - // and are cached based on that node's id - var node = getNodeForGeneratedName(name); - var nodeId = ts.getNodeId(node); - return nodeIdToGeneratedName[nodeId] || (nodeIdToGeneratedName[nodeId] = ts.unescapeIdentifier(generateNameForNode(node))); - } - else { - // Auto, Loop, and Unique names are cached based on their unique - // autoGenerateId. - var autoGenerateId = name.autoGenerateId; - return autoGeneratedIdToGeneratedName[autoGenerateId] || (autoGeneratedIdToGeneratedName[autoGenerateId] = ts.unescapeIdentifier(generateName(name))); - } + function emitJsxSelfClosingElement(node) { + write("<"); + emitJsxTagName(node.tagName); + write(" "); + emitList(node, node.attributes, 131328 /* JsxElementAttributes */); + write("/>"); } - function createDelimiterMap() { - var delimiters = []; - delimiters[0 /* None */] = ""; - delimiters[16 /* CommaDelimited */] = ","; - delimiters[4 /* BarDelimited */] = " |"; - delimiters[8 /* AmpersandDelimited */] = " &"; - return delimiters; + function emitJsxOpeningElement(node) { + write("<"); + emitJsxTagName(node.tagName); + writeIfAny(node.attributes, " "); + emitList(node, node.attributes, 131328 /* JsxElementAttributes */); + write(">"); } - function getDelimiter(format) { - return delimiters[format & 28 /* DelimitersMask */]; + function emitJsxText(node) { + writer.writeLiteral(getTextOfNode(node, /*includeTrivia*/ true)); } - function createBracketsMap() { - var brackets = []; - brackets[512 /* Braces */] = ["{", "}"]; - brackets[1024 /* Parenthesis */] = ["(", ")"]; - brackets[2048 /* AngleBrackets */] = ["<", ">"]; - brackets[4096 /* SquareBrackets */] = ["[", "]"]; - return brackets; + function emitJsxClosingElement(node) { + write(""); } - function getOpeningBracket(format) { - return brackets[format & 7680 /* BracketsMask */][0]; + function emitJsxAttribute(node) { + emit(node.name); + emitWithPrefix("=", node.initializer); } - function getClosingBracket(format) { - return brackets[format & 7680 /* BracketsMask */][1]; + function emitJsxSpreadAttribute(node) { + write("{..."); + emitExpression(node.expression); + write("}"); } - } - ts.emitFiles = emitFiles; - var ListFormat; - (function (ListFormat) { - ListFormat[ListFormat["None"] = 0] = "None"; - // Line separators - ListFormat[ListFormat["SingleLine"] = 0] = "SingleLine"; - ListFormat[ListFormat["MultiLine"] = 1] = "MultiLine"; - ListFormat[ListFormat["PreserveLines"] = 2] = "PreserveLines"; - ListFormat[ListFormat["LinesMask"] = 3] = "LinesMask"; - // Delimiters - ListFormat[ListFormat["NotDelimited"] = 0] = "NotDelimited"; - ListFormat[ListFormat["BarDelimited"] = 4] = "BarDelimited"; - ListFormat[ListFormat["AmpersandDelimited"] = 8] = "AmpersandDelimited"; - ListFormat[ListFormat["CommaDelimited"] = 16] = "CommaDelimited"; - ListFormat[ListFormat["DelimitersMask"] = 28] = "DelimitersMask"; - ListFormat[ListFormat["AllowTrailingComma"] = 32] = "AllowTrailingComma"; - // Whitespace - ListFormat[ListFormat["Indented"] = 64] = "Indented"; - ListFormat[ListFormat["SpaceBetweenBraces"] = 128] = "SpaceBetweenBraces"; - ListFormat[ListFormat["SpaceBetweenSiblings"] = 256] = "SpaceBetweenSiblings"; - // Brackets/Braces - ListFormat[ListFormat["Braces"] = 512] = "Braces"; - ListFormat[ListFormat["Parenthesis"] = 1024] = "Parenthesis"; - ListFormat[ListFormat["AngleBrackets"] = 2048] = "AngleBrackets"; - ListFormat[ListFormat["SquareBrackets"] = 4096] = "SquareBrackets"; - ListFormat[ListFormat["BracketsMask"] = 7680] = "BracketsMask"; - ListFormat[ListFormat["OptionalIfUndefined"] = 8192] = "OptionalIfUndefined"; - ListFormat[ListFormat["OptionalIfEmpty"] = 16384] = "OptionalIfEmpty"; - ListFormat[ListFormat["Optional"] = 24576] = "Optional"; - // Other - ListFormat[ListFormat["PreferNewLine"] = 32768] = "PreferNewLine"; - ListFormat[ListFormat["NoTrailingNewLine"] = 65536] = "NoTrailingNewLine"; - ListFormat[ListFormat["NoInterveningComments"] = 131072] = "NoInterveningComments"; - // Precomputed Formats - ListFormat[ListFormat["Modifiers"] = 256] = "Modifiers"; - ListFormat[ListFormat["HeritageClauses"] = 256] = "HeritageClauses"; - ListFormat[ListFormat["TypeLiteralMembers"] = 65] = "TypeLiteralMembers"; - ListFormat[ListFormat["TupleTypeElements"] = 336] = "TupleTypeElements"; - ListFormat[ListFormat["UnionTypeConstituents"] = 260] = "UnionTypeConstituents"; - ListFormat[ListFormat["IntersectionTypeConstituents"] = 264] = "IntersectionTypeConstituents"; - ListFormat[ListFormat["ObjectBindingPatternElements"] = 432] = "ObjectBindingPatternElements"; - ListFormat[ListFormat["ArrayBindingPatternElements"] = 304] = "ArrayBindingPatternElements"; - ListFormat[ListFormat["ObjectLiteralExpressionProperties"] = 978] = "ObjectLiteralExpressionProperties"; - ListFormat[ListFormat["ArrayLiteralExpressionElements"] = 4466] = "ArrayLiteralExpressionElements"; - ListFormat[ListFormat["CallExpressionArguments"] = 1296] = "CallExpressionArguments"; - ListFormat[ListFormat["NewExpressionArguments"] = 9488] = "NewExpressionArguments"; - ListFormat[ListFormat["TemplateExpressionSpans"] = 131072] = "TemplateExpressionSpans"; - ListFormat[ListFormat["SingleLineBlockStatements"] = 384] = "SingleLineBlockStatements"; - ListFormat[ListFormat["MultiLineBlockStatements"] = 65] = "MultiLineBlockStatements"; - ListFormat[ListFormat["VariableDeclarationList"] = 272] = "VariableDeclarationList"; - ListFormat[ListFormat["SingleLineFunctionBodyStatements"] = 384] = "SingleLineFunctionBodyStatements"; - ListFormat[ListFormat["MultiLineFunctionBodyStatements"] = 1] = "MultiLineFunctionBodyStatements"; - ListFormat[ListFormat["ClassHeritageClauses"] = 256] = "ClassHeritageClauses"; - ListFormat[ListFormat["ClassMembers"] = 65] = "ClassMembers"; - ListFormat[ListFormat["InterfaceMembers"] = 65] = "InterfaceMembers"; - ListFormat[ListFormat["EnumMembers"] = 81] = "EnumMembers"; - ListFormat[ListFormat["CaseBlockClauses"] = 65] = "CaseBlockClauses"; - ListFormat[ListFormat["NamedImportsOrExportsElements"] = 432] = "NamedImportsOrExportsElements"; - ListFormat[ListFormat["JsxElementChildren"] = 131072] = "JsxElementChildren"; - ListFormat[ListFormat["JsxElementAttributes"] = 131328] = "JsxElementAttributes"; - ListFormat[ListFormat["CaseOrDefaultClauseStatements"] = 81985] = "CaseOrDefaultClauseStatements"; - ListFormat[ListFormat["HeritageClauseTypes"] = 272] = "HeritageClauseTypes"; - ListFormat[ListFormat["SourceFileStatements"] = 65537] = "SourceFileStatements"; - ListFormat[ListFormat["Decorators"] = 24577] = "Decorators"; - ListFormat[ListFormat["TypeArguments"] = 26960] = "TypeArguments"; - ListFormat[ListFormat["TypeParameters"] = 26960] = "TypeParameters"; - ListFormat[ListFormat["Parameters"] = 1360] = "Parameters"; - ListFormat[ListFormat["IndexSignatureParameters"] = 4432] = "IndexSignatureParameters"; - })(ListFormat || (ListFormat = {})); -})(ts || (ts = {})); -/// -/// -/// -var ts; -(function (ts) { - /** The version of the TypeScript compiler release */ - ts.version = "2.1.0"; - var emptyArray = []; - function findConfigFile(searchPath, fileExists, configName) { - if (configName === void 0) { configName = "tsconfig.json"; } - while (true) { - var fileName = ts.combinePaths(searchPath, configName); - if (fileExists(fileName)) { - return fileName; + function emitJsxExpression(node) { + if (node.expression) { + write("{"); + emitExpression(node.expression); + write("}"); } - var parentPath = ts.getDirectoryPath(searchPath); - if (parentPath === searchPath) { - break; + } + function emitJsxTagName(node) { + if (node.kind === 69 /* Identifier */) { + emitExpression(node); + } + else { + emit(node); } - searchPath = parentPath; } - return undefined; - } - ts.findConfigFile = findConfigFile; - function resolveTripleslashReference(moduleName, containingFile) { - var basePath = ts.getDirectoryPath(containingFile); - var referencedFileName = ts.isRootedDiskPath(moduleName) ? moduleName : ts.combinePaths(basePath, moduleName); - return ts.normalizePath(referencedFileName); - } - ts.resolveTripleslashReference = resolveTripleslashReference; - /* @internal */ - function computeCommonSourceDirectoryOfFilenames(fileNames, currentDirectory, getCanonicalFileName) { - var commonPathComponents; - var failed = ts.forEach(fileNames, function (sourceFile) { - // Each file contributes into common source file path - var sourcePathComponents = ts.getNormalizedPathComponents(sourceFile, currentDirectory); - sourcePathComponents.pop(); // The base file name is not part of the common directory path - if (!commonPathComponents) { - // first file - commonPathComponents = sourcePathComponents; - return; + // + // Clauses + // + function emitCaseClause(node) { + write("case "); + emitExpression(node.expression); + write(":"); + emitCaseOrDefaultClauseStatements(node, node.statements); + } + function emitDefaultClause(node) { + write("default:"); + emitCaseOrDefaultClauseStatements(node, node.statements); + } + function emitCaseOrDefaultClauseStatements(parentNode, statements) { + var emitAsSingleStatement = statements.length === 1 && + ( + // treat synthesized nodes as located on the same line for emit purposes + ts.nodeIsSynthesized(parentNode) || + ts.nodeIsSynthesized(statements[0]) || + ts.rangeStartPositionsAreOnSameLine(parentNode, statements[0], currentSourceFile)); + if (emitAsSingleStatement) { + write(" "); + emit(statements[0]); } - for (var i = 0, n = Math.min(commonPathComponents.length, sourcePathComponents.length); i < n; i++) { - if (getCanonicalFileName(commonPathComponents[i]) !== getCanonicalFileName(sourcePathComponents[i])) { - if (i === 0) { - // Failed to find any common path component - return true; - } - // New common path found that is 0 -> i-1 - commonPathComponents.length = i; - break; - } + else { + emitList(parentNode, statements, 81985 /* CaseOrDefaultClauseStatements */); } - // If the sourcePathComponents was shorter than the commonPathComponents, truncate to the sourcePathComponents - if (sourcePathComponents.length < commonPathComponents.length) { - commonPathComponents.length = sourcePathComponents.length; + } + function emitHeritageClause(node) { + write(" "); + writeTokenText(node.token); + write(" "); + emitList(node, node.types, 272 /* HeritageClauseTypes */); + } + function emitCatchClause(node) { + writeLine(); + var openParenPos = writeToken(72 /* CatchKeyword */, node.pos); + write(" "); + writeToken(17 /* OpenParenToken */, openParenPos); + emit(node.variableDeclaration); + writeToken(18 /* CloseParenToken */, node.variableDeclaration ? node.variableDeclaration.end : openParenPos); + write(" "); + emit(node.block); + } + // + // Property assignments + // + function emitPropertyAssignment(node) { + emit(node.name); + write(": "); + // This is to ensure that we emit comment in the following case: + // For example: + // obj = { + // id: /*comment1*/ ()=>void + // } + // "comment1" is not considered to be leading comment for node.initializer + // but rather a trailing comment on the previous node. + var initializer = node.initializer; + if ((ts.getEmitFlags(initializer) & 16384 /* NoLeadingComments */) === 0) { + var commentRange = ts.getCommentRange(initializer); + emitTrailingCommentsOfPosition(commentRange.pos); } - }); - // A common path can not be found when paths span multiple drives on windows, for example - if (failed) { - return ""; + emitExpression(initializer); } - if (!commonPathComponents) { - return currentDirectory; + function emitShorthandPropertyAssignment(node) { + emit(node.name); + if (node.objectAssignmentInitializer) { + write(" = "); + emitExpression(node.objectAssignmentInitializer); + } } - return ts.getNormalizedPathFromPathComponents(commonPathComponents); - } - ts.computeCommonSourceDirectoryOfFilenames = computeCommonSourceDirectoryOfFilenames; - function trace(host, message) { - host.trace(ts.formatMessage.apply(undefined, arguments)); - } - function isTraceEnabled(compilerOptions, host) { - return compilerOptions.traceResolution && host.trace !== undefined; - } - /* @internal */ - function hasZeroOrOneAsteriskCharacter(str) { - var seenAsterisk = false; - for (var i = 0; i < str.length; i++) { - if (str.charCodeAt(i) === 42 /* asterisk */) { - if (!seenAsterisk) { - seenAsterisk = true; + // + // Enum + // + function emitEnumMember(node) { + emit(node.name); + emitExpressionWithPrefix(" = ", node.initializer); + } + // + // Top-level nodes + // + function emitSourceFile(node) { + writeLine(); + emitShebang(); + emitBodyWithDetachedComments(node, node.statements, emitSourceFileWorker); + } + function emitSourceFileWorker(node) { + var statements = node.statements; + var statementOffset = emitPrologueDirectives(statements); + var savedTempFlags = tempFlags; + tempFlags = 0; + emitHelpers(node); + emitList(node, statements, 1 /* MultiLine */, statementOffset); + tempFlags = savedTempFlags; + } + // Transformation nodes + function emitPartiallyEmittedExpression(node) { + emitExpression(node.expression); + } + /** + * Emits any prologue directives at the start of a Statement list, returning the + * number of prologue directives written to the output. + */ + function emitPrologueDirectives(statements, startWithNewLine) { + for (var i = 0; i < statements.length; i++) { + if (ts.isPrologueDirective(statements[i])) { + if (startWithNewLine || i > 0) { + writeLine(); + } + emit(statements[i]); } else { - // have already seen asterisk - return false; + // return index of the first non prologue directive + return i; } } + return statements.length; } - return true; - } - ts.hasZeroOrOneAsteriskCharacter = hasZeroOrOneAsteriskCharacter; - function createResolvedModule(resolvedFileName, isExternalLibraryImport, failedLookupLocations) { - return { resolvedModule: resolvedFileName ? { resolvedFileName: resolvedFileName, isExternalLibraryImport: isExternalLibraryImport } : undefined, failedLookupLocations: failedLookupLocations }; - } - function moduleHasNonRelativeName(moduleName) { - return !(ts.isRootedDiskPath(moduleName) || ts.isExternalModuleNameRelative(moduleName)); - } - function tryReadTypesSection(packageJsonPath, baseDirectory, state) { - var jsonContent = readJson(packageJsonPath, state.host); - function tryReadFromField(fieldName) { - if (ts.hasProperty(jsonContent, fieldName)) { - var typesFile = jsonContent[fieldName]; - if (typeof typesFile === "string") { - var typesFilePath_1 = ts.normalizePath(ts.combinePaths(baseDirectory, typesFile)); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.package_json_has_0_field_1_that_references_2, fieldName, typesFile, typesFilePath_1); - } - return typesFilePath_1; + function emitHelpers(node) { + var emitFlags = ts.getEmitFlags(node); + var helpersEmitted = false; + if (emitFlags & 1 /* EmitEmitHelpers */) { + helpersEmitted = emitEmitHelpers(currentSourceFile); + } + if (emitFlags & 2 /* EmitExportStar */) { + writeLines(exportStarHelper); + helpersEmitted = true; + } + if (emitFlags & 4 /* EmitSuperHelper */) { + writeLines(superHelper); + helpersEmitted = true; + } + if (emitFlags & 8 /* EmitAdvancedSuperHelper */) { + writeLines(advancedSuperHelper); + helpersEmitted = true; + } + return helpersEmitted; + } + function emitEmitHelpers(node) { + // Only emit helpers if the user did not say otherwise. + if (compilerOptions.noEmitHelpers) { + return false; + } + // Don't emit helpers if we can import them. + if (compilerOptions.importHelpers + && (ts.isExternalModule(node) || compilerOptions.isolatedModules)) { + return false; + } + var helpersEmitted = false; + // Only Emit __extends function when target ES5. + // For target ES6 and above, we can emit classDeclaration as is. + if ((languageVersion < 2 /* ES6 */) && (!extendsEmitted && node.flags & 1024 /* HasClassExtends */)) { + writeLines(extendsHelper); + extendsEmitted = true; + helpersEmitted = true; + } + if (compilerOptions.jsx !== 1 /* Preserve */ && !assignEmitted && (node.flags & 16384 /* HasJsxSpreadAttributes */)) { + writeLines(assignHelper); + assignEmitted = true; + } + if (!decorateEmitted && node.flags & 2048 /* HasDecorators */) { + writeLines(decorateHelper); + if (compilerOptions.emitDecoratorMetadata) { + writeLines(metadataHelper); } - else { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Expected_type_of_0_field_in_package_json_to_be_string_got_1, fieldName, typeof typesFile); + decorateEmitted = true; + helpersEmitted = true; + } + if (!paramEmitted && node.flags & 4096 /* HasParamDecorators */) { + writeLines(paramHelper); + paramEmitted = true; + helpersEmitted = true; + } + if (!awaiterEmitted && node.flags & 8192 /* HasAsyncFunctions */) { + writeLines(awaiterHelper); + if (languageVersion < 2 /* ES6 */) { + writeLines(generatorHelper); + } + awaiterEmitted = true; + helpersEmitted = true; + } + if (helpersEmitted) { + writeLine(); + } + return helpersEmitted; + } + function writeLines(text) { + var lines = text.split(/\r\n|\r|\n/g); + for (var i = 0; i < lines.length; i++) { + var line = lines[i]; + if (line.length) { + if (i > 0) { + writeLine(); } + write(line); } } } - var typesFilePath = tryReadFromField("typings") || tryReadFromField("types"); - if (typesFilePath) { - return typesFilePath; + // + // Helpers + // + function emitShebang() { + var shebang = ts.getShebang(currentText); + if (shebang) { + write(shebang); + writeLine(); + } } - // Use the main module for inferring types if no types package specified and the allowJs is set - if (state.compilerOptions.allowJs && jsonContent.main && typeof jsonContent.main === "string") { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.No_types_specified_in_package_json_but_allowJs_is_set_so_returning_main_value_of_0, jsonContent.main); + function emitModifiers(node, modifiers) { + if (modifiers && modifiers.length) { + emitList(node, modifiers, 256 /* Modifiers */); + write(" "); } - var mainFilePath = ts.normalizePath(ts.combinePaths(baseDirectory, jsonContent.main)); - return mainFilePath; } - return undefined; - } - function readJson(path, host) { - try { - var jsonText = host.readFile(path); - return jsonText ? JSON.parse(jsonText) : {}; + function emitWithPrefix(prefix, node) { + emitNodeWithPrefix(prefix, node, emit); } - catch (e) { - // gracefully handle if readFile fails or returns not JSON - return {}; + function emitExpressionWithPrefix(prefix, node) { + emitNodeWithPrefix(prefix, node, emitExpression); } - } - var typeReferenceExtensions = [".d.ts"]; - function getEffectiveTypeRoots(options, host) { - if (options.typeRoots) { - return options.typeRoots; + function emitNodeWithPrefix(prefix, node, emit) { + if (node) { + write(prefix); + emit(node); + } } - var currentDirectory; - if (options.configFilePath) { - currentDirectory = ts.getDirectoryPath(options.configFilePath); + function emitWithSuffix(node, suffix) { + if (node) { + emit(node); + write(suffix); + } } - else if (host.getCurrentDirectory) { - currentDirectory = host.getCurrentDirectory(); + function emitEmbeddedStatement(node) { + if (ts.isBlock(node)) { + write(" "); + emit(node); + } + else { + writeLine(); + increaseIndent(); + emit(node); + decreaseIndent(); + } } - return currentDirectory && getDefaultTypeRoots(currentDirectory, host); - } - ts.getEffectiveTypeRoots = getEffectiveTypeRoots; - /** - * Returns the path to every node_modules/@types directory from some ancestor directory. - * Returns undefined if there are none. - */ - function getDefaultTypeRoots(currentDirectory, host) { - if (!host.directoryExists) { - return [ts.combinePaths(currentDirectory, nodeModulesAtTypes)]; + function emitDecorators(parentNode, decorators) { + emitList(parentNode, decorators, 24577 /* Decorators */); } - var typeRoots; - while (true) { - var atTypes = ts.combinePaths(currentDirectory, nodeModulesAtTypes); - if (host.directoryExists(atTypes)) { - (typeRoots || (typeRoots = [])).push(atTypes); + function emitTypeArguments(parentNode, typeArguments) { + emitList(parentNode, typeArguments, 26960 /* TypeArguments */); + } + function emitTypeParameters(parentNode, typeParameters) { + emitList(parentNode, typeParameters, 26960 /* TypeParameters */); + } + function emitParameters(parentNode, parameters) { + emitList(parentNode, parameters, 1360 /* Parameters */); + } + function emitParametersForArrow(parentNode, parameters) { + if (parameters && + parameters.length === 1 && + parameters[0].type === undefined && + parameters[0].pos === parentNode.pos) { + emit(parameters[0]); } - var parent_15 = ts.getDirectoryPath(currentDirectory); - if (parent_15 === currentDirectory) { - break; + else { + emitParameters(parentNode, parameters); } - currentDirectory = parent_15; } - return typeRoots; - } - var nodeModulesAtTypes = ts.combinePaths("node_modules", "@types"); - /** - * @param {string | undefined} containingFile - file that contains type reference directive, can be undefined if containing file is unknown. - * This is possible in case if resolution is performed for directives specified via 'types' parameter. In this case initial path for secondary lookups - * is assumed to be the same as root directory of the project. - */ - function resolveTypeReferenceDirective(typeReferenceDirectiveName, containingFile, options, host) { - var traceEnabled = isTraceEnabled(options, host); - var moduleResolutionState = { - compilerOptions: options, - host: host, - skipTsx: true, - traceEnabled: traceEnabled - }; - var typeRoots = getEffectiveTypeRoots(options, host); - if (traceEnabled) { - if (containingFile === undefined) { - if (typeRoots === undefined) { - trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_not_set_root_directory_not_set, typeReferenceDirectiveName); + function emitParametersForIndexSignature(parentNode, parameters) { + emitList(parentNode, parameters, 4432 /* IndexSignatureParameters */); + } + function emitList(parentNode, children, format, start, count) { + emitNodeList(emit, parentNode, children, format, start, count); + } + function emitExpressionList(parentNode, children, format, start, count) { + emitNodeList(emitExpression, parentNode, children, format, start, count); + } + function emitNodeList(emit, parentNode, children, format, start, count) { + if (start === void 0) { start = 0; } + if (count === void 0) { count = children ? children.length - start : 0; } + var isUndefined = children === undefined; + if (isUndefined && format & 8192 /* OptionalIfUndefined */) { + return; + } + var isEmpty = isUndefined || children.length === 0 || start >= children.length || count === 0; + if (isEmpty && format & 16384 /* OptionalIfEmpty */) { + return; + } + if (format & 7680 /* BracketsMask */) { + write(getOpeningBracket(format)); + } + if (isEmpty) { + // Write a line terminator if the parent node was multi-line + if (format & 1 /* MultiLine */) { + writeLine(); } - else { - trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_not_set_root_directory_1, typeReferenceDirectiveName, typeRoots); + else if (format & 128 /* SpaceBetweenBraces */) { + write(" "); } } else { - if (typeRoots === undefined) { - trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_1_root_directory_not_set, typeReferenceDirectiveName, containingFile); + // Write the opening line terminator or leading whitespace. + var mayEmitInterveningComments = (format & 131072 /* NoInterveningComments */) === 0; + var shouldEmitInterveningComments = mayEmitInterveningComments; + if (shouldWriteLeadingLineTerminator(parentNode, children, format)) { + writeLine(); + shouldEmitInterveningComments = false; } - else { - trace(host, ts.Diagnostics.Resolving_type_reference_directive_0_containing_file_1_root_directory_2, typeReferenceDirectiveName, containingFile, typeRoots); + else if (format & 128 /* SpaceBetweenBraces */) { + write(" "); } - } - } - var failedLookupLocations = []; - // Check primary library paths - if (typeRoots && typeRoots.length) { - if (traceEnabled) { - trace(host, ts.Diagnostics.Resolving_with_primary_search_path_0, typeRoots.join(", ")); - } - var primarySearchPaths = typeRoots; - for (var _i = 0, primarySearchPaths_1 = primarySearchPaths; _i < primarySearchPaths_1.length; _i++) { - var typeRoot = primarySearchPaths_1[_i]; - var candidate = ts.combinePaths(typeRoot, typeReferenceDirectiveName); - var candidateDirectory = ts.getDirectoryPath(candidate); - var resolvedFile_1 = loadNodeModuleFromDirectory(typeReferenceExtensions, candidate, failedLookupLocations, !directoryProbablyExists(candidateDirectory, host), moduleResolutionState); - if (resolvedFile_1) { - if (traceEnabled) { - trace(host, ts.Diagnostics.Type_reference_directive_0_was_successfully_resolved_to_1_primary_Colon_2, typeReferenceDirectiveName, resolvedFile_1, true); + // Increase the indent, if requested. + if (format & 64 /* Indented */) { + increaseIndent(); + } + // Emit each child. + var previousSibling = void 0; + var shouldDecreaseIndentAfterEmit = void 0; + var delimiter = getDelimiter(format); + for (var i = 0; i < count; i++) { + var child = children[start + i]; + // Write the delimiter if this is not the first node. + if (previousSibling) { + write(delimiter); + // Write either a line terminator or whitespace to separate the elements. + if (shouldWriteSeparatingLineTerminator(previousSibling, child, format)) { + // If a synthesized node in a single-line list starts on a new + // line, we should increase the indent. + if ((format & (3 /* LinesMask */ | 64 /* Indented */)) === 0 /* SingleLine */) { + increaseIndent(); + shouldDecreaseIndentAfterEmit = true; + } + writeLine(); + shouldEmitInterveningComments = false; + } + else if (previousSibling && format & 256 /* SpaceBetweenSiblings */) { + write(" "); + } } - return { - resolvedTypeReferenceDirective: { primary: true, resolvedFileName: resolvedFile_1 }, - failedLookupLocations: failedLookupLocations - }; + if (shouldEmitInterveningComments) { + var commentRange = ts.getCommentRange(child); + emitTrailingCommentsOfPosition(commentRange.pos); + } + else { + shouldEmitInterveningComments = mayEmitInterveningComments; + } + // Emit this child. + emit(child); + if (shouldDecreaseIndentAfterEmit) { + decreaseIndent(); + shouldDecreaseIndentAfterEmit = false; + } + previousSibling = child; + } + // Write a trailing comma, if requested. + var hasTrailingComma = (format & 32 /* AllowTrailingComma */) && children.hasTrailingComma; + if (format & 16 /* CommaDelimited */ && hasTrailingComma) { + write(","); + } + // Decrease the indent, if requested. + if (format & 64 /* Indented */) { + decreaseIndent(); + } + // Write the closing line terminator or closing whitespace. + if (shouldWriteClosingLineTerminator(parentNode, children, format)) { + writeLine(); + } + else if (format & 128 /* SpaceBetweenBraces */) { + write(" "); } } - } - else { - if (traceEnabled) { - trace(host, ts.Diagnostics.Root_directory_cannot_be_determined_skipping_primary_search_paths); + if (format & 7680 /* BracketsMask */) { + write(getClosingBracket(format)); } } - var resolvedFile; - var initialLocationForSecondaryLookup; - if (containingFile) { - initialLocationForSecondaryLookup = ts.getDirectoryPath(containingFile); - } - if (initialLocationForSecondaryLookup !== undefined) { - // check secondary locations - if (traceEnabled) { - trace(host, ts.Diagnostics.Looking_up_in_node_modules_folder_initial_location_0, initialLocationForSecondaryLookup); - } - resolvedFile = loadModuleFromNodeModules(typeReferenceDirectiveName, initialLocationForSecondaryLookup, failedLookupLocations, moduleResolutionState); - if (traceEnabled) { - if (resolvedFile) { - trace(host, ts.Diagnostics.Type_reference_directive_0_was_successfully_resolved_to_1_primary_Colon_2, typeReferenceDirectiveName, resolvedFile, false); - } - else { - trace(host, ts.Diagnostics.Type_reference_directive_0_was_not_resolved, typeReferenceDirectiveName); - } + function writeIfAny(nodes, text) { + if (nodes && nodes.length > 0) { + write(text); } } - else { - if (traceEnabled) { - trace(host, ts.Diagnostics.Containing_file_is_not_specified_and_root_directory_cannot_be_determined_skipping_lookup_in_node_modules_folder); + function writeIfPresent(node, text) { + if (node !== undefined) { + write(text); } } - return { - resolvedTypeReferenceDirective: resolvedFile - ? { primary: false, resolvedFileName: resolvedFile } - : undefined, - failedLookupLocations: failedLookupLocations - }; - } - ts.resolveTypeReferenceDirective = resolveTypeReferenceDirective; - function resolveModuleName(moduleName, containingFile, compilerOptions, host) { - var traceEnabled = isTraceEnabled(compilerOptions, host); - if (traceEnabled) { - trace(host, ts.Diagnostics.Resolving_module_0_from_1, moduleName, containingFile); + function writeToken(token, pos, contextNode) { + return emitTokenWithSourceMap(contextNode, token, pos, writeTokenText); } - var moduleResolution = compilerOptions.moduleResolution; - if (moduleResolution === undefined) { - moduleResolution = ts.getEmitModuleKind(compilerOptions) === ts.ModuleKind.CommonJS ? ts.ModuleResolutionKind.NodeJs : ts.ModuleResolutionKind.Classic; - if (traceEnabled) { - trace(host, ts.Diagnostics.Module_resolution_kind_is_not_specified_using_0, ts.ModuleResolutionKind[moduleResolution]); - } + function writeTokenText(token, pos) { + var tokenString = ts.tokenToString(token); + write(tokenString); + return pos < 0 ? pos : pos + tokenString.length; } - else { - if (traceEnabled) { - trace(host, ts.Diagnostics.Explicitly_specified_module_resolution_kind_Colon_0, ts.ModuleResolutionKind[moduleResolution]); + function increaseIndentIf(value, valueToWriteWhenNotIndenting) { + if (value) { + increaseIndent(); + writeLine(); + } + else if (valueToWriteWhenNotIndenting) { + write(valueToWriteWhenNotIndenting); } } - var result; - switch (moduleResolution) { - case ts.ModuleResolutionKind.NodeJs: - result = nodeModuleNameResolver(moduleName, containingFile, compilerOptions, host); - break; - case ts.ModuleResolutionKind.Classic: - result = classicNameResolver(moduleName, containingFile, compilerOptions, host); - break; - } - if (traceEnabled) { - if (result.resolvedModule) { - trace(host, ts.Diagnostics.Module_name_0_was_successfully_resolved_to_1, moduleName, result.resolvedModule.resolvedFileName); + // Helper function to decrease the indent if we previously indented. Allows multiple + // previous indent values to be considered at a time. This also allows caller to just + // call this once, passing in all their appropriate indent values, instead of needing + // to call this helper function multiple times. + function decreaseIndentIf(value1, value2) { + if (value1) { + decreaseIndent(); } - else { - trace(host, ts.Diagnostics.Module_name_0_was_not_resolved, moduleName); + if (value2) { + decreaseIndent(); } } - return result; - } - ts.resolveModuleName = resolveModuleName; - /** - * Any module resolution kind can be augmented with optional settings: 'baseUrl', 'paths' and 'rootDirs' - they are used to - * mitigate differences between design time structure of the project and its runtime counterpart so the same import name - * can be resolved successfully by TypeScript compiler and runtime module loader. - * If these settings are set then loading procedure will try to use them to resolve module name and it can of failure it will - * fallback to standard resolution routine. - * - * - baseUrl - this setting controls how non-relative module names are resolved. If this setting is specified then non-relative - * names will be resolved relative to baseUrl: i.e. if baseUrl is '/a/b' then candidate location to resolve module name 'c/d' will - * be '/a/b/c/d' - * - paths - this setting can only be used when baseUrl is specified. allows to tune how non-relative module names - * will be resolved based on the content of the module name. - * Structure of 'paths' compiler options - * 'paths': { - * pattern-1: [...substitutions], - * pattern-2: [...substitutions], - * ... - * pattern-n: [...substitutions] - * } - * Pattern here is a string that can contain zero or one '*' character. During module resolution module name will be matched against - * all patterns in the list. Matching for patterns that don't contain '*' means that module name must be equal to pattern respecting the case. - * If pattern contains '*' then to match pattern "*" module name must start with the and end with . - * denotes part of the module name between and . - * If module name can be matches with multiple patterns then pattern with the longest prefix will be picked. - * After selecting pattern we'll use list of substitutions to get candidate locations of the module and the try to load module - * from the candidate location. - * Substitution is a string that can contain zero or one '*'. To get candidate location from substitution we'll pick every - * substitution in the list and replace '*' with string. If candidate location is not rooted it - * will be converted to absolute using baseUrl. - * For example: - * baseUrl: /a/b/c - * "paths": { - * // match all module names - * "*": [ - * "*", // use matched name as is, - * // will be looked as /a/b/c/ - * - * "folder1/*" // substitution will convert matched name to 'folder1/', - * // since it is not rooted then final candidate location will be /a/b/c/folder1/ - * ], - * // match module names that start with 'components/' - * "components/*": [ "/root/components/*" ] // substitution will convert /components/folder1/ to '/root/components/folder1/', - * // it is rooted so it will be final candidate location - * } - * - * 'rootDirs' allows the project to be spreaded across multiple locations and resolve modules with relative names as if - * they were in the same location. For example lets say there are two files - * '/local/src/content/file1.ts' - * '/shared/components/contracts/src/content/protocols/file2.ts' - * After bundling content of '/shared/components/contracts/src' will be merged with '/local/src' so - * if file1 has the following import 'import {x} from "./protocols/file2"' it will be resolved successfully in runtime. - * 'rootDirs' provides the way to tell compiler that in order to get the whole project it should behave as if content of all - * root dirs were merged together. - * I.e. for the example above 'rootDirs' will have two entries: [ '/local/src', '/shared/components/contracts/src' ]. - * Compiler will first convert './protocols/file2' into absolute path relative to the location of containing file: - * '/local/src/content/protocols/file2' and try to load it - failure. - * Then it will search 'rootDirs' looking for a longest matching prefix of this absolute path and if such prefix is found - absolute path will - * be converted to a path relative to found rootDir entry './content/protocols/file2' (*). As a last step compiler will check all remaining - * entries in 'rootDirs', use them to build absolute path out of (*) and try to resolve module from this location. - */ - function tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state) { - if (moduleHasNonRelativeName(moduleName)) { - return tryLoadModuleUsingBaseUrl(moduleName, loader, failedLookupLocations, supportedExtensions, state); - } - else { - return tryLoadModuleUsingRootDirs(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state); - } - } - function tryLoadModuleUsingRootDirs(moduleName, containingDirectory, loader, failedLookupLocations, supportedExtensions, state) { - if (!state.compilerOptions.rootDirs) { - return undefined; - } - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.rootDirs_option_is_set_using_it_to_resolve_relative_module_name_0, moduleName); - } - var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); - var matchedRootDir; - var matchedNormalizedPrefix; - for (var _i = 0, _a = state.compilerOptions.rootDirs; _i < _a.length; _i++) { - var rootDir = _a[_i]; - // rootDirs are expected to be absolute - // in case of tsconfig.json this will happen automatically - compiler will expand relative names - // using location of tsconfig.json as base location - var normalizedRoot = ts.normalizePath(rootDir); - if (!ts.endsWith(normalizedRoot, ts.directorySeparator)) { - normalizedRoot += ts.directorySeparator; + function shouldWriteLeadingLineTerminator(parentNode, children, format) { + if (format & 1 /* MultiLine */) { + return true; } - var isLongestMatchingPrefix = ts.startsWith(candidate, normalizedRoot) && - (matchedNormalizedPrefix === undefined || matchedNormalizedPrefix.length < normalizedRoot.length); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Checking_if_0_is_the_longest_matching_prefix_for_1_2, normalizedRoot, candidate, isLongestMatchingPrefix); + if (format & 2 /* PreserveLines */) { + if (format & 32768 /* PreferNewLine */) { + return true; + } + var firstChild = children[0]; + if (firstChild === undefined) { + return !ts.rangeIsOnSingleLine(parentNode, currentSourceFile); + } + else if (ts.positionIsSynthesized(parentNode.pos) || ts.nodeIsSynthesized(firstChild)) { + return synthesizedNodeStartsOnNewLine(firstChild, format); + } + else { + return !ts.rangeStartPositionsAreOnSameLine(parentNode, firstChild, currentSourceFile); + } } - if (isLongestMatchingPrefix) { - matchedNormalizedPrefix = normalizedRoot; - matchedRootDir = rootDir; + else { + return false; } } - if (matchedNormalizedPrefix) { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Longest_matching_prefix_for_0_is_1, candidate, matchedNormalizedPrefix); + function shouldWriteSeparatingLineTerminator(previousNode, nextNode, format) { + if (format & 1 /* MultiLine */) { + return true; } - var suffix = candidate.substr(matchedNormalizedPrefix.length); - // first - try to load from a initial location - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Loading_0_from_the_root_dir_1_candidate_location_2, suffix, matchedNormalizedPrefix, candidate); + else if (format & 2 /* PreserveLines */) { + if (previousNode === undefined || nextNode === undefined) { + return false; + } + else if (ts.nodeIsSynthesized(previousNode) || ts.nodeIsSynthesized(nextNode)) { + return synthesizedNodeStartsOnNewLine(previousNode, format) || synthesizedNodeStartsOnNewLine(nextNode, format); + } + else { + return !ts.rangeEndIsOnSameLineAsRangeStart(previousNode, nextNode, currentSourceFile); + } } - var resolvedFileName = loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(containingDirectory, state.host), state); - if (resolvedFileName) { - return resolvedFileName; + else { + return nextNode.startsOnNewLine; } - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Trying_other_entries_in_rootDirs); + } + function shouldWriteClosingLineTerminator(parentNode, children, format) { + if (format & 1 /* MultiLine */) { + return (format & 65536 /* NoTrailingNewLine */) === 0; } - // then try to resolve using remaining entries in rootDirs - for (var _b = 0, _c = state.compilerOptions.rootDirs; _b < _c.length; _b++) { - var rootDir = _c[_b]; - if (rootDir === matchedRootDir) { - // skip the initially matched entry - continue; + else if (format & 2 /* PreserveLines */) { + if (format & 32768 /* PreferNewLine */) { + return true; } - var candidate_1 = ts.combinePaths(ts.normalizePath(rootDir), suffix); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Loading_0_from_the_root_dir_1_candidate_location_2, suffix, rootDir, candidate_1); + var lastChild = ts.lastOrUndefined(children); + if (lastChild === undefined) { + return !ts.rangeIsOnSingleLine(parentNode, currentSourceFile); } - var baseDirectory = ts.getDirectoryPath(candidate_1); - var resolvedFileName_1 = loader(candidate_1, supportedExtensions, failedLookupLocations, !directoryProbablyExists(baseDirectory, state.host), state); - if (resolvedFileName_1) { - return resolvedFileName_1; + else if (ts.positionIsSynthesized(parentNode.pos) || ts.nodeIsSynthesized(lastChild)) { + return synthesizedNodeStartsOnNewLine(lastChild, format); + } + else { + return !ts.rangeEndPositionsAreOnSameLine(parentNode, lastChild, currentSourceFile); } } - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Module_resolution_using_rootDirs_has_failed); + else { + return false; } } - return undefined; - } - function tryLoadModuleUsingBaseUrl(moduleName, loader, failedLookupLocations, supportedExtensions, state) { - if (!state.compilerOptions.baseUrl) { - return undefined; + function synthesizedNodeStartsOnNewLine(node, format) { + if (ts.nodeIsSynthesized(node)) { + var startsOnNewLine = node.startsOnNewLine; + if (startsOnNewLine === undefined) { + return (format & 32768 /* PreferNewLine */) !== 0; + } + return startsOnNewLine; + } + return (format & 32768 /* PreferNewLine */) !== 0; } - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.baseUrl_option_is_set_to_0_using_this_value_to_resolve_non_relative_module_name_1, state.compilerOptions.baseUrl, moduleName); + function needsIndentation(parent, node1, node2) { + parent = skipSynthesizedParentheses(parent); + node1 = skipSynthesizedParentheses(node1); + node2 = skipSynthesizedParentheses(node2); + // Always use a newline for synthesized code if the synthesizer desires it. + if (node2.startsOnNewLine) { + return true; + } + return !ts.nodeIsSynthesized(parent) + && !ts.nodeIsSynthesized(node1) + && !ts.nodeIsSynthesized(node2) + && !ts.rangeEndIsOnSameLineAsRangeStart(node1, node2, currentSourceFile); } - // string is for exact match - var matchedPattern = undefined; - if (state.compilerOptions.paths) { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.paths_option_is_specified_looking_for_a_pattern_to_match_module_name_0, moduleName); + function skipSynthesizedParentheses(node) { + while (node.kind === 178 /* ParenthesizedExpression */ && ts.nodeIsSynthesized(node)) { + node = node.expression; } - matchedPattern = matchPatternOrExact(ts.getOwnKeys(state.compilerOptions.paths), moduleName); + return node; } - if (matchedPattern) { - var matchedStar = typeof matchedPattern === "string" ? undefined : matchedText(matchedPattern, moduleName); - var matchedPatternText = typeof matchedPattern === "string" ? matchedPattern : patternText(matchedPattern); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Module_name_0_matched_pattern_1, moduleName, matchedPatternText); + function getTextOfNode(node, includeTrivia) { + if (ts.isGeneratedIdentifier(node)) { + return getGeneratedIdentifier(node); } - for (var _i = 0, _a = state.compilerOptions.paths[matchedPatternText]; _i < _a.length; _i++) { - var subst = _a[_i]; - var path = matchedStar ? subst.replace("*", matchedStar) : subst; - var candidate = ts.normalizePath(ts.combinePaths(state.compilerOptions.baseUrl, path)); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Trying_substitution_0_candidate_module_location_Colon_1, subst, path); + else if (ts.isIdentifier(node) && (ts.nodeIsSynthesized(node) || !node.parent)) { + return ts.unescapeIdentifier(node.text); + } + else if (node.kind === 9 /* StringLiteral */ && node.textSourceNode) { + return getTextOfNode(node.textSourceNode, includeTrivia); + } + else if (ts.isLiteralExpression(node) && (ts.nodeIsSynthesized(node) || !node.parent)) { + return node.text; + } + return ts.getSourceTextOfNodeFromSourceFile(currentSourceFile, node, includeTrivia); + } + function getLiteralTextOfNode(node) { + if (node.kind === 9 /* StringLiteral */ && node.textSourceNode) { + var textSourceNode = node.textSourceNode; + if (ts.isIdentifier(textSourceNode)) { + return "\"" + ts.escapeNonAsciiCharacters(ts.escapeString(getTextOfNode(textSourceNode))) + "\""; } - var resolvedFileName = loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(ts.getDirectoryPath(candidate), state.host), state); - if (resolvedFileName) { - return resolvedFileName; + else { + return getLiteralTextOfNode(textSourceNode); } } - return undefined; + return ts.getLiteralText(node, currentSourceFile, languageVersion); } - else { - var candidate = ts.normalizePath(ts.combinePaths(state.compilerOptions.baseUrl, moduleName)); - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Resolving_module_name_0_relative_to_base_url_1_2, moduleName, state.compilerOptions.baseUrl, candidate); - } - return loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(ts.getDirectoryPath(candidate), state.host), state); + function isSingleLineEmptyBlock(block) { + return !block.multiLine + && isEmptyBlock(block); } - } - /** - * patternStrings contains both pattern strings (containing "*") and regular strings. - * Return an exact match if possible, or a pattern match, or undefined. - * (These are verified by verifyCompilerOptions to have 0 or 1 "*" characters.) - */ - function matchPatternOrExact(patternStrings, candidate) { - var patterns = []; - for (var _i = 0, patternStrings_1 = patternStrings; _i < patternStrings_1.length; _i++) { - var patternString = patternStrings_1[_i]; - var pattern = tryParsePattern(patternString); - if (pattern) { - patterns.push(pattern); - } - else if (patternString === candidate) { - // pattern was matched as is - no need to search further - return patternString; - } + function isEmptyBlock(block) { + return block.statements.length === 0 + && ts.rangeEndIsOnSameLineAsRangeStart(block, block, currentSourceFile); } - return findBestPatternMatch(patterns, function (_) { return _; }, candidate); - } - function patternText(_a) { - var prefix = _a.prefix, suffix = _a.suffix; - return prefix + "*" + suffix; - } - /** - * Given that candidate matches pattern, returns the text matching the '*'. - * E.g.: matchedText(tryParsePattern("foo*baz"), "foobarbaz") === "bar" - */ - function matchedText(pattern, candidate) { - ts.Debug.assert(isPatternMatch(pattern, candidate)); - return candidate.substr(pattern.prefix.length, candidate.length - pattern.suffix.length); - } - /** Return the object corresponding to the best pattern to match `candidate`. */ - /* @internal */ - function findBestPatternMatch(values, getPattern, candidate) { - var matchedValue = undefined; - // use length of prefix as betterness criteria - var longestMatchPrefixLength = -1; - for (var _i = 0, values_1 = values; _i < values_1.length; _i++) { - var v = values_1[_i]; - var pattern = getPattern(v); - if (isPatternMatch(pattern, candidate) && pattern.prefix.length > longestMatchPrefixLength) { - longestMatchPrefixLength = pattern.prefix.length; - matchedValue = v; + function isUniqueName(name) { + return !resolver.hasGlobalName(name) && + !ts.hasProperty(currentFileIdentifiers, name) && + !ts.hasProperty(generatedNameSet, name); + } + function isUniqueLocalName(name, container) { + for (var node = container; ts.isNodeDescendantOf(node, container); node = node.nextContainer) { + if (node.locals && ts.hasProperty(node.locals, name)) { + // We conservatively include alias symbols to cover cases where they're emitted as locals + if (node.locals[name].flags & (107455 /* Value */ | 1048576 /* ExportValue */ | 8388608 /* Alias */)) { + return false; + } + } } + return true; } - return matchedValue; - } - ts.findBestPatternMatch = findBestPatternMatch; - function isPatternMatch(_a, candidate) { - var prefix = _a.prefix, suffix = _a.suffix; - return candidate.length >= prefix.length + suffix.length && - ts.startsWith(candidate, prefix) && - ts.endsWith(candidate, suffix); - } - /* @internal */ - function tryParsePattern(pattern) { - // This should be verified outside of here and a proper error thrown. - ts.Debug.assert(hasZeroOrOneAsteriskCharacter(pattern)); - var indexOfStar = pattern.indexOf("*"); - return indexOfStar === -1 ? undefined : { - prefix: pattern.substr(0, indexOfStar), - suffix: pattern.substr(indexOfStar + 1) - }; - } - ts.tryParsePattern = tryParsePattern; - function nodeModuleNameResolver(moduleName, containingFile, compilerOptions, host) { - var containingDirectory = ts.getDirectoryPath(containingFile); - var supportedExtensions = ts.getSupportedExtensions(compilerOptions); - var traceEnabled = isTraceEnabled(compilerOptions, host); - var failedLookupLocations = []; - var state = { compilerOptions: compilerOptions, host: host, traceEnabled: traceEnabled, skipTsx: false }; - var resolvedFileName = tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, nodeLoadModuleByRelativeName, failedLookupLocations, supportedExtensions, state); - var isExternalLibraryImport = false; - if (!resolvedFileName) { - if (moduleHasNonRelativeName(moduleName)) { - if (traceEnabled) { - trace(host, ts.Diagnostics.Loading_module_0_from_node_modules_folder, moduleName); + /** + * Return the next available name in the pattern _a ... _z, _0, _1, ... + * TempFlags._i or TempFlags._n may be used to express a preference for that dedicated name. + * Note that names generated by makeTempVariableName and makeUniqueName will never conflict. + */ + function makeTempVariableName(flags) { + if (flags && !(tempFlags & flags)) { + var name_41 = flags === 268435456 /* _i */ ? "_i" : "_n"; + if (isUniqueName(name_41)) { + tempFlags |= flags; + return name_41; } - resolvedFileName = loadModuleFromNodeModules(moduleName, containingDirectory, failedLookupLocations, state); - isExternalLibraryImport = resolvedFileName !== undefined; } - else { - var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); - resolvedFileName = nodeLoadModuleByRelativeName(candidate, supportedExtensions, failedLookupLocations, /*onlyRecordFailures*/ false, state); + while (true) { + var count = tempFlags & 268435455 /* CountMask */; + tempFlags++; + // Skip over 'i' and 'n' + if (count !== 8 && count !== 13) { + var name_42 = count < 26 + ? "_" + String.fromCharCode(97 /* a */ + count) + : "_" + (count - 26); + if (isUniqueName(name_42)) { + return name_42; + } + } } } - if (resolvedFileName && host.realpath) { - var originalFileName = resolvedFileName; - resolvedFileName = ts.normalizePath(host.realpath(resolvedFileName)); - if (traceEnabled) { - trace(host, ts.Diagnostics.Resolving_real_path_for_0_result_1, originalFileName, resolvedFileName); + // Generate a name that is unique within the current file and doesn't conflict with any names + // in global scope. The name is formed by adding an '_n' suffix to the specified base name, + // where n is a positive integer. Note that names generated by makeTempVariableName and + // makeUniqueName are guaranteed to never conflict. + function makeUniqueName(baseName) { + // Find the first unique 'name_n', where n is a positive number + if (baseName.charCodeAt(baseName.length - 1) !== 95 /* _ */) { + baseName += "_"; } - } - return createResolvedModule(resolvedFileName, isExternalLibraryImport, failedLookupLocations); - } - ts.nodeModuleNameResolver = nodeModuleNameResolver; - function nodeLoadModuleByRelativeName(candidate, supportedExtensions, failedLookupLocations, onlyRecordFailures, state) { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Loading_module_as_file_Slash_folder_candidate_module_location_0, candidate); - } - var resolvedFileName = !ts.pathEndsWithDirectorySeparator(candidate) && loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, onlyRecordFailures, state); - return resolvedFileName || loadNodeModuleFromDirectory(supportedExtensions, candidate, failedLookupLocations, onlyRecordFailures, state); - } - /* @internal */ - function directoryProbablyExists(directoryName, host) { - // if host does not support 'directoryExists' assume that directory will exist - return !host.directoryExists || host.directoryExists(directoryName); - } - ts.directoryProbablyExists = directoryProbablyExists; - /** - * @param {boolean} onlyRecordFailures - if true then function won't try to actually load files but instead record all attempts as failures. This flag is necessary - * in cases when we know upfront that all load attempts will fail (because containing folder does not exists) however we still need to record all failed lookup locations. - */ - function loadModuleFromFile(candidate, extensions, failedLookupLocation, onlyRecordFailures, state) { - // First, try adding an extension. An import of "foo" could be matched by a file "foo.ts", or "foo.js" by "foo.js.ts" - var resolvedByAddingExtension = tryAddingExtensions(candidate, extensions, failedLookupLocation, onlyRecordFailures, state); - if (resolvedByAddingExtension) { - return resolvedByAddingExtension; - } - // If that didn't work, try stripping a ".js" or ".jsx" extension and replacing it with a TypeScript one; - // e.g. "./foo.js" can be matched by "./foo.ts" or "./foo.d.ts" - if (ts.hasJavaScriptFileExtension(candidate)) { - var extensionless = ts.removeFileExtension(candidate); - if (state.traceEnabled) { - var extension = candidate.substring(extensionless.length); - trace(state.host, ts.Diagnostics.File_name_0_has_a_1_extension_stripping_it, candidate, extension); + var i = 1; + while (true) { + var generatedName = baseName + i; + if (isUniqueName(generatedName)) { + return generatedNameSet[generatedName] = generatedName; + } + i++; } - return tryAddingExtensions(extensionless, extensions, failedLookupLocation, onlyRecordFailures, state); } - } - /** Try to return an existing file that adds one of the `extensions` to `candidate`. */ - function tryAddingExtensions(candidate, extensions, failedLookupLocation, onlyRecordFailures, state) { - if (!onlyRecordFailures) { - // check if containing folder exists - if it doesn't then just record failures for all supported extensions without disk probing - var directory = ts.getDirectoryPath(candidate); - if (directory) { - onlyRecordFailures = !directoryProbablyExists(directory, state.host); - } + function generateNameForModuleOrEnum(node) { + var name = getTextOfNode(node.name); + // Use module/enum name itself if it is unique, otherwise make a unique variation + return isUniqueLocalName(name, node) ? name : makeUniqueName(name); } - return ts.forEach(extensions, function (ext) { - return !(state.skipTsx && ts.isJsxOrTsxExtension(ext)) && tryFile(candidate + ext, failedLookupLocation, onlyRecordFailures, state); - }); - } - /** Return the file if it exists. */ - function tryFile(fileName, failedLookupLocation, onlyRecordFailures, state) { - if (!onlyRecordFailures && state.host.fileExists(fileName)) { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.File_0_exist_use_it_as_a_name_resolution_result, fileName); - } - return fileName; + function generateNameForImportOrExportDeclaration(node) { + var expr = ts.getExternalModuleName(node); + var baseName = expr.kind === 9 /* StringLiteral */ ? + ts.escapeIdentifier(ts.makeIdentifierFromModuleName(expr.text)) : "module"; + return makeUniqueName(baseName); } - else { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.File_0_does_not_exist, fileName); + function generateNameForExportDefault() { + return makeUniqueName("default"); + } + function generateNameForClassExpression() { + return makeUniqueName("class"); + } + /** + * Generates a unique name from a node. + * + * @param node A node. + */ + function generateNameForNode(node) { + switch (node.kind) { + case 69 /* Identifier */: + return makeUniqueName(getTextOfNode(node)); + case 225 /* ModuleDeclaration */: + case 224 /* EnumDeclaration */: + return generateNameForModuleOrEnum(node); + case 230 /* ImportDeclaration */: + case 236 /* ExportDeclaration */: + return generateNameForImportOrExportDeclaration(node); + case 220 /* FunctionDeclaration */: + case 221 /* ClassDeclaration */: + case 235 /* ExportAssignment */: + return generateNameForExportDefault(); + case 192 /* ClassExpression */: + return generateNameForClassExpression(); + default: + return makeTempVariableName(0 /* Auto */); } - failedLookupLocation.push(fileName); - return undefined; } - } - function loadNodeModuleFromDirectory(extensions, candidate, failedLookupLocation, onlyRecordFailures, state) { - var packageJsonPath = pathToPackageJson(candidate); - var directoryExists = !onlyRecordFailures && directoryProbablyExists(candidate, state.host); - if (directoryExists && state.host.fileExists(packageJsonPath)) { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.Found_package_json_at_0, packageJsonPath); + /** + * Generates a unique identifier for a node. + * + * @param name A generated name. + */ + function generateName(name) { + switch (name.autoGenerateKind) { + case 1 /* Auto */: + return makeTempVariableName(0 /* Auto */); + case 2 /* Loop */: + return makeTempVariableName(268435456 /* _i */); + case 3 /* Unique */: + return makeUniqueName(name.text); } - var typesFile = tryReadTypesSection(packageJsonPath, candidate, state); - if (typesFile) { - var onlyRecordFailures_1 = !directoryProbablyExists(ts.getDirectoryPath(typesFile), state.host); - // A package.json "typings" may specify an exact filename, or may choose to omit an extension. - var result = tryFile(typesFile, failedLookupLocation, onlyRecordFailures_1, state) || - tryAddingExtensions(typesFile, extensions, failedLookupLocation, onlyRecordFailures_1, state); - if (result) { - return result; + ts.Debug.fail("Unsupported GeneratedIdentifierKind."); + } + /** + * Gets the node from which a name should be generated. + * + * @param name A generated name wrapper. + */ + function getNodeForGeneratedName(name) { + var autoGenerateId = name.autoGenerateId; + var node = name; + var original = node.original; + while (original) { + node = original; + // if "node" is a different generated name (having a different + // "autoGenerateId"), use it and stop traversing. + if (ts.isIdentifier(node) + && node.autoGenerateKind === 4 /* Node */ + && node.autoGenerateId !== autoGenerateId) { + break; } + original = node.original; + } + // otherwise, return the original node for the source; + return node; + } + /** + * Gets the generated identifier text from a generated identifier. + * + * @param name The generated identifier. + */ + function getGeneratedIdentifier(name) { + if (name.autoGenerateKind === 4 /* Node */) { + // Generated names generate unique names based on their original node + // and are cached based on that node's id + var node = getNodeForGeneratedName(name); + var nodeId = ts.getNodeId(node); + return nodeIdToGeneratedName[nodeId] || (nodeIdToGeneratedName[nodeId] = ts.unescapeIdentifier(generateNameForNode(node))); } else { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.package_json_does_not_have_types_field); - } + // Auto, Loop, and Unique names are cached based on their unique + // autoGenerateId. + var autoGenerateId = name.autoGenerateId; + return autoGeneratedIdToGeneratedName[autoGenerateId] || (autoGeneratedIdToGeneratedName[autoGenerateId] = ts.unescapeIdentifier(generateName(name))); } } - else { - if (state.traceEnabled) { - trace(state.host, ts.Diagnostics.File_0_does_not_exist, packageJsonPath); - } - // record package json as one of failed lookup locations - in the future if this file will appear it will invalidate resolution results - failedLookupLocation.push(packageJsonPath); + function createDelimiterMap() { + var delimiters = []; + delimiters[0 /* None */] = ""; + delimiters[16 /* CommaDelimited */] = ","; + delimiters[4 /* BarDelimited */] = " |"; + delimiters[8 /* AmpersandDelimited */] = " &"; + return delimiters; } - return loadModuleFromFile(ts.combinePaths(candidate, "index"), extensions, failedLookupLocation, !directoryExists, state); - } - function pathToPackageJson(directory) { - return ts.combinePaths(directory, "package.json"); - } - function loadModuleFromNodeModulesFolder(moduleName, directory, failedLookupLocations, state) { - var nodeModulesFolder = ts.combinePaths(directory, "node_modules"); - var nodeModulesFolderExists = directoryProbablyExists(nodeModulesFolder, state.host); - var candidate = ts.normalizePath(ts.combinePaths(nodeModulesFolder, moduleName)); - var supportedExtensions = ts.getSupportedExtensions(state.compilerOptions); - var result = loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, !nodeModulesFolderExists, state); - if (result) { - return result; + function getDelimiter(format) { + return delimiters[format & 28 /* DelimitersMask */]; } - result = loadNodeModuleFromDirectory(supportedExtensions, candidate, failedLookupLocations, !nodeModulesFolderExists, state); - if (result) { - return result; + function createBracketsMap() { + var brackets = []; + brackets[512 /* Braces */] = ["{", "}"]; + brackets[1024 /* Parenthesis */] = ["(", ")"]; + brackets[2048 /* AngleBrackets */] = ["<", ">"]; + brackets[4096 /* SquareBrackets */] = ["[", "]"]; + return brackets; + } + function getOpeningBracket(format) { + return brackets[format & 7680 /* BracketsMask */][0]; + } + function getClosingBracket(format) { + return brackets[format & 7680 /* BracketsMask */][1]; } } - function loadModuleFromNodeModules(moduleName, directory, failedLookupLocations, state) { - directory = ts.normalizeSlashes(directory); + ts.emitFiles = emitFiles; + var ListFormat; + (function (ListFormat) { + ListFormat[ListFormat["None"] = 0] = "None"; + // Line separators + ListFormat[ListFormat["SingleLine"] = 0] = "SingleLine"; + ListFormat[ListFormat["MultiLine"] = 1] = "MultiLine"; + ListFormat[ListFormat["PreserveLines"] = 2] = "PreserveLines"; + ListFormat[ListFormat["LinesMask"] = 3] = "LinesMask"; + // Delimiters + ListFormat[ListFormat["NotDelimited"] = 0] = "NotDelimited"; + ListFormat[ListFormat["BarDelimited"] = 4] = "BarDelimited"; + ListFormat[ListFormat["AmpersandDelimited"] = 8] = "AmpersandDelimited"; + ListFormat[ListFormat["CommaDelimited"] = 16] = "CommaDelimited"; + ListFormat[ListFormat["DelimitersMask"] = 28] = "DelimitersMask"; + ListFormat[ListFormat["AllowTrailingComma"] = 32] = "AllowTrailingComma"; + // Whitespace + ListFormat[ListFormat["Indented"] = 64] = "Indented"; + ListFormat[ListFormat["SpaceBetweenBraces"] = 128] = "SpaceBetweenBraces"; + ListFormat[ListFormat["SpaceBetweenSiblings"] = 256] = "SpaceBetweenSiblings"; + // Brackets/Braces + ListFormat[ListFormat["Braces"] = 512] = "Braces"; + ListFormat[ListFormat["Parenthesis"] = 1024] = "Parenthesis"; + ListFormat[ListFormat["AngleBrackets"] = 2048] = "AngleBrackets"; + ListFormat[ListFormat["SquareBrackets"] = 4096] = "SquareBrackets"; + ListFormat[ListFormat["BracketsMask"] = 7680] = "BracketsMask"; + ListFormat[ListFormat["OptionalIfUndefined"] = 8192] = "OptionalIfUndefined"; + ListFormat[ListFormat["OptionalIfEmpty"] = 16384] = "OptionalIfEmpty"; + ListFormat[ListFormat["Optional"] = 24576] = "Optional"; + // Other + ListFormat[ListFormat["PreferNewLine"] = 32768] = "PreferNewLine"; + ListFormat[ListFormat["NoTrailingNewLine"] = 65536] = "NoTrailingNewLine"; + ListFormat[ListFormat["NoInterveningComments"] = 131072] = "NoInterveningComments"; + // Precomputed Formats + ListFormat[ListFormat["Modifiers"] = 256] = "Modifiers"; + ListFormat[ListFormat["HeritageClauses"] = 256] = "HeritageClauses"; + ListFormat[ListFormat["TypeLiteralMembers"] = 65] = "TypeLiteralMembers"; + ListFormat[ListFormat["TupleTypeElements"] = 336] = "TupleTypeElements"; + ListFormat[ListFormat["UnionTypeConstituents"] = 260] = "UnionTypeConstituents"; + ListFormat[ListFormat["IntersectionTypeConstituents"] = 264] = "IntersectionTypeConstituents"; + ListFormat[ListFormat["ObjectBindingPatternElements"] = 432] = "ObjectBindingPatternElements"; + ListFormat[ListFormat["ArrayBindingPatternElements"] = 304] = "ArrayBindingPatternElements"; + ListFormat[ListFormat["ObjectLiteralExpressionProperties"] = 978] = "ObjectLiteralExpressionProperties"; + ListFormat[ListFormat["ArrayLiteralExpressionElements"] = 4466] = "ArrayLiteralExpressionElements"; + ListFormat[ListFormat["CallExpressionArguments"] = 1296] = "CallExpressionArguments"; + ListFormat[ListFormat["NewExpressionArguments"] = 9488] = "NewExpressionArguments"; + ListFormat[ListFormat["TemplateExpressionSpans"] = 131072] = "TemplateExpressionSpans"; + ListFormat[ListFormat["SingleLineBlockStatements"] = 384] = "SingleLineBlockStatements"; + ListFormat[ListFormat["MultiLineBlockStatements"] = 65] = "MultiLineBlockStatements"; + ListFormat[ListFormat["VariableDeclarationList"] = 272] = "VariableDeclarationList"; + ListFormat[ListFormat["SingleLineFunctionBodyStatements"] = 384] = "SingleLineFunctionBodyStatements"; + ListFormat[ListFormat["MultiLineFunctionBodyStatements"] = 1] = "MultiLineFunctionBodyStatements"; + ListFormat[ListFormat["ClassHeritageClauses"] = 256] = "ClassHeritageClauses"; + ListFormat[ListFormat["ClassMembers"] = 65] = "ClassMembers"; + ListFormat[ListFormat["InterfaceMembers"] = 65] = "InterfaceMembers"; + ListFormat[ListFormat["EnumMembers"] = 81] = "EnumMembers"; + ListFormat[ListFormat["CaseBlockClauses"] = 65] = "CaseBlockClauses"; + ListFormat[ListFormat["NamedImportsOrExportsElements"] = 432] = "NamedImportsOrExportsElements"; + ListFormat[ListFormat["JsxElementChildren"] = 131072] = "JsxElementChildren"; + ListFormat[ListFormat["JsxElementAttributes"] = 131328] = "JsxElementAttributes"; + ListFormat[ListFormat["CaseOrDefaultClauseStatements"] = 81985] = "CaseOrDefaultClauseStatements"; + ListFormat[ListFormat["HeritageClauseTypes"] = 272] = "HeritageClauseTypes"; + ListFormat[ListFormat["SourceFileStatements"] = 65537] = "SourceFileStatements"; + ListFormat[ListFormat["Decorators"] = 24577] = "Decorators"; + ListFormat[ListFormat["TypeArguments"] = 26960] = "TypeArguments"; + ListFormat[ListFormat["TypeParameters"] = 26960] = "TypeParameters"; + ListFormat[ListFormat["Parameters"] = 1360] = "Parameters"; + ListFormat[ListFormat["IndexSignatureParameters"] = 4432] = "IndexSignatureParameters"; + })(ListFormat || (ListFormat = {})); +})(ts || (ts = {})); +/// +/// +/// +var ts; +(function (ts) { + /** The version of the TypeScript compiler release */ + ts.version = "2.1.0"; + var emptyArray = []; + function findConfigFile(searchPath, fileExists, configName) { + if (configName === void 0) { configName = "tsconfig.json"; } while (true) { - var baseName = ts.getBaseFileName(directory); - if (baseName !== "node_modules") { - // Try to load source from the package - var packageResult = loadModuleFromNodeModulesFolder(moduleName, directory, failedLookupLocations, state); - if (packageResult && ts.hasTypeScriptFileExtension(packageResult)) { - // Always prefer a TypeScript (.ts, .tsx, .d.ts) file shipped with the package - return packageResult; - } - else { - // Else prefer a types package over non-TypeScript results (e.g. JavaScript files) - var typesResult = loadModuleFromNodeModulesFolder(ts.combinePaths("@types", moduleName), directory, failedLookupLocations, state); - if (typesResult || packageResult) { - return typesResult || packageResult; - } - } + var fileName = ts.combinePaths(searchPath, configName); + if (fileExists(fileName)) { + return fileName; } - var parentPath = ts.getDirectoryPath(directory); - if (parentPath === directory) { + var parentPath = ts.getDirectoryPath(searchPath); + if (parentPath === searchPath) { break; } - directory = parentPath; + searchPath = parentPath; } return undefined; } - function classicNameResolver(moduleName, containingFile, compilerOptions, host) { - var traceEnabled = isTraceEnabled(compilerOptions, host); - var state = { compilerOptions: compilerOptions, host: host, traceEnabled: traceEnabled, skipTsx: !compilerOptions.jsx }; - var failedLookupLocations = []; - var supportedExtensions = ts.getSupportedExtensions(compilerOptions); - var containingDirectory = ts.getDirectoryPath(containingFile); - var resolvedFileName = tryLoadModuleUsingOptionalResolutionSettings(moduleName, containingDirectory, loadModuleFromFile, failedLookupLocations, supportedExtensions, state); - if (resolvedFileName) { - return createResolvedModule(resolvedFileName, /*isExternalLibraryImport*/ false, failedLookupLocations); - } - var referencedSourceFile; - if (moduleHasNonRelativeName(moduleName)) { - while (true) { - var searchName = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); - referencedSourceFile = loadModuleFromFile(searchName, supportedExtensions, failedLookupLocations, /*onlyRecordFailures*/ false, state); - if (referencedSourceFile) { - break; - } - var parentPath = ts.getDirectoryPath(containingDirectory); - if (parentPath === containingDirectory) { + ts.findConfigFile = findConfigFile; + function resolveTripleslashReference(moduleName, containingFile) { + var basePath = ts.getDirectoryPath(containingFile); + var referencedFileName = ts.isRootedDiskPath(moduleName) ? moduleName : ts.combinePaths(basePath, moduleName); + return ts.normalizePath(referencedFileName); + } + ts.resolveTripleslashReference = resolveTripleslashReference; + /* @internal */ + function computeCommonSourceDirectoryOfFilenames(fileNames, currentDirectory, getCanonicalFileName) { + var commonPathComponents; + var failed = ts.forEach(fileNames, function (sourceFile) { + // Each file contributes into common source file path + var sourcePathComponents = ts.getNormalizedPathComponents(sourceFile, currentDirectory); + sourcePathComponents.pop(); // The base file name is not part of the common directory path + if (!commonPathComponents) { + // first file + commonPathComponents = sourcePathComponents; + return; + } + for (var i = 0, n = Math.min(commonPathComponents.length, sourcePathComponents.length); i < n; i++) { + if (getCanonicalFileName(commonPathComponents[i]) !== getCanonicalFileName(sourcePathComponents[i])) { + if (i === 0) { + // Failed to find any common path component + return true; + } + // New common path found that is 0 -> i-1 + commonPathComponents.length = i; break; } - containingDirectory = parentPath; } + // If the sourcePathComponents was shorter than the commonPathComponents, truncate to the sourcePathComponents + if (sourcePathComponents.length < commonPathComponents.length) { + commonPathComponents.length = sourcePathComponents.length; + } + }); + // A common path can not be found when paths span multiple drives on windows, for example + if (failed) { + return ""; } - else { - var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); - referencedSourceFile = loadModuleFromFile(candidate, supportedExtensions, failedLookupLocations, /*onlyRecordFailures*/ false, state); + if (!commonPathComponents) { + return currentDirectory; } - return referencedSourceFile - ? { resolvedModule: { resolvedFileName: referencedSourceFile }, failedLookupLocations: failedLookupLocations } - : { resolvedModule: undefined, failedLookupLocations: failedLookupLocations }; + return ts.getNormalizedPathFromPathComponents(commonPathComponents); } - ts.classicNameResolver = classicNameResolver; + ts.computeCommonSourceDirectoryOfFilenames = computeCommonSourceDirectoryOfFilenames; function createCompilerHost(options, setParentNodes) { var existingDirectories = ts.createMap(); function getCanonicalFileName(fileName) { @@ -57672,7 +58101,7 @@ var ts; readFile: function (fileName) { return ts.sys.readFile(fileName); }, trace: function (s) { return ts.sys.write(s + newLine); }, directoryExists: function (directoryName) { return ts.sys.directoryExists(directoryName); }, - getEnvironmentVariable: function (name) { return ts.getEnvironmentVariable(name, /*host*/ undefined); }, + getEnvironmentVariable: function (name) { return ts.sys.getEnvironmentVariable ? ts.sys.getEnvironmentVariable(name) : ""; }, getDirectories: function (path) { return ts.sys.getDirectories(path); }, realpath: realpath }; @@ -57740,45 +58169,6 @@ var ts; } return resolutions; } - /** - * Given a set of options, returns the set of type directive names - * that should be included for this program automatically. - * This list could either come from the config file, - * or from enumerating the types root + initial secondary types lookup location. - * More type directives might appear in the program later as a result of loading actual source files; - * this list is only the set of defaults that are implicitly included. - */ - function getAutomaticTypeDirectiveNames(options, host) { - // Use explicit type list from tsconfig.json - if (options.types) { - return options.types; - } - // Walk the primary type lookup locations - var result = []; - if (host.directoryExists && host.getDirectories) { - var typeRoots = getEffectiveTypeRoots(options, host); - if (typeRoots) { - for (var _i = 0, typeRoots_1 = typeRoots; _i < typeRoots_1.length; _i++) { - var root = typeRoots_1[_i]; - if (host.directoryExists(root)) { - for (var _a = 0, _b = host.getDirectories(root); _a < _b.length; _a++) { - var typeDirectivePath = _b[_a]; - var normalized = ts.normalizePath(typeDirectivePath); - var packageJsonPath = pathToPackageJson(ts.combinePaths(root, normalized)); - // tslint:disable-next-line:no-null-keyword - var isNotNeededPackage = host.fileExists(packageJsonPath) && readJson(packageJsonPath, host).typings === null; - if (!isNotNeededPackage) { - // Return just the type directive names - result.push(ts.getBaseFileName(normalized)); - } - } - } - } - } - } - return result; - } - ts.getAutomaticTypeDirectiveNames = getAutomaticTypeDirectiveNames; function createProgram(rootNames, options, host, oldProgram) { var program; var files = []; @@ -57815,7 +58205,7 @@ var ts; resolveModuleNamesWorker = function (moduleNames, containingFile) { return host.resolveModuleNames(moduleNames, containingFile); }; } else { - var loader_1 = function (moduleName, containingFile) { return resolveModuleName(moduleName, containingFile, options, host).resolvedModule; }; + var loader_1 = function (moduleName, containingFile) { return ts.resolveModuleName(moduleName, containingFile, options, host).resolvedModule; }; resolveModuleNamesWorker = function (moduleNames, containingFile) { return loadWithLocalCache(moduleNames, containingFile, loader_1); }; } var resolveTypeReferenceDirectiveNamesWorker; @@ -57823,7 +58213,7 @@ var ts; resolveTypeReferenceDirectiveNamesWorker = function (typeDirectiveNames, containingFile) { return host.resolveTypeReferenceDirectives(typeDirectiveNames, containingFile); }; } else { - var loader_2 = function (typesRef, containingFile) { return resolveTypeReferenceDirective(typesRef, containingFile, options, host).resolvedTypeReferenceDirective; }; + var loader_2 = function (typesRef, containingFile) { return ts.resolveTypeReferenceDirective(typesRef, containingFile, options, host).resolvedTypeReferenceDirective; }; resolveTypeReferenceDirectiveNamesWorker = function (typeReferenceDirectiveNames, containingFile) { return loadWithLocalCache(typeReferenceDirectiveNames, containingFile, loader_2); }; } var filesByName = ts.createFileMap(); @@ -57833,8 +58223,8 @@ var ts; if (!tryReuseStructureFromOldProgram()) { ts.forEach(rootNames, function (name) { return processRootFile(name, /*isDefaultLib*/ false); }); // load type declarations specified via 'types' argument or implicitly from types/ and node_modules/@types folders - var typeReferences = getAutomaticTypeDirectiveNames(options, host); - if (typeReferences) { + var typeReferences = ts.getAutomaticTypeDirectiveNames(options, host); + if (typeReferences.length) { // This containingFilename needs to match with the one used in managed-side var containingFilename = ts.combinePaths(host.getCurrentDirectory(), "__inferred type names__.ts"); var resolutions = resolveTypeReferenceDirectiveNamesWorker(typeReferences, containingFilename); @@ -57884,7 +58274,8 @@ var ts; getSymbolCount: function () { return getDiagnosticsProducingTypeChecker().getSymbolCount(); }, getTypeCount: function () { return getDiagnosticsProducingTypeChecker().getTypeCount(); }, getFileProcessingDiagnostics: function () { return fileProcessingDiagnostics; }, - getResolvedTypeReferenceDirectives: function () { return resolvedTypeReferenceDirectives; } + getResolvedTypeReferenceDirectives: function () { return resolvedTypeReferenceDirectives; }, + dropDiagnosticsProducingTypeChecker: dropDiagnosticsProducingTypeChecker }; verifyCompilerOptions(); ts.performance.mark("afterProgram"); @@ -57938,6 +58329,7 @@ var ts; (oldOptions.configFilePath !== options.configFilePath) || (oldOptions.baseUrl !== options.baseUrl) || (oldOptions.maxNodeModuleJsDepth !== options.maxNodeModuleJsDepth) || + !ts.arrayIsEqualTo(oldOptions.lib, options.lib) || !ts.arrayIsEqualTo(oldOptions.typeRoots, oldOptions.typeRoots) || !ts.arrayIsEqualTo(oldOptions.rootDirs, options.rootDirs) || !ts.equalOwnProperties(oldOptions.paths, options.paths)) { @@ -58054,16 +58446,19 @@ var ts; function getDiagnosticsProducingTypeChecker() { return diagnosticsProducingTypeChecker || (diagnosticsProducingTypeChecker = ts.createTypeChecker(program, /*produceDiagnostics:*/ true)); } + function dropDiagnosticsProducingTypeChecker() { + diagnosticsProducingTypeChecker = undefined; + } function getTypeChecker() { return noDiagnosticsTypeChecker || (noDiagnosticsTypeChecker = ts.createTypeChecker(program, /*produceDiagnostics:*/ false)); } - function emit(sourceFile, writeFileCallback, cancellationToken) { - return runWithCancellationToken(function () { return emitWorker(program, sourceFile, writeFileCallback, cancellationToken); }); + function emit(sourceFile, writeFileCallback, cancellationToken, emitOnlyDtsFiles) { + return runWithCancellationToken(function () { return emitWorker(program, sourceFile, writeFileCallback, cancellationToken, emitOnlyDtsFiles); }); } function isEmitBlocked(emitFileName) { return hasEmitBlockingDiagnostics.contains(ts.toPath(emitFileName, currentDirectory, getCanonicalFileName)); } - function emitWorker(program, sourceFile, writeFileCallback, cancellationToken) { + function emitWorker(program, sourceFile, writeFileCallback, cancellationToken, emitOnlyDtsFiles) { var declarationDiagnostics = []; if (options.noEmit) { return { diagnostics: declarationDiagnostics, sourceMaps: undefined, emittedFiles: undefined, emitSkipped: true }; @@ -58095,7 +58490,7 @@ var ts; // checked is to not pass the file to getEmitResolver. var emitResolver = getDiagnosticsProducingTypeChecker().getEmitResolver((options.outFile || options.out) ? undefined : sourceFile); ts.performance.mark("beforeEmit"); - var emitResult = ts.emitFiles(emitResolver, getEmitHost(writeFileCallback), sourceFile); + var emitResult = ts.emitFiles(emitResolver, getEmitHost(writeFileCallback), sourceFile, emitOnlyDtsFiles); ts.performance.mark("afterEmit"); ts.performance.measure("Emit", "beforeEmit", "afterEmit"); return emitResult; @@ -58659,7 +59054,6 @@ var ts; for (var i = 0; i < moduleNames.length; i++) { var resolution = resolutions[i]; ts.setResolvedModule(file, moduleNames[i], resolution); - var resolvedPath = resolution ? ts.toPath(resolution.resolvedFileName, currentDirectory, getCanonicalFileName) : undefined; // add file to program only if: // - resolution was successful // - noResolve is falsy @@ -58676,7 +59070,7 @@ var ts; modulesWithElidedImports[file.path] = true; } else if (shouldAddFile) { - findSourceFile(resolution.resolvedFileName, resolvedPath, + findSourceFile(resolution.resolvedFileName, ts.toPath(resolution.resolvedFileName, currentDirectory, getCanonicalFileName), /*isDefaultLib*/ false, /*isReference*/ false, file, ts.skipTrivia(file.text, file.imports[i].pos), file.imports[i].end); } if (isFromNodeModulesSearch) { @@ -58692,8 +59086,8 @@ var ts; } function computeCommonSourceDirectory(sourceFiles) { var fileNames = []; - for (var _i = 0, sourceFiles_5 = sourceFiles; _i < sourceFiles_5.length; _i++) { - var file = sourceFiles_5[_i]; + for (var _i = 0, sourceFiles_6 = sourceFiles; _i < sourceFiles_6.length; _i++) { + var file = sourceFiles_6[_i]; if (!file.isDeclarationFile) { fileNames.push(file.fileName); } @@ -58704,8 +59098,8 @@ var ts; var allFilesBelongToPath = true; if (sourceFiles) { var absoluteRootDirectoryPath = host.getCanonicalFileName(ts.getNormalizedAbsolutePath(rootDirectory, currentDirectory)); - for (var _i = 0, sourceFiles_6 = sourceFiles; _i < sourceFiles_6.length; _i++) { - var sourceFile = sourceFiles_6[_i]; + for (var _i = 0, sourceFiles_7 = sourceFiles; _i < sourceFiles_7.length; _i++) { + var sourceFile = sourceFiles_7[_i]; if (!ts.isDeclarationFile(sourceFile)) { var absoluteSourceFilePath = host.getCanonicalFileName(ts.getNormalizedAbsolutePath(sourceFile.fileName, currentDirectory)); if (absoluteSourceFilePath.indexOf(absoluteRootDirectoryPath) !== 0) { @@ -58748,7 +59142,7 @@ var ts; if (!ts.hasProperty(options.paths, key)) { continue; } - if (!hasZeroOrOneAsteriskCharacter(key)) { + if (!ts.hasZeroOrOneAsteriskCharacter(key)) { programDiagnostics.add(ts.createCompilerDiagnostic(ts.Diagnostics.Pattern_0_can_have_at_most_one_Asterisk_character, key)); } if (ts.isArray(options.paths[key])) { @@ -58759,7 +59153,7 @@ var ts; var subst = _a[_i]; var typeOfSubst = typeof subst; if (typeOfSubst === "string") { - if (!hasZeroOrOneAsteriskCharacter(subst)) { + if (!ts.hasZeroOrOneAsteriskCharacter(subst)) { programDiagnostics.add(ts.createCompilerDiagnostic(ts.Diagnostics.Substitution_0_in_pattern_1_in_can_have_at_most_one_Asterisk_character, subst, key)); } } @@ -58799,6 +59193,9 @@ var ts; if (options.lib && options.noLib) { programDiagnostics.add(ts.createCompilerDiagnostic(ts.Diagnostics.Option_0_cannot_be_specified_with_option_1, "lib", "noLib")); } + if (options.noImplicitUseStrict && options.alwaysStrict) { + programDiagnostics.add(ts.createCompilerDiagnostic(ts.Diagnostics.Option_0_cannot_be_specified_with_option_1, "noImplicitUseStrict", "alwaysStrict")); + } var languageVersion = options.target || 0 /* ES3 */; var outFile = options.outFile || options.out; var firstNonAmbientExternalModuleSourceFile = ts.forEach(files, function (f) { return ts.isExternalModule(f) && !ts.isDeclarationFile(f) ? f : undefined; }); @@ -58891,12 +59288,15 @@ var ts; /// var ts; (function (ts) { + /* @internal */ + ts.compileOnSaveCommandLineOption = { name: "compileOnSave", type: "boolean" }; /* @internal */ ts.optionDeclarations = [ { name: "charset", type: "string" }, + ts.compileOnSaveCommandLineOption, { name: "declaration", shortName: "d", @@ -59327,6 +59727,11 @@ var ts; name: "importHelpers", type: "boolean", description: ts.Diagnostics.Import_emit_helpers_from_tslib + }, + { + name: "alwaysStrict", + type: "boolean", + description: ts.Diagnostics.Parse_in_strict_mode_and_emit_use_strict_for_each_source_file } ]; /* @internal */ @@ -59547,10 +59952,11 @@ var ts; * @param fileName The path to the config file * @param jsonText The text of the config file */ - function parseConfigFileTextToJson(fileName, jsonText) { + function parseConfigFileTextToJson(fileName, jsonText, stripComments) { + if (stripComments === void 0) { stripComments = true; } try { - var jsonTextWithoutComments = removeComments(jsonText); - return { config: /\S/.test(jsonTextWithoutComments) ? JSON.parse(jsonTextWithoutComments) : {} }; + var jsonTextToParse = stripComments ? removeComments(jsonText) : jsonText; + return { config: /\S/.test(jsonTextToParse) ? JSON.parse(jsonTextToParse) : {} }; } catch (e) { return { error: ts.createCompilerDiagnostic(ts.Diagnostics.Failed_to_parse_file_0_Colon_1, fileName, e.message) }; @@ -59712,13 +60118,15 @@ var ts; options = ts.extend(existingOptions, options); options.configFilePath = configFileName; var _c = getFileNames(errors), fileNames = _c.fileNames, wildcardDirectories = _c.wildcardDirectories; + var compileOnSave = convertCompileOnSaveOptionFromJson(json, basePath, errors); return { options: options, fileNames: fileNames, typingOptions: typingOptions, raw: json, errors: errors, - wildcardDirectories: wildcardDirectories + wildcardDirectories: wildcardDirectories, + compileOnSave: compileOnSave }; function tryExtendsName(extendedConfig) { // If the path isn't a rooted or relative path, don't try to resolve it (we reserve the right to special case module-id like paths in the future) @@ -59799,6 +60207,17 @@ var ts; var _b; } ts.parseJsonConfigFileContent = parseJsonConfigFileContent; + function convertCompileOnSaveOptionFromJson(jsonOption, basePath, errors) { + if (!ts.hasProperty(jsonOption, ts.compileOnSaveCommandLineOption.name)) { + return false; + } + var result = convertJsonOption(ts.compileOnSaveCommandLineOption, jsonOption["compileOnSave"], basePath, errors); + if (typeof result === "boolean" && result) { + return result; + } + return false; + } + ts.convertCompileOnSaveOptionFromJson = convertCompileOnSaveOptionFromJson; function convertCompilerOptionsFromJson(jsonOptions, basePath, configFileName) { var errors = []; var options = convertCompilerOptionsFromJsonWorker(jsonOptions, basePath, errors, configFileName); @@ -59812,7 +60231,9 @@ var ts; } ts.convertTypingOptionsFromJson = convertTypingOptionsFromJson; function convertCompilerOptionsFromJsonWorker(jsonOptions, basePath, errors, configFileName) { - var options = ts.getBaseFileName(configFileName) === "jsconfig.json" ? { allowJs: true, maxNodeModuleJsDepth: 2 } : {}; + var options = ts.getBaseFileName(configFileName) === "jsconfig.json" + ? { allowJs: true, maxNodeModuleJsDepth: 2, allowSyntheticDefaultImports: true, skipLibCheck: true } + : {}; convertOptionsFromJson(ts.optionDeclarations, jsonOptions, basePath, options, ts.Diagnostics.Unknown_compiler_option_0, errors); return options; } @@ -60742,7 +61163,7 @@ var ts; return true; case 69 /* Identifier */: // 'this' as a parameter - return node.originalKeywordKind === 97 /* ThisKeyword */ && node.parent.kind === 142 /* Parameter */; + return ts.identifierIsThisKeyword(node) && node.parent.kind === 142 /* Parameter */; default: return false; } @@ -61420,7 +61841,6 @@ var ts; })(ts || (ts = {})); // Display-part writer helpers /* @internal */ -var ts; (function (ts) { function isFirstDeclarationOfSymbolParameter(symbol) { return symbol.declarations && symbol.declarations.length > 0 && symbol.declarations[0].kind === 142 /* Parameter */; @@ -61657,7 +62077,7 @@ var ts; return ts.ensureScriptKind(fileName, scriptKind); } ts.getScriptKind = getScriptKind; - function parseAndReEmitConfigJSONFile(content) { + function sanitizeConfigFile(configFileName, content) { var options = { fileName: "config.js", compilerOptions: { @@ -61671,14 +62091,17 @@ var ts; // also, the emitted result will have "(" in the beginning and ");" in the end. We need to strip these // as well var trimmedOutput = outputText.trim(); - var configJsonObject = JSON.parse(trimmedOutput.substring(1, trimmedOutput.length - 2)); for (var _i = 0, diagnostics_2 = diagnostics; _i < diagnostics_2.length; _i++) { var diagnostic = diagnostics_2[_i]; diagnostic.start = diagnostic.start - 1; } - return { configJsonObject: configJsonObject, diagnostics: diagnostics }; + var _b = ts.parseConfigFileTextToJson(configFileName, trimmedOutput.substring(1, trimmedOutput.length - 2), /*stripComments*/ false), config = _b.config, error = _b.error; + return { + configJsonObject: config || {}, + diagnostics: error ? ts.concatenate(diagnostics, [error]) : diagnostics + }; } - ts.parseAndReEmitConfigJSONFile = parseAndReEmitConfigJSONFile; + ts.sanitizeConfigFile = sanitizeConfigFile; })(ts || (ts = {})); var ts; (function (ts) { @@ -62538,8 +62961,7 @@ var ts; return; case 142 /* Parameter */: if (token.parent.name === token) { - var isThis_1 = token.kind === 69 /* Identifier */ && token.originalKeywordKind === 97 /* ThisKeyword */; - return isThis_1 ? 3 /* keyword */ : 17 /* parameterName */; + return ts.isThisIdentifier(token) ? 3 /* keyword */ : 17 /* parameterName */; } return; } @@ -62583,14 +63005,14 @@ var ts; if (!completionData) { return undefined; } - var symbols = completionData.symbols, isMemberCompletion = completionData.isMemberCompletion, isNewIdentifierLocation = completionData.isNewIdentifierLocation, location = completionData.location, isJsDocTagName = completionData.isJsDocTagName; + var symbols = completionData.symbols, isGlobalCompletion = completionData.isGlobalCompletion, isMemberCompletion = completionData.isMemberCompletion, isNewIdentifierLocation = completionData.isNewIdentifierLocation, location = completionData.location, isJsDocTagName = completionData.isJsDocTagName; if (isJsDocTagName) { // If the current position is a jsDoc tag name, only tag names should be provided for completion - return { isMemberCompletion: false, isNewIdentifierLocation: false, entries: ts.JsDoc.getAllJsDocCompletionEntries() }; + return { isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: false, entries: ts.JsDoc.getAllJsDocCompletionEntries() }; } var entries = []; if (ts.isSourceFileJavaScript(sourceFile)) { - var uniqueNames = getCompletionEntriesFromSymbols(symbols, entries, location, /*performCharacterChecks*/ false); + var uniqueNames = getCompletionEntriesFromSymbols(symbols, entries, location, /*performCharacterChecks*/ true); ts.addRange(entries, getJavaScriptCompletionEntries(sourceFile, location.pos, uniqueNames)); } else { @@ -62619,7 +63041,7 @@ var ts; if (!isMemberCompletion && !isJsDocTagName) { ts.addRange(entries, keywordCompletions); } - return { isMemberCompletion: isMemberCompletion, isNewIdentifierLocation: isNewIdentifierLocation || ts.isSourceFileJavaScript(sourceFile), entries: entries }; + return { isGlobalCompletion: isGlobalCompletion, isMemberCompletion: isMemberCompletion, isNewIdentifierLocation: isNewIdentifierLocation, entries: entries }; function getJavaScriptCompletionEntries(sourceFile, position, uniqueNames) { var entries = []; var nameTable = ts.getNameTable(sourceFile); @@ -62690,7 +63112,9 @@ var ts; if (!node || node.kind !== 9 /* StringLiteral */) { return undefined; } - if (node.parent.kind === 253 /* PropertyAssignment */ && node.parent.parent.kind === 171 /* ObjectLiteralExpression */) { + if (node.parent.kind === 253 /* PropertyAssignment */ && + node.parent.parent.kind === 171 /* ObjectLiteralExpression */ && + node.parent.name === node) { // Get quoted name of properties of the object literal expression // i.e. interface ConfigFiles { // 'jspm:dev': string @@ -62740,7 +63164,7 @@ var ts; if (type) { getCompletionEntriesFromSymbols(type.getApparentProperties(), entries, element, /*performCharacterChecks*/ false); if (entries.length) { - return { isMemberCompletion: true, isNewIdentifierLocation: true, entries: entries }; + return { isGlobalCompletion: false, isMemberCompletion: true, isNewIdentifierLocation: true, entries: entries }; } } } @@ -62756,7 +63180,7 @@ var ts; } } if (entries.length) { - return { isMemberCompletion: false, isNewIdentifierLocation: true, entries: entries }; + return { isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: true, entries: entries }; } return undefined; } @@ -62766,7 +63190,7 @@ var ts; if (type) { getCompletionEntriesFromSymbols(type.getApparentProperties(), entries, node, /*performCharacterChecks*/ false); if (entries.length) { - return { isMemberCompletion: true, isNewIdentifierLocation: true, entries: entries }; + return { isGlobalCompletion: false, isMemberCompletion: true, isNewIdentifierLocation: true, entries: entries }; } } return undefined; @@ -62777,7 +63201,7 @@ var ts; var entries_2 = []; addStringLiteralCompletionsFromType(type, entries_2); if (entries_2.length) { - return { isMemberCompletion: false, isNewIdentifierLocation: false, entries: entries_2 }; + return { isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: false, entries: entries_2 }; } } return undefined; @@ -62819,6 +63243,7 @@ var ts; entries = getCompletionEntriesForNonRelativeModules(literalValue, scriptDirectory, span); } return { + isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: true, entries: entries @@ -62854,15 +63279,24 @@ var ts; } return result; } + /** + * Given a path ending at a directory, gets the completions for the path, and filters for those entries containing the basename. + */ function getCompletionEntriesForDirectoryFragment(fragment, scriptPath, extensions, includeExtensions, span, exclude, result) { if (result === void 0) { result = []; } - fragment = ts.getDirectoryPath(fragment); - if (!fragment) { - fragment = "./"; + if (fragment === undefined) { + fragment = ""; } - else { - fragment = ts.ensureTrailingDirectorySeparator(fragment); + fragment = ts.normalizeSlashes(fragment); + /** + * Remove the basename from the path. Note that we don't use the basename to filter completions; + * the client is responsible for refining completions. + */ + fragment = ts.getDirectoryPath(fragment); + if (fragment === "") { + fragment = "." + ts.directorySeparator; } + fragment = ts.ensureTrailingDirectorySeparator(fragment); var absolutePath = normalizeAndPreserveTrailingSlash(ts.isRootedDiskPath(fragment) ? fragment : ts.combinePaths(scriptPath, fragment)); var baseDirectory = ts.getDirectoryPath(absolutePath); var ignoreCase = !(host.useCaseSensitiveFileNames && host.useCaseSensitiveFileNames()); @@ -62870,6 +63304,12 @@ var ts; // Enumerate the available files if possible var files = tryReadDirectory(host, baseDirectory, extensions, /*exclude*/ undefined, /*include*/ ["./*"]); if (files) { + /** + * Multiple file entries might map to the same truncated name once we remove extensions + * (happens iff includeExtensions === false)so we use a set-like data structure. Eg: + * + * both foo.ts and foo.tsx become foo + */ var foundFiles = ts.createMap(); for (var _i = 0, files_3 = files; _i < files_3.length; _i++) { var filePath = files_3[_i]; @@ -63039,6 +63479,19 @@ var ts; if (!range) { return undefined; } + var completionInfo = { + /** + * We don't want the editor to offer any other completions, such as snippets, inside a comment. + */ + isGlobalCompletion: false, + isMemberCompletion: false, + /** + * The user may type in a path that doesn't yet exist, creating a "new identifier" + * with respect to the collection of identifiers the server is aware of. + */ + isNewIdentifierLocation: true, + entries: [] + }; var text = sourceFile.text.substr(range.pos, position - range.pos); var match = tripleSlashDirectiveFragmentRegex.exec(text); if (match) { @@ -63046,24 +63499,18 @@ var ts; var kind = match[2]; var toComplete = match[3]; var scriptPath = ts.getDirectoryPath(sourceFile.path); - var entries_3; if (kind === "path") { // Give completions for a relative path var span_10 = getDirectoryFragmentTextSpan(toComplete, range.pos + prefix.length); - entries_3 = getCompletionEntriesForDirectoryFragment(toComplete, scriptPath, ts.getSupportedExtensions(compilerOptions), /*includeExtensions*/ true, span_10, sourceFile.path); + completionInfo.entries = getCompletionEntriesForDirectoryFragment(toComplete, scriptPath, ts.getSupportedExtensions(compilerOptions), /*includeExtensions*/ true, span_10, sourceFile.path); } else { // Give completions based on the typings available var span_11 = { start: range.pos + prefix.length, length: match[0].length - prefix.length }; - entries_3 = getCompletionEntriesFromTypings(host, compilerOptions, scriptPath, span_11); + completionInfo.entries = getCompletionEntriesFromTypings(host, compilerOptions, scriptPath, span_11); } - return { - isMemberCompletion: false, - isNewIdentifierLocation: true, - entries: entries_3 - }; } - return undefined; + return completionInfo; } function getCompletionEntriesFromTypings(host, options, scriptPath, span, result) { if (result === void 0) { result = []; } @@ -63285,7 +63732,7 @@ var ts; } } if (isJsDocTagName) { - return { symbols: undefined, isMemberCompletion: false, isNewIdentifierLocation: false, location: undefined, isRightOfDot: false, isJsDocTagName: isJsDocTagName }; + return { symbols: undefined, isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: false, location: undefined, isRightOfDot: false, isJsDocTagName: isJsDocTagName }; } if (!insideJsDocTagExpression) { // Proceed if the current position is in jsDoc tag expression; otherwise it is a normal @@ -63349,6 +63796,7 @@ var ts; } } var semanticStart = ts.timestamp(); + var isGlobalCompletion = false; var isMemberCompletion; var isNewIdentifierLocation; var symbols = []; @@ -63382,11 +63830,13 @@ var ts; if (!tryGetGlobalSymbols()) { return undefined; } + isGlobalCompletion = true; } log("getCompletionData: Semantic work: " + (ts.timestamp() - semanticStart)); - return { symbols: symbols, isMemberCompletion: isMemberCompletion, isNewIdentifierLocation: isNewIdentifierLocation, location: location, isRightOfDot: (isRightOfDot || isRightOfOpenTag), isJsDocTagName: isJsDocTagName }; + return { symbols: symbols, isGlobalCompletion: isGlobalCompletion, isMemberCompletion: isMemberCompletion, isNewIdentifierLocation: isNewIdentifierLocation, location: location, isRightOfDot: (isRightOfDot || isRightOfOpenTag), isJsDocTagName: isJsDocTagName }; function getTypeScriptMemberSymbols() { // Right of dot member completion list + isGlobalCompletion = false; isMemberCompletion = true; isNewIdentifierLocation = false; if (node.kind === 69 /* Identifier */ || node.kind === 139 /* QualifiedName */ || node.kind === 172 /* PropertyAccessExpression */) { @@ -63448,6 +63898,7 @@ var ts; if ((jsxContainer.kind === 242 /* JsxSelfClosingElement */) || (jsxContainer.kind === 243 /* JsxOpeningElement */)) { // Cursor is inside a JSX self-closing element or opening element attrsType = typeChecker.getJsxElementAttributesType(jsxContainer); + isGlobalCompletion = false; if (attrsType) { symbols = filterJsxAttributes(typeChecker.getPropertiesOfType(attrsType), jsxContainer.attributes); isMemberCompletion = true; @@ -64026,9 +64477,15 @@ var ts; * Matches a triple slash reference directive with an incomplete string literal for its path. Used * to determine if the caret is currently within the string literal and capture the literal fragment * for completions. - * For example, this matches /// +/// +/// +/// /* @internal */ var ts; (function (ts) { @@ -66442,6 +66901,7 @@ var ts; // A map of loose file names to library names // that we are confident require typings var safeList; + var EmptySafeList = ts.createMap(); /** * @param host is the object providing I/O related operations. * @param fileNames are the file names that belong to the same project @@ -66458,10 +66918,13 @@ var ts; return { cachedTypingPaths: [], newTypingNames: [], filesToWatch: [] }; } // Only infer typings for .js and .jsx files - fileNames = ts.filter(ts.map(fileNames, ts.normalizePath), function (f) { return ts.scriptKindIs(f, /*LanguageServiceHost*/ undefined, 1 /* JS */, 2 /* JSX */); }); + fileNames = ts.filter(ts.map(fileNames, ts.normalizePath), function (f) { + var kind = ts.ensureScriptKind(f, ts.getScriptKindFromFileName(f)); + return kind === 1 /* JS */ || kind === 2 /* JSX */; + }); if (!safeList) { var result = ts.readConfigFile(safeListPath, function (path) { return host.readFile(path); }); - safeList = ts.createMap(result.config); + safeList = result.config ? ts.createMap(result.config) : EmptySafeList; } var filesToWatch = []; // Directories to search for package.json, bower.json and other typing information @@ -66552,13 +67015,10 @@ var ts; var jsFileNames = ts.filter(fileNames, ts.hasJavaScriptFileExtension); var inferredTypingNames = ts.map(jsFileNames, function (f) { return ts.removeFileExtension(ts.getBaseFileName(f.toLowerCase())); }); var cleanedTypingNames = ts.map(inferredTypingNames, function (f) { return f.replace(/((?:\.|-)min(?=\.|$))|((?:-|\.)\d+)/g, ""); }); - if (safeList === undefined) { - mergeTypings(cleanedTypingNames); - } - else { + if (safeList !== EmptySafeList) { mergeTypings(ts.filter(cleanedTypingNames, function (f) { return f in safeList; })); } - var hasJsxFile = ts.forEach(fileNames, function (f) { return ts.scriptKindIs(f, /*LanguageServiceHost*/ undefined, 2 /* JSX */); }); + var hasJsxFile = ts.forEach(fileNames, function (f) { return ts.ensureScriptKind(f, ts.getScriptKindFromFileName(f)) === 2 /* JSX */; }); if (hasJsxFile) { mergeTypings(["react"]); } @@ -66573,7 +67033,7 @@ var ts; return; } var typingNames = []; - var fileNames = host.readDirectory(nodeModulesPath, ["*.json"], /*excludes*/ undefined, /*includes*/ undefined, /*depth*/ 2); + var fileNames = host.readDirectory(nodeModulesPath, [".json"], /*excludes*/ undefined, /*includes*/ undefined, /*depth*/ 2); for (var _i = 0, fileNames_2 = fileNames; _i < fileNames_2.length; _i++) { var fileName = fileNames_2[_i]; var normalizedFileName = ts.normalizePath(fileName); @@ -66616,7 +67076,7 @@ var ts; (function (ts) { var NavigateTo; (function (NavigateTo) { - function getNavigateToItems(sourceFiles, checker, cancellationToken, searchValue, maxResultCount) { + function getNavigateToItems(sourceFiles, checker, cancellationToken, searchValue, maxResultCount, excludeDtsFiles) { var patternMatcher = ts.createPatternMatcher(searchValue); var rawItems = []; // This means "compare in a case insensitive manner." @@ -66624,6 +67084,9 @@ var ts; // Search the declarations in all files and output matched NavigateToItem into array of NavigateToItem[] ts.forEach(sourceFiles, function (sourceFile) { cancellationToken.throwIfCancellationRequested(); + if (excludeDtsFiles && ts.fileExtensionIs(sourceFile.fileName, ".d.ts")) { + return; + } var nameToDeclarations = sourceFile.getNamedDeclarations(); for (var name_49 in nameToDeclarations) { var declarations = nameToDeclarations[name_49]; @@ -66803,6 +67266,13 @@ var ts; return result; } NavigationBar.getNavigationBarItems = getNavigationBarItems; + function getNavigationTree(sourceFile) { + curSourceFile = sourceFile; + var result = convertToTree(rootNavigationBarNode(sourceFile)); + curSourceFile = undefined; + return result; + } + NavigationBar.getNavigationTree = getNavigationTree; // Keep sourceFile handy so we don't have to search for it every time we need to call `getText`. var curSourceFile; function nodeText(node) { @@ -67077,7 +67547,7 @@ var ts; } } // More efficient to create a collator once and use its `compare` than to call `a.localeCompare(b)` many times. - var collator = typeof Intl === "undefined" ? undefined : new Intl.Collator(); + var collator = typeof Intl === "object" && typeof Intl.Collator === "function" ? new Intl.Collator() : undefined; // Intl is missing in Safari, and node 0.10 treats "a" as greater than "B". var localeCompareIsCorrect = collator && collator.compare("a", "B") < 0; var localeCompareFix = localeCompareIsCorrect ? collator.compare : function (a, b) { @@ -67242,6 +67712,15 @@ var ts; } // NavigationBarItem requires an array, but will not mutate it, so just give it this for performance. var emptyChildItemArray = []; + function convertToTree(n) { + return { + text: getItemName(n.node), + kind: ts.getNodeKind(n.node), + kindModifiers: ts.getNodeModifiers(n.node), + spans: getSpans(n), + childItems: ts.map(n.children, convertToTree) + }; + } function convertToTopLevelItem(n) { return { text: getItemName(n.node), @@ -67265,16 +67744,16 @@ var ts; grayed: false }; } - function getSpans(n) { - var spans = [getNodeSpan(n.node)]; - if (n.additionalNodes) { - for (var _i = 0, _a = n.additionalNodes; _i < _a.length; _i++) { - var node = _a[_i]; - spans.push(getNodeSpan(node)); - } + } + function getSpans(n) { + var spans = [getNodeSpan(n.node)]; + if (n.additionalNodes) { + for (var _i = 0, _a = n.additionalNodes; _i < _a.length; _i++) { + var node = _a[_i]; + spans.push(getNodeSpan(node)); } - return spans; } + return spans; } function getModuleName(moduleDeclaration) { // We want to maintain quotation marks. @@ -71005,25 +71484,25 @@ var ts; }; RulesProvider.prototype.createActiveRules = function (options) { var rules = this.globalRules.HighPriorityCommonRules.slice(0); - if (options.InsertSpaceAfterCommaDelimiter) { + if (options.insertSpaceAfterCommaDelimiter) { rules.push(this.globalRules.SpaceAfterComma); } else { rules.push(this.globalRules.NoSpaceAfterComma); } - if (options.InsertSpaceAfterFunctionKeywordForAnonymousFunctions) { + if (options.insertSpaceAfterFunctionKeywordForAnonymousFunctions) { rules.push(this.globalRules.SpaceAfterAnonymousFunctionKeyword); } else { rules.push(this.globalRules.NoSpaceAfterAnonymousFunctionKeyword); } - if (options.InsertSpaceAfterKeywordsInControlFlowStatements) { + if (options.insertSpaceAfterKeywordsInControlFlowStatements) { rules.push(this.globalRules.SpaceAfterKeywordInControl); } else { rules.push(this.globalRules.NoSpaceAfterKeywordInControl); } - if (options.InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis) { + if (options.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis) { rules.push(this.globalRules.SpaceAfterOpenParen); rules.push(this.globalRules.SpaceBeforeCloseParen); rules.push(this.globalRules.NoSpaceBetweenParens); @@ -71033,7 +71512,7 @@ var ts; rules.push(this.globalRules.NoSpaceBeforeCloseParen); rules.push(this.globalRules.NoSpaceBetweenParens); } - if (options.InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets) { + if (options.insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets) { rules.push(this.globalRules.SpaceAfterOpenBracket); rules.push(this.globalRules.SpaceBeforeCloseBracket); rules.push(this.globalRules.NoSpaceBetweenBrackets); @@ -71045,7 +71524,7 @@ var ts; } // The default value of InsertSpaceAfterOpeningAndBeforeClosingNonemptyBraces is true // so if the option is undefined, we should treat it as true as well - if (options.InsertSpaceAfterOpeningAndBeforeClosingNonemptyBraces !== false) { + if (options.insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces !== false) { rules.push(this.globalRules.SpaceAfterOpenBrace); rules.push(this.globalRules.SpaceBeforeCloseBrace); rules.push(this.globalRules.NoSpaceBetweenEmptyBraceBrackets); @@ -71055,7 +71534,7 @@ var ts; rules.push(this.globalRules.NoSpaceBeforeCloseBrace); rules.push(this.globalRules.NoSpaceBetweenEmptyBraceBrackets); } - if (options.InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces) { + if (options.insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces) { rules.push(this.globalRules.SpaceAfterTemplateHeadAndMiddle); rules.push(this.globalRules.SpaceBeforeTemplateMiddleAndTail); } @@ -71063,7 +71542,7 @@ var ts; rules.push(this.globalRules.NoSpaceAfterTemplateHeadAndMiddle); rules.push(this.globalRules.NoSpaceBeforeTemplateMiddleAndTail); } - if (options.InsertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces) { + if (options.insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces) { rules.push(this.globalRules.SpaceAfterOpenBraceInJsxExpression); rules.push(this.globalRules.SpaceBeforeCloseBraceInJsxExpression); } @@ -71071,13 +71550,13 @@ var ts; rules.push(this.globalRules.NoSpaceAfterOpenBraceInJsxExpression); rules.push(this.globalRules.NoSpaceBeforeCloseBraceInJsxExpression); } - if (options.InsertSpaceAfterSemicolonInForStatements) { + if (options.insertSpaceAfterSemicolonInForStatements) { rules.push(this.globalRules.SpaceAfterSemicolonInFor); } else { rules.push(this.globalRules.NoSpaceAfterSemicolonInFor); } - if (options.InsertSpaceBeforeAndAfterBinaryOperators) { + if (options.insertSpaceBeforeAndAfterBinaryOperators) { rules.push(this.globalRules.SpaceBeforeBinaryOperator); rules.push(this.globalRules.SpaceAfterBinaryOperator); } @@ -71085,14 +71564,14 @@ var ts; rules.push(this.globalRules.NoSpaceBeforeBinaryOperator); rules.push(this.globalRules.NoSpaceAfterBinaryOperator); } - if (options.PlaceOpenBraceOnNewLineForControlBlocks) { + if (options.placeOpenBraceOnNewLineForControlBlocks) { rules.push(this.globalRules.NewLineBeforeOpenBraceInControl); } - if (options.PlaceOpenBraceOnNewLineForFunctions) { + if (options.placeOpenBraceOnNewLineForFunctions) { rules.push(this.globalRules.NewLineBeforeOpenBraceInFunction); rules.push(this.globalRules.NewLineBeforeOpenBraceInTypeScriptDeclWithBlock); } - if (options.InsertSpaceAfterTypeAssertion) { + if (options.insertSpaceAfterTypeAssertion) { rules.push(this.globalRules.SpaceAfterTypeAssertion); } else { @@ -71222,7 +71701,7 @@ var ts; return ts.rangeContainsRange(parent.members, node); case 225 /* ModuleDeclaration */: var body = parent.body; - return body && body.kind === 199 /* Block */ && ts.rangeContainsRange(body.statements, node); + return body && body.kind === 226 /* ModuleBlock */ && ts.rangeContainsRange(body.statements, node); case 256 /* SourceFile */: case 199 /* Block */: case 226 /* ModuleBlock */: @@ -71332,7 +71811,7 @@ var ts; break; } if (formatting.SmartIndenter.shouldIndentChildNode(n, child)) { - return options.IndentSize; + return options.indentSize; } previousLine = line; child = n; @@ -71404,7 +71883,7 @@ var ts; } function computeIndentation(node, startLine, inheritedIndentation, parent, parentDynamicIndentation, effectiveParentStartLine) { var indentation = inheritedIndentation; - var delta = formatting.SmartIndenter.shouldIndentChildNode(node) ? options.IndentSize : 0; + var delta = formatting.SmartIndenter.shouldIndentChildNode(node) ? options.indentSize : 0; if (effectiveParentStartLine === startLine) { // if node is located on the same line with the parent // - inherit indentation from the parent @@ -71412,7 +71891,7 @@ var ts; indentation = startLine === lastIndentedLine ? indentationOnLastIndentedLine : parentDynamicIndentation.getIndentation(); - delta = Math.min(options.IndentSize, parentDynamicIndentation.getDelta(node) + delta); + delta = Math.min(options.indentSize, parentDynamicIndentation.getDelta(node) + delta); } else if (indentation === -1 /* Unknown */) { if (formatting.SmartIndenter.childStartsOnTheSameLineWithElseInIfStatement(parent, node, startLine, sourceFile)) { @@ -71493,13 +71972,13 @@ var ts; recomputeIndentation: function (lineAdded) { if (node.parent && formatting.SmartIndenter.shouldIndentChildNode(node.parent, node)) { if (lineAdded) { - indentation += options.IndentSize; + indentation += options.indentSize; } else { - indentation -= options.IndentSize; + indentation -= options.indentSize; } if (formatting.SmartIndenter.shouldIndentChildNode(node)) { - delta = options.IndentSize; + delta = options.indentSize; } else { delta = 0; @@ -71919,7 +72398,7 @@ var ts; // edit should not be applied only if we have one line feed between elements var lineDelta = currentStartLine - previousStartLine; if (lineDelta !== 1) { - recordReplace(previousRange.end, currentRange.pos - previousRange.end, options.NewLineCharacter); + recordReplace(previousRange.end, currentRange.pos - previousRange.end, options.newLineCharacter); } break; case 2 /* Space */: @@ -71980,14 +72459,14 @@ var ts; var internedSpacesIndentation; function getIndentationString(indentation, options) { // reset interned strings if FormatCodeOptions were changed - var resetInternedStrings = !internedSizes || (internedSizes.tabSize !== options.TabSize || internedSizes.indentSize !== options.IndentSize); + var resetInternedStrings = !internedSizes || (internedSizes.tabSize !== options.tabSize || internedSizes.indentSize !== options.indentSize); if (resetInternedStrings) { - internedSizes = { tabSize: options.TabSize, indentSize: options.IndentSize }; + internedSizes = { tabSize: options.tabSize, indentSize: options.indentSize }; internedTabsIndentation = internedSpacesIndentation = undefined; } - if (!options.ConvertTabsToSpaces) { - var tabs = Math.floor(indentation / options.TabSize); - var spaces = indentation - tabs * options.TabSize; + if (!options.convertTabsToSpaces) { + var tabs = Math.floor(indentation / options.tabSize); + var spaces = indentation - tabs * options.tabSize; var tabString = void 0; if (!internedTabsIndentation) { internedTabsIndentation = []; @@ -72002,13 +72481,13 @@ var ts; } else { var spacesString = void 0; - var quotient = Math.floor(indentation / options.IndentSize); - var remainder = indentation % options.IndentSize; + var quotient = Math.floor(indentation / options.indentSize); + var remainder = indentation % options.indentSize; if (!internedSpacesIndentation) { internedSpacesIndentation = []; } if (internedSpacesIndentation[quotient] === undefined) { - spacesString = repeat(" ", options.IndentSize * quotient); + spacesString = repeat(" ", options.indentSize * quotient); internedSpacesIndentation[quotient] = spacesString; } else { @@ -72045,7 +72524,7 @@ var ts; } // no indentation when the indent style is set to none, // so we can return fast - if (options.IndentStyle === ts.IndentStyle.None) { + if (options.indentStyle === ts.IndentStyle.None) { return 0; } var precedingToken = ts.findPrecedingToken(position, sourceFile); @@ -72061,7 +72540,7 @@ var ts; // indentation is first non-whitespace character in a previous line // for block indentation, we should look for a line which contains something that's not // whitespace. - if (options.IndentStyle === ts.IndentStyle.Block) { + if (options.indentStyle === ts.IndentStyle.Block) { // move backwards until we find a line with a non-whitespace character, // then find the first non-whitespace character for that line. var current_1 = position; @@ -72095,7 +72574,7 @@ var ts; indentationDelta = 0; } else { - indentationDelta = lineAtPosition !== currentStart.line ? options.IndentSize : 0; + indentationDelta = lineAtPosition !== currentStart.line ? options.indentSize : 0; } break; } @@ -72106,7 +72585,7 @@ var ts; } actualIndentation = getLineIndentationWhenExpressionIsInMultiLine(current, sourceFile, options); if (actualIndentation !== -1 /* Unknown */) { - return actualIndentation + options.IndentSize; + return actualIndentation + options.indentSize; } previous = current; current = current.parent; @@ -72118,15 +72597,15 @@ var ts; return getIndentationForNodeWorker(current, currentStart, /*ignoreActualIndentationRange*/ undefined, indentationDelta, sourceFile, options); } SmartIndenter.getIndentation = getIndentation; - function getBaseIndentation(options) { - return options.BaseIndentSize || 0; - } - SmartIndenter.getBaseIndentation = getBaseIndentation; function getIndentationForNode(n, ignoreActualIndentationRange, sourceFile, options) { var start = sourceFile.getLineAndCharacterOfPosition(n.getStart(sourceFile)); return getIndentationForNodeWorker(n, start, ignoreActualIndentationRange, /*indentationDelta*/ 0, sourceFile, options); } SmartIndenter.getIndentationForNode = getIndentationForNode; + function getBaseIndentation(options) { + return options.baseIndentSize || 0; + } + SmartIndenter.getBaseIndentation = getBaseIndentation; function getIndentationForNodeWorker(current, currentStart, ignoreActualIndentationRange, indentationDelta, sourceFile, options) { var parent = current.parent; var parentStart; @@ -72161,7 +72640,7 @@ var ts; } // increase indentation if parent node wants its content to be indented and parent and child nodes don't start on the same line if (shouldIndentChildNode(parent, current) && !parentAndChildShareLine) { - indentationDelta += options.IndentSize; + indentationDelta += options.indentSize; } current = parent; currentStart = parentStart; @@ -72371,7 +72850,7 @@ var ts; break; } if (ch === 9 /* tab */) { - column += options.TabSize + (column % options.TabSize); + column += options.tabSize + (column % options.tabSize); } else { column++; @@ -72473,6 +72952,117 @@ var ts; })(SmartIndenter = formatting.SmartIndenter || (formatting.SmartIndenter = {})); })(formatting = ts.formatting || (ts.formatting = {})); })(ts || (ts = {})); +/* @internal */ +var ts; +(function (ts) { + var codefix; + (function (codefix) { + var codeFixes = ts.createMap(); + function registerCodeFix(action) { + ts.forEach(action.errorCodes, function (error) { + var fixes = codeFixes[error]; + if (!fixes) { + fixes = []; + codeFixes[error] = fixes; + } + fixes.push(action); + }); + } + codefix.registerCodeFix = registerCodeFix; + function getSupportedErrorCodes() { + return Object.keys(codeFixes); + } + codefix.getSupportedErrorCodes = getSupportedErrorCodes; + function getFixes(context) { + var fixes = codeFixes[context.errorCode]; + var allActions = []; + ts.forEach(fixes, function (f) { + var actions = f.getCodeActions(context); + if (actions && actions.length > 0) { + allActions = allActions.concat(actions); + } + }); + return allActions; + } + codefix.getFixes = getFixes; + })(codefix = ts.codefix || (ts.codefix = {})); +})(ts || (ts = {})); +/* @internal */ +var ts; +(function (ts) { + var codefix; + (function (codefix) { + function getOpenBraceEnd(constructor, sourceFile) { + // First token is the open curly, this is where we want to put the 'super' call. + return constructor.body.getFirstToken(sourceFile).getEnd(); + } + codefix.registerCodeFix({ + errorCodes: [ts.Diagnostics.Constructors_for_derived_classes_must_contain_a_super_call.code], + getCodeActions: function (context) { + var sourceFile = context.sourceFile; + var token = ts.getTokenAtPosition(sourceFile, context.span.start); + if (token.kind !== 121 /* ConstructorKeyword */) { + return undefined; + } + var newPosition = getOpenBraceEnd(token.parent, sourceFile); + return [{ + description: ts.getLocaleSpecificMessage(ts.Diagnostics.Add_missing_super_call), + changes: [{ fileName: sourceFile.fileName, textChanges: [{ newText: "super();", span: { start: newPosition, length: 0 } }] }] + }]; + } + }); + codefix.registerCodeFix({ + errorCodes: [ts.Diagnostics.super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class.code], + getCodeActions: function (context) { + var sourceFile = context.sourceFile; + var token = ts.getTokenAtPosition(sourceFile, context.span.start); + if (token.kind !== 97 /* ThisKeyword */) { + return undefined; + } + var constructor = ts.getContainingFunction(token); + var superCall = findSuperCall(constructor.body); + if (!superCall) { + return undefined; + } + // figure out if the this access is actuall inside the supercall + // i.e. super(this.a), since in that case we won't suggest a fix + if (superCall.expression && superCall.expression.kind == 174 /* CallExpression */) { + var arguments_1 = superCall.expression.arguments; + for (var i = 0; i < arguments_1.length; i++) { + if (arguments_1[i].expression === token) { + return undefined; + } + } + } + var newPosition = getOpenBraceEnd(constructor, sourceFile); + var changes = [{ + fileName: sourceFile.fileName, textChanges: [{ + newText: superCall.getText(sourceFile), + span: { start: newPosition, length: 0 } + }, + { + newText: "", + span: { start: superCall.getStart(sourceFile), length: superCall.getWidth(sourceFile) } + }] + }]; + return [{ + description: ts.getLocaleSpecificMessage(ts.Diagnostics.Make_super_call_the_first_statement_in_the_constructor), + changes: changes + }]; + function findSuperCall(n) { + if (n.kind === 202 /* ExpressionStatement */ && ts.isSuperCall(n.expression)) { + return n; + } + if (ts.isFunctionLike(n)) { + return undefined; + } + return ts.forEachChild(n, findSuperCall); + } + } + }); + })(codefix = ts.codefix || (ts.codefix = {})); +})(ts || (ts = {})); +/// /// /// /// @@ -72498,13 +73088,15 @@ var ts; /// /// /// +/// +/// var ts; (function (ts) { /** The version of the language service API */ ts.servicesVersion = "0.5"; function createNode(kind, pos, end, parent) { var node = kind >= 139 /* FirstNode */ ? new NodeObject(kind, pos, end) : - kind === 69 /* Identifier */ ? new IdentifierObject(kind, pos, end) : + kind === 69 /* Identifier */ ? new IdentifierObject(69 /* Identifier */, pos, end) : new TokenObject(kind, pos, end); node.parent = parent; return node; @@ -72732,15 +73324,16 @@ var ts; var TokenObject = (function (_super) { __extends(TokenObject, _super); function TokenObject(kind, pos, end) { - _super.call(this, pos, end); - this.kind = kind; + var _this = _super.call(this, pos, end) || this; + _this.kind = kind; + return _this; } return TokenObject; }(TokenOrIdentifierObject)); var IdentifierObject = (function (_super) { __extends(IdentifierObject, _super); function IdentifierObject(kind, pos, end) { - _super.call(this, pos, end); + return _super.call(this, pos, end) || this; } return IdentifierObject; }(TokenOrIdentifierObject)); @@ -72816,7 +73409,7 @@ var ts; var SourceFileObject = (function (_super) { __extends(SourceFileObject, _super); function SourceFileObject(kind, pos, end) { - _super.call(this, kind, pos, end); + return _super.call(this, kind, pos, end) || this; } SourceFileObject.prototype.update = function (newText, textChangeRange) { return ts.updateSourceFile(this, newText, textChangeRange); @@ -72985,6 +73578,30 @@ var ts; getSignatureConstructor: function () { return SignatureObject; } }; } + function toEditorSettings(optionsAsMap) { + var allPropertiesAreCamelCased = true; + for (var key in optionsAsMap) { + if (ts.hasProperty(optionsAsMap, key) && !isCamelCase(key)) { + allPropertiesAreCamelCased = false; + break; + } + } + if (allPropertiesAreCamelCased) { + return optionsAsMap; + } + var settings = {}; + for (var key in optionsAsMap) { + if (ts.hasProperty(optionsAsMap, key)) { + var newKey = isCamelCase(key) ? key : key.charAt(0).toLowerCase() + key.substr(1); + settings[newKey] = optionsAsMap[key]; + } + } + return settings; + } + ts.toEditorSettings = toEditorSettings; + function isCamelCase(s) { + return !s.length || s.charAt(0) === s.charAt(0).toLowerCase(); + } function displayPartsToString(displayParts) { if (displayParts) { return ts.map(displayParts, function (displayPart) { return displayPart.text; }).join(""); @@ -73000,9 +73617,13 @@ var ts; }; } ts.getDefaultCompilerOptions = getDefaultCompilerOptions; - // Cache host information about script should be refreshed + function getSupportedCodeFixes() { + return ts.codefix.getSupportedErrorCodes(); + } + ts.getSupportedCodeFixes = getSupportedCodeFixes; + // Cache host information about script Should be refreshed // at each language service public entry point, since we don't know when - // set of scripts handled by the host changes. + // the set of scripts handled by the host changes. var HostCache = (function () { function HostCache(host, getCanonicalFileName) { this.host = host; @@ -73185,7 +73806,8 @@ var ts; var ruleProvider; var program; var lastProjectVersion; - var useCaseSensitivefileNames = false; + var lastTypesRootVersion = 0; + var useCaseSensitivefileNames = host.useCaseSensitiveFileNames && host.useCaseSensitiveFileNames(); var cancellationToken = new CancellationTokenObject(host.getCancellationToken && host.getCancellationToken()); var currentDirectory = host.getCurrentDirectory(); // Check if the localized messages json is set, otherwise query the host for it @@ -73224,6 +73846,12 @@ var ts; lastProjectVersion = hostProjectVersion; } } + var typeRootsVersion = host.getTypeRootsVersion ? host.getTypeRootsVersion() : 0; + if (lastTypesRootVersion !== typeRootsVersion) { + log("TypeRoots version has changed; provide new program"); + program = undefined; + lastTypesRootVersion = typeRootsVersion; + } // Get a fresh cache of the host information var hostCache = new HostCache(host, getCanonicalFileName); // If the program is already up-to-date, we can reuse it @@ -73553,12 +74181,12 @@ var ts; return ts.FindAllReferences.findReferencedSymbols(program.getTypeChecker(), cancellationToken, program.getSourceFiles(), getValidSourceFile(fileName), position, findInStrings, findInComments); } /// NavigateTo - function getNavigateToItems(searchValue, maxResultCount, fileName) { + function getNavigateToItems(searchValue, maxResultCount, fileName, excludeDtsFiles) { synchronizeHostData(); var sourceFiles = fileName ? [getValidSourceFile(fileName)] : program.getSourceFiles(); - return ts.NavigateTo.getNavigateToItems(sourceFiles, program.getTypeChecker(), cancellationToken, searchValue, maxResultCount); + return ts.NavigateTo.getNavigateToItems(sourceFiles, program.getTypeChecker(), cancellationToken, searchValue, maxResultCount, excludeDtsFiles); } - function getEmitOutput(fileName) { + function getEmitOutput(fileName, emitOnlyDtsFiles) { synchronizeHostData(); var sourceFile = getValidSourceFile(fileName); var outputFiles = []; @@ -73569,7 +74197,7 @@ var ts; text: data }); } - var emitOutput = program.emit(sourceFile, writeFile, cancellationToken); + var emitOutput = program.emit(sourceFile, writeFile, cancellationToken, emitOnlyDtsFiles); return { outputFiles: outputFiles, emitSkipped: emitOutput.emitSkipped @@ -73647,14 +74275,28 @@ var ts; return ts.BreakpointResolver.spanInSourceFileAtLocation(sourceFile, position); } function getNavigationBarItems(fileName) { - var sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName); - return ts.NavigationBar.getNavigationBarItems(sourceFile); + return ts.NavigationBar.getNavigationBarItems(syntaxTreeCache.getCurrentSourceFile(fileName)); + } + function getNavigationTree(fileName) { + return ts.NavigationBar.getNavigationTree(syntaxTreeCache.getCurrentSourceFile(fileName)); + } + function isTsOrTsxFile(fileName) { + var kind = ts.getScriptKind(fileName, host); + return kind === 3 /* TS */ || kind === 4 /* TSX */; } function getSemanticClassifications(fileName, span) { + if (!isTsOrTsxFile(fileName)) { + // do not run semantic classification on non-ts-or-tsx files + return []; + } synchronizeHostData(); return ts.getSemanticClassifications(program.getTypeChecker(), cancellationToken, getValidSourceFile(fileName), program.getClassifiableNames(), span); } function getEncodedSemanticClassifications(fileName, span) { + if (!isTsOrTsxFile(fileName)) { + // do not run semantic classification on non-ts-or-tsx files + return { spans: [], endOfLineState: 0 /* None */ }; + } synchronizeHostData(); return ts.getEncodedSemanticClassifications(program.getTypeChecker(), cancellationToken, getValidSourceFile(fileName), program.getClassifiableNames(), span); } @@ -73715,34 +74357,60 @@ var ts; } function getIndentationAtPosition(fileName, position, editorOptions) { var start = ts.timestamp(); + var settings = toEditorSettings(editorOptions); var sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName); log("getIndentationAtPosition: getCurrentSourceFile: " + (ts.timestamp() - start)); start = ts.timestamp(); - var result = ts.formatting.SmartIndenter.getIndentation(position, sourceFile, editorOptions); + var result = ts.formatting.SmartIndenter.getIndentation(position, sourceFile, settings); log("getIndentationAtPosition: computeIndentation : " + (ts.timestamp() - start)); return result; } function getFormattingEditsForRange(fileName, start, end, options) { var sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName); - return ts.formatting.formatSelection(start, end, sourceFile, getRuleProvider(options), options); + var settings = toEditorSettings(options); + return ts.formatting.formatSelection(start, end, sourceFile, getRuleProvider(settings), settings); } function getFormattingEditsForDocument(fileName, options) { var sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName); - return ts.formatting.formatDocument(sourceFile, getRuleProvider(options), options); + var settings = toEditorSettings(options); + return ts.formatting.formatDocument(sourceFile, getRuleProvider(settings), settings); } function getFormattingEditsAfterKeystroke(fileName, position, key, options) { var sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName); + var settings = toEditorSettings(options); if (key === "}") { - return ts.formatting.formatOnClosingCurly(position, sourceFile, getRuleProvider(options), options); + return ts.formatting.formatOnClosingCurly(position, sourceFile, getRuleProvider(settings), settings); } else if (key === ";") { - return ts.formatting.formatOnSemicolon(position, sourceFile, getRuleProvider(options), options); + return ts.formatting.formatOnSemicolon(position, sourceFile, getRuleProvider(settings), settings); } else if (key === "\n") { - return ts.formatting.formatOnEnter(position, sourceFile, getRuleProvider(options), options); + return ts.formatting.formatOnEnter(position, sourceFile, getRuleProvider(settings), settings); } return []; } + function getCodeFixesAtPosition(fileName, start, end, errorCodes) { + synchronizeHostData(); + var sourceFile = getValidSourceFile(fileName); + var span = { start: start, length: end - start }; + var newLineChar = ts.getNewLineOrDefaultFromHost(host); + var allFixes = []; + ts.forEach(errorCodes, function (error) { + cancellationToken.throwIfCancellationRequested(); + var context = { + errorCode: error, + sourceFile: sourceFile, + span: span, + program: program, + newLineCharacter: newLineChar + }; + var fixes = ts.codefix.getFixes(context); + if (fixes) { + allFixes = allFixes.concat(fixes); + } + }); + return allFixes; + } function getDocCommentTemplateAtPosition(fileName, position) { return ts.JsDoc.getDocCommentTemplateAtPosition(ts.getNewLineOrDefaultFromHost(host), syntaxTreeCache.getCurrentSourceFile(fileName), position); } @@ -73926,6 +74594,7 @@ var ts; getRenameInfo: getRenameInfo, findRenameLocations: findRenameLocations, getNavigationBarItems: getNavigationBarItems, + getNavigationTree: getNavigationTree, getOutliningSpans: getOutliningSpans, getTodoComments: getTodoComments, getBraceMatchingAtPosition: getBraceMatchingAtPosition, @@ -73935,6 +74604,7 @@ var ts; getFormattingEditsAfterKeystroke: getFormattingEditsAfterKeystroke, getDocCommentTemplateAtPosition: getDocCommentTemplateAtPosition, isValidBraceCompletionAtPosition: isValidBraceCompletionAtPosition, + getCodeFixesAtPosition: getCodeFixesAtPosition, getEmitOutput: getEmitOutput, getNonBoundSourceFile: getNonBoundSourceFile, getSourceFile: getSourceFile, @@ -74624,7 +75294,7 @@ var ts; // /// /* @internal */ -var debugObjectHost = new Function("return this")(); +var debugObjectHost = (function () { return this; })(); // We need to use 'null' to interface with the managed side. /* tslint:disable:no-null-keyword */ /* tslint:disable:no-in-operator */ @@ -74712,6 +75382,12 @@ var ts; } return this.shimHost.getProjectVersion(); }; + LanguageServiceShimHostAdapter.prototype.getTypeRootsVersion = function () { + if (!this.shimHost.getTypeRootsVersion) { + return 0; + } + return this.shimHost.getTypeRootsVersion(); + }; LanguageServiceShimHostAdapter.prototype.useCaseSensitiveFileNames = function () { return this.shimHost.useCaseSensitiveFileNames ? this.shimHost.useCaseSensitiveFileNames() : false; }; @@ -74914,11 +75590,12 @@ var ts; var LanguageServiceShimObject = (function (_super) { __extends(LanguageServiceShimObject, _super); function LanguageServiceShimObject(factory, host, languageService) { - _super.call(this, factory); - this.host = host; - this.languageService = languageService; - this.logPerformance = false; - this.logger = this.host; + var _this = _super.call(this, factory) || this; + _this.host = host; + _this.languageService = languageService; + _this.logPerformance = false; + _this.logger = _this.host; + return _this; } LanguageServiceShimObject.prototype.forwardJSONCall = function (actionDescription, action) { return forwardJSONCall(this.logger, actionDescription, action, this.logPerformance); @@ -75156,6 +75833,10 @@ var ts; var _this = this; return this.forwardJSONCall("getNavigationBarItems('" + fileName + "')", function () { return _this.languageService.getNavigationBarItems(fileName); }); }; + LanguageServiceShimObject.prototype.getNavigationTree = function (fileName) { + var _this = this; + return this.forwardJSONCall("getNavigationTree('" + fileName + "')", function () { return _this.languageService.getNavigationTree(fileName); }); + }; LanguageServiceShimObject.prototype.getOutliningSpans = function (fileName) { var _this = this; return this.forwardJSONCall("getOutliningSpans('" + fileName + "')", function () { return _this.languageService.getOutliningSpans(fileName); }); @@ -75182,10 +75863,11 @@ var ts; var ClassifierShimObject = (function (_super) { __extends(ClassifierShimObject, _super); function ClassifierShimObject(factory, logger) { - _super.call(this, factory); - this.logger = logger; - this.logPerformance = false; - this.classifier = ts.createClassifier(); + var _this = _super.call(this, factory) || this; + _this.logger = logger; + _this.logPerformance = false; + _this.classifier = ts.createClassifier(); + return _this; } ClassifierShimObject.prototype.getEncodedLexicalClassifications = function (text, lexState, syntacticClassifierAbsent) { var _this = this; @@ -75208,10 +75890,11 @@ var ts; var CoreServicesShimObject = (function (_super) { __extends(CoreServicesShimObject, _super); function CoreServicesShimObject(factory, logger, host) { - _super.call(this, factory); - this.logger = logger; - this.host = host; - this.logPerformance = false; + var _this = _super.call(this, factory) || this; + _this.logger = logger; + _this.host = host; + _this.logPerformance = false; + return _this; } CoreServicesShimObject.prototype.forwardJSONCall = function (actionDescription, action) { return forwardJSONCall(this.logger, actionDescription, action, this.logPerformance); diff --git a/lib/typingsInstaller.js b/lib/typingsInstaller.js index b1248c700fa22..577a8ee4fc94f 100644 --- a/lib/typingsInstaller.js +++ b/lib/typingsInstaller.js @@ -72,7 +72,6 @@ var ts; (function (ts) { ts.timestamp = typeof performance !== "undefined" && performance.now ? function () { return performance.now(); } : Date.now ? Date.now : function () { return +(new Date()); }; })(ts || (ts = {})); -var ts; (function (ts) { var performance; (function (performance) { @@ -790,6 +789,56 @@ var ts; }; } ts.memoize = memoize; + function chain(a, b, c, d, e) { + if (e) { + var args_2 = []; + for (var i = 0; i < arguments.length; i++) { + args_2[i] = arguments[i]; + } + return function (t) { return compose.apply(void 0, map(args_2, function (f) { return f(t); })); }; + } + else if (d) { + return function (t) { return compose(a(t), b(t), c(t), d(t)); }; + } + else if (c) { + return function (t) { return compose(a(t), b(t), c(t)); }; + } + else if (b) { + return function (t) { return compose(a(t), b(t)); }; + } + else if (a) { + return function (t) { return compose(a(t)); }; + } + else { + return function (t) { return function (u) { return u; }; }; + } + } + ts.chain = chain; + function compose(a, b, c, d, e) { + if (e) { + var args_3 = []; + for (var i = 0; i < arguments.length; i++) { + args_3[i] = arguments[i]; + } + return function (t) { return reduceLeft(args_3, function (u, f) { return f(u); }, t); }; + } + else if (d) { + return function (t) { return d(c(b(a(t)))); }; + } + else if (c) { + return function (t) { return c(b(a(t))); }; + } + else if (b) { + return function (t) { return b(a(t)); }; + } + else if (a) { + return function (t) { return a(t); }; + } + else { + return function (t) { return t; }; + } + } + ts.compose = compose; function formatStringFromArgs(text, args, baseIndex) { baseIndex = baseIndex || 0; return text.replace(/{(\d+)}/g, function (match, index) { return args[+index + baseIndex]; }); @@ -1537,7 +1586,6 @@ var ts; this.transformFlags = 0; this.parent = undefined; this.original = undefined; - this.transformId = 0; } ts.objectAllocator = { getNodeConstructor: function () { return Node; }, @@ -1550,9 +1598,9 @@ var ts; }; var Debug; (function (Debug) { - var currentAssertionLevel; + Debug.currentAssertionLevel = 0; function shouldAssert(level) { - return getCurrentAssertionLevel() >= level; + return Debug.currentAssertionLevel >= level; } Debug.shouldAssert = shouldAssert; function assert(expression, message, verboseDebugInfo) { @@ -1570,30 +1618,7 @@ var ts; Debug.assert(false, message); } Debug.fail = fail; - function getCurrentAssertionLevel() { - if (currentAssertionLevel !== undefined) { - return currentAssertionLevel; - } - if (ts.sys === undefined) { - return 0; - } - var developmentMode = /^development$/i.test(getEnvironmentVariable("NODE_ENV")); - currentAssertionLevel = developmentMode - ? 1 - : 0; - return currentAssertionLevel; - } })(Debug = ts.Debug || (ts.Debug = {})); - function getEnvironmentVariable(name, host) { - if (host && host.getEnvironmentVariable) { - return host.getEnvironmentVariable(name); - } - if (ts.sys && ts.sys.getEnvironmentVariable) { - return ts.sys.getEnvironmentVariable(name); - } - return ""; - } - ts.getEnvironmentVariable = getEnvironmentVariable; function orderedRemoveItemAt(array, index) { for (var i = index; i < array.length - 1; i++) { array[i] = array[i + 1]; @@ -1624,6 +1649,60 @@ var ts; : (function (fileName) { return fileName.toLowerCase(); }); } ts.createGetCanonicalFileName = createGetCanonicalFileName; + function matchPatternOrExact(patternStrings, candidate) { + var patterns = []; + for (var _i = 0, patternStrings_1 = patternStrings; _i < patternStrings_1.length; _i++) { + var patternString = patternStrings_1[_i]; + var pattern = tryParsePattern(patternString); + if (pattern) { + patterns.push(pattern); + } + else if (patternString === candidate) { + return patternString; + } + } + return findBestPatternMatch(patterns, function (_) { return _; }, candidate); + } + ts.matchPatternOrExact = matchPatternOrExact; + function patternText(_a) { + var prefix = _a.prefix, suffix = _a.suffix; + return prefix + "*" + suffix; + } + ts.patternText = patternText; + function matchedText(pattern, candidate) { + Debug.assert(isPatternMatch(pattern, candidate)); + return candidate.substr(pattern.prefix.length, candidate.length - pattern.suffix.length); + } + ts.matchedText = matchedText; + function findBestPatternMatch(values, getPattern, candidate) { + var matchedValue = undefined; + var longestMatchPrefixLength = -1; + for (var _i = 0, values_1 = values; _i < values_1.length; _i++) { + var v = values_1[_i]; + var pattern = getPattern(v); + if (isPatternMatch(pattern, candidate) && pattern.prefix.length > longestMatchPrefixLength) { + longestMatchPrefixLength = pattern.prefix.length; + matchedValue = v; + } + } + return matchedValue; + } + ts.findBestPatternMatch = findBestPatternMatch; + function isPatternMatch(_a, candidate) { + var prefix = _a.prefix, suffix = _a.suffix; + return candidate.length >= prefix.length + suffix.length && + startsWith(candidate, prefix) && + endsWith(candidate, suffix); + } + function tryParsePattern(pattern) { + Debug.assert(hasZeroOrOneAsteriskCharacter(pattern)); + var indexOfStar = pattern.indexOf("*"); + return indexOfStar === -1 ? undefined : { + prefix: pattern.substr(0, indexOfStar), + suffix: pattern.substr(indexOfStar + 1) + }; + } + ts.tryParsePattern = tryParsePattern; function positionIsSynthesized(pos) { return !(pos >= 0); } @@ -1821,8 +1900,14 @@ var ts; function isNode4OrLater() { return parseInt(process.version.charAt(1)) >= 4; } + function isFileSystemCaseSensitive() { + if (platform === "win32" || platform === "win64") { + return false; + } + return !fileExists(__filename.toUpperCase()) || !fileExists(__filename.toLowerCase()); + } var platform = _os.platform(); - var useCaseSensitiveFileNames = platform !== "win32" && platform !== "win64" && platform !== "darwin"; + var useCaseSensitiveFileNames = isFileSystemCaseSensitive(); function readFile(fileName, encoding) { if (!fileExists(fileName)) { return undefined; @@ -1947,6 +2032,9 @@ var ts; }, watchDirectory: function (directoryName, callback, recursive) { var options; + if (!directoryExists(directoryName)) { + return; + } if (isNode4OrLater() && (process.platform === "win32" || process.platform === "darwin")) { options = { persistent: true, recursive: !!recursive }; } @@ -2090,6 +2178,11 @@ var ts; } return sys; })(); + if (ts.sys && ts.sys.getEnvironmentVariable) { + ts.Debug.currentAssertionLevel = /^development$/i.test(ts.sys.getEnvironmentVariable("NODE_ENV")) + ? 1 + : 0; + } })(ts || (ts = {})); var ts; (function (ts) { @@ -2414,7 +2507,7 @@ var ts; The_right_hand_side_of_a_for_in_statement_must_be_of_type_any_an_object_type_or_a_type_parameter: { code: 2407, category: ts.DiagnosticCategory.Error, key: "The_right_hand_side_of_a_for_in_statement_must_be_of_type_any_an_object_type_or_a_type_parameter_2407", message: "The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter." }, Setters_cannot_return_a_value: { code: 2408, category: ts.DiagnosticCategory.Error, key: "Setters_cannot_return_a_value_2408", message: "Setters cannot return a value." }, Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class: { code: 2409, category: ts.DiagnosticCategory.Error, key: "Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class_2409", message: "Return type of constructor signature must be assignable to the instance type of the class" }, - All_symbols_within_a_with_block_will_be_resolved_to_any: { code: 2410, category: ts.DiagnosticCategory.Error, key: "All_symbols_within_a_with_block_will_be_resolved_to_any_2410", message: "All symbols within a 'with' block will be resolved to 'any'." }, + The_with_statement_is_not_supported_All_symbols_in_a_with_block_will_have_type_any: { code: 2410, category: ts.DiagnosticCategory.Error, key: "The_with_statement_is_not_supported_All_symbols_in_a_with_block_will_have_type_any_2410", message: "The 'with' statement is not supported. All symbols in a 'with' block will have type 'any'." }, Property_0_of_type_1_is_not_assignable_to_string_index_type_2: { code: 2411, category: ts.DiagnosticCategory.Error, key: "Property_0_of_type_1_is_not_assignable_to_string_index_type_2_2411", message: "Property '{0}' of type '{1}' is not assignable to string index type '{2}'." }, Property_0_of_type_1_is_not_assignable_to_numeric_index_type_2: { code: 2412, category: ts.DiagnosticCategory.Error, key: "Property_0_of_type_1_is_not_assignable_to_numeric_index_type_2_2412", message: "Property '{0}' of type '{1}' is not assignable to numeric index type '{2}'." }, Numeric_index_type_0_is_not_assignable_to_string_index_type_1: { code: 2413, category: ts.DiagnosticCategory.Error, key: "Numeric_index_type_0_is_not_assignable_to_string_index_type_1_2413", message: "Numeric index type '{0}' is not assignable to string index type '{1}'." }, @@ -2575,7 +2668,7 @@ var ts; this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation: { code: 2683, category: ts.DiagnosticCategory.Error, key: "this_implicitly_has_type_any_because_it_does_not_have_a_type_annotation_2683", message: "'this' implicitly has type 'any' because it does not have a type annotation." }, The_this_context_of_type_0_is_not_assignable_to_method_s_this_of_type_1: { code: 2684, category: ts.DiagnosticCategory.Error, key: "The_this_context_of_type_0_is_not_assignable_to_method_s_this_of_type_1_2684", message: "The 'this' context of type '{0}' is not assignable to method's 'this' of type '{1}'." }, The_this_types_of_each_signature_are_incompatible: { code: 2685, category: ts.DiagnosticCategory.Error, key: "The_this_types_of_each_signature_are_incompatible_2685", message: "The 'this' types of each signature are incompatible." }, - Identifier_0_must_be_imported_from_a_module: { code: 2686, category: ts.DiagnosticCategory.Error, key: "Identifier_0_must_be_imported_from_a_module_2686", message: "Identifier '{0}' must be imported from a module" }, + _0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead: { code: 2686, category: ts.DiagnosticCategory.Error, key: "_0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead_2686", message: "'{0}' refers to a UMD global, but the current file is a module. Consider adding an import instead." }, All_declarations_of_0_must_have_identical_modifiers: { code: 2687, category: ts.DiagnosticCategory.Error, key: "All_declarations_of_0_must_have_identical_modifiers_2687", message: "All declarations of '{0}' must have identical modifiers." }, Cannot_find_type_definition_file_for_0: { code: 2688, category: ts.DiagnosticCategory.Error, key: "Cannot_find_type_definition_file_for_0_2688", message: "Cannot find type definition file for '{0}'." }, Cannot_extend_an_interface_0_Did_you_mean_implements: { code: 2689, category: ts.DiagnosticCategory.Error, key: "Cannot_extend_an_interface_0_Did_you_mean_implements_2689", message: "Cannot extend an interface '{0}'. Did you mean 'implements'?" }, @@ -2809,6 +2902,7 @@ var ts; Property_0_is_declared_but_never_used: { code: 6138, category: ts.DiagnosticCategory.Error, key: "Property_0_is_declared_but_never_used_6138", message: "Property '{0}' is declared but never used." }, Import_emit_helpers_from_tslib: { code: 6139, category: ts.DiagnosticCategory.Message, key: "Import_emit_helpers_from_tslib_6139", message: "Import emit helpers from 'tslib'." }, Auto_discovery_for_typings_is_enabled_in_project_0_Running_extra_resolution_pass_for_module_1_using_cache_location_2: { code: 6140, category: ts.DiagnosticCategory.Error, key: "Auto_discovery_for_typings_is_enabled_in_project_0_Running_extra_resolution_pass_for_module_1_using__6140", message: "Auto discovery for typings is enabled in project '{0}'. Running extra resolution pass for module '{1}' using cache location '{2}'." }, + Parse_in_strict_mode_and_emit_use_strict_for_each_source_file: { code: 6141, category: ts.DiagnosticCategory.Message, key: "Parse_in_strict_mode_and_emit_use_strict_for_each_source_file_6141", message: "Parse in strict mode and emit \"use strict\" for each source file" }, Variable_0_implicitly_has_an_1_type: { code: 7005, category: ts.DiagnosticCategory.Error, key: "Variable_0_implicitly_has_an_1_type_7005", message: "Variable '{0}' implicitly has an '{1}' type." }, Parameter_0_implicitly_has_an_1_type: { code: 7006, category: ts.DiagnosticCategory.Error, key: "Parameter_0_implicitly_has_an_1_type_7006", message: "Parameter '{0}' implicitly has an '{1}' type." }, Member_0_implicitly_has_an_1_type: { code: 7008, category: ts.DiagnosticCategory.Error, key: "Member_0_implicitly_has_an_1_type_7008", message: "Member '{0}' implicitly has an '{1}' type." }, @@ -2833,6 +2927,7 @@ var ts; Binding_element_0_implicitly_has_an_1_type: { code: 7031, category: ts.DiagnosticCategory.Error, key: "Binding_element_0_implicitly_has_an_1_type_7031", message: "Binding element '{0}' implicitly has an '{1}' type." }, Property_0_implicitly_has_type_any_because_its_set_accessor_lacks_a_parameter_type_annotation: { code: 7032, category: ts.DiagnosticCategory.Error, key: "Property_0_implicitly_has_type_any_because_its_set_accessor_lacks_a_parameter_type_annotation_7032", message: "Property '{0}' implicitly has type 'any', because its set accessor lacks a parameter type annotation." }, Property_0_implicitly_has_type_any_because_its_get_accessor_lacks_a_return_type_annotation: { code: 7033, category: ts.DiagnosticCategory.Error, key: "Property_0_implicitly_has_type_any_because_its_get_accessor_lacks_a_return_type_annotation_7033", message: "Property '{0}' implicitly has type 'any', because its get accessor lacks a return type annotation." }, + Variable_0_implicitly_has_type_any_in_some_locations_where_its_type_cannot_be_determined: { code: 7034, category: ts.DiagnosticCategory.Error, key: "Variable_0_implicitly_has_type_any_in_some_locations_where_its_type_cannot_be_determined_7034", message: "Variable '{0}' implicitly has type 'any' in some locations where its type cannot be determined." }, You_cannot_rename_this_element: { code: 8000, category: ts.DiagnosticCategory.Error, key: "You_cannot_rename_this_element_8000", message: "You cannot rename this element." }, You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library: { code: 8001, category: ts.DiagnosticCategory.Error, key: "You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library_8001", message: "You cannot rename elements that are defined in the standard TypeScript library." }, import_can_only_be_used_in_a_ts_file: { code: 8002, category: ts.DiagnosticCategory.Error, key: "import_can_only_be_used_in_a_ts_file_8002", message: "'import ... =' can only be used in a .ts file." }, @@ -2862,7 +2957,14 @@ var ts; super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class: { code: 17009, category: ts.DiagnosticCategory.Error, key: "super_must_be_called_before_accessing_this_in_the_constructor_of_a_derived_class_17009", message: "'super' must be called before accessing 'this' in the constructor of a derived class." }, Unknown_typing_option_0: { code: 17010, category: ts.DiagnosticCategory.Error, key: "Unknown_typing_option_0_17010", message: "Unknown typing option '{0}'." }, Circularity_detected_while_resolving_configuration_Colon_0: { code: 18000, category: ts.DiagnosticCategory.Error, key: "Circularity_detected_while_resolving_configuration_Colon_0_18000", message: "Circularity detected while resolving configuration: {0}" }, - The_path_in_an_extends_options_must_be_relative_or_rooted: { code: 18001, category: ts.DiagnosticCategory.Error, key: "The_path_in_an_extends_options_must_be_relative_or_rooted_18001", message: "The path in an 'extends' options must be relative or rooted." } + The_path_in_an_extends_options_must_be_relative_or_rooted: { code: 18001, category: ts.DiagnosticCategory.Error, key: "The_path_in_an_extends_options_must_be_relative_or_rooted_18001", message: "The path in an 'extends' options must be relative or rooted." }, + Add_missing_super_call: { code: 90001, category: ts.DiagnosticCategory.Message, key: "Add_missing_super_call_90001", message: "Add missing 'super()' call." }, + Make_super_call_the_first_statement_in_the_constructor: { code: 90002, category: ts.DiagnosticCategory.Message, key: "Make_super_call_the_first_statement_in_the_constructor_90002", message: "Make 'super()' call the first statement in the constructor." }, + Change_extends_to_implements: { code: 90003, category: ts.DiagnosticCategory.Message, key: "Change_extends_to_implements_90003", message: "Change 'extends' to 'implements'" }, + Remove_unused_identifiers: { code: 90004, category: ts.DiagnosticCategory.Message, key: "Remove_unused_identifiers_90004", message: "Remove unused identifiers" }, + Implement_interface_on_reference: { code: 90005, category: ts.DiagnosticCategory.Message, key: "Implement_interface_on_reference_90005", message: "Implement interface on reference" }, + Implement_interface_on_class: { code: 90006, category: ts.DiagnosticCategory.Message, key: "Implement_interface_on_class_90006", message: "Implement interface on class" }, + Implement_inherited_abstract_class: { code: 90007, category: ts.DiagnosticCategory.Message, key: "Implement_inherited_abstract_class_90007", message: "Implement inherited abstract class" } }; })(ts || (ts = {})); var ts; @@ -3772,7 +3874,7 @@ var ts; return token = 69; } function scanBinaryOrOctalDigits(base) { - ts.Debug.assert(base !== 2 || base !== 8, "Expected either base 2 or base 8"); + ts.Debug.assert(base === 2 || base === 8, "Expected either base 2 or base 8"); var value = 0; var numberOfDigits = 0; while (true) { @@ -4844,6 +4946,11 @@ var ts; name: "importHelpers", type: "boolean", description: ts.Diagnostics.Import_emit_helpers_from_tslib + }, + { + name: "alwaysStrict", + type: "boolean", + description: ts.Diagnostics.Parse_in_strict_mode_and_emit_use_strict_for_each_source_file } ]; ts.typingOptionDeclarations = [ @@ -5294,7 +5401,7 @@ var ts; ts.convertTypingOptionsFromJson = convertTypingOptionsFromJson; function convertCompilerOptionsFromJsonWorker(jsonOptions, basePath, errors, configFileName) { var options = ts.getBaseFileName(configFileName) === "jsconfig.json" - ? { allowJs: true, maxNodeModuleJsDepth: 2, allowSyntheticDefaultImports: true } + ? { allowJs: true, maxNodeModuleJsDepth: 2, allowSyntheticDefaultImports: true, skipLibCheck: true } : {}; convertOptionsFromJson(ts.optionDeclarations, jsonOptions, basePath, options, ts.Diagnostics.Unknown_compiler_option_0, errors); return options; @@ -5704,7 +5811,7 @@ var ts; else if (host.getCurrentDirectory) { currentDirectory = host.getCurrentDirectory(); } - return currentDirectory && getDefaultTypeRoots(currentDirectory, host); + return currentDirectory !== undefined && getDefaultTypeRoots(currentDirectory, host); } ts.getEffectiveTypeRoots = getEffectiveTypeRoots; function getDefaultTypeRoots(currentDirectory, host) { @@ -5958,11 +6065,11 @@ var ts; if (state.traceEnabled) { trace(state.host, ts.Diagnostics.paths_option_is_specified_looking_for_a_pattern_to_match_module_name_0, moduleName); } - matchedPattern = matchPatternOrExact(ts.getOwnKeys(state.compilerOptions.paths), moduleName); + matchedPattern = ts.matchPatternOrExact(ts.getOwnKeys(state.compilerOptions.paths), moduleName); } if (matchedPattern) { - var matchedStar = typeof matchedPattern === "string" ? undefined : matchedText(matchedPattern, moduleName); - var matchedPatternText = typeof matchedPattern === "string" ? matchedPattern : patternText(matchedPattern); + var matchedStar = typeof matchedPattern === "string" ? undefined : ts.matchedText(matchedPattern, moduleName); + var matchedPatternText = typeof matchedPattern === "string" ? matchedPattern : ts.patternText(matchedPattern); if (state.traceEnabled) { trace(state.host, ts.Diagnostics.Module_name_0_matched_pattern_1, moduleName, matchedPatternText); } @@ -5988,57 +6095,6 @@ var ts; return loader(candidate, supportedExtensions, failedLookupLocations, !directoryProbablyExists(ts.getDirectoryPath(candidate), state.host), state); } } - function matchPatternOrExact(patternStrings, candidate) { - var patterns = []; - for (var _i = 0, patternStrings_1 = patternStrings; _i < patternStrings_1.length; _i++) { - var patternString = patternStrings_1[_i]; - var pattern = tryParsePattern(patternString); - if (pattern) { - patterns.push(pattern); - } - else if (patternString === candidate) { - return patternString; - } - } - return findBestPatternMatch(patterns, function (_) { return _; }, candidate); - } - function patternText(_a) { - var prefix = _a.prefix, suffix = _a.suffix; - return prefix + "*" + suffix; - } - function matchedText(pattern, candidate) { - ts.Debug.assert(isPatternMatch(pattern, candidate)); - return candidate.substr(pattern.prefix.length, candidate.length - pattern.suffix.length); - } - function findBestPatternMatch(values, getPattern, candidate) { - var matchedValue = undefined; - var longestMatchPrefixLength = -1; - for (var _i = 0, values_1 = values; _i < values_1.length; _i++) { - var v = values_1[_i]; - var pattern = getPattern(v); - if (isPatternMatch(pattern, candidate) && pattern.prefix.length > longestMatchPrefixLength) { - longestMatchPrefixLength = pattern.prefix.length; - matchedValue = v; - } - } - return matchedValue; - } - ts.findBestPatternMatch = findBestPatternMatch; - function isPatternMatch(_a, candidate) { - var prefix = _a.prefix, suffix = _a.suffix; - return candidate.length >= prefix.length + suffix.length && - ts.startsWith(candidate, prefix) && - ts.endsWith(candidate, suffix); - } - function tryParsePattern(pattern) { - ts.Debug.assert(ts.hasZeroOrOneAsteriskCharacter(pattern)); - var indexOfStar = pattern.indexOf("*"); - return indexOfStar === -1 ? undefined : { - prefix: pattern.substr(0, indexOfStar), - suffix: pattern.substr(indexOfStar + 1) - }; - } - ts.tryParsePattern = tryParsePattern; function nodeModuleNameResolver(moduleName, containingFile, compilerOptions, host) { var containingDirectory = ts.getDirectoryPath(containingFile); var supportedExtensions = ts.getSupportedExtensions(compilerOptions); @@ -6169,20 +6225,28 @@ var ts; } } function loadModuleFromNodeModules(moduleName, directory, failedLookupLocations, state, checkOneLevel) { + return loadModuleFromNodeModulesWorker(moduleName, directory, failedLookupLocations, state, checkOneLevel, false); + } + ts.loadModuleFromNodeModules = loadModuleFromNodeModules; + function loadModuleFromNodeModulesAtTypes(moduleName, directory, failedLookupLocations, state) { + return loadModuleFromNodeModulesWorker(moduleName, directory, failedLookupLocations, state, false, true); + } + function loadModuleFromNodeModulesWorker(moduleName, directory, failedLookupLocations, state, checkOneLevel, typesOnly) { directory = ts.normalizeSlashes(directory); while (true) { var baseName = ts.getBaseFileName(directory); if (baseName !== "node_modules") { - var packageResult = loadModuleFromNodeModulesFolder(moduleName, directory, failedLookupLocations, state); - if (packageResult && ts.hasTypeScriptFileExtension(packageResult)) { - return packageResult; - } - else { - var typesResult = loadModuleFromNodeModulesFolder(ts.combinePaths("@types", moduleName), directory, failedLookupLocations, state); - if (typesResult || packageResult) { - return typesResult || packageResult; + var packageResult = void 0; + if (!typesOnly) { + packageResult = loadModuleFromNodeModulesFolder(moduleName, directory, failedLookupLocations, state); + if (packageResult && ts.hasTypeScriptFileExtension(packageResult)) { + return packageResult; } } + var typesResult = loadModuleFromNodeModulesFolder(ts.combinePaths("@types", moduleName), directory, failedLookupLocations, state); + if (typesResult || packageResult) { + return typesResult || packageResult; + } } var parentPath = ts.getDirectoryPath(directory); if (parentPath === directory || checkOneLevel) { @@ -6192,7 +6256,6 @@ var ts; } return undefined; } - ts.loadModuleFromNodeModules = loadModuleFromNodeModules; function classicNameResolver(moduleName, containingFile, compilerOptions, host) { var traceEnabled = isTraceEnabled(compilerOptions, host); var state = { compilerOptions: compilerOptions, host: host, traceEnabled: traceEnabled, skipTsx: !compilerOptions.jsx }; @@ -6205,18 +6268,8 @@ var ts; } var referencedSourceFile; if (moduleHasNonRelativeName(moduleName)) { - while (true) { - var searchName = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); - referencedSourceFile = loadModuleFromFile(searchName, supportedExtensions, failedLookupLocations, false, state); - if (referencedSourceFile) { - break; - } - var parentPath = ts.getDirectoryPath(containingDirectory); - if (parentPath === containingDirectory) { - break; - } - containingDirectory = parentPath; - } + referencedSourceFile = referencedSourceFile = loadModuleFromAncestorDirectories(moduleName, containingDirectory, supportedExtensions, failedLookupLocations, state) || + loadModuleFromNodeModulesAtTypes(moduleName, containingDirectory, failedLookupLocations, state); } else { var candidate = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); @@ -6227,6 +6280,20 @@ var ts; : { resolvedModule: undefined, failedLookupLocations: failedLookupLocations }; } ts.classicNameResolver = classicNameResolver; + function loadModuleFromAncestorDirectories(moduleName, containingDirectory, supportedExtensions, failedLookupLocations, state) { + while (true) { + var searchName = ts.normalizePath(ts.combinePaths(containingDirectory, moduleName)); + var referencedSourceFile = loadModuleFromFile(searchName, supportedExtensions, failedLookupLocations, false, state); + if (referencedSourceFile) { + return referencedSourceFile; + } + var parentPath = ts.getDirectoryPath(containingDirectory); + if (parentPath === containingDirectory) { + return undefined; + } + containingDirectory = parentPath; + } + } })(ts || (ts = {})); var ts; (function (ts) { @@ -6242,6 +6309,36 @@ var ts; var result = ts.resolveModuleName(packageName, ts.combinePaths(cachePath, "index.d.ts"), { moduleResolution: ts.ModuleResolutionKind.NodeJs }, installTypingHost); return result.resolvedModule && result.resolvedModule.resolvedFileName; } + (function (PackageNameValidationResult) { + PackageNameValidationResult[PackageNameValidationResult["Ok"] = 0] = "Ok"; + PackageNameValidationResult[PackageNameValidationResult["ScopedPackagesNotSupported"] = 1] = "ScopedPackagesNotSupported"; + PackageNameValidationResult[PackageNameValidationResult["NameTooLong"] = 2] = "NameTooLong"; + PackageNameValidationResult[PackageNameValidationResult["NameStartsWithDot"] = 3] = "NameStartsWithDot"; + PackageNameValidationResult[PackageNameValidationResult["NameStartsWithUnderscore"] = 4] = "NameStartsWithUnderscore"; + PackageNameValidationResult[PackageNameValidationResult["NameContainsNonURISafeCharacters"] = 5] = "NameContainsNonURISafeCharacters"; + })(typingsInstaller.PackageNameValidationResult || (typingsInstaller.PackageNameValidationResult = {})); + var PackageNameValidationResult = typingsInstaller.PackageNameValidationResult; + typingsInstaller.MaxPackageNameLength = 214; + function validatePackageName(packageName) { + ts.Debug.assert(!!packageName, "Package name is not specified"); + if (packageName.length > typingsInstaller.MaxPackageNameLength) { + return PackageNameValidationResult.NameTooLong; + } + if (packageName.charCodeAt(0) === 46) { + return PackageNameValidationResult.NameStartsWithDot; + } + if (packageName.charCodeAt(0) === 95) { + return PackageNameValidationResult.NameStartsWithUnderscore; + } + if (/^@[^/]+\/[^/]+$/.test(packageName)) { + return PackageNameValidationResult.ScopedPackagesNotSupported; + } + if (encodeURIComponent(packageName) !== packageName) { + return PackageNameValidationResult.NameContainsNonURISafeCharacters; + } + return PackageNameValidationResult.Ok; + } + typingsInstaller.validatePackageName = validatePackageName; typingsInstaller.NpmViewRequest = "npm view"; typingsInstaller.NpmInstallRequest = "npm install"; var TypingsInstaller = (function () { @@ -6364,15 +6461,54 @@ var ts; } this.knownCachesSet[cacheLocation] = true; }; + TypingsInstaller.prototype.filterTypings = function (typingsToInstall) { + if (typingsToInstall.length === 0) { + return typingsToInstall; + } + var result = []; + for (var _i = 0, typingsToInstall_1 = typingsToInstall; _i < typingsToInstall_1.length; _i++) { + var typing = typingsToInstall_1[_i]; + if (this.missingTypingsSet[typing]) { + continue; + } + var validationResult = validatePackageName(typing); + if (validationResult === PackageNameValidationResult.Ok) { + result.push(typing); + } + else { + this.missingTypingsSet[typing] = true; + if (this.log.isEnabled()) { + switch (validationResult) { + case PackageNameValidationResult.NameTooLong: + this.log.writeLine("Package name '" + typing + "' should be less than " + typingsInstaller.MaxPackageNameLength + " characters"); + break; + case PackageNameValidationResult.NameStartsWithDot: + this.log.writeLine("Package name '" + typing + "' cannot start with '.'"); + break; + case PackageNameValidationResult.NameStartsWithUnderscore: + this.log.writeLine("Package name '" + typing + "' cannot start with '_'"); + break; + case PackageNameValidationResult.ScopedPackagesNotSupported: + this.log.writeLine("Package '" + typing + "' is scoped and currently is not supported"); + break; + case PackageNameValidationResult.NameContainsNonURISafeCharacters: + this.log.writeLine("Package name '" + typing + "' contains non URI safe characters"); + break; + } + } + } + } + return result; + }; TypingsInstaller.prototype.installTypings = function (req, cachePath, currentlyCachedTypings, typingsToInstall) { var _this = this; if (this.log.isEnabled()) { this.log.writeLine("Installing typings " + JSON.stringify(typingsToInstall)); } - typingsToInstall = ts.filter(typingsToInstall, function (x) { return !_this.missingTypingsSet[x]; }); + typingsToInstall = this.filterTypings(typingsToInstall); if (typingsToInstall.length === 0) { if (this.log.isEnabled()) { - this.log.writeLine("All typings are known to be missing - no need to go any further"); + this.log.writeLine("All typings are known to be missing or invalid - no need to go any further"); } return; } @@ -6412,8 +6548,8 @@ var ts; if (_this.log.isEnabled()) { _this.log.writeLine("Installed typing files " + JSON.stringify(installedTypingFiles)); } - for (var _a = 0, typingsToInstall_1 = typingsToInstall; _a < typingsToInstall_1.length; _a++) { - var toInstall = typingsToInstall_1[_a]; + for (var _a = 0, typingsToInstall_2 = typingsToInstall; _a < typingsToInstall_2.length; _a++) { + var toInstall = typingsToInstall_2[_a]; if (!installedPackages[toInstall]) { if (_this.log.isEnabled()) { _this.log.writeLine("New missing typing package '" + toInstall + "'"); @@ -6429,8 +6565,8 @@ var ts; this.installRunCount++; var execInstallCmdCount = 0; var filteredTypings = []; - for (var _i = 0, typingsToInstall_2 = typingsToInstall; _i < typingsToInstall_2.length; _i++) { - var typing = typingsToInstall_2[_i]; + for (var _i = 0, typingsToInstall_3 = typingsToInstall; _i < typingsToInstall_3.length; _i++) { + var typing = typingsToInstall_3[_i]; execNpmViewTyping(this, typing); } function execNpmViewTyping(self, typing) { @@ -6553,13 +6689,14 @@ var ts; var NodeTypingsInstaller = (function (_super) { __extends(NodeTypingsInstaller, _super); function NodeTypingsInstaller(globalTypingsCacheLocation, throttleLimit, log) { - _super.call(this, globalTypingsCacheLocation, getNPMLocation(process.argv[0]), ts.toPath("typingSafeList.json", __dirname, ts.createGetCanonicalFileName(ts.sys.useCaseSensitiveFileNames)), throttleLimit, log); - this.installTypingHost = ts.sys; - if (this.log.isEnabled()) { - this.log.writeLine("Process id: " + process.pid); + var _this = _super.call(this, globalTypingsCacheLocation, getNPMLocation(process.argv[0]), ts.toPath("typingSafeList.json", __dirname, ts.createGetCanonicalFileName(ts.sys.useCaseSensitiveFileNames)), throttleLimit, log) || this; + _this.installTypingHost = ts.sys; + if (_this.log.isEnabled()) { + _this.log.writeLine("Process id: " + process.pid); } var exec = require("child_process").exec; - this.exec = exec; + _this.exec = exec; + return _this; } NodeTypingsInstaller.prototype.init = function () { var _this = this; diff --git a/scripts/buildProtocol.ts b/scripts/buildProtocol.ts new file mode 100644 index 0000000000000..55ad086815c68 --- /dev/null +++ b/scripts/buildProtocol.ts @@ -0,0 +1,150 @@ +/// + +import * as ts from "../lib/typescript"; +import * as path from "path"; + +function endsWith(s: string, suffix: string) { + return s.lastIndexOf(suffix, s.length - suffix.length) !== -1; +} + +class DeclarationsWalker { + private visitedTypes: ts.Type[] = []; + private text = ""; + private constructor(private typeChecker: ts.TypeChecker, private protocolFile: ts.SourceFile) { + } + + static getExtraDeclarations(typeChecker: ts.TypeChecker, protocolFile: ts.SourceFile): string { + let text = "declare namespace ts.server.protocol {\n"; + var walker = new DeclarationsWalker(typeChecker, protocolFile); + walker.visitTypeNodes(protocolFile); + return walker.text + ? `declare namespace ts.server.protocol {\n${walker.text}}` + : ""; + } + + private processType(type: ts.Type): void { + if (this.visitedTypes.indexOf(type) >= 0) { + return; + } + this.visitedTypes.push(type); + let s = type.aliasSymbol || type.getSymbol(); + if (!s) { + return; + } + if (s.name === "Array") { + // we should process type argument instead + return this.processType((type).typeArguments[0]); + } + else { + for (const decl of s.getDeclarations()) { + const sourceFile = decl.getSourceFile(); + if (sourceFile === this.protocolFile || path.basename(sourceFile.fileName) === "lib.d.ts") { + return; + } + // splice declaration in final d.ts file + let text = decl.getFullText(); + if (decl.kind === ts.SyntaxKind.EnumDeclaration && !(decl.flags & ts.NodeFlags.Const)) { + // patch enum declaration to make them constan + const declStart = decl.getStart() - decl.getFullStart(); + const prefix = text.substring(0, declStart); + const suffix = text.substring(declStart + "enum".length, decl.getEnd() - decl.getFullStart()); + text = prefix + "const enum" + suffix; + } + this.text += `${text}\n`; + + // recursively pull all dependencies into result dts file + this.visitTypeNodes(decl); + } + } + } + + private visitTypeNodes(node: ts.Node) { + if (node.parent) { + switch (node.parent.kind) { + case ts.SyntaxKind.VariableDeclaration: + case ts.SyntaxKind.MethodDeclaration: + case ts.SyntaxKind.MethodSignature: + case ts.SyntaxKind.PropertyDeclaration: + case ts.SyntaxKind.PropertySignature: + case ts.SyntaxKind.Parameter: + case ts.SyntaxKind.IndexSignature: + if (((node.parent).type) === node) { + const type = this.typeChecker.getTypeAtLocation(node); + if (type && !(type.flags & ts.TypeFlags.TypeParameter)) { + this.processType(type); + } + } + break; + } + } + ts.forEachChild(node, n => this.visitTypeNodes(n)); + } +} + +function generateProtocolFile(protocolTs: string, typeScriptServicesDts: string): string { + const options = { target: ts.ScriptTarget.ES5, declaration: true, noResolve: true, types: [], stripInternal: true }; + + /** + * 1st pass - generate a program from protocol.ts and typescriptservices.d.ts and emit core version of protocol.d.ts with all internal members stripped + * @return text of protocol.d.t.s + */ + function getInitialDtsFileForProtocol() { + const program = ts.createProgram([protocolTs, typeScriptServicesDts], options); + + let protocolDts: string; + program.emit(program.getSourceFile(protocolTs), (file, content) => { + if (endsWith(file, ".d.ts")) { + protocolDts = content; + } + }); + if (protocolDts === undefined) { + throw new Error(`Declaration file for protocol.ts is not generated`) + } + return protocolDts; + } + + const protocolFileName = "protocol.d.ts"; + /** + * Second pass - generate a program from protocol.d.ts and typescriptservices.d.ts, then augment core protocol.d.ts with extra types from typescriptservices.d.ts + */ + function getProgramWithProtocolText(protocolDts: string, includeTypeScriptServices: boolean) { + const host = ts.createCompilerHost(options); + const originalGetSourceFile = host.getSourceFile; + host.getSourceFile = (fileName) => { + if (fileName === protocolFileName) { + return ts.createSourceFile(fileName, protocolDts, options.target); + } + return originalGetSourceFile.apply(host, [fileName]); + } + const rootFiles = includeTypeScriptServices ? [protocolFileName, typeScriptServicesDts] : [protocolFileName]; + return ts.createProgram(rootFiles, options, host); + } + + let protocolDts = getInitialDtsFileForProtocol(); + const program = getProgramWithProtocolText(protocolDts, /*includeTypeScriptServices*/ true); + + const protocolFile = program.getSourceFile("protocol.d.ts"); + const extraDeclarations = DeclarationsWalker.getExtraDeclarations(program.getTypeChecker(), protocolFile); + if (extraDeclarations) { + protocolDts += extraDeclarations; + } + // do sanity check and try to compile generated text as standalone program + const sanityCheckProgram = getProgramWithProtocolText(protocolDts, /*includeTypeScriptServices*/ false); + const diagnostics = [...program.getSyntacticDiagnostics(), ...program.getSemanticDiagnostics(), ...program.getGlobalDiagnostics()]; + if (diagnostics.length) { + const flattenedDiagnostics = diagnostics.map(d => ts.flattenDiagnosticMessageText(d.messageText, "\n")).join("\n"); + throw new Error(`Unexpected errors during sanity check: ${flattenedDiagnostics}`); + } + return protocolDts; +} + +if (process.argv.length < 5) { + console.log(`Expected 3 arguments: path to 'protocol.ts', path to 'typescriptservices.d.ts' and path to output file`); + process.exit(1); +} + +const protocolTs = process.argv[2]; +const typeScriptServicesDts = process.argv[3]; +const outputFile = process.argv[4]; +const generatedProtocolDts = generateProtocolFile(protocolTs, typeScriptServicesDts); +ts.sys.writeFile(outputFile, generatedProtocolDts); diff --git a/src/compiler/binder.ts b/src/compiler/binder.ts index fe6a28448961a..cec86be7f4174 100644 --- a/src/compiler/binder.ts +++ b/src/compiler/binder.ts @@ -84,6 +84,7 @@ namespace ts { IsFunctionExpression = 1 << 4, HasLocals = 1 << 5, IsInterface = 1 << 6, + IsObjectLiteralOrClassExpressionMethod = 1 << 7, } const binder = createBinder(); @@ -121,7 +122,8 @@ namespace ts { // If this file is an external module, then it is automatically in strict-mode according to // ES6. If it is not an external module, then we'll determine if it is in strict mode or - // not depending on if we see "use strict" in certain places (or if we hit a class/namespace). + // not depending on if we see "use strict" in certain places or if we hit a class/namespace + // or if compiler options contain alwaysStrict. let inStrictMode: boolean; let symbolCount = 0; @@ -139,7 +141,7 @@ namespace ts { file = f; options = opts; languageVersion = getEmitScriptTarget(options); - inStrictMode = !!file.externalModuleIndicator; + inStrictMode = bindInStrictMode(file, opts); classifiableNames = new StringSet(); symbolCount = 0; skipTransformFlagAggregation = isDeclarationFile(file); @@ -174,6 +176,16 @@ namespace ts { return bindSourceFile; + function bindInStrictMode(file: SourceFile, opts: CompilerOptions): boolean { + if (opts.alwaysStrict && !isDeclarationFile(file)) { + // bind in strict mode source files with alwaysStrict option + return true; + } + else { + return !!file.externalModuleIndicator; + } + } + function createSymbol(flags: SymbolFlags, name: string): Symbol { symbolCount++; return new Symbol(flags, name); @@ -486,8 +498,8 @@ namespace ts { } else { currentFlow = { flags: FlowFlags.Start }; - if (containerFlags & ContainerFlags.IsFunctionExpression) { - (currentFlow).container = node; + if (containerFlags & (ContainerFlags.IsFunctionExpression | ContainerFlags.IsObjectLiteralOrClassExpressionMethod)) { + (currentFlow).container = node; } currentReturnTarget = undefined; } @@ -773,6 +785,15 @@ namespace ts { }; } + function createFlowArrayMutation(antecedent: FlowNode, node: CallExpression | BinaryExpression): FlowNode { + setFlowNodeReferenced(antecedent); + return { + flags: FlowFlags.ArrayMutation, + antecedent, + node + }; + } + function finishFlowLabel(flow: FlowLabel): FlowNode { const antecedents = flow.antecedents; if (!antecedents) { @@ -1153,6 +1174,12 @@ namespace ts { forEachChild(node, bind); if (operator === SyntaxKind.EqualsToken && !isAssignmentTarget(node)) { bindAssignmentTargetFlow(node.left); + if (node.left.kind === SyntaxKind.ElementAccessExpression) { + const elementAccess = node.left; + if (isNarrowableOperand(elementAccess.expression)) { + currentFlow = createFlowArrayMutation(currentFlow, node); + } + } } } } @@ -1213,6 +1240,12 @@ namespace ts { else { forEachChild(node, bind); } + if (node.expression.kind === SyntaxKind.PropertyAccessExpression) { + const propertyAccess = node.expression; + if (isNarrowableOperand(propertyAccess.expression) && isPushOrUnshiftIdentifier(propertyAccess.name)) { + currentFlow = createFlowArrayMutation(currentFlow, node); + } + } } function getContainerFlags(node: Node): ContainerFlags { @@ -1237,9 +1270,12 @@ namespace ts { case SyntaxKind.SourceFile: return ContainerFlags.IsContainer | ContainerFlags.IsControlFlowContainer | ContainerFlags.HasLocals; + case SyntaxKind.MethodDeclaration: + if (isObjectLiteralOrClassExpressionMethod(node)) { + return ContainerFlags.IsContainer | ContainerFlags.IsControlFlowContainer | ContainerFlags.HasLocals | ContainerFlags.IsFunctionLike | ContainerFlags.IsObjectLiteralOrClassExpressionMethod; + } case SyntaxKind.Constructor: case SyntaxKind.FunctionDeclaration: - case SyntaxKind.MethodDeclaration: case SyntaxKind.MethodSignature: case SyntaxKind.GetAccessor: case SyntaxKind.SetAccessor: @@ -1648,7 +1684,7 @@ namespace ts { } function checkStrictModeFunctionDeclaration(node: FunctionDeclaration) { - if (languageVersion < ScriptTarget.ES6) { + if (languageVersion < ScriptTarget.ES2015) { // Report error if function is not top level function declaration if (blockScopeContainer.kind !== SyntaxKind.SourceFile && blockScopeContainer.kind !== SyntaxKind.ModuleDeclaration && @@ -2238,6 +2274,10 @@ namespace ts { } } + if (currentFlow && isObjectLiteralOrClassExpressionMethod(node)) { + node.flowNode = currentFlow; + } + return hasDynamicName(node) ? bindAnonymousDeclaration(node, symbolFlags, "__computed") : declareSymbolAndAddToSymbolTable(node, symbolFlags, symbolExcludes); @@ -2311,6 +2351,9 @@ namespace ts { case SyntaxKind.CallExpression: return computeCallExpression(node, subtreeFlags); + case SyntaxKind.NewExpression: + return computeNewExpression(node, subtreeFlags); + case SyntaxKind.ModuleDeclaration: return computeModuleDeclaration(node, subtreeFlags); @@ -2388,11 +2431,15 @@ namespace ts { const expression = node.expression; const expressionKind = expression.kind; + if (node.typeArguments) { + transformFlags |= TransformFlags.AssertTypeScript; + } + if (subtreeFlags & TransformFlags.ContainsSpreadElementExpression || isSuperOrSuperProperty(expression, expressionKind)) { // If the this node contains a SpreadElementExpression, or is a super call, then it is an ES6 // node. - transformFlags |= TransformFlags.AssertES6; + transformFlags |= TransformFlags.AssertES2015; } node.transformFlags = transformFlags | TransformFlags.HasComputedFlags; @@ -2414,6 +2461,21 @@ namespace ts { return false; } + function computeNewExpression(node: NewExpression, subtreeFlags: TransformFlags) { + let transformFlags = subtreeFlags; + if (node.typeArguments) { + transformFlags |= TransformFlags.AssertTypeScript; + } + if (subtreeFlags & TransformFlags.ContainsSpreadElementExpression) { + // If the this node contains a SpreadElementExpression then it is an ES6 + // node. + transformFlags |= TransformFlags.AssertES2015; + } + node.transformFlags = transformFlags | TransformFlags.HasComputedFlags; + return transformFlags & ~TransformFlags.ArrayLiteralOrCallOrNewExcludes; + } + + function computeBinaryExpression(node: BinaryExpression, subtreeFlags: TransformFlags) { let transformFlags = subtreeFlags; const operatorTokenKind = node.operatorToken.kind; @@ -2423,12 +2485,12 @@ namespace ts { && (leftKind === SyntaxKind.ObjectLiteralExpression || leftKind === SyntaxKind.ArrayLiteralExpression)) { // Destructuring assignments are ES6 syntax. - transformFlags |= TransformFlags.AssertES6 | TransformFlags.DestructuringAssignment; + transformFlags |= TransformFlags.AssertES2015 | TransformFlags.DestructuringAssignment; } else if (operatorTokenKind === SyntaxKind.AsteriskAsteriskToken || operatorTokenKind === SyntaxKind.AsteriskAsteriskEqualsToken) { - // Exponentiation is ES7 syntax. - transformFlags |= TransformFlags.AssertES7; + // Exponentiation is ES2016 syntax. + transformFlags |= TransformFlags.AssertES2016; } node.transformFlags = transformFlags | TransformFlags.HasComputedFlags; @@ -2442,13 +2504,12 @@ namespace ts { const initializer = node.initializer; const dotDotDotToken = node.dotDotDotToken; - // If the parameter has a question token, then it is TypeScript syntax. - if (node.questionToken) { - transformFlags |= TransformFlags.AssertTypeScript; - } - - // If the parameter's name is 'this', then it is TypeScript syntax. - if (subtreeFlags & TransformFlags.ContainsDecorators || isThisIdentifier(name)) { + // The '?' token, type annotations, decorators, and 'this' parameters are TypeSCript + // syntax. + if (node.questionToken + || node.type + || subtreeFlags & TransformFlags.ContainsDecorators + || isThisIdentifier(name)) { transformFlags |= TransformFlags.AssertTypeScript; } @@ -2460,7 +2521,7 @@ namespace ts { // If a parameter has an initializer, a binding pattern or a dotDotDot token, then // it is ES6 syntax and its container must emit default value assignments or parameter destructuring downlevel. if (subtreeFlags & TransformFlags.ContainsBindingPattern || initializer || dotDotDotToken) { - transformFlags |= TransformFlags.AssertES6 | TransformFlags.ContainsDefaultValueAssignments; + transformFlags |= TransformFlags.AssertES2015 | TransformFlags.ContainsDefaultValueAssignments; } node.transformFlags = transformFlags | TransformFlags.HasComputedFlags; @@ -2501,13 +2562,14 @@ namespace ts { } else { // A ClassDeclaration is ES6 syntax. - transformFlags = subtreeFlags | TransformFlags.AssertES6; + transformFlags = subtreeFlags | TransformFlags.AssertES2015; // A class with a parameter property assignment, property initializer, or decorator is // TypeScript syntax. // An exported declaration may be TypeScript syntax. if ((subtreeFlags & TransformFlags.TypeScriptClassSyntaxMask) - || (modifierFlags & ModifierFlags.Export)) { + || (modifierFlags & ModifierFlags.Export) + || node.typeParameters) { transformFlags |= TransformFlags.AssertTypeScript; } @@ -2524,11 +2586,12 @@ namespace ts { function computeClassExpression(node: ClassExpression, subtreeFlags: TransformFlags) { // A ClassExpression is ES6 syntax. - let transformFlags = subtreeFlags | TransformFlags.AssertES6; + let transformFlags = subtreeFlags | TransformFlags.AssertES2015; // A class with a parameter property assignment, property initializer, or decorator is // TypeScript syntax. - if (subtreeFlags & TransformFlags.TypeScriptClassSyntaxMask) { + if (subtreeFlags & TransformFlags.TypeScriptClassSyntaxMask + || node.typeParameters) { transformFlags |= TransformFlags.AssertTypeScript; } @@ -2548,7 +2611,7 @@ namespace ts { switch (node.token) { case SyntaxKind.ExtendsKeyword: // An `extends` HeritageClause is ES6 syntax. - transformFlags |= TransformFlags.AssertES6; + transformFlags |= TransformFlags.AssertES2015; break; case SyntaxKind.ImplementsKeyword: @@ -2568,7 +2631,7 @@ namespace ts { function computeExpressionWithTypeArguments(node: ExpressionWithTypeArguments, subtreeFlags: TransformFlags) { // An ExpressionWithTypeArguments is ES6 syntax, as it is used in the // extends clause of a class. - let transformFlags = subtreeFlags | TransformFlags.AssertES6; + let transformFlags = subtreeFlags | TransformFlags.AssertES2015; // If an ExpressionWithTypeArguments contains type arguments, then it // is TypeScript syntax. @@ -2582,10 +2645,10 @@ namespace ts { function computeConstructor(node: ConstructorDeclaration, subtreeFlags: TransformFlags) { let transformFlags = subtreeFlags; - const body = node.body; - if (body === undefined) { - // An overload constructor is TypeScript syntax. + // TypeScript-specific modifiers and overloads are TypeScript syntax + if (hasModifier(node, ModifierFlags.TypeScriptModifier) + || !node.body) { transformFlags |= TransformFlags.AssertTypeScript; } @@ -2595,23 +2658,25 @@ namespace ts { function computeMethod(node: MethodDeclaration, subtreeFlags: TransformFlags) { // A MethodDeclaration is ES6 syntax. - let transformFlags = subtreeFlags | TransformFlags.AssertES6; - const modifierFlags = getModifierFlags(node); - const body = node.body; - const typeParameters = node.typeParameters; - const asteriskToken = node.asteriskToken; - - // A MethodDeclaration is TypeScript syntax if it is either async, abstract, overloaded, - // generic, or has a decorator. - if (!body - || typeParameters - || (modifierFlags & (ModifierFlags.Async | ModifierFlags.Abstract)) - || (subtreeFlags & TransformFlags.ContainsDecorators)) { + let transformFlags = subtreeFlags | TransformFlags.AssertES2015; + + // Decorators, TypeScript-specific modifiers, type parameters, type annotations, and + // overloads are TypeScript syntax. + if (node.decorators + || hasModifier(node, ModifierFlags.TypeScriptModifier) + || node.typeParameters + || node.type + || !node.body) { transformFlags |= TransformFlags.AssertTypeScript; } + // An async method declaration is ES2017 syntax. + if (hasModifier(node, ModifierFlags.Async)) { + transformFlags |= TransformFlags.AssertES2017; + } + // Currently, we only support generators that were originally async function bodies. - if (asteriskToken && getEmitFlags(node) & EmitFlags.AsyncFunctionBody) { + if (node.asteriskToken && getEmitFlags(node) & EmitFlags.AsyncFunctionBody) { transformFlags |= TransformFlags.AssertGenerator; } @@ -2621,14 +2686,13 @@ namespace ts { function computeAccessor(node: AccessorDeclaration, subtreeFlags: TransformFlags) { let transformFlags = subtreeFlags; - const modifierFlags = getModifierFlags(node); - const body = node.body; - // A MethodDeclaration is TypeScript syntax if it is either async, abstract, overloaded, - // generic, or has a decorator. - if (!body - || (modifierFlags & (ModifierFlags.Async | ModifierFlags.Abstract)) - || (subtreeFlags & TransformFlags.ContainsDecorators)) { + // Decorators, TypeScript-specific modifiers, type annotations, and overloads are + // TypeScript syntax. + if (node.decorators + || hasModifier(node, ModifierFlags.TypeScriptModifier) + || node.type + || !node.body) { transformFlags |= TransformFlags.AssertTypeScript; } @@ -2654,7 +2718,6 @@ namespace ts { let transformFlags: TransformFlags; const modifierFlags = getModifierFlags(node); const body = node.body; - const asteriskToken = node.asteriskToken; if (!body || (modifierFlags & ModifierFlags.Ambient)) { // An ambient declaration is TypeScript syntax. @@ -2666,19 +2729,27 @@ namespace ts { // If a FunctionDeclaration is exported, then it is either ES6 or TypeScript syntax. if (modifierFlags & ModifierFlags.Export) { - transformFlags |= TransformFlags.AssertTypeScript | TransformFlags.AssertES6; + transformFlags |= TransformFlags.AssertTypeScript | TransformFlags.AssertES2015; } - // If a FunctionDeclaration is async, then it is TypeScript syntax. - if (modifierFlags & ModifierFlags.Async) { + // TypeScript-specific modifiers, type parameters, and type annotations are TypeScript + // syntax. + if (modifierFlags & ModifierFlags.TypeScriptModifier + || node.typeParameters + || node.type) { transformFlags |= TransformFlags.AssertTypeScript; } + // An async function declaration is ES2017 syntax. + if (modifierFlags & ModifierFlags.Async) { + transformFlags |= TransformFlags.AssertES2017; + } + // If a FunctionDeclaration's subtree has marked the container as needing to capture the // lexical this, or the function contains parameters with initializers, then this node is // ES6 syntax. - if (subtreeFlags & TransformFlags.ES6FunctionSyntaxMask) { - transformFlags |= TransformFlags.AssertES6; + if (subtreeFlags & TransformFlags.ES2015FunctionSyntaxMask) { + transformFlags |= TransformFlags.AssertES2015; } // If a FunctionDeclaration is generator function and is the body of a @@ -2686,7 +2757,7 @@ namespace ts { // down-level generator. // Currently we do not support transforming any other generator fucntions // down level. - if (asteriskToken && getEmitFlags(node) & EmitFlags.AsyncFunctionBody) { + if (node.asteriskToken && getEmitFlags(node) & EmitFlags.AsyncFunctionBody) { transformFlags |= TransformFlags.AssertGenerator; } } @@ -2697,19 +2768,25 @@ namespace ts { function computeFunctionExpression(node: FunctionExpression, subtreeFlags: TransformFlags) { let transformFlags = subtreeFlags; - const modifierFlags = getModifierFlags(node); - const asteriskToken = node.asteriskToken; - // An async function expression is TypeScript syntax. - if (modifierFlags & ModifierFlags.Async) { + // TypeScript-specific modifiers, type parameters, and type annotations are TypeScript + // syntax. + if (hasModifier(node, ModifierFlags.TypeScriptModifier) + || node.typeParameters + || node.type) { transformFlags |= TransformFlags.AssertTypeScript; } + // An async function expression is ES2017 syntax. + if (hasModifier(node, ModifierFlags.Async)) { + transformFlags |= TransformFlags.AssertES2017; + } + // If a FunctionExpression's subtree has marked the container as needing to capture the // lexical this, or the function contains parameters with initializers, then this node is // ES6 syntax. - if (subtreeFlags & TransformFlags.ES6FunctionSyntaxMask) { - transformFlags |= TransformFlags.AssertES6; + if (subtreeFlags & TransformFlags.ES2015FunctionSyntaxMask) { + transformFlags |= TransformFlags.AssertES2015; } // If a FunctionExpression is generator function and is the body of a @@ -2717,7 +2794,7 @@ namespace ts { // down-level generator. // Currently we do not support transforming any other generator fucntions // down level. - if (asteriskToken && getEmitFlags(node) & EmitFlags.AsyncFunctionBody) { + if (node.asteriskToken && getEmitFlags(node) & EmitFlags.AsyncFunctionBody) { transformFlags |= TransformFlags.AssertGenerator; } @@ -2727,14 +2804,21 @@ namespace ts { function computeArrowFunction(node: ArrowFunction, subtreeFlags: TransformFlags) { // An ArrowFunction is ES6 syntax, and excludes markers that should not escape the scope of an ArrowFunction. - let transformFlags = subtreeFlags | TransformFlags.AssertES6; - const modifierFlags = getModifierFlags(node); + let transformFlags = subtreeFlags | TransformFlags.AssertES2015; - // An async arrow function is TypeScript syntax. - if (modifierFlags & ModifierFlags.Async) { + // TypeScript-specific modifiers, type parameters, and type annotations are TypeScript + // syntax. + if (hasModifier(node, ModifierFlags.TypeScriptModifier) + || node.typeParameters + || node.type) { transformFlags |= TransformFlags.AssertTypeScript; } + // An async arrow function is ES2017 syntax. + if (hasModifier(node, ModifierFlags.Async)) { + transformFlags |= TransformFlags.AssertES2017; + } + // If an ArrowFunction contains a lexical this, its container must capture the lexical this. if (subtreeFlags & TransformFlags.ContainsLexicalThis) { transformFlags |= TransformFlags.ContainsCapturedLexicalThis; @@ -2765,7 +2849,12 @@ namespace ts { // A VariableDeclaration with a binding pattern is ES6 syntax. if (nameKind === SyntaxKind.ObjectBindingPattern || nameKind === SyntaxKind.ArrayBindingPattern) { - transformFlags |= TransformFlags.AssertES6 | TransformFlags.ContainsBindingPattern; + transformFlags |= TransformFlags.AssertES2015 | TransformFlags.ContainsBindingPattern; + } + + // Type annotations are TypeScript syntax. + if (node.type) { + transformFlags |= TransformFlags.AssertTypeScript; } node.transformFlags = transformFlags | TransformFlags.HasComputedFlags; @@ -2786,11 +2875,11 @@ namespace ts { // If a VariableStatement is exported, then it is either ES6 or TypeScript syntax. if (modifierFlags & ModifierFlags.Export) { - transformFlags |= TransformFlags.AssertES6 | TransformFlags.AssertTypeScript; + transformFlags |= TransformFlags.AssertES2015 | TransformFlags.AssertTypeScript; } if (declarationListTransformFlags & TransformFlags.ContainsBindingPattern) { - transformFlags |= TransformFlags.AssertES6; + transformFlags |= TransformFlags.AssertES2015; } } @@ -2804,7 +2893,7 @@ namespace ts { // A labeled statement containing a block scoped binding *may* need to be transformed from ES6. if (subtreeFlags & TransformFlags.ContainsBlockScopedBinding && isIterationStatement(node, /*lookInLabeledStatements*/ true)) { - transformFlags |= TransformFlags.AssertES6; + transformFlags |= TransformFlags.AssertES2015; } node.transformFlags = transformFlags | TransformFlags.HasComputedFlags; @@ -2830,7 +2919,7 @@ namespace ts { // then we treat the statement as ES6 so that we can indicate that we do not // need to hold on to the right-hand side. if (node.expression.transformFlags & TransformFlags.DestructuringAssignment) { - transformFlags |= TransformFlags.AssertES6; + transformFlags |= TransformFlags.AssertES2015; } node.transformFlags = transformFlags | TransformFlags.HasComputedFlags; @@ -2853,12 +2942,12 @@ namespace ts { let transformFlags = subtreeFlags | TransformFlags.ContainsHoistedDeclarationOrCompletion; if (subtreeFlags & TransformFlags.ContainsBindingPattern) { - transformFlags |= TransformFlags.AssertES6; + transformFlags |= TransformFlags.AssertES2015; } // If a VariableDeclarationList is `let` or `const`, then it is ES6 syntax. if (node.flags & NodeFlags.BlockScoped) { - transformFlags |= TransformFlags.AssertES6 | TransformFlags.ContainsBlockScopedBinding; + transformFlags |= TransformFlags.AssertES2015 | TransformFlags.ContainsBlockScopedBinding; } node.transformFlags = transformFlags | TransformFlags.HasComputedFlags; @@ -2871,14 +2960,18 @@ namespace ts { let excludeFlags = TransformFlags.NodeExcludes; switch (kind) { + case SyntaxKind.AsyncKeyword: + case SyntaxKind.AwaitExpression: + // async/await is ES2017 syntax + transformFlags |= TransformFlags.AssertES2017; + break; + case SyntaxKind.PublicKeyword: case SyntaxKind.PrivateKeyword: case SyntaxKind.ProtectedKeyword: case SyntaxKind.AbstractKeyword: case SyntaxKind.DeclareKeyword: - case SyntaxKind.AsyncKeyword: case SyntaxKind.ConstKeyword: - case SyntaxKind.AwaitExpression: case SyntaxKind.EnumDeclaration: case SyntaxKind.EnumMember: case SyntaxKind.TypeAssertionExpression: @@ -2903,7 +2996,7 @@ namespace ts { case SyntaxKind.ExportKeyword: // This node is both ES6 and TypeScript syntax. - transformFlags |= TransformFlags.AssertES6 | TransformFlags.AssertTypeScript; + transformFlags |= TransformFlags.AssertES2015 | TransformFlags.AssertTypeScript; break; case SyntaxKind.DefaultKeyword: @@ -2916,12 +3009,12 @@ namespace ts { case SyntaxKind.ShorthandPropertyAssignment: case SyntaxKind.ForOfStatement: // These nodes are ES6 syntax. - transformFlags |= TransformFlags.AssertES6; + transformFlags |= TransformFlags.AssertES2015; break; case SyntaxKind.YieldExpression: // This node is ES6 syntax. - transformFlags |= TransformFlags.AssertES6 | TransformFlags.ContainsYield; + transformFlags |= TransformFlags.AssertES2015 | TransformFlags.ContainsYield; break; case SyntaxKind.AnyKeyword: @@ -2982,7 +3075,7 @@ namespace ts { case SyntaxKind.SuperKeyword: // This node is ES6 syntax. - transformFlags |= TransformFlags.AssertES6; + transformFlags |= TransformFlags.AssertES2015; break; case SyntaxKind.ThisKeyword: @@ -2993,7 +3086,7 @@ namespace ts { case SyntaxKind.ObjectBindingPattern: case SyntaxKind.ArrayBindingPattern: // These nodes are ES6 syntax. - transformFlags |= TransformFlags.AssertES6 | TransformFlags.ContainsBindingPattern; + transformFlags |= TransformFlags.AssertES2015 | TransformFlags.ContainsBindingPattern; break; case SyntaxKind.Decorator: @@ -3006,7 +3099,7 @@ namespace ts { if (subtreeFlags & TransformFlags.ContainsComputedPropertyName) { // If an ObjectLiteralExpression contains a ComputedPropertyName, then it // is an ES6 node. - transformFlags |= TransformFlags.AssertES6; + transformFlags |= TransformFlags.AssertES2015; } if (subtreeFlags & TransformFlags.ContainsLexicalThisInComputedPropertyName) { @@ -3023,7 +3116,7 @@ namespace ts { if (subtreeFlags & TransformFlags.ContainsSpreadElementExpression) { // If the this node contains a SpreadElementExpression, then it is an ES6 // node. - transformFlags |= TransformFlags.AssertES6; + transformFlags |= TransformFlags.AssertES2015; } break; @@ -3034,14 +3127,14 @@ namespace ts { case SyntaxKind.ForInStatement: // A loop containing a block scoped binding *may* need to be transformed from ES6. if (subtreeFlags & TransformFlags.ContainsBlockScopedBinding) { - transformFlags |= TransformFlags.AssertES6; + transformFlags |= TransformFlags.AssertES2015; } break; case SyntaxKind.SourceFile: if (subtreeFlags & TransformFlags.ContainsCapturedLexicalThis) { - transformFlags |= TransformFlags.AssertES6; + transformFlags |= TransformFlags.AssertES2015; } break; diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index a01c3d9f975ec..f0c656d719a3f 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -115,6 +115,7 @@ namespace ts { const intersectionTypes = new StringMap(); const stringLiteralTypes = new StringMap(); const numericLiteralTypes = new StringMap(); + const evolvingArrayTypes: AnonymousType[] = []; const unknownSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Transient, "unknown"); const resolvingSymbol = createSymbol(SymbolFlags.Transient, "__resolving__"); @@ -176,6 +177,7 @@ namespace ts { let globalBooleanType: ObjectType; let globalRegExpType: ObjectType; let anyArrayType: Type; + let autoArrayType: Type; let anyReadonlyArrayType: Type; // The library files are only loaded when the feature is used. @@ -923,7 +925,7 @@ namespace ts { if (result && isInExternalModule && (meaning & SymbolFlags.Value) === SymbolFlags.Value) { const decls = result.declarations; if (decls && decls.length === 1 && decls[0].kind === SyntaxKind.NamespaceExportDeclaration) { - error(errorLocation, Diagnostics.Identifier_0_must_be_imported_from_a_module, name); + error(errorLocation, Diagnostics._0_refers_to_a_UMD_global_but_the_current_file_is_a_module_Consider_adding_an_import_instead, name); } } } @@ -1475,6 +1477,10 @@ namespace ts { function getExportsForModule(moduleSymbol: Symbol): SymbolTable { const visitedSymbols: Symbol[] = []; + + // A module defined by an 'export=' consists on one export that needs to be resolved + moduleSymbol = resolveExternalModuleSymbol(moduleSymbol); + return visit(moduleSymbol) || moduleSymbol.exports; // The ES6 spec permits export * declarations in a module to circularly reference the module itself. For example, @@ -3065,9 +3071,14 @@ namespace ts { return undefined; } - function isAutoVariableInitializer(initializer: Expression) { - const expr = initializer && skipParentheses(initializer); - return !expr || expr.kind === SyntaxKind.NullKeyword || expr.kind === SyntaxKind.Identifier && getResolvedSymbol(expr) === undefinedSymbol; + function isNullOrUndefined(node: Expression) { + const expr = skipParentheses(node); + return expr.kind === SyntaxKind.NullKeyword || expr.kind === SyntaxKind.Identifier && getResolvedSymbol(expr) === undefinedSymbol; + } + + function isEmptyArrayLiteral(node: Expression) { + const expr = skipParentheses(node); + return expr.kind === SyntaxKind.ArrayLiteralExpression && (expr).elements.length === 0; } function addOptionality(type: Type, optional: boolean): Type { @@ -3108,12 +3119,18 @@ namespace ts { return addOptionality(getTypeFromTypeNode(declaration.type), /*optional*/ declaration.questionToken && includeOptionality); } - // Use control flow type inference for non-ambient, non-exported var or let variables with no initializer - // or a 'null' or 'undefined' initializer. if (declaration.kind === SyntaxKind.VariableDeclaration && !isBindingPattern(declaration.name) && - !(getCombinedNodeFlags(declaration) & NodeFlags.Const) && !(getCombinedModifierFlags(declaration) & ModifierFlags.Export) && - !isInAmbientContext(declaration) && isAutoVariableInitializer(declaration.initializer)) { - return autoType; + !(getCombinedModifierFlags(declaration) & ModifierFlags.Export) && !isInAmbientContext(declaration)) { + // Use control flow tracked 'any' type for non-ambient, non-exported var or let variables with no + // initializer or a 'null' or 'undefined' initializer. + if (!(getCombinedNodeFlags(declaration) & NodeFlags.Const) && (!declaration.initializer || isNullOrUndefined(declaration.initializer))) { + return autoType; + } + // Use control flow tracked 'any[]' type for non-ambient, non-exported variables with an empty array + // literal initializer. + if (declaration.initializer && isEmptyArrayLiteral(declaration.initializer)) { + return autoArrayType; + } } if (declaration.kind === SyntaxKind.Parameter) { @@ -3216,7 +3233,7 @@ namespace ts { const elements = pattern.elements; const lastElement = lastOrUndefined(elements); if (elements.length === 0 || (!isOmittedExpression(lastElement) && lastElement.dotDotDotToken)) { - return languageVersion >= ScriptTarget.ES6 ? createIterableType(anyType) : anyArrayType; + return languageVersion >= ScriptTarget.ES2015 ? createIterableType(anyType) : anyArrayType; } // If the pattern has at least one element, and no rest element, then it should imply a tuple type. const elementTypes = map(elements, e => isOmittedExpression(e) ? anyType : getTypeFromBindingElement(e, includePatternInType, reportErrors)); @@ -7429,7 +7446,7 @@ namespace ts { type.flags & TypeFlags.NumberLiteral ? numberType : type.flags & TypeFlags.BooleanLiteral ? booleanType : type.flags & TypeFlags.EnumLiteral ? (type).baseType : - type.flags & TypeFlags.Union && !(type.flags & TypeFlags.Enum) ? getUnionType(map((type).types, getBaseTypeOfLiteralType)) : + type.flags & TypeFlags.Union && !(type.flags & TypeFlags.Enum) ? getUnionType(sameMap((type).types, getBaseTypeOfLiteralType)) : type; } @@ -7438,7 +7455,7 @@ namespace ts { type.flags & TypeFlags.NumberLiteral && type.flags & TypeFlags.FreshLiteral ? numberType : type.flags & TypeFlags.BooleanLiteral ? booleanType : type.flags & TypeFlags.EnumLiteral ? (type).baseType : - type.flags & TypeFlags.Union && !(type.flags & TypeFlags.Enum) ? getUnionType(map((type).types, getWidenedLiteralType)) : + type.flags & TypeFlags.Union && !(type.flags & TypeFlags.Enum) ? getUnionType(sameMap((type).types, getWidenedLiteralType)) : type; } @@ -7577,10 +7594,10 @@ namespace ts { return getWidenedTypeOfObjectLiteral(type); } if (type.flags & TypeFlags.Union) { - return getUnionType(map((type).types, getWidenedConstituentType)); + return getUnionType(sameMap((type).types, getWidenedConstituentType)); } if (isArrayType(type) || isTupleType(type)) { - return createTypeReference((type).target, map((type).typeArguments, getWidenedType)); + return createTypeReference((type).target, sameMap((type).typeArguments, getWidenedType)); } } return type; @@ -7998,7 +8015,7 @@ namespace ts { const widenLiteralTypes = context.inferences[index].topLevel && !hasPrimitiveConstraint(signature.typeParameters[index]) && (context.inferences[index].isFixed || !isTypeParameterAtTopLevel(getReturnTypeOfSignature(signature), signature.typeParameters[index])); - const baseInferences = widenLiteralTypes ? map(inferences, getWidenedLiteralType) : inferences; + const baseInferences = widenLiteralTypes ? sameMap(inferences, getWidenedLiteralType) : inferences; // Infer widened union or supertype, or the unknown type for no common supertype const unionOrSuperType = context.inferUnionTypes ? getUnionType(baseInferences, /*subtypeReduction*/ true) : getCommonSupertype(baseInferences); inferredType = unionOrSuperType ? getWidenedType(unionOrSuperType) : unknownType; @@ -8401,6 +8418,13 @@ namespace ts { getAssignedType(node); } + function isEmptyArrayAssignment(node: VariableDeclaration | BindingElement | Expression) { + return node.kind === SyntaxKind.VariableDeclaration && (node).initializer && + isEmptyArrayLiteral((node).initializer) || + node.kind !== SyntaxKind.BindingElement && node.parent.kind === SyntaxKind.BinaryExpression && + isEmptyArrayLiteral((node.parent).right); + } + function getReferenceCandidate(node: Expression): Expression { switch (node.kind) { case SyntaxKind.ParenthesizedExpression: @@ -8416,6 +8440,14 @@ namespace ts { return node; } + function getReferenceRoot(node: Node): Node { + const parent = node.parent; + return parent.kind === SyntaxKind.ParenthesizedExpression || + parent.kind === SyntaxKind.BinaryExpression && (parent).operatorToken.kind === SyntaxKind.EqualsToken && (parent).left === node || + parent.kind === SyntaxKind.BinaryExpression && (parent).operatorToken.kind === SyntaxKind.CommaToken && (parent).right === node ? + getReferenceRoot(parent) : node; + } + function getTypeOfSwitchClause(clause: CaseClause | DefaultClause) { if (clause.kind === SyntaxKind.CaseClause) { const caseType = getRegularTypeOfLiteralType(checkExpression((clause).expression)); @@ -8467,6 +8499,28 @@ namespace ts { return f(type) ? type : neverType; } + function mapType(type: Type, f: (t: Type) => Type): Type { + return type.flags & TypeFlags.Union ? getUnionType(map((type).types, f)) : f(type); + } + + function extractTypesOfKind(type: Type, kind: TypeFlags) { + return filterType(type, t => (t.flags & kind) !== 0); + } + + // Return a new type in which occurrences of the string and number primitive types in + // typeWithPrimitives have been replaced with occurrences of string literals and numeric + // literals in typeWithLiterals, respectively. + function replacePrimitivesWithLiterals(typeWithPrimitives: Type, typeWithLiterals: Type) { + if (isTypeSubsetOf(stringType, typeWithPrimitives) && maybeTypeOfKind(typeWithLiterals, TypeFlags.StringLiteral) || + isTypeSubsetOf(numberType, typeWithPrimitives) && maybeTypeOfKind(typeWithLiterals, TypeFlags.NumberLiteral)) { + return mapType(typeWithPrimitives, t => + t.flags & TypeFlags.String ? extractTypesOfKind(typeWithLiterals, TypeFlags.String | TypeFlags.StringLiteral) : + t.flags & TypeFlags.Number ? extractTypesOfKind(typeWithLiterals, TypeFlags.Number | TypeFlags.NumberLiteral) : + t); + } + return typeWithPrimitives; + } + function isIncomplete(flowType: FlowType) { return flowType.flags === 0; } @@ -8479,21 +8533,113 @@ namespace ts { return incomplete ? { flags: 0, type } : type; } + // An evolving array type tracks the element types that have so far been seen in an + // 'x.push(value)' or 'x[n] = value' operation along the control flow graph. Evolving + // array types are ultimately converted into manifest array types (using getFinalArrayType) + // and never escape the getFlowTypeOfReference function. + function createEvolvingArrayType(elementType: Type): AnonymousType { + const result = createObjectType(TypeFlags.Anonymous); + result.elementType = elementType; + return result; + } + + function getEvolvingArrayType(elementType: Type): AnonymousType { + return evolvingArrayTypes[elementType.id] || (evolvingArrayTypes[elementType.id] = createEvolvingArrayType(elementType)); + } + + // When adding evolving array element types we do not perform subtype reduction. Instead, + // we defer subtype reduction until the evolving array type is finalized into a manifest + // array type. + function addEvolvingArrayElementType(evolvingArrayType: AnonymousType, node: Expression): AnonymousType { + const elementType = getBaseTypeOfLiteralType(checkExpression(node)); + return isTypeSubsetOf(elementType, evolvingArrayType.elementType) ? evolvingArrayType : getEvolvingArrayType(getUnionType([evolvingArrayType.elementType, elementType])); + } + + function isEvolvingArrayType(type: Type) { + return !!(type.flags & TypeFlags.Anonymous && (type).elementType); + } + + function createFinalArrayType(elementType: Type) { + return elementType.flags & TypeFlags.Never ? + autoArrayType : + createArrayType(elementType.flags & TypeFlags.Union ? + getUnionType((elementType).types, /*subtypeReduction*/ true) : + elementType); + } + + // We perform subtype reduction upon obtaining the final array type from an evolving array type. + function getFinalArrayType(evolvingArrayType: AnonymousType): Type { + return evolvingArrayType.finalArrayType || (evolvingArrayType.finalArrayType = createFinalArrayType(evolvingArrayType.elementType)); + } + + function finalizeEvolvingArrayType(type: Type): Type { + return isEvolvingArrayType(type) ? getFinalArrayType(type) : type; + } + + function getElementTypeOfEvolvingArrayType(type: Type) { + return isEvolvingArrayType(type) ? (type).elementType : neverType; + } + + function isEvolvingArrayTypeList(types: Type[]) { + let hasEvolvingArrayType = false; + for (const t of types) { + if (!(t.flags & TypeFlags.Never)) { + if (!isEvolvingArrayType(t)) { + return false; + } + hasEvolvingArrayType = true; + } + } + return hasEvolvingArrayType; + } + + // At flow control branch or loop junctions, if the type along every antecedent code path + // is an evolving array type, we construct a combined evolving array type. Otherwise we + // finalize all evolving array types. + function getUnionOrEvolvingArrayType(types: Type[], subtypeReduction: boolean) { + return isEvolvingArrayTypeList(types) ? + getEvolvingArrayType(getUnionType(map(types, getElementTypeOfEvolvingArrayType))) : + getUnionType(sameMap(types, finalizeEvolvingArrayType), subtypeReduction); + } + + // Return true if the given node is 'x' in an 'x.length', x.push(value)', 'x.unshift(value)' or + // 'x[n] = value' operation, where 'n' is an expression of type any, undefined, or a number-like type. + function isEvolvingArrayOperationTarget(node: Node) { + const root = getReferenceRoot(node); + const parent = root.parent; + const isLengthPushOrUnshift = parent.kind === SyntaxKind.PropertyAccessExpression && ( + (parent).name.text === "length" || + parent.parent.kind === SyntaxKind.CallExpression && isPushOrUnshiftIdentifier((parent).name)); + const isElementAssignment = parent.kind === SyntaxKind.ElementAccessExpression && + (parent).expression === root && + parent.parent.kind === SyntaxKind.BinaryExpression && + (parent.parent).operatorToken.kind === SyntaxKind.EqualsToken && + (parent.parent).left === parent && + !isAssignmentTarget(parent.parent) && + isTypeAnyOrAllConstituentTypesHaveKind(checkExpression((parent).argumentExpression), TypeFlags.NumberLike | TypeFlags.Undefined); + return isLengthPushOrUnshift || isElementAssignment; + } + function getFlowTypeOfReference(reference: Node, declaredType: Type, assumeInitialized: boolean, flowContainer: Node) { let key: string; if (!reference.flowNode || assumeInitialized && !(declaredType.flags & TypeFlags.Narrowable)) { return declaredType; } const initialType = assumeInitialized ? declaredType : - declaredType === autoType ? undefinedType : + declaredType === autoType || declaredType === autoArrayType ? undefinedType : includeFalsyTypes(declaredType, TypeFlags.Undefined); const visitedFlowStart = visitedFlowCount; - const result = getTypeFromFlowType(getTypeAtFlowNode(reference.flowNode)); + const evolvedType = getTypeFromFlowType(getTypeAtFlowNode(reference.flowNode)); visitedFlowCount = visitedFlowStart; - if (reference.parent.kind === SyntaxKind.NonNullExpression && getTypeWithFacts(result, TypeFacts.NEUndefinedOrNull).flags & TypeFlags.Never) { + // When the reference is 'x' in an 'x.length', 'x.push(value)', 'x.unshift(value)' or x[n] = value' operation, + // we give type 'any[]' to 'x' instead of using the type determined by control flow analysis such that operations + // on empty arrays are possible without implicit any errors and new element types can be inferred without + // type mismatch errors. + const resultType = isEvolvingArrayType(evolvedType) && isEvolvingArrayOperationTarget(reference) ? anyArrayType : finalizeEvolvingArrayType(evolvedType); + if (reference.parent.kind === SyntaxKind.NonNullExpression && getTypeWithFacts(resultType, TypeFacts.NEUndefinedOrNull).flags & TypeFlags.Never) { return declaredType; } - return result; + return resultType; function getTypeAtFlowNode(flow: FlowNode): FlowType { while (true) { @@ -8530,6 +8676,13 @@ namespace ts { getTypeAtFlowBranchLabel(flow) : getTypeAtFlowLoopLabel(flow); } + else if (flow.flags & FlowFlags.ArrayMutation) { + type = getTypeAtFlowArrayMutation(flow); + if (!type) { + flow = (flow).antecedent; + continue; + } + } else if (flow.flags & FlowFlags.Start) { // Check if we should continue with the control flow of the containing function. const container = (flow).container; @@ -8542,8 +8695,8 @@ namespace ts { } else { // Unreachable code errors are reported in the binding phase. Here we - // simply return the declared type to reduce follow-on errors. - type = declaredType; + // simply return the non-auto declared type to reduce follow-on errors. + type = convertAutoToAny(declaredType); } if (flow.flags & FlowFlags.Shared) { // Record visited node and the associated type in the cache. @@ -8564,9 +8717,17 @@ namespace ts { const flowType = getTypeAtFlowNode(flow.antecedent); return createFlowType(getBaseTypeOfLiteralType(getTypeFromFlowType(flowType)), isIncomplete(flowType)); } - return declaredType === autoType ? getBaseTypeOfLiteralType(getInitialOrAssignedType(node)) : - declaredType.flags & TypeFlags.Union ? getAssignmentReducedType(declaredType, getInitialOrAssignedType(node)) : - declaredType; + if (declaredType === autoType || declaredType === autoArrayType) { + if (isEmptyArrayAssignment(node)) { + return getEvolvingArrayType(neverType); + } + const assignedType = getBaseTypeOfLiteralType(getInitialOrAssignedType(node)); + return isTypeAssignableTo(assignedType, declaredType) ? assignedType : anyArrayType; + } + if (declaredType.flags & TypeFlags.Union) { + return getAssignmentReducedType(declaredType, getInitialOrAssignedType(node)); + } + return declaredType; } // We didn't have a direct match. However, if the reference is a dotted name, this // may be an assignment to a left hand part of the reference. For example, for a @@ -8579,24 +8740,56 @@ namespace ts { return undefined; } - function getTypeAtFlowCondition(flow: FlowCondition): FlowType { - const flowType = getTypeAtFlowNode(flow.antecedent); - let type = getTypeFromFlowType(flowType); - if (!(type.flags & TypeFlags.Never)) { - // If we have an antecedent type (meaning we're reachable in some way), we first - // attempt to narrow the antecedent type. If that produces the never type, and if - // the antecedent type is incomplete (i.e. a transient type in a loop), then we - // take the type guard as an indication that control *could* reach here once we - // have the complete type. We proceed by switching to the silent never type which - // doesn't report errors when operators are applied to it. Note that this is the - // *only* place a silent never type is ever generated. - const assumeTrue = (flow.flags & FlowFlags.TrueCondition) !== 0; - type = narrowType(type, flow.expression, assumeTrue); - if (type.flags & TypeFlags.Never && isIncomplete(flowType)) { - type = silentNeverType; + function getTypeAtFlowArrayMutation(flow: FlowArrayMutation): FlowType { + const node = flow.node; + const expr = node.kind === SyntaxKind.CallExpression ? + ((node).expression).expression : + ((node).left).expression; + if (isMatchingReference(reference, getReferenceCandidate(expr))) { + const flowType = getTypeAtFlowNode(flow.antecedent); + const type = getTypeFromFlowType(flowType); + if (isEvolvingArrayType(type)) { + let evolvedType = type; + if (node.kind === SyntaxKind.CallExpression) { + for (const arg of (node).arguments) { + evolvedType = addEvolvingArrayElementType(evolvedType, arg); + } + } + else { + const indexType = checkExpression(((node).left).argumentExpression); + if (isTypeAnyOrAllConstituentTypesHaveKind(indexType, TypeFlags.NumberLike | TypeFlags.Undefined)) { + evolvedType = addEvolvingArrayElementType(evolvedType, (node).right); + } + } + return evolvedType === type ? flowType : createFlowType(evolvedType, isIncomplete(flowType)); } + return flowType; } - return createFlowType(type, isIncomplete(flowType)); + return undefined; + } + + function getTypeAtFlowCondition(flow: FlowCondition): FlowType { + const flowType = getTypeAtFlowNode(flow.antecedent); + const type = getTypeFromFlowType(flowType); + if (type.flags & TypeFlags.Never) { + return flowType; + } + // If we have an antecedent type (meaning we're reachable in some way), we first + // attempt to narrow the antecedent type. If that produces the never type, and if + // the antecedent type is incomplete (i.e. a transient type in a loop), then we + // take the type guard as an indication that control *could* reach here once we + // have the complete type. We proceed by switching to the silent never type which + // doesn't report errors when operators are applied to it. Note that this is the + // *only* place a silent never type is ever generated. + const assumeTrue = (flow.flags & FlowFlags.TrueCondition) !== 0; + const nonEvolvingType = finalizeEvolvingArrayType(type); + const narrowedType = narrowType(nonEvolvingType, flow.expression, assumeTrue); + if (narrowedType === nonEvolvingType) { + return flowType; + } + const incomplete = isIncomplete(flowType); + const resultType = incomplete && narrowedType.flags & TypeFlags.Never ? silentNeverType : narrowedType; + return createFlowType(resultType, incomplete); } function getTypeAtSwitchClause(flow: FlowSwitchClause): FlowType { @@ -8639,7 +8832,7 @@ namespace ts { seenIncomplete = true; } } - return createFlowType(getUnionType(antecedentTypes, subtypeReduction), seenIncomplete); + return createFlowType(getUnionOrEvolvingArrayType(antecedentTypes, subtypeReduction), seenIncomplete); } function getTypeAtFlowLoopLabel(flow: FlowLabel): FlowType { @@ -8656,11 +8849,15 @@ namespace ts { } // If this flow loop junction and reference are already being processed, return // the union of the types computed for each branch so far, marked as incomplete. - // We should never see an empty array here because the first antecedent of a loop - // junction is always the non-looping control flow path that leads to the top. + // It is possible to see an empty array in cases where loops are nested and the + // back edge of the outer loop reaches an inner loop that is already being analyzed. + // In such cases we restart the analysis of the inner loop, which will then see + // a non-empty in-process array for the outer loop and eventually terminate because + // the first antecedent of a loop junction is always the non-looping control flow + // path that leads to the top. for (let i = flowLoopStart; i < flowLoopCount; i++) { - if (flowLoopNodes[i] === flow && flowLoopKeys[i] === key) { - return createFlowType(getUnionType(flowLoopTypes[i]), /*incomplete*/ true); + if (flowLoopNodes[i] === flow && flowLoopKeys[i] === key && flowLoopTypes[i].length) { + return createFlowType(getUnionOrEvolvingArrayType(flowLoopTypes[i], /*subtypeReduction*/ false), /*incomplete*/ true); } } // Add the flow loop junction and reference to the in-process stack and analyze @@ -8704,7 +8901,7 @@ namespace ts { } // The result is incomplete if the first antecedent (the non-looping control flow path) // is incomplete. - const result = getUnionType(antecedentTypes, subtypeReduction); + const result = getUnionOrEvolvingArrayType(antecedentTypes, subtypeReduction); if (isIncomplete(firstAntecedentType)) { return createFlowType(result, /*incomplete*/ true); } @@ -8804,7 +9001,7 @@ namespace ts { } if (assumeTrue) { const narrowedType = filterType(type, t => areTypesComparable(t, valueType)); - return narrowedType.flags & TypeFlags.Never ? type : narrowedType; + return narrowedType.flags & TypeFlags.Never ? type : replacePrimitivesWithLiterals(narrowedType, valueType); } if (isUnitType(valueType)) { const regularType = getRegularTypeOfLiteralType(valueType); @@ -8851,7 +9048,9 @@ namespace ts { const clauseTypes = switchTypes.slice(clauseStart, clauseEnd); const hasDefaultClause = clauseStart === clauseEnd || contains(clauseTypes, neverType); const discriminantType = getUnionType(clauseTypes); - const caseType = discriminantType.flags & TypeFlags.Never ? neverType : filterType(type, t => isTypeComparableTo(discriminantType, t)); + const caseType = + discriminantType.flags & TypeFlags.Never ? neverType : + replacePrimitivesWithLiterals(filterType(type, t => isTypeComparableTo(discriminantType, t)), discriminantType); if (!hasDefaultClause) { return caseType; } @@ -9084,6 +9283,10 @@ namespace ts { } } + function isConstVariable(symbol: Symbol) { + return symbol.flags & SymbolFlags.Variable && (getDeclarationNodeFlagsFromSymbol(symbol) & NodeFlags.Const) !== 0 && getTypeOfSymbol(symbol) !== autoArrayType; + } + function checkIdentifier(node: Identifier): Type { const symbol = getResolvedSymbol(node); @@ -9095,7 +9298,7 @@ namespace ts { // can explicitly bound arguments objects if (symbol === argumentsSymbol) { const container = getContainingFunction(node); - if (languageVersion < ScriptTarget.ES6) { + if (languageVersion < ScriptTarget.ES2015) { if (container.kind === SyntaxKind.ArrowFunction) { error(node, Diagnostics.The_arguments_object_cannot_be_referenced_in_an_arrow_function_in_ES3_and_ES5_Consider_using_a_standard_function_expression); } @@ -9120,7 +9323,7 @@ namespace ts { // Due to the emit for class decorators, any reference to the class from inside of the class body // must instead be rewritten to point to a temporary variable to avoid issues with the double-bind // behavior of class names in ES6. - if (languageVersion === ScriptTarget.ES6 + if (languageVersion === ScriptTarget.ES2015 && declaration.kind === SyntaxKind.ClassDeclaration && nodeIsDecorated(declaration)) { let container = getContainingClass(node); @@ -9174,28 +9377,28 @@ namespace ts { // When the control flow originates in a function expression or arrow function and we are referencing // a const variable or parameter from an outer function, we extend the origin of the control flow // analysis to include the immediately enclosing function. - while (flowContainer !== declarationContainer && - (flowContainer.kind === SyntaxKind.FunctionExpression || flowContainer.kind === SyntaxKind.ArrowFunction) && - (isReadonlySymbol(localOrExportSymbol) || isParameter && !isParameterAssigned(localOrExportSymbol))) { + while (flowContainer !== declarationContainer && (flowContainer.kind === SyntaxKind.FunctionExpression || + flowContainer.kind === SyntaxKind.ArrowFunction || isObjectLiteralOrClassExpressionMethod(flowContainer)) && + (isConstVariable(localOrExportSymbol) || isParameter && !isParameterAssigned(localOrExportSymbol))) { flowContainer = getControlFlowContainer(flowContainer); } // We only look for uninitialized variables in strict null checking mode, and only when we can analyze // the entire control flow graph from the variable's declaration (i.e. when the flow container and // declaration container are the same). const assumeInitialized = isParameter || isOuterVariable || - type !== autoType && (!strictNullChecks || (type.flags & TypeFlags.Any) !== 0) || + type !== autoType && type !== autoArrayType && (!strictNullChecks || (type.flags & TypeFlags.Any) !== 0) || isInAmbientContext(declaration); const flowType = getFlowTypeOfReference(node, type, assumeInitialized, flowContainer); // A variable is considered uninitialized when it is possible to analyze the entire control flow graph // from declaration to use, and when the variable's declared type doesn't include undefined but the // control flow based type does include undefined. - if (type === autoType) { - if (flowType === autoType) { + if (type === autoType || type === autoArrayType) { + if (flowType === autoType || flowType === autoArrayType) { if (compilerOptions.noImplicitAny) { - error(declaration.name, Diagnostics.Variable_0_implicitly_has_type_any_in_some_locations_where_its_type_cannot_be_determined, symbolToString(symbol)); - error(node, Diagnostics.Variable_0_implicitly_has_an_1_type, symbolToString(symbol), typeToString(anyType)); + error(declaration.name, Diagnostics.Variable_0_implicitly_has_type_1_in_some_locations_where_its_type_cannot_be_determined, symbolToString(symbol), typeToString(flowType)); + error(node, Diagnostics.Variable_0_implicitly_has_an_1_type, symbolToString(symbol), typeToString(flowType)); } - return anyType; + return convertAutoToAny(flowType); } } else if (!assumeInitialized && !(getFalsyFlags(type) & TypeFlags.Undefined) && getFalsyFlags(flowType) & TypeFlags.Undefined) { @@ -9219,7 +9422,7 @@ namespace ts { } function checkNestedBlockScopedBinding(node: Identifier, symbol: Symbol): void { - if (languageVersion >= ScriptTarget.ES6 || + if (languageVersion >= ScriptTarget.ES2015 || (symbol.flags & (SymbolFlags.BlockScopedVariable | SymbolFlags.Class)) === 0 || symbol.valueDeclaration.parent.kind === SyntaxKind.CatchClause) { return; @@ -9385,7 +9588,7 @@ namespace ts { container = getThisContainer(container, /* includeArrowFunctions */ false); // When targeting es6, arrow function lexically bind "this" so we do not need to do the work of binding "this" in emitted code - needToCaptureLexicalThis = (languageVersion < ScriptTarget.ES6); + needToCaptureLexicalThis = (languageVersion < ScriptTarget.ES2015); } switch (container.kind) { @@ -9493,7 +9696,7 @@ namespace ts { if (!isCallExpression) { while (container && container.kind === SyntaxKind.ArrowFunction) { container = getSuperContainer(container, /*stopOnFunctions*/ true); - needToCaptureLexicalThis = languageVersion < ScriptTarget.ES6; + needToCaptureLexicalThis = languageVersion < ScriptTarget.ES2015; } } @@ -9607,7 +9810,7 @@ namespace ts { } if (container.parent.kind === SyntaxKind.ObjectLiteralExpression) { - if (languageVersion < ScriptTarget.ES6) { + if (languageVersion < ScriptTarget.ES2015) { error(node, Diagnostics.super_is_only_allowed_in_members_of_object_literal_expressions_when_option_target_is_ES2015_or_higher); return unknownType; } @@ -9975,7 +10178,7 @@ namespace ts { const index = indexOf(arrayLiteral.elements, node); return getTypeOfPropertyOfContextualType(type, "" + index) || getIndexTypeOfContextualType(type, IndexKind.Number) - || (languageVersion >= ScriptTarget.ES6 ? getElementTypeOfIterable(type, /*errorNode*/ undefined) : undefined); + || (languageVersion >= ScriptTarget.ES2015 ? getElementTypeOfIterable(type, /*errorNode*/ undefined) : undefined); } return undefined; } @@ -10208,7 +10411,7 @@ namespace ts { // if there is no index type / iterated type. const restArrayType = checkExpression((e).expression, contextualMapper); const restElementType = getIndexTypeOfType(restArrayType, IndexKind.Number) || - (languageVersion >= ScriptTarget.ES6 ? getElementTypeOfIterable(restArrayType, /*errorNode*/ undefined) : undefined); + (languageVersion >= ScriptTarget.ES2015 ? getElementTypeOfIterable(restArrayType, /*errorNode*/ undefined) : undefined); if (restElementType) { elementTypes.push(restElementType); } @@ -10640,7 +10843,7 @@ namespace ts { } } - return getUnionType(signatures.map(getReturnTypeOfSignature), /*subtypeReduction*/ true); + return getUnionType(map(signatures, getReturnTypeOfSignature), /*subtypeReduction*/ true); } /// e.g. "props" for React.d.ts, @@ -10690,7 +10893,7 @@ namespace ts { } if (elemType.flags & TypeFlags.Union) { const types = (elemType).types; - return getUnionType(types.map(type => { + return getUnionType(map(types, type => { return getResolvedJsxType(node, type, elemClassType); }), /*subtypeReduction*/ true); } @@ -10956,7 +11159,7 @@ namespace ts { // - In a static member function or static member accessor // where this references the constructor function object of a derived class, // a super property access is permitted and must specify a public static member function of the base class. - if (languageVersion < ScriptTarget.ES6 && getDeclarationKindFromSymbol(prop) !== SyntaxKind.MethodDeclaration) { + if (languageVersion < ScriptTarget.ES2015 && getDeclarationKindFromSymbol(prop) !== SyntaxKind.MethodDeclaration) { // `prop` refers to a *property* declared in the super class // rather than a *method*, so it does not satisfy the above criteria. @@ -14278,7 +14481,7 @@ namespace ts { } if (node.type) { - if (languageVersion >= ScriptTarget.ES6 && isSyntacticallyValidGenerator(node)) { + if (languageVersion >= ScriptTarget.ES2015 && isSyntacticallyValidGenerator(node)) { const returnType = getTypeFromTypeNode(node.type); if (returnType === voidType) { error(node.type, Diagnostics.A_generator_cannot_have_a_void_type_annotation); @@ -15239,7 +15442,7 @@ namespace ts { * callable `then` signature. */ function checkAsyncFunctionReturnType(node: FunctionLikeDeclaration): Type { - if (languageVersion >= ScriptTarget.ES6) { + if (languageVersion >= ScriptTarget.ES2015) { const returnType = getTypeFromTypeNode(node.type); return checkCorrectPromiseType(returnType, node.type, Diagnostics.The_return_type_of_an_async_function_or_method_must_be_the_global_Promise_T_type); } @@ -15766,7 +15969,7 @@ namespace ts { function checkCollisionWithRequireExportsInGeneratedCode(node: Node, name: Identifier) { // No need to check for require or exports for ES6 modules and later - if (modulekind >= ModuleKind.ES6) { + if (modulekind >= ModuleKind.ES2015) { return; } @@ -15947,7 +16150,7 @@ namespace ts { } function convertAutoToAny(type: Type) { - return type === autoType ? anyType : type; + return type === autoType ? anyType : type === autoArrayType ? anyArrayType : type; } // Check variable, parameter, or property declaration @@ -16262,7 +16465,7 @@ namespace ts { if (isTypeAny(inputType)) { return inputType; } - if (languageVersion >= ScriptTarget.ES6) { + if (languageVersion >= ScriptTarget.ES2015) { return checkElementTypeOfIterable(inputType, errorNode); } if (allowStringInput) { @@ -16440,7 +16643,7 @@ namespace ts { * 2. Some constituent is a string and target is less than ES5 (because in ES3 string is not indexable). */ function checkElementTypeOfArrayOrString(arrayOrStringType: Type, errorNode: Node): Type { - Debug.assert(languageVersion < ScriptTarget.ES6); + Debug.assert(languageVersion < ScriptTarget.ES2015); // After we remove all types that are StringLike, we will know if there was a string constituent // based on whether the remaining type is the same as the initial type. @@ -17750,7 +17953,7 @@ namespace ts { } } else { - if (modulekind === ModuleKind.ES6 && !isInAmbientContext(node)) { + if (modulekind === ModuleKind.ES2015 && !isInAmbientContext(node)) { // Import equals declaration is deprecated in es6 or above grammarErrorOnNode(node, Diagnostics.Import_assignment_cannot_be_used_when_targeting_ECMAScript_2015_modules_Consider_using_import_Asterisk_as_ns_from_mod_import_a_from_mod_import_d_from_mod_or_another_module_format_instead); } @@ -17838,7 +18041,7 @@ namespace ts { checkExternalModuleExports(container); if (node.isExportEquals && !isInAmbientContext(node)) { - if (modulekind === ModuleKind.ES6) { + if (modulekind === ModuleKind.ES2015) { // export assignment is not supported in es6 modules grammarErrorOnNode(node, Diagnostics.Export_assignment_cannot_be_used_when_targeting_ECMAScript_2015_modules_Consider_using_export_default_or_another_module_format_instead); } @@ -17866,7 +18069,7 @@ namespace ts { } // Checks for export * conflicts const exports = getExportsOfModule(moduleSymbol); - exports.forEach(({ declarations, flags }, id) => { + exports && exports.forEach(({ declarations, flags }, id) => { if (id === "__export") { return; } @@ -17893,7 +18096,8 @@ namespace ts { } function isNotOverload(declaration: Declaration): boolean { - return declaration.kind !== SyntaxKind.FunctionDeclaration || !!(declaration as FunctionDeclaration).body; + return (declaration.kind !== SyntaxKind.FunctionDeclaration && declaration.kind !== SyntaxKind.MethodDeclaration) || + !!(declaration as FunctionDeclaration).body; } } @@ -19343,7 +19547,7 @@ namespace ts { getGlobalTemplateStringsArrayType = memoize(() => getGlobalType("TemplateStringsArray")); - if (languageVersion >= ScriptTarget.ES6) { + if (languageVersion >= ScriptTarget.ES2015) { getGlobalESSymbolType = memoize(() => getGlobalType("Symbol")); getGlobalIterableType = memoize(() => getGlobalType("Iterable", /*arity*/ 1)); getGlobalIteratorType = memoize(() => getGlobalType("Iterator", /*arity*/ 1)); @@ -19357,6 +19561,7 @@ namespace ts { } anyArrayType = createArrayType(anyType); + autoArrayType = createArrayType(autoType); const symbol = getGlobalSymbol("ReadonlyArray", SymbolFlags.Type, /*diagnostic*/ undefined); globalReadonlyArrayType = symbol && getTypeOfGlobalSymbol(symbol, /*arity*/ 1); @@ -19376,7 +19581,7 @@ namespace ts { // If we found the module, report errors if it does not have the necessary exports. if (helpersModule) { const exports = helpersModule.exports; - if (requestedExternalEmitHelpers & NodeFlags.HasClassExtends && languageVersion < ScriptTarget.ES6) { + if (requestedExternalEmitHelpers & NodeFlags.HasClassExtends && languageVersion < ScriptTarget.ES2015) { verifyHelperSymbol(exports, "__extends", SymbolFlags.Value); } if (requestedExternalEmitHelpers & NodeFlags.HasJsxSpreadAttributes && compilerOptions.jsx !== JsxEmit.Preserve) { @@ -19393,7 +19598,7 @@ namespace ts { } if (requestedExternalEmitHelpers & NodeFlags.HasAsyncFunctions) { verifyHelperSymbol(exports, "__awaiter", SymbolFlags.Value); - if (languageVersion < ScriptTarget.ES6) { + if (languageVersion < ScriptTarget.ES2015) { verifyHelperSymbol(exports, "__generator", SymbolFlags.Value); } } @@ -19970,7 +20175,7 @@ namespace ts { if (!node.body) { return grammarErrorOnNode(node.asteriskToken, Diagnostics.An_overload_signature_cannot_be_declared_as_a_generator); } - if (languageVersion < ScriptTarget.ES6) { + if (languageVersion < ScriptTarget.ES2015) { return grammarErrorOnNode(node.asteriskToken, Diagnostics.Generators_are_only_available_when_targeting_ECMAScript_2015_or_higher); } } diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 6ab346655c4f4..b49cb4ba6d6fe 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -101,7 +101,7 @@ namespace ts { "amd": ModuleKind.AMD, "system": ModuleKind.System, "umd": ModuleKind.UMD, - "es6": ModuleKind.ES6, + "es6": ModuleKind.ES2015, "es2015": ModuleKind.ES2015, }), description: Diagnostics.Specify_module_code_generation_Colon_commonjs_amd_system_umd_or_es2015, @@ -261,8 +261,10 @@ namespace ts { type: mapOfMapLike({ "es3": ScriptTarget.ES3, "es5": ScriptTarget.ES5, - "es6": ScriptTarget.ES6, + "es6": ScriptTarget.ES2015, "es2015": ScriptTarget.ES2015, + "es2016": ScriptTarget.ES2016, + "es2017": ScriptTarget.ES2017, }), description: Diagnostics.Specify_ECMAScript_target_version_Colon_ES3_default_ES5_or_ES2015, paramType: Diagnostics.VERSION, @@ -444,6 +446,11 @@ namespace ts { name: "importHelpers", type: "boolean", description: Diagnostics.Import_emit_helpers_from_tslib + }, + { + name: "alwaysStrict", + type: "boolean", + description: Diagnostics.Parse_in_strict_mode_and_emit_use_strict_for_each_source_file } ]; diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 8c06856d1b2bd..f2b761f94c053 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -19,6 +19,9 @@ namespace ts { True = -1 } + // More efficient to create a collator once and use its `compare` than to call `a.localeCompare(b)` many times. + export const collator: { compare(a: string, b: string): number } = typeof Intl === "object" && typeof Intl.Collator === "function" ? new Intl.Collator() : undefined; + export function createFileMap(keyMapper?: (key: string) => string): FileMap { const files = new StringMap(); return { @@ -240,13 +243,33 @@ namespace ts { if (array) { result = []; for (let i = 0; i < array.length; i++) { - const v = array[i]; - result.push(f(v, i)); + result.push(f(array[i], i)); } } return result; } + // Maps from T to T and avoids allocation if all elements map to themselves + export function sameMap(array: T[], f: (x: T, i: number) => T): T[] { + let result: T[]; + if (array) { + for (let i = 0; i < array.length; i++) { + if (result) { + result.push(f(array[i], i)); + } + else { + const item = array[i]; + const mapped = f(item, i); + if (item !== mapped) { + result = array.slice(0, i); + result.push(mapped); + } + } + } + } + return result || array; + } + /** * Flattens an array containing a mix of array or non-array elements. * @@ -374,6 +397,17 @@ namespace ts { return result; } + export function some(array: T[], predicate?: (value: T) => boolean): boolean { + if (array) { + for (const v of array) { + if (!predicate || predicate(v)) { + return true; + } + } + } + return false; + } + export function concatenate(array1: T[], array2: T[]): T[] { if (!array2 || !array2.length) return array1; if (!array1 || !array1.length) return array2; @@ -755,7 +789,8 @@ namespace ts { if (a === undefined) return Comparison.LessThan; if (b === undefined) return Comparison.GreaterThan; if (ignoreCase) { - if (String.prototype.localeCompare) { + if (collator && String.prototype.localeCompare) { + // accent means a ≠ b, a ≠ á, a = A const result = a.localeCompare(b, /*locales*/ undefined, { usage: "sort", sensitivity: "accent" }); return result < 0 ? Comparison.LessThan : result > 0 ? Comparison.GreaterThan : Comparison.EqualTo; } @@ -942,7 +977,7 @@ namespace ts { export function getEmitModuleKind(compilerOptions: CompilerOptions) { return typeof compilerOptions.module === "number" ? compilerOptions.module : - getEmitScriptTarget(compilerOptions) === ScriptTarget.ES6 ? ModuleKind.ES6 : ModuleKind.CommonJS; + getEmitScriptTarget(compilerOptions) >= ScriptTarget.ES2015 ? ModuleKind.ES2015 : ModuleKind.CommonJS; } /* @internal */ diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 1b8c995ea057f..21f04da488d9b 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1923,7 +1923,7 @@ "category": "Error", "code": 2685 }, - "Identifier '{0}' must be imported from a module": { + "'{0}' refers to a UMD global, but the current file is a module. Consider adding an import instead.": { "category": "Error", "code": 2686 }, @@ -2861,6 +2861,10 @@ "category": "Error", "code": 6140 }, + "Parse in strict mode and emit \"use strict\" for each source file": { + "category": "Message", + "code": 6141 + }, "Variable '{0}' implicitly has an '{1}' type.": { "category": "Error", "code": 7005 @@ -2957,7 +2961,7 @@ "category": "Error", "code": 7033 }, - "Variable '{0}' implicitly has type 'any' in some locations where its type cannot be determined.": { + "Variable '{0}' implicitly has type '{1}' in some locations where its type cannot be determined.": { "category": "Error", "code": 7034 }, diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 75cb889e1bd4c..4e4f78eadbffa 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -504,15 +504,29 @@ const _super = (function (geti, seti) { // Contextual keywords case SyntaxKind.AbstractKeyword: + case SyntaxKind.AsKeyword: case SyntaxKind.AnyKeyword: case SyntaxKind.AsyncKeyword: + case SyntaxKind.AwaitKeyword: case SyntaxKind.BooleanKeyword: + case SyntaxKind.ConstructorKeyword: case SyntaxKind.DeclareKeyword: - case SyntaxKind.NumberKeyword: + case SyntaxKind.GetKeyword: + case SyntaxKind.IsKeyword: + case SyntaxKind.ModuleKeyword: + case SyntaxKind.NamespaceKeyword: + case SyntaxKind.NeverKeyword: case SyntaxKind.ReadonlyKeyword: + case SyntaxKind.RequireKeyword: + case SyntaxKind.NumberKeyword: + case SyntaxKind.SetKeyword: case SyntaxKind.StringKeyword: case SyntaxKind.SymbolKeyword: + case SyntaxKind.TypeKeyword: + case SyntaxKind.UndefinedKeyword: + case SyntaxKind.FromKeyword: case SyntaxKind.GlobalKeyword: + case SyntaxKind.OfKeyword: writeTokenText(kind); return; @@ -1198,12 +1212,14 @@ const _super = (function (geti, seti) { function emitCallExpression(node: CallExpression) { emitExpression(node.expression); + emitTypeArguments(node, node.typeArguments); emitExpressionList(node, node.arguments, ListFormat.CallExpressionArguments); } function emitNewExpression(node: NewExpression) { write("new "); emitExpression(node.expression); + emitTypeArguments(node, node.typeArguments); emitExpressionList(node, node.arguments, ListFormat.NewExpressionArguments); } @@ -1575,6 +1591,7 @@ const _super = (function (geti, seti) { function emitVariableDeclaration(node: VariableDeclaration) { emit(node.name); + emitWithPrefix(": ", node.type); emitExpressionWithPrefix(" = ", node.initializer); } @@ -2165,7 +2182,7 @@ const _super = (function (geti, seti) { // Only Emit __extends function when target ES5. // For target ES6 and above, we can emit classDeclaration as is. - if ((languageVersion < ScriptTarget.ES6) && (!extendsEmitted && node.flags & NodeFlags.HasClassExtends)) { + if ((languageVersion < ScriptTarget.ES2015) && (!extendsEmitted && node.flags & NodeFlags.HasClassExtends)) { writeLines(extendsHelper); extendsEmitted = true; helpersEmitted = true; @@ -2192,9 +2209,12 @@ const _super = (function (geti, seti) { helpersEmitted = true; } - if (!awaiterEmitted && node.flags & NodeFlags.HasAsyncFunctions) { + // Only emit __awaiter function when target ES5/ES6. + // Only emit __generator function when target ES5. + // For target ES2017 and above, we can emit async/await as is. + if ((languageVersion < ScriptTarget.ES2017) && (!awaiterEmitted && node.flags & NodeFlags.HasAsyncFunctions)) { writeLines(awaiterHelper); - if (languageVersion < ScriptTarget.ES6) { + if (languageVersion < ScriptTarget.ES2015) { writeLines(generatorHelper); } diff --git a/src/compiler/factory.ts b/src/compiler/factory.ts index 8fee7ffa95d29..84fd7fd360d2e 100644 --- a/src/compiler/factory.ts +++ b/src/compiler/factory.ts @@ -525,9 +525,9 @@ namespace ts { return node; } - export function createFunctionExpression(asteriskToken: AsteriskToken, name: string | Identifier, typeParameters: TypeParameterDeclaration[], parameters: ParameterDeclaration[], type: TypeNode, body: Block, location?: TextRange, flags?: NodeFlags) { + export function createFunctionExpression(modifiers: Modifier[], asteriskToken: AsteriskToken, name: string | Identifier, typeParameters: TypeParameterDeclaration[], parameters: ParameterDeclaration[], type: TypeNode, body: Block, location?: TextRange, flags?: NodeFlags) { const node = createNode(SyntaxKind.FunctionExpression, location, flags); - node.modifiers = undefined; + node.modifiers = modifiers ? createNodeArray(modifiers) : undefined; node.asteriskToken = asteriskToken; node.name = typeof name === "string" ? createIdentifier(name) : name; node.typeParameters = typeParameters ? createNodeArray(typeParameters) : undefined; @@ -537,9 +537,9 @@ namespace ts { return node; } - export function updateFunctionExpression(node: FunctionExpression, name: Identifier, typeParameters: TypeParameterDeclaration[], parameters: ParameterDeclaration[], type: TypeNode, body: Block) { - if (node.name !== name || node.typeParameters !== typeParameters || node.parameters !== parameters || node.type !== type || node.body !== body) { - return updateNode(createFunctionExpression(node.asteriskToken, name, typeParameters, parameters, type, body, /*location*/ node, node.flags), node); + export function updateFunctionExpression(node: FunctionExpression, modifiers: Modifier[], name: Identifier, typeParameters: TypeParameterDeclaration[], parameters: ParameterDeclaration[], type: TypeNode, body: Block) { + if (node.name !== name || node.modifiers !== modifiers || node.typeParameters !== typeParameters || node.parameters !== parameters || node.type !== type || node.body !== body) { + return updateNode(createFunctionExpression(modifiers, node.asteriskToken, name, typeParameters, parameters, type, body, /*location*/ node, node.flags), node); } return node; } @@ -1736,6 +1736,7 @@ namespace ts { export function createAwaiterHelper(externalHelpersModuleName: Identifier | undefined, hasLexicalArguments: boolean, promiseConstructor: EntityName | Expression, body: Block) { const generatorFunc = createFunctionExpression( + /*modifiers*/ undefined, createToken(SyntaxKind.AsteriskToken), /*name*/ undefined, /*typeParameters*/ undefined, @@ -1909,6 +1910,7 @@ namespace ts { createCall( createParen( createFunctionExpression( + /*modifiers*/ undefined, /*asteriskToken*/ undefined, /*name*/ undefined, /*typeParameters*/ undefined, @@ -1984,7 +1986,7 @@ namespace ts { } else if (callee.kind === SyntaxKind.SuperKeyword) { thisArg = createThis(); - target = languageVersion < ScriptTarget.ES6 ? createIdentifier("_super", /*location*/ callee) : callee; + target = languageVersion < ScriptTarget.ES2015 ? createIdentifier("_super", /*location*/ callee) : callee; } else { switch (callee.kind) { @@ -2090,6 +2092,7 @@ namespace ts { const properties: ObjectLiteralElementLike[] = []; if (getAccessor) { const getterFunction = createFunctionExpression( + getAccessor.modifiers, /*asteriskToken*/ undefined, /*name*/ undefined, /*typeParameters*/ undefined, @@ -2105,6 +2108,7 @@ namespace ts { if (setAccessor) { const setterFunction = createFunctionExpression( + setAccessor.modifiers, /*asteriskToken*/ undefined, /*name*/ undefined, /*typeParameters*/ undefined, @@ -2171,6 +2175,7 @@ namespace ts { createMemberAccessForPropertyName(receiver, method.name, /*location*/ method.name), setOriginalNode( createFunctionExpression( + method.modifiers, method.asteriskToken, /*name*/ undefined, /*typeParameters*/ undefined, @@ -2206,7 +2211,7 @@ namespace ts { * @param visitor: Optional callback used to visit any custom prologue directives. */ export function addPrologueDirectives(target: Statement[], source: Statement[], ensureUseStrict?: boolean, visitor?: (node: Node) => VisitResult): number { - Debug.assert(target.length === 0, "PrologueDirectives should be at the first statement in the target statements array"); + Debug.assert(target.length === 0, "Prologue directives should be at the first statement in the target statements array"); let foundUseStrict = false; let statementOffset = 0; const numStatements = source.length; @@ -2219,20 +2224,51 @@ namespace ts { target.push(statement); } else { - if (ensureUseStrict && !foundUseStrict) { - target.push(startOnNewLine(createStatement(createLiteral("use strict")))); + break; + } + statementOffset++; + } + if (ensureUseStrict && !foundUseStrict) { + target.push(startOnNewLine(createStatement(createLiteral("use strict")))); + } + while (statementOffset < numStatements) { + const statement = source[statementOffset]; + if (getEmitFlags(statement) & EmitFlags.CustomPrologue) { + target.push(visitor ? visitNode(statement, visitor, isStatement) : statement); + } + else { + break; + } + statementOffset++; + } + return statementOffset; + } + + /** + * Ensures "use strict" directive is added + * + * @param node source file + */ + export function ensureUseStrict(node: SourceFile): SourceFile { + let foundUseStrict = false; + for (const statement of node.statements) { + if (isPrologueDirective(statement)) { + if (isUseStrictPrologue(statement as ExpressionStatement)) { foundUseStrict = true; - } - if (getEmitFlags(statement) & EmitFlags.CustomPrologue) { - target.push(visitor ? visitNode(statement, visitor, isStatement) : statement); - } - else { break; } } - statementOffset++; + else { + break; + } } - return statementOffset; + if (!foundUseStrict) { + const statements: Statement[] = []; + statements.push(startOnNewLine(createStatement(createLiteral("use strict")))); + // add "use strict" as the first statement + return updateSourceFileNode(node, statements.concat(node.statements)); + } + return node; } /** @@ -2908,4 +2944,4 @@ namespace ts { function tryGetModuleNameFromDeclaration(declaration: ImportEqualsDeclaration | ImportDeclaration | ExportDeclaration, host: EmitHost, resolver: EmitResolver, compilerOptions: CompilerOptions) { return tryGetModuleNameFromFile(resolver.getExternalModuleFileFromDeclaration(declaration), host, compilerOptions); } -} \ No newline at end of file +} diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 14a602684e61a..b43a107dd4d75 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1469,12 +1469,16 @@ namespace ts { programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Option_0_cannot_be_specified_with_option_1, "lib", "noLib")); } + if (options.noImplicitUseStrict && options.alwaysStrict) { + programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Option_0_cannot_be_specified_with_option_1, "noImplicitUseStrict", "alwaysStrict")); + } + const languageVersion = options.target || ScriptTarget.ES3; const outFile = options.outFile || options.out; const firstNonAmbientExternalModuleSourceFile = forEach(files, f => isExternalModule(f) && !isDeclarationFile(f) ? f : undefined); if (options.isolatedModules) { - if (options.module === ModuleKind.None && languageVersion < ScriptTarget.ES6) { + if (options.module === ModuleKind.None && languageVersion < ScriptTarget.ES2015) { programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Option_isolatedModules_can_only_be_used_when_either_option_module_is_provided_or_option_target_is_ES2015_or_higher)); } @@ -1484,7 +1488,7 @@ namespace ts { programDiagnostics.add(createFileDiagnostic(firstNonExternalModuleSourceFile, span.start, span.length, Diagnostics.Cannot_compile_namespaces_when_the_isolatedModules_flag_is_provided)); } } - else if (firstNonAmbientExternalModuleSourceFile && languageVersion < ScriptTarget.ES6 && options.module === ModuleKind.None) { + else if (firstNonAmbientExternalModuleSourceFile && languageVersion < ScriptTarget.ES2015 && options.module === ModuleKind.None) { // We cannot use createDiagnosticFromNode because nodes do not have parents yet const span = getErrorSpanForNode(firstNonAmbientExternalModuleSourceFile, firstNonAmbientExternalModuleSourceFile.externalModuleIndicator); programDiagnostics.add(createFileDiagnostic(firstNonAmbientExternalModuleSourceFile, span.start, span.length, Diagnostics.Cannot_use_imports_exports_or_module_augmentations_when_module_is_none)); diff --git a/src/compiler/scanner.ts b/src/compiler/scanner.ts index ccf3fa38950d5..289fc0ee84eff 100644 --- a/src/compiler/scanner.ts +++ b/src/compiler/scanner.ts @@ -1191,7 +1191,7 @@ namespace ts { } function scanBinaryOrOctalDigits(base: number): number { - Debug.assert(base !== 2 || base !== 8, "Expected either base 2 or base 8"); + Debug.assert(base === 2 || base === 8, "Expected either base 2 or base 8"); let value = 0; // For counting number of digits; Valid binaryIntegerLiteral must have at least one binary digit following B or b. diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index 4ec88f55736fd..376c3ed70b0d0 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -479,6 +479,10 @@ namespace ts { // Node 4.0 `fs.watch` function supports the "recursive" option on both OSX and Windows // (ref: https://github.com/nodejs/node/pull/2649 and https://github.com/Microsoft/TypeScript/issues/4643) let options: any; + if (!directoryExists(directoryName)) { + return; + } + if (isNode4OrLater() && (process.platform === "win32" || process.platform === "darwin")) { options = { persistent: true, recursive: !!recursive }; } diff --git a/src/compiler/transformer.ts b/src/compiler/transformer.ts index e341fd6f69f04..63e8956f53f15 100644 --- a/src/compiler/transformer.ts +++ b/src/compiler/transformer.ts @@ -1,17 +1,18 @@ /// /// /// -/// -/// +/// +/// +/// /// /// /// -/// +/// /* @internal */ namespace ts { const moduleTransformerMap = new NumberMap([ - [ModuleKind.ES6, transformES6Module], + [ModuleKind.ES2015, transformES2015Module], [ModuleKind.System, transformSystemModule], [ModuleKind.AMD, transformModule], [ModuleKind.CommonJS, transformModule], @@ -115,10 +116,16 @@ namespace ts { transformers.push(transformJsx); } - transformers.push(transformES7); + if (languageVersion < ScriptTarget.ES2017) { + transformers.push(transformES2017); + } + + if (languageVersion < ScriptTarget.ES2016) { + transformers.push(transformES2016); + } - if (languageVersion < ScriptTarget.ES6) { - transformers.push(transformES6); + if (languageVersion < ScriptTarget.ES2015) { + transformers.push(transformES2015); transformers.push(transformGenerators); } diff --git a/src/compiler/transformers/es6.ts b/src/compiler/transformers/es2015.ts similarity index 97% rename from src/compiler/transformers/es6.ts rename to src/compiler/transformers/es2015.ts index 3b4b50bbca3a2..27e5fbb44ba49 100644 --- a/src/compiler/transformers/es6.ts +++ b/src/compiler/transformers/es2015.ts @@ -4,7 +4,7 @@ /*@internal*/ namespace ts { - const enum ES6SubstitutionFlags { + const enum ES2015SubstitutionFlags { /** Enables substitutions for captured `this` */ CapturedThis = 1 << 0, /** Enables substitutions for block-scoped bindings. */ @@ -163,7 +163,7 @@ namespace ts { ReplaceWithReturn, } - export function transformES6(context: TransformationContext) { + export function transformES2015(context: TransformationContext) { const { startLexicalEnvironment, endLexicalEnvironment, @@ -197,7 +197,7 @@ namespace ts { * They are persisted between each SourceFile transformation and should not * be reset. */ - let enabledSubstitutions: ES6SubstitutionFlags; + let enabledSubstitutions: ES2015SubstitutionFlags; return transformSourceFile; @@ -252,7 +252,7 @@ namespace ts { } function shouldCheckNode(node: Node): boolean { - return (node.transformFlags & TransformFlags.ES6) !== 0 || + return (node.transformFlags & TransformFlags.ES2015) !== 0 || node.kind === SyntaxKind.LabeledStatement || (isIterationStatement(node, /*lookInLabeledStatements*/ false) && shouldConvertIterationStatementBody(node)); } @@ -261,7 +261,7 @@ namespace ts { if (shouldCheckNode(node)) { return visitJavaScript(node); } - else if (node.transformFlags & TransformFlags.ContainsES6) { + else if (node.transformFlags & TransformFlags.ContainsES2015) { return visitEachChild(node, visitor, context); } else { @@ -681,6 +681,7 @@ namespace ts { const extendsClauseElement = getClassExtendsHeritageClauseElement(node); const classFunction = createFunctionExpression( + /*modifiers*/ undefined, /*asteriskToken*/ undefined, /*name*/ undefined, /*typeParameters*/ undefined, @@ -1416,6 +1417,7 @@ namespace ts { if (getAccessor) { const getterFunction = transformFunctionLikeToExpression(getAccessor, /*location*/ undefined, /*name*/ undefined); setSourceMapRange(getterFunction, getSourceMapRange(getAccessor)); + setEmitFlags(getterFunction, EmitFlags.NoLeadingComments); const getter = createPropertyAssignment("get", getterFunction); setCommentRange(getter, getCommentRange(getAccessor)); properties.push(getter); @@ -1424,6 +1426,7 @@ namespace ts { if (setAccessor) { const setterFunction = transformFunctionLikeToExpression(setAccessor, /*location*/ undefined, /*name*/ undefined); setSourceMapRange(setterFunction, getSourceMapRange(setAccessor)); + setEmitFlags(setterFunction, EmitFlags.NoLeadingComments); const setter = createPropertyAssignment("set", setterFunction); setCommentRange(setter, getCommentRange(setAccessor)); properties.push(setter); @@ -1509,6 +1512,7 @@ namespace ts { const expression = setOriginalNode( createFunctionExpression( + /*modifiers*/ undefined, node.asteriskToken, name, /*typeParameters*/ undefined, @@ -2240,6 +2244,7 @@ namespace ts { /*type*/ undefined, setEmitFlags( createFunctionExpression( + /*modifiers*/ undefined, isAsyncBlockContainingAwait ? createToken(SyntaxKind.AsteriskToken) : undefined, /*name*/ undefined, /*typeParameters*/ undefined, @@ -3033,7 +3038,7 @@ namespace ts { function onEmitNode(emitContext: EmitContext, node: Node, emitCallback: (emitContext: EmitContext, node: Node) => void) { const savedEnclosingFunction = enclosingFunction; - if (enabledSubstitutions & ES6SubstitutionFlags.CapturedThis && isFunctionLike(node)) { + if (enabledSubstitutions & ES2015SubstitutionFlags.CapturedThis && isFunctionLike(node)) { // If we are tracking a captured `this`, keep track of the enclosing function. enclosingFunction = node; } @@ -3048,8 +3053,8 @@ namespace ts { * contains block-scoped bindings (e.g. `let` or `const`). */ function enableSubstitutionsForBlockScopedBindings() { - if ((enabledSubstitutions & ES6SubstitutionFlags.BlockScopedBindings) === 0) { - enabledSubstitutions |= ES6SubstitutionFlags.BlockScopedBindings; + if ((enabledSubstitutions & ES2015SubstitutionFlags.BlockScopedBindings) === 0) { + enabledSubstitutions |= ES2015SubstitutionFlags.BlockScopedBindings; context.enableSubstitution(SyntaxKind.Identifier); } } @@ -3059,8 +3064,8 @@ namespace ts { * contains a captured `this`. */ function enableSubstitutionsForCapturedThis() { - if ((enabledSubstitutions & ES6SubstitutionFlags.CapturedThis) === 0) { - enabledSubstitutions |= ES6SubstitutionFlags.CapturedThis; + if ((enabledSubstitutions & ES2015SubstitutionFlags.CapturedThis) === 0) { + enabledSubstitutions |= ES2015SubstitutionFlags.CapturedThis; context.enableSubstitution(SyntaxKind.ThisKeyword); context.enableEmitNotification(SyntaxKind.Constructor); context.enableEmitNotification(SyntaxKind.MethodDeclaration); @@ -3099,7 +3104,7 @@ namespace ts { function substituteIdentifier(node: Identifier) { // Only substitute the identifier if we have enabled substitutions for block-scoped // bindings. - if (enabledSubstitutions & ES6SubstitutionFlags.BlockScopedBindings) { + if (enabledSubstitutions & ES2015SubstitutionFlags.BlockScopedBindings) { const original = getParseTreeNode(node, isIdentifier); if (original && isNameOfDeclarationWithCollidingName(original)) { return getGeneratedNameForNode(original); @@ -3152,7 +3157,7 @@ namespace ts { * @param node An Identifier node. */ function substituteExpressionIdentifier(node: Identifier): Identifier { - if (enabledSubstitutions & ES6SubstitutionFlags.BlockScopedBindings) { + if (enabledSubstitutions & ES2015SubstitutionFlags.BlockScopedBindings) { const declaration = resolver.getReferencedDeclarationWithCollidingName(node); if (declaration) { return getGeneratedNameForNode(declaration.name); @@ -3168,7 +3173,7 @@ namespace ts { * @param node The ThisKeyword node. */ function substituteThisKeyword(node: PrimaryExpression): PrimaryExpression { - if (enabledSubstitutions & ES6SubstitutionFlags.CapturedThis + if (enabledSubstitutions & ES2015SubstitutionFlags.CapturedThis && enclosingFunction && getEmitFlags(enclosingFunction) & EmitFlags.CapturesThis) { return createIdentifier("_this", /*location*/ node); @@ -3255,4 +3260,4 @@ namespace ts { return isIdentifier(expression) && expression === parameter.name; } } -} \ No newline at end of file +} diff --git a/src/compiler/transformers/es7.ts b/src/compiler/transformers/es2016.ts similarity index 91% rename from src/compiler/transformers/es7.ts rename to src/compiler/transformers/es2016.ts index 4d5e96134c40d..fba1d3009037b 100644 --- a/src/compiler/transformers/es7.ts +++ b/src/compiler/transformers/es2016.ts @@ -3,7 +3,7 @@ /*@internal*/ namespace ts { - export function transformES7(context: TransformationContext) { + export function transformES2016(context: TransformationContext) { const { hoistVariableDeclaration } = context; return transformSourceFile; @@ -17,10 +17,10 @@ namespace ts { } function visitor(node: Node): VisitResult { - if (node.transformFlags & TransformFlags.ES7) { + if (node.transformFlags & TransformFlags.ES2016) { return visitorWorker(node); } - else if (node.transformFlags & TransformFlags.ContainsES7) { + else if (node.transformFlags & TransformFlags.ContainsES2016) { return visitEachChild(node, visitor, context); } else { @@ -40,7 +40,7 @@ namespace ts { } function visitBinaryExpression(node: BinaryExpression): Expression { - // We are here because ES7 adds support for the exponentiation operator. + // We are here because ES2016 adds support for the exponentiation operator. const left = visitNode(node.left, visitor, isExpression); const right = visitNode(node.right, visitor, isExpression); if (node.operatorToken.kind === SyntaxKind.AsteriskAsteriskEqualsToken) { @@ -98,4 +98,4 @@ namespace ts { } } } -} \ No newline at end of file +} diff --git a/src/compiler/transformers/es2017.ts b/src/compiler/transformers/es2017.ts new file mode 100644 index 0000000000000..7800a41e147a3 --- /dev/null +++ b/src/compiler/transformers/es2017.ts @@ -0,0 +1,510 @@ +/// +/// + +/*@internal*/ +namespace ts { + type SuperContainer = ClassDeclaration | MethodDeclaration | GetAccessorDeclaration | SetAccessorDeclaration | ConstructorDeclaration; + + export function transformES2017(context: TransformationContext) { + + const enum ES2017SubstitutionFlags { + /** Enables substitutions for async methods with `super` calls. */ + AsyncMethodsWithSuper = 1 << 0 + } + + const { + startLexicalEnvironment, + endLexicalEnvironment, + } = context; + + const resolver = context.getEmitResolver(); + const compilerOptions = context.getCompilerOptions(); + const languageVersion = getEmitScriptTarget(compilerOptions); + + // These variables contain state that changes as we descend into the tree. + let currentSourceFileExternalHelpersModuleName: Identifier; + /** + * Keeps track of whether expression substitution has been enabled for specific edge cases. + * They are persisted between each SourceFile transformation and should not be reset. + */ + let enabledSubstitutions: ES2017SubstitutionFlags; + + /** + * Keeps track of whether we are within any containing namespaces when performing + * just-in-time substitution while printing an expression identifier. + */ + let applicableSubstitutions: ES2017SubstitutionFlags; + + /** + * This keeps track of containers where `super` is valid, for use with + * just-in-time substitution for `super` expressions inside of async methods. + */ + let currentSuperContainer: SuperContainer; + + // Save the previous transformation hooks. + const previousOnEmitNode = context.onEmitNode; + const previousOnSubstituteNode = context.onSubstituteNode; + + // Set new transformation hooks. + context.onEmitNode = onEmitNode; + context.onSubstituteNode = onSubstituteNode; + + let currentScope: SourceFile | Block | ModuleBlock | CaseBlock; + + return transformSourceFile; + + function transformSourceFile(node: SourceFile) { + if (isDeclarationFile(node)) { + return node; + } + + currentSourceFileExternalHelpersModuleName = node.externalHelpersModuleName; + + return visitEachChild(node, visitor, context); + } + + function visitor(node: Node): VisitResult { + if (node.transformFlags & TransformFlags.ES2017) { + return visitorWorker(node); + } + else if (node.transformFlags & TransformFlags.ContainsES2017) { + return visitEachChild(node, visitor, context); + } + + return node; + } + + function visitorWorker(node: Node): VisitResult { + switch (node.kind) { + case SyntaxKind.AsyncKeyword: + // ES2017 async modifier should be elided for targets < ES2017 + return undefined; + + case SyntaxKind.AwaitExpression: + // ES2017 'await' expressions must be transformed for targets < ES2017. + return visitAwaitExpression(node); + + case SyntaxKind.MethodDeclaration: + // ES2017 method declarations may be 'async' + return visitMethodDeclaration(node); + + case SyntaxKind.FunctionDeclaration: + // ES2017 function declarations may be 'async' + return visitFunctionDeclaration(node); + + case SyntaxKind.FunctionExpression: + // ES2017 function expressions may be 'async' + return visitFunctionExpression(node); + + case SyntaxKind.ArrowFunction: + // ES2017 arrow functions may be 'async' + return visitArrowFunction(node); + + default: + Debug.failBadSyntaxKind(node); + return node; + } + } + + /** + * Visits an await expression. + * + * This function will be called any time a ES2017 await expression is encountered. + * + * @param node The await expression node. + */ + function visitAwaitExpression(node: AwaitExpression): Expression { + return setOriginalNode( + createYield( + /*asteriskToken*/ undefined, + visitNode(node.expression, visitor, isExpression), + /*location*/ node + ), + node + ); + } + + /** + * Visits a method declaration of a class. + * + * This function will be called when one of the following conditions are met: + * - The node is marked as async + * + * @param node The method node. + */ + function visitMethodDeclaration(node: MethodDeclaration) { + if (!isAsyncFunctionLike(node)) { + return node; + } + const method = createMethod( + /*decorators*/ undefined, + visitNodes(node.modifiers, visitor, isModifier), + node.asteriskToken, + node.name, + /*typeParameters*/ undefined, + visitNodes(node.parameters, visitor, isParameter), + /*type*/ undefined, + transformFunctionBody(node), + /*location*/ node + ); + + // While we emit the source map for the node after skipping decorators and modifiers, + // we need to emit the comments for the original range. + setCommentRange(method, node); + setSourceMapRange(method, moveRangePastDecorators(node)); + setOriginalNode(method, node); + + return method; + } + + /** + * Visits a function declaration. + * + * This function will be called when one of the following conditions are met: + * - The node is marked async + * + * @param node The function node. + */ + function visitFunctionDeclaration(node: FunctionDeclaration): VisitResult { + if (!isAsyncFunctionLike(node)) { + return node; + } + const func = createFunctionDeclaration( + /*decorators*/ undefined, + visitNodes(node.modifiers, visitor, isModifier), + node.asteriskToken, + node.name, + /*typeParameters*/ undefined, + visitNodes(node.parameters, visitor, isParameter), + /*type*/ undefined, + transformFunctionBody(node), + /*location*/ node + ); + setOriginalNode(func, node); + + return func; + } + + /** + * Visits a function expression node. + * + * This function will be called when one of the following conditions are met: + * - The node is marked async + * + * @param node The function expression node. + */ + function visitFunctionExpression(node: FunctionExpression): Expression { + if (!isAsyncFunctionLike(node)) { + return node; + } + if (nodeIsMissing(node.body)) { + return createOmittedExpression(); + } + + const func = createFunctionExpression( + /*modifiers*/ undefined, + node.asteriskToken, + node.name, + /*typeParameters*/ undefined, + visitNodes(node.parameters, visitor, isParameter), + /*type*/ undefined, + transformFunctionBody(node), + /*location*/ node + ); + + setOriginalNode(func, node); + + return func; + } + + /** + * @remarks + * This function will be called when one of the following conditions are met: + * - The node is marked async + */ + function visitArrowFunction(node: ArrowFunction) { + if (!isAsyncFunctionLike(node)) { + return node; + } + const func = createArrowFunction( + visitNodes(node.modifiers, visitor, isModifier), + /*typeParameters*/ undefined, + visitNodes(node.parameters, visitor, isParameter), + /*type*/ undefined, + node.equalsGreaterThanToken, + transformConciseBody(node), + /*location*/ node + ); + + setOriginalNode(func, node); + + return func; + } + + function transformFunctionBody(node: MethodDeclaration | AccessorDeclaration | FunctionDeclaration | FunctionExpression): FunctionBody { + return transformAsyncFunctionBody(node); + } + + function transformConciseBody(node: ArrowFunction): ConciseBody { + return transformAsyncFunctionBody(node); + } + + function transformFunctionBodyWorker(body: Block, start = 0) { + const savedCurrentScope = currentScope; + currentScope = body; + startLexicalEnvironment(); + + const statements = visitNodes(body.statements, visitor, isStatement, start); + const visited = updateBlock(body, statements); + const declarations = endLexicalEnvironment(); + currentScope = savedCurrentScope; + return mergeFunctionBodyLexicalEnvironment(visited, declarations); + } + + function transformAsyncFunctionBody(node: FunctionLikeDeclaration): ConciseBody | FunctionBody { + const nodeType = node.original ? (node.original).type : node.type; + const promiseConstructor = languageVersion < ScriptTarget.ES2015 ? getPromiseConstructor(nodeType) : undefined; + const isArrowFunction = node.kind === SyntaxKind.ArrowFunction; + const hasLexicalArguments = (resolver.getNodeCheckFlags(node) & NodeCheckFlags.CaptureArguments) !== 0; + + // An async function is emit as an outer function that calls an inner + // generator function. To preserve lexical bindings, we pass the current + // `this` and `arguments` objects to `__awaiter`. The generator function + // passed to `__awaiter` is executed inside of the callback to the + // promise constructor. + + + if (!isArrowFunction) { + const statements: Statement[] = []; + const statementOffset = addPrologueDirectives(statements, (node.body).statements, /*ensureUseStrict*/ false, visitor); + statements.push( + createReturn( + createAwaiterHelper( + currentSourceFileExternalHelpersModuleName, + hasLexicalArguments, + promiseConstructor, + transformFunctionBodyWorker(node.body, statementOffset) + ) + ) + ); + + const block = createBlock(statements, /*location*/ node.body, /*multiLine*/ true); + + // Minor optimization, emit `_super` helper to capture `super` access in an arrow. + // This step isn't needed if we eventually transform this to ES5. + if (languageVersion >= ScriptTarget.ES2015) { + if (resolver.getNodeCheckFlags(node) & NodeCheckFlags.AsyncMethodWithSuperBinding) { + enableSubstitutionForAsyncMethodsWithSuper(); + setEmitFlags(block, EmitFlags.EmitAdvancedSuperHelper); + } + else if (resolver.getNodeCheckFlags(node) & NodeCheckFlags.AsyncMethodWithSuper) { + enableSubstitutionForAsyncMethodsWithSuper(); + setEmitFlags(block, EmitFlags.EmitSuperHelper); + } + } + + return block; + } + else { + return createAwaiterHelper( + currentSourceFileExternalHelpersModuleName, + hasLexicalArguments, + promiseConstructor, + transformConciseBodyWorker(node.body, /*forceBlockFunctionBody*/ true) + ); + } + } + + function transformConciseBodyWorker(body: Block | Expression, forceBlockFunctionBody: boolean) { + if (isBlock(body)) { + return transformFunctionBodyWorker(body); + } + else { + startLexicalEnvironment(); + const visited: Expression | Block = visitNode(body, visitor, isConciseBody); + const declarations = endLexicalEnvironment(); + const merged = mergeFunctionBodyLexicalEnvironment(visited, declarations); + if (forceBlockFunctionBody && !isBlock(merged)) { + return createBlock([ + createReturn(merged) + ]); + } + else { + return merged; + } + } + } + + function getPromiseConstructor(type: TypeNode) { + const typeName = getEntityNameFromTypeNode(type); + if (typeName && isEntityName(typeName)) { + const serializationKind = resolver.getTypeReferenceSerializationKind(typeName); + if (serializationKind === TypeReferenceSerializationKind.TypeWithConstructSignatureAndValue + || serializationKind === TypeReferenceSerializationKind.Unknown) { + return typeName; + } + } + + return undefined; + } + + function enableSubstitutionForAsyncMethodsWithSuper() { + if ((enabledSubstitutions & ES2017SubstitutionFlags.AsyncMethodsWithSuper) === 0) { + enabledSubstitutions |= ES2017SubstitutionFlags.AsyncMethodsWithSuper; + + // We need to enable substitutions for call, property access, and element access + // if we need to rewrite super calls. + context.enableSubstitution(SyntaxKind.CallExpression); + context.enableSubstitution(SyntaxKind.PropertyAccessExpression); + context.enableSubstitution(SyntaxKind.ElementAccessExpression); + + // We need to be notified when entering and exiting declarations that bind super. + context.enableEmitNotification(SyntaxKind.ClassDeclaration); + context.enableEmitNotification(SyntaxKind.MethodDeclaration); + context.enableEmitNotification(SyntaxKind.GetAccessor); + context.enableEmitNotification(SyntaxKind.SetAccessor); + context.enableEmitNotification(SyntaxKind.Constructor); + } + } + + function substituteExpression(node: Expression) { + switch (node.kind) { + case SyntaxKind.PropertyAccessExpression: + return substitutePropertyAccessExpression(node); + case SyntaxKind.ElementAccessExpression: + return substituteElementAccessExpression(node); + case SyntaxKind.CallExpression: + if (enabledSubstitutions & ES2017SubstitutionFlags.AsyncMethodsWithSuper) { + return substituteCallExpression(node); + } + break; + } + + return node; + } + + function substitutePropertyAccessExpression(node: PropertyAccessExpression) { + if (enabledSubstitutions & ES2017SubstitutionFlags.AsyncMethodsWithSuper && node.expression.kind === SyntaxKind.SuperKeyword) { + const flags = getSuperContainerAsyncMethodFlags(); + if (flags) { + return createSuperAccessInAsyncMethod( + createLiteral(node.name.text), + flags, + node + ); + } + } + + return node; + } + + function substituteElementAccessExpression(node: ElementAccessExpression) { + if (enabledSubstitutions & ES2017SubstitutionFlags.AsyncMethodsWithSuper && node.expression.kind === SyntaxKind.SuperKeyword) { + const flags = getSuperContainerAsyncMethodFlags(); + if (flags) { + return createSuperAccessInAsyncMethod( + node.argumentExpression, + flags, + node + ); + } + } + + return node; + } + + function substituteCallExpression(node: CallExpression): Expression { + const expression = node.expression; + if (isSuperProperty(expression)) { + const flags = getSuperContainerAsyncMethodFlags(); + if (flags) { + const argumentExpression = isPropertyAccessExpression(expression) + ? substitutePropertyAccessExpression(expression) + : substituteElementAccessExpression(expression); + return createCall( + createPropertyAccess(argumentExpression, "call"), + /*typeArguments*/ undefined, + [ + createThis(), + ...node.arguments + ] + ); + } + } + return node; + } + + function isSuperContainer(node: Node): node is SuperContainer { + const kind = node.kind; + return kind === SyntaxKind.ClassDeclaration + || kind === SyntaxKind.Constructor + || kind === SyntaxKind.MethodDeclaration + || kind === SyntaxKind.GetAccessor + || kind === SyntaxKind.SetAccessor; + } + + /** + * Hook for node emit. + * + * @param node The node to emit. + * @param emit A callback used to emit the node in the printer. + */ + function onEmitNode(emitContext: EmitContext, node: Node, emitCallback: (emitContext: EmitContext, node: Node) => void): void { + const savedApplicableSubstitutions = applicableSubstitutions; + const savedCurrentSuperContainer = currentSuperContainer; + // If we need to support substitutions for `super` in an async method, + // we should track it here. + if (enabledSubstitutions & ES2017SubstitutionFlags.AsyncMethodsWithSuper && isSuperContainer(node)) { + currentSuperContainer = node; + } + + previousOnEmitNode(emitContext, node, emitCallback); + + applicableSubstitutions = savedApplicableSubstitutions; + currentSuperContainer = savedCurrentSuperContainer; + } + + /** + * Hooks node substitutions. + * + * @param node The node to substitute. + * @param isExpression A value indicating whether the node is to be used in an expression + * position. + */ + function onSubstituteNode(emitContext: EmitContext, node: Node) { + node = previousOnSubstituteNode(emitContext, node); + if (emitContext === EmitContext.Expression) { + return substituteExpression(node); + } + + return node; + } + + function createSuperAccessInAsyncMethod(argumentExpression: Expression, flags: NodeCheckFlags, location: TextRange): LeftHandSideExpression { + if (flags & NodeCheckFlags.AsyncMethodWithSuperBinding) { + return createPropertyAccess( + createCall( + createIdentifier("_super"), + /*typeArguments*/ undefined, + [argumentExpression] + ), + "value", + location + ); + } + else { + return createCall( + createIdentifier("_super"), + /*typeArguments*/ undefined, + [argumentExpression], + location + ); + } + } + + function getSuperContainerAsyncMethodFlags() { + return currentSuperContainer !== undefined + && resolver.getNodeCheckFlags(currentSuperContainer) & (NodeCheckFlags.AsyncMethodWithSuper | NodeCheckFlags.AsyncMethodWithSuperBinding); + } + } +} diff --git a/src/compiler/transformers/generators.ts b/src/compiler/transformers/generators.ts index 610b5f82d5b6b..9d9a8895a30be 100644 --- a/src/compiler/transformers/generators.ts +++ b/src/compiler/transformers/generators.ts @@ -496,6 +496,7 @@ namespace ts { if (node.asteriskToken && getEmitFlags(node) & EmitFlags.AsyncFunctionBody) { node = setOriginalNode( createFunctionExpression( + /*modifiers*/ undefined, /*asteriskToken*/ undefined, node.name, /*typeParameters*/ undefined, @@ -2591,6 +2592,7 @@ namespace ts { createThis(), setEmitFlags( createFunctionExpression( + /*modifiers*/ undefined, /*asteriskToken*/ undefined, /*name*/ undefined, /*typeParameters*/ undefined, @@ -3080,4 +3082,4 @@ namespace ts { ); } } -} \ No newline at end of file +} diff --git a/src/compiler/transformers/module/es2015.ts b/src/compiler/transformers/module/es2015.ts new file mode 100644 index 0000000000000..23fb444b6ff1e --- /dev/null +++ b/src/compiler/transformers/module/es2015.ts @@ -0,0 +1,43 @@ +/// +/// + +/*@internal*/ +namespace ts { + export function transformES2015Module(context: TransformationContext) { + const compilerOptions = context.getCompilerOptions(); + return transformSourceFile; + + function transformSourceFile(node: SourceFile) { + if (isDeclarationFile(node)) { + return node; + } + + if (isExternalModule(node) || compilerOptions.isolatedModules) { + return visitEachChild(node, visitor, context); + } + + return node; + } + + function visitor(node: Node): VisitResult { + switch (node.kind) { + case SyntaxKind.ImportEqualsDeclaration: + return visitImportEqualsDeclaration(node); + case SyntaxKind.ExportAssignment: + return visitExportAssignment(node); + } + + return node; + } + + function visitImportEqualsDeclaration(node: ImportEqualsDeclaration): VisitResult { + // Elide `import=` as it is not legal with --module ES6 + return undefined; + } + + function visitExportAssignment(node: ExportAssignment): VisitResult { + // Elide `export=` as it is not legal with --module ES6 + return node.isExportEquals ? undefined : node; + } + } +} diff --git a/src/compiler/transformers/module/es6.ts b/src/compiler/transformers/module/es6.ts deleted file mode 100644 index 09a2890727c58..0000000000000 --- a/src/compiler/transformers/module/es6.ts +++ /dev/null @@ -1,144 +0,0 @@ -/// -/// - -/*@internal*/ -namespace ts { - export function transformES6Module(context: TransformationContext) { - const compilerOptions = context.getCompilerOptions(); - const resolver = context.getEmitResolver(); - - let currentSourceFile: SourceFile; - - return transformSourceFile; - - function transformSourceFile(node: SourceFile) { - if (isDeclarationFile(node)) { - return node; - } - - if (isExternalModule(node) || compilerOptions.isolatedModules) { - currentSourceFile = node; - return visitEachChild(node, visitor, context); - } - return node; - } - - function visitor(node: Node): VisitResult { - switch (node.kind) { - case SyntaxKind.ImportDeclaration: - return visitImportDeclaration(node); - case SyntaxKind.ImportEqualsDeclaration: - return visitImportEqualsDeclaration(node); - case SyntaxKind.ImportClause: - return visitImportClause(node); - case SyntaxKind.NamedImports: - case SyntaxKind.NamespaceImport: - return visitNamedBindings(node); - case SyntaxKind.ImportSpecifier: - return visitImportSpecifier(node); - case SyntaxKind.ExportAssignment: - return visitExportAssignment(node); - case SyntaxKind.ExportDeclaration: - return visitExportDeclaration(node); - case SyntaxKind.NamedExports: - return visitNamedExports(node); - case SyntaxKind.ExportSpecifier: - return visitExportSpecifier(node); - } - - return node; - } - - function visitExportAssignment(node: ExportAssignment): ExportAssignment { - if (node.isExportEquals) { - return undefined; // do not emit export equals for ES6 - } - const original = getOriginalNode(node); - return nodeIsSynthesized(original) || resolver.isValueAliasDeclaration(original) ? node : undefined; - } - - function visitExportDeclaration(node: ExportDeclaration): ExportDeclaration { - if (!node.exportClause) { - return resolver.moduleExportsSomeValue(node.moduleSpecifier) ? node : undefined; - } - if (!resolver.isValueAliasDeclaration(node)) { - return undefined; - } - const newExportClause = visitNode(node.exportClause, visitor, isNamedExports, /*optional*/ true); - if (node.exportClause === newExportClause) { - return node; - } - return newExportClause - ? createExportDeclaration( - /*decorators*/ undefined, - /*modifiers*/ undefined, - newExportClause, - node.moduleSpecifier) - : undefined; - } - - function visitNamedExports(node: NamedExports): NamedExports { - const newExports = visitNodes(node.elements, visitor, isExportSpecifier); - if (node.elements === newExports) { - return node; - } - return newExports.length ? createNamedExports(newExports) : undefined; - } - - function visitExportSpecifier(node: ExportSpecifier): ExportSpecifier { - return resolver.isValueAliasDeclaration(node) ? node : undefined; - } - - function visitImportEqualsDeclaration(node: ImportEqualsDeclaration): ImportEqualsDeclaration { - return !isExternalModuleImportEqualsDeclaration(node) || resolver.isReferencedAliasDeclaration(node) ? node : undefined; - } - - function visitImportDeclaration(node: ImportDeclaration) { - if (node.importClause) { - const newImportClause = visitNode(node.importClause, visitor, isImportClause); - if (!newImportClause.name && !newImportClause.namedBindings) { - return undefined; - } - else if (newImportClause !== node.importClause) { - return createImportDeclaration( - /*decorators*/ undefined, - /*modifiers*/ undefined, - newImportClause, - node.moduleSpecifier); - } - } - return node; - } - - function visitImportClause(node: ImportClause): ImportClause { - let newDefaultImport = node.name; - if (!resolver.isReferencedAliasDeclaration(node)) { - newDefaultImport = undefined; - } - const newNamedBindings = visitNode(node.namedBindings, visitor, isNamedImportBindings, /*optional*/ true); - return newDefaultImport !== node.name || newNamedBindings !== node.namedBindings - ? createImportClause(newDefaultImport, newNamedBindings) - : node; - } - - function visitNamedBindings(node: NamedImportBindings): VisitResult { - if (node.kind === SyntaxKind.NamespaceImport) { - return resolver.isReferencedAliasDeclaration(node) ? node : undefined; - } - else { - const newNamedImportElements = visitNodes((node).elements, visitor, isImportSpecifier); - if (!newNamedImportElements || newNamedImportElements.length == 0) { - return undefined; - } - if (newNamedImportElements === (node).elements) { - return node; - } - return createNamedImports(newNamedImportElements); - } - } - - function visitImportSpecifier(node: ImportSpecifier) { - return resolver.isReferencedAliasDeclaration(node) ? node : undefined; - } - } -} \ No newline at end of file diff --git a/src/compiler/transformers/module/module.ts b/src/compiler/transformers/module/module.ts index de2e3c601887b..67aa5b2eeb42b 100644 --- a/src/compiler/transformers/module/module.ts +++ b/src/compiler/transformers/module/module.ts @@ -59,7 +59,7 @@ namespace ts { currentSourceFile = node; // Collect information about the external module. - ({ externalImports, exportSpecifiers, exportEquals, hasExportStarsToExportValues } = collectExternalModuleInfo(node, resolver)); + ({ externalImports, exportSpecifiers, exportEquals, hasExportStarsToExportValues } = collectExternalModuleInfo(node)); // Perform the transformation. const transformModule = transformModuleDelegates.get(moduleKind) || transformModuleDelegates.get(ModuleKind.None); @@ -179,6 +179,7 @@ namespace ts { // // function (require, exports, module1, module2) ... createFunctionExpression( + /*modifiers*/ undefined, /*asteriskToken*/ undefined, /*name*/ undefined, /*typeParameters*/ undefined, @@ -228,7 +229,7 @@ namespace ts { } function addExportEqualsIfNeeded(statements: Statement[], emitAsReturn: boolean) { - if (exportEquals && resolver.isValueAliasDeclaration(exportEquals)) { + if (exportEquals) { if (emitAsReturn) { const statement = createReturn( exportEquals.expression, @@ -415,7 +416,7 @@ namespace ts { ) ], /*location*/ undefined, - /*flags*/ languageVersion >= ScriptTarget.ES6 ? NodeFlags.Const : NodeFlags.None), + /*flags*/ languageVersion >= ScriptTarget.ES2015 ? NodeFlags.Const : NodeFlags.None), /*location*/ node ) ); @@ -461,23 +462,21 @@ namespace ts { ); } for (const specifier of node.exportClause.elements) { - if (resolver.isValueAliasDeclaration(specifier)) { - const exportedValue = createPropertyAccess( - generatedName, - specifier.propertyName || specifier.name - ); - statements.push( - createStatement( - createExportAssignment(specifier.name, exportedValue), - /*location*/ specifier - ) - ); - } + const exportedValue = createPropertyAccess( + generatedName, + specifier.propertyName || specifier.name + ); + statements.push( + createStatement( + createExportAssignment(specifier.name, exportedValue), + /*location*/ specifier + ) + ); } return singleOrMany(statements); } - else if (resolver.moduleExportsSomeValue(node.moduleSpecifier)) { + else { // export * from "mod"; return createStatement( createCall( @@ -495,15 +494,14 @@ namespace ts { } function visitExportAssignment(node: ExportAssignment): VisitResult { - if (!node.isExportEquals) { - if (nodeIsSynthesized(node) || resolver.isValueAliasDeclaration(node)) { - const statements: Statement[] = []; - addExportDefault(statements, node.expression, /*location*/ node); - return statements; - } + if (node.isExportEquals) { + // Elide as `export=` is handled in addExportEqualsIfNeeded + return undefined; } - return undefined; + const statements: Statement[] = []; + addExportDefault(statements, node.expression, /*location*/ node); + return statements; } function addExportDefault(statements: Statement[], expression: Expression, location: TextRange): void { @@ -568,7 +566,7 @@ namespace ts { } function collectExportMembers(names: Identifier[], node: Node): Identifier[] { - if (isAliasSymbolDeclaration(node) && resolver.isValueAliasDeclaration(node) && isDeclaration(node)) { + if (isAliasSymbolDeclaration(node) && isDeclaration(node)) { const name = node.name; if (isIdentifier(name)) { names.push(name); @@ -701,11 +699,13 @@ namespace ts { const statements: Statement[] = []; const name = node.name || getGeneratedNameForNode(node); if (hasModifier(node, ModifierFlags.Export)) { + // Keep async modifier for ES2017 transformer + const isAsync = hasModifier(node, ModifierFlags.Async); statements.push( setOriginalNode( createFunctionDeclaration( /*decorators*/ undefined, - /*modifiers*/ undefined, + isAsync ? [createNode(SyntaxKind.AsyncKeyword)] : undefined, node.asteriskToken, name, /*typeParameters*/ undefined, diff --git a/src/compiler/transformers/module/system.ts b/src/compiler/transformers/module/system.ts index c48dcfcba74f3..ff6227675d513 100644 --- a/src/compiler/transformers/module/system.ts +++ b/src/compiler/transformers/module/system.ts @@ -91,7 +91,7 @@ namespace ts { Debug.assert(!exportFunctionForFile); // Collect information about the external module and dependency groups. - ({ externalImports, exportSpecifiers, exportEquals, hasExportStarsToExportValues } = collectExternalModuleInfo(node, resolver)); + ({ externalImports, exportSpecifiers, exportEquals, hasExportStarsToExportValues } = collectExternalModuleInfo(node)); // Make sure that the name of the 'exports' function does not conflict with // existing identifiers. @@ -110,6 +110,7 @@ namespace ts { const moduleName = tryGetModuleNameFromFile(node, host, compilerOptions); const dependencies = createArrayLiteral(map(dependencyGroups, getNameOfDependencyGroup)); const body = createFunctionExpression( + /*modifiers*/ undefined, /*asteriskToken*/ undefined, /*name*/ undefined, /*typeParameters*/ undefined, @@ -244,6 +245,7 @@ namespace ts { ), createPropertyAssignment("execute", createFunctionExpression( + /*modifiers*/ undefined, /*asteriskToken*/ undefined, /*name*/ undefined, /*typeParameters*/ undefined, @@ -430,6 +432,7 @@ namespace ts { setters.push( createFunctionExpression( + /*modifiers*/ undefined, /*asteriskToken*/ undefined, /*name*/ undefined, /*typeParameters*/ undefined, @@ -573,28 +576,23 @@ namespace ts { } function visitExportSpecifier(specifier: ExportSpecifier): Statement { - if (resolver.getReferencedValueDeclaration(specifier.propertyName || specifier.name) - || resolver.isValueAliasDeclaration(specifier)) { - recordExportName(specifier.name); - return createExportStatement( - specifier.name, - specifier.propertyName || specifier.name - ); - } - return undefined; + recordExportName(specifier.name); + return createExportStatement( + specifier.name, + specifier.propertyName || specifier.name + ); } function visitExportAssignment(node: ExportAssignment): Statement { - if (!node.isExportEquals) { - if (nodeIsSynthesized(node) || resolver.isValueAliasDeclaration(node)) { - return createExportStatement( - createLiteral("default"), - node.expression - ); - } + if (node.isExportEquals) { + // Elide `export=` as it is illegal in a SystemJS module. + return undefined; } - return undefined; + return createExportStatement( + createLiteral("default"), + node.expression + ); } /** @@ -662,9 +660,11 @@ namespace ts { if (hasModifier(node, ModifierFlags.Export)) { // If the function is exported, ensure it has a name and rewrite the function without any export flags. const name = node.name || getGeneratedNameForNode(node); + // Keep async modifier for ES2017 transformer + const isAsync = hasModifier(node, ModifierFlags.Async); const newNode = createFunctionDeclaration( /*decorators*/ undefined, - /*modifiers*/ undefined, + isAsync ? [createNode(SyntaxKind.AsyncKeyword)] : undefined, node.asteriskToken, name, /*typeParameters*/ undefined, @@ -1402,4 +1402,4 @@ namespace ts { return updated; } } -} \ No newline at end of file +} diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 085a4aa94fa92..fb78f7b9db4ea 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -4,8 +4,6 @@ /*@internal*/ namespace ts { - type SuperContainer = ClassDeclaration | MethodDeclaration | GetAccessorDeclaration | SetAccessorDeclaration | ConstructorDeclaration; - /** * Indicates whether to emit type metadata in the new format. */ @@ -16,8 +14,6 @@ namespace ts { ClassAliases = 1 << 0, /** Enables substitutions for namespace exports. */ NamespaceExports = 1 << 1, - /** Enables substitutions for async methods with `super` calls. */ - AsyncMethodsWithSuper = 1 << 2, /* Enables substitutions for unqualified enum members */ NonQualifiedEnumMembers = 1 << 3 } @@ -72,12 +68,6 @@ namespace ts { */ let applicableSubstitutions: TypeScriptSubstitutionFlags; - /** - * This keeps track of containers where `super` is valid, for use with - * just-in-time substitution for `super` expressions inside of async methods. - */ - let currentSuperContainer: SuperContainer; - return transformSourceFile; /** @@ -147,6 +137,35 @@ namespace ts { return node; } + /** + * Specialized visitor that visits the immediate children of a SourceFile. + * + * @param node The node to visit. + */ + function sourceElementVisitor(node: Node): VisitResult { + return saveStateAndInvoke(node, sourceElementVisitorWorker); + } + + /** + * Specialized visitor that visits the immediate children of a SourceFile. + * + * @param node The node to visit. + */ + function sourceElementVisitorWorker(node: Node): VisitResult { + switch (node.kind) { + case SyntaxKind.ImportDeclaration: + return visitImportDeclaration(node); + case SyntaxKind.ImportEqualsDeclaration: + return visitImportEqualsDeclaration(node); + case SyntaxKind.ExportAssignment: + return visitExportAssignment(node); + case SyntaxKind.ExportDeclaration: + return visitExportDeclaration(node); + default: + return visitorWorker(node); + } + } + /** * Specialized visitor that visits the immediate children of a namespace. * @@ -244,7 +263,6 @@ namespace ts { case SyntaxKind.PrivateKeyword: case SyntaxKind.ProtectedKeyword: case SyntaxKind.AbstractKeyword: - case SyntaxKind.AsyncKeyword: case SyntaxKind.ConstKeyword: case SyntaxKind.DeclareKeyword: case SyntaxKind.ReadonlyKeyword: @@ -286,8 +304,7 @@ namespace ts { // TypeScript property declarations are elided. case SyntaxKind.Constructor: - // TypeScript constructors are transformed in `visitClassDeclaration`. - return undefined; + return visitConstructor(node); case SyntaxKind.InterfaceDeclaration: // TypeScript interfaces are elided, but some comments may be preserved. @@ -304,7 +321,6 @@ namespace ts { // - property declarations // - index signatures // - method overload signatures - // - async methods return visitClassDeclaration(node); case SyntaxKind.ClassExpression: @@ -317,7 +333,6 @@ namespace ts { // - property declarations // - index signatures // - method overload signatures - // - async methods return visitClassExpression(node); case SyntaxKind.HeritageClause: @@ -332,7 +347,7 @@ namespace ts { return visitExpressionWithTypeArguments(node); case SyntaxKind.MethodDeclaration: - // TypeScript method declarations may be 'async', and may have decorators, modifiers + // TypeScript method declarations may have decorators, modifiers // or type annotations. return visitMethodDeclaration(node); @@ -341,19 +356,19 @@ namespace ts { return visitGetAccessor(node); case SyntaxKind.SetAccessor: - // Set Accessors can have TypeScript modifiers, decorators, and type annotations. + // Set Accessors can have TypeScript modifiers and type annotations. return visitSetAccessor(node); case SyntaxKind.FunctionDeclaration: - // TypeScript function declarations may be 'async' + // Typescript function declarations can have modifiers, decorators, and type annotations. return visitFunctionDeclaration(node); case SyntaxKind.FunctionExpression: - // TypeScript function expressions may be 'async' + // TypeScript function expressions can have modifiers and type annotations. return visitFunctionExpression(node); case SyntaxKind.ArrowFunction: - // TypeScript arrow functions may be 'async' + // TypeScript arrow functions can have modifiers and type annotations. return visitArrowFunction(node); case SyntaxKind.Parameter: @@ -377,6 +392,12 @@ namespace ts { // TypeScript type assertions are removed, but their subtrees are preserved. return visitAssertionExpression(node); + case SyntaxKind.CallExpression: + return visitCallExpression(node); + + case SyntaxKind.NewExpression: + return visitNewExpression(node); + case SyntaxKind.NonNullExpression: // TypeScript non-null expressions are removed, but their subtrees are preserved. return visitNonNullExpression(node); @@ -385,14 +406,13 @@ namespace ts { // TypeScript enum declarations do not exist in ES6 and must be rewritten. return visitEnumDeclaration(node); - case SyntaxKind.AwaitExpression: - // TypeScript 'await' expressions must be transformed. - return visitAwaitExpression(node); - case SyntaxKind.VariableStatement: // TypeScript namespace exports for variable statements must be transformed. return visitVariableStatement(node); + case SyntaxKind.VariableDeclaration: + return visitVariableDeclaration(node); + case SyntaxKind.ModuleDeclaration: // TypeScript namespace declarations must be transformed. return visitModuleDeclaration(node); @@ -436,6 +456,11 @@ namespace ts { function visitSourceFile(node: SourceFile) { currentSourceFile = node; + // ensure "use strict" is emitted in all scenarios in alwaysStrict mode + if (compilerOptions.alwaysStrict) { + node = ensureUseStrict(node); + } + // If the source file requires any helpers and is an external module, and // the importHelpers compiler option is enabled, emit a synthesized import // statement for the helpers library. @@ -457,7 +482,7 @@ namespace ts { statements.push(externalHelpersModuleImport); currentSourceFileExternalHelpersModuleName = externalHelpersModuleName; - addRange(statements, visitNodes(node.statements, visitor, isStatement, statementOffset)); + addRange(statements, visitNodes(node.statements, sourceElementVisitor, isStatement, statementOffset)); addRange(statements, endLexicalEnvironment()); currentSourceFileExternalHelpersModuleName = undefined; @@ -465,7 +490,7 @@ namespace ts { node.externalHelpersModuleName = externalHelpersModuleName; } else { - node = visitEachChild(node, visitor, context); + node = visitEachChild(node, sourceElementVisitor, context); } setEmitFlags(node, EmitFlags.EmitEmitHelpers | getEmitFlags(node)); @@ -1794,7 +1819,7 @@ namespace ts { return createIdentifier("Number"); case SyntaxKind.SymbolKeyword: - return languageVersion < ScriptTarget.ES6 + return languageVersion < ScriptTarget.ES2015 ? getGlobalSymbolNameWithFallback() : createIdentifier("Symbol"); @@ -1891,7 +1916,7 @@ namespace ts { return createIdentifier("Array"); case TypeReferenceSerializationKind.ESSymbolType: - return languageVersion < ScriptTarget.ES6 + return languageVersion < ScriptTarget.ES2015 ? getGlobalSymbolNameWithFallback() : createIdentifier("Symbol"); @@ -2083,12 +2108,20 @@ namespace ts { return !nodeIsMissing(node.body); } + function visitConstructor(node: ConstructorDeclaration) { + if (!shouldEmitFunctionLikeDeclaration(node)) { + return undefined; + } + + return visitEachChild(node, visitor, context); + } + /** * Visits a method declaration of a class. * * This function will be called when one of the following conditions are met: * - The node is an overload - * - The node is marked as abstract, async, public, private, protected, or readonly + * - The node is marked as abstract, public, private, protected, or readonly * - The node has both a decorator and a computed property name * * @param node The method node. @@ -2199,8 +2232,8 @@ namespace ts { * * This function will be called when one of the following conditions are met: * - The node is an overload - * - The node is marked async * - The node is exported from a TypeScript namespace + * - The node has decorators * * @param node The function node. */ @@ -2235,7 +2268,7 @@ namespace ts { * Visits a function expression node. * * This function will be called when one of the following conditions are met: - * - The node is marked async + * - The node has type annotations * * @param node The function expression node. */ @@ -2245,6 +2278,7 @@ namespace ts { } const func = createFunctionExpression( + visitNodes(node.modifiers, visitor, isModifier), node.asteriskToken, node.name, /*typeParameters*/ undefined, @@ -2262,11 +2296,11 @@ namespace ts { /** * @remarks * This function will be called when one of the following conditions are met: - * - The node is marked async + * - The node has type annotations */ function visitArrowFunction(node: ArrowFunction) { const func = createArrowFunction( - /*modifiers*/ undefined, + visitNodes(node.modifiers, visitor, isModifier), /*typeParameters*/ undefined, visitNodes(node.parameters, visitor, isParameter), /*type*/ undefined, @@ -2281,30 +2315,25 @@ namespace ts { } function transformFunctionBody(node: MethodDeclaration | AccessorDeclaration | FunctionDeclaration | FunctionExpression): FunctionBody { - if (isAsyncFunctionLike(node)) { - return transformAsyncFunctionBody(node); - } - return transformFunctionBodyWorker(node.body); } function transformFunctionBodyWorker(body: Block, start = 0) { const savedCurrentScope = currentScope; + const savedCurrentScopeFirstDeclarationsOfName = currentScopeFirstDeclarationsOfName; currentScope = body; + currentScopeFirstDeclarationsOfName = new StringMap(); startLexicalEnvironment(); const statements = visitNodes(body.statements, visitor, isStatement, start); const visited = updateBlock(body, statements); const declarations = endLexicalEnvironment(); currentScope = savedCurrentScope; + currentScopeFirstDeclarationsOfName = savedCurrentScopeFirstDeclarationsOfName; return mergeFunctionBodyLexicalEnvironment(visited, declarations); } function transformConciseBody(node: ArrowFunction): ConciseBody { - if (isAsyncFunctionLike(node)) { - return transformAsyncFunctionBody(node); - } - return transformConciseBodyWorker(node.body, /*forceBlockFunctionBody*/ false); } @@ -2328,72 +2357,6 @@ namespace ts { } } - function getPromiseConstructor(type: TypeNode) { - const typeName = getEntityNameFromTypeNode(type); - if (typeName && isEntityName(typeName)) { - const serializationKind = resolver.getTypeReferenceSerializationKind(typeName); - if (serializationKind === TypeReferenceSerializationKind.TypeWithConstructSignatureAndValue - || serializationKind === TypeReferenceSerializationKind.Unknown) { - return typeName; - } - } - - return undefined; - } - - function transformAsyncFunctionBody(node: FunctionLikeDeclaration): ConciseBody | FunctionBody { - const promiseConstructor = languageVersion < ScriptTarget.ES6 ? getPromiseConstructor(node.type) : undefined; - const isArrowFunction = node.kind === SyntaxKind.ArrowFunction; - const hasLexicalArguments = (resolver.getNodeCheckFlags(node) & NodeCheckFlags.CaptureArguments) !== 0; - - // An async function is emit as an outer function that calls an inner - // generator function. To preserve lexical bindings, we pass the current - // `this` and `arguments` objects to `__awaiter`. The generator function - // passed to `__awaiter` is executed inside of the callback to the - // promise constructor. - - - if (!isArrowFunction) { - const statements: Statement[] = []; - const statementOffset = addPrologueDirectives(statements, (node.body).statements, /*ensureUseStrict*/ false, visitor); - statements.push( - createReturn( - createAwaiterHelper( - currentSourceFileExternalHelpersModuleName, - hasLexicalArguments, - promiseConstructor, - transformFunctionBodyWorker(node.body, statementOffset) - ) - ) - ); - - const block = createBlock(statements, /*location*/ node.body, /*multiLine*/ true); - - // Minor optimization, emit `_super` helper to capture `super` access in an arrow. - // This step isn't needed if we eventually transform this to ES5. - if (languageVersion >= ScriptTarget.ES6) { - if (resolver.getNodeCheckFlags(node) & NodeCheckFlags.AsyncMethodWithSuperBinding) { - enableSubstitutionForAsyncMethodsWithSuper(); - setEmitFlags(block, EmitFlags.EmitAdvancedSuperHelper); - } - else if (resolver.getNodeCheckFlags(node) & NodeCheckFlags.AsyncMethodWithSuper) { - enableSubstitutionForAsyncMethodsWithSuper(); - setEmitFlags(block, EmitFlags.EmitSuperHelper); - } - } - - return block; - } - else { - return createAwaiterHelper( - currentSourceFileExternalHelpersModuleName, - hasLexicalArguments, - promiseConstructor, - transformConciseBodyWorker(node.body, /*forceBlockFunctionBody*/ true) - ); - } - } - /** * Visits a parameter declaration node. * @@ -2476,22 +2439,12 @@ namespace ts { } } - /** - * Visits an await expression. - * - * This function will be called any time a TypeScript await expression is encountered. - * - * @param node The await expression node. - */ - function visitAwaitExpression(node: AwaitExpression): Expression { - return setOriginalNode( - createYield( - /*asteriskToken*/ undefined, - visitNode(node.expression, visitor, isExpression), - /*location*/ node - ), - node - ); + function visitVariableDeclaration(node: VariableDeclaration) { + return updateVariableDeclaration( + node, + visitNode(node.name, visitor, isBindingName), + /*type*/ undefined, + visitNode(node.initializer, visitor, isExpression)); } /** @@ -2536,6 +2489,22 @@ namespace ts { return createPartiallyEmittedExpression(expression, node); } + function visitCallExpression(node: CallExpression) { + return updateCall( + node, + visitNode(node.expression, visitor, isExpression), + /*typeArguments*/ undefined, + visitNodes(node.arguments, visitor, isExpression)); + } + + function visitNewExpression(node: NewExpression) { + return updateNew( + node, + visitNode(node.expression, visitor, isExpression), + /*typeArguments*/ undefined, + visitNodes(node.arguments, visitor, isExpression)); + } + /** * Determines whether to emit an enum declaration. * @@ -2617,6 +2586,7 @@ namespace ts { const enumStatement = createStatement( createCall( createFunctionExpression( + /*modifiers*/ undefined, /*asteriskToken*/ undefined, /*name*/ undefined, /*typeParameters*/ undefined, @@ -2730,7 +2700,7 @@ namespace ts { function isES6ExportedDeclaration(node: Node) { return isExternalModuleExport(node) - && moduleKind === ModuleKind.ES6; + && moduleKind === ModuleKind.ES2015; } /** @@ -2886,6 +2856,7 @@ namespace ts { const moduleStatement = createStatement( createCall( createFunctionExpression( + /*modifiers*/ undefined, /*asteriskToken*/ undefined, /*name*/ undefined, /*typeParameters*/ undefined, @@ -2991,6 +2962,133 @@ namespace ts { } } + /** + * Visits an import declaration, eliding it if it is not referenced. + * + * @param node The import declaration node. + */ + function visitImportDeclaration(node: ImportDeclaration): VisitResult { + if (!node.importClause) { + // Do not elide a side-effect only import declaration. + // import "foo"; + return node; + } + + // Elide the declaration if the import clause was elided. + const importClause = visitNode(node.importClause, visitImportClause, isImportClause, /*optional*/ true); + return importClause + ? updateImportDeclaration( + node, + /*decorators*/ undefined, + /*modifiers*/ undefined, + importClause, + node.moduleSpecifier) + : undefined; + } + + /** + * Visits an import clause, eliding it if it is not referenced. + * + * @param node The import clause node. + */ + function visitImportClause(node: ImportClause): VisitResult { + // Elide the import clause if we elide both its name and its named bindings. + const name = resolver.isReferencedAliasDeclaration(node) ? node.name : undefined; + const namedBindings = visitNode(node.namedBindings, visitNamedImportBindings, isNamedImportBindings, /*optional*/ true); + return (name || namedBindings) ? updateImportClause(node, name, namedBindings) : undefined; + } + + /** + * Visits named import bindings, eliding it if it is not referenced. + * + * @param node The named import bindings node. + */ + function visitNamedImportBindings(node: NamedImportBindings): VisitResult { + if (node.kind === SyntaxKind.NamespaceImport) { + // Elide a namespace import if it is not referenced. + return resolver.isReferencedAliasDeclaration(node) ? node : undefined; + } + else { + // Elide named imports if all of its import specifiers are elided. + const elements = visitNodes(node.elements, visitImportSpecifier, isImportSpecifier); + return some(elements) ? updateNamedImports(node, elements) : undefined; + } + } + + /** + * Visits an import specifier, eliding it if it is not referenced. + * + * @param node The import specifier node. + */ + function visitImportSpecifier(node: ImportSpecifier): VisitResult { + // Elide an import specifier if it is not referenced. + return resolver.isReferencedAliasDeclaration(node) ? node : undefined; + } + + /** + * Visits an export assignment, eliding it if it does not contain a clause that resolves + * to a value. + * + * @param node The export assignment node. + */ + function visitExportAssignment(node: ExportAssignment): VisitResult { + // Elide the export assignment if it does not reference a value. + return resolver.isValueAliasDeclaration(node) + ? visitEachChild(node, visitor, context) + : undefined; + } + + /** + * Visits an export declaration, eliding it if it does not contain a clause that resolves + * to a value. + * + * @param node The export declaration node. + */ + function visitExportDeclaration(node: ExportDeclaration): VisitResult { + if (!node.exportClause) { + // Elide a star export if the module it references does not export a value. + return resolver.moduleExportsSomeValue(node.moduleSpecifier) ? node : undefined; + } + + if (!resolver.isValueAliasDeclaration(node)) { + // Elide the export declaration if it does not export a value. + return undefined; + } + + // Elide the export declaration if all of its named exports are elided. + const exportClause = visitNode(node.exportClause, visitNamedExports, isNamedExports, /*optional*/ true); + return exportClause + ? updateExportDeclaration( + node, + /*decorators*/ undefined, + /*modifiers*/ undefined, + exportClause, + node.moduleSpecifier) + : undefined; + } + + /** + * Visits named exports, eliding it if it does not contain an export specifier that + * resolves to a value. + * + * @param node The named exports node. + */ + function visitNamedExports(node: NamedExports): VisitResult { + // Elide the named exports if all of its export specifiers were elided. + const elements = visitNodes(node.elements, visitExportSpecifier, isExportSpecifier); + return some(elements) ? updateNamedExports(node, elements) : undefined; + } + + /** + * Visits an export specifier, eliding it if it does not resolve to a value. + * + * @param node The export specifier node. + */ + function visitExportSpecifier(node: ExportSpecifier): VisitResult { + // Elide an export specifier if it does not reference a value. + return resolver.isValueAliasDeclaration(node) ? node : undefined; + } + /** * Determines whether to emit an import equals declaration. * @@ -3012,7 +3110,10 @@ namespace ts { */ function visitImportEqualsDeclaration(node: ImportEqualsDeclaration): VisitResult { if (isExternalModuleImportEqualsDeclaration(node)) { - return visitEachChild(node, visitor, context); + // Elide external module `import=` if it is not referenced. + return resolver.isReferencedAliasDeclaration(node) + ? visitEachChild(node, visitor, context) + : undefined; } if (!shouldEmitImportEqualsDeclaration(node)) { @@ -3246,25 +3347,6 @@ namespace ts { } } - function enableSubstitutionForAsyncMethodsWithSuper() { - if ((enabledSubstitutions & TypeScriptSubstitutionFlags.AsyncMethodsWithSuper) === 0) { - enabledSubstitutions |= TypeScriptSubstitutionFlags.AsyncMethodsWithSuper; - - // We need to enable substitutions for call, property access, and element access - // if we need to rewrite super calls. - context.enableSubstitution(SyntaxKind.CallExpression); - context.enableSubstitution(SyntaxKind.PropertyAccessExpression); - context.enableSubstitution(SyntaxKind.ElementAccessExpression); - - // We need to be notified when entering and exiting declarations that bind super. - context.enableEmitNotification(SyntaxKind.ClassDeclaration); - context.enableEmitNotification(SyntaxKind.MethodDeclaration); - context.enableEmitNotification(SyntaxKind.GetAccessor); - context.enableEmitNotification(SyntaxKind.SetAccessor); - context.enableEmitNotification(SyntaxKind.Constructor); - } - } - function enableSubstitutionForClassAliases() { if ((enabledSubstitutions & TypeScriptSubstitutionFlags.ClassAliases) === 0) { enabledSubstitutions |= TypeScriptSubstitutionFlags.ClassAliases; @@ -3292,15 +3374,6 @@ namespace ts { } } - function isSuperContainer(node: Node): node is SuperContainer { - const kind = node.kind; - return kind === SyntaxKind.ClassDeclaration - || kind === SyntaxKind.Constructor - || kind === SyntaxKind.MethodDeclaration - || kind === SyntaxKind.GetAccessor - || kind === SyntaxKind.SetAccessor; - } - function isTransformedModuleDeclaration(node: Node): boolean { return getOriginalNode(node).kind === SyntaxKind.ModuleDeclaration; } @@ -3317,12 +3390,6 @@ namespace ts { */ function onEmitNode(emitContext: EmitContext, node: Node, emitCallback: (emitContext: EmitContext, node: Node) => void): void { const savedApplicableSubstitutions = applicableSubstitutions; - const savedCurrentSuperContainer = currentSuperContainer; - // If we need to support substitutions for `super` in an async method, - // we should track it here. - if (enabledSubstitutions & TypeScriptSubstitutionFlags.AsyncMethodsWithSuper && isSuperContainer(node)) { - currentSuperContainer = node; - } if (enabledSubstitutions & TypeScriptSubstitutionFlags.NamespaceExports && isTransformedModuleDeclaration(node)) { applicableSubstitutions |= TypeScriptSubstitutionFlags.NamespaceExports; @@ -3335,7 +3402,6 @@ namespace ts { previousOnEmitNode(emitContext, node, emitCallback); applicableSubstitutions = savedApplicableSubstitutions; - currentSuperContainer = savedCurrentSuperContainer; } /** @@ -3382,11 +3448,6 @@ namespace ts { return substitutePropertyAccessExpression(node); case SyntaxKind.ElementAccessExpression: return substituteElementAccessExpression(node); - case SyntaxKind.CallExpression: - if (enabledSubstitutions & TypeScriptSubstitutionFlags.AsyncMethodsWithSuper) { - return substituteCallExpression(node); - } - break; } return node; @@ -3441,54 +3502,11 @@ namespace ts { return undefined; } - function substituteCallExpression(node: CallExpression): Expression { - const expression = node.expression; - if (isSuperProperty(expression)) { - const flags = getSuperContainerAsyncMethodFlags(); - if (flags) { - const argumentExpression = isPropertyAccessExpression(expression) - ? substitutePropertyAccessExpression(expression) - : substituteElementAccessExpression(expression); - return createCall( - createPropertyAccess(argumentExpression, "call"), - /*typeArguments*/ undefined, - [ - createThis(), - ...node.arguments - ] - ); - } - } - return node; - } - function substitutePropertyAccessExpression(node: PropertyAccessExpression) { - if (enabledSubstitutions & TypeScriptSubstitutionFlags.AsyncMethodsWithSuper && node.expression.kind === SyntaxKind.SuperKeyword) { - const flags = getSuperContainerAsyncMethodFlags(); - if (flags) { - return createSuperAccessInAsyncMethod( - createLiteral(node.name.text), - flags, - node - ); - } - } - return substituteConstantValue(node); } function substituteElementAccessExpression(node: ElementAccessExpression) { - if (enabledSubstitutions & TypeScriptSubstitutionFlags.AsyncMethodsWithSuper && node.expression.kind === SyntaxKind.SuperKeyword) { - const flags = getSuperContainerAsyncMethodFlags(); - if (flags) { - return createSuperAccessInAsyncMethod( - node.argumentExpression, - flags, - node - ); - } - } - return substituteConstantValue(node); } @@ -3521,32 +3539,5 @@ namespace ts { ? resolver.getConstantValue(node) : undefined; } - - function createSuperAccessInAsyncMethod(argumentExpression: Expression, flags: NodeCheckFlags, location: TextRange): LeftHandSideExpression { - if (flags & NodeCheckFlags.AsyncMethodWithSuperBinding) { - return createPropertyAccess( - createCall( - createIdentifier("_super"), - /*typeArguments*/ undefined, - [argumentExpression] - ), - "value", - location - ); - } - else { - return createCall( - createIdentifier("_super"), - /*typeArguments*/ undefined, - [argumentExpression], - location - ); - } - } - - function getSuperContainerAsyncMethodFlags() { - return currentSuperContainer !== undefined - && resolver.getNodeCheckFlags(currentSuperContainer) & (NodeCheckFlags.AsyncMethodWithSuper | NodeCheckFlags.AsyncMethodWithSuperBinding); - } } } diff --git a/src/compiler/tsconfig.json b/src/compiler/tsconfig.json index 19540367686a3..4bdcaa5e0c10d 100644 --- a/src/compiler/tsconfig.json +++ b/src/compiler/tsconfig.json @@ -25,13 +25,14 @@ "visitor.ts", "transformers/ts.ts", "transformers/jsx.ts", - "transformers/es7.ts", - "transformers/es6.ts", + "transformers/es2017.ts", + "transformers/es2016.ts", + "transformers/es2015.ts", "transformers/generators.ts", "transformers/destructuring.ts", "transformers/module/module.ts", "transformers/module/system.ts", - "transformers/module/es6.ts", + "transformers/module/es2015.ts", "transformer.ts", "comments.ts", "sourcemap.ts", diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 0e7f2fbc21360..a1fe82a8989a7 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -491,6 +491,8 @@ namespace ts { // Accessibility modifiers and 'readonly' can be attached to a parameter in a constructor to make it a property. ParameterPropertyModifier = AccessibilityModifier | Readonly, NonPublicAccessibilityModifier = Private | Protected, + + TypeScriptModifier = Ambient | Public | Private | Protected | Readonly | Abstract | Const } export const enum JsxFlags { @@ -1942,8 +1944,9 @@ namespace ts { TrueCondition = 1 << 5, // Condition known to be true FalseCondition = 1 << 6, // Condition known to be false SwitchClause = 1 << 7, // Switch statement clause - Referenced = 1 << 8, // Referenced as antecedent once - Shared = 1 << 9, // Referenced as antecedent more than once + ArrayMutation = 1 << 8, // Potential array mutation + Referenced = 1 << 9, // Referenced as antecedent once + Shared = 1 << 10, // Referenced as antecedent more than once Label = BranchLabel | LoopLabel, Condition = TrueCondition | FalseCondition } @@ -1957,7 +1960,7 @@ namespace ts { // function, the container property references the function (which in turn has a flowNode // property for the containing control flow). export interface FlowStart extends FlowNode { - container?: FunctionExpression | ArrowFunction; + container?: FunctionExpression | ArrowFunction | MethodDeclaration; } // FlowLabel represents a junction with multiple possible preceding control flows. @@ -1986,6 +1989,13 @@ namespace ts { antecedent: FlowNode; } + // FlowArrayMutation represents a node potentially mutates an array, i.e. an + // operation of the form 'x.push(value)', 'x.unshift(value)' or 'x[n] = value'. + export interface FlowArrayMutation extends FlowNode { + node: CallExpression | BinaryExpression; + antecedent: FlowNode; + } + export type FlowType = Type | IncompleteType; // Incomplete types occur during control flow analysis of loops. An IncompleteType @@ -2679,7 +2689,7 @@ namespace ts { // 'Narrowable' types are types where narrowing actually narrows. // This *should* be every type other than null, undefined, void, and never Narrowable = Any | StructuredType | TypeParameter | StringLike | NumberLike | BooleanLike | ESSymbol, - NotUnionOrUnit = Any | String | Number | ESSymbol | ObjectType, + NotUnionOrUnit = Any | ESSymbol | ObjectType, /* @internal */ RequiresWidening = ContainsWideningType | ContainsObjectLiteral, /* @internal */ @@ -2783,6 +2793,8 @@ namespace ts { export interface AnonymousType extends ObjectType { target?: AnonymousType; // Instantiation target mapper?: TypeMapper; // Instantiation mapper + elementType?: Type; // Element expressions of evolving array type + finalArrayType?: Type; // Final array type of evolving array type } /* @internal */ @@ -2952,11 +2964,7 @@ namespace ts { NodeJs = 2 } - export type RootPaths = string[]; - export type PathSubstitutions = MapLike; - export type TsConfigOnlyOptions = RootPaths | PathSubstitutions; - - export type CompilerOptionsValue = string | number | boolean | (string | number)[] | TsConfigOnlyOptions; + export type CompilerOptionsValue = string | number | boolean | (string | number)[] | string[] | MapLike; export interface CompilerOptions { allowJs?: boolean; @@ -2964,6 +2972,7 @@ namespace ts { allowSyntheticDefaultImports?: boolean; allowUnreachableCode?: boolean; allowUnusedLabels?: boolean; + alwaysStrict?: boolean; baseUrl?: string; charset?: string; /* @internal */ configFilePath?: string; @@ -3008,14 +3017,14 @@ namespace ts { out?: string; outDir?: string; outFile?: string; - paths?: PathSubstitutions; + paths?: MapLike; preserveConstEnums?: boolean; project?: string; /* @internal */ pretty?: DiagnosticStyle; reactNamespace?: string; removeComments?: boolean; rootDir?: string; - rootDirs?: RootPaths; + rootDirs?: string[]; skipLibCheck?: boolean; skipDefaultLibCheck?: boolean; sourceMap?: boolean; @@ -3058,8 +3067,7 @@ namespace ts { AMD = 2, UMD = 3, System = 4, - ES6 = 5, - ES2015 = ES6, + ES2015 = 5, } export const enum JsxEmit { @@ -3092,9 +3100,10 @@ namespace ts { export const enum ScriptTarget { ES3 = 0, ES5 = 1, - ES6 = 2, - ES2015 = ES6, - Latest = ES6, + ES2015 = 2, + ES2016 = 3, + ES2017 = 4, + Latest = ES2017, } export const enum LanguageVariant { @@ -3379,29 +3388,31 @@ namespace ts { ContainsTypeScript = 1 << 1, Jsx = 1 << 2, ContainsJsx = 1 << 3, - ES7 = 1 << 4, - ContainsES7 = 1 << 5, - ES6 = 1 << 6, - ContainsES6 = 1 << 7, - DestructuringAssignment = 1 << 8, - Generator = 1 << 9, - ContainsGenerator = 1 << 10, + ES2017 = 1 << 4, + ContainsES2017 = 1 << 5, + ES2016 = 1 << 6, + ContainsES2016 = 1 << 7, + ES2015 = 1 << 8, + ContainsES2015 = 1 << 9, + DestructuringAssignment = 1 << 10, + Generator = 1 << 11, + ContainsGenerator = 1 << 12, // Markers // - Flags used to indicate that a subtree contains a specific transformation. - ContainsDecorators = 1 << 11, - ContainsPropertyInitializer = 1 << 12, - ContainsLexicalThis = 1 << 13, - ContainsCapturedLexicalThis = 1 << 14, - ContainsLexicalThisInComputedPropertyName = 1 << 15, - ContainsDefaultValueAssignments = 1 << 16, - ContainsParameterPropertyAssignments = 1 << 17, - ContainsSpreadElementExpression = 1 << 18, - ContainsComputedPropertyName = 1 << 19, - ContainsBlockScopedBinding = 1 << 20, - ContainsBindingPattern = 1 << 21, - ContainsYield = 1 << 22, - ContainsHoistedDeclarationOrCompletion = 1 << 23, + ContainsDecorators = 1 << 13, + ContainsPropertyInitializer = 1 << 14, + ContainsLexicalThis = 1 << 15, + ContainsCapturedLexicalThis = 1 << 16, + ContainsLexicalThisInComputedPropertyName = 1 << 17, + ContainsDefaultValueAssignments = 1 << 18, + ContainsParameterPropertyAssignments = 1 << 19, + ContainsSpreadElementExpression = 1 << 20, + ContainsComputedPropertyName = 1 << 21, + ContainsBlockScopedBinding = 1 << 22, + ContainsBindingPattern = 1 << 23, + ContainsYield = 1 << 24, + ContainsHoistedDeclarationOrCompletion = 1 << 25, HasComputedFlags = 1 << 29, // Transform flags have been computed. @@ -3409,14 +3420,15 @@ namespace ts { // - Bitmasks that are used to assert facts about the syntax of a node and its subtree. AssertTypeScript = TypeScript | ContainsTypeScript, AssertJsx = Jsx | ContainsJsx, - AssertES7 = ES7 | ContainsES7, - AssertES6 = ES6 | ContainsES6, + AssertES2017 = ES2017 | ContainsES2017, + AssertES2016 = ES2016 | ContainsES2016, + AssertES2015 = ES2015 | ContainsES2015, AssertGenerator = Generator | ContainsGenerator, // Scope Exclusions // - Bitmasks that exclude flags from propagating out of a specific context // into the subtree flags of their container. - NodeExcludes = TypeScript | Jsx | ES7 | ES6 | DestructuringAssignment | Generator | HasComputedFlags, + NodeExcludes = TypeScript | Jsx | ES2017 | ES2016 | ES2015 | DestructuringAssignment | Generator | HasComputedFlags, ArrowFunctionExcludes = NodeExcludes | ContainsDecorators | ContainsDefaultValueAssignments | ContainsLexicalThis | ContainsParameterPropertyAssignments | ContainsBlockScopedBinding | ContainsYield | ContainsHoistedDeclarationOrCompletion, FunctionExcludes = NodeExcludes | ContainsDecorators | ContainsDefaultValueAssignments | ContainsCapturedLexicalThis | ContainsLexicalThis | ContainsParameterPropertyAssignments | ContainsBlockScopedBinding | ContainsYield | ContainsHoistedDeclarationOrCompletion, ConstructorExcludes = NodeExcludes | ContainsDefaultValueAssignments | ContainsLexicalThis | ContainsCapturedLexicalThis | ContainsBlockScopedBinding | ContainsYield | ContainsHoistedDeclarationOrCompletion, @@ -3432,7 +3444,7 @@ namespace ts { // Masks // - Additional bitmasks TypeScriptClassSyntaxMask = ContainsParameterPropertyAssignments | ContainsPropertyInitializer | ContainsDecorators, - ES6FunctionSyntaxMask = ContainsCapturedLexicalThis | ContainsDefaultValueAssignments, + ES2015FunctionSyntaxMask = ContainsCapturedLexicalThis | ContainsDefaultValueAssignments, } /* @internal */ diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 865f15a7b1b01..f3fb435de6c79 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -337,7 +337,7 @@ namespace ts { export function getLiteralText(node: LiteralLikeNode, sourceFile: SourceFile, languageVersion: ScriptTarget) { // Any template literal or string literal with an extended escape // (e.g. "\u{0067}") will need to be downleveled as a escaped string literal. - if (languageVersion < ScriptTarget.ES6 && (isTemplateLiteralKind(node.kind) || node.hasExtendedUnicodeEscape)) { + if (languageVersion < ScriptTarget.ES2015 && (isTemplateLiteralKind(node.kind) || node.hasExtendedUnicodeEscape)) { return getQuotedEscapedLiteralText('"', node.text, '"'); } @@ -345,7 +345,7 @@ namespace ts { // the node's parent reference, then simply get the text as it was originally written. if (!nodeIsSynthesized(node) && node.parent) { const text = getSourceTextOfNodeFromSourceFile(sourceFile, node); - if (languageVersion < ScriptTarget.ES6 && isBinaryOrOctalIntegerLiteral(node, text)) { + if (languageVersion < ScriptTarget.ES2015 && isBinaryOrOctalIntegerLiteral(node, text)) { return node.text; } return text; @@ -895,6 +895,12 @@ namespace ts { return node && node.kind === SyntaxKind.MethodDeclaration && node.parent.kind === SyntaxKind.ObjectLiteralExpression; } + export function isObjectLiteralOrClassExpressionMethod(node: Node): node is MethodDeclaration { + return node.kind === SyntaxKind.MethodDeclaration && + (node.parent.kind === SyntaxKind.ObjectLiteralExpression || + node.parent.kind === SyntaxKind.ClassExpression); + } + export function isIdentifierTypePredicate(predicate: TypePredicate): predicate is IdentifierTypePredicate { return predicate && predicate.kind === TypePredicateKind.Identifier; } @@ -1895,6 +1901,10 @@ namespace ts { return node.kind === SyntaxKind.Identifier && (node).text === "Symbol"; } + export function isPushOrUnshiftIdentifier(node: Identifier) { + return node.text === "push" || node.text === "unshift"; + } + export function isModifierKind(token: SyntaxKind): boolean { switch (token) { case SyntaxKind.AbstractKeyword: @@ -3496,7 +3506,7 @@ namespace ts { return positionIsSynthesized(range.pos) ? -1 : skipTrivia(sourceFile.text, range.pos); } - export function collectExternalModuleInfo(sourceFile: SourceFile, resolver: EmitResolver) { + export function collectExternalModuleInfo(sourceFile: SourceFile) { const externalImports: (ImportDeclaration | ImportEqualsDeclaration | ExportDeclaration)[] = []; const exportSpecifiers = new StringMap(); let exportEquals: ExportAssignment = undefined; @@ -3504,19 +3514,16 @@ namespace ts { for (const node of sourceFile.statements) { switch (node.kind) { case SyntaxKind.ImportDeclaration: - if (!(node).importClause || - resolver.isReferencedAliasDeclaration((node).importClause, /*checkChildren*/ true)) { - // import "mod" - // import x from "mod" where x is referenced - // import * as x from "mod" where x is referenced - // import { x, y } from "mod" where at least one import is referenced - externalImports.push(node); - } + // import "mod" + // import x from "mod" + // import * as x from "mod" + // import { x, y } from "mod" + externalImports.push(node); break; case SyntaxKind.ImportEqualsDeclaration: - if ((node).moduleReference.kind === SyntaxKind.ExternalModuleReference && resolver.isReferencedAliasDeclaration(node)) { - // import x = require("mod") where x is referenced + if ((node).moduleReference.kind === SyntaxKind.ExternalModuleReference) { + // import x = require("mod") externalImports.push(node); } break; @@ -3525,13 +3532,11 @@ namespace ts { if ((node).moduleSpecifier) { if (!(node).exportClause) { // export * from "mod" - if (resolver.moduleExportsSomeValue((node).moduleSpecifier)) { - externalImports.push(node); - hasExportStarsToExportValues = true; - } + externalImports.push(node); + hasExportStarsToExportValues = true; } - else if (resolver.isValueAliasDeclaration(node)) { - // export { x, y } from "mod" where at least one export is a value symbol + else { + // export { x, y } from "mod" externalImports.push(node); } } @@ -4161,7 +4166,17 @@ namespace ts { namespace ts { export function getDefaultLibFileName(options: CompilerOptions): string { - return options.target === ScriptTarget.ES6 ? "lib.es6.d.ts" : "lib.d.ts"; + switch (options.target) { + case ScriptTarget.ES2017: + return "lib.es2017.d.ts"; + case ScriptTarget.ES2016: + return "lib.es2016.d.ts"; + case ScriptTarget.ES2015: + return "lib.es2015.d.ts"; + + default: + return "lib.d.ts"; + } } export function textSpanEnd(span: TextSpan) { diff --git a/src/compiler/visitor.ts b/src/compiler/visitor.ts index b461d6da4e8d0..bdb927a59bc54 100644 --- a/src/compiler/visitor.ts +++ b/src/compiler/visitor.ts @@ -807,6 +807,7 @@ namespace ts { case SyntaxKind.FunctionExpression: return updateFunctionExpression(node, + visitNodes((node).modifiers, visitor, isModifier), visitNode((node).name, visitor, isPropertyName), visitNodes((node).typeParameters, visitor, isTypeParameter), (context.startLexicalEnvironment(), visitNodes((node).parameters, visitor, isParameter)), @@ -1357,4 +1358,4 @@ namespace ts { } } } -} \ No newline at end of file +} diff --git a/src/harness/harness.ts b/src/harness/harness.ts index 889dd6738b3f8..8ed8ac8ea6e05 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -941,7 +941,17 @@ namespace Harness { } export function getDefaultLibFileName(options: ts.CompilerOptions): string { - return options.target === ts.ScriptTarget.ES6 ? es2015DefaultLibFileName : defaultLibFileName; + switch (options.target) { + case ts.ScriptTarget.ES2017: + return "lib.es2017.d.ts"; + case ts.ScriptTarget.ES2016: + return "lib.es2016.d.ts"; + case ts.ScriptTarget.ES2015: + return es2015DefaultLibFileName; + + default: + return defaultLibFileName; + } } // Cache these between executions so we don't have to re-parse them for every test @@ -1634,7 +1644,7 @@ namespace Harness { export function collateOutputs(outputFiles: Harness.Compiler.GeneratedFile[]): string { // Collect, test, and sort the fileNames - outputFiles.sort((a, b) => cleanName(a.fileName).localeCompare(cleanName(b.fileName))); + outputFiles.sort((a, b) => ts.compareStrings(cleanName(a.fileName), cleanName(b.fileName))); // Emit them let result = ""; diff --git a/src/harness/tsconfig.json b/src/harness/tsconfig.json index f9d302ace81ed..5d7a81d1a7447 100644 --- a/src/harness/tsconfig.json +++ b/src/harness/tsconfig.json @@ -26,13 +26,14 @@ "../compiler/visitor.ts", "../compiler/transformers/ts.ts", "../compiler/transformers/jsx.ts", - "../compiler/transformers/es7.ts", - "../compiler/transformers/es6.ts", + "../compiler/transformers/es2017.ts", + "../compiler/transformers/es2016.ts", + "../compiler/transformers/es2015.ts", "../compiler/transformers/generators.ts", "../compiler/transformers/destructuring.ts", "../compiler/transformers/module/module.ts", "../compiler/transformers/module/system.ts", - "../compiler/transformers/module/es6.ts", + "../compiler/transformers/module/es2015.ts", "../compiler/transformer.ts", "../compiler/comments.ts", "../compiler/sourcemap.ts", diff --git a/src/harness/unittests/commandLineParsing.ts b/src/harness/unittests/commandLineParsing.ts index 028786eef24c4..cd9bf88df60e7 100644 --- a/src/harness/unittests/commandLineParsing.ts +++ b/src/harness/unittests/commandLineParsing.ts @@ -165,7 +165,7 @@ namespace ts { start: undefined, length: undefined, }, { - messageText: "Argument for '--target' option must be: 'es3', 'es5', 'es6', 'es2015'", + messageText: "Argument for '--target' option must be: 'es3', 'es5', 'es6', 'es2015', 'es2016', 'es2017'", category: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.category, code: ts.Diagnostics.Argument_for_0_option_must_be_Colon_1.code, diff --git a/src/harness/unittests/compileOnSave.ts b/src/harness/unittests/compileOnSave.ts index 24fd47ee0cbb5..797268000eb48 100644 --- a/src/harness/unittests/compileOnSave.ts +++ b/src/harness/unittests/compileOnSave.ts @@ -3,6 +3,8 @@ /// namespace ts.projectSystem { + import CommandNames = server.CommandNames; + function createTestTypingsInstaller(host: server.ServerHost) { return new TestTypingsInstaller("/a/data/", /*throttleLimit*/5, host); } @@ -75,7 +77,7 @@ namespace ts.projectSystem { }; // Change the content of file1 to `export var T: number;export function Foo() { };` - changeModuleFile1ShapeRequest1 = makeSessionRequest(server.CommandNames.Change, { + changeModuleFile1ShapeRequest1 = makeSessionRequest(CommandNames.Change, { file: moduleFile1.path, line: 1, offset: 1, @@ -85,7 +87,7 @@ namespace ts.projectSystem { }); // Change the content of file1 to `export var T: number;export function Foo() { };` - changeModuleFile1InternalRequest1 = makeSessionRequest(server.CommandNames.Change, { + changeModuleFile1InternalRequest1 = makeSessionRequest(CommandNames.Change, { file: moduleFile1.path, line: 1, offset: 1, @@ -95,7 +97,7 @@ namespace ts.projectSystem { }); // Change the content of file1 to `export var T: number;export function Foo() { };` - changeModuleFile1ShapeRequest2 = makeSessionRequest(server.CommandNames.Change, { + changeModuleFile1ShapeRequest2 = makeSessionRequest(CommandNames.Change, { file: moduleFile1.path, line: 1, offset: 1, @@ -104,7 +106,7 @@ namespace ts.projectSystem { insertString: `export var T2: number;` }); - moduleFile1FileListRequest = makeSessionRequest(server.CommandNames.CompileOnSaveAffectedFileList, { file: moduleFile1.path, projectFileName: configFile.path }); + moduleFile1FileListRequest = makeSessionRequest(CommandNames.CompileOnSaveAffectedFileList, { file: moduleFile1.path, projectFileName: configFile.path }); }); it("should contains only itself if a module file's shape didn't change, and all files referencing it if its shape changed", () => { @@ -120,7 +122,7 @@ namespace ts.projectSystem { sendAffectedFileRequestAndCheckResult(session, moduleFile1FileListRequest, [{ projectFileName: configFile.path, files: [moduleFile1, file1Consumer1, file1Consumer2] }]); // Change the content of file1 to `export var T: number;export function Foo() { console.log('hi'); };` - const changeFile1InternalRequest = makeSessionRequest(server.CommandNames.Change, { + const changeFile1InternalRequest = makeSessionRequest(CommandNames.Change, { file: moduleFile1.path, line: 1, offset: 46, @@ -143,7 +145,7 @@ namespace ts.projectSystem { sendAffectedFileRequestAndCheckResult(session, moduleFile1FileListRequest, [{ projectFileName: configFile.path, files: [moduleFile1, file1Consumer1, file1Consumer2] }]); // Change file2 content to `let y = Foo();` - const removeFile1Consumer1ImportRequest = makeSessionRequest(server.CommandNames.Change, { + const removeFile1Consumer1ImportRequest = makeSessionRequest(CommandNames.Change, { file: file1Consumer1.path, line: 1, offset: 1, @@ -156,7 +158,7 @@ namespace ts.projectSystem { sendAffectedFileRequestAndCheckResult(session, moduleFile1FileListRequest, [{ projectFileName: configFile.path, files: [moduleFile1, file1Consumer2] }]); // Add the import statements back to file2 - const addFile2ImportRequest = makeSessionRequest(server.CommandNames.Change, { + const addFile2ImportRequest = makeSessionRequest(CommandNames.Change, { file: file1Consumer1.path, line: 1, offset: 1, @@ -167,7 +169,7 @@ namespace ts.projectSystem { session.executeCommand(addFile2ImportRequest); // Change the content of file1 to `export var T2: string;export var T: number;export function Foo() { };` - const changeModuleFile1ShapeRequest2 = makeSessionRequest(server.CommandNames.Change, { + const changeModuleFile1ShapeRequest2 = makeSessionRequest(CommandNames.Change, { file: moduleFile1.path, line: 1, offset: 1, @@ -272,7 +274,7 @@ namespace ts.projectSystem { const session = new server.Session(host, nullCancellationToken, /*useSingleInferredProject*/ false, typingsInstaller, Utils.byteLength, process.hrtime, nullLogger, /*canUseEvents*/ false); openFilesForSession([globalFile3], session); - const changeGlobalFile3ShapeRequest = makeSessionRequest(server.CommandNames.Change, { + const changeGlobalFile3ShapeRequest = makeSessionRequest(CommandNames.Change, { file: globalFile3.path, line: 1, offset: 1, @@ -283,7 +285,7 @@ namespace ts.projectSystem { // check after file1 shape changes session.executeCommand(changeGlobalFile3ShapeRequest); - const globalFile3FileListRequest = makeSessionRequest(server.CommandNames.CompileOnSaveAffectedFileList, { file: globalFile3.path }); + const globalFile3FileListRequest = makeSessionRequest(CommandNames.CompileOnSaveAffectedFileList, { file: globalFile3.path }); sendAffectedFileRequestAndCheckResult(session, globalFile3FileListRequest, [{ projectFileName: configFile.path, files: [moduleFile1, file1Consumer1, file1Consumer2, globalFile3, moduleFile2] }]); }); @@ -316,7 +318,7 @@ namespace ts.projectSystem { const session = new server.Session(host, nullCancellationToken, /*useSingleInferredProject*/ false, typingsInstaller, Utils.byteLength, process.hrtime, nullLogger, /*canUseEvents*/ false); openFilesForSession([moduleFile1], session); - const file1ChangeShapeRequest = makeSessionRequest(server.CommandNames.Change, { + const file1ChangeShapeRequest = makeSessionRequest(CommandNames.Change, { file: moduleFile1.path, line: 1, offset: 27, @@ -345,7 +347,7 @@ namespace ts.projectSystem { const session = new server.Session(host, nullCancellationToken, /*useSingleInferredProject*/ false, typingsInstaller, Utils.byteLength, process.hrtime, nullLogger, /*canUseEvents*/ false); openFilesForSession([moduleFile1], session); - const file1ChangeShapeRequest = makeSessionRequest(server.CommandNames.Change, { + const file1ChangeShapeRequest = makeSessionRequest(CommandNames.Change, { file: moduleFile1.path, line: 1, offset: 27, @@ -369,7 +371,7 @@ namespace ts.projectSystem { openFilesForSession([moduleFile1, file1Consumer1], session); sendAffectedFileRequestAndCheckResult(session, moduleFile1FileListRequest, [{ projectFileName: configFile.path, files: [moduleFile1, file1Consumer1, file1Consumer1Consumer1] }]); - const changeFile1Consumer1ShapeRequest = makeSessionRequest(server.CommandNames.Change, { + const changeFile1Consumer1ShapeRequest = makeSessionRequest(CommandNames.Change, { file: file1Consumer1.path, line: 2, offset: 1, @@ -400,7 +402,7 @@ namespace ts.projectSystem { const session = new server.Session(host, nullCancellationToken, /*useSingleInferredProject*/ false, typingsInstaller, Utils.byteLength, process.hrtime, nullLogger, /*canUseEvents*/ false); openFilesForSession([file1, file2], session); - const file1AffectedListRequest = makeSessionRequest(server.CommandNames.CompileOnSaveAffectedFileList, { file: file1.path }); + const file1AffectedListRequest = makeSessionRequest(CommandNames.CompileOnSaveAffectedFileList, { file: file1.path }); sendAffectedFileRequestAndCheckResult(session, file1AffectedListRequest, [{ projectFileName: configFile.path, files: [file1, file2] }]); }); @@ -415,7 +417,7 @@ namespace ts.projectSystem { const session = createSession(host); openFilesForSession([file1, file2, file3], session); - const file1AffectedListRequest = makeSessionRequest(server.CommandNames.CompileOnSaveAffectedFileList, { file: file1.path }); + const file1AffectedListRequest = makeSessionRequest(CommandNames.CompileOnSaveAffectedFileList, { file: file1.path }); sendAffectedFileRequestAndCheckResult(session, file1AffectedListRequest, [ { projectFileName: configFile1.path, files: [file1, file2] }, @@ -437,11 +439,11 @@ namespace ts.projectSystem { host.reloadFS([referenceFile1, configFile]); host.triggerFileWatcherCallback(moduleFile1.path, /*removed*/ true); - const request = makeSessionRequest(server.CommandNames.CompileOnSaveAffectedFileList, { file: referenceFile1.path }); + const request = makeSessionRequest(CommandNames.CompileOnSaveAffectedFileList, { file: referenceFile1.path }); sendAffectedFileRequestAndCheckResult(session, request, [ { projectFileName: configFile.path, files: [referenceFile1] } ]); - const requestForMissingFile = makeSessionRequest(server.CommandNames.CompileOnSaveAffectedFileList, { file: moduleFile1.path }); + const requestForMissingFile = makeSessionRequest(CommandNames.CompileOnSaveAffectedFileList, { file: moduleFile1.path }); sendAffectedFileRequestAndCheckResult(session, requestForMissingFile, []); }); @@ -456,7 +458,7 @@ namespace ts.projectSystem { const session = createSession(host); openFilesForSession([referenceFile1], session); - const request = makeSessionRequest(server.CommandNames.CompileOnSaveAffectedFileList, { file: referenceFile1.path }); + const request = makeSessionRequest(CommandNames.CompileOnSaveAffectedFileList, { file: referenceFile1.path }); sendAffectedFileRequestAndCheckResult(session, request, [ { projectFileName: configFile.path, files: [referenceFile1] } ]); @@ -483,7 +485,7 @@ namespace ts.projectSystem { const session = new server.Session(host, nullCancellationToken, /*useSingleInferredProject*/ false, typingsInstaller, Utils.byteLength, process.hrtime, nullLogger, /*canUseEvents*/ false); openFilesForSession([file1, file2], session); - const compileFileRequest = makeSessionRequest(server.CommandNames.CompileOnSaveEmitFile, { file: file1.path, projectFileName: configFile.path }); + const compileFileRequest = makeSessionRequest(CommandNames.CompileOnSaveEmitFile, { file: file1.path, projectFileName: configFile.path }); session.executeCommand(compileFileRequest); const expectedEmittedFileName = "/a/b/f1.js"; diff --git a/src/harness/unittests/convertCompilerOptionsFromJson.ts b/src/harness/unittests/convertCompilerOptionsFromJson.ts index 33cad7130c76c..a2174c17e4157 100644 --- a/src/harness/unittests/convertCompilerOptionsFromJson.ts +++ b/src/harness/unittests/convertCompilerOptionsFromJson.ts @@ -176,7 +176,7 @@ namespace ts { file: undefined, start: 0, length: 0, - messageText: "Argument for '--target' option must be: 'es3', 'es5', 'es6', 'es2015'", + messageText: "Argument for '--target' option must be: 'es3', 'es5', 'es6', 'es2015', 'es2016', 'es2017'", code: Diagnostics.Argument_for_0_option_must_be_Colon_1.code, category: Diagnostics.Argument_for_0_option_must_be_Colon_1.category }] diff --git a/src/harness/unittests/moduleResolution.ts b/src/harness/unittests/moduleResolution.ts index a3f8367bb4a08..c0124871928df 100644 --- a/src/harness/unittests/moduleResolution.ts +++ b/src/harness/unittests/moduleResolution.ts @@ -1018,7 +1018,7 @@ import b = require("./moduleB"); const files = [f1, f2, f3, f4]; const names = map(files, f => f.name); - const sourceFiles = arrayToMap(map(files, f => createSourceFile(f.name, f.content, ScriptTarget.ES6)), f => f.fileName); + const sourceFiles = arrayToMap(map(files, f => createSourceFile(f.name, f.content, ScriptTarget.ES2015)), f => f.fileName); const compilerHost: CompilerHost = { fileExists : fileName => sourceFiles.has(fileName), getSourceFile: fileName => sourceFiles.get(fileName), diff --git a/src/harness/unittests/transpile.ts b/src/harness/unittests/transpile.ts index 808a5df37c15d..35b4f80835024 100644 --- a/src/harness/unittests/transpile.ts +++ b/src/harness/unittests/transpile.ts @@ -253,6 +253,10 @@ var x = 0;`, { options: { compilerOptions: { allowUnusedLabels: true }, fileName: "input.js", reportDiagnostics: true } }); + transpilesCorrectly("Supports setting 'alwaysStrict'", "x;", { + options: { compilerOptions: { alwaysStrict: true }, fileName: "input.js", reportDiagnostics: true } + }); + transpilesCorrectly("Supports setting 'baseUrl'", "x;", { options: { compilerOptions: { baseUrl: "./folder/baseUrl" }, fileName: "input.js", reportDiagnostics: true } }); diff --git a/src/harness/unittests/tsserverProjectSystem.ts b/src/harness/unittests/tsserverProjectSystem.ts index 3faca6af7e471..f80bdededb72a 100644 --- a/src/harness/unittests/tsserverProjectSystem.ts +++ b/src/harness/unittests/tsserverProjectSystem.ts @@ -19,10 +19,8 @@ namespace ts.projectSystem { export interface PostExecAction { readonly requestKind: TI.RequestKind; - readonly error: Error; - readonly stdout: string; - readonly stderr: string; - readonly callback: (err: Error, stdout: string, stderr: string) => void; + readonly success: boolean; + readonly callback: TI.RequestCompletedAction; } export function notImplemented(): any { @@ -54,7 +52,7 @@ namespace ts.projectSystem { export class TestTypingsInstaller extends TI.TypingsInstaller implements server.ITypingsInstaller { protected projectService: server.ProjectService; constructor(readonly globalTypingsCacheLocation: string, throttleLimit: number, readonly installTypingHost: server.ServerHost, log?: TI.Log) { - super(globalTypingsCacheLocation, "npm", safeList.path, throttleLimit, log); + super(globalTypingsCacheLocation, safeList.path, throttleLimit, log); this.init(); } @@ -65,7 +63,7 @@ namespace ts.projectSystem { const actionsToRun = this.postExecActions; this.postExecActions = []; for (const action of actionsToRun) { - action.callback(action.error, action.stdout, action.stderr); + action.callback(action.success); } } @@ -85,7 +83,7 @@ namespace ts.projectSystem { return this.installTypingHost; } - runCommand(requestKind: TI.RequestKind, requestId: number, command: string, cwd: string, cb: (err: Error, stdout: string, stderr: string) => void): void { + executeRequest(requestKind: TI.RequestKind, requestId: number, args: string[], cwd: string, cb: TI.RequestCompletedAction): void { switch (requestKind) { case TI.NpmViewRequest: case TI.NpmInstallRequest: @@ -108,9 +106,7 @@ namespace ts.projectSystem { addPostExecAction(requestKind: TI.RequestKind, stdout: string | string[], cb: TI.RequestCompletedAction) { const out = typeof stdout === "string" ? stdout : createNpmPackageJsonString(stdout); const action: PostExecAction = { - error: undefined, - stdout: out, - stderr: "", + success: !!out, callback: cb, requestKind }; @@ -139,7 +135,7 @@ namespace ts.projectSystem { } export class TestServerEventManager { - private events: server.ProjectServiceEvent[] = []; + public events: server.ProjectServiceEvent[] = []; handler: server.ProjectServiceEventHandler = (event: server.ProjectServiceEvent) => { this.events.push(event); @@ -2281,6 +2277,14 @@ namespace ts.projectSystem { const session = createSession(host, /*typingsInstaller*/ undefined, serverEventManager.handler); openFilesForSession([file], session); serverEventManager.checkEventCountOfType("configFileDiag", 1); + + for (const event of serverEventManager.events) { + if (event.eventName === "configFileDiag") { + assert.equal(event.data.configFileName, configFile.path); + assert.equal(event.data.triggerFile, file.path); + return; + } + } }); it("are generated when the config file doesn't have errors", () => { @@ -2378,4 +2382,33 @@ namespace ts.projectSystem { assert.isTrue(errorResult.length === 0); }); }); + + describe("non-existing directories listed in config file input array", () => { + it("should be tolerated without crashing the server", () => { + const configFile = { + path: "/a/b/tsconfig.json", + content: `{ + "compilerOptions": {}, + "include": ["app/*", "test/**/*", "something"] + }` + }; + const file1 = { + path: "/a/b/file1.ts", + content: "let t = 10;" + }; + + const host = createServerHost([file1, configFile]); + const projectService = createProjectService(host); + projectService.openClientFile(file1.path); + host.runQueuedTimeoutCallbacks(); + checkNumberOfConfiguredProjects(projectService, 1); + checkNumberOfInferredProjects(projectService, 1); + + const configuredProject = projectService.configuredProjects[0]; + assert.isTrue(configuredProject.getFileNames().length == 0); + + const inferredProject = projectService.inferredProjects[0]; + assert.isTrue(inferredProject.containsFile(file1.path)); + }); + }); } \ No newline at end of file diff --git a/src/harness/unittests/typingsInstaller.ts b/src/harness/unittests/typingsInstaller.ts index d79a1a7723cb4..ebd0f0ef7920c 100644 --- a/src/harness/unittests/typingsInstaller.ts +++ b/src/harness/unittests/typingsInstaller.ts @@ -31,11 +31,11 @@ namespace ts.projectSystem { function executeCommand(self: Installer, host: TestServerHost, installedTypings: string[], typingFiles: FileOrFolder[], requestKind: TI.RequestKind, cb: TI.RequestCompletedAction): void { switch (requestKind) { case TI.NpmInstallRequest: - self.addPostExecAction(requestKind, installedTypings, (err, stdout, stderr) => { + self.addPostExecAction(requestKind, installedTypings, success => { for (const file of typingFiles) { host.createFileOrFolder(file, /*createParentDirectory*/ true); } - cb(err, stdout, stderr); + cb(success); }); break; case TI.NpmViewRequest: @@ -81,7 +81,7 @@ namespace ts.projectSystem { constructor() { super(host); } - runCommand(requestKind: TI.RequestKind, requestId: number, command: string, cwd: string, cb: server.typingsInstaller.RequestCompletedAction) { + executeRequest(requestKind: TI.RequestKind, requestId: number, args: string[], cwd: string, cb: server.typingsInstaller.RequestCompletedAction) { const installedTypings = ["@types/jquery"]; const typingFiles = [jquery]; executeCommand(this, host, installedTypings, typingFiles, requestKind, cb); @@ -125,7 +125,7 @@ namespace ts.projectSystem { constructor() { super(host); } - runCommand(requestKind: TI.RequestKind, requestId: number, command: string, cwd: string, cb: server.typingsInstaller.RequestCompletedAction) { + executeRequest(requestKind: TI.RequestKind, requestId: number, args: string[], cwd: string, cb: server.typingsInstaller.RequestCompletedAction) { const installedTypings = ["@types/jquery"]; const typingFiles = [jquery]; executeCommand(this, host, installedTypings, typingFiles, requestKind, cb); @@ -221,7 +221,7 @@ namespace ts.projectSystem { enqueueIsCalled = true; super.enqueueInstallTypingsRequest(project, typingOptions); } - runCommand(requestKind: TI.RequestKind, requestId: number, command: string, cwd: string, cb: TI.RequestCompletedAction): void { + executeRequest(requestKind: TI.RequestKind, requestId: number, args: string[], cwd: string, cb: TI.RequestCompletedAction): void { const installedTypings = ["@types/jquery"]; const typingFiles = [jquery]; executeCommand(this, host, installedTypings, typingFiles, requestKind, cb); @@ -275,7 +275,7 @@ namespace ts.projectSystem { constructor() { super(host); } - runCommand(requestKind: TI.RequestKind, requestId: number, command: string, cwd: string, cb: TI.RequestCompletedAction): void { + executeRequest(requestKind: TI.RequestKind, requestId: number, args: string[], cwd: string, cb: TI.RequestCompletedAction): void { const installedTypings = ["@types/lodash", "@types/react"]; const typingFiles = [lodash, react]; executeCommand(this, host, installedTypings, typingFiles, requestKind, cb); @@ -323,7 +323,7 @@ namespace ts.projectSystem { enqueueIsCalled = true; super.enqueueInstallTypingsRequest(project, typingOptions); } - runCommand(requestKind: TI.RequestKind, requestId: number, command: string, cwd: string, cb: TI.RequestCompletedAction): void { + executeRequest(requestKind: TI.RequestKind, requestId: number, args: string[], cwd: string, cb: TI.RequestCompletedAction): void { const installedTypings: string[] = []; const typingFiles: FileOrFolder[] = []; executeCommand(this, host, installedTypings, typingFiles, requestKind, cb); @@ -398,7 +398,7 @@ namespace ts.projectSystem { constructor() { super(host); } - runCommand(requestKind: TI.RequestKind, requestId: number, command: string, cwd: string, cb: TI.RequestCompletedAction): void { + executeRequest(requestKind: TI.RequestKind, requestId: number, args: string[], cwd: string, cb: TI.RequestCompletedAction): void { const installedTypings = ["@types/commander", "@types/express", "@types/jquery", "@types/moment"]; const typingFiles = [commander, express, jquery, moment]; executeCommand(this, host, installedTypings, typingFiles, requestKind, cb); @@ -477,7 +477,7 @@ namespace ts.projectSystem { constructor() { super(host, { throttleLimit: 3 }); } - runCommand(requestKind: TI.RequestKind, requestId: number, command: string, cwd: string, cb: TI.RequestCompletedAction): void { + executeRequest(requestKind: TI.RequestKind, requestId: number, args: string[], cwd: string, cb: TI.RequestCompletedAction): void { const installedTypings = ["@types/commander", "@types/express", "@types/jquery", "@types/moment", "@types/lodash"]; executeCommand(this, host, installedTypings, typingFiles, requestKind, cb); } @@ -567,10 +567,10 @@ namespace ts.projectSystem { constructor() { super(host, { throttleLimit: 3 }); } - runCommand(requestKind: TI.RequestKind, requestId: number, command: string, cwd: string, cb: TI.RequestCompletedAction): void { + executeRequest(requestKind: TI.RequestKind, requestId: number, args: string[], cwd: string, cb: TI.RequestCompletedAction): void { if (requestKind === TI.NpmInstallRequest) { let typingFiles: (FileOrFolder & { typings: string}) [] = []; - if (command.indexOf("commander") >= 0) { + if (args.indexOf("@types/commander") >= 0) { typingFiles = [commander, jquery, lodash, cordova]; } else { @@ -655,7 +655,7 @@ namespace ts.projectSystem { constructor() { super(host, { globalTypingsCacheLocation: "/tmp" }); } - runCommand(requestKind: TI.RequestKind, requestId: number, command: string, cwd: string, cb: server.typingsInstaller.RequestCompletedAction) { + executeRequest(requestKind: TI.RequestKind, requestId: number, args: string[], cwd: string, cb: server.typingsInstaller.RequestCompletedAction) { const installedTypings = ["@types/jquery"]; const typingFiles = [jqueryDTS]; executeCommand(this, host, installedTypings, typingFiles, requestKind, cb); @@ -701,7 +701,7 @@ namespace ts.projectSystem { constructor() { super(host, { globalTypingsCacheLocation: "/tmp" }); } - runCommand(requestKind: TI.RequestKind, requestId: number, command: string, cwd: string, cb: server.typingsInstaller.RequestCompletedAction) { + executeRequest(requestKind: TI.RequestKind, requestId: number, args: string[], cwd: string, cb: server.typingsInstaller.RequestCompletedAction) { const installedTypings = ["@types/jquery"]; const typingFiles = [jqueryDTS]; executeCommand(this, host, installedTypings, typingFiles, requestKind, cb); @@ -766,7 +766,7 @@ namespace ts.projectSystem { constructor() { super(host, { globalTypingsCacheLocation: "/tmp" }, { isEnabled: () => true, writeLine: msg => messages.push(msg) }); } - runCommand(requestKind: TI.RequestKind, requestId: number, command: string, cwd: string, cb: server.typingsInstaller.RequestCompletedAction) { + executeRequest(requestKind: TI.RequestKind, requestId: number, args: string[], cwd: string, cb: server.typingsInstaller.RequestCompletedAction) { assert(false, "runCommand should not be invoked"); } })(); diff --git a/src/lib/es2015.core.d.ts b/src/lib/es2015.core.d.ts index 5d570bb086630..356512ecf5848 100644 --- a/src/lib/es2015.core.d.ts +++ b/src/lib/es2015.core.d.ts @@ -201,7 +201,7 @@ interface NumberConstructor { /** * Returns true if passed value is finite. - * Unlike the global isFininte, Number.isFinite doesn't forcibly convert the parameter to a + * Unlike the global isFinite, Number.isFinite doesn't forcibly convert the parameter to a * number. Only finite values of the type number, result in true. * @param number A numeric value. */ diff --git a/src/lib/es5.d.ts b/src/lib/es5.d.ts index ccff01a87228e..2c457a432c6b8 100644 --- a/src/lib/es5.d.ts +++ b/src/lib/es5.d.ts @@ -244,6 +244,9 @@ interface Function { */ bind(this: Function, thisArg: any, ...argArray: any[]): any; + /** Returns a string representation of a function. */ + toString(): string; + prototype: any; readonly length: number; diff --git a/src/server/builder.ts b/src/server/builder.ts index bb898cdee9c8f..a47f1fb46120f 100644 --- a/src/server/builder.ts +++ b/src/server/builder.ts @@ -1,6 +1,5 @@ /// /// -/// /// /// diff --git a/src/server/client.ts b/src/server/client.ts index a97dc1c0e1caf..a9d0ff985eb86 100644 --- a/src/server/client.ts +++ b/src/server/client.ts @@ -1,7 +1,6 @@ /// namespace ts.server { - export interface SessionClientHost extends LanguageServiceHost { writeMessage(message: string): void; } diff --git a/src/server/editorServices.ts b/src/server/editorServices.ts index 8c96a5c4c8532..a4a6bf66de78d 100644 --- a/src/server/editorServices.ts +++ b/src/server/editorServices.ts @@ -1,6 +1,5 @@ /// /// -/// /// /// /// @@ -12,7 +11,7 @@ namespace ts.server { export const maxProgramSizeForNonTsFiles = 20 * 1024 * 1024; export type ProjectServiceEvent = - { eventName: "context", data: { project: Project, fileName: NormalizedPath } } | { eventName: "configFileDiag", data: { triggerFile?: string, configFileName: string, diagnostics: Diagnostic[] } }; + { eventName: "context", data: { project: Project, fileName: NormalizedPath } } | { eventName: "configFileDiag", data: { triggerFile: string, configFileName: string, diagnostics: Diagnostic[] } }; export interface ProjectServiceEventHandler { (event: ProjectServiceEvent): void; @@ -394,12 +393,12 @@ namespace ts.server { this.throttledOperations.schedule( project.configFileName, /*delay*/250, - () => this.handleChangeInSourceFileForConfiguredProject(project)); + () => this.handleChangeInSourceFileForConfiguredProject(project, fileName)); } - private handleChangeInSourceFileForConfiguredProject(project: ConfiguredProject) { + private handleChangeInSourceFileForConfiguredProject(project: ConfiguredProject, triggerFile: string) { const { projectOptions, configFileErrors } = this.convertConfigFileContentToProjectOptions(project.configFileName); - this.reportConfigFileDiagnostics(project.getProjectName(), configFileErrors); + this.reportConfigFileDiagnostics(project.getProjectName(), configFileErrors, triggerFile); const newRootFiles = projectOptions.files.map((f => this.getCanonicalFileName(f))); const currentRootFiles = project.getRootFiles().map((f => this.getCanonicalFileName(f))); @@ -436,7 +435,7 @@ namespace ts.server { } const { configFileErrors } = this.convertConfigFileContentToProjectOptions(fileName); - this.reportConfigFileDiagnostics(fileName, configFileErrors); + this.reportConfigFileDiagnostics(fileName, configFileErrors, fileName); this.logger.info(`Detected newly added tsconfig file: ${fileName}`); this.reloadProjects(); @@ -712,7 +711,7 @@ namespace ts.server { Debug.assert(!!parsedCommandLine.fileNames); if (parsedCommandLine.fileNames.length === 0) { - errors.push(createCompilerDiagnostic(Diagnostics.The_config_file_0_found_doesn_t_contain_any_source_files, configFilename)); + (errors || (errors = [])).push(createCompilerDiagnostic(Diagnostics.The_config_file_0_found_doesn_t_contain_any_source_files, configFilename)); return { success: false, configFileErrors: errors }; } @@ -759,7 +758,7 @@ namespace ts.server { return project; } - private reportConfigFileDiagnostics(configFileName: string, diagnostics: Diagnostic[], triggerFile?: string) { + private reportConfigFileDiagnostics(configFileName: string, diagnostics: Diagnostic[], triggerFile: string) { if (!this.eventHandler) { return; } diff --git a/src/server/protocol.d.ts b/src/server/protocol.ts similarity index 80% rename from src/server/protocol.d.ts rename to src/server/protocol.ts index 2f447934040b3..80623f05aec3a 100644 --- a/src/server/protocol.d.ts +++ b/src/server/protocol.ts @@ -1,7 +1,102 @@ /** * Declaration module describing the TypeScript Server protocol */ -declare namespace ts.server.protocol { +namespace ts.server.protocol { + export namespace CommandTypes { + export type Brace = "brace"; + /* @internal */ + export type BraceFull = "brace-full"; + export type BraceCompletion = "braceCompletion"; + export type Change = "change"; + export type Close = "close"; + export type Completions = "completions"; + /* @internal */ + export type CompletionsFull = "completions-full"; + export type CompletionDetails = "completionEntryDetails"; + export type CompileOnSaveAffectedFileList = "compileOnSaveAffectedFileList"; + export type CompileOnSaveEmitFile = "compileOnSaveEmitFile"; + export type Configure = "configure"; + export type Definition = "definition"; + /* @internal */ + export type DefinitionFull = "definition-full"; + export type Implementation = "implementation"; + /* @internal */ + export type ImplementationFull = "implementation-full"; + export type Exit = "exit"; + export type Format = "format"; + export type Formatonkey = "formatonkey"; + /* @internal */ + export type FormatFull = "format-full"; + /* @internal */ + export type FormatonkeyFull = "formatonkey-full"; + /* @internal */ + export type FormatRangeFull = "formatRange-full"; + export type Geterr = "geterr"; + export type GeterrForProject = "geterrForProject"; + export type SemanticDiagnosticsSync = "semanticDiagnosticsSync"; + export type SyntacticDiagnosticsSync = "syntacticDiagnosticsSync"; + export type NavBar = "navbar"; + /* @internal */ + export type NavBarFull = "navbar-full"; + export type Navto = "navto"; + /* @internal */ + export type NavtoFull = "navto-full"; + export type NavTree = "navtree"; + export type NavTreeFull = "navtree-full"; + export type Occurrences = "occurrences"; + export type DocumentHighlights = "documentHighlights"; + /* @internal */ + export type DocumentHighlightsFull = "documentHighlights-full"; + export type Open = "open"; + export type Quickinfo = "quickinfo"; + /* @internal */ + export type QuickinfoFull = "quickinfo-full"; + export type References = "references"; + /* @internal */ + export type ReferencesFull = "references-full"; + export type Reload = "reload"; + export type Rename = "rename"; + /* @internal */ + export type RenameInfoFull = "rename-full"; + /* @internal */ + export type RenameLocationsFull = "renameLocations-full"; + export type Saveto = "saveto"; + export type SignatureHelp = "signatureHelp"; + /* @internal */ + export type SignatureHelpFull = "signatureHelp-full"; + export type TypeDefinition = "typeDefinition"; + export type ProjectInfo = "projectInfo"; + export type ReloadProjects = "reloadProjects"; + export type Unknown = "unknown"; + export type OpenExternalProject = "openExternalProject"; + export type OpenExternalProjects = "openExternalProjects"; + export type CloseExternalProject = "closeExternalProject"; + /* @internal */ + export type SynchronizeProjectList = "synchronizeProjectList"; + /* @internal */ + export type ApplyChangedToOpenFiles = "applyChangedToOpenFiles"; + /* @internal */ + export type EncodedSemanticClassificationsFull = "encodedSemanticClassifications-full"; + /* @internal */ + export type Cleanup = "cleanup"; + /* @internal */ + export type OutliningSpans = "outliningSpans"; + export type TodoComments = "todoComments"; + export type Indentation = "indentation"; + export type DocCommentTemplate = "docCommentTemplate"; + /* @internal */ + export type CompilerOptionsDiagnosticsFull = "compilerOptionsDiagnostics-full"; + /* @internal */ + export type NameOrDottedNameSpan = "nameOrDottedNameSpan"; + /* @internal */ + export type BreakpointStatement = "breakpointStatement"; + export type CompilerOptionsForInferredProjects = "compilerOptionsForInferredProjects"; + export type GetCodeFixes = "getCodeFixes"; + /* @internal */ + export type GetCodeFixesFull = "getCodeFixes-full"; + export type GetSupportedCodeFixes = "getSupportedCodeFixes"; + } + /** * A TypeScript Server message */ @@ -36,6 +131,7 @@ declare namespace ts.server.protocol { * Request to reload the project structure for all the opened files */ export interface ReloadProjectsRequest extends Message { + command: CommandTypes.ReloadProjects; } /** @@ -98,10 +194,25 @@ declare namespace ts.server.protocol { projectFileName?: string; } + /** + * Requests a JS Doc comment template for a given position + */ + export interface DocCommentTemplateRequest extends FileLocationRequest { + command: CommandTypes.DocCommentTemplate; + } + + /** + * Response to DocCommentTemplateRequest + */ + export interface DocCommandTemplateResponse extends Response { + body?: TextInsertion; + } + /** * A request to get TODO comments from the file */ export interface TodoCommentRequest extends FileRequest { + command: CommandTypes.TodoComments; arguments: TodoCommentRequestArgs; } @@ -115,13 +226,58 @@ declare namespace ts.server.protocol { descriptors: TodoCommentDescriptor[]; } + /** + * Response for TodoCommentRequest request. + */ + export interface TodoCommentsResponse extends Response { + body?: TodoComment[]; + } + + /** + * Request to obtain outlining spans in file. + */ + /* @internal */ + export interface OutliningSpansRequest extends FileRequest { + command: CommandTypes.OutliningSpans; + } + + /** + * Response to OutliningSpansRequest request. + */ + /* @internal */ + export interface OutliningSpansResponse extends Response { + body?: OutliningSpan[]; + } + /** * A request to get indentation for a location in file */ export interface IndentationRequest extends FileLocationRequest { + command: CommandTypes.Indentation; arguments: IndentationRequestArgs; } + /** + * Response for IndentationRequest request. + */ + export interface IndentationResponse extends Response { + body?: IndentationResult; + } + + /** + * Indentation result representing where indentation should be placed + */ + export interface IndentationResult { + /** + * The base position in the document that the indent should be relative to + */ + position: number; + /** + * The number of columns the indent should be at relative to the position's column. + */ + indentation: number; + } + /** * Arguments for IndentationRequest request. */ @@ -147,6 +303,7 @@ declare namespace ts.server.protocol { * A request to get the project information of the current file. */ export interface ProjectInfoRequest extends Request { + command: CommandTypes.ProjectInfo; arguments: ProjectInfoRequestArgs; } @@ -223,16 +380,17 @@ declare namespace ts.server.protocol { /** * The line number for the request (1-based). */ - line?: number; + line: number; /** * The character offset (on the line) for the request (1-based). */ - offset?: number; + offset: number; /** * Position (can be specified instead of line/offset pair) */ + /* @internal */ position?: number; } @@ -240,6 +398,7 @@ declare namespace ts.server.protocol { * Request for the available codefixes at a specific position. */ export interface CodeFixRequest extends Request { + command: CommandTypes.GetCodeFixes; arguments: CodeFixRequestArgs; } @@ -250,31 +409,33 @@ declare namespace ts.server.protocol { /** * The line number for the request (1-based). */ - startLine?: number; + startLine: number; /** * The character offset (on the line) for the request (1-based). */ - startOffset?: number; + startOffset: number; /** * Position (can be specified instead of line/offset pair) */ + /* @internal */ startPosition?: number; /** * The line number for the request (1-based). */ - endLine?: number; + endLine: number; /** * The character offset (on the line) for the request (1-based). */ - endOffset?: number; + endOffset: number; /** * Position (can be specified instead of line/offset pair) */ + /* @internal */ endPosition?: number; /** @@ -283,6 +444,13 @@ declare namespace ts.server.protocol { errorCodes?: number[]; } + /** + * Response for GetCodeFixes request. + */ + export interface GetCodeFixesResponse extends Response { + body?: CodeAction[]; + } + /** * A request whose arguments specify a file location (file, line, col). */ @@ -291,16 +459,34 @@ declare namespace ts.server.protocol { } /** - * A request to get semantic diagnostics for a span in the file + * A request to get codes of supported code fixes. */ - export interface SemanticDiagnosticsRequest extends FileRequest { - arguments: SemanticDiagnosticsRequestArgs; + export interface GetSupportedCodeFixesRequest extends Request { + command: CommandTypes.GetSupportedCodeFixes; } /** - * Arguments for SemanticDiagnosticsRequest request. + * A response for GetSupportedCodeFixesRequest request. */ - export interface SemanticDiagnosticsRequestArgs extends FileRequestArgs { + export interface GetSupportedCodeFixesResponse extends Response { + /** + * List of error codes supported by the server. + */ + body?: string[]; + } + + /** + * A request to get encoded semantic classifications for a span in the file + */ + /** @internal */ + export interface EncodedSemanticClassificationsRequest extends FileRequest { + arguments: EncodedSemanticClassificationsRequestArgs; + } + + /** + * Arguments for EncodedSemanticClassificationsRequest request. + */ + export interface EncodedSemanticClassificationsRequestArgs extends FileRequestArgs { /** * Start position of the span. */ @@ -328,6 +514,7 @@ declare namespace ts.server.protocol { * define the symbol found in file at location line, col. */ export interface DefinitionRequest extends FileLocationRequest { + command: CommandTypes.Definition; } /** @@ -336,6 +523,7 @@ declare namespace ts.server.protocol { * define the type for the symbol found in file at location line, col. */ export interface TypeDefinitionRequest extends FileLocationRequest { + command: CommandTypes.TypeDefinition; } /** @@ -344,6 +532,7 @@ declare namespace ts.server.protocol { * implement the symbol found in file at location line, col. */ export interface ImplementationRequest extends FileLocationRequest { + command: CommandTypes.Implementation; } /** @@ -404,6 +593,7 @@ declare namespace ts.server.protocol { * Request to get brace completion for a location in the file. */ export interface BraceCompletionRequest extends FileLocationRequest { + command: CommandTypes.BraceCompletion; arguments: BraceCompletionRequestArgs; } @@ -423,6 +613,7 @@ declare namespace ts.server.protocol { * in the file at a given line and column. */ export interface OccurrencesRequest extends FileLocationRequest { + command: CommandTypes.Occurrences; } export interface OccurrencesResponseItem extends FileSpan { @@ -442,6 +633,7 @@ declare namespace ts.server.protocol { * in the file at a given line and column. */ export interface DocumentHighlightsRequest extends FileLocationRequest { + command: CommandTypes.DocumentHighlights; arguments: DocumentHighlightsRequestArgs; } @@ -481,6 +673,7 @@ declare namespace ts.server.protocol { * reference the symbol found in file at location line, col. */ export interface ReferencesRequest extends FileLocationRequest { + command: CommandTypes.References; } export interface ReferencesResponseItem extends FileSpan { @@ -555,6 +748,7 @@ declare namespace ts.server.protocol { * name of the symbol so that client can print it unambiguously. */ export interface RenameRequest extends FileLocationRequest { + command: CommandTypes.Rename; arguments: RenameRequestArgs; } @@ -754,6 +948,7 @@ declare namespace ts.server.protocol { /** * Represents set of changes in open file */ + /* @internal */ export interface ChangedOpenFile { /** * Name of file @@ -765,65 +960,6 @@ declare namespace ts.server.protocol { changes: ts.TextChange[]; } - /** - * Editor options - */ - export interface EditorOptions { - - /** Number of spaces for each tab. Default value is 4. */ - tabSize?: number; - - /** Number of spaces to indent during formatting. Default value is 4. */ - indentSize?: number; - - /** Number of additional spaces to indent during formatting to preserve base indentation (ex. script block indentation). Default value is 0. */ - baseIndentSize?: number; - - /** The new line character to be used. Default value is the OS line delimiter. */ - newLineCharacter?: string; - - /** Whether tabs should be converted to spaces. Default value is true. */ - convertTabsToSpaces?: boolean; - } - - /** - * Format options - */ - export interface FormatOptions extends EditorOptions { - - /** Defines space handling after a comma delimiter. Default value is true. */ - insertSpaceAfterCommaDelimiter?: boolean; - - /** Defines space handling after a semicolon in a for statement. Default value is true */ - insertSpaceAfterSemicolonInForStatements?: boolean; - - /** Defines space handling after a binary operator. Default value is true. */ - insertSpaceBeforeAndAfterBinaryOperators?: boolean; - - /** Defines space handling after keywords in control flow statement. Default value is true. */ - insertSpaceAfterKeywordsInControlFlowStatements?: boolean; - - /** Defines space handling after function keyword for anonymous functions. Default value is false. */ - insertSpaceAfterFunctionKeywordForAnonymousFunctions?: boolean; - - /** Defines space handling after opening and before closing non empty parenthesis. Default value is false. */ - insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis?: boolean; - - /** Defines space handling after opening and before closing non empty brackets. Default value is false. */ - insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets?: boolean; - - /** Defines space handling before and after template string braces. Default value is false. */ - insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces?: boolean; - - /** Defines space handling before and after JSX expression braces. Default value is false. */ - insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces?: boolean; - - /** Defines whether an open brace is put onto a new line for functions or not. Default value is false. */ - placeOpenBraceOnNewLineForFunctions?: boolean; - - /** Defines whether an open brace is put onto a new line for control blocks or not. Default value is false. */ - placeOpenBraceOnNewLineForControlBlocks?: boolean; - } /** * Information found in a configure request. @@ -844,12 +980,7 @@ declare namespace ts.server.protocol { /** * The format options to use during formatting and other code editing features. */ - formatOptions?: FormatOptions; - - /** - * If set to true - then all loose files will land into one inferred project - */ - useOneInferredProject?: boolean; + formatOptions?: FormatCodeSettings; } /** @@ -857,6 +988,7 @@ declare namespace ts.server.protocol { * host information, such as host type, tab size, and indent size. */ export interface ConfigureRequest extends Request { + command: CommandTypes.Configure; arguments: ConfigureRequestArguments; } @@ -892,6 +1024,7 @@ declare namespace ts.server.protocol { * send a response to an open request. */ export interface OpenRequest extends Request { + command: CommandTypes.Open; arguments: OpenRequestArgs; } @@ -899,18 +1032,20 @@ declare namespace ts.server.protocol { * Request to open or update external project */ export interface OpenExternalProjectRequest extends Request { + command: CommandTypes.OpenExternalProject; arguments: OpenExternalProjectArgs; } /** * Arguments to OpenExternalProjectRequest request */ - type OpenExternalProjectArgs = ExternalProject; + export type OpenExternalProjectArgs = ExternalProject; /** * Request to open multiple external projects */ export interface OpenExternalProjectsRequest extends Request { + command: CommandTypes.OpenExternalProjects; arguments: OpenExternalProjectsArgs; } @@ -924,10 +1059,25 @@ declare namespace ts.server.protocol { projects: ExternalProject[]; } + /** + * Response to OpenExternalProjectRequest request. This is just an acknowledgement, so + * no body field is required. + */ + export interface OpenExternalProjectResponse extends Response { + } + + /** + * Response to OpenExternalProjectsRequest request. This is just an acknowledgement, so + * no body field is required. + */ + export interface OpenExternalProjectsResponse extends Response { + } + /** * Request to close external project. */ export interface CloseExternalProjectRequest extends Request { + command: CommandTypes.CloseExternalProject; arguments: CloseExternalProjectRequestArgs; } @@ -941,9 +1091,17 @@ declare namespace ts.server.protocol { projectFileName: string; } + /** + * Response to CloseExternalProjectRequest request. This is just an acknowledgement, so + * no body field is required. + */ + export interface CloseExternalProjectResponse extends Response { + } + /** * Request to check if given list of projects is up-to-date and synchronize them if necessary */ + /* @internal */ export interface SynchronizeProjectListRequest extends Request { arguments: SynchronizeProjectListRequestArgs; } @@ -961,6 +1119,7 @@ declare namespace ts.server.protocol { /** * Request to synchronize list of open files with the client */ + /* @internal */ export interface ApplyChangedToOpenFilesRequest extends Request { arguments: ApplyChangedToOpenFilesRequestArgs; } @@ -968,6 +1127,7 @@ declare namespace ts.server.protocol { /** * Arguments to ApplyChangedToOpenFilesRequest */ + /* @internal */ export interface ApplyChangedToOpenFilesRequestArgs { /** * List of newly open files @@ -993,6 +1153,7 @@ declare namespace ts.server.protocol { * or all open loose files and its transitive closure of referenced files if 'useOneInferredProject' is true. */ export interface SetCompilerOptionsForInferredProjectsRequest extends Request { + command: CommandTypes.CompilerOptionsForInferredProjects; arguments: SetCompilerOptionsForInferredProjectsArgs; } @@ -1006,11 +1167,19 @@ declare namespace ts.server.protocol { options: ExternalProjectCompilerOptions; } + /** + * Response to SetCompilerOptionsForInferredProjectsResponse request. This is just an acknowledgement, so + * no body field is required. + */ + export interface SetCompilerOptionsForInferredProjectsResponse extends Response { + } + /** * Exit request; value of command field is "exit". Ask the server process * to exit. */ export interface ExitRequest extends Request { + command: CommandTypes.Exit; } /** @@ -1021,6 +1190,7 @@ declare namespace ts.server.protocol { * currently send a response to a close request. */ export interface CloseRequest extends FileRequest { + command: CommandTypes.Close; } /** @@ -1028,6 +1198,7 @@ declare namespace ts.server.protocol { * NOTE: this us query-only operation and does not generate any output on disk. */ export interface CompileOnSaveAffectedFileListRequest extends FileRequest { + command: CommandTypes.CompileOnSaveAffectedFileList; } /** @@ -1055,7 +1226,8 @@ declare namespace ts.server.protocol { * Request to recompile the file. All generated outputs (.js, .d.ts or .js.map files) is written on disk. */ export interface CompileOnSaveEmitFileRequest extends FileRequest { - args: CompileOnSaveEmitFileRequestArgs; + command: CommandTypes.CompileOnSaveEmitFile; + arguments: CompileOnSaveEmitFileRequestArgs; } /** @@ -1075,6 +1247,7 @@ declare namespace ts.server.protocol { * line, col. */ export interface QuickInfoRequest extends FileLocationRequest { + command: CommandTypes.Quickinfo; } /** @@ -1136,12 +1309,12 @@ declare namespace ts.server.protocol { /** * End position of the range for which to format text in file. */ + /* @internal */ endPosition?: number; - /** * Format options to be used. */ - options?: ts.FormatCodeOptions; + options?: FormatCodeSettings; } /** @@ -1152,6 +1325,7 @@ declare namespace ts.server.protocol { * reformatted text. */ export interface FormatRequest extends FileLocationRequest { + command: CommandTypes.Format; arguments: FormatRequestArgs; } @@ -1213,7 +1387,7 @@ declare namespace ts.server.protocol { */ key: string; - options?: ts.FormatCodeOptions; + options?: FormatCodeSettings; } /** @@ -1225,6 +1399,7 @@ declare namespace ts.server.protocol { * reformatted text. */ export interface FormatOnKeyRequest extends FileLocationRequest { + command: CommandTypes.Formatonkey; arguments: FormatOnKeyRequestArgs; } @@ -1245,6 +1420,7 @@ declare namespace ts.server.protocol { * begin with prefix. */ export interface CompletionsRequest extends FileLocationRequest { + command: CommandTypes.Completions; arguments: CompletionsRequestArgs; } @@ -1265,6 +1441,7 @@ declare namespace ts.server.protocol { * detailed information for each completion entry. */ export interface CompletionDetailsRequest extends FileLocationRequest { + command: CommandTypes.CompletionDetails; arguments: CompletionDetailsRequestArgs; } @@ -1451,6 +1628,7 @@ declare namespace ts.server.protocol { * help. */ export interface SignatureHelpRequest extends FileLocationRequest { + command: CommandTypes.SignatureHelp; arguments: SignatureHelpRequestArgs; } @@ -1465,6 +1643,7 @@ declare namespace ts.server.protocol { * Synchronous request for semantic diagnostics of one file. */ export interface SemanticDiagnosticsSyncRequest extends FileRequest { + command: CommandTypes.SemanticDiagnosticsSync; arguments: SemanticDiagnosticsSyncRequestArgs; } @@ -1483,6 +1662,7 @@ declare namespace ts.server.protocol { * Synchronous request for syntactic diagnostics of one file. */ export interface SyntacticDiagnosticsSyncRequest extends FileRequest { + command: CommandTypes.SyntacticDiagnosticsSync; arguments: SyntacticDiagnosticsSyncRequestArgs; } @@ -1519,6 +1699,7 @@ declare namespace ts.server.protocol { * it request for every file in this project. */ export interface GeterrForProjectRequest extends Request { + command: CommandTypes.GeterrForProject; arguments: GeterrForProjectRequestArgs; } @@ -1550,6 +1731,7 @@ declare namespace ts.server.protocol { * file that is currently visible, in most-recently-used order. */ export interface GeterrRequest extends Request { + command: CommandTypes.Geterr; arguments: GeterrRequestArgs; } @@ -1642,11 +1824,12 @@ declare namespace ts.server.protocol { * The two names can be identical. */ export interface ReloadRequest extends FileRequest { + command: CommandTypes.Reload; arguments: ReloadRequestArgs; } /** - * Response to "reload" request. This is just an acknowledgement, so + * Response to "reload" request. This is just an acknowledgement, so * no body field is required. */ export interface ReloadResponse extends Response { @@ -1671,6 +1854,7 @@ declare namespace ts.server.protocol { * "saveto" request. */ export interface SavetoRequest extends FileRequest { + command: CommandTypes.Saveto; arguments: SavetoRequestArgs; } @@ -1703,6 +1887,7 @@ declare namespace ts.server.protocol { * context for the search is given by the named file. */ export interface NavtoRequest extends FileRequest { + command: CommandTypes.Navto; arguments: NavtoRequestArgs; } @@ -1786,6 +1971,7 @@ declare namespace ts.server.protocol { * Server does not currently send a response to a change request. */ export interface ChangeRequest extends FileLocationRequest { + command: CommandTypes.Change; arguments: ChangeRequestArgs; } @@ -1802,6 +1988,7 @@ declare namespace ts.server.protocol { * found in file at location line, offset. */ export interface BraceRequest extends FileLocationRequest { + command: CommandTypes.Brace; } /** @@ -1810,6 +1997,7 @@ declare namespace ts.server.protocol { * extracted from the requested file. */ export interface NavBarRequest extends FileRequest { + command: CommandTypes.NavBar; } /** @@ -1817,6 +2005,7 @@ declare namespace ts.server.protocol { * Return response giving the navigation tree of the requested file. */ export interface NavTreeRequest extends FileRequest { + command: CommandTypes.NavTree; } export interface NavigationBarItem { diff --git a/src/server/scriptInfo.ts b/src/server/scriptInfo.ts index e974f28c1155a..cb584c3bc8d6c 100644 --- a/src/server/scriptInfo.ts +++ b/src/server/scriptInfo.ts @@ -91,7 +91,7 @@ namespace ts.server { return this.containingProjects[0]; } - setFormatOptions(formatSettings: protocol.FormatOptions): void { + setFormatOptions(formatSettings: FormatCodeSettings): void { if (formatSettings) { if (!this.formatCodeSettings) { this.formatCodeSettings = getDefaultFormatCodeSettings(this.host); diff --git a/src/server/scriptVersionCache.ts b/src/server/scriptVersionCache.ts index 1508187c13e72..8d0efa081adaf 100644 --- a/src/server/scriptVersionCache.ts +++ b/src/server/scriptVersionCache.ts @@ -1,6 +1,5 @@ /// /// -/// /// namespace ts.server { diff --git a/src/server/server.ts b/src/server/server.ts index d59100bb57c39..e728d7e9d723c 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -265,13 +265,16 @@ namespace ts.server { installerEventPort: number, canUseEvents: boolean, useSingleInferredProject: boolean, + disableAutomaticTypingAcquisition: boolean, globalTypingsCacheLocation: string, logger: server.Logger) { super( host, cancellationToken, useSingleInferredProject, - new NodeTypingsInstaller(logger, installerEventPort, globalTypingsCacheLocation, host.newLine), + disableAutomaticTypingAcquisition + ? nullTypingsInstaller + : new NodeTypingsInstaller(logger, installerEventPort, globalTypingsCacheLocation, host.newLine), Buffer.byteLength, process.hrtime, logger, @@ -512,12 +515,14 @@ namespace ts.server { } const useSingleInferredProject = sys.args.indexOf("--useSingleInferredProject") >= 0; + const disableAutomaticTypingAcquisition = sys.args.indexOf("--disableAutomaticTypingAcquisition") >= 0; const ioSession = new IOSession( sys, cancellationToken, eventPort, /*canUseEvents*/ eventPort === undefined, useSingleInferredProject, + disableAutomaticTypingAcquisition, getGlobalTypingsCacheLocation(), logger); process.on("uncaughtException", function (err: Error) { diff --git a/src/server/session.ts b/src/server/session.ts index bab8c2a1431ac..9a29a8e60d4ea 100644 --- a/src/server/session.ts +++ b/src/server/session.ts @@ -1,6 +1,6 @@ /// /// -/// +/// /// namespace ts.server { @@ -83,74 +83,74 @@ namespace ts.server { } export namespace CommandNames { - export const Brace = "brace"; - export const BraceFull = "brace-full"; - export const BraceCompletion = "braceCompletion"; - export const Change = "change"; - export const Close = "close"; - export const Completions = "completions"; - export const CompletionsFull = "completions-full"; - export const CompletionDetails = "completionEntryDetails"; - export const CompileOnSaveAffectedFileList = "compileOnSaveAffectedFileList"; - export const CompileOnSaveEmitFile = "compileOnSaveEmitFile"; - export const Configure = "configure"; - export const Definition = "definition"; - export const DefinitionFull = "definition-full"; - export const Exit = "exit"; - export const Format = "format"; - export const Formatonkey = "formatonkey"; - export const FormatFull = "format-full"; - export const FormatonkeyFull = "formatonkey-full"; - export const FormatRangeFull = "formatRange-full"; - export const Geterr = "geterr"; - export const GeterrForProject = "geterrForProject"; - export const Implementation = "implementation"; - export const ImplementationFull = "implementation-full"; - export const SemanticDiagnosticsSync = "semanticDiagnosticsSync"; - export const SyntacticDiagnosticsSync = "syntacticDiagnosticsSync"; - export const NavBar = "navbar"; - export const NavBarFull = "navbar-full"; - export const NavTree = "navtree"; - export const NavTreeFull = "navtree-full"; - export const Navto = "navto"; - export const NavtoFull = "navto-full"; - export const Occurrences = "occurrences"; - export const DocumentHighlights = "documentHighlights"; - export const DocumentHighlightsFull = "documentHighlights-full"; - export const Open = "open"; - export const Quickinfo = "quickinfo"; - export const QuickinfoFull = "quickinfo-full"; - export const References = "references"; - export const ReferencesFull = "references-full"; - export const Reload = "reload"; - export const Rename = "rename"; - export const RenameInfoFull = "rename-full"; - export const RenameLocationsFull = "renameLocations-full"; - export const Saveto = "saveto"; - export const SignatureHelp = "signatureHelp"; - export const SignatureHelpFull = "signatureHelp-full"; - export const TypeDefinition = "typeDefinition"; - export const ProjectInfo = "projectInfo"; - export const ReloadProjects = "reloadProjects"; - export const Unknown = "unknown"; - export const OpenExternalProject = "openExternalProject"; - export const OpenExternalProjects = "openExternalProjects"; - export const CloseExternalProject = "closeExternalProject"; - export const SynchronizeProjectList = "synchronizeProjectList"; - export const ApplyChangedToOpenFiles = "applyChangedToOpenFiles"; - export const EncodedSemanticClassificationsFull = "encodedSemanticClassifications-full"; - export const Cleanup = "cleanup"; - export const OutliningSpans = "outliningSpans"; - export const TodoComments = "todoComments"; - export const Indentation = "indentation"; - export const DocCommentTemplate = "docCommentTemplate"; - export const CompilerOptionsDiagnosticsFull = "compilerOptionsDiagnostics-full"; - export const NameOrDottedNameSpan = "nameOrDottedNameSpan"; - export const BreakpointStatement = "breakpointStatement"; - export const CompilerOptionsForInferredProjects = "compilerOptionsForInferredProjects"; - export const GetCodeFixes = "getCodeFixes"; - export const GetCodeFixesFull = "getCodeFixes-full"; - export const GetSupportedCodeFixes = "getSupportedCodeFixes"; + export const Brace: protocol.CommandTypes.Brace = "brace"; + export const BraceFull: protocol.CommandTypes.BraceFull = "brace-full"; + export const BraceCompletion: protocol.CommandTypes.BraceCompletion = "braceCompletion"; + export const Change: protocol.CommandTypes.Change = "change"; + export const Close: protocol.CommandTypes.Close = "close"; + export const Completions: protocol.CommandTypes.Completions = "completions"; + export const CompletionsFull: protocol.CommandTypes.CompletionsFull = "completions-full"; + export const CompletionDetails: protocol.CommandTypes.CompletionDetails = "completionEntryDetails"; + export const CompileOnSaveAffectedFileList: protocol.CommandTypes.CompileOnSaveAffectedFileList = "compileOnSaveAffectedFileList"; + export const CompileOnSaveEmitFile: protocol.CommandTypes.CompileOnSaveEmitFile = "compileOnSaveEmitFile"; + export const Configure: protocol.CommandTypes.Configure = "configure"; + export const Definition: protocol.CommandTypes.Definition = "definition"; + export const DefinitionFull: protocol.CommandTypes.DefinitionFull = "definition-full"; + export const Exit: protocol.CommandTypes.Exit = "exit"; + export const Format: protocol.CommandTypes.Format = "format"; + export const Formatonkey: protocol.CommandTypes.Formatonkey = "formatonkey"; + export const FormatFull: protocol.CommandTypes.FormatFull = "format-full"; + export const FormatonkeyFull: protocol.CommandTypes.FormatonkeyFull = "formatonkey-full"; + export const FormatRangeFull: protocol.CommandTypes.FormatRangeFull = "formatRange-full"; + export const Geterr: protocol.CommandTypes.Geterr = "geterr"; + export const GeterrForProject: protocol.CommandTypes.GeterrForProject = "geterrForProject"; + export const Implementation: protocol.CommandTypes.Implementation = "implementation"; + export const ImplementationFull: protocol.CommandTypes.ImplementationFull = "implementation-full"; + export const SemanticDiagnosticsSync: protocol.CommandTypes.SemanticDiagnosticsSync = "semanticDiagnosticsSync"; + export const SyntacticDiagnosticsSync: protocol.CommandTypes.SyntacticDiagnosticsSync = "syntacticDiagnosticsSync"; + export const NavBar: protocol.CommandTypes.NavBar = "navbar"; + export const NavBarFull: protocol.CommandTypes.NavBarFull = "navbar-full"; + export const NavTree: protocol.CommandTypes.NavTree = "navtree"; + export const NavTreeFull: protocol.CommandTypes.NavTreeFull = "navtree-full"; + export const Navto: protocol.CommandTypes.Navto = "navto"; + export const NavtoFull: protocol.CommandTypes.NavtoFull = "navto-full"; + export const Occurrences: protocol.CommandTypes.Occurrences = "occurrences"; + export const DocumentHighlights: protocol.CommandTypes.DocumentHighlights = "documentHighlights"; + export const DocumentHighlightsFull: protocol.CommandTypes.DocumentHighlightsFull = "documentHighlights-full"; + export const Open: protocol.CommandTypes.Open = "open"; + export const Quickinfo: protocol.CommandTypes.Quickinfo = "quickinfo"; + export const QuickinfoFull: protocol.CommandTypes.QuickinfoFull = "quickinfo-full"; + export const References: protocol.CommandTypes.References = "references"; + export const ReferencesFull: protocol.CommandTypes.ReferencesFull = "references-full"; + export const Reload: protocol.CommandTypes.Reload = "reload"; + export const Rename: protocol.CommandTypes.Rename = "rename"; + export const RenameInfoFull: protocol.CommandTypes.RenameInfoFull = "rename-full"; + export const RenameLocationsFull: protocol.CommandTypes.RenameLocationsFull = "renameLocations-full"; + export const Saveto: protocol.CommandTypes.Saveto = "saveto"; + export const SignatureHelp: protocol.CommandTypes.SignatureHelp = "signatureHelp"; + export const SignatureHelpFull: protocol.CommandTypes.SignatureHelpFull = "signatureHelp-full"; + export const TypeDefinition: protocol.CommandTypes.TypeDefinition = "typeDefinition"; + export const ProjectInfo: protocol.CommandTypes.ProjectInfo = "projectInfo"; + export const ReloadProjects: protocol.CommandTypes.ReloadProjects = "reloadProjects"; + export const Unknown: protocol.CommandTypes.Unknown = "unknown"; + export const OpenExternalProject: protocol.CommandTypes.OpenExternalProject = "openExternalProject"; + export const OpenExternalProjects: protocol.CommandTypes.OpenExternalProjects = "openExternalProjects"; + export const CloseExternalProject: protocol.CommandTypes.CloseExternalProject = "closeExternalProject"; + export const SynchronizeProjectList: protocol.CommandTypes.SynchronizeProjectList = "synchronizeProjectList"; + export const ApplyChangedToOpenFiles: protocol.CommandTypes.ApplyChangedToOpenFiles = "applyChangedToOpenFiles"; + export const EncodedSemanticClassificationsFull: protocol.CommandTypes.EncodedSemanticClassificationsFull = "encodedSemanticClassifications-full"; + export const Cleanup: protocol.CommandTypes.Cleanup = "cleanup"; + export const OutliningSpans: protocol.CommandTypes.OutliningSpans = "outliningSpans"; + export const TodoComments: protocol.CommandTypes.TodoComments = "todoComments"; + export const Indentation: protocol.CommandTypes.Indentation = "indentation"; + export const DocCommentTemplate: protocol.CommandTypes.DocCommentTemplate = "docCommentTemplate"; + export const CompilerOptionsDiagnosticsFull: protocol.CommandTypes.CompilerOptionsDiagnosticsFull = "compilerOptionsDiagnostics-full"; + export const NameOrDottedNameSpan: protocol.CommandTypes.NameOrDottedNameSpan = "nameOrDottedNameSpan"; + export const BreakpointStatement: protocol.CommandTypes.BreakpointStatement = "breakpointStatement"; + export const CompilerOptionsForInferredProjects: protocol.CommandTypes.CompilerOptionsForInferredProjects = "compilerOptionsForInferredProjects"; + export const GetCodeFixes: protocol.CommandTypes.GetCodeFixes = "getCodeFixes"; + export const GetCodeFixesFull: protocol.CommandTypes.GetCodeFixesFull = "getCodeFixes-full"; + export const GetSupportedCodeFixes: protocol.CommandTypes.GetSupportedCodeFixes = "getSupportedCodeFixes"; } export function formatMessage(msg: T, logger: server.Logger, byteLength: (s: string, encoding: string) => number, newLine: string): string { @@ -363,7 +363,7 @@ namespace ts.server { } } - private getEncodedSemanticClassifications(args: protocol.SemanticDiagnosticsRequestArgs) { + private getEncodedSemanticClassifications(args: protocol.EncodedSemanticClassificationsRequestArgs) { const { file, project } = this.getFileAndProject(args); return project.getLanguageService().getEncodedSemanticClassifications(file, args); } @@ -761,7 +761,7 @@ namespace ts.server { if (this.eventHander) { this.eventHander({ eventName: "configFileDiag", - data: { fileName, configFileName, diagnostics: configFileErrors || [] } + data: { triggerFile: fileName, configFileName, diagnostics: configFileErrors || [] } }); } } @@ -967,7 +967,7 @@ namespace ts.server { result.push({ name, kind, kindModifiers, sortText, replacementSpan: convertedSpan }); } return result; - }, []).sort((a, b) => a.name.localeCompare(b.name)); + }, []).sort((a, b) => ts.compareStrings(a.name, b.name)); } else { return completions; @@ -1470,7 +1470,7 @@ namespace ts.server { [CommandNames.BraceCompletion]: (request: protocol.BraceCompletionRequest) => { return this.requiredResponse(this.isValidBraceCompletion(request.arguments)); }, - [CommandNames.DocCommentTemplate]: (request: protocol.FileLocationRequest) => { + [CommandNames.DocCommentTemplate]: (request: protocol.DocCommentTemplateRequest) => { return this.requiredResponse(this.getDocCommentTemplate(request.arguments)); }, [CommandNames.Format]: (request: protocol.FormatRequest) => { @@ -1512,7 +1512,7 @@ namespace ts.server { [CommandNames.CompilerOptionsDiagnosticsFull]: (request: protocol.CompilerOptionsDiagnosticsRequest) => { return this.requiredResponse(this.getCompilerOptionsDiagnostics(request.arguments)); }, - [CommandNames.EncodedSemanticClassificationsFull]: (request: protocol.SemanticDiagnosticsRequest) => { + [CommandNames.EncodedSemanticClassificationsFull]: (request: protocol.EncodedSemanticClassificationsRequest) => { return this.requiredResponse(this.getEncodedSemanticClassifications(request.arguments)); }, [CommandNames.Cleanup]: (request: protocol.Request) => { @@ -1590,7 +1590,8 @@ namespace ts.server { return this.requiredResponse(this.getDocumentHighlights(request.arguments, /*simplifiedResult*/ false)); }, [CommandNames.CompilerOptionsForInferredProjects]: (request: protocol.SetCompilerOptionsForInferredProjectsRequest) => { - return this.requiredResponse(this.setCompilerOptionsForInferredProjects(request.arguments)); + this.setCompilerOptionsForInferredProjects(request.arguments); + return this.requiredResponse(true); }, [CommandNames.ProjectInfo]: (request: protocol.ProjectInfoRequest) => { return this.requiredResponse(this.getProjectInfo(request.arguments)); diff --git a/src/server/tsconfig.json b/src/server/tsconfig.json index 7eb8c28f3834c..9f907446c0349 100644 --- a/src/server/tsconfig.json +++ b/src/server/tsconfig.json @@ -22,7 +22,7 @@ "typingsCache.ts", "project.ts", "editorServices.ts", - "protocol.d.ts", + "protocol.ts", "session.ts", "server.ts" ] diff --git a/src/server/typingsInstaller/nodeTypingsInstaller.ts b/src/server/typingsInstaller/nodeTypingsInstaller.ts index b73a8e4e79db3..ff643e885e432 100644 --- a/src/server/typingsInstaller/nodeTypingsInstaller.ts +++ b/src/server/typingsInstaller/nodeTypingsInstaller.ts @@ -33,23 +33,38 @@ namespace ts.server.typingsInstaller { } } - export class NodeTypingsInstaller extends TypingsInstaller { - private readonly exec: { (command: string, options: { cwd: string }, callback?: (error: Error, stdout: string, stderr: string) => void): any }; + type HttpGet = { + (url: string, callback: (response: HttpResponse) => void): NodeJS.EventEmitter; + }; + + interface HttpResponse extends NodeJS.ReadableStream { + statusCode: number; + statusMessage: string; + destroy(): void; + } + + type Exec = { + (command: string, options: { cwd: string }, callback?: (error: Error, stdout: string, stderr: string) => void): any + }; + export class NodeTypingsInstaller extends TypingsInstaller { + private readonly exec: Exec; + private readonly httpGet: HttpGet; + private readonly npmPath: string; readonly installTypingHost: InstallTypingHost = sys; constructor(globalTypingsCacheLocation: string, throttleLimit: number, log: Log) { super( globalTypingsCacheLocation, - /*npmPath*/ getNPMLocation(process.argv[0]), toPath("typingSafeList.json", __dirname, createGetCanonicalFileName(sys.useCaseSensitiveFileNames)), throttleLimit, log); if (this.log.isEnabled()) { this.log.writeLine(`Process id: ${process.pid}`); } - const { exec } = require("child_process"); - this.exec = exec; + this.npmPath = getNPMLocation(process.argv[0]); + this.exec = require("child_process").exec; + this.httpGet = require("http").get; } init() { @@ -75,17 +90,54 @@ namespace ts.server.typingsInstaller { } } - protected runCommand(requestKind: RequestKind, requestId: number, command: string, cwd: string, onRequestCompleted: RequestCompletedAction): void { + protected executeRequest(requestKind: RequestKind, requestId: number, args: string[], cwd: string, onRequestCompleted: RequestCompletedAction): void { if (this.log.isEnabled()) { - this.log.writeLine(`#${requestId} running command '${command}'.`); + this.log.writeLine(`#${requestId} executing ${requestKind}, arguments'${JSON.stringify(args)}'.`); + } + switch (requestKind) { + case NpmViewRequest: { + // const command = `${self.npmPath} view @types/${typing} --silent name`; + // use http request to global npm registry instead of running npm view + Debug.assert(args.length === 1); + const url = `http://registry.npmjs.org/@types%2f${args[0]}`; + const start = Date.now(); + this.httpGet(url, response => { + let ok = false; + if (this.log.isEnabled()) { + this.log.writeLine(`${requestKind} #${requestId} request to ${url}:: status code ${response.statusCode}, status message '${response.statusMessage}', took ${Date.now() - start} ms`); + } + switch (response.statusCode) { + case 200: // OK + case 301: // redirect - Moved - treat package as present + case 302: // redirect - Found - treat package as present + ok = true; + break; + } + response.destroy(); + onRequestCompleted(ok); + }).on("error", (err: Error) => { + if (this.log.isEnabled()) { + this.log.writeLine(`${requestKind} #${requestId} query to npm registry failed with error ${err.message}, stack ${err.stack}`); + } + onRequestCompleted(/*success*/ false); + }); + } + break; + case NpmInstallRequest: { + const command = `${this.npmPath} install ${args.join(" ")} --save-dev`; + const start = Date.now(); + this.exec(command, { cwd }, (err, stdout, stderr) => { + if (this.log.isEnabled()) { + this.log.writeLine(`${requestKind} #${requestId} took: ${Date.now() - start} ms${sys.newLine}stdout: ${stdout}${sys.newLine}stderr: ${stderr}`); + } + // treat any output on stdout as success + onRequestCompleted(!!stdout); + }); + } + break; + default: + Debug.assert(false, `Unknown request kind ${requestKind}`); } - this.exec(command, { cwd }, (err, stdout, stderr) => { - if (this.log.isEnabled()) { - this.log.writeLine(`${requestKind} #${requestId} stdout: ${stdout}`); - this.log.writeLine(`${requestKind} #${requestId} stderr: ${stderr}`); - } - onRequestCompleted(err, stdout, stderr); - }); } } diff --git a/src/server/typingsInstaller/typingsInstaller.ts b/src/server/typingsInstaller/typingsInstaller.ts index e574a4f7911b4..70879801423a4 100644 --- a/src/server/typingsInstaller/typingsInstaller.ts +++ b/src/server/typingsInstaller/typingsInstaller.ts @@ -65,11 +65,11 @@ namespace ts.server.typingsInstaller { export type RequestKind = typeof NpmViewRequest | typeof NpmInstallRequest; - export type RequestCompletedAction = (err: Error, stdout: string, stderr: string) => void; + export type RequestCompletedAction = (success: boolean) => void; type PendingRequest = { requestKind: RequestKind; requestId: number; - command: string; + args: string[]; cwd: string; onRequestCompleted: RequestCompletedAction }; @@ -88,7 +88,6 @@ namespace ts.server.typingsInstaller { constructor( readonly globalCachePath: string, - readonly npmPath: string, readonly safeListPath: Path, readonly throttleLimit: number, protected readonly log = nullLog) { @@ -331,13 +330,12 @@ namespace ts.server.typingsInstaller { let execInstallCmdCount = 0; const filteredTypings: string[] = []; for (const typing of typingsToInstall) { - execNpmViewTyping(this, typing); + filterExistingTypings(this, typing); } - function execNpmViewTyping(self: TypingsInstaller, typing: string) { - const command = `${self.npmPath} view @types/${typing} --silent name`; - self.execAsync(NpmViewRequest, requestId, command, cachePath, (err, stdout, stderr) => { - if (stdout) { + function filterExistingTypings(self: TypingsInstaller, typing: string) { + self.execAsync(NpmViewRequest, requestId, [typing], cachePath, ok => { + if (ok) { filteredTypings.push(typing); } execInstallCmdCount++; @@ -353,9 +351,8 @@ namespace ts.server.typingsInstaller { return; } const scopedTypings = filteredTypings.map(t => "@types/" + t); - const command = `${self.npmPath} install ${scopedTypings.join(" ")} --save-dev`; - self.execAsync(NpmInstallRequest, requestId, command, cachePath, (err, stdout, stderr) => { - postInstallAction(stdout ? scopedTypings : []); + self.execAsync(NpmInstallRequest, requestId, scopedTypings, cachePath, ok => { + postInstallAction(ok ? scopedTypings : []); }); } } @@ -403,8 +400,8 @@ namespace ts.server.typingsInstaller { }; } - private execAsync(requestKind: RequestKind, requestId: number, command: string, cwd: string, onRequestCompleted: RequestCompletedAction): void { - this.pendingRunRequests.unshift({ requestKind, requestId, command, cwd, onRequestCompleted }); + private execAsync(requestKind: RequestKind, requestId: number, args: string[], cwd: string, onRequestCompleted: RequestCompletedAction): void { + this.pendingRunRequests.unshift({ requestKind, requestId, args, cwd, onRequestCompleted }); this.executeWithThrottling(); } @@ -412,15 +409,15 @@ namespace ts.server.typingsInstaller { while (this.inFlightRequestCount < this.throttleLimit && this.pendingRunRequests.length) { this.inFlightRequestCount++; const request = this.pendingRunRequests.pop(); - this.runCommand(request.requestKind, request.requestId, request.command, request.cwd, (err, stdout, stderr) => { + this.executeRequest(request.requestKind, request.requestId, request.args, request.cwd, ok => { this.inFlightRequestCount--; - request.onRequestCompleted(err, stdout, stderr); + request.onRequestCompleted(ok); this.executeWithThrottling(); }); } } - protected abstract runCommand(requestKind: RequestKind, requestId: number, command: string, cwd: string, onRequestCompleted: RequestCompletedAction): void; + protected abstract executeRequest(requestKind: RequestKind, requestId: number, args: string[], cwd: string, onRequestCompleted: RequestCompletedAction): void; protected abstract sendResponse(response: SetTypings | InvalidateCachedTypings): void; } } \ No newline at end of file diff --git a/src/services/navigateTo.ts b/src/services/navigateTo.ts index 0e5f9f9741696..dbe32d84baf18 100644 --- a/src/services/navigateTo.ts +++ b/src/services/navigateTo.ts @@ -6,9 +6,6 @@ namespace ts.NavigateTo { const patternMatcher = createPatternMatcher(searchValue); let rawItems: RawNavigateToItem[] = []; - // This means "compare in a case insensitive manner." - const baseSensitivity: Intl.CollatorOptions = { sensitivity: "base" }; - // Search the declarations in all files and output matched NavigateToItem into array of NavigateToItem[] for (const sourceFile of sourceFiles) { cancellationToken.throwIfCancellationRequested(); @@ -187,8 +184,8 @@ namespace ts.NavigateTo { // We first sort case insensitively. So "Aaa" will come before "bar". // Then we sort case sensitively, so "aaa" will come before "Aaa". return i1.matchKind - i2.matchKind || - i1.name.localeCompare(i2.name, undefined, baseSensitivity) || - i1.name.localeCompare(i2.name); + ts.compareStringsCaseInsensitive(i1.name, i2.name) || + ts.compareStrings(i1.name, i2.name); } function createNavigateToItem(rawItem: RawNavigateToItem): NavigateToItem { diff --git a/src/services/navigationBar.ts b/src/services/navigationBar.ts index 7f27c2224140f..3137f5397a764 100644 --- a/src/services/navigationBar.ts +++ b/src/services/navigationBar.ts @@ -330,10 +330,8 @@ namespace ts.NavigationBar { } } - // More efficient to create a collator once and use its `compare` than to call `a.localeCompare(b)` many times. - const collator: { compare(a: string, b: string): number } = typeof Intl === "object" && typeof Intl.Collator === "function" ? new Intl.Collator() : undefined; // Intl is missing in Safari, and node 0.10 treats "a" as greater than "B". - const localeCompareIsCorrect = collator && collator.compare("a", "B") < 0; + const localeCompareIsCorrect = ts.collator && ts.collator.compare("a", "B") < 0; const localeCompareFix: (a: string, b: string) => number = localeCompareIsCorrect ? collator.compare : function(a, b) { // This isn't perfect, but it passes all of our tests. for (let i = 0; i < Math.min(a.length, b.length); i++) { @@ -344,7 +342,7 @@ namespace ts.NavigationBar { if (chA === "'" && chB === "\"") { return -1; } - const cmp = chA.toLocaleLowerCase().localeCompare(chB.toLocaleLowerCase()); + const cmp = ts.compareStrings(chA.toLocaleLowerCase(), chB.toLocaleLowerCase()); if (cmp !== 0) { return cmp; } diff --git a/src/services/tsconfig.json b/src/services/tsconfig.json index 0066ef94343d1..03d04935068c0 100644 --- a/src/services/tsconfig.json +++ b/src/services/tsconfig.json @@ -25,13 +25,14 @@ "../compiler/visitor.ts", "../compiler/transformers/ts.ts", "../compiler/transformers/jsx.ts", - "../compiler/transformers/es7.ts", - "../compiler/transformers/es6.ts", + "../compiler/transformers/es2017.ts", + "../compiler/transformers/es2016.ts", + "../compiler/transformers/es2015.ts", "../compiler/transformers/generators.ts", "../compiler/transformers/destructuring.ts", "../compiler/transformers/module/module.ts", "../compiler/transformers/module/system.ts", - "../compiler/transformers/module/es6.ts", + "../compiler/transformers/module/es2015.ts", "../compiler/transformer.ts", "../compiler/comments.ts", "../compiler/sourcemap.ts", diff --git a/src/services/types.ts b/src/services/types.ts index aecdfbe4171e1..8e7e0a71b1ee9 100644 --- a/src/services/types.ts +++ b/src/services/types.ts @@ -403,11 +403,11 @@ namespace ts { export interface EditorSettings { baseIndentSize?: number; - indentSize: number; - tabSize: number; - newLineCharacter: string; - convertTabsToSpaces: boolean; - indentStyle: IndentStyle; + indentSize?: number; + tabSize?: number; + newLineCharacter?: string; + convertTabsToSpaces?: boolean; + indentStyle?: IndentStyle; } /* @deprecated - consider using FormatCodeSettings instead */ @@ -428,19 +428,19 @@ namespace ts { } export interface FormatCodeSettings extends EditorSettings { - insertSpaceAfterCommaDelimiter: boolean; - insertSpaceAfterSemicolonInForStatements: boolean; - insertSpaceBeforeAndAfterBinaryOperators: boolean; - insertSpaceAfterKeywordsInControlFlowStatements: boolean; - insertSpaceAfterFunctionKeywordForAnonymousFunctions: boolean; - insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: boolean; - insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: boolean; + insertSpaceAfterCommaDelimiter?: boolean; + insertSpaceAfterSemicolonInForStatements?: boolean; + insertSpaceBeforeAndAfterBinaryOperators?: boolean; + insertSpaceAfterKeywordsInControlFlowStatements?: boolean; + insertSpaceAfterFunctionKeywordForAnonymousFunctions?: boolean; + insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis?: boolean; + insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets?: boolean; insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces?: boolean; - insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: boolean; - insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: boolean; + insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces?: boolean; + insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces?: boolean; insertSpaceAfterTypeAssertion?: boolean; - placeOpenBraceOnNewLineForFunctions: boolean; - placeOpenBraceOnNewLineForControlBlocks: boolean; + placeOpenBraceOnNewLineForFunctions?: boolean; + placeOpenBraceOnNewLineForControlBlocks?: boolean; } export interface DefinitionInfo { diff --git a/src/services/utilities.ts b/src/services/utilities.ts index b58c4188e0d0d..a255c5bc0a8f2 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -1339,7 +1339,7 @@ namespace ts { const options: TranspileOptions = { fileName: "config.js", compilerOptions: { - target: ScriptTarget.ES6, + target: ScriptTarget.ES2015, removeComments: true }, reportDiagnostics: true diff --git a/tests/baselines/reference/APISample_linter.js b/tests/baselines/reference/APISample_linter.js index 4e563c7cac336..1421b53a00caf 100644 --- a/tests/baselines/reference/APISample_linter.js +++ b/tests/baselines/reference/APISample_linter.js @@ -58,7 +58,7 @@ export function delint(sourceFile: ts.SourceFile) { const fileNames: string[] = process.argv.slice(2); fileNames.forEach(fileName => { // Parse a file - let sourceFile = ts.createSourceFile(fileName, readFileSync(fileName).toString(), ts.ScriptTarget.ES6, /*setParentNodes */ true); + let sourceFile = ts.createSourceFile(fileName, readFileSync(fileName).toString(), ts.ScriptTarget.ES2015, /*setParentNodes */ true); // delint it delint(sourceFile); @@ -113,7 +113,7 @@ exports.delint = delint; var fileNames = process.argv.slice(2); fileNames.forEach(function (fileName) { // Parse a file - var sourceFile = ts.createSourceFile(fileName, readFileSync(fileName).toString(), ts.ScriptTarget.ES6, /*setParentNodes */ true); + var sourceFile = ts.createSourceFile(fileName, readFileSync(fileName).toString(), ts.ScriptTarget.ES2015, /*setParentNodes */ true); // delint it delint(sourceFile); }); diff --git a/tests/baselines/reference/alwaysStrict.errors.txt b/tests/baselines/reference/alwaysStrict.errors.txt new file mode 100644 index 0000000000000..d7fcfb45801c7 --- /dev/null +++ b/tests/baselines/reference/alwaysStrict.errors.txt @@ -0,0 +1,10 @@ +tests/cases/compiler/alwaysStrict.ts(3,9): error TS1100: Invalid use of 'arguments' in strict mode. + + +==== tests/cases/compiler/alwaysStrict.ts (1 errors) ==== + + function f() { + var arguments = []; + ~~~~~~~~~ +!!! error TS1100: Invalid use of 'arguments' in strict mode. + } \ No newline at end of file diff --git a/tests/baselines/reference/alwaysStrict.js b/tests/baselines/reference/alwaysStrict.js new file mode 100644 index 0000000000000..bc5f9a2991205 --- /dev/null +++ b/tests/baselines/reference/alwaysStrict.js @@ -0,0 +1,11 @@ +//// [alwaysStrict.ts] + +function f() { + var arguments = []; +} + +//// [alwaysStrict.js] +"use strict"; +function f() { + var arguments = []; +} diff --git a/tests/baselines/reference/alwaysStrictAlreadyUseStrict.js b/tests/baselines/reference/alwaysStrictAlreadyUseStrict.js new file mode 100644 index 0000000000000..cc3be45dedc8d --- /dev/null +++ b/tests/baselines/reference/alwaysStrictAlreadyUseStrict.js @@ -0,0 +1,11 @@ +//// [alwaysStrictAlreadyUseStrict.ts] +"use strict" +function f() { + var a = []; +} + +//// [alwaysStrictAlreadyUseStrict.js] +"use strict"; +function f() { + var a = []; +} diff --git a/tests/baselines/reference/alwaysStrictAlreadyUseStrict.symbols b/tests/baselines/reference/alwaysStrictAlreadyUseStrict.symbols new file mode 100644 index 0000000000000..143f579b94df1 --- /dev/null +++ b/tests/baselines/reference/alwaysStrictAlreadyUseStrict.symbols @@ -0,0 +1,8 @@ +=== tests/cases/compiler/alwaysStrictAlreadyUseStrict.ts === +"use strict" +function f() { +>f : Symbol(f, Decl(alwaysStrictAlreadyUseStrict.ts, 0, 12)) + + var a = []; +>a : Symbol(a, Decl(alwaysStrictAlreadyUseStrict.ts, 2, 7)) +} diff --git a/tests/baselines/reference/alwaysStrictAlreadyUseStrict.types b/tests/baselines/reference/alwaysStrictAlreadyUseStrict.types new file mode 100644 index 0000000000000..0181b40de27d1 --- /dev/null +++ b/tests/baselines/reference/alwaysStrictAlreadyUseStrict.types @@ -0,0 +1,11 @@ +=== tests/cases/compiler/alwaysStrictAlreadyUseStrict.ts === +"use strict" +>"use strict" : "use strict" + +function f() { +>f : () => void + + var a = []; +>a : any[] +>[] : undefined[] +} diff --git a/tests/baselines/reference/alwaysStrictES6.errors.txt b/tests/baselines/reference/alwaysStrictES6.errors.txt new file mode 100644 index 0000000000000..b775a6e475513 --- /dev/null +++ b/tests/baselines/reference/alwaysStrictES6.errors.txt @@ -0,0 +1,10 @@ +tests/cases/compiler/alwaysStrictES6.ts(3,9): error TS1100: Invalid use of 'arguments' in strict mode. + + +==== tests/cases/compiler/alwaysStrictES6.ts (1 errors) ==== + + function f() { + var arguments = []; + ~~~~~~~~~ +!!! error TS1100: Invalid use of 'arguments' in strict mode. + } \ No newline at end of file diff --git a/tests/baselines/reference/alwaysStrictES6.js b/tests/baselines/reference/alwaysStrictES6.js new file mode 100644 index 0000000000000..2c7ad36bce2a5 --- /dev/null +++ b/tests/baselines/reference/alwaysStrictES6.js @@ -0,0 +1,11 @@ +//// [alwaysStrictES6.ts] + +function f() { + var arguments = []; +} + +//// [alwaysStrictES6.js] +"use strict"; +function f() { + var arguments = []; +} diff --git a/tests/baselines/reference/alwaysStrictModule.errors.txt b/tests/baselines/reference/alwaysStrictModule.errors.txt new file mode 100644 index 0000000000000..90c1a0b930acb --- /dev/null +++ b/tests/baselines/reference/alwaysStrictModule.errors.txt @@ -0,0 +1,12 @@ +tests/cases/compiler/alwaysStrictModule.ts(4,13): error TS1100: Invalid use of 'arguments' in strict mode. + + +==== tests/cases/compiler/alwaysStrictModule.ts (1 errors) ==== + + module M { + export function f() { + var arguments = []; + ~~~~~~~~~ +!!! error TS1100: Invalid use of 'arguments' in strict mode. + } + } \ No newline at end of file diff --git a/tests/baselines/reference/alwaysStrictModule.js b/tests/baselines/reference/alwaysStrictModule.js new file mode 100644 index 0000000000000..f39c87ed96c75 --- /dev/null +++ b/tests/baselines/reference/alwaysStrictModule.js @@ -0,0 +1,17 @@ +//// [alwaysStrictModule.ts] + +module M { + export function f() { + var arguments = []; + } +} + +//// [alwaysStrictModule.js] +"use strict"; +var M; +(function (M) { + function f() { + var arguments = []; + } + M.f = f; +})(M || (M = {})); diff --git a/tests/baselines/reference/alwaysStrictModule2.errors.txt b/tests/baselines/reference/alwaysStrictModule2.errors.txt new file mode 100644 index 0000000000000..3980d24abfc03 --- /dev/null +++ b/tests/baselines/reference/alwaysStrictModule2.errors.txt @@ -0,0 +1,22 @@ +tests/cases/compiler/a.ts(4,13): error TS1100: Invalid use of 'arguments' in strict mode. +tests/cases/compiler/b.ts(3,13): error TS1100: Invalid use of 'arguments' in strict mode. + + +==== tests/cases/compiler/a.ts (1 errors) ==== + + module M { + export function f() { + var arguments = []; + ~~~~~~~~~ +!!! error TS1100: Invalid use of 'arguments' in strict mode. + } + } + +==== tests/cases/compiler/b.ts (1 errors) ==== + module M { + export function f2() { + var arguments = []; + ~~~~~~~~~ +!!! error TS1100: Invalid use of 'arguments' in strict mode. + } + } \ No newline at end of file diff --git a/tests/baselines/reference/alwaysStrictModule2.js b/tests/baselines/reference/alwaysStrictModule2.js new file mode 100644 index 0000000000000..54f179ff10b2d --- /dev/null +++ b/tests/baselines/reference/alwaysStrictModule2.js @@ -0,0 +1,34 @@ +//// [tests/cases/compiler/alwaysStrictModule2.ts] //// + +//// [a.ts] + +module M { + export function f() { + var arguments = []; + } +} + +//// [b.ts] +module M { + export function f2() { + var arguments = []; + } +} + +//// [out.js] +"use strict"; +var M; +(function (M) { + function f() { + var arguments = []; + } + M.f = f; +})(M || (M = {})); +"use strict"; +var M; +(function (M) { + function f2() { + var arguments = []; + } + M.f2 = f2; +})(M || (M = {})); diff --git a/tests/baselines/reference/alwaysStrictNoImplicitUseStrict.errors.txt b/tests/baselines/reference/alwaysStrictNoImplicitUseStrict.errors.txt new file mode 100644 index 0000000000000..8df1d76e546b8 --- /dev/null +++ b/tests/baselines/reference/alwaysStrictNoImplicitUseStrict.errors.txt @@ -0,0 +1,14 @@ +error TS5053: Option 'noImplicitUseStrict' cannot be specified with option 'alwaysStrict'. +tests/cases/compiler/alwaysStrictNoImplicitUseStrict.ts(4,13): error TS1100: Invalid use of 'arguments' in strict mode. + + +!!! error TS5053: Option 'noImplicitUseStrict' cannot be specified with option 'alwaysStrict'. +==== tests/cases/compiler/alwaysStrictNoImplicitUseStrict.ts (1 errors) ==== + + module M { + export function f() { + var arguments = []; + ~~~~~~~~~ +!!! error TS1100: Invalid use of 'arguments' in strict mode. + } + } \ No newline at end of file diff --git a/tests/baselines/reference/alwaysStrictNoImplicitUseStrict.js b/tests/baselines/reference/alwaysStrictNoImplicitUseStrict.js new file mode 100644 index 0000000000000..cfa7f69629f74 --- /dev/null +++ b/tests/baselines/reference/alwaysStrictNoImplicitUseStrict.js @@ -0,0 +1,17 @@ +//// [alwaysStrictNoImplicitUseStrict.ts] + +module M { + export function f() { + var arguments = []; + } +} + +//// [alwaysStrictNoImplicitUseStrict.js] +"use strict"; +var M; +(function (M) { + function f() { + var arguments = []; + } + M.f = f; +})(M || (M = {})); diff --git a/tests/baselines/reference/arrowFunctionWithParameterNameAsync_es2017.js b/tests/baselines/reference/arrowFunctionWithParameterNameAsync_es2017.js new file mode 100644 index 0000000000000..daaff29cad927 --- /dev/null +++ b/tests/baselines/reference/arrowFunctionWithParameterNameAsync_es2017.js @@ -0,0 +1,6 @@ +//// [arrowFunctionWithParameterNameAsync_es2017.ts] + +const x = async => async; + +//// [arrowFunctionWithParameterNameAsync_es2017.js] +var x = function (async) { return async; }; diff --git a/tests/baselines/reference/arrowFunctionWithParameterNameAsync_es2017.symbols b/tests/baselines/reference/arrowFunctionWithParameterNameAsync_es2017.symbols new file mode 100644 index 0000000000000..481697c0afdc9 --- /dev/null +++ b/tests/baselines/reference/arrowFunctionWithParameterNameAsync_es2017.symbols @@ -0,0 +1,7 @@ +=== tests/cases/conformance/async/es2017/asyncArrowFunction/arrowFunctionWithParameterNameAsync_es2017.ts === + +const x = async => async; +>x : Symbol(x, Decl(arrowFunctionWithParameterNameAsync_es2017.ts, 1, 5)) +>async : Symbol(async, Decl(arrowFunctionWithParameterNameAsync_es2017.ts, 1, 9)) +>async : Symbol(async, Decl(arrowFunctionWithParameterNameAsync_es2017.ts, 1, 9)) + diff --git a/tests/baselines/reference/arrowFunctionWithParameterNameAsync_es2017.types b/tests/baselines/reference/arrowFunctionWithParameterNameAsync_es2017.types new file mode 100644 index 0000000000000..ccb8654284c2e --- /dev/null +++ b/tests/baselines/reference/arrowFunctionWithParameterNameAsync_es2017.types @@ -0,0 +1,8 @@ +=== tests/cases/conformance/async/es2017/asyncArrowFunction/arrowFunctionWithParameterNameAsync_es2017.ts === + +const x = async => async; +>x : (async: any) => any +>async => async : (async: any) => any +>async : any +>async : any + diff --git a/tests/baselines/reference/asyncArrowFunction10_es2017.errors.txt b/tests/baselines/reference/asyncArrowFunction10_es2017.errors.txt new file mode 100644 index 0000000000000..923dbed6dad93 --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunction10_es2017.errors.txt @@ -0,0 +1,12 @@ +tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction10_es2017.ts(4,11): error TS2304: Cannot find name 'await'. + + +==== tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction10_es2017.ts (1 errors) ==== + + var foo = async (): Promise => { + // Legal to use 'await' in a type context. + var v: await; + ~~~~~ +!!! error TS2304: Cannot find name 'await'. + } + \ No newline at end of file diff --git a/tests/baselines/reference/asyncArrowFunction10_es2017.js b/tests/baselines/reference/asyncArrowFunction10_es2017.js new file mode 100644 index 0000000000000..3c966201bc20c --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunction10_es2017.js @@ -0,0 +1,13 @@ +//// [asyncArrowFunction10_es2017.ts] + +var foo = async (): Promise => { + // Legal to use 'await' in a type context. + var v: await; +} + + +//// [asyncArrowFunction10_es2017.js] +var foo = async () => { + // Legal to use 'await' in a type context. + var v; +}; diff --git a/tests/baselines/reference/asyncArrowFunction1_es2017.js b/tests/baselines/reference/asyncArrowFunction1_es2017.js new file mode 100644 index 0000000000000..ff6a4624242bd --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunction1_es2017.js @@ -0,0 +1,8 @@ +//// [asyncArrowFunction1_es2017.ts] + +var foo = async (): Promise => { +}; + +//// [asyncArrowFunction1_es2017.js] +var foo = async () => { +}; diff --git a/tests/baselines/reference/asyncArrowFunction1_es2017.symbols b/tests/baselines/reference/asyncArrowFunction1_es2017.symbols new file mode 100644 index 0000000000000..35fd033d6b4af --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunction1_es2017.symbols @@ -0,0 +1,7 @@ +=== tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction1_es2017.ts === + +var foo = async (): Promise => { +>foo : Symbol(foo, Decl(asyncArrowFunction1_es2017.ts, 1, 3)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + +}; diff --git a/tests/baselines/reference/asyncArrowFunction1_es2017.types b/tests/baselines/reference/asyncArrowFunction1_es2017.types new file mode 100644 index 0000000000000..bdc6cacc91704 --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunction1_es2017.types @@ -0,0 +1,8 @@ +=== tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction1_es2017.ts === + +var foo = async (): Promise => { +>foo : () => Promise +>async (): Promise => {} : () => Promise +>Promise : Promise + +}; diff --git a/tests/baselines/reference/asyncArrowFunction2_es2017.js b/tests/baselines/reference/asyncArrowFunction2_es2017.js new file mode 100644 index 0000000000000..d2a5d0306b10d --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunction2_es2017.js @@ -0,0 +1,7 @@ +//// [asyncArrowFunction2_es2017.ts] +var f = (await) => { +} + +//// [asyncArrowFunction2_es2017.js] +var f = (await) => { +}; diff --git a/tests/baselines/reference/asyncArrowFunction2_es2017.symbols b/tests/baselines/reference/asyncArrowFunction2_es2017.symbols new file mode 100644 index 0000000000000..a56c5bb62c839 --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunction2_es2017.symbols @@ -0,0 +1,5 @@ +=== tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction2_es2017.ts === +var f = (await) => { +>f : Symbol(f, Decl(asyncArrowFunction2_es2017.ts, 0, 3)) +>await : Symbol(await, Decl(asyncArrowFunction2_es2017.ts, 0, 9)) +} diff --git a/tests/baselines/reference/asyncArrowFunction2_es2017.types b/tests/baselines/reference/asyncArrowFunction2_es2017.types new file mode 100644 index 0000000000000..1feb033e6eef8 --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunction2_es2017.types @@ -0,0 +1,6 @@ +=== tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction2_es2017.ts === +var f = (await) => { +>f : (await: any) => void +>(await) => {} : (await: any) => void +>await : any +} diff --git a/tests/baselines/reference/asyncArrowFunction3_es2017.errors.txt b/tests/baselines/reference/asyncArrowFunction3_es2017.errors.txt new file mode 100644 index 0000000000000..1c1fd7117606e --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunction3_es2017.errors.txt @@ -0,0 +1,8 @@ +tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction3_es2017.ts(1,20): error TS2372: Parameter 'await' cannot be referenced in its initializer. + + +==== tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction3_es2017.ts (1 errors) ==== + function f(await = await) { + ~~~~~ +!!! error TS2372: Parameter 'await' cannot be referenced in its initializer. + } \ No newline at end of file diff --git a/tests/baselines/reference/asyncArrowFunction3_es2017.js b/tests/baselines/reference/asyncArrowFunction3_es2017.js new file mode 100644 index 0000000000000..7ae371facc117 --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunction3_es2017.js @@ -0,0 +1,7 @@ +//// [asyncArrowFunction3_es2017.ts] +function f(await = await) { +} + +//// [asyncArrowFunction3_es2017.js] +function f(await = await) { +} diff --git a/tests/baselines/reference/asyncArrowFunction4_es2017.js b/tests/baselines/reference/asyncArrowFunction4_es2017.js new file mode 100644 index 0000000000000..ebec1e6dbd67d --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunction4_es2017.js @@ -0,0 +1,7 @@ +//// [asyncArrowFunction4_es2017.ts] +var await = () => { +} + +//// [asyncArrowFunction4_es2017.js] +var await = () => { +}; diff --git a/tests/baselines/reference/asyncArrowFunction4_es2017.symbols b/tests/baselines/reference/asyncArrowFunction4_es2017.symbols new file mode 100644 index 0000000000000..b44fc0dbdf60d --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunction4_es2017.symbols @@ -0,0 +1,4 @@ +=== tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction4_es2017.ts === +var await = () => { +>await : Symbol(await, Decl(asyncArrowFunction4_es2017.ts, 0, 3)) +} diff --git a/tests/baselines/reference/asyncArrowFunction4_es2017.types b/tests/baselines/reference/asyncArrowFunction4_es2017.types new file mode 100644 index 0000000000000..6af5f94c75869 --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunction4_es2017.types @@ -0,0 +1,5 @@ +=== tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction4_es2017.ts === +var await = () => { +>await : () => void +>() => {} : () => void +} diff --git a/tests/baselines/reference/asyncArrowFunction5_es2017.errors.txt b/tests/baselines/reference/asyncArrowFunction5_es2017.errors.txt new file mode 100644 index 0000000000000..3df5617710712 --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunction5_es2017.errors.txt @@ -0,0 +1,24 @@ +tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts(2,11): error TS2304: Cannot find name 'async'. +tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts(2,18): error TS2304: Cannot find name 'await'. +tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts(2,24): error TS1005: ',' expected. +tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts(2,26): error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'void'. +tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts(2,33): error TS1005: '=' expected. +tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts(2,40): error TS1109: Expression expected. + + +==== tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts (6 errors) ==== + + var foo = async (await): Promise => { + ~~~~~ +!!! error TS2304: Cannot find name 'async'. + ~~~~~ +!!! error TS2304: Cannot find name 'await'. + ~ +!!! error TS1005: ',' expected. + ~~~~~~~ +!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'void'. + ~ +!!! error TS1005: '=' expected. + ~~ +!!! error TS1109: Expression expected. + } \ No newline at end of file diff --git a/tests/baselines/reference/asyncArrowFunction5_es2017.js b/tests/baselines/reference/asyncArrowFunction5_es2017.js new file mode 100644 index 0000000000000..0c61a141f176c --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunction5_es2017.js @@ -0,0 +1,9 @@ +//// [asyncArrowFunction5_es2017.ts] + +var foo = async (await): Promise => { +} + +//// [asyncArrowFunction5_es2017.js] +var foo = async(await), Promise = ; +{ +} diff --git a/tests/baselines/reference/asyncArrowFunction6_es2017.errors.txt b/tests/baselines/reference/asyncArrowFunction6_es2017.errors.txt new file mode 100644 index 0000000000000..202ecb1727fd5 --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunction6_es2017.errors.txt @@ -0,0 +1,12 @@ +tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction6_es2017.ts(2,22): error TS2524: 'await' expressions cannot be used in a parameter initializer. +tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction6_es2017.ts(2,27): error TS1109: Expression expected. + + +==== tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction6_es2017.ts (2 errors) ==== + + var foo = async (a = await): Promise => { + ~~~~~ +!!! error TS2524: 'await' expressions cannot be used in a parameter initializer. + ~ +!!! error TS1109: Expression expected. + } \ No newline at end of file diff --git a/tests/baselines/reference/asyncArrowFunction6_es2017.js b/tests/baselines/reference/asyncArrowFunction6_es2017.js new file mode 100644 index 0000000000000..c36f75a180a76 --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunction6_es2017.js @@ -0,0 +1,8 @@ +//// [asyncArrowFunction6_es2017.ts] + +var foo = async (a = await): Promise => { +} + +//// [asyncArrowFunction6_es2017.js] +var foo = async (a = await ) => { +}; diff --git a/tests/baselines/reference/asyncArrowFunction7_es2017.errors.txt b/tests/baselines/reference/asyncArrowFunction7_es2017.errors.txt new file mode 100644 index 0000000000000..33e03624283e6 --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunction7_es2017.errors.txt @@ -0,0 +1,15 @@ +tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction7_es2017.ts(4,24): error TS2524: 'await' expressions cannot be used in a parameter initializer. +tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction7_es2017.ts(4,29): error TS1109: Expression expected. + + +==== tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction7_es2017.ts (2 errors) ==== + + var bar = async (): Promise => { + // 'await' here is an identifier, and not an await expression. + var foo = async (a = await): Promise => { + ~~~~~ +!!! error TS2524: 'await' expressions cannot be used in a parameter initializer. + ~ +!!! error TS1109: Expression expected. + } + } \ No newline at end of file diff --git a/tests/baselines/reference/asyncArrowFunction7_es2017.js b/tests/baselines/reference/asyncArrowFunction7_es2017.js new file mode 100644 index 0000000000000..11d8c879eb8bd --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunction7_es2017.js @@ -0,0 +1,14 @@ +//// [asyncArrowFunction7_es2017.ts] + +var bar = async (): Promise => { + // 'await' here is an identifier, and not an await expression. + var foo = async (a = await): Promise => { + } +} + +//// [asyncArrowFunction7_es2017.js] +var bar = async () => { + // 'await' here is an identifier, and not an await expression. + var foo = async (a = await ) => { + }; +}; diff --git a/tests/baselines/reference/asyncArrowFunction8_es2017.errors.txt b/tests/baselines/reference/asyncArrowFunction8_es2017.errors.txt new file mode 100644 index 0000000000000..75e460490bd85 --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunction8_es2017.errors.txt @@ -0,0 +1,10 @@ +tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction8_es2017.ts(3,19): error TS1109: Expression expected. + + +==== tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction8_es2017.ts (1 errors) ==== + + var foo = async (): Promise => { + var v = { [await]: foo } + ~ +!!! error TS1109: Expression expected. + } \ No newline at end of file diff --git a/tests/baselines/reference/asyncArrowFunction8_es2017.js b/tests/baselines/reference/asyncArrowFunction8_es2017.js new file mode 100644 index 0000000000000..1358f186ce221 --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunction8_es2017.js @@ -0,0 +1,10 @@ +//// [asyncArrowFunction8_es2017.ts] + +var foo = async (): Promise => { + var v = { [await]: foo } +} + +//// [asyncArrowFunction8_es2017.js] +var foo = async () => { + var v = { [await ]: foo }; +}; diff --git a/tests/baselines/reference/asyncArrowFunction9_es2017.errors.txt b/tests/baselines/reference/asyncArrowFunction9_es2017.errors.txt new file mode 100644 index 0000000000000..ee94f3f9caf72 --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunction9_es2017.errors.txt @@ -0,0 +1,23 @@ +tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction9_es2017.ts(1,11): error TS2304: Cannot find name 'async'. +tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction9_es2017.ts(1,18): error TS2304: Cannot find name 'a'. +tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction9_es2017.ts(1,37): error TS1005: ',' expected. +tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction9_es2017.ts(1,39): error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'void'. +tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction9_es2017.ts(1,46): error TS1005: '=' expected. +tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction9_es2017.ts(1,53): error TS1109: Expression expected. + + +==== tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction9_es2017.ts (6 errors) ==== + var foo = async (a = await => await): Promise => { + ~~~~~ +!!! error TS2304: Cannot find name 'async'. + ~ +!!! error TS2304: Cannot find name 'a'. + ~ +!!! error TS1005: ',' expected. + ~~~~~~~ +!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' must be of type 'PromiseConstructor', but here has type 'void'. + ~ +!!! error TS1005: '=' expected. + ~~ +!!! error TS1109: Expression expected. + } \ No newline at end of file diff --git a/tests/baselines/reference/asyncArrowFunction9_es2017.js b/tests/baselines/reference/asyncArrowFunction9_es2017.js new file mode 100644 index 0000000000000..c1fec991ec18c --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunction9_es2017.js @@ -0,0 +1,8 @@ +//// [asyncArrowFunction9_es2017.ts] +var foo = async (a = await => await): Promise => { +} + +//// [asyncArrowFunction9_es2017.js] +var foo = async(a = await => await), Promise = ; +{ +} diff --git a/tests/baselines/reference/asyncArrowFunctionCapturesArguments_es2017.js b/tests/baselines/reference/asyncArrowFunctionCapturesArguments_es2017.js new file mode 100644 index 0000000000000..f7f813ad50230 --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunctionCapturesArguments_es2017.js @@ -0,0 +1,16 @@ +//// [asyncArrowFunctionCapturesArguments_es2017.ts] +class C { + method() { + function other() {} + var fn = async () => await other.apply(this, arguments); + } +} + + +//// [asyncArrowFunctionCapturesArguments_es2017.js] +class C { + method() { + function other() { } + var fn = async () => await other.apply(this, arguments); + } +} diff --git a/tests/baselines/reference/asyncArrowFunctionCapturesArguments_es2017.symbols b/tests/baselines/reference/asyncArrowFunctionCapturesArguments_es2017.symbols new file mode 100644 index 0000000000000..ccd9b48e400e5 --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunctionCapturesArguments_es2017.symbols @@ -0,0 +1,20 @@ +=== tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunctionCapturesArguments_es2017.ts === +class C { +>C : Symbol(C, Decl(asyncArrowFunctionCapturesArguments_es2017.ts, 0, 0)) + + method() { +>method : Symbol(C.method, Decl(asyncArrowFunctionCapturesArguments_es2017.ts, 0, 9)) + + function other() {} +>other : Symbol(other, Decl(asyncArrowFunctionCapturesArguments_es2017.ts, 1, 13)) + + var fn = async () => await other.apply(this, arguments); +>fn : Symbol(fn, Decl(asyncArrowFunctionCapturesArguments_es2017.ts, 3, 9)) +>other.apply : Symbol(Function.apply, Decl(lib.es5.d.ts, --, --)) +>other : Symbol(other, Decl(asyncArrowFunctionCapturesArguments_es2017.ts, 1, 13)) +>apply : Symbol(Function.apply, Decl(lib.es5.d.ts, --, --)) +>this : Symbol(C, Decl(asyncArrowFunctionCapturesArguments_es2017.ts, 0, 0)) +>arguments : Symbol(arguments) + } +} + diff --git a/tests/baselines/reference/asyncArrowFunctionCapturesArguments_es2017.types b/tests/baselines/reference/asyncArrowFunctionCapturesArguments_es2017.types new file mode 100644 index 0000000000000..a7a80424e1977 --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunctionCapturesArguments_es2017.types @@ -0,0 +1,23 @@ +=== tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunctionCapturesArguments_es2017.ts === +class C { +>C : C + + method() { +>method : () => void + + function other() {} +>other : () => void + + var fn = async () => await other.apply(this, arguments); +>fn : () => Promise +>async () => await other.apply(this, arguments) : () => Promise +>await other.apply(this, arguments) : any +>other.apply(this, arguments) : any +>other.apply : (this: Function, thisArg: any, argArray?: any) => any +>other : () => void +>apply : (this: Function, thisArg: any, argArray?: any) => any +>this : this +>arguments : IArguments + } +} + diff --git a/tests/baselines/reference/asyncArrowFunctionCapturesThis_es2017.js b/tests/baselines/reference/asyncArrowFunctionCapturesThis_es2017.js new file mode 100644 index 0000000000000..13560557ffef0 --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunctionCapturesThis_es2017.js @@ -0,0 +1,14 @@ +//// [asyncArrowFunctionCapturesThis_es2017.ts] +class C { + method() { + var fn = async () => await this; + } +} + + +//// [asyncArrowFunctionCapturesThis_es2017.js] +class C { + method() { + var fn = async () => await this; + } +} diff --git a/tests/baselines/reference/asyncArrowFunctionCapturesThis_es2017.symbols b/tests/baselines/reference/asyncArrowFunctionCapturesThis_es2017.symbols new file mode 100644 index 0000000000000..bc14bafa0c5e6 --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunctionCapturesThis_es2017.symbols @@ -0,0 +1,13 @@ +=== tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunctionCapturesThis_es2017.ts === +class C { +>C : Symbol(C, Decl(asyncArrowFunctionCapturesThis_es2017.ts, 0, 0)) + + method() { +>method : Symbol(C.method, Decl(asyncArrowFunctionCapturesThis_es2017.ts, 0, 9)) + + var fn = async () => await this; +>fn : Symbol(fn, Decl(asyncArrowFunctionCapturesThis_es2017.ts, 2, 9)) +>this : Symbol(C, Decl(asyncArrowFunctionCapturesThis_es2017.ts, 0, 0)) + } +} + diff --git a/tests/baselines/reference/asyncArrowFunctionCapturesThis_es2017.types b/tests/baselines/reference/asyncArrowFunctionCapturesThis_es2017.types new file mode 100644 index 0000000000000..57f59302bb5bc --- /dev/null +++ b/tests/baselines/reference/asyncArrowFunctionCapturesThis_es2017.types @@ -0,0 +1,15 @@ +=== tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunctionCapturesThis_es2017.ts === +class C { +>C : C + + method() { +>method : () => void + + var fn = async () => await this; +>fn : () => Promise +>async () => await this : () => Promise +>await this : this +>this : this + } +} + diff --git a/tests/baselines/reference/asyncAwaitIsolatedModules_es2017.errors.txt b/tests/baselines/reference/asyncAwaitIsolatedModules_es2017.errors.txt new file mode 100644 index 0000000000000..95927c283aa4b --- /dev/null +++ b/tests/baselines/reference/asyncAwaitIsolatedModules_es2017.errors.txt @@ -0,0 +1,45 @@ +tests/cases/conformance/async/es2017/asyncAwaitIsolatedModules_es2017.ts(1,27): error TS2307: Cannot find module 'missing'. + + +==== tests/cases/conformance/async/es2017/asyncAwaitIsolatedModules_es2017.ts (1 errors) ==== + import { MyPromise } from "missing"; + ~~~~~~~~~ +!!! error TS2307: Cannot find module 'missing'. + + declare var p: Promise; + declare var mp: MyPromise; + + async function f0() { } + async function f1(): Promise { } + async function f3(): MyPromise { } + + let f4 = async function() { } + let f5 = async function(): Promise { } + let f6 = async function(): MyPromise { } + + let f7 = async () => { }; + let f8 = async (): Promise => { }; + let f9 = async (): MyPromise => { }; + let f10 = async () => p; + let f11 = async () => mp; + let f12 = async (): Promise => mp; + let f13 = async (): MyPromise => p; + + let o = { + async m1() { }, + async m2(): Promise { }, + async m3(): MyPromise { } + }; + + class C { + async m1() { } + async m2(): Promise { } + async m3(): MyPromise { } + static async m4() { } + static async m5(): Promise { } + static async m6(): MyPromise { } + } + + module M { + export async function f1() { } + } \ No newline at end of file diff --git a/tests/baselines/reference/asyncAwaitIsolatedModules_es2017.js b/tests/baselines/reference/asyncAwaitIsolatedModules_es2017.js new file mode 100644 index 0000000000000..065aa675b6e57 --- /dev/null +++ b/tests/baselines/reference/asyncAwaitIsolatedModules_es2017.js @@ -0,0 +1,73 @@ +//// [asyncAwaitIsolatedModules_es2017.ts] +import { MyPromise } from "missing"; + +declare var p: Promise; +declare var mp: MyPromise; + +async function f0() { } +async function f1(): Promise { } +async function f3(): MyPromise { } + +let f4 = async function() { } +let f5 = async function(): Promise { } +let f6 = async function(): MyPromise { } + +let f7 = async () => { }; +let f8 = async (): Promise => { }; +let f9 = async (): MyPromise => { }; +let f10 = async () => p; +let f11 = async () => mp; +let f12 = async (): Promise => mp; +let f13 = async (): MyPromise => p; + +let o = { + async m1() { }, + async m2(): Promise { }, + async m3(): MyPromise { } +}; + +class C { + async m1() { } + async m2(): Promise { } + async m3(): MyPromise { } + static async m4() { } + static async m5(): Promise { } + static async m6(): MyPromise { } +} + +module M { + export async function f1() { } +} + +//// [asyncAwaitIsolatedModules_es2017.js] +async function f0() { } +async function f1() { } +async function f3() { } +let f4 = async function () { }; +let f5 = async function () { }; +let f6 = async function () { }; +let f7 = async () => { }; +let f8 = async () => { }; +let f9 = async () => { }; +let f10 = async () => p; +let f11 = async () => mp; +let f12 = async () => mp; +let f13 = async () => p; +let o = { + async m1() { }, + async m2() { }, + async m3() { } +}; +class C { + async m1() { } + async m2() { } + async m3() { } + static async m4() { } + static async m5() { } + static async m6() { } +} +var M; +(function (M) { + async function f1() { } + M.f1 = f1; +})(M || (M = {})); diff --git a/tests/baselines/reference/asyncAwait_es2017.js b/tests/baselines/reference/asyncAwait_es2017.js new file mode 100644 index 0000000000000..314c99fa21077 --- /dev/null +++ b/tests/baselines/reference/asyncAwait_es2017.js @@ -0,0 +1,73 @@ +//// [asyncAwait_es2017.ts] +type MyPromise = Promise; +declare var MyPromise: typeof Promise; +declare var p: Promise; +declare var mp: MyPromise; + +async function f0() { } +async function f1(): Promise { } +async function f3(): MyPromise { } + +let f4 = async function() { } +let f5 = async function(): Promise { } +let f6 = async function(): MyPromise { } + +let f7 = async () => { }; +let f8 = async (): Promise => { }; +let f9 = async (): MyPromise => { }; +let f10 = async () => p; +let f11 = async () => mp; +let f12 = async (): Promise => mp; +let f13 = async (): MyPromise => p; + +let o = { + async m1() { }, + async m2(): Promise { }, + async m3(): MyPromise { } +}; + +class C { + async m1() { } + async m2(): Promise { } + async m3(): MyPromise { } + static async m4() { } + static async m5(): Promise { } + static async m6(): MyPromise { } +} + +module M { + export async function f1() { } +} + +//// [asyncAwait_es2017.js] +async function f0() { } +async function f1() { } +async function f3() { } +let f4 = async function () { }; +let f5 = async function () { }; +let f6 = async function () { }; +let f7 = async () => { }; +let f8 = async () => { }; +let f9 = async () => { }; +let f10 = async () => p; +let f11 = async () => mp; +let f12 = async () => mp; +let f13 = async () => p; +let o = { + async m1() { }, + async m2() { }, + async m3() { } +}; +class C { + async m1() { } + async m2() { } + async m3() { } + static async m4() { } + static async m5() { } + static async m6() { } +} +var M; +(function (M) { + async function f1() { } + M.f1 = f1; +})(M || (M = {})); diff --git a/tests/baselines/reference/asyncAwait_es2017.symbols b/tests/baselines/reference/asyncAwait_es2017.symbols new file mode 100644 index 0000000000000..34dc9802a25de --- /dev/null +++ b/tests/baselines/reference/asyncAwait_es2017.symbols @@ -0,0 +1,118 @@ +=== tests/cases/conformance/async/es2017/asyncAwait_es2017.ts === +type MyPromise = Promise; +>MyPromise : Symbol(MyPromise, Decl(asyncAwait_es2017.ts, 0, 0), Decl(asyncAwait_es2017.ts, 1, 11)) +>T : Symbol(T, Decl(asyncAwait_es2017.ts, 0, 15)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) +>T : Symbol(T, Decl(asyncAwait_es2017.ts, 0, 15)) + +declare var MyPromise: typeof Promise; +>MyPromise : Symbol(MyPromise, Decl(asyncAwait_es2017.ts, 0, 0), Decl(asyncAwait_es2017.ts, 1, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + +declare var p: Promise; +>p : Symbol(p, Decl(asyncAwait_es2017.ts, 2, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + +declare var mp: MyPromise; +>mp : Symbol(mp, Decl(asyncAwait_es2017.ts, 3, 11)) +>MyPromise : Symbol(MyPromise, Decl(asyncAwait_es2017.ts, 0, 0), Decl(asyncAwait_es2017.ts, 1, 11)) + +async function f0() { } +>f0 : Symbol(f0, Decl(asyncAwait_es2017.ts, 3, 34)) + +async function f1(): Promise { } +>f1 : Symbol(f1, Decl(asyncAwait_es2017.ts, 5, 23)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + +async function f3(): MyPromise { } +>f3 : Symbol(f3, Decl(asyncAwait_es2017.ts, 6, 38)) +>MyPromise : Symbol(MyPromise, Decl(asyncAwait_es2017.ts, 0, 0), Decl(asyncAwait_es2017.ts, 1, 11)) + +let f4 = async function() { } +>f4 : Symbol(f4, Decl(asyncAwait_es2017.ts, 9, 3)) + +let f5 = async function(): Promise { } +>f5 : Symbol(f5, Decl(asyncAwait_es2017.ts, 10, 3)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + +let f6 = async function(): MyPromise { } +>f6 : Symbol(f6, Decl(asyncAwait_es2017.ts, 11, 3)) +>MyPromise : Symbol(MyPromise, Decl(asyncAwait_es2017.ts, 0, 0), Decl(asyncAwait_es2017.ts, 1, 11)) + +let f7 = async () => { }; +>f7 : Symbol(f7, Decl(asyncAwait_es2017.ts, 13, 3)) + +let f8 = async (): Promise => { }; +>f8 : Symbol(f8, Decl(asyncAwait_es2017.ts, 14, 3)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + +let f9 = async (): MyPromise => { }; +>f9 : Symbol(f9, Decl(asyncAwait_es2017.ts, 15, 3)) +>MyPromise : Symbol(MyPromise, Decl(asyncAwait_es2017.ts, 0, 0), Decl(asyncAwait_es2017.ts, 1, 11)) + +let f10 = async () => p; +>f10 : Symbol(f10, Decl(asyncAwait_es2017.ts, 16, 3)) +>p : Symbol(p, Decl(asyncAwait_es2017.ts, 2, 11)) + +let f11 = async () => mp; +>f11 : Symbol(f11, Decl(asyncAwait_es2017.ts, 17, 3)) +>mp : Symbol(mp, Decl(asyncAwait_es2017.ts, 3, 11)) + +let f12 = async (): Promise => mp; +>f12 : Symbol(f12, Decl(asyncAwait_es2017.ts, 18, 3)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) +>mp : Symbol(mp, Decl(asyncAwait_es2017.ts, 3, 11)) + +let f13 = async (): MyPromise => p; +>f13 : Symbol(f13, Decl(asyncAwait_es2017.ts, 19, 3)) +>MyPromise : Symbol(MyPromise, Decl(asyncAwait_es2017.ts, 0, 0), Decl(asyncAwait_es2017.ts, 1, 11)) +>p : Symbol(p, Decl(asyncAwait_es2017.ts, 2, 11)) + +let o = { +>o : Symbol(o, Decl(asyncAwait_es2017.ts, 21, 3)) + + async m1() { }, +>m1 : Symbol(m1, Decl(asyncAwait_es2017.ts, 21, 9)) + + async m2(): Promise { }, +>m2 : Symbol(m2, Decl(asyncAwait_es2017.ts, 22, 16)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + + async m3(): MyPromise { } +>m3 : Symbol(m3, Decl(asyncAwait_es2017.ts, 23, 31)) +>MyPromise : Symbol(MyPromise, Decl(asyncAwait_es2017.ts, 0, 0), Decl(asyncAwait_es2017.ts, 1, 11)) + +}; + +class C { +>C : Symbol(C, Decl(asyncAwait_es2017.ts, 25, 2)) + + async m1() { } +>m1 : Symbol(C.m1, Decl(asyncAwait_es2017.ts, 27, 9)) + + async m2(): Promise { } +>m2 : Symbol(C.m2, Decl(asyncAwait_es2017.ts, 28, 15)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + + async m3(): MyPromise { } +>m3 : Symbol(C.m3, Decl(asyncAwait_es2017.ts, 29, 30)) +>MyPromise : Symbol(MyPromise, Decl(asyncAwait_es2017.ts, 0, 0), Decl(asyncAwait_es2017.ts, 1, 11)) + + static async m4() { } +>m4 : Symbol(C.m4, Decl(asyncAwait_es2017.ts, 30, 32)) + + static async m5(): Promise { } +>m5 : Symbol(C.m5, Decl(asyncAwait_es2017.ts, 31, 22)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + + static async m6(): MyPromise { } +>m6 : Symbol(C.m6, Decl(asyncAwait_es2017.ts, 32, 37)) +>MyPromise : Symbol(MyPromise, Decl(asyncAwait_es2017.ts, 0, 0), Decl(asyncAwait_es2017.ts, 1, 11)) +} + +module M { +>M : Symbol(M, Decl(asyncAwait_es2017.ts, 34, 1)) + + export async function f1() { } +>f1 : Symbol(f1, Decl(asyncAwait_es2017.ts, 36, 10)) +} diff --git a/tests/baselines/reference/asyncAwait_es2017.types b/tests/baselines/reference/asyncAwait_es2017.types new file mode 100644 index 0000000000000..a1226d0cbfe70 --- /dev/null +++ b/tests/baselines/reference/asyncAwait_es2017.types @@ -0,0 +1,129 @@ +=== tests/cases/conformance/async/es2017/asyncAwait_es2017.ts === +type MyPromise = Promise; +>MyPromise : Promise +>T : T +>Promise : Promise +>T : T + +declare var MyPromise: typeof Promise; +>MyPromise : PromiseConstructor +>Promise : PromiseConstructor + +declare var p: Promise; +>p : Promise +>Promise : Promise + +declare var mp: MyPromise; +>mp : Promise +>MyPromise : Promise + +async function f0() { } +>f0 : () => Promise + +async function f1(): Promise { } +>f1 : () => Promise +>Promise : Promise + +async function f3(): MyPromise { } +>f3 : () => Promise +>MyPromise : Promise + +let f4 = async function() { } +>f4 : () => Promise +>async function() { } : () => Promise + +let f5 = async function(): Promise { } +>f5 : () => Promise +>async function(): Promise { } : () => Promise +>Promise : Promise + +let f6 = async function(): MyPromise { } +>f6 : () => Promise +>async function(): MyPromise { } : () => Promise +>MyPromise : Promise + +let f7 = async () => { }; +>f7 : () => Promise +>async () => { } : () => Promise + +let f8 = async (): Promise => { }; +>f8 : () => Promise +>async (): Promise => { } : () => Promise +>Promise : Promise + +let f9 = async (): MyPromise => { }; +>f9 : () => Promise +>async (): MyPromise => { } : () => Promise +>MyPromise : Promise + +let f10 = async () => p; +>f10 : () => Promise +>async () => p : () => Promise +>p : Promise + +let f11 = async () => mp; +>f11 : () => Promise +>async () => mp : () => Promise +>mp : Promise + +let f12 = async (): Promise => mp; +>f12 : () => Promise +>async (): Promise => mp : () => Promise +>Promise : Promise +>mp : Promise + +let f13 = async (): MyPromise => p; +>f13 : () => Promise +>async (): MyPromise => p : () => Promise +>MyPromise : Promise +>p : Promise + +let o = { +>o : { m1(): Promise; m2(): Promise; m3(): Promise; } +>{ async m1() { }, async m2(): Promise { }, async m3(): MyPromise { }} : { m1(): Promise; m2(): Promise; m3(): Promise; } + + async m1() { }, +>m1 : () => Promise + + async m2(): Promise { }, +>m2 : () => Promise +>Promise : Promise + + async m3(): MyPromise { } +>m3 : () => Promise +>MyPromise : Promise + +}; + +class C { +>C : C + + async m1() { } +>m1 : () => Promise + + async m2(): Promise { } +>m2 : () => Promise +>Promise : Promise + + async m3(): MyPromise { } +>m3 : () => Promise +>MyPromise : Promise + + static async m4() { } +>m4 : () => Promise + + static async m5(): Promise { } +>m5 : () => Promise +>Promise : Promise + + static async m6(): MyPromise { } +>m6 : () => Promise +>MyPromise : Promise +} + +module M { +>M : typeof M + + export async function f1() { } +>f1 : () => Promise +} diff --git a/tests/baselines/reference/asyncFunctionDeclaration10_es2017.errors.txt b/tests/baselines/reference/asyncFunctionDeclaration10_es2017.errors.txt new file mode 100644 index 0000000000000..d5da34322640e --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration10_es2017.errors.txt @@ -0,0 +1,26 @@ +tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration10_es2017.ts(1,20): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. +tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration10_es2017.ts(1,30): error TS1109: Expression expected. +tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration10_es2017.ts(1,33): error TS1138: Parameter declaration expected. +tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration10_es2017.ts(1,33): error TS2304: Cannot find name 'await'. +tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration10_es2017.ts(1,38): error TS1005: ';' expected. +tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration10_es2017.ts(1,39): error TS1128: Declaration or statement expected. +tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration10_es2017.ts(1,53): error TS1109: Expression expected. + + +==== tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration10_es2017.ts (7 errors) ==== + async function foo(a = await => await): Promise { + ~~~~~~~~~ +!!! error TS2371: A parameter initializer is only allowed in a function or constructor implementation. + ~~ +!!! error TS1109: Expression expected. + ~~~~~ +!!! error TS1138: Parameter declaration expected. + ~~~~~ +!!! error TS2304: Cannot find name 'await'. + ~ +!!! error TS1005: ';' expected. + ~ +!!! error TS1128: Declaration or statement expected. + ~ +!!! error TS1109: Expression expected. + } \ No newline at end of file diff --git a/tests/baselines/reference/asyncFunctionDeclaration10_es2017.js b/tests/baselines/reference/asyncFunctionDeclaration10_es2017.js new file mode 100644 index 0000000000000..f8a22204311e5 --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration10_es2017.js @@ -0,0 +1,8 @@ +//// [asyncFunctionDeclaration10_es2017.ts] +async function foo(a = await => await): Promise { +} + +//// [asyncFunctionDeclaration10_es2017.js] +async function foo(a = await ) { } +await; +Promise < void > {}; diff --git a/tests/baselines/reference/asyncFunctionDeclaration10_es5.js b/tests/baselines/reference/asyncFunctionDeclaration10_es5.js index 05ff9daa9dabd..758bdab5dc902 100644 --- a/tests/baselines/reference/asyncFunctionDeclaration10_es5.js +++ b/tests/baselines/reference/asyncFunctionDeclaration10_es5.js @@ -3,5 +3,11 @@ async function foo(a = await => await): Promise { } //// [asyncFunctionDeclaration10_es5.js] +function foo(a) { + if (a === void 0) { a = yield ; } + return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { + return [2 /*return*/]; + }); }); +} await; Promise < void > {}; diff --git a/tests/baselines/reference/asyncFunctionDeclaration10_es6.js b/tests/baselines/reference/asyncFunctionDeclaration10_es6.js index 141c0cbab55cd..1f175035bf085 100644 --- a/tests/baselines/reference/asyncFunctionDeclaration10_es6.js +++ b/tests/baselines/reference/asyncFunctionDeclaration10_es6.js @@ -3,5 +3,8 @@ async function foo(a = await => await): Promise { } //// [asyncFunctionDeclaration10_es6.js] +function foo(a = yield ) { + return __awaiter(this, void 0, void 0, function* () { }); +} await; Promise < void > {}; diff --git a/tests/baselines/reference/asyncFunctionDeclaration11_es2017.js b/tests/baselines/reference/asyncFunctionDeclaration11_es2017.js new file mode 100644 index 0000000000000..0ae906ebfe13c --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration11_es2017.js @@ -0,0 +1,7 @@ +//// [asyncFunctionDeclaration11_es2017.ts] +async function await(): Promise { +} + +//// [asyncFunctionDeclaration11_es2017.js] +async function await() { +} diff --git a/tests/baselines/reference/asyncFunctionDeclaration11_es2017.symbols b/tests/baselines/reference/asyncFunctionDeclaration11_es2017.symbols new file mode 100644 index 0000000000000..02c35d81d6230 --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration11_es2017.symbols @@ -0,0 +1,5 @@ +=== tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration11_es2017.ts === +async function await(): Promise { +>await : Symbol(await, Decl(asyncFunctionDeclaration11_es2017.ts, 0, 0)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) +} diff --git a/tests/baselines/reference/asyncFunctionDeclaration11_es2017.types b/tests/baselines/reference/asyncFunctionDeclaration11_es2017.types new file mode 100644 index 0000000000000..b86601bf33da9 --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration11_es2017.types @@ -0,0 +1,5 @@ +=== tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration11_es2017.ts === +async function await(): Promise { +>await : () => Promise +>Promise : Promise +} diff --git a/tests/baselines/reference/asyncFunctionDeclaration12_es2017.errors.txt b/tests/baselines/reference/asyncFunctionDeclaration12_es2017.errors.txt new file mode 100644 index 0000000000000..f951bee715c5e --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration12_es2017.errors.txt @@ -0,0 +1,16 @@ +tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration12_es2017.ts(1,24): error TS1005: '(' expected. +tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration12_es2017.ts(1,29): error TS1005: '=' expected. +tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration12_es2017.ts(1,33): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. +tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration12_es2017.ts(1,47): error TS1005: '=>' expected. + + +==== tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration12_es2017.ts (4 errors) ==== + var v = async function await(): Promise { } + ~~~~~ +!!! error TS1005: '(' expected. + ~ +!!! error TS1005: '=' expected. + ~~~~~~~~~~~~~ +!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value. + ~ +!!! error TS1005: '=>' expected. \ No newline at end of file diff --git a/tests/baselines/reference/asyncFunctionDeclaration12_es2017.js b/tests/baselines/reference/asyncFunctionDeclaration12_es2017.js new file mode 100644 index 0000000000000..93fbb0610237d --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration12_es2017.js @@ -0,0 +1,5 @@ +//// [asyncFunctionDeclaration12_es2017.ts] +var v = async function await(): Promise { } + +//// [asyncFunctionDeclaration12_es2017.js] +var v = async function () { }, await = () => { }; diff --git a/tests/baselines/reference/asyncFunctionDeclaration13_es2017.errors.txt b/tests/baselines/reference/asyncFunctionDeclaration13_es2017.errors.txt new file mode 100644 index 0000000000000..41ea0a7cafbd0 --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration13_es2017.errors.txt @@ -0,0 +1,11 @@ +tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration13_es2017.ts(3,11): error TS2304: Cannot find name 'await'. + + +==== tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration13_es2017.ts (1 errors) ==== + async function foo(): Promise { + // Legal to use 'await' in a type context. + var v: await; + ~~~~~ +!!! error TS2304: Cannot find name 'await'. + } + \ No newline at end of file diff --git a/tests/baselines/reference/asyncFunctionDeclaration13_es2017.js b/tests/baselines/reference/asyncFunctionDeclaration13_es2017.js new file mode 100644 index 0000000000000..a16236aea7fdc --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration13_es2017.js @@ -0,0 +1,12 @@ +//// [asyncFunctionDeclaration13_es2017.ts] +async function foo(): Promise { + // Legal to use 'await' in a type context. + var v: await; +} + + +//// [asyncFunctionDeclaration13_es2017.js] +async function foo() { + // Legal to use 'await' in a type context. + var v; +} diff --git a/tests/baselines/reference/asyncFunctionDeclaration14_es2017.js b/tests/baselines/reference/asyncFunctionDeclaration14_es2017.js new file mode 100644 index 0000000000000..308b8f1763cb1 --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration14_es2017.js @@ -0,0 +1,9 @@ +//// [asyncFunctionDeclaration14_es2017.ts] +async function foo(): Promise { + return; +} + +//// [asyncFunctionDeclaration14_es2017.js] +async function foo() { + return; +} diff --git a/tests/baselines/reference/asyncFunctionDeclaration14_es2017.symbols b/tests/baselines/reference/asyncFunctionDeclaration14_es2017.symbols new file mode 100644 index 0000000000000..1b5bca844c062 --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration14_es2017.symbols @@ -0,0 +1,7 @@ +=== tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration14_es2017.ts === +async function foo(): Promise { +>foo : Symbol(foo, Decl(asyncFunctionDeclaration14_es2017.ts, 0, 0)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + + return; +} diff --git a/tests/baselines/reference/asyncFunctionDeclaration14_es2017.types b/tests/baselines/reference/asyncFunctionDeclaration14_es2017.types new file mode 100644 index 0000000000000..1e505f93a2c75 --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration14_es2017.types @@ -0,0 +1,7 @@ +=== tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration14_es2017.ts === +async function foo(): Promise { +>foo : () => Promise +>Promise : Promise + + return; +} diff --git a/tests/baselines/reference/asyncFunctionDeclaration1_es2017.js b/tests/baselines/reference/asyncFunctionDeclaration1_es2017.js new file mode 100644 index 0000000000000..549fd226b2f34 --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration1_es2017.js @@ -0,0 +1,7 @@ +//// [asyncFunctionDeclaration1_es2017.ts] +async function foo(): Promise { +} + +//// [asyncFunctionDeclaration1_es2017.js] +async function foo() { +} diff --git a/tests/baselines/reference/asyncFunctionDeclaration1_es2017.symbols b/tests/baselines/reference/asyncFunctionDeclaration1_es2017.symbols new file mode 100644 index 0000000000000..a712df4589636 --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration1_es2017.symbols @@ -0,0 +1,5 @@ +=== tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration1_es2017.ts === +async function foo(): Promise { +>foo : Symbol(foo, Decl(asyncFunctionDeclaration1_es2017.ts, 0, 0)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) +} diff --git a/tests/baselines/reference/asyncFunctionDeclaration1_es2017.types b/tests/baselines/reference/asyncFunctionDeclaration1_es2017.types new file mode 100644 index 0000000000000..ff97e686c43f7 --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration1_es2017.types @@ -0,0 +1,5 @@ +=== tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration1_es2017.ts === +async function foo(): Promise { +>foo : () => Promise +>Promise : Promise +} diff --git a/tests/baselines/reference/asyncFunctionDeclaration2_es2017.js b/tests/baselines/reference/asyncFunctionDeclaration2_es2017.js new file mode 100644 index 0000000000000..6de061456e823 --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration2_es2017.js @@ -0,0 +1,7 @@ +//// [asyncFunctionDeclaration2_es2017.ts] +function f(await) { +} + +//// [asyncFunctionDeclaration2_es2017.js] +function f(await) { +} diff --git a/tests/baselines/reference/asyncFunctionDeclaration2_es2017.symbols b/tests/baselines/reference/asyncFunctionDeclaration2_es2017.symbols new file mode 100644 index 0000000000000..aed41f50310c2 --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration2_es2017.symbols @@ -0,0 +1,5 @@ +=== tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration2_es2017.ts === +function f(await) { +>f : Symbol(f, Decl(asyncFunctionDeclaration2_es2017.ts, 0, 0)) +>await : Symbol(await, Decl(asyncFunctionDeclaration2_es2017.ts, 0, 11)) +} diff --git a/tests/baselines/reference/asyncFunctionDeclaration2_es2017.types b/tests/baselines/reference/asyncFunctionDeclaration2_es2017.types new file mode 100644 index 0000000000000..8413bea692ad6 --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration2_es2017.types @@ -0,0 +1,5 @@ +=== tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration2_es2017.ts === +function f(await) { +>f : (await: any) => void +>await : any +} diff --git a/tests/baselines/reference/asyncFunctionDeclaration3_es2017.errors.txt b/tests/baselines/reference/asyncFunctionDeclaration3_es2017.errors.txt new file mode 100644 index 0000000000000..53a8507483435 --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration3_es2017.errors.txt @@ -0,0 +1,8 @@ +tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration3_es2017.ts(1,20): error TS2372: Parameter 'await' cannot be referenced in its initializer. + + +==== tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration3_es2017.ts (1 errors) ==== + function f(await = await) { + ~~~~~ +!!! error TS2372: Parameter 'await' cannot be referenced in its initializer. + } \ No newline at end of file diff --git a/tests/baselines/reference/asyncFunctionDeclaration3_es2017.js b/tests/baselines/reference/asyncFunctionDeclaration3_es2017.js new file mode 100644 index 0000000000000..d6110265fdc1f --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration3_es2017.js @@ -0,0 +1,7 @@ +//// [asyncFunctionDeclaration3_es2017.ts] +function f(await = await) { +} + +//// [asyncFunctionDeclaration3_es2017.js] +function f(await = await) { +} diff --git a/tests/baselines/reference/asyncFunctionDeclaration4_es2017.js b/tests/baselines/reference/asyncFunctionDeclaration4_es2017.js new file mode 100644 index 0000000000000..6209531e5499a --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration4_es2017.js @@ -0,0 +1,7 @@ +//// [asyncFunctionDeclaration4_es2017.ts] +function await() { +} + +//// [asyncFunctionDeclaration4_es2017.js] +function await() { +} diff --git a/tests/baselines/reference/asyncFunctionDeclaration4_es2017.symbols b/tests/baselines/reference/asyncFunctionDeclaration4_es2017.symbols new file mode 100644 index 0000000000000..7afbd879c5964 --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration4_es2017.symbols @@ -0,0 +1,4 @@ +=== tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration4_es2017.ts === +function await() { +>await : Symbol(await, Decl(asyncFunctionDeclaration4_es2017.ts, 0, 0)) +} diff --git a/tests/baselines/reference/asyncFunctionDeclaration4_es2017.types b/tests/baselines/reference/asyncFunctionDeclaration4_es2017.types new file mode 100644 index 0000000000000..f7c4248f7c35e --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration4_es2017.types @@ -0,0 +1,4 @@ +=== tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration4_es2017.ts === +function await() { +>await : () => void +} diff --git a/tests/baselines/reference/asyncFunctionDeclaration5_es2017.errors.txt b/tests/baselines/reference/asyncFunctionDeclaration5_es2017.errors.txt new file mode 100644 index 0000000000000..ad8c02802069f --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration5_es2017.errors.txt @@ -0,0 +1,20 @@ +tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration5_es2017.ts(1,20): error TS1138: Parameter declaration expected. +tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration5_es2017.ts(1,20): error TS2304: Cannot find name 'await'. +tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration5_es2017.ts(1,25): error TS1005: ';' expected. +tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration5_es2017.ts(1,26): error TS1128: Declaration or statement expected. +tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration5_es2017.ts(1,40): error TS1109: Expression expected. + + +==== tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration5_es2017.ts (5 errors) ==== + async function foo(await): Promise { + ~~~~~ +!!! error TS1138: Parameter declaration expected. + ~~~~~ +!!! error TS2304: Cannot find name 'await'. + ~ +!!! error TS1005: ';' expected. + ~ +!!! error TS1128: Declaration or statement expected. + ~ +!!! error TS1109: Expression expected. + } \ No newline at end of file diff --git a/tests/baselines/reference/asyncFunctionDeclaration5_es2017.js b/tests/baselines/reference/asyncFunctionDeclaration5_es2017.js new file mode 100644 index 0000000000000..904bbe62f0ba8 --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration5_es2017.js @@ -0,0 +1,8 @@ +//// [asyncFunctionDeclaration5_es2017.ts] +async function foo(await): Promise { +} + +//// [asyncFunctionDeclaration5_es2017.js] +async function foo() { } +await; +Promise < void > {}; diff --git a/tests/baselines/reference/asyncFunctionDeclaration5_es5.js b/tests/baselines/reference/asyncFunctionDeclaration5_es5.js index 22c18ff6ad412..5f8f8f4273589 100644 --- a/tests/baselines/reference/asyncFunctionDeclaration5_es5.js +++ b/tests/baselines/reference/asyncFunctionDeclaration5_es5.js @@ -3,5 +3,10 @@ async function foo(await): Promise { } //// [asyncFunctionDeclaration5_es5.js] +function foo() { + return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { + return [2 /*return*/]; + }); }); +} await; Promise < void > {}; diff --git a/tests/baselines/reference/asyncFunctionDeclaration5_es6.js b/tests/baselines/reference/asyncFunctionDeclaration5_es6.js index 8d28c371465f8..9521b76041585 100644 --- a/tests/baselines/reference/asyncFunctionDeclaration5_es6.js +++ b/tests/baselines/reference/asyncFunctionDeclaration5_es6.js @@ -3,5 +3,8 @@ async function foo(await): Promise { } //// [asyncFunctionDeclaration5_es6.js] +function foo() { + return __awaiter(this, void 0, void 0, function* () { }); +} await; Promise < void > {}; diff --git a/tests/baselines/reference/asyncFunctionDeclaration6_es2017.errors.txt b/tests/baselines/reference/asyncFunctionDeclaration6_es2017.errors.txt new file mode 100644 index 0000000000000..b5a5ddccdce5b --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration6_es2017.errors.txt @@ -0,0 +1,11 @@ +tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration6_es2017.ts(1,24): error TS2524: 'await' expressions cannot be used in a parameter initializer. +tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration6_es2017.ts(1,29): error TS1109: Expression expected. + + +==== tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration6_es2017.ts (2 errors) ==== + async function foo(a = await): Promise { + ~~~~~ +!!! error TS2524: 'await' expressions cannot be used in a parameter initializer. + ~ +!!! error TS1109: Expression expected. + } \ No newline at end of file diff --git a/tests/baselines/reference/asyncFunctionDeclaration6_es2017.js b/tests/baselines/reference/asyncFunctionDeclaration6_es2017.js new file mode 100644 index 0000000000000..6df02000846d5 --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration6_es2017.js @@ -0,0 +1,7 @@ +//// [asyncFunctionDeclaration6_es2017.ts] +async function foo(a = await): Promise { +} + +//// [asyncFunctionDeclaration6_es2017.js] +async function foo(a = await ) { +} diff --git a/tests/baselines/reference/asyncFunctionDeclaration7_es2017.errors.txt b/tests/baselines/reference/asyncFunctionDeclaration7_es2017.errors.txt new file mode 100644 index 0000000000000..ad9ec9a043a66 --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration7_es2017.errors.txt @@ -0,0 +1,14 @@ +tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration7_es2017.ts(3,26): error TS2524: 'await' expressions cannot be used in a parameter initializer. +tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration7_es2017.ts(3,31): error TS1109: Expression expected. + + +==== tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration7_es2017.ts (2 errors) ==== + async function bar(): Promise { + // 'await' here is an identifier, and not a yield expression. + async function foo(a = await): Promise { + ~~~~~ +!!! error TS2524: 'await' expressions cannot be used in a parameter initializer. + ~ +!!! error TS1109: Expression expected. + } + } \ No newline at end of file diff --git a/tests/baselines/reference/asyncFunctionDeclaration7_es2017.js b/tests/baselines/reference/asyncFunctionDeclaration7_es2017.js new file mode 100644 index 0000000000000..c9aaa80b9b072 --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration7_es2017.js @@ -0,0 +1,13 @@ +//// [asyncFunctionDeclaration7_es2017.ts] +async function bar(): Promise { + // 'await' here is an identifier, and not a yield expression. + async function foo(a = await): Promise { + } +} + +//// [asyncFunctionDeclaration7_es2017.js] +async function bar() { + // 'await' here is an identifier, and not a yield expression. + async function foo(a = await ) { + } +} diff --git a/tests/baselines/reference/asyncFunctionDeclaration8_es2017.errors.txt b/tests/baselines/reference/asyncFunctionDeclaration8_es2017.errors.txt new file mode 100644 index 0000000000000..b82f012d69478 --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration8_es2017.errors.txt @@ -0,0 +1,10 @@ +tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration8_es2017.ts(1,12): error TS2304: Cannot find name 'await'. +tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration8_es2017.ts(1,20): error TS2304: Cannot find name 'foo'. + + +==== tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration8_es2017.ts (2 errors) ==== + var v = { [await]: foo } + ~~~~~ +!!! error TS2304: Cannot find name 'await'. + ~~~ +!!! error TS2304: Cannot find name 'foo'. \ No newline at end of file diff --git a/tests/baselines/reference/asyncFunctionDeclaration8_es2017.js b/tests/baselines/reference/asyncFunctionDeclaration8_es2017.js new file mode 100644 index 0000000000000..db7bfa4f9fb6d --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration8_es2017.js @@ -0,0 +1,5 @@ +//// [asyncFunctionDeclaration8_es2017.ts] +var v = { [await]: foo } + +//// [asyncFunctionDeclaration8_es2017.js] +var v = { [await]: foo }; diff --git a/tests/baselines/reference/asyncFunctionDeclaration9_es2017.errors.txt b/tests/baselines/reference/asyncFunctionDeclaration9_es2017.errors.txt new file mode 100644 index 0000000000000..27c41d8b2538c --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration9_es2017.errors.txt @@ -0,0 +1,9 @@ +tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration9_es2017.ts(2,19): error TS1109: Expression expected. + + +==== tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration9_es2017.ts (1 errors) ==== + async function foo(): Promise { + var v = { [await]: foo } + ~ +!!! error TS1109: Expression expected. + } \ No newline at end of file diff --git a/tests/baselines/reference/asyncFunctionDeclaration9_es2017.js b/tests/baselines/reference/asyncFunctionDeclaration9_es2017.js new file mode 100644 index 0000000000000..08cb8e44ba070 --- /dev/null +++ b/tests/baselines/reference/asyncFunctionDeclaration9_es2017.js @@ -0,0 +1,9 @@ +//// [asyncFunctionDeclaration9_es2017.ts] +async function foo(): Promise { + var v = { [await]: foo } +} + +//// [asyncFunctionDeclaration9_es2017.js] +async function foo() { + var v = { [await ]: foo }; +} diff --git a/tests/baselines/reference/asyncMethodWithSuper_es2017.js b/tests/baselines/reference/asyncMethodWithSuper_es2017.js new file mode 100644 index 0000000000000..b692579316380 --- /dev/null +++ b/tests/baselines/reference/asyncMethodWithSuper_es2017.js @@ -0,0 +1,90 @@ +//// [asyncMethodWithSuper_es2017.ts] +class A { + x() { + } +} + +class B extends A { + // async method with only call/get on 'super' does not require a binding + async simple() { + // call with property access + super.x(); + + // call with element access + super["x"](); + + // property access (read) + const a = super.x; + + // element access (read) + const b = super["x"]; + } + + // async method with assignment/destructuring on 'super' requires a binding + async advanced() { + const f = () => {}; + + // call with property access + super.x(); + + // call with element access + super["x"](); + + // property access (read) + const a = super.x; + + // element access (read) + const b = super["x"]; + + // property access (assign) + super.x = f; + + // element access (assign) + super["x"] = f; + + // destructuring assign with property access + ({ f: super.x } = { f }); + + // destructuring assign with element access + ({ f: super["x"] } = { f }); + } +} + +//// [asyncMethodWithSuper_es2017.js] +class A { + x() { + } +} +class B extends A { + // async method with only call/get on 'super' does not require a binding + async simple() { + // call with property access + super.x(); + // call with element access + super["x"](); + // property access (read) + const a = super.x; + // element access (read) + const b = super["x"]; + } + // async method with assignment/destructuring on 'super' requires a binding + async advanced() { + const f = () => { }; + // call with property access + super.x(); + // call with element access + super["x"](); + // property access (read) + const a = super.x; + // element access (read) + const b = super["x"]; + // property access (assign) + super.x = f; + // element access (assign) + super["x"] = f; + // destructuring assign with property access + ({ f: super.x } = { f }); + // destructuring assign with element access + ({ f: super["x"] } = { f }); + } +} diff --git a/tests/baselines/reference/asyncMethodWithSuper_es2017.symbols b/tests/baselines/reference/asyncMethodWithSuper_es2017.symbols new file mode 100644 index 0000000000000..39ed91d7acb4f --- /dev/null +++ b/tests/baselines/reference/asyncMethodWithSuper_es2017.symbols @@ -0,0 +1,102 @@ +=== tests/cases/conformance/async/es2017/asyncMethodWithSuper_es2017.ts === +class A { +>A : Symbol(A, Decl(asyncMethodWithSuper_es2017.ts, 0, 0)) + + x() { +>x : Symbol(A.x, Decl(asyncMethodWithSuper_es2017.ts, 0, 9)) + } +} + +class B extends A { +>B : Symbol(B, Decl(asyncMethodWithSuper_es2017.ts, 3, 1)) +>A : Symbol(A, Decl(asyncMethodWithSuper_es2017.ts, 0, 0)) + + // async method with only call/get on 'super' does not require a binding + async simple() { +>simple : Symbol(B.simple, Decl(asyncMethodWithSuper_es2017.ts, 5, 19)) + + // call with property access + super.x(); +>super.x : Symbol(A.x, Decl(asyncMethodWithSuper_es2017.ts, 0, 9)) +>super : Symbol(A, Decl(asyncMethodWithSuper_es2017.ts, 0, 0)) +>x : Symbol(A.x, Decl(asyncMethodWithSuper_es2017.ts, 0, 9)) + + // call with element access + super["x"](); +>super : Symbol(A, Decl(asyncMethodWithSuper_es2017.ts, 0, 0)) +>"x" : Symbol(A.x, Decl(asyncMethodWithSuper_es2017.ts, 0, 9)) + + // property access (read) + const a = super.x; +>a : Symbol(a, Decl(asyncMethodWithSuper_es2017.ts, 15, 13)) +>super.x : Symbol(A.x, Decl(asyncMethodWithSuper_es2017.ts, 0, 9)) +>super : Symbol(A, Decl(asyncMethodWithSuper_es2017.ts, 0, 0)) +>x : Symbol(A.x, Decl(asyncMethodWithSuper_es2017.ts, 0, 9)) + + // element access (read) + const b = super["x"]; +>b : Symbol(b, Decl(asyncMethodWithSuper_es2017.ts, 18, 13)) +>super : Symbol(A, Decl(asyncMethodWithSuper_es2017.ts, 0, 0)) +>"x" : Symbol(A.x, Decl(asyncMethodWithSuper_es2017.ts, 0, 9)) + } + + // async method with assignment/destructuring on 'super' requires a binding + async advanced() { +>advanced : Symbol(B.advanced, Decl(asyncMethodWithSuper_es2017.ts, 19, 5)) + + const f = () => {}; +>f : Symbol(f, Decl(asyncMethodWithSuper_es2017.ts, 23, 13)) + + // call with property access + super.x(); +>super.x : Symbol(A.x, Decl(asyncMethodWithSuper_es2017.ts, 0, 9)) +>super : Symbol(A, Decl(asyncMethodWithSuper_es2017.ts, 0, 0)) +>x : Symbol(A.x, Decl(asyncMethodWithSuper_es2017.ts, 0, 9)) + + // call with element access + super["x"](); +>super : Symbol(A, Decl(asyncMethodWithSuper_es2017.ts, 0, 0)) +>"x" : Symbol(A.x, Decl(asyncMethodWithSuper_es2017.ts, 0, 9)) + + // property access (read) + const a = super.x; +>a : Symbol(a, Decl(asyncMethodWithSuper_es2017.ts, 32, 13)) +>super.x : Symbol(A.x, Decl(asyncMethodWithSuper_es2017.ts, 0, 9)) +>super : Symbol(A, Decl(asyncMethodWithSuper_es2017.ts, 0, 0)) +>x : Symbol(A.x, Decl(asyncMethodWithSuper_es2017.ts, 0, 9)) + + // element access (read) + const b = super["x"]; +>b : Symbol(b, Decl(asyncMethodWithSuper_es2017.ts, 35, 13)) +>super : Symbol(A, Decl(asyncMethodWithSuper_es2017.ts, 0, 0)) +>"x" : Symbol(A.x, Decl(asyncMethodWithSuper_es2017.ts, 0, 9)) + + // property access (assign) + super.x = f; +>super.x : Symbol(A.x, Decl(asyncMethodWithSuper_es2017.ts, 0, 9)) +>super : Symbol(A, Decl(asyncMethodWithSuper_es2017.ts, 0, 0)) +>x : Symbol(A.x, Decl(asyncMethodWithSuper_es2017.ts, 0, 9)) +>f : Symbol(f, Decl(asyncMethodWithSuper_es2017.ts, 23, 13)) + + // element access (assign) + super["x"] = f; +>super : Symbol(A, Decl(asyncMethodWithSuper_es2017.ts, 0, 0)) +>"x" : Symbol(A.x, Decl(asyncMethodWithSuper_es2017.ts, 0, 9)) +>f : Symbol(f, Decl(asyncMethodWithSuper_es2017.ts, 23, 13)) + + // destructuring assign with property access + ({ f: super.x } = { f }); +>f : Symbol(f, Decl(asyncMethodWithSuper_es2017.ts, 44, 10)) +>super.x : Symbol(A.x, Decl(asyncMethodWithSuper_es2017.ts, 0, 9)) +>super : Symbol(A, Decl(asyncMethodWithSuper_es2017.ts, 0, 0)) +>x : Symbol(A.x, Decl(asyncMethodWithSuper_es2017.ts, 0, 9)) +>f : Symbol(f, Decl(asyncMethodWithSuper_es2017.ts, 44, 27)) + + // destructuring assign with element access + ({ f: super["x"] } = { f }); +>f : Symbol(f, Decl(asyncMethodWithSuper_es2017.ts, 47, 10)) +>super : Symbol(A, Decl(asyncMethodWithSuper_es2017.ts, 0, 0)) +>"x" : Symbol(A.x, Decl(asyncMethodWithSuper_es2017.ts, 0, 9)) +>f : Symbol(f, Decl(asyncMethodWithSuper_es2017.ts, 47, 30)) + } +} diff --git a/tests/baselines/reference/asyncMethodWithSuper_es2017.types b/tests/baselines/reference/asyncMethodWithSuper_es2017.types new file mode 100644 index 0000000000000..b2678e8b8379d --- /dev/null +++ b/tests/baselines/reference/asyncMethodWithSuper_es2017.types @@ -0,0 +1,123 @@ +=== tests/cases/conformance/async/es2017/asyncMethodWithSuper_es2017.ts === +class A { +>A : A + + x() { +>x : () => void + } +} + +class B extends A { +>B : B +>A : A + + // async method with only call/get on 'super' does not require a binding + async simple() { +>simple : () => Promise + + // call with property access + super.x(); +>super.x() : void +>super.x : () => void +>super : A +>x : () => void + + // call with element access + super["x"](); +>super["x"]() : void +>super["x"] : () => void +>super : A +>"x" : "x" + + // property access (read) + const a = super.x; +>a : () => void +>super.x : () => void +>super : A +>x : () => void + + // element access (read) + const b = super["x"]; +>b : () => void +>super["x"] : () => void +>super : A +>"x" : "x" + } + + // async method with assignment/destructuring on 'super' requires a binding + async advanced() { +>advanced : () => Promise + + const f = () => {}; +>f : () => void +>() => {} : () => void + + // call with property access + super.x(); +>super.x() : void +>super.x : () => void +>super : A +>x : () => void + + // call with element access + super["x"](); +>super["x"]() : void +>super["x"] : () => void +>super : A +>"x" : "x" + + // property access (read) + const a = super.x; +>a : () => void +>super.x : () => void +>super : A +>x : () => void + + // element access (read) + const b = super["x"]; +>b : () => void +>super["x"] : () => void +>super : A +>"x" : "x" + + // property access (assign) + super.x = f; +>super.x = f : () => void +>super.x : () => void +>super : A +>x : () => void +>f : () => void + + // element access (assign) + super["x"] = f; +>super["x"] = f : () => void +>super["x"] : () => void +>super : A +>"x" : "x" +>f : () => void + + // destructuring assign with property access + ({ f: super.x } = { f }); +>({ f: super.x } = { f }) : { f: () => void; } +>{ f: super.x } = { f } : { f: () => void; } +>{ f: super.x } : { f: () => void; } +>f : () => void +>super.x : () => void +>super : A +>x : () => void +>{ f } : { f: () => void; } +>f : () => void + + // destructuring assign with element access + ({ f: super["x"] } = { f }); +>({ f: super["x"] } = { f }) : { f: () => void; } +>{ f: super["x"] } = { f } : { f: () => void; } +>{ f: super["x"] } : { f: () => void; } +>f : () => void +>super["x"] : () => void +>super : A +>"x" : "x" +>{ f } : { f: () => void; } +>f : () => void + } +} diff --git a/tests/baselines/reference/asyncUnParenthesizedArrowFunction_es2017.js b/tests/baselines/reference/asyncUnParenthesizedArrowFunction_es2017.js new file mode 100644 index 0000000000000..c97fda43011dd --- /dev/null +++ b/tests/baselines/reference/asyncUnParenthesizedArrowFunction_es2017.js @@ -0,0 +1,9 @@ +//// [asyncUnParenthesizedArrowFunction_es2017.ts] + +declare function someOtherFunction(i: any): Promise; +const x = async i => await someOtherFunction(i) +const x1 = async (i) => await someOtherFunction(i); + +//// [asyncUnParenthesizedArrowFunction_es2017.js] +const x = async (i) => await someOtherFunction(i); +const x1 = async (i) => await someOtherFunction(i); diff --git a/tests/baselines/reference/asyncUnParenthesizedArrowFunction_es2017.symbols b/tests/baselines/reference/asyncUnParenthesizedArrowFunction_es2017.symbols new file mode 100644 index 0000000000000..b5f83de6021ae --- /dev/null +++ b/tests/baselines/reference/asyncUnParenthesizedArrowFunction_es2017.symbols @@ -0,0 +1,19 @@ +=== tests/cases/conformance/async/es2017/asyncArrowFunction/asyncUnParenthesizedArrowFunction_es2017.ts === + +declare function someOtherFunction(i: any): Promise; +>someOtherFunction : Symbol(someOtherFunction, Decl(asyncUnParenthesizedArrowFunction_es2017.ts, 0, 0)) +>i : Symbol(i, Decl(asyncUnParenthesizedArrowFunction_es2017.ts, 1, 35)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + +const x = async i => await someOtherFunction(i) +>x : Symbol(x, Decl(asyncUnParenthesizedArrowFunction_es2017.ts, 2, 5)) +>i : Symbol(i, Decl(asyncUnParenthesizedArrowFunction_es2017.ts, 2, 15)) +>someOtherFunction : Symbol(someOtherFunction, Decl(asyncUnParenthesizedArrowFunction_es2017.ts, 0, 0)) +>i : Symbol(i, Decl(asyncUnParenthesizedArrowFunction_es2017.ts, 2, 15)) + +const x1 = async (i) => await someOtherFunction(i); +>x1 : Symbol(x1, Decl(asyncUnParenthesizedArrowFunction_es2017.ts, 3, 5)) +>i : Symbol(i, Decl(asyncUnParenthesizedArrowFunction_es2017.ts, 3, 18)) +>someOtherFunction : Symbol(someOtherFunction, Decl(asyncUnParenthesizedArrowFunction_es2017.ts, 0, 0)) +>i : Symbol(i, Decl(asyncUnParenthesizedArrowFunction_es2017.ts, 3, 18)) + diff --git a/tests/baselines/reference/asyncUnParenthesizedArrowFunction_es2017.types b/tests/baselines/reference/asyncUnParenthesizedArrowFunction_es2017.types new file mode 100644 index 0000000000000..ff573e29ed367 --- /dev/null +++ b/tests/baselines/reference/asyncUnParenthesizedArrowFunction_es2017.types @@ -0,0 +1,25 @@ +=== tests/cases/conformance/async/es2017/asyncArrowFunction/asyncUnParenthesizedArrowFunction_es2017.ts === + +declare function someOtherFunction(i: any): Promise; +>someOtherFunction : (i: any) => Promise +>i : any +>Promise : Promise + +const x = async i => await someOtherFunction(i) +>x : (i: any) => Promise +>async i => await someOtherFunction(i) : (i: any) => Promise +>i : any +>await someOtherFunction(i) : void +>someOtherFunction(i) : Promise +>someOtherFunction : (i: any) => Promise +>i : any + +const x1 = async (i) => await someOtherFunction(i); +>x1 : (i: any) => Promise +>async (i) => await someOtherFunction(i) : (i: any) => Promise +>i : any +>await someOtherFunction(i) : void +>someOtherFunction(i) : Promise +>someOtherFunction : (i: any) => Promise +>i : any + diff --git a/tests/baselines/reference/asyncUseStrict_es2017.js b/tests/baselines/reference/asyncUseStrict_es2017.js new file mode 100644 index 0000000000000..1b9d1dc7f205f --- /dev/null +++ b/tests/baselines/reference/asyncUseStrict_es2017.js @@ -0,0 +1,13 @@ +//// [asyncUseStrict_es2017.ts] +declare var a: boolean; +declare var p: Promise; +async function func(): Promise { + "use strict"; + var b = await p || a; +} + +//// [asyncUseStrict_es2017.js] +async function func() { + "use strict"; + var b = await p || a; +} diff --git a/tests/baselines/reference/asyncUseStrict_es2017.symbols b/tests/baselines/reference/asyncUseStrict_es2017.symbols new file mode 100644 index 0000000000000..23fd654ca29d1 --- /dev/null +++ b/tests/baselines/reference/asyncUseStrict_es2017.symbols @@ -0,0 +1,18 @@ +=== tests/cases/conformance/async/es2017/asyncUseStrict_es2017.ts === +declare var a: boolean; +>a : Symbol(a, Decl(asyncUseStrict_es2017.ts, 0, 11)) + +declare var p: Promise; +>p : Symbol(p, Decl(asyncUseStrict_es2017.ts, 1, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + +async function func(): Promise { +>func : Symbol(func, Decl(asyncUseStrict_es2017.ts, 1, 32)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + + "use strict"; + var b = await p || a; +>b : Symbol(b, Decl(asyncUseStrict_es2017.ts, 4, 7)) +>p : Symbol(p, Decl(asyncUseStrict_es2017.ts, 1, 11)) +>a : Symbol(a, Decl(asyncUseStrict_es2017.ts, 0, 11)) +} diff --git a/tests/baselines/reference/asyncUseStrict_es2017.types b/tests/baselines/reference/asyncUseStrict_es2017.types new file mode 100644 index 0000000000000..267eda835251b --- /dev/null +++ b/tests/baselines/reference/asyncUseStrict_es2017.types @@ -0,0 +1,22 @@ +=== tests/cases/conformance/async/es2017/asyncUseStrict_es2017.ts === +declare var a: boolean; +>a : boolean + +declare var p: Promise; +>p : Promise +>Promise : Promise + +async function func(): Promise { +>func : () => Promise +>Promise : Promise + + "use strict"; +>"use strict" : "use strict" + + var b = await p || a; +>b : boolean +>await p || a : boolean +>await p : boolean +>p : Promise +>a : boolean +} diff --git a/tests/baselines/reference/awaitBinaryExpression1_es2017.js b/tests/baselines/reference/awaitBinaryExpression1_es2017.js new file mode 100644 index 0000000000000..9016a2b58269c --- /dev/null +++ b/tests/baselines/reference/awaitBinaryExpression1_es2017.js @@ -0,0 +1,17 @@ +//// [awaitBinaryExpression1_es2017.ts] +declare var a: boolean; +declare var p: Promise; +declare function before(): void; +declare function after(): void; +async function func(): Promise { + before(); + var b = await p || a; + after(); +} + +//// [awaitBinaryExpression1_es2017.js] +async function func() { + before(); + var b = await p || a; + after(); +} diff --git a/tests/baselines/reference/awaitBinaryExpression1_es2017.symbols b/tests/baselines/reference/awaitBinaryExpression1_es2017.symbols new file mode 100644 index 0000000000000..827769b3db5c9 --- /dev/null +++ b/tests/baselines/reference/awaitBinaryExpression1_es2017.symbols @@ -0,0 +1,29 @@ +=== tests/cases/conformance/async/es2017/awaitBinaryExpression/awaitBinaryExpression1_es2017.ts === +declare var a: boolean; +>a : Symbol(a, Decl(awaitBinaryExpression1_es2017.ts, 0, 11)) + +declare var p: Promise; +>p : Symbol(p, Decl(awaitBinaryExpression1_es2017.ts, 1, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + +declare function before(): void; +>before : Symbol(before, Decl(awaitBinaryExpression1_es2017.ts, 1, 32)) + +declare function after(): void; +>after : Symbol(after, Decl(awaitBinaryExpression1_es2017.ts, 2, 32)) + +async function func(): Promise { +>func : Symbol(func, Decl(awaitBinaryExpression1_es2017.ts, 3, 31)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + + before(); +>before : Symbol(before, Decl(awaitBinaryExpression1_es2017.ts, 1, 32)) + + var b = await p || a; +>b : Symbol(b, Decl(awaitBinaryExpression1_es2017.ts, 6, 7)) +>p : Symbol(p, Decl(awaitBinaryExpression1_es2017.ts, 1, 11)) +>a : Symbol(a, Decl(awaitBinaryExpression1_es2017.ts, 0, 11)) + + after(); +>after : Symbol(after, Decl(awaitBinaryExpression1_es2017.ts, 2, 32)) +} diff --git a/tests/baselines/reference/awaitBinaryExpression1_es2017.types b/tests/baselines/reference/awaitBinaryExpression1_es2017.types new file mode 100644 index 0000000000000..c86631804c598 --- /dev/null +++ b/tests/baselines/reference/awaitBinaryExpression1_es2017.types @@ -0,0 +1,33 @@ +=== tests/cases/conformance/async/es2017/awaitBinaryExpression/awaitBinaryExpression1_es2017.ts === +declare var a: boolean; +>a : boolean + +declare var p: Promise; +>p : Promise +>Promise : Promise + +declare function before(): void; +>before : () => void + +declare function after(): void; +>after : () => void + +async function func(): Promise { +>func : () => Promise +>Promise : Promise + + before(); +>before() : void +>before : () => void + + var b = await p || a; +>b : boolean +>await p || a : boolean +>await p : boolean +>p : Promise +>a : boolean + + after(); +>after() : void +>after : () => void +} diff --git a/tests/baselines/reference/awaitBinaryExpression2_es2017.js b/tests/baselines/reference/awaitBinaryExpression2_es2017.js new file mode 100644 index 0000000000000..1d5ad324fdab7 --- /dev/null +++ b/tests/baselines/reference/awaitBinaryExpression2_es2017.js @@ -0,0 +1,17 @@ +//// [awaitBinaryExpression2_es2017.ts] +declare var a: boolean; +declare var p: Promise; +declare function before(): void; +declare function after(): void; +async function func(): Promise { + before(); + var b = await p && a; + after(); +} + +//// [awaitBinaryExpression2_es2017.js] +async function func() { + before(); + var b = await p && a; + after(); +} diff --git a/tests/baselines/reference/awaitBinaryExpression2_es2017.symbols b/tests/baselines/reference/awaitBinaryExpression2_es2017.symbols new file mode 100644 index 0000000000000..549eb49cf9297 --- /dev/null +++ b/tests/baselines/reference/awaitBinaryExpression2_es2017.symbols @@ -0,0 +1,29 @@ +=== tests/cases/conformance/async/es2017/awaitBinaryExpression/awaitBinaryExpression2_es2017.ts === +declare var a: boolean; +>a : Symbol(a, Decl(awaitBinaryExpression2_es2017.ts, 0, 11)) + +declare var p: Promise; +>p : Symbol(p, Decl(awaitBinaryExpression2_es2017.ts, 1, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + +declare function before(): void; +>before : Symbol(before, Decl(awaitBinaryExpression2_es2017.ts, 1, 32)) + +declare function after(): void; +>after : Symbol(after, Decl(awaitBinaryExpression2_es2017.ts, 2, 32)) + +async function func(): Promise { +>func : Symbol(func, Decl(awaitBinaryExpression2_es2017.ts, 3, 31)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + + before(); +>before : Symbol(before, Decl(awaitBinaryExpression2_es2017.ts, 1, 32)) + + var b = await p && a; +>b : Symbol(b, Decl(awaitBinaryExpression2_es2017.ts, 6, 7)) +>p : Symbol(p, Decl(awaitBinaryExpression2_es2017.ts, 1, 11)) +>a : Symbol(a, Decl(awaitBinaryExpression2_es2017.ts, 0, 11)) + + after(); +>after : Symbol(after, Decl(awaitBinaryExpression2_es2017.ts, 2, 32)) +} diff --git a/tests/baselines/reference/awaitBinaryExpression2_es2017.types b/tests/baselines/reference/awaitBinaryExpression2_es2017.types new file mode 100644 index 0000000000000..f803b1ac8402b --- /dev/null +++ b/tests/baselines/reference/awaitBinaryExpression2_es2017.types @@ -0,0 +1,33 @@ +=== tests/cases/conformance/async/es2017/awaitBinaryExpression/awaitBinaryExpression2_es2017.ts === +declare var a: boolean; +>a : boolean + +declare var p: Promise; +>p : Promise +>Promise : Promise + +declare function before(): void; +>before : () => void + +declare function after(): void; +>after : () => void + +async function func(): Promise { +>func : () => Promise +>Promise : Promise + + before(); +>before() : void +>before : () => void + + var b = await p && a; +>b : boolean +>await p && a : boolean +>await p : boolean +>p : Promise +>a : boolean + + after(); +>after() : void +>after : () => void +} diff --git a/tests/baselines/reference/awaitBinaryExpression3_es2017.js b/tests/baselines/reference/awaitBinaryExpression3_es2017.js new file mode 100644 index 0000000000000..c752ec3be4a56 --- /dev/null +++ b/tests/baselines/reference/awaitBinaryExpression3_es2017.js @@ -0,0 +1,17 @@ +//// [awaitBinaryExpression3_es2017.ts] +declare var a: number; +declare var p: Promise; +declare function before(): void; +declare function after(): void; +async function func(): Promise { + before(); + var b = await p + a; + after(); +} + +//// [awaitBinaryExpression3_es2017.js] +async function func() { + before(); + var b = await p + a; + after(); +} diff --git a/tests/baselines/reference/awaitBinaryExpression3_es2017.symbols b/tests/baselines/reference/awaitBinaryExpression3_es2017.symbols new file mode 100644 index 0000000000000..29c8fb7f398ef --- /dev/null +++ b/tests/baselines/reference/awaitBinaryExpression3_es2017.symbols @@ -0,0 +1,29 @@ +=== tests/cases/conformance/async/es2017/awaitBinaryExpression/awaitBinaryExpression3_es2017.ts === +declare var a: number; +>a : Symbol(a, Decl(awaitBinaryExpression3_es2017.ts, 0, 11)) + +declare var p: Promise; +>p : Symbol(p, Decl(awaitBinaryExpression3_es2017.ts, 1, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + +declare function before(): void; +>before : Symbol(before, Decl(awaitBinaryExpression3_es2017.ts, 1, 31)) + +declare function after(): void; +>after : Symbol(after, Decl(awaitBinaryExpression3_es2017.ts, 2, 32)) + +async function func(): Promise { +>func : Symbol(func, Decl(awaitBinaryExpression3_es2017.ts, 3, 31)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + + before(); +>before : Symbol(before, Decl(awaitBinaryExpression3_es2017.ts, 1, 31)) + + var b = await p + a; +>b : Symbol(b, Decl(awaitBinaryExpression3_es2017.ts, 6, 7)) +>p : Symbol(p, Decl(awaitBinaryExpression3_es2017.ts, 1, 11)) +>a : Symbol(a, Decl(awaitBinaryExpression3_es2017.ts, 0, 11)) + + after(); +>after : Symbol(after, Decl(awaitBinaryExpression3_es2017.ts, 2, 32)) +} diff --git a/tests/baselines/reference/awaitBinaryExpression3_es2017.types b/tests/baselines/reference/awaitBinaryExpression3_es2017.types new file mode 100644 index 0000000000000..623d06952374f --- /dev/null +++ b/tests/baselines/reference/awaitBinaryExpression3_es2017.types @@ -0,0 +1,33 @@ +=== tests/cases/conformance/async/es2017/awaitBinaryExpression/awaitBinaryExpression3_es2017.ts === +declare var a: number; +>a : number + +declare var p: Promise; +>p : Promise +>Promise : Promise + +declare function before(): void; +>before : () => void + +declare function after(): void; +>after : () => void + +async function func(): Promise { +>func : () => Promise +>Promise : Promise + + before(); +>before() : void +>before : () => void + + var b = await p + a; +>b : number +>await p + a : number +>await p : number +>p : Promise +>a : number + + after(); +>after() : void +>after : () => void +} diff --git a/tests/baselines/reference/awaitBinaryExpression4_es2017.js b/tests/baselines/reference/awaitBinaryExpression4_es2017.js new file mode 100644 index 0000000000000..3fc296ea14250 --- /dev/null +++ b/tests/baselines/reference/awaitBinaryExpression4_es2017.js @@ -0,0 +1,17 @@ +//// [awaitBinaryExpression4_es2017.ts] +declare var a: boolean; +declare var p: Promise; +declare function before(): void; +declare function after(): void; +async function func(): Promise { + before(); + var b = (await p, a); + after(); +} + +//// [awaitBinaryExpression4_es2017.js] +async function func() { + before(); + var b = (await p, a); + after(); +} diff --git a/tests/baselines/reference/awaitBinaryExpression4_es2017.symbols b/tests/baselines/reference/awaitBinaryExpression4_es2017.symbols new file mode 100644 index 0000000000000..0db5a20e0c11e --- /dev/null +++ b/tests/baselines/reference/awaitBinaryExpression4_es2017.symbols @@ -0,0 +1,29 @@ +=== tests/cases/conformance/async/es2017/awaitBinaryExpression/awaitBinaryExpression4_es2017.ts === +declare var a: boolean; +>a : Symbol(a, Decl(awaitBinaryExpression4_es2017.ts, 0, 11)) + +declare var p: Promise; +>p : Symbol(p, Decl(awaitBinaryExpression4_es2017.ts, 1, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + +declare function before(): void; +>before : Symbol(before, Decl(awaitBinaryExpression4_es2017.ts, 1, 32)) + +declare function after(): void; +>after : Symbol(after, Decl(awaitBinaryExpression4_es2017.ts, 2, 32)) + +async function func(): Promise { +>func : Symbol(func, Decl(awaitBinaryExpression4_es2017.ts, 3, 31)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + + before(); +>before : Symbol(before, Decl(awaitBinaryExpression4_es2017.ts, 1, 32)) + + var b = (await p, a); +>b : Symbol(b, Decl(awaitBinaryExpression4_es2017.ts, 6, 7)) +>p : Symbol(p, Decl(awaitBinaryExpression4_es2017.ts, 1, 11)) +>a : Symbol(a, Decl(awaitBinaryExpression4_es2017.ts, 0, 11)) + + after(); +>after : Symbol(after, Decl(awaitBinaryExpression4_es2017.ts, 2, 32)) +} diff --git a/tests/baselines/reference/awaitBinaryExpression4_es2017.types b/tests/baselines/reference/awaitBinaryExpression4_es2017.types new file mode 100644 index 0000000000000..714b6b1ea3a59 --- /dev/null +++ b/tests/baselines/reference/awaitBinaryExpression4_es2017.types @@ -0,0 +1,34 @@ +=== tests/cases/conformance/async/es2017/awaitBinaryExpression/awaitBinaryExpression4_es2017.ts === +declare var a: boolean; +>a : boolean + +declare var p: Promise; +>p : Promise +>Promise : Promise + +declare function before(): void; +>before : () => void + +declare function after(): void; +>after : () => void + +async function func(): Promise { +>func : () => Promise +>Promise : Promise + + before(); +>before() : void +>before : () => void + + var b = (await p, a); +>b : boolean +>(await p, a) : boolean +>await p, a : boolean +>await p : boolean +>p : Promise +>a : boolean + + after(); +>after() : void +>after : () => void +} diff --git a/tests/baselines/reference/awaitBinaryExpression5_es2017.js b/tests/baselines/reference/awaitBinaryExpression5_es2017.js new file mode 100644 index 0000000000000..b6c5e12ce761f --- /dev/null +++ b/tests/baselines/reference/awaitBinaryExpression5_es2017.js @@ -0,0 +1,19 @@ +//// [awaitBinaryExpression5_es2017.ts] +declare var a: boolean; +declare var p: Promise; +declare function before(): void; +declare function after(): void; +async function func(): Promise { + before(); + var o: { a: boolean; }; + o.a = await p; + after(); +} + +//// [awaitBinaryExpression5_es2017.js] +async function func() { + before(); + var o; + o.a = await p; + after(); +} diff --git a/tests/baselines/reference/awaitBinaryExpression5_es2017.symbols b/tests/baselines/reference/awaitBinaryExpression5_es2017.symbols new file mode 100644 index 0000000000000..e2d30c658f711 --- /dev/null +++ b/tests/baselines/reference/awaitBinaryExpression5_es2017.symbols @@ -0,0 +1,34 @@ +=== tests/cases/conformance/async/es2017/awaitBinaryExpression/awaitBinaryExpression5_es2017.ts === +declare var a: boolean; +>a : Symbol(a, Decl(awaitBinaryExpression5_es2017.ts, 0, 11)) + +declare var p: Promise; +>p : Symbol(p, Decl(awaitBinaryExpression5_es2017.ts, 1, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + +declare function before(): void; +>before : Symbol(before, Decl(awaitBinaryExpression5_es2017.ts, 1, 32)) + +declare function after(): void; +>after : Symbol(after, Decl(awaitBinaryExpression5_es2017.ts, 2, 32)) + +async function func(): Promise { +>func : Symbol(func, Decl(awaitBinaryExpression5_es2017.ts, 3, 31)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + + before(); +>before : Symbol(before, Decl(awaitBinaryExpression5_es2017.ts, 1, 32)) + + var o: { a: boolean; }; +>o : Symbol(o, Decl(awaitBinaryExpression5_es2017.ts, 6, 7)) +>a : Symbol(a, Decl(awaitBinaryExpression5_es2017.ts, 6, 12)) + + o.a = await p; +>o.a : Symbol(a, Decl(awaitBinaryExpression5_es2017.ts, 6, 12)) +>o : Symbol(o, Decl(awaitBinaryExpression5_es2017.ts, 6, 7)) +>a : Symbol(a, Decl(awaitBinaryExpression5_es2017.ts, 6, 12)) +>p : Symbol(p, Decl(awaitBinaryExpression5_es2017.ts, 1, 11)) + + after(); +>after : Symbol(after, Decl(awaitBinaryExpression5_es2017.ts, 2, 32)) +} diff --git a/tests/baselines/reference/awaitBinaryExpression5_es2017.types b/tests/baselines/reference/awaitBinaryExpression5_es2017.types new file mode 100644 index 0000000000000..763f9e5086289 --- /dev/null +++ b/tests/baselines/reference/awaitBinaryExpression5_es2017.types @@ -0,0 +1,38 @@ +=== tests/cases/conformance/async/es2017/awaitBinaryExpression/awaitBinaryExpression5_es2017.ts === +declare var a: boolean; +>a : boolean + +declare var p: Promise; +>p : Promise +>Promise : Promise + +declare function before(): void; +>before : () => void + +declare function after(): void; +>after : () => void + +async function func(): Promise { +>func : () => Promise +>Promise : Promise + + before(); +>before() : void +>before : () => void + + var o: { a: boolean; }; +>o : { a: boolean; } +>a : boolean + + o.a = await p; +>o.a = await p : boolean +>o.a : boolean +>o : { a: boolean; } +>a : boolean +>await p : boolean +>p : Promise + + after(); +>after() : void +>after : () => void +} diff --git a/tests/baselines/reference/awaitCallExpression1_es2017.js b/tests/baselines/reference/awaitCallExpression1_es2017.js new file mode 100644 index 0000000000000..89fe7dfd91269 --- /dev/null +++ b/tests/baselines/reference/awaitCallExpression1_es2017.js @@ -0,0 +1,21 @@ +//// [awaitCallExpression1_es2017.ts] +declare var a: boolean; +declare var p: Promise; +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare function before(): void; +declare function after(): void; +async function func(): Promise { + before(); + var b = fn(a, a, a); + after(); +} + +//// [awaitCallExpression1_es2017.js] +async function func() { + before(); + var b = fn(a, a, a); + after(); +} diff --git a/tests/baselines/reference/awaitCallExpression1_es2017.symbols b/tests/baselines/reference/awaitCallExpression1_es2017.symbols new file mode 100644 index 0000000000000..68c28d153507e --- /dev/null +++ b/tests/baselines/reference/awaitCallExpression1_es2017.symbols @@ -0,0 +1,59 @@ +=== tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression1_es2017.ts === +declare var a: boolean; +>a : Symbol(a, Decl(awaitCallExpression1_es2017.ts, 0, 11)) + +declare var p: Promise; +>p : Symbol(p, Decl(awaitCallExpression1_es2017.ts, 1, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +>fn : Symbol(fn, Decl(awaitCallExpression1_es2017.ts, 1, 32)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression1_es2017.ts, 2, 20)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression1_es2017.ts, 2, 34)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression1_es2017.ts, 2, 49)) + +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +>o : Symbol(o, Decl(awaitCallExpression1_es2017.ts, 3, 11)) +>fn : Symbol(fn, Decl(awaitCallExpression1_es2017.ts, 3, 16)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression1_es2017.ts, 3, 20)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression1_es2017.ts, 3, 34)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression1_es2017.ts, 3, 49)) + +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>pfn : Symbol(pfn, Decl(awaitCallExpression1_es2017.ts, 4, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression1_es2017.ts, 4, 28)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression1_es2017.ts, 4, 42)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression1_es2017.ts, 4, 57)) + +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>po : Symbol(po, Decl(awaitCallExpression1_es2017.ts, 5, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) +>fn : Symbol(fn, Decl(awaitCallExpression1_es2017.ts, 5, 25)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression1_es2017.ts, 5, 29)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression1_es2017.ts, 5, 43)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression1_es2017.ts, 5, 58)) + +declare function before(): void; +>before : Symbol(before, Decl(awaitCallExpression1_es2017.ts, 5, 84)) + +declare function after(): void; +>after : Symbol(after, Decl(awaitCallExpression1_es2017.ts, 6, 32)) + +async function func(): Promise { +>func : Symbol(func, Decl(awaitCallExpression1_es2017.ts, 7, 31)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + + before(); +>before : Symbol(before, Decl(awaitCallExpression1_es2017.ts, 5, 84)) + + var b = fn(a, a, a); +>b : Symbol(b, Decl(awaitCallExpression1_es2017.ts, 10, 7)) +>fn : Symbol(fn, Decl(awaitCallExpression1_es2017.ts, 1, 32)) +>a : Symbol(a, Decl(awaitCallExpression1_es2017.ts, 0, 11)) +>a : Symbol(a, Decl(awaitCallExpression1_es2017.ts, 0, 11)) +>a : Symbol(a, Decl(awaitCallExpression1_es2017.ts, 0, 11)) + + after(); +>after : Symbol(after, Decl(awaitCallExpression1_es2017.ts, 6, 32)) +} diff --git a/tests/baselines/reference/awaitCallExpression1_es2017.types b/tests/baselines/reference/awaitCallExpression1_es2017.types new file mode 100644 index 0000000000000..c5cea09187866 --- /dev/null +++ b/tests/baselines/reference/awaitCallExpression1_es2017.types @@ -0,0 +1,62 @@ +=== tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression1_es2017.ts === +declare var a: boolean; +>a : boolean + +declare var p: Promise; +>p : Promise +>Promise : Promise + +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +>o : { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; } +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>pfn : Promise<(arg0: boolean, arg1: boolean, arg2: boolean) => void> +>Promise : Promise +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>po : Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }> +>Promise : Promise +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare function before(): void; +>before : () => void + +declare function after(): void; +>after : () => void + +async function func(): Promise { +>func : () => Promise +>Promise : Promise + + before(); +>before() : void +>before : () => void + + var b = fn(a, a, a); +>b : void +>fn(a, a, a) : void +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>a : boolean +>a : boolean +>a : boolean + + after(); +>after() : void +>after : () => void +} diff --git a/tests/baselines/reference/awaitCallExpression2_es2017.js b/tests/baselines/reference/awaitCallExpression2_es2017.js new file mode 100644 index 0000000000000..24b3012f85d33 --- /dev/null +++ b/tests/baselines/reference/awaitCallExpression2_es2017.js @@ -0,0 +1,21 @@ +//// [awaitCallExpression2_es2017.ts] +declare var a: boolean; +declare var p: Promise; +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare function before(): void; +declare function after(): void; +async function func(): Promise { + before(); + var b = fn(await p, a, a); + after(); +} + +//// [awaitCallExpression2_es2017.js] +async function func() { + before(); + var b = fn(await p, a, a); + after(); +} diff --git a/tests/baselines/reference/awaitCallExpression2_es2017.symbols b/tests/baselines/reference/awaitCallExpression2_es2017.symbols new file mode 100644 index 0000000000000..4528ae4c0743d --- /dev/null +++ b/tests/baselines/reference/awaitCallExpression2_es2017.symbols @@ -0,0 +1,59 @@ +=== tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression2_es2017.ts === +declare var a: boolean; +>a : Symbol(a, Decl(awaitCallExpression2_es2017.ts, 0, 11)) + +declare var p: Promise; +>p : Symbol(p, Decl(awaitCallExpression2_es2017.ts, 1, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +>fn : Symbol(fn, Decl(awaitCallExpression2_es2017.ts, 1, 32)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression2_es2017.ts, 2, 20)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression2_es2017.ts, 2, 34)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression2_es2017.ts, 2, 49)) + +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +>o : Symbol(o, Decl(awaitCallExpression2_es2017.ts, 3, 11)) +>fn : Symbol(fn, Decl(awaitCallExpression2_es2017.ts, 3, 16)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression2_es2017.ts, 3, 20)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression2_es2017.ts, 3, 34)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression2_es2017.ts, 3, 49)) + +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>pfn : Symbol(pfn, Decl(awaitCallExpression2_es2017.ts, 4, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression2_es2017.ts, 4, 28)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression2_es2017.ts, 4, 42)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression2_es2017.ts, 4, 57)) + +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>po : Symbol(po, Decl(awaitCallExpression2_es2017.ts, 5, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) +>fn : Symbol(fn, Decl(awaitCallExpression2_es2017.ts, 5, 25)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression2_es2017.ts, 5, 29)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression2_es2017.ts, 5, 43)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression2_es2017.ts, 5, 58)) + +declare function before(): void; +>before : Symbol(before, Decl(awaitCallExpression2_es2017.ts, 5, 84)) + +declare function after(): void; +>after : Symbol(after, Decl(awaitCallExpression2_es2017.ts, 6, 32)) + +async function func(): Promise { +>func : Symbol(func, Decl(awaitCallExpression2_es2017.ts, 7, 31)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + + before(); +>before : Symbol(before, Decl(awaitCallExpression2_es2017.ts, 5, 84)) + + var b = fn(await p, a, a); +>b : Symbol(b, Decl(awaitCallExpression2_es2017.ts, 10, 7)) +>fn : Symbol(fn, Decl(awaitCallExpression2_es2017.ts, 1, 32)) +>p : Symbol(p, Decl(awaitCallExpression2_es2017.ts, 1, 11)) +>a : Symbol(a, Decl(awaitCallExpression2_es2017.ts, 0, 11)) +>a : Symbol(a, Decl(awaitCallExpression2_es2017.ts, 0, 11)) + + after(); +>after : Symbol(after, Decl(awaitCallExpression2_es2017.ts, 6, 32)) +} diff --git a/tests/baselines/reference/awaitCallExpression2_es2017.types b/tests/baselines/reference/awaitCallExpression2_es2017.types new file mode 100644 index 0000000000000..a15609a40782a --- /dev/null +++ b/tests/baselines/reference/awaitCallExpression2_es2017.types @@ -0,0 +1,63 @@ +=== tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression2_es2017.ts === +declare var a: boolean; +>a : boolean + +declare var p: Promise; +>p : Promise +>Promise : Promise + +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +>o : { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; } +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>pfn : Promise<(arg0: boolean, arg1: boolean, arg2: boolean) => void> +>Promise : Promise +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>po : Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }> +>Promise : Promise +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare function before(): void; +>before : () => void + +declare function after(): void; +>after : () => void + +async function func(): Promise { +>func : () => Promise +>Promise : Promise + + before(); +>before() : void +>before : () => void + + var b = fn(await p, a, a); +>b : void +>fn(await p, a, a) : void +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>await p : boolean +>p : Promise +>a : boolean +>a : boolean + + after(); +>after() : void +>after : () => void +} diff --git a/tests/baselines/reference/awaitCallExpression3_es2017.js b/tests/baselines/reference/awaitCallExpression3_es2017.js new file mode 100644 index 0000000000000..6b432851b000c --- /dev/null +++ b/tests/baselines/reference/awaitCallExpression3_es2017.js @@ -0,0 +1,21 @@ +//// [awaitCallExpression3_es2017.ts] +declare var a: boolean; +declare var p: Promise; +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare function before(): void; +declare function after(): void; +async function func(): Promise { + before(); + var b = fn(a, await p, a); + after(); +} + +//// [awaitCallExpression3_es2017.js] +async function func() { + before(); + var b = fn(a, await p, a); + after(); +} diff --git a/tests/baselines/reference/awaitCallExpression3_es2017.symbols b/tests/baselines/reference/awaitCallExpression3_es2017.symbols new file mode 100644 index 0000000000000..a1120103fb070 --- /dev/null +++ b/tests/baselines/reference/awaitCallExpression3_es2017.symbols @@ -0,0 +1,59 @@ +=== tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression3_es2017.ts === +declare var a: boolean; +>a : Symbol(a, Decl(awaitCallExpression3_es2017.ts, 0, 11)) + +declare var p: Promise; +>p : Symbol(p, Decl(awaitCallExpression3_es2017.ts, 1, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +>fn : Symbol(fn, Decl(awaitCallExpression3_es2017.ts, 1, 32)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression3_es2017.ts, 2, 20)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression3_es2017.ts, 2, 34)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression3_es2017.ts, 2, 49)) + +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +>o : Symbol(o, Decl(awaitCallExpression3_es2017.ts, 3, 11)) +>fn : Symbol(fn, Decl(awaitCallExpression3_es2017.ts, 3, 16)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression3_es2017.ts, 3, 20)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression3_es2017.ts, 3, 34)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression3_es2017.ts, 3, 49)) + +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>pfn : Symbol(pfn, Decl(awaitCallExpression3_es2017.ts, 4, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression3_es2017.ts, 4, 28)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression3_es2017.ts, 4, 42)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression3_es2017.ts, 4, 57)) + +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>po : Symbol(po, Decl(awaitCallExpression3_es2017.ts, 5, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) +>fn : Symbol(fn, Decl(awaitCallExpression3_es2017.ts, 5, 25)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression3_es2017.ts, 5, 29)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression3_es2017.ts, 5, 43)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression3_es2017.ts, 5, 58)) + +declare function before(): void; +>before : Symbol(before, Decl(awaitCallExpression3_es2017.ts, 5, 84)) + +declare function after(): void; +>after : Symbol(after, Decl(awaitCallExpression3_es2017.ts, 6, 32)) + +async function func(): Promise { +>func : Symbol(func, Decl(awaitCallExpression3_es2017.ts, 7, 31)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + + before(); +>before : Symbol(before, Decl(awaitCallExpression3_es2017.ts, 5, 84)) + + var b = fn(a, await p, a); +>b : Symbol(b, Decl(awaitCallExpression3_es2017.ts, 10, 7)) +>fn : Symbol(fn, Decl(awaitCallExpression3_es2017.ts, 1, 32)) +>a : Symbol(a, Decl(awaitCallExpression3_es2017.ts, 0, 11)) +>p : Symbol(p, Decl(awaitCallExpression3_es2017.ts, 1, 11)) +>a : Symbol(a, Decl(awaitCallExpression3_es2017.ts, 0, 11)) + + after(); +>after : Symbol(after, Decl(awaitCallExpression3_es2017.ts, 6, 32)) +} diff --git a/tests/baselines/reference/awaitCallExpression3_es2017.types b/tests/baselines/reference/awaitCallExpression3_es2017.types new file mode 100644 index 0000000000000..a491836fa0610 --- /dev/null +++ b/tests/baselines/reference/awaitCallExpression3_es2017.types @@ -0,0 +1,63 @@ +=== tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression3_es2017.ts === +declare var a: boolean; +>a : boolean + +declare var p: Promise; +>p : Promise +>Promise : Promise + +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +>o : { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; } +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>pfn : Promise<(arg0: boolean, arg1: boolean, arg2: boolean) => void> +>Promise : Promise +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>po : Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }> +>Promise : Promise +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare function before(): void; +>before : () => void + +declare function after(): void; +>after : () => void + +async function func(): Promise { +>func : () => Promise +>Promise : Promise + + before(); +>before() : void +>before : () => void + + var b = fn(a, await p, a); +>b : void +>fn(a, await p, a) : void +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>a : boolean +>await p : boolean +>p : Promise +>a : boolean + + after(); +>after() : void +>after : () => void +} diff --git a/tests/baselines/reference/awaitCallExpression4_es2017.js b/tests/baselines/reference/awaitCallExpression4_es2017.js new file mode 100644 index 0000000000000..9243ac58f82ab --- /dev/null +++ b/tests/baselines/reference/awaitCallExpression4_es2017.js @@ -0,0 +1,21 @@ +//// [awaitCallExpression4_es2017.ts] +declare var a: boolean; +declare var p: Promise; +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare function before(): void; +declare function after(): void; +async function func(): Promise { + before(); + var b = (await pfn)(a, a, a); + after(); +} + +//// [awaitCallExpression4_es2017.js] +async function func() { + before(); + var b = (await pfn)(a, a, a); + after(); +} diff --git a/tests/baselines/reference/awaitCallExpression4_es2017.symbols b/tests/baselines/reference/awaitCallExpression4_es2017.symbols new file mode 100644 index 0000000000000..fb607d9841f4f --- /dev/null +++ b/tests/baselines/reference/awaitCallExpression4_es2017.symbols @@ -0,0 +1,59 @@ +=== tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression4_es2017.ts === +declare var a: boolean; +>a : Symbol(a, Decl(awaitCallExpression4_es2017.ts, 0, 11)) + +declare var p: Promise; +>p : Symbol(p, Decl(awaitCallExpression4_es2017.ts, 1, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +>fn : Symbol(fn, Decl(awaitCallExpression4_es2017.ts, 1, 32)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression4_es2017.ts, 2, 20)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression4_es2017.ts, 2, 34)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression4_es2017.ts, 2, 49)) + +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +>o : Symbol(o, Decl(awaitCallExpression4_es2017.ts, 3, 11)) +>fn : Symbol(fn, Decl(awaitCallExpression4_es2017.ts, 3, 16)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression4_es2017.ts, 3, 20)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression4_es2017.ts, 3, 34)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression4_es2017.ts, 3, 49)) + +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>pfn : Symbol(pfn, Decl(awaitCallExpression4_es2017.ts, 4, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression4_es2017.ts, 4, 28)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression4_es2017.ts, 4, 42)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression4_es2017.ts, 4, 57)) + +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>po : Symbol(po, Decl(awaitCallExpression4_es2017.ts, 5, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) +>fn : Symbol(fn, Decl(awaitCallExpression4_es2017.ts, 5, 25)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression4_es2017.ts, 5, 29)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression4_es2017.ts, 5, 43)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression4_es2017.ts, 5, 58)) + +declare function before(): void; +>before : Symbol(before, Decl(awaitCallExpression4_es2017.ts, 5, 84)) + +declare function after(): void; +>after : Symbol(after, Decl(awaitCallExpression4_es2017.ts, 6, 32)) + +async function func(): Promise { +>func : Symbol(func, Decl(awaitCallExpression4_es2017.ts, 7, 31)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + + before(); +>before : Symbol(before, Decl(awaitCallExpression4_es2017.ts, 5, 84)) + + var b = (await pfn)(a, a, a); +>b : Symbol(b, Decl(awaitCallExpression4_es2017.ts, 10, 7)) +>pfn : Symbol(pfn, Decl(awaitCallExpression4_es2017.ts, 4, 11)) +>a : Symbol(a, Decl(awaitCallExpression4_es2017.ts, 0, 11)) +>a : Symbol(a, Decl(awaitCallExpression4_es2017.ts, 0, 11)) +>a : Symbol(a, Decl(awaitCallExpression4_es2017.ts, 0, 11)) + + after(); +>after : Symbol(after, Decl(awaitCallExpression4_es2017.ts, 6, 32)) +} diff --git a/tests/baselines/reference/awaitCallExpression4_es2017.types b/tests/baselines/reference/awaitCallExpression4_es2017.types new file mode 100644 index 0000000000000..3f1b478e66925 --- /dev/null +++ b/tests/baselines/reference/awaitCallExpression4_es2017.types @@ -0,0 +1,64 @@ +=== tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression4_es2017.ts === +declare var a: boolean; +>a : boolean + +declare var p: Promise; +>p : Promise +>Promise : Promise + +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +>o : { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; } +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>pfn : Promise<(arg0: boolean, arg1: boolean, arg2: boolean) => void> +>Promise : Promise +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>po : Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }> +>Promise : Promise +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare function before(): void; +>before : () => void + +declare function after(): void; +>after : () => void + +async function func(): Promise { +>func : () => Promise +>Promise : Promise + + before(); +>before() : void +>before : () => void + + var b = (await pfn)(a, a, a); +>b : void +>(await pfn)(a, a, a) : void +>(await pfn) : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>await pfn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>pfn : Promise<(arg0: boolean, arg1: boolean, arg2: boolean) => void> +>a : boolean +>a : boolean +>a : boolean + + after(); +>after() : void +>after : () => void +} diff --git a/tests/baselines/reference/awaitCallExpression5_es2017.js b/tests/baselines/reference/awaitCallExpression5_es2017.js new file mode 100644 index 0000000000000..d08823ac5a354 --- /dev/null +++ b/tests/baselines/reference/awaitCallExpression5_es2017.js @@ -0,0 +1,21 @@ +//// [awaitCallExpression5_es2017.ts] +declare var a: boolean; +declare var p: Promise; +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare function before(): void; +declare function after(): void; +async function func(): Promise { + before(); + var b = o.fn(a, a, a); + after(); +} + +//// [awaitCallExpression5_es2017.js] +async function func() { + before(); + var b = o.fn(a, a, a); + after(); +} diff --git a/tests/baselines/reference/awaitCallExpression5_es2017.symbols b/tests/baselines/reference/awaitCallExpression5_es2017.symbols new file mode 100644 index 0000000000000..3854353314aef --- /dev/null +++ b/tests/baselines/reference/awaitCallExpression5_es2017.symbols @@ -0,0 +1,61 @@ +=== tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression5_es2017.ts === +declare var a: boolean; +>a : Symbol(a, Decl(awaitCallExpression5_es2017.ts, 0, 11)) + +declare var p: Promise; +>p : Symbol(p, Decl(awaitCallExpression5_es2017.ts, 1, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +>fn : Symbol(fn, Decl(awaitCallExpression5_es2017.ts, 1, 32)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression5_es2017.ts, 2, 20)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression5_es2017.ts, 2, 34)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression5_es2017.ts, 2, 49)) + +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +>o : Symbol(o, Decl(awaitCallExpression5_es2017.ts, 3, 11)) +>fn : Symbol(fn, Decl(awaitCallExpression5_es2017.ts, 3, 16)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression5_es2017.ts, 3, 20)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression5_es2017.ts, 3, 34)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression5_es2017.ts, 3, 49)) + +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>pfn : Symbol(pfn, Decl(awaitCallExpression5_es2017.ts, 4, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression5_es2017.ts, 4, 28)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression5_es2017.ts, 4, 42)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression5_es2017.ts, 4, 57)) + +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>po : Symbol(po, Decl(awaitCallExpression5_es2017.ts, 5, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) +>fn : Symbol(fn, Decl(awaitCallExpression5_es2017.ts, 5, 25)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression5_es2017.ts, 5, 29)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression5_es2017.ts, 5, 43)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression5_es2017.ts, 5, 58)) + +declare function before(): void; +>before : Symbol(before, Decl(awaitCallExpression5_es2017.ts, 5, 84)) + +declare function after(): void; +>after : Symbol(after, Decl(awaitCallExpression5_es2017.ts, 6, 32)) + +async function func(): Promise { +>func : Symbol(func, Decl(awaitCallExpression5_es2017.ts, 7, 31)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + + before(); +>before : Symbol(before, Decl(awaitCallExpression5_es2017.ts, 5, 84)) + + var b = o.fn(a, a, a); +>b : Symbol(b, Decl(awaitCallExpression5_es2017.ts, 10, 7)) +>o.fn : Symbol(fn, Decl(awaitCallExpression5_es2017.ts, 3, 16)) +>o : Symbol(o, Decl(awaitCallExpression5_es2017.ts, 3, 11)) +>fn : Symbol(fn, Decl(awaitCallExpression5_es2017.ts, 3, 16)) +>a : Symbol(a, Decl(awaitCallExpression5_es2017.ts, 0, 11)) +>a : Symbol(a, Decl(awaitCallExpression5_es2017.ts, 0, 11)) +>a : Symbol(a, Decl(awaitCallExpression5_es2017.ts, 0, 11)) + + after(); +>after : Symbol(after, Decl(awaitCallExpression5_es2017.ts, 6, 32)) +} diff --git a/tests/baselines/reference/awaitCallExpression5_es2017.types b/tests/baselines/reference/awaitCallExpression5_es2017.types new file mode 100644 index 0000000000000..f31320e44f7d4 --- /dev/null +++ b/tests/baselines/reference/awaitCallExpression5_es2017.types @@ -0,0 +1,64 @@ +=== tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression5_es2017.ts === +declare var a: boolean; +>a : boolean + +declare var p: Promise; +>p : Promise +>Promise : Promise + +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +>o : { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; } +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>pfn : Promise<(arg0: boolean, arg1: boolean, arg2: boolean) => void> +>Promise : Promise +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>po : Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }> +>Promise : Promise +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare function before(): void; +>before : () => void + +declare function after(): void; +>after : () => void + +async function func(): Promise { +>func : () => Promise +>Promise : Promise + + before(); +>before() : void +>before : () => void + + var b = o.fn(a, a, a); +>b : void +>o.fn(a, a, a) : void +>o.fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>o : { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; } +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>a : boolean +>a : boolean +>a : boolean + + after(); +>after() : void +>after : () => void +} diff --git a/tests/baselines/reference/awaitCallExpression6_es2017.js b/tests/baselines/reference/awaitCallExpression6_es2017.js new file mode 100644 index 0000000000000..77a47ba806589 --- /dev/null +++ b/tests/baselines/reference/awaitCallExpression6_es2017.js @@ -0,0 +1,21 @@ +//// [awaitCallExpression6_es2017.ts] +declare var a: boolean; +declare var p: Promise; +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare function before(): void; +declare function after(): void; +async function func(): Promise { + before(); + var b = o.fn(await p, a, a); + after(); +} + +//// [awaitCallExpression6_es2017.js] +async function func() { + before(); + var b = o.fn(await p, a, a); + after(); +} diff --git a/tests/baselines/reference/awaitCallExpression6_es2017.symbols b/tests/baselines/reference/awaitCallExpression6_es2017.symbols new file mode 100644 index 0000000000000..6a651510328a4 --- /dev/null +++ b/tests/baselines/reference/awaitCallExpression6_es2017.symbols @@ -0,0 +1,61 @@ +=== tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression6_es2017.ts === +declare var a: boolean; +>a : Symbol(a, Decl(awaitCallExpression6_es2017.ts, 0, 11)) + +declare var p: Promise; +>p : Symbol(p, Decl(awaitCallExpression6_es2017.ts, 1, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +>fn : Symbol(fn, Decl(awaitCallExpression6_es2017.ts, 1, 32)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression6_es2017.ts, 2, 20)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression6_es2017.ts, 2, 34)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression6_es2017.ts, 2, 49)) + +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +>o : Symbol(o, Decl(awaitCallExpression6_es2017.ts, 3, 11)) +>fn : Symbol(fn, Decl(awaitCallExpression6_es2017.ts, 3, 16)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression6_es2017.ts, 3, 20)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression6_es2017.ts, 3, 34)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression6_es2017.ts, 3, 49)) + +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>pfn : Symbol(pfn, Decl(awaitCallExpression6_es2017.ts, 4, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression6_es2017.ts, 4, 28)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression6_es2017.ts, 4, 42)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression6_es2017.ts, 4, 57)) + +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>po : Symbol(po, Decl(awaitCallExpression6_es2017.ts, 5, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) +>fn : Symbol(fn, Decl(awaitCallExpression6_es2017.ts, 5, 25)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression6_es2017.ts, 5, 29)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression6_es2017.ts, 5, 43)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression6_es2017.ts, 5, 58)) + +declare function before(): void; +>before : Symbol(before, Decl(awaitCallExpression6_es2017.ts, 5, 84)) + +declare function after(): void; +>after : Symbol(after, Decl(awaitCallExpression6_es2017.ts, 6, 32)) + +async function func(): Promise { +>func : Symbol(func, Decl(awaitCallExpression6_es2017.ts, 7, 31)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + + before(); +>before : Symbol(before, Decl(awaitCallExpression6_es2017.ts, 5, 84)) + + var b = o.fn(await p, a, a); +>b : Symbol(b, Decl(awaitCallExpression6_es2017.ts, 10, 7)) +>o.fn : Symbol(fn, Decl(awaitCallExpression6_es2017.ts, 3, 16)) +>o : Symbol(o, Decl(awaitCallExpression6_es2017.ts, 3, 11)) +>fn : Symbol(fn, Decl(awaitCallExpression6_es2017.ts, 3, 16)) +>p : Symbol(p, Decl(awaitCallExpression6_es2017.ts, 1, 11)) +>a : Symbol(a, Decl(awaitCallExpression6_es2017.ts, 0, 11)) +>a : Symbol(a, Decl(awaitCallExpression6_es2017.ts, 0, 11)) + + after(); +>after : Symbol(after, Decl(awaitCallExpression6_es2017.ts, 6, 32)) +} diff --git a/tests/baselines/reference/awaitCallExpression6_es2017.types b/tests/baselines/reference/awaitCallExpression6_es2017.types new file mode 100644 index 0000000000000..ccce4cad366eb --- /dev/null +++ b/tests/baselines/reference/awaitCallExpression6_es2017.types @@ -0,0 +1,65 @@ +=== tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression6_es2017.ts === +declare var a: boolean; +>a : boolean + +declare var p: Promise; +>p : Promise +>Promise : Promise + +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +>o : { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; } +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>pfn : Promise<(arg0: boolean, arg1: boolean, arg2: boolean) => void> +>Promise : Promise +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>po : Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }> +>Promise : Promise +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare function before(): void; +>before : () => void + +declare function after(): void; +>after : () => void + +async function func(): Promise { +>func : () => Promise +>Promise : Promise + + before(); +>before() : void +>before : () => void + + var b = o.fn(await p, a, a); +>b : void +>o.fn(await p, a, a) : void +>o.fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>o : { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; } +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>await p : boolean +>p : Promise +>a : boolean +>a : boolean + + after(); +>after() : void +>after : () => void +} diff --git a/tests/baselines/reference/awaitCallExpression7_es2017.js b/tests/baselines/reference/awaitCallExpression7_es2017.js new file mode 100644 index 0000000000000..b3391ced6b605 --- /dev/null +++ b/tests/baselines/reference/awaitCallExpression7_es2017.js @@ -0,0 +1,21 @@ +//// [awaitCallExpression7_es2017.ts] +declare var a: boolean; +declare var p: Promise; +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare function before(): void; +declare function after(): void; +async function func(): Promise { + before(); + var b = o.fn(a, await p, a); + after(); +} + +//// [awaitCallExpression7_es2017.js] +async function func() { + before(); + var b = o.fn(a, await p, a); + after(); +} diff --git a/tests/baselines/reference/awaitCallExpression7_es2017.symbols b/tests/baselines/reference/awaitCallExpression7_es2017.symbols new file mode 100644 index 0000000000000..e95078c68500a --- /dev/null +++ b/tests/baselines/reference/awaitCallExpression7_es2017.symbols @@ -0,0 +1,61 @@ +=== tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression7_es2017.ts === +declare var a: boolean; +>a : Symbol(a, Decl(awaitCallExpression7_es2017.ts, 0, 11)) + +declare var p: Promise; +>p : Symbol(p, Decl(awaitCallExpression7_es2017.ts, 1, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +>fn : Symbol(fn, Decl(awaitCallExpression7_es2017.ts, 1, 32)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression7_es2017.ts, 2, 20)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression7_es2017.ts, 2, 34)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression7_es2017.ts, 2, 49)) + +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +>o : Symbol(o, Decl(awaitCallExpression7_es2017.ts, 3, 11)) +>fn : Symbol(fn, Decl(awaitCallExpression7_es2017.ts, 3, 16)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression7_es2017.ts, 3, 20)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression7_es2017.ts, 3, 34)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression7_es2017.ts, 3, 49)) + +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>pfn : Symbol(pfn, Decl(awaitCallExpression7_es2017.ts, 4, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression7_es2017.ts, 4, 28)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression7_es2017.ts, 4, 42)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression7_es2017.ts, 4, 57)) + +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>po : Symbol(po, Decl(awaitCallExpression7_es2017.ts, 5, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) +>fn : Symbol(fn, Decl(awaitCallExpression7_es2017.ts, 5, 25)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression7_es2017.ts, 5, 29)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression7_es2017.ts, 5, 43)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression7_es2017.ts, 5, 58)) + +declare function before(): void; +>before : Symbol(before, Decl(awaitCallExpression7_es2017.ts, 5, 84)) + +declare function after(): void; +>after : Symbol(after, Decl(awaitCallExpression7_es2017.ts, 6, 32)) + +async function func(): Promise { +>func : Symbol(func, Decl(awaitCallExpression7_es2017.ts, 7, 31)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + + before(); +>before : Symbol(before, Decl(awaitCallExpression7_es2017.ts, 5, 84)) + + var b = o.fn(a, await p, a); +>b : Symbol(b, Decl(awaitCallExpression7_es2017.ts, 10, 7)) +>o.fn : Symbol(fn, Decl(awaitCallExpression7_es2017.ts, 3, 16)) +>o : Symbol(o, Decl(awaitCallExpression7_es2017.ts, 3, 11)) +>fn : Symbol(fn, Decl(awaitCallExpression7_es2017.ts, 3, 16)) +>a : Symbol(a, Decl(awaitCallExpression7_es2017.ts, 0, 11)) +>p : Symbol(p, Decl(awaitCallExpression7_es2017.ts, 1, 11)) +>a : Symbol(a, Decl(awaitCallExpression7_es2017.ts, 0, 11)) + + after(); +>after : Symbol(after, Decl(awaitCallExpression7_es2017.ts, 6, 32)) +} diff --git a/tests/baselines/reference/awaitCallExpression7_es2017.types b/tests/baselines/reference/awaitCallExpression7_es2017.types new file mode 100644 index 0000000000000..aefe791685235 --- /dev/null +++ b/tests/baselines/reference/awaitCallExpression7_es2017.types @@ -0,0 +1,65 @@ +=== tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression7_es2017.ts === +declare var a: boolean; +>a : boolean + +declare var p: Promise; +>p : Promise +>Promise : Promise + +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +>o : { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; } +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>pfn : Promise<(arg0: boolean, arg1: boolean, arg2: boolean) => void> +>Promise : Promise +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>po : Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }> +>Promise : Promise +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare function before(): void; +>before : () => void + +declare function after(): void; +>after : () => void + +async function func(): Promise { +>func : () => Promise +>Promise : Promise + + before(); +>before() : void +>before : () => void + + var b = o.fn(a, await p, a); +>b : void +>o.fn(a, await p, a) : void +>o.fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>o : { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; } +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>a : boolean +>await p : boolean +>p : Promise +>a : boolean + + after(); +>after() : void +>after : () => void +} diff --git a/tests/baselines/reference/awaitCallExpression8_es2017.js b/tests/baselines/reference/awaitCallExpression8_es2017.js new file mode 100644 index 0000000000000..7e04402e90aef --- /dev/null +++ b/tests/baselines/reference/awaitCallExpression8_es2017.js @@ -0,0 +1,21 @@ +//// [awaitCallExpression8_es2017.ts] +declare var a: boolean; +declare var p: Promise; +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare function before(): void; +declare function after(): void; +async function func(): Promise { + before(); + var b = (await po).fn(a, a, a); + after(); +} + +//// [awaitCallExpression8_es2017.js] +async function func() { + before(); + var b = (await po).fn(a, a, a); + after(); +} diff --git a/tests/baselines/reference/awaitCallExpression8_es2017.symbols b/tests/baselines/reference/awaitCallExpression8_es2017.symbols new file mode 100644 index 0000000000000..e3fd465c120f0 --- /dev/null +++ b/tests/baselines/reference/awaitCallExpression8_es2017.symbols @@ -0,0 +1,61 @@ +=== tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression8_es2017.ts === +declare var a: boolean; +>a : Symbol(a, Decl(awaitCallExpression8_es2017.ts, 0, 11)) + +declare var p: Promise; +>p : Symbol(p, Decl(awaitCallExpression8_es2017.ts, 1, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +>fn : Symbol(fn, Decl(awaitCallExpression8_es2017.ts, 1, 32)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression8_es2017.ts, 2, 20)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression8_es2017.ts, 2, 34)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression8_es2017.ts, 2, 49)) + +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +>o : Symbol(o, Decl(awaitCallExpression8_es2017.ts, 3, 11)) +>fn : Symbol(fn, Decl(awaitCallExpression8_es2017.ts, 3, 16)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression8_es2017.ts, 3, 20)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression8_es2017.ts, 3, 34)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression8_es2017.ts, 3, 49)) + +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>pfn : Symbol(pfn, Decl(awaitCallExpression8_es2017.ts, 4, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression8_es2017.ts, 4, 28)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression8_es2017.ts, 4, 42)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression8_es2017.ts, 4, 57)) + +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>po : Symbol(po, Decl(awaitCallExpression8_es2017.ts, 5, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) +>fn : Symbol(fn, Decl(awaitCallExpression8_es2017.ts, 5, 25)) +>arg0 : Symbol(arg0, Decl(awaitCallExpression8_es2017.ts, 5, 29)) +>arg1 : Symbol(arg1, Decl(awaitCallExpression8_es2017.ts, 5, 43)) +>arg2 : Symbol(arg2, Decl(awaitCallExpression8_es2017.ts, 5, 58)) + +declare function before(): void; +>before : Symbol(before, Decl(awaitCallExpression8_es2017.ts, 5, 84)) + +declare function after(): void; +>after : Symbol(after, Decl(awaitCallExpression8_es2017.ts, 6, 32)) + +async function func(): Promise { +>func : Symbol(func, Decl(awaitCallExpression8_es2017.ts, 7, 31)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + + before(); +>before : Symbol(before, Decl(awaitCallExpression8_es2017.ts, 5, 84)) + + var b = (await po).fn(a, a, a); +>b : Symbol(b, Decl(awaitCallExpression8_es2017.ts, 10, 7)) +>(await po).fn : Symbol(fn, Decl(awaitCallExpression8_es2017.ts, 5, 25)) +>po : Symbol(po, Decl(awaitCallExpression8_es2017.ts, 5, 11)) +>fn : Symbol(fn, Decl(awaitCallExpression8_es2017.ts, 5, 25)) +>a : Symbol(a, Decl(awaitCallExpression8_es2017.ts, 0, 11)) +>a : Symbol(a, Decl(awaitCallExpression8_es2017.ts, 0, 11)) +>a : Symbol(a, Decl(awaitCallExpression8_es2017.ts, 0, 11)) + + after(); +>after : Symbol(after, Decl(awaitCallExpression8_es2017.ts, 6, 32)) +} diff --git a/tests/baselines/reference/awaitCallExpression8_es2017.types b/tests/baselines/reference/awaitCallExpression8_es2017.types new file mode 100644 index 0000000000000..a9a115d39f28b --- /dev/null +++ b/tests/baselines/reference/awaitCallExpression8_es2017.types @@ -0,0 +1,66 @@ +=== tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression8_es2017.ts === +declare var a: boolean; +>a : boolean + +declare var p: Promise; +>p : Promise +>Promise : Promise + +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +>o : { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; } +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>pfn : Promise<(arg0: boolean, arg1: boolean, arg2: boolean) => void> +>Promise : Promise +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +>po : Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }> +>Promise : Promise +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>arg0 : boolean +>arg1 : boolean +>arg2 : boolean + +declare function before(): void; +>before : () => void + +declare function after(): void; +>after : () => void + +async function func(): Promise { +>func : () => Promise +>Promise : Promise + + before(); +>before() : void +>before : () => void + + var b = (await po).fn(a, a, a); +>b : void +>(await po).fn(a, a, a) : void +>(await po).fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>(await po) : { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; } +>await po : { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; } +>po : Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }> +>fn : (arg0: boolean, arg1: boolean, arg2: boolean) => void +>a : boolean +>a : boolean +>a : boolean + + after(); +>after() : void +>after : () => void +} diff --git a/tests/baselines/reference/awaitClassExpression_es2017.js b/tests/baselines/reference/awaitClassExpression_es2017.js new file mode 100644 index 0000000000000..d84f01f61cfa4 --- /dev/null +++ b/tests/baselines/reference/awaitClassExpression_es2017.js @@ -0,0 +1,14 @@ +//// [awaitClassExpression_es2017.ts] +declare class C { } +declare var p: Promise; + +async function func(): Promise { + class D extends (await p) { + } +} + +//// [awaitClassExpression_es2017.js] +async function func() { + class D extends (await p) { + } +} diff --git a/tests/baselines/reference/awaitClassExpression_es2017.symbols b/tests/baselines/reference/awaitClassExpression_es2017.symbols new file mode 100644 index 0000000000000..bebffa2bbada4 --- /dev/null +++ b/tests/baselines/reference/awaitClassExpression_es2017.symbols @@ -0,0 +1,18 @@ +=== tests/cases/conformance/async/es2017/awaitClassExpression_es2017.ts === +declare class C { } +>C : Symbol(C, Decl(awaitClassExpression_es2017.ts, 0, 0)) + +declare var p: Promise; +>p : Symbol(p, Decl(awaitClassExpression_es2017.ts, 1, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) +>C : Symbol(C, Decl(awaitClassExpression_es2017.ts, 0, 0)) + +async function func(): Promise { +>func : Symbol(func, Decl(awaitClassExpression_es2017.ts, 1, 33)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + + class D extends (await p) { +>D : Symbol(D, Decl(awaitClassExpression_es2017.ts, 3, 38)) +>p : Symbol(p, Decl(awaitClassExpression_es2017.ts, 1, 11)) + } +} diff --git a/tests/baselines/reference/awaitClassExpression_es2017.types b/tests/baselines/reference/awaitClassExpression_es2017.types new file mode 100644 index 0000000000000..39d17b1cc503c --- /dev/null +++ b/tests/baselines/reference/awaitClassExpression_es2017.types @@ -0,0 +1,20 @@ +=== tests/cases/conformance/async/es2017/awaitClassExpression_es2017.ts === +declare class C { } +>C : C + +declare var p: Promise; +>p : Promise +>Promise : Promise +>C : typeof C + +async function func(): Promise { +>func : () => Promise +>Promise : Promise + + class D extends (await p) { +>D : D +>(await p) : C +>await p : typeof C +>p : Promise + } +} diff --git a/tests/baselines/reference/await_unaryExpression_es2017.js b/tests/baselines/reference/await_unaryExpression_es2017.js new file mode 100644 index 0000000000000..83fce6588e646 --- /dev/null +++ b/tests/baselines/reference/await_unaryExpression_es2017.js @@ -0,0 +1,31 @@ +//// [await_unaryExpression_es2017.ts] + +async function bar() { + !await 42; // OK +} + +async function bar1() { + +await 42; // OK +} + +async function bar3() { + -await 42; // OK +} + +async function bar4() { + ~await 42; // OK +} + +//// [await_unaryExpression_es2017.js] +async function bar() { + !await 42; // OK +} +async function bar1() { + +await 42; // OK +} +async function bar3() { + -await 42; // OK +} +async function bar4() { + ~await 42; // OK +} diff --git a/tests/baselines/reference/await_unaryExpression_es2017.symbols b/tests/baselines/reference/await_unaryExpression_es2017.symbols new file mode 100644 index 0000000000000..1aa31031177c0 --- /dev/null +++ b/tests/baselines/reference/await_unaryExpression_es2017.symbols @@ -0,0 +1,25 @@ +=== tests/cases/conformance/async/es2017/await_unaryExpression_es2017.ts === + +async function bar() { +>bar : Symbol(bar, Decl(await_unaryExpression_es2017.ts, 0, 0)) + + !await 42; // OK +} + +async function bar1() { +>bar1 : Symbol(bar1, Decl(await_unaryExpression_es2017.ts, 3, 1)) + + +await 42; // OK +} + +async function bar3() { +>bar3 : Symbol(bar3, Decl(await_unaryExpression_es2017.ts, 7, 1)) + + -await 42; // OK +} + +async function bar4() { +>bar4 : Symbol(bar4, Decl(await_unaryExpression_es2017.ts, 11, 1)) + + ~await 42; // OK +} diff --git a/tests/baselines/reference/await_unaryExpression_es2017.types b/tests/baselines/reference/await_unaryExpression_es2017.types new file mode 100644 index 0000000000000..4f4254df318a9 --- /dev/null +++ b/tests/baselines/reference/await_unaryExpression_es2017.types @@ -0,0 +1,37 @@ +=== tests/cases/conformance/async/es2017/await_unaryExpression_es2017.ts === + +async function bar() { +>bar : () => Promise + + !await 42; // OK +>!await 42 : boolean +>await 42 : 42 +>42 : 42 +} + +async function bar1() { +>bar1 : () => Promise + + +await 42; // OK +>+await 42 : number +>await 42 : 42 +>42 : 42 +} + +async function bar3() { +>bar3 : () => Promise + + -await 42; // OK +>-await 42 : number +>await 42 : 42 +>42 : 42 +} + +async function bar4() { +>bar4 : () => Promise + + ~await 42; // OK +>~await 42 : number +>await 42 : 42 +>42 : 42 +} diff --git a/tests/baselines/reference/await_unaryExpression_es2017_1.js b/tests/baselines/reference/await_unaryExpression_es2017_1.js new file mode 100644 index 0000000000000..f863fb19a7e1f --- /dev/null +++ b/tests/baselines/reference/await_unaryExpression_es2017_1.js @@ -0,0 +1,38 @@ +//// [await_unaryExpression_es2017_1.ts] + +async function bar() { + !await 42; // OK +} + +async function bar1() { + delete await 42; // OK +} + +async function bar2() { + delete await 42; // OK +} + +async function bar3() { + void await 42; +} + +async function bar4() { + +await 42; +} + +//// [await_unaryExpression_es2017_1.js] +async function bar() { + !await 42; // OK +} +async function bar1() { + delete await 42; // OK +} +async function bar2() { + delete await 42; // OK +} +async function bar3() { + void await 42; +} +async function bar4() { + +await 42; +} diff --git a/tests/baselines/reference/await_unaryExpression_es2017_1.symbols b/tests/baselines/reference/await_unaryExpression_es2017_1.symbols new file mode 100644 index 0000000000000..81bb31a7efd57 --- /dev/null +++ b/tests/baselines/reference/await_unaryExpression_es2017_1.symbols @@ -0,0 +1,31 @@ +=== tests/cases/conformance/async/es2017/await_unaryExpression_es2017_1.ts === + +async function bar() { +>bar : Symbol(bar, Decl(await_unaryExpression_es2017_1.ts, 0, 0)) + + !await 42; // OK +} + +async function bar1() { +>bar1 : Symbol(bar1, Decl(await_unaryExpression_es2017_1.ts, 3, 1)) + + delete await 42; // OK +} + +async function bar2() { +>bar2 : Symbol(bar2, Decl(await_unaryExpression_es2017_1.ts, 7, 1)) + + delete await 42; // OK +} + +async function bar3() { +>bar3 : Symbol(bar3, Decl(await_unaryExpression_es2017_1.ts, 11, 1)) + + void await 42; +} + +async function bar4() { +>bar4 : Symbol(bar4, Decl(await_unaryExpression_es2017_1.ts, 15, 1)) + + +await 42; +} diff --git a/tests/baselines/reference/await_unaryExpression_es2017_1.types b/tests/baselines/reference/await_unaryExpression_es2017_1.types new file mode 100644 index 0000000000000..7afa4fe900190 --- /dev/null +++ b/tests/baselines/reference/await_unaryExpression_es2017_1.types @@ -0,0 +1,46 @@ +=== tests/cases/conformance/async/es2017/await_unaryExpression_es2017_1.ts === + +async function bar() { +>bar : () => Promise + + !await 42; // OK +>!await 42 : boolean +>await 42 : 42 +>42 : 42 +} + +async function bar1() { +>bar1 : () => Promise + + delete await 42; // OK +>delete await 42 : boolean +>await 42 : 42 +>42 : 42 +} + +async function bar2() { +>bar2 : () => Promise + + delete await 42; // OK +>delete await 42 : boolean +>await 42 : 42 +>42 : 42 +} + +async function bar3() { +>bar3 : () => Promise + + void await 42; +>void await 42 : undefined +>await 42 : 42 +>42 : 42 +} + +async function bar4() { +>bar4 : () => Promise + + +await 42; +>+await 42 : number +>await 42 : 42 +>42 : 42 +} diff --git a/tests/baselines/reference/await_unaryExpression_es2017_2.js b/tests/baselines/reference/await_unaryExpression_es2017_2.js new file mode 100644 index 0000000000000..b982b20245c0e --- /dev/null +++ b/tests/baselines/reference/await_unaryExpression_es2017_2.js @@ -0,0 +1,24 @@ +//// [await_unaryExpression_es2017_2.ts] + +async function bar1() { + delete await 42; +} + +async function bar2() { + delete await 42; +} + +async function bar3() { + void await 42; +} + +//// [await_unaryExpression_es2017_2.js] +async function bar1() { + delete await 42; +} +async function bar2() { + delete await 42; +} +async function bar3() { + void await 42; +} diff --git a/tests/baselines/reference/await_unaryExpression_es2017_2.symbols b/tests/baselines/reference/await_unaryExpression_es2017_2.symbols new file mode 100644 index 0000000000000..d4b8a7493d9c1 --- /dev/null +++ b/tests/baselines/reference/await_unaryExpression_es2017_2.symbols @@ -0,0 +1,19 @@ +=== tests/cases/conformance/async/es2017/await_unaryExpression_es2017_2.ts === + +async function bar1() { +>bar1 : Symbol(bar1, Decl(await_unaryExpression_es2017_2.ts, 0, 0)) + + delete await 42; +} + +async function bar2() { +>bar2 : Symbol(bar2, Decl(await_unaryExpression_es2017_2.ts, 3, 1)) + + delete await 42; +} + +async function bar3() { +>bar3 : Symbol(bar3, Decl(await_unaryExpression_es2017_2.ts, 7, 1)) + + void await 42; +} diff --git a/tests/baselines/reference/await_unaryExpression_es2017_2.types b/tests/baselines/reference/await_unaryExpression_es2017_2.types new file mode 100644 index 0000000000000..acaa76d2d6795 --- /dev/null +++ b/tests/baselines/reference/await_unaryExpression_es2017_2.types @@ -0,0 +1,28 @@ +=== tests/cases/conformance/async/es2017/await_unaryExpression_es2017_2.ts === + +async function bar1() { +>bar1 : () => Promise + + delete await 42; +>delete await 42 : boolean +>await 42 : 42 +>42 : 42 +} + +async function bar2() { +>bar2 : () => Promise + + delete await 42; +>delete await 42 : boolean +>await 42 : 42 +>42 : 42 +} + +async function bar3() { +>bar3 : () => Promise + + void await 42; +>void await 42 : undefined +>await 42 : 42 +>42 : 42 +} diff --git a/tests/baselines/reference/await_unaryExpression_es2017_3.errors.txt b/tests/baselines/reference/await_unaryExpression_es2017_3.errors.txt new file mode 100644 index 0000000000000..5d11c4bbb20eb --- /dev/null +++ b/tests/baselines/reference/await_unaryExpression_es2017_3.errors.txt @@ -0,0 +1,27 @@ +tests/cases/conformance/async/es2017/await_unaryExpression_es2017_3.ts(3,7): error TS1109: Expression expected. +tests/cases/conformance/async/es2017/await_unaryExpression_es2017_3.ts(7,7): error TS1109: Expression expected. + + +==== tests/cases/conformance/async/es2017/await_unaryExpression_es2017_3.ts (2 errors) ==== + + async function bar1() { + ++await 42; // Error + ~~~~~ +!!! error TS1109: Expression expected. + } + + async function bar2() { + --await 42; // Error + ~~~~~ +!!! error TS1109: Expression expected. + } + + async function bar3() { + var x = 42; + await x++; // OK but shouldn't need parenthesis + } + + async function bar4() { + var x = 42; + await x--; // OK but shouldn't need parenthesis + } \ No newline at end of file diff --git a/tests/baselines/reference/await_unaryExpression_es2017_3.js b/tests/baselines/reference/await_unaryExpression_es2017_3.js new file mode 100644 index 0000000000000..5e3c3c3a4bf44 --- /dev/null +++ b/tests/baselines/reference/await_unaryExpression_es2017_3.js @@ -0,0 +1,37 @@ +//// [await_unaryExpression_es2017_3.ts] + +async function bar1() { + ++await 42; // Error +} + +async function bar2() { + --await 42; // Error +} + +async function bar3() { + var x = 42; + await x++; // OK but shouldn't need parenthesis +} + +async function bar4() { + var x = 42; + await x--; // OK but shouldn't need parenthesis +} + +//// [await_unaryExpression_es2017_3.js] +async function bar1() { + ++; + await 42; // Error +} +async function bar2() { + --; + await 42; // Error +} +async function bar3() { + var x = 42; + await x++; // OK but shouldn't need parenthesis +} +async function bar4() { + var x = 42; + await x--; // OK but shouldn't need parenthesis +} diff --git a/tests/baselines/reference/constructorArgsErrors1.js b/tests/baselines/reference/constructorArgsErrors1.js index 15c5e64952bc2..bb0725ab8b9b3 100644 --- a/tests/baselines/reference/constructorArgsErrors1.js +++ b/tests/baselines/reference/constructorArgsErrors1.js @@ -6,7 +6,7 @@ class foo { //// [constructorArgsErrors1.js] var foo = (function () { - function foo(static a) { + function foo(a) { } return foo; }()); diff --git a/tests/baselines/reference/constructorArgsErrors5.js b/tests/baselines/reference/constructorArgsErrors5.js index 6ba68cb88b4ed..c481d6f323c6c 100644 --- a/tests/baselines/reference/constructorArgsErrors5.js +++ b/tests/baselines/reference/constructorArgsErrors5.js @@ -7,7 +7,7 @@ class foo { //// [constructorArgsErrors5.js] var foo = (function () { - function foo(export a) { + function foo(a) { } return foo; }()); diff --git a/tests/baselines/reference/controlFlowArrayErrors.errors.txt b/tests/baselines/reference/controlFlowArrayErrors.errors.txt new file mode 100644 index 0000000000000..2ef009dc0e115 --- /dev/null +++ b/tests/baselines/reference/controlFlowArrayErrors.errors.txt @@ -0,0 +1,105 @@ +tests/cases/compiler/controlFlowArrayErrors.ts(5,9): error TS7034: Variable 'x' implicitly has type 'any[]' in some locations where its type cannot be determined. +tests/cases/compiler/controlFlowArrayErrors.ts(6,13): error TS7005: Variable 'x' implicitly has an 'any[]' type. +tests/cases/compiler/controlFlowArrayErrors.ts(12,9): error TS7034: Variable 'x' implicitly has type 'any[]' in some locations where its type cannot be determined. +tests/cases/compiler/controlFlowArrayErrors.ts(14,13): error TS7005: Variable 'x' implicitly has an 'any[]' type. +tests/cases/compiler/controlFlowArrayErrors.ts(20,9): error TS7034: Variable 'x' implicitly has type 'any[]' in some locations where its type cannot be determined. +tests/cases/compiler/controlFlowArrayErrors.ts(23,9): error TS7005: Variable 'x' implicitly has an 'any[]' type. +tests/cases/compiler/controlFlowArrayErrors.ts(30,12): error TS2345: Argument of type 'true' is not assignable to parameter of type 'string | number'. +tests/cases/compiler/controlFlowArrayErrors.ts(35,12): error TS2345: Argument of type 'true' is not assignable to parameter of type 'string | number'. +tests/cases/compiler/controlFlowArrayErrors.ts(49,5): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '((...items: (string | number)[]) => number) | ((...items: boolean[]) => number)' has no compatible call signatures. +tests/cases/compiler/controlFlowArrayErrors.ts(57,12): error TS2345: Argument of type '"hello"' is not assignable to parameter of type 'number'. +tests/cases/compiler/controlFlowArrayErrors.ts(61,11): error TS7034: Variable 'x' implicitly has type 'any[]' in some locations where its type cannot be determined. +tests/cases/compiler/controlFlowArrayErrors.ts(64,9): error TS7005: Variable 'x' implicitly has an 'any[]' type. + + +==== tests/cases/compiler/controlFlowArrayErrors.ts (12 errors) ==== + + declare function cond(): boolean; + + function f1() { + let x = []; // Implicit any[] error in some locations + ~ +!!! error TS7034: Variable 'x' implicitly has type 'any[]' in some locations where its type cannot be determined. + let y = x; // Implicit any[] error + ~ +!!! error TS7005: Variable 'x' implicitly has an 'any[]' type. + x.push(5); + let z = x; + } + + function f2() { + let x; // Implicit any[] error in some locations + ~ +!!! error TS7034: Variable 'x' implicitly has type 'any[]' in some locations where its type cannot be determined. + x = []; + let y = x; // Implicit any[] error + ~ +!!! error TS7005: Variable 'x' implicitly has an 'any[]' type. + x.push(5); + let z = x; + } + + function f3() { + let x = []; // Implicit any[] error in some locations + ~ +!!! error TS7034: Variable 'x' implicitly has type 'any[]' in some locations where its type cannot be determined. + x.push(5); + function g() { + x; // Implicit any[] error + ~ +!!! error TS7005: Variable 'x' implicitly has an 'any[]' type. + } + } + + function f4() { + let x; + x = [5, "hello"]; // Non-evolving array + x.push(true); // Error + ~~~~ +!!! error TS2345: Argument of type 'true' is not assignable to parameter of type 'string | number'. + } + + function f5() { + let x = [5, "hello"]; // Non-evolving array + x.push(true); // Error + ~~~~ +!!! error TS2345: Argument of type 'true' is not assignable to parameter of type 'string | number'. + } + + function f6() { + let x; + if (cond()) { + x = []; + x.push(5); + x.push("hello"); + } + else { + x = [true]; // Non-evolving array + } + x; // boolean[] | (string | number)[] + x.push(99); // Error + ~~~~~~~~~~ +!!! error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '((...items: (string | number)[]) => number) | ((...items: boolean[]) => number)' has no compatible call signatures. + } + + function f7() { + let x = []; // x has evolving array value + x.push(5); + let y = x; // y has non-evolving array value + x.push("hello"); // Ok + y.push("hello"); // Error + ~~~~~~~ +!!! error TS2345: Argument of type '"hello"' is not assignable to parameter of type 'number'. + } + + function f8() { + const x = []; // Implicit any[] error in some locations + ~ +!!! error TS7034: Variable 'x' implicitly has type 'any[]' in some locations where its type cannot be determined. + x.push(5); + function g() { + x; // Implicit any[] error + ~ +!!! error TS7005: Variable 'x' implicitly has an 'any[]' type. + } + } \ No newline at end of file diff --git a/tests/baselines/reference/controlFlowArrayErrors.js b/tests/baselines/reference/controlFlowArrayErrors.js new file mode 100644 index 0000000000000..59995eb309475 --- /dev/null +++ b/tests/baselines/reference/controlFlowArrayErrors.js @@ -0,0 +1,125 @@ +//// [controlFlowArrayErrors.ts] + +declare function cond(): boolean; + +function f1() { + let x = []; // Implicit any[] error in some locations + let y = x; // Implicit any[] error + x.push(5); + let z = x; +} + +function f2() { + let x; // Implicit any[] error in some locations + x = []; + let y = x; // Implicit any[] error + x.push(5); + let z = x; +} + +function f3() { + let x = []; // Implicit any[] error in some locations + x.push(5); + function g() { + x; // Implicit any[] error + } +} + +function f4() { + let x; + x = [5, "hello"]; // Non-evolving array + x.push(true); // Error +} + +function f5() { + let x = [5, "hello"]; // Non-evolving array + x.push(true); // Error +} + +function f6() { + let x; + if (cond()) { + x = []; + x.push(5); + x.push("hello"); + } + else { + x = [true]; // Non-evolving array + } + x; // boolean[] | (string | number)[] + x.push(99); // Error +} + +function f7() { + let x = []; // x has evolving array value + x.push(5); + let y = x; // y has non-evolving array value + x.push("hello"); // Ok + y.push("hello"); // Error +} + +function f8() { + const x = []; // Implicit any[] error in some locations + x.push(5); + function g() { + x; // Implicit any[] error + } +} + +//// [controlFlowArrayErrors.js] +function f1() { + var x = []; // Implicit any[] error in some locations + var y = x; // Implicit any[] error + x.push(5); + var z = x; +} +function f2() { + var x; // Implicit any[] error in some locations + x = []; + var y = x; // Implicit any[] error + x.push(5); + var z = x; +} +function f3() { + var x = []; // Implicit any[] error in some locations + x.push(5); + function g() { + x; // Implicit any[] error + } +} +function f4() { + var x; + x = [5, "hello"]; // Non-evolving array + x.push(true); // Error +} +function f5() { + var x = [5, "hello"]; // Non-evolving array + x.push(true); // Error +} +function f6() { + var x; + if (cond()) { + x = []; + x.push(5); + x.push("hello"); + } + else { + x = [true]; // Non-evolving array + } + x; // boolean[] | (string | number)[] + x.push(99); // Error +} +function f7() { + var x = []; // x has evolving array value + x.push(5); + var y = x; // y has non-evolving array value + x.push("hello"); // Ok + y.push("hello"); // Error +} +function f8() { + var x = []; // Implicit any[] error in some locations + x.push(5); + function g() { + x; // Implicit any[] error + } +} diff --git a/tests/baselines/reference/controlFlowArrays.js b/tests/baselines/reference/controlFlowArrays.js new file mode 100644 index 0000000000000..3f1195994952b --- /dev/null +++ b/tests/baselines/reference/controlFlowArrays.js @@ -0,0 +1,343 @@ +//// [controlFlowArrays.ts] + +declare function cond(): boolean; + +function f1() { + let x = []; + x[0] = 5; + x[1] = "hello"; + x[2] = true; + return x; // (string | number | boolean)[] +} + +function f2() { + let x = []; + x.push(5); + x.push("hello"); + x.push(true); + return x; // (string | number | boolean)[] +} + +function f3() { + let x; + x = []; + x.push(5, "hello"); + return x; // (string | number)[] +} + +function f4() { + let x = []; + if (cond()) { + x.push(5); + } + else { + x.push("hello"); + } + return x; // (string | number)[] +} + +function f5() { + let x; + if (cond()) { + x = []; + x.push(5); + } + else { + x = []; + x.push("hello"); + } + return x; // (string | number)[] +} + +function f6() { + let x; + if (cond()) { + x = 5; + } + else { + x = []; + x.push("hello"); + } + return x; // number | string[] +} + +function f7() { + let x = null; + if (cond()) { + x = []; + while (cond()) { + x.push("hello"); + } + } + return x; // string[] | null +} + +function f8() { + let x = []; + x.push(5); + if (cond()) return x; // number[] + x.push("hello"); + if (cond()) return x; // (string | number)[] + x.push(true); + return x; // (string | number | boolean)[] +} + +function f9() { + let x = []; + if (cond()) { + x.push(5); + return x; // number[] + } + else { + x.push("hello"); + return x; // string[] + } +} + +function f10() { + let x = []; + if (cond()) { + x.push(true); + x; // boolean[] + } + else { + x.push(5); + x; // number[] + while (cond()) { + x.push("hello"); + } + x; // (string | number)[] + } + x.push(99); + return x; // (string | number | boolean)[] +} + +function f11() { + let x = []; + if (x.length === 0) { // x.length ok on implicit any[] + x.push("hello"); + } + return x; +} + +function f12() { + let x; + x = []; + if (x.length === 0) { // x.length ok on implicit any[] + x.push("hello"); + } + return x; +} + +function f13() { + var x = []; + x.push(5); + x.push("hello"); + x.push(true); + return x; // (string | number | boolean)[] +} + +function f14() { + const x = []; + x.push(5); + x.push("hello"); + x.push(true); + return x; // (string | number | boolean)[] +} + +function f15() { + let x = []; + while (cond()) { + while (cond()) {} + x.push("hello"); + } + return x; // string[] +} + +function f16() { + let x; + let y; + (x = [], x).push(5); + (x.push("hello"), x).push(true); + ((x))[3] = { a: 1 }; + return x; // (string | number | boolean | { a: number })[] +} + +function f17() { + let x = []; + x.unshift(5); + x.unshift("hello"); + x.unshift(true); + return x; // (string | number | boolean)[] +} + +function f18() { + let x = []; + x.push(5); + x.unshift("hello"); + x[2] = true; + return x; // (string | number | boolean)[] +} + +//// [controlFlowArrays.js] +function f1() { + var x = []; + x[0] = 5; + x[1] = "hello"; + x[2] = true; + return x; // (string | number | boolean)[] +} +function f2() { + var x = []; + x.push(5); + x.push("hello"); + x.push(true); + return x; // (string | number | boolean)[] +} +function f3() { + var x; + x = []; + x.push(5, "hello"); + return x; // (string | number)[] +} +function f4() { + var x = []; + if (cond()) { + x.push(5); + } + else { + x.push("hello"); + } + return x; // (string | number)[] +} +function f5() { + var x; + if (cond()) { + x = []; + x.push(5); + } + else { + x = []; + x.push("hello"); + } + return x; // (string | number)[] +} +function f6() { + var x; + if (cond()) { + x = 5; + } + else { + x = []; + x.push("hello"); + } + return x; // number | string[] +} +function f7() { + var x = null; + if (cond()) { + x = []; + while (cond()) { + x.push("hello"); + } + } + return x; // string[] | null +} +function f8() { + var x = []; + x.push(5); + if (cond()) + return x; // number[] + x.push("hello"); + if (cond()) + return x; // (string | number)[] + x.push(true); + return x; // (string | number | boolean)[] +} +function f9() { + var x = []; + if (cond()) { + x.push(5); + return x; // number[] + } + else { + x.push("hello"); + return x; // string[] + } +} +function f10() { + var x = []; + if (cond()) { + x.push(true); + x; // boolean[] + } + else { + x.push(5); + x; // number[] + while (cond()) { + x.push("hello"); + } + x; // (string | number)[] + } + x.push(99); + return x; // (string | number | boolean)[] +} +function f11() { + var x = []; + if (x.length === 0) { + x.push("hello"); + } + return x; +} +function f12() { + var x; + x = []; + if (x.length === 0) { + x.push("hello"); + } + return x; +} +function f13() { + var x = []; + x.push(5); + x.push("hello"); + x.push(true); + return x; // (string | number | boolean)[] +} +function f14() { + var x = []; + x.push(5); + x.push("hello"); + x.push(true); + return x; // (string | number | boolean)[] +} +function f15() { + var x = []; + while (cond()) { + while (cond()) { } + x.push("hello"); + } + return x; // string[] +} +function f16() { + var x; + var y; + (x = [], x).push(5); + (x.push("hello"), x).push(true); + ((x))[3] = { a: 1 }; + return x; // (string | number | boolean | { a: number })[] +} +function f17() { + var x = []; + x.unshift(5); + x.unshift("hello"); + x.unshift(true); + return x; // (string | number | boolean)[] +} +function f18() { + var x = []; + x.push(5); + x.unshift("hello"); + x[2] = true; + return x; // (string | number | boolean)[] +} diff --git a/tests/baselines/reference/controlFlowArrays.symbols b/tests/baselines/reference/controlFlowArrays.symbols new file mode 100644 index 0000000000000..184a813a05fd2 --- /dev/null +++ b/tests/baselines/reference/controlFlowArrays.symbols @@ -0,0 +1,470 @@ +=== tests/cases/compiler/controlFlowArrays.ts === + +declare function cond(): boolean; +>cond : Symbol(cond, Decl(controlFlowArrays.ts, 0, 0)) + +function f1() { +>f1 : Symbol(f1, Decl(controlFlowArrays.ts, 1, 33)) + + let x = []; +>x : Symbol(x, Decl(controlFlowArrays.ts, 4, 7)) + + x[0] = 5; +>x : Symbol(x, Decl(controlFlowArrays.ts, 4, 7)) + + x[1] = "hello"; +>x : Symbol(x, Decl(controlFlowArrays.ts, 4, 7)) + + x[2] = true; +>x : Symbol(x, Decl(controlFlowArrays.ts, 4, 7)) + + return x; // (string | number | boolean)[] +>x : Symbol(x, Decl(controlFlowArrays.ts, 4, 7)) +} + +function f2() { +>f2 : Symbol(f2, Decl(controlFlowArrays.ts, 9, 1)) + + let x = []; +>x : Symbol(x, Decl(controlFlowArrays.ts, 12, 7)) + + x.push(5); +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 12, 7)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + + x.push("hello"); +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 12, 7)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + + x.push(true); +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 12, 7)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + + return x; // (string | number | boolean)[] +>x : Symbol(x, Decl(controlFlowArrays.ts, 12, 7)) +} + +function f3() { +>f3 : Symbol(f3, Decl(controlFlowArrays.ts, 17, 1)) + + let x; +>x : Symbol(x, Decl(controlFlowArrays.ts, 20, 7)) + + x = []; +>x : Symbol(x, Decl(controlFlowArrays.ts, 20, 7)) + + x.push(5, "hello"); +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 20, 7)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + + return x; // (string | number)[] +>x : Symbol(x, Decl(controlFlowArrays.ts, 20, 7)) +} + +function f4() { +>f4 : Symbol(f4, Decl(controlFlowArrays.ts, 24, 1)) + + let x = []; +>x : Symbol(x, Decl(controlFlowArrays.ts, 27, 7)) + + if (cond()) { +>cond : Symbol(cond, Decl(controlFlowArrays.ts, 0, 0)) + + x.push(5); +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 27, 7)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + } + else { + x.push("hello"); +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 27, 7)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + } + return x; // (string | number)[] +>x : Symbol(x, Decl(controlFlowArrays.ts, 27, 7)) +} + +function f5() { +>f5 : Symbol(f5, Decl(controlFlowArrays.ts, 35, 1)) + + let x; +>x : Symbol(x, Decl(controlFlowArrays.ts, 38, 7)) + + if (cond()) { +>cond : Symbol(cond, Decl(controlFlowArrays.ts, 0, 0)) + + x = []; +>x : Symbol(x, Decl(controlFlowArrays.ts, 38, 7)) + + x.push(5); +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 38, 7)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + } + else { + x = []; +>x : Symbol(x, Decl(controlFlowArrays.ts, 38, 7)) + + x.push("hello"); +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 38, 7)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + } + return x; // (string | number)[] +>x : Symbol(x, Decl(controlFlowArrays.ts, 38, 7)) +} + +function f6() { +>f6 : Symbol(f6, Decl(controlFlowArrays.ts, 48, 1)) + + let x; +>x : Symbol(x, Decl(controlFlowArrays.ts, 51, 7)) + + if (cond()) { +>cond : Symbol(cond, Decl(controlFlowArrays.ts, 0, 0)) + + x = 5; +>x : Symbol(x, Decl(controlFlowArrays.ts, 51, 7)) + } + else { + x = []; +>x : Symbol(x, Decl(controlFlowArrays.ts, 51, 7)) + + x.push("hello"); +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 51, 7)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + } + return x; // number | string[] +>x : Symbol(x, Decl(controlFlowArrays.ts, 51, 7)) +} + +function f7() { +>f7 : Symbol(f7, Decl(controlFlowArrays.ts, 60, 1)) + + let x = null; +>x : Symbol(x, Decl(controlFlowArrays.ts, 63, 7)) + + if (cond()) { +>cond : Symbol(cond, Decl(controlFlowArrays.ts, 0, 0)) + + x = []; +>x : Symbol(x, Decl(controlFlowArrays.ts, 63, 7)) + + while (cond()) { +>cond : Symbol(cond, Decl(controlFlowArrays.ts, 0, 0)) + + x.push("hello"); +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 63, 7)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + } + } + return x; // string[] | null +>x : Symbol(x, Decl(controlFlowArrays.ts, 63, 7)) +} + +function f8() { +>f8 : Symbol(f8, Decl(controlFlowArrays.ts, 71, 1)) + + let x = []; +>x : Symbol(x, Decl(controlFlowArrays.ts, 74, 7)) + + x.push(5); +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 74, 7)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + + if (cond()) return x; // number[] +>cond : Symbol(cond, Decl(controlFlowArrays.ts, 0, 0)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 74, 7)) + + x.push("hello"); +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 74, 7)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + + if (cond()) return x; // (string | number)[] +>cond : Symbol(cond, Decl(controlFlowArrays.ts, 0, 0)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 74, 7)) + + x.push(true); +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 74, 7)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + + return x; // (string | number | boolean)[] +>x : Symbol(x, Decl(controlFlowArrays.ts, 74, 7)) +} + +function f9() { +>f9 : Symbol(f9, Decl(controlFlowArrays.ts, 81, 1)) + + let x = []; +>x : Symbol(x, Decl(controlFlowArrays.ts, 84, 7)) + + if (cond()) { +>cond : Symbol(cond, Decl(controlFlowArrays.ts, 0, 0)) + + x.push(5); +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 84, 7)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + + return x; // number[] +>x : Symbol(x, Decl(controlFlowArrays.ts, 84, 7)) + } + else { + x.push("hello"); +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 84, 7)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + + return x; // string[] +>x : Symbol(x, Decl(controlFlowArrays.ts, 84, 7)) + } +} + +function f10() { +>f10 : Symbol(f10, Decl(controlFlowArrays.ts, 93, 1)) + + let x = []; +>x : Symbol(x, Decl(controlFlowArrays.ts, 96, 7)) + + if (cond()) { +>cond : Symbol(cond, Decl(controlFlowArrays.ts, 0, 0)) + + x.push(true); +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 96, 7)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + + x; // boolean[] +>x : Symbol(x, Decl(controlFlowArrays.ts, 96, 7)) + } + else { + x.push(5); +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 96, 7)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + + x; // number[] +>x : Symbol(x, Decl(controlFlowArrays.ts, 96, 7)) + + while (cond()) { +>cond : Symbol(cond, Decl(controlFlowArrays.ts, 0, 0)) + + x.push("hello"); +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 96, 7)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + } + x; // (string | number)[] +>x : Symbol(x, Decl(controlFlowArrays.ts, 96, 7)) + } + x.push(99); +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 96, 7)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + + return x; // (string | number | boolean)[] +>x : Symbol(x, Decl(controlFlowArrays.ts, 96, 7)) +} + +function f11() { +>f11 : Symbol(f11, Decl(controlFlowArrays.ts, 111, 1)) + + let x = []; +>x : Symbol(x, Decl(controlFlowArrays.ts, 114, 7)) + + if (x.length === 0) { // x.length ok on implicit any[] +>x.length : Symbol(Array.length, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 114, 7)) +>length : Symbol(Array.length, Decl(lib.d.ts, --, --)) + + x.push("hello"); +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 114, 7)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + } + return x; +>x : Symbol(x, Decl(controlFlowArrays.ts, 114, 7)) +} + +function f12() { +>f12 : Symbol(f12, Decl(controlFlowArrays.ts, 119, 1)) + + let x; +>x : Symbol(x, Decl(controlFlowArrays.ts, 122, 7)) + + x = []; +>x : Symbol(x, Decl(controlFlowArrays.ts, 122, 7)) + + if (x.length === 0) { // x.length ok on implicit any[] +>x.length : Symbol(Array.length, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 122, 7)) +>length : Symbol(Array.length, Decl(lib.d.ts, --, --)) + + x.push("hello"); +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 122, 7)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + } + return x; +>x : Symbol(x, Decl(controlFlowArrays.ts, 122, 7)) +} + +function f13() { +>f13 : Symbol(f13, Decl(controlFlowArrays.ts, 128, 1)) + + var x = []; +>x : Symbol(x, Decl(controlFlowArrays.ts, 131, 7)) + + x.push(5); +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 131, 7)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + + x.push("hello"); +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 131, 7)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + + x.push(true); +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 131, 7)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + + return x; // (string | number | boolean)[] +>x : Symbol(x, Decl(controlFlowArrays.ts, 131, 7)) +} + +function f14() { +>f14 : Symbol(f14, Decl(controlFlowArrays.ts, 136, 1)) + + const x = []; +>x : Symbol(x, Decl(controlFlowArrays.ts, 139, 9)) + + x.push(5); +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 139, 9)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + + x.push("hello"); +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 139, 9)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + + x.push(true); +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 139, 9)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + + return x; // (string | number | boolean)[] +>x : Symbol(x, Decl(controlFlowArrays.ts, 139, 9)) +} + +function f15() { +>f15 : Symbol(f15, Decl(controlFlowArrays.ts, 144, 1)) + + let x = []; +>x : Symbol(x, Decl(controlFlowArrays.ts, 147, 7)) + + while (cond()) { +>cond : Symbol(cond, Decl(controlFlowArrays.ts, 0, 0)) + + while (cond()) {} +>cond : Symbol(cond, Decl(controlFlowArrays.ts, 0, 0)) + + x.push("hello"); +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 147, 7)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + } + return x; // string[] +>x : Symbol(x, Decl(controlFlowArrays.ts, 147, 7)) +} + +function f16() { +>f16 : Symbol(f16, Decl(controlFlowArrays.ts, 153, 1)) + + let x; +>x : Symbol(x, Decl(controlFlowArrays.ts, 156, 7)) + + let y; +>y : Symbol(y, Decl(controlFlowArrays.ts, 157, 7)) + + (x = [], x).push(5); +>(x = [], x).push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 156, 7)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 156, 7)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + + (x.push("hello"), x).push(true); +>(x.push("hello"), x).push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 156, 7)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 156, 7)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + + ((x))[3] = { a: 1 }; +>x : Symbol(x, Decl(controlFlowArrays.ts, 156, 7)) +>a : Symbol(a, Decl(controlFlowArrays.ts, 160, 16)) + + return x; // (string | number | boolean | { a: number })[] +>x : Symbol(x, Decl(controlFlowArrays.ts, 156, 7)) +} + +function f17() { +>f17 : Symbol(f17, Decl(controlFlowArrays.ts, 162, 1)) + + let x = []; +>x : Symbol(x, Decl(controlFlowArrays.ts, 165, 7)) + + x.unshift(5); +>x.unshift : Symbol(Array.unshift, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 165, 7)) +>unshift : Symbol(Array.unshift, Decl(lib.d.ts, --, --)) + + x.unshift("hello"); +>x.unshift : Symbol(Array.unshift, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 165, 7)) +>unshift : Symbol(Array.unshift, Decl(lib.d.ts, --, --)) + + x.unshift(true); +>x.unshift : Symbol(Array.unshift, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 165, 7)) +>unshift : Symbol(Array.unshift, Decl(lib.d.ts, --, --)) + + return x; // (string | number | boolean)[] +>x : Symbol(x, Decl(controlFlowArrays.ts, 165, 7)) +} + +function f18() { +>f18 : Symbol(f18, Decl(controlFlowArrays.ts, 170, 1)) + + let x = []; +>x : Symbol(x, Decl(controlFlowArrays.ts, 173, 7)) + + x.push(5); +>x.push : Symbol(Array.push, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 173, 7)) +>push : Symbol(Array.push, Decl(lib.d.ts, --, --)) + + x.unshift("hello"); +>x.unshift : Symbol(Array.unshift, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(controlFlowArrays.ts, 173, 7)) +>unshift : Symbol(Array.unshift, Decl(lib.d.ts, --, --)) + + x[2] = true; +>x : Symbol(x, Decl(controlFlowArrays.ts, 173, 7)) + + return x; // (string | number | boolean)[] +>x : Symbol(x, Decl(controlFlowArrays.ts, 173, 7)) +} diff --git a/tests/baselines/reference/controlFlowArrays.types b/tests/baselines/reference/controlFlowArrays.types new file mode 100644 index 0000000000000..2a27bbf10ded4 --- /dev/null +++ b/tests/baselines/reference/controlFlowArrays.types @@ -0,0 +1,615 @@ +=== tests/cases/compiler/controlFlowArrays.ts === + +declare function cond(): boolean; +>cond : () => boolean + +function f1() { +>f1 : () => (string | number | boolean)[] + + let x = []; +>x : any[] +>[] : never[] + + x[0] = 5; +>x[0] = 5 : 5 +>x[0] : any +>x : any[] +>0 : 0 +>5 : 5 + + x[1] = "hello"; +>x[1] = "hello" : "hello" +>x[1] : any +>x : any[] +>1 : 1 +>"hello" : "hello" + + x[2] = true; +>x[2] = true : true +>x[2] : any +>x : any[] +>2 : 2 +>true : true + + return x; // (string | number | boolean)[] +>x : (string | number | boolean)[] +} + +function f2() { +>f2 : () => (string | number | boolean)[] + + let x = []; +>x : any[] +>[] : never[] + + x.push(5); +>x.push(5) : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>5 : 5 + + x.push("hello"); +>x.push("hello") : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>"hello" : "hello" + + x.push(true); +>x.push(true) : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>true : true + + return x; // (string | number | boolean)[] +>x : (string | number | boolean)[] +} + +function f3() { +>f3 : () => (string | number)[] + + let x; +>x : any + + x = []; +>x = [] : never[] +>x : any +>[] : never[] + + x.push(5, "hello"); +>x.push(5, "hello") : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>5 : 5 +>"hello" : "hello" + + return x; // (string | number)[] +>x : (string | number)[] +} + +function f4() { +>f4 : () => (string | number)[] + + let x = []; +>x : any[] +>[] : never[] + + if (cond()) { +>cond() : boolean +>cond : () => boolean + + x.push(5); +>x.push(5) : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>5 : 5 + } + else { + x.push("hello"); +>x.push("hello") : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>"hello" : "hello" + } + return x; // (string | number)[] +>x : (string | number)[] +} + +function f5() { +>f5 : () => (string | number)[] + + let x; +>x : any + + if (cond()) { +>cond() : boolean +>cond : () => boolean + + x = []; +>x = [] : never[] +>x : any +>[] : never[] + + x.push(5); +>x.push(5) : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>5 : 5 + } + else { + x = []; +>x = [] : never[] +>x : any +>[] : never[] + + x.push("hello"); +>x.push("hello") : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>"hello" : "hello" + } + return x; // (string | number)[] +>x : (string | number)[] +} + +function f6() { +>f6 : () => number | string[] + + let x; +>x : any + + if (cond()) { +>cond() : boolean +>cond : () => boolean + + x = 5; +>x = 5 : 5 +>x : any +>5 : 5 + } + else { + x = []; +>x = [] : never[] +>x : any +>[] : never[] + + x.push("hello"); +>x.push("hello") : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>"hello" : "hello" + } + return x; // number | string[] +>x : number | string[] +} + +function f7() { +>f7 : () => string[] | null + + let x = null; +>x : any +>null : null + + if (cond()) { +>cond() : boolean +>cond : () => boolean + + x = []; +>x = [] : never[] +>x : any +>[] : never[] + + while (cond()) { +>cond() : boolean +>cond : () => boolean + + x.push("hello"); +>x.push("hello") : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>"hello" : "hello" + } + } + return x; // string[] | null +>x : string[] | null +} + +function f8() { +>f8 : () => (string | number | boolean)[] + + let x = []; +>x : any[] +>[] : never[] + + x.push(5); +>x.push(5) : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>5 : 5 + + if (cond()) return x; // number[] +>cond() : boolean +>cond : () => boolean +>x : number[] + + x.push("hello"); +>x.push("hello") : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>"hello" : "hello" + + if (cond()) return x; // (string | number)[] +>cond() : boolean +>cond : () => boolean +>x : (string | number)[] + + x.push(true); +>x.push(true) : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>true : true + + return x; // (string | number | boolean)[] +>x : (string | number | boolean)[] +} + +function f9() { +>f9 : () => string[] | number[] + + let x = []; +>x : any[] +>[] : never[] + + if (cond()) { +>cond() : boolean +>cond : () => boolean + + x.push(5); +>x.push(5) : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>5 : 5 + + return x; // number[] +>x : number[] + } + else { + x.push("hello"); +>x.push("hello") : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>"hello" : "hello" + + return x; // string[] +>x : string[] + } +} + +function f10() { +>f10 : () => (string | number | boolean)[] + + let x = []; +>x : any[] +>[] : never[] + + if (cond()) { +>cond() : boolean +>cond : () => boolean + + x.push(true); +>x.push(true) : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>true : true + + x; // boolean[] +>x : boolean[] + } + else { + x.push(5); +>x.push(5) : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>5 : 5 + + x; // number[] +>x : number[] + + while (cond()) { +>cond() : boolean +>cond : () => boolean + + x.push("hello"); +>x.push("hello") : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>"hello" : "hello" + } + x; // (string | number)[] +>x : (string | number)[] + } + x.push(99); +>x.push(99) : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>99 : 99 + + return x; // (string | number | boolean)[] +>x : (string | number | boolean)[] +} + +function f11() { +>f11 : () => string[] + + let x = []; +>x : any[] +>[] : never[] + + if (x.length === 0) { // x.length ok on implicit any[] +>x.length === 0 : boolean +>x.length : number +>x : any[] +>length : number +>0 : 0 + + x.push("hello"); +>x.push("hello") : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>"hello" : "hello" + } + return x; +>x : string[] +} + +function f12() { +>f12 : () => string[] + + let x; +>x : any + + x = []; +>x = [] : never[] +>x : any +>[] : never[] + + if (x.length === 0) { // x.length ok on implicit any[] +>x.length === 0 : boolean +>x.length : number +>x : any[] +>length : number +>0 : 0 + + x.push("hello"); +>x.push("hello") : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>"hello" : "hello" + } + return x; +>x : string[] +} + +function f13() { +>f13 : () => (string | number | boolean)[] + + var x = []; +>x : any[] +>[] : never[] + + x.push(5); +>x.push(5) : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>5 : 5 + + x.push("hello"); +>x.push("hello") : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>"hello" : "hello" + + x.push(true); +>x.push(true) : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>true : true + + return x; // (string | number | boolean)[] +>x : (string | number | boolean)[] +} + +function f14() { +>f14 : () => (string | number | boolean)[] + + const x = []; +>x : any[] +>[] : never[] + + x.push(5); +>x.push(5) : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>5 : 5 + + x.push("hello"); +>x.push("hello") : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>"hello" : "hello" + + x.push(true); +>x.push(true) : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>true : true + + return x; // (string | number | boolean)[] +>x : (string | number | boolean)[] +} + +function f15() { +>f15 : () => string[] + + let x = []; +>x : any[] +>[] : never[] + + while (cond()) { +>cond() : boolean +>cond : () => boolean + + while (cond()) {} +>cond() : boolean +>cond : () => boolean + + x.push("hello"); +>x.push("hello") : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>"hello" : "hello" + } + return x; // string[] +>x : string[] +} + +function f16() { +>f16 : () => (string | number | boolean | { a: number; })[] + + let x; +>x : any + + let y; +>y : any + + (x = [], x).push(5); +>(x = [], x).push(5) : number +>(x = [], x).push : (...items: any[]) => number +>(x = [], x) : any[] +>x = [], x : any[] +>x = [] : never[] +>x : any +>[] : never[] +>x : any[] +>push : (...items: any[]) => number +>5 : 5 + + (x.push("hello"), x).push(true); +>(x.push("hello"), x).push(true) : number +>(x.push("hello"), x).push : (...items: any[]) => number +>(x.push("hello"), x) : any[] +>x.push("hello"), x : any[] +>x.push("hello") : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>"hello" : "hello" +>x : any[] +>push : (...items: any[]) => number +>true : true + + ((x))[3] = { a: 1 }; +>((x))[3] = { a: 1 } : { a: number; } +>((x))[3] : any +>((x)) : any[] +>(x) : any[] +>x : any[] +>3 : 3 +>{ a: 1 } : { a: number; } +>a : number +>1 : 1 + + return x; // (string | number | boolean | { a: number })[] +>x : (string | number | boolean | { a: number; })[] +} + +function f17() { +>f17 : () => (string | number | boolean)[] + + let x = []; +>x : any[] +>[] : never[] + + x.unshift(5); +>x.unshift(5) : number +>x.unshift : (...items: any[]) => number +>x : any[] +>unshift : (...items: any[]) => number +>5 : 5 + + x.unshift("hello"); +>x.unshift("hello") : number +>x.unshift : (...items: any[]) => number +>x : any[] +>unshift : (...items: any[]) => number +>"hello" : "hello" + + x.unshift(true); +>x.unshift(true) : number +>x.unshift : (...items: any[]) => number +>x : any[] +>unshift : (...items: any[]) => number +>true : true + + return x; // (string | number | boolean)[] +>x : (string | number | boolean)[] +} + +function f18() { +>f18 : () => (string | number | boolean)[] + + let x = []; +>x : any[] +>[] : never[] + + x.push(5); +>x.push(5) : number +>x.push : (...items: any[]) => number +>x : any[] +>push : (...items: any[]) => number +>5 : 5 + + x.unshift("hello"); +>x.unshift("hello") : number +>x.unshift : (...items: any[]) => number +>x : any[] +>unshift : (...items: any[]) => number +>"hello" : "hello" + + x[2] = true; +>x[2] = true : true +>x[2] : any +>x : any[] +>2 : 2 +>true : true + + return x; // (string | number | boolean)[] +>x : (string | number | boolean)[] +} diff --git a/tests/baselines/reference/declFileTypeAnnotationStringLiteral.types b/tests/baselines/reference/declFileTypeAnnotationStringLiteral.types index 6d9c17b4ab907..b1a85f3fc3038 100644 --- a/tests/baselines/reference/declFileTypeAnnotationStringLiteral.types +++ b/tests/baselines/reference/declFileTypeAnnotationStringLiteral.types @@ -23,7 +23,7 @@ function foo(a: string): string | number { return a.length; >a.length : number ->a : string +>a : "hello" >length : number } diff --git a/tests/baselines/reference/declareModifierOnImport1.js b/tests/baselines/reference/declareModifierOnImport1.js index 510407b4debc7..3dc3601d4704a 100644 --- a/tests/baselines/reference/declareModifierOnImport1.js +++ b/tests/baselines/reference/declareModifierOnImport1.js @@ -2,3 +2,4 @@ declare import a = b; //// [declareModifierOnImport1.js] +var a = b; diff --git a/tests/baselines/reference/es2017basicAsync.js b/tests/baselines/reference/es2017basicAsync.js new file mode 100644 index 0000000000000..d0443c1899c70 --- /dev/null +++ b/tests/baselines/reference/es2017basicAsync.js @@ -0,0 +1,87 @@ +//// [es2017basicAsync.ts] + +async (): Promise => { + await 0; +} + +async function asyncFunc() { + await 0; +} + +const asyncArrowFunc = async (): Promise => { + await 0; +} + +async function asyncIIFE() { + await 0; + + await (async function(): Promise { + await 1; + })(); + + await (async function asyncNamedFunc(): Promise { + await 1; + })(); + + await (async (): Promise => { + await 1; + })(); +} + +class AsyncClass { + asyncPropFunc = async function(): Promise { + await 2; + } + + asyncPropNamedFunc = async function namedFunc(): Promise { + await 2; + } + + asyncPropArrowFunc = async (): Promise => { + await 2; + } + + async asyncMethod(): Promise { + await 2; + } +} + + +//// [es2017basicAsync.js] +async () => { + await 0; +}; +async function asyncFunc() { + await 0; +} +const asyncArrowFunc = async () => { + await 0; +}; +async function asyncIIFE() { + await 0; + await (async function () { + await 1; + })(); + await (async function asyncNamedFunc() { + await 1; + })(); + await (async () => { + await 1; + })(); +} +class AsyncClass { + constructor() { + this.asyncPropFunc = async function () { + await 2; + }; + this.asyncPropNamedFunc = async function namedFunc() { + await 2; + }; + this.asyncPropArrowFunc = async () => { + await 2; + }; + } + async asyncMethod() { + await 2; + } +} diff --git a/tests/baselines/reference/es2017basicAsync.symbols b/tests/baselines/reference/es2017basicAsync.symbols new file mode 100644 index 0000000000000..d79faf1502af2 --- /dev/null +++ b/tests/baselines/reference/es2017basicAsync.symbols @@ -0,0 +1,79 @@ +=== tests/cases/compiler/es2017basicAsync.ts === + +async (): Promise => { +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + + await 0; +} + +async function asyncFunc() { +>asyncFunc : Symbol(asyncFunc, Decl(es2017basicAsync.ts, 3, 1)) + + await 0; +} + +const asyncArrowFunc = async (): Promise => { +>asyncArrowFunc : Symbol(asyncArrowFunc, Decl(es2017basicAsync.ts, 9, 5)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + + await 0; +} + +async function asyncIIFE() { +>asyncIIFE : Symbol(asyncIIFE, Decl(es2017basicAsync.ts, 11, 1)) + + await 0; + + await (async function(): Promise { +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + + await 1; + })(); + + await (async function asyncNamedFunc(): Promise { +>asyncNamedFunc : Symbol(asyncNamedFunc, Decl(es2017basicAsync.ts, 20, 11)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + + await 1; + })(); + + await (async (): Promise => { +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + + await 1; + })(); +} + +class AsyncClass { +>AsyncClass : Symbol(AsyncClass, Decl(es2017basicAsync.ts, 27, 1)) + + asyncPropFunc = async function(): Promise { +>asyncPropFunc : Symbol(AsyncClass.asyncPropFunc, Decl(es2017basicAsync.ts, 29, 18)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + + await 2; + } + + asyncPropNamedFunc = async function namedFunc(): Promise { +>asyncPropNamedFunc : Symbol(AsyncClass.asyncPropNamedFunc, Decl(es2017basicAsync.ts, 32, 5)) +>namedFunc : Symbol(namedFunc, Decl(es2017basicAsync.ts, 34, 24)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + + await 2; + } + + asyncPropArrowFunc = async (): Promise => { +>asyncPropArrowFunc : Symbol(AsyncClass.asyncPropArrowFunc, Decl(es2017basicAsync.ts, 36, 5)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + + await 2; + } + + async asyncMethod(): Promise { +>asyncMethod : Symbol(AsyncClass.asyncMethod, Decl(es2017basicAsync.ts, 40, 5)) +>Promise : Symbol(Promise, Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --)) + + await 2; + } +} + diff --git a/tests/baselines/reference/es2017basicAsync.types b/tests/baselines/reference/es2017basicAsync.types new file mode 100644 index 0000000000000..30d19cb4dfe7f --- /dev/null +++ b/tests/baselines/reference/es2017basicAsync.types @@ -0,0 +1,121 @@ +=== tests/cases/compiler/es2017basicAsync.ts === + +async (): Promise => { +>async (): Promise => { await 0;} : () => Promise +>Promise : Promise + + await 0; +>await 0 : 0 +>0 : 0 +} + +async function asyncFunc() { +>asyncFunc : () => Promise + + await 0; +>await 0 : 0 +>0 : 0 +} + +const asyncArrowFunc = async (): Promise => { +>asyncArrowFunc : () => Promise +>async (): Promise => { await 0;} : () => Promise +>Promise : Promise + + await 0; +>await 0 : 0 +>0 : 0 +} + +async function asyncIIFE() { +>asyncIIFE : () => Promise + + await 0; +>await 0 : 0 +>0 : 0 + + await (async function(): Promise { +>await (async function(): Promise { await 1; })() : void +>(async function(): Promise { await 1; })() : Promise +>(async function(): Promise { await 1; }) : () => Promise +>async function(): Promise { await 1; } : () => Promise +>Promise : Promise + + await 1; +>await 1 : 1 +>1 : 1 + + })(); + + await (async function asyncNamedFunc(): Promise { +>await (async function asyncNamedFunc(): Promise { await 1; })() : void +>(async function asyncNamedFunc(): Promise { await 1; })() : Promise +>(async function asyncNamedFunc(): Promise { await 1; }) : () => Promise +>async function asyncNamedFunc(): Promise { await 1; } : () => Promise +>asyncNamedFunc : () => Promise +>Promise : Promise + + await 1; +>await 1 : 1 +>1 : 1 + + })(); + + await (async (): Promise => { +>await (async (): Promise => { await 1; })() : void +>(async (): Promise => { await 1; })() : Promise +>(async (): Promise => { await 1; }) : () => Promise +>async (): Promise => { await 1; } : () => Promise +>Promise : Promise + + await 1; +>await 1 : 1 +>1 : 1 + + })(); +} + +class AsyncClass { +>AsyncClass : AsyncClass + + asyncPropFunc = async function(): Promise { +>asyncPropFunc : () => Promise +>async function(): Promise { await 2; } : () => Promise +>Promise : Promise + + await 2; +>await 2 : 2 +>2 : 2 + } + + asyncPropNamedFunc = async function namedFunc(): Promise { +>asyncPropNamedFunc : () => Promise +>async function namedFunc(): Promise { await 2; } : () => Promise +>namedFunc : () => Promise +>Promise : Promise + + await 2; +>await 2 : 2 +>2 : 2 + } + + asyncPropArrowFunc = async (): Promise => { +>asyncPropArrowFunc : () => Promise +>async (): Promise => { await 2; } : () => Promise +>Promise : Promise + + await 2; +>await 2 : 2 +>2 : 2 + } + + async asyncMethod(): Promise { +>asyncMethod : () => Promise +>Promise : Promise + + await 2; +>await 2 : 2 +>2 : 2 + } +} + diff --git a/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBinding1InEs5.js b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBinding1InEs5.js index d62cfbc2872da..0a0e028b696ea 100644 --- a/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBinding1InEs5.js +++ b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBinding1InEs5.js @@ -16,7 +16,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.default = a; //// [es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5_1.js] "use strict"; -var es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5_0_1 = require("./es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5_0"), nameSpaceBinding = es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5_0_1; +var es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5_0_1 = require("./es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5_0"); var x = es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5_0_1.default; diff --git a/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBinding1WithExport.js b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBinding1WithExport.js index c19f68463fac3..5f2cb4e97e093 100644 --- a/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBinding1WithExport.js +++ b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBinding1WithExport.js @@ -19,7 +19,6 @@ define(["require", "exports"], function (require, exports) { //// [client.js] define(["require", "exports", "server"], function (require, exports, server_1) { "use strict"; - var nameSpaceBinding = server_1; exports.x = server_1.default; }); diff --git a/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBindingDts.js b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBindingDts.js index 20abd2e61327f..60bdc6548423b 100644 --- a/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBindingDts.js +++ b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBindingDts.js @@ -18,7 +18,7 @@ var a = (function () { exports.a = a; //// [client.js] "use strict"; -var server_1 = require("./server"), nameSpaceBinding = server_1; +var nameSpaceBinding = require("./server"); exports.x = new nameSpaceBinding.a(); diff --git a/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBindingDts1.js b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBindingDts1.js index 5a2f1f35290f9..8752157eddd39 100644 --- a/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBindingDts1.js +++ b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBindingDts1.js @@ -23,7 +23,6 @@ define(["require", "exports"], function (require, exports) { //// [client.js] define(["require", "exports", "server"], function (require, exports, server_1) { "use strict"; - var nameSpaceBinding = server_1; exports.x = new server_1.default(); }); diff --git a/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5.js b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5.js index 2639291d854e4..9d11e1547550e 100644 --- a/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5.js +++ b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5.js @@ -13,7 +13,7 @@ var x: number = nameSpaceBinding.a; exports.a = 10; //// [es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5_1.js] "use strict"; -var es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5_0_1 = require("./es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5_0"), nameSpaceBinding = es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5_0_1; +var nameSpaceBinding = require("./es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5_0"); var x = nameSpaceBinding.a; diff --git a/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBindingWithExport.js b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBindingWithExport.js index f417a086df790..e16ebf99957a2 100644 --- a/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBindingWithExport.js +++ b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBindingWithExport.js @@ -13,7 +13,7 @@ export var x: number = nameSpaceBinding.a; exports.a = 10; //// [client.js] "use strict"; -var server_1 = require("./server"), nameSpaceBinding = server_1; +var nameSpaceBinding = require("./server"); exports.x = nameSpaceBinding.a; diff --git a/tests/baselines/reference/implicitAnyWidenToAny.errors.txt b/tests/baselines/reference/implicitAnyWidenToAny.errors.txt index 95739a450a888..4302211d9a0eb 100644 --- a/tests/baselines/reference/implicitAnyWidenToAny.errors.txt +++ b/tests/baselines/reference/implicitAnyWidenToAny.errors.txt @@ -1,17 +1,14 @@ tests/cases/compiler/implicitAnyWidenToAny.ts(4,5): error TS7005: Variable 'widenArray' implicitly has an 'any[]' type. -tests/cases/compiler/implicitAnyWidenToAny.ts(5,5): error TS7005: Variable 'emptyArray' implicitly has an 'any[]' type. -==== tests/cases/compiler/implicitAnyWidenToAny.ts (2 errors) ==== +==== tests/cases/compiler/implicitAnyWidenToAny.ts (1 errors) ==== // these should be errors var x = null; // error at "x" var x1 = undefined; // error at "x1" var widenArray = [null, undefined]; // error at "widenArray" ~~~~~~~~~~ !!! error TS7005: Variable 'widenArray' implicitly has an 'any[]' type. - var emptyArray = []; // error at "emptyArray" - ~~~~~~~~~~ -!!! error TS7005: Variable 'emptyArray' implicitly has an 'any[]' type. + var emptyArray = []; // these should not be error class AnimalObj { diff --git a/tests/baselines/reference/implicitAnyWidenToAny.js b/tests/baselines/reference/implicitAnyWidenToAny.js index 0779a16674479..0d1c42ad9d049 100644 --- a/tests/baselines/reference/implicitAnyWidenToAny.js +++ b/tests/baselines/reference/implicitAnyWidenToAny.js @@ -3,7 +3,7 @@ var x = null; // error at "x" var x1 = undefined; // error at "x1" var widenArray = [null, undefined]; // error at "widenArray" -var emptyArray = []; // error at "emptyArray" +var emptyArray = []; // these should not be error class AnimalObj { @@ -32,7 +32,7 @@ var obj1 = anyReturnFunc(); var x = null; // error at "x" var x1 = undefined; // error at "x1" var widenArray = [null, undefined]; // error at "widenArray" -var emptyArray = []; // error at "emptyArray" +var emptyArray = []; // these should not be error var AnimalObj = (function () { function AnimalObj() { diff --git a/tests/baselines/reference/importDeclWithDeclareModifier.js b/tests/baselines/reference/importDeclWithDeclareModifier.js index e5267678a49dd..7c3d3782934f0 100644 --- a/tests/baselines/reference/importDeclWithDeclareModifier.js +++ b/tests/baselines/reference/importDeclWithDeclareModifier.js @@ -9,4 +9,5 @@ var b: a; //// [importDeclWithDeclareModifier.js] "use strict"; +exports.a = x.c; var b; diff --git a/tests/baselines/reference/literalTypes1.types b/tests/baselines/reference/literalTypes1.types index faff485d45adb..1baaad3ff88cc 100644 --- a/tests/baselines/reference/literalTypes1.types +++ b/tests/baselines/reference/literalTypes1.types @@ -129,7 +129,7 @@ function f4(x: 0 | 1 | true | string) { >"def" : "def" x; ->x : string +>x : "abc" | "def" break; case null: @@ -163,7 +163,7 @@ function f5(x: string | number | boolean) { >"abc" : "abc" x; ->x : string +>x : "abc" break; case 0: @@ -173,7 +173,7 @@ function f5(x: string | number | boolean) { >1 : 1 x; ->x : number +>x : 0 | 1 break; case true: @@ -190,7 +190,7 @@ function f5(x: string | number | boolean) { >123 : 123 x; ->x : string | number +>x : "hello" | 123 break; default: diff --git a/tests/baselines/reference/literalTypes3.js b/tests/baselines/reference/literalTypes3.js new file mode 100644 index 0000000000000..1989bb1d5a1be --- /dev/null +++ b/tests/baselines/reference/literalTypes3.js @@ -0,0 +1,125 @@ +//// [literalTypes3.ts] + +function f1(s: string) { + if (s === "foo") { + s; // "foo" + } + if (s === "foo" || s === "bar") { + s; // "foo" | "bar" + } +} + +function f2(s: string) { + switch (s) { + case "foo": + case "bar": + s; // "foo" | "bar" + case "baz": + s; // "foo" | "bar" | "baz" + break; + default: + s; // string + } +} + +function f3(s: string) { + return s === "foo" || s === "bar" ? s : undefined; // "foo" | "bar" | undefined +} + +function f4(x: number) { + if (x === 1 || x === 2) { + return x; // 1 | 2 + } + throw new Error(); +} + +function f5(x: number, y: 1 | 2) { + if (x === 0 || x === y) { + x; // 0 | 1 | 2 + } +} + +function f6(x: number, y: 1 | 2) { + if (y === x || 0 === x) { + x; // 0 | 1 | 2 + } +} + +function f7(x: number | "foo" | "bar", y: 1 | 2 | string) { + if (x === y) { + x; // "foo" | "bar" | 1 | 2 + } +} + +function f8(x: number | "foo" | "bar") { + switch (x) { + case 1: + case 2: + x; // 1 | 2 + break; + case "foo": + x; // "foo" + break; + default: + x; // number | "bar" + } +} + +//// [literalTypes3.js] +function f1(s) { + if (s === "foo") { + s; // "foo" + } + if (s === "foo" || s === "bar") { + s; // "foo" | "bar" + } +} +function f2(s) { + switch (s) { + case "foo": + case "bar": + s; // "foo" | "bar" + case "baz": + s; // "foo" | "bar" | "baz" + break; + default: + s; // string + } +} +function f3(s) { + return s === "foo" || s === "bar" ? s : undefined; // "foo" | "bar" | undefined +} +function f4(x) { + if (x === 1 || x === 2) { + return x; // 1 | 2 + } + throw new Error(); +} +function f5(x, y) { + if (x === 0 || x === y) { + x; // 0 | 1 | 2 + } +} +function f6(x, y) { + if (y === x || 0 === x) { + x; // 0 | 1 | 2 + } +} +function f7(x, y) { + if (x === y) { + x; // "foo" | "bar" | 1 | 2 + } +} +function f8(x) { + switch (x) { + case 1: + case 2: + x; // 1 | 2 + break; + case "foo": + x; // "foo" + break; + default: + x; // number | "bar" + } +} diff --git a/tests/baselines/reference/literalTypes3.symbols b/tests/baselines/reference/literalTypes3.symbols new file mode 100644 index 0000000000000..6cc7ab007e680 --- /dev/null +++ b/tests/baselines/reference/literalTypes3.symbols @@ -0,0 +1,137 @@ +=== tests/cases/conformance/types/literal/literalTypes3.ts === + +function f1(s: string) { +>f1 : Symbol(f1, Decl(literalTypes3.ts, 0, 0)) +>s : Symbol(s, Decl(literalTypes3.ts, 1, 12)) + + if (s === "foo") { +>s : Symbol(s, Decl(literalTypes3.ts, 1, 12)) + + s; // "foo" +>s : Symbol(s, Decl(literalTypes3.ts, 1, 12)) + } + if (s === "foo" || s === "bar") { +>s : Symbol(s, Decl(literalTypes3.ts, 1, 12)) +>s : Symbol(s, Decl(literalTypes3.ts, 1, 12)) + + s; // "foo" | "bar" +>s : Symbol(s, Decl(literalTypes3.ts, 1, 12)) + } +} + +function f2(s: string) { +>f2 : Symbol(f2, Decl(literalTypes3.ts, 8, 1)) +>s : Symbol(s, Decl(literalTypes3.ts, 10, 12)) + + switch (s) { +>s : Symbol(s, Decl(literalTypes3.ts, 10, 12)) + + case "foo": + case "bar": + s; // "foo" | "bar" +>s : Symbol(s, Decl(literalTypes3.ts, 10, 12)) + + case "baz": + s; // "foo" | "bar" | "baz" +>s : Symbol(s, Decl(literalTypes3.ts, 10, 12)) + + break; + default: + s; // string +>s : Symbol(s, Decl(literalTypes3.ts, 10, 12)) + } +} + +function f3(s: string) { +>f3 : Symbol(f3, Decl(literalTypes3.ts, 21, 1)) +>s : Symbol(s, Decl(literalTypes3.ts, 23, 12)) + + return s === "foo" || s === "bar" ? s : undefined; // "foo" | "bar" | undefined +>s : Symbol(s, Decl(literalTypes3.ts, 23, 12)) +>s : Symbol(s, Decl(literalTypes3.ts, 23, 12)) +>s : Symbol(s, Decl(literalTypes3.ts, 23, 12)) +>undefined : Symbol(undefined) +} + +function f4(x: number) { +>f4 : Symbol(f4, Decl(literalTypes3.ts, 25, 1)) +>x : Symbol(x, Decl(literalTypes3.ts, 27, 12)) + + if (x === 1 || x === 2) { +>x : Symbol(x, Decl(literalTypes3.ts, 27, 12)) +>x : Symbol(x, Decl(literalTypes3.ts, 27, 12)) + + return x; // 1 | 2 +>x : Symbol(x, Decl(literalTypes3.ts, 27, 12)) + } + throw new Error(); +>Error : Symbol(Error, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +} + +function f5(x: number, y: 1 | 2) { +>f5 : Symbol(f5, Decl(literalTypes3.ts, 32, 1)) +>x : Symbol(x, Decl(literalTypes3.ts, 34, 12)) +>y : Symbol(y, Decl(literalTypes3.ts, 34, 22)) + + if (x === 0 || x === y) { +>x : Symbol(x, Decl(literalTypes3.ts, 34, 12)) +>x : Symbol(x, Decl(literalTypes3.ts, 34, 12)) +>y : Symbol(y, Decl(literalTypes3.ts, 34, 22)) + + x; // 0 | 1 | 2 +>x : Symbol(x, Decl(literalTypes3.ts, 34, 12)) + } +} + +function f6(x: number, y: 1 | 2) { +>f6 : Symbol(f6, Decl(literalTypes3.ts, 38, 1)) +>x : Symbol(x, Decl(literalTypes3.ts, 40, 12)) +>y : Symbol(y, Decl(literalTypes3.ts, 40, 22)) + + if (y === x || 0 === x) { +>y : Symbol(y, Decl(literalTypes3.ts, 40, 22)) +>x : Symbol(x, Decl(literalTypes3.ts, 40, 12)) +>x : Symbol(x, Decl(literalTypes3.ts, 40, 12)) + + x; // 0 | 1 | 2 +>x : Symbol(x, Decl(literalTypes3.ts, 40, 12)) + } +} + +function f7(x: number | "foo" | "bar", y: 1 | 2 | string) { +>f7 : Symbol(f7, Decl(literalTypes3.ts, 44, 1)) +>x : Symbol(x, Decl(literalTypes3.ts, 46, 12)) +>y : Symbol(y, Decl(literalTypes3.ts, 46, 38)) + + if (x === y) { +>x : Symbol(x, Decl(literalTypes3.ts, 46, 12)) +>y : Symbol(y, Decl(literalTypes3.ts, 46, 38)) + + x; // "foo" | "bar" | 1 | 2 +>x : Symbol(x, Decl(literalTypes3.ts, 46, 12)) + } +} + +function f8(x: number | "foo" | "bar") { +>f8 : Symbol(f8, Decl(literalTypes3.ts, 50, 1)) +>x : Symbol(x, Decl(literalTypes3.ts, 52, 12)) + + switch (x) { +>x : Symbol(x, Decl(literalTypes3.ts, 52, 12)) + + case 1: + case 2: + x; // 1 | 2 +>x : Symbol(x, Decl(literalTypes3.ts, 52, 12)) + + break; + case "foo": + x; // "foo" +>x : Symbol(x, Decl(literalTypes3.ts, 52, 12)) + + break; + default: + x; // number | "bar" +>x : Symbol(x, Decl(literalTypes3.ts, 52, 12)) + } +} diff --git a/tests/baselines/reference/literalTypes3.types b/tests/baselines/reference/literalTypes3.types new file mode 100644 index 0000000000000..6161bc4b72a6f --- /dev/null +++ b/tests/baselines/reference/literalTypes3.types @@ -0,0 +1,177 @@ +=== tests/cases/conformance/types/literal/literalTypes3.ts === + +function f1(s: string) { +>f1 : (s: string) => void +>s : string + + if (s === "foo") { +>s === "foo" : boolean +>s : string +>"foo" : "foo" + + s; // "foo" +>s : "foo" + } + if (s === "foo" || s === "bar") { +>s === "foo" || s === "bar" : boolean +>s === "foo" : boolean +>s : string +>"foo" : "foo" +>s === "bar" : boolean +>s : string +>"bar" : "bar" + + s; // "foo" | "bar" +>s : "foo" | "bar" + } +} + +function f2(s: string) { +>f2 : (s: string) => void +>s : string + + switch (s) { +>s : string + + case "foo": +>"foo" : "foo" + + case "bar": +>"bar" : "bar" + + s; // "foo" | "bar" +>s : "foo" | "bar" + + case "baz": +>"baz" : "baz" + + s; // "foo" | "bar" | "baz" +>s : "foo" | "bar" | "baz" + + break; + default: + s; // string +>s : string + } +} + +function f3(s: string) { +>f3 : (s: string) => "foo" | "bar" | undefined +>s : string + + return s === "foo" || s === "bar" ? s : undefined; // "foo" | "bar" | undefined +>s === "foo" || s === "bar" ? s : undefined : "foo" | "bar" | undefined +>s === "foo" || s === "bar" : boolean +>s === "foo" : boolean +>s : string +>"foo" : "foo" +>s === "bar" : boolean +>s : string +>"bar" : "bar" +>s : "foo" | "bar" +>undefined : undefined +} + +function f4(x: number) { +>f4 : (x: number) => 1 | 2 +>x : number + + if (x === 1 || x === 2) { +>x === 1 || x === 2 : boolean +>x === 1 : boolean +>x : number +>1 : 1 +>x === 2 : boolean +>x : number +>2 : 2 + + return x; // 1 | 2 +>x : 1 | 2 + } + throw new Error(); +>new Error() : Error +>Error : ErrorConstructor +} + +function f5(x: number, y: 1 | 2) { +>f5 : (x: number, y: 1 | 2) => void +>x : number +>y : 1 | 2 + + if (x === 0 || x === y) { +>x === 0 || x === y : boolean +>x === 0 : boolean +>x : number +>0 : 0 +>x === y : boolean +>x : number +>y : 1 | 2 + + x; // 0 | 1 | 2 +>x : 1 | 2 | 0 + } +} + +function f6(x: number, y: 1 | 2) { +>f6 : (x: number, y: 1 | 2) => void +>x : number +>y : 1 | 2 + + if (y === x || 0 === x) { +>y === x || 0 === x : boolean +>y === x : boolean +>y : 1 | 2 +>x : number +>0 === x : boolean +>0 : 0 +>x : number + + x; // 0 | 1 | 2 +>x : 1 | 2 | 0 + } +} + +function f7(x: number | "foo" | "bar", y: 1 | 2 | string) { +>f7 : (x: number | "foo" | "bar", y: string | 1 | 2) => void +>x : number | "foo" | "bar" +>y : string | 1 | 2 + + if (x === y) { +>x === y : boolean +>x : number | "foo" | "bar" +>y : string | 1 | 2 + + x; // "foo" | "bar" | 1 | 2 +>x : "foo" | "bar" | 1 | 2 + } +} + +function f8(x: number | "foo" | "bar") { +>f8 : (x: number | "foo" | "bar") => void +>x : number | "foo" | "bar" + + switch (x) { +>x : number | "foo" | "bar" + + case 1: +>1 : 1 + + case 2: +>2 : 2 + + x; // 1 | 2 +>x : 1 | 2 + + break; + case "foo": +>"foo" : "foo" + + x; // "foo" +>x : "foo" + + break; + default: + x; // number | "bar" +>x : number | "bar" + } +} diff --git a/tests/baselines/reference/localClassesInLoop.types b/tests/baselines/reference/localClassesInLoop.types index c88bfc0435d0c..8805044589807 100644 --- a/tests/baselines/reference/localClassesInLoop.types +++ b/tests/baselines/reference/localClassesInLoop.types @@ -35,12 +35,12 @@ use(data[0]() === data[1]()); >use(data[0]() === data[1]()) : any >use : (a: any) => any >data[0]() === data[1]() : boolean ->data[0]() : any ->data[0] : any ->data : any[] +>data[0]() : typeof C +>data[0] : () => typeof C +>data : (() => typeof C)[] >0 : 0 ->data[1]() : any ->data[1] : any ->data : any[] +>data[1]() : typeof C +>data[1] : () => typeof C +>data : (() => typeof C)[] >1 : 1 diff --git a/tests/baselines/reference/localClassesInLoop_ES6.types b/tests/baselines/reference/localClassesInLoop_ES6.types index 9d487f6c60051..390f4b8b1ddb2 100644 --- a/tests/baselines/reference/localClassesInLoop_ES6.types +++ b/tests/baselines/reference/localClassesInLoop_ES6.types @@ -36,12 +36,12 @@ use(data[0]() === data[1]()); >use(data[0]() === data[1]()) : any >use : (a: any) => any >data[0]() === data[1]() : boolean ->data[0]() : any ->data[0] : any ->data : any[] +>data[0]() : typeof C +>data[0] : () => typeof C +>data : (() => typeof C)[] >0 : 0 ->data[1]() : any ->data[1] : any ->data : any[] +>data[1]() : typeof C +>data[1] : () => typeof C +>data : (() => typeof C)[] >1 : 1 diff --git a/tests/baselines/reference/narrowedConstInMethod.js b/tests/baselines/reference/narrowedConstInMethod.js new file mode 100644 index 0000000000000..f4cdc3def9184 --- /dev/null +++ b/tests/baselines/reference/narrowedConstInMethod.js @@ -0,0 +1,40 @@ +//// [narrowedConstInMethod.ts] + +function f() { + const x: string | null = {}; + if (x !== null) { + return { + bar() { return x.length; } // Error: possibly null x + }; + } +} + +function f2() { + const x: string | null = {}; + if (x !== null) { + return class { + bar() { return x.length; } // Error: possibly null x + }; + } +} + +//// [narrowedConstInMethod.js] +function f() { + var x = {}; + if (x !== null) { + return { + bar: function () { return x.length; } // Error: possibly null x + }; + } +} +function f2() { + var x = {}; + if (x !== null) { + return (function () { + function class_1() { + } + class_1.prototype.bar = function () { return x.length; }; // Error: possibly null x + return class_1; + }()); + } +} diff --git a/tests/baselines/reference/narrowedConstInMethod.symbols b/tests/baselines/reference/narrowedConstInMethod.symbols new file mode 100644 index 0000000000000..1fd05dcea5ead --- /dev/null +++ b/tests/baselines/reference/narrowedConstInMethod.symbols @@ -0,0 +1,41 @@ +=== tests/cases/compiler/narrowedConstInMethod.ts === + +function f() { +>f : Symbol(f, Decl(narrowedConstInMethod.ts, 0, 0)) + + const x: string | null = {}; +>x : Symbol(x, Decl(narrowedConstInMethod.ts, 2, 9)) + + if (x !== null) { +>x : Symbol(x, Decl(narrowedConstInMethod.ts, 2, 9)) + + return { + bar() { return x.length; } // Error: possibly null x +>bar : Symbol(bar, Decl(narrowedConstInMethod.ts, 4, 16)) +>x.length : Symbol(String.length, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(narrowedConstInMethod.ts, 2, 9)) +>length : Symbol(String.length, Decl(lib.d.ts, --, --)) + + }; + } +} + +function f2() { +>f2 : Symbol(f2, Decl(narrowedConstInMethod.ts, 8, 1)) + + const x: string | null = {}; +>x : Symbol(x, Decl(narrowedConstInMethod.ts, 11, 9)) + + if (x !== null) { +>x : Symbol(x, Decl(narrowedConstInMethod.ts, 11, 9)) + + return class { + bar() { return x.length; } // Error: possibly null x +>bar : Symbol((Anonymous class).bar, Decl(narrowedConstInMethod.ts, 13, 22)) +>x.length : Symbol(String.length, Decl(lib.d.ts, --, --)) +>x : Symbol(x, Decl(narrowedConstInMethod.ts, 11, 9)) +>length : Symbol(String.length, Decl(lib.d.ts, --, --)) + + }; + } +} diff --git a/tests/baselines/reference/narrowedConstInMethod.types b/tests/baselines/reference/narrowedConstInMethod.types new file mode 100644 index 0000000000000..a73f2edfccbea --- /dev/null +++ b/tests/baselines/reference/narrowedConstInMethod.types @@ -0,0 +1,55 @@ +=== tests/cases/compiler/narrowedConstInMethod.ts === + +function f() { +>f : () => { bar(): number; } | undefined + + const x: string | null = {}; +>x : string | null +>null : null +>{} : any +>{} : {} + + if (x !== null) { +>x !== null : boolean +>x : string | null +>null : null + + return { +>{ bar() { return x.length; } // Error: possibly null x } : { bar(): number; } + + bar() { return x.length; } // Error: possibly null x +>bar : () => number +>x.length : number +>x : string +>length : number + + }; + } +} + +function f2() { +>f2 : () => typeof (Anonymous class) | undefined + + const x: string | null = {}; +>x : string | null +>null : null +>{} : any +>{} : {} + + if (x !== null) { +>x !== null : boolean +>x : string | null +>null : null + + return class { +>class { bar() { return x.length; } // Error: possibly null x } : typeof (Anonymous class) + + bar() { return x.length; } // Error: possibly null x +>bar : () => number +>x.length : number +>x : string +>length : number + + }; + } +} diff --git a/tests/baselines/reference/parserErrorRecovery_ParameterList6.js b/tests/baselines/reference/parserErrorRecovery_ParameterList6.js index c3d5a838f88f5..7c221aaa20ac3 100644 --- a/tests/baselines/reference/parserErrorRecovery_ParameterList6.js +++ b/tests/baselines/reference/parserErrorRecovery_ParameterList6.js @@ -7,7 +7,6 @@ class Foo { var Foo = (function () { function Foo() { } - Foo.prototype.banana = function (x) { }; return Foo; }()); break ; diff --git a/tests/baselines/reference/sourceMapValidationIfElse.types b/tests/baselines/reference/sourceMapValidationIfElse.types index b4a76d5564738..87103b792e7c1 100644 --- a/tests/baselines/reference/sourceMapValidationIfElse.types +++ b/tests/baselines/reference/sourceMapValidationIfElse.types @@ -10,7 +10,7 @@ if (i == 10) { i++; >i++ : number ->i : number +>i : 10 } else { @@ -22,7 +22,7 @@ if (i == 10) { i++; >i++ : number ->i : number +>i : 10 } else if (i == 20) { >i == 20 : boolean @@ -31,7 +31,7 @@ else if (i == 20) { i--; >i-- : number ->i : number +>i : 20 } else if (i == 30) { >i == 30 : boolean diff --git a/tests/baselines/reference/sourceMapValidationSwitch.types b/tests/baselines/reference/sourceMapValidationSwitch.types index 9df86ee213251..359cabbfabe56 100644 --- a/tests/baselines/reference/sourceMapValidationSwitch.types +++ b/tests/baselines/reference/sourceMapValidationSwitch.types @@ -11,7 +11,7 @@ switch (x) { x++; >x++ : number ->x : number +>x : 5 break; case 10: @@ -19,7 +19,7 @@ switch (x) { { x--; >x-- : number ->x : number +>x : 10 break; } @@ -39,7 +39,7 @@ switch (x) x++; >x++ : number ->x : number +>x : 5 break; case 10: @@ -47,7 +47,7 @@ switch (x) { x--; >x-- : number ->x : number +>x : 10 break; } diff --git a/tests/baselines/reference/sourceMapValidationWhile.types b/tests/baselines/reference/sourceMapValidationWhile.types index 71b50d86736cc..8769d98ef256c 100644 --- a/tests/baselines/reference/sourceMapValidationWhile.types +++ b/tests/baselines/reference/sourceMapValidationWhile.types @@ -10,7 +10,7 @@ while (a == 10) { a++; >a++ : number ->a : number +>a : 10 } while (a == 10) >a == 10 : boolean @@ -19,5 +19,5 @@ while (a == 10) { a++; >a++ : number ->a : number +>a : 10 } diff --git a/tests/baselines/reference/strictNullChecksNoWidening.types b/tests/baselines/reference/strictNullChecksNoWidening.types index 375977ae37514..e24797850ee2b 100644 --- a/tests/baselines/reference/strictNullChecksNoWidening.types +++ b/tests/baselines/reference/strictNullChecksNoWidening.types @@ -14,7 +14,7 @@ var a3 = void 0; >0 : 0 var b1 = []; ->b1 : never[] +>b1 : any[] >[] : never[] var b2 = [,]; diff --git a/tests/baselines/reference/stringLiteralTypesInUnionTypes02.types b/tests/baselines/reference/stringLiteralTypesInUnionTypes02.types index 0be891ade44cd..853781082985b 100644 --- a/tests/baselines/reference/stringLiteralTypesInUnionTypes02.types +++ b/tests/baselines/reference/stringLiteralTypesInUnionTypes02.types @@ -19,7 +19,7 @@ if (x === "foo") { let a = x; >a : string ->x : string +>x : "foo" } else if (x !== "bar") { >x !== "bar" : boolean @@ -35,7 +35,7 @@ else if (x !== "bar") { else { let c = x; >c : string ->x : string +>x : "bar" let d = y; >d : string @@ -43,7 +43,7 @@ else { let e: (typeof x) | (typeof y) = c || d; >e : string ->x : string +>x : "bar" >y : string >c || d : string >c : string diff --git a/tests/baselines/reference/throwInEnclosingStatements.types b/tests/baselines/reference/throwInEnclosingStatements.types index 4955b9243aebf..2d99ac435aebf 100644 --- a/tests/baselines/reference/throwInEnclosingStatements.types +++ b/tests/baselines/reference/throwInEnclosingStatements.types @@ -25,7 +25,7 @@ switch (y) { >'a' : "a" throw y; ->y : string +>y : "a" default: throw y; diff --git a/tests/baselines/reference/transformsElideNullUndefinedType.js b/tests/baselines/reference/transformsElideNullUndefinedType.js new file mode 100644 index 0000000000000..63bc77559c127 --- /dev/null +++ b/tests/baselines/reference/transformsElideNullUndefinedType.js @@ -0,0 +1,108 @@ +//// [transformsElideNullUndefinedType.ts] + +var v0: null; +var v1: undefined; + +function f0(): null { return null; } +function f1(): undefined { return undefined; } + +var f2 = function (): null { return null; } +var f3 = function (): undefined { return undefined; } + +var f4 = (): null => null; +var f5 = (): undefined => undefined; + +function f6(p0: null) { } +function f7(p1: undefined) { } + +var f8 = function (p2: null) { } +var f9 = function (p3: undefined) { } + +var f10 = (p4: null) => { } +var f11 = (p5: undefined) => { } + +class C1 { + m0(): null { return null; } + m1(): undefined { return undefined; } + + m3(p6: null) { } + m4(p7: undefined) { } + + get a0(): null { return null; } + get a1(): undefined { return undefined; } + + set a2(p8: null) { } + set a3(p9: undefined) { } +} + +class C2 { constructor(p10: null) { } } +class C3 { constructor(p11: undefined) { } } + +class C4 { + f1; + constructor(p12: null) { } +} + +class C5 { + f2; + constructor(p13: undefined) { } +} + +var C6 = class { constructor(p12: null) { } } +var C7 = class { constructor(p13: undefined) { } } + +declare function fn(); +fn(); +fn(); + +declare class D {} +new D(); +new D(); + +//// [transformsElideNullUndefinedType.js] +var v0; +var v1; +function f0() { return null; } +function f1() { return undefined; } +var f2 = function () { return null; }; +var f3 = function () { return undefined; }; +var f4 = () => null; +var f5 = () => undefined; +function f6(p0) { } +function f7(p1) { } +var f8 = function (p2) { }; +var f9 = function (p3) { }; +var f10 = (p4) => { }; +var f11 = (p5) => { }; +class C1 { + m0() { return null; } + m1() { return undefined; } + m3(p6) { } + m4(p7) { } + get a0() { return null; } + get a1() { return undefined; } + set a2(p8) { } + set a3(p9) { } +} +class C2 { + constructor(p10) { } +} +class C3 { + constructor(p11) { } +} +class C4 { + constructor(p12) { } +} +class C5 { + constructor(p13) { } +} +var C6 = class { + constructor(p12) { } +}; +var C7 = class { + constructor(p13) { } +}; +fn(); +fn(); +new D(); +new D(); diff --git a/tests/baselines/reference/transformsElideNullUndefinedType.symbols b/tests/baselines/reference/transformsElideNullUndefinedType.symbols new file mode 100644 index 0000000000000..b651784a61ca8 --- /dev/null +++ b/tests/baselines/reference/transformsElideNullUndefinedType.symbols @@ -0,0 +1,143 @@ +=== tests/cases/compiler/transformsElideNullUndefinedType.ts === + +var v0: null; +>v0 : Symbol(v0, Decl(transformsElideNullUndefinedType.ts, 1, 3)) + +var v1: undefined; +>v1 : Symbol(v1, Decl(transformsElideNullUndefinedType.ts, 2, 3)) + +function f0(): null { return null; } +>f0 : Symbol(f0, Decl(transformsElideNullUndefinedType.ts, 2, 18)) + +function f1(): undefined { return undefined; } +>f1 : Symbol(f1, Decl(transformsElideNullUndefinedType.ts, 4, 36)) +>undefined : Symbol(undefined) + +var f2 = function (): null { return null; } +>f2 : Symbol(f2, Decl(transformsElideNullUndefinedType.ts, 7, 3)) + +var f3 = function (): undefined { return undefined; } +>f3 : Symbol(f3, Decl(transformsElideNullUndefinedType.ts, 8, 3)) +>undefined : Symbol(undefined) + +var f4 = (): null => null; +>f4 : Symbol(f4, Decl(transformsElideNullUndefinedType.ts, 10, 3)) + +var f5 = (): undefined => undefined; +>f5 : Symbol(f5, Decl(transformsElideNullUndefinedType.ts, 11, 3)) +>undefined : Symbol(undefined) + +function f6(p0: null) { } +>f6 : Symbol(f6, Decl(transformsElideNullUndefinedType.ts, 11, 36)) +>p0 : Symbol(p0, Decl(transformsElideNullUndefinedType.ts, 13, 12)) + +function f7(p1: undefined) { } +>f7 : Symbol(f7, Decl(transformsElideNullUndefinedType.ts, 13, 25)) +>p1 : Symbol(p1, Decl(transformsElideNullUndefinedType.ts, 14, 12)) + +var f8 = function (p2: null) { } +>f8 : Symbol(f8, Decl(transformsElideNullUndefinedType.ts, 16, 3)) +>p2 : Symbol(p2, Decl(transformsElideNullUndefinedType.ts, 16, 19)) + +var f9 = function (p3: undefined) { } +>f9 : Symbol(f9, Decl(transformsElideNullUndefinedType.ts, 17, 3)) +>p3 : Symbol(p3, Decl(transformsElideNullUndefinedType.ts, 17, 19)) + +var f10 = (p4: null) => { } +>f10 : Symbol(f10, Decl(transformsElideNullUndefinedType.ts, 19, 3)) +>p4 : Symbol(p4, Decl(transformsElideNullUndefinedType.ts, 19, 11)) + +var f11 = (p5: undefined) => { } +>f11 : Symbol(f11, Decl(transformsElideNullUndefinedType.ts, 20, 3)) +>p5 : Symbol(p5, Decl(transformsElideNullUndefinedType.ts, 20, 11)) + +class C1 { +>C1 : Symbol(C1, Decl(transformsElideNullUndefinedType.ts, 20, 32)) + + m0(): null { return null; } +>m0 : Symbol(C1.m0, Decl(transformsElideNullUndefinedType.ts, 22, 10)) + + m1(): undefined { return undefined; } +>m1 : Symbol(C1.m1, Decl(transformsElideNullUndefinedType.ts, 23, 31)) +>undefined : Symbol(undefined) + + m3(p6: null) { } +>m3 : Symbol(C1.m3, Decl(transformsElideNullUndefinedType.ts, 24, 41)) +>p6 : Symbol(p6, Decl(transformsElideNullUndefinedType.ts, 26, 7)) + + m4(p7: undefined) { } +>m4 : Symbol(C1.m4, Decl(transformsElideNullUndefinedType.ts, 26, 20)) +>p7 : Symbol(p7, Decl(transformsElideNullUndefinedType.ts, 27, 7)) + + get a0(): null { return null; } +>a0 : Symbol(C1.a0, Decl(transformsElideNullUndefinedType.ts, 27, 25)) + + get a1(): undefined { return undefined; } +>a1 : Symbol(C1.a1, Decl(transformsElideNullUndefinedType.ts, 29, 35)) +>undefined : Symbol(undefined) + + set a2(p8: null) { } +>a2 : Symbol(C1.a2, Decl(transformsElideNullUndefinedType.ts, 30, 45)) +>p8 : Symbol(p8, Decl(transformsElideNullUndefinedType.ts, 32, 11)) + + set a3(p9: undefined) { } +>a3 : Symbol(C1.a3, Decl(transformsElideNullUndefinedType.ts, 32, 24)) +>p9 : Symbol(p9, Decl(transformsElideNullUndefinedType.ts, 33, 11)) +} + +class C2 { constructor(p10: null) { } } +>C2 : Symbol(C2, Decl(transformsElideNullUndefinedType.ts, 34, 1)) +>p10 : Symbol(p10, Decl(transformsElideNullUndefinedType.ts, 36, 23)) + +class C3 { constructor(p11: undefined) { } } +>C3 : Symbol(C3, Decl(transformsElideNullUndefinedType.ts, 36, 39)) +>p11 : Symbol(p11, Decl(transformsElideNullUndefinedType.ts, 37, 23)) + +class C4 { +>C4 : Symbol(C4, Decl(transformsElideNullUndefinedType.ts, 37, 44)) + + f1; +>f1 : Symbol(C4.f1, Decl(transformsElideNullUndefinedType.ts, 39, 10)) + + constructor(p12: null) { } +>p12 : Symbol(p12, Decl(transformsElideNullUndefinedType.ts, 41, 16)) +} + +class C5 { +>C5 : Symbol(C5, Decl(transformsElideNullUndefinedType.ts, 42, 1)) + + f2; +>f2 : Symbol(C5.f2, Decl(transformsElideNullUndefinedType.ts, 44, 10)) + + constructor(p13: undefined) { } +>p13 : Symbol(p13, Decl(transformsElideNullUndefinedType.ts, 46, 16)) +} + +var C6 = class { constructor(p12: null) { } } +>C6 : Symbol(C6, Decl(transformsElideNullUndefinedType.ts, 49, 3)) +>p12 : Symbol(p12, Decl(transformsElideNullUndefinedType.ts, 49, 29)) + +var C7 = class { constructor(p13: undefined) { } } +>C7 : Symbol(C7, Decl(transformsElideNullUndefinedType.ts, 50, 3)) +>p13 : Symbol(p13, Decl(transformsElideNullUndefinedType.ts, 50, 29)) + +declare function fn(); +>fn : Symbol(fn, Decl(transformsElideNullUndefinedType.ts, 50, 50)) +>T : Symbol(T, Decl(transformsElideNullUndefinedType.ts, 52, 20)) + +fn(); +>fn : Symbol(fn, Decl(transformsElideNullUndefinedType.ts, 50, 50)) + +fn(); +>fn : Symbol(fn, Decl(transformsElideNullUndefinedType.ts, 50, 50)) + +declare class D {} +>D : Symbol(D, Decl(transformsElideNullUndefinedType.ts, 54, 16)) +>T : Symbol(T, Decl(transformsElideNullUndefinedType.ts, 56, 16)) + +new D(); +>D : Symbol(D, Decl(transformsElideNullUndefinedType.ts, 54, 16)) + +new D(); +>D : Symbol(D, Decl(transformsElideNullUndefinedType.ts, 54, 16)) + diff --git a/tests/baselines/reference/transformsElideNullUndefinedType.types b/tests/baselines/reference/transformsElideNullUndefinedType.types new file mode 100644 index 0000000000000..7f48d8a00e96f --- /dev/null +++ b/tests/baselines/reference/transformsElideNullUndefinedType.types @@ -0,0 +1,178 @@ +=== tests/cases/compiler/transformsElideNullUndefinedType.ts === + +var v0: null; +>v0 : null +>null : null + +var v1: undefined; +>v1 : undefined + +function f0(): null { return null; } +>f0 : () => null +>null : null +>null : null + +function f1(): undefined { return undefined; } +>f1 : () => undefined +>undefined : undefined + +var f2 = function (): null { return null; } +>f2 : () => null +>function (): null { return null; } : () => null +>null : null +>null : null + +var f3 = function (): undefined { return undefined; } +>f3 : () => undefined +>function (): undefined { return undefined; } : () => undefined +>undefined : undefined + +var f4 = (): null => null; +>f4 : () => null +>(): null => null : () => null +>null : null +>null : null + +var f5 = (): undefined => undefined; +>f5 : () => undefined +>(): undefined => undefined : () => undefined +>undefined : undefined + +function f6(p0: null) { } +>f6 : (p0: null) => void +>p0 : null +>null : null + +function f7(p1: undefined) { } +>f7 : (p1: undefined) => void +>p1 : undefined + +var f8 = function (p2: null) { } +>f8 : (p2: null) => void +>function (p2: null) { } : (p2: null) => void +>p2 : null +>null : null + +var f9 = function (p3: undefined) { } +>f9 : (p3: undefined) => void +>function (p3: undefined) { } : (p3: undefined) => void +>p3 : undefined + +var f10 = (p4: null) => { } +>f10 : (p4: null) => void +>(p4: null) => { } : (p4: null) => void +>p4 : null +>null : null + +var f11 = (p5: undefined) => { } +>f11 : (p5: undefined) => void +>(p5: undefined) => { } : (p5: undefined) => void +>p5 : undefined + +class C1 { +>C1 : C1 + + m0(): null { return null; } +>m0 : () => null +>null : null +>null : null + + m1(): undefined { return undefined; } +>m1 : () => undefined +>undefined : undefined + + m3(p6: null) { } +>m3 : (p6: null) => void +>p6 : null +>null : null + + m4(p7: undefined) { } +>m4 : (p7: undefined) => void +>p7 : undefined + + get a0(): null { return null; } +>a0 : null +>null : null +>null : null + + get a1(): undefined { return undefined; } +>a1 : undefined +>undefined : undefined + + set a2(p8: null) { } +>a2 : null +>p8 : null +>null : null + + set a3(p9: undefined) { } +>a3 : undefined +>p9 : undefined +} + +class C2 { constructor(p10: null) { } } +>C2 : C2 +>p10 : null +>null : null + +class C3 { constructor(p11: undefined) { } } +>C3 : C3 +>p11 : undefined + +class C4 { +>C4 : C4 + + f1; +>f1 : any + + constructor(p12: null) { } +>p12 : null +>null : null +} + +class C5 { +>C5 : C5 + + f2; +>f2 : any + + constructor(p13: undefined) { } +>p13 : undefined +} + +var C6 = class { constructor(p12: null) { } } +>C6 : typeof (Anonymous class) +>class { constructor(p12: null) { } } : typeof (Anonymous class) +>p12 : null +>null : null + +var C7 = class { constructor(p13: undefined) { } } +>C7 : typeof (Anonymous class) +>class { constructor(p13: undefined) { } } : typeof (Anonymous class) +>p13 : undefined + +declare function fn(); +>fn : () => any +>T : T + +fn(); +>fn() : any +>fn : () => any +>null : null + +fn(); +>fn() : any +>fn : () => any + +declare class D {} +>D : D +>T : T + +new D(); +>new D() : D +>D : typeof D +>null : null + +new D(); +>new D() : D +>D : typeof D + diff --git a/tests/baselines/reference/transpile/Report an error when compiler-options module-kind is out-of-range.js b/tests/baselines/reference/transpile/Report an error when compiler-options module-kind is out-of-range.js index c7570e811922c..1ceb1bcd1464c 100644 --- a/tests/baselines/reference/transpile/Report an error when compiler-options module-kind is out-of-range.js +++ b/tests/baselines/reference/transpile/Report an error when compiler-options module-kind is out-of-range.js @@ -1 +1,2 @@ +"use strict"; //# sourceMappingURL=file.js.map \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Report an error when compiler-options target-script is out-of-range.js b/tests/baselines/reference/transpile/Report an error when compiler-options target-script is out-of-range.js index c7570e811922c..1ceb1bcd1464c 100644 --- a/tests/baselines/reference/transpile/Report an error when compiler-options target-script is out-of-range.js +++ b/tests/baselines/reference/transpile/Report an error when compiler-options target-script is out-of-range.js @@ -1 +1,2 @@ +"use strict"; //# sourceMappingURL=file.js.map \ No newline at end of file diff --git a/tests/baselines/reference/transpile/Supports setting alwaysStrict.js b/tests/baselines/reference/transpile/Supports setting alwaysStrict.js new file mode 100644 index 0000000000000..8d91090453b8a --- /dev/null +++ b/tests/baselines/reference/transpile/Supports setting alwaysStrict.js @@ -0,0 +1,3 @@ +"use strict"; +x; +//# sourceMappingURL=input.js.map \ No newline at end of file diff --git a/tests/baselines/reference/typeAliasDeclarationEmit.js b/tests/baselines/reference/typeAliasDeclarationEmit.js index 6e82fe4b07da3..1f72e1d1b008f 100644 --- a/tests/baselines/reference/typeAliasDeclarationEmit.js +++ b/tests/baselines/reference/typeAliasDeclarationEmit.js @@ -6,6 +6,7 @@ export type CallbackArray = () => T; //// [typeAliasDeclarationEmit.js] define(["require", "exports"], function (require, exports) { + "use strict"; }); diff --git a/tests/baselines/reference/typeAliasDeclarationEmit2.js b/tests/baselines/reference/typeAliasDeclarationEmit2.js index 4bb1bd3efd9d7..b94eb56a2a288 100644 --- a/tests/baselines/reference/typeAliasDeclarationEmit2.js +++ b/tests/baselines/reference/typeAliasDeclarationEmit2.js @@ -4,6 +4,7 @@ export type A = { value: a }; //// [typeAliasDeclarationEmit2.js] define(["require", "exports"], function (require, exports) { + "use strict"; }); diff --git a/tests/baselines/reference/typeGuardFunctionErrors.js b/tests/baselines/reference/typeGuardFunctionErrors.js index 6f0880ad7f59a..244b0df3333db 100644 --- a/tests/baselines/reference/typeGuardFunctionErrors.js +++ b/tests/baselines/reference/typeGuardFunctionErrors.js @@ -171,7 +171,6 @@ var C = (function (_super) { function hasANonBooleanReturnStatement(x) { return ''; } -function hasTypeGuardTypeInsideTypeGuardType(x) { } is; A; { @@ -232,7 +231,6 @@ function b2(a, A) { if (a === void 0) { a = is; } } ; -function b3() { } is; A; { diff --git a/tests/baselines/reference/typedArrays.types b/tests/baselines/reference/typedArrays.types index 444d6a74e4303..cd2103f646122 100644 --- a/tests/baselines/reference/typedArrays.types +++ b/tests/baselines/reference/typedArrays.types @@ -1,7 +1,7 @@ === tests/cases/compiler/typedArrays.ts === function CreateTypedArrayTypes() { ->CreateTypedArrayTypes : () => any[] +>CreateTypedArrayTypes : () => (Int8ArrayConstructor | Uint8ArrayConstructor | Int16ArrayConstructor | Uint16ArrayConstructor | Int32ArrayConstructor | Uint32ArrayConstructor | Float32ArrayConstructor | Float64ArrayConstructor | Uint8ClampedArrayConstructor)[] var typedArrays = []; >typedArrays : any[] @@ -71,11 +71,11 @@ function CreateTypedArrayTypes() { >Uint8ClampedArray : Uint8ClampedArrayConstructor return typedArrays; ->typedArrays : any[] +>typedArrays : (Int8ArrayConstructor | Uint8ArrayConstructor | Int16ArrayConstructor | Uint16ArrayConstructor | Int32ArrayConstructor | Uint32ArrayConstructor | Float32ArrayConstructor | Float64ArrayConstructor | Uint8ClampedArrayConstructor)[] } function CreateTypedArrayInstancesFromLength(obj: number) { ->CreateTypedArrayInstancesFromLength : (obj: number) => any[] +>CreateTypedArrayInstancesFromLength : (obj: number) => (Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array)[] >obj : number var typedArrays = []; @@ -164,11 +164,11 @@ function CreateTypedArrayInstancesFromLength(obj: number) { >obj : number return typedArrays; ->typedArrays : any[] +>typedArrays : (Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array)[] } function CreateTypedArrayInstancesFromArray(obj: number[]) { ->CreateTypedArrayInstancesFromArray : (obj: number[]) => any[] +>CreateTypedArrayInstancesFromArray : (obj: number[]) => (Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array)[] >obj : number[] var typedArrays = []; @@ -257,11 +257,11 @@ function CreateTypedArrayInstancesFromArray(obj: number[]) { >obj : number[] return typedArrays; ->typedArrays : any[] +>typedArrays : (Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array)[] } function CreateIntegerTypedArraysFromArray2(obj:number[]) { ->CreateIntegerTypedArraysFromArray2 : (obj: number[]) => any[] +>CreateIntegerTypedArraysFromArray2 : (obj: number[]) => (Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array)[] >obj : number[] var typedArrays = []; @@ -368,11 +368,11 @@ function CreateIntegerTypedArraysFromArray2(obj:number[]) { >obj : number[] return typedArrays; ->typedArrays : any[] +>typedArrays : (Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array)[] } function CreateIntegerTypedArraysFromArrayLike(obj:ArrayLike) { ->CreateIntegerTypedArraysFromArrayLike : (obj: ArrayLike) => any[] +>CreateIntegerTypedArraysFromArrayLike : (obj: ArrayLike) => (Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array)[] >obj : ArrayLike >ArrayLike : ArrayLike @@ -480,11 +480,11 @@ function CreateIntegerTypedArraysFromArrayLike(obj:ArrayLike) { >obj : ArrayLike return typedArrays; ->typedArrays : any[] +>typedArrays : (Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array)[] } function CreateTypedArraysOf(obj) { ->CreateTypedArraysOf : (obj: any) => any[] +>CreateTypedArraysOf : (obj: any) => (Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array)[] >obj : any var typedArrays = []; @@ -600,11 +600,11 @@ function CreateTypedArraysOf(obj) { >obj : any return typedArrays; ->typedArrays : any[] +>typedArrays : (Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array)[] } function CreateTypedArraysOf2() { ->CreateTypedArraysOf2 : () => any[] +>CreateTypedArraysOf2 : () => (Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array)[] var typedArrays = []; >typedArrays : any[] @@ -737,11 +737,11 @@ function CreateTypedArraysOf2() { >4 : 4 return typedArrays; ->typedArrays : any[] +>typedArrays : (Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array)[] } function CreateTypedArraysFromMapFn(obj:ArrayLike, mapFn: (n:number, v:number)=> number) { ->CreateTypedArraysFromMapFn : (obj: ArrayLike, mapFn: (n: number, v: number) => number) => any[] +>CreateTypedArraysFromMapFn : (obj: ArrayLike, mapFn: (n: number, v: number) => number) => (Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array)[] >obj : ArrayLike >ArrayLike : ArrayLike >mapFn : (n: number, v: number) => number @@ -861,11 +861,11 @@ function CreateTypedArraysFromMapFn(obj:ArrayLike, mapFn: (n:number, v:n >mapFn : (n: number, v: number) => number return typedArrays; ->typedArrays : any[] +>typedArrays : (Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array)[] } function CreateTypedArraysFromThisObj(obj:ArrayLike, mapFn: (n:number, v:number)=> number, thisArg: {}) { ->CreateTypedArraysFromThisObj : (obj: ArrayLike, mapFn: (n: number, v: number) => number, thisArg: {}) => any[] +>CreateTypedArraysFromThisObj : (obj: ArrayLike, mapFn: (n: number, v: number) => number, thisArg: {}) => (Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array)[] >obj : ArrayLike >ArrayLike : ArrayLike >mapFn : (n: number, v: number) => number @@ -995,5 +995,5 @@ function CreateTypedArraysFromThisObj(obj:ArrayLike, mapFn: (n:number, v >thisArg : {} return typedArrays; ->typedArrays : any[] +>typedArrays : (Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array)[] } diff --git a/tests/baselines/reference/umd5.errors.txt b/tests/baselines/reference/umd5.errors.txt index 9628ad738db86..5595de8de29fb 100644 --- a/tests/baselines/reference/umd5.errors.txt +++ b/tests/baselines/reference/umd5.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/externalModules/a.ts(6,9): error TS2686: Identifier 'Foo' must be imported from a module +tests/cases/conformance/externalModules/a.ts(6,9): error TS2686: 'Foo' refers to a UMD global, but the current file is a module. Consider adding an import instead. ==== tests/cases/conformance/externalModules/a.ts (1 errors) ==== @@ -9,7 +9,7 @@ tests/cases/conformance/externalModules/a.ts(6,9): error TS2686: Identifier 'Foo // should error let z = Foo; ~~~ -!!! error TS2686: Identifier 'Foo' must be imported from a module +!!! error TS2686: 'Foo' refers to a UMD global, but the current file is a module. Consider adding an import instead. ==== tests/cases/conformance/externalModules/foo.d.ts (0 errors) ==== diff --git a/tests/baselines/reference/umd8.errors.txt b/tests/baselines/reference/umd8.errors.txt index 9396c8c5450bb..26c8adef56143 100644 --- a/tests/baselines/reference/umd8.errors.txt +++ b/tests/baselines/reference/umd8.errors.txt @@ -1,4 +1,4 @@ -tests/cases/conformance/externalModules/a.ts(7,14): error TS2686: Identifier 'Foo' must be imported from a module +tests/cases/conformance/externalModules/a.ts(7,14): error TS2686: 'Foo' refers to a UMD global, but the current file is a module. Consider adding an import instead. ==== tests/cases/conformance/externalModules/a.ts (1 errors) ==== @@ -10,7 +10,7 @@ tests/cases/conformance/externalModules/a.ts(7,14): error TS2686: Identifier 'Fo let z: Foo.SubThing; // OK in ns position let x: any = Foo; // Not OK in value position ~~~ -!!! error TS2686: Identifier 'Foo' must be imported from a module +!!! error TS2686: 'Foo' refers to a UMD global, but the current file is a module. Consider adding an import instead. ==== tests/cases/conformance/externalModules/foo.d.ts (0 errors) ==== diff --git a/tests/cases/compiler/APISample_linter.ts b/tests/cases/compiler/APISample_linter.ts index 89f0be7a0f553..dc5e2644b4ff1 100644 --- a/tests/cases/compiler/APISample_linter.ts +++ b/tests/cases/compiler/APISample_linter.ts @@ -61,7 +61,7 @@ export function delint(sourceFile: ts.SourceFile) { const fileNames: string[] = process.argv.slice(2); fileNames.forEach(fileName => { // Parse a file - let sourceFile = ts.createSourceFile(fileName, readFileSync(fileName).toString(), ts.ScriptTarget.ES6, /*setParentNodes */ true); + let sourceFile = ts.createSourceFile(fileName, readFileSync(fileName).toString(), ts.ScriptTarget.ES2015, /*setParentNodes */ true); // delint it delint(sourceFile); diff --git a/tests/cases/compiler/alwaysStrict.ts b/tests/cases/compiler/alwaysStrict.ts new file mode 100644 index 0000000000000..22ec09c6e3827 --- /dev/null +++ b/tests/cases/compiler/alwaysStrict.ts @@ -0,0 +1,5 @@ +// @alwaysStrict: true + +function f() { + var arguments = []; +} \ No newline at end of file diff --git a/tests/cases/compiler/alwaysStrictAlreadyUseStrict.ts b/tests/cases/compiler/alwaysStrictAlreadyUseStrict.ts new file mode 100644 index 0000000000000..1d804a6cf9038 --- /dev/null +++ b/tests/cases/compiler/alwaysStrictAlreadyUseStrict.ts @@ -0,0 +1,5 @@ +// @alwaysStrict: true +"use strict" +function f() { + var a = []; +} \ No newline at end of file diff --git a/tests/cases/compiler/alwaysStrictES6.ts b/tests/cases/compiler/alwaysStrictES6.ts new file mode 100644 index 0000000000000..edb542fa86a3c --- /dev/null +++ b/tests/cases/compiler/alwaysStrictES6.ts @@ -0,0 +1,6 @@ +// @target: ES6 +// @alwaysStrict: true + +function f() { + var arguments = []; +} \ No newline at end of file diff --git a/tests/cases/compiler/alwaysStrictModule.ts b/tests/cases/compiler/alwaysStrictModule.ts new file mode 100644 index 0000000000000..b706f5869a6d3 --- /dev/null +++ b/tests/cases/compiler/alwaysStrictModule.ts @@ -0,0 +1,8 @@ +// @module: commonjs +// @alwaysStrict: true + +module M { + export function f() { + var arguments = []; + } +} \ No newline at end of file diff --git a/tests/cases/compiler/alwaysStrictModule2.ts b/tests/cases/compiler/alwaysStrictModule2.ts new file mode 100644 index 0000000000000..6afecb0e77013 --- /dev/null +++ b/tests/cases/compiler/alwaysStrictModule2.ts @@ -0,0 +1,16 @@ +// @alwaysStrict: true +// @outFile: out.js + +// @fileName: a.ts +module M { + export function f() { + var arguments = []; + } +} + +// @fileName: b.ts +module M { + export function f2() { + var arguments = []; + } +} \ No newline at end of file diff --git a/tests/cases/compiler/alwaysStrictNoImplicitUseStrict.ts b/tests/cases/compiler/alwaysStrictNoImplicitUseStrict.ts new file mode 100644 index 0000000000000..d3173df8ab50c --- /dev/null +++ b/tests/cases/compiler/alwaysStrictNoImplicitUseStrict.ts @@ -0,0 +1,9 @@ +// @module: commonjs +// @alwaysStrict: true +// @noImplicitUseStrict: true + +module M { + export function f() { + var arguments = []; + } +} \ No newline at end of file diff --git a/tests/cases/compiler/controlFlowArrayErrors.ts b/tests/cases/compiler/controlFlowArrayErrors.ts new file mode 100644 index 0000000000000..08107ff365994 --- /dev/null +++ b/tests/cases/compiler/controlFlowArrayErrors.ts @@ -0,0 +1,67 @@ +// @noImplicitAny: true + +declare function cond(): boolean; + +function f1() { + let x = []; // Implicit any[] error in some locations + let y = x; // Implicit any[] error + x.push(5); + let z = x; +} + +function f2() { + let x; // Implicit any[] error in some locations + x = []; + let y = x; // Implicit any[] error + x.push(5); + let z = x; +} + +function f3() { + let x = []; // Implicit any[] error in some locations + x.push(5); + function g() { + x; // Implicit any[] error + } +} + +function f4() { + let x; + x = [5, "hello"]; // Non-evolving array + x.push(true); // Error +} + +function f5() { + let x = [5, "hello"]; // Non-evolving array + x.push(true); // Error +} + +function f6() { + let x; + if (cond()) { + x = []; + x.push(5); + x.push("hello"); + } + else { + x = [true]; // Non-evolving array + } + x; // boolean[] | (string | number)[] + x.push(99); // Error +} + +function f7() { + let x = []; // x has evolving array value + x.push(5); + let y = x; // y has non-evolving array value + x.push("hello"); // Ok + y.push("hello"); // Error +} + +function f8() { + const x = []; // Implicit any[] error in some locations + x.push(5); + function g() { + x; // Implicit any[] error + } +} \ No newline at end of file diff --git a/tests/cases/compiler/controlFlowArrays.ts b/tests/cases/compiler/controlFlowArrays.ts new file mode 100644 index 0000000000000..646c85f069f89 --- /dev/null +++ b/tests/cases/compiler/controlFlowArrays.ts @@ -0,0 +1,181 @@ +// @strictNullChecks: true +// @noImplicitAny: true + +declare function cond(): boolean; + +function f1() { + let x = []; + x[0] = 5; + x[1] = "hello"; + x[2] = true; + return x; // (string | number | boolean)[] +} + +function f2() { + let x = []; + x.push(5); + x.push("hello"); + x.push(true); + return x; // (string | number | boolean)[] +} + +function f3() { + let x; + x = []; + x.push(5, "hello"); + return x; // (string | number)[] +} + +function f4() { + let x = []; + if (cond()) { + x.push(5); + } + else { + x.push("hello"); + } + return x; // (string | number)[] +} + +function f5() { + let x; + if (cond()) { + x = []; + x.push(5); + } + else { + x = []; + x.push("hello"); + } + return x; // (string | number)[] +} + +function f6() { + let x; + if (cond()) { + x = 5; + } + else { + x = []; + x.push("hello"); + } + return x; // number | string[] +} + +function f7() { + let x = null; + if (cond()) { + x = []; + while (cond()) { + x.push("hello"); + } + } + return x; // string[] | null +} + +function f8() { + let x = []; + x.push(5); + if (cond()) return x; // number[] + x.push("hello"); + if (cond()) return x; // (string | number)[] + x.push(true); + return x; // (string | number | boolean)[] +} + +function f9() { + let x = []; + if (cond()) { + x.push(5); + return x; // number[] + } + else { + x.push("hello"); + return x; // string[] + } +} + +function f10() { + let x = []; + if (cond()) { + x.push(true); + x; // boolean[] + } + else { + x.push(5); + x; // number[] + while (cond()) { + x.push("hello"); + } + x; // (string | number)[] + } + x.push(99); + return x; // (string | number | boolean)[] +} + +function f11() { + let x = []; + if (x.length === 0) { // x.length ok on implicit any[] + x.push("hello"); + } + return x; +} + +function f12() { + let x; + x = []; + if (x.length === 0) { // x.length ok on implicit any[] + x.push("hello"); + } + return x; +} + +function f13() { + var x = []; + x.push(5); + x.push("hello"); + x.push(true); + return x; // (string | number | boolean)[] +} + +function f14() { + const x = []; + x.push(5); + x.push("hello"); + x.push(true); + return x; // (string | number | boolean)[] +} + +function f15() { + let x = []; + while (cond()) { + while (cond()) {} + x.push("hello"); + } + return x; // string[] +} + +function f16() { + let x; + let y; + (x = [], x).push(5); + (x.push("hello"), x).push(true); + ((x))[3] = { a: 1 }; + return x; // (string | number | boolean | { a: number })[] +} + +function f17() { + let x = []; + x.unshift(5); + x.unshift("hello"); + x.unshift(true); + return x; // (string | number | boolean)[] +} + +function f18() { + let x = []; + x.push(5); + x.unshift("hello"); + x[2] = true; + return x; // (string | number | boolean)[] +} \ No newline at end of file diff --git a/tests/cases/compiler/es2017basicAsync.ts b/tests/cases/compiler/es2017basicAsync.ts new file mode 100644 index 0000000000000..b08ee6a18ae55 --- /dev/null +++ b/tests/cases/compiler/es2017basicAsync.ts @@ -0,0 +1,49 @@ +// @target: es2017 +// @lib: es2017 +// @noEmitHelpers: true + +async (): Promise => { + await 0; +} + +async function asyncFunc() { + await 0; +} + +const asyncArrowFunc = async (): Promise => { + await 0; +} + +async function asyncIIFE() { + await 0; + + await (async function(): Promise { + await 1; + })(); + + await (async function asyncNamedFunc(): Promise { + await 1; + })(); + + await (async (): Promise => { + await 1; + })(); +} + +class AsyncClass { + asyncPropFunc = async function(): Promise { + await 2; + } + + asyncPropNamedFunc = async function namedFunc(): Promise { + await 2; + } + + asyncPropArrowFunc = async (): Promise => { + await 2; + } + + async asyncMethod(): Promise { + await 2; + } +} diff --git a/tests/cases/compiler/implicitAnyWidenToAny.ts b/tests/cases/compiler/implicitAnyWidenToAny.ts index b4f4b5eb3124c..dcbabd38b47e2 100644 --- a/tests/cases/compiler/implicitAnyWidenToAny.ts +++ b/tests/cases/compiler/implicitAnyWidenToAny.ts @@ -3,7 +3,7 @@ var x = null; // error at "x" var x1 = undefined; // error at "x1" var widenArray = [null, undefined]; // error at "widenArray" -var emptyArray = []; // error at "emptyArray" +var emptyArray = []; // these should not be error class AnimalObj { diff --git a/tests/cases/compiler/narrowedConstInMethod.ts b/tests/cases/compiler/narrowedConstInMethod.ts new file mode 100644 index 0000000000000..3ddf97a8df332 --- /dev/null +++ b/tests/cases/compiler/narrowedConstInMethod.ts @@ -0,0 +1,19 @@ +// @strictNullChecks: true + +function f() { + const x: string | null = {}; + if (x !== null) { + return { + bar() { return x.length; } // Error: possibly null x + }; + } +} + +function f2() { + const x: string | null = {}; + if (x !== null) { + return class { + bar() { return x.length; } // Error: possibly null x + }; + } +} \ No newline at end of file diff --git a/tests/cases/compiler/transformsElideNullUndefinedType.ts b/tests/cases/compiler/transformsElideNullUndefinedType.ts new file mode 100644 index 0000000000000..4a493a91d8824 --- /dev/null +++ b/tests/cases/compiler/transformsElideNullUndefinedType.ts @@ -0,0 +1,60 @@ +// @target: es6 + +var v0: null; +var v1: undefined; + +function f0(): null { return null; } +function f1(): undefined { return undefined; } + +var f2 = function (): null { return null; } +var f3 = function (): undefined { return undefined; } + +var f4 = (): null => null; +var f5 = (): undefined => undefined; + +function f6(p0: null) { } +function f7(p1: undefined) { } + +var f8 = function (p2: null) { } +var f9 = function (p3: undefined) { } + +var f10 = (p4: null) => { } +var f11 = (p5: undefined) => { } + +class C1 { + m0(): null { return null; } + m1(): undefined { return undefined; } + + m3(p6: null) { } + m4(p7: undefined) { } + + get a0(): null { return null; } + get a1(): undefined { return undefined; } + + set a2(p8: null) { } + set a3(p9: undefined) { } +} + +class C2 { constructor(p10: null) { } } +class C3 { constructor(p11: undefined) { } } + +class C4 { + f1; + constructor(p12: null) { } +} + +class C5 { + f2; + constructor(p13: undefined) { } +} + +var C6 = class { constructor(p12: null) { } } +var C7 = class { constructor(p13: undefined) { } } + +declare function fn(); +fn(); +fn(); + +declare class D {} +new D(); +new D(); \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/asyncArrowFunction/arrowFunctionWithParameterNameAsync_es2017.ts b/tests/cases/conformance/async/es2017/asyncArrowFunction/arrowFunctionWithParameterNameAsync_es2017.ts new file mode 100644 index 0000000000000..1ade02c09d2e8 --- /dev/null +++ b/tests/cases/conformance/async/es2017/asyncArrowFunction/arrowFunctionWithParameterNameAsync_es2017.ts @@ -0,0 +1,4 @@ +// @target: ES5 +// @noEmitHelpers: true + +const x = async => async; \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction10_es2017.ts b/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction10_es2017.ts new file mode 100644 index 0000000000000..09d97cc253be7 --- /dev/null +++ b/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction10_es2017.ts @@ -0,0 +1,7 @@ +// @target: es2017 +// @noEmitHelpers: true + +var foo = async (): Promise => { + // Legal to use 'await' in a type context. + var v: await; +} diff --git a/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction1_es2017.ts b/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction1_es2017.ts new file mode 100644 index 0000000000000..0f9ec479a3163 --- /dev/null +++ b/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction1_es2017.ts @@ -0,0 +1,5 @@ +// @target: es2017 +// @noEmitHelpers: true + +var foo = async (): Promise => { +}; \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction2_es2017.ts b/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction2_es2017.ts new file mode 100644 index 0000000000000..b11441372ffdc --- /dev/null +++ b/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction2_es2017.ts @@ -0,0 +1,4 @@ +// @target: es2017 +// @noEmitHelpers: true +var f = (await) => { +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction3_es2017.ts b/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction3_es2017.ts new file mode 100644 index 0000000000000..9b5924fc1455f --- /dev/null +++ b/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction3_es2017.ts @@ -0,0 +1,4 @@ +// @target: es2017 +// @noEmitHelpers: true +function f(await = await) { +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction4_es2017.ts b/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction4_es2017.ts new file mode 100644 index 0000000000000..5a9882778365f --- /dev/null +++ b/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction4_es2017.ts @@ -0,0 +1,4 @@ +// @target: es2017 +// @noEmitHelpers: true +var await = () => { +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts b/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts new file mode 100644 index 0000000000000..aabe5a99372ad --- /dev/null +++ b/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts @@ -0,0 +1,5 @@ +// @target: es2017 +// @noEmitHelpers: true + +var foo = async (await): Promise => { +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction6_es2017.ts b/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction6_es2017.ts new file mode 100644 index 0000000000000..d82ce86442107 --- /dev/null +++ b/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction6_es2017.ts @@ -0,0 +1,5 @@ +// @target: es2017 +// @noEmitHelpers: true + +var foo = async (a = await): Promise => { +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction7_es2017.ts b/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction7_es2017.ts new file mode 100644 index 0000000000000..bc49c5995e6a5 --- /dev/null +++ b/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction7_es2017.ts @@ -0,0 +1,8 @@ +// @target: es2017 +// @noEmitHelpers: true + +var bar = async (): Promise => { + // 'await' here is an identifier, and not an await expression. + var foo = async (a = await): Promise => { + } +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction8_es2017.ts b/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction8_es2017.ts new file mode 100644 index 0000000000000..387b09c791a47 --- /dev/null +++ b/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction8_es2017.ts @@ -0,0 +1,6 @@ +// @target: es2017 +// @noEmitHelpers: true + +var foo = async (): Promise => { + var v = { [await]: foo } +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction9_es2017.ts b/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction9_es2017.ts new file mode 100644 index 0000000000000..93254fee5c746 --- /dev/null +++ b/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction9_es2017.ts @@ -0,0 +1,4 @@ +// @target: es2017 +// @noEmitHelpers: true +var foo = async (a = await => await): Promise => { +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunctionCapturesArguments_es2017.ts b/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunctionCapturesArguments_es2017.ts new file mode 100644 index 0000000000000..c71c6ddecb0a8 --- /dev/null +++ b/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunctionCapturesArguments_es2017.ts @@ -0,0 +1,8 @@ +// @target: es2017 +// @noEmitHelpers: true +class C { + method() { + function other() {} + var fn = async () => await other.apply(this, arguments); + } +} diff --git a/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunctionCapturesThis_es2017.ts b/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunctionCapturesThis_es2017.ts new file mode 100644 index 0000000000000..3f398d9be82c1 --- /dev/null +++ b/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunctionCapturesThis_es2017.ts @@ -0,0 +1,7 @@ +// @target: es2017 +// @noEmitHelpers: true +class C { + method() { + var fn = async () => await this; + } +} diff --git a/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncUnParenthesizedArrowFunction_es2017.ts b/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncUnParenthesizedArrowFunction_es2017.ts new file mode 100644 index 0000000000000..9b63b7bd468c3 --- /dev/null +++ b/tests/cases/conformance/async/es2017/asyncArrowFunction/asyncUnParenthesizedArrowFunction_es2017.ts @@ -0,0 +1,6 @@ +// @target: es2017 +// @noEmitHelpers: true + +declare function someOtherFunction(i: any): Promise; +const x = async i => await someOtherFunction(i) +const x1 = async (i) => await someOtherFunction(i); \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/asyncAwaitIsolatedModules_es2017.ts b/tests/cases/conformance/async/es2017/asyncAwaitIsolatedModules_es2017.ts new file mode 100644 index 0000000000000..c0ab4838f70f2 --- /dev/null +++ b/tests/cases/conformance/async/es2017/asyncAwaitIsolatedModules_es2017.ts @@ -0,0 +1,41 @@ +// @target: es2017 +// @isolatedModules: true +import { MyPromise } from "missing"; + +declare var p: Promise; +declare var mp: MyPromise; + +async function f0() { } +async function f1(): Promise { } +async function f3(): MyPromise { } + +let f4 = async function() { } +let f5 = async function(): Promise { } +let f6 = async function(): MyPromise { } + +let f7 = async () => { }; +let f8 = async (): Promise => { }; +let f9 = async (): MyPromise => { }; +let f10 = async () => p; +let f11 = async () => mp; +let f12 = async (): Promise => mp; +let f13 = async (): MyPromise => p; + +let o = { + async m1() { }, + async m2(): Promise { }, + async m3(): MyPromise { } +}; + +class C { + async m1() { } + async m2(): Promise { } + async m3(): MyPromise { } + static async m4() { } + static async m5(): Promise { } + static async m6(): MyPromise { } +} + +module M { + export async function f1() { } +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/asyncAwait_es2017.ts b/tests/cases/conformance/async/es2017/asyncAwait_es2017.ts new file mode 100644 index 0000000000000..a255cb7cb710c --- /dev/null +++ b/tests/cases/conformance/async/es2017/asyncAwait_es2017.ts @@ -0,0 +1,40 @@ +// @target: es2017 +type MyPromise = Promise; +declare var MyPromise: typeof Promise; +declare var p: Promise; +declare var mp: MyPromise; + +async function f0() { } +async function f1(): Promise { } +async function f3(): MyPromise { } + +let f4 = async function() { } +let f5 = async function(): Promise { } +let f6 = async function(): MyPromise { } + +let f7 = async () => { }; +let f8 = async (): Promise => { }; +let f9 = async (): MyPromise => { }; +let f10 = async () => p; +let f11 = async () => mp; +let f12 = async (): Promise => mp; +let f13 = async (): MyPromise => p; + +let o = { + async m1() { }, + async m2(): Promise { }, + async m3(): MyPromise { } +}; + +class C { + async m1() { } + async m2(): Promise { } + async m3(): MyPromise { } + static async m4() { } + static async m5(): Promise { } + static async m6(): MyPromise { } +} + +module M { + export async function f1() { } +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/asyncMethodWithSuper_es2017.ts b/tests/cases/conformance/async/es2017/asyncMethodWithSuper_es2017.ts new file mode 100644 index 0000000000000..b9005c4859657 --- /dev/null +++ b/tests/cases/conformance/async/es2017/asyncMethodWithSuper_es2017.ts @@ -0,0 +1,52 @@ +// @target: es2017 +// @noEmitHelpers: true +class A { + x() { + } +} + +class B extends A { + // async method with only call/get on 'super' does not require a binding + async simple() { + // call with property access + super.x(); + + // call with element access + super["x"](); + + // property access (read) + const a = super.x; + + // element access (read) + const b = super["x"]; + } + + // async method with assignment/destructuring on 'super' requires a binding + async advanced() { + const f = () => {}; + + // call with property access + super.x(); + + // call with element access + super["x"](); + + // property access (read) + const a = super.x; + + // element access (read) + const b = super["x"]; + + // property access (assign) + super.x = f; + + // element access (assign) + super["x"] = f; + + // destructuring assign with property access + ({ f: super.x } = { f }); + + // destructuring assign with element access + ({ f: super["x"] } = { f }); + } +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/asyncUseStrict_es2017.ts b/tests/cases/conformance/async/es2017/asyncUseStrict_es2017.ts new file mode 100644 index 0000000000000..c4d4cafef8a13 --- /dev/null +++ b/tests/cases/conformance/async/es2017/asyncUseStrict_es2017.ts @@ -0,0 +1,8 @@ +// @target: es2017 +// @noEmitHelpers: true +declare var a: boolean; +declare var p: Promise; +async function func(): Promise { + "use strict"; + var b = await p || a; +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/awaitBinaryExpression/awaitBinaryExpression1_es2017.ts b/tests/cases/conformance/async/es2017/awaitBinaryExpression/awaitBinaryExpression1_es2017.ts new file mode 100644 index 0000000000000..ef9076a8bee13 --- /dev/null +++ b/tests/cases/conformance/async/es2017/awaitBinaryExpression/awaitBinaryExpression1_es2017.ts @@ -0,0 +1,11 @@ +// @target: es2017 +// @noEmitHelpers: true +declare var a: boolean; +declare var p: Promise; +declare function before(): void; +declare function after(): void; +async function func(): Promise { + before(); + var b = await p || a; + after(); +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/awaitBinaryExpression/awaitBinaryExpression2_es2017.ts b/tests/cases/conformance/async/es2017/awaitBinaryExpression/awaitBinaryExpression2_es2017.ts new file mode 100644 index 0000000000000..84d2f6222925d --- /dev/null +++ b/tests/cases/conformance/async/es2017/awaitBinaryExpression/awaitBinaryExpression2_es2017.ts @@ -0,0 +1,11 @@ +// @target: es2017 +// @noEmitHelpers: true +declare var a: boolean; +declare var p: Promise; +declare function before(): void; +declare function after(): void; +async function func(): Promise { + before(); + var b = await p && a; + after(); +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/awaitBinaryExpression/awaitBinaryExpression3_es2017.ts b/tests/cases/conformance/async/es2017/awaitBinaryExpression/awaitBinaryExpression3_es2017.ts new file mode 100644 index 0000000000000..1f3a8245d42de --- /dev/null +++ b/tests/cases/conformance/async/es2017/awaitBinaryExpression/awaitBinaryExpression3_es2017.ts @@ -0,0 +1,11 @@ +// @target: es2017 +// @noEmitHelpers: true +declare var a: number; +declare var p: Promise; +declare function before(): void; +declare function after(): void; +async function func(): Promise { + before(); + var b = await p + a; + after(); +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/awaitBinaryExpression/awaitBinaryExpression4_es2017.ts b/tests/cases/conformance/async/es2017/awaitBinaryExpression/awaitBinaryExpression4_es2017.ts new file mode 100644 index 0000000000000..6c36f1e4ce5b5 --- /dev/null +++ b/tests/cases/conformance/async/es2017/awaitBinaryExpression/awaitBinaryExpression4_es2017.ts @@ -0,0 +1,11 @@ +// @target: es2017 +// @noEmitHelpers: true +declare var a: boolean; +declare var p: Promise; +declare function before(): void; +declare function after(): void; +async function func(): Promise { + before(); + var b = (await p, a); + after(); +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/awaitBinaryExpression/awaitBinaryExpression5_es2017.ts b/tests/cases/conformance/async/es2017/awaitBinaryExpression/awaitBinaryExpression5_es2017.ts new file mode 100644 index 0000000000000..abbcbb95ba923 --- /dev/null +++ b/tests/cases/conformance/async/es2017/awaitBinaryExpression/awaitBinaryExpression5_es2017.ts @@ -0,0 +1,12 @@ +// @target: es2017 +// @noEmitHelpers: true +declare var a: boolean; +declare var p: Promise; +declare function before(): void; +declare function after(): void; +async function func(): Promise { + before(); + var o: { a: boolean; }; + o.a = await p; + after(); +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression1_es2017.ts b/tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression1_es2017.ts new file mode 100644 index 0000000000000..da46e04ed1e8e --- /dev/null +++ b/tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression1_es2017.ts @@ -0,0 +1,15 @@ +// @target: es2017 +// @noEmitHelpers: true +declare var a: boolean; +declare var p: Promise; +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare function before(): void; +declare function after(): void; +async function func(): Promise { + before(); + var b = fn(a, a, a); + after(); +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression2_es2017.ts b/tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression2_es2017.ts new file mode 100644 index 0000000000000..baf75ab95c3d3 --- /dev/null +++ b/tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression2_es2017.ts @@ -0,0 +1,15 @@ +// @target: es2017 +// @noEmitHelpers: true +declare var a: boolean; +declare var p: Promise; +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare function before(): void; +declare function after(): void; +async function func(): Promise { + before(); + var b = fn(await p, a, a); + after(); +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression3_es2017.ts b/tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression3_es2017.ts new file mode 100644 index 0000000000000..17cf6d8c6c9bb --- /dev/null +++ b/tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression3_es2017.ts @@ -0,0 +1,15 @@ +// @target: es2017 +// @noEmitHelpers: true +declare var a: boolean; +declare var p: Promise; +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare function before(): void; +declare function after(): void; +async function func(): Promise { + before(); + var b = fn(a, await p, a); + after(); +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression4_es2017.ts b/tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression4_es2017.ts new file mode 100644 index 0000000000000..ef5e1d203d980 --- /dev/null +++ b/tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression4_es2017.ts @@ -0,0 +1,15 @@ +// @target: es2017 +// @noEmitHelpers: true +declare var a: boolean; +declare var p: Promise; +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare function before(): void; +declare function after(): void; +async function func(): Promise { + before(); + var b = (await pfn)(a, a, a); + after(); +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression5_es2017.ts b/tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression5_es2017.ts new file mode 100644 index 0000000000000..8494bc11b4cd8 --- /dev/null +++ b/tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression5_es2017.ts @@ -0,0 +1,15 @@ +// @target: es2017 +// @noEmitHelpers: true +declare var a: boolean; +declare var p: Promise; +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare function before(): void; +declare function after(): void; +async function func(): Promise { + before(); + var b = o.fn(a, a, a); + after(); +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression6_es2017.ts b/tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression6_es2017.ts new file mode 100644 index 0000000000000..7939572baea5a --- /dev/null +++ b/tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression6_es2017.ts @@ -0,0 +1,15 @@ +// @target: es2017 +// @noEmitHelpers: true +declare var a: boolean; +declare var p: Promise; +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare function before(): void; +declare function after(): void; +async function func(): Promise { + before(); + var b = o.fn(await p, a, a); + after(); +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression7_es2017.ts b/tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression7_es2017.ts new file mode 100644 index 0000000000000..3f1231a50d6b4 --- /dev/null +++ b/tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression7_es2017.ts @@ -0,0 +1,15 @@ +// @target: es2017 +// @noEmitHelpers: true +declare var a: boolean; +declare var p: Promise; +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare function before(): void; +declare function after(): void; +async function func(): Promise { + before(); + var b = o.fn(a, await p, a); + after(); +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression8_es2017.ts b/tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression8_es2017.ts new file mode 100644 index 0000000000000..c669122bada3b --- /dev/null +++ b/tests/cases/conformance/async/es2017/awaitCallExpression/awaitCallExpression8_es2017.ts @@ -0,0 +1,15 @@ +// @target: es2017 +// @noEmitHelpers: true +declare var a: boolean; +declare var p: Promise; +declare function fn(arg0: boolean, arg1: boolean, arg2: boolean): void; +declare var o: { fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }; +declare var pfn: Promise<{ (arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare var po: Promise<{ fn(arg0: boolean, arg1: boolean, arg2: boolean): void; }>; +declare function before(): void; +declare function after(): void; +async function func(): Promise { + before(); + var b = (await po).fn(a, a, a); + after(); +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/awaitClassExpression_es2017.ts b/tests/cases/conformance/async/es2017/awaitClassExpression_es2017.ts new file mode 100644 index 0000000000000..fe34d53a03cf1 --- /dev/null +++ b/tests/cases/conformance/async/es2017/awaitClassExpression_es2017.ts @@ -0,0 +1,9 @@ +// @target: es2017 +// @noEmitHelpers: true +declare class C { } +declare var p: Promise; + +async function func(): Promise { + class D extends (await p) { + } +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/await_unaryExpression_es2017.ts b/tests/cases/conformance/async/es2017/await_unaryExpression_es2017.ts new file mode 100644 index 0000000000000..dee2554653d29 --- /dev/null +++ b/tests/cases/conformance/async/es2017/await_unaryExpression_es2017.ts @@ -0,0 +1,17 @@ +// @target: es2017 + +async function bar() { + !await 42; // OK +} + +async function bar1() { + +await 42; // OK +} + +async function bar3() { + -await 42; // OK +} + +async function bar4() { + ~await 42; // OK +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/await_unaryExpression_es2017_1.ts b/tests/cases/conformance/async/es2017/await_unaryExpression_es2017_1.ts new file mode 100644 index 0000000000000..f38260b332d70 --- /dev/null +++ b/tests/cases/conformance/async/es2017/await_unaryExpression_es2017_1.ts @@ -0,0 +1,21 @@ +// @target: es2017 + +async function bar() { + !await 42; // OK +} + +async function bar1() { + delete await 42; // OK +} + +async function bar2() { + delete await 42; // OK +} + +async function bar3() { + void await 42; +} + +async function bar4() { + +await 42; +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/await_unaryExpression_es2017_2.ts b/tests/cases/conformance/async/es2017/await_unaryExpression_es2017_2.ts new file mode 100644 index 0000000000000..b48c661f0b93a --- /dev/null +++ b/tests/cases/conformance/async/es2017/await_unaryExpression_es2017_2.ts @@ -0,0 +1,13 @@ +// @target: es2017 + +async function bar1() { + delete await 42; +} + +async function bar2() { + delete await 42; +} + +async function bar3() { + void await 42; +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/await_unaryExpression_es2017_3.ts b/tests/cases/conformance/async/es2017/await_unaryExpression_es2017_3.ts new file mode 100644 index 0000000000000..d2c608d0e7b32 --- /dev/null +++ b/tests/cases/conformance/async/es2017/await_unaryExpression_es2017_3.ts @@ -0,0 +1,19 @@ +// @target: es2017 + +async function bar1() { + ++await 42; // Error +} + +async function bar2() { + --await 42; // Error +} + +async function bar3() { + var x = 42; + await x++; // OK but shouldn't need parenthesis +} + +async function bar4() { + var x = 42; + await x--; // OK but shouldn't need parenthesis +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration10_es2017.ts b/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration10_es2017.ts new file mode 100644 index 0000000000000..9b744713945d0 --- /dev/null +++ b/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration10_es2017.ts @@ -0,0 +1,4 @@ +// @target: es2017 +// @noEmitHelpers: true +async function foo(a = await => await): Promise { +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration11_es2017.ts b/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration11_es2017.ts new file mode 100644 index 0000000000000..9613e86616374 --- /dev/null +++ b/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration11_es2017.ts @@ -0,0 +1,4 @@ +// @target: es2017 +// @noEmitHelpers: true +async function await(): Promise { +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration12_es2017.ts b/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration12_es2017.ts new file mode 100644 index 0000000000000..02b6e833c2c2e --- /dev/null +++ b/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration12_es2017.ts @@ -0,0 +1,3 @@ +// @target: es2017 +// @noEmitHelpers: true +var v = async function await(): Promise { } \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration13_es2017.ts b/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration13_es2017.ts new file mode 100644 index 0000000000000..40177c1fbcb57 --- /dev/null +++ b/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration13_es2017.ts @@ -0,0 +1,6 @@ +// @target: es2017 +// @noEmitHelpers: true +async function foo(): Promise { + // Legal to use 'await' in a type context. + var v: await; +} diff --git a/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration14_es2017.ts b/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration14_es2017.ts new file mode 100644 index 0000000000000..06bd2de3eef0b --- /dev/null +++ b/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration14_es2017.ts @@ -0,0 +1,5 @@ +// @target: es2017 +// @noEmitHelpers: true +async function foo(): Promise { + return; +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration1_es2017.ts b/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration1_es2017.ts new file mode 100644 index 0000000000000..178611ad91b66 --- /dev/null +++ b/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration1_es2017.ts @@ -0,0 +1,4 @@ +// @target: es2017 +// @noEmitHelpers: true +async function foo(): Promise { +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration2_es2017.ts b/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration2_es2017.ts new file mode 100644 index 0000000000000..08711a4cfbd8e --- /dev/null +++ b/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration2_es2017.ts @@ -0,0 +1,4 @@ +// @target: es2017 +// @noEmitHelpers: true +function f(await) { +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration3_es2017.ts b/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration3_es2017.ts new file mode 100644 index 0000000000000..9b5924fc1455f --- /dev/null +++ b/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration3_es2017.ts @@ -0,0 +1,4 @@ +// @target: es2017 +// @noEmitHelpers: true +function f(await = await) { +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration4_es2017.ts b/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration4_es2017.ts new file mode 100644 index 0000000000000..84c6c93b90faf --- /dev/null +++ b/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration4_es2017.ts @@ -0,0 +1,4 @@ +// @target: es2017 +// @noEmitHelpers: true +function await() { +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration5_es2017.ts b/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration5_es2017.ts new file mode 100644 index 0000000000000..c57fb73d6d712 --- /dev/null +++ b/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration5_es2017.ts @@ -0,0 +1,4 @@ +// @target: es2017 +// @noEmitHelpers: true +async function foo(await): Promise { +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration6_es2017.ts b/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration6_es2017.ts new file mode 100644 index 0000000000000..1ceb160c12b8a --- /dev/null +++ b/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration6_es2017.ts @@ -0,0 +1,4 @@ +// @target: es2017 +// @noEmitHelpers: true +async function foo(a = await): Promise { +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration7_es2017.ts b/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration7_es2017.ts new file mode 100644 index 0000000000000..e5c2649d34edd --- /dev/null +++ b/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration7_es2017.ts @@ -0,0 +1,7 @@ +// @target: es2017 +// @noEmitHelpers: true +async function bar(): Promise { + // 'await' here is an identifier, and not a yield expression. + async function foo(a = await): Promise { + } +} \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration8_es2017.ts b/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration8_es2017.ts new file mode 100644 index 0000000000000..64e62a2719ef0 --- /dev/null +++ b/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration8_es2017.ts @@ -0,0 +1,3 @@ +// @target: es2017 +// @noEmitHelpers: true +var v = { [await]: foo } \ No newline at end of file diff --git a/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration9_es2017.ts b/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration9_es2017.ts new file mode 100644 index 0000000000000..7d3b7213b99e0 --- /dev/null +++ b/tests/cases/conformance/async/es2017/functionDeclarations/asyncFunctionDeclaration9_es2017.ts @@ -0,0 +1,5 @@ +// @target: es2017 +// @noEmitHelpers: true +async function foo(): Promise { + var v = { [await]: foo } +} \ No newline at end of file diff --git a/tests/cases/conformance/types/literal/literalTypes3.ts b/tests/cases/conformance/types/literal/literalTypes3.ts new file mode 100644 index 0000000000000..2aa3f1020e26c --- /dev/null +++ b/tests/cases/conformance/types/literal/literalTypes3.ts @@ -0,0 +1,66 @@ +// @strictNullChecks: true + +function f1(s: string) { + if (s === "foo") { + s; // "foo" + } + if (s === "foo" || s === "bar") { + s; // "foo" | "bar" + } +} + +function f2(s: string) { + switch (s) { + case "foo": + case "bar": + s; // "foo" | "bar" + case "baz": + s; // "foo" | "bar" | "baz" + break; + default: + s; // string + } +} + +function f3(s: string) { + return s === "foo" || s === "bar" ? s : undefined; // "foo" | "bar" | undefined +} + +function f4(x: number) { + if (x === 1 || x === 2) { + return x; // 1 | 2 + } + throw new Error(); +} + +function f5(x: number, y: 1 | 2) { + if (x === 0 || x === y) { + x; // 0 | 1 | 2 + } +} + +function f6(x: number, y: 1 | 2) { + if (y === x || 0 === x) { + x; // 0 | 1 | 2 + } +} + +function f7(x: number | "foo" | "bar", y: 1 | 2 | string) { + if (x === y) { + x; // "foo" | "bar" | 1 | 2 + } +} + +function f8(x: number | "foo" | "bar") { + switch (x) { + case 1: + case 2: + x; // 1 | 2 + break; + case "foo": + x; // "foo" + break; + default: + x; // number | "bar" + } +} \ No newline at end of file diff --git a/tests/cases/fourslash/completionListInImportClause04.ts b/tests/cases/fourslash/completionListInImportClause04.ts new file mode 100644 index 0000000000000..672c754e80755 --- /dev/null +++ b/tests/cases/fourslash/completionListInImportClause04.ts @@ -0,0 +1,20 @@ +/// + +// @Filename: foo.d.ts +//// declare class Foo { +//// static prop1(x: number): number; +//// static prop1(x: string): string; +//// static prop2(x: boolean): boolean; +//// } +//// export = Foo; /*2*/ + +// @Filename: app.ts +////import {/*1*/} from './foo'; + +goTo.marker('1'); +verify.completionListContains('prop1'); +verify.completionListContains('prop2'); +verify.not.completionListContains('Foo'); +verify.numberOfErrorsInCurrentFile(0); +goTo.marker('2'); +verify.numberOfErrorsInCurrentFile(0); diff --git a/tests/cases/fourslash/functionTypes.ts b/tests/cases/fourslash/functionTypes.ts index 8973f73431b0d..0baccba286ef6 100644 --- a/tests/cases/fourslash/functionTypes.ts +++ b/tests/cases/fourslash/functionTypes.ts @@ -23,7 +23,7 @@ verify.numberOfErrorsInCurrentFile(0); for (var i = 1; i <= 7; i++) { goTo.marker('' + i); - verify.memberListCount(7); + verify.memberListCount(8); verify.completionListContains('apply'); verify.completionListContains('arguments'); verify.completionListContains('bind'); @@ -31,4 +31,5 @@ for (var i = 1; i <= 7; i++) { verify.completionListContains('length'); verify.completionListContains('caller'); verify.completionListContains('prototype'); + verify.completionListContains('toString'); }