UNPKG

935 BJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4
5const pattern = /(^|;)\s*(export|import)((\s+\w)|(\s*[{*=]))|import\(/m;
6/**
7 * detect possible imports/exports without a full parse.
8 *
9 * A negative test means that a file is definitely _not_ a module.
10 * A positive test means it _could_ be.
11 *
12 * Not perfect, just a fast way to disqualify large non-ES6 modules and
13 * avoid a parse.
14 * @type {import('./unambiguous').test}
15 */
16exports.test = function isMaybeUnambiguousModule(content) {
17 return pattern.test(content);
18};
19
20// future-/Babel-proof at the expense of being a little loose
21const unambiguousNodeType = /^(?:(?:Exp|Imp)ort.*Declaration|TSExportAssignment)$/;
22
23/**
24 * Given an AST, return true if the AST unambiguously represents a module.
25 * @type {import('./unambiguous').isModule}
26 */
27exports.isModule = function isUnambiguousModule(ast) {
28 return ast.body && ast.body.some((node) => unambiguousNodeType.test(node.type));
29};