Skip to content

Commit c103d4b

Browse files
committed
Add support for transformation opt-out
1 parent b160be3 commit c103d4b

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
- Added
44
- Add support for refs
55
- Add support for state types
6+
- Add support for opt-out in one of:
7+
- `@abstract` JSDoc comment
8+
- `abstract` modifier
9+
- `react-declassify-disable` comment
610
- Fixed
711
- Keep generator/async flags
812
- Fix renaming failure in some cases

src/analysis/head.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ export type ComponentHead = {
1010
};
1111

1212
export function analyzeHead(path: NodePath<ClassDeclaration>): ComponentHead | undefined {
13+
if (path.node.leadingComments?.some((comment) => /react-declassify-disable/.test(comment.value))) {
14+
// Explicitly disabled
15+
return;
16+
}
17+
if (
18+
path.node.leadingComments?.some((comment) =>
19+
comment.type === "CommentBlock" &&
20+
/^\*/.test(comment.value) &&
21+
/@abstract/.test(comment.value)
22+
)
23+
|| path.node.abstract
24+
) {
25+
// This is an abstract class to be inherited; do not attempt transformation.
26+
return;
27+
}
1328
const superClass = path.get("superClass");
1429
if (!superClass.isExpression()) {
1530
return;

src/index.test.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ describe("react-declassify", () => {
101101
class C extends React.Component {}
102102
`;
103103
const output = dedent`\
104-
/* react-declassify:disabled Cannot perform transformation: Missing render method */
104+
/* react-declassify-disable Cannot perform transformation: Missing render method */
105105
class C extends React.Component {}
106106
`;
107107
expect(transform(input)).toBe(output);
@@ -114,7 +114,7 @@ describe("react-declassify", () => {
114114
}
115115
`;
116116
const output = dedent`\
117-
/* react-declassify:disabled Cannot perform transformation: Missing render method */
117+
/* react-declassify-disable Cannot perform transformation: Missing render method */
118118
class C extends React.Component {
119119
rende() {}
120120
}
@@ -295,6 +295,37 @@ describe("react-declassify", () => {
295295
`;
296296
expect(transform(input)).toBe(input);
297297
});
298+
299+
describe("opt-out", () => {
300+
it("ignores if marked as react-declassify-disable", () => {
301+
const input = dedent`\
302+
/* react-declassify-disable */
303+
class C extends React.Component {
304+
render() {}
305+
}
306+
`;
307+
expect(transform(input)).toBe(input);
308+
});
309+
310+
it("ignores if marked as abstract", () => {
311+
const input = dedent`\
312+
abstract class C extends React.Component {
313+
render() {}
314+
}
315+
`;
316+
expect(transform(input, { ts: true })).toBe(input);
317+
});
318+
319+
it("ignores if marked as @abstract", () => {
320+
const input = dedent`\
321+
/** @abstract */
322+
class C extends React.Component {
323+
render() {}
324+
}
325+
`;
326+
expect(transform(input)).toBe(input);
327+
});
328+
});
298329
});
299330

300331
describe("Class forms", () => {

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export default function plugin(babel: typeof import("@babel/core")): PluginObj<P
4545
if (!(e instanceof AnalysisError)) {
4646
throw e;
4747
}
48-
t.addComment(declPath.node, "leading", ` react-declassify:disabled Cannot perform transformation: ${e.message} `);
48+
t.addComment(declPath.node, "leading", ` react-declassify-disable Cannot perform transformation: ${e.message} `);
4949
refreshComments(declPath.node);
5050
}
5151
} else {
@@ -67,7 +67,7 @@ export default function plugin(babel: typeof import("@babel/core")): PluginObj<P
6767
if (!(e instanceof AnalysisError)) {
6868
throw e;
6969
}
70-
t.addComment(path.node, "leading", ` react-declassify:disabled Cannot perform transformation: ${e.message} `);
70+
t.addComment(path.node, "leading", ` react-declassify-disable Cannot perform transformation: ${e.message} `);
7171
refreshComments(path.node);
7272
}
7373
}

0 commit comments

Comments
 (0)