Skip to content

Commit cefd17a

Browse files
authored
feat!: change the parser to an ESM-only package (#599)
1 parent 028d840 commit cefd17a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+371
-469
lines changed

.changeset/quick-beers-reflect.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte-eslint-parser": major
3+
---
4+
5+
feat!: change the parser to an ESM-only package

README.md

+20-140
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ You can check it on [Online DEMO](https://sveltejs.github.io/svelte-eslint-parse
88
> Please refer to the README for the version you are using.\
99
> For example, <https://github.com/sveltejs/svelte-eslint-parser/blob/v0.43.0/README.md>
1010
11-
**_Note that this parser has experimental support for Svelte v5, but may break with new versions of Svelte v5._**
12-
1311
[![NPM license](https://img.shields.io/npm/l/svelte-eslint-parser.svg)](https://www.npmjs.com/package/svelte-eslint-parser)
1412
[![NPM version](https://img.shields.io/npm/v/svelte-eslint-parser.svg)](https://www.npmjs.com/package/svelte-eslint-parser)
1513
[![NPM downloads](https://img.shields.io/badge/dynamic/json.svg?label=downloads&colorB=green&suffix=/day&query=$.downloads&uri=https://api.npmjs.org//downloads/point/last-day/svelte-eslint-parser&maxAge=3600)](http://www.npmtrends.com/svelte-eslint-parser)
@@ -41,12 +39,6 @@ It provides many unique check rules by using the template AST.
4139
ESLint plugin for internationalization (i18n) with Svelte.
4240
It provides rules to help internationalization your application created with Svelte.
4341

44-
## ❗ Attention
45-
46-
The [svelte-eslint-parser] can not be used with the [eslint-plugin-svelte3].
47-
48-
[svelte-eslint-parser]: https://github.com/sveltejs/svelte-eslint-parser
49-
5042
## 💿 Installation
5143

5244
```bash
@@ -74,20 +66,6 @@ export default [
7466
];
7567
```
7668

77-
### ESLint Config (`.eslintrc.*`)
78-
79-
```json
80-
{
81-
"extends": "eslint:recommended",
82-
"overrides": [
83-
{
84-
"files": ["*.svelte"],
85-
"parser": "svelte-eslint-parser"
86-
}
87-
]
88-
}
89-
```
90-
9169
### CLI
9270

9371
```console
@@ -101,18 +79,26 @@ $ eslint src --ext .svelte
10179
[`parserOptions`] has the same properties as what [espree](https://github.com/eslint/espree#usage), the default parser of ESLint, is supporting.
10280
For example:
10381

104-
```json
105-
{
106-
"parserOptions": {
107-
"sourceType": "module",
108-
"ecmaVersion": 2021,
109-
"ecmaFeatures": {
110-
"globalReturn": false,
111-
"impliedStrict": false,
112-
"jsx": false
113-
}
114-
}
115-
}
82+
```js
83+
import svelteParser from "svelte-eslint-parser";
84+
export default [
85+
// ...
86+
{
87+
files: ["**/*.svelte", "*.svelte"],
88+
languageOptions: {
89+
parser: svelteParser,
90+
parserOptions: {
91+
sourceType: "module",
92+
ecmaVersion: 2021,
93+
ecmaFeatures: {
94+
globalReturn: false,
95+
impliedStrict: false,
96+
jsx: false,
97+
},
98+
},
99+
},
100+
},
101+
];
116102
```
117103

118104
[`parserOptions`]: https://eslint.org/docs/latest/use/configure/parser#configure-parser-options
@@ -139,17 +125,6 @@ export default [
139125
];
140126
```
141127

142-
For example in `.eslintrc.*`:
143-
144-
```json
145-
{
146-
"parser": "svelte-eslint-parser",
147-
"parserOptions": {
148-
"parser": "@typescript-eslint/parser"
149-
}
150-
}
151-
```
152-
153128
If you are using the `"@typescript-eslint/parser"`, and if you want to use TypeScript in `<script>` of `.svelte`, you need to add more `parserOptions` configuration.
154129

155130
For example in `eslint.config.js`:
@@ -183,32 +158,6 @@ export default [
183158
];
184159
```
185160

186-
For example in `.eslintrc.*`:
187-
188-
```js
189-
module.exports = {
190-
// ...
191-
parser: "@typescript-eslint/parser",
192-
parserOptions: {
193-
// ...
194-
project: "path/to/your/tsconfig.json",
195-
extraFileExtensions: [".svelte"], // This is a required setting in `@typescript-eslint/parser` v4.24.0.
196-
},
197-
overrides: [
198-
{
199-
files: ["*.svelte"],
200-
parser: "svelte-eslint-parser",
201-
// Parse the `<script>` in `.svelte` as TypeScript by adding the following configuration.
202-
parserOptions: {
203-
parser: "@typescript-eslint/parser",
204-
},
205-
},
206-
// ...
207-
],
208-
// ...
209-
};
210-
```
211-
212161
#### Multiple parsers
213162

214163
If you want to switch the parser for each lang, specify the object.
@@ -235,21 +184,6 @@ export default [
235184
];
236185
```
237186

238-
For example in `.eslintrc.*`:
239-
240-
```json
241-
{
242-
"parser": "svelte-eslint-parser",
243-
"parserOptions": {
244-
"parser": {
245-
"ts": "@typescript-eslint/parser",
246-
"js": "espree",
247-
"typescript": "@typescript-eslint/parser"
248-
}
249-
}
250-
}
251-
```
252-
253187
### parserOptions.svelteConfig
254188

255189
If you are using `eslint.config.js`, you can provide a `svelte.config.js` in the `parserOptions.svelteConfig` property.
@@ -289,7 +223,6 @@ export default [
289223
parser: svelteParser,
290224
parserOptions: {
291225
svelteFeatures: {
292-
/* It may be changed or removed in minor versions without notice. */
293226
// This option is for Svelte 5. The default value is `true`.
294227
// If `false`, ESLint will not recognize rune symbols.
295228
// If not configured this option, The parser will try to read the option from `compilerOptions.runes` from `svelte.config.js`.
@@ -302,28 +235,8 @@ export default [
302235
];
303236
```
304237

305-
For example in `.eslintrc.*`:
306-
307-
```jsonc
308-
{
309-
"parser": "svelte-eslint-parser",
310-
"parserOptions": {
311-
"svelteFeatures": {
312-
/* It may be changed or removed in minor versions without notice. */
313-
// This option is for Svelte 5. The default value is `true`.
314-
// If `false`, ESLint will not recognize rune symbols.
315-
// If not configured this option, The parser will try to read the option from `compilerOptions.runes` from `svelte.config.js`.
316-
// If `parserOptions.svelteConfig` is not specified and the file cannot be parsed by static analysis, it will behave as `true`.
317-
"runes": true,
318-
},
319-
},
320-
}
321-
```
322-
323238
### Runes support
324239

325-
**_This is an experimental feature. It may be changed or removed in minor versions without notice._**
326-
327240
If you install Svelte v5 the parser will be able to parse runes, and will also be able to parse `*.js` and `*.ts` files.
328241
If you don't want to use Runes, you may need to configure. Please read [parserOptions.svelteFeatures](#parseroptionssveltefeatures) for more details.
329242

@@ -369,38 +282,6 @@ export default [
369282
];
370283
```
371284

372-
For example in `.eslintrc.*`:
373-
374-
```jsonc
375-
{
376-
"overrides": [
377-
{
378-
"files": ["*.svelte"],
379-
"parser": "svelte-eslint-parser",
380-
"parserOptions": {
381-
"parser": "...",
382-
/* ... */
383-
},
384-
},
385-
{
386-
"files": ["*.svelte.js"],
387-
"parser": "svelte-eslint-parser",
388-
"parserOptions": {
389-
/* ... */
390-
},
391-
},
392-
{
393-
"files": ["*.svelte.ts"],
394-
"parser": "svelte-eslint-parser",
395-
"parserOptions": {
396-
"parser": "...(ts parser)...",
397-
/* ... */
398-
},
399-
},
400-
],
401-
}
402-
```
403-
404285
## :computer: Editor Integrations
405286

406287
### Visual Studio Code
@@ -439,4 +320,3 @@ See the [LICENSE](LICENSE) file for license rights and limitations (MIT).
439320

440321
[Svelte]: https://svelte.dev/
441322
[ESLint]: https://eslint.org/
442-
[eslint-plugin-svelte3]: https://github.com/sveltejs/eslint-plugin-svelte3

benchmark/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
/* eslint-disable no-console -- ignore */
33
import * as Benchmark from "benchmark";
44
import fs from "fs";
5-
import { parseForESLint } from "../src/index";
6-
import { parseForESLint as parseOld } from "../node_modules/svelte-eslint-parser";
5+
import { parseForESLint } from "../src/index.js";
6+
import { parseForESLint as parseOld } from "../node_modules/svelte-eslint-parser/lib/index.js";
77

88
const contents = `${fs.readFileSync(
99
require.resolve("../explorer-v2/src/lib/RulesSettings.svelte"),

explorer-v2/build-system/shim/module.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1+
// eslint-disable-next-line n/no-extraneous-import -- shim
2+
import * as estree from 'espree';
13
export function createRequire() {
2-
return null;
4+
function req(mod) {
5+
if (mod === 'espree') {
6+
return estree;
7+
}
8+
throw new Error(`Cannot find module '${mod}'`);
9+
}
10+
11+
req.cache = {};
12+
return req;
313
}
414
export default {
515
createRequire

package.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"engines": {
1515
"node": "^18.20.4 || ^20.18.0 || >=22.10.0"
1616
},
17-
"type": "commonjs",
17+
"type": "module",
1818
"main": "lib/index.js",
1919
"files": [
2020
"lib"
@@ -30,8 +30,8 @@
3030
"build": "pnpm run build:meta && pnpm run build:tsc",
3131
"build:meta": "pnpm run ts ./tools/update-meta.ts",
3232
"build:tsc": "tsc --project ./tsconfig.build.json",
33-
"clean": "rimraf .nyc_output lib coverage",
34-
"cover": "nyc --reporter=lcov pnpm run test",
33+
"clean": "rimraf lib coverage",
34+
"cover": "c8 --reporter=lcov pnpm run test",
3535
"debug": "pnpm run mocha \"tests/src/**/*.ts\" --reporter dot --timeout 60000",
3636
"eslint-fix": "pnpm run lint --fix",
3737
"lint": "eslint .",
@@ -41,7 +41,7 @@
4141
"preversion": "pnpm run lint && pnpm run test",
4242
"release": "changeset publish",
4343
"test": "pnpm run mocha \"tests/src/**/*.ts\" --reporter dot --timeout 60000",
44-
"ts": "node -r esbuild-register",
44+
"ts": "node --import tsx/esm",
4545
"update-fixtures": "git add package.json && pnpm i -D svelte@4 && git checkout package.json && pnpm run run-update-fixtures && pnpm i && pnpm run run-update-fixtures",
4646
"run-update-fixtures": "pnpm run ts ./tools/update-fixtures.ts",
4747
"version:ci": "env-cmd -e version-ci pnpm run build:meta && changeset version"
@@ -79,10 +79,10 @@
7979
"@typescript-eslint/parser": "~8.16.0",
8080
"@typescript-eslint/types": "~8.16.0",
8181
"benchmark": "^2.1.4",
82+
"c8": "^10.1.2",
8283
"chai": "^5.0.0",
8384
"env-cmd": "^10.1.0",
8485
"esbuild": "^0.24.0",
85-
"esbuild-register": "^3.6.0",
8686
"eslint": "~9.16.0",
8787
"eslint-config-prettier": "^9.1.0",
8888
"eslint-plugin-eslint-comments": "^3.2.0",
@@ -100,14 +100,14 @@
100100
"magic-string": "^0.30.14",
101101
"mocha": "^10.8.2",
102102
"mocha-chai-jest-snapshot": "^1.1.6",
103-
"nyc": "^17.1.0",
104103
"prettier": "~3.4.1",
105104
"prettier-plugin-pkg": "^0.18.1",
106105
"prettier-plugin-svelte": "^3.3.2",
107106
"rimraf": "^6.0.1",
108107
"semver": "^7.6.3",
109108
"svelte": "^5.3.1",
110109
"svelte2tsx": "^0.7.28",
110+
"tsx": "^4.19.2",
111111
"typescript": "~5.7.2",
112112
"typescript-eslint": "^8.16.0",
113113
"typescript-eslint-parser-for-extra-files": "^0.7.0"

src/ast/base.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Locations } from "./common";
1+
import type { Locations } from "./common.js";
22

33
// internals
44
export interface BaseNode extends Locations {

src/ast/common.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { BaseNode } from "./base";
1+
import type { BaseNode } from "./base.js";
22

33
export interface Position {
44
/** >= 1 */

src/ast/html.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type ESTree from "estree";
22
import type { TSESTree } from "@typescript-eslint/types";
3-
import type { BaseNode } from "./base";
4-
import type { Token, Comment } from "./common";
3+
import type { BaseNode } from "./base.js";
4+
import type { Token, Comment } from "./common.js";
55

66
export type SvelteHTMLNode =
77
| SvelteProgram

src/ast/index.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import type { SvelteHTMLNode } from "./html";
2-
import type { SvelteScriptNode } from "./script";
1+
import type { SvelteHTMLNode } from "./html.js";
2+
import type { SvelteScriptNode } from "./script.js";
33

4-
export * from "./common";
5-
export * from "./html";
6-
export * from "./script";
4+
export * from "./common.js";
5+
export * from "./html.js";
6+
export * from "./script.js";
77

88
export type SvelteNode = SvelteHTMLNode | SvelteScriptNode;

src/ast/script.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type ESTree from "estree";
2-
import type { BaseNode } from "./base";
2+
import type { BaseNode } from "./base.js";
33

44
export type SvelteScriptNode = SvelteReactiveStatement;
55

src/context/fix-locations.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type * as ESTree from "estree";
2-
import type { Comment, Locations, Token } from "../ast";
3-
import type { Context } from ".";
4-
import { traverseNodes } from "../traverse";
2+
import type { Comment, Locations, Token } from "../ast/index.js";
3+
import type { Context } from "./index.js";
4+
import { traverseNodes } from "../traverse.js";
55

66
/** Fix locations */
77
export function fixLocations(

0 commit comments

Comments
 (0)