@codemod/parser
Advanced tools
Comparing version 1.1.0 to 1.1.1
{ | ||
"name": "@codemod/parser", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Wrapper around @babel/parser that allows parsing everything.", | ||
@@ -20,6 +20,6 @@ "repository": "https://github.com/codemod-js/codemod", | ||
"dependencies": { | ||
"@babel/parser": "^7.12.5" | ||
"@babel/parser": "^7.15.3" | ||
}, | ||
"devDependencies": { | ||
"@babel/types": "^7.12.6", | ||
"@babel/types": "^7.15.0", | ||
"@types/jest": "^25.1.0", | ||
@@ -33,3 +33,3 @@ "jest": "^25.1.0", | ||
}, | ||
"gitHead": "155f32176932dd0764d591056a9d784329f1cbe6" | ||
"gitHead": "edf9f0806ed620d374d40428ab894a3878ab6401" | ||
} |
@@ -40,2 +40,16 @@ "use strict"; | ||
}); | ||
test('does not mix conflicting "recordAndTuple" and "pipelineOperator" plugins', () => { | ||
// adding recordAndTuple to existing plugins | ||
expect(__1.buildOptions({ plugins: [['pipelineOperator', { proposal: 'smart' }]] }) | ||
.plugins).not.toContainEqual(['recordAndTuple', expect.anything()]); | ||
expect(__1.buildOptions({ | ||
plugins: [['pipelineOperator', { proposal: 'hack', topicToken: '#' }]], | ||
}).plugins).not.toContainEqual(['recordAndTuple', expect.anything()]); | ||
expect(__1.buildOptions({ | ||
plugins: [['pipelineOperator', { proposal: 'hack', topicToken: '%' }]], | ||
}).plugins).toContainEqual(['recordAndTuple', { syntaxType: 'hash' }]); | ||
// adding pipelineOperator to existing plugins | ||
expect(__1.buildOptions({ plugins: [['recordAndTuple', { syntaxType: 'hash' }]] }) | ||
.plugins).toContainEqual(['pipelineOperator', { proposal: 'minimal' }]); | ||
}); | ||
test('does not mutate `plugins` array', () => { | ||
@@ -82,2 +96,4 @@ const plugins = []; | ||
#[1, 2, #{a: 3}] | ||
// demonstrate 'pipelineOperator' plugin with proposal=minimal | ||
x |> y | ||
`).program.body.map((node) => t.isExpressionStatement(node) ? node.expression.type : node.type)).toEqual([ | ||
@@ -91,2 +107,3 @@ 'ReturnStatement', | ||
'TupleExpression', | ||
'BinaryExpression', | ||
]); | ||
@@ -93,0 +110,0 @@ }); |
@@ -1,8 +0,3 @@ | ||
import { ParserOptions as BabelParserOptions, ParserPlugin as BabelParserPlugin } from '@babel/parser'; | ||
/** | ||
* Add some type plugins that are in master but not yet published. | ||
*/ | ||
export declare type ParserPlugin = BabelParserPlugin | 'importAssertions' | ['recordAndTuple', { | ||
syntaxType: 'hash' | 'bar'; | ||
}]; | ||
import { ParserOptions as BabelParserOptions, ParserPlugin } from '@babel/parser'; | ||
declare type ParserPluginName = Extract<ParserPlugin, string> | Extract<ParserPlugin, [string, object]>[0]; | ||
export interface ParserOptions extends Omit<BabelParserOptions, 'plugins'> { | ||
@@ -16,1 +11,11 @@ plugins?: Array<ParserPlugin>; | ||
export default function buildOptions({ sourceType, allowAwaitOutsideFunction, allowImportExportEverywhere, allowReturnOutsideFunction, allowSuperOutsideMethod, allowUndeclaredExports, plugins, sourceFilename, ...rest }?: ParserOptions): ParserOptions; | ||
/** | ||
* Gets the name of `plugin`. | ||
* | ||
* @example | ||
* | ||
* getPluginName('decorators'); // 'decorators' | ||
* getPluginName(['flow', { all: true }]); // 'flow' | ||
*/ | ||
export declare function getPluginName(plugin: ParserPlugin): ParserPluginName; | ||
export {}; |
@@ -40,3 +40,3 @@ "use strict"; | ||
['decorators', { decoratorsBeforeExport: true }], | ||
['pipelineOperator', { proposal: 'smart' }], | ||
['pipelineOperator', { proposal: 'minimal' }], | ||
['recordAndTuple', { syntaxType: 'hash' }], | ||
@@ -51,3 +51,3 @@ ]); | ||
for (const plugin of DefaultParserPlugins) { | ||
if (shouldAddPlugin(plugins, getPluginName(plugin))) { | ||
if (shouldAddPlugin(plugins, plugin)) { | ||
plugins = [...plugins, plugin]; | ||
@@ -71,22 +71,2 @@ } | ||
/** | ||
* Gets plugins that cannot be enabled if the plugin given by `name` is enabled. | ||
* | ||
* @see https://github.com/babel/babel/blob/5fb4d84a33351c13057dc542513a3fe2309e08b0/packages/babel-parser/src/plugin-utils.js#L44-L49 | ||
*/ | ||
function getMutuallyExclusivePluginsForPlugin(name) { | ||
switch (name) { | ||
case 'flow': | ||
case 'flowComments': | ||
return ['typescript']; | ||
case 'typescript': | ||
return ['flow', 'flowComments']; | ||
case 'decorators': | ||
return ['decorators-legacy']; | ||
case 'decorators-legacy': | ||
return ['decorators']; | ||
default: | ||
return []; | ||
} | ||
} | ||
/** | ||
* Gets the type plugin to use for a given file name. | ||
@@ -107,2 +87,14 @@ * | ||
} | ||
function getPluginOptions(plugins, name) { | ||
for (const plugin of plugins) { | ||
if (Array.isArray(plugin)) { | ||
if (plugin[0] === name) { | ||
return plugin[1]; | ||
} | ||
} | ||
else if (plugin === name) { | ||
return {}; | ||
} | ||
} | ||
} | ||
/** | ||
@@ -117,7 +109,42 @@ * Determines whether a plugin list can accept a new plugin by name. | ||
*/ | ||
function shouldAddPlugin(plugins, name) { | ||
function shouldAddPlugin(plugins, plugin) { | ||
const name = getPluginName(plugin); | ||
if (pluginListIncludesPlugin(plugins, name)) { | ||
return false; | ||
} | ||
return !getMutuallyExclusivePluginsForPlugin(name).some((mutuallyExclusivePlugin) => pluginListIncludesPlugin(plugins, mutuallyExclusivePlugin)); | ||
switch (name) { | ||
case 'flow': | ||
case 'flowComments': | ||
return !getPluginOptions(plugins, 'typescript'); | ||
case 'typescript': | ||
return !(getPluginOptions(plugins, 'flow') || | ||
getPluginOptions(plugins, 'flowComments')); | ||
case 'decorators': | ||
return !getPluginOptions(plugins, 'decorators-legacy'); | ||
case 'decorators-legacy': | ||
return !getPluginOptions(plugins, 'decorators'); | ||
case 'recordAndTuple': | ||
case 'pipelineOperator': { | ||
const recordAndTupleOptions = name === 'recordAndTuple' | ||
? plugin[1] | ||
: getPluginOptions(plugins, 'recordAndTuple'); | ||
const pipelineOperatorOptions = name === 'pipelineOperator' | ||
? plugin[1] | ||
: getPluginOptions(plugins, 'pipelineOperator'); | ||
if ((recordAndTupleOptions === null || recordAndTupleOptions === void 0 ? void 0 : recordAndTupleOptions.syntaxType) === 'hash') { | ||
// https://github.com/babel/babel/blob/15f2f171ab13b224757ca43483a456e409f12a0a/packages/babel-parser/src/plugin-utils.js#L124-L128 | ||
if ((pipelineOperatorOptions === null || pipelineOperatorOptions === void 0 ? void 0 : pipelineOperatorOptions.proposal) === 'smart') { | ||
return false; | ||
} | ||
// https://github.com/babel/babel/blob/15f2f171ab13b224757ca43483a456e409f12a0a/packages/babel-parser/src/plugin-utils.js#L119-L123 | ||
if ((pipelineOperatorOptions === null || pipelineOperatorOptions === void 0 ? void 0 : pipelineOperatorOptions.proposal) === 'hack' && | ||
pipelineOperatorOptions.topicToken === '#') { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
default: | ||
return true; | ||
} | ||
} | ||
@@ -147,2 +174,3 @@ /** | ||
} | ||
exports.getPluginName = getPluginName; | ||
//# sourceMappingURL=options.js.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35160
326
12
Updated@babel/parser@^7.15.3