Skip to content

Commit

Permalink
@types/eslint allow non-ESTree AST nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Logicer16 committed Jan 17, 2024
1 parent eb3890d commit a4e5203
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 97 deletions.
4 changes: 2 additions & 2 deletions types/eslint-scope/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class Scope implements eslint.Scope.Scope {
upper: Scope | null;
childScopes: Scope[];
variableScope: Scope;
block: estree.Node;
block: estree.BaseNode;
variables: Variable[];
set: Map<string, Variable>;
references: Reference[];
Expand All @@ -47,7 +47,7 @@ export class Reference implements eslint.Scope.Reference {
identifier: estree.Identifier;
from: Scope;
resolved: Variable | null;
writeExpr: estree.Node | null;
writeExpr: estree.BaseNode | null;
init: boolean;

isWrite(): boolean;
Expand Down
29 changes: 17 additions & 12 deletions types/eslint/eslint-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Comment, PrivateIdentifier, PropertyDefinition, StaticBlock, WhileState

const SOURCE = `var foo = bar;`;

const AST: AST.Program = {
const AST: AST.ValidatedProgram = {
type: "Program",
sourceType: "module",
body: [],
Expand Down Expand Up @@ -271,7 +271,7 @@ sourceCode.markVariableAsUsed("foo", AST);

sourceCode.getDeclaredVariables(AST); // $ExpectType Variable[]

sourceCode.getAncestors(AST); // $ExpectType Node[]
sourceCode.getAncestors(AST); // $ExpectType BaseNode[]

// #endregion

Expand Down Expand Up @@ -683,25 +683,30 @@ linter.defineRules({

linter.getRules();

linter.defineParser("custom-parser", {
const parser: Linter.ParserModule = {
name: "foo",
version: "1.2.3",
meta: {
name: "foo",
version: "1.2.3",
},
parse: (src, opts) => AST,
});
linter.defineParser("custom-parser", {
name: "foo",
version: "1.2.3",
meta: {
name: "foo",
version: "1.2.3",
parse: (src, opts) => {
return {
...AST,
comments: opts.comment ? AST.comments : undefined,
tokens: opts.tokens ? AST.tokens : undefined,
loc: opts.loc ? AST.loc : undefined,
range: opts.range ? AST.range : undefined,
};
},
};
linter.defineParser("custom-parser", parser);
linter.defineParser("custom-parser", {
...parser,
parse: undefined,
parseForESLint(src, opts) {
return {
ast: AST,
ast: parser.parse(src, opts),
visitorKeys: {},
parserServices: {},
scopeManager,
Expand Down

0 comments on commit a4e5203

Please sign in to comment.