Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions ext/herb/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ static VALUE Herb_parse(int argc, VALUE* argv, VALUE self) {
}
if (!NIL_P(action_view_helpers) && RTEST(action_view_helpers)) { parser_options.action_view_helpers = true; }

VALUE transform_conditionals = rb_hash_lookup(options, rb_utf8_str_new_cstr("transform_conditionals"));
if (NIL_P(transform_conditionals)) {
transform_conditionals = rb_hash_lookup(options, ID2SYM(rb_intern("transform_conditionals")));
}
if (!NIL_P(transform_conditionals) && RTEST(transform_conditionals)) {
parser_options.transform_conditionals = true;
}

VALUE dot_notation_tags = rb_hash_lookup(options, rb_utf8_str_new_cstr("dot_notation_tags"));
if (NIL_P(dot_notation_tags)) {
dot_notation_tags = rb_hash_lookup(options, ID2SYM(rb_intern("dot_notation_tags")));
Expand Down
1 change: 1 addition & 0 deletions ext/herb/extension_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ VALUE create_parse_result(AST_DOCUMENT_NODE_T* root, VALUE source, const parser_
rb_hash_aset(kwargs, ID2SYM(rb_intern("track_whitespace")), options->track_whitespace ? Qtrue : Qfalse);
rb_hash_aset(kwargs, ID2SYM(rb_intern("analyze")), options->analyze ? Qtrue : Qfalse);
rb_hash_aset(kwargs, ID2SYM(rb_intern("action_view_helpers")), options->action_view_helpers ? Qtrue : Qfalse);
rb_hash_aset(kwargs, ID2SYM(rb_intern("transform_conditionals")), options->transform_conditionals ? Qtrue : Qfalse);
rb_hash_aset(kwargs, ID2SYM(rb_intern("render_nodes")), options->render_nodes ? Qtrue : Qfalse);
rb_hash_aset(kwargs, ID2SYM(rb_intern("strict_locals")), options->strict_locals ? Qtrue : Qfalse);
rb_hash_aset(kwargs, ID2SYM(rb_intern("prism_nodes")), options->prism_nodes ? Qtrue : Qfalse);
Expand Down
8 changes: 8 additions & 0 deletions java/herb_jni.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ Java_org_herb_Herb_parse(JNIEnv* env, jclass clazz, jstring source, jobject opti
parser_options.action_view_helpers = (actionViewHelpers == JNI_TRUE);
}

jmethodID getTransformConditionals =
(*env)->GetMethodID(env, optionsClass, "isTransformConditionals", "()Z");

if (getTransformConditionals != NULL) {
jboolean transformConditionals = (*env)->CallBooleanMethod(env, options, getTransformConditionals);
parser_options.transform_conditionals = (transformConditionals == JNI_TRUE);
}

jmethodID getRenderNodes =
(*env)->GetMethodID(env, optionsClass, "isRenderNodes", "()Z");

Expand Down
10 changes: 10 additions & 0 deletions java/org/herb/ParserOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class ParserOptions {
private boolean analyze = true;
private boolean strict = true;
private boolean actionViewHelpers = false;
private boolean transformConditionals = false;
private boolean renderNodes = false;
private boolean strictLocals = false;
private boolean prismNodes = false;
Expand Down Expand Up @@ -51,6 +52,15 @@ public boolean isActionViewHelpers() {
return actionViewHelpers;
}

public ParserOptions transformConditionals(boolean value) {
this.transformConditionals = value;
return this;
}

public boolean isTransformConditionals() {
return transformConditionals;
}

public ParserOptions renderNodes(boolean value) {
this.renderNodes = value;
return this;
Expand Down
6 changes: 6 additions & 0 deletions javascript/packages/core/src/parser-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export interface ParseOptions {
analyze?: boolean
strict?: boolean
action_view_helpers?: boolean
transform_conditionals?: boolean
render_nodes?: boolean
strict_locals?: boolean
prism_nodes?: boolean
Expand All @@ -19,6 +20,7 @@ export const DEFAULT_PARSER_OPTIONS: SerializedParserOptions = {
analyze: true,
strict: true,
action_view_helpers: false,
transform_conditionals: false,
render_nodes: false,
strict_locals: false,
prism_nodes: false,
Expand All @@ -44,6 +46,9 @@ export class ParserOptions {
/** Whether ActionView tag helper transformation was enabled during parsing. */
readonly action_view_helpers: boolean

/** Whether postfix conditional transformation was enabled during parsing. */
readonly transform_conditionals: boolean

/** Whether ActionView render call detection was enabled during parsing. */
readonly render_nodes: boolean

Expand Down Expand Up @@ -74,6 +79,7 @@ export class ParserOptions {
this.track_whitespace = options.track_whitespace ?? DEFAULT_PARSER_OPTIONS.track_whitespace
this.analyze = options.analyze ?? DEFAULT_PARSER_OPTIONS.analyze
this.action_view_helpers = options.action_view_helpers ?? DEFAULT_PARSER_OPTIONS.action_view_helpers
this.transform_conditionals = options.transform_conditionals ?? DEFAULT_PARSER_OPTIONS.transform_conditionals
this.render_nodes = options.render_nodes ?? DEFAULT_PARSER_OPTIONS.render_nodes
this.strict_locals = options.strict_locals ?? DEFAULT_PARSER_OPTIONS.strict_locals
this.prism_nodes = options.prism_nodes ?? DEFAULT_PARSER_OPTIONS.prism_nodes
Expand Down
4 changes: 4 additions & 0 deletions javascript/packages/linter/test/parse-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ describe("ParseCache", () => {
strict_locals: false,
action_view_helpers: false,
dot_notation_tags: false,
transform_conditionals: false,
html: true,
})
})
Expand All @@ -95,6 +96,7 @@ describe("ParseCache", () => {
strict_locals: false,
action_view_helpers: false,
dot_notation_tags: false,
transform_conditionals: false,
html: true,
})
})
Expand All @@ -114,6 +116,7 @@ describe("ParseCache", () => {
strict_locals: false,
action_view_helpers: false,
dot_notation_tags: false,
transform_conditionals: false,
html: true,
})
})
Expand All @@ -133,6 +136,7 @@ describe("ParseCache", () => {
strict_locals: false,
action_view_helpers: false,
dot_notation_tags: false,
transform_conditionals: false,
html: true,
})
})
Expand Down
1 change: 1 addition & 0 deletions javascript/packages/node/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"./extension/libherb/analyze/invalid_structures.c",
"./extension/libherb/analyze/missing_end.c",
"./extension/libherb/analyze/parse_errors.c",
"./extension/libherb/analyze/postfix_conditionals.c",
"./extension/libherb/analyze/prism_annotate.c",
"./extension/libherb/analyze/render_nodes.c",
"./extension/libherb/analyze/strict_locals.c",
Expand Down
9 changes: 7 additions & 2 deletions lib/herb/parser_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class ParserOptions
attr_reader :track_whitespace #: bool
attr_reader :analyze #: bool
attr_reader :action_view_helpers #: bool
attr_reader :transform_conditionals #: bool
attr_reader :render_nodes #: bool
attr_reader :strict_locals #: bool
attr_reader :prism_program #: bool
Expand All @@ -16,18 +17,20 @@ class ParserOptions
DEFAULT_TRACK_WHITESPACE = false #: bool
DEFAULT_ANALYZE = true #: bool
DEFAULT_ACTION_VIEW_HELPERS = false #: bool
DEFAULT_TRANSFORM_CONDITIONALS = false #: bool
DEFAULT_RENDER_NODES = false #: bool
DEFAULT_STRICT_LOCALS = false #: bool
DEFAULT_PRISM_PROGRAM = false #: bool
DEFAULT_PRISM_NODES = false #: bool
DEFAULT_PRISM_NODES_DEEP = false #: bool

