add noDeleteVars lint rule
Includes test for `noDeleteVars`, assumes the parser mode is `modern`
MODIFY
packages/@romejs/js-compiler/__rtests__/lint.ts
packages/@romejs/js-compiler/__rtests__/lint.ts
@@ -10,6 +10,7 @@ import lint from '../api/lint';
import {parseJS} from '@romejs/js-parser';
import {createUnknownFilePath} from '@romejs/path';
import {DEFAULT_PROJECT_CONFIG, ProjectConfig} from '@romejs/project';
+import {ConstSourceType} from '@romejs/js-ast';
const LINT_ENABLED_FORMAT_DISABLED_CONFIG: ProjectConfig = {
...DEFAULT_PROJECT_CONFIG,
@@ -31,12 +32,16 @@ const LINT_AND_FORMAT_ENABLED_CONFIG: ProjectConfig = {
},
};
-async function testLint(input: string, config: ProjectConfig) {
+async function testLint(
+ input: string,
+ config: ProjectConfig,
+ sourceType: ConstSourceType = 'module',
+) {
return await lint({
options: {},
ast: parseJS({
input,
- sourceType: 'module',
+ sourceType,
path: createUnknownFilePath('unknown'),
}),
sourceText: input,
@@ -251,3 +256,28 @@ test('disallow var', async t => {
},
]);
});
+
+test.only('no delete vars', async t => {
+ const res = await testLint(
+ `
+ const foo = "test";
+ delete foo;
+ `,
+ LINT_ENABLED_FORMAT_DISABLED_CONFIG,
+ 'script',
+ );
+
+ console.log(res.diagnostics);
+
+ t.looksLike(res.diagnostics[1], {
+ category: 'lint/noDeleteVars',
+ message: 'Variables should not be deleted.',
+ mtime: undefined,
+ filename: 'unknown',
+ start: {index: 29, line: 3, column: 4},
+ end: {index: 39, line: 3, column: 14},
+ language: 'js',
+ sourceType: 'module',
+ origins: [{category: 'lint'}],
+ });
+});

MODIFY
packages/@romejs/js-compiler/transforms/lint/index.ts
packages/@romejs/js-compiler/transforms/lint/index.ts
@@ -15,6 +15,7 @@ import noAsyncPromiseExecutor from './noAsyncPromiseExecutor';
import noLabelVar from './noLabelVar';
import noDuplicateKeys from './noDuplicateKeys';
import disallowVar from './disallowVar';
+import noDeleteVars from './noDeleteVars';
export const lintTransforms = [
undeclaredVariables,
@@ -27,4 +28,5 @@ export const lintTransforms = [
noLabelVar,
noDuplicateKeys,
disallowVar,
+ noDeleteVars,
];

ADD
packages/@romejs/js-compiler/transforms/lint/noDeleteVars.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';
+
+export default {
+ name: 'noDeleteVars',
+ enter(path: Path): AnyNode {
+ const {node} = path;
+
+ if (
+ node.type === 'UnaryExpression' &&
+ node.operator === 'delete' &&
+ node.argument.type === 'ReferenceIdentifier'
+ ) {
+ path.context.addNodeDiagnostic(node, {
+ category: 'lint/noDeleteVars',
+ message: 'Variables should not be deleted.',
+ });
+ }
+
+ return node;
+ },
+};

add noDeleteVars lint rule

- Includes test for `noDeleteVars`, assumes the parser mode is `modern`