Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
d8b0f23
Fixes error message when Type is used as a Namespace
mylesmegyesi Nov 16, 2016
5a9451a
Rename typingOptions.enableAutoDiscovery to typeAcquisition.enable
jramsay Nov 19, 2016
4ed3225
Add optimized getTypeOfExpression function
ahejlsberg Nov 20, 2016
d71c2dd
Accept new baselines
ahejlsberg Nov 20, 2016
c1c12c7
Add regression test
ahejlsberg Nov 20, 2016
70e130b
Maintain support for deprecated API typingOptions.enableAutoDiscovery
jramsay Nov 21, 2016
ab8d6c0
Port lib changes
mhegazy Nov 22, 2016
8a334ac
Add special handeling for function and array in Object.freeze
mhegazy Nov 22, 2016
72df02c
Add function
mhegazy Nov 22, 2016
6a5682c
Support JSDoc @augments tag
RyanCavanaugh Nov 22, 2016
103090b
report config errors when config file changed (#12372)
zhengbli Nov 22, 2016
86583d4
Merge pull request #12441 from RyanCavanaugh/jsDocAugments
RyanCavanaugh Nov 22, 2016
75a9435
Merge pull request #12433 from Microsoft/updatelibreducesymbols
mhegazy Nov 22, 2016
61204cc
Re-add sourceFiles to program emit callback
tlancina Nov 22, 2016
a8ef77c
Merge pull request #12445 from tlancina/master
mhegazy Nov 22, 2016
680fb2e
add typings installer user agent for npm requests (#12446)
vladima Nov 22, 2016
864e179
Merge pull request #12396 from Microsoft/getTypeOfExpression
ahejlsberg Nov 22, 2016
2b89d91
Addressing CR feedback
jramsay Nov 22, 2016
855431a
Merge pull request #12373 from Microsoft/RenameTypingOptions
jramsay Nov 23, 2016
4701eb7
Merge pull request #12357 from mylesmegyesi/12075-incorrectErrorMessa…
DanielRosenwasser Nov 23, 2016
6039556
Handel call and construct signatures
mhegazy Nov 23, 2016
15d870b
Use `Function` instead of call and construct signatures
mhegazy Nov 23, 2016
4e7313b
Instead of creating clone of the jsxFactory's leaf node, create synth…
sheetalkamat Nov 23, 2016
79bf477
Revert "Instead of creating clone of the jsxFactory's leaf node, crea…
sheetalkamat Nov 23, 2016
20a8a30
Instead of creating clone of the jsxFactory's leaf node, create synth…
sheetalkamat Nov 23, 2016
c05bf3b
respect casing when comparing names of config files (#12474)
vladima Nov 23, 2016
c2bef6d
Merge pull request #12475 from Microsoft/jsxFactoryLeaf
sheetalkamat Nov 23, 2016
72cee3e
Merge pull request #12434 from Microsoft/Fix12377
mhegazy Nov 23, 2016
730a687
Merge branch 'master' into release-2.1
mhegazy Nov 23, 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
75 changes: 53 additions & 22 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,7 @@ namespace ts {
if (!errorLocation ||
!checkAndReportErrorForMissingPrefix(errorLocation, name, nameArg) &&
!checkAndReportErrorForExtendingInterface(errorLocation) &&
!checkAndReportErrorForUsingTypeAsNamespace(errorLocation, name, meaning) &&
!checkAndReportErrorForUsingTypeAsValue(errorLocation, name, meaning)) {
error(errorLocation, nameNotFoundMessage, typeof nameArg === "string" ? nameArg : declarationNameToString(nameArg));
}
Expand Down Expand Up @@ -1032,6 +1033,18 @@ namespace ts {
}
}

function checkAndReportErrorForUsingTypeAsNamespace(errorLocation: Node, name: string, meaning: SymbolFlags): boolean {
if (meaning === SymbolFlags.Namespace) {
const symbol = resolveSymbol(resolveName(errorLocation, name, SymbolFlags.Type & ~SymbolFlags.Value, /*nameNotFoundMessage*/undefined, /*nameArg*/ undefined));
if (symbol) {
error(errorLocation, Diagnostics._0_only_refers_to_a_type_but_is_being_used_as_a_namespace_here, name);
return true;
}
}

return false;
}

function checkAndReportErrorForUsingTypeAsValue(errorLocation: Node, name: string, meaning: SymbolFlags): boolean {
if (meaning & (SymbolFlags.Value & ~SymbolFlags.NamespaceModule)) {
const symbol = resolveSymbol(resolveName(errorLocation, name, SymbolFlags.Type & ~SymbolFlags.Value, /*nameNotFoundMessage*/undefined, /*nameArg*/ undefined));
Expand Down Expand Up @@ -8975,7 +8988,7 @@ namespace ts {

function getTypeWithDefault(type: Type, defaultExpression: Expression) {
if (defaultExpression) {
const defaultType = checkExpression(defaultExpression);
const defaultType = getTypeOfExpression(defaultExpression);
return getUnionType([getTypeWithFacts(type, TypeFacts.NEUndefined), defaultType]);
}
return type;
Expand All @@ -9002,7 +9015,7 @@ namespace ts {
function getAssignedTypeOfBinaryExpression(node: BinaryExpression): Type {
return node.parent.kind === SyntaxKind.ArrayLiteralExpression || node.parent.kind === SyntaxKind.PropertyAssignment ?
getTypeWithDefault(getAssignedType(node), node.right) :
checkExpression(node.right);
getTypeOfExpression(node.right);
}

function getAssignedTypeOfArrayLiteralElement(node: ArrayLiteralExpression, element: Expression): Type {
Expand Down Expand Up @@ -9060,7 +9073,7 @@ namespace ts {
// from its initializer, we'll already have cached the type. Otherwise we compute it now
// without caching such that transient types are reflected.
const links = getNodeLinks(node);
return links.resolvedType || checkExpression(node);
return links.resolvedType || getTypeOfExpression(node);
}

function getInitialTypeOfVariableDeclaration(node: VariableDeclaration) {
Expand Down Expand Up @@ -9120,7 +9133,7 @@ namespace ts {

function getTypeOfSwitchClause(clause: CaseClause | DefaultClause) {
if (clause.kind === SyntaxKind.CaseClause) {
const caseType = getRegularTypeOfLiteralType(checkExpression((<CaseClause>clause).expression));
const caseType = getRegularTypeOfLiteralType(getTypeOfExpression((<CaseClause>clause).expression));
return isUnitType(caseType) ? caseType : undefined;
}
return neverType;
Expand Down Expand Up @@ -9225,7 +9238,7 @@ namespace ts {
// we defer subtype reduction until the evolving array type is finalized into a manifest
// array type.
function addEvolvingArrayElementType(evolvingArrayType: EvolvingArrayType, node: Expression): EvolvingArrayType {
const elementType = getBaseTypeOfLiteralType(checkExpression(node));
const elementType = getBaseTypeOfLiteralType(getTypeOfExpression(node));
return isTypeSubsetOf(elementType, evolvingArrayType.elementType) ? evolvingArrayType : getEvolvingArrayType(getUnionType([evolvingArrayType.elementType, elementType]));
}

Expand Down Expand Up @@ -9286,7 +9299,7 @@ namespace ts {
(<BinaryExpression>parent.parent).operatorToken.kind === SyntaxKind.EqualsToken &&
(<BinaryExpression>parent.parent).left === parent &&
!isAssignmentTarget(parent.parent) &&
isTypeAnyOrAllConstituentTypesHaveKind(checkExpression((<ElementAccessExpression>parent).argumentExpression), TypeFlags.NumberLike | TypeFlags.Undefined);
isTypeAnyOrAllConstituentTypesHaveKind(getTypeOfExpression((<ElementAccessExpression>parent).argumentExpression), TypeFlags.NumberLike | TypeFlags.Undefined);
return isLengthPushOrUnshift || isElementAssignment;
}

Expand Down Expand Up @@ -9448,7 +9461,7 @@ namespace ts {
}
}
else {
const indexType = checkExpression((<ElementAccessExpression>(<BinaryExpression>node).left).argumentExpression);
const indexType = getTypeOfExpression((<ElementAccessExpression>(<BinaryExpression>node).left).argumentExpression);
if (isTypeAnyOrAllConstituentTypesHaveKind(indexType, TypeFlags.NumberLike | TypeFlags.Undefined)) {
evolvedType = addEvolvingArrayElementType(evolvedType, (<BinaryExpression>node).right);
}
Expand Down Expand Up @@ -9673,7 +9686,7 @@ namespace ts {
if (operator === SyntaxKind.ExclamationEqualsToken || operator === SyntaxKind.ExclamationEqualsEqualsToken) {
assumeTrue = !assumeTrue;
}
const valueType = checkExpression(value);
const valueType = getTypeOfExpression(value);
if (valueType.flags & TypeFlags.Nullable) {
if (!strictNullChecks) {
return type;
Expand Down Expand Up @@ -9760,7 +9773,7 @@ namespace ts {
}

// Check that right operand is a function type with a prototype property
const rightType = checkExpression(expr.right);
const rightType = getTypeOfExpression(expr.right);
if (!isTypeSubtypeOf(rightType, globalFunctionType)) {
return type;
}
Expand Down Expand Up @@ -9901,7 +9914,7 @@ namespace ts {
location = location.parent;
}
if (isPartOfExpression(location) && !isAssignmentTarget(location)) {
const type = checkExpression(<Expression>location);
const type = getTypeOfExpression(<Expression>location);
if (getExportSymbolOfValueSymbolIfExported(getNodeLinks(location).resolvedSymbol) === symbol) {
return type;
}
Expand Down Expand Up @@ -10769,15 +10782,15 @@ namespace ts {

// In an assignment expression, the right operand is contextually typed by the type of the left operand.
if (node === binaryExpression.right) {
return checkExpression(binaryExpression.left);
return getTypeOfExpression(binaryExpression.left);
}
}
else if (operator === SyntaxKind.BarBarToken) {
// When an || expression has a contextual type, the operands are contextually typed by that type. When an ||
// expression has no contextual type, the right operand is contextually typed by the type of the left operand.
let type = getContextualType(binaryExpression);
if (!type && node === binaryExpression.right) {
type = checkExpression(binaryExpression.left);
type = getTypeOfExpression(binaryExpression.left);
}
return type;
}
Expand Down Expand Up @@ -12148,7 +12161,7 @@ namespace ts {
if (node.kind === SyntaxKind.ForInStatement &&
child === (<ForInStatement>node).statement &&
getForInVariableSymbol(<ForInStatement>node) === symbol &&
hasNumericPropertyNames(checkExpression((<ForInStatement>node).expression))) {
hasNumericPropertyNames(getTypeOfExpression((<ForInStatement>node).expression))) {
return true;
}
child = node;
Expand Down Expand Up @@ -13784,7 +13797,7 @@ namespace ts {
if (!node.possiblyExhaustive) {
return false;
}
const type = checkExpression(node.expression);
const type = getTypeOfExpression(node.expression);
if (!isLiteralType(type)) {
return false;
}
Expand Down Expand Up @@ -14876,6 +14889,24 @@ namespace ts {
return type;
}

// Returns the type of an expression. Unlike checkExpression, this function is simply concerned
// with computing the type and may not fully check all contained sub-expressions for errors.
function getTypeOfExpression(node: Expression) {
// Optimize for the common case of a call to a function with a single non-generic call
// signature where we can just fetch the return type without checking the arguments.
if (node.kind === SyntaxKind.CallExpression && (<CallExpression>node).expression.kind !== SyntaxKind.SuperKeyword) {
const funcType = checkNonNullExpression((<CallExpression>node).expression);
const signature = getSingleCallSignature(funcType);
if (signature && !signature.typeParameters) {
return getReturnTypeOfSignature(signature);
}
}
// Otherwise simply call checkExpression. Ideally, the entire family of checkXXX functions
// should have a parameter that indicates whether full error checking is required such that
// we can perform the optimizations locally.
return checkExpression(node);
}

// Checks an expression and returns its type. The contextualMapper parameter serves two purposes: When
// contextualMapper is not undefined and not equal to the identityMapper function object it indicates that the
// expression is being inferentially typed (section 4.15.2 in spec) and provides the type mapper to use in
Expand Down Expand Up @@ -18258,7 +18289,7 @@ namespace ts {
}
}

enumType = checkExpression(expression);
enumType = getTypeOfExpression(expression);
// allow references to constant members of other enums
if (!(enumType.symbol && (enumType.symbol.flags & SymbolFlags.Enum))) {
return undefined;
Expand Down Expand Up @@ -19428,7 +19459,7 @@ namespace ts {
// fallthrough

case SyntaxKind.SuperKeyword:
const type = isPartOfExpression(node) ? checkExpression(<Expression>node) : getTypeFromTypeNode(<TypeNode>node);
const type = isPartOfExpression(node) ? getTypeOfExpression(<Expression>node) : getTypeFromTypeNode(<TypeNode>node);
return type.symbol;

case SyntaxKind.ThisType:
Expand Down Expand Up @@ -19458,7 +19489,7 @@ namespace ts {
case SyntaxKind.NumericLiteral:
// index access
if (node.parent.kind === SyntaxKind.ElementAccessExpression && (<ElementAccessExpression>node.parent).argumentExpression === node) {
const objectType = checkExpression((<ElementAccessExpression>node.parent).expression);
const objectType = getTypeOfExpression((<ElementAccessExpression>node.parent).expression);
if (objectType === unknownType) return undefined;
const apparentType = getApparentType(objectType);
if (apparentType === unknownType) return undefined;
Expand Down Expand Up @@ -19497,7 +19528,7 @@ namespace ts {
}

if (isPartOfExpression(node)) {
return getTypeOfExpression(<Expression>node);
return getRegularTypeOfExpression(<Expression>node);
}

if (isExpressionWithTypeArgumentsInClassExtendsClause(node)) {
Expand Down Expand Up @@ -19559,7 +19590,7 @@ namespace ts {
// If this is from "for" initializer
// for ({a } = elems[0];.....) { }
if (expr.parent.kind === SyntaxKind.BinaryExpression) {
const iteratedType = checkExpression((<BinaryExpression>expr.parent).right);
const iteratedType = getTypeOfExpression((<BinaryExpression>expr.parent).right);
return checkDestructuringAssignment(expr, iteratedType || unknownType);
}
// If this is from nested object binding pattern
Expand Down Expand Up @@ -19589,11 +19620,11 @@ namespace ts {
return typeOfObjectLiteral && getPropertyOfType(typeOfObjectLiteral, location.text);
}

function getTypeOfExpression(expr: Expression): Type {
function getRegularTypeOfExpression(expr: Expression): Type {
if (isRightSideOfQualifiedNameOrPropertyAccess(expr)) {
expr = <Expression>expr.parent;
}
return getRegularTypeOfLiteralType(checkExpression(expr));
return getRegularTypeOfLiteralType(getTypeOfExpression(expr));
}

/**
Expand Down Expand Up @@ -20020,7 +20051,7 @@ namespace ts {
}

function writeTypeOfExpression(expr: Expression, enclosingDeclaration: Node, flags: TypeFormatFlags, writer: SymbolWriter) {
const type = getWidenedType(getTypeOfExpression(expr));
const type = getWidenedType(getRegularTypeOfExpression(expr));
getSymbolDisplayBuilder().buildTypeDisplay(type, writer, enclosingDeclaration, flags);
}

Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1995,6 +1995,10 @@
"category": "Error",
"code": 2701
},
"'{0}' only refers to a type, but is being used as a namespace here.": {
"category": "Error",
"code": 2702
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ namespace ts {

// Write the source map
if (compilerOptions.sourceMap && !compilerOptions.inlineSourceMap) {
writeFile(host, emitterDiagnostics, sourceMapFilePath, sourceMap.getText(), /*writeByteOrderMark*/ false);
writeFile(host, emitterDiagnostics, sourceMapFilePath, sourceMap.getText(), /*writeByteOrderMark*/ false, sourceFiles);
}

// Record source map data for the test harness.
Expand All @@ -152,7 +152,7 @@ namespace ts {
}

// Write the output file
writeFile(host, emitterDiagnostics, jsFilePath, writer.getText(), compilerOptions.emitBOM);
writeFile(host, emitterDiagnostics, jsFilePath, writer.getText(), compilerOptions.emitBOM, sourceFiles);

// Reset state
sourceMap.reset();
Expand Down
Loading