Skip to content

Commit

Permalink
add tests for dev-modules (#1167)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pessimistress committed Jul 18, 2019
1 parent d8abe1f commit d393a32
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 43 deletions.
12 changes: 2 additions & 10 deletions dev-modules/babel-plugin-inline-webgl-constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@ const COLOR_RESET = '\x1b[0m';
const COLOR_YELLOW = '\x1b[33m';
const COLOR_RED = '\x1b[31m';

const DEBUG = false; // Set to true to force tracing

module.exports = function _(opts) {
if (DEBUG) {
console.log(
`${COLOR_YELLOW}luma.gl: babel GL constant inlining plugin loaded: ${GL.LINES}${COLOR_RESET}`
);
}

return {
visitor: {
ImportDeclaration(path, state) {
Expand All @@ -26,7 +18,7 @@ module.exports = function _(opts) {
if (specifier.type === 'ImportDefaultSpecifier') {
const local = specifier.node.local;
if (local.type === 'Identifier' && local.name === 'GL') {
if (DEBUG || state.opts.verbose || state.opts.debug) {
if (state.opts.debug) {
const filename = getFilename(state);
const line = local.loc.start.line;
console.error(
Expand Down Expand Up @@ -63,7 +55,7 @@ module.exports = function _(opts) {
return;
}

if (DEBUG || state.opts.verbose || state.opts.debug) {
if (state.opts.debug) {
console.error(`${COLOR_YELLOW}${filename}: ${constant} ==> ${value}${COLOR_RESET}`);
}
path.replaceWith(opts.types.numericLiteral(value));
Expand Down
4 changes: 0 additions & 4 deletions dev-modules/babel-plugin-remove-glsl-comments/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const minimatch = require('minimatch');
const {resolve} = require('path');

// inline comment is only safe to remove if it's followed by a return (i.e. end of comment)
const INLINE_COMMENT_REGEX = /\s*\/\/.*[\n\r]/g;
Expand Down Expand Up @@ -33,9 +32,6 @@ function filterFile(state) {
const patterns = state.opts.patterns || DEFAULT_PATTERNS;

return patterns.some(p => {
if (p[0] === '.') {
p = resolve(p);
}
return minimatch(filename, p);
});
}
Expand Down
44 changes: 44 additions & 0 deletions dev-modules/eslint-plugin-luma-gl-custom-rules/check-log-call.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const logFunctions = new Set([
'warn',
'error',
'deprecated',
'removed',
'probe',
'log',
'info',
'once',
'table',
'image',
'time',
'timeEnd',
'timeStamp',
'group',
'groupCollapsed',
'groupEnd',
'withGroup'
]);

module.exports = {
create: context => {
return {
CallExpression: node => {
if (
node.callee &&
node.callee.object &&
node.callee.object.name === 'log' &&
logFunctions.has(node.callee.property.name) &&
node.parent &&
node.parent.type !== 'CallExpression'
) {
context.report({
node,
message: `Use log.${node.callee.property.name}(...)()`,
fix: fixer => {
return fixer.insertTextAfter(node, '()');
}
});
}
}
};
}
};
18 changes: 1 addition & 17 deletions dev-modules/eslint-plugin-luma-gl-custom-rules/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
module.exports = {
rules: {
'check-log-call': {
create: context => {
return {
CallExpression: node => {
if (
node.callee &&
node.callee.object &&
node.callee.object.name === 'log' &&
node.parent &&
node.parent.type !== 'CallExpression'
) {
context.report(node, `Use log.${node.callee.property.name}(...)()`);
}
}
};
}
}
'check-log-call': require('./check-log-call')
}
};
4 changes: 0 additions & 4 deletions test/dev-modules.js

This file was deleted.

24 changes: 18 additions & 6 deletions test/dev-modules/babel-plugin-inline-gl-constants.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ const TEST_CASES = [
input: 'const x = gl.FLOAT;',
output: 'const x = 5126;'
},
{
title: 'undefined constant',
input: 'const x = gl.FLOAT32;',
error: true
},
{
title: 'complex',
input: `
Expand Down Expand Up @@ -52,12 +57,19 @@ function clean(code) {
/* eslint-disable */
test('InlineGLSLConstants Babel Plugin', t => {
TEST_CASES.forEach(testCase => {
const {code} = babel.transform(testCase.input, {
presets: [['@babel/env', ES6_ENV]],
plugins: [[plugin]],
filename: 'test.js'
});
t.is(clean(code), clean(testCase.output), testCase.title);
const transform = () =>
babel.transform(testCase.input, {
presets: [['@babel/env', ES6_ENV]],
plugins: [[plugin, {debug: true}]],
filename: 'test.js',
configFile: false
});
if (testCase.error) {
t.throws(transform, testCase.title);
} else {
const {code} = transform();
t.is(clean(code), clean(testCase.output), testCase.title);
}
});

t.end();
Expand Down
4 changes: 2 additions & 2 deletions test/dev-modules/babel-plugin-remove-glsl-comments.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ test('RemoveGLSLComments Babel Plugin', t => {
TEST_CASES.forEach(testCase => {
const {code} = babel.transform(testCase.input, {
comments: true,
envName: 'es5',
presets: [['@babel/env', testCase.env]],
plugins: [[plugin, {patterns: testCase.patterns}]],
filename: testCase.filename || 'test.js'
filename: testCase.filename || 'test.js',
configFile: false
});
t.is(clean(code), clean(testCase.output), testCase.title);
});
Expand Down
24 changes: 24 additions & 0 deletions test/dev-modules/eslint-plugin-luma-gl-custom-rules.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import test from 'tape-catch';
import {rules} from 'dev-modules/eslint-plugin-luma-gl-custom-rules';
import {RuleTester} from 'eslint';

const ruleTester = new RuleTester();

test('EslintCustomRules#check-log-call', t => {
ruleTester.run('check-log-call', rules['check-log-call'], {
valid: ['log.log(1, "initialized")();', 'log.assert(gl, "error");', 'someObject.method();'],
invalid: [
{
code: 'log.error("error");',
errors: [
{
line: 1
}
],
output: 'log.error("error")();'
}
]
});

t.end();
});
1 change: 1 addition & 0 deletions test/dev-modules/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import './babel-plugin-inline-gl-constants.spec';
import './babel-plugin-remove-glsl-comments.spec';
import './eslint-plugin-luma-gl-custom-rules.spec';

0 comments on commit d393a32

Please sign in to comment.