forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode-amd-node-module.ts
60 lines (45 loc) · 1.46 KB
/
code-amd-node-module.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as eslint from 'eslint';
import { join } from 'path';
export = new class ApiProviderNaming implements eslint.Rule.RuleModule {
readonly meta: eslint.Rule.RuleMetaData = {
messages: {
amdX: 'Use `import type` for import declarations, use `amdX#importAMDNodeModule` for import expressions'
}
};
create(context: eslint.Rule.RuleContext): eslint.Rule.RuleListener {
const modules = new Set<string>();
try {
const { dependencies, optionalDependencies } = require(join(__dirname, '../package.json'));
const all = Object.keys(dependencies).concat(Object.keys(optionalDependencies));
for (const key of all) {
modules.add(key);
}
} catch (e) {
console.error(e);
throw e;
}
const checkImport = (node: any) => {
if (node.type !== 'Literal' || typeof node.value !== 'string') {
return;
}
if (node.parent.importKind === 'type') {
return;
}
if (!modules.has(node.value)) {
return;
}
context.report({
node,
messageId: 'amdX'
});
}
return {
['ImportExpression Literal']: checkImport,
['ImportDeclaration Literal']: checkImport
};
}
};