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

[fix] jsx-uses-vars: ignore namespaces #2985

Merged
merged 1 commit into from May 17, 2021

Conversation

remcohaszing
Copy link
Contributor

JSX namespaces are transpiled into strings, not identifiers.

This can be seen in here for TypeScript.

Babel behaves the same, but it requires a configuration which isn’t supported by their playground.

JSX namespaces are transpiled into strings, not identifiers.
@ljharb
Copy link
Member

ljharb commented May 12, 2021

I'm a bit confused. If they're compiled into strings, how could React possibly accept them?

@remcohaszing
Copy link
Contributor Author

React doesn't support JSX namespaces, but other JSX implementations do. For example see https://github.com/remcohaszing/xast-namespaces/blob/main/__fixtures__/inherit-named-namespace.tsx

@ljharb
Copy link
Member

ljharb commented May 12, 2021

This is eslint-plugin-react, though. It's not for non-react use cases.

Ignoring namespaces might still be the right choice here, since they're invalid in react, but I'm not 100% confident of that.

@remcohaszing
Copy link
Contributor Author

remcohaszing commented May 12, 2021

This is eslint-plugin-react, though. It's not for non-react use cases.

I get that. Still, Some rules are relevant to Preact or JSX in general without extra work needed. 🙂

Ignoring namespaces might still be the right choice here, since they're invalid in react, but I'm not 100% confident of that.

What's making you doubt? The playground link shows namespaces are transpiled to strings, not identifiers. Reporting namespaces is always a false positive.

Perhaps it makes sense to add a new rule then: react/no-jsx-namespaces.

@ljharb
Copy link
Member

ljharb commented May 12, 2021

What’s the semantics of this code when not using react? eg, how does something else know what it should resolve to?

@remcohaszing
Copy link
Contributor Author

This can be configured in tsconfig.json for TypeScript, or as options in @babel-plugin-transform-react-jsx or as JSX comments, which is also documented there.

For the linked example, this is configured here

For any known configuration, namespaces are converted to strings. So as far as this rule goes, namespaced JSX should be ignored, regardless whether React or any other library is used.

@ljharb
Copy link
Member

ljharb commented May 15, 2021

Right, but "converted to strings" doesn't answer the question - what semantics do they have in any jsx transformation? "HTML element" or "custom component reference" are what I'm familiar with; what would a namespaced string possibly do?

@remcohaszing
Copy link
Contributor Author

TL;DR: They represent XML elements.

xast (XML AST) is a way to represent XML in JavaScript. In the linked code, I use xastscript, a library which allows to use hyperscript / JSX to create xast nodes.

A more practical example can be found here. I use xastscript in a test to create XML for SSO using SAML.

I have a change locally to change the that code snippet to use JSX. This looks pretty much the same as the raw xml it represents, but with variables.

