Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Transforms] Update transforms with recent master #8960

Merged
merged 26 commits into from
Jun 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
20d8a94
Remove check narrowing only certain types, add test showing issues wi…
weswigham Feb 25, 2016
a2feb0e
string literal case test
weswigham Mar 11, 2016
92bf91d
Reconcile fix with CFA work
weswigham Apr 26, 2016
73d4aae
Defaultable -> NotNarrowable to align with use
weswigham Apr 26, 2016
70733da
Missed a defaultable in comments
weswigham Apr 26, 2016
a80529b
Add test for narrowing to unions of string literals
weswigham Apr 26, 2016
d111a0f
Merge branch 'master' into narrow-all-types
sandersn Jun 1, 2016
9a620bf
Actually merge from master
sandersn Jun 1, 2016
0e96c5e
Run fixupParentReferences when parsing isolated jsDocComment
zhengbli Jun 2, 2016
92177be
initial revision of unit test support for project system in tsserver
vladima Jun 2, 2016
2517238
Add non-widening forms of null and undefined
ahejlsberg Jun 2, 2016
5f3f2d3
Create separate control flows for property declarations with initiali…
ahejlsberg Jun 2, 2016
706683d
Add regression test
ahejlsberg Jun 2, 2016
9f087cb
Merge pull request #8941 from Microsoft/controlFlowPropertyDeclarations
ahejlsberg Jun 2, 2016
20bab14
Add tests
ahejlsberg Jun 2, 2016
ef0f6c8
Merge pull request #7235 from weswigham/narrow-all-types
sandersn Jun 2, 2016
fb2df77
Remove unused variable
ahejlsberg Jun 2, 2016
7e00d7e
Merge pull request #8931 from Microsoft/tsserver-projectsystem-tests
vladima Jun 2, 2016
d41ac8a
Add null check and CR feedback
zhengbli Jun 2, 2016
fc3e040
Revert "Merge pull request #7235 from weswigham/narrow-all-types"
sandersn Jun 2, 2016
e2a1a78
reuse the fixupParentReferences function
zhengbli Jun 2, 2016
24f15a4
Merge pull request #8946 from Microsoft/revert-narrow-all-types
sandersn Jun 2, 2016
131f759
Merge pull request #8930 from zhengbli/i8676
zhengbli Jun 2, 2016
3853555
Merge pull request #8944 from Microsoft/reviseWidening
ahejlsberg Jun 2, 2016
3183068
Merge branch 'master' into transforms_mergemaster
Jun 3, 2016
8012ff1
Fix up error from merging with master
Jun 3, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Jakefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ var harnessSources = harnessCoreSources.concat([
"tsconfigParsing.ts",
"commandLineParsing.ts",
"convertCompilerOptionsFromJson.ts",
"convertTypingOptionsFromJson.ts"
"convertTypingOptionsFromJson.ts",
"tsserverProjectSystem.ts"
].map(function (f) {
return path.join(unittestsDirectory, f);
})).concat([
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,8 @@ namespace ts {

case SyntaxKind.ModuleBlock:
return ContainerFlags.IsControlFlowContainer;
case SyntaxKind.PropertyDeclaration:
return (<PropertyDeclaration>node).initializer ? ContainerFlags.IsControlFlowContainer : 0;

case SyntaxKind.CatchClause:
case SyntaxKind.ForStatement:
Expand Down
36 changes: 19 additions & 17 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,16 @@ namespace ts {
const unknownSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Transient, "unknown");
const resolvingSymbol = createSymbol(SymbolFlags.Transient, "__resolving__");

const nullableWideningFlags = strictNullChecks ? 0 : TypeFlags.ContainsUndefinedOrNull;
const anyType = createIntrinsicType(TypeFlags.Any, "any");
const stringType = createIntrinsicType(TypeFlags.String, "string");
const numberType = createIntrinsicType(TypeFlags.Number, "number");
const booleanType = createIntrinsicType(TypeFlags.Boolean, "boolean");
const esSymbolType = createIntrinsicType(TypeFlags.ESSymbol, "symbol");
const voidType = createIntrinsicType(TypeFlags.Void, "void");
const undefinedType = createIntrinsicType(TypeFlags.Undefined | nullableWideningFlags, "undefined");
const nullType = createIntrinsicType(TypeFlags.Null | nullableWideningFlags, "null");
const emptyArrayElementType = createIntrinsicType(TypeFlags.Undefined | TypeFlags.ContainsUndefinedOrNull, "undefined");
const undefinedType = createIntrinsicType(TypeFlags.Undefined, "undefined");
const undefinedWideningType = strictNullChecks ? undefinedType : createIntrinsicType(TypeFlags.Undefined | TypeFlags.ContainsWideningType, "undefined");
const nullType = createIntrinsicType(TypeFlags.Null, "null");
const nullWideningType = strictNullChecks ? nullType : createIntrinsicType(TypeFlags.Null | TypeFlags.ContainsWideningType, "null");
const unknownType = createIntrinsicType(TypeFlags.Any, "unknown");
const neverType = createIntrinsicType(TypeFlags.Never, "never");

Expand Down Expand Up @@ -3403,7 +3403,7 @@ namespace ts {
error(type.symbol.valueDeclaration, Diagnostics._0_is_referenced_directly_or_indirectly_in_its_own_base_expression, symbolToString(type.symbol));
return type.resolvedBaseConstructorType = unknownType;
}
if (baseConstructorType !== unknownType && baseConstructorType !== nullType && !isConstructorType(baseConstructorType)) {
if (baseConstructorType !== unknownType && baseConstructorType !== nullWideningType && !isConstructorType(baseConstructorType)) {
error(baseTypeNode.expression, Diagnostics.Type_0_is_not_a_constructor_function_type, typeToString(baseConstructorType));
return type.resolvedBaseConstructorType = unknownType;
}
Expand Down Expand Up @@ -5009,6 +5009,7 @@ namespace ts {
containsAny?: boolean;
containsUndefined?: boolean;
containsNull?: boolean;
containsNonWideningType?: boolean;
}

function addTypeToSet(typeSet: TypeSet, type: Type, typeSetKind: TypeFlags) {
Expand All @@ -5019,6 +5020,7 @@ namespace ts {
if (type.flags & TypeFlags.Any) typeSet.containsAny = true;
if (type.flags & TypeFlags.Undefined) typeSet.containsUndefined = true;
if (type.flags & TypeFlags.Null) typeSet.containsNull = true;
if (!(type.flags & TypeFlags.ContainsWideningType)) typeSet.containsNonWideningType = true;
}
else if (type !== neverType && !contains(typeSet, type)) {
typeSet.push(type);
Expand Down Expand Up @@ -5079,8 +5081,8 @@ namespace ts {
removeSubtypes(typeSet);
}
if (typeSet.length === 0) {
return typeSet.containsNull ? nullType :
typeSet.containsUndefined ? undefinedType :
return typeSet.containsNull ? typeSet.containsNonWideningType ? nullType : nullWideningType :
typeSet.containsUndefined ? typeSet.containsNonWideningType ? undefinedType : undefinedWideningType :
neverType;
}
else if (typeSet.length === 1) {
Expand Down Expand Up @@ -5880,7 +5882,7 @@ namespace ts {
if (!(target.flags & TypeFlags.Never)) {
if (target.flags & TypeFlags.Any || source.flags & TypeFlags.Never) return Ternary.True;
if (source.flags & TypeFlags.Undefined) {
if (!strictNullChecks || target.flags & (TypeFlags.Undefined | TypeFlags.Void) || source === emptyArrayElementType) return Ternary.True;
if (!strictNullChecks || target.flags & (TypeFlags.Undefined | TypeFlags.Void)) return Ternary.True;
}
if (source.flags & TypeFlags.Null) {
if (!strictNullChecks || target.flags & TypeFlags.Null) return Ternary.True;
Expand Down Expand Up @@ -6970,7 +6972,7 @@ namespace ts {
if (type.flags & TypeFlags.ObjectLiteral) {
for (const p of getPropertiesOfObjectType(type)) {
const t = getTypeOfSymbol(p);
if (t.flags & TypeFlags.ContainsUndefinedOrNull) {
if (t.flags & TypeFlags.ContainsWideningType) {
if (!reportWideningErrorsInType(t)) {
error(p.valueDeclaration, Diagnostics.Object_literal_s_property_0_implicitly_has_an_1_type, p.name, typeToString(getWidenedType(t)));
}
Expand Down Expand Up @@ -7017,7 +7019,7 @@ namespace ts {
}

function reportErrorsFromWidening(declaration: Declaration, type: Type) {
if (produceDiagnostics && compilerOptions.noImplicitAny && type.flags & TypeFlags.ContainsUndefinedOrNull) {
if (produceDiagnostics && compilerOptions.noImplicitAny && type.flags & TypeFlags.ContainsWideningType) {
// Report implicit any error within type if possible, otherwise report error on declaration
if (!reportWideningErrorsInType(type)) {
reportImplicitAnyError(declaration, type);
Expand Down Expand Up @@ -8309,7 +8311,7 @@ namespace ts {
const classInstanceType = <InterfaceType>getDeclaredTypeOfSymbol(classSymbol);
const baseConstructorType = getBaseConstructorTypeOfClass(classInstanceType);

return baseConstructorType === nullType;
return baseConstructorType === nullWideningType;
}

function checkThisExpression(node: Node): Type {
Expand Down Expand Up @@ -9200,7 +9202,7 @@ namespace ts {
}
}
}
return createArrayType(elementTypes.length ? getUnionType(elementTypes) : emptyArrayElementType);
return createArrayType(elementTypes.length ? getUnionType(elementTypes) : strictNullChecks ? neverType : undefinedWideningType);
}

function isNumericName(name: DeclarationName): boolean {
Expand Down Expand Up @@ -12004,7 +12006,7 @@ namespace ts {

function checkVoidExpression(node: VoidExpression): Type {
checkExpression(node.expression);
return undefinedType;
return undefinedWideningType;
}

function checkAwaitExpression(node: AwaitExpression): Type {
Expand Down Expand Up @@ -12411,7 +12413,7 @@ namespace ts {
case SyntaxKind.InKeyword:
return checkInExpression(left, right, leftType, rightType);
case SyntaxKind.AmpersandAmpersandToken:
return addNullableKind(rightType, getNullableKind(leftType));
return strictNullChecks ? addNullableKind(rightType, getNullableKind(leftType)) : rightType;
case SyntaxKind.BarBarToken:
return getUnionType([getNonNullableType(leftType), rightType]);
case SyntaxKind.EqualsToken:
Expand Down Expand Up @@ -12678,7 +12680,7 @@ namespace ts {
case SyntaxKind.SuperKeyword:
return checkSuperExpression(node);
case SyntaxKind.NullKeyword:
return nullType;
return nullWideningType;
case SyntaxKind.TrueKeyword:
case SyntaxKind.FalseKeyword:
return booleanType;
Expand Down Expand Up @@ -12736,7 +12738,7 @@ namespace ts {
case SyntaxKind.SpreadElementExpression:
return checkSpreadElementExpression(<SpreadElementExpression>node, contextualMapper);
case SyntaxKind.OmittedExpression:
return undefinedType;
return undefinedWideningType;
case SyntaxKind.YieldExpression:
return checkYieldExpression(<YieldExpression>node);
case SyntaxKind.JsxExpression:
Expand Down Expand Up @@ -17728,7 +17730,7 @@ namespace ts {
// Setup global builtins
addToSymbolTable(globals, builtinGlobals, Diagnostics.Declaration_name_conflicts_with_built_in_global_identifier_0);

getSymbolLinks(undefinedSymbol).type = undefinedType;
getSymbolLinks(undefinedSymbol).type = undefinedWideningType;
getSymbolLinks(argumentsSymbol).type = getGlobalType("IArguments");
getSymbolLinks(unknownSymbol).type = unknownType;

Expand Down
15 changes: 11 additions & 4 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,14 @@ namespace ts {

/* @internal */
export function parseIsolatedJSDocComment(content: string, start?: number, length?: number) {
return Parser.JSDocParser.parseIsolatedJSDocComment(content, start, length);
const result = Parser.JSDocParser.parseIsolatedJSDocComment(content, start, length);
if (result && result.jsDocComment) {
// because the jsDocComment was parsed out of the source file, it might
// not be covered by the fixupParentReferences.
Parser.fixupParentReferences(result.jsDocComment);
}

return result;
}

/* @internal */
Expand Down Expand Up @@ -655,14 +662,14 @@ namespace ts {
return node;
}

export function fixupParentReferences(sourceFile: Node) {
export function fixupParentReferences(rootNode: Node) {
// normally parent references are set during binding. However, for clients that only need
// a syntax tree, and no semantic features, then the binding process is an unnecessary
// overhead. This functions allows us to set all the parents, without all the expense of
// binding.

let parent: Node = sourceFile;
forEachChild(sourceFile, visitNode);
let parent: Node = rootNode;
forEachChild(rootNode, visitNode);
return;

function visitNode(n: Node): void {
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2264,7 +2264,7 @@ namespace ts {
/* @internal */
FreshObjectLiteral = 0x00100000, // Fresh object literal type
/* @internal */
ContainsUndefinedOrNull = 0x00200000, // Type is or contains undefined or null type
ContainsWideningType = 0x00200000, // Type is or contains undefined or null widening type
/* @internal */
ContainsObjectLiteral = 0x00400000, // Type is or contains object literal type
/* @internal */
Expand All @@ -2287,9 +2287,9 @@ namespace ts {
StructuredType = ObjectType | Union | Intersection,
Narrowable = Any | ObjectType | Union | TypeParameter,
/* @internal */
RequiresWidening = ContainsUndefinedOrNull | ContainsObjectLiteral,
RequiresWidening = ContainsWideningType | ContainsObjectLiteral,
/* @internal */
PropagatingFlags = ContainsUndefinedOrNull | ContainsObjectLiteral | ContainsAnyFunctionType
PropagatingFlags = ContainsWideningType | ContainsObjectLiteral | ContainsAnyFunctionType
}

export type DestructuringPattern = BindingPattern | ObjectLiteralExpression | ArrayLiteralExpression;
Expand Down
2 changes: 1 addition & 1 deletion src/server/editorServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1138,7 +1138,7 @@ namespace ts.server {
else {
this.log("No config files found.");
}
return {};
return configFileName ? { configFileName } : {};
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/baselines/reference/arrayLiteralWidened.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// array literals are widened upon assignment according to their element type

var a = []; // any[]
var a = [,,];

var a = [null, null];
var a = [undefined, undefined];
Expand All @@ -12,15 +13,29 @@ var b = [[undefined, undefined]];

var c = [[[]]]; // any[][][]
var c = [[[null]],[undefined]]

// no widening when one or more elements are non-widening

var x: undefined = undefined;

var d = [x];
var d = [, x];
var d = [undefined, x];


//// [arrayLiteralWidened.js]
// array literals are widened upon assignment according to their element type
var a = []; // any[]
var a = [, ,];
var a = [null, null];
var a = [undefined, undefined];
var b = [[], [null, null]]; // any[][]
var b = [[], []];
var b = [[undefined, undefined]];
var c = [[[]]]; // any[][][]
var c = [[[null]], [undefined]];
// no widening when one or more elements are non-widening
var x = undefined;
var d = [x];
var d = [, x];
var d = [undefined, x];
38 changes: 30 additions & 8 deletions tests/baselines/reference/arrayLiteralWidened.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,53 @@
// array literals are widened upon assignment according to their element type

var a = []; // any[]
>a : Symbol(a, Decl(arrayLiteralWidened.ts, 2, 3), Decl(arrayLiteralWidened.ts, 4, 3), Decl(arrayLiteralWidened.ts, 5, 3))
>a : Symbol(a, Decl(arrayLiteralWidened.ts, 2, 3), Decl(arrayLiteralWidened.ts, 3, 3), Decl(arrayLiteralWidened.ts, 5, 3), Decl(arrayLiteralWidened.ts, 6, 3))

var a = [,,];
>a : Symbol(a, Decl(arrayLiteralWidened.ts, 2, 3), Decl(arrayLiteralWidened.ts, 3, 3), Decl(arrayLiteralWidened.ts, 5, 3), Decl(arrayLiteralWidened.ts, 6, 3))

var a = [null, null];
>a : Symbol(a, Decl(arrayLiteralWidened.ts, 2, 3), Decl(arrayLiteralWidened.ts, 4, 3), Decl(arrayLiteralWidened.ts, 5, 3))
>a : Symbol(a, Decl(arrayLiteralWidened.ts, 2, 3), Decl(arrayLiteralWidened.ts, 3, 3), Decl(arrayLiteralWidened.ts, 5, 3), Decl(arrayLiteralWidened.ts, 6, 3))

var a = [undefined, undefined];
>a : Symbol(a, Decl(arrayLiteralWidened.ts, 2, 3), Decl(arrayLiteralWidened.ts, 4, 3), Decl(arrayLiteralWidened.ts, 5, 3))
>a : Symbol(a, Decl(arrayLiteralWidened.ts, 2, 3), Decl(arrayLiteralWidened.ts, 3, 3), Decl(arrayLiteralWidened.ts, 5, 3), Decl(arrayLiteralWidened.ts, 6, 3))
>undefined : Symbol(undefined)
>undefined : Symbol(undefined)

var b = [[], [null, null]]; // any[][]
>b : Symbol(b, Decl(arrayLiteralWidened.ts, 7, 3), Decl(arrayLiteralWidened.ts, 8, 3), Decl(arrayLiteralWidened.ts, 9, 3))
>b : Symbol(b, Decl(arrayLiteralWidened.ts, 8, 3), Decl(arrayLiteralWidened.ts, 9, 3), Decl(arrayLiteralWidened.ts, 10, 3))

var b = [[], []];
>b : Symbol(b, Decl(arrayLiteralWidened.ts, 7, 3), Decl(arrayLiteralWidened.ts, 8, 3), Decl(arrayLiteralWidened.ts, 9, 3))
>b : Symbol(b, Decl(arrayLiteralWidened.ts, 8, 3), Decl(arrayLiteralWidened.ts, 9, 3), Decl(arrayLiteralWidened.ts, 10, 3))

var b = [[undefined, undefined]];
>b : Symbol(b, Decl(arrayLiteralWidened.ts, 7, 3), Decl(arrayLiteralWidened.ts, 8, 3), Decl(arrayLiteralWidened.ts, 9, 3))
>b : Symbol(b, Decl(arrayLiteralWidened.ts, 8, 3), Decl(arrayLiteralWidened.ts, 9, 3), Decl(arrayLiteralWidened.ts, 10, 3))
>undefined : Symbol(undefined)
>undefined : Symbol(undefined)

var c = [[[]]]; // any[][][]
>c : Symbol(c, Decl(arrayLiteralWidened.ts, 11, 3), Decl(arrayLiteralWidened.ts, 12, 3))
>c : Symbol(c, Decl(arrayLiteralWidened.ts, 12, 3), Decl(arrayLiteralWidened.ts, 13, 3))

var c = [[[null]],[undefined]]
>c : Symbol(c, Decl(arrayLiteralWidened.ts, 11, 3), Decl(arrayLiteralWidened.ts, 12, 3))
>c : Symbol(c, Decl(arrayLiteralWidened.ts, 12, 3), Decl(arrayLiteralWidened.ts, 13, 3))
>undefined : Symbol(undefined)

// no widening when one or more elements are non-widening

var x: undefined = undefined;
>x : Symbol(x, Decl(arrayLiteralWidened.ts, 17, 3))
>undefined : Symbol(undefined)

var d = [x];
>d : Symbol(d, Decl(arrayLiteralWidened.ts, 19, 3), Decl(arrayLiteralWidened.ts, 20, 3), Decl(arrayLiteralWidened.ts, 21, 3))
>x : Symbol(x, Decl(arrayLiteralWidened.ts, 17, 3))

var d = [, x];
>d : Symbol(d, Decl(arrayLiteralWidened.ts, 19, 3), Decl(arrayLiteralWidened.ts, 20, 3), Decl(arrayLiteralWidened.ts, 21, 3))
>x : Symbol(x, Decl(arrayLiteralWidened.ts, 17, 3))

var d = [undefined, x];
>d : Symbol(d, Decl(arrayLiteralWidened.ts, 19, 3), Decl(arrayLiteralWidened.ts, 20, 3), Decl(arrayLiteralWidened.ts, 21, 3))
>undefined : Symbol(undefined)
>x : Symbol(x, Decl(arrayLiteralWidened.ts, 17, 3))

29 changes: 29 additions & 0 deletions tests/baselines/reference/arrayLiteralWidened.types
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ var a = []; // any[]
>a : any[]
>[] : undefined[]

var a = [,,];
>a : any[]
>[,,] : undefined[]
> : undefined
> : undefined

var a = [null, null];
>a : any[]
>[null, null] : null[]
Expand Down Expand Up @@ -53,3 +59,26 @@ var c = [[[null]],[undefined]]
>[undefined] : undefined[]
>undefined : undefined

// no widening when one or more elements are non-widening

var x: undefined = undefined;
>x : undefined
>undefined : undefined

var d = [x];
>d : undefined[]
>[x] : undefined[]
>x : undefined

var d = [, x];
>d : undefined[]
>[, x] : undefined[]
> : undefined
>x : undefined

var d = [undefined, x];
>d : undefined[]
>[undefined, x] : undefined[]
>undefined : undefined
>x : undefined

Loading