mirror of
https://github.com/microsoft/vscode.git
synced 2025-12-20 02:08:47 +00:00
add lint rule for AMD node imports
This commit is contained in:
60
.eslintplugin/code-amd-node-module.ts
Normal file
60
.eslintplugin/code-amd-node-module.ts
Normal file
@@ -0,0 +1,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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -194,6 +194,14 @@
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"files": [
|
||||||
|
"src/**/{common,browser}/**/*.ts"
|
||||||
|
],
|
||||||
|
"rules": {
|
||||||
|
"local/code-amd-node-module": "warn"
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"files": [
|
"files": [
|
||||||
"src/**/*.ts"
|
"src/**/*.ts"
|
||||||
|
|||||||
Reference in New Issue
Block a user