Skip to content

Off-by-one columns in babel/code-frame #17316

Description

@RReverser

💻

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

import {parse} from '@babel/parser';
import {codeFrameColumns} from '@babel/code-frame';

var code = 'x = "foobar";';
var ast = parse(code);
console.error(codeFrameColumns(code, ast.program.body[0].expression.right.loc));

Configuration file name

No response

Configuration

No response

Current and expected behavior

Current output:

> 1 | x = "foobar";
    |    ^^^^^^^^

Expected output:

> 1 | x = "foobar";
    |     ^^^^^^^^

The column is clearly interpreted as 1-based even though Babel AST returns them as 0-based everywhere else, and codeFrameColumns accepts a NodeLocation which suggests it should use a compatible location format:

loc: NodeLocation,

Environment

npx envinfo --preset babel

System:
OS: Windows 11 10.0.26100
Binaries:
Node: 24.0.0 - C:\Program Files\nodejs\node.EXE
npm: 11.3.0 - C:\Program Files\nodejs\npm.CMD
pnpm: 9.15.5 - ~\AppData\Local\pnpm\pnpm.CMD
npmPackages:
@babel/code-frame: ^7.27.1 => 7.26.2
@babel/core: ^7.27.1 => 7.26.10
@babel/generator: ^7.27.1 => 7.26.10
@babel/parser: ^7.27.1 => 7.26.10
@babel/preset-env: ^7.27.1 => 7.26.9
@babel/types: ^7.27.1 => 7.26.10

Possible solution

No response

Additional context

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions