From 40a355a42d4a9444dc753c04c6608dade2f06a23 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 03:13:27 +0200 Subject: Adding upstream version 125.0.1. Signed-off-by: Daniel Baumann --- remote/test/puppeteer/tools/eslint/package.json | 3 +- .../puppeteer/tools/eslint/src/check-license.ts | 67 +++++++++++++--------- remote/test/puppeteer/tools/eslint/tsconfig.json | 5 +- 3 files changed, 44 insertions(+), 31 deletions(-) (limited to 'remote/test/puppeteer/tools/eslint') diff --git a/remote/test/puppeteer/tools/eslint/package.json b/remote/test/puppeteer/tools/eslint/package.json index 190367ae43..c7f7f4f38d 100644 --- a/remote/test/puppeteer/tools/eslint/package.json +++ b/remote/test/puppeteer/tools/eslint/package.json @@ -32,6 +32,7 @@ "author": "The Chromium Authors", "license": "Apache-2.0", "devDependencies": { - "@prettier/sync": "0.5.0" + "@prettier/sync": "0.5.1", + "@typescript-eslint/utils": "7.1.0" } } diff --git a/remote/test/puppeteer/tools/eslint/src/check-license.ts b/remote/test/puppeteer/tools/eslint/src/check-license.ts index 7ae1a54384..b8590a7c3f 100644 --- a/remote/test/puppeteer/tools/eslint/src/check-license.ts +++ b/remote/test/puppeteer/tools/eslint/src/check-license.ts @@ -11,15 +11,16 @@ const createRule = ESLintUtils.RuleCreator(name => { return `https://github.com/puppeteer/puppeteer/tree/main/tools/eslint/${name}.ts`; }); -const copyrightPattern = /Copyright ([0-9]{4}) Google Inc\./; +const currentYear = new Date().getFullYear(); -// const currentYear = new Date().getFullYear; - -// const licenseHeader = `/** -// * @license -// * Copyright ${currentYear} Google Inc. -// * SPDX-License-Identifier: Apache-2.0 -// */`; +// Needs to start and end with new line +const licenseHeader = ` +/** + * @license + * Copyright ${currentYear} Google Inc. + * SPDX-License-Identifier: Apache-2.0 + */ +`; const enforceLicenseRule = createRule<[], 'licenseRule'>({ name: 'check-license', @@ -29,7 +30,7 @@ const enforceLicenseRule = createRule<[], 'licenseRule'>({ description: 'Validate existence of license header', requiresTypeChecking: false, }, - fixable: undefined, // TODO: change to 'code' once fixer works. + fixable: 'code', schema: [], messages: { licenseRule: 'Add license header.', @@ -39,40 +40,52 @@ const enforceLicenseRule = createRule<[], 'licenseRule'>({ create(context) { const sourceCode = context.sourceCode; const comments = sourceCode.getAllComments(); - const header = - comments[0]?.type === 'Block' && isHeaderComment(comments[0]) - ? comments[0] - : null; - - function isHeaderComment(comment: TSESTree.Comment) { - if (comment && comment.range[0] >= 0 && comment.range[1] <= 88) { - return true; - } else { - return false; + let insertAfter = [0, 0] as TSESTree.Range; + let header: TSESTree.Comment | null = null; + // Check only the first 2 comments + for (let index = 0; index < 2; index++) { + const comment = comments[index]; + if (!comment) { + break; + } + // Shebang comments should be at the top + if ( + // Types don't have it debugger showed it... + (comment.type as string) === 'Shebang' || + (comment.type === 'Line' && comment.value.startsWith('#!')) + ) { + insertAfter = comment.range; + continue; + } + if (comment.type === 'Block') { + header = comment; + break; } } return { Program(node) { + if (context.filename.endsWith('.json')) { + return; + } + if ( header && - header.value.includes('@license') && - header.value.includes('SPDX-License-Identifier: Apache-2.0') && - copyrightPattern.test(header.value) + (header.value.includes('@license') || + header.value.includes('License') || + header.value.includes('Copyright')) ) { return; } // Add header license if (!header || !header.value.includes('@license')) { - // const startLoc: [number, number] = [0, 88]; context.report({ node: node, messageId: 'licenseRule', - // TODO: fix the fixer. - // fix(fixer) { - // return fixer.insertTextBeforeRange(startLoc, licenseHeader); - // }, + fix(fixer) { + return fixer.insertTextAfterRange(insertAfter, licenseHeader); + }, }); } }, diff --git a/remote/test/puppeteer/tools/eslint/tsconfig.json b/remote/test/puppeteer/tools/eslint/tsconfig.json index da26cc936b..3a71788a21 100644 --- a/remote/test/puppeteer/tools/eslint/tsconfig.json +++ b/remote/test/puppeteer/tools/eslint/tsconfig.json @@ -7,8 +7,7 @@ "outDir": "./lib", "declaration": false, "declarationMap": false, - "sourceMap": false, "composite": false, - "removeComments": true, - }, + "removeComments": true + } } -- cgit v1.2.3