Skip to content

Commit

Permalink
Support parsing typescript with babel
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Apr 22, 2019
1 parent ff33fc8 commit ec9e09c
Showing 1 changed file with 36 additions and 26 deletions.
62 changes: 36 additions & 26 deletions src/babelParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,42 @@
*/

const babel = require('@babel/core');
const path = require('path');

const defaultPlugins = [
'jsx',
'flow',
'asyncGenerators',
'bigInt',
'classProperties',
'classPrivateProperties',
'classPrivateMethods',
['decorators', { decoratorsBeforeExport: false }],
'doExpressions',
'dynamicImport',
'exportDefaultFrom',
'exportNamespaceFrom',
'functionBind',
'functionSent',
'importMeta',
'logicalAssignment',
'nullishCoalescingOperator',
'numericSeparator',
'objectRestSpread',
'optionalCatchBinding',
'optionalChaining',
['pipelineOperator', { proposal: 'minimal' }],
'throwExpressions',
];
const TYPESCRIPT_EXTS = {
'.ts': true,
'.tsx': true,
};

function getDefaultPlugins(options: BabelOptions) {
return [
'jsx',
TYPESCRIPT_EXTS[path.extname(options.filename || '')]
? 'typescript'
: 'flow',
'asyncGenerators',
'bigInt',
'classProperties',
'classPrivateProperties',
'classPrivateMethods',
['decorators', { decoratorsBeforeExport: false }],
'doExpressions',
'dynamicImport',
'exportDefaultFrom',
'exportNamespaceFrom',
'functionBind',
'functionSent',
'importMeta',
'logicalAssignment',
'nullishCoalescingOperator',
'numericSeparator',
'objectRestSpread',
'optionalCatchBinding',
'optionalChaining',
['pipelineOperator', { proposal: 'minimal' }],
'throwExpressions',
];
}

type ParserOptions = {
plugins?: Array<string | [string, {}]>,
Expand Down Expand Up @@ -73,7 +83,7 @@ function buildOptions(
const partialConfig = babel.loadPartialConfig(babelOptions);

if (!partialConfig.hasFilesystemConfig() && parserOpts.plugins.length === 0) {
parserOpts.plugins = [...defaultPlugins];
parserOpts.plugins = getDefaultPlugins(babelOptions);
}

// Recast needs tokens to be in the tree
Expand Down

0 comments on commit ec9e09c

Please sign in to comment.