#: (?strict: bool, ?track_whitespace: bool, ?analyze: bool, ?action_view_helpers: bool, ?render_nodes: bool, ?strict_locals: bool, ?prism_nodes: bool, ?prism_nodes_deep: bool, ?prism_program: bool) -> void
def initialize(strict: DEFAULT_STRICT, track_whitespace: DEFAULT_TRACK_WHITESPACE, analyze: DEFAULT_ANALYZE, action_view_helpers: DEFAULT_ACTION_VIEW_HELPERS, render_nodes: DEFAULT_RENDER_NODES, strict_locals: DEFAULT_STRICT_LOCALS, prism_nodes: DEFAULT_PRISM_NODES, prism_nodes_deep: DEFAULT_PRISM_NODES_DEEP, prism_program: DEFAULT_PRISM_PROGRAM)
#: (?strict: bool, ?track_whitespace: bool, ?analyze: bool, ?action_view_helpers: bool, ?transform_conditionals: bool, ?render_nodes: bool, ?strict_locals: bool, ?prism_nodes: bool, ?prism_nodes_deep: bool, ?prism_program: bool) -> void
def initialize(strict: DEFAULT_STRICT, track_whitespace: DEFAULT_TRACK_WHITESPACE, analyze: DEFAULT_ANALYZE, action_view_helpers: DEFAULT_ACTION_VIEW_HELPERS, transform_conditionals: DEFAULT_TRANSFORM_CONDITIONALS, render_nodes: DEFAULT_RENDER_NODES, strict_locals: DEFAULT_STRICT_LOCALS, prism_nodes: DEFAULT_PRISM_NODES, prism_nodes_deep: DEFAULT_PRISM_NODES_DEEP, prism_program: DEFAULT_PRISM_PROGRAM)
@strict = strict
@track_whitespace = track_whitespace
@analyze = analyze
@action_view_helpers = action_view_helpers
@transform_conditionals = transform_conditionals
@render_nodes = render_nodes
@strict_locals = strict_locals
@prism_nodes = prism_nodes
Expand All @@ -42,6 +45,7 @@ def to_h
track_whitespace: @track_whitespace,
analyze: @analyze,
action_view_helpers: @action_view_helpers,
transform_conditionals: @transform_conditionals,
render_nodes: @render_nodes,
strict_locals: @strict_locals,
prism_nodes: @prism_nodes,
Expand All @@ -57,6 +61,7 @@ def inspect
"track_whitespace=#{@track_whitespace}\n " \
"analyze=#{@analyze}\n " \
"action_view_helpers=#{@action_view_helpers}\n " \
"transform_conditionals=#{@transform_conditionals}\n " \
"render_nodes=#{@render_nodes}\n " \
"strict_locals=#{@strict_locals}\n " \
"prism_nodes=#{@prism_nodes}\n " \
Expand Down
10 changes: 10 additions & 0 deletions playground/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,16 @@
<span class="select-none">Action View helpers</span>
</label>

