web-infra-dev / web-infra-dev/rslint

[Feature]: Support import attributes in import rules

Open
#2,083 0 comments 2 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
459
Forks
33
Avg merge
1d 2h
Merged PRs (30d)
376

Description

### What problem does this feature solve?

RSLint 0.9.1 parses import attributes, but `import/no-duplicates` and `import/default` report valid imports that load the same file with different attributes.

For example, [Rsbuild supports `type: 'text'`](https://rsbuild.rs/guide/basic/static-assets#with-import-attributes) to import a file's source as a string:

```js
// module.js
export const value = 42;
```

```js
// index.js
import { value } from './module.js';
import source from './module.js' with { type: 'text' };

console.log(value, source);
```

With `importPlugin.configs.recommended`, the imports are reported as duplicates, and the text import is reported as having no default export. However, one import loads the JavaScript module while the other loads its source text.

Different explicit attributes also need to be distinguished:

```js
import data from './data.json' with { type: 'json' };
import source from './data.json' with { type: 'text' };
```

In the [Rsbuild repository at 53d1c36c7](https://github.com/web-infra-dev/rsbuild/tree/53d1c36c7), these scenarios produce 12 `import/no-duplicates` diagnostics and 6 `import/default` diagnostics across four test fixtures. Enabling `considerQueryString` does not resolve them.

### What does the proposed API of configuration look like?

Support these cases without additional configuration:

- Include import attributes when determining whether imports are duplicates. Different attributes must not be merged by autofix; equivalent attributes should compare equally regardless of property order.
- Make export analysis aware of the imported module type. A `type: 'text'` import provides a default string export, regardless of the original file's JavaScript exports.
- Preserve attributes when resolving modules and caching export information, so loading a file as JavaScript does not determine the analysis of a later text import.

Import attributes can affect module loading and resolution; see the [import attributes documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import/with).

Related: #2082 addresses query-string handling in the Recommended preset.

Contributor guide

Open the contributing guide

Research direction

Start at the implementations of import/no-duplicates and import/default, then trace the module-resolution and export-analysis entry points they use. Reproduce the four Rsbuild fixtures described in the issue and verify that differing attributes remain distinct, equivalent attributes compare equally, and type: 'text' provides a default export without leaking cached analysis between import types.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, javascript, typescript
Domain
devtools, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.