const tree = (
  <samlp:Response
    xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
    Destination="http://localhost:9999/api/apps/7/saml/1/acs"
    ID="_5190f0683c9e4b77a4e0a8ffd4d4a4dd"
    InResponseTo="id27748888-5253-48bf-8cf5-b65f793b7643"
    IssueInstant="2020-11-20T10:26:11.008603+00:00"
    Version="2.0"
  >
    <saml:Issuer xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">
      http://localhost:8000/saml/metadata.xml
    </saml:Issuer>
    <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
      <ds:SignedInfo>
        <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
        <ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
        <ds:Reference URI="#_5190f0683c9e4b77a4e0a8ffd4d4a4dd">
          <ds:Transforms>
            <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
            <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
          </ds:Transforms>
          <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
          <ds:DigestValue>{digest}</ds:DigestValue>
        </ds:Reference>
      </ds:SignedInfo>
      <ds:SignatureValue>
        GKQRfvJ0BR1geBqUttE6eXZCj9Ac+n1KPrN7R9odfrL8mXaU71aqW+rkNRCRV8NrY019bHDNDlWBpYDMLwsqcA==
      </ds:SignatureValue>
      <ds:KeyInfo>
        <ds:X509Data>
          <ds:X509Certificate>
            MIICKzCCAdWgAwIBAgIJAM8DxRNtPj90MA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwHhcNMTEwODEyMjA1MTIzWhcNMTIwODExMjA1MTIzWjBFMQswCQYDVQQGEwJBVTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANcNmgm4YlSUAr2xdWei5aRU/DbWtsQ47gjkv28Ekje3ob+6q0M+D5phwYDcv9ygYmuJ5wOi1cPprsWdFWmvSusCAwEAAaOBpzCBpDAdBgNVHQ4EFgQUzyBR9+vE8bygqvD6CZ/w6aQPikMwdQYDVR0jBG4wbIAUzyBR9+vE8bygqvD6CZ/w6aQPikOhSaRHMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGSCCQDPA8UTbT4/dDAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBBQUAA0EAIQuPLA/mlMJAMF680kL7reX5WgyRwAtRzJK6FgNjE7kRaLZQ79UKYVYa0VAyrRdoNEyVhG4tJFEiQJzaLWsl/A==
          </ds:X509Certificate>
        </ds:X509Data>
      </ds:KeyInfo>
    </ds:Signature>
    <samlp:Status>
      <samlp:StatusCode Value={statusCode} />
    </samlp:Status>
    <saml:Assertion
      xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
      ID="_2d1e69f46b0b4e569928a2a0861ff2a4"
      IssueInstant="2020-11-20T10:26:11.008603+00:00"
      Version="2.0"
    >
      <saml:Issuer>http://localhost:8000/saml/metadata.xml</saml:Issuer>
      <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
        <ds:SignedInfo>
          <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
          <ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
          <ds:Reference URI="#_2d1e69f46b0b4e569928a2a0861ff2a4">
            <ds:Transforms>
              <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature/" />
              <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
            </ds:Transforms>
            <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
            <ds:DigestValue>aeMoD3gP962UNfpc8Qxd0aAELMo=</ds:DigestValue>
          </ds:Reference>
        </ds:SignedInfo>
        <ds:SignatureValue>
          Y+4rrPo0doC1Tos1zsGZJr7IgNtkbf4kVKE/Au/+RNCSSLrDSOur5D5Ic5cYMhRRzidZh1xqcxaliRjgXZK4Lg==
        </ds:SignatureValue>
        <ds:KeyInfo>
          <ds:X509Data>
            <ds:X509Certificate>
              MIICKzCCAdWgAwIBAgIJAM8DxRNtPj90MA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwHhcNMTEwODEyMjA1MTIzWhcNMTIwODExMjA1MTIzWjBFMQswCQYDVQQGEwJBVTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANcNmgm4YlSUAr2xdWei5aRU/DbWtsQ47gjkv28Ekje3ob+6q0M+D5phwYDcv9ygYmuJ5wOi1cPprsWdFWmvSusCAwEAAaOBpzCBpDAdBgNVHQ4EFgQUzyBR9+vE8bygqvD6CZ/w6aQPikMwdQYDVR0jBG4wbIAUzyBR9+vE8bygqvD6CZ/w6aQPikOhSaRHMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGSCCQDPA8UTbT4/dDAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBBQUAA0EAIQuPLA/mlMJAMF680kL7reX5WgyRwAtRzJK6FgNjE7kRaLZQ79UKYVYa0VAyrRdoNEyVhG4tJFEiQJzaLWsl/A==
            </ds:X509Certificate>
          </ds:X509Data>
        </ds:KeyInfo>
      </ds:Signature>
      {subject && (
        <saml:Subject>
          {subject.nameId && (
            <saml:NameID
              Format="urn:oasis:names:tc:SAML:2.0:nameid-format:email"
              SPNameQualifier="http://localhost:9999/api/apps/7/saml/1/metadata.xml"
            >
              alex@example.com
            </saml:NameID>
          )}
          {subject.loginId && (
            <saml:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer">
              <saml:SubjectConfirmationData
                InResponseTo="id27748888-5253-48bf-8cf5-b65f793b7643"
                NotOnOrAfter="2020-11-20T10:41:11.008603+00:00"
                Recipient="http://localhost:9999/api/apps/7/saml/1/acs"
              />
            </saml:SubjectConfirmation>
          )}
        </saml:Subject>
      )}
      <saml:Conditions
        NotBefore="2020-11-20T10:23:11.008603+00:00"
        NotOnOrAfter="2020-11-20T10:41:11.008603+00:00"
      >
        <saml:AudienceRestriction>
          <saml:Audience>http://localhost:9999/api/apps/7/saml/1/metadata.xml</saml:Audience>
        </saml:AudienceRestriction>
      </saml:Conditions>
      <saml:AuthnStatement AuthnInstant="2020-11-20T10:26:11.008603+00:00">
        <saml:AuthnContext>
          <saml:AuthnContextClassRef>
            urn:oasis:names:tc:SAML:2.0:ac:classes:Password
          </saml:AuthnContextClassRef>
        </saml:AuthnContext>
      </saml:AuthnStatement>
      <saml:AttributeStatement>
        <saml:Attribute Name="foo" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic">
          <saml:AttributeValue>bar</saml:AttributeValue>
        </saml:Attribute>
      </saml:AttributeStatement>
    </saml:Assertion>
  </samlp:Response>
);

@ljharb
Copy link
Member

ljharb commented May 15, 2021

ok, so there's no custom code being ran for those, it's just custom tag names?

@remcohaszing
Copy link
Contributor Author

remcohaszing commented May 16, 2021

ok, so there's no custom code being ran for those, it's just custom tag names?

That's right

@ljharb ljharb force-pushed the fix-jsx-uses-vars-namespace branch from 9df5b41 to f864ac1 Compare May 17, 2021 04:43
@ljharb ljharb merged commit f864ac1 into jsx-eslint:master May 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants