|
| 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