From 88669fd04af506f64bd21dd59c30ce99b36425fa Mon Sep 17 00:00:00 2001 From: Andrew Z Allen Date: Sun, 10 Mar 2019 19:34:00 -0600 Subject: [PATCH] Respond to reviewer comments --- BUILD.bazel | 3 +- gazelle/parser/at.go | 23 -------- gazelle/parser/function.go | 16 ------ gazelle/{ => sass}/BUILD.bazel | 6 +-- gazelle/{ => sass}/config.go | 2 +- gazelle/{ => sass}/fileinfo.go | 18 ++++--- gazelle/{ => sass}/fileinfo_test.go | 11 ++-- gazelle/{ => sass}/gazellebinary_test.go | 23 ++++---- .../{ => sass/internal}/parser/BUILD.bazel | 0 gazelle/sass/internal/parser/at.go | 37 +++++++++++++ gazelle/{ => sass/internal}/parser/comment.go | 14 +++++ gazelle/{ => sass/internal}/parser/digit.go | 14 +++++ gazelle/sass/internal/parser/function.go | 30 +++++++++++ gazelle/{ => sass/internal}/parser/hash.go | 14 +++++ gazelle/{ => sass/internal}/parser/ident.go | 14 +++++ gazelle/{ => sass/internal}/parser/string.go | 14 +++++ gazelle/{ => sass/internal}/parser/symbol.go | 14 +++++ .../internal}/parser/testdata/1.in.scss | 0 .../internal}/parser/testdata/1.out.txt | 0 .../parser/testdata/14_imports_input.in.scss | 0 .../parser/testdata/14_imports_input.out.txt | 0 .../{ => sass/internal}/parser/tokenizer.go | 20 ++++--- .../internal}/parser/tokenizer_test.go | 16 +++++- .../{ => sass/internal}/parser/whitespace.go | 14 +++++ gazelle/{ => sass}/resolver.go | 2 +- gazelle/{ => sass}/sass.go | 52 ++++++++++++------- gazelle/{ => sass}/testdata/.gitignore | 0 gazelle/{ => sass}/testdata/BUILD.bazel | 2 +- gazelle/{ => sass}/testdata/README.md | 0 .../{ => sass}/testdata/comments/README.md | 0 .../{ => sass}/testdata/comments/WORKSPACE | 0 .../comments/hello_world/BUILD.bazel.out | 0 .../testdata/comments/hello_world/main.scss | 0 .../testdata/comments/shared/BUILD.bazel.out | 0 .../testdata/comments/shared/_colors.scss | 0 .../testdata/comments/shared/_fonts.scss | 0 gazelle/{ => sass}/testdata/simple/README.md | 0 gazelle/{ => sass}/testdata/simple/WORKSPACE | 0 .../simple/hello_world/BUILD.bazel.out | 0 .../testdata/simple/hello_world/main.scss | 0 .../testdata/simple/repeating/BUILD.bazel.out | 0 .../testdata/simple/repeating/main.scss | 0 .../testdata/simple/shared/BUILD.bazel.out | 0 .../testdata/simple/shared/_colors.scss | 0 .../testdata/simple/shared/_fonts.scss | 0 45 files changed, 265 insertions(+), 94 deletions(-) delete mode 100644 gazelle/parser/at.go delete mode 100644 gazelle/parser/function.go rename gazelle/{ => sass}/BUILD.bazel (92%) rename gazelle/{ => sass}/config.go (99%) rename gazelle/{ => sass}/fileinfo.go (89%) rename gazelle/{ => sass}/fileinfo_test.go (89%) rename gazelle/{ => sass}/gazellebinary_test.go (86%) rename gazelle/{ => sass/internal}/parser/BUILD.bazel (100%) create mode 100644 gazelle/sass/internal/parser/at.go rename gazelle/{ => sass/internal}/parser/comment.go (79%) rename gazelle/{ => sass/internal}/parser/digit.go (88%) create mode 100644 gazelle/sass/internal/parser/function.go rename gazelle/{ => sass/internal}/parser/hash.go (56%) rename gazelle/{ => sass/internal}/parser/ident.go (91%) rename gazelle/{ => sass/internal}/parser/string.go (78%) rename gazelle/{ => sass/internal}/parser/symbol.go (81%) rename gazelle/{ => sass/internal}/parser/testdata/1.in.scss (100%) rename gazelle/{ => sass/internal}/parser/testdata/1.out.txt (100%) rename gazelle/{ => sass/internal}/parser/testdata/14_imports_input.in.scss (100%) rename gazelle/{ => sass/internal}/parser/testdata/14_imports_input.out.txt (100%) rename gazelle/{ => sass/internal}/parser/tokenizer.go (96%) rename gazelle/{ => sass/internal}/parser/tokenizer_test.go (94%) rename gazelle/{ => sass/internal}/parser/whitespace.go (69%) rename gazelle/{ => sass}/resolver.go (99%) rename gazelle/{ => sass}/sass.go (74%) rename gazelle/{ => sass}/testdata/.gitignore (100%) rename gazelle/{ => sass}/testdata/BUILD.bazel (64%) rename gazelle/{ => sass}/testdata/README.md (100%) rename gazelle/{ => sass}/testdata/comments/README.md (100%) rename gazelle/{ => sass}/testdata/comments/WORKSPACE (100%) rename gazelle/{ => sass}/testdata/comments/hello_world/BUILD.bazel.out (100%) rename gazelle/{ => sass}/testdata/comments/hello_world/main.scss (100%) rename gazelle/{ => sass}/testdata/comments/shared/BUILD.bazel.out (100%) rename gazelle/{ => sass}/testdata/comments/shared/_colors.scss (100%) rename gazelle/{ => sass}/testdata/comments/shared/_fonts.scss (100%) rename gazelle/{ => sass}/testdata/simple/README.md (100%) rename gazelle/{ => sass}/testdata/simple/WORKSPACE (100%) rename gazelle/{ => sass}/testdata/simple/hello_world/BUILD.bazel.out (100%) rename gazelle/{ => sass}/testdata/simple/hello_world/main.scss (100%) rename gazelle/{ => sass}/testdata/simple/repeating/BUILD.bazel.out (100%) rename gazelle/{ => sass}/testdata/simple/repeating/main.scss (100%) rename gazelle/{ => sass}/testdata/simple/shared/BUILD.bazel.out (100%) rename gazelle/{ => sass}/testdata/simple/shared/_colors.scss (100%) rename gazelle/{ => sass}/testdata/simple/shared/_fonts.scss (100%) diff --git a/BUILD.bazel b/BUILD.bazel index a7e7bbf..4990466 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -28,7 +28,8 @@ gazelle( gazelle_binary( name = "gazelle_sass", languages = DEFAULT_LANGUAGES + [ - "@io_bazel_rules_sass//gazelle:go_default_library", + "@io_bazel_rules_sass//gazelle/sass:go_default_library", ], + tags = ["renovatebot"], visibility = ["//visibility:private"], ) diff --git a/gazelle/parser/at.go b/gazelle/parser/at.go deleted file mode 100644 index cc73c4e..0000000 --- a/gazelle/parser/at.go +++ /dev/null @@ -1,23 +0,0 @@ -package parser - -import ( - "fmt" -) - -// At is an at keyword token. -type At struct { - // The kind of at token. - Ident IdentLike -} - -func (a *At) String() string { - return fmt.Sprintf("", a.Ident.String()) -} -func (a *At) Type() string { return "At" } - -var _ Token = &At{} - -func (s *Scanner) scanAt() *At { - // Create a buffer and read the current character into it. - return &At{Ident: s.scanIdent()} -} diff --git a/gazelle/parser/function.go b/gazelle/parser/function.go deleted file mode 100644 index d87ff3f..0000000 --- a/gazelle/parser/function.go +++ /dev/null @@ -1,16 +0,0 @@ -package parser - -import "fmt" - -// Function is a function token. -type Function struct { - // The text inside of the comment. - Value string -} - -func (f *Function) String() string { - return fmt.Sprintf("", f.Value) -} -func (_ *Function) Type() string { return "Function" } - -var _ Token = &Function{} diff --git a/gazelle/BUILD.bazel b/gazelle/sass/BUILD.bazel similarity index 92% rename from gazelle/BUILD.bazel rename to gazelle/sass/BUILD.bazel index 0292848..a495d66 100644 --- a/gazelle/BUILD.bazel +++ b/gazelle/sass/BUILD.bazel @@ -30,7 +30,7 @@ go_library( importpath = "github.com/bazelbuild/rules_sass/gazelle", visibility = ["//visibility:public"], deps = [ - "//gazelle/parser:go_default_library", + "//gazelle/sass/internal/parser:go_default_library", "@bazel_gazelle//config:go_default_library", "@bazel_gazelle//label:go_default_library", "@bazel_gazelle//language:go_default_library", @@ -49,7 +49,7 @@ go_test( args = ["-gazelle=$(location :gazelle_sass)"], data = [ ":gazelle_sass", - "//gazelle/testdata", + "//gazelle/sass/testdata", ], embed = [":go_default_library"], importpath = "github.com/bazelbuild/rules_sass/gazelle", @@ -64,6 +64,6 @@ gazelle_binary( name = "gazelle_sass", # keep languages = [ - "@io_bazel_rules_sass//gazelle:go_default_library", + "@io_bazel_rules_sass//gazelle/sass:go_default_library", ], ) diff --git a/gazelle/config.go b/gazelle/sass/config.go similarity index 99% rename from gazelle/config.go rename to gazelle/sass/config.go index cda0904..15b0125 100644 --- a/gazelle/config.go +++ b/gazelle/sass/config.go @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package gazelle +package sass import ( "flag" diff --git a/gazelle/fileinfo.go b/gazelle/sass/fileinfo.go similarity index 89% rename from gazelle/fileinfo.go rename to gazelle/sass/fileinfo.go index 56a7837..40a458e 100644 --- a/gazelle/fileinfo.go +++ b/gazelle/sass/fileinfo.go @@ -13,11 +13,10 @@ See the License for the specific language governing permissions and limitations under the License. */ -package gazelle +package sass import ( "fmt" - "log" "os" "path/filepath" "sort" @@ -30,12 +29,15 @@ type FileInfo struct { Path, Name string Imports []string + + Errors []string } // sassFileInfo takes a dir and file name and parses the .sass file into // the constituent components, extracting metadata like the set of -// imports that it has. -func sassFileInfo(dir, name string) FileInfo { +// imports that it has. Returns an error object for all errors, if the error is +// fatal FileInfo will be nil. +func sassFileInfo(dir, name string) (FileInfo, error) { info := FileInfo{ Path: filepath.Join(dir, name), Name: name, @@ -43,9 +45,9 @@ func sassFileInfo(dir, name string) FileInfo { file, err := os.Open(filepath.Join(dir, name)) if err != nil { - log.Printf("%s: error reading sass file: %v", info.Path, err) - return info + return info, err } + defer file.Close() s := parser.New(file) @@ -64,7 +66,7 @@ func sassFileInfo(dir, name string) FileInfo { parseError := func(msg string, args ...interface{}) { // Prepend the file.Name() so we tell the user which file had the parse error. args = append([]interface{}{file.Name()}, args...) - fmt.Fprintf(os.Stderr, "%s: "+msg+"\n", args...) + info.Errors = append(info.Errors, fmt.Sprintf("%s: "+msg+"\n", args...)) } for t := scan(); t.Type() != "EOF"; t = scan() { @@ -117,5 +119,5 @@ func sassFileInfo(dir, name string) FileInfo { sort.Strings(info.Imports) - return info + return info, nil } diff --git a/gazelle/fileinfo_test.go b/gazelle/sass/fileinfo_test.go similarity index 89% rename from gazelle/fileinfo_test.go rename to gazelle/sass/fileinfo_test.go index 9470fa8..cb0867d 100644 --- a/gazelle/fileinfo_test.go +++ b/gazelle/sass/fileinfo_test.go @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package gazelle +package sass import ( "io/ioutil" @@ -77,14 +77,17 @@ func TestFileInfo(t *testing.T) { t.Fatal(err) } - got := sassFileInfo(dir, tc.name) + got, err := sassFileInfo(dir, tc.name) + if err != nil { + t.Fatal(err) + } - // Reexpose the fields we care bout for testing. + // Clear the fields we don't care about for testing. got = FileInfo{ Imports: got.Imports, } if !reflect.DeepEqual(got, tc.want) { - t.Errorf("Inequalith.\ngot %#v;\nwant %#v", got, tc.want) + t.Errorf("FileInfo don't match expectations.\ngot %#v;\nwant %#v", got, tc.want) } }) } diff --git a/gazelle/gazellebinary_test.go b/gazelle/sass/gazellebinary_test.go similarity index 86% rename from gazelle/gazellebinary_test.go rename to gazelle/sass/gazellebinary_test.go index c8c1415..6c5a496 100644 --- a/gazelle/gazellebinary_test.go +++ b/gazelle/sass/gazellebinary_test.go @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package gazelle +package sass import ( "flag" @@ -42,9 +42,6 @@ func runGazelle(dir string) error { err := cmd.Run() - // Flush stdout/stderr since this makes log output show up more often. - os.Stdout.Sync() - os.Stderr.Sync() return err } @@ -102,8 +99,9 @@ func TestTestdata(t *testing.T) { } err := filepath.Walk(testSuitePath, func(path string, info os.FileInfo, err error) error { - // There is no need to process directories. Skip them. - if info.IsDir() { + // There is no need to process directories or if the input path is + // already an error condition. Skip these cases. + if err != nil || info.IsDir() { return nil } @@ -112,14 +110,19 @@ func TestTestdata(t *testing.T) { return fmt.Errorf("Unable to read file %q. Err: %v", path, err) } - // content is a []byte not a string so it has to be typecast and we can't define the filespec at the beginning. - fileSpec := testtools.FileSpec{Path: strings.TrimPrefix(path, testSuitePath), Content: string(content)} + // By contract errors can't happen since we are finding the relative + // path of a file inside the path that we are walking. + relPath, _ := filepath.Rel(testSuitePath, path) + + // content is a []byte not a string so it has to be typecast and we + // can't define the filespec at the beginning. + fileSpec := testtools.FileSpec{Path: relPath, Content: string(content)} if strings.HasSuffix(path, "/BUILD.bazel.in") { - fileSpec.Path = strings.TrimSuffix(strings.TrimPrefix(path, testSuitePath), "BUILD.bazel.in") + "BUILD.bazel" + fileSpec.Path = strings.TrimSuffix(relPath, ".in") files = append(files, fileSpec) } else if strings.HasSuffix(path, "/BUILD.bazel.out") { - fileSpec.Path = strings.TrimSuffix(strings.TrimPrefix(path, testSuitePath), "BUILD.bazel.out") + "BUILD.bazel" + fileSpec.Path = strings.TrimSuffix(relPath, ".out") want = append(want, fileSpec) } else { files = append(files, fileSpec) diff --git a/gazelle/parser/BUILD.bazel b/gazelle/sass/internal/parser/BUILD.bazel similarity index 100% rename from gazelle/parser/BUILD.bazel rename to gazelle/sass/internal/parser/BUILD.bazel diff --git a/gazelle/sass/internal/parser/at.go b/gazelle/sass/internal/parser/at.go new file mode 100644 index 0000000..f1563db --- /dev/null +++ b/gazelle/sass/internal/parser/at.go @@ -0,0 +1,37 @@ +/* Copyright 2019 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package parser + +import ( + "fmt" +) + +// At is an at keyword token. +type At struct { + // The kind of at token. + Ident IdentLike +} + +func (a *At) String() string { + return fmt.Sprintf("", a.Ident.String()) +} +func (a *At) Type() string { return "At" } + +var _ Token = &At{} + +func (s *Scanner) scanAt() *At { + // Create a buffer and read the current character into it. + return &At{Ident: s.scanIdent()} +} diff --git a/gazelle/parser/comment.go b/gazelle/sass/internal/parser/comment.go similarity index 79% rename from gazelle/parser/comment.go rename to gazelle/sass/internal/parser/comment.go index 0001254..2307537 100644 --- a/gazelle/parser/comment.go +++ b/gazelle/sass/internal/parser/comment.go @@ -1,3 +1,17 @@ +/* Copyright 2019 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ package parser import ( diff --git a/gazelle/parser/digit.go b/gazelle/sass/internal/parser/digit.go similarity index 88% rename from gazelle/parser/digit.go rename to gazelle/sass/internal/parser/digit.go index 1710a7f..1cfa306 100644 --- a/gazelle/parser/digit.go +++ b/gazelle/sass/internal/parser/digit.go @@ -1,3 +1,17 @@ +/* Copyright 2019 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ package parser import ( diff --git a/gazelle/sass/internal/parser/function.go b/gazelle/sass/internal/parser/function.go new file mode 100644 index 0000000..19e0263 --- /dev/null +++ b/gazelle/sass/internal/parser/function.go @@ -0,0 +1,30 @@ +/* Copyright 2019 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package parser + +import "fmt" + +// Function is a function token. +type Function struct { + // The text inside of the comment. + Value string +} + +func (f *Function) String() string { + return fmt.Sprintf("", f.Value) +} +func (_ *Function) Type() string { return "Function" } + +var _ Token = &Function{} diff --git a/gazelle/parser/hash.go b/gazelle/sass/internal/parser/hash.go similarity index 56% rename from gazelle/parser/hash.go rename to gazelle/sass/internal/parser/hash.go index 4bef325..902ad91 100644 --- a/gazelle/parser/hash.go +++ b/gazelle/sass/internal/parser/hash.go @@ -1,3 +1,17 @@ +/* Copyright 2019 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ package parser import ( diff --git a/gazelle/parser/ident.go b/gazelle/sass/internal/parser/ident.go similarity index 91% rename from gazelle/parser/ident.go rename to gazelle/sass/internal/parser/ident.go index 2c9a55e..abdf34a 100644 --- a/gazelle/parser/ident.go +++ b/gazelle/sass/internal/parser/ident.go @@ -1,3 +1,17 @@ +/* Copyright 2019 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ package parser import ( diff --git a/gazelle/parser/string.go b/gazelle/sass/internal/parser/string.go similarity index 78% rename from gazelle/parser/string.go rename to gazelle/sass/internal/parser/string.go index 4f01f77..614f245 100644 --- a/gazelle/parser/string.go +++ b/gazelle/sass/internal/parser/string.go @@ -1,3 +1,17 @@ +/* Copyright 2019 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ package parser import ( diff --git a/gazelle/parser/symbol.go b/gazelle/sass/internal/parser/symbol.go similarity index 81% rename from gazelle/parser/symbol.go rename to gazelle/sass/internal/parser/symbol.go index e1fb029..7acaea7 100644 --- a/gazelle/parser/symbol.go +++ b/gazelle/sass/internal/parser/symbol.go @@ -1,3 +1,17 @@ +/* Copyright 2019 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ package parser type LeftParenthesis struct{} diff --git a/gazelle/parser/testdata/1.in.scss b/gazelle/sass/internal/parser/testdata/1.in.scss similarity index 100% rename from gazelle/parser/testdata/1.in.scss rename to gazelle/sass/internal/parser/testdata/1.in.scss diff --git a/gazelle/parser/testdata/1.out.txt b/gazelle/sass/internal/parser/testdata/1.out.txt similarity index 100% rename from gazelle/parser/testdata/1.out.txt rename to gazelle/sass/internal/parser/testdata/1.out.txt diff --git a/gazelle/parser/testdata/14_imports_input.in.scss b/gazelle/sass/internal/parser/testdata/14_imports_input.in.scss similarity index 100% rename from gazelle/parser/testdata/14_imports_input.in.scss rename to gazelle/sass/internal/parser/testdata/14_imports_input.in.scss diff --git a/gazelle/parser/testdata/14_imports_input.out.txt b/gazelle/sass/internal/parser/testdata/14_imports_input.out.txt similarity index 100% rename from gazelle/parser/testdata/14_imports_input.out.txt rename to gazelle/sass/internal/parser/testdata/14_imports_input.out.txt diff --git a/gazelle/parser/tokenizer.go b/gazelle/sass/internal/parser/tokenizer.go similarity index 96% rename from gazelle/parser/tokenizer.go rename to gazelle/sass/internal/parser/tokenizer.go index 193fbd9..32295c9 100644 --- a/gazelle/parser/tokenizer.go +++ b/gazelle/sass/internal/parser/tokenizer.go @@ -1,3 +1,17 @@ +/* Copyright 2019 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ // Package parser provides utilities useful for parsing a sass file. package parser @@ -71,12 +85,6 @@ func (s *Scanner) peek() rune { return eof } -/* peek gives you the next rune. -func (s *Scanner) peek() rune { - r, _ := utf8.DecodeRuneInString(s.peekN(1)) - return r -}*/ - // peek gives you the offsetth rune. func (s *Scanner) peekOffset(offset int) rune { if r := []rune(s.peekNOffset(1, offset)); len(r) == 1 { diff --git a/gazelle/parser/tokenizer_test.go b/gazelle/sass/internal/parser/tokenizer_test.go similarity index 94% rename from gazelle/parser/tokenizer_test.go rename to gazelle/sass/internal/parser/tokenizer_test.go index fa1dfcc..1df8676 100644 --- a/gazelle/parser/tokenizer_test.go +++ b/gazelle/sass/internal/parser/tokenizer_test.go @@ -1,3 +1,17 @@ +/* Copyright 2019 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ package parser import ( @@ -619,7 +633,7 @@ func TestScanNumber(t *testing.T) { func TestLargeInputs(t *testing.T) { found := false - testDataDir, err := bazel.Runfile(filepath.Join("gazelle", "parser", "testdata")) + testDataDir, err := bazel.Runfile(filepath.Join("gazelle", "sass", "internal", "parser", "testdata")) if err != nil { t.Fatalf("Error finding runfile gazelle/parser/testdata: %s", err) } diff --git a/gazelle/parser/whitespace.go b/gazelle/sass/internal/parser/whitespace.go similarity index 69% rename from gazelle/parser/whitespace.go rename to gazelle/sass/internal/parser/whitespace.go index 62e245f..29ca026 100644 --- a/gazelle/parser/whitespace.go +++ b/gazelle/sass/internal/parser/whitespace.go @@ -1,3 +1,17 @@ +/* Copyright 2019 The Bazel Authors. All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ package parser import ( diff --git a/gazelle/resolver.go b/gazelle/sass/resolver.go similarity index 99% rename from gazelle/resolver.go rename to gazelle/sass/resolver.go index f44bbce..df1e91a 100644 --- a/gazelle/resolver.go +++ b/gazelle/sass/resolver.go @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package gazelle +package sass import ( "errors" diff --git a/gazelle/sass.go b/gazelle/sass/sass.go similarity index 74% rename from gazelle/sass.go rename to gazelle/sass/sass.go index 88e981f..b2291bb 100644 --- a/gazelle/sass.go +++ b/gazelle/sass/sass.go @@ -13,14 +13,14 @@ See the License for the specific language governing permissions and limitations under the License. */ -// gazelle provides a minimal implementation of language.Language for +// This package provides a minimal implementation of language.Language for // rules_sass. -package gazelle +package sass import ( "fmt" - "io/ioutil" - "path/filepath" + "log" + "path" "strings" "github.com/bazelbuild/bazel-gazelle/config" @@ -43,7 +43,7 @@ func NewLanguage() language.Language { func (s *sasslang) Kinds() map[string]rule.KindInfo { return map[string]rule.KindInfo{ "sass_library": { - MatchAny: true, + MatchAny: false, NonEmptyAttrs: map[string]bool{ "srcs": true, }, @@ -94,29 +94,39 @@ func (s *sasslang) Loads() []rule.LoadInfo { // Any non-fatal errors this function encounters should be logged using // log.Print. func (s *sasslang) GenerateRules(args language.GenerateArgs) language.GenerateResult { - files, err := ioutil.ReadDir(args.Dir) - if err != nil { - panic(err) - } - // base is the last part of the path for this element. For example: // "hello_world" => "hello_world" // "foo/bar" => "bar" - base := filepath.Base(args.Rel) + base := path.Base(args.Rel) + if base == "." { + //args.Rel will return an empty string if you're in the root of the repo. + //This will then be translated into "." by path.Base which is not a valid + //name for a target. Therefore we will use the name of "root" just to have + //something that is valid. If the user doesn't want the target to be named + // `//:root`, then they can rename it and on the next generation it will + // persist the user supplied name. + base = "root" + } rules := []*rule.Rule{} imports := []interface{}{} var normalFiles []string - for _, file := range files { - f := file.Name() - + for _, f := range append(args.RegularFiles, args.GenFiles...) { // Only generate Sass entries for sass files (.scss/.sass) if !strings.HasSuffix(f, ".sass") && !strings.HasSuffix(f, ".scss") { continue } - fileInfo := sassFileInfo(args.Dir, f) + fileInfo, err := sassFileInfo(args.Dir, f) + if err != nil { + panic(fmt.Sprintf("Fatal error")) + } + + for err := range fileInfo.Errors { + log.Printf("Error parsing %s: %s\n", fileInfo.Name, err) + } + imports = append(imports, fileInfo.Imports) // The primary entrypoint on Sass is a main.scss file. @@ -125,10 +135,9 @@ func (s *sasslang) GenerateRules(args language.GenerateArgs) language.GenerateRe rule.SetAttr("src", "main.scss") rules = append(rules, rule) - } else if strings.HasPrefix(filepath.Base(f), "_") { + } else if strings.HasPrefix(path.Base(f), "_") { // Libraries in Sass have filenames that start with _. - base = filepath.Base(f) - + // For each file in the dir with a leading "_" create a new sass_library rule := rule.NewRule("sass_library", base[1:len(base)-5]) rule.SetAttr("srcs", []string{base}) @@ -151,10 +160,15 @@ func (s *sasslang) GenerateRules(args language.GenerateArgs) language.GenerateRe rules = append(rules, rule) } - // For each file in the dir with a leading "_" create a new sass_library return language.GenerateResult{ Gen: rules, Imports: imports, + // Empty is a list of rules that cannot be built with the files found in the + // directory GenerateRules was asked to process. These will be merged with + // existing rules. If ther merged rules are empty, they will be deleted. + // In order to keep the BUILD file clean, if no file is included in the + // default rule for this directory, then remove it. + Empty: []*rule.Rule{rule.NewRule("sass_library", base)}, } } diff --git a/gazelle/testdata/.gitignore b/gazelle/sass/testdata/.gitignore similarity index 100% rename from gazelle/testdata/.gitignore rename to gazelle/sass/testdata/.gitignore diff --git a/gazelle/testdata/BUILD.bazel b/gazelle/sass/testdata/BUILD.bazel similarity index 64% rename from gazelle/testdata/BUILD.bazel rename to gazelle/sass/testdata/BUILD.bazel index 071ab4e..43e493b 100644 --- a/gazelle/testdata/BUILD.bazel +++ b/gazelle/sass/testdata/BUILD.bazel @@ -3,5 +3,5 @@ filegroup( name = "testdata", srcs = glob(["**"]), - visibility = ["//gazelle:__pkg__"], + visibility = ["//gazelle/sass:__pkg__"], ) diff --git a/gazelle/testdata/README.md b/gazelle/sass/testdata/README.md similarity index 100% rename from gazelle/testdata/README.md rename to gazelle/sass/testdata/README.md diff --git a/gazelle/testdata/comments/README.md b/gazelle/sass/testdata/comments/README.md similarity index 100% rename from gazelle/testdata/comments/README.md rename to gazelle/sass/testdata/comments/README.md diff --git a/gazelle/testdata/comments/WORKSPACE b/gazelle/sass/testdata/comments/WORKSPACE similarity index 100% rename from gazelle/testdata/comments/WORKSPACE rename to gazelle/sass/testdata/comments/WORKSPACE diff --git a/gazelle/testdata/comments/hello_world/BUILD.bazel.out b/gazelle/sass/testdata/comments/hello_world/BUILD.bazel.out similarity index 100% rename from gazelle/testdata/comments/hello_world/BUILD.bazel.out rename to gazelle/sass/testdata/comments/hello_world/BUILD.bazel.out diff --git a/gazelle/testdata/comments/hello_world/main.scss b/gazelle/sass/testdata/comments/hello_world/main.scss similarity index 100% rename from gazelle/testdata/comments/hello_world/main.scss rename to gazelle/sass/testdata/comments/hello_world/main.scss diff --git a/gazelle/testdata/comments/shared/BUILD.bazel.out b/gazelle/sass/testdata/comments/shared/BUILD.bazel.out similarity index 100% rename from gazelle/testdata/comments/shared/BUILD.bazel.out rename to gazelle/sass/testdata/comments/shared/BUILD.bazel.out diff --git a/gazelle/testdata/comments/shared/_colors.scss b/gazelle/sass/testdata/comments/shared/_colors.scss similarity index 100% rename from gazelle/testdata/comments/shared/_colors.scss rename to gazelle/sass/testdata/comments/shared/_colors.scss diff --git a/gazelle/testdata/comments/shared/_fonts.scss b/gazelle/sass/testdata/comments/shared/_fonts.scss similarity index 100% rename from gazelle/testdata/comments/shared/_fonts.scss rename to gazelle/sass/testdata/comments/shared/_fonts.scss diff --git a/gazelle/testdata/simple/README.md b/gazelle/sass/testdata/simple/README.md similarity index 100% rename from gazelle/testdata/simple/README.md rename to gazelle/sass/testdata/simple/README.md diff --git a/gazelle/testdata/simple/WORKSPACE b/gazelle/sass/testdata/simple/WORKSPACE similarity index 100% rename from gazelle/testdata/simple/WORKSPACE rename to gazelle/sass/testdata/simple/WORKSPACE diff --git a/gazelle/testdata/simple/hello_world/BUILD.bazel.out b/gazelle/sass/testdata/simple/hello_world/BUILD.bazel.out similarity index 100% rename from gazelle/testdata/simple/hello_world/BUILD.bazel.out rename to gazelle/sass/testdata/simple/hello_world/BUILD.bazel.out diff --git a/gazelle/testdata/simple/hello_world/main.scss b/gazelle/sass/testdata/simple/hello_world/main.scss similarity index 100% rename from gazelle/testdata/simple/hello_world/main.scss rename to gazelle/sass/testdata/simple/hello_world/main.scss diff --git a/gazelle/testdata/simple/repeating/BUILD.bazel.out b/gazelle/sass/testdata/simple/repeating/BUILD.bazel.out similarity index 100% rename from gazelle/testdata/simple/repeating/BUILD.bazel.out rename to gazelle/sass/testdata/simple/repeating/BUILD.bazel.out diff --git a/gazelle/testdata/simple/repeating/main.scss b/gazelle/sass/testdata/simple/repeating/main.scss similarity index 100% rename from gazelle/testdata/simple/repeating/main.scss rename to gazelle/sass/testdata/simple/repeating/main.scss diff --git a/gazelle/testdata/simple/shared/BUILD.bazel.out b/gazelle/sass/testdata/simple/shared/BUILD.bazel.out similarity index 100% rename from gazelle/testdata/simple/shared/BUILD.bazel.out rename to gazelle/sass/testdata/simple/shared/BUILD.bazel.out diff --git a/gazelle/testdata/simple/shared/_colors.scss b/gazelle/sass/testdata/simple/shared/_colors.scss similarity index 100% rename from gazelle/testdata/simple/shared/_colors.scss rename to gazelle/sass/testdata/simple/shared/_colors.scss diff --git a/gazelle/testdata/simple/shared/_fonts.scss b/gazelle/sass/testdata/simple/shared/_fonts.scss similarity index 100% rename from gazelle/testdata/simple/shared/_fonts.scss rename to gazelle/sass/testdata/simple/shared/_fonts.scss