Skip to content
This repository was archived by the owner on Jan 13, 2024. It is now read-only.

Commit 6c98d49

Browse files
committed
detector: use Babel AST and default plugins
1 parent 3f1c883 commit 6c98d49

4 files changed

Lines changed: 155 additions & 76 deletions

File tree

lib/detector.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,40 @@
1-
/* eslint-disable operator-linebreak */
2-
/* eslint-disable prefer-const */
3-
4-
import { generate } from 'escodegen';
51
// eslint-disable-next-line import/no-extraneous-dependencies
62
import * as babelTypes from '@babel/types';
73
import * as babel from '@babel/parser';
4+
import generate from '@babel/generator';
85
import { log } from './log';
96

107
import { ALIAS_AS_RELATIVE, ALIAS_AS_RESOLVABLE } from './common';
118

12-
function isLiteral(
13-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
14-
node: any
15-
): node is babelTypes.StringLiteral | babelTypes.TemplateLiteral {
16-
// TODO: this function is a lie and can probably be better
17-
// I was using babelTypes.isStringLiteral but that broke a bunch of tests
18-
return (
19-
node &&
20-
(node.type === 'Literal' ||
21-
(node.type === 'TemplateLiteral' && node.expressions.length === 0))
22-
);
9+
function isLiteral(node: babelTypes.Node): node is babelTypes.Literal {
10+
if (node == null) {
11+
return false;
12+
}
13+
14+
if (!node.type.endsWith('Literal')) {
15+
return false;
16+
}
17+
18+
if (node.type === 'TemplateLiteral' && node.expressions.length !== 0) {
19+
return false;
20+
}
21+
22+
return true;
2323
}
2424

25-
function getLiteralValue(
26-
node: babelTypes.StringLiteral | babelTypes.TemplateLiteral
27-
) {
25+
function getLiteralValue(node: babelTypes.Literal) {
2826
if (node.type === 'TemplateLiteral') {
2927
return node.quasis[0].value.raw;
3028
}
3129

30+
if (node.type === 'NullLiteral') {
31+
throw new Error('Unexpected null in require expression');
32+
}
33+
34+
if (node.type === 'RegExpLiteral') {
35+
throw new Error('Unexpected regexp in require expression');
36+
}
37+
3238
return node.value;
3339
}
3440

@@ -75,7 +81,7 @@ function reconstructSpecifiers(
7581
}
7682

7783
function reconstruct(node: babelTypes.Node) {
78-
let v = generate(node).replace(/\n/g, '');
84+
let v = generate(node, { comments: false }).code.replace(/\n/g, '');
7985
let v2;
8086

8187
// eslint-disable-next-line no-constant-condition
@@ -468,7 +474,7 @@ function traverse(ast: babelTypes.File, visitor: VisitorFunction) {
468474

469475
for (let i = 0; i < stack.length; i += 1) {
470476
const item = stack[i];
471-
let [node] = item;
477+
const [node] = item;
472478

473479
if (node) {
474480
const trying = item[1] || babelTypes.isTryStatement(node);
@@ -496,7 +502,6 @@ export function parse(body: string) {
496502
return babel.parse(body, {
497503
allowImportExportEverywhere: true,
498504
allowReturnOutsideFunction: true,
499-
plugins: ['estree', 'bigInt', 'classPrivateProperties', 'classProperties'],
500505
});
501506
}
502507

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
"singleQuote": true
2020
},
2121
"dependencies": {
22+
"@babel/generator": "7.18.2",
2223
"@babel/parser": "7.18.4",
2324
"@babel/types": "7.18.4",
2425
"chalk": "^4.1.2",
25-
"escodegen": "^2.0.0",
2626
"fs-extra": "^9.1.0",
2727
"globby": "^11.1.0",
2828
"into-stream": "^6.0.0",

typings/babel__generator.d.ts

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
declare module '@babel/generator' {
2+
import type * as t from '@babel/types';
3+
import type { DecodedSourceMap, Mapping } from '@jridgewell/gen-mapping';
4+
5+
export interface GeneratorOptions {
6+
/**
7+
* Optional string to add as a block comment at the start of the output file.
8+
*/
9+
auxiliaryCommentBefore?: string;
10+
11+
/**
12+
* Optional string to add as a block comment at the end of the output file.
13+
*/
14+
auxiliaryCommentAfter?: string;
15+
16+
/**
17+
* Function that takes a comment (as a string) and returns true if the comment should be included in the output.
18+
* By default, comments are included if `opts.comments` is `true` or if `opts.minifed` is `false` and the comment
19+
* contains `@preserve` or `@license`.
20+
*/
21+
shouldPrintComment?(comment: string): boolean;
22+
23+
/**
24+
* Attempt to use the same line numbers in the output code as in the source code (helps preserve stack traces).
25+
* Defaults to `false`.
26+
*/
27+
retainLines?: boolean;
28+
29+
/**
30+
* Retain parens around function expressions (could be used to change engine parsing behavior)
31+
* Defaults to `false`.
32+
*/
33+
retainFunctionParens?: boolean;
34+
35+
/**
36+
* Should comments be included in output? Defaults to `true`.
37+
*/
38+
comments?: boolean;
39+
40+
/**
41+
* Set to true to avoid adding whitespace for formatting. Defaults to the value of `opts.minified`.
42+
*/
43+
compact?: boolean | 'auto';
44+
45+
/**
46+
* Should the output be minified. Defaults to `false`.
47+
*/
48+
minified?: boolean;
49+
50+
/**
51+
* Set to true to reduce whitespace (but not as much as opts.compact). Defaults to `false`.
52+
*/
53+
concise?: boolean;
54+
55+
/**
56+
* Used in warning messages
57+
*/
58+
filename?: string;
59+
60+
/**
61+
* Enable generating source maps. Defaults to `false`.
62+
*/
63+
sourceMaps?: boolean;
64+
65+
/**
66+
* A root for all relative URLs in the source map.
67+
*/
68+
sourceRoot?: string;
69+
70+
/**
71+
* The filename for the source code (i.e. the code in the `code` argument).
72+
* This will only be used if `code` is a string.
73+
*/
74+
sourceFileName?: string;
75+
76+
/**
77+
* Set to true to run jsesc with "json": true to print "\u00A9" vs. "©";
78+
*/
79+
jsonCompatibleStrings?: boolean;
80+
81+
/**
82+
* Set to true to enable support for experimental decorators syntax before module exports.
83+
* Defaults to `false`.
84+
*/
85+
decoratorsBeforeExport?: boolean;
86+
87+
// /**
88+
// * Options for outputting jsesc representation.
89+
// */
90+
// jsescOption?: jsescOptions;
91+
92+
/**
93+
* For use with the recordAndTuple token.
94+
*/
95+
recordAndTupleSyntaxType?: 'hash' | 'bar';
96+
/**
97+
* For use with the Hack-style pipe operator.
98+
* Changes what token is used for pipe bodies’ topic references.
99+
*/
100+
topicToken?: '^^' | '@@' | '^' | '%' | '#';
101+
}
102+
103+
export interface GeneratorResult {
104+
code: string;
105+
map: {
106+
version: number;
107+
sources: string[];
108+
names: string[];
109+
sourceRoot?: string;
110+
sourcesContent?: string[];
111+
mappings: string;
112+
file: string;
113+
} | null;
114+
decodedMap: DecodedSourceMap | undefined;
115+
rawMappings: Mapping[] | undefined;
116+
}
117+
118+
export default function generate(
119+
ast: t.Node,
120+
opts?: GeneratorOptions,
121+
code?: string | { [filename: string]: string }
122+
): GeneratorResult;
123+
}

yarn.lock

Lines changed: 5 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
json5 "^2.2.1"
5858
semver "^6.3.0"
5959

60-
"@babel/generator@^7.18.2":
60+
"@babel/generator@7.18.2", "@babel/generator@^7.18.2":
6161
version "7.18.2"
6262
resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.18.2.tgz#33873d6f89b21efe2da63fe554460f3df1c5880d"
6363
integrity sha512-W1lG5vUwFvfMd8HVXqdfbuG7RuaSrTCCD8cl8fP8wOivdbtbIg2Db3IWUcgvfxKbbn6ZBGYRW/Zk1MIwK49mgw==
@@ -844,7 +844,7 @@ deep-extend@^0.6.0:
844844
resolved "https://registry.yarnpkg.com/deep-extend/-/deep-extend-0.6.0.tgz#c4fa7c95404a17a9c3e8ca7e1537312b736330ac"
845845
integrity sha512-LOHxIOaPYdHlJRtCQfDIVZtfw/ufM8+rVj649RIHzcm/vGwQRXFt6OPqIFWsm2XEMrNIEtWR64sY1LEKD2vAOA==
846846

847-
deep-is@^0.1.3, deep-is@~0.1.3:
847+
deep-is@^0.1.3:
848848
version "0.1.4"
849849
resolved "https://registry.yarnpkg.com/deep-is/-/deep-is-0.1.4.tgz#a6f2dce612fadd2ef1f519b73551f17e85199831"
850850
integrity sha512-oIPzksmTg4/MriiaYGO+okXDT7ztn/w3Eptv/+gSIdMdKsJo0u4CfYNFJPy+4SKMuCqGw2wxnA+URMg3t8a/bQ==
@@ -968,18 +968,6 @@ escape-string-regexp@^4.0.0:
968968
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz#14ba83a5d373e3d311e5afca29cf5bfad965bf34"
969969
integrity sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==
970970

971-
escodegen@^2.0.0:
972-
version "2.0.0"
973-
resolved "https://registry.yarnpkg.com/escodegen/-/escodegen-2.0.0.tgz#5e32b12833e8aa8fa35e1bf0befa89380484c7dd"
974-
integrity sha512-mmHKys/C8BFUGI+MAWNcSYoORYLMdPzjrknd2Vc+bUsjN5bXcr8EhrNB+UTqfL1y3I9c4fw2ihgtMPQLBRiQxw==
975-
dependencies:
976-
esprima "^4.0.1"
977-
estraverse "^5.2.0"
978-
esutils "^2.0.2"
979-
optionator "^0.8.1"
980-
optionalDependencies:
981-
source-map "~0.6.1"
982-
983971
eslint-config-airbnb-base@14.2.1, eslint-config-airbnb-base@^14.2.0, eslint-config-airbnb-base@^14.2.1:
984972
version "14.2.1"
985973
resolved "https://registry.yarnpkg.com/eslint-config-airbnb-base/-/eslint-config-airbnb-base-14.2.1.tgz#8a2eb38455dc5a312550193b319cdaeef042cd1e"
@@ -1134,7 +1122,7 @@ espree@^7.3.0, espree@^7.3.1:
11341122
acorn-jsx "^5.3.1"
11351123
eslint-visitor-keys "^1.3.0"
11361124

1137-
esprima@^4.0.0, esprima@^4.0.1:
1125+
esprima@^4.0.0:
11381126
version "4.0.1"
11391127
resolved "https://registry.yarnpkg.com/esprima/-/esprima-4.0.1.tgz#13b04cdb3e6c5d19df91ab6987a8695619b0aa71"
11401128
integrity sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==
@@ -1220,7 +1208,7 @@ fast-json-stable-stringify@^2.0.0:
12201208
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz#874bf69c6f404c2b5d99c481341399fd55892633"
12211209
integrity sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==
12221210

1223-
fast-levenshtein@^2.0.6, fast-levenshtein@~2.0.6:
1211+
fast-levenshtein@^2.0.6:
12241212
version "2.0.6"
12251213
resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz#3d8a5c66883a16a30ca8643e851f19baa7797917"
12261214
integrity sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc=
@@ -1777,14 +1765,6 @@ levn@^0.4.1:
17771765
prelude-ls "^1.2.1"
17781766
type-check "~0.4.0"
17791767

1780-
levn@~0.3.0:
1781-
version "0.3.0"
1782-
resolved "https://registry.yarnpkg.com/levn/-/levn-0.3.0.tgz#3b09924edf9f083c0490fdd4c0bc4421e04764ee"
1783-
integrity sha1-OwmSTt+fCDwEkP3UwLxEIeBHZO4=
1784-
dependencies:
1785-
prelude-ls "~1.1.2"
1786-
type-check "~0.3.2"
1787-
17881768
lines-and-columns@^1.1.6:
17891769
version "1.1.6"
17901770
resolved "https://registry.yarnpkg.com/lines-and-columns/-/lines-and-columns-1.1.6.tgz#1c00c743b433cd0a4e80758f7b64a57440d9ff00"
@@ -2066,18 +2046,6 @@ onetime@^5.1.0:
20662046
dependencies:
20672047
mimic-fn "^2.1.0"
20682048

2069-
optionator@^0.8.1:
2070-
version "0.8.3"
2071-
resolved "https://registry.yarnpkg.com/optionator/-/optionator-0.8.3.tgz#84fa1d036fe9d3c7e21d99884b601167ec8fb495"
2072-
integrity sha512-+IW9pACdk3XWmmTXG8m3upGUJst5XRGzxMRjXzAuJ1XnIFNvfhjjIuYkDvysnPQ7qzqVzLt78BCruntqRhWQbA==
2073-
dependencies:
2074-
deep-is "~0.1.3"
2075-
fast-levenshtein "~2.0.6"
2076-
levn "~0.3.0"
2077-
prelude-ls "~1.1.2"
2078-
type-check "~0.3.2"
2079-
word-wrap "~1.2.3"
2080-
20812049
optionator@^0.9.1:
20822050
version "0.9.1"
20832051
resolved "https://registry.yarnpkg.com/optionator/-/optionator-0.9.1.tgz#4f236a6373dae0566a6d43e1326674f50c291499"
@@ -2218,11 +2186,6 @@ prelude-ls@^1.2.1:
22182186
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396"
22192187
integrity sha512-vkcDPrRZo1QZLbn5RLGPpg/WmIQ65qoWWhcGKf/b5eplkkarX0m9z8ppCat4mlOqUsWpyNuYgO3VRyrYHSzX5g==
22202188

2221-
prelude-ls@~1.1.2:
2222-
version "1.1.2"
2223-
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"
2224-
integrity sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ=
2225-
22262189
prettier@2.6.2:
22272190
version "2.6.2"
22282191
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.6.2.tgz#e26d71a18a74c3d0f0597f55f01fb6c06c206032"
@@ -2464,11 +2427,6 @@ slice-ansi@^4.0.0:
24642427
astral-regex "^2.0.0"
24652428
is-fullwidth-code-point "^3.0.0"
24662429

2467-
source-map@~0.6.1:
2468-
version "0.6.1"
2469-
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
2470-
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
2471-
24722430
sprintf-js@~1.0.2:
24732431
version "1.0.3"
24742432
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
@@ -2696,13 +2654,6 @@ type-check@^0.4.0, type-check@~0.4.0:
26962654
dependencies:
26972655
prelude-ls "^1.2.1"
26982656

2699-
type-check@~0.3.2:
2700-
version "0.3.2"
2701-
resolved "https://registry.yarnpkg.com/type-check/-/type-check-0.3.2.tgz#5884cab512cf1d355e3fb784f30804b2b520db72"
2702-
integrity sha1-WITKtRLPHTVeP7eE8wgEsrUg23I=
2703-
dependencies:
2704-
prelude-ls "~1.1.2"
2705-
27062657
type-fest@^0.20.2:
27072658
version "0.20.2"
27082659
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.20.2.tgz#1bf207f4b28f91583666cb5fbd327887301cd5f4"
@@ -2788,7 +2739,7 @@ wide-align@^1.1.0:
27882739
dependencies:
27892740
string-width "^1.0.2 || 2 || 3 || 4"
27902741

2791-
word-wrap@^1.2.3, word-wrap@~1.2.3:
2742+
word-wrap@^1.2.3:
27922743
version "1.2.3"
27932744
resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.3.tgz#610636f6b1f703891bd34771ccb17fb93b47079c"
27942745
integrity sha512-Hz/mrNwitNRh/HUAtM/VT/5VH+ygD6DV7mYKZAtHOrbs8U7lvPS6xf7EJKMF0uW1KJCl0H701g3ZGus+muE5vQ==

0 commit comments

Comments
 (0)