-
Notifications
You must be signed in to change notification settings - Fork 653
Expand file tree
/
Copy pathenforce-ui-kit-named-import.ts
More file actions
54 lines (47 loc) · 1.61 KB
/
enforce-ui-kit-named-import.ts
File metadata and controls
54 lines (47 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import * as path from 'node:path';
import {createRule} from './utils/ruleCreator.ts';
import {isStarAsImportSpecifier} from './utils/treeHelpers.ts';
const UI_KIT_PATH = path.join('front_end', 'ui', 'kit', 'kit.js');
export default createRule({
name: 'enforce-ui-kit-named-import',
meta: {
type: 'problem',
docs: {
description: 'Enforce named imports for front_end/ui/kit/kit.js.',
category: 'Possible Errors',
},
messages: {
namedKitImport:
'Imports from front_end/ui/kit/kit.js must be named (e.g., `import {Card} from \'../../ui/kit/kit.js\'`).',
},
schema: [],
},
defaultOptions: [],
create: function(context) {
return {
ImportDeclaration(node) {
const importPath = node.source.value;
const normalizedImportPath = path.normalize(importPath);
const currentFileAbsolutePath = context.filename;
const currentFileDirectory = path.dirname(currentFileAbsolutePath);
const resolvedAbsoluteImportPath = path.resolve(currentFileDirectory, normalizedImportPath);
if (!resolvedAbsoluteImportPath.endsWith(UI_KIT_PATH)) {
return;
}
const importPathForErrorMessage = importPath.replace(/\\/g, '/');
if (isStarAsImportSpecifier(node.specifiers)) {
context.report({
node,
messageId: 'namedKitImport',
data: {
importPathForErrorMessage,
},
});
}
},
};
},
});