Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: crash in scope analysis when using espree #318

Merged
merged 4 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chilly-numbers-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro-eslint-parser": patch
---

fix: crash in scope analysis when using espree
29 changes: 5 additions & 24 deletions .github/workflows/NodeCI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,15 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20.x, 22.x]
node-version: [18.x, 20.x, 22.x]
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: Install Packages
run: npm install --legacy-peer-deps
- name: Test
run: npm test
test-for-old-node:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x]
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: Install old versions
run: |+
npm i -D @typescript-eslint/parser@5 @typescript-eslint/eslint-plugin@5 --legacy-peer-deps
npx rimraf node_modules
- name: Install Packages
run: npm install --legacy-peer-deps
run: npm install -f
- name: Test
run: npm test
test-for-eslint-v7:
Expand All @@ -68,10 +49,10 @@ jobs:
node-version: ${{ matrix.node-version }}
- name: Install eslint v7
run: |+
npm i -D eslint@7 @typescript-eslint/parser@5 @typescript-eslint/eslint-plugin@5 --legacy-peer-deps
npm i -D eslint@7 @typescript-eslint/parser@5 @typescript-eslint/eslint-plugin@5 -f
npx rimraf node_modules
- name: Install Packages
run: npm install --legacy-peer-deps
run: npm install -f
- name: Test
run: npm test
test-and-coverage:
Expand All @@ -84,6 +65,6 @@ jobs:
- name: Test
run: npm run cover
- name: Coveralls GitHub Action
uses: coverallsapp/github-action@3dfc5567390f6fa9267c0ee9c251e4c8c3f18949 # v2.2.3
uses: coverallsapp/github-action@v2.2.3
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"astrojs-compiler-sync": "^1.0.0",
"debug": "^4.3.4",
"entities": "^4.5.0",
"eslint-scope": "^8.0.1",
"eslint-visitor-keys": "^4.0.0",
"espree": "^10.0.0",
"globby": "^11.1.0",
Expand Down
45 changes: 40 additions & 5 deletions src/parser/script.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import type { Context } from "../context";
import { debug } from "../debug";
import type { ParserOptionsContext } from "../context/parser-options";
import type {
ParserOptions,
ParserOptionsContext,
} from "../context/parser-options";
import type { ESLintExtendedProgram } from "../types";
import { tsPatch } from "./ts-patch";
import { isEnhancedParserObject } from "../context/resolve-parser/parser-object";
import { analyze } from "@typescript-eslint/scope-manager";
import type { ScopeManager } from "@typescript-eslint/scope-manager";
import { analyze as analyzeForTypeScript } from "@typescript-eslint/scope-manager";
import { analyze as analyzeForEcmaScript } from "eslint-scope";
import { KEYS } from "../visitor-keys";
import { getKeys } from "../traverse";
/**
* Parse for script
*/
Expand All @@ -17,17 +24,45 @@ export function parseScript(

const parserOptions = parserOptionsCtx.parserOptions;
if (!result.scopeManager && parserOptions.eslintScopeManager) {
result.scopeManager = analyze(result.ast, {
ecmaVersion: 1e8,
result.scopeManager = analyzeScope(result, parserOptions);
}

return result;
}

/**
* Analyze scope
*/
function analyzeScope(
result: ESLintExtendedProgram,
parserOptions: ParserOptions,
): ScopeManager {
try {
return analyzeForTypeScript(result.ast, {
globalReturn: parserOptions.ecmaFeatures?.globalReturn,
jsxPragma: parserOptions.jsxPragma,
jsxFragmentName: parserOptions.jsxFragmentName,
lib: parserOptions.lib,
sourceType: parserOptions.sourceType,
});
} catch {
// ignore
}
const ecmaFeatures = parserOptions.ecmaFeatures || {};

return result;
return analyzeForEcmaScript(result.ast, {
ignoreEval: true,
nodejsScope: ecmaFeatures.globalReturn,
impliedStrict: ecmaFeatures.impliedStrict as never,
ecmaVersion: 1e8,
sourceType:
parserOptions.sourceType === "commonjs"
? "script"
: parserOptions.sourceType || "script",
// @ts-expect-error -- Type bug?
childVisitorKeys: result.visitorKeys || KEYS,
fallback: getKeys,
}) as ScopeManager;
}

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/parser/ast/js-jsx01-requirements.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"scope": {
"@typescript-eslint/parser" : ">=7"
}
}
181 changes: 180 additions & 1 deletion tests/fixtures/parser/ast/js-jsx01-scope-output.json
Original file line number Diff line number Diff line change
@@ -1 +1,180 @@
null
{
"type": "global",
"variables": [],
"references": [],
"childScopes": [
{
"type": "module",
"variables": [
{
"name": "MyComponent",
"identifiers": [
{
"type": "Identifier",
"name": "MyComponent",
"range": [
11,
22
],
"loc": {
"start": {
"line": 2,
"column": 7
},
"end": {
"line": 2,
"column": 18
}
}
}
],
"defs": [
{
"type": "ImportBinding",
"name": {
"type": "Identifier",
"name": "MyComponent",
"range": [
11,
22
],
"loc": {
"start": {
"line": 2,
"column": 7
},
"end": {
"line": 2,
"column": 18
}
}
},
"node": {
"type": "ImportDefaultSpecifier",
"local": {
"type": "Identifier",
"name": "MyComponent",
"range": [
11,
22
],
"loc": {
"start": {
"line": 2,
"column": 7
},
"end": {
"line": 2,
"column": 18
}
}
},
"range": [
11,
22
],
"loc": {
"start": {
"line": 2,
"column": 7
},
"end": {
"line": 2,
"column": 18
}
}
}
}
],
"references": [
{
"identifier": {
"type": "JSXIdentifier",
"name": "MyComponent",
"range": [
57,
68
],
"loc": {
"start": {
"line": 5,
"column": 1
},
"end": {
"line": 5,
"column": 12
}
}
},
"from": "module",
"init": null,
"resolved": {
"type": "Identifier",
"name": "MyComponent",
"range": [
11,
22
],
"loc": {
"start": {
"line": 2,
"column": 7
},
"end": {
"line": 2,
"column": 18
}
}
}
}
]
}
],
"references": [
{
"identifier": {
"type": "JSXIdentifier",
"name": "MyComponent",
"range": [
57,
68
],
"loc": {
"start": {
"line": 5,
"column": 1
},
"end": {
"line": 5,
"column": 12
}
}
},
"from": "module",
"init": null,
"resolved": {
"type": "Identifier",
"name": "MyComponent",
"range": [
11,
22
],
"loc": {
"start": {
"line": 2,
"column": 7
},
"end": {
"line": 2,
"column": 18
}
}
}
}
],
"childScopes": [],
"through": []
}
],
"through": []
}
19 changes: 19 additions & 0 deletions tests/fixtures/parser/ast/js-test01-input.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
const sidebarSections = [{ text: "foo" }]
---

<nav aria-labelledby="grid-left">
<ul class="nav-groups">
{sidebarSections.map((section) => (
<li>
<div class="nav-group">{section.text}</div>
</li>
))}
</ul>
</nav>

<style>
.nav-groups > li + li {
margin-top: 2rem;
}
</style>
Loading
Loading