Dont load JavaScript if types packages are present

This commit is contained in:
Bill Ticehurst 2016-06-29 17:04:42 -07:00
parent 5f8cf1af3e
commit 5a45c44eb7
12 changed files with 71 additions and 17 deletions

View file

@ -779,13 +779,18 @@ namespace ts {
while (true) {
const baseName = getBaseFileName(directory);
if (baseName !== "node_modules") {
const result =
// first: try to load module as-is
loadModuleFromNodeModulesFolder(moduleName, directory, failedLookupLocations, state) ||
// second: try to load module from the scope '@types'
loadModuleFromNodeModulesFolder(combinePaths("@types", moduleName), directory, failedLookupLocations, state);
if (result) {
return result;
// Try to load source from the package
const packageResult = loadModuleFromNodeModulesFolder(moduleName, directory, failedLookupLocations, state);
if (packageResult && hasTypeScriptFileExtension(packageResult)) {
// Always prefer a TypeScript (.ts, .tsx, .d.ts) file shipped with the package
return packageResult;
}
else {
// Else prefer a types package over non-TypeScript results (e.g. JavaScript files)
const typesResult = loadModuleFromNodeModulesFolder(combinePaths("@types", moduleName), directory, failedLookupLocations, state);
if (typesResult || packageResult) {
return typesResult || packageResult;
}
}
}

View file

@ -1,6 +1,8 @@
define(["require", "exports", "m1"], function (require, exports, m1) {
define(["require", "exports", "m1", "m4"], function (require, exports, m1, m4) {
"use strict";
m1.f1("test");
m1.f2.a = 10;
m1.f2.person.age = "10"; // Error: Should be number
m1.f2.person.age = "10"; // Should error if loaded the .js files correctly
var r1 = m4.test.charAt(2); // Should error if correctly not using the .js file but using @types info
var r2 = 3 + m4.foo; // Should be OK if correctly using the @types .d.ts file
});

View file

@ -1,4 +1,5 @@
maxDepthIncreased/root.ts(4,1): error TS2322: Type 'string' is not assignable to type 'number'.
maxDepthIncreased/root.ts(7,1): error TS2322: Type 'string' is not assignable to type 'number'.
maxDepthIncreased/root.ts(8,13): error TS2339: Property 'test' does not exist on type 'typeof "C:/src/TypeScript/tests/cases/projects/NodeModulesSearch/maxDepthIncreased/node_modules/@...'.
==== index.js (0 errors) ====
@ -28,11 +29,22 @@ maxDepthIncreased/root.ts(4,1): error TS2322: Type 'string' is not assignable to
exports.f2 = m2;
==== maxDepthIncreased/root.ts (1 errors) ====
==== entry.d.ts (0 errors) ====
export declare var foo: number;
==== maxDepthIncreased/root.ts (2 errors) ====
import * as m1 from "m1";
import * as m4 from "m4";
m1.f1("test");
m1.f2.a = 10;
m1.f2.person.age = "10"; // Error: Should be number
m1.f2.person.age = "10"; // Should error if loaded the .js files correctly
~~~~~~~~~~~~~~~~
!!! error TS2322: Type 'string' is not assignable to type 'number'.
let r1 = m4.test.charAt(2); // Should error if correctly not using the .js file but using @types info
~~~~
!!! error TS2339: Property 'test' does not exist on type 'typeof "C:/src/TypeScript/tests/cases/projects/NodeModulesSearch/maxDepthIncreased/node_modules/@...'.
let r2 = 3 + m4.foo; // Should be OK if correctly using the @types .d.ts file

View file

@ -10,6 +10,7 @@
"maxDepthIncreased/node_modules/m2/node_modules/m3/index.js",
"maxDepthIncreased/node_modules/m2/entry.js",
"maxDepthIncreased/node_modules/m1/index.js",
"maxDepthIncreased/node_modules/@types/m4/entry.d.ts",
"maxDepthIncreased/root.ts"
],
"emittedFiles": [

View file

@ -1,5 +1,8 @@
"use strict";
var m1 = require("m1");
var m4 = require("m4");
m1.f1("test");
m1.f2.a = 10;
m1.f2.person.age = "10"; // Error: Should be number
m1.f2.person.age = "10"; // Should error if loaded the .js files correctly
var r1 = m4.test.charAt(2); // Should error if correctly not using the .js file but using @types info
var r2 = 3 + m4.foo; // Should be OK if correctly using the @types .d.ts file

View file

@ -1,4 +1,5 @@
maxDepthIncreased/root.ts(4,1): error TS2322: Type 'string' is not assignable to type 'number'.
maxDepthIncreased/root.ts(7,1): error TS2322: Type 'string' is not assignable to type 'number'.
maxDepthIncreased/root.ts(8,13): error TS2339: Property 'test' does not exist on type 'typeof "C:/src/TypeScript/tests/cases/projects/NodeModulesSearch/maxDepthIncreased/node_modules/@...'.
==== index.js (0 errors) ====
@ -28,11 +29,22 @@ maxDepthIncreased/root.ts(4,1): error TS2322: Type 'string' is not assignable to
exports.f2 = m2;
==== maxDepthIncreased/root.ts (1 errors) ====
==== entry.d.ts (0 errors) ====
export declare var foo: number;
==== maxDepthIncreased/root.ts (2 errors) ====
import * as m1 from "m1";
import * as m4 from "m4";
m1.f1("test");
m1.f2.a = 10;
m1.f2.person.age = "10"; // Error: Should be number
m1.f2.person.age = "10"; // Should error if loaded the .js files correctly
~~~~~~~~~~~~~~~~
!!! error TS2322: Type 'string' is not assignable to type 'number'.
let r1 = m4.test.charAt(2); // Should error if correctly not using the .js file but using @types info
~~~~
!!! error TS2339: Property 'test' does not exist on type 'typeof "C:/src/TypeScript/tests/cases/projects/NodeModulesSearch/maxDepthIncreased/node_modules/@...'.
let r2 = 3 + m4.foo; // Should be OK if correctly using the @types .d.ts file

View file

@ -10,6 +10,7 @@
"maxDepthIncreased/node_modules/m2/node_modules/m3/index.js",
"maxDepthIncreased/node_modules/m2/entry.js",
"maxDepthIncreased/node_modules/m1/index.js",
"maxDepthIncreased/node_modules/@types/m4/entry.d.ts",
"maxDepthIncreased/root.ts"
],
"emittedFiles": [

View file

@ -0,0 +1 @@
export declare var foo: number;

View file

@ -0,0 +1,5 @@
{
"types": "entry.d.ts",
"name": "m4",
"version": "1.0.0"
}

View file

@ -0,0 +1 @@
exports.test = "hello, world";

View file

@ -0,0 +1,5 @@
{
"name": "m4",
"version": "1.0.0",
"main": "entry.js"
}

View file

@ -1,4 +1,10 @@
import * as m1 from "m1";
import * as m4 from "m4";
m1.f1("test");
m1.f2.a = 10;
m1.f2.person.age = "10"; // Error: Should be number
m1.f2.person.age = "10"; // Should error if loaded the .js files correctly
let r1 = m4.test.charAt(2); // Should error if correctly not using the .js file but using @types info
let r2 = 3 + m4.foo; // Should be OK if correctly using the @types .d.ts file