Repository: sqlfluff/sqlfluff

Issue:
Add Support for Alternate File Types/Extensions
It would be awesome if sqlfluff could support configuring a list of file types/extensions to be considered by the `lint` and `fix` commands. Ideally, the configuration could be done in the `.sqlfluff` file.

One example would be Jinja2 files that are templated SQL statements. Using the convention of `.sql.j2	 for the file extension would allow these files to be targeted for formatting along side `.sql` files.

Thanks!

Discussion:

Patch:
diff --git a/src/sqlfluff/core/default_config.cfg b/src/sqlfluff/core/default_config.cfg
index 04d3bef42..669a11ad6 100644
--- a/src/sqlfluff/core/default_config.cfg
+++ b/src/sqlfluff/core/default_config.cfg
@@ -9,6 +9,10 @@ recurse = 0
 output_line_length = 80
 runaway_limit = 10
 ignore_templated_areas = True
+# Comma separated list of file extensions to lint.
+
+# NB: This config will only apply in the root folder.
+sql_file_exts = .sql,.sql.j2,.dml,.ddl
 
 [sqlfluff:indentation]
 indented_joins = False
diff --git a/src/sqlfluff/core/linter.py b/src/sqlfluff/core/linter.py
index 0dacdd0da..6f86356af 100644
--- a/src/sqlfluff/core/linter.py
+++ b/src/sqlfluff/core/linter.py
@@ -710,14 +710,12 @@ class Linter:
 
     def __init__(
         self,
-        sql_exts: Tuple[str, ...] = (".sql",),
         config: Optional[FluffConfig] = None,
         formatter: Any = None,
         dialect: Optional[str] = None,
         rules: Optional[Union[str, List[str]]] = None,
         user_rules: Optional[Union[str, List[str]]] = None,
     ) -> None:
-        self.sql_exts = sql_exts
         # Store the config object
         self.config = FluffConfig.from_kwargs(
             config=config, dialect=dialect, rules=rules
@@ -1193,7 +1191,7 @@ class Linter:
                 # that the ignore file is processed after the sql file.
 
                 # Scan for remaining files
-                for ext in self.sql_exts:
+                for ext in self.config.get("sql_file_exts", default=".sql").split(","):
                     # is it a sql file?
                     if fname.endswith(ext):
                         buffer.append(fpath)


Test patch:
diff --git a/test/core/linter_test.py b/test/core/linter_test.py
index 441030a05..15a80a568 100644
--- a/test/core/linter_test.py
+++ b/test/core/linter_test.py
@@ -28,6 +28,22 @@ def test__linter__path_from_paths__dir():
     }
 
 
+def test__linter__path_from_paths__default():
+    """Test .sql files are found by default."""
+    lntr = Linter()
+    paths = normalise_paths(lntr.paths_from_path("test/fixtures/linter"))
+    assert "test.fixtures.linter.passing.sql" in paths
+    assert "test.fixtures.linter.discovery_file.txt" not in paths
+
+
+def test__linter__path_from_paths__exts():
+    """Test configuration of file discovery."""
+    lntr = Linter(config=FluffConfig(overrides={"sql_file_exts": ".txt"}))
+    paths = normalise_paths(lntr.paths_from_path("test/fixtures/linter"))
+    assert "test.fixtures.linter.passing.sql" not in paths
+    assert "test.fixtures.linter.discovery_file.txt" in paths
+
+
 def test__linter__path_from_paths__file():
     """Test extracting paths from a file path."""
     lntr = Linter()
@@ -51,7 +67,7 @@ def test__linter__path_from_paths__not_exist_ignore():
 
 def test__linter__path_from_paths__explicit_ignore():
     """Test ignoring files that were passed explicitly."""
-    lntr = Linter(config=FluffConfig())
+    lntr = Linter()
     paths = lntr.paths_from_path(
         "test/fixtures/linter/sqlfluffignore/path_a/query_a.sql",
         ignore_non_existent_files=True,
diff --git a/test/fixtures/linter/discovery_file.txt b/test/fixtures/linter/discovery_file.txt
new file mode 100644
index 000000000..85fbbd314
--- /dev/null
+++ b/test/fixtures/linter/discovery_file.txt
@@ -0,0 +1,2 @@
+-- This is a text file to test discovery of configured file extensions.
+SELECT 1
\ No newline at end of file