-
Notifications
You must be signed in to change notification settings - Fork 0
/
es_modules_import.js
233 lines (198 loc) · 8.31 KB
/
es_modules_import.js
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
// 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
*/
'use strict';
const path = require('path');
const FRONT_END_DIRECTORY = path.join(__dirname, '..', '..', '..', 'front_end');
const INSPECTOR_OVERLAY_DIRECTORY = path.join(__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 isStarAsImportSpecifier(specifiers) {
return specifiers.length === 1 && specifiers[0].type === 'ImportNamespaceSpecifier';
}
function isSideEffectImportSpecifier(specifiers) {
return specifiers.length === 0;
}
function isModuleEntrypoint(fileName) {
const fileNameWithoutExtension = path.basename(fileName).replace(path.extname(fileName), '');
const directoryName = path.basename(path.dirname(fileName));
// TODO(crbug.com/1011811): remove -legacy fallback
return directoryName === fileNameWithoutExtension || `${directoryName}-legacy` === fileNameWithoutExtension;
}
function computeTopLevelFolder(fileName) {
const namespaceName = path.relative(FRONT_END_DIRECTORY, fileName);
return namespaceName.substring(0, namespaceName.indexOf(path.sep));
}
function checkImportExtension(importPath, importPathForErrorMessage, context, node) {
// import * as fs from 'fs';
if (!importPath.startsWith('.')) {
return;
}
if (!importPath.endsWith('.js') && !importPath.endsWith('.mjs')) {
context.report({
node,
message: 'Missing file extension for import "{{importPathForErrorMessage}}"',
data: {
importPathForErrorMessage,
},
fix(fixer) {
return fixer.replaceText(node.source, `'${importPathForErrorMessage}.js'`);
}
});
}
}
function nodeSpecifiersSpecialImportsOnly(specifiers) {
return specifiers.length === 1 && specifiers[0].type === 'ImportSpecifier' &&
['ls', 'assertNotNullOrUndefined'].includes(specifiers[0].imported.name);
}
function checkStarImport(context, node, importPath, importPathForErrorMessage, importingFileName, exportingFileName) {
if (isModuleEntrypoint(importingFileName)) {
return;
}
if (importingFileName.startsWith(COMPONENT_DOCS_DIRECTORY) &&
importPath.includes(path.join('front_end', 'helpers'))) {
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.
const importingFileIsEntrypoint =
importingFileName.endsWith('-entrypoint.ts') || importingFileName.endsWith('-meta.ts');
if (!importingFileIsEntrypoint) {
context.report({
node,
message:
'Incorrect same-namespace import: "{{importPathForErrorMessage}}". Use "import { Symbol } from \'./relative-file.js\';" instead.',
data: {
importPathForErrorMessage,
},
});
}
}
if (invalidCrossFolderUsage) {
context.report({
node,
message: CROSS_NAMESPACE_MESSAGE,
data: {importPathForErrorMessage},
});
}
}
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'check ES import usage',
category: 'Possible Errors',
},
fixable: 'code',
schema: [] // no options
},
create: function(context) {
const importingFileName = path.resolve(context.getFilename());
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 importPath = path.normalize(node.source.value);
const importPathForErrorMessage = node.source.value.replace(/\\/g, '/');
checkImportExtension(importPath, importPathForErrorMessage, context, node);
},
ImportDeclaration(node) {
const importPath = path.normalize(node.source.value);
const importPathForErrorMessage = node.source.value.replace(/\\/g, '/');
checkImportExtension(node.source.value, importPathForErrorMessage, context, node);
// Accidental relative URL:
// 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.
if (!node.source.value.startsWith('.') && !/^[\w\-_]+$/.test(node.source.value)) {
context.report({
node,
message: 'Invalid relative URL import. An import should start with either "../" or "./".',
});
}
if (importingFileName.startsWith(INSPECTOR_OVERLAY_DIRECTORY)) {
return;
}
if (isSideEffectImportSpecifier(node.specifiers)) {
return;
}
const exportingFileName = path.resolve(path.dirname(importingFileName), importPath);
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;
}
if (isStarAsImportSpecifier(node.specifiers)) {
checkStarImport(context, node, importPath, importPathForErrorMessage, importingFileName, exportingFileName);
} else {
if (computeTopLevelFolder(importingFileName) !== computeTopLevelFolder(exportingFileName)) {
let message = CROSS_NAMESPACE_MESSAGE;
if (importPath.endsWith(path.join('common', 'ls.js'))) {
message += ' You may only import common/ls.js directly from TypeScript source files.';
}
if (importPath.includes('third_party')) {
message +=
' If the third_party dependency does not expose a single entrypoint, update es_modules_import.js to make it exempt.';
}
context.report({
node,
message,
data: {
importPathForErrorMessage,
},
});
} else if (isModuleEntrypoint(importingFileName)) {
if (importingFileName.includes(['test_setup', 'test_setup.ts'].join(path.sep)) &&
importPath.includes([path.sep, 'helpers', 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,
message:
'Incorrect same-namespace import: "{{importPathForErrorMessage}}". Use "import * as File from \'./File.js\';" instead.',
data: {
importPathForErrorMessage,
}
});
}
}
}
};
}
};