Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add native golang formatter #388

Merged
merged 12 commits into from
Mar 10, 2020
Merged

Add native golang formatter #388

merged 12 commits into from
Mar 10, 2020

Conversation

sparkprime
Copy link
Contributor

No description provided.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.7%) to 77.956% when pulling 8de5a09 on sparkprime:formatter into 234b97c on google:master.

@coveralls
Copy link

coveralls commented Mar 5, 2020

Coverage Status

Coverage decreased (-0.7%) to 77.956% when pulling 5fc489f on sparkprime:formatter into 234b97c on google:master.

@sparkprime
Copy link
Contributor Author

Ok @sbarzowski this is clearly too big to review thoroughly but maybe you want to look at ast.go, the lexer and parser + the refactoring of cmd/jsonnet ? Maybe we want to make the linter also use that utility library?

@sbarzowski
Copy link
Collaborator

Sounds good. A common set of cmd utils is definitely a good idea. I did my first review pass. I'll take another look in the evening or tomorrow.

@@ -0,0 +1,305 @@
/*
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Putting this stuff under cmd/ is not ideal. I think this package should stay a thin wrapper. What about moving cmd/jsonnet/lib to internal/formatter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

pass/pass.go Outdated
limitations under the License.
*/

package pass
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that it's a separate package, but I think it should start as internal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

pass/pass.go Outdated
type Context interface{}

// CompilerPass is an interface for a pass that transforms the AST in some way.
type CompilerPass interface {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the word "compiler" here. There is no compiler :-). Maybe ASTPass?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

// }]
// The outer array can stay unexpanded, because there are no newlines between
// the square brackets and the braces.
type FixNewlines struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why export these passes if they're in the same package as Format? I think we should either:
a) Unexport these passes.
a) Extract each pass to its own internal-to-formatter package. They don't seem to depend (much) on jsonnetfmt.go, so perhaps that could work. I don't know if splitting stuff into tiny packages like that is actually a good idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now they're in internal, seems like this is OK.

cmd/utils.go Outdated
limitations under the License.
*/

package cmd
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's nice to have these utils extracted, but I would make it internal. Maybe cmd/internal/cmd/utils.go.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

cmd/utils.go Outdated

// HandleMemProfile creates a memory profile if requested by environment
// variable.
func HandleMemProfile() {
Copy link
Collaborator

@sbarzowski sbarzowski Mar 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Maybe CreateMemProfile, StartMemProfile or just MemProfile/ProfileMemory? Handle is almost meaningless.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

// This may emit a keyword or an identifier.
func (l *lexer) lexIdentifier() {
r := l.peek()
if !isIdentifierFirst(r) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: isIdentifier/isIdentifierFirst names are a bit confusing. I know they were there before this change, but perhaps we can rename them to something like isValidIdentifierChar / isValidIdentifierFirstChar? WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

ast/ast.go Outdated
@@ -43,6 +43,9 @@ type Node interface {
FreeVariables() Identifiers
SetFreeVariables(Identifiers)
SetContext(Context)
OpenFodderPtr() *Fodder
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This API with 3 functions is weird. Especially since modifying the fodder returned by OpenFodder is not safe - it still refers to the same memory chunk and the modification will be visible in the node.

I would have just one function OpenFodder here, which returns a pointer (like OpenFodderPtr currently).

(FYI I would also change FreeVariables and Context to just return a pointer if I was designing this interface again, but probably not worth it to break the API).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also add a comment explaining that it's a fodder going before the node and it makes sense to have it as part of the common Node interface, because you can put a comment before any expression.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -1353,27 +1355,28 @@ func (p *parser) parse(prec precedence) (ast.Node, errors.StaticError) {
// ---------------------------------------------------------------------------

// Parse parses a slice of tokens into a parse tree.
func Parse(t Tokens) (ast.Node, errors.StaticError) {
func Parse(t Tokens) (ast.Node, ast.Fodder, errors.StaticError) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's mention in this function's comment that it returns a trailing fodder going after the returned node.

Or maybe we should create a struct for the node and trailingFodder? It's kinda ugly to return them separately here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@sbarzowski
Copy link
Collaborator

I'm done with the review @sparkprime. Great stuff, thanks a lot for getting this done :-).

@sparkprime sparkprime merged commit 724650d into google:master Mar 10, 2020
@sh0rez
Copy link
Contributor

sh0rez commented Mar 11, 2020

This is so awesome!! Any way to use it from go directly? Would love to ship it as part of Tanka

@sparkprime
Copy link
Contributor Author

Yes we should probably provide a public API for the top level functionality. Take a look at internal/formatter which exposes formatter.Format and give us feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants