Skip to content

Use .ts extension in relative source imports - #591

Merged
eemeli merged 8 commits into
mainfrom
import-ts
Dec 30, 2024
Merged

Use .ts extension in relative source imports#591
eemeli merged 8 commits into
mainfrom
import-ts

Conversation

@eemeli

@eemeli eemeli commented Nov 29, 2024

Copy link
Copy Markdown
Owner

This change adopts the --rewriteRelativeImportExtensions option introduced in Typescript 5.7, and changes all relative paths to use the .ts extension.

Due to rollup/plugins#1820, a post-compilation transform is required to get the CLI tool to work right.

An ESLint rule is added to ensure that the .ts extension is always there; this may be of interest to others as well:

yaml/eslint.config.mjs

Lines 41 to 51 in 18e6112

'no-restricted-imports': [
'error',
{
patterns: [
{
regex: '^\\..*(?<!\\.ts)$',
message: 'Relative imports must use .ts extension.'
}
]
}
],

Together with explicit type on imports & exports, this change fixes #301 and makes the library work directly in Deno. A really simple Deno smoke test is included. It would be nice to get the full test suite to run on Deno as well, as discussed in #301 (comment)

Ping @justinmchase & @jeff-hykin: Would you have time to see if this works as expected?

@jeff-hykin

Copy link
Copy Markdown

Yeah thanks for the ping! If I remeber once I'm at my PC I'll try it out

@eemeli

eemeli commented Nov 29, 2024

Copy link
Copy Markdown
Owner Author

Also needed to add an afterDeclarations transform to drop the .ts extensions from the imports in the .d.ts files, in order to retain compatibility with older TS versions.

@eemeli

eemeli commented Dec 9, 2024

Copy link
Copy Markdown
Owner Author

@jeff-hykin Any time for a trial run yet?

I'm looking here for feedback from some actual Deno user to confirm that this does indeed provide a fix for #301, before just presuming so on my own initiative.

@justinmchase

justinmchase commented Dec 14, 2024

Copy link
Copy Markdown

Looks like its working to me, I cloned the repo, checked out the branch and wrote a little script.

test script

image

terminal results

image

@justinmchase

Copy link
Copy Markdown

Also, worth mentioning, but Deno has made a new package repo https://jsr.io which may be worth targeting instead of / in addition to deno land.

It's not much different in terms of what you publish, just done with a github action.

Here is a pretty minimal example repo I created for reference: https://github.com/justinmchase/type/blob/main/.github/workflows/publish.yml

@eemeli
eemeli merged commit 2c55723 into main Dec 30, 2024
@eemeli
eemeli deleted the import-ts branch December 30, 2024 10:13
@jeff-hykin

jeff-hykin commented Feb 18, 2025

Copy link
Copy Markdown

Sorry it took a really long time, but aside from one caveat, I can confirm it works in deno natively! Thank you!

// caveat: `import YAML`  (from readme) does *not* work, but the following does 
import * as YAML from 'https://raw.githubusercontent.com/eemeli/yaml/refs/heads/main/src/index.ts'
YAML.parse('3.14159') // 3.14159
YAML.parse('[ true, false, maybe, null ]\n') // [ true, false, 'maybe', null ]

I ran hacked versions of the tests, and they seem to pass too

