Skip to content

Commit 99e951e

Browse files
authored
chore: add dependency-check parser for mjs files (#151)
1 parent 916a70a commit 99e951e

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

packages/opentelemetry-node/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"lint:eslint": "eslint --ext=js,mjs,cjs . # requires node >=16.0.0",
3333
"lint:types": "rm -rf build/lint-types && tsc --outDir build/lint-types && diff -ur types build/lint-types",
3434
"lint:fix": "eslint --ext=js,mjs,cjs --fix . # requires node >=16.0.0",
35-
"lint:deps": "dependency-check require.js hook.mjs 'lib/**/*.js' 'test/**/*.js' -i @types/tape -i dotenv -i @opentelemetry/winston-transport # dep check does not like import.mjs",
35+
"lint:deps": "dependency-check require.js import.mjs hook.mjs 'lib/**/*.js' 'test/**/*.js' -e mjs:../../scripts/parse-mjs-source -i @types/tape -i dotenv -i @opentelemetry/winston-transport",
3636
"lint:license-files": "../../scripts/gen-notice.sh --lint . # requires node >=16",
3737
"lint:changelog": "../../scripts/extract-release-notes.sh .",
3838
"test": "NODE_OPTIONS='-r dotenv/config' DOTENV_CONFIG_PATH=./test/test-services.env tape test/**/*.test.js",
@@ -55,6 +55,7 @@
5555
"@opentelemetry/exporter-logs-otlp-proto": "^0.50.0",
5656
"@opentelemetry/exporter-metrics-otlp-proto": "^0.50.0",
5757
"@opentelemetry/host-metrics": "^0.35.0",
58+
"@opentelemetry/instrumentation": "^0.50.0",
5859
"@opentelemetry/instrumentation-bunyan": "^0.37.0",
5960
"@opentelemetry/instrumentation-express": "^0.37.0",
6061
"@opentelemetry/instrumentation-fastify": "^0.35.0",

scripts/parse-mjs-source.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
const acorn = require('acorn-node');
21+
const walk = require('acorn-node/walk');
22+
23+
function find(source, opts) {
24+
const ast = acorn.parse(source, {
25+
// to parse ESM
26+
sourceType: 'module',
27+
// top level await
28+
allowAwaitOutsideFunction: true,
29+
});
30+
31+
const modules = [];
32+
33+
// TODO: walk the AST and return the list of modues in
34+
// `strings` property
35+
walk.recursive(ast, null, {
36+
// import { a, b, c } from 'module';
37+
ImportDeclaration(node) {
38+
modules.push(node.source.value);
39+
},
40+
// export { a, b, c } from 'module';
41+
ExportNamedDeclaration(node) {
42+
modules.push(node.source.value);
43+
},
44+
// const x = await import('module');
45+
ExpressionStatement(node) {
46+
if (
47+
node.expression.type === 'AwaitExpression' &&
48+
node.expression.argument.type === 'CallExpression' &&
49+
node.expression.argument.callee.type === 'Import'
50+
) {
51+
modules.push(node.expression.argument.arguments[0].value);
52+
}
53+
},
54+
});
55+
56+
// Cleanup `node:` prefix for built-in modules
57+
return modules.map((m) => m.replace(/^node:/, ''));
58+
}
59+
60+
module.exports = function (src, opts) {
61+
return find(src, opts);
62+
};

0 commit comments

Comments
 (0)