acorn-import-meta
Advanced tools
Comparing version 0.2.1 to 0.3.0
@@ -0,1 +1,6 @@ | ||
## 0.3.0 (2018-09-14) | ||
* Update to new acorn 6 interface | ||
* Change license to MIT | ||
## 0.2.1 (2018-02-13) | ||
@@ -2,0 +7,0 @@ |
42
index.js
"use strict" | ||
module.exports = require("./inject")(require("acorn")) | ||
const tt = require("acorn").tokTypes | ||
const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g | ||
const nextTokenIsDot = parser => { | ||
skipWhiteSpace.lastIndex = parser.pos | ||
let skip = skipWhiteSpace.exec(parser.input) | ||
let next = parser.pos + skip[0].length | ||
return parser.input.slice(next, next + 1) === "." | ||
} | ||
module.exports = function(Parser) { | ||
return class extends Parser { | ||
parseExprAtom(refDestructuringErrors) { | ||
if (this.type !== tt._import || !nextTokenIsDot(this)) return super.parseExprAtom(refDestructuringErrors) | ||
if (!this.options.allowImportExportEverywhere && !this.inModule) { | ||
this.raise(this.start, "'import' and 'export' may appear only with 'sourceType: module'") | ||
} | ||
let node = this.startNode() | ||
node.meta = this.parseIdent(true) | ||
this.expect(tt.dot) | ||
node.property = this.parseIdent(true) | ||
if (node.property.name !== "meta") { | ||
this.raiseRecoverable(node.property.start, "The only valid meta property for import is import.meta") | ||
} | ||
return this.finishNode(node, "MetaProperty") | ||
} | ||
parseStatement(context, topLevel, exports) { | ||
if (this.type !== tt._import || !nextTokenIsDot(this)) { | ||
return super.parseStatement(context, topLevel, exports) | ||
} | ||
let node = this.startNode() | ||
let expr = this.parseExpression() | ||
return this.parseExpressionStatement(node, expr) | ||
} | ||
} | ||
} |
@@ -15,3 +15,3 @@ { | ||
}, | ||
"license": "(AGPL-3.0-or-later OR Apache-2.0)", | ||
"license": "MIT", | ||
"scripts": { | ||
@@ -22,10 +22,11 @@ "test": "mocha", | ||
}, | ||
"dependencies": { | ||
"acorn": "^5.4.1" | ||
"peerDependencies": { | ||
"acorn": "^6.0.0" | ||
}, | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"devDependencies": { | ||
"eslint": "^4.13.1", | ||
"eslint-plugin-node": "^6.0.0", | ||
"mocha": "^5.0.0", | ||
"acorn": "^6.0.0", | ||
"eslint": "^5.5.0", | ||
"eslint-plugin-node": "^7.0.1", | ||
"mocha": "^5.2.0", | ||
"test262": "git+https://github.com/tc39/test262.git#18c1e799a01cc976695983b61e225ce7959bdd91", | ||
@@ -32,0 +33,0 @@ "test262-parser-runner": "^0.3.1" |
@@ -11,25 +11,12 @@ # Support for import.meta in Acorn | ||
You can use this module directly in order to get an Acorn instance with the plugin installed: | ||
This module provides a plugin that extends the Acorn `Parser` class: | ||
```javascript | ||
var acorn = require('acorn-import-meta'); | ||
var acorn = require('acorn'); | ||
var importMeta = require('acorn-import-meta'); | ||
acorn.Parser.extend(importMeta).parse('console.log(import.meta.url)'); | ||
``` | ||
Or you can use `inject.js` for injecting the plugin into your own version of Acorn like this: | ||
```javascript | ||
var acorn = require('acorn-import-meta/inject')(require('./custom-acorn')); | ||
``` | ||
Then, use the `plugins` option to enable the plugiin: | ||
```javascript | ||
var ast = acorn.parse(code, { | ||
plugins: { importMeta: true } | ||
}); | ||
``` | ||
## License | ||
This plugin is released under the [GNU Affero General Public License version 3 or later](./LICENSE.AGPL) and the [Apache License version 2](LICENSE.Apache). | ||
Please feel free to open an issue if this choice of licenses is a problem for your use-case. | ||
This plugin is released under an [MIT License](./LICENSE). |
215
test/test.js
"use strict" | ||
const assert = require("assert") | ||
const acorn = require("..") | ||
const acorn = require("acorn") | ||
const importMeta = require("..") | ||
const Parser = acorn.Parser.extend(importMeta) | ||
@@ -10,5 +12,5 @@ function testFail(text, expectedError, additionalOptions) { | ||
try { | ||
acorn.parse(text, Object.assign({ ecmaVersion: 9, sourceType: "module", plugins: { importMeta: true } }, additionalOptions)) | ||
Parser.parse(text, Object.assign({ ecmaVersion: 9, sourceType: "module" }, additionalOptions)) | ||
} catch (e) { | ||
if (expectedError) assert.equal(e.message, expectedError) | ||
if (expectedError) assert.strictEqual(e.message, expectedError) | ||
failed = true | ||
@@ -21,5 +23,4 @@ } | ||
it(text, function () { | ||
const result = acorn.parse(text, Object.assign({ ecmaVersion: 9, sourceType: "module", plugins: { importMeta: true } }, additionalOptions)) | ||
assert.deepEqual(result, expectedResult) | ||
const result = Parser.parse(text, Object.assign({ ecmaVersion: 9, sourceType: "module" }, additionalOptions)) | ||
assert.deepStrictEqual(result, expectedResult) | ||
}) | ||
@@ -29,256 +30,218 @@ testFail(text, null, { sourceType: "script" }) | ||
const newNode = (start, props) => Object.assign(new acorn.Node({options: {}}, start), props) | ||
describe("acorn-import-meta", function () { | ||
test("const response = fetch(import.meta.url);", { | ||
test("const response = fetch(import.meta.url);", newNode(0, { | ||
type: "Program", | ||
start: 0, | ||
end: 40, | ||
body: [ | ||
{ | ||
newNode(0, { | ||
type: "VariableDeclaration", | ||
start: 0, | ||
end: 40, | ||
declarations: [ | ||
{ | ||
newNode(6, { | ||
type: "VariableDeclarator", | ||
start: 6, | ||
end: 39, | ||
id: { | ||
id: newNode(6, { | ||
type: "Identifier", | ||
start: 6, | ||
end: 14, | ||
name: "response" | ||
}, | ||
init: { | ||
}), | ||
init: newNode(17, { | ||
type: "CallExpression", | ||
start: 17, | ||
end: 39, | ||
callee: { | ||
callee: newNode(17, { | ||
type: "Identifier", | ||
start: 17, | ||
end: 22, | ||
name: "fetch" | ||
}, | ||
}), | ||
arguments: [ | ||
{ | ||
newNode(23, { | ||
type: "MemberExpression", | ||
start: 23, | ||
end: 38, | ||
object: { | ||
object: newNode(23, { | ||
type: "MetaProperty", | ||
start: 23, | ||
end: 34, | ||
meta: { | ||
meta: newNode(23, { | ||
type: "Identifier", | ||
start: 23, | ||
end: 29, | ||
name: "import" | ||
}, | ||
property: { | ||
}), | ||
property: newNode(30, { | ||
type: "Identifier", | ||
start: 30, | ||
end: 34, | ||
name: "meta" | ||
} | ||
}, | ||
property: { | ||
}) | ||
}), | ||
property: newNode(35, { | ||
type: "Identifier", | ||
start: 35, | ||
end: 38, | ||
name: "url" | ||
}, | ||
}), | ||
computed: false | ||
} | ||
}) | ||
] | ||
} | ||
} | ||
}) | ||
}) | ||
], | ||
kind: "const" | ||
} | ||
}) | ||
], | ||
sourceType: "module" | ||
}) | ||
test("const size = import.meta.scriptElement.dataset.size || 300;", { | ||
})) | ||
test("const size = import.meta.scriptElement.dataset.size || 300;", newNode(0, { | ||
type: "Program", | ||
start: 0, | ||
end: 59, | ||
body: [ | ||
{ | ||
newNode(0, { | ||
type: "VariableDeclaration", | ||
start: 0, | ||
end: 59, | ||
declarations: [ | ||
{ | ||
newNode(6, { | ||
type: "VariableDeclarator", | ||
start: 6, | ||
end: 58, | ||
id: { | ||
id: newNode(6, { | ||
type: "Identifier", | ||
start: 6, | ||
end: 10, | ||
name: "size" | ||
}, | ||
init: { | ||
}), | ||
init: newNode(13, { | ||
type: "LogicalExpression", | ||
start: 13, | ||
end: 58, | ||
left: { | ||
left: newNode(13, { | ||
type: "MemberExpression", | ||
start: 13, | ||
end: 51, | ||
object: { | ||
object: newNode(13, { | ||
type: "MemberExpression", | ||
start: 13, | ||
end: 46, | ||
object: { | ||
object: newNode(13, { | ||
type: "MemberExpression", | ||
start: 13, | ||
end: 38, | ||
object: { | ||
object: newNode(13, { | ||
type: "MetaProperty", | ||
start: 13, | ||
end: 24, | ||
meta: { | ||
meta: newNode(13, { | ||
type: "Identifier", | ||
start: 13, | ||
end: 19, | ||
name: "import" | ||
}, | ||
property: { | ||
}), | ||
property: newNode(20, { | ||
type: "Identifier", | ||
start: 20, | ||
end: 24, | ||
name: "meta" | ||
} | ||
}, | ||
property: { | ||
}) | ||
}), | ||
property: newNode(25, { | ||
type: "Identifier", | ||
start: 25, | ||
end: 38, | ||
name: "scriptElement" | ||
}, | ||
}), | ||
computed: false | ||
}, | ||
property: { | ||
}), | ||
property: newNode(39, { | ||
type: "Identifier", | ||
start: 39, | ||
end: 46, | ||
name: "dataset" | ||
}, | ||
}), | ||
computed: false | ||
}, | ||
property: { | ||
}), | ||
property: newNode(47, { | ||
type: "Identifier", | ||
start: 47, | ||
end: 51, | ||
name: "size" | ||
}, | ||
}), | ||
computed: false | ||
}, | ||
}), | ||
operator: "||", | ||
right: { | ||
right: newNode(55, { | ||
type: "Literal", | ||
start: 55, | ||
end: 58, | ||
value: 300, | ||
raw: "300" | ||
} | ||
} | ||
} | ||
}) | ||
}) | ||
}) | ||
], | ||
kind: "const" | ||
} | ||
}) | ||
], | ||
sourceType: "module" | ||
}) | ||
test("import.meta.resolve('something')", { | ||
})) | ||
test("import.meta.resolve('something')", newNode(0, { | ||
type: "Program", | ||
start: 0, | ||
end: 32, | ||
body: [ | ||
{ | ||
newNode(0, { | ||
type: "ExpressionStatement", | ||
start: 0, | ||
end: 32, | ||
expression: { | ||
expression: newNode(0, { | ||
type: "CallExpression", | ||
start: 0, | ||
end: 32, | ||
callee: { | ||
callee: newNode(0, { | ||
type: "MemberExpression", | ||
start: 0, | ||
end: 19, | ||
object: { | ||
object: newNode(0, { | ||
type: "MetaProperty", | ||
start: 0, | ||
end: 11, | ||
meta: { | ||
meta: newNode(0, { | ||
type: "Identifier", | ||
start: 0, | ||
end: 6, | ||
name: "import" | ||
}, | ||
property: { | ||
}), | ||
property: newNode(7, { | ||
type: "Identifier", | ||
start: 7, | ||
end: 11, | ||
name: "meta" | ||
} | ||
}, | ||
property: { | ||
}) | ||
}), | ||
property: newNode(12, { | ||
type: "Identifier", | ||
start: 12, | ||
end: 19, | ||
name: "resolve" | ||
}, | ||
}), | ||
computed: false | ||
}, | ||
}), | ||
arguments: [ | ||
{ | ||
newNode(20, { | ||
type: "Literal", | ||
start: 20, | ||
end: 31, | ||
value: "something", | ||
raw: "'something'" | ||
} | ||
}) | ||
] | ||
} | ||
} | ||
}) | ||
}) | ||
], | ||
sourceType: "module" | ||
}) | ||
})) | ||
test("import x from 'y'", { | ||
test("import x from 'y'", newNode(0, { | ||
type: "Program", | ||
start: 0, | ||
end: 17, | ||
body: [ | ||
{ | ||
newNode(0, { | ||
type: "ImportDeclaration", | ||
start: 0, | ||
end: 17, | ||
specifiers: [ | ||
{ | ||
newNode(7, { | ||
type: "ImportDefaultSpecifier", | ||
start: 7, | ||
end: 8, | ||
local: { | ||
local: newNode(7, { | ||
type: "Identifier", | ||
start: 7, | ||
end: 8, | ||
name: "x" | ||
} | ||
} | ||
}) | ||
}) | ||
], | ||
source: { | ||
source: newNode(14, { | ||
type: "Literal", | ||
start: 14, | ||
end: 17, | ||
value: "y", | ||
raw: "'y'" | ||
} | ||
} | ||
}) | ||
}) | ||
], | ||
sourceType: "module" | ||
}) | ||
})) | ||
testFail("let x = import.anotherMeta", "The only valid meta property for import is import.meta (1:15)") | ||
}) |
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
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
0
100
21209
6
9
595
22
+ Addedacorn@6.4.2(transitive)
- Removedacorn@^5.4.1
- Removedacorn@5.7.4(transitive)