Skip to content

Commit

Permalink
fix: million ignore (#976)
Browse files Browse the repository at this point in the history
* init

* update
  • Loading branch information
Aslemammad authored Feb 24, 2024
1 parent f5379d4 commit d1e018b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
10 changes: 10 additions & 0 deletions packages/compiler/auto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { isUseClient } from './utils/is-use-client';
import { registerImportDefinition } from './utils/register-import-definition';
import { unwrapNode } from './utils/unwrap-node';
import { unwrapPath } from './utils/unwrap-path';
import { shouldBeIgnored } from './utils/ast';

interface JSXStateContext {
bailout: boolean;
Expand Down Expand Up @@ -223,6 +224,9 @@ function transformFunctionDeclaration(
path: babel.NodePath<t.FunctionDeclaration>,
): void {
if (isStatementTopLevel(path)) {
if (shouldBeIgnored(path)) {
return
}
const decl = path.node;
// Check if declaration is FunctionDeclaration
if (
Expand Down Expand Up @@ -298,6 +302,9 @@ function transformVariableDeclarator(
) {
return;
}
if (shouldBeIgnored(path)) {
return
}
const identifier = path.node.id;
if (!t.isIdentifier(identifier)) {
return;
Expand Down Expand Up @@ -336,6 +343,9 @@ function transformCallExpression(
ctx: StateContext,
path: babel.NodePath<t.CallExpression>,
): void {
if (shouldBeIgnored(path)) {
return
}
const definition = getValidImportDefinition(ctx, path.get('callee'));
if (definition === REACT_IMPORTS.memo[ctx.serverMode]) {
const args = path.get('arguments');
Expand Down
20 changes: 7 additions & 13 deletions packages/compiler/block.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import * as t from '@babel/types';
import {
HIDDEN_IMPORTS,
JSX_SKIP_ANNOTATION,
SKIP_ANNOTATION,
SVG_ELEMENTS,
TRACKED_IMPORTS,
} from './constants';
import type { StateContext } from './types';
import { findComment } from './utils/ast';
import { HIDDEN_IMPORTS, JSX_SKIP_ANNOTATION, SKIP_ANNOTATION, SVG_ELEMENTS, TRACKED_IMPORTS } from './constants';
import type { StateContext } from "./types";
import { findComment, shouldBeIgnored } from './utils/ast';
import { isComponent, isComponentishName, isPathValid } from './utils/checks';
import { generateUniqueName } from './utils/generate-unique-name';
import { getDescriptiveName } from './utils/get-descriptive-name';
Expand Down Expand Up @@ -385,10 +379,10 @@ function transformJSX(
);
}

export function transformBlock(
ctx: StateContext,
path: babel.NodePath<t.CallExpression>,
): void {
export function transformBlock(ctx: StateContext, path: babel.NodePath<t.CallExpression>): void {
if (shouldBeIgnored(path)) {
return;
}
const definition = getValidImportDefinition(ctx, path.get('callee'));
// Check first if the call is a valid `block` call
if (TRACKED_IMPORTS.block[ctx.serverMode] !== definition) {
Expand Down
1 change: 1 addition & 0 deletions packages/compiler/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ImportDefinition } from './types';

export const RENDER_SCOPE = 'slot';
export const SKIP_ANNOTATION = '@million skip';
export const IGNORE_ANNOTATION = 'million-ignore';
export const JSX_SKIP_ANNOTATION = '@million jsx-skip';
export const SVG_ELEMENTS = [
'circle',
Expand Down
15 changes: 15 additions & 0 deletions packages/compiler/utils/ast.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { NodePath } from '@babel/core';
import type * as t from '@babel/types';
import { IGNORE_ANNOTATION } from '../constants';

export const findComment = (node: t.Node, comment: string) => {

Check warning on line 5 in packages/compiler/utils/ast.ts

View workflow job for this annotation

GitHub Actions / build (19.x)

Missing return type on function
const comments = node.leadingComments;
Expand All @@ -9,3 +11,16 @@ export const findComment = (node: t.Node, comment: string) => {
}
}
};

export const shouldBeIgnored = (path: NodePath<t.Node>) => {

Check failure on line 15 in packages/compiler/utils/ast.ts

View workflow job for this annotation

GitHub Actions / build (19.x)

This is the default value for this type parameter, so it can be omitted

Check warning on line 15 in packages/compiler/utils/ast.ts

View workflow job for this annotation

GitHub Actions / build (19.x)

Missing return type on function
for (const comment of path.node.leadingComments ?? []) {
if (comment.value.includes(IGNORE_ANNOTATION)) {
return true
}
}
if (path.parentPath) {
return shouldBeIgnored(path.parentPath)
}
return false
}

0 comments on commit d1e018b

Please # to comment.