mirror of
https://github.com/jsdoc/jsdoc.git
synced 2025-12-08 19:46:11 +00:00
switch to Babylon parser
This commit is contained in:
parent
a9ba41976d
commit
ffec4a4229
@ -1,23 +1,16 @@
|
||||
'use strict';
|
||||
|
||||
var espree = require('espree');
|
||||
var babylon = require('babylon');
|
||||
var logger = require('jsdoc/util/logger');
|
||||
|
||||
// exported so we can use them in tests
|
||||
var parserOptions = exports.parserOptions = {
|
||||
attachComment: true,
|
||||
comment: true,
|
||||
ecmaFeatures: {
|
||||
experimentalObjectRestSpread: true,
|
||||
globalReturn: true,
|
||||
impliedStrict: true,
|
||||
jsx: true
|
||||
},
|
||||
ecmaVersion: 7,
|
||||
loc: true,
|
||||
range: true,
|
||||
sourceType: 'module',
|
||||
tokens: true
|
||||
plugins: [
|
||||
'estree',
|
||||
'jsx',
|
||||
'objectRestSpread'
|
||||
]
|
||||
};
|
||||
|
||||
// TODO: docs
|
||||
@ -27,7 +20,7 @@ function parse(source, filename) {
|
||||
var ast;
|
||||
|
||||
try {
|
||||
ast = espree.parse(source, parserOptions);
|
||||
ast = babylon.parse(source, parserOptions);
|
||||
}
|
||||
catch (e) {
|
||||
logger.error('Unable to parse %s: %s', filename, e.message);
|
||||
|
||||
@ -29,6 +29,7 @@ exports.Syntax = {
|
||||
ExportNamedDeclaration: 'ExportNamedDeclaration',
|
||||
ExportSpecifier: 'ExportSpecifier',
|
||||
ExpressionStatement: 'ExpressionStatement',
|
||||
File: 'File',
|
||||
ForInStatement: 'ForInStatement',
|
||||
ForOfStatement: 'ForOfStatement',
|
||||
ForStatement: 'ForStatement',
|
||||
|
||||
@ -38,7 +38,7 @@ function getRawComment(comment) {
|
||||
* @return {boolean} `true` if the comment is a block comment, `false` otherwise.
|
||||
*/
|
||||
function isBlockComment(comment) {
|
||||
return comment.type === 'Block';
|
||||
return comment.type === 'CommentBlock';
|
||||
}
|
||||
|
||||
// TODO: docs
|
||||
@ -413,7 +413,8 @@ function isValidJsdoc(commentSrc) {
|
||||
// TODO: docs
|
||||
function hasJsdocComments(node) {
|
||||
return (node && node.leadingComments && node.leadingComments.length) ||
|
||||
(node && node.trailingComments && node.trailingComments.length);
|
||||
(node && node.trailingComments && node.trailingComments.length) ||
|
||||
(node && node.innerComments && node.innerComments.length);
|
||||
}
|
||||
|
||||
// TODO: docs
|
||||
@ -450,6 +451,10 @@ Visitor.prototype.visitNodeComments = function(node, parser, filename) {
|
||||
comments = comments.concat( node.trailingComments.slice(0) );
|
||||
}
|
||||
|
||||
if (node.innerComments && node.innerComments.length) {
|
||||
comments = comments.concat( node.innerComments.slice(0) );
|
||||
}
|
||||
|
||||
for (var i = 0, l = comments.length; i < l; i++) {
|
||||
comment = comments[i];
|
||||
rawComment = getRawComment(comment);
|
||||
|
||||
@ -9,6 +9,7 @@
|
||||
|
||||
var astnode = require('jsdoc/src/astnode');
|
||||
var doclet = require('jsdoc/doclet');
|
||||
var logger = require('jsdoc/util/logger');
|
||||
var Syntax = require('jsdoc/src/syntax').Syntax;
|
||||
|
||||
/**
|
||||
@ -237,6 +238,10 @@ walkers[Syntax.ExpressionStatement] = function(node, parent, state, cb) {
|
||||
cb(node.expression, node, state);
|
||||
};
|
||||
|
||||
walkers[Syntax.File] = function(node, parent, state, cb) {
|
||||
cb(node.program, node, state);
|
||||
};
|
||||
|
||||
walkers[Syntax.ForInStatement] = function(node, parent, state, cb) {
|
||||
cb(node.left, node, state);
|
||||
cb(node.right, node, state);
|
||||
@ -592,6 +597,11 @@ Walker.prototype._recurse = function(filename, ast) {
|
||||
scopes: []
|
||||
};
|
||||
|
||||
function logUnknownNodeType(node) {
|
||||
logger.debug('Found a node with unrecognized type %s. Ignoring the node and its ' +
|
||||
'descendants.', node.type);
|
||||
}
|
||||
|
||||
function cb(node, parent, cbState) {
|
||||
var currentScope;
|
||||
|
||||
@ -610,7 +620,11 @@ Walker.prototype._recurse = function(filename, ast) {
|
||||
}
|
||||
cbState.nodes.push(node);
|
||||
|
||||
self._walkers[node.type](node, parent, cbState, cb);
|
||||
if (!self._walkers[node.type]) {
|
||||
logUnknownNodeType(node);
|
||||
} else {
|
||||
self._walkers[node.type](node, parent, cbState, cb);
|
||||
}
|
||||
|
||||
if (isScope) {
|
||||
cbState.scopes.pop();
|
||||
|
||||
@ -13,10 +13,10 @@
|
||||
"url": "https://github.com/jsdoc3/jsdoc"
|
||||
},
|
||||
"dependencies": {
|
||||
"babylon": "~7.0.0-beta.16",
|
||||
"bluebird": "~3.4.6",
|
||||
"catharsis": "~0.8.8",
|
||||
"escape-string-regexp": "~1.0.5",
|
||||
"espree": "~3.1.7",
|
||||
"js2xmlparser": "~1.0.0",
|
||||
"klaw": "~1.3.0",
|
||||
"marked": "~0.3.6",
|
||||
|
||||
@ -3,46 +3,44 @@
|
||||
describe('jsdoc/src/astNode', function() {
|
||||
var astBuilder = require('jsdoc/src/astbuilder');
|
||||
var astNode = require('jsdoc/src/astnode');
|
||||
var babylon = require('babylon');
|
||||
var doop = require('jsdoc/util/doop');
|
||||
var env = require('jsdoc/env');
|
||||
var espree = require('espree');
|
||||
var Syntax = require('jsdoc/src/syntax').Syntax;
|
||||
|
||||
function parse(str) {
|
||||
return espree.parse(str, astBuilder.parserOptions);
|
||||
return babylon.parse(str, astBuilder.parserOptions).program.body[0];
|
||||
}
|
||||
|
||||
// create the AST nodes we'll be testing
|
||||
var arrayExpression = parse('[,]').body[0].expression;
|
||||
var arrowFunctionExpression = parse('var foo = () => {};').body[0].declarations[0].init;
|
||||
var assignmentExpression = parse('foo = 1;').body[0].expression;
|
||||
var binaryExpression = parse('foo & foo;').body[0].expression;
|
||||
var functionDeclaration1 = parse('function foo() {}').body[0];
|
||||
var functionDeclaration1a = parse('function bar() {}').body[0];
|
||||
var functionDeclaration2 = parse('function foo(bar) {}').body[0];
|
||||
var functionDeclaration3 = parse('function foo(bar, baz, qux) {}').body[0];
|
||||
var functionDeclaration4 = parse('function foo(...bar) {}').body[0];
|
||||
var functionExpression1 = parse('var foo = function() {};').body[0].declarations[0].init;
|
||||
var functionExpression2 = parse('var foo = function(bar) {};').body[0].declarations[0].init;
|
||||
var identifier = parse('foo;').body[0].expression;
|
||||
var literal = parse('1;').body[0].expression;
|
||||
var memberExpression = parse('foo.bar;').body[0].expression;
|
||||
var memberExpressionComputed1 = parse('foo["bar"];').body[0].expression;
|
||||
var memberExpressionComputed2 = parse('foo[\'bar\'];').body[0].expression;
|
||||
var methodDefinition = parse('class Foo { bar() {} }').body[0].body.body[0];
|
||||
var propertyGet = parse('var foo = { get bar() {} };').body[0].declarations[0].init
|
||||
.properties[0];
|
||||
var propertyInit = parse('var foo = { bar: {} };').body[0].declarations[0].init.properties[0];
|
||||
var propertySet = parse('var foo = { set bar(a) {} };').body[0].declarations[0].init
|
||||
.properties[0];
|
||||
var thisExpression = parse('this;').body[0].expression;
|
||||
var unaryExpression1 = parse('+1;').body[0].expression;
|
||||
var unaryExpression2 = parse('+foo;').body[0].expression;
|
||||
var variableDeclaration1 = parse('var foo = 1;').body[0];
|
||||
var variableDeclaration2 = parse('var foo = 1, bar = 2;').body[0];
|
||||
var variableDeclarator1 = parse('var foo = 1;').body[0].declarations[0];
|
||||
var variableDeclarator2 = parse('var foo;').body[0].declarations[0];
|
||||
var experimentalObjectRestSpread = parse('var one = {...two, three: 4};').body[0].declarations[0].init;
|
||||
var arrayExpression = parse('[,]').expression;
|
||||
var arrowFunctionExpression = parse('var foo = () => {};').declarations[0].init;
|
||||
var assignmentExpression = parse('foo = 1;').expression;
|
||||
var binaryExpression = parse('foo & foo;').expression;
|
||||
var experimentalObjectRestSpread = parse('var one = {...two, three: 4};').declarations[0].init;
|
||||
var functionDeclaration1 = parse('function foo() {}');
|
||||
var functionDeclaration1a = parse('function bar() {}');
|
||||
var functionDeclaration2 = parse('function foo(bar) {}');
|
||||
var functionDeclaration3 = parse('function foo(bar, baz, qux) {}');
|
||||
var functionDeclaration4 = parse('function foo(...bar) {}');
|
||||
var functionExpression1 = parse('var foo = function() {};').declarations[0].init;
|
||||
var functionExpression2 = parse('var foo = function(bar) {};').declarations[0].init;
|
||||
var identifier = parse('foo;').expression;
|
||||
var literal = parse('1;').expression;
|
||||
var memberExpression = parse('foo.bar;').expression;
|
||||
var memberExpressionComputed1 = parse('foo["bar"];').expression;
|
||||
var memberExpressionComputed2 = parse('foo[\'bar\'];').expression;
|
||||
var methodDefinition = parse('class Foo { bar() {} }').body.body[0];
|
||||
var propertyGet = parse('var foo = { get bar() {} };').declarations[0].init.properties[0];
|
||||
var propertyInit = parse('var foo = { bar: {} };').declarations[0].init.properties[0];
|
||||
var propertySet = parse('var foo = { set bar(a) {} };').declarations[0].init.properties[0];
|
||||
var thisExpression = parse('this;').expression;
|
||||
var unaryExpression1 = parse('+1;').expression;
|
||||
var unaryExpression2 = parse('+foo;').expression;
|
||||
var variableDeclaration1 = parse('var foo = 1;');
|
||||
var variableDeclaration2 = parse('var foo = 1, bar = 2;');
|
||||
var variableDeclarator1 = parse('var foo = 1;').declarations[0];
|
||||
var variableDeclarator2 = parse('var foo;').declarations[0];
|
||||
|
||||
it('should exist', function() {
|
||||
expect(typeof astNode).toBe('object');
|
||||
|
||||
@ -7,18 +7,4 @@ describe('jsdoc/src/syntax', function() {
|
||||
expect(Syntax).toBeDefined();
|
||||
expect(typeof Syntax).toBe('object');
|
||||
});
|
||||
|
||||
it('should define all of the node types that are defined by Espree', function() {
|
||||
var espreeSyntax = require('espree').Syntax;
|
||||
|
||||
Object.keys(espreeSyntax).forEach(function(nodeType) {
|
||||
expect(Syntax[nodeType]).toBeDefined();
|
||||
expect(Syntax[nodeType]).toBe(espreeSyntax[nodeType]);
|
||||
});
|
||||
});
|
||||
|
||||
it('should define the LetStatement node type', function() {
|
||||
expect(Syntax.LetStatement).toBeDefined();
|
||||
expect(Syntax.LetStatement).toBe('LetStatement');
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user