Skip to content

Add fallback error locations for nameless declarations, better class error locations #42585

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
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
15 changes: 11 additions & 4 deletions src/compiler/transformers/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ namespace ts {
reportNonlocalAugmentation
};
let errorNameNode: DeclarationName | undefined;
let errorFallbackNode: Declaration | undefined;

let currentSourceFile: SourceFile;
let refs: ESMap<NodeId, SourceFile>;
Expand Down Expand Up @@ -161,9 +162,9 @@ namespace ts {
}

function reportPrivateInBaseOfClassExpression(propertyName: string) {
if (errorNameNode) {
if (errorNameNode || errorFallbackNode) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd just capture these into a local so you can avoid the !

context.addDiagnostic(
createDiagnosticForNode(errorNameNode, Diagnostics.Property_0_of_exported_class_expression_may_not_be_private_or_protected, propertyName));
createDiagnosticForNode((errorNameNode || errorFallbackNode)!, Diagnostics.Property_0_of_exported_class_expression_may_not_be_private_or_protected, propertyName));
}
}

Expand Down Expand Up @@ -199,8 +200,8 @@ namespace ts {
}

function reportTruncationError() {
if (errorNameNode) {
context.addDiagnostic(createDiagnosticForNode(errorNameNode, Diagnostics.The_inferred_type_of_this_node_exceeds_the_maximum_length_the_compiler_will_serialize_An_explicit_type_annotation_is_needed));
if (errorNameNode || errorFallbackNode) {
context.addDiagnostic(createDiagnosticForNode((errorNameNode || errorFallbackNode)!, Diagnostics.The_inferred_type_of_this_node_exceeds_the_maximum_length_the_compiler_will_serialize_An_explicit_type_annotation_is_needed));
}
}

Expand Down Expand Up @@ -1102,7 +1103,9 @@ namespace ts {
diagnosticMessage: Diagnostics.Default_export_of_the_module_has_or_is_using_private_name_0,
errorNode: input
});
errorFallbackNode = input;
const varDecl = factory.createVariableDeclaration(newId, /*exclamationToken*/ undefined, resolver.createTypeOfExpression(input.expression, input, declarationEmitNodeBuilderFlags, symbolTracker), /*initializer*/ undefined);
errorFallbackNode = undefined;
const statement = factory.createVariableStatement(needsDeclare ? [factory.createModifier(SyntaxKind.DeclareKeyword)] : [], factory.createVariableDeclarationList([varDecl], NodeFlags.Const));
return [statement, factory.updateExportAssignment(input, input.decorators, input.modifiers, newId)];
}
Expand Down Expand Up @@ -1326,6 +1329,8 @@ namespace ts {
}
}
case SyntaxKind.ClassDeclaration: {
errorNameNode = input.name;
errorFallbackNode = input;
const modifiers = factory.createNodeArray(ensureModifiers(input));
const typeParameters = ensureTypeParams(input, input.typeParameters);
const ctor = getFirstConstructorWithBody(input);
Expand Down Expand Up @@ -1462,6 +1467,8 @@ namespace ts {
if (node as Node === input) {
return node;
}
errorFallbackNode = undefined;
errorNameNode = undefined;
return node && setOriginalNode(preserveJsDoc(node, input), input);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
tests/cases/compiler/another.ts(11,1): error TS4094: Property '_assertIsStripped' of exported class expression may not be private or protected.
tests/cases/compiler/another.ts(11,1): error TS4094: Property '_onDispose' of exported class expression may not be private or protected.
tests/cases/compiler/first.ts(12,1): error TS4094: Property '_assertIsStripped' of exported class expression may not be private or protected.
tests/cases/compiler/first.ts(12,1): error TS4094: Property '_onDispose' of exported class expression may not be private or protected.
tests/cases/compiler/first.ts(13,14): error TS4094: Property '_assertIsStripped' of exported class expression may not be private or protected.
tests/cases/compiler/first.ts(13,14): error TS4094: Property '_onDispose' of exported class expression may not be private or protected.


==== tests/cases/compiler/first.ts (4 errors) ====
declare function mix<TMix>(mixin: TMix): TMix;

const DisposableMixin = class {
protected _onDispose() {
this._assertIsStripped()
}
private _assertIsStripped() {
}
};

// No error, but definition is wrong.
export default mix(DisposableMixin);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS4094: Property '_assertIsStripped' of exported class expression may not be private or protected.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS4094: Property '_onDispose' of exported class expression may not be private or protected.
export class Monitor extends mix(DisposableMixin) {
~~~~~~~
!!! error TS4094: Property '_assertIsStripped' of exported class expression may not be private or protected.
~~~~~~~
!!! error TS4094: Property '_onDispose' of exported class expression may not be private or protected.
protected _onDispose() {
}
}

==== tests/cases/compiler/another.ts (2 errors) ====
declare function mix<TMix>(mixin: TMix): TMix;

const DisposableMixin = class {
protected _onDispose() {
this._assertIsStripped()
}
private _assertIsStripped() {
}
};

export default class extends mix(DisposableMixin) {
~~~~~~
!!! error TS4094: Property '_assertIsStripped' of exported class expression may not be private or protected.
~~~~~~
!!! error TS4094: Property '_onDispose' of exported class expression may not be private or protected.
protected _onDispose() {
}
}
115 changes: 115 additions & 0 deletions tests/baselines/reference/declarationEmitMixinPrivateProtected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//// [tests/cases/compiler/declarationEmitMixinPrivateProtected.ts] ////

//// [first.ts]
declare function mix<TMix>(mixin: TMix): TMix;

const DisposableMixin = class {
protected _onDispose() {
this._assertIsStripped()
}
private _assertIsStripped() {
}
};

// No error, but definition is wrong.
export default mix(DisposableMixin);
export class Monitor extends mix(DisposableMixin) {
protected _onDispose() {
}
}

//// [another.ts]
declare function mix<TMix>(mixin: TMix): TMix;

const DisposableMixin = class {
protected _onDispose() {
this._assertIsStripped()
}
private _assertIsStripped() {
}
};

export default class extends mix(DisposableMixin) {
protected _onDispose() {
}
}

//// [first.js]
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
exports.__esModule = true;
exports.Monitor = void 0;
var DisposableMixin = /** @class */ (function () {
function class_1() {
}
class_1.prototype._onDispose = function () {
this._assertIsStripped();
};
class_1.prototype._assertIsStripped = function () {
};
return class_1;
}());
// No error, but definition is wrong.
exports["default"] = mix(DisposableMixin);
var Monitor = /** @class */ (function (_super) {
__extends(Monitor, _super);
function Monitor() {
return _super !== null && _super.apply(this, arguments) || this;
}
Monitor.prototype._onDispose = function () {
};
return Monitor;
}(mix(DisposableMixin)));
exports.Monitor = Monitor;
//// [another.js]
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
exports.__esModule = true;
var DisposableMixin = /** @class */ (function () {
function class_1() {
}
class_1.prototype._onDispose = function () {
this._assertIsStripped();
};
class_1.prototype._assertIsStripped = function () {
};
return class_1;
}());
var default_1 = /** @class */ (function (_super) {
__extends(default_1, _super);
function default_1() {
return _super !== null && _super.apply(this, arguments) || this;
}
default_1.prototype._onDispose = function () {
};
return default_1;
}(mix(DisposableMixin)));
exports["default"] = default_1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
=== tests/cases/compiler/first.ts ===
declare function mix<TMix>(mixin: TMix): TMix;
>mix : Symbol(mix, Decl(first.ts, 0, 0))
>TMix : Symbol(TMix, Decl(first.ts, 0, 21))
>mixin : Symbol(mixin, Decl(first.ts, 0, 27))
>TMix : Symbol(TMix, Decl(first.ts, 0, 21))
>TMix : Symbol(TMix, Decl(first.ts, 0, 21))

const DisposableMixin = class {
>DisposableMixin : Symbol(DisposableMixin, Decl(first.ts, 2, 5))

protected _onDispose() {
>_onDispose : Symbol(DisposableMixin._onDispose, Decl(first.ts, 2, 31))

this._assertIsStripped()
>this._assertIsStripped : Symbol(DisposableMixin._assertIsStripped, Decl(first.ts, 5, 5))
>this : Symbol(DisposableMixin, Decl(first.ts, 2, 23))
>_assertIsStripped : Symbol(DisposableMixin._assertIsStripped, Decl(first.ts, 5, 5))
}
private _assertIsStripped() {
>_assertIsStripped : Symbol(DisposableMixin._assertIsStripped, Decl(first.ts, 5, 5))
}
};

// No error, but definition is wrong.
export default mix(DisposableMixin);
>mix : Symbol(mix, Decl(first.ts, 0, 0))
>DisposableMixin : Symbol(DisposableMixin, Decl(first.ts, 2, 5))

export class Monitor extends mix(DisposableMixin) {
>Monitor : Symbol(Monitor, Decl(first.ts, 11, 36))
>mix : Symbol(mix, Decl(first.ts, 0, 0))
>DisposableMixin : Symbol(DisposableMixin, Decl(first.ts, 2, 5))

protected _onDispose() {
>_onDispose : Symbol(Monitor._onDispose, Decl(first.ts, 12, 51))
}
}

=== tests/cases/compiler/another.ts ===
declare function mix<TMix>(mixin: TMix): TMix;
>mix : Symbol(mix, Decl(another.ts, 0, 0))
>TMix : Symbol(TMix, Decl(another.ts, 0, 21))
>mixin : Symbol(mixin, Decl(another.ts, 0, 27))
>TMix : Symbol(TMix, Decl(another.ts, 0, 21))
>TMix : Symbol(TMix, Decl(another.ts, 0, 21))

const DisposableMixin = class {
>DisposableMixin : Symbol(DisposableMixin, Decl(another.ts, 2, 5))

protected _onDispose() {
>_onDispose : Symbol(DisposableMixin._onDispose, Decl(another.ts, 2, 31))

this._assertIsStripped()
>this._assertIsStripped : Symbol(DisposableMixin._assertIsStripped, Decl(another.ts, 5, 5))
>this : Symbol(DisposableMixin, Decl(another.ts, 2, 23))
>_assertIsStripped : Symbol(DisposableMixin._assertIsStripped, Decl(another.ts, 5, 5))
}
private _assertIsStripped() {
>_assertIsStripped : Symbol(DisposableMixin._assertIsStripped, Decl(another.ts, 5, 5))
}
};

export default class extends mix(DisposableMixin) {
>mix : Symbol(mix, Decl(another.ts, 0, 0))
>DisposableMixin : Symbol(DisposableMixin, Decl(another.ts, 2, 5))

protected _onDispose() {
>_onDispose : Symbol(default._onDispose, Decl(another.ts, 10, 51))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
=== tests/cases/compiler/first.ts ===
declare function mix<TMix>(mixin: TMix): TMix;
>mix : <TMix>(mixin: TMix) => TMix
>mixin : TMix

const DisposableMixin = class {
>DisposableMixin : typeof DisposableMixin
>class { protected _onDispose() { this._assertIsStripped() } private _assertIsStripped() { }} : typeof DisposableMixin

protected _onDispose() {
>_onDispose : () => void

this._assertIsStripped()
>this._assertIsStripped() : void
>this._assertIsStripped : () => void
>this : this
>_assertIsStripped : () => void
}
private _assertIsStripped() {
>_assertIsStripped : () => void
}
};

// No error, but definition is wrong.
export default mix(DisposableMixin);
>mix(DisposableMixin) : typeof DisposableMixin
>mix : <TMix>(mixin: TMix) => TMix
>DisposableMixin : typeof DisposableMixin

export class Monitor extends mix(DisposableMixin) {
>Monitor : Monitor
>mix(DisposableMixin) : DisposableMixin
>mix : <TMix>(mixin: TMix) => TMix
>DisposableMixin : typeof DisposableMixin

protected _onDispose() {
>_onDispose : () => void
}
}

=== tests/cases/compiler/another.ts ===
declare function mix<TMix>(mixin: TMix): TMix;
>mix : <TMix>(mixin: TMix) => TMix
>mixin : TMix

const DisposableMixin = class {
>DisposableMixin : typeof DisposableMixin
>class { protected _onDispose() { this._assertIsStripped() } private _assertIsStripped() { }} : typeof DisposableMixin

protected _onDispose() {
>_onDispose : () => void

this._assertIsStripped()
>this._assertIsStripped() : void
>this._assertIsStripped : () => void
>this : this
>_assertIsStripped : () => void
}
private _assertIsStripped() {
>_assertIsStripped : () => void
}
};

export default class extends mix(DisposableMixin) {
>mix(DisposableMixin) : DisposableMixin
>mix : <TMix>(mixin: TMix) => TMix
>DisposableMixin : typeof DisposableMixin

protected _onDispose() {
>_onDispose : () => void
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
tests/cases/compiler/emitClassExpressionInDeclarationFile2.ts(1,12): error TS4094: Property 'p' of exported class expression may not be private or protected.
tests/cases/compiler/emitClassExpressionInDeclarationFile2.ts(1,12): error TS4094: Property 'ps' of exported class expression may not be private or protected.
tests/cases/compiler/emitClassExpressionInDeclarationFile2.ts(16,17): error TS4094: Property 'property' of exported class expression may not be private or protected.
tests/cases/compiler/emitClassExpressionInDeclarationFile2.ts(23,14): error TS4094: Property 'property' of exported class expression may not be private or protected.


==== tests/cases/compiler/emitClassExpressionInDeclarationFile2.ts (3 errors) ====
==== tests/cases/compiler/emitClassExpressionInDeclarationFile2.ts (4 errors) ====
export var noPrivates = class {
~~~~~~~~~~
!!! error TS4094: Property 'p' of exported class expression may not be private or protected.
Expand Down Expand Up @@ -33,6 +34,8 @@ tests/cases/compiler/emitClassExpressionInDeclarationFile2.ts(16,17): error TS40
}

export class Test extends WithTags(FooItem) {}
~~~~
!!! error TS4094: Property 'property' of exported class expression may not be private or protected.

const test = new Test();

Expand Down
Loading