Skip to content

Commit

Permalink
Check java keywords when init new project (#873)
Browse files Browse the repository at this point in the history
* Check java keywords when init new project

* Update validate.ts

Co-authored-by: Mike Grabowski <grabbou@gmail.com>
  • Loading branch information
sunnylqm and grabbou committed Feb 5, 2020
1 parent 98b79af commit 2de24bd
Showing 1 changed file with 58 additions and 1 deletion.
59 changes: 58 additions & 1 deletion packages/cli/src/commands/init/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,69 @@ import HelloWorldError from './errors/HelloWorldError';

const NAME_REGEX = /^[$A-Z_][0-9A-Z_$]*$/i;

// ref: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html
const javaKeywords = [
'abstract',
'continue',
'for',
'new',
'switch',
'assert',
'default',
'goto',
'package',
'synchronized',
'boolean',
'do',
'if',
'private',
'this',
'break',
'double',
'implements',
'protected',
'throw',
'byte',
'else',
'import',
'public',
'throws',
'case',
'enum',
'instanceof',
'return',
'transient',
'catch',
'extends',
'int',
'short',
'try',
'char',
'final',
'interface',
'static',
'void',
'class',
'finally',
'long',
'strictfp',
'volatile',
'const',
'float',
'native',
'super',
'while',
];

const reservedNames = ['react', 'react-native', ...javaKeywords];

export function validateProjectName(name: string) {
if (!String(name).match(NAME_REGEX)) {
throw new InvalidNameError(name);
}

if (name === 'React' || name === 'react') {
const lowerCaseName = name.toLowerCase();
if (reservedNames.includes(lowerCaseName)) {
throw new ReservedNameError();
}

Expand Down

0 comments on commit 2de24bd

Please sign in to comment.