Skip to content

Commit 2ea9132

Browse files
committed
migrate no-nls-in-standalone-editor rule
1 parent a3bd604 commit 2ea9132

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"@typescript-eslint/semi": "warn",
3030
"@typescript-eslint/class-name-casing": "warn",
3131
"code-translation-remind": "warn",
32+
"code-no-nls-in-standalone-editor": "warn",
3233
"code-layering": [
3334
"warn",
3435
{
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+
noNls: 'Not allowed to import vs/nls in standalone editor modules. Use standaloneStrings.ts'
14+
}
15+
};
16+
}
17+
create(context) {
18+
const fileName = context.getFilename();
19+
if (!(/vs(\/|\\)editor(\/|\\)standalone(\/|\\)/.test(fileName)
20+
|| /vs(\/|\\)editor(\/|\\)common(\/|\\)standalone(\/|\\)/.test(fileName)
21+
|| /vs(\/|\\)editor(\/|\\)editor.api/.test(fileName)
22+
|| /vs(\/|\\)editor(\/|\\)editor.main/.test(fileName)
23+
|| /vs(\/|\\)editor(\/|\\)editor.worker/.test(fileName))) {
24+
return {};
25+
}
26+
return {
27+
ImportDeclaration: (node) => {
28+
this._checkImport(context, node, node.source.value);
29+
},
30+
CallExpression: (node) => {
31+
var _a;
32+
const { callee, arguments: args } = node;
33+
if (callee.type === 'Import' && ((_a = args[0]) === null || _a === void 0 ? void 0 : _a.type) === 'Literal') {
34+
this._checkImport(context, node, args[0].value);
35+
}
36+
}
37+
};
38+
}
39+
_checkImport(context, node, path) {
40+
if (typeof path !== 'string') {
41+
return;
42+
}
43+
// resolve relative paths
44+
if (path[0] === '.') {
45+
path = path_1.join(context.getFilename(), path);
46+
}
47+
if (/vs(\/|\\)nls/.test(path)) {
48+
context.report({
49+
node,
50+
messageId: 'noNls'
51+
});
52+
}
53+
}
54+
};
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
noNls: 'Not allowed to import vs/nls in standalone editor modules. Use standaloneStrings.ts'
17+
}
18+
};
19+
20+
create(context: eslint.Rule.RuleContext): eslint.Rule.RuleListener {
21+
22+
const fileName = context.getFilename();
23+
if (!(
24+
/vs(\/|\\)editor(\/|\\)standalone(\/|\\)/.test(fileName)
25+
|| /vs(\/|\\)editor(\/|\\)common(\/|\\)standalone(\/|\\)/.test(fileName)
26+
|| /vs(\/|\\)editor(\/|\\)editor.api/.test(fileName)
27+
|| /vs(\/|\\)editor(\/|\\)editor.main/.test(fileName)
28+
|| /vs(\/|\\)editor(\/|\\)editor.worker/.test(fileName)
29+
)) {
30+
return {};
31+
}
32+
33+
return {
34+
ImportDeclaration: (node: estree.Node) => {
35+
this._checkImport(context, node, (<estree.ImportDeclaration>node).source.value);
36+
},
37+
CallExpression: (node: estree.Node) => {
38+
const { callee, arguments: args } = <estree.CallExpression>node;
39+
if ((<any>callee.type) === 'Import' && args[0]?.type === 'Literal') {
40+
this._checkImport(context, node, (<estree.Literal>args[0]).value);
41+
}
42+
}
43+
};
44+
}
45+
46+
private _checkImport(context: eslint.Rule.RuleContext, node: estree.Node, path: any) {
47+
if (typeof path !== 'string') {
48+
return;
49+
}
50+
51+
// resolve relative paths
52+
if (path[0] === '.') {
53+
path = join(context.getFilename(), path);
54+
}
55+
56+
if (
57+
/vs(\/|\\)nls/.test(path)
58+
) {
59+
context.report({
60+
node,
61+
messageId: 'noNls'
62+
});
63+
}
64+
65+
}
66+
};
67+

0 commit comments

Comments
 (0)