Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parsers for TypeScript and TSX #286

Merged
merged 1 commit into from
Dec 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Options:
--ignore-config FILE Ignore files if they match patterns sourced from a configuration file (e.g., a .gitignore)
--run-in-band Run serially in the current process [false]
-s, --silent No output [false]
--parser The parser to use for parsing your source files (babel | babylon | flow) [babel]
--parser The parser to use for parsing your source files (babel | babylon | flow | ts | tsx) [babel]
--version print version and exit
```

Expand Down Expand Up @@ -144,8 +144,8 @@ The transform can let jscodeshift know with which parser to parse the source
files (and features like templates).

To do that, the transform module can export `parser`, which can either be one
of the strings `"babel"`, `"babylon"`, or `"flow"`, or it can be a parser
object that is compatible with recast.
of the strings `"babel"`, `"babylon"`, `"flow"`, `"ts"`, or `"tsx"`,
or it can be a parser object that is compatible with recast.

For example:

Expand Down
4 changes: 2 additions & 2 deletions bin/jscodeshift.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ const opts = require('nomnom')
help: 'No output'
},
parser: {
choices: ['babel', 'babylon', 'flow'],
choices: ['babel', 'babylon', 'flow', 'ts', 'tsx'],
default: 'babel',
full: 'parser',
help: 'The parser to use for parsing your source files (babel | babylon | flow)'
help: 'The parser to use for parsing your source files (babel | babylon | flow | ts | tsx)'
},
version: {
flag: true,
Expand Down
18 changes: 18 additions & 0 deletions parser/ts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

'use strict';

const babylon = require('babylon');
const options = require('./tsOptions');

exports.parse = function parse (code) {
return babylon.parse(code, options);
};
44 changes: 44 additions & 0 deletions parser/tsOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

'use strict';

/**
* Options shared by the TypeScript and TSX parsers.
*/
module.exports = {
sourceType: 'module',
allowImportExportEverywhere: true,
allowReturnOutsideFunction: true,
plugins: [
'asyncGenerators',
'bigInt',
'classPrivateMethods',
'classPrivateProperties',
'classProperties',
'decorators-legacy',
'doExpressions',
'dynamicImport',
'exportDefaultFrom',
'exportExtensions',
'exportNamespaceFrom',
'functionBind',
'functionSent',
'importMeta',
'nullishCoalescingOperator',
'numericSeparator',
'objectRestSpread',
'optionalCatchBinding',
'optionalChaining',
['pipelineOperator', { proposal: 'minimal' }],
'throwExpressions',
'typescript'
],
};
21 changes: 21 additions & 0 deletions parser/tsx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

'use strict';

const _ = require('lodash');
const babylon = require('babylon');
const baseOptions = require('./tsOptions');

const options = _.merge(baseOptions, { plugins: ['jsx'] });

exports.parse = function parse (code) {
return babylon.parse(code, options);
};
4 changes: 4 additions & 0 deletions src/getParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ module.exports = function getParser(parserName) {
return require('../parser/babylon');
case 'flow':
return require('../parser/flow');
case 'ts':
return require('../parser/ts');
case 'tsx':
return require('../parser/tsx');
case 'babel':
default:
return require('../parser/babel5Compat');
Expand Down