-
Notifications
You must be signed in to change notification settings - Fork 498
/
Copy pathes-modules-import.ts
404 lines (363 loc) · 14.6 KB
/
es-modules-import.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// Copyright 2020 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.
/**
* @fileoverview Rule to check ES import usage
* @author Tim van der Lippe
*/
import type {TSESLint, TSESTree} from '@typescript-eslint/utils';
import path from 'path';
import {createRule} from './utils/ruleCreator.ts';
import {isStarAsImportSpecifier} from './utils/treeHelpers.ts';
// Define types based on TSESTree
type ImportDeclaration = TSESTree.ImportDeclaration;
type ExportNamedDeclaration = TSESTree.ExportNamedDeclaration;
type ImportSpecifier = TSESTree.ImportSpecifier;
type ImportDefaultSpecifier = TSESTree.ImportDefaultSpecifier;
type ImportNamespaceSpecifier = TSESTree.ImportNamespaceSpecifier;
type RuleContext = TSESLint.RuleContext<MessageIds, []>; // Define MessageIds below
type RuleFixer = TSESLint.RuleFixer;
// Define MessageIds used in the rule
type MessageIds = 'doubleSlashInImportPath'|'missingExtension'|'invalidRelativeUrl'|'incorrectSameNamespaceImportNamed'|
'incorrectSameNamespaceImportStar'|'crossNamespaceImport'|'crossNamespaceImportLs'|'crossNamespaceImportThirdParty'|
'incorrectSameNamespaceTestImport';
const FRONT_END_DIRECTORY = path.join(
// @ts-expect-error
import.meta.dirname,
'..',
'..',
'..',
'front_end',
);
const THIRD_PARTY_DIRECTORY = path.join(FRONT_END_DIRECTORY, 'third_party');
const INSPECTOR_OVERLAY_DIRECTORY = path.join(
// @ts-expect-error
import.meta.dirname,
'..',
'..',
'..',
'front_end',
'inspector_overlay',
);
const COMPONENT_DOCS_DIRECTORY = path.join(
FRONT_END_DIRECTORY,
'ui',
'components',
'docs',
);
const CROSS_NAMESPACE_MESSAGE =
'Incorrect cross-namespace import: "{{importPathForErrorMessage}}". Use "import * as Namespace from \'../namespace/namespace.js\';" instead.';
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
function isSideEffectImportSpecifier(
specifiers: Array<ImportSpecifier|ImportDefaultSpecifier|ImportNamespaceSpecifier>): boolean {
return specifiers.length === 0;
}
function isModuleEntrypoint(fileName: string): boolean {
const fileNameWithoutExtension = path.basename(fileName).replace(path.extname(fileName), '');
const directoryName = path.basename(path.dirname(fileName));
return (directoryName === fileNameWithoutExtension || `${directoryName}-testing` === fileNameWithoutExtension);
}
function computeTopLevelFolder(fileName: string): string {
const namespaceName = path.relative(FRONT_END_DIRECTORY, fileName);
return namespaceName.substring(0, namespaceName.indexOf(path.sep));
}
function checkImportExtension(
importPath: string,
importPathForErrorMessage: string,
context: RuleContext,
node: ImportDeclaration|ExportNamedDeclaration, // Node can be Import or Export
): void {
// detect import * as fs from 'fs';
if (!importPath.startsWith('.')) {
return;
}
if (!importPath.endsWith('.js') && !importPath.endsWith('.mjs')) {
context.report({
node,
messageId: 'missingExtension',
data: {
importPathForErrorMessage,
},
fix(fixer: RuleFixer) {
if (node.source) {
return fixer.replaceText(
node.source,
`'${importPathForErrorMessage}.js'`,
);
}
return null;
},
});
}
}
function nodeSpecifiersSpecialImportsOnly(
specifiers: Array<ImportSpecifier|ImportDefaultSpecifier|ImportNamespaceSpecifier>): boolean {
if (specifiers.length !== 1) {
return false;
}
const firstSpecifier = specifiers[0];
return (
firstSpecifier.type === 'ImportSpecifier' && firstSpecifier.imported.type === 'Identifier' &&
['ls', 'assertNotNullOrUndefined'].includes(firstSpecifier.imported.name));
}
function checkStarImport(
context: RuleContext,
node: ImportDeclaration,
importPath: string,
importPathForErrorMessage: string,
importingFileName: string,
exportingFileName: string,
): void {
if (isModuleEntrypoint(importingFileName)) {
return;
}
if (importingFileName.startsWith(COMPONENT_DOCS_DIRECTORY) &&
importPath.includes([path.sep, 'testing', path.sep].join(''))) {
return;
}
// The generated code is typically part of a different folder. Therefore,
// it is allowed to directly import these files, as they are only
// imported in 1 place at a time.
if (computeTopLevelFolder(exportingFileName) === 'generated') {
return;
}
const isSameFolder = path.dirname(importingFileName) === path.dirname(exportingFileName);
const invalidSameFolderUsage = isSameFolder && isModuleEntrypoint(exportingFileName);
const invalidCrossFolderUsage = !isSameFolder && !isModuleEntrypoint(exportingFileName);
if (invalidSameFolderUsage) {
// Meta files import their entrypoints and are considered separate entrypoints.
// Additionally, any file ending with `-entrypoint.ts` is considered an entrypoint
// as well. Therefore, they are allowed to import using a same-namespace star-import.
// For `.test.ts` files we also need to use the namespace import syntax, to access
// the module itself, so we need to allow this as well.
const importingFileIsEntrypointOrTest = importingFileName.endsWith('-entrypoint.ts') ||
importingFileName.endsWith('-meta.ts') || importingFileName.endsWith('.test.ts');
if (!importingFileIsEntrypointOrTest) {
context.report({
node,
messageId: 'incorrectSameNamespaceImportStar',
data: {
importPathForErrorMessage,
},
});
}
}
if (invalidCrossFolderUsage) {
context.report({
node,
messageId: 'crossNamespaceImport',
data: {importPathForErrorMessage},
});
}
}
export default createRule<[], MessageIds>({
name: 'es-modules-import',
meta: {
type: 'problem',
messages: {
// Define messages corresponding to MessageIds
doubleSlashInImportPath: 'Double slash in import path: "{{importPathForErrorMessage}}"',
missingExtension: 'Missing file extension for import "{{importPathForErrorMessage}}"',
invalidRelativeUrl: 'Invalid relative URL import. An import should start with either "../" or "./".',
incorrectSameNamespaceImportNamed:
'Incorrect same-namespace import: "{{importPathForErrorMessage}}". Use "import * as File from \'./File.js\';" instead.',
incorrectSameNamespaceImportStar:
'Incorrect same-namespace import: "{{importPathForErrorMessage}}". Use "import { Symbol } from \'./relative-file.js\';" instead.',
crossNamespaceImport: CROSS_NAMESPACE_MESSAGE,
crossNamespaceImportLs:
CROSS_NAMESPACE_MESSAGE + ' You may only import common/ls.js directly from TypeScript source files.',
crossNamespaceImportThirdParty: CROSS_NAMESPACE_MESSAGE +
' If the third_party dependency does not expose a single entrypoint, update es_modules_import.js to make it exempt.',
incorrectSameNamespaceTestImport:
'Incorrect same-namespace import: "{{importPathForErrorMessage}}". Use "import * as {{namespaceNameForErrorMessage}} from \'./{{namespaceFilenameForErrorMessage}}.js\';" instead.',
},
docs: {
description: 'check ES import usage',
category: 'Possible Errors',
},
fixable: 'code',
schema: [], // no options
},
defaultOptions: [], // Add defaultOptions
create: function(context: RuleContext) {
const filename = context.filename;
// Ensure filename is resolved to an absolute path if it's not already
const importingFileName = path.resolve(filename);
return {
ExportNamedDeclaration(node) {
// Any export in a file is called an `ExportNamedDeclaration`, but
// only directly-exporting-from-import declarations have the
// `node.source` set.
if (!node.source) {
return;
}
const value = node.source.value;
const importPath = path.normalize(value);
const importPathForErrorMessage = value.replace(/\\/g, '/');
checkImportExtension(
importPath,
importPathForErrorMessage,
context,
node,
);
},
ImportDeclaration(node) {
const value = node.source.value;
if (value.includes('//')) {
context.report({
node,
messageId: 'doubleSlashInImportPath',
data: {
importPathForErrorMessage: value,
},
fix(fixer: RuleFixer) {
const fixedValue = value.replaceAll('//', '/');
// Replace the original import string with the fixed one. We need
// the extra quotes around the value to ensure we produce valid
// JS - else it would end up as `import X from ../some/path.js`
return fixer.replaceText(node.source, `'${fixedValue}'`);
},
});
}
const importPath = path.normalize(value);
const importPathForErrorMessage = value.replace(/\\/g, '/');
checkImportExtension(
value,
importPathForErrorMessage,
context,
node,
);
// Type imports are unrestricted
if (node.importKind === 'type') {
// `import type ... from ...` syntax
return;
}
// Check specifiers for `import {type T} from ...`
if (node.specifiers.every(spec => spec.type === 'ImportSpecifier' && spec.importKind === 'type')) {
return;
}
// Accidental relative URL:
// e.g.: import * as Root from 'front_end/root/root.js';
//
// Should ignore named imports import * as fs from 'fs';
//
// Don't use `importPath` here, as `path.normalize` removes
// the `./` from same-folder import paths. Use original `value`.
if (!value.startsWith('.') && !/^[\w\-_]+$/.test(value)) {
context.report({
node,
messageId: 'invalidRelativeUrl', // Use messageId
});
}
// the Module import rules do not apply within:
// 1. inspector_overlay
// 2. front_end/third_party
if (importingFileName.startsWith(INSPECTOR_OVERLAY_DIRECTORY) ||
importingFileName.startsWith(THIRD_PARTY_DIRECTORY)) {
return;
}
if (isSideEffectImportSpecifier(node.specifiers)) {
return;
}
if (importPathForErrorMessage.endsWith('platform/platform.js') &&
nodeSpecifiersSpecialImportsOnly(node.specifiers)) {
/* We allow direct importing of the ls and assertNotNull utility as it's so frequently used. */
return;
}
const exportingFileName = path.resolve(
path.dirname(importingFileName),
importPath,
);
if (isStarAsImportSpecifier(node.specifiers)) {
// Pass absolute paths to checkStarImport
checkStarImport(
context,
node,
importPath,
importPathForErrorMessage,
importingFileName,
exportingFileName,
);
} else if (computeTopLevelFolder(importingFileName) !== computeTopLevelFolder(exportingFileName)) {
// Check if exportingFileName is actually under FRONT_END_DIRECTORY before comparing top level folders
if (!path.relative(FRONT_END_DIRECTORY, exportingFileName).startsWith('..')) {
if (importingFileName.endsWith('.test.ts') &&
importPath.includes([path.sep, 'testing', path.sep].join(''))) {
/** Within test files we allow the direct import of test helpers.*/
return;
}
// We explicitly allow destructuring imports from 'lit/lit.js'.
if (importPath.endsWith(path.join('lit', 'lit.js'))) {
return;
}
let messageId: MessageIds = 'crossNamespaceImport';
if (value.endsWith('common/ls.js')) {
messageId = 'crossNamespaceImportLs';
} else if (importPath.includes('third_party')) {
messageId = 'crossNamespaceImportThirdParty';
}
context.report({
node,
messageId,
data: {
importPathForErrorMessage,
},
});
}
} else if (isModuleEntrypoint(importingFileName)) {
if (importingFileName.includes(
['testing', 'test_setup.ts'].join(path.sep),
) &&
importPath.includes([path.sep, 'testing', path.sep].join(''))) {
/** Within test files we allow the direct import of test helpers.
* The entry point detection detects test_setup.ts as an
* entrypoint, but we don't treat it as such, it's just a file
* that Karma runs to setup the environment.
*/
return;
}
if (importPath.endsWith('.css.js')) {
// We allow files to import CSS files within the same module.
return;
}
context.report({
node,
messageId: 'incorrectSameNamespaceImportNamed', // Use messageId
data: {
importPathForErrorMessage,
},
});
} else if (path.dirname(importingFileName) === path.dirname(exportingFileName)) {
if (!importingFileName.endsWith('.test.ts') || !importingFileName.startsWith(FRONT_END_DIRECTORY)) {
return;
}
const importingDirectoryName = path.basename(
path.dirname(importingFileName),
);
if (importingDirectoryName === 'testing') {
// Special case of Foo.test.ts for a helper Foo.ts.
return;
}
// Unit tests must import from the entry points even for same-namespace
// imports, as we otherwise break the module system (in Release builds).
if (!isModuleEntrypoint(exportingFileName)) {
const namespaceNameForErrorMessage =
importingDirectoryName.substring(0, 1).toUpperCase() + importingDirectoryName.substring(1);
const namespaceFilenameForErrorMessage = importingDirectoryName;
context.report({
node,
messageId: 'incorrectSameNamespaceTestImport',
data: {
importPathForErrorMessage,
namespaceNameForErrorMessage,
namespaceFilenameForErrorMessage,
},
});
}
}
},
};
},
});