Skip to content

Commit d45d37a

Browse files
devversionkara
authored andcommitted
build: tslint rule to ensure license banners are set (#5133)
1 parent a555108 commit d45d37a

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

build-config.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ const buildVersion = require('./package.json').version;
99

1010
/** License that will be placed inside of all created bundles. */
1111
const buildLicense = `/**
12-
* @license Angular Material v${buildVersion}
13-
* Copyright (c) 2017 Google, Inc. https://material.angular.io/
14-
* License: MIT
15-
*/`;
12+
* @license
13+
* Copyright Google Inc. All Rights Reserved.
14+
*
15+
* Use of this source code is governed by an MIT-style license that can be
16+
* found in the LICENSE file at https://angular.io/license
17+
*/`;
1618

1719
module.exports = {
1820
projectVersion: buildVersion,
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const Lint = require('tslint');
2+
const path = require('path');
3+
const buildConfig = require('../../build-config');
4+
5+
/** Paths to the directories that are public packages and should be validated. */
6+
const packageDirs = [
7+
path.join(buildConfig.packagesDir, 'lib'),
8+
path.join(buildConfig.packagesDir, 'cdk')
9+
];
10+
11+
/** License banner that is placed at the top of every public TypeScript file. */
12+
const licenseBanner = buildConfig.licenseBanner;
13+
14+
/** Failure message that will be shown if a license banner is missing. */
15+
const ERROR_MESSAGE = 'Missing license header in this TypeScript file. ' +
16+
'Every TypeScript file of the library needs to have the Google license banner at the top.';
17+
18+
/** TSLint fix that can be used to add the license banner easily. */
19+
const tslintFix = Lint.Replacement.appendText(0, licenseBanner + '\n\n');
20+
21+
/**
22+
* Rule that walks through all TypeScript files of public packages and shows failures if a
23+
* file does not have the license banner at the top of the file.
24+
*/
25+
class Rule extends Lint.Rules.AbstractRule {
26+
27+
apply(sourceFile) {
28+
return this.applyWithWalker(new RequireLicenseBannerWalker(sourceFile, this.getOptions()));
29+
}
30+
}
31+
32+
class RequireLicenseBannerWalker extends Lint.RuleWalker {
33+
34+
visitSourceFile(sourceFile) {
35+
const filePath = path.join(buildConfig.projectDir, sourceFile.fileName);
36+
37+
// Do not check TypeScript source files that are not inside of a public package.
38+
if (!packageDirs.some(packageDir => filePath.includes(packageDir))) {
39+
return;
40+
}
41+
42+
// Do not check source files inside of public packages that are spec or definition files.
43+
if (filePath.endsWith('.spec.ts') || filePath.endsWith('.d.ts')) {
44+
return;
45+
}
46+
47+
const fileContent = sourceFile.getFullText();
48+
const licenseCommentPos = fileContent.indexOf(licenseBanner);
49+
50+
if (licenseCommentPos !== 0) {
51+
return this.addFailureAt(0, 0, ERROR_MESSAGE, tslintFix);
52+
}
53+
}
54+
}
55+
56+
exports.Rule = Rule;

tslint.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
// Disallows importing the whole RxJS library. Submodules can be still imported.
7373
"import-blacklist": [true, "rxjs"],
7474
// Avoids inconsistent linebreak styles in source files. Forces developers to use LF linebreaks.
75-
"linebreak-style": [true, "LF"]
75+
"linebreak-style": [true, "LF"],
76+
"require-license-banner": true
7677
}
7778
}

0 commit comments

Comments
 (0)