[Pkg-javascript-commits] [node-acorn-jsx] 464/484: Add namespaces options for spec compliancy and better linting support.
Bastien Roucariès
rouca at moszumanska.debian.org
Sat Aug 19 14:21:11 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to branch master
in repository node-acorn-jsx.
commit 2b52a0d62820ea12bd95c0a07942cb39afffa349
Author: Ingvar Stepanyan <me at rreverser.com>
Date: Mon Mar 14 20:06:12 2016 +0000
Add namespaces options for spec compliancy and better linting support.
Fixes #27.
---
README.md | 22 +++++++++++++++++++
inject.js | 20 ++++++++++++++++--
package.json | 2 +-
test/driver.js | 2 +-
test/tests-jsx.js | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 105 insertions(+), 4 deletions(-)
diff --git a/README.md b/README.md
index 6b3826d..cd9674c 100644
--- a/README.md
+++ b/README.md
@@ -37,6 +37,28 @@ var ast = acorn.parse(code, {
});
```
+Note that official spec doesn't support mix of XML namespaces and object-style access in tag names (#27) like in `<namespace:Object.Property />`, so it was deprecated in `acorn-jsx at 3.0`. If you still want to opt-in to support of such constructions, you can pass the following option:
+
+```javascript
+var ast = acorn.parse(code, {
+ plugins: {
+ jsx: { allowNamespacedObjects: true }
+ }
+});
+```
+
+Also, since most apps use pure React transformer, a new option was introduced that allows to prohibit namespaces completely:
+
+```javascript
+var ast = acorn.parse(code, {
+ plugins: {
+ jsx: { allowNamespaces: false }
+ }
+});
+```
+
+Note that by default `allowNamespaces` is enabled for spec compliancy.
+
## License
This plugin is issued under the [MIT license](./LICENSE).
diff --git a/inject.js b/inject.js
index bfe0358..2bc4e9f 100644
--- a/inject.js
+++ b/inject.js
@@ -198,7 +198,7 @@ module.exports = function(acorn) {
pp.jsx_parseNamespacedName = function() {
var startPos = this.start, startLoc = this.startLoc;
var name = this.jsx_parseIdentifier();
- if (!this.eat(tt.colon)) return name;
+ if (!this.options.plugins.jsx.allowNamespaces || !this.eat(tt.colon)) return name;
var node = this.startNodeAt(startPos, startLoc);
node.namespace = name;
node.name = this.jsx_parseIdentifier();
@@ -211,6 +211,9 @@ module.exports = function(acorn) {
pp.jsx_parseElementName = function() {
var startPos = this.start, startLoc = this.startLoc;
var node = this.jsx_parseNamespacedName();
+ if (this.type === tt.dot && node.type === 'JSXNamespacedName' && !this.options.plugins.jsx.allowNamespacedObjects) {
+ this.unexpected();
+ }
while (this.eat(tt.dot)) {
var newNode = this.startNodeAt(startPos, startLoc);
newNode.object = node;
@@ -356,7 +359,20 @@ module.exports = function(acorn) {
return this.jsx_parseElementAt(startPos, startLoc);
};
- acorn.plugins.jsx = function(instance) {
+ acorn.plugins.jsx = function(instance, opts) {
+ if (!opts) {
+ return;
+ }
+
+ if (typeof opts !== 'object') {
+ opts = {};
+ }
+
+ instance.options.plugins.jsx = {
+ allowNamespaces: opts.allowNamespaces !== false,
+ allowNamespacedObjects: !!opts.allowNamespacedObjects
+ };
+
instance.extend('parseExprAtom', function(inner) {
return function(refShortHandDefaultPos) {
if (this.type === tt.jsxText)
diff --git a/package.json b/package.json
index 842b27b..0b03371 100644
--- a/package.json
+++ b/package.json
@@ -2,7 +2,7 @@
"name": "acorn-jsx",
"description": "Alternative, faster React.js JSX parser",
"homepage": "https://github.com/RReverser/acorn-jsx",
- "version": "2.0.1",
+ "version": "3.0.0",
"maintainers": [
{
"name": "Ingvar Stepanyan",
diff --git a/test/driver.js b/test/driver.js
index 01ec6c6..06d5644 100644
--- a/test/driver.js
+++ b/test/driver.js
@@ -25,7 +25,7 @@ exports.runTests = function(config, callback) {
if (expected.onToken = testOpts.onToken) {
testOpts.onToken = [];
}
- testOpts.plugins = { jsx: true };
+ testOpts.plugins = testOpts.plugins || { jsx: true };
var ast = parse(test.code, testOpts);
if (test.error) {
if (config.loose) {
diff --git a/test/tests-jsx.js b/test/tests-jsx.js
index 48e6a1d..5c7d8d8 100644
--- a/test/tests-jsx.js
+++ b/test/tests-jsx.js
@@ -3639,6 +3639,69 @@ if (typeof exports !== "undefined") {
testFail("var x = <div>one</div><div>two</div>;", "Adjacent JSX elements must be wrapped in an enclosing tag (1:22)");
+testFail("<a:b.c />", "Unexpected token (1:4)");
+
+test("<a:b.c />", {
+ type: "Program",
+ range: [0, 9],
+ body: [{
+ type: "ExpressionStatement",
+ range: [0, 9],
+ expression: {
+ type: "JSXElement",
+ range: [0, 9],
+ openingElement: {
+ type: "JSXOpeningElement",
+ range: [0, 9],
+ attributes: [],
+ name: {
+ type: "JSXMemberExpression",
+ range: [1, 6],
+ object: {
+ type: "JSXNamespacedName",
+ range: [1, 4],
+ namespace: {
+ type: "JSXIdentifier",
+ range: [1, 2],
+ name: "a"
+ },
+ name: {
+ type: "JSXIdentifier",
+ range: [3, 4],
+ name: "b"
+ }
+ },
+ property: {
+ type: "JSXIdentifier",
+ range: [5, 6],
+ name: "c"
+ }
+ },
+ selfClosing: true
+ },
+ closingElement: null,
+ children: []
+ }
+ }]
+}, {
+ ranges: true,
+ plugins: {
+ jsx: { allowNamespacedObjects: true }
+ }
+});
+
+testFail('<ns:div />', 'Unexpected token (1:3)', {
+ plugins: {
+ jsx: { allowNamespaces: false }
+ }
+});
+
+testFail('<div ns:attr />', 'Unexpected token (1:7)', {
+ plugins: {
+ jsx: { allowNamespaces: false }
+ }
+});
+
test('<a>{/* foo */}</a>', {}, {
onToken: [
{
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-acorn-jsx.git
More information about the Pkg-javascript-commits
mailing list