question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow mapping of expected extensions in import/extension rule

See original GitHub issue

Context:

I have a typescript project for a node application using ESM (ecmascript modules) Because node requires that file extensions be specified for relative imports when using esm, typescript files must import relative modules with the “.js” extension. Using a typescript resolver enables linting to resolve these modules well, but when trying to enforce file extensions using the import/extensions rule, the linter expects the file extensions to be “.ts”. I would like a way to configure the rule to assert that extension rather be “.js”

Repro :

applying linting rules

npx eslint .

in the following gist https://gist.github.com/pwhissell/165232e5fdd62641efa771f000f1e651

Current output:

1:31 error Missing file extension "ts" for "./other.js" import/extensions 2:33 error Missing file extension "ts" for "./another" import/extensions

Expected:

2:33 error Missing file extension "js" for "./another" import/extensions

Example usage:

rules: { import/extensions: ["error", {"ts", "ignorePackages", "js"}] #<-- this would tell the linter that modules defined in ts files should always be imported with the .js extension }

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
pwhissellcommented, Apr 24, 2021

You can use those features regardless of your transpilation target; there’s no reason you need to use native ESM for that.

I’m sure your experience at tc39 gave you lots of understanding about ecmascript ambitions. Perhaps you should read a little on nodejs, esm and commonjs modules before making such claims.

here are a couple articles, posts and issues that may be helpfull

I’m still a bit confused. It sounds like the TS resolver is the problem here, not the linting rule?

It sounds like you haven’t taken a look at my gist, maybe you missed the link https://gist.github.com/pwhissell/165232e5fdd62641efa771f000f1e651

the resolver resolves the module well (wether the “.js” extension is present or not) but the “extension” linting rule enforces a .js extension

should i draft a PR?

1reaction
ljharbcommented, Mar 17, 2022

@berniegp the only things type module do are 1) make .js be ESM instead of CJS, which breaks compatibility with older nodes that assume .js is always CJS; 2) in some cases shuts off normal node resolution of extensions and index; 3) breaks tooling (like this plugin, which uses resolve) that doesn’t yet support exports or type.

If you just name your ESM files .mjs, everything works fine, and you don’t run into any of these problems.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Allow mapping of expected extensions in import/extension rule
Context: I have a typescript project for a node application using ESM (ecmascript modules) Because node requires that file extensions be ...
Read more >
Managing extension mappings (Watson Knowledge Catalog)
You can work with extension mapping documents, extended data sources, and other additional information assets by using the Information Governance Catalog user ...
Read more >
Improved Import / Export Magento 2 Extension Manual | FireBear
Improved Import and Export extension allows importing and exporting CSV/XML/JSON files with product data and product images to Magento 2.
Read more >
Extending IMDF - Indoor Mapping Data Format
Extending IMDF. IMDF supports formalized extension by declaring new Feature types or additional foreign members and properties of existing Feature types for ...
Read more >
Microsoft Edge Browser Policy Documentation
ImportExtensions, Allow importing of extensions ... Overrides Microsoft Edge default printer selection rules. This policy determines the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found