Skip to content

Commit

Permalink
Add ESLint (#52)
Browse files Browse the repository at this point in the history
* Fix skipped test for add file to project

* Add ESLint config and fix all the errors

* Use proper lint command

* Function is required here

* This file is unused

* Remove extraneous files and move utils to correct folders

* Add strict mode to global-cli again and use preset for other places

* Revert changes to debugger-ui for browser compatibility

* Revert changes in global-cli, package should run on older Node too
  • Loading branch information
grabbou authored Dec 19, 2018
1 parent 601cbd4 commit 08962ec
Show file tree
Hide file tree
Showing 209 changed files with 1,674 additions and 1,794 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- attach_workspace:
at: ~/react-native-cli
- run: |
# yarn lint – disable temporarily until ESLint is added to the project
yarn lint
yarn flow-check
tests:
<<: *defaults
Expand Down
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
**/node_modules/**
**/debugger-ui/**
**/templates/**
**/global-cli/**
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
],
"scripts": {
"test": "cd packages/local-cli && yarn jest; cd -",
"lint": "eslint packages",
"flow-check": "cd packages/local-cli && yarn flow check; cd -"
},
"devDependencies": {
"@callstack/eslint-config": "^3.0.2",
"eslint": "^5.10.0"
},
"eslintConfig": {
"extends": "@callstack",
"rules": {
"global-require": 0,
"no-console": 0
}
}
}
4 changes: 1 addition & 3 deletions packages/local-cli/__mocks__/beeper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
* @format
*/

'use strict';

// beeper@1.1.0 has a return statement outside of a function
// and therefore doesn't parse. Let's mock it so that we can
// run the tests.
module.exports = function() {};
module.exports = function beeper() {};
19 changes: 8 additions & 11 deletions packages/local-cli/__mocks__/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,34 @@
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/

'use strict';

const path = require('path');
const MemoryFS = require('metro-memory-fs');

let fs;

function setMockFilesystem(object, platform) {
reset(platform);
const root = platform === 'win32' ? 'c:\\' : '/';
mockDir(root, {...object});
mockDir(root, { ...object });
return root;
}

function mockDir(dirPath, desc) {
for (const entName in desc) {
for (const entName of Object.keys(desc)) {
const ent = desc[entName];
const entPath = path.join(dirPath, entName);
if (typeof ent === 'string' || ent instanceof Buffer) {
fs.writeFileSync(entPath, ent);
continue;
continue; // eslint-disable-line no-continue
}
if (typeof ent !== 'object') {
throw new Error(require('util').format('invalid entity:', ent));
}
if (ent.SYMLINK != null) {
fs.symlinkSync(ent.SYMLINK, entPath);
continue;
continue; // eslint-disable-line no-continue
}
fs.mkdirSync(entPath);
mockDir(entPath, ent);
Expand All @@ -43,18 +40,18 @@ function mockDir(dirPath, desc) {
function reset(platform) {
if (path.mock == null) {
throw new Error(
'to use this "fs" module mock, you must also mock the "path" module',
'to use this "fs" module mock, you must also mock the "path" module'
);
}
path.mock.reset(platform);
const cwd = () => (platform === 'win32' ? 'c:\\' : '/');
fs = new MemoryFS({platform, cwd});
fs = new MemoryFS({ platform, cwd });
Object.assign(mockFs, fs);
}

const mockFs = {};
mockFs.__setMockFilesystem = setMockFilesystem;
mockFs.mock = {clear: reset};
mockFs.mock = { clear: reset };

reset('posix');

Expand Down
4 changes: 1 addition & 3 deletions packages/local-cli/__mocks__/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
* @format
*/

'use strict';

const mockPath = {};

function reset(platform) {
Object.assign(mockPath, jest.requireActual('path')[platform]);
}

mockPath.mock = {reset};
mockPath.mock = { reset };

reset('posix');

Expand Down
6 changes: 2 additions & 4 deletions packages/local-cli/babel.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const path = require('path');
const escapeRegExp = require('escape-string-regexp');

module.exports = {
Expand All @@ -12,7 +11,6 @@ module.exports = {
],
require.resolve('@babel/preset-flow'),
],
only: [
new RegExp('^' + escapeRegExp(__dirname))
]
plugins: [require.resolve('@babel/plugin-transform-strict-mode')],
only: [new RegExp(`^${escapeRegExp(__dirname)}`)],
};
4 changes: 2 additions & 2 deletions packages/local-cli/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ if (isInstalledGlobally()) {
console.error(
[
chalk.red(
'Looks like you installed react-native globally, maybe you meant react-native-cli?',
'Looks like you installed react-native globally, maybe you meant react-native-cli?'
),
chalk.red('To fix the issue, run:'),
'npm uninstall -g react-native',
'npm install -g react-native-cli',
].join('\n'),
].join('\n')
);
process.exit(1);
} else {
Expand Down
2 changes: 0 additions & 2 deletions packages/local-cli/bundle/__mocks__/sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
* @format
*/

'use strict';

function sign(source) {
return source;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
* @emails oncall+javascript_foundation
*/

'use strict';

jest.dontMock('../filterPlatformAssetScales').dontMock('../assetPathUtils');

const filterPlatformAssetScales = require('../filterPlatformAssetScales');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@
* @emails oncall+javascript_foundation
*/

'use strict';

jest.dontMock('../getAssetDestPathAndroid').dontMock('../assetPathUtils');

const getAssetDestPathAndroid = require('../getAssetDestPathAndroid');

const path = require('path');
const getAssetDestPathAndroid = require('../getAssetDestPathAndroid');

describe('getAssetDestPathAndroid', () => {
it('should use the right destination folder', () => {
Expand All @@ -24,10 +21,10 @@ describe('getAssetDestPathAndroid', () => {
httpServerLocation: '/assets/test',
};

const expectDestPathForScaleToStartWith = (scale, path) => {
if (!getAssetDestPathAndroid(asset, scale).startsWith(path)) {
const expectDestPathForScaleToStartWith = (scale, location) => {
if (!getAssetDestPathAndroid(asset, scale).startsWith(location)) {
throw new Error(
`asset for scale ${scale} should start with path '${path}'`,
`asset for scale ${scale} should start with path '${location}'`
);
}
};
Expand All @@ -47,7 +44,7 @@ describe('getAssetDestPathAndroid', () => {
};

expect(getAssetDestPathAndroid(asset, 1)).toBe(
path.normalize('drawable-mdpi/app_test_icon.png'),
path.normalize('drawable-mdpi/app_test_icon.png')
);
});

Expand All @@ -69,7 +66,7 @@ describe('getAssetDestPathAndroid', () => {
};

expect(getAssetDestPathAndroid(asset, 1)).toBe(
path.normalize('raw/app_test_video.mp4'),
path.normalize('raw/app_test_video.mp4')
);
});
});
10 changes: 4 additions & 6 deletions packages/local-cli/bundle/__tests__/getAssetDestPathIOS-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
* @emails oncall+javascript_foundation
*/

'use strict';

jest.dontMock('../getAssetDestPathIOS');

const getAssetDestPathIOS = require('../getAssetDestPathIOS');
const path = require('path');
const getAssetDestPathIOS = require('../getAssetDestPathIOS');

describe('getAssetDestPathIOS', () => {
it('should build correct path', () => {
Expand All @@ -24,7 +22,7 @@ describe('getAssetDestPathIOS', () => {
};

expect(getAssetDestPathIOS(asset, 1)).toBe(
path.normalize('assets/test/icon.png'),
path.normalize('assets/test/icon.png')
);
});

Expand All @@ -36,10 +34,10 @@ describe('getAssetDestPathIOS', () => {
};

expect(getAssetDestPathIOS(asset, 2)).toBe(
path.normalize('assets/test/icon@2x.png'),
path.normalize('assets/test/icon@2x.png')
);
expect(getAssetDestPathIOS(asset, 3)).toBe(
path.normalize('assets/test/icon@3x.png'),
path.normalize('assets/test/icon@3x.png')
);
});
});
28 changes: 14 additions & 14 deletions packages/local-cli/bundle/assetPathUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
* @flow strict
*/

'use strict';

export type PackagerAsset = {
+httpServerLocation: string,
+name: string,
Expand All @@ -34,8 +32,9 @@ function getAndroidAssetSuffix(scale: number): string {
return 'xxhdpi';
case 4:
return 'xxxhdpi';
default:
throw new Error('no such scale');
}
throw new Error('no such scale');
}

// See https://developer.android.com/guide/topics/resources/drawable-resource.html
Expand All @@ -53,37 +52,38 @@ function getAndroidResourceFolderName(asset: PackagerAsset, scale: number) {
if (!drawableFileTypes.has(asset.type)) {
return 'raw';
}
var suffix = getAndroidAssetSuffix(scale);
const suffix = getAndroidAssetSuffix(scale);
if (!suffix) {
throw new Error(
"Don't know which android drawable suffix to use for asset: " +
JSON.stringify(asset),
`Don't know which android drawable suffix to use for asset: ${JSON.stringify(
asset
)}`
);
}
const androidFolder = 'drawable-' + suffix;
const androidFolder = `drawable-${suffix}`;
return androidFolder;
}

function getAndroidResourceIdentifier(asset: PackagerAsset) {
var folderPath = getBasePath(asset);
return (folderPath + '/' + asset.name)
const folderPath = getBasePath(asset);
return `${folderPath}/${asset.name}`
.toLowerCase()
.replace(/\//g, '_') // Encode folder structure in file name
.replace(/([^a-z0-9_])/g, '') // Remove illegal chars
.replace(/^assets_/, ''); // Remove "assets_" prefix
}

function getBasePath(asset: PackagerAsset) {
var basePath = asset.httpServerLocation;
let basePath = asset.httpServerLocation;
if (basePath[0] === '/') {
basePath = basePath.substr(1);
}
return basePath;
}

module.exports = {
getAndroidAssetSuffix: getAndroidAssetSuffix,
getAndroidResourceFolderName: getAndroidResourceFolderName,
getAndroidResourceIdentifier: getAndroidResourceIdentifier,
getBasePath: getBasePath,
getAndroidAssetSuffix,
getAndroidResourceFolderName,
getAndroidResourceIdentifier,
getBasePath,
};
18 changes: 10 additions & 8 deletions packages/local-cli/bundle/buildBundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,29 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/

'use strict';
import type { ContextT } from '../core/types.flow';
import type { CommandLineArgs } from './bundleCommandLineArgs';

const log = require('../util/log').out('bundle');
const Server = require('metro/src/Server');

const outputBundle = require('metro/src/shared/output/bundle');
const path = require('path');
const log = require('./log').out('bundle');
const saveAssets = require('./saveAssets');

const loadMetroConfig = require('../util/loadMetroConfig');

import type { ContextT } from '../core/types.flow';
import type { CommandLineArgs } from './bundleCommandLineArgs';

async function buildBundle(args: CommandLineArgs, ctx: ContextT, output = outputBundle) {
async function buildBundle(
args: CommandLineArgs,
ctx: ContextT,
output = outputBundle
) {
const config = await loadMetroConfig(ctx.root, {
resetCache: args.resetCache,
config: args.config
config: args.config,
});

// This is used by a bazillion of npm modules we don't control so we don't
Expand Down
3 changes: 0 additions & 3 deletions packages/local-cli/bundle/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@
* @format
*/

'use strict';

const buildBundle = require('./buildBundle');
const bundleCommandLineArgs = require('./bundleCommandLineArgs');
const outputBundle = require('metro/src/shared/output/bundle');

/**
* Builds the bundle starting to look for dependencies at the given entry path.
Expand Down
Loading

0 comments on commit 08962ec

Please sign in to comment.