A Babel syntax plugin that enables parsing of do expressions in JavaScript code
npx @tessl/cli install tessl/npm-babel--plugin-syntax-do-expressions@7.27.0@babel/plugin-syntax-do-expressions is a Babel syntax plugin that enables parsing of do expressions in JavaScript code. This is a minimal syntax-only plugin that does not transform code - it only enables the Babel parser to recognize and parse do expression syntax.
Do expressions are a proposed JavaScript language feature that allow using block statements as expressions, where the value of the last statement in the block becomes the value of the expression.
npm install --save-dev @babel/plugin-syntax-do-expressions// ES modules (default export)
import syntaxDoExpressions from "@babel/plugin-syntax-do-expressions";// CommonJS
const syntaxDoExpressions = require("@babel/plugin-syntax-do-expressions");This plugin is typically used in Babel configuration files rather than being imported directly in application code:
{
"plugins": ["@babel/plugin-syntax-do-expressions"]
}With programmatic Babel API:
import { transform } from "@babel/core";
import syntaxDoExpressions from "@babel/plugin-syntax-do-expressions";
const result = transform(code, {
plugins: [syntaxDoExpressions]
});The main export is a Babel plugin function created by the declare() helper from @babel/helper-plugin-utils.
/**
* Default export - Babel plugin function for syntax-do-expressions
* This is the result of calling declare() from @babel/helper-plugin-utils
* @param api - Babel plugin API providing version assertion and utilities
* @param options - Plugin options (not used by this plugin)
* @param dirname - Directory name (not used by this plugin)
* @returns Babel plugin configuration object
*/
declare const syntaxDoExpressions: (
api: PluginAPI,
options?: object,
dirname?: string
) => BabelPlugin;
export default syntaxDoExpressions;
interface PluginAPI {
assertVersion(range: number | string): void;
// Additional Babel API methods available but not used by this plugin
}
interface BabelPlugin {
name: string;
manipulateOptions(opts: any, parserOpts: ParserOptions): void;
}
interface ParserOptions {
plugins: string[];
// Additional parser options available
}The plugin object returned contains:
"syntax-do-expressions" - Plugin identifier"doExpressions" to the parser plugins arrayThe plugin's manipulateOptions method is called during Babel's parsing phase.
/**
* Babel plugin lifecycle method that modifies parser options
* @param opts - Babel transformation options (unused by this plugin)
* @param parserOpts - Parser configuration object to modify
*/
manipulateOptions(opts: any, parserOpts: ParserOptions): void;This method adds the "doExpressions" string to the parserOpts.plugins array, enabling the Babel parser to recognize do expression syntax.
Once this plugin is enabled, the Babel parser can recognize do expression syntax like:
// Do expression syntax that becomes parseable
const value = do {
if (condition) {
"success";
} else {
"failure";
}
};
const result = do {
const temp = compute();
temp * 2;
};@babel/core version ^7.0.0-0@babel/helper-plugin-utils for plugin creation utilities>=6.9.0This plugin does not throw custom errors. Any errors that occur are typically:
@babel/core compatibility checksCommonly used with other Babel plugins and presets:
{
"presets": ["@babel/preset-env"],
"plugins": [
"@babel/plugin-syntax-do-expressions",
"@babel/plugin-proposal-do-expressions"
]
}Note: This syntax plugin only enables parsing. To actually transform do expressions for older JavaScript environments, use @babel/plugin-proposal-do-expressions alongside this plugin.