feat(lint): add restricted-globals rule
disallow the use of some js global variables such as events and errors,
as this is considered as a bad practice.
MODIFY
packages/@romejs/diagnostics/categories.ts
packages/@romejs/diagnostics/categories.ts
@@ -56,6 +56,7 @@ export type DiagnosticCategory =
| 'lint/pendingFixes'
| 'lint/preferFunctionDeclarations'
| 'lint/preferTemplate'
+ | 'lint/restrictedGlobals'
| 'lint/singleVarDeclarator'
| 'lint/sparseArray'
| 'lint/undeclaredVariables'

MODIFY
packages/@romejs/diagnostics/descriptions.ts
packages/@romejs/diagnostics/descriptions.ts
@@ -512,6 +512,10 @@ export const descriptions = createMessages(
],
};
},
+ RESTRICTED_GLOBALS: (globalName) => ({
+ category: 'lint/restrictedGlobals',
+ message: markup`The use of the existing global variable <emphasis>${globalName}</emphasis> is not allowed. Use local variable instead.`,
+ }),
},
PROJECT_MANAGER: {

MODIFY
packages/@romejs/js-compiler/transforms/lint/index.ts
packages/@romejs/js-compiler/transforms/lint/index.ts
@@ -40,6 +40,7 @@ import noCatchAssign from './noCatchAssign';
import noVar from './noVar';
import preferFunctionDeclarations from './preferFunctionDeclarations';
import preferTemplate from './preferTemplate';
+import restrictedGlobals from './restrictedGlobals';
import singleVarDeclarator from './singleVarDeclarator';
import sparseArray from './sparseArray';
import undeclaredVariables from './undeclaredVariables';
@@ -82,6 +83,7 @@ export const lintTransforms = [
noVar,
preferFunctionDeclarations,
preferTemplate,
+ restrictedGlobals,
singleVarDeclarator,
sparseArray,
undeclaredVariables,

ADD
packages/@romejs/js-compiler/transforms/lint/restrictedGlobals.test.ts
+/**
+ * Copyright (c) Facebook, Inc. and its affiliates.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE file in the root directory of this source tree.
+ */
+
+import {test} from 'rome';
+import {testLint} from '../../api/lint.test';
+
+test(
+ 'restricted globals',
+ async (t) => {
+ await testLint(
+ t,
+ 'console.log(event);',
+ {category: 'lint/restrictedGlobals'},
+ );
+
+ await testLint(t, `
+ // valid use of event into the function scope.
+ function foo(event) {
+ console.info(event);
+ }
+
+ // invalid, event is used as a global.
+ foo(event)
+ `, {category: 'lint/restrictedGlobals'});
+ },
+);

ADD
packages/@romejs/js-compiler/transforms/lint/restrictedGlobals.ts
+/**
+ * Copyright (c) Facebook, Inc. and its affiliates.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE file in the root directory of this source tree.
+ */
+
+import {Path} from '@romejs/js-compiler';
+import {AnyNode} from '@romejs/js-ast';
+import {isInTypeAnnotation} from '@romejs/js-ast-utils';
+import {descriptions} from '@romejs/diagnostics';
+
+const RESTRICTED_GLOBALS = ['event', 'error'];
+
+export default {
+ name: 'restrictedGlobal',
+ enter(path: Path): AnyNode {
+ const {node, scope} = path;
+
+ if ((node.type === 'ReferenceIdentifier' || node.type ===
+ 'JSXReferenceIdentifier') && !isInTypeAnnotation(path)) {
+ const {name} = node;
+ const binding = scope.getBinding(name);
+
+ const isDefined = binding !== undefined;
+ const isAGlobal = scope.getRootScope().isGlobal(name);
+
+ if (!isDefined && isAGlobal && RESTRICTED_GLOBALS.includes(name)) {
+ path.context.addNodeDiagnostic(
+ node,
+ descriptions.LINT.RESTRICTED_GLOBALS(name),
+ );
+ }
+ }
+
+ return node;
+ },
+};

feat(lint): add restricted-globals rule

disallow the use of some js global variables such as events and errors,
as this is considered as a bad practice.