From a2d69b7d6c05668bf18b7027449509a39bf08023 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20Kooi?= Date: Tue, 13 Aug 2019 18:18:41 +0200 Subject: [PATCH 1/3] Use builtin import() parsing from acorn --- index.js | 1 - lib/dynamic-import/index.js | 86 ------------------------------------- package.json | 1 - test/index.js | 2 +- walk.js | 1 - 5 files changed, 1 insertion(+), 90 deletions(-) delete mode 100644 lib/dynamic-import/index.js diff --git a/index.js b/index.js index c0ec3bf..c23e130 100644 --- a/index.js +++ b/index.js @@ -6,7 +6,6 @@ var CJSParser = acorn.Parser .extend(require('./lib/class-fields')) .extend(require('./lib/static-class-features')) .extend(require('./lib/numeric-separator')) - .extend(require('./lib/dynamic-import').default) var ESModulesParser = CJSParser .extend(require('./lib/export-ns-from')) .extend(require('./lib/import-meta')) diff --git a/lib/dynamic-import/index.js b/lib/dynamic-import/index.js deleted file mode 100644 index bb37198..0000000 --- a/lib/dynamic-import/index.js +++ /dev/null @@ -1,86 +0,0 @@ -/* Generated by `npm run build`, do not edit! */ - -'use strict'; - -Object.defineProperty(exports, "__esModule", { - value: true -}); -exports.DynamicImportKey = undefined; - -var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) { descriptor.writable = true; } Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) { defineProperties(Constructor.prototype, protoProps); } if (staticProps) { defineProperties(Constructor, staticProps); } return Constructor; }; }(); - -var _get = function () { - function get(object, property, receiver) { if (object === null) { object = Function.prototype; } var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } - - return get; -}(); - -exports['default'] = dynamicImport; - -var _acorn = require('acorn'); - -function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } - -function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } - -function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) { Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } } /* eslint-disable no-underscore-dangle */ - - -var DynamicImportKey = exports.DynamicImportKey = 'Import'; - -// NOTE: This allows `yield import()` to parse correctly. -_acorn.tokTypes._import.startsExpr = true; - -function parseDynamicImport() { - var node = this.startNode(); - this.next(); - if (this.type !== _acorn.tokTypes.parenL) { - this.unexpected(); - } - return this.finishNode(node, DynamicImportKey); -} - -function parenAfter() { - return (/^(\s|\/\/.*|\/\*[^]*?\*\/)*\(/.test(this.input.slice(this.pos)) - ); -} - -function dynamicImport(Parser) { - return function (_Parser) { - _inherits(_class, _Parser); - - function _class() { - _classCallCheck(this, _class); - - return _possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).apply(this, arguments)); - } - - _createClass(_class, [{ - key: 'parseStatement', - value: function () { - function parseStatement(context, topLevel, exports) { - if (this.type === _acorn.tokTypes._import && parenAfter.call(this)) { - return this.parseExpressionStatement(this.startNode(), this.parseExpression()); - } - return _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'parseStatement', this).call(this, context, topLevel, exports); - } - - return parseStatement; - }() - }, { - key: 'parseExprAtom', - value: function () { - function parseExprAtom(refDestructuringErrors) { - if (this.type === _acorn.tokTypes._import) { - return parseDynamicImport.call(this); - } - return _get(_class.prototype.__proto__ || Object.getPrototypeOf(_class.prototype), 'parseExprAtom', this).call(this, refDestructuringErrors); - } - - return parseExprAtom; - }() - }]); - - return _class; - }(Parser); -} \ No newline at end of file diff --git a/package.json b/package.json index 1e9a008..6cde918 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,6 @@ "devDependencies": { "acorn-bigint": "^0.4.0", "acorn-class-fields": "^0.3.1", - "acorn-dynamic-import": "^4.0.0", "acorn-export-ns-from": "^0.1.0", "acorn-import-meta": "^0.3.0", "acorn-numeric-separator": "^0.3.0", diff --git a/test/index.js b/test/index.js index 59832db..200b0fe 100644 --- a/test/index.js +++ b/test/index.js @@ -143,7 +143,7 @@ test('walk supports plugin syntax', function (t) { ) t.plan(2) walk.simple(ast, { - Import: function () { + ImportExpression: function () { t.pass('import()') }, MetaProperty: function () { diff --git a/walk.js b/walk.js index 273a30c..479e0d7 100644 --- a/walk.js +++ b/walk.js @@ -2,7 +2,6 @@ var xtend = require('xtend') var walk = require('acorn-walk') var base = xtend(walk.base) -base.Import = function () {} function simple (node, visitors, baseVisitor, state, override) { return walk.simple(node, visitors, baseVisitor || base, state, override) From 379e8ee3cb4f7336bf6588e25c269b8972c79ca6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20Kooi?= Date: Tue, 13 Aug 2019 18:23:44 +0200 Subject: [PATCH 2/3] Update acorn-import-meta --- lib/import-meta/index.js | 3 +++ package.json | 2 +- test/index.js | 3 +++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/import-meta/index.js b/lib/import-meta/index.js index bd4adf9..7c84887 100644 --- a/lib/import-meta/index.js +++ b/lib/import-meta/index.js @@ -37,6 +37,9 @@ module.exports = function(Parser) { if (node.property.name !== "meta") { this.raiseRecoverable(node.property.start, "The only valid meta property for import is import.meta") } + if (this.containsEsc) { + this.raiseRecoverable(node.property.start, "\"meta\" in import.meta must not contain escape sequences") + } return this.finishNode(node, "MetaProperty") }; diff --git a/package.json b/package.json index 6cde918..fa17d97 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "acorn-bigint": "^0.4.0", "acorn-class-fields": "^0.3.1", "acorn-export-ns-from": "^0.1.0", - "acorn-import-meta": "^0.3.0", + "acorn-import-meta": "^1.0.0", "acorn-numeric-separator": "^0.3.0", "acorn-private-class-elements": "^0.1.1", "acorn-static-class-features": "^0.2.0", diff --git a/test/index.js b/test/index.js index 200b0fe..a0b7468 100644 --- a/test/index.js +++ b/test/index.js @@ -84,6 +84,9 @@ test('supports import.meta with sourceType: module', function (t) { t.doesNotThrow(function () { acorn.parse('console.log(import.meta.url)', { sourceType: 'module' }) }) + t.throws(function () { + acorn.parse('console.log(import.m\\u0065ta.ul)', { sourceType: 'module' }) + }, /must not contain escape sequences/) t.end() }) From a3c4fff8ed951e4ace141f7f841a088cd654f26f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20Kooi?= Date: Tue, 13 Aug 2019 18:26:41 +0200 Subject: [PATCH 3/3] Remove dynamic-import link --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 37e871f..0900f99 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,6 @@ The files in lib/ are generated from other packages: - lib/bigint: [acorn-bigint](https://github.com/acornjs/acorn-bigint]), MIT - lib/class-private-elements: [acorn-class-private-elements](https://github.com/acornjs/acorn-class-private-elements), MIT -- lib/dynamic-import: [acorn-dynamic-import](https://github.com/acornjs/acorn-dynamic-import), MIT - lib/export-ns-from: [acorn-export-ns-from](https://github.com/acornjs/acorn-export-ns-from), MIT - lib/import-meta: [acorn-import-meta](https://github.com/acornjs/acorn-import-meta), MIT - lib/numeric-separator: [acorn-numeric-separator](https://github.com/acornjs/acorn-numeric-separator]), MIT