-
Notifications
You must be signed in to change notification settings - Fork 498
/
Copy pathenforce-default-import-name.ts
86 lines (77 loc) · 2.74 KB
/
enforce-default-import-name.ts
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2024 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import path from 'path';
import {createRule} from './utils/ruleCreator.ts';
import {isStarAsImportSpecifier} from './utils/treeHelpers.ts';
// Define the structure of the options expected by the rule.
type RuleOptions = [{
modulePath: string,
importName: string,
}];
// Define the message IDs used by the rule.
type MessageIds = 'invalidName';
export default createRule<RuleOptions, MessageIds>({
name: 'enforce-default-import-name',
meta: {
type: 'problem',
docs: {
description: 'enforce default names for certain module imports',
category: 'Possible Errors',
},
fixable: 'code', // Note: No fixer provided in the original, but meta field kept.
messages: {
invalidName: 'When importing {{importPath}}, the name used must be {{requiredName}}',
},
schema: {
type: 'array',
items: {
type: 'object',
properties: {
modulePath: {type: 'string'},
importName: {type: 'string'},
},
required: ['modulePath', 'importName'], // Added for schema completeness
additionalProperties: false, // Added for schema completeness
},
minItems: 0, // Allow empty options array
},
},
defaultOptions: [{
modulePath: '',
importName: '',
}],
create: function(context) {
const filename = context.filename;
const options = context.options;
const importingFileName = path.resolve(filename);
const importingDir = path.dirname(importingFileName);
return {
ImportDeclaration(node) {
if (!isStarAsImportSpecifier(node.specifiers)) {
// We only support checking `import * as X` based on the DevTools
// conventions for module imports.
return;
}
const importSourceValue = node.source.value;
const normalizedImportPath = path.normalize(importSourceValue);
const importPathForErrorMessage = importSourceValue.replace(/\\/g, '/');
const absoluteImportPath = path.resolve(importingDir, normalizedImportPath);
const importNameInCode = node.specifiers[0].local.name;
for (const check of options) {
const absoluteCheckPath = path.resolve(check.modulePath);
if (absoluteImportPath === absoluteCheckPath && importNameInCode !== check.importName) {
context.report({
messageId: 'invalidName',
node: node.specifiers[0].local,
data: {
importPath: importPathForErrorMessage,
requiredName: check.importName,
},
});
}
}
},
};
},
});