Skip to content

Commit

Permalink
fix: don't escape HTML in templates
Browse files Browse the repository at this point in the history
When using `<%= code %>` in `ejs`, it escapes characters by default. This is useful when writing HTML, but in our case, it may sometimes lead to undesirable behavior.

For example, if you generate a project with the description "Satya's cool library", it would escape `'` and output: "Satya&#39 cool library".

This commit swaps the templates to use `<%- code %>` so that nothing is escaped.
  • Loading branch information
satya164 committed Dec 16, 2020
1 parent fc0b524 commit 99d23f6
Show file tree
Hide file tree
Showing 63 changed files with 245 additions and 245 deletions.
16 changes: 8 additions & 8 deletions templates/common/$package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "<%= project.slug %>",
"name": "<%- project.slug %>",
"version": "0.1.0",
"description": "<%= project.description %>",
"description": "<%- project.description %>",
"main": "lib/commonjs/index",
"module": "lib/module/index",
"types": "lib/typescript/src/index.d.ts",
Expand All @@ -13,7 +13,7 @@
"android",
"ios",
"cpp",
"<%= project.podspec %>.podspec",
"<%- project.podspec %>.podspec",
"!lib/typescript/example",
"!android/build",
"!ios/build",
Expand All @@ -36,19 +36,19 @@
"ios",
"android"
],
"repository": "<%= repo %>",
"author": "<%= author.name %> <<%= author.email %>> (<%= author.url %>)",
"repository": "<%- repo %>",
"author": "<%- author.name %> <<%- author.email %>> (<%- author.url %>)",
"license": "MIT",
"bugs": {
"url": "<%= repo %>/issues"
"url": "<%- repo %>/issues"
},
"homepage": "<%= repo %>#readme",
"homepage": "<%- repo %>#readme",
"publishConfig": {
"registry": "https://registry.npmjs.org/"
},
"devDependencies": {
"@commitlint/config-conventional": "^8.3.4",
"@react-native-community/bob": "^<%= bob.version %>",
"@react-native-community/bob": "^<%- bob.version %>",
"@react-native-community/eslint-config": "^2.0.0",
"@release-it/conventional-changelog": "^1.1.4",
"@types/jest": "^26.0.0",
Expand Down
4 changes: 2 additions & 2 deletions templates/common/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ Remember to add tests for your change if possible. Run the unit tests by:
yarn test
```
<% if (project.native) { %>
To edit the Objective-C files, open `example/ios/<%= project.name %>Example.xcworkspace` in XCode and find the source files at `Pods > Development Pods > <%= project.slug %>`.
To edit the Objective-C files, open `example/ios/<%- project.name %>Example.xcworkspace` in XCode and find the source files at `Pods > Development Pods > <%- project.slug %>`.

To edit the Kotlin files, open `example/android` in Android studio and find the source files at `<%= project.package %>` under `Android`.
To edit the Kotlin files, open `example/android` in Android studio and find the source files at `<%- project.package %>` under `Android`.
<% } %>
### Commit message convention

Expand Down
2 changes: 1 addition & 1 deletion templates/common/LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 <%= author.name %>
Copyright (c) 2020 <%- author.name %>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
10 changes: 5 additions & 5 deletions templates/common/README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# <%= project.slug %>
# <%- project.slug %>

<%= project.description %>
<%- project.description %>

## Installation

```sh
npm install <%= project.slug %>
npm install <%- project.slug %>
```

## Usage

```js
import <%= project.name %> from "<%= project.slug %>";
import <%- project.name %> from "<%- project.slug %>";

// ...

const result = await <%= project.name %>.multiply(3, 7);
const result = await <%- project.name %>.multiply(3, 7);
```

## Contributing
Expand Down
12 changes: 6 additions & 6 deletions templates/common/example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import * as React from 'react';
<% if (project.moduleType === "view") { %>
import { StyleSheet, View } from 'react-native';
import <%= project.name %>ViewManager from '<%= project.slug %>';
import <%- project.name %>ViewManager from '<%- project.slug %>';
<% } else {%>
import { StyleSheet, View, Text } from 'react-native';
import <%= project.name %> from '<%= project.slug %>';
import <%- project.name %> from '<%- project.slug %>';
<% }%>

<% if (project.moduleType === "view") { %>
export default function App() {
return (
<View style={styles.container}>
<<%= project.name %>ViewManager color="#32a852" style={styles.box} />
<<%- project.name %>ViewManager color="#32a852" style={styles.box} />
</View>
);
}
Expand All @@ -20,15 +20,15 @@ export default function App() {
const [result, setResult] = React.useState<number | undefined>();

React.useEffect(() => {
<%= project.name %>.multiply(3, 7).then(setResult);
<%- project.name %>.multiply(3, 7).then(setResult);
}, []);

return (
<View style={styles.container}>
<Text>Result: {result}</Text>
</View>
);
}
}
<% }%>

const styles = StyleSheet.create({
Expand All @@ -42,4 +42,4 @@ const styles = StyleSheet.create({
height: 60,
marginVertical: 20,
},
});
});
2 changes: 1 addition & 1 deletion templates/common/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"baseUrl": ".",
"paths": {
"<%= project.slug %>": ["./src/index"]
"<%- project.slug %>": ["./src/index"]
},
"allowUnreachableCode": false,
"allowUnusedLabels": false,
Expand Down
2 changes: 1 addition & 1 deletion templates/cpp-library/android/cpp-adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

extern "C"
JNIEXPORT jint JNICALL
Java_com_<%= project.package %>_<%= project.name %>Module_nativeMultiply(JNIEnv *env, jclass type, jint a, jint b) {
Java_com_<%- project.package %>_<%- project.name %>Module_nativeMultiply(JNIEnv *env, jclass type, jint a, jint b) {
return example::multiply(a, b);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

#endif

@interface <%= project.name %> : NSObject <RCTBridgeModule>
@interface <%- project.name %> : NSObject <RCTBridgeModule>

@end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#import "<%= project.name %>.h"
#import "<%- project.name %>.h"

@implementation <%= project.name %>
@implementation <%- project.name %>

RCT_EXPORT_MODULE()

Expand Down
4 changes: 2 additions & 2 deletions templates/example/example/$package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "<%= project.slug %>-example",
"description": "Example app for <%= project.slug %>",
"name": "<%- project.slug %>-example",
"description": "Example app for <%- project.slug %>",
"version": "0.0.1",
"private": true,
"scripts": {
Expand Down
10 changes: 5 additions & 5 deletions templates/example/example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import com.android.build.OutputFile
* // bundleInBeta: true,
*
* // whether to disable dev mode in custom build variants (by default only disabled in release)
* // for <%= project.name %>Example: to disable dev mode in the staging build type (if configured)
* // for <%- project.name %>Example: to disable dev mode in the staging build type (if configured)
* devDisabledInStaging: true,
* // The configuration property can be in the following formats
* // 'devDisabledIn${productFlavor}${buildType}'
Expand All @@ -64,7 +64,7 @@ import com.android.build.OutputFile
* // that we don't look at files in android/ or ios/ to determine whether the tasks are up to
* // date; if you have any other folders that you want to ignore for performance reasons (gradle
* // indexes the entire tree), add them here. Alternatively, if you have JS files in android/
* // for <%= project.name %>Example, you might want to remove it from here.
* // for <%- project.name %>Example, you might want to remove it from here.
* inputExcludes: ["android/**", "ios/**"],
*
* // override which node gets called and with what additional arguments
Expand Down Expand Up @@ -99,7 +99,7 @@ def enableProguardInReleaseBuilds = false
/**
* The preferred build flavor of JavaScriptCore.
*
* For <%= project.name %>Example, to use the international variant, you can use:
* For <%- project.name %>Example, to use the international variant, you can use:
* `def jscFlavor = 'org.webkit:android-jsc-intl:+'`
*
* The international variant includes ICU i18n library and necessary data
Expand Down Expand Up @@ -127,7 +127,7 @@ android {
}

defaultConfig {
applicationId "com.example.<%= project.package %>"
applicationId "com.example.<%- project.package %>"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
Expand Down Expand Up @@ -210,7 +210,7 @@ dependencies {
}

<% if (project.module) { -%>
implementation project(':<%= project.package %>')
implementation project(':<%- project.package %>')
<% } -%>
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* <p>This source code is licensed under the MIT license found in the LICENSE file in the root
* directory of this source tree.
*/
package com.example.<%= project.package %>;
package com.example.<%- project.package %>;

import android.content.Context;
import com.facebook.flipper.android.AndroidFlipperClient;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.<%= project.package %>">
package="com.example.<%- project.package %>">

<uses-permission android:name="android.permission.INTERNET" />

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.<%= project.package %>;
package com.example.<%- project.package %>;

import com.facebook.react.ReactActivity;

Expand All @@ -10,6 +10,6 @@ public class MainActivity extends ReactActivity {
*/
@Override
protected String getMainComponentName() {
return "<%= project.name %>Example";
return "<%- project.name %>Example";
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.<%= project.package %>;
package com.example.<%- project.package %>;

import android.app.Application;
import android.content.Context;
Expand All @@ -11,7 +11,7 @@
import java.lang.reflect.InvocationTargetException;
import java.util.List;
<% if (project.module) { -%>
import com.<%= project.package %>.<%= project.name %>Package;
import com.<%- project.package %>.<%- project.name %>Package;
<% } -%>

public class MainApplication extends Application implements ReactApplication {
Expand All @@ -27,9 +27,9 @@ public boolean getUseDeveloperSupport() {
protected List<ReactPackage> getPackages() {
@SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
// Packages that cannot be autolinked yet can be added manually here, for <%= project.name %>Example:
// Packages that cannot be autolinked yet can be added manually here, for <%- project.name %>Example:
// packages.add(new MyReactNativePackage());
<% if (project.module) { -%>packages.add(new <%= project.name %>Package());<% } -%>
<% if (project.module) { -%>packages.add(new <%- project.name %>Package());<% } -%>

return packages;
}
Expand Down Expand Up @@ -64,7 +64,7 @@ private static void initializeFlipper(Context context, ReactInstanceManager reac
We use reflection here to pick up the class that initializes Flipper,
since Flipper library is not available in release mode
*/
Class<?> aClass = Class.forName("com.<%= project.package %>Example.ReactNativeFlipper");
Class<?> aClass = Class.forName("com.<%- project.package %>Example.ReactNativeFlipper");
aClass
.getMethod("initializeFlipper", Context.class, ReactInstanceManager.class)
.invoke(null, context, reactInstanceManager);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<resources>
<string name="app_name"><%= project.name %> Example</string>
<string name="app_name"><%- project.name %> Example</string>
</resources>
6 changes: 3 additions & 3 deletions templates/example/example/android/settings.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
rootProject.name = '<%= project.name %>Example'
rootProject.name = '<%- project.name %>Example'
apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings)
include ':app'

<% if (project.module) { -%>
include ':<%= project.package %>'
project(':<%= project.package %>').projectDir = new File(rootProject.projectDir, '../../android')
include ':<%- project.package %>'
project(':<%- project.package %>').projectDir = new File(rootProject.projectDir, '../../android')
<% } -%>
4 changes: 2 additions & 2 deletions templates/example/example/app.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "<%= project.name %>Example",
"displayName": "<%= project.name %> Example"
"name": "<%- project.name %>Example",
"displayName": "<%- project.name %> Example"
}
2 changes: 1 addition & 1 deletion templates/example/example/ios/File.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// File.swift
// <%= project.name %>Example
// <%- project.name %>Example
//

import Foundation
6 changes: 3 additions & 3 deletions templates/example/example/ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def flipper_post_install(installer)
end
end

target '<%= project.name %>Example' do
# Pods for <%= project.name %>Example
target '<%- project.name %>Example' do
# Pods for <%- project.name %>Example
pod 'FBLazyVector', :path => "../node_modules/react-native/Libraries/FBLazyVector"
pod 'FBReactNativeSpec', :path => "../node_modules/react-native/Libraries/FBReactNativeSpec"
pod 'RCTRequired', :path => "../node_modules/react-native/Libraries/RCTRequired"
Expand Down Expand Up @@ -54,7 +54,7 @@ target '<%= project.name %>Example' do
pod 'Folly', :podspec => '../node_modules/react-native/third-party-podspecs/Folly.podspec'

<% if (project.module) { -%>
pod '<%= project.podspec %>', :path => '../..'
pod '<%- project.podspec %>', :path => '../..'
<% } -%>
use_native_modules!
Expand Down
Loading

0 comments on commit 99d23f6

Please sign in to comment.