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

default globals implementation #68

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions __tests__/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import sinon from 'sinon';
import { ExtendedPluginBuilder } from '../src/js-utils';
import 'code-equality-assertions/jest';
import { Preprocessor } from 'content-tag';
import { ALLOWED_GLOBALS } from '../src/scope-locals';

describe('htmlbars-inline-precompile', function () {
// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand Down Expand Up @@ -1901,6 +1902,37 @@ describe('htmlbars-inline-precompile', function () {
`);
});

describe('implements RFC#1070: default globals', function () {
for (let name of ALLOWED_GLOBALS) {
it(`${name}: allowed`, async function () {
plugins = [
[
HTMLBarsInlinePrecompile,
{
compiler,
targetFormat: 'hbs',
},
],
];

let transformed = await transform(
`import { template } from '@ember/template-compiler';
const data = {};
export default template('{{${name} data}}', { eval: function() { return eval(arguments[0]) } })
`
);

expect(transformed).toEqualCode(`
import { precompileTemplate } from "@ember/template-compilation";
import { setComponentTemplate } from "@ember/component";
import templateOnly from "@ember/component/template-only";
const data = {};
export default setComponentTemplate(precompileTemplate('{{${name} data}}', { strictMode: true, scope: () => ({ ${name}, data }) }), templateOnly());
`);
});
}
});

// You might think this would be confusing style, and you'd be correct. But
// that's what the lint rules are for. When it comes to correctness, we need
// our scope to behave like real Javascript, and Javascript doesn't care
Expand Down
72 changes: 71 additions & 1 deletion src/scope-locals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,76 @@ import { ASTPluginEnvironment, NodeVisitor } from '@glimmer/syntax';
import { astNodeHasBinding } from './hbs-utils';
import { readOnlyArray } from './read-only-array';

/**
* RFC: https://github.com/emberjs/rfcs/pull/1070
*
* Criteria for inclusion in this list:
*
* Any of:
* - begins with an uppercase letter
* - guaranteed to never be added to glimmer as a keyword (e.g.: globalThis)
*
* And:
* - must not need new to invoke
* - must not require lifetime management (e.g.: setTimeout)
* - must not be a single-word lower-case API, because of potential collision with future new HTML elements
* - if the API is a function, the return value should not be a promise
* - must be one one of these lists:
* - https://tc39.es/ecma262/#sec-global-object
* - https://tc39.es/ecma262/#sec-function-properties-of-the-global-object
* - https://html.spec.whatwg.org/multipage/nav-history-apis.html#window
* - https://html.spec.whatwg.org/multipage/indices.html#all-interfaces
* - https://html.spec.whatwg.org/multipage/webappapis.html
*/
export const ALLOWED_GLOBALS = new Set([
// ////////////////
// namespaces
// ////////////////
// TC39
'globalThis',
'Atomics',
'JSON',
'Math',
'Reflect',
// WHATWG
'localStorage',
'sessionStorage',
// ////////////////
// functions / utilities
// ////////////////
// TC39
'isNaN',
'isFinite',
'parseInt',
'parseFloat',
'decodeURI',
'decodeURIComponent',
'encodeURI',
'encodeURIComponent',
// WHATWG
'postMessage',
'structuredClone',
// ////////////////
// new-less Constructors (still functions)
// ////////////////
// TC39
'Array', // different behavior from (array)
'BigInt',
'Boolean',
'Date',
'Number',
'Object', // different behavior from (hash)
'String',
// ////////////////
// Values
// ////////////////
// TC39
'Infinity',
'NaN',
// WHATWG
'isSecureContext',
]);

/*
`mode` refers to the implicit and explicit formats defined here:

Expand Down Expand Up @@ -72,7 +142,7 @@ export class ScopeLocals {

#isInJsScope(hbsName: string, jsPath: NodePath) {
let jsName = this.#mapping[hbsName] ?? hbsName;
return ['globalThis'].includes(jsName) || jsPath.scope.getBinding(jsName);
return ALLOWED_GLOBALS.has(jsName) || jsPath.scope.getBinding(jsName);
}

// this AST transform discovers all possible upvars in HBS that refer to valid
Expand Down