Skip to content

Commit 37e6e27

Browse files
authored
Do not resolve require calls in typescript files even if it contains dynamic import (#39617)
* Existing tests showing require in ts file is not used for module resolution * Do not resolve require calls in typescript files even if it contains dynamic import Fixes #38611
1 parent f2c5643 commit 37e6e27

11 files changed

+168
-1
lines changed

src/compiler/program.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2176,7 +2176,7 @@ namespace ts {
21762176
const r = /import|require/g;
21772177
while (r.exec(file.text) !== null) { // eslint-disable-line no-null/no-null
21782178
const node = getNodeAtPosition(file, r.lastIndex);
2179-
if (isRequireCall(node, /*checkArgumentIsStringLiteralLike*/ true)) {
2179+
if (isJavaScriptFile && isRequireCall(node, /*checkArgumentIsStringLiteralLike*/ true)) {
21802180
imports = append(imports, node.arguments[0]);
21812181
}
21822182
// we have to check the argument list has length of 1. We will still have to process these even though we have parsing error.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [tests/cases/compiler/moduleResolutionWithRequire.ts] ////
2+
3+
//// [other.ts]
4+
export const other = 123;
5+
6+
//// [index.ts]
7+
declare const require: any;
8+
function foo() {
9+
const a = require('../outside-of-rootdir/foo');
10+
const { other }: { other: string } = require('./other');
11+
}
12+
13+
14+
//// [index.js]
15+
function foo() {
16+
var a = require('../outside-of-rootdir/foo');
17+
var other = require('./other').other;
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== /index.ts ===
2+
declare const require: any;
3+
>require : Symbol(require, Decl(index.ts, 0, 13))
4+
5+
function foo() {
6+
>foo : Symbol(foo, Decl(index.ts, 0, 27))
7+
8+
const a = require('../outside-of-rootdir/foo');
9+
>a : Symbol(a, Decl(index.ts, 2, 9))
10+
>require : Symbol(require, Decl(index.ts, 0, 13))
11+
12+
const { other }: { other: string } = require('./other');
13+
>other : Symbol(other, Decl(index.ts, 3, 11))
14+
>other : Symbol(other, Decl(index.ts, 3, 22))
15+
>require : Symbol(require, Decl(index.ts, 0, 13))
16+
}
17+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=== /index.ts ===
2+
declare const require: any;
3+
>require : any
4+
5+
function foo() {
6+
>foo : () => void
7+
8+
const a = require('../outside-of-rootdir/foo');
9+
>a : any
10+
>require('../outside-of-rootdir/foo') : any
11+
>require : any
12+
>'../outside-of-rootdir/foo' : "../outside-of-rootdir/foo"
13+
14+
const { other }: { other: string } = require('./other');
15+
>other : string
16+
>other : string
17+
>require('./other') : any
18+
>require : any
19+
>'./other' : "./other"
20+
}
21+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//// [tests/cases/compiler/moduleResolutionWithRequireAndImport.ts] ////
2+
3+
//// [other.ts]
4+
export const other = 123;
5+
6+
//// [index.ts]
7+
declare const require: any;
8+
const a: typeof import('./other') = null as any
9+
function foo() {
10+
const a = require('../outside-of-rootdir/foo');
11+
const { other }: { other: string } = require('./other');
12+
}
13+
14+
15+
//// [other.js]
16+
"use strict";
17+
exports.__esModule = true;
18+
exports.other = void 0;
19+
exports.other = 123;
20+
//// [index.js]
21+
var a = null;
22+
function foo() {
23+
var a = require('../outside-of-rootdir/foo');
24+
var other = require('./other').other;
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== /index.ts ===
2+
declare const require: any;
3+
>require : Symbol(require, Decl(index.ts, 0, 13))
4+
5+
const a: typeof import('./other') = null as any
6+
>a : Symbol(a, Decl(index.ts, 1, 5))
7+
8+
function foo() {
9+
>foo : Symbol(foo, Decl(index.ts, 1, 47))
10+
11+
const a = require('../outside-of-rootdir/foo');
12+
>a : Symbol(a, Decl(index.ts, 3, 9))
13+
>require : Symbol(require, Decl(index.ts, 0, 13))
14+
15+
const { other }: { other: string } = require('./other');
16+
>other : Symbol(other, Decl(index.ts, 4, 11))
17+
>other : Symbol(other, Decl(index.ts, 4, 22))
18+
>require : Symbol(require, Decl(index.ts, 0, 13))
19+
}
20+
21+
=== /other.ts ===
22+
export const other = 123;
23+
>other : Symbol(other, Decl(other.ts, 0, 12))
24+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
"======== Resolving module './other' from '/index.ts'. ========",
3+
"Module resolution kind is not specified, using 'NodeJs'.",
4+
"Loading module as file / folder, candidate module location '/other', target file type 'TypeScript'.",
5+
"File '/other.ts' exist - use it as a name resolution result.",
6+
"======== Module name './other' was successfully resolved to '/other.ts'. ========"
7+
]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
=== /index.ts ===
2+
declare const require: any;
3+
>require : any
4+
5+
const a: typeof import('./other') = null as any
6+
>a : typeof import("/other")
7+
>null as any : any
8+
>null : null
9+
10+
function foo() {
11+
>foo : () => void
12+
13+
const a = require('../outside-of-rootdir/foo');
14+
>a : any
15+
>require('../outside-of-rootdir/foo') : any
16+
>require : any
17+
>'../outside-of-rootdir/foo' : "../outside-of-rootdir/foo"
18+
19+
const { other }: { other: string } = require('./other');
20+
>other : string
21+
>other : string
22+
>require('./other') : any
23+
>require : any
24+
>'./other' : "./other"
25+
}
26+
27+
=== /other.ts ===
28+
export const other = 123;
29+
>other : 123
30+
>123 : 123
31+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @traceResolution: true
2+
3+
// @filename: /other.ts
4+
export const other = 123;
5+
6+
// @filename: /index.ts
7+
declare const require: any;
8+
function foo() {
9+
const a = require('../outside-of-rootdir/foo');
10+
const { other }: { other: string } = require('./other');
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// @traceResolution: true
2+
3+
// @filename: /other.ts
4+
export const other = 123;
5+
6+
// @filename: /index.ts
7+
declare const require: any;
8+
const a: typeof import('./other') = null as any
9+
function foo() {
10+
const a = require('../outside-of-rootdir/foo');
11+
const { other }: { other: string } = require('./other');
12+
}

0 commit comments

Comments
 (0)