1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
import * as _acorn from "acorn";
const leftCurlyBrace = "{".charCodeAt(0);
const space = " ".charCodeAt(0);
const withKeyword = "with";
const assertKeyword = "assert";
const FUNC_STATEMENT = 1, FUNC_HANGING_STATEMENT = 2, FUNC_NULLABLE_ID = 4
export const importAttributes = plugin({ keyword: "with" });
export const importAssertions = plugin({ keyword: "assert" });
export const importAttributesOrAssertions = plugin({ keyword: "with-assert" })
function plugin(options) {
return function(Parser) {
return pluginImpl(options, Parser);
};
}
function pluginImpl(options, Parser) {
// Use supplied version acorn version if present, to avoid
// reference mismatches due to different acorn versions. This
// allows this plugin to be used with Rollup which supplies
// its own internal version of acorn and thereby sidesteps
// the package manager.
const acorn = Parser.acorn || _acorn;
const { tokTypes: tt, TokenType } = acorn;
const { keyword } = options;
const isWithKeyword = keyword.includes(withKeyword);
const isAssertKeyword = keyword.includes(assertKeyword);
const isWithOrAssertKeyword = isWithKeyword && isAssertKeyword;
return class extends Parser {
constructor(...args) {
super(...args);
this.withToken = isWithKeyword && new TokenType(withKeyword);
this.assertToken = isAssertKeyword && new TokenType(assertKeyword);
}
_codeAt(i) {
return this.input.charCodeAt(i);
}
_eat(t) {
if (this.type !== t) {
this.unexpected();
}
this.next();
}
_matchKeywordToken() {
return (isWithOrAssertKeyword && (this.type === this.withToken || this.type === this.assertToken))
|| (isWithKeyword && this.type === this.withToken)
|| (isAssertKeyword && this.type === this.assertToken)
}
_getProperty() {
if (isWithOrAssertKeyword) {
return this.type === this.withToken ? "attributes" : "assertions";
}
return isWithKeyword ? "attributes" : "assertions";
}
readToken(code) {
let i = 0;
let keyword;
let token;
if (isWithOrAssertKeyword) {
if (this.input.slice(this.pos, this.pos + withKeyword.length) === withKeyword) {
keyword = withKeyword;
token = this.withToken;
} else if (this.input.slice(this.pos, this.pos + assertKeyword.length) === assertKeyword) {
keyword = assertKeyword;
token = this.assertToken;
} else {
return super.readToken(code);
}
i += keyword.length;
} else {
keyword = isWithKeyword ? withKeyword : assertKeyword;
token = isWithKeyword ? this.withToken : this.assertToken;
for (; i < keyword.length; i++) {
if (this._codeAt(this.pos + i) !== keyword.charCodeAt(i)) {
return super.readToken(code);
}
}
}
// ensure that the keyword is at the correct location
// ie `with{...` or `with {...`
for (;; i++) {
if (this._codeAt(this.pos + i) === leftCurlyBrace) {
// Found '{'
break;
} else if (this._codeAt(this.pos + i) === space) {
// white space is allowed between `with` and `{`, so continue.
continue;
} else {
return super.readToken(code);
}
}
// If we're inside a dynamic import expression we'll parse
// the `with` keyword as a standard object property name
// ie `import(""./foo.json", { with: { type: "json" } })`
if (this.type.label === "{") {
return super.readToken(code);
}
this.pos += keyword.length;
return this.finishToken(token);
}
parseDynamicImport(node) {
this.next(); // skip `(`
// Parse node.source.
node.source = this.parseMaybeAssign();
if (this.eat(tt.comma)) {
const expr = this.parseExpression();
node.arguments = [expr];
}
this._eat(tt.parenR);
return this.finishNode(node, "ImportExpression");
}
// ported from acorn/src/statement.js pp.parseExport
parseExport(node, exports) {
this.next();
// export * from '...'
if (this.eat(tt.star)) {
if (this.options.ecmaVersion >= 11) {
if (this.eatContextual("as")) {
node.exported = this.parseIdent(true);
this.checkExport(exports, node.exported.name, this.lastTokStart);
} else {
node.exported = null;
}
}
this.expectContextual("from");
if (this.type !== tt.string) { this.unexpected(); }
node.source = this.parseExprAtom();
if (this._matchKeywordToken()) {
const property = this._getProperty();
this.next();
const attributes = this.parseImportAttributes();
if (attributes) {
node[property] = attributes;
}
}
this.semicolon();
return this.finishNode(node, "ExportAllDeclaration")
}
if (this.eat(tt._default)) { // export default ...
this.checkExport(exports, "default", this.lastTokStart);
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);
} else if (this.type === tt._class) {
var cNode = this.startNode();
node.declaration = this.parseClass(cNode, "nullableID");
} else {
node.declaration = this.parseMaybeAssign();
this.semicolon();
}
return this.finishNode(node, "ExportDefaultDeclaration")
}
// export var|const|let|function|class ...
if (this.shouldParseExportStatement()) {
node.declaration = this.parseStatement(null);
if (node.declaration.type === "VariableDeclaration")
{ this.checkVariableExport(exports, node.declaration.declarations); }
else
{ this.checkExport(exports, node.declaration.id.name, node.declaration.id.start); }
node.specifiers = [];
node.source = null;
} else { // export { x, y as z } [from '...']
node.declaration = null;
node.specifiers = this.parseExportSpecifiers(exports);
if (this.eatContextual("from")) {
if (this.type !== tt.string) { this.unexpected(); }
node.source = this.parseExprAtom();
if (this._matchKeywordToken()) {
const property = this._getProperty();
this.next();
const attributes = this.parseImportAttributes();
if (attributes) {
node[property] = attributes;
}
}
} else {
for (var i = 0, list = node.specifiers; i < list.length; i += 1) {
// check for keywords used as local names
var spec = list[i];
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 '...'
if (this.type === tt.string) {
node.specifiers = [];
node.source = this.parseExprAtom();
} else {
node.specifiers = this.parseImportSpecifiers();
this.expectContextual("from");
node.source =
this.type === tt.string ? this.parseExprAtom() : this.unexpected();
}
if (this._matchKeywordToken()) {
const property = this._getProperty();
this.next();
const attributes = this.parseImportAttributes();
if (attributes) {
node[property] = attributes;
}
}
this.semicolon();
return this.finishNode(node, "ImportDeclaration");
}
parseImportAttributes() {
this._eat(tt.braceL);
const attrs = this.parsewithEntries();
this._eat(tt.braceR);
return attrs;
}
parsewithEntries() {
const attrs = [];
const attrNames = new Set();
do {
if (this.type === tt.braceR) {
break;
}
const node = this.startNode();
// parse withionKey : IdentifierName, StringLiteral
let withionKeyNode;
if (this.type === tt.string) {
withionKeyNode = this.parseLiteral(this.value);
} else {
withionKeyNode = this.parseIdent(true);
}
this.next();
node.key = withionKeyNode;
// 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 attributes");
}
attrNames.add(node.key.name);
if (this.type !== tt.string) {
this.raise(
this.pos,
"Only string is supported as an attribute value"
);
}
node.value = this.parseLiteral(this.value);
attrs.push(this.finishNode(node, "ImportAttribute"));
} while (this.eat(tt.comma));
return attrs;
}
};
}
|