<label class="flex items-center gap-1.5 text-gray-300 text-sm" title="Transform inline conditionals (postfix if/unless, ternaries) into ERBIfNode/ERBUnlessNode wrapping the expression">
<input
type="checkbox"
data-option="transform_conditionals"
data-action="change->playground#onOptionChange"
class="rounded border-gray-600 text-green-600 focus:ring-green-500 bg-gray-700"
/>
<span class="select-none">Transform conditionals</span>
</label>

<label class="flex items-center gap-1.5 text-gray-300 text-sm" title="Detect ActionView render calls and represent them as ERBRenderNode with extracted partial, collection, layout, object, as, and locals fields">
<input
type="checkbox"
Expand Down
1 change: 1 addition & 0 deletions playground/src/controllers/playground_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,7 @@ export default class extends Controller {
analyze: true,
strict: true,
action_view_helpers: false,
transform_conditionals: false,
render_nodes: false,
strict_locals: false,
prism_program: false,
Expand Down
3 changes: 3 additions & 0 deletions rust/src/herb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct ParserOptions {
pub analyze: bool,
pub strict: bool,
pub action_view_helpers: bool,
pub transform_conditionals: bool,
pub render_nodes: bool,
pub strict_locals: bool,
pub prism_nodes: bool,
Expand All @@ -25,6 +26,7 @@ impl Default for ParserOptions {
analyze: true,
strict: true,
action_view_helpers: false,
transform_conditionals: false,
render_nodes: false,
strict_locals: false,
prism_nodes: false,
Expand Down Expand Up @@ -108,6 +110,7 @@ pub fn parse_with_options(source: &str, options: &ParserOptions) -> Result<Parse
analyze: options.analyze,
strict: options.strict,
action_view_helpers: options.action_view_helpers,
transform_conditionals: options.transform_conditionals,
render_nodes: options.render_nodes,
strict_locals: options.strict_locals,
prism_program: options.prism_program,
Expand Down
8 changes: 6 additions & 2 deletions sig/herb/parser_options.rbs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading