-
Notifications
You must be signed in to change notification settings - Fork 12.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't elide imports when transforming JS files (#50404)
* don't elide imports in JS files * WIP: get rid of caching of resolved symbol, add transform tests * get rid of caching only for resolver functions * use getReferencedSymbol instead of getReferencedValueSymbol in module transform * WIP: add reportErrors flag to resolveName * Import transformations now work correctly * don't emit diagnostics when looking up referenced symbol * small fixes and get rid of unnecessary comments * update tests * clean up * CR: use nameNotFoundMessage to decide whether to report errors in resolveName
- Loading branch information
Showing
23 changed files
with
783 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
tests/cases/compiler/caller.js(1,21): error TS2307: Cannot find module 'fs' or its corresponding type declarations. | ||
tests/cases/compiler/caller.js(2,8): error TS18042: 'TruffleContract' is a type and cannot be imported in JavaScript files. Use 'import("@truffle/contract").TruffleContract' in a JSDoc type annotation. | ||
tests/cases/compiler/caller.js(4,43): error TS2708: Cannot use namespace 'TruffleContract' as a value. | ||
tests/cases/compiler/caller.js(4,60): error TS2708: Cannot use namespace 'TruffleContract' as a value. | ||
|
||
|
||
==== tests/cases/compiler/caller.js (4 errors) ==== | ||
import * as fs from 'fs'; | ||
~~~~ | ||
!!! error TS2307: Cannot find module 'fs' or its corresponding type declarations. | ||
import TruffleContract from '@truffle/contract'; // Runtime err: this import is elided in transform | ||
~~~~~~~~~~~~~~~ | ||
!!! error TS18042: 'TruffleContract' is a type and cannot be imported in JavaScript files. Use 'import("@truffle/contract").TruffleContract' in a JSDoc type annotation. | ||
console.log(fs); | ||
console.log('TruffleContract is ', typeof TruffleContract, TruffleContract); // `TruffleContract` is considered 'unused' | ||
~~~~~~~~~~~~~~~ | ||
!!! error TS2708: Cannot use namespace 'TruffleContract' as a value. | ||
~~~~~~~~~~~~~~~ | ||
!!! error TS2708: Cannot use namespace 'TruffleContract' as a value. | ||
|
||
|
||
==== tests/cases/compiler/node_modules/@truffle/contract/index.d.ts (0 errors) ==== | ||
declare module "@truffle/contract" { | ||
interface ContractObject { | ||
foo: number; | ||
} | ||
namespace TruffleContract { | ||
export type Contract = ContractObject; | ||
} | ||
export default TruffleContract; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//// [tests/cases/compiler/elidedJSImport1.ts] //// | ||
|
||
//// [caller.js] | ||
import * as fs from 'fs'; | ||
import TruffleContract from '@truffle/contract'; // Runtime err: this import is elided in transform | ||
console.log(fs); | ||
console.log('TruffleContract is ', typeof TruffleContract, TruffleContract); // `TruffleContract` is considered 'unused' | ||
|
||
|
||
//// [index.d.ts] | ||
declare module "@truffle/contract" { | ||
interface ContractObject { | ||
foo: number; | ||
} | ||
namespace TruffleContract { | ||
export type Contract = ContractObject; | ||
} | ||
export default TruffleContract; | ||
} | ||
|
||
//// [caller.js] | ||
import * as fs from 'fs'; | ||
import TruffleContract from '@truffle/contract'; // Runtime err: this import is elided in transform | ||
console.log(fs); | ||
console.log('TruffleContract is ', typeof TruffleContract, TruffleContract); // `TruffleContract` is considered 'unused' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
=== tests/cases/compiler/caller.js === | ||
import * as fs from 'fs'; | ||
>fs : Symbol(fs, Decl(caller.js, 0, 6)) | ||
|
||
import TruffleContract from '@truffle/contract'; // Runtime err: this import is elided in transform | ||
>TruffleContract : Symbol(TruffleContract, Decl(caller.js, 1, 6)) | ||
|
||
console.log(fs); | ||
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) | ||
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>fs : Symbol(fs, Decl(caller.js, 0, 6)) | ||
|
||
console.log('TruffleContract is ', typeof TruffleContract, TruffleContract); // `TruffleContract` is considered 'unused' | ||
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) | ||
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
|
||
|
||
=== tests/cases/compiler/node_modules/@truffle/contract/index.d.ts === | ||
declare module "@truffle/contract" { | ||
>"@truffle/contract" : Symbol("@truffle/contract", Decl(index.d.ts, 0, 0)) | ||
|
||
interface ContractObject { | ||
>ContractObject : Symbol(ContractObject, Decl(index.d.ts, 0, 36)) | ||
|
||
foo: number; | ||
>foo : Symbol(ContractObject.foo, Decl(index.d.ts, 1, 30)) | ||
} | ||
namespace TruffleContract { | ||
>TruffleContract : Symbol(TruffleContract, Decl(index.d.ts, 3, 5)) | ||
|
||
export type Contract = ContractObject; | ||
>Contract : Symbol(Contract, Decl(index.d.ts, 4, 31)) | ||
>ContractObject : Symbol(ContractObject, Decl(index.d.ts, 0, 36)) | ||
} | ||
export default TruffleContract; | ||
>TruffleContract : Symbol(TruffleContract, Decl(index.d.ts, 3, 5)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
=== tests/cases/compiler/caller.js === | ||
import * as fs from 'fs'; | ||
>fs : any | ||
|
||
import TruffleContract from '@truffle/contract'; // Runtime err: this import is elided in transform | ||
>TruffleContract : any | ||
|
||
console.log(fs); | ||
>console.log(fs) : void | ||
>console.log : (...data: any[]) => void | ||
>console : Console | ||
>log : (...data: any[]) => void | ||
>fs : any | ||
|
||
console.log('TruffleContract is ', typeof TruffleContract, TruffleContract); // `TruffleContract` is considered 'unused' | ||
>console.log('TruffleContract is ', typeof TruffleContract, TruffleContract) : void | ||
>console.log : (...data: any[]) => void | ||
>console : Console | ||
>log : (...data: any[]) => void | ||
>'TruffleContract is ' : "TruffleContract is " | ||
>typeof TruffleContract : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | ||
>TruffleContract : any | ||
>TruffleContract : any | ||
|
||
|
||
=== tests/cases/compiler/node_modules/@truffle/contract/index.d.ts === | ||
declare module "@truffle/contract" { | ||
>"@truffle/contract" : typeof import("@truffle/contract") | ||
|
||
interface ContractObject { | ||
foo: number; | ||
>foo : number | ||
} | ||
namespace TruffleContract { | ||
export type Contract = ContractObject; | ||
>Contract : ContractObject | ||
} | ||
export default TruffleContract; | ||
>TruffleContract : any | ||
} |
83 changes: 83 additions & 0 deletions
83
tests/baselines/reference/elidedJSImport2(module=commonjs).js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
//// [tests/cases/compiler/elidedJSImport2.ts] //// | ||
|
||
//// [index.js] | ||
import { Foo } from "./other.js"; | ||
import * as other from "./other.js"; | ||
import defaultFoo from "./other.js"; | ||
|
||
const x = new Foo(); | ||
const y = other.Foo(); | ||
const z = new defaultFoo(); | ||
|
||
//// [other.d.ts] | ||
export interface Foo { | ||
bar: number; | ||
} | ||
|
||
export default interface Bar { | ||
foo: number; | ||
} | ||
|
||
//// [other.js] | ||
export class Foo { | ||
bar = 2.4; | ||
} | ||
|
||
export default class Bar { | ||
foo = 1.2; | ||
} | ||
|
||
|
||
//// [index.js] | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
exports.__esModule = true; | ||
var other_js_1 = require("./other.js"); | ||
var other = __importStar(require("./other.js")); | ||
var other_js_2 = __importDefault(require("./other.js")); | ||
var x = new other_js_1.Foo(); | ||
var y = other.Foo(); | ||
var z = new other_js_2["default"](); | ||
//// [other.js] | ||
"use strict"; | ||
exports.__esModule = true; | ||
exports.Foo = void 0; | ||
var Foo = /** @class */ (function () { | ||
function Foo() { | ||
this.bar = 2.4; | ||
} | ||
return Foo; | ||
}()); | ||
exports.Foo = Foo; | ||
var Bar = /** @class */ (function () { | ||
function Bar() { | ||
this.foo = 1.2; | ||
} | ||
return Bar; | ||
}()); | ||
exports["default"] = Bar; |
50 changes: 50 additions & 0 deletions
50
tests/baselines/reference/elidedJSImport2(module=commonjs).symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
=== tests/cases/compiler/index.js === | ||
import { Foo } from "./other.js"; | ||
>Foo : Symbol(Foo, Decl(index.js, 0, 8)) | ||
|
||
import * as other from "./other.js"; | ||
>other : Symbol(other, Decl(index.js, 1, 6)) | ||
|
||
import defaultFoo from "./other.js"; | ||
>defaultFoo : Symbol(defaultFoo, Decl(index.js, 2, 6)) | ||
|
||
const x = new Foo(); | ||
>x : Symbol(x, Decl(index.js, 4, 5)) | ||
|
||
const y = other.Foo(); | ||
>y : Symbol(y, Decl(index.js, 5, 5)) | ||
>other : Symbol(other, Decl(index.js, 1, 6)) | ||
|
||
const z = new defaultFoo(); | ||
>z : Symbol(z, Decl(index.js, 6, 5)) | ||
|
||
=== tests/cases/compiler/other.d.ts === | ||
export interface Foo { | ||
>Foo : Symbol(Foo, Decl(other.d.ts, 0, 0)) | ||
|
||
bar: number; | ||
>bar : Symbol(Foo.bar, Decl(other.d.ts, 0, 22)) | ||
} | ||
|
||
export default interface Bar { | ||
>Bar : Symbol(Bar, Decl(other.d.ts, 2, 1)) | ||
|
||
foo: number; | ||
>foo : Symbol(Bar.foo, Decl(other.d.ts, 4, 30)) | ||
} | ||
|
||
=== tests/cases/compiler/other.js === | ||
export class Foo { | ||
>Foo : Symbol(Foo, Decl(other.js, 0, 0)) | ||
|
||
bar = 2.4; | ||
>bar : Symbol(Foo.bar, Decl(other.js, 0, 18)) | ||
} | ||
|
||
export default class Bar { | ||
>Bar : Symbol(Bar, Decl(other.js, 2, 1)) | ||
|
||
foo = 1.2; | ||
>foo : Symbol(Bar.foo, Decl(other.js, 4, 26)) | ||
} | ||
|
Oops, something went wrong.