Skip to content

Commit

Permalink
feat!: change the parser to an ESM-only package (#599)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi authored Dec 2, 2024
1 parent 028d840 commit cefd17a
Show file tree
Hide file tree
Showing 62 changed files with 371 additions and 469 deletions.
5 changes: 5 additions & 0 deletions .changeset/quick-beers-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte-eslint-parser": major
---

feat!: change the parser to an ESM-only package
160 changes: 20 additions & 140 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ You can check it on [Online DEMO](https://sveltejs.github.io/svelte-eslint-parse
> Please refer to the README for the version you are using.\
> For example, <https://github.com/sveltejs/svelte-eslint-parser/blob/v0.43.0/README.md>
**_Note that this parser has experimental support for Svelte v5, but may break with new versions of Svelte v5._**

[![NPM license](https://img.shields.io/npm/l/svelte-eslint-parser.svg)](https://www.npmjs.com/package/svelte-eslint-parser)
[![NPM version](https://img.shields.io/npm/v/svelte-eslint-parser.svg)](https://www.npmjs.com/package/svelte-eslint-parser)
[![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)
Expand Down Expand Up @@ -41,12 +39,6 @@ It provides many unique check rules by using the template AST.
ESLint plugin for internationalization (i18n) with Svelte.
It provides rules to help internationalization your application created with Svelte.

## ❗ Attention

The [svelte-eslint-parser] can not be used with the [eslint-plugin-svelte3].

[svelte-eslint-parser]: https://github.com/sveltejs/svelte-eslint-parser

## 💿 Installation

```bash
Expand Down Expand Up @@ -74,20 +66,6 @@ export default [
];
```

### ESLint Config (`.eslintrc.*`)

```json
{
"extends": "eslint:recommended",
"overrides": [
{
"files": ["*.svelte"],
"parser": "svelte-eslint-parser"
}
]
}
```

### CLI

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

```json
{
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2021,
"ecmaFeatures": {
"globalReturn": false,
"impliedStrict": false,
"jsx": false
}
}
}
```js
import svelteParser from "svelte-eslint-parser";
export default [
// ...
{
files: ["**/*.svelte", "*.svelte"],
languageOptions: {
parser: svelteParser,
parserOptions: {
sourceType: "module",
ecmaVersion: 2021,
ecmaFeatures: {
globalReturn: false,
impliedStrict: false,
jsx: false,
},
},
},
},
];
```

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

For example in `.eslintrc.*`:

```json
{
"parser": "svelte-eslint-parser",
"parserOptions": {
"parser": "@typescript-eslint/parser"
}
}
```

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.

For example in `eslint.config.js`:
Expand Down Expand Up @@ -183,32 +158,6 @@ export default [
];
```

For example in `.eslintrc.*`:

```js
module.exports = {
// ...
parser: "@typescript-eslint/parser",
parserOptions: {
// ...
project: "path/to/your/tsconfig.json",
extraFileExtensions: [".svelte"], // This is a required setting in `@typescript-eslint/parser` v4.24.0.
},
overrides: [
{
files: ["*.svelte"],
parser: "svelte-eslint-parser",
// Parse the `<script>` in `.svelte` as TypeScript by adding the following configuration.
parserOptions: {
parser: "@typescript-eslint/parser",
},
},
// ...
],
// ...
};
```

#### Multiple parsers

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

For example in `.eslintrc.*`:

```json
{
"parser": "svelte-eslint-parser",
"parserOptions": {
"parser": {
"ts": "@typescript-eslint/parser",
"js": "espree",
"typescript": "@typescript-eslint/parser"
}
}
}
```

### parserOptions.svelteConfig

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

For example in `.eslintrc.*`:

```jsonc
{
"parser": "svelte-eslint-parser",
"parserOptions": {
"svelteFeatures": {
/* It may be changed or removed in minor versions without notice. */
// This option is for Svelte 5. The default value is `true`.
// If `false`, ESLint will not recognize rune symbols.
// If not configured this option, The parser will try to read the option from `compilerOptions.runes` from `svelte.config.js`.
// If `parserOptions.svelteConfig` is not specified and the file cannot be parsed by static analysis, it will behave as `true`.
"runes": true,
},
},
}
```

### Runes support

**_This is an experimental feature. It may be changed or removed in minor versions without notice._**

If you install Svelte v5 the parser will be able to parse runes, and will also be able to parse `*.js` and `*.ts` files.
If you don't want to use Runes, you may need to configure. Please read [parserOptions.svelteFeatures](#parseroptionssveltefeatures) for more details.

Expand Down Expand Up @@ -369,38 +282,6 @@ export default [
];
```

For example in `.eslintrc.*`:

```jsonc
{
"overrides": [
{
"files": ["*.svelte"],
"parser": "svelte-eslint-parser",
"parserOptions": {
"parser": "...",
/* ... */
},
},
{
"files": ["*.svelte.js"],
"parser": "svelte-eslint-parser",
"parserOptions": {
/* ... */
},
},
{
"files": ["*.svelte.ts"],
"parser": "svelte-eslint-parser",
"parserOptions": {
"parser": "...(ts parser)...",
/* ... */
},
},
],
}
```

## :computer: Editor Integrations

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

[Svelte]: https://svelte.dev/
[ESLint]: https://eslint.org/
[eslint-plugin-svelte3]: https://github.com/sveltejs/eslint-plugin-svelte3
4 changes: 2 additions & 2 deletions benchmark/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
/* eslint-disable no-console -- ignore */
import * as Benchmark from "benchmark";
import fs from "fs";
import { parseForESLint } from "../src/index";
import { parseForESLint as parseOld } from "../node_modules/svelte-eslint-parser";
import { parseForESLint } from "../src/index.js";
import { parseForESLint as parseOld } from "../node_modules/svelte-eslint-parser/lib/index.js";

const contents = `${fs.readFileSync(
require.resolve("../explorer-v2/src/lib/RulesSettings.svelte"),
Expand Down
12 changes: 11 additions & 1 deletion explorer-v2/build-system/shim/module.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
// eslint-disable-next-line n/no-extraneous-import -- shim
import * as estree from 'espree';
export function createRequire() {
return null;
function req(mod) {
if (mod === 'espree') {
return estree;
}
throw new Error(`Cannot find module '${mod}'`);
}

req.cache = {};
return req;
}
export default {
createRequire
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"engines": {
"node": "^18.20.4 || ^20.18.0 || >=22.10.0"
},
"type": "commonjs",
"type": "module",
"main": "lib/index.js",
"files": [
"lib"
Expand All @@ -30,8 +30,8 @@
"build": "pnpm run build:meta && pnpm run build:tsc",
"build:meta": "pnpm run ts ./tools/update-meta.ts",
"build:tsc": "tsc --project ./tsconfig.build.json",
"clean": "rimraf .nyc_output lib coverage",
"cover": "nyc --reporter=lcov pnpm run test",
"clean": "rimraf lib coverage",
"cover": "c8 --reporter=lcov pnpm run test",
"debug": "pnpm run mocha \"tests/src/**/*.ts\" --reporter dot --timeout 60000",
"eslint-fix": "pnpm run lint --fix",
"lint": "eslint .",
Expand All @@ -41,7 +41,7 @@
"preversion": "pnpm run lint && pnpm run test",
"release": "changeset publish",
"test": "pnpm run mocha \"tests/src/**/*.ts\" --reporter dot --timeout 60000",
"ts": "node -r esbuild-register",
"ts": "node --import tsx/esm",
"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",
"run-update-fixtures": "pnpm run ts ./tools/update-fixtures.ts",
"version:ci": "env-cmd -e version-ci pnpm run build:meta && changeset version"
Expand Down Expand Up @@ -79,10 +79,10 @@
"@typescript-eslint/parser": "~8.16.0",
"@typescript-eslint/types": "~8.16.0",
"benchmark": "^2.1.4",
"c8": "^10.1.2",
"chai": "^5.0.0",
"env-cmd": "^10.1.0",
"esbuild": "^0.24.0",
"esbuild-register": "^3.6.0",
"eslint": "~9.16.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-eslint-comments": "^3.2.0",
Expand All @@ -100,14 +100,14 @@
"magic-string": "^0.30.14",
"mocha": "^10.8.2",
"mocha-chai-jest-snapshot": "^1.1.6",
"nyc": "^17.1.0",
"prettier": "~3.4.1",
"prettier-plugin-pkg": "^0.18.1",
"prettier-plugin-svelte": "^3.3.2",
"rimraf": "^6.0.1",
"semver": "^7.6.3",
"svelte": "^5.3.1",
"svelte2tsx": "^0.7.28",
"tsx": "^4.19.2",
"typescript": "~5.7.2",
"typescript-eslint": "^8.16.0",
"typescript-eslint-parser-for-extra-files": "^0.7.0"
Expand Down
2 changes: 1 addition & 1 deletion src/ast/base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Locations } from "./common";
import type { Locations } from "./common.js";

// internals
export interface BaseNode extends Locations {
Expand Down
2 changes: 1 addition & 1 deletion src/ast/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { BaseNode } from "./base";
import type { BaseNode } from "./base.js";

export interface Position {
/** >= 1 */
Expand Down
4 changes: 2 additions & 2 deletions src/ast/html.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type ESTree from "estree";
import type { TSESTree } from "@typescript-eslint/types";
import type { BaseNode } from "./base";
import type { Token, Comment } from "./common";
import type { BaseNode } from "./base.js";
import type { Token, Comment } from "./common.js";

export type SvelteHTMLNode =
| SvelteProgram
Expand Down
10 changes: 5 additions & 5 deletions src/ast/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { SvelteHTMLNode } from "./html";
import type { SvelteScriptNode } from "./script";
import type { SvelteHTMLNode } from "./html.js";
import type { SvelteScriptNode } from "./script.js";

export * from "./common";
export * from "./html";
export * from "./script";
export * from "./common.js";
export * from "./html.js";
export * from "./script.js";

export type SvelteNode = SvelteHTMLNode | SvelteScriptNode;
2 changes: 1 addition & 1 deletion src/ast/script.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type ESTree from "estree";
import type { BaseNode } from "./base";
import type { BaseNode } from "./base.js";

export type SvelteScriptNode = SvelteReactiveStatement;

Expand Down
6 changes: 3 additions & 3 deletions src/context/fix-locations.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type * as ESTree from "estree";
import type { Comment, Locations, Token } from "../ast";
import type { Context } from ".";
import { traverseNodes } from "../traverse";
import type { Comment, Locations, Token } from "../ast/index.js";
import type { Context } from "./index.js";
import { traverseNodes } from "../traverse.js";

/** Fix locations */
export function fixLocations(
Expand Down
Loading

0 comments on commit cefd17a

Please sign in to comment.