lint: add rule for no posix in regular expressions
MODIFY
packages/@romejs/diagnostics/categories.ts
packages/@romejs/diagnostics/categories.ts
@@ -41,6 +41,7 @@ export type DiagnosticCategory =
| 'lint/noFunctionAssign'
| 'lint/noImportAssign'
| 'lint/noLabelVar'
+ | 'lint/noPosixInRegularExpression'
| 'lint/noShadowRestrictedNames'
| 'lint/noTemplateCurlyInString'
| 'lint/noUnsafeFinally'

MODIFY
packages/@romejs/js-compiler/transforms/lint/index.ts
packages/@romejs/js-compiler/transforms/lint/index.ts
@@ -34,6 +34,7 @@ import sparseArray from './sparseArray';
import undeclaredVariables from './undeclaredVariables';
import unsafeNegation from './unsafeNegation';
import unusedVariables from './unusedVariables';
+import noPosixCharacterClassInRegularExpression from './noPosixInRegularExpression';
// Add transforms in alphabetical order.
export const lintTransforms = [
@@ -65,4 +66,5 @@ export const lintTransforms = [
undeclaredVariables,
unsafeNegation,
unusedVariables,
+ noPosixCharacterClassInRegularExpression,
];

ADD
packages/@romejs/js-compiler/transforms/lint/noPosixInRegularExpression.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 '@romejs/test';
+import {testLint} from '../../api/lint.test';
+import {PartialDiagnostic} from '@romejs/diagnostics/types';
+
+test('no POSIX in regular expression', async t => {
+ function checkCategory(diagnostic: PartialDiagnostic): Boolean {
+ return diagnostic.category === 'lint/noPosixInRegularExpression';
+ }
+
+ const testCases = [
+ 'const pattern = /[[:alpha:]]/',
+ 'const pattern = /[[.ch.]]/',
+ ];
+
+ for (const testCase of testCases) {
+ const {diagnostics} = await testLint(testCase);
+ t.truthy(diagnostics.find(checkCategory));
+ }
+});

ADD
packages/@romejs/js-compiler/transforms/lint/noPosixInRegularExpression.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 {AnyNode, RegExpCharSet} from '@romejs/js-ast';
+import {Path, Context} from '@romejs/js-compiler';
+
+function checkRegEx(node: RegExpCharSet, context: Context): RegExpCharSet {
+ node.body.forEach((currNode, i) => {
+ const nextNode = node.body[i + 1];
+ const lastNode = node.body[node.body.length - 1];
+ if (
+ currNode.type === 'RegExpCharacter' &&
+ currNode.value === '[' &&
+ nextNode.type === 'RegExpCharacter' &&
+ (nextNode.value === ':' || nextNode.value === '.') &&
+ lastNode.type === 'RegExpCharacter' &&
+ lastNode.value === nextNode.value
+ ) {
+ context.addNodeDiagnostic(currNode, {
+ fixable: false,
+ category: 'lint/noPosixInRegularExpression',
+ message:
+ 'POSIX Character Classes and Collating Sequences are not supported in ECMAscript Regular Expressions',
+ });
+ }
+ });
+
+ return node;
+}
+
+export default {
+ name: 'noPosixInRegularExpression',
+ enter(path: Path): AnyNode {
+ const {context, node} = path;
+
+ if (node.type === 'RegExpCharSet') {
+ return checkRegEx(node, context);
+ }
+
+ return node;
+ },
+};

lint: add rule for no posix in regular expressions