Skip to content

fix(typescript-estree): ensure consistent TSMappedType AST shape #11086

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

Conversation

dbarabashh
Copy link
Contributor

PR Checklist

Overview

var a: { [k in any]: any }              // No modifiers
var b: { [k in any]?: any }             // Optional modifier
var c: { readonly [k in any]: any }     // Readonly modifier
var d: { +readonly [k in any]?: any }   // +readonly and optional modifier
var e: { -readonly [k in any]: any }    // -readonly modifier

Example a: { [k in any]: any }

{
  type: "TSMappedType",
  constraint: {
    type: "TSAnyKeyword"
  },
  key: {
    type: "Identifier",
    name: "k"
  },
  nameType: null,
  optional: false,             // Now has explicit false value
  readonly: undefined,         // Now has explicit undefined value
  typeAnnotation: {
    type: "TSAnyKeyword"
  }
}

Example b: { [k in any]?: any }

{
  type: "TSMappedType",
  constraint: {
    type: "TSAnyKeyword"
  },
  key: {
    type: "Identifier",
    name: "k"
  },
  nameType: null,
  optional: true,              // Has true value because of ? modifier
  readonly: undefined,         // Now has explicit undefined value
  typeAnnotation: {
    type: "TSAnyKeyword"
  }
}

Example c: { readonly [k in any]: any }

{
  type: "TSMappedType",
  constraint: {
    type: "TSAnyKeyword"
  },
  key: {
    type: "Identifier",
    name: "k"
  },
  nameType: null,
  optional: false,             // Now has explicit false value
  readonly: true,              // Has true value because of readonly modifier
  typeAnnotation: {
    type: "TSAnyKeyword"
  }
}

Example d: { +readonly [k in any]?: any }

{
  type: "TSMappedType",
  constraint: {
    type: "TSAnyKeyword"
  },
  key: {
    type: "Identifier",
    name: "k"
  },
  nameType: null,
  optional: true,              // Has true value because of ? modifier
  readonly: "+",               // Has "+" value because of +readonly modifier
  typeAnnotation: {
    type: "TSAnyKeyword"
  }
}

Example e: { -readonly [k in any]: any }

{
  type: "TSMappedType",
  constraint: {
    type: "TSAnyKeyword"
  },
  key: {
    type: "Identifier",
    name: "k"
  },
  nameType: null,
  optional: false,             // Now has explicit false value
  readonly: "-",               // Has "-" value because of -readonly modifier
  typeAnnotation: {
    type: "TSAnyKeyword"
  }
}

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @dbarabashh!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint.

Copy link

netlify bot commented Apr 17, 2025

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 656a3f9
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/6818278fcd0dae0008550592
😎 Deploy Preview https://deploy-preview-11086--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 99 (🟢 up 1 from production)
Accessibility: 100 (no change from production)
Best Practices: 100 (no change from production)
SEO: 98 (no change from production)
PWA: 80 (no change from production)
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

nx-cloud bot commented Apr 17, 2025

View your CI Pipeline Execution ↗ for commit 656a3f9.

Command Status Duration Result
nx run-many --target=build --exclude website --... ✅ Succeeded 2s View ↗
nx run-many --target=clean ✅ Succeeded 10s View ↗

☁️ Nx Cloud last updated this comment at 2025-05-05 15:12:23 UTC

@dbarabashh dbarabashh changed the title fix(typescript-estree): Inconsistent TSMappedType AST shape fix(typescript-estree): ensure consistent TSMappedType AST shape Apr 17, 2025
@dbarabashh dbarabashh changed the title fix(typescript-estree): ensure consistent TSMappedType AST shape fix(eslint-plugin): ensure consistent TSMappedType AST shape Apr 18, 2025
readonly: node.readonlyToken
? node.readonlyToken.kind === SyntaxKind.ReadonlyKeyword ||
getTextForTokenKind(node.readonlyToken.kind)
: undefined,
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg May 2, 2025

Choose a reason for hiding this comment

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

Note that readonly is an enum of true, + and -. So previously I proposed undefined as the default value. I guess false does no harm here.

Agreed that either could work, and also agreed on going with undefined. That feels to me like a more normal default when there are multiple truthy options ('+', '-'). I can see why false might make sense as a counterpoint to true, but visually I think it makes less sense than undefined.

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

🙌 The source code change looks good to me, thanks! Requesting changes on going with the ast-spec tests that other types of syntax conversions are verified with too.

@@ -449,5 +449,79 @@ describe('convert', () => {
expect(tsMappedType.typeParameter).toBeUndefined();
expect(Object.keys(tsMappedType)).toContain('typeParameter');
});

describe('TSMappedType AST shape consistency', () => {
Copy link
Member

Choose a reason for hiding this comment

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

[Testing] We normally put fixtures in packages/ast-spec/src. Is there a reason not to here?

If not, I think packages/ast-spec/src/type/TSMappedType would be the right place to add new fixtures around readonly and/or optional mapped types.

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label May 2, 2025
Copy link

codecov bot commented May 2, 2025

Codecov Report

Attention: Patch coverage is 25.00000% with 6 lines in your changes missing coverage. Please review.

Project coverage is 90.84%. Comparing base (f30a20e) to head (656a3f9).
Report is 19 commits behind head on main.

Files with missing lines Patch % Lines
packages/typescript-estree/src/convert.ts 25.00% 6 Missing ⚠️

❌ Your patch status has failed because the patch coverage (25.00%) is below the target coverage (90.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #11086      +/-   ##
==========================================
+ Coverage   90.82%   90.84%   +0.01%     
==========================================
  Files         497      497              
  Lines       50204    50320     +116     
  Branches     8274     8311      +37     
==========================================
+ Hits        45600    45714     +114     
- Misses       4589     4591       +2     
  Partials       15       15              
Flag Coverage Δ
unittest 90.84% <25.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
packages/typescript-estree/src/convert.ts 30.74% <25.00%> (ø)

... and 7 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bradzacher bradzacher requested a review from JoshuaKGoldberg May 5, 2025 02:22
@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label May 5, 2025
@bradzacher bradzacher force-pushed the fix/ast-tsmappedtype-shape branch from 3c36704 to 6a655f2 Compare May 5, 2025 02:45
@bradzacher bradzacher force-pushed the fix/ast-tsmappedtype-shape branch from 6a655f2 to 76f693f Compare May 5, 2025 02:45
Copy link
Member

@bradzacher bradzacher left a comment

Choose a reason for hiding this comment

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

thanks -- just because my review time is limited I took a moment to fix up your fixtures (you had nested the folders incorrectly which meant the snapshots weren't generated -- we might wanna add an error for this...). And I added a few extra snapshots to cover some more cases.

this all LGTM!

@bradzacher bradzacher added the 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge label May 5, 2025
@dbarabashh
Copy link
Contributor Author

@bradzacher Thank you so much. I was just about to write a comment about the file structure.

and one more question, what should we do about the coverage rate?

@JoshuaKGoldberg JoshuaKGoldberg changed the title fix(eslint-plugin): ensure consistent TSMappedType AST shape fix(typescript-estree): ensure consistent TSMappedType AST shape May 5, 2025
@JoshuaKGoldberg
Copy link
Member

coverage rate

#6116 strikes again! We can ignore the coverage drop in this PR.

@JoshuaKGoldberg JoshuaKGoldberg merged commit 4a97721 into typescript-eslint:main May 5, 2025
60 of 61 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: Inconsistent TSMappedType AST shape for optional and readonly
3 participants