Babel syntax plugin that enables parsing of Unicode line and paragraph separators in JavaScript strings
npx @tessl/cli install tessl/npm-babel--plugin-syntax-json-strings@7.8.0@babel/plugin-syntax-json-strings is a Babel syntax plugin that enables parsing of JSON strings syntax, specifically allowing Unicode line separator (U+2028) and paragraph separator (U+2029) characters within JavaScript strings. This plugin provides the syntax parsing capability required for JSON superset compliance.
npm install --save-dev @babel/plugin-syntax-json-stringsNote: This package is distributed as a standalone npm package and is not part of the main Babel monorepo source code. It follows the standard Babel syntax plugin architecture pattern.
// Default export (plugin function)
const jsonStringsPlugin = require("@babel/plugin-syntax-json-strings");ES6 import:
import jsonStringsPlugin from "@babel/plugin-syntax-json-strings";This plugin is added to your Babel configuration to enable JSON strings syntax parsing:
babel.config.js:
module.exports = {
plugins: [
"@babel/plugin-syntax-json-strings"
]
};package.json:
{
"babel": {
"plugins": ["@babel/plugin-syntax-json-strings"]
}
}Once enabled, the parser can handle JavaScript strings containing U+2028 and U+2029 characters:
// These characters become parseable in JS strings:
const lineWithLineSeparator = "Line one\u2028Line two";
const lineWithParagraphSeparator = "Paragraph one\u2029Paragraph two";Note: This is a syntax-only plugin. It enables parsing but does not transform the code. For actual transformation of these characters, use @babel/plugin-proposal-json-strings which depends on this plugin.
The main export is a Babel plugin function that configures the parser to support JSON strings syntax.
/**
* Babel plugin function that enables JSON strings syntax parsing
* @param {Object} api - Babel API object with version assertion utilities
* @returns {Object} Babel plugin configuration object
*/
export default function(api: BabelAPI): BabelPlugin;
interface BabelAPI {
/** Assert that the Babel version is compatible */
assertVersion(version: number): void;
}
interface BabelPlugin {
/** Plugin identifier */
name: "syntax-json-strings";
/** Method that modifies parser options to enable jsonStrings syntax */
manipulateOptions(opts: BabelOptions, parserOpts: ParserOptions): void;
}
interface BabelOptions {
/** Babel transformation options */
[key: string]: any;
}
interface ParserOptions {
/** Array of parser plugins to enable */
plugins: (string | [string, any])[];
}The plugin returns a configuration object that follows the standard Babel plugin pattern:
interface BabelPlugin {
/**
* Plugin identifier used by Babel
* Set to "syntax-json-strings"
*/
name: "syntax-json-strings";
/**
* Modifies parser options to enable JSON strings syntax
* Adds "jsonStrings" to the parser plugins array
* @param opts - Babel transformation options (not used by syntax plugins)
* @param parserOpts - Babel parser options object
*/
manipulateOptions(opts: BabelOptions, parserOpts: ParserOptions): void;
}The plugin requires the following dependencies:
This is a syntax-only plugin that follows the standard Babel syntax plugin pattern:
api.assertVersion(7) to ensure compatibility with Babel 7+manipulateOptions to add "jsonStrings" to the parser plugins array@babel/plugin-proposal-json-strings)The plugin enables parsing of these Unicode characters in JavaScript strings:
These characters are valid in JSON but cause syntax errors in JavaScript without this plugin.
The plugin performs Babel API version assertion during initialization. If an incompatible Babel version is detected, the plugin will throw an error during the build process.