Skip to content

Commit 4bace9c

Browse files
authored
Merge pull request #609 from alphagov/improve-packagecomponent
Make PackageContents regex more robust
2 parents 9ccbc7e + c10b5c5 commit 4bace9c

2 files changed

Lines changed: 58 additions & 42 deletions

File tree

lib/package_contents.rb

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
module PackageContents
2+
SASS_IMPORT_PATTERN = %r{
3+
^\s* # leading whitespace
4+
@(?:import|use)\s+ # @import or @use
5+
["'] # opening quote
6+
([a-z-]+) # capture the name
7+
(?:/(?:_?index) # optional /index or /_index
8+
(?:\.scss)? # optional .scss extension
9+
)?
10+
["'] # closing quote
11+
}x
12+
213
def components
3-
extract_from "components/_index.scss" do |line|
4-
# Match anything found between `@import "` and `/index";`
5-
line[/(?<=@import ")[a-z-]*(?=\/index";)/]
6-
end
14+
extract_from "components/_index.scss"
715
end
816

917
def overrides
10-
extract_from "overrides/_index.scss" do |line|
11-
# Match anything found between `@import "` and `";`
12-
line[/(?<=@import ")[a-z-]*(?=";)/]
13-
end
18+
extract_from "overrides/_index.scss"
1419
end
1520

16-
def extract_from(file, &block)
21+
def extract_from(file, pattern: SASS_IMPORT_PATTERN)
1722
File.readlines("node_modules/govuk-frontend/dist/govuk/#{file}")
18-
.map(&block)
19-
.compact
23+
.filter_map { |line| line[pattern, 1] }
2024
.tap { |list| raise "No matches found in #{file}" if list.empty? }
2125
end
2226
end

spec/package_contents_spec.rb

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,55 +9,67 @@ class Test
99
@helper = Test.new
1010
end
1111

12-
describe "#components" do
13-
it "extracts components from components/_index.scss" do
12+
shared_examples "a sass import extractor" do |method, file|
13+
it "extracts names via @import" do
1414
allow(File).to receive(:readlines).and_return <<~FILE.split("\n")
1515
@import "../base";
1616
17-
@import "first-component/index";
18-
@import "second-component/index";
19-
@import "third-component/index";
17+
@import "first";
18+
@import "second/index";
19+
@import "th-ird/_index";
20+
@import "fourth/index.scss";
21+
@import 'fifth/_index.scss';
22+
// @import "false-match/index";
23+
@import "";
2024
FILE
2125

22-
expect(@helper.components).to eq(%w[
23-
first-component
24-
second-component
25-
third-component
26+
expect(@helper.send(method)).to eq(%w[
27+
first
28+
second
29+
th-ird
30+
fourth
31+
fifth
2632
])
2733
end
2834

29-
it "throws if no components are found in components/_index.scss" do
30-
allow(File).to receive(:readlines).and_return []
31-
32-
expect { @helper.components }.to raise_error(
33-
"No matches found in components/_index.scss",
34-
)
35-
end
36-
end
37-
38-
describe "#overrides" do
39-
it "extracts overrides from overrides/_index.scss" do
35+
it "extracts names via @use" do
4036
allow(File).to receive(:readlines).and_return <<~FILE.split("\n")
41-
@import "../a-dependency";
37+
@use "../base";
4238
43-
@import "one";
44-
@import "two";
45-
@import "th-ree";
39+
@use "first";
40+
@use "second/index";
41+
@use "th-ird/_index";
42+
@use "fourth/index.scss";
43+
@use 'fifth/_index.scss';
44+
@use "sixth" with ($config: true);
45+
// @use "false-match/index";
46+
@use "";
4647
FILE
4748

48-
expect(@helper.overrides).to eq(%w[
49-
one
50-
two
51-
th-ree
49+
expect(@helper.send(method)).to eq(%w[
50+
first
51+
second
52+
th-ird
53+
fourth
54+
fifth
55+
sixth
5256
])
5357
end
5458

55-
it "throws if no overrides are found in components/_index.scss" do
59+
it "throws if no matches are found" do
5660
allow(File).to receive(:readlines).and_return []
5761

58-
expect { @helper.overrides }.to raise_error(
59-
"No matches found in overrides/_index.scss",
62+
expect { @helper.send(method) }.to raise_error(
63+
"No matches found in #{file}",
6064
)
6165
end
6266
end
67+
68+
describe "#components" do
69+
it_behaves_like "a sass import extractor", :components, "components/_index.scss"
70+
end
71+
72+
describe "#overrides" do
73+
it_behaves_like "a sass import extractor", :overrides, "overrides/_index.scss"
74+
end
6375
end

0 commit comments

Comments
 (0)