Skip to content

Commit

Permalink
Migrate StyleSheet/*.js to use export statements (facebook#48609)
Browse files Browse the repository at this point in the history
Summary:

# Motivation
This is an attempt at modernizing the export syntax in some of the files in `Libraries/StyleSheet/`. It will allow these files to get properly ingested by modern Flow tooling.

# This diff
- Migrates the use of `module.exports` into `export default` for files located in `Libraries/StyleSheet/*.js`. Some files were omitted due to ballooning complexity, but will be addressed in other Diffs.
- Updating internal *require*s to use ".default", no product code seems to be affected.
- Migrating `require`s into `import`s where applicable, taking into account the performance implications (context: https://fb.workplace.com/groups/react.technologies.discussions/permalink/3638114866420225/)
- Updates the current iteration of API snapshots (intended).


Changelog:
[General][Breaking] - Deep imports from some files in `StyleSheet/` can break when using the `require()` syntax, but can be easily fixed by appending `.default`

Differential Revision: D68017325
  • Loading branch information
iwoplaza authored and facebook-github-bot committed Jan 10, 2025
1 parent fd0894b commit 6b7bfda
Show file tree
Hide file tree
Showing 13 changed files with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const resolveAssetSource = require('../Image/resolveAssetSource');
const processBackgroundImage =
require('../StyleSheet/processBackgroundImage').default;
const processColor = require('../StyleSheet/processColor').default;
const processColorArray = require('../StyleSheet/processColorArray');
const processColorArray = require('../StyleSheet/processColorArray').default;
const processFilter = require('../StyleSheet/processFilter').default;
const insetsDiffer = require('../Utilities/differ/insetsDiffer');
const matricesDiffer = require('../Utilities/differ/matricesDiffer');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const _normalizeColorObject = (
// an ios semantic color
return color;
} else if ('dynamic' in color && color.dynamic !== undefined) {
const normalizeColor = require('./normalizeColor');
const normalizeColor = require('./normalizeColor').default;

// a dynamic, appearance aware color
const dynamic = color.dynamic;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

import type {ImageStyleProp, TextStyleProp} from '../StyleSheet';

const StyleSheet = require('../StyleSheet');
import StyleSheet from '../StyleSheet';

const imageStyle = {tintColor: 'rgb(0, 0, 0)'};
const textStyle = {color: 'rgb(0, 0, 0)'};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

'use strict';

const processAspectRatio = require('../processAspectRatio');
import processAspectRatio from '../processAspectRatio';

describe('processAspectRatio', () => {
it('should accept numbers', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

'use strict';

const processTransform = require('../processTransform');
import processTransform from '../processTransform';

describe('processTransform', () => {
describe('validation', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ function normalizeColor(
}
}

module.exports = normalizeColor;
export default normalizeColor;
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ function processAspectRatio(aspectRatio?: number | string): ?number {
return Number(matches[0]);
}

module.exports = processAspectRatio;
export default processAspectRatio;
2 changes: 1 addition & 1 deletion packages/react-native/Libraries/StyleSheet/processColor.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import type {ColorValue, NativeColorValue} from './StyleSheet';

const Platform = require('../Utilities/Platform');
const normalizeColor = require('./normalizeColor');
const normalizeColor = require('./normalizeColor').default;

export type ProcessedColorValue = number | NativeColorValue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ function processColorElement(color: ColorValue): ProcessedColorValue {
return value;
}

module.exports = processColorArray;
export default processColorArray;
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ function processFontVariant(
return match;
}

module.exports = processFontVariant;
export default processFontVariant;
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,4 @@ function _validateTransform(
}
}

module.exports = processTransform;
export default processTransform;
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ function setNormalizedColorAlpha(input: number, alpha: number): number {
return ((input & 0xffffff00) | alpha) >>> 0;
}

module.exports = setNormalizedColorAlpha;
export default setNormalizedColorAlpha;
Original file line number Diff line number Diff line change
Expand Up @@ -8323,7 +8323,7 @@ exports[`public API should not change unintentionally Libraries/StyleSheet/norma
"declare function normalizeColor(
color: ?(ColorValue | ProcessedColorValue)
): ?ProcessedColorValue;
declare module.exports: normalizeColor;
declare export default typeof normalizeColor;
"
`;

Expand Down Expand Up @@ -8371,7 +8371,7 @@ exports[`public API should not change unintentionally Libraries/StyleSheet/priva

exports[`public API should not change unintentionally Libraries/StyleSheet/processAspectRatio.js 1`] = `
"declare function processAspectRatio(aspectRatio?: number | string): ?number;
declare module.exports: processAspectRatio;
declare export default typeof processAspectRatio;
"
`;

Expand Down Expand Up @@ -8421,7 +8421,7 @@ exports[`public API should not change unintentionally Libraries/StyleSheet/proce
"declare function processColorArray(
colors: ?$ReadOnlyArray<ColorValue>
): ?$ReadOnlyArray<ProcessedColorValue>;
declare module.exports: processColorArray;
declare export default typeof processColorArray;
"
`;

Expand Down Expand Up @@ -8453,15 +8453,15 @@ exports[`public API should not change unintentionally Libraries/StyleSheet/proce
"declare function processFontVariant(
fontVariant: ____FontVariantArray_Internal | string
): ?____FontVariantArray_Internal;
declare module.exports: processFontVariant;
declare export default typeof processFontVariant;
"
`;

exports[`public API should not change unintentionally Libraries/StyleSheet/processTransform.js 1`] = `
"declare function processTransform(
transform: Array<Object> | string
): Array<Object> | Array<number>;
declare module.exports: processTransform;
declare export default typeof processTransform;
"
`;

Expand All @@ -8474,7 +8474,7 @@ exports[`public API should not change unintentionally Libraries/StyleSheet/proce

exports[`public API should not change unintentionally Libraries/StyleSheet/setNormalizedColorAlpha.js 1`] = `
"declare function setNormalizedColorAlpha(input: number, alpha: number): number;
declare module.exports: setNormalizedColorAlpha;
declare export default typeof setNormalizedColorAlpha;
"
`;

Expand Down

0 comments on commit 6b7bfda

Please sign in to comment.