-
-
Notifications
You must be signed in to change notification settings - Fork 646
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JSX support built on the JS backend (#21206)
This is a small change for the JS backend, as the tree-sitter parser already supports JSX. This is a fairly big decision w.r.t how the js backend will evolve with typescript and other javascript transpile formats. Since many tools that support js also supports other kinds of files, the `JSRuntime...` will be used to refer to the files that can be treated as a source code file for some node-ish runtime who either processes the AST or executes actual programmes. Clarifiying examples: 1. A linter that can process any js-esque file should accept JSRuntimeSourceField 2. All test sources should use JSRuntimeDependencies 3. Files that can cross import (post bundling) different transpile formats should use JSRuntimeDependencies and JSRuntimeSourceField. Think typescript, JSX. 4. A checker that can only process typed vue-templates should not accept JSRuntimeSourceField. This PR together with #21176 essentially gives pants "react" support. --------- Co-authored-by: riisi <[email protected]>
- Loading branch information
Showing
26 changed files
with
4,856 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,3 +68,4 @@ GTAGS | |
/.venv | ||
.tool-versions | ||
TAGS | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Copyright 2024 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
# NOTE: Sources restricted from the default for python_sources due to conflict with | ||
# - //:all-__init__.py-files | ||
# - //src/python/pants/backend/jsx/__init__.py:../../../../../all-__init__.py-files | ||
python_sources( | ||
sources=[ | ||
"target_types.py", | ||
], | ||
) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Copyright 2024 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
python_sources() | ||
|
||
python_tests(name="tests") |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Copyright 2024 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from __future__ import annotations | ||
|
||
import dataclasses | ||
from dataclasses import dataclass | ||
from typing import Iterable | ||
|
||
from pants.backend.jsx.target_types import ( | ||
JSX_FILE_EXTENSIONS, | ||
JSXSourcesGeneratorTarget, | ||
JSXTestsGeneratorSourcesField, | ||
JSXTestsGeneratorTarget, | ||
) | ||
from pants.core.goals.tailor import ( | ||
AllOwnedSources, | ||
PutativeTarget, | ||
PutativeTargets, | ||
PutativeTargetsRequest, | ||
) | ||
from pants.core.util_rules.ownership import get_unowned_files_for_globs | ||
from pants.core.util_rules.source_files import classify_files_for_sources_and_tests | ||
from pants.engine.rules import Rule, collect_rules, rule | ||
from pants.engine.unions import UnionRule | ||
from pants.util.dirutil import group_by_dir | ||
from pants.util.logging import LogLevel | ||
|
||
|
||
@dataclass(frozen=True) | ||
class PutativeJSXTargetsRequest(PutativeTargetsRequest): | ||
pass | ||
|
||
|
||
@rule(level=LogLevel.DEBUG, desc="Determine candidate JSX targets to create") | ||
async def find_putative_jsx_targets( | ||
req: PutativeJSXTargetsRequest, all_owned_sources: AllOwnedSources | ||
) -> PutativeTargets: | ||
unowned_jsx_files = await get_unowned_files_for_globs( | ||
req, all_owned_sources, (f"*{ext}" for ext in JSX_FILE_EXTENSIONS) | ||
) | ||
classified_unowned_js_files = classify_files_for_sources_and_tests( | ||
paths=unowned_jsx_files, | ||
test_file_glob=JSXTestsGeneratorSourcesField.default, | ||
sources_generator=JSXSourcesGeneratorTarget, | ||
tests_generator=JSXTestsGeneratorTarget, | ||
) | ||
|
||
return PutativeTargets( | ||
PutativeTarget.for_target_type( | ||
tgt_type, path=dirname, name=name, triggering_sources=sorted(filenames) | ||
) | ||
for tgt_type, paths, name in (dataclasses.astuple(f) for f in classified_unowned_js_files) | ||
for dirname, filenames in group_by_dir(paths).items() | ||
) | ||
|
||
|
||
def rules() -> Iterable[Rule | UnionRule]: | ||
return ( | ||
*collect_rules(), | ||
UnionRule(PutativeTargetsRequest, PutativeJSXTargetsRequest), | ||
) |
Oops, something went wrong.