From d5e8aa4b1e5b1c09d364717e6f2d5987ad5a0404 Mon Sep 17 00:00:00 2001 From: Dale Bustad Date: Sun, 17 Jul 2016 01:13:12 -0700 Subject: [PATCH] If supplied, attach filename property to comment node loc. --- src/parser/comments.js | 1 + .../categorized/filename-specified/actual.js | 1 + .../filename-specified/expected.json | 88 ++++++++++++++----- 3 files changed, 66 insertions(+), 24 deletions(-) diff --git a/src/parser/comments.js b/src/parser/comments.js index d0c1cc3515..648c5601f7 100644 --- a/src/parser/comments.js +++ b/src/parser/comments.js @@ -33,6 +33,7 @@ function last(stack) { const pp = Parser.prototype; pp.addComment = function (comment) { + if (this.filename) comment.loc.filename = this.filename; this.state.trailingComments.push(comment); this.state.leadingComments.push(comment); }; diff --git a/test/fixtures/core/categorized/filename-specified/actual.js b/test/fixtures/core/categorized/filename-specified/actual.js index 76e45db756..5ab71c2c75 100644 --- a/test/fixtures/core/categorized/filename-specified/actual.js +++ b/test/fixtures/core/categorized/filename-specified/actual.js @@ -1 +1,2 @@ +// This comment should have a filename prop attached to loc var node = "shouldHaveFilenameLocProp"; diff --git a/test/fixtures/core/categorized/filename-specified/expected.json b/test/fixtures/core/categorized/filename-specified/expected.json index 9b5ba11fa1..f48495688b 100644 --- a/test/fixtures/core/categorized/filename-specified/expected.json +++ b/test/fixtures/core/categorized/filename-specified/expected.json @@ -1,14 +1,14 @@ { "type": "File", "start": 0, - "end": 39, + "end": 99, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 1, + "line": 2, "column": 39 }, "filename": "path/to/input-file.js" @@ -16,14 +16,14 @@ "program": { "type": "Program", "start": 0, - "end": 39, + "end": 99, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 1, + "line": 2, "column": 39 }, "filename": "path/to/input-file.js" @@ -32,15 +32,15 @@ "body": [ { "type": "VariableDeclaration", - "start": 0, - "end": 39, + "start": 60, + "end": 99, "loc": { "start": { - "line": 1, + "line": 2, "column": 0 }, "end": { - "line": 1, + "line": 2, "column": 39 }, "filename": "path/to/input-file.js" @@ -48,47 +48,48 @@ "declarations": [ { "type": "VariableDeclarator", - "start": 4, - "end": 38, + "start": 64, + "end": 98, "loc": { "start": { - "line": 1, + "line": 2, "column": 4 }, "end": { - "line": 1, + "line": 2, "column": 38 }, "filename": "path/to/input-file.js" }, "id": { "type": "Identifier", - "start": 4, - "end": 8, + "start": 64, + "end": 68, "loc": { "start": { - "line": 1, + "line": 2, "column": 4 }, "end": { - "line": 1, + "line": 2, "column": 8 }, "filename": "path/to/input-file.js" }, - "name": "node" + "name": "node", + "leadingComments": null }, "init": { "type": "StringLiteral", - "start": 11, - "end": 38, + "start": 71, + "end": 98, "loc": { "start": { - "line": 1, + "line": 2, "column": 11 }, "end": { - "line": 1, + "line": 2, "column": 38 }, "filename": "path/to/input-file.js" @@ -98,12 +99,51 @@ "raw": "\"shouldHaveFilenameLocProp\"" }, "value": "shouldHaveFilenameLocProp" - } + }, + "leadingComments": null } ], - "kind": "var" + "kind": "var", + "leadingComments": [ + { + "type": "CommentLine", + "value": " This comment should have a filename prop attached to loc", + "start": 0, + "end": 59, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 59 + }, + "filename": "path/to/input-file.js" + } + } + ] } ], "directives": [] - } + }, + "comments": [ + { + "type": "CommentLine", + "value": " This comment should have a filename prop attached to loc", + "start": 0, + "end": 59, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 59 + }, + "filename": "path/to/input-file.js" + } + } + ] } \ No newline at end of file