Skip to content

Commit c1f5462

Browse files
committed
change semver linting rules to new guidance to never pin semconv dep or use incubating
1 parent f84cced commit c1f5462

File tree

1 file changed

+17
-27
lines changed

1 file changed

+17
-27
lines changed

scripts/lint-semconv-deps.mjs

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ const TOP = path.resolve(fileURLToPath(new URL('.', import.meta.url)), '..');
3434
const SEMCONV = '@opentelemetry/semantic-conventions';
3535
const USE_COLOR = process.stdout.isTTY && !process.env.NO_COLOR?.length > 0;
3636

37+
let numProbs = 0;
3738
function problem(...args) {
39+
numProbs += 1;
3840
if (USE_COLOR) {
3941
process.stdout.write('\x1b[31m');
4042
}
41-
args.unshift('lint-semconv-deps error:')
43+
args.unshift('lint-semconv-deps error:');
4244
console.log(...args);
4345
if (USE_COLOR) {
4446
process.stdout.write('\x1b[39m');
@@ -56,53 +58,41 @@ function getAllWorkspaceDirs() {
5658
}
5759

5860
function lintSemconvDeps() {
59-
let numProbs = 0;
6061
const wsDirs = getAllWorkspaceDirs();
6162

6263
for (let wsDir of wsDirs) {
6364
const pj = JSON.parse(
6465
fs.readFileSync(path.join(wsDir, 'package.json'), 'utf8')
6566
);
6667
const depRange = pj?.dependencies?.[SEMCONV];
67-
if (!depRange) {
68+
const devDepRange = pj?.devDependencies?.[SEMCONV];
69+
if (!(depRange || devDepRange)) {
6870
continue;
6971
}
7072

71-
// Is incubating entry-point in use?
73+
// Rule: The semconv dep should *not* be pinned. Expect `^X.Y.Z`.
74+
const pinnedVerRe = /^\d+\.\d+\.\d+$/;
75+
if (depRange && pinnedVerRe.exec(depRange)) {
76+
problem(`${wsDir}/package.json: package ${pj.name} pins "${SEMCONV}" in dependencies, but should not (see https://github.com/open-telemetry/opentelemetry-js/tree/main/semantic-conventions#why-not-pin-the-version)`);
77+
} else if (devDepRange && pinnedVerRe.exec(devDepRange)) {
78+
problem(`${wsDir}/package.json: package ${pj.name} pins "${SEMCONV}" in devDependencies, but should not (see https://github.com/open-telemetry/opentelemetry-js/tree/main/semantic-conventions#why-not-pin-the-version)`);
79+
}
80+
81+
// Rule: The incubating entry-point should not be used.
7282
const srcFiles = globSync(path.join(wsDir, 'src', '**', '*.ts'));
73-
let usesIncubating = false;
74-
const usesIncubatingRe = /import \{?[^\{]* from '@opentelemetry\/semantic-conventions\/incubating'/s;
83+
const usesIncubatingRe = /import\s+\{?[^{;]*\s+from\s+'@opentelemetry\/semantic-conventions\/incubating'/s;
7584
for (let srcFile of srcFiles) {
7685
const srcText = fs.readFileSync(srcFile, 'utf8');
7786
const match = usesIncubatingRe.exec(srcText);
7887
if (match) {
79-
usesIncubating = true;
80-
break;
81-
}
82-
}
83-
84-
// Rule: If the semconv "incubating" entry-point is used, then the dep
85-
// should be pinned. Otherwise it should not be pinned.
86-
const pinnedVerRe = /^\d+\.\d+\.\d+$/;
87-
const pins = Boolean(pinnedVerRe.exec(depRange));
88-
if (usesIncubating) {
89-
if (!pins) {
90-
problem(`package ${pj.name} (in ${wsDir}) imports "${SEMCONV}/incubating" but does not *pin* the dependency: \`"${SEMCONV}": "${depRange}"\``);
91-
numProbs += 1;
92-
}
93-
} else {
94-
if (pins) {
95-
problem(`package ${pj.name} (in ${wsDir}) does not import "${SEMCONV}/incubating" but pins the dependency: \`"${SEMCONV}": "${depRange}"\` (it could use a caret-range)`);
96-
numProbs += 1;
88+
problem(`${srcFile}: uses the 'incubating' entry-point from '@opentelemetry/semantic-conventions', but should not (see https://github.com/open-telemetry/opentelemetry-js/tree/main/semantic-conventions#unstable-semconv)`)
9789
}
9890
}
9991
}
100-
101-
return numProbs;
10292
}
10393

10494
// mainline
105-
const numProbs = await lintSemconvDeps();
95+
await lintSemconvDeps();
10696
if (numProbs > 0) {
10797
process.exitCode = 1;
10898
}

0 commit comments

Comments
 (0)