add rule no danger props and tests
MODIFY
packages/@romejs/diagnostics/categories.ts
packages/@romejs/diagnostics/categories.ts
@@ -115,6 +115,7 @@ type LintDiagnosticCategory =
| 'lint/noCommaOperator'
| 'lint/noCompareNegZero'
| 'lint/noCondAssign'
+ | 'lint/noDanger'
| 'lint/noDangerWithChildren'
| 'lint/noDebugger'
| 'lint/noDelete'

MODIFY
packages/@romejs/diagnostics/descriptions.ts
packages/@romejs/diagnostics/descriptions.ts
@@ -292,6 +292,10 @@ export const descriptions = createMessages({
},
// @romejs/js-compiler
LINT: {
+ NO_DANGER: {
+ category: 'lint/noDanger',
+ message: 'dangerouslySetInnerHTML should be avoided',
+ },
IMPORT_DEFAULT_BASENAME: (prev: string, basename: string) => ({
category: 'lint/importDefaultBasename',
message: markup`When importing the default, use the basename <emphasis>${basename}</emphasis>`,

MODIFY
packages/@romejs/js-compiler/lint/rules/index.ts
packages/@romejs/js-compiler/lint/rules/index.ts
@@ -29,6 +29,7 @@ import noChildrenProp from './react/noChildrenProp';
import noCommaOperator from './regular/noCommaOperator';
import noCompareNegZero from './regular/noCompareNegZero';
import noCondAssign from './regular/noCondAssign';
+import noDanger from './react/noDanger';
import noDangerWithChildren from './react/noDangerWithChildren';
import noDebugger from './regular/noDebugger';
import noDelete from './regular/noDelete';
@@ -92,6 +93,7 @@ export const lintTransforms = [
noCommaOperator,
noCompareNegZero,
noCondAssign,
+ noDanger,
noDangerWithChildren,
noDebugger,
noDelete,

ADD
packages/@romejs/js-compiler/lint/rules/react/noDanger.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 {testLintMultiple} from '../testHelpers';
+
+test(
+ 'no danger',
+ async (t) => {
+ await testLintMultiple(
+ t,
+ [
+ // INVALID
+ '<div dangerouslySetInnerHTML={{ __html: "Hello World" }}></div>;',
+ // VALID
+ '<div>Hello World</div>;',
+ ],
+ {category: 'lint/noDanger'},
+ );
+ },
+);

ADD
packages/@romejs/js-compiler/lint/rules/react/noDanger.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 {descriptions} from '@romejs/diagnostics';
+
+function jsxDanger(node: AnyNode) {
+ if (node.type !== 'JSXElement') {
+ return false;
+ }
+ const hasDangerAttribute = !!node.attributes.find((attribute) =>
+ attribute.type === 'JSXAttribute' &&
+ attribute.name.name === 'dangerouslySetInnerHTML'
+ )
+ return hasDangerAttribute
+}
+
+export default {
+ name: 'noDanger',
+ enter(path: Path): AnyNode {
+ const {node} = path;
+
+ if (jsxDanger(node)) {
+ path.context.addNodeDiagnostic(
+ node,
+ descriptions.LINT.NO_DANGER,
+ );
+ }
+
+ return node;
+ },
+};

add rule no danger props and tests