Skip to content

Add disableImportOptimizations compiler option #30766

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

Closed
wants to merge 3 commits into from
Closed
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
6 changes: 6 additions & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,12 @@ namespace ts {
category: Diagnostics.Module_Resolution_Options,
description: Diagnostics.Do_not_resolve_the_real_path_of_symlinks,
},
{
name: "disableImportOptimizations",
type: "boolean",
category: Diagnostics.Module_Resolution_Options,
description: Diagnostics.Do_not_optimize_imports,
},

// Source Maps
{
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4072,6 +4072,10 @@
"category": "Message",
"code": 6381
},
"Do not optimize imports": {
"category": "Message",
"code": 6382
},

"The expected type comes from property '{0}' which is declared here on type '{1}'": {
"category": "Message",
Expand Down
20 changes: 20 additions & 0 deletions src/compiler/transformers/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3049,6 +3049,10 @@ namespace ts {
* @param node The import declaration node.
*/
function visitImportDeclaration(node: ImportDeclaration): VisitResult<Statement> {
if (compilerOptions.disableImportOptimizations) {
return node;
}

if (!node.importClause) {
// Do not elide a side-effect only import declaration.
// import "foo";
Expand All @@ -3073,6 +3077,10 @@ namespace ts {
* @param node The import clause node.
*/
function visitImportClause(node: ImportClause): VisitResult<ImportClause> {
if (compilerOptions.disableImportOptimizations) {
return node;
}

// Elide the import clause if we elide both its name and its named bindings.
const name = resolver.isReferencedAliasDeclaration(node) ? node.name : undefined;
const namedBindings = visitNode(node.namedBindings, visitNamedImportBindings, isNamedImportBindings);
Expand All @@ -3085,6 +3093,10 @@ namespace ts {
* @param node The named import bindings node.
*/
function visitNamedImportBindings(node: NamedImportBindings): VisitResult<NamedImportBindings> {
if (compilerOptions.disableImportOptimizations) {
return node;
}

if (node.kind === SyntaxKind.NamespaceImport) {
// Elide a namespace import if it is not referenced.
return resolver.isReferencedAliasDeclaration(node) ? node : undefined;
Expand All @@ -3102,6 +3114,10 @@ namespace ts {
* @param node The import specifier node.
*/
function visitImportSpecifier(node: ImportSpecifier): VisitResult<ImportSpecifier> {
if (compilerOptions.disableImportOptimizations) {
return node;
}

// Elide an import specifier if it is not referenced.
return resolver.isReferencedAliasDeclaration(node) ? node : undefined;
}
Expand All @@ -3113,6 +3129,10 @@ namespace ts {
* @param node The export assignment node.
*/
function visitExportAssignment(node: ExportAssignment): VisitResult<Statement> {
if (compilerOptions.disableImportOptimizations) {
return node;
}

// Elide the export assignment if it does not reference a value.
return resolver.isValueAliasDeclaration(node)
? visitEachChild(node, visitor, context)
Expand Down
42 changes: 42 additions & 0 deletions tests/baselines/reference/disableImportOptimizations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//// [tests/cases/compiler/disableImportOptimizations.ts] ////

//// [disableImportOptimizations1.ts]
export class ClassWithSideEffect {
static someValue = ClassWithSideEffect.initValue();

static initValue(): string {
console.log("We have a side-effect here!");
return "something";
}
}

//// [disableImportOptimizations2.ts]
import { ClassWithSideEffect } from "./disableImportOptimizations1"; //Import gets removed without disableImportOptimizations

export class OtherClass {
}

//// [disableImportOptimizations1.js]
"use strict";
exports.__esModule = true;
var ClassWithSideEffect = /** @class */ (function () {
function ClassWithSideEffect() {
}
ClassWithSideEffect.initValue = function () {
console.log("We have a side-effect here!");
return "something";
};
ClassWithSideEffect.someValue = ClassWithSideEffect.initValue();
return ClassWithSideEffect;
}());
exports.ClassWithSideEffect = ClassWithSideEffect;
//// [disableImportOptimizations2.js]
"use strict";
exports.__esModule = true;
var disableImportOptimizations1_1 = require("./disableImportOptimizations1"); //Import gets removed without disableImportOptimizations
var OtherClass = /** @class */ (function () {
function OtherClass() {
}
return OtherClass;
}());
exports.OtherClass = OtherClass;
29 changes: 29 additions & 0 deletions tests/baselines/reference/disableImportOptimizations.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
=== tests/cases/compiler/disableImportOptimizations1.ts ===
export class ClassWithSideEffect {
>ClassWithSideEffect : Symbol(ClassWithSideEffect, Decl(disableImportOptimizations1.ts, 0, 0))

static someValue = ClassWithSideEffect.initValue();
>someValue : Symbol(ClassWithSideEffect.someValue, Decl(disableImportOptimizations1.ts, 0, 34))
>ClassWithSideEffect.initValue : Symbol(ClassWithSideEffect.initValue, Decl(disableImportOptimizations1.ts, 1, 53))
>ClassWithSideEffect : Symbol(ClassWithSideEffect, Decl(disableImportOptimizations1.ts, 0, 0))
>initValue : Symbol(ClassWithSideEffect.initValue, Decl(disableImportOptimizations1.ts, 1, 53))

static initValue(): string {
>initValue : Symbol(ClassWithSideEffect.initValue, Decl(disableImportOptimizations1.ts, 1, 53))

console.log("We have a side-effect here!");
>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, --, --))

return "something";
}
}

=== tests/cases/compiler/disableImportOptimizations2.ts ===
import { ClassWithSideEffect } from "./disableImportOptimizations1"; //Import gets removed without disableImportOptimizations
>ClassWithSideEffect : Symbol(ClassWithSideEffect, Decl(disableImportOptimizations2.ts, 0, 8))

export class OtherClass {
>OtherClass : Symbol(OtherClass, Decl(disableImportOptimizations2.ts, 0, 68))
}
33 changes: 33 additions & 0 deletions tests/baselines/reference/disableImportOptimizations.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
=== tests/cases/compiler/disableImportOptimizations1.ts ===
export class ClassWithSideEffect {
>ClassWithSideEffect : ClassWithSideEffect

static someValue = ClassWithSideEffect.initValue();
>someValue : string
>ClassWithSideEffect.initValue() : string
>ClassWithSideEffect.initValue : () => string
>ClassWithSideEffect : typeof ClassWithSideEffect
>initValue : () => string

static initValue(): string {
>initValue : () => string

console.log("We have a side-effect here!");
>console.log("We have a side-effect here!") : void
>console.log : (message?: any, ...optionalParams: any[]) => void
>console : Console
>log : (message?: any, ...optionalParams: any[]) => void
>"We have a side-effect here!" : "We have a side-effect here!"

return "something";
>"something" : "something"
}
}

=== tests/cases/compiler/disableImportOptimizations2.ts ===
import { ClassWithSideEffect } from "./disableImportOptimizations1"; //Import gets removed without disableImportOptimizations
>ClassWithSideEffect : typeof ClassWithSideEffect

export class OtherClass {
>OtherClass : OtherClass
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"disableImportOptimizations": true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "disableImportOptimizations": true, /* Do not optimize imports */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "disableImportOptimizations": true, /* Do not optimize imports */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "disableImportOptimizations": true, /* Do not optimize imports */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "disableImportOptimizations": true, /* Do not optimize imports */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "disableImportOptimizations": true, /* Do not optimize imports */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "disableImportOptimizations": true, /* Do not optimize imports */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "disableImportOptimizations": true, /* Do not optimize imports */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "disableImportOptimizations": true, /* Do not optimize imports */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "disableImportOptimizations": true, /* Do not optimize imports */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
Expand Down
17 changes: 17 additions & 0 deletions tests/cases/compiler/disableImportOptimizations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// @disableImportOptimizations: true

// @fileName: disableImportOptimizations1.ts
export class ClassWithSideEffect {
static someValue = ClassWithSideEffect.initValue();

static initValue(): string {
console.log("We have a side-effect here!");
return "something";
}
}

// @fileName: disableImportOptimizations2.ts
import { ClassWithSideEffect } from "./disableImportOptimizations1"; //Import gets removed without disableImportOptimizations

export class OtherClass {
}