Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

eslint-plugin-file-extension-in-import-ts

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-plugin-file-extension-in-import-ts

The plugin check and fix file extensions in Node.js application type module in Typescript

  • 2.0.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

eslint-plugin-file-extension-in-import-ts

Disclaimer

This is small patch to the great rule:

file-extension-in-import

from eslint-plugin-node module.

Author of code:

mysticatea

I've just added mapping functionality.

Motivation

When we use type module we must add extension to the file

import foo from "./path/to/a/file"

It's a bad practice

import eslint from "eslint"
import foo from "./path/to/a/file.js"

It's a good practice.

node/file-extension-in-import rule raise error when we forgot to add extension to the import. But it doesn't work with Typescript.

Full description of rule here

Typescript usage

When we use Typescript we can't add .ts extension to the file. It won't be process with Typescript compiler. In this case we need to add .js extension.

In this case we need to replace .ts extension to .js.

In this plugin I will add possibility to make mapping for extensions:

'file-extension-in-import-ts/file-extension-in-import-ts': [
  'error',
  'always',
  { extMapping: {'.ts': '.js' } }
]

It means, this rule will fix our ts files to .js extension in import statement.

import module from './my-module';

will be changed to:

import module from './my-module/index.js';

Installation:

  1. Install the package:
npm i -D eslint-plugin-file-extension-in-import-ts
  1. Add to .eslintrc.js:
{
  "plugins": [
    "file-extension-in-import-ts"
  ],
  "rules": {
    "file-extension-in-import-ts/file-extension-in-import-ts": "error"
  }
}

By default, the plugin will process mapping:

  • .ts -> .js
  • .tsx -> .js

Troubleshooting

This plugin has conflict with eslint-import-plugin, the rule: import/no-unresolved rule

To quick fix it we can skip '.js' (in the folder with TS sources JS files are not existing).

'import/no-unresolved': ['error', { ignore: [ '\\.js$' ] }],

The MIT License

Copyright (c)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

FAQs

Package last updated on 26 Nov 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc