From e7d886881ce070fe76ce72ce703064336ab71cb4 Mon Sep 17 00:00:00 2001 From: James Sumners Date: Fri, 27 Dec 2024 12:52:12 -0500 Subject: [PATCH] fix header plugin --- eslint-plugin-newrelic-header.js | 71 ++++++++++++++++++++++++++++++++ eslint.config.js | 14 ++++++- 2 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 eslint-plugin-newrelic-header.js diff --git a/eslint-plugin-newrelic-header.js b/eslint-plugin-newrelic-header.js new file mode 100644 index 0000000000..23954eda11 --- /dev/null +++ b/eslint-plugin-newrelic-header.js @@ -0,0 +1,71 @@ +/* + * Copyright 2024 New Relic Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +'use strict' + +const headerTmpl = ` +/* + * Copyright {{year}} New Relic Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + */ +`.trim() + +const rule = { + meta: { + type: 'layout', + fixable: 'whitespace', + schema: false + }, + + create(context) { + return { + Program(node) { + const src = context.sourceCode.getText() + if (hasHeader(src) === true) { + return + } + context.report({ + loc: node.loc, + message: 'missing or invalid header', + fix(fixer) { + const rendered = headerTmpl.replace('{{year}}', new Date().getFullYear() + '') + '\n\n' + if (hasShebang(src) === true) { + return fixer.insertTextAfterRange([0, src.indexOf('\n')], '\n' + rendered) + } + return fixer.insertTextBefore( + node, + rendered + ) + } + }) + } + } + } +} + +module.exports = { + meta: { + name: 'eslint-plugin-newrelic-header', + version: '1.0.0' + }, + rules: { + header: rule + } +} + +function hasShebang(src) { + return /^#!\s?\//.test(src) +} + +function hasHeader(src) { + const headerLines = src.split('\n').slice(0, 5) + if (hasShebang(src) === true) { + headerLines.shift() + } + return headerLines[0] === '/*' && + / \* Copyright \d{4} New Relic Corporation\. All rights reserved\./.test(headerLines[1]) === true && + / \* SPDX-License-Identifier: Apache-2\.0/.test(headerLines[2]) === true && + headerLines[3] === ' */' +} diff --git a/eslint.config.js b/eslint.config.js index 69b6c33e4e..fa94b4defd 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -1,8 +1,14 @@ +/* + * Copyright 2024 New Relic Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + 'use strict' const neostandard = require('neostandard') const jsdoc = require('eslint-plugin-jsdoc') const sonarjs = require('eslint-plugin-sonarjs') +const header = require('./eslint-plugin-newrelic-header.js') // The new eslint configuration format is a simple array of configuration // objects. See https://eslint.org/docs/latest/use/configure/configuration-files#configuration-objects. @@ -20,9 +26,15 @@ const globalIgnores = { } const localConfig = { + plugins: { + header + }, + rules: { 'consistent-return': 'off', - 'header/header': 'off', + + // Enable file header checking and autocorrection. + 'header/header': 'error', // This one enforces `!!thing` syntax, which some folks find difficult // to read: