Skip to content

Commit

Permalink
Dont load JavaScript if types packages are present
Browse files Browse the repository at this point in the history
  • Loading branch information
billti committed Jun 30, 2016
1 parent 5f8cf1a commit 5a45c44
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 17 deletions.
19 changes: 12 additions & 7 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
});
Original file line number Diff line number Diff line change
@@ -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) ====
Expand Down Expand Up @@ -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

Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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) ====
Expand Down Expand Up @@ -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

Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5a45c44

Please sign in to comment.