Skip to content

Commit

Permalink
feat(ast-spec): bring Node objects in line with ESTree (#3771)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelDeBoey committed Sep 22, 2021
1 parent cbbf22a commit f893d1b
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 13 deletions.
3 changes: 3 additions & 0 deletions packages/ast-spec/src/base/NodeOrTokenData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import type { SourceLocation } from './SourceLocation';
export interface NodeOrTokenData {
/**
* The source location information of the node.
*
* The loc property is defined as nullable by ESTree, but ESLint requires this property.
*
* @see {SourceLocation}
*/
loc: SourceLocation;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface LineAndColumnData {
export interface Position {
/**
* Line number (1-indexed)
*/
Expand Down
6 changes: 3 additions & 3 deletions packages/ast-spec/src/base/SourceLocation.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { LineAndColumnData } from './LineAndColumnData';
import type { Position } from './Position';

export interface SourceLocation {
/**
* The position of the first character of the parsed source region
*/
start: LineAndColumnData;
start: Position;
/**
* The position of the first character after the parsed source region
*/
end: LineAndColumnData;
end: Position;
}
2 changes: 1 addition & 1 deletion packages/ast-spec/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export * from './base/Accessibility';
export * from './base/BaseNode'; // this is exported so that the `types` package can merge the decl and add the `parent` property
export * from './base/LineAndColumnData';
export * from './base/OptionalRangeAndLoc';
export * from './base/Position';
export * from './base/Range';
export * from './base/SourceLocation';

Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/util/getFunctionHeadLoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function getFunctionHeadLoc(
node: FunctionNode,
sourceCode: TSESLint.SourceCode,
): TSESTree.SourceLocation {
function getLocStart(): TSESTree.LineAndColumnData {
function getLocStart(): TSESTree.Position {
if (node.parent && node.parent.type === AST_NODE_TYPES.MethodDefinition) {
// return the start location for class method

Expand All @@ -58,7 +58,7 @@ export function getFunctionHeadLoc(
return node.loc.start;
}

function getLocEnd(): TSESTree.LineAndColumnData {
function getLocEnd(): TSESTree.Position {
if (node.type === AST_NODE_TYPES.ArrowFunctionExpression) {
// find the end location for arrow function expression
return sourceCode.getTokenBefore(
Expand Down
4 changes: 2 additions & 2 deletions packages/experimental-utils/src/ts-eslint/Rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,13 @@ interface ReportDescriptorNodeOptionalLoc {
*/
readonly loc?:
| Readonly<TSESTree.SourceLocation>
| Readonly<TSESTree.LineAndColumnData>;
| Readonly<TSESTree.Position>;
}
interface ReportDescriptorLocOnly {
/**
* An override of the location of the report
*/
loc: Readonly<TSESTree.SourceLocation> | Readonly<TSESTree.LineAndColumnData>;
loc: Readonly<TSESTree.SourceLocation> | Readonly<TSESTree.Position>;
}
type ReportDescriptor<TMessageIds extends string> =
ReportDescriptorWithSuggestion<TMessageIds> &
Expand Down
4 changes: 2 additions & 2 deletions packages/experimental-utils/src/ts-eslint/SourceCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ declare class SourceCodeBase extends TokenStore {
* @param loc A line/column location
* @returns The range index of the location in the file.
*/
getIndexFromLoc(location: TSESTree.LineAndColumnData): number;
getIndexFromLoc(location: TSESTree.Position): number;
/**
* Gets the entire source text split into an array of lines.
* @returns The source text as an array of lines.
Expand All @@ -261,7 +261,7 @@ declare class SourceCodeBase extends TokenStore {
* @param index The index of a character in a file
* @returns A {line, column} location object with a 0-indexed column
*/
getLocFromIndex(index: number): TSESTree.LineAndColumnData;
getLocFromIndex(index: number): TSESTree.Position;
/**
* Gets the deepest node containing a range index.
* @param index Range index of the desired node.
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript-estree/src/node-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export function getBinaryExpressionType<T extends ts.SyntaxKind>(
export function getLineAndCharacterFor(
pos: number,
ast: ts.SourceFile,
): TSESTree.LineAndColumnData {
): TSESTree.Position {
const loc = ast.getLineAndCharacterOfPosition(pos);
return {
line: loc.line + 1,
Expand Down
2 changes: 1 addition & 1 deletion packages/typescript-estree/tests/ast-alignment/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export function parse(
);
}
} catch (error: any) {
const loc = error.loc as TSESTree.LineAndColumnData | undefined;
const loc = error.loc as TSESTree.Position | undefined;
if (loc) {
error.codeFrame = codeFrameColumns(
text,
Expand Down

0 comments on commit f893d1b

Please sign in to comment.