Skip to content

Do not resolve require calls in typescript files even if it contains dynamic import #39617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2176,7 +2176,7 @@ namespace ts {
const r = /import|require/g;
while (r.exec(file.text) !== null) { // eslint-disable-line no-null/no-null
const node = getNodeAtPosition(file, r.lastIndex);
if (isRequireCall(node, /*checkArgumentIsStringLiteralLike*/ true)) {
if (isJavaScriptFile && isRequireCall(node, /*checkArgumentIsStringLiteralLike*/ true)) {
imports = append(imports, node.arguments[0]);
}
// we have to check the argument list has length of 1. We will still have to process these even though we have parsing error.
Expand Down
18 changes: 18 additions & 0 deletions tests/baselines/reference/moduleResolutionWithRequire.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//// [tests/cases/compiler/moduleResolutionWithRequire.ts] ////

//// [other.ts]
export const other = 123;

//// [index.ts]
declare const require: any;
function foo() {
const a = require('../outside-of-rootdir/foo');
const { other }: { other: string } = require('./other');
}


//// [index.js]
function foo() {
var a = require('../outside-of-rootdir/foo');
var other = require('./other').other;
}
17 changes: 17 additions & 0 deletions tests/baselines/reference/moduleResolutionWithRequire.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
=== /index.ts ===
declare const require: any;
>require : Symbol(require, Decl(index.ts, 0, 13))

function foo() {
>foo : Symbol(foo, Decl(index.ts, 0, 27))

const a = require('../outside-of-rootdir/foo');
>a : Symbol(a, Decl(index.ts, 2, 9))
>require : Symbol(require, Decl(index.ts, 0, 13))

const { other }: { other: string } = require('./other');
>other : Symbol(other, Decl(index.ts, 3, 11))
>other : Symbol(other, Decl(index.ts, 3, 22))
>require : Symbol(require, Decl(index.ts, 0, 13))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
21 changes: 21 additions & 0 deletions tests/baselines/reference/moduleResolutionWithRequire.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
=== /index.ts ===
declare const require: any;
>require : any

function foo() {
>foo : () => void

const a = require('../outside-of-rootdir/foo');
>a : any
>require('../outside-of-rootdir/foo') : any
>require : any
>'../outside-of-rootdir/foo' : "../outside-of-rootdir/foo"

const { other }: { other: string } = require('./other');
>other : string
>other : string
>require('./other') : any
>require : any
>'./other' : "./other"
}

25 changes: 25 additions & 0 deletions tests/baselines/reference/moduleResolutionWithRequireAndImport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//// [tests/cases/compiler/moduleResolutionWithRequireAndImport.ts] ////

//// [other.ts]
export const other = 123;

//// [index.ts]
declare const require: any;
const a: typeof import('./other') = null as any
function foo() {
const a = require('../outside-of-rootdir/foo');
const { other }: { other: string } = require('./other');
}


//// [other.js]
"use strict";
exports.__esModule = true;
exports.other = void 0;
exports.other = 123;
//// [index.js]
var a = null;
function foo() {
var a = require('../outside-of-rootdir/foo');
var other = require('./other').other;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
=== /index.ts ===
declare const require: any;
>require : Symbol(require, Decl(index.ts, 0, 13))

const a: typeof import('./other') = null as any
>a : Symbol(a, Decl(index.ts, 1, 5))

function foo() {
>foo : Symbol(foo, Decl(index.ts, 1, 47))

const a = require('../outside-of-rootdir/foo');
>a : Symbol(a, Decl(index.ts, 3, 9))
>require : Symbol(require, Decl(index.ts, 0, 13))

const { other }: { other: string } = require('./other');
>other : Symbol(other, Decl(index.ts, 4, 11))
>other : Symbol(other, Decl(index.ts, 4, 22))
>require : Symbol(require, Decl(index.ts, 0, 13))
}

=== /other.ts ===
export const other = 123;
>other : Symbol(other, Decl(other.ts, 0, 12))

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
"======== Resolving module './other' from '/index.ts'. ========",
"Module resolution kind is not specified, using 'NodeJs'.",
"Loading module as file / folder, candidate module location '/other', target file type 'TypeScript'.",
"File '/other.ts' exist - use it as a name resolution result.",
"======== Module name './other' was successfully resolved to '/other.ts'. ========"
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
=== /index.ts ===
declare const require: any;
>require : any

const a: typeof import('./other') = null as any
>a : typeof import("/other")
>null as any : any
>null : null

function foo() {
>foo : () => void

const a = require('../outside-of-rootdir/foo');
>a : any
>require('../outside-of-rootdir/foo') : any
>require : any
>'../outside-of-rootdir/foo' : "../outside-of-rootdir/foo"

const { other }: { other: string } = require('./other');
>other : string
>other : string
>require('./other') : any
>require : any
>'./other' : "./other"
}

=== /other.ts ===
export const other = 123;
>other : 123
>123 : 123

11 changes: 11 additions & 0 deletions tests/cases/compiler/moduleResolutionWithRequire.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @traceResolution: true

// @filename: /other.ts
export const other = 123;

// @filename: /index.ts
declare const require: any;
function foo() {
const a = require('../outside-of-rootdir/foo');
const { other }: { other: string } = require('./other');
}
12 changes: 12 additions & 0 deletions tests/cases/compiler/moduleResolutionWithRequireAndImport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @traceResolution: true

// @filename: /other.ts
export const other = 123;

// @filename: /index.ts
declare const require: any;
const a: typeof import('./other') = null as any
function foo() {
const a = require('../outside-of-rootdir/foo');
const { other }: { other: string } = require('./other');
}