From bfd21f5aa38c36ec2b4915b30377b6d4b3222d6a Mon Sep 17 00:00:00 2001 From: Brie Date: Sat, 17 Nov 2018 21:56:37 -0800 Subject: [PATCH] Add parsers for TypeScript and TSX --- README.md | 6 +++--- bin/jscodeshift.sh | 4 ++-- parser/ts.js | 18 ++++++++++++++++++ parser/tsOptions.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ parser/tsx.js | 21 +++++++++++++++++++++ src/getParser.js | 4 ++++ 6 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 parser/ts.js create mode 100644 parser/tsOptions.js create mode 100644 parser/tsx.js diff --git a/README.md b/README.md index adbcbd09..17570a5d 100644 --- a/README.md +++ b/README.md @@ -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 ``` @@ -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: diff --git a/bin/jscodeshift.sh b/bin/jscodeshift.sh index fe4fb4cf..08942603 100755 --- a/bin/jscodeshift.sh +++ b/bin/jscodeshift.sh @@ -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, diff --git a/parser/ts.js b/parser/ts.js new file mode 100644 index 00000000..806b5480 --- /dev/null +++ b/parser/ts.js @@ -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); +}; diff --git a/parser/tsOptions.js b/parser/tsOptions.js new file mode 100644 index 00000000..6c6b5d37 --- /dev/null +++ b/parser/tsOptions.js @@ -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' + ], +}; diff --git a/parser/tsx.js b/parser/tsx.js new file mode 100644 index 00000000..95574f64 --- /dev/null +++ b/parser/tsx.js @@ -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); +}; diff --git a/src/getParser.js b/src/getParser.js index 91ca2875..b2fc3f87 100644 --- a/src/getParser.js +++ b/src/getParser.js @@ -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');