Skip to content

Commit

Permalink
feat(ecmascript-proposal): split to plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
h13i32maru committed Feb 28, 2017
1 parent de4f78c commit df79107
Show file tree
Hide file tree
Showing 17 changed files with 12 additions and 225 deletions.
14 changes: 6 additions & 8 deletions src/ESDoc.js
Expand Up @@ -74,7 +74,7 @@ export default class ESDoc {
}

console.log(`parse: ${filePath}`);
const temp = this._traverse(config, config.source, filePath, packageName, mainFilePath);
const temp = this._traverse(config.source, filePath, packageName, mainFilePath);
if (!temp) return;
results.push(...temp.results);

Expand Down Expand Up @@ -162,7 +162,7 @@ export default class ESDoc {
}

console.log(`parse: ${filePath}`);
const temp = this._traverseForTest(config, config.test.type, config.test.source, filePath);
const temp = this._traverseForTest(config.test.type, config.test.source, filePath);
if (!temp) return;
results.push(...temp.results);

Expand Down Expand Up @@ -224,7 +224,6 @@ export default class ESDoc {

/**
* traverse doc comment in JavaScript file.
* @param {ESDocConfig} config - config of esdoc.
* @param {string} inDirPath - root directory path.
* @param {string} filePath - target JavaScript file path.
* @param {string} [packageName] - npm package name of target.
Expand All @@ -234,11 +233,11 @@ export default class ESDoc {
* @property {AST} ast - this is AST of JavaScript file.
* @private
*/
static _traverse(config, inDirPath, filePath, packageName, mainFilePath) {
static _traverse(inDirPath, filePath, packageName, mainFilePath) {
logger.i(`parsing: ${filePath}`);
let ast;
try {
ast = ESParser.parse(config, filePath);
ast = ESParser.parse(filePath);
} catch (e) {
InvalidCodeLogger.showFile(filePath, e);
return null;
Expand All @@ -261,7 +260,6 @@ export default class ESDoc {

/**
* traverse doc comment in test code file.
* @param {ESDocConfig} config - config of esdoc.
* @param {string} type - test code type.
* @param {string} inDirPath - root directory path.
* @param {string} filePath - target test code file path.
Expand All @@ -270,10 +268,10 @@ export default class ESDoc {
* @property {AST} ast - this is AST of test code.
* @private
*/
static _traverseForTest(config, type, inDirPath, filePath) {
static _traverseForTest(type, inDirPath, filePath) {
let ast;
try {
ast = ESParser.parse(config, filePath);
ast = ESParser.parse(filePath);
} catch (e) {
InvalidCodeLogger.showFile(filePath, e);
return null;
Expand Down
48 changes: 3 additions & 45 deletions src/Parser/ESParser.js
Expand Up @@ -11,67 +11,25 @@ import * as babylon from 'babylon';
export default class ESParser {
/**
* parse ECMAScript source code.
* @param {ESDocConfig} config - config of esdoc.
* @param {string} filePath - source code file path.
* @returns {AST} AST of source code.
*/
static parse(config, filePath) {
return this._parseWithBabylon(config, filePath);
}

/**
* parse ECMAScript source code with babylon.
* @param {ESDocConfig} config - config of esdoc.
* @param {string} filePath - source code file path.
* @returns {AST} AST of source code.
*/
static _parseWithBabylon(config, filePath) {
static parse(filePath) {
let code = fs.readFileSync(filePath, {encode: 'utf8'}).toString();
code = Plugin.onHandleCode(code, filePath);
if (code.charAt(0) === '#') code = code.replace(/^#!/, '//');

const option = this._buildParserOptionForBabylon(config);

let option = {sourceType: 'module', plugins: []};
let parser = (code) => {
return babylon.parse(code, option);
};

parser = Plugin.onHandleCodeParser(parser, option, filePath, code);
({parser, option} = Plugin.onHandleCodeParser(parser, option, filePath, code));

let ast = parser(code);

ast = Plugin.onHandleAST(ast, filePath, code);

return ast;
}

/**
* build babylon option.
* @param {ESDocConfig} config - config of esdoc
* @returns {{sourceType: string, plugins: string[]}} option of babylon.
* @private
*/
static _buildParserOptionForBabylon(config) {
const option = {
sourceType: 'module',
plugins: ['jsx']
};

const experimental = config.experimentalProposal;

if (experimental) {
if (experimental.classProperties) option.plugins.push('classProperties');
if (experimental.objectRestSpread) option.plugins.push('objectRestSpread');
if (experimental.doExpressions) option.plugins.push('doExpressions');
if (experimental.functionBind) option.plugins.push('functionBind');
if (experimental.functionSent) option.plugins.push('functionSent');
if (experimental.asyncGenerators) option.plugins.push('asyncGenerators');
if (experimental.asyncGenerators) option.plugins.push('asyncGenerators');
if (experimental.decorators) option.plugins.push('decorators');
if (experimental.exportExtensions) option.plugins.push('exportExtensions');
if (experimental.dynamicImport) option.plugins.push('dynamicImport');
}

return option;
}
}
6 changes: 3 additions & 3 deletions src/Plugin/Plugin.js
Expand Up @@ -82,16 +82,16 @@ class Plugin {
/**
* handle code parser.
* @param {function(code: string)} parser - original js parser.
* @param {object} option - default Espree options.
* @param {object} option - default babylon options.
* @param {string} filePath - source code file path.
* @param {string} code - original code.
* @returns {function(code: string)} handled parser.
* @returns {{parser: function(code: string), option: Object}} handled parser.
*/
onHandleCodeParser(parser, option, filePath, code) {
const ev = new PluginEvent();
ev.data = {parser, option, filePath, code};
this._execHandler('onHandleCodeParser', ev);
return ev.data.parser;
return {parser: ev.data.parser, option: ev.data.option};
}

/**
Expand Down
5 changes: 0 additions & 5 deletions test/fixture/package/esdoc.json
Expand Up @@ -28,10 +28,5 @@
"advanced": ["./test/fixture/package/manual/advanced.md"],
"faq": ["./test/fixture/package/manual/faq.md"],
"changelog": ["./test/fixture/package/CHANGELOG.md"]
},
"experimentalProposal": {
"classProperties": true,
"objectRestSpread": true,
"decorators": true
}
}
17 changes: 0 additions & 17 deletions test/fixture/package/src/ClassProperty/Definition.js

This file was deleted.

42 changes: 0 additions & 42 deletions test/fixture/package/src/Decorator/Definition.js

This file was deleted.

2 changes: 0 additions & 2 deletions test/fixture/package/src/Guess/Param.js
Expand Up @@ -18,7 +18,5 @@ export default class TestGuessParam {

method8(p1 = new Foo()){}

method9({x, y, ...z}){}

method10([p1, p2 = 10, p3 = null, p4 = 'text', p5 = v]){}
}
6 changes: 0 additions & 6 deletions test/fixture/package/src/Guess/Return.js
Expand Up @@ -17,10 +17,4 @@ export default class TestGuessReturn {
method4(){
return `text`;
}

method5(){
const a = 1;
const obj = {};
return {a, ...obj};
}
}
11 changes: 0 additions & 11 deletions test/fixture/package/src/JSX/Definition.js

This file was deleted.

10 changes: 0 additions & 10 deletions test/fixture/package/src/Type/Spread.js
Expand Up @@ -7,14 +7,4 @@ export default class TestTypeSpread {
* @param {...number} p1 - this is spread number p1.
*/
method1(...p1){}

/**
* this is method2.
* @param {Object} config - this is config.
* @param {number} config.x - this is number x.
* @param {string} config.y - this is string y.
* @param {number[]} config.a - thi is number[] a.
* @param {string[]} config.b - thi is number[] b.
*/
method2({x, y, ...z}){}
}
10 changes: 0 additions & 10 deletions test/fixture/syntax/AsyncGenerators.js

This file was deleted.

11 changes: 0 additions & 11 deletions test/fixture/syntax/DoExpressions.js

This file was deleted.

5 changes: 0 additions & 5 deletions test/fixture/syntax/DynamicImport.js

This file was deleted.

4 changes: 0 additions & 4 deletions test/fixture/syntax/ExportExtensions.js

This file was deleted.

6 changes: 0 additions & 6 deletions test/fixture/syntax/FunctionBind.js

This file was deleted.

5 changes: 0 additions & 5 deletions test/fixture/syntax/FunctionSent.js

This file was deleted.

35 changes: 0 additions & 35 deletions test/src/ParserTest/ESParserTest.js

This file was deleted.

0 comments on commit df79107

Please sign in to comment.