acorn-import-assertions
Advanced tools
Comparing version 1.8.0 to 1.9.0
106
lib/index.js
@@ -7,9 +7,5 @@ "use strict"; | ||
exports.importAssertions = importAssertions; | ||
var _acorn = _interopRequireWildcard(require("acorn")); | ||
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } | ||
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } | ||
const leftCurlyBrace = "{".charCodeAt(0); | ||
@@ -19,5 +15,4 @@ const space = " ".charCodeAt(0); | ||
const FUNC_STATEMENT = 1, | ||
FUNC_HANGING_STATEMENT = 2, | ||
FUNC_NULLABLE_ID = 4; | ||
FUNC_HANGING_STATEMENT = 2, | ||
FUNC_NULLABLE_ID = 4; | ||
function importAssertions(Parser) { | ||
@@ -39,7 +34,5 @@ // Use supplied version acorn version if present, to avoid | ||
} | ||
_codeAt(i) { | ||
return this.input.charCodeAt(i); | ||
} | ||
_eat(t) { | ||
@@ -49,9 +42,6 @@ if (this.type !== t) { | ||
} | ||
this.next(); | ||
} | ||
readToken(code) { | ||
let i = 0; | ||
for (; i < keyword.length; i++) { | ||
@@ -61,6 +51,6 @@ if (this._codeAt(this.pos + i) !== keyword.charCodeAt(i)) { | ||
} | ||
} // ensure that the keyword is at the correct location | ||
} | ||
// ensure that the keyword is at the correct location | ||
// ie `assert{...` or `assert {...` | ||
for (;; i++) { | ||
@@ -76,21 +66,18 @@ if (this._codeAt(this.pos + i) === leftCurlyBrace) { | ||
} | ||
} // If we're inside a dynamic import expression we'll parse | ||
} | ||
// If we're inside a dynamic import expression we'll parse | ||
// the `assert` keyword as a standard object property name | ||
// ie `import(""./foo.json", { assert: { type: "json" } })` | ||
if (this.type.label === "{") { | ||
return super.readToken(code); | ||
} | ||
this.pos += keyword.length; | ||
return this.finishToken(this.assertToken); | ||
} | ||
parseDynamicImport(node) { | ||
this.next(); // skip `(` | ||
// Parse node.source. | ||
node.source = this.parseMaybeAssign(); | ||
if (this.eat(tt.comma)) { | ||
@@ -100,12 +87,10 @@ const obj = this.parseObj(false); | ||
} | ||
this._eat(tt.parenR); | ||
return this.finishNode(node, "ImportExpression"); | ||
} // ported from acorn/src/statement.js pp.parseExport | ||
} | ||
// ported from acorn/src/statement.js pp.parseExport | ||
parseExport(node, exports) { | ||
this.next(); // export * from '...' | ||
this.next(); | ||
// export * from '...' | ||
if (this.eat(tt.star)) { | ||
@@ -120,15 +105,10 @@ if (this.options.ecmaVersion >= 11) { | ||
} | ||
this.expectContextual("from"); | ||
if (this.type !== tt.string) { | ||
this.unexpected(); | ||
} | ||
node.source = this.parseExprAtom(); | ||
if (this.type === this.assertToken) { | ||
if (this.type === this.assertToken || this.type === tt._with) { | ||
this.next(); | ||
const assertions = this.parseImportAssertions(); | ||
if (assertions) { | ||
@@ -138,7 +118,5 @@ node.assertions = assertions; | ||
} | ||
this.semicolon(); | ||
return this.finishNode(node, "ExportAllDeclaration"); | ||
} | ||
if (this.eat(tt._default)) { | ||
@@ -148,11 +126,8 @@ // export default ... | ||
var isAsync; | ||
if (this.type === tt._function || (isAsync = this.isAsyncFunction())) { | ||
var fNode = this.startNode(); | ||
this.next(); | ||
if (isAsync) { | ||
this.next(); | ||
} | ||
node.declaration = this.parseFunction(fNode, FUNC_STATEMENT | FUNC_NULLABLE_ID, false, isAsync); | ||
@@ -166,10 +141,7 @@ } else if (this.type === tt._class) { | ||
} | ||
return this.finishNode(node, "ExportDefaultDeclaration"); | ||
} // export var|const|let|function|class ... | ||
} | ||
// export var|const|let|function|class ... | ||
if (this.shouldParseExportStatement()) { | ||
node.declaration = this.parseStatement(null); | ||
if (node.declaration.type === "VariableDeclaration") { | ||
@@ -180,3 +152,2 @@ this.checkVariableExport(exports, node.declaration.declarations); | ||
} | ||
node.specifiers = []; | ||
@@ -188,3 +159,2 @@ node.source = null; | ||
node.specifiers = this.parseExportSpecifiers(exports); | ||
if (this.eatContextual("from")) { | ||
@@ -194,9 +164,6 @@ if (this.type !== tt.string) { | ||
} | ||
node.source = this.parseExprAtom(); | ||
if (this.type === this.assertToken) { | ||
if (this.type === this.assertToken || this.type === tt._with) { | ||
this.next(); | ||
const assertions = this.parseImportAssertions(); | ||
if (assertions) { | ||
@@ -210,19 +177,15 @@ node.assertions = assertions; | ||
var spec = list[i]; | ||
this.checkUnreserved(spec.local); // check if export is defined | ||
this.checkUnreserved(spec.local); | ||
// check if export is defined | ||
this.checkLocalExport(spec.local); | ||
} | ||
node.source = null; | ||
} | ||
this.semicolon(); | ||
} | ||
return this.finishNode(node, "ExportNamedDeclaration"); | ||
} | ||
parseImport(node) { | ||
this.next(); // import '...' | ||
this.next(); | ||
// import '...' | ||
if (this.type === tt.string) { | ||
@@ -236,7 +199,5 @@ node.specifiers = []; | ||
} | ||
if (this.type === this.assertToken) { | ||
if (this.type === this.assertToken || this.type == tt._with) { | ||
this.next(); | ||
const assertions = this.parseImportAssertions(); | ||
if (assertions) { | ||
@@ -246,21 +207,14 @@ node.assertions = assertions; | ||
} | ||
this.semicolon(); | ||
return this.finishNode(node, "ImportDeclaration"); | ||
} | ||
parseImportAssertions() { | ||
this._eat(tt.braceL); | ||
const attrs = this.parseAssertEntries(); | ||
this._eat(tt.braceR); | ||
return attrs; | ||
} | ||
parseAssertEntries() { | ||
const attrs = []; | ||
const attrNames = new Set(); | ||
do { | ||
@@ -270,7 +224,6 @@ if (this.type === tt.braceR) { | ||
} | ||
const node = this.startNode(); | ||
const node = this.startNode(); // parse AssertionKey : IdentifierName, StringLiteral | ||
// parse AssertionKey : IdentifierName, StringLiteral | ||
let assertionKeyNode; | ||
if (this.type === tt.string) { | ||
@@ -281,26 +234,21 @@ assertionKeyNode = this.parseLiteral(this.value); | ||
} | ||
this.next(); | ||
node.key = assertionKeyNode; | ||
this.next(); | ||
node.key = assertionKeyNode; // check if we already have an entry for an attribute | ||
// check if we already have an entry for an attribute | ||
// if a duplicate entry is found, throw an error | ||
// for now this logic will come into play only when someone declares `type` twice | ||
if (attrNames.has(node.key.name)) { | ||
this.raise(this.pos, "Duplicated key in assertions"); | ||
} | ||
attrNames.add(node.key.name); | ||
if (this.type !== tt.string) { | ||
this.raise(this.pos, "Only string is supported as an assertion value"); | ||
} | ||
node.value = this.parseLiteral(this.value); | ||
attrs.push(this.finishNode(node, "ImportAttribute")); | ||
} while (this.eat(tt.comma)); | ||
return attrs; | ||
} | ||
}; | ||
} |
{ | ||
"name": "acorn-import-assertions", | ||
"version": "1.8.0", | ||
"version": "1.9.0", | ||
"description": "Support for import assertions in acorn", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -99,3 +99,3 @@ import * as _acorn from "acorn"; | ||
if (this.type === this.assertToken) { | ||
if (this.type === this.assertToken || this.type === tt._with) { | ||
this.next(); | ||
@@ -144,3 +144,3 @@ const assertions = this.parseImportAssertions(); | ||
if (this.type === this.assertToken) { | ||
if (this.type === this.assertToken || this.type === tt._with) { | ||
this.next(); | ||
@@ -182,3 +182,3 @@ const assertions = this.parseImportAssertions(); | ||
if (this.type === this.assertToken) { | ||
if (this.type === this.assertToken || this.type == tt._with) { | ||
this.next(); | ||
@@ -185,0 +185,0 @@ const assertions = this.parseImportAssertions(); |
Sorry, the diff of this file is not supported yet
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
25734
652