-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Make direct assignments to cjs exports considered literal contexts #39816
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
Make direct assignments to cjs exports considered literal contexts #39816
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with this if we already issue an error for
module.exports.a = 1
module.exports.a = 2
Although I didn't think we did. It's a little weird that I don't see any new error baselines, so maybe we just don't have coverage.
Besides that, I have a couple of questions and a few formatting suggestions.
src/compiler/checker.ts
Outdated
@@ -8069,7 +8069,8 @@ namespace ts { | |||
if (containsSameNamedThisProperty(expression.left, expression.right)) { | |||
return anyType; | |||
} | |||
const type = resolvedSymbol ? getTypeOfSymbol(resolvedSymbol) : getWidenedLiteralType(checkExpressionCached(expression.right)); | |||
const isDirectExport = kind === AssignmentDeclarationKind.ExportsProperty && (isPropertyAccessExpression(expression.left) || isElementAccessExpression(expression.left)) && (isModuleExportsAccessExpression(expression.left.expression) || (isIdentifier(expression.left.expression) && isExportsIdentifier(expression.left.expression))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const isDirectExport = kind === AssignmentDeclarationKind.ExportsProperty && (isPropertyAccessExpression(expression.left) || isElementAccessExpression(expression.left)) && (isModuleExportsAccessExpression(expression.left.expression) || (isIdentifier(expression.left.expression) && isExportsIdentifier(expression.left.expression))); | |
const isDirectExport = kind === AssignmentDeclarationKind.ExportsProperty | |
&& isAccessExpression(expression.left) | |
&& (isModuleExportsAccessExpression(expression.left.expression) || (isIdentifier(expression.left.expression) && isExportsIdentifier(expression.left.expression))); |
I think there might be an existing utility that does what you want here, but it may include other cases. (Could it be isCommonJsExportPropertyAssignment
?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't find one. :S
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would still be nice to (1) switch to isAccessExpression
(2) break up this super-long line
We do! I was surprised we did as well! But given that, considering the exports as psuedo-readonly makes sense to me. |
@typescript-bot user test this |
Heya @weswigham, I've started to run the parallelized community code test suite on this PR at 380990f. You can monitor the build here. |
The user suite test run you requested has finished and failed. I've opened a PR with the baseline diff from master. |
Diff from |
Lemme fix that lint error... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I said during the design meeting, I think if there aren't many breaking changes in the user tests, you should just merge after we've cut 4.0 RC.
src/compiler/checker.ts
Outdated
@@ -8069,7 +8069,8 @@ namespace ts { | |||
if (containsSameNamedThisProperty(expression.left, expression.right)) { | |||
return anyType; | |||
} | |||
const type = resolvedSymbol ? getTypeOfSymbol(resolvedSymbol) : getWidenedLiteralType(checkExpressionCached(expression.right)); | |||
const isDirectExport = kind === AssignmentDeclarationKind.ExportsProperty && (isPropertyAccessExpression(expression.left) || isElementAccessExpression(expression.left)) && (isModuleExportsAccessExpression(expression.left.expression) || (isIdentifier(expression.left.expression) && isExportsIdentifier(expression.left.expression))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would still be nice to (1) switch to isAccessExpression
(2) break up this super-long line
src/compiler/checker.ts
Outdated
const isDirectExport = kind === AssignmentDeclarationKind.ExportsProperty && (isPropertyAccessExpression(expression.left) || isElementAccessExpression(expression.left)) && (isModuleExportsAccessExpression(expression.left.expression) || (isIdentifier(expression.left.expression) && isExportsIdentifier(expression.left.expression))); | ||
const type = resolvedSymbol ? getTypeOfSymbol(resolvedSymbol) | ||
: isDirectExport ? getRegularTypeOfLiteralType(checkExpressionCached(expression.right)) | ||
: getWidenedLiteralType(checkExpressionCached(expression.right)); | ||
if (type.flags & TypeFlags.Object && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we change how js file works we probably want to change how json works too. As we say that json is nothing but module.exports = json
but with json it is questionable if we really need to use literal type or not.
@weswigham what do you think about @sheetalkamat's point about JSON? I think that's the only thing blocking this from being merged. |
I'd rather not change the literaliness of json at only the top level (also it's an export assignment, not an assignment of an individual export, so it's a bit different in structure) since we have ongoing discussions about |
@sheetalkamat does that make sense to you? If so, can you sign off? |
I think @DanielRosenwasser should comment on this or potentially discuss this in design meeting to ensure we are all on same page. I am not sure why |
@DanielRosenwasser can you give an opinion on @sheetalkamat 's question? |
It sounds like this might be fixed as per #37403 (comment) |
Yeah, I can't actually guide/update a thing right now, soo.... would need help from y'all 🤷 |
Re-reading this, I vote that we take this change and then discuss a change to JSON literalness separately. I think this is a good change on its own and the mismatch with JSON won't be too noticeable. @sheetalkamat is that OK with you? |
@sandersn been awhile, but think we wanna merge this now? |
Fixes #37403
Fixes #37404