Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR removes the deprecated with‑statement support in template rendering and updates the API to always require two arguments—template and data—so that data keys are expanded as local variables.
- Removed tests exercising the old one-argument API and updated them to use the two-argument form.
- Adjusted caching in micro-template.js with a new Map-based mechanism and updated the benchmark script and package.json with new dependencies and scripts.
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| test/test.js | Updated tests to match the new two-argument API and removed deprecated behavior. |
| package.json | Added benchmark script and optionalDependencies for new benchmarking libraries. |
| misc/benchmark.js | Replaced custom benchmark with the mitata API for performance measurement. |
| lib/micro-template.js | Refactored the template function to remove with‑statements and use a Map for caching. |
| README.md | Updated documentation reflecting the new API and removal of with‑statement usage. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
with は既に非推奨であり、パフォーマンス的にも大きな負の側面がある https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Statements/with#browser_compatibility
いつなくなるかわからないものを標準動作としたくない
実装方法について
with構文で束縛するのではなく、Object.keys(data)で取得した全てのキーを new Function の引数として展開し、ローカル変数として直接参照できるように。new Function("__this", ...keys, body)の形で、dataの各プロパティ名を引数リストに追加。func.call(null, __this, ...keys.map(k => data[k]))のように、dataの値を順番に渡す。fooやbarのように素直に変数参照ができ、withの副作用やパフォーマンス問題を回避。__thisオブジェクトはエスケープや行番号管理用として引き続き利用。これにともなう破壊的な変更
template(t)の1引数呼び出し(関数を返す)は廃止され、必ずtemplate(t, data)の2引数で呼び出し、即座に変換済みの文字列を返す仕様に変更されました。dataオブジェクトのプロパティのみとなり、withによる暗黙的なスコープ拡張は行われません。dataのキーがJavaScriptの予約語や不正な識別子の場合、テンプレート生成時にエラーとなります(従来も同様の制約がありましたが、より顕在化します)。template(t, data)形式に修正が必要です。