-
-
Notifications
You must be signed in to change notification settings - Fork 3
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
Computed or Invalid property names #8
Comments
I think that you don't use enabled |
I you'd like to disable renaming for some properties, you can mark them as But I agree that it'd be nice to have some warnings/errors to notify that something could be broken. |
As for having the errors/warnings I think it would be ideal, because I can't think of a case where this would NOT cause an error in the output in production silently, when having a computed property at least. I think around line 129 in transformer.ts if its an element access expression and NOT a string literal throw a nice error :) |
Yeah, that's why you don't have any error. I think that it's really better to enable it, because it can avoid some hidden errors (this transformer rely on type system, and if you'll have
Yeah, just created #10 for that. So, what's we have at the end here (you need to use enabled |
In my project I have cases where I may use a computed property or a property that has a special character in it. It seems if noImplicityAny is turned on instead of simply doing |
Any access to |
Isn't the type irrelevant? Only the property name, it shouldn't even be an anytype as it is just accessing the property test.salad and test[‘salad’] are equivalent
…Sent from my Windows 10 device
From: Evgeniy Timokhov
Sent: Monday, June 29, 2020 10:27 AM
To: timocov/ts-transformer-properties-rename
Cc: Arthur Fiedler; Author
Subject: Re: [timocov/ts-transformer-properties-rename] Computed or Invalidproperty names (#8)
I would then need to do(test)['salad-dressing']and then run into the same issue?
Any access to any type isn't modified, because we don't know whether a type is exported or not.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
Not actually - the tool tries to understand whether a property is exposes to the public (or declared in outside of the project) uses types: ts-transformer-properties-rename/src/transformer.ts Lines 276 to 317 in c38f3e7
But to detect the property of what type, we uses type system.
Yes, they are and they both are handled by the tool in the same way (they should be).
And again - we need to know of what type the property is to handle renaming correctly (thus, for instance, one property could be internal one, another one could be public). |
Makes complete sense, so I'll slowly work my project to make everything type specific. I had given the updated code a whirl and significantly more compression/mangling occurred, however I did receive some errors when first testing the resulting code. I'm going to caulk this up as an issue with the any type issue for now without investigation, but I'll be sure to follow up when I get my code compiling with out implicit any. |
However, I was looking at a few issues I might run into and I think it would be ideal if any internal/private renamings were traced some to determine if they ever run into an inconsistent type and then abort renaming and generate a warning... Here are some cases I'm thinking... Btw the client side of my webapp project is over 2MB generated before minification so for me converting from no property mangling to property mangling and verifying everything is going to be time consuming. So some safe guards to make sure I find any potential issues at compile/transform time is going to be key. Source export class Direction {
public static Top = 1;
public static Bottom = 2;
public static UpsideDown = 3;
}
export class Directions {
public static fromString(direction: string): Direction {
return Direction[direction];
}
}
console.log("Dynamic property via method", Directions.fromString("Top"));
var direction: string = "Top"; // Could be from string
console.log("Dynamic property reference via property", Direction[direction]);
var direction: string = (<any>{ direction: 'Top' }).direction; // Could be from lets say XMLHttpRequest response
console.log("Dynamic property reference via property", Direction[direction]);
console.log("Static property reference", Direction["Top"]); // Works
// From XMLHttpRequest response without defining a type structure
var test1: string = (<any>{ direction: 'Top' }).direction;
// From XMLHttpRequest response without defining a type structure
var test2: string = (<any>{ /** @public */ direction: 'Top' }).direction;
// From XMLHttpRequest response with type structure (could be defined as test data instead of received from actual XMLHttpRequest)
var test3: string = (<{ direction: string }><any>{ direction: 'Top' }).direction;
// From XMLHttpRequest response with type structure (could be defined as test data instead of received from actual XMLHttpRequest)
var test4: string = (<{ /** @public */ direction: string }><any>{ direction: 'Top' }).direction; Output (function(exports){
exports.Directions = exports.Direction = void 0;
var Direction = (function () {
function Direction() {
}
Direction._internal_Top = 1;
Direction._internal_Bottom = 2;
Direction._internal_UpsideDown = 3;
return Direction;
}());
exports.Direction = Direction;
var Directions = (function () {
function Directions() {
}
Directions._internal_fromString = function (direction) {
return Direction[direction];
};
return Directions;
}());
exports.Directions = Directions;
console.log("Dynamic property via method", Directions._internal_fromString("Top"));
var direction = "Top";
console.log("Dynamic property reference via property", Direction[direction]);
var direction = { _internal_direction: 'Top' }.direction;
console.log("Dynamic property reference via property", Direction[direction]);
console.log("Static property reference", Direction["_internal_Top"]);
var test1 = { _internal_direction: 'Top' }.direction;
var test2 = { _internal_direction: 'Top' }.direction;
var test3 = { _internal_direction: 'Top' }._internal_direction;
var test4 = { _internal_direction: 'Top' }._internal_direction;
})(FC); Issues as I see them...
|
Yeah, it looks like a bug, but only in case of different type of But even in this case there is issue with this code - it's easy to access to
The type of
It's tricky and comes from TypeScript type system. The original code looks like I don't even know how to identify such cases to warn about them actually.
It looks like a bug, I'll try to fix it.
It works because it renamed both
The same as the first one about "late type binding". Overall, I agree that there might be cases where it'd be better to have warnings/errors or even just work, but often we just "faced" limitation of TypeScript type system and how it works. All cases I've seen (from your examples or in my projects) could be fixed by putting types for them. Some of practices couldn't be just used due their nature (like accessing to a random object's property, using Advanced optimizations require put some limitations to your project, what you can use and what can't, what can break your project easily. If you'd like you use TypeScript in JavaScript-like style (don't write types, don't specify all "inbound" and "outbound" types of variables/functions/classes), I'd say that you can't use this transformer (like you can't use google closure compiler for similar reasons - it also requires you write the code in the way it understand rather than you'd like to write). |
Actually it seems that it's incorrectly written JSDoc (or TSDoc?). If you write it in the following it will works as expected and won't be minified: var test2: string = ({
/** @public */
direction: 'Top'
}).direction |
JS is very dynamic language, which allows you doing a lot of crazy stuff, which will be grammatically correct, could be compiled with TypeScript, but cannot be easily (or even totally) optimized in any way. As I said, advanced optimizations require put some limitations to your project. I guess that it'd be one of them. You can use enums instead, or mark this class as public to avoid it's minification. I just tried to fix it, and there are a lot of other cases which |
Bug report
Input code
Expected output
Actual output
Additional context
The two issues are...
1. 'painInTheButton' + i wont get renamed to '_internal_painInTheButton' + i, even if it did, terser wouldn't be able to rename that property to the mapped mangle.
2. 'painInTheButton3' doesn't exist on the object, since I only defined up to 2.
I would prefer these computed and invalid properties to throw an error when compiling, and if I want to ignore them have some option to do so. I would rather not find the bug through user report :(
The reason I would think the invalid property should error is to specifically review, and then can change to warn about it, or ignore it
The text was updated successfully, but these errors were encountered: