Skip to content

Commit cd22d81

Browse files
committed
Merge branch 'master' into publicTransformers
2 parents a295aa8 + 4ec6848 commit cd22d81

File tree

239 files changed

+2400
-839
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

239 files changed

+2400
-839
lines changed

Jakefile.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ if (process.env.path !== undefined) {
3737
}
3838

3939
function filesFromConfig(configPath) {
40-
console.log(configPath);
4140
var configText = fs.readFileSync(configPath).toString();
4241
var config = ts.parseConfigFileTextToJson(configPath, configText, /*stripComments*/ true);
4342
if (config.error) {

src/compiler/checker.ts

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3322,7 +3322,7 @@ namespace ts {
33223322
type;
33233323
}
33243324

3325-
function getTypeForVariableLikeDeclarationFromJSDocComment(declaration: VariableLikeDeclaration) {
3325+
function getTypeForDeclarationFromJSDocComment(declaration: Node ) {
33263326
const jsdocType = getJSDocType(declaration);
33273327
if (jsdocType) {
33283328
return getTypeFromTypeNode(jsdocType);
@@ -3350,7 +3350,7 @@ namespace ts {
33503350
// If this is a variable in a JavaScript file, then use the JSDoc type (if it has
33513351
// one as its type), otherwise fallback to the below standard TS codepaths to
33523352
// try to figure it out.
3353-
const type = getTypeForVariableLikeDeclarationFromJSDocComment(declaration);
3353+
const type = getTypeForDeclarationFromJSDocComment(declaration);
33543354
if (type && type !== unknownType) {
33553355
return type;
33563356
}
@@ -3445,6 +3445,27 @@ namespace ts {
34453445
return undefined;
34463446
}
34473447

3448+
// Return the inferred type for a variable, parameter, or property declaration
3449+
function getTypeForJSSpecialPropertyDeclaration(declaration: Declaration): Type {
3450+
const expression = declaration.kind === SyntaxKind.BinaryExpression ? <BinaryExpression>declaration :
3451+
declaration.kind === SyntaxKind.PropertyAccessExpression ? <BinaryExpression>getAncestor(declaration, SyntaxKind.BinaryExpression) :
3452+
undefined;
3453+
3454+
if (!expression) {
3455+
return unknownType;
3456+
}
3457+
3458+
if (expression.flags & NodeFlags.JavaScriptFile) {
3459+
// If there is a JSDoc type, use it
3460+
const type = getTypeForDeclarationFromJSDocComment(expression.parent);
3461+
if (type && type !== unknownType) {
3462+
return getWidenedType(type);
3463+
}
3464+
}
3465+
3466+
return getWidenedLiteralType(checkExpressionCached(expression.right));
3467+
}
3468+
34483469
// Return the type implied by a binding pattern element. This is the type of the initializer of the element if
34493470
// one is present. Otherwise, if the element is itself a binding pattern, it is the type implied by the binding
34503471
// pattern. Otherwise, it is the type any.
@@ -3599,18 +3620,7 @@ namespace ts {
35993620
// * className.prototype.method = expr
36003621
if (declaration.kind === SyntaxKind.BinaryExpression ||
36013622
declaration.kind === SyntaxKind.PropertyAccessExpression && declaration.parent.kind === SyntaxKind.BinaryExpression) {
3602-
// Use JS Doc type if present on parent expression statement
3603-
if (declaration.flags & NodeFlags.JavaScriptFile) {
3604-
const jsdocType = getJSDocType(declaration.parent);
3605-
if (jsdocType) {
3606-
return links.type = getTypeFromTypeNode(jsdocType);
3607-
}
3608-
}
3609-
const declaredTypes = map(symbol.declarations,
3610-
decl => decl.kind === SyntaxKind.BinaryExpression ?
3611-
checkExpressionCached((<BinaryExpression>decl).right) :
3612-
checkExpressionCached((<BinaryExpression>decl.parent).right));
3613-
type = getUnionType(declaredTypes, /*subtypeReduction*/ true);
3623+
type = getWidenedType(getUnionType(map(symbol.declarations, getTypeForJSSpecialPropertyDeclaration), /*subtypeReduction*/ true));
36143624
}
36153625
else {
36163626
type = getWidenedTypeForVariableLikeDeclaration(<VariableLikeDeclaration>declaration, /*reportErrors*/ true);
@@ -3653,7 +3663,7 @@ namespace ts {
36533663
const setter = <AccessorDeclaration>getDeclarationOfKind(symbol, SyntaxKind.SetAccessor);
36543664

36553665
if (getter && getter.flags & NodeFlags.JavaScriptFile) {
3656-
const jsDocType = getTypeForVariableLikeDeclarationFromJSDocComment(getter);
3666+
const jsDocType = getTypeForDeclarationFromJSDocComment(getter);
36573667
if (jsDocType) {
36583668
return links.type = jsDocType;
36593669
}
@@ -9535,11 +9545,19 @@ namespace ts {
95359545
}
95369546

95379547
function getAssignedTypeOfBinaryExpression(node: BinaryExpression): Type {
9538-
return node.parent.kind === SyntaxKind.ArrayLiteralExpression || node.parent.kind === SyntaxKind.PropertyAssignment ?
9548+
const isDestructuringDefaultAssignment =
9549+
node.parent.kind === SyntaxKind.ArrayLiteralExpression && isDestructuringAssignmentTarget(node.parent) ||
9550+
node.parent.kind === SyntaxKind.PropertyAssignment && isDestructuringAssignmentTarget(node.parent.parent);
9551+
return isDestructuringDefaultAssignment ?
95399552
getTypeWithDefault(getAssignedType(node), node.right) :
95409553
getTypeOfExpression(node.right);
95419554
}
95429555

9556+
function isDestructuringAssignmentTarget(parent: Node) {
9557+
return parent.parent.kind === SyntaxKind.BinaryExpression && (parent.parent as BinaryExpression).left === parent ||
9558+
parent.parent.kind === SyntaxKind.ForOfStatement && (parent.parent as ForOfStatement).initializer === parent;
9559+
}
9560+
95439561
function getAssignedTypeOfArrayLiteralElement(node: ArrayLiteralExpression, element: Expression): Type {
95449562
return getTypeOfDestructuredArrayElement(getAssignedType(node), indexOf(node.elements, element));
95459563
}

src/compiler/comments.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace ts {
99
emitNodeWithComments(hint: EmitHint, node: Node, emitCallback: (hint: EmitHint, node: Node) => void): void;
1010
emitBodyWithDetachedComments(node: Node, detachedRange: TextRange, emitCallback: (node: Node) => void): void;
1111
emitTrailingCommentsOfPosition(pos: number): void;
12+
emitLeadingCommentsOfPosition(pos: number): void;
1213
}
1314

1415
export function createCommentWriter(printerOptions: PrinterOptions, emitPos: ((pos: number) => void) | undefined): CommentWriter {
@@ -32,6 +33,7 @@ namespace ts {
3233
emitNodeWithComments,
3334
emitBodyWithDetachedComments,
3435
emitTrailingCommentsOfPosition,
36+
emitLeadingCommentsOfPosition,
3537
};
3638

3739
function emitNodeWithComments(hint: EmitHint, node: Node, emitCallback: (hint: EmitHint, node: Node) => void) {
@@ -275,6 +277,14 @@ namespace ts {
275277
}
276278
}
277279

280+
function emitLeadingCommentsOfPosition(pos: number) {
281+
if (disabled || pos === -1) {
282+
return;
283+
}
284+
285+
emitLeadingComments(pos, /*isEmittedNode*/ true);
286+
}
287+
278288
function emitTrailingComments(pos: number) {
279289
forEachTrailingCommentToEmit(pos, emitTrailingComment);
280290
}

src/compiler/emitter.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// <reference path="checker.ts" />
1+
/// <reference path="checker.ts" />
22
/// <reference path="transformer.ts" />
33
/// <reference path="declarationEmitter.ts" />
44
/// <reference path="sourcemap.ts" />
@@ -208,6 +208,7 @@ namespace ts {
208208
emitNodeWithComments,
209209
emitBodyWithDetachedComments,
210210
emitTrailingCommentsOfPosition,
211+
emitLeadingCommentsOfPosition,
211212
} = comments;
212213

213214
let currentSourceFile: SourceFile;
@@ -1336,6 +1337,10 @@ namespace ts {
13361337
else {
13371338
writeToken(SyntaxKind.OpenBraceToken, node.pos, /*contextNode*/ node);
13381339
emitBlockStatements(node);
1340+
// We have to call emitLeadingComments explicitly here because otherwise leading comments of the close brace token will not be emitted
1341+
increaseIndent();
1342+
emitLeadingCommentsOfPosition(node.statements.end);
1343+
decreaseIndent();
13391344
writeToken(SyntaxKind.CloseBraceToken, node.statements.end, /*contextNode*/ node);
13401345
}
13411346
}
@@ -2218,6 +2223,15 @@ namespace ts {
22182223

22192224
// Write the delimiter if this is not the first node.
22202225
if (previousSibling) {
2226+
// i.e
2227+
// function commentedParameters(
2228+
// /* Parameter a */
2229+
// a
2230+
// /* End of parameter a */ -> this comment isn't considered to be trailing comment of parameter "a" due to newline
2231+
// ,
2232+
if (delimiter && previousSibling.end !== parentNode.end) {
2233+
emitLeadingCommentsOfPosition(previousSibling.end);
2234+
}
22212235
write(delimiter);
22222236

22232237
// Write either a line terminator or whitespace to separate the elements.
@@ -2264,6 +2278,17 @@ namespace ts {
22642278
write(",");
22652279
}
22662280

2281+
2282+
// Emit any trailing comment of the last element in the list
2283+
// i.e
2284+
// var array = [...
2285+
// 2
2286+
// /* end of element 2 */
2287+
// ];
2288+
if (previousSibling && delimiter && previousSibling.end !== parentNode.end) {
2289+
emitLeadingCommentsOfPosition(previousSibling.end);
2290+
}
2291+
22672292
// Decrease the indent, if requested.
22682293
if (format & ListFormat.Indented) {
22692294
decreaseIndent();

src/compiler/sys.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ namespace ts {
486486
// (ref: https://github.com/nodejs/node/pull/2649 and https://github.com/Microsoft/TypeScript/issues/4643)
487487
let options: any;
488488
if (!directoryExists(directoryName) || (isUNCPath(directoryName) && process.platform === "win32")) {
489-
// do nothing if either
489+
// do nothing if either
490490
// - target folder does not exist
491491
// - this is UNC path on Windows (https://github.com/Microsoft/TypeScript/issues/13874)
492492
return noOpFileWatcher;

0 commit comments

Comments
 (0)