Blankll added a commit to geek-fun/serverlessinsight that referenced this pull request Feb 18, 2025
![snyk-top-banner](https://redirect.github.com/andygongea/OWASP-Benchmark/assets/818805/c518c423-16fe-447e-b67f-ad5a49b5d123)


<h3>Snyk has created this PR to upgrade yaml from 2.6.1 to 2.7.0.</h3>

:information_source: Keep your dependencies up-to-date. This makes it
easier to fix existing vulnerabilities and to more quickly identify and
fix newly disclosed vulnerabilities when they affect your project.

<hr/>


- The recommended version is **1 version** ahead of your current
version.

- The recommended version was released **2 months ago**.



<details>
<summary><b>Release notes</b></summary>
<br/>
  <details>
    <summary>Package name: <b>yaml</b></summary>
    <ul>
      <li>
<b>2.7.0</b> - <a
href="https://redirect.github.com/eemeli/yaml/releases/tag/v2.7.0">2024-12-31</a></br><p>The
library is now available on JSR as <a href="https://jsr.io/@
eemeli/yaml" rel="nofollow">@ eemeli/yaml</a> and on deno.land/x as <a
href="https://deno.land/x/yaml" rel="nofollow">yaml</a>. In addition to
Node.js and browsers, it should work in Deno, Bun, and Cloudflare
Workers.</p>
<ul>
<li>Use .ts extension in all relative imports (<a class="issue-link
js-issue-link" data-error-text="Failed to load title"
data-id="2704495320" data-permission-text="Title is private"
data-url="eemeli/yaml#591"
data-hovercard-type="pull_request"
data-hovercard-url="/eemeli/yaml/pull/591/hovercard"
href="https://redirect.github.com/eemeli/yaml/pull/591">#591</a>)</li>
<li>Ignore newline after block seq indicator as space before value (<a
class="issue-link js-issue-link" data-error-text="Failed to load title"
data-id="2684051086" data-permission-text="Title is private"
data-url="eemeli/yaml#590"
data-hovercard-type="issue"
data-hovercard-url="/eemeli/yaml/issues/590/hovercard"
href="https://redirect.github.com/eemeli/yaml/issues/590">#590</a>)</li>
<li>Require Node.js 14.18 or later (was 14.6) (<a class="issue-link
js-issue-link" data-error-text="Failed to load title"
data-id="2765423835" data-permission-text="Title is private"
data-url="eemeli/yaml#598"
data-hovercard-type="issue"
data-hovercard-url="/eemeli/yaml/issues/598/hovercard"
href="https://redirect.github.com/eemeli/yaml/issues/598">#598</a>)</li>
</ul>
      </li>
      <li>
<b>2.6.1</b> - <a
href="https://redirect.github.com/eemeli/yaml/releases/tag/v2.6.1">2024-11-19</a></br><ul>
<li>Do not strip <code>:00</code> seconds from <code>!!timestamp</code>
values (<a class="issue-link js-issue-link" data-error-text="Failed to
load title" data-id="2561052215" data-permission-text="Title is private"
data-url="eemeli/yaml#578"
data-hovercard-type="pull_request"
data-hovercard-url="/eemeli/yaml/pull/578/hovercard"
href="https://redirect.github.com/eemeli/yaml/pull/578">#578</a>, with
thanks to <a class="user-mention notranslate" data-hovercard-type="user"
data-hovercard-url="/users/qraynaud/hovercard"
data-octo-click="hovercard-link-click"
data-octo-dimensions="link_type:self"
href="https://redirect.github.com/qraynaud">@ qraynaud</a>)</li>
<li>Tighten regexp for JSON <code>!!bool</code> (<a class="issue-link
js-issue-link" data-error-text="Failed to load title"
data-id="2651384053" data-permission-text="Title is private"
data-url="eemeli/yaml#587"
data-hovercard-type="pull_request"
data-hovercard-url="/eemeli/yaml/pull/587/hovercard"
href="https://redirect.github.com/eemeli/yaml/pull/587">#587</a>, with
thanks to <a class="user-mention notranslate" data-hovercard-type="user"
data-hovercard-url="/users/vra5107/hovercard"
data-octo-click="hovercard-link-click"
data-octo-dimensions="link_type:self"
href="https://redirect.github.com/vra5107">@ vra5107</a>)</li>
<li>Default to literal block scalar if folded would overflow (<a
class="issue-link js-issue-link" data-error-text="Failed to load title"
data-id="2594165845" data-permission-text="Title is private"
data-url="eemeli/yaml#585"
data-hovercard-type="issue"
data-hovercard-url="/eemeli/yaml/issues/585/hovercard"
href="https://redirect.github.com/eemeli/yaml/issues/585">#585</a>)</li>
</ul>
      </li>
    </ul>
from <a href="https://redirect.github.com/eemeli/yaml/releases">yaml
GitHub release notes</a>
  </details>
</details>

---

> [!IMPORTANT]
>
> - Check the changes in this PR to ensure they won't cause issues with
your project.
> - This PR was automatically created by Snyk using the credentials of a
real user.

---

**Note:** _You are seeing this because you or someone else with access
to this repository has authorized Snyk to open upgrade PRs._

**For more information:** <img
src="https://api.segment.io/v1/pixel/track?data=eyJ3cml0ZUtleSI6InJyWmxZcEdHY2RyTHZsb0lYd0dUcVg4WkFRTnNCOUEwIiwiYW5vbnltb3VzSWQiOiIzYjkwN2M1MC0zODJkLTQyMjQtYTFhZC02OGFmODhhNWY3MTMiLCJldmVudCI6IlBSIHZpZXdlZCIsInByb3BlcnRpZXMiOnsicHJJZCI6IjNiOTA3YzUwLTM4MmQtNDIyNC1hMWFkLTY4YWY4OGE1ZjcxMyJ9fQ=="
width="0" height="0"/>

> - 🧐 [View latest project
report](https://app.snyk.io/org/blankll/project/9c72c875-e7a2-4e68-85a9-7b26a5bc5b32?utm_source&#x3D;github&amp;utm_medium&#x3D;referral&amp;page&#x3D;upgrade-pr)
> - 📜 [Customise PR
templates](https://docs.snyk.io/scan-using-snyk/pull-requests/snyk-fix-pull-or-merge-requests/customize-pr-templates?utm_source=&utm_content=fix-pr-template)
> - 🛠 [Adjust upgrade PR
settings](https://app.snyk.io/org/blankll/project/9c72c875-e7a2-4e68-85a9-7b26a5bc5b32/settings/integration?utm_source&#x3D;github&amp;utm_medium&#x3D;referral&amp;page&#x3D;upgrade-pr)
> - 🔕 [Ignore this dependency or unsubscribe from future upgrade
PRs](https://app.snyk.io/org/blankll/project/9c72c875-e7a2-4e68-85a9-7b26a5bc5b32/settings/integration?pkg&#x3D;yaml&amp;utm_source&#x3D;github&amp;utm_medium&#x3D;referral&amp;page&#x3D;upgrade-pr#auto-dep-upgrades)

[//]: #
'snyk:metadata:{"customTemplate":{"variablesUsed":[],"fieldsUsed":[]},"dependencies":[{"name":"yaml","from":"2.6.1","to":"2.7.0"}],"env":"prod","hasFixes":false,"isBreakingChange":false,"isMajorUpgrade":false,"issuesToFix":[],"prId":"3b907c50-382d-4224-a1ad-68af88a5f713","prPublicId":"3b907c50-382d-4224-a1ad-68af88a5f713","packageManager":"npm","priorityScoreList":[],"projectPublicId":"9c72c875-e7a2-4e68-85a9-7b26a5bc5b32","projectUrl":"https://app.snyk.io/org/blankll/project/9c72c875-e7a2-4e68-85a9-7b26a5bc5b32?utm_source=github&utm_medium=referral&page=upgrade-pr","prType":"upgrade","templateFieldSources":{"branchName":"default","commitMessage":"default","description":"default","title":"default"},"templateVariants":[],"type":"auto","upgrade":[],"upgradeInfo":{"versionsDiff":1,"publishedDate":"2024-12-31T04:40:47.460Z"},"vulns":[]}'

Co-authored-by: snyk-bot <snyk-bot@snyk.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error loading in Deno

3 participants