Skip to content

Commit b05b481

Browse files
committed
migrate no-standalone-editor rule
1 parent 2ea9132 commit b05b481

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"@typescript-eslint/class-name-casing": "warn",
3131
"code-translation-remind": "warn",
3232
"code-no-nls-in-standalone-editor": "warn",
33+
"code-no-standalone-editor": "warn",
3334
"code-layering": [
3435
"warn",
3536
{
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"use strict";
2+
/*---------------------------------------------------------------------------------------------
3+
* Copyright (c) Microsoft Corporation. All rights reserved.
4+
* Licensed under the MIT License. See License.txt in the project root for license information.
5+
*--------------------------------------------------------------------------------------------*/
6+
const path_1 = require("path");
7+
module.exports = new class NoNlsInStandaloneEditorRule {
8+
constructor() {
9+
this.meta = {
10+
type: 'problem',
11+
schema: {},
12+
messages: {
13+
badImport: 'Not allowed to import standalone editor modules. See https://github.com/Microsoft/vscode/wiki/Code-Organization'
14+
}
15+
};
16+
}
17+
create(context) {
18+
if (/vs(\/|\\)editor/.test(context.getFilename())) {
19+
// the vs/editor folder is allowed to use the standalone editor
20+
return {};
21+
}
22+
return {
23+
ImportDeclaration: (node) => {
24+
this._checkImport(context, node, node.source.value);
25+
},
26+
CallExpression: (node) => {
27+
var _a;
28+
const { callee, arguments: args } = node;
29+
if (callee.type === 'Import' && ((_a = args[0]) === null || _a === void 0 ? void 0 : _a.type) === 'Literal') {
30+
this._checkImport(context, node, args[0].value);
31+
}
32+
}
33+
};
34+
}
35+
_checkImport(context, node, path) {
36+
if (typeof path !== 'string') {
37+
return;
38+
}
39+
// resolve relative paths
40+
if (path[0] === '.') {
41+
path = path_1.join(context.getFilename(), path);
42+
}
43+
if (/vs(\/|\\)editor(\/|\\)standalone(\/|\\)/.test(path)
44+
|| /vs(\/|\\)editor(\/|\\)common(\/|\\)standalone(\/|\\)/.test(path)
45+
|| /vs(\/|\\)editor(\/|\\)editor.api/.test(path)
46+
|| /vs(\/|\\)editor(\/|\\)editor.main/.test(path)
47+
|| /vs(\/|\\)editor(\/|\\)editor.worker/.test(path)) {
48+
context.report({
49+
node,
50+
messageId: 'badImport'
51+
});
52+
}
53+
}
54+
};
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import * as eslint from 'eslint';
7+
import * as estree from 'estree';
8+
import { join } from 'path';
9+
10+
export = new class NoNlsInStandaloneEditorRule implements eslint.Rule.RuleModule {
11+
12+
readonly meta = {
13+
type: 'problem',
14+
schema: {},
15+
messages: {
16+
badImport: 'Not allowed to import standalone editor modules. See https://github.com/Microsoft/vscode/wiki/Code-Organization'
17+
}
18+
};
19+
20+
create(context: eslint.Rule.RuleContext): eslint.Rule.RuleListener {
21+
22+
if (/vs(\/|\\)editor/.test(context.getFilename())) {
23+
// the vs/editor folder is allowed to use the standalone editor
24+
return {};
25+
}
26+
27+
return {
28+
ImportDeclaration: (node: estree.Node) => {
29+
this._checkImport(context, node, (<estree.ImportDeclaration>node).source.value);
30+
},
31+
CallExpression: (node: estree.Node) => {
32+
const { callee, arguments: args } = <estree.CallExpression>node;
33+
if ((<any>callee.type) === 'Import' && args[0]?.type === 'Literal') {
34+
this._checkImport(context, node, (<estree.Literal>args[0]).value);
35+
}
36+
}
37+
};
38+
}
39+
40+
private _checkImport(context: eslint.Rule.RuleContext, node: estree.Node, path: any) {
41+
if (typeof path !== 'string') {
42+
return;
43+
}
44+
45+
// resolve relative paths
46+
if (path[0] === '.') {
47+
path = join(context.getFilename(), path);
48+
}
49+
50+
if (
51+
/vs(\/|\\)editor(\/|\\)standalone(\/|\\)/.test(path)
52+
|| /vs(\/|\\)editor(\/|\\)common(\/|\\)standalone(\/|\\)/.test(path)
53+
|| /vs(\/|\\)editor(\/|\\)editor.api/.test(path)
54+
|| /vs(\/|\\)editor(\/|\\)editor.main/.test(path)
55+
|| /vs(\/|\\)editor(\/|\\)editor.worker/.test(path)
56+
) {
57+
context.report({
58+
node,
59+
messageId: 'badImport'
60+
});
61+
}
62+
}
63+
};
64+

0 commit comments

Comments
 (0)