Skip to content

Commit

Permalink
Add parsers for TypeScript and TSX
Browse files Browse the repository at this point in the history
  • Loading branch information
Brie committed Nov 18, 2018
1 parent 595b15c commit f51472c
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 5 deletions.
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);
};
43 changes: 43 additions & 0 deletions parser/tsOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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',
'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'] });