Skip to content

Commit

Permalink
🏗 Lint forbidden terms from presubmit (#33462)
Browse files Browse the repository at this point in the history
CI cycles on my PRs are often delayed because I use a forbidden term. It would be much easier if I could correct these uses pre-emptively, so introducing a lint rule can help.

1. Refactors `presubmit-checks.js` so that matches can be returned instead of logged.
2. Introduces eslint rule `local/no-forbidden-terms` that reports results like `amp presubmit`
  • Loading branch information
alanorozco committed Mar 25, 2021
1 parent 90769d4 commit b102e9b
Show file tree
Hide file tree
Showing 8 changed files with 1,521 additions and 1,509 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ module.exports = {
'local/no-es2015-number-props': 2,
'local/no-export-side-effect': 2,
'local/no-for-of-statement': 2,
'local/no-forbidden-terms': 2,
'local/no-function-async': 2,
'local/no-function-generator': 2,
'local/no-global': 0,
Expand Down Expand Up @@ -320,6 +321,7 @@ module.exports = {
'require': false,
},
'rules': {
'local/no-forbidden-terms': 0,
'local/no-module-exports': 0,
},
},
Expand Down
41 changes: 41 additions & 0 deletions build-system/eslint-rules/no-forbidden-terms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright 2021 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const {getForbiddenTerms} = require('../test-configs/forbidden-terms');
const {relative} = require('path');

/**
* @fileoverview
* Reports forbidden terms found by regex.
* See test-configs/forbidden-terms.js
*/

module.exports = function (context) {
return {
Program() {
const filename = relative(process.cwd(), context.getFilename());
const sourceCode = context.getSourceCode();

for (const report of getForbiddenTerms(filename, sourceCode.text)) {
const {match, message, loc} = report;
context.report({
loc,
message: `Forbidden: "${match}".${message ? `\n${message}.` : ''}`,
});
}
},
};
};

0 comments on commit b102e9b

Please sign in to comment.