Skip to content
This repository has been archived by the owner on Dec 8, 2023. It is now read-only.

Commit

Permalink
make helper patterns private
Browse files Browse the repository at this point in the history
  • Loading branch information
ilevyor committed Nov 29, 2023
1 parent e4b3977 commit e52c3c7
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 67 deletions.
6 changes: 3 additions & 3 deletions .grit/patterns/imports.grit
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pattern before_each_file_prep_imports() {
}
}

pattern the_import_statement($imports, $source) {
private pattern the_import_statement($imports, $source) {
import_statement(import = import_clause(name = named_imports($imports)), $source)
}

Expand Down Expand Up @@ -40,7 +40,7 @@ pattern ensure_import_from($source) {
}
}

pattern process_one_source($p, $all_imports) {
private pattern process_one_source($p, $all_imports) {
[$p, $source] where {
$imported_names = [],
$GLOBAL_IMPORTED_NAMES <: some bubble($p, $source, $imported_names, $all_imports) [$p, $name, $source] where {
Expand All @@ -55,7 +55,7 @@ pattern process_one_source($p, $all_imports) {
}
}

pattern insert_imports() {
private pattern insert_imports() {
$p where {
$all_imports = [],
$GLOBAL_IMPORTED_SOURCES <: some process_one_source($p, $all_imports),
Expand Down
4 changes: 2 additions & 2 deletions .grit/patterns/marzano.grit
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ pattern function_like($name, $args, $statements) {
}

// All core stdlib functions can be done here
pattern before_each_file_stdlib() {
private pattern before_each_file_stdlib() {
before_each_file_prep_imports()
}

pattern after_each_file_stdlib() {
private pattern after_each_file_stdlib() {
after_each_file_handle_imports()
}

Expand Down
84 changes: 42 additions & 42 deletions .grit/patterns/moment-to-date-fns.grit
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language js

// A call to `add`/`sub` method of a `momentjs` date object.
pattern moment_arith($date, $method, $count, $specifier) {
private pattern moment_arith($date, $method, $count, $specifier) {
`$date.$method($count, $specifier)` as $arith_exp where {
$method <: or { `add` => `addDate`, `subtract` => `subDate` },
if ($method <: `add`) {
Expand All @@ -14,14 +14,14 @@ pattern moment_arith($date, $method, $count, $specifier) {
}

// A call to `add/sub` where the caller (`this`) is an identifier. eg: foo.add(bar);
pattern moment_arith_lhs_identifier($date, $method, $count, $specifier) {
private pattern moment_arith_lhs_identifier($date, $method, $count, $specifier) {
moment_arith($date, $method, $count, $specifier) where {
$date <: identifier()
}
}

// Any call expression that returns a moment.js date or duration object.
pattern exp_returns_moment($exp) {
private pattern exp_returns_moment($exp) {
`$fn()` as $exp where {
$fn <: or {
`moment`,
Expand All @@ -40,65 +40,65 @@ pattern exp_returns_moment($exp) {
}

// A variable declarator that declares a moment.js Date or Duration object.
pattern moment_var_decl($var_name) {
private pattern moment_var_decl($var_name) {
variable_declarator(name=$var_name, $value) where {
$value <: exp_returns_moment(),
$var_name <: identifier()
}
}

// collect all identifiers that are declared as a moment.js date or duration
pattern collect_moment_identifier_decls($id_list) {
private pattern collect_moment_identifier_decls($id_list) {
moment_var_decl(var_name=$name) where {
$id_list += $name
}
}

// A `const` variable declaration that initializes a moment date or duration object
pattern moment_const_decl($declaration, $declarators) {
private pattern moment_const_decl($declaration, $declarators) {
`$varName = $moment` where {
$moment <: exp_returns_moment(),
$varName <: within `$declaration` where { $declaration <: `const $declarators` },
}
}
// Matches calls like `moment.startOf('day')`, `moment.startOf('week')`
pattern moment_set_to_start_or_end_literal($date, $func_type, $unit) {
private pattern moment_set_to_start_or_end_literal($date, $func_type, $unit) {
`$date.$func_type($specifier)` where {
$func_type <: or { `startOf`, `endOf` },
$specifier<: string(fragment=$unit)
}
}

// moment.max(...) or moment.min(...)
pattern moment_limit_to_max_or_min($date, $arg, $methodName) {
private pattern moment_limit_to_max_or_min($date, $arg, $methodName) {
`$date.$methodName($arg)` where {
$methodName <: or { `max`, `min` }
}
}

// Helper patterns to match format specifiers for different time units in moment js:
pattern year() { string(fragment=r"\b(?:y|years?)\b") }
pattern month() { string(fragment=r"\b(?:M|months?)\b") }
pattern quarter() { string(fragment=r"\b(?:q|quarters?)\b") }
pattern week() { string(fragment=r"\b(?:w|weeks?)\b") }
pattern day() { string(fragment=r"\b(?:d|days?)\b") }
pattern hour() { string(fragment=r"\b(?:h|hours?)\b") }
pattern minute() { string(fragment=r"\b(?:m|minutes?)\b") }
pattern second() { string(fragment=r"\b(?:s|seconds?)\b") }
pattern millisecond() { string(fragment=r"\b(?:ms|milliseconds?)\b") }

pattern regex_year() { r"\b(?:(?i)y|years?)\b" }
pattern regex_quarter() { r"\b(?:(?i)q|quarters?)\b" }
pattern regex_month() { r"\b(?:M|(?i)months?)\b" }
pattern regex_week() { r"\b(?:(?i)w|weeks?)\b" }
pattern regex_date() { r"\b(?:(?i)d|dates?)\b" }
pattern regex_day() { r"\b(?:(?i)d|days?)\b" }
pattern regex_hour() { r"\b(?:(?i)h|hours?)\b" }
pattern regex_minute() { r"\b(?:(?i)m|minutes?)\b" }
pattern regex_second() { r"\b(?:(?i)s|seconds?)\b" }
pattern regex_ms() { r"\b(?:(?i)ms|milliseconds?)\b" }

pattern moment_getter_or_setter() {
private pattern year() { string(fragment=r"\b(?:y|years?)\b") }
private pattern month() { string(fragment=r"\b(?:M|months?)\b") }
private pattern quarter() { string(fragment=r"\b(?:q|quarters?)\b") }
private pattern week() { string(fragment=r"\b(?:w|weeks?)\b") }
private pattern day() { string(fragment=r"\b(?:d|days?)\b") }
private pattern hour() { string(fragment=r"\b(?:h|hours?)\b") }
private pattern minute() { string(fragment=r"\b(?:m|minutes?)\b") }
private pattern second() { string(fragment=r"\b(?:s|seconds?)\b") }
private pattern millisecond() { string(fragment=r"\b(?:ms|milliseconds?)\b") }

private pattern regex_year() { r"\b(?:(?i)y|years?)\b" }
private pattern regex_quarter() { r"\b(?:(?i)q|quarters?)\b" }
private pattern regex_month() { r"\b(?:M|(?i)months?)\b" }
private pattern regex_week() { r"\b(?:(?i)w|weeks?)\b" }
private pattern regex_date() { r"\b(?:(?i)d|dates?)\b" }
private pattern regex_day() { r"\b(?:(?i)d|days?)\b" }
private pattern regex_hour() { r"\b(?:(?i)h|hours?)\b" }
private pattern regex_minute() { r"\b(?:(?i)m|minutes?)\b" }
private pattern regex_second() { r"\b(?:(?i)s|seconds?)\b" }
private pattern regex_ms() { r"\b(?:(?i)ms|milliseconds?)\b" }

private pattern moment_getter_or_setter() {
`$d.$accessor($arg)` as $exp where {
$accessor <: r"(year|month|week|day|hour|minute|second|millisecond)s?"($unit),
$duration_accessor = join(list=[$unit, "s"], separator=""),
Expand Down Expand Up @@ -143,7 +143,7 @@ pattern moment_getter_or_setter() {
}

// convert a moment.js unit specifier to its normal form ("y" -> "year")
pattern normalize_unit_specifier($norm) {
private pattern normalize_unit_specifier($norm) {
`$specifier` where {
$return = ``,
if ($specifier <: regex_year()) {
Expand Down Expand Up @@ -171,7 +171,7 @@ pattern normalize_unit_specifier($norm) {

// add/subtract: convert all momentjs arithmetic operations to date-fns operations.
// eg: today.add(oneday) => add(today, oneday)
pattern rewrite_moment_arithmetic() {
private pattern rewrite_moment_arithmetic() {
or {
// add/subtract: convert all momentjs arithmetic operations to date-fns operations.
// eg: today.add(oneday) => add(today, oneday)
Expand Down Expand Up @@ -231,7 +231,7 @@ pattern rewrite_moment_arithmetic() {
}
}

pattern rewrite_moment_getters_and_setters() {
private pattern rewrite_moment_getters_and_setters() {
or {
`$date.daysInMonth()` => `datefns.getDaysInMonth($date)`,
moment_getter_or_setter(),
Expand Down Expand Up @@ -316,7 +316,7 @@ pattern rewrite_moment_getters_and_setters() {
}
}

pattern rewrite_duration_set_and_get() {
private pattern rewrite_duration_set_and_get() {
`$duration.$getter()` where {
$getter <: or {
`asMilliseconds` => `toMilliseconds`,
Expand All @@ -341,7 +341,7 @@ pattern rewrite_duration_set_and_get() {
// 2. https://date-fns.org/v2.30.0/docs/Locale
// There is no 1:1 feature parity with moment.js, and thus a correct migration
// is not straight-forward (or even possible without using implementation details of date-fns's locale representation).
pattern remove_moment_global_customizations() {
private pattern remove_moment_global_customizations() {
or {
`$fn($_)` as $exp where {
$fn <: or {
Expand Down Expand Up @@ -369,7 +369,7 @@ pattern remove_moment_global_customizations() {
// This could technically be migrated by maintaining a global map of flags and
// turning every `utc()` call into `set`, and every `display` call into a custom
// overload, but I think that would introduce too much unwanted noise into the user's program.
pattern missing_datefns_features() {
private pattern missing_datefns_features() {
or {
`$date.$method()` where {
$method <: or { `utc`, `local` }
Expand All @@ -379,7 +379,7 @@ pattern missing_datefns_features() {
}

// Rewrite .toJSON and .toArray calls
pattern rewrite_to_json_or_array($moment_ids) {
private pattern rewrite_to_json_or_array($moment_ids) {
or {
`$d.toJSON()` as $exp where {
$out = `/* if "$d" is a moment-js object, replace with date.toJSON() call */ $exp`,
Expand All @@ -398,15 +398,15 @@ pattern rewrite_to_json_or_array($moment_ids) {
}

// rewrite `$d.toJSON()` to an appropriate JSON conversion call.
pattern rewrite_conversion_calls() {
private pattern rewrite_conversion_calls() {
program($statements) where {
$id_list = [],
$statements <: maybe contains collect_moment_identifier_decls($id_list),
$statements <: maybe contains bubble($id_list) rewrite_to_json_or_array(moment_ids=$id_list),
}
}

pattern rewrite_misc_methods() {
private pattern rewrite_misc_methods() {
or {
`$d.clone()` as $exp where {
$out = `((d => (d instanceof Date) ? (new Date(d.getTime())) : structuredClone(d) )($d))`,
Expand Down Expand Up @@ -445,7 +445,7 @@ pattern rewrite_misc_methods() {
}
}

pattern rewrite_moment_queries() {
private pattern rewrite_moment_queries() {
or {
`$date_a.$compare($date_b)` where {
$compare <: or { `isBefore`, `isAfter` }
Expand Down Expand Up @@ -484,13 +484,13 @@ pattern rewrite_moment_queries() {
}
}

pattern rewrite_moment_distance() {
private pattern rewrite_moment_distance() {
`$date.toNow()` => `datefns.formatDistanceToNow($date)`,
`$date_a.from($date_b)` => `(datefns.formatDistance($date_a, $date_b) + " ago")`,
`$date_a.to($date_b)` => `datefns.formatDistance($date_a, $date_b)`,
}

pattern rewrite_duration_ctors() {
private pattern rewrite_duration_ctors() {
or {
// NOTE: date-fns only provides a `Duration` object ([docs](https://date-fns.org/v2.30.0/docs/Duration)).
// Curiously, it does not provide any helpers to manipulate these duration objects.
Expand Down
9 changes: 0 additions & 9 deletions .grit/patterns/react.grit

This file was deleted.

22 changes: 11 additions & 11 deletions .grit/patterns/react_hooks.grit
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
engine marzano(0.1)
language js

pattern handle_ref($statements, $statement, $name, $type) {
private pattern handle_ref($statements, $statement, $name, $type) {
$value where {
or {
and {
Expand Down Expand Up @@ -38,7 +38,7 @@ pattern handle_ref($statements, $statement, $name, $type) {
}
}

pattern handle_one_statement($class_name, $statements, $states_statements, $static_statements, $render_statements, $constructor_statements, $handler_callback_suffix, $use_ref_from) {
private pattern handle_one_statement($class_name, $statements, $states_statements, $static_statements, $render_statements, $constructor_statements, $handler_callback_suffix, $use_ref_from) {
or {
method_definition($static, $async, $name, $body, $parameters) as $statement where {
or {
Expand Down Expand Up @@ -218,12 +218,12 @@ pattern handle_one_statement($class_name, $statements, $states_statements, $stat
},
}
}
pattern prepend_comment($statements) {
private pattern prepend_comment($statements) {
maybe after comment() as $comment where {
$statements += js"$comment"
}
}
pattern change_this($states_statements, $is_constructor) {
private pattern change_this($states_statements, $is_constructor) {
maybe contains bubble($states_statements, $is_constructor) or {
assignment_expression(
left = `this.state`,
Expand Down Expand Up @@ -255,7 +255,7 @@ pattern change_this($states_statements, $is_constructor) {
}
}

pattern find_miscellaneous_state($states_statements) {
private pattern find_miscellaneous_state($states_statements) {
maybe contains `this.state.$name` where {
$capitalized = capitalize(string = $name),
$program <: not contains `constructor ($_) { $body }` where {
Expand All @@ -266,7 +266,7 @@ pattern find_miscellaneous_state($states_statements) {
}


pattern gather_hooks($hooks) {
private pattern gather_hooks($hooks) {
contains or {
`useEffect` where {
$hooks <: not some `useEffect`,
Expand Down Expand Up @@ -327,7 +327,7 @@ pattern adjust_imports() {
}
}

pattern maybe_wrapped_class_declaration($class_name, $body, $class) {
private pattern maybe_wrapped_class_declaration($class_name, $body, $class) {
or {
export_statement(declaration = class_declaration(name = $class_name, $body, $heritage) as $class),
class_declaration(name = $class_name, $body, $heritage) as $class
Expand Down Expand Up @@ -438,7 +438,7 @@ export const $original_name = observer($base_name);`,
}
}

pattern find_dependencies($hoisted_states, $dependencies) {
private pattern find_dependencies($hoisted_states, $dependencies) {
contains bubble($hoisted_states, $dependencies) identifier() as $i where {
$i <: not `props`,
$hoisted_states <: some $i,
Expand All @@ -447,7 +447,7 @@ pattern find_dependencies($hoisted_states, $dependencies) {
}
}

pattern rewrite_accesses($hoisted_states, $hoisted_refs, $use_memos, $handler_callback_suffix) {
private pattern rewrite_accesses($hoisted_states, $hoisted_refs, $use_memos, $handler_callback_suffix) {
or {
`this.state.$x` => `$x`,
`this.$property` as $p where {
Expand Down Expand Up @@ -544,7 +544,7 @@ pattern rewrite_accesses($hoisted_states, $hoisted_refs, $use_memos, $handler_ca
}

// Find places we're accessing state, refs, or memos
pattern gather_accesses($hoisted_states, $hoisted_refs, $use_memos) {
private pattern gather_accesses($hoisted_states, $hoisted_refs, $use_memos) {
contains bubble($hoisted_states, $hoisted_refs, $use_memos) variable_declarator($name, $value) where {
or {
and {
Expand Down Expand Up @@ -614,7 +614,7 @@ pattern second_step($handler_callback_suffix) {
}
}

pattern fix_react_default_export() {
private pattern fix_react_default_export() {
$default_export as $default_export where {
$default_export <: r"(?s)(export default const)(.+?)=.+"($target, $name) where {
$target => js"const",
Expand Down

0 comments on commit e52c3c7

Please sign in to comment.