DEV: Apply syntax_tree formatting to lib/*

This commit is contained in:
David Taylor
2023-01-09 12:10:19 +00:00
parent b0fda61a8e
commit 6417173082
507 changed files with 16550 additions and 12627 deletions

View File

@ -1,27 +1,28 @@
# frozen_string_literal: true
require 'execjs'
require 'mini_racer'
require "execjs"
require "mini_racer"
class DiscourseJsProcessor
class TranspileError < StandardError; end
class TranspileError < StandardError
end
DISCOURSE_COMMON_BABEL_PLUGINS = [
'proposal-optional-chaining',
['proposal-decorators', { legacy: true } ],
'transform-template-literals',
'proposal-class-properties',
'proposal-class-static-block',
'proposal-private-property-in-object',
'proposal-private-methods',
'proposal-numeric-separator',
'proposal-logical-assignment-operators',
'proposal-nullish-coalescing-operator',
'proposal-json-strings',
'proposal-optional-catch-binding',
'transform-parameters',
'proposal-async-generator-functions',
'proposal-object-rest-spread',
'proposal-export-namespace-from',
"proposal-optional-chaining",
["proposal-decorators", { legacy: true }],
"transform-template-literals",
"proposal-class-properties",
"proposal-class-static-block",
"proposal-private-property-in-object",
"proposal-private-methods",
"proposal-numeric-separator",
"proposal-logical-assignment-operators",
"proposal-nullish-coalescing-operator",
"proposal-json-strings",
"proposal-optional-catch-binding",
"transform-parameters",
"proposal-async-generator-functions",
"proposal-object-rest-spread",
"proposal-export-namespace-from",
]
def self.plugin_transpile_paths
@ -33,22 +34,22 @@ class DiscourseJsProcessor
end
def self.call(input)
root_path = input[:load_path] || ''
logical_path = (input[:filename] || '').sub(root_path, '').gsub(/\.(js|es6).*$/, '').sub(/^\//, '')
root_path = input[:load_path] || ""
logical_path =
(input[:filename] || "").sub(root_path, "").gsub(/\.(js|es6).*$/, "").sub(%r{^/}, "")
data = input[:data]
if should_transpile?(input[:filename])
data = transpile(data, root_path, logical_path)
end
data = transpile(data, root_path, logical_path) if should_transpile?(input[:filename])
# add sourceURL until we can do proper source maps
if !Rails.env.production? && !ember_cli?(input[:filename])
plugin_name = root_path[/\/plugins\/([\w-]+)\/assets/, 1]
source_url = if plugin_name
"plugins/#{plugin_name}/assets/javascripts/#{logical_path}"
else
logical_path
end
plugin_name = root_path[%r{/plugins/([\w-]+)/assets}, 1]
source_url =
if plugin_name
"plugins/#{plugin_name}/assets/javascripts/#{logical_path}"
else
logical_path
end
data = "eval(#{data.inspect} + \"\\n//# sourceURL=#{source_url}\");\n"
end
@ -62,7 +63,7 @@ class DiscourseJsProcessor
end
def self.should_transpile?(filename)
filename ||= ''
filename ||= ""
# skip ember cli
return false if ember_cli?(filename)
@ -73,7 +74,7 @@ class DiscourseJsProcessor
# For .js check the path...
return false unless filename.end_with?(".js") || filename.end_with?(".js.erb")
relative_path = filename.sub(Rails.root.to_s, '').sub(/^\/*/, '')
relative_path = filename.sub(Rails.root.to_s, "").sub(%r{^/*}, "")
js_root = "app/assets/javascripts"
test_root = "test/javascripts"
@ -81,26 +82,27 @@ class DiscourseJsProcessor
return false if relative_path.start_with?("#{js_root}/locales/")
return false if relative_path.start_with?("#{js_root}/plugins/")
return true if %w(
start-discourse
onpopstate-handler
google-tag-manager
google-universal-analytics-v3
google-universal-analytics-v4
activate-account
auto-redirect
embed-application
app-boot
).any? { |f| relative_path == "#{js_root}/#{f}.js" }
if %w[
start-discourse
onpopstate-handler
google-tag-manager
google-universal-analytics-v3
google-universal-analytics-v4
activate-account
auto-redirect
embed-application
app-boot
].any? { |f| relative_path == "#{js_root}/#{f}.js" }
return true
end
return true if plugin_transpile_paths.any? { |prefix| relative_path.start_with?(prefix) }
!!(relative_path =~ /^#{js_root}\/[^\/]+\// ||
relative_path =~ /^#{test_root}\/[^\/]+\//)
!!(relative_path =~ %r{^#{js_root}/[^/]+/} || relative_path =~ %r{^#{test_root}/[^/]+/})
end
def self.skip_module?(data)
!!(data.present? && data =~ /^\/\/ discourse-skip-module$/)
!!(data.present? && data =~ %r{^// discourse-skip-module$})
end
class Transpiler
@ -113,19 +115,17 @@ class DiscourseJsProcessor
def self.load_file_in_context(ctx, path, wrap_in_module: nil)
contents = File.read("#{Rails.root}/app/assets/javascripts/#{path}")
if wrap_in_module
contents = <<~JS
contents = <<~JS if wrap_in_module
define(#{wrap_in_module.to_json}, ["exports", "require", "module"], function(exports, require, module){
#{contents}
});
JS
end
ctx.eval(contents, filename: path)
end
def self.create_new_context
# timeout any eval that takes longer than 15 seconds
ctx = MiniRacer::Context.new(timeout: 15000, ensure_gc_after_idle: 2000)
ctx = MiniRacer::Context.new(timeout: 15_000, ensure_gc_after_idle: 2000)
# General shims
ctx.attach("rails.logger.info", proc { |err| Rails.logger.info(err.to_s) })
@ -158,10 +158,26 @@ class DiscourseJsProcessor
# Template Compiler
load_file_in_context(ctx, "node_modules/ember-source/dist/ember-template-compiler.js")
load_file_in_context(ctx, "node_modules/babel-plugin-ember-template-compilation/src/plugin.js", wrap_in_module: "babel-plugin-ember-template-compilation/index")
load_file_in_context(ctx, "node_modules/babel-plugin-ember-template-compilation/src/expression-parser.js", wrap_in_module: "babel-plugin-ember-template-compilation/expression-parser")
load_file_in_context(ctx, "node_modules/babel-import-util/src/index.js", wrap_in_module: "babel-import-util")
load_file_in_context(ctx, "node_modules/ember-cli-htmlbars/lib/colocated-babel-plugin.js", wrap_in_module: "colocated-babel-plugin")
load_file_in_context(
ctx,
"node_modules/babel-plugin-ember-template-compilation/src/plugin.js",
wrap_in_module: "babel-plugin-ember-template-compilation/index",
)
load_file_in_context(
ctx,
"node_modules/babel-plugin-ember-template-compilation/src/expression-parser.js",
wrap_in_module: "babel-plugin-ember-template-compilation/expression-parser",
)
load_file_in_context(
ctx,
"node_modules/babel-import-util/src/index.js",
wrap_in_module: "babel-import-util",
)
load_file_in_context(
ctx,
"node_modules/ember-cli-htmlbars/lib/colocated-babel-plugin.js",
wrap_in_module: "colocated-babel-plugin",
)
# Widget HBS compiler
widget_hbs_compiler_source = File.read("#{Rails.root}/lib/javascripts/widget-hbs-compiler.js")
@ -170,32 +186,44 @@ class DiscourseJsProcessor
#{widget_hbs_compiler_source}
});
JS
widget_hbs_compiler_transpiled = ctx.call("rawBabelTransform", widget_hbs_compiler_source, {
ast: false,
moduleId: 'widget-hbs-compiler',
plugins: DISCOURSE_COMMON_BABEL_PLUGINS
})
widget_hbs_compiler_transpiled =
ctx.call(
"rawBabelTransform",
widget_hbs_compiler_source,
{ ast: false, moduleId: "widget-hbs-compiler", plugins: DISCOURSE_COMMON_BABEL_PLUGINS },
)
ctx.eval(widget_hbs_compiler_transpiled, filename: "widget-hbs-compiler.js")
# Raw HBS compiler
load_file_in_context(ctx, "node_modules/handlebars/dist/handlebars.js", wrap_in_module: "handlebars")
raw_hbs_transpiled = ctx.call(
"rawBabelTransform",
File.read("#{Rails.root}/app/assets/javascripts/discourse-common/addon/lib/raw-handlebars.js"),
{
ast: false,
moduleId: "raw-handlebars",
plugins: [
['transform-modules-amd', { noInterop: true }],
*DISCOURSE_COMMON_BABEL_PLUGINS
]
}
load_file_in_context(
ctx,
"node_modules/handlebars/dist/handlebars.js",
wrap_in_module: "handlebars",
)
raw_hbs_transpiled =
ctx.call(
"rawBabelTransform",
File.read(
"#{Rails.root}/app/assets/javascripts/discourse-common/addon/lib/raw-handlebars.js",
),
{
ast: false,
moduleId: "raw-handlebars",
plugins: [
["transform-modules-amd", { noInterop: true }],
*DISCOURSE_COMMON_BABEL_PLUGINS,
],
},
)
ctx.eval(raw_hbs_transpiled, filename: "raw-handlebars.js")
# Theme template AST transformation plugins
load_file_in_context(ctx, "discourse-js-processor.js", wrap_in_module: "discourse-js-processor")
load_file_in_context(
ctx,
"discourse-js-processor.js",
wrap_in_module: "discourse-js-processor",
)
# Make interfaces available via `v8.call`
ctx.eval <<~JS
@ -262,10 +290,10 @@ class DiscourseJsProcessor
{
skip_module: @skip_module,
moduleId: module_name(root_path, logical_path),
filename: logical_path || 'unknown',
filename: logical_path || "unknown",
themeId: theme_id,
commonPlugins: DISCOURSE_COMMON_BABEL_PLUGINS
}
commonPlugins: DISCOURSE_COMMON_BABEL_PLUGINS,
},
)
end
@ -274,15 +302,16 @@ class DiscourseJsProcessor
root_base = File.basename(Rails.root)
# If the resource is a plugin, use the plugin name as a prefix
if root_path =~ /(.*\/#{root_base}\/plugins\/[^\/]+)\//
if root_path =~ %r{(.*/#{root_base}/plugins/[^/]+)/}
plugin_path = "#{Regexp.last_match[1]}/plugin.rb"
plugin = Discourse.plugins.find { |p| p.path == plugin_path }
path = "discourse/plugins/#{plugin.name}/#{logical_path.sub(/javascripts\//, '')}" if plugin
path =
"discourse/plugins/#{plugin.name}/#{logical_path.sub(%r{javascripts/}, "")}" if plugin
end
# We need to strip the app subdirectory to replicate how ember-cli works.
path || logical_path&.gsub('app/', '')&.gsub('addon/', '')&.gsub('admin/addon', 'admin')
path || logical_path&.gsub("app/", "")&.gsub("addon/", "")&.gsub("admin/addon", "admin")
end
def compile_raw_template(source, theme_id: nil)