Subsume json (#7985)
* Add Subsume JSON transform https://github.com/tc39/proposal-json-superset is at stage 3. This allows U+2028 LINE SEPARATOR and U+2029 PARAGRAPH SEPARATOR to appear unescaped inside strings and directives. * Move to Stage 3 * Break diretive parsing * Update README * Handle multi-escape sequences * Remove babylon files after rename
This commit is contained in:
3
packages/babel-plugin-proposal-json-strings/.npmignore
Normal file
3
packages/babel-plugin-proposal-json-strings/.npmignore
Normal file
@@ -0,0 +1,3 @@
|
||||
src
|
||||
test
|
||||
*.log
|
||||
54
packages/babel-plugin-proposal-json-strings/README.md
Normal file
54
packages/babel-plugin-proposal-json-strings/README.md
Normal file
@@ -0,0 +1,54 @@
|
||||
# @babel/plugin-syntax-json-strings
|
||||
|
||||
Allow parsing of the U+2028 LINE SEPARATOR and U+2029 PARAGRAPH SEPARATOR in JS strings
|
||||
|
||||
## Examples
|
||||
|
||||
**In**
|
||||
|
||||
```js
|
||||
const ex = "before
after";
|
||||
// ^ There's a U+2028 char between 'before' and 'after'
|
||||
```
|
||||
|
||||
**Out**
|
||||
|
||||
```js
|
||||
const ex = "before\u2028after";
|
||||
// ^ There's a U+2028 char between 'before' and 'after'
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
```sh
|
||||
npm install --save-dev @babel/plugin-proposal-json-strings
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Via `.babelrc` (Recommended)
|
||||
|
||||
**.babelrc**
|
||||
|
||||
```json
|
||||
{
|
||||
"plugins": ["@babel/plugin-proposal-json-strings"]
|
||||
}
|
||||
```
|
||||
|
||||
### Via CLI
|
||||
|
||||
```sh
|
||||
babel --plugins @babel/plugin-proposal-json-strings script.js
|
||||
```
|
||||
|
||||
### Via Node API
|
||||
|
||||
```javascript
|
||||
require("@babel/core").transform("code", {
|
||||
plugins: ["@babel/plugin-proposal-json-strings"]
|
||||
});
|
||||
```
|
||||
|
||||
## References
|
||||
- [Proposal: Optional Catch Binding for ECMAScript](https://github.com/babel/proposals/issues/7)
|
||||
22
packages/babel-plugin-proposal-json-strings/package.json
Normal file
22
packages/babel-plugin-proposal-json-strings/package.json
Normal file
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "@babel/plugin-proposal-json-strings",
|
||||
"version": "7.0.0-beta.47",
|
||||
"description": "Escape U+2028 LINE SEPARATOR and U+2029 PARAGRAPH SEPARATOR in JS strings",
|
||||
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-proposal-json-strings",
|
||||
"license": "MIT",
|
||||
"main": "lib/index.js",
|
||||
"keywords": [
|
||||
"babel-plugin"
|
||||
],
|
||||
"dependencies": {
|
||||
"@babel/helper-plugin-utils": "7.0.0-beta.47",
|
||||
"@babel/plugin-syntax-json-strings": "7.0.0-beta.47"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@babel/core": "7.0.0-beta.47"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "7.0.0-beta.47",
|
||||
"@babel/helper-plugin-test-runner": "7.0.0-beta.47"
|
||||
}
|
||||
}
|
||||
29
packages/babel-plugin-proposal-json-strings/src/index.js
Normal file
29
packages/babel-plugin-proposal-json-strings/src/index.js
Normal file
@@ -0,0 +1,29 @@
|
||||
import { declare } from "@babel/helper-plugin-utils";
|
||||
import syntaxJsonStrings from "@babel/plugin-syntax-json-strings";
|
||||
|
||||
export default declare(api => {
|
||||
api.assertVersion(7);
|
||||
const regex = /(\\*)([\u2028\u2029])/g;
|
||||
function replace(match, escapes, separator) {
|
||||
// If there's an odd number, that means the separator itself was escaped.
|
||||
// "\X" escapes X.
|
||||
// "\\X" escapes the backslash, so X is unescaped.
|
||||
const isEscaped = escapes.length % 2 === 1;
|
||||
if (isEscaped) return match;
|
||||
|
||||
return `${escapes}\\u${separator.charCodeAt(0).toString(16)}`;
|
||||
}
|
||||
|
||||
return {
|
||||
inherits: syntaxJsonStrings,
|
||||
|
||||
visitor: {
|
||||
"DirectiveLiteral|StringLiteral"({ node }) {
|
||||
const { extra } = node;
|
||||
if (!extra || !extra.raw) return;
|
||||
|
||||
extra.raw = extra.raw.replace(regex, replace);
|
||||
},
|
||||
},
|
||||
};
|
||||
});
|
||||
@@ -0,0 +1,5 @@
|
||||
"before
after";
|
||||
// ^ That's a U+2028 LINE SEPARATOR UTF-16 char (between 'before' and 'after')
|
||||
"
"; // Just a U+2028 LINE SEPARATOR
|
||||
"\
"; // Escaped U+2028 LINE SEPARATOR
|
||||
"\u2028"; // Escaped U+2028 LINE SEPARATOR
|
||||
@@ -0,0 +1,5 @@
|
||||
"before
after";
|
||||
// ^ That's a U+2028 LINE SEPARATOR UTF-16 char (between 'before' and 'after')
|
||||
"
"; // Just a U+2028 LINE SEPARATOR
|
||||
"\
"; // Escaped U+2028 LINE SEPARATOR
|
||||
"\u2028"; // Escaped U+2028 LINE SEPARATOR
|
||||
@@ -0,0 +1,7 @@
|
||||
"before\u2028after"; // ^ That's a U+2028 LINE SEPARATOR UTF-16 char (between 'before' and 'after')
|
||||
|
||||
"\u2028"; // Just a U+2028 LINE SEPARATOR
|
||||
|
||||
"\
"; // Escaped U+2028 LINE SEPARATOR
|
||||
|
||||
"\u2028"; // Escaped U+2028 LINE SEPARATOR
|
||||
@@ -0,0 +1,5 @@
|
||||
"before
after";
|
||||
// ^ That's a U+2029 PARAGRAPH SEPARATOR UTF-16 char (between 'before' and 'after')
|
||||
"
"; // Just a U+2029 PARAGRAPH SEPARATOR
|
||||
"\
"; // Escaped U+2029 PARAGRAPH SEPARATOR
|
||||
"\u2029"; // Escaped U+2029 PARAGRAPH SEPARATOR
|
||||
@@ -0,0 +1,5 @@
|
||||
"before
after";
|
||||
// ^ That's a U+2029 PARAGRAPH SEPARATOR UTF-16 char (between 'before' and 'after')
|
||||
"
"; // Just a U+2029 PARAGRAPH SEPARATOR
|
||||
"\
"; // Escaped U+2029 PARAGRAPH SEPARATOR
|
||||
"\u2029"; // Escaped U+2029 PARAGRAPH SEPARATOR
|
||||
@@ -0,0 +1,7 @@
|
||||
"before\u2029after"; // ^ That's a U+2029 PARAGRAPH SEPARATOR UTF-16 char (between 'before' and 'after')
|
||||
|
||||
"\u2029"; // Just a U+2029 PARAGRAPH SEPARATOR
|
||||
|
||||
"\
"; // Escaped U+2029 PARAGRAPH SEPARATOR
|
||||
|
||||
"\u2029"; // Escaped U+2029 PARAGRAPH SEPARATOR
|
||||
@@ -0,0 +1,27 @@
|
||||
expect("\u2028".length).toBe(1);
|
||||
expect("\u2028").toBe("\u2028");
|
||||
|
||||
|
||||
expect("before
after".length).toBe(12);
|
||||
expect("before
after").toBe("before\u2028after");
|
||||
// ^ That's a U+2028 LINE SEPARATOR UTF-16 char (between 'before' and 'after')
|
||||
|
||||
expect("
".length).toBe(1);
|
||||
expect("
").toBe("\u2028");
|
||||
// ^ That's a U+2028 LINE SEPARATOR UTF-16 char
|
||||
|
||||
expect("\
".length).toBe(0);
|
||||
expect("\
").toBe("");
|
||||
// ^ That's a U+2028 LINE SEPARATOR UTF-16 char
|
||||
|
||||
expect("\\
".length).toBe(2);
|
||||
expect("\\
").toBe("\\\u2028");
|
||||
// ^ That's a U+2028 LINE SEPARATOR UTF-16 char
|
||||
|
||||
expect("\\\
".length).toBe(1);
|
||||
expect("\\\
").toBe("\\");
|
||||
// ^ That's a U+2028 LINE SEPARATOR UTF-16 char
|
||||
|
||||
expect("\\\\
".length).toBe(3);
|
||||
expect("\\\\
").toBe("\\\\\u2028");
|
||||
// ^ That's a U+2028 LINE SEPARATOR UTF-16 char
|
||||
@@ -0,0 +1,27 @@
|
||||
expect("\u2028".length).toBe(1);
|
||||
expect("\u2028").toBe("\u2028");
|
||||
|
||||
|
||||
expect("before
after".length).toBe(12);
|
||||
expect("before
after").toBe("before\u2028after");
|
||||
// ^ That's a U+2028 LINE SEPARATOR UTF-16 char (between 'before' and 'after')
|
||||
|
||||
expect("
".length).toBe(1);
|
||||
expect("
").toBe("\u2028");
|
||||
// ^ That's a U+2028 LINE SEPARATOR UTF-16 char
|
||||
|
||||
expect("\
".length).toBe(0);
|
||||
expect("\
").toBe("");
|
||||
// ^ That's a U+2028 LINE SEPARATOR UTF-16 char
|
||||
|
||||
expect("\\
".length).toBe(2);
|
||||
expect("\\
").toBe("\\\u2028");
|
||||
// ^ That's a U+2028 LINE SEPARATOR UTF-16 char
|
||||
|
||||
expect("\\\
".length).toBe(1);
|
||||
expect("\\\
").toBe("\\");
|
||||
// ^ That's a U+2028 LINE SEPARATOR UTF-16 char
|
||||
|
||||
expect("\\\\
".length).toBe(3);
|
||||
expect("\\\\
").toBe("\\\\\u2028");
|
||||
// ^ That's a U+2028 LINE SEPARATOR UTF-16 char
|
||||
@@ -0,0 +1,19 @@
|
||||
expect("\u2028".length).toBe(1);
|
||||
expect("\u2028").toBe("\u2028");
|
||||
expect("before\u2028after".length).toBe(12);
|
||||
expect("before\u2028after").toBe("before\u2028after"); // ^ That's a U+2028 LINE SEPARATOR UTF-16 char (between 'before' and 'after')
|
||||
|
||||
expect("\u2028".length).toBe(1);
|
||||
expect("\u2028").toBe("\u2028"); // ^ That's a U+2028 LINE SEPARATOR UTF-16 char
|
||||
|
||||
expect("\
".length).toBe(0);
|
||||
expect("\
").toBe(""); // ^ That's a U+2028 LINE SEPARATOR UTF-16 char
|
||||
|
||||
expect("\\\u2028".length).toBe(2);
|
||||
expect("\\\u2028").toBe("\\\u2028"); // ^ That's a U+2028 LINE SEPARATOR UTF-16 char
|
||||
|
||||
expect("\\\
".length).toBe(1);
|
||||
expect("\\\
").toBe("\\"); // ^ That's a U+2028 LINE SEPARATOR UTF-16 char
|
||||
|
||||
expect("\\\\\u2028".length).toBe(3);
|
||||
expect("\\\\\u2028").toBe("\\\\\u2028"); // ^ That's a U+2028 LINE SEPARATOR UTF-16 char
|
||||
@@ -0,0 +1,27 @@
|
||||
expect("\u2029".length).toBe(1);
|
||||
expect("\u2029").toBe("\u2029");
|
||||
|
||||
|
||||
expect("before
after".length).toBe(12);
|
||||
expect("before
after").toBe("before\u2029after");
|
||||
// ^ That's a U+2029 PARAGRAPH SEPARATOR UTF-16 char (between 'before' and 'after')
|
||||
|
||||
expect("
".length).toBe(1);
|
||||
expect("
").toBe("\u2029");
|
||||
// ^ That's a U+2029 PARAGRAPH SEPARATOR UTF-16 char
|
||||
|
||||
expect("\
".length).toBe(0);
|
||||
expect("\
").toBe("");
|
||||
// ^ That's a U+2029 PARAGRAPH SEPARATOR UTF-16 char
|
||||
|
||||
expect("\\
".length).toBe(2);
|
||||
expect("\\
").toBe("\\\u2029");
|
||||
// ^ That's a U+2029 PARAGRAPH SEPARATOR UTF-16 char
|
||||
|
||||
expect("\\\
".length).toBe(1);
|
||||
expect("\\\
").toBe("\\");
|
||||
// ^ That's a U+2029 PARAGRAPH SEPARATOR UTF-16 char
|
||||
|
||||
expect("\\\\
".length).toBe(3);
|
||||
expect("\\\\
").toBe("\\\\\u2029");
|
||||
// ^ That's a U+2029 PARAGRAPH SEPARATOR UTF-16 char
|
||||
@@ -0,0 +1,27 @@
|
||||
expect("\u2029".length).toBe(1);
|
||||
expect("\u2029").toBe("\u2029");
|
||||
|
||||
|
||||
expect("before
after".length).toBe(12);
|
||||
expect("before
after").toBe("before\u2029after");
|
||||
// ^ That's a U+2029 PARAGRAPH SEPARATOR UTF-16 char (between 'before' and 'after')
|
||||
|
||||
expect("
".length).toBe(1);
|
||||
expect("
").toBe("\u2029");
|
||||
// ^ That's a U+2029 PARAGRAPH SEPARATOR UTF-16 char
|
||||
|
||||
expect("\
".length).toBe(0);
|
||||
expect("\
").toBe("");
|
||||
// ^ That's a U+2029 PARAGRAPH SEPARATOR UTF-16 char
|
||||
|
||||
expect("\\
".length).toBe(2);
|
||||
expect("\\
").toBe("\\\u2029");
|
||||
// ^ That's a U+2029 PARAGRAPH SEPARATOR UTF-16 char
|
||||
|
||||
expect("\\\
".length).toBe(1);
|
||||
expect("\\\
").toBe("\\");
|
||||
// ^ That's a U+2029 PARAGRAPH SEPARATOR UTF-16 char
|
||||
|
||||
expect("\\\\
".length).toBe(3);
|
||||
expect("\\\\
").toBe("\\\\\u2029");
|
||||
// ^ That's a U+2029 PARAGRAPH SEPARATOR UTF-16 char
|
||||
@@ -0,0 +1,19 @@
|
||||
expect("\u2029".length).toBe(1);
|
||||
expect("\u2029").toBe("\u2029");
|
||||
expect("before\u2029after".length).toBe(12);
|
||||
expect("before\u2029after").toBe("before\u2029after"); // ^ That's a U+2029 PARAGRAPH SEPARATOR UTF-16 char (between 'before' and 'after')
|
||||
|
||||
expect("\u2029".length).toBe(1);
|
||||
expect("\u2029").toBe("\u2029"); // ^ That's a U+2029 PARAGRAPH SEPARATOR UTF-16 char
|
||||
|
||||
expect("\
".length).toBe(0);
|
||||
expect("\
").toBe(""); // ^ That's a U+2029 PARAGRAPH SEPARATOR UTF-16 char
|
||||
|
||||
expect("\\\u2029".length).toBe(2);
|
||||
expect("\\\u2029").toBe("\\\u2029"); // ^ That's a U+2029 PARAGRAPH SEPARATOR UTF-16 char
|
||||
|
||||
expect("\\\
".length).toBe(1);
|
||||
expect("\\\
").toBe("\\"); // ^ That's a U+2029 PARAGRAPH SEPARATOR UTF-16 char
|
||||
|
||||
expect("\\\\\u2029".length).toBe(3);
|
||||
expect("\\\\\u2029").toBe("\\\\\u2029"); // ^ That's a U+2029 PARAGRAPH SEPARATOR UTF-16 char
|
||||
3
packages/babel-plugin-proposal-json-strings/test/fixtures/options.json
vendored
Normal file
3
packages/babel-plugin-proposal-json-strings/test/fixtures/options.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["proposal-json-strings"]
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
import runner from "@babel/helper-plugin-test-runner";
|
||||
|
||||
runner(__dirname);
|
||||
Reference in New Issue
Block a user