-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into cases_webhook
- Loading branch information
Showing
50 changed files
with
1,674 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/kbn-eslint-plugin-imports/src/helpers/repo_source_classifier.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import { ImportResolver } from '@kbn/import-resolver'; | ||
import { RepoSourceClassifier } from '@kbn/repo-source-classifier'; | ||
|
||
const cache = new WeakMap<ImportResolver, RepoSourceClassifier>(); | ||
|
||
/** | ||
* Gets the instance of RepoSourceClassifier that should be used. We cache these instances | ||
* key'd off of ImportResolver instances because the caches maintained by the RepoSourceClassifer | ||
* should live the same amount of time. Both classes assume that the files on disk are | ||
* relatively "stable" for the lifetime of the object and once the files are assumed | ||
* to have change that a new object will be created and the old version with the old | ||
* caches will be thrown away and garbage collected. | ||
*/ | ||
export function getRepoSourceClassifier(resolver: ImportResolver) { | ||
const cached = cache.get(resolver); | ||
if (cached) { | ||
return cached; | ||
} | ||
|
||
const classifier = new RepoSourceClassifier(resolver); | ||
cache.set(resolver, classifier); | ||
return classifier; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import { Rule } from 'eslint'; | ||
|
||
/** | ||
* Get the path of the sourcefile being linted | ||
*/ | ||
export function getSourcePath(context: Rule.RuleContext) { | ||
const sourceFilename = context.getPhysicalFilename | ||
? context.getPhysicalFilename() | ||
: context.getFilename(); | ||
|
||
if (!sourceFilename) { | ||
throw new Error('unable to determine sourceFilename for file being linted'); | ||
} | ||
|
||
return sourceFilename; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
packages/kbn-eslint-plugin-imports/src/rules/no_boundary_crossing.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import { RuleTester } from 'eslint'; | ||
import { NoBoundaryCrossingRule } from './no_boundary_crossing'; | ||
import { ModuleType } from '@kbn/repo-source-classifier'; | ||
import dedent from 'dedent'; | ||
|
||
const make = (from: ModuleType, to: ModuleType, imp = 'import') => ({ | ||
filename: `${from}.ts`, | ||
code: dedent` | ||
${imp} '${to}' | ||
`, | ||
}); | ||
|
||
jest.mock('../get_import_resolver', () => { | ||
return { | ||
getImportResolver() { | ||
return { | ||
resolve(req: string) { | ||
return { | ||
type: 'file', | ||
absolute: { | ||
type: req, | ||
}, | ||
}; | ||
}, | ||
}; | ||
}, | ||
}; | ||
}); | ||
|
||
jest.mock('../helpers/repo_source_classifier', () => { | ||
return { | ||
getRepoSourceClassifier() { | ||
return { | ||
classify(r: string | { type: string }) { | ||
return { | ||
type: typeof r === 'string' ? (r.endsWith('.ts') ? r.slice(0, -3) : r) : r.type, | ||
}; | ||
}, | ||
}; | ||
}, | ||
}; | ||
}); | ||
|
||
const tsTester = [ | ||
'@typescript-eslint/parser', | ||
new RuleTester({ | ||
parser: require.resolve('@typescript-eslint/parser'), | ||
parserOptions: { | ||
sourceType: 'module', | ||
ecmaVersion: 2018, | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
}), | ||
] as const; | ||
|
||
const babelTester = [ | ||
'@babel/eslint-parser', | ||
new RuleTester({ | ||
parser: require.resolve('@babel/eslint-parser'), | ||
parserOptions: { | ||
sourceType: 'module', | ||
ecmaVersion: 2018, | ||
requireConfigFile: false, | ||
babelOptions: { | ||
presets: ['@kbn/babel-preset/node_preset'], | ||
}, | ||
}, | ||
}), | ||
] as const; | ||
|
||
for (const [name, tester] of [tsTester, babelTester]) { | ||
describe(name, () => { | ||
tester.run('@kbn/imports/no_boundary_crossing', NoBoundaryCrossingRule, { | ||
valid: [ | ||
make('common package', 'common package'), | ||
make('server package', 'common package'), | ||
make('browser package', 'common package'), | ||
make('server package', 'server package'), | ||
make('browser package', 'browser package'), | ||
make('tests or mocks', 'common package'), | ||
make('tests or mocks', 'browser package'), | ||
make('tests or mocks', 'server package'), | ||
make('tests or mocks', 'tests or mocks'), | ||
make('browser package', 'server package', 'import type { Foo } from'), | ||
make('server package', 'browser package', 'import type { Foo } from'), | ||
make('common package', 'browser package', 'import type { Foo } from'), | ||
], | ||
|
||
invalid: [ | ||
{ | ||
...make('common package', 'server package'), | ||
errors: [ | ||
{ | ||
line: 1, | ||
messageId: 'TYPE_MISMATCH', | ||
data: { | ||
importedType: 'server package', | ||
ownType: 'common package', | ||
suggestion: ` ${dedent` | ||
Suggestions: | ||
- Remove the import statement. | ||
- Limit your imports to "common package" or "static" code. | ||
- Covert to a type-only import. | ||
- Reach out to #kibana-operations for help. | ||
`}`, | ||
}, | ||
}, | ||
], | ||
}, | ||
{ | ||
...make('server package', 'tests or mocks'), | ||
errors: [ | ||
{ | ||
line: 1, | ||
messageId: 'TYPE_MISMATCH', | ||
}, | ||
], | ||
}, | ||
{ | ||
...make('browser package', 'tests or mocks'), | ||
errors: [ | ||
{ | ||
line: 1, | ||
messageId: 'TYPE_MISMATCH', | ||
}, | ||
], | ||
}, | ||
{ | ||
...make('common package', 'server package'), | ||
errors: [ | ||
{ | ||
line: 1, | ||
messageId: 'TYPE_MISMATCH', | ||
}, | ||
], | ||
}, | ||
{ | ||
...make('common package', 'browser package'), | ||
errors: [ | ||
{ | ||
line: 1, | ||
messageId: 'TYPE_MISMATCH', | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
}); | ||
} |
Oops, something went wrong.