From 2044025b83e6bb30569a808622298e86396006ec Mon Sep 17 00:00:00 2001 From: Reyad Attiyat Date: Tue, 11 Apr 2017 07:13:43 -0500 Subject: [PATCH] Breaking: Allow comment scanner to rescan tokens (fixes #216) Template strings and regex patterns need to be rescaned to not scan tokens as comments. Fix how comment tokens are removed. --- lib/ast-converter.js | 7 +- lib/convert-comments.js | 147 ++++ lib/node-utils.js | 35 +- parser.js | 100 +-- .../block-trailing-comment.result.js | 0 .../block-trailing-comment.src.js | 0 .../comment-within-condition.result.js | 0 .../comment-within-condition.src.js | 0 .../export-default-anonymous-class.result.js | 0 .../export-default-anonymous-class.src.js | 0 .../comments/jsx-block-comment.result.js | 732 ++++++++++++++++++ .../comments/jsx-block-comment.src.js | 7 + .../comments/jsx-tag-comments.result.js | 644 +++++++++++++++ .../fixtures/comments/jsx-tag-comments.src.js | 10 + .../line-comment-with-block-syntax.result.js | 40 + .../line-comment-with-block-syntax.src.js | 1 + .../mix-line-and-block-comments.result.js | 0 .../mix-line-and-block-comments.src.js | 0 .../comments/no-comment-regex.result.js | 195 +++++ .../fixtures/comments/no-comment-regex.src.js | 1 + .../comments/no-comment-template.result.js | 287 +++++++ .../comments/no-comment-template.src.js | 1 + .../surrounding-call-comments.result.js | 0 .../surrounding-call-comments.src.js | 0 .../surrounding-debugger-comments.result.js | 0 .../surrounding-debugger-comments.src.js | 0 .../surrounding-return-comments.result.js | 0 .../surrounding-return-comments.src.js | 0 .../surrounding-throw-comments.result.js | 0 .../surrounding-throw-comments.src.js | 0 .../surrounding-while-loop-comments.result.js | 0 .../surrounding-while-loop-comments.src.js | 0 ...-fallthrough-comment-in-function.result.js | 0 ...tch-fallthrough-comment-in-function.src.js | 0 .../switch-fallthrough-comment.result.js | 0 .../switch-fallthrough-comment.src.js | 0 ...h-no-default-comment-in-function.result.js | 0 ...itch-no-default-comment-in-function.src.js | 0 ...ault-comment-in-nested-functions.result.js | 0 ...default-comment-in-nested-functions.src.js | 0 .../switch-no-default-comment.result.js | 0 .../switch-no-default-comment.src.js | 0 .../comments/template-string-block.result.js | 414 ++++++++++ .../comments/template-string-block.src.js | 5 + tests/lib/{attach-comments.js => comments.js} | 12 +- 45 files changed, 2531 insertions(+), 107 deletions(-) create mode 100644 lib/convert-comments.js rename tests/fixtures/{attach-comments => comments}/block-trailing-comment.result.js (100%) rename tests/fixtures/{attach-comments => comments}/block-trailing-comment.src.js (100%) rename tests/fixtures/{attach-comments => comments}/comment-within-condition.result.js (100%) rename tests/fixtures/{attach-comments => comments}/comment-within-condition.src.js (100%) rename tests/fixtures/{attach-comments => comments}/export-default-anonymous-class.result.js (100%) rename tests/fixtures/{attach-comments => comments}/export-default-anonymous-class.src.js (100%) create mode 100644 tests/fixtures/comments/jsx-block-comment.result.js create mode 100644 tests/fixtures/comments/jsx-block-comment.src.js create mode 100644 tests/fixtures/comments/jsx-tag-comments.result.js create mode 100644 tests/fixtures/comments/jsx-tag-comments.src.js create mode 100644 tests/fixtures/comments/line-comment-with-block-syntax.result.js create mode 100644 tests/fixtures/comments/line-comment-with-block-syntax.src.js rename tests/fixtures/{attach-comments => comments}/mix-line-and-block-comments.result.js (100%) rename tests/fixtures/{attach-comments => comments}/mix-line-and-block-comments.src.js (100%) create mode 100644 tests/fixtures/comments/no-comment-regex.result.js create mode 100644 tests/fixtures/comments/no-comment-regex.src.js create mode 100644 tests/fixtures/comments/no-comment-template.result.js create mode 100644 tests/fixtures/comments/no-comment-template.src.js rename tests/fixtures/{attach-comments => comments}/surrounding-call-comments.result.js (100%) rename tests/fixtures/{attach-comments => comments}/surrounding-call-comments.src.js (100%) rename tests/fixtures/{attach-comments => comments}/surrounding-debugger-comments.result.js (100%) rename tests/fixtures/{attach-comments => comments}/surrounding-debugger-comments.src.js (100%) rename tests/fixtures/{attach-comments => comments}/surrounding-return-comments.result.js (100%) rename tests/fixtures/{attach-comments => comments}/surrounding-return-comments.src.js (100%) rename tests/fixtures/{attach-comments => comments}/surrounding-throw-comments.result.js (100%) rename tests/fixtures/{attach-comments => comments}/surrounding-throw-comments.src.js (100%) rename tests/fixtures/{attach-comments => comments}/surrounding-while-loop-comments.result.js (100%) rename tests/fixtures/{attach-comments => comments}/surrounding-while-loop-comments.src.js (100%) rename tests/fixtures/{attach-comments => comments}/switch-fallthrough-comment-in-function.result.js (100%) rename tests/fixtures/{attach-comments => comments}/switch-fallthrough-comment-in-function.src.js (100%) rename tests/fixtures/{attach-comments => comments}/switch-fallthrough-comment.result.js (100%) rename tests/fixtures/{attach-comments => comments}/switch-fallthrough-comment.src.js (100%) rename tests/fixtures/{attach-comments => comments}/switch-no-default-comment-in-function.result.js (100%) rename tests/fixtures/{attach-comments => comments}/switch-no-default-comment-in-function.src.js (100%) rename tests/fixtures/{attach-comments => comments}/switch-no-default-comment-in-nested-functions.result.js (100%) rename tests/fixtures/{attach-comments => comments}/switch-no-default-comment-in-nested-functions.src.js (100%) rename tests/fixtures/{attach-comments => comments}/switch-no-default-comment.result.js (100%) rename tests/fixtures/{attach-comments => comments}/switch-no-default-comment.src.js (100%) create mode 100644 tests/fixtures/comments/template-string-block.result.js create mode 100644 tests/fixtures/comments/template-string-block.src.js rename tests/lib/{attach-comments.js => comments.js} (92%) diff --git a/lib/ast-converter.js b/lib/ast-converter.js index 363eb84..0ee75f6 100644 --- a/lib/ast-converter.js +++ b/lib/ast-converter.js @@ -12,6 +12,7 @@ //------------------------------------------------------------------------------ const convert = require("./convert"), + convertComments = require("./convert-comments").convertComments, nodeUtils = require("./node-utils"); //------------------------------------------------------------------------------ @@ -67,10 +68,10 @@ module.exports = (ast, extra) => { } /** - * Add the comment nodes to the AST (that were parsed separately in parser.js) + * Optionally convert and include all comments in the AST */ - if (extra.comment || extra.attachComment) { - estree.comments = extra.comments || []; + if (extra.comment) { + estree.comments = convertComments(ast, extra.code); } return estree; diff --git a/lib/convert-comments.js b/lib/convert-comments.js new file mode 100644 index 0000000..e51cec9 --- /dev/null +++ b/lib/convert-comments.js @@ -0,0 +1,147 @@ +/** + * @fileoverview Convert comment using TypeScript token scanner + * @author James Henry + * @copyright jQuery Foundation and other contributors, https://jquery.org/ + * MIT License + */ + +"use strict"; + +//------------------------------------------------------------------------------ +// Requirements +//------------------------------------------------------------------------------ + +const ts = require("typescript"), + nodeUtils = require("./node-utils"); + +//------------------------------------------------------------------------------ +// Private +//------------------------------------------------------------------------------ + +/** + * Converts a TypeScript comment to an Esprima comment. + * @param {boolean} block True if it's a block comment, false if not. + * @param {string} text The text of the comment. + * @param {int} start The index at which the comment starts. + * @param {int} end The index at which the comment ends. + * @param {Location} startLoc The location at which the comment starts. + * @param {Location} endLoc The location at which the comment ends. + * @returns {Object} The comment object. + * @private + */ +function convertTypeScriptCommentToEsprimaComment(block, text, start, end, startLoc, endLoc) { + const comment = { + type: block ? "Block" : "Line", + value: text + }; + + if (typeof start === "number") { + comment.range = [start, end]; + } + + if (typeof startLoc === "object") { + comment.loc = { + start: startLoc, + end: endLoc + }; + } + + return comment; +} + +/** + * Convert comment from TypeScript Triva Scanner. + * @param {Object} triviaScanner TS Scanner + * @param {Object} ast the AST object + * @param {string} code TypeScript code + * @returns {ESTreeComment} the converted ESTreeComment + * @private + */ +function getCommentFromTriviaScanner(triviaScanner, ast, code) { + const kind = triviaScanner.getToken(); + const isBlock = (kind === ts.SyntaxKind.MultiLineCommentTrivia); + const range = { + pos: triviaScanner.getTokenPos(), + end: triviaScanner.getTextPos(), + kind: triviaScanner.getToken() + }; + + const comment = code.substring(range.pos, range.end); + const text = (isBlock) ? comment.replace(/^\/\*/, "").replace(/\*\/$/, "") : comment.replace(/^\/\//, ""); + const loc = nodeUtils.getLocFor(range.pos, range.end, ast); + + const esprimaComment = convertTypeScriptCommentToEsprimaComment(isBlock, text, range.pos, range.end, loc.start, loc.end); + + return esprimaComment; +} + +//------------------------------------------------------------------------------ +// Public +//------------------------------------------------------------------------------ + +/* eslint-disable no-use-before-define */ +module.exports = { + convertComments +}; + + +/** + * Convert all comments for the given AST. + * @param {Object} ast the AST object + * @param {string} code the TypeScript code + * @returns {ESTreeComment[]} the converted ESTreeComment + * @private + */ +function convertComments(ast, code) { + const comments = []; + + /** + * Create a TypeScript Scanner, with skipTrivia set to false so that + * we can parse the comments + */ + const triviaScanner = ts.createScanner(ast.languageVersion, false, 0, code); + + let kind = triviaScanner.scan(); + while (kind !== ts.SyntaxKind.EndOfFileToken) { + const start = triviaScanner.getTokenPos(); + const end = triviaScanner.getTextPos(); + + let container = null; + switch (kind) { + case ts.SyntaxKind.SingleLineCommentTrivia: + case ts.SyntaxKind.MultiLineCommentTrivia: { + const comment = getCommentFromTriviaScanner(triviaScanner, ast, code); + + comments.push(comment); + break; + } + case ts.SyntaxKind.CloseBraceToken: + container = nodeUtils.getNodeContainer(ast, start, end); + + if ( + container.kind === ts.SyntaxKind.TemplateMiddle || + container.kind === ts.SyntaxKind.TemplateTail + ) { + kind = triviaScanner.reScanTemplateToken(); + continue; + } + break; + case ts.SyntaxKind.SlashToken: + case ts.SyntaxKind.SlashEqualsToken: + container = nodeUtils.getNodeContainer(ast, start, end); + + if ( + container.kind === ts.SyntaxKind.RegularExpressionLiteral + ) { + kind = triviaScanner.reScanSlashToken(); + continue; + } + break; + default: + break; + } + kind = triviaScanner.scan(); + } + + return comments; +} diff --git a/lib/node-utils.js b/lib/node-utils.js index c5585d3..3e7f0de 100644 --- a/lib/node-utils.js +++ b/lib/node-utils.js @@ -187,7 +187,8 @@ module.exports = { fixExports, getTokenType, convertToken, - convertTokens + convertTokens, + getNodeContainer }; /* eslint-enable no-use-before-define */ @@ -633,3 +634,35 @@ function convertTokens(ast) { walk(ast); return result; } + +/** + * Get container token node between range + * @param {Object} ast the AST object + * @param {int} start The index at which the comment starts. + * @param {int} end The index at which the comment ends. + * @returns {TSToken} typescript container token + * @private + */ +function getNodeContainer(ast, start, end) { + let container = null; + + /** + * @param {TSNode} node the TSNode + * @returns {undefined} + */ + function walk(node) { + const nodeStart = node.pos; + const nodeEnd = node.end; + + if (start >= nodeStart && end <= nodeEnd) { + if (isToken(node)) { + container = node; + } else { + node.getChildren().forEach(walk); + } + } + } + walk(ast); + + return container; +} diff --git a/parser.js b/parser.js index 6958209..455333f 100644 --- a/parser.js +++ b/parser.js @@ -9,6 +9,7 @@ const astNodeTypes = require("./lib/ast-node-types"), ts = require("typescript"), + convert = require("./lib/ast-converter"), semver = require("semver"); const SUPPORTED_TYPESCRIPT_VERSIONS = require("./package.json").devDependencies.typescript; @@ -51,61 +52,6 @@ function resetExtra() { }; } -/** - * Converts a TypeScript comment to an Esprima comment. - * @param {boolean} block True if it's a block comment, false if not. - * @param {string} text The text of the comment. - * @param {int} start The index at which the comment starts. - * @param {int} end The index at which the comment ends. - * @param {Location} startLoc The location at which the comment starts. - * @param {Location} endLoc The location at which the comment ends. - * @returns {Object} The comment object. - * @private - */ -function convertTypeScriptCommentToEsprimaComment(block, text, start, end, startLoc, endLoc) { - const comment = { - type: block ? "Block" : "Line", - value: text - }; - - if (typeof start === "number") { - comment.range = [start, end]; - } - - if (typeof startLoc === "object") { - comment.loc = { - start: startLoc, - end: endLoc - }; - } - - return comment; -} - -/** - * Returns line and column data for the given start and end positions, - * for the given AST - * @param {Object} start start data - * @param {Object} end end data - * @param {Object} ast the AST object - * @returns {Object} the loc data - */ -function getLocFor(start, end, ast) { - const startLoc = ast.getLineAndCharacterOfPosition(start), - endLoc = ast.getLineAndCharacterOfPosition(end); - - return { - start: { - line: startLoc.line + 1, - column: startLoc.character - }, - end: { - line: endLoc.line + 1, - column: endLoc.character - } - }; -} - //------------------------------------------------------------------------------ // Parser //------------------------------------------------------------------------------ @@ -129,7 +75,6 @@ function parse(code, options) { if (typeof options !== "undefined") { extra.range = (typeof options.range === "boolean") && options.range; extra.loc = (typeof options.loc === "boolean") && options.loc; - extra.attachComment = (typeof options.attachComment === "boolean") && options.attachComment; if (extra.loc && options.source !== null && options.source !== undefined) { extra.source = toString(options.source); @@ -145,10 +90,6 @@ function parse(code, options) { if (typeof options.tolerant === "boolean" && options.tolerant) { extra.errors = []; } - if (extra.attachComment) { - extra.range = true; - extra.comments = []; - } if (options.ecmaFeatures && typeof options.ecmaFeatures === "object") { // pass through jsx option @@ -209,45 +150,8 @@ function parse(code, options) { const ast = program.getSourceFile(FILENAME); - if (extra.attachComment || extra.comment) { - /** - * Create a TypeScript Scanner, with skipTrivia set to false so that - * we can parse the comments - */ - const triviaScanner = ts.createScanner(ast.languageVersion, false, 0, code); - - let kind = triviaScanner.scan(); - - while (kind !== ts.SyntaxKind.EndOfFileToken) { - if (kind !== ts.SyntaxKind.SingleLineCommentTrivia && kind !== ts.SyntaxKind.MultiLineCommentTrivia) { - kind = triviaScanner.scan(); - continue; - } - - const isBlock = (kind === ts.SyntaxKind.MultiLineCommentTrivia); - const range = { - pos: triviaScanner.getTokenPos(), - end: triviaScanner.getTextPos(), - kind: triviaScanner.getToken() - }; - - const comment = code.substring(range.pos, range.end); - const text = comment.replace("//", "").replace("/*", "").replace("*/", ""); - const loc = getLocFor(range.pos, range.end, ast); - - const esprimaComment = convertTypeScriptCommentToEsprimaComment(isBlock, text, range.pos, range.end, loc.start, loc.end); - - extra.comments.push(esprimaComment); - - kind = triviaScanner.scan(); - } - - } - - const convert = require("./lib/ast-converter"); - + extra.code = code; return convert(ast, extra); - } //------------------------------------------------------------------------------ diff --git a/tests/fixtures/attach-comments/block-trailing-comment.result.js b/tests/fixtures/comments/block-trailing-comment.result.js similarity index 100% rename from tests/fixtures/attach-comments/block-trailing-comment.result.js rename to tests/fixtures/comments/block-trailing-comment.result.js diff --git a/tests/fixtures/attach-comments/block-trailing-comment.src.js b/tests/fixtures/comments/block-trailing-comment.src.js similarity index 100% rename from tests/fixtures/attach-comments/block-trailing-comment.src.js rename to tests/fixtures/comments/block-trailing-comment.src.js diff --git a/tests/fixtures/attach-comments/comment-within-condition.result.js b/tests/fixtures/comments/comment-within-condition.result.js similarity index 100% rename from tests/fixtures/attach-comments/comment-within-condition.result.js rename to tests/fixtures/comments/comment-within-condition.result.js diff --git a/tests/fixtures/attach-comments/comment-within-condition.src.js b/tests/fixtures/comments/comment-within-condition.src.js similarity index 100% rename from tests/fixtures/attach-comments/comment-within-condition.src.js rename to tests/fixtures/comments/comment-within-condition.src.js diff --git a/tests/fixtures/attach-comments/export-default-anonymous-class.result.js b/tests/fixtures/comments/export-default-anonymous-class.result.js similarity index 100% rename from tests/fixtures/attach-comments/export-default-anonymous-class.result.js rename to tests/fixtures/comments/export-default-anonymous-class.result.js diff --git a/tests/fixtures/attach-comments/export-default-anonymous-class.src.js b/tests/fixtures/comments/export-default-anonymous-class.src.js similarity index 100% rename from tests/fixtures/attach-comments/export-default-anonymous-class.src.js rename to tests/fixtures/comments/export-default-anonymous-class.src.js diff --git a/tests/fixtures/comments/jsx-block-comment.result.js b/tests/fixtures/comments/jsx-block-comment.result.js new file mode 100644 index 0000000..4e6b038 --- /dev/null +++ b/tests/fixtures/comments/jsx-block-comment.result.js @@ -0,0 +1,732 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 97 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 1 + } + }, + "body": [ + { + "type": "VariableDeclaration", + "range": [ + 0, + 97 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 1 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "range": [ + 6, + 97 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 7, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "range": [ + 6, + 10 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "name": "pure" + }, + "init": { + "type": "ArrowFunctionExpression", + "range": [ + 13, + 97 + ], + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 7, + "column": 1 + } + }, + "generator": false, + "id": null, + "params": [], + "body": { + "type": "BlockStatement", + "range": [ + 19, + 97 + ], + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 7, + "column": 1 + } + }, + "body": [ + { + "type": "ReturnStatement", + "range": [ + 25, + 95 + ], + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 6, + "column": 6 + } + }, + "argument": { + "type": "JSXElement", + "range": [ + 42, + 88 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 5, + "column": 14 + } + }, + "openingElement": { + "type": "JSXOpeningElement", + "range": [ + 42, + 47 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 13 + } + }, + "selfClosing": false, + "name": { + "type": "JSXIdentifier", + "range": [ + 43, + 46 + ], + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 12 + } + }, + "name": "Foo" + }, + "attributes": [] + }, + "closingElement": { + "type": "JSXClosingElement", + "range": [ + 82, + 88 + ], + "loc": { + "start": { + "line": 5, + "column": 8 + }, + "end": { + "line": 5, + "column": 14 + } + }, + "name": { + "type": "JSXIdentifier", + "range": [ + 84, + 87 + ], + "loc": { + "start": { + "line": 5, + "column": 10 + }, + "end": { + "line": 5, + "column": 13 + } + }, + "name": "Foo" + } + }, + "children": [ + { + "type": "Literal", + "range": [ + 47, + 60 + ], + "loc": { + "start": { + "line": 4, + "column": 47 + }, + "end": { + "line": 4, + "column": 12 + } + }, + "value": "\n ", + "raw": "\n " + }, + { + "type": "JSXExpressionContainer", + "range": [ + 60, + 73 + ], + "loc": { + "start": { + "line": 4, + "column": 12 + }, + "end": { + "line": 4, + "column": 25 + } + }, + "expression": { + "type": "JSXEmptyExpression", + "loc": { + "start": { + "line": 4, + "column": 13 + }, + "end": { + "line": 4, + "column": 24 + } + }, + "range": [ + 61, + 72 + ] + } + }, + { + "type": "Literal", + "range": [ + 73, + 82 + ], + "loc": { + "start": { + "line": 5, + "column": 73 + }, + "end": { + "line": 5, + "column": 8 + } + }, + "value": "\n ", + "raw": "\n " + } + ] + } + } + ] + }, + "async": false, + "expression": false + } + } + ], + "kind": "const" + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Keyword", + "value": "const", + "range": [ + 0, + 5 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 5 + } + } + }, + { + "type": "Identifier", + "value": "pure", + "range": [ + 6, + 10 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 10 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "range": [ + 11, + 12 + ], + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + } + } + }, + { + "type": "Punctuator", + "value": "(", + "range": [ + 13, + 14 + ], + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + } + } + }, + { + "type": "Punctuator", + "value": ")", + "range": [ + 14, + 15 + ], + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + } + } + }, + { + "type": "Punctuator", + "value": "=>", + "range": [ + 16, + 18 + ], + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 18 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 19, + 20 + ], + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + } + } + }, + { + "type": "Keyword", + "value": "return", + "range": [ + 25, + 31 + ], + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 10 + } + } + }, + { + "type": "Punctuator", + "value": "(", + "range": [ + 32, + 33 + ], + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 12 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 42, + 43 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 9 + } + } + }, + { + "type": "JSXIdentifier", + "value": "Foo", + "range": [ + 43, + 46 + ], + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 12 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 46, + 47 + ], + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 13 + } + } + }, + { + "type": "JSXText", + "value": "", + "range": [ + 60, + 60 + ], + "loc": { + "start": { + "line": 4, + "column": 12 + }, + "end": { + "line": 4, + "column": 12 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 60, + 61 + ], + "loc": { + "start": { + "line": 4, + "column": 12 + }, + "end": { + "line": 4, + "column": 13 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 72, + 73 + ], + "loc": { + "start": { + "line": 4, + "column": 24 + }, + "end": { + "line": 4, + "column": 25 + } + } + }, + { + "type": "JSXText", + "value": "", + "range": [ + 82, + 82 + ], + "loc": { + "start": { + "line": 5, + "column": 8 + }, + "end": { + "line": 5, + "column": 8 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 82, + 83 + ], + "loc": { + "start": { + "line": 5, + "column": 8 + }, + "end": { + "line": 5, + "column": 9 + } + } + }, + { + "type": "Punctuator", + "value": "/", + "range": [ + 83, + 84 + ], + "loc": { + "start": { + "line": 5, + "column": 9 + }, + "end": { + "line": 5, + "column": 10 + } + } + }, + { + "type": "JSXIdentifier", + "value": "Foo", + "range": [ + 84, + 87 + ], + "loc": { + "start": { + "line": 5, + "column": 10 + }, + "end": { + "line": 5, + "column": 13 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 87, + 88 + ], + "loc": { + "start": { + "line": 5, + "column": 13 + }, + "end": { + "line": 5, + "column": 14 + } + } + }, + { + "type": "Punctuator", + "value": ")", + "range": [ + 93, + 94 + ], + "loc": { + "start": { + "line": 6, + "column": 4 + }, + "end": { + "line": 6, + "column": 5 + } + } + }, + { + "type": "Punctuator", + "value": ";", + "range": [ + 94, + 95 + ], + "loc": { + "start": { + "line": 6, + "column": 5 + }, + "end": { + "line": 6, + "column": 6 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 96, + 97 + ], + "loc": { + "start": { + "line": 7, + "column": 0 + }, + "end": { + "line": 7, + "column": 1 + } + } + } + ], + "comments": [ + { + "type": "Block", + "value": "COMMENT", + "range": [ + 61, + 72 + ], + "loc": { + "start": { + "line": 4, + "column": 13 + }, + "end": { + "line": 4, + "column": 24 + } + } + } + ] +}; diff --git a/tests/fixtures/comments/jsx-block-comment.src.js b/tests/fixtures/comments/jsx-block-comment.src.js new file mode 100644 index 0000000..5abfb88 --- /dev/null +++ b/tests/fixtures/comments/jsx-block-comment.src.js @@ -0,0 +1,7 @@ +const pure = () => { + return ( + + {/*COMMENT*/} + + ); +} diff --git a/tests/fixtures/comments/jsx-tag-comments.result.js b/tests/fixtures/comments/jsx-tag-comments.result.js new file mode 100644 index 0000000..cddb435 --- /dev/null +++ b/tests/fixtures/comments/jsx-tag-comments.result.js @@ -0,0 +1,644 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 127 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 9, + "column": 1 + } + }, + "body": [ + { + "type": "VariableDeclaration", + "range": [ + 0, + 127 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 9, + "column": 1 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "range": [ + 6, + 127 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 9, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "range": [ + 6, + 10 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "name": "pure" + }, + "init": { + "type": "ArrowFunctionExpression", + "range": [ + 13, + 127 + ], + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 9, + "column": 1 + } + }, + "generator": false, + "id": null, + "params": [], + "body": { + "type": "BlockStatement", + "range": [ + 19, + 127 + ], + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 9, + "column": 1 + } + }, + "body": [ + { + "type": "ReturnStatement", + "range": [ + 25, + 125 + ], + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 8, + "column": 6 + } + }, + "argument": { + "type": "JSXElement", + "range": [ + 42, + 118 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 7, + "column": 14 + } + }, + "openingElement": { + "type": "JSXOpeningElement", + "range": [ + 42, + 103 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 6, + "column": 9 + } + }, + "selfClosing": false, + "name": { + "type": "JSXIdentifier", + "range": [ + 43, + 46 + ], + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 12 + } + }, + "name": "Foo" + }, + "attributes": [] + }, + "closingElement": { + "type": "JSXClosingElement", + "range": [ + 112, + 118 + ], + "loc": { + "start": { + "line": 7, + "column": 8 + }, + "end": { + "line": 7, + "column": 14 + } + }, + "name": { + "type": "JSXIdentifier", + "range": [ + 114, + 117 + ], + "loc": { + "start": { + "line": 7, + "column": 10 + }, + "end": { + "line": 7, + "column": 13 + } + }, + "name": "Foo" + } + }, + "children": [ + { + "type": "Literal", + "range": [ + 103, + 112 + ], + "loc": { + "start": { + "line": 7, + "column": 103 + }, + "end": { + "line": 7, + "column": 8 + } + }, + "value": "\n ", + "raw": "\n " + } + ] + } + } + ] + }, + "async": false, + "expression": false + } + } + ], + "kind": "const" + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Keyword", + "value": "const", + "range": [ + 0, + 5 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 5 + } + } + }, + { + "type": "Identifier", + "value": "pure", + "range": [ + 6, + 10 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 10 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "range": [ + 11, + 12 + ], + "loc": { + "start": { + "line": 1, + "column": 11 + }, + "end": { + "line": 1, + "column": 12 + } + } + }, + { + "type": "Punctuator", + "value": "(", + "range": [ + 13, + 14 + ], + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + } + } + }, + { + "type": "Punctuator", + "value": ")", + "range": [ + 14, + 15 + ], + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + } + } + }, + { + "type": "Punctuator", + "value": "=>", + "range": [ + 16, + 18 + ], + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 18 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 19, + 20 + ], + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 20 + } + } + }, + { + "type": "Keyword", + "value": "return", + "range": [ + 25, + 31 + ], + "loc": { + "start": { + "line": 2, + "column": 4 + }, + "end": { + "line": 2, + "column": 10 + } + } + }, + { + "type": "Punctuator", + "value": "(", + "range": [ + 32, + 33 + ], + "loc": { + "start": { + "line": 2, + "column": 11 + }, + "end": { + "line": 2, + "column": 12 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 42, + 43 + ], + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 9 + } + } + }, + { + "type": "JSXIdentifier", + "value": "Foo", + "range": [ + 43, + 46 + ], + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 12 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 102, + 103 + ], + "loc": { + "start": { + "line": 6, + "column": 8 + }, + "end": { + "line": 6, + "column": 9 + } + } + }, + { + "type": "JSXText", + "value": "", + "range": [ + 112, + 112 + ], + "loc": { + "start": { + "line": 7, + "column": 8 + }, + "end": { + "line": 7, + "column": 8 + } + } + }, + { + "type": "Punctuator", + "value": "<", + "range": [ + 112, + 113 + ], + "loc": { + "start": { + "line": 7, + "column": 8 + }, + "end": { + "line": 7, + "column": 9 + } + } + }, + { + "type": "Punctuator", + "value": "/", + "range": [ + 113, + 114 + ], + "loc": { + "start": { + "line": 7, + "column": 9 + }, + "end": { + "line": 7, + "column": 10 + } + } + }, + { + "type": "JSXIdentifier", + "value": "Foo", + "range": [ + 114, + 117 + ], + "loc": { + "start": { + "line": 7, + "column": 10 + }, + "end": { + "line": 7, + "column": 13 + } + } + }, + { + "type": "Punctuator", + "value": ">", + "range": [ + 117, + 118 + ], + "loc": { + "start": { + "line": 7, + "column": 13 + }, + "end": { + "line": 7, + "column": 14 + } + } + }, + { + "type": "Punctuator", + "value": ")", + "range": [ + 123, + 124 + ], + "loc": { + "start": { + "line": 8, + "column": 4 + }, + "end": { + "line": 8, + "column": 5 + } + } + }, + { + "type": "Punctuator", + "value": ";", + "range": [ + 124, + 125 + ], + "loc": { + "start": { + "line": 8, + "column": 5 + }, + "end": { + "line": 8, + "column": 6 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 126, + 127 + ], + "loc": { + "start": { + "line": 9, + "column": 0 + }, + "end": { + "line": 9, + "column": 1 + } + } + } + ], + "comments": [ + { + "type": "Line", + "value": " single", + "range": [ + 59, + 68 + ], + "loc": { + "start": { + "line": 4, + "column": 12 + }, + "end": { + "line": 4, + "column": 21 + } + } + }, + { + "type": "Block", + "value": " block ", + "range": [ + 81, + 92 + ], + "loc": { + "start": { + "line": 5, + "column": 12 + }, + "end": { + "line": 5, + "column": 23 + } + } + } + ] +}; + diff --git a/tests/fixtures/comments/jsx-tag-comments.src.js b/tests/fixtures/comments/jsx-tag-comments.src.js new file mode 100644 index 0000000..ef679a1 --- /dev/null +++ b/tests/fixtures/comments/jsx-tag-comments.src.js @@ -0,0 +1,10 @@ +const pure = () => { + return ( + + + ); +} + diff --git a/tests/fixtures/comments/line-comment-with-block-syntax.result.js b/tests/fixtures/comments/line-comment-with-block-syntax.result.js new file mode 100644 index 0000000..85be15e --- /dev/null +++ b/tests/fixtures/comments/line-comment-with-block-syntax.result.js @@ -0,0 +1,40 @@ +module.exports = { + "type": "Program", + "range": [ + 12, + 0 + ], + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 1, + "column": 0 + } + }, + "body": [], + "sourceType": "script", + "tokens": [], + "comments": [ + { + "type": "Line", + "value": " /*test*/", + "range": [ + 0, + 11 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 11 + } + } + } + ] +}; diff --git a/tests/fixtures/comments/line-comment-with-block-syntax.src.js b/tests/fixtures/comments/line-comment-with-block-syntax.src.js new file mode 100644 index 0000000..88994dd --- /dev/null +++ b/tests/fixtures/comments/line-comment-with-block-syntax.src.js @@ -0,0 +1 @@ +// /*test*/ diff --git a/tests/fixtures/attach-comments/mix-line-and-block-comments.result.js b/tests/fixtures/comments/mix-line-and-block-comments.result.js similarity index 100% rename from tests/fixtures/attach-comments/mix-line-and-block-comments.result.js rename to tests/fixtures/comments/mix-line-and-block-comments.result.js diff --git a/tests/fixtures/attach-comments/mix-line-and-block-comments.src.js b/tests/fixtures/comments/mix-line-and-block-comments.src.js similarity index 100% rename from tests/fixtures/attach-comments/mix-line-and-block-comments.src.js rename to tests/fixtures/comments/mix-line-and-block-comments.src.js diff --git a/tests/fixtures/comments/no-comment-regex.result.js b/tests/fixtures/comments/no-comment-regex.result.js new file mode 100644 index 0000000..5197380 --- /dev/null +++ b/tests/fixtures/comments/no-comment-regex.result.js @@ -0,0 +1,195 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 34 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 34 + } + }, + "body": [ + { + "type": "VariableDeclaration", + "range": [ + 0, + 34 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 34 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "range": [ + 6, + 33 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 33 + } + }, + "id": { + "type": "Identifier", + "range": [ + 6, + 11 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "name": "regex" + }, + "init": { + "type": "Literal", + "range": [ + 14, + 33 + ], + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 33 + } + }, + "value": null, + "raw": "/no comment\\/**foo/", + "regex": { + "pattern": "no comment\\/**foo", + "flags": "" + } + } + } + ], + "kind": "const" + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Keyword", + "value": "const", + "range": [ + 0, + 5 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 5 + } + } + }, + { + "type": "Identifier", + "value": "regex", + "range": [ + 6, + 11 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 11 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "range": [ + 12, + 13 + ], + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + } + } + }, + { + "type": "RegularExpression", + "value": "/no comment\\/**foo/", + "range": [ + 14, + 33 + ], + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 33 + } + }, + "regex": { + "pattern": "no comment\\/**foo", + "flags": "" + } + }, + { + "type": "Punctuator", + "value": ";", + "range": [ + 33, + 34 + ], + "loc": { + "start": { + "line": 1, + "column": 33 + }, + "end": { + "line": 1, + "column": 34 + } + } + } + ], + "comments": [] +}; diff --git a/tests/fixtures/comments/no-comment-regex.src.js b/tests/fixtures/comments/no-comment-regex.src.js new file mode 100644 index 0000000..c33c761 --- /dev/null +++ b/tests/fixtures/comments/no-comment-regex.src.js @@ -0,0 +1 @@ +const regex = /no comment\/**foo/; diff --git a/tests/fixtures/comments/no-comment-template.result.js b/tests/fixtures/comments/no-comment-template.result.js new file mode 100644 index 0000000..fba6338 --- /dev/null +++ b/tests/fixtures/comments/no-comment-template.result.js @@ -0,0 +1,287 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 37 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 37 + } + }, + "body": [ + { + "type": "VariableDeclaration", + "range": [ + 0, + 37 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 37 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "range": [ + 6, + 36 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 36 + } + }, + "id": { + "type": "Identifier", + "range": [ + 6, + 9 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "name": "str" + }, + "init": { + "type": "TemplateLiteral", + "range": [ + 12, + 36 + ], + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 36 + } + }, + "quasis": [ + { + "type": "TemplateElement", + "range": [ + 12, + 15 + ], + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 15 + } + }, + "value": { + "raw": "", + "cooked": "" + }, + "tail": false + }, + { + "type": "TemplateElement", + "range": [ + 24, + 36 + ], + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 36 + } + }, + "value": { + "raw": "/test/*.js", + "cooked": "/test/*.js" + }, + "tail": true + } + ], + "expressions": [ + { + "type": "Identifier", + "range": [ + 15, + 24 + ], + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "name": "__dirname" + } + ] + } + } + ], + "kind": "const" + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Keyword", + "value": "const", + "range": [ + 0, + 5 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 5 + } + } + }, + { + "type": "Identifier", + "value": "str", + "range": [ + 6, + 9 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 9 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "range": [ + 10, + 11 + ], + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + } + } + }, + { + "type": "Template", + "value": "`${", + "range": [ + 12, + 15 + ], + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 15 + } + } + }, + { + "type": "Identifier", + "value": "__dirname", + "range": [ + 15, + 24 + ], + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 24 + } + } + }, + { + "type": "Template", + "value": "}/test/*.js`", + "range": [ + 24, + 36 + ], + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 36 + } + } + }, + { + "type": "Punctuator", + "value": ";", + "range": [ + 36, + 37 + ], + "loc": { + "start": { + "line": 1, + "column": 36 + }, + "end": { + "line": 1, + "column": 37 + } + } + } + ], + "comments": [] +}; diff --git a/tests/fixtures/comments/no-comment-template.src.js b/tests/fixtures/comments/no-comment-template.src.js new file mode 100644 index 0000000..f22e6f9 --- /dev/null +++ b/tests/fixtures/comments/no-comment-template.src.js @@ -0,0 +1 @@ +const str = `${__dirname}/test/*.js`; diff --git a/tests/fixtures/attach-comments/surrounding-call-comments.result.js b/tests/fixtures/comments/surrounding-call-comments.result.js similarity index 100% rename from tests/fixtures/attach-comments/surrounding-call-comments.result.js rename to tests/fixtures/comments/surrounding-call-comments.result.js diff --git a/tests/fixtures/attach-comments/surrounding-call-comments.src.js b/tests/fixtures/comments/surrounding-call-comments.src.js similarity index 100% rename from tests/fixtures/attach-comments/surrounding-call-comments.src.js rename to tests/fixtures/comments/surrounding-call-comments.src.js diff --git a/tests/fixtures/attach-comments/surrounding-debugger-comments.result.js b/tests/fixtures/comments/surrounding-debugger-comments.result.js similarity index 100% rename from tests/fixtures/attach-comments/surrounding-debugger-comments.result.js rename to tests/fixtures/comments/surrounding-debugger-comments.result.js diff --git a/tests/fixtures/attach-comments/surrounding-debugger-comments.src.js b/tests/fixtures/comments/surrounding-debugger-comments.src.js similarity index 100% rename from tests/fixtures/attach-comments/surrounding-debugger-comments.src.js rename to tests/fixtures/comments/surrounding-debugger-comments.src.js diff --git a/tests/fixtures/attach-comments/surrounding-return-comments.result.js b/tests/fixtures/comments/surrounding-return-comments.result.js similarity index 100% rename from tests/fixtures/attach-comments/surrounding-return-comments.result.js rename to tests/fixtures/comments/surrounding-return-comments.result.js diff --git a/tests/fixtures/attach-comments/surrounding-return-comments.src.js b/tests/fixtures/comments/surrounding-return-comments.src.js similarity index 100% rename from tests/fixtures/attach-comments/surrounding-return-comments.src.js rename to tests/fixtures/comments/surrounding-return-comments.src.js diff --git a/tests/fixtures/attach-comments/surrounding-throw-comments.result.js b/tests/fixtures/comments/surrounding-throw-comments.result.js similarity index 100% rename from tests/fixtures/attach-comments/surrounding-throw-comments.result.js rename to tests/fixtures/comments/surrounding-throw-comments.result.js diff --git a/tests/fixtures/attach-comments/surrounding-throw-comments.src.js b/tests/fixtures/comments/surrounding-throw-comments.src.js similarity index 100% rename from tests/fixtures/attach-comments/surrounding-throw-comments.src.js rename to tests/fixtures/comments/surrounding-throw-comments.src.js diff --git a/tests/fixtures/attach-comments/surrounding-while-loop-comments.result.js b/tests/fixtures/comments/surrounding-while-loop-comments.result.js similarity index 100% rename from tests/fixtures/attach-comments/surrounding-while-loop-comments.result.js rename to tests/fixtures/comments/surrounding-while-loop-comments.result.js diff --git a/tests/fixtures/attach-comments/surrounding-while-loop-comments.src.js b/tests/fixtures/comments/surrounding-while-loop-comments.src.js similarity index 100% rename from tests/fixtures/attach-comments/surrounding-while-loop-comments.src.js rename to tests/fixtures/comments/surrounding-while-loop-comments.src.js diff --git a/tests/fixtures/attach-comments/switch-fallthrough-comment-in-function.result.js b/tests/fixtures/comments/switch-fallthrough-comment-in-function.result.js similarity index 100% rename from tests/fixtures/attach-comments/switch-fallthrough-comment-in-function.result.js rename to tests/fixtures/comments/switch-fallthrough-comment-in-function.result.js diff --git a/tests/fixtures/attach-comments/switch-fallthrough-comment-in-function.src.js b/tests/fixtures/comments/switch-fallthrough-comment-in-function.src.js similarity index 100% rename from tests/fixtures/attach-comments/switch-fallthrough-comment-in-function.src.js rename to tests/fixtures/comments/switch-fallthrough-comment-in-function.src.js diff --git a/tests/fixtures/attach-comments/switch-fallthrough-comment.result.js b/tests/fixtures/comments/switch-fallthrough-comment.result.js similarity index 100% rename from tests/fixtures/attach-comments/switch-fallthrough-comment.result.js rename to tests/fixtures/comments/switch-fallthrough-comment.result.js diff --git a/tests/fixtures/attach-comments/switch-fallthrough-comment.src.js b/tests/fixtures/comments/switch-fallthrough-comment.src.js similarity index 100% rename from tests/fixtures/attach-comments/switch-fallthrough-comment.src.js rename to tests/fixtures/comments/switch-fallthrough-comment.src.js diff --git a/tests/fixtures/attach-comments/switch-no-default-comment-in-function.result.js b/tests/fixtures/comments/switch-no-default-comment-in-function.result.js similarity index 100% rename from tests/fixtures/attach-comments/switch-no-default-comment-in-function.result.js rename to tests/fixtures/comments/switch-no-default-comment-in-function.result.js diff --git a/tests/fixtures/attach-comments/switch-no-default-comment-in-function.src.js b/tests/fixtures/comments/switch-no-default-comment-in-function.src.js similarity index 100% rename from tests/fixtures/attach-comments/switch-no-default-comment-in-function.src.js rename to tests/fixtures/comments/switch-no-default-comment-in-function.src.js diff --git a/tests/fixtures/attach-comments/switch-no-default-comment-in-nested-functions.result.js b/tests/fixtures/comments/switch-no-default-comment-in-nested-functions.result.js similarity index 100% rename from tests/fixtures/attach-comments/switch-no-default-comment-in-nested-functions.result.js rename to tests/fixtures/comments/switch-no-default-comment-in-nested-functions.result.js diff --git a/tests/fixtures/attach-comments/switch-no-default-comment-in-nested-functions.src.js b/tests/fixtures/comments/switch-no-default-comment-in-nested-functions.src.js similarity index 100% rename from tests/fixtures/attach-comments/switch-no-default-comment-in-nested-functions.src.js rename to tests/fixtures/comments/switch-no-default-comment-in-nested-functions.src.js diff --git a/tests/fixtures/attach-comments/switch-no-default-comment.result.js b/tests/fixtures/comments/switch-no-default-comment.result.js similarity index 100% rename from tests/fixtures/attach-comments/switch-no-default-comment.result.js rename to tests/fixtures/comments/switch-no-default-comment.result.js diff --git a/tests/fixtures/attach-comments/switch-no-default-comment.src.js b/tests/fixtures/comments/switch-no-default-comment.src.js similarity index 100% rename from tests/fixtures/attach-comments/switch-no-default-comment.src.js rename to tests/fixtures/comments/switch-no-default-comment.src.js diff --git a/tests/fixtures/comments/template-string-block.result.js b/tests/fixtures/comments/template-string-block.result.js new file mode 100644 index 0000000..a363db1 --- /dev/null +++ b/tests/fixtures/comments/template-string-block.result.js @@ -0,0 +1,414 @@ +module.exports = { + "type": "Program", + "range": [ + 0, + 64 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "body": [ + { + "type": "ExpressionStatement", + "range": [ + 0, + 10 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "expression": { + "type": "TemplateLiteral", + "range": [ + 0, + 9 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "quasis": [ + { + "type": "TemplateElement", + "range": [ + 0, + 3 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 3 + } + }, + "value": { + "raw": "", + "cooked": "" + }, + "tail": false + }, + { + "type": "TemplateElement", + "range": [ + 7, + 9 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "value": { + "raw": "", + "cooked": "" + }, + "tail": true + } + ], + "expressions": [ + { + "type": "Identifier", + "range": [ + 3, + 7 + ], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "name": "name" + } + ] + } + }, + { + "type": "BlockStatement", + "range": [ + 11, + 64 + ], + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "body": [ + { + "type": "ExpressionStatement", + "range": [ + 56, + 62 + ], + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 10 + } + }, + "expression": { + "type": "BinaryExpression", + "range": [ + 56, + 61 + ], + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 9 + } + }, + "operator": "+", + "left": { + "type": "Literal", + "range": [ + 56, + 57 + ], + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 5 + } + }, + "value": 1, + "raw": "1" + }, + "right": { + "type": "Literal", + "range": [ + 60, + 61 + ], + "loc": { + "start": { + "line": 4, + "column": 8 + }, + "end": { + "line": 4, + "column": 9 + } + }, + "value": 1, + "raw": "1" + } + } + } + ] + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Template", + "value": "`${", + "range": [ + 0, + 3 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "Identifier", + "value": "name", + "range": [ + 3, + 7 + ], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 7 + } + } + }, + { + "type": "Template", + "value": "}`", + "range": [ + 7, + 9 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 9 + } + } + }, + { + "type": "Punctuator", + "value": ";", + "range": [ + 9, + 10 + ], + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "range": [ + 11, + 12 + ], + "loc": { + "start": { + "line": 2, + "column": 0 + }, + "end": { + "line": 2, + "column": 1 + } + } + }, + { + "type": "Numeric", + "value": "1", + "range": [ + 56, + 57 + ], + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 5 + } + } + }, + { + "type": "Punctuator", + "value": "+", + "range": [ + 58, + 59 + ], + "loc": { + "start": { + "line": 4, + "column": 6 + }, + "end": { + "line": 4, + "column": 7 + } + } + }, + { + "type": "Numeric", + "value": "1", + "range": [ + 60, + 61 + ], + "loc": { + "start": { + "line": 4, + "column": 8 + }, + "end": { + "line": 4, + "column": 9 + } + } + }, + { + "type": "Punctuator", + "value": ";", + "range": [ + 61, + 62 + ], + "loc": { + "start": { + "line": 4, + "column": 9 + }, + "end": { + "line": 4, + "column": 10 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "range": [ + 63, + 64 + ], + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + } + } + ], + "comments": [ + { + "type": "Block", + "value": " TODO comment comment comment ", + "range": [ + 17, + 51 + ], + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 38 + } + } + } + ] +}; diff --git a/tests/fixtures/comments/template-string-block.src.js b/tests/fixtures/comments/template-string-block.src.js new file mode 100644 index 0000000..19351f4 --- /dev/null +++ b/tests/fixtures/comments/template-string-block.src.js @@ -0,0 +1,5 @@ +`${name}`; +{ + /* TODO comment comment comment */ + 1 + 1; +} diff --git a/tests/lib/attach-comments.js b/tests/lib/comments.js similarity index 92% rename from tests/lib/attach-comments.js rename to tests/lib/comments.js index 28120a8..178b7b6 100644 --- a/tests/lib/attach-comments.js +++ b/tests/lib/comments.js @@ -41,7 +41,7 @@ const assert = require("chai").assert, // Setup //------------------------------------------------------------------------------ -const FIXTURES_DIR = "./tests/fixtures/attach-comments"; +const FIXTURES_DIR = "./tests/fixtures/comments"; const testFiles = shelljs.find(FIXTURES_DIR) .filter(filename => filename.indexOf(".src.js") > -1) @@ -52,7 +52,7 @@ const testFiles = shelljs.find(FIXTURES_DIR) // Tests //------------------------------------------------------------------------------ -describe("attachComment: true", () => { +describe("Comments", () => { let config; @@ -61,15 +61,17 @@ describe("attachComment: true", () => { loc: true, range: true, tokens: true, - attachComment: true, - ecmaFeatures: {} + comment: true, + ecmaFeatures: { + jsx: true + } }; }); leche.withData(testFiles, filename => { const code = shelljs.cat(`${path.resolve(FIXTURES_DIR, filename)}.src.js`); - it("should produce correct AST when parsed with attachComment", () => { + it("should produce correct AST when parsed with comment", () => { const expected = require(`${path.resolve(__dirname, "../../", FIXTURES_DIR, filename)}.result.js`); let result;