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

Enable checking Typescript files behind a compiler flag #4753

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
1 change: 0 additions & 1 deletion compiler/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions compiler/crates/common/src/feature_flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ pub struct FeatureFlags {
/// Disable validation of the `edgeTypeName` argument on `@prependNode` and `@appendNode`.
#[serde(default)]
pub disable_edge_type_name_validation_on_declerative_connection_directives: FeatureFlag,

// Removes the @ts-nocheck from generated Typescript Files
drewatk marked this conversation as resolved.
Show resolved Hide resolved
#[serde(default)]
pub typescript_check_generated_files: bool,
}

#[derive(Debug, Deserialize, Clone, Serialize, Default, JsonSchema)]
Expand Down
15 changes: 14 additions & 1 deletion compiler/crates/relay-compiler/relay-compiler-config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,10 @@
}
}
]
},
"typescript_check_generated_files": {
"default": false,
"type": "boolean"
}
},
"additionalProperties": false
Expand Down Expand Up @@ -1770,7 +1774,8 @@
},
"text_artifacts": {
"kind": "disabled"
}
},
"typescript_check_generated_files": false
},
"type": "object",
"properties": {
Expand Down Expand Up @@ -2935,6 +2940,10 @@
}
}
]
},
"typescript_check_generated_files": {
"default": false,
"type": "boolean"
}
},
"additionalProperties": false
Expand Down Expand Up @@ -4303,6 +4312,10 @@
}
}
]
},
"typescript_check_generated_files": {
"default": false,
"type": "boolean"
}
},
"additionalProperties": false
Expand Down
27 changes: 17 additions & 10 deletions compiler/crates/relay-compiler/src/artifact_content/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub fn generate_preloadable_query_parameters(

// -- Begin Disable Lint Section --
content_sections.push(ContentSection::Generic(generate_disable_lint_section(
&project_config.typegen_config.language,
&project_config,
)?));
// -- End Disable Lint Section --

Expand Down Expand Up @@ -181,7 +181,7 @@ pub fn generate_updatable_query(

// -- Begin Disable Lint Section --
content_sections.push(ContentSection::Generic(generate_disable_lint_section(
&project_config.typegen_config.language,
&project_config,
)?));
// -- End Disable Lint Section --

Expand Down Expand Up @@ -322,7 +322,7 @@ pub fn generate_operation(

// -- Begin Disable Lint Section --
content_sections.push(ContentSection::Generic(generate_disable_lint_section(
&project_config.typegen_config.language,
&project_config,
)?));
// -- End Disable Lint Section --

Expand Down Expand Up @@ -513,7 +513,7 @@ pub fn generate_split_operation(

// -- Begin Disable Lint Section --
content_sections.push(ContentSection::Generic(generate_disable_lint_section(
&project_config.typegen_config.language,
&project_config,
)?));
// -- End Disable Lint Section --

Expand Down Expand Up @@ -665,7 +665,7 @@ fn generate_read_only_fragment(

// -- Begin Disable Lint Section --
content_sections.push(ContentSection::Generic(generate_disable_lint_section(
&project_config.typegen_config.language,
&project_config,
)?));
// -- End Disable Lint Section --

Expand Down Expand Up @@ -787,7 +787,7 @@ fn generate_assignable_fragment(

// -- Begin Disable Lint Section --
content_sections.push(ContentSection::Generic(generate_disable_lint_section(
&project_config.typegen_config.language,
&project_config,
)?));
// -- End Disable Lint Section --

Expand Down Expand Up @@ -888,13 +888,20 @@ fn write_variable_value_with_type(
}
}

fn generate_disable_lint_section(language: &TypegenLanguage) -> Result<GenericSection, FmtError> {
fn generate_disable_lint_section(
project_config: &ProjectConfig,
) -> Result<GenericSection, FmtError> {
let mut section = GenericSection::default();
match language {
match project_config.typegen_config.language {
TypegenLanguage::TypeScript => {
writeln!(section, "/* tslint:disable */")?;
writeln!(section, "/* eslint-disable */")?;
writeln!(section, "// @ts-nocheck")?;
if !project_config
.feature_flags
.typescript_check_generated_files
{
writeln!(section, "// @ts-nocheck")?;
}
}
TypegenLanguage::Flow | TypegenLanguage::JavaScript => {
writeln!(section, "/* eslint-disable */")?;
Expand Down Expand Up @@ -1066,7 +1073,7 @@ pub fn generate_resolvers_schema_module_content(

// -- Begin Disable Lint Section --
content_sections.push(ContentSection::Generic(generate_disable_lint_section(
&project_config.typegen_config.language,
&project_config,
)?));
// -- End Disable Lint Section --

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
==================================== INPUT ====================================
//- foo.ts
/**
* @RelayResolver User.foo: RelayResolverValue
*/

//- bar.ts
graphql`fragment barFragment on User {
foo
}`

//- relay.config.json
{
"language": "typescript",
"schema": "./schema.graphql",
"eagerEsModules": true,
"featureFlags": {
"typescript_check_generated_files": true
}
}

//- schema.graphql
type Query { user: User }
type User { foo: String }
==================================== OUTPUT ===================================
//- __generated__/barFragment.graphql.ts
/**
* <auto-generated> SignedSource<<d5205fa7d07ed17c0e5eac12b44a7d44>>
* @lightSyntaxTransform
* @nogrep
*/

/* tslint:disable */
/* eslint-disable */

import { Fragment, ReaderFragment } from 'relay-runtime';
import { FragmentRefs } from "relay-runtime";
export type barFragment$data = {
readonly foo: string | null | undefined;
readonly " $fragmentType": "barFragment";
};
export type barFragment$key = {
readonly " $data"?: barFragment$data;
readonly " $fragmentSpreads": FragmentRefs<"barFragment">;
};

const node: ReaderFragment = {
"argumentDefinitions": [],
"kind": "Fragment",
"metadata": null,
"name": "barFragment",
"selections": [
{
"alias": null,
"args": null,
"kind": "ScalarField",
"name": "foo",
"storageKey": null
}
],
"type": "User",
"abstractKey": null
};

(node as any).hash = "f60f2dcc6b71a6c9ec170e68dc2c994d";

export default node;
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//- foo.ts
/**
* @RelayResolver User.foo: RelayResolverValue
*/

//- bar.ts
graphql`fragment barFragment on User {
foo
}`

//- relay.config.json
{
"language": "typescript",
"schema": "./schema.graphql",
"eagerEsModules": true,
"featureFlags": {
"typescript_check_generated_files": true
}
}

//- schema.graphql
type Query { user: User }
type User { foo: String }
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<58762aeeff855edc7a25f98a2b63153f>>
* @generated SignedSource<<a5e334d173fd3b9099c524ede846804d>>
*/

mod relay_compiler_integration;
Expand Down Expand Up @@ -390,6 +390,13 @@ async fn spread_multiple_interface_fragments_on_concrete_type() {
test_fixture(transform_fixture, file!(), "spread_multiple_interface_fragments_on_concrete_type.input", "relay_compiler_integration/fixtures/spread_multiple_interface_fragments_on_concrete_type.expected", input, expected).await;
}

#[tokio::test]
async fn typescript_check_generated_files() {
let input = include_str!("relay_compiler_integration/fixtures/typescript_check_generated_files.input");
let expected = include_str!("relay_compiler_integration/fixtures/typescript_check_generated_files.expected");
test_fixture(transform_fixture, file!(), "typescript_check_generated_files.input", "relay_compiler_integration/fixtures/typescript_check_generated_files.expected", input, expected).await;
}

#[tokio::test]
async fn typescript_resolver_type_import() {
let input = include_str!("relay_compiler_integration/fixtures/typescript_resolver_type_import.input");
Expand Down