Skip to content

Commit 1401f5d

Browse files
committed
feat: include private self dependency
1 parent 03112e9 commit 1401f5d

File tree

4 files changed

+269
-88
lines changed

4 files changed

+269
-88
lines changed

src/dependency.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ export class Dependency {
3434
* Create new dependency from package description.
3535
*
3636
* @param {Object} pkg Package description.
37+
* @param {boolean} self If the package is the "self" package.
3738
* @constructor
3839
*/
39-
constructor(pkg) {
40+
constructor(pkg, self) {
41+
this.self = self || false;
42+
4043
this.name = pkg.name || null;
4144
this.maintainers = pkg.maintainers || [];
4245
this.version = pkg.version || null;

src/license-plugin.js

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ class LicensePlugin {
162162
this.debug(`iterative over directory tree, starting with: ${dir}`);
163163

164164
while (dir) {
165-
if (!includeSelf && dir === this._cwd) {
165+
const isSelf = dir === this._cwd;
166+
if (isSelf && !includeSelf) {
166167
// No need to scan "self" if it's not explicitly allowed.
167168
break;
168169
}
@@ -172,7 +173,7 @@ class LicensePlugin {
172173
pkg = this._cache.get(dir);
173174
if (pkg) {
174175
this.debug(`found package.json in cache (package: ${pkg.name})`);
175-
this.addDependency(pkg);
176+
this.addDependency(pkg, isSelf);
176177
}
177178

178179
break;
@@ -215,14 +216,14 @@ class LicensePlugin {
215216
}
216217

217218
// Add the new dependency to the set of third-party dependencies.
218-
this.addDependency(pkg);
219+
this.addDependency(pkg, isSelf);
219220

220221
// We can stop now.
221222
break;
222223
}
223224
}
224225

225-
if (dir === this._cwd) {
226+
if (isSelf) {
226227
// If "self" has been scanned, no need to go up in the directory tree.
227228
break;
228229
}
@@ -295,9 +296,10 @@ class LicensePlugin {
295296
* Add new dependency to the bundle descriptor.
296297
*
297298
* @param {Object} pkg Dependency package information.
299+
* @param {boolean} self If the package is the "self" package.
298300
* @return {void}
299301
*/
300-
addDependency(pkg) {
302+
addDependency(pkg, self) {
301303
const name = pkg.name || '';
302304
if (!name) {
303305
this.warn('Trying to add dependency without any name, skipping it.');
@@ -307,7 +309,7 @@ class LicensePlugin {
307309
const version = pkg.version || '';
308310
const key = this._options.thirdParty?.multipleVersions ? `${name}@${version}` : name;
309311
if (!this._dependencies.has(key)) {
310-
this._dependencies.set(key, new Dependency(pkg));
312+
this._dependencies.set(key, new Dependency(pkg, self));
311313
}
312314
}
313315

@@ -325,9 +327,18 @@ class LicensePlugin {
325327
}
326328

327329
const includePrivate = thirdParty.includePrivate || false;
328-
const outputDependencies = [...this._dependencies.values()].filter((dependency) => (
329-
includePrivate || !dependency.private
330-
));
330+
const includeSelf = thirdParty.includeSelf || false;
331+
const outputDependencies = [...this._dependencies.values()].filter((dependency) => {
332+
if (dependency.self && includeSelf) {
333+
return true;
334+
}
335+
336+
if (!dependency.private) {
337+
return true;
338+
}
339+
340+
return includePrivate;
341+
});
331342

332343
if (_.isFunction(thirdParty)) {
333344
thirdParty(outputDependencies);
@@ -459,7 +470,7 @@ class LicensePlugin {
459470
/**
460471
* Scan for dependency violations and print a warning if some violations are found.
461472
*
462-
* @param {Array<Object>} outputDependencies The dependencies to scan.
473+
* @param {Array<Dependency>} outputDependencies The dependencies to scan.
463474
* @param {string} allow The allowed licenses as a SPDX pattern.
464475
* @return {void}
465476
*/
@@ -472,11 +483,18 @@ class LicensePlugin {
472483
/**
473484
* Scan dependency for a dependency violation.
474485
*
475-
* @param {Object} dependency The dependency to scan.
486+
* @param {Dependency} dependency The dependency to scan.
476487
* @param {string|function|object} allow The allowed licenses as a SPDX pattern, or a validator function.
477488
* @return {void}
478489
*/
479490
_scanLicenseViolation(dependency, allow) {
491+
if (dependency.self) {
492+
// Do not validate license for the "self" package.
493+
// It's likely this package will use a private/proprietary license, and we only want to detect
494+
// violations for third party dependencies.
495+
return;
496+
}
497+
480498
const testFn = _.isString(allow) || _.isFunction(allow) ? allow : allow.test;
481499
const isValid = _.isFunction(testFn) ? testFn(dependency) : licenseValidator.isValid(dependency, testFn);
482500
if (!isValid) {

test/dependency.spec.js

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { join } from './utils/join';
2727

2828
describe('Dependency', () => {
2929
it('should extract package fields', () => {
30+
const self = false;
3031
const pkg = {
3132
name: 'foo',
3233
version: '1.0.0',
@@ -52,9 +53,10 @@ describe('Dependency', () => {
5253
],
5354
};
5455

55-
const dependency = new Dependency(pkg);
56+
const dependency = new Dependency(pkg, self);
5657

5758
expect(dependency).toEqual({
59+
self: false,
5860
name: 'foo',
5961
version: '1.0.0',
6062
license: 'MIT',
@@ -89,6 +91,7 @@ describe('Dependency', () => {
8991
});
9092

9193
it('should parse author field', () => {
94+
const self = false;
9295
const pkg = {
9396
name: 'foo',
9497
version: '1.0.0',
@@ -97,7 +100,7 @@ describe('Dependency', () => {
97100
author: 'Mickael Jeanroy <[email protected]> (https://mjeanroy.com)',
98101
};
99102

100-
const dependency = new Dependency(pkg);
103+
const dependency = new Dependency(pkg, self);
101104

102105
expect(dependency.author).toEqual({
103106
name: 'Mickael Jeanroy',
@@ -107,6 +110,7 @@ describe('Dependency', () => {
107110
});
108111

109112
it('should parse contributors field', () => {
113+
const self = false;
110114
const pkg = {
111115
name: 'foo',
112116
version: '1.0.0',
@@ -117,7 +121,7 @@ describe('Dependency', () => {
117121
],
118122
};
119123

120-
const dependency = new Dependency(pkg);
124+
const dependency = new Dependency(pkg, self);
121125

122126
expect(dependency.contributors.length).toBe(1);
123127
expect(dependency.contributors[0]).toEqual({
@@ -128,6 +132,7 @@ describe('Dependency', () => {
128132
});
129133

130134
it('should parse deprecated licenses field', () => {
135+
const self = false;
131136
const pkg = {
132137
name: 'foo',
133138
version: '1.0.0',
@@ -138,20 +143,21 @@ describe('Dependency', () => {
138143
],
139144
};
140145

141-
const dependency = new Dependency(pkg);
146+
const dependency = new Dependency(pkg, self);
142147

143148
expect(dependency.licenses).not.toBeDefined();
144149
expect(dependency.license).toBe('(MIT OR Apache 2.0)');
145150
});
146151

147152
it('should format dependency with name, version, and license fields', () => {
153+
const self = false;
148154
const pkg = {
149155
name: 'foo',
150156
version: '1.0.0',
151157
license: 'MIT',
152158
};
153159

154-
const dependency = new Dependency(pkg);
160+
const dependency = new Dependency(pkg, self);
155161

156162
expect(dependency.text()).toEqual(join([
157163
`Name: ${pkg.name}`,
@@ -162,14 +168,15 @@ describe('Dependency', () => {
162168
});
163169

164170
it('should format dependency with optional description fied', () => {
171+
const self = false;
165172
const pkg = {
166173
name: 'foo',
167174
version: '1.0.0',
168175
license: 'MIT',
169176
description: 'Desc',
170177
};
171178

172-
const dependency = new Dependency(pkg);
179+
const dependency = new Dependency(pkg, self);
173180

174181
expect(dependency.text()).toEqual(join([
175182
`Name: ${pkg.name}`,
@@ -181,6 +188,7 @@ describe('Dependency', () => {
181188
});
182189

183190
it('should format dependency with optional author field', () => {
191+
const self = false;
184192
const pkg = {
185193
name: 'foo',
186194
version: '1.0.0',
@@ -191,7 +199,7 @@ describe('Dependency', () => {
191199
},
192200
};
193201

194-
const dependency = new Dependency(pkg);
202+
const dependency = new Dependency(pkg, self);
195203

196204
expect(dependency.text()).toEqual(join([
197205
`Name: ${pkg.name}`,
@@ -203,6 +211,7 @@ describe('Dependency', () => {
203211
});
204212

205213
it('should format dependency with optional repository field', () => {
214+
const self = false;
206215
const pkg = {
207216
name: 'foo',
208217
version: '1.0.0',
@@ -213,7 +222,7 @@ describe('Dependency', () => {
213222
},
214223
};
215224

216-
const dependency = new Dependency(pkg);
225+
const dependency = new Dependency(pkg, self);
217226

218227
expect(dependency.text()).toEqual(join([
219228
`Name: ${pkg.name}`,
@@ -225,14 +234,15 @@ describe('Dependency', () => {
225234
});
226235

227236
it('should format dependency with optional homepage field', () => {
237+
const self = false;
228238
const pkg = {
229239
name: 'foo',
230240
version: '1.0.0',
231241
license: 'MIT',
232242
homepage: 'https://github.com/mjeanroy',
233243
};
234244

235-
const dependency = new Dependency(pkg);
245+
const dependency = new Dependency(pkg, self);
236246

237247
expect(dependency.text()).toEqual(join([
238248
`Name: ${pkg.name}`,
@@ -244,6 +254,7 @@ describe('Dependency', () => {
244254
});
245255

246256
it('should format dependency with optional contributors field', () => {
257+
const self = false;
247258
const pkg = {
248259
name: 'foo',
249260
version: '1.0.0',
@@ -254,7 +265,7 @@ describe('Dependency', () => {
254265
],
255266
};
256267

257-
const dependency = new Dependency(pkg);
268+
const dependency = new Dependency(pkg, self);
258269

259270
expect(dependency.text()).toEqual(join([
260271
`Name: ${pkg.name}`,
@@ -268,6 +279,7 @@ describe('Dependency', () => {
268279
});
269280

270281
it('should format dependency with all optional fields', () => {
282+
const self = false;
271283
const pkg = {
272284
name: 'foo',
273285
version: '1.0.0',
@@ -282,7 +294,7 @@ describe('Dependency', () => {
282294
],
283295
};
284296

285-
const dependency = new Dependency(pkg);
297+
const dependency = new Dependency(pkg, self);
286298

287299
expect(dependency.text()).toEqual(join([
288300
`Name: ${pkg.name}`,
@@ -300,6 +312,7 @@ describe('Dependency', () => {
300312
});
301313

302314
it('should format dependency with license text', () => {
315+
const self = false;
303316
const pkg = {
304317
name: 'foo',
305318
version: '1.0.0',
@@ -326,7 +339,7 @@ describe('Dependency', () => {
326339
],
327340
};
328341

329-
const dependency = new Dependency(pkg);
342+
const dependency = new Dependency(pkg, self);
330343

331344
expect(dependency.text()).toEqual(join([
332345
`Name: ${pkg.name}`,
@@ -348,6 +361,7 @@ describe('Dependency', () => {
348361
});
349362

350363
it('should format dependency with notice text', () => {
364+
const self = false;
351365
const pkg = {
352366
name: 'foo',
353367
version: '1.0.0',
@@ -358,7 +372,7 @@ describe('Dependency', () => {
358372
homepage: 'https://github.com/mjeanroy',
359373
};
360374

361-
const dependency = new Dependency(pkg);
375+
const dependency = new Dependency(pkg, self);
362376

363377
expect(dependency.text()).toEqual(join([
364378
`Name: ${pkg.name}`,

0 commit comments

Comments
 (0)