mirror of
https://github.com/elastic/kibana.git
synced 2025-06-27 18:51:07 -04:00
Implement package linter (#148496)
This PR implements a linter like the TS Project linter, except for packages in the repo. It does this by extracting the reusable bits from the TS Project linter and reusing them for the project linter. The only rule that exists for packages right now is that the "name" in the package.json file matches the "id" in Kibana.jsonc. The goal is to use a rule to migrate kibana.json files on the future. Additionally, a new rule for validating the indentation of tsconfig.json files was added. Validating and fixing violations is what has triggered review by so many teams, but we plan to treat those review requests as notifications of the changes and not as blockers for merging. Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
parent
039ed991d8
commit
d6be4a4b06
218 changed files with 3917 additions and 1650 deletions
3
packages/kbn-import-locator/README.md
Normal file
3
packages/kbn-import-locator/README.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
# @kbn/import-locator
|
||||
|
||||
ImportLocator is extracted from nx and used to very quickly find import statements in JS/TS code.
|
82
packages/kbn-import-locator/import_locator.ts
Normal file
82
packages/kbn-import-locator/import_locator.ts
Normal file
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License
|
||||
* 2.0 and the Server Side Public License, v 1; you may not use this file except
|
||||
* in compliance with, at your election, the Elastic License 2.0 or the Server
|
||||
* Side Public License, v 1.
|
||||
*/
|
||||
|
||||
// based on the code in https://github.com/nrwl/nx/blob/e12922b02908c90797e038324f2afa4bf69e2eab/packages/nx/src/project-graph/build-dependencies/typescript-import-locator.ts#L62
|
||||
// simplified to focuse on what we need, see license info in ./strip_source_code
|
||||
|
||||
import Fsp from 'fs/promises';
|
||||
import Ts from 'typescript';
|
||||
|
||||
import { stripSourceCode } from './strip_source_code';
|
||||
|
||||
const EMPTY = new Set<string>();
|
||||
|
||||
export class ImportLocator {
|
||||
private readonly scanner: Ts.Scanner;
|
||||
|
||||
constructor() {
|
||||
this.scanner = Ts.createScanner(Ts.ScriptTarget.Latest, false, Ts.LanguageVariant.JSX);
|
||||
}
|
||||
|
||||
get(path: string, content: string): Set<string> {
|
||||
const strippedContent = stripSourceCode(this.scanner, content);
|
||||
if (strippedContent === '') {
|
||||
return EMPTY;
|
||||
}
|
||||
|
||||
const imports = new Set<string>();
|
||||
const queue: Ts.Node[] = [
|
||||
Ts.createSourceFile(path, strippedContent, Ts.ScriptTarget.Latest, true),
|
||||
];
|
||||
const addNodeToQueue = (n: Ts.Node) => {
|
||||
queue.push(n);
|
||||
};
|
||||
|
||||
while (queue.length) {
|
||||
const node = queue.shift()!;
|
||||
|
||||
if (
|
||||
(Ts.isImportDeclaration(node) || Ts.isExportDeclaration(node)) &&
|
||||
node.moduleSpecifier &&
|
||||
Ts.isStringLiteral(node.moduleSpecifier)
|
||||
) {
|
||||
imports.add(node.moduleSpecifier.text);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (
|
||||
Ts.isCallExpression(node) &&
|
||||
node.expression.kind === Ts.SyntaxKind.ImportKeyword &&
|
||||
node.arguments.length === 1 &&
|
||||
Ts.isStringLiteral(node.arguments[0])
|
||||
) {
|
||||
imports.add(node.arguments[0].text);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (
|
||||
Ts.isCallExpression(node) &&
|
||||
node.expression.getText() === 'require' &&
|
||||
node.arguments.length === 1 &&
|
||||
Ts.isStringLiteral(node.arguments[0])
|
||||
) {
|
||||
imports.add(node.arguments[0].text);
|
||||
continue;
|
||||
}
|
||||
|
||||
Ts.forEachChild(node, addNodeToQueue);
|
||||
}
|
||||
|
||||
return imports;
|
||||
}
|
||||
|
||||
async read(path: string): Promise<Set<string>> {
|
||||
const content = await Fsp.readFile(path, 'utf8');
|
||||
return this.get(path, content);
|
||||
}
|
||||
}
|
13
packages/kbn-import-locator/jest.config.js
Normal file
13
packages/kbn-import-locator/jest.config.js
Normal file
|
@ -0,0 +1,13 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License
|
||||
* 2.0 and the Server Side Public License, v 1; you may not use this file except
|
||||
* in compliance with, at your election, the Elastic License 2.0 or the Server
|
||||
* Side Public License, v 1.
|
||||
*/
|
||||
|
||||
module.exports = {
|
||||
preset: '@kbn/test/jest_node',
|
||||
rootDir: '../..',
|
||||
roots: ['<rootDir>/packages/kbn-import-locator'],
|
||||
};
|
6
packages/kbn-import-locator/kibana.jsonc
Normal file
6
packages/kbn-import-locator/kibana.jsonc
Normal file
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"type": "shared-common",
|
||||
"id": "@kbn/import-locator",
|
||||
"owner": "@elastic/kibana-operations",
|
||||
"devOnly": true
|
||||
}
|
7
packages/kbn-import-locator/package.json
Normal file
7
packages/kbn-import-locator/package.json
Normal file
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"name": "@kbn/import-locator",
|
||||
"private": true,
|
||||
"version": "1.0.0",
|
||||
"license": "SSPL-1.0 OR Elastic License 2.0",
|
||||
"main": "./import_locator"
|
||||
}
|
137
packages/kbn-import-locator/strip_source_code.ts
Normal file
137
packages/kbn-import-locator/strip_source_code.ts
Normal file
|
@ -0,0 +1,137 @@
|
|||
/* eslint-disable @kbn/eslint/require-license-header */
|
||||
/* eslint-disable @typescript-eslint/no-shadow */
|
||||
|
||||
// pulled from https://github.com/nrwl/nx/blob/e12922b02908c90797e038324f2afa4bf69e2eab/packages/nx/src/utils/strip-source-code.ts#L4
|
||||
|
||||
/**
|
||||
* @notice
|
||||
*
|
||||
* This project includes code from the NX project, which is MIT licensed:
|
||||
*
|
||||
* (The MIT License)
|
||||
*
|
||||
* Copyright (c) 2017-2022 Narwhal Technologies Inc.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* 'Software'), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { SyntaxKind } from 'typescript';
|
||||
import type { Scanner } from 'typescript';
|
||||
|
||||
export function stripSourceCode(scanner: Scanner, contents: string): string {
|
||||
scanner.setText(contents);
|
||||
let token = scanner.scan();
|
||||
const statements = [];
|
||||
let start = null;
|
||||
while (token !== SyntaxKind.EndOfFileToken) {
|
||||
const potentialStart = scanner.getStartPos();
|
||||
switch (token) {
|
||||
case SyntaxKind.MultiLineCommentTrivia:
|
||||
case SyntaxKind.SingleLineCommentTrivia: {
|
||||
const isMultiLineCommentTrivia = token === SyntaxKind.MultiLineCommentTrivia;
|
||||
const start = potentialStart + 2;
|
||||
token = scanner.scan();
|
||||
const end = scanner.getStartPos() - (isMultiLineCommentTrivia ? 2 : 0);
|
||||
const comment = contents.substring(start, end).trim();
|
||||
if (comment === 'nx-ignore-next-line') {
|
||||
// reading till the end of the line
|
||||
while (token === SyntaxKind.WhitespaceTrivia || token === SyntaxKind.NewLineTrivia) {
|
||||
token = scanner.scan();
|
||||
}
|
||||
|
||||
// ignore next line
|
||||
while (token !== SyntaxKind.NewLineTrivia && token !== SyntaxKind.EndOfFileToken) {
|
||||
token = scanner.scan();
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case SyntaxKind.RequireKeyword:
|
||||
case SyntaxKind.ImportKeyword: {
|
||||
token = scanner.scan();
|
||||
while (token === SyntaxKind.WhitespaceTrivia || token === SyntaxKind.NewLineTrivia) {
|
||||
token = scanner.scan();
|
||||
}
|
||||
start = potentialStart;
|
||||
break;
|
||||
}
|
||||
|
||||
case SyntaxKind.ExportKeyword: {
|
||||
token = scanner.scan();
|
||||
while (token === SyntaxKind.WhitespaceTrivia || token === SyntaxKind.NewLineTrivia) {
|
||||
token = scanner.scan();
|
||||
}
|
||||
if (
|
||||
token === SyntaxKind.OpenBraceToken ||
|
||||
token === SyntaxKind.AsteriskToken ||
|
||||
token === SyntaxKind.TypeKeyword
|
||||
) {
|
||||
start = potentialStart;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case SyntaxKind.TemplateHead:
|
||||
case SyntaxKind.TemplateMiddle: {
|
||||
while (true) {
|
||||
token = scanner.scan();
|
||||
|
||||
if (token === SyntaxKind.SlashToken) {
|
||||
token = scanner.reScanSlashToken();
|
||||
}
|
||||
|
||||
if (token === SyntaxKind.EndOfFileToken) {
|
||||
// either the template is unterminated, or there
|
||||
// is some other edge case we haven't compensated for
|
||||
break;
|
||||
}
|
||||
|
||||
if (token === SyntaxKind.CloseBraceToken) {
|
||||
token = scanner.reScanTemplateToken(false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case SyntaxKind.StringLiteral: {
|
||||
if (start !== null) {
|
||||
token = scanner.scan();
|
||||
if (token === SyntaxKind.CloseParenToken) {
|
||||
token = scanner.scan();
|
||||
}
|
||||
const end = scanner.getStartPos();
|
||||
statements.push(contents.substring(start, end));
|
||||
start = null;
|
||||
} else {
|
||||
token = scanner.scan();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default: {
|
||||
token = scanner.scan();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return statements.join('\n');
|
||||
}
|
17
packages/kbn-import-locator/tsconfig.json
Normal file
17
packages/kbn-import-locator/tsconfig.json
Normal file
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "target/types",
|
||||
"types": [
|
||||
"jest",
|
||||
"node"
|
||||
]
|
||||
},
|
||||
"include": [
|
||||
"**/*.ts",
|
||||
],
|
||||
"exclude": [
|
||||
"target/**/*"
|
||||
],
|
||||
"kbn_references": []
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue