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

Babel: pass sourceFileName to the parser to populate the location filename #1407

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions lib/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function parse(source: string, options?: Partial<Options>) {
tolerant: util.getOption(options, "tolerant", true),
ecmaVersion: 6,
sourceType: util.getOption(options, "sourceType", "module"),
sourceFileName: util.getOption(options, "sourceFileName", null),
});

// Use ast.tokens if possible, and otherwise fall back to the Esprima
Expand Down
2 changes: 2 additions & 0 deletions parsers/_babel_options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getOption } from "../lib/util";
export type Overrides = Partial<{
sourceType: ParserOptions["sourceType"];
strictMode: ParserOptions["strictMode"];
sourceFileName: string;
}>;

export default function getBabelOptions(
Expand All @@ -16,6 +17,7 @@ export default function getBabelOptions(
return {
sourceType: getOption(options, "sourceType", "module"),
strictMode: getOption(options, "strictMode", false),
sourceFilename: getOption(options, "sourceFileName", undefined),
allowImportExportEverywhere: true,
allowReturnOutsideFunction: true,
startLine: 1,
Expand Down
11 changes: 11 additions & 0 deletions test/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,4 +497,15 @@ describe("Babel", function () {

assert.strictEqual(recast.prettyPrint(ast).code, code.join(eol));
});

it("adds the sourceFilename to location", function () {
const code = "const a = 1;";
const filename = "testfile.js";
const ast = recast.parse(code, {
...parseOptions,
sourceFileName: filename,
});

assert.strictEqual(ast.program.body[0].loc.filename, filename);
});
});
Loading