Skip to content

Commit

Permalink
resolve conflicts between locally defined Driver module and Ppxlib's …
Browse files Browse the repository at this point in the history
…Driver module
  • Loading branch information
lubegasimon committed Nov 14, 2023
1 parent b8ca477 commit 53146da
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 41 deletions.
27 changes: 14 additions & 13 deletions packages/ppx/src/css_to_emotion.re
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
open Ppxlib;
open Css_types;

module Helper = Ast_helper;
module Builder = Ast_builder.Default;
module Helper = Ppxlib.Ast_helper;
module Builder = Ppxlib.Ast_builder.Default;

exception Empty_buffer(string);

Expand Down Expand Up @@ -57,13 +56,14 @@ let string_to_const = (~loc, s) => {
};
};
let render_variable = (~loc, v) => {
open Ppxlib;
let txt = v |> String.concat(".") |> Longident.parse;
Helper.Exp.ident({loc, txt});
};

let source_code_of_loc = (loc: Location.t) => {
let Location.{loc_start, loc_end, _} = loc;
switch (Driver_.last_buffer^) {
switch (Driver.last_buffer^) {
| Some(buffer: Sedlexing.lexbuf) =>
/* TODO: pos_offset is hardcoded to 0, unsure about the effects */
let pos_offset = 0;
Expand All @@ -78,7 +78,7 @@ let concat = (~loc, expr, acc) => {
Helper.Exp.apply(~loc, concat_fn, [(Nolabel, expr), (Nolabel, acc)]);
};

let rec render_at_rule = (at_rule: at_rule): Parsetree.expression => {
let rec render_at_rule = (at_rule: at_rule) => {
switch (at_rule.name) {
| ("media", _) => render_media_query(at_rule)
| ("keyframes", loc) =>
Expand Down Expand Up @@ -111,11 +111,11 @@ let rec render_at_rule = (at_rule: at_rule): Parsetree.expression => {
| (n, loc) => Generate_lib.error(~loc, Printf.sprintf("Unknown @%s ", n))
};
}
and render_media_query = (at_rule: at_rule): Parsetree.expression => {
and render_media_query = (at_rule: at_rule) => {
let (prelude, prelude_loc) = at_rule.prelude;
let parse_condition =
(component_value: (component_value, location))
: result(string, Parsetree.expression) => {
(component_value: (component_value, Ppxlib.location))
: result(string, Ppxlib.Parsetree.expression) => {
let (value, loc) = component_value;
let component_value_location = loc;
switch (value) {
Expand Down Expand Up @@ -217,7 +217,7 @@ and render_media_query = (at_rule: at_rule): Parsetree.expression => {
);
};
}
and render_declaration = (d: declaration): list(Parsetree.expression) => {
and render_declaration = (d: declaration) => {
let (property, name_loc) = d.name;
let (_valueList, loc) = d.value;
/* String.trim is a hack, location should be correct and not contain any whitespace */
Expand Down Expand Up @@ -249,7 +249,7 @@ and render_declaration = (d: declaration): list(Parsetree.expression) => {
]
};
}
and render_declarations = ((ds, _loc: location)) => {
and render_declarations = ((ds, _loc: Ppxlib.location)) => {
ds
|> List.concat_map(declaration =>
switch (declaration) {
Expand Down Expand Up @@ -374,7 +374,7 @@ and render_selectors = selectors => {
|> List.map(((selector, _loc)) => render_selector(selector))
|> String.concat(", ");
}
and render_style_rule = (ident, rule: style_rule): Parsetree.expression => {
and render_style_rule = (ident, rule: style_rule) => {
let (prelude, _loc) = rule.prelude;
let (_block, loc) = rule.block;
let selector_expr =
Expand Down Expand Up @@ -407,7 +407,8 @@ let addLabel = (~loc, label, emotionExprs) => [
...emotionExprs,
];

let render_style_call = (declaration_list): Parsetree.expression => {
let render_style_call = declaration_list => {
open Ppxlib;
let loc = declaration_list.pexp_loc;
let arguments = [(Nolabel, declaration_list)];

Expand All @@ -419,7 +420,7 @@ let render_style_call = (declaration_list): Parsetree.expression => {
);
};

let render_keyframes = (declarations: rule_list): Parsetree.expression => {
let render_keyframes = (declarations: rule_list) => {
let (declarations, loc) = declarations;
let invalid_selector = {|
keyframe selector can be from | to | <percentage>
Expand Down
57 changes: 29 additions & 28 deletions packages/ppx/src/ppx.re
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
open Ppxlib;

module Builder = Ast_builder.Default;
module Builder = Ppxlib.Ast_builder.Default;

module Mapper = {
open Ppxlib;
let match = module_expr => {
open Ast_pattern;

Expand Down Expand Up @@ -632,7 +631,7 @@ module Mapper = {

let traverser = {
as _;
inherit class Ast_traverse.map as super;
inherit class Ppxlib.Ast_traverse.map as super;
pub! structure_item = expr => {
File.set(expr.pstr_loc.loc_start.pos_fname);
let expr = super#structure_item(expr);
Expand All @@ -642,7 +641,7 @@ let traverser = {

/* TODO: Throw better errors when this pattern doesn't match */
let static_pattern =
Ast_pattern.(
Ppxlib.Ast_pattern.(
pstr(
pstr_eval(
map(
Expand Down Expand Up @@ -684,22 +683,24 @@ switch (version) {
| None => ()
};

let string_payload_pattern = Ast_pattern.(single_expr_payload(estring(__)));
let string_payload_pattern =
Ppxlib.Ast_pattern.(single_expr_payload(estring(__)));

let any_payload_pattern = Ast_pattern.(single_expr_payload(__));
let any_payload_pattern = Ppxlib.Ast_pattern.(single_expr_payload(__));

let _ =
Driver.register_transformation(
Ppxlib.Driver.register_transformation(
/* Instrument is needed to run styled-ppx after metaquote,
we rely on this order in native tests */
~instrument=Driver.Instrument.make(~position=Before, traverser#structure),
~instrument=
Ppxlib.Driver.Instrument.make(~position=Before, traverser#structure),
~rules=[
/* %cx without let binding, it doesn't have CssJs.label
%cx is defined in traverser#structure */
Context_free.Rule.extension(
Extension.declare(
Ppxlib.Context_free.Rule.extension(
Ppxlib.Extension.declare(
"cx",
Extension.Context.Expression,
Ppxlib.Extension.Context.Expression,
static_pattern,
(~loc, ~path, payload) => {
File.set(path);
Expand All @@ -721,15 +722,15 @@ let _ =
},
),
),
Context_free.Rule.extension(
Extension.declare(
Ppxlib.Context_free.Rule.extension(
Ppxlib.Extension.declare(
"css",
Extension.Context.Expression,
Ppxlib.Extension.Context.Expression,
string_payload_pattern,
(~loc, ~path, payload) => {
File.set(path);
let pos = Some(loc.loc_start);
switch (Driver_.parse_declaration(~pos, payload)) {
switch (Driver.parse_declaration(~pos, payload)) {
| Ok(declarations) =>
let declarationListValues =
Css_to_emotion.render_declaration(declarations);
Expand All @@ -742,30 +743,30 @@ let _ =
},
),
),
Context_free.Rule.extension(
Extension.declare(
Ppxlib.Context_free.Rule.extension(
Ppxlib.Extension.declare(
"styled.global",
Extension.Context.Expression,
Ppxlib.Extension.Context.Expression,
string_payload_pattern,
(~loc, ~path, payload) => {
File.set(path);
let pos = Some(loc.loc_start);
switch (Driver_.parse_stylesheet(~pos, payload)) {
switch (Driver.parse_stylesheet(~pos, payload)) {
| Ok(stylesheets) => Css_to_emotion.render_global(stylesheets)
| Error((loc, msg)) => Generate_lib.error(~loc, msg)
};
},
),
),
Context_free.Rule.extension(
Extension.declare(
Ppxlib.Context_free.Rule.extension(
Ppxlib.Extension.declare(
"keyframe",
Extension.Context.Expression,
Ppxlib.Extension.Context.Expression,
string_payload_pattern,
(~loc, ~path, payload) => {
File.set(path);
let pos = Some(loc.loc_start);
switch (Driver_.parse_keyframes(~pos, payload)) {
switch (Driver.parse_keyframes(~pos, payload)) {
| Ok(declarations) =>
Css_to_emotion.render_keyframes(declarations)
| Error((loc, msg)) => Generate_lib.error(~loc, msg)
Expand All @@ -774,10 +775,10 @@ let _ =
),
),
/* This extension just raises an error to educate, since before 0.20 this was valid */
Context_free.Rule.extension(
Extension.declare(
Ppxlib.Context_free.Rule.extension(
Ppxlib.Extension.declare(
"styled",
Extension.Context.Module_expr,
Ppxlib.Extension.Context.Module_expr,
any_payload_pattern,
(~loc, ~path as _, payload) => {
Generate_lib.raiseError(
Expand All @@ -787,7 +788,7 @@ let _ =
~example=
Some(
"[%styled.div "
++ Pprintast.string_of_expression(payload)
++ Ppxlib.Pprintast.string_of_expression(payload)
++ "]",
),
~link=
Expand Down

0 comments on commit 53146da

Please sign in to comment.