-
Notifications
You must be signed in to change notification settings - Fork 226
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
Duplicating lines of code #144
Comments
This may be related to an issue I'm seeing with the const foo = {
func: function () {
Object.values({ })
.filter((x) => x.prop)
.forEach((x) => {
const y = x.prop;
});
},
}; This transforms into this code, with similar duplication: const foo = {
func: function () {
Object.values({})
.filter((x) => (x as $TSFixImplicitAny).prop)
.forEach((x) => {
// @ts-expect-error ts-migrate(2571) FIXME: Object is of type 'unknown'.
const y = x.prop;
});
// @ts-expect-error ts-migrate(2304) FIXME: Cannot find name 'x'.
const y = (x as $TSFixImplicitAny).prop;
});
},
}; There are a few interesting things I've figured out while trying to debug this. One, in order for this code to transform incorrectly, you need both the // [...]
.filter(x => { return x.prop; })
// [...] the transform will be successful without any duplicated lines. My current theory is that the |
Can confirm @zb140 's assessment that this issue seems to be happening with the
For example, let's say we have code as follows: constants.DEPARTMENT.OPTIONS = _.sortBy(
Object.keys(constants.DEPARTMENT.ROLE_MAP).map((value) => {
return {
value,
text: constants.DEPARTMENT.ROLE_MAP[value],
};
}),
["text"]
); where (constants as any).DEPARTMENT.OPTIONS = _.sortBy(Object.keys((constants as any).DEPARTMENT.ROLE_MAP).map((value) => {
return {
value,
// @ts-expect-error ts-migrate(2339) FIXME: Property 'DEPARTMENT' does not exist on type '{}'.
text: constants.DEPARTMENT.ROLE_MAP[value],
};
}), ["text"]);
return {
value,
text: (constants as any).DEPARTMENT.ROLE_MAP[value],
};
}),
["text"]
); However, if this were to be fixed ahead of time to: constants.DEPARTMENT.OPTIONS = _.sortBy(
Object.keys(constants.DEPARTMENT.ROLE_MAP).map((value) => {
return {
value,
text: (constants as any).DEPARTMENT.ROLE_MAP[value],
};
}),
["text"]
); then the duplication will NOT happen. One workaround that seems to work is extracting the callback function and making it a named function outside like so: const innerFn = (value) => {
return {
value,
text: constants.DEPARTMENT.ROLE_MAP[value],
};
}
constants.DEPARTMENT.OPTIONS = _.sortBy(
Object.keys(constants.DEPARTMENT.ROLE_MAP).map(innerFn),
["text"]
); Then running const innerFn = (value) => {
return {
value,
text: (constants as any).DEPARTMENT.ROLE_MAP[value],
};
};
(constants as any).DEPARTMENT.OPTIONS = _.sortBy(
Object.keys((constants as any).DEPARTMENT.ROLE_MAP).map(innerFn),
["text"]
); Though this is a bit too heavy handed a change for my taste. |
Hello I am currently migrating a old react project to typescript. |
I have been poking around at this, and think its either replacement ordering problem or changes aren't getting propagated up the node tree correctly.
Test Code
|
Looking at |
- Prevents code duplication when nested nested statement expressions need are run though the `add-conversions` pluggin. - Previously add-conversions would generate two over lapping updates which `apply()` can not handle correctly.
Should be fixed with #181 |
@Rudeg could you publish a new version to npm? Would be super grateful if so :) |
@shawngustaw sorry for the delay, done |
As of 0.1.32, I continue to get mangled output in certain cases, e.g. running this through add-conversions: function foo() {
return {}.prop;
}
export default {}.prop; produces: function foo() {
return {}.prop;
}
export default ({} as any).prop;
return ({} as any).prop;
}
export default {}.prop; |
There are most likely a few other ancestor node types that we need to check for when deciding if the node should be replaced. I don't know of a good way to actually figure out what those are other than people reporting issues. Hopefully it is as simple as just adding another test and additional |
@doncollins Sadly its not simple as just adding |
I am testing out ts-migrate-full on a large project that uses an older version of react. ts-migrate (maybe the explicit-any plugin??) doesn't seem to be properly replacing code in some plain
.js
files, as you can see in the code snippet below.Before
After
Expected
I am seeing ~1000 errors from tsc but its really just ~200 instances this thought the code base. Manually fixing these bugs isn't that bad, when compared to all the other work ts-migrate did for me.
The text was updated successfully, but these errors were encountered: