Skip to content

Commit 3ed8bca

Browse files
author
Kanchalai Tanglertsampan
committed
Merge branch 'master' into release-2.0
2 parents 1357606 + 0041d5c commit 3ed8bca

27 files changed

+251
-14
lines changed

src/compiler/checker.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,8 +1023,8 @@ namespace ts {
10231023
}
10241024
}
10251025

1026-
function getDeclarationOfAliasSymbol(symbol: Symbol): Declaration {
1027-
return findMap(symbol.declarations, d => isAliasSymbolDeclaration(d) ? d : undefined);
1026+
function getDeclarationOfAliasSymbol(symbol: Symbol): Declaration | undefined {
1027+
return forEach(symbol.declarations, d => isAliasSymbolDeclaration(d) ? d : undefined);
10281028
}
10291029

10301030
function getTargetOfImportEqualsDeclaration(node: ImportEqualsDeclaration): Symbol {
@@ -1191,6 +1191,7 @@ namespace ts {
11911191
if (!links.target) {
11921192
links.target = resolvingSymbol;
11931193
const node = getDeclarationOfAliasSymbol(symbol);
1194+
Debug.assert(!!node);
11941195
const target = getTargetOfAliasDeclaration(node);
11951196
if (links.target === resolvingSymbol) {
11961197
links.target = target || unknownSymbol;
@@ -1226,6 +1227,7 @@ namespace ts {
12261227
if (!links.referenced) {
12271228
links.referenced = true;
12281229
const node = getDeclarationOfAliasSymbol(symbol);
1230+
Debug.assert(!!node);
12291231
if (node.kind === SyntaxKind.ExportAssignment) {
12301232
// export default <symbol>
12311233
checkExpressionCached((<ExportAssignment>node).expression);
@@ -6301,6 +6303,18 @@ namespace ts {
63016303
reportError(message, sourceType, targetType);
63026304
}
63036305

6306+
function tryElaborateErrorsForPrimitivesAndObjects(source: Type, target: Type) {
6307+
const sourceType = typeToString(source);
6308+
const targetType = typeToString(target);
6309+
6310+
if ((globalStringType === source && stringType === target) ||
6311+
(globalNumberType === source && numberType === target) ||
6312+
(globalBooleanType === source && booleanType === target) ||
6313+
(getGlobalESSymbolType() === source && esSymbolType === target)) {
6314+
reportError(Diagnostics._0_is_a_primitive_but_1_is_a_wrapper_object_Prefer_using_0_when_possible, targetType, sourceType);
6315+
}
6316+
}
6317+
63046318
// Compare two types and return
63056319
// Ternary.True if they are related with no assumptions,
63066320
// Ternary.Maybe if they are related with assumptions of other relationships, or
@@ -6424,6 +6438,9 @@ namespace ts {
64246438
}
64256439

64266440
if (reportErrors) {
6441+
if (source.flags & TypeFlags.ObjectType && target.flags & TypeFlags.Primitive) {
6442+
tryElaborateErrorsForPrimitivesAndObjects(source, target);
6443+
}
64276444
reportRelationError(headMessage, source, target);
64286445
}
64296446
return Ternary.False;

src/compiler/commandLineParser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ namespace ts {
885885
function convertCompilerOptionsFromJsonWorker(jsonOptions: any,
886886
basePath: string, errors: Diagnostic[], configFileName?: string): CompilerOptions {
887887

888-
const options: CompilerOptions = getBaseFileName(configFileName) === "jsconfig.json" ? { allowJs: true } : {};
888+
const options: CompilerOptions = getBaseFileName(configFileName) === "jsconfig.json" ? { allowJs: true, maxNodeModuleJsDepth: 2 } : {};
889889
convertOptionsFromJson(optionDeclarations, jsonOptions, basePath, options, Diagnostics.Unknown_compiler_option_0, errors);
890890
return options;
891891
}

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,6 +1955,10 @@
19551955
"category": "Error",
19561956
"code": 2691
19571957
},
1958+
"'{0}' is a primitive, but '{1}' is a wrapper object. Prefer using '{0}' when possible.": {
1959+
"category": "Error",
1960+
"code": 2692
1961+
},
19581962
"Import declaration '{0}' is using private name '{1}'.": {
19591963
"category": "Error",
19601964
"code": 4000

src/compiler/program.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,7 @@ namespace ts {
11031103
// - This calls resolveModuleNames, and then calls findSourceFile for each resolved module.
11041104
// As all these operations happen - and are nested - within the createProgram call, they close over the below variables.
11051105
// The current resolution depth is tracked by incrementing/decrementing as the depth first search progresses.
1106-
const maxNodeModulesJsDepth = typeof options.maxNodeModuleJsDepth === "number" ? options.maxNodeModuleJsDepth : 2;
1106+
const maxNodeModulesJsDepth = typeof options.maxNodeModuleJsDepth === "number" ? options.maxNodeModuleJsDepth : 0;
11071107
let currentNodeModulesDepth = 0;
11081108

11091109
// If a module has some of its imports skipped due to being at the depth limit under node_modules, then track

src/harness/unittests/convertCompilerOptionsFromJson.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ namespace ts {
403403
{
404404
compilerOptions: <CompilerOptions>{
405405
allowJs: true,
406+
maxNodeModuleJsDepth: 2,
406407
module: ModuleKind.CommonJS,
407408
target: ScriptTarget.ES5,
408409
noImplicitAny: false,
@@ -429,6 +430,7 @@ namespace ts {
429430
{
430431
compilerOptions: <CompilerOptions>{
431432
allowJs: false,
433+
maxNodeModuleJsDepth: 2,
432434
module: ModuleKind.CommonJS,
433435
target: ScriptTarget.ES5,
434436
noImplicitAny: false,
@@ -450,7 +452,8 @@ namespace ts {
450452
{
451453
compilerOptions:
452454
{
453-
allowJs: true
455+
allowJs: true,
456+
maxNodeModuleJsDepth: 2
454457
},
455458
errors: [{
456459
file: undefined,
@@ -469,7 +472,8 @@ namespace ts {
469472
{
470473
compilerOptions:
471474
{
472-
allowJs: true
475+
allowJs: true,
476+
maxNodeModuleJsDepth: 2
473477
},
474478
errors: <Diagnostic[]>[]
475479
}

src/services/services.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,14 @@ namespace ts {
280280
let pos = this.pos;
281281
const useJSDocScanner = this.kind >= SyntaxKind.FirstJSDocTagNode && this.kind <= SyntaxKind.LastJSDocTagNode;
282282
const processNode = (node: Node) => {
283-
if (pos < node.pos) {
283+
const isJSDocTagNode = isJSDocTag(node);
284+
if (!isJSDocTagNode && pos < node.pos) {
284285
pos = this.addSyntheticNodes(children, pos, node.pos, useJSDocScanner);
285286
}
286287
children.push(node);
287-
pos = node.end;
288+
if (!isJSDocTagNode) {
289+
pos = node.end;
290+
}
288291
};
289292
const processNodes = (nodes: NodeArray<Node>) => {
290293
if (pos < nodes.pos) {
@@ -299,10 +302,6 @@ namespace ts {
299302
processNode(jsDocComment);
300303
}
301304
}
302-
// For syntactic classifications, all trivia are classcified together, including jsdoc comments.
303-
// For that to work, the jsdoc comments should still be the leading trivia of the first child.
304-
// Restoring the scanner position ensures that.
305-
pos = this.pos;
306305
forEachChild(this, processNode, processNodes);
307306
if (pos < this.end) {
308307
this.addSyntheticNodes(children, pos, this.end);

tests/baselines/reference/apparentTypeSubtyping.errors.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
tests/cases/conformance/types/typeRelationships/apparentType/apparentTypeSubtyping.ts(9,7): error TS2415: Class 'Derived<U>' incorrectly extends base class 'Base<string>'.
22
Types of property 'x' are incompatible.
33
Type 'String' is not assignable to type 'string'.
4+
'string' is a primitive, but 'String' is a wrapper object. Prefer using 'string' when possible.
45

56

67
==== tests/cases/conformance/types/typeRelationships/apparentType/apparentTypeSubtyping.ts (1 errors) ====
@@ -17,6 +18,7 @@ tests/cases/conformance/types/typeRelationships/apparentType/apparentTypeSubtypi
1718
!!! error TS2415: Class 'Derived<U>' incorrectly extends base class 'Base<string>'.
1819
!!! error TS2415: Types of property 'x' are incompatible.
1920
!!! error TS2415: Type 'String' is not assignable to type 'string'.
21+
!!! error TS2415: 'string' is a primitive, but 'String' is a wrapper object. Prefer using 'string' when possible.
2022
x: String;
2123
}
2224

tests/baselines/reference/apparentTypeSupertype.errors.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ tests/cases/conformance/types/typeRelationships/apparentType/apparentTypeSuperty
22
Types of property 'x' are incompatible.
33
Type 'U' is not assignable to type 'string'.
44
Type 'String' is not assignable to type 'string'.
5+
'string' is a primitive, but 'String' is a wrapper object. Prefer using 'string' when possible.
56

67

78
==== tests/cases/conformance/types/typeRelationships/apparentType/apparentTypeSupertype.ts (1 errors) ====
@@ -19,5 +20,6 @@ tests/cases/conformance/types/typeRelationships/apparentType/apparentTypeSuperty
1920
!!! error TS2415: Types of property 'x' are incompatible.
2021
!!! error TS2415: Type 'U' is not assignable to type 'string'.
2122
!!! error TS2415: Type 'String' is not assignable to type 'string'.
23+
!!! error TS2415: 'string' is a primitive, but 'String' is a wrapper object. Prefer using 'string' when possible.
2224
x: U;
2325
}

tests/baselines/reference/arrayLiterals3.errors.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(34,5): error
1717
Types of parameters 'items' and 'items' are incompatible.
1818
Type 'Number' is not assignable to type 'string | number'.
1919
Type 'Number' is not assignable to type 'number'.
20+
'number' is a primitive, but 'Number' is a wrapper object. Prefer using 'number' when possible.
2021

2122

2223
==== tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts (6 errors) ====
@@ -79,4 +80,5 @@ tests/cases/conformance/expressions/arrayLiterals/arrayLiterals3.ts(34,5): error
7980
!!! error TS2322: Types of parameters 'items' and 'items' are incompatible.
8081
!!! error TS2322: Type 'Number' is not assignable to type 'string | number'.
8182
!!! error TS2322: Type 'Number' is not assignable to type 'number'.
83+
!!! error TS2322: 'number' is a primitive, but 'Number' is a wrapper object. Prefer using 'number' when possible.
8284

tests/baselines/reference/assignFromBooleanInterface.errors.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
tests/cases/conformance/types/primitives/boolean/assignFromBooleanInterface.ts(3,1): error TS2322: Type 'Boolean' is not assignable to type 'boolean'.
2+
'boolean' is a primitive, but 'Boolean' is a wrapper object. Prefer using 'boolean' when possible.
23

34

45
==== tests/cases/conformance/types/primitives/boolean/assignFromBooleanInterface.ts (1 errors) ====
@@ -7,4 +8,5 @@ tests/cases/conformance/types/primitives/boolean/assignFromBooleanInterface.ts(3
78
x = a;
89
~
910
!!! error TS2322: Type 'Boolean' is not assignable to type 'boolean'.
11+
!!! error TS2322: 'boolean' is a primitive, but 'Boolean' is a wrapper object. Prefer using 'boolean' when possible.
1012
a = x;

0 commit comments

Comments
 (0)