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

Add ts-transform-inline-webgl-constants #456

Merged
merged 1 commit into from
Feb 29, 2024
Merged
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
4 changes: 4 additions & 0 deletions modules/dev-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
"./ts-transform-remove-glsl-comments": {
"require": "./dist/ts-plugins/ts-transform-remove-glsl-comments/index.cjs",
"import": "./dist/ts-plugins/ts-transform-remove-glsl-comments/index.js"
},
"./ts-transform-inline-webgl-constants": {
"require": "./dist/ts-plugins/ts-transform-inline-webgl-constants/index.cjs",
"import": "./dist/ts-plugins/ts-transform-inline-webgl-constants/index.js"
}
},
"types": "./dist/index.d.ts",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* TypeScript transform to replaces `gl.<constant>` or `GL.<constant>` references with
* the corresponding WebGL constant value. Requires `@luma.gl/constants` as peer dependency.
* Usage with ts-patch:
{
"plugins": [
{
"transform": "ocular-dev-tools/ts-transform-inline-webgl-constants"
}
]
}
*/
import type {Program, TransformationContext, SourceFile, Node, StringLiteral} from 'typescript';
import type {TransformerExtras, PluginConfig} from 'ts-patch';
import {GL} from '@luma.gl/constants';

export default function (program: Program, pluginConfig: PluginConfig, {ts}: TransformerExtras) {
return (ctx: TransformationContext) => {
const {factory} = ctx;

function filterLeftIdentifier(node: Node): boolean {
const left = node.getChildAt(0);
return ts.isIdentifier(left) && (left.text === 'GL' || left.text === 'gl');
}

return (sourceFile: SourceFile) => {
function visit(node: Node): Node {
if (ts.isPropertyAccessExpression(node) && filterLeftIdentifier(node)) {
const key = node.getChildAt(2);
if (ts.isIdentifier(key) && key.text in GL) {
return factory.createNumericLiteral(GL[key.text]);
}
}
if (ts.isElementAccessExpression(node) && filterLeftIdentifier(node)) {
const key = node.getChildAt(2);
if (ts.isStringLiteral(key) && key.text in GL) {
return factory.createNumericLiteral(GL[key.text]);
}
}
return ts.visitEachChild(node, visit, ctx);
}
return ts.visitNode(sourceFile, visit);
};
};
}
1 change: 1 addition & 0 deletions modules/dev-tools/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ import './lib/configuration.spec';
import './ts-plugins/ts-transform-version-inline.spec';
import './ts-plugins/ts-transform-append-extension.spec';
import './ts-plugins/ts-transform-remove-glsl-comments/index.spec';
import './ts-plugins/ts-transform-inline-webgl-constants.spec';
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import test from 'tape-promise/tape';
import {transpile, assertSourceEqual} from './test-transformer.js';
// @ts-expect-error Aliased import, remapped to valid path in esm-loader
import inlineConstants from 'ocular-dev-tools/ts-plugins/ts-transform-inline-webgl-constants';

const testCases = [
{
title: 'drop GL import',
input: `\
device.setParametersWebGL({
blendFunc: [GL.ONE, GL.ONE_MINUS_DST_COLOR, GL.SRC_ALPHA, GL.DST_ALPHA]
});
`,
output: `\
device.setParametersWebGL({
blendFunc: [1, 775, 770, 772]
});
`
},
{
title: 'gl constants replaced',
input: `gl.getParameter(gl.CULL_FACE_MODE);`,
output: `gl.getParameter(2885);`
},
{
title: 'static property replaced',
input: `console.log(GL['TRIANGLES']);`,
output: `console.log(4);`
},
{
title: 'dynamic property not replaced',
input: `\
const name = 'TRIANGLES';
console.log(GL[name]);
`,
output: `\
const name = 'TRIANGLES';
console.log(GL[name]);
`
}
];

test('ts-transform-inline-webgl-constants', (t) => {
for (const testCase of testCases) {
const result = transpile({
source: testCase.input,
transformer: inlineConstants,
config: {}
});

t.is(assertSourceEqual(result, testCase.output), true, testCase.title);
}

t.end();
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"devDependencies": {
"@babel/plugin-syntax-import-assertions": "^7.20.0",
"@luma.gl/constants": "^9.0.0-beta",
"pre-commit": "^1.2.2",
"pre-push": "^0.1.1",
"ts-morph": "^21.0.0"
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2185,6 +2185,11 @@
dependencies:
call-bind "^1.0.2"

"@luma.gl/constants@^9.0.0-beta":
version "9.0.0-beta.5"
resolved "https://registry.yarnpkg.com/@luma.gl/constants/-/constants-9.0.0-beta.5.tgz#12e558319f7f08bf2416067ca0a991721cc82092"
integrity sha512-nQP/3MGwZuFuoCCs7xAvduTlCn3bkcqm7UZcTDFGhqoUAPZFS8zcGe/JVwVH53I4YzV1qJFc+/+noUQc2si3IQ==

"@mrmlnc/readdir-enhanced@^2.2.1":
version "2.2.1"
resolved "https://registry.yarnpkg.com/@mrmlnc/readdir-enhanced/-/readdir-enhanced-2.2.1.tgz#524af240d1a360527b730475ecfa1344aa540dde"
Expand Down
Loading