diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 0bfbd381792e5..3d8cc2fe91922 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -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. diff --git a/tests/baselines/reference/moduleResolutionWithRequire.js b/tests/baselines/reference/moduleResolutionWithRequire.js new file mode 100644 index 0000000000000..bd822050d69b8 --- /dev/null +++ b/tests/baselines/reference/moduleResolutionWithRequire.js @@ -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; +} diff --git a/tests/baselines/reference/moduleResolutionWithRequire.symbols b/tests/baselines/reference/moduleResolutionWithRequire.symbols new file mode 100644 index 0000000000000..31f11dae4d69c --- /dev/null +++ b/tests/baselines/reference/moduleResolutionWithRequire.symbols @@ -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)) +} + diff --git a/tests/baselines/reference/moduleResolutionWithRequire.trace.json b/tests/baselines/reference/moduleResolutionWithRequire.trace.json new file mode 100644 index 0000000000000..0637a088a01e8 --- /dev/null +++ b/tests/baselines/reference/moduleResolutionWithRequire.trace.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/tests/baselines/reference/moduleResolutionWithRequire.types b/tests/baselines/reference/moduleResolutionWithRequire.types new file mode 100644 index 0000000000000..3af66c18d8f7e --- /dev/null +++ b/tests/baselines/reference/moduleResolutionWithRequire.types @@ -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" +} + diff --git a/tests/baselines/reference/moduleResolutionWithRequireAndImport.js b/tests/baselines/reference/moduleResolutionWithRequireAndImport.js new file mode 100644 index 0000000000000..2eb66f48210ea --- /dev/null +++ b/tests/baselines/reference/moduleResolutionWithRequireAndImport.js @@ -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; +} diff --git a/tests/baselines/reference/moduleResolutionWithRequireAndImport.symbols b/tests/baselines/reference/moduleResolutionWithRequireAndImport.symbols new file mode 100644 index 0000000000000..738b6c5d844ac --- /dev/null +++ b/tests/baselines/reference/moduleResolutionWithRequireAndImport.symbols @@ -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)) + diff --git a/tests/baselines/reference/moduleResolutionWithRequireAndImport.trace.json b/tests/baselines/reference/moduleResolutionWithRequireAndImport.trace.json new file mode 100644 index 0000000000000..1b54938304529 --- /dev/null +++ b/tests/baselines/reference/moduleResolutionWithRequireAndImport.trace.json @@ -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'. ========" +] \ No newline at end of file diff --git a/tests/baselines/reference/moduleResolutionWithRequireAndImport.types b/tests/baselines/reference/moduleResolutionWithRequireAndImport.types new file mode 100644 index 0000000000000..b2a390b15635d --- /dev/null +++ b/tests/baselines/reference/moduleResolutionWithRequireAndImport.types @@ -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 + diff --git a/tests/cases/compiler/moduleResolutionWithRequire.ts b/tests/cases/compiler/moduleResolutionWithRequire.ts new file mode 100644 index 0000000000000..1ff915cbf6dd8 --- /dev/null +++ b/tests/cases/compiler/moduleResolutionWithRequire.ts @@ -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'); +} diff --git a/tests/cases/compiler/moduleResolutionWithRequireAndImport.ts b/tests/cases/compiler/moduleResolutionWithRequireAndImport.ts new file mode 100644 index 0000000000000..8225f69fe9f2e --- /dev/null +++ b/tests/cases/compiler/moduleResolutionWithRequireAndImport.ts @@ -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'); +}