TypeScript/src/compiler/emitter.ts

2948 lines
121 KiB
TypeScript
Raw Normal View History

/// <reference path="checker.ts"/>
/// <reference path="transformer.ts" />
/// <reference path="declarationEmitter.ts"/>
/// <reference path="sourcemap.ts"/>
/// <reference path="comments.ts" />
2014-07-13 01:04:16 +02:00
/* @internal */
namespace ts {
// Flags enum to track count of temp variables and a few dedicated names
const enum TempFlags {
Auto = 0x00000000, // No preferred name
CountMask = 0x0FFFFFFF, // Temp variable counter
_i = 0x10000000, // Use/preference flag for '_i'
2015-03-24 00:16:29 +01:00
}
2016-09-27 23:02:10 +02:00
const id = (s: SourceFile) => s;
2016-10-19 15:26:50 +02:00
const nullTransformers: Transformer[] = [_ => id];
2016-09-27 23:02:10 +02:00
// targetSourceFile is when users only want one file in entire project to be emitted. This is used in compileOnSave feature
export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFile: SourceFile, emitOnlyDtsFiles?: boolean): EmitResult {
const delimiters = createDelimiterMap();
const brackets = createBracketsMap();
// emit output for the __extends helper function
const extendsHelper = `
2015-05-01 19:49:54 +02:00
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};`;
// Emit output for the __assign helper function.
// This is typically used for JSX spread attributes,
// and can be used for object literal spread properties.
const assignHelper = `
var __assign = (this && this.__assign) || Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};`;
// emit output for the __decorate helper function
const decorateHelper = `
2015-05-01 19:49:54 +02:00
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2015-09-11 01:27:35 +02:00
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
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;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};`;
// emit output for the __metadata helper function
const metadataHelper = `
2015-05-01 19:49:54 +02:00
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};`;
// emit output for the __param helper function
const paramHelper = `
2015-05-01 19:49:54 +02:00
var __param = (this && this.__param) || function (paramIndex, decorator) {
2015-04-24 00:36:07 +02:00
return function (target, key) { decorator(target, key, paramIndex); }
};`;
// emit output for the __awaiter helper function
2015-05-07 02:33:58 +02:00
const awaiterHelper = `
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments)).next());
});
};`;
2015-06-18 23:01:49 +02:00
// 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
const generatorHelper = `
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t;
return { next: verb(0), "throw": verb(1), "return": verb(2) };
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [0, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};`;
// emit output for the __export helper function
const exportStarHelper = `
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}`;
// emit output for the UMD helper function.
const umdHelper = `
(function (dependencies, factory) {
if (typeof module === 'object' && typeof module.exports === 'object') {
var v = factory(require, exports); if (v !== undefined) module.exports = v;
}
else if (typeof define === 'function' && define.amd) {
define(dependencies, factory);
}
})`;
const superHelper = `
const _super = name => super[name];`;
const advancedSuperHelper = `
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);`;
2015-11-04 23:02:33 +01:00
const compilerOptions = host.getCompilerOptions();
const languageVersion = getEmitScriptTarget(compilerOptions);
const moduleKind = getEmitModuleKind(compilerOptions);
2015-11-04 23:02:33 +01:00
const sourceMapDataList: SourceMapData[] = compilerOptions.sourceMap || compilerOptions.inlineSourceMap ? [] : undefined;
const emittedFilesList: string[] = compilerOptions.listEmittedFiles ? [] : undefined;
const emitterDiagnostics = createDiagnosticCollection();
2015-11-04 23:02:33 +01:00
const newLine = host.getNewLine();
2016-09-27 23:02:10 +02:00
const transformers: Transformer[] = emitOnlyDtsFiles ? nullTransformers : getTransformers(compilerOptions);
const writer = createTextWriter(newLine);
const {
write,
writeLine,
increaseIndent,
decreaseIndent
} = writer;
const sourceMap = createSourceMapWriter(host, writer);
const {
2016-09-26 22:52:09 +02:00
emitNodeWithSourceMap,
emitTokenWithSourceMap
} = sourceMap;
const comments = createCommentWriter(host, writer, sourceMap);
const {
emitNodeWithComments,
emitBodyWithDetachedComments,
emitTrailingCommentsOfPosition
} = comments;
let nodeIdToGeneratedName: string[];
let autoGeneratedIdToGeneratedName: string[];
let generatedNameSet: Map<string>;
let tempFlags: TempFlags;
let currentSourceFile: SourceFile;
let currentText: string;
let currentFileIdentifiers: Map<string>;
let extendsEmitted: boolean;
let assignEmitted: boolean;
let decorateEmitted: boolean;
let paramEmitted: boolean;
let awaiterEmitted: boolean;
let isOwnFileEmit: boolean;
let emitSkipped = false;
2016-09-27 00:21:03 +02:00
const sourceFiles = getSourceFilesToEmit(host, targetSourceFile);
// Transform the source files
2016-09-27 00:21:03 +02:00
performance.mark("beforeTransform");
const {
2016-09-27 00:21:03 +02:00
transformed,
2016-09-26 22:52:09 +02:00
emitNodeWithSubstitution,
emitNodeWithNotification
2016-09-27 00:21:03 +02:00
} = transformFiles(resolver, host, sourceFiles, transformers);
performance.measure("transformTime", "beforeTransform");
// Emit each output file
2016-09-27 00:21:03 +02:00
performance.mark("beforePrint");
2016-09-30 02:32:11 +02:00
forEachTransformedEmitFile(host, transformed, emitFile, emitOnlyDtsFiles);
[Transforms] Merge master 08/09 (#10263) * Improve error message * Remove `SupportedExpressionWithTypeArguments` type; just check that the expression of each `ExpressionWithTypeArguments` is an `EntityNameExpression`. * Fix bug * Fix #10083 - allowSyntheticDefaultImports alters getExternalModuleMember (#10096) * Use recursion, and fix error for undefined node * Rename function * Fix lint error * Narrowing type parameter intersects w/narrowed types This makes sure that a union type that includes a type parameter is still usable as the actual type that the type guard narrows to. * Don't allow ".d.ts" extension in an import either. * Add a helper function `getOrUpdateProperty` to prevent unprotected access to Maps. * Limit type guards as assertions to incomplete types in loops * Accept new baselines * Fix linting error * Allow JS multiple declarations of ctor properties When a property is declared in the constructor and on the prototype of an ES6 class, the property's symbol is discarded in favour of the method's symbol. That because the usual use for this pattern is to bind an instance function: `this.m = this.m.bind(this)`. In this case the type you want really is the method's type. * Use {} type facts for unconstrained type params Previously it was using TypeFacts.All. But the constraint of an unconstrained type parameter is actually {}. * Fix newline lint * Test that declares conflicting method first * [Release-2.0] Fix 9662: Visual Studio 2015 with TS2.0 gives incorrect @types path resolution errors (#9867) * Change the shape of the shim layer to support getAutomaticTypeDirectives * Change the key for looking up automatic type-directives * Update baselines from change look-up name of type-directives * Add @currentDirectory into the test * Update baselines * Fix linting error * Address PR: fix spelling mistake * Instead of return path of the type directive names just return type directive names * Remove unused reference files: these tests produce erros so they will not produce these files (#9233) * Add string-literal completion test for jsdoc * Support other (new) literal types in jsdoc * Don't allow properties inherited from Object to be automatically included in TSX attributes * Add new test baseline and delete else in binder The extra `else` caused a ton of test failures! * Fix lint * Port PR #10016 to Master (#10100) * Treat namespaceExportDeclaration as declaration * Update baselines * wip - add tests * Add tests * Show "export namespace" for quick-info * Fix more lint * Try using runtests-parallel for CI (#9970) * Try using runtests-parallel for CI * Put worker count setting into .travis.yml * Reduce worker count to 4 - 8 wasnt much different from 4-6 but had contention issues causing timeouts * Fix lssl task (#9967) * Surface noErrorTruncation option * Stricter check for discriminant properties in type guards * Add tests * Emit more efficient/concise "empty" ES6 ctor When there are property assignments in a the class body of an inheriting class, tsc current emit the following compilation: ```ts class Foo extends Bar { public foo = 1; } ``` ```js class Foo extends Bar { constructor(…args) { super(…args); this.foo = 1; } } ``` This introduces an unneeded local variable and might force a reification of the `arguments` object (or otherwise reify the arguments into an array). This is particularly bad when that output is fed into another transpiler like Babel. In Babel, you get something like this today: ```js var Foo = (function (_Bar) { _inherits(Foo, _Bar); function Foo() { _classCallCheck(this, Foo); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _Bar.call.apply(_Bar, [this].concat(args)); this.foo = 1; } return Foo; })(Bar); ``` This causes a lot of needless work/allocations and some very strange code (`.call.apply` o_0). Admittedly, this is not strictly tsc’s problem; it could have done a deeper analysis of the code and optimized out the extra dance. However, tsc could also have emitted this simpler, more concise and semantically equivalent code in the first place: ```js class Foo extends Bar { constructor() { super(…arguments); this.foo = 1; } } ``` Which compiles into the following in Babel: ```js var Foo = (function (_Bar) { _inherits(Foo, _Bar); function Foo() { _classCallCheck(this, Foo); _Bar.apply(this, arguments); this.foo = 1; } return Foo; })(Bar); ``` Which is well-optimized (today) in most engines and much less confusing to read. As far as I can tell, the proposed compilation has exactly the same semantics as before. Fixes #10175 * Fix instanceof operator narrowing issues * Accept new baselines * Add regression test * Improve naming and documentation from PR * Update comment * Add more tests * Accept new baselines * Reduce worker count to 3 (#10210) Since we saw a starvation issue on one of @sandersn's PRs. * Speed up fourslash tests * Duh * Make baselines faster by not writing out unneeded files * Fix non-strict-compliant test * Fix 10076: Fix Tuple Destructing with "this" (#10208) * Call checkExpression eventhough there is no appropriate type from destructuring of array * Add tests and baselines * use transpileModule * Remove use strict * Improve instanceof for structurally identical types * Introduce isTypeInstanceOf function * Add test * Accept new baselines * Fix loop over array to use for-of instead of for-in * Use correct this in tuple type parameter constraints Instantiate this in tuple types used as type parameter constraints * Add explanatory comment to resolveTupleTypeMembers * Ignore null, undefined, void when checking for discriminant property * Add regression test * Delay tuple type constraint resolution Create a new tuple that stores the this-type. * Always use thisType when generating tuple id * Optimize format of type list id strings used in maps * wip - fix error * Make ReadonlyArray iterable. * Allow OSX to fail while we investigate (#10255) The random test timeouts are an issue. * Fix error from using merging master * avoid using the global name * Fix single-quote lint * Update baselines * Fix linting * Optimize performance of maps * Update API sample * Fix processDiagnosticMessages script * Have travis take shallow clones of the repo (#10275) Just cloning TS on travis takes 23 seconds on linux (68 seconds on mac), hopefully having it do a shallow clone will help. We don't rely on any tagging/artifacts from the travis servers which clone depth could impact, so this shouldn't impact anything other than build speed. * Add folds to travis log (#10269) * Optimize filterType to only call getUnionType if necessary * Add shorthand types declaration for travis-fold (#10293) * Optimize getTypeWithFacts * Filter out nullable and primitive types in isDiscriminantProperty * Fix typo * Add regression tests * Optimize core filter function to only allocate when necessary * Address CR comments + more optimizations * Faster path for creating union types from filterType * Allow an @types direcotry to have a package.json which specifies `"typings": null` to disclude it from automatically included typings. * Lint * Collect timing information for commands running on travis (#10308) * Simplifies performance API * Use 'MapLike' instead of 'Map' in 'preferConstRule.ts'. * narrow from 'any' in most situations instanceof and user-defined typeguards narrow from 'any' unless the narrowed-to type is exactly 'Object' or 'Function'. This is a breaking change. * Update instanceof conformance tests * accept new baselines * add tests * accept new baselines * Use lowercase names for type reference directives * Use proper response codes in web tests * Treat ambient shorthand declarations as explicit uses of the `any` type * Rename 'find' functions * Parallel linting (#10313) * A perilous thing, a parallel lint * Use work queue rather than scheduling work * Dont read files for lint on main thread * Fix style * Fix the style fix (#10344) * Aligned mark names with values used by ts-perf. * Use an enum in checkClassForDuplicateDeclarations to aid readability * Rename to Accessor * Migrated more MapLikes to Maps * Add ES2015 Date constructor signature that accepts another Date (#10353) * Parameters with no assignments implicitly considered const * Add tests * Migrate additional MapLikes to Maps. * Fix 10625: JSX Not validating when index signature is present (#10352) * Check for type of property declaration before using index signature * Add tests and baselines * fix linting error * Adding more comments * Clean up/move some Map helper functions. * Revert some formatting changes. * Improve ReadonlyArray<T>.concat to match Array<T> The Array-based signature was incorrect and also out-of-date. * Fix link to blog * Remove old assertion about when we're allowed to use fileExists * Set isNewIdentifierLocation to true for JavaScript files * Update error message for conflicting type definitions Fixes #10370 * Explain why we lower-case type reference directives * Correctly merge bindThisPropertyAssignment Also simply it considerably after noticing that it's *only* called for Javascript files, so there was a lot of dead code for TS cases that never happened. * Fix comment * Property handle imcomplete control flow types in nested loops * Update due to CR suggestion * Add regression test * Assign and instantiate contextual this type if not present * Fix 10289: correctly generate tsconfig.json with --lib (#10355) * Separate generate tsconfig into its own function and implement init with --lib # Conflicts: # src/compiler/tsc.ts * Add tests and baselines; Update function name Add unittests and baselines Add unittests and baselines for generating tsconfig Move unittest into harness folder Update harness tsconfig.json USe correct function name * Use new MapLike interstead. Update unittest # Conflicts: # src/compiler/commandLineParser.ts * Update JakeFile * Add tests for incorrect cases * Address PR : remove explicity write node_modules * JSDoc supports null, undefined and never types * Update baselines in jsDocParsing unit tests * Restored comments to explain spreading 'arguments' into calls to 'super'. * Added test. * Use the non-nullable type of the contextual type for object completions. * Return non-JsDocComment children ... to make syntactic classification work * Add more tests for `export = foo.bar`. * Output test baselines to tests/baselines/local instead of root * Move supportedTypescriptExtensionsWithDtsFirst next to supportedTypeScriptExtensions and rename * Fix comment * Fix RWC Runner (#10420) * Use /// <reference types * Don't report an errors if it comes from lib.d.ts * Treat special property access symbol differently ... when retriving documentation * Fix tests * Update shim version to be 2.1 (#10424) * Check return code paths on getters (#10102) * Check return paths on getters * Remove TODO comment * Remove extraneous arguments from harness's runBaseline (#10419) * Remove extraneous arguments from runBaseline * Address comments from @yuit * Remove needless call to basename * Refactor baseliners out of compiler runner (#10440) * CR feedback * fix broken tests * Pass in baselineOpts into types baselines so that RWC baselines can be written to internal folder (#10443) * Add error message Add error message when trying to relate primitives to the boxed/apparent backing types. * fix linting error * follow advise * remove extra code * Add more test for 10426 * fix some errors * routine update of dom libs * Add test for jsdoc syntactic classification for function declaration * Simplify implementation * Tolerate certain errors in tsconfig.json * Add test for configFile error tolerance * Use TS parser to tolerate more errors in tsconfig.json * Implement tuple types as type references to synthesized generic types * Add comments + minor changes * Accept new baselines * Add .types extension * Properly guard for undefined in getTypeReferenceArity * Add jsdoc nullable union test case to fourslash * Fix class/interface merging issue + lint error * Allow "typings" in a package.json to be missing its extension (but also allow it to have an extension) * Contextually type this in getDeclFromSig, not checkThisExpr * Update parser comment with es7 grammar (#10459) * Use ES7 term of ExponentiationExpression * Update timeout for mac OS * Address PR: add space * allowSyntheticDefaultImports resolves to modules instead of variables Fixes #10429 by improving the fix in #10096 * Rename getContextuallyTypedThisParameter to getContextualThisParameter * Fix 10472: Invalid emitted code for await expression (#10483) * Properly emit await expression with yield expression * Add tests and update baselines * Move parsing await expression into parse unary-expression * Update incorrect comment * change error message * Fix broken build from merging with master * Fix linting error
2016-08-27 00:51:10 +02:00
performance.measure("printTime", "beforePrint");
2016-09-27 00:21:03 +02:00
// Clean up emit nodes on parse tree
for (const sourceFile of sourceFiles) {
disposeEmitNodes(sourceFile);
}
return {
emitSkipped,
diagnostics: emitterDiagnostics.getDiagnostics(),
emittedFiles: emittedFilesList,
sourceMaps: sourceMapDataList
};
function emitFile(jsFilePath: string, sourceMapFilePath: string, declarationFilePath: string, sourceFiles: SourceFile[], isBundledEmit: boolean) {
// Make sure not to write js file and source map file if any of them cannot be written
if (!host.isEmitBlocked(jsFilePath) && !compilerOptions.noEmit) {
2016-09-27 23:02:10 +02:00
if (!emitOnlyDtsFiles) {
printFile(jsFilePath, sourceMapFilePath, sourceFiles, isBundledEmit);
}
}
else {
emitSkipped = true;
}
if (declarationFilePath) {
2016-09-27 19:22:41 +02:00
emitSkipped = writeDeclarationFile(declarationFilePath, getOriginalSourceFiles(sourceFiles), isBundledEmit, host, resolver, emitterDiagnostics, emitOnlyDtsFiles) || emitSkipped;
}
if (!emitSkipped && emittedFilesList) {
2016-09-27 23:02:10 +02:00
if (!emitOnlyDtsFiles) {
emittedFilesList.push(jsFilePath);
}
if (sourceMapFilePath) {
emittedFilesList.push(sourceMapFilePath);
}
if (declarationFilePath) {
emittedFilesList.push(declarationFilePath);
}
}
}
function printFile(jsFilePath: string, sourceMapFilePath: string, sourceFiles: SourceFile[], isBundledEmit: boolean) {
sourceMap.initialize(jsFilePath, sourceMapFilePath, sourceFiles, isBundledEmit);
nodeIdToGeneratedName = [];
autoGeneratedIdToGeneratedName = [];
[Transforms] Merge master 08/09 (#10263) * Improve error message * Remove `SupportedExpressionWithTypeArguments` type; just check that the expression of each `ExpressionWithTypeArguments` is an `EntityNameExpression`. * Fix bug * Fix #10083 - allowSyntheticDefaultImports alters getExternalModuleMember (#10096) * Use recursion, and fix error for undefined node * Rename function * Fix lint error * Narrowing type parameter intersects w/narrowed types This makes sure that a union type that includes a type parameter is still usable as the actual type that the type guard narrows to. * Don't allow ".d.ts" extension in an import either. * Add a helper function `getOrUpdateProperty` to prevent unprotected access to Maps. * Limit type guards as assertions to incomplete types in loops * Accept new baselines * Fix linting error * Allow JS multiple declarations of ctor properties When a property is declared in the constructor and on the prototype of an ES6 class, the property's symbol is discarded in favour of the method's symbol. That because the usual use for this pattern is to bind an instance function: `this.m = this.m.bind(this)`. In this case the type you want really is the method's type. * Use {} type facts for unconstrained type params Previously it was using TypeFacts.All. But the constraint of an unconstrained type parameter is actually {}. * Fix newline lint * Test that declares conflicting method first * [Release-2.0] Fix 9662: Visual Studio 2015 with TS2.0 gives incorrect @types path resolution errors (#9867) * Change the shape of the shim layer to support getAutomaticTypeDirectives * Change the key for looking up automatic type-directives * Update baselines from change look-up name of type-directives * Add @currentDirectory into the test * Update baselines * Fix linting error * Address PR: fix spelling mistake * Instead of return path of the type directive names just return type directive names * Remove unused reference files: these tests produce erros so they will not produce these files (#9233) * Add string-literal completion test for jsdoc * Support other (new) literal types in jsdoc * Don't allow properties inherited from Object to be automatically included in TSX attributes * Add new test baseline and delete else in binder The extra `else` caused a ton of test failures! * Fix lint * Port PR #10016 to Master (#10100) * Treat namespaceExportDeclaration as declaration * Update baselines * wip - add tests * Add tests * Show "export namespace" for quick-info * Fix more lint * Try using runtests-parallel for CI (#9970) * Try using runtests-parallel for CI * Put worker count setting into .travis.yml * Reduce worker count to 4 - 8 wasnt much different from 4-6 but had contention issues causing timeouts * Fix lssl task (#9967) * Surface noErrorTruncation option * Stricter check for discriminant properties in type guards * Add tests * Emit more efficient/concise "empty" ES6 ctor When there are property assignments in a the class body of an inheriting class, tsc current emit the following compilation: ```ts class Foo extends Bar { public foo = 1; } ``` ```js class Foo extends Bar { constructor(…args) { super(…args); this.foo = 1; } } ``` This introduces an unneeded local variable and might force a reification of the `arguments` object (or otherwise reify the arguments into an array). This is particularly bad when that output is fed into another transpiler like Babel. In Babel, you get something like this today: ```js var Foo = (function (_Bar) { _inherits(Foo, _Bar); function Foo() { _classCallCheck(this, Foo); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _Bar.call.apply(_Bar, [this].concat(args)); this.foo = 1; } return Foo; })(Bar); ``` This causes a lot of needless work/allocations and some very strange code (`.call.apply` o_0). Admittedly, this is not strictly tsc’s problem; it could have done a deeper analysis of the code and optimized out the extra dance. However, tsc could also have emitted this simpler, more concise and semantically equivalent code in the first place: ```js class Foo extends Bar { constructor() { super(…arguments); this.foo = 1; } } ``` Which compiles into the following in Babel: ```js var Foo = (function (_Bar) { _inherits(Foo, _Bar); function Foo() { _classCallCheck(this, Foo); _Bar.apply(this, arguments); this.foo = 1; } return Foo; })(Bar); ``` Which is well-optimized (today) in most engines and much less confusing to read. As far as I can tell, the proposed compilation has exactly the same semantics as before. Fixes #10175 * Fix instanceof operator narrowing issues * Accept new baselines * Add regression test * Improve naming and documentation from PR * Update comment * Add more tests * Accept new baselines * Reduce worker count to 3 (#10210) Since we saw a starvation issue on one of @sandersn's PRs. * Speed up fourslash tests * Duh * Make baselines faster by not writing out unneeded files * Fix non-strict-compliant test * Fix 10076: Fix Tuple Destructing with "this" (#10208) * Call checkExpression eventhough there is no appropriate type from destructuring of array * Add tests and baselines * use transpileModule * Remove use strict * Improve instanceof for structurally identical types * Introduce isTypeInstanceOf function * Add test * Accept new baselines * Fix loop over array to use for-of instead of for-in * Use correct this in tuple type parameter constraints Instantiate this in tuple types used as type parameter constraints * Add explanatory comment to resolveTupleTypeMembers * Ignore null, undefined, void when checking for discriminant property * Add regression test * Delay tuple type constraint resolution Create a new tuple that stores the this-type. * Always use thisType when generating tuple id * Optimize format of type list id strings used in maps * wip - fix error * Make ReadonlyArray iterable. * Allow OSX to fail while we investigate (#10255) The random test timeouts are an issue. * Fix error from using merging master * avoid using the global name * Fix single-quote lint * Update baselines * Fix linting * Optimize performance of maps * Update API sample * Fix processDiagnosticMessages script * Have travis take shallow clones of the repo (#10275) Just cloning TS on travis takes 23 seconds on linux (68 seconds on mac), hopefully having it do a shallow clone will help. We don't rely on any tagging/artifacts from the travis servers which clone depth could impact, so this shouldn't impact anything other than build speed. * Add folds to travis log (#10269) * Optimize filterType to only call getUnionType if necessary * Add shorthand types declaration for travis-fold (#10293) * Optimize getTypeWithFacts * Filter out nullable and primitive types in isDiscriminantProperty * Fix typo * Add regression tests * Optimize core filter function to only allocate when necessary * Address CR comments + more optimizations * Faster path for creating union types from filterType * Allow an @types direcotry to have a package.json which specifies `"typings": null` to disclude it from automatically included typings. * Lint * Collect timing information for commands running on travis (#10308) * Simplifies performance API * Use 'MapLike' instead of 'Map' in 'preferConstRule.ts'. * narrow from 'any' in most situations instanceof and user-defined typeguards narrow from 'any' unless the narrowed-to type is exactly 'Object' or 'Function'. This is a breaking change. * Update instanceof conformance tests * accept new baselines * add tests * accept new baselines * Use lowercase names for type reference directives * Use proper response codes in web tests * Treat ambient shorthand declarations as explicit uses of the `any` type * Rename 'find' functions * Parallel linting (#10313) * A perilous thing, a parallel lint * Use work queue rather than scheduling work * Dont read files for lint on main thread * Fix style * Fix the style fix (#10344) * Aligned mark names with values used by ts-perf. * Use an enum in checkClassForDuplicateDeclarations to aid readability * Rename to Accessor * Migrated more MapLikes to Maps * Add ES2015 Date constructor signature that accepts another Date (#10353) * Parameters with no assignments implicitly considered const * Add tests * Migrate additional MapLikes to Maps. * Fix 10625: JSX Not validating when index signature is present (#10352) * Check for type of property declaration before using index signature * Add tests and baselines * fix linting error * Adding more comments * Clean up/move some Map helper functions. * Revert some formatting changes. * Improve ReadonlyArray<T>.concat to match Array<T> The Array-based signature was incorrect and also out-of-date. * Fix link to blog * Remove old assertion about when we're allowed to use fileExists * Set isNewIdentifierLocation to true for JavaScript files * Update error message for conflicting type definitions Fixes #10370 * Explain why we lower-case type reference directives * Correctly merge bindThisPropertyAssignment Also simply it considerably after noticing that it's *only* called for Javascript files, so there was a lot of dead code for TS cases that never happened. * Fix comment * Property handle imcomplete control flow types in nested loops * Update due to CR suggestion * Add regression test * Assign and instantiate contextual this type if not present * Fix 10289: correctly generate tsconfig.json with --lib (#10355) * Separate generate tsconfig into its own function and implement init with --lib # Conflicts: # src/compiler/tsc.ts * Add tests and baselines; Update function name Add unittests and baselines Add unittests and baselines for generating tsconfig Move unittest into harness folder Update harness tsconfig.json USe correct function name * Use new MapLike interstead. Update unittest # Conflicts: # src/compiler/commandLineParser.ts * Update JakeFile * Add tests for incorrect cases * Address PR : remove explicity write node_modules * JSDoc supports null, undefined and never types * Update baselines in jsDocParsing unit tests * Restored comments to explain spreading 'arguments' into calls to 'super'. * Added test. * Use the non-nullable type of the contextual type for object completions. * Return non-JsDocComment children ... to make syntactic classification work * Add more tests for `export = foo.bar`. * Output test baselines to tests/baselines/local instead of root * Move supportedTypescriptExtensionsWithDtsFirst next to supportedTypeScriptExtensions and rename * Fix comment * Fix RWC Runner (#10420) * Use /// <reference types * Don't report an errors if it comes from lib.d.ts * Treat special property access symbol differently ... when retriving documentation * Fix tests * Update shim version to be 2.1 (#10424) * Check return code paths on getters (#10102) * Check return paths on getters * Remove TODO comment * Remove extraneous arguments from harness's runBaseline (#10419) * Remove extraneous arguments from runBaseline * Address comments from @yuit * Remove needless call to basename * Refactor baseliners out of compiler runner (#10440) * CR feedback * fix broken tests * Pass in baselineOpts into types baselines so that RWC baselines can be written to internal folder (#10443) * Add error message Add error message when trying to relate primitives to the boxed/apparent backing types. * fix linting error * follow advise * remove extra code * Add more test for 10426 * fix some errors * routine update of dom libs * Add test for jsdoc syntactic classification for function declaration * Simplify implementation * Tolerate certain errors in tsconfig.json * Add test for configFile error tolerance * Use TS parser to tolerate more errors in tsconfig.json * Implement tuple types as type references to synthesized generic types * Add comments + minor changes * Accept new baselines * Add .types extension * Properly guard for undefined in getTypeReferenceArity * Add jsdoc nullable union test case to fourslash * Fix class/interface merging issue + lint error * Allow "typings" in a package.json to be missing its extension (but also allow it to have an extension) * Contextually type this in getDeclFromSig, not checkThisExpr * Update parser comment with es7 grammar (#10459) * Use ES7 term of ExponentiationExpression * Update timeout for mac OS * Address PR: add space * allowSyntheticDefaultImports resolves to modules instead of variables Fixes #10429 by improving the fix in #10096 * Rename getContextuallyTypedThisParameter to getContextualThisParameter * Fix 10472: Invalid emitted code for await expression (#10483) * Properly emit await expression with yield expression * Add tests and update baselines * Move parsing await expression into parse unary-expression * Update incorrect comment * change error message * Fix broken build from merging with master * Fix linting error
2016-08-27 00:51:10 +02:00
generatedNameSet = createMap<string>();
isOwnFileEmit = !isBundledEmit;
// Emit helpers from all the files
if (isBundledEmit && moduleKind) {
for (const sourceFile of sourceFiles) {
emitEmitHelpers(sourceFile);
}
}
// Print each transformed source file.
forEach(sourceFiles, printSourceFile);
writeLine();
const sourceMappingURL = sourceMap.getSourceMappingURL();
if (sourceMappingURL) {
2016-08-02 20:45:56 +02:00
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) {
writeFile(host, emitterDiagnostics, sourceMapFilePath, sourceMap.getText(), /*writeByteOrderMark*/ false);
2015-03-24 00:16:29 +01:00
}
2015-03-04 08:46:51 +01:00
// Record source map data for the test harness.
if (sourceMapDataList) {
sourceMapDataList.push(sourceMap.getSourceMapData());
2015-03-24 00:16:29 +01:00
}
// Write the output file
writeFile(host, emitterDiagnostics, jsFilePath, writer.getText(), compilerOptions.emitBOM);
// Reset state
sourceMap.reset();
comments.reset();
writer.reset();
tempFlags = TempFlags.Auto;
currentSourceFile = undefined;
currentText = undefined;
extendsEmitted = false;
assignEmitted = false;
decorateEmitted = false;
paramEmitted = false;
awaiterEmitted = false;
isOwnFileEmit = false;
}
2015-03-24 00:16:29 +01:00
function printSourceFile(node: SourceFile) {
currentSourceFile = node;
currentText = node.text;
currentFileIdentifiers = node.identifiers;
sourceMap.setSourceFile(node);
comments.setSourceFile(node);
2016-09-27 00:21:03 +02:00
pipelineEmitWithNotification(EmitContext.SourceFile, node);
}
/**
* Emits a node.
*/
function emit(node: Node) {
2016-09-27 00:21:03 +02:00
pipelineEmitWithNotification(EmitContext.Unspecified, node);
}
/**
2016-09-27 00:21:03 +02:00
* Emits an IdentifierName.
*/
function emitIdentifierName(node: Identifier) {
2016-09-27 00:21:03 +02:00
pipelineEmitWithNotification(EmitContext.IdentifierName, node);
}
/**
* Emits an expression node.
*/
function emitExpression(node: Expression) {
2016-09-27 00:21:03 +02:00
pipelineEmitWithNotification(EmitContext.Expression, node);
}
/**
2016-09-27 00:21:03 +02:00
* Emits a node with possible notification.
*
* NOTE: Do not call this method directly. It is part of the emit pipeline
2016-09-27 00:21:03 +02:00
* and should only be called from printSourceFile, emit, emitExpression, or
* emitIdentifierName.
*/
2016-09-27 00:21:03 +02:00
function pipelineEmitWithNotification(emitContext: EmitContext, node: Node) {
emitNodeWithNotification(emitContext, node, pipelineEmitWithComments);
}
/**
2016-09-27 00:21:03 +02:00
* Emits a node with comments.
*
2016-09-27 00:21:03 +02:00
* NOTE: Do not call this method directly. It is part of the emit pipeline
* and should only be called indirectly from pipelineEmitWithNotification.
*/
2016-09-27 00:21:03 +02:00
function pipelineEmitWithComments(emitContext: EmitContext, node: Node) {
// Do not emit comments for SourceFile
if (emitContext === EmitContext.SourceFile) {
pipelineEmitWithSourceMap(emitContext, node);
return;
}
2016-09-27 00:21:03 +02:00
emitNodeWithComments(emitContext, node, pipelineEmitWithSourceMap);
}
/**
2016-09-27 00:21:03 +02:00
* Emits a node with source maps.
*
* NOTE: Do not call this method directly. It is part of the emit pipeline
2016-09-27 00:21:03 +02:00
* and should only be called indirectly from pipelineEmitWithComments.
*/
2016-09-27 00:21:03 +02:00
function pipelineEmitWithSourceMap(emitContext: EmitContext, node: Node) {
// Do not emit source mappings for SourceFile or IdentifierName
if (emitContext === EmitContext.SourceFile
|| emitContext === EmitContext.IdentifierName) {
pipelineEmitWithSubstitution(emitContext, node);
return;
}
2016-09-27 00:21:03 +02:00
emitNodeWithSourceMap(emitContext, node, pipelineEmitWithSubstitution);
}
/**
2016-09-27 00:21:03 +02:00
* Emits a node with possible substitution.
*
2016-09-27 00:21:03 +02:00
* 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).
*/
2016-09-27 00:21:03 +02:00
function pipelineEmitWithSubstitution(emitContext: EmitContext, node: Node) {
emitNodeWithSubstitution(emitContext, node, pipelineEmitForContext);
}
/**
2016-09-27 00:21:03 +02:00
* Emits a node.
*
2016-09-27 00:21:03 +02:00
* NOTE: Do not call this method directly. It is part of the emit pipeline
* and should only be called indirectly from pipelineEmitWithSubstitution.
*/
2016-09-27 00:21:03 +02:00
function pipelineEmitForContext(emitContext: EmitContext, node: Node): void {
switch (emitContext) {
case EmitContext.SourceFile: return pipelineEmitInSourceFileContext(node);
case EmitContext.IdentifierName: return pipelineEmitInIdentifierNameContext(node);
case EmitContext.Unspecified: return pipelineEmitInUnspecifiedContext(node);
case EmitContext.Expression: return pipelineEmitInExpressionContext(node);
}
}
/**
2016-09-27 00:21:03 +02:00
* Emits a node in the SourceFile EmitContext.
*
2016-09-27 00:21:03 +02:00
* NOTE: Do not call this method directly. It is part of the emit pipeline
* and should only be called indirectly from pipelineEmitForContext.
*/
2016-09-27 00:21:03 +02:00
function pipelineEmitInSourceFileContext(node: Node): void {
const kind = node.kind;
switch (kind) {
// Top-level nodes
case SyntaxKind.SourceFile:
return emitSourceFile(<SourceFile>node);
}
}
/**
2016-09-27 00:21:03 +02:00
* Emits a node in the IdentifierName EmitContext.
*
2016-09-27 00:21:03 +02:00
* NOTE: Do not call this method directly. It is part of the emit pipeline
* and should only be called indirectly from pipelineEmitForContext.
*/
2016-09-27 00:21:03 +02:00
function pipelineEmitInIdentifierNameContext(node: Node): void {
const kind = node.kind;
switch (kind) {
// Identifiers
case SyntaxKind.Identifier:
return emitIdentifier(<Identifier>node);
}
}
/**
2016-09-27 00:21:03 +02:00
* Emits a node in the Unspecified EmitContext.
*
* NOTE: Do not call this method directly. It is part of the emit pipeline
2016-09-27 00:21:03 +02:00
* and should only be called indirectly from pipelineEmitForContext.
*/
2016-09-27 00:21:03 +02:00
function pipelineEmitInUnspecifiedContext(node: Node): void {
const kind = node.kind;
switch (kind) {
// Pseudo-literals
case SyntaxKind.TemplateHead:
case SyntaxKind.TemplateMiddle:
case SyntaxKind.TemplateTail:
return emitLiteral(<LiteralExpression>node);
// Identifiers
case SyntaxKind.Identifier:
return emitIdentifier(<Identifier>node);
// Reserved words
case SyntaxKind.ConstKeyword:
case SyntaxKind.DefaultKeyword:
case SyntaxKind.ExportKeyword:
case SyntaxKind.VoidKeyword:
// Strict mode reserved words
case SyntaxKind.PrivateKeyword:
case SyntaxKind.ProtectedKeyword:
case SyntaxKind.PublicKeyword:
case SyntaxKind.StaticKeyword:
// 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.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:
2016-09-27 00:21:03 +02:00
writeTokenText(kind);
return;
// Parse tree nodes
// Names
case SyntaxKind.QualifiedName:
return emitQualifiedName(<QualifiedName>node);
case SyntaxKind.ComputedPropertyName:
return emitComputedPropertyName(<ComputedPropertyName>node);
// Signature elements
case SyntaxKind.TypeParameter:
return emitTypeParameter(<TypeParameterDeclaration>node);
case SyntaxKind.Parameter:
return emitParameter(<ParameterDeclaration>node);
case SyntaxKind.Decorator:
return emitDecorator(<Decorator>node);
// Type members
case SyntaxKind.PropertySignature:
return emitPropertySignature(<PropertySignature>node);
case SyntaxKind.PropertyDeclaration:
return emitPropertyDeclaration(<PropertyDeclaration>node);
case SyntaxKind.MethodSignature:
return emitMethodSignature(<MethodSignature>node);
case SyntaxKind.MethodDeclaration:
return emitMethodDeclaration(<MethodDeclaration>node);
case SyntaxKind.Constructor:
return emitConstructor(<ConstructorDeclaration>node);
case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
return emitAccessorDeclaration(<AccessorDeclaration>node);
case SyntaxKind.CallSignature:
return emitCallSignature(<CallSignatureDeclaration>node);
case SyntaxKind.ConstructSignature:
return emitConstructSignature(<ConstructSignatureDeclaration>node);
case SyntaxKind.IndexSignature:
return emitIndexSignature(<IndexSignatureDeclaration>node);
// Types
case SyntaxKind.TypePredicate:
return emitTypePredicate(<TypePredicateNode>node);
case SyntaxKind.TypeReference:
return emitTypeReference(<TypeReferenceNode>node);
case SyntaxKind.FunctionType:
return emitFunctionType(<FunctionTypeNode>node);
case SyntaxKind.ConstructorType:
return emitConstructorType(<ConstructorTypeNode>node);
case SyntaxKind.TypeQuery:
return emitTypeQuery(<TypeQueryNode>node);
case SyntaxKind.TypeLiteral:
return emitTypeLiteral(<TypeLiteralNode>node);
case SyntaxKind.ArrayType:
return emitArrayType(<ArrayTypeNode>node);
case SyntaxKind.TupleType:
return emitTupleType(<TupleTypeNode>node);
case SyntaxKind.UnionType:
return emitUnionType(<UnionTypeNode>node);
case SyntaxKind.IntersectionType:
return emitIntersectionType(<IntersectionTypeNode>node);
case SyntaxKind.ParenthesizedType:
return emitParenthesizedType(<ParenthesizedTypeNode>node);
case SyntaxKind.ExpressionWithTypeArguments:
return emitExpressionWithTypeArguments(<ExpressionWithTypeArguments>node);
case SyntaxKind.ThisType:
return emitThisType();
case SyntaxKind.TypeOperator:
return emitTypeOperator(<TypeOperatorNode>node);
case SyntaxKind.IndexedAccessType:
return emitPropertyAccessType(<IndexedAccessTypeNode>node);
2016-08-02 20:45:56 +02:00
case SyntaxKind.LiteralType:
return emitLiteralType(<LiteralTypeNode>node);
// Binding patterns
case SyntaxKind.ObjectBindingPattern:
return emitObjectBindingPattern(<ObjectBindingPattern>node);
case SyntaxKind.ArrayBindingPattern:
return emitArrayBindingPattern(<ArrayBindingPattern>node);
case SyntaxKind.BindingElement:
return emitBindingElement(<BindingElement>node);
// Misc
case SyntaxKind.TemplateSpan:
return emitTemplateSpan(<TemplateSpan>node);
case SyntaxKind.SemicolonClassElement:
return emitSemicolonClassElement();
// Statements
case SyntaxKind.Block:
return emitBlock(<Block>node);
case SyntaxKind.VariableStatement:
return emitVariableStatement(<VariableStatement>node);
case SyntaxKind.EmptyStatement:
return emitEmptyStatement();
case SyntaxKind.ExpressionStatement:
return emitExpressionStatement(<ExpressionStatement>node);
case SyntaxKind.IfStatement:
return emitIfStatement(<IfStatement>node);
case SyntaxKind.DoStatement:
return emitDoStatement(<DoStatement>node);
case SyntaxKind.WhileStatement:
return emitWhileStatement(<WhileStatement>node);
case SyntaxKind.ForStatement:
return emitForStatement(<ForStatement>node);
case SyntaxKind.ForInStatement:
return emitForInStatement(<ForInStatement>node);
case SyntaxKind.ForOfStatement:
return emitForOfStatement(<ForOfStatement>node);
case SyntaxKind.ContinueStatement:
return emitContinueStatement(<ContinueStatement>node);
case SyntaxKind.BreakStatement:
return emitBreakStatement(<BreakStatement>node);
case SyntaxKind.ReturnStatement:
return emitReturnStatement(<ReturnStatement>node);
case SyntaxKind.WithStatement:
return emitWithStatement(<WithStatement>node);
case SyntaxKind.SwitchStatement:
return emitSwitchStatement(<SwitchStatement>node);
case SyntaxKind.LabeledStatement:
return emitLabeledStatement(<LabeledStatement>node);
case SyntaxKind.ThrowStatement:
return emitThrowStatement(<ThrowStatement>node);
case SyntaxKind.TryStatement:
return emitTryStatement(<TryStatement>node);
case SyntaxKind.DebuggerStatement:
return emitDebuggerStatement(<DebuggerStatement>node);
// Declarations
case SyntaxKind.VariableDeclaration:
return emitVariableDeclaration(<VariableDeclaration>node);
case SyntaxKind.VariableDeclarationList:
return emitVariableDeclarationList(<VariableDeclarationList>node);
case SyntaxKind.FunctionDeclaration:
return emitFunctionDeclaration(<FunctionDeclaration>node);
case SyntaxKind.ClassDeclaration:
return emitClassDeclaration(<ClassDeclaration>node);
case SyntaxKind.InterfaceDeclaration:
return emitInterfaceDeclaration(<InterfaceDeclaration>node);
case SyntaxKind.TypeAliasDeclaration:
return emitTypeAliasDeclaration(<TypeAliasDeclaration>node);
case SyntaxKind.EnumDeclaration:
return emitEnumDeclaration(<EnumDeclaration>node);
case SyntaxKind.ModuleDeclaration:
return emitModuleDeclaration(<ModuleDeclaration>node);
case SyntaxKind.ModuleBlock:
return emitModuleBlock(<ModuleBlock>node);
case SyntaxKind.CaseBlock:
return emitCaseBlock(<CaseBlock>node);
case SyntaxKind.ImportEqualsDeclaration:
return emitImportEqualsDeclaration(<ImportEqualsDeclaration>node);
case SyntaxKind.ImportDeclaration:
return emitImportDeclaration(<ImportDeclaration>node);
case SyntaxKind.ImportClause:
return emitImportClause(<ImportClause>node);
case SyntaxKind.NamespaceImport:
return emitNamespaceImport(<NamespaceImport>node);
case SyntaxKind.NamedImports:
return emitNamedImports(<NamedImports>node);
case SyntaxKind.ImportSpecifier:
return emitImportSpecifier(<ImportSpecifier>node);
case SyntaxKind.ExportAssignment:
return emitExportAssignment(<ExportAssignment>node);
case SyntaxKind.ExportDeclaration:
return emitExportDeclaration(<ExportDeclaration>node);
case SyntaxKind.NamedExports:
return emitNamedExports(<NamedExports>node);
case SyntaxKind.ExportSpecifier:
return emitExportSpecifier(<ExportSpecifier>node);
case SyntaxKind.MissingDeclaration:
return;
// Module references
case SyntaxKind.ExternalModuleReference:
return emitExternalModuleReference(<ExternalModuleReference>node);
// JSX (non-expression)
case SyntaxKind.JsxText:
return emitJsxText(<JsxText>node);
case SyntaxKind.JsxOpeningElement:
return emitJsxOpeningElement(<JsxOpeningElement>node);
case SyntaxKind.JsxClosingElement:
return emitJsxClosingElement(<JsxClosingElement>node);
case SyntaxKind.JsxAttribute:
return emitJsxAttribute(<JsxAttribute>node);
case SyntaxKind.JsxSpreadAttribute:
return emitJsxSpreadAttribute(<JsxSpreadAttribute>node);
case SyntaxKind.JsxExpression:
return emitJsxExpression(<JsxExpression>node);
// Clauses
case SyntaxKind.CaseClause:
return emitCaseClause(<CaseClause>node);
case SyntaxKind.DefaultClause:
return emitDefaultClause(<DefaultClause>node);
case SyntaxKind.HeritageClause:
return emitHeritageClause(<HeritageClause>node);
case SyntaxKind.CatchClause:
return emitCatchClause(<CatchClause>node);
// Property assignments
case SyntaxKind.PropertyAssignment:
return emitPropertyAssignment(<PropertyAssignment>node);
case SyntaxKind.ShorthandPropertyAssignment:
return emitShorthandPropertyAssignment(<ShorthandPropertyAssignment>node);
// Enum
case SyntaxKind.EnumMember:
return emitEnumMember(<EnumMember>node);
// JSDoc nodes (ignored)
// Transformation nodes (ignored)
}
2016-09-27 00:21:03 +02:00
// If the node is an expression, try to emit it as an expression with
// substitution.
if (isExpression(node)) {
2016-09-27 00:21:03 +02:00
return pipelineEmitWithSubstitution(EmitContext.Expression, node);
}
}
/**
2016-09-27 00:21:03 +02:00
* Emits a node in the Expression EmitContext.
*
2016-09-27 00:21:03 +02:00
* NOTE: Do not call this method directly. It is part of the emit pipeline
* and should only be called indirectly from pipelineEmitForContext.
*/
2016-09-27 00:21:03 +02:00
function pipelineEmitInExpressionContext(node: Node): void {
const kind = node.kind;
switch (kind) {
// Literals
case SyntaxKind.NumericLiteral:
return emitNumericLiteral(<NumericLiteral>node);
case SyntaxKind.StringLiteral:
case SyntaxKind.RegularExpressionLiteral:
case SyntaxKind.NoSubstitutionTemplateLiteral:
return emitLiteral(<LiteralExpression>node);
// Identifiers
case SyntaxKind.Identifier:
return emitIdentifier(<Identifier>node);
// Reserved words
case SyntaxKind.FalseKeyword:
case SyntaxKind.NullKeyword:
case SyntaxKind.SuperKeyword:
case SyntaxKind.TrueKeyword:
case SyntaxKind.ThisKeyword:
2016-09-27 00:21:03 +02:00
writeTokenText(kind);
return;
// Expressions
case SyntaxKind.ArrayLiteralExpression:
return emitArrayLiteralExpression(<ArrayLiteralExpression>node);
case SyntaxKind.ObjectLiteralExpression:
return emitObjectLiteralExpression(<ObjectLiteralExpression>node);
case SyntaxKind.PropertyAccessExpression:
return emitPropertyAccessExpression(<PropertyAccessExpression>node);
case SyntaxKind.ElementAccessExpression:
return emitElementAccessExpression(<ElementAccessExpression>node);
case SyntaxKind.CallExpression:
return emitCallExpression(<CallExpression>node);
case SyntaxKind.NewExpression:
return emitNewExpression(<NewExpression>node);
case SyntaxKind.TaggedTemplateExpression:
return emitTaggedTemplateExpression(<TaggedTemplateExpression>node);
case SyntaxKind.TypeAssertionExpression:
return emitTypeAssertionExpression(<TypeAssertion>node);
case SyntaxKind.ParenthesizedExpression:
return emitParenthesizedExpression(<ParenthesizedExpression>node);
case SyntaxKind.FunctionExpression:
return emitFunctionExpression(<FunctionExpression>node);
case SyntaxKind.ArrowFunction:
return emitArrowFunction(<ArrowFunction>node);
case SyntaxKind.DeleteExpression:
return emitDeleteExpression(<DeleteExpression>node);
case SyntaxKind.TypeOfExpression:
return emitTypeOfExpression(<TypeOfExpression>node);
case SyntaxKind.VoidExpression:
return emitVoidExpression(<VoidExpression>node);
case SyntaxKind.AwaitExpression:
return emitAwaitExpression(<AwaitExpression>node);
case SyntaxKind.PrefixUnaryExpression:
return emitPrefixUnaryExpression(<PrefixUnaryExpression>node);
case SyntaxKind.PostfixUnaryExpression:
return emitPostfixUnaryExpression(<PostfixUnaryExpression>node);
case SyntaxKind.BinaryExpression:
return emitBinaryExpression(<BinaryExpression>node);
case SyntaxKind.ConditionalExpression:
return emitConditionalExpression(<ConditionalExpression>node);
case SyntaxKind.TemplateExpression:
return emitTemplateExpression(<TemplateExpression>node);
case SyntaxKind.YieldExpression:
return emitYieldExpression(<YieldExpression>node);
case SyntaxKind.SpreadExpression:
return emitSpreadExpression(<SpreadExpression>node);
case SyntaxKind.ClassExpression:
return emitClassExpression(<ClassExpression>node);
case SyntaxKind.OmittedExpression:
return;
case SyntaxKind.AsExpression:
return emitAsExpression(<AsExpression>node);
case SyntaxKind.NonNullExpression:
return emitNonNullExpression(<NonNullExpression>node);
2015-04-03 00:22:53 +02:00
// JSX
case SyntaxKind.JsxElement:
return emitJsxElement(<JsxElement>node);
case SyntaxKind.JsxSelfClosingElement:
return emitJsxSelfClosingElement(<JsxSelfClosingElement>node);
// Transformation nodes
case SyntaxKind.PartiallyEmittedExpression:
return emitPartiallyEmittedExpression(<PartiallyEmittedExpression>node);
}
}
//
// Literals/Pseudo-literals
//
// SyntaxKind.NumericLiteral
function emitNumericLiteral(node: NumericLiteral) {
emitLiteral(node);
if (node.trailingComment) {
write(` /*${node.trailingComment}*/`);
}
}
// SyntaxKind.StringLiteral
// SyntaxKind.RegularExpressionLiteral
// SyntaxKind.NoSubstitutionTemplateLiteral
// SyntaxKind.TemplateHead
// SyntaxKind.TemplateMiddle
// SyntaxKind.TemplateTail
function emitLiteral(node: LiteralLikeNode) {
const text = getLiteralTextOfNode(node);
if ((compilerOptions.sourceMap || compilerOptions.inlineSourceMap)
&& (node.kind === SyntaxKind.StringLiteral || isTemplateLiteralKind(node.kind))) {
writer.writeLiteral(text);
}
else {
write(text);
}
}
//
// Identifiers
//
function emitIdentifier(node: Identifier) {
if (getEmitFlags(node) & EmitFlags.UMDDefine) {
writeLines(umdHelper);
}
else {
write(getTextOfNode(node, /*includeTrivia*/ false));
}
}
//
// Names
//
function emitQualifiedName(node: QualifiedName) {
emitEntityName(node.left);
write(".");
emit(node.right);
}
function emitEntityName(node: EntityName) {
if (node.kind === SyntaxKind.Identifier) {
emitExpression(<Identifier>node);
}
else {
emit(node);
}
}
function emitComputedPropertyName(node: ComputedPropertyName) {
write("[");
emitExpression(node.expression);
write("]");
}
2015-06-26 01:24:41 +02:00
//
// Signature elements
//
2015-06-26 01:24:41 +02:00
function emitTypeParameter(node: TypeParameterDeclaration) {
emit(node.name);
emitWithPrefix(" extends ", node.constraint);
}
function emitParameter(node: ParameterDeclaration) {
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: Decorator) {
write("@");
emitExpression(decorator.expression);
}
//
// Type members
//
function emitPropertySignature(node: PropertySignature) {
emitDecorators(node, node.decorators);
emitModifiers(node, node.modifiers);
emit(node.name);
writeIfPresent(node.questionToken, "?");
emitWithPrefix(": ", node.type);
write(";");
}
function emitPropertyDeclaration(node: PropertyDeclaration) {
emitDecorators(node, node.decorators);
emitModifiers(node, node.modifiers);
emit(node.name);
emitWithPrefix(": ", node.type);
emitExpressionWithPrefix(" = ", node.initializer);
write(";");
}
function emitMethodSignature(node: MethodSignature) {
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: MethodDeclaration) {
emitDecorators(node, node.decorators);
emitModifiers(node, node.modifiers);
writeIfPresent(node.asteriskToken, "*");
emit(node.name);
emitSignatureAndBody(node, emitSignatureHead);
}
function emitConstructor(node: ConstructorDeclaration) {
emitModifiers(node, node.modifiers);
write("constructor");
emitSignatureAndBody(node, emitSignatureHead);
}
2014-07-13 01:04:16 +02:00
function emitAccessorDeclaration(node: AccessorDeclaration) {
emitDecorators(node, node.decorators);
emitModifiers(node, node.modifiers);
write(node.kind === SyntaxKind.GetAccessor ? "get " : "set ");
emit(node.name);
emitSignatureAndBody(node, emitSignatureHead);
}
function emitCallSignature(node: CallSignatureDeclaration) {
emitDecorators(node, node.decorators);
emitModifiers(node, node.modifiers);
emitTypeParameters(node, node.typeParameters);
emitParameters(node, node.parameters);
emitWithPrefix(": ", node.type);
write(";");
}
function emitConstructSignature(node: ConstructSignatureDeclaration) {
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: IndexSignatureDeclaration) {
emitDecorators(node, node.decorators);
emitModifiers(node, node.modifiers);
emitParametersForIndexSignature(node, node.parameters);
emitWithPrefix(": ", node.type);
write(";");
}
function emitSemicolonClassElement() {
write(";");
}
2014-07-13 01:04:16 +02:00
//
// Types
//
2014-07-13 01:04:16 +02:00
function emitTypePredicate(node: TypePredicateNode) {
emit(node.parameterName);
write(" is ");
emit(node.type);
}
function emitTypeReference(node: TypeReferenceNode) {
emit(node.typeName);
emitTypeArguments(node, node.typeArguments);
}
function emitFunctionType(node: FunctionTypeNode) {
emitTypeParameters(node, node.typeParameters);
emitParametersForArrow(node, node.parameters);
write(" => ");
emit(node.type);
}
2014-07-13 01:04:16 +02:00
function emitConstructorType(node: ConstructorTypeNode) {
write("new ");
emitTypeParameters(node, node.typeParameters);
emitParametersForArrow(node, node.parameters);
write(" => ");
emit(node.type);
}
function emitTypeQuery(node: TypeQueryNode) {
write("typeof ");
emit(node.exprName);
}
2015-06-18 23:01:49 +02:00
function emitTypeLiteral(node: TypeLiteralNode) {
write("{");
emitList(node, node.members, ListFormat.TypeLiteralMembers);
write("}");
}
2015-06-18 23:01:49 +02:00
function emitArrayType(node: ArrayTypeNode) {
emit(node.elementType);
write("[]");
}
2015-06-18 23:01:49 +02:00
function emitTupleType(node: TupleTypeNode) {
write("[");
emitList(node, node.elementTypes, ListFormat.TupleTypeElements);
write("]");
}
2015-06-18 23:01:49 +02:00
function emitUnionType(node: UnionTypeNode) {
emitList(node, node.types, ListFormat.UnionTypeConstituents);
}
2015-06-18 23:01:49 +02:00
function emitIntersectionType(node: IntersectionTypeNode) {
emitList(node, node.types, ListFormat.IntersectionTypeConstituents);
}
function emitParenthesizedType(node: ParenthesizedTypeNode) {
write("(");
emit(node.type);
write(")");
}
function emitThisType() {
write("this");
}
function emitTypeOperator(node: TypeOperatorNode) {
writeTokenText(node.operator);
write(" ");
emit(node.type);
}
function emitPropertyAccessType(node: IndexedAccessTypeNode) {
emit(node.objectType);
write("[");
emit(node.indexType);
write("]");
}
2016-08-02 20:45:56 +02:00
function emitLiteralType(node: LiteralTypeNode) {
emitExpression(node.literal);
}
//
// Binding patterns
//
function emitObjectBindingPattern(node: ObjectBindingPattern) {
const elements = node.elements;
if (elements.length === 0) {
write("{}");
}
else {
write("{");
emitList(node, elements, ListFormat.ObjectBindingPatternElements);
write("}");
}
}
function emitArrayBindingPattern(node: ArrayBindingPattern) {
const elements = node.elements;
if (elements.length === 0) {
write("[]");
2015-03-10 23:41:41 +01:00
}
else {
write("[");
emitList(node, node.elements, ListFormat.ArrayBindingPatternElements);
write("]");
}
}
function emitBindingElement(node: BindingElement) {
emitWithSuffix(node.propertyName, ": ");
writeIfPresent(node.dotDotDotToken, "...");
emit(node.name);
emitExpressionWithPrefix(" = ", node.initializer);
}
//
// Expressions
//
function emitArrayLiteralExpression(node: ArrayLiteralExpression) {
const elements = node.elements;
if (elements.length === 0) {
write("[]");
}
else {
const preferNewLine = node.multiLine ? ListFormat.PreferNewLine : ListFormat.None;
emitExpressionList(node, elements, ListFormat.ArrayLiteralExpressionElements | preferNewLine);
}
}
function emitObjectLiteralExpression(node: ObjectLiteralExpression) {
const properties = node.properties;
if (properties.length === 0) {
write("{}");
}
else {
const indentedFlag = getEmitFlags(node) & EmitFlags.Indented;
if (indentedFlag) {
increaseIndent();
}
const preferNewLine = node.multiLine ? ListFormat.PreferNewLine : ListFormat.None;
const allowTrailingComma = languageVersion >= ScriptTarget.ES5 ? ListFormat.AllowTrailingComma : ListFormat.None;
emitList(node, properties, ListFormat.ObjectLiteralExpressionProperties | allowTrailingComma | preferNewLine);
if (indentedFlag) {
decreaseIndent();
2015-03-25 01:00:29 +01:00
}
}
}
function emitPropertyAccessExpression(node: PropertyAccessExpression) {
[Transforms] Merging Master to Transforms on 06/21 (#9294) * Initial support for globs in tsconfig.json * Added caching, more tests * Added stubs to ChakraHost interface for readDirectoryNames/readFileNames * Fixed typos in comments * Changed name of 'reduce' method added to FileSet * Heavily revised implementation that relies on an updated 'readDirectory' API. * more tests * Minor update to shims.ts for forthcoming VS support for globs. * Detailed comments for regular expressions and renamed some files. * Comment cleanup * Fixed new linter warnings * Add upper limit for the program size, fix readDirectory for the symlink files * Add comments * CR feedback / Change upper limit / Add disableSizeLimit compiler option * online and offline CR feedback * Don't count current opened client file if it's TS file * Speed up file searching * Make language service optional for a project * Fix failed tests * Fix project updateing issue after editing config file * Fixing linter and test errors * Bringing back excludes error and fixing faulty test * Fixing lint errors * Passing regular expressions to native hosts * Fix merging issues and multiple project scenario * Refactoring * add test and spit commandLineParser changes to another PR * Fix #8523 * check the declaration and use order if both are not in module file * Type guards using discriminant properties of string literal types * Fix #9098: report missing function impelementation errors for merged classes and namespaces * Narrow type in case/default sections in switch on discriminant property * No implicit returns following exhaustive switch statements * Narrow non-union types to ensure consistent results * Add tests * No Need to store dot token when parsing property access expression * Added tests. * Accepted baselines. * Check tuple types when getting the type node's type. * Accepted baselines. * Fix #9173: clear out lib and types before creating a program in transpileModule * Added tests. * Accepted baselines. * Always check type assertion types. * Clear out unused compiler options when transpiling * Accepted baselines. * improve error message for extending interface * accept baselines * Use helper functions to simplify range tests * Remove String, Number, and Boolean from TypeFlags.Falsy * Add regression test * Accept new baselines * Allow property declarations in .js files * Remove old test * Do not use Object.assing in test * Fix comment * Refactor code to make if statements cheaper * ignore casing when converting a source file path to relative path * add tests & add branches for module interface * Using baselines for transpile unittests (#9195) * Conver to Transpile unittest to use baselines instead * Add baselines * Fix linting error * use resolveEntityName to find interface * add new tests for extends interface * address code style * Refactor navigation bar * routine dom update * Updating readDirectory for tsserverProjectSystem unit tests * Array#map -> ts.map. * Responding to PR feedback * Add conditional index signature for Canvas2DContextAttributes (https://github.com/Microsoft/TypeScript/issues/9244) * Add libcheck tests * Add missing worker types * Accept webworker baselines * Classify `this` in parameter position as a keyword * Adding more matchFiles test cases * Use implicit boolean casts; it doesn't hurt performance * Use getCanonicalFileName * export interface used by other exported functions * Fix from merging with master * Update tests and baselines from merging with master * Remove using dotToken as it is no longer needed * Update baselines from removing dotToken * Address PR: Add NodeEmitFlags to no indent when emit * Address PR; and refactor setting NodeEmitFlags for createMemberAccessForPropertyName * Update baselines
2016-07-11 21:41:12 +02:00
let indentBeforeDot = false;
let indentAfterDot = false;
if (!(getEmitFlags(node) & EmitFlags.NoIndentation)) {
[Transforms] Merging Master to Transforms on 06/21 (#9294) * Initial support for globs in tsconfig.json * Added caching, more tests * Added stubs to ChakraHost interface for readDirectoryNames/readFileNames * Fixed typos in comments * Changed name of 'reduce' method added to FileSet * Heavily revised implementation that relies on an updated 'readDirectory' API. * more tests * Minor update to shims.ts for forthcoming VS support for globs. * Detailed comments for regular expressions and renamed some files. * Comment cleanup * Fixed new linter warnings * Add upper limit for the program size, fix readDirectory for the symlink files * Add comments * CR feedback / Change upper limit / Add disableSizeLimit compiler option * online and offline CR feedback * Don't count current opened client file if it's TS file * Speed up file searching * Make language service optional for a project * Fix failed tests * Fix project updateing issue after editing config file * Fixing linter and test errors * Bringing back excludes error and fixing faulty test * Fixing lint errors * Passing regular expressions to native hosts * Fix merging issues and multiple project scenario * Refactoring * add test and spit commandLineParser changes to another PR * Fix #8523 * check the declaration and use order if both are not in module file * Type guards using discriminant properties of string literal types * Fix #9098: report missing function impelementation errors for merged classes and namespaces * Narrow type in case/default sections in switch on discriminant property * No implicit returns following exhaustive switch statements * Narrow non-union types to ensure consistent results * Add tests * No Need to store dot token when parsing property access expression * Added tests. * Accepted baselines. * Check tuple types when getting the type node's type. * Accepted baselines. * Fix #9173: clear out lib and types before creating a program in transpileModule * Added tests. * Accepted baselines. * Always check type assertion types. * Clear out unused compiler options when transpiling * Accepted baselines. * improve error message for extending interface * accept baselines * Use helper functions to simplify range tests * Remove String, Number, and Boolean from TypeFlags.Falsy * Add regression test * Accept new baselines * Allow property declarations in .js files * Remove old test * Do not use Object.assing in test * Fix comment * Refactor code to make if statements cheaper * ignore casing when converting a source file path to relative path * add tests & add branches for module interface * Using baselines for transpile unittests (#9195) * Conver to Transpile unittest to use baselines instead * Add baselines * Fix linting error * use resolveEntityName to find interface * add new tests for extends interface * address code style * Refactor navigation bar * routine dom update * Updating readDirectory for tsserverProjectSystem unit tests * Array#map -> ts.map. * Responding to PR feedback * Add conditional index signature for Canvas2DContextAttributes (https://github.com/Microsoft/TypeScript/issues/9244) * Add libcheck tests * Add missing worker types * Accept webworker baselines * Classify `this` in parameter position as a keyword * Adding more matchFiles test cases * Use implicit boolean casts; it doesn't hurt performance * Use getCanonicalFileName * export interface used by other exported functions * Fix from merging with master * Update tests and baselines from merging with master * Remove using dotToken as it is no longer needed * Update baselines from removing dotToken * Address PR: Add NodeEmitFlags to no indent when emit * Address PR; and refactor setting NodeEmitFlags for createMemberAccessForPropertyName * Update baselines
2016-07-11 21:41:12 +02:00
const dotRangeStart = node.expression.end;
const dotRangeEnd = skipTrivia(currentText, node.expression.end) + 1;
const dotToken = <Node>{ kind: SyntaxKind.DotToken, pos: dotRangeStart, end: dotRangeEnd };
indentBeforeDot = needsIndentation(node, node.expression, dotToken);
indentAfterDot = needsIndentation(node, dotToken, node.name);
}
emitExpression(node.expression);
increaseIndentIf(indentBeforeDot);
const shouldEmitDotDot = !indentBeforeDot && needsDotDotForPropertyAccess(node.expression);
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: Expression) {
if (expression.kind === SyntaxKind.NumericLiteral) {
// check if numeric literal was originally written with a dot
const text = getLiteralTextOfNode(<LiteralExpression>expression);
return text.indexOf(tokenToString(SyntaxKind.DotToken)) < 0;
}
else if (isPropertyAccessExpression(expression) || isElementAccessExpression(expression)) {
// check if constant enum value is integer
const constantValue = getConstantValue(expression);
// isFinite handles cases when constantValue is undefined
return isFinite(constantValue)
&& Math.floor(constantValue) === constantValue
&& compilerOptions.removeComments;
}
}
2015-06-18 23:01:49 +02:00
function emitElementAccessExpression(node: ElementAccessExpression) {
emitExpression(node.expression);
write("[");
emitExpression(node.argumentExpression);
write("]");
}
2015-03-24 22:16:52 +01:00
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);
}
function emitTaggedTemplateExpression(node: TaggedTemplateExpression) {
emitExpression(node.tag);
write(" ");
emitExpression(node.template);
}
function emitTypeAssertionExpression(node: TypeAssertion) {
if (node.type) {
write("<");
emit(node.type);
write(">");
}
emitExpression(node.expression);
}
2015-03-24 22:16:52 +01:00
function emitParenthesizedExpression(node: ParenthesizedExpression) {
write("(");
emitExpression(node.expression);
write(")");
}
function emitFunctionExpression(node: FunctionExpression) {
emitFunctionDeclarationOrExpression(node);
}
function emitArrowFunction(node: ArrowFunction) {
emitDecorators(node, node.decorators);
emitModifiers(node, node.modifiers);
emitSignatureAndBody(node, emitArrowFunctionHead);
}
2015-03-18 01:09:39 +01:00
function emitArrowFunctionHead(node: ArrowFunction) {
emitTypeParameters(node, node.typeParameters);
emitParametersForArrow(node, node.parameters);
emitWithPrefix(": ", node.type);
write(" =>");
}
2015-03-18 01:09:39 +01:00
function emitDeleteExpression(node: DeleteExpression) {
write("delete ");
emitExpression(node.expression);
}
2015-07-27 13:52:57 +02:00
function emitTypeOfExpression(node: TypeOfExpression) {
write("typeof ");
emitExpression(node.expression);
}
2015-07-27 13:52:57 +02:00
function emitVoidExpression(node: VoidExpression) {
write("void ");
emitExpression(node.expression);
}
2015-07-27 13:52:57 +02:00
function emitAwaitExpression(node: AwaitExpression) {
write("await ");
emitExpression(node.expression);
}
2015-07-27 13:52:57 +02:00
function emitPrefixUnaryExpression(node: PrefixUnaryExpression) {
writeTokenText(node.operator);
if (shouldEmitWhitespaceBeforeOperand(node)) {
write(" ");
}
emitExpression(node.operand);
}
2015-07-27 13:52:57 +02:00
function shouldEmitWhitespaceBeforeOperand(node: PrefixUnaryExpression) {
// 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.
const operand = node.operand;
return operand.kind === SyntaxKind.PrefixUnaryExpression
&& ((node.operator === SyntaxKind.PlusToken && ((<PrefixUnaryExpression>operand).operator === SyntaxKind.PlusToken || (<PrefixUnaryExpression>operand).operator === SyntaxKind.PlusPlusToken))
|| (node.operator === SyntaxKind.MinusToken && ((<PrefixUnaryExpression>operand).operator === SyntaxKind.MinusToken || (<PrefixUnaryExpression>operand).operator === SyntaxKind.MinusMinusToken)));
}
function emitPostfixUnaryExpression(node: PostfixUnaryExpression) {
emitExpression(node.operand);
writeTokenText(node.operator);
}
2015-07-27 13:52:57 +02:00
function emitBinaryExpression(node: BinaryExpression) {
const isCommaOperator = node.operatorToken.kind !== SyntaxKind.CommaToken;
const indentBeforeOperator = needsIndentation(node, node.left, node.operatorToken);
const 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);
}
2015-07-27 13:52:57 +02:00
function emitConditionalExpression(node: ConditionalExpression) {
const indentBeforeQuestion = needsIndentation(node, node.condition, node.questionToken);
const indentAfterQuestion = needsIndentation(node, node.questionToken, node.whenTrue);
const indentBeforeColon = needsIndentation(node, node.whenTrue, node.colonToken);
const 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);
}
2015-07-27 13:52:57 +02:00
function emitTemplateExpression(node: TemplateExpression) {
emit(node.head);
emitList(node, node.templateSpans, ListFormat.TemplateExpressionSpans);
}
2015-07-27 13:52:57 +02:00
function emitYieldExpression(node: YieldExpression) {
write(node.asteriskToken ? "yield*" : "yield");
emitExpressionWithPrefix(" ", node.expression);
}
2015-07-27 13:52:57 +02:00
function emitSpreadExpression(node: SpreadExpression) {
write("...");
emitExpression(node.expression);
}
2015-07-27 13:52:57 +02:00
function emitClassExpression(node: ClassExpression) {
emitClassDeclarationOrExpression(node);
}
function emitExpressionWithTypeArguments(node: ExpressionWithTypeArguments) {
emitExpression(node.expression);
emitTypeArguments(node, node.typeArguments);
}
function emitAsExpression(node: AsExpression) {
emitExpression(node.expression);
if (node.type) {
write(" as ");
emit(node.type);
}
}
2015-07-27 13:52:57 +02:00
function emitNonNullExpression(node: NonNullExpression) {
emitExpression(node.expression);
write("!");
}
2015-07-27 13:52:57 +02:00
//
// Misc
//
2015-07-27 13:52:57 +02:00
function emitTemplateSpan(node: TemplateSpan) {
emitExpression(node.expression);
emit(node.literal);
}
2015-07-27 13:52:57 +02:00
//
// Statements
//
2015-07-27 13:52:57 +02:00
function emitBlock(node: Block) {
if (isSingleLineEmptyBlock(node)) {
writeToken(SyntaxKind.OpenBraceToken, node.pos, /*contextNode*/ node);
write(" ");
writeToken(SyntaxKind.CloseBraceToken, node.statements.end, /*contextNode*/ node);
}
else {
writeToken(SyntaxKind.OpenBraceToken, node.pos, /*contextNode*/ node);
emitBlockStatements(node);
writeToken(SyntaxKind.CloseBraceToken, node.statements.end, /*contextNode*/ node);
}
}
function emitBlockStatements(node: BlockLike) {
if (getEmitFlags(node) & EmitFlags.SingleLine) {
emitList(node, node.statements, ListFormat.SingleLineBlockStatements);
}
else {
emitList(node, node.statements, ListFormat.MultiLineBlockStatements);
2015-01-23 00:58:00 +01:00
}
}
2015-01-23 00:58:00 +01:00
function emitVariableStatement(node: VariableStatement) {
emitModifiers(node, node.modifiers);
emit(node.declarationList);
write(";");
}
2015-01-23 00:58:00 +01:00
function emitEmptyStatement() {
write(";");
}
2015-11-23 21:55:29 +01:00
function emitExpressionStatement(node: ExpressionStatement) {
emitExpression(node.expression);
write(";");
}
function emitIfStatement(node: IfStatement) {
const openParenPos = writeToken(SyntaxKind.IfKeyword, node.pos, node);
write(" ");
writeToken(SyntaxKind.OpenParenToken, openParenPos, node);
emitExpression(node.expression);
writeToken(SyntaxKind.CloseParenToken, node.expression.end, node);
emitEmbeddedStatement(node.thenStatement);
if (node.elseStatement) {
writeLine();
writeToken(SyntaxKind.ElseKeyword, node.thenStatement.end, node);
if (node.elseStatement.kind === SyntaxKind.IfStatement) {
write(" ");
emit(node.elseStatement);
}
else {
emitEmbeddedStatement(node.elseStatement);
}
2015-01-22 23:45:55 +01:00
}
}
function emitDoStatement(node: DoStatement) {
write("do");
emitEmbeddedStatement(node.statement);
if (isBlock(node.statement)) {
write(" ");
}
else {
writeLine();
}
write("while (");
emitExpression(node.expression);
write(");");
}
2014-07-13 01:04:16 +02:00
function emitWhileStatement(node: WhileStatement) {
write("while (");
emitExpression(node.expression);
write(")");
emitEmbeddedStatement(node.statement);
}
2015-01-23 00:58:00 +01:00
function emitForStatement(node: ForStatement) {
const openParenPos = writeToken(SyntaxKind.ForKeyword, node.pos);
write(" ");
writeToken(SyntaxKind.OpenParenToken, openParenPos, /*contextNode*/ node);
emitForBinding(node.initializer);
write(";");
emitExpressionWithPrefix(" ", node.condition);
write(";");
emitExpressionWithPrefix(" ", node.incrementor);
write(")");
emitEmbeddedStatement(node.statement);
}
function emitForInStatement(node: ForInStatement) {
const openParenPos = writeToken(SyntaxKind.ForKeyword, node.pos);
write(" ");
writeToken(SyntaxKind.OpenParenToken, openParenPos);
emitForBinding(node.initializer);
write(" in ");
emitExpression(node.expression);
writeToken(SyntaxKind.CloseParenToken, node.expression.end);
emitEmbeddedStatement(node.statement);
}
2015-11-23 21:55:29 +01:00
function emitForOfStatement(node: ForOfStatement) {
const openParenPos = writeToken(SyntaxKind.ForKeyword, node.pos);
write(" ");
writeToken(SyntaxKind.OpenParenToken, openParenPos);
emitForBinding(node.initializer);
write(" of ");
emitExpression(node.expression);
writeToken(SyntaxKind.CloseParenToken, node.expression.end);
emitEmbeddedStatement(node.statement);
}
2014-07-13 01:04:16 +02:00
function emitForBinding(node: VariableDeclarationList | Expression) {
if (node !== undefined) {
if (node.kind === SyntaxKind.VariableDeclarationList) {
emit(node);
}
else {
emitExpression(<Expression>node);
}
}
}
function emitContinueStatement(node: ContinueStatement) {
writeToken(SyntaxKind.ContinueKeyword, node.pos);
emitWithPrefix(" ", node.label);
write(";");
}
function emitBreakStatement(node: BreakStatement) {
writeToken(SyntaxKind.BreakKeyword, node.pos);
emitWithPrefix(" ", node.label);
write(";");
}
function emitReturnStatement(node: ReturnStatement) {
writeToken(SyntaxKind.ReturnKeyword, node.pos, /*contextNode*/ node);
emitExpressionWithPrefix(" ", node.expression);
write(";");
}
function emitWithStatement(node: WithStatement) {
write("with (");
emitExpression(node.expression);
write(")");
emitEmbeddedStatement(node.statement);
}
function emitSwitchStatement(node: SwitchStatement) {
const openParenPos = writeToken(SyntaxKind.SwitchKeyword, node.pos);
write(" ");
writeToken(SyntaxKind.OpenParenToken, openParenPos);
emitExpression(node.expression);
writeToken(SyntaxKind.CloseParenToken, node.expression.end);
write(" ");
emit(node.caseBlock);
}
function emitLabeledStatement(node: LabeledStatement) {
emit(node.label);
write(": ");
emit(node.statement);
}
function emitThrowStatement(node: ThrowStatement) {
write("throw");
emitExpressionWithPrefix(" ", node.expression);
write(";");
}
function emitTryStatement(node: TryStatement) {
write("try ");
emit(node.tryBlock);
emit(node.catchClause);
if (node.finallyBlock) {
writeLine();
write("finally ");
emit(node.finallyBlock);
}
}
function emitDebuggerStatement(node: DebuggerStatement) {
writeToken(SyntaxKind.DebuggerKeyword, node.pos);
write(";");
}
//
// Declarations
//
function emitVariableDeclaration(node: VariableDeclaration) {
emit(node.name);
emitWithPrefix(": ", node.type);
emitExpressionWithPrefix(" = ", node.initializer);
}
function emitVariableDeclarationList(node: VariableDeclarationList) {
write(isLet(node) ? "let " : isConst(node) ? "const " : "var ");
emitList(node, node.declarations, ListFormat.VariableDeclarationList);
}
function emitFunctionDeclaration(node: FunctionDeclaration) {
emitFunctionDeclarationOrExpression(node);
}
function emitFunctionDeclarationOrExpression(node: FunctionDeclaration | FunctionExpression) {
emitDecorators(node, node.decorators);
emitModifiers(node, node.modifiers);
write(node.asteriskToken ? "function* " : "function ");
emitIdentifierName(node.name);
emitSignatureAndBody(node, emitSignatureHead);
}
function emitSignatureAndBody(node: FunctionLikeDeclaration, emitSignatureHead: (node: SignatureDeclaration) => void) {
const body = node.body;
if (body) {
if (isBlock(body)) {
const indentedFlag = getEmitFlags(node) & EmitFlags.Indented;
if (indentedFlag) {
increaseIndent();
}
if (getEmitFlags(node) & EmitFlags.ReuseTempVariableScope) {
emitSignatureHead(node);
emitBlockFunctionBody(body);
}
else {
const savedTempFlags = tempFlags;
tempFlags = 0;
emitSignatureHead(node);
emitBlockFunctionBody(body);
tempFlags = savedTempFlags;
}
if (indentedFlag) {
decreaseIndent();
}
}
else {
emitSignatureHead(node);
write(" ");
emitExpression(body);
}
}
else {
emitSignatureHead(node);
write(";");
}
}
2015-07-27 13:52:57 +02:00
function emitSignatureHead(node: FunctionDeclaration | FunctionExpression | MethodDeclaration | AccessorDeclaration | ConstructorDeclaration) {
emitTypeParameters(node, node.typeParameters);
emitParameters(node, node.parameters);
emitWithPrefix(": ", node.type);
}
function shouldEmitBlockFunctionBodyOnSingleLine(body: Block) {
// 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.
2015-07-27 13:52:57 +02:00
if (getEmitFlags(body) & EmitFlags.SingleLine) {
return true;
2014-07-13 01:04:16 +02:00
}
if (body.multiLine) {
return false;
2015-03-23 20:37:22 +01:00
}
if (!nodeIsSynthesized(body) && !rangeIsOnSingleLine(body, currentSourceFile)) {
return false;
}
2015-03-23 20:37:22 +01:00
if (shouldWriteLeadingLineTerminator(body, body.statements, ListFormat.PreserveLines)
|| shouldWriteClosingLineTerminator(body, body.statements, ListFormat.PreserveLines)) {
return false;
}
let previousStatement: Statement;
for (const statement of body.statements) {
if (shouldWriteSeparatingLineTerminator(previousStatement, statement, ListFormat.PreserveLines)) {
return false;
}
previousStatement = statement;
}
return true;
}
function emitBlockFunctionBody(body: Block) {
write(" {");
increaseIndent();
emitBodyWithDetachedComments(body, body.statements,
shouldEmitBlockFunctionBodyOnSingleLine(body)
? emitBlockFunctionBodyOnSingleLine
: emitBlockFunctionBodyWorker);
decreaseIndent();
writeToken(SyntaxKind.CloseBraceToken, body.statements.end, body);
}
function emitBlockFunctionBodyOnSingleLine(body: Block) {
emitBlockFunctionBodyWorker(body, /*emitBlockFunctionBodyOnSingleLine*/ true);
}
function emitBlockFunctionBodyWorker(body: Block, emitBlockFunctionBodyOnSingleLine?: boolean) {
// Emit all the prologue directives (like "use strict").
const statementOffset = emitPrologueDirectives(body.statements, /*startWithNewLine*/ true);
const helpersEmitted = emitHelpers(body);
if (statementOffset === 0 && !helpersEmitted && emitBlockFunctionBodyOnSingleLine) {
decreaseIndent();
emitList(body, body.statements, ListFormat.SingleLineFunctionBodyStatements);
increaseIndent();
}
else {
emitList(body, body.statements, ListFormat.MultiLineFunctionBodyStatements, statementOffset);
2015-04-10 21:10:38 +02:00
}
}
2015-04-10 21:10:38 +02:00
function emitClassDeclaration(node: ClassDeclaration) {
emitClassDeclarationOrExpression(node);
}
2015-04-10 21:10:38 +02:00
function emitClassDeclarationOrExpression(node: ClassDeclaration | ClassExpression) {
emitDecorators(node, node.decorators);
emitModifiers(node, node.modifiers);
write("class");
emitNodeWithPrefix(" ", node.name, emitIdentifierName);
2015-04-10 21:10:38 +02:00
const indentedFlag = getEmitFlags(node) & EmitFlags.Indented;
if (indentedFlag) {
increaseIndent();
}
2015-04-10 21:10:38 +02:00
emitTypeParameters(node, node.typeParameters);
emitList(node, node.heritageClauses, ListFormat.ClassHeritageClauses);
2015-04-10 21:10:38 +02:00
const savedTempFlags = tempFlags;
tempFlags = 0;
2015-04-20 22:40:13 +02:00
write(" {");
emitList(node, node.members, ListFormat.ClassMembers);
write("}");
2015-04-10 21:10:38 +02:00
if (indentedFlag) {
decreaseIndent();
2015-04-10 21:10:38 +02:00
}
tempFlags = savedTempFlags;
}
function emitInterfaceDeclaration(node: InterfaceDeclaration) {
emitDecorators(node, node.decorators);
emitModifiers(node, node.modifiers);
write("interface ");
emit(node.name);
emitTypeParameters(node, node.typeParameters);
emitList(node, node.heritageClauses, ListFormat.HeritageClauses);
write(" {");
emitList(node, node.members, ListFormat.InterfaceMembers);
write("}");
}
function emitTypeAliasDeclaration(node: TypeAliasDeclaration) {
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: EnumDeclaration) {
emitModifiers(node, node.modifiers);
write("enum ");
emit(node.name);
const savedTempFlags = tempFlags;
tempFlags = 0;
write(" {");
emitList(node, node.members, ListFormat.EnumMembers);
write("}");
tempFlags = savedTempFlags;
}
function emitModuleDeclaration(node: ModuleDeclaration) {
emitModifiers(node, node.modifiers);
write(node.flags & NodeFlags.Namespace ? "namespace " : "module ");
emit(node.name);
let body = node.body;
while (body.kind === SyntaxKind.ModuleDeclaration) {
write(".");
emit((<ModuleDeclaration>body).name);
body = (<ModuleDeclaration>body).body;
}
write(" ");
emit(body);
}
function emitModuleBlock(node: ModuleBlock) {
if (isEmptyBlock(node)) {
write("{ }");
}
else {
const savedTempFlags = tempFlags;
tempFlags = 0;
write("{");
increaseIndent();
emitBlockStatements(node);
write("}");
tempFlags = savedTempFlags;
}
}
function emitCaseBlock(node: CaseBlock) {
writeToken(SyntaxKind.OpenBraceToken, node.pos);
emitList(node, node.clauses, ListFormat.CaseBlockClauses);
writeToken(SyntaxKind.CloseBraceToken, node.clauses.end);
}
function emitImportEqualsDeclaration(node: ImportEqualsDeclaration) {
emitModifiers(node, node.modifiers);
write("import ");
emit(node.name);
write(" = ");
emitModuleReference(node.moduleReference);
write(";");
}
2015-06-18 23:01:49 +02:00
function emitModuleReference(node: ModuleReference) {
if (node.kind === SyntaxKind.Identifier) {
emitExpression(<Identifier>node);
}
else {
emit(node);
}
}
function emitImportDeclaration(node: ImportDeclaration) {
emitModifiers(node, node.modifiers);
write("import ");
if (node.importClause) {
emit(node.importClause);
write(" from ");
}
emitExpression(node.moduleSpecifier);
write(";");
}
function emitImportClause(node: ImportClause) {
emit(node.name);
if (node.name && node.namedBindings) {
write(", ");
}
emit(node.namedBindings);
}
function emitNamespaceImport(node: NamespaceImport) {
write("* as ");
emit(node.name);
}
2015-06-18 23:01:49 +02:00
function emitNamedImports(node: NamedImports) {
emitNamedImportsOrExports(node);
}
2015-04-29 20:43:23 +02:00
function emitImportSpecifier(node: ImportSpecifier) {
emitImportOrExportSpecifier(node);
}
function emitExportAssignment(node: ExportAssignment) {
write(node.isExportEquals ? "export = " : "export default ");
emitExpression(node.expression);
write(";");
}
function emitExportDeclaration(node: ExportDeclaration) {
write("export ");
if (node.exportClause) {
emit(node.exportClause);
}
else {
write("*");
}
if (node.moduleSpecifier) {
write(" from ");
emitExpression(node.moduleSpecifier);
}
write(";");
}
function emitNamedExports(node: NamedExports) {
emitNamedImportsOrExports(node);
}
2015-04-10 21:10:38 +02:00
function emitExportSpecifier(node: ExportSpecifier) {
emitImportOrExportSpecifier(node);
}
2015-04-10 21:10:38 +02:00
function emitNamedImportsOrExports(node: NamedImportsOrExports) {
write("{");
emitList(node, node.elements, ListFormat.NamedImportsOrExportsElements);
write("}");
}
function emitImportOrExportSpecifier(node: ImportOrExportSpecifier) {
if (node.propertyName) {
emit(node.propertyName);
write(" as ");
}
emit(node.name);
}
//
// Module references
//
2015-04-10 21:10:38 +02:00
function emitExternalModuleReference(node: ExternalModuleReference) {
write("require(");
emitExpression(node.expression);
write(")");
}
//
// JSX
//
2015-04-10 21:10:38 +02:00
function emitJsxElement(node: JsxElement) {
emit(node.openingElement);
emitList(node, node.children, ListFormat.JsxElementChildren);
emit(node.closingElement);
}
function emitJsxSelfClosingElement(node: JsxSelfClosingElement) {
write("<");
emitJsxTagName(node.tagName);
write(" ");
emitList(node, node.attributes, ListFormat.JsxElementAttributes);
write("/>");
}
2015-05-11 07:23:12 +02:00
function emitJsxOpeningElement(node: JsxOpeningElement) {
write("<");
emitJsxTagName(node.tagName);
writeIfAny(node.attributes, " ");
emitList(node, node.attributes, ListFormat.JsxElementAttributes);
write(">");
}
2015-04-22 07:27:33 +02:00
function emitJsxText(node: JsxText) {
writer.writeLiteral(getTextOfNode(node, /*includeTrivia*/ true));
}
2015-04-10 21:10:38 +02:00
function emitJsxClosingElement(node: JsxClosingElement) {
write("</");
emitJsxTagName(node.tagName);
write(">");
}
2015-04-22 07:27:33 +02:00
function emitJsxAttribute(node: JsxAttribute) {
emit(node.name);
emitWithPrefix("=", node.initializer);
}
function emitJsxSpreadAttribute(node: JsxSpreadAttribute) {
write("{...");
emitExpression(node.expression);
write("}");
}
function emitJsxExpression(node: JsxExpression) {
if (node.expression) {
write("{");
emitExpression(node.expression);
write("}");
}
}
2015-04-22 07:27:33 +02:00
[Transforms] Merge master 07/11 into transform (#9697) * Use merge2, gulp-if, gulp-newer, and more projects * Add watch task * Working non-inline sourcemaps for runtests * browser tests now also loads sourcemaps from disk * Lazypipes and better services stream management * export interface used by other exported functions * Make goto-definition work for `this` parameter * Add new error for rest parameters * Add error message for rest parameter properties * Fix case when a document contains multiple script blocks with different base indentations. Use the base indent size if it is greater that the indentation of the inherited predecessor * Fix rwc-runner from breaking change in compiler (#9284) * Signatures use JSDoc to determine optionality * Changed implementation to use closure * Updated tests * Fixed linting error * Adding Code of Conduct notice * Don't crash when JS class property is self-referential. Fixes #9293 * Remove stale baselines * For optionality, check question token before JSDoc * Accept rest parameter properties error baselines * Change binding pattern parameter property error * Accept binding pattern properties error baselines * Lint * Port the sync version diagnostics API from tsserverVS-WIP branch to 2.0 * Do copyright without gulp-if and lazypipe * Change test comment and accept baseline * Remove tsd scripts task from gulpfile * Make use of module compiler option explicit, add strip internal to tsconfigs * Remove Signature#thisType and use Signature#thisParameter everywhere * Add Gulpfile lint to jake, fix lints * Change reference tests to verify actual ranges referenced and not just their count * Respond to PR comments * Add new lint rule * Fix object whitespace lints * Fix case of gulpfile dependencies * 1. pass subshell args 2. fix build order in services 1. /bin/sh requires its arguments joined into a single string unlike cmd. 2. services/ depends on a couple of files from server/ but the order was implicit, and changed from jakefile. Now the order is explicit in the tsconfig. * Fix single-quote lint * Check for exactly one space * Fix excess whitespace issues * Add matchFiles test to Gulpfile This was merged while the gulpfile was still in-progress * Fix LKG useDebug task and newLine flag * Update LKG * Clean before LKG in Gulpfile * Fix lint * Correct the api string name * Allow space in exec cmds * Fix typo * Add new APIs to protocol * Fix bug where `exports.` was prepended to namespace export accesses * Remove unnecessary parameter * extract expression into function * Add fourslash tests & address CR comments * Fix 8549: Using variable as Jsx tagname (#9337) * Parse JSXElement's name as property access instead of just entity name. So when one accesses property of the class through this, checker will check correctly * wip - just resolve to any type for now * Resolve string type to anytype and look up property in intrinsicElementsType of Jsx * Add tests and update baselines * Remove unneccessary comment * wip-address PR * Address PR * Add tets and update baselines * Fix linting error * Unused identifiers compiler code (#9200) * Code changes to update references of the Identifiers * Added code for handling function, method and coonstructor level local variables and parameters * Rebased with origin master * Code changes to handle unused private variables, private methods and typed parameters * Code changes to handle namespace level elements * Code changes to handle unimplemented interfaces * Code to optimize the d.ts check * Correct Code change to handle the parameters for methods inside interfaces * Fix for lint error * Remove Trailing whitespace * Code changes to handle interface implementations * Changes to display the error position correctly * Compiler Test Cases * Adding condition to ignore constructor parameters * Removing unnecessary tests * Additional changes for compiler code * Additional changes to handle constructor scenario * Fixing the consolidated case * Changed logic to search for private instead of public * Response to PR Comments * Changed the error code in test cases as result of merge with master * Adding the missing file * Adding the missing file II * Response to PR comments * Code changes for checking unused imports * Test Cases for Unused Imports * Response to PR comments * Code change specific to position of Import Declaration * Code change for handling the position for unused import * New scenarios for handling parameters in lambda function, type parameters in methods, etc. * Additional scenarios based on PR comments * Removing a redundant check * Added ambient check to imports and typeparatmeter reporting * Added one more scenario to handle type parameters * Added new scenario for TypeParameter on Interface * Refactoring the code * Added scenario to handle private class elements declared in constructor. * Minor change to erro reporting * Fix 8355: Fix emit metadata different between transpile and tsc --isolatedModule (#9232) * Instead of returning undefined for unknownSymbol return itself * Add Transpile unittest * Wip - Add project tests * Add project tests and baselines * Update existed tests * Add tests for emitting metadata with module targetting system * Fix 8467: Fix incorrect emit for accessing static property in static propertyDeclaration (#8551) * Fix incorrect emit for accessing static property in static propertyDeclaration * Update tests and baselines * Update function name * Fix when accessing static property inside arrow function * Add tests and baselines * do not format comma/closeparen in jsxelement * format jsx expression * Remove extra baselines * Fixed bugs and linting * Added project tests for node_modules JavaScript searches * Removed old TODO comment * make rules optional * Fixed the regexp for removing full paths * Fix type of the disableSizeLimit option * Update version to 2.0.0 * Remove upper boilerplate from issue template Our issue stats did not improve appreciably when we added the issue template. Reduce upper boilerplate text and try to make it more action-oriented * Remove unused compiler option (#9381) * Update LKG * Added emitHost method to return source from node modules * Marked new method internal * Update issue_template.md * new options should be optional for compatibility * Add getCurrentDirectory to ServerHost * Add nullchecks for typeRoots, remove getCurrentDirectory from ServerHost as it is always the installation location * VarDate interface and relevant Date.prototype members * Port 9396 to release 2.0 * Fix 9363: Object destructuring broken-variables are bound to the wrong object (#9383) * Fix emit incorrect destructuring mapping in var declaration * Add tests and baselines * Add additional tests and baselines * Fix crash in async functions when targetting ES5. When targetting ES5 and with --noImplicitReturns, an async function whose return type could not be determined would cause a compiler crash. * Add This type to lib * Merge master into release-2.0 (#9400) * do not format comma/closeparen in jsxelement * format jsx expression * make rules optional * Remove upper boilerplate from issue template Our issue stats did not improve appreciably when we added the issue template. Reduce upper boilerplate text and try to make it more action-oriented * Update issue_template.md * new options should be optional for compatibility * Add getCurrentDirectory to ServerHost * Add nullchecks for typeRoots, remove getCurrentDirectory from ServerHost as it is always the installation location * VarDate interface and relevant Date.prototype members * Fix 9363: Object destructuring broken-variables are bound to the wrong object (#9383) * Fix emit incorrect destructuring mapping in var declaration * Add tests and baselines * Add additional tests and baselines * Fix #9402: Do not report unused identifier errors for catch variables * getVarDate should be on the Date interface * Defere checking unsed identifier checks * Do not scan nodes preceding formatted region, just skip over them * Don't emit source files found under node_modules * Destructuring assignment removes undefined from type when default value is given * Add nullcheck when calculating indentations for implort clause * Use a deferred list to check for unused identifiers * push checks to checkUnusedIdentifiersDeferred * use isParameterPropertyDeclaration to test for paramter propoerties * runtests-parallel skips empty buckets Previously, it would enter them as buckets with no tests, which would make our test runners run *every* test. This was very obvious on machines with lots of cores. * Report unused identifiers in for statements * Do not check ambients, and overloads * Add tests * Consolidate type reference marking in getTypeFromTypeReference * Handel type aliases * Add tests * Add test * Dont load JavaScript if types packages are present * Renamed API * Use checkExpression, not checkExpressionCached * Do not report unused errors for module augmentations * Consolidate refernce marking in resolveName to allow marking aliases correctelly * add tests * Code review comments * Only mark symbols found in a local symbol table * Show "<unknown>" if the name of a declaration is unavailable * Parse `export default async function` as a declaration * Respond to PR comments * Better name for test * handel private properties correctelly * Port 9426 to release 2.0 * Handel Swtich statements check for locals on for statments only mark private properties * Removed one error to avoid full path issues * Don't emit source files found under node_modules (cherry picked from commit 5f8cf1af3e4be61037cbafd698535d32d292941f) * Dont load JavaScript if types packages are present (cherry picked from commit 5a45c44eb789f52ceb1aa0e23a230ecb599bfb08) * Renamed API (cherry picked from commit d8047b607f11cdf319284bb344282582c7c0aea0) * Removed one error to avoid full path issues (cherry picked from commit 5e4f13f342a75ec8f7cf65cb669bec9d6e6c5581) * Fix incorrectly-saved quote symbols in ThirdPartyNoticeText.txt * Fix #9458: exclude parameters starting with underscore from unusedParamter checks * change variable name for strict mode * Increase timeout from running RWC. As UWDWeb takes slightly longer now (#9454) * Handle relative paths in tsconfig exclude and include globs * Merge master into release branch 06/30 (#9447) * do not format comma/closeparen in jsxelement * format jsx expression * make rules optional * Remove upper boilerplate from issue template Our issue stats did not improve appreciably when we added the issue template. Reduce upper boilerplate text and try to make it more action-oriented * Update issue_template.md * new options should be optional for compatibility * Add getCurrentDirectory to ServerHost * Add nullchecks for typeRoots, remove getCurrentDirectory from ServerHost as it is always the installation location * VarDate interface and relevant Date.prototype members * Fix 9363: Object destructuring broken-variables are bound to the wrong object (#9383) * Fix emit incorrect destructuring mapping in var declaration * Add tests and baselines * Add additional tests and baselines * Fix crash in async functions when targetting ES5. When targetting ES5 and with --noImplicitReturns, an async function whose return type could not be determined would cause a compiler crash. * Add This type to lib * getVarDate should be on the Date interface * Don't emit source files found under node_modules * Destructuring assignment removes undefined from type when default value is given * Add nullcheck when calculating indentations for implort clause * Add test * Dont load JavaScript if types packages are present * Renamed API * Use checkExpression, not checkExpressionCached * Show "<unknown>" if the name of a declaration is unavailable * Parse `export default async function` as a declaration * Removed one error to avoid full path issues * Fix incorrectly-saved quote symbols in ThirdPartyNoticeText.txt * Improve names of whitespace functions * Handle relative paths in tsconfig exclude and include globs Port 9475 to release 2.0 * add new method getEmitOutputObject to return result of the emit as object with properties instead of json string * fix linter * Fix PromiseLike to be compatible with es6-promise (#9484) * Fix reading files from IOLog because previous our API captures (#9483) * Fix reading files from IOLog because previous our API captures * Refactoring the ioLog * Exclude FlowSwitchClause from flow graph for case expressions * Add regression test * Update LKG * Update language in comment * Add .mailmap file * Add authors script to generate authors from repo * Update AUTHORS.md for release-2.0 * Update script to pass more than one argument * Remove the unused text buffer from ScriptInfo * Fix #9531: account for async as an contextual keyword when parsing export assignments * Update LKG * Swap q from a reference to an import * Fix #9550: exclude 'this' type parameters from unusedParameters checks. * Update comment to reflect new dependency * Avoid putting children tags in jsdoccomment * Parse the result of getDirectories call * Update harness getDirectories implementation for shims * Fix multiple Salsa assignment-declarations Previously, all assignment-declarations needed to be of the same kind: either all `this.p = ...` assignments or `C.prototype.p = ...` assignments. * Test for multiple salsa assignment-declarations * Add test for parsed @typedef tag node shape * Provide a symbol for salsa-inferred class types * Update .mailmap * Fix module tracking * Updated test with relative import * Fixed the node tracking and a harness bug * fixed lint error * Fixed implicit any * Added missing test files * Removed duplicate logic * Update conflicting baseline. PR #9574 added a baseline that #9578 caused to be changed. The two PRs went in so close to each other that the CI build didn't catch the change to the new test's baseline. * Fix type of JSXTagName * Update baselines to use double-quote * Update baselines when emitting metadata decorator * Update baselines for async-await function * Update baselines for comment in capturing down-level for...of and for...in * Add missing Transpile tests * Remove old JS transpile baselines * Passing program as argument in emitWorker * Port PR#9607 transforms * Port new JSDOC tests to use baseline * substitute alias for class expression in statics * Address new lint warnings * Change name for substitution function.
2016-07-19 00:38:30 +02:00
function emitJsxTagName(node: JsxTagNameExpression) {
if (node.kind === SyntaxKind.Identifier) {
emitExpression(<Identifier>node);
}
else {
emit(node);
}
}
//
// Clauses
//
function emitCaseClause(node: CaseClause) {
write("case ");
emitExpression(node.expression);
write(":");
2015-04-11 15:33:09 +02:00
emitCaseOrDefaultClauseStatements(node, node.statements);
}
2015-04-22 07:27:33 +02:00
function emitDefaultClause(node: DefaultClause) {
write("default:");
emitCaseOrDefaultClauseStatements(node, node.statements);
}
2015-07-27 13:52:57 +02:00
function emitCaseOrDefaultClauseStatements(parentNode: Node, statements: NodeArray<Statement>) {
const emitAsSingleStatement =
statements.length === 1 &&
(
// treat synthesized nodes as located on the same line for emit purposes
nodeIsSynthesized(parentNode) ||
nodeIsSynthesized(statements[0]) ||
rangeStartPositionsAreOnSameLine(parentNode, statements[0], currentSourceFile)
);
if (emitAsSingleStatement) {
write(" ");
emit(statements[0]);
}
else {
emitList(parentNode, statements, ListFormat.CaseOrDefaultClauseStatements);
}
}
2015-07-27 13:52:57 +02:00
function emitHeritageClause(node: HeritageClause) {
write(" ");
writeTokenText(node.token);
write(" ");
emitList(node, node.types, ListFormat.HeritageClauseTypes);
}
2015-04-10 21:10:38 +02:00
function emitCatchClause(node: CatchClause) {
writeLine();
const openParenPos = writeToken(SyntaxKind.CatchKeyword, node.pos);
write(" ");
writeToken(SyntaxKind.OpenParenToken, openParenPos);
emit(node.variableDeclaration);
writeToken(SyntaxKind.CloseParenToken, node.variableDeclaration ? node.variableDeclaration.end : openParenPos);
write(" ");
emit(node.block);
}
2015-04-10 21:10:38 +02:00
//
// Property assignments
//
function emitPropertyAssignment(node: PropertyAssignment) {
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.
const initializer = node.initializer;
2016-09-26 22:52:09 +02:00
if ((getEmitFlags(initializer) & EmitFlags.NoLeadingComments) === 0) {
const commentRange = getCommentRange(initializer);
emitTrailingCommentsOfPosition(commentRange.pos);
}
emitExpression(initializer);
}
2015-04-10 21:10:38 +02:00
function emitShorthandPropertyAssignment(node: ShorthandPropertyAssignment) {
emit(node.name);
if (node.objectAssignmentInitializer) {
write(" = ");
emitExpression(node.objectAssignmentInitializer);
2015-04-10 21:10:38 +02:00
}
}
2015-04-10 21:10:38 +02:00
//
// Enum
//
function emitEnumMember(node: EnumMember) {
emit(node.name);
emitExpressionWithPrefix(" = ", node.initializer);
}
2015-04-10 21:10:38 +02:00
//
// Top-level nodes
//
function emitSourceFile(node: SourceFile) {
writeLine();
emitShebang();
emitBodyWithDetachedComments(node, node.statements, emitSourceFileWorker);
}
function emitSourceFileWorker(node: SourceFile) {
const statements = node.statements;
const statementOffset = emitPrologueDirectives(statements);
const savedTempFlags = tempFlags;
tempFlags = 0;
emitHelpers(node);
emitList(node, statements, ListFormat.MultiLine, statementOffset);
tempFlags = savedTempFlags;
}
// Transformation nodes
function emitPartiallyEmittedExpression(node: PartiallyEmittedExpression) {
emitExpression(node.expression);
}
2015-04-10 21:10:38 +02:00
/**
* Emits any prologue directives at the start of a Statement list, returning the
* number of prologue directives written to the output.
*/
function emitPrologueDirectives(statements: Node[], startWithNewLine?: boolean): number {
for (let i = 0; i < statements.length; i++) {
if (isPrologueDirective(statements[i])) {
if (startWithNewLine || i > 0) {
writeLine();
}
emit(statements[i]);
}
else {
// return index of the first non prologue directive
return i;
}
}
2015-04-10 21:10:38 +02:00
return statements.length;
}
2015-08-07 01:38:53 +02:00
function emitHelpers(node: Node) {
const emitFlags = getEmitFlags(node);
let helpersEmitted = false;
if (emitFlags & EmitFlags.EmitEmitHelpers) {
helpersEmitted = emitEmitHelpers(currentSourceFile);
2015-04-10 21:10:38 +02:00
}
if (emitFlags & EmitFlags.EmitExportStar) {
writeLines(exportStarHelper);
helpersEmitted = true;
2015-04-10 21:10:38 +02:00
}
if (emitFlags & EmitFlags.EmitSuperHelper) {
writeLines(superHelper);
helpersEmitted = true;
2015-11-02 21:53:27 +01:00
}
if (emitFlags & EmitFlags.EmitAdvancedSuperHelper) {
writeLines(advancedSuperHelper);
helpersEmitted = true;
}
return helpersEmitted;
}
2015-08-07 01:38:53 +02:00
function emitEmitHelpers(node: SourceFile) {
// 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
&& (isExternalModule(node) || compilerOptions.isolatedModules)) {
return false;
}
2015-08-17 19:37:44 +02:00
let helpersEmitted = false;
// Only Emit __extends function when target ES5.
// For target ES6 and above, we can emit classDeclaration as is.
2016-10-13 13:32:00 +02:00
if ((languageVersion < ScriptTarget.ES2015) && (!extendsEmitted && node.flags & NodeFlags.HasClassExtends)) {
writeLines(extendsHelper);
extendsEmitted = true;
helpersEmitted = true;
}
2015-04-10 21:10:38 +02:00
if (compilerOptions.jsx !== JsxEmit.Preserve && !assignEmitted && (node.flags & NodeFlags.HasSpreadAttribute)) {
writeLines(assignHelper);
assignEmitted = true;
}
if (!decorateEmitted && node.flags & NodeFlags.HasDecorators) {
writeLines(decorateHelper);
if (compilerOptions.emitDecoratorMetadata) {
writeLines(metadataHelper);
}
decorateEmitted = true;
helpersEmitted = true;
}
if (!paramEmitted && node.flags & NodeFlags.HasParamDecorators) {
writeLines(paramHelper);
paramEmitted = true;
helpersEmitted = true;
}
2015-04-17 02:32:40 +02:00
2016-10-05 17:09:58 +02:00
// 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);
2016-10-13 13:32:00 +02:00
if (languageVersion < ScriptTarget.ES2015) {
writeLines(generatorHelper);
}
awaiterEmitted = true;
helpersEmitted = true;
}
if (helpersEmitted) {
writeLine();
}
2015-04-17 02:32:40 +02:00
return helpersEmitted;
}
function writeLines(text: string): void {
const lines = text.split(/\r\n|\r|\n/g);
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (line.length) {
if (i > 0) {
writeLine();
}
write(line);
}
}
}
//
// Helpers
//
2015-04-20 22:40:13 +02:00
function emitShebang() {
const shebang = getShebang(currentText);
if (shebang) {
write(shebang);
writeLine();
}
}
function emitModifiers(node: Node, modifiers: NodeArray<Modifier>) {
if (modifiers && modifiers.length) {
emitList(node, modifiers, ListFormat.Modifiers);
write(" ");
}
}
function emitWithPrefix(prefix: string, node: Node) {
emitNodeWithPrefix(prefix, node, emit);
}
function emitExpressionWithPrefix(prefix: string, node: Node) {
emitNodeWithPrefix(prefix, node, emitExpression);
}
function emitNodeWithPrefix(prefix: string, node: Node, emit: (node: Node) => void) {
if (node) {
write(prefix);
emit(node);
}
}
function emitWithSuffix(node: Node, suffix: string) {
if (node) {
emit(node);
write(suffix);
}
}
function emitEmbeddedStatement(node: Statement) {
if (isBlock(node)) {
write(" ");
emit(node);
}
else {
writeLine();
increaseIndent();
emit(node);
decreaseIndent();
}
}
function emitDecorators(parentNode: Node, decorators: NodeArray<Decorator>) {
emitList(parentNode, decorators, ListFormat.Decorators);
}
function emitTypeArguments(parentNode: Node, typeArguments: NodeArray<TypeNode>) {
emitList(parentNode, typeArguments, ListFormat.TypeArguments);
}
function emitTypeParameters(parentNode: Node, typeParameters: NodeArray<TypeParameterDeclaration>) {
emitList(parentNode, typeParameters, ListFormat.TypeParameters);
}
function emitParameters(parentNode: Node, parameters: NodeArray<ParameterDeclaration>) {
emitList(parentNode, parameters, ListFormat.Parameters);
}
function emitParametersForArrow(parentNode: Node, parameters: NodeArray<ParameterDeclaration>) {
if (parameters &&
parameters.length === 1 &&
parameters[0].type === undefined &&
parameters[0].pos === parentNode.pos) {
emit(parameters[0]);
2015-03-17 03:25:02 +01:00
}
else {
emitParameters(parentNode, parameters);
}
}
function emitParametersForIndexSignature(parentNode: Node, parameters: NodeArray<ParameterDeclaration>) {
emitList(parentNode, parameters, ListFormat.IndexSignatureParameters);
}
2015-06-18 23:01:49 +02:00
function emitList(parentNode: Node, children: NodeArray<Node>, format: ListFormat, start?: number, count?: number) {
emitNodeList(emit, parentNode, children, format, start, count);
}
function emitExpressionList(parentNode: Node, children: NodeArray<Node>, format: ListFormat, start?: number, count?: number) {
emitNodeList(emitExpression, parentNode, children, format, start, count);
}
2015-06-18 23:01:49 +02:00
function emitNodeList(emit: (node: Node) => void, parentNode: Node, children: NodeArray<Node>, format: ListFormat, start = 0, count = children ? children.length - start : 0) {
const isUndefined = children === undefined;
if (isUndefined && format & ListFormat.OptionalIfUndefined) {
return;
}
const isEmpty = isUndefined || children.length === 0 || start >= children.length || count === 0;
if (isEmpty && format & ListFormat.OptionalIfEmpty) {
return;
2015-06-18 23:01:49 +02:00
}
if (format & ListFormat.BracketsMask) {
write(getOpeningBracket(format));
}
2016-01-27 07:59:34 +01:00
if (isEmpty) {
// Write a line terminator if the parent node was multi-line
if (format & ListFormat.MultiLine) {
writeLine();
2016-01-27 07:59:34 +01:00
}
else if (format & ListFormat.SpaceBetweenBraces) {
write(" ");
2015-06-18 23:01:49 +02:00
}
}
else {
// Write the opening line terminator or leading whitespace.
const mayEmitInterveningComments = (format & ListFormat.NoInterveningComments) === 0;
let shouldEmitInterveningComments = mayEmitInterveningComments;
if (shouldWriteLeadingLineTerminator(parentNode, children, format)) {
writeLine();
shouldEmitInterveningComments = false;
}
else if (format & ListFormat.SpaceBetweenBraces) {
write(" ");
2015-06-18 23:01:49 +02:00
}
// Increase the indent, if requested.
if (format & ListFormat.Indented) {
increaseIndent();
2015-06-18 23:01:49 +02:00
}
// Emit each child.
let previousSibling: Node;
let shouldDecreaseIndentAfterEmit: boolean;
const delimiter = getDelimiter(format);
for (let i = 0; i < count; i++) {
const 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 & (ListFormat.LinesMask | ListFormat.Indented)) === ListFormat.SingleLine) {
increaseIndent();
shouldDecreaseIndentAfterEmit = true;
}
writeLine();
shouldEmitInterveningComments = false;
}
else if (previousSibling && format & ListFormat.SpaceBetweenSiblings) {
write(" ");
}
}
2014-07-12 01:36:06 +02:00
if (shouldEmitInterveningComments) {
const commentRange = getCommentRange(child);
emitTrailingCommentsOfPosition(commentRange.pos);
}
else {
shouldEmitInterveningComments = mayEmitInterveningComments;
}
// Emit this child.
emit(child);
if (shouldDecreaseIndentAfterEmit) {
decreaseIndent();
shouldDecreaseIndentAfterEmit = false;
}
previousSibling = child;
}
2015-07-09 23:44:47 +02:00
// Write a trailing comma, if requested.
const hasTrailingComma = (format & ListFormat.AllowTrailingComma) && children.hasTrailingComma;
if (format & ListFormat.CommaDelimited && hasTrailingComma) {
write(",");
}
// Decrease the indent, if requested.
if (format & ListFormat.Indented) {
decreaseIndent();
}
// Write the closing line terminator or closing whitespace.
if (shouldWriteClosingLineTerminator(parentNode, children, format)) {
writeLine();
}
else if (format & ListFormat.SpaceBetweenBraces) {
write(" ");
}
}
2014-07-13 01:04:16 +02:00
if (format & ListFormat.BracketsMask) {
write(getClosingBracket(format));
2014-07-13 01:04:16 +02:00
}
}
2014-07-13 01:04:16 +02:00
function writeIfAny(nodes: NodeArray<Node>, text: string) {
if (nodes && nodes.length > 0) {
write(text);
}
}
function writeIfPresent(node: Node, text: string) {
if (node !== undefined) {
write(text);
}
}
function writeToken(token: SyntaxKind, pos: number, contextNode?: Node) {
2016-09-26 22:52:09 +02:00
return emitTokenWithSourceMap(contextNode, token, pos, writeTokenText);
}
2015-01-23 00:58:00 +01:00
function writeTokenText(token: SyntaxKind, pos?: number) {
const tokenString = tokenToString(token);
write(tokenString);
2016-09-27 00:21:03 +02:00
return pos < 0 ? pos : pos + tokenString.length;
}
2015-01-23 00:58:00 +01:00
function increaseIndentIf(value: boolean, valueToWriteWhenNotIndenting?: string) {
if (value) {
increaseIndent();
writeLine();
}
else if (valueToWriteWhenNotIndenting) {
write(valueToWriteWhenNotIndenting);
}
}
// 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: boolean, value2?: boolean) {
if (value1) {
decreaseIndent();
}
if (value2) {
decreaseIndent();
}
}
2015-01-23 00:58:00 +01:00
function shouldWriteLeadingLineTerminator(parentNode: Node, children: NodeArray<Node>, format: ListFormat) {
if (format & ListFormat.MultiLine) {
return true;
}
2015-01-23 00:58:00 +01:00
if (format & ListFormat.PreserveLines) {
if (format & ListFormat.PreferNewLine) {
return true;
2015-01-23 00:58:00 +01:00
}
const firstChild = children[0];
if (firstChild === undefined) {
return !rangeIsOnSingleLine(parentNode, currentSourceFile);
}
else if (positionIsSynthesized(parentNode.pos) || nodeIsSynthesized(firstChild)) {
return synthesizedNodeStartsOnNewLine(firstChild, format);
}
else {
return !rangeStartPositionsAreOnSameLine(parentNode, firstChild, currentSourceFile);
}
}
else {
return false;
}
}
function shouldWriteSeparatingLineTerminator(previousNode: Node, nextNode: Node, format: ListFormat) {
if (format & ListFormat.MultiLine) {
2015-01-23 00:58:00 +01:00
return true;
}
else if (format & ListFormat.PreserveLines) {
if (previousNode === undefined || nextNode === undefined) {
return false;
}
else if (nodeIsSynthesized(previousNode) || nodeIsSynthesized(nextNode)) {
return synthesizedNodeStartsOnNewLine(previousNode, format) || synthesizedNodeStartsOnNewLine(nextNode, format);
}
else {
return !rangeEndIsOnSameLineAsRangeStart(previousNode, nextNode, currentSourceFile);
2014-08-07 03:42:14 +02:00
}
}
else {
return nextNode.startsOnNewLine;
}
}
function shouldWriteClosingLineTerminator(parentNode: Node, children: NodeArray<Node>, format: ListFormat) {
if (format & ListFormat.MultiLine) {
return (format & ListFormat.NoTrailingNewLine) === 0;
2014-07-13 01:04:16 +02:00
}
else if (format & ListFormat.PreserveLines) {
if (format & ListFormat.PreferNewLine) {
return true;
}
2014-07-13 01:04:16 +02:00
const lastChild = lastOrUndefined(children);
if (lastChild === undefined) {
return !rangeIsOnSingleLine(parentNode, currentSourceFile);
}
else if (positionIsSynthesized(parentNode.pos) || nodeIsSynthesized(lastChild)) {
return synthesizedNodeStartsOnNewLine(lastChild, format);
}
else {
return !rangeEndPositionsAreOnSameLine(parentNode, lastChild, currentSourceFile);
2015-03-18 03:25:40 +01:00
}
}
else {
return false;
2015-03-18 03:25:40 +01:00
}
}
2015-03-18 03:25:40 +01:00
function synthesizedNodeStartsOnNewLine(node: Node, format?: ListFormat) {
if (nodeIsSynthesized(node)) {
const startsOnNewLine = node.startsOnNewLine;
if (startsOnNewLine === undefined) {
return (format & ListFormat.PreferNewLine) !== 0;
}
return startsOnNewLine;
}
2014-08-07 02:58:03 +02:00
return (format & ListFormat.PreferNewLine) !== 0;
}
function needsIndentation(parent: Node, node1: Node, node2: Node): boolean {
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 !nodeIsSynthesized(parent)
&& !nodeIsSynthesized(node1)
&& !nodeIsSynthesized(node2)
&& !rangeEndIsOnSameLineAsRangeStart(node1, node2, currentSourceFile);
}
function skipSynthesizedParentheses(node: Node) {
while (node.kind === SyntaxKind.ParenthesizedExpression && nodeIsSynthesized(node)) {
node = (<ParenthesizedExpression>node).expression;
}
return node;
}
function getTextOfNode(node: Node, includeTrivia?: boolean): string {
if (isGeneratedIdentifier(node)) {
[Transforms] Merge master 08/09 (#10263) * Improve error message * Remove `SupportedExpressionWithTypeArguments` type; just check that the expression of each `ExpressionWithTypeArguments` is an `EntityNameExpression`. * Fix bug * Fix #10083 - allowSyntheticDefaultImports alters getExternalModuleMember (#10096) * Use recursion, and fix error for undefined node * Rename function * Fix lint error * Narrowing type parameter intersects w/narrowed types This makes sure that a union type that includes a type parameter is still usable as the actual type that the type guard narrows to. * Don't allow ".d.ts" extension in an import either. * Add a helper function `getOrUpdateProperty` to prevent unprotected access to Maps. * Limit type guards as assertions to incomplete types in loops * Accept new baselines * Fix linting error * Allow JS multiple declarations of ctor properties When a property is declared in the constructor and on the prototype of an ES6 class, the property's symbol is discarded in favour of the method's symbol. That because the usual use for this pattern is to bind an instance function: `this.m = this.m.bind(this)`. In this case the type you want really is the method's type. * Use {} type facts for unconstrained type params Previously it was using TypeFacts.All. But the constraint of an unconstrained type parameter is actually {}. * Fix newline lint * Test that declares conflicting method first * [Release-2.0] Fix 9662: Visual Studio 2015 with TS2.0 gives incorrect @types path resolution errors (#9867) * Change the shape of the shim layer to support getAutomaticTypeDirectives * Change the key for looking up automatic type-directives * Update baselines from change look-up name of type-directives * Add @currentDirectory into the test * Update baselines * Fix linting error * Address PR: fix spelling mistake * Instead of return path of the type directive names just return type directive names * Remove unused reference files: these tests produce erros so they will not produce these files (#9233) * Add string-literal completion test for jsdoc * Support other (new) literal types in jsdoc * Don't allow properties inherited from Object to be automatically included in TSX attributes * Add new test baseline and delete else in binder The extra `else` caused a ton of test failures! * Fix lint * Port PR #10016 to Master (#10100) * Treat namespaceExportDeclaration as declaration * Update baselines * wip - add tests * Add tests * Show "export namespace" for quick-info * Fix more lint * Try using runtests-parallel for CI (#9970) * Try using runtests-parallel for CI * Put worker count setting into .travis.yml * Reduce worker count to 4 - 8 wasnt much different from 4-6 but had contention issues causing timeouts * Fix lssl task (#9967) * Surface noErrorTruncation option * Stricter check for discriminant properties in type guards * Add tests * Emit more efficient/concise "empty" ES6 ctor When there are property assignments in a the class body of an inheriting class, tsc current emit the following compilation: ```ts class Foo extends Bar { public foo = 1; } ``` ```js class Foo extends Bar { constructor(…args) { super(…args); this.foo = 1; } } ``` This introduces an unneeded local variable and might force a reification of the `arguments` object (or otherwise reify the arguments into an array). This is particularly bad when that output is fed into another transpiler like Babel. In Babel, you get something like this today: ```js var Foo = (function (_Bar) { _inherits(Foo, _Bar); function Foo() { _classCallCheck(this, Foo); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _Bar.call.apply(_Bar, [this].concat(args)); this.foo = 1; } return Foo; })(Bar); ``` This causes a lot of needless work/allocations and some very strange code (`.call.apply` o_0). Admittedly, this is not strictly tsc’s problem; it could have done a deeper analysis of the code and optimized out the extra dance. However, tsc could also have emitted this simpler, more concise and semantically equivalent code in the first place: ```js class Foo extends Bar { constructor() { super(…arguments); this.foo = 1; } } ``` Which compiles into the following in Babel: ```js var Foo = (function (_Bar) { _inherits(Foo, _Bar); function Foo() { _classCallCheck(this, Foo); _Bar.apply(this, arguments); this.foo = 1; } return Foo; })(Bar); ``` Which is well-optimized (today) in most engines and much less confusing to read. As far as I can tell, the proposed compilation has exactly the same semantics as before. Fixes #10175 * Fix instanceof operator narrowing issues * Accept new baselines * Add regression test * Improve naming and documentation from PR * Update comment * Add more tests * Accept new baselines * Reduce worker count to 3 (#10210) Since we saw a starvation issue on one of @sandersn's PRs. * Speed up fourslash tests * Duh * Make baselines faster by not writing out unneeded files * Fix non-strict-compliant test * Fix 10076: Fix Tuple Destructing with "this" (#10208) * Call checkExpression eventhough there is no appropriate type from destructuring of array * Add tests and baselines * use transpileModule * Remove use strict * Improve instanceof for structurally identical types * Introduce isTypeInstanceOf function * Add test * Accept new baselines * Fix loop over array to use for-of instead of for-in * Use correct this in tuple type parameter constraints Instantiate this in tuple types used as type parameter constraints * Add explanatory comment to resolveTupleTypeMembers * Ignore null, undefined, void when checking for discriminant property * Add regression test * Delay tuple type constraint resolution Create a new tuple that stores the this-type. * Always use thisType when generating tuple id * Optimize format of type list id strings used in maps * wip - fix error * Make ReadonlyArray iterable. * Allow OSX to fail while we investigate (#10255) The random test timeouts are an issue. * Fix error from using merging master * avoid using the global name * Fix single-quote lint * Update baselines * Fix linting * Optimize performance of maps * Update API sample * Fix processDiagnosticMessages script * Have travis take shallow clones of the repo (#10275) Just cloning TS on travis takes 23 seconds on linux (68 seconds on mac), hopefully having it do a shallow clone will help. We don't rely on any tagging/artifacts from the travis servers which clone depth could impact, so this shouldn't impact anything other than build speed. * Add folds to travis log (#10269) * Optimize filterType to only call getUnionType if necessary * Add shorthand types declaration for travis-fold (#10293) * Optimize getTypeWithFacts * Filter out nullable and primitive types in isDiscriminantProperty * Fix typo * Add regression tests * Optimize core filter function to only allocate when necessary * Address CR comments + more optimizations * Faster path for creating union types from filterType * Allow an @types direcotry to have a package.json which specifies `"typings": null` to disclude it from automatically included typings. * Lint * Collect timing information for commands running on travis (#10308) * Simplifies performance API * Use 'MapLike' instead of 'Map' in 'preferConstRule.ts'. * narrow from 'any' in most situations instanceof and user-defined typeguards narrow from 'any' unless the narrowed-to type is exactly 'Object' or 'Function'. This is a breaking change. * Update instanceof conformance tests * accept new baselines * add tests * accept new baselines * Use lowercase names for type reference directives * Use proper response codes in web tests * Treat ambient shorthand declarations as explicit uses of the `any` type * Rename 'find' functions * Parallel linting (#10313) * A perilous thing, a parallel lint * Use work queue rather than scheduling work * Dont read files for lint on main thread * Fix style * Fix the style fix (#10344) * Aligned mark names with values used by ts-perf. * Use an enum in checkClassForDuplicateDeclarations to aid readability * Rename to Accessor * Migrated more MapLikes to Maps * Add ES2015 Date constructor signature that accepts another Date (#10353) * Parameters with no assignments implicitly considered const * Add tests * Migrate additional MapLikes to Maps. * Fix 10625: JSX Not validating when index signature is present (#10352) * Check for type of property declaration before using index signature * Add tests and baselines * fix linting error * Adding more comments * Clean up/move some Map helper functions. * Revert some formatting changes. * Improve ReadonlyArray<T>.concat to match Array<T> The Array-based signature was incorrect and also out-of-date. * Fix link to blog * Remove old assertion about when we're allowed to use fileExists * Set isNewIdentifierLocation to true for JavaScript files * Update error message for conflicting type definitions Fixes #10370 * Explain why we lower-case type reference directives * Correctly merge bindThisPropertyAssignment Also simply it considerably after noticing that it's *only* called for Javascript files, so there was a lot of dead code for TS cases that never happened. * Fix comment * Property handle imcomplete control flow types in nested loops * Update due to CR suggestion * Add regression test * Assign and instantiate contextual this type if not present * Fix 10289: correctly generate tsconfig.json with --lib (#10355) * Separate generate tsconfig into its own function and implement init with --lib # Conflicts: # src/compiler/tsc.ts * Add tests and baselines; Update function name Add unittests and baselines Add unittests and baselines for generating tsconfig Move unittest into harness folder Update harness tsconfig.json USe correct function name * Use new MapLike interstead. Update unittest # Conflicts: # src/compiler/commandLineParser.ts * Update JakeFile * Add tests for incorrect cases * Address PR : remove explicity write node_modules * JSDoc supports null, undefined and never types * Update baselines in jsDocParsing unit tests * Restored comments to explain spreading 'arguments' into calls to 'super'. * Added test. * Use the non-nullable type of the contextual type for object completions. * Return non-JsDocComment children ... to make syntactic classification work * Add more tests for `export = foo.bar`. * Output test baselines to tests/baselines/local instead of root * Move supportedTypescriptExtensionsWithDtsFirst next to supportedTypeScriptExtensions and rename * Fix comment * Fix RWC Runner (#10420) * Use /// <reference types * Don't report an errors if it comes from lib.d.ts * Treat special property access symbol differently ... when retriving documentation * Fix tests * Update shim version to be 2.1 (#10424) * Check return code paths on getters (#10102) * Check return paths on getters * Remove TODO comment * Remove extraneous arguments from harness's runBaseline (#10419) * Remove extraneous arguments from runBaseline * Address comments from @yuit * Remove needless call to basename * Refactor baseliners out of compiler runner (#10440) * CR feedback * fix broken tests * Pass in baselineOpts into types baselines so that RWC baselines can be written to internal folder (#10443) * Add error message Add error message when trying to relate primitives to the boxed/apparent backing types. * fix linting error * follow advise * remove extra code * Add more test for 10426 * fix some errors * routine update of dom libs * Add test for jsdoc syntactic classification for function declaration * Simplify implementation * Tolerate certain errors in tsconfig.json * Add test for configFile error tolerance * Use TS parser to tolerate more errors in tsconfig.json * Implement tuple types as type references to synthesized generic types * Add comments + minor changes * Accept new baselines * Add .types extension * Properly guard for undefined in getTypeReferenceArity * Add jsdoc nullable union test case to fourslash * Fix class/interface merging issue + lint error * Allow "typings" in a package.json to be missing its extension (but also allow it to have an extension) * Contextually type this in getDeclFromSig, not checkThisExpr * Update parser comment with es7 grammar (#10459) * Use ES7 term of ExponentiationExpression * Update timeout for mac OS * Address PR: add space * allowSyntheticDefaultImports resolves to modules instead of variables Fixes #10429 by improving the fix in #10096 * Rename getContextuallyTypedThisParameter to getContextualThisParameter * Fix 10472: Invalid emitted code for await expression (#10483) * Properly emit await expression with yield expression * Add tests and update baselines * Move parsing await expression into parse unary-expression * Update incorrect comment * change error message * Fix broken build from merging with master * Fix linting error
2016-08-27 00:51:10 +02:00
return getGeneratedIdentifier(<Identifier>node);
}
else if (isIdentifier(node) && (nodeIsSynthesized(node) || !node.parent)) {
return unescapeIdentifier(node.text);
}
else if (node.kind === SyntaxKind.StringLiteral && (<StringLiteral>node).textSourceNode) {
return getTextOfNode((<StringLiteral>node).textSourceNode, includeTrivia);
}
else if (isLiteralExpression(node) && (nodeIsSynthesized(node) || !node.parent)) {
return node.text;
}
return getSourceTextOfNodeFromSourceFile(currentSourceFile, node, includeTrivia);
}
function getLiteralTextOfNode(node: LiteralLikeNode): string {
if (node.kind === SyntaxKind.StringLiteral && (<StringLiteral>node).textSourceNode) {
const textSourceNode = (<StringLiteral>node).textSourceNode;
if (isIdentifier(textSourceNode)) {
return "\"" + escapeNonAsciiCharacters(escapeString(getTextOfNode(textSourceNode))) + "\"";
}
else {
return getLiteralTextOfNode(textSourceNode);
}
}
return getLiteralText(node, currentSourceFile, languageVersion);
}
function isSingleLineEmptyBlock(block: Block) {
return !block.multiLine
&& isEmptyBlock(block);
}
function isEmptyBlock(block: BlockLike) {
return block.statements.length === 0
&& rangeEndIsOnSameLineAsRangeStart(block, block, currentSourceFile);
}
function isUniqueName(name: string): boolean {
return !resolver.hasGlobalName(name) &&
!hasProperty(currentFileIdentifiers, name) &&
!hasProperty(generatedNameSet, name);
}
2014-07-13 01:04:16 +02:00
function isUniqueLocalName(name: string, container: Node): boolean {
for (let node = container; isNodeDescendantOf(node, container); node = node.nextContainer) {
if (node.locals && hasProperty(node.locals, name)) {
// We conservatively include alias symbols to cover cases where they're emitted as locals
if (node.locals[name].flags & (SymbolFlags.Value | SymbolFlags.ExportValue | SymbolFlags.Alias)) {
return false;
}
2015-09-01 05:33:02 +02:00
}
}
return true;
}
/**
* 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: TempFlags): string {
if (flags && !(tempFlags & flags)) {
const name = flags === TempFlags._i ? "_i" : "_n";
if (isUniqueName(name)) {
tempFlags |= flags;
return name;
}
}
while (true) {
const count = tempFlags & TempFlags.CountMask;
tempFlags++;
// Skip over 'i' and 'n'
if (count !== 8 && count !== 13) {
const name = count < 26
? "_" + String.fromCharCode(CharacterCodes.a + count)
: "_" + (count - 26);
if (isUniqueName(name)) {
return name;
}
2015-09-01 05:33:02 +02:00
}
}
}
2015-09-01 05:33:02 +02:00
// 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: string): string {
// Find the first unique 'name_n', where n is a positive number
if (baseName.charCodeAt(baseName.length - 1) !== CharacterCodes._) {
baseName += "_";
}
let i = 1;
while (true) {
const generatedName = baseName + i;
if (isUniqueName(generatedName)) {
return generatedNameSet[generatedName] = generatedName;
}
i++;
}
}
function generateNameForModuleOrEnum(node: ModuleDeclaration | EnumDeclaration) {
const 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: ImportDeclaration | ExportDeclaration) {
const expr = getExternalModuleName(node);
const baseName = expr.kind === SyntaxKind.StringLiteral ?
escapeIdentifier(makeIdentifierFromModuleName((<LiteralExpression>expr).text)) : "module";
return makeUniqueName(baseName);
}
2014-07-13 01:04:16 +02:00
function generateNameForExportDefault() {
return makeUniqueName("default");
}
function generateNameForClassExpression() {
return makeUniqueName("class");
}
/**
* Generates a unique name from a node.
*
* @param node A node.
*/
function generateNameForNode(node: Node): string {
switch (node.kind) {
case SyntaxKind.Identifier:
return makeUniqueName(getTextOfNode(node));
case SyntaxKind.ModuleDeclaration:
case SyntaxKind.EnumDeclaration:
return generateNameForModuleOrEnum(<ModuleDeclaration | EnumDeclaration>node);
case SyntaxKind.ImportDeclaration:
case SyntaxKind.ExportDeclaration:
return generateNameForImportOrExportDeclaration(<ImportDeclaration | ExportDeclaration>node);
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.ClassDeclaration:
case SyntaxKind.ExportAssignment:
return generateNameForExportDefault();
case SyntaxKind.ClassExpression:
return generateNameForClassExpression();
default:
return makeTempVariableName(TempFlags.Auto);
}
}
/**
* Generates a unique identifier for a node.
*
* @param name A generated name.
*/
function generateName(name: Identifier) {
switch (name.autoGenerateKind) {
case GeneratedIdentifierKind.Auto:
return makeTempVariableName(TempFlags.Auto);
case GeneratedIdentifierKind.Loop:
return makeTempVariableName(TempFlags._i);
case GeneratedIdentifierKind.Unique:
return makeUniqueName(name.text);
}
Debug.fail("Unsupported GeneratedIdentifierKind.");
}
/**
* Gets the node from which a name should be generated.
*
* @param name A generated name wrapper.
*/
function getNodeForGeneratedName(name: Identifier) {
const autoGenerateId = name.autoGenerateId;
let node = name as Node;
let original = node.original;
while (original) {
node = original;
// if "node" is a different generated name (having a different
// "autoGenerateId"), use it and stop traversing.
if (isIdentifier(node)
&& node.autoGenerateKind === GeneratedIdentifierKind.Node
&& node.autoGenerateId !== autoGenerateId) {
break;
2015-08-02 04:24:18 +02:00
}
original = node.original;
2015-08-02 04:24:18 +02:00
}
// otherwise, return the original node for the source;
return node;
2014-07-13 01:04:16 +02:00
}
/**
* Gets the generated identifier text from a generated identifier.
*
* @param name The generated identifier.
*/
function getGeneratedIdentifier(name: Identifier) {
if (name.autoGenerateKind === GeneratedIdentifierKind.Node) {
// Generated names generate unique names based on their original node
// and are cached based on that node's id
const node = getNodeForGeneratedName(name);
const nodeId = getNodeId(node);
return nodeIdToGeneratedName[nodeId] || (nodeIdToGeneratedName[nodeId] = unescapeIdentifier(generateNameForNode(node)));
}
else {
// Auto, Loop, and Unique names are cached based on their unique
// autoGenerateId.
const autoGenerateId = name.autoGenerateId;
return autoGeneratedIdToGeneratedName[autoGenerateId] || (autoGeneratedIdToGeneratedName[autoGenerateId] = unescapeIdentifier(generateName(name)));
}
}
function createDelimiterMap() {
const delimiters: string[] = [];
delimiters[ListFormat.None] = "";
delimiters[ListFormat.CommaDelimited] = ",";
delimiters[ListFormat.BarDelimited] = " |";
delimiters[ListFormat.AmpersandDelimited] = " &";
return delimiters;
}
function getDelimiter(format: ListFormat) {
return delimiters[format & ListFormat.DelimitersMask];
}
function createBracketsMap() {
const brackets: string[][] = [];
brackets[ListFormat.Braces] = ["{", "}"];
brackets[ListFormat.Parenthesis] = ["(", ")"];
brackets[ListFormat.AngleBrackets] = ["<", ">"];
brackets[ListFormat.SquareBrackets] = ["[", "]"];
return brackets;
}
function getOpeningBracket(format: ListFormat) {
return brackets[format & ListFormat.BracketsMask][0];
}
function getClosingBracket(format: ListFormat) {
return brackets[format & ListFormat.BracketsMask][1];
}
}
const enum ListFormat {
None = 0,
// Line separators
SingleLine = 0, // Prints the list on a single line (default).
MultiLine = 1 << 0, // Prints the list on multiple lines.
PreserveLines = 1 << 1, // Prints the list using line preservation if possible.
LinesMask = SingleLine | MultiLine | PreserveLines,
// Delimiters
NotDelimited = 0, // There is no delimiter between list items (default).
BarDelimited = 1 << 2, // Each list item is space-and-bar (" |") delimited.
AmpersandDelimited = 1 << 3, // Each list item is space-and-ampersand (" &") delimited.
CommaDelimited = 1 << 4, // Each list item is comma (",") delimited.
DelimitersMask = BarDelimited | AmpersandDelimited | CommaDelimited,
AllowTrailingComma = 1 << 5, // Write a trailing comma (",") if present.
// Whitespace
Indented = 1 << 6, // The list should be indented.
SpaceBetweenBraces = 1 << 7, // Inserts a space after the opening brace and before the closing brace.
SpaceBetweenSiblings = 1 << 8, // Inserts a space between each sibling node.
// Brackets/Braces
Braces = 1 << 9, // The list is surrounded by "{" and "}".
Parenthesis = 1 << 10, // The list is surrounded by "(" and ")".
AngleBrackets = 1 << 11, // The list is surrounded by "<" and ">".
SquareBrackets = 1 << 12, // The list is surrounded by "[" and "]".
BracketsMask = Braces | Parenthesis | AngleBrackets | SquareBrackets,
OptionalIfUndefined = 1 << 13, // Do not emit brackets if the list is undefined.
OptionalIfEmpty = 1 << 14, // Do not emit brackets if the list is empty.
Optional = OptionalIfUndefined | OptionalIfEmpty,
// Other
PreferNewLine = 1 << 15, // Prefer adding a LineTerminator between synthesized nodes.
NoTrailingNewLine = 1 << 16, // Do not emit a trailing NewLine for a MultiLine list.
NoInterveningComments = 1 << 17, // Do not emit comments between each node
// Precomputed Formats
Modifiers = SingleLine | SpaceBetweenSiblings,
HeritageClauses = SingleLine | SpaceBetweenSiblings,
TypeLiteralMembers = MultiLine | Indented,
TupleTypeElements = CommaDelimited | SpaceBetweenSiblings | SingleLine | Indented,
UnionTypeConstituents = BarDelimited | SpaceBetweenSiblings | SingleLine,
IntersectionTypeConstituents = AmpersandDelimited | SpaceBetweenSiblings | SingleLine,
ObjectBindingPatternElements = SingleLine | AllowTrailingComma | SpaceBetweenBraces | CommaDelimited | SpaceBetweenSiblings,
ArrayBindingPatternElements = SingleLine | AllowTrailingComma | CommaDelimited | SpaceBetweenSiblings,
ObjectLiteralExpressionProperties = PreserveLines | CommaDelimited | SpaceBetweenSiblings | SpaceBetweenBraces | Indented | Braces,
ArrayLiteralExpressionElements = PreserveLines | CommaDelimited | SpaceBetweenSiblings | AllowTrailingComma | Indented | SquareBrackets,
CallExpressionArguments = CommaDelimited | SpaceBetweenSiblings | SingleLine | Parenthesis,
NewExpressionArguments = CommaDelimited | SpaceBetweenSiblings | SingleLine | Parenthesis | OptionalIfUndefined,
TemplateExpressionSpans = SingleLine | NoInterveningComments,
SingleLineBlockStatements = SpaceBetweenBraces | SpaceBetweenSiblings | SingleLine,
MultiLineBlockStatements = Indented | MultiLine,
VariableDeclarationList = CommaDelimited | SpaceBetweenSiblings | SingleLine,
SingleLineFunctionBodyStatements = SingleLine | SpaceBetweenSiblings | SpaceBetweenBraces,
MultiLineFunctionBodyStatements = MultiLine,
ClassHeritageClauses = SingleLine | SpaceBetweenSiblings,
ClassMembers = Indented | MultiLine,
InterfaceMembers = Indented | MultiLine,
EnumMembers = CommaDelimited | Indented | MultiLine,
CaseBlockClauses = Indented | MultiLine,
NamedImportsOrExportsElements = CommaDelimited | SpaceBetweenSiblings | AllowTrailingComma | SingleLine | SpaceBetweenBraces,
JsxElementChildren = SingleLine | NoInterveningComments,
JsxElementAttributes = SingleLine | SpaceBetweenSiblings | NoInterveningComments,
CaseOrDefaultClauseStatements = Indented | MultiLine | NoTrailingNewLine | OptionalIfEmpty,
HeritageClauseTypes = CommaDelimited | SpaceBetweenSiblings | SingleLine,
SourceFileStatements = MultiLine | NoTrailingNewLine,
Decorators = MultiLine | Optional,
TypeArguments = CommaDelimited | SpaceBetweenSiblings | SingleLine | Indented | AngleBrackets | Optional,
TypeParameters = CommaDelimited | SpaceBetweenSiblings | SingleLine | Indented | AngleBrackets | Optional,
Parameters = CommaDelimited | SpaceBetweenSiblings | SingleLine | Indented | Parenthesis,
IndexSignatureParameters = CommaDelimited | SpaceBetweenSiblings | SingleLine | Indented | SquareBrackets,
2014-07-13 01:04:16 +02:00
}
}