merge with origin/master, add trace message with type of 'typings' field is not 'string'

This commit is contained in:
Vladimir Matveev 2015-12-07 11:40:35 -08:00
commit 2dbf621e97
23 changed files with 308 additions and 54 deletions

View file

@ -6148,14 +6148,25 @@ namespace ts {
function inferFromTypes(source: Type, target: Type) {
if (source.flags & TypeFlags.Union && target.flags & TypeFlags.Union ||
source.flags & TypeFlags.Intersection && target.flags & TypeFlags.Intersection) {
// Source and target are both unions or both intersections. To improve the quality of
// inferences we first reduce the types by removing constituents that are identically
// matched by a constituent in the other type. For example, when inferring from
// 'string | string[]' to 'string | T', we reduce the types to 'string[]' and 'T'.
const reducedSource = reduceUnionOrIntersectionType(<UnionOrIntersectionType>source, <UnionOrIntersectionType>target);
const reducedTarget = reduceUnionOrIntersectionType(<UnionOrIntersectionType>target, <UnionOrIntersectionType>source);
source = reducedSource;
target = reducedTarget;
// Source and target are both unions or both intersections. First, find each
// target constituent type that has an identically matching source constituent
// type, and for each such target constituent type infer from the type to itself.
// When inferring from a type to itself we effectively find all type parameter
// occurrences within that type and infer themselves as their type arguments.
let matchingTypes: Type[];
for (const t of (<UnionOrIntersectionType>target).types) {
if (typeIdenticalToSomeType(t, (<UnionOrIntersectionType>source).types)) {
(matchingTypes || (matchingTypes = [])).push(t);
inferFromTypes(t, t);
}
}
// Next, to improve the quality of inferences, reduce the source and target types by
// removing the identically matched constituents. For example, when inferring from
// 'string | string[]' to 'string | T' we reduce the types to 'string[]' and 'T'.
if (matchingTypes) {
source = removeTypesFromUnionOrIntersection(<UnionOrIntersectionType>source, matchingTypes);
target = removeTypesFromUnionOrIntersection(<UnionOrIntersectionType>target, matchingTypes);
}
}
if (target.flags & TypeFlags.TypeParameter) {
// If target is a type parameter, make an inference, unless the source type contains
@ -6317,9 +6328,9 @@ namespace ts {
}
}
function typeIdenticalToSomeType(source: Type, target: UnionOrIntersectionType): boolean {
for (const t of target.types) {
if (isTypeIdenticalTo(source, t)) {
function typeIdenticalToSomeType(type: Type, types: Type[]): boolean {
for (const t of types) {
if (isTypeIdenticalTo(t, type)) {
return true;
}
}
@ -6327,29 +6338,17 @@ namespace ts {
}
/**
* Return the reduced form of the source type. This type is computed by by removing all source
* constituents that have an identical match in the target type.
* Return a new union or intersection type computed by removing a given set of types
* from a given union or intersection type.
*/
function reduceUnionOrIntersectionType(source: UnionOrIntersectionType, target: UnionOrIntersectionType) {
let sourceTypes = source.types;
let sourceIndex = 0;
let modified = false;
while (sourceIndex < sourceTypes.length) {
if (typeIdenticalToSomeType(sourceTypes[sourceIndex], target)) {
if (!modified) {
sourceTypes = sourceTypes.slice(0);
modified = true;
}
sourceTypes.splice(sourceIndex, 1);
}
else {
sourceIndex++;
function removeTypesFromUnionOrIntersection(type: UnionOrIntersectionType, typesToRemove: Type[]) {
const reducedTypes: Type[] = [];
for (const t of type.types) {
if (!typeIdenticalToSomeType(t, typesToRemove)) {
reducedTypes.push(t);
}
}
if (modified) {
return source.flags & TypeFlags.Union ? getUnionType(sourceTypes, /*noSubtypeReduction*/ true) : getIntersectionType(sourceTypes);
}
return source;
return type.flags & TypeFlags.Union ? getUnionType(reducedTypes, /*noSubtypeReduction*/ true) : getIntersectionType(reducedTypes);
}
function getInferenceCandidates(context: InferenceContext, index: number): Type[] {
@ -15493,6 +15492,11 @@ namespace ts {
let flags = 0;
for (const modifier of node.modifiers) {
switch (modifier.kind) {
case SyntaxKind.ConstKeyword:
if (node.kind !== SyntaxKind.EnumDeclaration && node.parent.kind === SyntaxKind.ClassDeclaration) {
return grammarErrorOnNode(node, Diagnostics.A_class_member_cannot_have_the_0_keyword, tokenToString(SyntaxKind.ConstKeyword));
}
break;
case SyntaxKind.PublicKeyword:
case SyntaxKind.ProtectedKeyword:
case SyntaxKind.PrivateKeyword:

View file

@ -791,7 +791,10 @@
"category": "Error",
"code": 1247
},
"A class member cannot have the '{0}' keyword.": {
"category": "Error",
"code": 1248
},
"'with' statements are not allowed in an async function block.": {
"category": "Error",
"code": 1300
@ -2507,6 +2510,10 @@
"Checking if '{0}' is the longest matching prefix for '{1}' - '{2}'.": {
"category": "Message",
"code": 6112
},
"Expected type of 'typings' field in 'package.json' to be 'string', got '{0}'.": {
"category": "Message",
"code": 6113
},
"Variable '{0}' implicitly has an '{1}' type.": {
"category": "Error",

View file

@ -1134,6 +1134,14 @@ namespace ts {
return token === t && tryParse(nextTokenCanFollowModifier);
}
function nextTokenIsOnSameLineAndCanFollowModifier() {
nextToken();
if (scanner.hasPrecedingLineBreak()) {
return false;
}
return canFollowModifier();
}
function nextTokenCanFollowModifier() {
if (token === SyntaxKind.ConstKeyword) {
// 'const' is only a modifier if followed by 'enum'.
@ -1154,11 +1162,7 @@ namespace ts {
return canFollowModifier();
}
nextToken();
if (scanner.hasPrecedingLineBreak()) {
return false;
}
return canFollowModifier();
return nextTokenIsOnSameLineAndCanFollowModifier();
}
function parseAnyContextualModifier(): boolean {
@ -4923,15 +4927,31 @@ namespace ts {
return decorators;
}
function parseModifiers(): ModifiersArray {
/*
* There are situations in which a modifier like 'const' will appear unexpectedly, such as on a class member.
* In those situations, if we are entirely sure that 'const' is not valid on its own (such as when ASI takes effect
* and turns it into a standalone declaration), then it is better to parse it and report an error later.
*
* In such situations, 'permitInvalidConstAsModifier' should be set to true.
*/
function parseModifiers(permitInvalidConstAsModifier?: boolean): ModifiersArray {
let flags = 0;
let modifiers: ModifiersArray;
while (true) {
const modifierStart = scanner.getStartPos();
const modifierKind = token;
if (!parseAnyContextualModifier()) {
break;
if (token === SyntaxKind.ConstKeyword && permitInvalidConstAsModifier) {
// We need to ensure that any subsequent modifiers appear on the same line
// so that when 'const' is a standalone declaration, we don't issue an error.
if (!tryParse(nextTokenIsOnSameLineAndCanFollowModifier)) {
break;
}
}
else {
if (!parseAnyContextualModifier()) {
break;
}
}
if (!modifiers) {
@ -4976,7 +4996,7 @@ namespace ts {
const fullStart = getNodePos();
const decorators = parseDecorators();
const modifiers = parseModifiers();
const modifiers = parseModifiers(/*permitInvalidConstAsModifier*/ true);
const accessor = tryParseAccessorDeclaration(fullStart, decorators, modifiers);
if (accessor) {

View file

@ -429,13 +429,18 @@ namespace ts {
}
if (jsonContent.typings) {
const typingsFile = normalizePath(combinePaths(candidate, jsonContent.typings));
if (traceEnabled) {
trace(host, Diagnostics.package_json_has_typings_field_0_that_references_1, jsonContent.typings, typingsFile);
if (typeof jsonContent.typings === "string") {
const typingsFile = normalizePath(combinePaths(candidate, jsonContent.typings));
if (traceEnabled) {
trace(host, Diagnostics.package_json_has_typings_field_0_that_references_1, jsonContent.typings, typingsFile);
}
const result = loadModuleFromFile(extensions, typingsFile, failedLookupLocation, host, traceEnabled);
if (result) {
return result;
}
}
const result = loadModuleFromFile(extensions, typingsFile , failedLookupLocation, host, traceEnabled);
if (result) {
return result;
else if (traceEnabled) {
trace(host, Diagnostics.Expected_type_of_typings_field_in_package_json_to_be_string_got_0, typeof jsonContent.typings);
}
}
else {
@ -446,7 +451,7 @@ namespace ts {
}
else {
if (traceEnabled) {
trace(host, Diagnostics.package_json_does_not_have_typings_field);
trace(host, Diagnostics.File_0_does_not_exist, packageJsonPath);
}
// record package json as one of failed lookup locations - in the future if this file will appear it will invalidate resolution results
failedLookupLocation.push(packageJsonPath);

13
src/lib/es6.d.ts vendored
View file

@ -1224,7 +1224,7 @@ declare namespace Reflect {
function isExtensible(target: any): boolean;
function ownKeys(target: any): Array<PropertyKey>;
function preventExtensions(target: any): boolean;
function set(target: any, propertyKey: PropertyKey, value: any, receiver? :any): boolean;
function set(target: any, propertyKey: PropertyKey, value: any, receiver?: any): boolean;
function setPrototypeOf(target: any, proto: any): boolean;
}
@ -1272,7 +1272,16 @@ interface PromiseConstructor {
* @param values An array of Promises.
* @returns A new Promise.
*/
all<T>(values: Iterable<T | PromiseLike<T>>): Promise<T[]>;
all<T1, T2>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>]): Promise<[T1, T2]>;
all<T1, T2, T3>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>]): Promise<[T1, T2, T3]>;
all<T1, T2, T3, T4>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>]): Promise<[T1, T2, T3, T4]>;
all<T1, T2, T3, T4, T5>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>]): Promise<[T1, T2, T3, T4, T5]>;
all<T1, T2, T3, T4, T5, T6>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>]): Promise<[T1, T2, T3, T4, T5, T6]>;
all<T1, T2, T3, T4, T5, T6, T7>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>]): Promise<[T1, T2, T3, T4, T5, T6, T7]>;
all<T1, T2, T3, T4, T5, T6, T7, T8>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8]>;
all<T1, T2, T3, T4, T5, T6, T7, T8, T9>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>;
all<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>, T10 | PromiseLike<T10>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>;
all<TAll>(values: Iterable<TAll | PromiseLike<TAll>>): Promise<TAll[]>;
/**
* Creates a Promise that is resolved or rejected when any of the provided Promises are resolved

View file

@ -461,6 +461,7 @@ namespace ts.formatting {
case SyntaxKind.ParenthesizedType:
case SyntaxKind.TaggedTemplateExpression:
case SyntaxKind.AwaitExpression:
case SyntaxKind.NamedImports:
return true;
}
return false;

View file

@ -0,0 +1,23 @@
tests/cases/compiler/ClassDeclaration26.ts(2,22): error TS1005: ';' expected.
tests/cases/compiler/ClassDeclaration26.ts(4,5): error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
tests/cases/compiler/ClassDeclaration26.ts(4,20): error TS1005: '=' expected.
tests/cases/compiler/ClassDeclaration26.ts(4,23): error TS1005: '=>' expected.
tests/cases/compiler/ClassDeclaration26.ts(5,1): error TS1128: Declaration or statement expected.
==== tests/cases/compiler/ClassDeclaration26.ts (5 errors) ====
class C {
public const var export foo = 10;
~~~~~~
!!! error TS1005: ';' expected.
var constructor() { }
~~~
!!! error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
~
!!! error TS1005: '=' expected.
~
!!! error TS1005: '=>' expected.
}
~
!!! error TS1128: Declaration or statement expected.

View file

@ -0,0 +1,15 @@
//// [ClassDeclaration26.ts]
class C {
public const var export foo = 10;
var constructor() { }
}
//// [ClassDeclaration26.js]
var C = (function () {
function C() {
this.foo = 10;
}
return C;
})();
var constructor = function () { };

View file

@ -0,0 +1,9 @@
tests/cases/compiler/ClassDeclarationWithInvalidConstOnPropertyDeclaration.ts(2,3): error TS1248: A class member cannot have the 'const' keyword.
==== tests/cases/compiler/ClassDeclarationWithInvalidConstOnPropertyDeclaration.ts (1 errors) ====
class AtomicNumbers {
static const H = 1;
~~~~~~~~~~~~~~~~~~~
!!! error TS1248: A class member cannot have the 'const' keyword.
}

View file

@ -0,0 +1,12 @@
//// [ClassDeclarationWithInvalidConstOnPropertyDeclaration.ts]
class AtomicNumbers {
static const H = 1;
}
//// [ClassDeclarationWithInvalidConstOnPropertyDeclaration.js]
var AtomicNumbers = (function () {
function AtomicNumbers() {
}
AtomicNumbers.H = 1;
return AtomicNumbers;
})();

View file

@ -0,0 +1,13 @@
//// [ClassDeclarationWithInvalidConstOnPropertyDeclaration2.ts]
class C {
const
x = 10;
}
//// [ClassDeclarationWithInvalidConstOnPropertyDeclaration2.js]
var C = (function () {
function C() {
this.x = 10;
}
return C;
})();

View file

@ -0,0 +1,10 @@
=== tests/cases/compiler/ClassDeclarationWithInvalidConstOnPropertyDeclaration2.ts ===
class C {
>C : Symbol(C, Decl(ClassDeclarationWithInvalidConstOnPropertyDeclaration2.ts, 0, 0))
const
>const : Symbol(const, Decl(ClassDeclarationWithInvalidConstOnPropertyDeclaration2.ts, 0, 9))
x = 10;
>x : Symbol(x, Decl(ClassDeclarationWithInvalidConstOnPropertyDeclaration2.ts, 1, 9))
}

View file

@ -0,0 +1,11 @@
=== tests/cases/compiler/ClassDeclarationWithInvalidConstOnPropertyDeclaration2.ts ===
class C {
>C : C
const
>const : any
x = 10;
>x : number
>10 : number
}

View file

@ -0,0 +1,14 @@
//// [recursiveUnionTypeInference.ts]
interface Foo<T> {
x: T;
}
function bar<T>(x: Foo<T> | string): T {
return bar(x);
}
//// [recursiveUnionTypeInference.js]
function bar(x) {
return bar(x);
}

View file

@ -0,0 +1,23 @@
=== tests/cases/compiler/recursiveUnionTypeInference.ts ===
interface Foo<T> {
>Foo : Symbol(Foo, Decl(recursiveUnionTypeInference.ts, 0, 0))
>T : Symbol(T, Decl(recursiveUnionTypeInference.ts, 0, 14))
x: T;
>x : Symbol(x, Decl(recursiveUnionTypeInference.ts, 0, 18))
>T : Symbol(T, Decl(recursiveUnionTypeInference.ts, 0, 14))
}
function bar<T>(x: Foo<T> | string): T {
>bar : Symbol(bar, Decl(recursiveUnionTypeInference.ts, 2, 1))
>T : Symbol(T, Decl(recursiveUnionTypeInference.ts, 4, 13))
>x : Symbol(x, Decl(recursiveUnionTypeInference.ts, 4, 16))
>Foo : Symbol(Foo, Decl(recursiveUnionTypeInference.ts, 0, 0))
>T : Symbol(T, Decl(recursiveUnionTypeInference.ts, 4, 13))
>T : Symbol(T, Decl(recursiveUnionTypeInference.ts, 4, 13))
return bar(x);
>bar : Symbol(bar, Decl(recursiveUnionTypeInference.ts, 2, 1))
>x : Symbol(x, Decl(recursiveUnionTypeInference.ts, 4, 16))
}

View file

@ -0,0 +1,24 @@
=== tests/cases/compiler/recursiveUnionTypeInference.ts ===
interface Foo<T> {
>Foo : Foo<T>
>T : T
x: T;
>x : T
>T : T
}
function bar<T>(x: Foo<T> | string): T {
>bar : <T>(x: Foo<T> | string) => T
>T : T
>x : Foo<T> | string
>Foo : Foo<T>
>T : T
>T : T
return bar(x);
>bar(x) : T
>bar : <T>(x: Foo<T> | string) => T
>x : Foo<T> | string
}

View file

@ -0,0 +1,5 @@
class C {
public const var export foo = 10;
var constructor() { }
}

View file

@ -0,0 +1,3 @@
class AtomicNumbers {
static const H = 1;
}

View file

@ -0,0 +1,4 @@
class C {
const
x = 10;
}

View file

@ -0,0 +1,7 @@
interface Foo<T> {
x: T;
}
function bar<T>(x: Foo<T> | string): T {
return bar(x);
}

View file

@ -1,12 +1,14 @@
/// <reference path='fourslash.ts' />
////export const class C {
//// private static c/*1*/onst foo;
//// constructor(public con/*2*/st foo) {
//// private static c/*1*/onst f/*2*/oo;
//// constructor(public con/*3*/st foo) {
//// }
////}
goTo.marker("1");
verify.occurrencesAtPositionCount(1);
verify.occurrencesAtPositionCount(0);
goTo.marker("2");
verify.occurrencesAtPositionCount(1);
goTo.marker("3");
verify.occurrencesAtPositionCount(0);

View file

@ -0,0 +1,12 @@
/// <reference path='fourslash.ts' />
////import {/*0*/
//// numbers as bn,/*1*/
//// list/*2*/
////} from '@bykov/basics';/*3*/
format.document();
goTo.marker("0"); verify.currentLineContentIs("import {");
goTo.marker("1"); verify.currentLineContentIs(" numbers as bn,");
goTo.marker("2"); verify.currentLineContentIs(" list");
goTo.marker("3"); verify.currentLineContentIs("} from '@bykov/basics';");

View file

@ -99,13 +99,34 @@ module ts {
assert.equal(resolution.failedLookupLocations.length, supportedTypeScriptExtensions.length);
}
it("module name as directory - load from typings", () => {
it("module name as directory - load from 'typings'", () => {
testLoadingFromPackageJson("/a/b/c/d.ts", "/a/b/c/bar/package.json", "c/d/e.d.ts", "/a/b/c/bar/c/d/e.d.ts", "./bar");
testLoadingFromPackageJson("/a/b/c/d.ts", "/a/bar/package.json", "e.d.ts", "/a/bar/e.d.ts", "../../bar");
testLoadingFromPackageJson("/a/b/c/d.ts", "/bar/package.json", "e.d.ts", "/bar/e.d.ts", "/bar");
testLoadingFromPackageJson("c:/a/b/c/d.ts", "c:/bar/package.json", "e.d.ts", "c:/bar/e.d.ts", "c:/bar");
});
function testTypingsIgnored(typings: any): void {
let containingFile = { name: "/a/b.ts" };
let packageJson = { name: "/node_modules/b/package.json", content: JSON.stringify({ "typings": typings }) };
let moduleFile = { name: "/a/b.d.ts" };
let indexPath = "/node_modules/b/index.d.ts";
let indexFile = { name: indexPath }
let resolution = nodeModuleNameResolver("b", containingFile.name, {}, createModuleResolutionHost(containingFile, packageJson, moduleFile, indexFile));
assert.equal(resolution.resolvedModule.resolvedFileName, indexPath);
}
it("module name as directory - handle invalid 'typings'", () => {
testTypingsIgnored(["a", "b"]);
testTypingsIgnored({ "a": "b" });
testTypingsIgnored(true);
testTypingsIgnored(null);
testTypingsIgnored(undefined);
});
it("module name as directory - load index.d.ts", () => {
let containingFile = { name: "/a/b/c.ts" };
let packageJson = { name: "/a/b/foo/package.json", content: JSON.stringify({ main: "/c/d" }) };