Skip to content
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

fix(eslint-plugin): [consistent-type-imports] import assertion checks added #7722

Conversation

rielAsh24
Copy link
Contributor

Added type assertion for json files

PR Checklist

 

Overview

This PR addresses the issue where typescript-eslint/eslint-plugin throws an error when an import statement with type imports includes an import assertions.

Reproduction Code:

import * as aJson from './a.json' assert { type: 'json' }

const what: aJson.A;

Actual Behaviour

typescript-eslint provides an invalid TypeScript code as a fix and hence had to be addressed by adding a rule that checks for such import type assertions.

Expected Behaviour

Since typescript 4.5, import assertions are supported, so No Error or All is Ok is the expected output.

 

Changes made:

  1. Modified rule in consistent-type-imports.ts for type only import. Now it checks for absence of assertions and only then suggests the fix.
// checks for presence of import assertions
if (node.assertions.length === 0) {
  yield * fixInsertTypeSpecifierForImportDeclaration(fixer, node, false);
}
  1. Tweaked reporting conditions in consistent-type-imports.ts for type only imports. Now it checks for absence of import assertions and only then reports the error.
/** checks if import has type assertions
 * ```
 * import * as type from 'mod' assert { type: 'json' };
 * ```
 * https://github.com/typescript-eslint/typescript-eslint/issues/7527
 */
if (report.node.assertions.length === 0) {
  context.report({
    node: report.node,
    messageId: "typeOverValue",
    *fix(fixer) {
      yield* fixToTypeImportDeclaration(fixer, report, sourceImports);
    }
  });
}
  1. Added a rule to consistent-type-imports.test.ts which considers import assertion for json files as a valid rule.
  {
    code: `
      import * as Type from 'foo' assert { type: 'json' };
      const a: typeof Type = Type;
    `,
    options: [{ prefer: 'no-type-imports' }],
    dependencyConstraints: {
      typescript: '4.5',
    },
  },

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @rielAsh24!

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.

@netlify
Copy link

netlify bot commented Oct 4, 2023

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 81f63d6
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/65269a352a2c260009d0fc9b
😎 Deploy Preview https://deploy-preview-7722--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 3 from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (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.

@rielAsh24 rielAsh24 force-pushed the Fix-7527_Import-assertions-fixed branch from c7f375e to ed1119f Compare October 4, 2023 14:23
@rielAsh24 rielAsh24 changed the title fix(eslint-plugin):[consistent-type-imports] Import assertion checks added fix(eslint-plugin): [consistent-type-imports] Import assertion checks added Oct 5, 2023
@rielAsh24 rielAsh24 changed the title fix(eslint-plugin): [consistent-type-imports] Import assertion checks added fix(eslint-plugin): [consistent-type-imports] import assertion checks added Oct 5, 2023
Added type assertion for json files
The linter checks for import assertions before suggesting fixes
@rielAsh24 rielAsh24 force-pushed the Fix-7527_Import-assertions-fixed branch from ed1119f to 81f63d6 Compare October 11, 2023 12:50
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.

Thanks!

Lady on a boat saying 'this is glorious'

@JoshuaKGoldberg JoshuaKGoldberg merged commit afdae37 into typescript-eslint:main Oct 11, 2023
46 checks passed
@rielAsh24
Copy link
Contributor Author

rielAsh24 commented Oct 11, 2023

Thanks!

Lady on a boat saying 'this is glorious'

Glad I could help! 😁

@rielAsh24 rielAsh24 deleted the Fix-7527_Import-assertions-fixed branch October 13, 2023 13:38
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: [consistent-type-imports] Incorrectly suggests import type on import assertion
2 participants